User:Monster Domosed/common.js: Difference between revisionsGive feedback
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) | return localStorage.getItem(PREFERENCE_KEY); | ||
} | } | ||
| Line 64: | Line 80: | ||
// ========================= | // ========================= | ||
// FAST LOAD | // FAST LOAD + SYNC | ||
// ========================= | // ========================= | ||
function applyStoredIcon() { | function applyStoredIcon() { | ||
var icon = getCachedIcon() || 'rem'; | |||
applyIcon(icon); | |||
if (serverIcon !== | 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 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) | |||
.done(function () { | |||
mw.notify('Icon updated successfully!', { | |||
type: ' | type: 'success' | ||
}); | |||
}) | |||
.fail(function () { | |||
mw.notify('Failed to save icon.', { | |||
type: 'error' | |||
}); | |||
}); | }); | ||
}); | |||
} | } | ||
| Line 146: | Line 191: | ||
// INIT | // INIT | ||
// ========================= | // ========================= | ||
mw.loader.using([ | mw.loader.using(['mediawiki.util'], function () { | ||
applyStoredIcon(); | applyStoredIcon(); | ||