MediaWiki:Gadget-friendlyshared.js

出自維基百科,自由嘅百科全書

注意:儲存之後,你可能要兜過你嘅瀏覽器快取至睇到更改。Internet Explorer: 撳住Ctrl掣再撳重新整理掣。 Firefox: 撳住Shift掣再撳重新載入(又或者撳Ctrl-Shift-R)。 Google Chrome同埋Safari用戶就噉撳個重載掣。

// <nowiki>


(function($) { // eslint-disable-line no-unused-vars


/*
 ****************************************
 *** friendlyshared.js: Shared IP tagging module
 ****************************************
 * Mode of invocation:     Tab ("Shared")
 * Active on:              Existing IP user talk pages
 */

Twinkle.shared = function friendlyshared() {
	if (mw.config.get('wgNamespaceNumber') === 3 && mw.util.isIPAddress(mw.config.get('wgTitle'))) {
		var username = mw.config.get('wgRelevantUserName');
		Twinkle.addPortletLink(function() {
			Twinkle.shared.callback(username);
		}, '共用IP', 'friendly-shared', '共用IP標籤');
	}
};

Twinkle.shared.callback = function friendlysharedCallback() {
	var Window = new Morebits.simpleWindow(600, 420);
	Window.setTitle('共用IP地址標籤');
	Window.setScriptName('Twinkle');
	Window.addFooterLink('Twinkle幫手', 'WP:TW/DOC#shared');

	var form = new Morebits.quickForm(Twinkle.shared.callback.evaluate);

	var div = form.append({
		type: 'div',
		id: 'sharedip-templatelist',
		className: 'morebits-scrollbox'
	}
	);
	div.append({ type: 'header', label: '共用IP地址模' });
	div.append({ type: 'radio', name: 'template', list: Twinkle.shared.standardList,
		event: function(e) {
			Twinkle.shared.callback.change_shared(e);
			e.stopPropagation();
		}
	});

	var org = form.append({ type: 'field', label: '填其他資料(optional)然後撳「Submit」' });
	org.append({
		type: 'input',
		name: 'organization',
		label: 'IP地址主人/操作員',
		disabled: true,
		tooltip: 'You can optionally enter the name of the organization that owns/operates the IP address.  You can use wikimarkup if necessary.'
	}
	);
	org.append({
		type: 'input',
		name: 'host',
		label: '主機名(Host name)(可以唔填)',
		disabled: true,
		tooltip: 'The host name (for example, proxy.example.com) can be optionally entered here and will be linked by the template.'
	}
	);
	org.append({
		type: 'input',
		name: 'contact',
		label: '聯絡資料(請求咗嘅話)',
		disabled: true,
		tooltip: 'You can optionally enter some contact details for the organization.  Use this parameter only if the organization has specifically requested that it be added.  You can use wikimarkup if necessary.'
	}
	);

	form.append({ type: 'submit' });

	var result = form.render();
	Window.setContent(result);
	Window.display();
};

Twinkle.shared.standardList = [
	{
		label: '{{Shared IP}}: 標準共用IP地址模(未開,唔用得住)',
		value: 'Shared IP',
		tooltip: 'IP user talk page template that shows helpful information to IP users and those wishing to warn, block or ban them'
	},
	{
		label: '{{Shared IP edu}}: 教育機構用嘅共用IP地址模(未開,唔用得住)',
		value: 'Shared IP edu'
	}/*,
	{
		label: '{{Shared IP corp}}: 商業用嘅共用IP地址模',
		value: 'Shared IP corp'
	},
	{
		label: '{{Shared IP public}}: 公共地方嘅共用IP地址模',
		value: 'Shared IP public'
	},
	{
		label: '{{Shared IP gov}}: 政府機構或設施嘅共用IP地址模',
		value: 'Shared IP gov'
	},
	{
		label: '{{Dynamic IP}}: shared IP address template modified for organizations with dynamic addressing',
		value: 'Dynamic IP'
	},
	{
		label: '{{Static IP}}: shared IP address template modified for static IP addresses',
		value: 'Static IP'
	},
	{
		label: '{{ISP}}: shared IP address template modified for ISP organizations (specifically proxies)',
		value: 'ISP'
	},
	{
		label: '{{Mobile IP}}: shared IP address template modified for mobile phone companies and their customers',
		value: 'Mobile IP'
	},
	{
		label: '{{Whois}}: template for IP addresses in need of monitoring, but unknown whether static, dynamic or shared',
		value: 'Whois'
	}*/
];

Twinkle.shared.callback.change_shared = function friendlysharedCallbackChangeShared(e) {
	e.target.form.contact.disabled = e.target.value !== 'Shared IP edu';  // only supported by {{Shared IP edu}}
	e.target.form.organization.disabled = false;
	e.target.form.host.disabled = e.target.value === 'Whois';  // host= not supported by {{Whois}}
};

Twinkle.shared.callbacks = {
	main: function(pageobj) {
		var params = pageobj.getCallbackParameters();
		var pageText = pageobj.getPageText();
		var found = false;

		for (var i = 0; i < Twinkle.shared.standardList.length; i++) {
			var tagRe = new RegExp('(\\{\\{' + Twinkle.shared.standardList[i].value + '(\\||\\}\\}))', 'im');
			if (tagRe.exec(pageText)) {
				Morebits.status.warn('Info', 'Found {{' + Twinkle.shared.standardList[i].value + '}} on the user\'s talk page already...aborting');
				found = true;
			}
		}

		if (found) {
			return;
		}

		Morebits.status.info('Info', 'Will add the shared IP address template to the top of the user\'s talk page.');
		var text = '{{' + params.template + '|' + params.organization;
		if (params.contact) {
			text += '|' + params.contact;
		}
		if (params.host) {
			text += '|host=' + params.host;
		}
		text += '}}\n\n';

		var summaryText = '加咗{{[[Template:' + params.template + '|' + params.template + ']]}}模。';
		pageobj.setPageText(text + pageText);
		pageobj.setEditSummary(summaryText);
		pageobj.setChangeTags(Twinkle.changeTags);
		pageobj.setMinorEdit(Twinkle.getPref('markSharedIPAsMinor'));
		pageobj.setCreateOption('recreate');
		pageobj.save();
	}
};

Twinkle.shared.callback.evaluate = function friendlysharedCallbackEvaluate(e) {
	var params = Morebits.quickForm.getInputData(e.target);
	if (!params.template) {
		alert('你一定要揀想用嘅共用IP地址模!');
		return;
	}
	if (!params.organization) {
		alert('You must input an organization for the {{' + params.template + '}} template!');
		return;
	}

	Morebits.simpleWindow.setButtonsEnabled(false);
	Morebits.status.init(e.target);

	Morebits.wiki.actionCompleted.redirect = mw.config.get('wgPageName');
	Morebits.wiki.actionCompleted.notice = '標籤搞掂,幾秒內重新載入傾偈版';

	var wikipedia_page = new Morebits.wiki.page(mw.config.get('wgPageName'), 'User talk page modification');
	wikipedia_page.setFollowRedirect(true);
	wikipedia_page.setCallbackParameters(params);
	wikipedia_page.load(Twinkle.shared.callbacks.main);
};

Twinkle.addInitCallback(Twinkle.shared, 'shared');
})(jQuery);


// </nowiki>