MediaWiki:Gadget-friendlytalkback.js

Bách khoa toàn thư mở Wikipedia
Chú ý: Sau khi lưu thay đổi trang, bạn phải xóa bộ nhớ đệm của trình duyệt để nhìn thấy các thay đổi. Google Chrome, Firefox, Internet ExplorerSafari: Giữ phím ⇧ Shift và nhấn nút Reload/Tải lại trên thanh công cụ của trình duyệt. Để biết chi tiết và hướng dẫn cho các trình duyệt khác, xem Trợ giúp:Xóa bộ nhớ đệm.
// <nowiki>


(function($) {


/*
 ****************************************
 *** friendlytalkback.js: Talkback module
 ****************************************
 * Mode of invocation:     Tab ("Hồi âm")
 * Active on:              Any page with relevant user name (userspace, contribs, etc.)
 * Config directives in:   FriendlyConfig
 */

Twinkle.talkback = function() {

	if (!mw.config.get('wgRelevantUserName')) {
		return;
	}

	Twinkle.addPortletLink(Twinkle.talkback.callback, 'Hồi âm', 'friendly-talkback', 'Hồi âm dễ dàng');
};

Twinkle.talkback.callback = function() {
	if (mw.config.get('wgRelevantUserName') === mw.config.get('wgUserName') && !confirm("Bạn chắc chắn muốn hồi âm cho chính mình chứ?")) {
		return;
	}

	var Window = new Morebits.simpleWindow(600, 350);
	Window.setTitle('Hồi âm');
	Window.setScriptName('Twinkle');
	Window.addFooterLink('Về {{hồi âm}}', 'Template:Hồi âm');
	Window.addFooterLink('Trợ giúp Twinkle', 'WP:TW/DOC#talkback');

	var form = new Morebits.quickForm(callback_evaluate);

	form.append({ type: 'radio', name: 'tbtarget',
		list: [
			{
				label: 'Hồi âm: trang thảo luận của tôi',
				value: 'mytalk',
				checked: 'true'
			},
			{
				label: 'Hồi âm: trang thảo luận của người khác',
				value: 'usertalk'
			},
			{
				label: 'Hồi âm: trang khác',
				value: 'other'
			},
			{
				label: '"Vui lòng xem"',
				value: 'see'
			},
			{
				label: 'Thông báo trên bảng thông báo',
				value: 'notice'
			},
			{
				label: 'Bạn có thư',
				value: 'mail'
			}
		],
		event: Twinkle.talkback.changeTarget
	});

	form.append({
		type: 'field',
		label: 'Work area',
		name: 'work_area'
	});

	var previewlink = document.createElement('a');
	$(previewlink).click(function() {
		Twinkle.talkback.preview(result);  // |result| is defined below
	});
	previewlink.style.cursor = 'pointer';
	previewlink.textContent = 'Xem trước';
	form.append({ type: 'div', id: 'talkbackpreview', label: [ previewlink ] });
	form.append({ type: 'div', id: 'friendlytalkback-previewbox', style: 'display: none' });

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

	var result = form.render();
	Window.setContent(result);
	Window.display();
	result.previewer = new Morebits.wiki.preview($(result).find('div#friendlytalkback-previewbox').last()[0]);

	// We must init the
	var evt = document.createEvent('Event');
	evt.initEvent('change', true, true);
	result.tbtarget[0].dispatchEvent(evt);

	// Check whether the user has opted out from talkback
	var query = {
		action: 'query',
		prop: 'extlinks',
		titles: 'User talk:' + mw.config.get('wgRelevantUserName'),
		elquery: 'userjs.invalid/noTalkback',
		ellimit: '1'
	};
	var wpapi = new Morebits.wiki.api('Tìm nạp trạng thái không tham gia hồi âm', query, Twinkle.talkback.callback.optoutStatus);
	wpapi.post();
};

Twinkle.talkback.optout = '';

Twinkle.talkback.callback.optoutStatus = function(apiobj) {
	var $el = $(apiobj.getXML()).find('el');
	if ($el.length) {
		Twinkle.talkback.optout = mw.config.get('wgRelevantUserName') + ' không muốn nhận hồi âm';
		var url = $el.text();
		var reason = mw.util.getParamValue('reason', url);
		Twinkle.talkback.optout += reason ? ': ' + reason : '.';
	}
	$('#twinkle-talkback-optout-message').text(Twinkle.talkback.optout);
};

var prev_page = '';
var prev_section = '';
var prev_message = '';

Twinkle.talkback.changeTarget = function(e) {
	var value = e.target.values;
	var root = e.target.form;
	var old_area = Morebits.quickForm.getElements(root, 'work_area')[0];

	if (root.section) {
		prev_section = root.section.value;
	}
	if (root.message) {
		prev_message = root.message.value;
	}
	if (root.page) {
		prev_page = root.page.value;
	}

	var work_area = new Morebits.quickForm.element({
		type: 'field',
		label: 'Thông tin hồi âm',
		name: 'work_area'
	});

	root.previewer.closePreview();

	switch (value) {
		case 'mytalk':
			/* falls through */
		default:
			work_area.append({
				type: 'div',
				label: '',
				style: 'color: red',
				id: 'twinkle-talkback-optout-message'
			});
			work_area.append({
				type: 'input',
				name: 'section',
				label: 'Đề mục liên kết (tùy chọn)',
				tooltip: 'Đề mục trên trang thảo luận mà bạn để lại lời nhắn. Để trống nếu không có liên kết đề mục.',
				value: prev_section
			});
			break;

		case 'usertalk':
			work_area.append({
				type: 'div',
				label: '',
				style: 'color: red',
				id: 'twinkle-talkback-optout-message'
			});
			work_area.append({
				type: 'input',
				name: 'page',
				label: 'Thành viên (bắt buộc)',
				tooltip: 'Tên thành viên mà bạn đã để lại tin nhắn trên trang thảo luận của họ. Bắt buộc.',
				value: prev_page,
				required: true
			});

			work_area.append({
				type: 'input',
				name: 'section',
				label: 'Đề mục liên kết (tùy chọn)',
				tooltip: 'Đề mục tại trang mà bạn đã để lại tin nhắn. Để trống nếu không có đề mục liên kết.',
				value: prev_section
			});
			break;

		case 'notice':
			var noticeboard = work_area.append({
				type: 'select',
				name: 'noticeboard',
				label: 'Bảng thông báo:',
				event: function(e) {
					if (e.target.value === 'afchd') {
						Morebits.quickForm.overrideElementLabel(e.target.form.section, 'Tiêu đề của bản nháp (không bao gồm tên tiền tố): ');
						Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, false);
					} else {
						Morebits.quickForm.resetElementLabel(e.target.form.section);
						Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, true);
					}
				}
			});

			$.each(Twinkle.talkback.noticeboards, function(value, data) {
				noticeboard.append({
					type: 'option',
					label: data.label,
					value: value,
					selected: !!data.defaultSelected
				});
			});

			work_area.append({
				type: 'input',
				name: 'section',
				label: 'Chủ đề liên kết',
				tooltip: 'Tiêu đề của chủ đề có liên quan trên trang bảng thông báo.',
				value: prev_section
			});
			break;

		case 'other':
			work_area.append({
				type: 'div',
				label: '',
				style: 'color: red',
				id: 'twinkle-talkback-optout-message'
			});
			work_area.append({
				type: 'input',
				name: 'page',
				label: 'Tên trang đầy đủ (bắt buộc)',
				tooltip: "Tên trang đầy đủ nơi bạn để lại tin nhắn. Ví dụ: 'Wikipedia talk: Twinkle'. Bắt buộc.",
				value: prev_page,
				required: true
			});

			work_area.append({
				type: 'input',
				name: 'section',
				label: 'Đề mục liên kết (tùy chọn)',
				tooltip: 'Đề mục tại trang mà bạn đã để lại tin nhắn. Để trống nếu không có đề mục liên kết.',
				value: prev_section
			});
			break;

		case 'mail':
			work_area.append({
				type: 'input',
				name: 'section',
				label: 'Tiêu đề của email (tùy chọn)',
				tooltip: 'Dòng tiêu đề của email bạn đã gửi.'
			});
			break;

		case 'see':
			work_area.append({
				type: 'input',
				name: 'page',
				label: 'Tên trang đầy đủ (bắt buộc)',
				tooltip: "Tên trang đầy đủ của nơi thảo luận đang được tổ chức. Ví dụ: 'Wikipedia talk: Twinkle'. Bắt buộc.",
				value: prev_page,
				required: true
			});
			work_area.append({
				type: 'input',
				name: 'section',
				label: 'Đề mục liên kết (tùy chọn)',
				tooltip: "Tiêu đề đề mục nơi cuộc thảo luận đang được tổ chức. Ví dụ: 'Đề xuất hợp nhất'.",
				value: prev_section
			});
			break;
	}

	if (value !== 'notice') {
		work_area.append({ type: 'textarea', label: 'Tin nhắn bổ sung (tùy chọn):', name: 'message', tooltip: 'Một thông báo bổ sung mà bạn muốn để lại bên dưới mẫu thảo luận. Chữ ký của bạn sẽ được thêm vào cuối tin nhắn nếu bạn để lại.' });
	}

	work_area = work_area.render();
	root.replaceChild(work_area, old_area);
	if (root.message) {
		root.message.value = prev_message;
	}

	$('#twinkle-talkback-optout-message').text(Twinkle.talkback.optout);
};

