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