Jump to content

User:Obiwankenobi/intercat.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
USAGE : Add the following line to your [[Special:Mypage/common.js|common.js page]]:
importScript('User:Obiwankenobi/intercat.js');

Then, add category intersection tags to pages, like:
<div class='intercat'>American men|American writers</div>
or, to auto-open the first 100, like:
<div class='intercat_open'>American men|American writers</div>
You can add as many categories as you want, separated by "|".
To use a different namespace, add the correct code to the front before the categories, separated by ";".
<div class='intercat_open'>2;Wikipedians_in_Cambridgeshire|User en</div>

Namespace 0 = Mainspace
Namespace 1 = Mainspace talk
Namespace 2 = Userspace
Namespace 3 = User_talk
Namespace 4 = Wikipedia
Namespace 5 = Wikipedia_talk
*/

var intercat = {
	e : [] ,
	max : 200 ,
	columns : 3 ,
	
	init : function () {
		var me = this ;
		me.page = mw.config.get('wgPageName') ;
		me.server = mw.config.get('wgServer').match ( /^\/*([^.]+)\.([^.]+)/ ) ;
		$('div.intercat,div.intercat_open').each ( function () {
			var o = $(this) ;
                        var cats;
                        var ns;
                        var nsarray = o.text().split(';'); // split off namespace ID if present
                        if (nsarray.length == 1) {
                                cats = nsarray[0];
                                ns=0; //default to mainspace
                        } else if (nsarray.length == 2) {
                                ns = nsarray[0];
                                cats = nsarray[1];
                        } else {
                                alert("Too many semicolons!");
                        }
			cats = cats.split('|') ; // now split the cats
                        
                        var h = '';
			if (cats.length > 1) {
                                h = "<div>Intersection of " ; //different display logic if we're intersecting vs enumerating
                        } else {
                                h = "<div>Recursive enumeration of all pages in " ;
                        }
			$.each ( cats , function ( k , v ) {
				if ( k > 0 ) h += " &times; " ;
				h += "<a href='/wiki/Category:" + escape ( v.replace(/ /g,'_') ) + "'>" + v.replace(/_/g,' ') + "</a> " ;
			} ) ;
			h += " | <span class='intercat_sh'><a href='#'>Show</a></span>" ;
			h += "<span class='intercat_loading' style='margin-left:20px;display:none'><i>Loading...</i></span>" ;
			h += "</div><div class='intercat_display' style='display:none'></div>" ;
			o.html ( h ) . css ( { border:'1px solid #DDDDDD' , padding:'2px' } ) ;
			var display = $(o.find('div.intercat_display')) ;
			var my_e = me.e.length ;
			me.e.push ( { o:o , cats:cats , loaded:{} , data:{} , offset:0, ns:ns, pagecount:0, total_results:0 } ) ;
			$(o.find('span.intercat_sh a')).click ( function () {
				var h = display.is(':visible') ? 'Show' : 'Hide' ;
				$(this).text ( h ) ;
				display.toggle() ;
				if ( display.is(':visible') ) me.show ( my_e ) ;
				return false ;
			} ) ;
		} ) ;
                // auto-load all of the auto-open cats
		$.each ( me.e , function ( my_e , v ) {
			if ( v.o.hasClass('intercat_open') ) {
				$(v.o.find('span.intercat_sh a')).click() ;
			}
		} ) ;
	} ,
	
	load : function ( my_e ) {
		var me = this ;
		$(me.e[my_e].o.find('.intercat_loading')).show() ;
		$.getJSON ( '//tools.wmflabs.org/catscan2/quick_intersection.php?callback=?' , {
			lang:me.server[1] ,
			project:me.server[2] ,
			cats:me.e[my_e].cats.join("\n") ,
			start:me.e[my_e].offset ,
			ns:me.e[my_e].ns,
			format:'json',
			sparse:1,
			group_by_key:1,
			get_count:1,
			max:me.max
		} , function ( d ) {
			me.e[my_e].data[me.e[my_e].offset] = d.pages ;
			me.e[my_e].loaded[me.e[my_e].offset] = d.pagecount ;
			me.e[my_e].pagecount = d.pagecount;
			me.e[my_e].total_results = d.total_results ;
			$(me.e[my_e].o.find('.intercat_loading')).hide() ;
			me.show ( my_e ) ;
		} ) ;
	} ,
	
	getRangeLink : function ( my_e , offset ) {
		var me = this ;
		if ( me.e[my_e].offset == offset ) return "<b>" + (offset+1) + "&mdash;" + (offset+me.max) + "</b>" ;
		return "<a href='#' onclick='intercat.e["+my_e+"].offset="+offset+";intercat.show("+my_e+");return false;'>" + (offset+1) + "&mdash;" + (offset+me.max) + "</a>" ;
	} ,
	
	show : function ( my_e ) {
		var me = this ;
		var off = me.e[my_e].offset ;
		if ( undefined === me.e[my_e].loaded[off] ) return me.load  ( my_e ) ;

                // iterate over all other intersects and close them
		$.each ( me.e , function ( my_e , v ) {
			if ( v != my_e ) {
                             // this doesn't work - can you make this work?   
			     // $(me.e[v].o.hide())	 ;
			}
		} ) ;
		var navbar = [] ;
		for ( var p = 0 ; p < off ; p += me.max ) {
			navbar.push ( me.getRangeLink ( my_e , p ) ) ;
		}
		navbar.push ( me.getRangeLink ( my_e , off ) ) ;
		if ( me.e[my_e].data[off].length == me.max ) {
			navbar.push ( me.getRangeLink ( my_e , off+me.max ) ) ;
			navbar.push ( '...' ) ;
		}
		var intersectOrEnum = '';
                if (me.e[my_e].cats.length > 1) {
                        intersectOrEnum = 'intersection';
                } else {
                        intersectOrEnum = 'category and all sub-categories';
                }

		var h = '' ;
                h += "<p>The following " + me.e[my_e].pagecount + " pages are in this " + intersectOrEnum + " out of " + me.e[my_e].total_results + " total. This list may not reflect recent changes (<a href=\"/wiki/Wikipedia:FAQ/Categories#Why_might_a_category_list_not_be_up_to_date.3F\" title=\"Wikipedia:FAQ/Categories\">learn more</a>). </p>" ;
		h += "<div style='border-bottom:1px solid #DDDDDD;'>" + navbar.join(" | ") + "</div>" ; 
		h += "<ul style='-moz-column-count:"+me.columns+";-webkit-column-count:"+me.columns+";column-count:"+me.columns+";list-style-position: inside;' start='" + (off+1) + "'>" ;
		$.each ( me.e[my_e].data[off] , function ( letter , list ) {
			h += "<h3>" + letter + "</h3>" ;
			$.each ( list ,	function ( k , v ) {
				h += "<li><a href='/wiki/" + v + "'>" ;
				h += v.replace(/_/g,' ') ;
				h += "</a></li>" ;
			} ) ;
			
		} ) ;
		h += "</ul>" ;
		h += "<div style='border-top:1px solid #DDDDDD;'>" + navbar.join(" | ") + "</div>" ; ;
		
		$(me.e[my_e].o.find('div.intercat_display')).html ( h ) ;
	} ,
	
	the_end : ''
} ;

$ ( function() {
	if ( mw.config.get('wgAction') != 'view' ) return ;
	intercat.init() ;
} ) ;