User:Trần Nguyễn Minh Huy/ToDoLister.js

// // Original from User:Evad37/ToDoLister.js /* 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 is to 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. var todo_viewnew = null;                  // Replace null with any value, e.g. "yes" (including quotation marks) to make the View link open in a new tab or window.



/* == 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 ); setDefault( 'todo_viewnew', null );

var todo_addbefore = ( todo_addfirst !== null ) ? '#todo_view':null;

var todo_usersubpage = 'User:' + mw.config.get('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( "" + todo_addlabel + "" ); //replace dummy values

if ( todo_viewnew !== null ) { $('li#todo_view > a, li#todo_view > span > a').attr( "target", "_blank" ); }

/* == 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 colon_if_needed = ( mw.config.get('wgNamespaceNumber') == 6 || mw.config.get('wgNamespaceNumber') == 14 ) ? '|3=:':''; // Colon needed for File & Category namespaces var append_content = '\n'; 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: 'ToDoLister adding 1 item: ' + mw.config.get('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( " (remove)" ); });

//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("", startloc) + 6;				var str1 = todo_contents.substr(0, startloc);				var str2 = todo_contents.substr(endloc);				var return_str = str1.concat(str2);				// Get page being removed for edit summary				var es_startloc = todo_contents.indexOf("", startloc);				var es_endloc = todo_contents.indexOf("", startloc) + 2; var es_str = todo_contents.substring(es_startloc, es_endloc); var return_es = 'ToDoLister removing 1 item: ' + es_str; edit_to_remove(return_str, return_es); }		} ).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, edit_sum) { new mw.Api.postWithToken( 'edit', {		action: 'edit',		title: 'User:' + mw.config.get('wgUserName') + '/' + todo_subpage,		text: new_text,		summary: edit_sum	} ).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 );		}	} ); } //