Merge "Revert "Introduce Special:RedirectExternal""
[lhc/web/wiklou.git] / resources / src / mediawiki.feedback / feedback.js
1 /*!
2 * mediawiki.feedback
3 *
4 * @author Ryan Kaldari, 2010
5 * @author Neil Kandalgaonkar, 2010-11
6 * @author Moriel Schottlender, 2015
7 * @since 1.19
8 */
9 ( function () {
10 /**
11 * This is a way of getting simple feedback from users. It's useful
12 * for testing new features -- users can give you feedback without
13 * the difficulty of opening a whole new talk page. For this reason,
14 * it also tends to collect a wider range of both positive and negative
15 * comments. However you do need to tend to the feedback page. It will
16 * get long relatively quickly, and you often get multiple messages
17 * reporting the same issue.
18 *
19 * It takes the form of thing on your page which, when clicked, opens a small
20 * dialog box. Submitting that dialog box appends its contents to a
21 * wiki page that you specify, as a new section.
22 *
23 * This feature works with any content model that defines a
24 * `mw.messagePoster.MessagePoster`.
25 *
26 * Minimal usage example:
27 *
28 * var feedback = new mw.Feedback();
29 * $( '#myButton' ).click( function () { feedback.launch(); } );
30 *
31 * You can also launch the feedback form with a prefilled subject and body.
32 * See the docs for the #launch() method.
33 *
34 * @class
35 * @constructor
36 * @param {Object} [config] Configuration object
37 * @cfg {mw.Title} [title="Feedback"] The title of the page where you collect
38 * feedback.
39 * @cfg {string} [apiUrl] api.php URL if the feedback page is on another wiki
40 * @cfg {string} [dialogTitleMessageKey="feedback-dialog-title"] Message key for the
41 * title of the dialog box
42 * @cfg {mw.Uri|string} [bugsLink="//phabricator.wikimedia.org/maniphest/task/edit/form/1/"] URL where
43 * bugs can be posted
44 * @cfg {mw.Uri|string} [bugsListLink="//phabricator.wikimedia.org/maniphest/query/advanced"] URL
45 * where bugs can be listed
46 * @cfg {boolean} [showUseragentCheckbox=false] Show a Useragent agreement checkbox as part of the form.
47 * @cfg {boolean} [useragentCheckboxMandatory=false] Make the Useragent checkbox mandatory.
48 * @cfg {string|jQuery} [useragentCheckboxMessage] Supply a custom message for the useragent checkbox.
49 * defaults to the message 'feedback-terms'.
50 */
51 mw.Feedback = function MwFeedback( config ) {
52 config = config || {};
53
54 this.dialogTitleMessageKey = config.dialogTitleMessageKey || 'feedback-dialog-title';
55
56 // Feedback page title
57 this.feedbackPageTitle = config.title || new mw.Title( 'Feedback' );
58
59 this.messagePosterPromise = mw.messagePoster.factory.create( this.feedbackPageTitle, config.apiUrl );
60 this.foreignApi = config.apiUrl ? new mw.ForeignApi( config.apiUrl ) : null;
61
62 // Links
63 this.bugsTaskSubmissionLink = config.bugsLink || '//phabricator.wikimedia.org/maniphest/task/edit/form/1/';
64 this.bugsTaskListLink = config.bugsListLink || '//phabricator.wikimedia.org/maniphest/query/advanced';
65
66 // Terms of use
67 this.useragentCheckboxShow = !!config.showUseragentCheckbox;
68 this.useragentCheckboxMandatory = !!config.useragentCheckboxMandatory;
69 this.useragentCheckboxMessage = config.useragentCheckboxMessage ||
70 $( '<p>' ).append( mw.msg( 'feedback-terms' ) );
71
72 // Message dialog
73 this.thankYouDialog = new OO.ui.MessageDialog();
74 };
75
76 /* Initialize */
77 OO.initClass( mw.Feedback );
78
79 /* Static Properties */
80 mw.Feedback.static.windowManager = null;
81 mw.Feedback.static.dialog = null;
82
83 /* Methods */
84
85 /**
86 * Respond to dialog submit event. If the information was
87 * submitted successfully, open a MessageDialog to thank the user.
88 *
89 * @param {string} status A status of the end of operation
90 * of the main feedback dialog. Empty if the dialog was
91 * dismissed with no action or the user followed the button
92 * to the external task reporting site.
93 * @param {string} feedbackPageName
94 * @param {string} feedbackPageUrl
95 */
96 mw.Feedback.prototype.onDialogSubmit = function ( status, feedbackPageName, feedbackPageUrl ) {
97 var dialogConfig;
98
99 if ( status !== 'submitted' ) {
100 return;
101 }
102
103 dialogConfig = {
104 title: mw.msg( 'feedback-thanks-title' ),
105 message: $( '<span>' ).msg(
106 'feedback-thanks',
107 feedbackPageName,
108 $( '<a>' ).attr( {
109 target: '_blank',
110 href: feedbackPageUrl
111 } )
112 ),
113 actions: [
114 {
115 action: 'accept',
116 label: mw.msg( 'feedback-close' ),
117 flags: 'primary'
118 }
119 ]
120 };
121
122 // Show the message dialog
123 this.constructor.static.windowManager.openWindow(
124 this.thankYouDialog,
125 dialogConfig
126 );
127 };
128
129 /**
130 * Modify the display form, and then open it, focusing interface on the subject.
131 *
132 * @param {Object} [contents] Prefilled contents for the feedback form.
133 * @param {string} [contents.subject] The subject of the feedback, as plaintext
134 * @param {string} [contents.message] The content of the feedback, as wikitext
135 */
136 mw.Feedback.prototype.launch = function ( contents ) {
137 // Dialog
138 if ( !this.constructor.static.dialog ) {
139 this.constructor.static.dialog = new mw.Feedback.Dialog();
140 this.constructor.static.dialog.connect( this, { submit: 'onDialogSubmit' } );
141 }
142 if ( !this.constructor.static.windowManager ) {
143 this.constructor.static.windowManager = new OO.ui.WindowManager();
144 this.constructor.static.windowManager.addWindows( [
145 this.constructor.static.dialog,
146 this.thankYouDialog
147 ] );
148 $( 'body' )
149 .append( this.constructor.static.windowManager.$element );
150 }
151 // Open the dialog
152 this.constructor.static.windowManager.openWindow(
153 this.constructor.static.dialog,
154 {
155 title: mw.msg( this.dialogTitleMessageKey ),
156 foreignApi: this.foreignApi,
157 settings: {
158 messagePosterPromise: this.messagePosterPromise,
159 title: this.feedbackPageTitle,
160 dialogTitleMessageKey: this.dialogTitleMessageKey,
161 bugsTaskSubmissionLink: this.bugsTaskSubmissionLink,
162 bugsTaskListLink: this.bugsTaskListLink,
163 useragentCheckbox: {
164 show: this.useragentCheckboxShow,
165 mandatory: this.useragentCheckboxMandatory,
166 message: this.useragentCheckboxMessage
167 }
168 },
169 contents: contents
170 }
171 );
172 };
173
174 /**
175 * mw.Feedback Dialog
176 *
177 * @class
178 * @extends OO.ui.ProcessDialog
179 *
180 * @constructor
181 * @param {Object} config Configuration object
182 */
183 mw.Feedback.Dialog = function mwFeedbackDialog( config ) {
184 // Parent constructor
185 mw.Feedback.Dialog.parent.call( this, config );
186
187 this.status = '';
188 this.feedbackPageTitle = null;
189 // Initialize
190 this.$element.addClass( 'mwFeedback-Dialog' );
191 };
192
193 OO.inheritClass( mw.Feedback.Dialog, OO.ui.ProcessDialog );
194
195 /* Static properties */
196 mw.Feedback.Dialog.static.name = 'mwFeedbackDialog';
197 mw.Feedback.Dialog.static.title = mw.msg( 'feedback-dialog-title' );
198 mw.Feedback.Dialog.static.size = 'medium';
199 mw.Feedback.Dialog.static.actions = [
200 {
201 action: 'submit',
202 label: mw.msg( 'feedback-submit' ),
203 flags: [ 'primary', 'progressive' ]
204 },
205 {
206 action: 'external',
207 label: mw.msg( 'feedback-external-bug-report-button' ),
208 flags: 'progressive'
209 },
210 {
211 action: 'cancel',
212 label: mw.msg( 'feedback-cancel' ),
213 flags: 'safe'
214 }
215 ];
216
217 /**
218 * @inheritdoc
219 */
220 mw.Feedback.Dialog.prototype.initialize = function () {
221 var feedbackSubjectFieldLayout, feedbackMessageFieldLayout,
222 feedbackFieldsetLayout, termsOfUseLabel;
223
224 // Parent method
225 mw.Feedback.Dialog.parent.prototype.initialize.call( this );
226
227 this.feedbackPanel = new OO.ui.PanelLayout( {
228 scrollable: false,
229 expanded: false,
230 padded: true
231 } );
232
233 this.$spinner = $( '<div>' )
234 .addClass( 'feedback-spinner' );
235
236 // Feedback form
237 this.feedbackMessageLabel = new OO.ui.LabelWidget( {
238 classes: [ 'mw-feedbackDialog-welcome-message' ]
239 } );
240 this.feedbackSubjectInput = new OO.ui.TextInputWidget( {
241 indicator: 'required'
242 } );
243 this.feedbackMessageInput = new OO.ui.MultilineTextInputWidget( {
244 autosize: true
245 } );
246 feedbackSubjectFieldLayout = new OO.ui.FieldLayout( this.feedbackSubjectInput, {
247 label: mw.msg( 'feedback-subject' )
248 } );
249 feedbackMessageFieldLayout = new OO.ui.FieldLayout( this.feedbackMessageInput, {
250 label: mw.msg( 'feedback-message' )
251 } );
252 feedbackFieldsetLayout = new OO.ui.FieldsetLayout( {
253 items: [ feedbackSubjectFieldLayout, feedbackMessageFieldLayout ],
254 classes: [ 'mw-feedbackDialog-feedback-form' ]
255 } );
256
257 // Useragent terms of use
258 this.useragentCheckbox = new OO.ui.CheckboxInputWidget();
259 this.useragentFieldLayout = new OO.ui.FieldLayout( this.useragentCheckbox, {
260 classes: [ 'mw-feedbackDialog-feedback-terms' ],
261 align: 'inline'
262 } );
263
264 termsOfUseLabel = new OO.ui.LabelWidget( {
265 classes: [ 'mw-feedbackDialog-feedback-termsofuse' ],
266 label: $( '<p>' ).append( mw.msg( 'feedback-termsofuse' ) )
267 } );
268
269 this.feedbackPanel.$element.append(
270 this.feedbackMessageLabel.$element,
271 feedbackFieldsetLayout.$element,
272 this.useragentFieldLayout.$element,
273 termsOfUseLabel.$element
274 );
275
276 // Events
277 this.feedbackSubjectInput.connect( this, { change: 'validateFeedbackForm' } );
278 this.feedbackMessageInput.connect( this, { change: 'validateFeedbackForm' } );
279 this.feedbackMessageInput.connect( this, { change: 'updateSize' } );
280 this.useragentCheckbox.connect( this, { change: 'validateFeedbackForm' } );
281
282 this.$body.append( this.feedbackPanel.$element );
283 };
284
285 /**
286 * Validate the feedback form
287 */
288 mw.Feedback.Dialog.prototype.validateFeedbackForm = function () {
289 var isValid = (
290 (
291 !this.useragentMandatory ||
292 this.useragentCheckbox.isSelected()
293 ) &&
294 this.feedbackSubjectInput.getValue()
295 );
296
297 this.actions.setAbilities( { submit: isValid } );
298 };
299
300 /**
301 * @inheritdoc
302 */
303 mw.Feedback.Dialog.prototype.getBodyHeight = function () {
304 return this.feedbackPanel.$element.outerHeight( true );
305 };
306
307 /**
308 * @inheritdoc
309 */
310 mw.Feedback.Dialog.prototype.getSetupProcess = function ( data ) {
311 return mw.Feedback.Dialog.parent.prototype.getSetupProcess.call( this, data )
312 .next( function () {
313 // Get the URL of the target page, we want to use that in links in the intro
314 // and in the success dialog
315 var dialog = this;
316 if ( data.foreignApi ) {
317 return data.foreignApi.get( {
318 action: 'query',
319 prop: 'info',
320 inprop: 'url',
321 formatversion: 2,
322 titles: data.settings.title.getPrefixedText()
323 } ).then( function ( data ) {
324 dialog.feedbackPageUrl = OO.getProp( data, 'query', 'pages', 0, 'canonicalurl' );
325 } );
326 } else {
327 this.feedbackPageUrl = data.settings.title.getUrl();
328 }
329 }, this )
330 .next( function () {
331 var $link,
332 settings = data.settings;
333 data.contents = data.contents || {};
334
335 // Prefill subject/message
336 this.feedbackSubjectInput.setValue( data.contents.subject );
337 this.feedbackMessageInput.setValue( data.contents.message );
338
339 this.status = '';
340 this.messagePosterPromise = settings.messagePosterPromise;
341 this.setBugReportLink( settings.bugsTaskSubmissionLink );
342 this.feedbackPageTitle = settings.title;
343 this.feedbackPageName = settings.title.getNameText();
344
345 // Useragent checkbox
346 if ( settings.useragentCheckbox.show ) {
347 this.useragentFieldLayout.setLabel( settings.useragentCheckbox.message );
348 }
349
350 this.useragentMandatory = settings.useragentCheckbox.mandatory;
351 this.useragentFieldLayout.toggle( settings.useragentCheckbox.show );
352
353 $link = $( '<a>' )
354 .attr( 'href', this.feedbackPageUrl )
355 .attr( 'target', '_blank' )
356 .text( this.feedbackPageName );
357 this.feedbackMessageLabel.setLabel(
358 mw.message( 'feedback-dialog-intro', $link ).parseDom()
359 );
360
361 this.validateFeedbackForm();
362 }, this );
363 };
364
365 /**
366 * @inheritdoc
367 */
368 mw.Feedback.Dialog.prototype.getReadyProcess = function ( data ) {
369 return mw.Feedback.Dialog.parent.prototype.getReadyProcess.call( this, data )
370 .next( function () {
371 this.feedbackSubjectInput.focus();
372 }, this );
373 };
374
375 /**
376 * @inheritdoc
377 */
378 mw.Feedback.Dialog.prototype.getActionProcess = function ( action ) {
379 if ( action === 'cancel' ) {
380 return new OO.ui.Process( function () {
381 this.close( { action: action } );
382 }, this );
383 } else if ( action === 'external' ) {
384 return new OO.ui.Process( function () {
385 // Open in a new window
386 window.open( this.getBugReportLink(), '_blank' );
387 // Close the dialog
388 this.close();
389 }, this );
390 } else if ( action === 'submit' ) {
391 return new OO.ui.Process( function () {
392 var fb = this,
393 userAgentMessage = ':' +
394 '<small>' +
395 mw.msg( 'feedback-useragent' ) +
396 ' ' +
397 mw.html.escape( navigator.userAgent ) +
398 '</small>\n\n',
399 subject = this.feedbackSubjectInput.getValue(),
400 message = this.feedbackMessageInput.getValue();
401
402 // Add user agent if checkbox is selected
403 if ( this.useragentCheckbox.isSelected() ) {
404 message = userAgentMessage + message;
405 }
406
407 // Post the message
408 return this.messagePosterPromise.then( function ( poster ) {
409 return fb.postMessage( poster, subject, message );
410 }, function () {
411 fb.status = 'error4';
412 mw.log.warn( 'Feedback report failed because MessagePoster could not be fetched' );
413 } ).then( function () {
414 fb.close();
415 }, function () {
416 return fb.getErrorMessage();
417 } );
418 }, this );
419 }
420 // Fallback to parent handler
421 return mw.Feedback.Dialog.parent.prototype.getActionProcess.call( this, action );
422 };
423
424 /**
425 * Returns an error message for the current status.
426 *
427 * @private
428 *
429 * @return {OO.ui.Error}
430 */
431 mw.Feedback.Dialog.prototype.getErrorMessage = function () {
432 // Messages: feedback-error1, feedback-error2, feedback-error3, feedback-error4
433 return new OO.ui.Error( mw.msg( 'feedback-' + this.status ) );
434 };
435
436 /**
437 * Posts the message
438 *
439 * @private
440 *
441 * @param {mw.messagePoster.MessagePoster} poster Poster implementation used to leave feedback
442 * @param {string} subject Subject of message
443 * @param {string} message Body of message
444 * @return {jQuery.Promise} Promise representing success of message posting action
445 */
446 mw.Feedback.Dialog.prototype.postMessage = function ( poster, subject, message ) {
447 var fb = this;
448
449 return poster.post(
450 subject,
451 message
452 ).then( function () {
453 fb.status = 'submitted';
454 }, function ( mainCode, secondaryCode, details ) {
455 if ( mainCode === 'api-fail' ) {
456 if ( secondaryCode === 'http' ) {
457 fb.status = 'error3';
458 // ajax request failed
459 mw.log.warn( 'Feedback report failed with HTTP error: ' + details.textStatus );
460 } else {
461 fb.status = 'error2';
462 mw.log.warn( 'Feedback report failed with API error: ' + secondaryCode );
463 }
464 } else {
465 fb.status = 'error1';
466 }
467 } );
468 };
469
470 /**
471 * @inheritdoc
472 */
473 mw.Feedback.Dialog.prototype.getTeardownProcess = function ( data ) {
474 return mw.Feedback.Dialog.parent.prototype.getTeardownProcess.call( this, data )
475 .first( function () {
476 this.emit( 'submit', this.status, this.feedbackPageName, this.feedbackPageUrl );
477 // Cleanup
478 this.status = '';
479 this.feedbackPageTitle = null;
480 this.feedbackSubjectInput.setValue( '' );
481 this.feedbackMessageInput.setValue( '' );
482 this.useragentCheckbox.setSelected( false );
483 }, this );
484 };
485
486 /**
487 * Set the bug report link
488 *
489 * @param {string} link Link to the external bug report form
490 */
491 mw.Feedback.Dialog.prototype.setBugReportLink = function ( link ) {
492 this.bugReportLink = link;
493 };
494
495 /**
496 * Get the bug report link
497 *
498 * @return {string} Link to the external bug report form
499 */
500 mw.Feedback.Dialog.prototype.getBugReportLink = function () {
501 return this.bugReportLink;
502 };
503
504 }() );