2 * OOjs UI v0.1.0-pre (f1abca8e82)
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-17T23:49:27Z
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 * @returns {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
1467 OO
.ui
.Window
= function OoUiWindow( config
) {
1468 // Configuration initialization
1469 config
= config
|| {};
1471 // Parent constructor
1472 OO
.ui
.Window
.super.call( this, config
);
1474 // Mixin constructors
1475 OO
.EventEmitter
.call( this );
1478 this.manager
= null;
1479 this.initialized
= false;
1480 this.visible
= false;
1481 this.opening
= null;
1482 this.closing
= null;
1485 this.loading
= null;
1486 this.size
= config
.size
|| this.constructor.static.size
;
1487 this.$frame
= this.$( '<div>' );
1488 this.$overlay
= this.$( '<div>' );
1492 .addClass( 'oo-ui-window' )
1493 .append( this.$frame
, this.$overlay
);
1494 this.$frame
.addClass( 'oo-ui-window-frame' );
1495 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1497 // NOTE: Additional intitialization will occur when #setManager is called
1502 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1503 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1505 /* Static Properties */
1508 * Symbolic name of size.
1510 * Size is used if no size is configured during construction.
1514 * @property {string}
1516 OO
.ui
.Window
.static.size
= 'medium';
1518 /* Static Methods */
1521 * Transplant the CSS styles from as parent document to a frame's document.
1523 * This loops over the style sheets in the parent document, and copies their nodes to the
1524 * frame's document. It then polls the document to see when all styles have loaded, and once they
1525 * have, resolves the promise.
1527 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1528 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1529 * Firefox, where the styles won't load until the iframe becomes visible.
1531 * For details of how we arrived at the strategy used in this function, see #load.
1535 * @param {HTMLDocument} parentDoc Document to transplant styles from
1536 * @param {HTMLDocument} frameDoc Document to transplant styles to
1537 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1538 * @return {jQuery.Promise} Promise resolved when styles have loaded
1540 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1541 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1542 $pollNodes
= $( [] ),
1543 // Fake font-family value
1544 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1545 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1546 deferred
= $.Deferred();
1548 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1549 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1550 if ( styleNode
.disabled
) {
1554 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1555 // External stylesheet; use @import
1556 styleText
= '@import url(' + styleNode
.href
+ ');';
1558 // Internal stylesheet; just copy the text
1559 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1560 // other browsers, so fall back to '' rather than 'undefined'
1561 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1564 // Create a node with a unique ID that we're going to monitor to see when the CSS
1566 if ( styleNode
.oouiFrameTransplantStylesId
) {
1567 // If we're nesting transplantStyles operations and this node already has
1568 // a CSS rule to wait for loading, reuse it
1569 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1571 // Otherwise, create a new ID
1572 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1575 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1576 // The font-family rule will only take effect once the @import finishes
1577 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1580 // Create a node with id=pollNodeId
1581 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1582 .attr( 'id', pollNodeId
)
1583 .appendTo( frameDoc
.body
)
1586 // Add our modified CSS as a <style> tag
1587 newNode
= frameDoc
.createElement( 'style' );
1588 newNode
.textContent
= styleText
;
1589 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1590 frameDoc
.head
.appendChild( newNode
);
1592 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1594 // Poll every 100ms until all external stylesheets have loaded
1595 $pendingPollNodes
= $pollNodes
;
1596 timeoutID
= setTimeout( function pollExternalStylesheets() {
1598 $pendingPollNodes
.length
> 0 &&
1599 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1601 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1604 if ( $pendingPollNodes
.length
=== 0 ) {
1606 if ( timeoutID
!== null ) {
1608 $pollNodes
.remove();
1612 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1615 // ...but give up after a while
1616 if ( timeout
!== 0 ) {
1617 setTimeout( function () {
1619 clearTimeout( timeoutID
);
1621 $pollNodes
.remove();
1624 }, timeout
|| 5000 );
1627 return deferred
.promise();
1633 * Handle mouse down events.
1635 * @param {jQuery.Event} e Mouse down event
1637 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1638 // Prevent clicking on the click-block from stealing focus
1639 if ( e
.target
=== this.$element
[0] ) {
1645 * Check if window has been initialized.
1647 * @return {boolean} Window has been initialized
1649 OO
.ui
.Window
.prototype.isInitialized = function () {
1650 return this.initialized
;
1654 * Check if window is visible.
1656 * @return {boolean} Window is visible
1658 OO
.ui
.Window
.prototype.isVisible = function () {
1659 return this.visible
;
1663 * Check if window is loading.
1665 * @return {boolean} Window is loading
1667 OO
.ui
.Window
.prototype.isLoading = function () {
1668 return this.loading
&& this.loading
.state() === 'pending';
1672 * Check if window is loaded.
1674 * @return {boolean} Window is loaded
1676 OO
.ui
.Window
.prototype.isLoaded = function () {
1677 return this.loading
&& this.loading
.state() === 'resolved';
1681 * Check if window is opening.
1683 * This is a wrapper around OO.ui.WindowManager#isOpening.
1685 * @return {boolean} Window is opening
1687 OO
.ui
.Window
.prototype.isOpening = function () {
1688 return this.manager
.isOpening( this );
1692 * Check if window is closing.
1694 * This is a wrapper around OO.ui.WindowManager#isClosing.
1696 * @return {boolean} Window is closing
1698 OO
.ui
.Window
.prototype.isClosing = function () {
1699 return this.manager
.isClosing( this );
1703 * Check if window is opened.
1705 * This is a wrapper around OO.ui.WindowManager#isOpened.
1707 * @return {boolean} Window is opened
1709 OO
.ui
.Window
.prototype.isOpened = function () {
1710 return this.manager
.isOpened( this );
1714 * Get the window manager.
1716 * @return {OO.ui.WindowManager} Manager of window
1718 OO
.ui
.Window
.prototype.getManager = function () {
1719 return this.manager
;
1723 * Get the window size.
1725 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1727 OO
.ui
.Window
.prototype.getSize = function () {
1732 * Get the height of the dialog contents.
1734 * @return {number} Content height
1736 OO
.ui
.Window
.prototype.getContentHeight = function () {
1737 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements
1738 var bodyHeight
, oldHeight
= this.$frame
[0].style
.height
;
1739 this.$frame
[0].style
.height
= '1px';
1740 bodyHeight
= this.getBodyHeight();
1741 this.$frame
[0].style
.height
= oldHeight
;
1744 // Add buffer for border
1745 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1746 // Use combined heights of children
1747 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1752 * Get the height of the dialog contents.
1754 * When this function is called, the dialog will temporarily have been resized
1755 * to height=1px, so .scrollHeight measurements can be taken accurately.
1757 * @return {number} Height of content
1759 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1760 return this.$body
[0].scrollHeight
;
1764 * Get the directionality of the frame
1766 * @return {string} Directionality, 'ltr' or 'rtl'
1768 OO
.ui
.Window
.prototype.getDir = function () {
1773 * Get a process for setting up a window for use.
1775 * Each time the window is opened this process will set it up for use in a particular context, based
1776 * on the `data` argument.
1778 * When you override this method, you can add additional setup steps to the process the parent
1779 * method provides using the 'first' and 'next' methods.
1782 * @param {Object} [data] Window opening data
1783 * @return {OO.ui.Process} Setup process
1785 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1786 return new OO
.ui
.Process();
1790 * Get a process for readying a window for use.
1792 * Each time the window is open and setup, this process will ready it up for use in a particular
1793 * context, based on the `data` argument.
1795 * When you override this method, you can add additional setup steps to the process the parent
1796 * method provides using the 'first' and 'next' methods.
1799 * @param {Object} [data] Window opening data
1800 * @return {OO.ui.Process} Setup process
1802 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1803 return new OO
.ui
.Process();
1807 * Get a process for holding a window from use.
1809 * Each time the window is closed, this process will hold it from use in a particular context, based
1810 * on the `data` argument.
1812 * When you override this method, you can add additional setup steps to the process the parent
1813 * method provides using the 'first' and 'next' methods.
1816 * @param {Object} [data] Window closing data
1817 * @return {OO.ui.Process} Hold process
1819 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1820 return new OO
.ui
.Process();
1824 * Get a process for tearing down a window after use.
1826 * Each time the window is closed this process will tear it down and do something with the user's
1827 * interactions within the window, based on the `data` argument.
1829 * When you override this method, you can add additional teardown steps to the process the parent
1830 * method provides using the 'first' and 'next' methods.
1833 * @param {Object} [data] Window closing data
1834 * @return {OO.ui.Process} Teardown process
1836 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1837 return new OO
.ui
.Process();
1841 * Toggle visibility of window.
1843 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1844 * instead of display.
1846 * @param {boolean} [show] Make window visible, omit to toggle visibility
1850 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1851 show
= show
=== undefined ? !this.visible
: !!show
;
1853 if ( show
!== this.isVisible() ) {
1854 this.visible
= show
;
1856 if ( this.isolated
&& !this.isLoaded() ) {
1857 // Hide the window using visibility instead of display until loading is complete
1858 // Can't use display: none; because that prevents the iframe from loading in Firefox
1859 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1861 this.$element
.toggle( show
).css( 'visibility', '' );
1863 this.emit( 'toggle', show
);
1870 * Set the window manager.
1872 * This must be called before initialize. Calling it more than once will cause an error.
1874 * @param {OO.ui.WindowManager} manager Manager for this window
1875 * @throws {Error} If called more than once
1878 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1879 if ( this.manager
) {
1880 throw new Error( 'Cannot set window manager, window already has a manager' );
1884 this.manager
= manager
;
1885 this.isolated
= manager
.shouldIsolate();
1888 if ( this.isolated
) {
1889 this.$iframe
= this.$( '<iframe>' );
1890 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1891 this.$frame
.append( this.$iframe
);
1892 this.$ = function () {
1893 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1895 // WARNING: Do not use this.$ again until #initialize is called
1897 this.$content
= this.$( '<div>' );
1898 this.$document
= $( this.getElementDocument() );
1899 this.$content
.addClass( 'oo-ui-window-content' );
1900 this.$frame
.append( this.$content
);
1902 this.toggle( false );
1904 // Figure out directionality:
1905 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1911 * Set the window size.
1913 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1916 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1918 this.manager
.updateWindowSize( this );
1923 * Set window dimensions.
1925 * Properties are applied to the frame container.
1927 * @param {Object} dim CSS dimension properties
1928 * @param {string|number} [dim.width] Width
1929 * @param {string|number} [dim.minWidth] Minimum width
1930 * @param {string|number} [dim.maxWidth] Maximum width
1931 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1932 * @param {string|number} [dim.minWidth] Minimum height
1933 * @param {string|number} [dim.maxWidth] Maximum height
1936 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1937 // Apply width before height so height is not based on wrapping content using the wrong width
1939 width
: dim
.width
|| '',
1940 minWidth
: dim
.minWidth
|| '',
1941 maxWidth
: dim
.maxWidth
|| ''
1944 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1945 minHeight
: dim
.minHeight
|| '',
1946 maxHeight
: dim
.maxHeight
|| ''
1952 * Initialize window contents.
1954 * The first time the window is opened, #initialize is called when it's safe to begin populating
1955 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1957 * Once this method is called, this.$ can be used to create elements within the frame.
1959 * @throws {Error} If not attached to a manager
1962 OO
.ui
.Window
.prototype.initialize = function () {
1963 if ( !this.manager
) {
1964 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1968 this.$head
= this.$( '<div>' );
1969 this.$body
= this.$( '<div>' );
1970 this.$foot
= this.$( '<div>' );
1971 this.$innerOverlay
= this.$( '<div>' );
1974 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
1977 this.$head
.addClass( 'oo-ui-window-head' );
1978 this.$body
.addClass( 'oo-ui-window-body' );
1979 this.$foot
.addClass( 'oo-ui-window-foot' );
1980 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
1981 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
1989 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1990 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1992 * @param {Object} [data] Window opening data
1993 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1994 * first argument will be a promise which will be resolved when the window begins closing
1996 OO
.ui
.Window
.prototype.open = function ( data
) {
1997 return this.manager
.openWindow( this, data
);
2003 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
2004 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
2006 * @param {Object} [data] Window closing data
2007 * @return {jQuery.Promise} Promise resolved when window is closed
2009 OO
.ui
.Window
.prototype.close = function ( data
) {
2010 return this.manager
.closeWindow( this, data
);
2016 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
2019 * @param {Object} [data] Window opening data
2020 * @return {jQuery.Promise} Promise resolved when window is setup
2022 OO
.ui
.Window
.prototype.setup = function ( data
) {
2024 deferred
= $.Deferred();
2026 this.$element
.show();
2027 this.visible
= true;
2028 this.getSetupProcess( data
).execute().done( function () {
2029 // Force redraw by asking the browser to measure the elements' widths
2030 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2031 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2035 return deferred
.promise();
2041 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
2044 * @param {Object} [data] Window opening data
2045 * @return {jQuery.Promise} Promise resolved when window is ready
2047 OO
.ui
.Window
.prototype.ready = function ( data
) {
2049 deferred
= $.Deferred();
2051 this.$content
.focus();
2052 this.getReadyProcess( data
).execute().done( function () {
2053 // Force redraw by asking the browser to measure the elements' widths
2054 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2055 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2059 return deferred
.promise();
2065 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2068 * @param {Object} [data] Window closing data
2069 * @return {jQuery.Promise} Promise resolved when window is held
2071 OO
.ui
.Window
.prototype.hold = function ( data
) {
2073 deferred
= $.Deferred();
2075 this.getHoldProcess( data
).execute().done( function () {
2076 // Get the focused element within the window's content
2077 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
2079 // Blur the focused element
2080 if ( $focus
.length
) {
2084 // Force redraw by asking the browser to measure the elements' widths
2085 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2086 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2090 return deferred
.promise();
2096 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2099 * @param {Object} [data] Window closing data
2100 * @return {jQuery.Promise} Promise resolved when window is torn down
2102 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2104 deferred
= $.Deferred();
2106 this.getTeardownProcess( data
).execute().done( function () {
2107 // Force redraw by asking the browser to measure the elements' widths
2108 win
.$element
.removeClass( 'oo-ui-window-load oo-ui-window-setup' ).width();
2109 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2110 win
.$element
.hide();
2111 win
.visible
= false;
2115 return deferred
.promise();
2119 * Load the frame contents.
2121 * Once the iframe's stylesheets are loaded the returned promise will be resolved. Calling while
2122 * loading will return a promise but not trigger a new loading cycle. Calling after loading is
2123 * complete will return a promise that's already been resolved.
2125 * Sounds simple right? Read on...
2127 * When you create a dynamic iframe using open/write/close, the window.load event for the
2128 * iframe is triggered when you call close, and there's no further load event to indicate that
2129 * everything is actually loaded.
2131 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2132 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2133 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2134 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2135 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2137 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2138 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2139 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2140 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2141 * accessing .cssRules is allowed.
2143 * However, now that we control the styles we're injecting, we might as well do away with
2144 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2145 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2146 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2147 * font-family rule is not applied until after the `@import` finishes.
2149 * All this stylesheet injection and polling magic is in #transplantStyles.
2151 * @return {jQuery.Promise} Promise resolved when loading is complete
2153 OO
.ui
.Window
.prototype.load = function () {
2154 var sub
, doc
, loading
,
2157 this.$element
.addClass( 'oo-ui-window-load' );
2159 // Non-isolated windows are already "loaded"
2160 if ( !this.loading
&& !this.isolated
) {
2161 this.loading
= $.Deferred().resolve();
2163 // Set initialized state after so sub-classes aren't confused by it being set by calling
2164 // their parent initialize method
2165 this.initialized
= true;
2168 // Return existing promise if already loading or loaded
2169 if ( this.loading
) {
2170 return this.loading
.promise();
2174 loading
= this.loading
= $.Deferred();
2175 sub
= this.$iframe
.prop( 'contentWindow' );
2178 // Initialize contents
2183 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2184 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2185 '<div class="oo-ui-window-content"></div>' +
2192 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$iframe
);
2193 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2194 this.$document
= this.$( doc
);
2197 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2198 .always( function () {
2199 // Initialize isolated windows
2201 // Set initialized state after so sub-classes aren't confused by it being set by calling
2202 // their parent initialize method
2203 win
.initialized
= true;
2204 // Undo the visibility: hidden; hack and apply display: none;
2205 // We can do this safely now that the iframe has initialized
2206 // (don't do this from within #initialize because it has to happen
2207 // after the all subclasses have been handled as well).
2208 win
.toggle( win
.isVisible() );
2213 return loading
.promise();
2217 * Base class for all dialogs.
2220 * - Manage the window (open and close, etc.).
2221 * - Store the internal name and display title.
2222 * - A stack to track one or more pending actions.
2223 * - Manage a set of actions that can be performed.
2224 * - Configure and create action widgets.
2227 * - Close the dialog with Escape key.
2228 * - Visually lock the dialog while an action is in
2229 * progress (aka "pending").
2231 * Subclass responsibilities:
2232 * - Display the title somewhere.
2233 * - Add content to the dialog.
2234 * - Provide a UI to close the dialog.
2235 * - Display the action widgets somewhere.
2239 * @extends OO.ui.Window
2240 * @mixins OO.ui.PendingElement
2243 * @param {Object} [config] Configuration options
2245 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2246 // Parent constructor
2247 OO
.ui
.Dialog
.super.call( this, config
);
2249 // Mixin constructors
2250 OO
.ui
.PendingElement
.call( this );
2253 this.actions
= new OO
.ui
.ActionSet();
2254 this.attachedActions
= [];
2255 this.currentAction
= null;
2258 this.actions
.connect( this, {
2259 click
: 'onActionClick',
2260 resize
: 'onActionResize',
2261 change
: 'onActionsChange'
2266 .addClass( 'oo-ui-dialog' )
2267 .attr( 'role', 'dialog' );
2272 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2273 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2275 /* Static Properties */
2278 * Symbolic name of dialog.
2283 * @property {string}
2285 OO
.ui
.Dialog
.static.name
= '';
2293 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2295 OO
.ui
.Dialog
.static.title
= '';
2298 * List of OO.ui.ActionWidget configuration options.
2302 * @property {Object[]}
2304 OO
.ui
.Dialog
.static.actions
= [];
2307 * Close dialog when the escape key is pressed.
2312 * @property {boolean}
2314 OO
.ui
.Dialog
.static.escapable
= true;
2319 * Handle frame document key down events.
2321 * @param {jQuery.Event} e Key down event
2323 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2324 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2331 * Handle action resized events.
2333 * @param {OO.ui.ActionWidget} action Action that was resized
2335 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2336 // Override in subclass
2340 * Handle action click events.
2342 * @param {OO.ui.ActionWidget} action Action that was clicked
2344 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2345 if ( !this.isPending() ) {
2346 this.currentAction
= action
;
2347 this.executeAction( action
.getAction() );
2352 * Handle actions change event.
2354 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2355 this.detachActions();
2356 if ( !this.isClosing() ) {
2357 this.attachActions();
2362 * Get set of actions.
2364 * @return {OO.ui.ActionSet}
2366 OO
.ui
.Dialog
.prototype.getActions = function () {
2367 return this.actions
;
2371 * Get a process for taking action.
2373 * When you override this method, you can add additional accept steps to the process the parent
2374 * method provides using the 'first' and 'next' methods.
2377 * @param {string} [action] Symbolic name of action
2378 * @return {OO.ui.Process} Action process
2380 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2381 return new OO
.ui
.Process()
2382 .next( function () {
2384 // An empty action always closes the dialog without data, which should always be
2385 // safe and make no changes
2394 * @param {Object} [data] Dialog opening data
2395 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2396 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2397 * action item, omit to use #static-actions
2399 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2403 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2404 .next( function () {
2407 config
= this.constructor.static,
2408 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2410 this.title
.setLabel(
2411 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2413 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2415 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2418 this.actions
.add( items
);
2425 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2427 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2428 .first( function () {
2429 this.actions
.clear();
2430 this.currentAction
= null;
2437 OO
.ui
.Dialog
.prototype.initialize = function () {
2439 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2442 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2445 if ( this.constructor.static.escapable
) {
2446 this.$document
.on( 'keydown', this.onDocumentKeyDown
.bind( this ) );
2450 this.$content
.addClass( 'oo-ui-dialog-content' );
2451 this.setPendingElement( this.$head
);
2455 * Attach action actions.
2457 OO
.ui
.Dialog
.prototype.attachActions = function () {
2458 // Remember the list of potentially attached actions
2459 this.attachedActions
= this.actions
.get();
2463 * Detach action actions.
2467 OO
.ui
.Dialog
.prototype.detachActions = function () {
2470 // Detach all actions that may have been previously attached
2471 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2472 this.attachedActions
[i
].$element
.detach();
2474 this.attachedActions
= [];
2478 * Execute an action.
2480 * @param {string} action Symbolic name of action to execute
2481 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2483 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2485 return this.getActionProcess( action
).execute()
2486 .always( this.popPending
.bind( this ) );
2490 * Collection of windows.
2493 * @extends OO.ui.Element
2494 * @mixins OO.EventEmitter
2496 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2497 * already opening or opened, the current window will be closed without data. Empty closing data
2498 * should always result in the window being closed without causing constructive or destructive
2501 * As a window is opened and closed, it passes through several stages and the manager emits several
2502 * corresponding events.
2504 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2505 * - {@link #event-opening} is emitted with `opening` promise
2506 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2507 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2508 * - `setup` progress notification is emitted from opening promise
2509 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2510 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2511 * - `ready` progress notification is emitted from opening promise
2512 * - `opening` promise is resolved with `opened` promise
2513 * - Window is now open
2515 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2516 * - `opened` promise is resolved with `closing` promise
2517 * - {@link #event-closing} is emitted with `closing` promise
2518 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2519 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2520 * - `hold` progress notification is emitted from opening promise
2521 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2522 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2523 * - `teardown` progress notification is emitted from opening promise
2524 * - Closing promise is resolved
2525 * - Window is now closed
2528 * @param {Object} [config] Configuration options
2529 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2530 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2531 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2533 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2534 // Configuration initialization
2535 config
= config
|| {};
2537 // Parent constructor
2538 OO
.ui
.WindowManager
.super.call( this, config
);
2540 // Mixin constructors
2541 OO
.EventEmitter
.call( this );
2544 this.factory
= config
.factory
;
2545 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2546 this.isolate
= !!config
.isolate
;
2548 this.opening
= null;
2550 this.closing
= null;
2551 this.preparingToOpen
= null;
2552 this.preparingToClose
= null;
2554 this.currentWindow
= null;
2555 this.$ariaHidden
= null;
2556 this.requestedSize
= null;
2557 this.onWindowResizeTimeout
= null;
2558 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2559 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2560 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2561 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2565 .addClass( 'oo-ui-windowManager' )
2566 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2571 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2572 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2577 * Window is opening.
2579 * Fired when the window begins to be opened.
2582 * @param {OO.ui.Window} win Window that's being opened
2583 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2584 * resolved the first argument will be a promise which will be resolved when the window begins
2585 * closing, the second argument will be the opening data; progress notifications will be fired on
2586 * the promise for `setup` and `ready` when those processes are completed respectively.
2587 * @param {Object} data Window opening data
2591 * Window is closing.
2593 * Fired when the window begins to be closed.
2596 * @param {OO.ui.Window} win Window that's being closed
2597 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2598 * is resolved the first argument will be a the closing data; progress notifications will be fired
2599 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2600 * @param {Object} data Window closing data
2604 * Window was resized.
2607 * @param {OO.ui.Window} win Window that was resized
2610 /* Static Properties */
2613 * Map of symbolic size names and CSS properties.
2617 * @property {Object}
2619 OO
.ui
.WindowManager
.static.sizes
= {
2630 // These can be non-numeric because they are never used in calculations
2637 * Symbolic name of default size.
2639 * Default size is used if the window's requested size is not recognized.
2643 * @property {string}
2645 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2650 * Handle window resize events.
2652 * @param {jQuery.Event} e Window resize event
2654 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2655 clearTimeout( this.onWindowResizeTimeout
);
2656 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2660 * Handle window resize events.
2662 * @param {jQuery.Event} e Window resize event
2664 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2665 if ( this.currentWindow
) {
2666 this.updateWindowSize( this.currentWindow
);
2671 * Handle window mouse wheel events.
2673 * @param {jQuery.Event} e Mouse wheel event
2675 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function ( e
) {
2676 // Kill all events in the parent window if the child window is isolated,
2677 // or if the event didn't come from the child window
2678 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2682 * Handle document key down events.
2684 * @param {jQuery.Event} e Key down event
2686 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2687 switch ( e
.which
) {
2688 case OO
.ui
.Keys
.PAGEUP
:
2689 case OO
.ui
.Keys
.PAGEDOWN
:
2690 case OO
.ui
.Keys
.END
:
2691 case OO
.ui
.Keys
.HOME
:
2692 case OO
.ui
.Keys
.LEFT
:
2694 case OO
.ui
.Keys
.RIGHT
:
2695 case OO
.ui
.Keys
.DOWN
:
2696 // Kill all events in the parent window if the child window is isolated,
2697 // or if the event didn't come from the child window
2698 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2703 * Check if window is opening.
2705 * @return {boolean} Window is opening
2707 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2708 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2712 * Check if window is closing.
2714 * @return {boolean} Window is closing
2716 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2717 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2721 * Check if window is opened.
2723 * @return {boolean} Window is opened
2725 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2726 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2730 * Check if window contents should be isolated.
2732 * Window content isolation is done using inline frames.
2734 * @return {boolean} Window contents should be isolated
2736 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2737 return this.isolate
;
2741 * Check if a window is being managed.
2743 * @param {OO.ui.Window} win Window to check
2744 * @return {boolean} Window is being managed
2746 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2749 for ( name
in this.windows
) {
2750 if ( this.windows
[name
] === win
) {
2759 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2761 * @param {OO.ui.Window} win Window being opened
2762 * @param {Object} [data] Window opening data
2763 * @return {number} Milliseconds to wait
2765 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2770 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2772 * @param {OO.ui.Window} win Window being opened
2773 * @param {Object} [data] Window opening data
2774 * @return {number} Milliseconds to wait
2776 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2781 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2783 * @param {OO.ui.Window} win Window being closed
2784 * @param {Object} [data] Window closing data
2785 * @return {number} Milliseconds to wait
2787 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2792 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2794 * @param {OO.ui.Window} win Window being closed
2795 * @param {Object} [data] Window closing data
2796 * @return {number} Milliseconds to wait
2798 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2799 return this.modal
? 250 : 0;
2803 * Get managed window by symbolic name.
2805 * If window is not yet instantiated, it will be instantiated and added automatically.
2807 * @param {string} name Symbolic window name
2808 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2809 * @throws {Error} If the symbolic name is unrecognized by the factory
2810 * @throws {Error} If the symbolic name unrecognized as a managed window
2812 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2813 var deferred
= $.Deferred(),
2814 win
= this.windows
[name
];
2816 if ( !( win
instanceof OO
.ui
.Window
) ) {
2817 if ( this.factory
) {
2818 if ( !this.factory
.lookup( name
) ) {
2819 deferred
.reject( new OO
.ui
.Error(
2820 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2823 win
= this.factory
.create( name
, this, { $: this.$ } );
2824 this.addWindows( [ win
] );
2825 deferred
.resolve( win
);
2828 deferred
.reject( new OO
.ui
.Error(
2829 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2833 deferred
.resolve( win
);
2836 return deferred
.promise();
2840 * Get current window.
2842 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2844 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2845 return this.currentWindow
;
2851 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2852 * @param {Object} [data] Window opening data
2853 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2854 * for more details about the `opening` promise
2857 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2860 opening
= $.Deferred();
2862 // Argument handling
2863 if ( typeof win
=== 'string' ) {
2864 return this.getWindow( win
).then( function ( win
) {
2865 return manager
.openWindow( win
, data
);
2870 if ( !this.hasWindow( win
) ) {
2871 opening
.reject( new OO
.ui
.Error(
2872 'Cannot open window: window is not attached to manager'
2874 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2875 opening
.reject( new OO
.ui
.Error(
2876 'Cannot open window: another window is opening or open'
2881 if ( opening
.state() !== 'rejected' ) {
2882 if ( !win
.getManager() ) {
2883 win
.setManager( this );
2885 preparing
.push( win
.load() );
2887 if ( this.closing
) {
2888 // If a window is currently closing, wait for it to complete
2889 preparing
.push( this.closing
);
2892 this.preparingToOpen
= $.when
.apply( $, preparing
);
2893 // Ensure handlers get called after preparingToOpen is set
2894 this.preparingToOpen
.done( function () {
2895 if ( manager
.modal
) {
2896 manager
.toggleGlobalEvents( true );
2897 manager
.toggleAriaIsolation( true );
2899 manager
.currentWindow
= win
;
2900 manager
.opening
= opening
;
2901 manager
.preparingToOpen
= null;
2902 manager
.emit( 'opening', win
, opening
, data
);
2903 setTimeout( function () {
2904 win
.setup( data
).then( function () {
2905 manager
.updateWindowSize( win
);
2906 manager
.opening
.notify( { state
: 'setup' } );
2907 setTimeout( function () {
2908 win
.ready( data
).then( function () {
2909 manager
.opening
.notify( { state
: 'ready' } );
2910 manager
.opening
= null;
2911 manager
.opened
= $.Deferred();
2912 opening
.resolve( manager
.opened
.promise(), data
);
2914 }, manager
.getReadyDelay() );
2916 }, manager
.getSetupDelay() );
2920 return opening
.promise();
2926 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2927 * @param {Object} [data] Window closing data
2928 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2929 * for more details about the `closing` promise
2930 * @throws {Error} If no window by that name is being managed
2933 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2936 closing
= $.Deferred(),
2939 // Argument handling
2940 if ( typeof win
=== 'string' ) {
2941 win
= this.windows
[win
];
2942 } else if ( !this.hasWindow( win
) ) {
2948 closing
.reject( new OO
.ui
.Error(
2949 'Cannot close window: window is not attached to manager'
2951 } else if ( win
!== this.currentWindow
) {
2952 closing
.reject( new OO
.ui
.Error(
2953 'Cannot close window: window already closed with different data'
2955 } else if ( this.preparingToClose
|| this.closing
) {
2956 closing
.reject( new OO
.ui
.Error(
2957 'Cannot close window: window already closing with different data'
2962 if ( closing
.state() !== 'rejected' ) {
2963 if ( this.opening
) {
2964 // If the window is currently opening, close it when it's done
2965 preparing
.push( this.opening
);
2968 this.preparingToClose
= $.when
.apply( $, preparing
);
2969 // Ensure handlers get called after preparingToClose is set
2970 this.preparingToClose
.done( function () {
2971 manager
.closing
= closing
;
2972 manager
.preparingToClose
= null;
2973 manager
.emit( 'closing', win
, closing
, data
);
2974 opened
= manager
.opened
;
2975 manager
.opened
= null;
2976 opened
.resolve( closing
.promise(), data
);
2977 setTimeout( function () {
2978 win
.hold( data
).then( function () {
2979 closing
.notify( { state
: 'hold' } );
2980 setTimeout( function () {
2981 win
.teardown( data
).then( function () {
2982 closing
.notify( { state
: 'teardown' } );
2983 if ( manager
.modal
) {
2984 manager
.toggleGlobalEvents( false );
2985 manager
.toggleAriaIsolation( false );
2987 manager
.closing
= null;
2988 manager
.currentWindow
= null;
2989 closing
.resolve( data
);
2991 }, manager
.getTeardownDelay() );
2993 }, manager
.getHoldDelay() );
2997 return closing
.promise();
3003 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
3004 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
3005 * a statically configured symbolic name
3007 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
3008 var i
, len
, win
, name
, list
;
3010 if ( $.isArray( windows
) ) {
3011 // Convert to map of windows by looking up symbolic names from static configuration
3013 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
3014 name
= windows
[i
].constructor.static.name
;
3015 if ( typeof name
!== 'string' ) {
3016 throw new Error( 'Cannot add window' );
3018 list
[name
] = windows
[i
];
3020 } else if ( $.isPlainObject( windows
) ) {
3025 for ( name
in list
) {
3027 this.windows
[name
] = win
;
3028 this.$element
.append( win
.$element
);
3035 * Windows will be closed before they are removed.
3037 * @param {string} name Symbolic name of window to remove
3038 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3039 * @throws {Error} If windows being removed are not being managed
3041 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3042 var i
, len
, win
, name
,
3045 cleanup = function ( name
, win
) {
3046 delete manager
.windows
[name
];
3047 win
.$element
.detach();
3050 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3052 win
= this.windows
[name
];
3054 throw new Error( 'Cannot remove window' );
3056 promises
.push( this.closeWindow( name
).then( cleanup
.bind( null, name
, win
) ) );
3059 return $.when
.apply( $, promises
);
3063 * Remove all windows.
3065 * Windows will be closed before they are removed.
3067 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3069 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3070 return this.removeWindows( Object
.keys( this.windows
) );
3076 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3080 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3081 // Bypass for non-current, and thus invisible, windows
3082 if ( win
!== this.currentWindow
) {
3086 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
3087 sizes
= this.constructor.static.sizes
,
3088 size
= win
.getSize();
3090 if ( !sizes
[size
] ) {
3091 size
= this.constructor.static.defaultSize
;
3093 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
3097 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3098 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3099 win
.setDimensions( sizes
[size
] );
3101 this.emit( 'resize', win
);
3107 * Bind or unbind global events for scrolling.
3109 * @param {boolean} [on] Bind global events
3112 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3113 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3116 if ( !this.globalEvents
) {
3117 this.$( this.getElementDocument() ).on( {
3118 // Prevent scrolling by keys in top-level window
3119 keydown
: this.onDocumentKeyDownHandler
3121 this.$( this.getElementWindow() ).on( {
3122 // Prevent scrolling by wheel in top-level window
3123 mousewheel
: this.onWindowMouseWheelHandler
,
3124 // Start listening for top-level window dimension changes
3125 'orientationchange resize': this.onWindowResizeHandler
3127 this.globalEvents
= true;
3129 } else if ( this.globalEvents
) {
3130 // Unbind global events
3131 this.$( this.getElementDocument() ).off( {
3132 // Allow scrolling by keys in top-level window
3133 keydown
: this.onDocumentKeyDownHandler
3135 this.$( this.getElementWindow() ).off( {
3136 // Allow scrolling by wheel in top-level window
3137 mousewheel
: this.onWindowMouseWheelHandler
,
3138 // Stop listening for top-level window dimension changes
3139 'orientationchange resize': this.onWindowResizeHandler
3141 this.globalEvents
= false;
3148 * Toggle screen reader visibility of content other than the window manager.
3150 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3153 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3154 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3157 if ( !this.$ariaHidden
) {
3158 // Hide everything other than the window manager from screen readers
3159 this.$ariaHidden
= $( 'body' )
3161 .not( this.$element
.parentsUntil( 'body' ).last() )
3162 .attr( 'aria-hidden', '' );
3164 } else if ( this.$ariaHidden
) {
3165 // Restore screen reader visiblity
3166 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3167 this.$ariaHidden
= null;
3174 * Destroy window manager.
3176 * Windows will not be closed, only removed from the DOM.
3178 OO
.ui
.WindowManager
.prototype.destroy = function () {
3179 this.toggleGlobalEvents( false );
3180 this.toggleAriaIsolation( false );
3181 this.$element
.remove();
3189 * @param {string|jQuery} message Description of error
3190 * @param {Object} [config] Configuration options
3191 * @cfg {boolean} [recoverable=true] Error is recoverable
3192 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3194 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3195 // Configuration initialization
3196 config
= config
|| {};
3199 this.message
= message
instanceof jQuery
? message
: String( message
);
3200 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3201 this.warning
= !!config
.warning
;
3206 OO
.initClass( OO
.ui
.Error
);
3211 * Check if error can be recovered from.
3213 * @return {boolean} Error is recoverable
3215 OO
.ui
.Error
.prototype.isRecoverable = function () {
3216 return this.recoverable
;
3220 * Check if the error is a warning
3222 * @return {boolean} Error is warning
3224 OO
.ui
.Error
.prototype.isWarning = function () {
3225 return this.warning
;
3229 * Get error message as DOM nodes.
3231 * @return {jQuery} Error message in DOM nodes
3233 OO
.ui
.Error
.prototype.getMessage = function () {
3234 return this.message
instanceof jQuery
?
3235 this.message
.clone() :
3236 $( '<div>' ).text( this.message
).contents();
3240 * Get error message as text.
3242 * @return {string} Error message
3244 OO
.ui
.Error
.prototype.getMessageText = function () {
3245 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3249 * A list of functions, called in sequence.
3251 * If a function added to a process returns boolean false the process will stop; if it returns an
3252 * object with a `promise` method the process will use the promise to either continue to the next
3253 * step when the promise is resolved or stop when the promise is rejected.
3258 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3259 * call, see #createStep for more information
3260 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3262 * @return {Object} Step object, with `callback` and `context` properties
3264 OO
.ui
.Process = function ( step
, context
) {
3269 if ( step
!== undefined ) {
3270 this.next( step
, context
);
3276 OO
.initClass( OO
.ui
.Process
);
3281 * Start the process.
3283 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3284 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3285 * process, the remaining steps will not be taken
3287 OO
.ui
.Process
.prototype.execute = function () {
3288 var i
, len
, promise
;
3291 * Continue execution.
3294 * @param {Array} step A function and the context it should be called in
3295 * @return {Function} Function that continues the process
3297 function proceed( step
) {
3298 return function () {
3299 // Execute step in the correct context
3301 result
= step
.callback
.call( step
.context
);
3303 if ( result
=== false ) {
3304 // Use rejected promise for boolean false results
3305 return $.Deferred().reject( [] ).promise();
3307 if ( typeof result
=== 'number' ) {
3309 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3311 // Use a delayed promise for numbers, expecting them to be in milliseconds
3312 deferred
= $.Deferred();
3313 setTimeout( deferred
.resolve
, result
);
3314 return deferred
.promise();
3316 if ( result
instanceof OO
.ui
.Error
) {
3317 // Use rejected promise for error
3318 return $.Deferred().reject( [ result
] ).promise();
3320 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3321 // Use rejected promise for list of errors
3322 return $.Deferred().reject( result
).promise();
3324 // Duck-type the object to see if it can produce a promise
3325 if ( result
&& $.isFunction( result
.promise
) ) {
3326 // Use a promise generated from the result
3327 return result
.promise();
3329 // Use resolved promise for other results
3330 return $.Deferred().resolve().promise();
3334 if ( this.steps
.length
) {
3335 // Generate a chain reaction of promises
3336 promise
= proceed( this.steps
[0] )();
3337 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3338 promise
= promise
.then( proceed( this.steps
[i
] ) );
3341 promise
= $.Deferred().resolve().promise();
3348 * Create a process step.
3351 * @param {number|jQuery.Promise|Function} step
3353 * - Number of milliseconds to wait; or
3354 * - Promise to wait to be resolved; or
3355 * - Function to execute
3356 * - If it returns boolean false the process will stop
3357 * - If it returns an object with a `promise` method the process will use the promise to either
3358 * continue to the next step when the promise is resolved or stop when the promise is rejected
3359 * - If it returns a number, the process will wait for that number of milliseconds before
3361 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3363 * @return {Object} Step object, with `callback` and `context` properties
3365 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3366 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3368 callback: function () {
3374 if ( $.isFunction( step
) ) {
3380 throw new Error( 'Cannot create process step: number, promise or function expected' );
3384 * Add step to the beginning of the process.
3386 * @inheritdoc #createStep
3387 * @return {OO.ui.Process} this
3390 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3391 this.steps
.unshift( this.createStep( step
, context
) );
3396 * Add step to the end of the process.
3398 * @inheritdoc #createStep
3399 * @return {OO.ui.Process} this
3402 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3403 this.steps
.push( this.createStep( step
, context
) );
3408 * Factory for tools.
3411 * @extends OO.Factory
3414 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3415 // Parent constructor
3416 OO
.ui
.ToolFactory
.super.call( this );
3421 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3426 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3427 var i
, len
, included
, promoted
, demoted
,
3431 // Collect included and not excluded tools
3432 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3435 promoted
= this.extract( promote
, used
);
3436 demoted
= this.extract( demote
, used
);
3439 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3440 if ( !used
[included
[i
]] ) {
3441 auto
.push( included
[i
] );
3445 return promoted
.concat( auto
).concat( demoted
);
3449 * Get a flat list of names from a list of names or groups.
3451 * Tools can be specified in the following ways:
3453 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3454 * - All tools in a group: `{ group: 'group-name' }`
3455 * - All tools: `'*'`
3458 * @param {Array|string} collection List of tools
3459 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3460 * names will be added as properties
3461 * @return {string[]} List of extracted names
3463 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3464 var i
, len
, item
, name
, tool
,
3467 if ( collection
=== '*' ) {
3468 for ( name
in this.registry
) {
3469 tool
= this.registry
[name
];
3471 // Only add tools by group name when auto-add is enabled
3472 tool
.static.autoAddToCatchall
&&
3473 // Exclude already used tools
3474 ( !used
|| !used
[name
] )
3482 } else if ( $.isArray( collection
) ) {
3483 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3484 item
= collection
[i
];
3485 // Allow plain strings as shorthand for named tools
3486 if ( typeof item
=== 'string' ) {
3487 item
= { name
: item
};
3489 if ( OO
.isPlainObject( item
) ) {
3491 for ( name
in this.registry
) {
3492 tool
= this.registry
[name
];
3494 // Include tools with matching group
3495 tool
.static.group
=== item
.group
&&
3496 // Only add tools by group name when auto-add is enabled
3497 tool
.static.autoAddToGroup
&&
3498 // Exclude already used tools
3499 ( !used
|| !used
[name
] )
3507 // Include tools with matching name and exclude already used tools
3508 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3509 names
.push( item
.name
);
3511 used
[item
.name
] = true;
3521 * Factory for tool groups.
3524 * @extends OO.Factory
3527 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3528 // Parent constructor
3529 OO
.Factory
.call( this );
3532 defaultClasses
= this.constructor.static.getDefaultClasses();
3534 // Register default toolgroups
3535 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3536 this.register( defaultClasses
[i
] );
3542 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3544 /* Static Methods */
3547 * Get a default set of classes to be registered on construction
3549 * @return {Function[]} Default classes
3551 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3554 OO
.ui
.ListToolGroup
,
3566 * @param {Object} [config] Configuration options
3568 OO
.ui
.Theme
= function OoUiTheme( config
) {
3569 // Configuration initialization
3570 config
= config
|| {};
3575 OO
.initClass( OO
.ui
.Theme
);
3580 * Get a list of classes to be applied to a widget.
3582 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3583 * otherwise state transitions will not work properly.
3585 * @param {OO.ui.Element} element Element for which to get classes
3586 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3588 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3589 return { on
: [], off
: [] };
3593 * Update CSS classes provided by the theme.
3595 * For elements with theme logic hooks, this should be called anytime there's a state change.
3597 * @param {OO.ui.Element} element Element for which to update classes
3598 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3600 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3601 var classes
= this.getElementClasses( element
);
3604 .removeClass( classes
.off
.join( ' ' ) )
3605 .addClass( classes
.on
.join( ' ' ) );
3609 * Element with a button.
3611 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3612 * and access keys for accessibility purposes.
3618 * @param {Object} [config] Configuration options
3619 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3620 * @cfg {boolean} [framed=true] Render button with a frame
3621 * @cfg {number} [tabIndex=0] Button's tab index. Use 0 to use default ordering, use -1 to prevent
3623 * @cfg {string} [accessKey] Button's access key
3625 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3626 // Configuration initialization
3627 config
= config
|| {};
3630 this.$button
= null;
3632 this.tabIndex
= null;
3633 this.accessKey
= null;
3634 this.active
= false;
3635 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3636 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3639 this.$element
.addClass( 'oo-ui-buttonElement' );
3640 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3641 this.setTabIndex( config
.tabIndex
|| 0 );
3642 this.setAccessKey( config
.accessKey
);
3643 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3648 OO
.initClass( OO
.ui
.ButtonElement
);
3650 /* Static Properties */
3653 * Cancel mouse down events.
3657 * @property {boolean}
3659 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3664 * Set the button element.
3666 * If an element is already set, it will be cleaned up before setting up the new element.
3668 * @param {jQuery} $button Element to use as button
3670 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3671 if ( this.$button
) {
3673 .removeClass( 'oo-ui-buttonElement-button' )
3674 .removeAttr( 'role accesskey tabindex' )
3675 .off( this.onMouseDownHandler
);
3678 this.$button
= $button
3679 .addClass( 'oo-ui-buttonElement-button' )
3680 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3681 .on( 'mousedown', this.onMouseDownHandler
);
3685 * Handles mouse down events.
3687 * @param {jQuery.Event} e Mouse down event
3689 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3690 if ( this.isDisabled() || e
.which
!== 1 ) {
3693 // Remove the tab-index while the button is down to prevent the button from stealing focus
3694 this.$button
.removeAttr( 'tabindex' );
3695 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3696 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3697 // reliably reapply the tabindex and remove the pressed class
3698 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3699 // Prevent change of focus unless specifically configured otherwise
3700 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3706 * Handles mouse up events.
3708 * @param {jQuery.Event} e Mouse up event
3710 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3711 if ( this.isDisabled() || e
.which
!== 1 ) {
3714 // Restore the tab-index after the button is up to restore the button's accesssibility
3715 this.$button
.attr( 'tabindex', this.tabIndex
);
3716 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3717 // Stop listening for mouseup, since we only needed this once
3718 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3722 * Check if button has a frame.
3724 * @return {boolean} Button is framed
3726 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3733 * @param {boolean} [framed] Make button framed, omit to toggle
3736 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3737 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3738 if ( framed
!== this.framed
) {
3739 this.framed
= framed
;
3741 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3742 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3743 this.updateThemeClasses();
3752 * @param {number|null} tabIndex Button's tab index, use null to remove
3755 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3756 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3758 if ( this.tabIndex
!== tabIndex
) {
3759 if ( this.$button
) {
3760 if ( tabIndex
!== null ) {
3761 this.$button
.attr( 'tabindex', tabIndex
);
3763 this.$button
.removeAttr( 'tabindex' );
3766 this.tabIndex
= tabIndex
;
3775 * @param {string} accessKey Button's access key, use empty string to remove
3778 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3779 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3781 if ( this.accessKey
!== accessKey
) {
3782 if ( this.$button
) {
3783 if ( accessKey
!== null ) {
3784 this.$button
.attr( 'accesskey', accessKey
);
3786 this.$button
.removeAttr( 'accesskey' );
3789 this.accessKey
= accessKey
;
3798 * @param {boolean} [value] Make button active
3801 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3802 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3807 * Element containing a sequence of child elements.
3813 * @param {Object} [config] Configuration options
3814 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3816 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3817 // Configuration intialization
3818 config
= config
|| {};
3823 this.aggregateItemEvents
= {};
3826 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3832 * Set the group element.
3834 * If an element is already set, items will be moved to the new element.
3836 * @param {jQuery} $group Element to use as group
3838 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3841 this.$group
= $group
;
3842 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3843 this.$group
.append( this.items
[i
].$element
);
3848 * Check if there are no items.
3850 * @return {boolean} Group is empty
3852 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3853 return !this.items
.length
;
3859 * @return {OO.ui.Element[]} Items
3861 OO
.ui
.GroupElement
.prototype.getItems = function () {
3862 return this.items
.slice( 0 );
3866 * Add an aggregate item event.
3868 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3869 * and with an additional leading parameter containing the item that emitted the original event.
3870 * Other arguments that were emitted from the original event are passed through.
3872 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3873 * event, use null value to remove aggregation
3874 * @throws {Error} If aggregation already exists
3876 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3877 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3879 for ( itemEvent
in events
) {
3880 groupEvent
= events
[itemEvent
];
3882 // Remove existing aggregated event
3883 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3884 // Don't allow duplicate aggregations
3886 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3888 // Remove event aggregation from existing items
3889 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3890 item
= this.items
[i
];
3891 if ( item
.connect
&& item
.disconnect
) {
3893 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3894 item
.disconnect( this, remove
);
3897 // Prevent future items from aggregating event
3898 delete this.aggregateItemEvents
[itemEvent
];
3901 // Add new aggregate event
3903 // Make future items aggregate event
3904 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3905 // Add event aggregation to existing items
3906 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3907 item
= this.items
[i
];
3908 if ( item
.connect
&& item
.disconnect
) {
3910 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3911 item
.connect( this, add
);
3921 * Adding an existing item will move it.
3923 * @param {OO.ui.Element[]} items Items
3924 * @param {number} [index] Index to insert items at
3927 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3928 var i
, len
, item
, event
, events
, currentIndex
,
3931 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3934 // Check if item exists then remove it first, effectively "moving" it
3935 currentIndex
= $.inArray( item
, this.items
);
3936 if ( currentIndex
>= 0 ) {
3937 this.removeItems( [ item
] );
3938 // Adjust index to compensate for removal
3939 if ( currentIndex
< index
) {
3944 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3946 for ( event
in this.aggregateItemEvents
) {
3947 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3949 item
.connect( this, events
);
3951 item
.setElementGroup( this );
3952 itemElements
.push( item
.$element
.get( 0 ) );
3955 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3956 this.$group
.append( itemElements
);
3957 this.items
.push
.apply( this.items
, items
);
3958 } else if ( index
=== 0 ) {
3959 this.$group
.prepend( itemElements
);
3960 this.items
.unshift
.apply( this.items
, items
);
3962 this.items
[index
].$element
.before( itemElements
);
3963 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3972 * Items will be detached, not removed, so they can be used later.
3974 * @param {OO.ui.Element[]} items Items to remove
3977 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3978 var i
, len
, item
, index
, remove
, itemEvent
;
3980 // Remove specific items
3981 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3983 index
= $.inArray( item
, this.items
);
3984 if ( index
!== -1 ) {
3986 item
.connect
&& item
.disconnect
&&
3987 !$.isEmptyObject( this.aggregateItemEvents
)
3990 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3991 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3993 item
.disconnect( this, remove
);
3995 item
.setElementGroup( null );
3996 this.items
.splice( index
, 1 );
3997 item
.$element
.detach();
4007 * Items will be detached, not removed, so they can be used later.
4011 OO
.ui
.GroupElement
.prototype.clearItems = function () {
4012 var i
, len
, item
, remove
, itemEvent
;
4015 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4016 item
= this.items
[i
];
4018 item
.connect
&& item
.disconnect
&&
4019 !$.isEmptyObject( this.aggregateItemEvents
)
4022 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4023 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4025 item
.disconnect( this, remove
);
4027 item
.setElementGroup( null );
4028 item
.$element
.detach();
4036 * Element containing an icon.
4038 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4039 * a control or convey information in a more space efficient way. Icons should rarely be used
4040 * without labels; such as in a toolbar where space is at a premium or within a context where the
4041 * meaning is very clear to the user.
4047 * @param {Object} [config] Configuration options
4048 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4049 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4050 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4052 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4054 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4055 // Configuration intialization
4056 config
= config
|| {};
4061 this.iconTitle
= null;
4064 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4065 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4066 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4071 OO
.initClass( OO
.ui
.IconElement
);
4073 /* Static Properties */
4078 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4080 * For i18n purposes, this property can be an object containing a `default` icon name property and
4081 * additional icon names keyed by language code.
4083 * Example of i18n icon definition:
4084 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4088 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4089 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4092 OO
.ui
.IconElement
.static.icon
= null;
4099 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4102 OO
.ui
.IconElement
.static.iconTitle
= null;
4107 * Set the icon element.
4109 * If an element is already set, it will be cleaned up before setting up the new element.
4111 * @param {jQuery} $icon Element to use as icon
4113 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4116 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4117 .removeAttr( 'title' );
4121 .addClass( 'oo-ui-iconElement-icon' )
4122 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4123 if ( this.iconTitle
!== null ) {
4124 this.$icon
.attr( 'title', this.iconTitle
);
4131 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4132 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4133 * language, use null to remove icon
4136 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4137 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4138 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4140 if ( this.icon
!== icon
) {
4142 if ( this.icon
!== null ) {
4143 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4145 if ( icon
!== null ) {
4146 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4152 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4153 this.updateThemeClasses();
4161 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4165 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4166 iconTitle
= typeof iconTitle
=== 'function' ||
4167 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4168 OO
.ui
.resolveMsg( iconTitle
) : null;
4170 if ( this.iconTitle
!== iconTitle
) {
4171 this.iconTitle
= iconTitle
;
4173 if ( this.iconTitle
!== null ) {
4174 this.$icon
.attr( 'title', iconTitle
);
4176 this.$icon
.removeAttr( 'title' );
4187 * @return {string} Icon name
4189 OO
.ui
.IconElement
.prototype.getIcon = function () {
4194 * Element containing an indicator.
4196 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4197 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4198 * instead of performing an action directly, or an item in a list which has errors that need to be
4205 * @param {Object} [config] Configuration options
4206 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4208 * @cfg {string} [indicator] Symbolic indicator name
4209 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4211 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4212 // Configuration initialization
4213 config
= config
|| {};
4216 this.$indicator
= null;
4217 this.indicator
= null;
4218 this.indicatorTitle
= null;
4221 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4222 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4223 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4228 OO
.initClass( OO
.ui
.IndicatorElement
);
4230 /* Static Properties */
4237 * @property {string|null} Symbolic indicator name
4239 OO
.ui
.IndicatorElement
.static.indicator
= null;
4246 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4249 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4254 * Set the indicator element.
4256 * If an element is already set, it will be cleaned up before setting up the new element.
4258 * @param {jQuery} $indicator Element to use as indicator
4260 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4261 if ( this.$indicator
) {
4263 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4264 .removeAttr( 'title' );
4267 this.$indicator
= $indicator
4268 .addClass( 'oo-ui-indicatorElement-indicator' )
4269 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4270 if ( this.indicatorTitle
!== null ) {
4271 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4276 * Set indicator name.
4278 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4281 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4282 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4284 if ( this.indicator
!== indicator
) {
4285 if ( this.$indicator
) {
4286 if ( this.indicator
!== null ) {
4287 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4289 if ( indicator
!== null ) {
4290 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4293 this.indicator
= indicator
;
4296 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4297 this.updateThemeClasses();
4303 * Set indicator title.
4305 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4306 * null for no indicator title
4309 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4310 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4311 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4312 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4314 if ( this.indicatorTitle
!== indicatorTitle
) {
4315 this.indicatorTitle
= indicatorTitle
;
4316 if ( this.$indicator
) {
4317 if ( this.indicatorTitle
!== null ) {
4318 this.$indicator
.attr( 'title', indicatorTitle
);
4320 this.$indicator
.removeAttr( 'title' );
4329 * Get indicator name.
4331 * @return {string} Symbolic name of indicator
4333 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4334 return this.indicator
;
4338 * Get indicator title.
4340 * @return {string} Indicator title text
4342 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4343 return this.indicatorTitle
;
4347 * Element containing a label.
4353 * @param {Object} [config] Configuration options
4354 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4355 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4356 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4358 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4359 // Configuration initialization
4360 config
= config
|| {};
4365 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4368 this.setLabel( config
.label
|| this.constructor.static.label
);
4369 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4374 OO
.initClass( OO
.ui
.LabelElement
);
4376 /* Static Properties */
4383 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4386 OO
.ui
.LabelElement
.static.label
= null;
4391 * Set the label element.
4393 * If an element is already set, it will be cleaned up before setting up the new element.
4395 * @param {jQuery} $label Element to use as label
4397 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4398 if ( this.$label
) {
4399 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4402 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4403 this.setLabelContent( this.label
);
4409 * An empty string will result in the label being hidden. A string containing only whitespace will
4410 * be converted to a single ` `.
4412 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4413 * text; or null for no label
4416 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4417 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4418 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4420 if ( this.label
!== label
) {
4421 if ( this.$label
) {
4422 this.setLabelContent( label
);
4427 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4435 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
4436 * text; or null for no label
4438 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4447 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4448 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4449 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4456 * Set the content of the label.
4458 * Do not call this method until after the label element has been set by #setLabelElement.
4461 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4462 * text; or null for no label
4464 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4465 if ( typeof label
=== 'string' ) {
4466 if ( label
.match( /^\s*$/ ) ) {
4467 // Convert whitespace only string to a single non-breaking space
4468 this.$label
.html( ' ' );
4470 this.$label
.text( label
);
4472 } else if ( label
instanceof jQuery
) {
4473 this.$label
.empty().append( label
);
4475 this.$label
.empty();
4477 this.$label
.css( 'display', !label
? 'none' : '' );
4481 * Element containing an OO.ui.PopupWidget object.
4487 * @param {Object} [config] Configuration options
4488 * @cfg {Object} [popup] Configuration to pass to popup
4489 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4491 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4492 // Configuration initialization
4493 config
= config
|| {};
4496 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4497 { autoClose
: true },
4499 { $: this.$, $autoCloseIgnore
: this.$element
}
4508 * @return {OO.ui.PopupWidget} Popup widget
4510 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4515 * Element with named flags that can be added, removed, listed and checked.
4517 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4518 * the flag name. Flags are primarily useful for styling.
4524 * @param {Object} [config] Configuration options
4525 * @cfg {string|string[]} [flags] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4526 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4528 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4529 // Configuration initialization
4530 config
= config
|| {};
4534 this.$flagged
= null;
4537 this.setFlags( config
.flags
);
4538 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4545 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4546 * added/removed properties
4552 * Set the flagged element.
4554 * If an element is already set, it will be cleaned up before setting up the new element.
4556 * @param {jQuery} $flagged Element to add flags to
4558 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4559 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4560 return 'oo-ui-flaggedElement-' + flag
;
4563 if ( this.$flagged
) {
4564 this.$flagged
.removeClass( classNames
);
4567 this.$flagged
= $flagged
.addClass( classNames
);
4571 * Check if a flag is set.
4573 * @param {string} flag Name of flag
4574 * @return {boolean} Has flag
4576 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4577 return flag
in this.flags
;
4581 * Get the names of all flags set.
4583 * @return {string[]} Flag names
4585 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4586 return Object
.keys( this.flags
);
4595 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4596 var flag
, className
,
4599 classPrefix
= 'oo-ui-flaggedElement-';
4601 for ( flag
in this.flags
) {
4602 className
= classPrefix
+ flag
;
4603 changes
[flag
] = false;
4604 delete this.flags
[flag
];
4605 remove
.push( className
);
4608 if ( this.$flagged
) {
4609 this.$flagged
.removeClass( remove
.join( ' ' ) );
4612 this.updateThemeClasses();
4613 this.emit( 'flag', changes
);
4619 * Add one or more flags.
4621 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4622 * keyed by flag name containing boolean set/remove instructions.
4626 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4627 var i
, len
, flag
, className
,
4631 classPrefix
= 'oo-ui-flaggedElement-';
4633 if ( typeof flags
=== 'string' ) {
4634 className
= classPrefix
+ flags
;
4636 if ( !this.flags
[flags
] ) {
4637 this.flags
[flags
] = true;
4638 add
.push( className
);
4640 } else if ( $.isArray( flags
) ) {
4641 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4643 className
= classPrefix
+ flag
;
4645 if ( !this.flags
[flag
] ) {
4646 changes
[flag
] = true;
4647 this.flags
[flag
] = true;
4648 add
.push( className
);
4651 } else if ( OO
.isPlainObject( flags
) ) {
4652 for ( flag
in flags
) {
4653 className
= classPrefix
+ flag
;
4654 if ( flags
[flag
] ) {
4656 if ( !this.flags
[flag
] ) {
4657 changes
[flag
] = true;
4658 this.flags
[flag
] = true;
4659 add
.push( className
);
4663 if ( this.flags
[flag
] ) {
4664 changes
[flag
] = false;
4665 delete this.flags
[flag
];
4666 remove
.push( className
);
4672 if ( this.$flagged
) {
4674 .addClass( add
.join( ' ' ) )
4675 .removeClass( remove
.join( ' ' ) );
4678 this.updateThemeClasses();
4679 this.emit( 'flag', changes
);
4685 * Element with a title.
4687 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4688 * not visible on touch devices.
4694 * @param {Object} [config] Configuration options
4695 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4696 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
4697 * static property 'title' is used.
4699 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4700 // Configuration initialization
4701 config
= config
|| {};
4704 this.$titled
= null;
4708 this.setTitle( config
.title
|| this.constructor.static.title
);
4709 this.setTitledElement( config
.$titled
|| this.$element
);
4714 OO
.initClass( OO
.ui
.TitledElement
);
4716 /* Static Properties */
4723 * @property {string|Function} Title text or a function that returns text
4725 OO
.ui
.TitledElement
.static.title
= null;
4730 * Set the titled element.
4732 * If an element is already set, it will be cleaned up before setting up the new element.
4734 * @param {jQuery} $titled Element to set title on
4736 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4737 if ( this.$titled
) {
4738 this.$titled
.removeAttr( 'title' );
4741 this.$titled
= $titled
;
4743 this.$titled
.attr( 'title', this.title
);
4750 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4753 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4754 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4756 if ( this.title
!== title
) {
4757 if ( this.$titled
) {
4758 if ( title
!== null ) {
4759 this.$titled
.attr( 'title', title
);
4761 this.$titled
.removeAttr( 'title' );
4773 * @return {string} Title string
4775 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4780 * Element that can be automatically clipped to visible boundaries.
4782 * Whenever the element's natural height changes, you have to call
4783 * #clip to make sure it's still clipping correctly.
4789 * @param {Object} [config] Configuration options
4790 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4792 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4793 // Configuration initialization
4794 config
= config
|| {};
4797 this.$clippable
= null;
4798 this.clipping
= false;
4799 this.clippedHorizontally
= false;
4800 this.clippedVertically
= false;
4801 this.$clippableContainer
= null;
4802 this.$clippableScroller
= null;
4803 this.$clippableWindow
= null;
4804 this.idealWidth
= null;
4805 this.idealHeight
= null;
4806 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
4807 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
4810 this.setClippableElement( config
.$clippable
|| this.$element
);
4816 * Set clippable element.
4818 * If an element is already set, it will be cleaned up before setting up the new element.
4820 * @param {jQuery} $clippable Element to make clippable
4822 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4823 if ( this.$clippable
) {
4824 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4825 this.$clippable
.css( { width
: '', height
: '' } );
4826 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4827 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4830 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4837 * Do not turn clipping on until after the element is attached to the DOM and visible.
4839 * @param {boolean} [clipping] Enable clipping, omit to toggle
4842 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4843 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4845 if ( this.clipping
!== clipping
) {
4846 this.clipping
= clipping
;
4848 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4849 // If the clippable container is the body, we have to listen to scroll events and check
4850 // jQuery.scrollTop on the window because of browser inconsistencies
4851 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4852 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4853 this.$clippableContainer
;
4854 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4855 this.$clippableWindow
= this.$( this.getElementWindow() )
4856 .on( 'resize', this.onClippableWindowResizeHandler
);
4857 // Initial clip after visible
4860 this.$clippable
.css( { width
: '', height
: '' } );
4861 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4862 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4864 this.$clippableContainer
= null;
4865 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4866 this.$clippableScroller
= null;
4867 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4868 this.$clippableWindow
= null;
4876 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4878 * @return {boolean} Element will be clipped to the visible area
4880 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4881 return this.clipping
;
4885 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4887 * @return {boolean} Part of the element is being clipped
4889 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4890 return this.clippedHorizontally
|| this.clippedVertically
;
4894 * Check if the right of the element is being clipped by the nearest scrollable container.
4896 * @return {boolean} Part of the element is being clipped
4898 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4899 return this.clippedHorizontally
;
4903 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4905 * @return {boolean} Part of the element is being clipped
4907 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4908 return this.clippedVertically
;
4912 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
4914 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4915 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4917 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4918 this.idealWidth
= width
;
4919 this.idealHeight
= height
;
4921 if ( !this.clipping
) {
4922 // Update dimensions
4923 this.$clippable
.css( { width
: width
, height
: height
} );
4925 // While clipping, idealWidth and idealHeight are not considered
4929 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4930 * the element's natural height changes.
4932 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4933 * overlapped by, the visible area of the nearest scrollable container.
4937 OO
.ui
.ClippableElement
.prototype.clip = function () {
4938 if ( !this.clipping
) {
4939 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
4944 cOffset
= this.$clippable
.offset(),
4945 $container
= this.$clippableContainer
.is( 'body' ) ?
4946 this.$clippableWindow
: this.$clippableContainer
,
4947 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
4948 ccHeight
= $container
.innerHeight() - buffer
,
4949 ccWidth
= $container
.innerWidth() - buffer
,
4950 scrollTop
= this.$clippableScroller
.scrollTop(),
4951 scrollLeft
= this.$clippableScroller
.scrollLeft(),
4952 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
4953 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
4954 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
4955 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
4956 clipWidth
= desiredWidth
< naturalWidth
,
4957 clipHeight
= desiredHeight
< naturalHeight
;
4960 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
4962 this.$clippable
.css( 'width', this.idealWidth
|| '' );
4963 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4964 this.$clippable
.css( 'overflowX', '' );
4967 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
4969 this.$clippable
.css( 'height', this.idealHeight
|| '' );
4970 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4971 this.$clippable
.css( 'overflowY', '' );
4974 this.clippedHorizontally
= clipWidth
;
4975 this.clippedVertically
= clipHeight
;
4981 * Generic toolbar tool.
4985 * @extends OO.ui.Widget
4986 * @mixins OO.ui.IconElement
4987 * @mixins OO.ui.FlaggedElement
4990 * @param {OO.ui.ToolGroup} toolGroup
4991 * @param {Object} [config] Configuration options
4992 * @cfg {string|Function} [title] Title text or a function that returns text
4994 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4995 // Configuration initialization
4996 config
= config
|| {};
4998 // Parent constructor
4999 OO
.ui
.Tool
.super.call( this, config
);
5001 // Mixin constructors
5002 OO
.ui
.IconElement
.call( this, config
);
5003 OO
.ui
.FlaggedElement
.call( this, config
);
5006 this.toolGroup
= toolGroup
;
5007 this.toolbar
= this.toolGroup
.getToolbar();
5008 this.active
= false;
5009 this.$title
= this.$( '<span>' );
5010 this.$titleText
= this.$( '<span>' );
5011 this.$accel
= this.$( '<span>' );
5012 this.$link
= this.$( '<a>' );
5016 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
5019 this.$titleText
.addClass( 'oo-ui-tool-title-text' );
5020 this.$accel
.addClass( 'oo-ui-tool-accel' );
5022 .addClass( 'oo-ui-tool-title' )
5023 .append( this.$titleText
, this.$accel
);
5025 .addClass( 'oo-ui-tool-link' )
5026 .append( this.$icon
, this.$title
)
5027 .prop( 'tabIndex', 0 )
5028 .attr( 'role', 'button' );
5030 .data( 'oo-ui-tool', this )
5032 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5033 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5035 .append( this.$link
);
5036 this.setTitle( config
.title
|| this.constructor.static.title
);
5041 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5042 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5043 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5051 /* Static Properties */
5057 OO
.ui
.Tool
.static.tagName
= 'span';
5060 * Symbolic name of tool.
5065 * @property {string}
5067 OO
.ui
.Tool
.static.name
= '';
5075 * @property {string}
5077 OO
.ui
.Tool
.static.group
= '';
5082 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
5083 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
5084 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
5085 * appended to the title if the tool is part of a bar tool group.
5090 * @property {string|Function} Title text or a function that returns text
5092 OO
.ui
.Tool
.static.title
= '';
5095 * Tool can be automatically added to catch-all groups.
5099 * @property {boolean}
5101 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
5104 * Tool can be automatically added to named groups.
5107 * @property {boolean}
5110 OO
.ui
.Tool
.static.autoAddToGroup
= true;
5113 * Check if this tool is compatible with given data.
5117 * @param {Mixed} data Data to check
5118 * @return {boolean} Tool can be used with data
5120 OO
.ui
.Tool
.static.isCompatibleWith = function () {
5127 * Handle the toolbar state being updated.
5129 * This is an abstract method that must be overridden in a concrete subclass.
5133 OO
.ui
.Tool
.prototype.onUpdateState = function () {
5135 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
5140 * Handle the tool being selected.
5142 * This is an abstract method that must be overridden in a concrete subclass.
5146 OO
.ui
.Tool
.prototype.onSelect = function () {
5148 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
5153 * Check if the button is active.
5155 * @return {boolean} Button is active
5157 OO
.ui
.Tool
.prototype.isActive = function () {
5162 * Make the button appear active or inactive.
5164 * @param {boolean} state Make button appear active
5166 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
5167 this.active
= !!state
;
5168 if ( this.active
) {
5169 this.$element
.addClass( 'oo-ui-tool-active' );
5171 this.$element
.removeClass( 'oo-ui-tool-active' );
5176 * Get the tool title.
5178 * @param {string|Function} title Title text or a function that returns text
5181 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
5182 this.title
= OO
.ui
.resolveMsg( title
);
5188 * Get the tool title.
5190 * @return {string} Title text
5192 OO
.ui
.Tool
.prototype.getTitle = function () {
5197 * Get the tool's symbolic name.
5199 * @return {string} Symbolic name of tool
5201 OO
.ui
.Tool
.prototype.getName = function () {
5202 return this.constructor.static.name
;
5208 OO
.ui
.Tool
.prototype.updateTitle = function () {
5209 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
5210 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
5211 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
5214 this.$titleText
.text( this.title
);
5215 this.$accel
.text( accel
);
5217 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
5218 tooltipParts
.push( this.title
);
5220 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
5221 tooltipParts
.push( accel
);
5223 if ( tooltipParts
.length
) {
5224 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
5226 this.$link
.removeAttr( 'title' );
5233 OO
.ui
.Tool
.prototype.destroy = function () {
5234 this.toolbar
.disconnect( this );
5235 this.$element
.remove();
5239 * Collection of tool groups.
5242 * @extends OO.ui.Element
5243 * @mixins OO.EventEmitter
5244 * @mixins OO.ui.GroupElement
5247 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5248 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5249 * @param {Object} [config] Configuration options
5250 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5251 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5253 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5254 // Configuration initialization
5255 config
= config
|| {};
5257 // Parent constructor
5258 OO
.ui
.Toolbar
.super.call( this, config
);
5260 // Mixin constructors
5261 OO
.EventEmitter
.call( this );
5262 OO
.ui
.GroupElement
.call( this, config
);
5265 this.toolFactory
= toolFactory
;
5266 this.toolGroupFactory
= toolGroupFactory
;
5269 this.$bar
= this.$( '<div>' );
5270 this.$actions
= this.$( '<div>' );
5271 this.initialized
= false;
5275 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5276 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
5279 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5280 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
5281 if ( config
.actions
) {
5282 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
5283 this.$bar
.append( this.$actions
);
5285 this.$bar
.append( '<div style="clear:both"></div>' );
5286 if ( config
.shadow
) {
5287 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5289 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5294 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5295 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5296 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5301 * Get the tool factory.
5303 * @return {OO.ui.ToolFactory} Tool factory
5305 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5306 return this.toolFactory
;
5310 * Get the tool group factory.
5312 * @return {OO.Factory} Tool group factory
5314 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5315 return this.toolGroupFactory
;
5319 * Handles mouse down events.
5321 * @param {jQuery.Event} e Mouse down event
5323 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5324 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5325 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5326 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5332 * Sets up handles and preloads required information for the toolbar to work.
5333 * This must be called immediately after it is attached to a visible document.
5335 OO
.ui
.Toolbar
.prototype.initialize = function () {
5336 this.initialized
= true;
5342 * Tools can be specified in the following ways:
5344 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5345 * - All tools in a group: `{ group: 'group-name' }`
5346 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5348 * @param {Object.<string,Array>} groups List of tool group configurations
5349 * @param {Array|string} [groups.include] Tools to include
5350 * @param {Array|string} [groups.exclude] Tools to exclude
5351 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5352 * @param {Array|string} [groups.demote] Tools to demote to the end
5354 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5355 var i
, len
, type
, group
,
5357 defaultType
= 'bar';
5359 // Cleanup previous groups
5362 // Build out new groups
5363 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5365 if ( group
.include
=== '*' ) {
5366 // Apply defaults to catch-all groups
5367 if ( group
.type
=== undefined ) {
5368 group
.type
= 'list';
5370 if ( group
.label
=== undefined ) {
5371 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
5374 // Check type has been registered
5375 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5377 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5380 this.addItems( items
);
5384 * Remove all tools and groups from the toolbar.
5386 OO
.ui
.Toolbar
.prototype.reset = function () {
5391 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5392 this.items
[i
].destroy();
5398 * Destroys toolbar, removing event handlers and DOM elements.
5400 * Call this whenever you are done using a toolbar.
5402 OO
.ui
.Toolbar
.prototype.destroy = function () {
5404 this.$element
.remove();
5408 * Check if tool has not been used yet.
5410 * @param {string} name Symbolic name of tool
5411 * @return {boolean} Tool is available
5413 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5414 return !this.tools
[name
];
5418 * Prevent tool from being used again.
5420 * @param {OO.ui.Tool} tool Tool to reserve
5422 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5423 this.tools
[tool
.getName()] = tool
;
5427 * Allow tool to be used again.
5429 * @param {OO.ui.Tool} tool Tool to release
5431 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5432 delete this.tools
[tool
.getName()];
5436 * Get accelerator label for tool.
5438 * This is a stub that should be overridden to provide access to accelerator information.
5440 * @param {string} name Symbolic name of tool
5441 * @return {string|undefined} Tool accelerator label if available
5443 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5448 * Collection of tools.
5450 * Tools can be specified in the following ways:
5452 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5453 * - All tools in a group: `{ group: 'group-name' }`
5454 * - All tools: `'*'`
5458 * @extends OO.ui.Widget
5459 * @mixins OO.ui.GroupElement
5462 * @param {OO.ui.Toolbar} toolbar
5463 * @param {Object} [config] Configuration options
5464 * @cfg {Array|string} [include=[]] List of tools to include
5465 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5466 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5467 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5469 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5470 // Configuration initialization
5471 config
= config
|| {};
5473 // Parent constructor
5474 OO
.ui
.ToolGroup
.super.call( this, config
);
5476 // Mixin constructors
5477 OO
.ui
.GroupElement
.call( this, config
);
5480 this.toolbar
= toolbar
;
5482 this.pressed
= null;
5483 this.autoDisabled
= false;
5484 this.include
= config
.include
|| [];
5485 this.exclude
= config
.exclude
|| [];
5486 this.promote
= config
.promote
|| [];
5487 this.demote
= config
.demote
|| [];
5488 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
5492 'mousedown touchstart': this.onPointerDown
.bind( this ),
5493 'mouseup touchend': this.onPointerUp
.bind( this ),
5494 mouseover
: this.onMouseOver
.bind( this ),
5495 mouseout
: this.onMouseOut
.bind( this )
5497 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5498 this.aggregate( { disable
: 'itemDisable' } );
5499 this.connect( this, { itemDisable
: 'updateDisabled' } );
5502 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5504 .addClass( 'oo-ui-toolGroup' )
5505 .append( this.$group
);
5511 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5512 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5520 /* Static Properties */
5523 * Show labels in tooltips.
5527 * @property {boolean}
5529 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5532 * Show acceleration labels in tooltips.
5536 * @property {boolean}
5538 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5541 * Automatically disable the toolgroup when all tools are disabled
5545 * @property {boolean}
5547 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5554 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5555 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5561 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5562 var i
, item
, allDisabled
= true;
5564 if ( this.constructor.static.autoDisable
) {
5565 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5566 item
= this.items
[i
];
5567 if ( !item
.isDisabled() ) {
5568 allDisabled
= false;
5572 this.autoDisabled
= allDisabled
;
5574 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5578 * Handle mouse down events.
5580 * @param {jQuery.Event} e Mouse down event
5582 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5583 // e.which is 0 for touch events, 1 for left mouse button
5584 if ( !this.isDisabled() && e
.which
<= 1 ) {
5585 this.pressed
= this.getTargetTool( e
);
5586 if ( this.pressed
) {
5587 this.pressed
.setActive( true );
5588 this.getElementDocument().addEventListener(
5589 'mouseup', this.onCapturedMouseUpHandler
, true
5597 * Handle captured mouse up events.
5599 * @param {Event} e Mouse up event
5601 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5602 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5603 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5604 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5605 this.onPointerUp( e
);
5609 * Handle mouse up events.
5611 * @param {jQuery.Event} e Mouse up event
5613 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5614 var tool
= this.getTargetTool( e
);
5616 // e.which is 0 for touch events, 1 for left mouse button
5617 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5618 this.pressed
.onSelect();
5621 this.pressed
= null;
5626 * Handle mouse over events.
5628 * @param {jQuery.Event} e Mouse over event
5630 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5631 var tool
= this.getTargetTool( e
);
5633 if ( this.pressed
&& this.pressed
=== tool
) {
5634 this.pressed
.setActive( true );
5639 * Handle mouse out events.
5641 * @param {jQuery.Event} e Mouse out event
5643 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5644 var tool
= this.getTargetTool( e
);
5646 if ( this.pressed
&& this.pressed
=== tool
) {
5647 this.pressed
.setActive( false );
5652 * Get the closest tool to a jQuery.Event.
5654 * Only tool links are considered, which prevents other elements in the tool such as popups from
5655 * triggering tool group interactions.
5658 * @param {jQuery.Event} e
5659 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5661 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5663 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5665 if ( $item
.length
) {
5666 tool
= $item
.parent().data( 'oo-ui-tool' );
5669 return tool
&& !tool
.isDisabled() ? tool
: null;
5673 * Handle tool registry register events.
5675 * If a tool is registered after the group is created, we must repopulate the list to account for:
5677 * - a tool being added that may be included
5678 * - a tool already included being overridden
5680 * @param {string} name Symbolic name of tool
5682 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5687 * Get the toolbar this group is in.
5689 * @return {OO.ui.Toolbar} Toolbar of group
5691 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5692 return this.toolbar
;
5696 * Add and remove tools based on configuration.
5698 OO
.ui
.ToolGroup
.prototype.populate = function () {
5699 var i
, len
, name
, tool
,
5700 toolFactory
= this.toolbar
.getToolFactory(),
5704 list
= this.toolbar
.getToolFactory().getTools(
5705 this.include
, this.exclude
, this.promote
, this.demote
5708 // Build a list of needed tools
5709 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5713 toolFactory
.lookup( name
) &&
5714 // Tool is available or is already in this group
5715 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5717 tool
= this.tools
[name
];
5719 // Auto-initialize tools on first use
5720 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5723 this.toolbar
.reserveTool( tool
);
5728 // Remove tools that are no longer needed
5729 for ( name
in this.tools
) {
5730 if ( !names
[name
] ) {
5731 this.tools
[name
].destroy();
5732 this.toolbar
.releaseTool( this.tools
[name
] );
5733 remove
.push( this.tools
[name
] );
5734 delete this.tools
[name
];
5737 if ( remove
.length
) {
5738 this.removeItems( remove
);
5740 // Update emptiness state
5742 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5744 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5746 // Re-add tools (moving existing ones to new locations)
5747 this.addItems( add
);
5748 // Disabled state may depend on items
5749 this.updateDisabled();
5753 * Destroy tool group.
5755 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5759 this.toolbar
.getToolFactory().disconnect( this );
5760 for ( name
in this.tools
) {
5761 this.toolbar
.releaseTool( this.tools
[name
] );
5762 this.tools
[name
].disconnect( this ).destroy();
5763 delete this.tools
[name
];
5765 this.$element
.remove();
5769 * Dialog for showing a message.
5772 * - Registers two actions by default (safe and primary).
5773 * - Renders action widgets in the footer.
5776 * @extends OO.ui.Dialog
5779 * @param {Object} [config] Configuration options
5781 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5782 // Parent constructor
5783 OO
.ui
.MessageDialog
.super.call( this, config
);
5786 this.verticalActionLayout
= null;
5789 this.$element
.addClass( 'oo-ui-messageDialog' );
5794 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5796 /* Static Properties */
5798 OO
.ui
.MessageDialog
.static.name
= 'message';
5800 OO
.ui
.MessageDialog
.static.size
= 'small';
5802 OO
.ui
.MessageDialog
.static.verbose
= false;
5807 * A confirmation dialog's title should describe what the progressive action will do. An alert
5808 * dialog's title should describe what event occured.
5812 * @property {jQuery|string|Function|null}
5814 OO
.ui
.MessageDialog
.static.title
= null;
5817 * A confirmation dialog's message should describe the consequences of the progressive action. An
5818 * alert dialog's message should describe why the event occured.
5822 * @property {jQuery|string|Function|null}
5824 OO
.ui
.MessageDialog
.static.message
= null;
5826 OO
.ui
.MessageDialog
.static.actions
= [
5827 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5828 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5836 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5838 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5842 * Toggle action layout between vertical and horizontal.
5844 * @param {boolean} [value] Layout actions vertically, omit to toggle
5847 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5848 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5850 if ( value
!== this.verticalActionLayout
) {
5851 this.verticalActionLayout
= value
;
5853 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5854 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5863 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5865 return new OO
.ui
.Process( function () {
5866 this.close( { action
: action
} );
5869 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5875 * @param {Object} [data] Dialog opening data
5876 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5877 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5878 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5879 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5882 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5886 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5887 .next( function () {
5888 this.title
.setLabel(
5889 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5891 this.message
.setLabel(
5892 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5894 this.message
.$element
.toggleClass(
5895 'oo-ui-messageDialog-message-verbose',
5896 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5904 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5905 return Math
.round( this.text
.$element
.outerHeight( true ) );
5911 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5913 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5916 this.$actions
= this.$( '<div>' );
5917 this.container
= new OO
.ui
.PanelLayout( {
5918 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5920 this.text
= new OO
.ui
.PanelLayout( {
5921 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5923 this.message
= new OO
.ui
.LabelWidget( {
5924 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5928 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5929 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5930 this.container
.$element
.append( this.text
.$element
);
5931 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5932 this.$body
.append( this.container
.$element
);
5933 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5934 this.$foot
.append( this.$actions
);
5940 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5941 var i
, len
, other
, special
, others
;
5944 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5946 special
= this.actions
.getSpecial();
5947 others
= this.actions
.getOthers();
5948 if ( special
.safe
) {
5949 this.$actions
.append( special
.safe
.$element
);
5950 special
.safe
.toggleFramed( false );
5952 if ( others
.length
) {
5953 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5955 this.$actions
.append( other
.$element
);
5956 other
.toggleFramed( false );
5959 if ( special
.primary
) {
5960 this.$actions
.append( special
.primary
.$element
);
5961 special
.primary
.toggleFramed( false );
5965 if ( !this.isOpening() ) {
5966 this.manager
.updateWindowSize( this );
5968 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5972 * Fit action actions into columns or rows.
5974 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5976 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5978 actions
= this.actions
.get();
5981 this.toggleVerticalActionLayout( false );
5982 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5983 action
= actions
[i
];
5984 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5985 this.toggleVerticalActionLayout( true );
5992 * Navigation dialog window.
5995 * - Show and hide errors.
5996 * - Retry an action.
5999 * - Renders header with dialog title and one action widget on either side
6000 * (a 'safe' button on the left, and a 'primary' button on the right, both of
6001 * which close the dialog).
6002 * - Displays any action widgets in the footer (none by default).
6003 * - Ability to dismiss errors.
6005 * Subclass responsibilities:
6006 * - Register a 'safe' action.
6007 * - Register a 'primary' action.
6008 * - Add content to the dialog.
6012 * @extends OO.ui.Dialog
6015 * @param {Object} [config] Configuration options
6017 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
6018 // Parent constructor
6019 OO
.ui
.ProcessDialog
.super.call( this, config
);
6022 this.$element
.addClass( 'oo-ui-processDialog' );
6027 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
6032 * Handle dismiss button click events.
6036 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
6041 * Handle retry button click events.
6043 * Hides errors and then tries again.
6045 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
6047 this.executeAction( this.currentAction
.getAction() );
6053 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
6054 if ( this.actions
.isSpecial( action
) ) {
6057 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
6063 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
6065 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
6068 this.$navigation
= this.$( '<div>' );
6069 this.$location
= this.$( '<div>' );
6070 this.$safeActions
= this.$( '<div>' );
6071 this.$primaryActions
= this.$( '<div>' );
6072 this.$otherActions
= this.$( '<div>' );
6073 this.dismissButton
= new OO
.ui
.ButtonWidget( {
6075 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
6077 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
6078 this.$errors
= this.$( '<div>' );
6079 this.$errorsTitle
= this.$( '<div>' );
6082 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
6083 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
6086 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
6088 .append( this.title
.$element
)
6089 .addClass( 'oo-ui-processDialog-location' );
6090 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
6091 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
6092 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
6094 .addClass( 'oo-ui-processDialog-errors-title' )
6095 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
6097 .addClass( 'oo-ui-processDialog-errors' )
6098 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
6100 .addClass( 'oo-ui-processDialog-content' )
6101 .append( this.$errors
);
6103 .addClass( 'oo-ui-processDialog-navigation' )
6104 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
6105 this.$head
.append( this.$navigation
);
6106 this.$foot
.append( this.$otherActions
);
6112 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
6113 var i
, len
, other
, special
, others
;
6116 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
6118 special
= this.actions
.getSpecial();
6119 others
= this.actions
.getOthers();
6120 if ( special
.primary
) {
6121 this.$primaryActions
.append( special
.primary
.$element
);
6122 special
.primary
.toggleFramed( true );
6124 if ( others
.length
) {
6125 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6127 this.$otherActions
.append( other
.$element
);
6128 other
.toggleFramed( true );
6131 if ( special
.safe
) {
6132 this.$safeActions
.append( special
.safe
.$element
);
6133 special
.safe
.toggleFramed( true );
6137 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6143 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
6144 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
6145 .fail( this.showErrors
.bind( this ) );
6149 * Fit label between actions.
6153 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
6154 var width
= Math
.max(
6155 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
6156 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
6158 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
6164 * Handle errors that occured durring accept or reject processes.
6166 * @param {OO.ui.Error[]} errors Errors to be handled
6168 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
6174 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
6175 if ( !errors
[i
].isRecoverable() ) {
6176 recoverable
= false;
6178 if ( errors
[i
].isWarning() ) {
6181 $item
= this.$( '<div>' )
6182 .addClass( 'oo-ui-processDialog-error' )
6183 .append( errors
[i
].getMessage() );
6184 items
.push( $item
[0] );
6186 this.$errorItems
= this.$( items
);
6187 if ( recoverable
) {
6188 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
6190 this.currentAction
.setDisabled( true );
6193 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
6195 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
6197 this.retryButton
.toggle( recoverable
);
6198 this.$errorsTitle
.after( this.$errorItems
);
6199 this.$errors
.show().scrollTop( 0 );
6205 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
6206 this.$errors
.hide();
6207 this.$errorItems
.remove();
6208 this.$errorItems
= null;
6212 * Layout containing a series of pages.
6215 * @extends OO.ui.Layout
6218 * @param {Object} [config] Configuration options
6219 * @cfg {boolean} [continuous=false] Show all pages, one after another
6220 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
6221 * @cfg {boolean} [outlined=false] Show an outline
6222 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
6224 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
6225 // Configuration initialization
6226 config
= config
|| {};
6228 // Parent constructor
6229 OO
.ui
.BookletLayout
.super.call( this, config
);
6232 this.currentPageName
= null;
6234 this.ignoreFocus
= false;
6235 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
6236 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
6237 this.outlineVisible
= false;
6238 this.outlined
= !!config
.outlined
;
6239 if ( this.outlined
) {
6240 this.editable
= !!config
.editable
;
6241 this.outlineControlsWidget
= null;
6242 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
6243 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
6244 this.gridLayout
= new OO
.ui
.GridLayout(
6245 [ this.outlinePanel
, this.stackLayout
],
6246 { $: this.$, widths
: [ 1, 2 ] }
6248 this.outlineVisible
= true;
6249 if ( this.editable
) {
6250 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6251 this.outlineWidget
, { $: this.$ }
6257 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6258 if ( this.outlined
) {
6259 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
6261 if ( this.autoFocus
) {
6262 // Event 'focus' does not bubble, but 'focusin' does
6263 this.stackLayout
.onDOMEvent( 'focusin', this.onStackLayoutFocus
.bind( this ) );
6267 this.$element
.addClass( 'oo-ui-bookletLayout' );
6268 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6269 if ( this.outlined
) {
6270 this.outlinePanel
.$element
6271 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6272 .append( this.outlineWidget
.$element
);
6273 if ( this.editable
) {
6274 this.outlinePanel
.$element
6275 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6276 .append( this.outlineControlsWidget
.$element
);
6278 this.$element
.append( this.gridLayout
.$element
);
6280 this.$element
.append( this.stackLayout
.$element
);
6286 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6292 * @param {OO.ui.PageLayout} page Current page
6297 * @param {OO.ui.PageLayout[]} page Added pages
6298 * @param {number} index Index pages were added at
6303 * @param {OO.ui.PageLayout[]} pages Removed pages
6309 * Handle stack layout focus.
6311 * @param {jQuery.Event} e Focusin event
6313 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6316 // Find the page that an element was focused within
6317 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6318 for ( name
in this.pages
) {
6319 // Check for page match, exclude current page to find only page changes
6320 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6321 this.setPage( name
);
6328 * Handle stack layout set events.
6330 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6332 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6333 var $input
, layout
= this;
6335 page
.scrollElementIntoView( { complete: function () {
6336 if ( layout
.autoFocus
) {
6337 // Set focus to the first input if nothing on the page is focused yet
6338 if ( !page
.$element
.find( ':focus' ).length
) {
6339 $input
= page
.$element
.find( ':input:first' );
6340 if ( $input
.length
) {
6350 * Handle outline widget select events.
6352 * @param {OO.ui.OptionWidget|null} item Selected item
6354 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
6356 this.setPage( item
.getData() );
6361 * Check if booklet has an outline.
6365 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6366 return this.outlined
;
6370 * Check if booklet has editing controls.
6374 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6375 return this.editable
;
6379 * Check if booklet has a visible outline.
6383 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6384 return this.outlined
&& this.outlineVisible
;
6388 * Hide or show the outline.
6390 * @param {boolean} [show] Show outline, omit to invert current state
6393 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6394 if ( this.outlined
) {
6395 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6396 this.outlineVisible
= show
;
6397 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6404 * Get the outline widget.
6406 * @param {OO.ui.PageLayout} page Page to be selected
6407 * @return {OO.ui.PageLayout|null} Closest page to another
6409 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6410 var next
, prev
, level
,
6411 pages
= this.stackLayout
.getItems(),
6412 index
= $.inArray( page
, pages
);
6414 if ( index
!== -1 ) {
6415 next
= pages
[index
+ 1];
6416 prev
= pages
[index
- 1];
6417 // Prefer adjacent pages at the same level
6418 if ( this.outlined
) {
6419 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
6422 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
6428 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
6434 return prev
|| next
|| null;
6438 * Get the outline widget.
6440 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
6442 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6443 return this.outlineWidget
;
6447 * Get the outline controls widget. If the outline is not editable, null is returned.
6449 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6451 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6452 return this.outlineControlsWidget
;
6456 * Get a page by name.
6458 * @param {string} name Symbolic name of page
6459 * @return {OO.ui.PageLayout|undefined} Page, if found
6461 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6462 return this.pages
[name
];
6466 * Get the current page name.
6468 * @return {string|null} Current page name
6470 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6471 return this.currentPageName
;
6475 * Add a page to the layout.
6477 * When pages are added with the same names as existing pages, the existing pages will be
6478 * automatically removed before the new pages are added.
6480 * @param {OO.ui.PageLayout[]} pages Pages to add
6481 * @param {number} index Index to insert pages after
6485 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6486 var i
, len
, name
, page
, item
, currentIndex
,
6487 stackLayoutPages
= this.stackLayout
.getItems(),
6491 // Remove pages with same names
6492 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6494 name
= page
.getName();
6496 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6497 // Correct the insertion index
6498 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6499 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6502 remove
.push( this.pages
[name
] );
6505 if ( remove
.length
) {
6506 this.removePages( remove
);
6510 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6512 name
= page
.getName();
6513 this.pages
[page
.getName()] = page
;
6514 if ( this.outlined
) {
6515 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
6516 page
.setOutlineItem( item
);
6521 if ( this.outlined
&& items
.length
) {
6522 this.outlineWidget
.addItems( items
, index
);
6523 this.updateOutlineWidget();
6525 this.stackLayout
.addItems( pages
, index
);
6526 this.emit( 'add', pages
, index
);
6532 * Remove a page from the layout.
6537 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6538 var i
, len
, name
, page
,
6541 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6543 name
= page
.getName();
6544 delete this.pages
[name
];
6545 if ( this.outlined
) {
6546 items
.push( this.outlineWidget
.getItemFromData( name
) );
6547 page
.setOutlineItem( null );
6550 if ( this.outlined
&& items
.length
) {
6551 this.outlineWidget
.removeItems( items
);
6552 this.updateOutlineWidget();
6554 this.stackLayout
.removeItems( pages
);
6555 this.emit( 'remove', pages
);
6561 * Clear all pages from the layout.
6566 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6568 pages
= this.stackLayout
.getItems();
6571 this.currentPageName
= null;
6572 if ( this.outlined
) {
6573 this.outlineWidget
.clearItems();
6574 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6575 pages
[i
].setOutlineItem( null );
6578 this.stackLayout
.clearItems();
6580 this.emit( 'remove', pages
);
6586 * Set the current page by name.
6589 * @param {string} name Symbolic name of page
6591 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6594 page
= this.pages
[name
];
6596 if ( name
!== this.currentPageName
) {
6597 if ( this.outlined
) {
6598 selectedItem
= this.outlineWidget
.getSelectedItem();
6599 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6600 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
6604 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6605 this.pages
[this.currentPageName
].setActive( false );
6606 // Blur anything focused if the next page doesn't have anything focusable - this
6607 // is not needed if the next page has something focusable because once it is focused
6608 // this blur happens automatically
6609 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6610 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6611 if ( $focused
.length
) {
6616 this.currentPageName
= name
;
6617 this.stackLayout
.setItem( page
);
6618 page
.setActive( true );
6619 this.emit( 'set', page
);
6625 * Call this after adding or removing items from the OutlineWidget.
6629 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
6630 // Auto-select first item when nothing is selected anymore
6631 if ( !this.outlineWidget
.getSelectedItem() ) {
6632 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
6639 * Layout made of a field and optional label.
6641 * Available label alignment modes include:
6642 * - left: Label is before the field and aligned away from it, best for when the user will be
6643 * scanning for a specific label in a form with many fields
6644 * - right: Label is before the field and aligned toward it, best for forms the user is very
6645 * familiar with and will tab through field checking quickly to verify which field they are in
6646 * - top: Label is before the field and above it, best for when the user will need to fill out all
6647 * fields from top to bottom in a form with few fields
6648 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6649 * checkboxes or radio buttons
6652 * @extends OO.ui.Layout
6653 * @mixins OO.ui.LabelElement
6656 * @param {OO.ui.Widget} fieldWidget Field widget
6657 * @param {Object} [config] Configuration options
6658 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6659 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6661 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
6662 // Configuration initialization
6663 config
= $.extend( { align
: 'left' }, config
);
6665 // Parent constructor
6666 OO
.ui
.FieldLayout
.super.call( this, config
);
6668 // Mixin constructors
6669 OO
.ui
.LabelElement
.call( this, config
);
6672 this.$field
= this.$( '<div>' );
6673 this.fieldWidget
= fieldWidget
;
6675 if ( config
.help
) {
6676 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6678 classes
: [ 'oo-ui-fieldLayout-help' ],
6683 this.popupButtonWidget
.getPopup().$body
.append(
6685 .text( config
.help
)
6686 .addClass( 'oo-ui-fieldLayout-help-content' )
6688 this.$help
= this.popupButtonWidget
.$element
;
6690 this.$help
= this.$( [] );
6694 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
6695 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
6697 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
6700 this.$element
.addClass( 'oo-ui-fieldLayout' );
6702 .addClass( 'oo-ui-fieldLayout-field' )
6703 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
6704 .append( this.fieldWidget
.$element
);
6705 this.setAlignment( config
.align
);
6710 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6711 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6713 /* Static Properties */
6715 OO
.ui
.FieldLayout
.static.tagName
= 'label';
6720 * Handle field disable events.
6722 * @param {boolean} value Field is disabled
6724 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6725 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6729 * Handle label mouse click events.
6731 * @param {jQuery.Event} e Mouse click event
6733 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6734 this.fieldWidget
.simulateLabelClick();
6741 * @return {OO.ui.Widget} Field widget
6743 OO
.ui
.FieldLayout
.prototype.getField = function () {
6744 return this.fieldWidget
;
6748 * Set the field alignment mode.
6751 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6754 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6755 if ( value
!== this.align
) {
6756 // Default to 'left'
6757 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6761 if ( value
=== 'inline' ) {
6762 this.$element
.append( this.$field
, this.$label
, this.$help
);
6764 this.$element
.append( this.$help
, this.$label
, this.$field
);
6766 // Set classes. The following classes can be used here:
6767 // * oo-ui-fieldLayout-align-left
6768 // * oo-ui-fieldLayout-align-right
6769 // * oo-ui-fieldLayout-align-top
6770 // * oo-ui-fieldLayout-align-inline
6772 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6774 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
6782 * Layout made of a fieldset and optional legend.
6784 * Just add OO.ui.FieldLayout items.
6787 * @extends OO.ui.Layout
6788 * @mixins OO.ui.IconElement
6789 * @mixins OO.ui.LabelElement
6790 * @mixins OO.ui.GroupElement
6793 * @param {Object} [config] Configuration options
6794 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6796 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6797 // Configuration initialization
6798 config
= config
|| {};
6800 // Parent constructor
6801 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6803 // Mixin constructors
6804 OO
.ui
.IconElement
.call( this, config
);
6805 OO
.ui
.LabelElement
.call( this, config
);
6806 OO
.ui
.GroupElement
.call( this, config
);
6810 .addClass( 'oo-ui-fieldsetLayout' )
6811 .prepend( this.$icon
, this.$label
, this.$group
);
6812 if ( $.isArray( config
.items
) ) {
6813 this.addItems( config
.items
);
6819 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6820 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6821 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6822 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6825 * Layout with an HTML form.
6828 * @extends OO.ui.Layout
6831 * @param {Object} [config] Configuration options
6832 * @cfg {string} [method] HTML form `method` attribute
6833 * @cfg {string} [action] HTML form `action` attribute
6834 * @cfg {string} [enctype] HTML form `enctype` attribute
6836 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6837 // Configuration initialization
6838 config
= config
|| {};
6840 // Parent constructor
6841 OO
.ui
.FormLayout
.super.call( this, config
);
6844 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
6848 .addClass( 'oo-ui-formLayout' )
6850 method
: config
.method
,
6851 action
: config
.action
,
6852 enctype
: config
.enctype
6858 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6866 /* Static Properties */
6868 OO
.ui
.FormLayout
.static.tagName
= 'form';
6873 * Handle form submit events.
6875 * @param {jQuery.Event} e Submit event
6878 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6879 this.emit( 'submit' );
6884 * Layout made of proportionally sized columns and rows.
6887 * @extends OO.ui.Layout
6890 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6891 * @param {Object} [config] Configuration options
6892 * @cfg {number[]} [widths] Widths of columns as ratios
6893 * @cfg {number[]} [heights] Heights of rows as ratios
6895 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6898 // Configuration initialization
6899 config
= config
|| {};
6901 // Parent constructor
6902 OO
.ui
.GridLayout
.super.call( this, config
);
6910 this.$element
.addClass( 'oo-ui-gridLayout' );
6911 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6912 this.panels
.push( panels
[i
] );
6913 this.$element
.append( panels
[i
].$element
);
6915 if ( config
.widths
|| config
.heights
) {
6916 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6918 // Arrange in columns by default
6919 widths
= this.panels
.map( function () { return 1; } );
6920 this.layout( widths
, [ 1 ] );
6926 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6941 * Set grid dimensions.
6943 * @param {number[]} widths Widths of columns as ratios
6944 * @param {number[]} heights Heights of rows as ratios
6946 * @throws {Error} If grid is not large enough to fit all panels
6948 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6952 cols
= widths
.length
,
6953 rows
= heights
.length
;
6955 // Verify grid is big enough to fit panels
6956 if ( cols
* rows
< this.panels
.length
) {
6957 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6960 // Sum up denominators
6961 for ( x
= 0; x
< cols
; x
++ ) {
6964 for ( y
= 0; y
< rows
; y
++ ) {
6970 for ( x
= 0; x
< cols
; x
++ ) {
6971 this.widths
[x
] = widths
[x
] / xd
;
6973 for ( y
= 0; y
< rows
; y
++ ) {
6974 this.heights
[y
] = heights
[y
] / yd
;
6978 this.emit( 'layout' );
6982 * Update panel positions and sizes.
6986 OO
.ui
.GridLayout
.prototype.update = function () {
6987 var x
, y
, panel
, width
, height
, dimensions
,
6991 cols
= this.widths
.length
,
6992 rows
= this.heights
.length
;
6994 for ( y
= 0; y
< rows
; y
++ ) {
6995 height
= this.heights
[y
];
6996 for ( x
= 0; x
< cols
; x
++ ) {
6997 width
= this.widths
[x
];
6998 panel
= this.panels
[i
];
7000 width
: Math
.round( width
* 100 ) + '%',
7001 height
: Math
.round( height
* 100 ) + '%',
7002 top
: Math
.round( top
* 100 ) + '%'
7005 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
7006 dimensions
.right
= Math
.round( left
* 100 ) + '%';
7008 dimensions
.left
= Math
.round( left
* 100 ) + '%';
7010 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
7011 if ( width
=== 0 || height
=== 0 ) {
7012 dimensions
.visibility
= 'hidden';
7014 dimensions
.visibility
= '';
7016 panel
.$element
.css( dimensions
);
7024 this.emit( 'update' );
7028 * Get a panel at a given position.
7030 * The x and y position is affected by the current grid layout.
7032 * @param {number} x Horizontal position
7033 * @param {number} y Vertical position
7034 * @return {OO.ui.PanelLayout} The panel at the given postion
7036 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
7037 return this.panels
[ ( x
* this.widths
.length
) + y
];
7041 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
7044 * @extends OO.ui.Layout
7047 * @param {Object} [config] Configuration options
7048 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
7049 * @cfg {boolean} [padded=false] Pad the content from the edges
7050 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
7052 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
7053 // Configuration initialization
7054 config
= $.extend( {
7060 // Parent constructor
7061 OO
.ui
.PanelLayout
.super.call( this, config
);
7064 this.$element
.addClass( 'oo-ui-panelLayout' );
7065 if ( config
.scrollable
) {
7066 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
7068 if ( config
.padded
) {
7069 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
7071 if ( config
.expanded
) {
7072 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
7078 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
7081 * Page within an booklet layout.
7084 * @extends OO.ui.PanelLayout
7087 * @param {string} name Unique symbolic name of page
7088 * @param {Object} [config] Configuration options
7089 * @param {string} [outlineItem] Outline item widget
7091 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
7092 // Configuration initialization
7093 config
= $.extend( { scrollable
: true }, config
);
7095 // Parent constructor
7096 OO
.ui
.PageLayout
.super.call( this, config
);
7100 this.outlineItem
= config
.outlineItem
|| null;
7101 this.active
= false;
7104 this.$element
.addClass( 'oo-ui-pageLayout' );
7109 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
7115 * @param {boolean} active Page is active
7123 * @return {string} Symbolic name of page
7125 OO
.ui
.PageLayout
.prototype.getName = function () {
7130 * Check if page is active.
7132 * @return {boolean} Page is active
7134 OO
.ui
.PageLayout
.prototype.isActive = function () {
7141 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
7143 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
7144 return this.outlineItem
;
7150 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
7151 * outline item as desired; this method is called for setting (with an object) and unsetting
7152 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
7153 * operating on null instead of an OO.ui.OutlineItemWidget object.
7155 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
7158 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
7159 this.outlineItem
= outlineItem
|| null;
7160 if ( outlineItem
) {
7161 this.setupOutlineItem();
7167 * Setup outline item.
7169 * @localdoc Subclasses should override this method to adjust the outline item as desired.
7171 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
7174 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
7179 * Set page active state.
7181 * @param {boolean} Page is active
7184 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
7187 if ( active
!== this.active
) {
7188 this.active
= active
;
7189 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
7190 this.emit( 'active', this.active
);
7195 * Layout containing a series of mutually exclusive pages.
7198 * @extends OO.ui.PanelLayout
7199 * @mixins OO.ui.GroupElement
7202 * @param {Object} [config] Configuration options
7203 * @cfg {boolean} [continuous=false] Show all pages, one after another
7204 * @cfg {string} [icon=''] Symbolic icon name
7205 * @cfg {OO.ui.Layout[]} [items] Layouts to add
7207 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
7208 // Configuration initialization
7209 config
= $.extend( { scrollable
: true }, config
);
7211 // Parent constructor
7212 OO
.ui
.StackLayout
.super.call( this, config
);
7214 // Mixin constructors
7215 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7218 this.currentItem
= null;
7219 this.continuous
= !!config
.continuous
;
7222 this.$element
.addClass( 'oo-ui-stackLayout' );
7223 if ( this.continuous
) {
7224 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
7226 if ( $.isArray( config
.items
) ) {
7227 this.addItems( config
.items
);
7233 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
7234 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
7240 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
7246 * Get the current item.
7248 * @return {OO.ui.Layout|null}
7250 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
7251 return this.currentItem
;
7255 * Unset the current item.
7258 * @param {OO.ui.StackLayout} layout
7261 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7262 var prevItem
= this.currentItem
;
7263 if ( prevItem
=== null ) {
7267 this.currentItem
= null;
7268 this.emit( 'set', null );
7274 * Adding an existing item (by value) will move it.
7276 * @param {OO.ui.Layout[]} items Items to add
7277 * @param {number} [index] Index to insert items after
7280 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7282 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7284 if ( !this.currentItem
&& items
.length
) {
7285 this.setItem( items
[0] );
7294 * Items will be detached, not removed, so they can be used later.
7296 * @param {OO.ui.Layout[]} items Items to remove
7300 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7302 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7304 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7305 if ( this.items
.length
) {
7306 this.setItem( this.items
[0] );
7308 this.unsetCurrentItem();
7318 * Items will be detached, not removed, so they can be used later.
7323 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7324 this.unsetCurrentItem();
7325 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7333 * Any currently shown item will be hidden.
7335 * FIXME: If the passed item to show has not been added in the items list, then
7336 * this method drops it and unsets the current item.
7338 * @param {OO.ui.Layout} item Item to show
7342 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7345 if ( item
!== this.currentItem
) {
7346 if ( !this.continuous
) {
7347 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7348 this.items
[i
].$element
.css( 'display', '' );
7351 if ( $.inArray( item
, this.items
) !== -1 ) {
7352 if ( !this.continuous
) {
7353 item
.$element
.css( 'display', 'block' );
7355 this.currentItem
= item
;
7356 this.emit( 'set', item
);
7358 this.unsetCurrentItem();
7366 * Horizontal bar layout of tools as icon buttons.
7369 * @extends OO.ui.ToolGroup
7372 * @param {OO.ui.Toolbar} toolbar
7373 * @param {Object} [config] Configuration options
7375 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7376 // Parent constructor
7377 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7380 this.$element
.addClass( 'oo-ui-barToolGroup' );
7385 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7387 /* Static Properties */
7389 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7391 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7393 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7396 * Popup list of tools with an icon and optional label.
7400 * @extends OO.ui.ToolGroup
7401 * @mixins OO.ui.IconElement
7402 * @mixins OO.ui.IndicatorElement
7403 * @mixins OO.ui.LabelElement
7404 * @mixins OO.ui.TitledElement
7405 * @mixins OO.ui.ClippableElement
7408 * @param {OO.ui.Toolbar} toolbar
7409 * @param {Object} [config] Configuration options
7410 * @cfg {string} [header] Text to display at the top of the pop-up
7412 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7413 // Configuration initialization
7414 config
= config
|| {};
7416 // Parent constructor
7417 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7419 // Mixin constructors
7420 OO
.ui
.IconElement
.call( this, config
);
7421 OO
.ui
.IndicatorElement
.call( this, config
);
7422 OO
.ui
.LabelElement
.call( this, config
);
7423 OO
.ui
.TitledElement
.call( this, config
);
7424 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7427 this.active
= false;
7428 this.dragging
= false;
7429 this.onBlurHandler
= this.onBlur
.bind( this );
7430 this.$handle
= this.$( '<span>' );
7434 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
7435 'mouseup touchend': this.onHandlePointerUp
.bind( this )
7440 .addClass( 'oo-ui-popupToolGroup-handle' )
7441 .append( this.$icon
, this.$label
, this.$indicator
);
7442 // If the pop-up should have a header, add it to the top of the toolGroup.
7443 // Note: If this feature is useful for other widgets, we could abstract it into an
7444 // OO.ui.HeaderedElement mixin constructor.
7445 if ( config
.header
!== undefined ) {
7447 .prepend( this.$( '<span>' )
7448 .addClass( 'oo-ui-popupToolGroup-header' )
7449 .text( config
.header
)
7453 .addClass( 'oo-ui-popupToolGroup' )
7454 .prepend( this.$handle
);
7459 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7460 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7461 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7462 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7463 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7464 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7466 /* Static Properties */
7473 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7475 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7477 if ( this.isDisabled() && this.isElementAttached() ) {
7478 this.setActive( false );
7483 * Handle focus being lost.
7485 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7487 * @param {jQuery.Event} e Mouse up event
7489 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7490 // Only deactivate when clicking outside the dropdown element
7491 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7492 this.setActive( false );
7499 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7500 // e.which is 0 for touch events, 1 for left mouse button
7501 // Only close toolgroup when a tool was actually selected
7502 // FIXME: this duplicates logic from the parent class
7503 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== this.getTargetTool( e
) ) {
7504 this.setActive( false );
7506 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7510 * Handle mouse up events.
7512 * @param {jQuery.Event} e Mouse up event
7514 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7519 * Handle mouse down events.
7521 * @param {jQuery.Event} e Mouse down event
7523 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7524 // e.which is 0 for touch events, 1 for left mouse button
7525 if ( !this.isDisabled() && e
.which
<= 1 ) {
7526 this.setActive( !this.active
);
7532 * Switch into active mode.
7534 * When active, mouseup events anywhere in the document will trigger deactivation.
7536 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7538 if ( this.active
!== value
) {
7539 this.active
= value
;
7541 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7543 // Try anchoring the popup to the left first
7544 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7545 this.toggleClipping( true );
7546 if ( this.isClippedHorizontally() ) {
7547 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7548 this.toggleClipping( false );
7550 .removeClass( 'oo-ui-popupToolGroup-left' )
7551 .addClass( 'oo-ui-popupToolGroup-right' );
7552 this.toggleClipping( true );
7555 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7556 this.$element
.removeClass(
7557 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7559 this.toggleClipping( false );
7565 * Drop down list layout of tools as labeled icon buttons.
7567 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
7568 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
7569 * may want to use the 'promote' and 'demote' configuration options to achieve this.
7572 * @extends OO.ui.PopupToolGroup
7575 * @param {OO.ui.Toolbar} toolbar
7576 * @param {Object} [config] Configuration options
7577 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
7579 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
7580 * allowed to be collapsed.
7581 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
7583 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7584 // Configuration intialization
7585 config
= config
|| {};
7587 // Properties (must be set before parent constructor, which calls #populate)
7588 this.allowCollapse
= config
.allowCollapse
;
7589 this.forceExpand
= config
.forceExpand
;
7590 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
7591 this.collapsibleTools
= [];
7593 // Parent constructor
7594 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7597 this.$element
.addClass( 'oo-ui-listToolGroup' );
7602 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7604 /* Static Properties */
7606 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7608 OO
.ui
.ListToolGroup
.static.name
= 'list';
7615 OO
.ui
.ListToolGroup
.prototype.populate = function () {
7616 var i
, len
, allowCollapse
= [];
7618 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
7620 // Update the list of collapsible tools
7621 if ( this.allowCollapse
!== undefined ) {
7622 allowCollapse
= this.allowCollapse
;
7623 } else if ( this.forceExpand
!== undefined ) {
7624 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
7627 this.collapsibleTools
= [];
7628 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
7629 if ( this.tools
[ allowCollapse
[i
] ] !== undefined ) {
7630 this.collapsibleTools
.push( this.tools
[ allowCollapse
[i
] ] );
7634 // Keep at the end, even when tools are added
7635 this.$group
.append( this.getExpandCollapseTool().$element
);
7637 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
7639 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
7640 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
7641 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
7642 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
7643 this.getExpandCollapseTool().$element
.css( 'display', 'inline-block' );
7646 this.updateCollapsibleState();
7649 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
7650 if ( this.expandCollapseTool
=== undefined ) {
7651 var ExpandCollapseTool = function () {
7652 ExpandCollapseTool
.super.apply( this, arguments
);
7655 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
7657 ExpandCollapseTool
.prototype.onSelect = function () {
7658 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
7659 this.toolGroup
.updateCollapsibleState();
7660 this.setActive( false );
7662 ExpandCollapseTool
.prototype.onUpdateState = function () {
7663 // Do nothing. Tool interface requires an implementation of this function.
7666 ExpandCollapseTool
.static.name
= 'more-fewer';
7668 this.expandCollapseTool
= new ExpandCollapseTool( this );
7670 return this.expandCollapseTool
;
7676 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
7677 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7679 // Do not close the popup when the user wants to show more/fewer tools
7680 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
7681 // Prevent the popup list from being hidden
7682 this.setActive( true );
7688 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
7691 this.getExpandCollapseTool()
7692 .setIcon( this.expanded
? 'collapse' : 'expand' )
7693 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
7695 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
7696 this.collapsibleTools
[i
].toggle( this.expanded
);
7701 * Drop down menu layout of tools as selectable menu items.
7704 * @extends OO.ui.PopupToolGroup
7707 * @param {OO.ui.Toolbar} toolbar
7708 * @param {Object} [config] Configuration options
7710 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7711 // Configuration initialization
7712 config
= config
|| {};
7714 // Parent constructor
7715 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7718 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7721 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7726 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7728 /* Static Properties */
7730 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7732 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7737 * Handle the toolbar state being updated.
7739 * When the state changes, the title of each active item in the menu will be joined together and
7740 * used as a label for the group. The label will be empty if none of the items are active.
7742 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7746 for ( name
in this.tools
) {
7747 if ( this.tools
[name
].isActive() ) {
7748 labelTexts
.push( this.tools
[name
].getTitle() );
7752 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7756 * Tool that shows a popup when selected.
7760 * @extends OO.ui.Tool
7761 * @mixins OO.ui.PopupElement
7764 * @param {OO.ui.Toolbar} toolbar
7765 * @param {Object} [config] Configuration options
7767 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7768 // Parent constructor
7769 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7771 // Mixin constructors
7772 OO
.ui
.PopupElement
.call( this, config
);
7776 .addClass( 'oo-ui-popupTool' )
7777 .append( this.popup
.$element
);
7782 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7783 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7788 * Handle the tool being selected.
7792 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7793 if ( !this.isDisabled() ) {
7794 this.popup
.toggle();
7796 this.setActive( false );
7801 * Handle the toolbar state being updated.
7805 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7806 this.setActive( false );
7810 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7812 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7816 * @extends OO.ui.GroupElement
7819 * @param {Object} [config] Configuration options
7821 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7822 // Parent constructor
7823 OO
.ui
.GroupWidget
.super.call( this, config
);
7828 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7833 * Set the disabled state of the widget.
7835 * This will also update the disabled state of child widgets.
7837 * @param {boolean} disabled Disable widget
7840 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7844 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7845 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7847 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7849 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7850 this.items
[i
].updateDisabled();
7858 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7860 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7861 * allows bidrectional communication.
7863 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7870 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7877 * Check if widget is disabled.
7879 * Checks parent if present, making disabled state inheritable.
7881 * @return {boolean} Widget is disabled
7883 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7884 return this.disabled
||
7885 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7889 * Set group element is in.
7891 * @param {OO.ui.GroupElement|null} group Group element, null if none
7894 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7896 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7897 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7899 // Initialize item disabled states
7900 this.updateDisabled();
7906 * Mixin that adds a menu showing suggested values for a text input.
7908 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7910 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
7911 * be aware that this will cause new suggestions to be looked up for the new value. If this is
7912 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
7918 * @param {OO.ui.TextInputWidget} input Input widget
7919 * @param {Object} [config] Configuration options
7920 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
7921 * @cfg {jQuery} [$container=input.$element] Element to render menu under
7923 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7924 // Configuration initialization
7925 config
= config
|| {};
7928 this.lookupInput
= input
;
7929 this.$overlay
= config
.$overlay
|| this.$element
;
7930 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7931 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7932 input
: this.lookupInput
,
7933 $container
: config
.$container
7935 this.lookupCache
= {};
7936 this.lookupQuery
= null;
7937 this.lookupRequest
= null;
7938 this.lookupsDisabled
= false;
7939 this.lookupInputFocused
= false;
7942 this.lookupInput
.$input
.on( {
7943 focus
: this.onLookupInputFocus
.bind( this ),
7944 blur
: this.onLookupInputBlur
.bind( this ),
7945 mousedown
: this.onLookupInputMouseDown
.bind( this )
7947 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7948 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
7951 this.$element
.addClass( 'oo-ui-lookupWidget' );
7952 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7953 this.$overlay
.append( this.lookupMenu
.$element
);
7959 * Handle input focus event.
7961 * @param {jQuery.Event} e Input focus event
7963 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7964 this.lookupInputFocused
= true;
7965 this.populateLookupMenu();
7969 * Handle input blur event.
7971 * @param {jQuery.Event} e Input blur event
7973 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7974 this.closeLookupMenu();
7975 this.lookupInputFocused
= false;
7979 * Handle input mouse down event.
7981 * @param {jQuery.Event} e Input mouse down event
7983 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7984 // Only open the menu if the input was already focused.
7985 // This way we allow the user to open the menu again after closing it with Esc
7986 // by clicking in the input. Opening (and populating) the menu when initially
7987 // clicking into the input is handled by the focus handler.
7988 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
7989 this.populateLookupMenu();
7994 * Handle input change event.
7996 * @param {string} value New input value
7998 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7999 if ( this.lookupInputFocused
) {
8000 this.populateLookupMenu();
8005 * Handle the lookup menu being shown/hidden.
8006 * @param {boolean} visible Whether the lookup menu is now visible.
8008 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
8010 // When the menu is hidden, abort any active request and clear the menu.
8011 // This has to be done here in addition to closeLookupMenu(), because
8012 // MenuWidget will close itself when the user presses Esc.
8013 this.abortLookupRequest();
8014 this.lookupMenu
.clearItems();
8021 * @return {OO.ui.TextInputMenuWidget}
8023 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
8024 return this.lookupMenu
;
8028 * Disable or re-enable lookups.
8030 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
8032 * @param {boolean} disabled Disable lookups
8034 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
8035 this.lookupsDisabled
= !!disabled
;
8039 * Open the menu. If there are no entries in the menu, this does nothing.
8043 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
8044 if ( !this.lookupMenu
.isEmpty() ) {
8045 this.lookupMenu
.toggle( true );
8051 * Close the menu, empty it, and abort any pending request.
8055 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
8056 this.lookupMenu
.toggle( false );
8057 this.abortLookupRequest();
8058 this.lookupMenu
.clearItems();
8063 * Request menu items based on the input's current value, and when they arrive,
8064 * populate the menu with these items and show the menu.
8066 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
8070 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
8072 value
= this.lookupInput
.getValue();
8074 if ( this.lookupsDisabled
) {
8078 // If the input is empty, clear the menu
8079 if ( value
=== '' ) {
8080 this.closeLookupMenu();
8081 // Skip population if there is already a request pending for the current value
8082 } else if ( value
!== this.lookupQuery
) {
8083 this.getLookupMenuItems()
8084 .done( function ( items
) {
8085 widget
.lookupMenu
.clearItems();
8086 if ( items
.length
) {
8090 widget
.initializeLookupMenuSelection();
8092 widget
.lookupMenu
.toggle( false );
8095 .fail( function () {
8096 widget
.lookupMenu
.clearItems();
8104 * Select and highlight the first selectable item in the menu.
8108 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
8109 if ( !this.lookupMenu
.getSelectedItem() ) {
8110 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
8112 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
8116 * Get lookup menu items for the current query.
8118 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
8119 * of the done event. If the request was aborted to make way for a subsequent request,
8120 * this promise will not be rejected: it will remain pending forever.
8122 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
8124 value
= this.lookupInput
.getValue(),
8125 deferred
= $.Deferred(),
8128 this.abortLookupRequest();
8129 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
8130 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
8132 this.lookupInput
.pushPending();
8133 this.lookupQuery
= value
;
8134 ourRequest
= this.lookupRequest
= this.getLookupRequest();
8136 .always( function () {
8137 // We need to pop pending even if this is an old request, otherwise
8138 // the widget will remain pending forever.
8139 // TODO: this assumes that an aborted request will fail or succeed soon after
8140 // being aborted, or at least eventually. It would be nice if we could popPending()
8141 // at abort time, but only if we knew that we hadn't already called popPending()
8142 // for that request.
8143 widget
.lookupInput
.popPending();
8145 .done( function ( data
) {
8146 // If this is an old request (and aborting it somehow caused it to still succeed),
8147 // ignore its success completely
8148 if ( ourRequest
=== widget
.lookupRequest
) {
8149 widget
.lookupQuery
= null;
8150 widget
.lookupRequest
= null;
8151 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
8152 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
8155 .fail( function () {
8156 // If this is an old request (or a request failing because it's being aborted),
8157 // ignore its failure completely
8158 if ( ourRequest
=== widget
.lookupRequest
) {
8159 widget
.lookupQuery
= null;
8160 widget
.lookupRequest
= null;
8165 return deferred
.promise();
8169 * Abort the currently pending lookup request, if any.
8171 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
8172 var oldRequest
= this.lookupRequest
;
8174 // First unset this.lookupRequest to the fail handler will notice
8175 // that the request is no longer current
8176 this.lookupRequest
= null;
8177 this.lookupQuery
= null;
8183 * Get a new request object of the current lookup query value.
8186 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
8188 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
8189 // Stub, implemented in subclass
8194 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
8197 * @param {Mixed} data Cached result data, usually an array
8198 * @return {OO.ui.MenuItemWidget[]} Menu items
8200 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
8201 // Stub, implemented in subclass
8206 * Get lookup cache item from server response data.
8209 * @param {Mixed} data Response from server
8210 * @return {Mixed} Cached result data
8212 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
8213 // Stub, implemented in subclass
8218 * Set of controls for an OO.ui.OutlineWidget.
8220 * Controls include moving items up and down, removing items, and adding different kinds of items.
8223 * @extends OO.ui.Widget
8224 * @mixins OO.ui.GroupElement
8225 * @mixins OO.ui.IconElement
8228 * @param {OO.ui.OutlineWidget} outline Outline to control
8229 * @param {Object} [config] Configuration options
8231 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
8232 // Configuration initialization
8233 config
= $.extend( { icon
: 'add' }, config
);
8235 // Parent constructor
8236 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
8238 // Mixin constructors
8239 OO
.ui
.GroupElement
.call( this, config
);
8240 OO
.ui
.IconElement
.call( this, config
);
8243 this.outline
= outline
;
8244 this.$movers
= this.$( '<div>' );
8245 this.upButton
= new OO
.ui
.ButtonWidget( {
8249 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
8251 this.downButton
= new OO
.ui
.ButtonWidget( {
8255 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
8257 this.removeButton
= new OO
.ui
.ButtonWidget( {
8261 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
8265 outline
.connect( this, {
8266 select
: 'onOutlineChange',
8267 add
: 'onOutlineChange',
8268 remove
: 'onOutlineChange'
8270 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
8271 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
8272 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
8275 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
8276 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
8278 .addClass( 'oo-ui-outlineControlsWidget-movers' )
8279 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
8280 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
8285 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
8286 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
8287 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
8293 * @param {number} places Number of places to move
8303 * Handle outline change events.
8305 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
8306 var i
, len
, firstMovable
, lastMovable
,
8307 items
= this.outline
.getItems(),
8308 selectedItem
= this.outline
.getSelectedItem(),
8309 movable
= selectedItem
&& selectedItem
.isMovable(),
8310 removable
= selectedItem
&& selectedItem
.isRemovable();
8315 while ( ++i
< len
) {
8316 if ( items
[i
].isMovable() ) {
8317 firstMovable
= items
[i
];
8323 if ( items
[i
].isMovable() ) {
8324 lastMovable
= items
[i
];
8329 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
8330 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
8331 this.removeButton
.setDisabled( !removable
);
8335 * Mixin for widgets with a boolean on/off state.
8341 * @param {Object} [config] Configuration options
8342 * @cfg {boolean} [value=false] Initial value
8344 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8345 // Configuration initialization
8346 config
= config
|| {};
8352 this.$element
.addClass( 'oo-ui-toggleWidget' );
8353 this.setValue( !!config
.value
);
8360 * @param {boolean} value Changed value
8366 * Get the value of the toggle.
8370 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8375 * Set the value of the toggle.
8377 * @param {boolean} value New value
8381 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8383 if ( this.value
!== value
) {
8385 this.emit( 'change', value
);
8386 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8387 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8393 * Group widget for multiple related buttons.
8395 * Use together with OO.ui.ButtonWidget.
8398 * @extends OO.ui.Widget
8399 * @mixins OO.ui.GroupElement
8402 * @param {Object} [config] Configuration options
8403 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
8405 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
8406 // Configuration initialization
8407 config
= config
|| {};
8409 // Parent constructor
8410 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
8412 // Mixin constructors
8413 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8416 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
8417 if ( $.isArray( config
.items
) ) {
8418 this.addItems( config
.items
);
8424 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
8425 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
8428 * Generic widget for buttons.
8431 * @extends OO.ui.Widget
8432 * @mixins OO.ui.ButtonElement
8433 * @mixins OO.ui.IconElement
8434 * @mixins OO.ui.IndicatorElement
8435 * @mixins OO.ui.LabelElement
8436 * @mixins OO.ui.TitledElement
8437 * @mixins OO.ui.FlaggedElement
8440 * @param {Object} [config] Configuration options
8441 * @cfg {string} [href] Hyperlink to visit when clicked
8442 * @cfg {string} [target] Target to open hyperlink in
8444 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8445 // Configuration initialization
8446 config
= $.extend( { target
: '_blank' }, config
);
8448 // Parent constructor
8449 OO
.ui
.ButtonWidget
.super.call( this, config
);
8451 // Mixin constructors
8452 OO
.ui
.ButtonElement
.call( this, config
);
8453 OO
.ui
.IconElement
.call( this, config
);
8454 OO
.ui
.IndicatorElement
.call( this, config
);
8455 OO
.ui
.LabelElement
.call( this, config
);
8456 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
8457 OO
.ui
.FlaggedElement
.call( this, config
);
8462 this.isHyperlink
= false;
8466 click
: this.onClick
.bind( this ),
8467 keypress
: this.onKeyPress
.bind( this )
8471 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8473 .addClass( 'oo-ui-buttonWidget' )
8474 .append( this.$button
);
8475 this.setHref( config
.href
);
8476 this.setTarget( config
.target
);
8481 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8482 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8483 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8484 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8485 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8486 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8487 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8498 * Handles mouse click events.
8500 * @param {jQuery.Event} e Mouse click event
8503 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8504 if ( !this.isDisabled() ) {
8505 this.emit( 'click' );
8506 if ( this.isHyperlink
) {
8514 * Handles keypress events.
8516 * @param {jQuery.Event} e Keypress event
8519 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8520 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8521 this.emit( 'click' );
8522 if ( this.isHyperlink
) {
8530 * Get hyperlink location.
8532 * @return {string} Hyperlink location
8534 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8539 * Get hyperlink target.
8541 * @return {string} Hyperlink target
8543 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8548 * Set hyperlink location.
8550 * @param {string|null} href Hyperlink location, null to remove
8552 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8553 href
= typeof href
=== 'string' ? href
: null;
8555 if ( href
!== this.href
) {
8557 if ( href
!== null ) {
8558 this.$button
.attr( 'href', href
);
8559 this.isHyperlink
= true;
8561 this.$button
.removeAttr( 'href' );
8562 this.isHyperlink
= false;
8570 * Set hyperlink target.
8572 * @param {string|null} target Hyperlink target, null to remove
8574 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8575 target
= typeof target
=== 'string' ? target
: null;
8577 if ( target
!== this.target
) {
8578 this.target
= target
;
8579 if ( target
!== null ) {
8580 this.$button
.attr( 'target', target
);
8582 this.$button
.removeAttr( 'target' );
8590 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8593 * @extends OO.ui.ButtonWidget
8594 * @mixins OO.ui.PendingElement
8597 * @param {Object} [config] Configuration options
8598 * @cfg {string} [action] Symbolic action name
8599 * @cfg {string[]} [modes] Symbolic mode names
8600 * @cfg {boolean} [framed=false] Render button with a frame
8602 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8603 // Configuration initialization
8604 config
= $.extend( { framed
: false }, config
);
8606 // Parent constructor
8607 OO
.ui
.ActionWidget
.super.call( this, config
);
8609 // Mixin constructors
8610 OO
.ui
.PendingElement
.call( this, config
);
8613 this.action
= config
.action
|| '';
8614 this.modes
= config
.modes
|| [];
8619 this.$element
.addClass( 'oo-ui-actionWidget' );
8624 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8625 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
8636 * Check if action is available in a certain mode.
8638 * @param {string} mode Name of mode
8639 * @return {boolean} Has mode
8641 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8642 return this.modes
.indexOf( mode
) !== -1;
8646 * Get symbolic action name.
8650 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8655 * Get symbolic action name.
8659 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8660 return this.modes
.slice();
8664 * Emit a resize event if the size has changed.
8668 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8671 if ( this.isElementAttached() ) {
8672 width
= this.$element
.width();
8673 height
= this.$element
.height();
8675 if ( width
!== this.width
|| height
!== this.height
) {
8677 this.height
= height
;
8678 this.emit( 'resize' );
8688 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8690 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8691 this.propagateResize();
8699 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8701 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8702 this.propagateResize();
8710 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8712 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8713 this.propagateResize();
8721 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8723 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8724 this.propagateResize();
8730 * Toggle visibility of button.
8732 * @param {boolean} [show] Show button, omit to toggle visibility
8735 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8737 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8738 this.propagateResize();
8744 * Button that shows and hides a popup.
8747 * @extends OO.ui.ButtonWidget
8748 * @mixins OO.ui.PopupElement
8751 * @param {Object} [config] Configuration options
8753 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8754 // Parent constructor
8755 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8757 // Mixin constructors
8758 OO
.ui
.PopupElement
.call( this, config
);
8762 .addClass( 'oo-ui-popupButtonWidget' )
8763 .append( this.popup
.$element
);
8768 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8769 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8774 * Handles mouse click events.
8776 * @param {jQuery.Event} e Mouse click event
8778 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8779 // Skip clicks within the popup
8780 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8784 if ( !this.isDisabled() ) {
8785 this.popup
.toggle();
8787 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8793 * Button that toggles on and off.
8796 * @extends OO.ui.ButtonWidget
8797 * @mixins OO.ui.ToggleWidget
8800 * @param {Object} [config] Configuration options
8801 * @cfg {boolean} [value=false] Initial value
8803 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8804 // Configuration initialization
8805 config
= config
|| {};
8807 // Parent constructor
8808 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8810 // Mixin constructors
8811 OO
.ui
.ToggleWidget
.call( this, config
);
8814 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8819 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8820 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8827 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8828 if ( !this.isDisabled() ) {
8829 this.setValue( !this.value
);
8833 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8839 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8841 if ( value
!== this.value
) {
8842 this.setActive( value
);
8845 // Parent method (from mixin)
8846 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8852 * Dropdown menu of options.
8854 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
8855 * can be accessed using the #getMenu method.
8857 * Use with OO.ui.MenuItemWidget.
8860 * @extends OO.ui.Widget
8861 * @mixins OO.ui.IconElement
8862 * @mixins OO.ui.IndicatorElement
8863 * @mixins OO.ui.LabelElement
8864 * @mixins OO.ui.TitledElement
8867 * @param {Object} [config] Configuration options
8868 * @cfg {Object} [menu] Configuration options to pass to menu widget
8870 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
8871 // Configuration initialization
8872 config
= $.extend( { indicator
: 'down' }, config
);
8874 // Parent constructor
8875 OO
.ui
.DropdownWidget
.super.call( this, config
);
8877 // Mixin constructors
8878 OO
.ui
.IconElement
.call( this, config
);
8879 OO
.ui
.IndicatorElement
.call( this, config
);
8880 OO
.ui
.LabelElement
.call( this, config
);
8881 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8884 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8885 this.$handle
= this.$( '<span>' );
8888 this.$element
.on( { click
: this.onClick
.bind( this ) } );
8889 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8893 .addClass( 'oo-ui-dropdownWidget-handle' )
8894 .append( this.$icon
, this.$label
, this.$indicator
);
8896 .addClass( 'oo-ui-dropdownWidget' )
8897 .append( this.$handle
, this.menu
.$element
);
8902 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
8903 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
8904 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
8905 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
8906 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
8913 * @return {OO.ui.MenuWidget} Menu of widget
8915 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
8920 * Handles menu select events.
8922 * @param {OO.ui.MenuItemWidget} item Selected menu item
8924 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
8931 selectedLabel
= item
.getLabel();
8933 // If the label is a DOM element, clone it, because setLabel will append() it
8934 if ( selectedLabel
instanceof jQuery
) {
8935 selectedLabel
= selectedLabel
.clone();
8938 this.setLabel( selectedLabel
);
8942 * Handles mouse click events.
8944 * @param {jQuery.Event} e Mouse click event
8946 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
8947 // Skip clicks within the menu
8948 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8952 if ( !this.isDisabled() ) {
8953 if ( this.menu
.isVisible() ) {
8954 this.menu
.toggle( false );
8956 this.menu
.toggle( true );
8965 * See OO.ui.IconElement for more information.
8968 * @extends OO.ui.Widget
8969 * @mixins OO.ui.IconElement
8970 * @mixins OO.ui.TitledElement
8973 * @param {Object} [config] Configuration options
8975 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8976 // Configuration initialization
8977 config
= config
|| {};
8979 // Parent constructor
8980 OO
.ui
.IconWidget
.super.call( this, config
);
8982 // Mixin constructors
8983 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
8984 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8987 this.$element
.addClass( 'oo-ui-iconWidget' );
8992 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8993 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
8994 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8996 /* Static Properties */
8998 OO
.ui
.IconWidget
.static.tagName
= 'span';
9003 * See OO.ui.IndicatorElement for more information.
9006 * @extends OO.ui.Widget
9007 * @mixins OO.ui.IndicatorElement
9008 * @mixins OO.ui.TitledElement
9011 * @param {Object} [config] Configuration options
9013 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
9014 // Configuration initialization
9015 config
= config
|| {};
9017 // Parent constructor
9018 OO
.ui
.IndicatorWidget
.super.call( this, config
);
9020 // Mixin constructors
9021 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
9022 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9025 this.$element
.addClass( 'oo-ui-indicatorWidget' );
9030 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
9031 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
9032 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
9034 /* Static Properties */
9036 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
9039 * Base class for input widgets.
9043 * @extends OO.ui.Widget
9044 * @mixins OO.ui.FlaggedElement
9047 * @param {Object} [config] Configuration options
9048 * @cfg {string} [name=''] HTML input name
9049 * @cfg {string} [value=''] Input value
9050 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
9052 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
9053 // Configuration initialization
9054 config
= config
|| {};
9056 // Parent constructor
9057 OO
.ui
.InputWidget
.super.call( this, config
);
9059 // Mixin constructors
9060 OO
.ui
.FlaggedElement
.call( this, config
);
9063 this.$input
= this.getInputElement( config
);
9065 this.inputFilter
= config
.inputFilter
;
9068 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
9072 .attr( 'name', config
.name
)
9073 .prop( 'disabled', this.isDisabled() );
9074 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
9075 this.setValue( config
.value
);
9080 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
9081 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
9087 * @param {string} value
9093 * Get input element.
9096 * @param {Object} [config] Configuration options
9097 * @return {jQuery} Input element
9099 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
9100 return this.$( '<input>' );
9104 * Handle potentially value-changing events.
9106 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
9108 OO
.ui
.InputWidget
.prototype.onEdit = function () {
9110 if ( !this.isDisabled() ) {
9111 // Allow the stack to clear so the value will be updated
9112 setTimeout( function () {
9113 widget
.setValue( widget
.$input
.val() );
9119 * Get the value of the input.
9121 * @return {string} Input value
9123 OO
.ui
.InputWidget
.prototype.getValue = function () {
9128 * Sets the direction of the current input, either RTL or LTR
9130 * @param {boolean} isRTL
9132 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
9134 this.$input
.removeClass( 'oo-ui-ltr' );
9135 this.$input
.addClass( 'oo-ui-rtl' );
9137 this.$input
.removeClass( 'oo-ui-rtl' );
9138 this.$input
.addClass( 'oo-ui-ltr' );
9143 * Set the value of the input.
9145 * @param {string} value New value
9149 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
9150 value
= this.sanitizeValue( value
);
9151 if ( this.value
!== value
) {
9153 this.emit( 'change', this.value
);
9155 // Update the DOM if it has changed. Note that with sanitizeValue, it
9156 // is possible for the DOM value to change without this.value changing.
9157 if ( this.$input
.val() !== this.value
) {
9158 this.$input
.val( this.value
);
9164 * Sanitize incoming value.
9166 * Ensures value is a string, and converts undefined and null to empty string.
9169 * @param {string} value Original value
9170 * @return {string} Sanitized value
9172 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
9173 if ( value
=== undefined || value
=== null ) {
9175 } else if ( this.inputFilter
) {
9176 return this.inputFilter( String( value
) );
9178 return String( value
);
9183 * Simulate the behavior of clicking on a label bound to this input.
9185 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
9186 if ( !this.isDisabled() ) {
9187 if ( this.$input
.is( ':checkbox,:radio' ) ) {
9188 this.$input
.click();
9189 } else if ( this.$input
.is( ':input' ) ) {
9190 this.$input
[0].focus();
9198 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
9199 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
9200 if ( this.$input
) {
9201 this.$input
.prop( 'disabled', this.isDisabled() );
9211 OO
.ui
.InputWidget
.prototype.focus = function () {
9212 this.$input
[0].focus();
9221 OO
.ui
.InputWidget
.prototype.blur = function () {
9222 this.$input
[0].blur();
9227 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
9230 * @extends OO.ui.InputWidget
9231 * @mixins OO.ui.ButtonElement
9232 * @mixins OO.ui.IconElement
9233 * @mixins OO.ui.IndicatorElement
9234 * @mixins OO.ui.LabelElement
9235 * @mixins OO.ui.TitledElement
9236 * @mixins OO.ui.FlaggedElement
9239 * @param {Object} [config] Configuration options
9240 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
9241 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
9242 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
9243 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
9244 * won't be possible to set a value (which will internally become identical to the label).
9246 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
9247 // Configuration initialization
9248 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
9250 // Properties (must be set before parent constructor, which calls #setValue)
9251 this.useInputTag
= config
.useInputTag
;
9253 // Parent constructor
9254 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
9256 // Mixin constructors
9257 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
9258 OO
.ui
.IconElement
.call( this, config
);
9259 OO
.ui
.IndicatorElement
.call( this, config
);
9260 OO
.ui
.LabelElement
.call( this, config
);
9261 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
9262 OO
.ui
.FlaggedElement
.call( this, config
);
9266 click
: this.onClick
.bind( this ),
9267 keypress
: this.onKeyPress
.bind( this )
9271 if ( !config
.useInputTag
) {
9272 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
9274 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
9279 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
9280 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
9281 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
9282 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
9283 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
9284 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
9285 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
9296 * Get input element.
9299 * @param {Object} [config] Configuration options
9300 * @return {jQuery} Input element
9302 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
9303 // Configuration intialization
9304 config
= config
|| {};
9306 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
9308 return this.$( html
);
9314 * Overridden to support setting the 'value' of `<input/>` elements.
9316 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
9317 * text; or null for no label
9320 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
9321 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
9323 if ( this.useInputTag
) {
9324 if ( typeof label
=== 'function' ) {
9325 label
= OO
.ui
.resolveMsg( label
);
9327 if ( label
instanceof jQuery
) {
9328 label
= label
.text();
9333 this.$input
.val( label
);
9340 * Set the value of the input.
9342 * Overridden to disable for `<input/>` elements, which have value identical to the label.
9344 * @param {string} value New value
9347 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
9348 if ( !this.useInputTag
) {
9349 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
9355 * Handles mouse click events.
9357 * @param {jQuery.Event} e Mouse click event
9360 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
9361 if ( !this.isDisabled() ) {
9362 this.emit( 'click' );
9368 * Handles keypress events.
9370 * @param {jQuery.Event} e Keypress event
9373 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
9374 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9375 this.emit( 'click' );
9381 * Checkbox input widget.
9384 * @extends OO.ui.InputWidget
9387 * @param {Object} [config] Configuration options
9389 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
9390 // Parent constructor
9391 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
9394 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
9399 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
9404 * Get input element.
9407 * @return {jQuery} Input element
9409 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
9410 return this.$( '<input type="checkbox" />' );
9414 * Get checked state of the checkbox
9416 * @return {boolean} If the checkbox is checked
9418 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
9423 * Set checked state of the checkbox
9425 * @param {boolean} value New value
9427 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
9429 if ( this.value
!== value
) {
9431 this.$input
.prop( 'checked', this.value
);
9432 this.emit( 'change', this.value
);
9439 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
9441 if ( !this.isDisabled() ) {
9442 // Allow the stack to clear so the value will be updated
9443 setTimeout( function () {
9444 widget
.setValue( widget
.$input
.prop( 'checked' ) );
9450 * Input widget with a text field.
9453 * @extends OO.ui.InputWidget
9454 * @mixins OO.ui.IconElement
9455 * @mixins OO.ui.IndicatorElement
9456 * @mixins OO.ui.PendingElement
9459 * @param {Object} [config] Configuration options
9460 * @cfg {string} [type='text'] HTML tag `type` attribute
9461 * @cfg {string} [placeholder] Placeholder text
9462 * @cfg {boolean} [readOnly=false] Prevent changes
9463 * @cfg {boolean} [multiline=false] Allow multiple lines of text
9464 * @cfg {boolean} [autosize=false] Automatically resize to fit content
9465 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
9466 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
9467 * one, see #static-validationPatterns)
9469 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
9470 // Configuration initialization
9471 config
= $.extend( { readOnly
: false }, config
);
9473 // Parent constructor
9474 OO
.ui
.TextInputWidget
.super.call( this, config
);
9476 // Mixin constructors
9477 OO
.ui
.IconElement
.call( this, config
);
9478 OO
.ui
.IndicatorElement
.call( this, config
);
9479 OO
.ui
.PendingElement
.call( this, config
);
9482 this.readOnly
= false;
9483 this.multiline
= !!config
.multiline
;
9484 this.autosize
= !!config
.autosize
;
9485 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
9486 this.validate
= null;
9488 this.setValidation( config
.validate
);
9492 keypress
: this.onKeyPress
.bind( this ),
9493 blur
: this.setValidityFlag
.bind( this )
9495 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
9496 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
9497 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
9501 .addClass( 'oo-ui-textInputWidget' )
9502 .append( this.$icon
, this.$indicator
);
9503 this.setReadOnly( config
.readOnly
);
9504 if ( config
.placeholder
) {
9505 this.$input
.attr( 'placeholder', config
.placeholder
);
9507 this.$element
.attr( 'role', 'textbox' );
9512 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
9513 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
9514 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
9515 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
9517 /* Static properties */
9519 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
9527 * User presses enter inside the text box.
9529 * Not called if input is multiline.
9535 * User clicks the icon.
9541 * User clicks the indicator.
9549 * Handle icon mouse down events.
9551 * @param {jQuery.Event} e Mouse down event
9554 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
9555 if ( e
.which
=== 1 ) {
9556 this.$input
[0].focus();
9557 this.emit( 'icon' );
9563 * Handle indicator mouse down events.
9565 * @param {jQuery.Event} e Mouse down event
9568 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
9569 if ( e
.which
=== 1 ) {
9570 this.$input
[0].focus();
9571 this.emit( 'indicator' );
9577 * Handle key press events.
9579 * @param {jQuery.Event} e Key press event
9580 * @fires enter If enter key is pressed and input is not multiline
9582 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
9583 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
9584 this.emit( 'enter' );
9589 * Handle element attach events.
9591 * @param {jQuery.Event} e Element attach event
9593 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
9600 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
9604 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9610 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9612 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9614 this.setValidityFlag();
9620 * Check if the widget is read-only.
9624 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
9625 return this.readOnly
;
9629 * Set the read-only state of the widget.
9631 * This should probably change the widgets's appearance and prevent it from being used.
9633 * @param {boolean} state Make input read-only
9636 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
9637 this.readOnly
= !!state
;
9638 this.$input
.prop( 'readOnly', this.readOnly
);
9643 * Automatically adjust the size of the text input.
9645 * This only affects multi-line inputs that are auto-sized.
9649 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9650 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
9652 if ( this.multiline
&& this.autosize
) {
9653 $clone
= this.$input
.clone()
9654 .val( this.$input
.val() )
9655 // Set inline height property to 0 to measure scroll height
9656 .css( { height
: 0 } )
9657 .insertAfter( this.$input
);
9658 scrollHeight
= $clone
[0].scrollHeight
;
9659 // Remove inline height property to measure natural heights
9660 $clone
.css( 'height', '' );
9661 innerHeight
= $clone
.innerHeight();
9662 outerHeight
= $clone
.outerHeight();
9663 // Measure max rows height
9664 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' ).val( '' );
9665 maxInnerHeight
= $clone
.innerHeight();
9666 // Difference between reported innerHeight and scrollHeight with no scrollbars present
9667 // Equals 1 on Blink-based browsers and 0 everywhere else
9668 measurementError
= maxInnerHeight
- $clone
[0].scrollHeight
;
9670 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
9671 // Only apply inline height when expansion beyond natural height is needed
9672 if ( idealHeight
> innerHeight
) {
9673 // Use the difference between the inner and outer height as a buffer
9674 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
9676 this.$input
.css( 'height', '' );
9683 * Get input element.
9686 * @param {Object} [config] Configuration options
9687 * @return {jQuery} Input element
9689 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9690 // Configuration initialization
9691 config
= config
|| {};
9693 var type
= config
.type
|| 'text';
9695 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + type
+ '" />' );
9699 * Check if input supports multiple lines.
9703 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9704 return !!this.multiline
;
9708 * Check if input automatically adjusts its size.
9712 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9713 return !!this.autosize
;
9717 * Select the contents of the input.
9721 OO
.ui
.TextInputWidget
.prototype.select = function () {
9722 this.$input
.select();
9727 * Sets the validation pattern to use.
9728 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
9729 * one, see #static-validationPatterns)
9731 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
9732 if ( validate
instanceof RegExp
) {
9733 this.validate
= validate
;
9735 this.validate
= this.constructor.static.validationPatterns
[validate
] || /.*/;
9740 * Sets the 'invalid' flag appropriately.
9742 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
9744 this.isValid().done( function ( valid
) {
9745 widget
.setFlags( { invalid
: !valid
} );
9750 * Returns whether or not the current value is considered valid, according to the
9751 * supplied validation pattern.
9753 * @return {jQuery.Deferred}
9755 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
9756 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
9760 * Text input with a menu of optional values.
9763 * @extends OO.ui.Widget
9766 * @param {Object} [config] Configuration options
9767 * @cfg {Object} [menu] Configuration options to pass to menu widget
9768 * @cfg {Object} [input] Configuration options to pass to input widget
9769 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
9771 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9772 // Configuration initialization
9773 config
= config
|| {};
9775 // Parent constructor
9776 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9779 this.$overlay
= config
.$overlay
|| this.$element
;
9780 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9781 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9784 this.menu
= new OO
.ui
.TextInputMenuWidget( this.input
, $.extend(
9786 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
9789 disabled
: this.isDisabled()
9795 this.input
.connect( this, {
9796 change
: 'onInputChange',
9797 indicator
: 'onInputIndicator',
9798 enter
: 'onInputEnter'
9800 this.menu
.connect( this, {
9801 choose
: 'onMenuChoose',
9802 add
: 'onMenuItemsChange',
9803 remove
: 'onMenuItemsChange'
9807 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
9808 this.$overlay
.append( this.menu
.$element
);
9809 this.onMenuItemsChange();
9814 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9819 * Handle input change events.
9821 * @param {string} value New value
9823 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9824 var match
= this.menu
.getItemFromData( value
);
9826 this.menu
.selectItem( match
);
9828 if ( !this.isDisabled() ) {
9829 this.menu
.toggle( true );
9834 * Handle input indicator events.
9836 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9837 if ( !this.isDisabled() ) {
9843 * Handle input enter events.
9845 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
9846 if ( !this.isDisabled() ) {
9847 this.menu
.toggle( false );
9852 * Handle menu choose events.
9854 * @param {OO.ui.OptionWidget} item Chosen item
9856 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
9858 this.input
.setValue( item
.getData() );
9863 * Handle menu item change events.
9865 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
9866 var match
= this.menu
.getItemFromData( this.input
.getValue() );
9867 this.menu
.selectItem( match
);
9868 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
9874 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
9876 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
9879 this.input
.setDisabled( this.isDisabled() );
9882 this.menu
.setDisabled( this.isDisabled() );
9892 * @extends OO.ui.Widget
9893 * @mixins OO.ui.LabelElement
9896 * @param {Object} [config] Configuration options
9898 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
9899 // Configuration initialization
9900 config
= config
|| {};
9902 // Parent constructor
9903 OO
.ui
.LabelWidget
.super.call( this, config
);
9905 // Mixin constructors
9906 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
9907 OO
.ui
.TitledElement
.call( this, config
);
9910 this.input
= config
.input
;
9913 if ( this.input
instanceof OO
.ui
.InputWidget
) {
9914 this.$element
.on( 'click', this.onClick
.bind( this ) );
9918 this.$element
.addClass( 'oo-ui-labelWidget' );
9923 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
9924 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
9925 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
9927 /* Static Properties */
9929 OO
.ui
.LabelWidget
.static.tagName
= 'span';
9934 * Handles label mouse click events.
9936 * @param {jQuery.Event} e Mouse click event
9938 OO
.ui
.LabelWidget
.prototype.onClick = function () {
9939 this.input
.simulateLabelClick();
9944 * Generic option widget for use with OO.ui.SelectWidget.
9947 * @extends OO.ui.Widget
9948 * @mixins OO.ui.LabelElement
9949 * @mixins OO.ui.FlaggedElement
9952 * @param {Mixed} data Option data
9953 * @param {Object} [config] Configuration options
9955 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
9956 // Configuration initialization
9957 config
= config
|| {};
9959 // Parent constructor
9960 OO
.ui
.OptionWidget
.super.call( this, config
);
9962 // Mixin constructors
9963 OO
.ui
.ItemWidget
.call( this );
9964 OO
.ui
.LabelElement
.call( this, config
);
9965 OO
.ui
.FlaggedElement
.call( this, config
);
9969 this.selected
= false;
9970 this.highlighted
= false;
9971 this.pressed
= false;
9975 .data( 'oo-ui-optionWidget', this )
9976 .attr( 'role', 'option' )
9977 .addClass( 'oo-ui-optionWidget' )
9978 .append( this.$label
);
9983 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
9984 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
9985 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
9986 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
9988 /* Static Properties */
9990 OO
.ui
.OptionWidget
.static.selectable
= true;
9992 OO
.ui
.OptionWidget
.static.highlightable
= true;
9994 OO
.ui
.OptionWidget
.static.pressable
= true;
9996 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
10001 * Check if option can be selected.
10003 * @return {boolean} Item is selectable
10005 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
10006 return this.constructor.static.selectable
&& !this.isDisabled();
10010 * Check if option can be highlighted.
10012 * @return {boolean} Item is highlightable
10014 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
10015 return this.constructor.static.highlightable
&& !this.isDisabled();
10019 * Check if option can be pressed.
10021 * @return {boolean} Item is pressable
10023 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
10024 return this.constructor.static.pressable
&& !this.isDisabled();
10028 * Check if option is selected.
10030 * @return {boolean} Item is selected
10032 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
10033 return this.selected
;
10037 * Check if option is highlighted.
10039 * @return {boolean} Item is highlighted
10041 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
10042 return this.highlighted
;
10046 * Check if option is pressed.
10048 * @return {boolean} Item is pressed
10050 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
10051 return this.pressed
;
10055 * Set selected state.
10057 * @param {boolean} [state=false] Select option
10060 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
10061 if ( this.constructor.static.selectable
) {
10062 this.selected
= !!state
;
10063 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
10064 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
10065 this.scrollElementIntoView();
10067 this.updateThemeClasses();
10073 * Set highlighted state.
10075 * @param {boolean} [state=false] Highlight option
10078 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
10079 if ( this.constructor.static.highlightable
) {
10080 this.highlighted
= !!state
;
10081 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
10082 this.updateThemeClasses();
10088 * Set pressed state.
10090 * @param {boolean} [state=false] Press option
10093 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
10094 if ( this.constructor.static.pressable
) {
10095 this.pressed
= !!state
;
10096 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
10097 this.updateThemeClasses();
10103 * Make the option's highlight flash.
10105 * While flashing, the visual style of the pressed state is removed if present.
10107 * @return {jQuery.Promise} Promise resolved when flashing is done
10109 OO
.ui
.OptionWidget
.prototype.flash = function () {
10111 $element
= this.$element
,
10112 deferred
= $.Deferred();
10114 if ( !this.isDisabled() && this.constructor.static.pressable
) {
10115 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
10116 setTimeout( function () {
10117 // Restore original classes
10119 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
10120 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
10122 setTimeout( function () {
10123 deferred
.resolve();
10129 return deferred
.promise();
10135 * @return {Mixed} Option data
10137 OO
.ui
.OptionWidget
.prototype.getData = function () {
10142 * Option widget with an option icon and indicator.
10144 * Use together with OO.ui.SelectWidget.
10147 * @extends OO.ui.OptionWidget
10148 * @mixins OO.ui.IconElement
10149 * @mixins OO.ui.IndicatorElement
10152 * @param {Mixed} data Option data
10153 * @param {Object} [config] Configuration options
10155 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
10156 // Parent constructor
10157 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
10159 // Mixin constructors
10160 OO
.ui
.IconElement
.call( this, config
);
10161 OO
.ui
.IndicatorElement
.call( this, config
);
10165 .addClass( 'oo-ui-decoratedOptionWidget' )
10166 .prepend( this.$icon
)
10167 .append( this.$indicator
);
10172 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
10173 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
10174 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
10177 * Option widget that looks like a button.
10179 * Use together with OO.ui.ButtonSelectWidget.
10182 * @extends OO.ui.DecoratedOptionWidget
10183 * @mixins OO.ui.ButtonElement
10186 * @param {Mixed} data Option data
10187 * @param {Object} [config] Configuration options
10189 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
10190 // Parent constructor
10191 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
10193 // Mixin constructors
10194 OO
.ui
.ButtonElement
.call( this, config
);
10197 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
10198 this.$button
.append( this.$element
.contents() );
10199 this.$element
.append( this.$button
);
10204 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10205 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
10207 /* Static Properties */
10209 // Allow button mouse down events to pass through so they can be handled by the parent select widget
10210 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
10217 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
10218 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
10220 if ( this.constructor.static.selectable
) {
10221 this.setActive( state
);
10228 * Item of an OO.ui.MenuWidget.
10231 * @extends OO.ui.DecoratedOptionWidget
10234 * @param {Mixed} data Item data
10235 * @param {Object} [config] Configuration options
10237 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
10238 // Configuration initialization
10239 config
= $.extend( { icon
: 'check' }, config
);
10241 // Parent constructor
10242 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
10246 .attr( 'role', 'menuitem' )
10247 .addClass( 'oo-ui-menuItemWidget' );
10252 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10255 * Section to group one or more items in a OO.ui.MenuWidget.
10258 * @extends OO.ui.DecoratedOptionWidget
10261 * @param {Mixed} data Item data
10262 * @param {Object} [config] Configuration options
10264 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
10265 // Parent constructor
10266 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
10269 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
10274 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10276 /* Static Properties */
10278 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
10280 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
10283 * Items for an OO.ui.OutlineWidget.
10286 * @extends OO.ui.DecoratedOptionWidget
10289 * @param {Mixed} data Item data
10290 * @param {Object} [config] Configuration options
10291 * @cfg {number} [level] Indentation level
10292 * @cfg {boolean} [movable] Allow modification from outline controls
10294 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
10295 // Configuration initialization
10296 config
= config
|| {};
10298 // Parent constructor
10299 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
10303 this.movable
= !!config
.movable
;
10304 this.removable
= !!config
.removable
;
10307 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
10308 this.setLevel( config
.level
);
10313 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10315 /* Static Properties */
10317 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
10319 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
10321 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
10323 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
10328 * Check if item is movable.
10330 * Movablilty is used by outline controls.
10332 * @return {boolean} Item is movable
10334 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
10335 return this.movable
;
10339 * Check if item is removable.
10341 * Removablilty is used by outline controls.
10343 * @return {boolean} Item is removable
10345 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
10346 return this.removable
;
10350 * Get indentation level.
10352 * @return {number} Indentation level
10354 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
10361 * Movablilty is used by outline controls.
10363 * @param {boolean} movable Item is movable
10366 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
10367 this.movable
= !!movable
;
10368 this.updateThemeClasses();
10373 * Set removability.
10375 * Removablilty is used by outline controls.
10377 * @param {boolean} movable Item is removable
10380 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
10381 this.removable
= !!removable
;
10382 this.updateThemeClasses();
10387 * Set indentation level.
10389 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
10392 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
10393 var levels
= this.constructor.static.levels
,
10394 levelClass
= this.constructor.static.levelClass
,
10397 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
10399 if ( this.level
=== i
) {
10400 this.$element
.addClass( levelClass
+ i
);
10402 this.$element
.removeClass( levelClass
+ i
);
10405 this.updateThemeClasses();
10411 * Container for content that is overlaid and positioned absolutely.
10414 * @extends OO.ui.Widget
10415 * @mixins OO.ui.LabelElement
10418 * @param {Object} [config] Configuration options
10419 * @cfg {number} [width=320] Width of popup in pixels
10420 * @cfg {number} [height] Height of popup, omit to use automatic height
10421 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
10422 * @cfg {string} [align='center'] Alignment of popup to origin
10423 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
10424 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
10425 * @cfg {jQuery} [$content] Content to append to the popup's body
10426 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
10427 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
10428 * @cfg {boolean} [head] Show label and close button at the top
10429 * @cfg {boolean} [padded] Add padding to the body
10431 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
10432 // Configuration initialization
10433 config
= config
|| {};
10435 // Parent constructor
10436 OO
.ui
.PopupWidget
.super.call( this, config
);
10438 // Mixin constructors
10439 OO
.ui
.LabelElement
.call( this, config
);
10440 OO
.ui
.ClippableElement
.call( this, config
);
10443 this.visible
= false;
10444 this.$popup
= this.$( '<div>' );
10445 this.$head
= this.$( '<div>' );
10446 this.$body
= this.$( '<div>' );
10447 this.$anchor
= this.$( '<div>' );
10448 // If undefined, will be computed lazily in updateDimensions()
10449 this.$container
= config
.$container
;
10450 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
10451 this.autoClose
= !!config
.autoClose
;
10452 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
10453 this.transitionTimeout
= null;
10454 this.anchor
= null;
10455 this.width
= config
.width
!== undefined ? config
.width
: 320;
10456 this.height
= config
.height
!== undefined ? config
.height
: null;
10457 this.align
= config
.align
|| 'center';
10458 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
10459 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
10462 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
10465 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
10466 this.$body
.addClass( 'oo-ui-popupWidget-body' );
10467 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
10469 .addClass( 'oo-ui-popupWidget-head' )
10470 .append( this.$label
, this.closeButton
.$element
);
10471 if ( !config
.head
) {
10475 .addClass( 'oo-ui-popupWidget-popup' )
10476 .append( this.$head
, this.$body
);
10479 .addClass( 'oo-ui-popupWidget' )
10480 .append( this.$popup
, this.$anchor
);
10481 // Move content, which was added to #$element by OO.ui.Widget, to the body
10482 if ( config
.$content
instanceof jQuery
) {
10483 this.$body
.append( config
.$content
);
10485 if ( config
.padded
) {
10486 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
10488 this.setClippableElement( this.$body
);
10493 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
10494 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
10495 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
10500 * Handles mouse down events.
10502 * @param {jQuery.Event} e Mouse down event
10504 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
10506 this.isVisible() &&
10507 !$.contains( this.$element
[0], e
.target
) &&
10508 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
10510 this.toggle( false );
10515 * Bind mouse down listener.
10517 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
10518 // Capture clicks outside popup
10519 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
10523 * Handles close button click events.
10525 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
10526 if ( this.isVisible() ) {
10527 this.toggle( false );
10532 * Unbind mouse down listener.
10534 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
10535 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
10539 * Set whether to show a anchor.
10541 * @param {boolean} [show] Show anchor, omit to toggle
10543 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
10544 show
= show
=== undefined ? !this.anchored
: !!show
;
10546 if ( this.anchored
!== show
) {
10548 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
10550 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
10552 this.anchored
= show
;
10557 * Check if showing a anchor.
10559 * @return {boolean} anchor is visible
10561 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
10562 return this.anchor
;
10568 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
10569 show
= show
=== undefined ? !this.isVisible() : !!show
;
10571 var change
= show
!== this.isVisible();
10574 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
10578 if ( this.autoClose
) {
10579 this.bindMouseDownListener();
10581 this.updateDimensions();
10582 this.toggleClipping( true );
10584 this.toggleClipping( false );
10585 if ( this.autoClose
) {
10586 this.unbindMouseDownListener();
10595 * Set the size of the popup.
10597 * Changing the size may also change the popup's position depending on the alignment.
10599 * @param {number} width Width
10600 * @param {number} height Height
10601 * @param {boolean} [transition=false] Use a smooth transition
10604 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
10605 this.width
= width
;
10606 this.height
= height
!== undefined ? height
: null;
10607 if ( this.isVisible() ) {
10608 this.updateDimensions( transition
);
10613 * Update the size and position.
10615 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
10616 * be called automatically.
10618 * @param {boolean} [transition=false] Use a smooth transition
10621 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
10622 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
10623 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
10626 if ( !this.$container
) {
10627 // Lazy-initialize $container if not specified in constructor
10628 this.$container
= this.$( this.getClosestScrollableElementContainer() );
10631 // Set height and width before measuring things, since it might cause our measurements
10632 // to change (e.g. due to scrollbars appearing or disappearing)
10635 height
: this.height
!== null ? this.height
: 'auto'
10638 // Compute initial popupOffset based on alignment
10639 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
10641 // Figure out if this will cause the popup to go beyond the edge of the container
10642 originOffset
= Math
.round( this.$element
.offset().left
);
10643 containerLeft
= Math
.round( this.$container
.offset().left
);
10644 containerWidth
= this.$container
.innerWidth();
10645 containerRight
= containerLeft
+ containerWidth
;
10646 popupLeft
= popupOffset
- this.containerPadding
;
10647 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
10648 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
10649 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10651 // Adjust offset to make the popup not go beyond the edge, if needed
10652 if ( overlapRight
< 0 ) {
10653 popupOffset
+= overlapRight
;
10654 } else if ( overlapLeft
< 0 ) {
10655 popupOffset
-= overlapLeft
;
10658 // Adjust offset to avoid anchor being rendered too close to the edge
10659 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
10660 // TODO: Find a measurement that works for CSS anchors and image anchors
10661 anchorWidth
= this.$anchor
[0].scrollWidth
* 2;
10662 if ( popupOffset
+ this.width
< anchorWidth
) {
10663 popupOffset
= anchorWidth
- this.width
;
10664 } else if ( -popupOffset
< anchorWidth
) {
10665 popupOffset
= -anchorWidth
;
10668 // Prevent transition from being interrupted
10669 clearTimeout( this.transitionTimeout
);
10670 if ( transition
) {
10671 // Enable transition
10672 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10675 // Position body relative to anchor
10676 this.$popup
.css( 'margin-left', popupOffset
);
10678 if ( transition
) {
10679 // Prevent transitioning after transition is complete
10680 this.transitionTimeout
= setTimeout( function () {
10681 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10684 // Prevent transitioning immediately
10685 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10688 // Reevaluate clipping state since we've relocated and resized the popup
10695 * Progress bar widget.
10698 * @extends OO.ui.Widget
10701 * @param {Object} [config] Configuration options
10702 * @cfg {number|boolean} [progress=false] Initial progress percent or false for indeterminate
10704 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
10705 // Configuration initialization
10706 config
= config
|| {};
10708 // Parent constructor
10709 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
10712 this.$bar
= this.$( '<div>' );
10713 this.progress
= null;
10716 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
10717 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar');
10720 role
: 'progressbar',
10721 'aria-valuemin': 0,
10722 'aria-valuemax': 100
10724 .addClass( 'oo-ui-progressBarWidget' )
10725 .append( this.$bar
);
10730 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
10732 /* Static Properties */
10734 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
10739 * Get progress percent
10741 * @return {number} Progress percent
10743 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
10744 return this.progress
;
10748 * Set progress percent
10750 * @param {number|boolean} progress Progress percent or false for indeterminate
10752 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
10753 this.progress
= progress
;
10755 if ( progress
!== false ) {
10756 this.$bar
.css( 'width', this.progress
+ '%' );
10757 this.$element
.attr( 'aria-valuenow', this.progress
);
10759 this.$bar
.css( 'width', '' );
10760 this.$element
.removeAttr( 'aria-valuenow' );
10762 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
10768 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10769 * Results are cleared and populated each time the query is changed.
10772 * @extends OO.ui.Widget
10775 * @param {Object} [config] Configuration options
10776 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10777 * @cfg {string} [value] Initial query value
10779 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10780 // Configuration intialization
10781 config
= config
|| {};
10783 // Parent constructor
10784 OO
.ui
.SearchWidget
.super.call( this, config
);
10787 this.query
= new OO
.ui
.TextInputWidget( {
10790 placeholder
: config
.placeholder
,
10791 value
: config
.value
10793 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10794 this.$query
= this.$( '<div>' );
10795 this.$results
= this.$( '<div>' );
10798 this.query
.connect( this, {
10799 change
: 'onQueryChange',
10800 enter
: 'onQueryEnter'
10802 this.results
.connect( this, {
10803 highlight
: 'onResultsHighlight',
10804 select
: 'onResultsSelect'
10806 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
10810 .addClass( 'oo-ui-searchWidget-query' )
10811 .append( this.query
.$element
);
10813 .addClass( 'oo-ui-searchWidget-results' )
10814 .append( this.results
.$element
);
10816 .addClass( 'oo-ui-searchWidget' )
10817 .append( this.$results
, this.$query
);
10822 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
10828 * @param {Object|null} item Item data or null if no item is highlighted
10833 * @param {Object|null} item Item data or null if no item is selected
10839 * Handle query key down events.
10841 * @param {jQuery.Event} e Key down event
10843 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
10844 var highlightedItem
, nextItem
,
10845 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
10848 highlightedItem
= this.results
.getHighlightedItem();
10849 if ( !highlightedItem
) {
10850 highlightedItem
= this.results
.getSelectedItem();
10852 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
10853 this.results
.highlightItem( nextItem
);
10854 nextItem
.scrollElementIntoView();
10859 * Handle select widget select events.
10861 * Clears existing results. Subclasses should repopulate items according to new query.
10863 * @param {string} value New value
10865 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
10867 this.results
.clearItems();
10871 * Handle select widget enter key events.
10873 * Selects highlighted item.
10875 * @param {string} value New value
10877 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
10879 this.results
.selectItem( this.results
.getHighlightedItem() );
10883 * Handle select widget highlight events.
10885 * @param {OO.ui.OptionWidget} item Highlighted item
10888 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
10889 this.emit( 'highlight', item
? item
.getData() : null );
10893 * Handle select widget select events.
10895 * @param {OO.ui.OptionWidget} item Selected item
10898 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
10899 this.emit( 'select', item
? item
.getData() : null );
10903 * Get the query input.
10905 * @return {OO.ui.TextInputWidget} Query input
10907 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
10912 * Get the results list.
10914 * @return {OO.ui.SelectWidget} Select list
10916 OO
.ui
.SearchWidget
.prototype.getResults = function () {
10917 return this.results
;
10921 * Generic selection of options.
10923 * Items can contain any rendering, and are uniquely identified by a hash of their data. Any widget
10924 * that provides options, from which the user must choose one, should be built on this class.
10926 * Use together with OO.ui.OptionWidget.
10929 * @extends OO.ui.Widget
10930 * @mixins OO.ui.GroupElement
10933 * @param {Object} [config] Configuration options
10934 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
10936 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
10937 // Configuration initialization
10938 config
= config
|| {};
10940 // Parent constructor
10941 OO
.ui
.SelectWidget
.super.call( this, config
);
10943 // Mixin constructors
10944 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
10947 this.pressed
= false;
10948 this.selecting
= null;
10950 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
10951 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
10954 this.$element
.on( {
10955 mousedown
: this.onMouseDown
.bind( this ),
10956 mouseover
: this.onMouseOver
.bind( this ),
10957 mouseleave
: this.onMouseLeave
.bind( this )
10961 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
10962 if ( $.isArray( config
.items
) ) {
10963 this.addItems( config
.items
);
10969 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
10971 // Need to mixin base class as well
10972 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
10973 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
10979 * @param {OO.ui.OptionWidget|null} item Highlighted item
10984 * @param {OO.ui.OptionWidget|null} item Pressed item
10989 * @param {OO.ui.OptionWidget|null} item Selected item
10994 * @param {OO.ui.OptionWidget|null} item Chosen item
10999 * @param {OO.ui.OptionWidget[]} items Added items
11000 * @param {number} index Index items were added at
11005 * @param {OO.ui.OptionWidget[]} items Removed items
11011 * Handle mouse down events.
11014 * @param {jQuery.Event} e Mouse down event
11016 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
11019 if ( !this.isDisabled() && e
.which
=== 1 ) {
11020 this.togglePressed( true );
11021 item
= this.getTargetItem( e
);
11022 if ( item
&& item
.isSelectable() ) {
11023 this.pressItem( item
);
11024 this.selecting
= item
;
11025 this.getElementDocument().addEventListener(
11027 this.onMouseUpHandler
,
11030 this.getElementDocument().addEventListener(
11032 this.onMouseMoveHandler
,
11041 * Handle mouse up events.
11044 * @param {jQuery.Event} e Mouse up event
11046 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
11049 this.togglePressed( false );
11050 if ( !this.selecting
) {
11051 item
= this.getTargetItem( e
);
11052 if ( item
&& item
.isSelectable() ) {
11053 this.selecting
= item
;
11056 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
11057 this.pressItem( null );
11058 this.chooseItem( this.selecting
);
11059 this.selecting
= null;
11062 this.getElementDocument().removeEventListener(
11064 this.onMouseUpHandler
,
11067 this.getElementDocument().removeEventListener(
11069 this.onMouseMoveHandler
,
11077 * Handle mouse move events.
11080 * @param {jQuery.Event} e Mouse move event
11082 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
11085 if ( !this.isDisabled() && this.pressed
) {
11086 item
= this.getTargetItem( e
);
11087 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
11088 this.pressItem( item
);
11089 this.selecting
= item
;
11096 * Handle mouse over events.
11099 * @param {jQuery.Event} e Mouse over event
11101 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
11104 if ( !this.isDisabled() ) {
11105 item
= this.getTargetItem( e
);
11106 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
11112 * Handle mouse leave events.
11115 * @param {jQuery.Event} e Mouse over event
11117 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
11118 if ( !this.isDisabled() ) {
11119 this.highlightItem( null );
11125 * Get the closest item to a jQuery.Event.
11128 * @param {jQuery.Event} e
11129 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
11131 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
11132 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
11133 if ( $item
.length
) {
11134 return $item
.data( 'oo-ui-optionWidget' );
11140 * Get selected item.
11142 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
11144 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
11147 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11148 if ( this.items
[i
].isSelected() ) {
11149 return this.items
[i
];
11156 * Get highlighted item.
11158 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
11160 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
11163 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11164 if ( this.items
[i
].isHighlighted() ) {
11165 return this.items
[i
];
11172 * Get an existing item with equivilant data.
11174 * @param {Object} data Item data to search for
11175 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
11177 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
11178 var hash
= OO
.getHash( data
);
11180 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11181 return this.hashes
[hash
];
11188 * Toggle pressed state.
11190 * @param {boolean} pressed An option is being pressed
11192 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
11193 if ( pressed
=== undefined ) {
11194 pressed
= !this.pressed
;
11196 if ( pressed
!== this.pressed
) {
11198 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
11199 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
11200 this.pressed
= pressed
;
11205 * Highlight an item.
11207 * Highlighting is mutually exclusive.
11209 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
11213 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
11214 var i
, len
, highlighted
,
11217 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11218 highlighted
= this.items
[i
] === item
;
11219 if ( this.items
[i
].isHighlighted() !== highlighted
) {
11220 this.items
[i
].setHighlighted( highlighted
);
11225 this.emit( 'highlight', item
);
11234 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
11238 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
11239 var i
, len
, selected
,
11242 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11243 selected
= this.items
[i
] === item
;
11244 if ( this.items
[i
].isSelected() !== selected
) {
11245 this.items
[i
].setSelected( selected
);
11250 this.emit( 'select', item
);
11259 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
11263 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
11264 var i
, len
, pressed
,
11267 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11268 pressed
= this.items
[i
] === item
;
11269 if ( this.items
[i
].isPressed() !== pressed
) {
11270 this.items
[i
].setPressed( pressed
);
11275 this.emit( 'press', item
);
11284 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
11285 * an item is selected using the keyboard or mouse.
11287 * @param {OO.ui.OptionWidget} item Item to choose
11291 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
11292 this.selectItem( item
);
11293 this.emit( 'choose', item
);
11299 * Get an item relative to another one.
11301 * @param {OO.ui.OptionWidget} item Item to start at
11302 * @param {number} direction Direction to move in, -1 to look backward, 1 to move forward
11303 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
11305 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
11306 var inc
= direction
> 0 ? 1 : -1,
11307 len
= this.items
.length
,
11308 index
= item
instanceof OO
.ui
.OptionWidget
?
11309 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
11310 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
11312 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
11313 Math
.max( index
, -1 ) :
11314 // Default to n-1 instead of -1, if nothing is selected let's start at the end
11315 Math
.min( index
, len
);
11317 while ( len
!== 0 ) {
11318 i
= ( i
+ inc
+ len
) % len
;
11319 item
= this.items
[i
];
11320 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11323 // Stop iterating when we've looped all the way around
11324 if ( i
=== stopAt
) {
11332 * Get the next selectable item.
11334 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
11336 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
11339 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11340 item
= this.items
[i
];
11341 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11352 * When items are added with the same values as existing items, the existing items will be
11353 * automatically removed before the new items are added.
11355 * @param {OO.ui.OptionWidget[]} items Items to add
11356 * @param {number} [index] Index to insert items after
11360 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
11361 var i
, len
, item
, hash
,
11364 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11366 hash
= OO
.getHash( item
.getData() );
11367 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11368 // Remove item with same value
11369 remove
.push( this.hashes
[hash
] );
11371 this.hashes
[hash
] = item
;
11373 if ( remove
.length
) {
11374 this.removeItems( remove
);
11378 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
11380 // Always provide an index, even if it was omitted
11381 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
11389 * Items will be detached, not removed, so they can be used later.
11391 * @param {OO.ui.OptionWidget[]} items Items to remove
11395 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
11396 var i
, len
, item
, hash
;
11398 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11400 hash
= OO
.getHash( item
.getData() );
11401 if ( Object
.prototype.hasOwnProperty
.call( this.hashes
, hash
) ) {
11402 // Remove existing item
11403 delete this.hashes
[hash
];
11405 if ( item
.isSelected() ) {
11406 this.selectItem( null );
11411 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
11413 this.emit( 'remove', items
);
11421 * Items will be detached, not removed, so they can be used later.
11426 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
11427 var items
= this.items
.slice();
11432 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
11433 this.selectItem( null );
11435 this.emit( 'remove', items
);
11441 * Select widget containing button options.
11443 * Use together with OO.ui.ButtonOptionWidget.
11446 * @extends OO.ui.SelectWidget
11449 * @param {Object} [config] Configuration options
11451 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
11452 // Parent constructor
11453 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
11456 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
11461 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
11464 * Overlaid menu of options.
11466 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
11469 * Use together with OO.ui.MenuItemWidget.
11472 * @extends OO.ui.SelectWidget
11473 * @mixins OO.ui.ClippableElement
11476 * @param {Object} [config] Configuration options
11477 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
11478 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
11479 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
11481 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
11482 // Configuration initialization
11483 config
= config
|| {};
11485 // Parent constructor
11486 OO
.ui
.MenuWidget
.super.call( this, config
);
11488 // Mixin constructors
11489 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
11492 this.flashing
= false;
11493 this.visible
= false;
11494 this.newItems
= null;
11495 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
11496 this.$input
= config
.input
? config
.input
.$input
: null;
11497 this.$widget
= config
.widget
? config
.widget
.$element
: null;
11498 this.$previousFocus
= null;
11499 this.isolated
= !config
.input
;
11500 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
11501 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
11506 .attr( 'role', 'menu' )
11507 .addClass( 'oo-ui-menuWidget' );
11512 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
11513 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
11518 * Handles document mouse down events.
11520 * @param {jQuery.Event} e Key down event
11522 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
11524 !OO
.ui
.contains( this.$element
[0], e
.target
, true ) &&
11525 ( !this.$widget
|| !OO
.ui
.contains( this.$widget
[0], e
.target
, true ) )
11527 this.toggle( false );
11532 * Handles key down events.
11534 * @param {jQuery.Event} e Key down event
11536 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
11539 highlightItem
= this.getHighlightedItem();
11541 if ( !this.isDisabled() && this.isVisible() ) {
11542 if ( !highlightItem
) {
11543 highlightItem
= this.getSelectedItem();
11545 switch ( e
.keyCode
) {
11546 case OO
.ui
.Keys
.ENTER
:
11547 this.chooseItem( highlightItem
);
11550 case OO
.ui
.Keys
.UP
:
11551 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
11554 case OO
.ui
.Keys
.DOWN
:
11555 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
11558 case OO
.ui
.Keys
.ESCAPE
:
11559 if ( highlightItem
) {
11560 highlightItem
.setHighlighted( false );
11562 this.toggle( false );
11568 this.highlightItem( nextItem
);
11569 nextItem
.scrollElementIntoView();
11573 e
.preventDefault();
11574 e
.stopPropagation();
11581 * Bind key down listener.
11583 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
11584 if ( this.$input
) {
11585 this.$input
.on( 'keydown', this.onKeyDownHandler
);
11587 // Capture menu navigation keys
11588 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
11593 * Unbind key down listener.
11595 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
11596 if ( this.$input
) {
11597 this.$input
.off( 'keydown' );
11599 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
11606 * This will close the menu when done, unlike selectItem which only changes selection.
11608 * @param {OO.ui.OptionWidget} item Item to choose
11611 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
11615 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
11617 if ( item
&& !this.flashing
) {
11618 this.flashing
= true;
11619 item
.flash().done( function () {
11620 widget
.toggle( false );
11621 widget
.flashing
= false;
11624 this.toggle( false );
11633 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
11637 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
11640 if ( !this.newItems
) {
11641 this.newItems
= [];
11644 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11646 if ( this.isVisible() ) {
11647 // Defer fitting label until item has been attached
11650 this.newItems
.push( item
);
11654 // Reevaluate clipping
11663 OO
.ui
.MenuWidget
.prototype.removeItems = function ( items
) {
11665 OO
.ui
.MenuWidget
.super.prototype.removeItems
.call( this, items
);
11667 // Reevaluate clipping
11676 OO
.ui
.MenuWidget
.prototype.clearItems = function () {
11678 OO
.ui
.MenuWidget
.super.prototype.clearItems
.call( this );
11680 // Reevaluate clipping
11689 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
11690 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
11693 change
= visible
!== this.isVisible(),
11694 elementDoc
= this.getElementDocument(),
11695 widgetDoc
= this.$widget
? this.$widget
[0].ownerDocument
: null;
11698 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
11702 this.bindKeyDownListener();
11704 // Change focus to enable keyboard navigation
11705 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
11706 this.$previousFocus
= this.$( ':focus' );
11707 this.$input
[0].focus();
11709 if ( this.newItems
&& this.newItems
.length
) {
11710 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
11711 this.newItems
[i
].fitLabel();
11713 this.newItems
= null;
11715 this.toggleClipping( true );
11718 if ( this.autoHide
) {
11719 elementDoc
.addEventListener(
11720 'mousedown', this.onDocumentMouseDownHandler
, true
11722 // Support $widget being in a different document
11723 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11724 widgetDoc
.addEventListener(
11725 'mousedown', this.onDocumentMouseDownHandler
, true
11730 this.unbindKeyDownListener();
11731 if ( this.isolated
&& this.$previousFocus
) {
11732 this.$previousFocus
[0].focus();
11733 this.$previousFocus
= null;
11735 elementDoc
.removeEventListener(
11736 'mousedown', this.onDocumentMouseDownHandler
, true
11738 // Support $widget being in a different document
11739 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11740 widgetDoc
.removeEventListener(
11741 'mousedown', this.onDocumentMouseDownHandler
, true
11744 this.toggleClipping( false );
11752 * Menu for a text input widget.
11754 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11755 * is in a different frame, the menu's position is automatically calculated and maintained when the
11756 * menu is toggled or the window is resized.
11759 * @extends OO.ui.MenuWidget
11762 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11763 * @param {Object} [config] Configuration options
11764 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11766 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
11767 // Configuration intialization
11768 config
= config
|| {};
11770 // Parent constructor
11771 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
11774 this.input
= input
;
11775 this.$container
= config
.$container
|| this.input
.$element
;
11776 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
11779 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
11784 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
11789 * Handle window resize event.
11791 * @param {jQuery.Event} e Window resize event
11793 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
11800 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
11801 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
11803 var change
= visible
!== this.isVisible();
11805 if ( change
&& visible
) {
11806 // Make sure the width is set before the parent method runs.
11807 // After this we have to call this.position(); again to actually
11808 // position ourselves correctly.
11813 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
11816 if ( this.isVisible() ) {
11818 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11820 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
11828 * Position the menu.
11832 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
11833 var $container
= this.$container
,
11834 pos
= OO
.ui
.Element
.getRelativePosition( $container
, this.$element
.offsetParent() );
11836 // Position under input
11837 pos
.top
+= $container
.height();
11838 this.$element
.css( pos
);
11841 this.setIdealSize( $container
.width() );
11842 // We updated the position, so re-evaluate the clipping state
11849 * Structured list of items.
11851 * Use with OO.ui.OutlineItemWidget.
11854 * @extends OO.ui.SelectWidget
11857 * @param {Object} [config] Configuration options
11859 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
11860 // Configuration initialization
11861 config
= config
|| {};
11863 // Parent constructor
11864 OO
.ui
.OutlineWidget
.super.call( this, config
);
11867 this.$element
.addClass( 'oo-ui-outlineWidget' );
11872 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
11875 * Switch that slides on and off.
11878 * @extends OO.ui.Widget
11879 * @mixins OO.ui.ToggleWidget
11882 * @param {Object} [config] Configuration options
11883 * @cfg {boolean} [value=false] Initial value
11885 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
11886 // Parent constructor
11887 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
11889 // Mixin constructors
11890 OO
.ui
.ToggleWidget
.call( this, config
);
11893 this.dragging
= false;
11894 this.dragStart
= null;
11895 this.sliding
= false;
11896 this.$glow
= this.$( '<span>' );
11897 this.$grip
= this.$( '<span>' );
11900 this.$element
.on( 'click', this.onClick
.bind( this ) );
11903 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
11904 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
11906 .addClass( 'oo-ui-toggleSwitchWidget' )
11907 .append( this.$glow
, this.$grip
);
11912 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
11913 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
11918 * Handle mouse down events.
11920 * @param {jQuery.Event} e Mouse down event
11922 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
11923 if ( !this.isDisabled() && e
.which
=== 1 ) {
11924 this.setValue( !this.value
);