Usuari:Coet/wikistorm/articlesForDeletion.js

De la Viquipèdia, l'enciclopèdia lliure

Nota: Després de desar, heu de netejar la memòria cau del navegador per veure els canvis. En la majoria de navegadors amb Windows o Linux, premeu Ctrl+F5 o bé premeu Shift i cliqueu el botó "Actualitza" (Ctrl i "Actualitza" amb Internet Explorer). Vegeu més informació i instruccions per a cada navegador a Viquipèdia:Neteja de la memòria cau.

(function($) {
	var page = new wsPage('Viquipèdia:Esborrar pàgines/Propostes');
	
	var ArticlesForDeletion = {
		content: '',
		articles: [],
		titles: [],
		dispatch: () => {
			let matches, articles = [], titles = [];
			let pattern = /==(?<title>.*)==(?<body>[\s\S]*?)(?:(?===)|$)/g;
			
			while ((matches = pattern.exec(ArticlesForDeletion.content)) !== null) {
			    if (matches.index === pattern.lastIndex) {
			        pattern.lastIndex++;
			    }
			    var art = new Wikistorm.utils.ArticleForDeletion();
			    art.title = matches.groups.title.trim();
			    art.body = matches.groups.body;
			    art.parseVotes();
			    art.parsePromotion();
			    art.setAward();
			    articles.push(art);
			    titles.push(art.title);
			}
			ArticlesForDeletion.articles = articles;
			ArticlesForDeletion.titles = titles;
		}
	};
	var wsAfD = ArticlesForDeletion;
	
	// Add AfD Comunity portlet link.
	let portletParams = {
		portletId: 'p-Comunitat',
		href: page.url,
		title: "Propostes d'esborrament",
		id: 'pt-ws-AfDLink',
		tooltip: `Vés a ${page.title}`,
		except: page.normalized
	};
	wsAddPortletLink(portletParams);
	
	var wsTarget = '#firstHeading';
	var wsVotecountingDiv = '<div id="votecounting"></div>';
	
	Wikistorm.openDialog = (params) => {
		if(!params) {
			params = {
				target: wsTarget,
				position: 'fixed',
				modal: true
			};
		}
		
		// Close all open dialogs.
		$.each($(".ui-dialog"), function() {
	        let dlg = $(this).children(".ui-dialog-content");
	        dlg.dialog("close");
	    });
		
		// Open dialog if current page is AfD page.
		if (mw.config.get('wgPageName') === page.normalized) {
			wsAfD.content = wsAfDPage.content;
			wsAfD.dispatch();
			
			$(params.target).append(wsVotecountingDiv);
			options = {
				resizable: false,
				title: 'Escrutini',
				modal: params.modal,
				autoOpen: false,
				width: '690px',
				position: {
					my: "right top",
					at: "right top",
					of: params.target,
					collision: "none"
				},
				show: {
					effect: "blind",
					duration: 300
				}, 
				hide: {
					effect: "explode",
					duration: 300
				},
				create: (evt, ui) => {
					// Col·loquem el div de manera que no puge ni baixe mentre
					// ens movem verticalment.
					$(evt.target).parent().css({position: params.position});
				},
				close: () => {
					// Se duplica la segona vegada d'obrir-lo. https://stackoverflow.com/a/23464887
					if ($(this).data("dialog")) {
						// Ara el problema era que no sempre té el dialog inicialitzat... https://stackoverflow.com/a/33635870
			        	$(this).empty();
			        	$(this).dialog("destroy");
					}
			    }
			};
			var fieldsetId = 'ws-propostes';
			fields = '<table><tr><th>article</th><th>pos</th><th>neg</th><th>abst</th><th>dies</th><th>resultat</th><th>proponent i data inicial</th></tr>';
			for(let article of wsAfD.articles) {
				var title = Wikistorm.utils.normalizeTitle(article.title);
				var formattedDate = `${article.promotion.date.format()}`;
				var field = `<tr><td><a href="#${title}">${article.title}</a></td> `;
				field += `<td>${article.voteSummary.positive}</td> `;
				field += `<td>${article.voteSummary.negative}</td> `;
				field += `<td>${article.voteSummary.abstentions}</td> `;
				field += `<td>${article.promotion.datediff.days}</td>`;
				field += `<td>${article.award.result}</td>`;
				field += `<td><a class="mw-redirect" target="_blank" href="/wiki/User:${article.promotion.user}">${article.promotion.user}</a> ${formattedDate}</td></tr>`;
				
				fields += field;
			}
			fields += '</table>';
			$('#votecounting').append(`<fieldset id="${fieldsetId}"><legend>Propostes d'esborrament</legend>${fields}</fieldset>`);
			$('#votecounting').dialog(options).dialog('open');
		}
	};
	
	// Adding onClick event for sections
	if (typeof wsAfDPage !== 'undefined') {
		$(wsTarget).on('click', (evt) => {
			Wikistorm.openDialog();
		});
		
		let id = 0;
		$('h2').each(function() {
			++id;
			if (id>1) {
				let h2_id = `h2_votecounting_${id}`;
				let h2_tgt = `#${h2_id}`;
				$(this).attr('id', h2_id);
				$(h2_tgt).on('click', () => {
					let params = {target: h2_tgt, position: 'relative', modal: false};
					Wikistorm.openDialog(params);
				});
			}
		});
	}
})(jQuery);