Twinkle.talkback.noticeboards = {
	'an': {
		label: "WP:AN (Tin nhắn cho bảo quản viên)",
		text: '== ' + Twinkle.getPref('adminNoticeHeading') + ' ==\n' +
		"{{subst:ANI-notice|thread=$SECTION|noticeboard=Wikipedia:Tin nhắn cho bảo quản viên}} ~~~~",
		editSummary: 'Thông báo thảo luận tại [[Wikipedia:Tin nhắn cho bảo quản viên]]'
	},
	'an3': {
		label: "WP:AN3 (Tin nhắn cho bảo quản viên/Bút chiến)",
		text: '{{subst:An3-notice|$SECTION}} ~~~~',
		editSummary: "Thông báo thảo luận tại [[Wikipedia:Tin nhắn cho bảo quản viên/Bút chiến]]"
	},
	'ani': {
		label: "WP:ANI (Tin nhắn cho bảo quản viên/Sự cố)",
		text: '== ' + Twinkle.getPref('adminNoticeHeading') + ' ==\n' +
		"{{subst:ANI-notice|thread=$SECTION|noticeboard=Tin nhắn cho bảo quản viên/Sự cố}} ~~~~",
		editSummary: 'Thông báo thảo luận tại [[Wikipedia:Tin nhắn cho bảo quản viên/Sự cố]]',
		defaultSelected: true
	},
	// let's keep AN and its cousins at the top
	'afchd': {
		label: 'WP:AFCHD (Tạo bài viết/Giúp sử dụng Wikipedia)',
		text: '{{subst:AFCHD/u|$SECTION}} ~~~~',
		editSummary: 'Bạn có câu trả lời tại [[Wikipedia:Giúp sử dụng Wikipedia]]'
	},
	'coin': {
		label: 'WP:COIN (Bảng thông báo xung đột lợi ích)',
		text: '{{subst:Coin-notice|thread=$SECTION}} ~~~~',
		editSummary: 'Thông báo thảo luận tại [[Wikipedia:Bảng thông báo xung đột lợi ích]]'
	},
	'drn': {
		label: 'WP:DRN (Bảng thông báo giải quyết tranh chấp)',
		text: '{{subst:DRN-notice|thread=$SECTION}} ~~~~',
		editSummary: 'Thông báo thảo luận tại [[Wikipedia:Bảng thông báo giải quyết tranh chấp]]'
	},
	'effp': {
		label: 'WP:EFFP/R (Chỉnh sửa bộ lọc báo cáo dương tính giả)',
		text: '{{EFFPReply|1=$SECTION|2=~~~~}}',
		editSummary: 'Bạn có câu trả lời cho [[Wikipedia:Edit filter/False positives/Reports|edit filter false positive report]]'
	},
	'hd': {
		label: 'WP:HD (Giúp sử dụng Wikipedia)',
		text: '== Câu hỏi của bạn tại Giúp sử dụng Wikipedia ==\n' + '{{helpdeskreply|1=$SECTION|ts=~~~~~}}',
		editSummary: 'Bạn có câu trả lời tại [[Wikipedia:Giúp sử dụng Wikipedia]]'
	},
	'th': {
		label: 'WP:THQ (Teahouse question forum)',
		text: "== Phòng trà thảo luận: bạn có tin nhắn! ==\n{{WP:Teahouse/Teahouse talkback|WP:Teahouse/Questions|$SECTION|ts=~~~~}}",
		editSummary: 'Bạn có câu trả lời tại [[Wikipedia:Phòng trà]]'
	},
	'otrs': {
		label: 'WP:OTRS/N (Bảng thông báo OTRS)',
		text: '{{OTRSreply|1=$SECTION|2=~~~~}}',
		editSummary: 'Bạn có câu trả lời tại [[Wikipedia:Bảng thông báo OTRS]]'
	}
};

