MediaWiki:UnifiedControlPanel.js

!function(cfg) { // unified control panel. codename: ucp // fancy switches to do fancy actions

// clone urlvars in order to preserve it natural var urlVars = cloneObject($.getUrlVars); cfg.debug = !0 || cfg.debug || urlVars.debug; if (cfg.loaded && !cfg.debug) return; cfg.loaded = !0;

function log { if (!cfg.debug) return; var a = [].slice.call(arguments); a.unshift('ucp'); console.log.apply(this, a); }// log

function cloneObject(what) { // cuz $.extend and json.parse(.stringify) loses x when x undefined is       if (!what) return; var cloned = {}; for (var i in what) if (what.hasOwnProperty(i)) cloned[i] = what[i]; return cloned }// cloneObject

function updateAction(source, target) { // updates target actions. source: source // source: {action.id: action} // target: actions = [] if (!source || !Object.keys(source).length) return; log('ua.source', source); Object.values(source).forEach(function(sourceAction) {           // update target            var targetIndex = Object.values(target).findIndex(function(action) { return action.id === sourceAction.id; });           log('ua.index', targetIndex, sourceAction);            if (targetIndex === -1) targetIndex = target.length;            target[targetIndex] = $.extend(true, target[targetIndex], sourceAction);            log('ua.newtarget', target[targetIndex]);        }); }// updateAction

function preload(globalActions, data) { // preloads data from data or current location // globalActions: actions to check against data // in order to convert data more precisely // returns actions = {action: data} data = data || urlVars; if (!data || !Object.keys(data).length) return; var actions = {}; Object.keys(data).forEach(function(key) {           if (!key) return;            var ga;            // if action is in globalActions            ga = globalActions.map(function(v) { return (v.id === key || v.action === key + '=' + data[key]) ? v : false; }).filter(Boolean)[0];           log('p.ga', ga);            actions[key] = {                id: ga ? ga.id : key,                action: ga ? ga.action : key,                caption: ga ? ga.caption : key,                checked: true,                hasInput: ga ? ga.hasInput : (typeof(data[key]) === 'undefined' ? undefined : true),               inputVal: typeof(data[key]) === 'undefined' ? undefined : data[key],            };        }); return actions; }// preload

function btnShowForm_click { if (cfg.form.parents('html').length) { // attached to the dom. detach it           btnCancel_click; } else { // not attached yet cfg.form.prependTo('.WikiaSiteWrapper:first'); // set size and position cfg.form.offset({               top: $(window).scrollTop + ((($(window).height - cfg.form.height) / 2 > 0) ? ($(window).height - cfg.form.height) / 2 : 0),               left: $(window).scrollLeft + ((($(window).width - cfg.form.width) / 2 > 0) ? ($(window).width - cfg.form.width) / 2 : 0)           }); log('sf.offset', cfg.form.offset); }   }// btnShowForm_click function btnCancel_click { cfg.form = cfg.form.detach; }// btnCancel_click function btnDo_click { // do stuff // actions are not encoded. by design var actions = [];

cfg.form.find('.ucp-action input:checked').each(function {           var $chkbox = $(this),                $action = $chkbox.closest('.ucp-action'),                $input = $action.find('input[type="text"]'),                action = $action.data('action'),                actionValue = $input.val || null;            actions.push(action + (actionValue === null ? '' : '=' + actionValue));        });// each action.input:checked

actions = actions.filter(Boolean); if (!actions.length && !location.search) return;// nothing to do       log('dc.actions', actions); // will keep ? at the end of the url. by design location.search = actions.length ? '?' + actions.join('&') : ''; }// btnDo_click function createForm(actions) { // actions: [{id, action, caption, title, checked, hasInput, inputSize, inputPh, inputVal, params: [{param: value}]}] // action.id is always equal to action name (raw for raw etc), unless it 2nd+ entry // any other format might (and most probably will) not work on preload stage // trans: {id: text} // returns $form // encode/decode stuff totally on user is. by design importArticle({type: 'style', article: 'w:c:dev:MediaWiki:UnifiedControlPanel.css'}); actions = $.isArray(actions) ? actions : []; var $form = $(' ', {               id: 'ucp-main',            }), $btnDo = $(' ', {               class: 'ucp-btn ucp-btn-do wds-button',                text: cfg.trans.btndotext || 'do it',                title: cfg.trans.btndotitle || 'just do it',            }), $btnCancel = $(' ', {               class: 'ucp-btn ucp-btn-cancel wds-button wds-is-secondary',                text: cfg.trans.btncanceltext || 'cancel',                title: cfg.trans.btncanceltitle || 'just don\'t',            }); $btnDo.click(btnDo_click); $btnCancel.click(btnCancel_click); /*       $form.on('keydown.ucp', function(e) {            if (e && e.keyCode === 27) btnCancel_click;        }); */       // add actions actions.forEach(function(action) {           var $action = $(' ', { class: 'ucp-action', id: 'ucp-action-' + action.id, 'data-action': action.action, }),               $chkbox = $(' ', { type: 'checkbox', id: 'ucp-chk-' + action.id, }),               $label = $(' ', { for: 'ucp-chk-' + action.id, id: 'ucp-label-' + action.id, text: cfg.trans[action.id + 'label'] || action.caption, title: cfg.trans[action.id + 'title'] || action.title, }),               $input = action.hasInput ? $(' ', { type: 'text', id: 'ucp-input-' + action.id, size: action.inputSize || 5, placeholder: action.inputPh || '',// doesn't work; added later via .attr }) : null;           // some adjustments                        // for some weird reason placeholder can't be added as {plch:x} earlier            if (action.hasInput) {                $input.attr('placeholder', action.inputPh || '');                if (typeof(action.inputVal) !== 'undefined') $input.val(action.inputVal);            }            // add preloaded values            if (action.checked) $chkbox.attr('checked', 'CHECKED');            $action.append($chkbox).append($label).append($input);            $form.append($action);        }); $form.append(''); $form.append($btnDo).append($btnCancel); // some adjustments // set bgcolor to body.bgcolor $form.css('background-color', $('body').css('background-color')); return $form; }// createForm

function init(trans) { cfg.trans = cfg.trans || trans || {}; var $btnShowForm = $('', {               class: 'ucp-btn-showform wds-button',                text: cfg.trans.btnshowformtext || 'ucp',                title: cfg.trans.btnshowformtitle || 'unified control panel',            }), actions = $.isArray(cfg.actions) ? cfg.actions : [ {               id: 'raw', action: 'action=raw', caption: 'action=raw', title: 'Show raw content of the article', },           {                id: 'render', action: 'action=render', caption: 'action=render', title: 'Show rendered article content', },           {                id: 'credits', action: 'action=credits', caption: 'action=credits', title: 'Shows a list of people who contributed to the page', },           {                id: 'purge', action: 'action=purge', caption: 'action=purge', title: 'Don\'t use it', },           {                id: 'debug', action: 'debug=1', caption: 'Enable debug mode', title: 'it\'s not the test mode', },           {                id: 'usesitejs', action: 'usesitejs', caption: 'Turn off sitewide js', title: 'turn the shit off', },           {                id: 'usesitecss', action: 'usesitecss', caption: 'Turn off sitewide css', title: 'turn the shit off', },           {                id: 'useuserjs', action: 'useuserjs', caption: 'Turn off user js', title: 'turn the shit off', },           {                id: 'useusercss', action: 'useusercss', caption: 'Turn off user css', title: 'turn the shit off', },           {                id: 'uselang', action: 'uselang', caption: 'uselang=lang', title: 'use chosen lang', hasInput: !0, inputSize: 6, inputPh: 'langid', },       ];        actions = actions.concat(cfg.moreactions || []).filter(Boolean); updateAction(preload(actions), actions); log('init.actions', actions); $btnShowForm.click(btnShowForm_click); $('#WikiaBar .tools').append($btnShowForm); cfg.form = createForm(actions); }// init

init; }((window.fng = window.fng || {}).ucp = window.fng.ucp || {});