4 * @author Ryan Kaldari, 2010
5 * @author Neil Kandalgaonkar, 2010-11
6 * @author Moriel Schottlender, 2015
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.
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.
23 * This feature works with any content model that defines a
24 * `mw.messagePoster.MessagePoster`.
26 * Minimal usage example:
28 * var feedback = new mw.Feedback();
29 * $( '#myButton' ).click( function () { feedback.launch(); } );
31 * You can also launch the feedback form with a prefilled subject and body.
32 * See the docs for the #launch() method.
36 * @param {Object} [config] Configuration object
37 * @cfg {mw.Title} [title="Feedback"] The title of the page where you collect
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
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'.
51 mw
.Feedback
= function MwFeedback( config
) {
52 config
= config
|| {};
54 this.dialogTitleMessageKey
= config
.dialogTitleMessageKey
|| 'feedback-dialog-title';
56 // Feedback page title
57 this.feedbackPageTitle
= config
.title
|| new mw
.Title( 'Feedback' );
59 this.messagePosterPromise
= mw
.messagePoster
.factory
.create( this.feedbackPageTitle
, config
.apiUrl
);
60 this.foreignApi
= config
.apiUrl
? new mw
.ForeignApi( config
.apiUrl
) : null;
63 this.bugsTaskSubmissionLink
= config
.bugsLink
|| '//phabricator.wikimedia.org/maniphest/task/edit/form/1/';
64 this.bugsTaskListLink
= config
.bugsListLink
|| '//phabricator.wikimedia.org/maniphest/query/advanced';
67 this.useragentCheckboxShow
= !!config
.showUseragentCheckbox
;
68 this.useragentCheckboxMandatory
= !!config
.useragentCheckboxMandatory
;
69 this.useragentCheckboxMessage
= config
.useragentCheckboxMessage
||
70 $( '<p>' ).append( mw
.msg( 'feedback-terms' ) );
73 this.thankYouDialog
= new OO
.ui
.MessageDialog();
77 OO
.initClass( mw
.Feedback
);
79 /* Static Properties */
80 mw
.Feedback
.static.windowManager
= null;
81 mw
.Feedback
.static.dialog
= null;
86 * Respond to dialog submit event. If the information was
87 * submitted successfully, open a MessageDialog to thank the user.
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
96 mw
.Feedback
.prototype.onDialogSubmit = function ( status
, feedbackPageName
, feedbackPageUrl
) {
99 if ( status
!== 'submitted' ) {
104 title
: mw
.msg( 'feedback-thanks-title' ),
105 message
: $( '<span>' ).msg(
110 href
: feedbackPageUrl
116 label
: mw
.msg( 'feedback-close' ),
122 // Show the message dialog
123 this.constructor.static.windowManager
.openWindow(
130 * Modify the display form, and then open it, focusing interface on the subject.
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
136 mw
.Feedback
.prototype.launch = function ( contents
) {
138 if ( !this.constructor.static.dialog
) {
139 this.constructor.static.dialog
= new mw
.Feedback
.Dialog();
140 this.constructor.static.dialog
.connect( this, { submit
: 'onDialogSubmit' } );
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
,
149 .append( this.constructor.static.windowManager
.$element
);
152 this.constructor.static.windowManager
.openWindow(
153 this.constructor.static.dialog
,
155 title
: mw
.msg( this.dialogTitleMessageKey
),
156 foreignApi
: this.foreignApi
,
158 messagePosterPromise
: this.messagePosterPromise
,
159 title
: this.feedbackPageTitle
,
160 dialogTitleMessageKey
: this.dialogTitleMessageKey
,
161 bugsTaskSubmissionLink
: this.bugsTaskSubmissionLink
,
162 bugsTaskListLink
: this.bugsTaskListLink
,
164 show
: this.useragentCheckboxShow
,
165 mandatory
: this.useragentCheckboxMandatory
,
166 message
: this.useragentCheckboxMessage
178 * @extends OO.ui.ProcessDialog
181 * @param {Object} config Configuration object
183 mw
.Feedback
.Dialog
= function mwFeedbackDialog( config
) {
184 // Parent constructor
185 mw
.Feedback
.Dialog
.parent
.call( this, config
);
188 this.feedbackPageTitle
= null;
190 this.$element
.addClass( 'mwFeedback-Dialog' );
193 OO
.inheritClass( mw
.Feedback
.Dialog
, OO
.ui
.ProcessDialog
);
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
= [
202 label
: mw
.msg( 'feedback-submit' ),
203 flags
: [ 'primary', 'progressive' ]
207 label
: mw
.msg( 'feedback-external-bug-report-button' ),
212 label
: mw
.msg( 'feedback-cancel' ),
220 mw
.Feedback
.Dialog
.prototype.initialize = function () {
221 var feedbackSubjectFieldLayout
, feedbackMessageFieldLayout
,
222 feedbackFieldsetLayout
, termsOfUseLabel
;
225 mw
.Feedback
.Dialog
.parent
.prototype.initialize
.call( this );
227 this.feedbackPanel
= new OO
.ui
.PanelLayout( {
233 this.$spinner
= $( '<div>' )
234 .addClass( 'feedback-spinner' );
237 this.feedbackMessageLabel
= new OO
.ui
.LabelWidget( {
238 classes
: [ 'mw-feedbackDialog-welcome-message' ]
240 this.feedbackSubjectInput
= new OO
.ui
.TextInputWidget( {
241 indicator
: 'required'
243 this.feedbackMessageInput
= new OO
.ui
.MultilineTextInputWidget( {
246 feedbackSubjectFieldLayout
= new OO
.ui
.FieldLayout( this.feedbackSubjectInput
, {
247 label
: mw
.msg( 'feedback-subject' )
249 feedbackMessageFieldLayout
= new OO
.ui
.FieldLayout( this.feedbackMessageInput
, {
250 label
: mw
.msg( 'feedback-message' )
252 feedbackFieldsetLayout
= new OO
.ui
.FieldsetLayout( {
253 items
: [ feedbackSubjectFieldLayout
, feedbackMessageFieldLayout
],
254 classes
: [ 'mw-feedbackDialog-feedback-form' ]
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' ],
264 termsOfUseLabel
= new OO
.ui
.LabelWidget( {
265 classes
: [ 'mw-feedbackDialog-feedback-termsofuse' ],
266 label
: $( '<p>' ).append( mw
.msg( 'feedback-termsofuse' ) )
269 this.feedbackPanel
.$element
.append(
270 this.feedbackMessageLabel
.$element
,
271 feedbackFieldsetLayout
.$element
,
272 this.useragentFieldLayout
.$element
,
273 termsOfUseLabel
.$element
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' } );
282 this.$body
.append( this.feedbackPanel
.$element
);
286 * Validate the feedback form
288 mw
.Feedback
.Dialog
.prototype.validateFeedbackForm = function () {
291 !this.useragentMandatory
||
292 this.useragentCheckbox
.isSelected()
294 this.feedbackSubjectInput
.getValue()
297 this.actions
.setAbilities( { submit
: isValid
} );
303 mw
.Feedback
.Dialog
.prototype.getBodyHeight = function () {
304 return this.feedbackPanel
.$element
.outerHeight( true );
310 mw
.Feedback
.Dialog
.prototype.getSetupProcess = function ( data
) {
311 return mw
.Feedback
.Dialog
.parent
.prototype.getSetupProcess
.call( this, data
)
313 // Get the URL of the target page, we want to use that in links in the intro
314 // and in the success dialog
316 if ( data
.foreignApi
) {
317 return data
.foreignApi
.get( {
322 titles
: data
.settings
.title
.getPrefixedText()
323 } ).then( function ( data
) {
324 dialog
.feedbackPageUrl
= OO
.getProp( data
, 'query', 'pages', 0, 'canonicalurl' );
327 this.feedbackPageUrl
= data
.settings
.title
.getUrl();
331 var plainMsg
, parsedMsg
,
332 settings
= data
.settings
;
333 data
.contents
= data
.contents
|| {};
335 // Prefill subject/message
336 this.feedbackSubjectInput
.setValue( data
.contents
.subject
);
337 this.feedbackMessageInput
.setValue( data
.contents
.message
);
340 this.messagePosterPromise
= settings
.messagePosterPromise
;
341 this.setBugReportLink( settings
.bugsTaskSubmissionLink
);
342 this.feedbackPageTitle
= settings
.title
;
343 this.feedbackPageName
= settings
.title
.getNameText();
345 // Useragent checkbox
346 if ( settings
.useragentCheckbox
.show
) {
347 this.useragentFieldLayout
.setLabel( settings
.useragentCheckbox
.message
);
350 this.useragentMandatory
= settings
.useragentCheckbox
.mandatory
;
351 this.useragentFieldLayout
.toggle( settings
.useragentCheckbox
.show
);
353 // HACK: Setting a link in the messages doesn't work. There is already a report
354 // about this, and the bug report offers a somewhat hacky work around that
355 // includes setting a separate message to be parsed.
356 // We want to make sure the user can configure both the title of the page and
357 // a separate url, so this must be allowed to parse correctly.
358 // See https://phabricator.wikimedia.org/T49395#490610
360 'feedback-dialog-temporary-message':
361 '<a href="' + this.feedbackPageUrl
+ '" target="_blank">' + this.feedbackPageName
+ '</a>'
363 plainMsg
= mw
.message( 'feedback-dialog-temporary-message' ).plain();
364 mw
.messages
.set( { 'feedback-dialog-temporary-message-parsed': plainMsg
} );
365 parsedMsg
= mw
.message( 'feedback-dialog-temporary-message-parsed' );
366 this.feedbackMessageLabel
.setLabel(
369 .append( mw
.message( 'feedback-dialog-intro', parsedMsg
).parse() )
372 this.validateFeedbackForm();
379 mw
.Feedback
.Dialog
.prototype.getReadyProcess = function ( data
) {
380 return mw
.Feedback
.Dialog
.parent
.prototype.getReadyProcess
.call( this, data
)
382 this.feedbackSubjectInput
.focus();
389 mw
.Feedback
.Dialog
.prototype.getActionProcess = function ( action
) {
390 if ( action
=== 'cancel' ) {
391 return new OO
.ui
.Process( function () {
392 this.close( { action
: action
} );
394 } else if ( action
=== 'external' ) {
395 return new OO
.ui
.Process( function () {
396 // Open in a new window
397 window
.open( this.getBugReportLink(), '_blank' );
401 } else if ( action
=== 'submit' ) {
402 return new OO
.ui
.Process( function () {
404 userAgentMessage
= ':' +
406 mw
.msg( 'feedback-useragent' ) +
408 mw
.html
.escape( navigator
.userAgent
) +
410 subject
= this.feedbackSubjectInput
.getValue(),
411 message
= this.feedbackMessageInput
.getValue();
413 // Add user agent if checkbox is selected
414 if ( this.useragentCheckbox
.isSelected() ) {
415 message
= userAgentMessage
+ message
;
419 return this.messagePosterPromise
.then( function ( poster
) {
420 return fb
.postMessage( poster
, subject
, message
);
422 fb
.status
= 'error4';
423 mw
.log
.warn( 'Feedback report failed because MessagePoster could not be fetched' );
424 } ).then( function () {
427 return fb
.getErrorMessage();
431 // Fallback to parent handler
432 return mw
.Feedback
.Dialog
.parent
.prototype.getActionProcess
.call( this, action
);
436 * Returns an error message for the current status.
440 * @return {OO.ui.Error}
442 mw
.Feedback
.Dialog
.prototype.getErrorMessage = function () {
443 // Messages: feedback-error1, feedback-error2, feedback-error3, feedback-error4
444 return new OO
.ui
.Error( mw
.msg( 'feedback-' + this.status
) );
452 * @param {mw.messagePoster.MessagePoster} poster Poster implementation used to leave feedback
453 * @param {string} subject Subject of message
454 * @param {string} message Body of message
455 * @return {jQuery.Promise} Promise representing success of message posting action
457 mw
.Feedback
.Dialog
.prototype.postMessage = function ( poster
, subject
, message
) {
463 ).then( function () {
464 fb
.status
= 'submitted';
465 }, function ( mainCode
, secondaryCode
, details
) {
466 if ( mainCode
=== 'api-fail' ) {
467 if ( secondaryCode
=== 'http' ) {
468 fb
.status
= 'error3';
469 // ajax request failed
470 mw
.log
.warn( 'Feedback report failed with HTTP error: ' + details
.textStatus
);
472 fb
.status
= 'error2';
473 mw
.log
.warn( 'Feedback report failed with API error: ' + secondaryCode
);
476 fb
.status
= 'error1';
484 mw
.Feedback
.Dialog
.prototype.getTeardownProcess = function ( data
) {
485 return mw
.Feedback
.Dialog
.parent
.prototype.getTeardownProcess
.call( this, data
)
486 .first( function () {
487 this.emit( 'submit', this.status
, this.feedbackPageName
, this.feedbackPageUrl
);
490 this.feedbackPageTitle
= null;
491 this.feedbackSubjectInput
.setValue( '' );
492 this.feedbackMessageInput
.setValue( '' );
493 this.useragentCheckbox
.setSelected( false );
498 * Set the bug report link
500 * @param {string} link Link to the external bug report form
502 mw
.Feedback
.Dialog
.prototype.setBugReportLink = function ( link
) {
503 this.bugReportLink
= link
;
507 * Get the bug report link
509 * @return {string} Link to the external bug report form
511 mw
.Feedback
.Dialog
.prototype.getBugReportLink = function () {
512 return this.bugReportLink
;