User:Monster Domosed/common.js: Difference between revisionsGive feedback
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
document.documentElement.classList.add('usericon-loading'); | |||
var availableIcons = [ | var availableIcons = [ | ||
| Line 50: | Line 52: | ||
function getCachedIcon() { | function getCachedIcon() { | ||
return localStorage.getItem(PREFERENCE_KEY) | return localStorage.getItem(PREFERENCE_KEY); | ||
} | } | ||
| Line 63: | Line 65: | ||
} | } | ||
function applyStoredIcon() { | function applyStoredIcon() { | ||
var icon = getCachedIcon(); | |||
// fallback ONLY if nothing exists yet | |||
if (!icon) { | |||
icon = 'rem'; | |||
setCachedIcon(icon); | |||
} | |||
applyIcon(icon); | |||
if (serverIcon !== | // now safe to show UI | ||
document.documentElement.classList.remove('usericon-loading'); | |||
} | // 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 | ||
// ========================= | // ========================= | ||
function createIconSelector() { | function createIconSelector() { | ||
var currentIcon = getCachedIcon(); | var currentIcon = getCachedIcon() || 'rem'; | ||
var html = '<div id="usericon-selector">' + | var html = '<div id="usericon-selector">' + | ||
| Line 128: | Line 141: | ||
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) | |||
.done(function () { | |||
mw.notify('Icon updated and synced across devices!', { | |||
type: ' | type: 'success' | ||
}); | |||
}) | |||
.fail(function () { | |||
mw.notify('Failed to save icon.', { | |||
type: 'error' | |||
}); | |||
}); | }); | ||
}); | |||
} | } | ||
| Line 146: | Line 161: | ||
// INIT | // INIT | ||
// ========================= | // ========================= | ||
mw.loader.using([ | mw.loader.using(['mediawiki.util'], function () { | ||
applyStoredIcon(); | applyStoredIcon(); | ||