MediaWiki:Gadget-wikidialog.js

/* This script provides a function to set up dialog fields and buttons based on markup generated by dialog formatting templates. Any javascript page importing this page should also contain a commented link to it, for Special:WhatLinksHere/.

For instructions, see the |talk page. */

$(function {	if (! window.wikidialog) window.wikidialog = {};	if (! window.wikidialog.show) {		//		// version		//		window.wikidialog.gadgetVersion = function { return '0.10 (2018-02-22)'; }		//		window.wikidialog.show = function {			//			// text			//			var things = $('span.wikidialog-text');			var thingIds = things.map(function  { return $(this).attr('id'); });			things.replaceWith(function (idx) { if ((idx > 0) && (thingIds[idx] == thingIds[idx - 1])) { return ''; } var s = $(this).text; // .substring(0,1024); // BOUND TEXT CONTENT SIZE var d = ''; if (  s.search(/^size=(\d*)&/) == 0) { d = s.match(/^size=(\d*)&(.*)/); s = d[2]; d = d[1]; }				for (var j = idx + 1; j < thingIds.length; j++) { if (thingIds[idx] == thingIds[j]) { s += ' ' + $(things[j]).text; } else break; }				var e = $(document.createElement('input')).attr('type','text').addClass('wikidialog-text').attr('id',thingIds[idx]).val(s).attr('size',d); if ($(this).is('.wikidialog-hidden')) e.css('display','none'); return e;			});			//			// textarea			//			things = $('span.wikidialog-textarea');			thingIds = things.map(function { return $(this).attr('id'); });			things.replaceWith(function (idx) { if ((idx > 0) && (thingIds[idx] == thingIds[idx - 1])) { return ' '; } var s = $(this).text; // .substring(0,131072); // BOUND TEXTAREA CONTENT SIZE var c = ''; if (  s.search(/^cols=(\d*)&/) == 0) { c = s.match(/^cols=(\d*)&(.*)/); s = c[2]; c = c[1]; }				var r = ''; if (  s.search(/^rows=(\d*)&/) == 0) { r = s.match(/^rows=(\d*)&(.*)/); s = r[2]; r = r[1]; }				for (var j = idx + 1; j < thingIds.length; j++) { if (thingIds[idx] == thingIds[j]) { s += '\n' + $(things[j]).text; } else break; }				var e = $(document.createElement('textarea')).addClass('wikidialog-textarea').attr('id',thingIds[idx]).attr('cols',c).attr('rows',r).val(s); if ($(this).is('.wikidialog-hidden')) e.css('display','none'); return e;			});			//			// checkbox			//			$('span.wikidialog-checkbox').replaceWith(function { var i = $(this).attr('id'); var s = $(this).text; // .substring(0,32); // BOUND CHECKBOX CONTENT SIZE var result = $(document.createElement('input')).attr('type','checkbox').addClass('wikidialog-checkbox').attr('id',i); if (s.length > 8) result.attr('checked','yes'); return result; });			//			// select			//			$('span.wikidialog-select').replaceWith(function { var i = $(this).attr('id'); var s = $(this).text.split('&'); // .substring(0,8192).split('&'); // BOUND SELECT CONTENT SIZE var e = document.createElement('select'); for (var k=1; (k < s.length); k += 2) if (s[k-1]) { e.appendChild(new Option(s[k-1],s[k],false,false)); }				return $(e).addClass('wikidialog-select').attr('id',i); });			//			// button			//			$('span.wikidialog-button').replaceWith(function { function getWikidialogID { var s = location.search.match(/[\?&]wikidialogid=(\d+)/); return (s && s.length && (s.length > 1)) ? Number(s[1]) : 0; }				function getRollbackUrl(url, id) { var s = url.match(/[\?&]wikidialogid=(\d+)/); if (s && s.length && (s.length > 0)) { // url is stable return url; }					s = sessionStorage.wikidialogUnstableUrl; if (s && (url == s)) { // url is unstable and known dialog-active // expect it to have had id one less than next-id s = ('wikidialogid=' + (id - 1)); } else { // url is unstable and might be dialog-active or not s = 'wikidialogrolledback=x'; }					return (url + ((url.search(/\?/) > 0) ? '&' : '?') + s); }				function makeButtonHandler(wikidialogButtonData) { // in some browsers, event target may be a copy lacking the data return function (event) { var target = $(event.target); var action = wikidialogButtonData.action; if (action.substring(0,5).toLowerCase == 'user:') { var ug = mw.config.get('wgUserGroups'); if (!ug || ug.join(' ').indexOf("sysop") === -1) { alert('action requires admin privs: ' + action); return; }					} else { action = mw.config.get('wgSiteName') + ':Dialog/' + action; }					// action must be fully protected $('input.wikidialog-button, button.wikidialog-button').attr('disabled',true); $.getJSON(						mw.util.wikiScript('api'),						{							format: 'json',							action:  'query',							prop:    'info',							inprop:  'protection',							titles:  action						}					).done(						function(data) {							function anchorEncode(s) {								return encodeURIComponent( s.replace( / /g, '_' ) )									.replace( /!/g, '%21' ).replace( /'/g, '%27' )									.replace( /\(/g, '%28' ).replace( /\)/g, '%29' )									.replace( /\*/g, '%2A' ).replace( /~/g, '%7E' )									.replace( /%3A/g, ':' ).replace( /%/g, '.' );							}							function idSelector(s) {								s = s.replace( /\./g, '\\.' );								return 'input#wikidialog-' + s + ', textarea#wikidialog-' + s + ', select#wikidialog-' + s;							}							function fieldSelector(s) {								var sa = anchorEncode(s);								if (sa == s) {									return idSelector(s);								} else {									return idSelector(s) + ', ' + idSelector(sa); }							}							var s = ''; if (data.query && data.query.pages) for (var p in data.query.pages) { if ("missing" in data.query.pages[p]) s = 'page not found'; else if ('invalid' in data.query.pages[p]) s = 'page name invalid'; else if ('protection' in data.query.pages[p]) { if (data.query.pages[p].protection[0] && data.query.pages[p].protection[1]) { if (('level' in data.query.pages[p].protection[0]) &&												('level' in data.query.pages[p].protection[1])) { if ((data.query.pages[p].protection[0].level == 'sysop') &&													(data.query.pages[p].protection[1].level == 'sysop')) { } else s = 'page unsecured'; } else s = 'page query misfired'; } else s = 'page unsecured'; } else s = 'page query misfired'; }							else s = 'page query failed'; if (s) { $('input.wikidialog-button, button.wikidialog-button').attr('disabled',false); alert(s + ': ' + action); return; }							// action is now known to be fully protected // sessionStorage.wikidialogNextID was assured before setting up the button var id = sessionStorage.wikidialogNextID; s = mw.util.getUrl(action) + '?wikidialogid=' + id; sessionStorage.wikidialogNextID++; sessionStorage['wikidialog:' + id + ':incoming'] = s; if (! target.hasClass('wikidialog-norollback')) { sessionStorage['wikidialog:' + id + ':prevurl'] = getRollbackUrl(location.href, Number(id)); }							if ('unauth' in wikidialogButtonData) sessionStorage['wikidialog:' + id + ':unauth'] = wikidialogButtonData.unauth; else                                 sessionStorage['wikidialog:' + id + ':unauth'] = mw.config.get('wgPageName'); if ('origin' in wikidialogButtonData) sessionStorage['wikidialog:' + id + ':origin'] = wikidialogButtonData.origin; if ('proxy' in wikidialogButtonData) sessionStorage['wikidialog:' + id + ':proxy']  = wikidialogButtonData.proxy; sessionStorage['wikidialog:' + id + ':params'] = ''; for (var j = 0; j < wikidialogButtonData.length; j++) { var d = wikidialogButtonData[j].match(/[\w\-\u0080-\uFFFF]+/g); // STUFF ALLOWED IN FIELD NAMES d[1] = (d[1] ? d[1] : d[0]); d[1] = $(fieldSelector(d[1])); if (d[1].length == 0) { } else { sessionStorage['wikidialog:' + id + ':params'] += d[0] + ','; var elt = d[1].first; d[1] = d[1].add(target); for (var k = 0; k < d[1].length; k++) { if ($(d[1][k]).hasClass('wikidialog-button')) { k++; if (k < d[1].length) elt = $(d[1][k]); break; }									}									switch (elt.attr('type')) { case 'checkbox': d[1] = (elt[0].checked ? 'yes' : ''); break; default: d[1] = elt.val; break; }									sessionStorage['wikidialog:' + id + ':in:' + d[0]] = d[1]; }							}							if (target.hasClass('wikidialog-rescript')) { delete window.wikidialog; }							sessionStorage.wikidialogSequenceBound = 10; // POINT TO ADJUST max action-sequence length //  also occurs in receive location = s;						} ).fail(function { $('input.wikidialog-button, button.wikidialog-button').attr('disabled',false); alert('action target check failed: ' + action); });				 }; // end return function  ...				} // end function makeButtonHandler				if (! (sessionStorage)) { // allows handler to assume sessionStorage					return " &lt;button requires sessionStorage (not found; perhaps your browser is out of date?)&gt; ";				}				if (sessionStorage.wikidialogNextID) {} else sessionStorage.wikidialogNextID = '1';				if (Number(sessionStorage.wikidialogNextID) <= getWikidialogID) {					alert('Warning: session data may be inaccessible\n(dialog ID should be less than stored count:\n dialog ID is ' +						getWikidialogID + '\n stored count is ' + sessionStorage.wikidialogNextID + ')');					sessionStorage.wikidialogNextID = getWikidialogID + 1;				}				var s = $(this).text; // .substring(0,1024); // BOUND BUTTON CONTENT SIZE				var a = '';				if (  s.search(/^action=([\w\s\-\/\:\u0080-\uFFFF]+)&/) == 0) { // STUFF ALLOWED IN ACTION NAMES					a = s.match(/^action=([\w\s\-\/\:\u0080-\uFFFF]+)&(.*)/);    // STUFF ALLOWED IN ACTION NAMES					s = a[2]; a = a[1]; }				var b = ''; if (  s.search(/^label=([\w\s\-\/\:\(\)\.\,\;'\u0080-\uFFFF]+)&/) == 0) { // STUFF ALLOWED IN BUTTON LABELS b = s.match(/^label=([\w\s\-\/\:\(\)\.\,\;'\u0080-\uFFFF]+)&(.*)/);  // STUFF ALLOWED IN BUTTON LABELS s = b[2]; b = b[1]; if ($(this).is('.wikidialog-italic-label')) { b = ' + b + '; } if ($(this).is('.wikidialog-bold-label' )) { b =  + b + ; } }				var wikidialogButtonData = $(this).data; if ('wikidialogButtonData' in wikidialogButtonData) wikidialogButtonData = wikidialogButtonData.wikidialogButtonData; else wikidialogButtondata = {}; wikidialogButtonData.action = a;				wikidialogButtonData.length = 0; var s = s.match(/([\w\-\u0080-\uFFFF]+\:?[\w\-\u0080-\uFFFF]*)&/g); // STUFF ALLOWED IN FIELD NAMES if (s != null) { wikidialogButtonData.length = s.length; for (var j = 0; j < s.length; j++) { wikidialogButtonData[j] = s[j].substring(0,s[j].length - 1); a += ((j == 0) ? ' < ' : ', ') + s[j].substring(0,s[j].search(/[\:&]/)); }				}				return $(document.createElement('button')).attr('type','button').addClass(this.className).html(b).attr('title',a) .data('wikidialogButtonData',wikidialogButtonData).click(makeButtonHandler(wikidialogButtonData)); });			//			// ifsupported			//			if (sessionStorage) {				$('.wikidialog-alternative').css('display','none');				$('.wikidialog-dependent').css('display','inline');			}		};	}	window.wikidialog.show; });