MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) м Отмена версии 82389, сделанной Pok (обсуждение) Метка: отмена |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
;(function($, mw | ;(function($, mw){ | ||
// Only run on specific page | |||
if (mw.config.get('wgPageName') !== 'Обсуждение_участника:Pok') return; | |||
var COOKIE_THEME = 'theme', | |||
COOKIE_DARK = 'darkmode', | |||
COOKIE_STICKY = 'stickyheader', | |||
COOKIE_FIXED = 'readermode', | |||
theme = $.cookie(COOKIE_THEME) || ($.cookie(COOKIE_DARK) === 'true' ? 'dark' : 'light'), | |||
fixedMode = $.cookie(COOKIE_FIXED) === 'true', | |||
popup; | |||
function applyTheme(name) { | |||
// Remove old theme classes | |||
$('body').removeClass(function(i, cls) { | |||
return (cls.match(/mw-theme-\S+/g) || []).join(' '); | |||
}); | |||
if (name !== 'light') { | |||
$('body').addClass('mw-theme-' + name); | |||
} | |||
} | |||
function initThemeToggle() { | |||
var link = mw.util.addPortletLink( | |||
'user-tools', // Portlet | |||
'#', // href | |||
'Theme', // link text | |||
'pt-theme-toggle', // id | |||
'Toggle theme', // tooltip | |||
null, // before | |||
'#user-tools' // after selector ensures inside #user-tools | |||
); | |||
$(link).find('a').on('click', function(e) { | |||
e.preventDefault(); | |||
if (!popup) { | |||
mw.loader.using(['oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets'], createPopup); | |||
} else { | |||
popup.toggle(); | |||
} | |||
}); | |||
} | |||
function createPopup() { | |||
var toggle = new OO.ui.ButtonSelectWidget({ | |||
items: [ | |||
new OO.ui.ButtonOptionWidget({data: 'light', label: 'Light'}), | |||
new OO.ui.ButtonOptionWidget({data: 'dark', label: 'Dark'}) | |||
] | |||
}); | |||
toggle.selectItemByData(theme); | |||
toggle.on('choose', function() { | |||
theme = toggle.findSelectedItem().getData(); | |||
$.cookie(COOKIE_THEME, theme, {expires: 365, path: '/'}); | |||
applyTheme(theme); | |||
}); | |||
popup = new OO.ui.PopupWidget({ | |||
$content: toggle.$element, | |||
$floatableContainer: $( '#pt-theme-toggle' ), | |||
autoClose: true | |||
}); | |||
$(document.body).append(popup.$element); | |||
popup.toggle(true); | |||
} | |||
function toggleFixedWidth() { | |||
fixedMode = !fixedMode; | |||
if (fixedMode) { | |||
mw.loader.load('wg.fixedwidth'); | |||
$('body').addClass('mw-fixed-width'); | |||
} else { | |||
$('body').removeClass('mw-fixed-width'); | |||
} | |||
$.cookie(COOKIE_FIXED, fixedMode, {expires: 365, path: '/'}); | |||
} | |||
function initFixedToggle() { | |||
var link = mw.util.addPortletLink( | |||
'p-personal', | |||
'#', | |||
'Fixed width', | |||
'pt-fixed-toggle', | |||
'Toggle fixed width' | |||
); | |||
$(link).find('a').on('click', function(e) { | |||
e.preventDefault(); | |||
toggleFixedWidth(); | |||
}); | |||
} | |||
function initStickyToggle() { | |||
var initial = $.cookie(COOKIE_STICKY) === 'true', | |||
link = mw.util.addPortletLink( | |||
'p-personal', | |||
'#', | |||
'Sticky header', | |||
'pt-sticky-toggle', | |||
'Toggle sticky header' | |||
); | |||
$(link).find('a').on('click', function(e) { | |||
e.preventDefault(); | |||
initial = !initial; | |||
$.cookie(COOKIE_STICKY, initial, {expires: 365, path: '/'}); | |||
if (initial) { | |||
$('body').addClass('mw-sticky-header'); | |||
} else { | |||
$('body').removeClass('mw-sticky-header'); | |||
} | |||
}); | |||
// apply on load | |||
if (initial) $('body').addClass('mw-sticky-header'); | |||
} | |||
// Initialization after core modules | |||
mw.loader.using(['jquery.cookie', 'oojs-ui-core'], function() { | |||
applyTheme(theme); | |||
initThemeToggle(); | |||
initFixedToggle(); | |||
initStickyToggle(); | |||
}); | |||
})(jQuery, mediaWiki); | |||
const IFRAME_CONFIG = { | const IFRAME_CONFIG = { | ||