MediaWiki:Common.js: Difference between revisions

From DataTrek
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 82: Line 82:
//See https://www.mediawiki.org/wiki/Manual:Messages_API#Using_messages_in_JavaScript
//See https://www.mediawiki.org/wiki/Manual:Messages_API#Using_messages_in_JavaScript
console.log(idName);
console.log("idName: " + idName);
console.log(name);
console.log("name: " + name);
messages = new mw.Map();
messages = new mw.Map();
/*mw.messages.set( {
/*mw.messages.set( {
Line 93: Line 93:
} );*/
} );*/


//messages.set({idName: name});
messages.set({idName: name});
messages.set({name: idName});
//messages.set({name: idName});
//menuMessage = new mw.Message(mw.messages, idName);
//menuMessage = new mw.Message(mw.messages, idName);
//menuMessage = new mw.Message(idName, name);
//menuMessage = new mw.Message(idName, name);
menuMessage = new mw.Message(messages, name);
menuMessage = new mw.Message(messages, idName);
console.log(menuMessage);
console.log("menuMessage: " + menuMessage);
console.log(menuMessage.text());
console.log("menuMessage.text(): " + menuMessage.text());
var node = document.getElementById( target )
var node = document.getElementById( target )

Revision as of 23:18, 9 February 2021

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

/*Code from https://www.mediawiki.org/wiki/Manual:Interface/Sidebar#Add_or_remove_toolbox_sections_(JavaScript) */
function ModifySidebar( action, section, name, link ) {
	try {
		switch ( section ) {
			case 'languages':
				var target = 'p-lang';
				break;
			case 'toolbox':
				var target = 'p-tb';
				break;
			case 'navigation':
				var target = 'p-navigation';
				break;
			default:
				var target = 'p-' + section;
				break;
		}

		if ( action == 'add' ) {
			var node = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];

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

			aNode.appendChild( document.createTextNode( name ) );
			aNode.setAttribute( 'href', link );
			liNode.appendChild( aNode );
			liNode.className = 'plainlinks';
			node.appendChild( liNode );
		}

		if ( action == 'remove' ) {
			var list = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];

			var listelements = list.getElementsByTagName( 'li' );

			for ( var i = 0; i < listelements.length; i++ ) {
				if (
					listelements[i].getElementsByTagName( 'a' )[0].innerHTML == name ||
					listelements[i].getElementsByTagName( 'a' )[0].href == link
				)
				{
					list.removeChild( listelements[i] );
				}
			}
		}


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

function AddItemToSidebar(section, idName, name, 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);
		messages = new mw.Map();
		/*mw.messages.set( {
			idName: name,
			't-wb-json': 'JSON',
    'hello': 'Hello world',
    'hello-user': 'Hello, $1!',
    'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3'
} );*/

		messages.set({idName: name});
		//messages.set({name: idName});
		//menuMessage = new mw.Message(mw.messages, idName);
		//menuMessage = new mw.Message(idName, name);
		menuMessage = new mw.Message(messages, idName);
		console.log("menuMessage: " + menuMessage);
		console.log("menuMessage.text(): " + menuMessage.text());
		
		var node = document.getElementById( target )
						   .getElementsByTagName( 'div' )[0]
						   .getElementsByTagName( 'ul' )[0];

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

		/*aNode.appendChild( document.createTextNode( name ) );*/
		//aNode.text = mw.message(idName).text();
		
		//aNode.text = menuMessage.text();
		aNode.text = menuMessage.toString();
		
		aNode.setAttribute( 'href', link );
		liNode.appendChild( aNode );
		/*liNode.className = 'plainlinks';*/
		liNode.id = idName;
		node.appendChild( liNode );

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

function CustomizeModificationsOfSidebar() {
	/* Access multiple ones for use throughout a larger code base
   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'
	]);

	// adds [[Special:CategoryTree|Special:CategoryTree]] to toolbox
	//ModifySidebar( 'add', 'toolbox', 'CategoryTree', 'https://en.wikipedia.org/wiki/Special:CategoryTree' );
	// removes [[Special:Upload|Special:Upload]] from toolbox
	//ModifySidebar( 'remove', 'toolbox', 'Upload file', 'https://en.wikipedia.org/wiki/Special:Upload' );
	var PageName = conf.wgPageName;
	var ItemName = PageName.split(':');
	
	ModifySidebar( 'add', 'toolbox', 'Item JSON', conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
}

function AddJSONLinkToSidebar() {
	/*Access multiple ones for use throughout a larger code base
	/*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(':');
		/*
		console.log(ItemName);
		*/
		AddItemToSidebar('toolbox', 't-wb-json', 'JSON', conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
	}
}

/*jQuery( CustomizeModificationsOfSidebar );*/
jQuery(AddJSONLinkToSidebar);