MediaWiki:ChatObject/code.js

/* global console, mediaWiki */ /** * Easily manipulate Chat * * @class Chat * @singleton */ Chat = ( function ( mw ) {	'use strict';	var config = mw.config;	if ( config.get( 'wgCanonicalSpecialPageName' ) !== 'Chat' ) {		return;	}	document.head.appendChild( document.createElement( 'style' ) );	document.getElementsByTagName( 'style' )[4].appendChild( document.createTextNode( '.wordmark button { display: inline-block; margin-top: 12px; vertical-align: top; ' ) );	return {		/**		 * Map of the chat's color scheme		 */		colorScheme: {},		/**		 * Append a button to the chat's header		 * @param {string} label The button's label		 * @param {Function} callback Function to be run upon the button being clicked		*/		addToolbarButton: function ( label, callback ) {			var button;			if ( typeof label !== 'string' || typeof callback !== 'function' ) {				console.log( 'Invalid button setup' );				return;			}			button = document.createElement( 'button' );			button.textContent = label;			button.addEventListener( 'click', callback );			document.getElementsByClassName( 'wordmark' )[0].appendChild( button );		},		/**		 * Append CSS to a preset element		 * @param {string} selector A CSS selector		 * @param {Object} properties An object mapping CSS property names to values		 */		appendStyle: function ( selector, properties ) {			var css = selector, prop;			if ( typeof selector !== 'string' || properties !== Object( properties ) ) {				console.log( 'Invalid CSS' );				return;			}			css += ' {\n';			for ( prop in properties ) {				css += prop + ': ' + properties[prop];			}			document.getElementsByTagName( 'style' )[4].appendChild( document.createTextNode( css ) );		},		/**		 * Import a script or stylesheet from the given URI		 * @param {string} uri An URI pointing to the resource		 */		importResource: function ( uri ) {			var loader;			if ( typeof uri !== 'string'	) {				console.log( 'Invalid URI' );				return;			}			if ( uri.indexOf( '.css' ) !== -1 ) {				loader = document.createElement( 'link' );				loader.rel = 'stylesheet';				loader.href = uri;				document.head.appendChild( loader );			} else if ( uri.indexOf( '.js' ) !== -1 ) {				loader = document.createElement( 'script' );				loader.src = uri;				document.head.appendChild( loader );			} else {				console.log( 'Unsupported resource' );			}		},		/**		 * Change the callback function and text of a button		 * @param {number} index Which button to change (first button is 0)		 * @param {Function} callback (optional) The new callback		 * @param {string} The button's new label		 */		modifyToolbarButton: function ( index, callback, label ) { if ( typeof index !== 'number'				|| ( typeof callback !== 'function' && typeof callback !== undefined )				|| typeof label !== 'string'				|| document.getElementById( 'ChatHeader' ).getElementsByTagName( 'button' ).length < index			) { console.log( 'Cannot modify button' ); return; }			document.getElementById( 'ChatHeader' ).getElementsByTagName( 'button' )[index].addEventListener(				'click', callback			); document.getElementById( 'ChatHeader' ).getElementsByTagName( 'button' )[index].textContent = label; },		/**		 * Change the chat's color scheme * @param {Object} scheme An object mapping changeable colors to the new colors */		setColorScheme: function ( scheme ) { if ( scheme !== Object( scheme ) ) { console.log( 'Invalid scheme structure' ); return; }			document.body.style.backgroundColor = scheme.pageBackground || document.body.style.backgroundColor; document.body.style.color = scheme.textColor || document.body.style.color; document.getElementById( 'WikiaPage' ).style.backgroundColor = scheme.chatBackground || document.getElementById( 'WikiaPage' ).style.backgroundColor; this.colorScheme = scheme; }	}; }( mediaWiki ) );