Jump to navigation
Jump to search
(mw.storage.getObject/setObject now exists) Tag: Undo |
(Don't restrict control selector to inline elements) |
||
Line 34: | Line 34: | ||
} | } | ||
$( ' | $( '.controlOpt' ).each( function() { | ||
if ( $( this ).hasClass( controlClass ) ) { | if ( $( this ).hasClass( controlClass ) ) { | ||
$( this ).show(); | $( this ).show(); |
Latest revision as of 19:16, 14 January 2023
// 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 = mw.storage.getObject( storageKey ) || {};
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.setObject( storageKey, storage );
}
$( '.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 = $( '<select></select>', {
'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 = $( '<option></option>', {
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 );
} );
} )();