3 * https://www.mediawiki.org/wiki/OOUI
5 * Copyright 2011–2018 OOUI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2018-04-17T22:23:58Z
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
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
24 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Action_sets
27 * @extends OO.ui.ButtonWidget
28 * @mixins OO.ui.mixin.PendingElement
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
38 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
39 // Configuration initialization
40 config
= $.extend( { framed
: false }, config
);
43 OO
.ui
.ActionWidget
.parent
.call( this, config
);
46 OO
.ui
.mixin
.PendingElement
.call( this, config
);
49 this.action
= config
.action
|| '';
50 this.modes
= config
.modes
|| [];
55 this.$element
.addClass( 'oo-ui-actionWidget' );
60 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
61 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.mixin
.PendingElement
);
66 * Check if the action is configured to be available in the specified `mode`.
68 * @param {string} mode Name of mode
69 * @return {boolean} The action is configured with the mode
71 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
72 return this.modes
.indexOf( mode
) !== -1;
76 * Get the symbolic name of the action (e.g., ‘continue’ or ‘cancel’).
80 OO
.ui
.ActionWidget
.prototype.getAction = function () {
85 * Get the symbolic name of the mode or modes for which the action is configured to be available.
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
93 OO
.ui
.ActionWidget
.prototype.getModes = function () {
94 return this.modes
.slice();
97 /* eslint-disable no-unused-vars */
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}.
103 * ActionSets contain two types of actions:
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.
108 * See the [OOUI documentation on MediaWiki][1] for more information.
111 * // Example: An action set used in a process dialog
112 * function MyProcessDialog( config ) {
113 * MyProcessDialog.parent.call( this, config );
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' }
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 ]
135 * this.$body.append( this.stackLayout.$element );
137 * MyProcessDialog.prototype.getSetupProcess = function ( data ) {
138 * return MyProcessDialog.parent.prototype.getSetupProcess.call( this, data )
139 * .next( function () {
140 * this.actions.setMode( 'edit' );
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' ) {
152 * return new OO.ui.Process( function () {
156 * return MyProcessDialog.parent.prototype.getActionProcess.call( this, action );
158 * MyProcessDialog.prototype.getBodyHeight = function () {
159 * return this.panel1.$element.outerHeight( true );
161 * var windowManager = new OO.ui.WindowManager();
162 * $( 'body' ).append( windowManager.$element );
163 * var dialog = new MyProcessDialog( {
166 * windowManager.addWindows( [ dialog ] );
167 * windowManager.openWindow( dialog );
169 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Action_sets
173 * @mixins OO.EventEmitter
176 * @param {Object} [config] Configuration options
178 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
179 // Configuration initialization
180 config
= config
|| {};
182 // Mixin constructors
183 OO
.EventEmitter
.call( this );
188 actions
: 'getAction',
192 this.categorized
= {};
195 this.organized
= false;
196 this.changing
= false;
197 this.changed
= false;
199 /* eslint-enable no-unused-vars */
203 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
205 /* Static Properties */
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.
212 * [2]:https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs
219 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
226 * A 'click' event is emitted when an action is clicked.
228 * @param {OO.ui.ActionWidget} action Action that was clicked
234 * An 'add' event is emitted when actions are {@link #method-add added} to the action set.
236 * @param {OO.ui.ActionWidget[]} added Actions added
242 * A 'remove' event is emitted when actions are {@link #method-remove removed}
243 * or {@link #clear cleared}.
245 * @param {OO.ui.ActionWidget[]} added Actions removed
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.
259 * Handle action change events.
264 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
265 this.organized
= false;
266 if ( this.changing
) {
269 this.emit( 'change' );
274 * Check if an action is one of the special actions.
276 * @param {OO.ui.ActionWidget} action Action to check
277 * @return {boolean} Action is special
279 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
282 for ( flag
in this.special
) {
283 if ( action
=== this.special
[ flag
] ) {
292 * Get action widgets based on the specified filter: ‘actions’, ‘flags’, ‘modes’, ‘visible’,
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
303 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
304 var i
, len
, list
, category
, actions
, index
, match
, matches
;
309 // Collect category candidates
311 for ( category
in this.categorized
) {
312 list
= filters
[ category
];
314 if ( !Array
.isArray( list
) ) {
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
);
325 // Remove by boolean filters
326 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
327 match
= matches
[ i
];
329 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
330 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
332 matches
.splice( i
, 1 );
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 );
344 index
= matches
.lastIndexOf( match
);
349 return this.list
.slice();
353 * Get 'special' actions.
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.
358 * @return {OO.ui.ActionWidget[]|null} 'Special' action widgets.
360 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
362 return $.extend( {}, this.special
);
366 * Get 'other' actions.
368 * Other actions include all non-special visible action widgets.
370 * @return {OO.ui.ActionWidget[]} 'Other' action widgets
372 OO
.ui
.ActionSet
.prototype.getOthers = function () {
374 return this.others
.slice();
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.
381 * @param {string} mode The mode. Only actions configured to be available in the specified
382 * mode will be made visible.
387 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
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
) );
396 this.organized
= false;
397 this.changing
= false;
398 this.emit( 'change' );
404 * Set the abilities of the specified actions.
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`
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.
414 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
415 var i
, len
, action
, item
;
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
] );
429 * Executes a function once per action.
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.
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
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' );
452 * Add action widgets to the action set.
454 * @param {OO.ui.ActionWidget[]} actions Action widgets to add
459 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
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' ]
469 this.list
.push( action
);
471 this.organized
= false;
472 this.emit( 'add', actions
);
473 this.changing
= false;
474 this.emit( 'change' );
480 * Remove action widgets from the set.
482 * To remove all actions, you may wish to use the #clear method instead.
484 * @param {OO.ui.ActionWidget[]} actions Action widgets to remove
489 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
490 var i
, len
, index
, action
;
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 );
501 this.organized
= false;
502 this.emit( 'remove', actions
);
503 this.changing
= false;
504 this.emit( 'change' );
510 * Remove all action widets from the set.
512 * To remove only specified actions, use the {@link #method-remove remove} method instead.
518 OO
.ui
.ActionSet
.prototype.clear = function () {
520 removed
= this.list
.slice();
522 this.changing
= true;
523 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
524 action
= this.list
[ i
];
525 action
.disconnect( this );
530 this.organized
= false;
531 this.emit( 'remove', removed
);
532 this.changing
= false;
533 this.emit( 'change' );
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.
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
;
551 if ( !this.organized
) {
552 this.categorized
= {};
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
] = {};
563 list
= action
[ this.categories
[ category
] ]();
564 if ( !Array
.isArray( list
) ) {
567 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
569 if ( !this.categorized
[ category
][ item
] ) {
570 this.categorized
[ category
][ item
] = [];
572 this.categorized
[ category
][ item
].push( action
);
575 // Populate special/others
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
;
586 this.others
.push( action
);
590 this.organized
= true;
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.
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.
605 * If the error is a warning, the error interface will include a 'Dismiss' and a 'Continue' button, which will try the
608 * For an example of error interfaces, please see the [OOUI documentation on MediaWiki][1].
610 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Processes_and_errors
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.
624 OO
.ui
.Error
= function OoUiError( message
, config
) {
625 // Allow passing positional parameters inside the config object
626 if ( OO
.isPlainObject( message
) && config
=== undefined ) {
628 message
= config
.message
;
631 // Configuration initialization
632 config
= config
|| {};
635 this.message
= message
instanceof jQuery
? message
: String( message
);
636 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
637 this.warning
= !!config
.warning
;
642 OO
.initClass( OO
.ui
.Error
);
647 * Check if the error is recoverable.
649 * If the error is recoverable, users are able to try the process again.
651 * @return {boolean} Error is recoverable
653 OO
.ui
.Error
.prototype.isRecoverable = function () {
654 return this.recoverable
;
658 * Check if the error is a warning.
660 * If the error is a warning, the error interface will include a 'Dismiss' and a 'Continue' button.
662 * @return {boolean} Error is warning
664 OO
.ui
.Error
.prototype.isWarning = function () {
669 * Get error message as DOM nodes.
671 * @return {jQuery} Error message in DOM nodes
673 OO
.ui
.Error
.prototype.getMessage = function () {
674 return this.message
instanceof jQuery
?
675 this.message
.clone() :
676 $( '<div>' ).text( this.message
).contents();
680 * Get the error message text.
682 * @return {string} Error message
684 OO
.ui
.Error
.prototype.getMessageText = function () {
685 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
689 * A Process is a list of steps that are called in sequence. The step can be a number, a jQuery promise,
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.
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.
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.
711 OO
.ui
.Process = function ( step
, context
) {
716 if ( step
!== undefined ) {
717 this.next( step
, context
);
723 OO
.initClass( OO
.ui
.Process
);
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.
734 OO
.ui
.Process
.prototype.execute = function () {
738 * Continue execution.
741 * @param {Array} step A function and the context it should be called in
742 * @return {Function} Function that continues the process
744 function proceed( step
) {
746 // Execute step in the correct context
748 result
= step
.callback
.call( step
.context
);
750 if ( result
=== false ) {
751 // Use rejected promise for boolean false results
752 return $.Deferred().reject( [] ).promise();
754 if ( typeof result
=== 'number' ) {
756 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
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();
763 if ( result
instanceof OO
.ui
.Error
) {
764 // Use rejected promise for error
765 return $.Deferred().reject( [ result
] ).promise();
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();
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();
776 // Use resolved promise for other results
777 return $.Deferred().resolve().promise();
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
] ) );
788 promise
= $.Deferred().resolve().promise();
795 * Create a process step.
798 * @param {number|jQuery.Promise|Function} step
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
812 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
813 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
815 callback: function () {
821 if ( $.isFunction( step
) ) {
827 throw new Error( 'Cannot create process step: number, promise or function expected' );
831 * Add step to the beginning of the process.
833 * @inheritdoc #createStep
834 * @return {OO.ui.Process} this
837 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
838 this.steps
.unshift( this.createStep( step
, context
) );
843 * Add step to the end of the process.
845 * @inheritdoc #createStep
846 * @return {OO.ui.Process} this
849 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
850 this.steps
.push( this.createStep( step
, context
) );
855 * A window instance represents the life cycle for one single opening of a window
858 * While OO.ui.WindowManager will reuse OO.ui.Window objects, each time a window is
859 * opened, a new lifecycle starts.
861 * For more information, please see the [OOUI documentation on MediaWiki] [1].
863 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows
869 OO
.ui
.WindowInstance
= function OOuiWindowInstance() {
871 opening
: $.Deferred(),
872 opened
: $.Deferred(),
873 closing
: $.Deferred(),
881 this.deferreds
= deferreds
;
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.
888 * @property {jQuery.Promise}
890 this.opening
= deferreds
.opening
.promise();
892 * @property {jQuery.Promise}
894 this.opened
= this.opening
.then( function () {
895 return deferreds
.opened
;
898 * @property {jQuery.Promise}
900 this.closing
= this.opened
.then( function () {
901 return deferreds
.closing
;
904 * @property {jQuery.Promise}
906 this.closed
= this.closing
.then( function () {
907 return deferreds
.closed
;
913 OO
.initClass( OO
.ui
.WindowInstance
);
916 * Check if window is opening.
918 * @return {boolean} Window is opening
920 OO
.ui
.WindowInstance
.prototype.isOpening = function () {
921 return this.deferreds
.opened
.state() === 'pending';
925 * Check if window is opened.
927 * @return {boolean} Window is opened
929 OO
.ui
.WindowInstance
.prototype.isOpened = function () {
930 return this.deferreds
.opened
.state() === 'resolved' &&
931 this.deferreds
.closing
.state() === 'pending';
935 * Check if window is closing.
937 * @return {boolean} Window is closing
939 OO
.ui
.WindowInstance
.prototype.isClosing = function () {
940 return this.deferreds
.closing
.state() === 'resolved' &&
941 this.deferreds
.closed
.state() === 'pending';
945 * Check if window is closed.
947 * @return {boolean} Window is closed
949 OO
.ui
.WindowInstance
.prototype.isClosed = function () {
950 return this.deferreds
.closed
.state() === 'resolved';
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.
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:
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.
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
975 * **Opened**: the window is now open.
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.
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
992 * See the [OOUI documentation on MediaWiki][1] for more information.
994 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
997 * @extends OO.ui.Element
998 * @mixins OO.EventEmitter
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
1007 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
1008 // Configuration initialization
1009 config
= config
|| {};
1011 // Parent constructor
1012 OO
.ui
.WindowManager
.parent
.call( this, config
);
1014 // Mixin constructors
1015 OO
.EventEmitter
.call( this );
1018 this.factory
= config
.factory
;
1019 this.modal
= config
.modal
=== undefined || !!config
.modal
;
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 );
1036 .addClass( 'oo-ui-windowManager' )
1037 .attr( 'aria-hidden', true )
1038 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
1043 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
1044 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
1049 * An 'opening' event is emitted when the window begins to be opened.
1052 * @param {OO.ui.Window} win Window that's being opened
1053 * @param {jQuery.Promise} opened A promise resolved with a value when the window is opened successfully.
1054 * This promise also emits `setup` and `ready` notifications. When this promise is resolved, the first
1055 * argument of the value is an 'closed' promise, the second argument is the opening data.
1056 * @param {Object} data Window opening data
1060 * A 'closing' event is emitted when the window begins to be closed.
1063 * @param {OO.ui.Window} win Window that's being closed
1064 * @param {jQuery.Promise} closed A promise resolved with a value when the window is closed successfully.
1065 * This promise also emits `hold` and `teardown` notifications. When this promise is resolved, the first
1066 * argument of its value is the closing data.
1067 * @param {Object} data Window closing data
1071 * A 'resize' event is emitted when a window is resized.
1074 * @param {OO.ui.Window} win Window that was resized
1077 /* Static Properties */
1080 * Map of the symbolic name of each window size and its CSS properties.
1084 * @property {Object}
1086 OO
.ui
.WindowManager
.static.sizes
= {
1100 // These can be non-numeric because they are never used in calculations
1107 * Symbolic name of the default window size.
1109 * The default size is used if the window's requested size is not recognized.
1113 * @property {string}
1115 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
1120 * Handle window resize events.
1123 * @param {jQuery.Event} e Window resize event
1125 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
1126 clearTimeout( this.onWindowResizeTimeout
);
1127 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
1131 * Handle window resize events.
1134 * @param {jQuery.Event} e Window resize event
1136 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
1137 var currentFocusedElement
= document
.activeElement
;
1138 if ( this.currentWindow
) {
1139 this.updateWindowSize( this.currentWindow
);
1141 // Restore focus to the original element if it has changed.
1142 // When a layout change is made on resize inputs lose focus
1143 // on Android (Chrome and Firefox). See T162127.
1144 if ( currentFocusedElement
!== document
.activeElement
) {
1145 currentFocusedElement
.focus();
1151 * Check if window is opening.
1153 * @param {OO.ui.Window} win Window to check
1154 * @return {boolean} Window is opening
1156 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
1157 return win
=== this.currentWindow
&& !!this.lifecycle
&&
1158 this.lifecycle
.isOpening();
1162 * Check if window is closing.
1164 * @param {OO.ui.Window} win Window to check
1165 * @return {boolean} Window is closing
1167 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
1168 return win
=== this.currentWindow
&& !!this.lifecycle
&&
1169 this.lifecycle
.isClosing();
1173 * Check if window is opened.
1175 * @param {OO.ui.Window} win Window to check
1176 * @return {boolean} Window is opened
1178 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
1179 return win
=== this.currentWindow
&& !!this.lifecycle
&&
1180 this.lifecycle
.isOpened();
1184 * Check if a window is being managed.
1186 * @param {OO.ui.Window} win Window to check
1187 * @return {boolean} Window is being managed
1189 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
1192 for ( name
in this.windows
) {
1193 if ( this.windows
[ name
] === win
) {
1202 * Get the number of milliseconds to wait after opening begins before executing the ‘setup’ process.
1204 * @param {OO.ui.Window} win Window being opened
1205 * @param {Object} [data] Window opening data
1206 * @return {number} Milliseconds to wait
1208 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
1213 * Get the number of milliseconds to wait after setup has finished before executing the ‘ready’ process.
1215 * @param {OO.ui.Window} win Window being opened
1216 * @param {Object} [data] Window opening data
1217 * @return {number} Milliseconds to wait
1219 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
1224 * Get the number of milliseconds to wait after closing has begun before executing the 'hold' process.
1226 * @param {OO.ui.Window} win Window being closed
1227 * @param {Object} [data] Window closing data
1228 * @return {number} Milliseconds to wait
1230 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
1235 * Get the number of milliseconds to wait after the ‘hold’ process has finished before
1236 * executing the ‘teardown’ process.
1238 * @param {OO.ui.Window} win Window being closed
1239 * @param {Object} [data] Window closing data
1240 * @return {number} Milliseconds to wait
1242 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
1243 return this.modal
? 250 : 0;
1247 * Get a window by its symbolic name.
1249 * If the window is not yet instantiated and its symbolic name is recognized by a factory, it will be
1250 * instantiated and added to the window manager automatically. Please see the [OOUI documentation on MediaWiki][3]
1251 * for more information about using factories.
1252 * [3]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
1254 * @param {string} name Symbolic name of the window
1255 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
1256 * @throws {Error} An error is thrown if the symbolic name is not recognized by the factory.
1257 * @throws {Error} An error is thrown if the named window is not recognized as a managed window.
1259 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
1260 var deferred
= $.Deferred(),
1261 win
= this.windows
[ name
];
1263 if ( !( win
instanceof OO
.ui
.Window
) ) {
1264 if ( this.factory
) {
1265 if ( !this.factory
.lookup( name
) ) {
1266 deferred
.reject( new OO
.ui
.Error(
1267 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
1270 win
= this.factory
.create( name
);
1271 this.addWindows( [ win
] );
1272 deferred
.resolve( win
);
1275 deferred
.reject( new OO
.ui
.Error(
1276 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
1280 deferred
.resolve( win
);
1283 return deferred
.promise();
1287 * Get current window.
1289 * @return {OO.ui.Window|null} Currently opening/opened/closing window
1291 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
1292 return this.currentWindow
;
1298 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
1299 * @param {Object} [data] Window opening data
1300 * @param {jQuery|null} [data.$returnFocusTo] Element to which the window will return focus when closed.
1301 * Defaults the current activeElement. If set to null, focus isn't changed on close.
1302 * @return {OO.ui.WindowInstance|jQuery.Promise} A lifecycle object representing this particular
1303 * opening of the window. For backwards-compatibility, then object is also a Thenable that is resolved
1304 * when the window is done opening, with nested promise for when closing starts. This behaviour
1305 * is deprecated and is not compatible with jQuery 3. See T163510.
1308 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
, lifecycle
, compatOpening
) {
1313 // Internal parameter 'lifecycle' allows this method to always return
1314 // a lifecycle even if the window still needs to be created
1315 // asynchronously when 'win' is a string.
1316 lifecycle
= lifecycle
|| new OO
.ui
.WindowInstance();
1317 compatOpening
= compatOpening
|| $.Deferred();
1319 // Turn lifecycle into a Thenable for backwards-compatibility with
1320 // the deprecated nested-promise behaviour, see T163510.
1321 [ 'state', 'always', 'catch', 'pipe', 'then', 'promise', 'progress', 'done', 'fail' ]
1322 .forEach( function ( method
) {
1323 lifecycle
[ method
] = function () {
1324 OO
.ui
.warnDeprecation(
1325 'Using the return value of openWindow as a promise is deprecated. ' +
1326 'Use .openWindow( ... ).opening.' + method
+ '( ... ) instead.'
1328 return compatOpening
[ method
].apply( this, arguments
);
1332 // Argument handling
1333 if ( typeof win
=== 'string' ) {
1334 this.getWindow( win
).then(
1336 manager
.openWindow( win
, data
, lifecycle
, compatOpening
);
1339 lifecycle
.deferreds
.opening
.reject( err
);
1346 if ( !this.hasWindow( win
) ) {
1347 error
= 'Cannot open window: window is not attached to manager';
1348 } else if ( this.lifecycle
&& this.lifecycle
.isOpened() ) {
1349 error
= 'Cannot open window: another window is open';
1350 } else if ( this.preparingToOpen
|| ( this.lifecycle
&& this.lifecycle
.isOpening() ) ) {
1351 error
= 'Cannot open window: another window is opening';
1355 compatOpening
.reject( new OO
.ui
.Error( error
) );
1356 lifecycle
.deferreds
.opening
.reject( new OO
.ui
.Error( error
) );
1360 // If a window is currently closing, wait for it to complete
1361 this.preparingToOpen
= $.when( this.lifecycle
&& this.lifecycle
.closed
);
1362 // Ensure handlers get called after preparingToOpen is set
1363 this.preparingToOpen
.done( function () {
1364 if ( manager
.modal
) {
1365 manager
.toggleGlobalEvents( true );
1366 manager
.toggleAriaIsolation( true );
1368 manager
.$returnFocusTo
= data
.$returnFocusTo
!== undefined ? data
.$returnFocusTo
: $( document
.activeElement
);
1369 manager
.currentWindow
= win
;
1370 manager
.lifecycle
= lifecycle
;
1371 manager
.preparingToOpen
= null;
1372 manager
.emit( 'opening', win
, compatOpening
, data
);
1373 lifecycle
.deferreds
.opening
.resolve( data
);
1374 setTimeout( function () {
1375 manager
.compatOpened
= $.Deferred();
1376 win
.setup( data
).then( function () {
1377 manager
.updateWindowSize( win
);
1378 compatOpening
.notify( { state
: 'setup' } );
1379 setTimeout( function () {
1380 win
.ready( data
).then( function () {
1381 compatOpening
.notify( { state
: 'ready' } );
1382 lifecycle
.deferreds
.opened
.resolve( data
);
1383 compatOpening
.resolve( manager
.compatOpened
.promise(), data
);
1385 lifecycle
.deferreds
.opened
.reject();
1386 compatOpening
.reject();
1387 manager
.closeWindow( win
);
1389 }, manager
.getReadyDelay() );
1391 lifecycle
.deferreds
.opened
.reject();
1392 compatOpening
.reject();
1393 manager
.closeWindow( win
);
1395 }, manager
.getSetupDelay() );
1404 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
1405 * @param {Object} [data] Window closing data
1406 * @return {OO.ui.WindowInstance|jQuery.Promise} A lifecycle object representing this particular
1407 * opening of the window. For backwards-compatibility, the object is also a Thenable that is resolved
1408 * when the window is done closing, see T163510.
1411 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
1414 compatClosing
= $.Deferred(),
1415 lifecycle
= this.lifecycle
,
1418 // Argument handling
1419 if ( typeof win
=== 'string' ) {
1420 win
= this.windows
[ win
];
1421 } else if ( !this.hasWindow( win
) ) {
1427 error
= 'Cannot close window: no window is currently open';
1428 } else if ( !win
) {
1429 error
= 'Cannot close window: window is not attached to manager';
1430 } else if ( win
!== this.currentWindow
|| this.lifecycle
.isClosed() ) {
1431 error
= 'Cannot close window: window already closed with different data';
1432 } else if ( this.preparingToClose
|| this.lifecycle
.isClosing() ) {
1433 error
= 'Cannot close window: window already closing with different data';
1437 // This function was called for the wrong window and we don't want to mess with the current
1439 lifecycle
= new OO
.ui
.WindowInstance();
1440 // Pretend the window has been opened, so that we can pretend to fail to close it.
1441 lifecycle
.deferreds
.opening
.resolve( {} );
1442 lifecycle
.deferreds
.opened
.resolve( {} );
1445 // Turn lifecycle into a Thenable for backwards-compatibility with
1446 // the deprecated nested-promise behaviour, see T163510.
1447 [ 'state', 'always', 'catch', 'pipe', 'then', 'promise', 'progress', 'done', 'fail' ]
1448 .forEach( function ( method
) {
1449 lifecycle
[ method
] = function () {
1450 OO
.ui
.warnDeprecation(
1451 'Using the return value of closeWindow as a promise is deprecated. ' +
1452 'Use .closeWindow( ... ).closed.' + method
+ '( ... ) instead.'
1454 return compatClosing
[ method
].apply( this, arguments
);
1459 compatClosing
.reject( new OO
.ui
.Error( error
) );
1460 lifecycle
.deferreds
.closing
.reject( new OO
.ui
.Error( error
) );
1464 // If the window is currently opening, close it when it's done
1465 this.preparingToClose
= $.when( this.lifecycle
.opened
);
1466 // Ensure handlers get called after preparingToClose is set
1467 this.preparingToClose
.always( function () {
1468 manager
.preparingToClose
= null;
1469 manager
.emit( 'closing', win
, compatClosing
, data
);
1470 lifecycle
.deferreds
.closing
.resolve( data
);
1471 compatOpened
= manager
.compatOpened
;
1472 manager
.compatOpened
= null;
1473 compatOpened
.resolve( compatClosing
.promise(), data
);
1474 setTimeout( function () {
1475 win
.hold( data
).then( function () {
1476 compatClosing
.notify( { state
: 'hold' } );
1477 setTimeout( function () {
1478 win
.teardown( data
).then( function () {
1479 compatClosing
.notify( { state
: 'teardown' } );
1480 if ( manager
.modal
) {
1481 manager
.toggleGlobalEvents( false );
1482 manager
.toggleAriaIsolation( false );
1484 if ( manager
.$returnFocusTo
&& manager
.$returnFocusTo
.length
) {
1485 manager
.$returnFocusTo
[ 0 ].focus();
1487 manager
.currentWindow
= null;
1488 manager
.lifecycle
= null;
1489 lifecycle
.deferreds
.closed
.resolve( data
);
1490 compatClosing
.resolve( data
);
1492 }, manager
.getTeardownDelay() );
1494 }, manager
.getHoldDelay() );
1501 * Add windows to the window manager.
1503 * Windows can be added by reference, symbolic name, or explicitly defined symbolic names.
1504 * See the [OOUI documentation on MediaWiki] [2] for examples.
1505 * [2]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
1507 * This function can be called in two manners:
1509 * 1. `.addWindows( [ windowA, windowB, ... ] )` (where `windowA`, `windowB` are OO.ui.Window objects)
1511 * This syntax registers windows under the symbolic names defined in their `.static.name`
1512 * properties. For example, if `windowA.constructor.static.name` is `'nameA'`, calling
1513 * `.openWindow( 'nameA' )` afterwards will open the window `windowA`. This syntax requires the
1514 * static name to be set, otherwise an exception will be thrown.
1516 * This is the recommended way, as it allows for an easier switch to using a window factory.
1518 * 2. `.addWindows( { nameA: windowA, nameB: windowB, ... } )`
1520 * This syntax registers windows under the explicitly given symbolic names. In this example,
1521 * calling `.openWindow( 'nameA' )` afterwards will open the window `windowA`, regardless of what
1522 * its `.static.name` is set to. The static name is not required to be set.
1524 * This should only be used if you need to override the default symbolic names.
1528 * var windowManager = new OO.ui.WindowManager();
1529 * $( 'body' ).append( windowManager.$element );
1531 * // Add a window under the default name: see OO.ui.MessageDialog.static.name
1532 * windowManager.addWindows( [ new OO.ui.MessageDialog() ] );
1533 * // Add a window under an explicit name
1534 * windowManager.addWindows( { myMessageDialog: new OO.ui.MessageDialog() } );
1536 * // Open window by default name
1537 * windowManager.openWindow( 'message' );
1538 * // Open window by explicitly given name
1539 * windowManager.openWindow( 'myMessageDialog' );
1542 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows An array of window objects specified
1543 * by reference, symbolic name, or explicitly defined symbolic names.
1544 * @throws {Error} An error is thrown if a window is added by symbolic name, but has neither an
1545 * explicit nor a statically configured symbolic name.
1547 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
1548 var i
, len
, win
, name
, list
;
1550 if ( Array
.isArray( windows
) ) {
1551 // Convert to map of windows by looking up symbolic names from static configuration
1553 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
1554 name
= windows
[ i
].constructor.static.name
;
1556 throw new Error( 'Windows must have a `name` static property defined.' );
1558 list
[ name
] = windows
[ i
];
1560 } else if ( OO
.isPlainObject( windows
) ) {
1565 for ( name
in list
) {
1567 this.windows
[ name
] = win
.toggle( false );
1568 this.$element
.append( win
.$element
);
1569 win
.setManager( this );
1574 * Remove the specified windows from the windows manager.
1576 * Windows will be closed before they are removed. If you wish to remove all windows, you may wish to use
1577 * the #clearWindows method instead. If you no longer need the window manager and want to ensure that it no
1578 * longer listens to events, use the #destroy method.
1580 * @param {string[]} names Symbolic names of windows to remove
1581 * @return {jQuery.Promise} Promise resolved when window is closed and removed
1582 * @throws {Error} An error is thrown if the named windows are not managed by the window manager.
1584 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
1585 var i
, len
, win
, name
, cleanupWindow
,
1588 cleanup = function ( name
, win
) {
1589 delete manager
.windows
[ name
];
1590 win
.$element
.detach();
1593 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
1595 win
= this.windows
[ name
];
1597 throw new Error( 'Cannot remove window' );
1599 cleanupWindow
= cleanup
.bind( null, name
, win
);
1600 promises
.push( this.closeWindow( name
).closed
.then( cleanupWindow
, cleanupWindow
) );
1603 return $.when
.apply( $, promises
);
1607 * Remove all windows from the window manager.
1609 * Windows will be closed before they are removed. Note that the window manager, though not in use, will still
1610 * listen to events. If the window manager will not be used again, you may wish to use the #destroy method instead.
1611 * To remove just a subset of windows, use the #removeWindows method.
1613 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
1615 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
1616 return this.removeWindows( Object
.keys( this.windows
) );
1620 * Set dialog size. In general, this method should not be called directly.
1622 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
1624 * @param {OO.ui.Window} win Window to update, should be the current window
1627 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
1630 // Bypass for non-current, and thus invisible, windows
1631 if ( win
!== this.currentWindow
) {
1635 isFullscreen
= win
.getSize() === 'full';
1637 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', isFullscreen
);
1638 this.$element
.toggleClass( 'oo-ui-windowManager-floating', !isFullscreen
);
1639 win
.setDimensions( win
.getSizeProperties() );
1641 this.emit( 'resize', win
);
1647 * Bind or unbind global events for scrolling.
1650 * @param {boolean} [on] Bind global events
1653 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
1654 var scrollWidth
, bodyMargin
,
1655 $body
= $( this.getElementDocument().body
),
1656 // We could have multiple window managers open so only modify
1657 // the body css at the bottom of the stack
1658 stackDepth
= $body
.data( 'windowManagerGlobalEvents' ) || 0;
1660 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
1663 if ( !this.globalEvents
) {
1664 $( this.getElementWindow() ).on( {
1665 // Start listening for top-level window dimension changes
1666 'orientationchange resize': this.onWindowResizeHandler
1668 if ( stackDepth
=== 0 ) {
1669 scrollWidth
= window
.innerWidth
- document
.documentElement
.clientWidth
;
1670 bodyMargin
= parseFloat( $body
.css( 'margin-right' ) ) || 0;
1673 'margin-right': bodyMargin
+ scrollWidth
1677 this.globalEvents
= true;
1679 } else if ( this.globalEvents
) {
1680 $( this.getElementWindow() ).off( {
1681 // Stop listening for top-level window dimension changes
1682 'orientationchange resize': this.onWindowResizeHandler
1685 if ( stackDepth
=== 0 ) {
1691 this.globalEvents
= false;
1693 $body
.data( 'windowManagerGlobalEvents', stackDepth
);
1699 * Toggle screen reader visibility of content other than the window manager.
1702 * @param {boolean} [isolate] Make only the window manager visible to screen readers
1705 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
1706 var $topLevelElement
;
1707 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
1710 if ( !this.$ariaHidden
) {
1711 // Find the top level element containing the window manager or the
1712 // window manager's element itself in case its a direct child of body
1713 $topLevelElement
= this.$element
.parentsUntil( 'body' ).last();
1714 $topLevelElement
= $topLevelElement
.length
=== 0 ? this.$element
: $topLevelElement
;
1716 // In case previously set by another window manager
1717 this.$element
.removeAttr( 'aria-hidden' );
1719 // Hide everything other than the window manager from screen readers
1720 this.$ariaHidden
= $( 'body' )
1723 .not( $topLevelElement
)
1724 .attr( 'aria-hidden', true );
1726 } else if ( this.$ariaHidden
) {
1727 // Restore screen reader visibility
1728 this.$ariaHidden
.removeAttr( 'aria-hidden' );
1729 this.$ariaHidden
= null;
1731 // and hide the window manager
1732 this.$element
.attr( 'aria-hidden', true );
1739 * Destroy the window manager.
1741 * Destroying the window manager ensures that it will no longer listen to events. If you would like to
1742 * continue using the window manager, but wish to remove all windows from it, use the #clearWindows method
1745 OO
.ui
.WindowManager
.prototype.destroy = function () {
1746 this.toggleGlobalEvents( false );
1747 this.toggleAriaIsolation( false );
1748 this.clearWindows();
1749 this.$element
.remove();
1753 * A window is a container for elements that are in a child frame. They are used with
1754 * a window manager (OO.ui.WindowManager), which is used to open and close the window and control
1755 * its presentation. The size of a window is specified using a symbolic name (e.g., ‘small’, ‘medium’,
1756 * ‘large’), which is interpreted by the window manager. If the requested size is not recognized,
1757 * the window manager will choose a sensible fallback.
1759 * The lifecycle of a window has three primary stages (opening, opened, and closing) in which
1760 * different processes are executed:
1762 * **opening**: The opening stage begins when the window manager's {@link OO.ui.WindowManager#openWindow
1763 * openWindow} or the window's {@link #open open} methods are used, and the window manager begins to open
1766 * - {@link #getSetupProcess} method is called and its result executed
1767 * - {@link #getReadyProcess} method is called and its result executed
1769 * **opened**: The window is now open
1771 * **closing**: The closing stage begins when the window manager's
1772 * {@link OO.ui.WindowManager#closeWindow closeWindow}
1773 * or the window's {@link #close} methods are used, and the window manager begins to close the window.
1775 * - {@link #getHoldProcess} method is called and its result executed
1776 * - {@link #getTeardownProcess} method is called and its result executed. The window is now closed
1778 * Each of the window's processes (setup, ready, hold, and teardown) can be extended in subclasses
1779 * by overriding the window's #getSetupProcess, #getReadyProcess, #getHoldProcess and #getTeardownProcess
1780 * methods. Note that each {@link OO.ui.Process process} is executed in series, so asynchronous
1781 * processing can complete. Always assume window processes are executed asynchronously.
1783 * For more information, please see the [OOUI documentation on MediaWiki] [1].
1785 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows
1789 * @extends OO.ui.Element
1790 * @mixins OO.EventEmitter
1793 * @param {Object} [config] Configuration options
1794 * @cfg {string} [size] Symbolic name of the dialog size: `small`, `medium`, `large`, `larger` or
1795 * `full`. If omitted, the value of the {@link #static-size static size} property will be used.
1797 OO
.ui
.Window
= function OoUiWindow( config
) {
1798 // Configuration initialization
1799 config
= config
|| {};
1801 // Parent constructor
1802 OO
.ui
.Window
.parent
.call( this, config
);
1804 // Mixin constructors
1805 OO
.EventEmitter
.call( this );
1808 this.manager
= null;
1809 this.size
= config
.size
|| this.constructor.static.size
;
1810 this.$frame
= $( '<div>' );
1812 * Overlay element to use for the `$overlay` configuration option of widgets that support it.
1813 * Things put inside of it are overlaid on top of the window and are not bound to its dimensions.
1814 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
1816 * MyDialog.prototype.initialize = function () {
1818 * var popupButton = new OO.ui.PopupButtonWidget( {
1819 * $overlay: this.$overlay,
1820 * label: 'Popup button',
1822 * $content: $( '<p>Popup contents.</p><p>Popup contents.</p><p>Popup contents.</p>' ),
1829 * @property {jQuery}
1831 this.$overlay
= $( '<div>' );
1832 this.$content
= $( '<div>' );
1834 this.$focusTrapBefore
= $( '<div>' ).prop( 'tabIndex', 0 );
1835 this.$focusTrapAfter
= $( '<div>' ).prop( 'tabIndex', 0 );
1836 this.$focusTraps
= this.$focusTrapBefore
.add( this.$focusTrapAfter
);
1839 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1841 .addClass( 'oo-ui-window-content' )
1842 .attr( 'tabindex', 0 );
1844 .addClass( 'oo-ui-window-frame' )
1845 .append( this.$focusTrapBefore
, this.$content
, this.$focusTrapAfter
);
1848 .addClass( 'oo-ui-window' )
1849 .append( this.$frame
, this.$overlay
);
1851 // Initially hidden - using #toggle may cause errors if subclasses override toggle with methods
1852 // that reference properties not initialized at that time of parent class construction
1853 // TODO: Find a better way to handle post-constructor setup
1854 this.visible
= false;
1855 this.$element
.addClass( 'oo-ui-element-hidden' );
1860 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1861 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1863 /* Static Properties */
1866 * Symbolic name of the window size: `small`, `medium`, `large`, `larger` or `full`.
1868 * The static size is used if no #size is configured during construction.
1872 * @property {string}
1874 OO
.ui
.Window
.static.size
= 'medium';
1879 * Handle mouse down events.
1882 * @param {jQuery.Event} e Mouse down event
1884 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1885 // Prevent clicking on the click-block from stealing focus
1886 if ( e
.target
=== this.$element
[ 0 ] ) {
1892 * Check if the window has been initialized.
1894 * Initialization occurs when a window is added to a manager.
1896 * @return {boolean} Window has been initialized
1898 OO
.ui
.Window
.prototype.isInitialized = function () {
1899 return !!this.manager
;
1903 * Check if the window is visible.
1905 * @return {boolean} Window is visible
1907 OO
.ui
.Window
.prototype.isVisible = function () {
1908 return this.visible
;
1912 * Check if the window is opening.
1914 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isOpening isOpening}
1917 * @return {boolean} Window is opening
1919 OO
.ui
.Window
.prototype.isOpening = function () {
1920 return this.manager
.isOpening( this );
1924 * Check if the window is closing.
1926 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isClosing isClosing} method.
1928 * @return {boolean} Window is closing
1930 OO
.ui
.Window
.prototype.isClosing = function () {
1931 return this.manager
.isClosing( this );
1935 * Check if the window is opened.
1937 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isOpened isOpened} method.
1939 * @return {boolean} Window is opened
1941 OO
.ui
.Window
.prototype.isOpened = function () {
1942 return this.manager
.isOpened( this );
1946 * Get the window manager.
1948 * All windows must be attached to a window manager, which is used to open
1949 * and close the window and control its presentation.
1951 * @return {OO.ui.WindowManager} Manager of window
1953 OO
.ui
.Window
.prototype.getManager = function () {
1954 return this.manager
;
1958 * Get the symbolic name of the window size (e.g., `small` or `medium`).
1960 * @return {string} Symbolic name of the size: `small`, `medium`, `large`, `larger`, `full`
1962 OO
.ui
.Window
.prototype.getSize = function () {
1963 var viewport
= OO
.ui
.Element
.static.getDimensions( this.getElementWindow() ),
1964 sizes
= this.manager
.constructor.static.sizes
,
1967 if ( !sizes
[ size
] ) {
1968 size
= this.manager
.constructor.static.defaultSize
;
1970 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[ size
].width
) {
1978 * Get the size properties associated with the current window size
1980 * @return {Object} Size properties
1982 OO
.ui
.Window
.prototype.getSizeProperties = function () {
1983 return this.manager
.constructor.static.sizes
[ this.getSize() ];
1987 * Disable transitions on window's frame for the duration of the callback function, then enable them
1991 * @param {Function} callback Function to call while transitions are disabled
1993 OO
.ui
.Window
.prototype.withoutSizeTransitions = function ( callback
) {
1994 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1995 // Disable transitions first, otherwise we'll get values from when the window was animating.
1996 // We need to build the transition CSS properties using these specific properties since
1997 // Firefox doesn't return anything useful when asked just for 'transition'.
1998 var oldTransition
= this.$frame
.css( 'transition-property' ) + ' ' +
1999 this.$frame
.css( 'transition-duration' ) + ' ' +
2000 this.$frame
.css( 'transition-timing-function' ) + ' ' +
2001 this.$frame
.css( 'transition-delay' );
2003 this.$frame
.css( 'transition', 'none' );
2006 // Force reflow to make sure the style changes done inside callback
2007 // really are not transitioned
2008 this.$frame
.height();
2009 this.$frame
.css( 'transition', oldTransition
);
2013 * Get the height of the full window contents (i.e., the window head, body and foot together).
2015 * What consistitutes the head, body, and foot varies depending on the window type.
2016 * A {@link OO.ui.MessageDialog message dialog} displays a title and message in its body,
2017 * and any actions in the foot. A {@link OO.ui.ProcessDialog process dialog} displays a title
2018 * and special actions in the head, and dialog content in the body.
2020 * To get just the height of the dialog body, use the #getBodyHeight method.
2022 * @return {number} The height of the window contents (the dialog head, body and foot) in pixels
2024 OO
.ui
.Window
.prototype.getContentHeight = function () {
2027 bodyStyleObj
= this.$body
[ 0 ].style
,
2028 frameStyleObj
= this.$frame
[ 0 ].style
;
2030 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
2031 // Disable transitions first, otherwise we'll get values from when the window was animating.
2032 this.withoutSizeTransitions( function () {
2033 var oldHeight
= frameStyleObj
.height
,
2034 oldPosition
= bodyStyleObj
.position
;
2035 frameStyleObj
.height
= '1px';
2036 // Force body to resize to new width
2037 bodyStyleObj
.position
= 'relative';
2038 bodyHeight
= win
.getBodyHeight();
2039 frameStyleObj
.height
= oldHeight
;
2040 bodyStyleObj
.position
= oldPosition
;
2044 // Add buffer for border
2045 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
2046 // Use combined heights of children
2047 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
2052 * Get the height of the window body.
2054 * To get the height of the full window contents (the window body, head, and foot together),
2055 * use #getContentHeight.
2057 * When this function is called, the window will temporarily have been resized
2058 * to height=1px, so .scrollHeight measurements can be taken accurately.
2060 * @return {number} Height of the window body in pixels
2062 OO
.ui
.Window
.prototype.getBodyHeight = function () {
2063 return this.$body
[ 0 ].scrollHeight
;
2067 * Get the directionality of the frame (right-to-left or left-to-right).
2069 * @return {string} Directionality: `'ltr'` or `'rtl'`
2071 OO
.ui
.Window
.prototype.getDir = function () {
2072 return OO
.ui
.Element
.static.getDir( this.$content
) || 'ltr';
2076 * Get the 'setup' process.
2078 * The setup process is used to set up a window for use in a particular context,
2079 * based on the `data` argument. This method is called during the opening phase of the window’s
2082 * Override this method to add additional steps to the ‘setup’ process the parent method provides
2083 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2086 * To add window content that persists between openings, you may wish to use the #initialize method
2089 * @param {Object} [data] Window opening data
2090 * @return {OO.ui.Process} Setup process
2092 OO
.ui
.Window
.prototype.getSetupProcess = function () {
2093 return new OO
.ui
.Process();
2097 * Get the ‘ready’ process.
2099 * The ready process is used to ready a window for use in a particular
2100 * context, based on the `data` argument. This method is called during the opening phase of
2101 * the window’s lifecycle, after the window has been {@link #getSetupProcess setup}.
2103 * Override this method to add additional steps to the ‘ready’ process the parent method
2104 * provides using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next}
2105 * methods of OO.ui.Process.
2107 * @param {Object} [data] Window opening data
2108 * @return {OO.ui.Process} Ready process
2110 OO
.ui
.Window
.prototype.getReadyProcess = function () {
2111 return new OO
.ui
.Process();
2115 * Get the 'hold' process.
2117 * The hold process is used to keep a window from being used in a particular context,
2118 * based on the `data` argument. This method is called during the closing phase of the window’s
2121 * Override this method to add additional steps to the 'hold' process the parent method provides
2122 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2125 * @param {Object} [data] Window closing data
2126 * @return {OO.ui.Process} Hold process
2128 OO
.ui
.Window
.prototype.getHoldProcess = function () {
2129 return new OO
.ui
.Process();
2133 * Get the ‘teardown’ process.
2135 * The teardown process is used to teardown a window after use. During teardown,
2136 * user interactions within the window are conveyed and the window is closed, based on the `data`
2137 * argument. This method is called during the closing phase of the window’s lifecycle.
2139 * Override this method to add additional steps to the ‘teardown’ process the parent method provides
2140 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2143 * @param {Object} [data] Window closing data
2144 * @return {OO.ui.Process} Teardown process
2146 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
2147 return new OO
.ui
.Process();
2151 * Set the window manager.
2153 * This will cause the window to initialize. Calling it more than once will cause an error.
2155 * @param {OO.ui.WindowManager} manager Manager for this window
2156 * @throws {Error} An error is thrown if the method is called more than once
2159 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
2160 if ( this.manager
) {
2161 throw new Error( 'Cannot set window manager, window already has a manager' );
2164 this.manager
= manager
;
2171 * Set the window size by symbolic name (e.g., 'small' or 'medium')
2173 * @param {string} size Symbolic name of size: `small`, `medium`, `large`, `larger` or
2177 OO
.ui
.Window
.prototype.setSize = function ( size
) {
2184 * Update the window size.
2186 * @throws {Error} An error is thrown if the window is not attached to a window manager
2189 OO
.ui
.Window
.prototype.updateSize = function () {
2190 if ( !this.manager
) {
2191 throw new Error( 'Cannot update window size, must be attached to a manager' );
2194 this.manager
.updateWindowSize( this );
2200 * Set window dimensions. This method is called by the {@link OO.ui.WindowManager window manager}
2201 * when the window is opening. In general, setDimensions should not be called directly.
2203 * To set the size of the window, use the #setSize method.
2205 * @param {Object} dim CSS dimension properties
2206 * @param {string|number} [dim.width] Width
2207 * @param {string|number} [dim.minWidth] Minimum width
2208 * @param {string|number} [dim.maxWidth] Maximum width
2209 * @param {string|number} [dim.height] Height, omit to set based on height of contents
2210 * @param {string|number} [dim.minHeight] Minimum height
2211 * @param {string|number} [dim.maxHeight] Maximum height
2214 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
2217 styleObj
= this.$frame
[ 0 ].style
;
2219 // Calculate the height we need to set using the correct width
2220 if ( dim
.height
=== undefined ) {
2221 this.withoutSizeTransitions( function () {
2222 var oldWidth
= styleObj
.width
;
2223 win
.$frame
.css( 'width', dim
.width
|| '' );
2224 height
= win
.getContentHeight();
2225 styleObj
.width
= oldWidth
;
2228 height
= dim
.height
;
2232 width
: dim
.width
|| '',
2233 minWidth
: dim
.minWidth
|| '',
2234 maxWidth
: dim
.maxWidth
|| '',
2235 height
: height
|| '',
2236 minHeight
: dim
.minHeight
|| '',
2237 maxHeight
: dim
.maxHeight
|| ''
2244 * Initialize window contents.
2246 * Before the window is opened for the first time, #initialize is called so that content that
2247 * persists between openings can be added to the window.
2249 * To set up a window with new content each time the window opens, use #getSetupProcess.
2251 * @throws {Error} An error is thrown if the window is not attached to a window manager
2254 OO
.ui
.Window
.prototype.initialize = function () {
2255 if ( !this.manager
) {
2256 throw new Error( 'Cannot initialize window, must be attached to a manager' );
2260 this.$head
= $( '<div>' );
2261 this.$body
= $( '<div>' );
2262 this.$foot
= $( '<div>' );
2263 this.$document
= $( this.getElementDocument() );
2266 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
2269 this.$head
.addClass( 'oo-ui-window-head' );
2270 this.$body
.addClass( 'oo-ui-window-body' );
2271 this.$foot
.addClass( 'oo-ui-window-foot' );
2272 this.$content
.append( this.$head
, this.$body
, this.$foot
);
2278 * Called when someone tries to focus the hidden element at the end of the dialog.
2279 * Sends focus back to the start of the dialog.
2281 * @param {jQuery.Event} event Focus event
2283 OO
.ui
.Window
.prototype.onFocusTrapFocused = function ( event
) {
2284 var backwards
= this.$focusTrapBefore
.is( event
.target
),
2285 element
= OO
.ui
.findFocusable( this.$content
, backwards
);
2287 // There's a focusable element inside the content, at the front or
2288 // back depending on which focus trap we hit; select it.
2291 // There's nothing focusable inside the content. As a fallback,
2292 // this.$content is focusable, and focusing it will keep our focus
2293 // properly trapped. It's not a *meaningful* focus, since it's just
2294 // the content-div for the Window, but it's better than letting focus
2295 // escape into the page.
2296 this.$content
.focus();
2303 * This method is a wrapper around a call to the window manager’s {@link OO.ui.WindowManager#openWindow openWindow}
2304 * method, which returns a promise resolved when the window is done opening.
2306 * To customize the window each time it opens, use #getSetupProcess or #getReadyProcess.
2308 * @param {Object} [data] Window opening data
2309 * @return {jQuery.Promise} Promise resolved with a value when the window is opened, or rejected
2310 * if the window fails to open. When the promise is resolved successfully, the first argument of the
2311 * value is a new promise, which is resolved when the window begins closing.
2312 * @throws {Error} An error is thrown if the window is not attached to a window manager
2314 OO
.ui
.Window
.prototype.open = function ( data
) {
2315 if ( !this.manager
) {
2316 throw new Error( 'Cannot open window, must be attached to a manager' );
2319 return this.manager
.openWindow( this, data
);
2325 * This method is a wrapper around a call to the window
2326 * manager’s {@link OO.ui.WindowManager#closeWindow closeWindow} method,
2327 * which returns a closing promise resolved when the window is done closing.
2329 * The window's #getHoldProcess and #getTeardownProcess methods are called during the closing
2330 * phase of the window’s lifecycle and can be used to specify closing behavior each time
2331 * the window closes.
2333 * @param {Object} [data] Window closing data
2334 * @return {jQuery.Promise} Promise resolved when window is closed
2335 * @throws {Error} An error is thrown if the window is not attached to a window manager
2337 OO
.ui
.Window
.prototype.close = function ( data
) {
2338 if ( !this.manager
) {
2339 throw new Error( 'Cannot close window, must be attached to a manager' );
2342 return this.manager
.closeWindow( this, data
);
2348 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2351 * @param {Object} [data] Window opening data
2352 * @return {jQuery.Promise} Promise resolved when window is setup
2354 OO
.ui
.Window
.prototype.setup = function ( data
) {
2357 this.toggle( true );
2359 this.focusTrapHandler
= OO
.ui
.bind( this.onFocusTrapFocused
, this );
2360 this.$focusTraps
.on( 'focus', this.focusTrapHandler
);
2362 return this.getSetupProcess( data
).execute().then( function () {
2363 // Force redraw by asking the browser to measure the elements' widths
2364 win
.$element
.addClass( 'oo-ui-window-active oo-ui-window-setup' ).width();
2365 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2372 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2375 * @param {Object} [data] Window opening data
2376 * @return {jQuery.Promise} Promise resolved when window is ready
2378 OO
.ui
.Window
.prototype.ready = function ( data
) {
2381 this.$content
.focus();
2382 return this.getReadyProcess( data
).execute().then( function () {
2383 // Force redraw by asking the browser to measure the elements' widths
2384 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2385 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2392 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2395 * @param {Object} [data] Window closing data
2396 * @return {jQuery.Promise} Promise resolved when window is held
2398 OO
.ui
.Window
.prototype.hold = function ( data
) {
2401 return this.getHoldProcess( data
).execute().then( function () {
2402 // Get the focused element within the window's content
2403 var $focus
= win
.$content
.find( OO
.ui
.Element
.static.getDocument( win
.$content
).activeElement
);
2405 // Blur the focused element
2406 if ( $focus
.length
) {
2410 // Force redraw by asking the browser to measure the elements' widths
2411 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2412 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2419 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2422 * @param {Object} [data] Window closing data
2423 * @return {jQuery.Promise} Promise resolved when window is torn down
2425 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2428 return this.getTeardownProcess( data
).execute().then( function () {
2429 // Force redraw by asking the browser to measure the elements' widths
2430 win
.$element
.removeClass( 'oo-ui-window-active oo-ui-window-setup' ).width();
2431 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2432 win
.$focusTraps
.off( 'focus', win
.focusTrapHandler
);
2433 win
.toggle( false );
2438 * The Dialog class serves as the base class for the other types of dialogs.
2439 * Unless extended to include controls, the rendered dialog box is a simple window
2440 * that users can close by hitting the ‘Esc’ key. Dialog windows are used with OO.ui.WindowManager,
2441 * which opens, closes, and controls the presentation of the window. See the
2442 * [OOUI documentation on MediaWiki] [1] for more information.
2445 * // A simple dialog window.
2446 * function MyDialog( config ) {
2447 * MyDialog.parent.call( this, config );
2449 * OO.inheritClass( MyDialog, OO.ui.Dialog );
2450 * MyDialog.static.name = 'myDialog';
2451 * MyDialog.prototype.initialize = function () {
2452 * MyDialog.parent.prototype.initialize.call( this );
2453 * this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
2454 * this.content.$element.append( '<p>A simple dialog window. Press \'Esc\' to close.</p>' );
2455 * this.$body.append( this.content.$element );
2457 * MyDialog.prototype.getBodyHeight = function () {
2458 * return this.content.$element.outerHeight( true );
2460 * var myDialog = new MyDialog( {
2463 * // Create and append a window manager, which opens and closes the window.
2464 * var windowManager = new OO.ui.WindowManager();
2465 * $( 'body' ).append( windowManager.$element );
2466 * windowManager.addWindows( [ myDialog ] );
2467 * // Open the window!
2468 * windowManager.openWindow( myDialog );
2470 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Dialogs
2474 * @extends OO.ui.Window
2475 * @mixins OO.ui.mixin.PendingElement
2478 * @param {Object} [config] Configuration options
2480 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2481 // Parent constructor
2482 OO
.ui
.Dialog
.parent
.call( this, config
);
2484 // Mixin constructors
2485 OO
.ui
.mixin
.PendingElement
.call( this );
2488 this.actions
= new OO
.ui
.ActionSet();
2489 this.attachedActions
= [];
2490 this.currentAction
= null;
2491 this.onDialogKeyDownHandler
= this.onDialogKeyDown
.bind( this );
2494 this.actions
.connect( this, {
2495 click
: 'onActionClick',
2496 change
: 'onActionsChange'
2501 .addClass( 'oo-ui-dialog' )
2502 .attr( 'role', 'dialog' );
2507 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2508 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.mixin
.PendingElement
);
2510 /* Static Properties */
2513 * Symbolic name of dialog.
2515 * The dialog class must have a symbolic name in order to be registered with OO.Factory.
2516 * Please see the [OOUI documentation on MediaWiki] [3] for more information.
2518 * [3]: https://www.mediawiki.org/wiki/OOUI/Windows/Window_managers
2523 * @property {string}
2525 OO
.ui
.Dialog
.static.name
= '';
2530 * The title can be specified as a plaintext string, a {@link OO.ui.mixin.LabelElement Label} node, or a function
2531 * that will produce a Label node or string. The title can also be specified with data passed to the
2532 * constructor (see #getSetupProcess). In this case, the static value will be overridden.
2537 * @property {jQuery|string|Function}
2539 OO
.ui
.Dialog
.static.title
= '';
2542 * An array of configured {@link OO.ui.ActionWidget action widgets}.
2544 * Actions can also be specified with data passed to the constructor (see #getSetupProcess). In this case, the static
2545 * value will be overridden.
2547 * [2]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs#Action_sets
2551 * @property {Object[]}
2553 OO
.ui
.Dialog
.static.actions
= [];
2556 * Close the dialog when the 'Esc' key is pressed.
2561 * @property {boolean}
2563 OO
.ui
.Dialog
.static.escapable
= true;
2568 * Handle frame document key down events.
2571 * @param {jQuery.Event} e Key down event
2573 OO
.ui
.Dialog
.prototype.onDialogKeyDown = function ( e
) {
2575 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
&& this.constructor.static.escapable
) {
2576 this.executeAction( '' );
2578 e
.stopPropagation();
2579 } else if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& ( e
.ctrlKey
|| e
.metaKey
) ) {
2580 actions
= this.actions
.get( { flags
: 'primary', visible
: true, disabled
: false } );
2581 if ( actions
.length
> 0 ) {
2582 this.executeAction( actions
[ 0 ].getAction() );
2584 e
.stopPropagation();
2590 * Handle action click events.
2593 * @param {OO.ui.ActionWidget} action Action that was clicked
2595 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2596 if ( !this.isPending() ) {
2597 this.executeAction( action
.getAction() );
2602 * Handle actions change event.
2606 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2607 this.detachActions();
2608 if ( !this.isClosing() ) {
2609 this.attachActions();
2614 * Get the set of actions used by the dialog.
2616 * @return {OO.ui.ActionSet}
2618 OO
.ui
.Dialog
.prototype.getActions = function () {
2619 return this.actions
;
2623 * Get a process for taking action.
2625 * When you override this method, you can create a new OO.ui.Process and return it, or add additional
2626 * accept steps to the process the parent method provides using the {@link OO.ui.Process#first 'first'}
2627 * and {@link OO.ui.Process#next 'next'} methods of OO.ui.Process.
2629 * @param {string} [action] Symbolic name of action
2630 * @return {OO.ui.Process} Action process
2632 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2633 return new OO
.ui
.Process()
2634 .next( function () {
2636 // An empty action always closes the dialog without data, which should always be
2637 // safe and make no changes
2646 * @param {Object} [data] Dialog opening data
2647 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use
2648 * the {@link #static-title static title}
2649 * @param {Object[]} [data.actions] List of configuration options for each
2650 * {@link OO.ui.ActionWidget action widget}, omit to use {@link #static-actions static actions}.
2652 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2656 return OO
.ui
.Dialog
.parent
.prototype.getSetupProcess
.call( this, data
)
2657 .next( function () {
2658 var config
= this.constructor.static,
2659 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
,
2660 title
= data
.title
!== undefined ? data
.title
: config
.title
;
2662 this.title
.setLabel( title
).setTitle( title
);
2663 this.actions
.add( this.getActionWidgets( actions
) );
2665 this.$element
.on( 'keydown', this.onDialogKeyDownHandler
);
2672 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2674 return OO
.ui
.Dialog
.parent
.prototype.getTeardownProcess
.call( this, data
)
2675 .first( function () {
2676 this.$element
.off( 'keydown', this.onDialogKeyDownHandler
);
2678 this.actions
.clear();
2679 this.currentAction
= null;
2686 OO
.ui
.Dialog
.prototype.initialize = function () {
2688 OO
.ui
.Dialog
.parent
.prototype.initialize
.call( this );
2691 this.title
= new OO
.ui
.LabelWidget();
2694 this.$content
.addClass( 'oo-ui-dialog-content' );
2695 this.$element
.attr( 'aria-labelledby', this.title
.getElementId() );
2696 this.setPendingElement( this.$head
);
2700 * Get action widgets from a list of configs
2702 * @param {Object[]} actions Action widget configs
2703 * @return {OO.ui.ActionWidget[]} Action widgets
2705 OO
.ui
.Dialog
.prototype.getActionWidgets = function ( actions
) {
2706 var i
, len
, widgets
= [];
2707 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2709 new OO
.ui
.ActionWidget( actions
[ i
] )
2716 * Attach action actions.
2720 OO
.ui
.Dialog
.prototype.attachActions = function () {
2721 // Remember the list of potentially attached actions
2722 this.attachedActions
= this.actions
.get();
2726 * Detach action actions.
2731 OO
.ui
.Dialog
.prototype.detachActions = function () {
2734 // Detach all actions that may have been previously attached
2735 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2736 this.attachedActions
[ i
].$element
.detach();
2738 this.attachedActions
= [];
2742 * Execute an action.
2744 * @param {string} action Symbolic name of action to execute
2745 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2747 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2749 this.currentAction
= action
;
2750 return this.getActionProcess( action
).execute()
2751 .always( this.popPending
.bind( this ) );
2755 * MessageDialogs display a confirmation or alert message. By default, the rendered dialog box
2756 * consists of a header that contains the dialog title, a body with the message, and a footer that
2757 * contains any {@link OO.ui.ActionWidget action widgets}. The MessageDialog class is the only type
2758 * of {@link OO.ui.Dialog dialog} that is usually instantiated directly.
2760 * There are two basic types of message dialogs, confirmation and alert:
2762 * - **confirmation**: the dialog title describes what a progressive action will do and the message provides
2763 * more details about the consequences.
2764 * - **alert**: the dialog title describes which event occurred and the message provides more information
2765 * about why the event occurred.
2767 * The MessageDialog class specifies two actions: ‘accept’, the primary
2768 * action (e.g., ‘ok’) and ‘reject,’ the safe action (e.g., ‘cancel’). Both will close the window,
2769 * passing along the selected action.
2771 * For more information and examples, please see the [OOUI documentation on MediaWiki][1].
2774 * // Example: Creating and opening a message dialog window.
2775 * var messageDialog = new OO.ui.MessageDialog();
2777 * // Create and append a window manager.
2778 * var windowManager = new OO.ui.WindowManager();
2779 * $( 'body' ).append( windowManager.$element );
2780 * windowManager.addWindows( [ messageDialog ] );
2781 * // Open the window.
2782 * windowManager.openWindow( messageDialog, {
2783 * title: 'Basic message dialog',
2784 * message: 'This is the message'
2787 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Message_Dialogs
2790 * @extends OO.ui.Dialog
2793 * @param {Object} [config] Configuration options
2795 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
2796 // Parent constructor
2797 OO
.ui
.MessageDialog
.parent
.call( this, config
);
2800 this.verticalActionLayout
= null;
2803 this.$element
.addClass( 'oo-ui-messageDialog' );
2808 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
2810 /* Static Properties */
2816 OO
.ui
.MessageDialog
.static.name
= 'message';
2822 OO
.ui
.MessageDialog
.static.size
= 'small';
2827 * The title of a confirmation dialog describes what a progressive action will do. The
2828 * title of an alert dialog describes which event occurred.
2832 * @property {jQuery|string|Function|null}
2834 OO
.ui
.MessageDialog
.static.title
= null;
2837 * The message displayed in the dialog body.
2839 * A confirmation message describes the consequences of a progressive action. An alert
2840 * message describes why an event occurred.
2844 * @property {jQuery|string|Function|null}
2846 OO
.ui
.MessageDialog
.static.message
= null;
2852 OO
.ui
.MessageDialog
.static.actions
= [
2853 // Note that OO.ui.alert() and OO.ui.confirm() rely on these.
2854 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
2855 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
2863 OO
.ui
.MessageDialog
.prototype.setManager = function ( manager
) {
2864 OO
.ui
.MessageDialog
.parent
.prototype.setManager
.call( this, manager
);
2867 this.manager
.connect( this, {
2875 * Handle window resized events.
2879 OO
.ui
.MessageDialog
.prototype.onResize = function () {
2881 dialog
.fitActions();
2882 // Wait for CSS transition to finish and do it again :(
2883 setTimeout( function () {
2884 dialog
.fitActions();
2889 * Toggle action layout between vertical and horizontal.
2892 * @param {boolean} [value] Layout actions vertically, omit to toggle
2895 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
2896 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
2898 if ( value
!== this.verticalActionLayout
) {
2899 this.verticalActionLayout
= value
;
2901 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
2902 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
2911 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
2913 return new OO
.ui
.Process( function () {
2914 this.close( { action
: action
} );
2917 return OO
.ui
.MessageDialog
.parent
.prototype.getActionProcess
.call( this, action
);
2923 * @param {Object} [data] Dialog opening data
2924 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
2925 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
2926 * @param {string} [data.size] Symbolic name of the dialog size, see OO.ui.Window
2927 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
2930 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
2934 return OO
.ui
.MessageDialog
.parent
.prototype.getSetupProcess
.call( this, data
)
2935 .next( function () {
2936 this.title
.setLabel(
2937 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2939 this.message
.setLabel(
2940 data
.message
!== undefined ? data
.message
: this.constructor.static.message
2942 this.size
= data
.size
!== undefined ? data
.size
: this.constructor.static.size
;
2949 OO
.ui
.MessageDialog
.prototype.getReadyProcess = function ( data
) {
2953 return OO
.ui
.MessageDialog
.parent
.prototype.getReadyProcess
.call( this, data
)
2954 .next( function () {
2955 // Focus the primary action button
2956 var actions
= this.actions
.get();
2957 actions
= actions
.filter( function ( action
) {
2958 return action
.getFlags().indexOf( 'primary' ) > -1;
2960 if ( actions
.length
> 0 ) {
2961 actions
[ 0 ].focus();
2969 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
2970 var bodyHeight
, oldOverflow
,
2971 $scrollable
= this.container
.$element
;
2973 oldOverflow
= $scrollable
[ 0 ].style
.overflow
;
2974 $scrollable
[ 0 ].style
.overflow
= 'hidden';
2976 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
2978 bodyHeight
= this.text
.$element
.outerHeight( true );
2979 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
2987 OO
.ui
.MessageDialog
.prototype.setDimensions = function ( dim
) {
2988 var $scrollable
= this.container
.$element
;
2989 OO
.ui
.MessageDialog
.parent
.prototype.setDimensions
.call( this, dim
);
2991 // Twiddle the overflow property, otherwise an unnecessary scrollbar will be produced.
2992 // Need to do it after transition completes (250ms), add 50ms just in case.
2993 setTimeout( function () {
2994 var oldOverflow
= $scrollable
[ 0 ].style
.overflow
,
2995 activeElement
= document
.activeElement
;
2997 $scrollable
[ 0 ].style
.overflow
= 'hidden';
2999 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
3001 // Check reconsiderScrollbars didn't destroy our focus, as we
3002 // are doing this after the ready process.
3003 if ( activeElement
&& activeElement
!== document
.activeElement
&& activeElement
.focus
) {
3004 activeElement
.focus();
3007 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
3016 OO
.ui
.MessageDialog
.prototype.initialize = function () {
3018 OO
.ui
.MessageDialog
.parent
.prototype.initialize
.call( this );
3021 this.$actions
= $( '<div>' );
3022 this.container
= new OO
.ui
.PanelLayout( {
3023 scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
3025 this.text
= new OO
.ui
.PanelLayout( {
3026 padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
3028 this.message
= new OO
.ui
.LabelWidget( {
3029 classes
: [ 'oo-ui-messageDialog-message' ]
3033 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
3034 this.$content
.addClass( 'oo-ui-messageDialog-content' );
3035 this.container
.$element
.append( this.text
.$element
);
3036 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
3037 this.$body
.append( this.container
.$element
);
3038 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
3039 this.$foot
.append( this.$actions
);
3045 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
3046 var i
, len
, other
, special
, others
;
3049 OO
.ui
.MessageDialog
.parent
.prototype.attachActions
.call( this );
3051 special
= this.actions
.getSpecial();
3052 others
= this.actions
.getOthers();
3054 if ( special
.safe
) {
3055 this.$actions
.append( special
.safe
.$element
);
3056 special
.safe
.toggleFramed( false );
3058 if ( others
.length
) {
3059 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
3060 other
= others
[ i
];
3061 this.$actions
.append( other
.$element
);
3062 other
.toggleFramed( false );
3065 if ( special
.primary
) {
3066 this.$actions
.append( special
.primary
.$element
);
3067 special
.primary
.toggleFramed( false );
3070 if ( !this.isOpening() ) {
3071 // If the dialog is currently opening, this will be called automatically soon.
3072 // This also calls #fitActions.
3078 * Fit action actions into columns or rows.
3080 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
3084 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
3086 previous
= this.verticalActionLayout
,
3087 actions
= this.actions
.get();
3090 this.toggleVerticalActionLayout( false );
3091 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
3092 action
= actions
[ i
];
3093 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
3094 this.toggleVerticalActionLayout( true );
3099 // Move the body out of the way of the foot
3100 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
3102 if ( this.verticalActionLayout
!== previous
) {
3103 // We changed the layout, window height might need to be updated.
3109 * ProcessDialog windows encapsulate a {@link OO.ui.Process process} and all of the code necessary
3110 * to complete it. If the process terminates with an error, a customizable {@link OO.ui.Error error
3111 * interface} alerts users to the trouble, permitting the user to dismiss the error and try again when
3112 * relevant. The ProcessDialog class is always extended and customized with the actions and content
3113 * required for each process.
3115 * The process dialog box consists of a header that visually represents the ‘working’ state of long
3116 * processes with an animation. The header contains the dialog title as well as
3117 * two {@link OO.ui.ActionWidget action widgets}: a ‘safe’ action on the left (e.g., ‘Cancel’) and
3118 * a ‘primary’ action on the right (e.g., ‘Done’).
3120 * Like other windows, the process dialog is managed by a {@link OO.ui.WindowManager window manager}.
3121 * Please see the [OOUI documentation on MediaWiki][1] for more information and examples.
3124 * // Example: Creating and opening a process dialog window.
3125 * function MyProcessDialog( config ) {
3126 * MyProcessDialog.parent.call( this, config );
3128 * OO.inheritClass( MyProcessDialog, OO.ui.ProcessDialog );
3130 * MyProcessDialog.static.name = 'myProcessDialog';
3131 * MyProcessDialog.static.title = 'Process dialog';
3132 * MyProcessDialog.static.actions = [
3133 * { action: 'save', label: 'Done', flags: 'primary' },
3134 * { label: 'Cancel', flags: 'safe' }
3137 * MyProcessDialog.prototype.initialize = function () {
3138 * MyProcessDialog.parent.prototype.initialize.apply( this, arguments );
3139 * this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
3140 * 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>' );
3141 * this.$body.append( this.content.$element );
3143 * MyProcessDialog.prototype.getActionProcess = function ( action ) {
3144 * var dialog = this;
3146 * return new OO.ui.Process( function () {
3147 * dialog.close( { action: action } );
3150 * return MyProcessDialog.parent.prototype.getActionProcess.call( this, action );
3153 * var windowManager = new OO.ui.WindowManager();
3154 * $( 'body' ).append( windowManager.$element );
3156 * var dialog = new MyProcessDialog();
3157 * windowManager.addWindows( [ dialog ] );
3158 * windowManager.openWindow( dialog );
3160 * [1]: https://www.mediawiki.org/wiki/OOUI/Windows/Process_Dialogs
3164 * @extends OO.ui.Dialog
3167 * @param {Object} [config] Configuration options
3169 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
3170 // Parent constructor
3171 OO
.ui
.ProcessDialog
.parent
.call( this, config
);
3174 this.fitOnOpen
= false;
3177 this.$element
.addClass( 'oo-ui-processDialog' );
3182 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
3187 * Handle dismiss button click events.
3193 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
3198 * Handle retry button click events.
3200 * Hides errors and then tries again.
3204 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
3206 this.executeAction( this.currentAction
);
3212 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
3214 OO
.ui
.ProcessDialog
.parent
.prototype.initialize
.call( this );
3217 this.$navigation
= $( '<div>' );
3218 this.$location
= $( '<div>' );
3219 this.$safeActions
= $( '<div>' );
3220 this.$primaryActions
= $( '<div>' );
3221 this.$otherActions
= $( '<div>' );
3222 this.dismissButton
= new OO
.ui
.ButtonWidget( {
3223 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
3225 this.retryButton
= new OO
.ui
.ButtonWidget();
3226 this.$errors
= $( '<div>' );
3227 this.$errorsTitle
= $( '<div>' );
3230 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
3231 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
3234 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
3236 .append( this.title
.$element
)
3237 .addClass( 'oo-ui-processDialog-location' );
3238 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
3239 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
3240 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
3242 .addClass( 'oo-ui-processDialog-errors-title' )
3243 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
3245 .addClass( 'oo-ui-processDialog-errors oo-ui-element-hidden' )
3246 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
3248 .addClass( 'oo-ui-processDialog-content' )
3249 .append( this.$errors
);
3251 .addClass( 'oo-ui-processDialog-navigation' )
3252 // Note: Order of appends below is important. These are in the order
3253 // we want tab to go through them. Display-order is handled entirely
3254 // by CSS absolute-positioning. As such, primary actions like "done"
3256 .append( this.$primaryActions
, this.$location
, this.$safeActions
);
3257 this.$head
.append( this.$navigation
);
3258 this.$foot
.append( this.$otherActions
);
3264 OO
.ui
.ProcessDialog
.prototype.getActionWidgets = function ( actions
) {
3266 isMobile
= OO
.ui
.isMobile(),
3269 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
3270 config
= $.extend( { framed
: !OO
.ui
.isMobile() }, actions
[ i
] );
3272 ( config
.flags
=== 'back' || ( Array
.isArray( config
.flags
) && config
.flags
.indexOf( 'back' ) !== -1 ) )
3280 new OO
.ui
.ActionWidget( config
)
3289 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
3290 var i
, len
, other
, special
, others
;
3293 OO
.ui
.ProcessDialog
.parent
.prototype.attachActions
.call( this );
3295 special
= this.actions
.getSpecial();
3296 others
= this.actions
.getOthers();
3297 if ( special
.primary
) {
3298 this.$primaryActions
.append( special
.primary
.$element
);
3300 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
3301 other
= others
[ i
];
3302 this.$otherActions
.append( other
.$element
);
3304 if ( special
.safe
) {
3305 this.$safeActions
.append( special
.safe
.$element
);
3309 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
3315 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
3317 return OO
.ui
.ProcessDialog
.parent
.prototype.executeAction
.call( this, action
)
3318 .fail( function ( errors
) {
3319 process
.showErrors( errors
|| [] );
3326 OO
.ui
.ProcessDialog
.prototype.setDimensions = function () {
3328 OO
.ui
.ProcessDialog
.parent
.prototype.setDimensions
.apply( this, arguments
);
3334 * Fit label between actions.
3339 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
3340 var safeWidth
, primaryWidth
, biggerWidth
, labelWidth
, navigationWidth
, leftWidth
, rightWidth
,
3341 size
= this.getSizeProperties();
3343 if ( typeof size
.width
!== 'number' ) {
3344 if ( this.isOpened() ) {
3345 navigationWidth
= this.$head
.width() - 20;
3346 } else if ( this.isOpening() ) {
3347 if ( !this.fitOnOpen
) {
3348 // Size is relative and the dialog isn't open yet, so wait.
3349 // FIXME: This should ideally be handled by setup somehow.
3350 this.manager
.lifecycle
.opened
.done( this.fitLabel
.bind( this ) );
3351 this.fitOnOpen
= true;
3358 navigationWidth
= size
.width
- 20;
3361 safeWidth
= this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0;
3362 primaryWidth
= this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0;
3363 biggerWidth
= Math
.max( safeWidth
, primaryWidth
);
3365 labelWidth
= this.title
.$element
.width();
3367 if ( 2 * biggerWidth
+ labelWidth
< navigationWidth
) {
3368 // We have enough space to center the label
3369 leftWidth
= rightWidth
= biggerWidth
;
3371 // Let's hope we at least have enough space not to overlap, because we can't wrap the label…
3372 if ( this.getDir() === 'ltr' ) {
3373 leftWidth
= safeWidth
;
3374 rightWidth
= primaryWidth
;
3376 leftWidth
= primaryWidth
;
3377 rightWidth
= safeWidth
;
3381 this.$location
.css( { paddingLeft
: leftWidth
, paddingRight
: rightWidth
} );
3387 * Handle errors that occurred during accept or reject processes.
3390 * @param {OO.ui.Error[]|OO.ui.Error} errors Errors to be handled
3392 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
3393 var i
, len
, $item
, actions
,
3399 if ( errors
instanceof OO
.ui
.Error
) {
3400 errors
= [ errors
];
3403 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
3404 if ( !errors
[ i
].isRecoverable() ) {
3405 recoverable
= false;
3407 if ( errors
[ i
].isWarning() ) {
3410 $item
= $( '<div>' )
3411 .addClass( 'oo-ui-processDialog-error' )
3412 .append( errors
[ i
].getMessage() );
3413 items
.push( $item
[ 0 ] );
3415 this.$errorItems
= $( items
);
3416 if ( recoverable
) {
3417 abilities
[ this.currentAction
] = true;
3418 // Copy the flags from the first matching action
3419 actions
= this.actions
.get( { actions
: this.currentAction
} );
3420 if ( actions
.length
) {
3421 this.retryButton
.clearFlags().setFlags( actions
[ 0 ].getFlags() );
3424 abilities
[ this.currentAction
] = false;
3425 this.actions
.setAbilities( abilities
);
3428 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
3430 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
3432 this.retryButton
.toggle( recoverable
);
3433 this.$errorsTitle
.after( this.$errorItems
);
3434 this.$errors
.removeClass( 'oo-ui-element-hidden' ).scrollTop( 0 );
3442 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
3443 this.$errors
.addClass( 'oo-ui-element-hidden' );
3444 if ( this.$errorItems
) {
3445 this.$errorItems
.remove();
3446 this.$errorItems
= null;
3453 OO
.ui
.ProcessDialog
.prototype.getTeardownProcess = function ( data
) {
3455 return OO
.ui
.ProcessDialog
.parent
.prototype.getTeardownProcess
.call( this, data
)
3456 .first( function () {
3457 // Make sure to hide errors
3459 this.fitOnOpen
= false;
3468 * Lazy-initialize and return a global OO.ui.WindowManager instance, used by OO.ui.alert and
3472 * @return {OO.ui.WindowManager}
3474 OO
.ui
.getWindowManager = function () {
3475 if ( !OO
.ui
.windowManager
) {
3476 OO
.ui
.windowManager
= new OO
.ui
.WindowManager();
3477 $( 'body' ).append( OO
.ui
.windowManager
.$element
);
3478 OO
.ui
.windowManager
.addWindows( [ new OO
.ui
.MessageDialog() ] );
3480 return OO
.ui
.windowManager
;
3484 * Display a quick modal alert dialog, using a OO.ui.MessageDialog. While the dialog is open, the
3485 * rest of the page will be dimmed out and the user won't be able to interact with it. The dialog
3486 * has only one action button, labelled "OK", clicking it will simply close the dialog.
3488 * A window manager is created automatically when this function is called for the first time.
3491 * OO.ui.alert( 'Something happened!' ).done( function () {
3492 * console.log( 'User closed the dialog.' );
3495 * OO.ui.alert( 'Something larger happened!', { size: 'large' } );
3497 * @param {jQuery|string} text Message text to display
3498 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3499 * @return {jQuery.Promise} Promise resolved when the user closes the dialog
3501 OO
.ui
.alert = function ( text
, options
) {
3502 return OO
.ui
.getWindowManager().openWindow( 'message', $.extend( {
3504 actions
: [ OO
.ui
.MessageDialog
.static.actions
[ 0 ] ]
3505 }, options
) ).closed
.then( function () {
3511 * Display a quick modal confirmation dialog, using a OO.ui.MessageDialog. While the dialog is open,
3512 * the rest of the page will be dimmed out and the user won't be able to interact with it. The
3513 * dialog has two action buttons, one to confirm an operation (labelled "OK") and one to cancel it
3514 * (labelled "Cancel").
3516 * A window manager is created automatically when this function is called for the first time.
3519 * OO.ui.confirm( 'Are you sure?' ).done( function ( confirmed ) {
3520 * if ( confirmed ) {
3521 * console.log( 'User clicked "OK"!' );
3523 * console.log( 'User clicked "Cancel" or closed the dialog.' );
3527 * @param {jQuery|string} text Message text to display
3528 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3529 * @return {jQuery.Promise} Promise resolved when the user closes the dialog. If the user chose to
3530 * confirm, the promise will resolve to boolean `true`; otherwise, it will resolve to boolean
3533 OO
.ui
.confirm = function ( text
, options
) {
3534 return OO
.ui
.getWindowManager().openWindow( 'message', $.extend( {
3536 }, options
) ).closed
.then( function ( data
) {
3537 return !!( data
&& data
.action
=== 'accept' );
3542 * Display a quick modal prompt dialog, using a OO.ui.MessageDialog. While the dialog is open,
3543 * the rest of the page will be dimmed out and the user won't be able to interact with it. The
3544 * dialog has a text input widget and two action buttons, one to confirm an operation (labelled "OK")
3545 * and one to cancel it (labelled "Cancel").
3547 * A window manager is created automatically when this function is called for the first time.
3550 * OO.ui.prompt( 'Choose a line to go to', { textInput: { placeholder: 'Line number' } } ).done( function ( result ) {
3551 * if ( result !== null ) {
3552 * console.log( 'User typed "' + result + '" then clicked "OK".' );
3554 * console.log( 'User clicked "Cancel" or closed the dialog.' );
3558 * @param {jQuery|string} text Message text to display
3559 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3560 * @param {Object} [options.textInput] Additional options for text input widget, see OO.ui.TextInputWidget
3561 * @return {jQuery.Promise} Promise resolved when the user closes the dialog. If the user chose to
3562 * confirm, the promise will resolve with the value of the text input widget; otherwise, it will
3563 * resolve to `null`.
3565 OO
.ui
.prompt = function ( text
, options
) {
3567 manager
= OO
.ui
.getWindowManager(),
3568 textInput
= new OO
.ui
.TextInputWidget( ( options
&& options
.textInput
) || {} ),
3569 textField
= new OO
.ui
.FieldLayout( textInput
, {
3574 instance
= manager
.openWindow( 'message', $.extend( {
3575 message
: textField
.$element
3578 // TODO: This is a little hacky, and could be done by extending MessageDialog instead.
3579 instance
.opened
.then( function () {
3580 textInput
.on( 'enter', function () {
3581 manager
.getCurrentWindow().close( { action
: 'accept' } );
3586 return instance
.closed
.then( function ( data
) {
3587 return data
&& data
.action
=== 'accept' ? textInput
.getValue() : null;
3593 //# sourceMappingURL=oojs-ui-windows.js.map