User:Evad37/ToDoLister/old.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
/* Add any or all of these lines after the importScript line to set various options (the default values are shown):

	var todo_portlet = "p-personal";           // Defines which portlet menu the link is added to - see [[Help:Customizing toolbars]] for options (choose one of the valid values for portletId)
	var todo_subpage = "todo";                 // Subpage where the to-do list entry be added
	var todo_viewlabel = "View ToDo";          // Custom label for the link. Replace quoted text with your desired name.
	var todo_addlabel = "Add to ToDo";         // Custom label for the link. Replace quoted text with your desired name.
	var todo_addfirst = null;                  // Replace null with any value, e.g. "yes" (including quotation marks) to show the Add link before the View link.

*/

/* == General setup == */

// Set default options for any that haven't been set
function setDefault( option, val ) {
	if ( window[option] === undefined ) { window[option] = val; }
}

setDefault( 'todo_portlet', 'p-personal' );
setDefault( 'todo_subpage', 'todo' );
setDefault( 'todo_viewlabel', 'View ToDo' );
setDefault( 'todo_addlabel', 'Add to ToDo' );
setDefault( 'todo_addfirst', null );

if ( todo_addfirst != null ) { var todo_addbefore = '#todo_view'; } else { var todo_addbefore = null; }

var todo_usersubpage = 'User:' + wgUserName + '/' + todo_subpage

//insert view link
mw.util.addPortletLink(
todo_portlet,
'/wiki/' + todo_usersubpage,
todo_viewlabel,
'todo_view');

//insert add link
mw.util.addPortletLink(
todo_portlet,
'',                 //dummy, will be replaced
'',                 //dummy, will be replaced
'todo_add',
null,
null,
todo_addbefore);
$('li#todo_add > a, li#todo_add > span > a').replaceWith( "<a onclick='todoPrompt()'>" + todo_addlabel + "</a>" ); //replace dummy values


/* == Add items to the Todo list == */

// Prompt to get comment, then proceed to edit
function todoPrompt() {
	var todo_pcomment = prompt("Enter a comment for the to-do list");
	if ( todo_pcomment != null ) {
		var append_content = '\n{{' + 'subst:' + 'User:Evad37/ToDoLister/additem|1=' + wgPageName + '|2=' + todo_pcomment + '}}';
		process_edit(todo_usersubpage, append_content);
	}
}

//Perform edit to add entry
function process_edit(edit_title, edit_append) {
	new mw.Api().postWithToken( 'edit', {
		action: 'edit',
		title: edit_title,
		appendtext: edit_append,
		summary: '[[User:Evad37/ToDoLister|ToDoLister]] adding 1 item: [[' + wgPageName + ']]'
	} ).done( function( result, jqXHR ) {
		alert( "Added successfully" );
	} ).fail( function( code, result ) {
		if ( code === "http" ) {
			alert( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
		} else if ( code === "ok-but-empty" ) {
			alert( "Error: Got an empty response from the server" );
		} else {
			alert( "API error: " + code );
		}
	} );
}


/* == Remove items from Todo list == */

//Show remove links on todo list
$('li.todolistitem').each(function() {
	$(this).append( " (<a onclick='remove_entry(\"" + this.id + "\")'>remove</a>)" );
});

//Look through page wikitext, return a string with item removed
function remove_entry(entryID) {
	//first check click was intended
	var conf = confirm("Are you sure you want to remove this item?");
	if (conf == true) {
		//Get content of todo list
		new mw.Api().get( {
			action: 'query',
			titles: todo_usersubpage,
			prop: [ 'revisions', 'info' ],
			rvprop: 'content',
			indexpageids: 1,
			rawcontinue: ''
		} ).done( function( result, jqXHR ) {
			var todo_id = result.query.pageids
			var todo_contents = result.query.pages[ todo_id ].revisions[ 0 ][ '*' ];
			var startloc = todo_contents.indexOf("<li id=\"" + entryID + "\"");
			// sanity check: don't proceed if item with that id isn't found
			if (startloc < 0) {                                       
				alert("Uh oh, something went wrong – please remove the entry manually.");
			} else {
				var endloc = todo_contents.indexOf("</li>", startloc) + 6;
				var str1 = todo_contents.substr(0, startloc);
				var str2 = todo_contents.substr(endloc);
				var return_str = str1.concat(str2);
				edit_to_remove(return_str);
			}
		} ).fail( function( code, result ) {
			if ( code === "http" ) {
				alert( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
			} else if ( code === "ok-but-empty" ) {
				alert( "Error: Got an empty response from the server" );
			} else {
				alert( "API error: " + code );
			}
		} );
	}
}

//Perform edit to remove entry
function edit_to_remove(new_text) {
	new mw.Api().postWithToken( 'edit', {
		action: 'edit',
		title: 'User:' + wgUserName + '/' + todo_subpage,
		text: new_text,
		summary: '[[User:Evad37/ToDoLister|ToDoLister]] removing 1 item'
	} ).done( function( result, jqXHR ) {
		alert( "Removed successfully" );
		location.reload();
	} ).fail( function( code, result ) {
		if ( code === "http" ) {
			alert( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
		} else if ( code === "ok-but-empty" ) {
			alert( "Error: Got an empty response from the server" );
		} else {
			alert( "API error: " + code );
		}
	} );
}
// </nowiki>