MediaWiki:Gadget-Adiutor-RFD-Opinion.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*
 * Adiutor: Adiutor enables versatile editing options and modules to assist a variety of user actions to enhance the Wikipedia editing experience.
 * Author: Vikipolimer
 * Learn more at: https://meta.wikimedia.org/wiki/Adiutor
 * Licensing and Attribution: Licensed under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
 * Module: AfD Helper
 */
/* <nowiki> */
// Get essential configuration from MediaWiki
var mwConfig = mw.config.get(["skin", "wgAction", "wgArticleId", "wgPageName", "wgNamespaceNumber", "wgTitle", "wgUserGroups", "wgUserName", "wgUserEditCount", "wgUserRegistration", "wgCanonicalNamespace"]);
var api = new mw.Api();
var wikiId = mw.config.get('wgWikiID');
var adiutorUserOptions = JSON.parse(mw.user.options.get('userjs-adiutor-'+wikiId));
var subPageRegex = /(Deletion_requests\/)/;
var pageNameWithoutPrefix = mwConfig.wgPageName.replace(/^Commons:Deletion_requests\//, '');
var rationaleInput;
var previewWikitext, firstMonthOfDateString, firstDateMatch, selectedOption, opinionTemplate, pageName, header_warn;
var InputFilled = false;
console.log(InputFilled);
rationaleInput = new OO.ui.MultilineTextInputWidget({
	placeholder: 'Please give your opinion on this nomination',
	indicator: 'required',
	notices: ['This field is required, please do not leave it blank.'],
	classes: ['afd-helper-comment-textarea']
});
var header_warn = new OO.ui.MessageWidget({
	type: 'notice',
	inline: true,
	label: new OO.ui.HtmlSnippet('Nominations for deletion are not a voting process!<br><small>Decisions in deletion nominations are made based on policies and guidelines, not the number of opinions.</small>')
});
var page_name = new OO.ui.MessageWidget({
	type: 'notice',
	icon: 'article',
	label: new OO.ui.HtmlSnippet('<a href="/wiki/'.concat(pageNameWithoutPrefix.trim(), '" target="_blank">').concat(pageNameWithoutPrefix.trim(), "</a>")),
	classes: ['afd-helper-page-name']
});

function opinionDialog(config, buttonSelect) {
	opinionDialog.super.call(this, config, buttonSelect);
}
OO.inheritClass(opinionDialog, OO.ui.ProcessDialog);
opinionDialog.static.title = 'Adiutor (Beta) - Opinion for RFD';
opinionDialog.static.name = 'opinionDialog';
opinionDialog.static.actions = [{
	action: 'continue',
	modes: 'edit,preview',
	label: 'Add Opinion',
	disabled: InputFilled,
	icon: 'add',
	flags: ['primary', 'progressive']
}, {
	modes: 'edit',
	label: 'Cancel',
	flags: 'safe',
	icon: 'close'
}, {
	action: 'back',
	modes: 'about,preview',
	label: 'Back',
	flags: 'safe'
}, {
	action: 'preview',
	modes: 'edit,preview',
	label: 'Preview',
	icon: 'eye',
	align: 'right'
}];
opinionDialog.prototype.initialize = function() {
	opinionDialog.super.prototype.initialize.apply(this, arguments);
	var buttonOptions = [{
		id: 1,
		label: 'Keep',
		title: 'Keep',
		template: '*{{Vote keep}}'
	}, {
		id: 2,
		label: 'Speedy keep',
		title: 'Speedy keep',
		template: '*{{Vote speedykeep}}'
	}, {
		id: 3,
		label: 'Delete',
		title: 'Delete',
		template: '*{{Vote delete}}'
	}, {
		id: 4,
		label: 'Speedy delete',
		title: 'Speedy delete',
		template: '*{{Vote speedy}}'
	}, {
		id: 5,
		label: 'Neutral',
		title: 'Neutral',
		template: '*{{Neutral}}'
	}, {
		id: 6,
		label: 'Comment',
		title: 'Comment',
		template: '*{{Comment}}'
	}];
	var buttonSelect = new OO.ui.ButtonSelectWidget({
		items: buttonOptions.map(function(option) {
			return new OO.ui.ButtonOptionWidget({
				id: option.id,
				label: option.label,
				title: option.title,
				icon: option.icon,
				invisibleLabel: option.invisibleLabel,
				data: option.template,
				classes: ['afd-opinion-select-btn']
			});
		}),
		classes: ['afd-opinion-select-btn-group']
	});
	buttonSelect.on('select', function() {
		selectedOption = buttonSelect.findSelectedItem();
		updatePreview();
		console.log(opinionTemplate);
	});
	rationaleInput.off('input').on('input', function() {
		updatePreview();
		console.log(previewWikitext);
	});
	rationaleInput.on('change', function() {
		updatePreview();
		InputFilled = true;
		console.log(previewWikitext);
	});

	function updatePreview() {
		var inputValue = rationaleInput.getValue();
		opinionTemplate = (selectedOption ? selectedOption.data : '') + ' ' + inputValue + ' ~~~~';
		previewWikitext = opinionTemplate;
	}
	this.opinionPanel = new OO.ui.PanelLayout({
		padded: true,
		expanded: false
	});
	this.opinionPanel.$element.append(header_warn.$element, page_name.$element, '</br>', buttonSelect.$element, '</br>', rationaleInput.$element);
	this.panel2 = new OO.ui.PanelLayout({
		padded: true,
		expanded: false
	});
	this.stackLayout = new OO.ui.StackLayout({
		items: [this.opinionPanel]
	});
	this.$body.append(this.stackLayout.$element);
};
opinionDialog.prototype.getSetupProcess = function(data) {
	return opinionDialog.super.prototype.getSetupProcess.call(this, data).next(function() {
		this.actions.setMode('edit');
	}, this);
};
opinionDialog.prototype.getActionProcess = function(action) {
	if(action === 'about') {
		this.actions.setMode('about');
		//
	} else if(action === 'back') {
		this.actions.setMode('edit');
		this.stackLayout.setItem(this.opinionPanel);
	} else if(action === 'continue') {
		var dialog = this;
		return new OO.ui.Process(function() {
			if(rationaleInput.value != "") {
				addOpinion(rationaleInput, adiutorUserOptions);
				dialog.close();
			} else {
				alertDialog("Express your opinion!", "Decisions in deletion nominations are made based on policies and guidelines, not the number of opinions.");
			}
		});
	}
	if(action === 'preview') {
		api.get({
			action: 'parse',
			text: previewWikitext,
			disablelimitreport: 1,
			disabletidy: 1,
			prop: 'text',
			format: "json"
		}).done(function(data) {
			function WikiPreviewDialog(config) {
				WikiPreviewDialog.super.call(this, config);
			}
			OO.inheritClass(WikiPreviewDialog, OO.ui.ProcessDialog);
			WikiPreviewDialog.static.name = 'WikiPreviewDialog';
			WikiPreviewDialog.static.title = 'Preview';
			WikiPreviewDialog.static.actions = [{
				action: 'save',
				modes: 'edit,preview',
				label: 'Add Opinion',
				icon: 'add',
				flags: ['primary', 'progressive']
			}, {
				label: 'Close',
				flags: 'safe'
			}];
			WikiPreviewDialog.prototype.initialize = function() {
				WikiPreviewDialog.super.prototype.initialize.apply(this, arguments);
				this.content = new OO.ui.PanelLayout({
					padded: true,
					expanded: false
				});
				this.content.$element.html(data.parse.text['*']);
				this.$body.append(this.content.$element);
			};
			WikiPreviewDialog.prototype.getActionProcess = function(action) {
				var dialog = this;
				if(action) {
					return new OO.ui.Process(function() {
						addOpinion(rationaleInput, adiutorUserOptions);
						dialog.close({
							action: action
						});
					});
				}
				return WikiPreviewDialog.super.prototype.getActionProcess.call(this, action);
			};
			var windowManager = new OO.ui.WindowManager();
			$(document.body).append(windowManager.$element);
			var dialog = new WikiPreviewDialog({
				size: 'full',
			});
			windowManager.addWindows([dialog]);
			windowManager.openWindow(dialog);
		});
	}
	return opinionDialog.super.prototype.getActionProcess.call(this, action);
};
var windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
var dialog = new opinionDialog({
	size: 'large',
	classes: ['afd-helper-window']
});
windowManager.addWindows([dialog]);
windowManager.openWindow(dialog);

function alertDialog(title, message) {
	var AlertDialog = new OO.ui.MessageDialog();
	var windowManager = new OO.ui.WindowManager();
	$(document.body).append(windowManager.$element);
	windowManager.addWindows([AlertDialog]);
	windowManager.openWindow(AlertDialog, {
		title: title,
		message: message,
	});
}

function addOpinion() {
	api.postWithToken('csrf', {
		action: 'edit',
		title: mwConfig.wgPageName,
		appendtext: '\n' + opinionTemplate,
		summary: 'Opinion on the nomination',
		tags: 'Adiutor',
		format: 'json'
	}).done(function() {
		location.reload();
	});
}
/* </nowiki> */