User:Monster Domosed/common.js: Difference between revisions

nop
Tag: Undo
No edit summary
Line 40: Line 40:
         { id: 'warden', name: 'Warden' },
         { id: 'warden', name: 'Warden' },
         { id: 'wraith', name: 'Wraith' },
         { id: 'wraith', name: 'Wraith' },
         { id: 'yamato', name: 'Yamato' }
         { id: 'yamato', name: 'Yamato' },
        { id: 'rem-helper', name: 'Rem Helper' }
     ];
     ];


     var PREFERENCE_KEY = 'userjs-selectedicon';
     var PREFERENCE_KEY = 'userjs-selectedicon';
    var remClickCount = 0;
    var remClickTimer = null;
    var buddyMessages = [
        "I got little buddies.",
        "They cute.",
        "They love.",
        "They help.",
        "They great at shuffleboard.",
        "They do lots of things!",
        "Hi friends!",
        "Come help!",
        "I know you'll help me."
    ];


     function getFilePath(filename) {
     function getFilePath(filename) {
Line 50: Line 66:


     function getCachedIcon() {
     function getCachedIcon() {
         return localStorage.getItem(PREFERENCE_KEY) || 'rem';
         return localStorage.getItem(PREFERENCE_KEY);
     }
     }


Line 64: Line 80:


     // =========================
     // =========================
     // FAST LOAD (NO WAIT)
     // FAST LOAD + SYNC
     // =========================
     // =========================
     function applyStoredIcon() {
     function applyStoredIcon() {
         applyIcon(getCachedIcon());
         var icon = getCachedIcon() || 'rem';


         // background sync (keeps devices in sync)
         applyIcon(icon);
        var api = new mw.Api();
        api.get({
            action: 'query',
            meta: 'userinfo',
            uiprop: 'options'
        }).done(function (data) {
            var serverIcon =
                data.query.userinfo.options[PREFERENCE_KEY] || 'rem';


             if (serverIcon !== getCachedIcon()) {
        mw.loader.using(['mediawiki.api'], function () {
                setCachedIcon(serverIcon);
            var api = new mw.Api();
                applyIcon(serverIcon);
 
             }
             api.get({
                action: 'query',
                meta: 'userinfo',
                uiprop: 'options'
            }).done(function (data) {
                var serverIcon =
                    data.query.userinfo.options[PREFERENCE_KEY] || 'rem';
 
                if (serverIcon !== icon) {
                    setCachedIcon(serverIcon);
                    applyIcon(serverIcon);
                }
             });
         });
         });
     }
     }


     // =========================
     // =========================
     // ICON SELECTOR UI
     // ICON SELECTOR
     // =========================
     // =========================
     function createIconSelector() {
     function createIconSelector() {
         var currentIcon = getCachedIcon();
         var currentIcon = getCachedIcon() || 'rem';


         var html = '<div id="usericon-selector">' +
         var html = '<div id="usericon-selector">' +
Line 113: Line 133:
         $('.usericon-option').on('click', function () {
         $('.usericon-option').on('click', function () {
             var iconId = $(this).data('icon');
             var iconId = $(this).data('icon');
            if (iconId === 'rem') {
                remClickCount++;
                clearTimeout(remClickTimer);
                remClickTimer = setTimeout(function () {
                    remClickCount = 0;
                }, 1500);
                if (remClickCount >= 5) {
                    remClickCount = 0;
                    iconId = 'rem-helper';
                    var msg = buddyMessages[Math.floor(Math.random() * buddyMessages.length)];
                    mw.notify(msg, {
                        type: 'success'
                    });
                }
            } else {
                remClickCount = 0;
            }


             $('.usericon-option').removeClass('selected');
             $('.usericon-option').removeClass('selected');
Line 128: Line 171:
         applyIcon(iconId);
         applyIcon(iconId);


         var api = new mw.Api();
         mw.loader.using(['mediawiki.api'], function () {
            var api = new mw.Api();


        api.saveOption(PREFERENCE_KEY, iconId)
            api.saveOption(PREFERENCE_KEY, iconId)
            .done(function () {
                .done(function () {
                mw.notify('Icon updated successfully!', {
                    mw.notify('Icon updated successfully!', {
                     type: 'success'
                        type: 'success'
                     });
                })
                .fail(function () {
                    mw.notify('Failed to save icon.', {
                        type: 'error'
                    });
                 });
                 });
            })
        });
            .fail(function () {
                mw.notify('Failed to save icon.', {
                    type: 'error'
                });
            });
     }
     }


Line 146: Line 191:
     // INIT
     // INIT
     // =========================
     // =========================
     mw.loader.using(['mediawiki.api', 'mediawiki.util'], function () {
     mw.loader.using(['mediawiki.util'], function () {
         applyStoredIcon();
         applyStoredIcon();