| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
function FeedbackDialog( config ) { |
| 13 |
|
| 14 |
FeedbackDialog.super.call( this, config ); |
| 15 |
|
| 16 |
this.status = ''; |
| 17 |
this.feedbackPageTitle = null; |
| 18 |
|
| 19 |
this.$element.addClass( 'mwFeedback-Dialog' ); |
| 20 |
} |
| 21 |
|
| 22 |
OO.inheritClass( FeedbackDialog, OO.ui.ProcessDialog ); |
| 23 |
|
| 24 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
FeedbackDialog.prototype.initialize = function () { |
| 53 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 133 |
|
| 134 |
|
| 135 |
FeedbackDialog.prototype.getBodyHeight = function () { |
| 136 |
return this.feedbackPanel.$element.outerHeight( true ); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
FeedbackDialog.prototype.getSetupProcess = function ( data ) { |
| 144 |
return FeedbackDialog.super.prototype.getSetupProcess.call( this, data ) |
| 145 |
.next( function () { |
| 146 |
|
| 147 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 199 |
|
| 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 |
|
| 210 |
|
| 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 |
|
| 220 |
window.open( this.getBugReportLink(), '_blank' ); |
| 221 |
|
| 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 |
|
| 237 |
if ( this.useragentCheckbox.isSelected() ) { |
| 238 |
message = userAgentMessage + message; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 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 |
|
| 251 |
return FeedbackDialog.super.prototype.getActionProcess.call( this, action ); |
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
FeedbackDialog.prototype.getErrorMessage = function () { |
| 262 |
if ( this.$statusFromApi ) { |
| 263 |
return new OO.ui.Error( this.$statusFromApi ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
return new OO.ui.Error( mw.msg( 'feedback-' + this.status ) ); |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 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 |
|
| 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 |
|
| 308 |
|
| 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 |
|
| 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 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
FeedbackDialog.prototype.setBugReportLink = function ( link ) { |
| 331 |
this.bugReportLink = link; |
| 332 |
}; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
FeedbackDialog.prototype.getBugReportLink = function () { |
| 342 |
return this.bugReportLink; |
| 343 |
}; |
| 344 |
|
| 345 |
module.exports = FeedbackDialog; |