MediaWiki:Common.js: Difference between revisions

From DataTrek
Jump to navigation Jump to search
m (Parameter)
No edit summary
Line 2: Line 2:


/*Code from https://www.mediawiki.org/w/index.php?title=User:TheDJ/common.js&oldid=2187846*/
/*Code from https://www.mediawiki.org/w/index.php?title=User:TheDJ/common.js&oldid=2187846*/
function sideJSONMessage(idName) {
function sideJSONMessage(messageName) {
console.log("Entering " + idName);
console.log("Entering " + messageName);
jQuery.when(mw.loader.using(['mediawiki.api', 'mediawiki.jqueryMsg']), jQuery.ready ).done( function() {
jQuery.when(mw.loader.using(['mediawiki.api', 'mediawiki.jqueryMsg']), jQuery.ready ).done( function() {
console.log("Entered");
console.log("Entered");
new mediaWiki.Api().loadMessagesIfMissing([idName]).done( function() {
new mediaWiki.Api().loadMessagesIfMissing([messageName, 'tooltip-' + messageName]).done( function() {
AddJSONLinkToSidebar(idName);
AddJSONLinkToSidebar(messageName);
});
});
});
});
Line 48: Line 48:
aNode.setAttribute( 'href', link );
aNode.setAttribute( 'href', link );
aNode.setAttribute( 'title', mw.message('tooltip-' + idName).text());
liNode.appendChild( aNode );
liNode.appendChild( aNode );
/*liNode.className = 'plainlinks';*/
/*liNode.className = 'plainlinks';*/
Line 59: Line 60:
}
}


function AddJSONLinkToSidebar(idName) {
function AddJSONLinkToSidebar(messageName) {
/*Returns an array containing the variables requested.
/*Returns an array containing the variables requested.
/*From https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#mediaWiki.config*/
/*From https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#mediaWiki.config*/
Line 77: Line 78:
var ItemName = PageName.split(':');
var ItemName = PageName.split(':');
//AddItemToSidebar('toolbox', 't-wb-json', 'JSON', conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
//AddItemToSidebar('toolbox', 't-wb-json', 'JSON', conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
AddItemToSidebar('toolbox', idName, conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
AddItemToSidebar('toolbox', messageName, conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
}
}
}
}


jQuery(sideJSONMessage('t-wb-json'));
jQuery(sideJSONMessage('t-wb-json'));

Revision as of 00:57, 26 March 2021

/* Any JavaScript here will be loaded for all users on every page load. */

/*Code from https://www.mediawiki.org/w/index.php?title=User:TheDJ/common.js&oldid=2187846*/
function sideJSONMessage(messageName) {
	console.log("Entering " + messageName);
	jQuery.when(mw.loader.using(['mediawiki.api', 'mediawiki.jqueryMsg']), jQuery.ready ).done( function() {
		console.log("Entered");
		new mediaWiki.Api().loadMessagesIfMissing([messageName, 'tooltip-' + messageName]).done( function() {
			AddJSONLinkToSidebar(messageName);
	});
});
}

function AddItemToSidebar(section, idName, link) {
	var target
	var menuMessage
	
	try {
		switch ( section ) {
			case 'languages':
				target = 'p-lang';
				break;
			case 'toolbox':
				target = 'p-tb';
				break;
			case 'navigation':
				target = 'p-navigation';
				break;
			default:
				target = 'p-' + section;
				break;
		}
		
		//See https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Message
		//See https://www.mediawiki.org/wiki/Manual:Messages_API#Using_messages_in_JavaScript
		
		console.log("idName: " + idName);
		//console.log("name: " + name);
		
		var node = document.getElementById( target )
						   .getElementsByTagName( 'div' )[0]
						   .getElementsByTagName( 'ul' )[0];

		var aNode = document.createElement( 'a' );
		var liNode = document.createElement( 'li' );

		aNode.text = mw.message(idName).text();
		
		aNode.setAttribute( 'href', link );
		aNode.setAttribute( 'title', mw.message('tooltip-' + idName).text());
		liNode.appendChild( aNode );
		/*liNode.className = 'plainlinks';*/
		liNode.id = idName;
		node.appendChild( liNode );

	} catch( e ) {
		// let's just ignore what's happened
		return;
	}
}

function AddJSONLinkToSidebar(messageName) {
	/*Returns an array containing the variables requested.
	/*From https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#mediaWiki.config*/
	var conf = mw.config.get([
		'wgServer',
		'wgWikibaseItemId',
		'wgPageName'
	]);
	
	var PageName = conf.wgPageName;
	/*
	console.log(PageName);
	console.log(PageName.substr(0, 5));
	console.log(PageName.substr(0, 5).toLowerCase());
	*/
	if (PageName.substr(0, 5).toLowerCase() == "item:") {
		var ItemName = PageName.split(':');
		//AddItemToSidebar('toolbox', 't-wb-json', 'JSON', conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
		AddItemToSidebar('toolbox', messageName, conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
	}
}

jQuery(sideJSONMessage('t-wb-json'));