/
friendlytalkback.js
250 lines (219 loc) · 7.41 KB
/
friendlytalkback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
****************************************
*** friendlytalkback.js: Talkback module
****************************************
* Mode of invocation: Tab ("TB")
* Active on: Existing user talk pages
* Config directives in: FriendlyConfig
*/
Twinkle.talkback = function friendlytalkback() {
if( mw.config.get('wgNamespaceNumber') === 3 ) {
var username = mw.config.get('wgTitle').split( '/' )[0].replace( /\"/, "\\\""); // only first part before any slashes
$(twAddPortletLink("#", "TB", "friendly-talkback", "Easy talkback", "")).click(function() { Twinkle.talkback.callback(username); });
}
};
Twinkle.talkback.callback = function friendlytalkbackCallback( uid ) {
if( uid === mw.config.get('wgUserName') ){
alert( 'Is it really so bad that you\'re talking back to yourself?' );
return;
}
var Window = new 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 QuickForm( Twinkle.talkback.callback.evaluate );
form.append( { type: 'radio', name: 'tbtarget',
list: [ {
label: 'My talk page',
value: 'mytalk',
checked: 'true' },
{
label: 'Other user talk page',
value: 'usertalk' },
{
label: "Administrators' noticeboard",
value: 'an' },
{
label: 'Other page',
value: 'other' } ],
event: Twinkle.talkback.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 );
};
Twinkle.talkback.prev_page = '';
Twinkle.talkback.prev_section = '';
Twinkle.talkback.prev_message = '';
Twinkle.talkback.callback.change_target = function friendlytagCallbackChangeTarget(e) {
var value = e.target.values;
var root = e.target.form;
var old_area;
if(root.section) {
Twinkle.talkback.prev_section = root.section.value;
}
if(root.message) {
Twinkle.talkback.prev_message = root.message.value;
}
if(root.page) {
Twinkle.talkback.prev_page = root.page.value;
}
for( var i = 0; i < root.childNodes.length; ++i ) {
var node = root.childNodes[i];
if (node instanceof Element && node.getAttribute( 'name' ) === 'work_area' ) {
old_area = node;
break;
}
}
var work_area = new QuickForm.element( {
type: 'field',
label: 'Talkback 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: Twinkle.talkback.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: Twinkle.talkback.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: Twinkle.talkback.prev_section
} );
break;
case 'an':
var noticeboard = work_area.append( {
type: 'select',
name: 'noticeboard',
label: 'Noticeboard:'
} );
noticeboard.append( {
type: 'option',
label: "WP:AN (Administrators' noticeboard)",
value: "Wikipedia:Administrators' noticeboard"
} );
noticeboard.append( {
type: 'option',
label: 'WP:ANI (Administrators\' noticeboard/Incidents)',
selected: true,
value: "Wikipedia:Administrators' noticeboard/Incidents"
} );
work_area.append( {
type:'input',
name:'section',
label:'Linked thread',
tooltip:'The heading of the relevant AN or ANI thread.',
value: Twinkle.talkback.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: Twinkle.talkback.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: Twinkle.talkback.prev_section
} );
break;
}
if (value !== "an") {
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 = Twinkle.talkback.prev_message;
}
};
Twinkle.talkback.callback.evaluate = function friendlytalkbackCallbackEvaluate(e) {
var tbtarget = e.target.getChecked( 'tbtarget' )[0];
var page = null;
var section = e.target.section.value;
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 === "an") {
page = e.target.noticeboard.value;
}
var message;
if (e.target.message) {
message = e.target.message.value;
}
SimpleWindow.setButtonsEnabled( false );
Status.init( e.target );
Wikipedia.actionCompleted.redirect = mw.config.get('wgPageName');
Wikipedia.actionCompleted.notice = "Talkback complete; reloading talk page in a few seconds";
var talkpage = new Wikipedia.page(mw.config.get('wgPageName'), "Adding talkback");
var tbPageName = (tbtarget === 'mytalk') ? mw.config.get('wgUserName') : page;
var text;
if ( tbtarget === "an" ) {
text = "\n== " + Twinkle.getFriendlyPref('adminNoticeHeading') + " ==\n{{subst:ANI-notice|thread=";
text += section + "|noticeboard=" + tbPageName + "}} ~~~~";
talkpage.setEditSummary("Notice of AN/ANI discussion" + Twinkle.getPref('summaryAd'));
} else {
//clean talkback heading: strip section header markers, were erroneously suggested in the documentation
text = '\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 + ' ~~~~';
} 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();
};