Merge "Add part to update ctd_user_defined in populateChangeTagDef"
[lhc/web/wiklou.git] / resources / lib / ooui / oojs-ui-windows.js
1 /*!
2 * OOUI v0.28.2
3 * https://www.mediawiki.org/wiki/OOUI
4 *
5 * Copyright 2011–2018 OOUI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
8 *
9 * Date: 2018-09-11T23:05:15Z
10 */
11 ( function ( OO ) {
12
13 'use strict';
14
15 /**
16 * An ActionWidget is a {@link OO.ui.ButtonWidget button widget} that executes an action.
17 * Action widgets are used with OO.ui.ActionSet, which manages the behavior and availability
18 * of the actions.
19 *
20 * Both actions and action sets are primarily used with {@link OO.ui.Dialog Dialogs}.
21 * Please see the [OOUI documentation on MediaWiki] [1] for more information
22 * and examples.
23 *
24 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Action_sets
25 *
26 * @class
27 * @extends OO.ui.ButtonWidget
28 * @mixins OO.ui.mixin.PendingElement
29 *
30 * @constructor
31 * @param {Object} [config] Configuration options
32 * @cfg {string} [action] Symbolic name of the action (e.g., ‘continue’ or ‘cancel’).
33 * @cfg {string[]} [modes] Symbolic names of the modes (e.g., ‘edit’ or ‘read’) in which the action
34 * should be made available. See the action set's {@link OO.ui.ActionSet#setMode setMode} method
35 * for more information about setting modes.
36 * @cfg {boolean} [framed=false] Render the action button with a frame
37 */
38 OO.ui.ActionWidget = function OoUiActionWidget( config ) {
39 // Configuration initialization
40 config = $.extend( { framed: false }, config );
41
42 // Parent constructor
43 OO.ui.ActionWidget.parent.call( this, config );
44
45 // Mixin constructors
46 OO.ui.mixin.PendingElement.call( this, config );
47
48 // Properties
49 this.action = config.action || '';
50 this.modes = config.modes || [];
51 this.width = 0;
52 this.height = 0;
53
54 // Initialization
55 this.$element.addClass( 'oo-ui-actionWidget' );
56 };
57
58 /* Setup */
59
60 OO.inheritClass( OO.ui.ActionWidget, OO.ui.ButtonWidget );
61 OO.mixinClass( OO.ui.ActionWidget, OO.ui.mixin.PendingElement );
62
63 /* Methods */
64
65 /**
66 * Check if the action is configured to be available in the specified `mode`.
67 *
68 * @param {string} mode Name of mode
69 * @return {boolean} The action is configured with the mode
70 */
71 OO.ui.ActionWidget.prototype.hasMode = function ( mode ) {
72 return this.modes.indexOf( mode ) !== -1;
73 };
74
75 /**
76 * Get the symbolic name of the action (e.g., ‘continue’ or ‘cancel’).
77 *
78 * @return {string}
79 */
80 OO.ui.ActionWidget.prototype.getAction = function () {
81 return this.action;
82 };
83
84 /**
85 * Get the symbolic name of the mode or modes for which the action is configured to be available.
86 *
87 * The current mode is set with the action set's {@link OO.ui.ActionSet#setMode setMode} method.
88 * Only actions that are configured to be avaiable in the current mode will be visible. All other actions
89 * are hidden.
90 *
91 * @return {string[]}
92 */
93 OO.ui.ActionWidget.prototype.getModes = function () {
94 return this.modes.slice();
95 };
96
97 /* eslint-disable no-unused-vars */
98 /**
99 * ActionSets manage the behavior of the {@link OO.ui.ActionWidget action widgets} that comprise them.
100 * Actions can be made available for specific contexts (modes) and circumstances
101 * (abilities). Action sets are primarily used with {@link OO.ui.Dialog Dialogs}.
102 *
103 * ActionSets contain two types of actions:
104 *
105 * - Special: Special actions are the first visible actions with special flags, such as 'safe' and 'primary', the default special flags. Additional special flags can be configured in subclasses with the static #specialFlags property.
106 * - Other: Other actions include all non-special visible actions.
107 *
108 * See the [OOUI documentation on MediaWiki][1] for more information.
109 *
110 * @example
111 * // Example: An action set used in a process dialog
112 * function MyProcessDialog( config ) {
113 * MyProcessDialog.parent.call( this, config );
114 * }
115 * OO.inheritClass( MyProcessDialog, OO.ui.ProcessDialog );
116 * MyProcessDialog.static.title = 'An action set in a process dialog';
117 * MyProcessDialog.static.name = 'myProcessDialog';
118 * // An action set that uses modes ('edit' and 'help' mode, in this example).
119 * MyProcessDialog.static.actions = [
120 * { action: 'continue', modes: 'edit', label: 'Continue', flags: [ 'primary', 'progressive' ] },
121 * { action: 'help', modes: 'edit', label: 'Help' },
122 * { modes: 'edit', label: 'Cancel', flags: 'safe' },
123 * { action: 'back', modes: 'help', label: 'Back', flags: 'safe' }
124 * ];
125 *
126 * MyProcessDialog.prototype.initialize = function () {
127 * MyProcessDialog.parent.prototype.initialize.apply( this, arguments );
128 * this.panel1 = new OO.ui.PanelLayout( { padded: true, expanded: false } );
129 * this.panel1.$element.append( '<p>This dialog uses an action set (continue, help, cancel, back) configured with modes. This is edit mode. Click \'help\' to see help mode.</p>' );
130 * this.panel2 = new OO.ui.PanelLayout( { padded: true, expanded: false } );
131 * this.panel2.$element.append( '<p>This is help mode. Only the \'back\' action widget is configured to be visible here. Click \'back\' to return to \'edit\' mode.</p>' );
132 * this.stackLayout = new OO.ui.StackLayout( {
133 * items: [ this.panel1, this.panel2 ]
134 * } );
135 * this.$body.append( this.stackLayout.$element );
136 * };
137 * MyProcessDialog.prototype.getSetupProcess = function ( data ) {
138 * return MyProcessDialog.parent.prototype.getSetupProcess.call( this, data )
139 * .next( function () {
140 * this.actions.setMode( 'edit' );
141 * }, this );
142 * };
143 * MyProcessDialog.prototype.getActionProcess = function ( action ) {
144 * if ( action === 'help' ) {
145 * this.actions.setMode( 'help' );
146 * this.stackLayout.setItem( this.panel2 );
147 * } else if ( action === 'back' ) {
148 * this.actions.setMode( 'edit' );
149 * this.stackLayout.setItem( this.panel1 );
150 * } else if ( action === 'continue' ) {
151 * var dialog = this;
152 * return new OO.ui.Process( function () {
153 * dialog.close();
154 * } );
155 * }
156 * return MyProcessDialog.parent.prototype.getActionProcess.call( this, action );
157 * };
158 * MyProcessDialog.prototype.getBodyHeight = function () {
159 * return this.panel1.$element.outerHeight( true );
160 * };
161 * var windowManager = new OO.ui.WindowManager();
162 * $( 'body' ).append( windowManager.$element );
163 * var dialog = new MyProcessDialog( {
164 * size: 'medium'
165 * } );
166 * windowManager.addWindows( [ dialog ] );
167 * windowManager.openWindow( dialog );
168 *
169 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Action_sets
170 *
171 * @abstract
172 * @class
173 * @mixins OO.EventEmitter
174 *
175 * @constructor
176 * @param {Object} [config] Configuration options
177 */
178 OO.ui.ActionSet = function OoUiActionSet( config ) {
179 // Configuration initialization
180 config = config || {};
181
182 // Mixin constructors
183 OO.EventEmitter.call( this );
184
185 // Properties
186 this.list = [];
187 this.categories = {
188 actions: 'getAction',
189 flags: 'getFlags',
190 modes: 'getModes'
191 };
192 this.categorized = {};
193 this.special = {};
194 this.others = [];
195 this.organized = false;
196 this.changing = false;
197 this.changed = false;
198 };
199 /* eslint-enable no-unused-vars */
200
201 /* Setup */
202
203 OO.mixinClass( OO.ui.ActionSet, OO.EventEmitter );
204
205 /* Static Properties */
206
207 /**
208 * Symbolic name of the flags used to identify special actions. Special actions are displayed in the
209 * header of a {@link OO.ui.ProcessDialog process dialog}.
210 * See the [OOUI documentation on MediaWiki][2] for more information and examples.
211 *
212 * [2]:https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs
213 *
214 * @abstract
215 * @static
216 * @inheritable
217 * @property {string}
218 */
219 OO.ui.ActionSet.static.specialFlags = [ 'safe', 'primary' ];
220
221 /* Events */
222
223 /**
224 * @event click
225 *
226 * A 'click' event is emitted when an action is clicked.
227 *
228 * @param {OO.ui.ActionWidget} action Action that was clicked
229 */
230
231 /**
232 * @event add
233 *
234 * An 'add' event is emitted when actions are {@link #method-add added} to the action set.
235 *
236 * @param {OO.ui.ActionWidget[]} added Actions added
237 */
238
239 /**
240 * @event remove
241 *
242 * A 'remove' event is emitted when actions are {@link #method-remove removed}
243 * or {@link #clear cleared}.
244 *
245 * @param {OO.ui.ActionWidget[]} added Actions removed
246 */
247
248 /**
249 * @event change
250 *
251 * A 'change' event is emitted when actions are {@link #method-add added}, {@link #clear cleared},
252 * or {@link #method-remove removed} from the action set or when the {@link #setMode mode} is changed.
253 *
254 */
255
256 /* Methods */
257
258 /**
259 * Handle action change events.
260 *
261 * @private
262 * @fires change
263 */
264 OO.ui.ActionSet.prototype.onActionChange = function () {
265 this.organized = false;
266 if ( this.changing ) {
267 this.changed = true;
268 } else {
269 this.emit( 'change' );
270 }
271 };
272
273 /**
274 * Check if an action is one of the special actions.
275 *
276 * @param {OO.ui.ActionWidget} action Action to check
277 * @return {boolean} Action is special
278 */
279 OO.ui.ActionSet.prototype.isSpecial = function ( action ) {
280 var flag;
281
282 for ( flag in this.special ) {
283 if ( action === this.special[ flag ] ) {
284 return true;
285 }
286 }
287
288 return false;
289 };
290
291 /**
292 * Get action widgets based on the specified filter: ‘actions’, ‘flags’, ‘modes’, ‘visible’,
293 * or ‘disabled’.
294 *
295 * @param {Object} [filters] Filters to use, omit to get all actions
296 * @param {string|string[]} [filters.actions] Actions that action widgets must have
297 * @param {string|string[]} [filters.flags] Flags that action widgets must have (e.g., 'safe')
298 * @param {string|string[]} [filters.modes] Modes that action widgets must have
299 * @param {boolean} [filters.visible] Action widgets must be visible
300 * @param {boolean} [filters.disabled] Action widgets must be disabled
301 * @return {OO.ui.ActionWidget[]} Action widgets matching all criteria
302 */
303 OO.ui.ActionSet.prototype.get = function ( filters ) {
304 var i, len, list, category, actions, index, match, matches;
305
306 if ( filters ) {
307 this.organize();
308
309 // Collect category candidates
310 matches = [];
311 for ( category in this.categorized ) {
312 list = filters[ category ];
313 if ( list ) {
314 if ( !Array.isArray( list ) ) {
315 list = [ list ];
316 }
317 for ( i = 0, len = list.length; i < len; i++ ) {
318 actions = this.categorized[ category ][ list[ i ] ];
319 if ( Array.isArray( actions ) ) {
320 matches.push.apply( matches, actions );
321 }
322 }
323 }
324 }
325 // Remove by boolean filters
326 for ( i = 0, len = matches.length; i < len; i++ ) {
327 match = matches[ i ];
328 if (
329 ( filters.visible !== undefined && match.isVisible() !== filters.visible ) ||
330 ( filters.disabled !== undefined && match.isDisabled() !== filters.disabled )
331 ) {
332 matches.splice( i, 1 );
333 len--;
334 i--;
335 }
336 }
337 // Remove duplicates
338 for ( i = 0, len = matches.length; i < len; i++ ) {
339 match = matches[ i ];
340 index = matches.lastIndexOf( match );
341 while ( index !== i ) {
342 matches.splice( index, 1 );
343 len--;
344 index = matches.lastIndexOf( match );
345 }
346 }
347 return matches;
348 }
349 return this.list.slice();
350 };
351
352 /**
353 * Get 'special' actions.
354 *
355 * Special actions are the first visible action widgets with special flags, such as 'safe' and 'primary'.
356 * Special flags can be configured in subclasses by changing the static #specialFlags property.
357 *
358 * @return {OO.ui.ActionWidget[]|null} 'Special' action widgets.
359 */
360 OO.ui.ActionSet.prototype.getSpecial = function () {
361 this.organize();
362 return $.extend( {}, this.special );
363 };
364
365 /**
366 * Get 'other' actions.
367 *
368 * Other actions include all non-special visible action widgets.
369 *
370 * @return {OO.ui.ActionWidget[]} 'Other' action widgets
371 */
372 OO.ui.ActionSet.prototype.getOthers = function () {
373 this.organize();
374 return this.others.slice();
375 };
376
377 /**
378 * Set the mode (e.g., ‘edit’ or ‘view’). Only {@link OO.ui.ActionWidget#modes actions} configured
379 * to be available in the specified mode will be made visible. All other actions will be hidden.
380 *
381 * @param {string} mode The mode. Only actions configured to be available in the specified
382 * mode will be made visible.
383 * @chainable
384 * @fires toggle
385 * @fires change
386 */
387 OO.ui.ActionSet.prototype.setMode = function ( mode ) {
388 var i, len, action;
389
390 this.changing = true;
391 for ( i = 0, len = this.list.length; i < len; i++ ) {
392 action = this.list[ i ];
393 action.toggle( action.hasMode( mode ) );
394 }
395
396 this.organized = false;
397 this.changing = false;
398 this.emit( 'change' );
399
400 return this;
401 };
402
403 /**
404 * Set the abilities of the specified actions.
405 *
406 * Action widgets that are configured with the specified actions will be enabled
407 * or disabled based on the boolean values specified in the `actions`
408 * parameter.
409 *
410 * @param {Object.<string,boolean>} actions A list keyed by action name with boolean
411 * values that indicate whether or not the action should be enabled.
412 * @chainable
413 */
414 OO.ui.ActionSet.prototype.setAbilities = function ( actions ) {
415 var i, len, action, item;
416
417 for ( i = 0, len = this.list.length; i < len; i++ ) {
418 item = this.list[ i ];
419 action = item.getAction();
420 if ( actions[ action ] !== undefined ) {
421 item.setDisabled( !actions[ action ] );
422 }
423 }
424
425 return this;
426 };
427
428 /**
429 * Executes a function once per action.
430 *
431 * When making changes to multiple actions, use this method instead of iterating over the actions
432 * manually to defer emitting a #change event until after all actions have been changed.
433 *
434 * @param {Object|null} filter Filters to use to determine which actions to iterate over; see #get
435 * @param {Function} callback Callback to run for each action; callback is invoked with three
436 * arguments: the action, the action's index, the list of actions being iterated over
437 * @chainable
438 */
439 OO.ui.ActionSet.prototype.forEach = function ( filter, callback ) {
440 this.changed = false;
441 this.changing = true;
442 this.get( filter ).forEach( callback );
443 this.changing = false;
444 if ( this.changed ) {
445 this.emit( 'change' );
446 }
447
448 return this;
449 };
450
451 /**
452 * Add action widgets to the action set.
453 *
454 * @param {OO.ui.ActionWidget[]} actions Action widgets to add
455 * @chainable
456 * @fires add
457 * @fires change
458 */
459 OO.ui.ActionSet.prototype.add = function ( actions ) {
460 var i, len, action;
461
462 this.changing = true;
463 for ( i = 0, len = actions.length; i < len; i++ ) {
464 action = actions[ i ];
465 action.connect( this, {
466 click: [ 'emit', 'click', action ],
467 toggle: [ 'onActionChange' ]
468 } );
469 this.list.push( action );
470 }
471 this.organized = false;
472 this.emit( 'add', actions );
473 this.changing = false;
474 this.emit( 'change' );
475
476 return this;
477 };
478
479 /**
480 * Remove action widgets from the set.
481 *
482 * To remove all actions, you may wish to use the #clear method instead.
483 *
484 * @param {OO.ui.ActionWidget[]} actions Action widgets to remove
485 * @chainable
486 * @fires remove
487 * @fires change
488 */
489 OO.ui.ActionSet.prototype.remove = function ( actions ) {
490 var i, len, index, action;
491
492 this.changing = true;
493 for ( i = 0, len = actions.length; i < len; i++ ) {
494 action = actions[ i ];
495 index = this.list.indexOf( action );
496 if ( index !== -1 ) {
497 action.disconnect( this );
498 this.list.splice( index, 1 );
499 }
500 }
501 this.organized = false;
502 this.emit( 'remove', actions );
503 this.changing = false;
504 this.emit( 'change' );
505
506 return this;
507 };
508
509 /**
510 * Remove all action widets from the set.
511 *
512 * To remove only specified actions, use the {@link #method-remove remove} method instead.
513 *
514 * @chainable
515 * @fires remove
516 * @fires change
517 */
518 OO.ui.ActionSet.prototype.clear = function () {
519 var i, len, action,
520 removed = this.list.slice();
521
522 this.changing = true;
523 for ( i = 0, len = this.list.length; i < len; i++ ) {
524 action = this.list[ i ];
525 action.disconnect( this );
526 }
527
528 this.list = [];
529
530 this.organized = false;
531 this.emit( 'remove', removed );
532 this.changing = false;
533 this.emit( 'change' );
534
535 return this;
536 };
537
538 /**
539 * Organize actions.
540 *
541 * This is called whenever organized information is requested. It will only reorganize the actions
542 * if something has changed since the last time it ran.
543 *
544 * @private
545 * @chainable
546 */
547 OO.ui.ActionSet.prototype.organize = function () {
548 var i, iLen, j, jLen, flag, action, category, list, item, special,
549 specialFlags = this.constructor.static.specialFlags;
550
551 if ( !this.organized ) {
552 this.categorized = {};
553 this.special = {};
554 this.others = [];
555 for ( i = 0, iLen = this.list.length; i < iLen; i++ ) {
556 action = this.list[ i ];
557 if ( action.isVisible() ) {
558 // Populate categories
559 for ( category in this.categories ) {
560 if ( !this.categorized[ category ] ) {
561 this.categorized[ category ] = {};
562 }
563 list = action[ this.categories[ category ] ]();
564 if ( !Array.isArray( list ) ) {
565 list = [ list ];
566 }
567 for ( j = 0, jLen = list.length; j < jLen; j++ ) {
568 item = list[ j ];
569 if ( !this.categorized[ category ][ item ] ) {
570 this.categorized[ category ][ item ] = [];
571 }
572 this.categorized[ category ][ item ].push( action );
573 }
574 }
575 // Populate special/others
576 special = false;
577 for ( j = 0, jLen = specialFlags.length; j < jLen; j++ ) {
578 flag = specialFlags[ j ];
579 if ( !this.special[ flag ] && action.hasFlag( flag ) ) {
580 this.special[ flag ] = action;
581 special = true;
582 break;
583 }
584 }
585 if ( !special ) {
586 this.others.push( action );
587 }
588 }
589 }
590 this.organized = true;
591 }
592
593 return this;
594 };
595
596 /**
597 * Errors contain a required message (either a string or jQuery selection) that is used to describe what went wrong
598 * in a {@link OO.ui.Process process}. The error's #recoverable and #warning configurations are used to customize the
599 * appearance and functionality of the error interface.
600 *
601 * The basic error interface contains a formatted error message as well as two buttons: 'Dismiss' and 'Try again' (i.e., the error
602 * is 'recoverable' by default). If the error is not recoverable, the 'Try again' button will not be rendered and the widget
603 * that initiated the failed process will be disabled.
604 *
605 * If the error is a warning, the error interface will include a 'Dismiss' and a 'Continue' button, which will try the
606 * process again.
607 *
608 * For an example of error interfaces, please see the [OOUI documentation on MediaWiki][1].
609 *
610 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Processes_and_errors
611 *
612 * @class
613 *
614 * @constructor
615 * @param {string|jQuery} message Description of error
616 * @param {Object} [config] Configuration options
617 * @cfg {boolean} [recoverable=true] Error is recoverable.
618 * By default, errors are recoverable, and users can try the process again.
619 * @cfg {boolean} [warning=false] Error is a warning.
620 * If the error is a warning, the error interface will include a
621 * 'Dismiss' and a 'Continue' button. It is the responsibility of the developer to ensure that the warning
622 * is not triggered a second time if the user chooses to continue.
623 */
624 OO.ui.Error = function OoUiError( message, config ) {
625 // Allow passing positional parameters inside the config object
626 if ( OO.isPlainObject( message ) && config === undefined ) {
627 config = message;
628 message = config.message;
629 }
630
631 // Configuration initialization
632 config = config || {};
633
634 // Properties
635 this.message = message instanceof jQuery ? message : String( message );
636 this.recoverable = config.recoverable === undefined || !!config.recoverable;
637 this.warning = !!config.warning;
638 };
639
640 /* Setup */
641
642 OO.initClass( OO.ui.Error );
643
644 /* Methods */
645
646 /**
647 * Check if the error is recoverable.
648 *
649 * If the error is recoverable, users are able to try the process again.
650 *
651 * @return {boolean} Error is recoverable
652 */
653 OO.ui.Error.prototype.isRecoverable = function () {
654 return this.recoverable;
655 };
656
657 /**
658 * Check if the error is a warning.
659 *
660 * If the error is a warning, the error interface will include a 'Dismiss' and a 'Continue' button.
661 *
662 * @return {boolean} Error is warning
663 */
664 OO.ui.Error.prototype.isWarning = function () {
665 return this.warning;
666 };
667
668 /**
669 * Get error message as DOM nodes.
670 *
671 * @return {jQuery} Error message in DOM nodes
672 */
673 OO.ui.Error.prototype.getMessage = function () {
674 return this.message instanceof jQuery ?
675 this.message.clone() :
676 $( '<div>' ).text( this.message ).contents();
677 };
678
679 /**
680 * Get the error message text.
681 *
682 * @return {string} Error message
683 */
684 OO.ui.Error.prototype.getMessageText = function () {
685 return this.message instanceof jQuery ? this.message.text() : this.message;
686 };
687
688 /**
689 * A Process is a list of steps that are called in sequence. The step can be a number, a jQuery promise,
690 * or a function:
691 *
692 * - **number**: the process will wait for the specified number of milliseconds before proceeding.
693 * - **promise**: the process will continue to the next step when the promise is successfully resolved
694 * or stop if the promise is rejected.
695 * - **function**: the process will execute the function. The process will stop if the function returns
696 * either a boolean `false` or a promise that is rejected; if the function returns a number, the process
697 * will wait for that number of milliseconds before proceeding.
698 *
699 * If the process fails, an {@link OO.ui.Error error} is generated. Depending on how the error is
700 * configured, users can dismiss the error and try the process again, or not. If a process is stopped,
701 * its remaining steps will not be performed.
702 *
703 * @class
704 *
705 * @constructor
706 * @param {number|jQuery.Promise|Function} step Number of miliseconds to wait before proceeding, promise
707 * that must be resolved before proceeding, or a function to execute. See #createStep for more information. see #createStep for more information
708 * @param {Object} [context=null] Execution context of the function. The context is ignored if the step is
709 * a number or promise.
710 */
711 OO.ui.Process = function ( step, context ) {
712 // Properties
713 this.steps = [];
714
715 // Initialization
716 if ( step !== undefined ) {
717 this.next( step, context );
718 }
719 };
720
721 /* Setup */
722
723 OO.initClass( OO.ui.Process );
724
725 /* Methods */
726
727 /**
728 * Start the process.
729 *
730 * @return {jQuery.Promise} Promise that is resolved when all steps have successfully completed.
731 * If any of the steps return a promise that is rejected or a boolean false, this promise is rejected
732 * and any remaining steps are not performed.
733 */
734 OO.ui.Process.prototype.execute = function () {
735 var i, len, promise;
736
737 /**
738 * Continue execution.
739 *
740 * @ignore
741 * @param {Array} step A function and the context it should be called in
742 * @return {Function} Function that continues the process
743 */
744 function proceed( step ) {
745 return function () {
746 // Execute step in the correct context
747 var deferred,
748 result = step.callback.call( step.context );
749
750 if ( result === false ) {
751 // Use rejected promise for boolean false results
752 return $.Deferred().reject( [] ).promise();
753 }
754 if ( typeof result === 'number' ) {
755 if ( result < 0 ) {
756 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
757 }
758 // Use a delayed promise for numbers, expecting them to be in milliseconds
759 deferred = $.Deferred();
760 setTimeout( deferred.resolve, result );
761 return deferred.promise();
762 }
763 if ( result instanceof OO.ui.Error ) {
764 // Use rejected promise for error
765 return $.Deferred().reject( [ result ] ).promise();
766 }
767 if ( Array.isArray( result ) && result.length && result[ 0 ] instanceof OO.ui.Error ) {
768 // Use rejected promise for list of errors
769 return $.Deferred().reject( result ).promise();
770 }
771 // Duck-type the object to see if it can produce a promise
772 if ( result && $.isFunction( result.promise ) ) {
773 // Use a promise generated from the result
774 return result.promise();
775 }
776 // Use resolved promise for other results
777 return $.Deferred().resolve().promise();
778 };
779 }
780
781 if ( this.steps.length ) {
782 // Generate a chain reaction of promises
783 promise = proceed( this.steps[ 0 ] )();
784 for ( i = 1, len = this.steps.length; i < len; i++ ) {
785 promise = promise.then( proceed( this.steps[ i ] ) );
786 }
787 } else {
788 promise = $.Deferred().resolve().promise();
789 }
790
791 return promise;
792 };
793
794 /**
795 * Create a process step.
796 *
797 * @private
798 * @param {number|jQuery.Promise|Function} step
799 *
800 * - Number of milliseconds to wait before proceeding
801 * - Promise that must be resolved before proceeding
802 * - Function to execute
803 * - If the function returns a boolean false the process will stop
804 * - If the function returns a promise, the process will continue to the next
805 * step when the promise is resolved or stop if the promise is rejected
806 * - If the function returns a number, the process will wait for that number of
807 * milliseconds before proceeding
808 * @param {Object} [context=null] Execution context of the function. The context is
809 * ignored if the step is a number or promise.
810 * @return {Object} Step object, with `callback` and `context` properties
811 */
812 OO.ui.Process.prototype.createStep = function ( step, context ) {
813 if ( typeof step === 'number' || $.isFunction( step.promise ) ) {
814 return {
815 callback: function () {
816 return step;
817 },
818 context: null
819 };
820 }
821 if ( $.isFunction( step ) ) {
822 return {
823 callback: step,
824 context: context
825 };
826 }
827 throw new Error( 'Cannot create process step: number, promise or function expected' );
828 };
829
830 /**
831 * Add step to the beginning of the process.
832 *
833 * @inheritdoc #createStep
834 * @return {OO.ui.Process} this
835 * @chainable
836 */
837 OO.ui.Process.prototype.first = function ( step, context ) {
838 this.steps.unshift( this.createStep( step, context ) );
839 return this;
840 };
841
842 /**
843 * Add step to the end of the process.
844 *
845 * @inheritdoc #createStep
846 * @return {OO.ui.Process} this
847 * @chainable
848 */
849 OO.ui.Process.prototype.next = function ( step, context ) {
850 this.steps.push( this.createStep( step, context ) );
851 return this;
852 };
853
854 /**
855 * A window instance represents the life cycle for one single opening of a window
856 * until its closing.
857 *
858 * While OO.ui.WindowManager will reuse OO.ui.Window objects, each time a window is
859 * opened, a new lifecycle starts.
860 *
861 * For more information, please see the [OOUI documentation on MediaWiki] [1].
862 *
863 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows
864 *
865 * @class
866 *
867 * @constructor
868 */
869 OO.ui.WindowInstance = function OOuiWindowInstance() {
870 var deferreds = {
871 opening: $.Deferred(),
872 opened: $.Deferred(),
873 closing: $.Deferred(),
874 closed: $.Deferred()
875 };
876
877 /**
878 * @private
879 * @property {Object}
880 */
881 this.deferreds = deferreds;
882
883 // Set these up as chained promises so that rejecting of
884 // an earlier stage automatically rejects the subsequent
885 // would-be stages as well.
886
887 /**
888 * @property {jQuery.Promise}
889 */
890 this.opening = deferreds.opening.promise();
891 /**
892 * @property {jQuery.Promise}
893 */
894 this.opened = this.opening.then( function () {
895 return deferreds.opened;
896 } );
897 /**
898 * @property {jQuery.Promise}
899 */
900 this.closing = this.opened.then( function () {
901 return deferreds.closing;
902 } );
903 /**
904 * @property {jQuery.Promise}
905 */
906 this.closed = this.closing.then( function () {
907 return deferreds.closed;
908 } );
909 };
910
911 /* Setup */
912
913 OO.initClass( OO.ui.WindowInstance );
914
915 /**
916 * Check if window is opening.
917 *
918 * @return {boolean} Window is opening
919 */
920 OO.ui.WindowInstance.prototype.isOpening = function () {
921 return this.deferreds.opened.state() === 'pending';
922 };
923
924 /**
925 * Check if window is opened.
926 *
927 * @return {boolean} Window is opened
928 */
929 OO.ui.WindowInstance.prototype.isOpened = function () {
930 return this.deferreds.opened.state() === 'resolved' &&
931 this.deferreds.closing.state() === 'pending';
932 };
933
934 /**
935 * Check if window is closing.
936 *
937 * @return {boolean} Window is closing
938 */
939 OO.ui.WindowInstance.prototype.isClosing = function () {
940 return this.deferreds.closing.state() === 'resolved' &&
941 this.deferreds.closed.state() === 'pending';
942 };
943
944 /**
945 * Check if window is closed.
946 *
947 * @return {boolean} Window is closed
948 */
949 OO.ui.WindowInstance.prototype.isClosed = function () {
950 return this.deferreds.closed.state() === 'resolved';
951 };
952
953 /**
954 * Window managers are used to open and close {@link OO.ui.Window windows} and control their presentation.
955 * Managed windows are mutually exclusive. If a new window is opened while a current window is opening
956 * or is opened, the current window will be closed and any ongoing {@link OO.ui.Process process} will be cancelled. Windows
957 * themselves are persistent and—rather than being torn down when closed—can be repopulated with the
958 * pertinent data and reused.
959 *
960 * Over the lifecycle of a window, the window manager makes available three promises: `opening`,
961 * `opened`, and `closing`, which represent the primary stages of the cycle:
962 *
963 * **Opening**: the opening stage begins when the window manager’s #openWindow or a window’s
964 * {@link OO.ui.Window#open open} method is used, and the window manager begins to open the window.
965 *
966 * - an `opening` event is emitted with an `opening` promise
967 * - the #getSetupDelay method is called and the returned value is used to time a pause in execution before the
968 * window’s {@link OO.ui.Window#method-setup setup} method is called which executes OO.ui.Window#getSetupProcess.
969 * - a `setup` progress notification is emitted from the `opening` promise
970 * - the #getReadyDelay method is called the returned value is used to time a pause in execution before the
971 * window’s {@link OO.ui.Window#method-ready ready} method is called which executes OO.ui.Window#getReadyProcess.
972 * - a `ready` progress notification is emitted from the `opening` promise
973 * - the `opening` promise is resolved with an `opened` promise
974 *
975 * **Opened**: the window is now open.
976 *
977 * **Closing**: the closing stage begins when the window manager's #closeWindow or the
978 * window's {@link OO.ui.Window#close close} methods is used, and the window manager begins
979 * to close the window.
980 *
981 * - the `opened` promise is resolved with `closing` promise and a `closing` event is emitted
982 * - the #getHoldDelay method is called and the returned value is used to time a pause in execution before
983 * the window's {@link OO.ui.Window#getHoldProcess getHoldProces} method is called on the
984 * window and its result executed
985 * - a `hold` progress notification is emitted from the `closing` promise
986 * - the #getTeardownDelay() method is called and the returned value is used to time a pause in execution before
987 * the window's {@link OO.ui.Window#getTeardownProcess getTeardownProcess} method is called on the
988 * window and its result executed
989 * - a `teardown` progress notification is emitted from the `closing` promise
990 * - the `closing` promise is resolved. The window is now closed
991 *
992 * See the [OOUI documentation on MediaWiki][1] for more information.
993 *
994 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
995 *
996 * @class
997 * @extends OO.ui.Element
998 * @mixins OO.EventEmitter
999 *
1000 * @constructor
1001 * @param {Object} [config] Configuration options
1002 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
1003 * Note that window classes that are instantiated with a factory must have
1004 * a {@link OO.ui.Dialog#static-name static name} property that specifies a symbolic name.
1005 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
1006 */
1007 OO.ui.WindowManager = function OoUiWindowManager( config ) {
1008 // Configuration initialization
1009 config = config || {};
1010
1011 // Parent constructor
1012 OO.ui.WindowManager.parent.call( this, config );
1013
1014 // Mixin constructors
1015 OO.EventEmitter.call( this );
1016
1017 // Properties
1018 this.factory = config.factory;
1019 this.modal = config.modal === undefined || !!config.modal;
1020 this.windows = {};
1021 // Deprecated placeholder promise given to compatOpening in openWindow()
1022 // that is resolved in closeWindow().
1023 this.compatOpened = null;
1024 this.preparingToOpen = null;
1025 this.preparingToClose = null;
1026 this.currentWindow = null;
1027 this.globalEvents = false;
1028 this.$returnFocusTo = null;
1029 this.$ariaHidden = null;
1030 this.onWindowResizeTimeout = null;
1031 this.onWindowResizeHandler = this.onWindowResize.bind( this );
1032 this.afterWindowResizeHandler = this.afterWindowResize.bind( this );
1033
1034 // Initialization
1035 this.$element
1036 .addClass( 'oo-ui-windowManager' )
1037 .toggleClass( 'oo-ui-windowManager-modal', this.modal );
1038 if ( this.modal ) {
1039 this.$element.attr( 'aria-hidden', true );
1040 }
1041 };
1042
1043 /* Setup */
1044
1045 OO.inheritClass( OO.ui.WindowManager, OO.ui.Element );
1046 OO.mixinClass( OO.ui.WindowManager, OO.EventEmitter );
1047
1048 /* Events */
1049
1050 /**
1051 * An 'opening' event is emitted when the window begins to be opened.
1052 *
1053 * @event opening
1054 * @param {OO.ui.Window} win Window that's being opened
1055 * @param {jQuery.Promise} opened A promise resolved with a value when the window is opened successfully.
1056 * This promise also emits `setup` and `ready` notifications. When this promise is resolved, the first
1057 * argument of the value is an 'closed' promise, the second argument is the opening data.
1058 * @param {Object} data Window opening data
1059 */
1060
1061 /**
1062 * A 'closing' event is emitted when the window begins to be closed.
1063 *
1064 * @event closing
1065 * @param {OO.ui.Window} win Window that's being closed
1066 * @param {jQuery.Promise} closed A promise resolved with a value when the window is closed successfully.
1067 * This promise also emits `hold` and `teardown` notifications. When this promise is resolved, the first
1068 * argument of its value is the closing data.
1069 * @param {Object} data Window closing data
1070 */
1071
1072 /**
1073 * A 'resize' event is emitted when a window is resized.
1074 *
1075 * @event resize
1076 * @param {OO.ui.Window} win Window that was resized
1077 */
1078
1079 /* Static Properties */
1080
1081 /**
1082 * Map of the symbolic name of each window size and its CSS properties.
1083 *
1084 * @static
1085 * @inheritable
1086 * @property {Object}
1087 */
1088 OO.ui.WindowManager.static.sizes = {
1089 small: {
1090 width: 300
1091 },
1092 medium: {
1093 width: 500
1094 },
1095 large: {
1096 width: 700
1097 },
1098 larger: {
1099 width: 900
1100 },
1101 full: {
1102 // These can be non-numeric because they are never used in calculations
1103 width: '100%',
1104 height: '100%'
1105 }
1106 };
1107
1108 /**
1109 * Symbolic name of the default window size.
1110 *
1111 * The default size is used if the window's requested size is not recognized.
1112 *
1113 * @static
1114 * @inheritable
1115 * @property {string}
1116 */
1117 OO.ui.WindowManager.static.defaultSize = 'medium';
1118
1119 /* Methods */
1120
1121 /**
1122 * Handle window resize events.
1123 *
1124 * @private
1125 * @param {jQuery.Event} e Window resize event
1126 */
1127 OO.ui.WindowManager.prototype.onWindowResize = function () {
1128 clearTimeout( this.onWindowResizeTimeout );
1129 this.onWindowResizeTimeout = setTimeout( this.afterWindowResizeHandler, 200 );
1130 };
1131
1132 /**
1133 * Handle window resize events.
1134 *
1135 * @private
1136 * @param {jQuery.Event} e Window resize event
1137 */
1138 OO.ui.WindowManager.prototype.afterWindowResize = function () {
1139 var currentFocusedElement = document.activeElement;
1140 if ( this.currentWindow ) {
1141 this.updateWindowSize( this.currentWindow );
1142
1143 // Restore focus to the original element if it has changed.
1144 // When a layout change is made on resize inputs lose focus
1145 // on Android (Chrome and Firefox). See T162127.
1146 if ( currentFocusedElement !== document.activeElement ) {
1147 currentFocusedElement.focus();
1148 }
1149 }
1150 };
1151
1152 /**
1153 * Check if window is opening.
1154 *
1155 * @param {OO.ui.Window} win Window to check
1156 * @return {boolean} Window is opening
1157 */
1158 OO.ui.WindowManager.prototype.isOpening = function ( win ) {
1159 return win === this.currentWindow && !!this.lifecycle &&
1160 this.lifecycle.isOpening();
1161 };
1162
1163 /**
1164 * Check if window is closing.
1165 *
1166 * @param {OO.ui.Window} win Window to check
1167 * @return {boolean} Window is closing
1168 */
1169 OO.ui.WindowManager.prototype.isClosing = function ( win ) {
1170 return win === this.currentWindow && !!this.lifecycle &&
1171 this.lifecycle.isClosing();
1172 };
1173
1174 /**
1175 * Check if window is opened.
1176 *
1177 * @param {OO.ui.Window} win Window to check
1178 * @return {boolean} Window is opened
1179 */
1180 OO.ui.WindowManager.prototype.isOpened = function ( win ) {
1181 return win === this.currentWindow && !!this.lifecycle &&
1182 this.lifecycle.isOpened();
1183 };
1184
1185 /**
1186 * Check if a window is being managed.
1187 *
1188 * @param {OO.ui.Window} win Window to check
1189 * @return {boolean} Window is being managed
1190 */
1191 OO.ui.WindowManager.prototype.hasWindow = function ( win ) {
1192 var name;
1193
1194 for ( name in this.windows ) {
1195 if ( this.windows[ name ] === win ) {
1196 return true;
1197 }
1198 }
1199
1200 return false;
1201 };
1202
1203 /**
1204 * Get the number of milliseconds to wait after opening begins before executing the ‘setup’ process.
1205 *
1206 * @param {OO.ui.Window} win Window being opened
1207 * @param {Object} [data] Window opening data
1208 * @return {number} Milliseconds to wait
1209 */
1210 OO.ui.WindowManager.prototype.getSetupDelay = function () {
1211 return 0;
1212 };
1213
1214 /**
1215 * Get the number of milliseconds to wait after setup has finished before executing the ‘ready’ process.
1216 *
1217 * @param {OO.ui.Window} win Window being opened
1218 * @param {Object} [data] Window opening data
1219 * @return {number} Milliseconds to wait
1220 */
1221 OO.ui.WindowManager.prototype.getReadyDelay = function () {
1222 return this.modal ? OO.ui.theme.getDialogTransitionDuration() : 0;
1223 };
1224
1225 /**
1226 * Get the number of milliseconds to wait after closing has begun before executing the 'hold' process.
1227 *
1228 * @param {OO.ui.Window} win Window being closed
1229 * @param {Object} [data] Window closing data
1230 * @return {number} Milliseconds to wait
1231 */
1232 OO.ui.WindowManager.prototype.getHoldDelay = function () {
1233 return 0;
1234 };
1235
1236 /**
1237 * Get the number of milliseconds to wait after the ‘hold’ process has finished before
1238 * executing the ‘teardown’ process.
1239 *
1240 * @param {OO.ui.Window} win Window being closed
1241 * @param {Object} [data] Window closing data
1242 * @return {number} Milliseconds to wait
1243 */
1244 OO.ui.WindowManager.prototype.getTeardownDelay = function () {
1245 return this.modal ? OO.ui.theme.getDialogTransitionDuration() : 0;
1246 };
1247
1248 /**
1249 * Get a window by its symbolic name.
1250 *
1251 * If the window is not yet instantiated and its symbolic name is recognized by a factory, it will be
1252 * instantiated and added to the window manager automatically. Please see the [OOUI documentation on MediaWiki][3]
1253 * for more information about using factories.
1254 * [3]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
1255 *
1256 * @param {string} name Symbolic name of the window
1257 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
1258 * @throws {Error} An error is thrown if the symbolic name is not recognized by the factory.
1259 * @throws {Error} An error is thrown if the named window is not recognized as a managed window.
1260 */
1261 OO.ui.WindowManager.prototype.getWindow = function ( name ) {
1262 var deferred = $.Deferred(),
1263 win = this.windows[ name ];
1264
1265 if ( !( win instanceof OO.ui.Window ) ) {
1266 if ( this.factory ) {
1267 if ( !this.factory.lookup( name ) ) {
1268 deferred.reject( new OO.ui.Error(
1269 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
1270 ) );
1271 } else {
1272 win = this.factory.create( name );
1273 this.addWindows( [ win ] );
1274 deferred.resolve( win );
1275 }
1276 } else {
1277 deferred.reject( new OO.ui.Error(
1278 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
1279 ) );
1280 }
1281 } else {
1282 deferred.resolve( win );
1283 }
1284
1285 return deferred.promise();
1286 };
1287
1288 /**
1289 * Get current window.
1290 *
1291 * @return {OO.ui.Window|null} Currently opening/opened/closing window
1292 */
1293 OO.ui.WindowManager.prototype.getCurrentWindow = function () {
1294 return this.currentWindow;
1295 };
1296
1297 /**
1298 * Open a window.
1299 *
1300 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
1301 * @param {Object} [data] Window opening data
1302 * @param {jQuery|null} [data.$returnFocusTo] Element to which the window will return focus when closed.
1303 * Defaults the current activeElement. If set to null, focus isn't changed on close.
1304 * @return {OO.ui.WindowInstance} A lifecycle object representing this particular
1305 * opening of the window. For backwards-compatibility, then object is also a Thenable that is resolved
1306 * when the window is done opening, with nested promise for when closing starts. This behaviour
1307 * is deprecated and is not compatible with jQuery 3. See T163510.
1308 * @fires opening
1309 */
1310 OO.ui.WindowManager.prototype.openWindow = function ( win, data, lifecycle, compatOpening ) {
1311 var error,
1312 manager = this;
1313 data = data || {};
1314
1315 // Internal parameter 'lifecycle' allows this method to always return
1316 // a lifecycle even if the window still needs to be created
1317 // asynchronously when 'win' is a string.
1318 lifecycle = lifecycle || new OO.ui.WindowInstance();
1319 compatOpening = compatOpening || $.Deferred();
1320
1321 // Turn lifecycle into a Thenable for backwards-compatibility with
1322 // the deprecated nested-promise behaviour, see T163510.
1323 [ 'state', 'always', 'catch', 'pipe', 'then', 'promise', 'progress', 'done', 'fail' ]
1324 .forEach( function ( method ) {
1325 lifecycle[ method ] = function () {
1326 OO.ui.warnDeprecation(
1327 'Using the return value of openWindow as a promise is deprecated. ' +
1328 'Use .openWindow( ... ).opening.' + method + '( ... ) instead.'
1329 );
1330 return compatOpening[ method ].apply( this, arguments );
1331 };
1332 } );
1333
1334 // Argument handling
1335 if ( typeof win === 'string' ) {
1336 this.getWindow( win ).then(
1337 function ( win ) {
1338 manager.openWindow( win, data, lifecycle, compatOpening );
1339 },
1340 function ( err ) {
1341 lifecycle.deferreds.opening.reject( err );
1342 }
1343 );
1344 return lifecycle;
1345 }
1346
1347 // Error handling
1348 if ( !this.hasWindow( win ) ) {
1349 error = 'Cannot open window: window is not attached to manager';
1350 } else if ( this.lifecycle && this.lifecycle.isOpened() ) {
1351 error = 'Cannot open window: another window is open';
1352 } else if ( this.preparingToOpen || ( this.lifecycle && this.lifecycle.isOpening() ) ) {
1353 error = 'Cannot open window: another window is opening';
1354 }
1355
1356 if ( error ) {
1357 compatOpening.reject( new OO.ui.Error( error ) );
1358 lifecycle.deferreds.opening.reject( new OO.ui.Error( error ) );
1359 return lifecycle;
1360 }
1361
1362 // If a window is currently closing, wait for it to complete
1363 this.preparingToOpen = $.when( this.lifecycle && this.lifecycle.closed );
1364 // Ensure handlers get called after preparingToOpen is set
1365 this.preparingToOpen.done( function () {
1366 if ( manager.modal ) {
1367 manager.toggleGlobalEvents( true );
1368 manager.toggleAriaIsolation( true );
1369 }
1370 manager.$returnFocusTo = data.$returnFocusTo !== undefined ? data.$returnFocusTo : $( document.activeElement );
1371 manager.currentWindow = win;
1372 manager.lifecycle = lifecycle;
1373 manager.preparingToOpen = null;
1374 manager.emit( 'opening', win, compatOpening, data );
1375 lifecycle.deferreds.opening.resolve( data );
1376 setTimeout( function () {
1377 manager.compatOpened = $.Deferred();
1378 win.setup( data ).then( function () {
1379 compatOpening.notify( { state: 'setup' } );
1380 setTimeout( function () {
1381 win.ready( data ).then( function () {
1382 compatOpening.notify( { state: 'ready' } );
1383 lifecycle.deferreds.opened.resolve( data );
1384 compatOpening.resolve( manager.compatOpened.promise(), data );
1385 }, function () {
1386 lifecycle.deferreds.opened.reject();
1387 compatOpening.reject();
1388 manager.closeWindow( win );
1389 } );
1390 }, manager.getReadyDelay() );
1391 }, function () {
1392 lifecycle.deferreds.opened.reject();
1393 compatOpening.reject();
1394 manager.closeWindow( win );
1395 } );
1396 }, manager.getSetupDelay() );
1397 } );
1398
1399 return lifecycle;
1400 };
1401
1402 /**
1403 * Close a window.
1404 *
1405 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
1406 * @param {Object} [data] Window closing data
1407 * @return {OO.ui.WindowInstance} A lifecycle object representing this particular
1408 * opening of the window. For backwards-compatibility, the object is also a Thenable that is resolved
1409 * when the window is done closing, see T163510.
1410 * @fires closing
1411 */
1412 OO.ui.WindowManager.prototype.closeWindow = function ( win, data ) {
1413 var error,
1414 manager = this,
1415 compatClosing = $.Deferred(),
1416 lifecycle = this.lifecycle,
1417 compatOpened;
1418
1419 // Argument handling
1420 if ( typeof win === 'string' ) {
1421 win = this.windows[ win ];
1422 } else if ( !this.hasWindow( win ) ) {
1423 win = null;
1424 }
1425
1426 // Error handling
1427 if ( !lifecycle ) {
1428 error = 'Cannot close window: no window is currently open';
1429 } else if ( !win ) {
1430 error = 'Cannot close window: window is not attached to manager';
1431 } else if ( win !== this.currentWindow || this.lifecycle.isClosed() ) {
1432 error = 'Cannot close window: window already closed with different data';
1433 } else if ( this.preparingToClose || this.lifecycle.isClosing() ) {
1434 error = 'Cannot close window: window already closing with different data';
1435 }
1436
1437 if ( error ) {
1438 // This function was called for the wrong window and we don't want to mess with the current
1439 // window's state.
1440 lifecycle = new OO.ui.WindowInstance();
1441 // Pretend the window has been opened, so that we can pretend to fail to close it.
1442 lifecycle.deferreds.opening.resolve( {} );
1443 lifecycle.deferreds.opened.resolve( {} );
1444 }
1445
1446 // Turn lifecycle into a Thenable for backwards-compatibility with
1447 // the deprecated nested-promise behaviour, see T163510.
1448 [ 'state', 'always', 'catch', 'pipe', 'then', 'promise', 'progress', 'done', 'fail' ]
1449 .forEach( function ( method ) {
1450 lifecycle[ method ] = function () {
1451 OO.ui.warnDeprecation(
1452 'Using the return value of closeWindow as a promise is deprecated. ' +
1453 'Use .closeWindow( ... ).closed.' + method + '( ... ) instead.'
1454 );
1455 return compatClosing[ method ].apply( this, arguments );
1456 };
1457 } );
1458
1459 if ( error ) {
1460 compatClosing.reject( new OO.ui.Error( error ) );
1461 lifecycle.deferreds.closing.reject( new OO.ui.Error( error ) );
1462 return lifecycle;
1463 }
1464
1465 // If the window is currently opening, close it when it's done
1466 this.preparingToClose = $.when( this.lifecycle.opened );
1467 // Ensure handlers get called after preparingToClose is set
1468 this.preparingToClose.always( function () {
1469 manager.preparingToClose = null;
1470 manager.emit( 'closing', win, compatClosing, data );
1471 lifecycle.deferreds.closing.resolve( data );
1472 compatOpened = manager.compatOpened;
1473 manager.compatOpened = null;
1474 compatOpened.resolve( compatClosing.promise(), data );
1475 setTimeout( function () {
1476 win.hold( data ).then( function () {
1477 compatClosing.notify( { state: 'hold' } );
1478 setTimeout( function () {
1479 win.teardown( data ).then( function () {
1480 compatClosing.notify( { state: 'teardown' } );
1481 if ( manager.modal ) {
1482 manager.toggleGlobalEvents( false );
1483 manager.toggleAriaIsolation( false );
1484 }
1485 if ( manager.$returnFocusTo && manager.$returnFocusTo.length ) {
1486 manager.$returnFocusTo[ 0 ].focus();
1487 }
1488 manager.currentWindow = null;
1489 manager.lifecycle = null;
1490 lifecycle.deferreds.closed.resolve( data );
1491 compatClosing.resolve( data );
1492 } );
1493 }, manager.getTeardownDelay() );
1494 } );
1495 }, manager.getHoldDelay() );
1496 } );
1497
1498 return lifecycle;
1499 };
1500
1501 /**
1502 * Add windows to the window manager.
1503 *
1504 * Windows can be added by reference, symbolic name, or explicitly defined symbolic names.
1505 * See the [OOUI documentation on MediaWiki] [2] for examples.
1506 * [2]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
1507 *
1508 * This function can be called in two manners:
1509 *
1510 * 1. `.addWindows( [ windowA, windowB, ... ] )` (where `windowA`, `windowB` are OO.ui.Window objects)
1511 *
1512 * This syntax registers windows under the symbolic names defined in their `.static.name`
1513 * properties. For example, if `windowA.constructor.static.name` is `'nameA'`, calling
1514 * `.openWindow( 'nameA' )` afterwards will open the window `windowA`. This syntax requires the
1515 * static name to be set, otherwise an exception will be thrown.
1516 *
1517 * This is the recommended way, as it allows for an easier switch to using a window factory.
1518 *
1519 * 2. `.addWindows( { nameA: windowA, nameB: windowB, ... } )`
1520 *
1521 * This syntax registers windows under the explicitly given symbolic names. In this example,
1522 * calling `.openWindow( 'nameA' )` afterwards will open the window `windowA`, regardless of what
1523 * its `.static.name` is set to. The static name is not required to be set.
1524 *
1525 * This should only be used if you need to override the default symbolic names.
1526 *
1527 * Example:
1528 *
1529 * var windowManager = new OO.ui.WindowManager();
1530 * $( 'body' ).append( windowManager.$element );
1531 *
1532 * // Add a window under the default name: see OO.ui.MessageDialog.static.name
1533 * windowManager.addWindows( [ new OO.ui.MessageDialog() ] );
1534 * // Add a window under an explicit name
1535 * windowManager.addWindows( { myMessageDialog: new OO.ui.MessageDialog() } );
1536 *
1537 * // Open window by default name
1538 * windowManager.openWindow( 'message' );
1539 * // Open window by explicitly given name
1540 * windowManager.openWindow( 'myMessageDialog' );
1541 *
1542 *
1543 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows An array of window objects specified
1544 * by reference, symbolic name, or explicitly defined symbolic names.
1545 * @throws {Error} An error is thrown if a window is added by symbolic name, but has neither an
1546 * explicit nor a statically configured symbolic name.
1547 */
1548 OO.ui.WindowManager.prototype.addWindows = function ( windows ) {
1549 var i, len, win, name, list;
1550
1551 if ( Array.isArray( windows ) ) {
1552 // Convert to map of windows by looking up symbolic names from static configuration
1553 list = {};
1554 for ( i = 0, len = windows.length; i < len; i++ ) {
1555 name = windows[ i ].constructor.static.name;
1556 if ( !name ) {
1557 throw new Error( 'Windows must have a `name` static property defined.' );
1558 }
1559 list[ name ] = windows[ i ];
1560 }
1561 } else if ( OO.isPlainObject( windows ) ) {
1562 list = windows;
1563 }
1564
1565 // Add windows
1566 for ( name in list ) {
1567 win = list[ name ];
1568 this.windows[ name ] = win.toggle( false );
1569 this.$element.append( win.$element );
1570 win.setManager( this );
1571 }
1572 };
1573
1574 /**
1575 * Remove the specified windows from the windows manager.
1576 *
1577 * Windows will be closed before they are removed. If you wish to remove all windows, you may wish to use
1578 * the #clearWindows method instead. If you no longer need the window manager and want to ensure that it no
1579 * longer listens to events, use the #destroy method.
1580 *
1581 * @param {string[]} names Symbolic names of windows to remove
1582 * @return {jQuery.Promise} Promise resolved when window is closed and removed
1583 * @throws {Error} An error is thrown if the named windows are not managed by the window manager.
1584 */
1585 OO.ui.WindowManager.prototype.removeWindows = function ( names ) {
1586 var i, len, win, name, cleanupWindow,
1587 manager = this,
1588 promises = [],
1589 cleanup = function ( name, win ) {
1590 delete manager.windows[ name ];
1591 win.$element.detach();
1592 };
1593
1594 for ( i = 0, len = names.length; i < len; i++ ) {
1595 name = names[ i ];
1596 win = this.windows[ name ];
1597 if ( !win ) {
1598 throw new Error( 'Cannot remove window' );
1599 }
1600 cleanupWindow = cleanup.bind( null, name, win );
1601 promises.push( this.closeWindow( name ).closed.then( cleanupWindow, cleanupWindow ) );
1602 }
1603
1604 return $.when.apply( $, promises );
1605 };
1606
1607 /**
1608 * Remove all windows from the window manager.
1609 *
1610 * Windows will be closed before they are removed. Note that the window manager, though not in use, will still
1611 * listen to events. If the window manager will not be used again, you may wish to use the #destroy method instead.
1612 * To remove just a subset of windows, use the #removeWindows method.
1613 *
1614 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
1615 */
1616 OO.ui.WindowManager.prototype.clearWindows = function () {
1617 return this.removeWindows( Object.keys( this.windows ) );
1618 };
1619
1620 /**
1621 * Set dialog size. In general, this method should not be called directly.
1622 *
1623 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
1624 *
1625 * @param {OO.ui.Window} win Window to update, should be the current window
1626 * @chainable
1627 */
1628 OO.ui.WindowManager.prototype.updateWindowSize = function ( win ) {
1629 var isFullscreen;
1630
1631 // Bypass for non-current, and thus invisible, windows
1632 if ( win !== this.currentWindow ) {
1633 return;
1634 }
1635
1636 isFullscreen = win.getSize() === 'full';
1637
1638 this.$element.toggleClass( 'oo-ui-windowManager-fullscreen', isFullscreen );
1639 this.$element.toggleClass( 'oo-ui-windowManager-floating', !isFullscreen );
1640 win.setDimensions( win.getSizeProperties() );
1641
1642 this.emit( 'resize', win );
1643
1644 return this;
1645 };
1646
1647 /**
1648 * Bind or unbind global events for scrolling.
1649 *
1650 * @private
1651 * @param {boolean} [on] Bind global events
1652 * @chainable
1653 */
1654 OO.ui.WindowManager.prototype.toggleGlobalEvents = function ( on ) {
1655 var scrollWidth, bodyMargin,
1656 $body = $( this.getElementDocument().body ),
1657 // We could have multiple window managers open so only modify
1658 // the body css at the bottom of the stack
1659 stackDepth = $body.data( 'windowManagerGlobalEvents' ) || 0;
1660
1661 on = on === undefined ? !!this.globalEvents : !!on;
1662
1663 if ( on ) {
1664 if ( !this.globalEvents ) {
1665 $( this.getElementWindow() ).on( {
1666 // Start listening for top-level window dimension changes
1667 'orientationchange resize': this.onWindowResizeHandler
1668 } );
1669 if ( stackDepth === 0 ) {
1670 scrollWidth = window.innerWidth - document.documentElement.clientWidth;
1671 bodyMargin = parseFloat( $body.css( 'margin-right' ) ) || 0;
1672 $body.css( {
1673 overflow: 'hidden',
1674 'margin-right': bodyMargin + scrollWidth
1675 } );
1676 }
1677 stackDepth++;
1678 this.globalEvents = true;
1679 }
1680 } else if ( this.globalEvents ) {
1681 $( this.getElementWindow() ).off( {
1682 // Stop listening for top-level window dimension changes
1683 'orientationchange resize': this.onWindowResizeHandler
1684 } );
1685 stackDepth--;
1686 if ( stackDepth === 0 ) {
1687 $body.css( {
1688 overflow: '',
1689 'margin-right': ''
1690 } );
1691 }
1692 this.globalEvents = false;
1693 }
1694 $body.data( 'windowManagerGlobalEvents', stackDepth );
1695
1696 return this;
1697 };
1698
1699 /**
1700 * Toggle screen reader visibility of content other than the window manager.
1701 *
1702 * @private
1703 * @param {boolean} [isolate] Make only the window manager visible to screen readers
1704 * @chainable
1705 */
1706 OO.ui.WindowManager.prototype.toggleAriaIsolation = function ( isolate ) {
1707 var $topLevelElement;
1708 isolate = isolate === undefined ? !this.$ariaHidden : !!isolate;
1709
1710 if ( isolate ) {
1711 if ( !this.$ariaHidden ) {
1712 // Find the top level element containing the window manager or the
1713 // window manager's element itself in case its a direct child of body
1714 $topLevelElement = this.$element.parentsUntil( 'body' ).last();
1715 $topLevelElement = $topLevelElement.length === 0 ? this.$element : $topLevelElement;
1716
1717 // In case previously set by another window manager
1718 this.$element.removeAttr( 'aria-hidden' );
1719
1720 // Hide everything other than the window manager from screen readers
1721 this.$ariaHidden = $( 'body' )
1722 .children()
1723 .not( 'script' )
1724 .not( $topLevelElement )
1725 .attr( 'aria-hidden', true );
1726 }
1727 } else if ( this.$ariaHidden ) {
1728 // Restore screen reader visibility
1729 this.$ariaHidden.removeAttr( 'aria-hidden' );
1730 this.$ariaHidden = null;
1731
1732 // and hide the window manager
1733 this.$element.attr( 'aria-hidden', true );
1734 }
1735
1736 return this;
1737 };
1738
1739 /**
1740 * Destroy the window manager.
1741 *
1742 * Destroying the window manager ensures that it will no longer listen to events. If you would like to
1743 * continue using the window manager, but wish to remove all windows from it, use the #clearWindows method
1744 * instead.
1745 */
1746 OO.ui.WindowManager.prototype.destroy = function () {
1747 this.toggleGlobalEvents( false );
1748 this.toggleAriaIsolation( false );
1749 this.clearWindows();
1750 this.$element.remove();
1751 };
1752
1753 /**
1754 * A window is a container for elements that are in a child frame. They are used with
1755 * a window manager (OO.ui.WindowManager), which is used to open and close the window and control
1756 * its presentation. The size of a window is specified using a symbolic name (e.g., ‘small’, ‘medium’,
1757 * ‘large’), which is interpreted by the window manager. If the requested size is not recognized,
1758 * the window manager will choose a sensible fallback.
1759 *
1760 * The lifecycle of a window has three primary stages (opening, opened, and closing) in which
1761 * different processes are executed:
1762 *
1763 * **opening**: The opening stage begins when the window manager's {@link OO.ui.WindowManager#openWindow
1764 * openWindow} or the window's {@link #open open} methods are used, and the window manager begins to open
1765 * the window.
1766 *
1767 * - {@link #getSetupProcess} method is called and its result executed
1768 * - {@link #getReadyProcess} method is called and its result executed
1769 *
1770 * **opened**: The window is now open
1771 *
1772 * **closing**: The closing stage begins when the window manager's
1773 * {@link OO.ui.WindowManager#closeWindow closeWindow}
1774 * or the window's {@link #close} methods are used, and the window manager begins to close the window.
1775 *
1776 * - {@link #getHoldProcess} method is called and its result executed
1777 * - {@link #getTeardownProcess} method is called and its result executed. The window is now closed
1778 *
1779 * Each of the window's processes (setup, ready, hold, and teardown) can be extended in subclasses
1780 * by overriding the window's #getSetupProcess, #getReadyProcess, #getHoldProcess and #getTeardownProcess
1781 * methods. Note that each {@link OO.ui.Process process} is executed in series, so asynchronous
1782 * processing can complete. Always assume window processes are executed asynchronously.
1783 *
1784 * For more information, please see the [OOUI documentation on MediaWiki] [1].
1785 *
1786 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows
1787 *
1788 * @abstract
1789 * @class
1790 * @extends OO.ui.Element
1791 * @mixins OO.EventEmitter
1792 *
1793 * @constructor
1794 * @param {Object} [config] Configuration options
1795 * @cfg {string} [size] Symbolic name of the dialog size: `small`, `medium`, `large`, `larger` or
1796 * `full`. If omitted, the value of the {@link #static-size static size} property will be used.
1797 */
1798 OO.ui.Window = function OoUiWindow( config ) {
1799 // Configuration initialization
1800 config = config || {};
1801
1802 // Parent constructor
1803 OO.ui.Window.parent.call( this, config );
1804
1805 // Mixin constructors
1806 OO.EventEmitter.call( this );
1807
1808 // Properties
1809 this.manager = null;
1810 this.size = config.size || this.constructor.static.size;
1811 this.$frame = $( '<div>' );
1812 /**
1813 * Overlay element to use for the `$overlay` configuration option of widgets that support it.
1814 * Things put inside of it are overlaid on top of the window and are not bound to its dimensions.
1815 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
1816 *
1817 * MyDialog.prototype.initialize = function () {
1818 * ...
1819 * var popupButton = new OO.ui.PopupButtonWidget( {
1820 * $overlay: this.$overlay,
1821 * label: 'Popup button',
1822 * popup: {
1823 * $content: $( '<p>Popup contents.</p><p>Popup contents.</p><p>Popup contents.</p>' ),
1824 * padded: true
1825 * }
1826 * } );
1827 * ...
1828 * };
1829 *
1830 * @property {jQuery}
1831 */
1832 this.$overlay = $( '<div>' );
1833 this.$content = $( '<div>' );
1834
1835 this.$focusTrapBefore = $( '<div>' ).prop( 'tabIndex', 0 );
1836 this.$focusTrapAfter = $( '<div>' ).prop( 'tabIndex', 0 );
1837 this.$focusTraps = this.$focusTrapBefore.add( this.$focusTrapAfter );
1838
1839 // Initialization
1840 this.$overlay.addClass( 'oo-ui-window-overlay' );
1841 this.$content
1842 .addClass( 'oo-ui-window-content' )
1843 .attr( 'tabindex', 0 );
1844 this.$frame
1845 .addClass( 'oo-ui-window-frame' )
1846 .append( this.$focusTrapBefore, this.$content, this.$focusTrapAfter );
1847
1848 this.$element
1849 .addClass( 'oo-ui-window' )
1850 .append( this.$frame, this.$overlay );
1851
1852 // Initially hidden - using #toggle may cause errors if subclasses override toggle with methods
1853 // that reference properties not initialized at that time of parent class construction
1854 // TODO: Find a better way to handle post-constructor setup
1855 this.visible = false;
1856 this.$element.addClass( 'oo-ui-element-hidden' );
1857 };
1858
1859 /* Setup */
1860
1861 OO.inheritClass( OO.ui.Window, OO.ui.Element );
1862 OO.mixinClass( OO.ui.Window, OO.EventEmitter );
1863
1864 /* Static Properties */
1865
1866 /**
1867 * Symbolic name of the window size: `small`, `medium`, `large`, `larger` or `full`.
1868 *
1869 * The static size is used if no #size is configured during construction.
1870 *
1871 * @static
1872 * @inheritable
1873 * @property {string}
1874 */
1875 OO.ui.Window.static.size = 'medium';
1876
1877 /* Methods */
1878
1879 /**
1880 * Handle mouse down events.
1881 *
1882 * @private
1883 * @param {jQuery.Event} e Mouse down event
1884 */
1885 OO.ui.Window.prototype.onMouseDown = function ( e ) {
1886 // Prevent clicking on the click-block from stealing focus
1887 if ( e.target === this.$element[ 0 ] ) {
1888 return false;
1889 }
1890 };
1891
1892 /**
1893 * Check if the window has been initialized.
1894 *
1895 * Initialization occurs when a window is added to a manager.
1896 *
1897 * @return {boolean} Window has been initialized
1898 */
1899 OO.ui.Window.prototype.isInitialized = function () {
1900 return !!this.manager;
1901 };
1902
1903 /**
1904 * Check if the window is visible.
1905 *
1906 * @return {boolean} Window is visible
1907 */
1908 OO.ui.Window.prototype.isVisible = function () {
1909 return this.visible;
1910 };
1911
1912 /**
1913 * Check if the window is opening.
1914 *
1915 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isOpening isOpening}
1916 * method.
1917 *
1918 * @return {boolean} Window is opening
1919 */
1920 OO.ui.Window.prototype.isOpening = function () {
1921 return this.manager.isOpening( this );
1922 };
1923
1924 /**
1925 * Check if the window is closing.
1926 *
1927 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isClosing isClosing} method.
1928 *
1929 * @return {boolean} Window is closing
1930 */
1931 OO.ui.Window.prototype.isClosing = function () {
1932 return this.manager.isClosing( this );
1933 };
1934
1935 /**
1936 * Check if the window is opened.
1937 *
1938 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isOpened isOpened} method.
1939 *
1940 * @return {boolean} Window is opened
1941 */
1942 OO.ui.Window.prototype.isOpened = function () {
1943 return this.manager.isOpened( this );
1944 };
1945
1946 /**
1947 * Get the window manager.
1948 *
1949 * All windows must be attached to a window manager, which is used to open
1950 * and close the window and control its presentation.
1951 *
1952 * @return {OO.ui.WindowManager} Manager of window
1953 */
1954 OO.ui.Window.prototype.getManager = function () {
1955 return this.manager;
1956 };
1957
1958 /**
1959 * Get the symbolic name of the window size (e.g., `small` or `medium`).
1960 *
1961 * @return {string} Symbolic name of the size: `small`, `medium`, `large`, `larger`, `full`
1962 */
1963 OO.ui.Window.prototype.getSize = function () {
1964 var viewport = OO.ui.Element.static.getDimensions( this.getElementWindow() ),
1965 sizes = this.manager.constructor.static.sizes,
1966 size = this.size;
1967
1968 if ( !sizes[ size ] ) {
1969 size = this.manager.constructor.static.defaultSize;
1970 }
1971 if ( size !== 'full' && viewport.rect.right - viewport.rect.left < sizes[ size ].width ) {
1972 size = 'full';
1973 }
1974
1975 return size;
1976 };
1977
1978 /**
1979 * Get the size properties associated with the current window size
1980 *
1981 * @return {Object} Size properties
1982 */
1983 OO.ui.Window.prototype.getSizeProperties = function () {
1984 return this.manager.constructor.static.sizes[ this.getSize() ];
1985 };
1986
1987 /**
1988 * Disable transitions on window's frame for the duration of the callback function, then enable them
1989 * back.
1990 *
1991 * @private
1992 * @param {Function} callback Function to call while transitions are disabled
1993 */
1994 OO.ui.Window.prototype.withoutSizeTransitions = function ( callback ) {
1995 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1996 // Disable transitions first, otherwise we'll get values from when the window was animating.
1997 // We need to build the transition CSS properties using these specific properties since
1998 // Firefox doesn't return anything useful when asked just for 'transition'.
1999 var oldTransition = this.$frame.css( 'transition-property' ) + ' ' +
2000 this.$frame.css( 'transition-duration' ) + ' ' +
2001 this.$frame.css( 'transition-timing-function' ) + ' ' +
2002 this.$frame.css( 'transition-delay' );
2003
2004 this.$frame.css( 'transition', 'none' );
2005 callback();
2006
2007 // Force reflow to make sure the style changes done inside callback
2008 // really are not transitioned
2009 this.$frame.height();
2010 this.$frame.css( 'transition', oldTransition );
2011 };
2012
2013 /**
2014 * Get the height of the full window contents (i.e., the window head, body and foot together).
2015 *
2016 * What consistitutes the head, body, and foot varies depending on the window type.
2017 * A {@link OO.ui.MessageDialog message dialog} displays a title and message in its body,
2018 * and any actions in the foot. A {@link OO.ui.ProcessDialog process dialog} displays a title
2019 * and special actions in the head, and dialog content in the body.
2020 *
2021 * To get just the height of the dialog body, use the #getBodyHeight method.
2022 *
2023 * @return {number} The height of the window contents (the dialog head, body and foot) in pixels
2024 */
2025 OO.ui.Window.prototype.getContentHeight = function () {
2026 var bodyHeight,
2027 win = this,
2028 bodyStyleObj = this.$body[ 0 ].style,
2029 frameStyleObj = this.$frame[ 0 ].style;
2030
2031 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
2032 // Disable transitions first, otherwise we'll get values from when the window was animating.
2033 this.withoutSizeTransitions( function () {
2034 var oldHeight = frameStyleObj.height,
2035 oldPosition = bodyStyleObj.position;
2036 frameStyleObj.height = '1px';
2037 // Force body to resize to new width
2038 bodyStyleObj.position = 'relative';
2039 bodyHeight = win.getBodyHeight();
2040 frameStyleObj.height = oldHeight;
2041 bodyStyleObj.position = oldPosition;
2042 } );
2043
2044 return (
2045 // Add buffer for border
2046 ( this.$frame.outerHeight() - this.$frame.innerHeight() ) +
2047 // Use combined heights of children
2048 ( this.$head.outerHeight( true ) + bodyHeight + this.$foot.outerHeight( true ) )
2049 );
2050 };
2051
2052 /**
2053 * Get the height of the window body.
2054 *
2055 * To get the height of the full window contents (the window body, head, and foot together),
2056 * use #getContentHeight.
2057 *
2058 * When this function is called, the window will temporarily have been resized
2059 * to height=1px, so .scrollHeight measurements can be taken accurately.
2060 *
2061 * @return {number} Height of the window body in pixels
2062 */
2063 OO.ui.Window.prototype.getBodyHeight = function () {
2064 return this.$body[ 0 ].scrollHeight;
2065 };
2066
2067 /**
2068 * Get the directionality of the frame (right-to-left or left-to-right).
2069 *
2070 * @return {string} Directionality: `'ltr'` or `'rtl'`
2071 */
2072 OO.ui.Window.prototype.getDir = function () {
2073 return OO.ui.Element.static.getDir( this.$content ) || 'ltr';
2074 };
2075
2076 /**
2077 * Get the 'setup' process.
2078 *
2079 * The setup process is used to set up a window for use in a particular context, based on the `data`
2080 * argument. This method is called during the opening phase of the window’s lifecycle (before the
2081 * opening animation). You can add elements to the window in this process or set their default
2082 * values.
2083 *
2084 * Override this method to add additional steps to the ‘setup’ process the parent method provides
2085 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2086 * of OO.ui.Process.
2087 *
2088 * To add window content that persists between openings, you may wish to use the #initialize method
2089 * instead.
2090 *
2091 * @param {Object} [data] Window opening data
2092 * @return {OO.ui.Process} Setup process
2093 */
2094 OO.ui.Window.prototype.getSetupProcess = function () {
2095 return new OO.ui.Process();
2096 };
2097
2098 /**
2099 * Get the ‘ready’ process.
2100 *
2101 * The ready process is used to ready a window for use in a particular context, based on the `data`
2102 * argument. This method is called during the opening phase of the window’s lifecycle, after the
2103 * window has been {@link #getSetupProcess setup} (after the opening animation). You can focus
2104 * elements in the window in this process, or open their dropdowns.
2105 *
2106 * Override this method to add additional steps to the ‘ready’ process the parent method
2107 * provides using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next}
2108 * methods of OO.ui.Process.
2109 *
2110 * @param {Object} [data] Window opening data
2111 * @return {OO.ui.Process} Ready process
2112 */
2113 OO.ui.Window.prototype.getReadyProcess = function () {
2114 return new OO.ui.Process();
2115 };
2116
2117 /**
2118 * Get the 'hold' process.
2119 *
2120 * The hold process is used to keep a window from being used in a particular context, based on the
2121 * `data` argument. This method is called during the closing phase of the window’s lifecycle (before
2122 * the closing animation). You can close dropdowns of elements in the window in this process, if
2123 * they do not get closed automatically.
2124 *
2125 * Override this method to add additional steps to the 'hold' process the parent method provides
2126 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2127 * of OO.ui.Process.
2128 *
2129 * @param {Object} [data] Window closing data
2130 * @return {OO.ui.Process} Hold process
2131 */
2132 OO.ui.Window.prototype.getHoldProcess = function () {
2133 return new OO.ui.Process();
2134 };
2135
2136 /**
2137 * Get the ‘teardown’ process.
2138 *
2139 * The teardown process is used to teardown a window after use. During teardown, user interactions
2140 * within the window are conveyed and the window is closed, based on the `data` argument. This
2141 * method is called during the closing phase of the window’s lifecycle (after the closing
2142 * animation). You can remove elements in the window in this process or clear their values.
2143 *
2144 * Override this method to add additional steps to the ‘teardown’ process the parent method provides
2145 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2146 * of OO.ui.Process.
2147 *
2148 * @param {Object} [data] Window closing data
2149 * @return {OO.ui.Process} Teardown process
2150 */
2151 OO.ui.Window.prototype.getTeardownProcess = function () {
2152 return new OO.ui.Process();
2153 };
2154
2155 /**
2156 * Set the window manager.
2157 *
2158 * This will cause the window to initialize. Calling it more than once will cause an error.
2159 *
2160 * @param {OO.ui.WindowManager} manager Manager for this window
2161 * @throws {Error} An error is thrown if the method is called more than once
2162 * @chainable
2163 */
2164 OO.ui.Window.prototype.setManager = function ( manager ) {
2165 if ( this.manager ) {
2166 throw new Error( 'Cannot set window manager, window already has a manager' );
2167 }
2168
2169 this.manager = manager;
2170 this.initialize();
2171
2172 return this;
2173 };
2174
2175 /**
2176 * Set the window size by symbolic name (e.g., 'small' or 'medium')
2177 *
2178 * @param {string} size Symbolic name of size: `small`, `medium`, `large`, `larger` or
2179 * `full`
2180 * @chainable
2181 */
2182 OO.ui.Window.prototype.setSize = function ( size ) {
2183 this.size = size;
2184 this.updateSize();
2185 return this;
2186 };
2187
2188 /**
2189 * Update the window size.
2190 *
2191 * @throws {Error} An error is thrown if the window is not attached to a window manager
2192 * @chainable
2193 */
2194 OO.ui.Window.prototype.updateSize = function () {
2195 if ( !this.manager ) {
2196 throw new Error( 'Cannot update window size, must be attached to a manager' );
2197 }
2198
2199 this.manager.updateWindowSize( this );
2200
2201 return this;
2202 };
2203
2204 /**
2205 * Set window dimensions. This method is called by the {@link OO.ui.WindowManager window manager}
2206 * when the window is opening. In general, setDimensions should not be called directly.
2207 *
2208 * To set the size of the window, use the #setSize method.
2209 *
2210 * @param {Object} dim CSS dimension properties
2211 * @param {string|number} [dim.width] Width
2212 * @param {string|number} [dim.minWidth] Minimum width
2213 * @param {string|number} [dim.maxWidth] Maximum width
2214 * @param {string|number} [dim.height] Height, omit to set based on height of contents
2215 * @param {string|number} [dim.minHeight] Minimum height
2216 * @param {string|number} [dim.maxHeight] Maximum height
2217 * @chainable
2218 */
2219 OO.ui.Window.prototype.setDimensions = function ( dim ) {
2220 var height,
2221 win = this,
2222 styleObj = this.$frame[ 0 ].style;
2223
2224 // Calculate the height we need to set using the correct width
2225 if ( dim.height === undefined ) {
2226 this.withoutSizeTransitions( function () {
2227 var oldWidth = styleObj.width;
2228 win.$frame.css( 'width', dim.width || '' );
2229 height = win.getContentHeight();
2230 styleObj.width = oldWidth;
2231 } );
2232 } else {
2233 height = dim.height;
2234 }
2235
2236 this.$frame.css( {
2237 width: dim.width || '',
2238 minWidth: dim.minWidth || '',
2239 maxWidth: dim.maxWidth || '',
2240 height: height || '',
2241 minHeight: dim.minHeight || '',
2242 maxHeight: dim.maxHeight || ''
2243 } );
2244
2245 return this;
2246 };
2247
2248 /**
2249 * Initialize window contents.
2250 *
2251 * Before the window is opened for the first time, #initialize is called so that content that
2252 * persists between openings can be added to the window.
2253 *
2254 * To set up a window with new content each time the window opens, use #getSetupProcess.
2255 *
2256 * @throws {Error} An error is thrown if the window is not attached to a window manager
2257 * @chainable
2258 */
2259 OO.ui.Window.prototype.initialize = function () {
2260 if ( !this.manager ) {
2261 throw new Error( 'Cannot initialize window, must be attached to a manager' );
2262 }
2263
2264 // Properties
2265 this.$head = $( '<div>' );
2266 this.$body = $( '<div>' );
2267 this.$foot = $( '<div>' );
2268 this.$document = $( this.getElementDocument() );
2269
2270 // Events
2271 this.$element.on( 'mousedown', this.onMouseDown.bind( this ) );
2272
2273 // Initialization
2274 this.$head.addClass( 'oo-ui-window-head' );
2275 this.$body.addClass( 'oo-ui-window-body' );
2276 this.$foot.addClass( 'oo-ui-window-foot' );
2277 this.$content.append( this.$head, this.$body, this.$foot );
2278
2279 return this;
2280 };
2281
2282 /**
2283 * Called when someone tries to focus the hidden element at the end of the dialog.
2284 * Sends focus back to the start of the dialog.
2285 *
2286 * @param {jQuery.Event} event Focus event
2287 */
2288 OO.ui.Window.prototype.onFocusTrapFocused = function ( event ) {
2289 var backwards = this.$focusTrapBefore.is( event.target ),
2290 element = OO.ui.findFocusable( this.$content, backwards );
2291 if ( element ) {
2292 // There's a focusable element inside the content, at the front or
2293 // back depending on which focus trap we hit; select it.
2294 element.focus();
2295 } else {
2296 // There's nothing focusable inside the content. As a fallback,
2297 // this.$content is focusable, and focusing it will keep our focus
2298 // properly trapped. It's not a *meaningful* focus, since it's just
2299 // the content-div for the Window, but it's better than letting focus
2300 // escape into the page.
2301 this.$content.focus();
2302 }
2303 };
2304
2305 /**
2306 * Open the window.
2307 *
2308 * This method is a wrapper around a call to the window
2309 * manager’s {@link OO.ui.WindowManager#openWindow openWindow} method.
2310 *
2311 * To customize the window each time it opens, use #getSetupProcess or #getReadyProcess.
2312 *
2313 * @param {Object} [data] Window opening data
2314 * @return {OO.ui.WindowInstance} See OO.ui.WindowManager#openWindow
2315 * @throws {Error} An error is thrown if the window is not attached to a window manager
2316 */
2317 OO.ui.Window.prototype.open = function ( data ) {
2318 if ( !this.manager ) {
2319 throw new Error( 'Cannot open window, must be attached to a manager' );
2320 }
2321
2322 return this.manager.openWindow( this, data );
2323 };
2324
2325 /**
2326 * Close the window.
2327 *
2328 * This method is a wrapper around a call to the window
2329 * manager’s {@link OO.ui.WindowManager#closeWindow closeWindow} method.
2330 *
2331 * The window's #getHoldProcess and #getTeardownProcess methods are called during the closing
2332 * phase of the window’s lifecycle and can be used to specify closing behavior each time
2333 * the window closes.
2334 *
2335 * @param {Object} [data] Window closing data
2336 * @return {OO.ui.WindowInstance} See OO.ui.WindowManager#closeWindow
2337 * @throws {Error} An error is thrown if the window is not attached to a window manager
2338 */
2339 OO.ui.Window.prototype.close = function ( data ) {
2340 if ( !this.manager ) {
2341 throw new Error( 'Cannot close window, must be attached to a manager' );
2342 }
2343
2344 return this.manager.closeWindow( this, data );
2345 };
2346
2347 /**
2348 * Setup window.
2349 *
2350 * This is called by OO.ui.WindowManager during window opening (before the animation), and should
2351 * not be called directly by other systems.
2352 *
2353 * @param {Object} [data] Window opening data
2354 * @return {jQuery.Promise} Promise resolved when window is setup
2355 */
2356 OO.ui.Window.prototype.setup = function ( data ) {
2357 var win = this;
2358
2359 this.toggle( true );
2360
2361 this.focusTrapHandler = OO.ui.bind( this.onFocusTrapFocused, this );
2362 this.$focusTraps.on( 'focus', this.focusTrapHandler );
2363
2364 return this.getSetupProcess( data ).execute().then( function () {
2365 win.updateSize();
2366 // Force redraw by asking the browser to measure the elements' widths
2367 win.$element.addClass( 'oo-ui-window-active oo-ui-window-setup' ).width();
2368 win.$content.addClass( 'oo-ui-window-content-setup' ).width();
2369 } );
2370 };
2371
2372 /**
2373 * Ready window.
2374 *
2375 * This is called by OO.ui.WindowManager during window opening (after the animation), and should not
2376 * be called directly by other systems.
2377 *
2378 * @param {Object} [data] Window opening data
2379 * @return {jQuery.Promise} Promise resolved when window is ready
2380 */
2381 OO.ui.Window.prototype.ready = function ( data ) {
2382 var win = this;
2383
2384 this.$content.focus();
2385 return this.getReadyProcess( data ).execute().then( function () {
2386 // Force redraw by asking the browser to measure the elements' widths
2387 win.$element.addClass( 'oo-ui-window-ready' ).width();
2388 win.$content.addClass( 'oo-ui-window-content-ready' ).width();
2389 } );
2390 };
2391
2392 /**
2393 * Hold window.
2394 *
2395 * This is called by OO.ui.WindowManager during window closing (before the animation), and should
2396 * not be called directly by other systems.
2397 *
2398 * @param {Object} [data] Window closing data
2399 * @return {jQuery.Promise} Promise resolved when window is held
2400 */
2401 OO.ui.Window.prototype.hold = function ( data ) {
2402 var win = this;
2403
2404 return this.getHoldProcess( data ).execute().then( function () {
2405 // Get the focused element within the window's content
2406 var $focus = win.$content.find( OO.ui.Element.static.getDocument( win.$content ).activeElement );
2407
2408 // Blur the focused element
2409 if ( $focus.length ) {
2410 $focus[ 0 ].blur();
2411 }
2412
2413 // Force redraw by asking the browser to measure the elements' widths
2414 win.$element.removeClass( 'oo-ui-window-ready oo-ui-window-setup' ).width();
2415 win.$content.removeClass( 'oo-ui-window-content-ready oo-ui-window-content-setup' ).width();
2416 } );
2417 };
2418
2419 /**
2420 * Teardown window.
2421 *
2422 * This is called by OO.ui.WindowManager during window closing (after the animation), and should not be called directly
2423 * by other systems.
2424 *
2425 * @param {Object} [data] Window closing data
2426 * @return {jQuery.Promise} Promise resolved when window is torn down
2427 */
2428 OO.ui.Window.prototype.teardown = function ( data ) {
2429 var win = this;
2430
2431 return this.getTeardownProcess( data ).execute().then( function () {
2432 // Force redraw by asking the browser to measure the elements' widths
2433 win.$element.removeClass( 'oo-ui-window-active' ).width();
2434
2435 win.$focusTraps.off( 'focus', win.focusTrapHandler );
2436 win.toggle( false );
2437 } );
2438 };
2439
2440 /**
2441 * The Dialog class serves as the base class for the other types of dialogs.
2442 * Unless extended to include controls, the rendered dialog box is a simple window
2443 * that users can close by hitting the ‘Esc’ key. Dialog windows are used with OO.ui.WindowManager,
2444 * which opens, closes, and controls the presentation of the window. See the
2445 * [OOUI documentation on MediaWiki] [1] for more information.
2446 *
2447 * @example
2448 * // A simple dialog window.
2449 * function MyDialog( config ) {
2450 * MyDialog.parent.call( this, config );
2451 * }
2452 * OO.inheritClass( MyDialog, OO.ui.Dialog );
2453 * MyDialog.static.name = 'myDialog';
2454 * MyDialog.prototype.initialize = function () {
2455 * MyDialog.parent.prototype.initialize.call( this );
2456 * this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
2457 * this.content.$element.append( '<p>A simple dialog window. Press \'Esc\' to close.</p>' );
2458 * this.$body.append( this.content.$element );
2459 * };
2460 * MyDialog.prototype.getBodyHeight = function () {
2461 * return this.content.$element.outerHeight( true );
2462 * };
2463 * var myDialog = new MyDialog( {
2464 * size: 'medium'
2465 * } );
2466 * // Create and append a window manager, which opens and closes the window.
2467 * var windowManager = new OO.ui.WindowManager();
2468 * $( 'body' ).append( windowManager.$element );
2469 * windowManager.addWindows( [ myDialog ] );
2470 * // Open the window!
2471 * windowManager.openWindow( myDialog );
2472 *
2473 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Dialogs
2474 *
2475 * @abstract
2476 * @class
2477 * @extends OO.ui.Window
2478 * @mixins OO.ui.mixin.PendingElement
2479 *
2480 * @constructor
2481 * @param {Object} [config] Configuration options
2482 */
2483 OO.ui.Dialog = function OoUiDialog( config ) {
2484 // Parent constructor
2485 OO.ui.Dialog.parent.call( this, config );
2486
2487 // Mixin constructors
2488 OO.ui.mixin.PendingElement.call( this );
2489
2490 // Properties
2491 this.actions = new OO.ui.ActionSet();
2492 this.attachedActions = [];
2493 this.currentAction = null;
2494 this.onDialogKeyDownHandler = this.onDialogKeyDown.bind( this );
2495
2496 // Events
2497 this.actions.connect( this, {
2498 click: 'onActionClick',
2499 change: 'onActionsChange'
2500 } );
2501
2502 // Initialization
2503 this.$element
2504 .addClass( 'oo-ui-dialog' )
2505 .attr( 'role', 'dialog' );
2506 };
2507
2508 /* Setup */
2509
2510 OO.inheritClass( OO.ui.Dialog, OO.ui.Window );
2511 OO.mixinClass( OO.ui.Dialog, OO.ui.mixin.PendingElement );
2512
2513 /* Static Properties */
2514
2515 /**
2516 * Symbolic name of dialog.
2517 *
2518 * The dialog class must have a symbolic name in order to be registered with OO.Factory.
2519 * Please see the [OOUI documentation on MediaWiki] [3] for more information.
2520 *
2521 * [3]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
2522 *
2523 * @abstract
2524 * @static
2525 * @inheritable
2526 * @property {string}
2527 */
2528 OO.ui.Dialog.static.name = '';
2529
2530 /**
2531 * The dialog title.
2532 *
2533 * The title can be specified as a plaintext string, a {@link OO.ui.mixin.LabelElement Label} node, or a function
2534 * that will produce a Label node or string. The title can also be specified with data passed to the
2535 * constructor (see #getSetupProcess). In this case, the static value will be overridden.
2536 *
2537 * @abstract
2538 * @static
2539 * @inheritable
2540 * @property {jQuery|string|Function}
2541 */
2542 OO.ui.Dialog.static.title = '';
2543
2544 /**
2545 * An array of configured {@link OO.ui.ActionWidget action widgets}.
2546 *
2547 * Actions can also be specified with data passed to the constructor (see #getSetupProcess). In this case, the static
2548 * value will be overridden.
2549 *
2550 * [2]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Action_sets
2551 *
2552 * @static
2553 * @inheritable
2554 * @property {Object[]}
2555 */
2556 OO.ui.Dialog.static.actions = [];
2557
2558 /**
2559 * Close the dialog when the 'Esc' key is pressed.
2560 *
2561 * @static
2562 * @abstract
2563 * @inheritable
2564 * @property {boolean}
2565 */
2566 OO.ui.Dialog.static.escapable = true;
2567
2568 /* Methods */
2569
2570 /**
2571 * Handle frame document key down events.
2572 *
2573 * @private
2574 * @param {jQuery.Event} e Key down event
2575 */
2576 OO.ui.Dialog.prototype.onDialogKeyDown = function ( e ) {
2577 var actions;
2578 if ( e.which === OO.ui.Keys.ESCAPE && this.constructor.static.escapable ) {
2579 this.executeAction( '' );
2580 e.preventDefault();
2581 e.stopPropagation();
2582 } else if ( e.which === OO.ui.Keys.ENTER && ( e.ctrlKey || e.metaKey ) ) {
2583 actions = this.actions.get( { flags: 'primary', visible: true, disabled: false } );
2584 if ( actions.length > 0 ) {
2585 this.executeAction( actions[ 0 ].getAction() );
2586 e.preventDefault();
2587 e.stopPropagation();
2588 }
2589 }
2590 };
2591
2592 /**
2593 * Handle action click events.
2594 *
2595 * @private
2596 * @param {OO.ui.ActionWidget} action Action that was clicked
2597 */
2598 OO.ui.Dialog.prototype.onActionClick = function ( action ) {
2599 if ( !this.isPending() ) {
2600 this.executeAction( action.getAction() );
2601 }
2602 };
2603
2604 /**
2605 * Handle actions change event.
2606 *
2607 * @private
2608 */
2609 OO.ui.Dialog.prototype.onActionsChange = function () {
2610 this.detachActions();
2611 if ( !this.isClosing() ) {
2612 this.attachActions();
2613 if ( !this.isOpening() ) {
2614 // If the dialog is currently opening, this will be called automatically soon.
2615 this.updateSize();
2616 }
2617 }
2618 };
2619
2620 /**
2621 * Get the set of actions used by the dialog.
2622 *
2623 * @return {OO.ui.ActionSet}
2624 */
2625 OO.ui.Dialog.prototype.getActions = function () {
2626 return this.actions;
2627 };
2628
2629 /**
2630 * Get a process for taking action.
2631 *
2632 * When you override this method, you can create a new OO.ui.Process and return it, or add additional
2633 * accept steps to the process the parent method provides using the {@link OO.ui.Process#first 'first'}
2634 * and {@link OO.ui.Process#next 'next'} methods of OO.ui.Process.
2635 *
2636 * @param {string} [action] Symbolic name of action
2637 * @return {OO.ui.Process} Action process
2638 */
2639 OO.ui.Dialog.prototype.getActionProcess = function ( action ) {
2640 return new OO.ui.Process()
2641 .next( function () {
2642 if ( !action ) {
2643 // An empty action always closes the dialog without data, which should always be
2644 // safe and make no changes
2645 this.close();
2646 }
2647 }, this );
2648 };
2649
2650 /**
2651 * @inheritdoc
2652 *
2653 * @param {Object} [data] Dialog opening data
2654 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use
2655 * the {@link #static-title static title}
2656 * @param {Object[]} [data.actions] List of configuration options for each
2657 * {@link OO.ui.ActionWidget action widget}, omit to use {@link #static-actions static actions}.
2658 */
2659 OO.ui.Dialog.prototype.getSetupProcess = function ( data ) {
2660 data = data || {};
2661
2662 // Parent method
2663 return OO.ui.Dialog.parent.prototype.getSetupProcess.call( this, data )
2664 .next( function () {
2665 var config = this.constructor.static,
2666 actions = data.actions !== undefined ? data.actions : config.actions,
2667 title = data.title !== undefined ? data.title : config.title;
2668
2669 this.title.setLabel( title ).setTitle( title );
2670 this.actions.add( this.getActionWidgets( actions ) );
2671
2672 this.$element.on( 'keydown', this.onDialogKeyDownHandler );
2673 }, this );
2674 };
2675
2676 /**
2677 * @inheritdoc
2678 */
2679 OO.ui.Dialog.prototype.getTeardownProcess = function ( data ) {
2680 // Parent method
2681 return OO.ui.Dialog.parent.prototype.getTeardownProcess.call( this, data )
2682 .first( function () {
2683 this.$element.off( 'keydown', this.onDialogKeyDownHandler );
2684
2685 this.actions.clear();
2686 this.currentAction = null;
2687 }, this );
2688 };
2689
2690 /**
2691 * @inheritdoc
2692 */
2693 OO.ui.Dialog.prototype.initialize = function () {
2694 // Parent method
2695 OO.ui.Dialog.parent.prototype.initialize.call( this );
2696
2697 // Properties
2698 this.title = new OO.ui.LabelWidget();
2699
2700 // Initialization
2701 this.$content.addClass( 'oo-ui-dialog-content' );
2702 this.$element.attr( 'aria-labelledby', this.title.getElementId() );
2703 this.setPendingElement( this.$head );
2704 };
2705
2706 /**
2707 * Get action widgets from a list of configs
2708 *
2709 * @param {Object[]} actions Action widget configs
2710 * @return {OO.ui.ActionWidget[]} Action widgets
2711 */
2712 OO.ui.Dialog.prototype.getActionWidgets = function ( actions ) {
2713 var i, len, widgets = [];
2714 for ( i = 0, len = actions.length; i < len; i++ ) {
2715 widgets.push( this.getActionWidget( actions[ i ] ) );
2716 }
2717 return widgets;
2718 };
2719
2720 /**
2721 * Get action widget from config
2722 *
2723 * Override this method to change the action widget class used.
2724 *
2725 * @param {Object} config Action widget config
2726 * @return {OO.ui.ActionWidget} Action widget
2727 */
2728 OO.ui.Dialog.prototype.getActionWidget = function ( config ) {
2729 return new OO.ui.ActionWidget( this.getActionWidgetConfig( config ) );
2730 };
2731
2732 /**
2733 * Get action widget config
2734 *
2735 * Override this method to modify the action widget config
2736 *
2737 * @param {Object} config Initial action widget config
2738 * @return {Object} Action widget config
2739 */
2740 OO.ui.Dialog.prototype.getActionWidgetConfig = function ( config ) {
2741 return config;
2742 };
2743
2744 /**
2745 * Attach action actions.
2746 *
2747 * @protected
2748 */
2749 OO.ui.Dialog.prototype.attachActions = function () {
2750 // Remember the list of potentially attached actions
2751 this.attachedActions = this.actions.get();
2752 };
2753
2754 /**
2755 * Detach action actions.
2756 *
2757 * @protected
2758 * @chainable
2759 */
2760 OO.ui.Dialog.prototype.detachActions = function () {
2761 var i, len;
2762
2763 // Detach all actions that may have been previously attached
2764 for ( i = 0, len = this.attachedActions.length; i < len; i++ ) {
2765 this.attachedActions[ i ].$element.detach();
2766 }
2767 this.attachedActions = [];
2768 };
2769
2770 /**
2771 * Execute an action.
2772 *
2773 * @param {string} action Symbolic name of action to execute
2774 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2775 */
2776 OO.ui.Dialog.prototype.executeAction = function ( action ) {
2777 this.pushPending();
2778 this.currentAction = action;
2779 return this.getActionProcess( action ).execute()
2780 .always( this.popPending.bind( this ) );
2781 };
2782
2783 /**
2784 * MessageDialogs display a confirmation or alert message. By default, the rendered dialog box
2785 * consists of a header that contains the dialog title, a body with the message, and a footer that
2786 * contains any {@link OO.ui.ActionWidget action widgets}. The MessageDialog class is the only type
2787 * of {@link OO.ui.Dialog dialog} that is usually instantiated directly.
2788 *
2789 * There are two basic types of message dialogs, confirmation and alert:
2790 *
2791 * - **confirmation**: the dialog title describes what a progressive action will do and the message provides
2792 * more details about the consequences.
2793 * - **alert**: the dialog title describes which event occurred and the message provides more information
2794 * about why the event occurred.
2795 *
2796 * The MessageDialog class specifies two actions: ‘accept’, the primary
2797 * action (e.g., ‘ok’) and ‘reject,’ the safe action (e.g., ‘cancel’). Both will close the window,
2798 * passing along the selected action.
2799 *
2800 * For more information and examples, please see the [OOUI documentation on MediaWiki][1].
2801 *
2802 * @example
2803 * // Example: Creating and opening a message dialog window.
2804 * var messageDialog = new OO.ui.MessageDialog();
2805 *
2806 * // Create and append a window manager.
2807 * var windowManager = new OO.ui.WindowManager();
2808 * $( 'body' ).append( windowManager.$element );
2809 * windowManager.addWindows( [ messageDialog ] );
2810 * // Open the window.
2811 * windowManager.openWindow( messageDialog, {
2812 * title: 'Basic message dialog',
2813 * message: 'This is the message'
2814 * } );
2815 *
2816 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Message_Dialogs
2817 *
2818 * @class
2819 * @extends OO.ui.Dialog
2820 *
2821 * @constructor
2822 * @param {Object} [config] Configuration options
2823 */
2824 OO.ui.MessageDialog = function OoUiMessageDialog( config ) {
2825 // Parent constructor
2826 OO.ui.MessageDialog.parent.call( this, config );
2827
2828 // Properties
2829 this.verticalActionLayout = null;
2830
2831 // Initialization
2832 this.$element.addClass( 'oo-ui-messageDialog' );
2833 };
2834
2835 /* Setup */
2836
2837 OO.inheritClass( OO.ui.MessageDialog, OO.ui.Dialog );
2838
2839 /* Static Properties */
2840
2841 /**
2842 * @static
2843 * @inheritdoc
2844 */
2845 OO.ui.MessageDialog.static.name = 'message';
2846
2847 /**
2848 * @static
2849 * @inheritdoc
2850 */
2851 OO.ui.MessageDialog.static.size = 'small';
2852
2853 /**
2854 * Dialog title.
2855 *
2856 * The title of a confirmation dialog describes what a progressive action will do. The
2857 * title of an alert dialog describes which event occurred.
2858 *
2859 * @static
2860 * @inheritable
2861 * @property {jQuery|string|Function|null}
2862 */
2863 OO.ui.MessageDialog.static.title = null;
2864
2865 /**
2866 * The message displayed in the dialog body.
2867 *
2868 * A confirmation message describes the consequences of a progressive action. An alert
2869 * message describes why an event occurred.
2870 *
2871 * @static
2872 * @inheritable
2873 * @property {jQuery|string|Function|null}
2874 */
2875 OO.ui.MessageDialog.static.message = null;
2876
2877 /**
2878 * @static
2879 * @inheritdoc
2880 */
2881 OO.ui.MessageDialog.static.actions = [
2882 // Note that OO.ui.alert() and OO.ui.confirm() rely on these.
2883 { action: 'accept', label: OO.ui.deferMsg( 'ooui-dialog-message-accept' ), flags: 'primary' },
2884 { action: 'reject', label: OO.ui.deferMsg( 'ooui-dialog-message-reject' ), flags: 'safe' }
2885 ];
2886
2887 /* Methods */
2888
2889 /**
2890 * Toggle action layout between vertical and horizontal.
2891 *
2892 * @private
2893 * @param {boolean} [value] Layout actions vertically, omit to toggle
2894 * @chainable
2895 */
2896 OO.ui.MessageDialog.prototype.toggleVerticalActionLayout = function ( value ) {
2897 value = value === undefined ? !this.verticalActionLayout : !!value;
2898
2899 if ( value !== this.verticalActionLayout ) {
2900 this.verticalActionLayout = value;
2901 this.$actions
2902 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value )
2903 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value );
2904 }
2905
2906 return this;
2907 };
2908
2909 /**
2910 * @inheritdoc
2911 */
2912 OO.ui.MessageDialog.prototype.getActionProcess = function ( action ) {
2913 if ( action ) {
2914 return new OO.ui.Process( function () {
2915 this.close( { action: action } );
2916 }, this );
2917 }
2918 return OO.ui.MessageDialog.parent.prototype.getActionProcess.call( this, action );
2919 };
2920
2921 /**
2922 * @inheritdoc
2923 *
2924 * @param {Object} [data] Dialog opening data
2925 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
2926 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
2927 * @param {string} [data.size] Symbolic name of the dialog size, see OO.ui.Window
2928 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
2929 * action item
2930 */
2931 OO.ui.MessageDialog.prototype.getSetupProcess = function ( data ) {
2932 data = data || {};
2933
2934 // Parent method
2935 return OO.ui.MessageDialog.parent.prototype.getSetupProcess.call( this, data )
2936 .next( function () {
2937 this.title.setLabel(
2938 data.title !== undefined ? data.title : this.constructor.static.title
2939 );
2940 this.message.setLabel(
2941 data.message !== undefined ? data.message : this.constructor.static.message
2942 );
2943 this.size = data.size !== undefined ? data.size : this.constructor.static.size;
2944 }, this );
2945 };
2946
2947 /**
2948 * @inheritdoc
2949 */
2950 OO.ui.MessageDialog.prototype.getReadyProcess = function ( data ) {
2951 data = data || {};
2952
2953 // Parent method
2954 return OO.ui.MessageDialog.parent.prototype.getReadyProcess.call( this, data )
2955 .next( function () {
2956 // Focus the primary action button
2957 var actions = this.actions.get();
2958 actions = actions.filter( function ( action ) {
2959 return action.getFlags().indexOf( 'primary' ) > -1;
2960 } );
2961 if ( actions.length > 0 ) {
2962 actions[ 0 ].focus();
2963 }
2964 }, this );
2965 };
2966
2967 /**
2968 * @inheritdoc
2969 */
2970 OO.ui.MessageDialog.prototype.getBodyHeight = function () {
2971 var bodyHeight, oldOverflow,
2972 $scrollable = this.container.$element;
2973
2974 oldOverflow = $scrollable[ 0 ].style.overflow;
2975 $scrollable[ 0 ].style.overflow = 'hidden';
2976
2977 OO.ui.Element.static.reconsiderScrollbars( $scrollable[ 0 ] );
2978
2979 bodyHeight = this.text.$element.outerHeight( true );
2980 $scrollable[ 0 ].style.overflow = oldOverflow;
2981
2982 return bodyHeight;
2983 };
2984
2985 /**
2986 * @inheritdoc
2987 */
2988 OO.ui.MessageDialog.prototype.setDimensions = function ( dim ) {
2989 var
2990 dialog = this,
2991 $scrollable = this.container.$element;
2992 OO.ui.MessageDialog.parent.prototype.setDimensions.call( this, dim );
2993
2994 // Twiddle the overflow property, otherwise an unnecessary scrollbar will be produced.
2995 // Need to do it after transition completes (250ms), add 50ms just in case.
2996 setTimeout( function () {
2997 var oldOverflow = $scrollable[ 0 ].style.overflow,
2998 activeElement = document.activeElement;
2999
3000 $scrollable[ 0 ].style.overflow = 'hidden';
3001
3002 OO.ui.Element.static.reconsiderScrollbars( $scrollable[ 0 ] );
3003
3004 // Check reconsiderScrollbars didn't destroy our focus, as we
3005 // are doing this after the ready process.
3006 if ( activeElement && activeElement !== document.activeElement && activeElement.focus ) {
3007 activeElement.focus();
3008 }
3009
3010 $scrollable[ 0 ].style.overflow = oldOverflow;
3011 }, 300 );
3012
3013 dialog.fitActions();
3014 // Wait for CSS transition to finish and do it again :(
3015 setTimeout( function () {
3016 dialog.fitActions();
3017 }, 300 );
3018
3019 return this;
3020 };
3021
3022 /**
3023 * @inheritdoc
3024 */
3025 OO.ui.MessageDialog.prototype.initialize = function () {
3026 // Parent method
3027 OO.ui.MessageDialog.parent.prototype.initialize.call( this );
3028
3029 // Properties
3030 this.$actions = $( '<div>' );
3031 this.container = new OO.ui.PanelLayout( {
3032 scrollable: true, classes: [ 'oo-ui-messageDialog-container' ]
3033 } );
3034 this.text = new OO.ui.PanelLayout( {
3035 padded: true, expanded: false, classes: [ 'oo-ui-messageDialog-text' ]
3036 } );
3037 this.message = new OO.ui.LabelWidget( {
3038 classes: [ 'oo-ui-messageDialog-message' ]
3039 } );
3040
3041 // Initialization
3042 this.title.$element.addClass( 'oo-ui-messageDialog-title' );
3043 this.$content.addClass( 'oo-ui-messageDialog-content' );
3044 this.container.$element.append( this.text.$element );
3045 this.text.$element.append( this.title.$element, this.message.$element );
3046 this.$body.append( this.container.$element );
3047 this.$actions.addClass( 'oo-ui-messageDialog-actions' );
3048 this.$foot.append( this.$actions );
3049 };
3050
3051 /**
3052 * @inheritdoc
3053 */
3054 OO.ui.MessageDialog.prototype.getActionWidgetConfig = function ( config ) {
3055 // Force unframed
3056 return $.extend( {}, config, { framed: false } );
3057 };
3058
3059 /**
3060 * @inheritdoc
3061 */
3062 OO.ui.MessageDialog.prototype.attachActions = function () {
3063 var i, len, special, others;
3064
3065 // Parent method
3066 OO.ui.MessageDialog.parent.prototype.attachActions.call( this );
3067
3068 special = this.actions.getSpecial();
3069 others = this.actions.getOthers();
3070
3071 if ( special.safe ) {
3072 this.$actions.append( special.safe.$element );
3073 special.safe.toggleFramed( true );
3074 }
3075 for ( i = 0, len = others.length; i < len; i++ ) {
3076 this.$actions.append( others[ i ].$element );
3077 others[ i ].toggleFramed( true );
3078 }
3079 if ( special.primary ) {
3080 this.$actions.append( special.primary.$element );
3081 special.primary.toggleFramed( true );
3082 }
3083 };
3084
3085 /**
3086 * Fit action actions into columns or rows.
3087 *
3088 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
3089 *
3090 * @private
3091 */
3092 OO.ui.MessageDialog.prototype.fitActions = function () {
3093 var i, len, action,
3094 previous = this.verticalActionLayout,
3095 actions = this.actions.get();
3096
3097 // Detect clipping
3098 this.toggleVerticalActionLayout( false );
3099 for ( i = 0, len = actions.length; i < len; i++ ) {
3100 action = actions[ i ];
3101 if ( action.$element[ 0 ].scrollWidth > action.$element[ 0 ].clientWidth ) {
3102 this.toggleVerticalActionLayout( true );
3103 break;
3104 }
3105 }
3106
3107 // Move the body out of the way of the foot
3108 this.$body.css( 'bottom', this.$foot.outerHeight( true ) );
3109
3110 if ( this.verticalActionLayout !== previous ) {
3111 // We changed the layout, window height might need to be updated.
3112 this.updateSize();
3113 }
3114 };
3115
3116 /**
3117 * ProcessDialog windows encapsulate a {@link OO.ui.Process process} and all of the code necessary
3118 * to complete it. If the process terminates with an error, a customizable {@link OO.ui.Error error
3119 * interface} alerts users to the trouble, permitting the user to dismiss the error and try again when
3120 * relevant. The ProcessDialog class is always extended and customized with the actions and content
3121 * required for each process.
3122 *
3123 * The process dialog box consists of a header that visually represents the ‘working’ state of long
3124 * processes with an animation. The header contains the dialog title as well as
3125 * two {@link OO.ui.ActionWidget action widgets}: a ‘safe’ action on the left (e.g., ‘Cancel’) and
3126 * a ‘primary’ action on the right (e.g., ‘Done’).
3127 *
3128 * Like other windows, the process dialog is managed by a {@link OO.ui.WindowManager window manager}.
3129 * Please see the [OOUI documentation on MediaWiki][1] for more information and examples.
3130 *
3131 * @example
3132 * // Example: Creating and opening a process dialog window.
3133 * function MyProcessDialog( config ) {
3134 * MyProcessDialog.parent.call( this, config );
3135 * }
3136 * OO.inheritClass( MyProcessDialog, OO.ui.ProcessDialog );
3137 *
3138 * MyProcessDialog.static.name = 'myProcessDialog';
3139 * MyProcessDialog.static.title = 'Process dialog';
3140 * MyProcessDialog.static.actions = [
3141 * { action: 'save', label: 'Done', flags: 'primary' },
3142 * { label: 'Cancel', flags: 'safe' }
3143 * ];
3144 *
3145 * MyProcessDialog.prototype.initialize = function () {
3146 * MyProcessDialog.parent.prototype.initialize.apply( this, arguments );
3147 * this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
3148 * this.content.$element.append( '<p>This is a process dialog window. The header contains the title and two buttons: \'Cancel\' (a safe action) on the left and \'Done\' (a primary action) on the right.</p>' );
3149 * this.$body.append( this.content.$element );
3150 * };
3151 * MyProcessDialog.prototype.getActionProcess = function ( action ) {
3152 * var dialog = this;
3153 * if ( action ) {
3154 * return new OO.ui.Process( function () {
3155 * dialog.close( { action: action } );
3156 * } );
3157 * }
3158 * return MyProcessDialog.parent.prototype.getActionProcess.call( this, action );
3159 * };
3160 *
3161 * var windowManager = new OO.ui.WindowManager();
3162 * $( 'body' ).append( windowManager.$element );
3163 *
3164 * var dialog = new MyProcessDialog();
3165 * windowManager.addWindows( [ dialog ] );
3166 * windowManager.openWindow( dialog );
3167 *
3168 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs
3169 *
3170 * @abstract
3171 * @class
3172 * @extends OO.ui.Dialog
3173 *
3174 * @constructor
3175 * @param {Object} [config] Configuration options
3176 */
3177 OO.ui.ProcessDialog = function OoUiProcessDialog( config ) {
3178 // Parent constructor
3179 OO.ui.ProcessDialog.parent.call( this, config );
3180
3181 // Properties
3182 this.fitOnOpen = false;
3183
3184 // Initialization
3185 this.$element.addClass( 'oo-ui-processDialog' );
3186 };
3187
3188 /* Setup */
3189
3190 OO.inheritClass( OO.ui.ProcessDialog, OO.ui.Dialog );
3191
3192 /* Methods */
3193
3194 /**
3195 * Handle dismiss button click events.
3196 *
3197 * Hides errors.
3198 *
3199 * @private
3200 */
3201 OO.ui.ProcessDialog.prototype.onDismissErrorButtonClick = function () {
3202 this.hideErrors();
3203 };
3204
3205 /**
3206 * Handle retry button click events.
3207 *
3208 * Hides errors and then tries again.
3209 *
3210 * @private
3211 */
3212 OO.ui.ProcessDialog.prototype.onRetryButtonClick = function () {
3213 this.hideErrors();
3214 this.executeAction( this.currentAction );
3215 };
3216
3217 /**
3218 * @inheritdoc
3219 */
3220 OO.ui.ProcessDialog.prototype.initialize = function () {
3221 // Parent method
3222 OO.ui.ProcessDialog.parent.prototype.initialize.call( this );
3223
3224 // Properties
3225 this.$navigation = $( '<div>' );
3226 this.$location = $( '<div>' );
3227 this.$safeActions = $( '<div>' );
3228 this.$primaryActions = $( '<div>' );
3229 this.$otherActions = $( '<div>' );
3230 this.dismissButton = new OO.ui.ButtonWidget( {
3231 label: OO.ui.msg( 'ooui-dialog-process-dismiss' )
3232 } );
3233 this.retryButton = new OO.ui.ButtonWidget();
3234 this.$errors = $( '<div>' );
3235 this.$errorsTitle = $( '<div>' );
3236
3237 // Events
3238 this.dismissButton.connect( this, { click: 'onDismissErrorButtonClick' } );
3239 this.retryButton.connect( this, { click: 'onRetryButtonClick' } );
3240
3241 // Initialization
3242 this.title.$element.addClass( 'oo-ui-processDialog-title' );
3243 this.$location
3244 .append( this.title.$element )
3245 .addClass( 'oo-ui-processDialog-location' );
3246 this.$safeActions.addClass( 'oo-ui-processDialog-actions-safe' );
3247 this.$primaryActions.addClass( 'oo-ui-processDialog-actions-primary' );
3248 this.$otherActions.addClass( 'oo-ui-processDialog-actions-other' );
3249 this.$errorsTitle
3250 .addClass( 'oo-ui-processDialog-errors-title' )
3251 .text( OO.ui.msg( 'ooui-dialog-process-error' ) );
3252 this.$errors
3253 .addClass( 'oo-ui-processDialog-errors oo-ui-element-hidden' )
3254 .append( this.$errorsTitle, this.dismissButton.$element, this.retryButton.$element );
3255 this.$content
3256 .addClass( 'oo-ui-processDialog-content' )
3257 .append( this.$errors );
3258 this.$navigation
3259 .addClass( 'oo-ui-processDialog-navigation' )
3260 // Note: Order of appends below is important. These are in the order
3261 // we want tab to go through them. Display-order is handled entirely
3262 // by CSS absolute-positioning. As such, primary actions like "done"
3263 // should go first.
3264 .append( this.$primaryActions, this.$location, this.$safeActions );
3265 this.$head.append( this.$navigation );
3266 this.$foot.append( this.$otherActions );
3267 };
3268
3269 /**
3270 * @inheritdoc
3271 */
3272 OO.ui.ProcessDialog.prototype.getActionWidgetConfig = function ( config ) {
3273 var isMobile = OO.ui.isMobile();
3274
3275 // Default to unframed on mobile
3276 config = $.extend( { framed: !isMobile }, config );
3277 // Change back buttons to icon only on mobile
3278 if (
3279 isMobile &&
3280 ( config.flags === 'back' || ( Array.isArray( config.flags ) && config.flags.indexOf( 'back' ) !== -1 ) )
3281 ) {
3282 $.extend( config, {
3283 icon: 'previous',
3284 label: ''
3285 } );
3286 }
3287
3288 return config;
3289 };
3290
3291 /**
3292 * @inheritdoc
3293 */
3294 OO.ui.ProcessDialog.prototype.attachActions = function () {
3295 var i, len, other, special, others;
3296
3297 // Parent method
3298 OO.ui.ProcessDialog.parent.prototype.attachActions.call( this );
3299
3300 special = this.actions.getSpecial();
3301 others = this.actions.getOthers();
3302 if ( special.primary ) {
3303 this.$primaryActions.append( special.primary.$element );
3304 }
3305 for ( i = 0, len = others.length; i < len; i++ ) {
3306 other = others[ i ];
3307 this.$otherActions.append( other.$element );
3308 }
3309 if ( special.safe ) {
3310 this.$safeActions.append( special.safe.$element );
3311 }
3312 };
3313
3314 /**
3315 * @inheritdoc
3316 */
3317 OO.ui.ProcessDialog.prototype.executeAction = function ( action ) {
3318 var process = this;
3319 return OO.ui.ProcessDialog.parent.prototype.executeAction.call( this, action )
3320 .fail( function ( errors ) {
3321 process.showErrors( errors || [] );
3322 } );
3323 };
3324
3325 /**
3326 * @inheritdoc
3327 */
3328 OO.ui.ProcessDialog.prototype.setDimensions = function () {
3329 var dialog = this;
3330
3331 // Parent method
3332 OO.ui.ProcessDialog.parent.prototype.setDimensions.apply( this, arguments );
3333
3334 this.fitLabel();
3335
3336 // If there are many actions, they might be shown on multiple lines. Their layout can change when
3337 // resizing the dialog and when changing the actions. Adjust the height of the footer to fit them.
3338 dialog.$body.css( 'bottom', dialog.$foot.outerHeight( true ) );
3339 // Wait for CSS transition to finish and do it again :(
3340 setTimeout( function () {
3341 dialog.$body.css( 'bottom', dialog.$foot.outerHeight( true ) );
3342 }, 300 );
3343 };
3344
3345 /**
3346 * Fit label between actions.
3347 *
3348 * @private
3349 * @chainable
3350 */
3351 OO.ui.ProcessDialog.prototype.fitLabel = function () {
3352 var safeWidth, primaryWidth, biggerWidth, labelWidth, navigationWidth, leftWidth, rightWidth,
3353 size = this.getSizeProperties();
3354
3355 if ( typeof size.width !== 'number' ) {
3356 if ( this.isOpened() ) {
3357 navigationWidth = this.$head.width() - 20;
3358 } else if ( this.isOpening() ) {
3359 if ( !this.fitOnOpen ) {
3360 // Size is relative and the dialog isn't open yet, so wait.
3361 // FIXME: This should ideally be handled by setup somehow.
3362 this.manager.lifecycle.opened.done( this.fitLabel.bind( this ) );
3363 this.fitOnOpen = true;
3364 }
3365 return;
3366 } else {
3367 return;
3368 }
3369 } else {
3370 navigationWidth = size.width - 20;
3371 }
3372
3373 safeWidth = this.$safeActions.is( ':visible' ) ? this.$safeActions.width() : 0;
3374 primaryWidth = this.$primaryActions.is( ':visible' ) ? this.$primaryActions.width() : 0;
3375 biggerWidth = Math.max( safeWidth, primaryWidth );
3376
3377 labelWidth = this.title.$element.width();
3378
3379 if ( 2 * biggerWidth + labelWidth < navigationWidth ) {
3380 // We have enough space to center the label
3381 leftWidth = rightWidth = biggerWidth;
3382 } else {
3383 // Let's hope we at least have enough space not to overlap, because we can't wrap the label…
3384 if ( this.getDir() === 'ltr' ) {
3385 leftWidth = safeWidth;
3386 rightWidth = primaryWidth;
3387 } else {
3388 leftWidth = primaryWidth;
3389 rightWidth = safeWidth;
3390 }
3391 }
3392
3393 this.$location.css( { paddingLeft: leftWidth, paddingRight: rightWidth } );
3394
3395 return this;
3396 };
3397
3398 /**
3399 * Handle errors that occurred during accept or reject processes.
3400 *
3401 * @private
3402 * @param {OO.ui.Error[]|OO.ui.Error} errors Errors to be handled
3403 */
3404 OO.ui.ProcessDialog.prototype.showErrors = function ( errors ) {
3405 var i, len, $item, actions,
3406 items = [],
3407 abilities = {},
3408 recoverable = true,
3409 warning = false;
3410
3411 if ( errors instanceof OO.ui.Error ) {
3412 errors = [ errors ];
3413 }
3414
3415 for ( i = 0, len = errors.length; i < len; i++ ) {
3416 if ( !errors[ i ].isRecoverable() ) {
3417 recoverable = false;
3418 }
3419 if ( errors[ i ].isWarning() ) {
3420 warning = true;
3421 }
3422 $item = $( '<div>' )
3423 .addClass( 'oo-ui-processDialog-error' )
3424 .append( errors[ i ].getMessage() );
3425 items.push( $item[ 0 ] );
3426 }
3427 this.$errorItems = $( items );
3428 if ( recoverable ) {
3429 abilities[ this.currentAction ] = true;
3430 // Copy the flags from the first matching action
3431 actions = this.actions.get( { actions: this.currentAction } );
3432 if ( actions.length ) {
3433 this.retryButton.clearFlags().setFlags( actions[ 0 ].getFlags() );
3434 }
3435 } else {
3436 abilities[ this.currentAction ] = false;
3437 this.actions.setAbilities( abilities );
3438 }
3439 if ( warning ) {
3440 this.retryButton.setLabel( OO.ui.msg( 'ooui-dialog-process-continue' ) );
3441 } else {
3442 this.retryButton.setLabel( OO.ui.msg( 'ooui-dialog-process-retry' ) );
3443 }
3444 this.retryButton.toggle( recoverable );
3445 this.$errorsTitle.after( this.$errorItems );
3446 this.$errors.removeClass( 'oo-ui-element-hidden' ).scrollTop( 0 );
3447 };
3448
3449 /**
3450 * Hide errors.
3451 *
3452 * @private
3453 */
3454 OO.ui.ProcessDialog.prototype.hideErrors = function () {
3455 this.$errors.addClass( 'oo-ui-element-hidden' );
3456 if ( this.$errorItems ) {
3457 this.$errorItems.remove();
3458 this.$errorItems = null;
3459 }
3460 };
3461
3462 /**
3463 * @inheritdoc
3464 */
3465 OO.ui.ProcessDialog.prototype.getTeardownProcess = function ( data ) {
3466 // Parent method
3467 return OO.ui.ProcessDialog.parent.prototype.getTeardownProcess.call( this, data )
3468 .first( function () {
3469 // Make sure to hide errors
3470 this.hideErrors();
3471 this.fitOnOpen = false;
3472 }, this );
3473 };
3474
3475 /**
3476 * @class OO.ui
3477 */
3478
3479 /**
3480 * Lazy-initialize and return a global OO.ui.WindowManager instance, used by OO.ui.alert and
3481 * OO.ui.confirm.
3482 *
3483 * @private
3484 * @return {OO.ui.WindowManager}
3485 */
3486 OO.ui.getWindowManager = function () {
3487 if ( !OO.ui.windowManager ) {
3488 OO.ui.windowManager = new OO.ui.WindowManager();
3489 $( 'body' ).append( OO.ui.windowManager.$element );
3490 OO.ui.windowManager.addWindows( [ new OO.ui.MessageDialog() ] );
3491 }
3492 return OO.ui.windowManager;
3493 };
3494
3495 /**
3496 * Display a quick modal alert dialog, using a OO.ui.MessageDialog. While the dialog is open, the
3497 * rest of the page will be dimmed out and the user won't be able to interact with it. The dialog
3498 * has only one action button, labelled "OK", clicking it will simply close the dialog.
3499 *
3500 * A window manager is created automatically when this function is called for the first time.
3501 *
3502 * @example
3503 * OO.ui.alert( 'Something happened!' ).done( function () {
3504 * console.log( 'User closed the dialog.' );
3505 * } );
3506 *
3507 * OO.ui.alert( 'Something larger happened!', { size: 'large' } );
3508 *
3509 * @param {jQuery|string} text Message text to display
3510 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3511 * @return {jQuery.Promise} Promise resolved when the user closes the dialog
3512 */
3513 OO.ui.alert = function ( text, options ) {
3514 return OO.ui.getWindowManager().openWindow( 'message', $.extend( {
3515 message: text,
3516 actions: [ OO.ui.MessageDialog.static.actions[ 0 ] ]
3517 }, options ) ).closed.then( function () {
3518 return undefined;
3519 } );
3520 };
3521
3522 /**
3523 * Display a quick modal confirmation dialog, using a OO.ui.MessageDialog. While the dialog is open,
3524 * the rest of the page will be dimmed out and the user won't be able to interact with it. The
3525 * dialog has two action buttons, one to confirm an operation (labelled "OK") and one to cancel it
3526 * (labelled "Cancel").
3527 *
3528 * A window manager is created automatically when this function is called for the first time.
3529 *
3530 * @example
3531 * OO.ui.confirm( 'Are you sure?' ).done( function ( confirmed ) {
3532 * if ( confirmed ) {
3533 * console.log( 'User clicked "OK"!' );
3534 * } else {
3535 * console.log( 'User clicked "Cancel" or closed the dialog.' );
3536 * }
3537 * } );
3538 *
3539 * @param {jQuery|string} text Message text to display
3540 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3541 * @return {jQuery.Promise} Promise resolved when the user closes the dialog. If the user chose to
3542 * confirm, the promise will resolve to boolean `true`; otherwise, it will resolve to boolean
3543 * `false`.
3544 */
3545 OO.ui.confirm = function ( text, options ) {
3546 return OO.ui.getWindowManager().openWindow( 'message', $.extend( {
3547 message: text
3548 }, options ) ).closed.then( function ( data ) {
3549 return !!( data && data.action === 'accept' );
3550 } );
3551 };
3552
3553 /**
3554 * Display a quick modal prompt dialog, using a OO.ui.MessageDialog. While the dialog is open,
3555 * the rest of the page will be dimmed out and the user won't be able to interact with it. The
3556 * dialog has a text input widget and two action buttons, one to confirm an operation (labelled "OK")
3557 * and one to cancel it (labelled "Cancel").
3558 *
3559 * A window manager is created automatically when this function is called for the first time.
3560 *
3561 * @example
3562 * OO.ui.prompt( 'Choose a line to go to', { textInput: { placeholder: 'Line number' } } ).done( function ( result ) {
3563 * if ( result !== null ) {
3564 * console.log( 'User typed "' + result + '" then clicked "OK".' );
3565 * } else {
3566 * console.log( 'User clicked "Cancel" or closed the dialog.' );
3567 * }
3568 * } );
3569 *
3570 * @param {jQuery|string} text Message text to display
3571 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3572 * @param {Object} [options.textInput] Additional options for text input widget, see OO.ui.TextInputWidget
3573 * @return {jQuery.Promise} Promise resolved when the user closes the dialog. If the user chose to
3574 * confirm, the promise will resolve with the value of the text input widget; otherwise, it will
3575 * resolve to `null`.
3576 */
3577 OO.ui.prompt = function ( text, options ) {
3578 var instance,
3579 manager = OO.ui.getWindowManager(),
3580 textInput = new OO.ui.TextInputWidget( ( options && options.textInput ) || {} ),
3581 textField = new OO.ui.FieldLayout( textInput, {
3582 align: 'top',
3583 label: text
3584 } );
3585
3586 instance = manager.openWindow( 'message', $.extend( {
3587 message: textField.$element
3588 }, options ) );
3589
3590 // TODO: This is a little hacky, and could be done by extending MessageDialog instead.
3591 instance.opened.then( function () {
3592 textInput.on( 'enter', function () {
3593 manager.getCurrentWindow().close( { action: 'accept' } );
3594 } );
3595 textInput.focus();
3596 } );
3597
3598 return instance.closed.then( function ( data ) {
3599 return data && data.action === 'accept' ? textInput.getValue() : null;
3600 } );
3601 };
3602
3603 }( OO ) );
3604
3605 //# sourceMappingURL=oojs-ui-windows.js.map.json