2 * OOjs UI v0.1.0-pre (12b66051ff)
3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2014-11-06T19:08:33Z
16 * Namespace for all classes, static methods and static properties.
48 * Get the user's language and any fallback languages.
50 * These language codes are used to localize user interface elements in the user's language.
52 * In environments that provide a localization system, this function should be overridden to
53 * return the user's language(s). The default implementation returns English (en) only.
55 * @return {string[]} Language codes, in descending order of priority
57 OO
.ui
.getUserLanguages = function () {
62 * Get a value in an object keyed by language code.
64 * @param {Object.<string,Mixed>} obj Object keyed by language code
65 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
66 * @param {string} [fallback] Fallback code, used if no matching language can be found
67 * @return {Mixed} Local value
69 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
76 // Known user language
77 langs
= OO
.ui
.getUserLanguages();
78 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
85 if ( obj
[fallback
] ) {
88 // First existing language
98 * Message store for the default implementation of OO.ui.msg
100 * Environments that provide a localization system should not use this, but should override
101 * OO.ui.msg altogether.
106 // Tool tip for a button that moves items in a list down one place
107 'ooui-outline-control-move-down': 'Move item down',
108 // Tool tip for a button that moves items in a list up one place
109 'ooui-outline-control-move-up': 'Move item up',
110 // Tool tip for a button that removes items from a list
111 'ooui-outline-control-remove': 'Remove item',
112 // Label for the toolbar group that contains a list of all other available tools
113 'ooui-toolbar-more': 'More',
114 // Label for the fake tool that expands the full list of tools in a toolbar group
115 'ooui-toolgroup-expand': 'More',
116 // Label for the fake tool that collapses the full list of tools in a toolbar group
117 'ooui-toolgroup-collapse': 'Fewer',
118 // Default label for the accept button of a confirmation dialog
119 'ooui-dialog-message-accept': 'OK',
120 // Default label for the reject button of a confirmation dialog
121 'ooui-dialog-message-reject': 'Cancel',
122 // Title for process dialog error description
123 'ooui-dialog-process-error': 'Something went wrong',
124 // Label for process dialog dismiss error button, visible when describing errors
125 'ooui-dialog-process-dismiss': 'Dismiss',
126 // Label for process dialog retry action button, visible when describing only recoverable errors
127 'ooui-dialog-process-retry': 'Try again',
128 // Label for process dialog retry action button, visible when describing only warnings
129 'ooui-dialog-process-continue': 'Continue'
133 * Get a localized message.
135 * In environments that provide a localization system, this function should be overridden to
136 * return the message translated in the user's language. The default implementation always returns
139 * After the message key, message parameters may optionally be passed. In the default implementation,
140 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
141 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
142 * they support unnamed, ordered message parameters.
145 * @param {string} key Message key
146 * @param {Mixed...} [params] Message parameters
147 * @return {string} Translated message with parameters substituted
149 OO
.ui
.msg = function ( key
) {
150 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
151 if ( typeof message
=== 'string' ) {
152 // Perform $1 substitution
153 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
154 var i
= parseInt( n
, 10 );
155 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
158 // Return placeholder if message not found
159 message
= '[' + key
+ ']';
165 * Package a message and arguments for deferred resolution.
167 * Use this when you are statically specifying a message and the message may not yet be present.
169 * @param {string} key Message key
170 * @param {Mixed...} [params] Message parameters
171 * @return {Function} Function that returns the resolved message when executed
173 OO
.ui
.deferMsg = function () {
174 var args
= arguments
;
176 return OO
.ui
.msg
.apply( OO
.ui
, args
);
183 * If the message is a function it will be executed, otherwise it will pass through directly.
185 * @param {Function|string} msg Deferred message, or message text
186 * @return {string} Resolved message
188 OO
.ui
.resolveMsg = function ( msg
) {
189 if ( $.isFunction( msg
) ) {
198 * Element that can be marked as pending.
204 * @param {Object} [config] Configuration options
206 OO
.ui
.PendingElement
= function OoUiPendingElement( config
) {
207 // Configuration initialization
208 config
= config
|| {};
212 this.$pending
= null;
215 this.setPendingElement( config
.$pending
|| this.$element
);
220 OO
.initClass( OO
.ui
.PendingElement
);
225 * Set the pending element (and clean up any existing one).
227 * @param {jQuery} $pending The element to set to pending.
229 OO
.ui
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
230 if ( this.$pending
) {
231 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
234 this.$pending
= $pending
;
235 if ( this.pending
> 0 ) {
236 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
241 * Check if input is pending.
245 OO
.ui
.PendingElement
.prototype.isPending = function () {
246 return !!this.pending
;
250 * Increase the pending stack.
254 OO
.ui
.PendingElement
.prototype.pushPending = function () {
255 if ( this.pending
=== 0 ) {
256 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
257 this.updateThemeClasses();
265 * Reduce the pending stack.
271 OO
.ui
.PendingElement
.prototype.popPending = function () {
272 if ( this.pending
=== 1 ) {
273 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
274 this.updateThemeClasses();
276 this.pending
= Math
.max( 0, this.pending
- 1 );
286 * @mixins OO.EventEmitter
289 * @param {Object} [config] Configuration options
291 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
292 // Configuration intialization
293 config
= config
|| {};
295 // Mixin constructors
296 OO
.EventEmitter
.call( this );
301 actions
: 'getAction',
305 this.categorized
= {};
308 this.organized
= false;
309 this.changing
= false;
310 this.changed
= false;
315 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
317 /* Static Properties */
320 * Symbolic name of dialog.
327 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
333 * @param {OO.ui.ActionWidget} action Action that was clicked
338 * @param {OO.ui.ActionWidget} action Action that was resized
343 * @param {OO.ui.ActionWidget[]} added Actions added
348 * @param {OO.ui.ActionWidget[]} added Actions removed
358 * Handle action change events.
362 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
363 this.organized
= false;
364 if ( this.changing
) {
367 this.emit( 'change' );
372 * Check if a action is one of the special actions.
374 * @param {OO.ui.ActionWidget} action Action to check
375 * @return {boolean} Action is special
377 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
380 for ( flag
in this.special
) {
381 if ( action
=== this.special
[flag
] ) {
392 * @param {Object} [filters] Filters to use, omit to get all actions
393 * @param {string|string[]} [filters.actions] Actions that actions must have
394 * @param {string|string[]} [filters.flags] Flags that actions must have
395 * @param {string|string[]} [filters.modes] Modes that actions must have
396 * @param {boolean} [filters.visible] Actions must be visible
397 * @param {boolean} [filters.disabled] Actions must be disabled
398 * @return {OO.ui.ActionWidget[]} Actions 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
++ ) {
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
++ ) {
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 actions with special flags, such as 'safe' and 'primary'.
453 * Special flags can be configured by changing #static-specialFlags in a subclass.
455 * @return {OO.ui.ActionWidget|null} Safe action
457 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
459 return $.extend( {}, this.special
);
465 * Other actions include all non-special visible actions.
467 * @return {OO.ui.ActionWidget[]} Other actions
469 OO
.ui
.ActionSet
.prototype.getOthers = function () {
471 return this.others
.slice();
475 * Toggle actions based on their modes.
477 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
478 * visibility; matching actions will be shown, non-matching actions will be hidden.
480 * @param {string} mode Mode actions must have
485 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
488 this.changing
= true;
489 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
490 action
= this.list
[i
];
491 action
.toggle( action
.hasMode( mode
) );
494 this.organized
= false;
495 this.changing
= false;
496 this.emit( 'change' );
502 * Change which actions are able to be performed.
504 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
506 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
507 * indicate actions are able to be performed
510 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
511 var i
, len
, action
, item
;
513 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
515 action
= item
.getAction();
516 if ( actions
[action
] !== undefined ) {
517 item
.setDisabled( !actions
[action
] );
525 * Executes a function once per action.
527 * When making changes to multiple actions, use this method instead of iterating over the actions
528 * manually to defer emitting a change event until after all actions have been changed.
530 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
531 * @param {Function} callback Callback to run for each action; callback is invoked with three
532 * arguments: the action, the action's index, the list of actions being iterated over
535 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
536 this.changed
= false;
537 this.changing
= true;
538 this.get( filter
).forEach( callback
);
539 this.changing
= false;
540 if ( this.changed
) {
541 this.emit( 'change' );
550 * @param {OO.ui.ActionWidget[]} actions Actions to add
555 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
558 this.changing
= true;
559 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
561 action
.connect( this, {
562 click
: [ 'emit', 'click', action
],
563 resize
: [ 'emit', 'resize', action
],
564 toggle
: [ 'onActionChange' ]
566 this.list
.push( action
);
568 this.organized
= false;
569 this.emit( 'add', actions
);
570 this.changing
= false;
571 this.emit( 'change' );
579 * @param {OO.ui.ActionWidget[]} actions Actions to remove
584 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
585 var i
, len
, index
, action
;
587 this.changing
= true;
588 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
590 index
= this.list
.indexOf( action
);
591 if ( index
!== -1 ) {
592 action
.disconnect( this );
593 this.list
.splice( index
, 1 );
596 this.organized
= false;
597 this.emit( 'remove', actions
);
598 this.changing
= false;
599 this.emit( 'change' );
605 * Remove all actions.
611 OO
.ui
.ActionSet
.prototype.clear = function () {
613 removed
= this.list
.slice();
615 this.changing
= true;
616 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
617 action
= this.list
[i
];
618 action
.disconnect( this );
623 this.organized
= false;
624 this.emit( 'remove', removed
);
625 this.changing
= false;
626 this.emit( 'change' );
634 * This is called whenver organized information is requested. It will only reorganize the actions
635 * if something has changed since the last time it ran.
640 OO
.ui
.ActionSet
.prototype.organize = function () {
641 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
642 specialFlags
= this.constructor.static.specialFlags
;
644 if ( !this.organized
) {
645 this.categorized
= {};
648 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
649 action
= this.list
[i
];
650 if ( action
.isVisible() ) {
651 // Populate catgeories
652 for ( category
in this.categories
) {
653 if ( !this.categorized
[category
] ) {
654 this.categorized
[category
] = {};
656 list
= action
[this.categories
[category
]]();
657 if ( !Array
.isArray( list
) ) {
660 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
662 if ( !this.categorized
[category
][item
] ) {
663 this.categorized
[category
][item
] = [];
665 this.categorized
[category
][item
].push( action
);
668 // Populate special/others
670 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
671 flag
= specialFlags
[j
];
672 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
673 this.special
[flag
] = action
;
679 this.others
.push( action
);
683 this.organized
= true;
690 * DOM element abstraction.
696 * @param {Object} [config] Configuration options
697 * @cfg {Function} [$] jQuery for the frame the widget is in
698 * @cfg {string[]} [classes] CSS class names to add
699 * @cfg {string} [text] Text to insert
700 * @cfg {jQuery} [$content] Content elements to append (after text)
702 OO
.ui
.Element
= function OoUiElement( config
) {
703 // Configuration initialization
704 config
= config
|| {};
707 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
708 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
709 this.elementGroup
= null;
710 this.debouncedUpdateThemeClassesHandler
= this.debouncedUpdateThemeClasses
.bind( this );
711 this.updateThemeClassesPending
= false;
714 if ( $.isArray( config
.classes
) ) {
715 this.$element
.addClass( config
.classes
.join( ' ' ) );
718 this.$element
.text( config
.text
);
720 if ( config
.$content
) {
721 this.$element
.append( config
.$content
);
727 OO
.initClass( OO
.ui
.Element
);
729 /* Static Properties */
734 * This may be ignored if #getTagName is overridden.
740 OO
.ui
.Element
.static.tagName
= 'div';
745 * Get a jQuery function within a specific document.
748 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
749 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
751 * @return {Function} Bound jQuery function
753 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
754 function wrapper( selector
) {
755 return $( selector
, wrapper
.context
);
758 wrapper
.context
= this.getDocument( context
);
761 wrapper
.$iframe
= $iframe
;
768 * Get the document of an element.
771 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
772 * @return {HTMLDocument|null} Document object
774 OO
.ui
.Element
.getDocument = function ( obj
) {
775 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
776 return ( obj
[0] && obj
[0].ownerDocument
) ||
777 // Empty jQuery selections might have a context
784 ( obj
.nodeType
=== 9 && obj
) ||
789 * Get the window of an element or document.
792 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
793 * @return {Window} Window object
795 OO
.ui
.Element
.getWindow = function ( obj
) {
796 var doc
= this.getDocument( obj
);
797 return doc
.parentWindow
|| doc
.defaultView
;
801 * Get the direction of an element or document.
804 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
805 * @return {string} Text direction, either 'ltr' or 'rtl'
807 OO
.ui
.Element
.getDir = function ( obj
) {
810 if ( obj
instanceof jQuery
) {
813 isDoc
= obj
.nodeType
=== 9;
814 isWin
= obj
.document
!== undefined;
815 if ( isDoc
|| isWin
) {
821 return $( obj
).css( 'direction' );
825 * Get the offset between two frames.
827 * TODO: Make this function not use recursion.
830 * @param {Window} from Window of the child frame
831 * @param {Window} [to=window] Window of the parent frame
832 * @param {Object} [offset] Offset to start with, used internally
833 * @return {Object} Offset object, containing left and top properties
835 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
836 var i
, len
, frames
, frame
, rect
;
842 offset
= { top
: 0, left
: 0 };
844 if ( from.parent
=== from ) {
848 // Get iframe element
849 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
850 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
851 if ( frames
[i
].contentWindow
=== from ) {
857 // Recursively accumulate offset values
859 rect
= frame
.getBoundingClientRect();
860 offset
.left
+= rect
.left
;
861 offset
.top
+= rect
.top
;
863 this.getFrameOffset( from.parent
, offset
);
870 * Get the offset between two elements.
872 * The two elements may be in a different frame, but in that case the frame $element is in must
873 * be contained in the frame $anchor is in.
876 * @param {jQuery} $element Element whose position to get
877 * @param {jQuery} $anchor Element to get $element's position relative to
878 * @return {Object} Translated position coordinates, containing top and left properties
880 OO
.ui
.Element
.getRelativePosition = function ( $element
, $anchor
) {
881 var iframe
, iframePos
,
882 pos
= $element
.offset(),
883 anchorPos
= $anchor
.offset(),
884 elementDocument
= this.getDocument( $element
),
885 anchorDocument
= this.getDocument( $anchor
);
887 // If $element isn't in the same document as $anchor, traverse up
888 while ( elementDocument
!== anchorDocument
) {
889 iframe
= elementDocument
.defaultView
.frameElement
;
891 throw new Error( '$element frame is not contained in $anchor frame' );
893 iframePos
= $( iframe
).offset();
894 pos
.left
+= iframePos
.left
;
895 pos
.top
+= iframePos
.top
;
896 elementDocument
= iframe
.ownerDocument
;
898 pos
.left
-= anchorPos
.left
;
899 pos
.top
-= anchorPos
.top
;
904 * Get element border sizes.
907 * @param {HTMLElement} el Element to measure
908 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
910 OO
.ui
.Element
.getBorders = function ( el
) {
911 var doc
= el
.ownerDocument
,
912 win
= doc
.parentWindow
|| doc
.defaultView
,
913 style
= win
&& win
.getComputedStyle
?
914 win
.getComputedStyle( el
, null ) :
917 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
918 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
919 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
920 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
923 top
: Math
.round( top
),
924 left
: Math
.round( left
),
925 bottom
: Math
.round( bottom
),
926 right
: Math
.round( right
)
931 * Get dimensions of an element or window.
934 * @param {HTMLElement|Window} el Element to measure
935 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
937 OO
.ui
.Element
.getDimensions = function ( el
) {
939 doc
= el
.ownerDocument
|| el
.document
,
940 win
= doc
.parentWindow
|| doc
.defaultView
;
942 if ( win
=== el
|| el
=== doc
.documentElement
) {
945 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
947 top
: $win
.scrollTop(),
948 left
: $win
.scrollLeft()
950 scrollbar
: { right
: 0, bottom
: 0 },
954 bottom
: $win
.innerHeight(),
955 right
: $win
.innerWidth()
961 borders
: this.getBorders( el
),
963 top
: $el
.scrollTop(),
964 left
: $el
.scrollLeft()
967 right
: $el
.innerWidth() - el
.clientWidth
,
968 bottom
: $el
.innerHeight() - el
.clientHeight
970 rect
: el
.getBoundingClientRect()
976 * Get closest scrollable container.
978 * Traverses up until either a scrollable element or the root is reached, in which case the window
982 * @param {HTMLElement} el Element to find scrollable container for
983 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
984 * @return {HTMLElement} Closest scrollable container
986 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
988 props
= [ 'overflow' ],
989 $parent
= $( el
).parent();
991 if ( dimension
=== 'x' || dimension
=== 'y' ) {
992 props
.push( 'overflow-' + dimension
);
995 while ( $parent
.length
) {
996 if ( $parent
[0] === el
.ownerDocument
.body
) {
1001 val
= $parent
.css( props
[i
] );
1002 if ( val
=== 'auto' || val
=== 'scroll' ) {
1006 $parent
= $parent
.parent();
1008 return this.getDocument( el
).body
;
1012 * Scroll element into view.
1015 * @param {HTMLElement} el Element to scroll into view
1016 * @param {Object} [config] Configuration options
1017 * @param {string} [config.duration] jQuery animation duration value
1018 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
1019 * to scroll in both directions
1020 * @param {Function} [config.complete] Function to call when scrolling completes
1022 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
1023 // Configuration initialization
1024 config
= config
|| {};
1027 callback
= typeof config
.complete
=== 'function' && config
.complete
,
1028 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
1030 eld
= this.getDimensions( el
),
1031 scd
= this.getDimensions( sc
),
1032 $win
= $( this.getWindow( el
) );
1034 // Compute the distances between the edges of el and the edges of the scroll viewport
1035 if ( $sc
.is( 'body' ) ) {
1036 // If the scrollable container is the <body> this is easy
1039 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
1040 left
: eld
.rect
.left
,
1041 right
: $win
.innerWidth() - eld
.rect
.right
1044 // Otherwise, we have to subtract el's coordinates from sc's coordinates
1046 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
1047 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
1048 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
1049 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
1053 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1054 if ( rel
.top
< 0 ) {
1055 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
1056 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
1057 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
1060 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1061 if ( rel
.left
< 0 ) {
1062 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
1063 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
1064 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
1067 if ( !$.isEmptyObject( anim
) ) {
1068 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
1070 $sc
.queue( function ( next
) {
1083 * Bind a handler for an event on a DOM element.
1085 * Used to be for working around a jQuery bug (jqbug.com/14180),
1086 * but obsolete as of jQuery 1.11.0.
1089 * @deprecated Use jQuery#on instead.
1090 * @param {HTMLElement|jQuery} el DOM element
1091 * @param {string} event Event to bind
1092 * @param {Function} callback Callback to call when the event fires
1094 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1095 $( el
).on( event
, callback
);
1099 * Unbind a handler bound with #static-method-onDOMEvent.
1101 * @deprecated Use jQuery#off instead.
1103 * @param {HTMLElement|jQuery} el DOM element
1104 * @param {string} event Event to unbind
1105 * @param {Function} [callback] Callback to unbind
1107 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1108 $( el
).off( event
, callback
);
1114 * Check if element supports one or more methods.
1116 * @param {string|string[]} methods Method or list of methods to check
1117 * @return {boolean} All methods are supported
1119 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1123 methods
= $.isArray( methods
) ? methods
: [ methods
];
1124 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1125 if ( $.isFunction( this[methods
[i
]] ) ) {
1130 return methods
.length
=== support
;
1134 * Update the theme-provided classes.
1136 * @localdoc This is called in element mixins and widget classes anytime state changes.
1137 * Updating is debounced, minimizing overhead of changing multiple attributes and
1138 * guaranteeing that theme updates do not occur within an element's constructor
1140 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1141 if ( !this.updateThemeClassesPending
) {
1142 this.updateThemeClassesPending
= true;
1143 setTimeout( this.debouncedUpdateThemeClassesHandler
);
1150 OO
.ui
.Element
.prototype.debouncedUpdateThemeClasses = function () {
1151 OO
.ui
.theme
.updateElementClasses( this );
1152 this.updateThemeClassesPending
= false;
1156 * Get the HTML tag name.
1158 * Override this method to base the result on instance information.
1160 * @return {string} HTML tag name
1162 OO
.ui
.Element
.prototype.getTagName = function () {
1163 return this.constructor.static.tagName
;
1167 * Check if the element is attached to the DOM
1168 * @return {boolean} The element is attached to the DOM
1170 OO
.ui
.Element
.prototype.isElementAttached = function () {
1171 return $.contains( this.getElementDocument(), this.$element
[0] );
1175 * Get the DOM document.
1177 * @return {HTMLDocument} Document object
1179 OO
.ui
.Element
.prototype.getElementDocument = function () {
1180 return OO
.ui
.Element
.getDocument( this.$element
);
1184 * Get the DOM window.
1186 * @return {Window} Window object
1188 OO
.ui
.Element
.prototype.getElementWindow = function () {
1189 return OO
.ui
.Element
.getWindow( this.$element
);
1193 * Get closest scrollable container.
1195 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1196 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1200 * Get group element is in.
1202 * @return {OO.ui.GroupElement|null} Group element, null if none
1204 OO
.ui
.Element
.prototype.getElementGroup = function () {
1205 return this.elementGroup
;
1209 * Set group element is in.
1211 * @param {OO.ui.GroupElement|null} group Group element, null if none
1214 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1215 this.elementGroup
= group
;
1220 * Scroll element into view.
1222 * @param {Object} [config] Configuration options
1224 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1225 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1229 * Bind a handler for an event on this.$element
1231 * @deprecated Use jQuery#on instead.
1232 * @param {string} event
1233 * @param {Function} callback
1235 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1236 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1240 * Unbind a handler bound with #offDOMEvent
1242 * @deprecated Use jQuery#off instead.
1243 * @param {string} event
1244 * @param {Function} callback
1246 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1247 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1251 * Container for elements.
1255 * @extends OO.ui.Element
1256 * @mixins OO.EventEmitter
1259 * @param {Object} [config] Configuration options
1261 OO
.ui
.Layout
= function OoUiLayout( config
) {
1262 // Configuration initialization
1263 config
= config
|| {};
1265 // Parent constructor
1266 OO
.ui
.Layout
.super.call( this, config
);
1268 // Mixin constructors
1269 OO
.EventEmitter
.call( this );
1272 this.$element
.addClass( 'oo-ui-layout' );
1277 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1278 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1281 * User interface control.
1285 * @extends OO.ui.Element
1286 * @mixins OO.EventEmitter
1289 * @param {Object} [config] Configuration options
1290 * @cfg {boolean} [disabled=false] Disable
1292 OO
.ui
.Widget
= function OoUiWidget( config
) {
1293 // Initialize config
1294 config
= $.extend( { disabled
: false }, config
);
1296 // Parent constructor
1297 OO
.ui
.Widget
.super.call( this, config
);
1299 // Mixin constructors
1300 OO
.EventEmitter
.call( this );
1303 this.visible
= true;
1304 this.disabled
= null;
1305 this.wasDisabled
= null;
1308 this.$element
.addClass( 'oo-ui-widget' );
1309 this.setDisabled( !!config
.disabled
);
1314 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1315 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1321 * @param {boolean} disabled Widget is disabled
1326 * @param {boolean} visible Widget is visible
1332 * Check if the widget is disabled.
1334 * @return {boolean} Button is disabled
1336 OO
.ui
.Widget
.prototype.isDisabled = function () {
1337 return this.disabled
;
1341 * Check if widget is visible.
1343 * @return {boolean} Widget is visible
1345 OO
.ui
.Widget
.prototype.isVisible = function () {
1346 return this.visible
;
1350 * Set the disabled state of the widget.
1352 * This should probably change the widgets' appearance and prevent it from being used.
1354 * @param {boolean} disabled Disable widget
1357 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1360 this.disabled
= !!disabled
;
1361 isDisabled
= this.isDisabled();
1362 if ( isDisabled
!== this.wasDisabled
) {
1363 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1364 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1365 this.emit( 'disable', isDisabled
);
1366 this.updateThemeClasses();
1368 this.wasDisabled
= isDisabled
;
1374 * Toggle visibility of widget.
1376 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1380 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1381 show
= show
=== undefined ? !this.visible
: !!show
;
1383 if ( show
!== this.isVisible() ) {
1384 this.visible
= show
;
1385 this.$element
.toggle( show
);
1386 this.emit( 'toggle', show
);
1393 * Update the disabled state, in case of changes in parent widget.
1397 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1398 this.setDisabled( this.disabled
);
1403 * Container for elements in a child frame.
1405 * Use together with OO.ui.WindowManager.
1409 * @extends OO.ui.Element
1410 * @mixins OO.EventEmitter
1412 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1413 * teardown processes are executed when the window is closed.
1415 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1416 * - Window manager begins opening window
1417 * - {@link #getSetupProcess} method is called and its result executed
1418 * - {@link #getReadyProcess} method is called and its result executed
1419 * - Window is now open
1421 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1422 * - Window manager begins closing window
1423 * - {@link #getHoldProcess} method is called and its result executed
1424 * - {@link #getTeardownProcess} method is called and its result executed
1425 * - Window is now closed
1427 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1428 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1429 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1430 * processing can complete. Always assume window processes are executed asychronously. See
1431 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1432 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1434 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1435 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1438 * @param {Object} [config] Configuration options
1439 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1443 OO
.ui
.Window
= function OoUiWindow( config
) {
1444 // Configuration initialization
1445 config
= config
|| {};
1447 // Parent constructor
1448 OO
.ui
.Window
.super.call( this, config
);
1450 // Mixin constructors
1451 OO
.EventEmitter
.call( this );
1454 this.manager
= null;
1455 this.initialized
= false;
1456 this.visible
= false;
1457 this.opening
= null;
1458 this.closing
= null;
1461 this.loading
= null;
1462 this.size
= config
.size
|| this.constructor.static.size
;
1463 this.$frame
= this.$( '<div>' );
1464 this.$overlay
= this.$( '<div>' );
1468 .addClass( 'oo-ui-window' )
1469 .append( this.$frame
, this.$overlay
);
1470 this.$frame
.addClass( 'oo-ui-window-frame' );
1471 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1473 // NOTE: Additional intitialization will occur when #setManager is called
1478 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1479 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1481 /* Static Properties */
1484 * Symbolic name of size.
1486 * Size is used if no size is configured during construction.
1490 * @property {string}
1492 OO
.ui
.Window
.static.size
= 'medium';
1494 /* Static Methods */
1497 * Transplant the CSS styles from as parent document to a frame's document.
1499 * This loops over the style sheets in the parent document, and copies their nodes to the
1500 * frame's document. It then polls the document to see when all styles have loaded, and once they
1501 * have, resolves the promise.
1503 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1504 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1505 * Firefox, where the styles won't load until the iframe becomes visible.
1507 * For details of how we arrived at the strategy used in this function, see #load.
1511 * @param {HTMLDocument} parentDoc Document to transplant styles from
1512 * @param {HTMLDocument} frameDoc Document to transplant styles to
1513 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1514 * @return {jQuery.Promise} Promise resolved when styles have loaded
1516 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1517 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1518 $pollNodes
= $( [] ),
1519 // Fake font-family value
1520 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1521 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1522 deferred
= $.Deferred();
1524 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1525 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1526 if ( styleNode
.disabled
) {
1530 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1531 // External stylesheet; use @import
1532 styleText
= '@import url(' + styleNode
.href
+ ');';
1534 // Internal stylesheet; just copy the text
1535 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1536 // other browsers, so fall back to '' rather than 'undefined'
1537 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1540 // Create a node with a unique ID that we're going to monitor to see when the CSS
1542 if ( styleNode
.oouiFrameTransplantStylesId
) {
1543 // If we're nesting transplantStyles operations and this node already has
1544 // a CSS rule to wait for loading, reuse it
1545 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1547 // Otherwise, create a new ID
1548 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1551 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1552 // The font-family rule will only take effect once the @import finishes
1553 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1556 // Create a node with id=pollNodeId
1557 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1558 .attr( 'id', pollNodeId
)
1559 .appendTo( frameDoc
.body
)
1562 // Add our modified CSS as a <style> tag
1563 newNode
= frameDoc
.createElement( 'style' );
1564 newNode
.textContent
= styleText
;
1565 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1566 frameDoc
.head
.appendChild( newNode
);
1568 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1570 // Poll every 100ms until all external stylesheets have loaded
1571 $pendingPollNodes
= $pollNodes
;
1572 timeoutID
= setTimeout( function pollExternalStylesheets() {
1574 $pendingPollNodes
.length
> 0 &&
1575 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1577 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1580 if ( $pendingPollNodes
.length
=== 0 ) {
1582 if ( timeoutID
!== null ) {
1584 $pollNodes
.remove();
1588 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1591 // ...but give up after a while
1592 if ( timeout
!== 0 ) {
1593 setTimeout( function () {
1595 clearTimeout( timeoutID
);
1597 $pollNodes
.remove();
1600 }, timeout
|| 5000 );
1603 return deferred
.promise();
1609 * Handle mouse down events.
1611 * @param {jQuery.Event} e Mouse down event
1613 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1614 // Prevent clicking on the click-block from stealing focus
1615 if ( e
.target
=== this.$element
[0] ) {
1621 * Check if window has been initialized.
1623 * @return {boolean} Window has been initialized
1625 OO
.ui
.Window
.prototype.isInitialized = function () {
1626 return this.initialized
;
1630 * Check if window is visible.
1632 * @return {boolean} Window is visible
1634 OO
.ui
.Window
.prototype.isVisible = function () {
1635 return this.visible
;
1639 * Check if window is loading.
1641 * @return {boolean} Window is loading
1643 OO
.ui
.Window
.prototype.isLoading = function () {
1644 return this.loading
&& this.loading
.state() === 'pending';
1648 * Check if window is loaded.
1650 * @return {boolean} Window is loaded
1652 OO
.ui
.Window
.prototype.isLoaded = function () {
1653 return this.loading
&& this.loading
.state() === 'resolved';
1657 * Check if window is opening.
1659 * This is a wrapper around OO.ui.WindowManager#isOpening.
1661 * @return {boolean} Window is opening
1663 OO
.ui
.Window
.prototype.isOpening = function () {
1664 return this.manager
.isOpening( this );
1668 * Check if window is closing.
1670 * This is a wrapper around OO.ui.WindowManager#isClosing.
1672 * @return {boolean} Window is closing
1674 OO
.ui
.Window
.prototype.isClosing = function () {
1675 return this.manager
.isClosing( this );
1679 * Check if window is opened.
1681 * This is a wrapper around OO.ui.WindowManager#isOpened.
1683 * @return {boolean} Window is opened
1685 OO
.ui
.Window
.prototype.isOpened = function () {
1686 return this.manager
.isOpened( this );
1690 * Get the window manager.
1692 * @return {OO.ui.WindowManager} Manager of window
1694 OO
.ui
.Window
.prototype.getManager = function () {
1695 return this.manager
;
1699 * Get the window size.
1701 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1703 OO
.ui
.Window
.prototype.getSize = function () {
1708 * Get the height of the dialog contents.
1710 * @return {number} Content height
1712 OO
.ui
.Window
.prototype.getContentHeight = function () {
1713 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements
1714 var bodyHeight
, oldHeight
= this.$frame
[0].style
.height
;
1715 this.$frame
[0].style
.height
= '1px';
1716 bodyHeight
= this.getBodyHeight();
1717 this.$frame
[0].style
.height
= oldHeight
;
1720 // Add buffer for border
1721 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1722 // Use combined heights of children
1723 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1728 * Get the height of the dialog contents.
1730 * When this function is called, the dialog will temporarily have been resized
1731 * to height=1px, so .scrollHeight measurements can be taken accurately.
1733 * @return {number} Height of content
1735 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1736 return this.$body
[0].scrollHeight
;
1740 * Get the directionality of the frame
1742 * @return {string} Directionality, 'ltr' or 'rtl'
1744 OO
.ui
.Window
.prototype.getDir = function () {
1749 * Get a process for setting up a window for use.
1751 * Each time the window is opened this process will set it up for use in a particular context, based
1752 * on the `data` argument.
1754 * When you override this method, you can add additional setup steps to the process the parent
1755 * method provides using the 'first' and 'next' methods.
1758 * @param {Object} [data] Window opening data
1759 * @return {OO.ui.Process} Setup process
1761 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1762 return new OO
.ui
.Process();
1766 * Get a process for readying a window for use.
1768 * Each time the window is open and setup, this process will ready it up for use in a particular
1769 * context, based on the `data` argument.
1771 * When you override this method, you can add additional setup steps to the process the parent
1772 * method provides using the 'first' and 'next' methods.
1775 * @param {Object} [data] Window opening data
1776 * @return {OO.ui.Process} Setup process
1778 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1779 return new OO
.ui
.Process();
1783 * Get a process for holding a window from use.
1785 * Each time the window is closed, this process will hold it from use in a particular context, based
1786 * on the `data` argument.
1788 * When you override this method, you can add additional setup steps to the process the parent
1789 * method provides using the 'first' and 'next' methods.
1792 * @param {Object} [data] Window closing data
1793 * @return {OO.ui.Process} Hold process
1795 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1796 return new OO
.ui
.Process();
1800 * Get a process for tearing down a window after use.
1802 * Each time the window is closed this process will tear it down and do something with the user's
1803 * interactions within the window, based on the `data` argument.
1805 * When you override this method, you can add additional teardown steps to the process the parent
1806 * method provides using the 'first' and 'next' methods.
1809 * @param {Object} [data] Window closing data
1810 * @return {OO.ui.Process} Teardown process
1812 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1813 return new OO
.ui
.Process();
1817 * Toggle visibility of window.
1819 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1820 * instead of display.
1822 * @param {boolean} [show] Make window visible, omit to toggle visibility
1826 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1827 show
= show
=== undefined ? !this.visible
: !!show
;
1829 if ( show
!== this.isVisible() ) {
1830 this.visible
= show
;
1832 if ( this.isolated
&& !this.isLoaded() ) {
1833 // Hide the window using visibility instead of display until loading is complete
1834 // Can't use display: none; because that prevents the iframe from loading in Firefox
1835 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1837 this.$element
.toggle( show
).css( 'visibility', '' );
1839 this.emit( 'toggle', show
);
1846 * Set the window manager.
1848 * This must be called before initialize. Calling it more than once will cause an error.
1850 * @param {OO.ui.WindowManager} manager Manager for this window
1851 * @throws {Error} If called more than once
1854 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1855 if ( this.manager
) {
1856 throw new Error( 'Cannot set window manager, window already has a manager' );
1860 this.manager
= manager
;
1861 this.isolated
= manager
.shouldIsolate();
1864 if ( this.isolated
) {
1865 this.$iframe
= this.$( '<iframe>' );
1866 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1867 this.$frame
.append( this.$iframe
);
1868 this.$ = function () {
1869 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1871 // WARNING: Do not use this.$ again until #initialize is called
1873 this.$content
= this.$( '<div>' );
1874 this.$document
= $( this.getElementDocument() );
1875 this.$content
.addClass( 'oo-ui-window-content' );
1876 this.$frame
.append( this.$content
);
1878 this.toggle( false );
1880 // Figure out directionality:
1881 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1887 * Set the window size.
1889 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1892 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1894 this.manager
.updateWindowSize( this );
1899 * Set window dimensions.
1901 * Properties are applied to the frame container.
1903 * @param {Object} dim CSS dimension properties
1904 * @param {string|number} [dim.width] Width
1905 * @param {string|number} [dim.minWidth] Minimum width
1906 * @param {string|number} [dim.maxWidth] Maximum width
1907 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1908 * @param {string|number} [dim.minWidth] Minimum height
1909 * @param {string|number} [dim.maxWidth] Maximum height
1912 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1913 // Apply width before height so height is not based on wrapping content using the wrong width
1915 width
: dim
.width
|| '',
1916 minWidth
: dim
.minWidth
|| '',
1917 maxWidth
: dim
.maxWidth
|| ''
1920 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1921 minHeight
: dim
.minHeight
|| '',
1922 maxHeight
: dim
.maxHeight
|| ''
1928 * Initialize window contents.
1930 * The first time the window is opened, #initialize is called when it's safe to begin populating
1931 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1933 * Once this method is called, this.$ can be used to create elements within the frame.
1935 * @throws {Error} If not attached to a manager
1938 OO
.ui
.Window
.prototype.initialize = function () {
1939 if ( !this.manager
) {
1940 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1944 this.$head
= this.$( '<div>' );
1945 this.$body
= this.$( '<div>' );
1946 this.$foot
= this.$( '<div>' );
1947 this.$innerOverlay
= this.$( '<div>' );
1950 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
1953 this.$head
.addClass( 'oo-ui-window-head' );
1954 this.$body
.addClass( 'oo-ui-window-body' );
1955 this.$foot
.addClass( 'oo-ui-window-foot' );
1956 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
1957 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
1965 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1966 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1968 * @param {Object} [data] Window opening data
1969 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1970 * first argument will be a promise which will be resolved when the window begins closing
1972 OO
.ui
.Window
.prototype.open = function ( data
) {
1973 return this.manager
.openWindow( this, data
);
1979 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1980 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1982 * @param {Object} [data] Window closing data
1983 * @return {jQuery.Promise} Promise resolved when window is closed
1985 OO
.ui
.Window
.prototype.close = function ( data
) {
1986 return this.manager
.closeWindow( this, data
);
1992 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1995 * @param {Object} [data] Window opening data
1996 * @return {jQuery.Promise} Promise resolved when window is setup
1998 OO
.ui
.Window
.prototype.setup = function ( data
) {
2000 deferred
= $.Deferred();
2002 this.$element
.show();
2003 this.visible
= true;
2004 this.getSetupProcess( data
).execute().done( function () {
2005 // Force redraw by asking the browser to measure the elements' widths
2006 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2007 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2011 return deferred
.promise();
2017 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
2020 * @param {Object} [data] Window opening data
2021 * @return {jQuery.Promise} Promise resolved when window is ready
2023 OO
.ui
.Window
.prototype.ready = function ( data
) {
2025 deferred
= $.Deferred();
2027 this.$content
.focus();
2028 this.getReadyProcess( data
).execute().done( function () {
2029 // Force redraw by asking the browser to measure the elements' widths
2030 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2031 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2035 return deferred
.promise();
2041 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2044 * @param {Object} [data] Window closing data
2045 * @return {jQuery.Promise} Promise resolved when window is held
2047 OO
.ui
.Window
.prototype.hold = function ( data
) {
2049 deferred
= $.Deferred();
2051 this.getHoldProcess( data
).execute().done( function () {
2052 // Get the focused element within the window's content
2053 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
2055 // Blur the focused element
2056 if ( $focus
.length
) {
2060 // Force redraw by asking the browser to measure the elements' widths
2061 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2062 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2066 return deferred
.promise();
2072 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2075 * @param {Object} [data] Window closing data
2076 * @return {jQuery.Promise} Promise resolved when window is torn down
2078 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2080 deferred
= $.Deferred();
2082 this.getTeardownProcess( data
).execute().done( function () {
2083 // Force redraw by asking the browser to measure the elements' widths
2084 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
2085 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2086 win
.$element
.hide();
2087 win
.visible
= false;
2091 return deferred
.promise();
2095 * Load the frame contents.
2097 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
2098 * promise will be resolved. Calling while loading will return a promise but not trigger a new
2099 * loading cycle. Calling after loading is complete will return a promise that's already been
2102 * Sounds simple right? Read on...
2104 * When you create a dynamic iframe using open/write/close, the window.load event for the
2105 * iframe is triggered when you call close, and there's no further load event to indicate that
2106 * everything is actually loaded.
2108 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2109 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2110 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2111 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2112 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2114 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2115 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2116 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2117 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2118 * accessing .cssRules is allowed.
2120 * However, now that we control the styles we're injecting, we might as well do away with
2121 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2122 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2123 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2124 * font-family rule is not applied until after the `@import` finishes.
2126 * All this stylesheet injection and polling magic is in #transplantStyles.
2128 * @return {jQuery.Promise} Promise resolved when loading is complete
2131 OO
.ui
.Window
.prototype.load = function () {
2132 var sub
, doc
, loading
,
2135 // Non-isolated windows are already "loaded"
2136 if ( !this.loading
&& !this.isolated
) {
2137 this.loading
= $.Deferred().resolve();
2139 // Set initialized state after so sub-classes aren't confused by it being set by calling
2140 // their parent initialize method
2141 this.initialized
= true;
2144 // Return existing promise if already loading or loaded
2145 if ( this.loading
) {
2146 return this.loading
.promise();
2150 loading
= this.loading
= $.Deferred();
2151 sub
= this.$iframe
.prop( 'contentWindow' );
2154 // Initialize contents
2159 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2160 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2161 '<div class="oo-ui-window-content"></div>' +
2168 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$iframe
);
2169 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2170 this.$document
= this.$( doc
);
2173 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2174 .always( function () {
2175 // Initialize isolated windows
2177 // Set initialized state after so sub-classes aren't confused by it being set by calling
2178 // their parent initialize method
2179 win
.initialized
= true;
2180 // Undo the visibility: hidden; hack and apply display: none;
2181 // We can do this safely now that the iframe has initialized
2182 // (don't do this from within #initialize because it has to happen
2183 // after the all subclasses have been handled as well).
2184 win
.toggle( win
.isVisible() );
2189 return loading
.promise();
2193 * Base class for all dialogs.
2196 * - Manage the window (open and close, etc.).
2197 * - Store the internal name and display title.
2198 * - A stack to track one or more pending actions.
2199 * - Manage a set of actions that can be performed.
2200 * - Configure and create action widgets.
2203 * - Close the dialog with Escape key.
2204 * - Visually lock the dialog while an action is in
2205 * progress (aka "pending").
2207 * Subclass responsibilities:
2208 * - Display the title somewhere.
2209 * - Add content to the dialog.
2210 * - Provide a UI to close the dialog.
2211 * - Display the action widgets somewhere.
2215 * @extends OO.ui.Window
2216 * @mixins OO.ui.PendingElement
2219 * @param {Object} [config] Configuration options
2221 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2222 // Parent constructor
2223 OO
.ui
.Dialog
.super.call( this, config
);
2225 // Mixin constructors
2226 OO
.ui
.PendingElement
.call( this );
2229 this.actions
= new OO
.ui
.ActionSet();
2230 this.attachedActions
= [];
2231 this.currentAction
= null;
2234 this.actions
.connect( this, {
2235 click
: 'onActionClick',
2236 resize
: 'onActionResize',
2237 change
: 'onActionsChange'
2242 .addClass( 'oo-ui-dialog' )
2243 .attr( 'role', 'dialog' );
2248 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2249 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2251 /* Static Properties */
2254 * Symbolic name of dialog.
2259 * @property {string}
2261 OO
.ui
.Dialog
.static.name
= '';
2269 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2271 OO
.ui
.Dialog
.static.title
= '';
2274 * List of OO.ui.ActionWidget configuration options.
2278 * @property {Object[]}
2280 OO
.ui
.Dialog
.static.actions
= [];
2283 * Close dialog when the escape key is pressed.
2288 * @property {boolean}
2290 OO
.ui
.Dialog
.static.escapable
= true;
2295 * Handle frame document key down events.
2297 * @param {jQuery.Event} e Key down event
2299 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2300 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2307 * Handle action resized events.
2309 * @param {OO.ui.ActionWidget} action Action that was resized
2311 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2312 // Override in subclass
2316 * Handle action click events.
2318 * @param {OO.ui.ActionWidget} action Action that was clicked
2320 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2321 if ( !this.isPending() ) {
2322 this.currentAction
= action
;
2323 this.executeAction( action
.getAction() );
2328 * Handle actions change event.
2330 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2331 this.detachActions();
2332 if ( !this.isClosing() ) {
2333 this.attachActions();
2338 * Get set of actions.
2340 * @return {OO.ui.ActionSet}
2342 OO
.ui
.Dialog
.prototype.getActions = function () {
2343 return this.actions
;
2347 * Get a process for taking action.
2349 * When you override this method, you can add additional accept steps to the process the parent
2350 * method provides using the 'first' and 'next' methods.
2353 * @param {string} [action] Symbolic name of action
2354 * @return {OO.ui.Process} Action process
2356 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2357 return new OO
.ui
.Process()
2358 .next( function () {
2360 // An empty action always closes the dialog without data, which should always be
2361 // safe and make no changes
2370 * @param {Object} [data] Dialog opening data
2371 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2372 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2373 * action item, omit to use #static-actions
2375 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2379 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2380 .next( function () {
2383 config
= this.constructor.static,
2384 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2386 this.title
.setLabel(
2387 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2389 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2391 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2394 this.actions
.add( items
);
2401 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2403 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2404 .first( function () {
2405 this.actions
.clear();
2406 this.currentAction
= null;
2413 OO
.ui
.Dialog
.prototype.initialize = function () {
2415 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2418 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2421 if ( this.constructor.static.escapable
) {
2422 this.$document
.on( 'keydown', this.onDocumentKeyDown
.bind( this ) );
2426 this.$content
.addClass( 'oo-ui-dialog-content' );
2427 this.setPendingElement( this.$head
);
2431 * Attach action actions.
2433 OO
.ui
.Dialog
.prototype.attachActions = function () {
2434 // Remember the list of potentially attached actions
2435 this.attachedActions
= this.actions
.get();
2439 * Detach action actions.
2443 OO
.ui
.Dialog
.prototype.detachActions = function () {
2446 // Detach all actions that may have been previously attached
2447 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2448 this.attachedActions
[i
].$element
.detach();
2450 this.attachedActions
= [];
2454 * Execute an action.
2456 * @param {string} action Symbolic name of action to execute
2457 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2459 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2461 return this.getActionProcess( action
).execute()
2462 .always( this.popPending
.bind( this ) );
2466 * Collection of windows.
2469 * @extends OO.ui.Element
2470 * @mixins OO.EventEmitter
2472 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2473 * already opening or opened, the current window will be closed without data. Empty closing data
2474 * should always result in the window being closed without causing constructive or destructive
2477 * As a window is opened and closed, it passes through several stages and the manager emits several
2478 * corresponding events.
2480 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2481 * - {@link #event-opening} is emitted with `opening` promise
2482 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2483 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2484 * - `setup` progress notification is emitted from opening promise
2485 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2486 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2487 * - `ready` progress notification is emitted from opening promise
2488 * - `opening` promise is resolved with `opened` promise
2489 * - Window is now open
2491 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2492 * - `opened` promise is resolved with `closing` promise
2493 * - {@link #event-closing} is emitted with `closing` promise
2494 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2495 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2496 * - `hold` progress notification is emitted from opening promise
2497 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2498 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2499 * - `teardown` progress notification is emitted from opening promise
2500 * - Closing promise is resolved
2501 * - Window is now closed
2504 * @param {Object} [config] Configuration options
2505 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2506 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2507 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2509 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2510 // Configuration initialization
2511 config
= config
|| {};
2513 // Parent constructor
2514 OO
.ui
.WindowManager
.super.call( this, config
);
2516 // Mixin constructors
2517 OO
.EventEmitter
.call( this );
2520 this.factory
= config
.factory
;
2521 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2522 this.isolate
= !!config
.isolate
;
2524 this.opening
= null;
2526 this.closing
= null;
2527 this.preparingToOpen
= null;
2528 this.preparingToClose
= null;
2530 this.currentWindow
= null;
2531 this.$ariaHidden
= null;
2532 this.requestedSize
= null;
2533 this.onWindowResizeTimeout
= null;
2534 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2535 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2536 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2537 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2541 .addClass( 'oo-ui-windowManager' )
2542 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2547 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2548 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2553 * Window is opening.
2555 * Fired when the window begins to be opened.
2558 * @param {OO.ui.Window} win Window that's being opened
2559 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2560 * resolved the first argument will be a promise which will be resolved when the window begins
2561 * closing, the second argument will be the opening data; progress notifications will be fired on
2562 * the promise for `setup` and `ready` when those processes are completed respectively.
2563 * @param {Object} data Window opening data
2567 * Window is closing.
2569 * Fired when the window begins to be closed.
2572 * @param {OO.ui.Window} win Window that's being closed
2573 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2574 * is resolved the first argument will be a the closing data; progress notifications will be fired
2575 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2576 * @param {Object} data Window closing data
2580 * Window was resized.
2583 * @param {OO.ui.Window} win Window that was resized
2586 /* Static Properties */
2589 * Map of symbolic size names and CSS properties.
2593 * @property {Object}
2595 OO
.ui
.WindowManager
.static.sizes
= {
2606 // These can be non-numeric because they are never used in calculations
2613 * Symbolic name of default size.
2615 * Default size is used if the window's requested size is not recognized.
2619 * @property {string}
2621 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2626 * Handle window resize events.
2628 * @param {jQuery.Event} e Window resize event
2630 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2631 clearTimeout( this.onWindowResizeTimeout
);
2632 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2636 * Handle window resize events.
2638 * @param {jQuery.Event} e Window resize event
2640 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2641 if ( this.currentWindow
) {
2642 this.updateWindowSize( this.currentWindow
);
2647 * Handle window mouse wheel events.
2649 * @param {jQuery.Event} e Mouse wheel event
2651 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function ( e
) {
2652 // Kill all events in the parent window if the child window is isolated,
2653 // or if the event didn't come from the child window
2654 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2658 * Handle document key down events.
2660 * @param {jQuery.Event} e Key down event
2662 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2663 switch ( e
.which
) {
2664 case OO
.ui
.Keys
.PAGEUP
:
2665 case OO
.ui
.Keys
.PAGEDOWN
:
2666 case OO
.ui
.Keys
.END
:
2667 case OO
.ui
.Keys
.HOME
:
2668 case OO
.ui
.Keys
.LEFT
:
2670 case OO
.ui
.Keys
.RIGHT
:
2671 case OO
.ui
.Keys
.DOWN
:
2672 // Kill all events in the parent window if the child window is isolated,
2673 // or if the event didn't come from the child window
2674 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2679 * Check if window is opening.
2681 * @return {boolean} Window is opening
2683 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2684 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2688 * Check if window is closing.
2690 * @return {boolean} Window is closing
2692 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2693 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2697 * Check if window is opened.
2699 * @return {boolean} Window is opened
2701 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2702 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2706 * Check if window contents should be isolated.
2708 * Window content isolation is done using inline frames.
2710 * @return {boolean} Window contents should be isolated
2712 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2713 return this.isolate
;
2717 * Check if a window is being managed.
2719 * @param {OO.ui.Window} win Window to check
2720 * @return {boolean} Window is being managed
2722 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2725 for ( name
in this.windows
) {
2726 if ( this.windows
[name
] === win
) {
2735 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2737 * @param {OO.ui.Window} win Window being opened
2738 * @param {Object} [data] Window opening data
2739 * @return {number} Milliseconds to wait
2741 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2746 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2748 * @param {OO.ui.Window} win Window being opened
2749 * @param {Object} [data] Window opening data
2750 * @return {number} Milliseconds to wait
2752 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2757 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2759 * @param {OO.ui.Window} win Window being closed
2760 * @param {Object} [data] Window closing data
2761 * @return {number} Milliseconds to wait
2763 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2768 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2770 * @param {OO.ui.Window} win Window being closed
2771 * @param {Object} [data] Window closing data
2772 * @return {number} Milliseconds to wait
2774 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2775 return this.modal
? 250 : 0;
2779 * Get managed window by symbolic name.
2781 * If window is not yet instantiated, it will be instantiated and added automatically.
2783 * @param {string} name Symbolic window name
2784 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2785 * @throws {Error} If the symbolic name is unrecognized by the factory
2786 * @throws {Error} If the symbolic name unrecognized as a managed window
2788 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2789 var deferred
= $.Deferred(),
2790 win
= this.windows
[name
];
2792 if ( !( win
instanceof OO
.ui
.Window
) ) {
2793 if ( this.factory
) {
2794 if ( !this.factory
.lookup( name
) ) {
2795 deferred
.reject( new OO
.ui
.Error(
2796 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2799 win
= this.factory
.create( name
, this, { $: this.$ } );
2800 this.addWindows( [ win
] );
2801 deferred
.resolve( win
);
2804 deferred
.reject( new OO
.ui
.Error(
2805 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2809 deferred
.resolve( win
);
2812 return deferred
.promise();
2816 * Get current window.
2818 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2820 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2821 return this.currentWindow
;
2827 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2828 * @param {Object} [data] Window opening data
2829 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2830 * for more details about the `opening` promise
2833 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2836 opening
= $.Deferred();
2838 // Argument handling
2839 if ( typeof win
=== 'string' ) {
2840 return this.getWindow( win
).then( function ( win
) {
2841 return manager
.openWindow( win
, data
);
2846 if ( !this.hasWindow( win
) ) {
2847 opening
.reject( new OO
.ui
.Error(
2848 'Cannot open window: window is not attached to manager'
2850 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2851 opening
.reject( new OO
.ui
.Error(
2852 'Cannot open window: another window is opening or open'
2857 if ( opening
.state() !== 'rejected' ) {
2858 // Begin loading the window if it's not loading or loaded already - may take noticable time
2859 // and we want to do this in parallel with any other preparatory actions
2860 if ( !win
.isLoading() && !win
.isLoaded() ) {
2861 // Finish initializing the window (must be done after manager is attached to DOM)
2862 win
.setManager( this );
2863 preparing
.push( win
.load() );
2866 if ( this.closing
) {
2867 // If a window is currently closing, wait for it to complete
2868 preparing
.push( this.closing
);
2871 this.preparingToOpen
= $.when
.apply( $, preparing
);
2872 // Ensure handlers get called after preparingToOpen is set
2873 this.preparingToOpen
.done( function () {
2874 if ( manager
.modal
) {
2875 manager
.toggleGlobalEvents( true );
2876 manager
.toggleAriaIsolation( true );
2878 manager
.currentWindow
= win
;
2879 manager
.opening
= opening
;
2880 manager
.preparingToOpen
= null;
2881 manager
.emit( 'opening', win
, opening
, data
);
2882 setTimeout( function () {
2883 win
.setup( data
).then( function () {
2884 manager
.updateWindowSize( win
);
2885 manager
.opening
.notify( { state
: 'setup' } );
2886 setTimeout( function () {
2887 win
.ready( data
).then( function () {
2888 manager
.opening
.notify( { state
: 'ready' } );
2889 manager
.opening
= null;
2890 manager
.opened
= $.Deferred();
2891 opening
.resolve( manager
.opened
.promise(), data
);
2893 }, manager
.getReadyDelay() );
2895 }, manager
.getSetupDelay() );
2899 return opening
.promise();
2905 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2906 * @param {Object} [data] Window closing data
2907 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2908 * for more details about the `closing` promise
2909 * @throws {Error} If no window by that name is being managed
2912 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2915 closing
= $.Deferred(),
2918 // Argument handling
2919 if ( typeof win
=== 'string' ) {
2920 win
= this.windows
[win
];
2921 } else if ( !this.hasWindow( win
) ) {
2927 closing
.reject( new OO
.ui
.Error(
2928 'Cannot close window: window is not attached to manager'
2930 } else if ( win
!== this.currentWindow
) {
2931 closing
.reject( new OO
.ui
.Error(
2932 'Cannot close window: window already closed with different data'
2934 } else if ( this.preparingToClose
|| this.closing
) {
2935 closing
.reject( new OO
.ui
.Error(
2936 'Cannot close window: window already closing with different data'
2941 if ( closing
.state() !== 'rejected' ) {
2942 if ( this.opening
) {
2943 // If the window is currently opening, close it when it's done
2944 preparing
.push( this.opening
);
2947 this.preparingToClose
= $.when
.apply( $, preparing
);
2948 // Ensure handlers get called after preparingToClose is set
2949 this.preparingToClose
.done( function () {
2950 manager
.closing
= closing
;
2951 manager
.preparingToClose
= null;
2952 manager
.emit( 'closing', win
, closing
, data
);
2953 opened
= manager
.opened
;
2954 manager
.opened
= null;
2955 opened
.resolve( closing
.promise(), data
);
2956 setTimeout( function () {
2957 win
.hold( data
).then( function () {
2958 closing
.notify( { state
: 'hold' } );
2959 setTimeout( function () {
2960 win
.teardown( data
).then( function () {
2961 closing
.notify( { state
: 'teardown' } );
2962 if ( manager
.modal
) {
2963 manager
.toggleGlobalEvents( false );
2964 manager
.toggleAriaIsolation( false );
2966 manager
.closing
= null;
2967 manager
.currentWindow
= null;
2968 closing
.resolve( data
);
2970 }, manager
.getTeardownDelay() );
2972 }, manager
.getHoldDelay() );
2976 return closing
.promise();
2982 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2983 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2984 * a statically configured symbolic name
2986 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2987 var i
, len
, win
, name
, list
;
2989 if ( $.isArray( windows
) ) {
2990 // Convert to map of windows by looking up symbolic names from static configuration
2992 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2993 name
= windows
[i
].constructor.static.name
;
2994 if ( typeof name
!== 'string' ) {
2995 throw new Error( 'Cannot add window' );
2997 list
[name
] = windows
[i
];
2999 } else if ( $.isPlainObject( windows
) ) {
3004 for ( name
in list
) {
3006 this.windows
[name
] = win
;
3007 this.$element
.append( win
.$element
);
3014 * Windows will be closed before they are removed.
3016 * @param {string} name Symbolic name of window to remove
3017 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3018 * @throws {Error} If windows being removed are not being managed
3020 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3021 var i
, len
, win
, name
,
3024 cleanup = function ( name
, win
) {
3025 delete manager
.windows
[name
];
3026 win
.$element
.detach();
3029 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3031 win
= this.windows
[name
];
3033 throw new Error( 'Cannot remove window' );
3035 promises
.push( this.closeWindow( name
).then( cleanup
.bind( null, name
, win
) ) );
3038 return $.when
.apply( $, promises
);
3042 * Remove all windows.
3044 * Windows will be closed before they are removed.
3046 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3048 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3049 return this.removeWindows( Object
.keys( this.windows
) );
3055 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3059 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3060 // Bypass for non-current, and thus invisible, windows
3061 if ( win
!== this.currentWindow
) {
3065 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
3066 sizes
= this.constructor.static.sizes
,
3067 size
= win
.getSize();
3069 if ( !sizes
[size
] ) {
3070 size
= this.constructor.static.defaultSize
;
3072 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
3076 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3077 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3078 win
.setDimensions( sizes
[size
] );
3080 this.emit( 'resize', win
);
3086 * Bind or unbind global events for scrolling.
3088 * @param {boolean} [on] Bind global events
3091 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3092 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3095 if ( !this.globalEvents
) {
3096 this.$( this.getElementDocument() ).on( {
3097 // Prevent scrolling by keys in top-level window
3098 keydown
: this.onDocumentKeyDownHandler
3100 this.$( this.getElementWindow() ).on( {
3101 // Prevent scrolling by wheel in top-level window
3102 mousewheel
: this.onWindowMouseWheelHandler
,
3103 // Start listening for top-level window dimension changes
3104 'orientationchange resize': this.onWindowResizeHandler
3106 this.globalEvents
= true;
3108 } else if ( this.globalEvents
) {
3109 // Unbind global events
3110 this.$( this.getElementDocument() ).off( {
3111 // Allow scrolling by keys in top-level window
3112 keydown
: this.onDocumentKeyDownHandler
3114 this.$( this.getElementWindow() ).off( {
3115 // Allow scrolling by wheel in top-level window
3116 mousewheel
: this.onWindowMouseWheelHandler
,
3117 // Stop listening for top-level window dimension changes
3118 'orientationchange resize': this.onWindowResizeHandler
3120 this.globalEvents
= false;
3127 * Toggle screen reader visibility of content other than the window manager.
3129 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3132 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3133 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3136 if ( !this.$ariaHidden
) {
3137 // Hide everything other than the window manager from screen readers
3138 this.$ariaHidden
= $( 'body' )
3140 .not( this.$element
.parentsUntil( 'body' ).last() )
3141 .attr( 'aria-hidden', '' );
3143 } else if ( this.$ariaHidden
) {
3144 // Restore screen reader visiblity
3145 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3146 this.$ariaHidden
= null;
3153 * Destroy window manager.
3155 * Windows will not be closed, only removed from the DOM.
3157 OO
.ui
.WindowManager
.prototype.destroy = function () {
3158 this.toggleGlobalEvents( false );
3159 this.toggleAriaIsolation( false );
3160 this.$element
.remove();
3168 * @param {string|jQuery} message Description of error
3169 * @param {Object} [config] Configuration options
3170 * @cfg {boolean} [recoverable=true] Error is recoverable
3171 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3173 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3174 // Configuration initialization
3175 config
= config
|| {};
3178 this.message
= message
instanceof jQuery
? message
: String( message
);
3179 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3180 this.warning
= !!config
.warning
;
3185 OO
.initClass( OO
.ui
.Error
);
3190 * Check if error can be recovered from.
3192 * @return {boolean} Error is recoverable
3194 OO
.ui
.Error
.prototype.isRecoverable = function () {
3195 return this.recoverable
;
3199 * Check if the error is a warning
3201 * @return {boolean} Error is warning
3203 OO
.ui
.Error
.prototype.isWarning = function () {
3204 return this.warning
;
3208 * Get error message as DOM nodes.
3210 * @return {jQuery} Error message in DOM nodes
3212 OO
.ui
.Error
.prototype.getMessage = function () {
3213 return this.message
instanceof jQuery
?
3214 this.message
.clone() :
3215 $( '<div>' ).text( this.message
).contents();
3219 * Get error message as text.
3221 * @return {string} Error message
3223 OO
.ui
.Error
.prototype.getMessageText = function () {
3224 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3228 * A list of functions, called in sequence.
3230 * If a function added to a process returns boolean false the process will stop; if it returns an
3231 * object with a `promise` method the process will use the promise to either continue to the next
3232 * step when the promise is resolved or stop when the promise is rejected.
3237 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3238 * call, see #createStep for more information
3239 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3241 * @return {Object} Step object, with `callback` and `context` properties
3243 OO
.ui
.Process = function ( step
, context
) {
3248 if ( step
!== undefined ) {
3249 this.next( step
, context
);
3255 OO
.initClass( OO
.ui
.Process
);
3260 * Start the process.
3262 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3263 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3264 * process, the remaining steps will not be taken
3266 OO
.ui
.Process
.prototype.execute = function () {
3267 var i
, len
, promise
;
3270 * Continue execution.
3273 * @param {Array} step A function and the context it should be called in
3274 * @return {Function} Function that continues the process
3276 function proceed( step
) {
3277 return function () {
3278 // Execute step in the correct context
3280 result
= step
.callback
.call( step
.context
);
3282 if ( result
=== false ) {
3283 // Use rejected promise for boolean false results
3284 return $.Deferred().reject( [] ).promise();
3286 if ( typeof result
=== 'number' ) {
3288 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3290 // Use a delayed promise for numbers, expecting them to be in milliseconds
3291 deferred
= $.Deferred();
3292 setTimeout( deferred
.resolve
, result
);
3293 return deferred
.promise();
3295 if ( result
instanceof OO
.ui
.Error
) {
3296 // Use rejected promise for error
3297 return $.Deferred().reject( [ result
] ).promise();
3299 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3300 // Use rejected promise for list of errors
3301 return $.Deferred().reject( result
).promise();
3303 // Duck-type the object to see if it can produce a promise
3304 if ( result
&& $.isFunction( result
.promise
) ) {
3305 // Use a promise generated from the result
3306 return result
.promise();
3308 // Use resolved promise for other results
3309 return $.Deferred().resolve().promise();
3313 if ( this.steps
.length
) {
3314 // Generate a chain reaction of promises
3315 promise
= proceed( this.steps
[0] )();
3316 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3317 promise
= promise
.then( proceed( this.steps
[i
] ) );
3320 promise
= $.Deferred().resolve().promise();
3327 * Create a process step.
3330 * @param {number|jQuery.Promise|Function} step
3332 * - Number of milliseconds to wait; or
3333 * - Promise to wait to be resolved; or
3334 * - Function to execute
3335 * - If it returns boolean false the process will stop
3336 * - If it returns an object with a `promise` method the process will use the promise to either
3337 * continue to the next step when the promise is resolved or stop when the promise is rejected
3338 * - If it returns a number, the process will wait for that number of milliseconds before
3340 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3342 * @return {Object} Step object, with `callback` and `context` properties
3344 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3345 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3347 callback: function () {
3353 if ( $.isFunction( step
) ) {
3359 throw new Error( 'Cannot create process step: number, promise or function expected' );
3363 * Add step to the beginning of the process.
3365 * @inheritdoc #createStep
3366 * @return {OO.ui.Process} this
3369 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3370 this.steps
.unshift( this.createStep( step
, context
) );
3375 * Add step to the end of the process.
3377 * @inheritdoc #createStep
3378 * @return {OO.ui.Process} this
3381 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3382 this.steps
.push( this.createStep( step
, context
) );
3387 * Factory for tools.
3390 * @extends OO.Factory
3393 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3394 // Parent constructor
3395 OO
.ui
.ToolFactory
.super.call( this );
3400 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3405 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3406 var i
, len
, included
, promoted
, demoted
,
3410 // Collect included and not excluded tools
3411 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3414 promoted
= this.extract( promote
, used
);
3415 demoted
= this.extract( demote
, used
);
3418 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3419 if ( !used
[included
[i
]] ) {
3420 auto
.push( included
[i
] );
3424 return promoted
.concat( auto
).concat( demoted
);
3428 * Get a flat list of names from a list of names or groups.
3430 * Tools can be specified in the following ways:
3432 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3433 * - All tools in a group: `{ group: 'group-name' }`
3434 * - All tools: `'*'`
3437 * @param {Array|string} collection List of tools
3438 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3439 * names will be added as properties
3440 * @return {string[]} List of extracted names
3442 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3443 var i
, len
, item
, name
, tool
,
3446 if ( collection
=== '*' ) {
3447 for ( name
in this.registry
) {
3448 tool
= this.registry
[name
];
3450 // Only add tools by group name when auto-add is enabled
3451 tool
.static.autoAddToCatchall
&&
3452 // Exclude already used tools
3453 ( !used
|| !used
[name
] )
3461 } else if ( $.isArray( collection
) ) {
3462 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3463 item
= collection
[i
];
3464 // Allow plain strings as shorthand for named tools
3465 if ( typeof item
=== 'string' ) {
3466 item
= { name
: item
};
3468 if ( OO
.isPlainObject( item
) ) {
3470 for ( name
in this.registry
) {
3471 tool
= this.registry
[name
];
3473 // Include tools with matching group
3474 tool
.static.group
=== item
.group
&&
3475 // Only add tools by group name when auto-add is enabled
3476 tool
.static.autoAddToGroup
&&
3477 // Exclude already used tools
3478 ( !used
|| !used
[name
] )
3486 // Include tools with matching name and exclude already used tools
3487 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3488 names
.push( item
.name
);
3490 used
[item
.name
] = true;
3500 * Factory for tool groups.
3503 * @extends OO.Factory
3506 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3507 // Parent constructor
3508 OO
.Factory
.call( this );
3511 defaultClasses
= this.constructor.static.getDefaultClasses();
3513 // Register default toolgroups
3514 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3515 this.register( defaultClasses
[i
] );
3521 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3523 /* Static Methods */
3526 * Get a default set of classes to be registered on construction
3528 * @return {Function[]} Default classes
3530 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3533 OO
.ui
.ListToolGroup
,
3545 * @param {Object} [config] Configuration options
3547 OO
.ui
.Theme
= function OoUiTheme( config
) {
3548 // Configuration initialization
3549 config
= config
|| {};
3554 OO
.initClass( OO
.ui
.Theme
);
3559 * Get a list of classes to be applied to a widget.
3561 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3562 * otherwise state transitions will not work properly.
3564 * @param {OO.ui.Element} element Element for which to get classes
3565 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3567 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3568 return { on
: [], off
: [] };
3572 * Update CSS classes provided by the theme.
3574 * For elements with theme logic hooks, this should be called anytime there's a state change.
3576 * @param {OO.ui.Element} element Element for which to update classes
3577 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3579 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3580 var classes
= this.getElementClasses( element
);
3583 .removeClass( classes
.off
.join( ' ' ) )
3584 .addClass( classes
.on
.join( ' ' ) );
3588 * Element with a button.
3590 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3591 * and access keys for accessibility purposes.
3597 * @param {Object} [config] Configuration options
3598 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3599 * @cfg {boolean} [framed=true] Render button with a frame
3600 * @cfg {number} [tabIndex=0] Button's tab index. Use 0 to use default ordering, use -1 to prevent
3602 * @cfg {string} [accessKey] Button's access key
3604 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3605 // Configuration initialization
3606 config
= config
|| {};
3609 this.$button
= null;
3611 this.tabIndex
= null;
3612 this.accessKey
= null;
3613 this.active
= false;
3614 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3615 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3618 this.$element
.addClass( 'oo-ui-buttonElement' );
3619 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3620 this.setTabIndex( config
.tabIndex
|| 0 );
3621 this.setAccessKey( config
.accessKey
);
3622 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3627 OO
.initClass( OO
.ui
.ButtonElement
);
3629 /* Static Properties */
3632 * Cancel mouse down events.
3636 * @property {boolean}
3638 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3643 * Set the button element.
3645 * If an element is already set, it will be cleaned up before setting up the new element.
3647 * @param {jQuery} $button Element to use as button
3649 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3650 if ( this.$button
) {
3652 .removeClass( 'oo-ui-buttonElement-button' )
3653 .removeAttr( 'role accesskey tabindex' )
3654 .off( this.onMouseDownHandler
);
3657 this.$button
= $button
3658 .addClass( 'oo-ui-buttonElement-button' )
3659 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3660 .on( 'mousedown', this.onMouseDownHandler
);
3664 * Handles mouse down events.
3666 * @param {jQuery.Event} e Mouse down event
3668 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3669 if ( this.isDisabled() || e
.which
!== 1 ) {
3672 // Remove the tab-index while the button is down to prevent the button from stealing focus
3673 this.$button
.removeAttr( 'tabindex' );
3674 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3675 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3676 // reliably reapply the tabindex and remove the pressed class
3677 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3678 // Prevent change of focus unless specifically configured otherwise
3679 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3685 * Handles mouse up events.
3687 * @param {jQuery.Event} e Mouse up event
3689 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3690 if ( this.isDisabled() || e
.which
!== 1 ) {
3693 // Restore the tab-index after the button is up to restore the button's accesssibility
3694 this.$button
.attr( 'tabindex', this.tabIndex
);
3695 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3696 // Stop listening for mouseup, since we only needed this once
3697 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3701 * Check if button has a frame.
3703 * @return {boolean} Button is framed
3705 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3712 * @param {boolean} [framed] Make button framed, omit to toggle
3715 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3716 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3717 if ( framed
!== this.framed
) {
3718 this.framed
= framed
;
3720 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3721 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3722 this.updateThemeClasses();
3731 * @param {number|null} tabIndex Button's tab index, use null to remove
3734 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3735 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3737 if ( this.tabIndex
!== tabIndex
) {
3738 if ( this.$button
) {
3739 if ( tabIndex
!== null ) {
3740 this.$button
.attr( 'tabindex', tabIndex
);
3742 this.$button
.removeAttr( 'tabindex' );
3745 this.tabIndex
= tabIndex
;
3754 * @param {string} accessKey Button's access key, use empty string to remove
3757 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3758 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3760 if ( this.accessKey
!== accessKey
) {
3761 if ( this.$button
) {
3762 if ( accessKey
!== null ) {
3763 this.$button
.attr( 'accesskey', accessKey
);
3765 this.$button
.removeAttr( 'accesskey' );
3768 this.accessKey
= accessKey
;
3777 * @param {boolean} [value] Make button active
3780 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3781 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3786 * Element containing a sequence of child elements.
3792 * @param {Object} [config] Configuration options
3793 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3795 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3796 // Configuration intialization
3797 config
= config
|| {};
3802 this.aggregateItemEvents
= {};
3805 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3811 * Set the group element.
3813 * If an element is already set, items will be moved to the new element.
3815 * @param {jQuery} $group Element to use as group
3817 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3820 this.$group
= $group
;
3821 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3822 this.$group
.append( this.items
[i
].$element
);
3827 * Check if there are no items.
3829 * @return {boolean} Group is empty
3831 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3832 return !this.items
.length
;
3838 * @return {OO.ui.Element[]} Items
3840 OO
.ui
.GroupElement
.prototype.getItems = function () {
3841 return this.items
.slice( 0 );
3845 * Add an aggregate item event.
3847 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3848 * and with an additional leading parameter containing the item that emitted the original event.
3849 * Other arguments that were emitted from the original event are passed through.
3851 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3852 * event, use null value to remove aggregation
3853 * @throws {Error} If aggregation already exists
3855 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3856 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3858 for ( itemEvent
in events
) {
3859 groupEvent
= events
[itemEvent
];
3861 // Remove existing aggregated event
3862 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3863 // Don't allow duplicate aggregations
3865 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3867 // Remove event aggregation from existing items
3868 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3869 item
= this.items
[i
];
3870 if ( item
.connect
&& item
.disconnect
) {
3872 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3873 item
.disconnect( this, remove
);
3876 // Prevent future items from aggregating event
3877 delete this.aggregateItemEvents
[itemEvent
];
3880 // Add new aggregate event
3882 // Make future items aggregate event
3883 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3884 // Add event aggregation to existing items
3885 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3886 item
= this.items
[i
];
3887 if ( item
.connect
&& item
.disconnect
) {
3889 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3890 item
.connect( this, add
);
3900 * Adding an existing item will move it.
3902 * @param {OO.ui.Element[]} items Items
3903 * @param {number} [index] Index to insert items at
3906 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3907 var i
, len
, item
, event
, events
, currentIndex
,
3910 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3913 // Check if item exists then remove it first, effectively "moving" it
3914 currentIndex
= $.inArray( item
, this.items
);
3915 if ( currentIndex
>= 0 ) {
3916 this.removeItems( [ item
] );
3917 // Adjust index to compensate for removal
3918 if ( currentIndex
< index
) {
3923 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3925 for ( event
in this.aggregateItemEvents
) {
3926 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3928 item
.connect( this, events
);
3930 item
.setElementGroup( this );
3931 itemElements
.push( item
.$element
.get( 0 ) );
3934 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3935 this.$group
.append( itemElements
);
3936 this.items
.push
.apply( this.items
, items
);
3937 } else if ( index
=== 0 ) {
3938 this.$group
.prepend( itemElements
);
3939 this.items
.unshift
.apply( this.items
, items
);
3941 this.items
[index
].$element
.before( itemElements
);
3942 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3951 * Items will be detached, not removed, so they can be used later.
3953 * @param {OO.ui.Element[]} items Items to remove
3956 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3957 var i
, len
, item
, index
, remove
, itemEvent
;
3959 // Remove specific items
3960 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3962 index
= $.inArray( item
, this.items
);
3963 if ( index
!== -1 ) {
3965 item
.connect
&& item
.disconnect
&&
3966 !$.isEmptyObject( this.aggregateItemEvents
)
3969 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3970 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3972 item
.disconnect( this, remove
);
3974 item
.setElementGroup( null );
3975 this.items
.splice( index
, 1 );
3976 item
.$element
.detach();
3986 * Items will be detached, not removed, so they can be used later.
3990 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3991 var i
, len
, item
, remove
, itemEvent
;
3994 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3995 item
= this.items
[i
];
3997 item
.connect
&& item
.disconnect
&&
3998 !$.isEmptyObject( this.aggregateItemEvents
)
4001 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4002 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4004 item
.disconnect( this, remove
);
4006 item
.setElementGroup( null );
4007 item
.$element
.detach();
4015 * Element containing an icon.
4017 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4018 * a control or convey information in a more space efficient way. Icons should rarely be used
4019 * without labels; such as in a toolbar where space is at a premium or within a context where the
4020 * meaning is very clear to the user.
4026 * @param {Object} [config] Configuration options
4027 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4028 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4029 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4031 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4033 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4034 // Configuration intialization
4035 config
= config
|| {};
4040 this.iconTitle
= null;
4043 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4044 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4045 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4050 OO
.initClass( OO
.ui
.IconElement
);
4052 /* Static Properties */
4057 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4059 * For i18n purposes, this property can be an object containing a `default` icon name property and
4060 * additional icon names keyed by language code.
4062 * Example of i18n icon definition:
4063 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4067 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4068 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4071 OO
.ui
.IconElement
.static.icon
= null;
4078 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4081 OO
.ui
.IconElement
.static.iconTitle
= null;
4086 * Set the icon element.
4088 * If an element is already set, it will be cleaned up before setting up the new element.
4090 * @param {jQuery} $icon Element to use as icon
4092 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4095 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4096 .removeAttr( 'title' );
4100 .addClass( 'oo-ui-iconElement-icon' )
4101 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4102 if ( this.iconTitle
!== null ) {
4103 this.$icon
.attr( 'title', this.iconTitle
);
4110 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4111 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4112 * language, use null to remove icon
4115 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4116 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4117 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4119 if ( this.icon
!== icon
) {
4121 if ( this.icon
!== null ) {
4122 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4124 if ( icon
!== null ) {
4125 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4131 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4132 this.updateThemeClasses();
4140 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4144 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4145 iconTitle
= typeof iconTitle
=== 'function' ||
4146 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4147 OO
.ui
.resolveMsg( iconTitle
) : null;
4149 if ( this.iconTitle
!== iconTitle
) {
4150 this.iconTitle
= iconTitle
;
4152 if ( this.iconTitle
!== null ) {
4153 this.$icon
.attr( 'title', iconTitle
);
4155 this.$icon
.removeAttr( 'title' );
4166 * @return {string} Icon name
4168 OO
.ui
.IconElement
.prototype.getIcon = function () {
4173 * Element containing an indicator.
4175 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4176 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4177 * instead of performing an action directly, or an item in a list which has errors that need to be
4184 * @param {Object} [config] Configuration options
4185 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4187 * @cfg {string} [indicator] Symbolic indicator name
4188 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4190 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4191 // Configuration initialization
4192 config
= config
|| {};
4195 this.$indicator
= null;
4196 this.indicator
= null;
4197 this.indicatorTitle
= null;
4200 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4201 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4202 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4207 OO
.initClass( OO
.ui
.IndicatorElement
);
4209 /* Static Properties */
4216 * @property {string|null} Symbolic indicator name
4218 OO
.ui
.IndicatorElement
.static.indicator
= null;
4225 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4228 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4233 * Set the indicator element.
4235 * If an element is already set, it will be cleaned up before setting up the new element.
4237 * @param {jQuery} $indicator Element to use as indicator
4239 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4240 if ( this.$indicator
) {
4242 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4243 .removeAttr( 'title' );
4246 this.$indicator
= $indicator
4247 .addClass( 'oo-ui-indicatorElement-indicator' )
4248 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4249 if ( this.indicatorTitle
!== null ) {
4250 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4255 * Set indicator name.
4257 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4260 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4261 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4263 if ( this.indicator
!== indicator
) {
4264 if ( this.$indicator
) {
4265 if ( this.indicator
!== null ) {
4266 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4268 if ( indicator
!== null ) {
4269 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4272 this.indicator
= indicator
;
4275 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4276 this.updateThemeClasses();
4282 * Set indicator title.
4284 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4285 * null for no indicator title
4288 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4289 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4290 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4291 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4293 if ( this.indicatorTitle
!== indicatorTitle
) {
4294 this.indicatorTitle
= indicatorTitle
;
4295 if ( this.$indicator
) {
4296 if ( this.indicatorTitle
!== null ) {
4297 this.$indicator
.attr( 'title', indicatorTitle
);
4299 this.$indicator
.removeAttr( 'title' );
4308 * Get indicator name.
4310 * @return {string} Symbolic name of indicator
4312 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4313 return this.indicator
;
4317 * Get indicator title.
4319 * @return {string} Indicator title text
4321 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4322 return this.indicatorTitle
;
4326 * Element containing a label.
4332 * @param {Object} [config] Configuration options
4333 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4334 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4335 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4337 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4338 // Configuration initialization
4339 config
= config
|| {};
4344 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4347 this.setLabel( config
.label
|| this.constructor.static.label
);
4348 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4353 OO
.initClass( OO
.ui
.LabelElement
);
4355 /* Static Properties */
4362 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4365 OO
.ui
.LabelElement
.static.label
= null;
4370 * Set the label element.
4372 * If an element is already set, it will be cleaned up before setting up the new element.
4374 * @param {jQuery} $label Element to use as label
4376 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4377 if ( this.$label
) {
4378 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4381 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4382 this.setLabelContent( this.label
);
4388 * An empty string will result in the label being hidden. A string containing only whitespace will
4389 * be converted to a single ` `.
4391 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4392 * text; or null for no label
4395 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4396 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4397 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4399 if ( this.label
!== label
) {
4400 if ( this.$label
) {
4401 this.setLabelContent( label
);
4406 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4414 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
4415 * text; or null for no label
4417 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4426 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4427 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4428 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4435 * Set the content of the label.
4437 * Do not call this method until after the label element has been set by #setLabelElement.
4440 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4441 * text; or null for no label
4443 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4444 if ( typeof label
=== 'string' ) {
4445 if ( label
.match( /^\s*$/ ) ) {
4446 // Convert whitespace only string to a single non-breaking space
4447 this.$label
.html( ' ' );
4449 this.$label
.text( label
);
4451 } else if ( label
instanceof jQuery
) {
4452 this.$label
.empty().append( label
);
4454 this.$label
.empty();
4456 this.$label
.css( 'display', !label
? 'none' : '' );
4460 * Element containing an OO.ui.PopupWidget object.
4466 * @param {Object} [config] Configuration options
4467 * @cfg {Object} [popup] Configuration to pass to popup
4468 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4470 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4471 // Configuration initialization
4472 config
= config
|| {};
4475 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4476 { autoClose
: true },
4478 { $: this.$, $autoCloseIgnore
: this.$element
}
4487 * @return {OO.ui.PopupWidget} Popup widget
4489 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4494 * Element with named flags that can be added, removed, listed and checked.
4496 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4497 * the flag name. Flags are primarily useful for styling.
4503 * @param {Object} [config] Configuration options
4504 * @cfg {string|string[]} [flags] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4505 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4507 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4508 // Configuration initialization
4509 config
= config
|| {};
4513 this.$flagged
= null;
4516 this.setFlags( config
.flags
);
4517 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4524 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4525 * added/removed properties
4531 * Set the flagged element.
4533 * If an element is already set, it will be cleaned up before setting up the new element.
4535 * @param {jQuery} $flagged Element to add flags to
4537 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4538 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4539 return 'oo-ui-flaggedElement-' + flag
;
4542 if ( this.$flagged
) {
4543 this.$flagged
.removeClass( classNames
);
4546 this.$flagged
= $flagged
.addClass( classNames
);
4550 * Check if a flag is set.
4552 * @param {string} flag Name of flag
4553 * @return {boolean} Has flag
4555 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4556 return flag
in this.flags
;
4560 * Get the names of all flags set.
4562 * @return {string[]} Flag names
4564 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4565 return Object
.keys( this.flags
);
4574 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4575 var flag
, className
,
4578 classPrefix
= 'oo-ui-flaggedElement-';
4580 for ( flag
in this.flags
) {
4581 className
= classPrefix
+ flag
;
4582 changes
[flag
] = false;
4583 delete this.flags
[flag
];
4584 remove
.push( className
);
4587 if ( this.$flagged
) {
4588 this.$flagged
.removeClass( remove
.join( ' ' ) );
4591 this.updateThemeClasses();
4592 this.emit( 'flag', changes
);
4598 * Add one or more flags.
4600 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4601 * keyed by flag name containing boolean set/remove instructions.
4605 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4606 var i
, len
, flag
, className
,
4610 classPrefix
= 'oo-ui-flaggedElement-';
4612 if ( typeof flags
=== 'string' ) {
4613 className
= classPrefix
+ flags
;
4615 if ( !this.flags
[flags
] ) {
4616 this.flags
[flags
] = true;
4617 add
.push( className
);
4619 } else if ( $.isArray( flags
) ) {
4620 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4622 className
= classPrefix
+ flag
;
4624 if ( !this.flags
[flag
] ) {
4625 changes
[flag
] = true;
4626 this.flags
[flag
] = true;
4627 add
.push( className
);
4630 } else if ( OO
.isPlainObject( flags
) ) {
4631 for ( flag
in flags
) {
4632 className
= classPrefix
+ flag
;
4633 if ( flags
[flag
] ) {
4635 if ( !this.flags
[flag
] ) {
4636 changes
[flag
] = true;
4637 this.flags
[flag
] = true;
4638 add
.push( className
);
4642 if ( this.flags
[flag
] ) {
4643 changes
[flag
] = false;
4644 delete this.flags
[flag
];
4645 remove
.push( className
);
4651 if ( this.$flagged
) {
4653 .addClass( add
.join( ' ' ) )
4654 .removeClass( remove
.join( ' ' ) );
4657 this.updateThemeClasses();
4658 this.emit( 'flag', changes
);
4664 * Element with a title.
4666 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4667 * not visible on touch devices.
4673 * @param {Object} [config] Configuration options
4674 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4675 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
4676 * static property 'title' is used.
4678 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4679 // Configuration initialization
4680 config
= config
|| {};
4683 this.$titled
= null;
4687 this.setTitle( config
.title
|| this.constructor.static.title
);
4688 this.setTitledElement( config
.$titled
|| this.$element
);
4693 OO
.initClass( OO
.ui
.TitledElement
);
4695 /* Static Properties */
4702 * @property {string|Function} Title text or a function that returns text
4704 OO
.ui
.TitledElement
.static.title
= null;
4709 * Set the titled element.
4711 * If an element is already set, it will be cleaned up before setting up the new element.
4713 * @param {jQuery} $titled Element to set title on
4715 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4716 if ( this.$titled
) {
4717 this.$titled
.removeAttr( 'title' );
4720 this.$titled
= $titled
;
4722 this.$titled
.attr( 'title', this.title
);
4729 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4732 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4733 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4735 if ( this.title
!== title
) {
4736 if ( this.$titled
) {
4737 if ( title
!== null ) {
4738 this.$titled
.attr( 'title', title
);
4740 this.$titled
.removeAttr( 'title' );
4752 * @return {string} Title string
4754 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4759 * Element that can be automatically clipped to visible boundaries.
4761 * Whenever the element's natural height changes, you have to call
4762 * #clip to make sure it's still clipping correctly.
4768 * @param {Object} [config] Configuration options
4769 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4771 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4772 // Configuration initialization
4773 config
= config
|| {};
4776 this.$clippable
= null;
4777 this.clipping
= false;
4778 this.clippedHorizontally
= false;
4779 this.clippedVertically
= false;
4780 this.$clippableContainer
= null;
4781 this.$clippableScroller
= null;
4782 this.$clippableWindow
= null;
4783 this.idealWidth
= null;
4784 this.idealHeight
= null;
4785 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
4786 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
4789 this.setClippableElement( config
.$clippable
|| this.$element
);
4795 * Set clippable element.
4797 * If an element is already set, it will be cleaned up before setting up the new element.
4799 * @param {jQuery} $clippable Element to make clippable
4801 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4802 if ( this.$clippable
) {
4803 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4804 this.$clippable
.css( { width
: '', height
: '' } );
4805 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4806 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4809 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4816 * Do not turn clipping on until after the element is attached to the DOM and visible.
4818 * @param {boolean} [clipping] Enable clipping, omit to toggle
4821 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4822 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4824 if ( this.clipping
!== clipping
) {
4825 this.clipping
= clipping
;
4827 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4828 // If the clippable container is the body, we have to listen to scroll events and check
4829 // jQuery.scrollTop on the window because of browser inconsistencies
4830 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4831 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4832 this.$clippableContainer
;
4833 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4834 this.$clippableWindow
= this.$( this.getElementWindow() )
4835 .on( 'resize', this.onClippableWindowResizeHandler
);
4836 // Initial clip after visible
4839 this.$clippable
.css( { width
: '', height
: '' } );
4840 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4841 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4843 this.$clippableContainer
= null;
4844 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4845 this.$clippableScroller
= null;
4846 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4847 this.$clippableWindow
= null;
4855 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4857 * @return {boolean} Element will be clipped to the visible area
4859 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4860 return this.clipping
;
4864 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4866 * @return {boolean} Part of the element is being clipped
4868 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4869 return this.clippedHorizontally
|| this.clippedVertically
;
4873 * Check if the right of the element is being clipped by the nearest scrollable container.
4875 * @return {boolean} Part of the element is being clipped
4877 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4878 return this.clippedHorizontally
;
4882 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4884 * @return {boolean} Part of the element is being clipped
4886 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4887 return this.clippedVertically
;
4891 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
4893 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4894 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4896 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4897 this.idealWidth
= width
;
4898 this.idealHeight
= height
;
4900 if ( !this.clipping
) {
4901 // Update dimensions
4902 this.$clippable
.css( { width
: width
, height
: height
} );
4904 // While clipping, idealWidth and idealHeight are not considered
4908 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4909 * the element's natural height changes.
4911 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4912 * overlapped by, the visible area of the nearest scrollable container.
4916 OO
.ui
.ClippableElement
.prototype.clip = function () {
4917 if ( !this.clipping
) {
4918 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
4923 cOffset
= this.$clippable
.offset(),
4924 $container
= this.$clippableContainer
.is( 'body' ) ?
4925 this.$clippableWindow
: this.$clippableContainer
,
4926 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
4927 ccHeight
= $container
.innerHeight() - buffer
,
4928 ccWidth
= $container
.innerWidth() - buffer
,
4929 scrollTop
= this.$clippableScroller
.scrollTop(),
4930 scrollLeft
= this.$clippableScroller
.scrollLeft(),
4931 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
4932 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
4933 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
4934 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
4935 clipWidth
= desiredWidth
< naturalWidth
,
4936 clipHeight
= desiredHeight
< naturalHeight
;
4939 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
4941 this.$clippable
.css( 'width', this.idealWidth
|| '' );
4942 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4943 this.$clippable
.css( 'overflowX', '' );
4946 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
4948 this.$clippable
.css( 'height', this.idealHeight
|| '' );
4949 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4950 this.$clippable
.css( 'overflowY', '' );
4953 this.clippedHorizontally
= clipWidth
;
4954 this.clippedVertically
= clipHeight
;
4960 * Generic toolbar tool.
4964 * @extends OO.ui.Widget
4965 * @mixins OO.ui.IconElement
4966 * @mixins OO.ui.FlaggedElement
4969 * @param {OO.ui.ToolGroup} toolGroup
4970 * @param {Object} [config] Configuration options
4971 * @cfg {string|Function} [title] Title text or a function that returns text
4973 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4974 // Configuration initialization
4975 config
= config
|| {};
4977 // Parent constructor
4978 OO
.ui
.Tool
.super.call( this, config
);
4980 // Mixin constructors
4981 OO
.ui
.IconElement
.call( this, config
);
4982 OO
.ui
.FlaggedElement
.call( this, config
);
4985 this.toolGroup
= toolGroup
;
4986 this.toolbar
= this.toolGroup
.getToolbar();
4987 this.active
= false;
4988 this.$title
= this.$( '<span>' );
4989 this.$titleText
= this.$( '<span>' );
4990 this.$accel
= this.$( '<span>' );
4991 this.$link
= this.$( '<a>' );
4995 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
4998 this.$titleText
.addClass( 'oo-ui-tool-title-text' );
4999 this.$accel
.addClass( 'oo-ui-tool-accel' );
5001 .addClass( 'oo-ui-tool-title' )
5002 .append( this.$titleText
, this.$accel
);
5004 .addClass( 'oo-ui-tool-link' )
5005 .append( this.$icon
, this.$title
)
5006 .prop( 'tabIndex', 0 )
5007 .attr( 'role', 'button' );
5009 .data( 'oo-ui-tool', this )
5011 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5012 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5014 .append( this.$link
);
5015 this.setTitle( config
.title
|| this.constructor.static.title
);
5020 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5021 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5022 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5030 /* Static Properties */
5036 OO
.ui
.Tool
.static.tagName
= 'span';
5039 * Symbolic name of tool.
5044 * @property {string}
5046 OO
.ui
.Tool
.static.name
= '';
5054 * @property {string}
5056 OO
.ui
.Tool
.static.group
= '';
5061 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
5062 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
5063 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
5064 * appended to the title if the tool is part of a bar tool group.
5069 * @property {string|Function} Title text or a function that returns text
5071 OO
.ui
.Tool
.static.title
= '';
5074 * Tool can be automatically added to catch-all groups.
5078 * @property {boolean}
5080 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
5083 * Tool can be automatically added to named groups.
5086 * @property {boolean}
5089 OO
.ui
.Tool
.static.autoAddToGroup
= true;
5092 * Check if this tool is compatible with given data.
5096 * @param {Mixed} data Data to check
5097 * @return {boolean} Tool can be used with data
5099 OO
.ui
.Tool
.static.isCompatibleWith = function () {
5106 * Handle the toolbar state being updated.
5108 * This is an abstract method that must be overridden in a concrete subclass.
5112 OO
.ui
.Tool
.prototype.onUpdateState = function () {
5114 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
5119 * Handle the tool being selected.
5121 * This is an abstract method that must be overridden in a concrete subclass.
5125 OO
.ui
.Tool
.prototype.onSelect = function () {
5127 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
5132 * Check if the button is active.
5134 * @return {boolean} Button is active
5136 OO
.ui
.Tool
.prototype.isActive = function () {
5141 * Make the button appear active or inactive.
5143 * @param {boolean} state Make button appear active
5145 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
5146 this.active
= !!state
;
5147 if ( this.active
) {
5148 this.$element
.addClass( 'oo-ui-tool-active' );
5150 this.$element
.removeClass( 'oo-ui-tool-active' );
5155 * Get the tool title.
5157 * @param {string|Function} title Title text or a function that returns text
5160 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
5161 this.title
= OO
.ui
.resolveMsg( title
);
5167 * Get the tool title.
5169 * @return {string} Title text
5171 OO
.ui
.Tool
.prototype.getTitle = function () {
5176 * Get the tool's symbolic name.
5178 * @return {string} Symbolic name of tool
5180 OO
.ui
.Tool
.prototype.getName = function () {
5181 return this.constructor.static.name
;
5187 OO
.ui
.Tool
.prototype.updateTitle = function () {
5188 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
5189 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
5190 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
5193 this.$titleText
.text( this.title
);
5194 this.$accel
.text( accel
);
5196 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
5197 tooltipParts
.push( this.title
);
5199 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
5200 tooltipParts
.push( accel
);
5202 if ( tooltipParts
.length
) {
5203 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
5205 this.$link
.removeAttr( 'title' );
5212 OO
.ui
.Tool
.prototype.destroy = function () {
5213 this.toolbar
.disconnect( this );
5214 this.$element
.remove();
5218 * Collection of tool groups.
5221 * @extends OO.ui.Element
5222 * @mixins OO.EventEmitter
5223 * @mixins OO.ui.GroupElement
5226 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5227 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5228 * @param {Object} [config] Configuration options
5229 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5230 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5232 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5233 // Configuration initialization
5234 config
= config
|| {};
5236 // Parent constructor
5237 OO
.ui
.Toolbar
.super.call( this, config
);
5239 // Mixin constructors
5240 OO
.EventEmitter
.call( this );
5241 OO
.ui
.GroupElement
.call( this, config
);
5244 this.toolFactory
= toolFactory
;
5245 this.toolGroupFactory
= toolGroupFactory
;
5248 this.$bar
= this.$( '<div>' );
5249 this.$actions
= this.$( '<div>' );
5250 this.initialized
= false;
5254 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5255 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
5258 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5259 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
5260 if ( config
.actions
) {
5261 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
5262 this.$bar
.append( this.$actions
);
5264 this.$bar
.append( '<div style="clear:both"></div>' );
5265 if ( config
.shadow
) {
5266 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5268 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5273 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5274 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5275 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5280 * Get the tool factory.
5282 * @return {OO.ui.ToolFactory} Tool factory
5284 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5285 return this.toolFactory
;
5289 * Get the tool group factory.
5291 * @return {OO.Factory} Tool group factory
5293 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5294 return this.toolGroupFactory
;
5298 * Handles mouse down events.
5300 * @param {jQuery.Event} e Mouse down event
5302 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5303 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5304 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5305 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5311 * Sets up handles and preloads required information for the toolbar to work.
5312 * This must be called immediately after it is attached to a visible document.
5314 OO
.ui
.Toolbar
.prototype.initialize = function () {
5315 this.initialized
= true;
5321 * Tools can be specified in the following ways:
5323 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5324 * - All tools in a group: `{ group: 'group-name' }`
5325 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5327 * @param {Object.<string,Array>} groups List of tool group configurations
5328 * @param {Array|string} [groups.include] Tools to include
5329 * @param {Array|string} [groups.exclude] Tools to exclude
5330 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5331 * @param {Array|string} [groups.demote] Tools to demote to the end
5333 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5334 var i
, len
, type
, group
,
5336 defaultType
= 'bar';
5338 // Cleanup previous groups
5341 // Build out new groups
5342 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5344 if ( group
.include
=== '*' ) {
5345 // Apply defaults to catch-all groups
5346 if ( group
.type
=== undefined ) {
5347 group
.type
= 'list';
5349 if ( group
.label
=== undefined ) {
5350 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
5353 // Check type has been registered
5354 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5356 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5359 this.addItems( items
);
5363 * Remove all tools and groups from the toolbar.
5365 OO
.ui
.Toolbar
.prototype.reset = function () {
5370 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5371 this.items
[i
].destroy();
5377 * Destroys toolbar, removing event handlers and DOM elements.
5379 * Call this whenever you are done using a toolbar.
5381 OO
.ui
.Toolbar
.prototype.destroy = function () {
5383 this.$element
.remove();
5387 * Check if tool has not been used yet.
5389 * @param {string} name Symbolic name of tool
5390 * @return {boolean} Tool is available
5392 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5393 return !this.tools
[name
];
5397 * Prevent tool from being used again.
5399 * @param {OO.ui.Tool} tool Tool to reserve
5401 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5402 this.tools
[tool
.getName()] = tool
;
5406 * Allow tool to be used again.
5408 * @param {OO.ui.Tool} tool Tool to release
5410 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5411 delete this.tools
[tool
.getName()];
5415 * Get accelerator label for tool.
5417 * This is a stub that should be overridden to provide access to accelerator information.
5419 * @param {string} name Symbolic name of tool
5420 * @return {string|undefined} Tool accelerator label if available
5422 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5427 * Collection of tools.
5429 * Tools can be specified in the following ways:
5431 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5432 * - All tools in a group: `{ group: 'group-name' }`
5433 * - All tools: `'*'`
5437 * @extends OO.ui.Widget
5438 * @mixins OO.ui.GroupElement
5441 * @param {OO.ui.Toolbar} toolbar
5442 * @param {Object} [config] Configuration options
5443 * @cfg {Array|string} [include=[]] List of tools to include
5444 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5445 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5446 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5448 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5449 // Configuration initialization
5450 config
= config
|| {};
5452 // Parent constructor
5453 OO
.ui
.ToolGroup
.super.call( this, config
);
5455 // Mixin constructors
5456 OO
.ui
.GroupElement
.call( this, config
);
5459 this.toolbar
= toolbar
;
5461 this.pressed
= null;
5462 this.autoDisabled
= false;
5463 this.include
= config
.include
|| [];
5464 this.exclude
= config
.exclude
|| [];
5465 this.promote
= config
.promote
|| [];
5466 this.demote
= config
.demote
|| [];
5467 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
5471 'mousedown touchstart': this.onPointerDown
.bind( this ),
5472 'mouseup touchend': this.onPointerUp
.bind( this ),
5473 mouseover
: this.onMouseOver
.bind( this ),
5474 mouseout
: this.onMouseOut
.bind( this )
5476 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5477 this.aggregate( { disable
: 'itemDisable' } );
5478 this.connect( this, { itemDisable
: 'updateDisabled' } );
5481 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5483 .addClass( 'oo-ui-toolGroup' )
5484 .append( this.$group
);
5490 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5491 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5499 /* Static Properties */
5502 * Show labels in tooltips.
5506 * @property {boolean}
5508 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5511 * Show acceleration labels in tooltips.
5515 * @property {boolean}
5517 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5520 * Automatically disable the toolgroup when all tools are disabled
5524 * @property {boolean}
5526 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5533 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5534 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5540 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5541 var i
, item
, allDisabled
= true;
5543 if ( this.constructor.static.autoDisable
) {
5544 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5545 item
= this.items
[i
];
5546 if ( !item
.isDisabled() ) {
5547 allDisabled
= false;
5551 this.autoDisabled
= allDisabled
;
5553 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5557 * Handle mouse down events.
5559 * @param {jQuery.Event} e Mouse down event
5561 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5562 // e.which is 0 for touch events, 1 for left mouse button
5563 if ( !this.isDisabled() && e
.which
<= 1 ) {
5564 this.pressed
= this.getTargetTool( e
);
5565 if ( this.pressed
) {
5566 this.pressed
.setActive( true );
5567 this.getElementDocument().addEventListener(
5568 'mouseup', this.onCapturedMouseUpHandler
, true
5576 * Handle captured mouse up events.
5578 * @param {Event} e Mouse up event
5580 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5581 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5582 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5583 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5584 this.onPointerUp( e
);
5588 * Handle mouse up events.
5590 * @param {jQuery.Event} e Mouse up event
5592 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5593 var tool
= this.getTargetTool( e
);
5595 // e.which is 0 for touch events, 1 for left mouse button
5596 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5597 this.pressed
.onSelect();
5600 this.pressed
= null;
5605 * Handle mouse over events.
5607 * @param {jQuery.Event} e Mouse over event
5609 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5610 var tool
= this.getTargetTool( e
);
5612 if ( this.pressed
&& this.pressed
=== tool
) {
5613 this.pressed
.setActive( true );
5618 * Handle mouse out events.
5620 * @param {jQuery.Event} e Mouse out event
5622 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5623 var tool
= this.getTargetTool( e
);
5625 if ( this.pressed
&& this.pressed
=== tool
) {
5626 this.pressed
.setActive( false );
5631 * Get the closest tool to a jQuery.Event.
5633 * Only tool links are considered, which prevents other elements in the tool such as popups from
5634 * triggering tool group interactions.
5637 * @param {jQuery.Event} e
5638 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5640 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5642 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5644 if ( $item
.length
) {
5645 tool
= $item
.parent().data( 'oo-ui-tool' );
5648 return tool
&& !tool
.isDisabled() ? tool
: null;
5652 * Handle tool registry register events.
5654 * If a tool is registered after the group is created, we must repopulate the list to account for:
5656 * - a tool being added that may be included
5657 * - a tool already included being overridden
5659 * @param {string} name Symbolic name of tool
5661 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5666 * Get the toolbar this group is in.
5668 * @return {OO.ui.Toolbar} Toolbar of group
5670 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5671 return this.toolbar
;
5675 * Add and remove tools based on configuration.
5677 OO
.ui
.ToolGroup
.prototype.populate = function () {
5678 var i
, len
, name
, tool
,
5679 toolFactory
= this.toolbar
.getToolFactory(),
5683 list
= this.toolbar
.getToolFactory().getTools(
5684 this.include
, this.exclude
, this.promote
, this.demote
5687 // Build a list of needed tools
5688 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5692 toolFactory
.lookup( name
) &&
5693 // Tool is available or is already in this group
5694 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5696 tool
= this.tools
[name
];
5698 // Auto-initialize tools on first use
5699 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5702 this.toolbar
.reserveTool( tool
);
5707 // Remove tools that are no longer needed
5708 for ( name
in this.tools
) {
5709 if ( !names
[name
] ) {
5710 this.tools
[name
].destroy();
5711 this.toolbar
.releaseTool( this.tools
[name
] );
5712 remove
.push( this.tools
[name
] );
5713 delete this.tools
[name
];
5716 if ( remove
.length
) {
5717 this.removeItems( remove
);
5719 // Update emptiness state
5721 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5723 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5725 // Re-add tools (moving existing ones to new locations)
5726 this.addItems( add
);
5727 // Disabled state may depend on items
5728 this.updateDisabled();
5732 * Destroy tool group.
5734 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5738 this.toolbar
.getToolFactory().disconnect( this );
5739 for ( name
in this.tools
) {
5740 this.toolbar
.releaseTool( this.tools
[name
] );
5741 this.tools
[name
].disconnect( this ).destroy();
5742 delete this.tools
[name
];
5744 this.$element
.remove();
5748 * Dialog for showing a message.
5751 * - Registers two actions by default (safe and primary).
5752 * - Renders action widgets in the footer.
5755 * @extends OO.ui.Dialog
5758 * @param {Object} [config] Configuration options
5760 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5761 // Parent constructor
5762 OO
.ui
.MessageDialog
.super.call( this, config
);
5765 this.verticalActionLayout
= null;
5768 this.$element
.addClass( 'oo-ui-messageDialog' );
5773 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5775 /* Static Properties */
5777 OO
.ui
.MessageDialog
.static.name
= 'message';
5779 OO
.ui
.MessageDialog
.static.size
= 'small';
5781 OO
.ui
.MessageDialog
.static.verbose
= false;
5786 * A confirmation dialog's title should describe what the progressive action will do. An alert
5787 * dialog's title should describe what event occured.
5791 * @property {jQuery|string|Function|null}
5793 OO
.ui
.MessageDialog
.static.title
= null;
5796 * A confirmation dialog's message should describe the consequences of the progressive action. An
5797 * alert dialog's message should describe why the event occured.
5801 * @property {jQuery|string|Function|null}
5803 OO
.ui
.MessageDialog
.static.message
= null;
5805 OO
.ui
.MessageDialog
.static.actions
= [
5806 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5807 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5815 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5817 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5821 * Toggle action layout between vertical and horizontal.
5823 * @param {boolean} [value] Layout actions vertically, omit to toggle
5826 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5827 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5829 if ( value
!== this.verticalActionLayout
) {
5830 this.verticalActionLayout
= value
;
5832 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5833 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5842 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5844 return new OO
.ui
.Process( function () {
5845 this.close( { action
: action
} );
5848 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5854 * @param {Object} [data] Dialog opening data
5855 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5856 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5857 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5858 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5861 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5865 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5866 .next( function () {
5867 this.title
.setLabel(
5868 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5870 this.message
.setLabel(
5871 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5873 this.message
.$element
.toggleClass(
5874 'oo-ui-messageDialog-message-verbose',
5875 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5883 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5884 return Math
.round( this.text
.$element
.outerHeight( true ) );
5890 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5892 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5895 this.$actions
= this.$( '<div>' );
5896 this.container
= new OO
.ui
.PanelLayout( {
5897 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5899 this.text
= new OO
.ui
.PanelLayout( {
5900 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5902 this.message
= new OO
.ui
.LabelWidget( {
5903 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5907 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5908 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5909 this.container
.$element
.append( this.text
.$element
);
5910 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5911 this.$body
.append( this.container
.$element
);
5912 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5913 this.$foot
.append( this.$actions
);
5919 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5920 var i
, len
, other
, special
, others
;
5923 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5925 special
= this.actions
.getSpecial();
5926 others
= this.actions
.getOthers();
5927 if ( special
.safe
) {
5928 this.$actions
.append( special
.safe
.$element
);
5929 special
.safe
.toggleFramed( false );
5931 if ( others
.length
) {
5932 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5934 this.$actions
.append( other
.$element
);
5935 other
.toggleFramed( false );
5938 if ( special
.primary
) {
5939 this.$actions
.append( special
.primary
.$element
);
5940 special
.primary
.toggleFramed( false );
5944 if ( !this.isOpening() ) {
5945 this.manager
.updateWindowSize( this );
5947 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5951 * Fit action actions into columns or rows.
5953 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5955 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5957 actions
= this.actions
.get();
5960 this.toggleVerticalActionLayout( false );
5961 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5962 action
= actions
[i
];
5963 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5964 this.toggleVerticalActionLayout( true );
5971 * Navigation dialog window.
5974 * - Show and hide errors.
5975 * - Retry an action.
5978 * - Renders header with dialog title and one action widget on either side
5979 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5980 * which close the dialog).
5981 * - Displays any action widgets in the footer (none by default).
5982 * - Ability to dismiss errors.
5984 * Subclass responsibilities:
5985 * - Register a 'safe' action.
5986 * - Register a 'primary' action.
5987 * - Add content to the dialog.
5991 * @extends OO.ui.Dialog
5994 * @param {Object} [config] Configuration options
5996 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
5997 // Parent constructor
5998 OO
.ui
.ProcessDialog
.super.call( this, config
);
6001 this.$element
.addClass( 'oo-ui-processDialog' );
6006 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
6011 * Handle dismiss button click events.
6015 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
6020 * Handle retry button click events.
6022 * Hides errors and then tries again.
6024 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
6026 this.executeAction( this.currentAction
.getAction() );
6032 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
6033 if ( this.actions
.isSpecial( action
) ) {
6036 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
6042 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
6044 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
6047 this.$navigation
= this.$( '<div>' );
6048 this.$location
= this.$( '<div>' );
6049 this.$safeActions
= this.$( '<div>' );
6050 this.$primaryActions
= this.$( '<div>' );
6051 this.$otherActions
= this.$( '<div>' );
6052 this.dismissButton
= new OO
.ui
.ButtonWidget( {
6054 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
6056 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
6057 this.$errors
= this.$( '<div>' );
6058 this.$errorsTitle
= this.$( '<div>' );
6061 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
6062 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
6065 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
6067 .append( this.title
.$element
)
6068 .addClass( 'oo-ui-processDialog-location' );
6069 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
6070 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
6071 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
6073 .addClass( 'oo-ui-processDialog-errors-title' )
6074 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
6076 .addClass( 'oo-ui-processDialog-errors' )
6077 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
6079 .addClass( 'oo-ui-processDialog-content' )
6080 .append( this.$errors
);
6082 .addClass( 'oo-ui-processDialog-navigation' )
6083 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
6084 this.$head
.append( this.$navigation
);
6085 this.$foot
.append( this.$otherActions
);
6091 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
6092 var i
, len
, other
, special
, others
;
6095 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
6097 special
= this.actions
.getSpecial();
6098 others
= this.actions
.getOthers();
6099 if ( special
.primary
) {
6100 this.$primaryActions
.append( special
.primary
.$element
);
6101 special
.primary
.toggleFramed( true );
6103 if ( others
.length
) {
6104 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6106 this.$otherActions
.append( other
.$element
);
6107 other
.toggleFramed( true );
6110 if ( special
.safe
) {
6111 this.$safeActions
.append( special
.safe
.$element
);
6112 special
.safe
.toggleFramed( true );
6116 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6122 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
6123 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
6124 .fail( this.showErrors
.bind( this ) );
6128 * Fit label between actions.
6132 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
6133 var width
= Math
.max(
6134 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
6135 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
6137 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
6143 * Handle errors that occured durring accept or reject processes.
6145 * @param {OO.ui.Error[]} errors Errors to be handled
6147 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
6153 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
6154 if ( !errors
[i
].isRecoverable() ) {
6155 recoverable
= false;
6157 if ( errors
[i
].isWarning() ) {
6160 $item
= this.$( '<div>' )
6161 .addClass( 'oo-ui-processDialog-error' )
6162 .append( errors
[i
].getMessage() );
6163 items
.push( $item
[0] );
6165 this.$errorItems
= this.$( items
);
6166 if ( recoverable
) {
6167 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
6169 this.currentAction
.setDisabled( true );
6172 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
6174 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
6176 this.retryButton
.toggle( recoverable
);
6177 this.$errorsTitle
.after( this.$errorItems
);
6178 this.$errors
.show().scrollTop( 0 );
6184 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
6185 this.$errors
.hide();
6186 this.$errorItems
.remove();
6187 this.$errorItems
= null;
6191 * Layout containing a series of pages.
6194 * @extends OO.ui.Layout
6197 * @param {Object} [config] Configuration options
6198 * @cfg {boolean} [continuous=false] Show all pages, one after another
6199 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
6200 * @cfg {boolean} [outlined=false] Show an outline
6201 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
6203 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
6204 // Configuration initialization
6205 config
= config
|| {};
6207 // Parent constructor
6208 OO
.ui
.BookletLayout
.super.call( this, config
);
6211 this.currentPageName
= null;
6213 this.ignoreFocus
= false;
6214 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
6215 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
6216 this.outlineVisible
= false;
6217 this.outlined
= !!config
.outlined
;
6218 if ( this.outlined
) {
6219 this.editable
= !!config
.editable
;
6220 this.outlineControlsWidget
= null;
6221 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
6222 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
6223 this.gridLayout
= new OO
.ui
.GridLayout(
6224 [ this.outlinePanel
, this.stackLayout
],
6225 { $: this.$, widths
: [ 1, 2 ] }
6227 this.outlineVisible
= true;
6228 if ( this.editable
) {
6229 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6230 this.outlineWidget
, { $: this.$ }
6236 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6237 if ( this.outlined
) {
6238 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
6240 if ( this.autoFocus
) {
6241 // Event 'focus' does not bubble, but 'focusin' does
6242 this.stackLayout
.onDOMEvent( 'focusin', this.onStackLayoutFocus
.bind( this ) );
6246 this.$element
.addClass( 'oo-ui-bookletLayout' );
6247 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6248 if ( this.outlined
) {
6249 this.outlinePanel
.$element
6250 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6251 .append( this.outlineWidget
.$element
);
6252 if ( this.editable
) {
6253 this.outlinePanel
.$element
6254 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6255 .append( this.outlineControlsWidget
.$element
);
6257 this.$element
.append( this.gridLayout
.$element
);
6259 this.$element
.append( this.stackLayout
.$element
);
6265 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6271 * @param {OO.ui.PageLayout} page Current page
6276 * @param {OO.ui.PageLayout[]} page Added pages
6277 * @param {number} index Index pages were added at
6282 * @param {OO.ui.PageLayout[]} pages Removed pages
6288 * Handle stack layout focus.
6290 * @param {jQuery.Event} e Focusin event
6292 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6295 // Find the page that an element was focused within
6296 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6297 for ( name
in this.pages
) {
6298 // Check for page match, exclude current page to find only page changes
6299 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6300 this.setPage( name
);
6307 * Handle stack layout set events.
6309 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6311 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6312 var $input
, layout
= this;
6314 page
.scrollElementIntoView( { complete: function () {
6315 if ( layout
.autoFocus
) {
6316 // Set focus to the first input if nothing on the page is focused yet
6317 if ( !page
.$element
.find( ':focus' ).length
) {
6318 $input
= page
.$element
.find( ':input:first' );
6319 if ( $input
.length
) {
6329 * Handle outline widget select events.
6331 * @param {OO.ui.OptionWidget|null} item Selected item
6333 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
6335 this.setPage( item
.getData() );
6340 * Check if booklet has an outline.
6344 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6345 return this.outlined
;
6349 * Check if booklet has editing controls.
6353 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6354 return this.editable
;
6358 * Check if booklet has a visible outline.
6362 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6363 return this.outlined
&& this.outlineVisible
;
6367 * Hide or show the outline.
6369 * @param {boolean} [show] Show outline, omit to invert current state
6372 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6373 if ( this.outlined
) {
6374 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6375 this.outlineVisible
= show
;
6376 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6383 * Get the outline widget.
6385 * @param {OO.ui.PageLayout} page Page to be selected
6386 * @return {OO.ui.PageLayout|null} Closest page to another
6388 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6389 var next
, prev
, level
,
6390 pages
= this.stackLayout
.getItems(),
6391 index
= $.inArray( page
, pages
);
6393 if ( index
!== -1 ) {
6394 next
= pages
[index
+ 1];
6395 prev
= pages
[index
- 1];
6396 // Prefer adjacent pages at the same level
6397 if ( this.outlined
) {
6398 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
6401 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
6407 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
6413 return prev
|| next
|| null;
6417 * Get the outline widget.
6419 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
6421 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6422 return this.outlineWidget
;
6426 * Get the outline controls widget. If the outline is not editable, null is returned.
6428 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6430 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6431 return this.outlineControlsWidget
;
6435 * Get a page by name.
6437 * @param {string} name Symbolic name of page
6438 * @return {OO.ui.PageLayout|undefined} Page, if found
6440 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6441 return this.pages
[name
];
6445 * Get the current page name.
6447 * @return {string|null} Current page name
6449 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6450 return this.currentPageName
;
6454 * Add a page to the layout.
6456 * When pages are added with the same names as existing pages, the existing pages will be
6457 * automatically removed before the new pages are added.
6459 * @param {OO.ui.PageLayout[]} pages Pages to add
6460 * @param {number} index Index to insert pages after
6464 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6465 var i
, len
, name
, page
, item
, currentIndex
,
6466 stackLayoutPages
= this.stackLayout
.getItems(),
6470 // Remove pages with same names
6471 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6473 name
= page
.getName();
6475 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6476 // Correct the insertion index
6477 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6478 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6481 remove
.push( this.pages
[name
] );
6484 if ( remove
.length
) {
6485 this.removePages( remove
);
6489 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6491 name
= page
.getName();
6492 this.pages
[page
.getName()] = page
;
6493 if ( this.outlined
) {
6494 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
6495 page
.setOutlineItem( item
);
6500 if ( this.outlined
&& items
.length
) {
6501 this.outlineWidget
.addItems( items
, index
);
6502 this.updateOutlineWidget();
6504 this.stackLayout
.addItems( pages
, index
);
6505 this.emit( 'add', pages
, index
);
6511 * Remove a page from the layout.
6516 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6517 var i
, len
, name
, page
,
6520 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6522 name
= page
.getName();
6523 delete this.pages
[name
];
6524 if ( this.outlined
) {
6525 items
.push( this.outlineWidget
.getItemFromData( name
) );
6526 page
.setOutlineItem( null );
6529 if ( this.outlined
&& items
.length
) {
6530 this.outlineWidget
.removeItems( items
);
6531 this.updateOutlineWidget();
6533 this.stackLayout
.removeItems( pages
);
6534 this.emit( 'remove', pages
);
6540 * Clear all pages from the layout.
6545 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6547 pages
= this.stackLayout
.getItems();
6550 this.currentPageName
= null;
6551 if ( this.outlined
) {
6552 this.outlineWidget
.clearItems();
6553 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6554 pages
[i
].setOutlineItem( null );
6557 this.stackLayout
.clearItems();
6559 this.emit( 'remove', pages
);
6565 * Set the current page by name.
6568 * @param {string} name Symbolic name of page
6570 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6573 page
= this.pages
[name
];
6575 if ( name
!== this.currentPageName
) {
6576 if ( this.outlined
) {
6577 selectedItem
= this.outlineWidget
.getSelectedItem();
6578 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6579 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
6583 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6584 this.pages
[this.currentPageName
].setActive( false );
6585 // Blur anything focused if the next page doesn't have anything focusable - this
6586 // is not needed if the next page has something focusable because once it is focused
6587 // this blur happens automatically
6588 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6589 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6590 if ( $focused
.length
) {
6595 this.currentPageName
= name
;
6596 this.stackLayout
.setItem( page
);
6597 page
.setActive( true );
6598 this.emit( 'set', page
);
6604 * Call this after adding or removing items from the OutlineWidget.
6608 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
6609 // Auto-select first item when nothing is selected anymore
6610 if ( !this.outlineWidget
.getSelectedItem() ) {
6611 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
6618 * Layout made of a field and optional label.
6620 * Available label alignment modes include:
6621 * - left: Label is before the field and aligned away from it, best for when the user will be
6622 * scanning for a specific label in a form with many fields
6623 * - right: Label is before the field and aligned toward it, best for forms the user is very
6624 * familiar with and will tab through field checking quickly to verify which field they are in
6625 * - top: Label is before the field and above it, best for when the user will need to fill out all
6626 * fields from top to bottom in a form with few fields
6627 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6628 * checkboxes or radio buttons
6631 * @extends OO.ui.Layout
6632 * @mixins OO.ui.LabelElement
6635 * @param {OO.ui.Widget} fieldWidget Field widget
6636 * @param {Object} [config] Configuration options
6637 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6638 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6640 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
6641 // Configuration initialization
6642 config
= $.extend( { align
: 'left' }, config
);
6644 // Parent constructor
6645 OO
.ui
.FieldLayout
.super.call( this, config
);
6647 // Mixin constructors
6648 OO
.ui
.LabelElement
.call( this, config
);
6651 this.$field
= this.$( '<div>' );
6652 this.fieldWidget
= fieldWidget
;
6654 if ( config
.help
) {
6655 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6657 classes
: [ 'oo-ui-fieldLayout-help' ],
6662 this.popupButtonWidget
.getPopup().$body
.append(
6664 .text( config
.help
)
6665 .addClass( 'oo-ui-fieldLayout-help-content' )
6667 this.$help
= this.popupButtonWidget
.$element
;
6669 this.$help
= this.$( [] );
6673 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
6674 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
6676 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
6679 this.$element
.addClass( 'oo-ui-fieldLayout' );
6681 .addClass( 'oo-ui-fieldLayout-field' )
6682 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
6683 .append( this.fieldWidget
.$element
);
6684 this.setAlignment( config
.align
);
6689 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6690 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6692 /* Static Properties */
6694 OO
.ui
.FieldLayout
.static.tagName
= 'label';
6699 * Handle field disable events.
6701 * @param {boolean} value Field is disabled
6703 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6704 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6708 * Handle label mouse click events.
6710 * @param {jQuery.Event} e Mouse click event
6712 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6713 this.fieldWidget
.simulateLabelClick();
6720 * @return {OO.ui.Widget} Field widget
6722 OO
.ui
.FieldLayout
.prototype.getField = function () {
6723 return this.fieldWidget
;
6727 * Set the field alignment mode.
6730 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6733 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6734 if ( value
!== this.align
) {
6735 // Default to 'left'
6736 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6740 if ( value
=== 'inline' ) {
6741 this.$element
.append( this.$field
, this.$label
, this.$help
);
6743 this.$element
.append( this.$help
, this.$label
, this.$field
);
6745 // Set classes. The following classes can be used here:
6746 // * oo-ui-fieldLayout-align-left
6747 // * oo-ui-fieldLayout-align-right
6748 // * oo-ui-fieldLayout-align-top
6749 // * oo-ui-fieldLayout-align-inline
6751 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6753 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
6761 * Layout made of a fieldset and optional legend.
6763 * Just add OO.ui.FieldLayout items.
6766 * @extends OO.ui.Layout
6767 * @mixins OO.ui.IconElement
6768 * @mixins OO.ui.LabelElement
6769 * @mixins OO.ui.GroupElement
6772 * @param {Object} [config] Configuration options
6773 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6775 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6776 // Configuration initialization
6777 config
= config
|| {};
6779 // Parent constructor
6780 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6782 // Mixin constructors
6783 OO
.ui
.IconElement
.call( this, config
);
6784 OO
.ui
.LabelElement
.call( this, config
);
6785 OO
.ui
.GroupElement
.call( this, config
);
6789 .addClass( 'oo-ui-fieldsetLayout' )
6790 .prepend( this.$icon
, this.$label
, this.$group
);
6791 if ( $.isArray( config
.items
) ) {
6792 this.addItems( config
.items
);
6798 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6799 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6800 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6801 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6804 * Layout with an HTML form.
6807 * @extends OO.ui.Layout
6810 * @param {Object} [config] Configuration options
6811 * @cfg {string} [method] HTML form `method` attribute
6812 * @cfg {string} [action] HTML form `action` attribute
6813 * @cfg {string} [enctype] HTML form `enctype` attribute
6815 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6816 // Configuration initialization
6817 config
= config
|| {};
6819 // Parent constructor
6820 OO
.ui
.FormLayout
.super.call( this, config
);
6823 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
6827 .addClass( 'oo-ui-formLayout' )
6829 method
: config
.method
,
6830 action
: config
.action
,
6831 enctype
: config
.enctype
6837 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6845 /* Static Properties */
6847 OO
.ui
.FormLayout
.static.tagName
= 'form';
6852 * Handle form submit events.
6854 * @param {jQuery.Event} e Submit event
6857 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6858 this.emit( 'submit' );
6863 * Layout made of proportionally sized columns and rows.
6866 * @extends OO.ui.Layout
6869 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6870 * @param {Object} [config] Configuration options
6871 * @cfg {number[]} [widths] Widths of columns as ratios
6872 * @cfg {number[]} [heights] Heights of rows as ratios
6874 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6877 // Configuration initialization
6878 config
= config
|| {};
6880 // Parent constructor
6881 OO
.ui
.GridLayout
.super.call( this, config
);
6889 this.$element
.addClass( 'oo-ui-gridLayout' );
6890 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6891 this.panels
.push( panels
[i
] );
6892 this.$element
.append( panels
[i
].$element
);
6894 if ( config
.widths
|| config
.heights
) {
6895 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6897 // Arrange in columns by default
6898 widths
= this.panels
.map( function () { return 1; } );
6899 this.layout( widths
, [ 1 ] );
6905 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6920 * Set grid dimensions.
6922 * @param {number[]} widths Widths of columns as ratios
6923 * @param {number[]} heights Heights of rows as ratios
6925 * @throws {Error} If grid is not large enough to fit all panels
6927 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6931 cols
= widths
.length
,
6932 rows
= heights
.length
;
6934 // Verify grid is big enough to fit panels
6935 if ( cols
* rows
< this.panels
.length
) {
6936 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6939 // Sum up denominators
6940 for ( x
= 0; x
< cols
; x
++ ) {
6943 for ( y
= 0; y
< rows
; y
++ ) {
6949 for ( x
= 0; x
< cols
; x
++ ) {
6950 this.widths
[x
] = widths
[x
] / xd
;
6952 for ( y
= 0; y
< rows
; y
++ ) {
6953 this.heights
[y
] = heights
[y
] / yd
;
6957 this.emit( 'layout' );
6961 * Update panel positions and sizes.
6965 OO
.ui
.GridLayout
.prototype.update = function () {
6966 var x
, y
, panel
, width
, height
, dimensions
,
6970 cols
= this.widths
.length
,
6971 rows
= this.heights
.length
;
6973 for ( y
= 0; y
< rows
; y
++ ) {
6974 height
= this.heights
[y
];
6975 for ( x
= 0; x
< cols
; x
++ ) {
6976 width
= this.widths
[x
];
6977 panel
= this.panels
[i
];
6979 width
: Math
.round( width
* 100 ) + '%',
6980 height
: Math
.round( height
* 100 ) + '%',
6981 top
: Math
.round( top
* 100 ) + '%'
6984 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6985 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6987 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6989 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6990 if ( width
=== 0 || height
=== 0 ) {
6991 dimensions
.visibility
= 'hidden';
6993 dimensions
.visibility
= '';
6995 panel
.$element
.css( dimensions
);
7003 this.emit( 'update' );
7007 * Get a panel at a given position.
7009 * The x and y position is affected by the current grid layout.
7011 * @param {number} x Horizontal position
7012 * @param {number} y Vertical position
7013 * @return {OO.ui.PanelLayout} The panel at the given postion
7015 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
7016 return this.panels
[ ( x
* this.widths
.length
) + y
];
7020 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
7023 * @extends OO.ui.Layout
7026 * @param {Object} [config] Configuration options
7027 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
7028 * @cfg {boolean} [padded=false] Pad the content from the edges
7029 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
7031 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
7032 // Configuration initialization
7033 config
= $.extend( {
7039 // Parent constructor
7040 OO
.ui
.PanelLayout
.super.call( this, config
);
7043 this.$element
.addClass( 'oo-ui-panelLayout' );
7044 if ( config
.scrollable
) {
7045 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
7047 if ( config
.padded
) {
7048 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
7050 if ( config
.expanded
) {
7051 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
7057 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
7060 * Page within an booklet layout.
7063 * @extends OO.ui.PanelLayout
7066 * @param {string} name Unique symbolic name of page
7067 * @param {Object} [config] Configuration options
7068 * @param {string} [outlineItem] Outline item widget
7070 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
7071 // Configuration initialization
7072 config
= $.extend( { scrollable
: true }, config
);
7074 // Parent constructor
7075 OO
.ui
.PageLayout
.super.call( this, config
);
7079 this.outlineItem
= config
.outlineItem
|| null;
7080 this.active
= false;
7083 this.$element
.addClass( 'oo-ui-pageLayout' );
7088 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
7094 * @param {boolean} active Page is active
7102 * @return {string} Symbolic name of page
7104 OO
.ui
.PageLayout
.prototype.getName = function () {
7109 * Check if page is active.
7111 * @return {boolean} Page is active
7113 OO
.ui
.PageLayout
.prototype.isActive = function () {
7120 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
7122 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
7123 return this.outlineItem
;
7129 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
7130 * outline item as desired; this method is called for setting (with an object) and unsetting
7131 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
7132 * operating on null instead of an OO.ui.OutlineItemWidget object.
7134 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
7137 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
7138 this.outlineItem
= outlineItem
|| null;
7139 if ( outlineItem
) {
7140 this.setupOutlineItem();
7146 * Setup outline item.
7148 * @localdoc Subclasses should override this method to adjust the outline item as desired.
7150 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
7153 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
7158 * Set page active state.
7160 * @param {boolean} Page is active
7163 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
7166 if ( active
!== this.active
) {
7167 this.active
= active
;
7168 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
7169 this.emit( 'active', this.active
);
7174 * Layout containing a series of mutually exclusive pages.
7177 * @extends OO.ui.PanelLayout
7178 * @mixins OO.ui.GroupElement
7181 * @param {Object} [config] Configuration options
7182 * @cfg {boolean} [continuous=false] Show all pages, one after another
7183 * @cfg {string} [icon=''] Symbolic icon name
7184 * @cfg {OO.ui.Layout[]} [items] Layouts to add
7186 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
7187 // Configuration initialization
7188 config
= $.extend( { scrollable
: true }, config
);
7190 // Parent constructor
7191 OO
.ui
.StackLayout
.super.call( this, config
);
7193 // Mixin constructors
7194 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7197 this.currentItem
= null;
7198 this.continuous
= !!config
.continuous
;
7201 this.$element
.addClass( 'oo-ui-stackLayout' );
7202 if ( this.continuous
) {
7203 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
7205 if ( $.isArray( config
.items
) ) {
7206 this.addItems( config
.items
);
7212 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
7213 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
7219 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
7225 * Get the current item.
7227 * @return {OO.ui.Layout|null}
7229 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
7230 return this.currentItem
;
7234 * Unset the current item.
7237 * @param {OO.ui.StackLayout} layout
7240 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7241 var prevItem
= this.currentItem
;
7242 if ( prevItem
=== null ) {
7246 this.currentItem
= null;
7247 this.emit( 'set', null );
7253 * Adding an existing item (by value) will move it.
7255 * @param {OO.ui.Layout[]} items Items to add
7256 * @param {number} [index] Index to insert items after
7259 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7261 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7263 if ( !this.currentItem
&& items
.length
) {
7264 this.setItem( items
[0] );
7273 * Items will be detached, not removed, so they can be used later.
7275 * @param {OO.ui.Layout[]} items Items to remove
7279 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7281 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7283 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7284 if ( this.items
.length
) {
7285 this.setItem( this.items
[0] );
7287 this.unsetCurrentItem();
7297 * Items will be detached, not removed, so they can be used later.
7302 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7303 this.unsetCurrentItem();
7304 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7312 * Any currently shown item will be hidden.
7314 * FIXME: If the passed item to show has not been added in the items list, then
7315 * this method drops it and unsets the current item.
7317 * @param {OO.ui.Layout} item Item to show
7321 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7324 if ( item
!== this.currentItem
) {
7325 if ( !this.continuous
) {
7326 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7327 this.items
[i
].$element
.css( 'display', '' );
7330 if ( $.inArray( item
, this.items
) !== -1 ) {
7331 if ( !this.continuous
) {
7332 item
.$element
.css( 'display', 'block' );
7334 this.currentItem
= item
;
7335 this.emit( 'set', item
);
7337 this.unsetCurrentItem();
7345 * Horizontal bar layout of tools as icon buttons.
7348 * @extends OO.ui.ToolGroup
7351 * @param {OO.ui.Toolbar} toolbar
7352 * @param {Object} [config] Configuration options
7354 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7355 // Parent constructor
7356 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7359 this.$element
.addClass( 'oo-ui-barToolGroup' );
7364 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7366 /* Static Properties */
7368 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7370 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7372 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7375 * Popup list of tools with an icon and optional label.
7379 * @extends OO.ui.ToolGroup
7380 * @mixins OO.ui.IconElement
7381 * @mixins OO.ui.IndicatorElement
7382 * @mixins OO.ui.LabelElement
7383 * @mixins OO.ui.TitledElement
7384 * @mixins OO.ui.ClippableElement
7387 * @param {OO.ui.Toolbar} toolbar
7388 * @param {Object} [config] Configuration options
7389 * @cfg {string} [header] Text to display at the top of the pop-up
7391 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7392 // Configuration initialization
7393 config
= config
|| {};
7395 // Parent constructor
7396 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7398 // Mixin constructors
7399 OO
.ui
.IconElement
.call( this, config
);
7400 OO
.ui
.IndicatorElement
.call( this, config
);
7401 OO
.ui
.LabelElement
.call( this, config
);
7402 OO
.ui
.TitledElement
.call( this, config
);
7403 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7406 this.active
= false;
7407 this.dragging
= false;
7408 this.onBlurHandler
= this.onBlur
.bind( this );
7409 this.$handle
= this.$( '<span>' );
7413 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
7414 'mouseup touchend': this.onHandlePointerUp
.bind( this )
7419 .addClass( 'oo-ui-popupToolGroup-handle' )
7420 .append( this.$icon
, this.$label
, this.$indicator
);
7421 // If the pop-up should have a header, add it to the top of the toolGroup.
7422 // Note: If this feature is useful for other widgets, we could abstract it into an
7423 // OO.ui.HeaderedElement mixin constructor.
7424 if ( config
.header
!== undefined ) {
7426 .prepend( this.$( '<span>' )
7427 .addClass( 'oo-ui-popupToolGroup-header' )
7428 .text( config
.header
)
7432 .addClass( 'oo-ui-popupToolGroup' )
7433 .prepend( this.$handle
);
7438 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7439 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7440 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7441 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7442 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7443 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7445 /* Static Properties */
7452 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7454 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7456 if ( this.isDisabled() && this.isElementAttached() ) {
7457 this.setActive( false );
7462 * Handle focus being lost.
7464 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7466 * @param {jQuery.Event} e Mouse up event
7468 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7469 // Only deactivate when clicking outside the dropdown element
7470 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7471 this.setActive( false );
7478 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7479 // e.which is 0 for touch events, 1 for left mouse button
7480 if ( !this.isDisabled() && e
.which
<= 1 ) {
7481 this.setActive( false );
7483 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7487 * Handle mouse up events.
7489 * @param {jQuery.Event} e Mouse up event
7491 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7496 * Handle mouse down events.
7498 * @param {jQuery.Event} e Mouse down event
7500 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7501 // e.which is 0 for touch events, 1 for left mouse button
7502 if ( !this.isDisabled() && e
.which
<= 1 ) {
7503 this.setActive( !this.active
);
7509 * Switch into active mode.
7511 * When active, mouseup events anywhere in the document will trigger deactivation.
7513 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7515 if ( this.active
!== value
) {
7516 this.active
= value
;
7518 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7520 // Try anchoring the popup to the left first
7521 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7522 this.toggleClipping( true );
7523 if ( this.isClippedHorizontally() ) {
7524 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7525 this.toggleClipping( false );
7527 .removeClass( 'oo-ui-popupToolGroup-left' )
7528 .addClass( 'oo-ui-popupToolGroup-right' );
7529 this.toggleClipping( true );
7532 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7533 this.$element
.removeClass(
7534 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7536 this.toggleClipping( false );
7542 * Drop down list layout of tools as labeled icon buttons.
7544 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
7545 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
7546 * may want to use the 'promote' and 'demote' configuration options to achieve this.
7549 * @extends OO.ui.PopupToolGroup
7552 * @param {OO.ui.Toolbar} toolbar
7553 * @param {Object} [config] Configuration options
7554 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
7556 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
7557 * allowed to be collapsed.
7558 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
7560 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7561 // Configuration intialization
7562 config
= config
|| {};
7564 // Properties (must be set before parent constructor, which calls #populate)
7565 this.allowCollapse
= config
.allowCollapse
;
7566 this.forceExpand
= config
.forceExpand
;
7567 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
7568 this.collapsibleTools
= [];
7570 // Parent constructor
7571 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7574 this.$element
.addClass( 'oo-ui-listToolGroup' );
7579 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7581 /* Static Properties */
7583 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7585 OO
.ui
.ListToolGroup
.static.name
= 'list';
7592 OO
.ui
.ListToolGroup
.prototype.populate = function () {
7593 var i
, len
, allowCollapse
= [];
7595 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
7597 // Update the list of collapsible tools
7598 if ( this.allowCollapse
!== undefined ) {
7599 allowCollapse
= this.allowCollapse
;
7600 } else if ( this.forceExpand
!== undefined ) {
7601 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
7604 this.collapsibleTools
= [];
7605 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
7606 if ( this.tools
[ allowCollapse
[i
] ] !== undefined ) {
7607 this.collapsibleTools
.push( this.tools
[ allowCollapse
[i
] ] );
7611 // Keep at the end, even when tools are added
7612 this.$group
.append( this.getExpandCollapseTool().$element
);
7614 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
7616 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
7617 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
7618 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
7619 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
7620 this.getExpandCollapseTool().$element
.css( 'display', 'inline-block' );
7623 this.updateCollapsibleState();
7626 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
7627 if ( this.expandCollapseTool
=== undefined ) {
7628 var ExpandCollapseTool = function () {
7629 ExpandCollapseTool
.super.apply( this, arguments
);
7632 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
7634 ExpandCollapseTool
.prototype.onSelect = function () {
7635 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
7636 this.toolGroup
.updateCollapsibleState();
7637 this.setActive( false );
7639 ExpandCollapseTool
.prototype.onUpdateState = function () {
7640 // Do nothing. Tool interface requires an implementation of this function.
7643 ExpandCollapseTool
.static.name
= 'more-fewer';
7645 this.expandCollapseTool
= new ExpandCollapseTool( this );
7647 return this.expandCollapseTool
;
7653 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
7654 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7656 // Do not close the popup when the user wants to show more/fewer tools
7657 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
7658 // Prevent the popup list from being hidden
7659 this.setActive( true );
7665 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
7668 this.getExpandCollapseTool()
7669 .setIcon( this.expanded
? 'collapse' : 'expand' )
7670 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
7672 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
7673 this.collapsibleTools
[i
].toggle( this.expanded
);
7678 * Drop down menu layout of tools as selectable menu items.
7681 * @extends OO.ui.PopupToolGroup
7684 * @param {OO.ui.Toolbar} toolbar
7685 * @param {Object} [config] Configuration options
7687 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7688 // Configuration initialization
7689 config
= config
|| {};
7691 // Parent constructor
7692 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7695 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7698 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7703 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7705 /* Static Properties */
7707 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7709 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7714 * Handle the toolbar state being updated.
7716 * When the state changes, the title of each active item in the menu will be joined together and
7717 * used as a label for the group. The label will be empty if none of the items are active.
7719 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7723 for ( name
in this.tools
) {
7724 if ( this.tools
[name
].isActive() ) {
7725 labelTexts
.push( this.tools
[name
].getTitle() );
7729 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7733 * Tool that shows a popup when selected.
7737 * @extends OO.ui.Tool
7738 * @mixins OO.ui.PopupElement
7741 * @param {OO.ui.Toolbar} toolbar
7742 * @param {Object} [config] Configuration options
7744 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7745 // Parent constructor
7746 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7748 // Mixin constructors
7749 OO
.ui
.PopupElement
.call( this, config
);
7753 .addClass( 'oo-ui-popupTool' )
7754 .append( this.popup
.$element
);
7759 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7760 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7765 * Handle the tool being selected.
7769 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7770 if ( !this.isDisabled() ) {
7771 this.popup
.toggle();
7773 this.setActive( false );
7778 * Handle the toolbar state being updated.
7782 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7783 this.setActive( false );
7787 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7789 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7793 * @extends OO.ui.GroupElement
7796 * @param {Object} [config] Configuration options
7798 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7799 // Parent constructor
7800 OO
.ui
.GroupWidget
.super.call( this, config
);
7805 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7810 * Set the disabled state of the widget.
7812 * This will also update the disabled state of child widgets.
7814 * @param {boolean} disabled Disable widget
7817 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7821 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7822 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7824 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7826 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7827 this.items
[i
].updateDisabled();
7835 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7837 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7838 * allows bidrectional communication.
7840 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7847 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7854 * Check if widget is disabled.
7856 * Checks parent if present, making disabled state inheritable.
7858 * @return {boolean} Widget is disabled
7860 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7861 return this.disabled
||
7862 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7866 * Set group element is in.
7868 * @param {OO.ui.GroupElement|null} group Group element, null if none
7871 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7873 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7874 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7876 // Initialize item disabled states
7877 this.updateDisabled();
7883 * Mixin that adds a menu showing suggested values for a text input.
7885 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7887 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
7888 * be aware that this will cause new suggestions to be looked up for the new value. If this is
7889 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
7895 * @param {OO.ui.TextInputWidget} input Input widget
7896 * @param {Object} [config] Configuration options
7897 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
7898 * @cfg {jQuery} [$container=input.$element] Element to render menu under
7900 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7901 // Configuration initialization
7902 config
= config
|| {};
7905 this.lookupInput
= input
;
7906 this.$overlay
= config
.$overlay
|| this.$element
;
7907 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7908 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7909 input
: this.lookupInput
,
7910 $container
: config
.$container
7912 this.lookupCache
= {};
7913 this.lookupQuery
= null;
7914 this.lookupRequest
= null;
7915 this.lookupsDisabled
= false;
7916 this.lookupInputFocused
= false;
7919 this.lookupInput
.$input
.on( {
7920 focus
: this.onLookupInputFocus
.bind( this ),
7921 blur
: this.onLookupInputBlur
.bind( this ),
7922 mousedown
: this.onLookupInputMouseDown
.bind( this )
7924 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7925 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
7928 this.$element
.addClass( 'oo-ui-lookupWidget' );
7929 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7930 this.$overlay
.append( this.lookupMenu
.$element
);
7936 * Handle input focus event.
7938 * @param {jQuery.Event} e Input focus event
7940 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7941 this.lookupInputFocused
= true;
7942 this.populateLookupMenu();
7946 * Handle input blur event.
7948 * @param {jQuery.Event} e Input blur event
7950 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7951 this.closeLookupMenu();
7952 this.lookupInputFocused
= false;
7956 * Handle input mouse down event.
7958 * @param {jQuery.Event} e Input mouse down event
7960 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7961 // Only open the menu if the input was already focused.
7962 // This way we allow the user to open the menu again after closing it with Esc
7963 // by clicking in the input. Opening (and populating) the menu when initially
7964 // clicking into the input is handled by the focus handler.
7965 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
7966 this.populateLookupMenu();
7971 * Handle input change event.
7973 * @param {string} value New input value
7975 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7976 if ( this.lookupInputFocused
) {
7977 this.populateLookupMenu();
7982 * Handle the lookup menu being shown/hidden.
7983 * @param {boolean} visible Whether the lookup menu is now visible.
7985 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
7987 // When the menu is hidden, abort any active request and clear the menu.
7988 // This has to be done here in addition to closeLookupMenu(), because
7989 // MenuWidget will close itself when the user presses Esc.
7990 this.abortLookupRequest();
7991 this.lookupMenu
.clearItems();
7998 * @return {OO.ui.TextInputMenuWidget}
8000 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
8001 return this.lookupMenu
;
8005 * Disable or re-enable lookups.
8007 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
8009 * @param {boolean} disabled Disable lookups
8011 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
8012 this.lookupsDisabled
= !!disabled
;
8016 * Open the menu. If there are no entries in the menu, this does nothing.
8020 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
8021 if ( !this.lookupMenu
.isEmpty() ) {
8022 this.lookupMenu
.toggle( true );
8028 * Close the menu, empty it, and abort any pending request.
8032 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
8033 this.lookupMenu
.toggle( false );
8034 this.abortLookupRequest();
8035 this.lookupMenu
.clearItems();
8040 * Request menu items based on the input's current value, and when they arrive,
8041 * populate the menu with these items and show the menu.
8043 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
8047 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
8049 value
= this.lookupInput
.getValue();
8051 if ( this.lookupsDisabled
) {
8055 // If the input is empty, clear the menu
8056 if ( value
=== '' ) {
8057 this.closeLookupMenu();
8058 // Skip population if there is already a request pending for the current value
8059 } else if ( value
!== this.lookupQuery
) {
8060 this.getLookupMenuItems()
8061 .done( function ( items
) {
8062 widget
.lookupMenu
.clearItems();
8063 if ( items
.length
) {
8067 widget
.initializeLookupMenuSelection();
8069 widget
.lookupMenu
.toggle( false );
8072 .fail( function () {
8073 widget
.lookupMenu
.clearItems();
8081 * Select and highlight the first selectable item in the menu.
8085 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
8086 if ( !this.lookupMenu
.getSelectedItem() ) {
8087 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
8089 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
8093 * Get lookup menu items for the current query.
8095 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
8096 * of the done event. If the request was aborted to make way for a subsequent request,
8097 * this promise will not be rejected: it will remain pending forever.
8099 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
8101 value
= this.lookupInput
.getValue(),
8102 deferred
= $.Deferred(),
8105 this.abortLookupRequest();
8106 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
8107 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
8109 this.lookupInput
.pushPending();
8110 this.lookupQuery
= value
;
8111 ourRequest
= this.lookupRequest
= this.getLookupRequest();
8113 .always( function () {
8114 // We need to pop pending even if this is an old request, otherwise
8115 // the widget will remain pending forever.
8116 // TODO: this assumes that an aborted request will fail or succeed soon after
8117 // being aborted, or at least eventually. It would be nice if we could popPending()
8118 // at abort time, but only if we knew that we hadn't already called popPending()
8119 // for that request.
8120 widget
.lookupInput
.popPending();
8122 .done( function ( data
) {
8123 // If this is an old request (and aborting it somehow caused it to still succeed),
8124 // ignore its success completely
8125 if ( ourRequest
=== widget
.lookupRequest
) {
8126 widget
.lookupQuery
= null;
8127 widget
.lookupRequest
= null;
8128 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
8129 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
8132 .fail( function () {
8133 // If this is an old request (or a request failing because it's being aborted),
8134 // ignore its failure completely
8135 if ( ourRequest
=== widget
.lookupRequest
) {
8136 widget
.lookupQuery
= null;
8137 widget
.lookupRequest
= null;
8142 return deferred
.promise();
8146 * Abort the currently pending lookup request, if any.
8148 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
8149 var oldRequest
= this.lookupRequest
;
8151 // First unset this.lookupRequest to the fail handler will notice
8152 // that the request is no longer current
8153 this.lookupRequest
= null;
8154 this.lookupQuery
= null;
8160 * Get a new request object of the current lookup query value.
8163 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
8165 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
8166 // Stub, implemented in subclass
8171 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
8174 * @param {Mixed} data Cached result data, usually an array
8175 * @return {OO.ui.MenuItemWidget[]} Menu items
8177 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
8178 // Stub, implemented in subclass
8183 * Get lookup cache item from server response data.
8186 * @param {Mixed} data Response from server
8187 * @return {Mixed} Cached result data
8189 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
8190 // Stub, implemented in subclass
8195 * Set of controls for an OO.ui.OutlineWidget.
8197 * Controls include moving items up and down, removing items, and adding different kinds of items.
8200 * @extends OO.ui.Widget
8201 * @mixins OO.ui.GroupElement
8202 * @mixins OO.ui.IconElement
8205 * @param {OO.ui.OutlineWidget} outline Outline to control
8206 * @param {Object} [config] Configuration options
8208 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
8209 // Configuration initialization
8210 config
= $.extend( { icon
: 'add' }, config
);
8212 // Parent constructor
8213 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
8215 // Mixin constructors
8216 OO
.ui
.GroupElement
.call( this, config
);
8217 OO
.ui
.IconElement
.call( this, config
);
8220 this.outline
= outline
;
8221 this.$movers
= this.$( '<div>' );
8222 this.upButton
= new OO
.ui
.ButtonWidget( {
8226 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
8228 this.downButton
= new OO
.ui
.ButtonWidget( {
8232 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
8234 this.removeButton
= new OO
.ui
.ButtonWidget( {
8238 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
8242 outline
.connect( this, {
8243 select
: 'onOutlineChange',
8244 add
: 'onOutlineChange',
8245 remove
: 'onOutlineChange'
8247 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
8248 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
8249 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
8252 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
8253 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
8255 .addClass( 'oo-ui-outlineControlsWidget-movers' )
8256 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
8257 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
8262 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
8263 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
8264 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
8270 * @param {number} places Number of places to move
8280 * Handle outline change events.
8282 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
8283 var i
, len
, firstMovable
, lastMovable
,
8284 items
= this.outline
.getItems(),
8285 selectedItem
= this.outline
.getSelectedItem(),
8286 movable
= selectedItem
&& selectedItem
.isMovable(),
8287 removable
= selectedItem
&& selectedItem
.isRemovable();
8292 while ( ++i
< len
) {
8293 if ( items
[i
].isMovable() ) {
8294 firstMovable
= items
[i
];
8300 if ( items
[i
].isMovable() ) {
8301 lastMovable
= items
[i
];
8306 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
8307 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
8308 this.removeButton
.setDisabled( !removable
);
8312 * Mixin for widgets with a boolean on/off state.
8318 * @param {Object} [config] Configuration options
8319 * @cfg {boolean} [value=false] Initial value
8321 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8322 // Configuration initialization
8323 config
= config
|| {};
8329 this.$element
.addClass( 'oo-ui-toggleWidget' );
8330 this.setValue( !!config
.value
);
8337 * @param {boolean} value Changed value
8343 * Get the value of the toggle.
8347 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8352 * Set the value of the toggle.
8354 * @param {boolean} value New value
8358 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8360 if ( this.value
!== value
) {
8362 this.emit( 'change', value
);
8363 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8364 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8370 * Group widget for multiple related buttons.
8372 * Use together with OO.ui.ButtonWidget.
8375 * @extends OO.ui.Widget
8376 * @mixins OO.ui.GroupElement
8379 * @param {Object} [config] Configuration options
8380 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
8382 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
8383 // Configuration initialization
8384 config
= config
|| {};
8386 // Parent constructor
8387 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
8389 // Mixin constructors
8390 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8393 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
8394 if ( $.isArray( config
.items
) ) {
8395 this.addItems( config
.items
);
8401 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
8402 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
8405 * Generic widget for buttons.
8408 * @extends OO.ui.Widget
8409 * @mixins OO.ui.ButtonElement
8410 * @mixins OO.ui.IconElement
8411 * @mixins OO.ui.IndicatorElement
8412 * @mixins OO.ui.LabelElement
8413 * @mixins OO.ui.TitledElement
8414 * @mixins OO.ui.FlaggedElement
8417 * @param {Object} [config] Configuration options
8418 * @cfg {string} [href] Hyperlink to visit when clicked
8419 * @cfg {string} [target] Target to open hyperlink in
8421 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8422 // Configuration initialization
8423 config
= $.extend( { target
: '_blank' }, config
);
8425 // Parent constructor
8426 OO
.ui
.ButtonWidget
.super.call( this, config
);
8428 // Mixin constructors
8429 OO
.ui
.ButtonElement
.call( this, config
);
8430 OO
.ui
.IconElement
.call( this, config
);
8431 OO
.ui
.IndicatorElement
.call( this, config
);
8432 OO
.ui
.LabelElement
.call( this, config
);
8433 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
8434 OO
.ui
.FlaggedElement
.call( this, config
);
8439 this.isHyperlink
= false;
8443 click
: this.onClick
.bind( this ),
8444 keypress
: this.onKeyPress
.bind( this )
8448 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8450 .addClass( 'oo-ui-buttonWidget' )
8451 .append( this.$button
);
8452 this.setHref( config
.href
);
8453 this.setTarget( config
.target
);
8458 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8459 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8460 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8461 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8462 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8463 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8464 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8475 * Handles mouse click events.
8477 * @param {jQuery.Event} e Mouse click event
8480 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8481 if ( !this.isDisabled() ) {
8482 this.emit( 'click' );
8483 if ( this.isHyperlink
) {
8491 * Handles keypress events.
8493 * @param {jQuery.Event} e Keypress event
8496 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8497 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8498 this.emit( 'click' );
8499 if ( this.isHyperlink
) {
8507 * Get hyperlink location.
8509 * @return {string} Hyperlink location
8511 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8516 * Get hyperlink target.
8518 * @return {string} Hyperlink target
8520 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8525 * Set hyperlink location.
8527 * @param {string|null} href Hyperlink location, null to remove
8529 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8530 href
= typeof href
=== 'string' ? href
: null;
8532 if ( href
!== this.href
) {
8534 if ( href
!== null ) {
8535 this.$button
.attr( 'href', href
);
8536 this.isHyperlink
= true;
8538 this.$button
.removeAttr( 'href' );
8539 this.isHyperlink
= false;
8547 * Set hyperlink target.
8549 * @param {string|null} target Hyperlink target, null to remove
8551 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8552 target
= typeof target
=== 'string' ? target
: null;
8554 if ( target
!== this.target
) {
8555 this.target
= target
;
8556 if ( target
!== null ) {
8557 this.$button
.attr( 'target', target
);
8559 this.$button
.removeAttr( 'target' );
8567 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8570 * @extends OO.ui.ButtonWidget
8571 * @mixins OO.ui.PendingElement
8574 * @param {Object} [config] Configuration options
8575 * @cfg {string} [action] Symbolic action name
8576 * @cfg {string[]} [modes] Symbolic mode names
8577 * @cfg {boolean} [framed=false] Render button with a frame
8579 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8580 // Configuration initialization
8581 config
= $.extend( { framed
: false }, config
);
8583 // Parent constructor
8584 OO
.ui
.ActionWidget
.super.call( this, config
);
8586 // Mixin constructors
8587 OO
.ui
.PendingElement
.call( this, config
);
8590 this.action
= config
.action
|| '';
8591 this.modes
= config
.modes
|| [];
8596 this.$element
.addClass( 'oo-ui-actionWidget' );
8601 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8602 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
8613 * Check if action is available in a certain mode.
8615 * @param {string} mode Name of mode
8616 * @return {boolean} Has mode
8618 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8619 return this.modes
.indexOf( mode
) !== -1;
8623 * Get symbolic action name.
8627 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8632 * Get symbolic action name.
8636 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8637 return this.modes
.slice();
8641 * Emit a resize event if the size has changed.
8645 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8648 if ( this.isElementAttached() ) {
8649 width
= this.$element
.width();
8650 height
= this.$element
.height();
8652 if ( width
!== this.width
|| height
!== this.height
) {
8654 this.height
= height
;
8655 this.emit( 'resize' );
8665 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8667 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8668 this.propagateResize();
8676 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8678 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8679 this.propagateResize();
8687 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8689 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8690 this.propagateResize();
8698 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8700 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8701 this.propagateResize();
8707 * Toggle visibility of button.
8709 * @param {boolean} [show] Show button, omit to toggle visibility
8712 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8714 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8715 this.propagateResize();
8721 * Button that shows and hides a popup.
8724 * @extends OO.ui.ButtonWidget
8725 * @mixins OO.ui.PopupElement
8728 * @param {Object} [config] Configuration options
8730 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8731 // Parent constructor
8732 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8734 // Mixin constructors
8735 OO
.ui
.PopupElement
.call( this, config
);
8739 .addClass( 'oo-ui-popupButtonWidget' )
8740 .append( this.popup
.$element
);
8745 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8746 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8751 * Handles mouse click events.
8753 * @param {jQuery.Event} e Mouse click event
8755 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8756 // Skip clicks within the popup
8757 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8761 if ( !this.isDisabled() ) {
8762 this.popup
.toggle();
8764 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8770 * Button that toggles on and off.
8773 * @extends OO.ui.ButtonWidget
8774 * @mixins OO.ui.ToggleWidget
8777 * @param {Object} [config] Configuration options
8778 * @cfg {boolean} [value=false] Initial value
8780 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8781 // Configuration initialization
8782 config
= config
|| {};
8784 // Parent constructor
8785 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8787 // Mixin constructors
8788 OO
.ui
.ToggleWidget
.call( this, config
);
8791 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8796 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8797 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8804 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8805 if ( !this.isDisabled() ) {
8806 this.setValue( !this.value
);
8810 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8816 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8818 if ( value
!== this.value
) {
8819 this.setActive( value
);
8822 // Parent method (from mixin)
8823 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8829 * Dropdown menu of options.
8831 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
8832 * can be accessed using the #getMenu method.
8834 * Use with OO.ui.MenuItemWidget.
8837 * @extends OO.ui.Widget
8838 * @mixins OO.ui.IconElement
8839 * @mixins OO.ui.IndicatorElement
8840 * @mixins OO.ui.LabelElement
8841 * @mixins OO.ui.TitledElement
8844 * @param {Object} [config] Configuration options
8845 * @cfg {Object} [menu] Configuration options to pass to menu widget
8847 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
8848 // Configuration initialization
8849 config
= $.extend( { indicator
: 'down' }, config
);
8851 // Parent constructor
8852 OO
.ui
.DropdownWidget
.super.call( this, config
);
8854 // Mixin constructors
8855 OO
.ui
.IconElement
.call( this, config
);
8856 OO
.ui
.IndicatorElement
.call( this, config
);
8857 OO
.ui
.LabelElement
.call( this, config
);
8858 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8861 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8862 this.$handle
= this.$( '<span>' );
8865 this.$element
.on( { click
: this.onClick
.bind( this ) } );
8866 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8870 .addClass( 'oo-ui-dropdownWidget-handle' )
8871 .append( this.$icon
, this.$label
, this.$indicator
);
8873 .addClass( 'oo-ui-dropdownWidget' )
8874 .append( this.$handle
, this.menu
.$element
);
8879 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
8880 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
8881 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
8882 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
8883 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
8890 * @return {OO.ui.MenuWidget} Menu of widget
8892 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
8897 * Handles menu select events.
8899 * @param {OO.ui.MenuItemWidget} item Selected menu item
8901 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
8908 selectedLabel
= item
.getLabel();
8910 // If the label is a DOM element, clone it, because setLabel will append() it
8911 if ( selectedLabel
instanceof jQuery
) {
8912 selectedLabel
= selectedLabel
.clone();
8915 this.setLabel( selectedLabel
);
8919 * Handles mouse click events.
8921 * @param {jQuery.Event} e Mouse click event
8923 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
8924 // Skip clicks within the menu
8925 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8929 if ( !this.isDisabled() ) {
8930 if ( this.menu
.isVisible() ) {
8931 this.menu
.toggle( false );
8933 this.menu
.toggle( true );
8942 * See OO.ui.IconElement for more information.
8945 * @extends OO.ui.Widget
8946 * @mixins OO.ui.IconElement
8947 * @mixins OO.ui.TitledElement
8950 * @param {Object} [config] Configuration options
8952 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8953 // Configuration initialization
8954 config
= config
|| {};
8956 // Parent constructor
8957 OO
.ui
.IconWidget
.super.call( this, config
);
8959 // Mixin constructors
8960 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
8961 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8964 this.$element
.addClass( 'oo-ui-iconWidget' );
8969 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8970 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
8971 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8973 /* Static Properties */
8975 OO
.ui
.IconWidget
.static.tagName
= 'span';
8980 * See OO.ui.IndicatorElement for more information.
8983 * @extends OO.ui.Widget
8984 * @mixins OO.ui.IndicatorElement
8985 * @mixins OO.ui.TitledElement
8988 * @param {Object} [config] Configuration options
8990 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8991 // Configuration initialization
8992 config
= config
|| {};
8994 // Parent constructor
8995 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8997 // Mixin constructors
8998 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
8999 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9002 this.$element
.addClass( 'oo-ui-indicatorWidget' );
9007 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
9008 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
9009 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
9011 /* Static Properties */
9013 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
9016 * Base class for input widgets.
9020 * @extends OO.ui.Widget
9021 * @mixins OO.ui.FlaggedElement
9024 * @param {Object} [config] Configuration options
9025 * @cfg {string} [name=''] HTML input name
9026 * @cfg {string} [value=''] Input value
9027 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
9029 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
9030 // Configuration initialization
9031 config
= config
|| {};
9033 // Parent constructor
9034 OO
.ui
.InputWidget
.super.call( this, config
);
9036 // Mixin constructors
9037 OO
.ui
.FlaggedElement
.call( this, config
);
9040 this.$input
= this.getInputElement( config
);
9042 this.inputFilter
= config
.inputFilter
;
9045 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
9049 .attr( 'name', config
.name
)
9050 .prop( 'disabled', this.isDisabled() );
9051 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
9052 this.setValue( config
.value
);
9057 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
9058 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
9064 * @param {string} value
9070 * Get input element.
9073 * @param {Object} [config] Configuration options
9074 * @return {jQuery} Input element
9076 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
9077 return this.$( '<input>' );
9081 * Handle potentially value-changing events.
9083 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
9085 OO
.ui
.InputWidget
.prototype.onEdit = function () {
9087 if ( !this.isDisabled() ) {
9088 // Allow the stack to clear so the value will be updated
9089 setTimeout( function () {
9090 widget
.setValue( widget
.$input
.val() );
9096 * Get the value of the input.
9098 * @return {string} Input value
9100 OO
.ui
.InputWidget
.prototype.getValue = function () {
9105 * Sets the direction of the current input, either RTL or LTR
9107 * @param {boolean} isRTL
9109 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
9111 this.$input
.removeClass( 'oo-ui-ltr' );
9112 this.$input
.addClass( 'oo-ui-rtl' );
9114 this.$input
.removeClass( 'oo-ui-rtl' );
9115 this.$input
.addClass( 'oo-ui-ltr' );
9120 * Set the value of the input.
9122 * @param {string} value New value
9126 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
9127 value
= this.sanitizeValue( value
);
9128 if ( this.value
!== value
) {
9130 this.emit( 'change', this.value
);
9132 // Update the DOM if it has changed. Note that with sanitizeValue, it
9133 // is possible for the DOM value to change without this.value changing.
9134 if ( this.$input
.val() !== this.value
) {
9135 this.$input
.val( this.value
);
9141 * Sanitize incoming value.
9143 * Ensures value is a string, and converts undefined and null to empty string.
9146 * @param {string} value Original value
9147 * @return {string} Sanitized value
9149 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
9150 if ( value
=== undefined || value
=== null ) {
9152 } else if ( this.inputFilter
) {
9153 return this.inputFilter( String( value
) );
9155 return String( value
);
9160 * Simulate the behavior of clicking on a label bound to this input.
9162 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
9163 if ( !this.isDisabled() ) {
9164 if ( this.$input
.is( ':checkbox,:radio' ) ) {
9165 this.$input
.click();
9166 } else if ( this.$input
.is( ':input' ) ) {
9167 this.$input
[0].focus();
9175 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
9176 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
9177 if ( this.$input
) {
9178 this.$input
.prop( 'disabled', this.isDisabled() );
9188 OO
.ui
.InputWidget
.prototype.focus = function () {
9189 this.$input
[0].focus();
9198 OO
.ui
.InputWidget
.prototype.blur = function () {
9199 this.$input
[0].blur();
9204 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
9207 * @extends OO.ui.InputWidget
9208 * @mixins OO.ui.ButtonElement
9209 * @mixins OO.ui.IconElement
9210 * @mixins OO.ui.IndicatorElement
9211 * @mixins OO.ui.LabelElement
9212 * @mixins OO.ui.TitledElement
9213 * @mixins OO.ui.FlaggedElement
9216 * @param {Object} [config] Configuration options
9217 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
9218 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
9219 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
9220 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
9221 * won't be possible to set a value (which will internally become identical to the label).
9223 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
9224 // Configuration initialization
9225 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
9227 // Properties (must be set before parent constructor, which calls #setValue)
9228 this.useInputTag
= config
.useInputTag
;
9230 // Parent constructor
9231 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
9233 // Mixin constructors
9234 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
9235 OO
.ui
.IconElement
.call( this, config
);
9236 OO
.ui
.IndicatorElement
.call( this, config
);
9237 OO
.ui
.LabelElement
.call( this, config
);
9238 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
9239 OO
.ui
.FlaggedElement
.call( this, config
);
9243 click
: this.onClick
.bind( this ),
9244 keypress
: this.onKeyPress
.bind( this )
9248 if ( !config
.useInputTag
) {
9249 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
9251 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
9256 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
9257 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
9258 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
9259 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
9260 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
9261 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
9262 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
9273 * Get input element.
9276 * @param {Object} [config] Configuration options
9277 * @return {jQuery} Input element
9279 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
9280 // Configuration intialization
9281 config
= config
|| {};
9283 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
9285 return this.$( html
);
9291 * Overridden to support setting the 'value' of `<input/>` elements.
9293 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
9294 * text; or null for no label
9297 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
9298 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
9300 if ( this.useInputTag
) {
9301 if ( typeof label
=== 'function' ) {
9302 label
= OO
.ui
.resolveMsg( label
);
9304 if ( label
instanceof jQuery
) {
9305 label
= label
.text();
9310 this.$input
.val( label
);
9317 * Set the value of the input.
9319 * Overridden to disable for `<input/>` elements, which have value identical to the label.
9321 * @param {string} value New value
9324 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
9325 if ( !this.useInputTag
) {
9326 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
9332 * Handles mouse click events.
9334 * @param {jQuery.Event} e Mouse click event
9337 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
9338 if ( !this.isDisabled() ) {
9339 this.emit( 'click' );
9345 * Handles keypress events.
9347 * @param {jQuery.Event} e Keypress event
9350 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
9351 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9352 this.emit( 'click' );
9358 * Checkbox input widget.
9361 * @extends OO.ui.InputWidget
9364 * @param {Object} [config] Configuration options
9366 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
9367 // Parent constructor
9368 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
9371 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
9376 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
9381 * Get input element.
9384 * @return {jQuery} Input element
9386 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
9387 return this.$( '<input type="checkbox" />' );
9391 * Get checked state of the checkbox
9393 * @return {boolean} If the checkbox is checked
9395 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
9400 * Set checked state of the checkbox
9402 * @param {boolean} value New value
9404 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
9406 if ( this.value
!== value
) {
9408 this.$input
.prop( 'checked', this.value
);
9409 this.emit( 'change', this.value
);
9416 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
9418 if ( !this.isDisabled() ) {
9419 // Allow the stack to clear so the value will be updated
9420 setTimeout( function () {
9421 widget
.setValue( widget
.$input
.prop( 'checked' ) );
9427 * Input widget with a text field.
9430 * @extends OO.ui.InputWidget
9431 * @mixins OO.ui.IconElement
9432 * @mixins OO.ui.IndicatorElement
9433 * @mixins OO.ui.PendingElement
9436 * @param {Object} [config] Configuration options
9437 * @cfg {string} [type='text'] HTML tag `type` attribute
9438 * @cfg {string} [placeholder] Placeholder text
9439 * @cfg {boolean} [readOnly=false] Prevent changes
9440 * @cfg {boolean} [multiline=false] Allow multiple lines of text
9441 * @cfg {boolean} [autosize=false] Automatically resize to fit content
9442 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
9443 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
9444 * one, see #static-validationPatterns)
9446 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
9447 // Configuration initialization
9448 config
= $.extend( { readOnly
: false }, config
);
9450 // Parent constructor
9451 OO
.ui
.TextInputWidget
.super.call( this, config
);
9453 // Mixin constructors
9454 OO
.ui
.IconElement
.call( this, config
);
9455 OO
.ui
.IndicatorElement
.call( this, config
);
9456 OO
.ui
.PendingElement
.call( this, config
);
9459 this.readOnly
= false;
9460 this.multiline
= !!config
.multiline
;
9461 this.autosize
= !!config
.autosize
;
9462 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
9463 this.validate
= null;
9465 this.setValidation( config
.validate
);
9469 keypress
: this.onKeyPress
.bind( this ),
9470 blur
: this.setValidityFlag
.bind( this )
9472 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
9473 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
9474 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
9478 .addClass( 'oo-ui-textInputWidget' )
9479 .append( this.$icon
, this.$indicator
);
9480 this.setReadOnly( config
.readOnly
);
9481 if ( config
.placeholder
) {
9482 this.$input
.attr( 'placeholder', config
.placeholder
);
9484 this.$element
.attr( 'role', 'textbox' );
9489 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
9490 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
9491 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
9492 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
9494 /* Static properties */
9496 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
9504 * User presses enter inside the text box.
9506 * Not called if input is multiline.
9512 * User clicks the icon.
9518 * User clicks the indicator.
9526 * Handle icon mouse down events.
9528 * @param {jQuery.Event} e Mouse down event
9531 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
9532 if ( e
.which
=== 1 ) {
9533 this.$input
[0].focus();
9534 this.emit( 'icon' );
9540 * Handle indicator mouse down events.
9542 * @param {jQuery.Event} e Mouse down event
9545 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
9546 if ( e
.which
=== 1 ) {
9547 this.$input
[0].focus();
9548 this.emit( 'indicator' );
9554 * Handle key press events.
9556 * @param {jQuery.Event} e Key press event
9557 * @fires enter If enter key is pressed and input is not multiline
9559 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
9560 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
9561 this.emit( 'enter' );
9566 * Handle element attach events.
9568 * @param {jQuery.Event} e Element attach event
9570 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
9577 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
9581 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9587 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9589 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9591 this.setValidityFlag();
9597 * Check if the widget is read-only.
9601 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
9602 return this.readOnly
;
9606 * Set the read-only state of the widget.
9608 * This should probably change the widgets's appearance and prevent it from being used.
9610 * @param {boolean} state Make input read-only
9613 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
9614 this.readOnly
= !!state
;
9615 this.$input
.prop( 'readOnly', this.readOnly
);
9620 * Automatically adjust the size of the text input.
9622 * This only affects multi-line inputs that are auto-sized.
9626 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9627 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
9629 if ( this.multiline
&& this.autosize
) {
9630 $clone
= this.$input
.clone()
9631 .val( this.$input
.val() )
9632 // Set inline height property to 0 to measure scroll height
9633 .css( { height
: 0 } )
9634 .insertAfter( this.$input
);
9635 scrollHeight
= $clone
[0].scrollHeight
;
9636 // Remove inline height property to measure natural heights
9637 $clone
.css( 'height', '' );
9638 innerHeight
= $clone
.innerHeight();
9639 outerHeight
= $clone
.outerHeight();
9640 // Measure max rows height
9641 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' ).val( '' );
9642 maxInnerHeight
= $clone
.innerHeight();
9643 // Difference between reported innerHeight and scrollHeight with no scrollbars present
9644 // Equals 1 on Blink-based browsers and 0 everywhere else
9645 measurementError
= maxInnerHeight
- $clone
[0].scrollHeight
;
9647 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
9648 // Only apply inline height when expansion beyond natural height is needed
9649 if ( idealHeight
> innerHeight
) {
9650 // Use the difference between the inner and outer height as a buffer
9651 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
9653 this.$input
.css( 'height', '' );
9660 * Get input element.
9663 * @param {Object} [config] Configuration options
9664 * @return {jQuery} Input element
9666 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9667 // Configuration initialization
9668 config
= config
|| {};
9670 var type
= config
.type
|| 'text';
9672 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + type
+ '" />' );
9676 * Check if input supports multiple lines.
9680 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9681 return !!this.multiline
;
9685 * Check if input automatically adjusts its size.
9689 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9690 return !!this.autosize
;
9694 * Select the contents of the input.
9698 OO
.ui
.TextInputWidget
.prototype.select = function () {
9699 this.$input
.select();
9704 * Sets the validation pattern to use.
9705 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
9706 * one, see #static-validationPatterns)
9708 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
9709 if ( validate
instanceof RegExp
) {
9710 this.validate
= validate
;
9712 this.validate
= this.constructor.static.validationPatterns
[validate
] || /.*/;
9717 * Sets the 'invalid' flag appropriately.
9719 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
9721 this.isValid().done( function ( valid
) {
9722 widget
.setFlags( { invalid
: !valid
} );
9727 * Returns whether or not the current value is considered valid, according to the
9728 * supplied validation pattern.
9730 * @return {jQuery.Deferred}
9732 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
9733 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
9737 * Text input with a menu of optional values.
9740 * @extends OO.ui.Widget
9743 * @param {Object} [config] Configuration options
9744 * @cfg {Object} [menu] Configuration options to pass to menu widget
9745 * @cfg {Object} [input] Configuration options to pass to input widget
9746 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
9748 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9749 // Configuration initialization
9750 config
= config
|| {};
9752 // Parent constructor
9753 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9756 this.$overlay
= config
.$overlay
|| this.$element
;
9757 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9758 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9761 this.menu
= new OO
.ui
.TextInputMenuWidget( this.input
, $.extend(
9763 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
9766 disabled
: this.isDisabled()
9772 this.input
.connect( this, {
9773 change
: 'onInputChange',
9774 indicator
: 'onInputIndicator',
9775 enter
: 'onInputEnter'
9777 this.menu
.connect( this, {
9778 choose
: 'onMenuChoose',
9779 add
: 'onMenuItemsChange',
9780 remove
: 'onMenuItemsChange'
9784 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
9785 this.$overlay
.append( this.menu
.$element
);
9786 this.onMenuItemsChange();
9791 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9796 * Handle input change events.
9798 * @param {string} value New value
9800 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9801 var match
= this.menu
.getItemFromData( value
);
9803 this.menu
.selectItem( match
);
9805 if ( !this.isDisabled() ) {
9806 this.menu
.toggle( true );
9811 * Handle input indicator events.
9813 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9814 if ( !this.isDisabled() ) {
9820 * Handle input enter events.
9822 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
9823 if ( !this.isDisabled() ) {
9824 this.menu
.toggle( false );
9829 * Handle menu choose events.
9831 * @param {OO.ui.OptionWidget} item Chosen item
9833 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
9835 this.input
.setValue( item
.getData() );
9840 * Handle menu item change events.
9842 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
9843 var match
= this.menu
.getItemFromData( this.input
.getValue() );
9844 this.menu
.selectItem( match
);
9845 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
9851 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
9853 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
9856 this.input
.setDisabled( this.isDisabled() );
9859 this.menu
.setDisabled( this.isDisabled() );
9869 * @extends OO.ui.Widget
9870 * @mixins OO.ui.LabelElement
9873 * @param {Object} [config] Configuration options
9875 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
9876 // Configuration initialization
9877 config
= config
|| {};
9879 // Parent constructor
9880 OO
.ui
.LabelWidget
.super.call( this, config
);
9882 // Mixin constructors
9883 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
9884 OO
.ui
.TitledElement
.call( this, config
);
9887 this.input
= config
.input
;
9890 if ( this.input
instanceof OO
.ui
.InputWidget
) {
9891 this.$element
.on( 'click', this.onClick
.bind( this ) );
9895 this.$element
.addClass( 'oo-ui-labelWidget' );
9900 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
9901 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
9902 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
9904 /* Static Properties */
9906 OO
.ui
.LabelWidget
.static.tagName
= 'span';
9911 * Handles label mouse click events.
9913 * @param {jQuery.Event} e Mouse click event
9915 OO
.ui
.LabelWidget
.prototype.onClick = function () {
9916 this.input
.simulateLabelClick();
9921 * Generic option widget for use with OO.ui.SelectWidget.
9924 * @extends OO.ui.Widget
9925 * @mixins OO.ui.LabelElement
9926 * @mixins OO.ui.FlaggedElement
9929 * @param {Mixed} data Option data
9930 * @param {Object} [config] Configuration options
9931 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
9933 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
9934 // Configuration initialization
9935 config
= config
|| {};
9937 // Parent constructor
9938 OO
.ui
.OptionWidget
.super.call( this, config
);
9940 // Mixin constructors
9941 OO
.ui
.ItemWidget
.call( this );
9942 OO
.ui
.LabelElement
.call( this, config
);
9943 OO
.ui
.FlaggedElement
.call( this, config
);
9947 this.selected
= false;
9948 this.highlighted
= false;
9949 this.pressed
= false;
9953 .data( 'oo-ui-optionWidget', this )
9954 .attr( 'rel', config
.rel
)
9955 .attr( 'role', 'option' )
9956 .addClass( 'oo-ui-optionWidget' )
9957 .append( this.$label
);
9962 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
9963 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
9964 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
9965 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
9967 /* Static Properties */
9969 OO
.ui
.OptionWidget
.static.selectable
= true;
9971 OO
.ui
.OptionWidget
.static.highlightable
= true;
9973 OO
.ui
.OptionWidget
.static.pressable
= true;
9975 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
9980 * Check if option can be selected.
9982 * @return {boolean} Item is selectable
9984 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
9985 return this.constructor.static.selectable
&& !this.isDisabled();
9989 * Check if option can be highlighted.
9991 * @return {boolean} Item is highlightable
9993 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
9994 return this.constructor.static.highlightable
&& !this.isDisabled();
9998 * Check if option can be pressed.
10000 * @return {boolean} Item is pressable
10002 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
10003 return this.constructor.static.pressable
&& !this.isDisabled();
10007 * Check if option is selected.
10009 * @return {boolean} Item is selected
10011 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
10012 return this.selected
;
10016 * Check if option is highlighted.
10018 * @return {boolean} Item is highlighted
10020 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
10021 return this.highlighted
;
10025 * Check if option is pressed.
10027 * @return {boolean} Item is pressed
10029 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
10030 return this.pressed
;
10034 * Set selected state.
10036 * @param {boolean} [state=false] Select option
10039 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
10040 if ( this.constructor.static.selectable
) {
10041 this.selected
= !!state
;
10042 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
10043 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
10044 this.scrollElementIntoView();
10046 this.updateThemeClasses();
10052 * Set highlighted state.
10054 * @param {boolean} [state=false] Highlight option
10057 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
10058 if ( this.constructor.static.highlightable
) {
10059 this.highlighted
= !!state
;
10060 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
10061 this.updateThemeClasses();
10067 * Set pressed state.
10069 * @param {boolean} [state=false] Press option
10072 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
10073 if ( this.constructor.static.pressable
) {
10074 this.pressed
= !!state
;
10075 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
10076 this.updateThemeClasses();
10082 * Make the option's highlight flash.
10084 * While flashing, the visual style of the pressed state is removed if present.
10086 * @return {jQuery.Promise} Promise resolved when flashing is done
10088 OO
.ui
.OptionWidget
.prototype.flash = function () {
10090 $element
= this.$element
,
10091 deferred
= $.Deferred();
10093 if ( !this.isDisabled() && this.constructor.static.pressable
) {
10094 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
10095 setTimeout( function () {
10096 // Restore original classes
10098 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
10099 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
10101 setTimeout( function () {
10102 deferred
.resolve();
10108 return deferred
.promise();
10114 * @return {Mixed} Option data
10116 OO
.ui
.OptionWidget
.prototype.getData = function () {
10121 * Option widget with an option icon and indicator.
10123 * Use together with OO.ui.SelectWidget.
10126 * @extends OO.ui.OptionWidget
10127 * @mixins OO.ui.IconElement
10128 * @mixins OO.ui.IndicatorElement
10131 * @param {Mixed} data Option data
10132 * @param {Object} [config] Configuration options
10134 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
10135 // Parent constructor
10136 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
10138 // Mixin constructors
10139 OO
.ui
.IconElement
.call( this, config
);
10140 OO
.ui
.IndicatorElement
.call( this, config
);
10144 .addClass( 'oo-ui-decoratedOptionWidget' )
10145 .prepend( this.$icon
)
10146 .append( this.$indicator
);
10151 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
10152 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
10153 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
10156 * Option widget that looks like a button.
10158 * Use together with OO.ui.ButtonSelectWidget.
10161 * @extends OO.ui.DecoratedOptionWidget
10162 * @mixins OO.ui.ButtonElement
10165 * @param {Mixed} data Option data
10166 * @param {Object} [config] Configuration options
10168 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
10169 // Parent constructor
10170 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
10172 // Mixin constructors
10173 OO
.ui
.ButtonElement
.call( this, config
);
10176 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
10177 this.$button
.append( this.$element
.contents() );
10178 this.$element
.append( this.$button
);
10183 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10184 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
10186 /* Static Properties */
10188 // Allow button mouse down events to pass through so they can be handled by the parent select widget
10189 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
10196 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
10197 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
10199 if ( this.constructor.static.selectable
) {
10200 this.setActive( state
);
10207 * Item of an OO.ui.MenuWidget.
10210 * @extends OO.ui.DecoratedOptionWidget
10213 * @param {Mixed} data Item data
10214 * @param {Object} [config] Configuration options
10216 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
10217 // Configuration initialization
10218 config
= $.extend( { icon
: 'check' }, config
);
10220 // Parent constructor
10221 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
10225 .attr( 'role', 'menuitem' )
10226 .addClass( 'oo-ui-menuItemWidget' );
10231 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10234 * Section to group one or more items in a OO.ui.MenuWidget.
10237 * @extends OO.ui.DecoratedOptionWidget
10240 * @param {Mixed} data Item data
10241 * @param {Object} [config] Configuration options
10243 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
10244 // Parent constructor
10245 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
10248 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
10253 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10255 /* Static Properties */
10257 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
10259 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
10262 * Items for an OO.ui.OutlineWidget.
10265 * @extends OO.ui.DecoratedOptionWidget
10268 * @param {Mixed} data Item data
10269 * @param {Object} [config] Configuration options
10270 * @cfg {number} [level] Indentation level
10271 * @cfg {boolean} [movable] Allow modification from outline controls
10273 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
10274 // Configuration initialization
10275 config
= config
|| {};
10277 // Parent constructor
10278 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
10282 this.movable
= !!config
.movable
;
10283 this.removable
= !!config
.removable
;
10286 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
10287 this.setLevel( config
.level
);
10292 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10294 /* Static Properties */
10296 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
10298 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
10300 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
10302 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
10307 * Check if item is movable.
10309 * Movablilty is used by outline controls.
10311 * @return {boolean} Item is movable
10313 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
10314 return this.movable
;
10318 * Check if item is removable.
10320 * Removablilty is used by outline controls.
10322 * @return {boolean} Item is removable
10324 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
10325 return this.removable
;
10329 * Get indentation level.
10331 * @return {number} Indentation level
10333 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
10340 * Movablilty is used by outline controls.
10342 * @param {boolean} movable Item is movable
10345 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
10346 this.movable
= !!movable
;
10347 this.updateThemeClasses();
10352 * Set removability.
10354 * Removablilty is used by outline controls.
10356 * @param {boolean} movable Item is removable
10359 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
10360 this.removable
= !!removable
;
10361 this.updateThemeClasses();
10366 * Set indentation level.
10368 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
10371 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
10372 var levels
= this.constructor.static.levels
,
10373 levelClass
= this.constructor.static.levelClass
,
10376 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
10378 if ( this.level
=== i
) {
10379 this.$element
.addClass( levelClass
+ i
);
10381 this.$element
.removeClass( levelClass
+ i
);
10384 this.updateThemeClasses();
10390 * Container for content that is overlaid and positioned absolutely.
10393 * @extends OO.ui.Widget
10394 * @mixins OO.ui.LabelElement
10397 * @param {Object} [config] Configuration options
10398 * @cfg {number} [width=320] Width of popup in pixels
10399 * @cfg {number} [height] Height of popup, omit to use automatic height
10400 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
10401 * @cfg {string} [align='center'] Alignment of popup to origin
10402 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
10403 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
10404 * @cfg {jQuery} [$content] Content to append to the popup's body
10405 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
10406 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
10407 * @cfg {boolean} [head] Show label and close button at the top
10408 * @cfg {boolean} [padded] Add padding to the body
10410 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
10411 // Configuration initialization
10412 config
= config
|| {};
10414 // Parent constructor
10415 OO
.ui
.PopupWidget
.super.call( this, config
);
10417 // Mixin constructors
10418 OO
.ui
.LabelElement
.call( this, config
);
10419 OO
.ui
.ClippableElement
.call( this, config
);
10422 this.visible
= false;
10423 this.$popup
= this.$( '<div>' );
10424 this.$head
= this.$( '<div>' );
10425 this.$body
= this.$( '<div>' );
10426 this.$anchor
= this.$( '<div>' );
10427 // If undefined, will be computed lazily in updateDimensions()
10428 this.$container
= config
.$container
;
10429 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
10430 this.autoClose
= !!config
.autoClose
;
10431 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
10432 this.transitionTimeout
= null;
10433 this.anchor
= null;
10434 this.width
= config
.width
!== undefined ? config
.width
: 320;
10435 this.height
= config
.height
!== undefined ? config
.height
: null;
10436 this.align
= config
.align
|| 'center';
10437 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
10438 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
10441 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
10444 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
10445 this.$body
.addClass( 'oo-ui-popupWidget-body' );
10446 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
10448 .addClass( 'oo-ui-popupWidget-head' )
10449 .append( this.$label
, this.closeButton
.$element
);
10450 if ( !config
.head
) {
10454 .addClass( 'oo-ui-popupWidget-popup' )
10455 .append( this.$head
, this.$body
);
10458 .addClass( 'oo-ui-popupWidget' )
10459 .append( this.$popup
, this.$anchor
);
10460 // Move content, which was added to #$element by OO.ui.Widget, to the body
10461 if ( config
.$content
instanceof jQuery
) {
10462 this.$body
.append( config
.$content
);
10464 if ( config
.padded
) {
10465 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
10467 this.setClippableElement( this.$body
);
10472 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
10473 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
10474 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
10479 * Handles mouse down events.
10481 * @param {jQuery.Event} e Mouse down event
10483 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
10485 this.isVisible() &&
10486 !$.contains( this.$element
[0], e
.target
) &&
10487 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
10489 this.toggle( false );
10494 * Bind mouse down listener.
10496 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
10497 // Capture clicks outside popup
10498 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
10502 * Handles close button click events.
10504 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
10505 if ( this.isVisible() ) {
10506 this.toggle( false );
10511 * Unbind mouse down listener.
10513 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
10514 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
10518 * Set whether to show a anchor.
10520 * @param {boolean} [show] Show anchor, omit to toggle
10522 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
10523 show
= show
=== undefined ? !this.anchored
: !!show
;
10525 if ( this.anchored
!== show
) {
10527 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
10529 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
10531 this.anchored
= show
;
10536 * Check if showing a anchor.
10538 * @return {boolean} anchor is visible
10540 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
10541 return this.anchor
;
10547 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
10548 show
= show
=== undefined ? !this.isVisible() : !!show
;
10550 var change
= show
!== this.isVisible();
10553 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
10557 if ( this.autoClose
) {
10558 this.bindMouseDownListener();
10560 this.updateDimensions();
10561 this.toggleClipping( true );
10563 this.toggleClipping( false );
10564 if ( this.autoClose
) {
10565 this.unbindMouseDownListener();
10574 * Set the size of the popup.
10576 * Changing the size may also change the popup's position depending on the alignment.
10578 * @param {number} width Width
10579 * @param {number} height Height
10580 * @param {boolean} [transition=false] Use a smooth transition
10583 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
10584 this.width
= width
;
10585 this.height
= height
!== undefined ? height
: null;
10586 if ( this.isVisible() ) {
10587 this.updateDimensions( transition
);
10592 * Update the size and position.
10594 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
10595 * be called automatically.
10597 * @param {boolean} [transition=false] Use a smooth transition
10600 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
10601 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
10602 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
10605 if ( !this.$container
) {
10606 // Lazy-initialize $container if not specified in constructor
10607 this.$container
= this.$( this.getClosestScrollableElementContainer() );
10610 // Set height and width before measuring things, since it might cause our measurements
10611 // to change (e.g. due to scrollbars appearing or disappearing)
10614 height
: this.height
!== null ? this.height
: 'auto'
10617 // Compute initial popupOffset based on alignment
10618 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
10620 // Figure out if this will cause the popup to go beyond the edge of the container
10621 originOffset
= Math
.round( this.$element
.offset().left
);
10622 containerLeft
= Math
.round( this.$container
.offset().left
);
10623 containerWidth
= this.$container
.innerWidth();
10624 containerRight
= containerLeft
+ containerWidth
;
10625 popupLeft
= popupOffset
- this.containerPadding
;
10626 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
10627 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
10628 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10630 // Adjust offset to make the popup not go beyond the edge, if needed
10631 if ( overlapRight
< 0 ) {
10632 popupOffset
+= overlapRight
;
10633 } else if ( overlapLeft
< 0 ) {
10634 popupOffset
-= overlapLeft
;
10637 // Adjust offset to avoid anchor being rendered too close to the edge
10638 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
10639 // TODO: Find a measurement that works for CSS anchors and image anchors
10640 anchorWidth
= this.$anchor
[0].scrollWidth
* 2;
10641 if ( popupOffset
+ this.width
< anchorWidth
) {
10642 popupOffset
= anchorWidth
- this.width
;
10643 } else if ( -popupOffset
< anchorWidth
) {
10644 popupOffset
= -anchorWidth
;
10647 // Prevent transition from being interrupted
10648 clearTimeout( this.transitionTimeout
);
10649 if ( transition
) {
10650 // Enable transition
10651 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10654 // Position body relative to anchor
10655 this.$popup
.css( 'margin-left', popupOffset
);
10657 if ( transition
) {
10658 // Prevent transitioning after transition is complete
10659 this.transitionTimeout
= setTimeout( function () {
10660 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10663 // Prevent transitioning immediately
10664 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10667 // Reevaluate clipping state since we've relocated and resized the popup
10674 * Progress bar widget.
10677 * @extends OO.ui.Widget
10680 * @param {Object} [config] Configuration options
10681 * @cfg {number} [progress=0] Initial progress
10683 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
10684 // Configuration initialization
10685 config
= config
|| {};
10687 // Parent constructor
10688 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
10691 this.$bar
= this.$( '<div>' );
10692 this.progress
= null;
10695 this.setProgress( config
.progress
|| 0 );
10696 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar');
10699 role
: 'progressbar',
10700 'aria-valuemin': 0,
10701 'aria-valuemax': 100
10703 .addClass( 'oo-ui-progressBarWidget' )
10704 .append( this.$bar
);
10709 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
10711 /* Static Properties */
10713 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
10718 * Get progress percent
10720 * @return {number} Progress percent
10722 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
10723 return this.progress
;
10727 * Set progress percent
10729 * @param {number} progress Progress percent
10731 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
10732 this.progress
= progress
;
10734 this.$bar
.css( 'width', this.progress
+ '%' );
10735 this.$element
.attr( 'aria-valuenow', this.progress
);
10741 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10742 * Results are cleared and populated each time the query is changed.
10745 * @extends OO.ui.Widget
10748 * @param {Object} [config] Configuration options
10749 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10750 * @cfg {string} [value] Initial query value
10752 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10753 // Configuration intialization
10754 config
= config
|| {};
10756 // Parent constructor
10757 OO
.ui
.SearchWidget
.super.call( this, config
);
10760 this.query
= new OO
.ui
.TextInputWidget( {
10763 placeholder
: config
.placeholder
,
10764 value
: config
.value
10766 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10767 this.$query
= this.$( '<div>' );
10768 this.$results
= this.$( '<div>' );
10771 this.query
.connect( this, {
10772 change
: 'onQueryChange',
10773 enter
: 'onQueryEnter'
10775 this.results
.connect( this, {
10776 highlight
: 'onResultsHighlight',
10777 select
: 'onResultsSelect'
10779 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
10783 .addClass( 'oo-ui-searchWidget-query' )
10784 .append( this.query
.$element
);
10786 .addClass( 'oo-ui-searchWidget-results' )
10787 .append( this.results
.$element
);
10789 .addClass( 'oo-ui-searchWidget' )
10790 .append( this.$results
, this.$query
);
10795 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
10801 * @param {Object|null} item Item data or null if no item is highlighted
10806 * @param {Object|null} item Item data or null if no item is selected
10812 * Handle query key down events.
10814 * @param {jQuery.Event} e Key down event
10816 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
10817 var highlightedItem
, nextItem
,
10818 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
10821 highlightedItem
= this.results
.getHighlightedItem();
10822 if ( !highlightedItem
) {
10823 highlightedItem
= this.results
.getSelectedItem();
10825 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
10826 this.results
.highlightItem( nextItem
);
10827 nextItem
.scrollElementIntoView();
10832 * Handle select widget select events.
10834 * Clears existing results. Subclasses should repopulate items according to new query.
10836 * @param {string} value New value
10838 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
10840 this.results
.clearItems();
10844 * Handle select widget enter key events.
10846 * Selects highlighted item.
10848 * @param {string} value New value
10850 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
10852 this.results
.selectItem( this.results
.getHighlightedItem() );
10856 * Handle select widget highlight events.
10858 * @param {OO.ui.OptionWidget} item Highlighted item
10861 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
10862 this.emit( 'highlight', item
? item
.getData() : null );
10866 * Handle select widget select events.
10868 * @param {OO.ui.OptionWidget} item Selected item
10871 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
10872 this.emit( 'select', item
? item
.getData() : null );
10876 * Get the query input.
10878 * @return {OO.ui.TextInputWidget} Query input
10880 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
10885 * Get the results list.
10887 * @return {OO.ui.SelectWidget} Select list
10889 OO
.ui
.SearchWidget
.prototype.getResults = function () {
10890 return this.results
;
10894 * Generic selection of options.
10896 * Items can contain any rendering, and are uniquely identified by a hash of their data. Any widget
10897 * that provides options, from which the user must choose one, should be built on this class.
10899 * Use together with OO.ui.OptionWidget.
10902 * @extends OO.ui.Widget
10903 * @mixins OO.ui.GroupElement
10906 * @param {Object} [config] Configuration options
10907 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
10909 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
10910 // Configuration initialization
10911 config
= config
|| {};
10913 // Parent constructor
10914 OO
.ui
.SelectWidget
.super.call( this, config
);
10916 // Mixin constructors
10917 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
10920 this.pressed
= false;
10921 this.selecting
= null;
10923 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
10924 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
10927 this.$element
.on( {
10928 mousedown
: this.onMouseDown
.bind( this ),
10929 mouseover
: this.onMouseOver
.bind( this ),
10930 mouseleave
: this.onMouseLeave
.bind( this )
10934 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
10935 if ( $.isArray( config
.items
) ) {
10936 this.addItems( config
.items
);
10942 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
10944 // Need to mixin base class as well
10945 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
10946 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
10952 * @param {OO.ui.OptionWidget|null} item Highlighted item
10957 * @param {OO.ui.OptionWidget|null} item Pressed item
10962 * @param {OO.ui.OptionWidget|null} item Selected item
10967 * @param {OO.ui.OptionWidget|null} item Chosen item
10972 * @param {OO.ui.OptionWidget[]} items Added items
10973 * @param {number} index Index items were added at
10978 * @param {OO.ui.OptionWidget[]} items Removed items
10984 * Handle mouse down events.
10987 * @param {jQuery.Event} e Mouse down event
10989 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
10992 if ( !this.isDisabled() && e
.which
=== 1 ) {
10993 this.togglePressed( true );
10994 item
= this.getTargetItem( e
);
10995 if ( item
&& item
.isSelectable() ) {
10996 this.pressItem( item
);
10997 this.selecting
= item
;
10998 this.getElementDocument().addEventListener(
11000 this.onMouseUpHandler
,
11003 this.getElementDocument().addEventListener(
11005 this.onMouseMoveHandler
,
11014 * Handle mouse up events.
11017 * @param {jQuery.Event} e Mouse up event
11019 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
11022 this.togglePressed( false );
11023 if ( !this.selecting
) {
11024 item
= this.getTargetItem( e
);
11025 if ( item
&& item
.isSelectable() ) {
11026 this.selecting
= item
;
11029 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
11030 this.pressItem( null );
11031 this.chooseItem( this.selecting
);
11032 this.selecting
= null;
11035 this.getElementDocument().removeEventListener(
11037 this.onMouseUpHandler
,
11040 this.getElementDocument().removeEventListener(
11042 this.onMouseMoveHandler
,
11050 * Handle mouse move events.
11053 * @param {jQuery.Event} e Mouse move event
11055 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
11058 if ( !this.isDisabled() && this.pressed
) {
11059 item
= this.getTargetItem( e
);
11060 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
11061 this.pressItem( item
);
11062 this.selecting
= item
;
11069 * Handle mouse over events.
11072 * @param {jQuery.Event} e Mouse over event
11074 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
11077 if ( !this.isDisabled() ) {
11078 item
= this.getTargetItem( e
);
11079 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
11085 * Handle mouse leave events.
11088 * @param {jQuery.Event} e Mouse over event
11090 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
11091 if ( !this.isDisabled() ) {
11092 this.highlightItem( null );
11098 * Get the closest item to a jQuery.Event.
11101 * @param {jQuery.Event} e
11102 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
11104 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
11105 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
11106 if ( $item
.length
) {
11107 return $item
.data( 'oo-ui-optionWidget' );
11113 * Get selected item.
11115 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
11117 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
11120 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11121 if ( this.items
[i
].isSelected() ) {
11122 return this.items
[i
];
11129 * Get highlighted item.
11131 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
11133 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
11136 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11137 if ( this.items
[i
].isHighlighted() ) {
11138 return this.items
[i
];
11145 * Get an existing item with equivilant data.
11147 * @param {Object} data Item data to search for
11148 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
11150 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
11151 var hash
= OO
.getHash( data
);
11153 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11154 return this.hashes
[hash
];
11161 * Toggle pressed state.
11163 * @param {boolean} pressed An option is being pressed
11165 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
11166 if ( pressed
=== undefined ) {
11167 pressed
= !this.pressed
;
11169 if ( pressed
!== this.pressed
) {
11171 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
11172 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
11173 this.pressed
= pressed
;
11178 * Highlight an item.
11180 * Highlighting is mutually exclusive.
11182 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
11186 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
11187 var i
, len
, highlighted
,
11190 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11191 highlighted
= this.items
[i
] === item
;
11192 if ( this.items
[i
].isHighlighted() !== highlighted
) {
11193 this.items
[i
].setHighlighted( highlighted
);
11198 this.emit( 'highlight', item
);
11207 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
11211 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
11212 var i
, len
, selected
,
11215 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11216 selected
= this.items
[i
] === item
;
11217 if ( this.items
[i
].isSelected() !== selected
) {
11218 this.items
[i
].setSelected( selected
);
11223 this.emit( 'select', item
);
11232 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
11236 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
11237 var i
, len
, pressed
,
11240 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11241 pressed
= this.items
[i
] === item
;
11242 if ( this.items
[i
].isPressed() !== pressed
) {
11243 this.items
[i
].setPressed( pressed
);
11248 this.emit( 'press', item
);
11257 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
11258 * an item is selected using the keyboard or mouse.
11260 * @param {OO.ui.OptionWidget} item Item to choose
11264 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
11265 this.selectItem( item
);
11266 this.emit( 'choose', item
);
11272 * Get an item relative to another one.
11274 * @param {OO.ui.OptionWidget} item Item to start at
11275 * @param {number} direction Direction to move in
11276 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
11278 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
11279 var inc
= direction
> 0 ? 1 : -1,
11280 len
= this.items
.length
,
11281 index
= item
instanceof OO
.ui
.OptionWidget
?
11282 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
11283 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
11285 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
11286 Math
.max( index
, -1 ) :
11287 // Default to n-1 instead of -1, if nothing is selected let's start at the end
11288 Math
.min( index
, len
);
11290 while ( len
!== 0 ) {
11291 i
= ( i
+ inc
+ len
) % len
;
11292 item
= this.items
[i
];
11293 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11296 // Stop iterating when we've looped all the way around
11297 if ( i
=== stopAt
) {
11305 * Get the next selectable item.
11307 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
11309 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
11312 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11313 item
= this.items
[i
];
11314 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11325 * When items are added with the same values as existing items, the existing items will be
11326 * automatically removed before the new items are added.
11328 * @param {OO.ui.OptionWidget[]} items Items to add
11329 * @param {number} [index] Index to insert items after
11333 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
11334 var i
, len
, item
, hash
,
11337 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11339 hash
= OO
.getHash( item
.getData() );
11340 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11341 // Remove item with same value
11342 remove
.push( this.hashes
[hash
] );
11344 this.hashes
[hash
] = item
;
11346 if ( remove
.length
) {
11347 this.removeItems( remove
);
11351 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
11353 // Always provide an index, even if it was omitted
11354 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
11362 * Items will be detached, not removed, so they can be used later.
11364 * @param {OO.ui.OptionWidget[]} items Items to remove
11368 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
11369 var i
, len
, item
, hash
;
11371 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11373 hash
= OO
.getHash( item
.getData() );
11374 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11375 // Remove existing item
11376 delete this.hashes
[hash
];
11378 if ( item
.isSelected() ) {
11379 this.selectItem( null );
11384 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
11386 this.emit( 'remove', items
);
11394 * Items will be detached, not removed, so they can be used later.
11399 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
11400 var items
= this.items
.slice();
11405 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
11406 this.selectItem( null );
11408 this.emit( 'remove', items
);
11414 * Select widget containing button options.
11416 * Use together with OO.ui.ButtonOptionWidget.
11419 * @extends OO.ui.SelectWidget
11422 * @param {Object} [config] Configuration options
11424 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
11425 // Parent constructor
11426 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
11429 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
11434 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
11437 * Overlaid menu of options.
11439 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
11442 * Use together with OO.ui.MenuItemWidget.
11445 * @extends OO.ui.SelectWidget
11446 * @mixins OO.ui.ClippableElement
11449 * @param {Object} [config] Configuration options
11450 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
11451 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
11452 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
11454 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
11455 // Configuration initialization
11456 config
= config
|| {};
11458 // Parent constructor
11459 OO
.ui
.MenuWidget
.super.call( this, config
);
11461 // Mixin constructors
11462 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
11465 this.flashing
= false;
11466 this.visible
= false;
11467 this.newItems
= null;
11468 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
11469 this.$input
= config
.input
? config
.input
.$input
: null;
11470 this.$widget
= config
.widget
? config
.widget
.$element
: null;
11471 this.$previousFocus
= null;
11472 this.isolated
= !config
.input
;
11473 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
11474 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
11479 .attr( 'role', 'menu' )
11480 .addClass( 'oo-ui-menuWidget' );
11485 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
11486 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
11491 * Handles document mouse down events.
11493 * @param {jQuery.Event} e Key down event
11495 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
11496 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
11497 this.toggle( false );
11502 * Handles key down events.
11504 * @param {jQuery.Event} e Key down event
11506 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
11509 highlightItem
= this.getHighlightedItem();
11511 if ( !this.isDisabled() && this.isVisible() ) {
11512 if ( !highlightItem
) {
11513 highlightItem
= this.getSelectedItem();
11515 switch ( e
.keyCode
) {
11516 case OO
.ui
.Keys
.ENTER
:
11517 this.chooseItem( highlightItem
);
11520 case OO
.ui
.Keys
.UP
:
11521 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
11524 case OO
.ui
.Keys
.DOWN
:
11525 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
11528 case OO
.ui
.Keys
.ESCAPE
:
11529 if ( highlightItem
) {
11530 highlightItem
.setHighlighted( false );
11532 this.toggle( false );
11538 this.highlightItem( nextItem
);
11539 nextItem
.scrollElementIntoView();
11543 e
.preventDefault();
11544 e
.stopPropagation();
11551 * Bind key down listener.
11553 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
11554 if ( this.$input
) {
11555 this.$input
.on( 'keydown', this.onKeyDownHandler
);
11557 // Capture menu navigation keys
11558 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
11563 * Unbind key down listener.
11565 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
11566 if ( this.$input
) {
11567 this.$input
.off( 'keydown' );
11569 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
11576 * This will close the menu when done, unlike selectItem which only changes selection.
11578 * @param {OO.ui.OptionWidget} item Item to choose
11581 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
11585 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
11587 if ( item
&& !this.flashing
) {
11588 this.flashing
= true;
11589 item
.flash().done( function () {
11590 widget
.toggle( false );
11591 widget
.flashing
= false;
11594 this.toggle( false );
11603 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
11607 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
11610 if ( !this.newItems
) {
11611 this.newItems
= [];
11614 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11616 if ( this.isVisible() ) {
11617 // Defer fitting label until item has been attached
11620 this.newItems
.push( item
);
11624 // Reevaluate clipping
11633 OO
.ui
.MenuWidget
.prototype.removeItems = function ( items
) {
11635 OO
.ui
.MenuWidget
.super.prototype.removeItems
.call( this, items
);
11637 // Reevaluate clipping
11646 OO
.ui
.MenuWidget
.prototype.clearItems = function () {
11648 OO
.ui
.MenuWidget
.super.prototype.clearItems
.call( this );
11650 // Reevaluate clipping
11659 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
11660 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
11663 change
= visible
!== this.isVisible(),
11664 elementDoc
= this.getElementDocument(),
11665 widgetDoc
= this.$widget
? this.$widget
[0].ownerDocument
: null;
11668 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
11672 this.bindKeyDownListener();
11674 // Change focus to enable keyboard navigation
11675 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
11676 this.$previousFocus
= this.$( ':focus' );
11677 this.$input
[0].focus();
11679 if ( this.newItems
&& this.newItems
.length
) {
11680 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
11681 this.newItems
[i
].fitLabel();
11683 this.newItems
= null;
11685 this.toggleClipping( true );
11688 if ( this.autoHide
) {
11689 elementDoc
.addEventListener(
11690 'mousedown', this.onDocumentMouseDownHandler
, true
11692 // Support $widget being in a different document
11693 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11694 widgetDoc
.addEventListener(
11695 'mousedown', this.onDocumentMouseDownHandler
, true
11700 this.unbindKeyDownListener();
11701 if ( this.isolated
&& this.$previousFocus
) {
11702 this.$previousFocus
[0].focus();
11703 this.$previousFocus
= null;
11705 elementDoc
.removeEventListener(
11706 'mousedown', this.onDocumentMouseDownHandler
, true
11708 // Support $widget being in a different document
11709 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11710 widgetDoc
.removeEventListener(
11711 'mousedown', this.onDocumentMouseDownHandler
, true
11714 this.toggleClipping( false );
11722 * Menu for a text input widget.
11724 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11725 * is in a different frame, the menu's position is automatically calculated and maintained when the
11726 * menu is toggled or the window is resized.
11729 * @extends OO.ui.MenuWidget
11732 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11733 * @param {Object} [config] Configuration options
11734 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11736 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
11737 // Configuration intialization
11738 config
= config
|| {};
11740 // Parent constructor
11741 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
11744 this.input
= input
;
11745 this.$container
= config
.$container
|| this.input
.$element
;
11746 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
11749 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
11754 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
11759 * Handle window resize event.
11761 * @param {jQuery.Event} e Window resize event
11763 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
11770 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
11771 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
11773 var change
= visible
!== this.isVisible();
11775 if ( change
&& visible
) {
11776 // Make sure the width is set before the parent method runs.
11777 // After this we have to call this.position(); again to actually
11778 // position ourselves correctly.
11783 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
11786 if ( this.isVisible() ) {
11788 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11790 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
11798 * Position the menu.
11802 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
11803 var $container
= this.$container
,
11804 pos
= OO
.ui
.Element
.getRelativePosition( $container
, this.$element
.offsetParent() );
11806 // Position under input
11807 pos
.top
+= $container
.height();
11808 this.$element
.css( pos
);
11811 this.setIdealSize( $container
.width() );
11812 // We updated the position, so re-evaluate the clipping state
11819 * Structured list of items.
11821 * Use with OO.ui.OutlineItemWidget.
11824 * @extends OO.ui.SelectWidget
11827 * @param {Object} [config] Configuration options
11829 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
11830 // Configuration initialization
11831 config
= config
|| {};
11833 // Parent constructor
11834 OO
.ui
.OutlineWidget
.super.call( this, config
);
11837 this.$element
.addClass( 'oo-ui-outlineWidget' );
11842 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
11845 * Switch that slides on and off.
11848 * @extends OO.ui.Widget
11849 * @mixins OO.ui.ToggleWidget
11852 * @param {Object} [config] Configuration options
11853 * @cfg {boolean} [value=false] Initial value
11855 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
11856 // Parent constructor
11857 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
11859 // Mixin constructors
11860 OO
.ui
.ToggleWidget
.call( this, config
);
11863 this.dragging
= false;
11864 this.dragStart
= null;
11865 this.sliding
= false;
11866 this.$glow
= this.$( '<span>' );
11867 this.$grip
= this.$( '<span>' );
11870 this.$element
.on( 'click', this.onClick
.bind( this ) );
11873 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
11874 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
11876 .addClass( 'oo-ui-toggleSwitchWidget' )
11877 .append( this.$glow
, this.$grip
);
11882 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
11883 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
11888 * Handle mouse down events.
11890 * @param {jQuery.Event} e Mouse down event
11892 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
11893 if ( !this.isDisabled() && e
.which
=== 1 ) {
11894 this.setValue( !this.value
);