MediaWiki:Gadget-friendlytalkback.js: Difference between revisions

From TestWiki
Content added Content deleted
imported>Amalthea
(v2.0-689-g073eb2e: talkback: Add {{please see}}; protect: Make tag expiry work for PC protection. Update protection templates; warn: Use {{{2}}} parameter for extra message; morebits: use wgRelevantUserName to get user)
imported>Amorymeltzer
(Repo at d404881: Greatly expand style guidelines, enforce linting rules (#657))
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
//<nowiki>
// <nowiki>




(function($){
(function($) {




Line 16: Line 16:
Twinkle.talkback = function() {
Twinkle.talkback = function() {


if ( !mw.config.get('wgRelevantUserName') ) {
if (!mw.config.get('wgRelevantUserName')) {
return;
return;
}
}


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


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


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


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


form.append({ type: "radio", name: "tbtarget",
form.append({ type: 'radio', name: 'tbtarget',
list: [
list: [
{
{
label: "Talkback: my talk page",
label: 'Talkback: my talk page',
value: "mytalk",
value: 'mytalk',
checked: "true"
checked: 'true'
},
},
{
{
label: "Talkback: other user talk page",
label: 'Talkback: other user talk page',
value: "usertalk"
value: 'usertalk'
},
},
{
{
label: "Talkback: other page",
label: 'Talkback: other page',
value: "other"
value: 'other'
},
},
{
{
label: "\"Please see\"",
label: '"Please see"',
value: "see"
value: 'see'
},
},
{
{
label: "Noticeboard notification",
label: 'Noticeboard notification',
value: "notice"
value: 'notice'
},
},
{
{
label: "\"You've got mail\"",
label: "\"You've got mail\"",
value: "mail"
value: 'mail'
}
}
],
],
event: callback_change_target
event: callback_change_target
});
});


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


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


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


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


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


Line 109: Line 103:


if ($el.length) {
if ($el.length) {
Twinkle.talkback.optout = mw.config.get('wgRelevantUserName') + " prefers not to receive talkbacks";
Twinkle.talkback.optout = mw.config.get('wgRelevantUserName') + ' prefers not to receive talkbacks';
var url = $el.text();
var url = $el.text();
if (url.indexOf("reason=") > -1) {
if (url.indexOf('reason=') > -1) {
Twinkle.talkback.optout += ": " + decodeURIComponent(url.substring(url.indexOf("reason=") + 7)) + ".";
Twinkle.talkback.optout += ': ' + decodeURIComponent(url.substring(url.indexOf('reason=') + 7)) + '.';
} else {
} else {
Twinkle.talkback.optout += ".";
Twinkle.talkback.optout += '.';
}
}
} else {
} else {
Line 120: Line 114:
}
}


var $status = $("#twinkle-talkback-optout-message");
var $status = $('#twinkle-talkback-optout-message');
if ($status.length) {
if ($status.length) {
$status.append(Twinkle.talkback.optout);
$status.append(Twinkle.talkback.optout);
Line 126: Line 120:
};
};


var prev_page = "";
var prev_page = '';
var prev_section = "";
var prev_section = '';
var prev_message = "";
var prev_message = '';


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


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


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


switch( value ) {
switch (value) {
case "mytalk":
case 'mytalk':
/* falls through */
/* falls through */
default:
default:
work_area.append({
work_area.append({
type: "div",
type: 'div',
label: "",
label: '',
style: "color: red",
style: 'color: red',
id: "twinkle-talkback-optout-message"
id: 'twinkle-talkback-optout-message'
});
});
work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"section",
name: 'section',
label:"Linked section (optional)",
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.",
tooltip: 'The section heading on your talk page where you left a message. Leave empty for no section to be linked.',
value: prev_section
value: prev_section
});
});
break;
break;
case "usertalk":
case 'usertalk':
work_area.append({
work_area.append({
type: "div",
type: 'div',
label: "",
label: '',
style: "color: red",
style: 'color: red',
id: "twinkle-talkback-optout-message"
id: 'twinkle-talkback-optout-message'
});
});
work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"page",
name: 'page',
label:"User",
label: 'User',
tooltip:"The username of the user on whose talk page you left a message.",
tooltip: 'The username of the user on whose talk page you left a message.',
value: prev_page
value: prev_page
});
});

work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"section",
name: 'section',
label:"Linked section (optional)",
label: 'Linked section (optional)',
tooltip:"The section heading on the page where you left a message. Leave empty for no section to be linked.",
tooltip: 'The section heading on the page where you left a message. Leave empty for no section to be linked.',
value: prev_section
value: prev_section
});
});
break;
break;
case "notice":
case 'notice':
var noticeboard = work_area.append({
var noticeboard = work_area.append({
type: "select",
type: 'select',
name: "noticeboard",
name: 'noticeboard',
label: "Noticeboard:",
label: 'Noticeboard:',
event: function(e) {
event: function(e) {
if (e.target.value === "afchd") {
if (e.target.value === 'afchd') {
Morebits.quickForm.overrideElementLabel(e.target.form.section, "Title of draft (excluding the prefix): ");
Morebits.quickForm.overrideElementLabel(e.target.form.section, 'Title of draft (excluding the prefix): ');
Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, false);
Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, false);
} else {
} else {
Morebits.quickForm.resetElementLabel(e.target.form.section);
Morebits.quickForm.resetElementLabel(e.target.form.section);
Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, true);
Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, true);
}
}
}
});
}
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:AN (Administrators' noticeboard)",
label: "WP:AN (Administrators' noticeboard)",
value: "an"
value: 'an'
});
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:AN3 (Administrators' noticeboard/Edit warring)",
label: "WP:AN3 (Administrators' noticeboard/Edit warring)",
value: "an3"
value: 'an3'
});
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:ANI (Administrators' noticeboard/Incidents)",
label: "WP:ANI (Administrators' noticeboard/Incidents)",
selected: true,
selected: true,
value: "ani"
value: 'ani'
});
});
// let's keep AN and its cousins at the top
// let's keep AN and its cousins at the top
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:AFCHD (Articles for creation/Help desk)",
label: 'WP:AFCHD (Articles for creation/Help desk)',
value: "afchd"
value: 'afchd'
});
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:COIN (Conflict of interest noticeboard)",
label: 'WP:COIN (Conflict of interest noticeboard)',
value: "coin"
value: 'coin'
});
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:DRN (Dispute resolution noticeboard)",
label: 'WP:DRN (Dispute resolution noticeboard)',
value: "drn"
value: 'drn'
});
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:HD (Help desk)",
label: 'WP:HD (Help desk)',
value: "hd"
value: 'hd'
});
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:OTRS/N (OTRS noticeboard)",
label: 'WP:OTRS/N (OTRS noticeboard)',
value: "otrs"
value: 'otrs'
});
});
noticeboard.append({
noticeboard.append({
type: "option",
type: 'option',
label: "WP:THQ (Teahouse question forum)",
label: 'WP:THQ (Teahouse question forum)',
value: "th"
value: 'th'
});
});
work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"section",
name: 'section',
label:"Linked thread",
label: 'Linked thread',
tooltip:"The heading of the relevant thread on the noticeboard page.",
tooltip: 'The heading of the relevant thread on the noticeboard page.',
value: prev_section
value: prev_section
});
});
break;
break;
case "other":
case 'other':
work_area.append({
work_area.append({
type: "div",
type: 'div',
label: "",
label: '',
style: "color: red",
style: 'color: red',
id: "twinkle-talkback-optout-message"
id: 'twinkle-talkback-optout-message'
});
});
work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"page",
name: 'page',
label:"Full page name",
label: 'Full page name',
tooltip:"The full page name where you left the message. For example: 'Wikipedia talk:Twinkle'.",
tooltip: "The full page name where you left the message. For example: 'Wikipedia talk:Twinkle'.",
value: prev_page
value: prev_page
});
});

