MediaWiki:Gadget-ControlSelector.js

// Created by User:Prod with help from User:DrBob // jQueryfied and migrated to Gadget form by User:Skizzerz ( function {    "use strict";

var storageKey = 'gadget-controlselector'; var guideName = mw.config.get( 'wgPageName' ).split( '/' )[0]; var userId = mw.config.get( 'wgUserId' ); if ( userId === null ) { userId = 0; }

userId = userId.toString; var storage = JSON.parse( mw.storage.get( storageKey ) || "{}" ); //var storage = mw.storage.getObject( storageKey ) || {}; // MW 1.34+ if ( typeof storage[userId] === "undefined" ) { storage[userId] = {}; }

var initialIndex = 0; if ( typeof storage[userId][guideName] !== "undefined" ) { initialIndex = storage[userId][guideName]; }

function selectControlSet( event, initialLoad ) { initialLoad = initialLoad || false;

var newIndex = parseInt( $( '#control_selector_select' ).val, 10 ); var controlClass = 'control' + newIndex;

// save user selection for later if ( !initialLoad ) { storage[userId][guideName] = newIndex; mw.storage.set( storageKey, JSON.stringify( storage ) ); //mw.storage.setObject( storageKey, storage ); // MW 1.34+ }

$( 'span.controlOpt' ).each( function {           if ( $( this ).hasClass( controlClass ) ) {                $( this ).show;            } else {                $( this ).hide;            }        } ); }

$( function {        var controlDiv = $( '#control_selector_inner' );        if ( controlDiv.length === 0 ) {            return false;        }

var ControlSelector = $( ' ', {            'class': 'ControlSet',            id: 'control_selector_select'        } );

var sysTexts = controlDiv.text.split( ',' ); var i, opt; for ( i = 0; i < Math.min( 10, sysTexts.length ); ++i ) { opt = $( ' ', {                value: i,                text: sysTexts[i].trim            } );

ControlSelector.append( opt ); }

ControlSelector.val( initialIndex ); ControlSelector.change( selectControlSet ); controlDiv.empty.append( ControlSelector ); controlDiv.parent.css( 'display', 'block' );

// toggle page visibility to user preferred value selectControlSet( null, true ); } ); } );