MediaWiki:CodeLoad.js

/*jslint browser */ /*global window, console, jQuery, mediaWiki, importArticles */

(function ($, mw) {   'use strict';

var cl = { definitions: {}, groups: null, userPrefs: {} };

// check if a definition should be enabled cl.definitionEnabled = function (id) { return cl.userPrefs[id] === true || (cl.userPrefs[id] !== false && cl.definitions[id].isdefault === true); };

// check if a definition is available for user's current skin/usergroup cl.definitionAvailable = function (id) { var req = cl.definitions[id].requirements; if (!req) { return true; }

var a = true; var b = true; if (req.usergroups) { a = req.usergroups.split('|').some(function (group) {               return mw.config.get('wgUserGroups').indexOf(group) !== -1;            }); }       if (req.skins) { b = req.skins.split('|').indexOf(mw.config.get('skin')) !== -1; }       return a && b;    };

// test if localStorage exists and is usable // source: https://github.com/Modernizr/Modernizr/tree/master/feature-detects/storage/localstorage.js   // license: MIT - https://opensource.org/licenses/MIT var localStorageIsUsable = (function {        var mod = 'modernizr';        try {            localStorage.setItem(mod, mod);            localStorage.removeItem(mod);            return true;        } catch (ignore) {            return false;        }    });

// attempt to parse user prefs var parseUserPrefs = function { try { cl.userPrefs = JSON.parse(cl.getUserPrefs) || {}; } catch (ex) { console.warn('Codeload: invalid user prefs:', ex.message); }   };

// clear prefs from local storage cl.clearUserPrefs = function { if (localStorageIsUsable) { localStorage.removeItem('CodeLoad-prefs'); }   };

// get prefs from local storage cl.getUserPrefs = function { return localStorageIsUsable && localStorage.getItem('CodeLoad-prefs'); };

// set prefs to local storage cl.setUserPrefs = function (prefs) { if (typeof prefs !== 'string') { prefs = JSON.stringify(prefs, Object.keys(cl.definitions), 4); }       if (localStorageIsUsable) { localStorage.setItem('CodeLoad-prefs', prefs); }       parseUserPrefs; };

// user's prefs page on wiki - User:Example/preferences-codeload.css var userPrefsPage = mw.config.get('wgFormattedNamespaces')['2'] + ':' + mw.config.get('wgUserName') + '/preferences-codeload.css';

// load prefs from user's prefs page cl.loadUserPrefs = function { return $.ajax({           url: mw.util.getUrl(userPrefsPage),            data: {                action: 'raw',                maxage: '0',                smaxage: '0'            },            dataType: 'text'        }).fail(function  {            // page probably doesn't exist…            cl.setUserPrefs('{}');        }).done(function (data) {            cl.setUserPrefs(data);        }); };

// save current prefs to user's prefs page cl.saveUserPrefs = function (summary) { return $.ajax({           url: mw.util.wikiScript('api'),            data: {                action: 'edit',                format: 'json',                minor: true,                summary: summary || '',                text: cl.getUserPrefs,                title: userPrefsPage,                token: mw.user.tokens.get('editToken')            },            dataType: 'json',            type: 'POST'        }); };

function main { var imports = [ 'MediaWiki:CodeLoad-definitions.js' ];

if (           mw.config.get('wgCanonicalSpecialPageName') === 'Blankpage' &&            mw.util.getParamValue('blankspecial') === 'CodeLoadPrefs'        ) { // page = Special:BlankPage?blankspecial=CodeLoadPrefs imports.push(               'u:dev:MediaWiki:CodeLoad.js/localisation.js',                'u:dev:MediaWiki:CodeLoad.js/preferences.js'            ); } else if (           mw.config.get('wgPageName') === userPrefsPage &&            mw.config.get('wgIsArticle')        ) { // page = User:Example/preferences-codeload.css imports.push(               'u:dev:MediaWiki:CodeLoad.js/localisation.js',                'u:dev:MediaWiki:CodeLoad.js/preferences-userpage.js'            ); } else { // any other page imports.push('u:dev:MediaWiki:CodeLoad.js/loader.js'); }

importArticles({           type: 'script',            articles: imports        }); }

function init { // make global window.codeLoad = cl;

if (cl.getUserPrefs) { // prefs are already cached in local storage parseUserPrefs; main; } else { // prefs need to be loaded from page cl.loadUserPrefs.always(main); }   }

init;

}(jQuery, mediaWiki));