2 * OOjs UI v0.1.0-pre (d4cfcce969)
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-21T00:12:34Z
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
97 * Check if a node is contained within another node
99 * Similar to jQuery#contains except a list of containers can be supplied
100 * and a boolean argument allows you to include the container in the match list
102 * @param {HTMLElement|HTMLElement[]} containers Container node(s) to search in
103 * @param {HTMLElement} contained Node to find
104 * @param {boolean} [matchContainers] Include the container(s) in the list of nodes to match, otherwise only match descendents
105 * @return {boolean} The node is in the list of target nodes
107 OO
.ui
.contains = function ( containers
, contained
, matchContainers
) {
109 if ( !Array
.isArray( containers
) ) {
110 containers
= [ containers
];
112 for ( i
= containers
.length
- 1; i
>= 0; i
-- ) {
113 if ( ( matchContainers
&& contained
=== containers
[i
] ) || $.contains( containers
[i
], contained
) ) {
122 * Message store for the default implementation of OO.ui.msg
124 * Environments that provide a localization system should not use this, but should override
125 * OO.ui.msg altogether.
130 // Tool tip for a button that moves items in a list down one place
131 'ooui-outline-control-move-down': 'Move item down',
132 // Tool tip for a button that moves items in a list up one place
133 'ooui-outline-control-move-up': 'Move item up',
134 // Tool tip for a button that removes items from a list
135 'ooui-outline-control-remove': 'Remove item',
136 // Label for the toolbar group that contains a list of all other available tools
137 'ooui-toolbar-more': 'More',
138 // Label for the fake tool that expands the full list of tools in a toolbar group
139 'ooui-toolgroup-expand': 'More',
140 // Label for the fake tool that collapses the full list of tools in a toolbar group
141 'ooui-toolgroup-collapse': 'Fewer',
142 // Default label for the accept button of a confirmation dialog
143 'ooui-dialog-message-accept': 'OK',
144 // Default label for the reject button of a confirmation dialog
145 'ooui-dialog-message-reject': 'Cancel',
146 // Title for process dialog error description
147 'ooui-dialog-process-error': 'Something went wrong',
148 // Label for process dialog dismiss error button, visible when describing errors
149 'ooui-dialog-process-dismiss': 'Dismiss',
150 // Label for process dialog retry action button, visible when describing only recoverable errors
151 'ooui-dialog-process-retry': 'Try again',
152 // Label for process dialog retry action button, visible when describing only warnings
153 'ooui-dialog-process-continue': 'Continue'
157 * Get a localized message.
159 * In environments that provide a localization system, this function should be overridden to
160 * return the message translated in the user's language. The default implementation always returns
163 * After the message key, message parameters may optionally be passed. In the default implementation,
164 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
165 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
166 * they support unnamed, ordered message parameters.
169 * @param {string} key Message key
170 * @param {Mixed...} [params] Message parameters
171 * @return {string} Translated message with parameters substituted
173 OO
.ui
.msg = function ( key
) {
174 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
175 if ( typeof message
=== 'string' ) {
176 // Perform $1 substitution
177 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
178 var i
= parseInt( n
, 10 );
179 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
182 // Return placeholder if message not found
183 message
= '[' + key
+ ']';
189 * Package a message and arguments for deferred resolution.
191 * Use this when you are statically specifying a message and the message may not yet be present.
193 * @param {string} key Message key
194 * @param {Mixed...} [params] Message parameters
195 * @return {Function} Function that returns the resolved message when executed
197 OO
.ui
.deferMsg = function () {
198 var args
= arguments
;
200 return OO
.ui
.msg
.apply( OO
.ui
, args
);
207 * If the message is a function it will be executed, otherwise it will pass through directly.
209 * @param {Function|string} msg Deferred message, or message text
210 * @return {string} Resolved message
212 OO
.ui
.resolveMsg = function ( msg
) {
213 if ( $.isFunction( msg
) ) {
222 * Element that can be marked as pending.
228 * @param {Object} [config] Configuration options
229 * @cfg {jQuery} [$pending] Element to mark as pending, defaults to this.$element
231 OO
.ui
.PendingElement
= function OoUiPendingElement( config
) {
232 // Configuration initialization
233 config
= config
|| {};
237 this.$pending
= null;
240 this.setPendingElement( config
.$pending
|| this.$element
);
245 OO
.initClass( OO
.ui
.PendingElement
);
250 * Set the pending element (and clean up any existing one).
252 * @param {jQuery} $pending The element to set to pending.
254 OO
.ui
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
255 if ( this.$pending
) {
256 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
259 this.$pending
= $pending
;
260 if ( this.pending
> 0 ) {
261 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
266 * Check if input is pending.
270 OO
.ui
.PendingElement
.prototype.isPending = function () {
271 return !!this.pending
;
275 * Increase the pending stack.
279 OO
.ui
.PendingElement
.prototype.pushPending = function () {
280 if ( this.pending
=== 0 ) {
281 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
282 this.updateThemeClasses();
290 * Reduce the pending stack.
296 OO
.ui
.PendingElement
.prototype.popPending = function () {
297 if ( this.pending
=== 1 ) {
298 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
299 this.updateThemeClasses();
301 this.pending
= Math
.max( 0, this.pending
- 1 );
311 * @mixins OO.EventEmitter
314 * @param {Object} [config] Configuration options
316 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
317 // Configuration intialization
318 config
= config
|| {};
320 // Mixin constructors
321 OO
.EventEmitter
.call( this );
326 actions
: 'getAction',
330 this.categorized
= {};
333 this.organized
= false;
334 this.changing
= false;
335 this.changed
= false;
340 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
342 /* Static Properties */
345 * Symbolic name of dialog.
352 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
358 * @param {OO.ui.ActionWidget} action Action that was clicked
363 * @param {OO.ui.ActionWidget} action Action that was resized
368 * @param {OO.ui.ActionWidget[]} added Actions added
373 * @param {OO.ui.ActionWidget[]} added Actions removed
383 * Handle action change events.
387 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
388 this.organized
= false;
389 if ( this.changing
) {
392 this.emit( 'change' );
397 * Check if a action is one of the special actions.
399 * @param {OO.ui.ActionWidget} action Action to check
400 * @return {boolean} Action is special
402 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
405 for ( flag
in this.special
) {
406 if ( action
=== this.special
[flag
] ) {
417 * @param {Object} [filters] Filters to use, omit to get all actions
418 * @param {string|string[]} [filters.actions] Actions that actions must have
419 * @param {string|string[]} [filters.flags] Flags that actions must have
420 * @param {string|string[]} [filters.modes] Modes that actions must have
421 * @param {boolean} [filters.visible] Actions must be visible
422 * @param {boolean} [filters.disabled] Actions must be disabled
423 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
425 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
426 var i
, len
, list
, category
, actions
, index
, match
, matches
;
431 // Collect category candidates
433 for ( category
in this.categorized
) {
434 list
= filters
[category
];
436 if ( !Array
.isArray( list
) ) {
439 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
440 actions
= this.categorized
[category
][list
[i
]];
441 if ( Array
.isArray( actions
) ) {
442 matches
.push
.apply( matches
, actions
);
447 // Remove by boolean filters
448 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
451 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
452 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
454 matches
.splice( i
, 1 );
460 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
462 index
= matches
.lastIndexOf( match
);
463 while ( index
!== i
) {
464 matches
.splice( index
, 1 );
466 index
= matches
.lastIndexOf( match
);
471 return this.list
.slice();
475 * Get special actions.
477 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
478 * Special flags can be configured by changing #static-specialFlags in a subclass.
480 * @return {OO.ui.ActionWidget|null} Safe action
482 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
484 return $.extend( {}, this.special
);
490 * Other actions include all non-special visible actions.
492 * @return {OO.ui.ActionWidget[]} Other actions
494 OO
.ui
.ActionSet
.prototype.getOthers = function () {
496 return this.others
.slice();
500 * Toggle actions based on their modes.
502 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
503 * visibility; matching actions will be shown, non-matching actions will be hidden.
505 * @param {string} mode Mode actions must have
510 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
513 this.changing
= true;
514 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
515 action
= this.list
[i
];
516 action
.toggle( action
.hasMode( mode
) );
519 this.organized
= false;
520 this.changing
= false;
521 this.emit( 'change' );
527 * Change which actions are able to be performed.
529 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
531 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
532 * indicate actions are able to be performed
535 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
536 var i
, len
, action
, item
;
538 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
540 action
= item
.getAction();
541 if ( actions
[action
] !== undefined ) {
542 item
.setDisabled( !actions
[action
] );
550 * Executes a function once per action.
552 * When making changes to multiple actions, use this method instead of iterating over the actions
553 * manually to defer emitting a change event until after all actions have been changed.
555 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
556 * @param {Function} callback Callback to run for each action; callback is invoked with three
557 * arguments: the action, the action's index, the list of actions being iterated over
560 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
561 this.changed
= false;
562 this.changing
= true;
563 this.get( filter
).forEach( callback
);
564 this.changing
= false;
565 if ( this.changed
) {
566 this.emit( 'change' );
575 * @param {OO.ui.ActionWidget[]} actions Actions to add
580 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
583 this.changing
= true;
584 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
586 action
.connect( this, {
587 click
: [ 'emit', 'click', action
],
588 resize
: [ 'emit', 'resize', action
],
589 toggle
: [ 'onActionChange' ]
591 this.list
.push( action
);
593 this.organized
= false;
594 this.emit( 'add', actions
);
595 this.changing
= false;
596 this.emit( 'change' );
604 * @param {OO.ui.ActionWidget[]} actions Actions to remove
609 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
610 var i
, len
, index
, action
;
612 this.changing
= true;
613 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
615 index
= this.list
.indexOf( action
);
616 if ( index
!== -1 ) {
617 action
.disconnect( this );
618 this.list
.splice( index
, 1 );
621 this.organized
= false;
622 this.emit( 'remove', actions
);
623 this.changing
= false;
624 this.emit( 'change' );
630 * Remove all actions.
636 OO
.ui
.ActionSet
.prototype.clear = function () {
638 removed
= this.list
.slice();
640 this.changing
= true;
641 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
642 action
= this.list
[i
];
643 action
.disconnect( this );
648 this.organized
= false;
649 this.emit( 'remove', removed
);
650 this.changing
= false;
651 this.emit( 'change' );
659 * This is called whenver organized information is requested. It will only reorganize the actions
660 * if something has changed since the last time it ran.
665 OO
.ui
.ActionSet
.prototype.organize = function () {
666 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
667 specialFlags
= this.constructor.static.specialFlags
;
669 if ( !this.organized
) {
670 this.categorized
= {};
673 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
674 action
= this.list
[i
];
675 if ( action
.isVisible() ) {
676 // Populate catgeories
677 for ( category
in this.categories
) {
678 if ( !this.categorized
[category
] ) {
679 this.categorized
[category
] = {};
681 list
= action
[this.categories
[category
]]();
682 if ( !Array
.isArray( list
) ) {
685 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
687 if ( !this.categorized
[category
][item
] ) {
688 this.categorized
[category
][item
] = [];
690 this.categorized
[category
][item
].push( action
);
693 // Populate special/others
695 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
696 flag
= specialFlags
[j
];
697 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
698 this.special
[flag
] = action
;
704 this.others
.push( action
);
708 this.organized
= true;
715 * DOM element abstraction.
721 * @param {Object} [config] Configuration options
722 * @cfg {Function} [$] jQuery for the frame the widget is in
723 * @cfg {string[]} [classes] CSS class names to add
724 * @cfg {string} [text] Text to insert
725 * @cfg {jQuery} [$content] Content elements to append (after text)
727 OO
.ui
.Element
= function OoUiElement( config
) {
728 // Configuration initialization
729 config
= config
|| {};
732 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
733 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
734 this.elementGroup
= null;
735 this.debouncedUpdateThemeClassesHandler
= this.debouncedUpdateThemeClasses
.bind( this );
736 this.updateThemeClassesPending
= false;
739 if ( $.isArray( config
.classes
) ) {
740 this.$element
.addClass( config
.classes
.join( ' ' ) );
743 this.$element
.text( config
.text
);
745 if ( config
.$content
) {
746 this.$element
.append( config
.$content
);
752 OO
.initClass( OO
.ui
.Element
);
754 /* Static Properties */
759 * This may be ignored if #getTagName is overridden.
765 OO
.ui
.Element
.static.tagName
= 'div';
770 * Get a jQuery function within a specific document.
773 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
774 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
776 * @return {Function} Bound jQuery function
778 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
779 function wrapper( selector
) {
780 return $( selector
, wrapper
.context
);
783 wrapper
.context
= this.getDocument( context
);
786 wrapper
.$iframe
= $iframe
;
793 * Get the document of an element.
796 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
797 * @return {HTMLDocument|null} Document object
799 OO
.ui
.Element
.getDocument = function ( obj
) {
800 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
801 return ( obj
[0] && obj
[0].ownerDocument
) ||
802 // Empty jQuery selections might have a context
809 ( obj
.nodeType
=== 9 && obj
) ||
814 * Get the window of an element or document.
817 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
818 * @return {Window} Window object
820 OO
.ui
.Element
.getWindow = function ( obj
) {
821 var doc
= this.getDocument( obj
);
822 return doc
.parentWindow
|| doc
.defaultView
;
826 * Get the direction of an element or document.
829 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
830 * @return {string} Text direction, either 'ltr' or 'rtl'
832 OO
.ui
.Element
.getDir = function ( obj
) {
835 if ( obj
instanceof jQuery
) {
838 isDoc
= obj
.nodeType
=== 9;
839 isWin
= obj
.document
!== undefined;
840 if ( isDoc
|| isWin
) {
846 return $( obj
).css( 'direction' );
850 * Get the offset between two frames.
852 * TODO: Make this function not use recursion.
855 * @param {Window} from Window of the child frame
856 * @param {Window} [to=window] Window of the parent frame
857 * @param {Object} [offset] Offset to start with, used internally
858 * @return {Object} Offset object, containing left and top properties
860 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
861 var i
, len
, frames
, frame
, rect
;
867 offset
= { top
: 0, left
: 0 };
869 if ( from.parent
=== from ) {
873 // Get iframe element
874 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
875 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
876 if ( frames
[i
].contentWindow
=== from ) {
882 // Recursively accumulate offset values
884 rect
= frame
.getBoundingClientRect();
885 offset
.left
+= rect
.left
;
886 offset
.top
+= rect
.top
;
888 this.getFrameOffset( from.parent
, offset
);
895 * Get the offset between two elements.
897 * The two elements may be in a different frame, but in that case the frame $element is in must
898 * be contained in the frame $anchor is in.
901 * @param {jQuery} $element Element whose position to get
902 * @param {jQuery} $anchor Element to get $element's position relative to
903 * @return {Object} Translated position coordinates, containing top and left properties
905 OO
.ui
.Element
.getRelativePosition = function ( $element
, $anchor
) {
906 var iframe
, iframePos
,
907 pos
= $element
.offset(),
908 anchorPos
= $anchor
.offset(),
909 elementDocument
= this.getDocument( $element
),
910 anchorDocument
= this.getDocument( $anchor
);
912 // If $element isn't in the same document as $anchor, traverse up
913 while ( elementDocument
!== anchorDocument
) {
914 iframe
= elementDocument
.defaultView
.frameElement
;
916 throw new Error( '$element frame is not contained in $anchor frame' );
918 iframePos
= $( iframe
).offset();
919 pos
.left
+= iframePos
.left
;
920 pos
.top
+= iframePos
.top
;
921 elementDocument
= iframe
.ownerDocument
;
923 pos
.left
-= anchorPos
.left
;
924 pos
.top
-= anchorPos
.top
;
929 * Get element border sizes.
932 * @param {HTMLElement} el Element to measure
933 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
935 OO
.ui
.Element
.getBorders = function ( el
) {
936 var doc
= el
.ownerDocument
,
937 win
= doc
.parentWindow
|| doc
.defaultView
,
938 style
= win
&& win
.getComputedStyle
?
939 win
.getComputedStyle( el
, null ) :
942 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
943 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
944 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
945 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
948 top
: Math
.round( top
),
949 left
: Math
.round( left
),
950 bottom
: Math
.round( bottom
),
951 right
: Math
.round( right
)
956 * Get dimensions of an element or window.
959 * @param {HTMLElement|Window} el Element to measure
960 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
962 OO
.ui
.Element
.getDimensions = function ( el
) {
964 doc
= el
.ownerDocument
|| el
.document
,
965 win
= doc
.parentWindow
|| doc
.defaultView
;
967 if ( win
=== el
|| el
=== doc
.documentElement
) {
970 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
972 top
: $win
.scrollTop(),
973 left
: $win
.scrollLeft()
975 scrollbar
: { right
: 0, bottom
: 0 },
979 bottom
: $win
.innerHeight(),
980 right
: $win
.innerWidth()
986 borders
: this.getBorders( el
),
988 top
: $el
.scrollTop(),
989 left
: $el
.scrollLeft()
992 right
: $el
.innerWidth() - el
.clientWidth
,
993 bottom
: $el
.innerHeight() - el
.clientHeight
995 rect
: el
.getBoundingClientRect()
1001 * Get closest scrollable container.
1003 * Traverses up until either a scrollable element or the root is reached, in which case the window
1007 * @param {HTMLElement} el Element to find scrollable container for
1008 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
1009 * @return {HTMLElement} Closest scrollable container
1011 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
1013 props
= [ 'overflow' ],
1014 $parent
= $( el
).parent();
1016 if ( dimension
=== 'x' || dimension
=== 'y' ) {
1017 props
.push( 'overflow-' + dimension
);
1020 while ( $parent
.length
) {
1021 if ( $parent
[0] === el
.ownerDocument
.body
) {
1026 val
= $parent
.css( props
[i
] );
1027 if ( val
=== 'auto' || val
=== 'scroll' ) {
1031 $parent
= $parent
.parent();
1033 return this.getDocument( el
).body
;
1037 * Scroll element into view.
1040 * @param {HTMLElement} el Element to scroll into view
1041 * @param {Object} [config] Configuration options
1042 * @param {string} [config.duration] jQuery animation duration value
1043 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
1044 * to scroll in both directions
1045 * @param {Function} [config.complete] Function to call when scrolling completes
1047 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
1048 // Configuration initialization
1049 config
= config
|| {};
1052 callback
= typeof config
.complete
=== 'function' && config
.complete
,
1053 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
1055 eld
= this.getDimensions( el
),
1056 scd
= this.getDimensions( sc
),
1057 $win
= $( this.getWindow( el
) );
1059 // Compute the distances between the edges of el and the edges of the scroll viewport
1060 if ( $sc
.is( 'body' ) ) {
1061 // If the scrollable container is the <body> this is easy
1064 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
1065 left
: eld
.rect
.left
,
1066 right
: $win
.innerWidth() - eld
.rect
.right
1069 // Otherwise, we have to subtract el's coordinates from sc's coordinates
1071 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
1072 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
1073 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
1074 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
1078 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1079 if ( rel
.top
< 0 ) {
1080 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
1081 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
1082 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
1085 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1086 if ( rel
.left
< 0 ) {
1087 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
1088 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
1089 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
1092 if ( !$.isEmptyObject( anim
) ) {
1093 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
1095 $sc
.queue( function ( next
) {
1108 * Bind a handler for an event on a DOM element.
1110 * Used to be for working around a jQuery bug (jqbug.com/14180),
1111 * but obsolete as of jQuery 1.11.0.
1114 * @deprecated Use jQuery#on instead.
1115 * @param {HTMLElement|jQuery} el DOM element
1116 * @param {string} event Event to bind
1117 * @param {Function} callback Callback to call when the event fires
1119 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1120 $( el
).on( event
, callback
);
1124 * Unbind a handler bound with #static-method-onDOMEvent.
1126 * @deprecated Use jQuery#off instead.
1128 * @param {HTMLElement|jQuery} el DOM element
1129 * @param {string} event Event to unbind
1130 * @param {Function} [callback] Callback to unbind
1132 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1133 $( el
).off( event
, callback
);
1139 * Check if element supports one or more methods.
1141 * @param {string|string[]} methods Method or list of methods to check
1142 * @return {boolean} All methods are supported
1144 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1148 methods
= $.isArray( methods
) ? methods
: [ methods
];
1149 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1150 if ( $.isFunction( this[methods
[i
]] ) ) {
1155 return methods
.length
=== support
;
1159 * Update the theme-provided classes.
1161 * @localdoc This is called in element mixins and widget classes anytime state changes.
1162 * Updating is debounced, minimizing overhead of changing multiple attributes and
1163 * guaranteeing that theme updates do not occur within an element's constructor
1165 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1166 if ( !this.updateThemeClassesPending
) {
1167 this.updateThemeClassesPending
= true;
1168 setTimeout( this.debouncedUpdateThemeClassesHandler
);
1175 OO
.ui
.Element
.prototype.debouncedUpdateThemeClasses = function () {
1176 OO
.ui
.theme
.updateElementClasses( this );
1177 this.updateThemeClassesPending
= false;
1181 * Get the HTML tag name.
1183 * Override this method to base the result on instance information.
1185 * @return {string} HTML tag name
1187 OO
.ui
.Element
.prototype.getTagName = function () {
1188 return this.constructor.static.tagName
;
1192 * Check if the element is attached to the DOM
1193 * @return {boolean} The element is attached to the DOM
1195 OO
.ui
.Element
.prototype.isElementAttached = function () {
1196 return $.contains( this.getElementDocument(), this.$element
[0] );
1200 * Get the DOM document.
1202 * @return {HTMLDocument} Document object
1204 OO
.ui
.Element
.prototype.getElementDocument = function () {
1205 return OO
.ui
.Element
.getDocument( this.$element
);
1209 * Get the DOM window.
1211 * @return {Window} Window object
1213 OO
.ui
.Element
.prototype.getElementWindow = function () {
1214 return OO
.ui
.Element
.getWindow( this.$element
);
1218 * Get closest scrollable container.
1220 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1221 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1225 * Get group element is in.
1227 * @return {OO.ui.GroupElement|null} Group element, null if none
1229 OO
.ui
.Element
.prototype.getElementGroup = function () {
1230 return this.elementGroup
;
1234 * Set group element is in.
1236 * @param {OO.ui.GroupElement|null} group Group element, null if none
1239 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1240 this.elementGroup
= group
;
1245 * Scroll element into view.
1247 * @param {Object} [config] Configuration options
1249 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1250 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1254 * Bind a handler for an event on this.$element
1256 * @deprecated Use jQuery#on instead.
1257 * @param {string} event
1258 * @param {Function} callback
1260 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1261 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1265 * Unbind a handler bound with #offDOMEvent
1267 * @deprecated Use jQuery#off instead.
1268 * @param {string} event
1269 * @param {Function} callback
1271 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1272 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1276 * Container for elements.
1280 * @extends OO.ui.Element
1281 * @mixins OO.EventEmitter
1284 * @param {Object} [config] Configuration options
1286 OO
.ui
.Layout
= function OoUiLayout( config
) {
1287 // Configuration initialization
1288 config
= config
|| {};
1290 // Parent constructor
1291 OO
.ui
.Layout
.super.call( this, config
);
1293 // Mixin constructors
1294 OO
.EventEmitter
.call( this );
1297 this.$element
.addClass( 'oo-ui-layout' );
1302 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1303 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1306 * User interface control.
1310 * @extends OO.ui.Element
1311 * @mixins OO.EventEmitter
1314 * @param {Object} [config] Configuration options
1315 * @cfg {boolean} [disabled=false] Disable
1317 OO
.ui
.Widget
= function OoUiWidget( config
) {
1318 // Initialize config
1319 config
= $.extend( { disabled
: false }, config
);
1321 // Parent constructor
1322 OO
.ui
.Widget
.super.call( this, config
);
1324 // Mixin constructors
1325 OO
.EventEmitter
.call( this );
1328 this.visible
= true;
1329 this.disabled
= null;
1330 this.wasDisabled
= null;
1333 this.$element
.addClass( 'oo-ui-widget' );
1334 this.setDisabled( !!config
.disabled
);
1339 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1340 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1346 * @param {boolean} disabled Widget is disabled
1351 * @param {boolean} visible Widget is visible
1357 * Check if the widget is disabled.
1359 * @return {boolean} Button is disabled
1361 OO
.ui
.Widget
.prototype.isDisabled = function () {
1362 return this.disabled
;
1366 * Check if widget is visible.
1368 * @return {boolean} Widget is visible
1370 OO
.ui
.Widget
.prototype.isVisible = function () {
1371 return this.visible
;
1375 * Set the disabled state of the widget.
1377 * This should probably change the widgets' appearance and prevent it from being used.
1379 * @param {boolean} disabled Disable widget
1382 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1385 this.disabled
= !!disabled
;
1386 isDisabled
= this.isDisabled();
1387 if ( isDisabled
!== this.wasDisabled
) {
1388 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1389 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1390 this.emit( 'disable', isDisabled
);
1391 this.updateThemeClasses();
1393 this.wasDisabled
= isDisabled
;
1399 * Toggle visibility of widget.
1401 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1405 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1406 show
= show
=== undefined ? !this.visible
: !!show
;
1408 if ( show
!== this.isVisible() ) {
1409 this.visible
= show
;
1410 this.$element
.toggle( show
);
1411 this.emit( 'toggle', show
);
1418 * Update the disabled state, in case of changes in parent widget.
1422 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1423 this.setDisabled( this.disabled
);
1428 * Container for elements in a child frame.
1430 * Use together with OO.ui.WindowManager.
1434 * @extends OO.ui.Element
1435 * @mixins OO.EventEmitter
1437 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1438 * teardown processes are executed when the window is closed.
1440 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1441 * - Window manager begins opening window
1442 * - {@link #getSetupProcess} method is called and its result executed
1443 * - {@link #getReadyProcess} method is called and its result executed
1444 * - Window is now open
1446 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1447 * - Window manager begins closing window
1448 * - {@link #getHoldProcess} method is called and its result executed
1449 * - {@link #getTeardownProcess} method is called and its result executed
1450 * - Window is now closed
1452 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1453 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1454 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1455 * processing can complete. Always assume window processes are executed asychronously. See
1456 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1457 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1459 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1460 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1463 * @param {Object} [config] Configuration options
1464 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1468 OO
.ui
.Window
= function OoUiWindow( config
) {
1469 // Configuration initialization
1470 config
= config
|| {};
1472 // Parent constructor
1473 OO
.ui
.Window
.super.call( this, config
);
1475 // Mixin constructors
1476 OO
.EventEmitter
.call( this );
1479 this.manager
= null;
1480 this.initialized
= false;
1481 this.visible
= false;
1482 this.opening
= null;
1483 this.closing
= null;
1486 this.loading
= null;
1487 this.size
= config
.size
|| this.constructor.static.size
;
1488 this.$frame
= this.$( '<div>' );
1489 this.$overlay
= this.$( '<div>' );
1493 .addClass( 'oo-ui-window' )
1494 .append( this.$frame
, this.$overlay
);
1495 this.$frame
.addClass( 'oo-ui-window-frame' );
1496 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1498 // NOTE: Additional intitialization will occur when #setManager is called
1503 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1504 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1506 /* Static Properties */
1509 * Symbolic name of size.
1511 * Size is used if no size is configured during construction.
1515 * @property {string}
1517 OO
.ui
.Window
.static.size
= 'medium';
1519 /* Static Methods */
1522 * Transplant the CSS styles from as parent document to a frame's document.
1524 * This loops over the style sheets in the parent document, and copies their nodes to the
1525 * frame's document. It then polls the document to see when all styles have loaded, and once they
1526 * have, resolves the promise.
1528 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1529 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1530 * Firefox, where the styles won't load until the iframe becomes visible.
1532 * For details of how we arrived at the strategy used in this function, see #load.
1536 * @param {HTMLDocument} parentDoc Document to transplant styles from
1537 * @param {HTMLDocument} frameDoc Document to transplant styles to
1538 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1539 * @return {jQuery.Promise} Promise resolved when styles have loaded
1541 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1542 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1543 $pollNodes
= $( [] ),
1544 // Fake font-family value
1545 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1546 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1547 deferred
= $.Deferred();
1549 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1550 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1551 if ( styleNode
.disabled
) {
1555 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1556 // External stylesheet; use @import
1557 styleText
= '@import url(' + styleNode
.href
+ ');';
1559 // Internal stylesheet; just copy the text
1560 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1561 // other browsers, so fall back to '' rather than 'undefined'
1562 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1565 // Create a node with a unique ID that we're going to monitor to see when the CSS
1567 if ( styleNode
.oouiFrameTransplantStylesId
) {
1568 // If we're nesting transplantStyles operations and this node already has
1569 // a CSS rule to wait for loading, reuse it
1570 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1572 // Otherwise, create a new ID
1573 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1576 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1577 // The font-family rule will only take effect once the @import finishes
1578 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1581 // Create a node with id=pollNodeId
1582 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1583 .attr( 'id', pollNodeId
)
1584 .appendTo( frameDoc
.body
)
1587 // Add our modified CSS as a <style> tag
1588 newNode
= frameDoc
.createElement( 'style' );
1589 newNode
.textContent
= styleText
;
1590 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1591 frameDoc
.head
.appendChild( newNode
);
1593 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1595 // Poll every 100ms until all external stylesheets have loaded
1596 $pendingPollNodes
= $pollNodes
;
1597 timeoutID
= setTimeout( function pollExternalStylesheets() {
1599 $pendingPollNodes
.length
> 0 &&
1600 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1602 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1605 if ( $pendingPollNodes
.length
=== 0 ) {
1607 if ( timeoutID
!== null ) {
1609 $pollNodes
.remove();
1613 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1616 // ...but give up after a while
1617 if ( timeout
!== 0 ) {
1618 setTimeout( function () {
1620 clearTimeout( timeoutID
);
1622 $pollNodes
.remove();
1625 }, timeout
|| 5000 );
1628 return deferred
.promise();
1634 * Handle mouse down events.
1636 * @param {jQuery.Event} e Mouse down event
1638 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1639 // Prevent clicking on the click-block from stealing focus
1640 if ( e
.target
=== this.$element
[0] ) {
1646 * Check if window has been initialized.
1648 * @return {boolean} Window has been initialized
1650 OO
.ui
.Window
.prototype.isInitialized = function () {
1651 return this.initialized
;
1655 * Check if window is visible.
1657 * @return {boolean} Window is visible
1659 OO
.ui
.Window
.prototype.isVisible = function () {
1660 return this.visible
;
1664 * Check if window is loading.
1666 * @return {boolean} Window is loading
1668 OO
.ui
.Window
.prototype.isLoading = function () {
1669 return this.loading
&& this.loading
.state() === 'pending';
1673 * Check if window is loaded.
1675 * @return {boolean} Window is loaded
1677 OO
.ui
.Window
.prototype.isLoaded = function () {
1678 return this.loading
&& this.loading
.state() === 'resolved';
1682 * Check if window is opening.
1684 * This is a wrapper around OO.ui.WindowManager#isOpening.
1686 * @return {boolean} Window is opening
1688 OO
.ui
.Window
.prototype.isOpening = function () {
1689 return this.manager
.isOpening( this );
1693 * Check if window is closing.
1695 * This is a wrapper around OO.ui.WindowManager#isClosing.
1697 * @return {boolean} Window is closing
1699 OO
.ui
.Window
.prototype.isClosing = function () {
1700 return this.manager
.isClosing( this );
1704 * Check if window is opened.
1706 * This is a wrapper around OO.ui.WindowManager#isOpened.
1708 * @return {boolean} Window is opened
1710 OO
.ui
.Window
.prototype.isOpened = function () {
1711 return this.manager
.isOpened( this );
1715 * Get the window manager.
1717 * @return {OO.ui.WindowManager} Manager of window
1719 OO
.ui
.Window
.prototype.getManager = function () {
1720 return this.manager
;
1724 * Get the window size.
1726 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1728 OO
.ui
.Window
.prototype.getSize = function () {
1733 * Get the height of the dialog contents.
1735 * @return {number} Content height
1737 OO
.ui
.Window
.prototype.getContentHeight = function () {
1738 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements
1739 var bodyHeight
, oldHeight
= this.$frame
[0].style
.height
;
1740 this.$frame
[0].style
.height
= '1px';
1741 bodyHeight
= this.getBodyHeight();
1742 this.$frame
[0].style
.height
= oldHeight
;
1745 // Add buffer for border
1746 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1747 // Use combined heights of children
1748 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1753 * Get the height of the dialog contents.
1755 * When this function is called, the dialog will temporarily have been resized
1756 * to height=1px, so .scrollHeight measurements can be taken accurately.
1758 * @return {number} Height of content
1760 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1761 return this.$body
[0].scrollHeight
;
1765 * Get the directionality of the frame
1767 * @return {string} Directionality, 'ltr' or 'rtl'
1769 OO
.ui
.Window
.prototype.getDir = function () {
1774 * Get a process for setting up a window for use.
1776 * Each time the window is opened this process will set it up for use in a particular context, based
1777 * on the `data` argument.
1779 * When you override this method, you can add additional setup steps to the process the parent
1780 * method provides using the 'first' and 'next' methods.
1783 * @param {Object} [data] Window opening data
1784 * @return {OO.ui.Process} Setup process
1786 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1787 return new OO
.ui
.Process();
1791 * Get a process for readying a window for use.
1793 * Each time the window is open and setup, this process will ready it up for use in a particular
1794 * context, based on the `data` argument.
1796 * When you override this method, you can add additional setup steps to the process the parent
1797 * method provides using the 'first' and 'next' methods.
1800 * @param {Object} [data] Window opening data
1801 * @return {OO.ui.Process} Setup process
1803 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1804 return new OO
.ui
.Process();
1808 * Get a process for holding a window from use.
1810 * Each time the window is closed, this process will hold it from use in a particular context, based
1811 * on the `data` argument.
1813 * When you override this method, you can add additional setup steps to the process the parent
1814 * method provides using the 'first' and 'next' methods.
1817 * @param {Object} [data] Window closing data
1818 * @return {OO.ui.Process} Hold process
1820 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1821 return new OO
.ui
.Process();
1825 * Get a process for tearing down a window after use.
1827 * Each time the window is closed this process will tear it down and do something with the user's
1828 * interactions within the window, based on the `data` argument.
1830 * When you override this method, you can add additional teardown steps to the process the parent
1831 * method provides using the 'first' and 'next' methods.
1834 * @param {Object} [data] Window closing data
1835 * @return {OO.ui.Process} Teardown process
1837 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1838 return new OO
.ui
.Process();
1842 * Toggle visibility of window.
1844 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1845 * instead of display.
1847 * @param {boolean} [show] Make window visible, omit to toggle visibility
1851 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1852 show
= show
=== undefined ? !this.visible
: !!show
;
1854 if ( show
!== this.isVisible() ) {
1855 this.visible
= show
;
1857 if ( this.isolated
&& !this.isLoaded() ) {
1858 // Hide the window using visibility instead of display until loading is complete
1859 // Can't use display: none; because that prevents the iframe from loading in Firefox
1860 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1862 this.$element
.toggle( show
).css( 'visibility', '' );
1864 this.emit( 'toggle', show
);
1871 * Set the window manager.
1873 * This must be called before initialize. Calling it more than once will cause an error.
1875 * @param {OO.ui.WindowManager} manager Manager for this window
1876 * @throws {Error} If called more than once
1879 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1880 if ( this.manager
) {
1881 throw new Error( 'Cannot set window manager, window already has a manager' );
1885 this.manager
= manager
;
1886 this.isolated
= manager
.shouldIsolate();
1889 if ( this.isolated
) {
1890 this.$iframe
= this.$( '<iframe>' );
1891 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1892 this.$frame
.append( this.$iframe
);
1893 this.$ = function () {
1894 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1896 // WARNING: Do not use this.$ again until #initialize is called
1898 this.$content
= this.$( '<div>' );
1899 this.$document
= $( this.getElementDocument() );
1900 this.$content
.addClass( 'oo-ui-window-content' );
1901 this.$frame
.append( this.$content
);
1903 this.toggle( false );
1905 // Figure out directionality:
1906 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1912 * Set the window size.
1914 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1917 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1919 this.manager
.updateWindowSize( this );
1924 * Set window dimensions.
1926 * Properties are applied to the frame container.
1928 * @param {Object} dim CSS dimension properties
1929 * @param {string|number} [dim.width] Width
1930 * @param {string|number} [dim.minWidth] Minimum width
1931 * @param {string|number} [dim.maxWidth] Maximum width
1932 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1933 * @param {string|number} [dim.minWidth] Minimum height
1934 * @param {string|number} [dim.maxWidth] Maximum height
1937 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1938 // Apply width before height so height is not based on wrapping content using the wrong width
1940 width
: dim
.width
|| '',
1941 minWidth
: dim
.minWidth
|| '',
1942 maxWidth
: dim
.maxWidth
|| ''
1945 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1946 minHeight
: dim
.minHeight
|| '',
1947 maxHeight
: dim
.maxHeight
|| ''
1953 * Initialize window contents.
1955 * The first time the window is opened, #initialize is called when it's safe to begin populating
1956 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1958 * Once this method is called, this.$ can be used to create elements within the frame.
1960 * @throws {Error} If not attached to a manager
1963 OO
.ui
.Window
.prototype.initialize = function () {
1964 if ( !this.manager
) {
1965 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1969 this.$head
= this.$( '<div>' );
1970 this.$body
= this.$( '<div>' );
1971 this.$foot
= this.$( '<div>' );
1972 this.$innerOverlay
= this.$( '<div>' );
1975 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
1978 this.$head
.addClass( 'oo-ui-window-head' );
1979 this.$body
.addClass( 'oo-ui-window-body' );
1980 this.$foot
.addClass( 'oo-ui-window-foot' );
1981 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
1982 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
1990 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1991 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1993 * @param {Object} [data] Window opening data
1994 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1995 * first argument will be a promise which will be resolved when the window begins closing
1997 OO
.ui
.Window
.prototype.open = function ( data
) {
1998 return this.manager
.openWindow( this, data
);
2004 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
2005 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
2007 * @param {Object} [data] Window closing data
2008 * @return {jQuery.Promise} Promise resolved when window is closed
2010 OO
.ui
.Window
.prototype.close = function ( data
) {
2011 return this.manager
.closeWindow( this, data
);
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 setup
2023 OO
.ui
.Window
.prototype.setup = function ( data
) {
2025 deferred
= $.Deferred();
2027 this.$element
.show();
2028 this.visible
= true;
2029 this.getSetupProcess( data
).execute().done( function () {
2030 // Force redraw by asking the browser to measure the elements' widths
2031 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2032 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2036 return deferred
.promise();
2042 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
2045 * @param {Object} [data] Window opening data
2046 * @return {jQuery.Promise} Promise resolved when window is ready
2048 OO
.ui
.Window
.prototype.ready = function ( data
) {
2050 deferred
= $.Deferred();
2052 this.$content
.focus();
2053 this.getReadyProcess( data
).execute().done( function () {
2054 // Force redraw by asking the browser to measure the elements' widths
2055 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2056 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2060 return deferred
.promise();
2066 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2069 * @param {Object} [data] Window closing data
2070 * @return {jQuery.Promise} Promise resolved when window is held
2072 OO
.ui
.Window
.prototype.hold = function ( data
) {
2074 deferred
= $.Deferred();
2076 this.getHoldProcess( data
).execute().done( function () {
2077 // Get the focused element within the window's content
2078 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
2080 // Blur the focused element
2081 if ( $focus
.length
) {
2085 // Force redraw by asking the browser to measure the elements' widths
2086 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2087 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2091 return deferred
.promise();
2097 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2100 * @param {Object} [data] Window closing data
2101 * @return {jQuery.Promise} Promise resolved when window is torn down
2103 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2105 deferred
= $.Deferred();
2107 this.getTeardownProcess( data
).execute().done( function () {
2108 // Force redraw by asking the browser to measure the elements' widths
2109 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
2110 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2111 win
.$element
.hide();
2112 win
.visible
= false;
2116 return deferred
.promise();
2120 * Load the frame contents.
2122 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
2123 * promise will be resolved. Calling while loading will return a promise but not trigger a new
2124 * loading cycle. Calling after loading is complete will return a promise that's already been
2127 * Sounds simple right? Read on...
2129 * When you create a dynamic iframe using open/write/close, the window.load event for the
2130 * iframe is triggered when you call close, and there's no further load event to indicate that
2131 * everything is actually loaded.
2133 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2134 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2135 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2136 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2137 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2139 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2140 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2141 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2142 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2143 * accessing .cssRules is allowed.
2145 * However, now that we control the styles we're injecting, we might as well do away with
2146 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2147 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2148 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2149 * font-family rule is not applied until after the `@import` finishes.
2151 * All this stylesheet injection and polling magic is in #transplantStyles.
2153 * @return {jQuery.Promise} Promise resolved when loading is complete
2156 OO
.ui
.Window
.prototype.load = function () {
2157 var sub
, doc
, loading
,
2160 // Non-isolated windows are already "loaded"
2161 if ( !this.loading
&& !this.isolated
) {
2162 this.loading
= $.Deferred().resolve();
2164 // Set initialized state after so sub-classes aren't confused by it being set by calling
2165 // their parent initialize method
2166 this.initialized
= true;
2169 // Return existing promise if already loading or loaded
2170 if ( this.loading
) {
2171 return this.loading
.promise();
2175 loading
= this.loading
= $.Deferred();
2176 sub
= this.$iframe
.prop( 'contentWindow' );
2179 // Initialize contents
2184 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2185 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2186 '<div class="oo-ui-window-content"></div>' +
2193 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$iframe
);
2194 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2195 this.$document
= this.$( doc
);
2198 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2199 .always( function () {
2200 // Initialize isolated windows
2202 // Set initialized state after so sub-classes aren't confused by it being set by calling
2203 // their parent initialize method
2204 win
.initialized
= true;
2205 // Undo the visibility: hidden; hack and apply display: none;
2206 // We can do this safely now that the iframe has initialized
2207 // (don't do this from within #initialize because it has to happen
2208 // after the all subclasses have been handled as well).
2209 win
.toggle( win
.isVisible() );
2214 return loading
.promise();
2218 * Base class for all dialogs.
2221 * - Manage the window (open and close, etc.).
2222 * - Store the internal name and display title.
2223 * - A stack to track one or more pending actions.
2224 * - Manage a set of actions that can be performed.
2225 * - Configure and create action widgets.
2228 * - Close the dialog with Escape key.
2229 * - Visually lock the dialog while an action is in
2230 * progress (aka "pending").
2232 * Subclass responsibilities:
2233 * - Display the title somewhere.
2234 * - Add content to the dialog.
2235 * - Provide a UI to close the dialog.
2236 * - Display the action widgets somewhere.
2240 * @extends OO.ui.Window
2241 * @mixins OO.ui.PendingElement
2244 * @param {Object} [config] Configuration options
2246 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2247 // Parent constructor
2248 OO
.ui
.Dialog
.super.call( this, config
);
2250 // Mixin constructors
2251 OO
.ui
.PendingElement
.call( this );
2254 this.actions
= new OO
.ui
.ActionSet();
2255 this.attachedActions
= [];
2256 this.currentAction
= null;
2259 this.actions
.connect( this, {
2260 click
: 'onActionClick',
2261 resize
: 'onActionResize',
2262 change
: 'onActionsChange'
2267 .addClass( 'oo-ui-dialog' )
2268 .attr( 'role', 'dialog' );
2273 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2274 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2276 /* Static Properties */
2279 * Symbolic name of dialog.
2284 * @property {string}
2286 OO
.ui
.Dialog
.static.name
= '';
2294 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2296 OO
.ui
.Dialog
.static.title
= '';
2299 * List of OO.ui.ActionWidget configuration options.
2303 * @property {Object[]}
2305 OO
.ui
.Dialog
.static.actions
= [];
2308 * Close dialog when the escape key is pressed.
2313 * @property {boolean}
2315 OO
.ui
.Dialog
.static.escapable
= true;
2320 * Handle frame document key down events.
2322 * @param {jQuery.Event} e Key down event
2324 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2325 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2332 * Handle action resized events.
2334 * @param {OO.ui.ActionWidget} action Action that was resized
2336 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2337 // Override in subclass
2341 * Handle action click events.
2343 * @param {OO.ui.ActionWidget} action Action that was clicked
2345 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2346 if ( !this.isPending() ) {
2347 this.currentAction
= action
;
2348 this.executeAction( action
.getAction() );
2353 * Handle actions change event.
2355 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2356 this.detachActions();
2357 if ( !this.isClosing() ) {
2358 this.attachActions();
2363 * Get set of actions.
2365 * @return {OO.ui.ActionSet}
2367 OO
.ui
.Dialog
.prototype.getActions = function () {
2368 return this.actions
;
2372 * Get a process for taking action.
2374 * When you override this method, you can add additional accept steps to the process the parent
2375 * method provides using the 'first' and 'next' methods.
2378 * @param {string} [action] Symbolic name of action
2379 * @return {OO.ui.Process} Action process
2381 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2382 return new OO
.ui
.Process()
2383 .next( function () {
2385 // An empty action always closes the dialog without data, which should always be
2386 // safe and make no changes
2395 * @param {Object} [data] Dialog opening data
2396 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2397 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2398 * action item, omit to use #static-actions
2400 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2404 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2405 .next( function () {
2408 config
= this.constructor.static,
2409 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2411 this.title
.setLabel(
2412 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2414 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2416 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2419 this.actions
.add( items
);
2426 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2428 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2429 .first( function () {
2430 this.actions
.clear();
2431 this.currentAction
= null;
2438 OO
.ui
.Dialog
.prototype.initialize = function () {
2440 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2443 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2446 if ( this.constructor.static.escapable
) {
2447 this.$document
.on( 'keydown', this.onDocumentKeyDown
.bind( this ) );
2451 this.$content
.addClass( 'oo-ui-dialog-content' );
2452 this.setPendingElement( this.$head
);
2456 * Attach action actions.
2458 OO
.ui
.Dialog
.prototype.attachActions = function () {
2459 // Remember the list of potentially attached actions
2460 this.attachedActions
= this.actions
.get();
2464 * Detach action actions.
2468 OO
.ui
.Dialog
.prototype.detachActions = function () {
2471 // Detach all actions that may have been previously attached
2472 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2473 this.attachedActions
[i
].$element
.detach();
2475 this.attachedActions
= [];
2479 * Execute an action.
2481 * @param {string} action Symbolic name of action to execute
2482 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2484 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2486 return this.getActionProcess( action
).execute()
2487 .always( this.popPending
.bind( this ) );
2491 * Collection of windows.
2494 * @extends OO.ui.Element
2495 * @mixins OO.EventEmitter
2497 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2498 * already opening or opened, the current window will be closed without data. Empty closing data
2499 * should always result in the window being closed without causing constructive or destructive
2502 * As a window is opened and closed, it passes through several stages and the manager emits several
2503 * corresponding events.
2505 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2506 * - {@link #event-opening} is emitted with `opening` promise
2507 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2508 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2509 * - `setup` progress notification is emitted from opening promise
2510 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2511 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2512 * - `ready` progress notification is emitted from opening promise
2513 * - `opening` promise is resolved with `opened` promise
2514 * - Window is now open
2516 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2517 * - `opened` promise is resolved with `closing` promise
2518 * - {@link #event-closing} is emitted with `closing` promise
2519 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2520 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2521 * - `hold` progress notification is emitted from opening promise
2522 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2523 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2524 * - `teardown` progress notification is emitted from opening promise
2525 * - Closing promise is resolved
2526 * - Window is now closed
2529 * @param {Object} [config] Configuration options
2530 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2531 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2532 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2534 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2535 // Configuration initialization
2536 config
= config
|| {};
2538 // Parent constructor
2539 OO
.ui
.WindowManager
.super.call( this, config
);
2541 // Mixin constructors
2542 OO
.EventEmitter
.call( this );
2545 this.factory
= config
.factory
;
2546 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2547 this.isolate
= !!config
.isolate
;
2549 this.opening
= null;
2551 this.closing
= null;
2552 this.preparingToOpen
= null;
2553 this.preparingToClose
= null;
2555 this.currentWindow
= null;
2556 this.$ariaHidden
= null;
2557 this.requestedSize
= null;
2558 this.onWindowResizeTimeout
= null;
2559 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2560 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2561 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2562 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2566 .addClass( 'oo-ui-windowManager' )
2567 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2572 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2573 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2578 * Window is opening.
2580 * Fired when the window begins to be opened.
2583 * @param {OO.ui.Window} win Window that's being opened
2584 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2585 * resolved the first argument will be a promise which will be resolved when the window begins
2586 * closing, the second argument will be the opening data; progress notifications will be fired on
2587 * the promise for `setup` and `ready` when those processes are completed respectively.
2588 * @param {Object} data Window opening data
2592 * Window is closing.
2594 * Fired when the window begins to be closed.
2597 * @param {OO.ui.Window} win Window that's being closed
2598 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2599 * is resolved the first argument will be a the closing data; progress notifications will be fired
2600 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2601 * @param {Object} data Window closing data
2605 * Window was resized.
2608 * @param {OO.ui.Window} win Window that was resized
2611 /* Static Properties */
2614 * Map of symbolic size names and CSS properties.
2618 * @property {Object}
2620 OO
.ui
.WindowManager
.static.sizes
= {
2631 // These can be non-numeric because they are never used in calculations
2638 * Symbolic name of default size.
2640 * Default size is used if the window's requested size is not recognized.
2644 * @property {string}
2646 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2651 * Handle window resize events.
2653 * @param {jQuery.Event} e Window resize event
2655 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2656 clearTimeout( this.onWindowResizeTimeout
);
2657 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2661 * Handle window resize events.
2663 * @param {jQuery.Event} e Window resize event
2665 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2666 if ( this.currentWindow
) {
2667 this.updateWindowSize( this.currentWindow
);
2672 * Handle window mouse wheel events.
2674 * @param {jQuery.Event} e Mouse wheel event
2676 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function ( e
) {
2677 // Kill all events in the parent window if the child window is isolated,
2678 // or if the event didn't come from the child window
2679 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2683 * Handle document key down events.
2685 * @param {jQuery.Event} e Key down event
2687 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2688 switch ( e
.which
) {
2689 case OO
.ui
.Keys
.PAGEUP
:
2690 case OO
.ui
.Keys
.PAGEDOWN
:
2691 case OO
.ui
.Keys
.END
:
2692 case OO
.ui
.Keys
.HOME
:
2693 case OO
.ui
.Keys
.LEFT
:
2695 case OO
.ui
.Keys
.RIGHT
:
2696 case OO
.ui
.Keys
.DOWN
:
2697 // Kill all events in the parent window if the child window is isolated,
2698 // or if the event didn't come from the child window
2699 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2704 * Check if window is opening.
2706 * @return {boolean} Window is opening
2708 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2709 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2713 * Check if window is closing.
2715 * @return {boolean} Window is closing
2717 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2718 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2722 * Check if window is opened.
2724 * @return {boolean} Window is opened
2726 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2727 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2731 * Check if window contents should be isolated.
2733 * Window content isolation is done using inline frames.
2735 * @return {boolean} Window contents should be isolated
2737 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2738 return this.isolate
;
2742 * Check if a window is being managed.
2744 * @param {OO.ui.Window} win Window to check
2745 * @return {boolean} Window is being managed
2747 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2750 for ( name
in this.windows
) {
2751 if ( this.windows
[name
] === win
) {
2760 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2762 * @param {OO.ui.Window} win Window being opened
2763 * @param {Object} [data] Window opening data
2764 * @return {number} Milliseconds to wait
2766 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2771 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2773 * @param {OO.ui.Window} win Window being opened
2774 * @param {Object} [data] Window opening data
2775 * @return {number} Milliseconds to wait
2777 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2782 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2784 * @param {OO.ui.Window} win Window being closed
2785 * @param {Object} [data] Window closing data
2786 * @return {number} Milliseconds to wait
2788 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2793 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2795 * @param {OO.ui.Window} win Window being closed
2796 * @param {Object} [data] Window closing data
2797 * @return {number} Milliseconds to wait
2799 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2800 return this.modal
? 250 : 0;
2804 * Get managed window by symbolic name.
2806 * If window is not yet instantiated, it will be instantiated and added automatically.
2808 * @param {string} name Symbolic window name
2809 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2810 * @throws {Error} If the symbolic name is unrecognized by the factory
2811 * @throws {Error} If the symbolic name unrecognized as a managed window
2813 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2814 var deferred
= $.Deferred(),
2815 win
= this.windows
[name
];
2817 if ( !( win
instanceof OO
.ui
.Window
) ) {
2818 if ( this.factory
) {
2819 if ( !this.factory
.lookup( name
) ) {
2820 deferred
.reject( new OO
.ui
.Error(
2821 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2824 win
= this.factory
.create( name
, this, { $: this.$ } );
2825 this.addWindows( [ win
] );
2826 deferred
.resolve( win
);
2829 deferred
.reject( new OO
.ui
.Error(
2830 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2834 deferred
.resolve( win
);
2837 return deferred
.promise();
2841 * Get current window.
2843 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2845 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2846 return this.currentWindow
;
2852 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2853 * @param {Object} [data] Window opening data
2854 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2855 * for more details about the `opening` promise
2858 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2861 opening
= $.Deferred();
2863 // Argument handling
2864 if ( typeof win
=== 'string' ) {
2865 return this.getWindow( win
).then( function ( win
) {
2866 return manager
.openWindow( win
, data
);
2871 if ( !this.hasWindow( win
) ) {
2872 opening
.reject( new OO
.ui
.Error(
2873 'Cannot open window: window is not attached to manager'
2875 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2876 opening
.reject( new OO
.ui
.Error(
2877 'Cannot open window: another window is opening or open'
2882 if ( opening
.state() !== 'rejected' ) {
2883 // Begin loading the window if it's not loading or loaded already - may take noticable time
2884 // and we want to do this in parallel with any other preparatory actions
2885 if ( !win
.isLoading() && !win
.isLoaded() ) {
2886 // Finish initializing the window (must be done after manager is attached to DOM)
2887 win
.setManager( this );
2888 preparing
.push( win
.load() );
2891 if ( this.closing
) {
2892 // If a window is currently closing, wait for it to complete
2893 preparing
.push( this.closing
);
2896 this.preparingToOpen
= $.when
.apply( $, preparing
);
2897 // Ensure handlers get called after preparingToOpen is set
2898 this.preparingToOpen
.done( function () {
2899 if ( manager
.modal
) {
2900 manager
.toggleGlobalEvents( true );
2901 manager
.toggleAriaIsolation( true );
2903 manager
.currentWindow
= win
;
2904 manager
.opening
= opening
;
2905 manager
.preparingToOpen
= null;
2906 manager
.emit( 'opening', win
, opening
, data
);
2907 setTimeout( function () {
2908 win
.setup( data
).then( function () {
2909 manager
.updateWindowSize( win
);
2910 manager
.opening
.notify( { state
: 'setup' } );
2911 setTimeout( function () {
2912 win
.ready( data
).then( function () {
2913 manager
.opening
.notify( { state
: 'ready' } );
2914 manager
.opening
= null;
2915 manager
.opened
= $.Deferred();
2916 opening
.resolve( manager
.opened
.promise(), data
);
2918 }, manager
.getReadyDelay() );
2920 }, manager
.getSetupDelay() );
2924 return opening
.promise();
2930 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2931 * @param {Object} [data] Window closing data
2932 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2933 * for more details about the `closing` promise
2934 * @throws {Error} If no window by that name is being managed
2937 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2940 closing
= $.Deferred(),
2943 // Argument handling
2944 if ( typeof win
=== 'string' ) {
2945 win
= this.windows
[win
];
2946 } else if ( !this.hasWindow( win
) ) {
2952 closing
.reject( new OO
.ui
.Error(
2953 'Cannot close window: window is not attached to manager'
2955 } else if ( win
!== this.currentWindow
) {
2956 closing
.reject( new OO
.ui
.Error(
2957 'Cannot close window: window already closed with different data'
2959 } else if ( this.preparingToClose
|| this.closing
) {
2960 closing
.reject( new OO
.ui
.Error(
2961 'Cannot close window: window already closing with different data'
2966 if ( closing
.state() !== 'rejected' ) {
2967 if ( this.opening
) {
2968 // If the window is currently opening, close it when it's done
2969 preparing
.push( this.opening
);
2972 this.preparingToClose
= $.when
.apply( $, preparing
);
2973 // Ensure handlers get called after preparingToClose is set
2974 this.preparingToClose
.done( function () {
2975 manager
.closing
= closing
;
2976 manager
.preparingToClose
= null;
2977 manager
.emit( 'closing', win
, closing
, data
);
2978 opened
= manager
.opened
;
2979 manager
.opened
= null;
2980 opened
.resolve( closing
.promise(), data
);
2981 setTimeout( function () {
2982 win
.hold( data
).then( function () {
2983 closing
.notify( { state
: 'hold' } );
2984 setTimeout( function () {
2985 win
.teardown( data
).then( function () {
2986 closing
.notify( { state
: 'teardown' } );
2987 if ( manager
.modal
) {
2988 manager
.toggleGlobalEvents( false );
2989 manager
.toggleAriaIsolation( false );
2991 manager
.closing
= null;
2992 manager
.currentWindow
= null;
2993 closing
.resolve( data
);
2995 }, manager
.getTeardownDelay() );
2997 }, manager
.getHoldDelay() );
3001 return closing
.promise();
3007 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
3008 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
3009 * a statically configured symbolic name
3011 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
3012 var i
, len
, win
, name
, list
;
3014 if ( $.isArray( windows
) ) {
3015 // Convert to map of windows by looking up symbolic names from static configuration
3017 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
3018 name
= windows
[i
].constructor.static.name
;
3019 if ( typeof name
!== 'string' ) {
3020 throw new Error( 'Cannot add window' );
3022 list
[name
] = windows
[i
];
3024 } else if ( $.isPlainObject( windows
) ) {
3029 for ( name
in list
) {
3031 this.windows
[name
] = win
;
3032 this.$element
.append( win
.$element
);
3039 * Windows will be closed before they are removed.
3041 * @param {string} name Symbolic name of window to remove
3042 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3043 * @throws {Error} If windows being removed are not being managed
3045 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3046 var i
, len
, win
, name
,
3049 cleanup = function ( name
, win
) {
3050 delete manager
.windows
[name
];
3051 win
.$element
.detach();
3054 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3056 win
= this.windows
[name
];
3058 throw new Error( 'Cannot remove window' );
3060 promises
.push( this.closeWindow( name
).then( cleanup
.bind( null, name
, win
) ) );
3063 return $.when
.apply( $, promises
);
3067 * Remove all windows.
3069 * Windows will be closed before they are removed.
3071 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3073 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3074 return this.removeWindows( Object
.keys( this.windows
) );
3080 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3084 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3085 // Bypass for non-current, and thus invisible, windows
3086 if ( win
!== this.currentWindow
) {
3090 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
3091 sizes
= this.constructor.static.sizes
,
3092 size
= win
.getSize();
3094 if ( !sizes
[size
] ) {
3095 size
= this.constructor.static.defaultSize
;
3097 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
3101 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3102 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3103 win
.setDimensions( sizes
[size
] );
3105 this.emit( 'resize', win
);
3111 * Bind or unbind global events for scrolling.
3113 * @param {boolean} [on] Bind global events
3116 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3117 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3120 if ( !this.globalEvents
) {
3121 this.$( this.getElementDocument() ).on( {
3122 // Prevent scrolling by keys in top-level window
3123 keydown
: this.onDocumentKeyDownHandler
3125 this.$( this.getElementWindow() ).on( {
3126 // Prevent scrolling by wheel in top-level window
3127 mousewheel
: this.onWindowMouseWheelHandler
,
3128 // Start listening for top-level window dimension changes
3129 'orientationchange resize': this.onWindowResizeHandler
3131 this.globalEvents
= true;
3133 } else if ( this.globalEvents
) {
3134 // Unbind global events
3135 this.$( this.getElementDocument() ).off( {
3136 // Allow scrolling by keys in top-level window
3137 keydown
: this.onDocumentKeyDownHandler
3139 this.$( this.getElementWindow() ).off( {
3140 // Allow scrolling by wheel in top-level window
3141 mousewheel
: this.onWindowMouseWheelHandler
,
3142 // Stop listening for top-level window dimension changes
3143 'orientationchange resize': this.onWindowResizeHandler
3145 this.globalEvents
= false;
3152 * Toggle screen reader visibility of content other than the window manager.
3154 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3157 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3158 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3161 if ( !this.$ariaHidden
) {
3162 // Hide everything other than the window manager from screen readers
3163 this.$ariaHidden
= $( 'body' )
3165 .not( this.$element
.parentsUntil( 'body' ).last() )
3166 .attr( 'aria-hidden', '' );
3168 } else if ( this.$ariaHidden
) {
3169 // Restore screen reader visiblity
3170 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3171 this.$ariaHidden
= null;
3178 * Destroy window manager.
3180 * Windows will not be closed, only removed from the DOM.
3182 OO
.ui
.WindowManager
.prototype.destroy = function () {
3183 this.toggleGlobalEvents( false );
3184 this.toggleAriaIsolation( false );
3185 this.$element
.remove();
3193 * @param {string|jQuery} message Description of error
3194 * @param {Object} [config] Configuration options
3195 * @cfg {boolean} [recoverable=true] Error is recoverable
3196 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3198 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3199 // Configuration initialization
3200 config
= config
|| {};
3203 this.message
= message
instanceof jQuery
? message
: String( message
);
3204 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3205 this.warning
= !!config
.warning
;
3210 OO
.initClass( OO
.ui
.Error
);
3215 * Check if error can be recovered from.
3217 * @return {boolean} Error is recoverable
3219 OO
.ui
.Error
.prototype.isRecoverable = function () {
3220 return this.recoverable
;
3224 * Check if the error is a warning
3226 * @return {boolean} Error is warning
3228 OO
.ui
.Error
.prototype.isWarning = function () {
3229 return this.warning
;
3233 * Get error message as DOM nodes.
3235 * @return {jQuery} Error message in DOM nodes
3237 OO
.ui
.Error
.prototype.getMessage = function () {
3238 return this.message
instanceof jQuery
?
3239 this.message
.clone() :
3240 $( '<div>' ).text( this.message
).contents();
3244 * Get error message as text.
3246 * @return {string} Error message
3248 OO
.ui
.Error
.prototype.getMessageText = function () {
3249 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3253 * A list of functions, called in sequence.
3255 * If a function added to a process returns boolean false the process will stop; if it returns an
3256 * object with a `promise` method the process will use the promise to either continue to the next
3257 * step when the promise is resolved or stop when the promise is rejected.
3262 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3263 * call, see #createStep for more information
3264 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3266 * @return {Object} Step object, with `callback` and `context` properties
3268 OO
.ui
.Process = function ( step
, context
) {
3273 if ( step
!== undefined ) {
3274 this.next( step
, context
);
3280 OO
.initClass( OO
.ui
.Process
);
3285 * Start the process.
3287 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3288 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3289 * process, the remaining steps will not be taken
3291 OO
.ui
.Process
.prototype.execute = function () {
3292 var i
, len
, promise
;
3295 * Continue execution.
3298 * @param {Array} step A function and the context it should be called in
3299 * @return {Function} Function that continues the process
3301 function proceed( step
) {
3302 return function () {
3303 // Execute step in the correct context
3305 result
= step
.callback
.call( step
.context
);
3307 if ( result
=== false ) {
3308 // Use rejected promise for boolean false results
3309 return $.Deferred().reject( [] ).promise();
3311 if ( typeof result
=== 'number' ) {
3313 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3315 // Use a delayed promise for numbers, expecting them to be in milliseconds
3316 deferred
= $.Deferred();
3317 setTimeout( deferred
.resolve
, result
);
3318 return deferred
.promise();
3320 if ( result
instanceof OO
.ui
.Error
) {
3321 // Use rejected promise for error
3322 return $.Deferred().reject( [ result
] ).promise();
3324 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3325 // Use rejected promise for list of errors
3326 return $.Deferred().reject( result
).promise();
3328 // Duck-type the object to see if it can produce a promise
3329 if ( result
&& $.isFunction( result
.promise
) ) {
3330 // Use a promise generated from the result
3331 return result
.promise();
3333 // Use resolved promise for other results
3334 return $.Deferred().resolve().promise();
3338 if ( this.steps
.length
) {
3339 // Generate a chain reaction of promises
3340 promise
= proceed( this.steps
[0] )();
3341 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3342 promise
= promise
.then( proceed( this.steps
[i
] ) );
3345 promise
= $.Deferred().resolve().promise();
3352 * Create a process step.
3355 * @param {number|jQuery.Promise|Function} step
3357 * - Number of milliseconds to wait; or
3358 * - Promise to wait to be resolved; or
3359 * - Function to execute
3360 * - If it returns boolean false the process will stop
3361 * - If it returns an object with a `promise` method the process will use the promise to either
3362 * continue to the next step when the promise is resolved or stop when the promise is rejected
3363 * - If it returns a number, the process will wait for that number of milliseconds before
3365 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3367 * @return {Object} Step object, with `callback` and `context` properties
3369 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3370 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3372 callback: function () {
3378 if ( $.isFunction( step
) ) {
3384 throw new Error( 'Cannot create process step: number, promise or function expected' );
3388 * Add step to the beginning of the process.
3390 * @inheritdoc #createStep
3391 * @return {OO.ui.Process} this
3394 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3395 this.steps
.unshift( this.createStep( step
, context
) );
3400 * Add step to the end of the process.
3402 * @inheritdoc #createStep
3403 * @return {OO.ui.Process} this
3406 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3407 this.steps
.push( this.createStep( step
, context
) );
3412 * Factory for tools.
3415 * @extends OO.Factory
3418 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3419 // Parent constructor
3420 OO
.ui
.ToolFactory
.super.call( this );
3425 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3430 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3431 var i
, len
, included
, promoted
, demoted
,
3435 // Collect included and not excluded tools
3436 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3439 promoted
= this.extract( promote
, used
);
3440 demoted
= this.extract( demote
, used
);
3443 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3444 if ( !used
[included
[i
]] ) {
3445 auto
.push( included
[i
] );
3449 return promoted
.concat( auto
).concat( demoted
);
3453 * Get a flat list of names from a list of names or groups.
3455 * Tools can be specified in the following ways:
3457 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3458 * - All tools in a group: `{ group: 'group-name' }`
3459 * - All tools: `'*'`
3462 * @param {Array|string} collection List of tools
3463 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3464 * names will be added as properties
3465 * @return {string[]} List of extracted names
3467 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3468 var i
, len
, item
, name
, tool
,
3471 if ( collection
=== '*' ) {
3472 for ( name
in this.registry
) {
3473 tool
= this.registry
[name
];
3475 // Only add tools by group name when auto-add is enabled
3476 tool
.static.autoAddToCatchall
&&
3477 // Exclude already used tools
3478 ( !used
|| !used
[name
] )
3486 } else if ( $.isArray( collection
) ) {
3487 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3488 item
= collection
[i
];
3489 // Allow plain strings as shorthand for named tools
3490 if ( typeof item
=== 'string' ) {
3491 item
= { name
: item
};
3493 if ( OO
.isPlainObject( item
) ) {
3495 for ( name
in this.registry
) {
3496 tool
= this.registry
[name
];
3498 // Include tools with matching group
3499 tool
.static.group
=== item
.group
&&
3500 // Only add tools by group name when auto-add is enabled
3501 tool
.static.autoAddToGroup
&&
3502 // Exclude already used tools
3503 ( !used
|| !used
[name
] )
3511 // Include tools with matching name and exclude already used tools
3512 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3513 names
.push( item
.name
);
3515 used
[item
.name
] = true;
3525 * Factory for tool groups.
3528 * @extends OO.Factory
3531 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3532 // Parent constructor
3533 OO
.Factory
.call( this );
3536 defaultClasses
= this.constructor.static.getDefaultClasses();
3538 // Register default toolgroups
3539 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3540 this.register( defaultClasses
[i
] );
3546 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3548 /* Static Methods */
3551 * Get a default set of classes to be registered on construction
3553 * @return {Function[]} Default classes
3555 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3558 OO
.ui
.ListToolGroup
,
3570 * @param {Object} [config] Configuration options
3572 OO
.ui
.Theme
= function OoUiTheme( config
) {
3573 // Configuration initialization
3574 config
= config
|| {};
3579 OO
.initClass( OO
.ui
.Theme
);
3584 * Get a list of classes to be applied to a widget.
3586 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3587 * otherwise state transitions will not work properly.
3589 * @param {OO.ui.Element} element Element for which to get classes
3590 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3592 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3593 return { on
: [], off
: [] };
3597 * Update CSS classes provided by the theme.
3599 * For elements with theme logic hooks, this should be called anytime there's a state change.
3601 * @param {OO.ui.Element} element Element for which to update classes
3602 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3604 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3605 var classes
= this.getElementClasses( element
);
3608 .removeClass( classes
.off
.join( ' ' ) )
3609 .addClass( classes
.on
.join( ' ' ) );
3613 * Element with a button.
3615 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3616 * and access keys for accessibility purposes.
3622 * @param {Object} [config] Configuration options
3623 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3624 * @cfg {boolean} [framed=true] Render button with a frame
3625 * @cfg {number} [tabIndex=0] Button's tab index. Use 0 to use default ordering, use -1 to prevent
3627 * @cfg {string} [accessKey] Button's access key
3629 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3630 // Configuration initialization
3631 config
= config
|| {};
3634 this.$button
= null;
3636 this.tabIndex
= null;
3637 this.accessKey
= null;
3638 this.active
= false;
3639 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3640 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3643 this.$element
.addClass( 'oo-ui-buttonElement' );
3644 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3645 this.setTabIndex( config
.tabIndex
|| 0 );
3646 this.setAccessKey( config
.accessKey
);
3647 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3652 OO
.initClass( OO
.ui
.ButtonElement
);
3654 /* Static Properties */
3657 * Cancel mouse down events.
3661 * @property {boolean}
3663 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3668 * Set the button element.
3670 * If an element is already set, it will be cleaned up before setting up the new element.
3672 * @param {jQuery} $button Element to use as button
3674 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3675 if ( this.$button
) {
3677 .removeClass( 'oo-ui-buttonElement-button' )
3678 .removeAttr( 'role accesskey tabindex' )
3679 .off( this.onMouseDownHandler
);
3682 this.$button
= $button
3683 .addClass( 'oo-ui-buttonElement-button' )
3684 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3685 .on( 'mousedown', this.onMouseDownHandler
);
3689 * Handles mouse down events.
3691 * @param {jQuery.Event} e Mouse down event
3693 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3694 if ( this.isDisabled() || e
.which
!== 1 ) {
3697 // Remove the tab-index while the button is down to prevent the button from stealing focus
3698 this.$button
.removeAttr( 'tabindex' );
3699 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3700 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3701 // reliably reapply the tabindex and remove the pressed class
3702 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3703 // Prevent change of focus unless specifically configured otherwise
3704 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3710 * Handles mouse up events.
3712 * @param {jQuery.Event} e Mouse up event
3714 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3715 if ( this.isDisabled() || e
.which
!== 1 ) {
3718 // Restore the tab-index after the button is up to restore the button's accesssibility
3719 this.$button
.attr( 'tabindex', this.tabIndex
);
3720 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3721 // Stop listening for mouseup, since we only needed this once
3722 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3726 * Check if button has a frame.
3728 * @return {boolean} Button is framed
3730 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3737 * @param {boolean} [framed] Make button framed, omit to toggle
3740 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3741 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3742 if ( framed
!== this.framed
) {
3743 this.framed
= framed
;
3745 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3746 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3747 this.updateThemeClasses();
3756 * @param {number|null} tabIndex Button's tab index, use null to remove
3759 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3760 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3762 if ( this.tabIndex
!== tabIndex
) {
3763 if ( this.$button
) {
3764 if ( tabIndex
!== null ) {
3765 this.$button
.attr( 'tabindex', tabIndex
);
3767 this.$button
.removeAttr( 'tabindex' );
3770 this.tabIndex
= tabIndex
;
3779 * @param {string} accessKey Button's access key, use empty string to remove
3782 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3783 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3785 if ( this.accessKey
!== accessKey
) {
3786 if ( this.$button
) {
3787 if ( accessKey
!== null ) {
3788 this.$button
.attr( 'accesskey', accessKey
);
3790 this.$button
.removeAttr( 'accesskey' );
3793 this.accessKey
= accessKey
;
3802 * @param {boolean} [value] Make button active
3805 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3806 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3811 * Element containing a sequence of child elements.
3817 * @param {Object} [config] Configuration options
3818 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3820 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3821 // Configuration intialization
3822 config
= config
|| {};
3827 this.aggregateItemEvents
= {};
3830 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3836 * Set the group element.
3838 * If an element is already set, items will be moved to the new element.
3840 * @param {jQuery} $group Element to use as group
3842 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3845 this.$group
= $group
;
3846 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3847 this.$group
.append( this.items
[i
].$element
);
3852 * Check if there are no items.
3854 * @return {boolean} Group is empty
3856 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3857 return !this.items
.length
;
3863 * @return {OO.ui.Element[]} Items
3865 OO
.ui
.GroupElement
.prototype.getItems = function () {
3866 return this.items
.slice( 0 );
3870 * Add an aggregate item event.
3872 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3873 * and with an additional leading parameter containing the item that emitted the original event.
3874 * Other arguments that were emitted from the original event are passed through.
3876 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3877 * event, use null value to remove aggregation
3878 * @throws {Error} If aggregation already exists
3880 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3881 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3883 for ( itemEvent
in events
) {
3884 groupEvent
= events
[itemEvent
];
3886 // Remove existing aggregated event
3887 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3888 // Don't allow duplicate aggregations
3890 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3892 // Remove event aggregation from existing items
3893 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3894 item
= this.items
[i
];
3895 if ( item
.connect
&& item
.disconnect
) {
3897 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3898 item
.disconnect( this, remove
);
3901 // Prevent future items from aggregating event
3902 delete this.aggregateItemEvents
[itemEvent
];
3905 // Add new aggregate event
3907 // Make future items aggregate event
3908 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3909 // Add event aggregation to existing items
3910 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3911 item
= this.items
[i
];
3912 if ( item
.connect
&& item
.disconnect
) {
3914 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3915 item
.connect( this, add
);
3925 * Adding an existing item will move it.
3927 * @param {OO.ui.Element[]} items Items
3928 * @param {number} [index] Index to insert items at
3931 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3932 var i
, len
, item
, event
, events
, currentIndex
,
3935 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3938 // Check if item exists then remove it first, effectively "moving" it
3939 currentIndex
= $.inArray( item
, this.items
);
3940 if ( currentIndex
>= 0 ) {
3941 this.removeItems( [ item
] );
3942 // Adjust index to compensate for removal
3943 if ( currentIndex
< index
) {
3948 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3950 for ( event
in this.aggregateItemEvents
) {
3951 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3953 item
.connect( this, events
);
3955 item
.setElementGroup( this );
3956 itemElements
.push( item
.$element
.get( 0 ) );
3959 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3960 this.$group
.append( itemElements
);
3961 this.items
.push
.apply( this.items
, items
);
3962 } else if ( index
=== 0 ) {
3963 this.$group
.prepend( itemElements
);
3964 this.items
.unshift
.apply( this.items
, items
);
3966 this.items
[index
].$element
.before( itemElements
);
3967 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3976 * Items will be detached, not removed, so they can be used later.
3978 * @param {OO.ui.Element[]} items Items to remove
3981 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3982 var i
, len
, item
, index
, remove
, itemEvent
;
3984 // Remove specific items
3985 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3987 index
= $.inArray( item
, this.items
);
3988 if ( index
!== -1 ) {
3990 item
.connect
&& item
.disconnect
&&
3991 !$.isEmptyObject( this.aggregateItemEvents
)
3994 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3995 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3997 item
.disconnect( this, remove
);
3999 item
.setElementGroup( null );
4000 this.items
.splice( index
, 1 );
4001 item
.$element
.detach();
4011 * Items will be detached, not removed, so they can be used later.
4015 OO
.ui
.GroupElement
.prototype.clearItems = function () {
4016 var i
, len
, item
, remove
, itemEvent
;
4019 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4020 item
= this.items
[i
];
4022 item
.connect
&& item
.disconnect
&&
4023 !$.isEmptyObject( this.aggregateItemEvents
)
4026 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4027 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4029 item
.disconnect( this, remove
);
4031 item
.setElementGroup( null );
4032 item
.$element
.detach();
4040 * Element containing an icon.
4042 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4043 * a control or convey information in a more space efficient way. Icons should rarely be used
4044 * without labels; such as in a toolbar where space is at a premium or within a context where the
4045 * meaning is very clear to the user.
4051 * @param {Object} [config] Configuration options
4052 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4053 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4054 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4056 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4058 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4059 // Configuration intialization
4060 config
= config
|| {};
4065 this.iconTitle
= null;
4068 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4069 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4070 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4075 OO
.initClass( OO
.ui
.IconElement
);
4077 /* Static Properties */
4082 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4084 * For i18n purposes, this property can be an object containing a `default` icon name property and
4085 * additional icon names keyed by language code.
4087 * Example of i18n icon definition:
4088 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4092 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4093 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4096 OO
.ui
.IconElement
.static.icon
= null;
4103 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4106 OO
.ui
.IconElement
.static.iconTitle
= null;
4111 * Set the icon element.
4113 * If an element is already set, it will be cleaned up before setting up the new element.
4115 * @param {jQuery} $icon Element to use as icon
4117 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4120 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4121 .removeAttr( 'title' );
4125 .addClass( 'oo-ui-iconElement-icon' )
4126 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4127 if ( this.iconTitle
!== null ) {
4128 this.$icon
.attr( 'title', this.iconTitle
);
4135 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4136 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4137 * language, use null to remove icon
4140 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4141 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4142 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4144 if ( this.icon
!== icon
) {
4146 if ( this.icon
!== null ) {
4147 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4149 if ( icon
!== null ) {
4150 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4156 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4157 this.updateThemeClasses();
4165 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4169 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4170 iconTitle
= typeof iconTitle
=== 'function' ||
4171 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4172 OO
.ui
.resolveMsg( iconTitle
) : null;
4174 if ( this.iconTitle
!== iconTitle
) {
4175 this.iconTitle
= iconTitle
;
4177 if ( this.iconTitle
!== null ) {
4178 this.$icon
.attr( 'title', iconTitle
);
4180 this.$icon
.removeAttr( 'title' );
4191 * @return {string} Icon name
4193 OO
.ui
.IconElement
.prototype.getIcon = function () {
4198 * Element containing an indicator.
4200 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4201 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4202 * instead of performing an action directly, or an item in a list which has errors that need to be
4209 * @param {Object} [config] Configuration options
4210 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4212 * @cfg {string} [indicator] Symbolic indicator name
4213 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4215 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4216 // Configuration initialization
4217 config
= config
|| {};
4220 this.$indicator
= null;
4221 this.indicator
= null;
4222 this.indicatorTitle
= null;
4225 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4226 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4227 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4232 OO
.initClass( OO
.ui
.IndicatorElement
);
4234 /* Static Properties */
4241 * @property {string|null} Symbolic indicator name
4243 OO
.ui
.IndicatorElement
.static.indicator
= null;
4250 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4253 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4258 * Set the indicator element.
4260 * If an element is already set, it will be cleaned up before setting up the new element.
4262 * @param {jQuery} $indicator Element to use as indicator
4264 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4265 if ( this.$indicator
) {
4267 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4268 .removeAttr( 'title' );
4271 this.$indicator
= $indicator
4272 .addClass( 'oo-ui-indicatorElement-indicator' )
4273 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4274 if ( this.indicatorTitle
!== null ) {
4275 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4280 * Set indicator name.
4282 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4285 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4286 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4288 if ( this.indicator
!== indicator
) {
4289 if ( this.$indicator
) {
4290 if ( this.indicator
!== null ) {
4291 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4293 if ( indicator
!== null ) {
4294 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4297 this.indicator
= indicator
;
4300 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4301 this.updateThemeClasses();
4307 * Set indicator title.
4309 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4310 * null for no indicator title
4313 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4314 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4315 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4316 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4318 if ( this.indicatorTitle
!== indicatorTitle
) {
4319 this.indicatorTitle
= indicatorTitle
;
4320 if ( this.$indicator
) {
4321 if ( this.indicatorTitle
!== null ) {
4322 this.$indicator
.attr( 'title', indicatorTitle
);
4324 this.$indicator
.removeAttr( 'title' );
4333 * Get indicator name.
4335 * @return {string} Symbolic name of indicator
4337 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4338 return this.indicator
;
4342 * Get indicator title.
4344 * @return {string} Indicator title text
4346 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4347 return this.indicatorTitle
;
4351 * Element containing a label.
4357 * @param {Object} [config] Configuration options
4358 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4359 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4360 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4362 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4363 // Configuration initialization
4364 config
= config
|| {};
4369 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4372 this.setLabel( config
.label
|| this.constructor.static.label
);
4373 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4378 OO
.initClass( OO
.ui
.LabelElement
);
4380 /* Static Properties */
4387 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4390 OO
.ui
.LabelElement
.static.label
= null;
4395 * Set the label element.
4397 * If an element is already set, it will be cleaned up before setting up the new element.
4399 * @param {jQuery} $label Element to use as label
4401 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4402 if ( this.$label
) {
4403 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4406 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4407 this.setLabelContent( this.label
);
4413 * An empty string will result in the label being hidden. A string containing only whitespace will
4414 * be converted to a single ` `.
4416 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4417 * text; or null for no label
4420 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4421 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4422 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4424 if ( this.label
!== label
) {
4425 if ( this.$label
) {
4426 this.setLabelContent( label
);
4431 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4439 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
4440 * text; or null for no label
4442 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4451 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4452 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4453 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4460 * Set the content of the label.
4462 * Do not call this method until after the label element has been set by #setLabelElement.
4465 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4466 * text; or null for no label
4468 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4469 if ( typeof label
=== 'string' ) {
4470 if ( label
.match( /^\s*$/ ) ) {
4471 // Convert whitespace only string to a single non-breaking space
4472 this.$label
.html( ' ' );
4474 this.$label
.text( label
);
4476 } else if ( label
instanceof jQuery
) {
4477 this.$label
.empty().append( label
);
4479 this.$label
.empty();
4481 this.$label
.css( 'display', !label
? 'none' : '' );
4485 * Element containing an OO.ui.PopupWidget object.
4491 * @param {Object} [config] Configuration options
4492 * @cfg {Object} [popup] Configuration to pass to popup
4493 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4495 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4496 // Configuration initialization
4497 config
= config
|| {};
4500 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4501 { autoClose
: true },
4503 { $: this.$, $autoCloseIgnore
: this.$element
}
4512 * @return {OO.ui.PopupWidget} Popup widget
4514 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4519 * Element with named flags that can be added, removed, listed and checked.
4521 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4522 * the flag name. Flags are primarily useful for styling.
4528 * @param {Object} [config] Configuration options
4529 * @cfg {string|string[]} [flags] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4530 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4532 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4533 // Configuration initialization
4534 config
= config
|| {};
4538 this.$flagged
= null;
4541 this.setFlags( config
.flags
);
4542 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4549 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4550 * added/removed properties
4556 * Set the flagged element.
4558 * If an element is already set, it will be cleaned up before setting up the new element.
4560 * @param {jQuery} $flagged Element to add flags to
4562 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4563 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4564 return 'oo-ui-flaggedElement-' + flag
;
4567 if ( this.$flagged
) {
4568 this.$flagged
.removeClass( classNames
);
4571 this.$flagged
= $flagged
.addClass( classNames
);
4575 * Check if a flag is set.
4577 * @param {string} flag Name of flag
4578 * @return {boolean} Has flag
4580 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4581 return flag
in this.flags
;
4585 * Get the names of all flags set.
4587 * @return {string[]} Flag names
4589 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4590 return Object
.keys( this.flags
);
4599 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4600 var flag
, className
,
4603 classPrefix
= 'oo-ui-flaggedElement-';
4605 for ( flag
in this.flags
) {
4606 className
= classPrefix
+ flag
;
4607 changes
[flag
] = false;
4608 delete this.flags
[flag
];
4609 remove
.push( className
);
4612 if ( this.$flagged
) {
4613 this.$flagged
.removeClass( remove
.join( ' ' ) );
4616 this.updateThemeClasses();
4617 this.emit( 'flag', changes
);
4623 * Add one or more flags.
4625 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4626 * keyed by flag name containing boolean set/remove instructions.
4630 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4631 var i
, len
, flag
, className
,
4635 classPrefix
= 'oo-ui-flaggedElement-';
4637 if ( typeof flags
=== 'string' ) {
4638 className
= classPrefix
+ flags
;
4640 if ( !this.flags
[flags
] ) {
4641 this.flags
[flags
] = true;
4642 add
.push( className
);
4644 } else if ( $.isArray( flags
) ) {
4645 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4647 className
= classPrefix
+ flag
;
4649 if ( !this.flags
[flag
] ) {
4650 changes
[flag
] = true;
4651 this.flags
[flag
] = true;
4652 add
.push( className
);
4655 } else if ( OO
.isPlainObject( flags
) ) {
4656 for ( flag
in flags
) {
4657 className
= classPrefix
+ flag
;
4658 if ( flags
[flag
] ) {
4660 if ( !this.flags
[flag
] ) {
4661 changes
[flag
] = true;
4662 this.flags
[flag
] = true;
4663 add
.push( className
);
4667 if ( this.flags
[flag
] ) {
4668 changes
[flag
] = false;
4669 delete this.flags
[flag
];
4670 remove
.push( className
);
4676 if ( this.$flagged
) {
4678 .addClass( add
.join( ' ' ) )
4679 .removeClass( remove
.join( ' ' ) );
4682 this.updateThemeClasses();
4683 this.emit( 'flag', changes
);
4689 * Element with a title.
4691 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4692 * not visible on touch devices.
4698 * @param {Object} [config] Configuration options
4699 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4700 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
4701 * static property 'title' is used.
4703 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4704 // Configuration initialization
4705 config
= config
|| {};
4708 this.$titled
= null;
4712 this.setTitle( config
.title
|| this.constructor.static.title
);
4713 this.setTitledElement( config
.$titled
|| this.$element
);
4718 OO
.initClass( OO
.ui
.TitledElement
);
4720 /* Static Properties */
4727 * @property {string|Function} Title text or a function that returns text
4729 OO
.ui
.TitledElement
.static.title
= null;
4734 * Set the titled element.
4736 * If an element is already set, it will be cleaned up before setting up the new element.
4738 * @param {jQuery} $titled Element to set title on
4740 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4741 if ( this.$titled
) {
4742 this.$titled
.removeAttr( 'title' );
4745 this.$titled
= $titled
;
4747 this.$titled
.attr( 'title', this.title
);
4754 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4757 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4758 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4760 if ( this.title
!== title
) {
4761 if ( this.$titled
) {
4762 if ( title
!== null ) {
4763 this.$titled
.attr( 'title', title
);
4765 this.$titled
.removeAttr( 'title' );
4777 * @return {string} Title string
4779 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4784 * Element that can be automatically clipped to visible boundaries.
4786 * Whenever the element's natural height changes, you have to call
4787 * #clip to make sure it's still clipping correctly.
4793 * @param {Object} [config] Configuration options
4794 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4796 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4797 // Configuration initialization
4798 config
= config
|| {};
4801 this.$clippable
= null;
4802 this.clipping
= false;
4803 this.clippedHorizontally
= false;
4804 this.clippedVertically
= false;
4805 this.$clippableContainer
= null;
4806 this.$clippableScroller
= null;
4807 this.$clippableWindow
= null;
4808 this.idealWidth
= null;
4809 this.idealHeight
= null;
4810 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
4811 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
4814 this.setClippableElement( config
.$clippable
|| this.$element
);
4820 * Set clippable element.
4822 * If an element is already set, it will be cleaned up before setting up the new element.
4824 * @param {jQuery} $clippable Element to make clippable
4826 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4827 if ( this.$clippable
) {
4828 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4829 this.$clippable
.css( { width
: '', height
: '' } );
4830 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4831 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4834 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4841 * Do not turn clipping on until after the element is attached to the DOM and visible.
4843 * @param {boolean} [clipping] Enable clipping, omit to toggle
4846 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4847 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4849 if ( this.clipping
!== clipping
) {
4850 this.clipping
= clipping
;
4852 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4853 // If the clippable container is the body, we have to listen to scroll events and check
4854 // jQuery.scrollTop on the window because of browser inconsistencies
4855 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4856 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4857 this.$clippableContainer
;
4858 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4859 this.$clippableWindow
= this.$( this.getElementWindow() )
4860 .on( 'resize', this.onClippableWindowResizeHandler
);
4861 // Initial clip after visible
4864 this.$clippable
.css( { width
: '', height
: '' } );
4865 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4866 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4868 this.$clippableContainer
= null;
4869 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4870 this.$clippableScroller
= null;
4871 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4872 this.$clippableWindow
= null;
4880 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4882 * @return {boolean} Element will be clipped to the visible area
4884 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4885 return this.clipping
;
4889 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4891 * @return {boolean} Part of the element is being clipped
4893 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4894 return this.clippedHorizontally
|| this.clippedVertically
;
4898 * Check if the right of the element is being clipped by the nearest scrollable container.
4900 * @return {boolean} Part of the element is being clipped
4902 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4903 return this.clippedHorizontally
;
4907 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4909 * @return {boolean} Part of the element is being clipped
4911 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4912 return this.clippedVertically
;
4916 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
4918 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4919 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4921 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4922 this.idealWidth
= width
;
4923 this.idealHeight
= height
;
4925 if ( !this.clipping
) {
4926 // Update dimensions
4927 this.$clippable
.css( { width
: width
, height
: height
} );
4929 // While clipping, idealWidth and idealHeight are not considered
4933 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4934 * the element's natural height changes.
4936 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4937 * overlapped by, the visible area of the nearest scrollable container.
4941 OO
.ui
.ClippableElement
.prototype.clip = function () {
4942 if ( !this.clipping
) {
4943 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
4948 cOffset
= this.$clippable
.offset(),
4949 $container
= this.$clippableContainer
.is( 'body' ) ?
4950 this.$clippableWindow
: this.$clippableContainer
,
4951 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
4952 ccHeight
= $container
.innerHeight() - buffer
,
4953 ccWidth
= $container
.innerWidth() - buffer
,
4954 scrollTop
= this.$clippableScroller
.scrollTop(),
4955 scrollLeft
= this.$clippableScroller
.scrollLeft(),
4956 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
4957 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
4958 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
4959 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
4960 clipWidth
= desiredWidth
< naturalWidth
,
4961 clipHeight
= desiredHeight
< naturalHeight
;
4964 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
4966 this.$clippable
.css( 'width', this.idealWidth
|| '' );
4967 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4968 this.$clippable
.css( 'overflowX', '' );
4971 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
4973 this.$clippable
.css( 'height', this.idealHeight
|| '' );
4974 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4975 this.$clippable
.css( 'overflowY', '' );
4978 this.clippedHorizontally
= clipWidth
;
4979 this.clippedVertically
= clipHeight
;
4985 * Generic toolbar tool.
4989 * @extends OO.ui.Widget
4990 * @mixins OO.ui.IconElement
4991 * @mixins OO.ui.FlaggedElement
4994 * @param {OO.ui.ToolGroup} toolGroup
4995 * @param {Object} [config] Configuration options
4996 * @cfg {string|Function} [title] Title text or a function that returns text
4998 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4999 // Configuration initialization
5000 config
= config
|| {};
5002 // Parent constructor
5003 OO
.ui
.Tool
.super.call( this, config
);
5005 // Mixin constructors
5006 OO
.ui
.IconElement
.call( this, config
);
5007 OO
.ui
.FlaggedElement
.call( this, config
);
5010 this.toolGroup
= toolGroup
;
5011 this.toolbar
= this.toolGroup
.getToolbar();
5012 this.active
= false;
5013 this.$title
= this.$( '<span>' );
5014 this.$titleText
= this.$( '<span>' );
5015 this.$accel
= this.$( '<span>' );
5016 this.$link
= this.$( '<a>' );
5020 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
5023 this.$titleText
.addClass( 'oo-ui-tool-title-text' );
5025 .addClass( 'oo-ui-tool-accel' )
5027 // This may need to be changed if the key names are ever localized,
5028 // but for now they are essentially written in English
5033 .addClass( 'oo-ui-tool-title' )
5034 .append( this.$titleText
, this.$accel
);
5036 .addClass( 'oo-ui-tool-link' )
5037 .append( this.$icon
, this.$title
)
5038 .prop( 'tabIndex', 0 )
5039 .attr( 'role', 'button' );
5041 .data( 'oo-ui-tool', this )
5043 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5044 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5046 .append( this.$link
);
5047 this.setTitle( config
.title
|| this.constructor.static.title
);
5052 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5053 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5054 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5062 /* Static Properties */
5068 OO
.ui
.Tool
.static.tagName
= 'span';
5071 * Symbolic name of tool.
5076 * @property {string}
5078 OO
.ui
.Tool
.static.name
= '';
5086 * @property {string}
5088 OO
.ui
.Tool
.static.group
= '';
5093 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
5094 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
5095 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
5096 * appended to the title if the tool is part of a bar tool group.
5101 * @property {string|Function} Title text or a function that returns text
5103 OO
.ui
.Tool
.static.title
= '';
5106 * Tool can be automatically added to catch-all groups.
5110 * @property {boolean}
5112 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
5115 * Tool can be automatically added to named groups.
5118 * @property {boolean}
5121 OO
.ui
.Tool
.static.autoAddToGroup
= true;
5124 * Check if this tool is compatible with given data.
5128 * @param {Mixed} data Data to check
5129 * @return {boolean} Tool can be used with data
5131 OO
.ui
.Tool
.static.isCompatibleWith = function () {
5138 * Handle the toolbar state being updated.
5140 * This is an abstract method that must be overridden in a concrete subclass.
5144 OO
.ui
.Tool
.prototype.onUpdateState = function () {
5146 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
5151 * Handle the tool being selected.
5153 * This is an abstract method that must be overridden in a concrete subclass.
5157 OO
.ui
.Tool
.prototype.onSelect = function () {
5159 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
5164 * Check if the button is active.
5166 * @return {boolean} Button is active
5168 OO
.ui
.Tool
.prototype.isActive = function () {
5173 * Make the button appear active or inactive.
5175 * @param {boolean} state Make button appear active
5177 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
5178 this.active
= !!state
;
5179 if ( this.active
) {
5180 this.$element
.addClass( 'oo-ui-tool-active' );
5182 this.$element
.removeClass( 'oo-ui-tool-active' );
5187 * Get the tool title.
5189 * @param {string|Function} title Title text or a function that returns text
5192 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
5193 this.title
= OO
.ui
.resolveMsg( title
);
5199 * Get the tool title.
5201 * @return {string} Title text
5203 OO
.ui
.Tool
.prototype.getTitle = function () {
5208 * Get the tool's symbolic name.
5210 * @return {string} Symbolic name of tool
5212 OO
.ui
.Tool
.prototype.getName = function () {
5213 return this.constructor.static.name
;
5219 OO
.ui
.Tool
.prototype.updateTitle = function () {
5220 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
5221 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
5222 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
5225 this.$titleText
.text( this.title
);
5226 this.$accel
.text( accel
);
5228 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
5229 tooltipParts
.push( this.title
);
5231 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
5232 tooltipParts
.push( accel
);
5234 if ( tooltipParts
.length
) {
5235 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
5237 this.$link
.removeAttr( 'title' );
5244 OO
.ui
.Tool
.prototype.destroy = function () {
5245 this.toolbar
.disconnect( this );
5246 this.$element
.remove();
5250 * Collection of tool groups.
5253 * @extends OO.ui.Element
5254 * @mixins OO.EventEmitter
5255 * @mixins OO.ui.GroupElement
5258 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5259 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5260 * @param {Object} [config] Configuration options
5261 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5262 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5264 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5265 // Configuration initialization
5266 config
= config
|| {};
5268 // Parent constructor
5269 OO
.ui
.Toolbar
.super.call( this, config
);
5271 // Mixin constructors
5272 OO
.EventEmitter
.call( this );
5273 OO
.ui
.GroupElement
.call( this, config
);
5276 this.toolFactory
= toolFactory
;
5277 this.toolGroupFactory
= toolGroupFactory
;
5280 this.$bar
= this.$( '<div>' );
5281 this.$actions
= this.$( '<div>' );
5282 this.initialized
= false;
5286 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5287 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
5290 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5291 if ( config
.actions
) {
5292 this.$bar
.append( this.$actions
.addClass( 'oo-ui-toolbar-actions' ) );
5295 .addClass( 'oo-ui-toolbar-bar' )
5296 .append( this.$group
, '<div style="clear:both"></div>' );
5297 if ( config
.shadow
) {
5298 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5300 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5305 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5306 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5307 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5312 * Get the tool factory.
5314 * @return {OO.ui.ToolFactory} Tool factory
5316 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5317 return this.toolFactory
;
5321 * Get the tool group factory.
5323 * @return {OO.Factory} Tool group factory
5325 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5326 return this.toolGroupFactory
;
5330 * Handles mouse down events.
5332 * @param {jQuery.Event} e Mouse down event
5334 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5335 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5336 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5337 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5343 * Sets up handles and preloads required information for the toolbar to work.
5344 * This must be called immediately after it is attached to a visible document.
5346 OO
.ui
.Toolbar
.prototype.initialize = function () {
5347 this.initialized
= true;
5353 * Tools can be specified in the following ways:
5355 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5356 * - All tools in a group: `{ group: 'group-name' }`
5357 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5359 * @param {Object.<string,Array>} groups List of tool group configurations
5360 * @param {Array|string} [groups.include] Tools to include
5361 * @param {Array|string} [groups.exclude] Tools to exclude
5362 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5363 * @param {Array|string} [groups.demote] Tools to demote to the end
5365 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5366 var i
, len
, type
, group
,
5368 defaultType
= 'bar';
5370 // Cleanup previous groups
5373 // Build out new groups
5374 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5376 if ( group
.include
=== '*' ) {
5377 // Apply defaults to catch-all groups
5378 if ( group
.type
=== undefined ) {
5379 group
.type
= 'list';
5381 if ( group
.label
=== undefined ) {
5382 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
5385 // Check type has been registered
5386 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5388 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5391 this.addItems( items
);
5395 * Remove all tools and groups from the toolbar.
5397 OO
.ui
.Toolbar
.prototype.reset = function () {
5402 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5403 this.items
[i
].destroy();
5409 * Destroys toolbar, removing event handlers and DOM elements.
5411 * Call this whenever you are done using a toolbar.
5413 OO
.ui
.Toolbar
.prototype.destroy = function () {
5415 this.$element
.remove();
5419 * Check if tool has not been used yet.
5421 * @param {string} name Symbolic name of tool
5422 * @return {boolean} Tool is available
5424 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5425 return !this.tools
[name
];
5429 * Prevent tool from being used again.
5431 * @param {OO.ui.Tool} tool Tool to reserve
5433 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5434 this.tools
[tool
.getName()] = tool
;
5438 * Allow tool to be used again.
5440 * @param {OO.ui.Tool} tool Tool to release
5442 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5443 delete this.tools
[tool
.getName()];
5447 * Get accelerator label for tool.
5449 * This is a stub that should be overridden to provide access to accelerator information.
5451 * @param {string} name Symbolic name of tool
5452 * @return {string|undefined} Tool accelerator label if available
5454 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5459 * Collection of tools.
5461 * Tools can be specified in the following ways:
5463 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5464 * - All tools in a group: `{ group: 'group-name' }`
5465 * - All tools: `'*'`
5469 * @extends OO.ui.Widget
5470 * @mixins OO.ui.GroupElement
5473 * @param {OO.ui.Toolbar} toolbar
5474 * @param {Object} [config] Configuration options
5475 * @cfg {Array|string} [include=[]] List of tools to include
5476 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5477 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5478 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5480 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5481 // Configuration initialization
5482 config
= config
|| {};
5484 // Parent constructor
5485 OO
.ui
.ToolGroup
.super.call( this, config
);
5487 // Mixin constructors
5488 OO
.ui
.GroupElement
.call( this, config
);
5491 this.toolbar
= toolbar
;
5493 this.pressed
= null;
5494 this.autoDisabled
= false;
5495 this.include
= config
.include
|| [];
5496 this.exclude
= config
.exclude
|| [];
5497 this.promote
= config
.promote
|| [];
5498 this.demote
= config
.demote
|| [];
5499 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
5503 'mousedown touchstart': this.onPointerDown
.bind( this ),
5504 'mouseup touchend': this.onPointerUp
.bind( this ),
5505 mouseover
: this.onMouseOver
.bind( this ),
5506 mouseout
: this.onMouseOut
.bind( this )
5508 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5509 this.aggregate( { disable
: 'itemDisable' } );
5510 this.connect( this, { itemDisable
: 'updateDisabled' } );
5513 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5515 .addClass( 'oo-ui-toolGroup' )
5516 .append( this.$group
);
5522 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5523 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5531 /* Static Properties */
5534 * Show labels in tooltips.
5538 * @property {boolean}
5540 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5543 * Show acceleration labels in tooltips.
5547 * @property {boolean}
5549 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5552 * Automatically disable the toolgroup when all tools are disabled
5556 * @property {boolean}
5558 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5565 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5566 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5572 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5573 var i
, item
, allDisabled
= true;
5575 if ( this.constructor.static.autoDisable
) {
5576 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5577 item
= this.items
[i
];
5578 if ( !item
.isDisabled() ) {
5579 allDisabled
= false;
5583 this.autoDisabled
= allDisabled
;
5585 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5589 * Handle mouse down events.
5591 * @param {jQuery.Event} e Mouse down event
5593 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5594 // e.which is 0 for touch events, 1 for left mouse button
5595 if ( !this.isDisabled() && e
.which
<= 1 ) {
5596 this.pressed
= this.getTargetTool( e
);
5597 if ( this.pressed
) {
5598 this.pressed
.setActive( true );
5599 this.getElementDocument().addEventListener(
5600 'mouseup', this.onCapturedMouseUpHandler
, true
5608 * Handle captured mouse up events.
5610 * @param {Event} e Mouse up event
5612 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5613 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5614 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5615 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5616 this.onPointerUp( e
);
5620 * Handle mouse up events.
5622 * @param {jQuery.Event} e Mouse up event
5624 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5625 var tool
= this.getTargetTool( e
);
5627 // e.which is 0 for touch events, 1 for left mouse button
5628 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5629 this.pressed
.onSelect();
5632 this.pressed
= null;
5637 * Handle mouse over events.
5639 * @param {jQuery.Event} e Mouse over event
5641 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5642 var tool
= this.getTargetTool( e
);
5644 if ( this.pressed
&& this.pressed
=== tool
) {
5645 this.pressed
.setActive( true );
5650 * Handle mouse out events.
5652 * @param {jQuery.Event} e Mouse out event
5654 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5655 var tool
= this.getTargetTool( e
);
5657 if ( this.pressed
&& this.pressed
=== tool
) {
5658 this.pressed
.setActive( false );
5663 * Get the closest tool to a jQuery.Event.
5665 * Only tool links are considered, which prevents other elements in the tool such as popups from
5666 * triggering tool group interactions.
5669 * @param {jQuery.Event} e
5670 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5672 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5674 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5676 if ( $item
.length
) {
5677 tool
= $item
.parent().data( 'oo-ui-tool' );
5680 return tool
&& !tool
.isDisabled() ? tool
: null;
5684 * Handle tool registry register events.
5686 * If a tool is registered after the group is created, we must repopulate the list to account for:
5688 * - a tool being added that may be included
5689 * - a tool already included being overridden
5691 * @param {string} name Symbolic name of tool
5693 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5698 * Get the toolbar this group is in.
5700 * @return {OO.ui.Toolbar} Toolbar of group
5702 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5703 return this.toolbar
;
5707 * Add and remove tools based on configuration.
5709 OO
.ui
.ToolGroup
.prototype.populate = function () {
5710 var i
, len
, name
, tool
,
5711 toolFactory
= this.toolbar
.getToolFactory(),
5715 list
= this.toolbar
.getToolFactory().getTools(
5716 this.include
, this.exclude
, this.promote
, this.demote
5719 // Build a list of needed tools
5720 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5724 toolFactory
.lookup( name
) &&
5725 // Tool is available or is already in this group
5726 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5728 tool
= this.tools
[name
];
5730 // Auto-initialize tools on first use
5731 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5734 this.toolbar
.reserveTool( tool
);
5739 // Remove tools that are no longer needed
5740 for ( name
in this.tools
) {
5741 if ( !names
[name
] ) {
5742 this.tools
[name
].destroy();
5743 this.toolbar
.releaseTool( this.tools
[name
] );
5744 remove
.push( this.tools
[name
] );
5745 delete this.tools
[name
];
5748 if ( remove
.length
) {
5749 this.removeItems( remove
);
5751 // Update emptiness state
5753 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5755 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5757 // Re-add tools (moving existing ones to new locations)
5758 this.addItems( add
);
5759 // Disabled state may depend on items
5760 this.updateDisabled();
5764 * Destroy tool group.
5766 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5770 this.toolbar
.getToolFactory().disconnect( this );
5771 for ( name
in this.tools
) {
5772 this.toolbar
.releaseTool( this.tools
[name
] );
5773 this.tools
[name
].disconnect( this ).destroy();
5774 delete this.tools
[name
];
5776 this.$element
.remove();
5780 * Dialog for showing a message.
5783 * - Registers two actions by default (safe and primary).
5784 * - Renders action widgets in the footer.
5787 * @extends OO.ui.Dialog
5790 * @param {Object} [config] Configuration options
5792 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5793 // Parent constructor
5794 OO
.ui
.MessageDialog
.super.call( this, config
);
5797 this.verticalActionLayout
= null;
5800 this.$element
.addClass( 'oo-ui-messageDialog' );
5805 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5807 /* Static Properties */
5809 OO
.ui
.MessageDialog
.static.name
= 'message';
5811 OO
.ui
.MessageDialog
.static.size
= 'small';
5813 OO
.ui
.MessageDialog
.static.verbose
= false;
5818 * A confirmation dialog's title should describe what the progressive action will do. An alert
5819 * dialog's title should describe what event occured.
5823 * @property {jQuery|string|Function|null}
5825 OO
.ui
.MessageDialog
.static.title
= null;
5828 * A confirmation dialog's message should describe the consequences of the progressive action. An
5829 * alert dialog's message should describe why the event occured.
5833 * @property {jQuery|string|Function|null}
5835 OO
.ui
.MessageDialog
.static.message
= null;
5837 OO
.ui
.MessageDialog
.static.actions
= [
5838 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5839 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5847 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5849 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5853 * Toggle action layout between vertical and horizontal.
5855 * @param {boolean} [value] Layout actions vertically, omit to toggle
5858 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5859 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5861 if ( value
!== this.verticalActionLayout
) {
5862 this.verticalActionLayout
= value
;
5864 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5865 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5874 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5876 return new OO
.ui
.Process( function () {
5877 this.close( { action
: action
} );
5880 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5886 * @param {Object} [data] Dialog opening data
5887 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5888 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5889 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5890 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5893 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5897 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5898 .next( function () {
5899 this.title
.setLabel(
5900 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5902 this.message
.setLabel(
5903 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5905 this.message
.$element
.toggleClass(
5906 'oo-ui-messageDialog-message-verbose',
5907 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5915 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5916 return Math
.round( this.text
.$element
.outerHeight( true ) );
5922 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5924 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5927 this.$actions
= this.$( '<div>' );
5928 this.container
= new OO
.ui
.PanelLayout( {
5929 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5931 this.text
= new OO
.ui
.PanelLayout( {
5932 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5934 this.message
= new OO
.ui
.LabelWidget( {
5935 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5939 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5940 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5941 this.container
.$element
.append( this.text
.$element
);
5942 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5943 this.$body
.append( this.container
.$element
);
5944 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5945 this.$foot
.append( this.$actions
);
5951 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5952 var i
, len
, other
, special
, others
;
5955 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5957 special
= this.actions
.getSpecial();
5958 others
= this.actions
.getOthers();
5959 if ( special
.safe
) {
5960 this.$actions
.append( special
.safe
.$element
);
5961 special
.safe
.toggleFramed( false );
5963 if ( others
.length
) {
5964 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5966 this.$actions
.append( other
.$element
);
5967 other
.toggleFramed( false );
5970 if ( special
.primary
) {
5971 this.$actions
.append( special
.primary
.$element
);
5972 special
.primary
.toggleFramed( false );
5976 if ( !this.isOpening() ) {
5977 this.manager
.updateWindowSize( this );
5979 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5983 * Fit action actions into columns or rows.
5985 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5987 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5989 actions
= this.actions
.get();
5992 this.toggleVerticalActionLayout( false );
5993 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5994 action
= actions
[i
];
5995 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5996 this.toggleVerticalActionLayout( true );
6003 * Navigation dialog window.
6006 * - Show and hide errors.
6007 * - Retry an action.
6010 * - Renders header with dialog title and one action widget on either side
6011 * (a 'safe' button on the left, and a 'primary' button on the right, both of
6012 * which close the dialog).
6013 * - Displays any action widgets in the footer (none by default).
6014 * - Ability to dismiss errors.
6016 * Subclass responsibilities:
6017 * - Register a 'safe' action.
6018 * - Register a 'primary' action.
6019 * - Add content to the dialog.
6023 * @extends OO.ui.Dialog
6026 * @param {Object} [config] Configuration options
6028 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
6029 // Parent constructor
6030 OO
.ui
.ProcessDialog
.super.call( this, config
);
6033 this.$element
.addClass( 'oo-ui-processDialog' );
6038 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
6043 * Handle dismiss button click events.
6047 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
6052 * Handle retry button click events.
6054 * Hides errors and then tries again.
6056 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
6058 this.executeAction( this.currentAction
.getAction() );
6064 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
6065 if ( this.actions
.isSpecial( action
) ) {
6068 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
6074 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
6076 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
6079 this.$navigation
= this.$( '<div>' );
6080 this.$location
= this.$( '<div>' );
6081 this.$safeActions
= this.$( '<div>' );
6082 this.$primaryActions
= this.$( '<div>' );
6083 this.$otherActions
= this.$( '<div>' );
6084 this.dismissButton
= new OO
.ui
.ButtonWidget( {
6086 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
6088 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
6089 this.$errors
= this.$( '<div>' );
6090 this.$errorsTitle
= this.$( '<div>' );
6093 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
6094 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
6097 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
6099 .append( this.title
.$element
)
6100 .addClass( 'oo-ui-processDialog-location' );
6101 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
6102 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
6103 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
6105 .addClass( 'oo-ui-processDialog-errors-title' )
6106 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
6108 .addClass( 'oo-ui-processDialog-errors' )
6109 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
6111 .addClass( 'oo-ui-processDialog-content' )
6112 .append( this.$errors
);
6114 .addClass( 'oo-ui-processDialog-navigation' )
6115 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
6116 this.$head
.append( this.$navigation
);
6117 this.$foot
.append( this.$otherActions
);
6123 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
6124 var i
, len
, other
, special
, others
;
6127 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
6129 special
= this.actions
.getSpecial();
6130 others
= this.actions
.getOthers();
6131 if ( special
.primary
) {
6132 this.$primaryActions
.append( special
.primary
.$element
);
6133 special
.primary
.toggleFramed( true );
6135 if ( others
.length
) {
6136 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6138 this.$otherActions
.append( other
.$element
);
6139 other
.toggleFramed( true );
6142 if ( special
.safe
) {
6143 this.$safeActions
.append( special
.safe
.$element
);
6144 special
.safe
.toggleFramed( true );
6148 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6154 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
6155 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
6156 .fail( this.showErrors
.bind( this ) );
6160 * Fit label between actions.
6164 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
6165 var width
= Math
.max(
6166 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
6167 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
6169 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
6175 * Handle errors that occured durring accept or reject processes.
6177 * @param {OO.ui.Error[]} errors Errors to be handled
6179 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
6185 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
6186 if ( !errors
[i
].isRecoverable() ) {
6187 recoverable
= false;
6189 if ( errors
[i
].isWarning() ) {
6192 $item
= this.$( '<div>' )
6193 .addClass( 'oo-ui-processDialog-error' )
6194 .append( errors
[i
].getMessage() );
6195 items
.push( $item
[0] );
6197 this.$errorItems
= this.$( items
);
6198 if ( recoverable
) {
6199 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
6201 this.currentAction
.setDisabled( true );
6204 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
6206 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
6208 this.retryButton
.toggle( recoverable
);
6209 this.$errorsTitle
.after( this.$errorItems
);
6210 this.$errors
.show().scrollTop( 0 );
6216 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
6217 this.$errors
.hide();
6218 this.$errorItems
.remove();
6219 this.$errorItems
= null;
6223 * Layout containing a series of pages.
6226 * @extends OO.ui.Layout
6229 * @param {Object} [config] Configuration options
6230 * @cfg {boolean} [continuous=false] Show all pages, one after another
6231 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
6232 * @cfg {boolean} [outlined=false] Show an outline
6233 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
6235 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
6236 // Configuration initialization
6237 config
= config
|| {};
6239 // Parent constructor
6240 OO
.ui
.BookletLayout
.super.call( this, config
);
6243 this.currentPageName
= null;
6245 this.ignoreFocus
= false;
6246 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
6247 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
6248 this.outlineVisible
= false;
6249 this.outlined
= !!config
.outlined
;
6250 if ( this.outlined
) {
6251 this.editable
= !!config
.editable
;
6252 this.outlineControlsWidget
= null;
6253 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget( { $: this.$ } );
6254 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
6255 this.gridLayout
= new OO
.ui
.GridLayout(
6256 [ this.outlinePanel
, this.stackLayout
],
6257 { $: this.$, widths
: [ 1, 2 ] }
6259 this.outlineVisible
= true;
6260 if ( this.editable
) {
6261 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6262 this.outlineSelectWidget
, { $: this.$ }
6268 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6269 if ( this.outlined
) {
6270 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
6272 if ( this.autoFocus
) {
6273 // Event 'focus' does not bubble, but 'focusin' does
6274 this.stackLayout
.onDOMEvent( 'focusin', this.onStackLayoutFocus
.bind( this ) );
6278 this.$element
.addClass( 'oo-ui-bookletLayout' );
6279 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6280 if ( this.outlined
) {
6281 this.outlinePanel
.$element
6282 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6283 .append( this.outlineSelectWidget
.$element
);
6284 if ( this.editable
) {
6285 this.outlinePanel
.$element
6286 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6287 .append( this.outlineControlsWidget
.$element
);
6289 this.$element
.append( this.gridLayout
.$element
);
6291 this.$element
.append( this.stackLayout
.$element
);
6297 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6303 * @param {OO.ui.PageLayout} page Current page
6308 * @param {OO.ui.PageLayout[]} page Added pages
6309 * @param {number} index Index pages were added at
6314 * @param {OO.ui.PageLayout[]} pages Removed pages
6320 * Handle stack layout focus.
6322 * @param {jQuery.Event} e Focusin event
6324 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6327 // Find the page that an element was focused within
6328 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6329 for ( name
in this.pages
) {
6330 // Check for page match, exclude current page to find only page changes
6331 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6332 this.setPage( name
);
6339 * Handle stack layout set events.
6341 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6343 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6344 var $input
, layout
= this;
6346 page
.scrollElementIntoView( { complete: function () {
6347 if ( layout
.autoFocus
) {
6348 // Set focus to the first input if nothing on the page is focused yet
6349 if ( !page
.$element
.find( ':focus' ).length
) {
6350 $input
= page
.$element
.find( ':input:first' );
6351 if ( $input
.length
) {
6361 * Handle outline widget select events.
6363 * @param {OO.ui.OptionWidget|null} item Selected item
6365 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
6367 this.setPage( item
.getData() );
6372 * Check if booklet has an outline.
6376 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6377 return this.outlined
;
6381 * Check if booklet has editing controls.
6385 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6386 return this.editable
;
6390 * Check if booklet has a visible outline.
6394 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6395 return this.outlined
&& this.outlineVisible
;
6399 * Hide or show the outline.
6401 * @param {boolean} [show] Show outline, omit to invert current state
6404 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6405 if ( this.outlined
) {
6406 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6407 this.outlineVisible
= show
;
6408 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6415 * Get the outline widget.
6417 * @param {OO.ui.PageLayout} page Page to be selected
6418 * @return {OO.ui.PageLayout|null} Closest page to another
6420 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6421 var next
, prev
, level
,
6422 pages
= this.stackLayout
.getItems(),
6423 index
= $.inArray( page
, pages
);
6425 if ( index
!== -1 ) {
6426 next
= pages
[index
+ 1];
6427 prev
= pages
[index
- 1];
6428 // Prefer adjacent pages at the same level
6429 if ( this.outlined
) {
6430 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
6433 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
6439 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
6445 return prev
|| next
|| null;
6449 * Get the outline widget.
6451 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if boolet has no outline
6453 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6454 return this.outlineSelectWidget
;
6458 * Get the outline controls widget. If the outline is not editable, null is returned.
6460 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6462 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6463 return this.outlineControlsWidget
;
6467 * Get a page by name.
6469 * @param {string} name Symbolic name of page
6470 * @return {OO.ui.PageLayout|undefined} Page, if found
6472 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6473 return this.pages
[name
];
6477 * Get the current page name.
6479 * @return {string|null} Current page name
6481 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6482 return this.currentPageName
;
6486 * Add a page to the layout.
6488 * When pages are added with the same names as existing pages, the existing pages will be
6489 * automatically removed before the new pages are added.
6491 * @param {OO.ui.PageLayout[]} pages Pages to add
6492 * @param {number} index Index to insert pages after
6496 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6497 var i
, len
, name
, page
, item
, currentIndex
,
6498 stackLayoutPages
= this.stackLayout
.getItems(),
6502 // Remove pages with same names
6503 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6505 name
= page
.getName();
6507 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6508 // Correct the insertion index
6509 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6510 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6513 remove
.push( this.pages
[name
] );
6516 if ( remove
.length
) {
6517 this.removePages( remove
);
6521 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6523 name
= page
.getName();
6524 this.pages
[page
.getName()] = page
;
6525 if ( this.outlined
) {
6526 item
= new OO
.ui
.OutlineOptionWidget( name
, page
, { $: this.$ } );
6527 page
.setOutlineItem( item
);
6532 if ( this.outlined
&& items
.length
) {
6533 this.outlineSelectWidget
.addItems( items
, index
);
6534 this.updateOutlineSelectWidget();
6536 this.stackLayout
.addItems( pages
, index
);
6537 this.emit( 'add', pages
, index
);
6543 * Remove a page from the layout.
6548 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6549 var i
, len
, name
, page
,
6552 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6554 name
= page
.getName();
6555 delete this.pages
[name
];
6556 if ( this.outlined
) {
6557 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
6558 page
.setOutlineItem( null );
6561 if ( this.outlined
&& items
.length
) {
6562 this.outlineSelectWidget
.removeItems( items
);
6563 this.updateOutlineSelectWidget();
6565 this.stackLayout
.removeItems( pages
);
6566 this.emit( 'remove', pages
);
6572 * Clear all pages from the layout.
6577 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6579 pages
= this.stackLayout
.getItems();
6582 this.currentPageName
= null;
6583 if ( this.outlined
) {
6584 this.outlineSelectWidget
.clearItems();
6585 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6586 pages
[i
].setOutlineItem( null );
6589 this.stackLayout
.clearItems();
6591 this.emit( 'remove', pages
);
6597 * Set the current page by name.
6600 * @param {string} name Symbolic name of page
6602 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6605 page
= this.pages
[name
];
6607 if ( name
!== this.currentPageName
) {
6608 if ( this.outlined
) {
6609 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
6610 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6611 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getItemFromData( name
) );
6615 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6616 this.pages
[this.currentPageName
].setActive( false );
6617 // Blur anything focused if the next page doesn't have anything focusable - this
6618 // is not needed if the next page has something focusable because once it is focused
6619 // this blur happens automatically
6620 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6621 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6622 if ( $focused
.length
) {
6627 this.currentPageName
= name
;
6628 this.stackLayout
.setItem( page
);
6629 page
.setActive( true );
6630 this.emit( 'set', page
);
6636 * Call this after adding or removing items from the OutlineSelectWidget.
6640 OO
.ui
.BookletLayout
.prototype.updateOutlineSelectWidget = function () {
6641 // Auto-select first item when nothing is selected anymore
6642 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
6643 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
6650 * Layout made of a field and optional label.
6652 * Available label alignment modes include:
6653 * - left: Label is before the field and aligned away from it, best for when the user will be
6654 * scanning for a specific label in a form with many fields
6655 * - right: Label is before the field and aligned toward it, best for forms the user is very
6656 * familiar with and will tab through field checking quickly to verify which field they are in
6657 * - top: Label is before the field and above it, best for when the user will need to fill out all
6658 * fields from top to bottom in a form with few fields
6659 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6660 * checkboxes or radio buttons
6663 * @extends OO.ui.Layout
6664 * @mixins OO.ui.LabelElement
6667 * @param {OO.ui.Widget} fieldWidget Field widget
6668 * @param {Object} [config] Configuration options
6669 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6670 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6672 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
6673 // Configuration initialization
6674 config
= $.extend( { align
: 'left' }, config
);
6676 // Parent constructor
6677 OO
.ui
.FieldLayout
.super.call( this, config
);
6679 // Mixin constructors
6680 OO
.ui
.LabelElement
.call( this, config
);
6683 this.$field
= this.$( '<div>' );
6684 this.fieldWidget
= fieldWidget
;
6686 if ( config
.help
) {
6687 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6689 classes
: [ 'oo-ui-fieldLayout-help' ],
6694 this.popupButtonWidget
.getPopup().$body
.append(
6696 .text( config
.help
)
6697 .addClass( 'oo-ui-fieldLayout-help-content' )
6699 this.$help
= this.popupButtonWidget
.$element
;
6701 this.$help
= this.$( [] );
6705 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
6706 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
6708 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
6711 this.$element
.addClass( 'oo-ui-fieldLayout' );
6713 .addClass( 'oo-ui-fieldLayout-field' )
6714 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
6715 .append( this.fieldWidget
.$element
);
6716 this.setAlignment( config
.align
);
6721 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6722 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6724 /* Static Properties */
6726 OO
.ui
.FieldLayout
.static.tagName
= 'label';
6731 * Handle field disable events.
6733 * @param {boolean} value Field is disabled
6735 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6736 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6740 * Handle label mouse click events.
6742 * @param {jQuery.Event} e Mouse click event
6744 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6745 this.fieldWidget
.simulateLabelClick();
6752 * @return {OO.ui.Widget} Field widget
6754 OO
.ui
.FieldLayout
.prototype.getField = function () {
6755 return this.fieldWidget
;
6759 * Set the field alignment mode.
6762 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6765 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6766 if ( value
!== this.align
) {
6767 // Default to 'left'
6768 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6772 if ( value
=== 'inline' ) {
6773 this.$element
.append( this.$field
, this.$label
, this.$help
);
6775 this.$element
.append( this.$help
, this.$label
, this.$field
);
6777 // Set classes. The following classes can be used here:
6778 // * oo-ui-fieldLayout-align-left
6779 // * oo-ui-fieldLayout-align-right
6780 // * oo-ui-fieldLayout-align-top
6781 // * oo-ui-fieldLayout-align-inline
6783 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6785 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
6793 * Layout made of a fieldset and optional legend.
6795 * Just add OO.ui.FieldLayout items.
6798 * @extends OO.ui.Layout
6799 * @mixins OO.ui.IconElement
6800 * @mixins OO.ui.LabelElement
6801 * @mixins OO.ui.GroupElement
6804 * @param {Object} [config] Configuration options
6805 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6807 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6808 // Configuration initialization
6809 config
= config
|| {};
6811 // Parent constructor
6812 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6814 // Mixin constructors
6815 OO
.ui
.IconElement
.call( this, config
);
6816 OO
.ui
.LabelElement
.call( this, config
);
6817 OO
.ui
.GroupElement
.call( this, config
);
6821 .addClass( 'oo-ui-fieldsetLayout' )
6822 .prepend( this.$icon
, this.$label
, this.$group
);
6823 if ( $.isArray( config
.items
) ) {
6824 this.addItems( config
.items
);
6830 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6831 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6832 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6833 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6836 * Layout with an HTML form.
6839 * @extends OO.ui.Layout
6842 * @param {Object} [config] Configuration options
6843 * @cfg {string} [method] HTML form `method` attribute
6844 * @cfg {string} [action] HTML form `action` attribute
6845 * @cfg {string} [enctype] HTML form `enctype` attribute
6847 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6848 // Configuration initialization
6849 config
= config
|| {};
6851 // Parent constructor
6852 OO
.ui
.FormLayout
.super.call( this, config
);
6855 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
6859 .addClass( 'oo-ui-formLayout' )
6861 method
: config
.method
,
6862 action
: config
.action
,
6863 enctype
: config
.enctype
6869 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6877 /* Static Properties */
6879 OO
.ui
.FormLayout
.static.tagName
= 'form';
6884 * Handle form submit events.
6886 * @param {jQuery.Event} e Submit event
6889 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6890 this.emit( 'submit' );
6895 * Layout made of proportionally sized columns and rows.
6898 * @extends OO.ui.Layout
6901 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6902 * @param {Object} [config] Configuration options
6903 * @cfg {number[]} [widths] Widths of columns as ratios
6904 * @cfg {number[]} [heights] Heights of rows as ratios
6906 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6909 // Configuration initialization
6910 config
= config
|| {};
6912 // Parent constructor
6913 OO
.ui
.GridLayout
.super.call( this, config
);
6921 this.$element
.addClass( 'oo-ui-gridLayout' );
6922 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6923 this.panels
.push( panels
[i
] );
6924 this.$element
.append( panels
[i
].$element
);
6926 if ( config
.widths
|| config
.heights
) {
6927 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6929 // Arrange in columns by default
6930 widths
= this.panels
.map( function () { return 1; } );
6931 this.layout( widths
, [ 1 ] );
6937 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6952 * Set grid dimensions.
6954 * @param {number[]} widths Widths of columns as ratios
6955 * @param {number[]} heights Heights of rows as ratios
6957 * @throws {Error} If grid is not large enough to fit all panels
6959 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6963 cols
= widths
.length
,
6964 rows
= heights
.length
;
6966 // Verify grid is big enough to fit panels
6967 if ( cols
* rows
< this.panels
.length
) {
6968 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6971 // Sum up denominators
6972 for ( x
= 0; x
< cols
; x
++ ) {
6975 for ( y
= 0; y
< rows
; y
++ ) {
6981 for ( x
= 0; x
< cols
; x
++ ) {
6982 this.widths
[x
] = widths
[x
] / xd
;
6984 for ( y
= 0; y
< rows
; y
++ ) {
6985 this.heights
[y
] = heights
[y
] / yd
;
6989 this.emit( 'layout' );
6993 * Update panel positions and sizes.
6997 OO
.ui
.GridLayout
.prototype.update = function () {
6998 var x
, y
, panel
, width
, height
, dimensions
,
7002 cols
= this.widths
.length
,
7003 rows
= this.heights
.length
;
7005 for ( y
= 0; y
< rows
; y
++ ) {
7006 height
= this.heights
[y
];
7007 for ( x
= 0; x
< cols
; x
++ ) {
7008 width
= this.widths
[x
];
7009 panel
= this.panels
[i
];
7011 width
: Math
.round( width
* 100 ) + '%',
7012 height
: Math
.round( height
* 100 ) + '%',
7013 top
: Math
.round( top
* 100 ) + '%'
7016 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
7017 dimensions
.right
= Math
.round( left
* 100 ) + '%';
7019 dimensions
.left
= Math
.round( left
* 100 ) + '%';
7021 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
7022 if ( width
=== 0 || height
=== 0 ) {
7023 dimensions
.visibility
= 'hidden';
7025 dimensions
.visibility
= '';
7027 panel
.$element
.css( dimensions
);
7035 this.emit( 'update' );
7039 * Get a panel at a given position.
7041 * The x and y position is affected by the current grid layout.
7043 * @param {number} x Horizontal position
7044 * @param {number} y Vertical position
7045 * @return {OO.ui.PanelLayout} The panel at the given postion
7047 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
7048 return this.panels
[ ( x
* this.widths
.length
) + y
];
7052 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
7055 * @extends OO.ui.Layout
7058 * @param {Object} [config] Configuration options
7059 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
7060 * @cfg {boolean} [padded=false] Pad the content from the edges
7061 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
7063 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
7064 // Configuration initialization
7065 config
= $.extend( {
7071 // Parent constructor
7072 OO
.ui
.PanelLayout
.super.call( this, config
);
7075 this.$element
.addClass( 'oo-ui-panelLayout' );
7076 if ( config
.scrollable
) {
7077 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
7079 if ( config
.padded
) {
7080 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
7082 if ( config
.expanded
) {
7083 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
7089 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
7092 * Page within an booklet layout.
7095 * @extends OO.ui.PanelLayout
7098 * @param {string} name Unique symbolic name of page
7099 * @param {Object} [config] Configuration options
7100 * @param {string} [outlineItem] Outline item widget
7102 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
7103 // Configuration initialization
7104 config
= $.extend( { scrollable
: true }, config
);
7106 // Parent constructor
7107 OO
.ui
.PageLayout
.super.call( this, config
);
7111 this.outlineItem
= config
.outlineItem
|| null;
7112 this.active
= false;
7115 this.$element
.addClass( 'oo-ui-pageLayout' );
7120 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
7126 * @param {boolean} active Page is active
7134 * @return {string} Symbolic name of page
7136 OO
.ui
.PageLayout
.prototype.getName = function () {
7141 * Check if page is active.
7143 * @return {boolean} Page is active
7145 OO
.ui
.PageLayout
.prototype.isActive = function () {
7152 * @return {OO.ui.OutlineOptionWidget|null} Outline item widget
7154 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
7155 return this.outlineItem
;
7161 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
7162 * outline item as desired; this method is called for setting (with an object) and unsetting
7163 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
7164 * operating on null instead of an OO.ui.OutlineOptionWidget object.
7166 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline item widget, null to clear
7169 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
7170 this.outlineItem
= outlineItem
|| null;
7171 if ( outlineItem
) {
7172 this.setupOutlineItem();
7178 * Setup outline item.
7180 * @localdoc Subclasses should override this method to adjust the outline item as desired.
7182 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline item widget to setup
7185 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
7190 * Set page active state.
7192 * @param {boolean} Page is active
7195 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
7198 if ( active
!== this.active
) {
7199 this.active
= active
;
7200 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
7201 this.emit( 'active', this.active
);
7206 * Layout containing a series of mutually exclusive pages.
7209 * @extends OO.ui.PanelLayout
7210 * @mixins OO.ui.GroupElement
7213 * @param {Object} [config] Configuration options
7214 * @cfg {boolean} [continuous=false] Show all pages, one after another
7215 * @cfg {string} [icon=''] Symbolic icon name
7216 * @cfg {OO.ui.Layout[]} [items] Layouts to add
7218 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
7219 // Configuration initialization
7220 config
= $.extend( { scrollable
: true }, config
);
7222 // Parent constructor
7223 OO
.ui
.StackLayout
.super.call( this, config
);
7225 // Mixin constructors
7226 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7229 this.currentItem
= null;
7230 this.continuous
= !!config
.continuous
;
7233 this.$element
.addClass( 'oo-ui-stackLayout' );
7234 if ( this.continuous
) {
7235 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
7237 if ( $.isArray( config
.items
) ) {
7238 this.addItems( config
.items
);
7244 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
7245 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
7251 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
7257 * Get the current item.
7259 * @return {OO.ui.Layout|null}
7261 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
7262 return this.currentItem
;
7266 * Unset the current item.
7269 * @param {OO.ui.StackLayout} layout
7272 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7273 var prevItem
= this.currentItem
;
7274 if ( prevItem
=== null ) {
7278 this.currentItem
= null;
7279 this.emit( 'set', null );
7285 * Adding an existing item (by value) will move it.
7287 * @param {OO.ui.Layout[]} items Items to add
7288 * @param {number} [index] Index to insert items after
7291 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7293 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7295 if ( !this.currentItem
&& items
.length
) {
7296 this.setItem( items
[0] );
7305 * Items will be detached, not removed, so they can be used later.
7307 * @param {OO.ui.Layout[]} items Items to remove
7311 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7313 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7315 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7316 if ( this.items
.length
) {
7317 this.setItem( this.items
[0] );
7319 this.unsetCurrentItem();
7329 * Items will be detached, not removed, so they can be used later.
7334 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7335 this.unsetCurrentItem();
7336 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7344 * Any currently shown item will be hidden.
7346 * FIXME: If the passed item to show has not been added in the items list, then
7347 * this method drops it and unsets the current item.
7349 * @param {OO.ui.Layout} item Item to show
7353 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7356 if ( item
!== this.currentItem
) {
7357 if ( !this.continuous
) {
7358 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7359 this.items
[i
].$element
.css( 'display', '' );
7362 if ( $.inArray( item
, this.items
) !== -1 ) {
7363 if ( !this.continuous
) {
7364 item
.$element
.css( 'display', 'block' );
7366 this.currentItem
= item
;
7367 this.emit( 'set', item
);
7369 this.unsetCurrentItem();
7377 * Horizontal bar layout of tools as icon buttons.
7380 * @extends OO.ui.ToolGroup
7383 * @param {OO.ui.Toolbar} toolbar
7384 * @param {Object} [config] Configuration options
7386 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7387 // Parent constructor
7388 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7391 this.$element
.addClass( 'oo-ui-barToolGroup' );
7396 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7398 /* Static Properties */
7400 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7402 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7404 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7407 * Popup list of tools with an icon and optional label.
7411 * @extends OO.ui.ToolGroup
7412 * @mixins OO.ui.IconElement
7413 * @mixins OO.ui.IndicatorElement
7414 * @mixins OO.ui.LabelElement
7415 * @mixins OO.ui.TitledElement
7416 * @mixins OO.ui.ClippableElement
7419 * @param {OO.ui.Toolbar} toolbar
7420 * @param {Object} [config] Configuration options
7421 * @cfg {string} [header] Text to display at the top of the pop-up
7423 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7424 // Configuration initialization
7425 config
= config
|| {};
7427 // Parent constructor
7428 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7430 // Mixin constructors
7431 OO
.ui
.IconElement
.call( this, config
);
7432 OO
.ui
.IndicatorElement
.call( this, config
);
7433 OO
.ui
.LabelElement
.call( this, config
);
7434 OO
.ui
.TitledElement
.call( this, config
);
7435 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7438 this.active
= false;
7439 this.dragging
= false;
7440 this.onBlurHandler
= this.onBlur
.bind( this );
7441 this.$handle
= this.$( '<span>' );
7445 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
7446 'mouseup touchend': this.onHandlePointerUp
.bind( this )
7451 .addClass( 'oo-ui-popupToolGroup-handle' )
7452 .append( this.$icon
, this.$label
, this.$indicator
);
7453 // If the pop-up should have a header, add it to the top of the toolGroup.
7454 // Note: If this feature is useful for other widgets, we could abstract it into an
7455 // OO.ui.HeaderedElement mixin constructor.
7456 if ( config
.header
!== undefined ) {
7458 .prepend( this.$( '<span>' )
7459 .addClass( 'oo-ui-popupToolGroup-header' )
7460 .text( config
.header
)
7464 .addClass( 'oo-ui-popupToolGroup' )
7465 .prepend( this.$handle
);
7470 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7471 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7472 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7473 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7474 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7475 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7477 /* Static Properties */
7484 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7486 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7488 if ( this.isDisabled() && this.isElementAttached() ) {
7489 this.setActive( false );
7494 * Handle focus being lost.
7496 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7498 * @param {jQuery.Event} e Mouse up event
7500 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7501 // Only deactivate when clicking outside the dropdown element
7502 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7503 this.setActive( false );
7510 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7511 // e.which is 0 for touch events, 1 for left mouse button
7512 // Only close toolgroup when a tool was actually selected
7513 // FIXME: this duplicates logic from the parent class
7514 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== this.getTargetTool( e
) ) {
7515 this.setActive( false );
7517 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7521 * Handle mouse up events.
7523 * @param {jQuery.Event} e Mouse up event
7525 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7530 * Handle mouse down events.
7532 * @param {jQuery.Event} e Mouse down event
7534 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7535 // e.which is 0 for touch events, 1 for left mouse button
7536 if ( !this.isDisabled() && e
.which
<= 1 ) {
7537 this.setActive( !this.active
);
7543 * Switch into active mode.
7545 * When active, mouseup events anywhere in the document will trigger deactivation.
7547 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7549 if ( this.active
!== value
) {
7550 this.active
= value
;
7552 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7554 // Try anchoring the popup to the left first
7555 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7556 this.toggleClipping( true );
7557 if ( this.isClippedHorizontally() ) {
7558 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7559 this.toggleClipping( false );
7561 .removeClass( 'oo-ui-popupToolGroup-left' )
7562 .addClass( 'oo-ui-popupToolGroup-right' );
7563 this.toggleClipping( true );
7566 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7567 this.$element
.removeClass(
7568 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7570 this.toggleClipping( false );
7576 * Drop down list layout of tools as labeled icon buttons.
7578 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
7579 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
7580 * may want to use the 'promote' and 'demote' configuration options to achieve this.
7583 * @extends OO.ui.PopupToolGroup
7586 * @param {OO.ui.Toolbar} toolbar
7587 * @param {Object} [config] Configuration options
7588 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
7590 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
7591 * allowed to be collapsed.
7592 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
7594 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7595 // Configuration intialization
7596 config
= config
|| {};
7598 // Properties (must be set before parent constructor, which calls #populate)
7599 this.allowCollapse
= config
.allowCollapse
;
7600 this.forceExpand
= config
.forceExpand
;
7601 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
7602 this.collapsibleTools
= [];
7604 // Parent constructor
7605 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7608 this.$element
.addClass( 'oo-ui-listToolGroup' );
7613 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7615 /* Static Properties */
7617 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7619 OO
.ui
.ListToolGroup
.static.name
= 'list';
7626 OO
.ui
.ListToolGroup
.prototype.populate = function () {
7627 var i
, len
, allowCollapse
= [];
7629 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
7631 // Update the list of collapsible tools
7632 if ( this.allowCollapse
!== undefined ) {
7633 allowCollapse
= this.allowCollapse
;
7634 } else if ( this.forceExpand
!== undefined ) {
7635 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
7638 this.collapsibleTools
= [];
7639 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
7640 if ( this.tools
[ allowCollapse
[i
] ] !== undefined ) {
7641 this.collapsibleTools
.push( this.tools
[ allowCollapse
[i
] ] );
7645 // Keep at the end, even when tools are added
7646 this.$group
.append( this.getExpandCollapseTool().$element
);
7648 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
7650 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
7651 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
7652 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
7653 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
7654 this.getExpandCollapseTool().$element
.css( 'display', 'inline-block' );
7657 this.updateCollapsibleState();
7660 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
7661 if ( this.expandCollapseTool
=== undefined ) {
7662 var ExpandCollapseTool = function () {
7663 ExpandCollapseTool
.super.apply( this, arguments
);
7666 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
7668 ExpandCollapseTool
.prototype.onSelect = function () {
7669 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
7670 this.toolGroup
.updateCollapsibleState();
7671 this.setActive( false );
7673 ExpandCollapseTool
.prototype.onUpdateState = function () {
7674 // Do nothing. Tool interface requires an implementation of this function.
7677 ExpandCollapseTool
.static.name
= 'more-fewer';
7679 this.expandCollapseTool
= new ExpandCollapseTool( this );
7681 return this.expandCollapseTool
;
7687 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
7688 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7690 // Do not close the popup when the user wants to show more/fewer tools
7691 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
7692 // Prevent the popup list from being hidden
7693 this.setActive( true );
7699 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
7702 this.getExpandCollapseTool()
7703 .setIcon( this.expanded
? 'collapse' : 'expand' )
7704 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
7706 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
7707 this.collapsibleTools
[i
].toggle( this.expanded
);
7712 * Drop down menu layout of tools as selectable menu items.
7715 * @extends OO.ui.PopupToolGroup
7718 * @param {OO.ui.Toolbar} toolbar
7719 * @param {Object} [config] Configuration options
7721 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7722 // Configuration initialization
7723 config
= config
|| {};
7725 // Parent constructor
7726 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7729 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7732 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7737 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7739 /* Static Properties */
7741 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7743 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7748 * Handle the toolbar state being updated.
7750 * When the state changes, the title of each active item in the menu will be joined together and
7751 * used as a label for the group. The label will be empty if none of the items are active.
7753 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7757 for ( name
in this.tools
) {
7758 if ( this.tools
[name
].isActive() ) {
7759 labelTexts
.push( this.tools
[name
].getTitle() );
7763 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7767 * Tool that shows a popup when selected.
7771 * @extends OO.ui.Tool
7772 * @mixins OO.ui.PopupElement
7775 * @param {OO.ui.Toolbar} toolbar
7776 * @param {Object} [config] Configuration options
7778 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7779 // Parent constructor
7780 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7782 // Mixin constructors
7783 OO
.ui
.PopupElement
.call( this, config
);
7787 .addClass( 'oo-ui-popupTool' )
7788 .append( this.popup
.$element
);
7793 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7794 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7799 * Handle the tool being selected.
7803 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7804 if ( !this.isDisabled() ) {
7805 this.popup
.toggle();
7807 this.setActive( false );
7812 * Handle the toolbar state being updated.
7816 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7817 this.setActive( false );
7821 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7823 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7827 * @extends OO.ui.GroupElement
7830 * @param {Object} [config] Configuration options
7832 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7833 // Parent constructor
7834 OO
.ui
.GroupWidget
.super.call( this, config
);
7839 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7844 * Set the disabled state of the widget.
7846 * This will also update the disabled state of child widgets.
7848 * @param {boolean} disabled Disable widget
7851 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7855 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7856 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7858 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7860 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7861 this.items
[i
].updateDisabled();
7869 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7871 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7872 * allows bidrectional communication.
7874 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7881 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7888 * Check if widget is disabled.
7890 * Checks parent if present, making disabled state inheritable.
7892 * @return {boolean} Widget is disabled
7894 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7895 return this.disabled
||
7896 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7900 * Set group element is in.
7902 * @param {OO.ui.GroupElement|null} group Group element, null if none
7905 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7907 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7908 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7910 // Initialize item disabled states
7911 this.updateDisabled();
7917 * Mixin that adds a menu showing suggested values for a text input.
7919 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7921 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
7922 * be aware that this will cause new suggestions to be looked up for the new value. If this is
7923 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
7929 * @param {OO.ui.TextInputWidget} input Input widget
7930 * @param {Object} [config] Configuration options
7931 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
7932 * @cfg {jQuery} [$container=input.$element] Element to render menu under
7934 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7935 // Configuration initialization
7936 config
= config
|| {};
7939 this.lookupInput
= input
;
7940 this.$overlay
= config
.$overlay
|| this.$element
;
7941 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
7942 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7943 input
: this.lookupInput
,
7944 $container
: config
.$container
7946 this.lookupCache
= {};
7947 this.lookupQuery
= null;
7948 this.lookupRequest
= null;
7949 this.lookupsDisabled
= false;
7950 this.lookupInputFocused
= false;
7953 this.lookupInput
.$input
.on( {
7954 focus
: this.onLookupInputFocus
.bind( this ),
7955 blur
: this.onLookupInputBlur
.bind( this ),
7956 mousedown
: this.onLookupInputMouseDown
.bind( this )
7958 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7959 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
7962 this.$element
.addClass( 'oo-ui-lookupWidget' );
7963 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7964 this.$overlay
.append( this.lookupMenu
.$element
);
7970 * Handle input focus event.
7972 * @param {jQuery.Event} e Input focus event
7974 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7975 this.lookupInputFocused
= true;
7976 this.populateLookupMenu();
7980 * Handle input blur event.
7982 * @param {jQuery.Event} e Input blur event
7984 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7985 this.closeLookupMenu();
7986 this.lookupInputFocused
= false;
7990 * Handle input mouse down event.
7992 * @param {jQuery.Event} e Input mouse down event
7994 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7995 // Only open the menu if the input was already focused.
7996 // This way we allow the user to open the menu again after closing it with Esc
7997 // by clicking in the input. Opening (and populating) the menu when initially
7998 // clicking into the input is handled by the focus handler.
7999 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
8000 this.populateLookupMenu();
8005 * Handle input change event.
8007 * @param {string} value New input value
8009 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
8010 if ( this.lookupInputFocused
) {
8011 this.populateLookupMenu();
8016 * Handle the lookup menu being shown/hidden.
8017 * @param {boolean} visible Whether the lookup menu is now visible.
8019 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
8021 // When the menu is hidden, abort any active request and clear the menu.
8022 // This has to be done here in addition to closeLookupMenu(), because
8023 // MenuSelectWidget will close itself when the user presses Esc.
8024 this.abortLookupRequest();
8025 this.lookupMenu
.clearItems();
8032 * @return {OO.ui.TextInputMenuSelectWidget}
8034 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
8035 return this.lookupMenu
;
8039 * Disable or re-enable lookups.
8041 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
8043 * @param {boolean} disabled Disable lookups
8045 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
8046 this.lookupsDisabled
= !!disabled
;
8050 * Open the menu. If there are no entries in the menu, this does nothing.
8054 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
8055 if ( !this.lookupMenu
.isEmpty() ) {
8056 this.lookupMenu
.toggle( true );
8062 * Close the menu, empty it, and abort any pending request.
8066 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
8067 this.lookupMenu
.toggle( false );
8068 this.abortLookupRequest();
8069 this.lookupMenu
.clearItems();
8074 * Request menu items based on the input's current value, and when they arrive,
8075 * populate the menu with these items and show the menu.
8077 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
8081 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
8083 value
= this.lookupInput
.getValue();
8085 if ( this.lookupsDisabled
) {
8089 // If the input is empty, clear the menu
8090 if ( value
=== '' ) {
8091 this.closeLookupMenu();
8092 // Skip population if there is already a request pending for the current value
8093 } else if ( value
!== this.lookupQuery
) {
8094 this.getLookupMenuItems()
8095 .done( function ( items
) {
8096 widget
.lookupMenu
.clearItems();
8097 if ( items
.length
) {
8101 widget
.initializeLookupMenuSelection();
8103 widget
.lookupMenu
.toggle( false );
8106 .fail( function () {
8107 widget
.lookupMenu
.clearItems();
8115 * Select and highlight the first selectable item in the menu.
8119 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
8120 if ( !this.lookupMenu
.getSelectedItem() ) {
8121 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
8123 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
8127 * Get lookup menu items for the current query.
8129 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
8130 * of the done event. If the request was aborted to make way for a subsequent request,
8131 * this promise will not be rejected: it will remain pending forever.
8133 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
8135 value
= this.lookupInput
.getValue(),
8136 deferred
= $.Deferred(),
8139 this.abortLookupRequest();
8140 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
8141 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
8143 this.lookupInput
.pushPending();
8144 this.lookupQuery
= value
;
8145 ourRequest
= this.lookupRequest
= this.getLookupRequest();
8147 .always( function () {
8148 // We need to pop pending even if this is an old request, otherwise
8149 // the widget will remain pending forever.
8150 // TODO: this assumes that an aborted request will fail or succeed soon after
8151 // being aborted, or at least eventually. It would be nice if we could popPending()
8152 // at abort time, but only if we knew that we hadn't already called popPending()
8153 // for that request.
8154 widget
.lookupInput
.popPending();
8156 .done( function ( data
) {
8157 // If this is an old request (and aborting it somehow caused it to still succeed),
8158 // ignore its success completely
8159 if ( ourRequest
=== widget
.lookupRequest
) {
8160 widget
.lookupQuery
= null;
8161 widget
.lookupRequest
= null;
8162 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
8163 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
8166 .fail( function () {
8167 // If this is an old request (or a request failing because it's being aborted),
8168 // ignore its failure completely
8169 if ( ourRequest
=== widget
.lookupRequest
) {
8170 widget
.lookupQuery
= null;
8171 widget
.lookupRequest
= null;
8176 return deferred
.promise();
8180 * Abort the currently pending lookup request, if any.
8182 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
8183 var oldRequest
= this.lookupRequest
;
8185 // First unset this.lookupRequest to the fail handler will notice
8186 // that the request is no longer current
8187 this.lookupRequest
= null;
8188 this.lookupQuery
= null;
8194 * Get a new request object of the current lookup query value.
8197 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
8199 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
8200 // Stub, implemented in subclass
8205 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
8208 * @param {Mixed} data Cached result data, usually an array
8209 * @return {OO.ui.MenuOptionWidget[]} Menu items
8211 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
8212 // Stub, implemented in subclass
8217 * Get lookup cache item from server response data.
8220 * @param {Mixed} data Response from server
8221 * @return {Mixed} Cached result data
8223 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
8224 // Stub, implemented in subclass
8229 * Set of controls for an OO.ui.OutlineSelectWidget.
8231 * Controls include moving items up and down, removing items, and adding different kinds of items.
8234 * @extends OO.ui.Widget
8235 * @mixins OO.ui.GroupElement
8236 * @mixins OO.ui.IconElement
8239 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
8240 * @param {Object} [config] Configuration options
8242 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
8243 // Configuration initialization
8244 config
= $.extend( { icon
: 'add' }, config
);
8246 // Parent constructor
8247 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
8249 // Mixin constructors
8250 OO
.ui
.GroupElement
.call( this, config
);
8251 OO
.ui
.IconElement
.call( this, config
);
8254 this.outline
= outline
;
8255 this.$movers
= this.$( '<div>' );
8256 this.upButton
= new OO
.ui
.ButtonWidget( {
8260 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
8262 this.downButton
= new OO
.ui
.ButtonWidget( {
8266 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
8268 this.removeButton
= new OO
.ui
.ButtonWidget( {
8272 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
8276 outline
.connect( this, {
8277 select
: 'onOutlineChange',
8278 add
: 'onOutlineChange',
8279 remove
: 'onOutlineChange'
8281 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
8282 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
8283 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
8286 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
8287 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
8289 .addClass( 'oo-ui-outlineControlsWidget-movers' )
8290 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
8291 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
8296 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
8297 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
8298 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
8304 * @param {number} places Number of places to move
8314 * Handle outline change events.
8316 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
8317 var i
, len
, firstMovable
, lastMovable
,
8318 items
= this.outline
.getItems(),
8319 selectedItem
= this.outline
.getSelectedItem(),
8320 movable
= selectedItem
&& selectedItem
.isMovable(),
8321 removable
= selectedItem
&& selectedItem
.isRemovable();
8326 while ( ++i
< len
) {
8327 if ( items
[i
].isMovable() ) {
8328 firstMovable
= items
[i
];
8334 if ( items
[i
].isMovable() ) {
8335 lastMovable
= items
[i
];
8340 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
8341 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
8342 this.removeButton
.setDisabled( !removable
);
8346 * Mixin for widgets with a boolean on/off state.
8352 * @param {Object} [config] Configuration options
8353 * @cfg {boolean} [value=false] Initial value
8355 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8356 // Configuration initialization
8357 config
= config
|| {};
8363 this.$element
.addClass( 'oo-ui-toggleWidget' );
8364 this.setValue( !!config
.value
);
8371 * @param {boolean} value Changed value
8377 * Get the value of the toggle.
8381 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8386 * Set the value of the toggle.
8388 * @param {boolean} value New value
8392 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8394 if ( this.value
!== value
) {
8396 this.emit( 'change', value
);
8397 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8398 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8404 * Group widget for multiple related buttons.
8406 * Use together with OO.ui.ButtonWidget.
8409 * @extends OO.ui.Widget
8410 * @mixins OO.ui.GroupElement
8413 * @param {Object} [config] Configuration options
8414 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
8416 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
8417 // Configuration initialization
8418 config
= config
|| {};
8420 // Parent constructor
8421 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
8423 // Mixin constructors
8424 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8427 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
8428 if ( $.isArray( config
.items
) ) {
8429 this.addItems( config
.items
);
8435 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
8436 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
8439 * Generic widget for buttons.
8442 * @extends OO.ui.Widget
8443 * @mixins OO.ui.ButtonElement
8444 * @mixins OO.ui.IconElement
8445 * @mixins OO.ui.IndicatorElement
8446 * @mixins OO.ui.LabelElement
8447 * @mixins OO.ui.TitledElement
8448 * @mixins OO.ui.FlaggedElement
8451 * @param {Object} [config] Configuration options
8452 * @cfg {string} [href] Hyperlink to visit when clicked
8453 * @cfg {string} [target] Target to open hyperlink in
8455 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8456 // Configuration initialization
8457 config
= $.extend( { target
: '_blank' }, config
);
8459 // Parent constructor
8460 OO
.ui
.ButtonWidget
.super.call( this, config
);
8462 // Mixin constructors
8463 OO
.ui
.ButtonElement
.call( this, config
);
8464 OO
.ui
.IconElement
.call( this, config
);
8465 OO
.ui
.IndicatorElement
.call( this, config
);
8466 OO
.ui
.LabelElement
.call( this, config
);
8467 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
8468 OO
.ui
.FlaggedElement
.call( this, config
);
8473 this.isHyperlink
= false;
8477 click
: this.onClick
.bind( this ),
8478 keypress
: this.onKeyPress
.bind( this )
8482 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8484 .addClass( 'oo-ui-buttonWidget' )
8485 .append( this.$button
);
8486 this.setHref( config
.href
);
8487 this.setTarget( config
.target
);
8492 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8493 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8494 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8495 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8496 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8497 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8498 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8509 * Handles mouse click events.
8511 * @param {jQuery.Event} e Mouse click event
8514 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8515 if ( !this.isDisabled() ) {
8516 this.emit( 'click' );
8517 if ( this.isHyperlink
) {
8525 * Handles keypress events.
8527 * @param {jQuery.Event} e Keypress event
8530 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8531 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8532 this.emit( 'click' );
8533 if ( this.isHyperlink
) {
8541 * Get hyperlink location.
8543 * @return {string} Hyperlink location
8545 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8550 * Get hyperlink target.
8552 * @return {string} Hyperlink target
8554 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8559 * Set hyperlink location.
8561 * @param {string|null} href Hyperlink location, null to remove
8563 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8564 href
= typeof href
=== 'string' ? href
: null;
8566 if ( href
!== this.href
) {
8568 if ( href
!== null ) {
8569 this.$button
.attr( 'href', href
);
8570 this.isHyperlink
= true;
8572 this.$button
.removeAttr( 'href' );
8573 this.isHyperlink
= false;
8581 * Set hyperlink target.
8583 * @param {string|null} target Hyperlink target, null to remove
8585 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8586 target
= typeof target
=== 'string' ? target
: null;
8588 if ( target
!== this.target
) {
8589 this.target
= target
;
8590 if ( target
!== null ) {
8591 this.$button
.attr( 'target', target
);
8593 this.$button
.removeAttr( 'target' );
8601 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8604 * @extends OO.ui.ButtonWidget
8605 * @mixins OO.ui.PendingElement
8608 * @param {Object} [config] Configuration options
8609 * @cfg {string} [action] Symbolic action name
8610 * @cfg {string[]} [modes] Symbolic mode names
8611 * @cfg {boolean} [framed=false] Render button with a frame
8613 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8614 // Configuration initialization
8615 config
= $.extend( { framed
: false }, config
);
8617 // Parent constructor
8618 OO
.ui
.ActionWidget
.super.call( this, config
);
8620 // Mixin constructors
8621 OO
.ui
.PendingElement
.call( this, config
);
8624 this.action
= config
.action
|| '';
8625 this.modes
= config
.modes
|| [];
8630 this.$element
.addClass( 'oo-ui-actionWidget' );
8635 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8636 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
8647 * Check if action is available in a certain mode.
8649 * @param {string} mode Name of mode
8650 * @return {boolean} Has mode
8652 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8653 return this.modes
.indexOf( mode
) !== -1;
8657 * Get symbolic action name.
8661 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8666 * Get symbolic action name.
8670 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8671 return this.modes
.slice();
8675 * Emit a resize event if the size has changed.
8679 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8682 if ( this.isElementAttached() ) {
8683 width
= this.$element
.width();
8684 height
= this.$element
.height();
8686 if ( width
!== this.width
|| height
!== this.height
) {
8688 this.height
= height
;
8689 this.emit( 'resize' );
8699 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8701 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8702 this.propagateResize();
8710 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8712 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8713 this.propagateResize();
8721 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8723 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8724 this.propagateResize();
8732 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8734 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8735 this.propagateResize();
8741 * Toggle visibility of button.
8743 * @param {boolean} [show] Show button, omit to toggle visibility
8746 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8748 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8749 this.propagateResize();
8755 * Button that shows and hides a popup.
8758 * @extends OO.ui.ButtonWidget
8759 * @mixins OO.ui.PopupElement
8762 * @param {Object} [config] Configuration options
8764 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8765 // Parent constructor
8766 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8768 // Mixin constructors
8769 OO
.ui
.PopupElement
.call( this, config
);
8773 .addClass( 'oo-ui-popupButtonWidget' )
8774 .append( this.popup
.$element
);
8779 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8780 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8785 * Handles mouse click events.
8787 * @param {jQuery.Event} e Mouse click event
8789 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8790 // Skip clicks within the popup
8791 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8795 if ( !this.isDisabled() ) {
8796 this.popup
.toggle();
8798 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8804 * Button that toggles on and off.
8807 * @extends OO.ui.ButtonWidget
8808 * @mixins OO.ui.ToggleWidget
8811 * @param {Object} [config] Configuration options
8812 * @cfg {boolean} [value=false] Initial value
8814 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8815 // Configuration initialization
8816 config
= config
|| {};
8818 // Parent constructor
8819 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8821 // Mixin constructors
8822 OO
.ui
.ToggleWidget
.call( this, config
);
8825 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8830 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8831 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8838 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8839 if ( !this.isDisabled() ) {
8840 this.setValue( !this.value
);
8844 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8850 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8852 if ( value
!== this.value
) {
8853 this.setActive( value
);
8856 // Parent method (from mixin)
8857 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8863 * Dropdown menu of options.
8865 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
8866 * can be accessed using the #getMenu method.
8868 * Use with OO.ui.MenuOptionWidget.
8871 * @extends OO.ui.Widget
8872 * @mixins OO.ui.IconElement
8873 * @mixins OO.ui.IndicatorElement
8874 * @mixins OO.ui.LabelElement
8875 * @mixins OO.ui.TitledElement
8878 * @param {Object} [config] Configuration options
8879 * @cfg {Object} [menu] Configuration options to pass to menu widget
8881 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
8882 // Configuration initialization
8883 config
= $.extend( { indicator
: 'down' }, config
);
8885 // Parent constructor
8886 OO
.ui
.DropdownWidget
.super.call( this, config
);
8888 // Mixin constructors
8889 OO
.ui
.IconElement
.call( this, config
);
8890 OO
.ui
.IndicatorElement
.call( this, config
);
8891 OO
.ui
.LabelElement
.call( this, config
);
8892 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8895 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8896 this.$handle
= this.$( '<span>' );
8899 this.$element
.on( { click
: this.onClick
.bind( this ) } );
8900 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8904 .addClass( 'oo-ui-dropdownWidget-handle' )
8905 .append( this.$icon
, this.$label
, this.$indicator
);
8907 .addClass( 'oo-ui-dropdownWidget' )
8908 .append( this.$handle
, this.menu
.$element
);
8913 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
8914 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
8915 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
8916 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
8917 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
8924 * @return {OO.ui.MenuSelectWidget} Menu of widget
8926 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
8931 * Handles menu select events.
8933 * @param {OO.ui.MenuOptionWidget} item Selected menu item
8935 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
8942 selectedLabel
= item
.getLabel();
8944 // If the label is a DOM element, clone it, because setLabel will append() it
8945 if ( selectedLabel
instanceof jQuery
) {
8946 selectedLabel
= selectedLabel
.clone();
8949 this.setLabel( selectedLabel
);
8953 * Handles mouse click events.
8955 * @param {jQuery.Event} e Mouse click event
8957 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
8958 // Skip clicks within the menu
8959 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8963 if ( !this.isDisabled() ) {
8964 if ( this.menu
.isVisible() ) {
8965 this.menu
.toggle( false );
8967 this.menu
.toggle( true );
8976 * See OO.ui.IconElement for more information.
8979 * @extends OO.ui.Widget
8980 * @mixins OO.ui.IconElement
8981 * @mixins OO.ui.TitledElement
8984 * @param {Object} [config] Configuration options
8986 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8987 // Configuration initialization
8988 config
= config
|| {};
8990 // Parent constructor
8991 OO
.ui
.IconWidget
.super.call( this, config
);
8993 // Mixin constructors
8994 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
8995 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8998 this.$element
.addClass( 'oo-ui-iconWidget' );
9003 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
9004 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
9005 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
9007 /* Static Properties */
9009 OO
.ui
.IconWidget
.static.tagName
= 'span';
9014 * See OO.ui.IndicatorElement for more information.
9017 * @extends OO.ui.Widget
9018 * @mixins OO.ui.IndicatorElement
9019 * @mixins OO.ui.TitledElement
9022 * @param {Object} [config] Configuration options
9024 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
9025 // Configuration initialization
9026 config
= config
|| {};
9028 // Parent constructor
9029 OO
.ui
.IndicatorWidget
.super.call( this, config
);
9031 // Mixin constructors
9032 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
9033 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9036 this.$element
.addClass( 'oo-ui-indicatorWidget' );
9041 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
9042 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
9043 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
9045 /* Static Properties */
9047 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
9050 * Base class for input widgets.
9054 * @extends OO.ui.Widget
9055 * @mixins OO.ui.FlaggedElement
9058 * @param {Object} [config] Configuration options
9059 * @cfg {string} [name=''] HTML input name
9060 * @cfg {string} [value=''] Input value
9061 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
9063 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
9064 // Configuration initialization
9065 config
= config
|| {};
9067 // Parent constructor
9068 OO
.ui
.InputWidget
.super.call( this, config
);
9070 // Mixin constructors
9071 OO
.ui
.FlaggedElement
.call( this, config
);
9074 this.$input
= this.getInputElement( config
);
9076 this.inputFilter
= config
.inputFilter
;
9079 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
9083 .attr( 'name', config
.name
)
9084 .prop( 'disabled', this.isDisabled() );
9085 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
9086 this.setValue( config
.value
);
9091 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
9092 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
9098 * @param {string} value
9104 * Get input element.
9107 * @param {Object} [config] Configuration options
9108 * @return {jQuery} Input element
9110 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
9111 return this.$( '<input>' );
9115 * Handle potentially value-changing events.
9117 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
9119 OO
.ui
.InputWidget
.prototype.onEdit = function () {
9121 if ( !this.isDisabled() ) {
9122 // Allow the stack to clear so the value will be updated
9123 setTimeout( function () {
9124 widget
.setValue( widget
.$input
.val() );
9130 * Get the value of the input.
9132 * @return {string} Input value
9134 OO
.ui
.InputWidget
.prototype.getValue = function () {
9139 * Sets the direction of the current input, either RTL or LTR
9141 * @param {boolean} isRTL
9143 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
9145 this.$input
.removeClass( 'oo-ui-ltr' );
9146 this.$input
.addClass( 'oo-ui-rtl' );
9148 this.$input
.removeClass( 'oo-ui-rtl' );
9149 this.$input
.addClass( 'oo-ui-ltr' );
9154 * Set the value of the input.
9156 * @param {string} value New value
9160 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
9161 value
= this.sanitizeValue( value
);
9162 if ( this.value
!== value
) {
9164 this.emit( 'change', this.value
);
9166 // Update the DOM if it has changed. Note that with sanitizeValue, it
9167 // is possible for the DOM value to change without this.value changing.
9168 if ( this.$input
.val() !== this.value
) {
9169 this.$input
.val( this.value
);
9175 * Sanitize incoming value.
9177 * Ensures value is a string, and converts undefined and null to empty string.
9180 * @param {string} value Original value
9181 * @return {string} Sanitized value
9183 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
9184 if ( value
=== undefined || value
=== null ) {
9186 } else if ( this.inputFilter
) {
9187 return this.inputFilter( String( value
) );
9189 return String( value
);
9194 * Simulate the behavior of clicking on a label bound to this input.
9196 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
9197 if ( !this.isDisabled() ) {
9198 if ( this.$input
.is( ':checkbox,:radio' ) ) {
9199 this.$input
.click();
9200 } else if ( this.$input
.is( ':input' ) ) {
9201 this.$input
[0].focus();
9209 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
9210 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
9211 if ( this.$input
) {
9212 this.$input
.prop( 'disabled', this.isDisabled() );
9222 OO
.ui
.InputWidget
.prototype.focus = function () {
9223 this.$input
[0].focus();
9232 OO
.ui
.InputWidget
.prototype.blur = function () {
9233 this.$input
[0].blur();
9238 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
9241 * @extends OO.ui.InputWidget
9242 * @mixins OO.ui.ButtonElement
9243 * @mixins OO.ui.IconElement
9244 * @mixins OO.ui.IndicatorElement
9245 * @mixins OO.ui.LabelElement
9246 * @mixins OO.ui.TitledElement
9247 * @mixins OO.ui.FlaggedElement
9250 * @param {Object} [config] Configuration options
9251 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
9252 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
9253 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
9254 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
9255 * won't be possible to set a value (which will internally become identical to the label).
9257 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
9258 // Configuration initialization
9259 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
9261 // Properties (must be set before parent constructor, which calls #setValue)
9262 this.useInputTag
= config
.useInputTag
;
9264 // Parent constructor
9265 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
9267 // Mixin constructors
9268 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
9269 OO
.ui
.IconElement
.call( this, config
);
9270 OO
.ui
.IndicatorElement
.call( this, config
);
9271 OO
.ui
.LabelElement
.call( this, config
);
9272 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
9273 OO
.ui
.FlaggedElement
.call( this, config
);
9277 click
: this.onClick
.bind( this ),
9278 keypress
: this.onKeyPress
.bind( this )
9282 if ( !config
.useInputTag
) {
9283 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
9285 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
9290 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
9291 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
9292 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
9293 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
9294 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
9295 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
9296 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
9307 * Get input element.
9310 * @param {Object} [config] Configuration options
9311 * @return {jQuery} Input element
9313 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
9314 // Configuration intialization
9315 config
= config
|| {};
9317 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
9319 return this.$( html
);
9325 * Overridden to support setting the 'value' of `<input/>` elements.
9327 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
9328 * text; or null for no label
9331 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
9332 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
9334 if ( this.useInputTag
) {
9335 if ( typeof label
=== 'function' ) {
9336 label
= OO
.ui
.resolveMsg( label
);
9338 if ( label
instanceof jQuery
) {
9339 label
= label
.text();
9344 this.$input
.val( label
);
9351 * Set the value of the input.
9353 * Overridden to disable for `<input/>` elements, which have value identical to the label.
9355 * @param {string} value New value
9358 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
9359 if ( !this.useInputTag
) {
9360 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
9366 * Handles mouse click events.
9368 * @param {jQuery.Event} e Mouse click event
9371 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
9372 if ( !this.isDisabled() ) {
9373 this.emit( 'click' );
9379 * Handles keypress events.
9381 * @param {jQuery.Event} e Keypress event
9384 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
9385 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9386 this.emit( 'click' );
9392 * Checkbox input widget.
9395 * @extends OO.ui.InputWidget
9398 * @param {Object} [config] Configuration options
9400 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
9401 // Parent constructor
9402 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
9405 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
9410 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
9415 * Get input element.
9418 * @return {jQuery} Input element
9420 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
9421 return this.$( '<input type="checkbox" />' );
9425 * Get checked state of the checkbox
9427 * @return {boolean} If the checkbox is checked
9429 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
9434 * Set checked state of the checkbox
9436 * @param {boolean} value New value
9438 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
9440 if ( this.value
!== value
) {
9442 this.$input
.prop( 'checked', this.value
);
9443 this.emit( 'change', this.value
);
9450 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
9452 if ( !this.isDisabled() ) {
9453 // Allow the stack to clear so the value will be updated
9454 setTimeout( function () {
9455 widget
.setValue( widget
.$input
.prop( 'checked' ) );
9461 * Input widget with a text field.
9464 * @extends OO.ui.InputWidget
9465 * @mixins OO.ui.IconElement
9466 * @mixins OO.ui.IndicatorElement
9467 * @mixins OO.ui.PendingElement
9470 * @param {Object} [config] Configuration options
9471 * @cfg {string} [type='text'] HTML tag `type` attribute
9472 * @cfg {string} [placeholder] Placeholder text
9473 * @cfg {boolean} [readOnly=false] Prevent changes
9474 * @cfg {boolean} [multiline=false] Allow multiple lines of text
9475 * @cfg {boolean} [autosize=false] Automatically resize to fit content
9476 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
9477 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
9478 * one, see #static-validationPatterns)
9480 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
9481 // Configuration initialization
9482 config
= $.extend( { readOnly
: false }, config
);
9484 // Parent constructor
9485 OO
.ui
.TextInputWidget
.super.call( this, config
);
9487 // Mixin constructors
9488 OO
.ui
.IconElement
.call( this, config
);
9489 OO
.ui
.IndicatorElement
.call( this, config
);
9490 OO
.ui
.PendingElement
.call( this, config
);
9493 this.readOnly
= false;
9494 this.multiline
= !!config
.multiline
;
9495 this.autosize
= !!config
.autosize
;
9496 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
9497 this.validate
= null;
9499 this.setValidation( config
.validate
);
9503 keypress
: this.onKeyPress
.bind( this ),
9504 blur
: this.setValidityFlag
.bind( this )
9506 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
9507 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
9508 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
9512 .addClass( 'oo-ui-textInputWidget' )
9513 .append( this.$icon
, this.$indicator
);
9514 this.setReadOnly( config
.readOnly
);
9515 if ( config
.placeholder
) {
9516 this.$input
.attr( 'placeholder', config
.placeholder
);
9518 this.$element
.attr( 'role', 'textbox' );
9523 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
9524 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
9525 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
9526 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
9528 /* Static properties */
9530 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
9538 * User presses enter inside the text box.
9540 * Not called if input is multiline.
9546 * User clicks the icon.
9552 * User clicks the indicator.
9560 * Handle icon mouse down events.
9562 * @param {jQuery.Event} e Mouse down event
9565 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
9566 if ( e
.which
=== 1 ) {
9567 this.$input
[0].focus();
9568 this.emit( 'icon' );
9574 * Handle indicator mouse down events.
9576 * @param {jQuery.Event} e Mouse down event
9579 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
9580 if ( e
.which
=== 1 ) {
9581 this.$input
[0].focus();
9582 this.emit( 'indicator' );
9588 * Handle key press events.
9590 * @param {jQuery.Event} e Key press event
9591 * @fires enter If enter key is pressed and input is not multiline
9593 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
9594 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
9595 this.emit( 'enter' );
9600 * Handle element attach events.
9602 * @param {jQuery.Event} e Element attach event
9604 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
9611 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
9615 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9621 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9623 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9625 this.setValidityFlag();
9631 * Check if the widget is read-only.
9635 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
9636 return this.readOnly
;
9640 * Set the read-only state of the widget.
9642 * This should probably change the widgets's appearance and prevent it from being used.
9644 * @param {boolean} state Make input read-only
9647 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
9648 this.readOnly
= !!state
;
9649 this.$input
.prop( 'readOnly', this.readOnly
);
9654 * Automatically adjust the size of the text input.
9656 * This only affects multi-line inputs that are auto-sized.
9660 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9661 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
9663 if ( this.multiline
&& this.autosize
) {
9664 $clone
= this.$input
.clone()
9665 .val( this.$input
.val() )
9666 // Set inline height property to 0 to measure scroll height
9667 .css( { height
: 0 } )
9668 .insertAfter( this.$input
);
9669 scrollHeight
= $clone
[0].scrollHeight
;
9670 // Remove inline height property to measure natural heights
9671 $clone
.css( 'height', '' );
9672 innerHeight
= $clone
.innerHeight();
9673 outerHeight
= $clone
.outerHeight();
9674 // Measure max rows height
9675 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' ).val( '' );
9676 maxInnerHeight
= $clone
.innerHeight();
9677 // Difference between reported innerHeight and scrollHeight with no scrollbars present
9678 // Equals 1 on Blink-based browsers and 0 everywhere else
9679 measurementError
= maxInnerHeight
- $clone
[0].scrollHeight
;
9681 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
9682 // Only apply inline height when expansion beyond natural height is needed
9683 if ( idealHeight
> innerHeight
) {
9684 // Use the difference between the inner and outer height as a buffer
9685 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
9687 this.$input
.css( 'height', '' );
9694 * Get input element.
9697 * @param {Object} [config] Configuration options
9698 * @return {jQuery} Input element
9700 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9701 // Configuration initialization
9702 config
= config
|| {};
9704 var type
= config
.type
|| 'text';
9706 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + type
+ '" />' );
9710 * Check if input supports multiple lines.
9714 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9715 return !!this.multiline
;
9719 * Check if input automatically adjusts its size.
9723 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9724 return !!this.autosize
;
9728 * Select the contents of the input.
9732 OO
.ui
.TextInputWidget
.prototype.select = function () {
9733 this.$input
.select();
9738 * Sets the validation pattern to use.
9739 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
9740 * one, see #static-validationPatterns)
9742 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
9743 if ( validate
instanceof RegExp
) {
9744 this.validate
= validate
;
9746 this.validate
= this.constructor.static.validationPatterns
[validate
] || /.*/;
9751 * Sets the 'invalid' flag appropriately.
9753 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
9755 this.isValid().done( function ( valid
) {
9756 widget
.setFlags( { invalid
: !valid
} );
9761 * Returns whether or not the current value is considered valid, according to the
9762 * supplied validation pattern.
9764 * @return {jQuery.Deferred}
9766 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
9767 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
9771 * Text input with a menu of optional values.
9774 * @extends OO.ui.Widget
9777 * @param {Object} [config] Configuration options
9778 * @cfg {Object} [menu] Configuration options to pass to menu widget
9779 * @cfg {Object} [input] Configuration options to pass to input widget
9780 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
9782 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9783 // Configuration initialization
9784 config
= config
|| {};
9786 // Parent constructor
9787 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9790 this.$overlay
= config
.$overlay
|| this.$element
;
9791 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9792 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9795 this.menu
= new OO
.ui
.TextInputMenuSelectWidget( this.input
, $.extend(
9797 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
9800 disabled
: this.isDisabled()
9806 this.input
.connect( this, {
9807 change
: 'onInputChange',
9808 indicator
: 'onInputIndicator',
9809 enter
: 'onInputEnter'
9811 this.menu
.connect( this, {
9812 choose
: 'onMenuChoose',
9813 add
: 'onMenuItemsChange',
9814 remove
: 'onMenuItemsChange'
9818 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
9819 this.$overlay
.append( this.menu
.$element
);
9820 this.onMenuItemsChange();
9825 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9830 * Handle input change events.
9832 * @param {string} value New value
9834 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9835 var match
= this.menu
.getItemFromData( value
);
9837 this.menu
.selectItem( match
);
9839 if ( !this.isDisabled() ) {
9840 this.menu
.toggle( true );
9845 * Handle input indicator events.
9847 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9848 if ( !this.isDisabled() ) {
9854 * Handle input enter events.
9856 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
9857 if ( !this.isDisabled() ) {
9858 this.menu
.toggle( false );
9863 * Handle menu choose events.
9865 * @param {OO.ui.OptionWidget} item Chosen item
9867 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
9869 this.input
.setValue( item
.getData() );
9874 * Handle menu item change events.
9876 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
9877 var match
= this.menu
.getItemFromData( this.input
.getValue() );
9878 this.menu
.selectItem( match
);
9879 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
9885 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
9887 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
9890 this.input
.setDisabled( this.isDisabled() );
9893 this.menu
.setDisabled( this.isDisabled() );
9903 * @extends OO.ui.Widget
9904 * @mixins OO.ui.LabelElement
9907 * @param {Object} [config] Configuration options
9909 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
9910 // Configuration initialization
9911 config
= config
|| {};
9913 // Parent constructor
9914 OO
.ui
.LabelWidget
.super.call( this, config
);
9916 // Mixin constructors
9917 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
9918 OO
.ui
.TitledElement
.call( this, config
);
9921 this.input
= config
.input
;
9924 if ( this.input
instanceof OO
.ui
.InputWidget
) {
9925 this.$element
.on( 'click', this.onClick
.bind( this ) );
9929 this.$element
.addClass( 'oo-ui-labelWidget' );
9934 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
9935 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
9936 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
9938 /* Static Properties */
9940 OO
.ui
.LabelWidget
.static.tagName
= 'span';
9945 * Handles label mouse click events.
9947 * @param {jQuery.Event} e Mouse click event
9949 OO
.ui
.LabelWidget
.prototype.onClick = function () {
9950 this.input
.simulateLabelClick();
9955 * Generic option widget for use with OO.ui.SelectWidget.
9958 * @extends OO.ui.Widget
9959 * @mixins OO.ui.LabelElement
9960 * @mixins OO.ui.FlaggedElement
9963 * @param {Mixed} data Option data
9964 * @param {Object} [config] Configuration options
9966 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
9967 // Configuration initialization
9968 config
= config
|| {};
9970 // Parent constructor
9971 OO
.ui
.OptionWidget
.super.call( this, config
);
9973 // Mixin constructors
9974 OO
.ui
.ItemWidget
.call( this );
9975 OO
.ui
.LabelElement
.call( this, config
);
9976 OO
.ui
.FlaggedElement
.call( this, config
);
9980 this.selected
= false;
9981 this.highlighted
= false;
9982 this.pressed
= false;
9986 .data( 'oo-ui-optionWidget', this )
9987 .attr( 'role', 'option' )
9988 .addClass( 'oo-ui-optionWidget' )
9989 .append( this.$label
);
9994 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
9995 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
9996 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
9997 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
9999 /* Static Properties */
10001 OO
.ui
.OptionWidget
.static.selectable
= true;
10003 OO
.ui
.OptionWidget
.static.highlightable
= true;
10005 OO
.ui
.OptionWidget
.static.pressable
= true;
10007 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
10012 * Check if option can be selected.
10014 * @return {boolean} Item is selectable
10016 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
10017 return this.constructor.static.selectable
&& !this.isDisabled();
10021 * Check if option can be highlighted.
10023 * @return {boolean} Item is highlightable
10025 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
10026 return this.constructor.static.highlightable
&& !this.isDisabled();
10030 * Check if option can be pressed.
10032 * @return {boolean} Item is pressable
10034 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
10035 return this.constructor.static.pressable
&& !this.isDisabled();
10039 * Check if option is selected.
10041 * @return {boolean} Item is selected
10043 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
10044 return this.selected
;
10048 * Check if option is highlighted.
10050 * @return {boolean} Item is highlighted
10052 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
10053 return this.highlighted
;
10057 * Check if option is pressed.
10059 * @return {boolean} Item is pressed
10061 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
10062 return this.pressed
;
10066 * Set selected state.
10068 * @param {boolean} [state=false] Select option
10071 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
10072 if ( this.constructor.static.selectable
) {
10073 this.selected
= !!state
;
10074 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
10075 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
10076 this.scrollElementIntoView();
10078 this.updateThemeClasses();
10084 * Set highlighted state.
10086 * @param {boolean} [state=false] Highlight option
10089 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
10090 if ( this.constructor.static.highlightable
) {
10091 this.highlighted
= !!state
;
10092 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
10093 this.updateThemeClasses();
10099 * Set pressed state.
10101 * @param {boolean} [state=false] Press option
10104 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
10105 if ( this.constructor.static.pressable
) {
10106 this.pressed
= !!state
;
10107 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
10108 this.updateThemeClasses();
10114 * Make the option's highlight flash.
10116 * While flashing, the visual style of the pressed state is removed if present.
10118 * @return {jQuery.Promise} Promise resolved when flashing is done
10120 OO
.ui
.OptionWidget
.prototype.flash = function () {
10122 $element
= this.$element
,
10123 deferred
= $.Deferred();
10125 if ( !this.isDisabled() && this.constructor.static.pressable
) {
10126 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
10127 setTimeout( function () {
10128 // Restore original classes
10130 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
10131 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
10133 setTimeout( function () {
10134 deferred
.resolve();
10140 return deferred
.promise();
10146 * @return {Mixed} Option data
10148 OO
.ui
.OptionWidget
.prototype.getData = function () {
10153 * Option widget with an option icon and indicator.
10155 * Use together with OO.ui.SelectWidget.
10158 * @extends OO.ui.OptionWidget
10159 * @mixins OO.ui.IconElement
10160 * @mixins OO.ui.IndicatorElement
10163 * @param {Mixed} data Option data
10164 * @param {Object} [config] Configuration options
10166 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
10167 // Parent constructor
10168 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
10170 // Mixin constructors
10171 OO
.ui
.IconElement
.call( this, config
);
10172 OO
.ui
.IndicatorElement
.call( this, config
);
10176 .addClass( 'oo-ui-decoratedOptionWidget' )
10177 .prepend( this.$icon
)
10178 .append( this.$indicator
);
10183 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
10184 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
10185 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
10188 * Option widget that looks like a button.
10190 * Use together with OO.ui.ButtonSelectWidget.
10193 * @extends OO.ui.DecoratedOptionWidget
10194 * @mixins OO.ui.ButtonElement
10197 * @param {Mixed} data Option data
10198 * @param {Object} [config] Configuration options
10200 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
10201 // Parent constructor
10202 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
10204 // Mixin constructors
10205 OO
.ui
.ButtonElement
.call( this, config
);
10208 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
10209 this.$button
.append( this.$element
.contents() );
10210 this.$element
.append( this.$button
);
10215 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10216 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
10218 /* Static Properties */
10220 // Allow button mouse down events to pass through so they can be handled by the parent select widget
10221 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
10228 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
10229 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
10231 if ( this.constructor.static.selectable
) {
10232 this.setActive( state
);
10239 * Item of an OO.ui.MenuSelectWidget.
10242 * @extends OO.ui.DecoratedOptionWidget
10245 * @param {Mixed} data Item data
10246 * @param {Object} [config] Configuration options
10248 OO
.ui
.MenuOptionWidget
= function OoUiMenuOptionWidget( data
, config
) {
10249 // Configuration initialization
10250 config
= $.extend( { icon
: 'check' }, config
);
10252 // Parent constructor
10253 OO
.ui
.MenuOptionWidget
.super.call( this, data
, config
);
10257 .attr( 'role', 'menuitem' )
10258 .addClass( 'oo-ui-menuOptionWidget' );
10263 OO
.inheritClass( OO
.ui
.MenuOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10266 * Section to group one or more items in a OO.ui.MenuSelectWidget.
10269 * @extends OO.ui.DecoratedOptionWidget
10272 * @param {Mixed} data Item data
10273 * @param {Object} [config] Configuration options
10275 OO
.ui
.MenuSectionOptionWidget
= function OoUiMenuSectionOptionWidget( data
, config
) {
10276 // Parent constructor
10277 OO
.ui
.MenuSectionOptionWidget
.super.call( this, data
, config
);
10280 this.$element
.addClass( 'oo-ui-menuSectionOptionWidget' );
10285 OO
.inheritClass( OO
.ui
.MenuSectionOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10287 /* Static Properties */
10289 OO
.ui
.MenuSectionOptionWidget
.static.selectable
= false;
10291 OO
.ui
.MenuSectionOptionWidget
.static.highlightable
= false;
10294 * Items for an OO.ui.OutlineSelectWidget.
10297 * @extends OO.ui.DecoratedOptionWidget
10300 * @param {Mixed} data Item data
10301 * @param {Object} [config] Configuration options
10302 * @cfg {number} [level] Indentation level
10303 * @cfg {boolean} [movable] Allow modification from outline controls
10305 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( data
, config
) {
10306 // Configuration initialization
10307 config
= config
|| {};
10309 // Parent constructor
10310 OO
.ui
.OutlineOptionWidget
.super.call( this, data
, config
);
10314 this.movable
= !!config
.movable
;
10315 this.removable
= !!config
.removable
;
10318 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
10319 this.setLevel( config
.level
);
10324 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10326 /* Static Properties */
10328 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
10330 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
10332 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
10334 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
10339 * Check if item is movable.
10341 * Movablilty is used by outline controls.
10343 * @return {boolean} Item is movable
10345 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
10346 return this.movable
;
10350 * Check if item is removable.
10352 * Removablilty is used by outline controls.
10354 * @return {boolean} Item is removable
10356 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
10357 return this.removable
;
10361 * Get indentation level.
10363 * @return {number} Indentation level
10365 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
10372 * Movablilty is used by outline controls.
10374 * @param {boolean} movable Item is movable
10377 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
10378 this.movable
= !!movable
;
10379 this.updateThemeClasses();
10384 * Set removability.
10386 * Removablilty is used by outline controls.
10388 * @param {boolean} movable Item is removable
10391 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
10392 this.removable
= !!removable
;
10393 this.updateThemeClasses();
10398 * Set indentation level.
10400 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
10403 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
10404 var levels
= this.constructor.static.levels
,
10405 levelClass
= this.constructor.static.levelClass
,
10408 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
10410 if ( this.level
=== i
) {
10411 this.$element
.addClass( levelClass
+ i
);
10413 this.$element
.removeClass( levelClass
+ i
);
10416 this.updateThemeClasses();
10422 * Container for content that is overlaid and positioned absolutely.
10425 * @extends OO.ui.Widget
10426 * @mixins OO.ui.LabelElement
10429 * @param {Object} [config] Configuration options
10430 * @cfg {number} [width=320] Width of popup in pixels
10431 * @cfg {number} [height] Height of popup, omit to use automatic height
10432 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
10433 * @cfg {string} [align='center'] Alignment of popup to origin
10434 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
10435 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
10436 * @cfg {jQuery} [$content] Content to append to the popup's body
10437 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
10438 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
10439 * @cfg {boolean} [head] Show label and close button at the top
10440 * @cfg {boolean} [padded] Add padding to the body
10442 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
10443 // Configuration initialization
10444 config
= config
|| {};
10446 // Parent constructor
10447 OO
.ui
.PopupWidget
.super.call( this, config
);
10449 // Mixin constructors
10450 OO
.ui
.LabelElement
.call( this, config
);
10451 OO
.ui
.ClippableElement
.call( this, config
);
10454 this.visible
= false;
10455 this.$popup
= this.$( '<div>' );
10456 this.$head
= this.$( '<div>' );
10457 this.$body
= this.$( '<div>' );
10458 this.$anchor
= this.$( '<div>' );
10459 // If undefined, will be computed lazily in updateDimensions()
10460 this.$container
= config
.$container
;
10461 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
10462 this.autoClose
= !!config
.autoClose
;
10463 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
10464 this.transitionTimeout
= null;
10465 this.anchor
= null;
10466 this.width
= config
.width
!== undefined ? config
.width
: 320;
10467 this.height
= config
.height
!== undefined ? config
.height
: null;
10468 this.align
= config
.align
|| 'center';
10469 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
10470 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
10473 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
10476 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
10477 this.$body
.addClass( 'oo-ui-popupWidget-body' );
10478 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
10480 .addClass( 'oo-ui-popupWidget-head' )
10481 .append( this.$label
, this.closeButton
.$element
);
10482 if ( !config
.head
) {
10486 .addClass( 'oo-ui-popupWidget-popup' )
10487 .append( this.$head
, this.$body
);
10490 .addClass( 'oo-ui-popupWidget' )
10491 .append( this.$popup
, this.$anchor
);
10492 // Move content, which was added to #$element by OO.ui.Widget, to the body
10493 if ( config
.$content
instanceof jQuery
) {
10494 this.$body
.append( config
.$content
);
10496 if ( config
.padded
) {
10497 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
10499 this.setClippableElement( this.$body
);
10504 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
10505 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
10506 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
10511 * Handles mouse down events.
10513 * @param {jQuery.Event} e Mouse down event
10515 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
10517 this.isVisible() &&
10518 !$.contains( this.$element
[0], e
.target
) &&
10519 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
10521 this.toggle( false );
10526 * Bind mouse down listener.
10528 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
10529 // Capture clicks outside popup
10530 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
10534 * Handles close button click events.
10536 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
10537 if ( this.isVisible() ) {
10538 this.toggle( false );
10543 * Unbind mouse down listener.
10545 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
10546 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
10550 * Set whether to show a anchor.
10552 * @param {boolean} [show] Show anchor, omit to toggle
10554 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
10555 show
= show
=== undefined ? !this.anchored
: !!show
;
10557 if ( this.anchored
!== show
) {
10559 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
10561 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
10563 this.anchored
= show
;
10568 * Check if showing a anchor.
10570 * @return {boolean} anchor is visible
10572 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
10573 return this.anchor
;
10579 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
10580 show
= show
=== undefined ? !this.isVisible() : !!show
;
10582 var change
= show
!== this.isVisible();
10585 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
10589 if ( this.autoClose
) {
10590 this.bindMouseDownListener();
10592 this.updateDimensions();
10593 this.toggleClipping( true );
10595 this.toggleClipping( false );
10596 if ( this.autoClose
) {
10597 this.unbindMouseDownListener();
10606 * Set the size of the popup.
10608 * Changing the size may also change the popup's position depending on the alignment.
10610 * @param {number} width Width
10611 * @param {number} height Height
10612 * @param {boolean} [transition=false] Use a smooth transition
10615 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
10616 this.width
= width
;
10617 this.height
= height
!== undefined ? height
: null;
10618 if ( this.isVisible() ) {
10619 this.updateDimensions( transition
);
10624 * Update the size and position.
10626 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
10627 * be called automatically.
10629 * @param {boolean} [transition=false] Use a smooth transition
10632 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
10633 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
10634 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
10637 if ( !this.$container
) {
10638 // Lazy-initialize $container if not specified in constructor
10639 this.$container
= this.$( this.getClosestScrollableElementContainer() );
10642 // Set height and width before measuring things, since it might cause our measurements
10643 // to change (e.g. due to scrollbars appearing or disappearing)
10646 height
: this.height
!== null ? this.height
: 'auto'
10649 // Compute initial popupOffset based on alignment
10650 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
10652 // Figure out if this will cause the popup to go beyond the edge of the container
10653 originOffset
= Math
.round( this.$element
.offset().left
);
10654 containerLeft
= Math
.round( this.$container
.offset().left
);
10655 containerWidth
= this.$container
.innerWidth();
10656 containerRight
= containerLeft
+ containerWidth
;
10657 popupLeft
= popupOffset
- this.containerPadding
;
10658 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
10659 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
10660 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10662 // Adjust offset to make the popup not go beyond the edge, if needed
10663 if ( overlapRight
< 0 ) {
10664 popupOffset
+= overlapRight
;
10665 } else if ( overlapLeft
< 0 ) {
10666 popupOffset
-= overlapLeft
;
10669 // Adjust offset to avoid anchor being rendered too close to the edge
10670 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
10671 // TODO: Find a measurement that works for CSS anchors and image anchors
10672 anchorWidth
= this.$anchor
[0].scrollWidth
* 2;
10673 if ( popupOffset
+ this.width
< anchorWidth
) {
10674 popupOffset
= anchorWidth
- this.width
;
10675 } else if ( -popupOffset
< anchorWidth
) {
10676 popupOffset
= -anchorWidth
;
10679 // Prevent transition from being interrupted
10680 clearTimeout( this.transitionTimeout
);
10681 if ( transition
) {
10682 // Enable transition
10683 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10686 // Position body relative to anchor
10687 this.$popup
.css( 'margin-left', popupOffset
);
10689 if ( transition
) {
10690 // Prevent transitioning after transition is complete
10691 this.transitionTimeout
= setTimeout( function () {
10692 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10695 // Prevent transitioning immediately
10696 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10699 // Reevaluate clipping state since we've relocated and resized the popup
10706 * Progress bar widget.
10709 * @extends OO.ui.Widget
10712 * @param {Object} [config] Configuration options
10713 * @cfg {number|boolean} [progress=false] Initial progress percent or false for indeterminate
10715 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
10716 // Configuration initialization
10717 config
= config
|| {};
10719 // Parent constructor
10720 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
10723 this.$bar
= this.$( '<div>' );
10724 this.progress
= null;
10727 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
10728 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar');
10731 role
: 'progressbar',
10732 'aria-valuemin': 0,
10733 'aria-valuemax': 100
10735 .addClass( 'oo-ui-progressBarWidget' )
10736 .append( this.$bar
);
10741 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
10743 /* Static Properties */
10745 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
10750 * Get progress percent
10752 * @return {number} Progress percent
10754 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
10755 return this.progress
;
10759 * Set progress percent
10761 * @param {number|boolean} progress Progress percent or false for indeterminate
10763 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
10764 this.progress
= progress
;
10766 if ( progress
!== false ) {
10767 this.$bar
.css( 'width', this.progress
+ '%' );
10768 this.$element
.attr( 'aria-valuenow', this.progress
);
10770 this.$bar
.css( 'width', '' );
10771 this.$element
.removeAttr( 'aria-valuenow' );
10773 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
10779 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10780 * Results are cleared and populated each time the query is changed.
10783 * @extends OO.ui.Widget
10786 * @param {Object} [config] Configuration options
10787 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10788 * @cfg {string} [value] Initial query value
10790 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10791 // Configuration intialization
10792 config
= config
|| {};
10794 // Parent constructor
10795 OO
.ui
.SearchWidget
.super.call( this, config
);
10798 this.query
= new OO
.ui
.TextInputWidget( {
10801 placeholder
: config
.placeholder
,
10802 value
: config
.value
10804 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10805 this.$query
= this.$( '<div>' );
10806 this.$results
= this.$( '<div>' );
10809 this.query
.connect( this, {
10810 change
: 'onQueryChange',
10811 enter
: 'onQueryEnter'
10813 this.results
.connect( this, {
10814 highlight
: 'onResultsHighlight',
10815 select
: 'onResultsSelect'
10817 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
10821 .addClass( 'oo-ui-searchWidget-query' )
10822 .append( this.query
.$element
);
10824 .addClass( 'oo-ui-searchWidget-results' )
10825 .append( this.results
.$element
);
10827 .addClass( 'oo-ui-searchWidget' )
10828 .append( this.$results
, this.$query
);
10833 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
10839 * @param {Object|null} item Item data or null if no item is highlighted
10844 * @param {Object|null} item Item data or null if no item is selected
10850 * Handle query key down events.
10852 * @param {jQuery.Event} e Key down event
10854 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
10855 var highlightedItem
, nextItem
,
10856 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
10859 highlightedItem
= this.results
.getHighlightedItem();
10860 if ( !highlightedItem
) {
10861 highlightedItem
= this.results
.getSelectedItem();
10863 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
10864 this.results
.highlightItem( nextItem
);
10865 nextItem
.scrollElementIntoView();
10870 * Handle select widget select events.
10872 * Clears existing results. Subclasses should repopulate items according to new query.
10874 * @param {string} value New value
10876 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
10878 this.results
.clearItems();
10882 * Handle select widget enter key events.
10884 * Selects highlighted item.
10886 * @param {string} value New value
10888 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
10890 this.results
.selectItem( this.results
.getHighlightedItem() );
10894 * Handle select widget highlight events.
10896 * @param {OO.ui.OptionWidget} item Highlighted item
10899 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
10900 this.emit( 'highlight', item
? item
.getData() : null );
10904 * Handle select widget select events.
10906 * @param {OO.ui.OptionWidget} item Selected item
10909 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
10910 this.emit( 'select', item
? item
.getData() : null );
10914 * Get the query input.
10916 * @return {OO.ui.TextInputWidget} Query input
10918 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
10923 * Get the results list.
10925 * @return {OO.ui.SelectWidget} Select list
10927 OO
.ui
.SearchWidget
.prototype.getResults = function () {
10928 return this.results
;
10932 * Generic selection of options.
10934 * Items can contain any rendering, and are uniquely identified by a hash of their data. Any widget
10935 * that provides options, from which the user must choose one, should be built on this class.
10937 * Use together with OO.ui.OptionWidget.
10940 * @extends OO.ui.Widget
10941 * @mixins OO.ui.GroupElement
10944 * @param {Object} [config] Configuration options
10945 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
10947 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
10948 // Configuration initialization
10949 config
= config
|| {};
10951 // Parent constructor
10952 OO
.ui
.SelectWidget
.super.call( this, config
);
10954 // Mixin constructors
10955 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
10958 this.pressed
= false;
10959 this.selecting
= null;
10961 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
10962 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
10965 this.$element
.on( {
10966 mousedown
: this.onMouseDown
.bind( this ),
10967 mouseover
: this.onMouseOver
.bind( this ),
10968 mouseleave
: this.onMouseLeave
.bind( this )
10972 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
10973 if ( $.isArray( config
.items
) ) {
10974 this.addItems( config
.items
);
10980 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
10982 // Need to mixin base class as well
10983 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
10984 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
10990 * @param {OO.ui.OptionWidget|null} item Highlighted item
10995 * @param {OO.ui.OptionWidget|null} item Pressed item
11000 * @param {OO.ui.OptionWidget|null} item Selected item
11005 * @param {OO.ui.OptionWidget|null} item Chosen item
11010 * @param {OO.ui.OptionWidget[]} items Added items
11011 * @param {number} index Index items were added at
11016 * @param {OO.ui.OptionWidget[]} items Removed items
11022 * Handle mouse down events.
11025 * @param {jQuery.Event} e Mouse down event
11027 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
11030 if ( !this.isDisabled() && e
.which
=== 1 ) {
11031 this.togglePressed( true );
11032 item
= this.getTargetItem( e
);
11033 if ( item
&& item
.isSelectable() ) {
11034 this.pressItem( item
);
11035 this.selecting
= item
;
11036 this.getElementDocument().addEventListener(
11038 this.onMouseUpHandler
,
11041 this.getElementDocument().addEventListener(
11043 this.onMouseMoveHandler
,
11052 * Handle mouse up events.
11055 * @param {jQuery.Event} e Mouse up event
11057 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
11060 this.togglePressed( false );
11061 if ( !this.selecting
) {
11062 item
= this.getTargetItem( e
);
11063 if ( item
&& item
.isSelectable() ) {
11064 this.selecting
= item
;
11067 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
11068 this.pressItem( null );
11069 this.chooseItem( this.selecting
);
11070 this.selecting
= null;
11073 this.getElementDocument().removeEventListener(
11075 this.onMouseUpHandler
,
11078 this.getElementDocument().removeEventListener(
11080 this.onMouseMoveHandler
,
11088 * Handle mouse move events.
11091 * @param {jQuery.Event} e Mouse move event
11093 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
11096 if ( !this.isDisabled() && this.pressed
) {
11097 item
= this.getTargetItem( e
);
11098 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
11099 this.pressItem( item
);
11100 this.selecting
= item
;
11107 * Handle mouse over events.
11110 * @param {jQuery.Event} e Mouse over event
11112 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
11115 if ( !this.isDisabled() ) {
11116 item
= this.getTargetItem( e
);
11117 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
11123 * Handle mouse leave events.
11126 * @param {jQuery.Event} e Mouse over event
11128 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
11129 if ( !this.isDisabled() ) {
11130 this.highlightItem( null );
11136 * Get the closest item to a jQuery.Event.
11139 * @param {jQuery.Event} e
11140 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
11142 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
11143 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
11144 if ( $item
.length
) {
11145 return $item
.data( 'oo-ui-optionWidget' );
11151 * Get selected item.
11153 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
11155 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
11158 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11159 if ( this.items
[i
].isSelected() ) {
11160 return this.items
[i
];
11167 * Get highlighted item.
11169 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
11171 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
11174 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11175 if ( this.items
[i
].isHighlighted() ) {
11176 return this.items
[i
];
11183 * Get an existing item with equivilant data.
11185 * @param {Object} data Item data to search for
11186 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
11188 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
11189 var hash
= OO
.getHash( data
);
11191 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11192 return this.hashes
[hash
];
11199 * Toggle pressed state.
11201 * @param {boolean} pressed An option is being pressed
11203 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
11204 if ( pressed
=== undefined ) {
11205 pressed
= !this.pressed
;
11207 if ( pressed
!== this.pressed
) {
11209 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
11210 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
11211 this.pressed
= pressed
;
11216 * Highlight an item.
11218 * Highlighting is mutually exclusive.
11220 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
11224 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
11225 var i
, len
, highlighted
,
11228 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11229 highlighted
= this.items
[i
] === item
;
11230 if ( this.items
[i
].isHighlighted() !== highlighted
) {
11231 this.items
[i
].setHighlighted( highlighted
);
11236 this.emit( 'highlight', item
);
11245 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
11249 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
11250 var i
, len
, selected
,
11253 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11254 selected
= this.items
[i
] === item
;
11255 if ( this.items
[i
].isSelected() !== selected
) {
11256 this.items
[i
].setSelected( selected
);
11261 this.emit( 'select', item
);
11270 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
11274 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
11275 var i
, len
, pressed
,
11278 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11279 pressed
= this.items
[i
] === item
;
11280 if ( this.items
[i
].isPressed() !== pressed
) {
11281 this.items
[i
].setPressed( pressed
);
11286 this.emit( 'press', item
);
11295 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
11296 * an item is selected using the keyboard or mouse.
11298 * @param {OO.ui.OptionWidget} item Item to choose
11302 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
11303 this.selectItem( item
);
11304 this.emit( 'choose', item
);
11310 * Get an item relative to another one.
11312 * @param {OO.ui.OptionWidget} item Item to start at
11313 * @param {number} direction Direction to move in, -1 to look backward, 1 to move forward
11314 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
11316 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
11317 var inc
= direction
> 0 ? 1 : -1,
11318 len
= this.items
.length
,
11319 index
= item
instanceof OO
.ui
.OptionWidget
?
11320 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
11321 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
11323 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
11324 Math
.max( index
, -1 ) :
11325 // Default to n-1 instead of -1, if nothing is selected let's start at the end
11326 Math
.min( index
, len
);
11328 while ( len
!== 0 ) {
11329 i
= ( i
+ inc
+ len
) % len
;
11330 item
= this.items
[i
];
11331 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11334 // Stop iterating when we've looped all the way around
11335 if ( i
=== stopAt
) {
11343 * Get the next selectable item.
11345 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
11347 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
11350 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11351 item
= this.items
[i
];
11352 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11363 * When items are added with the same values as existing items, the existing items will be
11364 * automatically removed before the new items are added.
11366 * @param {OO.ui.OptionWidget[]} items Items to add
11367 * @param {number} [index] Index to insert items after
11371 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
11372 var i
, len
, item
, hash
,
11375 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11377 hash
= OO
.getHash( item
.getData() );
11378 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11379 // Remove item with same value
11380 remove
.push( this.hashes
[hash
] );
11382 this.hashes
[hash
] = item
;
11384 if ( remove
.length
) {
11385 this.removeItems( remove
);
11389 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
11391 // Always provide an index, even if it was omitted
11392 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
11400 * Items will be detached, not removed, so they can be used later.
11402 * @param {OO.ui.OptionWidget[]} items Items to remove
11406 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
11407 var i
, len
, item
, hash
;
11409 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11411 hash
= OO
.getHash( item
.getData() );
11412 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11413 // Remove existing item
11414 delete this.hashes
[hash
];
11416 if ( item
.isSelected() ) {
11417 this.selectItem( null );
11422 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
11424 this.emit( 'remove', items
);
11432 * Items will be detached, not removed, so they can be used later.
11437 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
11438 var items
= this.items
.slice();
11443 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
11444 this.selectItem( null );
11446 this.emit( 'remove', items
);
11452 * Select widget containing button options.
11454 * Use together with OO.ui.ButtonOptionWidget.
11457 * @extends OO.ui.SelectWidget
11460 * @param {Object} [config] Configuration options
11462 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
11463 // Parent constructor
11464 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
11467 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
11472 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
11475 * Overlaid menu of options.
11477 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
11480 * Use together with OO.ui.MenuOptionWidget.
11483 * @extends OO.ui.SelectWidget
11484 * @mixins OO.ui.ClippableElement
11487 * @param {Object} [config] Configuration options
11488 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
11489 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
11490 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
11492 OO
.ui
.MenuSelectWidget
= function OoUiMenuSelectWidget( config
) {
11493 // Configuration initialization
11494 config
= config
|| {};
11496 // Parent constructor
11497 OO
.ui
.MenuSelectWidget
.super.call( this, config
);
11499 // Mixin constructors
11500 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
11503 this.flashing
= false;
11504 this.visible
= false;
11505 this.newItems
= null;
11506 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
11507 this.$input
= config
.input
? config
.input
.$input
: null;
11508 this.$widget
= config
.widget
? config
.widget
.$element
: null;
11509 this.$previousFocus
= null;
11510 this.isolated
= !config
.input
;
11511 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
11512 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
11517 .attr( 'role', 'menu' )
11518 .addClass( 'oo-ui-menuSelectWidget' );
11523 OO
.inheritClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.SelectWidget
);
11524 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.ClippableElement
);
11529 * Handles document mouse down events.
11531 * @param {jQuery.Event} e Key down event
11533 OO
.ui
.MenuSelectWidget
.prototype.onDocumentMouseDown = function ( e
) {
11535 !OO
.ui
.contains( this.$element
[0], e
.target
, true ) &&
11536 ( !this.$widget
|| !OO
.ui
.contains( this.$widget
[0], e
.target
, true ) )
11538 this.toggle( false );
11543 * Handles key down events.
11545 * @param {jQuery.Event} e Key down event
11547 OO
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
11550 highlightItem
= this.getHighlightedItem();
11552 if ( !this.isDisabled() && this.isVisible() ) {
11553 if ( !highlightItem
) {
11554 highlightItem
= this.getSelectedItem();
11556 switch ( e
.keyCode
) {
11557 case OO
.ui
.Keys
.ENTER
:
11558 this.chooseItem( highlightItem
);
11561 case OO
.ui
.Keys
.UP
:
11562 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
11565 case OO
.ui
.Keys
.DOWN
:
11566 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
11569 case OO
.ui
.Keys
.ESCAPE
:
11570 if ( highlightItem
) {
11571 highlightItem
.setHighlighted( false );
11573 this.toggle( false );
11579 this.highlightItem( nextItem
);
11580 nextItem
.scrollElementIntoView();
11584 e
.preventDefault();
11585 e
.stopPropagation();
11592 * Bind key down listener.
11594 OO
.ui
.MenuSelectWidget
.prototype.bindKeyDownListener = function () {
11595 if ( this.$input
) {
11596 this.$input
.on( 'keydown', this.onKeyDownHandler
);
11598 // Capture menu navigation keys
11599 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
11604 * Unbind key down listener.
11606 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyDownListener = function () {
11607 if ( this.$input
) {
11608 this.$input
.off( 'keydown' );
11610 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
11617 * This will close the menu when done, unlike selectItem which only changes selection.
11619 * @param {OO.ui.OptionWidget} item Item to choose
11622 OO
.ui
.MenuSelectWidget
.prototype.chooseItem = function ( item
) {
11626 OO
.ui
.MenuSelectWidget
.super.prototype.chooseItem
.call( this, item
);
11628 if ( item
&& !this.flashing
) {
11629 this.flashing
= true;
11630 item
.flash().done( function () {
11631 widget
.toggle( false );
11632 widget
.flashing
= false;
11635 this.toggle( false );
11644 OO
.ui
.MenuSelectWidget
.prototype.addItems = function ( items
, index
) {
11648 OO
.ui
.MenuSelectWidget
.super.prototype.addItems
.call( this, items
, index
);
11651 if ( !this.newItems
) {
11652 this.newItems
= [];
11655 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11657 if ( this.isVisible() ) {
11658 // Defer fitting label until item has been attached
11661 this.newItems
.push( item
);
11665 // Reevaluate clipping
11674 OO
.ui
.MenuSelectWidget
.prototype.removeItems = function ( items
) {
11676 OO
.ui
.MenuSelectWidget
.super.prototype.removeItems
.call( this, items
);
11678 // Reevaluate clipping
11687 OO
.ui
.MenuSelectWidget
.prototype.clearItems = function () {
11689 OO
.ui
.MenuSelectWidget
.super.prototype.clearItems
.call( this );
11691 // Reevaluate clipping
11700 OO
.ui
.MenuSelectWidget
.prototype.toggle = function ( visible
) {
11701 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
11704 change
= visible
!== this.isVisible(),
11705 elementDoc
= this.getElementDocument(),
11706 widgetDoc
= this.$widget
? this.$widget
[0].ownerDocument
: null;
11709 OO
.ui
.MenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
11713 this.bindKeyDownListener();
11715 // Change focus to enable keyboard navigation
11716 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
11717 this.$previousFocus
= this.$( ':focus' );
11718 this.$input
[0].focus();
11720 if ( this.newItems
&& this.newItems
.length
) {
11721 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
11722 this.newItems
[i
].fitLabel();
11724 this.newItems
= null;
11726 this.toggleClipping( true );
11729 if ( this.autoHide
) {
11730 elementDoc
.addEventListener(
11731 'mousedown', this.onDocumentMouseDownHandler
, true
11733 // Support $widget being in a different document
11734 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11735 widgetDoc
.addEventListener(
11736 'mousedown', this.onDocumentMouseDownHandler
, true
11741 this.unbindKeyDownListener();
11742 if ( this.isolated
&& this.$previousFocus
) {
11743 this.$previousFocus
[0].focus();
11744 this.$previousFocus
= null;
11746 elementDoc
.removeEventListener(
11747 'mousedown', this.onDocumentMouseDownHandler
, true
11749 // Support $widget being in a different document
11750 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11751 widgetDoc
.removeEventListener(
11752 'mousedown', this.onDocumentMouseDownHandler
, true
11755 this.toggleClipping( false );
11763 * Menu for a text input widget.
11765 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11766 * is in a different frame, the menu's position is automatically calculated and maintained when the
11767 * menu is toggled or the window is resized.
11770 * @extends OO.ui.MenuSelectWidget
11773 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11774 * @param {Object} [config] Configuration options
11775 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11777 OO
.ui
.TextInputMenuSelectWidget
= function OoUiTextInputMenuSelectWidget( input
, config
) {
11778 // Configuration intialization
11779 config
= config
|| {};
11781 // Parent constructor
11782 OO
.ui
.TextInputMenuSelectWidget
.super.call( this, config
);
11785 this.input
= input
;
11786 this.$container
= config
.$container
|| this.input
.$element
;
11787 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
11790 this.$element
.addClass( 'oo-ui-textInputMenuSelectWidget' );
11795 OO
.inheritClass( OO
.ui
.TextInputMenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
11800 * Handle window resize event.
11802 * @param {jQuery.Event} e Window resize event
11804 OO
.ui
.TextInputMenuSelectWidget
.prototype.onWindowResize = function () {
11811 OO
.ui
.TextInputMenuSelectWidget
.prototype.toggle = function ( visible
) {
11812 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
11814 var change
= visible
!== this.isVisible();
11816 if ( change
&& visible
) {
11817 // Make sure the width is set before the parent method runs.
11818 // After this we have to call this.position(); again to actually
11819 // position ourselves correctly.
11824 OO
.ui
.TextInputMenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
11827 if ( this.isVisible() ) {
11829 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11831 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
11839 * Position the menu.
11843 OO
.ui
.TextInputMenuSelectWidget
.prototype.position = function () {
11844 var $container
= this.$container
,
11845 pos
= OO
.ui
.Element
.getRelativePosition( $container
, this.$element
.offsetParent() );
11847 // Position under input
11848 pos
.top
+= $container
.height();
11849 this.$element
.css( pos
);
11852 this.setIdealSize( $container
.width() );
11853 // We updated the position, so re-evaluate the clipping state
11860 * Structured list of items.
11862 * Use with OO.ui.OutlineOptionWidget.
11865 * @extends OO.ui.SelectWidget
11868 * @param {Object} [config] Configuration options
11870 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
11871 // Configuration initialization
11872 config
= config
|| {};
11874 // Parent constructor
11875 OO
.ui
.OutlineSelectWidget
.super.call( this, config
);
11878 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
11883 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
11886 * Switch that slides on and off.
11889 * @extends OO.ui.Widget
11890 * @mixins OO.ui.ToggleWidget
11893 * @param {Object} [config] Configuration options
11894 * @cfg {boolean} [value=false] Initial value
11896 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
11897 // Parent constructor
11898 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
11900 // Mixin constructors
11901 OO
.ui
.ToggleWidget
.call( this, config
);
11904 this.dragging
= false;
11905 this.dragStart
= null;
11906 this.sliding
= false;
11907 this.$glow
= this.$( '<span>' );
11908 this.$grip
= this.$( '<span>' );
11911 this.$element
.on( 'click', this.onClick
.bind( this ) );
11914 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
11915 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
11917 .addClass( 'oo-ui-toggleSwitchWidget' )
11918 .append( this.$glow
, this.$grip
);
11923 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
11924 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
11929 * Handle mouse down events.
11931 * @param {jQuery.Event} e Mouse down event
11933 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
11934 if ( !this.isDisabled() && e
.which
=== 1 ) {
11935 this.setValue( !this.value
);