3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2015 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2015-01-16T00:05:04Z
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
],
175 params
= Array
.prototype.slice
.call( arguments
, 1 );
176 if ( typeof message
=== 'string' ) {
177 // Perform $1 substitution
178 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
179 var i
= parseInt( n
, 10 );
180 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
183 // Return placeholder if message not found
184 message
= '[' + key
+ ']';
190 * Package a message and arguments for deferred resolution.
192 * Use this when you are statically specifying a message and the message may not yet be present.
194 * @param {string} key Message key
195 * @param {Mixed...} [params] Message parameters
196 * @return {Function} Function that returns the resolved message when executed
198 OO
.ui
.deferMsg = function () {
199 var args
= arguments
;
201 return OO
.ui
.msg
.apply( OO
.ui
, args
);
208 * If the message is a function it will be executed, otherwise it will pass through directly.
210 * @param {Function|string} msg Deferred message, or message text
211 * @return {string} Resolved message
213 OO
.ui
.resolveMsg = function ( msg
) {
214 if ( $.isFunction( msg
) ) {
223 * Element that can be marked as pending.
229 * @param {Object} [config] Configuration options
230 * @cfg {jQuery} [$pending] Element to mark as pending, defaults to this.$element
232 OO
.ui
.PendingElement
= function OoUiPendingElement( config
) {
233 // Configuration initialization
234 config
= config
|| {};
238 this.$pending
= null;
241 this.setPendingElement( config
.$pending
|| this.$element
);
246 OO
.initClass( OO
.ui
.PendingElement
);
251 * Set the pending element (and clean up any existing one).
253 * @param {jQuery} $pending The element to set to pending.
255 OO
.ui
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
256 if ( this.$pending
) {
257 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
260 this.$pending
= $pending
;
261 if ( this.pending
> 0 ) {
262 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
267 * Check if input is pending.
271 OO
.ui
.PendingElement
.prototype.isPending = function () {
272 return !!this.pending
;
276 * Increase the pending stack.
280 OO
.ui
.PendingElement
.prototype.pushPending = function () {
281 if ( this.pending
=== 0 ) {
282 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
283 this.updateThemeClasses();
291 * Reduce the pending stack.
297 OO
.ui
.PendingElement
.prototype.popPending = function () {
298 if ( this.pending
=== 1 ) {
299 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
300 this.updateThemeClasses();
302 this.pending
= Math
.max( 0, this.pending
- 1 );
312 * @mixins OO.EventEmitter
315 * @param {Object} [config] Configuration options
317 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
318 // Configuration initialization
319 config
= config
|| {};
321 // Mixin constructors
322 OO
.EventEmitter
.call( this );
327 actions
: 'getAction',
331 this.categorized
= {};
334 this.organized
= false;
335 this.changing
= false;
336 this.changed
= false;
341 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
343 /* Static Properties */
346 * Symbolic name of dialog.
353 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
359 * @param {OO.ui.ActionWidget} action Action that was clicked
364 * @param {OO.ui.ActionWidget} action Action that was resized
369 * @param {OO.ui.ActionWidget[]} added Actions added
374 * @param {OO.ui.ActionWidget[]} added Actions removed
384 * Handle action change events.
388 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
389 this.organized
= false;
390 if ( this.changing
) {
393 this.emit( 'change' );
398 * Check if a action is one of the special actions.
400 * @param {OO.ui.ActionWidget} action Action to check
401 * @return {boolean} Action is special
403 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
406 for ( flag
in this.special
) {
407 if ( action
=== this.special
[flag
] ) {
418 * @param {Object} [filters] Filters to use, omit to get all actions
419 * @param {string|string[]} [filters.actions] Actions that actions must have
420 * @param {string|string[]} [filters.flags] Flags that actions must have
421 * @param {string|string[]} [filters.modes] Modes that actions must have
422 * @param {boolean} [filters.visible] Actions must be visible
423 * @param {boolean} [filters.disabled] Actions must be disabled
424 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
426 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
427 var i
, len
, list
, category
, actions
, index
, match
, matches
;
432 // Collect category candidates
434 for ( category
in this.categorized
) {
435 list
= filters
[category
];
437 if ( !Array
.isArray( list
) ) {
440 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
441 actions
= this.categorized
[category
][list
[i
]];
442 if ( Array
.isArray( actions
) ) {
443 matches
.push
.apply( matches
, actions
);
448 // Remove by boolean filters
449 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
452 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
453 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
455 matches
.splice( i
, 1 );
461 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
463 index
= matches
.lastIndexOf( match
);
464 while ( index
!== i
) {
465 matches
.splice( index
, 1 );
467 index
= matches
.lastIndexOf( match
);
472 return this.list
.slice();
476 * Get special actions.
478 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
479 * Special flags can be configured by changing #static-specialFlags in a subclass.
481 * @return {OO.ui.ActionWidget|null} Safe action
483 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
485 return $.extend( {}, this.special
);
491 * Other actions include all non-special visible actions.
493 * @return {OO.ui.ActionWidget[]} Other actions
495 OO
.ui
.ActionSet
.prototype.getOthers = function () {
497 return this.others
.slice();
501 * Toggle actions based on their modes.
503 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
504 * visibility; matching actions will be shown, non-matching actions will be hidden.
506 * @param {string} mode Mode actions must have
511 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
514 this.changing
= true;
515 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
516 action
= this.list
[i
];
517 action
.toggle( action
.hasMode( mode
) );
520 this.organized
= false;
521 this.changing
= false;
522 this.emit( 'change' );
528 * Change which actions are able to be performed.
530 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
532 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
533 * indicate actions are able to be performed
536 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
537 var i
, len
, action
, item
;
539 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
541 action
= item
.getAction();
542 if ( actions
[action
] !== undefined ) {
543 item
.setDisabled( !actions
[action
] );
551 * Executes a function once per action.
553 * When making changes to multiple actions, use this method instead of iterating over the actions
554 * manually to defer emitting a change event until after all actions have been changed.
556 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
557 * @param {Function} callback Callback to run for each action; callback is invoked with three
558 * arguments: the action, the action's index, the list of actions being iterated over
561 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
562 this.changed
= false;
563 this.changing
= true;
564 this.get( filter
).forEach( callback
);
565 this.changing
= false;
566 if ( this.changed
) {
567 this.emit( 'change' );
576 * @param {OO.ui.ActionWidget[]} actions Actions to add
581 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
584 this.changing
= true;
585 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
587 action
.connect( this, {
588 click
: [ 'emit', 'click', action
],
589 resize
: [ 'emit', 'resize', action
],
590 toggle
: [ 'onActionChange' ]
592 this.list
.push( action
);
594 this.organized
= false;
595 this.emit( 'add', actions
);
596 this.changing
= false;
597 this.emit( 'change' );
605 * @param {OO.ui.ActionWidget[]} actions Actions to remove
610 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
611 var i
, len
, index
, action
;
613 this.changing
= true;
614 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
616 index
= this.list
.indexOf( action
);
617 if ( index
!== -1 ) {
618 action
.disconnect( this );
619 this.list
.splice( index
, 1 );
622 this.organized
= false;
623 this.emit( 'remove', actions
);
624 this.changing
= false;
625 this.emit( 'change' );
631 * Remove all actions.
637 OO
.ui
.ActionSet
.prototype.clear = function () {
639 removed
= this.list
.slice();
641 this.changing
= true;
642 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
643 action
= this.list
[i
];
644 action
.disconnect( this );
649 this.organized
= false;
650 this.emit( 'remove', removed
);
651 this.changing
= false;
652 this.emit( 'change' );
660 * This is called whenever organized information is requested. It will only reorganize the actions
661 * if something has changed since the last time it ran.
666 OO
.ui
.ActionSet
.prototype.organize = function () {
667 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
668 specialFlags
= this.constructor.static.specialFlags
;
670 if ( !this.organized
) {
671 this.categorized
= {};
674 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
675 action
= this.list
[i
];
676 if ( action
.isVisible() ) {
677 // Populate categories
678 for ( category
in this.categories
) {
679 if ( !this.categorized
[category
] ) {
680 this.categorized
[category
] = {};
682 list
= action
[this.categories
[category
]]();
683 if ( !Array
.isArray( list
) ) {
686 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
688 if ( !this.categorized
[category
][item
] ) {
689 this.categorized
[category
][item
] = [];
691 this.categorized
[category
][item
].push( action
);
694 // Populate special/others
696 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
697 flag
= specialFlags
[j
];
698 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
699 this.special
[flag
] = action
;
705 this.others
.push( action
);
709 this.organized
= true;
716 * DOM element abstraction.
722 * @param {Object} [config] Configuration options
723 * @cfg {Function} [$] jQuery for the frame the widget is in
724 * @cfg {string[]} [classes] CSS class names to add
725 * @cfg {string} [id] HTML id attribute
726 * @cfg {string} [text] Text to insert
727 * @cfg {jQuery} [$content] Content elements to append (after text)
728 * @cfg {Mixed} [data] Element data
730 OO
.ui
.Element
= function OoUiElement( config
) {
731 // Configuration initialization
732 config
= config
|| {};
735 this.$ = config
.$ || OO
.ui
.Element
.static.getJQuery( document
);
736 this.data
= config
.data
;
737 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
738 this.elementGroup
= null;
739 this.debouncedUpdateThemeClassesHandler
= this.debouncedUpdateThemeClasses
.bind( this );
740 this.updateThemeClassesPending
= false;
743 if ( $.isArray( config
.classes
) ) {
744 this.$element
.addClass( config
.classes
.join( ' ' ) );
747 this.$element
.attr( 'id', config
.id
);
750 this.$element
.text( config
.text
);
752 if ( config
.$content
) {
753 this.$element
.append( config
.$content
);
759 OO
.initClass( OO
.ui
.Element
);
761 /* Static Properties */
766 * This may be ignored if #getTagName is overridden.
772 OO
.ui
.Element
.static.tagName
= 'div';
777 * Get a jQuery function within a specific document.
780 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
781 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
783 * @return {Function} Bound jQuery function
785 OO
.ui
.Element
.static.getJQuery = function ( context
, $iframe
) {
786 function wrapper( selector
) {
787 return $( selector
, wrapper
.context
);
790 wrapper
.context
= this.getDocument( context
);
793 wrapper
.$iframe
= $iframe
;
800 * Get the document of an element.
803 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
804 * @return {HTMLDocument|null} Document object
806 OO
.ui
.Element
.static.getDocument = function ( obj
) {
807 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
808 return ( obj
[0] && obj
[0].ownerDocument
) ||
809 // Empty jQuery selections might have a context
816 ( obj
.nodeType
=== 9 && obj
) ||
821 * Get the window of an element or document.
824 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
825 * @return {Window} Window object
827 OO
.ui
.Element
.static.getWindow = function ( obj
) {
828 var doc
= this.getDocument( obj
);
829 return doc
.parentWindow
|| doc
.defaultView
;
833 * Get the direction of an element or document.
836 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
837 * @return {string} Text direction, either 'ltr' or 'rtl'
839 OO
.ui
.Element
.static.getDir = function ( obj
) {
842 if ( obj
instanceof jQuery
) {
845 isDoc
= obj
.nodeType
=== 9;
846 isWin
= obj
.document
!== undefined;
847 if ( isDoc
|| isWin
) {
853 return $( obj
).css( 'direction' );
857 * Get the offset between two frames.
859 * TODO: Make this function not use recursion.
862 * @param {Window} from Window of the child frame
863 * @param {Window} [to=window] Window of the parent frame
864 * @param {Object} [offset] Offset to start with, used internally
865 * @return {Object} Offset object, containing left and top properties
867 OO
.ui
.Element
.static.getFrameOffset = function ( from, to
, offset
) {
868 var i
, len
, frames
, frame
, rect
;
874 offset
= { top
: 0, left
: 0 };
876 if ( from.parent
=== from ) {
880 // Get iframe element
881 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
882 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
883 if ( frames
[i
].contentWindow
=== from ) {
889 // Recursively accumulate offset values
891 rect
= frame
.getBoundingClientRect();
892 offset
.left
+= rect
.left
;
893 offset
.top
+= rect
.top
;
895 this.getFrameOffset( from.parent
, offset
);
902 * Get the offset between two elements.
904 * The two elements may be in a different frame, but in that case the frame $element is in must
905 * be contained in the frame $anchor is in.
908 * @param {jQuery} $element Element whose position to get
909 * @param {jQuery} $anchor Element to get $element's position relative to
910 * @return {Object} Translated position coordinates, containing top and left properties
912 OO
.ui
.Element
.static.getRelativePosition = function ( $element
, $anchor
) {
913 var iframe
, iframePos
,
914 pos
= $element
.offset(),
915 anchorPos
= $anchor
.offset(),
916 elementDocument
= this.getDocument( $element
),
917 anchorDocument
= this.getDocument( $anchor
);
919 // If $element isn't in the same document as $anchor, traverse up
920 while ( elementDocument
!== anchorDocument
) {
921 iframe
= elementDocument
.defaultView
.frameElement
;
923 throw new Error( '$element frame is not contained in $anchor frame' );
925 iframePos
= $( iframe
).offset();
926 pos
.left
+= iframePos
.left
;
927 pos
.top
+= iframePos
.top
;
928 elementDocument
= iframe
.ownerDocument
;
930 pos
.left
-= anchorPos
.left
;
931 pos
.top
-= anchorPos
.top
;
936 * Get element border sizes.
939 * @param {HTMLElement} el Element to measure
940 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
942 OO
.ui
.Element
.static.getBorders = function ( el
) {
943 var doc
= el
.ownerDocument
,
944 win
= doc
.parentWindow
|| doc
.defaultView
,
945 style
= win
&& win
.getComputedStyle
?
946 win
.getComputedStyle( el
, null ) :
949 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
950 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
951 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
952 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
963 * Get dimensions of an element or window.
966 * @param {HTMLElement|Window} el Element to measure
967 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
969 OO
.ui
.Element
.static.getDimensions = function ( el
) {
971 doc
= el
.ownerDocument
|| el
.document
,
972 win
= doc
.parentWindow
|| doc
.defaultView
;
974 if ( win
=== el
|| el
=== doc
.documentElement
) {
977 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
979 top
: $win
.scrollTop(),
980 left
: $win
.scrollLeft()
982 scrollbar
: { right
: 0, bottom
: 0 },
986 bottom
: $win
.innerHeight(),
987 right
: $win
.innerWidth()
993 borders
: this.getBorders( el
),
995 top
: $el
.scrollTop(),
996 left
: $el
.scrollLeft()
999 right
: $el
.innerWidth() - el
.clientWidth
,
1000 bottom
: $el
.innerHeight() - el
.clientHeight
1002 rect
: el
.getBoundingClientRect()
1008 * Get scrollable object parent
1010 * documentElement can't be used to get or set the scrollTop
1011 * property on Blink. Changing and testing its value lets us
1012 * use 'body' or 'documentElement' based on what is working.
1014 * https://code.google.com/p/chromium/issues/detail?id=303131
1017 * @param {HTMLElement} el Element to find scrollable parent for
1018 * @return {HTMLElement} Scrollable parent
1020 OO
.ui
.Element
.static.getRootScrollableElement = function ( el
) {
1021 var scrollTop
, body
;
1023 if ( OO
.ui
.scrollableElement
=== undefined ) {
1024 body
= el
.ownerDocument
.body
;
1025 scrollTop
= body
.scrollTop
;
1028 if ( body
.scrollTop
=== 1 ) {
1029 body
.scrollTop
= scrollTop
;
1030 OO
.ui
.scrollableElement
= 'body';
1032 OO
.ui
.scrollableElement
= 'documentElement';
1036 return el
.ownerDocument
[ OO
.ui
.scrollableElement
];
1040 * Get closest scrollable container.
1042 * Traverses up until either a scrollable element or the root is reached, in which case the window
1046 * @param {HTMLElement} el Element to find scrollable container for
1047 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
1048 * @return {HTMLElement} Closest scrollable container
1050 OO
.ui
.Element
.static.getClosestScrollableContainer = function ( el
, dimension
) {
1052 props
= [ 'overflow' ],
1053 $parent
= $( el
).parent();
1055 if ( dimension
=== 'x' || dimension
=== 'y' ) {
1056 props
.push( 'overflow-' + dimension
);
1059 while ( $parent
.length
) {
1060 if ( $parent
[0] === this.getRootScrollableElement( el
) ) {
1065 val
= $parent
.css( props
[i
] );
1066 if ( val
=== 'auto' || val
=== 'scroll' ) {
1070 $parent
= $parent
.parent();
1072 return this.getDocument( el
).body
;
1076 * Scroll element into view.
1079 * @param {HTMLElement} el Element to scroll into view
1080 * @param {Object} [config] Configuration options
1081 * @param {string} [config.duration] jQuery animation duration value
1082 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
1083 * to scroll in both directions
1084 * @param {Function} [config.complete] Function to call when scrolling completes
1086 OO
.ui
.Element
.static.scrollIntoView = function ( el
, config
) {
1087 // Configuration initialization
1088 config
= config
|| {};
1091 callback
= typeof config
.complete
=== 'function' && config
.complete
,
1092 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
1094 eld
= this.getDimensions( el
),
1095 scd
= this.getDimensions( sc
),
1096 $win
= $( this.getWindow( el
) );
1098 // Compute the distances between the edges of el and the edges of the scroll viewport
1099 if ( $sc
.is( 'html, body' ) ) {
1100 // If the scrollable container is the root, this is easy
1103 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
1104 left
: eld
.rect
.left
,
1105 right
: $win
.innerWidth() - eld
.rect
.right
1108 // Otherwise, we have to subtract el's coordinates from sc's coordinates
1110 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
1111 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
1112 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
1113 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
1117 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1118 if ( rel
.top
< 0 ) {
1119 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
1120 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
1121 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
1124 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1125 if ( rel
.left
< 0 ) {
1126 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
1127 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
1128 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
1131 if ( !$.isEmptyObject( anim
) ) {
1132 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
1134 $sc
.queue( function ( next
) {
1151 * @return {Mixed} Element data
1153 OO
.ui
.Element
.prototype.getData = function () {
1160 * @param {Mixed} Element data
1163 OO
.ui
.Element
.prototype.setData = function ( data
) {
1169 * Check if element supports one or more methods.
1171 * @param {string|string[]} methods Method or list of methods to check
1172 * @return {boolean} All methods are supported
1174 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1178 methods
= $.isArray( methods
) ? methods
: [ methods
];
1179 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1180 if ( $.isFunction( this[methods
[i
]] ) ) {
1185 return methods
.length
=== support
;
1189 * Update the theme-provided classes.
1191 * @localdoc This is called in element mixins and widget classes any time state changes.
1192 * Updating is debounced, minimizing overhead of changing multiple attributes and
1193 * guaranteeing that theme updates do not occur within an element's constructor
1195 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1196 if ( !this.updateThemeClassesPending
) {
1197 this.updateThemeClassesPending
= true;
1198 setTimeout( this.debouncedUpdateThemeClassesHandler
);
1205 OO
.ui
.Element
.prototype.debouncedUpdateThemeClasses = function () {
1206 OO
.ui
.theme
.updateElementClasses( this );
1207 this.updateThemeClassesPending
= false;
1211 * Get the HTML tag name.
1213 * Override this method to base the result on instance information.
1215 * @return {string} HTML tag name
1217 OO
.ui
.Element
.prototype.getTagName = function () {
1218 return this.constructor.static.tagName
;
1222 * Check if the element is attached to the DOM
1223 * @return {boolean} The element is attached to the DOM
1225 OO
.ui
.Element
.prototype.isElementAttached = function () {
1226 return $.contains( this.getElementDocument(), this.$element
[0] );
1230 * Get the DOM document.
1232 * @return {HTMLDocument} Document object
1234 OO
.ui
.Element
.prototype.getElementDocument = function () {
1235 // Don't use this.$.context because subclasses can rebind this.$
1236 // Don't cache this in other ways either because subclasses could can change this.$element
1237 return OO
.ui
.Element
.static.getDocument( this.$element
);
1241 * Get the DOM window.
1243 * @return {Window} Window object
1245 OO
.ui
.Element
.prototype.getElementWindow = function () {
1246 return OO
.ui
.Element
.static.getWindow( this.$element
);
1250 * Get closest scrollable container.
1252 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1253 return OO
.ui
.Element
.static.getClosestScrollableContainer( this.$element
[0] );
1257 * Get group element is in.
1259 * @return {OO.ui.GroupElement|null} Group element, null if none
1261 OO
.ui
.Element
.prototype.getElementGroup = function () {
1262 return this.elementGroup
;
1266 * Set group element is in.
1268 * @param {OO.ui.GroupElement|null} group Group element, null if none
1271 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1272 this.elementGroup
= group
;
1277 * Scroll element into view.
1279 * @param {Object} [config] Configuration options
1281 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1282 return OO
.ui
.Element
.static.scrollIntoView( this.$element
[0], config
);
1286 * Container for elements.
1290 * @extends OO.ui.Element
1291 * @mixins OO.EventEmitter
1294 * @param {Object} [config] Configuration options
1296 OO
.ui
.Layout
= function OoUiLayout( config
) {
1297 // Configuration initialization
1298 config
= config
|| {};
1300 // Parent constructor
1301 OO
.ui
.Layout
.super.call( this, config
);
1303 // Mixin constructors
1304 OO
.EventEmitter
.call( this );
1307 this.$element
.addClass( 'oo-ui-layout' );
1312 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1313 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1316 * User interface control.
1320 * @extends OO.ui.Element
1321 * @mixins OO.EventEmitter
1324 * @param {Object} [config] Configuration options
1325 * @cfg {boolean} [disabled=false] Disable
1327 OO
.ui
.Widget
= function OoUiWidget( config
) {
1328 // Initialize config
1329 config
= $.extend( { disabled
: false }, config
);
1331 // Parent constructor
1332 OO
.ui
.Widget
.super.call( this, config
);
1334 // Mixin constructors
1335 OO
.EventEmitter
.call( this );
1338 this.visible
= true;
1339 this.disabled
= null;
1340 this.wasDisabled
= null;
1343 this.$element
.addClass( 'oo-ui-widget' );
1344 this.setDisabled( !!config
.disabled
);
1349 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1350 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1356 * @param {boolean} disabled Widget is disabled
1361 * @param {boolean} visible Widget is visible
1367 * Check if the widget is disabled.
1369 * @return {boolean} Button is disabled
1371 OO
.ui
.Widget
.prototype.isDisabled = function () {
1372 return this.disabled
;
1376 * Check if widget is visible.
1378 * @return {boolean} Widget is visible
1380 OO
.ui
.Widget
.prototype.isVisible = function () {
1381 return this.visible
;
1385 * Set the disabled state of the widget.
1387 * This should probably change the widgets' appearance and prevent it from being used.
1389 * @param {boolean} disabled Disable widget
1392 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1395 this.disabled
= !!disabled
;
1396 isDisabled
= this.isDisabled();
1397 if ( isDisabled
!== this.wasDisabled
) {
1398 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1399 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1400 this.emit( 'disable', isDisabled
);
1401 this.updateThemeClasses();
1403 this.wasDisabled
= isDisabled
;
1409 * Toggle visibility of widget.
1411 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1415 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1416 show
= show
=== undefined ? !this.visible
: !!show
;
1418 if ( show
!== this.isVisible() ) {
1419 this.visible
= show
;
1420 this.$element
.toggle( show
);
1421 this.emit( 'toggle', show
);
1428 * Update the disabled state, in case of changes in parent widget.
1432 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1433 this.setDisabled( this.disabled
);
1438 * Container for elements in a child frame.
1440 * Use together with OO.ui.WindowManager.
1444 * @extends OO.ui.Element
1445 * @mixins OO.EventEmitter
1447 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1448 * teardown processes are executed when the window is closed.
1450 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1451 * - Window manager begins opening window
1452 * - {@link #getSetupProcess} method is called and its result executed
1453 * - {@link #getReadyProcess} method is called and its result executed
1454 * - Window is now open
1456 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1457 * - Window manager begins closing window
1458 * - {@link #getHoldProcess} method is called and its result executed
1459 * - {@link #getTeardownProcess} method is called and its result executed
1460 * - Window is now closed
1462 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1463 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1464 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchronous
1465 * processing can complete. Always assume window processes are executed asynchronously. See
1466 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1467 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1469 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1470 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1473 * @param {Object} [config] Configuration options
1474 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large`, `larger` or
1475 * `full`; omit to use #static-size
1477 OO
.ui
.Window
= function OoUiWindow( config
) {
1478 // Configuration initialization
1479 config
= config
|| {};
1481 // Parent constructor
1482 OO
.ui
.Window
.super.call( this, config
);
1484 // Mixin constructors
1485 OO
.EventEmitter
.call( this );
1488 this.manager
= null;
1489 this.initialized
= false;
1490 this.visible
= false;
1491 this.opening
= null;
1492 this.closing
= null;
1495 this.loading
= null;
1496 this.size
= config
.size
|| this.constructor.static.size
;
1497 this.$frame
= this.$( '<div>' );
1498 this.$overlay
= this.$( '<div>' );
1502 .addClass( 'oo-ui-window' )
1503 .append( this.$frame
, this.$overlay
);
1504 this.$frame
.addClass( 'oo-ui-window-frame' );
1505 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1507 // NOTE: Additional initialization will occur when #setManager is called
1512 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1513 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1515 /* Static Properties */
1518 * Symbolic name of size.
1520 * Size is used if no size is configured during construction.
1524 * @property {string}
1526 OO
.ui
.Window
.static.size
= 'medium';
1528 /* Static Methods */
1531 * Transplant the CSS styles from as parent document to a frame's document.
1533 * This loops over the style sheets in the parent document, and copies their nodes to the
1534 * frame's document. It then polls the document to see when all styles have loaded, and once they
1535 * have, resolves the promise.
1537 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1538 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1539 * Firefox, where the styles won't load until the iframe becomes visible.
1541 * For details of how we arrived at the strategy used in this function, see #load.
1545 * @param {HTMLDocument} parentDoc Document to transplant styles from
1546 * @param {HTMLDocument} frameDoc Document to transplant styles to
1547 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1548 * @return {jQuery.Promise} Promise resolved when styles have loaded
1550 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1551 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1552 $pollNodes
= $( [] ),
1553 // Fake font-family value
1554 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1555 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1556 deferred
= $.Deferred();
1558 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1559 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1560 if ( styleNode
.disabled
) {
1564 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1565 // External stylesheet; use @import
1566 styleText
= '@import url(' + styleNode
.href
+ ');';
1568 // Internal stylesheet; just copy the text
1569 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1570 // other browsers, so fall back to '' rather than 'undefined'
1571 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1574 // Create a node with a unique ID that we're going to monitor to see when the CSS
1576 if ( styleNode
.oouiFrameTransplantStylesId
) {
1577 // If we're nesting transplantStyles operations and this node already has
1578 // a CSS rule to wait for loading, reuse it
1579 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1581 // Otherwise, create a new ID
1582 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1585 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1586 // The font-family rule will only take effect once the @import finishes
1587 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1590 // Create a node with id=pollNodeId
1591 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1592 .attr( 'id', pollNodeId
)
1593 .appendTo( frameDoc
.body
)
1596 // Add our modified CSS as a <style> tag
1597 newNode
= frameDoc
.createElement( 'style' );
1598 newNode
.textContent
= styleText
;
1599 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1600 frameDoc
.head
.appendChild( newNode
);
1602 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1604 // Poll every 100ms until all external stylesheets have loaded
1605 $pendingPollNodes
= $pollNodes
;
1606 timeoutID
= setTimeout( function pollExternalStylesheets() {
1608 $pendingPollNodes
.length
> 0 &&
1609 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1611 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1614 if ( $pendingPollNodes
.length
=== 0 ) {
1616 if ( timeoutID
!== null ) {
1618 $pollNodes
.remove();
1622 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1625 // ...but give up after a while
1626 if ( timeout
!== 0 ) {
1627 setTimeout( function () {
1629 clearTimeout( timeoutID
);
1631 $pollNodes
.remove();
1634 }, timeout
|| 5000 );
1637 return deferred
.promise();
1643 * Handle mouse down events.
1645 * @param {jQuery.Event} e Mouse down event
1647 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1648 // Prevent clicking on the click-block from stealing focus
1649 if ( e
.target
=== this.$element
[0] ) {
1655 * Check if window has been initialized.
1657 * @return {boolean} Window has been initialized
1659 OO
.ui
.Window
.prototype.isInitialized = function () {
1660 return this.initialized
;
1664 * Check if window is visible.
1666 * @return {boolean} Window is visible
1668 OO
.ui
.Window
.prototype.isVisible = function () {
1669 return this.visible
;
1673 * Check if window is loading.
1675 * @return {boolean} Window is loading
1677 OO
.ui
.Window
.prototype.isLoading = function () {
1678 return this.loading
&& this.loading
.state() === 'pending';
1682 * Check if window is loaded.
1684 * @return {boolean} Window is loaded
1686 OO
.ui
.Window
.prototype.isLoaded = function () {
1687 return this.loading
&& this.loading
.state() === 'resolved';
1691 * Check if window is opening.
1693 * This is a wrapper around OO.ui.WindowManager#isOpening.
1695 * @return {boolean} Window is opening
1697 OO
.ui
.Window
.prototype.isOpening = function () {
1698 return this.manager
.isOpening( this );
1702 * Check if window is closing.
1704 * This is a wrapper around OO.ui.WindowManager#isClosing.
1706 * @return {boolean} Window is closing
1708 OO
.ui
.Window
.prototype.isClosing = function () {
1709 return this.manager
.isClosing( this );
1713 * Check if window is opened.
1715 * This is a wrapper around OO.ui.WindowManager#isOpened.
1717 * @return {boolean} Window is opened
1719 OO
.ui
.Window
.prototype.isOpened = function () {
1720 return this.manager
.isOpened( this );
1724 * Get the window manager.
1726 * @return {OO.ui.WindowManager} Manager of window
1728 OO
.ui
.Window
.prototype.getManager = function () {
1729 return this.manager
;
1733 * Get the window size.
1735 * @return {string} Symbolic size name, e.g. `small`, `medium`, `large`, `larger`, `full`
1737 OO
.ui
.Window
.prototype.getSize = function () {
1742 * Disable transitions on window's frame for the duration of the callback function, then enable them
1746 * @param {Function} callback Function to call while transitions are disabled
1748 OO
.ui
.Window
.prototype.withoutSizeTransitions = function ( callback
) {
1749 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1750 // Disable transitions first, otherwise we'll get values from when the window was animating.
1752 styleObj
= this.$frame
[0].style
;
1753 oldTransition
= styleObj
.transition
|| styleObj
.OTransition
|| styleObj
.MsTransition
||
1754 styleObj
.MozTransition
|| styleObj
.WebkitTransition
;
1755 styleObj
.transition
= styleObj
.OTransition
= styleObj
.MsTransition
=
1756 styleObj
.MozTransition
= styleObj
.WebkitTransition
= 'none';
1758 // Force reflow to make sure the style changes done inside callback really are not transitioned
1759 this.$frame
.height();
1760 styleObj
.transition
= styleObj
.OTransition
= styleObj
.MsTransition
=
1761 styleObj
.MozTransition
= styleObj
.WebkitTransition
= oldTransition
;
1765 * Get the height of the dialog contents.
1767 * @return {number} Content height
1769 OO
.ui
.Window
.prototype.getContentHeight = function () {
1772 bodyStyleObj
= this.$body
[0].style
,
1773 frameStyleObj
= this.$frame
[0].style
;
1775 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1776 // Disable transitions first, otherwise we'll get values from when the window was animating.
1777 this.withoutSizeTransitions( function () {
1778 var oldHeight
= frameStyleObj
.height
,
1779 oldPosition
= bodyStyleObj
.position
;
1780 frameStyleObj
.height
= '1px';
1781 // Force body to resize to new width
1782 bodyStyleObj
.position
= 'relative';
1783 bodyHeight
= win
.getBodyHeight();
1784 frameStyleObj
.height
= oldHeight
;
1785 bodyStyleObj
.position
= oldPosition
;
1789 // Add buffer for border
1790 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1791 // Use combined heights of children
1792 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1797 * Get the height of the dialog contents.
1799 * When this function is called, the dialog will temporarily have been resized
1800 * to height=1px, so .scrollHeight measurements can be taken accurately.
1802 * @return {number} Height of content
1804 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1805 return this.$body
[0].scrollHeight
;
1809 * Get the directionality of the frame
1811 * @return {string} Directionality, 'ltr' or 'rtl'
1813 OO
.ui
.Window
.prototype.getDir = function () {
1818 * Get a process for setting up a window for use.
1820 * Each time the window is opened this process will set it up for use in a particular context, based
1821 * on the `data` argument.
1823 * When you override this method, you can add additional setup steps to the process the parent
1824 * method provides using the 'first' and 'next' methods.
1827 * @param {Object} [data] Window opening data
1828 * @return {OO.ui.Process} Setup process
1830 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1831 return new OO
.ui
.Process();
1835 * Get a process for readying a window for use.
1837 * Each time the window is open and setup, this process will ready it up for use in a particular
1838 * context, based on the `data` argument.
1840 * When you override this method, you can add additional setup steps to the process the parent
1841 * method provides using the 'first' and 'next' methods.
1844 * @param {Object} [data] Window opening data
1845 * @return {OO.ui.Process} Setup process
1847 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1848 return new OO
.ui
.Process();
1852 * Get a process for holding a window from use.
1854 * Each time the window is closed, this process will hold it from use in a particular context, based
1855 * on the `data` argument.
1857 * When you override this method, you can add additional setup steps to the process the parent
1858 * method provides using the 'first' and 'next' methods.
1861 * @param {Object} [data] Window closing data
1862 * @return {OO.ui.Process} Hold process
1864 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1865 return new OO
.ui
.Process();
1869 * Get a process for tearing down a window after use.
1871 * Each time the window is closed this process will tear it down and do something with the user's
1872 * interactions within the window, based on the `data` argument.
1874 * When you override this method, you can add additional teardown steps to the process the parent
1875 * method provides using the 'first' and 'next' methods.
1878 * @param {Object} [data] Window closing data
1879 * @return {OO.ui.Process} Teardown process
1881 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1882 return new OO
.ui
.Process();
1886 * Toggle visibility of window.
1888 * If the window is isolated and hasn't fully loaded yet, the visibility property will be used
1889 * instead of display.
1891 * @param {boolean} [show] Make window visible, omit to toggle visibility
1895 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1896 show
= show
=== undefined ? !this.visible
: !!show
;
1898 if ( show
!== this.isVisible() ) {
1899 this.visible
= show
;
1901 if ( this.isolated
&& !this.isLoaded() ) {
1902 // Hide the window using visibility instead of display until loading is complete
1903 // Can't use display: none; because that prevents the iframe from loading in Firefox
1904 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1906 this.$element
.toggle( show
).css( 'visibility', '' );
1908 this.emit( 'toggle', show
);
1915 * Set the window manager.
1917 * This must be called before initialize. Calling it more than once will cause an error.
1919 * @param {OO.ui.WindowManager} manager Manager for this window
1920 * @throws {Error} If called more than once
1923 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1924 if ( this.manager
) {
1925 throw new Error( 'Cannot set window manager, window already has a manager' );
1929 this.manager
= manager
;
1930 this.isolated
= manager
.shouldIsolate();
1933 if ( this.isolated
) {
1934 this.$iframe
= this.$( '<iframe>' );
1935 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1936 this.$frame
.append( this.$iframe
);
1937 this.$ = function () {
1938 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1940 // WARNING: Do not use this.$ again until #initialize is called
1942 this.$content
= this.$( '<div>' );
1943 this.$document
= $( this.getElementDocument() );
1944 this.$content
.addClass( 'oo-ui-window-content' ).attr( 'tabIndex', 0 );
1945 this.$frame
.append( this.$content
);
1947 this.toggle( false );
1949 // Figure out directionality:
1950 this.dir
= OO
.ui
.Element
.static.getDir( this.$iframe
|| this.$content
) || 'ltr';
1956 * Set the window size.
1958 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1961 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1963 this.manager
.updateWindowSize( this );
1968 * Set window dimensions.
1970 * Properties are applied to the frame container.
1972 * @param {Object} dim CSS dimension properties
1973 * @param {string|number} [dim.width] Width
1974 * @param {string|number} [dim.minWidth] Minimum width
1975 * @param {string|number} [dim.maxWidth] Maximum width
1976 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1977 * @param {string|number} [dim.minWidth] Minimum height
1978 * @param {string|number} [dim.maxWidth] Maximum height
1981 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1984 styleObj
= this.$frame
[0].style
;
1986 // Calculate the height we need to set using the correct width
1987 if ( dim
.height
=== undefined ) {
1988 this.withoutSizeTransitions( function () {
1989 var oldWidth
= styleObj
.width
;
1990 win
.$frame
.css( 'width', dim
.width
|| '' );
1991 height
= win
.getContentHeight();
1992 styleObj
.width
= oldWidth
;
1995 height
= dim
.height
;
1999 width
: dim
.width
|| '',
2000 minWidth
: dim
.minWidth
|| '',
2001 maxWidth
: dim
.maxWidth
|| '',
2002 height
: height
|| '',
2003 minHeight
: dim
.minHeight
|| '',
2004 maxHeight
: dim
.maxHeight
|| ''
2011 * Initialize window contents.
2013 * The first time the window is opened, #initialize is called when it's safe to begin populating
2014 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
2016 * Once this method is called, this.$ can be used to create elements within the frame.
2018 * @throws {Error} If not attached to a manager
2021 OO
.ui
.Window
.prototype.initialize = function () {
2022 if ( !this.manager
) {
2023 throw new Error( 'Cannot initialize window, must be attached to a manager' );
2027 this.$head
= this.$( '<div>' );
2028 this.$body
= this.$( '<div>' );
2029 this.$foot
= this.$( '<div>' );
2030 this.$innerOverlay
= this.$( '<div>' );
2033 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
2036 this.$head
.addClass( 'oo-ui-window-head' );
2037 this.$body
.addClass( 'oo-ui-window-body' );
2038 this.$foot
.addClass( 'oo-ui-window-foot' );
2039 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
2040 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
2048 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
2049 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
2051 * @param {Object} [data] Window opening data
2052 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
2053 * first argument will be a promise which will be resolved when the window begins closing
2055 OO
.ui
.Window
.prototype.open = function ( data
) {
2056 return this.manager
.openWindow( this, data
);
2062 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
2063 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
2065 * @param {Object} [data] Window closing data
2066 * @return {jQuery.Promise} Promise resolved when window is closed
2068 OO
.ui
.Window
.prototype.close = function ( data
) {
2069 return this.manager
.closeWindow( this, data
);
2075 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2078 * @param {Object} [data] Window opening data
2079 * @return {jQuery.Promise} Promise resolved when window is setup
2081 OO
.ui
.Window
.prototype.setup = function ( data
) {
2083 deferred
= $.Deferred();
2085 this.$element
.show();
2086 this.visible
= true;
2087 this.getSetupProcess( data
).execute().done( function () {
2088 // Force redraw by asking the browser to measure the elements' widths
2089 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2090 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2094 return deferred
.promise();
2100 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2103 * @param {Object} [data] Window opening data
2104 * @return {jQuery.Promise} Promise resolved when window is ready
2106 OO
.ui
.Window
.prototype.ready = function ( data
) {
2108 deferred
= $.Deferred();
2110 this.$content
.focus();
2111 this.getReadyProcess( data
).execute().done( function () {
2112 // Force redraw by asking the browser to measure the elements' widths
2113 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2114 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2118 return deferred
.promise();
2124 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2127 * @param {Object} [data] Window closing data
2128 * @return {jQuery.Promise} Promise resolved when window is held
2130 OO
.ui
.Window
.prototype.hold = function ( data
) {
2132 deferred
= $.Deferred();
2134 this.getHoldProcess( data
).execute().done( function () {
2135 // Get the focused element within the window's content
2136 var $focus
= win
.$content
.find( OO
.ui
.Element
.static.getDocument( win
.$content
).activeElement
);
2138 // Blur the focused element
2139 if ( $focus
.length
) {
2143 // Force redraw by asking the browser to measure the elements' widths
2144 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2145 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2149 return deferred
.promise();
2155 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2158 * @param {Object} [data] Window closing data
2159 * @return {jQuery.Promise} Promise resolved when window is torn down
2161 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2163 deferred
= $.Deferred();
2165 this.getTeardownProcess( data
).execute().done( function () {
2166 // Force redraw by asking the browser to measure the elements' widths
2167 win
.$element
.removeClass( 'oo-ui-window-load oo-ui-window-setup' ).width();
2168 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2169 win
.$element
.hide();
2170 win
.visible
= false;
2174 return deferred
.promise();
2178 * Load the frame contents.
2180 * Once the iframe's stylesheets are loaded the returned promise will be resolved. Calling while
2181 * loading will return a promise but not trigger a new loading cycle. Calling after loading is
2182 * complete will return a promise that's already been resolved.
2184 * Sounds simple right? Read on...
2186 * When you create a dynamic iframe using open/write/close, the window.load event for the
2187 * iframe is triggered when you call close, and there's no further load event to indicate that
2188 * everything is actually loaded.
2190 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2191 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2192 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2193 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2194 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2196 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2197 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2198 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2199 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2200 * accessing .cssRules is allowed.
2202 * However, now that we control the styles we're injecting, we might as well do away with
2203 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2204 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2205 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2206 * font-family rule is not applied until after the `@import` finishes.
2208 * All this stylesheet injection and polling magic is in #transplantStyles.
2210 * @return {jQuery.Promise} Promise resolved when loading is complete
2212 OO
.ui
.Window
.prototype.load = function () {
2213 var sub
, doc
, loading
,
2216 this.$element
.addClass( 'oo-ui-window-load' );
2218 // Non-isolated windows are already "loaded"
2219 if ( !this.loading
&& !this.isolated
) {
2220 this.loading
= $.Deferred().resolve();
2222 // Set initialized state after so sub-classes aren't confused by it being set by calling
2223 // their parent initialize method
2224 this.initialized
= true;
2227 // Return existing promise if already loading or loaded
2228 if ( this.loading
) {
2229 return this.loading
.promise();
2233 loading
= this.loading
= $.Deferred();
2234 sub
= this.$iframe
.prop( 'contentWindow' );
2237 // Initialize contents
2242 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2243 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2244 '<div class="oo-ui-window-content"></div>' +
2251 this.$ = OO
.ui
.Element
.static.getJQuery( doc
, this.$iframe
);
2252 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2253 this.$document
= this.$( doc
);
2256 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2257 .always( function () {
2258 // Initialize isolated windows
2260 // Set initialized state after so sub-classes aren't confused by it being set by calling
2261 // their parent initialize method
2262 win
.initialized
= true;
2263 // Undo the visibility: hidden; hack and apply display: none;
2264 // We can do this safely now that the iframe has initialized
2265 // (don't do this from within #initialize because it has to happen
2266 // after the all subclasses have been handled as well).
2267 win
.toggle( win
.isVisible() );
2272 return loading
.promise();
2276 * Base class for all dialogs.
2279 * - Manage the window (open and close, etc.).
2280 * - Store the internal name and display title.
2281 * - A stack to track one or more pending actions.
2282 * - Manage a set of actions that can be performed.
2283 * - Configure and create action widgets.
2286 * - Close the dialog with Escape key.
2287 * - Visually lock the dialog while an action is in
2288 * progress (aka "pending").
2290 * Subclass responsibilities:
2291 * - Display the title somewhere.
2292 * - Add content to the dialog.
2293 * - Provide a UI to close the dialog.
2294 * - Display the action widgets somewhere.
2298 * @extends OO.ui.Window
2299 * @mixins OO.ui.PendingElement
2302 * @param {Object} [config] Configuration options
2304 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2305 // Parent constructor
2306 OO
.ui
.Dialog
.super.call( this, config
);
2308 // Mixin constructors
2309 OO
.ui
.PendingElement
.call( this );
2312 this.actions
= new OO
.ui
.ActionSet();
2313 this.attachedActions
= [];
2314 this.currentAction
= null;
2315 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2318 this.actions
.connect( this, {
2319 click
: 'onActionClick',
2320 resize
: 'onActionResize',
2321 change
: 'onActionsChange'
2326 .addClass( 'oo-ui-dialog' )
2327 .attr( 'role', 'dialog' );
2332 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2333 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2335 /* Static Properties */
2338 * Symbolic name of dialog.
2343 * @property {string}
2345 OO
.ui
.Dialog
.static.name
= '';
2353 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2355 OO
.ui
.Dialog
.static.title
= '';
2358 * List of OO.ui.ActionWidget configuration options.
2362 * @property {Object[]}
2364 OO
.ui
.Dialog
.static.actions
= [];
2367 * Close dialog when the escape key is pressed.
2372 * @property {boolean}
2374 OO
.ui
.Dialog
.static.escapable
= true;
2379 * Handle frame document key down events.
2381 * @param {jQuery.Event} e Key down event
2383 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2384 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2387 e
.stopPropagation();
2392 * Handle action resized events.
2394 * @param {OO.ui.ActionWidget} action Action that was resized
2396 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2397 // Override in subclass
2401 * Handle action click events.
2403 * @param {OO.ui.ActionWidget} action Action that was clicked
2405 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2406 if ( !this.isPending() ) {
2407 this.currentAction
= action
;
2408 this.executeAction( action
.getAction() );
2413 * Handle actions change event.
2415 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2416 this.detachActions();
2417 if ( !this.isClosing() ) {
2418 this.attachActions();
2423 * Get set of actions.
2425 * @return {OO.ui.ActionSet}
2427 OO
.ui
.Dialog
.prototype.getActions = function () {
2428 return this.actions
;
2432 * Get a process for taking action.
2434 * When you override this method, you can add additional accept steps to the process the parent
2435 * method provides using the 'first' and 'next' methods.
2438 * @param {string} [action] Symbolic name of action
2439 * @return {OO.ui.Process} Action process
2441 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2442 return new OO
.ui
.Process()
2443 .next( function () {
2445 // An empty action always closes the dialog without data, which should always be
2446 // safe and make no changes
2455 * @param {Object} [data] Dialog opening data
2456 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2457 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2458 * action item, omit to use #static-actions
2460 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2464 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2465 .next( function () {
2468 config
= this.constructor.static,
2469 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2471 this.title
.setLabel(
2472 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2474 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2476 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2479 this.actions
.add( items
);
2481 if ( this.constructor.static.escapable
) {
2482 this.$document
.on( 'keydown', this.onDocumentKeyDownHandler
);
2490 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2492 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2493 .first( function () {
2494 if ( this.constructor.static.escapable
) {
2495 this.$document
.off( 'keydown', this.onDocumentKeyDownHandler
);
2498 this.actions
.clear();
2499 this.currentAction
= null;
2506 OO
.ui
.Dialog
.prototype.initialize = function () {
2508 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2511 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2514 this.$content
.addClass( 'oo-ui-dialog-content' );
2515 this.setPendingElement( this.$head
);
2519 * Attach action actions.
2521 OO
.ui
.Dialog
.prototype.attachActions = function () {
2522 // Remember the list of potentially attached actions
2523 this.attachedActions
= this.actions
.get();
2527 * Detach action actions.
2531 OO
.ui
.Dialog
.prototype.detachActions = function () {
2534 // Detach all actions that may have been previously attached
2535 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2536 this.attachedActions
[i
].$element
.detach();
2538 this.attachedActions
= [];
2542 * Execute an action.
2544 * @param {string} action Symbolic name of action to execute
2545 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2547 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2549 return this.getActionProcess( action
).execute()
2550 .always( this.popPending
.bind( this ) );
2554 * Collection of windows.
2557 * @extends OO.ui.Element
2558 * @mixins OO.EventEmitter
2560 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2561 * already opening or opened, the current window will be closed without data. Empty closing data
2562 * should always result in the window being closed without causing constructive or destructive
2565 * As a window is opened and closed, it passes through several stages and the manager emits several
2566 * corresponding events.
2568 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2569 * - {@link #event-opening} is emitted with `opening` promise
2570 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2571 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2572 * - `setup` progress notification is emitted from opening promise
2573 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2574 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2575 * - `ready` progress notification is emitted from opening promise
2576 * - `opening` promise is resolved with `opened` promise
2577 * - Window is now open
2579 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2580 * - `opened` promise is resolved with `closing` promise
2581 * - {@link #event-closing} is emitted with `closing` promise
2582 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2583 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2584 * - `hold` progress notification is emitted from opening promise
2585 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2586 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2587 * - `teardown` progress notification is emitted from opening promise
2588 * - Closing promise is resolved
2589 * - Window is now closed
2592 * @param {Object} [config] Configuration options
2593 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2594 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2595 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2597 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2598 // Configuration initialization
2599 config
= config
|| {};
2601 // Parent constructor
2602 OO
.ui
.WindowManager
.super.call( this, config
);
2604 // Mixin constructors
2605 OO
.EventEmitter
.call( this );
2608 this.factory
= config
.factory
;
2609 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2610 this.isolate
= !!config
.isolate
;
2612 this.opening
= null;
2614 this.closing
= null;
2615 this.preparingToOpen
= null;
2616 this.preparingToClose
= null;
2618 this.currentWindow
= null;
2619 this.$ariaHidden
= null;
2620 this.requestedSize
= null;
2621 this.onWindowResizeTimeout
= null;
2622 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2623 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2624 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2625 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2629 .addClass( 'oo-ui-windowManager' )
2630 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2635 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2636 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2641 * Window is opening.
2643 * Fired when the window begins to be opened.
2646 * @param {OO.ui.Window} win Window that's being opened
2647 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2648 * resolved the first argument will be a promise which will be resolved when the window begins
2649 * closing, the second argument will be the opening data; progress notifications will be fired on
2650 * the promise for `setup` and `ready` when those processes are completed respectively.
2651 * @param {Object} data Window opening data
2655 * Window is closing.
2657 * Fired when the window begins to be closed.
2660 * @param {OO.ui.Window} win Window that's being closed
2661 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2662 * is resolved the first argument will be a the closing data; progress notifications will be fired
2663 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2664 * @param {Object} data Window closing data
2668 * Window was resized.
2671 * @param {OO.ui.Window} win Window that was resized
2674 /* Static Properties */
2677 * Map of symbolic size names and CSS properties.
2681 * @property {Object}
2683 OO
.ui
.WindowManager
.static.sizes
= {
2697 // These can be non-numeric because they are never used in calculations
2704 * Symbolic name of default size.
2706 * Default size is used if the window's requested size is not recognized.
2710 * @property {string}
2712 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2717 * Handle window resize events.
2719 * @param {jQuery.Event} e Window resize event
2721 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2722 clearTimeout( this.onWindowResizeTimeout
);
2723 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2727 * Handle window resize events.
2729 * @param {jQuery.Event} e Window resize event
2731 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2732 if ( this.currentWindow
) {
2733 this.updateWindowSize( this.currentWindow
);
2738 * Handle window mouse wheel events.
2740 * @param {jQuery.Event} e Mouse wheel event
2742 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2743 // Kill all events in the parent window if the child window is isolated
2744 return !this.shouldIsolate();
2748 * Handle document key down events.
2750 * @param {jQuery.Event} e Key down event
2752 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2753 switch ( e
.which
) {
2754 case OO
.ui
.Keys
.PAGEUP
:
2755 case OO
.ui
.Keys
.PAGEDOWN
:
2756 case OO
.ui
.Keys
.END
:
2757 case OO
.ui
.Keys
.HOME
:
2758 case OO
.ui
.Keys
.LEFT
:
2760 case OO
.ui
.Keys
.RIGHT
:
2761 case OO
.ui
.Keys
.DOWN
:
2762 // Kill all events in the parent window if the child window is isolated
2763 return !this.shouldIsolate();
2768 * Check if window is opening.
2770 * @return {boolean} Window is opening
2772 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2773 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2777 * Check if window is closing.
2779 * @return {boolean} Window is closing
2781 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2782 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2786 * Check if window is opened.
2788 * @return {boolean} Window is opened
2790 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2791 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2795 * Check if window contents should be isolated.
2797 * Window content isolation is done using inline frames.
2799 * @return {boolean} Window contents should be isolated
2801 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2802 return this.isolate
;
2806 * Check if a window is being managed.
2808 * @param {OO.ui.Window} win Window to check
2809 * @return {boolean} Window is being managed
2811 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2814 for ( name
in this.windows
) {
2815 if ( this.windows
[name
] === win
) {
2824 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2826 * @param {OO.ui.Window} win Window being opened
2827 * @param {Object} [data] Window opening data
2828 * @return {number} Milliseconds to wait
2830 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2835 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2837 * @param {OO.ui.Window} win Window being opened
2838 * @param {Object} [data] Window opening data
2839 * @return {number} Milliseconds to wait
2841 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2846 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2848 * @param {OO.ui.Window} win Window being closed
2849 * @param {Object} [data] Window closing data
2850 * @return {number} Milliseconds to wait
2852 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2857 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2859 * @param {OO.ui.Window} win Window being closed
2860 * @param {Object} [data] Window closing data
2861 * @return {number} Milliseconds to wait
2863 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2864 return this.modal
? 250 : 0;
2868 * Get managed window by symbolic name.
2870 * If window is not yet instantiated, it will be instantiated and added automatically.
2872 * @param {string} name Symbolic window name
2873 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2874 * @throws {Error} If the symbolic name is unrecognized by the factory
2875 * @throws {Error} If the symbolic name unrecognized as a managed window
2877 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2878 var deferred
= $.Deferred(),
2879 win
= this.windows
[name
];
2881 if ( !( win
instanceof OO
.ui
.Window
) ) {
2882 if ( this.factory
) {
2883 if ( !this.factory
.lookup( name
) ) {
2884 deferred
.reject( new OO
.ui
.Error(
2885 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2888 win
= this.factory
.create( name
, this, { $: this.$ } );
2889 this.addWindows( [ win
] );
2890 deferred
.resolve( win
);
2893 deferred
.reject( new OO
.ui
.Error(
2894 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2898 deferred
.resolve( win
);
2901 return deferred
.promise();
2905 * Get current window.
2907 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2909 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2910 return this.currentWindow
;
2916 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2917 * @param {Object} [data] Window opening data
2918 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2919 * for more details about the `opening` promise
2922 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2925 opening
= $.Deferred();
2927 // Argument handling
2928 if ( typeof win
=== 'string' ) {
2929 return this.getWindow( win
).then( function ( win
) {
2930 return manager
.openWindow( win
, data
);
2935 if ( !this.hasWindow( win
) ) {
2936 opening
.reject( new OO
.ui
.Error(
2937 'Cannot open window: window is not attached to manager'
2939 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2940 opening
.reject( new OO
.ui
.Error(
2941 'Cannot open window: another window is opening or open'
2946 if ( opening
.state() !== 'rejected' ) {
2947 if ( !win
.getManager() ) {
2948 win
.setManager( this );
2950 preparing
.push( win
.load() );
2952 if ( this.closing
) {
2953 // If a window is currently closing, wait for it to complete
2954 preparing
.push( this.closing
);
2957 this.preparingToOpen
= $.when
.apply( $, preparing
);
2958 // Ensure handlers get called after preparingToOpen is set
2959 this.preparingToOpen
.done( function () {
2960 if ( manager
.modal
) {
2961 manager
.toggleGlobalEvents( true );
2962 manager
.toggleAriaIsolation( true );
2964 manager
.currentWindow
= win
;
2965 manager
.opening
= opening
;
2966 manager
.preparingToOpen
= null;
2967 manager
.emit( 'opening', win
, opening
, data
);
2968 setTimeout( function () {
2969 win
.setup( data
).then( function () {
2970 manager
.updateWindowSize( win
);
2971 manager
.opening
.notify( { state
: 'setup' } );
2972 setTimeout( function () {
2973 win
.ready( data
).then( function () {
2974 manager
.opening
.notify( { state
: 'ready' } );
2975 manager
.opening
= null;
2976 manager
.opened
= $.Deferred();
2977 opening
.resolve( manager
.opened
.promise(), data
);
2979 }, manager
.getReadyDelay() );
2981 }, manager
.getSetupDelay() );
2985 return opening
.promise();
2991 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2992 * @param {Object} [data] Window closing data
2993 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2994 * for more details about the `closing` promise
2995 * @throws {Error} If no window by that name is being managed
2998 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
3001 closing
= $.Deferred(),
3004 // Argument handling
3005 if ( typeof win
=== 'string' ) {
3006 win
= this.windows
[win
];
3007 } else if ( !this.hasWindow( win
) ) {
3013 closing
.reject( new OO
.ui
.Error(
3014 'Cannot close window: window is not attached to manager'
3016 } else if ( win
!== this.currentWindow
) {
3017 closing
.reject( new OO
.ui
.Error(
3018 'Cannot close window: window already closed with different data'
3020 } else if ( this.preparingToClose
|| this.closing
) {
3021 closing
.reject( new OO
.ui
.Error(
3022 'Cannot close window: window already closing with different data'
3027 if ( closing
.state() !== 'rejected' ) {
3028 if ( this.opening
) {
3029 // If the window is currently opening, close it when it's done
3030 preparing
.push( this.opening
);
3033 this.preparingToClose
= $.when
.apply( $, preparing
);
3034 // Ensure handlers get called after preparingToClose is set
3035 this.preparingToClose
.done( function () {
3036 manager
.closing
= closing
;
3037 manager
.preparingToClose
= null;
3038 manager
.emit( 'closing', win
, closing
, data
);
3039 opened
= manager
.opened
;
3040 manager
.opened
= null;
3041 opened
.resolve( closing
.promise(), data
);
3042 setTimeout( function () {
3043 win
.hold( data
).then( function () {
3044 closing
.notify( { state
: 'hold' } );
3045 setTimeout( function () {
3046 win
.teardown( data
).then( function () {
3047 closing
.notify( { state
: 'teardown' } );
3048 if ( manager
.modal
) {
3049 manager
.toggleGlobalEvents( false );
3050 manager
.toggleAriaIsolation( false );
3052 manager
.closing
= null;
3053 manager
.currentWindow
= null;
3054 closing
.resolve( data
);
3056 }, manager
.getTeardownDelay() );
3058 }, manager
.getHoldDelay() );
3062 return closing
.promise();
3068 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
3069 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
3070 * a statically configured symbolic name
3072 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
3073 var i
, len
, win
, name
, list
;
3075 if ( $.isArray( windows
) ) {
3076 // Convert to map of windows by looking up symbolic names from static configuration
3078 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
3079 name
= windows
[i
].constructor.static.name
;
3080 if ( typeof name
!== 'string' ) {
3081 throw new Error( 'Cannot add window' );
3083 list
[name
] = windows
[i
];
3085 } else if ( $.isPlainObject( windows
) ) {
3090 for ( name
in list
) {
3092 this.windows
[name
] = win
;
3093 this.$element
.append( win
.$element
);
3100 * Windows will be closed before they are removed.
3102 * @param {string[]} names Symbolic names of windows to remove
3103 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3104 * @throws {Error} If windows being removed are not being managed
3106 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3107 var i
, len
, win
, name
, cleanupWindow
,
3110 cleanup = function ( name
, win
) {
3111 delete manager
.windows
[name
];
3112 win
.$element
.detach();
3115 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3117 win
= this.windows
[name
];
3119 throw new Error( 'Cannot remove window' );
3121 cleanupWindow
= cleanup
.bind( null, name
, win
);
3122 promises
.push( this.closeWindow( name
).then( cleanupWindow
, cleanupWindow
) );
3125 return $.when
.apply( $, promises
);
3129 * Remove all windows.
3131 * Windows will be closed before they are removed.
3133 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3135 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3136 return this.removeWindows( Object
.keys( this.windows
) );
3142 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3146 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3147 // Bypass for non-current, and thus invisible, windows
3148 if ( win
!== this.currentWindow
) {
3152 var viewport
= OO
.ui
.Element
.static.getDimensions( win
.getElementWindow() ),
3153 sizes
= this.constructor.static.sizes
,
3154 size
= win
.getSize();
3156 if ( !sizes
[size
] ) {
3157 size
= this.constructor.static.defaultSize
;
3159 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
3163 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3164 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3165 win
.setDimensions( sizes
[size
] );
3167 this.emit( 'resize', win
);
3173 * Bind or unbind global events for scrolling.
3175 * @param {boolean} [on] Bind global events
3178 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3179 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3182 if ( !this.globalEvents
) {
3183 this.$( this.getElementDocument() ).on( {
3184 // Prevent scrolling by keys in top-level window
3185 keydown
: this.onDocumentKeyDownHandler
3187 this.$( this.getElementWindow() ).on( {
3188 // Prevent scrolling by wheel in top-level window
3189 mousewheel
: this.onWindowMouseWheelHandler
,
3190 // Start listening for top-level window dimension changes
3191 'orientationchange resize': this.onWindowResizeHandler
3193 // Disable window scrolling in isolated windows
3194 if ( !this.shouldIsolate() ) {
3195 $( this.getElementDocument().body
).css( 'overflow', 'hidden' );
3197 this.globalEvents
= true;
3199 } else if ( this.globalEvents
) {
3200 // Unbind global events
3201 this.$( this.getElementDocument() ).off( {
3202 // Allow scrolling by keys in top-level window
3203 keydown
: this.onDocumentKeyDownHandler
3205 this.$( this.getElementWindow() ).off( {
3206 // Allow scrolling by wheel in top-level window
3207 mousewheel
: this.onWindowMouseWheelHandler
,
3208 // Stop listening for top-level window dimension changes
3209 'orientationchange resize': this.onWindowResizeHandler
3211 if ( !this.shouldIsolate() ) {
3212 $( this.getElementDocument().body
).css( 'overflow', '' );
3214 this.globalEvents
= false;
3221 * Toggle screen reader visibility of content other than the window manager.
3223 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3226 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3227 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3230 if ( !this.$ariaHidden
) {
3231 // Hide everything other than the window manager from screen readers
3232 this.$ariaHidden
= $( 'body' )
3234 .not( this.$element
.parentsUntil( 'body' ).last() )
3235 .attr( 'aria-hidden', '' );
3237 } else if ( this.$ariaHidden
) {
3238 // Restore screen reader visibility
3239 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3240 this.$ariaHidden
= null;
3247 * Destroy window manager.
3249 OO
.ui
.WindowManager
.prototype.destroy = function () {
3250 this.toggleGlobalEvents( false );
3251 this.toggleAriaIsolation( false );
3252 this.clearWindows();
3253 this.$element
.remove();
3260 * @param {string|jQuery} message Description of error
3261 * @param {Object} [config] Configuration options
3262 * @cfg {boolean} [recoverable=true] Error is recoverable
3263 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3265 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3266 // Configuration initialization
3267 config
= config
|| {};
3270 this.message
= message
instanceof jQuery
? message
: String( message
);
3271 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3272 this.warning
= !!config
.warning
;
3277 OO
.initClass( OO
.ui
.Error
);
3282 * Check if error can be recovered from.
3284 * @return {boolean} Error is recoverable
3286 OO
.ui
.Error
.prototype.isRecoverable = function () {
3287 return this.recoverable
;
3291 * Check if the error is a warning
3293 * @return {boolean} Error is warning
3295 OO
.ui
.Error
.prototype.isWarning = function () {
3296 return this.warning
;
3300 * Get error message as DOM nodes.
3302 * @return {jQuery} Error message in DOM nodes
3304 OO
.ui
.Error
.prototype.getMessage = function () {
3305 return this.message
instanceof jQuery
?
3306 this.message
.clone() :
3307 $( '<div>' ).text( this.message
).contents();
3311 * Get error message as text.
3313 * @return {string} Error message
3315 OO
.ui
.Error
.prototype.getMessageText = function () {
3316 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3320 * A list of functions, called in sequence.
3322 * If a function added to a process returns boolean false the process will stop; if it returns an
3323 * object with a `promise` method the process will use the promise to either continue to the next
3324 * step when the promise is resolved or stop when the promise is rejected.
3329 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3330 * call, see #createStep for more information
3331 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3333 * @return {Object} Step object, with `callback` and `context` properties
3335 OO
.ui
.Process = function ( step
, context
) {
3340 if ( step
!== undefined ) {
3341 this.next( step
, context
);
3347 OO
.initClass( OO
.ui
.Process
);
3352 * Start the process.
3354 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3355 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3356 * process, the remaining steps will not be taken
3358 OO
.ui
.Process
.prototype.execute = function () {
3359 var i
, len
, promise
;
3362 * Continue execution.
3365 * @param {Array} step A function and the context it should be called in
3366 * @return {Function} Function that continues the process
3368 function proceed( step
) {
3369 return function () {
3370 // Execute step in the correct context
3372 result
= step
.callback
.call( step
.context
);
3374 if ( result
=== false ) {
3375 // Use rejected promise for boolean false results
3376 return $.Deferred().reject( [] ).promise();
3378 if ( typeof result
=== 'number' ) {
3380 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3382 // Use a delayed promise for numbers, expecting them to be in milliseconds
3383 deferred
= $.Deferred();
3384 setTimeout( deferred
.resolve
, result
);
3385 return deferred
.promise();
3387 if ( result
instanceof OO
.ui
.Error
) {
3388 // Use rejected promise for error
3389 return $.Deferred().reject( [ result
] ).promise();
3391 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3392 // Use rejected promise for list of errors
3393 return $.Deferred().reject( result
).promise();
3395 // Duck-type the object to see if it can produce a promise
3396 if ( result
&& $.isFunction( result
.promise
) ) {
3397 // Use a promise generated from the result
3398 return result
.promise();
3400 // Use resolved promise for other results
3401 return $.Deferred().resolve().promise();
3405 if ( this.steps
.length
) {
3406 // Generate a chain reaction of promises
3407 promise
= proceed( this.steps
[0] )();
3408 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3409 promise
= promise
.then( proceed( this.steps
[i
] ) );
3412 promise
= $.Deferred().resolve().promise();
3419 * Create a process step.
3422 * @param {number|jQuery.Promise|Function} step
3424 * - Number of milliseconds to wait; or
3425 * - Promise to wait to be resolved; or
3426 * - Function to execute
3427 * - If it returns boolean false the process will stop
3428 * - If it returns an object with a `promise` method the process will use the promise to either
3429 * continue to the next step when the promise is resolved or stop when the promise is rejected
3430 * - If it returns a number, the process will wait for that number of milliseconds before
3432 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3434 * @return {Object} Step object, with `callback` and `context` properties
3436 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3437 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3439 callback: function () {
3445 if ( $.isFunction( step
) ) {
3451 throw new Error( 'Cannot create process step: number, promise or function expected' );
3455 * Add step to the beginning of the process.
3457 * @inheritdoc #createStep
3458 * @return {OO.ui.Process} this
3461 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3462 this.steps
.unshift( this.createStep( step
, context
) );
3467 * Add step to the end of the process.
3469 * @inheritdoc #createStep
3470 * @return {OO.ui.Process} this
3473 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3474 this.steps
.push( this.createStep( step
, context
) );
3479 * Factory for tools.
3482 * @extends OO.Factory
3485 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3486 // Parent constructor
3487 OO
.ui
.ToolFactory
.super.call( this );
3492 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3497 * Get tools from the factory
3499 * @param {Array} include Included tools
3500 * @param {Array} exclude Excluded tools
3501 * @param {Array} promote Promoted tools
3502 * @param {Array} demote Demoted tools
3503 * @return {string[]} List of tools
3505 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3506 var i
, len
, included
, promoted
, demoted
,
3510 // Collect included and not excluded tools
3511 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3514 promoted
= this.extract( promote
, used
);
3515 demoted
= this.extract( demote
, used
);
3518 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3519 if ( !used
[included
[i
]] ) {
3520 auto
.push( included
[i
] );
3524 return promoted
.concat( auto
).concat( demoted
);
3528 * Get a flat list of names from a list of names or groups.
3530 * Tools can be specified in the following ways:
3532 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3533 * - All tools in a group: `{ group: 'group-name' }`
3534 * - All tools: `'*'`
3537 * @param {Array|string} collection List of tools
3538 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3539 * names will be added as properties
3540 * @return {string[]} List of extracted names
3542 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3543 var i
, len
, item
, name
, tool
,
3546 if ( collection
=== '*' ) {
3547 for ( name
in this.registry
) {
3548 tool
= this.registry
[name
];
3550 // Only add tools by group name when auto-add is enabled
3551 tool
.static.autoAddToCatchall
&&
3552 // Exclude already used tools
3553 ( !used
|| !used
[name
] )
3561 } else if ( $.isArray( collection
) ) {
3562 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3563 item
= collection
[i
];
3564 // Allow plain strings as shorthand for named tools
3565 if ( typeof item
=== 'string' ) {
3566 item
= { name
: item
};
3568 if ( OO
.isPlainObject( item
) ) {
3570 for ( name
in this.registry
) {
3571 tool
= this.registry
[name
];
3573 // Include tools with matching group
3574 tool
.static.group
=== item
.group
&&
3575 // Only add tools by group name when auto-add is enabled
3576 tool
.static.autoAddToGroup
&&
3577 // Exclude already used tools
3578 ( !used
|| !used
[name
] )
3586 // Include tools with matching name and exclude already used tools
3587 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3588 names
.push( item
.name
);
3590 used
[item
.name
] = true;
3600 * Factory for tool groups.
3603 * @extends OO.Factory
3606 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3607 // Parent constructor
3608 OO
.Factory
.call( this );
3611 defaultClasses
= this.constructor.static.getDefaultClasses();
3613 // Register default toolgroups
3614 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3615 this.register( defaultClasses
[i
] );
3621 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3623 /* Static Methods */
3626 * Get a default set of classes to be registered on construction
3628 * @return {Function[]} Default classes
3630 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3633 OO
.ui
.ListToolGroup
,
3645 * @param {Object} [config] Configuration options
3647 OO
.ui
.Theme
= function OoUiTheme( config
) {
3648 // Configuration initialization
3649 config
= config
|| {};
3654 OO
.initClass( OO
.ui
.Theme
);
3659 * Get a list of classes to be applied to a widget.
3661 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3662 * otherwise state transitions will not work properly.
3664 * @param {OO.ui.Element} element Element for which to get classes
3665 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3667 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3668 return { on
: [], off
: [] };
3672 * Update CSS classes provided by the theme.
3674 * For elements with theme logic hooks, this should be called any time there's a state change.
3676 * @param {OO.ui.Element} element Element for which to update classes
3677 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3679 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3680 var classes
= this.getElementClasses( element
);
3683 .removeClass( classes
.off
.join( ' ' ) )
3684 .addClass( classes
.on
.join( ' ' ) );
3688 * Element with a button.
3690 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3691 * and access keys for accessibility purposes.
3697 * @param {Object} [config] Configuration options
3698 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3699 * @cfg {boolean} [framed=true] Render button with a frame
3700 * @cfg {number} [tabIndex=0] Button's tab index. Use 0 to use default ordering, use -1 to prevent
3702 * @cfg {string} [accessKey] Button's access key
3704 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3705 // Configuration initialization
3706 config
= config
|| {};
3709 this.$button
= null;
3711 this.tabIndex
= null;
3712 this.accessKey
= null;
3713 this.active
= false;
3714 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3715 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3718 this.$element
.addClass( 'oo-ui-buttonElement' );
3719 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3720 this.setTabIndex( config
.tabIndex
|| 0 );
3721 this.setAccessKey( config
.accessKey
);
3722 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3727 OO
.initClass( OO
.ui
.ButtonElement
);
3729 /* Static Properties */
3732 * Cancel mouse down events.
3736 * @property {boolean}
3738 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3743 * Set the button element.
3745 * If an element is already set, it will be cleaned up before setting up the new element.
3747 * @param {jQuery} $button Element to use as button
3749 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3750 if ( this.$button
) {
3752 .removeClass( 'oo-ui-buttonElement-button' )
3753 .removeAttr( 'role accesskey tabindex' )
3754 .off( 'mousedown', this.onMouseDownHandler
);
3757 this.$button
= $button
3758 .addClass( 'oo-ui-buttonElement-button' )
3759 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3760 .on( 'mousedown', this.onMouseDownHandler
);
3764 * Handles mouse down events.
3766 * @param {jQuery.Event} e Mouse down event
3768 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3769 if ( this.isDisabled() || e
.which
!== 1 ) {
3772 // Remove the tab-index while the button is down to prevent the button from stealing focus
3773 this.$button
.removeAttr( 'tabindex' );
3774 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3775 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3776 // reliably reapply the tabindex and remove the pressed class
3777 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3778 // Prevent change of focus unless specifically configured otherwise
3779 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3785 * Handles mouse up events.
3787 * @param {jQuery.Event} e Mouse up event
3789 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3790 if ( this.isDisabled() || e
.which
!== 1 ) {
3793 // Restore the tab-index after the button is up to restore the button's accessibility
3794 this.$button
.attr( 'tabindex', this.tabIndex
);
3795 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3796 // Stop listening for mouseup, since we only needed this once
3797 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3801 * Check if button has a frame.
3803 * @return {boolean} Button is framed
3805 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3812 * @param {boolean} [framed] Make button framed, omit to toggle
3815 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3816 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3817 if ( framed
!== this.framed
) {
3818 this.framed
= framed
;
3820 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3821 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3822 this.updateThemeClasses();
3831 * @param {number|null} tabIndex Button's tab index, use null to remove
3834 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3835 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3837 if ( this.tabIndex
!== tabIndex
) {
3838 if ( this.$button
) {
3839 if ( tabIndex
!== null ) {
3840 this.$button
.attr( 'tabindex', tabIndex
);
3842 this.$button
.removeAttr( 'tabindex' );
3845 this.tabIndex
= tabIndex
;
3854 * @param {string} accessKey Button's access key, use empty string to remove
3857 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3858 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3860 if ( this.accessKey
!== accessKey
) {
3861 if ( this.$button
) {
3862 if ( accessKey
!== null ) {
3863 this.$button
.attr( 'accesskey', accessKey
);
3865 this.$button
.removeAttr( 'accesskey' );
3868 this.accessKey
= accessKey
;
3877 * @param {boolean} [value] Make button active
3880 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3881 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3886 * Element containing a sequence of child elements.
3892 * @param {Object} [config] Configuration options
3893 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3895 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3896 // Configuration initialization
3897 config
= config
|| {};
3902 this.aggregateItemEvents
= {};
3905 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3911 * Set the group element.
3913 * If an element is already set, items will be moved to the new element.
3915 * @param {jQuery} $group Element to use as group
3917 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3920 this.$group
= $group
;
3921 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3922 this.$group
.append( this.items
[i
].$element
);
3927 * Check if there are no items.
3929 * @return {boolean} Group is empty
3931 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3932 return !this.items
.length
;
3938 * @return {OO.ui.Element[]} Items
3940 OO
.ui
.GroupElement
.prototype.getItems = function () {
3941 return this.items
.slice( 0 );
3945 * Get an item by its data.
3947 * Data is compared by a hash of its value. Only the first item with matching data will be returned.
3949 * @param {Object} data Item data to search for
3950 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
3952 OO
.ui
.GroupElement
.prototype.getItemFromData = function ( data
) {
3954 hash
= OO
.getHash( data
);
3956 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3957 item
= this.items
[i
];
3958 if ( hash
=== OO
.getHash( item
.getData() ) ) {
3967 * Get items by their data.
3969 * Data is compared by a hash of its value. All items with matching data will be returned.
3971 * @param {Object} data Item data to search for
3972 * @return {OO.ui.Element[]} Items with equivalent data
3974 OO
.ui
.GroupElement
.prototype.getItemsFromData = function ( data
) {
3976 hash
= OO
.getHash( data
),
3979 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3980 item
= this.items
[i
];
3981 if ( hash
=== OO
.getHash( item
.getData() ) ) {
3990 * Add an aggregate item event.
3992 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3993 * and with an additional leading parameter containing the item that emitted the original event.
3994 * Other arguments that were emitted from the original event are passed through.
3996 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3997 * event, use null value to remove aggregation
3998 * @throws {Error} If aggregation already exists
4000 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
4001 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
4003 for ( itemEvent
in events
) {
4004 groupEvent
= events
[itemEvent
];
4006 // Remove existing aggregated event
4007 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4008 // Don't allow duplicate aggregations
4010 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
4012 // Remove event aggregation from existing items
4013 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4014 item
= this.items
[i
];
4015 if ( item
.connect
&& item
.disconnect
) {
4017 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
4018 item
.disconnect( this, remove
);
4021 // Prevent future items from aggregating event
4022 delete this.aggregateItemEvents
[itemEvent
];
4025 // Add new aggregate event
4027 // Make future items aggregate event
4028 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
4029 // Add event aggregation to existing items
4030 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4031 item
= this.items
[i
];
4032 if ( item
.connect
&& item
.disconnect
) {
4034 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
4035 item
.connect( this, add
);
4045 * Adding an existing item will move it.
4047 * @param {OO.ui.Element[]} items Items
4048 * @param {number} [index] Index to insert items at
4051 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
4052 var i
, len
, item
, event
, events
, currentIndex
,
4055 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4058 // Check if item exists then remove it first, effectively "moving" it
4059 currentIndex
= $.inArray( item
, this.items
);
4060 if ( currentIndex
>= 0 ) {
4061 this.removeItems( [ item
] );
4062 // Adjust index to compensate for removal
4063 if ( currentIndex
< index
) {
4068 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
4070 for ( event
in this.aggregateItemEvents
) {
4071 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
4073 item
.connect( this, events
);
4075 item
.setElementGroup( this );
4076 itemElements
.push( item
.$element
.get( 0 ) );
4079 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
4080 this.$group
.append( itemElements
);
4081 this.items
.push
.apply( this.items
, items
);
4082 } else if ( index
=== 0 ) {
4083 this.$group
.prepend( itemElements
);
4084 this.items
.unshift
.apply( this.items
, items
);
4086 this.items
[index
].$element
.before( itemElements
);
4087 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
4096 * Items will be detached, not removed, so they can be used later.
4098 * @param {OO.ui.Element[]} items Items to remove
4101 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
4102 var i
, len
, item
, index
, remove
, itemEvent
;
4104 // Remove specific items
4105 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4107 index
= $.inArray( item
, this.items
);
4108 if ( index
!== -1 ) {
4110 item
.connect
&& item
.disconnect
&&
4111 !$.isEmptyObject( this.aggregateItemEvents
)
4114 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4115 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4117 item
.disconnect( this, remove
);
4119 item
.setElementGroup( null );
4120 this.items
.splice( index
, 1 );
4121 item
.$element
.detach();
4131 * Items will be detached, not removed, so they can be used later.
4135 OO
.ui
.GroupElement
.prototype.clearItems = function () {
4136 var i
, len
, item
, remove
, itemEvent
;
4139 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4140 item
= this.items
[i
];
4142 item
.connect
&& item
.disconnect
&&
4143 !$.isEmptyObject( this.aggregateItemEvents
)
4146 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4147 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4149 item
.disconnect( this, remove
);
4151 item
.setElementGroup( null );
4152 item
.$element
.detach();
4160 * A mixin for an element that can be dragged and dropped.
4161 * Use in conjunction with DragGroupWidget
4168 OO
.ui
.DraggableElement
= function OoUiDraggableElement() {
4172 // Initialize and events
4174 .attr( 'draggable', true )
4175 .addClass( 'oo-ui-draggableElement' )
4177 dragstart
: this.onDragStart
.bind( this ),
4178 dragover
: this.onDragOver
.bind( this ),
4179 dragend
: this.onDragEnd
.bind( this ),
4180 drop
: this.onDrop
.bind( this )
4188 * @param {OO.ui.DraggableElement} item Dragging item
4202 * Respond to dragstart event.
4203 * @param {jQuery.Event} event jQuery event
4206 OO
.ui
.DraggableElement
.prototype.onDragStart = function ( e
) {
4207 var dataTransfer
= e
.originalEvent
.dataTransfer
;
4208 // Define drop effect
4209 dataTransfer
.dropEffect
= 'none';
4210 dataTransfer
.effectAllowed
= 'move';
4211 // We must set up a dataTransfer data property or Firefox seems to
4212 // ignore the fact the element is draggable.
4214 dataTransfer
.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
4216 // The above is only for firefox. No need to set a catch clause
4217 // if it fails, move on.
4219 // Add dragging class
4220 this.$element
.addClass( 'oo-ui-draggableElement-dragging' );
4222 this.emit( 'dragstart', this );
4227 * Respond to dragend event.
4230 OO
.ui
.DraggableElement
.prototype.onDragEnd = function () {
4231 this.$element
.removeClass( 'oo-ui-draggableElement-dragging' );
4232 this.emit( 'dragend' );
4236 * Handle drop event.
4237 * @param {jQuery.Event} event jQuery event
4240 OO
.ui
.DraggableElement
.prototype.onDrop = function ( e
) {
4242 this.emit( 'drop', e
);
4246 * In order for drag/drop to work, the dragover event must
4247 * return false and stop propogation.
4249 OO
.ui
.DraggableElement
.prototype.onDragOver = function ( e
) {
4255 * Store it in the DOM so we can access from the widget drag event
4256 * @param {number} Item index
4258 OO
.ui
.DraggableElement
.prototype.setIndex = function ( index
) {
4259 if ( this.index
!== index
) {
4261 this.$element
.data( 'index', index
);
4267 * @return {number} Item index
4269 OO
.ui
.DraggableElement
.prototype.getIndex = function () {
4274 * Element containing a sequence of child elements that can be dragged
4281 * @param {Object} [config] Configuration options
4282 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
4283 * @cfg {string} [orientation] Item orientation, 'horizontal' or 'vertical'. Defaults to 'vertical'
4285 OO
.ui
.DraggableGroupElement
= function OoUiDraggableGroupElement( config
) {
4286 // Configuration initialization
4287 config
= config
|| {};
4289 // Parent constructor
4290 OO
.ui
.GroupElement
.call( this, config
);
4293 this.orientation
= config
.orientation
|| 'vertical';
4294 this.dragItem
= null;
4295 this.itemDragOver
= null;
4297 this.sideInsertion
= '';
4301 dragstart
: 'itemDragStart',
4302 dragend
: 'itemDragEnd',
4305 this.connect( this, {
4306 itemDragStart
: 'onItemDragStart',
4307 itemDrop
: 'onItemDrop',
4308 itemDragEnd
: 'onItemDragEnd'
4311 dragover
: $.proxy( this.onDragOver
, this ),
4312 dragleave
: $.proxy( this.onDragLeave
, this )
4316 if ( $.isArray( config
.items
) ) {
4317 this.addItems( config
.items
);
4319 this.$placeholder
= $( '<div>' )
4320 .addClass( 'oo-ui-draggableGroupElement-placeholder' );
4322 .addClass( 'oo-ui-draggableGroupElement' )
4323 .append( this.$status
)
4324 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' )
4325 .prepend( this.$placeholder
);
4329 OO
.mixinClass( OO
.ui
.DraggableGroupElement
, OO
.ui
.GroupElement
);
4335 * @param {OO.ui.DraggableElement} item Reordered item
4336 * @param {number} [newIndex] New index for the item
4342 * Respond to item drag start event
4343 * @param {OO.ui.DraggableElement} item Dragged item
4345 OO
.ui
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
4348 // Map the index of each object
4349 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4350 this.items
[i
].setIndex( i
);
4353 if ( this.orientation
=== 'horizontal' ) {
4354 // Set the height of the indicator
4355 this.$placeholder
.css( {
4356 height
: item
.$element
.outerHeight(),
4360 // Set the width of the indicator
4361 this.$placeholder
.css( {
4363 width
: item
.$element
.outerWidth()
4366 this.setDragItem( item
);
4370 * Respond to item drag end event
4372 OO
.ui
.DraggableGroupElement
.prototype.onItemDragEnd = function () {
4373 this.unsetDragItem();
4378 * Handle drop event and switch the order of the items accordingly
4379 * @param {OO.ui.DraggableElement} item Dropped item
4382 OO
.ui
.DraggableGroupElement
.prototype.onItemDrop = function ( item
) {
4383 var toIndex
= item
.getIndex();
4384 // Check if the dropped item is from the current group
4385 // TODO: Figure out a way to configure a list of legally droppable
4386 // elements even if they are not yet in the list
4387 if ( this.getDragItem() ) {
4388 // If the insertion point is 'after', the insertion index
4389 // is shifted to the right (or to the left in RTL, hence 'after')
4390 if ( this.sideInsertion
=== 'after' ) {
4393 // Emit change event
4394 this.emit( 'reorder', this.getDragItem(), toIndex
);
4396 // Return false to prevent propogation
4401 * Handle dragleave event.
4403 OO
.ui
.DraggableGroupElement
.prototype.onDragLeave = function () {
4404 // This means the item was dragged outside the widget
4411 * Respond to dragover event
4412 * @param {jQuery.Event} event Event details
4414 OO
.ui
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
4415 var dragOverObj
, $optionWidget
, itemOffset
, itemMidpoint
, itemBoundingRect
,
4416 itemSize
, cssOutput
, dragPosition
, itemIndex
, itemPosition
,
4417 clientX
= e
.originalEvent
.clientX
,
4418 clientY
= e
.originalEvent
.clientY
;
4420 // Get the OptionWidget item we are dragging over
4421 dragOverObj
= this.getElementDocument().elementFromPoint( clientX
, clientY
);
4422 $optionWidget
= $( dragOverObj
).closest( '.oo-ui-draggableElement' );
4423 if ( $optionWidget
[0] ) {
4424 itemOffset
= $optionWidget
.offset();
4425 itemBoundingRect
= $optionWidget
[0].getBoundingClientRect();
4426 itemPosition
= $optionWidget
.position();
4427 itemIndex
= $optionWidget
.data( 'index' );
4432 this.isDragging() &&
4433 itemIndex
!== this.getDragItem().getIndex()
4435 if ( this.orientation
=== 'horizontal' ) {
4436 // Calculate where the mouse is relative to the item width
4437 itemSize
= itemBoundingRect
.width
;
4438 itemMidpoint
= itemBoundingRect
.left
+ itemSize
/ 2;
4439 dragPosition
= clientX
;
4440 // Which side of the item we hover over will dictate
4441 // where the placeholder will appear, on the left or
4444 left
: dragPosition
< itemMidpoint
? itemPosition
.left
: itemPosition
.left
+ itemSize
,
4445 top
: itemPosition
.top
4448 // Calculate where the mouse is relative to the item height
4449 itemSize
= itemBoundingRect
.height
;
4450 itemMidpoint
= itemBoundingRect
.top
+ itemSize
/ 2;
4451 dragPosition
= clientY
;
4452 // Which side of the item we hover over will dictate
4453 // where the placeholder will appear, on the top or
4456 top
: dragPosition
< itemMidpoint
? itemPosition
.top
: itemPosition
.top
+ itemSize
,
4457 left
: itemPosition
.left
4460 // Store whether we are before or after an item to rearrange
4461 // For horizontal layout, we need to account for RTL, as this is flipped
4462 if ( this.orientation
=== 'horizontal' && this.$element
.css( 'direction' ) === 'rtl' ) {
4463 this.sideInsertion
= dragPosition
< itemMidpoint
? 'after' : 'before';
4465 this.sideInsertion
= dragPosition
< itemMidpoint
? 'before' : 'after';
4467 // Add drop indicator between objects
4468 if ( this.sideInsertion
) {
4481 // This means the item was dragged outside the widget
4491 * Set a dragged item
4492 * @param {OO.ui.DraggableElement} item Dragged item
4494 OO
.ui
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
4495 this.dragItem
= item
;
4499 * Unset the current dragged item
4501 OO
.ui
.DraggableGroupElement
.prototype.unsetDragItem = function () {
4502 this.dragItem
= null;
4503 this.itemDragOver
= null;
4504 this.$placeholder
.hide();
4505 this.sideInsertion
= '';
4509 * Get the current dragged item
4510 * @return {OO.ui.DraggableElement|null} item Dragged item or null if no item is dragged
4512 OO
.ui
.DraggableGroupElement
.prototype.getDragItem = function () {
4513 return this.dragItem
;
4517 * Check if there's an item being dragged.
4518 * @return {Boolean} Item is being dragged
4520 OO
.ui
.DraggableGroupElement
.prototype.isDragging = function () {
4521 return this.getDragItem() !== null;
4525 * Element containing an icon.
4527 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4528 * a control or convey information in a more space efficient way. Icons should rarely be used
4529 * without labels; such as in a toolbar where space is at a premium or within a context where the
4530 * meaning is very clear to the user.
4536 * @param {Object} [config] Configuration options
4537 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4538 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4539 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4541 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4543 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4544 // Configuration initialization
4545 config
= config
|| {};
4550 this.iconTitle
= null;
4553 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4554 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4555 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4560 OO
.initClass( OO
.ui
.IconElement
);
4562 /* Static Properties */
4567 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4569 * For i18n purposes, this property can be an object containing a `default` icon name property and
4570 * additional icon names keyed by language code.
4572 * Example of i18n icon definition:
4573 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4577 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4578 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4581 OO
.ui
.IconElement
.static.icon
= null;
4588 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4591 OO
.ui
.IconElement
.static.iconTitle
= null;
4596 * Set the icon element.
4598 * If an element is already set, it will be cleaned up before setting up the new element.
4600 * @param {jQuery} $icon Element to use as icon
4602 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4605 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4606 .removeAttr( 'title' );
4610 .addClass( 'oo-ui-iconElement-icon' )
4611 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4612 if ( this.iconTitle
!== null ) {
4613 this.$icon
.attr( 'title', this.iconTitle
);
4620 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4621 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4622 * language, use null to remove icon
4625 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4626 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4627 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4629 if ( this.icon
!== icon
) {
4631 if ( this.icon
!== null ) {
4632 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4634 if ( icon
!== null ) {
4635 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4641 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4642 this.updateThemeClasses();
4650 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4654 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4655 iconTitle
= typeof iconTitle
=== 'function' ||
4656 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4657 OO
.ui
.resolveMsg( iconTitle
) : null;
4659 if ( this.iconTitle
!== iconTitle
) {
4660 this.iconTitle
= iconTitle
;
4662 if ( this.iconTitle
!== null ) {
4663 this.$icon
.attr( 'title', iconTitle
);
4665 this.$icon
.removeAttr( 'title' );
4676 * @return {string} Icon name
4678 OO
.ui
.IconElement
.prototype.getIcon = function () {
4685 * @return {string} Icon title text
4687 OO
.ui
.IconElement
.prototype.getIconTitle = function () {
4688 return this.iconTitle
;
4692 * Element containing an indicator.
4694 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4695 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4696 * instead of performing an action directly, or an item in a list which has errors that need to be
4703 * @param {Object} [config] Configuration options
4704 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4706 * @cfg {string} [indicator] Symbolic indicator name
4707 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4709 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4710 // Configuration initialization
4711 config
= config
|| {};
4714 this.$indicator
= null;
4715 this.indicator
= null;
4716 this.indicatorTitle
= null;
4719 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4720 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4721 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4726 OO
.initClass( OO
.ui
.IndicatorElement
);
4728 /* Static Properties */
4735 * @property {string|null} Symbolic indicator name
4737 OO
.ui
.IndicatorElement
.static.indicator
= null;
4744 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4747 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4752 * Set the indicator element.
4754 * If an element is already set, it will be cleaned up before setting up the new element.
4756 * @param {jQuery} $indicator Element to use as indicator
4758 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4759 if ( this.$indicator
) {
4761 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4762 .removeAttr( 'title' );
4765 this.$indicator
= $indicator
4766 .addClass( 'oo-ui-indicatorElement-indicator' )
4767 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4768 if ( this.indicatorTitle
!== null ) {
4769 this.$indicator
.attr( 'title', this.indicatorTitle
);
4774 * Set indicator name.
4776 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4779 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4780 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4782 if ( this.indicator
!== indicator
) {
4783 if ( this.$indicator
) {
4784 if ( this.indicator
!== null ) {
4785 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4787 if ( indicator
!== null ) {
4788 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4791 this.indicator
= indicator
;
4794 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4795 this.updateThemeClasses();
4801 * Set indicator title.
4803 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4804 * null for no indicator title
4807 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4808 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4809 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4810 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4812 if ( this.indicatorTitle
!== indicatorTitle
) {
4813 this.indicatorTitle
= indicatorTitle
;
4814 if ( this.$indicator
) {
4815 if ( this.indicatorTitle
!== null ) {
4816 this.$indicator
.attr( 'title', indicatorTitle
);
4818 this.$indicator
.removeAttr( 'title' );
4827 * Get indicator name.
4829 * @return {string} Symbolic name of indicator
4831 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4832 return this.indicator
;
4836 * Get indicator title.
4838 * @return {string} Indicator title text
4840 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4841 return this.indicatorTitle
;
4845 * Element containing a label.
4851 * @param {Object} [config] Configuration options
4852 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4853 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4854 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4856 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4857 // Configuration initialization
4858 config
= config
|| {};
4863 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4866 this.setLabel( config
.label
|| this.constructor.static.label
);
4867 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4872 OO
.initClass( OO
.ui
.LabelElement
);
4874 /* Static Properties */
4881 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4884 OO
.ui
.LabelElement
.static.label
= null;
4889 * Set the label element.
4891 * If an element is already set, it will be cleaned up before setting up the new element.
4893 * @param {jQuery} $label Element to use as label
4895 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4896 if ( this.$label
) {
4897 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4900 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4901 this.setLabelContent( this.label
);
4907 * An empty string will result in the label being hidden. A string containing only whitespace will
4908 * be converted to a single ` `.
4910 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4911 * text; or null for no label
4914 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4915 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4916 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4918 if ( this.label
!== label
) {
4919 if ( this.$label
) {
4920 this.setLabelContent( label
);
4925 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4933 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
4934 * text; or null for no label
4936 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4945 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4946 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4947 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4954 * Set the content of the label.
4956 * Do not call this method until after the label element has been set by #setLabelElement.
4959 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4960 * text; or null for no label
4962 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4963 if ( typeof label
=== 'string' ) {
4964 if ( label
.match( /^\s*$/ ) ) {
4965 // Convert whitespace only string to a single non-breaking space
4966 this.$label
.html( ' ' );
4968 this.$label
.text( label
);
4970 } else if ( label
instanceof jQuery
) {
4971 this.$label
.empty().append( label
);
4973 this.$label
.empty();
4978 * Mixin that adds a menu showing suggested values for a OO.ui.TextInputWidget.
4980 * Subclasses that set the value of #lookupInput from #onLookupMenuItemChoose should
4981 * be aware that this will cause new suggestions to be looked up for the new value. If this is
4982 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
4988 * @param {Object} [config] Configuration options
4989 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
4990 * @cfg {jQuery} [$container=this.$element] Element to render menu under
4992 OO
.ui
.LookupElement
= function OoUiLookupElement( config
) {
4993 // Configuration initialization
4994 config
= config
|| {};
4997 this.$overlay
= config
.$overlay
|| this.$element
;
4998 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
4999 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
5000 $container
: config
.$container
5002 this.lookupCache
= {};
5003 this.lookupQuery
= null;
5004 this.lookupRequest
= null;
5005 this.lookupsDisabled
= false;
5006 this.lookupInputFocused
= false;
5010 focus
: this.onLookupInputFocus
.bind( this ),
5011 blur
: this.onLookupInputBlur
.bind( this ),
5012 mousedown
: this.onLookupInputMouseDown
.bind( this )
5014 this.connect( this, { change
: 'onLookupInputChange' } );
5015 this.lookupMenu
.connect( this, {
5016 toggle
: 'onLookupMenuToggle',
5017 choose
: 'onLookupMenuItemChoose'
5021 this.$element
.addClass( 'oo-ui-lookupElement' );
5022 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
5023 this.$overlay
.append( this.lookupMenu
.$element
);
5029 * Handle input focus event.
5031 * @param {jQuery.Event} e Input focus event
5033 OO
.ui
.LookupElement
.prototype.onLookupInputFocus = function () {
5034 this.lookupInputFocused
= true;
5035 this.populateLookupMenu();
5039 * Handle input blur event.
5041 * @param {jQuery.Event} e Input blur event
5043 OO
.ui
.LookupElement
.prototype.onLookupInputBlur = function () {
5044 this.closeLookupMenu();
5045 this.lookupInputFocused
= false;
5049 * Handle input mouse down event.
5051 * @param {jQuery.Event} e Input mouse down event
5053 OO
.ui
.LookupElement
.prototype.onLookupInputMouseDown = function () {
5054 // Only open the menu if the input was already focused.
5055 // This way we allow the user to open the menu again after closing it with Esc
5056 // by clicking in the input. Opening (and populating) the menu when initially
5057 // clicking into the input is handled by the focus handler.
5058 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
5059 this.populateLookupMenu();
5064 * Handle input change event.
5066 * @param {string} value New input value
5068 OO
.ui
.LookupElement
.prototype.onLookupInputChange = function () {
5069 if ( this.lookupInputFocused
) {
5070 this.populateLookupMenu();
5075 * Handle the lookup menu being shown/hidden.
5077 * @param {boolean} visible Whether the lookup menu is now visible.
5079 OO
.ui
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
5081 // When the menu is hidden, abort any active request and clear the menu.
5082 // This has to be done here in addition to closeLookupMenu(), because
5083 // MenuSelectWidget will close itself when the user presses Esc.
5084 this.abortLookupRequest();
5085 this.lookupMenu
.clearItems();
5090 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
5092 * @param {OO.ui.MenuOptionWidget|null} item Selected item
5094 OO
.ui
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
5096 this.setValue( item
.getData() );
5103 * @return {OO.ui.TextInputMenuSelectWidget}
5105 OO
.ui
.LookupElement
.prototype.getLookupMenu = function () {
5106 return this.lookupMenu
;
5110 * Disable or re-enable lookups.
5112 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
5114 * @param {boolean} disabled Disable lookups
5116 OO
.ui
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
5117 this.lookupsDisabled
= !!disabled
;
5121 * Open the menu. If there are no entries in the menu, this does nothing.
5125 OO
.ui
.LookupElement
.prototype.openLookupMenu = function () {
5126 if ( !this.lookupMenu
.isEmpty() ) {
5127 this.lookupMenu
.toggle( true );
5133 * Close the menu, empty it, and abort any pending request.
5137 OO
.ui
.LookupElement
.prototype.closeLookupMenu = function () {
5138 this.lookupMenu
.toggle( false );
5139 this.abortLookupRequest();
5140 this.lookupMenu
.clearItems();
5145 * Request menu items based on the input's current value, and when they arrive,
5146 * populate the menu with these items and show the menu.
5148 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
5152 OO
.ui
.LookupElement
.prototype.populateLookupMenu = function () {
5154 value
= this.getValue();
5156 if ( this.lookupsDisabled
) {
5160 // If the input is empty, clear the menu
5161 if ( value
=== '' ) {
5162 this.closeLookupMenu();
5163 // Skip population if there is already a request pending for the current value
5164 } else if ( value
!== this.lookupQuery
) {
5165 this.getLookupMenuItems()
5166 .done( function ( items
) {
5167 widget
.lookupMenu
.clearItems();
5168 if ( items
.length
) {
5172 widget
.initializeLookupMenuSelection();
5174 widget
.lookupMenu
.toggle( false );
5177 .fail( function () {
5178 widget
.lookupMenu
.clearItems();
5186 * Select and highlight the first selectable item in the menu.
5190 OO
.ui
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
5191 if ( !this.lookupMenu
.getSelectedItem() ) {
5192 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
5194 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
5198 * Get lookup menu items for the current query.
5200 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
5201 * the done event. If the request was aborted to make way for a subsequent request, this promise
5202 * will not be rejected: it will remain pending forever.
5204 OO
.ui
.LookupElement
.prototype.getLookupMenuItems = function () {
5206 value
= this.getValue(),
5207 deferred
= $.Deferred(),
5210 this.abortLookupRequest();
5211 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
5212 deferred
.resolve( this.getLookupMenuOptionsFromData( this.lookupCache
[value
] ) );
5215 this.lookupQuery
= value
;
5216 ourRequest
= this.lookupRequest
= this.getLookupRequest();
5218 .always( function () {
5219 // We need to pop pending even if this is an old request, otherwise
5220 // the widget will remain pending forever.
5221 // TODO: this assumes that an aborted request will fail or succeed soon after
5222 // being aborted, or at least eventually. It would be nice if we could popPending()
5223 // at abort time, but only if we knew that we hadn't already called popPending()
5224 // for that request.
5225 widget
.popPending();
5227 .done( function ( data
) {
5228 // If this is an old request (and aborting it somehow caused it to still succeed),
5229 // ignore its success completely
5230 if ( ourRequest
=== widget
.lookupRequest
) {
5231 widget
.lookupQuery
= null;
5232 widget
.lookupRequest
= null;
5233 widget
.lookupCache
[value
] = widget
.getLookupCacheDataFromResponse( data
);
5234 deferred
.resolve( widget
.getLookupMenuOptionsFromData( widget
.lookupCache
[value
] ) );
5237 .fail( function () {
5238 // If this is an old request (or a request failing because it's being aborted),
5239 // ignore its failure completely
5240 if ( ourRequest
=== widget
.lookupRequest
) {
5241 widget
.lookupQuery
= null;
5242 widget
.lookupRequest
= null;
5247 return deferred
.promise();
5251 * Abort the currently pending lookup request, if any.
5253 OO
.ui
.LookupElement
.prototype.abortLookupRequest = function () {
5254 var oldRequest
= this.lookupRequest
;
5256 // First unset this.lookupRequest to the fail handler will notice
5257 // that the request is no longer current
5258 this.lookupRequest
= null;
5259 this.lookupQuery
= null;
5265 * Get a new request object of the current lookup query value.
5268 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
5270 OO
.ui
.LookupElement
.prototype.getLookupRequest = function () {
5271 // Stub, implemented in subclass
5276 * Pre-process data returned by the request from #getLookupRequest.
5278 * The return value of this function will be cached, and any further queries for the given value
5279 * will use the cache rather than doing API requests.
5282 * @param {Mixed} data Response from server
5283 * @return {Mixed} Cached result data
5285 OO
.ui
.LookupElement
.prototype.getLookupCacheDataFromResponse = function () {
5286 // Stub, implemented in subclass
5291 * Get a list of menu option widgets from the (possibly cached) data returned by
5292 * #getLookupCacheDataFromResponse.
5295 * @param {Mixed} data Cached result data, usually an array
5296 * @return {OO.ui.MenuOptionWidget[]} Menu items
5298 OO
.ui
.LookupElement
.prototype.getLookupMenuOptionsFromData = function () {
5299 // Stub, implemented in subclass
5304 * Element containing an OO.ui.PopupWidget object.
5310 * @param {Object} [config] Configuration options
5311 * @cfg {Object} [popup] Configuration to pass to popup
5312 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
5314 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
5315 // Configuration initialization
5316 config
= config
|| {};
5319 this.popup
= new OO
.ui
.PopupWidget( $.extend(
5320 { autoClose
: true },
5322 { $: this.$, $autoCloseIgnore
: this.$element
}
5331 * @return {OO.ui.PopupWidget} Popup widget
5333 OO
.ui
.PopupElement
.prototype.getPopup = function () {
5338 * Element with named flags that can be added, removed, listed and checked.
5340 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
5341 * the flag name. Flags are primarily useful for styling.
5347 * @param {Object} [config] Configuration options
5348 * @cfg {string|string[]} [flags] Flags describing importance and functionality, e.g. 'primary',
5349 * 'safe', 'progressive', 'destructive' or 'constructive'
5350 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
5352 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
5353 // Configuration initialization
5354 config
= config
|| {};
5358 this.$flagged
= null;
5361 this.setFlags( config
.flags
);
5362 this.setFlaggedElement( config
.$flagged
|| this.$element
);
5369 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
5370 * added/removed properties
5376 * Set the flagged element.
5378 * If an element is already set, it will be cleaned up before setting up the new element.
5380 * @param {jQuery} $flagged Element to add flags to
5382 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
5383 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
5384 return 'oo-ui-flaggedElement-' + flag
;
5387 if ( this.$flagged
) {
5388 this.$flagged
.removeClass( classNames
);
5391 this.$flagged
= $flagged
.addClass( classNames
);
5395 * Check if a flag is set.
5397 * @param {string} flag Name of flag
5398 * @return {boolean} Has flag
5400 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
5401 return flag
in this.flags
;
5405 * Get the names of all flags set.
5407 * @return {string[]} Flag names
5409 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
5410 return Object
.keys( this.flags
);
5419 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
5420 var flag
, className
,
5423 classPrefix
= 'oo-ui-flaggedElement-';
5425 for ( flag
in this.flags
) {
5426 className
= classPrefix
+ flag
;
5427 changes
[flag
] = false;
5428 delete this.flags
[flag
];
5429 remove
.push( className
);
5432 if ( this.$flagged
) {
5433 this.$flagged
.removeClass( remove
.join( ' ' ) );
5436 this.updateThemeClasses();
5437 this.emit( 'flag', changes
);
5443 * Add one or more flags.
5445 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
5446 * keyed by flag name containing boolean set/remove instructions.
5450 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
5451 var i
, len
, flag
, className
,
5455 classPrefix
= 'oo-ui-flaggedElement-';
5457 if ( typeof flags
=== 'string' ) {
5458 className
= classPrefix
+ flags
;
5460 if ( !this.flags
[flags
] ) {
5461 this.flags
[flags
] = true;
5462 add
.push( className
);
5464 } else if ( $.isArray( flags
) ) {
5465 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
5467 className
= classPrefix
+ flag
;
5469 if ( !this.flags
[flag
] ) {
5470 changes
[flag
] = true;
5471 this.flags
[flag
] = true;
5472 add
.push( className
);
5475 } else if ( OO
.isPlainObject( flags
) ) {
5476 for ( flag
in flags
) {
5477 className
= classPrefix
+ flag
;
5478 if ( flags
[flag
] ) {
5480 if ( !this.flags
[flag
] ) {
5481 changes
[flag
] = true;
5482 this.flags
[flag
] = true;
5483 add
.push( className
);
5487 if ( this.flags
[flag
] ) {
5488 changes
[flag
] = false;
5489 delete this.flags
[flag
];
5490 remove
.push( className
);
5496 if ( this.$flagged
) {
5498 .addClass( add
.join( ' ' ) )
5499 .removeClass( remove
.join( ' ' ) );
5502 this.updateThemeClasses();
5503 this.emit( 'flag', changes
);
5509 * Element with a title.
5511 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
5512 * not visible on touch devices.
5518 * @param {Object} [config] Configuration options
5519 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
5520 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
5521 * static property 'title' is used.
5523 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
5524 // Configuration initialization
5525 config
= config
|| {};
5528 this.$titled
= null;
5532 this.setTitle( config
.title
|| this.constructor.static.title
);
5533 this.setTitledElement( config
.$titled
|| this.$element
);
5538 OO
.initClass( OO
.ui
.TitledElement
);
5540 /* Static Properties */
5547 * @property {string|Function} Title text or a function that returns text
5549 OO
.ui
.TitledElement
.static.title
= null;
5554 * Set the titled element.
5556 * If an element is already set, it will be cleaned up before setting up the new element.
5558 * @param {jQuery} $titled Element to set title on
5560 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
5561 if ( this.$titled
) {
5562 this.$titled
.removeAttr( 'title' );
5565 this.$titled
= $titled
;
5567 this.$titled
.attr( 'title', this.title
);
5574 * @param {string|Function|null} title Title text, a function that returns text or null for no title
5577 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
5578 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
5580 if ( this.title
!== title
) {
5581 if ( this.$titled
) {
5582 if ( title
!== null ) {
5583 this.$titled
.attr( 'title', title
);
5585 this.$titled
.removeAttr( 'title' );
5597 * @return {string} Title string
5599 OO
.ui
.TitledElement
.prototype.getTitle = function () {
5604 * Element that can be automatically clipped to visible boundaries.
5606 * Whenever the element's natural height changes, you have to call
5607 * #clip to make sure it's still clipping correctly.
5613 * @param {Object} [config] Configuration options
5614 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
5616 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
5617 // Configuration initialization
5618 config
= config
|| {};
5621 this.$clippable
= null;
5622 this.clipping
= false;
5623 this.clippedHorizontally
= false;
5624 this.clippedVertically
= false;
5625 this.$clippableContainer
= null;
5626 this.$clippableScroller
= null;
5627 this.$clippableWindow
= null;
5628 this.idealWidth
= null;
5629 this.idealHeight
= null;
5630 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
5631 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
5634 this.setClippableElement( config
.$clippable
|| this.$element
);
5640 * Set clippable element.
5642 * If an element is already set, it will be cleaned up before setting up the new element.
5644 * @param {jQuery} $clippable Element to make clippable
5646 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
5647 if ( this.$clippable
) {
5648 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
5649 this.$clippable
.css( { width
: '', height
: '' } );
5650 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5651 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
5654 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
5661 * Do not turn clipping on until after the element is attached to the DOM and visible.
5663 * @param {boolean} [clipping] Enable clipping, omit to toggle
5666 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
5667 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
5669 if ( this.clipping
!== clipping
) {
5670 this.clipping
= clipping
;
5672 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
5673 // If the clippable container is the root, we have to listen to scroll events and check
5674 // jQuery.scrollTop on the window because of browser inconsistencies
5675 this.$clippableScroller
= this.$clippableContainer
.is( 'html, body' ) ?
5676 this.$( OO
.ui
.Element
.static.getWindow( this.$clippableContainer
) ) :
5677 this.$clippableContainer
;
5678 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
5679 this.$clippableWindow
= this.$( this.getElementWindow() )
5680 .on( 'resize', this.onClippableWindowResizeHandler
);
5681 // Initial clip after visible
5684 this.$clippable
.css( { width
: '', height
: '' } );
5685 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5686 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
5688 this.$clippableContainer
= null;
5689 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
5690 this.$clippableScroller
= null;
5691 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
5692 this.$clippableWindow
= null;
5700 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
5702 * @return {boolean} Element will be clipped to the visible area
5704 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
5705 return this.clipping
;
5709 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
5711 * @return {boolean} Part of the element is being clipped
5713 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
5714 return this.clippedHorizontally
|| this.clippedVertically
;
5718 * Check if the right of the element is being clipped by the nearest scrollable container.
5720 * @return {boolean} Part of the element is being clipped
5722 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
5723 return this.clippedHorizontally
;
5727 * Check if the bottom of the element is being clipped by the nearest scrollable container.
5729 * @return {boolean} Part of the element is being clipped
5731 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
5732 return this.clippedVertically
;
5736 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
5738 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
5739 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
5741 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
5742 this.idealWidth
= width
;
5743 this.idealHeight
= height
;
5745 if ( !this.clipping
) {
5746 // Update dimensions
5747 this.$clippable
.css( { width
: width
, height
: height
} );
5749 // While clipping, idealWidth and idealHeight are not considered
5753 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
5754 * the element's natural height changes.
5756 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
5757 * overlapped by, the visible area of the nearest scrollable container.
5761 OO
.ui
.ClippableElement
.prototype.clip = function () {
5762 if ( !this.clipping
) {
5763 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
5767 var buffer
= 7, // Chosen by fair dice roll
5768 cOffset
= this.$clippable
.offset(),
5769 $container
= this.$clippableContainer
.is( 'html, body' ) ?
5770 this.$clippableWindow
: this.$clippableContainer
,
5771 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
5772 ccHeight
= $container
.innerHeight() - buffer
,
5773 ccWidth
= $container
.innerWidth() - buffer
,
5774 cHeight
= this.$clippable
.outerHeight() + buffer
,
5775 cWidth
= this.$clippable
.outerWidth() + buffer
,
5776 scrollTop
= this.$clippableScroller
.scrollTop(),
5777 scrollLeft
= this.$clippableScroller
.scrollLeft(),
5778 desiredWidth
= cOffset
.left
< 0 ?
5779 cWidth
+ cOffset
.left
:
5780 ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
5781 desiredHeight
= cOffset
.top
< 0 ?
5782 cHeight
+ cOffset
.top
:
5783 ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
5784 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
5785 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
5786 clipWidth
= desiredWidth
< naturalWidth
,
5787 clipHeight
= desiredHeight
< naturalHeight
;
5790 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
5792 this.$clippable
.css( 'width', this.idealWidth
|| '' );
5793 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5794 this.$clippable
.css( 'overflowX', '' );
5797 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
5799 this.$clippable
.css( 'height', this.idealHeight
|| '' );
5800 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5801 this.$clippable
.css( 'overflowY', '' );
5804 this.clippedHorizontally
= clipWidth
;
5805 this.clippedVertically
= clipHeight
;
5811 * Generic toolbar tool.
5815 * @extends OO.ui.Widget
5816 * @mixins OO.ui.IconElement
5817 * @mixins OO.ui.FlaggedElement
5820 * @param {OO.ui.ToolGroup} toolGroup
5821 * @param {Object} [config] Configuration options
5822 * @cfg {string|Function} [title] Title text or a function that returns text
5824 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
5825 // Configuration initialization
5826 config
= config
|| {};
5828 // Parent constructor
5829 OO
.ui
.Tool
.super.call( this, config
);
5831 // Mixin constructors
5832 OO
.ui
.IconElement
.call( this, config
);
5833 OO
.ui
.FlaggedElement
.call( this, config
);
5836 this.toolGroup
= toolGroup
;
5837 this.toolbar
= this.toolGroup
.getToolbar();
5838 this.active
= false;
5839 this.$title
= this.$( '<span>' );
5840 this.$accel
= this.$( '<span>' );
5841 this.$link
= this.$( '<a>' );
5845 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
5848 this.$title
.addClass( 'oo-ui-tool-title' );
5850 .addClass( 'oo-ui-tool-accel' )
5852 // This may need to be changed if the key names are ever localized,
5853 // but for now they are essentially written in English
5858 .addClass( 'oo-ui-tool-link' )
5859 .append( this.$icon
, this.$title
, this.$accel
)
5860 .prop( 'tabIndex', 0 )
5861 .attr( 'role', 'button' );
5863 .data( 'oo-ui-tool', this )
5865 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5866 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5868 .append( this.$link
);
5869 this.setTitle( config
.title
|| this.constructor.static.title
);
5874 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5875 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5876 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5884 /* Static Properties */
5890 OO
.ui
.Tool
.static.tagName
= 'span';
5893 * Symbolic name of tool.
5898 * @property {string}
5900 OO
.ui
.Tool
.static.name
= '';
5908 * @property {string}
5910 OO
.ui
.Tool
.static.group
= '';
5915 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
5916 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
5917 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
5918 * appended to the title if the tool is part of a bar tool group.
5923 * @property {string|Function} Title text or a function that returns text
5925 OO
.ui
.Tool
.static.title
= '';
5928 * Tool can be automatically added to catch-all groups.
5932 * @property {boolean}
5934 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
5937 * Tool can be automatically added to named groups.
5940 * @property {boolean}
5943 OO
.ui
.Tool
.static.autoAddToGroup
= true;
5946 * Check if this tool is compatible with given data.
5950 * @param {Mixed} data Data to check
5951 * @return {boolean} Tool can be used with data
5953 OO
.ui
.Tool
.static.isCompatibleWith = function () {
5960 * Handle the toolbar state being updated.
5962 * This is an abstract method that must be overridden in a concrete subclass.
5966 OO
.ui
.Tool
.prototype.onUpdateState = function () {
5968 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
5973 * Handle the tool being selected.
5975 * This is an abstract method that must be overridden in a concrete subclass.
5979 OO
.ui
.Tool
.prototype.onSelect = function () {
5981 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
5986 * Check if the button is active.
5988 * @return {boolean} Button is active
5990 OO
.ui
.Tool
.prototype.isActive = function () {
5995 * Make the button appear active or inactive.
5997 * @param {boolean} state Make button appear active
5999 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
6000 this.active
= !!state
;
6001 if ( this.active
) {
6002 this.$element
.addClass( 'oo-ui-tool-active' );
6004 this.$element
.removeClass( 'oo-ui-tool-active' );
6009 * Get the tool title.
6011 * @param {string|Function} title Title text or a function that returns text
6014 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
6015 this.title
= OO
.ui
.resolveMsg( title
);
6021 * Get the tool title.
6023 * @return {string} Title text
6025 OO
.ui
.Tool
.prototype.getTitle = function () {
6030 * Get the tool's symbolic name.
6032 * @return {string} Symbolic name of tool
6034 OO
.ui
.Tool
.prototype.getName = function () {
6035 return this.constructor.static.name
;
6041 OO
.ui
.Tool
.prototype.updateTitle = function () {
6042 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
6043 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
6044 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
6047 this.$title
.text( this.title
);
6048 this.$accel
.text( accel
);
6050 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
6051 tooltipParts
.push( this.title
);
6053 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
6054 tooltipParts
.push( accel
);
6056 if ( tooltipParts
.length
) {
6057 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
6059 this.$link
.removeAttr( 'title' );
6066 OO
.ui
.Tool
.prototype.destroy = function () {
6067 this.toolbar
.disconnect( this );
6068 this.$element
.remove();
6072 * Collection of tool groups.
6075 * @extends OO.ui.Element
6076 * @mixins OO.EventEmitter
6077 * @mixins OO.ui.GroupElement
6080 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
6081 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
6082 * @param {Object} [config] Configuration options
6083 * @cfg {boolean} [actions] Add an actions section opposite to the tools
6084 * @cfg {boolean} [shadow] Add a shadow below the toolbar
6086 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
6087 // Configuration initialization
6088 config
= config
|| {};
6090 // Parent constructor
6091 OO
.ui
.Toolbar
.super.call( this, config
);
6093 // Mixin constructors
6094 OO
.EventEmitter
.call( this );
6095 OO
.ui
.GroupElement
.call( this, config
);
6098 this.toolFactory
= toolFactory
;
6099 this.toolGroupFactory
= toolGroupFactory
;
6102 this.$bar
= this.$( '<div>' );
6103 this.$actions
= this.$( '<div>' );
6104 this.initialized
= false;
6108 .add( this.$bar
).add( this.$group
).add( this.$actions
)
6109 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
6112 this.$group
.addClass( 'oo-ui-toolbar-tools' );
6113 if ( config
.actions
) {
6114 this.$bar
.append( this.$actions
.addClass( 'oo-ui-toolbar-actions' ) );
6117 .addClass( 'oo-ui-toolbar-bar' )
6118 .append( this.$group
, '<div style="clear:both"></div>' );
6119 if ( config
.shadow
) {
6120 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
6122 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
6127 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
6128 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
6129 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
6134 * Get the tool factory.
6136 * @return {OO.ui.ToolFactory} Tool factory
6138 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
6139 return this.toolFactory
;
6143 * Get the tool group factory.
6145 * @return {OO.Factory} Tool group factory
6147 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
6148 return this.toolGroupFactory
;
6152 * Handles mouse down events.
6154 * @param {jQuery.Event} e Mouse down event
6156 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
6157 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
6158 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
6159 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
6165 * Sets up handles and preloads required information for the toolbar to work.
6166 * This must be called after it is attached to a visible document and before doing anything else.
6168 OO
.ui
.Toolbar
.prototype.initialize = function () {
6169 this.initialized
= true;
6175 * Tools can be specified in the following ways:
6177 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
6178 * - All tools in a group: `{ group: 'group-name' }`
6179 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
6181 * @param {Object.<string,Array>} groups List of tool group configurations
6182 * @param {Array|string} [groups.include] Tools to include
6183 * @param {Array|string} [groups.exclude] Tools to exclude
6184 * @param {Array|string} [groups.promote] Tools to promote to the beginning
6185 * @param {Array|string} [groups.demote] Tools to demote to the end
6187 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
6188 var i
, len
, type
, group
,
6190 defaultType
= 'bar';
6192 // Cleanup previous groups
6195 // Build out new groups
6196 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
6198 if ( group
.include
=== '*' ) {
6199 // Apply defaults to catch-all groups
6200 if ( group
.type
=== undefined ) {
6201 group
.type
= 'list';
6203 if ( group
.label
=== undefined ) {
6204 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
6207 // Check type has been registered
6208 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
6210 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
6213 this.addItems( items
);
6217 * Remove all tools and groups from the toolbar.
6219 OO
.ui
.Toolbar
.prototype.reset = function () {
6224 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6225 this.items
[i
].destroy();
6231 * Destroys toolbar, removing event handlers and DOM elements.
6233 * Call this whenever you are done using a toolbar.
6235 OO
.ui
.Toolbar
.prototype.destroy = function () {
6237 this.$element
.remove();
6241 * Check if tool has not been used yet.
6243 * @param {string} name Symbolic name of tool
6244 * @return {boolean} Tool is available
6246 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
6247 return !this.tools
[name
];
6251 * Prevent tool from being used again.
6253 * @param {OO.ui.Tool} tool Tool to reserve
6255 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
6256 this.tools
[tool
.getName()] = tool
;
6260 * Allow tool to be used again.
6262 * @param {OO.ui.Tool} tool Tool to release
6264 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
6265 delete this.tools
[tool
.getName()];
6269 * Get accelerator label for tool.
6271 * This is a stub that should be overridden to provide access to accelerator information.
6273 * @param {string} name Symbolic name of tool
6274 * @return {string|undefined} Tool accelerator label if available
6276 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
6281 * Collection of tools.
6283 * Tools can be specified in the following ways:
6285 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
6286 * - All tools in a group: `{ group: 'group-name' }`
6287 * - All tools: `'*'`
6291 * @extends OO.ui.Widget
6292 * @mixins OO.ui.GroupElement
6295 * @param {OO.ui.Toolbar} toolbar
6296 * @param {Object} [config] Configuration options
6297 * @cfg {Array|string} [include=[]] List of tools to include
6298 * @cfg {Array|string} [exclude=[]] List of tools to exclude
6299 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
6300 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
6302 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
6303 // Configuration initialization
6304 config
= config
|| {};
6306 // Parent constructor
6307 OO
.ui
.ToolGroup
.super.call( this, config
);
6309 // Mixin constructors
6310 OO
.ui
.GroupElement
.call( this, config
);
6313 this.toolbar
= toolbar
;
6315 this.pressed
= null;
6316 this.autoDisabled
= false;
6317 this.include
= config
.include
|| [];
6318 this.exclude
= config
.exclude
|| [];
6319 this.promote
= config
.promote
|| [];
6320 this.demote
= config
.demote
|| [];
6321 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
6325 'mousedown touchstart': this.onPointerDown
.bind( this ),
6326 'mouseup touchend': this.onPointerUp
.bind( this ),
6327 mouseover
: this.onMouseOver
.bind( this ),
6328 mouseout
: this.onMouseOut
.bind( this )
6330 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
6331 this.aggregate( { disable
: 'itemDisable' } );
6332 this.connect( this, { itemDisable
: 'updateDisabled' } );
6335 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
6337 .addClass( 'oo-ui-toolGroup' )
6338 .append( this.$group
);
6344 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
6345 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
6353 /* Static Properties */
6356 * Show labels in tooltips.
6360 * @property {boolean}
6362 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
6365 * Show acceleration labels in tooltips.
6369 * @property {boolean}
6371 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
6374 * Automatically disable the toolgroup when all tools are disabled
6378 * @property {boolean}
6380 OO
.ui
.ToolGroup
.static.autoDisable
= true;
6387 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
6388 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
6394 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
6395 var i
, item
, allDisabled
= true;
6397 if ( this.constructor.static.autoDisable
) {
6398 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
6399 item
= this.items
[i
];
6400 if ( !item
.isDisabled() ) {
6401 allDisabled
= false;
6405 this.autoDisabled
= allDisabled
;
6407 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
6411 * Handle mouse down events.
6413 * @param {jQuery.Event} e Mouse down event
6415 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
6416 // e.which is 0 for touch events, 1 for left mouse button
6417 if ( !this.isDisabled() && e
.which
<= 1 ) {
6418 this.pressed
= this.getTargetTool( e
);
6419 if ( this.pressed
) {
6420 this.pressed
.setActive( true );
6421 this.getElementDocument().addEventListener(
6422 'mouseup', this.onCapturedMouseUpHandler
, true
6430 * Handle captured mouse up events.
6432 * @param {Event} e Mouse up event
6434 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
6435 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
6436 // onPointerUp may be called a second time, depending on where the mouse is when the button is
6437 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
6438 this.onPointerUp( e
);
6442 * Handle mouse up events.
6444 * @param {jQuery.Event} e Mouse up event
6446 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
6447 var tool
= this.getTargetTool( e
);
6449 // e.which is 0 for touch events, 1 for left mouse button
6450 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
6451 this.pressed
.onSelect();
6454 this.pressed
= null;
6459 * Handle mouse over events.
6461 * @param {jQuery.Event} e Mouse over event
6463 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
6464 var tool
= this.getTargetTool( e
);
6466 if ( this.pressed
&& this.pressed
=== tool
) {
6467 this.pressed
.setActive( true );
6472 * Handle mouse out events.
6474 * @param {jQuery.Event} e Mouse out event
6476 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
6477 var tool
= this.getTargetTool( e
);
6479 if ( this.pressed
&& this.pressed
=== tool
) {
6480 this.pressed
.setActive( false );
6485 * Get the closest tool to a jQuery.Event.
6487 * Only tool links are considered, which prevents other elements in the tool such as popups from
6488 * triggering tool group interactions.
6491 * @param {jQuery.Event} e
6492 * @return {OO.ui.Tool|null} Tool, `null` if none was found
6494 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
6496 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
6498 if ( $item
.length
) {
6499 tool
= $item
.parent().data( 'oo-ui-tool' );
6502 return tool
&& !tool
.isDisabled() ? tool
: null;
6506 * Handle tool registry register events.
6508 * If a tool is registered after the group is created, we must repopulate the list to account for:
6510 * - a tool being added that may be included
6511 * - a tool already included being overridden
6513 * @param {string} name Symbolic name of tool
6515 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
6520 * Get the toolbar this group is in.
6522 * @return {OO.ui.Toolbar} Toolbar of group
6524 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
6525 return this.toolbar
;
6529 * Add and remove tools based on configuration.
6531 OO
.ui
.ToolGroup
.prototype.populate = function () {
6532 var i
, len
, name
, tool
,
6533 toolFactory
= this.toolbar
.getToolFactory(),
6537 list
= this.toolbar
.getToolFactory().getTools(
6538 this.include
, this.exclude
, this.promote
, this.demote
6541 // Build a list of needed tools
6542 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
6546 toolFactory
.lookup( name
) &&
6547 // Tool is available or is already in this group
6548 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
6550 tool
= this.tools
[name
];
6552 // Auto-initialize tools on first use
6553 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
6556 this.toolbar
.reserveTool( tool
);
6561 // Remove tools that are no longer needed
6562 for ( name
in this.tools
) {
6563 if ( !names
[name
] ) {
6564 this.tools
[name
].destroy();
6565 this.toolbar
.releaseTool( this.tools
[name
] );
6566 remove
.push( this.tools
[name
] );
6567 delete this.tools
[name
];
6570 if ( remove
.length
) {
6571 this.removeItems( remove
);
6573 // Update emptiness state
6575 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
6577 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
6579 // Re-add tools (moving existing ones to new locations)
6580 this.addItems( add
);
6581 // Disabled state may depend on items
6582 this.updateDisabled();
6586 * Destroy tool group.
6588 OO
.ui
.ToolGroup
.prototype.destroy = function () {
6592 this.toolbar
.getToolFactory().disconnect( this );
6593 for ( name
in this.tools
) {
6594 this.toolbar
.releaseTool( this.tools
[name
] );
6595 this.tools
[name
].disconnect( this ).destroy();
6596 delete this.tools
[name
];
6598 this.$element
.remove();
6602 * Dialog for showing a message.
6605 * - Registers two actions by default (safe and primary).
6606 * - Renders action widgets in the footer.
6609 * @extends OO.ui.Dialog
6612 * @param {Object} [config] Configuration options
6614 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
6615 // Parent constructor
6616 OO
.ui
.MessageDialog
.super.call( this, config
);
6619 this.verticalActionLayout
= null;
6622 this.$element
.addClass( 'oo-ui-messageDialog' );
6627 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
6629 /* Static Properties */
6631 OO
.ui
.MessageDialog
.static.name
= 'message';
6633 OO
.ui
.MessageDialog
.static.size
= 'small';
6635 OO
.ui
.MessageDialog
.static.verbose
= false;
6640 * A confirmation dialog's title should describe what the progressive action will do. An alert
6641 * dialog's title should describe what event occurred.
6645 * @property {jQuery|string|Function|null}
6647 OO
.ui
.MessageDialog
.static.title
= null;
6650 * A confirmation dialog's message should describe the consequences of the progressive action. An
6651 * alert dialog's message should describe why the event occurred.
6655 * @property {jQuery|string|Function|null}
6657 OO
.ui
.MessageDialog
.static.message
= null;
6659 OO
.ui
.MessageDialog
.static.actions
= [
6660 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
6661 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
6669 OO
.ui
.MessageDialog
.prototype.setManager = function ( manager
) {
6670 OO
.ui
.MessageDialog
.super.prototype.setManager
.call( this, manager
);
6673 this.manager
.connect( this, {
6683 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
6685 return OO
.ui
.MessageDialog
.super.prototype.onActionResize
.call( this, action
);
6689 * Handle window resized events.
6691 OO
.ui
.MessageDialog
.prototype.onResize = function () {
6693 dialog
.fitActions();
6694 // Wait for CSS transition to finish and do it again :(
6695 setTimeout( function () {
6696 dialog
.fitActions();
6701 * Toggle action layout between vertical and horizontal.
6703 * @param {boolean} [value] Layout actions vertically, omit to toggle
6706 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
6707 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
6709 if ( value
!== this.verticalActionLayout
) {
6710 this.verticalActionLayout
= value
;
6712 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
6713 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
6722 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
6724 return new OO
.ui
.Process( function () {
6725 this.close( { action
: action
} );
6728 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
6734 * @param {Object} [data] Dialog opening data
6735 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
6736 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
6737 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
6738 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
6741 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
6745 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
6746 .next( function () {
6747 this.title
.setLabel(
6748 data
.title
!== undefined ? data
.title
: this.constructor.static.title
6750 this.message
.setLabel(
6751 data
.message
!== undefined ? data
.message
: this.constructor.static.message
6753 this.message
.$element
.toggleClass(
6754 'oo-ui-messageDialog-message-verbose',
6755 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
6763 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
6764 var bodyHeight
, oldOverflow
,
6765 $scrollable
= this.container
.$element
;
6767 oldOverflow
= $scrollable
[0].style
.overflow
;
6768 $scrollable
[0].style
.overflow
= 'hidden';
6770 // Force… ugh… something to happen
6771 $scrollable
.contents().hide();
6772 $scrollable
.height();
6773 $scrollable
.contents().show();
6775 bodyHeight
= this.text
.$element
.outerHeight( true );
6776 $scrollable
[0].style
.overflow
= oldOverflow
;
6784 OO
.ui
.MessageDialog
.prototype.setDimensions = function ( dim
) {
6785 var $scrollable
= this.container
.$element
;
6786 OO
.ui
.MessageDialog
.super.prototype.setDimensions
.call( this, dim
);
6788 // Twiddle the overflow property, otherwise an unnecessary scrollbar will be produced.
6789 // Need to do it after transition completes (250ms), add 50ms just in case.
6790 setTimeout( function () {
6791 var oldOverflow
= $scrollable
[0].style
.overflow
;
6792 $scrollable
[0].style
.overflow
= 'hidden';
6794 // Force… ugh… something to happen
6795 $scrollable
.contents().hide();
6796 $scrollable
.height();
6797 $scrollable
.contents().show();
6799 $scrollable
[0].style
.overflow
= oldOverflow
;
6808 OO
.ui
.MessageDialog
.prototype.initialize = function () {
6810 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
6813 this.$actions
= this.$( '<div>' );
6814 this.container
= new OO
.ui
.PanelLayout( {
6815 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
6817 this.text
= new OO
.ui
.PanelLayout( {
6818 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
6820 this.message
= new OO
.ui
.LabelWidget( {
6821 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
6825 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
6826 this.$content
.addClass( 'oo-ui-messageDialog-content' );
6827 this.container
.$element
.append( this.text
.$element
);
6828 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
6829 this.$body
.append( this.container
.$element
);
6830 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
6831 this.$foot
.append( this.$actions
);
6837 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
6838 var i
, len
, other
, special
, others
;
6841 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
6843 special
= this.actions
.getSpecial();
6844 others
= this.actions
.getOthers();
6845 if ( special
.safe
) {
6846 this.$actions
.append( special
.safe
.$element
);
6847 special
.safe
.toggleFramed( false );
6849 if ( others
.length
) {
6850 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6852 this.$actions
.append( other
.$element
);
6853 other
.toggleFramed( false );
6856 if ( special
.primary
) {
6857 this.$actions
.append( special
.primary
.$element
);
6858 special
.primary
.toggleFramed( false );
6861 if ( !this.isOpening() ) {
6862 // If the dialog is currently opening, this will be called automatically soon.
6863 // This also calls #fitActions.
6864 this.manager
.updateWindowSize( this );
6869 * Fit action actions into columns or rows.
6871 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
6873 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
6875 previous
= this.verticalActionLayout
,
6876 actions
= this.actions
.get();
6879 this.toggleVerticalActionLayout( false );
6880 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
6881 action
= actions
[i
];
6882 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
6883 this.toggleVerticalActionLayout( true );
6888 if ( this.verticalActionLayout
!== previous
) {
6889 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6890 // We changed the layout, window height might need to be updated.
6891 this.manager
.updateWindowSize( this );
6896 * Navigation dialog window.
6899 * - Show and hide errors.
6900 * - Retry an action.
6903 * - Renders header with dialog title and one action widget on either side
6904 * (a 'safe' button on the left, and a 'primary' button on the right, both of
6905 * which close the dialog).
6906 * - Displays any action widgets in the footer (none by default).
6907 * - Ability to dismiss errors.
6909 * Subclass responsibilities:
6910 * - Register a 'safe' action.
6911 * - Register a 'primary' action.
6912 * - Add content to the dialog.
6916 * @extends OO.ui.Dialog
6919 * @param {Object} [config] Configuration options
6921 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
6922 // Parent constructor
6923 OO
.ui
.ProcessDialog
.super.call( this, config
);
6926 this.$element
.addClass( 'oo-ui-processDialog' );
6931 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
6936 * Handle dismiss button click events.
6940 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
6945 * Handle retry button click events.
6947 * Hides errors and then tries again.
6949 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
6951 this.executeAction( this.currentAction
.getAction() );
6957 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
6958 if ( this.actions
.isSpecial( action
) ) {
6961 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
6967 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
6969 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
6972 this.$navigation
= this.$( '<div>' );
6973 this.$location
= this.$( '<div>' );
6974 this.$safeActions
= this.$( '<div>' );
6975 this.$primaryActions
= this.$( '<div>' );
6976 this.$otherActions
= this.$( '<div>' );
6977 this.dismissButton
= new OO
.ui
.ButtonWidget( {
6979 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
6981 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
6982 this.$errors
= this.$( '<div>' );
6983 this.$errorsTitle
= this.$( '<div>' );
6986 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
6987 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
6990 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
6992 .append( this.title
.$element
)
6993 .addClass( 'oo-ui-processDialog-location' );
6994 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
6995 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
6996 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
6998 .addClass( 'oo-ui-processDialog-errors-title' )
6999 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
7001 .addClass( 'oo-ui-processDialog-errors' )
7002 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
7004 .addClass( 'oo-ui-processDialog-content' )
7005 .append( this.$errors
);
7007 .addClass( 'oo-ui-processDialog-navigation' )
7008 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
7009 this.$head
.append( this.$navigation
);
7010 this.$foot
.append( this.$otherActions
);
7016 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
7017 var i
, len
, other
, special
, others
;
7020 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
7022 special
= this.actions
.getSpecial();
7023 others
= this.actions
.getOthers();
7024 if ( special
.primary
) {
7025 this.$primaryActions
.append( special
.primary
.$element
);
7026 special
.primary
.toggleFramed( true );
7028 if ( others
.length
) {
7029 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
7031 this.$otherActions
.append( other
.$element
);
7032 other
.toggleFramed( true );
7035 if ( special
.safe
) {
7036 this.$safeActions
.append( special
.safe
.$element
);
7037 special
.safe
.toggleFramed( true );
7041 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
7047 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
7048 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
7049 .fail( this.showErrors
.bind( this ) );
7053 * Fit label between actions.
7057 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
7058 var width
= Math
.max(
7059 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
7060 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
7062 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
7068 * Handle errors that occurred during accept or reject processes.
7070 * @param {OO.ui.Error[]} errors Errors to be handled
7072 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
7078 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
7079 if ( !errors
[i
].isRecoverable() ) {
7080 recoverable
= false;
7082 if ( errors
[i
].isWarning() ) {
7085 $item
= this.$( '<div>' )
7086 .addClass( 'oo-ui-processDialog-error' )
7087 .append( errors
[i
].getMessage() );
7088 items
.push( $item
[0] );
7090 this.$errorItems
= this.$( items
);
7091 if ( recoverable
) {
7092 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
7094 this.currentAction
.setDisabled( true );
7097 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
7099 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
7101 this.retryButton
.toggle( recoverable
);
7102 this.$errorsTitle
.after( this.$errorItems
);
7103 this.$errors
.show().scrollTop( 0 );
7109 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
7110 this.$errors
.hide();
7111 this.$errorItems
.remove();
7112 this.$errorItems
= null;
7116 * Layout containing a series of pages.
7119 * @extends OO.ui.Layout
7122 * @param {Object} [config] Configuration options
7123 * @cfg {boolean} [continuous=false] Show all pages, one after another
7124 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
7125 * @cfg {boolean} [outlined=false] Show an outline
7126 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
7128 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
7129 // Configuration initialization
7130 config
= config
|| {};
7132 // Parent constructor
7133 OO
.ui
.BookletLayout
.super.call( this, config
);
7136 this.currentPageName
= null;
7138 this.ignoreFocus
= false;
7139 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
7140 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
7141 this.outlineVisible
= false;
7142 this.outlined
= !!config
.outlined
;
7143 if ( this.outlined
) {
7144 this.editable
= !!config
.editable
;
7145 this.outlineControlsWidget
= null;
7146 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget( { $: this.$ } );
7147 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
7148 this.gridLayout
= new OO
.ui
.GridLayout(
7149 [ this.outlinePanel
, this.stackLayout
],
7150 { $: this.$, widths
: [ 1, 2 ] }
7152 this.outlineVisible
= true;
7153 if ( this.editable
) {
7154 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
7155 this.outlineSelectWidget
, { $: this.$ }
7161 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
7162 if ( this.outlined
) {
7163 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
7165 if ( this.autoFocus
) {
7166 // Event 'focus' does not bubble, but 'focusin' does
7167 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
7171 this.$element
.addClass( 'oo-ui-bookletLayout' );
7172 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
7173 if ( this.outlined
) {
7174 this.outlinePanel
.$element
7175 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
7176 .append( this.outlineSelectWidget
.$element
);
7177 if ( this.editable
) {
7178 this.outlinePanel
.$element
7179 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
7180 .append( this.outlineControlsWidget
.$element
);
7182 this.$element
.append( this.gridLayout
.$element
);
7184 this.$element
.append( this.stackLayout
.$element
);
7190 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
7196 * @param {OO.ui.PageLayout} page Current page
7201 * @param {OO.ui.PageLayout[]} page Added pages
7202 * @param {number} index Index pages were added at
7207 * @param {OO.ui.PageLayout[]} pages Removed pages
7213 * Handle stack layout focus.
7215 * @param {jQuery.Event} e Focusin event
7217 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
7220 // Find the page that an element was focused within
7221 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
7222 for ( name
in this.pages
) {
7223 // Check for page match, exclude current page to find only page changes
7224 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
7225 this.setPage( name
);
7232 * Handle stack layout set events.
7234 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
7236 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
7239 page
.scrollElementIntoView( { complete: function () {
7240 if ( layout
.autoFocus
) {
7248 * Focus the first input in the current page.
7250 * If no page is selected, the first selectable page will be selected.
7251 * If the focus is already in an element on the current page, nothing will happen.
7253 OO
.ui
.BookletLayout
.prototype.focus = function () {
7254 var $input
, page
= this.stackLayout
.getCurrentItem();
7255 if ( !page
&& this.outlined
) {
7256 this.selectFirstSelectablePage();
7257 page
= this.stackLayout
.getCurrentItem();
7262 // Only change the focus if is not already in the current page
7263 if ( !page
.$element
.find( ':focus' ).length
) {
7264 $input
= page
.$element
.find( ':input:first' );
7265 if ( $input
.length
) {
7272 * Handle outline widget select events.
7274 * @param {OO.ui.OptionWidget|null} item Selected item
7276 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
7278 this.setPage( item
.getData() );
7283 * Check if booklet has an outline.
7287 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
7288 return this.outlined
;
7292 * Check if booklet has editing controls.
7296 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
7297 return this.editable
;
7301 * Check if booklet has a visible outline.
7305 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
7306 return this.outlined
&& this.outlineVisible
;
7310 * Hide or show the outline.
7312 * @param {boolean} [show] Show outline, omit to invert current state
7315 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
7316 if ( this.outlined
) {
7317 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
7318 this.outlineVisible
= show
;
7319 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
7326 * Get the outline widget.
7328 * @param {OO.ui.PageLayout} page Page to be selected
7329 * @return {OO.ui.PageLayout|null} Closest page to another
7331 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
7332 var next
, prev
, level
,
7333 pages
= this.stackLayout
.getItems(),
7334 index
= $.inArray( page
, pages
);
7336 if ( index
!== -1 ) {
7337 next
= pages
[index
+ 1];
7338 prev
= pages
[index
- 1];
7339 // Prefer adjacent pages at the same level
7340 if ( this.outlined
) {
7341 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
7344 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
7350 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
7356 return prev
|| next
|| null;
7360 * Get the outline widget.
7362 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if booklet has no outline
7364 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
7365 return this.outlineSelectWidget
;
7369 * Get the outline controls widget. If the outline is not editable, null is returned.
7371 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
7373 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
7374 return this.outlineControlsWidget
;
7378 * Get a page by name.
7380 * @param {string} name Symbolic name of page
7381 * @return {OO.ui.PageLayout|undefined} Page, if found
7383 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
7384 return this.pages
[name
];
7388 * Get the current page name.
7390 * @return {string|null} Current page name
7392 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
7393 return this.currentPageName
;
7397 * Add a page to the layout.
7399 * When pages are added with the same names as existing pages, the existing pages will be
7400 * automatically removed before the new pages are added.
7402 * @param {OO.ui.PageLayout[]} pages Pages to add
7403 * @param {number} index Index to insert pages after
7407 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
7408 var i
, len
, name
, page
, item
, currentIndex
,
7409 stackLayoutPages
= this.stackLayout
.getItems(),
7413 // Remove pages with same names
7414 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7416 name
= page
.getName();
7418 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
7419 // Correct the insertion index
7420 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
7421 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
7424 remove
.push( this.pages
[name
] );
7427 if ( remove
.length
) {
7428 this.removePages( remove
);
7432 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7434 name
= page
.getName();
7435 this.pages
[page
.getName()] = page
;
7436 if ( this.outlined
) {
7437 item
= new OO
.ui
.OutlineOptionWidget( { $: this.$, data
: name
} );
7438 page
.setOutlineItem( item
);
7443 if ( this.outlined
&& items
.length
) {
7444 this.outlineSelectWidget
.addItems( items
, index
);
7445 this.selectFirstSelectablePage();
7447 this.stackLayout
.addItems( pages
, index
);
7448 this.emit( 'add', pages
, index
);
7454 * Remove a page from the layout.
7459 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
7460 var i
, len
, name
, page
,
7463 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7465 name
= page
.getName();
7466 delete this.pages
[name
];
7467 if ( this.outlined
) {
7468 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
7469 page
.setOutlineItem( null );
7472 if ( this.outlined
&& items
.length
) {
7473 this.outlineSelectWidget
.removeItems( items
);
7474 this.selectFirstSelectablePage();
7476 this.stackLayout
.removeItems( pages
);
7477 this.emit( 'remove', pages
);
7483 * Clear all pages from the layout.
7488 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
7490 pages
= this.stackLayout
.getItems();
7493 this.currentPageName
= null;
7494 if ( this.outlined
) {
7495 this.outlineSelectWidget
.clearItems();
7496 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7497 pages
[i
].setOutlineItem( null );
7500 this.stackLayout
.clearItems();
7502 this.emit( 'remove', pages
);
7508 * Set the current page by name.
7511 * @param {string} name Symbolic name of page
7513 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
7516 page
= this.pages
[name
];
7518 if ( name
!== this.currentPageName
) {
7519 if ( this.outlined
) {
7520 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
7521 if ( selectedItem
&& selectedItem
.getData() !== name
) {
7522 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getItemFromData( name
) );
7526 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
7527 this.pages
[this.currentPageName
].setActive( false );
7528 // Blur anything focused if the next page doesn't have anything focusable - this
7529 // is not needed if the next page has something focusable because once it is focused
7530 // this blur happens automatically
7531 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
7532 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
7533 if ( $focused
.length
) {
7538 this.currentPageName
= name
;
7539 this.stackLayout
.setItem( page
);
7540 page
.setActive( true );
7541 this.emit( 'set', page
);
7547 * Select the first selectable page.
7551 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
7552 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
7553 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
7560 * Layout made of a field and optional label.
7562 * Available label alignment modes include:
7563 * - left: Label is before the field and aligned away from it, best for when the user will be
7564 * scanning for a specific label in a form with many fields
7565 * - right: Label is before the field and aligned toward it, best for forms the user is very
7566 * familiar with and will tab through field checking quickly to verify which field they are in
7567 * - top: Label is before the field and above it, best for when the user will need to fill out all
7568 * fields from top to bottom in a form with few fields
7569 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
7570 * checkboxes or radio buttons
7573 * @extends OO.ui.Layout
7574 * @mixins OO.ui.LabelElement
7577 * @param {OO.ui.Widget} fieldWidget Field widget
7578 * @param {Object} [config] Configuration options
7579 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
7580 * @cfg {string} [help] Explanatory text shown as a '?' icon.
7582 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
7583 var hasInputWidget
= fieldWidget
instanceof OO
.ui
.InputWidget
;
7585 // Configuration initialization
7586 config
= $.extend( { align
: 'left' }, config
);
7588 // Properties (must be set before parent constructor, which calls #getTagName)
7589 this.fieldWidget
= fieldWidget
;
7591 // Parent constructor
7592 OO
.ui
.FieldLayout
.super.call( this, config
);
7594 // Mixin constructors
7595 OO
.ui
.LabelElement
.call( this, config
);
7598 this.$field
= this.$( '<div>' );
7599 this.$body
= this.$( '<' + ( hasInputWidget
? 'label' : 'div' ) + '>' );
7601 if ( config
.help
) {
7602 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
7604 classes
: [ 'oo-ui-fieldLayout-help' ],
7609 this.popupButtonWidget
.getPopup().$body
.append(
7611 .text( config
.help
)
7612 .addClass( 'oo-ui-fieldLayout-help-content' )
7614 this.$help
= this.popupButtonWidget
.$element
;
7616 this.$help
= this.$( [] );
7620 if ( hasInputWidget
) {
7621 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
7623 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
7627 .addClass( 'oo-ui-fieldLayout' )
7628 .append( this.$help
, this.$body
);
7629 this.$body
.addClass( 'oo-ui-fieldLayout-body' );
7631 .addClass( 'oo-ui-fieldLayout-field' )
7632 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
7633 .append( this.fieldWidget
.$element
);
7635 this.setAlignment( config
.align
);
7640 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
7641 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
7646 * Handle field disable events.
7648 * @param {boolean} value Field is disabled
7650 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
7651 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
7655 * Handle label mouse click events.
7657 * @param {jQuery.Event} e Mouse click event
7659 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
7660 this.fieldWidget
.simulateLabelClick();
7667 * @return {OO.ui.Widget} Field widget
7669 OO
.ui
.FieldLayout
.prototype.getField = function () {
7670 return this.fieldWidget
;
7674 * Set the field alignment mode.
7677 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
7680 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
7681 if ( value
!== this.align
) {
7682 // Default to 'left'
7683 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
7687 if ( value
=== 'inline' ) {
7688 this.$body
.append( this.$field
, this.$label
);
7690 this.$body
.append( this.$label
, this.$field
);
7692 // Set classes. The following classes can be used here:
7693 // * oo-ui-fieldLayout-align-left
7694 // * oo-ui-fieldLayout-align-right
7695 // * oo-ui-fieldLayout-align-top
7696 // * oo-ui-fieldLayout-align-inline
7698 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
7700 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
7708 * Layout made of a field, a button, and an optional label.
7711 * @extends OO.ui.FieldLayout
7714 * @param {OO.ui.Widget} fieldWidget Field widget
7715 * @param {OO.ui.ButtonWidget} buttonWidget Button widget
7716 * @param {Object} [config] Configuration options
7717 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
7718 * @cfg {string} [help] Explanatory text shown as a '?' icon.
7720 OO
.ui
.ActionFieldLayout
= function OoUiActionFieldLayout( fieldWidget
, buttonWidget
, config
) {
7721 // Configuration initialization
7722 config
= $.extend( { align
: 'left' }, config
);
7724 // Properties (must be set before parent constructor, which calls #getTagName)
7725 this.fieldWidget
= fieldWidget
;
7726 this.buttonWidget
= buttonWidget
;
7728 // Parent constructor
7729 OO
.ui
.ActionFieldLayout
.super.call( this, fieldWidget
, config
);
7731 // Mixin constructors
7732 OO
.ui
.LabelElement
.call( this, config
);
7735 this.$button
= this.$( '<div>' )
7736 .addClass( 'oo-ui-actionFieldLayout-button' )
7737 .append( this.buttonWidget
.$element
);
7739 this.$input
= this.$( '<div>' )
7740 .addClass( 'oo-ui-actionFieldLayout-input' )
7741 .append( this.fieldWidget
.$element
);
7744 .addClass( 'oo-ui-actionFieldLayout' )
7745 .append( this.$input
, this.$button
);
7750 OO
.inheritClass( OO
.ui
.ActionFieldLayout
, OO
.ui
.FieldLayout
);
7753 * Layout made of a fieldset and optional legend.
7755 * Just add OO.ui.FieldLayout items.
7758 * @extends OO.ui.Layout
7759 * @mixins OO.ui.IconElement
7760 * @mixins OO.ui.LabelElement
7761 * @mixins OO.ui.GroupElement
7764 * @param {Object} [config] Configuration options
7765 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
7767 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
7768 // Configuration initialization
7769 config
= config
|| {};
7771 // Parent constructor
7772 OO
.ui
.FieldsetLayout
.super.call( this, config
);
7774 // Mixin constructors
7775 OO
.ui
.IconElement
.call( this, config
);
7776 OO
.ui
.LabelElement
.call( this, config
);
7777 OO
.ui
.GroupElement
.call( this, config
);
7781 .addClass( 'oo-ui-fieldsetLayout' )
7782 .prepend( this.$icon
, this.$label
, this.$group
);
7783 if ( $.isArray( config
.items
) ) {
7784 this.addItems( config
.items
);
7790 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
7791 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
7792 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
7793 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
7796 * Layout with an HTML form.
7799 * @extends OO.ui.Layout
7802 * @param {Object} [config] Configuration options
7803 * @cfg {string} [method] HTML form `method` attribute
7804 * @cfg {string} [action] HTML form `action` attribute
7805 * @cfg {string} [enctype] HTML form `enctype` attribute
7807 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
7808 // Configuration initialization
7809 config
= config
|| {};
7811 // Parent constructor
7812 OO
.ui
.FormLayout
.super.call( this, config
);
7815 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
7819 .addClass( 'oo-ui-formLayout' )
7821 method
: config
.method
,
7822 action
: config
.action
,
7823 enctype
: config
.enctype
7829 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
7837 /* Static Properties */
7839 OO
.ui
.FormLayout
.static.tagName
= 'form';
7844 * Handle form submit events.
7846 * @param {jQuery.Event} e Submit event
7849 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
7850 this.emit( 'submit' );
7855 * Layout made of proportionally sized columns and rows.
7858 * @extends OO.ui.Layout
7861 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
7862 * @param {Object} [config] Configuration options
7863 * @cfg {number[]} [widths] Widths of columns as ratios
7864 * @cfg {number[]} [heights] Heights of rows as ratios
7866 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
7869 // Configuration initialization
7870 config
= config
|| {};
7872 // Parent constructor
7873 OO
.ui
.GridLayout
.super.call( this, config
);
7881 this.$element
.addClass( 'oo-ui-gridLayout' );
7882 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
7883 this.panels
.push( panels
[i
] );
7884 this.$element
.append( panels
[i
].$element
);
7886 if ( config
.widths
|| config
.heights
) {
7887 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
7889 // Arrange in columns by default
7890 widths
= this.panels
.map( function () { return 1; } );
7891 this.layout( widths
, [ 1 ] );
7897 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
7912 * Set grid dimensions.
7914 * @param {number[]} widths Widths of columns as ratios
7915 * @param {number[]} heights Heights of rows as ratios
7917 * @throws {Error} If grid is not large enough to fit all panels
7919 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
7923 cols
= widths
.length
,
7924 rows
= heights
.length
;
7926 // Verify grid is big enough to fit panels
7927 if ( cols
* rows
< this.panels
.length
) {
7928 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
7931 // Sum up denominators
7932 for ( x
= 0; x
< cols
; x
++ ) {
7935 for ( y
= 0; y
< rows
; y
++ ) {
7941 for ( x
= 0; x
< cols
; x
++ ) {
7942 this.widths
[x
] = widths
[x
] / xd
;
7944 for ( y
= 0; y
< rows
; y
++ ) {
7945 this.heights
[y
] = heights
[y
] / yd
;
7949 this.emit( 'layout' );
7953 * Update panel positions and sizes.
7957 OO
.ui
.GridLayout
.prototype.update = function () {
7958 var x
, y
, panel
, width
, height
, dimensions
,
7962 cols
= this.widths
.length
,
7963 rows
= this.heights
.length
;
7965 for ( y
= 0; y
< rows
; y
++ ) {
7966 height
= this.heights
[y
];
7967 for ( x
= 0; x
< cols
; x
++ ) {
7968 width
= this.widths
[x
];
7969 panel
= this.panels
[i
];
7971 width
: ( width
* 100 ) + '%',
7972 height
: ( height
* 100 ) + '%',
7973 top
: ( top
* 100 ) + '%'
7976 if ( OO
.ui
.Element
.static.getDir( this.$.context
) === 'rtl' ) {
7977 dimensions
.right
= ( left
* 100 ) + '%';
7979 dimensions
.left
= ( left
* 100 ) + '%';
7981 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
7982 if ( width
=== 0 || height
=== 0 ) {
7983 dimensions
.visibility
= 'hidden';
7985 dimensions
.visibility
= '';
7987 panel
.$element
.css( dimensions
);
7995 this.emit( 'update' );
7999 * Get a panel at a given position.
8001 * The x and y position is affected by the current grid layout.
8003 * @param {number} x Horizontal position
8004 * @param {number} y Vertical position
8005 * @return {OO.ui.PanelLayout} The panel at the given position
8007 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
8008 return this.panels
[ ( x
* this.widths
.length
) + y
];
8012 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
8015 * @extends OO.ui.Layout
8018 * @param {Object} [config] Configuration options
8019 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
8020 * @cfg {boolean} [padded=false] Pad the content from the edges
8021 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
8023 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
8024 // Configuration initialization
8025 config
= $.extend( {
8031 // Parent constructor
8032 OO
.ui
.PanelLayout
.super.call( this, config
);
8035 this.$element
.addClass( 'oo-ui-panelLayout' );
8036 if ( config
.scrollable
) {
8037 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
8039 if ( config
.padded
) {
8040 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
8042 if ( config
.expanded
) {
8043 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
8049 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
8052 * Page within an booklet layout.
8055 * @extends OO.ui.PanelLayout
8058 * @param {string} name Unique symbolic name of page
8059 * @param {Object} [config] Configuration options
8061 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
8062 // Configuration initialization
8063 config
= $.extend( { scrollable
: true }, config
);
8065 // Parent constructor
8066 OO
.ui
.PageLayout
.super.call( this, config
);
8070 this.outlineItem
= null;
8071 this.active
= false;
8074 this.$element
.addClass( 'oo-ui-pageLayout' );
8079 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
8085 * @param {boolean} active Page is active
8093 * @return {string} Symbolic name of page
8095 OO
.ui
.PageLayout
.prototype.getName = function () {
8100 * Check if page is active.
8102 * @return {boolean} Page is active
8104 OO
.ui
.PageLayout
.prototype.isActive = function () {
8111 * @return {OO.ui.OutlineOptionWidget|null} Outline item widget
8113 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
8114 return this.outlineItem
;
8120 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
8121 * outline item as desired; this method is called for setting (with an object) and unsetting
8122 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
8123 * operating on null instead of an OO.ui.OutlineOptionWidget object.
8125 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline item widget, null to clear
8128 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
8129 this.outlineItem
= outlineItem
|| null;
8130 if ( outlineItem
) {
8131 this.setupOutlineItem();
8137 * Setup outline item.
8139 * @localdoc Subclasses should override this method to adjust the outline item as desired.
8141 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline item widget to setup
8144 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
8149 * Set page active state.
8151 * @param {boolean} Page is active
8154 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
8157 if ( active
!== this.active
) {
8158 this.active
= active
;
8159 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
8160 this.emit( 'active', this.active
);
8165 * Layout containing a series of mutually exclusive pages.
8168 * @extends OO.ui.PanelLayout
8169 * @mixins OO.ui.GroupElement
8172 * @param {Object} [config] Configuration options
8173 * @cfg {boolean} [continuous=false] Show all pages, one after another
8174 * @cfg {OO.ui.Layout[]} [items] Layouts to add
8176 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
8177 // Configuration initialization
8178 config
= $.extend( { scrollable
: true }, config
);
8180 // Parent constructor
8181 OO
.ui
.StackLayout
.super.call( this, config
);
8183 // Mixin constructors
8184 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8187 this.currentItem
= null;
8188 this.continuous
= !!config
.continuous
;
8191 this.$element
.addClass( 'oo-ui-stackLayout' );
8192 if ( this.continuous
) {
8193 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
8195 if ( $.isArray( config
.items
) ) {
8196 this.addItems( config
.items
);
8202 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
8203 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
8209 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
8215 * Get the current item.
8217 * @return {OO.ui.Layout|null}
8219 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
8220 return this.currentItem
;
8224 * Unset the current item.
8227 * @param {OO.ui.StackLayout} layout
8230 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
8231 var prevItem
= this.currentItem
;
8232 if ( prevItem
=== null ) {
8236 this.currentItem
= null;
8237 this.emit( 'set', null );
8243 * Adding an existing item (by value) will move it.
8245 * @param {OO.ui.Layout[]} items Items to add
8246 * @param {number} [index] Index to insert items after
8249 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
8251 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
8253 if ( !this.currentItem
&& items
.length
) {
8254 this.setItem( items
[0] );
8263 * Items will be detached, not removed, so they can be used later.
8265 * @param {OO.ui.Layout[]} items Items to remove
8269 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
8271 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
8273 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
8274 if ( this.items
.length
) {
8275 this.setItem( this.items
[0] );
8277 this.unsetCurrentItem();
8287 * Items will be detached, not removed, so they can be used later.
8292 OO
.ui
.StackLayout
.prototype.clearItems = function () {
8293 this.unsetCurrentItem();
8294 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
8302 * Any currently shown item will be hidden.
8304 * FIXME: If the passed item to show has not been added in the items list, then
8305 * this method drops it and unsets the current item.
8307 * @param {OO.ui.Layout} item Item to show
8311 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
8314 if ( item
!== this.currentItem
) {
8315 if ( !this.continuous
) {
8316 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
8317 this.items
[i
].$element
.css( 'display', '' );
8320 if ( $.inArray( item
, this.items
) !== -1 ) {
8321 if ( !this.continuous
) {
8322 item
.$element
.css( 'display', 'block' );
8324 this.currentItem
= item
;
8325 this.emit( 'set', item
);
8327 this.unsetCurrentItem();
8335 * Horizontal bar layout of tools as icon buttons.
8338 * @extends OO.ui.ToolGroup
8341 * @param {OO.ui.Toolbar} toolbar
8342 * @param {Object} [config] Configuration options
8344 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
8345 // Parent constructor
8346 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
8349 this.$element
.addClass( 'oo-ui-barToolGroup' );
8354 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
8356 /* Static Properties */
8358 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
8360 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
8362 OO
.ui
.BarToolGroup
.static.name
= 'bar';
8365 * Popup list of tools with an icon and optional label.
8369 * @extends OO.ui.ToolGroup
8370 * @mixins OO.ui.IconElement
8371 * @mixins OO.ui.IndicatorElement
8372 * @mixins OO.ui.LabelElement
8373 * @mixins OO.ui.TitledElement
8374 * @mixins OO.ui.ClippableElement
8377 * @param {OO.ui.Toolbar} toolbar
8378 * @param {Object} [config] Configuration options
8379 * @cfg {string} [header] Text to display at the top of the pop-up
8381 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
8382 // Configuration initialization
8383 config
= config
|| {};
8385 // Parent constructor
8386 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
8388 // Mixin constructors
8389 OO
.ui
.IconElement
.call( this, config
);
8390 OO
.ui
.IndicatorElement
.call( this, config
);
8391 OO
.ui
.LabelElement
.call( this, config
);
8392 OO
.ui
.TitledElement
.call( this, config
);
8393 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
8396 this.active
= false;
8397 this.dragging
= false;
8398 this.onBlurHandler
= this.onBlur
.bind( this );
8399 this.$handle
= this.$( '<span>' );
8403 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
8404 'mouseup touchend': this.onHandlePointerUp
.bind( this )
8409 .addClass( 'oo-ui-popupToolGroup-handle' )
8410 .append( this.$icon
, this.$label
, this.$indicator
);
8411 // If the pop-up should have a header, add it to the top of the toolGroup.
8412 // Note: If this feature is useful for other widgets, we could abstract it into an
8413 // OO.ui.HeaderedElement mixin constructor.
8414 if ( config
.header
!== undefined ) {
8416 .prepend( this.$( '<span>' )
8417 .addClass( 'oo-ui-popupToolGroup-header' )
8418 .text( config
.header
)
8422 .addClass( 'oo-ui-popupToolGroup' )
8423 .prepend( this.$handle
);
8428 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
8429 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
8430 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
8431 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
8432 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
8433 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
8435 /* Static Properties */
8442 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
8444 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
8446 if ( this.isDisabled() && this.isElementAttached() ) {
8447 this.setActive( false );
8452 * Handle focus being lost.
8454 * The event is actually generated from a mouseup, so it is not a normal blur event object.
8456 * @param {jQuery.Event} e Mouse up event
8458 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
8459 // Only deactivate when clicking outside the dropdown element
8460 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
8461 this.setActive( false );
8468 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
8469 // e.which is 0 for touch events, 1 for left mouse button
8470 // Only close toolgroup when a tool was actually selected
8471 // FIXME: this duplicates logic from the parent class
8472 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== this.getTargetTool( e
) ) {
8473 this.setActive( false );
8475 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
8479 * Handle mouse up events.
8481 * @param {jQuery.Event} e Mouse up event
8483 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
8488 * Handle mouse down events.
8490 * @param {jQuery.Event} e Mouse down event
8492 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
8493 // e.which is 0 for touch events, 1 for left mouse button
8494 if ( !this.isDisabled() && e
.which
<= 1 ) {
8495 this.setActive( !this.active
);
8501 * Switch into active mode.
8503 * When active, mouseup events anywhere in the document will trigger deactivation.
8505 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
8507 if ( this.active
!== value
) {
8508 this.active
= value
;
8510 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
8512 // Try anchoring the popup to the left first
8513 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
8514 this.toggleClipping( true );
8515 if ( this.isClippedHorizontally() ) {
8516 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
8517 this.toggleClipping( false );
8519 .removeClass( 'oo-ui-popupToolGroup-left' )
8520 .addClass( 'oo-ui-popupToolGroup-right' );
8521 this.toggleClipping( true );
8524 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
8525 this.$element
.removeClass(
8526 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
8528 this.toggleClipping( false );
8534 * Drop down list layout of tools as labeled icon buttons.
8536 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
8537 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
8538 * may want to use the 'promote' and 'demote' configuration options to achieve this.
8541 * @extends OO.ui.PopupToolGroup
8544 * @param {OO.ui.Toolbar} toolbar
8545 * @param {Object} [config] Configuration options
8546 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
8548 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
8549 * allowed to be collapsed.
8550 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
8552 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
8553 // Configuration initialization
8554 config
= config
|| {};
8556 // Properties (must be set before parent constructor, which calls #populate)
8557 this.allowCollapse
= config
.allowCollapse
;
8558 this.forceExpand
= config
.forceExpand
;
8559 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
8560 this.collapsibleTools
= [];
8562 // Parent constructor
8563 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
8566 this.$element
.addClass( 'oo-ui-listToolGroup' );
8571 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
8573 /* Static Properties */
8575 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
8577 OO
.ui
.ListToolGroup
.static.name
= 'list';
8584 OO
.ui
.ListToolGroup
.prototype.populate = function () {
8585 var i
, len
, allowCollapse
= [];
8587 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
8589 // Update the list of collapsible tools
8590 if ( this.allowCollapse
!== undefined ) {
8591 allowCollapse
= this.allowCollapse
;
8592 } else if ( this.forceExpand
!== undefined ) {
8593 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
8596 this.collapsibleTools
= [];
8597 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
8598 if ( this.tools
[ allowCollapse
[i
] ] !== undefined ) {
8599 this.collapsibleTools
.push( this.tools
[ allowCollapse
[i
] ] );
8603 // Keep at the end, even when tools are added
8604 this.$group
.append( this.getExpandCollapseTool().$element
);
8606 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
8608 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
8609 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
8610 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
8611 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
8612 this.getExpandCollapseTool().$element
.css( 'display', 'block' );
8615 this.updateCollapsibleState();
8618 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
8619 if ( this.expandCollapseTool
=== undefined ) {
8620 var ExpandCollapseTool = function () {
8621 ExpandCollapseTool
.super.apply( this, arguments
);
8624 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
8626 ExpandCollapseTool
.prototype.onSelect = function () {
8627 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
8628 this.toolGroup
.updateCollapsibleState();
8629 this.setActive( false );
8631 ExpandCollapseTool
.prototype.onUpdateState = function () {
8632 // Do nothing. Tool interface requires an implementation of this function.
8635 ExpandCollapseTool
.static.name
= 'more-fewer';
8637 this.expandCollapseTool
= new ExpandCollapseTool( this );
8639 return this.expandCollapseTool
;
8645 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
8646 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
8648 // Do not close the popup when the user wants to show more/fewer tools
8649 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
8650 // Prevent the popup list from being hidden
8651 this.setActive( true );
8657 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
8660 this.getExpandCollapseTool()
8661 .setIcon( this.expanded
? 'collapse' : 'expand' )
8662 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
8664 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
8665 this.collapsibleTools
[i
].toggle( this.expanded
);
8670 * Drop down menu layout of tools as selectable menu items.
8673 * @extends OO.ui.PopupToolGroup
8676 * @param {OO.ui.Toolbar} toolbar
8677 * @param {Object} [config] Configuration options
8679 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
8680 // Configuration initialization
8681 config
= config
|| {};
8683 // Parent constructor
8684 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
8687 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
8690 this.$element
.addClass( 'oo-ui-menuToolGroup' );
8695 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
8697 /* Static Properties */
8699 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
8701 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
8706 * Handle the toolbar state being updated.
8708 * When the state changes, the title of each active item in the menu will be joined together and
8709 * used as a label for the group. The label will be empty if none of the items are active.
8711 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
8715 for ( name
in this.tools
) {
8716 if ( this.tools
[name
].isActive() ) {
8717 labelTexts
.push( this.tools
[name
].getTitle() );
8721 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
8725 * Tool that shows a popup when selected.
8729 * @extends OO.ui.Tool
8730 * @mixins OO.ui.PopupElement
8733 * @param {OO.ui.Toolbar} toolbar
8734 * @param {Object} [config] Configuration options
8736 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
8737 // Parent constructor
8738 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
8740 // Mixin constructors
8741 OO
.ui
.PopupElement
.call( this, config
);
8745 .addClass( 'oo-ui-popupTool' )
8746 .append( this.popup
.$element
);
8751 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
8752 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
8757 * Handle the tool being selected.
8761 OO
.ui
.PopupTool
.prototype.onSelect = function () {
8762 if ( !this.isDisabled() ) {
8763 this.popup
.toggle();
8765 this.setActive( false );
8770 * Handle the toolbar state being updated.
8774 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
8775 this.setActive( false );
8779 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
8781 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
8785 * @extends OO.ui.GroupElement
8788 * @param {Object} [config] Configuration options
8790 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
8791 // Parent constructor
8792 OO
.ui
.GroupWidget
.super.call( this, config
);
8797 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
8802 * Set the disabled state of the widget.
8804 * This will also update the disabled state of child widgets.
8806 * @param {boolean} disabled Disable widget
8809 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
8813 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
8814 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
8816 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
8818 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
8819 this.items
[i
].updateDisabled();
8827 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
8829 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
8830 * allows bidirectional communication.
8832 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
8839 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
8846 * Check if widget is disabled.
8848 * Checks parent if present, making disabled state inheritable.
8850 * @return {boolean} Widget is disabled
8852 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
8853 return this.disabled
||
8854 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
8858 * Set group element is in.
8860 * @param {OO.ui.GroupElement|null} group Group element, null if none
8863 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
8865 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
8866 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
8868 // Initialize item disabled states
8869 this.updateDisabled();
8875 * Mixin that adds a menu showing suggested values for a text input.
8877 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
8879 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
8880 * be aware that this will cause new suggestions to be looked up for the new value. If this is
8881 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
8885 * @deprecated Use LookupElement instead.
8888 * @param {OO.ui.TextInputWidget} input Input widget
8889 * @param {Object} [config] Configuration options
8890 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
8891 * @cfg {jQuery} [$container=input.$element] Element to render menu under
8893 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
8894 // Configuration initialization
8895 config
= config
|| {};
8898 this.lookupInput
= input
;
8899 this.$overlay
= config
.$overlay
|| this.$element
;
8900 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
8901 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
8902 input
: this.lookupInput
,
8903 $container
: config
.$container
8905 this.lookupCache
= {};
8906 this.lookupQuery
= null;
8907 this.lookupRequest
= null;
8908 this.lookupsDisabled
= false;
8909 this.lookupInputFocused
= false;
8912 this.lookupInput
.$input
.on( {
8913 focus
: this.onLookupInputFocus
.bind( this ),
8914 blur
: this.onLookupInputBlur
.bind( this ),
8915 mousedown
: this.onLookupInputMouseDown
.bind( this )
8917 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
8918 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
8921 this.$element
.addClass( 'oo-ui-lookupWidget' );
8922 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
8923 this.$overlay
.append( this.lookupMenu
.$element
);
8929 * Handle input focus event.
8931 * @param {jQuery.Event} e Input focus event
8933 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
8934 this.lookupInputFocused
= true;
8935 this.populateLookupMenu();
8939 * Handle input blur event.
8941 * @param {jQuery.Event} e Input blur event
8943 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
8944 this.closeLookupMenu();
8945 this.lookupInputFocused
= false;
8949 * Handle input mouse down event.
8951 * @param {jQuery.Event} e Input mouse down event
8953 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
8954 // Only open the menu if the input was already focused.
8955 // This way we allow the user to open the menu again after closing it with Esc
8956 // by clicking in the input. Opening (and populating) the menu when initially
8957 // clicking into the input is handled by the focus handler.
8958 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
8959 this.populateLookupMenu();
8964 * Handle input change event.
8966 * @param {string} value New input value
8968 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
8969 if ( this.lookupInputFocused
) {
8970 this.populateLookupMenu();
8975 * Handle the lookup menu being shown/hidden.
8976 * @param {boolean} visible Whether the lookup menu is now visible.
8978 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
8980 // When the menu is hidden, abort any active request and clear the menu.
8981 // This has to be done here in addition to closeLookupMenu(), because
8982 // MenuSelectWidget will close itself when the user presses Esc.
8983 this.abortLookupRequest();
8984 this.lookupMenu
.clearItems();
8991 * @return {OO.ui.TextInputMenuSelectWidget}
8993 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
8994 return this.lookupMenu
;
8998 * Disable or re-enable lookups.
9000 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
9002 * @param {boolean} disabled Disable lookups
9004 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
9005 this.lookupsDisabled
= !!disabled
;
9009 * Open the menu. If there are no entries in the menu, this does nothing.
9013 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
9014 if ( !this.lookupMenu
.isEmpty() ) {
9015 this.lookupMenu
.toggle( true );
9021 * Close the menu, empty it, and abort any pending request.
9025 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
9026 this.lookupMenu
.toggle( false );
9027 this.abortLookupRequest();
9028 this.lookupMenu
.clearItems();
9033 * Request menu items based on the input's current value, and when they arrive,
9034 * populate the menu with these items and show the menu.
9036 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
9040 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
9042 value
= this.lookupInput
.getValue();
9044 if ( this.lookupsDisabled
) {
9048 // If the input is empty, clear the menu
9049 if ( value
=== '' ) {
9050 this.closeLookupMenu();
9051 // Skip population if there is already a request pending for the current value
9052 } else if ( value
!== this.lookupQuery
) {
9053 this.getLookupMenuItems()
9054 .done( function ( items
) {
9055 widget
.lookupMenu
.clearItems();
9056 if ( items
.length
) {
9060 widget
.initializeLookupMenuSelection();
9062 widget
.lookupMenu
.toggle( false );
9065 .fail( function () {
9066 widget
.lookupMenu
.clearItems();
9074 * Select and highlight the first selectable item in the menu.
9078 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
9079 if ( !this.lookupMenu
.getSelectedItem() ) {
9080 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
9082 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
9086 * Get lookup menu items for the current query.
9088 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
9089 * of the done event. If the request was aborted to make way for a subsequent request,
9090 * this promise will not be rejected: it will remain pending forever.
9092 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
9094 value
= this.lookupInput
.getValue(),
9095 deferred
= $.Deferred(),
9098 this.abortLookupRequest();
9099 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
9100 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
9102 this.lookupInput
.pushPending();
9103 this.lookupQuery
= value
;
9104 ourRequest
= this.lookupRequest
= this.getLookupRequest();
9106 .always( function () {
9107 // We need to pop pending even if this is an old request, otherwise
9108 // the widget will remain pending forever.
9109 // TODO: this assumes that an aborted request will fail or succeed soon after
9110 // being aborted, or at least eventually. It would be nice if we could popPending()
9111 // at abort time, but only if we knew that we hadn't already called popPending()
9112 // for that request.
9113 widget
.lookupInput
.popPending();
9115 .done( function ( data
) {
9116 // If this is an old request (and aborting it somehow caused it to still succeed),
9117 // ignore its success completely
9118 if ( ourRequest
=== widget
.lookupRequest
) {
9119 widget
.lookupQuery
= null;
9120 widget
.lookupRequest
= null;
9121 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
9122 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
9125 .fail( function () {
9126 // If this is an old request (or a request failing because it's being aborted),
9127 // ignore its failure completely
9128 if ( ourRequest
=== widget
.lookupRequest
) {
9129 widget
.lookupQuery
= null;
9130 widget
.lookupRequest
= null;
9135 return deferred
.promise();
9139 * Abort the currently pending lookup request, if any.
9141 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
9142 var oldRequest
= this.lookupRequest
;
9144 // First unset this.lookupRequest to the fail handler will notice
9145 // that the request is no longer current
9146 this.lookupRequest
= null;
9147 this.lookupQuery
= null;
9153 * Get a new request object of the current lookup query value.
9156 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
9158 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
9159 // Stub, implemented in subclass
9164 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
9167 * @param {Mixed} data Cached result data, usually an array
9168 * @return {OO.ui.MenuOptionWidget[]} Menu items
9170 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
9171 // Stub, implemented in subclass
9176 * Get lookup cache item from server response data.
9179 * @param {Mixed} data Response from server
9180 * @return {Mixed} Cached result data
9182 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
9183 // Stub, implemented in subclass
9188 * Set of controls for an OO.ui.OutlineSelectWidget.
9190 * Controls include moving items up and down, removing items, and adding different kinds of items.
9193 * @extends OO.ui.Widget
9194 * @mixins OO.ui.GroupElement
9195 * @mixins OO.ui.IconElement
9198 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
9199 * @param {Object} [config] Configuration options
9201 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
9202 // Configuration initialization
9203 config
= $.extend( { icon
: 'add' }, config
);
9205 // Parent constructor
9206 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
9208 // Mixin constructors
9209 OO
.ui
.GroupElement
.call( this, config
);
9210 OO
.ui
.IconElement
.call( this, config
);
9213 this.outline
= outline
;
9214 this.$movers
= this.$( '<div>' );
9215 this.upButton
= new OO
.ui
.ButtonWidget( {
9219 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
9221 this.downButton
= new OO
.ui
.ButtonWidget( {
9225 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
9227 this.removeButton
= new OO
.ui
.ButtonWidget( {
9231 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
9235 outline
.connect( this, {
9236 select
: 'onOutlineChange',
9237 add
: 'onOutlineChange',
9238 remove
: 'onOutlineChange'
9240 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
9241 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
9242 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
9245 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
9246 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
9248 .addClass( 'oo-ui-outlineControlsWidget-movers' )
9249 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
9250 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
9255 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
9256 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
9257 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
9263 * @param {number} places Number of places to move
9273 * Handle outline change events.
9275 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
9276 var i
, len
, firstMovable
, lastMovable
,
9277 items
= this.outline
.getItems(),
9278 selectedItem
= this.outline
.getSelectedItem(),
9279 movable
= selectedItem
&& selectedItem
.isMovable(),
9280 removable
= selectedItem
&& selectedItem
.isRemovable();
9285 while ( ++i
< len
) {
9286 if ( items
[i
].isMovable() ) {
9287 firstMovable
= items
[i
];
9293 if ( items
[i
].isMovable() ) {
9294 lastMovable
= items
[i
];
9299 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
9300 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
9301 this.removeButton
.setDisabled( !removable
);
9305 * Mixin for widgets with a boolean on/off state.
9311 * @param {Object} [config] Configuration options
9312 * @cfg {boolean} [value=false] Initial value
9314 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
9315 // Configuration initialization
9316 config
= config
|| {};
9322 this.$element
.addClass( 'oo-ui-toggleWidget' );
9323 this.setValue( !!config
.value
);
9330 * @param {boolean} value Changed value
9336 * Get the value of the toggle.
9340 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
9345 * Set the value of the toggle.
9347 * @param {boolean} value New value
9351 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
9353 if ( this.value
!== value
) {
9355 this.emit( 'change', value
);
9356 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
9357 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
9363 * Group widget for multiple related buttons.
9365 * Use together with OO.ui.ButtonWidget.
9368 * @extends OO.ui.Widget
9369 * @mixins OO.ui.GroupElement
9372 * @param {Object} [config] Configuration options
9373 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
9375 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
9376 // Configuration initialization
9377 config
= config
|| {};
9379 // Parent constructor
9380 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
9382 // Mixin constructors
9383 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
9386 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
9387 if ( $.isArray( config
.items
) ) {
9388 this.addItems( config
.items
);
9394 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
9395 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
9398 * Generic widget for buttons.
9401 * @extends OO.ui.Widget
9402 * @mixins OO.ui.ButtonElement
9403 * @mixins OO.ui.IconElement
9404 * @mixins OO.ui.IndicatorElement
9405 * @mixins OO.ui.LabelElement
9406 * @mixins OO.ui.TitledElement
9407 * @mixins OO.ui.FlaggedElement
9410 * @param {Object} [config] Configuration options
9411 * @cfg {string} [href] Hyperlink to visit when clicked
9412 * @cfg {string} [target] Target to open hyperlink in
9414 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
9415 // Configuration initialization
9416 config
= config
|| {};
9418 // Parent constructor
9419 OO
.ui
.ButtonWidget
.super.call( this, config
);
9421 // Mixin constructors
9422 OO
.ui
.ButtonElement
.call( this, config
);
9423 OO
.ui
.IconElement
.call( this, config
);
9424 OO
.ui
.IndicatorElement
.call( this, config
);
9425 OO
.ui
.LabelElement
.call( this, config
);
9426 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
9427 OO
.ui
.FlaggedElement
.call( this, config
);
9432 this.isHyperlink
= false;
9436 click
: this.onClick
.bind( this ),
9437 keypress
: this.onKeyPress
.bind( this )
9441 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
9443 .addClass( 'oo-ui-buttonWidget' )
9444 .append( this.$button
);
9445 this.setHref( config
.href
);
9446 this.setTarget( config
.target
);
9451 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
9452 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
9453 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
9454 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
9455 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
9456 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
9457 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
9468 * Handles mouse click events.
9470 * @param {jQuery.Event} e Mouse click event
9473 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
9474 if ( !this.isDisabled() ) {
9475 this.emit( 'click' );
9476 if ( this.isHyperlink
) {
9484 * Handles keypress events.
9486 * @param {jQuery.Event} e Keypress event
9489 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
9490 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9491 this.emit( 'click' );
9492 if ( this.isHyperlink
) {
9500 * Get hyperlink location.
9502 * @return {string} Hyperlink location
9504 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
9509 * Get hyperlink target.
9511 * @return {string} Hyperlink target
9513 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
9518 * Set hyperlink location.
9520 * @param {string|null} href Hyperlink location, null to remove
9522 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
9523 href
= typeof href
=== 'string' ? href
: null;
9525 if ( href
!== this.href
) {
9527 if ( href
!== null ) {
9528 this.$button
.attr( 'href', href
);
9529 this.isHyperlink
= true;
9531 this.$button
.removeAttr( 'href' );
9532 this.isHyperlink
= false;
9540 * Set hyperlink target.
9542 * @param {string|null} target Hyperlink target, null to remove
9544 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
9545 target
= typeof target
=== 'string' ? target
: null;
9547 if ( target
!== this.target
) {
9548 this.target
= target
;
9549 if ( target
!== null ) {
9550 this.$button
.attr( 'target', target
);
9552 this.$button
.removeAttr( 'target' );
9560 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
9563 * @extends OO.ui.ButtonWidget
9564 * @mixins OO.ui.PendingElement
9567 * @param {Object} [config] Configuration options
9568 * @cfg {string} [action] Symbolic action name
9569 * @cfg {string[]} [modes] Symbolic mode names
9570 * @cfg {boolean} [framed=false] Render button with a frame
9572 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
9573 // Configuration initialization
9574 config
= $.extend( { framed
: false }, config
);
9576 // Parent constructor
9577 OO
.ui
.ActionWidget
.super.call( this, config
);
9579 // Mixin constructors
9580 OO
.ui
.PendingElement
.call( this, config
);
9583 this.action
= config
.action
|| '';
9584 this.modes
= config
.modes
|| [];
9589 this.$element
.addClass( 'oo-ui-actionWidget' );
9594 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
9595 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
9606 * Check if action is available in a certain mode.
9608 * @param {string} mode Name of mode
9609 * @return {boolean} Has mode
9611 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
9612 return this.modes
.indexOf( mode
) !== -1;
9616 * Get symbolic action name.
9620 OO
.ui
.ActionWidget
.prototype.getAction = function () {
9625 * Get symbolic action name.
9629 OO
.ui
.ActionWidget
.prototype.getModes = function () {
9630 return this.modes
.slice();
9634 * Emit a resize event if the size has changed.
9638 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
9641 if ( this.isElementAttached() ) {
9642 width
= this.$element
.width();
9643 height
= this.$element
.height();
9645 if ( width
!== this.width
|| height
!== this.height
) {
9647 this.height
= height
;
9648 this.emit( 'resize' );
9658 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
9660 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
9661 this.propagateResize();
9669 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
9671 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
9672 this.propagateResize();
9680 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
9682 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
9683 this.propagateResize();
9691 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
9693 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
9694 this.propagateResize();
9700 * Toggle visibility of button.
9702 * @param {boolean} [show] Show button, omit to toggle visibility
9705 OO
.ui
.ActionWidget
.prototype.toggle = function () {
9707 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
9708 this.propagateResize();
9714 * Button that shows and hides a popup.
9717 * @extends OO.ui.ButtonWidget
9718 * @mixins OO.ui.PopupElement
9721 * @param {Object} [config] Configuration options
9723 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
9724 // Parent constructor
9725 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
9727 // Mixin constructors
9728 OO
.ui
.PopupElement
.call( this, config
);
9732 .addClass( 'oo-ui-popupButtonWidget' )
9733 .append( this.popup
.$element
);
9738 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
9739 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
9744 * Handles mouse click events.
9746 * @param {jQuery.Event} e Mouse click event
9748 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
9749 // Skip clicks within the popup
9750 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
9754 if ( !this.isDisabled() ) {
9755 this.popup
.toggle();
9757 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
9763 * Button that toggles on and off.
9766 * @extends OO.ui.ButtonWidget
9767 * @mixins OO.ui.ToggleWidget
9770 * @param {Object} [config] Configuration options
9771 * @cfg {boolean} [value=false] Initial value
9773 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
9774 // Configuration initialization
9775 config
= config
|| {};
9777 // Parent constructor
9778 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
9780 // Mixin constructors
9781 OO
.ui
.ToggleWidget
.call( this, config
);
9784 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
9789 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
9790 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
9797 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
9798 if ( !this.isDisabled() ) {
9799 this.setValue( !this.value
);
9803 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
9809 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
9811 if ( value
!== this.value
) {
9812 this.setActive( value
);
9815 // Parent method (from mixin)
9816 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
9822 * Dropdown menu of options.
9824 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
9825 * can be accessed using the #getMenu method.
9827 * Use with OO.ui.MenuOptionWidget.
9830 * @extends OO.ui.Widget
9831 * @mixins OO.ui.IconElement
9832 * @mixins OO.ui.IndicatorElement
9833 * @mixins OO.ui.LabelElement
9834 * @mixins OO.ui.TitledElement
9837 * @param {Object} [config] Configuration options
9838 * @cfg {Object} [menu] Configuration options to pass to menu widget
9840 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
9841 // Configuration initialization
9842 config
= $.extend( { indicator
: 'down' }, config
);
9844 // Parent constructor
9845 OO
.ui
.DropdownWidget
.super.call( this, config
);
9847 // Mixin constructors
9848 OO
.ui
.IconElement
.call( this, config
);
9849 OO
.ui
.IndicatorElement
.call( this, config
);
9850 OO
.ui
.LabelElement
.call( this, config
);
9851 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
9854 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
9855 this.$handle
= this.$( '<span>' );
9858 this.$element
.on( { click
: this.onClick
.bind( this ) } );
9859 this.menu
.connect( this, { select
: 'onMenuSelect' } );
9863 .addClass( 'oo-ui-dropdownWidget-handle' )
9864 .append( this.$icon
, this.$label
, this.$indicator
);
9866 .addClass( 'oo-ui-dropdownWidget' )
9867 .append( this.$handle
, this.menu
.$element
);
9872 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
9873 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
9874 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
9875 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
9876 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
9883 * @return {OO.ui.MenuSelectWidget} Menu of widget
9885 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
9890 * Handles menu select events.
9892 * @param {OO.ui.MenuOptionWidget} item Selected menu item
9894 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
9901 selectedLabel
= item
.getLabel();
9903 // If the label is a DOM element, clone it, because setLabel will append() it
9904 if ( selectedLabel
instanceof jQuery
) {
9905 selectedLabel
= selectedLabel
.clone();
9908 this.setLabel( selectedLabel
);
9912 * Handles mouse click events.
9914 * @param {jQuery.Event} e Mouse click event
9916 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
9917 // Skip clicks within the menu
9918 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
9922 if ( !this.isDisabled() ) {
9923 if ( this.menu
.isVisible() ) {
9924 this.menu
.toggle( false );
9926 this.menu
.toggle( true );
9935 * See OO.ui.IconElement for more information.
9938 * @extends OO.ui.Widget
9939 * @mixins OO.ui.IconElement
9940 * @mixins OO.ui.TitledElement
9943 * @param {Object} [config] Configuration options
9945 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
9946 // Configuration initialization
9947 config
= config
|| {};
9949 // Parent constructor
9950 OO
.ui
.IconWidget
.super.call( this, config
);
9952 // Mixin constructors
9953 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
9954 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9957 this.$element
.addClass( 'oo-ui-iconWidget' );
9962 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
9963 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
9964 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
9966 /* Static Properties */
9968 OO
.ui
.IconWidget
.static.tagName
= 'span';
9973 * See OO.ui.IndicatorElement for more information.
9976 * @extends OO.ui.Widget
9977 * @mixins OO.ui.IndicatorElement
9978 * @mixins OO.ui.TitledElement
9981 * @param {Object} [config] Configuration options
9983 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
9984 // Configuration initialization
9985 config
= config
|| {};
9987 // Parent constructor
9988 OO
.ui
.IndicatorWidget
.super.call( this, config
);
9990 // Mixin constructors
9991 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
9992 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9995 this.$element
.addClass( 'oo-ui-indicatorWidget' );
10000 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
10001 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
10002 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
10004 /* Static Properties */
10006 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
10009 * Base class for input widgets.
10013 * @extends OO.ui.Widget
10014 * @mixins OO.ui.FlaggedElement
10017 * @param {Object} [config] Configuration options
10018 * @cfg {string} [name=''] HTML input name
10019 * @cfg {string} [value=''] Input value
10020 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
10022 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
10023 // Configuration initialization
10024 config
= config
|| {};
10026 // Parent constructor
10027 OO
.ui
.InputWidget
.super.call( this, config
);
10029 // Mixin constructors
10030 OO
.ui
.FlaggedElement
.call( this, config
);
10033 this.$input
= this.getInputElement( config
);
10035 this.inputFilter
= config
.inputFilter
;
10038 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
10042 .attr( 'name', config
.name
)
10043 .prop( 'disabled', this.isDisabled() );
10044 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
10045 this.setValue( config
.value
);
10050 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
10051 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
10057 * @param {string} value
10063 * Get input element.
10066 * @param {Object} [config] Configuration options
10067 * @return {jQuery} Input element
10069 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
10070 return this.$( '<input>' );
10074 * Handle potentially value-changing events.
10076 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
10078 OO
.ui
.InputWidget
.prototype.onEdit = function () {
10080 if ( !this.isDisabled() ) {
10081 // Allow the stack to clear so the value will be updated
10082 setTimeout( function () {
10083 widget
.setValue( widget
.$input
.val() );
10089 * Get the value of the input.
10091 * @return {string} Input value
10093 OO
.ui
.InputWidget
.prototype.getValue = function () {
10098 * Sets the direction of the current input, either RTL or LTR
10100 * @param {boolean} isRTL
10102 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
10104 this.$input
.removeClass( 'oo-ui-ltr' );
10105 this.$input
.addClass( 'oo-ui-rtl' );
10107 this.$input
.removeClass( 'oo-ui-rtl' );
10108 this.$input
.addClass( 'oo-ui-ltr' );
10113 * Set the value of the input.
10115 * @param {string} value New value
10119 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
10120 value
= this.cleanUpValue( value
);
10121 // Update the DOM if it has changed. Note that with cleanUpValue, it
10122 // is possible for the DOM value to change without this.value changing.
10123 if ( this.$input
.val() !== value
) {
10124 this.$input
.val( value
);
10126 if ( this.value
!== value
) {
10127 this.value
= value
;
10128 this.emit( 'change', this.value
);
10134 * Clean up incoming value.
10136 * Ensures value is a string, and converts undefined and null to empty string.
10139 * @param {string} value Original value
10140 * @return {string} Cleaned up value
10142 OO
.ui
.InputWidget
.prototype.cleanUpValue = function ( value
) {
10143 if ( value
=== undefined || value
=== null ) {
10145 } else if ( this.inputFilter
) {
10146 return this.inputFilter( String( value
) );
10148 return String( value
);
10153 * Simulate the behavior of clicking on a label bound to this input.
10155 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
10156 if ( !this.isDisabled() ) {
10157 if ( this.$input
.is( ':checkbox,:radio' ) ) {
10158 this.$input
.click();
10159 } else if ( this.$input
.is( ':input' ) ) {
10160 this.$input
[0].focus();
10168 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
10169 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
10170 if ( this.$input
) {
10171 this.$input
.prop( 'disabled', this.isDisabled() );
10181 OO
.ui
.InputWidget
.prototype.focus = function () {
10182 this.$input
[0].focus();
10191 OO
.ui
.InputWidget
.prototype.blur = function () {
10192 this.$input
[0].blur();
10197 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
10200 * @extends OO.ui.InputWidget
10201 * @mixins OO.ui.ButtonElement
10202 * @mixins OO.ui.IconElement
10203 * @mixins OO.ui.IndicatorElement
10204 * @mixins OO.ui.LabelElement
10205 * @mixins OO.ui.TitledElement
10206 * @mixins OO.ui.FlaggedElement
10209 * @param {Object} [config] Configuration options
10210 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
10211 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
10212 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
10213 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
10214 * won't be possible to set a value (which will internally become identical to the label).
10216 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
10217 // Configuration initialization
10218 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
10220 // Properties (must be set before parent constructor, which calls #setValue)
10221 this.useInputTag
= config
.useInputTag
;
10223 // Parent constructor
10224 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
10226 // Mixin constructors
10227 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
10228 OO
.ui
.IconElement
.call( this, config
);
10229 OO
.ui
.IndicatorElement
.call( this, config
);
10230 OO
.ui
.LabelElement
.call( this, config
);
10231 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
10232 OO
.ui
.FlaggedElement
.call( this, config
);
10236 click
: this.onClick
.bind( this ),
10237 keypress
: this.onKeyPress
.bind( this )
10241 if ( !config
.useInputTag
) {
10242 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
10244 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
10249 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
10250 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
10251 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
10252 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
10253 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
10254 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
10255 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
10266 * Get input element.
10269 * @param {Object} [config] Configuration options
10270 * @return {jQuery} Input element
10272 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
10273 // Configuration initialization
10274 config
= config
|| {};
10276 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
10278 return this.$( html
);
10284 * Overridden to support setting the 'value' of `<input/>` elements.
10286 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
10287 * text; or null for no label
10290 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
10291 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
10293 if ( this.useInputTag
) {
10294 if ( typeof label
=== 'function' ) {
10295 label
= OO
.ui
.resolveMsg( label
);
10297 if ( label
instanceof jQuery
) {
10298 label
= label
.text();
10303 this.$input
.val( label
);
10310 * Set the value of the input.
10312 * Overridden to disable for `<input/>` elements, which have value identical to the label.
10314 * @param {string} value New value
10317 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
10318 if ( !this.useInputTag
) {
10319 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
10325 * Handles mouse click events.
10327 * @param {jQuery.Event} e Mouse click event
10330 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
10331 if ( !this.isDisabled() ) {
10332 this.emit( 'click' );
10338 * Handles keypress events.
10340 * @param {jQuery.Event} e Keypress event
10343 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
10344 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
10345 this.emit( 'click' );
10351 * Checkbox input widget.
10354 * @extends OO.ui.InputWidget
10357 * @param {Object} [config] Configuration options
10358 * @cfg {boolean} [selected=false] Whether the checkbox is initially selected
10360 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
10361 // Parent constructor
10362 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
10365 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
10366 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
10371 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
10376 * Get input element.
10379 * @return {jQuery} Input element
10381 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
10382 return this.$( '<input type="checkbox" />' );
10388 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
10390 if ( !this.isDisabled() ) {
10391 // Allow the stack to clear so the value will be updated
10392 setTimeout( function () {
10393 widget
.setSelected( widget
.$input
.prop( 'checked' ) );
10399 * Set selection state of this checkbox.
10401 * @param {boolean} state Whether the checkbox is selected
10404 OO
.ui
.CheckboxInputWidget
.prototype.setSelected = function ( state
) {
10406 if ( this.selected
!== state
) {
10407 this.selected
= state
;
10408 this.$input
.prop( 'checked', this.selected
);
10409 this.emit( 'change', this.selected
);
10415 * Check if this checkbox is selected.
10417 * @return {boolean} Checkbox is selected
10419 OO
.ui
.CheckboxInputWidget
.prototype.isSelected = function () {
10420 return this.selected
;
10424 * Radio input widget.
10426 * Radio buttons only make sense as a set, and you probably want to use the OO.ui.RadioSelectWidget
10427 * class instead of using this class directly.
10430 * @extends OO.ui.InputWidget
10433 * @param {Object} [config] Configuration options
10434 * @cfg {boolean} [selected=false] Whether the radio button is initially selected
10436 OO
.ui
.RadioInputWidget
= function OoUiRadioInputWidget( config
) {
10437 // Parent constructor
10438 OO
.ui
.RadioInputWidget
.super.call( this, config
);
10441 this.$element
.addClass( 'oo-ui-radioInputWidget' );
10442 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
10447 OO
.inheritClass( OO
.ui
.RadioInputWidget
, OO
.ui
.InputWidget
);
10452 * Get input element.
10455 * @return {jQuery} Input element
10457 OO
.ui
.RadioInputWidget
.prototype.getInputElement = function () {
10458 return this.$( '<input type="radio" />' );
10464 OO
.ui
.RadioInputWidget
.prototype.onEdit = function () {
10465 // RadioInputWidget doesn't track its state.
10469 * Set selection state of this radio button.
10471 * @param {boolean} state Whether the button is selected
10474 OO
.ui
.RadioInputWidget
.prototype.setSelected = function ( state
) {
10475 // RadioInputWidget doesn't track its state.
10476 this.$input
.prop( 'checked', state
);
10481 * Check if this radio button is selected.
10483 * @return {boolean} Radio is selected
10485 OO
.ui
.RadioInputWidget
.prototype.isSelected = function () {
10486 return this.$input
.prop( 'checked' );
10490 * Input widget with a text field.
10493 * @extends OO.ui.InputWidget
10494 * @mixins OO.ui.IconElement
10495 * @mixins OO.ui.IndicatorElement
10496 * @mixins OO.ui.PendingElement
10499 * @param {Object} [config] Configuration options
10500 * @cfg {string} [type='text'] HTML tag `type` attribute
10501 * @cfg {string} [placeholder] Placeholder text
10502 * @cfg {boolean} [autofocus=false] Ask the browser to focus this widget, using the 'autofocus' HTML
10504 * @cfg {boolean} [readOnly=false] Prevent changes
10505 * @cfg {boolean} [multiline=false] Allow multiple lines of text
10506 * @cfg {boolean} [autosize=false] Automatically resize to fit content
10507 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
10508 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
10509 * one, see #static-validationPatterns)
10511 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
10512 // Configuration initialization
10513 config
= $.extend( { readOnly
: false }, config
);
10515 // Parent constructor
10516 OO
.ui
.TextInputWidget
.super.call( this, config
);
10518 // Mixin constructors
10519 OO
.ui
.IconElement
.call( this, config
);
10520 OO
.ui
.IndicatorElement
.call( this, config
);
10521 OO
.ui
.PendingElement
.call( this, config
);
10524 this.readOnly
= false;
10525 this.multiline
= !!config
.multiline
;
10526 this.autosize
= !!config
.autosize
;
10527 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
10528 this.validate
= null;
10530 // Clone for resizing
10531 if ( this.autosize
) {
10532 this.$clone
= this.$input
10534 .insertAfter( this.$input
)
10538 this.setValidation( config
.validate
);
10542 keypress
: this.onKeyPress
.bind( this ),
10543 blur
: this.setValidityFlag
.bind( this )
10545 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
10546 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
10547 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
10551 .addClass( 'oo-ui-textInputWidget' )
10552 .append( this.$icon
, this.$indicator
);
10553 this.setReadOnly( config
.readOnly
);
10554 if ( config
.placeholder
) {
10555 this.$input
.attr( 'placeholder', config
.placeholder
);
10557 if ( config
.autofocus
) {
10558 this.$input
.attr( 'autofocus', 'autofocus' );
10560 this.$element
.attr( 'role', 'textbox' );
10565 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
10566 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
10567 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
10568 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
10570 /* Static properties */
10572 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
10580 * User presses enter inside the text box.
10582 * Not called if input is multiline.
10588 * User clicks the icon.
10594 * User clicks the indicator.
10602 * Handle icon mouse down events.
10604 * @param {jQuery.Event} e Mouse down event
10607 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
10608 if ( e
.which
=== 1 ) {
10609 this.$input
[0].focus();
10610 this.emit( 'icon' );
10616 * Handle indicator mouse down events.
10618 * @param {jQuery.Event} e Mouse down event
10621 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
10622 if ( e
.which
=== 1 ) {
10623 this.$input
[0].focus();
10624 this.emit( 'indicator' );
10630 * Handle key press events.
10632 * @param {jQuery.Event} e Key press event
10633 * @fires enter If enter key is pressed and input is not multiline
10635 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
10636 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
10637 this.emit( 'enter', e
);
10642 * Handle element attach events.
10644 * @param {jQuery.Event} e Element attach event
10646 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
10653 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
10657 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
10663 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
10665 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
10667 this.setValidityFlag();
10673 * Check if the widget is read-only.
10675 * @return {boolean}
10677 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
10678 return this.readOnly
;
10682 * Set the read-only state of the widget.
10684 * This should probably change the widget's appearance and prevent it from being used.
10686 * @param {boolean} state Make input read-only
10689 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
10690 this.readOnly
= !!state
;
10691 this.$input
.prop( 'readOnly', this.readOnly
);
10696 * Automatically adjust the size of the text input.
10698 * This only affects multi-line inputs that are auto-sized.
10702 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
10703 var scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
10705 if ( this.multiline
&& this.autosize
&& this.$input
.val() !== this.valCache
) {
10707 .val( this.$input
.val() )
10708 .attr( 'rows', '' )
10709 // Set inline height property to 0 to measure scroll height
10710 .css( 'height', 0 );
10712 this.$clone
[0].style
.display
= 'block';
10714 this.valCache
= this.$input
.val();
10716 scrollHeight
= this.$clone
[0].scrollHeight
;
10718 // Remove inline height property to measure natural heights
10719 this.$clone
.css( 'height', '' );
10720 innerHeight
= this.$clone
.innerHeight();
10721 outerHeight
= this.$clone
.outerHeight();
10723 // Measure max rows height
10725 .attr( 'rows', this.maxRows
)
10726 .css( 'height', 'auto' )
10728 maxInnerHeight
= this.$clone
.innerHeight();
10730 // Difference between reported innerHeight and scrollHeight with no scrollbars present
10731 // Equals 1 on Blink-based browsers and 0 everywhere else
10732 measurementError
= maxInnerHeight
- this.$clone
[0].scrollHeight
;
10733 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
10735 this.$clone
[0].style
.display
= 'none';
10737 // Only apply inline height when expansion beyond natural height is needed
10738 if ( idealHeight
> innerHeight
) {
10739 // Use the difference between the inner and outer height as a buffer
10740 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
10742 this.$input
.css( 'height', '' );
10749 * Get input element.
10752 * @param {Object} [config] Configuration options
10753 * @return {jQuery} Input element
10755 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
10756 // Configuration initialization
10757 config
= config
|| {};
10759 var type
= config
.type
|| 'text';
10761 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + type
+ '" />' );
10765 * Check if input supports multiple lines.
10767 * @return {boolean}
10769 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
10770 return !!this.multiline
;
10774 * Check if input automatically adjusts its size.
10776 * @return {boolean}
10778 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
10779 return !!this.autosize
;
10783 * Select the contents of the input.
10787 OO
.ui
.TextInputWidget
.prototype.select = function () {
10788 this.$input
.select();
10793 * Sets the validation pattern to use.
10794 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
10795 * one, see #static-validationPatterns)
10797 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
10798 if ( validate
instanceof RegExp
) {
10799 this.validate
= validate
;
10801 this.validate
= this.constructor.static.validationPatterns
[validate
] || /.*/;
10806 * Sets the 'invalid' flag appropriately.
10808 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
10810 this.isValid().done( function ( valid
) {
10811 widget
.setFlags( { invalid
: !valid
} );
10816 * Returns whether or not the current value is considered valid, according to the
10817 * supplied validation pattern.
10819 * @return {jQuery.Deferred}
10821 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
10822 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
10826 * Text input with a menu of optional values.
10829 * @extends OO.ui.Widget
10832 * @param {Object} [config] Configuration options
10833 * @cfg {Object} [menu] Configuration options to pass to menu widget
10834 * @cfg {Object} [input] Configuration options to pass to input widget
10835 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
10837 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
10838 // Configuration initialization
10839 config
= config
|| {};
10841 // Parent constructor
10842 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
10845 this.$overlay
= config
.$overlay
|| this.$element
;
10846 this.input
= new OO
.ui
.TextInputWidget( $.extend(
10847 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
10850 this.menu
= new OO
.ui
.TextInputMenuSelectWidget( this.input
, $.extend(
10852 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
10855 disabled
: this.isDisabled()
10861 this.input
.connect( this, {
10862 change
: 'onInputChange',
10863 indicator
: 'onInputIndicator',
10864 enter
: 'onInputEnter'
10866 this.menu
.connect( this, {
10867 choose
: 'onMenuChoose',
10868 add
: 'onMenuItemsChange',
10869 remove
: 'onMenuItemsChange'
10873 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
10874 this.$overlay
.append( this.menu
.$element
);
10875 this.onMenuItemsChange();
10880 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
10885 * Get the combobox's menu.
10886 * @return {OO.ui.TextInputMenuSelectWidget} Menu widget
10888 OO
.ui
.ComboBoxWidget
.prototype.getMenu = function () {
10893 * Handle input change events.
10895 * @param {string} value New value
10897 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
10898 var match
= this.menu
.getItemFromData( value
);
10900 this.menu
.selectItem( match
);
10902 if ( !this.isDisabled() ) {
10903 this.menu
.toggle( true );
10908 * Handle input indicator events.
10910 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
10911 if ( !this.isDisabled() ) {
10912 this.menu
.toggle();
10917 * Handle input enter events.
10919 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
10920 if ( !this.isDisabled() ) {
10921 this.menu
.toggle( false );
10926 * Handle menu choose events.
10928 * @param {OO.ui.OptionWidget} item Chosen item
10930 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
10932 this.input
.setValue( item
.getData() );
10937 * Handle menu item change events.
10939 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
10940 var match
= this.menu
.getItemFromData( this.input
.getValue() );
10941 this.menu
.selectItem( match
);
10942 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
10948 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
10950 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
10952 if ( this.input
) {
10953 this.input
.setDisabled( this.isDisabled() );
10956 this.menu
.setDisabled( this.isDisabled() );
10966 * @extends OO.ui.Widget
10967 * @mixins OO.ui.LabelElement
10970 * @param {Object} [config] Configuration options
10971 * @cfg {OO.ui.InputWidget} [input] Input widget this label is for
10973 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
10974 // Configuration initialization
10975 config
= config
|| {};
10977 // Parent constructor
10978 OO
.ui
.LabelWidget
.super.call( this, config
);
10980 // Mixin constructors
10981 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
10982 OO
.ui
.TitledElement
.call( this, config
);
10985 this.input
= config
.input
;
10988 if ( this.input
instanceof OO
.ui
.InputWidget
) {
10989 this.$element
.on( 'click', this.onClick
.bind( this ) );
10993 this.$element
.addClass( 'oo-ui-labelWidget' );
10998 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
10999 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
11000 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
11002 /* Static Properties */
11004 OO
.ui
.LabelWidget
.static.tagName
= 'span';
11009 * Handles label mouse click events.
11011 * @param {jQuery.Event} e Mouse click event
11013 OO
.ui
.LabelWidget
.prototype.onClick = function () {
11014 this.input
.simulateLabelClick();
11019 * Generic option widget for use with OO.ui.SelectWidget.
11022 * @extends OO.ui.Widget
11023 * @mixins OO.ui.LabelElement
11024 * @mixins OO.ui.FlaggedElement
11027 * @param {Object} [config] Configuration options
11029 OO
.ui
.OptionWidget
= function OoUiOptionWidget( config
) {
11030 // Configuration initialization
11031 config
= config
|| {};
11033 // Parent constructor
11034 OO
.ui
.OptionWidget
.super.call( this, config
);
11036 // Mixin constructors
11037 OO
.ui
.ItemWidget
.call( this );
11038 OO
.ui
.LabelElement
.call( this, config
);
11039 OO
.ui
.FlaggedElement
.call( this, config
);
11042 this.selected
= false;
11043 this.highlighted
= false;
11044 this.pressed
= false;
11048 .data( 'oo-ui-optionWidget', this )
11049 .attr( 'role', 'option' )
11050 .addClass( 'oo-ui-optionWidget' )
11051 .append( this.$label
);
11056 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
11057 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
11058 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
11059 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
11061 /* Static Properties */
11063 OO
.ui
.OptionWidget
.static.selectable
= true;
11065 OO
.ui
.OptionWidget
.static.highlightable
= true;
11067 OO
.ui
.OptionWidget
.static.pressable
= true;
11069 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
11074 * Check if option can be selected.
11076 * @return {boolean} Item is selectable
11078 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
11079 return this.constructor.static.selectable
&& !this.isDisabled();
11083 * Check if option can be highlighted.
11085 * @return {boolean} Item is highlightable
11087 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
11088 return this.constructor.static.highlightable
&& !this.isDisabled();
11092 * Check if option can be pressed.
11094 * @return {boolean} Item is pressable
11096 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
11097 return this.constructor.static.pressable
&& !this.isDisabled();
11101 * Check if option is selected.
11103 * @return {boolean} Item is selected
11105 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
11106 return this.selected
;
11110 * Check if option is highlighted.
11112 * @return {boolean} Item is highlighted
11114 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
11115 return this.highlighted
;
11119 * Check if option is pressed.
11121 * @return {boolean} Item is pressed
11123 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
11124 return this.pressed
;
11128 * Set selected state.
11130 * @param {boolean} [state=false] Select option
11133 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
11134 if ( this.constructor.static.selectable
) {
11135 this.selected
= !!state
;
11136 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
11137 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
11138 this.scrollElementIntoView();
11140 this.updateThemeClasses();
11146 * Set highlighted state.
11148 * @param {boolean} [state=false] Highlight option
11151 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
11152 if ( this.constructor.static.highlightable
) {
11153 this.highlighted
= !!state
;
11154 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
11155 this.updateThemeClasses();
11161 * Set pressed state.
11163 * @param {boolean} [state=false] Press option
11166 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
11167 if ( this.constructor.static.pressable
) {
11168 this.pressed
= !!state
;
11169 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
11170 this.updateThemeClasses();
11176 * Make the option's highlight flash.
11178 * While flashing, the visual style of the pressed state is removed if present.
11180 * @return {jQuery.Promise} Promise resolved when flashing is done
11182 OO
.ui
.OptionWidget
.prototype.flash = function () {
11184 $element
= this.$element
,
11185 deferred
= $.Deferred();
11187 if ( !this.isDisabled() && this.constructor.static.pressable
) {
11188 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
11189 setTimeout( function () {
11190 // Restore original classes
11192 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
11193 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
11195 setTimeout( function () {
11196 deferred
.resolve();
11202 return deferred
.promise();
11206 * Option widget with an option icon and indicator.
11208 * Use together with OO.ui.SelectWidget.
11211 * @extends OO.ui.OptionWidget
11212 * @mixins OO.ui.IconElement
11213 * @mixins OO.ui.IndicatorElement
11216 * @param {Object} [config] Configuration options
11218 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( config
) {
11219 // Parent constructor
11220 OO
.ui
.DecoratedOptionWidget
.super.call( this, config
);
11222 // Mixin constructors
11223 OO
.ui
.IconElement
.call( this, config
);
11224 OO
.ui
.IndicatorElement
.call( this, config
);
11228 .addClass( 'oo-ui-decoratedOptionWidget' )
11229 .prepend( this.$icon
)
11230 .append( this.$indicator
);
11235 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
11236 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
11237 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
11240 * Option widget that looks like a button.
11242 * Use together with OO.ui.ButtonSelectWidget.
11245 * @extends OO.ui.DecoratedOptionWidget
11246 * @mixins OO.ui.ButtonElement
11249 * @param {Object} [config] Configuration options
11251 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
11252 // Parent constructor
11253 OO
.ui
.ButtonOptionWidget
.super.call( this, config
);
11255 // Mixin constructors
11256 OO
.ui
.ButtonElement
.call( this, config
);
11259 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
11260 this.$button
.append( this.$element
.contents() );
11261 this.$element
.append( this.$button
);
11266 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11267 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
11269 /* Static Properties */
11271 // Allow button mouse down events to pass through so they can be handled by the parent select widget
11272 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
11279 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
11280 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
11282 if ( this.constructor.static.selectable
) {
11283 this.setActive( state
);
11290 * Option widget that looks like a radio button.
11292 * Use together with OO.ui.RadioSelectWidget.
11295 * @extends OO.ui.OptionWidget
11298 * @param {Object} [config] Configuration options
11300 OO
.ui
.RadioOptionWidget
= function OoUiRadioOptionWidget( config
) {
11301 // Parent constructor
11302 OO
.ui
.RadioOptionWidget
.super.call( this, config
);
11305 this.radio
= new OO
.ui
.RadioInputWidget( { value
: config
.data
} );
11309 .addClass( 'oo-ui-radioOptionWidget' )
11310 .prepend( this.radio
.$element
);
11315 OO
.inheritClass( OO
.ui
.RadioOptionWidget
, OO
.ui
.OptionWidget
);
11317 /* Static Properties */
11319 OO
.ui
.RadioOptionWidget
.static.highlightable
= false;
11321 OO
.ui
.RadioOptionWidget
.static.pressable
= false;
11328 OO
.ui
.RadioOptionWidget
.prototype.setSelected = function ( state
) {
11329 OO
.ui
.RadioOptionWidget
.super.prototype.setSelected
.call( this, state
);
11331 this.radio
.setSelected( state
);
11337 * Item of an OO.ui.MenuSelectWidget.
11340 * @extends OO.ui.DecoratedOptionWidget
11343 * @param {Object} [config] Configuration options
11345 OO
.ui
.MenuOptionWidget
= function OoUiMenuOptionWidget( config
) {
11346 // Configuration initialization
11347 config
= $.extend( { icon
: 'check' }, config
);
11349 // Parent constructor
11350 OO
.ui
.MenuOptionWidget
.super.call( this, config
);
11354 .attr( 'role', 'menuitem' )
11355 .addClass( 'oo-ui-menuOptionWidget' );
11360 OO
.inheritClass( OO
.ui
.MenuOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11363 * Section to group one or more items in a OO.ui.MenuSelectWidget.
11366 * @extends OO.ui.DecoratedOptionWidget
11369 * @param {Object} [config] Configuration options
11371 OO
.ui
.MenuSectionOptionWidget
= function OoUiMenuSectionOptionWidget( config
) {
11372 // Parent constructor
11373 OO
.ui
.MenuSectionOptionWidget
.super.call( this, config
);
11376 this.$element
.addClass( 'oo-ui-menuSectionOptionWidget' );
11381 OO
.inheritClass( OO
.ui
.MenuSectionOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11383 /* Static Properties */
11385 OO
.ui
.MenuSectionOptionWidget
.static.selectable
= false;
11387 OO
.ui
.MenuSectionOptionWidget
.static.highlightable
= false;
11390 * Items for an OO.ui.OutlineSelectWidget.
11393 * @extends OO.ui.DecoratedOptionWidget
11396 * @param {Object} [config] Configuration options
11397 * @cfg {number} [level] Indentation level
11398 * @cfg {boolean} [movable] Allow modification from outline controls
11400 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
11401 // Configuration initialization
11402 config
= config
|| {};
11404 // Parent constructor
11405 OO
.ui
.OutlineOptionWidget
.super.call( this, config
);
11409 this.movable
= !!config
.movable
;
11410 this.removable
= !!config
.removable
;
11413 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
11414 this.setLevel( config
.level
);
11419 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11421 /* Static Properties */
11423 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
11425 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
11427 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
11429 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
11434 * Check if item is movable.
11436 * Movability is used by outline controls.
11438 * @return {boolean} Item is movable
11440 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
11441 return this.movable
;
11445 * Check if item is removable.
11447 * Removability is used by outline controls.
11449 * @return {boolean} Item is removable
11451 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
11452 return this.removable
;
11456 * Get indentation level.
11458 * @return {number} Indentation level
11460 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
11467 * Movability is used by outline controls.
11469 * @param {boolean} movable Item is movable
11472 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
11473 this.movable
= !!movable
;
11474 this.updateThemeClasses();
11479 * Set removability.
11481 * Removability is used by outline controls.
11483 * @param {boolean} movable Item is removable
11486 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
11487 this.removable
= !!removable
;
11488 this.updateThemeClasses();
11493 * Set indentation level.
11495 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
11498 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
11499 var levels
= this.constructor.static.levels
,
11500 levelClass
= this.constructor.static.levelClass
,
11503 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
11505 if ( this.level
=== i
) {
11506 this.$element
.addClass( levelClass
+ i
);
11508 this.$element
.removeClass( levelClass
+ i
);
11511 this.updateThemeClasses();
11517 * Container for content that is overlaid and positioned absolutely.
11520 * @extends OO.ui.Widget
11521 * @mixins OO.ui.LabelElement
11524 * @param {Object} [config] Configuration options
11525 * @cfg {number} [width=320] Width of popup in pixels
11526 * @cfg {number} [height] Height of popup, omit to use automatic height
11527 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
11528 * @cfg {string} [align='center'] Alignment of popup to origin
11529 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
11530 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
11531 * @cfg {jQuery} [$content] Content to append to the popup's body
11532 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
11533 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
11534 * @cfg {boolean} [head] Show label and close button at the top
11535 * @cfg {boolean} [padded] Add padding to the body
11537 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
11538 // Configuration initialization
11539 config
= config
|| {};
11541 // Parent constructor
11542 OO
.ui
.PopupWidget
.super.call( this, config
);
11544 // Mixin constructors
11545 OO
.ui
.LabelElement
.call( this, config
);
11546 OO
.ui
.ClippableElement
.call( this, config
);
11549 this.visible
= false;
11550 this.$popup
= this.$( '<div>' );
11551 this.$head
= this.$( '<div>' );
11552 this.$body
= this.$( '<div>' );
11553 this.$anchor
= this.$( '<div>' );
11554 // If undefined, will be computed lazily in updateDimensions()
11555 this.$container
= config
.$container
;
11556 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
11557 this.autoClose
= !!config
.autoClose
;
11558 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
11559 this.transitionTimeout
= null;
11560 this.anchor
= null;
11561 this.width
= config
.width
!== undefined ? config
.width
: 320;
11562 this.height
= config
.height
!== undefined ? config
.height
: null;
11563 this.align
= config
.align
|| 'center';
11564 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
11565 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
11568 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
11571 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
11572 this.$body
.addClass( 'oo-ui-popupWidget-body' );
11573 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
11575 .addClass( 'oo-ui-popupWidget-head' )
11576 .append( this.$label
, this.closeButton
.$element
);
11577 if ( !config
.head
) {
11581 .addClass( 'oo-ui-popupWidget-popup' )
11582 .append( this.$head
, this.$body
);
11585 .addClass( 'oo-ui-popupWidget' )
11586 .append( this.$popup
, this.$anchor
);
11587 // Move content, which was added to #$element by OO.ui.Widget, to the body
11588 if ( config
.$content
instanceof jQuery
) {
11589 this.$body
.append( config
.$content
);
11591 if ( config
.padded
) {
11592 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
11594 this.setClippableElement( this.$body
);
11599 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
11600 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
11601 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
11606 * Handles mouse down events.
11608 * @param {jQuery.Event} e Mouse down event
11610 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
11612 this.isVisible() &&
11613 !$.contains( this.$element
[0], e
.target
) &&
11614 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
11616 this.toggle( false );
11621 * Bind mouse down listener.
11623 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
11624 // Capture clicks outside popup
11625 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
11629 * Handles close button click events.
11631 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
11632 if ( this.isVisible() ) {
11633 this.toggle( false );
11638 * Unbind mouse down listener.
11640 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
11641 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
11645 * Set whether to show a anchor.
11647 * @param {boolean} [show] Show anchor, omit to toggle
11649 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
11650 show
= show
=== undefined ? !this.anchored
: !!show
;
11652 if ( this.anchored
!== show
) {
11654 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
11656 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
11658 this.anchored
= show
;
11663 * Check if showing a anchor.
11665 * @return {boolean} anchor is visible
11667 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
11668 return this.anchor
;
11674 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
11675 show
= show
=== undefined ? !this.isVisible() : !!show
;
11677 var change
= show
!== this.isVisible();
11680 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
11684 if ( this.autoClose
) {
11685 this.bindMouseDownListener();
11687 this.updateDimensions();
11688 this.toggleClipping( true );
11690 this.toggleClipping( false );
11691 if ( this.autoClose
) {
11692 this.unbindMouseDownListener();
11701 * Set the size of the popup.
11703 * Changing the size may also change the popup's position depending on the alignment.
11705 * @param {number} width Width
11706 * @param {number} height Height
11707 * @param {boolean} [transition=false] Use a smooth transition
11710 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
11711 this.width
= width
;
11712 this.height
= height
!== undefined ? height
: null;
11713 if ( this.isVisible() ) {
11714 this.updateDimensions( transition
);
11719 * Update the size and position.
11721 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
11722 * be called automatically.
11724 * @param {boolean} [transition=false] Use a smooth transition
11727 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
11728 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
11729 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
11732 if ( !this.$container
) {
11733 // Lazy-initialize $container if not specified in constructor
11734 this.$container
= this.$( this.getClosestScrollableElementContainer() );
11737 // Set height and width before measuring things, since it might cause our measurements
11738 // to change (e.g. due to scrollbars appearing or disappearing)
11741 height
: this.height
!== null ? this.height
: 'auto'
11744 // Compute initial popupOffset based on alignment
11745 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
11747 // Figure out if this will cause the popup to go beyond the edge of the container
11748 originOffset
= this.$element
.offset().left
;
11749 containerLeft
= this.$container
.offset().left
;
11750 containerWidth
= this.$container
.innerWidth();
11751 containerRight
= containerLeft
+ containerWidth
;
11752 popupLeft
= popupOffset
- this.containerPadding
;
11753 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
11754 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
11755 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
11757 // Adjust offset to make the popup not go beyond the edge, if needed
11758 if ( overlapRight
< 0 ) {
11759 popupOffset
+= overlapRight
;
11760 } else if ( overlapLeft
< 0 ) {
11761 popupOffset
-= overlapLeft
;
11764 // Adjust offset to avoid anchor being rendered too close to the edge
11765 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
11766 // TODO: Find a measurement that works for CSS anchors and image anchors
11767 anchorWidth
= this.$anchor
[0].scrollWidth
* 2;
11768 if ( popupOffset
+ this.width
< anchorWidth
) {
11769 popupOffset
= anchorWidth
- this.width
;
11770 } else if ( -popupOffset
< anchorWidth
) {
11771 popupOffset
= -anchorWidth
;
11774 // Prevent transition from being interrupted
11775 clearTimeout( this.transitionTimeout
);
11776 if ( transition
) {
11777 // Enable transition
11778 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
11781 // Position body relative to anchor
11782 this.$popup
.css( 'margin-left', popupOffset
);
11784 if ( transition
) {
11785 // Prevent transitioning after transition is complete
11786 this.transitionTimeout
= setTimeout( function () {
11787 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
11790 // Prevent transitioning immediately
11791 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
11794 // Reevaluate clipping state since we've relocated and resized the popup
11801 * Progress bar widget.
11804 * @extends OO.ui.Widget
11807 * @param {Object} [config] Configuration options
11808 * @cfg {number|boolean} [progress=false] Initial progress percent or false for indeterminate
11810 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
11811 // Configuration initialization
11812 config
= config
|| {};
11814 // Parent constructor
11815 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
11818 this.$bar
= this.$( '<div>' );
11819 this.progress
= null;
11822 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
11823 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar' );
11826 role
: 'progressbar',
11827 'aria-valuemin': 0,
11828 'aria-valuemax': 100
11830 .addClass( 'oo-ui-progressBarWidget' )
11831 .append( this.$bar
);
11836 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
11838 /* Static Properties */
11840 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
11845 * Get progress percent
11847 * @return {number} Progress percent
11849 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
11850 return this.progress
;
11854 * Set progress percent
11856 * @param {number|boolean} progress Progress percent or false for indeterminate
11858 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
11859 this.progress
= progress
;
11861 if ( progress
!== false ) {
11862 this.$bar
.css( 'width', this.progress
+ '%' );
11863 this.$element
.attr( 'aria-valuenow', this.progress
);
11865 this.$bar
.css( 'width', '' );
11866 this.$element
.removeAttr( 'aria-valuenow' );
11868 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
11874 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
11875 * Results are cleared and populated each time the query is changed.
11878 * @extends OO.ui.Widget
11881 * @param {Object} [config] Configuration options
11882 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
11883 * @cfg {string} [value] Initial query value
11885 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
11886 // Configuration initialization
11887 config
= config
|| {};
11889 // Parent constructor
11890 OO
.ui
.SearchWidget
.super.call( this, config
);
11893 this.query
= new OO
.ui
.TextInputWidget( {
11896 placeholder
: config
.placeholder
,
11897 value
: config
.value
11899 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
11900 this.$query
= this.$( '<div>' );
11901 this.$results
= this.$( '<div>' );
11904 this.query
.connect( this, {
11905 change
: 'onQueryChange',
11906 enter
: 'onQueryEnter'
11908 this.results
.connect( this, {
11909 highlight
: 'onResultsHighlight',
11910 select
: 'onResultsSelect'
11912 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
11916 .addClass( 'oo-ui-searchWidget-query' )
11917 .append( this.query
.$element
);
11919 .addClass( 'oo-ui-searchWidget-results' )
11920 .append( this.results
.$element
);
11922 .addClass( 'oo-ui-searchWidget' )
11923 .append( this.$results
, this.$query
);
11928 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
11934 * @param {Object|null} item Item data or null if no item is highlighted
11939 * @param {Object|null} item Item data or null if no item is selected
11945 * Handle query key down events.
11947 * @param {jQuery.Event} e Key down event
11949 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
11950 var highlightedItem
, nextItem
,
11951 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
11954 highlightedItem
= this.results
.getHighlightedItem();
11955 if ( !highlightedItem
) {
11956 highlightedItem
= this.results
.getSelectedItem();
11958 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
11959 this.results
.highlightItem( nextItem
);
11960 nextItem
.scrollElementIntoView();
11965 * Handle select widget select events.
11967 * Clears existing results. Subclasses should repopulate items according to new query.
11969 * @param {string} value New value
11971 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
11973 this.results
.clearItems();
11977 * Handle select widget enter key events.
11979 * Selects highlighted item.
11981 * @param {string} value New value
11983 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
11985 this.results
.selectItem( this.results
.getHighlightedItem() );
11989 * Handle select widget highlight events.
11991 * @param {OO.ui.OptionWidget} item Highlighted item
11994 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
11995 this.emit( 'highlight', item
? item
.getData() : null );
11999 * Handle select widget select events.
12001 * @param {OO.ui.OptionWidget} item Selected item
12004 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
12005 this.emit( 'select', item
? item
.getData() : null );
12009 * Get the query input.
12011 * @return {OO.ui.TextInputWidget} Query input
12013 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
12018 * Get the results list.
12020 * @return {OO.ui.SelectWidget} Select list
12022 OO
.ui
.SearchWidget
.prototype.getResults = function () {
12023 return this.results
;
12027 * Generic selection of options.
12029 * Items can contain any rendering. Any widget that provides options, from which the user must
12030 * choose one, should be built on this class.
12032 * Use together with OO.ui.OptionWidget.
12035 * @extends OO.ui.Widget
12036 * @mixins OO.ui.GroupElement
12039 * @param {Object} [config] Configuration options
12040 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
12042 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
12043 // Configuration initialization
12044 config
= config
|| {};
12046 // Parent constructor
12047 OO
.ui
.SelectWidget
.super.call( this, config
);
12049 // Mixin constructors
12050 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
12053 this.pressed
= false;
12054 this.selecting
= null;
12055 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
12056 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
12059 this.$element
.on( {
12060 mousedown
: this.onMouseDown
.bind( this ),
12061 mouseover
: this.onMouseOver
.bind( this ),
12062 mouseleave
: this.onMouseLeave
.bind( this )
12066 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
12067 if ( $.isArray( config
.items
) ) {
12068 this.addItems( config
.items
);
12074 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
12076 // Need to mixin base class as well
12077 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
12078 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
12084 * @param {OO.ui.OptionWidget|null} item Highlighted item
12089 * @param {OO.ui.OptionWidget|null} item Pressed item
12094 * @param {OO.ui.OptionWidget|null} item Selected item
12099 * @param {OO.ui.OptionWidget|null} item Chosen item
12104 * @param {OO.ui.OptionWidget[]} items Added items
12105 * @param {number} index Index items were added at
12110 * @param {OO.ui.OptionWidget[]} items Removed items
12116 * Handle mouse down events.
12119 * @param {jQuery.Event} e Mouse down event
12121 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
12124 if ( !this.isDisabled() && e
.which
=== 1 ) {
12125 this.togglePressed( true );
12126 item
= this.getTargetItem( e
);
12127 if ( item
&& item
.isSelectable() ) {
12128 this.pressItem( item
);
12129 this.selecting
= item
;
12130 this.getElementDocument().addEventListener(
12132 this.onMouseUpHandler
,
12135 this.getElementDocument().addEventListener(
12137 this.onMouseMoveHandler
,
12146 * Handle mouse up events.
12149 * @param {jQuery.Event} e Mouse up event
12151 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
12154 this.togglePressed( false );
12155 if ( !this.selecting
) {
12156 item
= this.getTargetItem( e
);
12157 if ( item
&& item
.isSelectable() ) {
12158 this.selecting
= item
;
12161 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
12162 this.pressItem( null );
12163 this.chooseItem( this.selecting
);
12164 this.selecting
= null;
12167 this.getElementDocument().removeEventListener(
12169 this.onMouseUpHandler
,
12172 this.getElementDocument().removeEventListener(
12174 this.onMouseMoveHandler
,
12182 * Handle mouse move events.
12185 * @param {jQuery.Event} e Mouse move event
12187 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
12190 if ( !this.isDisabled() && this.pressed
) {
12191 item
= this.getTargetItem( e
);
12192 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
12193 this.pressItem( item
);
12194 this.selecting
= item
;
12201 * Handle mouse over events.
12204 * @param {jQuery.Event} e Mouse over event
12206 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
12209 if ( !this.isDisabled() ) {
12210 item
= this.getTargetItem( e
);
12211 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
12217 * Handle mouse leave events.
12220 * @param {jQuery.Event} e Mouse over event
12222 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
12223 if ( !this.isDisabled() ) {
12224 this.highlightItem( null );
12230 * Get the closest item to a jQuery.Event.
12233 * @param {jQuery.Event} e
12234 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
12236 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
12237 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
12238 if ( $item
.length
) {
12239 return $item
.data( 'oo-ui-optionWidget' );
12245 * Get selected item.
12247 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
12249 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
12252 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12253 if ( this.items
[i
].isSelected() ) {
12254 return this.items
[i
];
12261 * Get highlighted item.
12263 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
12265 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
12268 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12269 if ( this.items
[i
].isHighlighted() ) {
12270 return this.items
[i
];
12277 * Toggle pressed state.
12279 * @param {boolean} pressed An option is being pressed
12281 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
12282 if ( pressed
=== undefined ) {
12283 pressed
= !this.pressed
;
12285 if ( pressed
!== this.pressed
) {
12287 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
12288 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
12289 this.pressed
= pressed
;
12294 * Highlight an item.
12296 * Highlighting is mutually exclusive.
12298 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
12302 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
12303 var i
, len
, highlighted
,
12306 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12307 highlighted
= this.items
[i
] === item
;
12308 if ( this.items
[i
].isHighlighted() !== highlighted
) {
12309 this.items
[i
].setHighlighted( highlighted
);
12314 this.emit( 'highlight', item
);
12323 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
12327 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
12328 var i
, len
, selected
,
12331 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12332 selected
= this.items
[i
] === item
;
12333 if ( this.items
[i
].isSelected() !== selected
) {
12334 this.items
[i
].setSelected( selected
);
12339 this.emit( 'select', item
);
12348 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
12352 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
12353 var i
, len
, pressed
,
12356 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12357 pressed
= this.items
[i
] === item
;
12358 if ( this.items
[i
].isPressed() !== pressed
) {
12359 this.items
[i
].setPressed( pressed
);
12364 this.emit( 'press', item
);
12373 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
12374 * an item is selected using the keyboard or mouse.
12376 * @param {OO.ui.OptionWidget} item Item to choose
12380 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
12381 this.selectItem( item
);
12382 this.emit( 'choose', item
);
12388 * Get an item relative to another one.
12390 * @param {OO.ui.OptionWidget|null} item Item to start at, null to get relative to list start
12391 * @param {number} direction Direction to move in, -1 to move backward, 1 to move forward
12392 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
12394 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
12395 var currentIndex
, nextIndex
, i
,
12396 increase
= direction
> 0 ? 1 : -1,
12397 len
= this.items
.length
;
12399 if ( item
instanceof OO
.ui
.OptionWidget
) {
12400 currentIndex
= $.inArray( item
, this.items
);
12401 nextIndex
= ( currentIndex
+ increase
+ len
) % len
;
12403 // If no item is selected and moving forward, start at the beginning.
12404 // If moving backward, start at the end.
12405 nextIndex
= direction
> 0 ? 0 : len
- 1;
12408 for ( i
= 0; i
< len
; i
++ ) {
12409 item
= this.items
[nextIndex
];
12410 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
12413 nextIndex
= ( nextIndex
+ increase
+ len
) % len
;
12419 * Get the next selectable item.
12421 * @return {OO.ui.OptionWidget|null} Item, `null` if there aren't any selectable items
12423 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
12426 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12427 item
= this.items
[i
];
12428 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
12439 * @param {OO.ui.OptionWidget[]} items Items to add
12440 * @param {number} [index] Index to insert items after
12444 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
12446 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
12448 // Always provide an index, even if it was omitted
12449 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
12457 * Items will be detached, not removed, so they can be used later.
12459 * @param {OO.ui.OptionWidget[]} items Items to remove
12463 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
12466 // Deselect items being removed
12467 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
12469 if ( item
.isSelected() ) {
12470 this.selectItem( null );
12475 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
12477 this.emit( 'remove', items
);
12485 * Items will be detached, not removed, so they can be used later.
12490 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
12491 var items
= this.items
.slice();
12494 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
12497 this.selectItem( null );
12499 this.emit( 'remove', items
);
12505 * Select widget containing button options.
12507 * Use together with OO.ui.ButtonOptionWidget.
12510 * @extends OO.ui.SelectWidget
12513 * @param {Object} [config] Configuration options
12515 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
12516 // Parent constructor
12517 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
12520 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
12525 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
12528 * Select widget containing radio button options.
12530 * Use together with OO.ui.RadioOptionWidget.
12533 * @extends OO.ui.SelectWidget
12536 * @param {Object} [config] Configuration options
12538 OO
.ui
.RadioSelectWidget
= function OoUiRadioSelectWidget( config
) {
12539 // Parent constructor
12540 OO
.ui
.RadioSelectWidget
.super.call( this, config
);
12543 this.$element
.addClass( 'oo-ui-radioSelectWidget' );
12548 OO
.inheritClass( OO
.ui
.RadioSelectWidget
, OO
.ui
.SelectWidget
);
12551 * Overlaid menu of options.
12553 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
12556 * Use together with OO.ui.MenuOptionWidget.
12559 * @extends OO.ui.SelectWidget
12560 * @mixins OO.ui.ClippableElement
12563 * @param {Object} [config] Configuration options
12564 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
12565 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
12566 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
12568 OO
.ui
.MenuSelectWidget
= function OoUiMenuSelectWidget( config
) {
12569 // Configuration initialization
12570 config
= config
|| {};
12572 // Parent constructor
12573 OO
.ui
.MenuSelectWidget
.super.call( this, config
);
12575 // Mixin constructors
12576 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
12579 this.flashing
= false;
12580 this.visible
= false;
12581 this.newItems
= null;
12582 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
12583 this.$input
= config
.input
? config
.input
.$input
: null;
12584 this.$widget
= config
.widget
? config
.widget
.$element
: null;
12585 this.$previousFocus
= null;
12586 this.isolated
= !config
.input
;
12587 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
12588 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
12593 .attr( 'role', 'menu' )
12594 .addClass( 'oo-ui-menuSelectWidget' );
12599 OO
.inheritClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.SelectWidget
);
12600 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.ClippableElement
);
12605 * Handles document mouse down events.
12607 * @param {jQuery.Event} e Key down event
12609 OO
.ui
.MenuSelectWidget
.prototype.onDocumentMouseDown = function ( e
) {
12611 !OO
.ui
.contains( this.$element
[0], e
.target
, true ) &&
12612 ( !this.$widget
|| !OO
.ui
.contains( this.$widget
[0], e
.target
, true ) )
12614 this.toggle( false );
12619 * Handles key down events.
12621 * @param {jQuery.Event} e Key down event
12623 OO
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
12626 highlightItem
= this.getHighlightedItem();
12628 if ( !this.isDisabled() && this.isVisible() ) {
12629 if ( !highlightItem
) {
12630 highlightItem
= this.getSelectedItem();
12632 switch ( e
.keyCode
) {
12633 case OO
.ui
.Keys
.ENTER
:
12634 this.chooseItem( highlightItem
);
12637 case OO
.ui
.Keys
.UP
:
12638 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
12641 case OO
.ui
.Keys
.DOWN
:
12642 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
12645 case OO
.ui
.Keys
.ESCAPE
:
12646 if ( highlightItem
) {
12647 highlightItem
.setHighlighted( false );
12649 this.toggle( false );
12655 this.highlightItem( nextItem
);
12656 nextItem
.scrollElementIntoView();
12660 e
.preventDefault();
12661 e
.stopPropagation();
12668 * Bind key down listener.
12670 OO
.ui
.MenuSelectWidget
.prototype.bindKeyDownListener = function () {
12671 if ( this.$input
) {
12672 this.$input
.on( 'keydown', this.onKeyDownHandler
);
12674 // Capture menu navigation keys
12675 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
12680 * Unbind key down listener.
12682 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyDownListener = function () {
12683 if ( this.$input
) {
12684 this.$input
.off( 'keydown' );
12686 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
12693 * This will close the menu when done, unlike selectItem which only changes selection.
12695 * @param {OO.ui.OptionWidget} item Item to choose
12698 OO
.ui
.MenuSelectWidget
.prototype.chooseItem = function ( item
) {
12702 OO
.ui
.MenuSelectWidget
.super.prototype.chooseItem
.call( this, item
);
12704 if ( item
&& !this.flashing
) {
12705 this.flashing
= true;
12706 item
.flash().done( function () {
12707 widget
.toggle( false );
12708 widget
.flashing
= false;
12711 this.toggle( false );
12720 OO
.ui
.MenuSelectWidget
.prototype.addItems = function ( items
, index
) {
12724 OO
.ui
.MenuSelectWidget
.super.prototype.addItems
.call( this, items
, index
);
12727 if ( !this.newItems
) {
12728 this.newItems
= [];
12731 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
12733 if ( this.isVisible() ) {
12734 // Defer fitting label until item has been attached
12737 this.newItems
.push( item
);
12741 // Reevaluate clipping
12750 OO
.ui
.MenuSelectWidget
.prototype.removeItems = function ( items
) {
12752 OO
.ui
.MenuSelectWidget
.super.prototype.removeItems
.call( this, items
);
12754 // Reevaluate clipping
12763 OO
.ui
.MenuSelectWidget
.prototype.clearItems = function () {
12765 OO
.ui
.MenuSelectWidget
.super.prototype.clearItems
.call( this );
12767 // Reevaluate clipping
12776 OO
.ui
.MenuSelectWidget
.prototype.toggle = function ( visible
) {
12777 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
12780 change
= visible
!== this.isVisible(),
12781 elementDoc
= this.getElementDocument(),
12782 widgetDoc
= this.$widget
? this.$widget
[0].ownerDocument
: null;
12785 OO
.ui
.MenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
12789 this.bindKeyDownListener();
12791 // Change focus to enable keyboard navigation
12792 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
12793 this.$previousFocus
= this.$( ':focus' );
12794 this.$input
[0].focus();
12796 if ( this.newItems
&& this.newItems
.length
) {
12797 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
12798 this.newItems
[i
].fitLabel();
12800 this.newItems
= null;
12802 this.toggleClipping( true );
12805 if ( this.autoHide
) {
12806 elementDoc
.addEventListener(
12807 'mousedown', this.onDocumentMouseDownHandler
, true
12809 // Support $widget being in a different document
12810 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
12811 widgetDoc
.addEventListener(
12812 'mousedown', this.onDocumentMouseDownHandler
, true
12817 this.unbindKeyDownListener();
12818 if ( this.isolated
&& this.$previousFocus
) {
12819 this.$previousFocus
[0].focus();
12820 this.$previousFocus
= null;
12822 elementDoc
.removeEventListener(
12823 'mousedown', this.onDocumentMouseDownHandler
, true
12825 // Support $widget being in a different document
12826 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
12827 widgetDoc
.removeEventListener(
12828 'mousedown', this.onDocumentMouseDownHandler
, true
12831 this.toggleClipping( false );
12839 * Menu for a text input widget.
12841 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
12842 * is in a different frame, the menu's position is automatically calculated and maintained when the
12843 * menu is toggled or the window is resized.
12846 * @extends OO.ui.MenuSelectWidget
12849 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
12850 * @param {Object} [config] Configuration options
12851 * @cfg {jQuery} [$container=input.$element] Element to render menu under
12853 OO
.ui
.TextInputMenuSelectWidget
= function OoUiTextInputMenuSelectWidget( input
, config
) {
12854 // Configuration initialization
12855 config
= config
|| {};
12857 // Parent constructor
12858 OO
.ui
.TextInputMenuSelectWidget
.super.call( this, config
);
12861 this.input
= input
;
12862 this.$container
= config
.$container
|| this.input
.$element
;
12863 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
12866 this.$element
.addClass( 'oo-ui-textInputMenuSelectWidget' );
12871 OO
.inheritClass( OO
.ui
.TextInputMenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
12876 * Handle window resize event.
12878 * @param {jQuery.Event} e Window resize event
12880 OO
.ui
.TextInputMenuSelectWidget
.prototype.onWindowResize = function () {
12887 OO
.ui
.TextInputMenuSelectWidget
.prototype.toggle = function ( visible
) {
12888 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
12890 var change
= visible
!== this.isVisible();
12892 if ( change
&& visible
) {
12893 // Make sure the width is set before the parent method runs.
12894 // After this we have to call this.position(); again to actually
12895 // position ourselves correctly.
12900 OO
.ui
.TextInputMenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
12903 if ( this.isVisible() ) {
12905 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
12907 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
12915 * Position the menu.
12919 OO
.ui
.TextInputMenuSelectWidget
.prototype.position = function () {
12920 var $container
= this.$container
,
12921 pos
= OO
.ui
.Element
.static.getRelativePosition( $container
, this.$element
.offsetParent() );
12923 // Position under input
12924 pos
.top
+= $container
.height();
12925 this.$element
.css( pos
);
12928 this.setIdealSize( $container
.width() );
12929 // We updated the position, so re-evaluate the clipping state
12936 * Structured list of items.
12938 * Use with OO.ui.OutlineOptionWidget.
12941 * @extends OO.ui.SelectWidget
12944 * @param {Object} [config] Configuration options
12946 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
12947 // Configuration initialization
12948 config
= config
|| {};
12950 // Parent constructor
12951 OO
.ui
.OutlineSelectWidget
.super.call( this, config
);
12954 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
12959 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
12962 * Switch that slides on and off.
12965 * @extends OO.ui.Widget
12966 * @mixins OO.ui.ToggleWidget
12969 * @param {Object} [config] Configuration options
12970 * @cfg {boolean} [value=false] Initial value
12972 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
12973 // Parent constructor
12974 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
12976 // Mixin constructors
12977 OO
.ui
.ToggleWidget
.call( this, config
);
12980 this.dragging
= false;
12981 this.dragStart
= null;
12982 this.sliding
= false;
12983 this.$glow
= this.$( '<span>' );
12984 this.$grip
= this.$( '<span>' );
12987 this.$element
.on( 'click', this.onClick
.bind( this ) );
12990 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
12991 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
12993 .addClass( 'oo-ui-toggleSwitchWidget' )
12994 .append( this.$glow
, this.$grip
);
12999 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
13000 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
13005 * Handle mouse down events.
13007 * @param {jQuery.Event} e Mouse down event
13009 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
13010 if ( !this.isDisabled() && e
.which
=== 1 ) {
13011 this.setValue( !this.value
);