User:Thundercraft5/global.js/CommentManagement.js

/* jshint esversion: 6, forin: true, immed: true, indent: 4, latedef: true, newcap: true, noarg: true, undef: true, undef: true, unused: true, browser: true, jquery: true, onevar: true, eqeqeq: true, multistr: true, maxerr: 999999, -W082 /* global mw, CommentManagement, importArticles: false */

/** * CommentManagement * * A script to manage comments on UCP. * * @author			Thundercraft5 (https://dev.fandom.com/wiki/User:Thundercraft5) * @version			0.1 * @license			CC-BY-SA 3.0 * @creation 1/2/2021 Thundercraft5 */

"use strict"; if (window.CommentManagement && window.CommentManagement.loaded) { return window.CommentManagement.warn('Script double loaded, exiting...'); }	/**	 * Main Script Class * 	 * @class				CommentManagement * @init				hooks * @loadcondition		canRun */	window.CommentManagement = $.extend({		// Variable for double load protection		loaded: true,		lang: 'en',
 * (function {

/**#====================================================================#		 * Default configuration options * 		 * These fields contain any default configuration variables. **#=====================================================================#		 */		/**#====================================================================#		 * Global Variables * 		 * These fields contain all data used by this script. **#=====================================================================#		 */		wg: mw.config.get([			'wgPageName',			'wgArticlePath',			'wgUserGroups',			'wgNamespaceNumber',		]), isUCP: parseFloat(mw.config.get('wgVersion')) > 1.19, namespaces: mw.config.get('wgFormattedNamespaces'), csrfToken: mw.user.tokens.values.csrfToken, version: 0.1, hooksCount: 0, /**		 * Data object for Hooks fired by this script * 		 * @data		Hooks for this script * @field		hook * @used */		hook: Object.freeze({			loaded: mw.hook('dev.CommentManagement.loaded'),		}), /**		 * Data object for All external dependencies/waitfor's data *		 * @field		imports * @data		All external dependencies/waitfor's data * @used		import */		imports: Object.freeze({			scripts: Object.freeze([ 'u:dev:MediaWiki:UI-js/code.js', 'u:dev:MediaWiki:I18n-js/code.js', ]),			style: Object.freeze([ 'u:dev:MediaWiki:CommentManagement.css', ]),			hooks: Object.freeze([ 'dev.i18n', 'dev.ui', ]),			await: Object.freeze([ 'mediawiki.api', 'mediawiki.notify', 'mediawiki.util', ]),			otherOnloadPromises: [			],		}),

/**		 * User rights level object. * Stores data about the necessary rights to preform an action. *		 * @field				rights * @parent				CommentManagement * @used				getRights */		rights: Object.freeze({			"global": Object.freeze([ 'staff', 'helper', 'global-discussions-moderator', 'wiki-manager', 'soap', ]),

"checkuser": Object.freeze([				'soap',				'helper',				'staff',				'wiki-manager',				'content-team-member',				'checkuser',				'util',			]),

"block": Object.freeze([				'sysop',				'staff',				'helper',				'bureaucrat',				'global-discussions-moderator',				'wiki-manager',				'soap',			]),

"delete": Object.freeze([				'content-moderator',				'threadmoderator',				'sysop',				'soap',				'staff',				'helper',				'global-discussions-moderator',				'wiki-manager',				'content-team-member',				'util',			]), "protect": Object.freeze([				'content-moderator',				'sysop',				'soap',				'staff',				'helper',				'global-discussions-moderator',				'wiki-manager',				'content-team-member',				'util',			]),

"move": Object.freeze([				"user",			]), }),

/**#====================================================================#		 * Loader functions * 		 * These functions initialize the script and add the event handlers * to the page. **#=====================================================================#		 */

/**		 * Main initializer script function. * Adds the hooks the imported scripts fire. * Then, it loads them. *		 * @method		hooks * @class		CommentManagement */		hooks: function { this.imports.hooks.forEach(function(v) {				mw.hook(v).add(this.onHook.bind(this, v));			}.bind(this)); this.import; },		/**		 * Function for when a hook is loaded. * This is function is called 2 times. When the hooks count reach 2, it adds the click event handlers. *		 * @method		onHook * @class		CommentManagement */		onHook: function(value, arg) { // Increment Hook count ++this.hooksCount;

// Switch hook value switch(value) { case ("dev.i18n"): this.i18n = arg; break;

case ('dev.ui'): this.ui = arg; break; }			// When hooks count reach 2, load the script if (this.hooksCount === 2) { this.i18n.loadMessages('CommentManagement', { language: this.lang }).then(this.init.bind(this)); } else return; },		/**		 * Dependency importer. * This method imports any external scripts used by this one. * 		 * @method		import * @class		CommentManagement */		import: function { this.log('importing...');

if (!this.isUCP) this.imports.await[1] = 'ext.bannerNotifications';

// Add necessary imports on top of other promises this.imports.otherOnloadPromises.push(				mw.loader.using(this.imports.await),				importArticles({ type: "script", articles: this.imports.scripts, }),				importArticles({ type: "style", articles: this.imports.style, })			);

// Load all imports $.when.apply($, this.imports.otherOnloadPromises); },

/**		 * Actual script initializer. * This function sets up the event handlers on the page * and sets the variables that cannot be loaded beforehand. *		 * @method		init * @class		CommentManagement */		init: function(i18n) { this.api = new mw.Api; this.i18n = i18n; this.wg.wgArticlePath = this.wg.wgArticlePath.replace('$1', ''), this.getParamValue = mw.util.getParamValue;

Object.freeze(this.wg); this.log('Ready');

// Fire any hooks attached to this script this.hook.loaded.fire; this.i18n.useUserLang; this.initInterface; },

/**#====================================================================#		 * Utility functions * 		 * These functions preform utilities for the script. **#=====================================================================#		 */

/**		 * Rights data function. * Returns an array of user groups for the requested action. * 		 * @method					getRights * @class					CommentManagement * @returns	{Array}			An Array of user groups for the requested action * @param {String} action	The action to request */		getRights: function(action) { action = action.toLowerCase;

switch (action.toLowerCase) { case ("global"): return this.rights[action]; case ("checkuser"): return this.rights[action]; case ("block"): return this.rights[action]; case ("protect"): return this.rights[action]; case ("delete"): return this.rights[action]; case ("move"): return this.rights[action]; }		},

/**		 * User groups rights checker function. * Checks if the user has the groups for the requested level. *		 * @method					hasRights * @class				  CommentManagement * @returns {Boolean}		Whether the user has the requested rights * @param {String} level	The action to request */		hasRights: function(level) { var rights = this.getRights(level), len = rights.length;

while (len--) { if (this.wg.wgUserGroups.indexOf(rights[len]) !== -1) { return true; }			}

return false; },

/**		 * Function to check whether the script can run. * Used outside the class. * 		 * @method		canRun * @class		CommentManagement */		canRun: function { return this.hasRights("delete"); },

/**		 * Function to load an `i18-js` message. * 		 * @method				msg * @class				CommentManagement * @param {String} 1	the message code * @param {*} arguments arguments to the message * @return {String} 	The message's contents */		msg: function { return this.i18n.msg.apply(null, arguments).plain; },

/**		 * Function to check if a value is nullish as this software * does not support the `??` operator. *		 * @method				isNullish * @class				CommentManagement * @param {*} v 		The value to check * @returns {Boolean}	Whether not the value is nullish */		isNullish: function(v) { return v === null || v === undefined; },

/**		 * Function to set up a nullish default as this software * does not support the `??` operator. * 		 * @method				nullishDefault * @class				CommentManagement * @param {*} v			The value to check and return if it is not nullish * @param {*} _default	The value to return if `v` is nullish * @returns {*} 		`v` if it is not nullish and `` if it is		 */ nullishDefault: function(v, _default) { return this.isNullish(v) ? _default : v;		},

/**		 * Function to create a logging method in the class * 		 * @method			logBuilder * @class			CommentManagement * @param {String} level - The level to log at. * @param {Arguments} args - The function arguments to call the logging function with. * @return {String} - The message that was passed to the logging function. */		logBuilder: function(level, args) { console[level.toLowerCase].apply(null, Array.from(args).unshift('[CommentManagement v' + this.version + ']:', '[' + level.toUpperCase + ']')); return args.join(' '); },

/**		 * Function to log a error message with the script's name. * 		 * @method			error * @class			CommentManagement * @param {*} arguments - The arguments to pass to the logging function. * @return {String} - The message that was passed to the logging function. */		error: function { return this.logBuilder('warn', arguments); },

/**		 * Function to log a warning message with the script's name. * 		 * @method			warn * @class			CommentManagement * @param {*} arguments - The arguments to pass to the logging function. * @return {String} - The message that was passed to the logging function. */		warn: function { return this.logBuilder('warn', arguments); },

/**		 * Function to log a debug message with the script's name. * 		 * @method			log * @class			CommentManagement * @param {*|Test} arguments - The arguments to pass to the logging function. * @return {String} - The message that was passed to the logging function. */		log: function { return this.logBuilder('warn', arguments); },

/**#=====================================================================#		 * Main functions * 		 * These functions do the main work when running this script. **#=====================================================================#		 */

}, window.CommentManagement);

if (!CommentManagement.canRun) return this.log('User does not have necessary rights, exiting...');

CommentManagement.hooks; });