MediaWiki:Gadget-WikiEditor-Tools-loader.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/** This script contains the code required for loading.
 *  All other code is located at that page.
 *
 * This is intended to reduce the code bloat on pages that do not need this functionality,
 * and also make it easier to switch in debug versions.
 */

( function () {

	/* Not editing, no Wikieditor to care about */
	if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) === -1 ) {
		return;
	}

	if ( !window.wsutils ) {
		window.wsutils = {};
	}

	/**
	 * This is the boilerplate wikieditor configurator function
	 * (without the "is editing" check, as we do that above)
	 *
	 * Any code can use this boilerplate (including user JS) as long as this script
	 * is loaded first.
	 *
	 * @param  {Function} callback function to call (called with the wikieditor)
	 */
	window.wsutils.wikiEditorConfigIncantation = function ( callback ) {
		mw.loader.using( 'user.options' ).then( function () {
			// This can be the string '0' if the user disabled the preference
			// ([[phab:T54542#555387]])
			if ( mw.user.options.get( 'usebetatoolbar' ) === 1 ) {
				$.when(
					mw.loader.using( 'ext.wikiEditor' ), $.ready
				).then( function () {
					/* eslint-disable-next-line no-jquery/no-global-selector */
					var $tb = $( '#wpTextbox1' );
					callback( $tb.wikiEditor.bind( $tb ) );
				} );
			}
		} );
	};

	var cm = mw.config.get( 'wgPageContentModel' );
	var ns = mw.config.get( 'wgCanonicalNamespace' );
	var isSandbox = mw.config.get( 'wgTitle' ).search( /\bSandbox/ ) !== -1;

	var scripts = [];

	if ( cm === 'wikitext' ) {
		if ( ns === 'Template' || isSandbox ) {
			scripts.push( 'WikiEditor-TemplateTools' );
		} else if ( ns === 'Module_talk' ) {
			scripts.push( 'WikiEditor-ModuleTools' );
		}
	} else if ( [ 'css', 'sanitized-css' ].indexOf( cm ) !== -1 ) {
		scripts.push( 'WikiEditor-CssTools' );
	} else if ( cm === 'Scribunto' ) {
		scripts.push( 'WikiEditor-ModuleTools' );
	}

	if ( scripts.length ) {
		// Preload these common deps while the main scripts load
		mw.loader.load( [ 'user.options', 'ext.wikiEditor' ] );

		var loc = function ( s ) {
			return 'ext.gadget.' + s;
		};

		// load all the scripts
		scripts
			.map( loc )
			.forEach( function ( s ) {
				mw.loader.load( s );
			} );
	}
}() );