Кулланучы:Tohaomg/cee.js

Wikipedia — ирекле энциклопедия проектыннан ([http://tt.wikipedia.org.ttcysuttlart1999.aylandirow.tmf.org.ru/wiki/Tohaomg/cee.js latin yazuında])

Искәрмә: Мөгаен, үзгәрүләрне күрер өчен сезгә үзгәртүләр ясаганнан соң браузерыгызның кэшын чистартырга туры килер.

  • Firefox / Safari: Shift төймәсенә басып торып, кораллар панеленда Яңартырга дигәненә яки Ctrl+F5 яисә Ctrl+R (Macта ⌘+R) дигәненә басыгыз
  • Google Chrome: Ctrl+Shift+R (Macта ⌘+Shift+R) басыгыз
  • Internet Explorer / Edge: Ctrl басып тотып, Яңартырга дигәненә басыгыз, яки Ctrl+F5 басыгыз
  • Opera: басыгыз Ctrl+F5.
//https://lv.wikipedia.org/wiki/Dalībnieks:Edgars2007/cee.js һәм https://uk.wikipedia.org/wiki/Користувач:Tohaomg/cee.js" үрнәкләреннән адаптацияләнгән
(function($, mw) {
	mw.loader.load("jquery.chosen");
	mw.loader.load("mediawiki.ui.input", "text/css");
	var delsortCategories = {
		"temats": ["архитектура", "ашамлык", "икътисад", "медицина", "мәгариф", 
		  "мәдәният", "спорт", "табигать", "тарих", "фән"],
		"valsts": ["Aвстрия", "Kосово", "Tөркия", "Азәрбайҗан", "Албания", 
		  "Беларусия", "Болгария", "Босния һәм Герцеговина", "Греция", "Грузия", 
		  "Әрмәнстан", "Казакъстан", "Кырым-татар", "Латвия", "Литва", 
		  "Лужица", "Маҗарстан", "Мальта", "Польша", "Румыния һәм Moлдова", 
		  "Сербия", "Словакия", "Словения", "Төньяк Македония", "Украина", 
		  "Хорватия", "Черногория", "Чех Җөмһүрияте", "Эсперанто", "Эстония"]
	};
	var ADVERTISEMENT = " ([[Кулланучы:Tohaomg/cee.js|cee.js]]) ярдәмендә";
	if (mw.config.get('wgNamespaceNumber') == 0) {
		var portletLink = mw.util.addPortletLink('p-cactions', '#', 'Вики-яз 2020', 'pt-cee', '«Вики-яз 2020» калыбын өстәү');
		$(portletLink).click(function(e) {
			e.preventDefault();
			$("#jump-to-nav").after('<div style="border: thin solid rgb(197, 197, 197); box-shadow: 0px 3px 8px rgba(0, 0, 0, 0.25); border-radius: 3px; padding: 5px; position: relative;" id="delsort">' + 
			  '<div id="delsort-title" style="font-size: larger; font-weight: bold; text-align: center;">«Вики-яз 2020» калыбын өстәү</div>' + 
			  '<table cellspacing="5"><tr><td>Кулланучы:<td><input type="text" id="select-name" style="width:250px;" autofocus /><br>' + 
			  '<tr><td>Темалар:<td><select multiple="multiple" id="select-temats" data-placeholder="Темалар" style="width:250px;"></select><br>' + 
			  '<tr><td>Илләр яки җәмгыятьләр:<td><select multiple="multiple" id="select-valsts" data-placeholder="Илләр яки җәмгыятьләр" style="width:250px;"></select></table>' + 
			  '<button style="position: absolute; top: 5px; right: 5px;" id="close-button" class="mw-ui-button mw-ui-destructive mw-ui-quiet">Баш тарту</button>' + '</div>');
			$("#close-button").click(function(e) {
				$("#delsort").remove();
			});
			
			get_author();
			
			for (var key in delsortCategories) {
				var aaa = [];
				for (i = 0; i < delsortCategories[key].length; i++) {
					aaa.push('<option value="' + delsortCategories[key][i] + '">' + delsortCategories[key][i] + '</option>');
				}
				$("#delsort #select-" + key).append(aaa.join(''));
			}
			// Set up the chosen one (some code stolen from http://stackoverflow.com/a/27445788)
			$("#delsort select").chosen();
			$("#delsort .chzn-container").css("text-align", "left");
			// Add the button that triggers sorting
			$("#delsort").append($("<div>").css("text-align", "center").append($("<button> ")
			 .addClass("mw-ui-button").addClass("mw-ui-progressive").attr("id", "sort-button").text("Саклау").click(function(e) {
				// Make a status list
				$("#delsort").append($("<ul> ").attr("id", "status"));
				var user_name = $("#delsort #select-name").val();
				console.log(user_name);
				// Build a list of categories
				var temati_res = $("#delsort #select-temats").val() || [];
				$("#delsort #select-temats .custom-delsort-field").each(function(index, element) {
					temati_res.push($(element).val());
				});
				temati_res = temati_res.filter(Boolean); // remove empty strings
				console.log(temati_res);
				// Build a list of categories
				var reg_res = $("#delsort #select-valsts").val() || [];
				$("#delsort #select-valsts .custom-delsort-field").each(function(index, element) {
					reg_res.push($(element).val());
				});
				reg_res = reg_res.filter(Boolean); // remove empty strings
				console.log(reg_res);
				categories = {
					'user': user_name,
					'regioni': reg_res,
					'temas': temati_res
				};
				// Actually do the delsort
				saveChanges(categories);
			})));
		});
	} // End if ( mw.config.get( "wgPageName" ).indexOf('Wikipedia:Articles_for_deletion/') ... )
	/*
	 * Saves the changes to the current discussion page by adding delsort notices (if applicable) and updating the AFDC cat
	 */
	function get_author() {
		$.ajax({
			url: mw.util.wikiScript('api'),
			type: 'POST',
			dataType: 'json',
			data: {
				format: 'json',
				action: 'query',
				prop: 'revisions',
				titles: mw.config.get('wgPageName'),
				rvprop: 'user',
				rvlimit: 1,
				rvdir: "newer"
			}
		}).done(function(data) {
			try {
				var pageId = Object.keys(data.query.pages)[0];
				user = data.query.pages[pageId].revisions[0]['user'];
				$("#select-name").val(user);
			} catch (e) {
				console.log('did not get user');
			}
		});
	}
	
	function saveChanges(cats) {
		// Indicate to the user that we're doing some deletion sorting
		$("#delsort-table").remove();
		$("#delsort #sort-button").text("Sorting discussion...").prop("disabled", true).fadeOut(400, function() {
			$(this).remove();
		});
		var categoryTitleComponent = (cats.length === 1) ? ("the \"" + cats[0] + "\" category") : (cats.length + " categories");
		// Start the animation, using super-a(dvanced techniques
		var animationInterval = setInterval(function() {
			$("#delsort-dots").text($("#delsort-dots").text() + ".");
			if ($("#delsort-dots").text().length > 3) {
				$("#delsort-dots").text("");
			}
		}, 600);
		// Place (a) notification(s) on the discussion and update its AFDC cat
		postDelsortNoticesAndUpdateAfdc(cats);
	}

	function format_template(data) {
		var tpl = [];
		var dalibnieks = data['user'];
		if (dalibnieks && dalibnieks != '') tpl.push('|кулланучы=' + dalibnieks);
		var regioni = data['regioni'];
		for (i = 0; i < regioni.length; i++) {
			if (i == 0) {
				tpl.push('|ил=' + regioni[i]);
			} else {
				tpl.push('|ил' + (i + 1) + '=' + regioni[i]);
			}
		}
		var temas = data['temas'];
		for (i = 0; i < temas.length; i++) {
			if (i == 0) {
				tpl.push('|тема=' + temas[i]);
			} else {
				tpl.push('|тема' + (i + 1) + '=' + temas[i]);
			}
		}
		return '{'+'{Вики-яз 2020' + tpl.join('') + '}}';
	}

	function save_page(wikitext, cats) {
		wikitext = format_template(cats) + wikitext;
		$.ajax({
			url: mw.util.wikiScript('api'),
			type: 'POST',
			dataType: 'json',
			data: {
				format: 'json',
				action: 'edit',
				title: 'Бәхәс:' + mw.config.get('wgPageName'),
				summary: "[[Калып:Вики-яз 2020|&#123;&#123;Вики-яз 2020&#125;&#125;]] калыбы өстәлде " + ADVERTISEMENT,
				token: mw.user.tokens.get('csrfToken'),
				watchlist: "nochange",
				text: wikitext
			}
		}).done(function(data) {
			if (data && data.edit && data.edit.result && data.edit.result == 'Success') {
				console.log('good');
				$("#delsort").hide();
			} else {
				console.log('bad');
			}
		}).fail(function() {
			console.log('bad2');
		});
	}
	/*
	 * Adds some notices to the discussion page that this discussion was sorted.
	 */
	function postDelsortNoticesAndUpdateAfdc(cats) {
		var wikitext;
		$.getJSON(mw.util.wikiScript('api'), {
			format: 'json',
			action: 'query',
			prop: 'revisions',
			rvprop: 'content',
			rvlimit: 1,
			titles: 'Бәхәс:' + mw.config.get('wgPageName')
		}).done(function(data) {
			try {
				var pageId = Object.keys(data.query.pages)[0];
				wikitext = data.query.pages[pageId].revisions[0]['*'];
				console.log('saved as 1');
				save_page(wikitext, cats);
			} catch (e) {
				console.log('saved as 2');
				save_page('', cats);
			}
		});
	}
}(jQuery, mediaWiki));