Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/dialogs/ve.ui.MWSaveDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWSaveDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Dialog for saving MediaWiki pages.
10 *
11 * Note that most methods are not safe to call before the dialog has initialized, except where
12 * noted otherwise.
13 *
14 * @class
15 * @extends OO.ui.ProcessDialog
16 *
17 * @constructor
18 * @param {Object} [config] Config options
19 */
20 ve.ui.MWSaveDialog = function VeUiMwSaveDialog( config ) {
21 // Parent constructor
22 ve.ui.MWSaveDialog.super.call( this, config );
23
24 // Properties
25 this.editSummaryCodePointLimit = mw.config.get( 'wgCommentCodePointLimit' );
26 this.restoring = false;
27 this.messages = {};
28 this.setupDeferred = ve.createDeferred();
29 this.checkboxesByName = null;
30 this.changedEditSummary = false;
31 this.canReview = false;
32 this.canPreview = false;
33 this.hasDiff = false;
34 this.diffElement = null;
35 this.diffElementPromise = null;
36 this.getDiffElementPromise = null;
37
38 // Initialization
39 this.$element.addClass( 've-ui-mwSaveDialog' );
40 };
41
42 /* Inheritance */
43
44 OO.inheritClass( ve.ui.MWSaveDialog, OO.ui.ProcessDialog );
45
46 /* Static Properties */
47
48 ve.ui.MWSaveDialog.static.name = 'mwSave';
49
50 ve.ui.MWSaveDialog.static.title =
51 OO.ui.deferMsg( 'visualeditor-savedialog-title-save' );
52
53 ve.ui.MWSaveDialog.static.actions = [
54 {
55 action: 'save',
56 // label will be set by config.saveButtonLabel
57 flags: [ 'primary', 'progressive' ],
58 modes: [ 'save', 'review', 'preview' ]
59 },
60 {
61 label: OO.ui.deferMsg( 'visualeditor-savedialog-label-resume-editing' ),
62 flags: [ 'safe', OO.ui.isMobile() ? 'back' : 'close' ],
63 modes: [ 'save', 'conflict' ]
64 },
65 {
66 action: 'review',
67 label: OO.ui.deferMsg( 'visualeditor-savedialog-label-review' ),
68 modes: [ 'save', 'preview' ]
69 },
70 {
71 action: 'preview',
72 label: OO.ui.deferMsg( 'showpreview' ),
73 modes: [ 'save', 'review' ]
74 },
75 {
76 action: 'approve',
77 label: OO.ui.deferMsg( 'visualeditor-savedialog-label-review-good' ),
78 flags: [ 'safe', 'back' ],
79 modes: [ 'review', 'preview' ]
80 },
81 {
82 action: 'resolve',
83 label: OO.ui.deferMsg( 'visualeditor-savedialog-label-resolve-conflict' ),
84 flags: [ 'primary', 'progressive' ],
85 modes: 'conflict'
86 }
87 ];
88
89 /* Events */
90
91 /**
92 * Emitted when the user clicks the save button
93 *
94 * @event ve.ui.MWSaveDialog#save
95 * @param {jQuery.Deferred} saveDeferred Deferred object to resolve/reject when the save
96 * succeeds/fails.
97 */
98
99 /**
100 * Emitted when the user clicks the review changes button
101 *
102 * @event ve.ui.MWSaveDialog#review
103 */
104
105 /**
106 * Emitted when the user clicks the show preview button
107 *
108 * @event ve.ui.MWSaveDialog#preview
109 */
110
111 /**
112 * Emitted when the user clicks the resolve conflict button
113 *
114 * @event ve.ui.MWSaveDialog#resolve
115 */
116
117 /**
118 * Emitted when the user clicks the retry/continue save button after an error.
119 *
120 * @event ve.ui.MWSaveDialog#retry
121 */
122
123 /* Methods */
124
125 /**
126 * Set review content and show review panel.
127 *
128 * @param {jQuery.Promise} wikitextDiffPromise Wikitext diff HTML promise
129 * @param {jQuery.Promise} visualDiffGeneratorPromise Visual diff promise
130 * @param {HTMLDocument} [baseDoc] Base document against which to normalise links when rendering visualDiff
131 */
132 ve.ui.MWSaveDialog.prototype.setDiffAndReview = function ( wikitextDiffPromise, visualDiffGeneratorPromise, baseDoc ) {
133 this.clearDiff();
134
135 function createDiffElement( visualDiff ) {
136 const diffElement = new ve.ui.DiffElement( visualDiff );
137 // The following classes are used here:
138 // * mw-content-ltr
139 // * mw-content-rtl
140 diffElement.$document.addClass( [
141 'mw-body-content',
142 'mw-parser-output',
143 'mw-content-' + visualDiff.newDoc.getDir()
144 ] );
145 ve.targetLinksToNewWindow( diffElement.$document[ 0 ] );
146 // Run styles so links render with their appropriate classes
147 ve.init.platform.linkCache.styleParsoidElements( diffElement.$document, baseDoc );
148 mw.libs.ve.fixFragmentLinks( diffElement.$document[ 0 ], mw.Title.newFromText( ve.init.target.getPageName() ), 'mw-save-visualdiff-' );
149 return diffElement;
150 }
151
152 // Visual diff
153 this.$reviewVisualDiff.append( new OO.ui.ProgressBarWidget().$element );
154 // Don't generate the DiffElement until the tab is switched to
155 this.getDiffElementPromise = function () {
156 return visualDiffGeneratorPromise.then( ( visualDiffGenerator ) => createDiffElement( visualDiffGenerator() ) );
157 };
158
159 this.baseDoc = baseDoc;
160
161 // Wikitext diff
162 this.$reviewWikitextDiff.append( new OO.ui.ProgressBarWidget().$element );
163 wikitextDiffPromise.then( ( wikitextDiff ) => {
164 if ( wikitextDiff ) {
165 // wikitextDiff is an HTML string we trust from the API
166 // eslint-disable-next-line no-jquery/no-append-html
167 this.$reviewWikitextDiff.empty().append( wikitextDiff );
168 // Remove the HTML diff-mode ButtonGroupWidget because this.reviewModeButtonSelect replaces it.
169 // This matches what's done for action=diff in modules/ve-mw/preinit/ve.init.mw.DiffPage.init.js
170 this.$reviewWikitextDiff.find( '.ve-init-mw-diffPage-diffMode' ).empty();
171 } else {
172 this.$reviewWikitextDiff.empty().append(
173 $( '<div>' ).addClass( 've-ui-mwSaveDialog-no-changes' ).text( ve.msg( 'visualeditor-diff-no-changes' ) )
174 );
175 }
176 }, ( code, errorObject ) => {
177 const $errorMessage = ve.init.target.extractErrorMessages( errorObject );
178
179 this.$reviewWikitextDiff.empty().append(
180 new OO.ui.MessageWidget( {
181 type: 'error',
182 label: $errorMessage
183 } ).$element
184 );
185 } ).always( () => {
186 this.updateSize();
187 } );
188
189 this.hasDiff = true;
190 this.popPending();
191 this.swapPanel( 'review' );
192 };
193
194 /**
195 * Set preview content and show preview panel.
196 *
197 * @param {Object|jQuery} response action=parse API response, or error message
198 */
199 ve.ui.MWSaveDialog.prototype.showPreview = function ( response ) {
200 if ( response instanceof $ ) {
201 this.$previewViewer.empty().append(
202 // eslint-disable-next-line no-jquery/no-append-html
203 $( '<em>' ).append( response )
204 );
205 } else {
206 const data = response.parse,
207 config = mw.config.get( 'wgVisualEditor' );
208
209 mw.config.set( data.jsconfigvars );
210 mw.loader.using( ( data.modules || [] ).concat( data.modulestyles || [] ) );
211
212 // eslint-disable-next-line no-jquery/no-html
213 this.$previewHeading.html( data.displaytitle );
214 // eslint-disable-next-line no-jquery/no-append-html
215 this.$previewViewer.empty().append(
216 // The following classes are used here:
217 // * mw-content-ltr
218 // * mw-content-rtl
219 // eslint-disable-next-line no-jquery/no-html
220 $( '<div>' )
221 .addClass( 'mw-content-' + config.pageLanguageDir )
222 .attr( {
223 lang: config.pageLanguageCode,
224 dir: config.pageLanguageDir
225 } )
226 .html( data.text ),
227 data.categorieshtml
228 );
229
230 ve.targetLinksToNewWindow( this.$previewViewer[ 0 ] );
231 mw.libs.ve.fixFragmentLinks( this.$previewViewer[ 0 ], mw.Title.newFromText( ve.init.target.getPageName() ), 'mw-save-preview-' );
232
233 // Run hooks so other things can alter the document
234 mw.hook( 'wikipage.content' ).fire( this.$previewViewer );
235 }
236
237 this.popPending();
238 this.swapPanel( 'preview' );
239 };
240
241 /**
242 * @inheritdoc
243 */
244 ve.ui.MWSaveDialog.prototype.pushPending = function () {
245 this.getActions().setAbilities( { review: false, preview: false } );
246 return ve.ui.MWSaveDialog.super.prototype.pushPending.call( this );
247 };
248
249 /**
250 * @inheritdoc
251 */
252 ve.ui.MWSaveDialog.prototype.popPending = function () {
253 const ret = ve.ui.MWSaveDialog.super.prototype.popPending.call( this );
254 if ( !this.isPending() ) {
255 this.getActions().setAbilities( { review: true, preview: true } );
256 }
257 return ret;
258 };
259
260 /**
261 * Clear the diff displayed in the review panel, if any.
262 */
263 ve.ui.MWSaveDialog.prototype.clearDiff = function () {
264 this.$reviewWikitextDiff.empty();
265 this.$reviewVisualDiff.empty();
266 this.$previewViewer.empty();
267 this.hasDiff = false;
268 this.diffElement = null;
269 this.diffElementPromise = null;
270 this.getDiffElementPromise = null;
271 };
272
273 /**
274 * Swap state in the save dialog.
275 *
276 * @param {string} panel One of 'save', 'review' or 'conflict'
277 * @param {boolean} [noFocus=false] Don't attempt to focus anything (e.g. while setting up)
278 * @throws {Error} Unknown saveDialog panel
279 */
280 ve.ui.MWSaveDialog.prototype.swapPanel = function ( panel, noFocus ) {
281 if ( ( [ 'save', 'review', 'preview', 'conflict' ].indexOf( panel ) ) === -1 ) {
282 throw new Error( 'Unknown saveDialog panel: ' + panel );
283 }
284
285 const mode = panel,
286 panelObj = this[ panel + 'Panel' ];
287
288 // Update the window title
289 // The following messages are used here:
290 // * visualeditor-savedialog-title-conflict
291 // * visualeditor-savedialog-title-preview
292 // * visualeditor-savedialog-title-review
293 // * visualeditor-savedialog-title-save
294 this.title.setLabel( ve.msg( 'visualeditor-savedialog-title-' + panel ) );
295
296 // Reset save button if we disabled it for e.g. unrecoverable spam error
297 this.actions.setAbilities( { save: true } );
298
299 if ( !noFocus ) {
300 // On panels without inputs, ensure the dialog is focused so events
301 // are captured, e.g. 'Esc' to close
302 this.$content[ 0 ].focus();
303 }
304
305 let size = 'medium';
306 switch ( panel ) {
307 case 'save':
308 if ( !noFocus && this.panels.getCurrentItem() !== this.savePanel ) {
309 // HACK: FF needs *another* defer
310 setTimeout( () => {
311 this.editSummaryInput.moveCursorToEnd();
312 } );
313 }
314 break;
315 case 'conflict':
316 this.actions.setAbilities( { save: false } );
317 break;
318 case 'preview':
319 size = 'full';
320 this.previewPanel.$element[ 0 ].focus();
321 this.previewPanel.$element.prepend( this.$previewEditSummaryContainer );
322 break;
323 case 'review':
324 size = 'larger';
325 this.reviewModeButtonSelect.$element.after( this.$previewEditSummaryContainer );
326 setTimeout( () => {
327 this.updateReviewMode();
328
329 ve.track(
330 'activity.' + this.constructor.static.name,
331 { action: 'review-initial-' + this.reviewModeButtonSelect.findSelectedItem().getData() }
332 );
333 } );
334 break;
335 }
336 if ( panel === 'preview' || panel === 'review' ) {
337 const currentEditSummaryWikitext = this.editSummaryInput.getValue();
338 if ( this.lastEditSummaryWikitext === undefined || this.lastEditSummaryWikitext !== currentEditSummaryWikitext ) {
339 if ( this.editSummaryXhr ) {
340 this.editSummaryXhr.abort();
341 }
342 this.lastEditSummaryWikitext = currentEditSummaryWikitext;
343 this.$previewEditSummary.empty();
344
345 if ( !currentEditSummaryWikitext || currentEditSummaryWikitext.trim() === '' ) {
346 // Don't bother with an API request for an empty summary
347 this.$previewEditSummary.text( ve.msg( 'visualeditor-savedialog-review-nosummary' ) );
348 } else {
349 this.$previewEditSummary.parent().removeClass( 'oo-ui-element-hidden' );
350 const $spinner = $.createSpinner();
351 this.$previewEditSummary.append( $spinner );
352 this.editSummaryXhr = ve.init.target.getContentApi().post( {
353 action: 'parse',
354 title: ve.init.target.getPageName(),
355 prop: '',
356 summary: currentEditSummaryWikitext
357 } ).done( ( result ) => {
358 if ( result.parse.parsedsummary === '' ) {
359 this.$previewEditSummary.parent().addClass( 'oo-ui-element-hidden' );
360 this.$previewEditSummary.empty();
361 } else {
362 // Escape message before inserting parameter to prevent i18n XSS
363 const wrappedSummary = ve.escapeHtml( ve.msg( 'parentheses' ) )
364 .replace( '$1', () => result.parse.parsedsummary );
365 // Intentionally treated as HTML
366 // eslint-disable-next-line no-jquery/no-html
367 this.$previewEditSummary.html( wrappedSummary );
368 ve.targetLinksToNewWindow( this.$previewEditSummary[ 0 ] );
369 }
370 } ).fail( () => {
371 this.$previewEditSummary.parent().addClass( 'oo-ui-element-hidden' );
372 this.$previewEditSummary.empty();
373 } ).always( () => {
374 this.updateSize();
375 } );
376 }
377 }
378 }
379
380 // Show the target panel
381 this.panels.setItem( panelObj );
382 this.setSize( size );
383
384 // Set mode after setting size so that the footer is measured correctly
385 this.actions.setMode( mode );
386
387 // Only show preview in source mode
388 this.actions.forEach( { actions: 'preview' }, ( action ) => {
389 action.toggle( this.canPreview && panel !== 'preview' );
390 } );
391
392 // Diff API doesn't support section=new
393 this.actions.forEach( { actions: 'review' }, ( action ) => {
394 action.toggle( this.canReview && panel !== 'review' );
395 } );
396
397 // Support: iOS
398 // HACK: iOS Safari sometimes makes the entire panel completely disappear (T221289).
399 // Rebuilding it makes it reappear.
400 OO.ui.Element.static.reconsiderScrollbars( panelObj.$element[ 0 ] );
401
402 mw.hook( 've.saveDialog.stateChanged' ).fire();
403 };
404
405 /**
406 * Show a message in the save dialog.
407 *
408 * @param {string} name Message's unique name
409 * @param {jQuery|string|Function|OO.ui.HtmlSnippet} label Message content. See OO.ui.mixin.LabelElement.
410 * @param {string} config MessageWidget config. Defaults to an inline warning.
411 * @return {jQuery.Promise} Promise which resolves when the message has been shown, rejects if no new message shown.
412 */
413 ve.ui.MWSaveDialog.prototype.showMessage = function ( name, label, config ) {
414 if ( !this.messages[ name ] ) {
415 const messageWidget = new OO.ui.MessageWidget( ve.extendObject( {
416 classes: [ 've-ui-mwSaveDialog-message' ],
417 label: label,
418 inline: true,
419 type: 'warning'
420 }, config ) );
421 this.$saveMessages.append( messageWidget.$element.css( 'display', 'none' ) );
422
423 // FIXME: Use CSS transitions
424 // eslint-disable-next-line no-jquery/no-slide
425 const promise = messageWidget.$element.slideDown( {
426 duration: 250,
427 progress: this.updateSize.bind( this )
428 } ).promise();
429
430 this.swapPanel( 'save' );
431
432 // Can be hidden later by #clearMessage
433 this.messages[ name ] = messageWidget.$element;
434
435 return promise;
436 }
437 return ve.createDeferred().reject().promise();
438 };
439
440 /**
441 * Remove a message from the save dialog.
442 *
443 * @param {string} name Message's unique name
444 */
445 ve.ui.MWSaveDialog.prototype.clearMessage = function ( name ) {
446 const $message = this.messages[ name ];
447 if ( $message ) {
448 // FIXME: Use CSS transitions
449 // eslint-disable-next-line no-jquery/no-slide
450 $message.slideUp( {
451 duration: 250,
452 progress: this.updateSize.bind( this )
453 } ).promise().then( () => {
454 $message.remove();
455 this.updateSize();
456 } );
457 delete this.messages[ name ];
458 }
459 };
460
461 /**
462 * Remove all messages from the save dialog.
463 */
464 ve.ui.MWSaveDialog.prototype.clearAllMessages = function () {
465 this.$saveMessages.empty();
466 this.messages = {};
467 };
468
469 /**
470 * Reset the fields of the save dialog.
471 */
472 ve.ui.MWSaveDialog.prototype.reset = function () {
473 // Reset summary input
474 this.editSummaryInput.setValue( '' );
475 // Uncheck minoredit
476 if ( this.checkboxesByName.wpMinoredit ) {
477 this.checkboxesByName.wpMinoredit.setSelected( false );
478 }
479 this.clearDiff();
480 };
481
482 /**
483 * Initialize MediaWiki page specific checkboxes.
484 *
485 * This method is safe to call even when the dialog hasn't been initialized yet.
486 *
487 * @param {OO.ui.FieldLayout[]} checkboxFields Checkbox fields
488 */
489 ve.ui.MWSaveDialog.prototype.setupCheckboxes = function ( checkboxFields ) {
490 this.setupDeferred.done( () => {
491 checkboxFields.forEach( ( field ) => {
492 this.$saveCheckboxes.append( field.$element );
493 } );
494 this.updateOptionsBar();
495 } );
496 };
497
498 /**
499 * Change the edit summary prefilled in the save dialog.
500 *
501 * This method is safe to call even when the dialog hasn't been initialized yet.
502 *
503 * @param {string} summary Edit summary to prefill
504 */
505 ve.ui.MWSaveDialog.prototype.setEditSummary = function ( summary ) {
506 this.setupDeferred.done( () => {
507 this.editSummaryInput.setValue( summary );
508 } );
509 };
510
511 /**
512 * @inheritdoc
513 */
514 ve.ui.MWSaveDialog.prototype.initialize = function () {
515 const mwString = require( 'mediawiki.String' );
516
517 // Parent method
518 ve.ui.MWSaveDialog.super.prototype.initialize.call( this );
519
520 // Properties
521 this.panels = new OO.ui.StackLayout( { scrollable: false } );
522 this.savePanel = new OO.ui.PanelLayout( {
523 expanded: false,
524 padded: true,
525 classes: [ 've-ui-mwSaveDialog-savePanel' ]
526 } );
527
528 // Character counter in edit summary
529 this.editSummaryCountLabel = new OO.ui.LabelWidget( {
530 classes: [ 've-ui-mwSaveDialog-editSummary-count' ],
531 label: '',
532 title: ve.msg( 'visualeditor-editsummary-characters-remaining' )
533 } );
534
535 // Save panel
536 // eslint-disable-next-line no-jquery/no-html
537 this.$editSummaryLabel = $( '<div>' ).addClass( 've-ui-mwSaveDialog-summaryLabel' )
538 .html( ve.init.platform.getParsedMessage( 'summary' ) );
539 ve.targetLinksToNewWindow( this.$editSummaryLabel[ 0 ] );
540 this.editSummaryInput = new ve.ui.MWEditSummaryWidget( {
541 $overlay: this.$overlay,
542 placeholder: ve.msg( 'visualeditor-editsummary' ),
543 classes: [ 've-ui-mwSaveDialog-summary' ]
544 } );
545 // Show a warning if the user presses Enter
546 this.editSummaryInput.on( 'enter', () => {
547 this.showMessage(
548 'keyboard-shortcut-submit',
549 ve.msg(
550 'visualeditor-savedialog-keyboard-shortcut-submit',
551 new ve.ui.Trigger( ve.ui.commandHelpRegistry.lookup( 'dialogConfirm' ).shortcuts[ 0 ] ).getMessage()
552 )
553 ).then( () => {
554 // Restore focus after potential window resize
555 this.editSummaryInput.focus();
556 } );
557 } );
558 // Limit length, and display the remaining characters
559 this.editSummaryInput.$input.codePointLimit( this.editSummaryCodePointLimit );
560 this.editSummaryInput.on( 'change', () => {
561 const remaining = this.editSummaryCodePointLimit - mwString.codePointLength( this.editSummaryInput.getValue() );
562 // TODO: This looks a bit weird, there is no unit in the UI, just numbers.
563 this.changedEditSummary = true;
564 if ( remaining > 99 ) {
565 this.editSummaryCountLabel.setLabel( '' );
566 } else {
567 this.editSummaryCountLabel.setLabel( mw.language.convertNumber( remaining ) );
568 }
569
570 this.updateOptionsBar();
571 } );
572 this.editSummaryInput.on( 'resize', () => {
573 if ( this.overflowTimeout !== undefined ) {
574 clearTimeout( this.overflowTimeout );
575 }
576 this.panels.$element.css( 'overflow', 'hidden' );
577 this.updateSize();
578 this.overflowTimeout = setTimeout( () => {
579 this.panels.$element.css( 'overflow', '' );
580 }, 250 );
581 } );
582
583 this.$saveCheckboxes = $( '<div>' ).addClass( 've-ui-mwSaveDialog-checkboxes' );
584 this.$saveOptions = $( '<div>' ).addClass( 've-ui-mwSaveDialog-options' ).append(
585 this.$saveCheckboxes,
586 this.editSummaryCountLabel.$element
587 );
588 this.$license = $( '<p>' ).addClass( 've-ui-mwSaveDialog-license' );
589 this.$saveMessages = $( '<div>' ).addClass( 've-ui-mwSaveDialog-messages' );
590 this.$saveFoot = $( '<div>' ).addClass( 've-ui-mwSaveDialog-foot' ).append( this.$license );
591 ve.targetLinksToNewWindow( this.$saveFoot[ 0 ] );
592 this.savePanel.$element.append(
593 this.$editSummaryLabel,
594 this.editSummaryInput.$element,
595 this.$saveOptions,
596 this.$saveMessages,
597 this.$saveFoot
598 );
599
600 this.updateOptionsBar();
601
602 // Review panel
603 this.reviewPanel = new OO.ui.PanelLayout( {
604 expanded: false,
605 padded: true
606 } );
607
608 this.$reviewVisualDiff = $( '<div>' ).addClass( 've-ui-mwSaveDialog-viewer' );
609 this.$reviewWikitextDiff = $( '<div>' ).addClass( 've-ui-mwSaveDialog-viewer' );
610
611 this.reviewModeButtonSelect = new OO.ui.ButtonSelectWidget( {
612 items: [
613 new OO.ui.ButtonOptionWidget( { data: 'visual', icon: 'eye', label: ve.msg( 'visualeditor-savedialog-review-visual' ) } ),
614 new OO.ui.ButtonOptionWidget( { data: 'source', icon: 'wikiText', label: ve.msg( 'visualeditor-savedialog-review-wikitext' ) } )
615 ],
616 classes: [ 've-ui-mwSaveDialog-reviewMode' ]
617 } );
618 this.reviewModeButtonSelect.connect( this, {
619 choose: 'onReviewChoose',
620 select: 'updateReviewMode'
621 } );
622
623 this.$previewEditSummary = $( '<span>' ).addClass( 've-ui-mwSaveDialog-summaryPreview' ).addClass( 'comment' );
624 this.$previewEditSummaryContainer = $( '<div>' )
625 .addClass( 'mw-summary-preview' )
626 .text( ve.msg( 'summary-preview' ) )
627 .append( $( '<br>' ), this.$previewEditSummary );
628 this.$reviewActions = $( '<div>' ).addClass( 've-ui-mwSaveDialog-actions' );
629 this.reviewPanel.$element.append(
630 this.reviewModeButtonSelect.$element,
631 this.$reviewVisualDiff,
632 this.$reviewWikitextDiff,
633 this.$reviewActions
634 );
635
636 // Preview panel
637 this.previewPanel = new OO.ui.PanelLayout( {
638 classes: [ 've-ui-mwSaveDialog-preview' ],
639 expanded: false,
640 padded: true
641 } );
642 this.$previewHeading = $( '<h1>' ).addClass( 'firstHeading' );
643 this.$previewViewer = $( '<div>' ).addClass( [
644 'mw-body-content',
645 'mw-parser-output'
646 ] );
647 this.previewPanel.$element
648 // Make focusable for keyboard accessible scrolling
649 .prop( 'tabIndex', 0 )
650 .append(
651 $( '<div>' ).addClass( 'mw-content-container' ).append(
652 $( '<div>' ).addClass( 'mw-body' ).append(
653 this.$previewHeading,
654 this.$previewViewer
655 )
656 )
657 );
658
659 // Conflict panel
660 this.conflictPanel = new OO.ui.PanelLayout( {
661 expanded: false,
662 padded: true
663 } );
664 // eslint-disable-next-line no-jquery/no-html
665 this.$conflict = $( '<div>' ).addClass( 've-ui-mwSaveDialog-conflict' )
666 .html( ve.init.platform.getParsedMessage( 'visualeditor-editconflict' ) );
667 ve.targetLinksToNewWindow( this.$conflict[ 0 ] );
668 this.conflictPanel.$element.append( this.$conflict );
669
670 // Panel stack
671 this.panels.addItems( [
672 this.savePanel,
673 this.reviewPanel,
674 this.previewPanel,
675 this.conflictPanel
676 ] );
677
678 // Initialization
679 this.$body.append( this.panels.$element );
680
681 this.setupDeferred.resolve();
682 };
683
684 ve.ui.MWSaveDialog.prototype.updateOptionsBar = function () {
685 const showOptions = !!this.editSummaryCountLabel.getLabel() || !this.$saveCheckboxes.is( ':empty' );
686 if ( showOptions !== this.showOptions ) {
687 this.savePanel.$element.toggleClass( 've-ui-mwSaveDialog-withOptions', showOptions );
688 this.showOptions = showOptions;
689 this.updateSize();
690 }
691 };
692
693 /**
694 * Update the current review mode
695 *
696 * @param {OO.ui.ButtonOptionWidget} [button] The button clicked, or false if this is the initial setup
697 */
698 ve.ui.MWSaveDialog.prototype.updateReviewMode = function () {
699 if ( !this.hasDiff ) {
700 return;
701 }
702
703 const diffMode = this.reviewModeButtonSelect.findSelectedItem().getData(),
704 surfaceMode = ve.init.target.getSurface().getMode(),
705 isVisual = diffMode === 'visual';
706
707 // Config values used here:
708 // * visualeditor-diffmode-visual
709 // * visualeditor-diffmode-source
710 ve.userConfig( 'visualeditor-diffmode-' + surfaceMode, diffMode );
711
712 this.$reviewVisualDiff.toggleClass( 'oo-ui-element-hidden', !isVisual );
713 this.$reviewWikitextDiff.toggleClass( 'oo-ui-element-hidden', isVisual );
714 if ( isVisual ) {
715 if ( !this.diffElement ) {
716 if ( !this.diffElementPromise ) {
717 this.diffElementPromise = this.getDiffElementPromise().then( ( diffElement ) => {
718 this.diffElement = diffElement;
719 this.$reviewVisualDiff.empty().append( diffElement.$element );
720 this.positionDiffElement();
721 } );
722 }
723 return;
724 }
725 this.positionDiffElement();
726 }
727
728 // Support: iOS
729 // HACK: iOS Safari sometimes makes the entire panel completely disappear (T219680).
730 // Rebuilding it makes it reappear.
731 OO.ui.Element.static.reconsiderScrollbars( this.reviewPanel.$element[ 0 ] );
732
733 this.updateSize();
734 };
735
736 /**
737 * Update the current review mode
738 *
739 * @param {OO.ui.OptionWidget} item Item chosen
740 */
741 ve.ui.MWSaveDialog.prototype.onReviewChoose = function ( item ) {
742 ve.track( 'activity.' + this.constructor.static.name, { action: 'review-switch-' + item.getData() } );
743 };
744
745 /**
746 * @inheritdoc
747 */
748 ve.ui.MWSaveDialog.prototype.setDimensions = function () {
749 // Parent method
750 ve.ui.MWSaveDialog.super.prototype.setDimensions.apply( this, arguments );
751
752 if ( !this.positioning ) {
753 this.positionDiffElement();
754 }
755 };
756
757 /**
758 * Re-position elements within the diff element
759 *
760 * Should be called whenever the diff element's container has changed width.
761 */
762 ve.ui.MWSaveDialog.prototype.positionDiffElement = function () {
763 if ( this.panels.getCurrentItem() === this.reviewPanel ) {
764 setTimeout( () => {
765 this.withoutSizeTransitions( () => {
766 // This is delayed, so check the visual diff is still visible
767 if ( this.diffElement && this.isVisible() && this.reviewModeButtonSelect.findSelectedItem().getData() === 'visual' ) {
768 this.diffElement.positionDescriptions();
769 this.positioning = true;
770 this.updateSize();
771 this.positioning = false;
772 }
773 } );
774 }, OO.ui.theme.getDialogTransitionDuration() );
775 }
776 };
777
778 /**
779 * @inheritdoc
780 * @param {Object} [data]
781 * @param {boolean} [data.canReview=false] User can review changes
782 * @param {boolean} [data.canPreview=false] User can preview changes
783 * @param {string} [data.copyrightWarning] HTML to display as the copyright message
784 * @param {OO.ui.FieldLayout[]} [data.checkboxFields=[]] Checkbox fields
785 * @param {Object} [data.checkboxesByName={}] Checkbox widgets, indexed by name
786 * @param {string} [data.sectionTitle] Section title, if in new section mode
787 * @param {string} [data.editSummary] Edit summary
788 * @param {string} [data.initialPanel='save'] Initial panel to show
789 * @param {jQuery|string|OO.ui.HtmlSnippet|Function|null} [data.saveButtonLabel] Label for the save button
790 */
791 ve.ui.MWSaveDialog.prototype.getSetupProcess = function ( data ) {
792 return ve.ui.MWSaveDialog.super.prototype.getSetupProcess.call( this, data )
793 .next( () => {
794 const surfaceMode = ve.init.target.getSurface().getMode();
795
796 this.canReview = !!data.canReview;
797 this.canPreview = !!data.canPreview;
798 // eslint-disable-next-line no-jquery/no-html
799 this.$license.html( data.copyrightWarning );
800 this.setupCheckboxes( data.checkboxFields || [] );
801 this.checkboxesByName = data.checkboxesByName || {};
802 // HACK: Change layout when wpWatchlistExpiry is present to force wpWatchthis
803 // onto a new line, hopefully with the expiry dropdown
804 this.$saveCheckboxes.toggleClass( 've-ui-mwSaveDialog-checkboxes-withExpiry', !!this.checkboxesByName.wpWatchlistExpiry );
805 // Toggle the watchlist-expiry dropdown's disabled state according to the
806 // selected state of the watchthis checkbox.
807 if ( this.checkboxesByName.wpWatchthis && this.checkboxesByName.wpWatchlistExpiry ) {
808 // Set initial state to match the watchthis checkbox.
809 this.checkboxesByName.wpWatchlistExpiry.setDisabled( !this.checkboxesByName.wpWatchthis.isSelected() );
810 // Change state on every change of the watchthis checkbox.
811 this.checkboxesByName.wpWatchthis.on( 'change', ( enabled ) => {
812 this.checkboxesByName.wpWatchlistExpiry.setDisabled( !enabled );
813 } );
814 }
815
816 function trackCheckbox( n ) {
817 ve.track( 'activity.mwSave', { action: 'checkbox-' + n } );
818 }
819 for ( const name in this.checkboxesByName ) {
820 this.checkboxesByName[ name ].$element.off( '.mwSave' ).on( 'click.mwSave', trackCheckbox.bind( this, name ) );
821 }
822
823 if ( data.sectionTitle ) {
824 this.setEditSummary( ve.msg( 'newsectionsummary', data.sectionTitle ) );
825 this.editSummaryInput.setDisabled( true );
826 } else {
827 this.editSummaryInput.setDisabled( false );
828 if ( !this.changedEditSummary ) {
829 this.setEditSummary( data.editSummary );
830 }
831 }
832
833 // Config values used here:
834 // * visualeditor-diffmode-visual
835 // * visualeditor-diffmode-source
836 this.reviewModeButtonSelect.selectItemByData(
837 ve.userConfig( 'visualeditor-diffmode-' + surfaceMode ) || surfaceMode
838 );
839
840 // Old messages should not persist
841 this.clearAllMessages();
842 // Don't focus during setup to prevent scroll jumping (T153010)
843 this.swapPanel( data.initialPanel || 'save', true );
844 // Update save button label
845 this.actions.forEach( { actions: 'save' }, ( action ) => {
846 action.setLabel( data.saveButtonLabel );
847 } );
848 } );
849 };
850
851 /**
852 * @inheritdoc
853 */
854 ve.ui.MWSaveDialog.prototype.getReadyProcess = function ( data ) {
855 return ve.ui.MWSaveDialog.super.prototype.getReadyProcess.call( this, data )
856 .next( () => {
857 // HACK: iOS Safari sometimes makes the entire panel completely disappear (T221289).
858 // Rebuilding it makes it reappear.
859 OO.ui.Element.static.reconsiderScrollbars( this.panels.getCurrentItem().$element[ 0 ] );
860
861 // Support: Firefox
862 // In Firefox, trying to focus a hidden input will throw an
863 // exception. This would happen when opening the preview via
864 // keyboard shortcut.
865 if ( this.panels.getCurrentItem() === this.savePanel ) {
866 // This includes a #focus call
867 this.editSummaryInput.moveCursorToEnd();
868 }
869 } );
870 };
871
872 /**
873 * @inheritdoc
874 */
875 ve.ui.MWSaveDialog.prototype.getTeardownProcess = function ( data ) {
876 return ve.ui.MWSaveDialog.super.prototype.getTeardownProcess.call( this, data )
877 .next( () => {
878 this.emit( 'close' );
879 } );
880 };
881
882 /**
883 * @inheritdoc
884 */
885 ve.ui.MWSaveDialog.prototype.getActionProcess = function ( action ) {
886 ve.track( 'activity.' + this.constructor.static.name, { action: 'dialog-' + ( action || 'abort' ) } );
887
888 if ( action === 'save' ) {
889 return new OO.ui.Process( () => {
890 const saveDeferred = ve.createDeferred();
891 this.clearMessage( 'keyboard-shortcut-submit' );
892 this.emit( 'save', saveDeferred );
893 return saveDeferred.promise();
894 } );
895 }
896 if ( action === 'review' || action === 'preview' || action === 'resolve' ) {
897 return new OO.ui.Process( () => {
898 this.emit( action );
899 } );
900 }
901 if ( action === 'approve' ) {
902 return new OO.ui.Process( () => {
903 this.swapPanel( 'save' );
904 } );
905 }
906
907 return ve.ui.MWSaveDialog.super.prototype.getActionProcess.call( this, action );
908 };
909
910 /**
911 * @inheritdoc
912 */
913 ve.ui.MWSaveDialog.prototype.getBodyHeight = function () {
914 // Don't vary the height when the foot is made visible or not
915 return this.panels.getCurrentItem().$element.outerHeight( true );
916 };
917
918 /**
919 * Handle retry button click events.
920 *
921 * Hides errors and then tries again.
922 */
923 ve.ui.MWSaveDialog.prototype.onRetryButtonClick = function () {
924 this.emit( 'retry' );
925 ve.ui.MWSaveDialog.super.prototype.onRetryButtonClick.apply( this, arguments );
926 };
927
928 /* Registration */
929
930 ve.ui.windowFactory.register( ve.ui.MWSaveDialog );