MediaWiki:Gadget-friendlytalkback.js: Difference between revisions

Content added Content deleted
imported>AzaToth
(v2.0-519-g437a507: Fix some issues for deploying multiple files)
 
imported>AzaToth
m (v2.0-545-gbd83dc0: xfd: stop AFD process if page doesn't exist)
Line 13: Line 13:
* Config directives in: FriendlyConfig
* Config directives in: FriendlyConfig
*/
*/
;(function(){


Twinkle.talkback = function() {
Twinkle.talkback = function() {

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

Twinkle.addPortletLink( Twinkle.talkback.callback, "TB", "friendly-talkback", "Easy talkback" );
};

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

var Window = new Morebits.simpleWindow( 600, 350 );
Window.setTitle("Talkback");
Window.setScriptName("Twinkle");
Window.addFooterLink( "About {{talkback}}", "Template:Talkback" );
Window.addFooterLink( "Twinkle help", "WP:TW/DOC#talkback" );

var form = new Morebits.quickForm( callback_evaluate );

form.append({ type: "radio", name: "tbtarget",
list: [
{
label: "Talkback: my talk page",
value: "mytalk",
checked: "true"
},
{
label: "Talkback: other user talk page",
value: "usertalk"
},
{
label: "Talkback: other page",
value: "other"
},
{
label: "Noticeboard notification",
value: "notice"
},
{
label: "\"You've got mail\"",
value: "mail"
}
],
event: 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 );

// Check whether the user has opted out from talkback
// TODO: wgCategories is only set on action=view (bug 45033)
var wgcat = mw.config.get("wgCategories");
if (wgcat.length && wgcat.indexOf("Users who do not wish to receive talkbacks") === -1) {
Twinkle.talkback.optout = false;
} else {
var query = {
action: 'query',
prop: 'extlinks',
titles: mw.config.get('wgPageName'),
elquery: 'userjs.invalid/noTalkback',
ellimit: '1'
};
var wpapi = new Morebits.wiki.api("Fetching talkback opt-out status", query, Twinkle.talkback.callback.optoutStatus);
wpapi.post();
}
};

Twinkle.talkback.optout = null;

Twinkle.talkback.callback.optoutStatus = function(apiobj) {
var xml = apiobj.getXML();
var $el = $(xml).find('el');

if ($el.length) {
Twinkle.talkback.optout = mw.config.get("wgUserName") + " prefers not to receive talkbacks";
var url = $el.text();
if (url.indexOf("reason=") > -1) {
Twinkle.talkback.optout += ": " + decodeURIComponent(url.substring(url.indexOf("reason=") + 7)) + ".";
} else {
Twinkle.talkback.optout += ".";
}
}
} else {
Twinkle.addPortletLink( callback, "TB", "friendly-talkback", "Easy talkback" );
Twinkle.talkback.optout = false;
};
}

var $status = $("#twinkle-talkback-optout-message");
var callback = function( ) {
if ($status.length) {
if( Morebits.getPageAssociatedUser() === mw.config.get("wgUserName") && !confirm("Is it really so bad that you're talking back to yourself?") ){
$status.append(Twinkle.talkback.optout);
return;
}
}
};

var Window = new Morebits.simpleWindow( 600, 350 );
var prev_page = "";
Window.setTitle("Talkback");
var prev_section = "";
Window.setScriptName("Twinkle");
var prev_message = "";
Window.addFooterLink( "About {{talkback}}", "Template:Talkback" );

Window.addFooterLink( "Twinkle help", "WP:TW/DOC#talkback" );
var callback_change_target = function( e ) {
var value = e.target.values;
var form = new Morebits.quickForm( callback_evaluate );
var root = e.target.form;
var old_area = Morebits.quickForm.getElements(root, "work_area")[0];
form.append({ type: "radio", name: "tbtarget",

list: [
if(root.section) {
{
prev_section = root.section.value;
label: "Talkback: my talk page",
}
value: "mytalk",
if(root.message) {
checked: "true"
prev_message = root.message.value;
},
}
{
if(root.page) {
label: "Talkback: other user talk page",
prev_page = root.page.value;
value: "usertalk"
}
},

{
var work_area = new Morebits.quickForm.element({
label: "Talkback: other page",
value: "other"
type: "field",
label: "Talkback information",
},
name: "work_area"
{
});
label: "Noticeboard notification",

value: "notice"
switch( value ) {
},
case "mytalk":
{
/* falls through */
label: "\"You've got mail\"",
default:
value: "mail"
work_area.append({
}
],
type: "div",
label: "",
event: callback_change_target
style: "color: red",
id: "twinkle-talkback-optout-message"
});
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: prev_section
});
});
break;
case "usertalk":
form.append({
work_area.append({
type: "field",
label: "Work area",
type: "div",
name: "work_area"
label: "",
style: "color: red",
id: "twinkle-talkback-optout-message"
});
});
work_area.append({
form.append({ type: "submit" });
type:"input",
name:"page",
label:"User",
var result = form.render();
tooltip:"The username of the user on whose talk page you left a message.",
Window.setContent( result );
value: prev_page
Window.display();
});
// We must init the
work_area.append({
var evt = document.createEvent("Event");
type:"input",
evt.initEvent( "change", true, true );
name:"section",
result.tbtarget[0].dispatchEvent( evt );
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: prev_section
var prev_page = "";
});
var prev_section = "";
break;
var prev_message = "";
case "notice":
var noticeboard = work_area.append({
var callback_change_target = function( e ) {
type: "select",
var value = e.target.values;
name: "noticeboard",
var root = e.target.form;
label: "Noticeboard:"
var old_area = Morebits.quickForm.getElements(root, "work_area")[0];
});
noticeboard.append({
if(root.section) {
type: "option",
prev_section = root.section.value;
label: "WP:AN (Administrators' noticeboard)",
}
value: "an"
if(root.message) {
});
prev_message = root.message.value;
noticeboard.append({
}
type: "option",
if(root.page) {
label: "WP:AN3 (Administrators' noticeboard/Edit warring)",
prev_page = root.page.value;
selected: true,
value: "an3"
});
noticeboard.append({
type: "option",
label: "WP:ANI (Administrators' noticeboard/Incidents)",
selected: true,
value: "ani"
});
noticeboard.append({
type: "option",
label: "WP:COIN (Conflict of interest noticeboard)",
value: "coin"
});
noticeboard.append({
type: "option",
label: "WP:DRN (Dispute resolution noticeboard)",
value: "drn"
});
noticeboard.append({
type: "option",
label: "WP:OTRS/N (OTRS noticeboard)",
value: "otrs"
});
noticeboard.append({
type: "option",
label: "WP:HD (Help desk)",
value: "hd"
});
noticeboard.append({
type: "option",
label: "WP:THQ (Teahouse question forum)",
value: "th"
});
work_area.append({
type:"input",
name:"section",
label:"Linked thread",
tooltip:"The heading of the relevant thread on the noticeboard page.",
value: prev_section
});
break;
case "other":
work_area.append({
type: "div",
label: "",
style: "color: red",
id: "twinkle-talkback-optout-message"
});
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: 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: prev_section
});
break;
case "mail":
work_area.append({
type:"input",
name:"section",
label:"Subject of email (optional)",
tooltip:"The subject line of the email you sent."
});
break;
}

if (value !== "notice") {
work_area.append({ type:"textarea", label:"Additional message (optional):", name:"message", tooltip:"An additional message that you would like to leave below the talkback 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 = prev_message;
}

if (Twinkle.talkback.optout) {
$("#twinkle-talkback-optout-message").append(Twinkle.talkback.optout);
}
};

var 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;
}
}
}
} else if (tbtarget === "notice") {
page = e.target.noticeboard.value;
}


