User:Bargioni/personal sort identifiers.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Show identifiers ordered on a personal basis

// Please, predefine a global variable named identifiers_preferred_sort, eg: 
// identifiers_preferred_sort = new Array('P214','P213','P244','others');
// to have VIAF, ISNI, LCNAF at the top of the identifiers list, and sort others by P ascending number;
// default is 
// identifiers_preferred_sort = new Array('others');

// FOR DEVELOPERS: this script could be enhanced: "others" could also be:
// others by descending P number
// others by asc|desc labels (in the current language)
// others hide (adding a button to toggle them)
// others remove

$( function($) {
	// check if we're viewing an item
	let qid = mw.config.get( 'wbEntityId' );
	if ( !qid ) {
		return;
	}

	var retries = 0;
	var max_retries = 20;
	if( typeof(identifiers_preferred_sort) == "undefined" ) {
		identifiers_preferred_sort = new Array('others');
	}

	function weight(x,identifiers_preferred_sort) {
		var p;
		p = identifiers_preferred_sort.indexOf(x); 
		if (p > -1) return p;
		p = identifiers_preferred_sort.indexOf('others'); 
		if (p > -1) {
			p = p === 0 ? 1 : p;
			var property_number = x.replace(/^P/,''); // the string after P
			return property_number * p;
		}
	}
	
	function sorter(a,b) {
		var weight_a = weight(a,identifiers_preferred_sort);
		var weight_b = weight(b,identifiers_preferred_sort);
		return weight_a - weight_b;
	}
	
	function manage_identifiers(){
		var ids = $('.wikibase-statementgrouplistview>div').get(2); // the block containing identifiers
		var div_ids = $(ids).find('>div'); // the divs of each identifier
		if (div_ids.length > 0) {
			// now we have at least one identifier
			// console.log('Found '+div_ids.length+' identifiers'); // test
			if (div_ids.length > 1) {
				// we have more than 1 identifier
				var Ps = div_ids.map(function(i,e){return $(e).attr('id')});
				Ps = Ps.toArray();
				// console.log('Identifiers before sorting: ',Ps); // test
				Ps = Ps.sort(sorter);
				// console.log('Identifiers after sorting : ',Ps); // test
				$('#identifiers').append('<span id="msg_sorted"> sorted</span>');
				$('#msg_sorted').css({"color":"gray","font-size":"0.8em"});
				// moving divs
				for (var i = 0; i < Ps.length; i++) {
					var P = Ps[i]; // e.g. P227
					var E = $('#'+P).detach();
					if (i === 0) {
						$(ids).append(E);
					}
					else {
						$('#'+Ps[i-1]).after(E);
					}
				}
			}
		}
		else {
			// console.log('waiting for identifiers... try again'); // test
			if (retries++ < max_retries) setTimeout(manage_identifiers, 100);
		}
	}
	
	if ($('#identifiers').length) {
		// we have identifiers, but we have to wait for them
		setTimeout(manage_identifiers, 100);
	}
});