Медијавики:ActivateGadget.js

Iz Vojne Enciklopedije
Пређи на навигацију Пређи на претрагу

Напомена: Пошто објавите измене, можда ћете морати да обришете кеш прегледача како бисте их видели.

  • Firefox / Safari: Држите Shift и кликните на Reload или притисните Ctrl-F5 или Ctrl-R (⌘-R на Mac-у).
  • Google Chrome: Притисните Ctrl-Shift-R (⌘-Shift-R на Mac-у).
  • Internet Explorer / Edge: Држите Ctrl и кликните на Refresh или притисните Ctrl-F5.
  • Opera: Притисните Ctrl-F5.
/**
 * Evaluates the URL for the gadgetname query param
 * If the URL param is present, it looks up whether such a gadget exists
 * and prompts whether to activate the gadget.
 *
 * @rev 1 (2013-01-06)
 * @author Rillke, 2012
 */
// List the global variables for jsHint-Validation. Please make sure that it passes http://jshint.com/
// Scheme: globalVariable:allowOverwriting[, globalVariable:allowOverwriting][, globalVariable:allowOverwriting]
/*global jQuery:false, mediaWiki:false*/

// Set jsHint-options. You should not set forin or undef to false if your script does not validate.
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, curly:false, browser:true*/

(function($, mw) {
	"use strict";

	mw.loader.using(['jquery.form', 'jquery.ui.dialog', 'jquery.ui.progressbar',  'mediawiki.user', 'mediawiki.util', 'ext.gadget.libJQuery', 'ext.gadget.SettingsManager'], function() {


		var gadgetname = mw.util.getParamValue('gadgetname'),
			deactivate = mw.util.getParamValue('disablegadget'),
			gadget = mw.libs.settingsManager.gadget(gadgetname),
			headingAnon = "Please sign-in to activate gadget $1".replace('$1', gadgetname),
			headingNormal = "Confirm $1 of gadget $2".replace('$1', deactivate ? "deactivation" : "activation").replace('$2', gadgetname),
			UNUSED_PARAM = $.noop;

		// If the gadgetname URL parameter wasn't supplied, silently fail
		if (!gadgetname) return;

		// Check whether such a gadget exists
		if (null === gadget.getState()) return;

		// Check whether it is already enabled/disabled
		if (deactivate) {
			if (!gadget.isEnabled()) return mw.notify("The $1 gadget is already disabled in your preferences.".replace('$1', gadgetname));
		} else {
			if (gadget.isEnabled()) return mw.notify("The $1 gadget is already enabled in your preferences.".replace('$1', gadgetname));
		}
		


		var $dlg = $('<div>').hide().appendTo('#mw-content-text');
		if (mw.user.isAnon()) {
			// Ask the user to login
			$dlg.attr({
				title: headingAnon
			}).load('//vojnaenciklopedija.com/index.php?title=Special:UserLogin #userloginForm', function() {
				$(this).dialog({
					width: 600
				}).find('form:first').ajaxForm(function(responseText, statusText, xhr, $form) {
					UNUSED_PARAM($form);
					
					if (!responseText || responseText.indexOf('errorbox') < 0) {
						// reload this page
						window.location = window.location;
					} else {
						var $logInNode = $('#pt-login');
						var logInLink = ($logInNode.find('a').length ? $logInNode.find('a').attr('href') : $logInNode.attr('href')) || mw.util.wikiScript() + '?' + $.param({
							title: 'Special:UserLogin',
							uselang: mw.config.get('wgUserLanguage'),
							returnto: mw.config.get('wgPageName')
						});
						window.location = logInLink;
					}
				});

			});
		} else {
			var $aboutGadget = $('<div>').css({
				'min-height': '50px',
				'border': '1px solid grey',
				'padding': '5px'
			});
			$dlg.attr({
				title: headingNormal
			}).text("Would you like to $1 gadget $2?".replace('$1', deactivate ? "deactivate" : "activate").replace('$2', gadgetname)).append($aboutGadget).dialog({
				width: 600,
				buttons: {
					"Yes": function() {
						var $prog = $('<div>').css({
							'height': '50px'
						}).progressbar();
						var action = deactivate ? 'disable' : 'enable';
						
						$dlg.parent().find('button').button({
							disabled: true
						});
						$dlg.text('').append($prog);
						if (!deactivate) gadget.load();
						gadget[action](function() {
							$prog.progressbar({
								value: 100
							});
							var $successtext = $('<div>').text("Gadget $1 successfully. Please reload the pages to see any effect.".replace('$1', deactivate ? "deactivated" : "activated"));
							$dlg.append($successtext);
							$successtext.load(mw.util.wikiScript() + '?' + $.param({
								title: 'Template:Purge_client_cache/render',
								uselang: mw.config.get('wgUserLanguage'),
								action: 'render'
							}));
							setTimeout(function() {
								window.location = mw.util.getUrl(mw.config.get('wgPageName'));
							}, 4500);
						}, function() {
							var $errtext = $('<div>').text("Failed to change the gadget's state in your preferences. We regret the inconvenience. Manually change your preferences, please. Thank you.");
							$dlg.append($errtext);
							setTimeout(function() {
								window.location = mw.util.getUrl('Special:Preferences') + '#mw-prefsection-gadgets';
							}, 4500);
						});

					},
					"No": function() {
						$dlg.dialog('close');
						$dlg.remove();
						mw.notify("You have probably entered the wrong URL in the address bar of your browser or followed the wrong link.");
					}
				},
				open: function() {
					// Look out for http://bugs.jqueryui.com/ticket/6830 / jQuery UI 1.9
					var $buttons = $(this).parent().find('.ui-dialog-buttonpane button'),
						$submitButton = $buttons.eq(0).specialButton('proceed').focus(),
						$cancelButton = $buttons.eq(1).specialButton('cancel');
						
					UNUSED_PARAM($submitButton);
					UNUSED_PARAM($cancelButton);
				}
			});
			$.get(mw.util.wikiScript('api'), {
				format: 'json',
				action: 'parse',
				title: mw.config.get('wgPageName'),
				redirects: 1,
				prop: 'text',
				uselang: mw.config.get('wgUserLanguage'),
				text: '{{int:Gadget-' + gadgetname + '}}'
			}, function(r) {
				$aboutGadget.append(r.parse.text['*']);
			});
		}


	});


}(jQuery, mediaWiki));