work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"section",
name: 'section',
label:"Linked section (optional)",
label: 'Linked section (optional)',
tooltip:"The section heading on the page where you left a message. Leave empty for no section to be linked.",
tooltip: 'The section heading on the page where you left a message. Leave empty for no section to be linked.',
value: prev_section
value: prev_section
});
});
break;
break;
case "mail":
case 'mail':
work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"section",
name: 'section',
label:"Subject of email (optional)",
label: 'Subject of email (optional)',
tooltip:"The subject line of the email you sent."
tooltip: 'The subject line of the email you sent.'
});
});
break;
break;
case "see":
case 'see':
work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"page",
name: 'page',
label:"Full page name",
label: 'Full page name',
tooltip:"The full page name of where the discussion is being held. For example: 'Wikipedia talk:Twinkle'.",
tooltip: "The full page name of where the discussion is being held. For example: 'Wikipedia talk:Twinkle'.",
value: prev_page
value: prev_page
});
});
work_area.append({
work_area.append({
type:"input",
type: 'input',
name:"section",
name: 'section',
label:"Linked section (optional)",
label: 'Linked section (optional)',
tooltip:"The section heading where the discussion is being held. For example: 'Merge proposal'.",
tooltip: "The section heading where the discussion is being held. For example: 'Merge proposal'.",
value: prev_section
value: prev_section
});
});
break;
break;
}
}


