User:Cqm/ common.js

( function ( window, document, $, mw ) {

'use strict'; /**	 * Polyfills */	if ( !Array.isArray ) { }	if ( !Array.prototype.indexOf ) { }	if ( !String.prototype.trim ) { }

if ( !mw.toolbar.addButtons ) { }

/**	 * Define global variables for imported scripts */	window.ajaxPages = [ // foo ];		/**		 * Cache mw.config values */	var	conf = mw.config.get( [			// foo		] ), /**		 * For tracking what scripts and styles have been imported */		scripts = [], styles = [], tags = [], /**		 * Polyfill for mw.user.getRights, based on the original * Becomes available in mw1.20 */		deferreds = {}, getRights = mw.user.getRights || function ( callback ) {

var api; if ( !deferreds.rights ) {

deferreds.rights = $.Deferred; api = new mw.Api;

api.get( {					action: 'query',					meta: 'userinfo',					uiproop: 'rights'				} ).always( function ( data ) {					var rights;					if ( data.query && data.query.userinfo ) {						rights = data.query.userinfo.rights;					}					deferreds.rights.resolve( rights || [] );				} ); }			deferreds.rights.done( callback ).promise;

},		/**		 *		 */		reusable = { /**			 *			 */			isFunction: function ( obj ) { },			/**			 *			 */			debug: function ( set ) { },		},		/**		 * Contains conditonals, imports and functions for each script to run *		 * Based on DemoScripts.js  */		includes = { /**			 * Possible fields: * - conditional {boolean|function} A conditional that when evaluated to true *              will import the scripts/stylesheets or run the function. *              If this is a function it needs to return true to continue. * - scripts {array|string} Any scripts to import * - styles {array|string} Any stylesheets to import * - tag {string} Tag to track which scripts/functions have loaded * - expose {boolean} Whether to add exec to window.rswiki * - exec {function} Function to run */			foo: { conditional: true, scripts: [], styles: [], tag: 'common:foo', expose: true, exec: function { }			},			addtalkheader: { conditional: true, tag: 'common:addtalkheader', exec: function { }			},			collapsenavbox: { conditional: conf.wgNamespaceNumber === 0 && !!$( '.navbox' ).length, tag: 'common:collapsenavbox', exec: function { }			},			customeditbuttons: { conditional: ['edit', 'submit'].indexOf( conf.wgAction ) > -1, tag: 'common:foo', exec: function { }			},

ga: { conditional: true, tag: 'common:ga', exec: function { }			},

insertusername: { conditional: true, tag: 'common:insertusername', exec: function { }			},

navbox: { conditional: true, tag: 'common:navbox', exec: function { }			},

sigreminder: { conditional: true, tag: 'common:sigreminder', exec: function { }			},

skinredirect: { conditional: true, tag: 'common:skinredirect', exec: function { }			},

tagswitch: { conditional: true, tag: 'common:tagswitch', exec: function { }			},

topicon: { conditional: true, tag: 'common:topicon', exec: function { }			},		};	function init { $.each( includes, function {			if ( reusable.isFunction( this.conditonal ) ) {				// script imports				if ( Array.isArray( this.scripts ) ) {					scripts.concat( this.scripts );				} else if ( typeof this.scripts === 'string' ) {					scripts.push( this.scripts );				}				// style imports				if ( Array.isArray( this.styles ) ) {					styles.concat( this.styles );				} else if ( typeof this.styles === 'string' ) {					styles.push( this.styles );				}				// tags				if ( typeof this.tag === 'string' ) {					tags.push( this.tag );				}				// functions				if ( this.exec !== undefined ) {					this.exec;					if ( this.expose ) {						console.log( this );					}				}			}		} );

window.rswiki.scripts.concat( scripts ); window.rswiki.styles.concat( styles ); window.rswiki.tags.concat( tags ); importArticles( {			type: 'style',			articles: styles		}, {			type: 'script',			articles: scripts		} ); }	// create global rswiki object if it doesn't exist already window.rswiki = window.rswiki || {}; window.rswiki.scripts = window.rswiki.scripts || []; window.rswiki.styles = window.rswiki.styles || []; window.rswiki.tags = window.rswiki.tags || []; // expose getRights polyfill to global scope if it doesn't exist // for use in other scripts should it be needed mw.user.getRights = getRights; $( function {		var x;		if ( reusable.debug ) {			for ( x in includes ) {				// iterate through each include object				// assign includes[x].exec to rswiki object			}		}		$.extend( window.rswiki, reusable );		init;	} );

} ( this, this.document, this.jQuery, this.mediaWiki ) );