پرش به محتوا

مدیاویکی:Gadget-TranslationAssistant.js: تفاوت میان نسخه‌ها

از دانشنامه علوم انسانی
imported>Ebrahim
بدون خلاصۀ ویرایش
imported>Ebrahim
بدون خلاصۀ ویرایش
خط ۲۸: خط ۲۸:
));
));
$('a', link).click(function () {
$('a', link).click(function () {
var source = mw.config.get('wgPageName');
var source = $('#translationhelper-source-langcode').text();
getTitle(mw.config.get('wgPageName'), source).then(function (title) {
getTitle(mw.config.get('wgPageName'), source).then(function (title) {
run(title, source);
run(title, source);
});
});
}).css('font-weight', 'bold');
}).css('font-weight', 'bold');
link.append(' ', $('<span contenteditable id="translationhelper">en</span>').keypress(function (e) {
link.append(' ', $('<span contenteditable id="translationhelper-source-langcode">en</span>').keypress(function (e) {
if (e.which !== 13) return;
if (e.which !== 13) return;

نسخهٔ ۲۷ ژوئن ۲۰۱۸، ساعت ۰۸:۵۸

$(function () {
	'use strict';
	
	if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) === -1) {
		return; // only for edit mode for now
	}
	
	function getTitle(page, source) {
		return $.post('//tools.wmflabs.org/linkstranslator/', {
			from: mw.config.get('wgDBname'),
			to: source + 'wiki',
			p: page
		}).then(function (result) {
			return Object.keys(result).map(function (x) { return result[x]; })[0];
		});
	}
	
	var enName = new mw.Uri().query.enName;
	
	if (new mw.Uri().query.enName !== undefined) {
		run(enName);
	} else {
		var link = $(mw.util.addPortletLink(
			'p-tb',
			'#',
			'کمک ترجمه',
			't-translationhelper'
		));
		$('a', link).click(function () {
			var source = $('#translationhelper-source-langcode').text();
			getTitle(mw.config.get('wgPageName'), source).then(function (title) {
				run(title, source);
			});
		}).css('font-weight', 'bold');
		link.append(' ', $('<span contenteditable id="translationhelper-source-langcode">en</span>').keypress(function (e) {
			if (e.which !== 13) return;
			
			$(this).blur();
			e.preventDefault();
		}).click(function () {
			document.execCommand('selectAll', false, null);
		}));
	}
	
	function run(page, source) {
		var enWikiFrame = $('<iframe>', {
			src: 'https://' + source + '.wikipedia.org' + mw.util.getUrl(page) + '#tofawikiframe',
			style: 'width: 100%; height: 100%;'
		});
		var enWikiSource = $('<textarea>', { style: 'display: none; width: 100%; height: 100%', dir: 'ltr', disabled: 'disabled' });
		var helperPage = $('<div>').append(
			$('<label>').append($('<input>', { type: 'checkbox' }).change(function () {
				if (this.checked) {
					enWikiFrame.hide();
					enWikiSource.show().val('صبر کنید…');
					$.Deferred().resolve().then(function (result) {
						return window.tofawikipassenwikisource
							? window.tofawikipassenwikisource
							: getRawPage(page, source).then(function (result) {
								return window.tofawikipassenwikisource = result;
							});
					}).then(function (result) {
						enWikiSource.val(result);
					});
				} else {
					enWikiFrame.show();
					enWikiSource.hide();
				}
			}), ' نمایش ویکی‌کد'), '<br>', enWikiFrame, enWikiSource
		);
		if (mw.config.get('tofawiki.sidebyside', true) === true) {
			$('.wikiEditor-ui .wikiEditor-ui-view').css('width', '50%');
			helperPage.attr('style', 'width: 50%; height: 500px; top: 0px; left: 0px; position: absolute;');
			helperPage.insertAfter('.wikiEditor-ui .wikiEditor-ui-view');
		} else {
			helperPage.attr('style', 'width: 100%; height: 400px;');
			helperPage.insertAfter('#wpTextbox1');
		}
	}
	
	function getRawPage(page, source) {
		return new mw.Api({ ajax: { url: '//' + source + '.wikipedia.org/w/api.php' } }).get({
			action: 'query',
			prop: 'revisions',
			titles: page,
			rvprop: 'content',
			format: 'json',
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (data) {
			return data.query.pages && !data.query.pages[-1]
				? Object.keys(data.query.pages).map(function (x) { return data.query.pages[x]; })[0].revisions[0]['*']
				: '';
		});
	}
});