ব্যবহারকারী:আফতাবুজ্জামান/মোবাইল-পুনর্বহাল.js

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
/**
 * mobileUndo, a script allowing reversion of edits from
 * mobile diff pages, (Special:MobileDiff)
 *
 * Additional functionality includes changing the
 * position of the thanks button and the addition of a
 * confirm dialog to the thank button, to prevent mis-clicks
 * due to both buttons being close to each other.
 * 
 * Based on the original by User:FR30799386 at [[meta:User:FR30799386/undo.js]]
 * 
 */

// <nowiki>
$.when(mw.loader.using(['mediawiki.util', 'mediawiki.api', 'oojs-ui-windows', 'oojs-ui-core', 'oojs-ui.styles.icons-editing-core']), $.ready).then(function() {

	return new mw.Api().getMessages(['editundo', 'Undo-nochange']);

}).then(function(globalmessages) {

	if (mw.config.get('wgCanonicalSpecialPageName') !== 'MobileDiff') {
		return;
	}

	if ($('.warningbox').length || !mw.config.get('wgRelevantPageIsProbablyEditable')) {
		return;
	}

    var msg = {
		"mu-success": "সফল",
		"mu-fail": "ব্যর্থ হয়েছে",
		"mu-loading": "লোড করা হচ্ছে...",
		"editsummary-ad": " ([[w:bn:ব্যবহারকারী:আফতাবুজ্জামান/মোবাইল-পুনর্বহাল.js|প]])",
		"mu-undo-successful": "পুনর্বহাল সফল হয়েছে.... পুনঃলোড করা হচ্ছে",
		"mu-empty-reponse-from-server": "সার্ভার থেকে একটি খালি প্রতিক্রিয়া পাওয়া গেছে",
		"mu-edit-summary-prompt": "আপনার সম্পাদনা সারাংশে একটি কারণ যুক্ত করুন!",
		"mu-edit-summary-prompt-placeholder": "কারণ...",
		"mu-thank-success": "ধন্যবাদ দিয়েছেন",
		"mu-thank-confirm": "প্রকাশ্য ধন্যবাদ জানাবেন?",
		"mu-thank-loading": "ধন্যবাদ জানানো হচ্ছে...",
		"mu-thank-successful": "আপনি $1 কে ধন্যবাদ জানিয়েছেন।",
		"mu-thank-failed": "$1 কে ধন্যবাদ জানানো ব্যর্থ হয়েছে।",
		"mu-http-error": "HTTP ত্রুটি: ",
		"mu-error-code": "ত্রুটি কোড: ",
		"mu-fail-begin-text": "পুনর্বহাল করা যায়নি: "
	};

	var api = new mw.Api({
		ajax: { headers: { 'Api-User-Agent': '[[w:bn:ব্যবহারকারী:আফতাবুজ্জামান/মোবাইল-পুনর্বহাল.js]]' } }
	});


	mw.util.addCSS(
		'#mw-mf-userinfo .mobileUndo-ui .mw-ui-icon.mw-ui-icon-before:before { display: none; }' +
		'#mobileUndo > a > span.oo-ui-iconElement-icon.oo-ui-icon-editUndo.oo-ui-image-invert { display: inline-block; position:initial; }' +
		'#mobileUndo > a { padding: 0.57142857em 0.9375em; }'+
		'#mw-mf-userinfo > div > div.mobileUndo-ui > a > div{ line-height: normal; }'
	);

	$('#mw-mf-userinfo .post-content').append(
		$('<div>').attr('class', 'mobileUndo-ui').css({
			'display': 'inline-block',
			'float': mw.config.get('wgVisualEditor').pageLanguageDir === 'ltr' ? 'right' : 'left'
		})
	);

	$('.mobileUndo-ui').append(
		$('.mw-mf-action-button').remove().css({
			'clear': 'none'
		})
	);

	var mobileUndo = new OO.ui.ButtonWidget({
		label: globalmessages.editundo[0].toUpperCase() + globalmessages.editundo.slice(1),
		icon: 'editUndo',
		title: 'mobileUndo',
		flags: ['destructive', 'primary'],
		id: 'mobileUndo'
	});
	$('.mobileUndo-ui').append(mobileUndo.$element);
	if (mw.config.get('wgVisualEditor').pageLanguageDir === 'ltr') {
		$('#mobileUndo').css('margin-right', '0.3em');
	} else {
		$('#mobileUndo').css('margin-left', '0.3em');
	}

	var username =
		$('.mw-mf-user-link').text() ||
		$('#mw-mf-userinfo .post-content div:nth-child(2)').text();
	var revId = $('#mw-mf-diffarea > div.mw-mf-diff-info > h3 > a').attr('href').split('&oldid=')[1];

	$('#mw-mf-userinfo .post-content .mobileUndo-ui .mw-mf-action-button').click(function(e) {
		e.preventDefault();
		OO.ui.confirm(msg['mu-thank-confirm']).done(function(confirmed) {
			if (confirmed) {
				$('.mw-mf-action-button')
					.css('pointer-events', 'none')
					.text(msg['mu-thank-loading']);

				api.postWithToken('csrf', {
					action: "thank",
					rev: revId,
				}).done(function() {
					mw.notify(msg['mu-thank-successful'].replace(/\$1/g, username));
					$('.mw-mf-action-button').text(msg['mu-thank-success']);
				}).fail(function() {
					mw.notify(msg['mu-thank-failed'].replace(/\$1/g, username));
					$('.mw-mf-action-button').text(msg['mu-failed']);
				});
			}
		});
	});

	mobileUndo.on('click', function() {
		OO.ui.prompt(msg['mu-edit-summary-prompt'], {
			textInput: {
				placeholder: msg['mu-edit-summary-prompt-placeholder']
			}
		}).done(function(customEditSummary) {

			api.getMessages(['undo-summary'], {
				amlang: mw.config.get('wgServerName').split('.wik')[0]
			}).then(function(message) {
				var editSummary = message['undo-summary'].replace(/\$1/g, revId).replace(/\$2/g, username);
				if (customEditSummary === null) {
					return;
				}
				mobileUndo.setDisabled(true);
				editSummary = editSummary + ' ' + customEditSummary + (msg['editsummary-ad']);

				// Feedback UI
				mobileUndo.setLabel(msg['mu-loading']);
				return api.postWithToken('csrf', {
					action: "edit",
					title: mw.config.get('wgRelevantPageName'),
					summary: editSummary,
					undo: revId,
				});

			}).done(function(result) {
				if (typeof result.edit.nochange !== 'undefined') {
					mobileUndo.setLabel(msg['mu-fail']);
					OO.ui.alert($('<p>').html(globalmessages['Undo-nochange'])).done(function() {});
					return;
				}
				mobileUndo.setLabel(msg['mu-success']);
				mw.notify(msg['mu-undo-successful']);
				setTimeout(function() {
					window.location.href = mw.util.getUrl('Special:MobileDiff/' + result.edit.newrevid);
				}, 1500);

			}).fail(function(code, jqxhr) {
				mobileUndo.setLabel(msg['mu-fail']);
				var details;
				if (code === 'http') {
					if (jqxhr.textStatus === 'error') {
						details = msg['mu-http-error'] + jqxhr.xhr.status;
					} else {
						details = msg['mu-http-error'] + jqxhr.textStatus;
					}
				} else if (code === 'ok-but-empty') {
					details = msg['mu-empty response-from-server'];
				} else {
					details = msg['mu-error-code'] + code + '<br>' + jqxhr.error.info;
				}
				OO.ui.alert($('<p>').html(msg['mu-fail-begin-text'] + details)).done(function() {});
			});
		});
	});

});
// </nowiki>