callback_evaluate = function(e) {
	var input = Morebits.quickForm.getInputData(e.target);

	var fullUserTalkPageName = new mw.Title(mw.config.get('wgRelevantUserName'), 3).toText();
	var talkpage = new Morebits.wiki.page(fullUserTalkPageName, 'Thêm hồi âm');

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

	Morebits.wiki.actionCompleted.redirect = fullUserTalkPageName;
	Morebits.wiki.actionCompleted.notice = 'Hồi âm hoàn tất; tải lại trang thảo luận sau vài giây';

	// Not used for notice or mail
	input.page = input.page || mw.config.get('wgUserName');

	switch (input.tbtarget) {
		case 'notice':
			talkpage.setEditSummary(Twinkle.talkback.noticeboards[input.noticeboard].editSummary);
			break;
		case 'mail':
			talkpage.setEditSummary("Thông báo: Bạn có thư");
			break;
		case 'see':
			talkpage.setEditSummary('Vui lòng kiểm tra cuộc thảo luận tại [[:' + input.page +
			(input.section ? '#' + input.section : '') + ']]');
			break;
		default:  // tbtarget one of mytalk, usertalk, other
			var editSummary = 'Hồi âm ([[:';
			if (input.tbtarget !== 'other' && !/^\s*user talk:/i.test(input.page)) {
				editSummary += 'User talk:';
			}
			talkpage.setEditSummary(editSummary + input.page + (input.section ? '#' + input.section : '') + ']])');
	}

	talkpage.setAppendText('\n\n' + Twinkle.talkback.getNoticeWikitext(input));
	talkpage.setChangeTags(Twinkle.changeTags);
	talkpage.setCreateOption('recreate');
	talkpage.setMinorEdit(Twinkle.getPref('markTalkbackAsMinor'));
	talkpage.setFollowRedirect(true);
	talkpage.append();
};

