MediaWiki:Gadget-fixedWidthToggle.js: различия между версиями

Материал из Абдулопедии
Перейти к навигации Перейти к поиску
(Новая страница: «$(function() { var i18n = { toggle: 'Toggle fixed width', toastTitle: 'Fixed width toggle', failedToast: 'Could not save fixed width preference.', }; var isDefaultEnabled = mw.user.options.get('gadget-fixedWidth'), cookieOptions = { prefix: '', expires: 365 * 86400 }, portletLink = mw.util.addPortletLink( 'p-personal', '', '', ( isDefaultEnabled ? 'pt-fw-disable' : 'pt-fw-enable' ), i18n.toggle, null, $('#pt-dm-toggle, #pt-user...»)
 
м
 
Строка 1: Строка 1:
$(function() {
$(function() {
var i18n = {
var i18n = {
toggle: 'Toggle fixed width',
toggle: 'Переключить фиксированную ширину страницы',
toastTitle: 'Fixed width toggle',
toastTitle: 'Переключение режима фиксированной ширины',
failedToast: 'Could not save fixed width preference.',
failedToast: 'Не удалось переключить настройку фиксированной ширины.',
};
};



Текущая версия на 07:54, 6 января 2024

$(function() {
	var i18n = {
		toggle: 'Переключить фиксированную ширину страницы',
		toastTitle: 'Переключение режима фиксированной ширины',
		failedToast: 'Не удалось переключить настройку фиксированной ширины.',
	};

	var isDefaultEnabled = mw.user.options.get('gadget-fixedWidth'),
	cookieOptions = {
		prefix: '',
		expires: 365 * 86400
	},
	portletLink = mw.util.addPortletLink(
		'p-personal',
		'',
		'',
		( isDefaultEnabled ? 'pt-fw-disable' : 'pt-fw-enable' ),
		i18n.toggle,
		null,
		$('#pt-dm-toggle, #pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
	);
	
	if ( mw.user.isAnon() ) {
		var useFixedWidth = mw.cookie.get('fixedWidth', cookieOptions.prefix, isDefaultEnabled && 'true') === 'true';
		if ( useFixedWidth != isDefaultEnabled ) { // bool != int
			toggleFixedWidth(isDefaultEnabled);
		}
	}
	
	$(portletLink).find('a').click(function(e) {
		e.preventDefault();
		
		var isEnabled = mw.user.options.get('gadget-fixedWidth');
		if ( mw.user.isAnon() ) {
			mw.cookie.set('fixedWidth', isEnabled ? 'false' : 'true', cookieOptions);
		}
		else {
			new mw.Api().saveOption('gadget-fixedWidth', isEnabled ? 0 : 1).fail(function() {
				mw.notify(i18n.failedToast, {
					title: i18n.toastTitle,
					type: 'warn',
					tag: 'fixedWidthToggle'
				});
			});
		}
		toggleFixedWidth(isEnabled);
	});
	
	function toggleFixedWidth(isEnabled) {
		if ( isEnabled ) {
			mw.user.options.set('gadget-fixedWidth', 0);
			document.documentElement.style.setProperty('--fixed-width', '100vw');
			portletLink.id = 'pt-fw-enable';
		}
		else {
			mw.user.options.set('gadget-fixedWidth', 1);
			document.documentElement.style.setProperty('--fixed-width', '');
			mw.loader.using(['ext.gadget.fixedWidth']).then(function() {
				portletLink.id = 'pt-fw-disable';
			});
		}
	}
});