if (value !== "notice") {
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.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();
work_area = work_area.render();
root.replaceChild( work_area, old_area );
root.replaceChild(work_area, old_area);
if (root.message) {
if (root.message) {
root.message.value = prev_message;
root.message.value = prev_message;
Line 322: Line 316:


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


var callback_evaluate = function( e ) {
var callback_evaluate = function(e) {


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


if( tbtarget === "usertalk" || tbtarget === "other" || tbtarget === "see" ) {
if (tbtarget === 'usertalk' || tbtarget === 'other' || tbtarget === 'see') {
page = e.target.page.value;
page = e.target.page.value;

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


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


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


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


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


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


if( message ) {
if (message) {
text += "\n" + message.trim() + " ~~~~";
text += '\n' + message.trim() + ' ~~~~';
} else if( Twinkle.getFriendlyPref("insertTalkbackSignature") ) {
} else if (Twinkle.getFriendlyPref('insertTalkbackSignature')) {
text += "\n~~~~";
text += '\n~~~~';
}
}


talkpage.setEditSummary("Notification: You've got mail" + Twinkle.getPref("summaryAd"));
talkpage.setEditSummary("Notification: You've got mail" + Twinkle.getPref('summaryAd'));

} else if ( tbtarget === "see" ) {
} else if (tbtarget === 'see') {
text = "\n\n{{subst:Please see|location=" + tbPageName;
text = '\n\n{{subst:Please see|location=' + tbPageName;
if (section) {
if (section) {
text += "#" + section;
text += '#' + section;
}
}
text += "|more=" + message.trim() + "}}";
text += '|more=' + message.trim() + '}}';
talkpage.setEditSummary("Please check the discussion at [[" + tbPageName + "#" + section + "]]" + Twinkle.getPref("summaryAd"));
talkpage.setEditSummary('Please check the discussion at [[:' + tbPageName +
(section ? '#' + section : '') + ']]' + Twinkle.getPref('summaryAd'));

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


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


text += "|ts=~~~~~}}";
text += '|ts=~~~~~}}';


if( message ) {
if (message) {
text += "\n" + message.trim() + " ~~~~";
text += '\n' + message.trim() + ' ~~~~';
} else if( Twinkle.getFriendlyPref("insertTalkbackSignature") ) {
} else if (Twinkle.getFriendlyPref('insertTalkbackSignature')) {
text += "\n~~~~";
text += '\n~~~~';
}
}


var editSummary = 'Talkback ([[:';
talkpage.setEditSummary("Talkback ([[" + (tbtarget === "other" ? "" : "User talk:") + tbPageName +
if (tbtarget !== 'other' && !/^\s*user talk:/i.test(tbPageName)) {
(section ? ("#" + section) : "") + "]])" + Twinkle.getPref("summaryAd"));
editSummary += 'User talk:';
}
editSummary += tbPageName + (section ? '#' + section : '') + ']])';
talkpage.setEditSummary(editSummary + Twinkle.getPref('summaryAd'));
}
}


talkpage.setAppendText( text );
talkpage.setAppendText(text);
talkpage.setCreateOption("recreate");
talkpage.setCreateOption('recreate');
talkpage.setMinorEdit(Twinkle.getFriendlyPref("markTalkbackAsMinor"));
talkpage.setMinorEdit(Twinkle.getFriendlyPref('markTalkbackAsMinor'));
talkpage.setFollowRedirect( true );
talkpage.setFollowRedirect(true);
talkpage.append();
talkpage.append();
};
};
Line 462: Line 461:




//</nowiki>
// </nowiki>

Latest revision as of 19:48, 12 June 2019

// <nowiki>


(function($) {


/*
 ****************************************
 *** friendlytalkback.js: Talkback module
 ****************************************
 * Mode of invocation:     Tab ("TB")
 * Active on:              Existing user talk pages
 * Config directives in:   FriendlyConfig
 */

Twinkle.talkback = function() {

	if (!mw.config.get('wgRelevantUserName')) {
		return;
	}

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

Twinkle.talkback.callback = function() {
	if (mw.config.get('wgRelevantUserName') === 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: '"Please see"',
				value: 'see'
			},
			{
				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
	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('wgRelevantUserName') + ' 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.talkback.optout = false;
	}

	var $status = $('#twinkle-talkback-optout-message');
	if ($status.length) {
		$status.append(Twinkle.talkback.optout);
	}
};

var prev_page = '';
var prev_section = '';
var prev_message = '';

var 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) {
		prev_section = root.section.value;
	}
	if (root.message) {
		prev_message = root.message.value;
	}
	if (root.page) {
		prev_page = root.page.value;
	}

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

	switch (value) {
		case 'mytalk':
			/* falls through */
		default:
			work_area.append({
				type: 'div',
				label: '',
				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':
			work_area.append({
				type: 'div',
				label: '',
				style: 'color: red',
				id: 'twinkle-talkback-optout-message'
			});
			work_area.append({
				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;
		case 'notice':
			var noticeboard = work_area.append({
				type: 'select',
				name: 'noticeboard',
				label: 'Noticeboard:',
				event: function(e) {
					if (e.target.value === 'afchd') {
						Morebits.quickForm.overrideElementLabel(e.target.form.section, 'Title of draft (excluding the prefix): ');
						Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, false);
					} else {
						Morebits.quickForm.resetElementLabel(e.target.form.section);
						Morebits.quickForm.setElementTooltipVisibility(e.target.form.section, true);
					}
				}
			});
			noticeboard.append({
				type: 'option',
				label: "WP:AN (Administrators' noticeboard)",
				value: 'an'
			});
			noticeboard.append({
				type: 'option',
				label: "WP:AN3 (Administrators' noticeboard/Edit warring)",
				value: 'an3'
			});
			noticeboard.append({
				type: 'option',
				label: "WP:ANI (Administrators' noticeboard/Incidents)",
				selected: true,
				value: 'ani'
			});
			// let's keep AN and its cousins at the top
			noticeboard.append({
				type: 'option',
				label: 'WP:AFCHD (Articles for creation/Help desk)',
				value: 'afchd'
			});
			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:HD (Help desk)',
				value: 'hd'
			});
			noticeboard.append({
				type: 'option',
				label: 'WP:OTRS/N (OTRS noticeboard)',
				value: 'otrs'
			});
			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;
		case 'see':
			work_area.append({
				type: 'input',
				name: 'page',
				label: 'Full page name',
				tooltip: "The full page name of where the discussion is being held. For example: 'Wikipedia talk:Twinkle'.",
				value: prev_page
			});
			work_area.append({
				type: 'input',
				name: 'section',
				label: 'Linked section (optional)',
				tooltip: "The section heading where the discussion is being held. For example: 'Merge proposal'.",
				value: prev_section
			});
			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] + ':' + mw.config.get('wgRelevantUserName');

	if (tbtarget === 'usertalk' || tbtarget === 'other' || tbtarget === 'see') {
		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 'afchd':
				text = '\n\n{{subst:AFCHD/u|' + section + '}} ~~~~';
				talkpage.setEditSummary('You have replies at the [[Wikipedia:AFCHD|Articles for Creation Help Desk]]' + Twinkle.getPref('summaryAd'));
				break;
			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';
		}

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

		if (message) {
			text += '\n' + message.trim() + '  ~~~~';
		} else if (Twinkle.getFriendlyPref('insertTalkbackSignature')) {
			text += '\n~~~~';
		}

		talkpage.setEditSummary("Notification: You've got mail" + Twinkle.getPref('summaryAd'));

	} else if (tbtarget === 'see') {
		text = '\n\n{{subst:Please see|location=' + tbPageName;
		if (section) {
			text += '#' + section;
		}
		text += '|more=' + message.trim() + '}}';
		talkpage.setEditSummary('Please check the discussion at [[:' + tbPageName +
			(section ? '#' + section : '') + ']]' + Twinkle.getPref('summaryAd'));

	} else {  // tbtarget one of mytalk, usertalk, other
		// clean talkback heading: strip section header markers that were erroneously suggested in the documentation
		text = '\n\n==' + Twinkle.getFriendlyPref('talkbackHeading').replace(/^\s*=+\s*(.*?)\s*=+$\s*/, '$1') + '==\n{{talkback|';
		text += tbPageName;

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

		text += '|ts=~~~~~}}';

		if (message) {
			text += '\n' + message.trim() + ' ~~~~';
		} else if (Twinkle.getFriendlyPref('insertTalkbackSignature')) {
			text += '\n~~~~';
		}

		var editSummary = 'Talkback ([[:';
		if (tbtarget !== 'other' && !/^\s*user talk:/i.test(tbPageName)) {
			editSummary += 'User talk:';
		}
		editSummary += tbPageName + (section ? '#' + section : '') + ']])';
		talkpage.setEditSummary(editSummary + Twinkle.getPref('summaryAd'));
	}

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

})(jQuery);


// </nowiki>