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

No edit summary
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' }
     ];
     ];


Line 79: Line 78:
     }
     }


    // =========================
    // FAST LOAD + SYNC
    // =========================
     function applyStoredIcon() {
     function applyStoredIcon() {
         var icon = getCachedIcon() || 'rem';
         var icon = getCachedIcon() || 'rem';
Line 106: Line 102:
     }
     }


    // =========================
    // ICON SELECTOR
    // =========================
     function createIconSelector() {
     function createIconSelector() {
         var currentIcon = getCachedIcon() || 'rem';
         var currentIcon = getCachedIcon() || 'rem';
Line 144: Line 137:
                 if (remClickCount >= 5) {
                 if (remClickCount >= 5) {
                     remClickCount = 0;
                     remClickCount = 0;
                    iconId = 'rem-helper';


                     var msg = buddyMessages[Math.floor(Math.random() * buddyMessages.length)];
                     var msg = buddyMessages[Math.floor(Math.random() * buddyMessages.length)];
Line 164: Line 155:
     }
     }


    // =========================
    // SAVE + SYNC
    // =========================
     function selectIcon(iconId) {
     function selectIcon(iconId) {
    var currentIcon = getCachedIcon() || 'rem';
        var currentIcon = getCachedIcon() || 'rem';
 
    if (currentIcon === iconId) {
        if (currentIcon === iconId) {
        return;
            return;
    }
        }
 
    setCachedIcon(iconId);
        setCachedIcon(iconId);
    applyIcon(iconId);
        applyIcon(iconId);
 
    mw.loader.using(['mediawiki.api'], function () {
        mw.loader.using(['mediawiki.api'], function () {
        var api = new mw.Api();
            var api = new mw.Api();
 
        api.saveOption(PREFERENCE_KEY, iconId)
            api.saveOption(PREFERENCE_KEY, iconId)
            .done(function () {
                .done(function () {
                if (iconId !== 'rem-helper') {
                    if (iconId !== 'rem-helper') {
                    mw.notify('Icon updated successfully!', {
                        mw.notify('Icon updated successfully!', {
                        type: 'success'
                            type: 'success'
                    });
                        });
                }
                    }
            })
                })
            .fail(function () {
                .fail(function () {
                mw.notify('Failed to save icon.', {
                    mw.notify('Failed to save icon.', {
                    type: 'error'
                        type: 'error'
                });
                    });
            });
                });
    });
        });
}
    }


    // =========================
    // INIT
    // =========================
     mw.loader.using(['mediawiki.util'], function () {
     mw.loader.using(['mediawiki.util'], function () {
         applyStoredIcon();
         applyStoredIcon();