User:Ricordisamoa/SectionTools.js: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
m fixes
important update: Change to section edit links
Line 7: Line 7:
$(document).ready(function(){
$(document).ready(function(){
if(wgAction!="view") return;// in 'view' mode
if(wgAction!="view") return;// in 'view' mode
$("span.editsection").each(function(i,e){
$("span.mw-editsection").each(function(i,e){
var index=$(e).children("a").first().attr("href").match(/^.+[\?&]section=([0-9]+)$/);
var index=$(e).children("a").first().attr("href").match(/^.+[\?&]section=([0-9]+)$/);
if(index==null) return;// no valid section-index
if(index==null) return;// no valid section-index

Revision as of 08:28, 3 May 2013

/* <nowiki>
 * SectionTools.js by [[User:Ricordisamoa]]
 * uses jQuery & Ajax
 * adds some links to simply *rename* or *remove* a wiki-section
 * extended and improved version of [[commons:User:Ricordisamoa/RenameSection.js]]
*/
$(document).ready(function(){
	if(wgAction!="view") return;// in 'view' mode
	$("span.mw-editsection").each(function(i,e){
		var index=$(e).children("a").first().attr("href").match(/^.+[\?&]section=([0-9]+)$/);
		if(index==null) return;// no valid section-index
		index=parseInt(index[1]);
		$.ajax({
			url:mw.util.wikiScript(),
			data:{
				title:wgPageName,
				action:"raw",
				section:index
			},
			cache:false// important
		})
		.done(function(d){
			var toolsLink="[["+(wgServer==="//meta.wikimedia.org"?"":"m:")+"User:Ricordisamoa/SectionTools|";
			var oldTitle=d.match(/^[\s\n\r]*=+\s*([^=\n\r]+[^\s=])\s*=+/)[1];
			$("<a>")
			.text("rename")
			.attr({
				title:"rename this section",
				href:"#"
			})
			.click(function(event){
				event.preventDefault();
				$(e).siblings("span.mw-headline")
				.text(oldTitle)// putting the original heading
				.attr("contenteditable",true)// make editable
				.keydown(function(evt){
					if(evt.keyCode===13){// 'enter'
						var newTitle=$(this).text();
						var newText=d.replace(/^([\s\n\r]*=+\s*)[^=\n\r]+[^\s=](\s*=+)/,"$1"+newTitle+"$2");
						$.post(
							mw.util.wikiScript("api"),
							{
								action:"edit",
								text:newText,
								title:wgPageName,
								section:index,
								summary:"/* "+oldTitle+" */ "+toolsLink+"renamed]] section into '"+newTitle+"'",
								token:mw.user.tokens.get("editToken")
							}
						)
						.done(function(){
							jsMsg("Section renamed!");
							setTimeout(function(){document.location.reload();},2500);
						})
						.fail(function(){
							jsMsg("The ajax request failed.");
						});
						return false;
					}
				});
			})
			.insertAfter($(e).children().last())
			.before("|");
			$("<a>")
			.text("remove")
			.attr({
				title:"remove this section",
				href:"#"
			})
			.click(function(event){
				event.preventDefault();
				$.post(
					mw.util.wikiScript("api"),
					{
						action:"edit",
						text:"",
						title:wgPageName,
						section:index,
						summary:"/* "+oldTitle+" */ "+toolsLink+"removed]] section",
						token:mw.user.tokens.get("editToken")
					}
				)
				.done(function(){
					jsMsg("Section removed!");
					setTimeout(function(){document.location.reload();},2500);
				})
				.fail(function(){
					jsMsg("The ajax request failed.");
				});
			})
			.insertAfter($(e).children().last())
			.before("|");
		});
	});
});
/* </nowiki> */