Jump to content

User:This, that and the other/friendlywhisperback.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.
/*
  Twinkle whisperback module
  
  Note that this will load Twinkle even if you don't have it 
*/

var whisperback = function() {

	if ( Morebits.getPageAssociatedUser() === false ) {
		return;
	}

	$(mw.util.addPortletLink( "p-cactions", "javascript:whisperback.callback()", "WB", "friendly-whisperback", "Easy whisperback" ));
};

mw.loader.using(["ext.gadget.Twinkle"], whisperback);

whisperback.callback = function( ) {
	if( Morebits.getPageAssociatedUser() === mw.config.get("wgUserName") && !confirm("Is it really so bad that you're whispering back to yourself?") ){
		return;
	}

	var Window = new Morebits.simpleWindow( 600, 350 );
	Window.setTitle("Whisperback");
	Window.setScriptName("AJL's Scripts");
	Window.addFooterLink( "About {{whisperback}}", "Template:Whisperback" );

	var form = new Morebits.quickForm( whisperback.callback_evaluate );

	form.append({ type: "radio", name: "tbtarget",
				list: [
					{
						label: "Whisperback: my talk page",
						value: "mytalk",
						checked: "true" 
					},
					{
						label: "Whisperback: other user talk page",
						value: "usertalk"
					},
					{
						label: "Whisperback: other page",
						value: "other"
					}
				],
				event: whisperback.callback_change_target
			});

	form.append({
			type: "field",
			label: "Work area",
			name: "work_area"
		});

	form.append({ type: "submit" });

	var result = form.render();
	Window.setContent( result );
	Window.display();

	// We must init the
	var evt = document.createEvent("Event");
	evt.initEvent( "change", true, true );
	result.tbtarget[0].dispatchEvent( evt );
};

whisperback.prev_page = "";
whisperback.prev_section = "";
whisperback.prev_message = "";

whisperback.callback_change_target = function( e ) {
	var value = e.target.values;
	var root = e.target.form;
	var old_area = Morebits.quickForm.getElements(root, "work_area")[0];

	if(root.section) {
		whisperback.prev_section = root.section.value;
	}
	if(root.message) {
		whisperback.prev_message = root.message.value;
	}
	if(root.page) {
		whisperback.prev_page = root.page.value;
	}

	var work_area = new Morebits.quickForm.element({
			type: "field",
			label: "Whisperback information",
			name: "work_area"
		});

	switch( value ) {
		case "mytalk":
			/* falls through */
		default:
			work_area.append({
					type:"input",
					name:"section",
					label:"Linked section (optional)",
					tooltip:"The section heading on your talk page where you left a message. Leave empty for no section to be linked.",
					value: whisperback.prev_section
				});
			break;
		case "usertalk":
			work_area.append({
					type:"input",
					name:"page",
					label:"User",
					tooltip:"The username of the user on whose talk page you left a message.",
					value: whisperback.prev_page
				});
			
			work_area.append({
					type:"input",
					name:"section",
					label:"Linked section (optional)",
					tooltip:"The section heading on the page where you left a message. Leave empty for no section to be linked.",
					value: whisperback.prev_section
				});
			break;
		case "other":
			work_area.append({
					type:"input",
					name:"page",
					label:"Full page name",
					tooltip:"The full page name where you left the message. For example: 'Wikipedia talk:Twinkle'.",
					value: whisperback.prev_page
				});
			
			work_area.append({
					type:"input",
					name:"section",
					label:"Linked section (optional)",
					tooltip:"The section heading on the page where you left a message. Leave empty for no section to be linked.",
					value: whisperback.prev_section
				});
			break;
	}

	work_area.append({ type:"textarea", label:"Additional message (optional):", name:"message", tooltip:"An additional message that you would like to leave below the whisperback template. Your signature will be added to the end of the message if you leave one." });

	work_area = work_area.render();
	root.replaceChild( work_area, old_area );
	if (root.message) {
		root.message.value = whisperback.prev_message;
	}
};

whisperback.callback_evaluate = function( e ) {

	var tbtarget = e.target.getChecked( "tbtarget" )[0];
	var page = null;
	var section = e.target.section.value;
	var fullUserTalkPageName = mw.config.get("wgFormattedNamespaces")[ mw.config.get("wgNamespaceIds").user_talk ] + ":" + Morebits.getPageAssociatedUser();

	if( tbtarget === "usertalk" || tbtarget === "other" ) {
		page = e.target.page.value;
		
		if( tbtarget === "usertalk" ) {
			if( !page ) {
				alert("You must specify the username of the user whose talk page you left a message on.");
				return;
			}
		} else {
			if( !page ) {
				alert("You must specify the full page name when your message is not on a user talk page.");
				return;
			}
		}
	} 

	var message;
	if (e.target.message) {
		message = e.target.message.value;
	}

	Morebits.simpleWindow.setButtonsEnabled( false );
	Morebits.status.init( e.target );

	Morebits.wiki.actionCompleted.redirect = fullUserTalkPageName;
	Morebits.wiki.actionCompleted.notice = "Talkback complete; reloading talk page in a few seconds";

	var talkpage = new Morebits.wiki.page(fullUserTalkPageName, "Adding talkback");
	var tbPageName = (tbtarget === "mytalk") ? mw.config.get("wgUserName") : page;

	var text;
	text = "\n== New messages ==\n{{whisperback|";
	text += tbPageName;

	if( section ) {
		text += "|" + section;
	}

	text += "|ts=11:55, 14 July 2012 (UTC)}}";

	if( message ) {
		text += "\n" + message + "  — <span style="border:dashed #666;border-width:1px 0 0 1px">[[User:This, that and the other|This, that]]</span>, and <span style="border:dashed #666;border-width:0 1px 1px 0">[[User talk:This, that and the other|the other<small> (talk)</small>]]</span> 11:55, 14 July 2012 (UTC)";
	}

	talkpage.setEditSummary("Talkback ([[" + (tbtarget === "other" ? "" : "User talk:") + tbPageName +
		(section ? ("#" + section) : "") + "]])" + Twinkle.getPref("summaryAd"));

	talkpage.setAppendText( text );
	talkpage.setCreateOption("recreate");
	talkpage.setMinorEdit(false);
	talkpage.setFollowRedirect( true );
	talkpage.append();
};