User:Inductiveload/addVIAFdata.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/* This tool is now a gadget and can be enabled via Special:Preferences
 * or by placing mw.loader.load('ext.gadget.VIAFDataImporter'); in your
 * personal Javascript.
 */

/**
 * Gadget that loads [[MediaWiki:VIAFDataImporter.js]]
 *   (written by [[User:Inductiveload]])
 * after clicking the according button
 *
 * Help at [[Help:Gadget-VIAFDataImporter]]
 *
 * @rev 1 (2012-06-08)
 * @author Rillke, 2012
 *
 */

/*global jQuery:false, mediaWiki:false*/
/*jshint curly:false*/

(function($, mw) {
	'use strict';

	if ($.inArray(mw.config.get('wgNamespaceNumber'), [0, 2, 14, 100, 106]) === -1) return;

	var ii, // ImporterInstance
		waiting = false,
		portlet = mw.util.addPortletLink('p-tb', '#', 'Add authority control', 'tb-viaf-authority-control', 'Add an authority control template using data from VIAF'),
		scriptLoc = 'MediaWiki:VIAFDataImporter.js';

	var _onScriptLoad = function(evt, st, instance) {
		if ('VIAFDataImporter' !== st || !instance) return;
		ii = instance;
		waiting = false;
		$(document).unbind('scriptLoaded.VIAFLoader');
		_loadVIAFData();
	};
	var _loadVIAFData = function(e) {
		if (e) e.preventDefault();
		if (waiting) return;
		if (ii) {
			ii.add_VIAF_pane();
			$('html,body').stop().animate({scrollTop: $('#add_viaf_data_pane').position().top + mw.util.$content.position().top}, 500);
		} else {
			waiting = true;
			$(document).bind('scriptLoaded.VIAFLoader', _onScriptLoad);
			mw.loader.load( mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?title=' + scriptLoc + '&action=raw&ctype=text/javascript&maxage=2419200&smaxage=2419200&dummy=0' );
		}
	};
	$(portlet).click(_loadVIAFData);

})(jQuery, mediaWiki);