var message;
var work_area = new Morebits.quickForm.element({
if (e.target.message) {
type: "field",
message = e.target.message.value;
label: "Talkback information",
}
name: "work_area"

});
Morebits.simpleWindow.setButtonsEnabled( false );
Morebits.status.init( e.target );
switch( value ) {

case "mytalk":
Morebits.wiki.actionCompleted.redirect = fullUserTalkPageName;
/* falls through */
Morebits.wiki.actionCompleted.notice = "Talkback complete; reloading talk page in a few seconds";
default:

work_area.append({
var talkpage = new Morebits.wiki.page(fullUserTalkPageName, "Adding talkback");
type:"input",
var tbPageName = (tbtarget === "mytalk") ? mw.config.get("wgUserName") : page;
name:"section",

label:"Linked section (optional)",
var text;
tooltip:"The section heading on your talk page where you left a message. Leave empty for no section to be linked.",
if ( tbtarget === "notice" ) {
value: prev_section
switch (page) {
});
case "an":
text = "\n\n== " + Twinkle.getFriendlyPref("adminNoticeHeading") + " ==\n";
text += "{{subst:ANI-notice|thread=" + section + "|noticeboard=Wikipedia:Administrators' noticeboard}} ~~~~";
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Administrators' noticeboard]]" + Twinkle.getPref("summaryAd") );
break;
break;
case "usertalk":
case "an3":
text = "\n\n{{subst:An3-notice|" + section + "}} ~~~~";
work_area.append({
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Administrators' noticeboard/Edit warring]]" + Twinkle.getPref("summaryAd") );
type:"input",
name:"page",
label:"User",
tooltip:"The username of the user on whose talk page you left a message.",
value: 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: prev_section
});
break;
break;
case "notice":
case "ani":
text = "\n\n== " + Twinkle.getFriendlyPref("adminNoticeHeading") + " ==\n";
var noticeboard = work_area.append({
text += "{{subst:ANI-notice|thread=" + section + "|noticeboard=Wikipedia:Administrators' noticeboard/Incidents}} ~~~~";
type: "select",
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Administrators' noticeboard/Incidents]]" + Twinkle.getPref("summaryAd") );
name: "noticeboard",
label: "Noticeboard:"
});
noticeboard.append({
type: "option",
label: "WP:AN (Administrators' noticeboard)",
value: "an"
});
noticeboard.append({
type: "option",
label: "WP:AN3 (Administrators' noticeboard/Edit warring)",
selected: true,
value: "an3"
});
noticeboard.append({
type: "option",
label: "WP:ANI (Administrators' noticeboard/Incidents)",
selected: true,
value: "ani"
});
noticeboard.append({
type: "option",
label: "WP:COIN (Conflict of interest noticeboard)",
value: "coin"
});
noticeboard.append({
type: "option",
label: "WP:DRN (Dispute resolution noticeboard)",
value: "drn"
});
noticeboard.append({
type: "option",
label: "WP:OTRS/N (OTRS noticeboard)",
value: "otrs"
});
noticeboard.append({
type: "option",
label: "WP:HD (Help desk)",
value: "hd"
});
noticeboard.append({
type: "option",
label: "WP:THQ (Teahouse question forum)",
value: "th"
});
work_area.append({
type:"input",
name:"section",
label:"Linked thread",
tooltip:"The heading of the relevant thread on the noticeboard page.",
value: prev_section
});
break;
break;
case "other":
case "coin":
text = "\n\n{{subst:Coin-notice|thread=" + section + "}} ~~~~";
work_area.append({
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Conflict of interest noticeboard]]" + Twinkle.getPref("summaryAd") );
type:"input",
name:"page",
label:"Full page name",
tooltip:"The full page name where you left the message. For example: 'Wikipedia talk:Twinkle'.",
value: 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: prev_section
});
break;
break;
case "mail":
case "drn":
text = "\n\n{{subst:DRN-notice|thread=" + section + "}} ~~~~";
work_area.append({
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Dispute resolution noticeboard]]" + Twinkle.getPref("summaryAd") );
type:"input",
name:"section",
label:"Subject of email (optional)",
tooltip:"The subject line of the email you sent."
});
break;
break;
case "hd":
text = "\n\n== Your question at the Help desk ==\n";
text += "{{helpdeskreply|1=" + section + "|ts=~~~~~}}";
talkpage.setEditSummary( "You have replies at the [[Wikipedia:Help desk|Wikipedia help desk]]" + Twinkle.getPref("summaryAd") );
break;
case "otrs":
text = "\n\n{{OTRSreply|1=" + section + "|2=~~~~}}";
talkpage.setEditSummary( "You have replies at the [[Wikipedia:OTRS noticeboard|OTRS noticeboard]]" + Twinkle.getPref("summaryAd") );
break;
case "th":
text = "\n\n== Teahouse talkback: you've got messages! ==\n{{WP:Teahouse/Teahouse talkback|WP:Teahouse/Questions|" + section + "|ts=~~~~}}";
talkpage.setEditSummary( "You have replies at the [[Wikipedia:Teahouse/Questions|Teahouse question board]]" + Twinkle.getPref("summaryAd") );
break;
default:
throw "Twinkle.talkback, function callback_evaluate: default case reached";
}
}

