User:Bargioni/hl mixnmatch pusc.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.
// highlight PUSC aux data in mix-n-match panel in case of a match on one of them
$( function($) {
	setTimeout(hl,3000); // waiting for mnm table to load - is there a better way?
});
function hl() {
	var mnm_rows = $('#mnm_entries td').filter(function(i,e){if ($(e).text().indexOf('PUSC')>-1) return $(e)});
	if (mnm_rows.length == 1) {
		// PUSC row of mix-n-match found
		var pusc_row = mnm_rows['0'];
		var pusc_aux_cell = $(pusc_row).parent().find('small');
		var pusc_aux = pusc_aux_cell.text();
		var aux = JSON.parse(pusc_aux); 
		// example: {"isni":"0000000066416384","viaf":"48152366","deathdate":"2016","birthdate":"1941"}
		var item_properties = new Object({});
		item_properties.birthdate = $('#P569 div.wikibase-statementview-mainsnak').text().trim();
		// birth dates match on year only
		if (item_properties.birthdate) item_properties.birthdate = year(item_properties.birthdate);
		if (aux.birthdate) aux.birthdate = new Date(aux.birthdate).getFullYear();
		item_properties.deathdate = $('#P570 div.wikibase-statementview-mainsnak').text().trim();
		// death dates match on year only
		if (item_properties.deathdate) item_properties.deathdate = year(item_properties.deathdate);
		if (aux.deathdate) aux.deathdate = new Date(aux.deathdate).getFullYear();
		item_properties.isni = $('#P213 div.wikibase-statementview-mainsnak').text().trim().replace(/ /g,'');
		item_properties.viaf = $('#P214 div.wikibase-statementview-mainsnak').text().trim();
		// highlight matches in PUSC aux data
		var matched_props = new Array();
		for	(var prop in aux) {
			if (aux[prop] && item_properties[prop]) {
				if (aux[prop] == item_properties[prop]) {
		 			matched_props.push(prop);
				}
			}
		}
		if (matched_props.length) {
			pusc_aux_cell.wrap('<span style="background-color:yellow">');
			pusc_aux_cell.append('<br>'+(matched_props.join(', ')));
		}
	}
}

function year(s) {
	var parts = s.split(/ /); // 1 apr 2019 | aprile 2019 | 2019
	return parts[parts.length-1];
}