User:Evad37/Module/multiButtonConfirm.js

/** * @module multiButtonConfirm * @description A confirmation dialog that can have multiple customised buttons. * Derived from . * @author User:Evad37 * @version 1.0.0 * @requires mediawiki.util * @requires oojs-ui-core * @requires oojs-ui-widgets * @requires oojs-ui-windows * @exports {Function} multiButtonConfirm *  @param {Object} config  configuartion options *  @param {String} config.title  Title for the dialogue *  @param {String} config.message  Message for the dialogue. HTML tags (except *    for, , , , , and --plain tags only, no *     attributes allowed) are escaped; wikilinks are turned into real links. *  @param {Object[]} config.actions  Optional. Array of configuration objects for *    OO.ui.ActionWidget . *    If not specified, the default actions are 'accept' (with label 'OK') and *    'reject' (with label 'Cancel'). *  @param {String} config.size  Symbolic name of the dialog size: small, *    medium, large, larger or full. *  @returns {Promise} action taken by user */ Window.exportScriptModule('multiButtonConfirm', (function { var dependenciesLoaded = $.Deferred; mw.loader.using(		['mediawiki.util', 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows'],		dependenciesLoaded.resolve,		dependenciesLoaded.reject	 ); return dependenciesLoaded.then(function {		/**		 * Un-escapes some HTML tags (, , , , , and );		 * turns wikilinks into real links. Ignores anyting within		 * ... tags.		 * Input will first be escaped using mw.html.escape unless specified 		 * otherwise.		 * @param {String} text		 * @param {Object} config Configuration options		 * @param {Boolean} config.noEscape - do not escape the input first		 * @returns {String} unescaped text		 */		var safeUnescape = function(text, config) {			var path = 'https:' + mw.config.get('wgServer') + '/wiki/';			return ( config && config.noEscape && text || mw.html.escape(text) )			// Step 1: unescape tags			.replace( /&lt;(\/?pre\s?\/?)&gt;/g, '<$1>'			)			// Step 2: replace piped wikilinks with real links (unless inside tags)			.replace( /\[\[([^\|\]]*?)\|([^\|\]]*?)\]\](?![^<]*?<\/pre>)/g, '$2' )			// Step 3: replace other wikilinks with real links (unless inside tags)			.replace( /\[\[([^\|\]]+?)]\](?![^<]*?<\/pre>)/g, '$1' )			// Step 4: unescape other tags:, , , , (unless inside tags)			.replace( /&lt;(\/?(?:br|p|ul|li|hr)\s?\/?)&gt;(?![^<]*?<\/pre>)/g, '<$1>'			);		};		/** @see #exports */		var multiButtonConfirm = function(config) {			var dialogClosed = $.Deferred;			// Wrap message in a HtmlSnippet to prevent escaping			var htmlSnippetMessage = new OO.ui.HtmlSnippet( safeUnescape(config.message) );			var windowManager = new OO.ui.WindowManager;			var messageDialog = new OO.ui.MessageDialog;			$('body').append( windowManager.$element );			windowManager.addWindows( [ messageDialog ] );			windowManager.openWindow( messageDialog, { 'title': config.title, 'message': htmlSnippetMessage, 'actions': config.actions, 'size': config.size } );			windowManager.on('closing', function(_win, promise) { promise.then(function(data) {					dialogClosed.resolve(data && data.action);					windowManager.destroy;				}); });			return dialogClosed.promise;		};		return multiButtonConfirm;	 }); }));