MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) м Отмена версии 82389, сделанной Pok (обсуждение) Метка: отмена |
||
| Строка 1: | Строка 1: | ||
;(function($, mw, rs){ | ;(function($, mw, rs){ | ||
if (mw.config.get('wgPageName') !== 'Обсуждение_участника:Pok') return; | |||
var DARK_COOKIE = 'darkmode', | |||
STICKY_HEADER_COOKIE = 'stickyheader', | |||
THEME_COOKIE = 'theme', | |||
fixedWidthEnabled = $.cookie('readermode') === 'true', | |||
theme = $.cookie(THEME_COOKIE) || ($.cookie(DARK_COOKIE) === 'true' ? 'dark' : 'light'), | |||
stickySwitch, | |||
closeButton, | |||
settingsPortletLink, | |||
themePopup; | |||
var self = { | |||
init: function () { | |||
self.createThemeButton(); | |||
self.createSettingsButton(); | |||
self.createFixedWidthToggle(); | |||
if ( $.cookie(THEME_COOKIE) == null ) { | |||
$.cookie(THEME_COOKIE, theme, {expires: 365, path: '/'}); | |||
} | |||
}, | |||
createThemeButton: function() { | |||
// Add the theme toggle button inside #user-tools | |||
themePortletLink = mw.util.addPortletLink( | |||
'user-tools', | |||
'', | |||
'pt-theme-toggle', | |||
'Change theme', | |||
null, | |||
'#user-tools' | |||
); | |||
$(themePortletLink).find('a') | |||
.addClass('oo-ui-icon-advanced') | |||
.on('click', function(e) { | |||
e.preventDefault(); | |||
if (!themePopup) { | |||
mw.loader.using(['oojs-ui-core','oojs-ui-windows','oojs-ui-widgets']).then(self.buildThemePopup); | |||
} else { | |||
themePopup.toggle(); | |||
} | |||
}); | |||
}, | |||
buildThemePopup: function() { | |||
var themeSwitch = new OO.ui.ButtonSelectWidget({ | |||
items: [ | |||
new OO.ui.ButtonOptionWidget({data: 'light', label: 'Light'}), | |||
new OO.ui.ButtonOptionWidget({data: 'dark', label: 'Dark'}) | |||
] | |||
}); | |||
themeSwitch.selectItemByData(theme); | |||
themeSwitch.on('choose', function() { | |||
theme = themeSwitch.findSelectedItem().getData(); | |||
$.cookie(THEME_COOKIE, theme, {expires: 365, path: '/'}); | |||
self.applyTheme(theme); | |||
}); | |||
themePopup = new OO.ui.PopupWidget({ | |||
$content: themeSwitch.$element, | |||
$floatableContainer: $(themePortletLink), | |||
autoClose: true | |||
}); | |||
$(document.body).append(themePopup.$element); | |||
themePopup.toggle(true); | |||
}, | |||
applyTheme: function(name) { | |||
$('body').removeClass(function(i, cls) { | |||
return (cls.match(/wgl-theme-\S+/g) || []).join(' '); | |||
}); | |||
if (name !== 'light') { | |||
mw.loader.using(['wgl.theme.' + name]).then(function() { | |||
$('body').addClass('wgl-theme-' + name); | |||
}); | |||
} | |||
}, | |||
toggleFixedWidth: function () { | |||
fixedWidthEnabled = !fixedWidthEnabled; | |||
if (fixedWidthEnabled) { | |||
$('body').addClass('wgl-fixedWidth'); | |||
mw.loader.load('wg.fixedwidth'); | |||
} else { | |||
$('body').removeClass('wgl-fixedWidth'); | |||
} | |||
$.cookie('readermode', fixedWidthEnabled, {expires: 365, path: '/'}); | |||
}, | |||
createFixedWidthToggle: function() { | |||
var link = mw.util.addPortletLink('p-personal', '', '', 'pt-fixed-width', 'Toggle fixed-width mode'); | |||
$(link).find('a').on('click', function(e) { | |||
e.preventDefault(); | |||
self.toggleFixedWidth(); | |||
}); | |||
}, | |||
createSettingsButton: function() { | |||
settingsPortletLink = mw.util.addPortletLink('p-personal', '', '', 'pt-settings', 'Appearance settings'); | |||
$(settingsPortletLink).find('a').on('click', function(e) { | |||
e.preventDefault(); | |||
mw.loader.using(['oojs-ui-core','oojs-ui-windows','oojs-ui-widgets']).then(self.openSettingsModal); | |||
}); | |||
}, | |||
openSettingsModal: function() { | |||
var stickySwitch = new OO.ui.ToggleSwitchWidget({value: $.cookie(STICKY_HEADER_COOKIE)==='true'}); | |||
stickySwitch.on('change', function() { | |||
$.cookie(STICKY_HEADER_COOKIE, stickySwitch.getValue(), {expires:365, path:'/'}); | |||
}); | |||
var closeBtn = new OO.ui.ButtonInputWidget({label: 'Close', flags: 'destructive'}); | |||
var $content = $('<div>').append( | |||
$('<h3>').text('Sticky header'), | |||
stickySwitch.$element, | |||
closeBtn.$element | |||
); | |||
rs.createOOUIWindow('settings', 'Appearance settings', {size:'medium'}, function(modal){ | |||
modal.$body.append($content); | |||
closeBtn.on('click', function(){ window.OOUIWindowManager.closeWindow(modal); }); | |||
}, true); | |||
} | |||
}; | |||
mw.loader.using(['ext.gadget.rsw-util'], function(){ | |||
$(self.init); | |||
}); | |||
}(jQuery, mediaWiki, rswiki)); | }(jQuery, mediaWiki, rswiki)); | ||