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

No edit summary
nop
Tag: Undo
Line 1: Line 1:
(function () {
(function () {
     'use strict';
     'use strict';
    document.documentElement.classList.add('usericon-loading');


     var availableIcons = [
     var availableIcons = [
Line 52: Line 50:


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


Line 65: Line 63:
     }
     }


    // =========================
    // FAST LOAD (NO WAIT)
    // =========================
     function applyStoredIcon() {
     function applyStoredIcon() {
         var icon = getCachedIcon();
         applyIcon(getCachedIcon());
 
        // fallback ONLY if nothing exists yet
        if (!icon) {
            icon = 'rem';
            setCachedIcon(icon);
        }


         applyIcon(icon);
         // background sync (keeps devices in sync)
        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';


        // now safe to show UI
             if (serverIcon !== getCachedIcon()) {
        document.documentElement.classList.remove('usericon-loading');
                setCachedIcon(serverIcon);
 
                applyIcon(serverIcon);
        // background sync with server (cross-device sync)
             }
        mw.loader.using(['mediawiki.api'], function () {
            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 !== icon) {
                    setCachedIcon(serverIcon);
                    applyIcon(serverIcon);
                }
             });
         });
         });
     }
     }


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


         var html = '<div id="usericon-selector">' +
         var html = '<div id="usericon-selector">' +
Line 141: Line 128:
         applyIcon(iconId);
         applyIcon(iconId);


         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 () {
                    mw.notify('Icon updated and synced across devices!', {
                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 161: Line 146:
     // INIT
     // INIT
     // =========================
     // =========================
     mw.loader.using(['mediawiki.util'], function () {
     mw.loader.using(['mediawiki.api', 'mediawiki.util'], function () {
         applyStoredIcon();
         applyStoredIcon();