if (value !== "notice") {
} else if ( tbtarget === "mail" ) {
text = "\n\n==" + Twinkle.getFriendlyPref("mailHeading") + "==\n{{you've got mail|subject=";
work_area.append({ type:"textarea", label:"Additional message (optional):", name:"message", tooltip:"An additional message that you would like to leave below the talkback template. Your signature will be added to the end of the message if you leave one." });
text += section + "|ts=~~~~~}}";

if( message ) {
text += "\n" + message + " ~~~~";
} else if( Twinkle.getFriendlyPref("insertTalkbackSignature") ) {
text += "\n~~~~";
}
}
work_area = work_area.render();
root.replaceChild( work_area, old_area );
if (root.message) {
root.message.value = prev_message;
}
};
var 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;
}
}
} else if (tbtarget === "notice") {
page = e.target.noticeboard.value;
}
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;
if ( tbtarget === "notice" ) {
switch (page) {
case "an":
text = "\n\n== " + Twinkle.getFriendlyPref("adminNoticeHeading") + " ==\n";
text += "{{subst:ANI-notice|thread=" + section + "|noticeboard=Wikipedia:Administrators' noticeboard}} ~~~~";
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Administrators' noticeboard]]" + Twinkle.getPref("summaryAd") );
break;
case "an3":
text = "\n\n{{subst:An3-notice|" + section + "}} ~~~~";
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Administrators' noticeboard/Edit warring]]" + Twinkle.getPref("summaryAd") );
break;
case "ani":
text = "\n\n== " + Twinkle.getFriendlyPref("adminNoticeHeading") + " ==\n";
text += "{{subst:ANI-notice|thread=" + section + "|noticeboard=Wikipedia:Administrators' noticeboard/Incidents}} ~~~~";
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Administrators' noticeboard/Incidents]]" + Twinkle.getPref("summaryAd") );
break;
case "coin":
text = "\n\n{{subst:Coin-notice|thread=" + section + "}} ~~~~";
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Conflict of interest noticeboard]]" + Twinkle.getPref("summaryAd") );
break;
case "drn":
text = "\n\n{{subst:DRN-notice|thread=" + section + "}} ~~~~";
talkpage.setEditSummary( "Notice of discussion at [[Wikipedia:Dispute resolution noticeboard]]" + Twinkle.getPref("summaryAd") );
break;
case "hd":
text = "\n\n== Your question at the Help desk ==\n";
text += "{{helpdeskreply|1=" + section + "|ts=~~~~~}}";
talkpage.setEditSummary( "You have replies at the [[Wikipedia:Help desk|Wikipedia help desk]]" + Twinkle.getPref("summaryAd") );
break;
case "otrs":
text = "\n\n{{OTRSreply|1=" + section + "|2=~~~~}}";
talkpage.setEditSummary( "You have replies at the [[Wikipedia:OTRS noticeboard|OTRS noticeboard]]" + Twinkle.getPref("summaryAd") );
break;
case "th":
text = "\n\n== Teahouse talkback: you've got messages! ==\n{{WP:Teahouse/Teahouse talkback|WP:Teahouse/Questions|" + section + "|ts=~~~~}}";
talkpage.setEditSummary( "You have replies at the [[Wikipedia:Teahouse/Questions|Teahouse question board]]" + Twinkle.getPref("summaryAd") );
break;
default:
throw "Twinkle.talkback, function callback_evaluate: default case reached";
}