Twinkle.talkback.preview = function(form) {
	var input = Morebits.quickForm.getInputData(form);

	if (input.tbtarget !== 'notice') {
		// usertalk, other, see
		input.page = input.page || mw.config.get('wgUserName');
	}

	var noticetext = Twinkle.talkback.getNoticeWikitext(input);
	form.previewer.beginRender(noticetext, 'User_talk:' + mw.config.get('wgRelevantUserName')); // Force wikitext/correct username
};

Twinkle.talkback.getNoticeWikitext = function(input) {
	var text;

	switch (input.tbtarget) {
		case 'notice':
			text = Morebits.string.safeReplace(Twinkle.talkback.noticeboards[input.noticeboard].text, '$SECTION', input.section);
			break;
		case 'mail':
			text =
				'==' + Twinkle.getPref('mailHeading') + '==\n' +
				"{{You've got mail|subject=" + input.section + '|ts=~~~~~}}';

			if (input.message) {
				text += '\n' + input.message + '  ~~~~';
			} else if (Twinkle.getPref('insertTalkbackSignature')) {
				text += '\n~~~~';
			}
			break;
		case 'see':
			text = '{{subst:Please see|location=' + input.page + (input.section ? '#' + input.section : '') + '|more=' + input.message + '}}';
			break;
		default:  // tbtarget one of mytalk, usertalk, other
			// clean talkback heading: strip section header markers that were erroneously suggested in the documentation
			text =
				'==' + Twinkle.getPref('talkbackHeading').replace(/^\s*=+\s*(.*?)\s*=+$\s*/, '$1') + '==\n' +
				'{{talkback|' + input.page + (input.section ? '|' + input.section : '') + '|ts=~~~~~}}';

			if (input.message) {
				text += '\n' + input.message + ' ~~~~';
			} else if (Twinkle.getPref('insertTalkbackSignature')) {
				text += '\n~~~~';
			}
	}
	return text;
};

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


// </nowiki>