2 * OOjs UI v0.1.0-pre (9ed4cf2557)
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-22T01:21:13Z
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 descendants
105 * @return {boolean} The node is in the list of target nodes
107 OO
.ui
.contains = function ( containers
, contained
, matchContainers
) {
109 if ( !Array
.isArray( containers
) ) {
110 containers
= [ containers
];
112 for ( i
= containers
.length
- 1; i
>= 0; i
-- ) {
113 if ( ( matchContainers
&& contained
=== containers
[i
] ) || $.contains( containers
[i
], contained
) ) {
122 * Message store for the default implementation of OO.ui.msg
124 * Environments that provide a localization system should not use this, but should override
125 * OO.ui.msg altogether.
130 // Tool tip for a button that moves items in a list down one place
131 'ooui-outline-control-move-down': 'Move item down',
132 // Tool tip for a button that moves items in a list up one place
133 'ooui-outline-control-move-up': 'Move item up',
134 // Tool tip for a button that removes items from a list
135 'ooui-outline-control-remove': 'Remove item',
136 // Label for the toolbar group that contains a list of all other available tools
137 'ooui-toolbar-more': 'More',
138 // Label for the fake tool that expands the full list of tools in a toolbar group
139 'ooui-toolgroup-expand': 'More',
140 // Label for the fake tool that collapses the full list of tools in a toolbar group
141 'ooui-toolgroup-collapse': 'Fewer',
142 // Default label for the accept button of a confirmation dialog
143 'ooui-dialog-message-accept': 'OK',
144 // Default label for the reject button of a confirmation dialog
145 'ooui-dialog-message-reject': 'Cancel',
146 // Title for process dialog error description
147 'ooui-dialog-process-error': 'Something went wrong',
148 // Label for process dialog dismiss error button, visible when describing errors
149 'ooui-dialog-process-dismiss': 'Dismiss',
150 // Label for process dialog retry action button, visible when describing only recoverable errors
151 'ooui-dialog-process-retry': 'Try again',
152 // Label for process dialog retry action button, visible when describing only warnings
153 'ooui-dialog-process-continue': 'Continue'
157 * Get a localized message.
159 * In environments that provide a localization system, this function should be overridden to
160 * return the message translated in the user's language. The default implementation always returns
163 * After the message key, message parameters may optionally be passed. In the default implementation,
164 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
165 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
166 * they support unnamed, ordered message parameters.
169 * @param {string} key Message key
170 * @param {Mixed...} [params] Message parameters
171 * @return {string} Translated message with parameters substituted
173 OO
.ui
.msg = function ( key
) {
174 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
175 if ( typeof message
=== 'string' ) {
176 // Perform $1 substitution
177 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
178 var i
= parseInt( n
, 10 );
179 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
182 // Return placeholder if message not found
183 message
= '[' + key
+ ']';
189 * Package a message and arguments for deferred resolution.
191 * Use this when you are statically specifying a message and the message may not yet be present.
193 * @param {string} key Message key
194 * @param {Mixed...} [params] Message parameters
195 * @return {Function} Function that returns the resolved message when executed
197 OO
.ui
.deferMsg = function () {
198 var args
= arguments
;
200 return OO
.ui
.msg
.apply( OO
.ui
, args
);
207 * If the message is a function it will be executed, otherwise it will pass through directly.
209 * @param {Function|string} msg Deferred message, or message text
210 * @return {string} Resolved message
212 OO
.ui
.resolveMsg = function ( msg
) {
213 if ( $.isFunction( msg
) ) {
222 * Element that can be marked as pending.
228 * @param {Object} [config] Configuration options
229 * @cfg {jQuery} [$pending] Element to mark as pending, defaults to this.$element
231 OO
.ui
.PendingElement
= function OoUiPendingElement( config
) {
232 // Configuration initialization
233 config
= config
|| {};
237 this.$pending
= null;
240 this.setPendingElement( config
.$pending
|| this.$element
);
245 OO
.initClass( OO
.ui
.PendingElement
);
250 * Set the pending element (and clean up any existing one).
252 * @param {jQuery} $pending The element to set to pending.
254 OO
.ui
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
255 if ( this.$pending
) {
256 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
259 this.$pending
= $pending
;
260 if ( this.pending
> 0 ) {
261 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
266 * Check if input is pending.
270 OO
.ui
.PendingElement
.prototype.isPending = function () {
271 return !!this.pending
;
275 * Increase the pending stack.
279 OO
.ui
.PendingElement
.prototype.pushPending = function () {
280 if ( this.pending
=== 0 ) {
281 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
282 this.updateThemeClasses();
290 * Reduce the pending stack.
296 OO
.ui
.PendingElement
.prototype.popPending = function () {
297 if ( this.pending
=== 1 ) {
298 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
299 this.updateThemeClasses();
301 this.pending
= Math
.max( 0, this.pending
- 1 );
311 * @mixins OO.EventEmitter
314 * @param {Object} [config] Configuration options
316 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
317 // Configuration initialization
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 whenever 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 categories
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)
726 * @cfg {Mixed} [data] Element data
728 OO
.ui
.Element
= function OoUiElement( config
) {
729 // Configuration initialization
730 config
= config
|| {};
733 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
734 this.data
= config
.data
;
735 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
736 this.elementGroup
= null;
737 this.debouncedUpdateThemeClassesHandler
= this.debouncedUpdateThemeClasses
.bind( this );
738 this.updateThemeClassesPending
= false;
741 if ( $.isArray( config
.classes
) ) {
742 this.$element
.addClass( config
.classes
.join( ' ' ) );
745 this.$element
.text( config
.text
);
747 if ( config
.$content
) {
748 this.$element
.append( config
.$content
);
754 OO
.initClass( OO
.ui
.Element
);
756 /* Static Properties */
761 * This may be ignored if #getTagName is overridden.
767 OO
.ui
.Element
.static.tagName
= 'div';
772 * Get a jQuery function within a specific document.
775 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
776 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
778 * @return {Function} Bound jQuery function
780 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
781 function wrapper( selector
) {
782 return $( selector
, wrapper
.context
);
785 wrapper
.context
= this.getDocument( context
);
788 wrapper
.$iframe
= $iframe
;
795 * Get the document of an element.
798 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
799 * @return {HTMLDocument|null} Document object
801 OO
.ui
.Element
.getDocument = function ( obj
) {
802 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
803 return ( obj
[0] && obj
[0].ownerDocument
) ||
804 // Empty jQuery selections might have a context
811 ( obj
.nodeType
=== 9 && obj
) ||
816 * Get the window of an element or document.
819 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
820 * @return {Window} Window object
822 OO
.ui
.Element
.getWindow = function ( obj
) {
823 var doc
= this.getDocument( obj
);
824 return doc
.parentWindow
|| doc
.defaultView
;
828 * Get the direction of an element or document.
831 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
832 * @return {string} Text direction, either 'ltr' or 'rtl'
834 OO
.ui
.Element
.getDir = function ( obj
) {
837 if ( obj
instanceof jQuery
) {
840 isDoc
= obj
.nodeType
=== 9;
841 isWin
= obj
.document
!== undefined;
842 if ( isDoc
|| isWin
) {
848 return $( obj
).css( 'direction' );
852 * Get the offset between two frames.
854 * TODO: Make this function not use recursion.
857 * @param {Window} from Window of the child frame
858 * @param {Window} [to=window] Window of the parent frame
859 * @param {Object} [offset] Offset to start with, used internally
860 * @return {Object} Offset object, containing left and top properties
862 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
863 var i
, len
, frames
, frame
, rect
;
869 offset
= { top
: 0, left
: 0 };
871 if ( from.parent
=== from ) {
875 // Get iframe element
876 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
877 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
878 if ( frames
[i
].contentWindow
=== from ) {
884 // Recursively accumulate offset values
886 rect
= frame
.getBoundingClientRect();
887 offset
.left
+= rect
.left
;
888 offset
.top
+= rect
.top
;
890 this.getFrameOffset( from.parent
, offset
);
897 * Get the offset between two elements.
899 * The two elements may be in a different frame, but in that case the frame $element is in must
900 * be contained in the frame $anchor is in.
903 * @param {jQuery} $element Element whose position to get
904 * @param {jQuery} $anchor Element to get $element's position relative to
905 * @return {Object} Translated position coordinates, containing top and left properties
907 OO
.ui
.Element
.getRelativePosition = function ( $element
, $anchor
) {
908 var iframe
, iframePos
,
909 pos
= $element
.offset(),
910 anchorPos
= $anchor
.offset(),
911 elementDocument
= this.getDocument( $element
),
912 anchorDocument
= this.getDocument( $anchor
);
914 // If $element isn't in the same document as $anchor, traverse up
915 while ( elementDocument
!== anchorDocument
) {
916 iframe
= elementDocument
.defaultView
.frameElement
;
918 throw new Error( '$element frame is not contained in $anchor frame' );
920 iframePos
= $( iframe
).offset();
921 pos
.left
+= iframePos
.left
;
922 pos
.top
+= iframePos
.top
;
923 elementDocument
= iframe
.ownerDocument
;
925 pos
.left
-= anchorPos
.left
;
926 pos
.top
-= anchorPos
.top
;
931 * Get element border sizes.
934 * @param {HTMLElement} el Element to measure
935 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
937 OO
.ui
.Element
.getBorders = function ( el
) {
938 var doc
= el
.ownerDocument
,
939 win
= doc
.parentWindow
|| doc
.defaultView
,
940 style
= win
&& win
.getComputedStyle
?
941 win
.getComputedStyle( el
, null ) :
944 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
945 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
946 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
947 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
950 top
: Math
.round( top
),
951 left
: Math
.round( left
),
952 bottom
: Math
.round( bottom
),
953 right
: Math
.round( right
)
958 * Get dimensions of an element or window.
961 * @param {HTMLElement|Window} el Element to measure
962 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
964 OO
.ui
.Element
.getDimensions = function ( el
) {
966 doc
= el
.ownerDocument
|| el
.document
,
967 win
= doc
.parentWindow
|| doc
.defaultView
;
969 if ( win
=== el
|| el
=== doc
.documentElement
) {
972 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
974 top
: $win
.scrollTop(),
975 left
: $win
.scrollLeft()
977 scrollbar
: { right
: 0, bottom
: 0 },
981 bottom
: $win
.innerHeight(),
982 right
: $win
.innerWidth()
988 borders
: this.getBorders( el
),
990 top
: $el
.scrollTop(),
991 left
: $el
.scrollLeft()
994 right
: $el
.innerWidth() - el
.clientWidth
,
995 bottom
: $el
.innerHeight() - el
.clientHeight
997 rect
: el
.getBoundingClientRect()
1003 * Get closest scrollable container.
1005 * Traverses up until either a scrollable element or the root is reached, in which case the window
1009 * @param {HTMLElement} el Element to find scrollable container for
1010 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
1011 * @return {HTMLElement} Closest scrollable container
1013 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
1015 props
= [ 'overflow' ],
1016 $parent
= $( el
).parent();
1018 if ( dimension
=== 'x' || dimension
=== 'y' ) {
1019 props
.push( 'overflow-' + dimension
);
1022 while ( $parent
.length
) {
1023 if ( $parent
[0] === el
.ownerDocument
.body
) {
1028 val
= $parent
.css( props
[i
] );
1029 if ( val
=== 'auto' || val
=== 'scroll' ) {
1033 $parent
= $parent
.parent();
1035 return this.getDocument( el
).body
;
1039 * Scroll element into view.
1042 * @param {HTMLElement} el Element to scroll into view
1043 * @param {Object} [config] Configuration options
1044 * @param {string} [config.duration] jQuery animation duration value
1045 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
1046 * to scroll in both directions
1047 * @param {Function} [config.complete] Function to call when scrolling completes
1049 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
1050 // Configuration initialization
1051 config
= config
|| {};
1054 callback
= typeof config
.complete
=== 'function' && config
.complete
,
1055 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
1057 eld
= this.getDimensions( el
),
1058 scd
= this.getDimensions( sc
),
1059 $win
= $( this.getWindow( el
) );
1061 // Compute the distances between the edges of el and the edges of the scroll viewport
1062 if ( $sc
.is( 'body' ) ) {
1063 // If the scrollable container is the <body> this is easy
1066 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
1067 left
: eld
.rect
.left
,
1068 right
: $win
.innerWidth() - eld
.rect
.right
1071 // Otherwise, we have to subtract el's coordinates from sc's coordinates
1073 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
1074 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
1075 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
1076 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
1080 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1081 if ( rel
.top
< 0 ) {
1082 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
1083 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
1084 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
1087 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1088 if ( rel
.left
< 0 ) {
1089 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
1090 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
1091 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
1094 if ( !$.isEmptyObject( anim
) ) {
1095 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
1097 $sc
.queue( function ( next
) {
1110 * Bind a handler for an event on a DOM element.
1112 * Used to be for working around a jQuery bug (jqbug.com/14180),
1113 * but obsolete as of jQuery 1.11.0.
1116 * @deprecated Use jQuery#on instead.
1117 * @param {HTMLElement|jQuery} el DOM element
1118 * @param {string} event Event to bind
1119 * @param {Function} callback Callback to call when the event fires
1121 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1122 $( el
).on( event
, callback
);
1126 * Unbind a handler bound with #static-method-onDOMEvent.
1128 * @deprecated Use jQuery#off instead.
1130 * @param {HTMLElement|jQuery} el DOM element
1131 * @param {string} event Event to unbind
1132 * @param {Function} [callback] Callback to unbind
1134 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1135 $( el
).off( event
, callback
);
1143 * @return {Mixed} Element data
1145 OO
.ui
.Element
.prototype.getData = function () {
1152 * @param {Mixed} Element data
1155 OO
.ui
.Element
.prototype.setData = function ( data
) {
1161 * Check if element supports one or more methods.
1163 * @param {string|string[]} methods Method or list of methods to check
1164 * @return {boolean} All methods are supported
1166 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1170 methods
= $.isArray( methods
) ? methods
: [ methods
];
1171 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1172 if ( $.isFunction( this[methods
[i
]] ) ) {
1177 return methods
.length
=== support
;
1181 * Update the theme-provided classes.
1183 * @localdoc This is called in element mixins and widget classes any time state changes.
1184 * Updating is debounced, minimizing overhead of changing multiple attributes and
1185 * guaranteeing that theme updates do not occur within an element's constructor
1187 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1188 if ( !this.updateThemeClassesPending
) {
1189 this.updateThemeClassesPending
= true;
1190 setTimeout( this.debouncedUpdateThemeClassesHandler
);
1197 OO
.ui
.Element
.prototype.debouncedUpdateThemeClasses = function () {
1198 OO
.ui
.theme
.updateElementClasses( this );
1199 this.updateThemeClassesPending
= false;
1203 * Get the HTML tag name.
1205 * Override this method to base the result on instance information.
1207 * @return {string} HTML tag name
1209 OO
.ui
.Element
.prototype.getTagName = function () {
1210 return this.constructor.static.tagName
;
1214 * Check if the element is attached to the DOM
1215 * @return {boolean} The element is attached to the DOM
1217 OO
.ui
.Element
.prototype.isElementAttached = function () {
1218 return $.contains( this.getElementDocument(), this.$element
[0] );
1222 * Get the DOM document.
1224 * @return {HTMLDocument} Document object
1226 OO
.ui
.Element
.prototype.getElementDocument = function () {
1227 return OO
.ui
.Element
.getDocument( this.$element
);
1231 * Get the DOM window.
1233 * @return {Window} Window object
1235 OO
.ui
.Element
.prototype.getElementWindow = function () {
1236 return OO
.ui
.Element
.getWindow( this.$element
);
1240 * Get closest scrollable container.
1242 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1243 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1247 * Get group element is in.
1249 * @return {OO.ui.GroupElement|null} Group element, null if none
1251 OO
.ui
.Element
.prototype.getElementGroup = function () {
1252 return this.elementGroup
;
1256 * Set group element is in.
1258 * @param {OO.ui.GroupElement|null} group Group element, null if none
1261 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1262 this.elementGroup
= group
;
1267 * Scroll element into view.
1269 * @param {Object} [config] Configuration options
1271 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1272 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1276 * Bind a handler for an event on this.$element
1278 * @deprecated Use jQuery#on instead.
1279 * @param {string} event
1280 * @param {Function} callback
1282 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1283 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1287 * Unbind a handler bound with #offDOMEvent
1289 * @deprecated Use jQuery#off instead.
1290 * @param {string} event
1291 * @param {Function} callback
1293 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1294 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1298 * Container for elements.
1302 * @extends OO.ui.Element
1303 * @mixins OO.EventEmitter
1306 * @param {Object} [config] Configuration options
1308 OO
.ui
.Layout
= function OoUiLayout( config
) {
1309 // Configuration initialization
1310 config
= config
|| {};
1312 // Parent constructor
1313 OO
.ui
.Layout
.super.call( this, config
);
1315 // Mixin constructors
1316 OO
.EventEmitter
.call( this );
1319 this.$element
.addClass( 'oo-ui-layout' );
1324 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1325 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1328 * User interface control.
1332 * @extends OO.ui.Element
1333 * @mixins OO.EventEmitter
1336 * @param {Object} [config] Configuration options
1337 * @cfg {boolean} [disabled=false] Disable
1339 OO
.ui
.Widget
= function OoUiWidget( config
) {
1340 // Initialize config
1341 config
= $.extend( { disabled
: false }, config
);
1343 // Parent constructor
1344 OO
.ui
.Widget
.super.call( this, config
);
1346 // Mixin constructors
1347 OO
.EventEmitter
.call( this );
1350 this.visible
= true;
1351 this.disabled
= null;
1352 this.wasDisabled
= null;
1355 this.$element
.addClass( 'oo-ui-widget' );
1356 this.setDisabled( !!config
.disabled
);
1361 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1362 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1368 * @param {boolean} disabled Widget is disabled
1373 * @param {boolean} visible Widget is visible
1379 * Check if the widget is disabled.
1381 * @return {boolean} Button is disabled
1383 OO
.ui
.Widget
.prototype.isDisabled = function () {
1384 return this.disabled
;
1388 * Check if widget is visible.
1390 * @return {boolean} Widget is visible
1392 OO
.ui
.Widget
.prototype.isVisible = function () {
1393 return this.visible
;
1397 * Set the disabled state of the widget.
1399 * This should probably change the widgets' appearance and prevent it from being used.
1401 * @param {boolean} disabled Disable widget
1404 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1407 this.disabled
= !!disabled
;
1408 isDisabled
= this.isDisabled();
1409 if ( isDisabled
!== this.wasDisabled
) {
1410 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1411 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1412 this.emit( 'disable', isDisabled
);
1413 this.updateThemeClasses();
1415 this.wasDisabled
= isDisabled
;
1421 * Toggle visibility of widget.
1423 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1427 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1428 show
= show
=== undefined ? !this.visible
: !!show
;
1430 if ( show
!== this.isVisible() ) {
1431 this.visible
= show
;
1432 this.$element
.toggle( show
);
1433 this.emit( 'toggle', show
);
1440 * Update the disabled state, in case of changes in parent widget.
1444 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1445 this.setDisabled( this.disabled
);
1450 * Container for elements in a child frame.
1452 * Use together with OO.ui.WindowManager.
1456 * @extends OO.ui.Element
1457 * @mixins OO.EventEmitter
1459 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1460 * teardown processes are executed when the window is closed.
1462 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1463 * - Window manager begins opening window
1464 * - {@link #getSetupProcess} method is called and its result executed
1465 * - {@link #getReadyProcess} method is called and its result executed
1466 * - Window is now open
1468 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1469 * - Window manager begins closing window
1470 * - {@link #getHoldProcess} method is called and its result executed
1471 * - {@link #getTeardownProcess} method is called and its result executed
1472 * - Window is now closed
1474 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1475 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1476 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchronous
1477 * processing can complete. Always assume window processes are executed asynchronously. See
1478 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1479 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1481 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1482 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1485 * @param {Object} [config] Configuration options
1486 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1489 OO
.ui
.Window
= function OoUiWindow( config
) {
1490 // Configuration initialization
1491 config
= config
|| {};
1493 // Parent constructor
1494 OO
.ui
.Window
.super.call( this, config
);
1496 // Mixin constructors
1497 OO
.EventEmitter
.call( this );
1500 this.manager
= null;
1501 this.initialized
= false;
1502 this.visible
= false;
1503 this.opening
= null;
1504 this.closing
= null;
1507 this.loading
= null;
1508 this.size
= config
.size
|| this.constructor.static.size
;
1509 this.$frame
= this.$( '<div>' );
1510 this.$overlay
= this.$( '<div>' );
1514 .addClass( 'oo-ui-window' )
1515 .append( this.$frame
, this.$overlay
);
1516 this.$frame
.addClass( 'oo-ui-window-frame' );
1517 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1519 // NOTE: Additional initialization will occur when #setManager is called
1524 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1525 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1527 /* Static Properties */
1530 * Symbolic name of size.
1532 * Size is used if no size is configured during construction.
1536 * @property {string}
1538 OO
.ui
.Window
.static.size
= 'medium';
1540 /* Static Methods */
1543 * Transplant the CSS styles from as parent document to a frame's document.
1545 * This loops over the style sheets in the parent document, and copies their nodes to the
1546 * frame's document. It then polls the document to see when all styles have loaded, and once they
1547 * have, resolves the promise.
1549 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1550 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1551 * Firefox, where the styles won't load until the iframe becomes visible.
1553 * For details of how we arrived at the strategy used in this function, see #load.
1557 * @param {HTMLDocument} parentDoc Document to transplant styles from
1558 * @param {HTMLDocument} frameDoc Document to transplant styles to
1559 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1560 * @return {jQuery.Promise} Promise resolved when styles have loaded
1562 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1563 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1564 $pollNodes
= $( [] ),
1565 // Fake font-family value
1566 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1567 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1568 deferred
= $.Deferred();
1570 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1571 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1572 if ( styleNode
.disabled
) {
1576 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1577 // External stylesheet; use @import
1578 styleText
= '@import url(' + styleNode
.href
+ ');';
1580 // Internal stylesheet; just copy the text
1581 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1582 // other browsers, so fall back to '' rather than 'undefined'
1583 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1586 // Create a node with a unique ID that we're going to monitor to see when the CSS
1588 if ( styleNode
.oouiFrameTransplantStylesId
) {
1589 // If we're nesting transplantStyles operations and this node already has
1590 // a CSS rule to wait for loading, reuse it
1591 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1593 // Otherwise, create a new ID
1594 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1597 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1598 // The font-family rule will only take effect once the @import finishes
1599 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1602 // Create a node with id=pollNodeId
1603 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1604 .attr( 'id', pollNodeId
)
1605 .appendTo( frameDoc
.body
)
1608 // Add our modified CSS as a <style> tag
1609 newNode
= frameDoc
.createElement( 'style' );
1610 newNode
.textContent
= styleText
;
1611 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1612 frameDoc
.head
.appendChild( newNode
);
1614 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1616 // Poll every 100ms until all external stylesheets have loaded
1617 $pendingPollNodes
= $pollNodes
;
1618 timeoutID
= setTimeout( function pollExternalStylesheets() {
1620 $pendingPollNodes
.length
> 0 &&
1621 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1623 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1626 if ( $pendingPollNodes
.length
=== 0 ) {
1628 if ( timeoutID
!== null ) {
1630 $pollNodes
.remove();
1634 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1637 // ...but give up after a while
1638 if ( timeout
!== 0 ) {
1639 setTimeout( function () {
1641 clearTimeout( timeoutID
);
1643 $pollNodes
.remove();
1646 }, timeout
|| 5000 );
1649 return deferred
.promise();
1655 * Handle mouse down events.
1657 * @param {jQuery.Event} e Mouse down event
1659 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1660 // Prevent clicking on the click-block from stealing focus
1661 if ( e
.target
=== this.$element
[0] ) {
1667 * Check if window has been initialized.
1669 * @return {boolean} Window has been initialized
1671 OO
.ui
.Window
.prototype.isInitialized = function () {
1672 return this.initialized
;
1676 * Check if window is visible.
1678 * @return {boolean} Window is visible
1680 OO
.ui
.Window
.prototype.isVisible = function () {
1681 return this.visible
;
1685 * Check if window is loading.
1687 * @return {boolean} Window is loading
1689 OO
.ui
.Window
.prototype.isLoading = function () {
1690 return this.loading
&& this.loading
.state() === 'pending';
1694 * Check if window is loaded.
1696 * @return {boolean} Window is loaded
1698 OO
.ui
.Window
.prototype.isLoaded = function () {
1699 return this.loading
&& this.loading
.state() === 'resolved';
1703 * Check if window is opening.
1705 * This is a wrapper around OO.ui.WindowManager#isOpening.
1707 * @return {boolean} Window is opening
1709 OO
.ui
.Window
.prototype.isOpening = function () {
1710 return this.manager
.isOpening( this );
1714 * Check if window is closing.
1716 * This is a wrapper around OO.ui.WindowManager#isClosing.
1718 * @return {boolean} Window is closing
1720 OO
.ui
.Window
.prototype.isClosing = function () {
1721 return this.manager
.isClosing( this );
1725 * Check if window is opened.
1727 * This is a wrapper around OO.ui.WindowManager#isOpened.
1729 * @return {boolean} Window is opened
1731 OO
.ui
.Window
.prototype.isOpened = function () {
1732 return this.manager
.isOpened( this );
1736 * Get the window manager.
1738 * @return {OO.ui.WindowManager} Manager of window
1740 OO
.ui
.Window
.prototype.getManager = function () {
1741 return this.manager
;
1745 * Get the window size.
1747 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1749 OO
.ui
.Window
.prototype.getSize = function () {
1754 * Get the height of the dialog contents.
1756 * @return {number} Content height
1758 OO
.ui
.Window
.prototype.getContentHeight = function () {
1759 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements
1760 var bodyHeight
, oldHeight
= this.$frame
[0].style
.height
;
1761 this.$frame
[0].style
.height
= '1px';
1762 bodyHeight
= this.getBodyHeight();
1763 this.$frame
[0].style
.height
= oldHeight
;
1766 // Add buffer for border
1767 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1768 // Use combined heights of children
1769 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1774 * Get the height of the dialog contents.
1776 * When this function is called, the dialog will temporarily have been resized
1777 * to height=1px, so .scrollHeight measurements can be taken accurately.
1779 * @return {number} Height of content
1781 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1782 return this.$body
[0].scrollHeight
;
1786 * Get the directionality of the frame
1788 * @return {string} Directionality, 'ltr' or 'rtl'
1790 OO
.ui
.Window
.prototype.getDir = function () {
1795 * Get a process for setting up a window for use.
1797 * Each time the window is opened this process will set it up for use in a particular context, based
1798 * on the `data` argument.
1800 * When you override this method, you can add additional setup steps to the process the parent
1801 * method provides using the 'first' and 'next' methods.
1804 * @param {Object} [data] Window opening data
1805 * @return {OO.ui.Process} Setup process
1807 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1808 return new OO
.ui
.Process();
1812 * Get a process for readying a window for use.
1814 * Each time the window is open and setup, this process will ready it up for use in a particular
1815 * context, based on the `data` argument.
1817 * When you override this method, you can add additional setup steps to the process the parent
1818 * method provides using the 'first' and 'next' methods.
1821 * @param {Object} [data] Window opening data
1822 * @return {OO.ui.Process} Setup process
1824 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1825 return new OO
.ui
.Process();
1829 * Get a process for holding a window from use.
1831 * Each time the window is closed, this process will hold it from use in a particular context, based
1832 * on the `data` argument.
1834 * When you override this method, you can add additional setup steps to the process the parent
1835 * method provides using the 'first' and 'next' methods.
1838 * @param {Object} [data] Window closing data
1839 * @return {OO.ui.Process} Hold process
1841 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1842 return new OO
.ui
.Process();
1846 * Get a process for tearing down a window after use.
1848 * Each time the window is closed this process will tear it down and do something with the user's
1849 * interactions within the window, based on the `data` argument.
1851 * When you override this method, you can add additional teardown steps to the process the parent
1852 * method provides using the 'first' and 'next' methods.
1855 * @param {Object} [data] Window closing data
1856 * @return {OO.ui.Process} Teardown process
1858 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1859 return new OO
.ui
.Process();
1863 * Toggle visibility of window.
1865 * If the window is isolated and hasn't fully loaded yet, the visibility property will be used
1866 * instead of display.
1868 * @param {boolean} [show] Make window visible, omit to toggle visibility
1872 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1873 show
= show
=== undefined ? !this.visible
: !!show
;
1875 if ( show
!== this.isVisible() ) {
1876 this.visible
= show
;
1878 if ( this.isolated
&& !this.isLoaded() ) {
1879 // Hide the window using visibility instead of display until loading is complete
1880 // Can't use display: none; because that prevents the iframe from loading in Firefox
1881 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1883 this.$element
.toggle( show
).css( 'visibility', '' );
1885 this.emit( 'toggle', show
);
1892 * Set the window manager.
1894 * This must be called before initialize. Calling it more than once will cause an error.
1896 * @param {OO.ui.WindowManager} manager Manager for this window
1897 * @throws {Error} If called more than once
1900 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1901 if ( this.manager
) {
1902 throw new Error( 'Cannot set window manager, window already has a manager' );
1906 this.manager
= manager
;
1907 this.isolated
= manager
.shouldIsolate();
1910 if ( this.isolated
) {
1911 this.$iframe
= this.$( '<iframe>' );
1912 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1913 this.$frame
.append( this.$iframe
);
1914 this.$ = function () {
1915 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1917 // WARNING: Do not use this.$ again until #initialize is called
1919 this.$content
= this.$( '<div>' );
1920 this.$document
= $( this.getElementDocument() );
1921 this.$content
.addClass( 'oo-ui-window-content' );
1922 this.$frame
.append( this.$content
);
1924 this.toggle( false );
1926 // Figure out directionality:
1927 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1933 * Set the window size.
1935 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1938 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1940 this.manager
.updateWindowSize( this );
1945 * Set window dimensions.
1947 * Properties are applied to the frame container.
1949 * @param {Object} dim CSS dimension properties
1950 * @param {string|number} [dim.width] Width
1951 * @param {string|number} [dim.minWidth] Minimum width
1952 * @param {string|number} [dim.maxWidth] Maximum width
1953 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1954 * @param {string|number} [dim.minWidth] Minimum height
1955 * @param {string|number} [dim.maxWidth] Maximum height
1958 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1959 // Apply width before height so height is not based on wrapping content using the wrong width
1961 width
: dim
.width
|| '',
1962 minWidth
: dim
.minWidth
|| '',
1963 maxWidth
: dim
.maxWidth
|| ''
1966 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1967 minHeight
: dim
.minHeight
|| '',
1968 maxHeight
: dim
.maxHeight
|| ''
1974 * Initialize window contents.
1976 * The first time the window is opened, #initialize is called when it's safe to begin populating
1977 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1979 * Once this method is called, this.$ can be used to create elements within the frame.
1981 * @throws {Error} If not attached to a manager
1984 OO
.ui
.Window
.prototype.initialize = function () {
1985 if ( !this.manager
) {
1986 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1990 this.$head
= this.$( '<div>' );
1991 this.$body
= this.$( '<div>' );
1992 this.$foot
= this.$( '<div>' );
1993 this.$innerOverlay
= this.$( '<div>' );
1996 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
1999 this.$head
.addClass( 'oo-ui-window-head' );
2000 this.$body
.addClass( 'oo-ui-window-body' );
2001 this.$foot
.addClass( 'oo-ui-window-foot' );
2002 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
2003 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
2011 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
2012 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
2014 * @param {Object} [data] Window opening data
2015 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
2016 * first argument will be a promise which will be resolved when the window begins closing
2018 OO
.ui
.Window
.prototype.open = function ( data
) {
2019 return this.manager
.openWindow( this, data
);
2025 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
2026 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
2028 * @param {Object} [data] Window closing data
2029 * @return {jQuery.Promise} Promise resolved when window is closed
2031 OO
.ui
.Window
.prototype.close = function ( data
) {
2032 return this.manager
.closeWindow( this, data
);
2038 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2041 * @param {Object} [data] Window opening data
2042 * @return {jQuery.Promise} Promise resolved when window is setup
2044 OO
.ui
.Window
.prototype.setup = function ( data
) {
2046 deferred
= $.Deferred();
2048 this.$element
.show();
2049 this.visible
= true;
2050 this.getSetupProcess( data
).execute().done( function () {
2051 // Force redraw by asking the browser to measure the elements' widths
2052 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2053 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2057 return deferred
.promise();
2063 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2066 * @param {Object} [data] Window opening data
2067 * @return {jQuery.Promise} Promise resolved when window is ready
2069 OO
.ui
.Window
.prototype.ready = function ( data
) {
2071 deferred
= $.Deferred();
2073 this.$content
.focus();
2074 this.getReadyProcess( data
).execute().done( function () {
2075 // Force redraw by asking the browser to measure the elements' widths
2076 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2077 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2081 return deferred
.promise();
2087 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2090 * @param {Object} [data] Window closing data
2091 * @return {jQuery.Promise} Promise resolved when window is held
2093 OO
.ui
.Window
.prototype.hold = function ( data
) {
2095 deferred
= $.Deferred();
2097 this.getHoldProcess( data
).execute().done( function () {
2098 // Get the focused element within the window's content
2099 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
2101 // Blur the focused element
2102 if ( $focus
.length
) {
2106 // Force redraw by asking the browser to measure the elements' widths
2107 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2108 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2112 return deferred
.promise();
2118 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2121 * @param {Object} [data] Window closing data
2122 * @return {jQuery.Promise} Promise resolved when window is torn down
2124 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2126 deferred
= $.Deferred();
2128 this.getTeardownProcess( data
).execute().done( function () {
2129 // Force redraw by asking the browser to measure the elements' widths
2130 win
.$element
.removeClass( 'oo-ui-window-load oo-ui-window-setup' ).width();
2131 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2132 win
.$element
.hide();
2133 win
.visible
= false;
2137 return deferred
.promise();
2141 * Load the frame contents.
2143 * Once the iframe's stylesheets are loaded the returned promise will be resolved. Calling while
2144 * loading will return a promise but not trigger a new loading cycle. Calling after loading is
2145 * complete will return a promise that's already been resolved.
2147 * Sounds simple right? Read on...
2149 * When you create a dynamic iframe using open/write/close, the window.load event for the
2150 * iframe is triggered when you call close, and there's no further load event to indicate that
2151 * everything is actually loaded.
2153 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2154 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2155 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2156 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2157 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2159 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2160 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2161 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2162 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2163 * accessing .cssRules is allowed.
2165 * However, now that we control the styles we're injecting, we might as well do away with
2166 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2167 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2168 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2169 * font-family rule is not applied until after the `@import` finishes.
2171 * All this stylesheet injection and polling magic is in #transplantStyles.
2173 * @return {jQuery.Promise} Promise resolved when loading is complete
2175 OO
.ui
.Window
.prototype.load = function () {
2176 var sub
, doc
, loading
,
2179 this.$element
.addClass( 'oo-ui-window-load' );
2181 // Non-isolated windows are already "loaded"
2182 if ( !this.loading
&& !this.isolated
) {
2183 this.loading
= $.Deferred().resolve();
2185 // Set initialized state after so sub-classes aren't confused by it being set by calling
2186 // their parent initialize method
2187 this.initialized
= true;
2190 // Return existing promise if already loading or loaded
2191 if ( this.loading
) {
2192 return this.loading
.promise();
2196 loading
= this.loading
= $.Deferred();
2197 sub
= this.$iframe
.prop( 'contentWindow' );
2200 // Initialize contents
2205 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2206 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2207 '<div class="oo-ui-window-content"></div>' +
2214 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$iframe
);
2215 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2216 this.$document
= this.$( doc
);
2219 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2220 .always( function () {
2221 // Initialize isolated windows
2223 // Set initialized state after so sub-classes aren't confused by it being set by calling
2224 // their parent initialize method
2225 win
.initialized
= true;
2226 // Undo the visibility: hidden; hack and apply display: none;
2227 // We can do this safely now that the iframe has initialized
2228 // (don't do this from within #initialize because it has to happen
2229 // after the all subclasses have been handled as well).
2230 win
.toggle( win
.isVisible() );
2235 return loading
.promise();
2239 * Base class for all dialogs.
2242 * - Manage the window (open and close, etc.).
2243 * - Store the internal name and display title.
2244 * - A stack to track one or more pending actions.
2245 * - Manage a set of actions that can be performed.
2246 * - Configure and create action widgets.
2249 * - Close the dialog with Escape key.
2250 * - Visually lock the dialog while an action is in
2251 * progress (aka "pending").
2253 * Subclass responsibilities:
2254 * - Display the title somewhere.
2255 * - Add content to the dialog.
2256 * - Provide a UI to close the dialog.
2257 * - Display the action widgets somewhere.
2261 * @extends OO.ui.Window
2262 * @mixins OO.ui.PendingElement
2265 * @param {Object} [config] Configuration options
2267 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2268 // Parent constructor
2269 OO
.ui
.Dialog
.super.call( this, config
);
2271 // Mixin constructors
2272 OO
.ui
.PendingElement
.call( this );
2275 this.actions
= new OO
.ui
.ActionSet();
2276 this.attachedActions
= [];
2277 this.currentAction
= null;
2280 this.actions
.connect( this, {
2281 click
: 'onActionClick',
2282 resize
: 'onActionResize',
2283 change
: 'onActionsChange'
2288 .addClass( 'oo-ui-dialog' )
2289 .attr( 'role', 'dialog' );
2294 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2295 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2297 /* Static Properties */
2300 * Symbolic name of dialog.
2305 * @property {string}
2307 OO
.ui
.Dialog
.static.name
= '';
2315 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2317 OO
.ui
.Dialog
.static.title
= '';
2320 * List of OO.ui.ActionWidget configuration options.
2324 * @property {Object[]}
2326 OO
.ui
.Dialog
.static.actions
= [];
2329 * Close dialog when the escape key is pressed.
2334 * @property {boolean}
2336 OO
.ui
.Dialog
.static.escapable
= true;
2341 * Handle frame document key down events.
2343 * @param {jQuery.Event} e Key down event
2345 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2346 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2353 * Handle action resized events.
2355 * @param {OO.ui.ActionWidget} action Action that was resized
2357 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2358 // Override in subclass
2362 * Handle action click events.
2364 * @param {OO.ui.ActionWidget} action Action that was clicked
2366 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2367 if ( !this.isPending() ) {
2368 this.currentAction
= action
;
2369 this.executeAction( action
.getAction() );
2374 * Handle actions change event.
2376 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2377 this.detachActions();
2378 if ( !this.isClosing() ) {
2379 this.attachActions();
2384 * Get set of actions.
2386 * @return {OO.ui.ActionSet}
2388 OO
.ui
.Dialog
.prototype.getActions = function () {
2389 return this.actions
;
2393 * Get a process for taking action.
2395 * When you override this method, you can add additional accept steps to the process the parent
2396 * method provides using the 'first' and 'next' methods.
2399 * @param {string} [action] Symbolic name of action
2400 * @return {OO.ui.Process} Action process
2402 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2403 return new OO
.ui
.Process()
2404 .next( function () {
2406 // An empty action always closes the dialog without data, which should always be
2407 // safe and make no changes
2416 * @param {Object} [data] Dialog opening data
2417 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2418 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2419 * action item, omit to use #static-actions
2421 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2425 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2426 .next( function () {
2429 config
= this.constructor.static,
2430 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2432 this.title
.setLabel(
2433 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2435 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2437 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2440 this.actions
.add( items
);
2447 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2449 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2450 .first( function () {
2451 this.actions
.clear();
2452 this.currentAction
= null;
2459 OO
.ui
.Dialog
.prototype.initialize = function () {
2461 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2464 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2467 if ( this.constructor.static.escapable
) {
2468 this.$document
.on( 'keydown', this.onDocumentKeyDown
.bind( this ) );
2472 this.$content
.addClass( 'oo-ui-dialog-content' );
2473 this.setPendingElement( this.$head
);
2477 * Attach action actions.
2479 OO
.ui
.Dialog
.prototype.attachActions = function () {
2480 // Remember the list of potentially attached actions
2481 this.attachedActions
= this.actions
.get();
2485 * Detach action actions.
2489 OO
.ui
.Dialog
.prototype.detachActions = function () {
2492 // Detach all actions that may have been previously attached
2493 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2494 this.attachedActions
[i
].$element
.detach();
2496 this.attachedActions
= [];
2500 * Execute an action.
2502 * @param {string} action Symbolic name of action to execute
2503 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2505 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2507 return this.getActionProcess( action
).execute()
2508 .always( this.popPending
.bind( this ) );
2512 * Collection of windows.
2515 * @extends OO.ui.Element
2516 * @mixins OO.EventEmitter
2518 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2519 * already opening or opened, the current window will be closed without data. Empty closing data
2520 * should always result in the window being closed without causing constructive or destructive
2523 * As a window is opened and closed, it passes through several stages and the manager emits several
2524 * corresponding events.
2526 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2527 * - {@link #event-opening} is emitted with `opening` promise
2528 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2529 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2530 * - `setup` progress notification is emitted from opening promise
2531 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2532 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2533 * - `ready` progress notification is emitted from opening promise
2534 * - `opening` promise is resolved with `opened` promise
2535 * - Window is now open
2537 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2538 * - `opened` promise is resolved with `closing` promise
2539 * - {@link #event-closing} is emitted with `closing` promise
2540 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2541 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2542 * - `hold` progress notification is emitted from opening promise
2543 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2544 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2545 * - `teardown` progress notification is emitted from opening promise
2546 * - Closing promise is resolved
2547 * - Window is now closed
2550 * @param {Object} [config] Configuration options
2551 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2552 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2553 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2555 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2556 // Configuration initialization
2557 config
= config
|| {};
2559 // Parent constructor
2560 OO
.ui
.WindowManager
.super.call( this, config
);
2562 // Mixin constructors
2563 OO
.EventEmitter
.call( this );
2566 this.factory
= config
.factory
;
2567 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2568 this.isolate
= !!config
.isolate
;
2570 this.opening
= null;
2572 this.closing
= null;
2573 this.preparingToOpen
= null;
2574 this.preparingToClose
= null;
2576 this.currentWindow
= null;
2577 this.$ariaHidden
= null;
2578 this.requestedSize
= null;
2579 this.onWindowResizeTimeout
= null;
2580 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2581 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2582 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2583 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2587 .addClass( 'oo-ui-windowManager' )
2588 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2593 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2594 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2599 * Window is opening.
2601 * Fired when the window begins to be opened.
2604 * @param {OO.ui.Window} win Window that's being opened
2605 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2606 * resolved the first argument will be a promise which will be resolved when the window begins
2607 * closing, the second argument will be the opening data; progress notifications will be fired on
2608 * the promise for `setup` and `ready` when those processes are completed respectively.
2609 * @param {Object} data Window opening data
2613 * Window is closing.
2615 * Fired when the window begins to be closed.
2618 * @param {OO.ui.Window} win Window that's being closed
2619 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2620 * is resolved the first argument will be a the closing data; progress notifications will be fired
2621 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2622 * @param {Object} data Window closing data
2626 * Window was resized.
2629 * @param {OO.ui.Window} win Window that was resized
2632 /* Static Properties */
2635 * Map of symbolic size names and CSS properties.
2639 * @property {Object}
2641 OO
.ui
.WindowManager
.static.sizes
= {
2652 // These can be non-numeric because they are never used in calculations
2659 * Symbolic name of default size.
2661 * Default size is used if the window's requested size is not recognized.
2665 * @property {string}
2667 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2672 * Handle window resize events.
2674 * @param {jQuery.Event} e Window resize event
2676 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2677 clearTimeout( this.onWindowResizeTimeout
);
2678 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2682 * Handle window resize events.
2684 * @param {jQuery.Event} e Window resize event
2686 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2687 if ( this.currentWindow
) {
2688 this.updateWindowSize( this.currentWindow
);
2693 * Handle window mouse wheel events.
2695 * @param {jQuery.Event} e Mouse wheel event
2697 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function ( e
) {
2698 // Kill all events in the parent window if the child window is isolated,
2699 // or if the event didn't come from the child window
2700 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2704 * Handle document key down events.
2706 * @param {jQuery.Event} e Key down event
2708 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2709 switch ( e
.which
) {
2710 case OO
.ui
.Keys
.PAGEUP
:
2711 case OO
.ui
.Keys
.PAGEDOWN
:
2712 case OO
.ui
.Keys
.END
:
2713 case OO
.ui
.Keys
.HOME
:
2714 case OO
.ui
.Keys
.LEFT
:
2716 case OO
.ui
.Keys
.RIGHT
:
2717 case OO
.ui
.Keys
.DOWN
:
2718 // Kill all events in the parent window if the child window is isolated,
2719 // or if the event didn't come from the child window
2720 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2725 * Check if window is opening.
2727 * @return {boolean} Window is opening
2729 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2730 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2734 * Check if window is closing.
2736 * @return {boolean} Window is closing
2738 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2739 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2743 * Check if window is opened.
2745 * @return {boolean} Window is opened
2747 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2748 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2752 * Check if window contents should be isolated.
2754 * Window content isolation is done using inline frames.
2756 * @return {boolean} Window contents should be isolated
2758 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2759 return this.isolate
;
2763 * Check if a window is being managed.
2765 * @param {OO.ui.Window} win Window to check
2766 * @return {boolean} Window is being managed
2768 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2771 for ( name
in this.windows
) {
2772 if ( this.windows
[name
] === win
) {
2781 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2783 * @param {OO.ui.Window} win Window being opened
2784 * @param {Object} [data] Window opening data
2785 * @return {number} Milliseconds to wait
2787 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2792 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2794 * @param {OO.ui.Window} win Window being opened
2795 * @param {Object} [data] Window opening data
2796 * @return {number} Milliseconds to wait
2798 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2803 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2805 * @param {OO.ui.Window} win Window being closed
2806 * @param {Object} [data] Window closing data
2807 * @return {number} Milliseconds to wait
2809 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2814 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2816 * @param {OO.ui.Window} win Window being closed
2817 * @param {Object} [data] Window closing data
2818 * @return {number} Milliseconds to wait
2820 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2821 return this.modal
? 250 : 0;
2825 * Get managed window by symbolic name.
2827 * If window is not yet instantiated, it will be instantiated and added automatically.
2829 * @param {string} name Symbolic window name
2830 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2831 * @throws {Error} If the symbolic name is unrecognized by the factory
2832 * @throws {Error} If the symbolic name unrecognized as a managed window
2834 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2835 var deferred
= $.Deferred(),
2836 win
= this.windows
[name
];
2838 if ( !( win
instanceof OO
.ui
.Window
) ) {
2839 if ( this.factory
) {
2840 if ( !this.factory
.lookup( name
) ) {
2841 deferred
.reject( new OO
.ui
.Error(
2842 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2845 win
= this.factory
.create( name
, this, { $: this.$ } );
2846 this.addWindows( [ win
] );
2847 deferred
.resolve( win
);
2850 deferred
.reject( new OO
.ui
.Error(
2851 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2855 deferred
.resolve( win
);
2858 return deferred
.promise();
2862 * Get current window.
2864 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2866 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2867 return this.currentWindow
;
2873 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2874 * @param {Object} [data] Window opening data
2875 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2876 * for more details about the `opening` promise
2879 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2882 opening
= $.Deferred();
2884 // Argument handling
2885 if ( typeof win
=== 'string' ) {
2886 return this.getWindow( win
).then( function ( win
) {
2887 return manager
.openWindow( win
, data
);
2892 if ( !this.hasWindow( win
) ) {
2893 opening
.reject( new OO
.ui
.Error(
2894 'Cannot open window: window is not attached to manager'
2896 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2897 opening
.reject( new OO
.ui
.Error(
2898 'Cannot open window: another window is opening or open'
2903 if ( opening
.state() !== 'rejected' ) {
2904 if ( !win
.getManager() ) {
2905 win
.setManager( this );
2907 preparing
.push( win
.load() );
2909 if ( this.closing
) {
2910 // If a window is currently closing, wait for it to complete
2911 preparing
.push( this.closing
);
2914 this.preparingToOpen
= $.when
.apply( $, preparing
);
2915 // Ensure handlers get called after preparingToOpen is set
2916 this.preparingToOpen
.done( function () {
2917 if ( manager
.modal
) {
2918 manager
.toggleGlobalEvents( true );
2919 manager
.toggleAriaIsolation( true );
2921 manager
.currentWindow
= win
;
2922 manager
.opening
= opening
;
2923 manager
.preparingToOpen
= null;
2924 manager
.emit( 'opening', win
, opening
, data
);
2925 setTimeout( function () {
2926 win
.setup( data
).then( function () {
2927 manager
.updateWindowSize( win
);
2928 manager
.opening
.notify( { state
: 'setup' } );
2929 setTimeout( function () {
2930 win
.ready( data
).then( function () {
2931 manager
.opening
.notify( { state
: 'ready' } );
2932 manager
.opening
= null;
2933 manager
.opened
= $.Deferred();
2934 opening
.resolve( manager
.opened
.promise(), data
);
2936 }, manager
.getReadyDelay() );
2938 }, manager
.getSetupDelay() );
2942 return opening
.promise();
2948 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2949 * @param {Object} [data] Window closing data
2950 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2951 * for more details about the `closing` promise
2952 * @throws {Error} If no window by that name is being managed
2955 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2958 closing
= $.Deferred(),
2961 // Argument handling
2962 if ( typeof win
=== 'string' ) {
2963 win
= this.windows
[win
];
2964 } else if ( !this.hasWindow( win
) ) {
2970 closing
.reject( new OO
.ui
.Error(
2971 'Cannot close window: window is not attached to manager'
2973 } else if ( win
!== this.currentWindow
) {
2974 closing
.reject( new OO
.ui
.Error(
2975 'Cannot close window: window already closed with different data'
2977 } else if ( this.preparingToClose
|| this.closing
) {
2978 closing
.reject( new OO
.ui
.Error(
2979 'Cannot close window: window already closing with different data'
2984 if ( closing
.state() !== 'rejected' ) {
2985 if ( this.opening
) {
2986 // If the window is currently opening, close it when it's done
2987 preparing
.push( this.opening
);
2990 this.preparingToClose
= $.when
.apply( $, preparing
);
2991 // Ensure handlers get called after preparingToClose is set
2992 this.preparingToClose
.done( function () {
2993 manager
.closing
= closing
;
2994 manager
.preparingToClose
= null;
2995 manager
.emit( 'closing', win
, closing
, data
);
2996 opened
= manager
.opened
;
2997 manager
.opened
= null;
2998 opened
.resolve( closing
.promise(), data
);
2999 setTimeout( function () {
3000 win
.hold( data
).then( function () {
3001 closing
.notify( { state
: 'hold' } );
3002 setTimeout( function () {
3003 win
.teardown( data
).then( function () {
3004 closing
.notify( { state
: 'teardown' } );
3005 if ( manager
.modal
) {
3006 manager
.toggleGlobalEvents( false );
3007 manager
.toggleAriaIsolation( false );
3009 manager
.closing
= null;
3010 manager
.currentWindow
= null;
3011 closing
.resolve( data
);
3013 }, manager
.getTeardownDelay() );
3015 }, manager
.getHoldDelay() );
3019 return closing
.promise();
3025 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
3026 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
3027 * a statically configured symbolic name
3029 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
3030 var i
, len
, win
, name
, list
;
3032 if ( $.isArray( windows
) ) {
3033 // Convert to map of windows by looking up symbolic names from static configuration
3035 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
3036 name
= windows
[i
].constructor.static.name
;
3037 if ( typeof name
!== 'string' ) {
3038 throw new Error( 'Cannot add window' );
3040 list
[name
] = windows
[i
];
3042 } else if ( $.isPlainObject( windows
) ) {
3047 for ( name
in list
) {
3049 this.windows
[name
] = win
;
3050 this.$element
.append( win
.$element
);
3057 * Windows will be closed before they are removed.
3059 * @param {string} name Symbolic name of window to remove
3060 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3061 * @throws {Error} If windows being removed are not being managed
3063 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3064 var i
, len
, win
, name
,
3067 cleanup = function ( name
, win
) {
3068 delete manager
.windows
[name
];
3069 win
.$element
.detach();
3072 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3074 win
= this.windows
[name
];
3076 throw new Error( 'Cannot remove window' );
3078 promises
.push( this.closeWindow( name
).then( cleanup
.bind( null, name
, win
) ) );
3081 return $.when
.apply( $, promises
);
3085 * Remove all windows.
3087 * Windows will be closed before they are removed.
3089 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3091 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3092 return this.removeWindows( Object
.keys( this.windows
) );
3098 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3102 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3103 // Bypass for non-current, and thus invisible, windows
3104 if ( win
!== this.currentWindow
) {
3108 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
3109 sizes
= this.constructor.static.sizes
,
3110 size
= win
.getSize();
3112 if ( !sizes
[size
] ) {
3113 size
= this.constructor.static.defaultSize
;
3115 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
3119 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3120 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3121 win
.setDimensions( sizes
[size
] );
3123 this.emit( 'resize', win
);
3129 * Bind or unbind global events for scrolling.
3131 * @param {boolean} [on] Bind global events
3134 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3135 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3138 if ( !this.globalEvents
) {
3139 this.$( this.getElementDocument() ).on( {
3140 // Prevent scrolling by keys in top-level window
3141 keydown
: this.onDocumentKeyDownHandler
3143 this.$( this.getElementWindow() ).on( {
3144 // Prevent scrolling by wheel in top-level window
3145 mousewheel
: this.onWindowMouseWheelHandler
,
3146 // Start listening for top-level window dimension changes
3147 'orientationchange resize': this.onWindowResizeHandler
3149 this.globalEvents
= true;
3151 } else if ( this.globalEvents
) {
3152 // Unbind global events
3153 this.$( this.getElementDocument() ).off( {
3154 // Allow scrolling by keys in top-level window
3155 keydown
: this.onDocumentKeyDownHandler
3157 this.$( this.getElementWindow() ).off( {
3158 // Allow scrolling by wheel in top-level window
3159 mousewheel
: this.onWindowMouseWheelHandler
,
3160 // Stop listening for top-level window dimension changes
3161 'orientationchange resize': this.onWindowResizeHandler
3163 this.globalEvents
= false;
3170 * Toggle screen reader visibility of content other than the window manager.
3172 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3175 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3176 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3179 if ( !this.$ariaHidden
) {
3180 // Hide everything other than the window manager from screen readers
3181 this.$ariaHidden
= $( 'body' )
3183 .not( this.$element
.parentsUntil( 'body' ).last() )
3184 .attr( 'aria-hidden', '' );
3186 } else if ( this.$ariaHidden
) {
3187 // Restore screen reader visibility
3188 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3189 this.$ariaHidden
= null;
3196 * Destroy window manager.
3198 * Windows will not be closed, only removed from the DOM.
3200 OO
.ui
.WindowManager
.prototype.destroy = function () {
3201 this.toggleGlobalEvents( false );
3202 this.toggleAriaIsolation( false );
3203 this.$element
.remove();
3211 * @param {string|jQuery} message Description of error
3212 * @param {Object} [config] Configuration options
3213 * @cfg {boolean} [recoverable=true] Error is recoverable
3214 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3216 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3217 // Configuration initialization
3218 config
= config
|| {};
3221 this.message
= message
instanceof jQuery
? message
: String( message
);
3222 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3223 this.warning
= !!config
.warning
;
3228 OO
.initClass( OO
.ui
.Error
);
3233 * Check if error can be recovered from.
3235 * @return {boolean} Error is recoverable
3237 OO
.ui
.Error
.prototype.isRecoverable = function () {
3238 return this.recoverable
;
3242 * Check if the error is a warning
3244 * @return {boolean} Error is warning
3246 OO
.ui
.Error
.prototype.isWarning = function () {
3247 return this.warning
;
3251 * Get error message as DOM nodes.
3253 * @return {jQuery} Error message in DOM nodes
3255 OO
.ui
.Error
.prototype.getMessage = function () {
3256 return this.message
instanceof jQuery
?
3257 this.message
.clone() :
3258 $( '<div>' ).text( this.message
).contents();
3262 * Get error message as text.
3264 * @return {string} Error message
3266 OO
.ui
.Error
.prototype.getMessageText = function () {
3267 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3271 * A list of functions, called in sequence.
3273 * If a function added to a process returns boolean false the process will stop; if it returns an
3274 * object with a `promise` method the process will use the promise to either continue to the next
3275 * step when the promise is resolved or stop when the promise is rejected.
3280 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3281 * call, see #createStep for more information
3282 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3284 * @return {Object} Step object, with `callback` and `context` properties
3286 OO
.ui
.Process = function ( step
, context
) {
3291 if ( step
!== undefined ) {
3292 this.next( step
, context
);
3298 OO
.initClass( OO
.ui
.Process
);
3303 * Start the process.
3305 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3306 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3307 * process, the remaining steps will not be taken
3309 OO
.ui
.Process
.prototype.execute = function () {
3310 var i
, len
, promise
;
3313 * Continue execution.
3316 * @param {Array} step A function and the context it should be called in
3317 * @return {Function} Function that continues the process
3319 function proceed( step
) {
3320 return function () {
3321 // Execute step in the correct context
3323 result
= step
.callback
.call( step
.context
);
3325 if ( result
=== false ) {
3326 // Use rejected promise for boolean false results
3327 return $.Deferred().reject( [] ).promise();
3329 if ( typeof result
=== 'number' ) {
3331 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3333 // Use a delayed promise for numbers, expecting them to be in milliseconds
3334 deferred
= $.Deferred();
3335 setTimeout( deferred
.resolve
, result
);
3336 return deferred
.promise();
3338 if ( result
instanceof OO
.ui
.Error
) {
3339 // Use rejected promise for error
3340 return $.Deferred().reject( [ result
] ).promise();
3342 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3343 // Use rejected promise for list of errors
3344 return $.Deferred().reject( result
).promise();
3346 // Duck-type the object to see if it can produce a promise
3347 if ( result
&& $.isFunction( result
.promise
) ) {
3348 // Use a promise generated from the result
3349 return result
.promise();
3351 // Use resolved promise for other results
3352 return $.Deferred().resolve().promise();
3356 if ( this.steps
.length
) {
3357 // Generate a chain reaction of promises
3358 promise
= proceed( this.steps
[0] )();
3359 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3360 promise
= promise
.then( proceed( this.steps
[i
] ) );
3363 promise
= $.Deferred().resolve().promise();
3370 * Create a process step.
3373 * @param {number|jQuery.Promise|Function} step
3375 * - Number of milliseconds to wait; or
3376 * - Promise to wait to be resolved; or
3377 * - Function to execute
3378 * - If it returns boolean false the process will stop
3379 * - If it returns an object with a `promise` method the process will use the promise to either
3380 * continue to the next step when the promise is resolved or stop when the promise is rejected
3381 * - If it returns a number, the process will wait for that number of milliseconds before
3383 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3385 * @return {Object} Step object, with `callback` and `context` properties
3387 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3388 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3390 callback: function () {
3396 if ( $.isFunction( step
) ) {
3402 throw new Error( 'Cannot create process step: number, promise or function expected' );
3406 * Add step to the beginning of the process.
3408 * @inheritdoc #createStep
3409 * @return {OO.ui.Process} this
3412 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3413 this.steps
.unshift( this.createStep( step
, context
) );
3418 * Add step to the end of the process.
3420 * @inheritdoc #createStep
3421 * @return {OO.ui.Process} this
3424 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3425 this.steps
.push( this.createStep( step
, context
) );
3430 * Factory for tools.
3433 * @extends OO.Factory
3436 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3437 // Parent constructor
3438 OO
.ui
.ToolFactory
.super.call( this );
3443 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3448 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3449 var i
, len
, included
, promoted
, demoted
,
3453 // Collect included and not excluded tools
3454 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3457 promoted
= this.extract( promote
, used
);
3458 demoted
= this.extract( demote
, used
);
3461 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3462 if ( !used
[included
[i
]] ) {
3463 auto
.push( included
[i
] );
3467 return promoted
.concat( auto
).concat( demoted
);
3471 * Get a flat list of names from a list of names or groups.
3473 * Tools can be specified in the following ways:
3475 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3476 * - All tools in a group: `{ group: 'group-name' }`
3477 * - All tools: `'*'`
3480 * @param {Array|string} collection List of tools
3481 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3482 * names will be added as properties
3483 * @return {string[]} List of extracted names
3485 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3486 var i
, len
, item
, name
, tool
,
3489 if ( collection
=== '*' ) {
3490 for ( name
in this.registry
) {
3491 tool
= this.registry
[name
];
3493 // Only add tools by group name when auto-add is enabled
3494 tool
.static.autoAddToCatchall
&&
3495 // Exclude already used tools
3496 ( !used
|| !used
[name
] )
3504 } else if ( $.isArray( collection
) ) {
3505 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3506 item
= collection
[i
];
3507 // Allow plain strings as shorthand for named tools
3508 if ( typeof item
=== 'string' ) {
3509 item
= { name
: item
};
3511 if ( OO
.isPlainObject( item
) ) {
3513 for ( name
in this.registry
) {
3514 tool
= this.registry
[name
];
3516 // Include tools with matching group
3517 tool
.static.group
=== item
.group
&&
3518 // Only add tools by group name when auto-add is enabled
3519 tool
.static.autoAddToGroup
&&
3520 // Exclude already used tools
3521 ( !used
|| !used
[name
] )
3529 // Include tools with matching name and exclude already used tools
3530 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3531 names
.push( item
.name
);
3533 used
[item
.name
] = true;
3543 * Factory for tool groups.
3546 * @extends OO.Factory
3549 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3550 // Parent constructor
3551 OO
.Factory
.call( this );
3554 defaultClasses
= this.constructor.static.getDefaultClasses();
3556 // Register default toolgroups
3557 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3558 this.register( defaultClasses
[i
] );
3564 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3566 /* Static Methods */
3569 * Get a default set of classes to be registered on construction
3571 * @return {Function[]} Default classes
3573 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3576 OO
.ui
.ListToolGroup
,
3588 * @param {Object} [config] Configuration options
3590 OO
.ui
.Theme
= function OoUiTheme( config
) {
3591 // Configuration initialization
3592 config
= config
|| {};
3597 OO
.initClass( OO
.ui
.Theme
);
3602 * Get a list of classes to be applied to a widget.
3604 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3605 * otherwise state transitions will not work properly.
3607 * @param {OO.ui.Element} element Element for which to get classes
3608 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3610 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3611 return { on
: [], off
: [] };
3615 * Update CSS classes provided by the theme.
3617 * For elements with theme logic hooks, this should be called any time there's a state change.
3619 * @param {OO.ui.Element} element Element for which to update classes
3620 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3622 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3623 var classes
= this.getElementClasses( element
);
3626 .removeClass( classes
.off
.join( ' ' ) )
3627 .addClass( classes
.on
.join( ' ' ) );
3631 * Element with a button.
3633 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3634 * and access keys for accessibility purposes.
3640 * @param {Object} [config] Configuration options
3641 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3642 * @cfg {boolean} [framed=true] Render button with a frame
3643 * @cfg {number} [tabIndex=0] Button's tab index. Use 0 to use default ordering, use -1 to prevent
3645 * @cfg {string} [accessKey] Button's access key
3647 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3648 // Configuration initialization
3649 config
= config
|| {};
3652 this.$button
= null;
3654 this.tabIndex
= null;
3655 this.accessKey
= null;
3656 this.active
= false;
3657 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3658 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3661 this.$element
.addClass( 'oo-ui-buttonElement' );
3662 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3663 this.setTabIndex( config
.tabIndex
|| 0 );
3664 this.setAccessKey( config
.accessKey
);
3665 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3670 OO
.initClass( OO
.ui
.ButtonElement
);
3672 /* Static Properties */
3675 * Cancel mouse down events.
3679 * @property {boolean}
3681 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3686 * Set the button element.
3688 * If an element is already set, it will be cleaned up before setting up the new element.
3690 * @param {jQuery} $button Element to use as button
3692 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3693 if ( this.$button
) {
3695 .removeClass( 'oo-ui-buttonElement-button' )
3696 .removeAttr( 'role accesskey tabindex' )
3697 .off( this.onMouseDownHandler
);
3700 this.$button
= $button
3701 .addClass( 'oo-ui-buttonElement-button' )
3702 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3703 .on( 'mousedown', this.onMouseDownHandler
);
3707 * Handles mouse down events.
3709 * @param {jQuery.Event} e Mouse down event
3711 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3712 if ( this.isDisabled() || e
.which
!== 1 ) {
3715 // Remove the tab-index while the button is down to prevent the button from stealing focus
3716 this.$button
.removeAttr( 'tabindex' );
3717 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3718 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3719 // reliably reapply the tabindex and remove the pressed class
3720 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3721 // Prevent change of focus unless specifically configured otherwise
3722 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3728 * Handles mouse up events.
3730 * @param {jQuery.Event} e Mouse up event
3732 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3733 if ( this.isDisabled() || e
.which
!== 1 ) {
3736 // Restore the tab-index after the button is up to restore the button's accessibility
3737 this.$button
.attr( 'tabindex', this.tabIndex
);
3738 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3739 // Stop listening for mouseup, since we only needed this once
3740 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3744 * Check if button has a frame.
3746 * @return {boolean} Button is framed
3748 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3755 * @param {boolean} [framed] Make button framed, omit to toggle
3758 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3759 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3760 if ( framed
!== this.framed
) {
3761 this.framed
= framed
;
3763 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3764 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3765 this.updateThemeClasses();
3774 * @param {number|null} tabIndex Button's tab index, use null to remove
3777 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3778 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3780 if ( this.tabIndex
!== tabIndex
) {
3781 if ( this.$button
) {
3782 if ( tabIndex
!== null ) {
3783 this.$button
.attr( 'tabindex', tabIndex
);
3785 this.$button
.removeAttr( 'tabindex' );
3788 this.tabIndex
= tabIndex
;
3797 * @param {string} accessKey Button's access key, use empty string to remove
3800 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3801 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3803 if ( this.accessKey
!== accessKey
) {
3804 if ( this.$button
) {
3805 if ( accessKey
!== null ) {
3806 this.$button
.attr( 'accesskey', accessKey
);
3808 this.$button
.removeAttr( 'accesskey' );
3811 this.accessKey
= accessKey
;
3820 * @param {boolean} [value] Make button active
3823 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3824 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3829 * Element containing a sequence of child elements.
3835 * @param {Object} [config] Configuration options
3836 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3838 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3839 // Configuration initialization
3840 config
= config
|| {};
3845 this.aggregateItemEvents
= {};
3848 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3854 * Set the group element.
3856 * If an element is already set, items will be moved to the new element.
3858 * @param {jQuery} $group Element to use as group
3860 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3863 this.$group
= $group
;
3864 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3865 this.$group
.append( this.items
[i
].$element
);
3870 * Check if there are no items.
3872 * @return {boolean} Group is empty
3874 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3875 return !this.items
.length
;
3881 * @return {OO.ui.Element[]} Items
3883 OO
.ui
.GroupElement
.prototype.getItems = function () {
3884 return this.items
.slice( 0 );
3888 * Get an item by its data.
3890 * Data is compared by a hash of its value. Only the first item with matching data will be returned.
3892 * @param {Object} data Item data to search for
3893 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
3895 OO
.ui
.GroupElement
.prototype.getItemFromData = function ( data
) {
3897 hash
= OO
.getHash( data
);
3899 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3900 item
= this.items
[i
];
3901 if ( hash
=== OO
.getHash( item
.getData() ) ) {
3910 * Get items by their data.
3912 * Data is compared by a hash of its value. All items with matching data will be returned.
3914 * @param {Object} data Item data to search for
3915 * @return {OO.ui.Element[]} Items with equivalent data
3917 OO
.ui
.GroupElement
.prototype.getItemsFromData = function ( data
) {
3919 hash
= OO
.getHash( data
),
3922 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3923 item
= this.items
[i
];
3924 if ( hash
=== OO
.getHash( item
.getData() ) ) {
3933 * Add an aggregate item event.
3935 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3936 * and with an additional leading parameter containing the item that emitted the original event.
3937 * Other arguments that were emitted from the original event are passed through.
3939 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3940 * event, use null value to remove aggregation
3941 * @throws {Error} If aggregation already exists
3943 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3944 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3946 for ( itemEvent
in events
) {
3947 groupEvent
= events
[itemEvent
];
3949 // Remove existing aggregated event
3950 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3951 // Don't allow duplicate aggregations
3953 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3955 // Remove event aggregation from existing items
3956 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3957 item
= this.items
[i
];
3958 if ( item
.connect
&& item
.disconnect
) {
3960 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3961 item
.disconnect( this, remove
);
3964 // Prevent future items from aggregating event
3965 delete this.aggregateItemEvents
[itemEvent
];
3968 // Add new aggregate event
3970 // Make future items aggregate event
3971 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3972 // Add event aggregation to existing items
3973 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3974 item
= this.items
[i
];
3975 if ( item
.connect
&& item
.disconnect
) {
3977 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3978 item
.connect( this, add
);
3988 * Adding an existing item will move it.
3990 * @param {OO.ui.Element[]} items Items
3991 * @param {number} [index] Index to insert items at
3994 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3995 var i
, len
, item
, event
, events
, currentIndex
,
3998 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4001 // Check if item exists then remove it first, effectively "moving" it
4002 currentIndex
= $.inArray( item
, this.items
);
4003 if ( currentIndex
>= 0 ) {
4004 this.removeItems( [ item
] );
4005 // Adjust index to compensate for removal
4006 if ( currentIndex
< index
) {
4011 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
4013 for ( event
in this.aggregateItemEvents
) {
4014 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
4016 item
.connect( this, events
);
4018 item
.setElementGroup( this );
4019 itemElements
.push( item
.$element
.get( 0 ) );
4022 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
4023 this.$group
.append( itemElements
);
4024 this.items
.push
.apply( this.items
, items
);
4025 } else if ( index
=== 0 ) {
4026 this.$group
.prepend( itemElements
);
4027 this.items
.unshift
.apply( this.items
, items
);
4029 this.items
[index
].$element
.before( itemElements
);
4030 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
4039 * Items will be detached, not removed, so they can be used later.
4041 * @param {OO.ui.Element[]} items Items to remove
4044 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
4045 var i
, len
, item
, index
, remove
, itemEvent
;
4047 // Remove specific items
4048 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4050 index
= $.inArray( item
, this.items
);
4051 if ( index
!== -1 ) {
4053 item
.connect
&& item
.disconnect
&&
4054 !$.isEmptyObject( this.aggregateItemEvents
)
4057 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4058 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4060 item
.disconnect( this, remove
);
4062 item
.setElementGroup( null );
4063 this.items
.splice( index
, 1 );
4064 item
.$element
.detach();
4074 * Items will be detached, not removed, so they can be used later.
4078 OO
.ui
.GroupElement
.prototype.clearItems = function () {
4079 var i
, len
, item
, remove
, itemEvent
;
4082 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4083 item
= this.items
[i
];
4085 item
.connect
&& item
.disconnect
&&
4086 !$.isEmptyObject( this.aggregateItemEvents
)
4089 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4090 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4092 item
.disconnect( this, remove
);
4094 item
.setElementGroup( null );
4095 item
.$element
.detach();
4103 * Element containing an icon.
4105 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4106 * a control or convey information in a more space efficient way. Icons should rarely be used
4107 * without labels; such as in a toolbar where space is at a premium or within a context where the
4108 * meaning is very clear to the user.
4114 * @param {Object} [config] Configuration options
4115 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4116 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4117 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4119 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4121 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4122 // Configuration initialization
4123 config
= config
|| {};
4128 this.iconTitle
= null;
4131 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4132 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4133 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4138 OO
.initClass( OO
.ui
.IconElement
);
4140 /* Static Properties */
4145 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4147 * For i18n purposes, this property can be an object containing a `default` icon name property and
4148 * additional icon names keyed by language code.
4150 * Example of i18n icon definition:
4151 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4155 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4156 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4159 OO
.ui
.IconElement
.static.icon
= null;
4166 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4169 OO
.ui
.IconElement
.static.iconTitle
= null;
4174 * Set the icon element.
4176 * If an element is already set, it will be cleaned up before setting up the new element.
4178 * @param {jQuery} $icon Element to use as icon
4180 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4183 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4184 .removeAttr( 'title' );
4188 .addClass( 'oo-ui-iconElement-icon' )
4189 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4190 if ( this.iconTitle
!== null ) {
4191 this.$icon
.attr( 'title', this.iconTitle
);
4198 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4199 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4200 * language, use null to remove icon
4203 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4204 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4205 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4207 if ( this.icon
!== icon
) {
4209 if ( this.icon
!== null ) {
4210 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4212 if ( icon
!== null ) {
4213 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4219 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4220 this.updateThemeClasses();
4228 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4232 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4233 iconTitle
= typeof iconTitle
=== 'function' ||
4234 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4235 OO
.ui
.resolveMsg( iconTitle
) : null;
4237 if ( this.iconTitle
!== iconTitle
) {
4238 this.iconTitle
= iconTitle
;
4240 if ( this.iconTitle
!== null ) {
4241 this.$icon
.attr( 'title', iconTitle
);
4243 this.$icon
.removeAttr( 'title' );
4254 * @return {string} Icon name
4256 OO
.ui
.IconElement
.prototype.getIcon = function () {
4261 * Element containing an indicator.
4263 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4264 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4265 * instead of performing an action directly, or an item in a list which has errors that need to be
4272 * @param {Object} [config] Configuration options
4273 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4275 * @cfg {string} [indicator] Symbolic indicator name
4276 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4278 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4279 // Configuration initialization
4280 config
= config
|| {};
4283 this.$indicator
= null;
4284 this.indicator
= null;
4285 this.indicatorTitle
= null;
4288 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4289 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4290 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4295 OO
.initClass( OO
.ui
.IndicatorElement
);
4297 /* Static Properties */
4304 * @property {string|null} Symbolic indicator name
4306 OO
.ui
.IndicatorElement
.static.indicator
= null;
4313 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4316 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4321 * Set the indicator element.
4323 * If an element is already set, it will be cleaned up before setting up the new element.
4325 * @param {jQuery} $indicator Element to use as indicator
4327 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4328 if ( this.$indicator
) {
4330 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4331 .removeAttr( 'title' );
4334 this.$indicator
= $indicator
4335 .addClass( 'oo-ui-indicatorElement-indicator' )
4336 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4337 if ( this.indicatorTitle
!== null ) {
4338 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4343 * Set indicator name.
4345 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4348 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4349 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4351 if ( this.indicator
!== indicator
) {
4352 if ( this.$indicator
) {
4353 if ( this.indicator
!== null ) {
4354 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4356 if ( indicator
!== null ) {
4357 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4360 this.indicator
= indicator
;
4363 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4364 this.updateThemeClasses();
4370 * Set indicator title.
4372 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4373 * null for no indicator title
4376 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4377 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4378 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4379 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4381 if ( this.indicatorTitle
!== indicatorTitle
) {
4382 this.indicatorTitle
= indicatorTitle
;
4383 if ( this.$indicator
) {
4384 if ( this.indicatorTitle
!== null ) {
4385 this.$indicator
.attr( 'title', indicatorTitle
);
4387 this.$indicator
.removeAttr( 'title' );
4396 * Get indicator name.
4398 * @return {string} Symbolic name of indicator
4400 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4401 return this.indicator
;
4405 * Get indicator title.
4407 * @return {string} Indicator title text
4409 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4410 return this.indicatorTitle
;
4414 * Element containing a label.
4420 * @param {Object} [config] Configuration options
4421 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4422 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4423 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4425 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4426 // Configuration initialization
4427 config
= config
|| {};
4432 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4435 this.setLabel( config
.label
|| this.constructor.static.label
);
4436 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4441 OO
.initClass( OO
.ui
.LabelElement
);
4443 /* Static Properties */
4450 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4453 OO
.ui
.LabelElement
.static.label
= null;
4458 * Set the label element.
4460 * If an element is already set, it will be cleaned up before setting up the new element.
4462 * @param {jQuery} $label Element to use as label
4464 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4465 if ( this.$label
) {
4466 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4469 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4470 this.setLabelContent( this.label
);
4476 * An empty string will result in the label being hidden. A string containing only whitespace will
4477 * be converted to a single ` `.
4479 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4480 * text; or null for no label
4483 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4484 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4485 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4487 if ( this.label
!== label
) {
4488 if ( this.$label
) {
4489 this.setLabelContent( label
);
4494 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4502 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
4503 * text; or null for no label
4505 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4514 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4515 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4516 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4523 * Set the content of the label.
4525 * Do not call this method until after the label element has been set by #setLabelElement.
4528 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4529 * text; or null for no label
4531 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4532 if ( typeof label
=== 'string' ) {
4533 if ( label
.match( /^\s*$/ ) ) {
4534 // Convert whitespace only string to a single non-breaking space
4535 this.$label
.html( ' ' );
4537 this.$label
.text( label
);
4539 } else if ( label
instanceof jQuery
) {
4540 this.$label
.empty().append( label
);
4542 this.$label
.empty();
4544 this.$label
.css( 'display', !label
? 'none' : '' );
4548 * Element containing an OO.ui.PopupWidget object.
4554 * @param {Object} [config] Configuration options
4555 * @cfg {Object} [popup] Configuration to pass to popup
4556 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4558 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4559 // Configuration initialization
4560 config
= config
|| {};
4563 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4564 { autoClose
: true },
4566 { $: this.$, $autoCloseIgnore
: this.$element
}
4575 * @return {OO.ui.PopupWidget} Popup widget
4577 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4582 * Element with named flags that can be added, removed, listed and checked.
4584 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4585 * the flag name. Flags are primarily useful for styling.
4591 * @param {Object} [config] Configuration options
4592 * @cfg {string|string[]} [flags] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4593 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4595 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4596 // Configuration initialization
4597 config
= config
|| {};
4601 this.$flagged
= null;
4604 this.setFlags( config
.flags
);
4605 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4612 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4613 * added/removed properties
4619 * Set the flagged element.
4621 * If an element is already set, it will be cleaned up before setting up the new element.
4623 * @param {jQuery} $flagged Element to add flags to
4625 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4626 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4627 return 'oo-ui-flaggedElement-' + flag
;
4630 if ( this.$flagged
) {
4631 this.$flagged
.removeClass( classNames
);
4634 this.$flagged
= $flagged
.addClass( classNames
);
4638 * Check if a flag is set.
4640 * @param {string} flag Name of flag
4641 * @return {boolean} Has flag
4643 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4644 return flag
in this.flags
;
4648 * Get the names of all flags set.
4650 * @return {string[]} Flag names
4652 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4653 return Object
.keys( this.flags
);
4662 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4663 var flag
, className
,
4666 classPrefix
= 'oo-ui-flaggedElement-';
4668 for ( flag
in this.flags
) {
4669 className
= classPrefix
+ flag
;
4670 changes
[flag
] = false;
4671 delete this.flags
[flag
];
4672 remove
.push( className
);
4675 if ( this.$flagged
) {
4676 this.$flagged
.removeClass( remove
.join( ' ' ) );
4679 this.updateThemeClasses();
4680 this.emit( 'flag', changes
);
4686 * Add one or more flags.
4688 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4689 * keyed by flag name containing boolean set/remove instructions.
4693 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4694 var i
, len
, flag
, className
,
4698 classPrefix
= 'oo-ui-flaggedElement-';
4700 if ( typeof flags
=== 'string' ) {
4701 className
= classPrefix
+ flags
;
4703 if ( !this.flags
[flags
] ) {
4704 this.flags
[flags
] = true;
4705 add
.push( className
);
4707 } else if ( $.isArray( flags
) ) {
4708 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4710 className
= classPrefix
+ flag
;
4712 if ( !this.flags
[flag
] ) {
4713 changes
[flag
] = true;
4714 this.flags
[flag
] = true;
4715 add
.push( className
);
4718 } else if ( OO
.isPlainObject( flags
) ) {
4719 for ( flag
in flags
) {
4720 className
= classPrefix
+ flag
;
4721 if ( flags
[flag
] ) {
4723 if ( !this.flags
[flag
] ) {
4724 changes
[flag
] = true;
4725 this.flags
[flag
] = true;
4726 add
.push( className
);
4730 if ( this.flags
[flag
] ) {
4731 changes
[flag
] = false;
4732 delete this.flags
[flag
];
4733 remove
.push( className
);
4739 if ( this.$flagged
) {
4741 .addClass( add
.join( ' ' ) )
4742 .removeClass( remove
.join( ' ' ) );
4745 this.updateThemeClasses();
4746 this.emit( 'flag', changes
);
4752 * Element with a title.
4754 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4755 * not visible on touch devices.
4761 * @param {Object} [config] Configuration options
4762 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4763 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
4764 * static property 'title' is used.
4766 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4767 // Configuration initialization
4768 config
= config
|| {};
4771 this.$titled
= null;
4775 this.setTitle( config
.title
|| this.constructor.static.title
);
4776 this.setTitledElement( config
.$titled
|| this.$element
);
4781 OO
.initClass( OO
.ui
.TitledElement
);
4783 /* Static Properties */
4790 * @property {string|Function} Title text or a function that returns text
4792 OO
.ui
.TitledElement
.static.title
= null;
4797 * Set the titled element.
4799 * If an element is already set, it will be cleaned up before setting up the new element.
4801 * @param {jQuery} $titled Element to set title on
4803 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4804 if ( this.$titled
) {
4805 this.$titled
.removeAttr( 'title' );
4808 this.$titled
= $titled
;
4810 this.$titled
.attr( 'title', this.title
);
4817 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4820 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4821 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4823 if ( this.title
!== title
) {
4824 if ( this.$titled
) {
4825 if ( title
!== null ) {
4826 this.$titled
.attr( 'title', title
);
4828 this.$titled
.removeAttr( 'title' );
4840 * @return {string} Title string
4842 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4847 * Element that can be automatically clipped to visible boundaries.
4849 * Whenever the element's natural height changes, you have to call
4850 * #clip to make sure it's still clipping correctly.
4856 * @param {Object} [config] Configuration options
4857 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4859 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4860 // Configuration initialization
4861 config
= config
|| {};
4864 this.$clippable
= null;
4865 this.clipping
= false;
4866 this.clippedHorizontally
= false;
4867 this.clippedVertically
= false;
4868 this.$clippableContainer
= null;
4869 this.$clippableScroller
= null;
4870 this.$clippableWindow
= null;
4871 this.idealWidth
= null;
4872 this.idealHeight
= null;
4873 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
4874 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
4877 this.setClippableElement( config
.$clippable
|| this.$element
);
4883 * Set clippable element.
4885 * If an element is already set, it will be cleaned up before setting up the new element.
4887 * @param {jQuery} $clippable Element to make clippable
4889 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4890 if ( this.$clippable
) {
4891 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4892 this.$clippable
.css( { width
: '', height
: '' } );
4893 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4894 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4897 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4904 * Do not turn clipping on until after the element is attached to the DOM and visible.
4906 * @param {boolean} [clipping] Enable clipping, omit to toggle
4909 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4910 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4912 if ( this.clipping
!== clipping
) {
4913 this.clipping
= clipping
;
4915 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4916 // If the clippable container is the body, we have to listen to scroll events and check
4917 // jQuery.scrollTop on the window because of browser inconsistencies
4918 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4919 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4920 this.$clippableContainer
;
4921 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4922 this.$clippableWindow
= this.$( this.getElementWindow() )
4923 .on( 'resize', this.onClippableWindowResizeHandler
);
4924 // Initial clip after visible
4927 this.$clippable
.css( { width
: '', height
: '' } );
4928 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4929 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4931 this.$clippableContainer
= null;
4932 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4933 this.$clippableScroller
= null;
4934 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4935 this.$clippableWindow
= null;
4943 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4945 * @return {boolean} Element will be clipped to the visible area
4947 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4948 return this.clipping
;
4952 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4954 * @return {boolean} Part of the element is being clipped
4956 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4957 return this.clippedHorizontally
|| this.clippedVertically
;
4961 * Check if the right of the element is being clipped by the nearest scrollable container.
4963 * @return {boolean} Part of the element is being clipped
4965 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4966 return this.clippedHorizontally
;
4970 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4972 * @return {boolean} Part of the element is being clipped
4974 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4975 return this.clippedVertically
;
4979 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
4981 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4982 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4984 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4985 this.idealWidth
= width
;
4986 this.idealHeight
= height
;
4988 if ( !this.clipping
) {
4989 // Update dimensions
4990 this.$clippable
.css( { width
: width
, height
: height
} );
4992 // While clipping, idealWidth and idealHeight are not considered
4996 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4997 * the element's natural height changes.
4999 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
5000 * overlapped by, the visible area of the nearest scrollable container.
5004 OO
.ui
.ClippableElement
.prototype.clip = function () {
5005 if ( !this.clipping
) {
5006 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
5011 cOffset
= this.$clippable
.offset(),
5012 $container
= this.$clippableContainer
.is( 'body' ) ?
5013 this.$clippableWindow
: this.$clippableContainer
,
5014 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
5015 ccHeight
= $container
.innerHeight() - buffer
,
5016 ccWidth
= $container
.innerWidth() - buffer
,
5017 scrollTop
= this.$clippableScroller
.scrollTop(),
5018 scrollLeft
= this.$clippableScroller
.scrollLeft(),
5019 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
5020 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
5021 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
5022 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
5023 clipWidth
= desiredWidth
< naturalWidth
,
5024 clipHeight
= desiredHeight
< naturalHeight
;
5027 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
5029 this.$clippable
.css( 'width', this.idealWidth
|| '' );
5030 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5031 this.$clippable
.css( 'overflowX', '' );
5034 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
5036 this.$clippable
.css( 'height', this.idealHeight
|| '' );
5037 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5038 this.$clippable
.css( 'overflowY', '' );
5041 this.clippedHorizontally
= clipWidth
;
5042 this.clippedVertically
= clipHeight
;
5048 * Generic toolbar tool.
5052 * @extends OO.ui.Widget
5053 * @mixins OO.ui.IconElement
5054 * @mixins OO.ui.FlaggedElement
5057 * @param {OO.ui.ToolGroup} toolGroup
5058 * @param {Object} [config] Configuration options
5059 * @cfg {string|Function} [title] Title text or a function that returns text
5061 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
5062 // Configuration initialization
5063 config
= config
|| {};
5065 // Parent constructor
5066 OO
.ui
.Tool
.super.call( this, config
);
5068 // Mixin constructors
5069 OO
.ui
.IconElement
.call( this, config
);
5070 OO
.ui
.FlaggedElement
.call( this, config
);
5073 this.toolGroup
= toolGroup
;
5074 this.toolbar
= this.toolGroup
.getToolbar();
5075 this.active
= false;
5076 this.$title
= this.$( '<span>' );
5077 this.$titleText
= this.$( '<span>' );
5078 this.$accel
= this.$( '<span>' );
5079 this.$link
= this.$( '<a>' );
5083 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
5086 this.$titleText
.addClass( 'oo-ui-tool-title-text' );
5088 .addClass( 'oo-ui-tool-accel' )
5090 // This may need to be changed if the key names are ever localized,
5091 // but for now they are essentially written in English
5096 .addClass( 'oo-ui-tool-title' )
5097 .append( this.$titleText
, this.$accel
);
5099 .addClass( 'oo-ui-tool-link' )
5100 .append( this.$icon
, this.$title
)
5101 .prop( 'tabIndex', 0 )
5102 .attr( 'role', 'button' );
5104 .data( 'oo-ui-tool', this )
5106 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5107 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5109 .append( this.$link
);
5110 this.setTitle( config
.title
|| this.constructor.static.title
);
5115 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5116 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5117 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5125 /* Static Properties */
5131 OO
.ui
.Tool
.static.tagName
= 'span';
5134 * Symbolic name of tool.
5139 * @property {string}
5141 OO
.ui
.Tool
.static.name
= '';
5149 * @property {string}
5151 OO
.ui
.Tool
.static.group
= '';
5156 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
5157 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
5158 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
5159 * appended to the title if the tool is part of a bar tool group.
5164 * @property {string|Function} Title text or a function that returns text
5166 OO
.ui
.Tool
.static.title
= '';
5169 * Tool can be automatically added to catch-all groups.
5173 * @property {boolean}
5175 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
5178 * Tool can be automatically added to named groups.
5181 * @property {boolean}
5184 OO
.ui
.Tool
.static.autoAddToGroup
= true;
5187 * Check if this tool is compatible with given data.
5191 * @param {Mixed} data Data to check
5192 * @return {boolean} Tool can be used with data
5194 OO
.ui
.Tool
.static.isCompatibleWith = function () {
5201 * Handle the toolbar state being updated.
5203 * This is an abstract method that must be overridden in a concrete subclass.
5207 OO
.ui
.Tool
.prototype.onUpdateState = function () {
5209 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
5214 * Handle the tool being selected.
5216 * This is an abstract method that must be overridden in a concrete subclass.
5220 OO
.ui
.Tool
.prototype.onSelect = function () {
5222 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
5227 * Check if the button is active.
5229 * @return {boolean} Button is active
5231 OO
.ui
.Tool
.prototype.isActive = function () {
5236 * Make the button appear active or inactive.
5238 * @param {boolean} state Make button appear active
5240 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
5241 this.active
= !!state
;
5242 if ( this.active
) {
5243 this.$element
.addClass( 'oo-ui-tool-active' );
5245 this.$element
.removeClass( 'oo-ui-tool-active' );
5250 * Get the tool title.
5252 * @param {string|Function} title Title text or a function that returns text
5255 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
5256 this.title
= OO
.ui
.resolveMsg( title
);
5262 * Get the tool title.
5264 * @return {string} Title text
5266 OO
.ui
.Tool
.prototype.getTitle = function () {
5271 * Get the tool's symbolic name.
5273 * @return {string} Symbolic name of tool
5275 OO
.ui
.Tool
.prototype.getName = function () {
5276 return this.constructor.static.name
;
5282 OO
.ui
.Tool
.prototype.updateTitle = function () {
5283 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
5284 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
5285 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
5288 this.$titleText
.text( this.title
);
5289 this.$accel
.text( accel
);
5291 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
5292 tooltipParts
.push( this.title
);
5294 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
5295 tooltipParts
.push( accel
);
5297 if ( tooltipParts
.length
) {
5298 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
5300 this.$link
.removeAttr( 'title' );
5307 OO
.ui
.Tool
.prototype.destroy = function () {
5308 this.toolbar
.disconnect( this );
5309 this.$element
.remove();
5313 * Collection of tool groups.
5316 * @extends OO.ui.Element
5317 * @mixins OO.EventEmitter
5318 * @mixins OO.ui.GroupElement
5321 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5322 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5323 * @param {Object} [config] Configuration options
5324 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5325 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5327 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5328 // Configuration initialization
5329 config
= config
|| {};
5331 // Parent constructor
5332 OO
.ui
.Toolbar
.super.call( this, config
);
5334 // Mixin constructors
5335 OO
.EventEmitter
.call( this );
5336 OO
.ui
.GroupElement
.call( this, config
);
5339 this.toolFactory
= toolFactory
;
5340 this.toolGroupFactory
= toolGroupFactory
;
5343 this.$bar
= this.$( '<div>' );
5344 this.$actions
= this.$( '<div>' );
5345 this.initialized
= false;
5349 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5350 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
5353 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5354 if ( config
.actions
) {
5355 this.$bar
.append( this.$actions
.addClass( 'oo-ui-toolbar-actions' ) );
5358 .addClass( 'oo-ui-toolbar-bar' )
5359 .append( this.$group
, '<div style="clear:both"></div>' );
5360 if ( config
.shadow
) {
5361 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5363 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5368 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5369 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5370 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5375 * Get the tool factory.
5377 * @return {OO.ui.ToolFactory} Tool factory
5379 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5380 return this.toolFactory
;
5384 * Get the tool group factory.
5386 * @return {OO.Factory} Tool group factory
5388 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5389 return this.toolGroupFactory
;
5393 * Handles mouse down events.
5395 * @param {jQuery.Event} e Mouse down event
5397 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5398 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5399 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5400 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5406 * Sets up handles and preloads required information for the toolbar to work.
5407 * This must be called immediately after it is attached to a visible document.
5409 OO
.ui
.Toolbar
.prototype.initialize = function () {
5410 this.initialized
= true;
5416 * Tools can be specified in the following ways:
5418 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5419 * - All tools in a group: `{ group: 'group-name' }`
5420 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5422 * @param {Object.<string,Array>} groups List of tool group configurations
5423 * @param {Array|string} [groups.include] Tools to include
5424 * @param {Array|string} [groups.exclude] Tools to exclude
5425 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5426 * @param {Array|string} [groups.demote] Tools to demote to the end
5428 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5429 var i
, len
, type
, group
,
5431 defaultType
= 'bar';
5433 // Cleanup previous groups
5436 // Build out new groups
5437 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5439 if ( group
.include
=== '*' ) {
5440 // Apply defaults to catch-all groups
5441 if ( group
.type
=== undefined ) {
5442 group
.type
= 'list';
5444 if ( group
.label
=== undefined ) {
5445 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
5448 // Check type has been registered
5449 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5451 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5454 this.addItems( items
);
5458 * Remove all tools and groups from the toolbar.
5460 OO
.ui
.Toolbar
.prototype.reset = function () {
5465 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5466 this.items
[i
].destroy();
5472 * Destroys toolbar, removing event handlers and DOM elements.
5474 * Call this whenever you are done using a toolbar.
5476 OO
.ui
.Toolbar
.prototype.destroy = function () {
5478 this.$element
.remove();
5482 * Check if tool has not been used yet.
5484 * @param {string} name Symbolic name of tool
5485 * @return {boolean} Tool is available
5487 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5488 return !this.tools
[name
];
5492 * Prevent tool from being used again.
5494 * @param {OO.ui.Tool} tool Tool to reserve
5496 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5497 this.tools
[tool
.getName()] = tool
;
5501 * Allow tool to be used again.
5503 * @param {OO.ui.Tool} tool Tool to release
5505 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5506 delete this.tools
[tool
.getName()];
5510 * Get accelerator label for tool.
5512 * This is a stub that should be overridden to provide access to accelerator information.
5514 * @param {string} name Symbolic name of tool
5515 * @return {string|undefined} Tool accelerator label if available
5517 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5522 * Collection of tools.
5524 * Tools can be specified in the following ways:
5526 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5527 * - All tools in a group: `{ group: 'group-name' }`
5528 * - All tools: `'*'`
5532 * @extends OO.ui.Widget
5533 * @mixins OO.ui.GroupElement
5536 * @param {OO.ui.Toolbar} toolbar
5537 * @param {Object} [config] Configuration options
5538 * @cfg {Array|string} [include=[]] List of tools to include
5539 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5540 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5541 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5543 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5544 // Configuration initialization
5545 config
= config
|| {};
5547 // Parent constructor
5548 OO
.ui
.ToolGroup
.super.call( this, config
);
5550 // Mixin constructors
5551 OO
.ui
.GroupElement
.call( this, config
);
5554 this.toolbar
= toolbar
;
5556 this.pressed
= null;
5557 this.autoDisabled
= false;
5558 this.include
= config
.include
|| [];
5559 this.exclude
= config
.exclude
|| [];
5560 this.promote
= config
.promote
|| [];
5561 this.demote
= config
.demote
|| [];
5562 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
5566 'mousedown touchstart': this.onPointerDown
.bind( this ),
5567 'mouseup touchend': this.onPointerUp
.bind( this ),
5568 mouseover
: this.onMouseOver
.bind( this ),
5569 mouseout
: this.onMouseOut
.bind( this )
5571 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5572 this.aggregate( { disable
: 'itemDisable' } );
5573 this.connect( this, { itemDisable
: 'updateDisabled' } );
5576 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5578 .addClass( 'oo-ui-toolGroup' )
5579 .append( this.$group
);
5585 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5586 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5594 /* Static Properties */
5597 * Show labels in tooltips.
5601 * @property {boolean}
5603 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5606 * Show acceleration labels in tooltips.
5610 * @property {boolean}
5612 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5615 * Automatically disable the toolgroup when all tools are disabled
5619 * @property {boolean}
5621 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5628 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5629 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5635 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5636 var i
, item
, allDisabled
= true;
5638 if ( this.constructor.static.autoDisable
) {
5639 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5640 item
= this.items
[i
];
5641 if ( !item
.isDisabled() ) {
5642 allDisabled
= false;
5646 this.autoDisabled
= allDisabled
;
5648 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5652 * Handle mouse down events.
5654 * @param {jQuery.Event} e Mouse down event
5656 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5657 // e.which is 0 for touch events, 1 for left mouse button
5658 if ( !this.isDisabled() && e
.which
<= 1 ) {
5659 this.pressed
= this.getTargetTool( e
);
5660 if ( this.pressed
) {
5661 this.pressed
.setActive( true );
5662 this.getElementDocument().addEventListener(
5663 'mouseup', this.onCapturedMouseUpHandler
, true
5671 * Handle captured mouse up events.
5673 * @param {Event} e Mouse up event
5675 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5676 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5677 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5678 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5679 this.onPointerUp( e
);
5683 * Handle mouse up events.
5685 * @param {jQuery.Event} e Mouse up event
5687 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5688 var tool
= this.getTargetTool( e
);
5690 // e.which is 0 for touch events, 1 for left mouse button
5691 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5692 this.pressed
.onSelect();
5695 this.pressed
= null;
5700 * Handle mouse over events.
5702 * @param {jQuery.Event} e Mouse over event
5704 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5705 var tool
= this.getTargetTool( e
);
5707 if ( this.pressed
&& this.pressed
=== tool
) {
5708 this.pressed
.setActive( true );
5713 * Handle mouse out events.
5715 * @param {jQuery.Event} e Mouse out event
5717 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5718 var tool
= this.getTargetTool( e
);
5720 if ( this.pressed
&& this.pressed
=== tool
) {
5721 this.pressed
.setActive( false );
5726 * Get the closest tool to a jQuery.Event.
5728 * Only tool links are considered, which prevents other elements in the tool such as popups from
5729 * triggering tool group interactions.
5732 * @param {jQuery.Event} e
5733 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5735 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5737 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5739 if ( $item
.length
) {
5740 tool
= $item
.parent().data( 'oo-ui-tool' );
5743 return tool
&& !tool
.isDisabled() ? tool
: null;
5747 * Handle tool registry register events.
5749 * If a tool is registered after the group is created, we must repopulate the list to account for:
5751 * - a tool being added that may be included
5752 * - a tool already included being overridden
5754 * @param {string} name Symbolic name of tool
5756 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5761 * Get the toolbar this group is in.
5763 * @return {OO.ui.Toolbar} Toolbar of group
5765 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5766 return this.toolbar
;
5770 * Add and remove tools based on configuration.
5772 OO
.ui
.ToolGroup
.prototype.populate = function () {
5773 var i
, len
, name
, tool
,
5774 toolFactory
= this.toolbar
.getToolFactory(),
5778 list
= this.toolbar
.getToolFactory().getTools(
5779 this.include
, this.exclude
, this.promote
, this.demote
5782 // Build a list of needed tools
5783 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5787 toolFactory
.lookup( name
) &&
5788 // Tool is available or is already in this group
5789 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5791 tool
= this.tools
[name
];
5793 // Auto-initialize tools on first use
5794 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5797 this.toolbar
.reserveTool( tool
);
5802 // Remove tools that are no longer needed
5803 for ( name
in this.tools
) {
5804 if ( !names
[name
] ) {
5805 this.tools
[name
].destroy();
5806 this.toolbar
.releaseTool( this.tools
[name
] );
5807 remove
.push( this.tools
[name
] );
5808 delete this.tools
[name
];
5811 if ( remove
.length
) {
5812 this.removeItems( remove
);
5814 // Update emptiness state
5816 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5818 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5820 // Re-add tools (moving existing ones to new locations)
5821 this.addItems( add
);
5822 // Disabled state may depend on items
5823 this.updateDisabled();
5827 * Destroy tool group.
5829 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5833 this.toolbar
.getToolFactory().disconnect( this );
5834 for ( name
in this.tools
) {
5835 this.toolbar
.releaseTool( this.tools
[name
] );
5836 this.tools
[name
].disconnect( this ).destroy();
5837 delete this.tools
[name
];
5839 this.$element
.remove();
5843 * Dialog for showing a message.
5846 * - Registers two actions by default (safe and primary).
5847 * - Renders action widgets in the footer.
5850 * @extends OO.ui.Dialog
5853 * @param {Object} [config] Configuration options
5855 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5856 // Parent constructor
5857 OO
.ui
.MessageDialog
.super.call( this, config
);
5860 this.verticalActionLayout
= null;
5863 this.$element
.addClass( 'oo-ui-messageDialog' );
5868 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5870 /* Static Properties */
5872 OO
.ui
.MessageDialog
.static.name
= 'message';
5874 OO
.ui
.MessageDialog
.static.size
= 'small';
5876 OO
.ui
.MessageDialog
.static.verbose
= false;
5881 * A confirmation dialog's title should describe what the progressive action will do. An alert
5882 * dialog's title should describe what event occurred.
5886 * @property {jQuery|string|Function|null}
5888 OO
.ui
.MessageDialog
.static.title
= null;
5891 * A confirmation dialog's message should describe the consequences of the progressive action. An
5892 * alert dialog's message should describe why the event occurred.
5896 * @property {jQuery|string|Function|null}
5898 OO
.ui
.MessageDialog
.static.message
= null;
5900 OO
.ui
.MessageDialog
.static.actions
= [
5901 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5902 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5910 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5912 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5916 * Toggle action layout between vertical and horizontal.
5918 * @param {boolean} [value] Layout actions vertically, omit to toggle
5921 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5922 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5924 if ( value
!== this.verticalActionLayout
) {
5925 this.verticalActionLayout
= value
;
5927 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5928 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5937 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5939 return new OO
.ui
.Process( function () {
5940 this.close( { action
: action
} );
5943 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5949 * @param {Object} [data] Dialog opening data
5950 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5951 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5952 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5953 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5956 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5960 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5961 .next( function () {
5962 this.title
.setLabel(
5963 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5965 this.message
.setLabel(
5966 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5968 this.message
.$element
.toggleClass(
5969 'oo-ui-messageDialog-message-verbose',
5970 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5978 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5979 return Math
.round( this.text
.$element
.outerHeight( true ) );
5985 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5987 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5990 this.$actions
= this.$( '<div>' );
5991 this.container
= new OO
.ui
.PanelLayout( {
5992 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5994 this.text
= new OO
.ui
.PanelLayout( {
5995 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5997 this.message
= new OO
.ui
.LabelWidget( {
5998 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
6002 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
6003 this.$content
.addClass( 'oo-ui-messageDialog-content' );
6004 this.container
.$element
.append( this.text
.$element
);
6005 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
6006 this.$body
.append( this.container
.$element
);
6007 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
6008 this.$foot
.append( this.$actions
);
6014 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
6015 var i
, len
, other
, special
, others
;
6018 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
6020 special
= this.actions
.getSpecial();
6021 others
= this.actions
.getOthers();
6022 if ( special
.safe
) {
6023 this.$actions
.append( special
.safe
.$element
);
6024 special
.safe
.toggleFramed( false );
6026 if ( others
.length
) {
6027 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6029 this.$actions
.append( other
.$element
);
6030 other
.toggleFramed( false );
6033 if ( special
.primary
) {
6034 this.$actions
.append( special
.primary
.$element
);
6035 special
.primary
.toggleFramed( false );
6039 if ( !this.isOpening() ) {
6040 this.manager
.updateWindowSize( this );
6042 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6046 * Fit action actions into columns or rows.
6048 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
6050 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
6052 actions
= this.actions
.get();
6055 this.toggleVerticalActionLayout( false );
6056 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
6057 action
= actions
[i
];
6058 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
6059 this.toggleVerticalActionLayout( true );
6066 * Navigation dialog window.
6069 * - Show and hide errors.
6070 * - Retry an action.
6073 * - Renders header with dialog title and one action widget on either side
6074 * (a 'safe' button on the left, and a 'primary' button on the right, both of
6075 * which close the dialog).
6076 * - Displays any action widgets in the footer (none by default).
6077 * - Ability to dismiss errors.
6079 * Subclass responsibilities:
6080 * - Register a 'safe' action.
6081 * - Register a 'primary' action.
6082 * - Add content to the dialog.
6086 * @extends OO.ui.Dialog
6089 * @param {Object} [config] Configuration options
6091 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
6092 // Parent constructor
6093 OO
.ui
.ProcessDialog
.super.call( this, config
);
6096 this.$element
.addClass( 'oo-ui-processDialog' );
6101 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
6106 * Handle dismiss button click events.
6110 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
6115 * Handle retry button click events.
6117 * Hides errors and then tries again.
6119 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
6121 this.executeAction( this.currentAction
.getAction() );
6127 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
6128 if ( this.actions
.isSpecial( action
) ) {
6131 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
6137 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
6139 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
6142 this.$navigation
= this.$( '<div>' );
6143 this.$location
= this.$( '<div>' );
6144 this.$safeActions
= this.$( '<div>' );
6145 this.$primaryActions
= this.$( '<div>' );
6146 this.$otherActions
= this.$( '<div>' );
6147 this.dismissButton
= new OO
.ui
.ButtonWidget( {
6149 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
6151 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
6152 this.$errors
= this.$( '<div>' );
6153 this.$errorsTitle
= this.$( '<div>' );
6156 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
6157 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
6160 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
6162 .append( this.title
.$element
)
6163 .addClass( 'oo-ui-processDialog-location' );
6164 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
6165 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
6166 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
6168 .addClass( 'oo-ui-processDialog-errors-title' )
6169 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
6171 .addClass( 'oo-ui-processDialog-errors' )
6172 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
6174 .addClass( 'oo-ui-processDialog-content' )
6175 .append( this.$errors
);
6177 .addClass( 'oo-ui-processDialog-navigation' )
6178 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
6179 this.$head
.append( this.$navigation
);
6180 this.$foot
.append( this.$otherActions
);
6186 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
6187 var i
, len
, other
, special
, others
;
6190 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
6192 special
= this.actions
.getSpecial();
6193 others
= this.actions
.getOthers();
6194 if ( special
.primary
) {
6195 this.$primaryActions
.append( special
.primary
.$element
);
6196 special
.primary
.toggleFramed( true );
6198 if ( others
.length
) {
6199 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6201 this.$otherActions
.append( other
.$element
);
6202 other
.toggleFramed( true );
6205 if ( special
.safe
) {
6206 this.$safeActions
.append( special
.safe
.$element
);
6207 special
.safe
.toggleFramed( true );
6211 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6217 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
6218 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
6219 .fail( this.showErrors
.bind( this ) );
6223 * Fit label between actions.
6227 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
6228 var width
= Math
.max(
6229 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
6230 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
6232 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
6238 * Handle errors that occurred during accept or reject processes.
6240 * @param {OO.ui.Error[]} errors Errors to be handled
6242 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
6248 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
6249 if ( !errors
[i
].isRecoverable() ) {
6250 recoverable
= false;
6252 if ( errors
[i
].isWarning() ) {
6255 $item
= this.$( '<div>' )
6256 .addClass( 'oo-ui-processDialog-error' )
6257 .append( errors
[i
].getMessage() );
6258 items
.push( $item
[0] );
6260 this.$errorItems
= this.$( items
);
6261 if ( recoverable
) {
6262 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
6264 this.currentAction
.setDisabled( true );
6267 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
6269 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
6271 this.retryButton
.toggle( recoverable
);
6272 this.$errorsTitle
.after( this.$errorItems
);
6273 this.$errors
.show().scrollTop( 0 );
6279 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
6280 this.$errors
.hide();
6281 this.$errorItems
.remove();
6282 this.$errorItems
= null;
6286 * Layout containing a series of pages.
6289 * @extends OO.ui.Layout
6292 * @param {Object} [config] Configuration options
6293 * @cfg {boolean} [continuous=false] Show all pages, one after another
6294 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
6295 * @cfg {boolean} [outlined=false] Show an outline
6296 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
6298 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
6299 // Configuration initialization
6300 config
= config
|| {};
6302 // Parent constructor
6303 OO
.ui
.BookletLayout
.super.call( this, config
);
6306 this.currentPageName
= null;
6308 this.ignoreFocus
= false;
6309 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
6310 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
6311 this.outlineVisible
= false;
6312 this.outlined
= !!config
.outlined
;
6313 if ( this.outlined
) {
6314 this.editable
= !!config
.editable
;
6315 this.outlineControlsWidget
= null;
6316 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget( { $: this.$ } );
6317 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
6318 this.gridLayout
= new OO
.ui
.GridLayout(
6319 [ this.outlinePanel
, this.stackLayout
],
6320 { $: this.$, widths
: [ 1, 2 ] }
6322 this.outlineVisible
= true;
6323 if ( this.editable
) {
6324 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6325 this.outlineSelectWidget
, { $: this.$ }
6331 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6332 if ( this.outlined
) {
6333 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
6335 if ( this.autoFocus
) {
6336 // Event 'focus' does not bubble, but 'focusin' does
6337 this.stackLayout
.onDOMEvent( 'focusin', this.onStackLayoutFocus
.bind( this ) );
6341 this.$element
.addClass( 'oo-ui-bookletLayout' );
6342 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6343 if ( this.outlined
) {
6344 this.outlinePanel
.$element
6345 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6346 .append( this.outlineSelectWidget
.$element
);
6347 if ( this.editable
) {
6348 this.outlinePanel
.$element
6349 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6350 .append( this.outlineControlsWidget
.$element
);
6352 this.$element
.append( this.gridLayout
.$element
);
6354 this.$element
.append( this.stackLayout
.$element
);
6360 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6366 * @param {OO.ui.PageLayout} page Current page
6371 * @param {OO.ui.PageLayout[]} page Added pages
6372 * @param {number} index Index pages were added at
6377 * @param {OO.ui.PageLayout[]} pages Removed pages
6383 * Handle stack layout focus.
6385 * @param {jQuery.Event} e Focusin event
6387 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6390 // Find the page that an element was focused within
6391 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6392 for ( name
in this.pages
) {
6393 // Check for page match, exclude current page to find only page changes
6394 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6395 this.setPage( name
);
6402 * Handle stack layout set events.
6404 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6406 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6407 var $input
, layout
= this;
6409 page
.scrollElementIntoView( { complete: function () {
6410 if ( layout
.autoFocus
) {
6411 // Set focus to the first input if nothing on the page is focused yet
6412 if ( !page
.$element
.find( ':focus' ).length
) {
6413 $input
= page
.$element
.find( ':input:first' );
6414 if ( $input
.length
) {
6424 * Handle outline widget select events.
6426 * @param {OO.ui.OptionWidget|null} item Selected item
6428 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
6430 this.setPage( item
.getData() );
6435 * Check if booklet has an outline.
6439 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6440 return this.outlined
;
6444 * Check if booklet has editing controls.
6448 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6449 return this.editable
;
6453 * Check if booklet has a visible outline.
6457 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6458 return this.outlined
&& this.outlineVisible
;
6462 * Hide or show the outline.
6464 * @param {boolean} [show] Show outline, omit to invert current state
6467 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6468 if ( this.outlined
) {
6469 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6470 this.outlineVisible
= show
;
6471 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6478 * Get the outline widget.
6480 * @param {OO.ui.PageLayout} page Page to be selected
6481 * @return {OO.ui.PageLayout|null} Closest page to another
6483 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6484 var next
, prev
, level
,
6485 pages
= this.stackLayout
.getItems(),
6486 index
= $.inArray( page
, pages
);
6488 if ( index
!== -1 ) {
6489 next
= pages
[index
+ 1];
6490 prev
= pages
[index
- 1];
6491 // Prefer adjacent pages at the same level
6492 if ( this.outlined
) {
6493 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
6496 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
6502 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
6508 return prev
|| next
|| null;
6512 * Get the outline widget.
6514 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if booklet has no outline
6516 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6517 return this.outlineSelectWidget
;
6521 * Get the outline controls widget. If the outline is not editable, null is returned.
6523 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6525 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6526 return this.outlineControlsWidget
;
6530 * Get a page by name.
6532 * @param {string} name Symbolic name of page
6533 * @return {OO.ui.PageLayout|undefined} Page, if found
6535 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6536 return this.pages
[name
];
6540 * Get the current page name.
6542 * @return {string|null} Current page name
6544 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6545 return this.currentPageName
;
6549 * Add a page to the layout.
6551 * When pages are added with the same names as existing pages, the existing pages will be
6552 * automatically removed before the new pages are added.
6554 * @param {OO.ui.PageLayout[]} pages Pages to add
6555 * @param {number} index Index to insert pages after
6559 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6560 var i
, len
, name
, page
, item
, currentIndex
,
6561 stackLayoutPages
= this.stackLayout
.getItems(),
6565 // Remove pages with same names
6566 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6568 name
= page
.getName();
6570 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6571 // Correct the insertion index
6572 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6573 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6576 remove
.push( this.pages
[name
] );
6579 if ( remove
.length
) {
6580 this.removePages( remove
);
6584 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6586 name
= page
.getName();
6587 this.pages
[page
.getName()] = page
;
6588 if ( this.outlined
) {
6589 item
= new OO
.ui
.OutlineOptionWidget( { $: this.$, data
: name
} );
6590 page
.setOutlineItem( item
);
6595 if ( this.outlined
&& items
.length
) {
6596 this.outlineSelectWidget
.addItems( items
, index
);
6597 this.updateOutlineSelectWidget();
6599 this.stackLayout
.addItems( pages
, index
);
6600 this.emit( 'add', pages
, index
);
6606 * Remove a page from the layout.
6611 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6612 var i
, len
, name
, page
,
6615 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6617 name
= page
.getName();
6618 delete this.pages
[name
];
6619 if ( this.outlined
) {
6620 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
6621 page
.setOutlineItem( null );
6624 if ( this.outlined
&& items
.length
) {
6625 this.outlineSelectWidget
.removeItems( items
);
6626 this.updateOutlineSelectWidget();
6628 this.stackLayout
.removeItems( pages
);
6629 this.emit( 'remove', pages
);
6635 * Clear all pages from the layout.
6640 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6642 pages
= this.stackLayout
.getItems();
6645 this.currentPageName
= null;
6646 if ( this.outlined
) {
6647 this.outlineSelectWidget
.clearItems();
6648 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6649 pages
[i
].setOutlineItem( null );
6652 this.stackLayout
.clearItems();
6654 this.emit( 'remove', pages
);
6660 * Set the current page by name.
6663 * @param {string} name Symbolic name of page
6665 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6668 page
= this.pages
[name
];
6670 if ( name
!== this.currentPageName
) {
6671 if ( this.outlined
) {
6672 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
6673 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6674 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getItemFromData( name
) );
6678 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6679 this.pages
[this.currentPageName
].setActive( false );
6680 // Blur anything focused if the next page doesn't have anything focusable - this
6681 // is not needed if the next page has something focusable because once it is focused
6682 // this blur happens automatically
6683 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6684 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6685 if ( $focused
.length
) {
6690 this.currentPageName
= name
;
6691 this.stackLayout
.setItem( page
);
6692 page
.setActive( true );
6693 this.emit( 'set', page
);
6699 * Call this after adding or removing items from the OutlineSelectWidget.
6703 OO
.ui
.BookletLayout
.prototype.updateOutlineSelectWidget = function () {
6704 // Auto-select first item when nothing is selected anymore
6705 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
6706 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
6713 * Layout made of a field and optional label.
6715 * Available label alignment modes include:
6716 * - left: Label is before the field and aligned away from it, best for when the user will be
6717 * scanning for a specific label in a form with many fields
6718 * - right: Label is before the field and aligned toward it, best for forms the user is very
6719 * familiar with and will tab through field checking quickly to verify which field they are in
6720 * - top: Label is before the field and above it, best for when the user will need to fill out all
6721 * fields from top to bottom in a form with few fields
6722 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6723 * checkboxes or radio buttons
6726 * @extends OO.ui.Layout
6727 * @mixins OO.ui.LabelElement
6730 * @param {OO.ui.Widget} fieldWidget Field widget
6731 * @param {Object} [config] Configuration options
6732 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6733 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6735 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
6736 // Configuration initialization
6737 config
= $.extend( { align
: 'left' }, config
);
6739 // Properties (must be set before parent constructor, which calls #getTagName)
6740 this.fieldWidget
= fieldWidget
;
6742 // Parent constructor
6743 OO
.ui
.FieldLayout
.super.call( this, config
);
6745 // Mixin constructors
6746 OO
.ui
.LabelElement
.call( this, config
);
6749 this.$field
= this.$( '<div>' );
6751 if ( config
.help
) {
6752 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6754 classes
: [ 'oo-ui-fieldLayout-help' ],
6759 this.popupButtonWidget
.getPopup().$body
.append(
6761 .text( config
.help
)
6762 .addClass( 'oo-ui-fieldLayout-help-content' )
6764 this.$help
= this.popupButtonWidget
.$element
;
6766 this.$help
= this.$( [] );
6770 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
6771 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
6773 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
6776 this.$element
.addClass( 'oo-ui-fieldLayout' );
6778 .addClass( 'oo-ui-fieldLayout-field' )
6779 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
6780 .append( this.fieldWidget
.$element
);
6781 this.setAlignment( config
.align
);
6786 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6787 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6794 OO
.ui
.FieldLayout
.prototype.getTagName = function () {
6795 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
6803 * Handle field disable events.
6805 * @param {boolean} value Field is disabled
6807 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6808 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6812 * Handle label mouse click events.
6814 * @param {jQuery.Event} e Mouse click event
6816 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6817 this.fieldWidget
.simulateLabelClick();
6824 * @return {OO.ui.Widget} Field widget
6826 OO
.ui
.FieldLayout
.prototype.getField = function () {
6827 return this.fieldWidget
;
6831 * Set the field alignment mode.
6834 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6837 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6838 if ( value
!== this.align
) {
6839 // Default to 'left'
6840 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6844 if ( value
=== 'inline' ) {
6845 this.$element
.append( this.$field
, this.$label
, this.$help
);
6847 this.$element
.append( this.$help
, this.$label
, this.$field
);
6849 // Set classes. The following classes can be used here:
6850 // * oo-ui-fieldLayout-align-left
6851 // * oo-ui-fieldLayout-align-right
6852 // * oo-ui-fieldLayout-align-top
6853 // * oo-ui-fieldLayout-align-inline
6855 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6857 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
6865 * Layout made of a fieldset and optional legend.
6867 * Just add OO.ui.FieldLayout items.
6870 * @extends OO.ui.Layout
6871 * @mixins OO.ui.IconElement
6872 * @mixins OO.ui.LabelElement
6873 * @mixins OO.ui.GroupElement
6876 * @param {Object} [config] Configuration options
6877 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6879 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6880 // Configuration initialization
6881 config
= config
|| {};
6883 // Parent constructor
6884 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6886 // Mixin constructors
6887 OO
.ui
.IconElement
.call( this, config
);
6888 OO
.ui
.LabelElement
.call( this, config
);
6889 OO
.ui
.GroupElement
.call( this, config
);
6893 .addClass( 'oo-ui-fieldsetLayout' )
6894 .prepend( this.$icon
, this.$label
, this.$group
);
6895 if ( $.isArray( config
.items
) ) {
6896 this.addItems( config
.items
);
6902 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6903 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6904 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6905 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6908 * Layout with an HTML form.
6911 * @extends OO.ui.Layout
6914 * @param {Object} [config] Configuration options
6915 * @cfg {string} [method] HTML form `method` attribute
6916 * @cfg {string} [action] HTML form `action` attribute
6917 * @cfg {string} [enctype] HTML form `enctype` attribute
6919 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6920 // Configuration initialization
6921 config
= config
|| {};
6923 // Parent constructor
6924 OO
.ui
.FormLayout
.super.call( this, config
);
6927 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
6931 .addClass( 'oo-ui-formLayout' )
6933 method
: config
.method
,
6934 action
: config
.action
,
6935 enctype
: config
.enctype
6941 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6949 /* Static Properties */
6951 OO
.ui
.FormLayout
.static.tagName
= 'form';
6956 * Handle form submit events.
6958 * @param {jQuery.Event} e Submit event
6961 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6962 this.emit( 'submit' );
6967 * Layout made of proportionally sized columns and rows.
6970 * @extends OO.ui.Layout
6973 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6974 * @param {Object} [config] Configuration options
6975 * @cfg {number[]} [widths] Widths of columns as ratios
6976 * @cfg {number[]} [heights] Heights of rows as ratios
6978 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6981 // Configuration initialization
6982 config
= config
|| {};
6984 // Parent constructor
6985 OO
.ui
.GridLayout
.super.call( this, config
);
6993 this.$element
.addClass( 'oo-ui-gridLayout' );
6994 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6995 this.panels
.push( panels
[i
] );
6996 this.$element
.append( panels
[i
].$element
);
6998 if ( config
.widths
|| config
.heights
) {
6999 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
7001 // Arrange in columns by default
7002 widths
= this.panels
.map( function () { return 1; } );
7003 this.layout( widths
, [ 1 ] );
7009 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
7024 * Set grid dimensions.
7026 * @param {number[]} widths Widths of columns as ratios
7027 * @param {number[]} heights Heights of rows as ratios
7029 * @throws {Error} If grid is not large enough to fit all panels
7031 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
7035 cols
= widths
.length
,
7036 rows
= heights
.length
;
7038 // Verify grid is big enough to fit panels
7039 if ( cols
* rows
< this.panels
.length
) {
7040 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
7043 // Sum up denominators
7044 for ( x
= 0; x
< cols
; x
++ ) {
7047 for ( y
= 0; y
< rows
; y
++ ) {
7053 for ( x
= 0; x
< cols
; x
++ ) {
7054 this.widths
[x
] = widths
[x
] / xd
;
7056 for ( y
= 0; y
< rows
; y
++ ) {
7057 this.heights
[y
] = heights
[y
] / yd
;
7061 this.emit( 'layout' );
7065 * Update panel positions and sizes.
7069 OO
.ui
.GridLayout
.prototype.update = function () {
7070 var x
, y
, panel
, width
, height
, dimensions
,
7074 cols
= this.widths
.length
,
7075 rows
= this.heights
.length
;
7077 for ( y
= 0; y
< rows
; y
++ ) {
7078 height
= this.heights
[y
];
7079 for ( x
= 0; x
< cols
; x
++ ) {
7080 width
= this.widths
[x
];
7081 panel
= this.panels
[i
];
7083 width
: Math
.round( width
* 100 ) + '%',
7084 height
: Math
.round( height
* 100 ) + '%',
7085 top
: Math
.round( top
* 100 ) + '%'
7088 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
7089 dimensions
.right
= Math
.round( left
* 100 ) + '%';
7091 dimensions
.left
= Math
.round( left
* 100 ) + '%';
7093 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
7094 if ( width
=== 0 || height
=== 0 ) {
7095 dimensions
.visibility
= 'hidden';
7097 dimensions
.visibility
= '';
7099 panel
.$element
.css( dimensions
);
7107 this.emit( 'update' );
7111 * Get a panel at a given position.
7113 * The x and y position is affected by the current grid layout.
7115 * @param {number} x Horizontal position
7116 * @param {number} y Vertical position
7117 * @return {OO.ui.PanelLayout} The panel at the given position
7119 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
7120 return this.panels
[ ( x
* this.widths
.length
) + y
];
7124 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
7127 * @extends OO.ui.Layout
7130 * @param {Object} [config] Configuration options
7131 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
7132 * @cfg {boolean} [padded=false] Pad the content from the edges
7133 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
7135 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
7136 // Configuration initialization
7137 config
= $.extend( {
7143 // Parent constructor
7144 OO
.ui
.PanelLayout
.super.call( this, config
);
7147 this.$element
.addClass( 'oo-ui-panelLayout' );
7148 if ( config
.scrollable
) {
7149 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
7151 if ( config
.padded
) {
7152 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
7154 if ( config
.expanded
) {
7155 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
7161 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
7164 * Page within an booklet layout.
7167 * @extends OO.ui.PanelLayout
7170 * @param {string} name Unique symbolic name of page
7171 * @param {Object} [config] Configuration options
7172 * @param {string} [outlineItem] Outline item widget
7174 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
7175 // Configuration initialization
7176 config
= $.extend( { scrollable
: true }, config
);
7178 // Parent constructor
7179 OO
.ui
.PageLayout
.super.call( this, config
);
7183 this.outlineItem
= config
.outlineItem
|| null;
7184 this.active
= false;
7187 this.$element
.addClass( 'oo-ui-pageLayout' );
7192 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
7198 * @param {boolean} active Page is active
7206 * @return {string} Symbolic name of page
7208 OO
.ui
.PageLayout
.prototype.getName = function () {
7213 * Check if page is active.
7215 * @return {boolean} Page is active
7217 OO
.ui
.PageLayout
.prototype.isActive = function () {
7224 * @return {OO.ui.OutlineOptionWidget|null} Outline item widget
7226 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
7227 return this.outlineItem
;
7233 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
7234 * outline item as desired; this method is called for setting (with an object) and unsetting
7235 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
7236 * operating on null instead of an OO.ui.OutlineOptionWidget object.
7238 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline item widget, null to clear
7241 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
7242 this.outlineItem
= outlineItem
|| null;
7243 if ( outlineItem
) {
7244 this.setupOutlineItem();
7250 * Setup outline item.
7252 * @localdoc Subclasses should override this method to adjust the outline item as desired.
7254 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline item widget to setup
7257 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
7262 * Set page active state.
7264 * @param {boolean} Page is active
7267 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
7270 if ( active
!== this.active
) {
7271 this.active
= active
;
7272 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
7273 this.emit( 'active', this.active
);
7278 * Layout containing a series of mutually exclusive pages.
7281 * @extends OO.ui.PanelLayout
7282 * @mixins OO.ui.GroupElement
7285 * @param {Object} [config] Configuration options
7286 * @cfg {boolean} [continuous=false] Show all pages, one after another
7287 * @cfg {string} [icon=''] Symbolic icon name
7288 * @cfg {OO.ui.Layout[]} [items] Layouts to add
7290 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
7291 // Configuration initialization
7292 config
= $.extend( { scrollable
: true }, config
);
7294 // Parent constructor
7295 OO
.ui
.StackLayout
.super.call( this, config
);
7297 // Mixin constructors
7298 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7301 this.currentItem
= null;
7302 this.continuous
= !!config
.continuous
;
7305 this.$element
.addClass( 'oo-ui-stackLayout' );
7306 if ( this.continuous
) {
7307 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
7309 if ( $.isArray( config
.items
) ) {
7310 this.addItems( config
.items
);
7316 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
7317 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
7323 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
7329 * Get the current item.
7331 * @return {OO.ui.Layout|null}
7333 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
7334 return this.currentItem
;
7338 * Unset the current item.
7341 * @param {OO.ui.StackLayout} layout
7344 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7345 var prevItem
= this.currentItem
;
7346 if ( prevItem
=== null ) {
7350 this.currentItem
= null;
7351 this.emit( 'set', null );
7357 * Adding an existing item (by value) will move it.
7359 * @param {OO.ui.Layout[]} items Items to add
7360 * @param {number} [index] Index to insert items after
7363 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7365 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7367 if ( !this.currentItem
&& items
.length
) {
7368 this.setItem( items
[0] );
7377 * Items will be detached, not removed, so they can be used later.
7379 * @param {OO.ui.Layout[]} items Items to remove
7383 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7385 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7387 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7388 if ( this.items
.length
) {
7389 this.setItem( this.items
[0] );
7391 this.unsetCurrentItem();
7401 * Items will be detached, not removed, so they can be used later.
7406 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7407 this.unsetCurrentItem();
7408 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7416 * Any currently shown item will be hidden.
7418 * FIXME: If the passed item to show has not been added in the items list, then
7419 * this method drops it and unsets the current item.
7421 * @param {OO.ui.Layout} item Item to show
7425 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7428 if ( item
!== this.currentItem
) {
7429 if ( !this.continuous
) {
7430 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7431 this.items
[i
].$element
.css( 'display', '' );
7434 if ( $.inArray( item
, this.items
) !== -1 ) {
7435 if ( !this.continuous
) {
7436 item
.$element
.css( 'display', 'block' );
7438 this.currentItem
= item
;
7439 this.emit( 'set', item
);
7441 this.unsetCurrentItem();
7449 * Horizontal bar layout of tools as icon buttons.
7452 * @extends OO.ui.ToolGroup
7455 * @param {OO.ui.Toolbar} toolbar
7456 * @param {Object} [config] Configuration options
7458 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7459 // Parent constructor
7460 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7463 this.$element
.addClass( 'oo-ui-barToolGroup' );
7468 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7470 /* Static Properties */
7472 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7474 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7476 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7479 * Popup list of tools with an icon and optional label.
7483 * @extends OO.ui.ToolGroup
7484 * @mixins OO.ui.IconElement
7485 * @mixins OO.ui.IndicatorElement
7486 * @mixins OO.ui.LabelElement
7487 * @mixins OO.ui.TitledElement
7488 * @mixins OO.ui.ClippableElement
7491 * @param {OO.ui.Toolbar} toolbar
7492 * @param {Object} [config] Configuration options
7493 * @cfg {string} [header] Text to display at the top of the pop-up
7495 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7496 // Configuration initialization
7497 config
= config
|| {};
7499 // Parent constructor
7500 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7502 // Mixin constructors
7503 OO
.ui
.IconElement
.call( this, config
);
7504 OO
.ui
.IndicatorElement
.call( this, config
);
7505 OO
.ui
.LabelElement
.call( this, config
);
7506 OO
.ui
.TitledElement
.call( this, config
);
7507 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7510 this.active
= false;
7511 this.dragging
= false;
7512 this.onBlurHandler
= this.onBlur
.bind( this );
7513 this.$handle
= this.$( '<span>' );
7517 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
7518 'mouseup touchend': this.onHandlePointerUp
.bind( this )
7523 .addClass( 'oo-ui-popupToolGroup-handle' )
7524 .append( this.$icon
, this.$label
, this.$indicator
);
7525 // If the pop-up should have a header, add it to the top of the toolGroup.
7526 // Note: If this feature is useful for other widgets, we could abstract it into an
7527 // OO.ui.HeaderedElement mixin constructor.
7528 if ( config
.header
!== undefined ) {
7530 .prepend( this.$( '<span>' )
7531 .addClass( 'oo-ui-popupToolGroup-header' )
7532 .text( config
.header
)
7536 .addClass( 'oo-ui-popupToolGroup' )
7537 .prepend( this.$handle
);
7542 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7543 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7544 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7545 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7546 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7547 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7549 /* Static Properties */
7556 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7558 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7560 if ( this.isDisabled() && this.isElementAttached() ) {
7561 this.setActive( false );
7566 * Handle focus being lost.
7568 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7570 * @param {jQuery.Event} e Mouse up event
7572 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7573 // Only deactivate when clicking outside the dropdown element
7574 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7575 this.setActive( false );
7582 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7583 // e.which is 0 for touch events, 1 for left mouse button
7584 // Only close toolgroup when a tool was actually selected
7585 // FIXME: this duplicates logic from the parent class
7586 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== this.getTargetTool( e
) ) {
7587 this.setActive( false );
7589 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7593 * Handle mouse up events.
7595 * @param {jQuery.Event} e Mouse up event
7597 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7602 * Handle mouse down events.
7604 * @param {jQuery.Event} e Mouse down event
7606 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7607 // e.which is 0 for touch events, 1 for left mouse button
7608 if ( !this.isDisabled() && e
.which
<= 1 ) {
7609 this.setActive( !this.active
);
7615 * Switch into active mode.
7617 * When active, mouseup events anywhere in the document will trigger deactivation.
7619 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7621 if ( this.active
!== value
) {
7622 this.active
= value
;
7624 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7626 // Try anchoring the popup to the left first
7627 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7628 this.toggleClipping( true );
7629 if ( this.isClippedHorizontally() ) {
7630 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7631 this.toggleClipping( false );
7633 .removeClass( 'oo-ui-popupToolGroup-left' )
7634 .addClass( 'oo-ui-popupToolGroup-right' );
7635 this.toggleClipping( true );
7638 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7639 this.$element
.removeClass(
7640 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7642 this.toggleClipping( false );
7648 * Drop down list layout of tools as labeled icon buttons.
7650 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
7651 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
7652 * may want to use the 'promote' and 'demote' configuration options to achieve this.
7655 * @extends OO.ui.PopupToolGroup
7658 * @param {OO.ui.Toolbar} toolbar
7659 * @param {Object} [config] Configuration options
7660 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
7662 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
7663 * allowed to be collapsed.
7664 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
7666 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7667 // Configuration initialization
7668 config
= config
|| {};
7670 // Properties (must be set before parent constructor, which calls #populate)
7671 this.allowCollapse
= config
.allowCollapse
;
7672 this.forceExpand
= config
.forceExpand
;
7673 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
7674 this.collapsibleTools
= [];
7676 // Parent constructor
7677 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7680 this.$element
.addClass( 'oo-ui-listToolGroup' );
7685 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7687 /* Static Properties */
7689 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7691 OO
.ui
.ListToolGroup
.static.name
= 'list';
7698 OO
.ui
.ListToolGroup
.prototype.populate = function () {
7699 var i
, len
, allowCollapse
= [];
7701 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
7703 // Update the list of collapsible tools
7704 if ( this.allowCollapse
!== undefined ) {
7705 allowCollapse
= this.allowCollapse
;
7706 } else if ( this.forceExpand
!== undefined ) {
7707 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
7710 this.collapsibleTools
= [];
7711 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
7712 if ( this.tools
[ allowCollapse
[i
] ] !== undefined ) {
7713 this.collapsibleTools
.push( this.tools
[ allowCollapse
[i
] ] );
7717 // Keep at the end, even when tools are added
7718 this.$group
.append( this.getExpandCollapseTool().$element
);
7720 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
7722 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
7723 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
7724 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
7725 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
7726 this.getExpandCollapseTool().$element
.css( 'display', 'inline-block' );
7729 this.updateCollapsibleState();
7732 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
7733 if ( this.expandCollapseTool
=== undefined ) {
7734 var ExpandCollapseTool = function () {
7735 ExpandCollapseTool
.super.apply( this, arguments
);
7738 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
7740 ExpandCollapseTool
.prototype.onSelect = function () {
7741 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
7742 this.toolGroup
.updateCollapsibleState();
7743 this.setActive( false );
7745 ExpandCollapseTool
.prototype.onUpdateState = function () {
7746 // Do nothing. Tool interface requires an implementation of this function.
7749 ExpandCollapseTool
.static.name
= 'more-fewer';
7751 this.expandCollapseTool
= new ExpandCollapseTool( this );
7753 return this.expandCollapseTool
;
7759 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
7760 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7762 // Do not close the popup when the user wants to show more/fewer tools
7763 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
7764 // Prevent the popup list from being hidden
7765 this.setActive( true );
7771 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
7774 this.getExpandCollapseTool()
7775 .setIcon( this.expanded
? 'collapse' : 'expand' )
7776 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
7778 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
7779 this.collapsibleTools
[i
].toggle( this.expanded
);
7784 * Drop down menu layout of tools as selectable menu items.
7787 * @extends OO.ui.PopupToolGroup
7790 * @param {OO.ui.Toolbar} toolbar
7791 * @param {Object} [config] Configuration options
7793 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7794 // Configuration initialization
7795 config
= config
|| {};
7797 // Parent constructor
7798 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7801 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7804 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7809 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7811 /* Static Properties */
7813 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7815 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7820 * Handle the toolbar state being updated.
7822 * When the state changes, the title of each active item in the menu will be joined together and
7823 * used as a label for the group. The label will be empty if none of the items are active.
7825 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7829 for ( name
in this.tools
) {
7830 if ( this.tools
[name
].isActive() ) {
7831 labelTexts
.push( this.tools
[name
].getTitle() );
7835 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7839 * Tool that shows a popup when selected.
7843 * @extends OO.ui.Tool
7844 * @mixins OO.ui.PopupElement
7847 * @param {OO.ui.Toolbar} toolbar
7848 * @param {Object} [config] Configuration options
7850 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7851 // Parent constructor
7852 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7854 // Mixin constructors
7855 OO
.ui
.PopupElement
.call( this, config
);
7859 .addClass( 'oo-ui-popupTool' )
7860 .append( this.popup
.$element
);
7865 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7866 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7871 * Handle the tool being selected.
7875 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7876 if ( !this.isDisabled() ) {
7877 this.popup
.toggle();
7879 this.setActive( false );
7884 * Handle the toolbar state being updated.
7888 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7889 this.setActive( false );
7893 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7895 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7899 * @extends OO.ui.GroupElement
7902 * @param {Object} [config] Configuration options
7904 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7905 // Parent constructor
7906 OO
.ui
.GroupWidget
.super.call( this, config
);
7911 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7916 * Set the disabled state of the widget.
7918 * This will also update the disabled state of child widgets.
7920 * @param {boolean} disabled Disable widget
7923 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7927 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7928 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7930 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7932 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7933 this.items
[i
].updateDisabled();
7941 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7943 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7944 * allows bidirectional communication.
7946 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7953 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7960 * Check if widget is disabled.
7962 * Checks parent if present, making disabled state inheritable.
7964 * @return {boolean} Widget is disabled
7966 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7967 return this.disabled
||
7968 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7972 * Set group element is in.
7974 * @param {OO.ui.GroupElement|null} group Group element, null if none
7977 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7979 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7980 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7982 // Initialize item disabled states
7983 this.updateDisabled();
7989 * Mixin that adds a menu showing suggested values for a text input.
7991 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7993 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
7994 * be aware that this will cause new suggestions to be looked up for the new value. If this is
7995 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
8001 * @param {OO.ui.TextInputWidget} input Input widget
8002 * @param {Object} [config] Configuration options
8003 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
8004 * @cfg {jQuery} [$container=input.$element] Element to render menu under
8006 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
8007 // Configuration initialization
8008 config
= config
|| {};
8011 this.lookupInput
= input
;
8012 this.$overlay
= config
.$overlay
|| this.$element
;
8013 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
8014 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
8015 input
: this.lookupInput
,
8016 $container
: config
.$container
8018 this.lookupCache
= {};
8019 this.lookupQuery
= null;
8020 this.lookupRequest
= null;
8021 this.lookupsDisabled
= false;
8022 this.lookupInputFocused
= false;
8025 this.lookupInput
.$input
.on( {
8026 focus
: this.onLookupInputFocus
.bind( this ),
8027 blur
: this.onLookupInputBlur
.bind( this ),
8028 mousedown
: this.onLookupInputMouseDown
.bind( this )
8030 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
8031 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
8034 this.$element
.addClass( 'oo-ui-lookupWidget' );
8035 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
8036 this.$overlay
.append( this.lookupMenu
.$element
);
8042 * Handle input focus event.
8044 * @param {jQuery.Event} e Input focus event
8046 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
8047 this.lookupInputFocused
= true;
8048 this.populateLookupMenu();
8052 * Handle input blur event.
8054 * @param {jQuery.Event} e Input blur event
8056 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
8057 this.closeLookupMenu();
8058 this.lookupInputFocused
= false;
8062 * Handle input mouse down event.
8064 * @param {jQuery.Event} e Input mouse down event
8066 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
8067 // Only open the menu if the input was already focused.
8068 // This way we allow the user to open the menu again after closing it with Esc
8069 // by clicking in the input. Opening (and populating) the menu when initially
8070 // clicking into the input is handled by the focus handler.
8071 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
8072 this.populateLookupMenu();
8077 * Handle input change event.
8079 * @param {string} value New input value
8081 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
8082 if ( this.lookupInputFocused
) {
8083 this.populateLookupMenu();
8088 * Handle the lookup menu being shown/hidden.
8089 * @param {boolean} visible Whether the lookup menu is now visible.
8091 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
8093 // When the menu is hidden, abort any active request and clear the menu.
8094 // This has to be done here in addition to closeLookupMenu(), because
8095 // MenuSelectWidget will close itself when the user presses Esc.
8096 this.abortLookupRequest();
8097 this.lookupMenu
.clearItems();
8104 * @return {OO.ui.TextInputMenuSelectWidget}
8106 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
8107 return this.lookupMenu
;
8111 * Disable or re-enable lookups.
8113 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
8115 * @param {boolean} disabled Disable lookups
8117 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
8118 this.lookupsDisabled
= !!disabled
;
8122 * Open the menu. If there are no entries in the menu, this does nothing.
8126 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
8127 if ( !this.lookupMenu
.isEmpty() ) {
8128 this.lookupMenu
.toggle( true );
8134 * Close the menu, empty it, and abort any pending request.
8138 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
8139 this.lookupMenu
.toggle( false );
8140 this.abortLookupRequest();
8141 this.lookupMenu
.clearItems();
8146 * Request menu items based on the input's current value, and when they arrive,
8147 * populate the menu with these items and show the menu.
8149 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
8153 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
8155 value
= this.lookupInput
.getValue();
8157 if ( this.lookupsDisabled
) {
8161 // If the input is empty, clear the menu
8162 if ( value
=== '' ) {
8163 this.closeLookupMenu();
8164 // Skip population if there is already a request pending for the current value
8165 } else if ( value
!== this.lookupQuery
) {
8166 this.getLookupMenuItems()
8167 .done( function ( items
) {
8168 widget
.lookupMenu
.clearItems();
8169 if ( items
.length
) {
8173 widget
.initializeLookupMenuSelection();
8175 widget
.lookupMenu
.toggle( false );
8178 .fail( function () {
8179 widget
.lookupMenu
.clearItems();
8187 * Select and highlight the first selectable item in the menu.
8191 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
8192 if ( !this.lookupMenu
.getSelectedItem() ) {
8193 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
8195 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
8199 * Get lookup menu items for the current query.
8201 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
8202 * of the done event. If the request was aborted to make way for a subsequent request,
8203 * this promise will not be rejected: it will remain pending forever.
8205 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
8207 value
= this.lookupInput
.getValue(),
8208 deferred
= $.Deferred(),
8211 this.abortLookupRequest();
8212 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
8213 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
8215 this.lookupInput
.pushPending();
8216 this.lookupQuery
= value
;
8217 ourRequest
= this.lookupRequest
= this.getLookupRequest();
8219 .always( function () {
8220 // We need to pop pending even if this is an old request, otherwise
8221 // the widget will remain pending forever.
8222 // TODO: this assumes that an aborted request will fail or succeed soon after
8223 // being aborted, or at least eventually. It would be nice if we could popPending()
8224 // at abort time, but only if we knew that we hadn't already called popPending()
8225 // for that request.
8226 widget
.lookupInput
.popPending();
8228 .done( function ( data
) {
8229 // If this is an old request (and aborting it somehow caused it to still succeed),
8230 // ignore its success completely
8231 if ( ourRequest
=== widget
.lookupRequest
) {
8232 widget
.lookupQuery
= null;
8233 widget
.lookupRequest
= null;
8234 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
8235 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
8238 .fail( function () {
8239 // If this is an old request (or a request failing because it's being aborted),
8240 // ignore its failure completely
8241 if ( ourRequest
=== widget
.lookupRequest
) {
8242 widget
.lookupQuery
= null;
8243 widget
.lookupRequest
= null;
8248 return deferred
.promise();
8252 * Abort the currently pending lookup request, if any.
8254 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
8255 var oldRequest
= this.lookupRequest
;
8257 // First unset this.lookupRequest to the fail handler will notice
8258 // that the request is no longer current
8259 this.lookupRequest
= null;
8260 this.lookupQuery
= null;
8266 * Get a new request object of the current lookup query value.
8269 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
8271 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
8272 // Stub, implemented in subclass
8277 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
8280 * @param {Mixed} data Cached result data, usually an array
8281 * @return {OO.ui.MenuOptionWidget[]} Menu items
8283 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
8284 // Stub, implemented in subclass
8289 * Get lookup cache item from server response data.
8292 * @param {Mixed} data Response from server
8293 * @return {Mixed} Cached result data
8295 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
8296 // Stub, implemented in subclass
8301 * Set of controls for an OO.ui.OutlineSelectWidget.
8303 * Controls include moving items up and down, removing items, and adding different kinds of items.
8306 * @extends OO.ui.Widget
8307 * @mixins OO.ui.GroupElement
8308 * @mixins OO.ui.IconElement
8311 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
8312 * @param {Object} [config] Configuration options
8314 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
8315 // Configuration initialization
8316 config
= $.extend( { icon
: 'add' }, config
);
8318 // Parent constructor
8319 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
8321 // Mixin constructors
8322 OO
.ui
.GroupElement
.call( this, config
);
8323 OO
.ui
.IconElement
.call( this, config
);
8326 this.outline
= outline
;
8327 this.$movers
= this.$( '<div>' );
8328 this.upButton
= new OO
.ui
.ButtonWidget( {
8332 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
8334 this.downButton
= new OO
.ui
.ButtonWidget( {
8338 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
8340 this.removeButton
= new OO
.ui
.ButtonWidget( {
8344 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
8348 outline
.connect( this, {
8349 select
: 'onOutlineChange',
8350 add
: 'onOutlineChange',
8351 remove
: 'onOutlineChange'
8353 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
8354 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
8355 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
8358 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
8359 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
8361 .addClass( 'oo-ui-outlineControlsWidget-movers' )
8362 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
8363 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
8368 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
8369 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
8370 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
8376 * @param {number} places Number of places to move
8386 * Handle outline change events.
8388 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
8389 var i
, len
, firstMovable
, lastMovable
,
8390 items
= this.outline
.getItems(),
8391 selectedItem
= this.outline
.getSelectedItem(),
8392 movable
= selectedItem
&& selectedItem
.isMovable(),
8393 removable
= selectedItem
&& selectedItem
.isRemovable();
8398 while ( ++i
< len
) {
8399 if ( items
[i
].isMovable() ) {
8400 firstMovable
= items
[i
];
8406 if ( items
[i
].isMovable() ) {
8407 lastMovable
= items
[i
];
8412 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
8413 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
8414 this.removeButton
.setDisabled( !removable
);
8418 * Mixin for widgets with a boolean on/off state.
8424 * @param {Object} [config] Configuration options
8425 * @cfg {boolean} [value=false] Initial value
8427 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8428 // Configuration initialization
8429 config
= config
|| {};
8435 this.$element
.addClass( 'oo-ui-toggleWidget' );
8436 this.setValue( !!config
.value
);
8443 * @param {boolean} value Changed value
8449 * Get the value of the toggle.
8453 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8458 * Set the value of the toggle.
8460 * @param {boolean} value New value
8464 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8466 if ( this.value
!== value
) {
8468 this.emit( 'change', value
);
8469 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8470 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8476 * Group widget for multiple related buttons.
8478 * Use together with OO.ui.ButtonWidget.
8481 * @extends OO.ui.Widget
8482 * @mixins OO.ui.GroupElement
8485 * @param {Object} [config] Configuration options
8486 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
8488 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
8489 // Configuration initialization
8490 config
= config
|| {};
8492 // Parent constructor
8493 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
8495 // Mixin constructors
8496 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8499 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
8500 if ( $.isArray( config
.items
) ) {
8501 this.addItems( config
.items
);
8507 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
8508 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
8511 * Generic widget for buttons.
8514 * @extends OO.ui.Widget
8515 * @mixins OO.ui.ButtonElement
8516 * @mixins OO.ui.IconElement
8517 * @mixins OO.ui.IndicatorElement
8518 * @mixins OO.ui.LabelElement
8519 * @mixins OO.ui.TitledElement
8520 * @mixins OO.ui.FlaggedElement
8523 * @param {Object} [config] Configuration options
8524 * @cfg {string} [href] Hyperlink to visit when clicked
8525 * @cfg {string} [target] Target to open hyperlink in
8527 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8528 // Configuration initialization
8529 config
= $.extend( { target
: '_blank' }, config
);
8531 // Parent constructor
8532 OO
.ui
.ButtonWidget
.super.call( this, config
);
8534 // Mixin constructors
8535 OO
.ui
.ButtonElement
.call( this, config
);
8536 OO
.ui
.IconElement
.call( this, config
);
8537 OO
.ui
.IndicatorElement
.call( this, config
);
8538 OO
.ui
.LabelElement
.call( this, config
);
8539 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
8540 OO
.ui
.FlaggedElement
.call( this, config
);
8545 this.isHyperlink
= false;
8549 click
: this.onClick
.bind( this ),
8550 keypress
: this.onKeyPress
.bind( this )
8554 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8556 .addClass( 'oo-ui-buttonWidget' )
8557 .append( this.$button
);
8558 this.setHref( config
.href
);
8559 this.setTarget( config
.target
);
8564 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8565 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8566 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8567 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8568 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8569 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8570 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8581 * Handles mouse click events.
8583 * @param {jQuery.Event} e Mouse click event
8586 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8587 if ( !this.isDisabled() ) {
8588 this.emit( 'click' );
8589 if ( this.isHyperlink
) {
8597 * Handles keypress events.
8599 * @param {jQuery.Event} e Keypress event
8602 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8603 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8604 this.emit( 'click' );
8605 if ( this.isHyperlink
) {
8613 * Get hyperlink location.
8615 * @return {string} Hyperlink location
8617 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8622 * Get hyperlink target.
8624 * @return {string} Hyperlink target
8626 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8631 * Set hyperlink location.
8633 * @param {string|null} href Hyperlink location, null to remove
8635 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8636 href
= typeof href
=== 'string' ? href
: null;
8638 if ( href
!== this.href
) {
8640 if ( href
!== null ) {
8641 this.$button
.attr( 'href', href
);
8642 this.isHyperlink
= true;
8644 this.$button
.removeAttr( 'href' );
8645 this.isHyperlink
= false;
8653 * Set hyperlink target.
8655 * @param {string|null} target Hyperlink target, null to remove
8657 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8658 target
= typeof target
=== 'string' ? target
: null;
8660 if ( target
!== this.target
) {
8661 this.target
= target
;
8662 if ( target
!== null ) {
8663 this.$button
.attr( 'target', target
);
8665 this.$button
.removeAttr( 'target' );
8673 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8676 * @extends OO.ui.ButtonWidget
8677 * @mixins OO.ui.PendingElement
8680 * @param {Object} [config] Configuration options
8681 * @cfg {string} [action] Symbolic action name
8682 * @cfg {string[]} [modes] Symbolic mode names
8683 * @cfg {boolean} [framed=false] Render button with a frame
8685 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8686 // Configuration initialization
8687 config
= $.extend( { framed
: false }, config
);
8689 // Parent constructor
8690 OO
.ui
.ActionWidget
.super.call( this, config
);
8692 // Mixin constructors
8693 OO
.ui
.PendingElement
.call( this, config
);
8696 this.action
= config
.action
|| '';
8697 this.modes
= config
.modes
|| [];
8702 this.$element
.addClass( 'oo-ui-actionWidget' );
8707 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8708 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
8719 * Check if action is available in a certain mode.
8721 * @param {string} mode Name of mode
8722 * @return {boolean} Has mode
8724 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8725 return this.modes
.indexOf( mode
) !== -1;
8729 * Get symbolic action name.
8733 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8738 * Get symbolic action name.
8742 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8743 return this.modes
.slice();
8747 * Emit a resize event if the size has changed.
8751 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8754 if ( this.isElementAttached() ) {
8755 width
= this.$element
.width();
8756 height
= this.$element
.height();
8758 if ( width
!== this.width
|| height
!== this.height
) {
8760 this.height
= height
;
8761 this.emit( 'resize' );
8771 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8773 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8774 this.propagateResize();
8782 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8784 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8785 this.propagateResize();
8793 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8795 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8796 this.propagateResize();
8804 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8806 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8807 this.propagateResize();
8813 * Toggle visibility of button.
8815 * @param {boolean} [show] Show button, omit to toggle visibility
8818 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8820 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8821 this.propagateResize();
8827 * Button that shows and hides a popup.
8830 * @extends OO.ui.ButtonWidget
8831 * @mixins OO.ui.PopupElement
8834 * @param {Object} [config] Configuration options
8836 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8837 // Parent constructor
8838 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8840 // Mixin constructors
8841 OO
.ui
.PopupElement
.call( this, config
);
8845 .addClass( 'oo-ui-popupButtonWidget' )
8846 .append( this.popup
.$element
);
8851 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8852 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8857 * Handles mouse click events.
8859 * @param {jQuery.Event} e Mouse click event
8861 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8862 // Skip clicks within the popup
8863 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8867 if ( !this.isDisabled() ) {
8868 this.popup
.toggle();
8870 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8876 * Button that toggles on and off.
8879 * @extends OO.ui.ButtonWidget
8880 * @mixins OO.ui.ToggleWidget
8883 * @param {Object} [config] Configuration options
8884 * @cfg {boolean} [value=false] Initial value
8886 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8887 // Configuration initialization
8888 config
= config
|| {};
8890 // Parent constructor
8891 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8893 // Mixin constructors
8894 OO
.ui
.ToggleWidget
.call( this, config
);
8897 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8902 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8903 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8910 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8911 if ( !this.isDisabled() ) {
8912 this.setValue( !this.value
);
8916 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8922 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8924 if ( value
!== this.value
) {
8925 this.setActive( value
);
8928 // Parent method (from mixin)
8929 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8935 * Dropdown menu of options.
8937 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
8938 * can be accessed using the #getMenu method.
8940 * Use with OO.ui.MenuOptionWidget.
8943 * @extends OO.ui.Widget
8944 * @mixins OO.ui.IconElement
8945 * @mixins OO.ui.IndicatorElement
8946 * @mixins OO.ui.LabelElement
8947 * @mixins OO.ui.TitledElement
8950 * @param {Object} [config] Configuration options
8951 * @cfg {Object} [menu] Configuration options to pass to menu widget
8953 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
8954 // Configuration initialization
8955 config
= $.extend( { indicator
: 'down' }, config
);
8957 // Parent constructor
8958 OO
.ui
.DropdownWidget
.super.call( this, config
);
8960 // Mixin constructors
8961 OO
.ui
.IconElement
.call( this, config
);
8962 OO
.ui
.IndicatorElement
.call( this, config
);
8963 OO
.ui
.LabelElement
.call( this, config
);
8964 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8967 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8968 this.$handle
= this.$( '<span>' );
8971 this.$element
.on( { click
: this.onClick
.bind( this ) } );
8972 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8976 .addClass( 'oo-ui-dropdownWidget-handle' )
8977 .append( this.$icon
, this.$label
, this.$indicator
);
8979 .addClass( 'oo-ui-dropdownWidget' )
8980 .append( this.$handle
, this.menu
.$element
);
8985 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
8986 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
8987 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
8988 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
8989 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
8996 * @return {OO.ui.MenuSelectWidget} Menu of widget
8998 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
9003 * Handles menu select events.
9005 * @param {OO.ui.MenuOptionWidget} item Selected menu item
9007 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
9014 selectedLabel
= item
.getLabel();
9016 // If the label is a DOM element, clone it, because setLabel will append() it
9017 if ( selectedLabel
instanceof jQuery
) {
9018 selectedLabel
= selectedLabel
.clone();
9021 this.setLabel( selectedLabel
);
9025 * Handles mouse click events.
9027 * @param {jQuery.Event} e Mouse click event
9029 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
9030 // Skip clicks within the menu
9031 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
9035 if ( !this.isDisabled() ) {
9036 if ( this.menu
.isVisible() ) {
9037 this.menu
.toggle( false );
9039 this.menu
.toggle( true );
9048 * See OO.ui.IconElement for more information.
9051 * @extends OO.ui.Widget
9052 * @mixins OO.ui.IconElement
9053 * @mixins OO.ui.TitledElement
9056 * @param {Object} [config] Configuration options
9058 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
9059 // Configuration initialization
9060 config
= config
|| {};
9062 // Parent constructor
9063 OO
.ui
.IconWidget
.super.call( this, config
);
9065 // Mixin constructors
9066 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
9067 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9070 this.$element
.addClass( 'oo-ui-iconWidget' );
9075 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
9076 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
9077 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
9079 /* Static Properties */
9081 OO
.ui
.IconWidget
.static.tagName
= 'span';
9086 * See OO.ui.IndicatorElement for more information.
9089 * @extends OO.ui.Widget
9090 * @mixins OO.ui.IndicatorElement
9091 * @mixins OO.ui.TitledElement
9094 * @param {Object} [config] Configuration options
9096 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
9097 // Configuration initialization
9098 config
= config
|| {};
9100 // Parent constructor
9101 OO
.ui
.IndicatorWidget
.super.call( this, config
);
9103 // Mixin constructors
9104 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
9105 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9108 this.$element
.addClass( 'oo-ui-indicatorWidget' );
9113 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
9114 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
9115 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
9117 /* Static Properties */
9119 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
9122 * Base class for input widgets.
9126 * @extends OO.ui.Widget
9127 * @mixins OO.ui.FlaggedElement
9130 * @param {Object} [config] Configuration options
9131 * @cfg {string} [name=''] HTML input name
9132 * @cfg {string} [value=''] Input value
9133 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
9135 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
9136 // Configuration initialization
9137 config
= config
|| {};
9139 // Parent constructor
9140 OO
.ui
.InputWidget
.super.call( this, config
);
9142 // Mixin constructors
9143 OO
.ui
.FlaggedElement
.call( this, config
);
9146 this.$input
= this.getInputElement( config
);
9148 this.inputFilter
= config
.inputFilter
;
9151 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
9155 .attr( 'name', config
.name
)
9156 .prop( 'disabled', this.isDisabled() );
9157 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
9158 this.setValue( config
.value
);
9163 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
9164 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
9170 * @param {string} value
9176 * Get input element.
9179 * @param {Object} [config] Configuration options
9180 * @return {jQuery} Input element
9182 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
9183 return this.$( '<input>' );
9187 * Handle potentially value-changing events.
9189 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
9191 OO
.ui
.InputWidget
.prototype.onEdit = function () {
9193 if ( !this.isDisabled() ) {
9194 // Allow the stack to clear so the value will be updated
9195 setTimeout( function () {
9196 widget
.setValue( widget
.$input
.val() );
9202 * Get the value of the input.
9204 * @return {string} Input value
9206 OO
.ui
.InputWidget
.prototype.getValue = function () {
9211 * Sets the direction of the current input, either RTL or LTR
9213 * @param {boolean} isRTL
9215 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
9217 this.$input
.removeClass( 'oo-ui-ltr' );
9218 this.$input
.addClass( 'oo-ui-rtl' );
9220 this.$input
.removeClass( 'oo-ui-rtl' );
9221 this.$input
.addClass( 'oo-ui-ltr' );
9226 * Set the value of the input.
9228 * @param {string} value New value
9232 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
9233 value
= this.cleanUpValue( value
);
9234 if ( this.value
!== value
) {
9236 this.emit( 'change', this.value
);
9238 // Update the DOM if it has changed. Note that with cleanUpValue, it
9239 // is possible for the DOM value to change without this.value changing.
9240 if ( this.$input
.val() !== this.value
) {
9241 this.$input
.val( this.value
);
9247 * Clean up incoming value.
9249 * Ensures value is a string, and converts undefined and null to empty string.
9252 * @param {string} value Original value
9253 * @return {string} Cleaned up value
9255 OO
.ui
.InputWidget
.prototype.cleanUpValue = function ( value
) {
9256 if ( value
=== undefined || value
=== null ) {
9258 } else if ( this.inputFilter
) {
9259 return this.inputFilter( String( value
) );
9261 return String( value
);
9266 * Simulate the behavior of clicking on a label bound to this input.
9268 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
9269 if ( !this.isDisabled() ) {
9270 if ( this.$input
.is( ':checkbox,:radio' ) ) {
9271 this.$input
.click();
9272 } else if ( this.$input
.is( ':input' ) ) {
9273 this.$input
[0].focus();
9281 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
9282 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
9283 if ( this.$input
) {
9284 this.$input
.prop( 'disabled', this.isDisabled() );
9294 OO
.ui
.InputWidget
.prototype.focus = function () {
9295 this.$input
[0].focus();
9304 OO
.ui
.InputWidget
.prototype.blur = function () {
9305 this.$input
[0].blur();
9310 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
9313 * @extends OO.ui.InputWidget
9314 * @mixins OO.ui.ButtonElement
9315 * @mixins OO.ui.IconElement
9316 * @mixins OO.ui.IndicatorElement
9317 * @mixins OO.ui.LabelElement
9318 * @mixins OO.ui.TitledElement
9319 * @mixins OO.ui.FlaggedElement
9322 * @param {Object} [config] Configuration options
9323 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
9324 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
9325 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
9326 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
9327 * won't be possible to set a value (which will internally become identical to the label).
9329 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
9330 // Configuration initialization
9331 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
9333 // Properties (must be set before parent constructor, which calls #setValue)
9334 this.useInputTag
= config
.useInputTag
;
9336 // Parent constructor
9337 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
9339 // Mixin constructors
9340 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
9341 OO
.ui
.IconElement
.call( this, config
);
9342 OO
.ui
.IndicatorElement
.call( this, config
);
9343 OO
.ui
.LabelElement
.call( this, config
);
9344 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
9345 OO
.ui
.FlaggedElement
.call( this, config
);
9349 click
: this.onClick
.bind( this ),
9350 keypress
: this.onKeyPress
.bind( this )
9354 if ( !config
.useInputTag
) {
9355 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
9357 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
9362 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
9363 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
9364 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
9365 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
9366 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
9367 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
9368 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
9379 * Get input element.
9382 * @param {Object} [config] Configuration options
9383 * @return {jQuery} Input element
9385 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
9386 // Configuration initialization
9387 config
= config
|| {};
9389 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
9391 return this.$( html
);
9397 * Overridden to support setting the 'value' of `<input/>` elements.
9399 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
9400 * text; or null for no label
9403 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
9404 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
9406 if ( this.useInputTag
) {
9407 if ( typeof label
=== 'function' ) {
9408 label
= OO
.ui
.resolveMsg( label
);
9410 if ( label
instanceof jQuery
) {
9411 label
= label
.text();
9416 this.$input
.val( label
);
9423 * Set the value of the input.
9425 * Overridden to disable for `<input/>` elements, which have value identical to the label.
9427 * @param {string} value New value
9430 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
9431 if ( !this.useInputTag
) {
9432 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
9438 * Handles mouse click events.
9440 * @param {jQuery.Event} e Mouse click event
9443 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
9444 if ( !this.isDisabled() ) {
9445 this.emit( 'click' );
9451 * Handles keypress events.
9453 * @param {jQuery.Event} e Keypress event
9456 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
9457 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9458 this.emit( 'click' );
9464 * Checkbox input widget.
9467 * @extends OO.ui.InputWidget
9470 * @param {Object} [config] Configuration options
9472 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
9473 // Parent constructor
9474 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
9477 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
9482 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
9487 * Get input element.
9490 * @return {jQuery} Input element
9492 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
9493 return this.$( '<input type="checkbox" />' );
9497 * Get checked state of the checkbox
9499 * @return {boolean} If the checkbox is checked
9501 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
9506 * Set checked state of the checkbox
9508 * @param {boolean} value New value
9510 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
9512 if ( this.value
!== value
) {
9514 this.$input
.prop( 'checked', this.value
);
9515 this.emit( 'change', this.value
);
9522 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
9524 if ( !this.isDisabled() ) {
9525 // Allow the stack to clear so the value will be updated
9526 setTimeout( function () {
9527 widget
.setValue( widget
.$input
.prop( 'checked' ) );
9533 * Radio input widget.
9535 * Radio buttons only make sense as a set, and you probably want to use the OO.ui.RadioSelectWidget
9536 * class instead of using this class directly.
9538 * This class doesn't make it possible to learn whether the radio button is selected ("pressed").
9541 * @extends OO.ui.InputWidget
9544 * @param {Object} [config] Configuration options
9545 * @param {boolean} [config.selected=false] Whether the radio button is initially selected
9547 OO
.ui
.RadioInputWidget
= function OoUiRadioInputWidget( config
) {
9548 // Parent constructor
9549 OO
.ui
.RadioInputWidget
.super.call( this, config
);
9552 this.$element
.addClass( 'oo-ui-radioInputWidget' );
9553 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
9558 OO
.inheritClass( OO
.ui
.RadioInputWidget
, OO
.ui
.InputWidget
);
9563 * Get input element.
9566 * @return {jQuery} Input element
9568 OO
.ui
.RadioInputWidget
.prototype.getInputElement = function () {
9569 return this.$( '<input type="radio" />' );
9575 OO
.ui
.RadioInputWidget
.prototype.onEdit = function () {
9576 // RadioInputWidget doesn't track its state.
9580 * Set selection state of this radio button.
9582 * @param {boolean} state Whether the button is selected
9585 OO
.ui
.RadioInputWidget
.prototype.setSelected = function ( state
) {
9586 // RadioInputWidget doesn't track its state.
9587 this.$input
.prop( 'checked', state
);
9592 * Check if this radio button is selected.
9594 * @return {boolean} Radio is selected
9596 OO
.ui
.RadioInputWidget
.prototype.isSelected = function () {
9597 return this.$input
.prop( 'checked' );
9601 * Input widget with a text field.
9604 * @extends OO.ui.InputWidget
9605 * @mixins OO.ui.IconElement
9606 * @mixins OO.ui.IndicatorElement
9607 * @mixins OO.ui.PendingElement
9610 * @param {Object} [config] Configuration options
9611 * @cfg {string} [type='text'] HTML tag `type` attribute
9612 * @cfg {string} [placeholder] Placeholder text
9613 * @cfg {boolean} [readOnly=false] Prevent changes
9614 * @cfg {boolean} [multiline=false] Allow multiple lines of text
9615 * @cfg {boolean} [autosize=false] Automatically resize to fit content
9616 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
9617 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
9618 * one, see #static-validationPatterns)
9620 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
9621 // Configuration initialization
9622 config
= $.extend( { readOnly
: false }, config
);
9624 // Parent constructor
9625 OO
.ui
.TextInputWidget
.super.call( this, config
);
9627 // Mixin constructors
9628 OO
.ui
.IconElement
.call( this, config
);
9629 OO
.ui
.IndicatorElement
.call( this, config
);
9630 OO
.ui
.PendingElement
.call( this, config
);
9633 this.readOnly
= false;
9634 this.multiline
= !!config
.multiline
;
9635 this.autosize
= !!config
.autosize
;
9636 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
9637 this.validate
= null;
9639 this.setValidation( config
.validate
);
9643 keypress
: this.onKeyPress
.bind( this ),
9644 blur
: this.setValidityFlag
.bind( this )
9646 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
9647 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
9648 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
9652 .addClass( 'oo-ui-textInputWidget' )
9653 .append( this.$icon
, this.$indicator
);
9654 this.setReadOnly( config
.readOnly
);
9655 if ( config
.placeholder
) {
9656 this.$input
.attr( 'placeholder', config
.placeholder
);
9658 this.$element
.attr( 'role', 'textbox' );
9663 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
9664 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
9665 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
9666 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
9668 /* Static properties */
9670 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
9678 * User presses enter inside the text box.
9680 * Not called if input is multiline.
9686 * User clicks the icon.
9692 * User clicks the indicator.
9700 * Handle icon mouse down events.
9702 * @param {jQuery.Event} e Mouse down event
9705 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
9706 if ( e
.which
=== 1 ) {
9707 this.$input
[0].focus();
9708 this.emit( 'icon' );
9714 * Handle indicator mouse down events.
9716 * @param {jQuery.Event} e Mouse down event
9719 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
9720 if ( e
.which
=== 1 ) {
9721 this.$input
[0].focus();
9722 this.emit( 'indicator' );
9728 * Handle key press events.
9730 * @param {jQuery.Event} e Key press event
9731 * @fires enter If enter key is pressed and input is not multiline
9733 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
9734 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
9735 this.emit( 'enter' );
9740 * Handle element attach events.
9742 * @param {jQuery.Event} e Element attach event
9744 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
9751 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
9755 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9761 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9763 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9765 this.setValidityFlag();
9771 * Check if the widget is read-only.
9775 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
9776 return this.readOnly
;
9780 * Set the read-only state of the widget.
9782 * This should probably change the widget's appearance and prevent it from being used.
9784 * @param {boolean} state Make input read-only
9787 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
9788 this.readOnly
= !!state
;
9789 this.$input
.prop( 'readOnly', this.readOnly
);
9794 * Automatically adjust the size of the text input.
9796 * This only affects multi-line inputs that are auto-sized.
9800 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9801 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
9803 if ( this.multiline
&& this.autosize
) {
9804 $clone
= this.$input
.clone()
9805 .val( this.$input
.val() )
9806 // Set inline height property to 0 to measure scroll height
9807 .css( { height
: 0 } )
9808 .insertAfter( this.$input
);
9809 scrollHeight
= $clone
[0].scrollHeight
;
9810 // Remove inline height property to measure natural heights
9811 $clone
.css( 'height', '' );
9812 innerHeight
= $clone
.innerHeight();
9813 outerHeight
= $clone
.outerHeight();
9814 // Measure max rows height
9815 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' ).val( '' );
9816 maxInnerHeight
= $clone
.innerHeight();
9817 // Difference between reported innerHeight and scrollHeight with no scrollbars present
9818 // Equals 1 on Blink-based browsers and 0 everywhere else
9819 measurementError
= maxInnerHeight
- $clone
[0].scrollHeight
;
9821 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
9822 // Only apply inline height when expansion beyond natural height is needed
9823 if ( idealHeight
> innerHeight
) {
9824 // Use the difference between the inner and outer height as a buffer
9825 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
9827 this.$input
.css( 'height', '' );
9834 * Get input element.
9837 * @param {Object} [config] Configuration options
9838 * @return {jQuery} Input element
9840 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9841 // Configuration initialization
9842 config
= config
|| {};
9844 var type
= config
.type
|| 'text';
9846 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + type
+ '" />' );
9850 * Check if input supports multiple lines.
9854 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9855 return !!this.multiline
;
9859 * Check if input automatically adjusts its size.
9863 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9864 return !!this.autosize
;
9868 * Select the contents of the input.
9872 OO
.ui
.TextInputWidget
.prototype.select = function () {
9873 this.$input
.select();
9878 * Sets the validation pattern to use.
9879 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
9880 * one, see #static-validationPatterns)
9882 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
9883 if ( validate
instanceof RegExp
) {
9884 this.validate
= validate
;
9886 this.validate
= this.constructor.static.validationPatterns
[validate
] || /.*/;
9891 * Sets the 'invalid' flag appropriately.
9893 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
9895 this.isValid().done( function ( valid
) {
9896 widget
.setFlags( { invalid
: !valid
} );
9901 * Returns whether or not the current value is considered valid, according to the
9902 * supplied validation pattern.
9904 * @return {jQuery.Deferred}
9906 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
9907 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
9911 * Text input with a menu of optional values.
9914 * @extends OO.ui.Widget
9917 * @param {Object} [config] Configuration options
9918 * @cfg {Object} [menu] Configuration options to pass to menu widget
9919 * @cfg {Object} [input] Configuration options to pass to input widget
9920 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
9922 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9923 // Configuration initialization
9924 config
= config
|| {};
9926 // Parent constructor
9927 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9930 this.$overlay
= config
.$overlay
|| this.$element
;
9931 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9932 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9935 this.menu
= new OO
.ui
.TextInputMenuSelectWidget( this.input
, $.extend(
9937 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
9940 disabled
: this.isDisabled()
9946 this.input
.connect( this, {
9947 change
: 'onInputChange',
9948 indicator
: 'onInputIndicator',
9949 enter
: 'onInputEnter'
9951 this.menu
.connect( this, {
9952 choose
: 'onMenuChoose',
9953 add
: 'onMenuItemsChange',
9954 remove
: 'onMenuItemsChange'
9958 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
9959 this.$overlay
.append( this.menu
.$element
);
9960 this.onMenuItemsChange();
9965 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9970 * Get the combobox's menu.
9971 * @return {OO.ui.TextInputMenuSelectWidget} Menu widget
9973 OO
.ui
.ComboBoxWidget
.prototype.getMenu = function () {
9978 * Handle input change events.
9980 * @param {string} value New value
9982 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9983 var match
= this.menu
.getItemFromData( value
);
9985 this.menu
.selectItem( match
);
9987 if ( !this.isDisabled() ) {
9988 this.menu
.toggle( true );
9993 * Handle input indicator events.
9995 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9996 if ( !this.isDisabled() ) {
10002 * Handle input enter events.
10004 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
10005 if ( !this.isDisabled() ) {
10006 this.menu
.toggle( false );
10011 * Handle menu choose events.
10013 * @param {OO.ui.OptionWidget} item Chosen item
10015 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
10017 this.input
.setValue( item
.getData() );
10022 * Handle menu item change events.
10024 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
10025 var match
= this.menu
.getItemFromData( this.input
.getValue() );
10026 this.menu
.selectItem( match
);
10027 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
10033 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
10035 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
10037 if ( this.input
) {
10038 this.input
.setDisabled( this.isDisabled() );
10041 this.menu
.setDisabled( this.isDisabled() );
10051 * @extends OO.ui.Widget
10052 * @mixins OO.ui.LabelElement
10055 * @param {Object} [config] Configuration options
10057 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
10058 // Configuration initialization
10059 config
= config
|| {};
10061 // Parent constructor
10062 OO
.ui
.LabelWidget
.super.call( this, config
);
10064 // Mixin constructors
10065 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
10066 OO
.ui
.TitledElement
.call( this, config
);
10069 this.input
= config
.input
;
10072 if ( this.input
instanceof OO
.ui
.InputWidget
) {
10073 this.$element
.on( 'click', this.onClick
.bind( this ) );
10077 this.$element
.addClass( 'oo-ui-labelWidget' );
10082 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
10083 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
10084 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
10086 /* Static Properties */
10088 OO
.ui
.LabelWidget
.static.tagName
= 'span';
10093 * Handles label mouse click events.
10095 * @param {jQuery.Event} e Mouse click event
10097 OO
.ui
.LabelWidget
.prototype.onClick = function () {
10098 this.input
.simulateLabelClick();
10103 * Generic option widget for use with OO.ui.SelectWidget.
10106 * @extends OO.ui.Widget
10107 * @mixins OO.ui.LabelElement
10108 * @mixins OO.ui.FlaggedElement
10111 * @param {Object} [config] Configuration options
10113 OO
.ui
.OptionWidget
= function OoUiOptionWidget( config
) {
10114 // Configuration initialization
10115 config
= config
|| {};
10117 // Parent constructor
10118 OO
.ui
.OptionWidget
.super.call( this, config
);
10120 // Mixin constructors
10121 OO
.ui
.ItemWidget
.call( this );
10122 OO
.ui
.LabelElement
.call( this, config
);
10123 OO
.ui
.FlaggedElement
.call( this, config
);
10126 this.selected
= false;
10127 this.highlighted
= false;
10128 this.pressed
= false;
10132 .data( 'oo-ui-optionWidget', this )
10133 .attr( 'role', 'option' )
10134 .addClass( 'oo-ui-optionWidget' )
10135 .append( this.$label
);
10140 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
10141 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
10142 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
10143 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
10145 /* Static Properties */
10147 OO
.ui
.OptionWidget
.static.selectable
= true;
10149 OO
.ui
.OptionWidget
.static.highlightable
= true;
10151 OO
.ui
.OptionWidget
.static.pressable
= true;
10153 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
10158 * Check if option can be selected.
10160 * @return {boolean} Item is selectable
10162 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
10163 return this.constructor.static.selectable
&& !this.isDisabled();
10167 * Check if option can be highlighted.
10169 * @return {boolean} Item is highlightable
10171 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
10172 return this.constructor.static.highlightable
&& !this.isDisabled();
10176 * Check if option can be pressed.
10178 * @return {boolean} Item is pressable
10180 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
10181 return this.constructor.static.pressable
&& !this.isDisabled();
10185 * Check if option is selected.
10187 * @return {boolean} Item is selected
10189 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
10190 return this.selected
;
10194 * Check if option is highlighted.
10196 * @return {boolean} Item is highlighted
10198 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
10199 return this.highlighted
;
10203 * Check if option is pressed.
10205 * @return {boolean} Item is pressed
10207 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
10208 return this.pressed
;
10212 * Set selected state.
10214 * @param {boolean} [state=false] Select option
10217 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
10218 if ( this.constructor.static.selectable
) {
10219 this.selected
= !!state
;
10220 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
10221 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
10222 this.scrollElementIntoView();
10224 this.updateThemeClasses();
10230 * Set highlighted state.
10232 * @param {boolean} [state=false] Highlight option
10235 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
10236 if ( this.constructor.static.highlightable
) {
10237 this.highlighted
= !!state
;
10238 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
10239 this.updateThemeClasses();
10245 * Set pressed state.
10247 * @param {boolean} [state=false] Press option
10250 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
10251 if ( this.constructor.static.pressable
) {
10252 this.pressed
= !!state
;
10253 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
10254 this.updateThemeClasses();
10260 * Make the option's highlight flash.
10262 * While flashing, the visual style of the pressed state is removed if present.
10264 * @return {jQuery.Promise} Promise resolved when flashing is done
10266 OO
.ui
.OptionWidget
.prototype.flash = function () {
10268 $element
= this.$element
,
10269 deferred
= $.Deferred();
10271 if ( !this.isDisabled() && this.constructor.static.pressable
) {
10272 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
10273 setTimeout( function () {
10274 // Restore original classes
10276 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
10277 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
10279 setTimeout( function () {
10280 deferred
.resolve();
10286 return deferred
.promise();
10290 * Option widget with an option icon and indicator.
10292 * Use together with OO.ui.SelectWidget.
10295 * @extends OO.ui.OptionWidget
10296 * @mixins OO.ui.IconElement
10297 * @mixins OO.ui.IndicatorElement
10300 * @param {Object} [config] Configuration options
10302 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( config
) {
10303 // Parent constructor
10304 OO
.ui
.DecoratedOptionWidget
.super.call( this, config
);
10306 // Mixin constructors
10307 OO
.ui
.IconElement
.call( this, config
);
10308 OO
.ui
.IndicatorElement
.call( this, config
);
10312 .addClass( 'oo-ui-decoratedOptionWidget' )
10313 .prepend( this.$icon
)
10314 .append( this.$indicator
);
10319 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
10320 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
10321 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
10324 * Option widget that looks like a button.
10326 * Use together with OO.ui.ButtonSelectWidget.
10329 * @extends OO.ui.DecoratedOptionWidget
10330 * @mixins OO.ui.ButtonElement
10333 * @param {Object} [config] Configuration options
10335 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
10336 // Parent constructor
10337 OO
.ui
.ButtonOptionWidget
.super.call( this, config
);
10339 // Mixin constructors
10340 OO
.ui
.ButtonElement
.call( this, config
);
10343 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
10344 this.$button
.append( this.$element
.contents() );
10345 this.$element
.append( this.$button
);
10350 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10351 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
10353 /* Static Properties */
10355 // Allow button mouse down events to pass through so they can be handled by the parent select widget
10356 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
10363 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
10364 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
10366 if ( this.constructor.static.selectable
) {
10367 this.setActive( state
);
10374 * Option widget that looks like a radio button.
10376 * Use together with OO.ui.RadioSelectWidget.
10379 * @extends OO.ui.OptionWidget
10380 * @mixins OO.ui.ButtonElement
10383 * @param {Object} [config] Configuration options
10385 OO
.ui
.RadioOptionWidget
= function OoUiRadioOptionWidget( config
) {
10386 // Parent constructor
10387 OO
.ui
.RadioOptionWidget
.super.call( this, config
);
10390 this.radio
= new OO
.ui
.RadioInputWidget( { value
: config
.data
} );
10394 .addClass( 'oo-ui-radioOptionWidget' )
10395 .prepend( this.radio
.$element
);
10400 OO
.inheritClass( OO
.ui
.RadioOptionWidget
, OO
.ui
.OptionWidget
);
10402 /* Static Properties */
10404 OO
.ui
.RadioOptionWidget
.static.highlightable
= false;
10406 OO
.ui
.RadioOptionWidget
.static.pressable
= false;
10413 OO
.ui
.RadioOptionWidget
.prototype.setSelected = function ( state
) {
10414 OO
.ui
.RadioOptionWidget
.super.prototype.setSelected
.call( this, state
);
10416 this.radio
.setSelected( state
);
10422 * Item of an OO.ui.MenuSelectWidget.
10425 * @extends OO.ui.DecoratedOptionWidget
10428 * @param {Object} [config] Configuration options
10430 OO
.ui
.MenuOptionWidget
= function OoUiMenuOptionWidget( config
) {
10431 // Configuration initialization
10432 config
= $.extend( { icon
: 'check' }, config
);
10434 // Parent constructor
10435 OO
.ui
.MenuOptionWidget
.super.call( this, config
);
10439 .attr( 'role', 'menuitem' )
10440 .addClass( 'oo-ui-menuOptionWidget' );
10445 OO
.inheritClass( OO
.ui
.MenuOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10448 * Section to group one or more items in a OO.ui.MenuSelectWidget.
10451 * @extends OO.ui.DecoratedOptionWidget
10454 * @param {Object} [config] Configuration options
10456 OO
.ui
.MenuSectionOptionWidget
= function OoUiMenuSectionOptionWidget( config
) {
10457 // Parent constructor
10458 OO
.ui
.MenuSectionOptionWidget
.super.call( this, config
);
10461 this.$element
.addClass( 'oo-ui-menuSectionOptionWidget' );
10466 OO
.inheritClass( OO
.ui
.MenuSectionOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10468 /* Static Properties */
10470 OO
.ui
.MenuSectionOptionWidget
.static.selectable
= false;
10472 OO
.ui
.MenuSectionOptionWidget
.static.highlightable
= false;
10475 * Items for an OO.ui.OutlineSelectWidget.
10478 * @extends OO.ui.DecoratedOptionWidget
10481 * @param {Object} [config] Configuration options
10482 * @cfg {number} [level] Indentation level
10483 * @cfg {boolean} [movable] Allow modification from outline controls
10485 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
10486 // Configuration initialization
10487 config
= config
|| {};
10489 // Parent constructor
10490 OO
.ui
.OutlineOptionWidget
.super.call( this, config
);
10494 this.movable
= !!config
.movable
;
10495 this.removable
= !!config
.removable
;
10498 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
10499 this.setLevel( config
.level
);
10504 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10506 /* Static Properties */
10508 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
10510 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
10512 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
10514 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
10519 * Check if item is movable.
10521 * Movability is used by outline controls.
10523 * @return {boolean} Item is movable
10525 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
10526 return this.movable
;
10530 * Check if item is removable.
10532 * Removability is used by outline controls.
10534 * @return {boolean} Item is removable
10536 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
10537 return this.removable
;
10541 * Get indentation level.
10543 * @return {number} Indentation level
10545 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
10552 * Movability is used by outline controls.
10554 * @param {boolean} movable Item is movable
10557 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
10558 this.movable
= !!movable
;
10559 this.updateThemeClasses();
10564 * Set removability.
10566 * Removability is used by outline controls.
10568 * @param {boolean} movable Item is removable
10571 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
10572 this.removable
= !!removable
;
10573 this.updateThemeClasses();
10578 * Set indentation level.
10580 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
10583 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
10584 var levels
= this.constructor.static.levels
,
10585 levelClass
= this.constructor.static.levelClass
,
10588 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
10590 if ( this.level
=== i
) {
10591 this.$element
.addClass( levelClass
+ i
);
10593 this.$element
.removeClass( levelClass
+ i
);
10596 this.updateThemeClasses();
10602 * Container for content that is overlaid and positioned absolutely.
10605 * @extends OO.ui.Widget
10606 * @mixins OO.ui.LabelElement
10609 * @param {Object} [config] Configuration options
10610 * @cfg {number} [width=320] Width of popup in pixels
10611 * @cfg {number} [height] Height of popup, omit to use automatic height
10612 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
10613 * @cfg {string} [align='center'] Alignment of popup to origin
10614 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
10615 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
10616 * @cfg {jQuery} [$content] Content to append to the popup's body
10617 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
10618 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
10619 * @cfg {boolean} [head] Show label and close button at the top
10620 * @cfg {boolean} [padded] Add padding to the body
10622 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
10623 // Configuration initialization
10624 config
= config
|| {};
10626 // Parent constructor
10627 OO
.ui
.PopupWidget
.super.call( this, config
);
10629 // Mixin constructors
10630 OO
.ui
.LabelElement
.call( this, config
);
10631 OO
.ui
.ClippableElement
.call( this, config
);
10634 this.visible
= false;
10635 this.$popup
= this.$( '<div>' );
10636 this.$head
= this.$( '<div>' );
10637 this.$body
= this.$( '<div>' );
10638 this.$anchor
= this.$( '<div>' );
10639 // If undefined, will be computed lazily in updateDimensions()
10640 this.$container
= config
.$container
;
10641 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
10642 this.autoClose
= !!config
.autoClose
;
10643 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
10644 this.transitionTimeout
= null;
10645 this.anchor
= null;
10646 this.width
= config
.width
!== undefined ? config
.width
: 320;
10647 this.height
= config
.height
!== undefined ? config
.height
: null;
10648 this.align
= config
.align
|| 'center';
10649 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
10650 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
10653 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
10656 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
10657 this.$body
.addClass( 'oo-ui-popupWidget-body' );
10658 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
10660 .addClass( 'oo-ui-popupWidget-head' )
10661 .append( this.$label
, this.closeButton
.$element
);
10662 if ( !config
.head
) {
10666 .addClass( 'oo-ui-popupWidget-popup' )
10667 .append( this.$head
, this.$body
);
10670 .addClass( 'oo-ui-popupWidget' )
10671 .append( this.$popup
, this.$anchor
);
10672 // Move content, which was added to #$element by OO.ui.Widget, to the body
10673 if ( config
.$content
instanceof jQuery
) {
10674 this.$body
.append( config
.$content
);
10676 if ( config
.padded
) {
10677 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
10679 this.setClippableElement( this.$body
);
10684 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
10685 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
10686 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
10691 * Handles mouse down events.
10693 * @param {jQuery.Event} e Mouse down event
10695 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
10697 this.isVisible() &&
10698 !$.contains( this.$element
[0], e
.target
) &&
10699 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
10701 this.toggle( false );
10706 * Bind mouse down listener.
10708 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
10709 // Capture clicks outside popup
10710 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
10714 * Handles close button click events.
10716 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
10717 if ( this.isVisible() ) {
10718 this.toggle( false );
10723 * Unbind mouse down listener.
10725 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
10726 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
10730 * Set whether to show a anchor.
10732 * @param {boolean} [show] Show anchor, omit to toggle
10734 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
10735 show
= show
=== undefined ? !this.anchored
: !!show
;
10737 if ( this.anchored
!== show
) {
10739 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
10741 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
10743 this.anchored
= show
;
10748 * Check if showing a anchor.
10750 * @return {boolean} anchor is visible
10752 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
10753 return this.anchor
;
10759 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
10760 show
= show
=== undefined ? !this.isVisible() : !!show
;
10762 var change
= show
!== this.isVisible();
10765 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
10769 if ( this.autoClose
) {
10770 this.bindMouseDownListener();
10772 this.updateDimensions();
10773 this.toggleClipping( true );
10775 this.toggleClipping( false );
10776 if ( this.autoClose
) {
10777 this.unbindMouseDownListener();
10786 * Set the size of the popup.
10788 * Changing the size may also change the popup's position depending on the alignment.
10790 * @param {number} width Width
10791 * @param {number} height Height
10792 * @param {boolean} [transition=false] Use a smooth transition
10795 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
10796 this.width
= width
;
10797 this.height
= height
!== undefined ? height
: null;
10798 if ( this.isVisible() ) {
10799 this.updateDimensions( transition
);
10804 * Update the size and position.
10806 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
10807 * be called automatically.
10809 * @param {boolean} [transition=false] Use a smooth transition
10812 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
10813 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
10814 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
10817 if ( !this.$container
) {
10818 // Lazy-initialize $container if not specified in constructor
10819 this.$container
= this.$( this.getClosestScrollableElementContainer() );
10822 // Set height and width before measuring things, since it might cause our measurements
10823 // to change (e.g. due to scrollbars appearing or disappearing)
10826 height
: this.height
!== null ? this.height
: 'auto'
10829 // Compute initial popupOffset based on alignment
10830 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
10832 // Figure out if this will cause the popup to go beyond the edge of the container
10833 originOffset
= Math
.round( this.$element
.offset().left
);
10834 containerLeft
= Math
.round( this.$container
.offset().left
);
10835 containerWidth
= this.$container
.innerWidth();
10836 containerRight
= containerLeft
+ containerWidth
;
10837 popupLeft
= popupOffset
- this.containerPadding
;
10838 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
10839 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
10840 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10842 // Adjust offset to make the popup not go beyond the edge, if needed
10843 if ( overlapRight
< 0 ) {
10844 popupOffset
+= overlapRight
;
10845 } else if ( overlapLeft
< 0 ) {
10846 popupOffset
-= overlapLeft
;
10849 // Adjust offset to avoid anchor being rendered too close to the edge
10850 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
10851 // TODO: Find a measurement that works for CSS anchors and image anchors
10852 anchorWidth
= this.$anchor
[0].scrollWidth
* 2;
10853 if ( popupOffset
+ this.width
< anchorWidth
) {
10854 popupOffset
= anchorWidth
- this.width
;
10855 } else if ( -popupOffset
< anchorWidth
) {
10856 popupOffset
= -anchorWidth
;
10859 // Prevent transition from being interrupted
10860 clearTimeout( this.transitionTimeout
);
10861 if ( transition
) {
10862 // Enable transition
10863 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10866 // Position body relative to anchor
10867 this.$popup
.css( 'margin-left', popupOffset
);
10869 if ( transition
) {
10870 // Prevent transitioning after transition is complete
10871 this.transitionTimeout
= setTimeout( function () {
10872 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10875 // Prevent transitioning immediately
10876 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10879 // Reevaluate clipping state since we've relocated and resized the popup
10886 * Progress bar widget.
10889 * @extends OO.ui.Widget
10892 * @param {Object} [config] Configuration options
10893 * @cfg {number|boolean} [progress=false] Initial progress percent or false for indeterminate
10895 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
10896 // Configuration initialization
10897 config
= config
|| {};
10899 // Parent constructor
10900 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
10903 this.$bar
= this.$( '<div>' );
10904 this.progress
= null;
10907 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
10908 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar');
10911 role
: 'progressbar',
10912 'aria-valuemin': 0,
10913 'aria-valuemax': 100
10915 .addClass( 'oo-ui-progressBarWidget' )
10916 .append( this.$bar
);
10921 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
10923 /* Static Properties */
10925 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
10930 * Get progress percent
10932 * @return {number} Progress percent
10934 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
10935 return this.progress
;
10939 * Set progress percent
10941 * @param {number|boolean} progress Progress percent or false for indeterminate
10943 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
10944 this.progress
= progress
;
10946 if ( progress
!== false ) {
10947 this.$bar
.css( 'width', this.progress
+ '%' );
10948 this.$element
.attr( 'aria-valuenow', this.progress
);
10950 this.$bar
.css( 'width', '' );
10951 this.$element
.removeAttr( 'aria-valuenow' );
10953 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
10959 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10960 * Results are cleared and populated each time the query is changed.
10963 * @extends OO.ui.Widget
10966 * @param {Object} [config] Configuration options
10967 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10968 * @cfg {string} [value] Initial query value
10970 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10971 // Configuration initialization
10972 config
= config
|| {};
10974 // Parent constructor
10975 OO
.ui
.SearchWidget
.super.call( this, config
);
10978 this.query
= new OO
.ui
.TextInputWidget( {
10981 placeholder
: config
.placeholder
,
10982 value
: config
.value
10984 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10985 this.$query
= this.$( '<div>' );
10986 this.$results
= this.$( '<div>' );
10989 this.query
.connect( this, {
10990 change
: 'onQueryChange',
10991 enter
: 'onQueryEnter'
10993 this.results
.connect( this, {
10994 highlight
: 'onResultsHighlight',
10995 select
: 'onResultsSelect'
10997 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
11001 .addClass( 'oo-ui-searchWidget-query' )
11002 .append( this.query
.$element
);
11004 .addClass( 'oo-ui-searchWidget-results' )
11005 .append( this.results
.$element
);
11007 .addClass( 'oo-ui-searchWidget' )
11008 .append( this.$results
, this.$query
);
11013 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
11019 * @param {Object|null} item Item data or null if no item is highlighted
11024 * @param {Object|null} item Item data or null if no item is selected
11030 * Handle query key down events.
11032 * @param {jQuery.Event} e Key down event
11034 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
11035 var highlightedItem
, nextItem
,
11036 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
11039 highlightedItem
= this.results
.getHighlightedItem();
11040 if ( !highlightedItem
) {
11041 highlightedItem
= this.results
.getSelectedItem();
11043 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
11044 this.results
.highlightItem( nextItem
);
11045 nextItem
.scrollElementIntoView();
11050 * Handle select widget select events.
11052 * Clears existing results. Subclasses should repopulate items according to new query.
11054 * @param {string} value New value
11056 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
11058 this.results
.clearItems();
11062 * Handle select widget enter key events.
11064 * Selects highlighted item.
11066 * @param {string} value New value
11068 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
11070 this.results
.selectItem( this.results
.getHighlightedItem() );
11074 * Handle select widget highlight events.
11076 * @param {OO.ui.OptionWidget} item Highlighted item
11079 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
11080 this.emit( 'highlight', item
? item
.getData() : null );
11084 * Handle select widget select events.
11086 * @param {OO.ui.OptionWidget} item Selected item
11089 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
11090 this.emit( 'select', item
? item
.getData() : null );
11094 * Get the query input.
11096 * @return {OO.ui.TextInputWidget} Query input
11098 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
11103 * Get the results list.
11105 * @return {OO.ui.SelectWidget} Select list
11107 OO
.ui
.SearchWidget
.prototype.getResults = function () {
11108 return this.results
;
11112 * Generic selection of options.
11114 * Items can contain any rendering. Any widget that provides options, from which the user must
11115 * choose one, should be built on this class.
11117 * Use together with OO.ui.OptionWidget.
11120 * @extends OO.ui.Widget
11121 * @mixins OO.ui.GroupElement
11124 * @param {Object} [config] Configuration options
11125 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
11127 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
11128 // Configuration initialization
11129 config
= config
|| {};
11131 // Parent constructor
11132 OO
.ui
.SelectWidget
.super.call( this, config
);
11134 // Mixin constructors
11135 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
11138 this.pressed
= false;
11139 this.selecting
= null;
11140 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
11141 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
11144 this.$element
.on( {
11145 mousedown
: this.onMouseDown
.bind( this ),
11146 mouseover
: this.onMouseOver
.bind( this ),
11147 mouseleave
: this.onMouseLeave
.bind( this )
11151 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
11152 if ( $.isArray( config
.items
) ) {
11153 this.addItems( config
.items
);
11159 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
11161 // Need to mixin base class as well
11162 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
11163 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
11169 * @param {OO.ui.OptionWidget|null} item Highlighted item
11174 * @param {OO.ui.OptionWidget|null} item Pressed item
11179 * @param {OO.ui.OptionWidget|null} item Selected item
11184 * @param {OO.ui.OptionWidget|null} item Chosen item
11189 * @param {OO.ui.OptionWidget[]} items Added items
11190 * @param {number} index Index items were added at
11195 * @param {OO.ui.OptionWidget[]} items Removed items
11201 * Handle mouse down events.
11204 * @param {jQuery.Event} e Mouse down event
11206 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
11209 if ( !this.isDisabled() && e
.which
=== 1 ) {
11210 this.togglePressed( true );
11211 item
= this.getTargetItem( e
);
11212 if ( item
&& item
.isSelectable() ) {
11213 this.pressItem( item
);
11214 this.selecting
= item
;
11215 this.getElementDocument().addEventListener(
11217 this.onMouseUpHandler
,
11220 this.getElementDocument().addEventListener(
11222 this.onMouseMoveHandler
,
11231 * Handle mouse up events.
11234 * @param {jQuery.Event} e Mouse up event
11236 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
11239 this.togglePressed( false );
11240 if ( !this.selecting
) {
11241 item
= this.getTargetItem( e
);
11242 if ( item
&& item
.isSelectable() ) {
11243 this.selecting
= item
;
11246 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
11247 this.pressItem( null );
11248 this.chooseItem( this.selecting
);
11249 this.selecting
= null;
11252 this.getElementDocument().removeEventListener(
11254 this.onMouseUpHandler
,
11257 this.getElementDocument().removeEventListener(
11259 this.onMouseMoveHandler
,
11267 * Handle mouse move events.
11270 * @param {jQuery.Event} e Mouse move event
11272 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
11275 if ( !this.isDisabled() && this.pressed
) {
11276 item
= this.getTargetItem( e
);
11277 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
11278 this.pressItem( item
);
11279 this.selecting
= item
;
11286 * Handle mouse over events.
11289 * @param {jQuery.Event} e Mouse over event
11291 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
11294 if ( !this.isDisabled() ) {
11295 item
= this.getTargetItem( e
);
11296 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
11302 * Handle mouse leave events.
11305 * @param {jQuery.Event} e Mouse over event
11307 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
11308 if ( !this.isDisabled() ) {
11309 this.highlightItem( null );
11315 * Get the closest item to a jQuery.Event.
11318 * @param {jQuery.Event} e
11319 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
11321 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
11322 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
11323 if ( $item
.length
) {
11324 return $item
.data( 'oo-ui-optionWidget' );
11330 * Get selected item.
11332 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
11334 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
11337 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11338 if ( this.items
[i
].isSelected() ) {
11339 return this.items
[i
];
11346 * Get highlighted item.
11348 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
11350 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
11353 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11354 if ( this.items
[i
].isHighlighted() ) {
11355 return this.items
[i
];
11362 * Toggle pressed state.
11364 * @param {boolean} pressed An option is being pressed
11366 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
11367 if ( pressed
=== undefined ) {
11368 pressed
= !this.pressed
;
11370 if ( pressed
!== this.pressed
) {
11372 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
11373 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
11374 this.pressed
= pressed
;
11379 * Highlight an item.
11381 * Highlighting is mutually exclusive.
11383 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
11387 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
11388 var i
, len
, highlighted
,
11391 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11392 highlighted
= this.items
[i
] === item
;
11393 if ( this.items
[i
].isHighlighted() !== highlighted
) {
11394 this.items
[i
].setHighlighted( highlighted
);
11399 this.emit( 'highlight', item
);
11408 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
11412 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
11413 var i
, len
, selected
,
11416 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11417 selected
= this.items
[i
] === item
;
11418 if ( this.items
[i
].isSelected() !== selected
) {
11419 this.items
[i
].setSelected( selected
);
11424 this.emit( 'select', item
);
11433 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
11437 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
11438 var i
, len
, pressed
,
11441 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11442 pressed
= this.items
[i
] === item
;
11443 if ( this.items
[i
].isPressed() !== pressed
) {
11444 this.items
[i
].setPressed( pressed
);
11449 this.emit( 'press', item
);
11458 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
11459 * an item is selected using the keyboard or mouse.
11461 * @param {OO.ui.OptionWidget} item Item to choose
11465 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
11466 this.selectItem( item
);
11467 this.emit( 'choose', item
);
11473 * Get an item relative to another one.
11475 * @param {OO.ui.OptionWidget} item Item to start at
11476 * @param {number} direction Direction to move in, -1 to look backward, 1 to move forward
11477 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
11479 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
11480 var inc
= direction
> 0 ? 1 : -1,
11481 len
= this.items
.length
,
11482 index
= item
instanceof OO
.ui
.OptionWidget
?
11483 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
11484 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
11486 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
11487 Math
.max( index
, -1 ) :
11488 // Default to n-1 instead of -1, if nothing is selected let's start at the end
11489 Math
.min( index
, len
);
11491 while ( len
!== 0 ) {
11492 i
= ( i
+ inc
+ len
) % len
;
11493 item
= this.items
[i
];
11494 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11497 // Stop iterating when we've looped all the way around
11498 if ( i
=== stopAt
) {
11506 * Get the next selectable item.
11508 * @return {OO.ui.OptionWidget|null} Item, `null` if there aren't any selectable items
11510 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
11513 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11514 item
= this.items
[i
];
11515 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11526 * @param {OO.ui.OptionWidget[]} items Items to add
11527 * @param {number} [index] Index to insert items after
11531 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
11533 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
11535 // Always provide an index, even if it was omitted
11536 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
11544 * Items will be detached, not removed, so they can be used later.
11546 * @param {OO.ui.OptionWidget[]} items Items to remove
11550 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
11553 // Deselect items being removed
11554 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11556 if ( item
.isSelected() ) {
11557 this.selectItem( null );
11562 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
11564 this.emit( 'remove', items
);
11572 * Items will be detached, not removed, so they can be used later.
11577 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
11578 var items
= this.items
.slice();
11581 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
11584 this.selectItem( null );
11586 this.emit( 'remove', items
);
11592 * Select widget containing button options.
11594 * Use together with OO.ui.ButtonOptionWidget.
11597 * @extends OO.ui.SelectWidget
11600 * @param {Object} [config] Configuration options
11602 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
11603 // Parent constructor
11604 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
11607 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
11612 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
11615 * Select widget containing radio button options.
11617 * Use together with OO.ui.RadioOptionWidget.
11620 * @extends OO.ui.SelectWidget
11623 * @param {Object} [config] Configuration options
11625 OO
.ui
.RadioSelectWidget
= function OoUiRadioSelectWidget( config
) {
11626 // Parent constructor
11627 OO
.ui
.RadioSelectWidget
.super.call( this, config
);
11630 this.$element
.addClass( 'oo-ui-radioSelectWidget' );
11635 OO
.inheritClass( OO
.ui
.RadioSelectWidget
, OO
.ui
.SelectWidget
);
11638 * Overlaid menu of options.
11640 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
11643 * Use together with OO.ui.MenuOptionWidget.
11646 * @extends OO.ui.SelectWidget
11647 * @mixins OO.ui.ClippableElement
11650 * @param {Object} [config] Configuration options
11651 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
11652 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
11653 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
11655 OO
.ui
.MenuSelectWidget
= function OoUiMenuSelectWidget( config
) {
11656 // Configuration initialization
11657 config
= config
|| {};
11659 // Parent constructor
11660 OO
.ui
.MenuSelectWidget
.super.call( this, config
);
11662 // Mixin constructors
11663 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
11666 this.flashing
= false;
11667 this.visible
= false;
11668 this.newItems
= null;
11669 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
11670 this.$input
= config
.input
? config
.input
.$input
: null;
11671 this.$widget
= config
.widget
? config
.widget
.$element
: null;
11672 this.$previousFocus
= null;
11673 this.isolated
= !config
.input
;
11674 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
11675 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
11680 .attr( 'role', 'menu' )
11681 .addClass( 'oo-ui-menuSelectWidget' );
11686 OO
.inheritClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.SelectWidget
);
11687 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.ClippableElement
);
11692 * Handles document mouse down events.
11694 * @param {jQuery.Event} e Key down event
11696 OO
.ui
.MenuSelectWidget
.prototype.onDocumentMouseDown = function ( e
) {
11698 !OO
.ui
.contains( this.$element
[0], e
.target
, true ) &&
11699 ( !this.$widget
|| !OO
.ui
.contains( this.$widget
[0], e
.target
, true ) )
11701 this.toggle( false );
11706 * Handles key down events.
11708 * @param {jQuery.Event} e Key down event
11710 OO
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
11713 highlightItem
= this.getHighlightedItem();
11715 if ( !this.isDisabled() && this.isVisible() ) {
11716 if ( !highlightItem
) {
11717 highlightItem
= this.getSelectedItem();
11719 switch ( e
.keyCode
) {
11720 case OO
.ui
.Keys
.ENTER
:
11721 this.chooseItem( highlightItem
);
11724 case OO
.ui
.Keys
.UP
:
11725 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
11728 case OO
.ui
.Keys
.DOWN
:
11729 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
11732 case OO
.ui
.Keys
.ESCAPE
:
11733 if ( highlightItem
) {
11734 highlightItem
.setHighlighted( false );
11736 this.toggle( false );
11742 this.highlightItem( nextItem
);
11743 nextItem
.scrollElementIntoView();
11747 e
.preventDefault();
11748 e
.stopPropagation();
11755 * Bind key down listener.
11757 OO
.ui
.MenuSelectWidget
.prototype.bindKeyDownListener = function () {
11758 if ( this.$input
) {
11759 this.$input
.on( 'keydown', this.onKeyDownHandler
);
11761 // Capture menu navigation keys
11762 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
11767 * Unbind key down listener.
11769 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyDownListener = function () {
11770 if ( this.$input
) {
11771 this.$input
.off( 'keydown' );
11773 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
11780 * This will close the menu when done, unlike selectItem which only changes selection.
11782 * @param {OO.ui.OptionWidget} item Item to choose
11785 OO
.ui
.MenuSelectWidget
.prototype.chooseItem = function ( item
) {
11789 OO
.ui
.MenuSelectWidget
.super.prototype.chooseItem
.call( this, item
);
11791 if ( item
&& !this.flashing
) {
11792 this.flashing
= true;
11793 item
.flash().done( function () {
11794 widget
.toggle( false );
11795 widget
.flashing
= false;
11798 this.toggle( false );
11807 OO
.ui
.MenuSelectWidget
.prototype.addItems = function ( items
, index
) {
11811 OO
.ui
.MenuSelectWidget
.super.prototype.addItems
.call( this, items
, index
);
11814 if ( !this.newItems
) {
11815 this.newItems
= [];
11818 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11820 if ( this.isVisible() ) {
11821 // Defer fitting label until item has been attached
11824 this.newItems
.push( item
);
11828 // Reevaluate clipping
11837 OO
.ui
.MenuSelectWidget
.prototype.removeItems = function ( items
) {
11839 OO
.ui
.MenuSelectWidget
.super.prototype.removeItems
.call( this, items
);
11841 // Reevaluate clipping
11850 OO
.ui
.MenuSelectWidget
.prototype.clearItems = function () {
11852 OO
.ui
.MenuSelectWidget
.super.prototype.clearItems
.call( this );
11854 // Reevaluate clipping
11863 OO
.ui
.MenuSelectWidget
.prototype.toggle = function ( visible
) {
11864 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
11867 change
= visible
!== this.isVisible(),
11868 elementDoc
= this.getElementDocument(),
11869 widgetDoc
= this.$widget
? this.$widget
[0].ownerDocument
: null;
11872 OO
.ui
.MenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
11876 this.bindKeyDownListener();
11878 // Change focus to enable keyboard navigation
11879 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
11880 this.$previousFocus
= this.$( ':focus' );
11881 this.$input
[0].focus();
11883 if ( this.newItems
&& this.newItems
.length
) {
11884 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
11885 this.newItems
[i
].fitLabel();
11887 this.newItems
= null;
11889 this.toggleClipping( true );
11892 if ( this.autoHide
) {
11893 elementDoc
.addEventListener(
11894 'mousedown', this.onDocumentMouseDownHandler
, true
11896 // Support $widget being in a different document
11897 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11898 widgetDoc
.addEventListener(
11899 'mousedown', this.onDocumentMouseDownHandler
, true
11904 this.unbindKeyDownListener();
11905 if ( this.isolated
&& this.$previousFocus
) {
11906 this.$previousFocus
[0].focus();
11907 this.$previousFocus
= null;
11909 elementDoc
.removeEventListener(
11910 'mousedown', this.onDocumentMouseDownHandler
, true
11912 // Support $widget being in a different document
11913 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11914 widgetDoc
.removeEventListener(
11915 'mousedown', this.onDocumentMouseDownHandler
, true
11918 this.toggleClipping( false );
11926 * Menu for a text input widget.
11928 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11929 * is in a different frame, the menu's position is automatically calculated and maintained when the
11930 * menu is toggled or the window is resized.
11933 * @extends OO.ui.MenuSelectWidget
11936 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11937 * @param {Object} [config] Configuration options
11938 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11940 OO
.ui
.TextInputMenuSelectWidget
= function OoUiTextInputMenuSelectWidget( input
, config
) {
11941 // Configuration initialization
11942 config
= config
|| {};
11944 // Parent constructor
11945 OO
.ui
.TextInputMenuSelectWidget
.super.call( this, config
);
11948 this.input
= input
;
11949 this.$container
= config
.$container
|| this.input
.$element
;
11950 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
11953 this.$element
.addClass( 'oo-ui-textInputMenuSelectWidget' );
11958 OO
.inheritClass( OO
.ui
.TextInputMenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
11963 * Handle window resize event.
11965 * @param {jQuery.Event} e Window resize event
11967 OO
.ui
.TextInputMenuSelectWidget
.prototype.onWindowResize = function () {
11974 OO
.ui
.TextInputMenuSelectWidget
.prototype.toggle = function ( visible
) {
11975 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
11977 var change
= visible
!== this.isVisible();
11979 if ( change
&& visible
) {
11980 // Make sure the width is set before the parent method runs.
11981 // After this we have to call this.position(); again to actually
11982 // position ourselves correctly.
11987 OO
.ui
.TextInputMenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
11990 if ( this.isVisible() ) {
11992 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11994 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
12002 * Position the menu.
12006 OO
.ui
.TextInputMenuSelectWidget
.prototype.position = function () {
12007 var $container
= this.$container
,
12008 pos
= OO
.ui
.Element
.getRelativePosition( $container
, this.$element
.offsetParent() );
12010 // Position under input
12011 pos
.top
+= $container
.height();
12012 this.$element
.css( pos
);
12015 this.setIdealSize( $container
.width() );
12016 // We updated the position, so re-evaluate the clipping state
12023 * Structured list of items.
12025 * Use with OO.ui.OutlineOptionWidget.
12028 * @extends OO.ui.SelectWidget
12031 * @param {Object} [config] Configuration options
12033 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
12034 // Configuration initialization
12035 config
= config
|| {};
12037 // Parent constructor
12038 OO
.ui
.OutlineSelectWidget
.super.call( this, config
);
12041 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
12046 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
12049 * Switch that slides on and off.
12052 * @extends OO.ui.Widget
12053 * @mixins OO.ui.ToggleWidget
12056 * @param {Object} [config] Configuration options
12057 * @cfg {boolean} [value=false] Initial value
12059 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
12060 // Parent constructor
12061 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
12063 // Mixin constructors
12064 OO
.ui
.ToggleWidget
.call( this, config
);
12067 this.dragging
= false;
12068 this.dragStart
= null;
12069 this.sliding
= false;
12070 this.$glow
= this.$( '<span>' );
12071 this.$grip
= this.$( '<span>' );
12074 this.$element
.on( 'click', this.onClick
.bind( this ) );
12077 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
12078 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
12080 .addClass( 'oo-ui-toggleSwitchWidget' )
12081 .append( this.$glow
, this.$grip
);
12086 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
12087 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
12092 * Handle mouse down events.
12094 * @param {jQuery.Event} e Mouse down event
12096 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
12097 if ( !this.isDisabled() && e
.which
=== 1 ) {
12098 this.setValue( !this.value
);