talkpage.setEditSummary("Notification: You've got mail" + Twinkle.getPref("summaryAd"));
} else if ( tbtarget === "mail" ) {
text = "\n\n==" + Twinkle.getFriendlyPref("mailHeading") + "==\n{{you've got mail|subject=";
text += section + "|ts=~~~~~}}";


} else {
if( message ) {
//clean talkback heading: strip section header markers, were erroneously suggested in the documentation
text += "\n" + message + " ~~~~";
} else if( Twinkle.getFriendlyPref("insertTalkbackSignature") ) {
text = "\n\n==" + Twinkle.getFriendlyPref("talkbackHeading").replace( /^\s*=+\s*(.*?)\s*=+$\s*/, "$1" ) + "==\n{{talkback|";
text += "\n~~~~";
text += tbPageName;
}


if( section ) {
talkpage.setEditSummary("Notification: You've got mail" + Twinkle.getPref("summaryAd"));
text += "|" + section;
}


text += "|ts=~~~~~}}";
} else {
//clean talkback heading: strip section header markers, were erroneously suggested in the documentation
text = "\n\n==" + Twinkle.getFriendlyPref("talkbackHeading").replace( /^\s*=+\s*(.*?)\s*=+$\s*/, "$1" ) + "==\n{{talkback|";
text += tbPageName;


if( section ) {
if( message ) {
text += "|" + section;
text += "\n" + message + " ~~~~";
} else if( Twinkle.getFriendlyPref("insertTalkbackSignature") ) {
}
text += "\n~~~~";
text += "|ts=~~~~~}}";
if( message ) {
text += "\n" + message + " ~~~~";
} else if( Twinkle.getFriendlyPref("insertTalkbackSignature") ) {
text += "\n~~~~";
}
talkpage.setEditSummary("Talkback ([[" + (tbtarget === "other" ? "" : "User talk:") + tbPageName +
(section ? ("#" + section) : "") + "]])" + Twinkle.getPref("summaryAd"));
}
}
talkpage.setAppendText( text );
talkpage.setCreateOption("recreate");
talkpage.setMinorEdit(Twinkle.getFriendlyPref("markTalkbackAsMinor"));
talkpage.setFollowRedirect( true );
talkpage.append();
};


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

talkpage.setAppendText( text );
talkpage.setCreateOption("recreate");
talkpage.setMinorEdit(Twinkle.getFriendlyPref("markTalkbackAsMinor"));
talkpage.setFollowRedirect( true );
talkpage.append();
};

})(jQuery);
})(jQuery);