MediaWiki:CodeLoad.js/preferences.js

/*jslint browser */ /*global jQuery, mediaWiki, codeLoad, BannerNotification */

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

var prefGroups = {}; var $prefs;

// calculate new prefs, and save them to local storage/user prefs page function setPrefs(event) { var newPrefs = {};

$prefs.find('input[type="checkbox"]').each(function (ignore, element) {           newPrefs[element.value] = element.checked;        });

cl.setUserPrefs(newPrefs);

var bannerSuccess = new BannerNotification(mw.message('codeload-save-success').escaped, 'confirm'); var bannerFail = new BannerNotification(mw.message('codeload-save-fail').escaped, 'error');

if (event.target.id === 'codeload-save') { cl.saveUserPrefs(mw.message('codeload-edit-summary').plain).always(function (data) {               if (data.edit && data.edit.result === 'Success') {                    bannerSuccess.show;                } else {                    bannerFail.show;                }            }); } else { bannerSuccess.show; }   }

// reset prefs to definition default function resetPrefs(event) { event.preventDefault;

$prefs.find('input[type="checkbox"]').each(function (ignore, element) {           element.checked = cl.definitions[element.value].isdefault === true;        }); }

// add each prefs group to the list function addPrefsGroup(id) { if (typeof cl.groups[id] === 'string') { mw.messages.set('codeload-group-' + id, cl.groups[id]); }

var $groupHeading = $(' ').msg('codeload-group-' + id); var $group = $(' ');

prefGroups[id] = $group; $prefs.append($groupHeading, $group); }

// add each code definition to the list function addPrefEntry(id) { // have to create each message individually rather than use substitution, else // title and description will be only escaped, rather than parsed as wikitext if (           typeof cl.definitions[id].title === 'string' &&            typeof cl.definitions[id].description === 'string'        ) { mw.messages.set(               'codeload-entry-' + id,                mw.message('codeload-entry').plain                    .replace('$1', cl.definitions[id].title)                    .replace('$2', cl.definitions[id].description)            ); }

var $entry = $(' ').addClass('codeload-entry').append(            $(' ').attr({ type: 'checkbox', checked: cl.definitionEnabled(id), value: id           }),            $('  ').msg('codeload-entry-' + id)        );

if (!cl.definitionAvailable(id)) { $entry.hide; }

if (cl.groups) { var $group = prefGroups[cl.definitions[id].group]; if (!$group) { // no or unknown group, so add to group 'Other' if (!prefGroups.other) { addPrefsGroup('other'); }               $group = prefGroups.other; }           $group.append($entry); } else { $prefs.append($entry); }   }

function main { var $content = $('#mw-content-text');

// set document title var docTitle = document.title.split(' - '); docTitle['0'] = mw.message('codeload-heading').escaped; document.title = docTitle.join(' - ');

// set content title + blank content space $('.header-title > h1, .AdminDashboardGeneralHeader > h1, #firstHeading').msg('codeload-heading'); $content.text('');

// style mw.util.addCSS(           '.codeload-entries {' +                'margin: 25px 0;' +            '}' +            '.codeload-entry {' +                'display: block;' +            '}'        );

// assemble var $intro = $(' ').msg('codeload-intro'); $prefs = $(' ').addClass('codeload-entries'); var $buttons = $(' ').append(            $('  ')                .attr({ 'class': 'button', 'id': 'codeload-save', 'title': mw.message('codeload-save-tooltip').escaped })               .click(setPrefs)                .msg('codeload-save'),            '\n',            $('  ')                .attr({ 'class': 'button secondary', 'id': 'codeload-use', 'title': mw.message('codeload-use-tooltip').escaped })               .click(setPrefs)                .msg('codeload-use'),            '\n',            $('')                .attr({ 'href': '#', 'title': mw.message('codeload-reset-tooltip').escaped })               .click(resetPrefs)                .msg('codeload-reset')        );

if (cl.groups) { Object.keys(cl.groups).forEach(addPrefsGroup); }

if (Object.keys(cl.definitions).length) { Object.keys(cl.definitions).forEach(addPrefEntry); } else { $prefs.msg('codeload-no-definitions'); }

$content.append($intro, $prefs, $buttons);

// if local storage not available, hide the useless 'Use' button if (cl.getUserPrefs === false) { $('#codeload-use').hide; }   }

mw.loader.using('mediawiki.jqueryMsg', function {        $(main);    });

}(jQuery, mediaWiki, codeLoad));