Debian Codegraph @

resources/src/mediawiki.feedback/FeedbackDialog.js

raw symbols SCIP
1 /**
2 * @class Dialog
3 * @classdesc Feedback dialog for use within the context mw.Feedback. Typically
4 * constructed using {@link mw.Feedback#launch} instead of directly using the constructor.
5 * @memberof mw.Feedback
6 * @extends OO.ui.ProcessDialog
7 *
8 * @constructor
9 * @description Create an instance of `mw.Feedback.Dialog`.
10 * @param {Object} config Configuration object
11 */
12 function FeedbackDialog( config ) {
13 // Parent constructor
14 FeedbackDialog.super.call( this, config );
15
16 this.status = '';
17 this.feedbackPageTitle = null;
18 // Initialize
19 this.$element.addClass( 'mwFeedback-Dialog' );
20 }
21
22 OO.inheritClass( FeedbackDialog, OO.ui.ProcessDialog );
23
24 /* Static properties */
25 FeedbackDialog.static.name = 'mwFeedbackDialog';
26 FeedbackDialog.static.title = mw.msg( 'feedback-dialog-title' );
27 FeedbackDialog.static.size = 'medium';
28 FeedbackDialog.static.actions = [
29 {
30 action: 'submit',
31 label: mw.msg( 'feedback-submit' ),
32 flags: [ 'primary', 'progressive' ]
33 },
34 {
35 action: 'external',
36 label: mw.msg( 'feedback-external-bug-report-button' ),
37 flags: 'progressive'
38 },
39 {
40 action: 'cancel',
41 label: mw.msg( 'feedback-cancel' ),
42 flags: 'safe'
43 }
44 ];
45
46 /**
47 * Initializes the dialog.
48 *
49 * @ignore
50 * @inheritdoc
51 */
52 FeedbackDialog.prototype.initialize = function () {
53 // Parent method
54 FeedbackDialog.super.prototype.initialize.call( this );
55
56 this.feedbackPanel = new OO.ui.PanelLayout( {
57 scrollable: false,
58 expanded: false,
59 padded: true
60 } );
61
62 // Feedback form
63 this.feedbackMessageLabel = new OO.ui.LabelWidget( {
64 classes: [ 'mw-feedbackDialog-welcome-message' ]
65 } );
66 this.feedbackSubjectInput = new OO.ui.TextInputWidget( {
67 indicator: 'required'
68 } );
69 this.feedbackMessageInput = new OO.ui.MultilineTextInputWidget( {
70 autosize: true
71 } );
72 const feedbackSubjectFieldLayout = new OO.ui.FieldLayout( this.feedbackSubjectInput, {
73 label: mw.msg( 'feedback-subject' )
74 } );
75 const feedbackMessageFieldLayout = new OO.ui.FieldLayout( this.feedbackMessageInput, {
76 label: mw.msg( 'feedback-message' )
77 } );
78 const feedbackFieldsetLayout = new OO.ui.FieldsetLayout( {
79 items: [ feedbackSubjectFieldLayout, feedbackMessageFieldLayout ],
80 classes: [ 'mw-feedbackDialog-feedback-form' ]
81 } );
82
83 // Useragent terms of use
84 this.useragentCheckbox = new OO.ui.CheckboxInputWidget();
85 this.useragentFieldLayout = new OO.ui.FieldLayout( this.useragentCheckbox, {
86 classes: [ 'mw-feedbackDialog-feedback-terms' ],
87 align: 'inline'
88 } );
89
90 const $termsOfUseLabelText = $( '<p>' ).append( mw.message( 'feedback-termsofuse' ).parseDom() );
91 $termsOfUseLabelText.find( 'a' ).attr( 'target', '_blank' );
92 const termsOfUseLabel = new OO.ui.LabelWidget( {
93 classes: [ 'mw-feedbackDialog-feedback-termsofuse' ],
94 label: $termsOfUseLabelText
95 } );
96
97 this.feedbackPanel.$element.append(
98 this.feedbackMessageLabel.$element,
99 feedbackFieldsetLayout.$element,
100 this.useragentFieldLayout.$element,
101 termsOfUseLabel.$element
102 );
103
104 // Events
105 this.feedbackSubjectInput.connect( this, { change: 'validateFeedbackForm' } );
106 this.feedbackMessageInput.connect( this, { change: 'validateFeedbackForm' } );
107 this.feedbackMessageInput.connect( this, { change: 'updateSize' } );
108 this.useragentCheckbox.connect( this, { change: 'validateFeedbackForm' } );
109
110 this.$body.append( this.feedbackPanel.$element );
111 };
112
113 /**
114 * Validate the feedback form.
115 *
116 * @method validateFeedbackForm
117 * @memberof mw.Feedback.Dialog
118 */
119 FeedbackDialog.prototype.validateFeedbackForm = function () {
120 const isValid = (
121 (
122 !this.useragentMandatory ||
123 this.useragentCheckbox.isSelected()
124 ) &&
125 this.feedbackSubjectInput.getValue()
126 );
127
128 this.actions.setAbilities( { submit: isValid } );
129 };
130
131 /**
132 * @inheritdoc
133 * @ignore
134 */
135 FeedbackDialog.prototype.getBodyHeight = function () {
136 return this.feedbackPanel.$element.outerHeight( true );
137 };
138
139 /**
140 * @inheritdoc
141 * @ignore
142 */
143 FeedbackDialog.prototype.getSetupProcess = function ( data ) {
144 return FeedbackDialog.super.prototype.getSetupProcess.call( this, data )
145 .next( function () {
146 // Get the URL of the target page, we want to use that in links in the intro
147 // and in the success dialog
148 const dialog = this;
149 if ( data.foreignApi ) {
150 return data.foreignApi.get( {
151 action: 'query',
152 prop: 'info',
153 inprop: 'url',
154 formatversion: 2,
155 titles: data.settings.title.getPrefixedText()
156 } ).then( ( response ) => {
157 dialog.feedbackPageUrl = OO.getProp( response, 'query', 'pages', 0, 'canonicalurl' );
158 } );
159 } else {
160 this.feedbackPageUrl = data.settings.title.getUrl();
161 }
162 }, this )
163 .next( function () {
164 const settings = data.settings;
165 data.contents = data.contents || {};
166
167 // Prefill subject/message
168 this.feedbackSubjectInput.setValue( data.contents.subject );
169 this.feedbackMessageInput.setValue( data.contents.message );
170
171 this.status = '';
172 this.messagePosterPromise = settings.messagePosterPromise;
173 this.setBugReportLink( settings.bugsTaskSubmissionLink );
174 this.feedbackPageTitle = settings.title;
175 this.feedbackPageName = settings.title.getMainText();
176
177 // Useragent checkbox
178 if ( settings.useragentCheckbox.show ) {
179 this.useragentFieldLayout.setLabel( settings.useragentCheckbox.message );
180 }
181
182 this.useragentMandatory = settings.useragentCheckbox.mandatory;
183 this.useragentFieldLayout.toggle( settings.useragentCheckbox.show );
184
185 const $link = $( '<a>' )
186 .attr( 'href', this.feedbackPageUrl )
187 .attr( 'target', '_blank' )
188 .text( this.feedbackPageName );
189 this.feedbackMessageLabel.setLabel(
190 mw.message( 'feedback-dialog-intro', $link ).parseDom()
191 );
192
193 this.validateFeedbackForm();
194 }, this );
195 };
196
197 /**
198 * @inheritdoc
199 * @ignore
200 */
201 FeedbackDialog.prototype.getReadyProcess = function ( data ) {
202 return FeedbackDialog.super.prototype.getReadyProcess.call( this, data )
203 .next( function () {
204 this.feedbackSubjectInput.focus();
205 }, this );
206 };
207
208 /**
209 * @inheritdoc
210 * @ignore
211 */
212 FeedbackDialog.prototype.getActionProcess = function ( action ) {
213 if ( action === 'cancel' ) {
214 return new OO.ui.Process( function () {
215 this.close( { action: action } );
216 }, this );
217 } else if ( action === 'external' ) {
218 return new OO.ui.Process( function () {
219 // Open in a new window
220 window.open( this.getBugReportLink(), '_blank' );
221 // Close the dialog
222 this.close();
223 }, this );
224 } else if ( action === 'submit' ) {
225 return new OO.ui.Process( function () {
226 const fb = this,
227 userAgentMessage = ':' +
228 '<small>' +
229 mw.msg( 'feedback-useragent' ) +
230 ' ' +
231 mw.html.escape( navigator.userAgent ) +
232 '</small>\n\n',
233 subject = this.feedbackSubjectInput.getValue();
234 let message = this.feedbackMessageInput.getValue();
235
236 // Add user agent if checkbox is selected
237 if ( this.useragentCheckbox.isSelected() ) {
238 message = userAgentMessage + message;
239 }
240
241 // Post the message
242 return this.messagePosterPromise.then( ( poster ) => fb.postMessage( poster, subject, message ), () => {
243 fb.status = 'error4';
244 mw.log.warn( 'Feedback report failed because MessagePoster could not be fetched' );
245 } ).then( () => {
246 fb.close();
247 }, () => fb.getErrorMessage() );
248 }, this );
249 }
250 // Fallback to parent handler
251 return FeedbackDialog.super.prototype.getActionProcess.call( this, action );
252 };
253
254 /**
255 * Returns an error message for the current status.
256 *
257 * @private
258 *
259 * @return {OO.ui.Error}
260 */
261 FeedbackDialog.prototype.getErrorMessage = function () {
262 if ( this.$statusFromApi ) {
263 return new OO.ui.Error( this.$statusFromApi );
264 }
265 // The following messages can be used here:
266 // * feedback-error1
267 // * feedback-error4
268 return new OO.ui.Error( mw.msg( 'feedback-' + this.status ) );
269 };
270
271 /**
272 * Posts the message
273 *
274 * @private
275 *
276 * @param {mw.messagePoster.MessagePoster} poster Poster implementation used to leave feedback
277 * @param {string} subject Subject of message
278 * @param {string} message Body of message
279 * @return {jQuery.Promise} Promise representing success of message posting action
280 */
281 FeedbackDialog.prototype.postMessage = function ( poster, subject, message ) {
282 const fb = this;
283
284 return poster.post(
285 subject,
286 message
287 ).then( () => {
288 fb.status = 'submitted';
289 }, ( mainCode, secondaryCode, details ) => {
290 if ( mainCode === 'api-fail' ) {
291 if ( secondaryCode === 'http' ) {
292 fb.status = 'error3';
293 // ajax request failed
294 mw.log.warn( 'Feedback report failed with HTTP error: ' + details.textStatus );
295 } else {
296 fb.status = 'error2';
297 mw.log.warn( 'Feedback report failed with API error: ' + secondaryCode );
298 }
299 fb.$statusFromApi = ( new mw.Api() ).getErrorMessage( details );
300 } else {
301 fb.status = 'error1';
302 }
303 } );
304 };
305
306 /**
307 * @ignore
308 * @inheritdoc
309 */
310 FeedbackDialog.prototype.getTeardownProcess = function ( data ) {
311 return FeedbackDialog.super.prototype.getTeardownProcess.call( this, data )
312 .first( function () {
313 this.emit( 'submit', this.status, this.feedbackPageName, this.feedbackPageUrl );
314 // Cleanup
315 this.status = '';
316 this.feedbackPageTitle = null;
317 this.feedbackSubjectInput.setValue( '' );
318 this.feedbackMessageInput.setValue( '' );
319 this.useragentCheckbox.setSelected( false );
320 }, this );
321 };
322
323 /**
324 * Set the bug report link.
325 *
326 * @method setBugReportLink
327 * @param {string} link Link to the external bug report form
328 * @memberof mw.Feedback.Dialog
329 */
330 FeedbackDialog.prototype.setBugReportLink = function ( link ) {
331 this.bugReportLink = link;
332 };
333
334 /**
335 * Get the bug report link.
336 *
337 * @method getBugReportLink
338 * @return {string} Link to the external bug report form
339 * @memberof mw.Feedback.Dialog
340 */
341 FeedbackDialog.prototype.getBugReportLink = function () {
342 return this.bugReportLink;
343 };
344
345 module.exports = FeedbackDialog;