From StrategyWiki, the video game walkthrough and strategy guide wiki
Revision as of 21:35, 29 April 2020 by Skizzerz (talk | contribs) (migrate to gadget and add feature to save selected controlset on a per-guide basis)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// 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 );
        }

        $( '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 = $( '<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 );
    } );
} )();