3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2017 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2017-02-08T00:38:31Z
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 [OOjs UI documentation on MediaWiki] [1] for more information
24 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/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 * A resize event is emitted when the size of the widget changes.
74 * Check if the action is configured to be available in the specified `mode`.
76 * @param {string} mode Name of mode
77 * @return {boolean} The action is configured with the mode
79 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
80 return this.modes
.indexOf( mode
) !== -1;
84 * Get the symbolic name of the action (e.g., ‘continue’ or ‘cancel’).
88 OO
.ui
.ActionWidget
.prototype.getAction = function () {
93 * Get the symbolic name of the mode or modes for which the action is configured to be available.
95 * The current mode is set with the action set's {@link OO.ui.ActionSet#setMode setMode} method.
96 * Only actions that are configured to be avaiable in the current mode will be visible. All other actions
101 OO
.ui
.ActionWidget
.prototype.getModes = function () {
102 return this.modes
.slice();
106 * Emit a resize event if the size has changed.
111 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
114 if ( this.isElementAttached() ) {
115 width
= this.$element
.width();
116 height
= this.$element
.height();
118 if ( width
!== this.width
|| height
!== this.height
) {
120 this.height
= height
;
121 this.emit( 'resize' );
131 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
133 OO
.ui
.mixin
.IconElement
.prototype.setIcon
.apply( this, arguments
);
134 this.propagateResize();
142 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
144 OO
.ui
.mixin
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
145 this.propagateResize();
153 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
155 OO
.ui
.mixin
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
156 this.propagateResize();
164 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
166 OO
.ui
.mixin
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
167 this.propagateResize();
173 * Toggle the visibility of the action button.
175 * @param {boolean} [show] Show button, omit to toggle visibility
178 OO
.ui
.ActionWidget
.prototype.toggle = function () {
180 OO
.ui
.ActionWidget
.parent
.prototype.toggle
.apply( this, arguments
);
181 this.propagateResize();
186 /* eslint-disable no-unused-vars */
188 * ActionSets manage the behavior of the {@link OO.ui.ActionWidget action widgets} that comprise them.
189 * Actions can be made available for specific contexts (modes) and circumstances
190 * (abilities). Action sets are primarily used with {@link OO.ui.Dialog Dialogs}.
192 * ActionSets contain two types of actions:
194 * - 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.
195 * - Other: Other actions include all non-special visible actions.
197 * Please see the [OOjs UI documentation on MediaWiki][1] for more information.
200 * // Example: An action set used in a process dialog
201 * function MyProcessDialog( config ) {
202 * MyProcessDialog.parent.call( this, config );
204 * OO.inheritClass( MyProcessDialog, OO.ui.ProcessDialog );
205 * MyProcessDialog.static.title = 'An action set in a process dialog';
206 * MyProcessDialog.static.name = 'myProcessDialog';
207 * // An action set that uses modes ('edit' and 'help' mode, in this example).
208 * MyProcessDialog.static.actions = [
209 * { action: 'continue', modes: 'edit', label: 'Continue', flags: [ 'primary', 'constructive' ] },
210 * { action: 'help', modes: 'edit', label: 'Help' },
211 * { modes: 'edit', label: 'Cancel', flags: 'safe' },
212 * { action: 'back', modes: 'help', label: 'Back', flags: 'safe' }
215 * MyProcessDialog.prototype.initialize = function () {
216 * MyProcessDialog.parent.prototype.initialize.apply( this, arguments );
217 * this.panel1 = new OO.ui.PanelLayout( { padded: true, expanded: false } );
218 * 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>' );
219 * this.panel2 = new OO.ui.PanelLayout( { padded: true, expanded: false } );
220 * 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>' );
221 * this.stackLayout = new OO.ui.StackLayout( {
222 * items: [ this.panel1, this.panel2 ]
224 * this.$body.append( this.stackLayout.$element );
226 * MyProcessDialog.prototype.getSetupProcess = function ( data ) {
227 * return MyProcessDialog.parent.prototype.getSetupProcess.call( this, data )
228 * .next( function () {
229 * this.actions.setMode( 'edit' );
232 * MyProcessDialog.prototype.getActionProcess = function ( action ) {
233 * if ( action === 'help' ) {
234 * this.actions.setMode( 'help' );
235 * this.stackLayout.setItem( this.panel2 );
236 * } else if ( action === 'back' ) {
237 * this.actions.setMode( 'edit' );
238 * this.stackLayout.setItem( this.panel1 );
239 * } else if ( action === 'continue' ) {
241 * return new OO.ui.Process( function () {
245 * return MyProcessDialog.parent.prototype.getActionProcess.call( this, action );
247 * MyProcessDialog.prototype.getBodyHeight = function () {
248 * return this.panel1.$element.outerHeight( true );
250 * var windowManager = new OO.ui.WindowManager();
251 * $( 'body' ).append( windowManager.$element );
252 * var dialog = new MyProcessDialog( {
255 * windowManager.addWindows( [ dialog ] );
256 * windowManager.openWindow( dialog );
258 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Process_Dialogs#Action_sets
262 * @mixins OO.EventEmitter
265 * @param {Object} [config] Configuration options
267 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
268 // Configuration initialization
269 config
= config
|| {};
271 // Mixin constructors
272 OO
.EventEmitter
.call( this );
277 actions
: 'getAction',
281 this.categorized
= {};
284 this.organized
= false;
285 this.changing
= false;
286 this.changed
= false;
288 /* eslint-enable no-unused-vars */
292 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
294 /* Static Properties */
297 * Symbolic name of the flags used to identify special actions. Special actions are displayed in the
298 * header of a {@link OO.ui.ProcessDialog process dialog}.
299 * See the [OOjs UI documentation on MediaWiki][2] for more information and examples.
301 * [2]:https://www.mediawiki.org/wiki/OOjs_UI/Windows/Process_Dialogs
308 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
315 * A 'click' event is emitted when an action is clicked.
317 * @param {OO.ui.ActionWidget} action Action that was clicked
323 * A 'resize' event is emitted when an action widget is resized.
325 * @param {OO.ui.ActionWidget} action Action that was resized
331 * An 'add' event is emitted when actions are {@link #method-add added} to the action set.
333 * @param {OO.ui.ActionWidget[]} added Actions added
339 * A 'remove' event is emitted when actions are {@link #method-remove removed}
340 * or {@link #clear cleared}.
342 * @param {OO.ui.ActionWidget[]} added Actions removed
348 * A 'change' event is emitted when actions are {@link #method-add added}, {@link #clear cleared},
349 * or {@link #method-remove removed} from the action set or when the {@link #setMode mode} is changed.
356 * Handle action change events.
361 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
362 this.organized
= false;
363 if ( this.changing
) {
366 this.emit( 'change' );
371 * Check if an action is one of the special actions.
373 * @param {OO.ui.ActionWidget} action Action to check
374 * @return {boolean} Action is special
376 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
379 for ( flag
in this.special
) {
380 if ( action
=== this.special
[ flag
] ) {
389 * Get action widgets based on the specified filter: ‘actions’, ‘flags’, ‘modes’, ‘visible’,
392 * @param {Object} [filters] Filters to use, omit to get all actions
393 * @param {string|string[]} [filters.actions] Actions that action widgets must have
394 * @param {string|string[]} [filters.flags] Flags that action widgets must have (e.g., 'safe')
395 * @param {string|string[]} [filters.modes] Modes that action widgets must have
396 * @param {boolean} [filters.visible] Action widgets must be visible
397 * @param {boolean} [filters.disabled] Action widgets must be disabled
398 * @return {OO.ui.ActionWidget[]} Action widgets matching all criteria
400 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
401 var i
, len
, list
, category
, actions
, index
, match
, matches
;
406 // Collect category candidates
408 for ( category
in this.categorized
) {
409 list
= filters
[ category
];
411 if ( !Array
.isArray( list
) ) {
414 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
415 actions
= this.categorized
[ category
][ list
[ i
] ];
416 if ( Array
.isArray( actions
) ) {
417 matches
.push
.apply( matches
, actions
);
422 // Remove by boolean filters
423 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
424 match
= matches
[ i
];
426 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
427 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
429 matches
.splice( i
, 1 );
435 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
436 match
= matches
[ i
];
437 index
= matches
.lastIndexOf( match
);
438 while ( index
!== i
) {
439 matches
.splice( index
, 1 );
441 index
= matches
.lastIndexOf( match
);
446 return this.list
.slice();
450 * Get 'special' actions.
452 * Special actions are the first visible action widgets with special flags, such as 'safe' and 'primary'.
453 * Special flags can be configured in subclasses by changing the static #specialFlags property.
455 * @return {OO.ui.ActionWidget[]|null} 'Special' action widgets.
457 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
459 return $.extend( {}, this.special
);
463 * Get 'other' actions.
465 * Other actions include all non-special visible action widgets.
467 * @return {OO.ui.ActionWidget[]} 'Other' action widgets
469 OO
.ui
.ActionSet
.prototype.getOthers = function () {
471 return this.others
.slice();
475 * Set the mode (e.g., ‘edit’ or ‘view’). Only {@link OO.ui.ActionWidget#modes actions} configured
476 * to be available in the specified mode will be made visible. All other actions will be hidden.
478 * @param {string} mode The mode. Only actions configured to be available in the specified
479 * mode will be made visible.
484 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
487 this.changing
= true;
488 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
489 action
= this.list
[ i
];
490 action
.toggle( action
.hasMode( mode
) );
493 this.organized
= false;
494 this.changing
= false;
495 this.emit( 'change' );
501 * Set the abilities of the specified actions.
503 * Action widgets that are configured with the specified actions will be enabled
504 * or disabled based on the boolean values specified in the `actions`
507 * @param {Object.<string,boolean>} actions A list keyed by action name with boolean
508 * values that indicate whether or not the action should be enabled.
511 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
512 var i
, len
, action
, item
;
514 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
515 item
= this.list
[ i
];
516 action
= item
.getAction();
517 if ( actions
[ action
] !== undefined ) {
518 item
.setDisabled( !actions
[ action
] );
526 * Executes a function once per action.
528 * When making changes to multiple actions, use this method instead of iterating over the actions
529 * manually to defer emitting a #change event until after all actions have been changed.
531 * @param {Object|null} filter Filters to use to determine which actions to iterate over; see #get
532 * @param {Function} callback Callback to run for each action; callback is invoked with three
533 * arguments: the action, the action's index, the list of actions being iterated over
536 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
537 this.changed
= false;
538 this.changing
= true;
539 this.get( filter
).forEach( callback
);
540 this.changing
= false;
541 if ( this.changed
) {
542 this.emit( 'change' );
549 * Add action widgets to the action set.
551 * @param {OO.ui.ActionWidget[]} actions Action widgets to add
556 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
559 this.changing
= true;
560 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
561 action
= actions
[ i
];
562 action
.connect( this, {
563 click
: [ 'emit', 'click', action
],
564 resize
: [ 'emit', 'resize', action
],
565 toggle
: [ 'onActionChange' ]
567 this.list
.push( action
);
569 this.organized
= false;
570 this.emit( 'add', actions
);
571 this.changing
= false;
572 this.emit( 'change' );
578 * Remove action widgets from the set.
580 * To remove all actions, you may wish to use the #clear method instead.
582 * @param {OO.ui.ActionWidget[]} actions Action widgets to remove
587 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
588 var i
, len
, index
, action
;
590 this.changing
= true;
591 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
592 action
= actions
[ i
];
593 index
= this.list
.indexOf( action
);
594 if ( index
!== -1 ) {
595 action
.disconnect( this );
596 this.list
.splice( index
, 1 );
599 this.organized
= false;
600 this.emit( 'remove', actions
);
601 this.changing
= false;
602 this.emit( 'change' );
608 * Remove all action widets from the set.
610 * To remove only specified actions, use the {@link #method-remove remove} method instead.
616 OO
.ui
.ActionSet
.prototype.clear = function () {
618 removed
= this.list
.slice();
620 this.changing
= true;
621 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
622 action
= this.list
[ i
];
623 action
.disconnect( this );
628 this.organized
= false;
629 this.emit( 'remove', removed
);
630 this.changing
= false;
631 this.emit( 'change' );
639 * This is called whenever organized information is requested. It will only reorganize the actions
640 * if something has changed since the last time it ran.
645 OO
.ui
.ActionSet
.prototype.organize = function () {
646 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
647 specialFlags
= this.constructor.static.specialFlags
;
649 if ( !this.organized
) {
650 this.categorized
= {};
653 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
654 action
= this.list
[ i
];
655 if ( action
.isVisible() ) {
656 // Populate categories
657 for ( category
in this.categories
) {
658 if ( !this.categorized
[ category
] ) {
659 this.categorized
[ category
] = {};
661 list
= action
[ this.categories
[ category
] ]();
662 if ( !Array
.isArray( list
) ) {
665 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
667 if ( !this.categorized
[ category
][ item
] ) {
668 this.categorized
[ category
][ item
] = [];
670 this.categorized
[ category
][ item
].push( action
);
673 // Populate special/others
675 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
676 flag
= specialFlags
[ j
];
677 if ( !this.special
[ flag
] && action
.hasFlag( flag
) ) {
678 this.special
[ flag
] = action
;
684 this.others
.push( action
);
688 this.organized
= true;
695 * Errors contain a required message (either a string or jQuery selection) that is used to describe what went wrong
696 * in a {@link OO.ui.Process process}. The error's #recoverable and #warning configurations are used to customize the
697 * appearance and functionality of the error interface.
699 * The basic error interface contains a formatted error message as well as two buttons: 'Dismiss' and 'Try again' (i.e., the error
700 * is 'recoverable' by default). If the error is not recoverable, the 'Try again' button will not be rendered and the widget
701 * that initiated the failed process will be disabled.
703 * If the error is a warning, the error interface will include a 'Dismiss' and a 'Continue' button, which will try the
706 * For an example of error interfaces, please see the [OOjs UI documentation on MediaWiki][1].
708 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Process_Dialogs#Processes_and_errors
713 * @param {string|jQuery} message Description of error
714 * @param {Object} [config] Configuration options
715 * @cfg {boolean} [recoverable=true] Error is recoverable.
716 * By default, errors are recoverable, and users can try the process again.
717 * @cfg {boolean} [warning=false] Error is a warning.
718 * If the error is a warning, the error interface will include a
719 * 'Dismiss' and a 'Continue' button. It is the responsibility of the developer to ensure that the warning
720 * is not triggered a second time if the user chooses to continue.
722 OO
.ui
.Error
= function OoUiError( message
, config
) {
723 // Allow passing positional parameters inside the config object
724 if ( OO
.isPlainObject( message
) && config
=== undefined ) {
726 message
= config
.message
;
729 // Configuration initialization
730 config
= config
|| {};
733 this.message
= message
instanceof jQuery
? message
: String( message
);
734 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
735 this.warning
= !!config
.warning
;
740 OO
.initClass( OO
.ui
.Error
);
745 * Check if the error is recoverable.
747 * If the error is recoverable, users are able to try the process again.
749 * @return {boolean} Error is recoverable
751 OO
.ui
.Error
.prototype.isRecoverable = function () {
752 return this.recoverable
;
756 * Check if the error is a warning.
758 * If the error is a warning, the error interface will include a 'Dismiss' and a 'Continue' button.
760 * @return {boolean} Error is warning
762 OO
.ui
.Error
.prototype.isWarning = function () {
767 * Get error message as DOM nodes.
769 * @return {jQuery} Error message in DOM nodes
771 OO
.ui
.Error
.prototype.getMessage = function () {
772 return this.message
instanceof jQuery
?
773 this.message
.clone() :
774 $( '<div>' ).text( this.message
).contents();
778 * Get the error message text.
780 * @return {string} Error message
782 OO
.ui
.Error
.prototype.getMessageText = function () {
783 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
787 * A Process is a list of steps that are called in sequence. The step can be a number, a jQuery promise,
790 * - **number**: the process will wait for the specified number of milliseconds before proceeding.
791 * - **promise**: the process will continue to the next step when the promise is successfully resolved
792 * or stop if the promise is rejected.
793 * - **function**: the process will execute the function. The process will stop if the function returns
794 * either a boolean `false` or a promise that is rejected; if the function returns a number, the process
795 * will wait for that number of milliseconds before proceeding.
797 * If the process fails, an {@link OO.ui.Error error} is generated. Depending on how the error is
798 * configured, users can dismiss the error and try the process again, or not. If a process is stopped,
799 * its remaining steps will not be performed.
804 * @param {number|jQuery.Promise|Function} step Number of miliseconds to wait before proceeding, promise
805 * that must be resolved before proceeding, or a function to execute. See #createStep for more information. see #createStep for more information
806 * @param {Object} [context=null] Execution context of the function. The context is ignored if the step is
807 * a number or promise.
809 OO
.ui
.Process = function ( step
, context
) {
814 if ( step
!== undefined ) {
815 this.next( step
, context
);
821 OO
.initClass( OO
.ui
.Process
);
828 * @return {jQuery.Promise} Promise that is resolved when all steps have successfully completed.
829 * If any of the steps return a promise that is rejected or a boolean false, this promise is rejected
830 * and any remaining steps are not performed.
832 OO
.ui
.Process
.prototype.execute = function () {
836 * Continue execution.
839 * @param {Array} step A function and the context it should be called in
840 * @return {Function} Function that continues the process
842 function proceed( step
) {
844 // Execute step in the correct context
846 result
= step
.callback
.call( step
.context
);
848 if ( result
=== false ) {
849 // Use rejected promise for boolean false results
850 return $.Deferred().reject( [] ).promise();
852 if ( typeof result
=== 'number' ) {
854 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
856 // Use a delayed promise for numbers, expecting them to be in milliseconds
857 deferred
= $.Deferred();
858 setTimeout( deferred
.resolve
, result
);
859 return deferred
.promise();
861 if ( result
instanceof OO
.ui
.Error
) {
862 // Use rejected promise for error
863 return $.Deferred().reject( [ result
] ).promise();
865 if ( Array
.isArray( result
) && result
.length
&& result
[ 0 ] instanceof OO
.ui
.Error
) {
866 // Use rejected promise for list of errors
867 return $.Deferred().reject( result
).promise();
869 // Duck-type the object to see if it can produce a promise
870 if ( result
&& $.isFunction( result
.promise
) ) {
871 // Use a promise generated from the result
872 return result
.promise();
874 // Use resolved promise for other results
875 return $.Deferred().resolve().promise();
879 if ( this.steps
.length
) {
880 // Generate a chain reaction of promises
881 promise
= proceed( this.steps
[ 0 ] )();
882 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
883 promise
= promise
.then( proceed( this.steps
[ i
] ) );
886 promise
= $.Deferred().resolve().promise();
893 * Create a process step.
896 * @param {number|jQuery.Promise|Function} step
898 * - Number of milliseconds to wait before proceeding
899 * - Promise that must be resolved before proceeding
900 * - Function to execute
901 * - If the function returns a boolean false the process will stop
902 * - If the function returns a promise, the process will continue to the next
903 * step when the promise is resolved or stop if the promise is rejected
904 * - If the function returns a number, the process will wait for that number of
905 * milliseconds before proceeding
906 * @param {Object} [context=null] Execution context of the function. The context is
907 * ignored if the step is a number or promise.
908 * @return {Object} Step object, with `callback` and `context` properties
910 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
911 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
913 callback: function () {
919 if ( $.isFunction( step
) ) {
925 throw new Error( 'Cannot create process step: number, promise or function expected' );
929 * Add step to the beginning of the process.
931 * @inheritdoc #createStep
932 * @return {OO.ui.Process} this
935 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
936 this.steps
.unshift( this.createStep( step
, context
) );
941 * Add step to the end of the process.
943 * @inheritdoc #createStep
944 * @return {OO.ui.Process} this
947 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
948 this.steps
.push( this.createStep( step
, context
) );
953 * Window managers are used to open and close {@link OO.ui.Window windows} and control their presentation.
954 * Managed windows are mutually exclusive. If a new window is opened while a current window is opening
955 * or is opened, the current window will be closed and any ongoing {@link OO.ui.Process process} will be cancelled. Windows
956 * themselves are persistent and—rather than being torn down when closed—can be repopulated with the
957 * pertinent data and reused.
959 * Over the lifecycle of a window, the window manager makes available three promises: `opening`,
960 * `opened`, and `closing`, which represent the primary stages of the cycle:
962 * **Opening**: the opening stage begins when the window manager’s #openWindow or a window’s
963 * {@link OO.ui.Window#open open} method is used, and the window manager begins to open the window.
965 * - an `opening` event is emitted with an `opening` promise
966 * - the #getSetupDelay method is called and the returned value is used to time a pause in execution before
967 * the window’s {@link OO.ui.Window#getSetupProcess getSetupProcess} method is called on the
968 * window and its result executed
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
971 * the window’s {@link OO.ui.Window#getReadyProcess getReadyProcess} method is called on the
972 * window and its result executed
973 * - a `ready` progress notification is emitted from the `opening` promise
974 * - the `opening` promise is resolved with an `opened` promise
976 * **Opened**: the window is now open.
978 * **Closing**: the closing stage begins when the window manager's #closeWindow or the
979 * window's {@link OO.ui.Window#close close} methods is used, and the window manager begins
980 * to close the window.
982 * - the `opened` promise is resolved with `closing` promise and a `closing` event is emitted
983 * - the #getHoldDelay method is called and the returned value is used to time a pause in execution before
984 * the window's {@link OO.ui.Window#getHoldProcess getHoldProces} method is called on the
985 * window and its result executed
986 * - a `hold` progress notification is emitted from the `closing` promise
987 * - the #getTeardownDelay() method is called and the returned value is used to time a pause in execution before
988 * the window's {@link OO.ui.Window#getTeardownProcess getTeardownProcess} method is called on the
989 * window and its result executed
990 * - a `teardown` progress notification is emitted from the `closing` promise
991 * - the `closing` promise is resolved. The window is now closed
993 * See the [OOjs UI documentation on MediaWiki][1] for more information.
995 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Window_managers
998 * @extends OO.ui.Element
999 * @mixins OO.EventEmitter
1002 * @param {Object} [config] Configuration options
1003 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
1004 * Note that window classes that are instantiated with a factory must have
1005 * a {@link OO.ui.Dialog#static-name static name} property that specifies a symbolic name.
1006 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
1008 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
1009 // Configuration initialization
1010 config
= config
|| {};
1012 // Parent constructor
1013 OO
.ui
.WindowManager
.parent
.call( this, config
);
1015 // Mixin constructors
1016 OO
.EventEmitter
.call( this );
1019 this.factory
= config
.factory
;
1020 this.modal
= config
.modal
=== undefined || !!config
.modal
;
1022 this.opening
= null;
1024 this.closing
= null;
1025 this.preparingToOpen
= null;
1026 this.preparingToClose
= null;
1027 this.currentWindow
= null;
1028 this.globalEvents
= false;
1029 this.$returnFocusTo
= null;
1030 this.$ariaHidden
= null;
1031 this.onWindowResizeTimeout
= null;
1032 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
1033 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
1037 .addClass( 'oo-ui-windowManager' )
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} opening An `opening` promise resolved with a value when the window is opened successfully.
1054 * When the `opening` promise is resolved, the first argument of the value is an 'opened' promise, the second argument
1055 * is the opening data. The `opening` promise emits `setup` and `ready` notifications when those processes are complete.
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} closing A `closing` promise is resolved with a value when the window
1065 * is closed successfully. The promise emits `hold` and `teardown` notifications when those
1066 * processes are complete. When the `closing` promise is resolved, the first argument of its value
1067 * is the closing data.
1068 * @param {Object} data Window closing data
1072 * A 'resize' event is emitted when a window is resized.
1075 * @param {OO.ui.Window} win Window that was resized
1078 /* Static Properties */
1081 * Map of the symbolic name of each window size and its CSS properties.
1085 * @property {Object}
1087 OO
.ui
.WindowManager
.static.sizes
= {
1101 // These can be non-numeric because they are never used in calculations
1108 * Symbolic name of the default window size.
1110 * The default size is used if the window's requested size is not recognized.
1114 * @property {string}
1116 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
1121 * Handle window resize events.
1124 * @param {jQuery.Event} e Window resize event
1126 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
1127 clearTimeout( this.onWindowResizeTimeout
);
1128 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
1132 * Handle window resize events.
1135 * @param {jQuery.Event} e Window resize event
1137 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
1138 if ( this.currentWindow
) {
1139 this.updateWindowSize( this.currentWindow
);
1144 * Check if window is opening.
1146 * @param {OO.ui.Window} win Window to check
1147 * @return {boolean} Window is opening
1149 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
1150 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
1154 * Check if window is closing.
1156 * @param {OO.ui.Window} win Window to check
1157 * @return {boolean} Window is closing
1159 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
1160 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
1164 * Check if window is opened.
1166 * @param {OO.ui.Window} win Window to check
1167 * @return {boolean} Window is opened
1169 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
1170 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
1174 * Check if a window is being managed.
1176 * @param {OO.ui.Window} win Window to check
1177 * @return {boolean} Window is being managed
1179 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
1182 for ( name
in this.windows
) {
1183 if ( this.windows
[ name
] === win
) {
1192 * Get the number of milliseconds to wait after opening begins before executing the ‘setup’ process.
1194 * @param {OO.ui.Window} win Window being opened
1195 * @param {Object} [data] Window opening data
1196 * @return {number} Milliseconds to wait
1198 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
1203 * Get the number of milliseconds to wait after setup has finished before executing the ‘ready’ process.
1205 * @param {OO.ui.Window} win Window being opened
1206 * @param {Object} [data] Window opening data
1207 * @return {number} Milliseconds to wait
1209 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
1214 * Get the number of milliseconds to wait after closing has begun before executing the 'hold' process.
1216 * @param {OO.ui.Window} win Window being closed
1217 * @param {Object} [data] Window closing data
1218 * @return {number} Milliseconds to wait
1220 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
1225 * Get the number of milliseconds to wait after the ‘hold’ process has finished before
1226 * executing the ‘teardown’ process.
1228 * @param {OO.ui.Window} win Window being closed
1229 * @param {Object} [data] Window closing data
1230 * @return {number} Milliseconds to wait
1232 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
1233 return this.modal
? 250 : 0;
1237 * Get a window by its symbolic name.
1239 * If the window is not yet instantiated and its symbolic name is recognized by a factory, it will be
1240 * instantiated and added to the window manager automatically. Please see the [OOjs UI documentation on MediaWiki][3]
1241 * for more information about using factories.
1242 * [3]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Window_managers
1244 * @param {string} name Symbolic name of the window
1245 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
1246 * @throws {Error} An error is thrown if the symbolic name is not recognized by the factory.
1247 * @throws {Error} An error is thrown if the named window is not recognized as a managed window.
1249 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
1250 var deferred
= $.Deferred(),
1251 win
= this.windows
[ name
];
1253 if ( !( win
instanceof OO
.ui
.Window
) ) {
1254 if ( this.factory
) {
1255 if ( !this.factory
.lookup( name
) ) {
1256 deferred
.reject( new OO
.ui
.Error(
1257 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
1260 win
= this.factory
.create( name
);
1261 this.addWindows( [ win
] );
1262 deferred
.resolve( win
);
1265 deferred
.reject( new OO
.ui
.Error(
1266 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
1270 deferred
.resolve( win
);
1273 return deferred
.promise();
1277 * Get current window.
1279 * @return {OO.ui.Window|null} Currently opening/opened/closing window
1281 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
1282 return this.currentWindow
;
1288 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
1289 * @param {Object} [data] Window opening data
1290 * @param {jQuery|null} [data.$returnFocusTo] Element to which the window will return focus when closed.
1291 * Defaults the current activeElement. If set to null, focus isn't changed on close.
1292 * @return {jQuery.Promise} An `opening` promise resolved when the window is done opening.
1293 * See {@link #event-opening 'opening' event} for more information about `opening` promises.
1296 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
1298 opening
= $.Deferred();
1301 // Argument handling
1302 if ( typeof win
=== 'string' ) {
1303 return this.getWindow( win
).then( function ( win
) {
1304 return manager
.openWindow( win
, data
);
1309 if ( !this.hasWindow( win
) ) {
1310 opening
.reject( new OO
.ui
.Error(
1311 'Cannot open window: window is not attached to manager'
1313 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
1314 opening
.reject( new OO
.ui
.Error(
1315 'Cannot open window: another window is opening or open'
1320 if ( opening
.state() !== 'rejected' ) {
1321 // If a window is currently closing, wait for it to complete
1322 this.preparingToOpen
= $.when( this.closing
);
1323 // Ensure handlers get called after preparingToOpen is set
1324 this.preparingToOpen
.done( function () {
1325 if ( manager
.modal
) {
1326 manager
.toggleGlobalEvents( true );
1327 manager
.toggleAriaIsolation( true );
1329 manager
.$returnFocusTo
= data
.$returnFocusTo
|| $( document
.activeElement
);
1330 manager
.currentWindow
= win
;
1331 manager
.opening
= opening
;
1332 manager
.preparingToOpen
= null;
1333 manager
.emit( 'opening', win
, opening
, data
);
1334 setTimeout( function () {
1335 win
.setup( data
).then( function () {
1336 manager
.updateWindowSize( win
);
1337 manager
.opening
.notify( { state
: 'setup' } );
1338 setTimeout( function () {
1339 win
.ready( data
).then( function () {
1340 manager
.opening
.notify( { state
: 'ready' } );
1341 manager
.opening
= null;
1342 manager
.opened
= $.Deferred();
1343 opening
.resolve( manager
.opened
.promise(), data
);
1345 manager
.opening
= null;
1346 manager
.opened
= $.Deferred();
1348 manager
.closeWindow( win
);
1350 }, manager
.getReadyDelay() );
1352 manager
.opening
= null;
1353 manager
.opened
= $.Deferred();
1355 manager
.closeWindow( win
);
1357 }, manager
.getSetupDelay() );
1361 return opening
.promise();
1367 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
1368 * @param {Object} [data] Window closing data
1369 * @return {jQuery.Promise} A `closing` promise resolved when the window is done closing.
1370 * See {@link #event-closing 'closing' event} for more information about closing promises.
1371 * @throws {Error} An error is thrown if the window is not managed by the window manager.
1374 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
1376 closing
= $.Deferred(),
1379 // Argument handling
1380 if ( typeof win
=== 'string' ) {
1381 win
= this.windows
[ win
];
1382 } else if ( !this.hasWindow( win
) ) {
1388 closing
.reject( new OO
.ui
.Error(
1389 'Cannot close window: window is not attached to manager'
1391 } else if ( win
!== this.currentWindow
) {
1392 closing
.reject( new OO
.ui
.Error(
1393 'Cannot close window: window already closed with different data'
1395 } else if ( this.preparingToClose
|| this.closing
) {
1396 closing
.reject( new OO
.ui
.Error(
1397 'Cannot close window: window already closing with different data'
1402 if ( closing
.state() !== 'rejected' ) {
1403 // If the window is currently opening, close it when it's done
1404 this.preparingToClose
= $.when( this.opening
);
1405 // Ensure handlers get called after preparingToClose is set
1406 this.preparingToClose
.always( function () {
1407 manager
.closing
= closing
;
1408 manager
.preparingToClose
= null;
1409 manager
.emit( 'closing', win
, closing
, data
);
1410 opened
= manager
.opened
;
1411 manager
.opened
= null;
1412 opened
.resolve( closing
.promise(), data
);
1413 setTimeout( function () {
1414 win
.hold( data
).then( function () {
1415 closing
.notify( { state
: 'hold' } );
1416 setTimeout( function () {
1417 win
.teardown( data
).then( function () {
1418 closing
.notify( { state
: 'teardown' } );
1419 if ( manager
.modal
) {
1420 manager
.toggleGlobalEvents( false );
1421 manager
.toggleAriaIsolation( false );
1423 if ( manager
.$returnFocusTo
&& manager
.$returnFocusTo
.length
) {
1424 manager
.$returnFocusTo
[ 0 ].focus();
1426 manager
.closing
= null;
1427 manager
.currentWindow
= null;
1428 closing
.resolve( data
);
1430 }, manager
.getTeardownDelay() );
1432 }, manager
.getHoldDelay() );
1436 return closing
.promise();
1440 * Add windows to the window manager.
1442 * Windows can be added by reference, symbolic name, or explicitly defined symbolic names.
1443 * See the [OOjs ui documentation on MediaWiki] [2] for examples.
1444 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Window_managers
1446 * This function can be called in two manners:
1448 * 1. `.addWindows( [ windowA, windowB, ... ] )` (where `windowA`, `windowB` are OO.ui.Window objects)
1450 * This syntax registers windows under the symbolic names defined in their `.static.name`
1451 * properties. For example, if `windowA.constructor.static.name` is `'nameA'`, calling
1452 * `.openWindow( 'nameA' )` afterwards will open the window `windowA`. This syntax requires the
1453 * static name to be set, otherwise an exception will be thrown.
1455 * This is the recommended way, as it allows for an easier switch to using a window factory.
1457 * 2. `.addWindows( { nameA: windowA, nameB: windowB, ... } )`
1459 * This syntax registers windows under the explicitly given symbolic names. In this example,
1460 * calling `.openWindow( 'nameA' )` afterwards will open the window `windowA`, regardless of what
1461 * its `.static.name` is set to. The static name is not required to be set.
1463 * This should only be used if you need to override the default symbolic names.
1467 * var windowManager = new OO.ui.WindowManager();
1468 * $( 'body' ).append( windowManager.$element );
1470 * // Add a window under the default name: see OO.ui.MessageDialog.static.name
1471 * windowManager.addWindows( [ new OO.ui.MessageDialog() ] );
1472 * // Add a window under an explicit name
1473 * windowManager.addWindows( { myMessageDialog: new OO.ui.MessageDialog() } );
1475 * // Open window by default name
1476 * windowManager.openWindow( 'message' );
1477 * // Open window by explicitly given name
1478 * windowManager.openWindow( 'myMessageDialog' );
1481 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows An array of window objects specified
1482 * by reference, symbolic name, or explicitly defined symbolic names.
1483 * @throws {Error} An error is thrown if a window is added by symbolic name, but has neither an
1484 * explicit nor a statically configured symbolic name.
1486 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
1487 var i
, len
, win
, name
, list
;
1489 if ( Array
.isArray( windows
) ) {
1490 // Convert to map of windows by looking up symbolic names from static configuration
1492 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
1493 name
= windows
[ i
].constructor.static.name
;
1495 throw new Error( 'Windows must have a `name` static property defined.' );
1497 list
[ name
] = windows
[ i
];
1499 } else if ( OO
.isPlainObject( windows
) ) {
1504 for ( name
in list
) {
1506 this.windows
[ name
] = win
.toggle( false );
1507 this.$element
.append( win
.$element
);
1508 win
.setManager( this );
1513 * Remove the specified windows from the windows manager.
1515 * Windows will be closed before they are removed. If you wish to remove all windows, you may wish to use
1516 * the #clearWindows method instead. If you no longer need the window manager and want to ensure that it no
1517 * longer listens to events, use the #destroy method.
1519 * @param {string[]} names Symbolic names of windows to remove
1520 * @return {jQuery.Promise} Promise resolved when window is closed and removed
1521 * @throws {Error} An error is thrown if the named windows are not managed by the window manager.
1523 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
1524 var i
, len
, win
, name
, cleanupWindow
,
1527 cleanup = function ( name
, win
) {
1528 delete manager
.windows
[ name
];
1529 win
.$element
.detach();
1532 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
1534 win
= this.windows
[ name
];
1536 throw new Error( 'Cannot remove window' );
1538 cleanupWindow
= cleanup
.bind( null, name
, win
);
1539 promises
.push( this.closeWindow( name
).then( cleanupWindow
, cleanupWindow
) );
1542 return $.when
.apply( $, promises
);
1546 * Remove all windows from the window manager.
1548 * Windows will be closed before they are removed. Note that the window manager, though not in use, will still
1549 * listen to events. If the window manager will not be used again, you may wish to use the #destroy method instead.
1550 * To remove just a subset of windows, use the #removeWindows method.
1552 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
1554 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
1555 return this.removeWindows( Object
.keys( this.windows
) );
1559 * Set dialog size. In general, this method should not be called directly.
1561 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
1563 * @param {OO.ui.Window} win Window to update, should be the current window
1566 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
1569 // Bypass for non-current, and thus invisible, windows
1570 if ( win
!== this.currentWindow
) {
1574 isFullscreen
= win
.getSize() === 'full';
1576 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', isFullscreen
);
1577 this.$element
.toggleClass( 'oo-ui-windowManager-floating', !isFullscreen
);
1578 win
.setDimensions( win
.getSizeProperties() );
1580 this.emit( 'resize', win
);
1586 * Bind or unbind global events for scrolling.
1589 * @param {boolean} [on] Bind global events
1592 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
1593 var scrollWidth
, bodyMargin
,
1594 $body
= $( this.getElementDocument().body
),
1595 // We could have multiple window managers open so only modify
1596 // the body css at the bottom of the stack
1597 stackDepth
= $body
.data( 'windowManagerGlobalEvents' ) || 0;
1599 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
1602 if ( !this.globalEvents
) {
1603 $( this.getElementWindow() ).on( {
1604 // Start listening for top-level window dimension changes
1605 'orientationchange resize': this.onWindowResizeHandler
1607 if ( stackDepth
=== 0 ) {
1608 scrollWidth
= window
.innerWidth
- document
.documentElement
.clientWidth
;
1609 bodyMargin
= parseFloat( $body
.css( 'margin-right' ) ) || 0;
1612 'margin-right': bodyMargin
+ scrollWidth
1616 this.globalEvents
= true;
1618 } else if ( this.globalEvents
) {
1619 $( this.getElementWindow() ).off( {
1620 // Stop listening for top-level window dimension changes
1621 'orientationchange resize': this.onWindowResizeHandler
1624 if ( stackDepth
=== 0 ) {
1630 this.globalEvents
= false;
1632 $body
.data( 'windowManagerGlobalEvents', stackDepth
);
1638 * Toggle screen reader visibility of content other than the window manager.
1641 * @param {boolean} [isolate] Make only the window manager visible to screen readers
1644 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
1645 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
1648 if ( !this.$ariaHidden
) {
1649 // Hide everything other than the window manager from screen readers
1650 this.$ariaHidden
= $( 'body' )
1652 .not( this.$element
.parentsUntil( 'body' ).last() )
1653 .attr( 'aria-hidden', '' );
1655 } else if ( this.$ariaHidden
) {
1656 // Restore screen reader visibility
1657 this.$ariaHidden
.removeAttr( 'aria-hidden' );
1658 this.$ariaHidden
= null;
1665 * Destroy the window manager.
1667 * Destroying the window manager ensures that it will no longer listen to events. If you would like to
1668 * continue using the window manager, but wish to remove all windows from it, use the #clearWindows method
1671 OO
.ui
.WindowManager
.prototype.destroy = function () {
1672 this.toggleGlobalEvents( false );
1673 this.toggleAriaIsolation( false );
1674 this.clearWindows();
1675 this.$element
.remove();
1679 * A window is a container for elements that are in a child frame. They are used with
1680 * a window manager (OO.ui.WindowManager), which is used to open and close the window and control
1681 * its presentation. The size of a window is specified using a symbolic name (e.g., ‘small’, ‘medium’,
1682 * ‘large’), which is interpreted by the window manager. If the requested size is not recognized,
1683 * the window manager will choose a sensible fallback.
1685 * The lifecycle of a window has three primary stages (opening, opened, and closing) in which
1686 * different processes are executed:
1688 * **opening**: The opening stage begins when the window manager's {@link OO.ui.WindowManager#openWindow
1689 * openWindow} or the window's {@link #open open} methods are used, and the window manager begins to open
1692 * - {@link #getSetupProcess} method is called and its result executed
1693 * - {@link #getReadyProcess} method is called and its result executed
1695 * **opened**: The window is now open
1697 * **closing**: The closing stage begins when the window manager's
1698 * {@link OO.ui.WindowManager#closeWindow closeWindow}
1699 * or the window's {@link #close} methods are used, and the window manager begins to close the window.
1701 * - {@link #getHoldProcess} method is called and its result executed
1702 * - {@link #getTeardownProcess} method is called and its result executed. The window is now closed
1704 * Each of the window's processes (setup, ready, hold, and teardown) can be extended in subclasses
1705 * by overriding the window's #getSetupProcess, #getReadyProcess, #getHoldProcess and #getTeardownProcess
1706 * methods. Note that each {@link OO.ui.Process process} is executed in series, so asynchronous
1707 * processing can complete. Always assume window processes are executed asynchronously.
1709 * For more information, please see the [OOjs UI documentation on MediaWiki] [1].
1711 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Windows
1715 * @extends OO.ui.Element
1716 * @mixins OO.EventEmitter
1719 * @param {Object} [config] Configuration options
1720 * @cfg {string} [size] Symbolic name of the dialog size: `small`, `medium`, `large`, `larger` or
1721 * `full`. If omitted, the value of the {@link #static-size static size} property will be used.
1723 OO
.ui
.Window
= function OoUiWindow( config
) {
1724 // Configuration initialization
1725 config
= config
|| {};
1727 // Parent constructor
1728 OO
.ui
.Window
.parent
.call( this, config
);
1730 // Mixin constructors
1731 OO
.EventEmitter
.call( this );
1734 this.manager
= null;
1735 this.size
= config
.size
|| this.constructor.static.size
;
1736 this.$frame
= $( '<div>' );
1737 this.$overlay
= $( '<div>' );
1738 this.$content
= $( '<div>' );
1740 this.$focusTrapBefore
= $( '<div>' ).prop( 'tabIndex', 0 );
1741 this.$focusTrapAfter
= $( '<div>' ).prop( 'tabIndex', 0 );
1742 this.$focusTraps
= this.$focusTrapBefore
.add( this.$focusTrapAfter
);
1745 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1747 .addClass( 'oo-ui-window-content' )
1748 .attr( 'tabindex', 0 );
1750 .addClass( 'oo-ui-window-frame' )
1751 .append( this.$focusTrapBefore
, this.$content
, this.$focusTrapAfter
);
1754 .addClass( 'oo-ui-window' )
1755 .append( this.$frame
, this.$overlay
);
1757 // Initially hidden - using #toggle may cause errors if subclasses override toggle with methods
1758 // that reference properties not initialized at that time of parent class construction
1759 // TODO: Find a better way to handle post-constructor setup
1760 this.visible
= false;
1761 this.$element
.addClass( 'oo-ui-element-hidden' );
1766 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1767 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1769 /* Static Properties */
1772 * Symbolic name of the window size: `small`, `medium`, `large`, `larger` or `full`.
1774 * The static size is used if no #size is configured during construction.
1778 * @property {string}
1780 OO
.ui
.Window
.static.size
= 'medium';
1785 * Handle mouse down events.
1788 * @param {jQuery.Event} e Mouse down event
1790 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1791 // Prevent clicking on the click-block from stealing focus
1792 if ( e
.target
=== this.$element
[ 0 ] ) {
1798 * Check if the window has been initialized.
1800 * Initialization occurs when a window is added to a manager.
1802 * @return {boolean} Window has been initialized
1804 OO
.ui
.Window
.prototype.isInitialized = function () {
1805 return !!this.manager
;
1809 * Check if the window is visible.
1811 * @return {boolean} Window is visible
1813 OO
.ui
.Window
.prototype.isVisible = function () {
1814 return this.visible
;
1818 * Check if the window is opening.
1820 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isOpening isOpening}
1823 * @return {boolean} Window is opening
1825 OO
.ui
.Window
.prototype.isOpening = function () {
1826 return this.manager
.isOpening( this );
1830 * Check if the window is closing.
1832 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isClosing isClosing} method.
1834 * @return {boolean} Window is closing
1836 OO
.ui
.Window
.prototype.isClosing = function () {
1837 return this.manager
.isClosing( this );
1841 * Check if the window is opened.
1843 * This method is a wrapper around the window manager's {@link OO.ui.WindowManager#isOpened isOpened} method.
1845 * @return {boolean} Window is opened
1847 OO
.ui
.Window
.prototype.isOpened = function () {
1848 return this.manager
.isOpened( this );
1852 * Get the window manager.
1854 * All windows must be attached to a window manager, which is used to open
1855 * and close the window and control its presentation.
1857 * @return {OO.ui.WindowManager} Manager of window
1859 OO
.ui
.Window
.prototype.getManager = function () {
1860 return this.manager
;
1864 * Get the symbolic name of the window size (e.g., `small` or `medium`).
1866 * @return {string} Symbolic name of the size: `small`, `medium`, `large`, `larger`, `full`
1868 OO
.ui
.Window
.prototype.getSize = function () {
1869 var viewport
= OO
.ui
.Element
.static.getDimensions( this.getElementWindow() ),
1870 sizes
= this.manager
.constructor.static.sizes
,
1873 if ( !sizes
[ size
] ) {
1874 size
= this.manager
.constructor.static.defaultSize
;
1876 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[ size
].width
) {
1884 * Get the size properties associated with the current window size
1886 * @return {Object} Size properties
1888 OO
.ui
.Window
.prototype.getSizeProperties = function () {
1889 return this.manager
.constructor.static.sizes
[ this.getSize() ];
1893 * Disable transitions on window's frame for the duration of the callback function, then enable them
1897 * @param {Function} callback Function to call while transitions are disabled
1899 OO
.ui
.Window
.prototype.withoutSizeTransitions = function ( callback
) {
1900 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1901 // Disable transitions first, otherwise we'll get values from when the window was animating.
1902 // We need to build the transition CSS properties using these specific properties since
1903 // Firefox doesn't return anything useful when asked just for 'transition'.
1904 var oldTransition
= this.$frame
.css( 'transition-property' ) + ' ' +
1905 this.$frame
.css( 'transition-duration' ) + ' ' +
1906 this.$frame
.css( 'transition-timing-function' ) + ' ' +
1907 this.$frame
.css( 'transition-delay' );
1909 this.$frame
.css( 'transition', 'none' );
1912 // Force reflow to make sure the style changes done inside callback
1913 // really are not transitioned
1914 this.$frame
.height();
1915 this.$frame
.css( 'transition', oldTransition
);
1919 * Get the height of the full window contents (i.e., the window head, body and foot together).
1921 * What consistitutes the head, body, and foot varies depending on the window type.
1922 * A {@link OO.ui.MessageDialog message dialog} displays a title and message in its body,
1923 * and any actions in the foot. A {@link OO.ui.ProcessDialog process dialog} displays a title
1924 * and special actions in the head, and dialog content in the body.
1926 * To get just the height of the dialog body, use the #getBodyHeight method.
1928 * @return {number} The height of the window contents (the dialog head, body and foot) in pixels
1930 OO
.ui
.Window
.prototype.getContentHeight = function () {
1933 bodyStyleObj
= this.$body
[ 0 ].style
,
1934 frameStyleObj
= this.$frame
[ 0 ].style
;
1936 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1937 // Disable transitions first, otherwise we'll get values from when the window was animating.
1938 this.withoutSizeTransitions( function () {
1939 var oldHeight
= frameStyleObj
.height
,
1940 oldPosition
= bodyStyleObj
.position
;
1941 frameStyleObj
.height
= '1px';
1942 // Force body to resize to new width
1943 bodyStyleObj
.position
= 'relative';
1944 bodyHeight
= win
.getBodyHeight();
1945 frameStyleObj
.height
= oldHeight
;
1946 bodyStyleObj
.position
= oldPosition
;
1950 // Add buffer for border
1951 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1952 // Use combined heights of children
1953 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1958 * Get the height of the window body.
1960 * To get the height of the full window contents (the window body, head, and foot together),
1961 * use #getContentHeight.
1963 * When this function is called, the window will temporarily have been resized
1964 * to height=1px, so .scrollHeight measurements can be taken accurately.
1966 * @return {number} Height of the window body in pixels
1968 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1969 return this.$body
[ 0 ].scrollHeight
;
1973 * Get the directionality of the frame (right-to-left or left-to-right).
1975 * @return {string} Directionality: `'ltr'` or `'rtl'`
1977 OO
.ui
.Window
.prototype.getDir = function () {
1978 return OO
.ui
.Element
.static.getDir( this.$content
) || 'ltr';
1982 * Get the 'setup' process.
1984 * The setup process is used to set up a window for use in a particular context,
1985 * based on the `data` argument. This method is called during the opening phase of the window’s
1988 * Override this method to add additional steps to the ‘setup’ process the parent method provides
1989 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
1992 * To add window content that persists between openings, you may wish to use the #initialize method
1995 * @param {Object} [data] Window opening data
1996 * @return {OO.ui.Process} Setup process
1998 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1999 return new OO
.ui
.Process();
2003 * Get the ‘ready’ process.
2005 * The ready process is used to ready a window for use in a particular
2006 * context, based on the `data` argument. This method is called during the opening phase of
2007 * the window’s lifecycle, after the window has been {@link #getSetupProcess setup}.
2009 * Override this method to add additional steps to the ‘ready’ process the parent method
2010 * provides using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next}
2011 * methods of OO.ui.Process.
2013 * @param {Object} [data] Window opening data
2014 * @return {OO.ui.Process} Ready process
2016 OO
.ui
.Window
.prototype.getReadyProcess = function () {
2017 return new OO
.ui
.Process();
2021 * Get the 'hold' process.
2023 * The hold process is used to keep a window from being used in a particular context,
2024 * based on the `data` argument. This method is called during the closing phase of the window’s
2027 * Override this method to add additional steps to the 'hold' process the parent method provides
2028 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2031 * @param {Object} [data] Window closing data
2032 * @return {OO.ui.Process} Hold process
2034 OO
.ui
.Window
.prototype.getHoldProcess = function () {
2035 return new OO
.ui
.Process();
2039 * Get the ‘teardown’ process.
2041 * The teardown process is used to teardown a window after use. During teardown,
2042 * user interactions within the window are conveyed and the window is closed, based on the `data`
2043 * argument. This method is called during the closing phase of the window’s lifecycle.
2045 * Override this method to add additional steps to the ‘teardown’ process the parent method provides
2046 * using the {@link OO.ui.Process#first first} and {@link OO.ui.Process#next next} methods
2049 * @param {Object} [data] Window closing data
2050 * @return {OO.ui.Process} Teardown process
2052 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
2053 return new OO
.ui
.Process();
2057 * Set the window manager.
2059 * This will cause the window to initialize. Calling it more than once will cause an error.
2061 * @param {OO.ui.WindowManager} manager Manager for this window
2062 * @throws {Error} An error is thrown if the method is called more than once
2065 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
2066 if ( this.manager
) {
2067 throw new Error( 'Cannot set window manager, window already has a manager' );
2070 this.manager
= manager
;
2077 * Set the window size by symbolic name (e.g., 'small' or 'medium')
2079 * @param {string} size Symbolic name of size: `small`, `medium`, `large`, `larger` or
2083 OO
.ui
.Window
.prototype.setSize = function ( size
) {
2090 * Update the window size.
2092 * @throws {Error} An error is thrown if the window is not attached to a window manager
2095 OO
.ui
.Window
.prototype.updateSize = function () {
2096 if ( !this.manager
) {
2097 throw new Error( 'Cannot update window size, must be attached to a manager' );
2100 this.manager
.updateWindowSize( this );
2106 * Set window dimensions. This method is called by the {@link OO.ui.WindowManager window manager}
2107 * when the window is opening. In general, setDimensions should not be called directly.
2109 * To set the size of the window, use the #setSize method.
2111 * @param {Object} dim CSS dimension properties
2112 * @param {string|number} [dim.width] Width
2113 * @param {string|number} [dim.minWidth] Minimum width
2114 * @param {string|number} [dim.maxWidth] Maximum width
2115 * @param {string|number} [dim.height] Height, omit to set based on height of contents
2116 * @param {string|number} [dim.minHeight] Minimum height
2117 * @param {string|number} [dim.maxHeight] Maximum height
2120 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
2123 styleObj
= this.$frame
[ 0 ].style
;
2125 // Calculate the height we need to set using the correct width
2126 if ( dim
.height
=== undefined ) {
2127 this.withoutSizeTransitions( function () {
2128 var oldWidth
= styleObj
.width
;
2129 win
.$frame
.css( 'width', dim
.width
|| '' );
2130 height
= win
.getContentHeight();
2131 styleObj
.width
= oldWidth
;
2134 height
= dim
.height
;
2138 width
: dim
.width
|| '',
2139 minWidth
: dim
.minWidth
|| '',
2140 maxWidth
: dim
.maxWidth
|| '',
2141 height
: height
|| '',
2142 minHeight
: dim
.minHeight
|| '',
2143 maxHeight
: dim
.maxHeight
|| ''
2150 * Initialize window contents.
2152 * Before the window is opened for the first time, #initialize is called so that content that
2153 * persists between openings can be added to the window.
2155 * To set up a window with new content each time the window opens, use #getSetupProcess.
2157 * @throws {Error} An error is thrown if the window is not attached to a window manager
2160 OO
.ui
.Window
.prototype.initialize = function () {
2161 if ( !this.manager
) {
2162 throw new Error( 'Cannot initialize window, must be attached to a manager' );
2166 this.$head
= $( '<div>' );
2167 this.$body
= $( '<div>' );
2168 this.$foot
= $( '<div>' );
2169 this.$document
= $( this.getElementDocument() );
2172 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
2175 this.$head
.addClass( 'oo-ui-window-head' );
2176 this.$body
.addClass( 'oo-ui-window-body' );
2177 this.$foot
.addClass( 'oo-ui-window-foot' );
2178 this.$content
.append( this.$head
, this.$body
, this.$foot
);
2184 * Called when someone tries to focus the hidden element at the end of the dialog.
2185 * Sends focus back to the start of the dialog.
2187 * @param {jQuery.Event} event Focus event
2189 OO
.ui
.Window
.prototype.onFocusTrapFocused = function ( event
) {
2190 var backwards
= this.$focusTrapBefore
.is( event
.target
),
2191 element
= OO
.ui
.findFocusable( this.$content
, backwards
);
2193 // There's a focusable element inside the content, at the front or
2194 // back depending on which focus trap we hit; select it.
2197 // There's nothing focusable inside the content. As a fallback,
2198 // this.$content is focusable, and focusing it will keep our focus
2199 // properly trapped. It's not a *meaningful* focus, since it's just
2200 // the content-div for the Window, but it's better than letting focus
2201 // escape into the page.
2202 this.$content
.focus();
2209 * This method is a wrapper around a call to the window manager’s {@link OO.ui.WindowManager#openWindow openWindow}
2210 * method, which returns a promise resolved when the window is done opening.
2212 * To customize the window each time it opens, use #getSetupProcess or #getReadyProcess.
2214 * @param {Object} [data] Window opening data
2215 * @return {jQuery.Promise} Promise resolved with a value when the window is opened, or rejected
2216 * if the window fails to open. When the promise is resolved successfully, the first argument of the
2217 * value is a new promise, which is resolved when the window begins closing.
2218 * @throws {Error} An error is thrown if the window is not attached to a window manager
2220 OO
.ui
.Window
.prototype.open = function ( data
) {
2221 if ( !this.manager
) {
2222 throw new Error( 'Cannot open window, must be attached to a manager' );
2225 return this.manager
.openWindow( this, data
);
2231 * This method is a wrapper around a call to the window
2232 * manager’s {@link OO.ui.WindowManager#closeWindow closeWindow} method,
2233 * which returns a closing promise resolved when the window is done closing.
2235 * The window's #getHoldProcess and #getTeardownProcess methods are called during the closing
2236 * phase of the window’s lifecycle and can be used to specify closing behavior each time
2237 * the window closes.
2239 * @param {Object} [data] Window closing data
2240 * @return {jQuery.Promise} Promise resolved when window is closed
2241 * @throws {Error} An error is thrown if the window is not attached to a window manager
2243 OO
.ui
.Window
.prototype.close = function ( data
) {
2244 if ( !this.manager
) {
2245 throw new Error( 'Cannot close window, must be attached to a manager' );
2248 return this.manager
.closeWindow( this, data
);
2254 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2257 * @param {Object} [data] Window opening data
2258 * @return {jQuery.Promise} Promise resolved when window is setup
2260 OO
.ui
.Window
.prototype.setup = function ( data
) {
2263 this.toggle( true );
2265 this.focusTrapHandler
= OO
.ui
.bind( this.onFocusTrapFocused
, this );
2266 this.$focusTraps
.on( 'focus', this.focusTrapHandler
);
2268 return this.getSetupProcess( data
).execute().then( function () {
2269 // Force redraw by asking the browser to measure the elements' widths
2270 win
.$element
.addClass( 'oo-ui-window-active oo-ui-window-setup' ).width();
2271 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2278 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2281 * @param {Object} [data] Window opening data
2282 * @return {jQuery.Promise} Promise resolved when window is ready
2284 OO
.ui
.Window
.prototype.ready = function ( data
) {
2287 this.$content
.focus();
2288 return this.getReadyProcess( data
).execute().then( function () {
2289 // Force redraw by asking the browser to measure the elements' widths
2290 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2291 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2298 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2301 * @param {Object} [data] Window closing data
2302 * @return {jQuery.Promise} Promise resolved when window is held
2304 OO
.ui
.Window
.prototype.hold = function ( data
) {
2307 return this.getHoldProcess( data
).execute().then( function () {
2308 // Get the focused element within the window's content
2309 var $focus
= win
.$content
.find( OO
.ui
.Element
.static.getDocument( win
.$content
).activeElement
);
2311 // Blur the focused element
2312 if ( $focus
.length
) {
2316 // Force redraw by asking the browser to measure the elements' widths
2317 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2318 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2325 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2328 * @param {Object} [data] Window closing data
2329 * @return {jQuery.Promise} Promise resolved when window is torn down
2331 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2334 return this.getTeardownProcess( data
).execute().then( function () {
2335 // Force redraw by asking the browser to measure the elements' widths
2336 win
.$element
.removeClass( 'oo-ui-window-active oo-ui-window-setup' ).width();
2337 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2338 win
.$focusTraps
.off( 'focus', win
.focusTrapHandler
);
2339 win
.toggle( false );
2344 * The Dialog class serves as the base class for the other types of dialogs.
2345 * Unless extended to include controls, the rendered dialog box is a simple window
2346 * that users can close by hitting the ‘Esc’ key. Dialog windows are used with OO.ui.WindowManager,
2347 * which opens, closes, and controls the presentation of the window. See the
2348 * [OOjs UI documentation on MediaWiki] [1] for more information.
2351 * // A simple dialog window.
2352 * function MyDialog( config ) {
2353 * MyDialog.parent.call( this, config );
2355 * OO.inheritClass( MyDialog, OO.ui.Dialog );
2356 * MyDialog.static.name = 'myDialog';
2357 * MyDialog.prototype.initialize = function () {
2358 * MyDialog.parent.prototype.initialize.call( this );
2359 * this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
2360 * this.content.$element.append( '<p>A simple dialog window. Press \'Esc\' to close.</p>' );
2361 * this.$body.append( this.content.$element );
2363 * MyDialog.prototype.getBodyHeight = function () {
2364 * return this.content.$element.outerHeight( true );
2366 * var myDialog = new MyDialog( {
2369 * // Create and append a window manager, which opens and closes the window.
2370 * var windowManager = new OO.ui.WindowManager();
2371 * $( 'body' ).append( windowManager.$element );
2372 * windowManager.addWindows( [ myDialog ] );
2373 * // Open the window!
2374 * windowManager.openWindow( myDialog );
2376 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Dialogs
2380 * @extends OO.ui.Window
2381 * @mixins OO.ui.mixin.PendingElement
2384 * @param {Object} [config] Configuration options
2386 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2387 // Parent constructor
2388 OO
.ui
.Dialog
.parent
.call( this, config
);
2390 // Mixin constructors
2391 OO
.ui
.mixin
.PendingElement
.call( this );
2394 this.actions
= new OO
.ui
.ActionSet();
2395 this.attachedActions
= [];
2396 this.currentAction
= null;
2397 this.onDialogKeyDownHandler
= this.onDialogKeyDown
.bind( this );
2400 this.actions
.connect( this, {
2401 click
: 'onActionClick',
2402 resize
: 'onActionResize',
2403 change
: 'onActionsChange'
2408 .addClass( 'oo-ui-dialog' )
2409 .attr( 'role', 'dialog' );
2414 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2415 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.mixin
.PendingElement
);
2417 /* Static Properties */
2420 * Symbolic name of dialog.
2422 * The dialog class must have a symbolic name in order to be registered with OO.Factory.
2423 * Please see the [OOjs UI documentation on MediaWiki] [3] for more information.
2425 * [3]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Window_managers
2430 * @property {string}
2432 OO
.ui
.Dialog
.static.name
= '';
2437 * The title can be specified as a plaintext string, a {@link OO.ui.mixin.LabelElement Label} node, or a function
2438 * that will produce a Label node or string. The title can also be specified with data passed to the
2439 * constructor (see #getSetupProcess). In this case, the static value will be overridden.
2444 * @property {jQuery|string|Function}
2446 OO
.ui
.Dialog
.static.title
= '';
2449 * An array of configured {@link OO.ui.ActionWidget action widgets}.
2451 * Actions can also be specified with data passed to the constructor (see #getSetupProcess). In this case, the static
2452 * value will be overridden.
2454 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Process_Dialogs#Action_sets
2458 * @property {Object[]}
2460 OO
.ui
.Dialog
.static.actions
= [];
2463 * Close the dialog when the 'Esc' key is pressed.
2468 * @property {boolean}
2470 OO
.ui
.Dialog
.static.escapable
= true;
2475 * Handle frame document key down events.
2478 * @param {jQuery.Event} e Key down event
2480 OO
.ui
.Dialog
.prototype.onDialogKeyDown = function ( e
) {
2482 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
&& this.constructor.static.escapable
) {
2483 this.executeAction( '' );
2485 e
.stopPropagation();
2486 } else if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& ( e
.ctrlKey
|| e
.metaKey
) ) {
2487 actions
= this.actions
.get( { flags
: 'primary', visible
: true, disabled
: false } );
2488 if ( actions
.length
> 0 ) {
2489 this.executeAction( actions
[ 0 ].getAction() );
2491 e
.stopPropagation();
2497 * Handle action resized events.
2500 * @param {OO.ui.ActionWidget} action Action that was resized
2502 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2503 // Override in subclass
2507 * Handle action click events.
2510 * @param {OO.ui.ActionWidget} action Action that was clicked
2512 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2513 if ( !this.isPending() ) {
2514 this.executeAction( action
.getAction() );
2519 * Handle actions change event.
2523 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2524 this.detachActions();
2525 if ( !this.isClosing() ) {
2526 this.attachActions();
2531 * Get the set of actions used by the dialog.
2533 * @return {OO.ui.ActionSet}
2535 OO
.ui
.Dialog
.prototype.getActions = function () {
2536 return this.actions
;
2540 * Get a process for taking action.
2542 * When you override this method, you can create a new OO.ui.Process and return it, or add additional
2543 * accept steps to the process the parent method provides using the {@link OO.ui.Process#first 'first'}
2544 * and {@link OO.ui.Process#next 'next'} methods of OO.ui.Process.
2546 * @param {string} [action] Symbolic name of action
2547 * @return {OO.ui.Process} Action process
2549 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2550 return new OO
.ui
.Process()
2551 .next( function () {
2553 // An empty action always closes the dialog without data, which should always be
2554 // safe and make no changes
2563 * @param {Object} [data] Dialog opening data
2564 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use
2565 * the {@link #static-title static title}
2566 * @param {Object[]} [data.actions] List of configuration options for each
2567 * {@link OO.ui.ActionWidget action widget}, omit to use {@link #static-actions static actions}.
2569 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2573 return OO
.ui
.Dialog
.parent
.prototype.getSetupProcess
.call( this, data
)
2574 .next( function () {
2575 var config
= this.constructor.static,
2576 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
,
2577 title
= data
.title
!== undefined ? data
.title
: config
.title
;
2579 this.title
.setLabel( title
).setTitle( title
);
2580 this.actions
.add( this.getActionWidgets( actions
) );
2582 this.$element
.on( 'keydown', this.onDialogKeyDownHandler
);
2589 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2591 return OO
.ui
.Dialog
.parent
.prototype.getTeardownProcess
.call( this, data
)
2592 .first( function () {
2593 this.$element
.off( 'keydown', this.onDialogKeyDownHandler
);
2595 this.actions
.clear();
2596 this.currentAction
= null;
2603 OO
.ui
.Dialog
.prototype.initialize = function () {
2607 OO
.ui
.Dialog
.parent
.prototype.initialize
.call( this );
2609 titleId
= OO
.ui
.generateElementId();
2612 this.title
= new OO
.ui
.LabelWidget( {
2617 this.$content
.addClass( 'oo-ui-dialog-content' );
2618 this.$element
.attr( 'aria-labelledby', titleId
);
2619 this.setPendingElement( this.$head
);
2623 * Get action widgets from a list of configs
2625 * @param {Object[]} actions Action widget configs
2626 * @return {OO.ui.ActionWidget[]} Action widgets
2628 OO
.ui
.Dialog
.prototype.getActionWidgets = function ( actions
) {
2629 var i
, len
, widgets
= [];
2630 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2632 new OO
.ui
.ActionWidget( actions
[ i
] )
2639 * Attach action actions.
2643 OO
.ui
.Dialog
.prototype.attachActions = function () {
2644 // Remember the list of potentially attached actions
2645 this.attachedActions
= this.actions
.get();
2649 * Detach action actions.
2654 OO
.ui
.Dialog
.prototype.detachActions = function () {
2657 // Detach all actions that may have been previously attached
2658 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2659 this.attachedActions
[ i
].$element
.detach();
2661 this.attachedActions
= [];
2665 * Execute an action.
2667 * @param {string} action Symbolic name of action to execute
2668 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2670 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2672 this.currentAction
= action
;
2673 return this.getActionProcess( action
).execute()
2674 .always( this.popPending
.bind( this ) );
2678 * MessageDialogs display a confirmation or alert message. By default, the rendered dialog box
2679 * consists of a header that contains the dialog title, a body with the message, and a footer that
2680 * contains any {@link OO.ui.ActionWidget action widgets}. The MessageDialog class is the only type
2681 * of {@link OO.ui.Dialog dialog} that is usually instantiated directly.
2683 * There are two basic types of message dialogs, confirmation and alert:
2685 * - **confirmation**: the dialog title describes what a progressive action will do and the message provides
2686 * more details about the consequences.
2687 * - **alert**: the dialog title describes which event occurred and the message provides more information
2688 * about why the event occurred.
2690 * The MessageDialog class specifies two actions: ‘accept’, the primary
2691 * action (e.g., ‘ok’) and ‘reject,’ the safe action (e.g., ‘cancel’). Both will close the window,
2692 * passing along the selected action.
2694 * For more information and examples, please see the [OOjs UI documentation on MediaWiki][1].
2697 * // Example: Creating and opening a message dialog window.
2698 * var messageDialog = new OO.ui.MessageDialog();
2700 * // Create and append a window manager.
2701 * var windowManager = new OO.ui.WindowManager();
2702 * $( 'body' ).append( windowManager.$element );
2703 * windowManager.addWindows( [ messageDialog ] );
2704 * // Open the window.
2705 * windowManager.openWindow( messageDialog, {
2706 * title: 'Basic message dialog',
2707 * message: 'This is the message'
2710 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Message_Dialogs
2713 * @extends OO.ui.Dialog
2716 * @param {Object} [config] Configuration options
2718 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
2719 // Parent constructor
2720 OO
.ui
.MessageDialog
.parent
.call( this, config
);
2723 this.verticalActionLayout
= null;
2726 this.$element
.addClass( 'oo-ui-messageDialog' );
2731 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
2733 /* Static Properties */
2739 OO
.ui
.MessageDialog
.static.name
= 'message';
2745 OO
.ui
.MessageDialog
.static.size
= 'small';
2749 * @deprecated since v0.18.4 as default; TODO: Remove
2751 OO
.ui
.MessageDialog
.static.verbose
= true;
2756 * The title of a confirmation dialog describes what a progressive action will do. The
2757 * title of an alert dialog describes which event occurred.
2761 * @property {jQuery|string|Function|null}
2763 OO
.ui
.MessageDialog
.static.title
= null;
2766 * The message displayed in the dialog body.
2768 * A confirmation message describes the consequences of a progressive action. An alert
2769 * message describes why an event occurred.
2773 * @property {jQuery|string|Function|null}
2775 OO
.ui
.MessageDialog
.static.message
= null;
2781 OO
.ui
.MessageDialog
.static.actions
= [
2782 // Note that OO.ui.alert() and OO.ui.confirm() rely on these.
2783 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
2784 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
2792 OO
.ui
.MessageDialog
.prototype.setManager = function ( manager
) {
2793 OO
.ui
.MessageDialog
.parent
.prototype.setManager
.call( this, manager
);
2796 this.manager
.connect( this, {
2806 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
2808 return OO
.ui
.MessageDialog
.parent
.prototype.onActionResize
.call( this, action
);
2812 * Handle window resized events.
2816 OO
.ui
.MessageDialog
.prototype.onResize = function () {
2818 dialog
.fitActions();
2819 // Wait for CSS transition to finish and do it again :(
2820 setTimeout( function () {
2821 dialog
.fitActions();
2826 * Toggle action layout between vertical and horizontal.
2829 * @param {boolean} [value] Layout actions vertically, omit to toggle
2832 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
2833 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
2835 if ( value
!== this.verticalActionLayout
) {
2836 this.verticalActionLayout
= value
;
2838 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
2839 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
2848 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
2850 return new OO
.ui
.Process( function () {
2851 this.close( { action
: action
} );
2854 return OO
.ui
.MessageDialog
.parent
.prototype.getActionProcess
.call( this, action
);
2860 * @param {Object} [data] Dialog opening data
2861 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
2862 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
2863 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
2866 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
2870 return OO
.ui
.MessageDialog
.parent
.prototype.getSetupProcess
.call( this, data
)
2871 .next( function () {
2872 this.title
.setLabel(
2873 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2875 this.message
.setLabel(
2876 data
.message
!== undefined ? data
.message
: this.constructor.static.message
2878 // @deprecated since v0.18.4 as default; TODO: Remove and make default instead.
2879 this.message
.$element
.toggleClass(
2880 'oo-ui-messageDialog-message-verbose',
2881 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
2889 OO
.ui
.MessageDialog
.prototype.getReadyProcess = function ( data
) {
2893 return OO
.ui
.MessageDialog
.parent
.prototype.getReadyProcess
.call( this, data
)
2894 .next( function () {
2895 // Focus the primary action button
2896 var actions
= this.actions
.get();
2897 actions
= actions
.filter( function ( action
) {
2898 return action
.getFlags().indexOf( 'primary' ) > -1;
2900 if ( actions
.length
> 0 ) {
2901 actions
[ 0 ].$button
.focus();
2909 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
2910 var bodyHeight
, oldOverflow
,
2911 $scrollable
= this.container
.$element
;
2913 oldOverflow
= $scrollable
[ 0 ].style
.overflow
;
2914 $scrollable
[ 0 ].style
.overflow
= 'hidden';
2916 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
2918 bodyHeight
= this.text
.$element
.outerHeight( true );
2919 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
2927 OO
.ui
.MessageDialog
.prototype.setDimensions = function ( dim
) {
2928 var $scrollable
= this.container
.$element
;
2929 OO
.ui
.MessageDialog
.parent
.prototype.setDimensions
.call( this, dim
);
2931 // Twiddle the overflow property, otherwise an unnecessary scrollbar will be produced.
2932 // Need to do it after transition completes (250ms), add 50ms just in case.
2933 setTimeout( function () {
2934 var oldOverflow
= $scrollable
[ 0 ].style
.overflow
,
2935 activeElement
= document
.activeElement
;
2937 $scrollable
[ 0 ].style
.overflow
= 'hidden';
2939 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
2941 // Check reconsiderScrollbars didn't destroy our focus, as we
2942 // are doing this after the ready process.
2943 if ( activeElement
&& activeElement
!== document
.activeElement
&& activeElement
.focus
) {
2944 activeElement
.focus();
2947 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
2956 OO
.ui
.MessageDialog
.prototype.initialize = function () {
2958 OO
.ui
.MessageDialog
.parent
.prototype.initialize
.call( this );
2961 this.$actions
= $( '<div>' );
2962 this.container
= new OO
.ui
.PanelLayout( {
2963 scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
2965 this.text
= new OO
.ui
.PanelLayout( {
2966 padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
2968 this.message
= new OO
.ui
.LabelWidget( {
2969 classes
: [ 'oo-ui-messageDialog-message' ]
2973 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
2974 this.$content
.addClass( 'oo-ui-messageDialog-content' );
2975 this.container
.$element
.append( this.text
.$element
);
2976 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
2977 this.$body
.append( this.container
.$element
);
2978 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
2979 this.$foot
.append( this.$actions
);
2985 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
2986 var i
, len
, other
, special
, others
;
2989 OO
.ui
.MessageDialog
.parent
.prototype.attachActions
.call( this );
2991 special
= this.actions
.getSpecial();
2992 others
= this.actions
.getOthers();
2994 if ( special
.safe
) {
2995 this.$actions
.append( special
.safe
.$element
);
2996 special
.safe
.toggleFramed( false );
2998 if ( others
.length
) {
2999 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
3000 other
= others
[ i
];
3001 this.$actions
.append( other
.$element
);
3002 other
.toggleFramed( false );
3005 if ( special
.primary
) {
3006 this.$actions
.append( special
.primary
.$element
);
3007 special
.primary
.toggleFramed( false );
3010 if ( !this.isOpening() ) {
3011 // If the dialog is currently opening, this will be called automatically soon.
3012 // This also calls #fitActions.
3018 * Fit action actions into columns or rows.
3020 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
3024 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
3026 previous
= this.verticalActionLayout
,
3027 actions
= this.actions
.get();
3030 this.toggleVerticalActionLayout( false );
3031 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
3032 action
= actions
[ i
];
3033 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
3034 this.toggleVerticalActionLayout( true );
3039 // Move the body out of the way of the foot
3040 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
3042 if ( this.verticalActionLayout
!== previous
) {
3043 // We changed the layout, window height might need to be updated.
3049 * ProcessDialog windows encapsulate a {@link OO.ui.Process process} and all of the code necessary
3050 * to complete it. If the process terminates with an error, a customizable {@link OO.ui.Error error
3051 * interface} alerts users to the trouble, permitting the user to dismiss the error and try again when
3052 * relevant. The ProcessDialog class is always extended and customized with the actions and content
3053 * required for each process.
3055 * The process dialog box consists of a header that visually represents the ‘working’ state of long
3056 * processes with an animation. The header contains the dialog title as well as
3057 * two {@link OO.ui.ActionWidget action widgets}: a ‘safe’ action on the left (e.g., ‘Cancel’) and
3058 * a ‘primary’ action on the right (e.g., ‘Done’).
3060 * Like other windows, the process dialog is managed by a {@link OO.ui.WindowManager window manager}.
3061 * Please see the [OOjs UI documentation on MediaWiki][1] for more information and examples.
3064 * // Example: Creating and opening a process dialog window.
3065 * function MyProcessDialog( config ) {
3066 * MyProcessDialog.parent.call( this, config );
3068 * OO.inheritClass( MyProcessDialog, OO.ui.ProcessDialog );
3070 * MyProcessDialog.static.name = 'myProcessDialog';
3071 * MyProcessDialog.static.title = 'Process dialog';
3072 * MyProcessDialog.static.actions = [
3073 * { action: 'save', label: 'Done', flags: 'primary' },
3074 * { label: 'Cancel', flags: 'safe' }
3077 * MyProcessDialog.prototype.initialize = function () {
3078 * MyProcessDialog.parent.prototype.initialize.apply( this, arguments );
3079 * this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
3080 * 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>' );
3081 * this.$body.append( this.content.$element );
3083 * MyProcessDialog.prototype.getActionProcess = function ( action ) {
3084 * var dialog = this;
3086 * return new OO.ui.Process( function () {
3087 * dialog.close( { action: action } );
3090 * return MyProcessDialog.parent.prototype.getActionProcess.call( this, action );
3093 * var windowManager = new OO.ui.WindowManager();
3094 * $( 'body' ).append( windowManager.$element );
3096 * var dialog = new MyProcessDialog();
3097 * windowManager.addWindows( [ dialog ] );
3098 * windowManager.openWindow( dialog );
3100 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Windows/Process_Dialogs
3104 * @extends OO.ui.Dialog
3107 * @param {Object} [config] Configuration options
3109 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
3110 // Parent constructor
3111 OO
.ui
.ProcessDialog
.parent
.call( this, config
);
3114 this.fitOnOpen
= false;
3117 this.$element
.addClass( 'oo-ui-processDialog' );
3122 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
3127 * Handle dismiss button click events.
3133 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
3138 * Handle retry button click events.
3140 * Hides errors and then tries again.
3144 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
3146 this.executeAction( this.currentAction
);
3152 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
3153 if ( this.actions
.isSpecial( action
) ) {
3156 return OO
.ui
.ProcessDialog
.parent
.prototype.onActionResize
.call( this, action
);
3162 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
3164 OO
.ui
.ProcessDialog
.parent
.prototype.initialize
.call( this );
3167 this.$navigation
= $( '<div>' );
3168 this.$location
= $( '<div>' );
3169 this.$safeActions
= $( '<div>' );
3170 this.$primaryActions
= $( '<div>' );
3171 this.$otherActions
= $( '<div>' );
3172 this.dismissButton
= new OO
.ui
.ButtonWidget( {
3173 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
3175 this.retryButton
= new OO
.ui
.ButtonWidget();
3176 this.$errors
= $( '<div>' );
3177 this.$errorsTitle
= $( '<div>' );
3180 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
3181 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
3184 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
3186 .append( this.title
.$element
)
3187 .addClass( 'oo-ui-processDialog-location' );
3188 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
3189 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
3190 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
3192 .addClass( 'oo-ui-processDialog-errors-title' )
3193 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
3195 .addClass( 'oo-ui-processDialog-errors oo-ui-element-hidden' )
3196 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
3198 .addClass( 'oo-ui-processDialog-content' )
3199 .append( this.$errors
);
3201 .addClass( 'oo-ui-processDialog-navigation' )
3202 // Note: Order of appends below is important. These are in the order
3203 // we want tab to go through them. Display-order is handled entirely
3204 // by CSS absolute-positioning. As such, primary actions like "done"
3206 .append( this.$primaryActions
, this.$location
, this.$safeActions
);
3207 this.$head
.append( this.$navigation
);
3208 this.$foot
.append( this.$otherActions
);
3214 OO
.ui
.ProcessDialog
.prototype.getActionWidgets = function ( actions
) {
3216 isMobile
= OO
.ui
.isMobile(),
3219 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
3220 config
= $.extend( { framed
: !OO
.ui
.isMobile() }, actions
[ i
] );
3222 ( config
.flags
=== 'back' || ( Array
.isArray( config
.flags
) && config
.flags
.indexOf( 'back' ) !== -1 ) )
3230 new OO
.ui
.ActionWidget( config
)
3239 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
3240 var i
, len
, other
, special
, others
;
3243 OO
.ui
.ProcessDialog
.parent
.prototype.attachActions
.call( this );
3245 special
= this.actions
.getSpecial();
3246 others
= this.actions
.getOthers();
3247 if ( special
.primary
) {
3248 this.$primaryActions
.append( special
.primary
.$element
);
3250 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
3251 other
= others
[ i
];
3252 this.$otherActions
.append( other
.$element
);
3254 if ( special
.safe
) {
3255 this.$safeActions
.append( special
.safe
.$element
);
3259 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
3265 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
3267 return OO
.ui
.ProcessDialog
.parent
.prototype.executeAction
.call( this, action
)
3268 .fail( function ( errors
) {
3269 process
.showErrors( errors
|| [] );
3276 OO
.ui
.ProcessDialog
.prototype.setDimensions = function () {
3278 OO
.ui
.ProcessDialog
.parent
.prototype.setDimensions
.apply( this, arguments
);
3284 * Fit label between actions.
3289 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
3290 var safeWidth
, primaryWidth
, biggerWidth
, labelWidth
, navigationWidth
, leftWidth
, rightWidth
,
3291 size
= this.getSizeProperties();
3293 if ( typeof size
.width
!== 'number' ) {
3294 if ( this.isOpened() ) {
3295 navigationWidth
= this.$head
.width() - 20;
3296 } else if ( this.isOpening() ) {
3297 if ( !this.fitOnOpen
) {
3298 // Size is relative and the dialog isn't open yet, so wait.
3299 this.manager
.opening
.done( this.fitLabel
.bind( this ) );
3300 this.fitOnOpen
= true;
3307 navigationWidth
= size
.width
- 20;
3310 safeWidth
= this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0;
3311 primaryWidth
= this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0;
3312 biggerWidth
= Math
.max( safeWidth
, primaryWidth
);
3314 labelWidth
= this.title
.$element
.width();
3316 if ( 2 * biggerWidth
+ labelWidth
< navigationWidth
) {
3317 // We have enough space to center the label
3318 leftWidth
= rightWidth
= biggerWidth
;
3320 // Let's hope we at least have enough space not to overlap, because we can't wrap the label…
3321 if ( this.getDir() === 'ltr' ) {
3322 leftWidth
= safeWidth
;
3323 rightWidth
= primaryWidth
;
3325 leftWidth
= primaryWidth
;
3326 rightWidth
= safeWidth
;
3330 this.$location
.css( { paddingLeft
: leftWidth
, paddingRight
: rightWidth
} );
3336 * Handle errors that occurred during accept or reject processes.
3339 * @param {OO.ui.Error[]|OO.ui.Error} errors Errors to be handled
3341 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
3342 var i
, len
, $item
, actions
,
3348 if ( errors
instanceof OO
.ui
.Error
) {
3349 errors
= [ errors
];
3352 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
3353 if ( !errors
[ i
].isRecoverable() ) {
3354 recoverable
= false;
3356 if ( errors
[ i
].isWarning() ) {
3359 $item
= $( '<div>' )
3360 .addClass( 'oo-ui-processDialog-error' )
3361 .append( errors
[ i
].getMessage() );
3362 items
.push( $item
[ 0 ] );
3364 this.$errorItems
= $( items
);
3365 if ( recoverable
) {
3366 abilities
[ this.currentAction
] = true;
3367 // Copy the flags from the first matching action
3368 actions
= this.actions
.get( { actions
: this.currentAction
} );
3369 if ( actions
.length
) {
3370 this.retryButton
.clearFlags().setFlags( actions
[ 0 ].getFlags() );
3373 abilities
[ this.currentAction
] = false;
3374 this.actions
.setAbilities( abilities
);
3377 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
3379 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
3381 this.retryButton
.toggle( recoverable
);
3382 this.$errorsTitle
.after( this.$errorItems
);
3383 this.$errors
.removeClass( 'oo-ui-element-hidden' ).scrollTop( 0 );
3391 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
3392 this.$errors
.addClass( 'oo-ui-element-hidden' );
3393 if ( this.$errorItems
) {
3394 this.$errorItems
.remove();
3395 this.$errorItems
= null;
3402 OO
.ui
.ProcessDialog
.prototype.getTeardownProcess = function ( data
) {
3404 return OO
.ui
.ProcessDialog
.parent
.prototype.getTeardownProcess
.call( this, data
)
3405 .first( function () {
3406 // Make sure to hide errors
3408 this.fitOnOpen
= false;
3417 * Lazy-initialize and return a global OO.ui.WindowManager instance, used by OO.ui.alert and
3421 * @return {OO.ui.WindowManager}
3423 OO
.ui
.getWindowManager = function () {
3424 if ( !OO
.ui
.windowManager
) {
3425 OO
.ui
.windowManager
= new OO
.ui
.WindowManager();
3426 $( 'body' ).append( OO
.ui
.windowManager
.$element
);
3427 OO
.ui
.windowManager
.addWindows( [ new OO
.ui
.MessageDialog() ] );
3429 return OO
.ui
.windowManager
;
3433 * Display a quick modal alert dialog, using a OO.ui.MessageDialog. While the dialog is open, the
3434 * rest of the page will be dimmed out and the user won't be able to interact with it. The dialog
3435 * has only one action button, labelled "OK", clicking it will simply close the dialog.
3437 * A window manager is created automatically when this function is called for the first time.
3440 * OO.ui.alert( 'Something happened!' ).done( function () {
3441 * console.log( 'User closed the dialog.' );
3444 * @param {jQuery|string} text Message text to display
3445 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3446 * @return {jQuery.Promise} Promise resolved when the user closes the dialog
3448 OO
.ui
.alert = function ( text
, options
) {
3449 return OO
.ui
.getWindowManager().openWindow( 'message', $.extend( {
3451 actions
: [ OO
.ui
.MessageDialog
.static.actions
[ 0 ] ]
3452 }, options
) ).then( function ( opened
) {
3453 return opened
.then( function ( closing
) {
3454 return closing
.then( function () {
3455 return $.Deferred().resolve();
3462 * Display a quick modal confirmation dialog, using a OO.ui.MessageDialog. While the dialog is open,
3463 * the rest of the page will be dimmed out and the user won't be able to interact with it. The
3464 * dialog has two action buttons, one to confirm an operation (labelled "OK") and one to cancel it
3465 * (labelled "Cancel").
3467 * A window manager is created automatically when this function is called for the first time.
3470 * OO.ui.confirm( 'Are you sure?' ).done( function ( confirmed ) {
3471 * if ( confirmed ) {
3472 * console.log( 'User clicked "OK"!' );
3474 * console.log( 'User clicked "Cancel" or closed the dialog.' );
3478 * @param {jQuery|string} text Message text to display
3479 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3480 * @return {jQuery.Promise} Promise resolved when the user closes the dialog. If the user chose to
3481 * confirm, the promise will resolve to boolean `true`; otherwise, it will resolve to boolean
3484 OO
.ui
.confirm = function ( text
, options
) {
3485 return OO
.ui
.getWindowManager().openWindow( 'message', $.extend( {
3487 }, options
) ).then( function ( opened
) {
3488 return opened
.then( function ( closing
) {
3489 return closing
.then( function ( data
) {
3490 return $.Deferred().resolve( !!( data
&& data
.action
=== 'accept' ) );
3497 * Display a quick modal prompt dialog, using a OO.ui.MessageDialog. While the dialog is open,
3498 * the rest of the page will be dimmed out and the user won't be able to interact with it. The
3499 * dialog has a text input widget and two action buttons, one to confirm an operation (labelled "OK")
3500 * and one to cancel it (labelled "Cancel").
3502 * A window manager is created automatically when this function is called for the first time.
3505 * OO.ui.prompt( 'Choose a line to go to', { textInput: { placeholder: 'Line number' } } ).done( function ( result ) {
3506 * if ( result !== null ) {
3507 * console.log( 'User typed "' + result + '" then clicked "OK".' );
3509 * console.log( 'User clicked "Cancel" or closed the dialog.' );
3513 * @param {jQuery|string} text Message text to display
3514 * @param {Object} [options] Additional options, see OO.ui.MessageDialog#getSetupProcess
3515 * @param {Object} [options.textInput] Additional options for text input widget, see OO.ui.TextInputWidget
3516 * @return {jQuery.Promise} Promise resolved when the user closes the dialog. If the user chose to
3517 * confirm, the promise will resolve with the value of the text input widget; otherwise, it will
3518 * resolve to `null`.
3520 OO
.ui
.prompt = function ( text
, options
) {
3521 var manager
= OO
.ui
.getWindowManager(),
3522 textInput
= new OO
.ui
.TextInputWidget( ( options
&& options
.textInput
) || {} ),
3523 textField
= new OO
.ui
.FieldLayout( textInput
, {
3528 // TODO: This is a little hacky, and could be done by extending MessageDialog instead.
3530 return manager
.openWindow( 'message', $.extend( {
3531 message
: textField
.$element
3532 }, options
) ).then( function ( opened
) {
3534 textInput
.on( 'enter', function () {
3535 manager
.getCurrentWindow().close( { action
: 'accept' } );
3538 return opened
.then( function ( closing
) {
3539 return closing
.then( function ( data
) {
3540 return $.Deferred().resolve( data
&& data
.action
=== 'accept' ? textInput
.getValue() : null );