میڈیاویکی:Gadget-Section-Links.js

نظرثانی بتاریخ 19:25، 22 جون 2023ء از dfghjk>Admin (۱ نسخه واردشده)
(فرق) → پرانا نسخہ | تازہ ترین نسخہ (فرق) | تازہ نسخہ ← (فرق)

یاد دہانی: محفوظ کرنے کے بعد ان تبدیلیوں کو دیکھنے کے لیے آپ کو اپنے براؤزر کا کیش (cache) صاف کرنا ہوگا۔

  • فائرفاکس/ سفاری: جب Reload پر کلک کریں تو Shift دباکر رکھیں، یا Ctrl-F5 یا Ctrl-R دبائیں (Mac پر R-⌘)
  • گوگل کروم: Ctrl-Shift-R دبائیں (Mac پر Shift-R-⌘)
  • انٹرنیٹ ایکسپلورر: جب Refresh پر کلک کریں تو Ctrl یا Ctrl-F5 دبائیں
  • اوپیرا: Tools → Preferences میں جائیں اور کیش (cache) صاف کریں
//Dokumentation unter [[:de:Benutzer:Schnark/js/section-links]] <nowiki>
/*global mediaWiki: true*/
(function($, mw){

mw.messages.set({
	'sections-edit': 'ویرایش بخش',
	'sections-edit0': 'ویرایش بخش آغازین',
	'sections-plus': 'افزودن زیربخش جدید',
	'sections-top': 'رفتن به بالا',
	'sections-link': 'پیوند به این زیربخش ',
	'sections-prev': 'رفتن به زیربخش قبلی',
	'sections-next': 'رفتن به زیربخش بعدی',
	'sections-done': 'افزودن {{شد}}',
	'sections-done-reason': 'شما می‌توانید متن کوتاهی را بنویسید که قبل از امضای شما نوشته شود',
	'sections-done-error': 'خطایی رخ داد!'
});

var config = {
	edit0: true, 
	tools: true, 
	plus: true, 
	top: true, 
	link: true, 
	nav: true, 
	done: true
},
configDone = true,
/*icon = {
	edit: 'data:image/gif;base64,R0lGODlhEAAQAMIFAAAAAP8AAHd3d/93AP//AP///////////yH5BAEKAAcALAAAAAAQABAAAAM0eLp88S2eMV6QaxBCodTcNmBTyJGgNmKpyIrum5KCQJ10Ta1Y7ZMKQA0ADAKIxcMxGSwmAAA7',
	plus: 'data:image/gif;base64,R0lGODlhEAAQAKECAAB3AAD/AP///////yH5BAEKAAIALAAAAAAQABAAAAIulC8AyAgRVmMvTgrlNVVflXmcAkIRuVTmypbsa6HqSTalsnFZrlt8x0vsgrhGAQA7',
	//top: 'data:image/gif;base64,R0lGODlhEAAQAKECAAAAdwAA/////////yH5BAEKAAIALAAAAAAQABAAAAItlC8AyAgRVnMQyvlivjRay2yVFx3iSC7nCirWBjtqy4Xg1NXYjRtbb+IBFZMCADs=',
	top: 'data:image/gif;base64,R0lGODlhEAAQAKECAAAAdwAA/////////yH5BAEKAAIALAAAAAAQABAAAAIyFI6ZFu3/gFH0iIuvzRyDJnXZ54QiGZ1QyZEH6HogE0hxTIe3iU9qLWrNgjIg0WPKFAAAOw==',
	//link: 'data:image/gif;base64,R0lGODlhEAAQAKECAHd3d8zMzP///////yH5BAEKAAIALAAAAAAQABAAAAIulI+ggG273FGTOlslfkyGkHTNZ23UJ0BiqqBGYHYw42ZVbedhs1ViH4JIgitHAQA7',
	link: 'data:image/gif;base64,R0lGODlhEAAQAKECADw8PGZmZv///////yH5BAEKAAIALAAAAAAQABAAAAIulI+ggG273FGTOlslfkyGkHTNZ23UJ0BiqqBGYHYw42ZVbedhs1ViH4JIgitHAQA7',
	prev: 'data:image/gif;base64,R0lGODlhEAAQAKECAAAAdwAA/////////yH5BAEKAAIALAAAAAAQABAAAAIrlC8AyAgRVnMQyvmqxVpfkylWeJDmRiabGiVX6nLtRM00uN7sR8O3/+MZCgA7',
	next: 'data:image/gif;base64,R0lGODlhEAAQAKECAAAAdwAA/////////yH5BAEKAAIALAAAAAAQABAAAAIrlC8AyAgRVmMvTgrlNVXvvh1go4iZuIzqCbYnl3WjCdUvZltXde/95zkUAAA7',
	done: 'data:image/gif;base64,R0lGODlhEAAQAKECAAB3AAD/AP///////yH5BAEKAAIALAAAAAAQABAAAAInlI+pCrAcgnsHxEmtpPVyoWGIGEJTmWgbmkaS96hw7Ir09oE2txsFADs=',
	del: 'data:image/gif;base64,R0lGODlhEAAQAKECAHcAAP8AAP///////yH5BAEKAAIALAAAAAAQABAAAAIyFC6peWAB3XoRwLjsPRfT/VVdIIUjOWlnma2TooJv3LHmWNJcbUp6zAKqbB9H48VAFAAAOw=='
},*/
icon = { //von [[Benutzer:TMg]] optimiert
	edit: 'data:image/gif;base64,R0lGODlhEAAQALMPAPqxb/1wA/+QAtHU1v/qAP+2AP/TAH' +
		'F0dp1zVOjFq/v28vyMPgAAAP8AAP//AP///yH5BAEAAA8ALAAAAAAQABAAAARU8Mk5QWuL' +
		'6hnCbRu1FI53hY8SOIbgCAEaGCVRKmHiFq4AyLYVIRYCkATHREjlGLIEqFGJFsCFEAGeo5' +
		'BBHbACA9F7+AZ+qAejfBikU4z1WzJguNMRADs=',
	plus: 'data:image/gif;base64,R0lGODlhEAAQALMJAAd2BwF3AQF4AQBhAABeAADyAIK/gg' +
		'BgAAD/AP///wAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAkALAAAAAAQABAAAARHMMlpQjAz' +
		'a1IK0WByIMgRamN5Zqm5SgM5gAZx3ANAAsN9EAZBgUQsEgsCoXGJQNZ8uV3vBgTFELOXiO' +
		'R6tbRblZbjAVcuoAgAOw==',
	top: 'data:image/gif;base64,R0lGODlhEAAQALMPAOjs8QAZY4GXvgBa/gIqcwJN2Qc2kzV' +
		'XmAAPXwBR69Pa5gAeawBi/wBe/wAUYP///yH5BAEAAA8ALAAAAAAQABAAAARmUCBHq6VIr' +
		'MG6/92wSFWwEMQSVNnjugBRFATw3q6yJE2TLApcbtcYDHxAnC6RMPQMzOSQmeqdosHH7gc' +
		'I9AKA7cJ1MCQdPcdDZzi8BFl0Q70WCF1y+v2V39/6fi4ITAiBLgIodjgRADs=',
	link: 'data:image/gif;base64,R0lGODlhEAAQALMPACkpKcjIyIuLi+3t7Z2dnWNjY0NDQ9' +
		'TU1Nzc3LW1tXx8fHJyclJSUltbWxcXF////yH5BAEAAA8ALAAAAAAQABAAAAR28Mk5RyLk' +
		'0P3Oao1hCJx0FMUgNUBQFswGLNwAKIkrGTG3KIWGy6agqAQKhqGBeDAAzU5hpiAgHI6FAU' +
		'AwwRRJBAGABSQkg0XhkSgYHLOHapJoqAIgbInQmAQQCw5zFAQGG1slB0toT2clAgAAW1wl' +
		'fgsMClElEQA7',
	prev: 'data:image/gif;base64,R0lGODlhEAAQALMPAAIqdQAZYwc2kwBc/wBP6ABEyKWzzY' +
		'KXvytPkuzw8wBR69Pa5gAeawAUYABe/////yH5BAEAAA8ALAAAAAAQABAAAARZ8MmZEEoz' +
		'58VKYYu2McQwEKD4cIRjOmg4KopgCnQqcTTAOA4GICcjKRiJADCQYChQEoQg1QA2VgwBYm' +
		'KQVR3XlUH1+IbJEjN6ol6Xre43ON6gndEHAOAgigAAOw==',
	next: 'data:image/gif;base64,R0lGODlhEAAQALMPAAIqdQAZYwc2kwBc/wBP6ABEyKWzzY' +
		'KXvytPkuzw8wBR69Pa5gAeawAUYABe/////yH5BAEAAA8ALAAAAAAQABAAAARb8Ml5ADgz' +
		'66ZU0+DTOM4XZmN5oqS5iu0rpa5mLHP8LMaECAwcbccQICQMgoKRCJACCYaCwJAspAoAg8' +
		'QAdIKTa0cwGAi+uMyV4Cg7qGnNugxfXQsF8CuBQCRAEQA7',
	done: 'data:image/gif;base64,R0lGODlhEAAQALMPAAiKCG+ybwCyANHk0QJ3Aujx6LPVsw' +
		'DSAMTdxAD0AADqAABsACeNJ0+iTwD/AP///yH5BAEAAA8ALAAAAAAQABAAAARa8MlJq7Vt' +
		'sVvLcgLATczhHMT4GITjEIi6KOA2BQYVAO5STAvBIidZJECBm8CRGD4aS8WCYjAyF4GWA0' +
		'CcVI8JgukgsiBmrt6Ac6aBGqq2Y6p6DBYAeN1etkQAADs=',
	del: 'data:image/gif;base64,R0lGODlhEAAQALMPAIMSEurc3Nm8vPoAAGUAAPLo6GsAAHY' +
		'AAG4AAOYAAObT020AANW0tPQAAP8AAP///yH5BAEAAA8ALAAAAAAQABAAAARp8Mkiq7XlE' +
		'HafIkAgAcmAcJViJMkhEYPTLKiaOA5BMUgjL4LACndAPRgLXwOBuDmKF6TS9zRaBAdqtSM' +
		'J9HAmAdcG/lk9w+d3ZiRXBUnZ6eWEHuMDA4W0rfAGLRIKGmdHBghiFRRciw8RADs='
},
css =
'h1 .editsection,' +
'h2 .editsection,' +
'h3 .editsection,' +
'h4 .editsection,' +
'h5 .editsection,' +
'h6 .editsection {' +
	'display: none;' +
'}' +
'.section-links {' +
	'display: inline-block;' +
	'margin-left: 2px;' +
	'position: relative;' +
	'top: 4px;' +
	'-moz-user-select: none;' +
	'-ms-user-select: none;' +
	'-webkit-user-select: none;' +
'}' +
'.section-links a {' +
	'height: 16px;' +
	'width: 16px;' +
	'overflow: hidden;' +
	'text-indent: -999em;' +
	'display: inline-block;' +
	'margin: 2px;' +
	'opacity: 0.35;' +
	'-moz-transition: opacity 0.2s;' +
	'-ms-transition: opacity 0.2s;' +
	'-o-transition: opacity 0.2s;' +
	'-webkit-transition: opacity 0.2s;' +
	'transition: opacity 0.2s;' +
'}' +
'h1:hover .section-links a,' +
'h2:hover .section-links a,' +
'h3:hover .section-links a,' +
'h4:hover .section-links a,' +
'h5:hover .section-links a,' +
'h6:hover .section-links a {' +
	'-moz-transition-delay: 0.2s;' +
	'-ms-transition-delay: 0.2s;' +
	'-o-transition-delay: 0.2s;' +
	'-webkit-transition-delay: 0.2s;' +
	'transition-delay: 0.2s;' +
	'opacity: 0.6;' +
'}' +
'body .section-links a:hover,' +
'body .section-links a:focus {' +
	'-moz-transition-delay: 0.1s;' +
	'-ms-transition-delay: 0.1s;' +
	'-o-transition-delay: 0.1s;' +
	'-webkit-transition-delay: 0.1s;' +
	'transition-delay: 0.1s;' +
	'opacity: 1;' +
'}',
waitStyleSheet = true,
additionalTools = true;

switch (mw.config.get('wgDBname')) {
case 'fawiki':
	configDone = {
		selector: '#autoarchiv-erledigt-baustein',
		done: 'tagging section as resolved',
		template: 'Section resolved'
	};
	break;
}

function validateURL (url) {
	return (/^((https?:)?\/)?\//).test(url);
}

function formatReason (reason) {
	if (reason === '' || reason[0] === '|') {
		return reason;
	}
	return ': ' + reason;
}

function getTitleIconActionForLink (a) {
	var $a, text;
	if (mw.config.get('wgPageName').indexOf('fa:Deletion_requests/') === 0) {
		$a = $(a);
		if (!$a.parent('span').hasClass('reqHandlerLinks2')) {
			return false;
		}
		text = $a.text();
		if (text === 'Close: Kept') {
			return [text, icon.done, clickOn(a)];
		} else if (text === 'Close: Deleted') {
			return [text, icon.del, clickOn(a)];
		}
	}
	return false;
}

function clickOn (a) {
	return function () {
		a.click();
	};
}

function scrollTop () {
	window.scrollTo(0, 0);
}

function onDone (data) {
	if (data && data.edit && data.edit.result === 'Success') {
		window.location.reload(false);
	} else {
		waitStyleSheet.disabled = true;
		window.alert(mw.msg('sections-done-error'));
	}
}

function markDone ($button) {
	var	data = $button.parent('.section-links').data(),
		edit = data.edit,
		hash = data.hash,
		title, section, reason, text, summary;
	if (!validateURL(edit)) {
		return;
	}
	title = mw.util.getParamValue('title', edit);
	section = mw.util.getParamValue('section', edit);
	reason = window.prompt(mw.msg('sections-done-reason'));
	if (reason === null) {
		return;
	}
	reason = $.trim(reason);
	text = '\n\n{{' + configDone.template + '|1=~~~~' + formatReason(reason) + '}}';
	summary = '/* ' +
		hash.replace(/_/g, ' ')
			.replace(/(\.[0-9A-F]{2})+/g, function (utf8) {
				return decodeURIComponent(utf8.replace(/\./g, '%'));
			}) +
		' */ ' + configDone.done;
	if (waitStyleSheet) {
		waitStyleSheet.disabled = false;
	} else {
		waitStyleSheet = mw.util.addCSS('* {cursor: wait !important;}');
	}
	$.post(mw.util.wikiScript('api'), {
		action: 'edit',
		format: 'json',
		title: title,
		section: section,
		summary: summary,
		minor: true,
		appendtext: text,
		token: mw.user.tokens.get('csrfToken')
	}).always(onDone);
}

function makeButton(text, icon, href) {
	var	isF = $.isFunction(href),
		$button = $(mw.html.element('a', {
			href: isF ? '#' : href,
			style: 'background-image:url(' + icon + ');',
			title: text
		}, ' ' + text));
	if (isF) {
		$button.click(function (e) {
			e.preventDefault();
			href($(this));
		});
	}
	return $button;
}

function addContainers($body) {
	var sectionEdit = false;
	$body.find('h1,h2,h3,h4,h5,h6').each(function (i, h) {
		var	$h = $(h),
			$a = $h.find('.editsection a'),
			hash = $h.find('.mw-headline').attr('id') || '',
			edit = $a.attr('href') || '',
			tools = config.tools && $a.length > 1 && $a.slice(1);
		if (!validateURL(edit)) {
			edit = '';
		} else {
			sectionEdit = true;
		}
		if (tools) {
			additionalTools = true;
		}
		if (i === 0 || hash !== '') {
			$h.append($('<div>')
				.addClass('section-links noprint')
				.data({
					hash: hash,
					edit: edit,
					tools: tools
				}));
		}
	});
	return sectionEdit;
}

function addEdit ($containers) {
	$containers.each(function (i) {
		var	$this = $(this),
			url = $this.data('edit'),
			title = mw.msg('sections-edit');

		if (i === 0) {
			if (!config.edit0) {
				return;
			}
			url = $('#ca-edit a').attr('href');
			if (url) {
				url += '&section=0';
			}
			title = mw.msg('sections-edit0');
		}
		if (validateURL(url)) {
			$this.append(makeButton(title, icon.edit, url));
		}
	});
}

function addTools ($containers) {
	if (!additionalTools) {
		return;
	}
	$containers.each(function () {
		var	$this = $(this),
			tools = $this.data('tools'),
			i, ret;
		if (tools) {
			for (i = 0; i < tools.length; i++) {
				ret = getTitleIconActionForLink(tools[i]);
				if (ret) {
					$this.append(makeButton(ret[0], ret[1], ret[2]));
				}
			}
		}
	});
}

function addPlus ($containers) {
	if (!mw.config.get('wgIsArticle') || $containers.length <= 1) {
		return;
	}
	var $plus = $('#ca-addsection a');
	if ($plus.length !== 1) {
		return;
	}
	$containers.last().append(makeButton(mw.msg('sections-plus'), icon.plus, $plus.attr('href')));
}

function addTop ($containers) {
	$containers.each(function (i) {
		if (i === 0) {
			return;
		}
		$(this).append(makeButton(mw.msg('sections-top'), icon.top, scrollTop));
	});
}

function addLink ($containers) {
	$containers.each(function (i) {
		if (i === 0) {
			return;
		}
		var $this = $(this);
		$this.append(makeButton(mw.msg('sections-link'), icon.link, '#' + $this.data('hash')));
	});
}

function addNav ($containers) {
	var $prev = false, prev = false;
	$containers.each(function () {
		var $this = $(this);
		if ($prev) {
			$prev.append(makeButton(mw.msg('sections-next'), icon.next, '#' + $this.data('hash')));
		}
		if (prev) {
			$this.append(makeButton(mw.msg('sections-prev'), icon.prev, '#' + prev));
		}
		$prev = $this;
		prev = $this.data('hash');
	});
}

function addDone ($containers) {
	$containers.each(function (i) {
		var $this = $(this);
		if (i === 0 || !validateURL($this.data('edit')) || !$this.parent().is('h2')) {
			return;
		}
		$this.append(makeButton(mw.msg('sections-done'), icon.done, markDone));
	});
}

function addButtons () {
	var	sectionEdit = addContainers(mw.util.$content),
		$containers = $('.section-links');
	if (sectionEdit) {
		addEdit($containers);
	}
	if (config.tools) {
		addTools($containers);
	}
	if (config.plus) {
		addPlus($containers);
	}
	if (config.top) {
		addTop($containers);
	}
	if (config.link) {
		addLink($containers);
	}
	if (config.nav) {
		addNav($containers);
	}
	if (sectionEdit && config.done && configDone && $(configDone.selector).length) {
		addDone($containers);
	}
}

function updateConfig (opt) {
	config[opt] = mw.user.options.get('schnark-sections-' + opt, config[opt]);
}

function init () {
	if (mw.config.get('wgAction') !== 'view' || mw.config.get('wgIsMainPage')) {
		return;
	}
	updateConfig('edit0');
	updateConfig('tools');
	updateConfig('plus');
	updateConfig('top');
	updateConfig('link');
	updateConfig('nav');
	updateConfig('done');
	mw.util.addCSS(css);
	$(addButtons);
}

mw.loader.using('mediawiki.util', init);

})(jQuery, mediaWiki);
//</nowiki>