User:Shining-Armor/scripts/FunctionalBatchDelete.js

// Batch Delete using functional recursion and immutability. // A lot of this isn't actually immutable because DoM manipulation does not work in such a way.

(function (document, mw, $) {	function add_error_message (message) {		var content = document.getElementById('functional_batch_delete_output').innerHTML;		var content_new = content + ' ' + message + ' ';		document.getElementById('functional_batch_delete_output').innerHTML = content_new; // This part isn't /really/ functional but it's the best that can be done	}	function add_success_message (message) {		var content = document.getElementById('functional_batch_delete_output').innerHTML;		var content_new = content + ' ' + message + '  ';		document.getElementById('functional_batch_delete_output').innerHTML = content_new; // This part isn't /really/ functional but it's the best that can be done	}	function delete_page_callback (error, data) {		if (error) {			add_error_message((data.error !== false) ? data.page + ' could not be deleted because ' + data.error : 'An unknown error was encountered while deleting ' + data.page );		} else {			add_success_message(data + ' was deleted successfully!');		}	}	function delete_page (page, callback) {		new mw.Api.post({ format: 'json', action: 'delete', title: page, reason: 'Mass deletion via Functional Batch Delete' + '', token: mw.user.tokens.get('editToken') }).done(function (result) { if (!result.error) { callback(false, page); } else { callback(true, {page: page, error: result.error.code}); }		}).fail(function { callback(true, {page: page, error: false}); });	}	function delete_pages (pages, callback) {		if (typeof pages === 'object') {			if (pages.length > 0) {				delete_page(pages[0], callback);				pages.splice(0, 1);				delete_pages(pages, callback);			}		}	}	function get_page_list {		var content = document.getElementById('functional_batch_delete_input').value;		var pages = content.split("\n");		content = '';		delete_pages(pages, delete_page_callback);	}	function close_modal {		$('#functional_batch_delete_modal').closeModal;	}	function open_delete_modal  {		$.showCustomModal('Batch Delete', ' Pages to delete (seperated by newline):  Output  ', { id: 'functional_batch_delete_modal', width: 660, buttons: [{ message: 'Start', defaultButton: true, handler: function { get_page_list; } }, {				message: 'Close', defaultButton: false, handler: function { close_modal; } }]		});	}	function add_toggle_listener (callback) {		document.getElementById('functional_batch_delete_toggle').addEventListener('click', callback);	}	function add_button {		var content = document.getElementById('my-tools-menu').innerHTML;		var content_new = ' Batch Delete' + content;		document.getElementById('my-tools-menu').innerHTML = content_new; // This part isn't /really/ functional but it's the best that can be done	}	add_button;	add_toggle_listener(open_delete_modal); })(document, mediaWiki, jQuery);