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-02-04T16:51:55Z
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
] ) {
86 return 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
++ ) {
450 match
= matches
[ 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
++ ) {
462 match
= matches
[ 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
++ ) {
540 item
= this.list
[ 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
++ ) {
586 action
= actions
[ 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
++ ) {
615 action
= actions
[ 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
) ) {
1061 return $parent
[ 0 ];
1065 val
= $parent
.css( props
[ i
] );
1066 if ( val
=== 'auto' || val
=== 'scroll' ) {
1067 return $parent
[ 0 ];
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
) {
1147 * Force the browser to reconsider whether it really needs to render scrollbars inside the element
1148 * and reserve space for them, because it probably doesn't.
1150 * Workaround primarily for <https://code.google.com/p/chromium/issues/detail?id=387290>, but also
1151 * similar bugs in other browsers. "Just" forcing a reflow is not sufficient in all cases, we need
1152 * to first actually detach (or hide, but detaching is simpler) all children, *then* force a reflow,
1153 * and then reattach (or show) them back.
1156 * @param {HTMLElement} el Element to reconsider the scrollbars on
1158 OO
.ui
.Element
.static.reconsiderScrollbars = function ( el
) {
1159 var i
, len
, nodes
= [];
1160 // Detach all children
1161 while ( el
.firstChild
) {
1162 nodes
.push( el
.firstChild
);
1163 el
.removeChild( el
.firstChild
);
1166 void el
.offsetHeight
;
1167 // Reattach all children
1168 for ( i
= 0, len
= nodes
.length
; i
< len
; i
++ ) {
1169 el
.appendChild( nodes
[ i
] );
1178 * @return {Mixed} Element data
1180 OO
.ui
.Element
.prototype.getData = function () {
1187 * @param {Mixed} Element data
1190 OO
.ui
.Element
.prototype.setData = function ( data
) {
1196 * Check if element supports one or more methods.
1198 * @param {string|string[]} methods Method or list of methods to check
1199 * @return {boolean} All methods are supported
1201 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1205 methods
= $.isArray( methods
) ? methods
: [ methods
];
1206 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1207 if ( $.isFunction( this[ methods
[ i
] ] ) ) {
1212 return methods
.length
=== support
;
1216 * Update the theme-provided classes.
1218 * @localdoc This is called in element mixins and widget classes any time state changes.
1219 * Updating is debounced, minimizing overhead of changing multiple attributes and
1220 * guaranteeing that theme updates do not occur within an element's constructor
1222 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1223 if ( !this.updateThemeClassesPending
) {
1224 this.updateThemeClassesPending
= true;
1225 setTimeout( this.debouncedUpdateThemeClassesHandler
);
1232 OO
.ui
.Element
.prototype.debouncedUpdateThemeClasses = function () {
1233 OO
.ui
.theme
.updateElementClasses( this );
1234 this.updateThemeClassesPending
= false;
1238 * Get the HTML tag name.
1240 * Override this method to base the result on instance information.
1242 * @return {string} HTML tag name
1244 OO
.ui
.Element
.prototype.getTagName = function () {
1245 return this.constructor.static.tagName
;
1249 * Check if the element is attached to the DOM
1250 * @return {boolean} The element is attached to the DOM
1252 OO
.ui
.Element
.prototype.isElementAttached = function () {
1253 return $.contains( this.getElementDocument(), this.$element
[ 0 ] );
1257 * Get the DOM document.
1259 * @return {HTMLDocument} Document object
1261 OO
.ui
.Element
.prototype.getElementDocument = function () {
1262 // Don't use this.$.context because subclasses can rebind this.$
1263 // Don't cache this in other ways either because subclasses could can change this.$element
1264 return OO
.ui
.Element
.static.getDocument( this.$element
);
1268 * Get the DOM window.
1270 * @return {Window} Window object
1272 OO
.ui
.Element
.prototype.getElementWindow = function () {
1273 return OO
.ui
.Element
.static.getWindow( this.$element
);
1277 * Get closest scrollable container.
1279 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1280 return OO
.ui
.Element
.static.getClosestScrollableContainer( this.$element
[ 0 ] );
1284 * Get group element is in.
1286 * @return {OO.ui.GroupElement|null} Group element, null if none
1288 OO
.ui
.Element
.prototype.getElementGroup = function () {
1289 return this.elementGroup
;
1293 * Set group element is in.
1295 * @param {OO.ui.GroupElement|null} group Group element, null if none
1298 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1299 this.elementGroup
= group
;
1304 * Scroll element into view.
1306 * @param {Object} [config] Configuration options
1308 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1309 return OO
.ui
.Element
.static.scrollIntoView( this.$element
[ 0 ], config
);
1313 * Container for elements.
1317 * @extends OO.ui.Element
1318 * @mixins OO.EventEmitter
1321 * @param {Object} [config] Configuration options
1323 OO
.ui
.Layout
= function OoUiLayout( config
) {
1324 // Configuration initialization
1325 config
= config
|| {};
1327 // Parent constructor
1328 OO
.ui
.Layout
.super.call( this, config
);
1330 // Mixin constructors
1331 OO
.EventEmitter
.call( this );
1334 this.$element
.addClass( 'oo-ui-layout' );
1339 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1340 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1343 * User interface control.
1347 * @extends OO.ui.Element
1348 * @mixins OO.EventEmitter
1351 * @param {Object} [config] Configuration options
1352 * @cfg {boolean} [disabled=false] Disable
1354 OO
.ui
.Widget
= function OoUiWidget( config
) {
1355 // Initialize config
1356 config
= $.extend( { disabled
: false }, config
);
1358 // Parent constructor
1359 OO
.ui
.Widget
.super.call( this, config
);
1361 // Mixin constructors
1362 OO
.EventEmitter
.call( this );
1365 this.visible
= true;
1366 this.disabled
= null;
1367 this.wasDisabled
= null;
1370 this.$element
.addClass( 'oo-ui-widget' );
1371 this.setDisabled( !!config
.disabled
);
1376 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1377 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1383 * @param {boolean} disabled Widget is disabled
1388 * @param {boolean} visible Widget is visible
1394 * Check if the widget is disabled.
1396 * @return {boolean} Button is disabled
1398 OO
.ui
.Widget
.prototype.isDisabled = function () {
1399 return this.disabled
;
1403 * Check if widget is visible.
1405 * @return {boolean} Widget is visible
1407 OO
.ui
.Widget
.prototype.isVisible = function () {
1408 return this.visible
;
1412 * Set the disabled state of the widget.
1414 * This should probably change the widgets' appearance and prevent it from being used.
1416 * @param {boolean} disabled Disable widget
1419 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1422 this.disabled
= !!disabled
;
1423 isDisabled
= this.isDisabled();
1424 if ( isDisabled
!== this.wasDisabled
) {
1425 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1426 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1427 this.$element
.attr( 'aria-disabled', isDisabled
.toString() );
1428 this.emit( 'disable', isDisabled
);
1429 this.updateThemeClasses();
1431 this.wasDisabled
= isDisabled
;
1437 * Toggle visibility of widget.
1439 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1443 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1444 show
= show
=== undefined ? !this.visible
: !!show
;
1446 if ( show
!== this.isVisible() ) {
1447 this.visible
= show
;
1448 this.$element
.toggleClass( 'oo-ui-element-hidden', !this.visible
);
1449 this.emit( 'toggle', show
);
1456 * Update the disabled state, in case of changes in parent widget.
1460 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1461 this.setDisabled( this.disabled
);
1466 * Container for elements in a child frame.
1468 * Use together with OO.ui.WindowManager.
1472 * @extends OO.ui.Element
1473 * @mixins OO.EventEmitter
1475 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1476 * teardown processes are executed when the window is closed.
1478 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1479 * - Window manager begins opening window
1480 * - {@link #getSetupProcess} method is called and its result executed
1481 * - {@link #getReadyProcess} method is called and its result executed
1482 * - Window is now open
1484 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1485 * - Window manager begins closing window
1486 * - {@link #getHoldProcess} method is called and its result executed
1487 * - {@link #getTeardownProcess} method is called and its result executed
1488 * - Window is now closed
1490 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1491 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1492 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchronous
1493 * processing can complete. Always assume window processes are executed asynchronously. See
1494 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1495 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1497 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1498 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1501 * @param {Object} [config] Configuration options
1502 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large`, `larger` or
1503 * `full`; omit to use #static-size
1505 OO
.ui
.Window
= function OoUiWindow( config
) {
1506 // Configuration initialization
1507 config
= config
|| {};
1509 // Parent constructor
1510 OO
.ui
.Window
.super.call( this, config
);
1512 // Mixin constructors
1513 OO
.EventEmitter
.call( this );
1516 this.manager
= null;
1517 this.initialized
= false;
1518 this.visible
= false;
1519 this.opening
= null;
1520 this.closing
= null;
1523 this.loading
= null;
1524 this.size
= config
.size
|| this.constructor.static.size
;
1525 this.$frame
= this.$( '<div>' );
1526 this.$overlay
= this.$( '<div>' );
1530 .addClass( 'oo-ui-window' )
1531 .append( this.$frame
, this.$overlay
);
1532 this.$frame
.addClass( 'oo-ui-window-frame' );
1533 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1535 // NOTE: Additional initialization will occur when #setManager is called
1540 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1541 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1543 /* Static Properties */
1546 * Symbolic name of size.
1548 * Size is used if no size is configured during construction.
1552 * @property {string}
1554 OO
.ui
.Window
.static.size
= 'medium';
1556 /* Static Methods */
1559 * Transplant the CSS styles from as parent document to a frame's document.
1561 * This loops over the style sheets in the parent document, and copies their nodes to the
1562 * frame's document. It then polls the document to see when all styles have loaded, and once they
1563 * have, resolves the promise.
1565 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1566 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1567 * Firefox, where the styles won't load until the iframe becomes visible.
1569 * For details of how we arrived at the strategy used in this function, see #load.
1573 * @param {HTMLDocument} parentDoc Document to transplant styles from
1574 * @param {HTMLDocument} frameDoc Document to transplant styles to
1575 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1576 * @return {jQuery.Promise} Promise resolved when styles have loaded
1578 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1579 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1580 $pollNodes
= $( [] ),
1581 // Fake font-family value
1582 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1583 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1584 deferred
= $.Deferred();
1586 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1587 styleNode
= parentDoc
.styleSheets
[ i
].ownerNode
;
1588 if ( styleNode
.disabled
) {
1592 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1593 // External stylesheet; use @import
1594 styleText
= '@import url(' + styleNode
.href
+ ');';
1596 // Internal stylesheet; just copy the text
1597 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1598 // other browsers, so fall back to '' rather than 'undefined'
1599 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[ i
].cssText
|| '';
1602 // Create a node with a unique ID that we're going to monitor to see when the CSS
1604 if ( styleNode
.oouiFrameTransplantStylesId
) {
1605 // If we're nesting transplantStyles operations and this node already has
1606 // a CSS rule to wait for loading, reuse it
1607 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1609 // Otherwise, create a new ID
1610 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1613 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1614 // The font-family rule will only take effect once the @import finishes
1615 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1618 // Create a node with id=pollNodeId
1619 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1620 .attr( 'id', pollNodeId
)
1621 .appendTo( frameDoc
.body
)
1624 // Add our modified CSS as a <style> tag
1625 newNode
= frameDoc
.createElement( 'style' );
1626 newNode
.textContent
= styleText
;
1627 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1628 frameDoc
.head
.appendChild( newNode
);
1630 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1632 // Poll every 100ms until all external stylesheets have loaded
1633 $pendingPollNodes
= $pollNodes
;
1634 timeoutID
= setTimeout( function pollExternalStylesheets() {
1636 $pendingPollNodes
.length
> 0 &&
1637 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1639 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1642 if ( $pendingPollNodes
.length
=== 0 ) {
1644 if ( timeoutID
!== null ) {
1646 $pollNodes
.remove();
1650 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1653 // ...but give up after a while
1654 if ( timeout
!== 0 ) {
1655 setTimeout( function () {
1657 clearTimeout( timeoutID
);
1659 $pollNodes
.remove();
1662 }, timeout
|| 5000 );
1665 return deferred
.promise();
1671 * Handle mouse down events.
1673 * @param {jQuery.Event} e Mouse down event
1675 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1676 // Prevent clicking on the click-block from stealing focus
1677 if ( e
.target
=== this.$element
[ 0 ] ) {
1683 * Check if window has been initialized.
1685 * @return {boolean} Window has been initialized
1687 OO
.ui
.Window
.prototype.isInitialized = function () {
1688 return this.initialized
;
1692 * Check if window is visible.
1694 * @return {boolean} Window is visible
1696 OO
.ui
.Window
.prototype.isVisible = function () {
1697 return this.visible
;
1701 * Check if window is loading.
1703 * @return {boolean} Window is loading
1705 OO
.ui
.Window
.prototype.isLoading = function () {
1706 return this.loading
&& this.loading
.state() === 'pending';
1710 * Check if window is loaded.
1712 * @return {boolean} Window is loaded
1714 OO
.ui
.Window
.prototype.isLoaded = function () {
1715 return this.loading
&& this.loading
.state() === 'resolved';
1719 * Check if window is opening.
1721 * This is a wrapper around OO.ui.WindowManager#isOpening.
1723 * @return {boolean} Window is opening
1725 OO
.ui
.Window
.prototype.isOpening = function () {
1726 return this.manager
.isOpening( this );
1730 * Check if window is closing.
1732 * This is a wrapper around OO.ui.WindowManager#isClosing.
1734 * @return {boolean} Window is closing
1736 OO
.ui
.Window
.prototype.isClosing = function () {
1737 return this.manager
.isClosing( this );
1741 * Check if window is opened.
1743 * This is a wrapper around OO.ui.WindowManager#isOpened.
1745 * @return {boolean} Window is opened
1747 OO
.ui
.Window
.prototype.isOpened = function () {
1748 return this.manager
.isOpened( this );
1752 * Get the window manager.
1754 * @return {OO.ui.WindowManager} Manager of window
1756 OO
.ui
.Window
.prototype.getManager = function () {
1757 return this.manager
;
1761 * Get the window size.
1763 * @return {string} Symbolic size name, e.g. `small`, `medium`, `large`, `larger`, `full`
1765 OO
.ui
.Window
.prototype.getSize = function () {
1770 * Disable transitions on window's frame for the duration of the callback function, then enable them
1774 * @param {Function} callback Function to call while transitions are disabled
1776 OO
.ui
.Window
.prototype.withoutSizeTransitions = function ( callback
) {
1777 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1778 // Disable transitions first, otherwise we'll get values from when the window was animating.
1780 styleObj
= this.$frame
[ 0 ].style
;
1781 oldTransition
= styleObj
.transition
|| styleObj
.OTransition
|| styleObj
.MsTransition
||
1782 styleObj
.MozTransition
|| styleObj
.WebkitTransition
;
1783 styleObj
.transition
= styleObj
.OTransition
= styleObj
.MsTransition
=
1784 styleObj
.MozTransition
= styleObj
.WebkitTransition
= 'none';
1786 // Force reflow to make sure the style changes done inside callback really are not transitioned
1787 this.$frame
.height();
1788 styleObj
.transition
= styleObj
.OTransition
= styleObj
.MsTransition
=
1789 styleObj
.MozTransition
= styleObj
.WebkitTransition
= oldTransition
;
1793 * Get the height of the dialog contents.
1795 * @return {number} Content height
1797 OO
.ui
.Window
.prototype.getContentHeight = function () {
1800 bodyStyleObj
= this.$body
[ 0 ].style
,
1801 frameStyleObj
= this.$frame
[ 0 ].style
;
1803 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1804 // Disable transitions first, otherwise we'll get values from when the window was animating.
1805 this.withoutSizeTransitions( function () {
1806 var oldHeight
= frameStyleObj
.height
,
1807 oldPosition
= bodyStyleObj
.position
;
1808 frameStyleObj
.height
= '1px';
1809 // Force body to resize to new width
1810 bodyStyleObj
.position
= 'relative';
1811 bodyHeight
= win
.getBodyHeight();
1812 frameStyleObj
.height
= oldHeight
;
1813 bodyStyleObj
.position
= oldPosition
;
1817 // Add buffer for border
1818 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1819 // Use combined heights of children
1820 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1825 * Get the height of the dialog contents.
1827 * When this function is called, the dialog will temporarily have been resized
1828 * to height=1px, so .scrollHeight measurements can be taken accurately.
1830 * @return {number} Height of content
1832 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1833 return this.$body
[ 0 ].scrollHeight
;
1837 * Get the directionality of the frame
1839 * @return {string} Directionality, 'ltr' or 'rtl'
1841 OO
.ui
.Window
.prototype.getDir = function () {
1846 * Get a process for setting up a window for use.
1848 * Each time the window is opened this process will set it up for use in a particular context, based
1849 * on the `data` argument.
1851 * When you override this method, you can add additional setup steps to the process the parent
1852 * method provides using the 'first' and 'next' methods.
1855 * @param {Object} [data] Window opening data
1856 * @return {OO.ui.Process} Setup process
1858 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1859 return new OO
.ui
.Process();
1863 * Get a process for readying a window for use.
1865 * Each time the window is open and setup, this process will ready it up for use in a particular
1866 * context, based on the `data` argument.
1868 * When you override this method, you can add additional setup steps to the process the parent
1869 * method provides using the 'first' and 'next' methods.
1872 * @param {Object} [data] Window opening data
1873 * @return {OO.ui.Process} Setup process
1875 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1876 return new OO
.ui
.Process();
1880 * Get a process for holding a window from use.
1882 * Each time the window is closed, this process will hold it from use in a particular context, based
1883 * on the `data` argument.
1885 * When you override this method, you can add additional setup steps to the process the parent
1886 * method provides using the 'first' and 'next' methods.
1889 * @param {Object} [data] Window closing data
1890 * @return {OO.ui.Process} Hold process
1892 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1893 return new OO
.ui
.Process();
1897 * Get a process for tearing down a window after use.
1899 * Each time the window is closed this process will tear it down and do something with the user's
1900 * interactions within the window, based on the `data` argument.
1902 * When you override this method, you can add additional teardown steps to the process the parent
1903 * method provides using the 'first' and 'next' methods.
1906 * @param {Object} [data] Window closing data
1907 * @return {OO.ui.Process} Teardown process
1909 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1910 return new OO
.ui
.Process();
1914 * Toggle visibility of window.
1916 * If the window is isolated and hasn't fully loaded yet, the visibility property will be used
1917 * instead of display.
1919 * @param {boolean} [show] Make window visible, omit to toggle visibility
1923 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1924 show
= show
=== undefined ? !this.visible
: !!show
;
1926 if ( show
!== this.isVisible() ) {
1927 this.visible
= show
;
1929 if ( this.isolated
&& !this.isLoaded() ) {
1930 // Hide the window using visibility instead of display until loading is complete
1931 // Can't use display: none; because that prevents the iframe from loading in Firefox
1933 .css( 'visibility', show
? 'visible' : 'hidden' );
1936 .toggleClass( 'oo-ui-element-hidden', !this.visible
)
1937 .css( 'visibility', '' );
1939 this.emit( 'toggle', show
);
1946 * Set the window manager.
1948 * This must be called before initialize. Calling it more than once will cause an error.
1950 * @param {OO.ui.WindowManager} manager Manager for this window
1951 * @throws {Error} If called more than once
1954 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1955 if ( this.manager
) {
1956 throw new Error( 'Cannot set window manager, window already has a manager' );
1960 this.manager
= manager
;
1961 this.isolated
= manager
.shouldIsolate();
1964 if ( this.isolated
) {
1965 this.$iframe
= this.$( '<iframe>' );
1966 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1967 this.$frame
.append( this.$iframe
);
1968 this.$ = function () {
1969 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1971 // WARNING: Do not use this.$ again until #initialize is called
1973 this.$content
= this.$( '<div>' );
1974 this.$document
= $( this.getElementDocument() );
1975 this.$content
.addClass( 'oo-ui-window-content' ).attr( 'tabIndex', 0 );
1976 this.$frame
.append( this.$content
);
1978 this.toggle( false );
1980 // Figure out directionality:
1981 this.dir
= OO
.ui
.Element
.static.getDir( this.$iframe
|| this.$content
) || 'ltr';
1987 * Set the window size.
1989 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1992 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1999 * Update the window size.
2003 OO
.ui
.Window
.prototype.updateSize = function () {
2004 this.manager
.updateWindowSize( this );
2009 * Set window dimensions.
2011 * Properties are applied to the frame container.
2013 * @param {Object} dim CSS dimension properties
2014 * @param {string|number} [dim.width] Width
2015 * @param {string|number} [dim.minWidth] Minimum width
2016 * @param {string|number} [dim.maxWidth] Maximum width
2017 * @param {string|number} [dim.width] Height, omit to set based on height of contents
2018 * @param {string|number} [dim.minWidth] Minimum height
2019 * @param {string|number} [dim.maxWidth] Maximum height
2022 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
2025 styleObj
= this.$frame
[ 0 ].style
;
2027 // Calculate the height we need to set using the correct width
2028 if ( dim
.height
=== undefined ) {
2029 this.withoutSizeTransitions( function () {
2030 var oldWidth
= styleObj
.width
;
2031 win
.$frame
.css( 'width', dim
.width
|| '' );
2032 height
= win
.getContentHeight();
2033 styleObj
.width
= oldWidth
;
2036 height
= dim
.height
;
2040 width
: dim
.width
|| '',
2041 minWidth
: dim
.minWidth
|| '',
2042 maxWidth
: dim
.maxWidth
|| '',
2043 height
: height
|| '',
2044 minHeight
: dim
.minHeight
|| '',
2045 maxHeight
: dim
.maxHeight
|| ''
2052 * Initialize window contents.
2054 * The first time the window is opened, #initialize is called when it's safe to begin populating
2055 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
2057 * Once this method is called, this.$ can be used to create elements within the frame.
2059 * @throws {Error} If not attached to a manager
2062 OO
.ui
.Window
.prototype.initialize = function () {
2063 if ( !this.manager
) {
2064 throw new Error( 'Cannot initialize window, must be attached to a manager' );
2068 this.$head
= this.$( '<div>' );
2069 this.$body
= this.$( '<div>' );
2070 this.$foot
= this.$( '<div>' );
2071 this.$innerOverlay
= this.$( '<div>' );
2074 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
2077 this.$head
.addClass( 'oo-ui-window-head' );
2078 this.$body
.addClass( 'oo-ui-window-body' );
2079 this.$foot
.addClass( 'oo-ui-window-foot' );
2080 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
2081 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
2089 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
2090 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
2092 * @param {Object} [data] Window opening data
2093 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
2094 * first argument will be a promise which will be resolved when the window begins closing
2096 OO
.ui
.Window
.prototype.open = function ( data
) {
2097 return this.manager
.openWindow( this, data
);
2103 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
2104 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
2106 * @param {Object} [data] Window closing data
2107 * @return {jQuery.Promise} Promise resolved when window is closed
2109 OO
.ui
.Window
.prototype.close = function ( data
) {
2110 return this.manager
.closeWindow( this, data
);
2116 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2119 * @param {Object} [data] Window opening data
2120 * @return {jQuery.Promise} Promise resolved when window is setup
2122 OO
.ui
.Window
.prototype.setup = function ( data
) {
2124 deferred
= $.Deferred();
2126 this.toggle( true );
2127 this.getSetupProcess( data
).execute().done( function () {
2128 // Force redraw by asking the browser to measure the elements' widths
2129 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2130 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2134 return deferred
.promise();
2140 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2143 * @param {Object} [data] Window opening data
2144 * @return {jQuery.Promise} Promise resolved when window is ready
2146 OO
.ui
.Window
.prototype.ready = function ( data
) {
2148 deferred
= $.Deferred();
2150 this.$content
.focus();
2151 this.getReadyProcess( data
).execute().done( function () {
2152 // Force redraw by asking the browser to measure the elements' widths
2153 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2154 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2158 return deferred
.promise();
2164 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2167 * @param {Object} [data] Window closing data
2168 * @return {jQuery.Promise} Promise resolved when window is held
2170 OO
.ui
.Window
.prototype.hold = function ( data
) {
2172 deferred
= $.Deferred();
2174 this.getHoldProcess( data
).execute().done( function () {
2175 // Get the focused element within the window's content
2176 var $focus
= win
.$content
.find( OO
.ui
.Element
.static.getDocument( win
.$content
).activeElement
);
2178 // Blur the focused element
2179 if ( $focus
.length
) {
2183 // Force redraw by asking the browser to measure the elements' widths
2184 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2185 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2189 return deferred
.promise();
2195 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2198 * @param {Object} [data] Window closing data
2199 * @return {jQuery.Promise} Promise resolved when window is torn down
2201 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2203 deferred
= $.Deferred();
2205 this.getTeardownProcess( data
).execute().done( function () {
2206 // Force redraw by asking the browser to measure the elements' widths
2207 win
.$element
.removeClass( 'oo-ui-window-load oo-ui-window-setup' ).width();
2208 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2209 win
.$element
.addClass( 'oo-ui-element-hidden' );
2210 win
.visible
= false;
2214 return deferred
.promise();
2218 * Load the frame contents.
2220 * Once the iframe's stylesheets are loaded the returned promise will be resolved. Calling while
2221 * loading will return a promise but not trigger a new loading cycle. Calling after loading is
2222 * complete will return a promise that's already been resolved.
2224 * Sounds simple right? Read on...
2226 * When you create a dynamic iframe using open/write/close, the window.load event for the
2227 * iframe is triggered when you call close, and there's no further load event to indicate that
2228 * everything is actually loaded.
2230 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2231 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2232 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2233 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2234 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2236 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2237 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2238 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2239 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2240 * accessing .cssRules is allowed.
2242 * However, now that we control the styles we're injecting, we might as well do away with
2243 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2244 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2245 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2246 * font-family rule is not applied until after the `@import` finishes.
2248 * All this stylesheet injection and polling magic is in #transplantStyles.
2250 * @return {jQuery.Promise} Promise resolved when loading is complete
2252 OO
.ui
.Window
.prototype.load = function () {
2253 var sub
, doc
, loading
,
2256 this.$element
.addClass( 'oo-ui-window-load' );
2258 // Non-isolated windows are already "loaded"
2259 if ( !this.loading
&& !this.isolated
) {
2260 this.loading
= $.Deferred().resolve();
2262 // Set initialized state after so sub-classes aren't confused by it being set by calling
2263 // their parent initialize method
2264 this.initialized
= true;
2267 // Return existing promise if already loading or loaded
2268 if ( this.loading
) {
2269 return this.loading
.promise();
2273 loading
= this.loading
= $.Deferred();
2274 sub
= this.$iframe
.prop( 'contentWindow' );
2277 // Initialize contents
2282 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2283 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2284 '<div class="oo-ui-window-content"></div>' +
2291 this.$ = OO
.ui
.Element
.static.getJQuery( doc
, this.$iframe
);
2292 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2293 this.$document
= this.$( doc
);
2296 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[ 0 ] )
2297 .always( function () {
2298 // Initialize isolated windows
2300 // Set initialized state after so sub-classes aren't confused by it being set by calling
2301 // their parent initialize method
2302 win
.initialized
= true;
2303 // Undo the visibility: hidden; hack and apply display: none;
2304 // We can do this safely now that the iframe has initialized
2305 // (don't do this from within #initialize because it has to happen
2306 // after the all subclasses have been handled as well).
2307 win
.toggle( win
.isVisible() );
2312 return loading
.promise();
2316 * Base class for all dialogs.
2319 * - Manage the window (open and close, etc.).
2320 * - Store the internal name and display title.
2321 * - A stack to track one or more pending actions.
2322 * - Manage a set of actions that can be performed.
2323 * - Configure and create action widgets.
2326 * - Close the dialog with Escape key.
2327 * - Visually lock the dialog while an action is in
2328 * progress (aka "pending").
2330 * Subclass responsibilities:
2331 * - Display the title somewhere.
2332 * - Add content to the dialog.
2333 * - Provide a UI to close the dialog.
2334 * - Display the action widgets somewhere.
2338 * @extends OO.ui.Window
2339 * @mixins OO.ui.PendingElement
2342 * @param {Object} [config] Configuration options
2344 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2345 // Parent constructor
2346 OO
.ui
.Dialog
.super.call( this, config
);
2348 // Mixin constructors
2349 OO
.ui
.PendingElement
.call( this );
2352 this.actions
= new OO
.ui
.ActionSet();
2353 this.attachedActions
= [];
2354 this.currentAction
= null;
2355 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2358 this.actions
.connect( this, {
2359 click
: 'onActionClick',
2360 resize
: 'onActionResize',
2361 change
: 'onActionsChange'
2366 .addClass( 'oo-ui-dialog' )
2367 .attr( 'role', 'dialog' );
2372 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2373 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2375 /* Static Properties */
2378 * Symbolic name of dialog.
2383 * @property {string}
2385 OO
.ui
.Dialog
.static.name
= '';
2393 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2395 OO
.ui
.Dialog
.static.title
= '';
2398 * List of OO.ui.ActionWidget configuration options.
2402 * @property {Object[]}
2404 OO
.ui
.Dialog
.static.actions
= [];
2407 * Close dialog when the escape key is pressed.
2412 * @property {boolean}
2414 OO
.ui
.Dialog
.static.escapable
= true;
2419 * Handle frame document key down events.
2421 * @param {jQuery.Event} e Key down event
2423 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2424 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2427 e
.stopPropagation();
2432 * Handle action resized events.
2434 * @param {OO.ui.ActionWidget} action Action that was resized
2436 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2437 // Override in subclass
2441 * Handle action click events.
2443 * @param {OO.ui.ActionWidget} action Action that was clicked
2445 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2446 if ( !this.isPending() ) {
2447 this.currentAction
= action
;
2448 this.executeAction( action
.getAction() );
2453 * Handle actions change event.
2455 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2456 this.detachActions();
2457 if ( !this.isClosing() ) {
2458 this.attachActions();
2463 * Get set of actions.
2465 * @return {OO.ui.ActionSet}
2467 OO
.ui
.Dialog
.prototype.getActions = function () {
2468 return this.actions
;
2472 * Get a process for taking action.
2474 * When you override this method, you can add additional accept steps to the process the parent
2475 * method provides using the 'first' and 'next' methods.
2478 * @param {string} [action] Symbolic name of action
2479 * @return {OO.ui.Process} Action process
2481 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2482 return new OO
.ui
.Process()
2483 .next( function () {
2485 // An empty action always closes the dialog without data, which should always be
2486 // safe and make no changes
2495 * @param {Object} [data] Dialog opening data
2496 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2497 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2498 * action item, omit to use #static-actions
2500 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2504 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2505 .next( function () {
2508 config
= this.constructor.static,
2509 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2511 this.title
.setLabel(
2512 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2514 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2516 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[ i
] ) )
2519 this.actions
.add( items
);
2521 if ( this.constructor.static.escapable
) {
2522 this.$document
.on( 'keydown', this.onDocumentKeyDownHandler
);
2530 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2532 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2533 .first( function () {
2534 if ( this.constructor.static.escapable
) {
2535 this.$document
.off( 'keydown', this.onDocumentKeyDownHandler
);
2538 this.actions
.clear();
2539 this.currentAction
= null;
2546 OO
.ui
.Dialog
.prototype.initialize = function () {
2548 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2551 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2554 this.$content
.addClass( 'oo-ui-dialog-content' );
2555 this.setPendingElement( this.$head
);
2559 * Attach action actions.
2561 OO
.ui
.Dialog
.prototype.attachActions = function () {
2562 // Remember the list of potentially attached actions
2563 this.attachedActions
= this.actions
.get();
2567 * Detach action actions.
2571 OO
.ui
.Dialog
.prototype.detachActions = function () {
2574 // Detach all actions that may have been previously attached
2575 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2576 this.attachedActions
[ i
].$element
.detach();
2578 this.attachedActions
= [];
2582 * Execute an action.
2584 * @param {string} action Symbolic name of action to execute
2585 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2587 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2589 return this.getActionProcess( action
).execute()
2590 .always( this.popPending
.bind( this ) );
2594 * Collection of windows.
2597 * @extends OO.ui.Element
2598 * @mixins OO.EventEmitter
2600 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2601 * already opening or opened, the current window will be closed without data. Empty closing data
2602 * should always result in the window being closed without causing constructive or destructive
2605 * As a window is opened and closed, it passes through several stages and the manager emits several
2606 * corresponding events.
2608 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2609 * - {@link #event-opening} is emitted with `opening` promise
2610 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2611 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2612 * - `setup` progress notification is emitted from opening promise
2613 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2614 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2615 * - `ready` progress notification is emitted from opening promise
2616 * - `opening` promise is resolved with `opened` promise
2617 * - Window is now open
2619 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2620 * - `opened` promise is resolved with `closing` promise
2621 * - {@link #event-closing} is emitted with `closing` promise
2622 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2623 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2624 * - `hold` progress notification is emitted from opening promise
2625 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2626 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2627 * - `teardown` progress notification is emitted from opening promise
2628 * - Closing promise is resolved
2629 * - Window is now closed
2632 * @param {Object} [config] Configuration options
2633 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2634 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2635 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2637 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2638 // Configuration initialization
2639 config
= config
|| {};
2641 // Parent constructor
2642 OO
.ui
.WindowManager
.super.call( this, config
);
2644 // Mixin constructors
2645 OO
.EventEmitter
.call( this );
2648 this.factory
= config
.factory
;
2649 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2650 this.isolate
= !!config
.isolate
;
2652 this.opening
= null;
2654 this.closing
= null;
2655 this.preparingToOpen
= null;
2656 this.preparingToClose
= null;
2658 this.currentWindow
= null;
2659 this.$ariaHidden
= null;
2660 this.requestedSize
= null;
2661 this.onWindowResizeTimeout
= null;
2662 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2663 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2664 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2665 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2669 .addClass( 'oo-ui-windowManager' )
2670 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2675 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2676 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2681 * Window is opening.
2683 * Fired when the window begins to be opened.
2686 * @param {OO.ui.Window} win Window that's being opened
2687 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2688 * resolved the first argument will be a promise which will be resolved when the window begins
2689 * closing, the second argument will be the opening data; progress notifications will be fired on
2690 * the promise for `setup` and `ready` when those processes are completed respectively.
2691 * @param {Object} data Window opening data
2695 * Window is closing.
2697 * Fired when the window begins to be closed.
2700 * @param {OO.ui.Window} win Window that's being closed
2701 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2702 * is resolved the first argument will be a the closing data; progress notifications will be fired
2703 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2704 * @param {Object} data Window closing data
2708 * Window was resized.
2711 * @param {OO.ui.Window} win Window that was resized
2714 /* Static Properties */
2717 * Map of symbolic size names and CSS properties.
2721 * @property {Object}
2723 OO
.ui
.WindowManager
.static.sizes
= {
2737 // These can be non-numeric because they are never used in calculations
2744 * Symbolic name of default size.
2746 * Default size is used if the window's requested size is not recognized.
2750 * @property {string}
2752 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2757 * Handle window resize events.
2759 * @param {jQuery.Event} e Window resize event
2761 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2762 clearTimeout( this.onWindowResizeTimeout
);
2763 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2767 * Handle window resize events.
2769 * @param {jQuery.Event} e Window resize event
2771 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2772 if ( this.currentWindow
) {
2773 this.updateWindowSize( this.currentWindow
);
2778 * Handle window mouse wheel events.
2780 * @param {jQuery.Event} e Mouse wheel event
2782 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2783 // Kill all events in the parent window if the child window is isolated
2784 return !this.shouldIsolate();
2788 * Handle document key down events.
2790 * @param {jQuery.Event} e Key down event
2792 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2793 switch ( e
.which
) {
2794 case OO
.ui
.Keys
.PAGEUP
:
2795 case OO
.ui
.Keys
.PAGEDOWN
:
2796 case OO
.ui
.Keys
.END
:
2797 case OO
.ui
.Keys
.HOME
:
2798 case OO
.ui
.Keys
.LEFT
:
2800 case OO
.ui
.Keys
.RIGHT
:
2801 case OO
.ui
.Keys
.DOWN
:
2802 // Kill all events in the parent window if the child window is isolated
2803 return !this.shouldIsolate();
2808 * Check if window is opening.
2810 * @return {boolean} Window is opening
2812 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2813 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2817 * Check if window is closing.
2819 * @return {boolean} Window is closing
2821 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2822 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2826 * Check if window is opened.
2828 * @return {boolean} Window is opened
2830 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2831 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2835 * Check if window contents should be isolated.
2837 * Window content isolation is done using inline frames.
2839 * @return {boolean} Window contents should be isolated
2841 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2842 return this.isolate
;
2846 * Check if a window is being managed.
2848 * @param {OO.ui.Window} win Window to check
2849 * @return {boolean} Window is being managed
2851 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2854 for ( name
in this.windows
) {
2855 if ( this.windows
[ name
] === win
) {
2864 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2866 * @param {OO.ui.Window} win Window being opened
2867 * @param {Object} [data] Window opening data
2868 * @return {number} Milliseconds to wait
2870 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2875 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2877 * @param {OO.ui.Window} win Window being opened
2878 * @param {Object} [data] Window opening data
2879 * @return {number} Milliseconds to wait
2881 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2886 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2888 * @param {OO.ui.Window} win Window being closed
2889 * @param {Object} [data] Window closing data
2890 * @return {number} Milliseconds to wait
2892 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2897 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2899 * @param {OO.ui.Window} win Window being closed
2900 * @param {Object} [data] Window closing data
2901 * @return {number} Milliseconds to wait
2903 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2904 return this.modal
? 250 : 0;
2908 * Get managed window by symbolic name.
2910 * If window is not yet instantiated, it will be instantiated and added automatically.
2912 * @param {string} name Symbolic window name
2913 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2914 * @throws {Error} If the symbolic name is unrecognized by the factory
2915 * @throws {Error} If the symbolic name unrecognized as a managed window
2917 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2918 var deferred
= $.Deferred(),
2919 win
= this.windows
[ name
];
2921 if ( !( win
instanceof OO
.ui
.Window
) ) {
2922 if ( this.factory
) {
2923 if ( !this.factory
.lookup( name
) ) {
2924 deferred
.reject( new OO
.ui
.Error(
2925 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2928 win
= this.factory
.create( name
, this, { $: this.$ } );
2929 this.addWindows( [ win
] );
2930 deferred
.resolve( win
);
2933 deferred
.reject( new OO
.ui
.Error(
2934 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2938 deferred
.resolve( win
);
2941 return deferred
.promise();
2945 * Get current window.
2947 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2949 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2950 return this.currentWindow
;
2956 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2957 * @param {Object} [data] Window opening data
2958 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2959 * for more details about the `opening` promise
2962 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2965 opening
= $.Deferred();
2967 // Argument handling
2968 if ( typeof win
=== 'string' ) {
2969 return this.getWindow( win
).then( function ( win
) {
2970 return manager
.openWindow( win
, data
);
2975 if ( !this.hasWindow( win
) ) {
2976 opening
.reject( new OO
.ui
.Error(
2977 'Cannot open window: window is not attached to manager'
2979 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2980 opening
.reject( new OO
.ui
.Error(
2981 'Cannot open window: another window is opening or open'
2986 if ( opening
.state() !== 'rejected' ) {
2987 if ( !win
.getManager() ) {
2988 win
.setManager( this );
2990 preparing
.push( win
.load() );
2992 if ( this.closing
) {
2993 // If a window is currently closing, wait for it to complete
2994 preparing
.push( this.closing
);
2997 this.preparingToOpen
= $.when
.apply( $, preparing
);
2998 // Ensure handlers get called after preparingToOpen is set
2999 this.preparingToOpen
.done( function () {
3000 if ( manager
.modal
) {
3001 manager
.toggleGlobalEvents( true );
3002 manager
.toggleAriaIsolation( true );
3004 manager
.currentWindow
= win
;
3005 manager
.opening
= opening
;
3006 manager
.preparingToOpen
= null;
3007 manager
.emit( 'opening', win
, opening
, data
);
3008 setTimeout( function () {
3009 win
.setup( data
).then( function () {
3010 manager
.updateWindowSize( win
);
3011 manager
.opening
.notify( { state
: 'setup' } );
3012 setTimeout( function () {
3013 win
.ready( data
).then( function () {
3014 manager
.opening
.notify( { state
: 'ready' } );
3015 manager
.opening
= null;
3016 manager
.opened
= $.Deferred();
3017 opening
.resolve( manager
.opened
.promise(), data
);
3019 }, manager
.getReadyDelay() );
3021 }, manager
.getSetupDelay() );
3025 return opening
.promise();
3031 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
3032 * @param {Object} [data] Window closing data
3033 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
3034 * for more details about the `closing` promise
3035 * @throws {Error} If no window by that name is being managed
3038 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
3041 closing
= $.Deferred(),
3044 // Argument handling
3045 if ( typeof win
=== 'string' ) {
3046 win
= this.windows
[ win
];
3047 } else if ( !this.hasWindow( win
) ) {
3053 closing
.reject( new OO
.ui
.Error(
3054 'Cannot close window: window is not attached to manager'
3056 } else if ( win
!== this.currentWindow
) {
3057 closing
.reject( new OO
.ui
.Error(
3058 'Cannot close window: window already closed with different data'
3060 } else if ( this.preparingToClose
|| this.closing
) {
3061 closing
.reject( new OO
.ui
.Error(
3062 'Cannot close window: window already closing with different data'
3067 if ( closing
.state() !== 'rejected' ) {
3068 if ( this.opening
) {
3069 // If the window is currently opening, close it when it's done
3070 preparing
.push( this.opening
);
3073 this.preparingToClose
= $.when
.apply( $, preparing
);
3074 // Ensure handlers get called after preparingToClose is set
3075 this.preparingToClose
.done( function () {
3076 manager
.closing
= closing
;
3077 manager
.preparingToClose
= null;
3078 manager
.emit( 'closing', win
, closing
, data
);
3079 opened
= manager
.opened
;
3080 manager
.opened
= null;
3081 opened
.resolve( closing
.promise(), data
);
3082 setTimeout( function () {
3083 win
.hold( data
).then( function () {
3084 closing
.notify( { state
: 'hold' } );
3085 setTimeout( function () {
3086 win
.teardown( data
).then( function () {
3087 closing
.notify( { state
: 'teardown' } );
3088 if ( manager
.modal
) {
3089 manager
.toggleGlobalEvents( false );
3090 manager
.toggleAriaIsolation( false );
3092 manager
.closing
= null;
3093 manager
.currentWindow
= null;
3094 closing
.resolve( data
);
3096 }, manager
.getTeardownDelay() );
3098 }, manager
.getHoldDelay() );
3102 return closing
.promise();
3108 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
3109 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
3110 * a statically configured symbolic name
3112 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
3113 var i
, len
, win
, name
, list
;
3115 if ( $.isArray( windows
) ) {
3116 // Convert to map of windows by looking up symbolic names from static configuration
3118 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
3119 name
= windows
[ i
].constructor.static.name
;
3120 if ( typeof name
!== 'string' ) {
3121 throw new Error( 'Cannot add window' );
3123 list
[ name
] = windows
[ i
];
3125 } else if ( $.isPlainObject( windows
) ) {
3130 for ( name
in list
) {
3132 this.windows
[ name
] = win
;
3133 this.$element
.append( win
.$element
);
3140 * Windows will be closed before they are removed.
3142 * @param {string[]} names Symbolic names of windows to remove
3143 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3144 * @throws {Error} If windows being removed are not being managed
3146 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3147 var i
, len
, win
, name
, cleanupWindow
,
3150 cleanup = function ( name
, win
) {
3151 delete manager
.windows
[ name
];
3152 win
.$element
.detach();
3155 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3157 win
= this.windows
[ name
];
3159 throw new Error( 'Cannot remove window' );
3161 cleanupWindow
= cleanup
.bind( null, name
, win
);
3162 promises
.push( this.closeWindow( name
).then( cleanupWindow
, cleanupWindow
) );
3165 return $.when
.apply( $, promises
);
3169 * Remove all windows.
3171 * Windows will be closed before they are removed.
3173 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3175 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3176 return this.removeWindows( Object
.keys( this.windows
) );
3182 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3186 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3187 // Bypass for non-current, and thus invisible, windows
3188 if ( win
!== this.currentWindow
) {
3192 var viewport
= OO
.ui
.Element
.static.getDimensions( win
.getElementWindow() ),
3193 sizes
= this.constructor.static.sizes
,
3194 size
= win
.getSize();
3196 if ( !sizes
[ size
] ) {
3197 size
= this.constructor.static.defaultSize
;
3199 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[ size
].width
) {
3203 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3204 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3205 win
.setDimensions( sizes
[ size
] );
3207 this.emit( 'resize', win
);
3213 * Bind or unbind global events for scrolling.
3215 * @param {boolean} [on] Bind global events
3218 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3219 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3222 if ( !this.globalEvents
) {
3223 this.$( this.getElementDocument() ).on( {
3224 // Prevent scrolling by keys in top-level window
3225 keydown
: this.onDocumentKeyDownHandler
3227 this.$( this.getElementWindow() ).on( {
3228 // Prevent scrolling by wheel in top-level window
3229 mousewheel
: this.onWindowMouseWheelHandler
,
3230 // Start listening for top-level window dimension changes
3231 'orientationchange resize': this.onWindowResizeHandler
3233 // Disable window scrolling in isolated windows
3234 if ( !this.shouldIsolate() ) {
3235 $( this.getElementDocument().body
).css( 'overflow', 'hidden' );
3237 this.globalEvents
= true;
3239 } else if ( this.globalEvents
) {
3240 // Unbind global events
3241 this.$( this.getElementDocument() ).off( {
3242 // Allow scrolling by keys in top-level window
3243 keydown
: this.onDocumentKeyDownHandler
3245 this.$( this.getElementWindow() ).off( {
3246 // Allow scrolling by wheel in top-level window
3247 mousewheel
: this.onWindowMouseWheelHandler
,
3248 // Stop listening for top-level window dimension changes
3249 'orientationchange resize': this.onWindowResizeHandler
3251 if ( !this.shouldIsolate() ) {
3252 $( this.getElementDocument().body
).css( 'overflow', '' );
3254 this.globalEvents
= false;
3261 * Toggle screen reader visibility of content other than the window manager.
3263 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3266 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3267 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3270 if ( !this.$ariaHidden
) {
3271 // Hide everything other than the window manager from screen readers
3272 this.$ariaHidden
= $( 'body' )
3274 .not( this.$element
.parentsUntil( 'body' ).last() )
3275 .attr( 'aria-hidden', '' );
3277 } else if ( this.$ariaHidden
) {
3278 // Restore screen reader visibility
3279 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3280 this.$ariaHidden
= null;
3287 * Destroy window manager.
3289 OO
.ui
.WindowManager
.prototype.destroy = function () {
3290 this.toggleGlobalEvents( false );
3291 this.toggleAriaIsolation( false );
3292 this.clearWindows();
3293 this.$element
.remove();
3300 * @param {string|jQuery} message Description of error
3301 * @param {Object} [config] Configuration options
3302 * @cfg {boolean} [recoverable=true] Error is recoverable
3303 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3305 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3306 // Configuration initialization
3307 config
= config
|| {};
3310 this.message
= message
instanceof jQuery
? message
: String( message
);
3311 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3312 this.warning
= !!config
.warning
;
3317 OO
.initClass( OO
.ui
.Error
);
3322 * Check if error can be recovered from.
3324 * @return {boolean} Error is recoverable
3326 OO
.ui
.Error
.prototype.isRecoverable = function () {
3327 return this.recoverable
;
3331 * Check if the error is a warning
3333 * @return {boolean} Error is warning
3335 OO
.ui
.Error
.prototype.isWarning = function () {
3336 return this.warning
;
3340 * Get error message as DOM nodes.
3342 * @return {jQuery} Error message in DOM nodes
3344 OO
.ui
.Error
.prototype.getMessage = function () {
3345 return this.message
instanceof jQuery
?
3346 this.message
.clone() :
3347 $( '<div>' ).text( this.message
).contents();
3351 * Get error message as text.
3353 * @return {string} Error message
3355 OO
.ui
.Error
.prototype.getMessageText = function () {
3356 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3360 * A list of functions, called in sequence.
3362 * If a function added to a process returns boolean false the process will stop; if it returns an
3363 * object with a `promise` method the process will use the promise to either continue to the next
3364 * step when the promise is resolved or stop when the promise is rejected.
3369 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3370 * call, see #createStep for more information
3371 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3373 * @return {Object} Step object, with `callback` and `context` properties
3375 OO
.ui
.Process = function ( step
, context
) {
3380 if ( step
!== undefined ) {
3381 this.next( step
, context
);
3387 OO
.initClass( OO
.ui
.Process
);
3392 * Start the process.
3394 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3395 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3396 * process, the remaining steps will not be taken
3398 OO
.ui
.Process
.prototype.execute = function () {
3399 var i
, len
, promise
;
3402 * Continue execution.
3405 * @param {Array} step A function and the context it should be called in
3406 * @return {Function} Function that continues the process
3408 function proceed( step
) {
3409 return function () {
3410 // Execute step in the correct context
3412 result
= step
.callback
.call( step
.context
);
3414 if ( result
=== false ) {
3415 // Use rejected promise for boolean false results
3416 return $.Deferred().reject( [] ).promise();
3418 if ( typeof result
=== 'number' ) {
3420 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3422 // Use a delayed promise for numbers, expecting them to be in milliseconds
3423 deferred
= $.Deferred();
3424 setTimeout( deferred
.resolve
, result
);
3425 return deferred
.promise();
3427 if ( result
instanceof OO
.ui
.Error
) {
3428 // Use rejected promise for error
3429 return $.Deferred().reject( [ result
] ).promise();
3431 if ( $.isArray( result
) && result
.length
&& result
[ 0 ] instanceof OO
.ui
.Error
) {
3432 // Use rejected promise for list of errors
3433 return $.Deferred().reject( result
).promise();
3435 // Duck-type the object to see if it can produce a promise
3436 if ( result
&& $.isFunction( result
.promise
) ) {
3437 // Use a promise generated from the result
3438 return result
.promise();
3440 // Use resolved promise for other results
3441 return $.Deferred().resolve().promise();
3445 if ( this.steps
.length
) {
3446 // Generate a chain reaction of promises
3447 promise
= proceed( this.steps
[ 0 ] )();
3448 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3449 promise
= promise
.then( proceed( this.steps
[ i
] ) );
3452 promise
= $.Deferred().resolve().promise();
3459 * Create a process step.
3462 * @param {number|jQuery.Promise|Function} step
3464 * - Number of milliseconds to wait; or
3465 * - Promise to wait to be resolved; or
3466 * - Function to execute
3467 * - If it returns boolean false the process will stop
3468 * - If it returns an object with a `promise` method the process will use the promise to either
3469 * continue to the next step when the promise is resolved or stop when the promise is rejected
3470 * - If it returns a number, the process will wait for that number of milliseconds before
3472 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3474 * @return {Object} Step object, with `callback` and `context` properties
3476 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3477 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3479 callback: function () {
3485 if ( $.isFunction( step
) ) {
3491 throw new Error( 'Cannot create process step: number, promise or function expected' );
3495 * Add step to the beginning of the process.
3497 * @inheritdoc #createStep
3498 * @return {OO.ui.Process} this
3501 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3502 this.steps
.unshift( this.createStep( step
, context
) );
3507 * Add step to the end of the process.
3509 * @inheritdoc #createStep
3510 * @return {OO.ui.Process} this
3513 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3514 this.steps
.push( this.createStep( step
, context
) );
3519 * Factory for tools.
3522 * @extends OO.Factory
3525 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3526 // Parent constructor
3527 OO
.ui
.ToolFactory
.super.call( this );
3532 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3537 * Get tools from the factory
3539 * @param {Array} include Included tools
3540 * @param {Array} exclude Excluded tools
3541 * @param {Array} promote Promoted tools
3542 * @param {Array} demote Demoted tools
3543 * @return {string[]} List of tools
3545 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3546 var i
, len
, included
, promoted
, demoted
,
3550 // Collect included and not excluded tools
3551 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3554 promoted
= this.extract( promote
, used
);
3555 demoted
= this.extract( demote
, used
);
3558 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3559 if ( !used
[ included
[ i
] ] ) {
3560 auto
.push( included
[ i
] );
3564 return promoted
.concat( auto
).concat( demoted
);
3568 * Get a flat list of names from a list of names or groups.
3570 * Tools can be specified in the following ways:
3572 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3573 * - All tools in a group: `{ group: 'group-name' }`
3574 * - All tools: `'*'`
3577 * @param {Array|string} collection List of tools
3578 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3579 * names will be added as properties
3580 * @return {string[]} List of extracted names
3582 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3583 var i
, len
, item
, name
, tool
,
3586 if ( collection
=== '*' ) {
3587 for ( name
in this.registry
) {
3588 tool
= this.registry
[ name
];
3590 // Only add tools by group name when auto-add is enabled
3591 tool
.static.autoAddToCatchall
&&
3592 // Exclude already used tools
3593 ( !used
|| !used
[ name
] )
3597 used
[ name
] = true;
3601 } else if ( $.isArray( collection
) ) {
3602 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3603 item
= collection
[ i
];
3604 // Allow plain strings as shorthand for named tools
3605 if ( typeof item
=== 'string' ) {
3606 item
= { name
: item
};
3608 if ( OO
.isPlainObject( item
) ) {
3610 for ( name
in this.registry
) {
3611 tool
= this.registry
[ name
];
3613 // Include tools with matching group
3614 tool
.static.group
=== item
.group
&&
3615 // Only add tools by group name when auto-add is enabled
3616 tool
.static.autoAddToGroup
&&
3617 // Exclude already used tools
3618 ( !used
|| !used
[ name
] )
3622 used
[ name
] = true;
3626 // Include tools with matching name and exclude already used tools
3627 } else if ( item
.name
&& ( !used
|| !used
[ item
.name
] ) ) {
3628 names
.push( item
.name
);
3630 used
[ item
.name
] = true;
3640 * Factory for tool groups.
3643 * @extends OO.Factory
3646 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3647 // Parent constructor
3648 OO
.Factory
.call( this );
3651 defaultClasses
= this.constructor.static.getDefaultClasses();
3653 // Register default toolgroups
3654 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3655 this.register( defaultClasses
[ i
] );
3661 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3663 /* Static Methods */
3666 * Get a default set of classes to be registered on construction
3668 * @return {Function[]} Default classes
3670 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3673 OO
.ui
.ListToolGroup
,
3685 * @param {Object} [config] Configuration options
3687 OO
.ui
.Theme
= function OoUiTheme( config
) {
3688 // Configuration initialization
3689 config
= config
|| {};
3694 OO
.initClass( OO
.ui
.Theme
);
3699 * Get a list of classes to be applied to a widget.
3701 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3702 * otherwise state transitions will not work properly.
3704 * @param {OO.ui.Element} element Element for which to get classes
3705 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3707 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3708 return { on
: [], off
: [] };
3712 * Update CSS classes provided by the theme.
3714 * For elements with theme logic hooks, this should be called any time there's a state change.
3716 * @param {OO.ui.Element} element Element for which to update classes
3717 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3719 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3720 var classes
= this.getElementClasses( element
);
3723 .removeClass( classes
.off
.join( ' ' ) )
3724 .addClass( classes
.on
.join( ' ' ) );
3728 * Element supporting "sequential focus navigation" using the 'tabindex' attribute.
3734 * @param {Object} [config] Configuration options
3735 * @cfg {jQuery} [$tabIndexed] tabIndexed node, assigned to #$tabIndexed, omit to use #$element
3736 * @cfg {number|Function} [tabIndex=0] Tab index value. Use 0 to use default ordering, use -1 to
3737 * prevent tab focusing. (default: 0)
3739 OO
.ui
.TabIndexedElement
= function OoUiTabIndexedElement( config
) {
3740 // Configuration initialization
3741 config
= config
|| {};
3744 this.$tabIndexed
= null;
3745 this.tabIndex
= null;
3748 this.connect( this, { disable
: 'onDisable' } );
3751 this.setTabIndex( config
.tabIndex
|| 0 );
3752 this.setTabIndexedElement( config
.$tabIndexed
|| this.$element
);
3757 OO
.initClass( OO
.ui
.TabIndexedElement
);
3762 * Set the element with 'tabindex' attribute.
3764 * If an element is already set, it will be cleaned up before setting up the new element.
3766 * @param {jQuery} $tabIndexed Element to set tab index on
3768 OO
.ui
.TabIndexedElement
.prototype.setTabIndexedElement = function ( $tabIndexed
) {
3769 if ( this.$tabIndexed
) {
3770 this.$tabIndexed
.removeAttr( 'tabindex aria-disabled' );
3773 this.$tabIndexed
= $tabIndexed
;
3774 if ( this.tabIndex
!== null ) {
3775 this.$tabIndexed
.attr( {
3776 // Do not index over disabled elements
3777 tabindex
: this.isDisabled() ? -1 : this.tabIndex
,
3778 // ChromeVox and NVDA do not seem to inherit this from parent elements
3779 'aria-disabled': this.isDisabled().toString()
3785 * Set tab index value.
3787 * @param {number|null} tabIndex Tab index value or null for no tabIndex
3790 OO
.ui
.TabIndexedElement
.prototype.setTabIndex = function ( tabIndex
) {
3791 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3793 if ( this.tabIndex
!== tabIndex
) {
3794 if ( this.$tabIndexed
) {
3795 if ( tabIndex
!== null ) {
3796 this.$tabIndexed
.attr( {
3797 // Do not index over disabled elements
3798 tabindex
: this.isDisabled() ? -1 : tabIndex
,
3799 // ChromeVox and NVDA do not seem to inherit this from parent elements
3800 'aria-disabled': this.isDisabled().toString()
3803 this.$tabIndexed
.removeAttr( 'tabindex aria-disabled' );
3806 this.tabIndex
= tabIndex
;
3813 * Handle disable events.
3815 * @param {boolean} disabled Element is disabled
3817 OO
.ui
.TabIndexedElement
.prototype.onDisable = function ( disabled
) {
3818 if ( this.$tabIndexed
&& this.tabIndex
!== null ) {
3819 this.$tabIndexed
.attr( {
3820 // Do not index over disabled elements
3821 tabindex
: disabled
? -1 : this.tabIndex
,
3822 // ChromeVox and NVDA do not seem to inherit this from parent elements
3823 'aria-disabled': disabled
.toString()
3829 * Get tab index value.
3831 * @return {number} Tab index value
3833 OO
.ui
.TabIndexedElement
.prototype.getTabIndex = function () {
3834 return this.tabIndex
;
3838 * Element with a button.
3840 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3841 * and access keys for accessibility purposes.
3847 * @param {Object} [config] Configuration options
3848 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3849 * @cfg {boolean} [framed=true] Render button with a frame
3850 * @cfg {string} [accessKey] Button's access key
3852 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3853 // Configuration initialization
3854 config
= config
|| {};
3857 this.$button
= config
.$button
|| this.$( '<a>' );
3859 this.accessKey
= null;
3860 this.active
= false;
3861 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3862 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3865 this.$element
.addClass( 'oo-ui-buttonElement' );
3866 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3867 this.setAccessKey( config
.accessKey
);
3868 this.setButtonElement( this.$button
);
3873 OO
.initClass( OO
.ui
.ButtonElement
);
3875 /* Static Properties */
3878 * Cancel mouse down events.
3882 * @property {boolean}
3884 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3889 * Set the button element.
3891 * If an element is already set, it will be cleaned up before setting up the new element.
3893 * @param {jQuery} $button Element to use as button
3895 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3896 if ( this.$button
) {
3898 .removeClass( 'oo-ui-buttonElement-button' )
3899 .removeAttr( 'role accesskey' )
3900 .off( 'mousedown', this.onMouseDownHandler
);
3903 this.$button
= $button
3904 .addClass( 'oo-ui-buttonElement-button' )
3905 .attr( { role
: 'button', accesskey
: this.accessKey
} )
3906 .on( 'mousedown', this.onMouseDownHandler
);
3910 * Handles mouse down events.
3912 * @param {jQuery.Event} e Mouse down event
3914 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3915 if ( this.isDisabled() || e
.which
!== 1 ) {
3918 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3919 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3920 // reliably remove the pressed class
3921 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3922 // Prevent change of focus unless specifically configured otherwise
3923 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3929 * Handles mouse up events.
3931 * @param {jQuery.Event} e Mouse up event
3933 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3934 if ( this.isDisabled() || e
.which
!== 1 ) {
3937 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3938 // Stop listening for mouseup, since we only needed this once
3939 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3943 * Check if button has a frame.
3945 * @return {boolean} Button is framed
3947 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3954 * @param {boolean} [framed] Make button framed, omit to toggle
3957 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3958 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3959 if ( framed
!== this.framed
) {
3960 this.framed
= framed
;
3962 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3963 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3964 this.updateThemeClasses();
3973 * @param {string} accessKey Button's access key, use empty string to remove
3976 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3977 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3979 if ( this.accessKey
!== accessKey
) {
3980 if ( this.$button
) {
3981 if ( accessKey
!== null ) {
3982 this.$button
.attr( 'accesskey', accessKey
);
3984 this.$button
.removeAttr( 'accesskey' );
3987 this.accessKey
= accessKey
;
3996 * @param {boolean} [value] Make button active
3999 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
4000 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
4005 * Element containing a sequence of child elements.
4011 * @param {Object} [config] Configuration options
4012 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
4014 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
4015 // Configuration initialization
4016 config
= config
|| {};
4021 this.aggregateItemEvents
= {};
4024 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
4030 * Set the group element.
4032 * If an element is already set, items will be moved to the new element.
4034 * @param {jQuery} $group Element to use as group
4036 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
4039 this.$group
= $group
;
4040 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4041 this.$group
.append( this.items
[ i
].$element
);
4046 * Check if there are no items.
4048 * @return {boolean} Group is empty
4050 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
4051 return !this.items
.length
;
4057 * @return {OO.ui.Element[]} Items
4059 OO
.ui
.GroupElement
.prototype.getItems = function () {
4060 return this.items
.slice( 0 );
4064 * Get an item by its data.
4066 * Data is compared by a hash of its value. Only the first item with matching data will be returned.
4068 * @param {Object} data Item data to search for
4069 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
4071 OO
.ui
.GroupElement
.prototype.getItemFromData = function ( data
) {
4073 hash
= OO
.getHash( data
);
4075 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4076 item
= this.items
[ i
];
4077 if ( hash
=== OO
.getHash( item
.getData() ) ) {
4086 * Get items by their data.
4088 * Data is compared by a hash of its value. All items with matching data will be returned.
4090 * @param {Object} data Item data to search for
4091 * @return {OO.ui.Element[]} Items with equivalent data
4093 OO
.ui
.GroupElement
.prototype.getItemsFromData = function ( data
) {
4095 hash
= OO
.getHash( data
),
4098 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4099 item
= this.items
[ i
];
4100 if ( hash
=== OO
.getHash( item
.getData() ) ) {
4109 * Add an aggregate item event.
4111 * Aggregated events are listened to on each item and then emitted by the group under a new name,
4112 * and with an additional leading parameter containing the item that emitted the original event.
4113 * Other arguments that were emitted from the original event are passed through.
4115 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
4116 * event, use null value to remove aggregation
4117 * @throws {Error} If aggregation already exists
4119 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
4120 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
4122 for ( itemEvent
in events
) {
4123 groupEvent
= events
[ itemEvent
];
4125 // Remove existing aggregated event
4126 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4127 // Don't allow duplicate aggregations
4129 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
4131 // Remove event aggregation from existing items
4132 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4133 item
= this.items
[ i
];
4134 if ( item
.connect
&& item
.disconnect
) {
4136 remove
[ itemEvent
] = [ 'emit', groupEvent
, item
];
4137 item
.disconnect( this, remove
);
4140 // Prevent future items from aggregating event
4141 delete this.aggregateItemEvents
[ itemEvent
];
4144 // Add new aggregate event
4146 // Make future items aggregate event
4147 this.aggregateItemEvents
[ itemEvent
] = groupEvent
;
4148 // Add event aggregation to existing items
4149 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4150 item
= this.items
[ i
];
4151 if ( item
.connect
&& item
.disconnect
) {
4153 add
[ itemEvent
] = [ 'emit', groupEvent
, item
];
4154 item
.connect( this, add
);
4164 * Adding an existing item will move it.
4166 * @param {OO.ui.Element[]} items Items
4167 * @param {number} [index] Index to insert items at
4170 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
4171 var i
, len
, item
, event
, events
, currentIndex
,
4174 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4177 // Check if item exists then remove it first, effectively "moving" it
4178 currentIndex
= $.inArray( item
, this.items
);
4179 if ( currentIndex
>= 0 ) {
4180 this.removeItems( [ item
] );
4181 // Adjust index to compensate for removal
4182 if ( currentIndex
< index
) {
4187 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
4189 for ( event
in this.aggregateItemEvents
) {
4190 events
[ event
] = [ 'emit', this.aggregateItemEvents
[ event
], item
];
4192 item
.connect( this, events
);
4194 item
.setElementGroup( this );
4195 itemElements
.push( item
.$element
.get( 0 ) );
4198 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
4199 this.$group
.append( itemElements
);
4200 this.items
.push
.apply( this.items
, items
);
4201 } else if ( index
=== 0 ) {
4202 this.$group
.prepend( itemElements
);
4203 this.items
.unshift
.apply( this.items
, items
);
4205 this.items
[ index
].$element
.before( itemElements
);
4206 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
4215 * Items will be detached, not removed, so they can be used later.
4217 * @param {OO.ui.Element[]} items Items to remove
4220 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
4221 var i
, len
, item
, index
, remove
, itemEvent
;
4223 // Remove specific items
4224 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4226 index
= $.inArray( item
, this.items
);
4227 if ( index
!== -1 ) {
4229 item
.connect
&& item
.disconnect
&&
4230 !$.isEmptyObject( this.aggregateItemEvents
)
4233 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4234 remove
[ itemEvent
] = [ 'emit', this.aggregateItemEvents
[ itemEvent
], item
];
4236 item
.disconnect( this, remove
);
4238 item
.setElementGroup( null );
4239 this.items
.splice( index
, 1 );
4240 item
.$element
.detach();
4250 * Items will be detached, not removed, so they can be used later.
4254 OO
.ui
.GroupElement
.prototype.clearItems = function () {
4255 var i
, len
, item
, remove
, itemEvent
;
4258 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4259 item
= this.items
[ i
];
4261 item
.connect
&& item
.disconnect
&&
4262 !$.isEmptyObject( this.aggregateItemEvents
)
4265 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4266 remove
[ itemEvent
] = [ 'emit', this.aggregateItemEvents
[ itemEvent
], item
];
4268 item
.disconnect( this, remove
);
4270 item
.setElementGroup( null );
4271 item
.$element
.detach();
4279 * A mixin for an element that can be dragged and dropped.
4280 * Use in conjunction with DragGroupWidget
4287 OO
.ui
.DraggableElement
= function OoUiDraggableElement() {
4291 // Initialize and events
4293 .attr( 'draggable', true )
4294 .addClass( 'oo-ui-draggableElement' )
4296 dragstart
: this.onDragStart
.bind( this ),
4297 dragover
: this.onDragOver
.bind( this ),
4298 dragend
: this.onDragEnd
.bind( this ),
4299 drop
: this.onDrop
.bind( this )
4307 * @param {OO.ui.DraggableElement} item Dragging item
4321 * Respond to dragstart event.
4322 * @param {jQuery.Event} event jQuery event
4325 OO
.ui
.DraggableElement
.prototype.onDragStart = function ( e
) {
4326 var dataTransfer
= e
.originalEvent
.dataTransfer
;
4327 // Define drop effect
4328 dataTransfer
.dropEffect
= 'none';
4329 dataTransfer
.effectAllowed
= 'move';
4330 // We must set up a dataTransfer data property or Firefox seems to
4331 // ignore the fact the element is draggable.
4333 dataTransfer
.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
4335 // The above is only for firefox. No need to set a catch clause
4336 // if it fails, move on.
4338 // Add dragging class
4339 this.$element
.addClass( 'oo-ui-draggableElement-dragging' );
4341 this.emit( 'dragstart', this );
4346 * Respond to dragend event.
4349 OO
.ui
.DraggableElement
.prototype.onDragEnd = function () {
4350 this.$element
.removeClass( 'oo-ui-draggableElement-dragging' );
4351 this.emit( 'dragend' );
4355 * Handle drop event.
4356 * @param {jQuery.Event} event jQuery event
4359 OO
.ui
.DraggableElement
.prototype.onDrop = function ( e
) {
4361 this.emit( 'drop', e
);
4365 * In order for drag/drop to work, the dragover event must
4366 * return false and stop propogation.
4368 OO
.ui
.DraggableElement
.prototype.onDragOver = function ( e
) {
4374 * Store it in the DOM so we can access from the widget drag event
4375 * @param {number} Item index
4377 OO
.ui
.DraggableElement
.prototype.setIndex = function ( index
) {
4378 if ( this.index
!== index
) {
4380 this.$element
.data( 'index', index
);
4386 * @return {number} Item index
4388 OO
.ui
.DraggableElement
.prototype.getIndex = function () {
4393 * Element containing a sequence of child elements that can be dragged
4400 * @param {Object} [config] Configuration options
4401 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
4402 * @cfg {string} [orientation] Item orientation, 'horizontal' or 'vertical'. Defaults to 'vertical'
4404 OO
.ui
.DraggableGroupElement
= function OoUiDraggableGroupElement( config
) {
4405 // Configuration initialization
4406 config
= config
|| {};
4408 // Parent constructor
4409 OO
.ui
.GroupElement
.call( this, config
);
4412 this.orientation
= config
.orientation
|| 'vertical';
4413 this.dragItem
= null;
4414 this.itemDragOver
= null;
4416 this.sideInsertion
= '';
4420 dragstart
: 'itemDragStart',
4421 dragend
: 'itemDragEnd',
4424 this.connect( this, {
4425 itemDragStart
: 'onItemDragStart',
4426 itemDrop
: 'onItemDrop',
4427 itemDragEnd
: 'onItemDragEnd'
4430 dragover
: $.proxy( this.onDragOver
, this ),
4431 dragleave
: $.proxy( this.onDragLeave
, this )
4435 if ( $.isArray( config
.items
) ) {
4436 this.addItems( config
.items
);
4438 this.$placeholder
= $( '<div>' )
4439 .addClass( 'oo-ui-draggableGroupElement-placeholder' );
4441 .addClass( 'oo-ui-draggableGroupElement' )
4442 .append( this.$status
)
4443 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' )
4444 .prepend( this.$placeholder
);
4448 OO
.mixinClass( OO
.ui
.DraggableGroupElement
, OO
.ui
.GroupElement
);
4454 * @param {OO.ui.DraggableElement} item Reordered item
4455 * @param {number} [newIndex] New index for the item
4461 * Respond to item drag start event
4462 * @param {OO.ui.DraggableElement} item Dragged item
4464 OO
.ui
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
4467 // Map the index of each object
4468 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4469 this.items
[ i
].setIndex( i
);
4472 if ( this.orientation
=== 'horizontal' ) {
4473 // Set the height of the indicator
4474 this.$placeholder
.css( {
4475 height
: item
.$element
.outerHeight(),
4479 // Set the width of the indicator
4480 this.$placeholder
.css( {
4482 width
: item
.$element
.outerWidth()
4485 this.setDragItem( item
);
4489 * Respond to item drag end event
4491 OO
.ui
.DraggableGroupElement
.prototype.onItemDragEnd = function () {
4492 this.unsetDragItem();
4497 * Handle drop event and switch the order of the items accordingly
4498 * @param {OO.ui.DraggableElement} item Dropped item
4501 OO
.ui
.DraggableGroupElement
.prototype.onItemDrop = function ( item
) {
4502 var toIndex
= item
.getIndex();
4503 // Check if the dropped item is from the current group
4504 // TODO: Figure out a way to configure a list of legally droppable
4505 // elements even if they are not yet in the list
4506 if ( this.getDragItem() ) {
4507 // If the insertion point is 'after', the insertion index
4508 // is shifted to the right (or to the left in RTL, hence 'after')
4509 if ( this.sideInsertion
=== 'after' ) {
4512 // Emit change event
4513 this.emit( 'reorder', this.getDragItem(), toIndex
);
4515 // Return false to prevent propogation
4520 * Handle dragleave event.
4522 OO
.ui
.DraggableGroupElement
.prototype.onDragLeave = function () {
4523 // This means the item was dragged outside the widget
4526 .addClass( 'oo-ui-element-hidden' );
4530 * Respond to dragover event
4531 * @param {jQuery.Event} event Event details
4533 OO
.ui
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
4534 var dragOverObj
, $optionWidget
, itemOffset
, itemMidpoint
, itemBoundingRect
,
4535 itemSize
, cssOutput
, dragPosition
, itemIndex
, itemPosition
,
4536 clientX
= e
.originalEvent
.clientX
,
4537 clientY
= e
.originalEvent
.clientY
;
4539 // Get the OptionWidget item we are dragging over
4540 dragOverObj
= this.getElementDocument().elementFromPoint( clientX
, clientY
);
4541 $optionWidget
= $( dragOverObj
).closest( '.oo-ui-draggableElement' );
4542 if ( $optionWidget
[ 0 ] ) {
4543 itemOffset
= $optionWidget
.offset();
4544 itemBoundingRect
= $optionWidget
[ 0 ].getBoundingClientRect();
4545 itemPosition
= $optionWidget
.position();
4546 itemIndex
= $optionWidget
.data( 'index' );
4551 this.isDragging() &&
4552 itemIndex
!== this.getDragItem().getIndex()
4554 if ( this.orientation
=== 'horizontal' ) {
4555 // Calculate where the mouse is relative to the item width
4556 itemSize
= itemBoundingRect
.width
;
4557 itemMidpoint
= itemBoundingRect
.left
+ itemSize
/ 2;
4558 dragPosition
= clientX
;
4559 // Which side of the item we hover over will dictate
4560 // where the placeholder will appear, on the left or
4563 left
: dragPosition
< itemMidpoint
? itemPosition
.left
: itemPosition
.left
+ itemSize
,
4564 top
: itemPosition
.top
4567 // Calculate where the mouse is relative to the item height
4568 itemSize
= itemBoundingRect
.height
;
4569 itemMidpoint
= itemBoundingRect
.top
+ itemSize
/ 2;
4570 dragPosition
= clientY
;
4571 // Which side of the item we hover over will dictate
4572 // where the placeholder will appear, on the top or
4575 top
: dragPosition
< itemMidpoint
? itemPosition
.top
: itemPosition
.top
+ itemSize
,
4576 left
: itemPosition
.left
4579 // Store whether we are before or after an item to rearrange
4580 // For horizontal layout, we need to account for RTL, as this is flipped
4581 if ( this.orientation
=== 'horizontal' && this.$element
.css( 'direction' ) === 'rtl' ) {
4582 this.sideInsertion
= dragPosition
< itemMidpoint
? 'after' : 'before';
4584 this.sideInsertion
= dragPosition
< itemMidpoint
? 'before' : 'after';
4586 // Add drop indicator between objects
4587 if ( this.sideInsertion
) {
4590 .removeClass( 'oo-ui-element-hidden' );
4597 .addClass( 'oo-ui-element-hidden' );
4600 // This means the item was dragged outside the widget
4603 .addClass( 'oo-ui-element-hidden' );
4610 * Set a dragged item
4611 * @param {OO.ui.DraggableElement} item Dragged item
4613 OO
.ui
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
4614 this.dragItem
= item
;
4618 * Unset the current dragged item
4620 OO
.ui
.DraggableGroupElement
.prototype.unsetDragItem = function () {
4621 this.dragItem
= null;
4622 this.itemDragOver
= null;
4623 this.$placeholder
.addClass( 'oo-ui-element-hidden' );
4624 this.sideInsertion
= '';
4628 * Get the current dragged item
4629 * @return {OO.ui.DraggableElement|null} item Dragged item or null if no item is dragged
4631 OO
.ui
.DraggableGroupElement
.prototype.getDragItem = function () {
4632 return this.dragItem
;
4636 * Check if there's an item being dragged.
4637 * @return {Boolean} Item is being dragged
4639 OO
.ui
.DraggableGroupElement
.prototype.isDragging = function () {
4640 return this.getDragItem() !== null;
4644 * Element containing an icon.
4646 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4647 * a control or convey information in a more space efficient way. Icons should rarely be used
4648 * without labels; such as in a toolbar where space is at a premium or within a context where the
4649 * meaning is very clear to the user.
4655 * @param {Object} [config] Configuration options
4656 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4657 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4658 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4660 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4662 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4663 // Configuration initialization
4664 config
= config
|| {};
4669 this.iconTitle
= null;
4672 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4673 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4674 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4679 OO
.initClass( OO
.ui
.IconElement
);
4681 /* Static Properties */
4686 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4688 * For i18n purposes, this property can be an object containing a `default` icon name property and
4689 * additional icon names keyed by language code.
4691 * Example of i18n icon definition:
4692 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4696 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4697 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4700 OO
.ui
.IconElement
.static.icon
= null;
4707 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4710 OO
.ui
.IconElement
.static.iconTitle
= null;
4715 * Set the icon element.
4717 * If an element is already set, it will be cleaned up before setting up the new element.
4719 * @param {jQuery} $icon Element to use as icon
4721 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4724 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4725 .removeAttr( 'title' );
4729 .addClass( 'oo-ui-iconElement-icon' )
4730 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4731 if ( this.iconTitle
!== null ) {
4732 this.$icon
.attr( 'title', this.iconTitle
);
4739 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4740 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4741 * language, use null to remove icon
4744 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4745 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4746 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4748 if ( this.icon
!== icon
) {
4750 if ( this.icon
!== null ) {
4751 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4753 if ( icon
!== null ) {
4754 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4760 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4761 this.updateThemeClasses();
4769 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4773 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4774 iconTitle
= typeof iconTitle
=== 'function' ||
4775 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4776 OO
.ui
.resolveMsg( iconTitle
) : null;
4778 if ( this.iconTitle
!== iconTitle
) {
4779 this.iconTitle
= iconTitle
;
4781 if ( this.iconTitle
!== null ) {
4782 this.$icon
.attr( 'title', iconTitle
);
4784 this.$icon
.removeAttr( 'title' );
4795 * @return {string} Icon name
4797 OO
.ui
.IconElement
.prototype.getIcon = function () {
4804 * @return {string} Icon title text
4806 OO
.ui
.IconElement
.prototype.getIconTitle = function () {
4807 return this.iconTitle
;
4811 * Element containing an indicator.
4813 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4814 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4815 * instead of performing an action directly, or an item in a list which has errors that need to be
4822 * @param {Object} [config] Configuration options
4823 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4825 * @cfg {string} [indicator] Symbolic indicator name
4826 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4828 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4829 // Configuration initialization
4830 config
= config
|| {};
4833 this.$indicator
= null;
4834 this.indicator
= null;
4835 this.indicatorTitle
= null;
4838 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4839 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4840 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4845 OO
.initClass( OO
.ui
.IndicatorElement
);
4847 /* Static Properties */
4854 * @property {string|null} Symbolic indicator name
4856 OO
.ui
.IndicatorElement
.static.indicator
= null;
4863 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4866 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4871 * Set the indicator element.
4873 * If an element is already set, it will be cleaned up before setting up the new element.
4875 * @param {jQuery} $indicator Element to use as indicator
4877 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4878 if ( this.$indicator
) {
4880 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4881 .removeAttr( 'title' );
4884 this.$indicator
= $indicator
4885 .addClass( 'oo-ui-indicatorElement-indicator' )
4886 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4887 if ( this.indicatorTitle
!== null ) {
4888 this.$indicator
.attr( 'title', this.indicatorTitle
);
4893 * Set indicator name.
4895 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4898 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4899 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4901 if ( this.indicator
!== indicator
) {
4902 if ( this.$indicator
) {
4903 if ( this.indicator
!== null ) {
4904 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4906 if ( indicator
!== null ) {
4907 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4910 this.indicator
= indicator
;
4913 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4914 this.updateThemeClasses();
4920 * Set indicator title.
4922 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4923 * null for no indicator title
4926 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4927 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4928 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4929 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4931 if ( this.indicatorTitle
!== indicatorTitle
) {
4932 this.indicatorTitle
= indicatorTitle
;
4933 if ( this.$indicator
) {
4934 if ( this.indicatorTitle
!== null ) {
4935 this.$indicator
.attr( 'title', indicatorTitle
);
4937 this.$indicator
.removeAttr( 'title' );
4946 * Get indicator name.
4948 * @return {string} Symbolic name of indicator
4950 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4951 return this.indicator
;
4955 * Get indicator title.
4957 * @return {string} Indicator title text
4959 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4960 return this.indicatorTitle
;
4964 * Element containing a label.
4970 * @param {Object} [config] Configuration options
4971 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4972 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4973 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4975 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4976 // Configuration initialization
4977 config
= config
|| {};
4982 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4985 this.setLabel( config
.label
|| this.constructor.static.label
);
4986 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4991 OO
.initClass( OO
.ui
.LabelElement
);
4996 * @event labelChange
4997 * @param {string} value
5000 /* Static Properties */
5007 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
5010 OO
.ui
.LabelElement
.static.label
= null;
5015 * Set the label element.
5017 * If an element is already set, it will be cleaned up before setting up the new element.
5019 * @param {jQuery} $label Element to use as label
5021 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
5022 if ( this.$label
) {
5023 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
5026 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
5027 this.setLabelContent( this.label
);
5033 * An empty string will result in the label being hidden. A string containing only whitespace will
5034 * be converted to a single ` `.
5036 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
5037 * text; or null for no label
5040 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
5041 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
5042 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
5044 this.$element
.toggleClass( 'oo-ui-labelElement', !!label
);
5046 if ( this.label
!== label
) {
5047 if ( this.$label
) {
5048 this.setLabelContent( label
);
5051 this.emit( 'labelChange' );
5060 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
5061 * text; or null for no label
5063 OO
.ui
.LabelElement
.prototype.getLabel = function () {
5072 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
5073 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
5074 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
5081 * Set the content of the label.
5083 * Do not call this method until after the label element has been set by #setLabelElement.
5086 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
5087 * text; or null for no label
5089 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
5090 if ( typeof label
=== 'string' ) {
5091 if ( label
.match( /^\s*$/ ) ) {
5092 // Convert whitespace only string to a single non-breaking space
5093 this.$label
.html( ' ' );
5095 this.$label
.text( label
);
5097 } else if ( label
instanceof jQuery
) {
5098 this.$label
.empty().append( label
);
5100 this.$label
.empty();
5105 * Mixin that adds a menu showing suggested values for a OO.ui.TextInputWidget.
5107 * Subclasses that set the value of #lookupInput from #onLookupMenuItemChoose should
5108 * be aware that this will cause new suggestions to be looked up for the new value. If this is
5109 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
5115 * @param {Object} [config] Configuration options
5116 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
5117 * @cfg {jQuery} [$container=this.$element] Element to render menu under
5119 OO
.ui
.LookupElement
= function OoUiLookupElement( config
) {
5120 // Configuration initialization
5121 config
= config
|| {};
5124 this.$overlay
= config
.$overlay
|| this.$element
;
5125 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
5126 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
5127 $container
: config
.$container
5129 this.lookupCache
= {};
5130 this.lookupQuery
= null;
5131 this.lookupRequest
= null;
5132 this.lookupsDisabled
= false;
5133 this.lookupInputFocused
= false;
5137 focus
: this.onLookupInputFocus
.bind( this ),
5138 blur
: this.onLookupInputBlur
.bind( this ),
5139 mousedown
: this.onLookupInputMouseDown
.bind( this )
5141 this.connect( this, { change
: 'onLookupInputChange' } );
5142 this.lookupMenu
.connect( this, {
5143 toggle
: 'onLookupMenuToggle',
5144 choose
: 'onLookupMenuItemChoose'
5148 this.$element
.addClass( 'oo-ui-lookupElement' );
5149 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
5150 this.$overlay
.append( this.lookupMenu
.$element
);
5156 * Handle input focus event.
5158 * @param {jQuery.Event} e Input focus event
5160 OO
.ui
.LookupElement
.prototype.onLookupInputFocus = function () {
5161 this.lookupInputFocused
= true;
5162 this.populateLookupMenu();
5166 * Handle input blur event.
5168 * @param {jQuery.Event} e Input blur event
5170 OO
.ui
.LookupElement
.prototype.onLookupInputBlur = function () {
5171 this.closeLookupMenu();
5172 this.lookupInputFocused
= false;
5176 * Handle input mouse down event.
5178 * @param {jQuery.Event} e Input mouse down event
5180 OO
.ui
.LookupElement
.prototype.onLookupInputMouseDown = function () {
5181 // Only open the menu if the input was already focused.
5182 // This way we allow the user to open the menu again after closing it with Esc
5183 // by clicking in the input. Opening (and populating) the menu when initially
5184 // clicking into the input is handled by the focus handler.
5185 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
5186 this.populateLookupMenu();
5191 * Handle input change event.
5193 * @param {string} value New input value
5195 OO
.ui
.LookupElement
.prototype.onLookupInputChange = function () {
5196 if ( this.lookupInputFocused
) {
5197 this.populateLookupMenu();
5202 * Handle the lookup menu being shown/hidden.
5204 * @param {boolean} visible Whether the lookup menu is now visible.
5206 OO
.ui
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
5208 // When the menu is hidden, abort any active request and clear the menu.
5209 // This has to be done here in addition to closeLookupMenu(), because
5210 // MenuSelectWidget will close itself when the user presses Esc.
5211 this.abortLookupRequest();
5212 this.lookupMenu
.clearItems();
5217 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
5219 * @param {OO.ui.MenuOptionWidget|null} item Selected item
5221 OO
.ui
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
5223 this.setValue( item
.getData() );
5230 * @return {OO.ui.TextInputMenuSelectWidget}
5232 OO
.ui
.LookupElement
.prototype.getLookupMenu = function () {
5233 return this.lookupMenu
;
5237 * Disable or re-enable lookups.
5239 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
5241 * @param {boolean} disabled Disable lookups
5243 OO
.ui
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
5244 this.lookupsDisabled
= !!disabled
;
5248 * Open the menu. If there are no entries in the menu, this does nothing.
5252 OO
.ui
.LookupElement
.prototype.openLookupMenu = function () {
5253 if ( !this.lookupMenu
.isEmpty() ) {
5254 this.lookupMenu
.toggle( true );
5260 * Close the menu, empty it, and abort any pending request.
5264 OO
.ui
.LookupElement
.prototype.closeLookupMenu = function () {
5265 this.lookupMenu
.toggle( false );
5266 this.abortLookupRequest();
5267 this.lookupMenu
.clearItems();
5272 * Request menu items based on the input's current value, and when they arrive,
5273 * populate the menu with these items and show the menu.
5275 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
5279 OO
.ui
.LookupElement
.prototype.populateLookupMenu = function () {
5281 value
= this.getValue();
5283 if ( this.lookupsDisabled
) {
5287 // If the input is empty, clear the menu
5288 if ( value
=== '' ) {
5289 this.closeLookupMenu();
5290 // Skip population if there is already a request pending for the current value
5291 } else if ( value
!== this.lookupQuery
) {
5292 this.getLookupMenuItems()
5293 .done( function ( items
) {
5294 widget
.lookupMenu
.clearItems();
5295 if ( items
.length
) {
5299 widget
.initializeLookupMenuSelection();
5301 widget
.lookupMenu
.toggle( false );
5304 .fail( function () {
5305 widget
.lookupMenu
.clearItems();
5313 * Select and highlight the first selectable item in the menu.
5317 OO
.ui
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
5318 if ( !this.lookupMenu
.getSelectedItem() ) {
5319 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
5321 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
5325 * Get lookup menu items for the current query.
5327 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
5328 * the done event. If the request was aborted to make way for a subsequent request, this promise
5329 * will not be rejected: it will remain pending forever.
5331 OO
.ui
.LookupElement
.prototype.getLookupMenuItems = function () {
5333 value
= this.getValue(),
5334 deferred
= $.Deferred(),
5337 this.abortLookupRequest();
5338 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
5339 deferred
.resolve( this.getLookupMenuOptionsFromData( this.lookupCache
[ value
] ) );
5342 this.lookupQuery
= value
;
5343 ourRequest
= this.lookupRequest
= this.getLookupRequest();
5345 .always( function () {
5346 // We need to pop pending even if this is an old request, otherwise
5347 // the widget will remain pending forever.
5348 // TODO: this assumes that an aborted request will fail or succeed soon after
5349 // being aborted, or at least eventually. It would be nice if we could popPending()
5350 // at abort time, but only if we knew that we hadn't already called popPending()
5351 // for that request.
5352 widget
.popPending();
5354 .done( function ( data
) {
5355 // If this is an old request (and aborting it somehow caused it to still succeed),
5356 // ignore its success completely
5357 if ( ourRequest
=== widget
.lookupRequest
) {
5358 widget
.lookupQuery
= null;
5359 widget
.lookupRequest
= null;
5360 widget
.lookupCache
[ value
] = widget
.getLookupCacheDataFromResponse( data
);
5361 deferred
.resolve( widget
.getLookupMenuOptionsFromData( widget
.lookupCache
[ value
] ) );
5364 .fail( function () {
5365 // If this is an old request (or a request failing because it's being aborted),
5366 // ignore its failure completely
5367 if ( ourRequest
=== widget
.lookupRequest
) {
5368 widget
.lookupQuery
= null;
5369 widget
.lookupRequest
= null;
5374 return deferred
.promise();
5378 * Abort the currently pending lookup request, if any.
5380 OO
.ui
.LookupElement
.prototype.abortLookupRequest = function () {
5381 var oldRequest
= this.lookupRequest
;
5383 // First unset this.lookupRequest to the fail handler will notice
5384 // that the request is no longer current
5385 this.lookupRequest
= null;
5386 this.lookupQuery
= null;
5392 * Get a new request object of the current lookup query value.
5395 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
5397 OO
.ui
.LookupElement
.prototype.getLookupRequest = function () {
5398 // Stub, implemented in subclass
5403 * Pre-process data returned by the request from #getLookupRequest.
5405 * The return value of this function will be cached, and any further queries for the given value
5406 * will use the cache rather than doing API requests.
5409 * @param {Mixed} data Response from server
5410 * @return {Mixed} Cached result data
5412 OO
.ui
.LookupElement
.prototype.getLookupCacheDataFromResponse = function () {
5413 // Stub, implemented in subclass
5418 * Get a list of menu option widgets from the (possibly cached) data returned by
5419 * #getLookupCacheDataFromResponse.
5422 * @param {Mixed} data Cached result data, usually an array
5423 * @return {OO.ui.MenuOptionWidget[]} Menu items
5425 OO
.ui
.LookupElement
.prototype.getLookupMenuOptionsFromData = function () {
5426 // Stub, implemented in subclass
5431 * Element containing an OO.ui.PopupWidget object.
5437 * @param {Object} [config] Configuration options
5438 * @cfg {Object} [popup] Configuration to pass to popup
5439 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
5441 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
5442 // Configuration initialization
5443 config
= config
|| {};
5446 this.popup
= new OO
.ui
.PopupWidget( $.extend(
5447 { autoClose
: true },
5449 { $: this.$, $autoCloseIgnore
: this.$element
}
5458 * @return {OO.ui.PopupWidget} Popup widget
5460 OO
.ui
.PopupElement
.prototype.getPopup = function () {
5465 * Element with named flags that can be added, removed, listed and checked.
5467 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
5468 * the flag name. Flags are primarily useful for styling.
5474 * @param {Object} [config] Configuration options
5475 * @cfg {string|string[]} [flags] Flags describing importance and functionality, e.g. 'primary',
5476 * 'safe', 'progressive', 'destructive' or 'constructive'
5477 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
5479 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
5480 // Configuration initialization
5481 config
= config
|| {};
5485 this.$flagged
= null;
5488 this.setFlags( config
.flags
);
5489 this.setFlaggedElement( config
.$flagged
|| this.$element
);
5496 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
5497 * added/removed properties
5503 * Set the flagged element.
5505 * If an element is already set, it will be cleaned up before setting up the new element.
5507 * @param {jQuery} $flagged Element to add flags to
5509 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
5510 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
5511 return 'oo-ui-flaggedElement-' + flag
;
5514 if ( this.$flagged
) {
5515 this.$flagged
.removeClass( classNames
);
5518 this.$flagged
= $flagged
.addClass( classNames
);
5522 * Check if a flag is set.
5524 * @param {string} flag Name of flag
5525 * @return {boolean} Has flag
5527 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
5528 return flag
in this.flags
;
5532 * Get the names of all flags set.
5534 * @return {string[]} Flag names
5536 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
5537 return Object
.keys( this.flags
);
5546 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
5547 var flag
, className
,
5550 classPrefix
= 'oo-ui-flaggedElement-';
5552 for ( flag
in this.flags
) {
5553 className
= classPrefix
+ flag
;
5554 changes
[ flag
] = false;
5555 delete this.flags
[ flag
];
5556 remove
.push( className
);
5559 if ( this.$flagged
) {
5560 this.$flagged
.removeClass( remove
.join( ' ' ) );
5563 this.updateThemeClasses();
5564 this.emit( 'flag', changes
);
5570 * Add one or more flags.
5572 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
5573 * keyed by flag name containing boolean set/remove instructions.
5577 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
5578 var i
, len
, flag
, className
,
5582 classPrefix
= 'oo-ui-flaggedElement-';
5584 if ( typeof flags
=== 'string' ) {
5585 className
= classPrefix
+ flags
;
5587 if ( !this.flags
[ flags
] ) {
5588 this.flags
[ flags
] = true;
5589 add
.push( className
);
5591 } else if ( $.isArray( flags
) ) {
5592 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
5594 className
= classPrefix
+ flag
;
5596 if ( !this.flags
[ flag
] ) {
5597 changes
[ flag
] = true;
5598 this.flags
[ flag
] = true;
5599 add
.push( className
);
5602 } else if ( OO
.isPlainObject( flags
) ) {
5603 for ( flag
in flags
) {
5604 className
= classPrefix
+ flag
;
5605 if ( flags
[ flag
] ) {
5607 if ( !this.flags
[ flag
] ) {
5608 changes
[ flag
] = true;
5609 this.flags
[ flag
] = true;
5610 add
.push( className
);
5614 if ( this.flags
[ flag
] ) {
5615 changes
[ flag
] = false;
5616 delete this.flags
[ flag
];
5617 remove
.push( className
);
5623 if ( this.$flagged
) {
5625 .addClass( add
.join( ' ' ) )
5626 .removeClass( remove
.join( ' ' ) );
5629 this.updateThemeClasses();
5630 this.emit( 'flag', changes
);
5636 * Element with a title.
5638 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
5639 * not visible on touch devices.
5645 * @param {Object} [config] Configuration options
5646 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
5647 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
5648 * static property 'title' is used.
5650 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
5651 // Configuration initialization
5652 config
= config
|| {};
5655 this.$titled
= null;
5659 this.setTitle( config
.title
|| this.constructor.static.title
);
5660 this.setTitledElement( config
.$titled
|| this.$element
);
5665 OO
.initClass( OO
.ui
.TitledElement
);
5667 /* Static Properties */
5674 * @property {string|Function} Title text or a function that returns text
5676 OO
.ui
.TitledElement
.static.title
= null;
5681 * Set the titled element.
5683 * If an element is already set, it will be cleaned up before setting up the new element.
5685 * @param {jQuery} $titled Element to set title on
5687 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
5688 if ( this.$titled
) {
5689 this.$titled
.removeAttr( 'title' );
5692 this.$titled
= $titled
;
5694 this.$titled
.attr( 'title', this.title
);
5701 * @param {string|Function|null} title Title text, a function that returns text or null for no title
5704 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
5705 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
5707 if ( this.title
!== title
) {
5708 if ( this.$titled
) {
5709 if ( title
!== null ) {
5710 this.$titled
.attr( 'title', title
);
5712 this.$titled
.removeAttr( 'title' );
5724 * @return {string} Title string
5726 OO
.ui
.TitledElement
.prototype.getTitle = function () {
5731 * Element that can be automatically clipped to visible boundaries.
5733 * Whenever the element's natural height changes, you have to call
5734 * #clip to make sure it's still clipping correctly.
5740 * @param {Object} [config] Configuration options
5741 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
5743 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
5744 // Configuration initialization
5745 config
= config
|| {};
5748 this.$clippable
= null;
5749 this.clipping
= false;
5750 this.clippedHorizontally
= false;
5751 this.clippedVertically
= false;
5752 this.$clippableContainer
= null;
5753 this.$clippableScroller
= null;
5754 this.$clippableWindow
= null;
5755 this.idealWidth
= null;
5756 this.idealHeight
= null;
5757 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
5758 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
5761 this.setClippableElement( config
.$clippable
|| this.$element
);
5767 * Set clippable element.
5769 * If an element is already set, it will be cleaned up before setting up the new element.
5771 * @param {jQuery} $clippable Element to make clippable
5773 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
5774 if ( this.$clippable
) {
5775 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
5776 this.$clippable
.css( { width
: '', height
: '', overflowX
: '', overflowY
: '' } );
5777 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
5780 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
5787 * Do not turn clipping on until after the element is attached to the DOM and visible.
5789 * @param {boolean} [clipping] Enable clipping, omit to toggle
5792 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
5793 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
5795 if ( this.clipping
!== clipping
) {
5796 this.clipping
= clipping
;
5798 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
5799 // If the clippable container is the root, we have to listen to scroll events and check
5800 // jQuery.scrollTop on the window because of browser inconsistencies
5801 this.$clippableScroller
= this.$clippableContainer
.is( 'html, body' ) ?
5802 this.$( OO
.ui
.Element
.static.getWindow( this.$clippableContainer
) ) :
5803 this.$clippableContainer
;
5804 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
5805 this.$clippableWindow
= this.$( this.getElementWindow() )
5806 .on( 'resize', this.onClippableWindowResizeHandler
);
5807 // Initial clip after visible
5810 this.$clippable
.css( { width
: '', height
: '', overflowX
: '', overflowY
: '' } );
5811 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
5813 this.$clippableContainer
= null;
5814 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
5815 this.$clippableScroller
= null;
5816 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
5817 this.$clippableWindow
= null;
5825 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
5827 * @return {boolean} Element will be clipped to the visible area
5829 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
5830 return this.clipping
;
5834 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
5836 * @return {boolean} Part of the element is being clipped
5838 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
5839 return this.clippedHorizontally
|| this.clippedVertically
;
5843 * Check if the right of the element is being clipped by the nearest scrollable container.
5845 * @return {boolean} Part of the element is being clipped
5847 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
5848 return this.clippedHorizontally
;
5852 * Check if the bottom of the element is being clipped by the nearest scrollable container.
5854 * @return {boolean} Part of the element is being clipped
5856 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
5857 return this.clippedVertically
;
5861 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
5863 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
5864 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
5866 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
5867 this.idealWidth
= width
;
5868 this.idealHeight
= height
;
5870 if ( !this.clipping
) {
5871 // Update dimensions
5872 this.$clippable
.css( { width
: width
, height
: height
} );
5874 // While clipping, idealWidth and idealHeight are not considered
5878 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
5879 * the element's natural height changes.
5881 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
5882 * overlapped by, the visible area of the nearest scrollable container.
5886 OO
.ui
.ClippableElement
.prototype.clip = function () {
5887 if ( !this.clipping
) {
5888 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
5892 var buffer
= 7, // Chosen by fair dice roll
5893 cOffset
= this.$clippable
.offset(),
5894 $container
= this.$clippableContainer
.is( 'html, body' ) ?
5895 this.$clippableWindow
: this.$clippableContainer
,
5896 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
5897 ccHeight
= $container
.innerHeight() - buffer
,
5898 ccWidth
= $container
.innerWidth() - buffer
,
5899 cHeight
= this.$clippable
.outerHeight() + buffer
,
5900 cWidth
= this.$clippable
.outerWidth() + buffer
,
5901 scrollTop
= this.$clippableScroller
.scrollTop(),
5902 scrollLeft
= this.$clippableScroller
.scrollLeft(),
5903 desiredWidth
= cOffset
.left
< 0 ?
5904 cWidth
+ cOffset
.left
:
5905 ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
5906 desiredHeight
= cOffset
.top
< 0 ?
5907 cHeight
+ cOffset
.top
:
5908 ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
5909 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
5910 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
5911 clipWidth
= desiredWidth
< naturalWidth
,
5912 clipHeight
= desiredHeight
< naturalHeight
;
5915 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
5917 this.$clippable
.css( { width
: this.idealWidth
|| '', overflowX
: '' } );
5920 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
5922 this.$clippable
.css( { height
: this.idealHeight
|| '', overflowY
: '' } );
5925 // If we stopped clipping in at least one of the dimensions
5926 if ( !clipWidth
|| !clipHeight
) {
5927 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
5930 this.clippedHorizontally
= clipWidth
;
5931 this.clippedVertically
= clipHeight
;
5937 * Generic toolbar tool.
5941 * @extends OO.ui.Widget
5942 * @mixins OO.ui.IconElement
5943 * @mixins OO.ui.FlaggedElement
5946 * @param {OO.ui.ToolGroup} toolGroup
5947 * @param {Object} [config] Configuration options
5948 * @cfg {string|Function} [title] Title text or a function that returns text
5950 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
5951 // Configuration initialization
5952 config
= config
|| {};
5954 // Parent constructor
5955 OO
.ui
.Tool
.super.call( this, config
);
5957 // Mixin constructors
5958 OO
.ui
.IconElement
.call( this, config
);
5959 OO
.ui
.FlaggedElement
.call( this, config
);
5962 this.toolGroup
= toolGroup
;
5963 this.toolbar
= this.toolGroup
.getToolbar();
5964 this.active
= false;
5965 this.$title
= this.$( '<span>' );
5966 this.$accel
= this.$( '<span>' );
5967 this.$link
= this.$( '<a>' );
5971 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
5974 this.$title
.addClass( 'oo-ui-tool-title' );
5976 .addClass( 'oo-ui-tool-accel' )
5978 // This may need to be changed if the key names are ever localized,
5979 // but for now they are essentially written in English
5984 .addClass( 'oo-ui-tool-link' )
5985 .append( this.$icon
, this.$title
, this.$accel
)
5986 .prop( 'tabIndex', 0 )
5987 .attr( 'role', 'button' );
5989 .data( 'oo-ui-tool', this )
5991 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5992 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5994 .append( this.$link
);
5995 this.setTitle( config
.title
|| this.constructor.static.title
);
6000 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
6001 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
6002 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
6010 /* Static Properties */
6016 OO
.ui
.Tool
.static.tagName
= 'span';
6019 * Symbolic name of tool.
6024 * @property {string}
6026 OO
.ui
.Tool
.static.name
= '';
6034 * @property {string}
6036 OO
.ui
.Tool
.static.group
= '';
6041 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
6042 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
6043 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
6044 * appended to the title if the tool is part of a bar tool group.
6049 * @property {string|Function} Title text or a function that returns text
6051 OO
.ui
.Tool
.static.title
= '';
6054 * Tool can be automatically added to catch-all groups.
6058 * @property {boolean}
6060 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
6063 * Tool can be automatically added to named groups.
6066 * @property {boolean}
6069 OO
.ui
.Tool
.static.autoAddToGroup
= true;
6072 * Check if this tool is compatible with given data.
6076 * @param {Mixed} data Data to check
6077 * @return {boolean} Tool can be used with data
6079 OO
.ui
.Tool
.static.isCompatibleWith = function () {
6086 * Handle the toolbar state being updated.
6088 * This is an abstract method that must be overridden in a concrete subclass.
6092 OO
.ui
.Tool
.prototype.onUpdateState = function () {
6094 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
6099 * Handle the tool being selected.
6101 * This is an abstract method that must be overridden in a concrete subclass.
6105 OO
.ui
.Tool
.prototype.onSelect = function () {
6107 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
6112 * Check if the button is active.
6114 * @return {boolean} Button is active
6116 OO
.ui
.Tool
.prototype.isActive = function () {
6121 * Make the button appear active or inactive.
6123 * @param {boolean} state Make button appear active
6125 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
6126 this.active
= !!state
;
6127 if ( this.active
) {
6128 this.$element
.addClass( 'oo-ui-tool-active' );
6130 this.$element
.removeClass( 'oo-ui-tool-active' );
6135 * Get the tool title.
6137 * @param {string|Function} title Title text or a function that returns text
6140 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
6141 this.title
= OO
.ui
.resolveMsg( title
);
6147 * Get the tool title.
6149 * @return {string} Title text
6151 OO
.ui
.Tool
.prototype.getTitle = function () {
6156 * Get the tool's symbolic name.
6158 * @return {string} Symbolic name of tool
6160 OO
.ui
.Tool
.prototype.getName = function () {
6161 return this.constructor.static.name
;
6167 OO
.ui
.Tool
.prototype.updateTitle = function () {
6168 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
6169 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
6170 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
6173 this.$title
.text( this.title
);
6174 this.$accel
.text( accel
);
6176 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
6177 tooltipParts
.push( this.title
);
6179 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
6180 tooltipParts
.push( accel
);
6182 if ( tooltipParts
.length
) {
6183 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
6185 this.$link
.removeAttr( 'title' );
6192 OO
.ui
.Tool
.prototype.destroy = function () {
6193 this.toolbar
.disconnect( this );
6194 this.$element
.remove();
6198 * Collection of tool groups.
6201 * @extends OO.ui.Element
6202 * @mixins OO.EventEmitter
6203 * @mixins OO.ui.GroupElement
6206 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
6207 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
6208 * @param {Object} [config] Configuration options
6209 * @cfg {boolean} [actions] Add an actions section opposite to the tools
6210 * @cfg {boolean} [shadow] Add a shadow below the toolbar
6212 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
6213 // Configuration initialization
6214 config
= config
|| {};
6216 // Parent constructor
6217 OO
.ui
.Toolbar
.super.call( this, config
);
6219 // Mixin constructors
6220 OO
.EventEmitter
.call( this );
6221 OO
.ui
.GroupElement
.call( this, config
);
6224 this.toolFactory
= toolFactory
;
6225 this.toolGroupFactory
= toolGroupFactory
;
6228 this.$bar
= this.$( '<div>' );
6229 this.$actions
= this.$( '<div>' );
6230 this.initialized
= false;
6234 .add( this.$bar
).add( this.$group
).add( this.$actions
)
6235 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
6238 this.$group
.addClass( 'oo-ui-toolbar-tools' );
6239 if ( config
.actions
) {
6240 this.$bar
.append( this.$actions
.addClass( 'oo-ui-toolbar-actions' ) );
6243 .addClass( 'oo-ui-toolbar-bar' )
6244 .append( this.$group
, '<div style="clear:both"></div>' );
6245 if ( config
.shadow
) {
6246 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
6248 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
6253 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
6254 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
6255 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
6260 * Get the tool factory.
6262 * @return {OO.ui.ToolFactory} Tool factory
6264 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
6265 return this.toolFactory
;
6269 * Get the tool group factory.
6271 * @return {OO.Factory} Tool group factory
6273 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
6274 return this.toolGroupFactory
;
6278 * Handles mouse down events.
6280 * @param {jQuery.Event} e Mouse down event
6282 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
6283 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
6284 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
6285 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[ 0 ] === $closestWidgetToToolbar
[ 0 ] ) {
6291 * Sets up handles and preloads required information for the toolbar to work.
6292 * This must be called after it is attached to a visible document and before doing anything else.
6294 OO
.ui
.Toolbar
.prototype.initialize = function () {
6295 this.initialized
= true;
6301 * Tools can be specified in the following ways:
6303 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
6304 * - All tools in a group: `{ group: 'group-name' }`
6305 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
6307 * @param {Object.<string,Array>} groups List of tool group configurations
6308 * @param {Array|string} [groups.include] Tools to include
6309 * @param {Array|string} [groups.exclude] Tools to exclude
6310 * @param {Array|string} [groups.promote] Tools to promote to the beginning
6311 * @param {Array|string} [groups.demote] Tools to demote to the end
6313 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
6314 var i
, len
, type
, group
,
6316 defaultType
= 'bar';
6318 // Cleanup previous groups
6321 // Build out new groups
6322 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
6323 group
= groups
[ i
];
6324 if ( group
.include
=== '*' ) {
6325 // Apply defaults to catch-all groups
6326 if ( group
.type
=== undefined ) {
6327 group
.type
= 'list';
6329 if ( group
.label
=== undefined ) {
6330 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
6333 // Check type has been registered
6334 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
6336 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
6339 this.addItems( items
);
6343 * Remove all tools and groups from the toolbar.
6345 OO
.ui
.Toolbar
.prototype.reset = function () {
6350 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6351 this.items
[ i
].destroy();
6357 * Destroys toolbar, removing event handlers and DOM elements.
6359 * Call this whenever you are done using a toolbar.
6361 OO
.ui
.Toolbar
.prototype.destroy = function () {
6363 this.$element
.remove();
6367 * Check if tool has not been used yet.
6369 * @param {string} name Symbolic name of tool
6370 * @return {boolean} Tool is available
6372 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
6373 return !this.tools
[ name
];
6377 * Prevent tool from being used again.
6379 * @param {OO.ui.Tool} tool Tool to reserve
6381 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
6382 this.tools
[ tool
.getName() ] = tool
;
6386 * Allow tool to be used again.
6388 * @param {OO.ui.Tool} tool Tool to release
6390 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
6391 delete this.tools
[ tool
.getName() ];
6395 * Get accelerator label for tool.
6397 * This is a stub that should be overridden to provide access to accelerator information.
6399 * @param {string} name Symbolic name of tool
6400 * @return {string|undefined} Tool accelerator label if available
6402 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
6407 * Collection of tools.
6409 * Tools can be specified in the following ways:
6411 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
6412 * - All tools in a group: `{ group: 'group-name' }`
6413 * - All tools: `'*'`
6417 * @extends OO.ui.Widget
6418 * @mixins OO.ui.GroupElement
6421 * @param {OO.ui.Toolbar} toolbar
6422 * @param {Object} [config] Configuration options
6423 * @cfg {Array|string} [include=[]] List of tools to include
6424 * @cfg {Array|string} [exclude=[]] List of tools to exclude
6425 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
6426 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
6428 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
6429 // Configuration initialization
6430 config
= config
|| {};
6432 // Parent constructor
6433 OO
.ui
.ToolGroup
.super.call( this, config
);
6435 // Mixin constructors
6436 OO
.ui
.GroupElement
.call( this, config
);
6439 this.toolbar
= toolbar
;
6441 this.pressed
= null;
6442 this.autoDisabled
= false;
6443 this.include
= config
.include
|| [];
6444 this.exclude
= config
.exclude
|| [];
6445 this.promote
= config
.promote
|| [];
6446 this.demote
= config
.demote
|| [];
6447 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
6451 'mousedown touchstart': this.onPointerDown
.bind( this ),
6452 'mouseup touchend': this.onPointerUp
.bind( this ),
6453 mouseover
: this.onMouseOver
.bind( this ),
6454 mouseout
: this.onMouseOut
.bind( this )
6456 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
6457 this.aggregate( { disable
: 'itemDisable' } );
6458 this.connect( this, { itemDisable
: 'updateDisabled' } );
6461 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
6463 .addClass( 'oo-ui-toolGroup' )
6464 .append( this.$group
);
6470 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
6471 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
6479 /* Static Properties */
6482 * Show labels in tooltips.
6486 * @property {boolean}
6488 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
6491 * Show acceleration labels in tooltips.
6495 * @property {boolean}
6497 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
6500 * Automatically disable the toolgroup when all tools are disabled
6504 * @property {boolean}
6506 OO
.ui
.ToolGroup
.static.autoDisable
= true;
6513 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
6514 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
6520 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
6521 var i
, item
, allDisabled
= true;
6523 if ( this.constructor.static.autoDisable
) {
6524 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
6525 item
= this.items
[ i
];
6526 if ( !item
.isDisabled() ) {
6527 allDisabled
= false;
6531 this.autoDisabled
= allDisabled
;
6533 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
6537 * Handle mouse down events.
6539 * @param {jQuery.Event} e Mouse down event
6541 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
6542 // e.which is 0 for touch events, 1 for left mouse button
6543 if ( !this.isDisabled() && e
.which
<= 1 ) {
6544 this.pressed
= this.getTargetTool( e
);
6545 if ( this.pressed
) {
6546 this.pressed
.setActive( true );
6547 this.getElementDocument().addEventListener(
6548 'mouseup', this.onCapturedMouseUpHandler
, true
6556 * Handle captured mouse up events.
6558 * @param {Event} e Mouse up event
6560 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
6561 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
6562 // onPointerUp may be called a second time, depending on where the mouse is when the button is
6563 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
6564 this.onPointerUp( e
);
6568 * Handle mouse up events.
6570 * @param {jQuery.Event} e Mouse up event
6572 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
6573 var tool
= this.getTargetTool( e
);
6575 // e.which is 0 for touch events, 1 for left mouse button
6576 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
6577 this.pressed
.onSelect();
6580 this.pressed
= null;
6585 * Handle mouse over events.
6587 * @param {jQuery.Event} e Mouse over event
6589 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
6590 var tool
= this.getTargetTool( e
);
6592 if ( this.pressed
&& this.pressed
=== tool
) {
6593 this.pressed
.setActive( true );
6598 * Handle mouse out events.
6600 * @param {jQuery.Event} e Mouse out event
6602 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
6603 var tool
= this.getTargetTool( e
);
6605 if ( this.pressed
&& this.pressed
=== tool
) {
6606 this.pressed
.setActive( false );
6611 * Get the closest tool to a jQuery.Event.
6613 * Only tool links are considered, which prevents other elements in the tool such as popups from
6614 * triggering tool group interactions.
6617 * @param {jQuery.Event} e
6618 * @return {OO.ui.Tool|null} Tool, `null` if none was found
6620 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
6622 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
6624 if ( $item
.length
) {
6625 tool
= $item
.parent().data( 'oo-ui-tool' );
6628 return tool
&& !tool
.isDisabled() ? tool
: null;
6632 * Handle tool registry register events.
6634 * If a tool is registered after the group is created, we must repopulate the list to account for:
6636 * - a tool being added that may be included
6637 * - a tool already included being overridden
6639 * @param {string} name Symbolic name of tool
6641 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
6646 * Get the toolbar this group is in.
6648 * @return {OO.ui.Toolbar} Toolbar of group
6650 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
6651 return this.toolbar
;
6655 * Add and remove tools based on configuration.
6657 OO
.ui
.ToolGroup
.prototype.populate = function () {
6658 var i
, len
, name
, tool
,
6659 toolFactory
= this.toolbar
.getToolFactory(),
6663 list
= this.toolbar
.getToolFactory().getTools(
6664 this.include
, this.exclude
, this.promote
, this.demote
6667 // Build a list of needed tools
6668 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
6672 toolFactory
.lookup( name
) &&
6673 // Tool is available or is already in this group
6674 ( this.toolbar
.isToolAvailable( name
) || this.tools
[ name
] )
6676 tool
= this.tools
[ name
];
6678 // Auto-initialize tools on first use
6679 this.tools
[ name
] = tool
= toolFactory
.create( name
, this );
6682 this.toolbar
.reserveTool( tool
);
6684 names
[ name
] = true;
6687 // Remove tools that are no longer needed
6688 for ( name
in this.tools
) {
6689 if ( !names
[ name
] ) {
6690 this.tools
[ name
].destroy();
6691 this.toolbar
.releaseTool( this.tools
[ name
] );
6692 remove
.push( this.tools
[ name
] );
6693 delete this.tools
[ name
];
6696 if ( remove
.length
) {
6697 this.removeItems( remove
);
6699 // Update emptiness state
6701 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
6703 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
6705 // Re-add tools (moving existing ones to new locations)
6706 this.addItems( add
);
6707 // Disabled state may depend on items
6708 this.updateDisabled();
6712 * Destroy tool group.
6714 OO
.ui
.ToolGroup
.prototype.destroy = function () {
6718 this.toolbar
.getToolFactory().disconnect( this );
6719 for ( name
in this.tools
) {
6720 this.toolbar
.releaseTool( this.tools
[ name
] );
6721 this.tools
[ name
].disconnect( this ).destroy();
6722 delete this.tools
[ name
];
6724 this.$element
.remove();
6728 * Dialog for showing a message.
6731 * - Registers two actions by default (safe and primary).
6732 * - Renders action widgets in the footer.
6735 * @extends OO.ui.Dialog
6738 * @param {Object} [config] Configuration options
6740 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
6741 // Parent constructor
6742 OO
.ui
.MessageDialog
.super.call( this, config
);
6745 this.verticalActionLayout
= null;
6748 this.$element
.addClass( 'oo-ui-messageDialog' );
6753 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
6755 /* Static Properties */
6757 OO
.ui
.MessageDialog
.static.name
= 'message';
6759 OO
.ui
.MessageDialog
.static.size
= 'small';
6761 OO
.ui
.MessageDialog
.static.verbose
= false;
6766 * A confirmation dialog's title should describe what the progressive action will do. An alert
6767 * dialog's title should describe what event occurred.
6771 * @property {jQuery|string|Function|null}
6773 OO
.ui
.MessageDialog
.static.title
= null;
6776 * A confirmation dialog's message should describe the consequences of the progressive action. An
6777 * alert dialog's message should describe why the event occurred.
6781 * @property {jQuery|string|Function|null}
6783 OO
.ui
.MessageDialog
.static.message
= null;
6785 OO
.ui
.MessageDialog
.static.actions
= [
6786 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
6787 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
6795 OO
.ui
.MessageDialog
.prototype.setManager = function ( manager
) {
6796 OO
.ui
.MessageDialog
.super.prototype.setManager
.call( this, manager
);
6799 this.manager
.connect( this, {
6809 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
6811 return OO
.ui
.MessageDialog
.super.prototype.onActionResize
.call( this, action
);
6815 * Handle window resized events.
6817 OO
.ui
.MessageDialog
.prototype.onResize = function () {
6819 dialog
.fitActions();
6820 // Wait for CSS transition to finish and do it again :(
6821 setTimeout( function () {
6822 dialog
.fitActions();
6827 * Toggle action layout between vertical and horizontal.
6829 * @param {boolean} [value] Layout actions vertically, omit to toggle
6832 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
6833 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
6835 if ( value
!== this.verticalActionLayout
) {
6836 this.verticalActionLayout
= value
;
6838 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
6839 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
6848 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
6850 return new OO
.ui
.Process( function () {
6851 this.close( { action
: action
} );
6854 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
6860 * @param {Object} [data] Dialog opening data
6861 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
6862 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
6863 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
6864 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
6867 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
6871 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
6872 .next( function () {
6873 this.title
.setLabel(
6874 data
.title
!== undefined ? data
.title
: this.constructor.static.title
6876 this.message
.setLabel(
6877 data
.message
!== undefined ? data
.message
: this.constructor.static.message
6879 this.message
.$element
.toggleClass(
6880 'oo-ui-messageDialog-message-verbose',
6881 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
6889 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
6890 var bodyHeight
, oldOverflow
,
6891 $scrollable
= this.container
.$element
;
6893 oldOverflow
= $scrollable
[ 0 ].style
.overflow
;
6894 $scrollable
[ 0 ].style
.overflow
= 'hidden';
6896 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
6898 bodyHeight
= this.text
.$element
.outerHeight( true );
6899 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
6907 OO
.ui
.MessageDialog
.prototype.setDimensions = function ( dim
) {
6908 var $scrollable
= this.container
.$element
;
6909 OO
.ui
.MessageDialog
.super.prototype.setDimensions
.call( this, dim
);
6911 // Twiddle the overflow property, otherwise an unnecessary scrollbar will be produced.
6912 // Need to do it after transition completes (250ms), add 50ms just in case.
6913 setTimeout( function () {
6914 var oldOverflow
= $scrollable
[ 0 ].style
.overflow
;
6915 $scrollable
[ 0 ].style
.overflow
= 'hidden';
6917 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
6919 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
6928 OO
.ui
.MessageDialog
.prototype.initialize = function () {
6930 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
6933 this.$actions
= this.$( '<div>' );
6934 this.container
= new OO
.ui
.PanelLayout( {
6935 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
6937 this.text
= new OO
.ui
.PanelLayout( {
6938 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
6940 this.message
= new OO
.ui
.LabelWidget( {
6941 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
6945 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
6946 this.$content
.addClass( 'oo-ui-messageDialog-content' );
6947 this.container
.$element
.append( this.text
.$element
);
6948 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
6949 this.$body
.append( this.container
.$element
);
6950 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
6951 this.$foot
.append( this.$actions
);
6957 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
6958 var i
, len
, other
, special
, others
;
6961 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
6963 special
= this.actions
.getSpecial();
6964 others
= this.actions
.getOthers();
6965 if ( special
.safe
) {
6966 this.$actions
.append( special
.safe
.$element
);
6967 special
.safe
.toggleFramed( false );
6969 if ( others
.length
) {
6970 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6971 other
= others
[ i
];
6972 this.$actions
.append( other
.$element
);
6973 other
.toggleFramed( false );
6976 if ( special
.primary
) {
6977 this.$actions
.append( special
.primary
.$element
);
6978 special
.primary
.toggleFramed( false );
6981 if ( !this.isOpening() ) {
6982 // If the dialog is currently opening, this will be called automatically soon.
6983 // This also calls #fitActions.
6989 * Fit action actions into columns or rows.
6991 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
6993 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
6995 previous
= this.verticalActionLayout
,
6996 actions
= this.actions
.get();
6999 this.toggleVerticalActionLayout( false );
7000 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
7001 action
= actions
[ i
];
7002 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
7003 this.toggleVerticalActionLayout( true );
7008 if ( this.verticalActionLayout
!== previous
) {
7009 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
7010 // We changed the layout, window height might need to be updated.
7016 * Navigation dialog window.
7019 * - Show and hide errors.
7020 * - Retry an action.
7023 * - Renders header with dialog title and one action widget on either side
7024 * (a 'safe' button on the left, and a 'primary' button on the right, both of
7025 * which close the dialog).
7026 * - Displays any action widgets in the footer (none by default).
7027 * - Ability to dismiss errors.
7029 * Subclass responsibilities:
7030 * - Register a 'safe' action.
7031 * - Register a 'primary' action.
7032 * - Add content to the dialog.
7036 * @extends OO.ui.Dialog
7039 * @param {Object} [config] Configuration options
7041 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
7042 // Parent constructor
7043 OO
.ui
.ProcessDialog
.super.call( this, config
);
7046 this.$element
.addClass( 'oo-ui-processDialog' );
7051 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
7056 * Handle dismiss button click events.
7060 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
7065 * Handle retry button click events.
7067 * Hides errors and then tries again.
7069 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
7071 this.executeAction( this.currentAction
.getAction() );
7077 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
7078 if ( this.actions
.isSpecial( action
) ) {
7081 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
7087 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
7089 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
7092 this.$navigation
= this.$( '<div>' );
7093 this.$location
= this.$( '<div>' );
7094 this.$safeActions
= this.$( '<div>' );
7095 this.$primaryActions
= this.$( '<div>' );
7096 this.$otherActions
= this.$( '<div>' );
7097 this.dismissButton
= new OO
.ui
.ButtonWidget( {
7099 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
7101 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
7102 this.$errors
= this.$( '<div>' );
7103 this.$errorsTitle
= this.$( '<div>' );
7106 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
7107 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
7110 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
7112 .append( this.title
.$element
)
7113 .addClass( 'oo-ui-processDialog-location' );
7114 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
7115 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
7116 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
7118 .addClass( 'oo-ui-processDialog-errors-title' )
7119 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
7121 .addClass( 'oo-ui-processDialog-errors' )
7122 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
7124 .addClass( 'oo-ui-processDialog-content' )
7125 .append( this.$errors
);
7127 .addClass( 'oo-ui-processDialog-navigation' )
7128 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
7129 this.$head
.append( this.$navigation
);
7130 this.$foot
.append( this.$otherActions
);
7136 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
7137 var i
, len
, other
, special
, others
;
7140 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
7142 special
= this.actions
.getSpecial();
7143 others
= this.actions
.getOthers();
7144 if ( special
.primary
) {
7145 this.$primaryActions
.append( special
.primary
.$element
);
7146 special
.primary
.toggleFramed( true );
7148 if ( others
.length
) {
7149 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
7150 other
= others
[ i
];
7151 this.$otherActions
.append( other
.$element
);
7152 other
.toggleFramed( true );
7155 if ( special
.safe
) {
7156 this.$safeActions
.append( special
.safe
.$element
);
7157 special
.safe
.toggleFramed( true );
7161 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
7167 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
7168 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
7169 .fail( this.showErrors
.bind( this ) );
7173 * Fit label between actions.
7177 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
7178 var width
= Math
.max(
7179 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
7180 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
7182 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
7188 * Handle errors that occurred during accept or reject processes.
7190 * @param {OO.ui.Error[]} errors Errors to be handled
7192 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
7198 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
7199 if ( !errors
[ i
].isRecoverable() ) {
7200 recoverable
= false;
7202 if ( errors
[ i
].isWarning() ) {
7205 $item
= this.$( '<div>' )
7206 .addClass( 'oo-ui-processDialog-error' )
7207 .append( errors
[ i
].getMessage() );
7208 items
.push( $item
[ 0 ] );
7210 this.$errorItems
= this.$( items
);
7211 if ( recoverable
) {
7212 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
7214 this.currentAction
.setDisabled( true );
7217 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
7219 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
7221 this.retryButton
.toggle( recoverable
);
7222 this.$errorsTitle
.after( this.$errorItems
);
7223 this.$errors
.removeClass( 'oo-ui-widget-hidden' ).scrollTop( 0 );
7229 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
7230 this.$errors
.addClass( 'oo-ui-widget-hidden' );
7231 this.$errorItems
.remove();
7232 this.$errorItems
= null;
7236 * Layout made of a field and optional label.
7238 * Available label alignment modes include:
7239 * - left: Label is before the field and aligned away from it, best for when the user will be
7240 * scanning for a specific label in a form with many fields
7241 * - right: Label is before the field and aligned toward it, best for forms the user is very
7242 * familiar with and will tab through field checking quickly to verify which field they are in
7243 * - top: Label is before the field and above it, best for when the user will need to fill out all
7244 * fields from top to bottom in a form with few fields
7245 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
7246 * checkboxes or radio buttons
7249 * @extends OO.ui.Layout
7250 * @mixins OO.ui.LabelElement
7253 * @param {OO.ui.Widget} fieldWidget Field widget
7254 * @param {Object} [config] Configuration options
7255 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
7256 * @cfg {string} [help] Explanatory text shown as a '?' icon.
7258 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
7259 var hasInputWidget
= fieldWidget
instanceof OO
.ui
.InputWidget
;
7261 // Configuration initialization
7262 config
= $.extend( { align
: 'left' }, config
);
7264 // Properties (must be set before parent constructor, which calls #getTagName)
7265 this.fieldWidget
= fieldWidget
;
7267 // Parent constructor
7268 OO
.ui
.FieldLayout
.super.call( this, config
);
7270 // Mixin constructors
7271 OO
.ui
.LabelElement
.call( this, config
);
7274 this.$field
= this.$( '<div>' );
7275 this.$body
= this.$( '<' + ( hasInputWidget
? 'label' : 'div' ) + '>' );
7277 if ( config
.help
) {
7278 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
7280 classes
: [ 'oo-ui-fieldLayout-help' ],
7285 this.popupButtonWidget
.getPopup().$body
.append(
7287 .text( config
.help
)
7288 .addClass( 'oo-ui-fieldLayout-help-content' )
7290 this.$help
= this.popupButtonWidget
.$element
;
7292 this.$help
= this.$( [] );
7296 if ( hasInputWidget
) {
7297 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
7299 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
7303 .addClass( 'oo-ui-fieldLayout' )
7304 .append( this.$help
, this.$body
);
7305 this.$body
.addClass( 'oo-ui-fieldLayout-body' );
7307 .addClass( 'oo-ui-fieldLayout-field' )
7308 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
7309 .append( this.fieldWidget
.$element
);
7311 this.setAlignment( config
.align
);
7316 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
7317 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
7322 * Handle field disable events.
7324 * @param {boolean} value Field is disabled
7326 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
7327 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
7331 * Handle label mouse click events.
7333 * @param {jQuery.Event} e Mouse click event
7335 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
7336 this.fieldWidget
.simulateLabelClick();
7343 * @return {OO.ui.Widget} Field widget
7345 OO
.ui
.FieldLayout
.prototype.getField = function () {
7346 return this.fieldWidget
;
7350 * Set the field alignment mode.
7353 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
7356 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
7357 if ( value
!== this.align
) {
7358 // Default to 'left'
7359 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
7363 if ( value
=== 'inline' ) {
7364 this.$body
.append( this.$field
, this.$label
);
7366 this.$body
.append( this.$label
, this.$field
);
7368 // Set classes. The following classes can be used here:
7369 // * oo-ui-fieldLayout-align-left
7370 // * oo-ui-fieldLayout-align-right
7371 // * oo-ui-fieldLayout-align-top
7372 // * oo-ui-fieldLayout-align-inline
7374 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
7376 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
7384 * Layout made of a field, a button, and an optional label.
7387 * @extends OO.ui.FieldLayout
7390 * @param {OO.ui.Widget} fieldWidget Field widget
7391 * @param {OO.ui.ButtonWidget} buttonWidget Button widget
7392 * @param {Object} [config] Configuration options
7393 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
7394 * @cfg {string} [help] Explanatory text shown as a '?' icon.
7396 OO
.ui
.ActionFieldLayout
= function OoUiActionFieldLayout( fieldWidget
, buttonWidget
, config
) {
7397 // Configuration initialization
7398 config
= $.extend( { align
: 'left' }, config
);
7400 // Properties (must be set before parent constructor, which calls #getTagName)
7401 this.fieldWidget
= fieldWidget
;
7402 this.buttonWidget
= buttonWidget
;
7404 // Parent constructor
7405 OO
.ui
.ActionFieldLayout
.super.call( this, fieldWidget
, config
);
7407 // Mixin constructors
7408 OO
.ui
.LabelElement
.call( this, config
);
7411 this.$button
= this.$( '<div>' )
7412 .addClass( 'oo-ui-actionFieldLayout-button' )
7413 .append( this.buttonWidget
.$element
);
7415 this.$input
= this.$( '<div>' )
7416 .addClass( 'oo-ui-actionFieldLayout-input' )
7417 .append( this.fieldWidget
.$element
);
7420 .addClass( 'oo-ui-actionFieldLayout' )
7421 .append( this.$input
, this.$button
);
7426 OO
.inheritClass( OO
.ui
.ActionFieldLayout
, OO
.ui
.FieldLayout
);
7429 * Layout made of a fieldset and optional legend.
7431 * Just add OO.ui.FieldLayout items.
7434 * @extends OO.ui.Layout
7435 * @mixins OO.ui.IconElement
7436 * @mixins OO.ui.LabelElement
7437 * @mixins OO.ui.GroupElement
7440 * @param {Object} [config] Configuration options
7441 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
7443 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
7444 // Configuration initialization
7445 config
= config
|| {};
7447 // Parent constructor
7448 OO
.ui
.FieldsetLayout
.super.call( this, config
);
7450 // Mixin constructors
7451 OO
.ui
.IconElement
.call( this, config
);
7452 OO
.ui
.LabelElement
.call( this, config
);
7453 OO
.ui
.GroupElement
.call( this, config
);
7455 if ( config
.help
) {
7456 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
7458 classes
: [ 'oo-ui-fieldsetLayout-help' ],
7463 this.popupButtonWidget
.getPopup().$body
.append(
7465 .text( config
.help
)
7466 .addClass( 'oo-ui-fieldsetLayout-help-content' )
7468 this.$help
= this.popupButtonWidget
.$element
;
7470 this.$help
= this.$( [] );
7475 .addClass( 'oo-ui-fieldsetLayout' )
7476 .prepend( this.$help
, this.$icon
, this.$label
, this.$group
);
7477 if ( $.isArray( config
.items
) ) {
7478 this.addItems( config
.items
);
7484 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
7485 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
7486 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
7487 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
7490 * Layout with an HTML form.
7493 * @extends OO.ui.Layout
7496 * @param {Object} [config] Configuration options
7497 * @cfg {string} [method] HTML form `method` attribute
7498 * @cfg {string} [action] HTML form `action` attribute
7499 * @cfg {string} [enctype] HTML form `enctype` attribute
7501 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
7502 // Configuration initialization
7503 config
= config
|| {};
7505 // Parent constructor
7506 OO
.ui
.FormLayout
.super.call( this, config
);
7509 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
7513 .addClass( 'oo-ui-formLayout' )
7515 method
: config
.method
,
7516 action
: config
.action
,
7517 enctype
: config
.enctype
7523 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
7531 /* Static Properties */
7533 OO
.ui
.FormLayout
.static.tagName
= 'form';
7538 * Handle form submit events.
7540 * @param {jQuery.Event} e Submit event
7543 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
7544 this.emit( 'submit' );
7549 * Layout made of proportionally sized columns and rows.
7552 * @extends OO.ui.Layout
7555 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
7556 * @param {Object} [config] Configuration options
7557 * @cfg {number[]} [widths] Widths of columns as ratios
7558 * @cfg {number[]} [heights] Heights of rows as ratios
7560 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
7563 // Configuration initialization
7564 config
= config
|| {};
7566 // Parent constructor
7567 OO
.ui
.GridLayout
.super.call( this, config
);
7575 this.$element
.addClass( 'oo-ui-gridLayout' );
7576 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
7577 this.panels
.push( panels
[ i
] );
7578 this.$element
.append( panels
[ i
].$element
);
7580 if ( config
.widths
|| config
.heights
) {
7581 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
7583 // Arrange in columns by default
7584 widths
= this.panels
.map( function () { return 1; } );
7585 this.layout( widths
, [ 1 ] );
7591 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
7606 * Set grid dimensions.
7608 * @param {number[]} widths Widths of columns as ratios
7609 * @param {number[]} heights Heights of rows as ratios
7611 * @throws {Error} If grid is not large enough to fit all panels
7613 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
7617 cols
= widths
.length
,
7618 rows
= heights
.length
;
7620 // Verify grid is big enough to fit panels
7621 if ( cols
* rows
< this.panels
.length
) {
7622 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
7625 // Sum up denominators
7626 for ( x
= 0; x
< cols
; x
++ ) {
7629 for ( y
= 0; y
< rows
; y
++ ) {
7635 for ( x
= 0; x
< cols
; x
++ ) {
7636 this.widths
[ x
] = widths
[ x
] / xd
;
7638 for ( y
= 0; y
< rows
; y
++ ) {
7639 this.heights
[ y
] = heights
[ y
] / yd
;
7643 this.emit( 'layout' );
7647 * Update panel positions and sizes.
7651 OO
.ui
.GridLayout
.prototype.update = function () {
7652 var x
, y
, panel
, width
, height
, dimensions
,
7656 cols
= this.widths
.length
,
7657 rows
= this.heights
.length
;
7659 for ( y
= 0; y
< rows
; y
++ ) {
7660 height
= this.heights
[ y
];
7661 for ( x
= 0; x
< cols
; x
++ ) {
7662 width
= this.widths
[ x
];
7663 panel
= this.panels
[ i
];
7665 width
: ( width
* 100 ) + '%',
7666 height
: ( height
* 100 ) + '%',
7667 top
: ( top
* 100 ) + '%'
7670 if ( OO
.ui
.Element
.static.getDir( this.$.context
) === 'rtl' ) {
7671 dimensions
.right
= ( left
* 100 ) + '%';
7673 dimensions
.left
= ( left
* 100 ) + '%';
7675 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
7676 if ( width
=== 0 || height
=== 0 ) {
7677 dimensions
.visibility
= 'hidden';
7679 dimensions
.visibility
= '';
7681 panel
.$element
.css( dimensions
);
7689 this.emit( 'update' );
7693 * Get a panel at a given position.
7695 * The x and y position is affected by the current grid layout.
7697 * @param {number} x Horizontal position
7698 * @param {number} y Vertical position
7699 * @return {OO.ui.PanelLayout} The panel at the given position
7701 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
7702 return this.panels
[ ( x
* this.widths
.length
) + y
];
7706 * Layout with a content and menu area.
7708 * The menu area can be positioned at the top, after, bottom or before. The content area will fill
7709 * all remaining space.
7712 * @extends OO.ui.Layout
7715 * @param {Object} [config] Configuration options
7716 * @cfg {number|string} [menuSize='18em'] Size of menu in pixels or any CSS unit
7717 * @cfg {boolean} [showMenu=true] Show menu
7718 * @cfg {string} [position='before'] Position of menu, either `top`, `after`, `bottom` or `before`
7719 * @cfg {boolean} [collapse] Collapse the menu out of view
7721 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
7722 var positions
= this.constructor.static.menuPositions
;
7724 // Configuration initialization
7725 config
= config
|| {};
7727 // Parent constructor
7728 OO
.ui
.MenuLayout
.super.call( this, config
);
7731 this.showMenu
= config
.showMenu
!== false;
7732 this.menuSize
= config
.menuSize
|| '18em';
7733 this.menuPosition
= positions
[ config
.menuPosition
] || positions
.before
;
7738 * @property {jQuery}
7740 this.$menu
= this.$( '<div>' );
7744 * @property {jQuery}
7746 this.$content
= this.$( '<div>' );
7749 this.toggleMenu( this.showMenu
);
7752 .addClass( 'oo-ui-menuLayout-menu' )
7753 .css( this.menuPosition
.sizeProperty
, this.menuSize
);
7754 this.$content
.addClass( 'oo-ui-menuLayout-content' );
7756 .addClass( 'oo-ui-menuLayout ' + this.menuPosition
.className
)
7757 .append( this.$content
, this.$menu
);
7762 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
7764 /* Static Properties */
7766 OO
.ui
.MenuLayout
.static.menuPositions
= {
7768 sizeProperty
: 'height',
7769 className
: 'oo-ui-menuLayout-top'
7772 sizeProperty
: 'width',
7773 className
: 'oo-ui-menuLayout-after'
7776 sizeProperty
: 'height',
7777 className
: 'oo-ui-menuLayout-bottom'
7780 sizeProperty
: 'width',
7781 className
: 'oo-ui-menuLayout-before'
7790 * @param {boolean} showMenu Show menu, omit to toggle
7793 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
7794 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
7796 if ( this.showMenu
!== showMenu
) {
7797 this.showMenu
= showMenu
;
7805 * Check if menu is visible
7807 * @return {boolean} Menu is visible
7809 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
7810 return this.showMenu
;
7816 * @param {number|string} size Size of menu in pixels or any CSS unit
7819 OO
.ui
.MenuLayout
.prototype.setMenuSize = function ( size
) {
7820 this.menuSize
= size
;
7827 * Update menu and content CSS based on current menu size and visibility
7829 * This method is called internally when size or position is changed.
7831 OO
.ui
.MenuLayout
.prototype.updateSizes = function () {
7832 if ( this.showMenu
) {
7834 .css( this.menuPosition
.sizeProperty
, this.menuSize
)
7835 .css( 'overflow', '' );
7836 // Set offsets on all sides. CSS resets all but one with
7837 // 'important' rules so directionality flips are supported
7838 this.$content
.css( {
7840 right
: this.menuSize
,
7841 bottom
: this.menuSize
,
7846 .css( this.menuPosition
.sizeProperty
, 0 )
7847 .css( 'overflow', 'hidden' );
7848 this.$content
.css( {
7860 * @return {number|string} Menu size
7862 OO
.ui
.MenuLayout
.prototype.getMenuSize = function () {
7863 return this.menuSize
;
7867 * Set menu position.
7869 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
7870 * @throws {Error} If position value is not supported
7873 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
7874 var positions
= this.constructor.static.menuPositions
;
7876 if ( !positions
[ position
] ) {
7877 throw new Error( 'Cannot set position; unsupported position value: ' + position
);
7880 this.$menu
.css( this.menuPosition
.sizeProperty
, '' );
7881 this.$element
.removeClass( this.menuPosition
.className
);
7883 this.menuPosition
= positions
[ position
];
7886 this.$element
.addClass( this.menuPosition
.className
);
7892 * Get menu position.
7894 * @return {string} Menu position
7896 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
7897 return this.menuPosition
;
7901 * Layout containing a series of pages.
7904 * @extends OO.ui.MenuLayout
7907 * @param {Object} [config] Configuration options
7908 * @cfg {boolean} [continuous=false] Show all pages, one after another
7909 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
7910 * @cfg {boolean} [outlined=false] Show an outline
7911 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
7913 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
7914 // Configuration initialization
7915 config
= config
|| {};
7917 // Parent constructor
7918 OO
.ui
.BookletLayout
.super.call( this, config
);
7921 this.currentPageName
= null;
7923 this.ignoreFocus
= false;
7924 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
7925 this.$content
.append( this.stackLayout
.$element
);
7926 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
7927 this.outlineVisible
= false;
7928 this.outlined
= !!config
.outlined
;
7929 if ( this.outlined
) {
7930 this.editable
= !!config
.editable
;
7931 this.outlineControlsWidget
= null;
7932 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget( { $: this.$ } );
7933 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
7934 this.$menu
.append( this.outlinePanel
.$element
);
7935 this.outlineVisible
= true;
7936 if ( this.editable
) {
7937 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
7938 this.outlineSelectWidget
, { $: this.$ }
7942 this.toggleMenu( this.outlined
);
7945 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
7946 if ( this.outlined
) {
7947 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
7949 if ( this.autoFocus
) {
7950 // Event 'focus' does not bubble, but 'focusin' does
7951 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
7955 this.$element
.addClass( 'oo-ui-bookletLayout' );
7956 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
7957 if ( this.outlined
) {
7958 this.outlinePanel
.$element
7959 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
7960 .append( this.outlineSelectWidget
.$element
);
7961 if ( this.editable
) {
7962 this.outlinePanel
.$element
7963 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
7964 .append( this.outlineControlsWidget
.$element
);
7971 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
7977 * @param {OO.ui.PageLayout} page Current page
7982 * @param {OO.ui.PageLayout[]} page Added pages
7983 * @param {number} index Index pages were added at
7988 * @param {OO.ui.PageLayout[]} pages Removed pages
7994 * Handle stack layout focus.
7996 * @param {jQuery.Event} e Focusin event
7998 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
8001 // Find the page that an element was focused within
8002 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
8003 for ( name
in this.pages
) {
8004 // Check for page match, exclude current page to find only page changes
8005 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
8006 this.setPage( name
);
8013 * Handle stack layout set events.
8015 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
8017 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
8020 page
.scrollElementIntoView( { complete: function () {
8021 if ( layout
.autoFocus
) {
8029 * Focus the first input in the current page.
8031 * If no page is selected, the first selectable page will be selected.
8032 * If the focus is already in an element on the current page, nothing will happen.
8034 OO
.ui
.BookletLayout
.prototype.focus = function () {
8035 var $input
, page
= this.stackLayout
.getCurrentItem();
8036 if ( !page
&& this.outlined
) {
8037 this.selectFirstSelectablePage();
8038 page
= this.stackLayout
.getCurrentItem();
8043 // Only change the focus if is not already in the current page
8044 if ( !page
.$element
.find( ':focus' ).length
) {
8045 $input
= page
.$element
.find( ':input:first' );
8046 if ( $input
.length
) {
8047 $input
[ 0 ].focus();
8053 * Handle outline widget select events.
8055 * @param {OO.ui.OptionWidget|null} item Selected item
8057 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
8059 this.setPage( item
.getData() );
8064 * Check if booklet has an outline.
8068 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
8069 return this.outlined
;
8073 * Check if booklet has editing controls.
8077 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
8078 return this.editable
;
8082 * Check if booklet has a visible outline.
8086 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
8087 return this.outlined
&& this.outlineVisible
;
8091 * Hide or show the outline.
8093 * @param {boolean} [show] Show outline, omit to invert current state
8096 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
8097 if ( this.outlined
) {
8098 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
8099 this.outlineVisible
= show
;
8100 this.toggleMenu( show
);
8107 * Get the outline widget.
8109 * @param {OO.ui.PageLayout} page Page to be selected
8110 * @return {OO.ui.PageLayout|null} Closest page to another
8112 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
8113 var next
, prev
, level
,
8114 pages
= this.stackLayout
.getItems(),
8115 index
= $.inArray( page
, pages
);
8117 if ( index
!== -1 ) {
8118 next
= pages
[ index
+ 1 ];
8119 prev
= pages
[ index
- 1 ];
8120 // Prefer adjacent pages at the same level
8121 if ( this.outlined
) {
8122 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
8125 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
8131 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
8137 return prev
|| next
|| null;
8141 * Get the outline widget.
8143 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if booklet has no outline
8145 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
8146 return this.outlineSelectWidget
;
8150 * Get the outline controls widget. If the outline is not editable, null is returned.
8152 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
8154 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
8155 return this.outlineControlsWidget
;
8159 * Get a page by name.
8161 * @param {string} name Symbolic name of page
8162 * @return {OO.ui.PageLayout|undefined} Page, if found
8164 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
8165 return this.pages
[ name
];
8169 * Get the current page
8171 * @return {OO.ui.PageLayout|undefined} Current page, if found
8173 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
8174 var name
= this.getCurrentPageName();
8175 return name
? this.getPage( name
) : undefined;
8179 * Get the current page name.
8181 * @return {string|null} Current page name
8183 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
8184 return this.currentPageName
;
8188 * Add a page to the layout.
8190 * When pages are added with the same names as existing pages, the existing pages will be
8191 * automatically removed before the new pages are added.
8193 * @param {OO.ui.PageLayout[]} pages Pages to add
8194 * @param {number} index Index to insert pages after
8198 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
8199 var i
, len
, name
, page
, item
, currentIndex
,
8200 stackLayoutPages
= this.stackLayout
.getItems(),
8204 // Remove pages with same names
8205 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
8207 name
= page
.getName();
8209 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
8210 // Correct the insertion index
8211 currentIndex
= $.inArray( this.pages
[ name
], stackLayoutPages
);
8212 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
8215 remove
.push( this.pages
[ name
] );
8218 if ( remove
.length
) {
8219 this.removePages( remove
);
8223 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
8225 name
= page
.getName();
8226 this.pages
[ page
.getName() ] = page
;
8227 if ( this.outlined
) {
8228 item
= new OO
.ui
.OutlineOptionWidget( { $: this.$, data
: name
} );
8229 page
.setOutlineItem( item
);
8234 if ( this.outlined
&& items
.length
) {
8235 this.outlineSelectWidget
.addItems( items
, index
);
8236 this.selectFirstSelectablePage();
8238 this.stackLayout
.addItems( pages
, index
);
8239 this.emit( 'add', pages
, index
);
8245 * Remove a page from the layout.
8250 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
8251 var i
, len
, name
, page
,
8254 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
8256 name
= page
.getName();
8257 delete this.pages
[ name
];
8258 if ( this.outlined
) {
8259 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
8260 page
.setOutlineItem( null );
8263 if ( this.outlined
&& items
.length
) {
8264 this.outlineSelectWidget
.removeItems( items
);
8265 this.selectFirstSelectablePage();
8267 this.stackLayout
.removeItems( pages
);
8268 this.emit( 'remove', pages
);
8274 * Clear all pages from the layout.
8279 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
8281 pages
= this.stackLayout
.getItems();
8284 this.currentPageName
= null;
8285 if ( this.outlined
) {
8286 this.outlineSelectWidget
.clearItems();
8287 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
8288 pages
[ i
].setOutlineItem( null );
8291 this.stackLayout
.clearItems();
8293 this.emit( 'remove', pages
);
8299 * Set the current page by name.
8302 * @param {string} name Symbolic name of page
8304 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
8307 page
= this.pages
[ name
];
8309 if ( name
!== this.currentPageName
) {
8310 if ( this.outlined
) {
8311 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
8312 if ( selectedItem
&& selectedItem
.getData() !== name
) {
8313 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getItemFromData( name
) );
8317 if ( this.currentPageName
&& this.pages
[ this.currentPageName
] ) {
8318 this.pages
[ this.currentPageName
].setActive( false );
8319 // Blur anything focused if the next page doesn't have anything focusable - this
8320 // is not needed if the next page has something focusable because once it is focused
8321 // this blur happens automatically
8322 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
8323 $focused
= this.pages
[ this.currentPageName
].$element
.find( ':focus' );
8324 if ( $focused
.length
) {
8325 $focused
[ 0 ].blur();
8329 this.currentPageName
= name
;
8330 this.stackLayout
.setItem( page
);
8331 page
.setActive( true );
8332 this.emit( 'set', page
);
8338 * Select the first selectable page.
8342 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
8343 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
8344 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
8351 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
8354 * @extends OO.ui.Layout
8357 * @param {Object} [config] Configuration options
8358 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
8359 * @cfg {boolean} [padded=false] Pad the content from the edges
8360 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
8362 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
8363 // Configuration initialization
8364 config
= $.extend( {
8370 // Parent constructor
8371 OO
.ui
.PanelLayout
.super.call( this, config
);
8374 this.$element
.addClass( 'oo-ui-panelLayout' );
8375 if ( config
.scrollable
) {
8376 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
8378 if ( config
.padded
) {
8379 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
8381 if ( config
.expanded
) {
8382 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
8388 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
8391 * Page within an booklet layout.
8394 * @extends OO.ui.PanelLayout
8397 * @param {string} name Unique symbolic name of page
8398 * @param {Object} [config] Configuration options
8400 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
8401 // Configuration initialization
8402 config
= $.extend( { scrollable
: true }, config
);
8404 // Parent constructor
8405 OO
.ui
.PageLayout
.super.call( this, config
);
8409 this.outlineItem
= null;
8410 this.active
= false;
8413 this.$element
.addClass( 'oo-ui-pageLayout' );
8418 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
8424 * @param {boolean} active Page is active
8432 * @return {string} Symbolic name of page
8434 OO
.ui
.PageLayout
.prototype.getName = function () {
8439 * Check if page is active.
8441 * @return {boolean} Page is active
8443 OO
.ui
.PageLayout
.prototype.isActive = function () {
8450 * @return {OO.ui.OutlineOptionWidget|null} Outline item widget
8452 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
8453 return this.outlineItem
;
8459 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
8460 * outline item as desired; this method is called for setting (with an object) and unsetting
8461 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
8462 * operating on null instead of an OO.ui.OutlineOptionWidget object.
8464 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline item widget, null to clear
8467 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
8468 this.outlineItem
= outlineItem
|| null;
8469 if ( outlineItem
) {
8470 this.setupOutlineItem();
8476 * Setup outline item.
8478 * @localdoc Subclasses should override this method to adjust the outline item as desired.
8480 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline item widget to setup
8483 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
8488 * Set page active state.
8490 * @param {boolean} Page is active
8493 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
8496 if ( active
!== this.active
) {
8497 this.active
= active
;
8498 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
8499 this.emit( 'active', this.active
);
8504 * Layout containing a series of mutually exclusive pages.
8507 * @extends OO.ui.PanelLayout
8508 * @mixins OO.ui.GroupElement
8511 * @param {Object} [config] Configuration options
8512 * @cfg {boolean} [continuous=false] Show all pages, one after another
8513 * @cfg {OO.ui.Layout[]} [items] Layouts to add
8515 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
8516 // Configuration initialization
8517 config
= $.extend( { scrollable
: true }, config
);
8519 // Parent constructor
8520 OO
.ui
.StackLayout
.super.call( this, config
);
8522 // Mixin constructors
8523 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8526 this.currentItem
= null;
8527 this.continuous
= !!config
.continuous
;
8530 this.$element
.addClass( 'oo-ui-stackLayout' );
8531 if ( this.continuous
) {
8532 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
8534 if ( $.isArray( config
.items
) ) {
8535 this.addItems( config
.items
);
8541 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
8542 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
8548 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
8554 * Get the current item.
8556 * @return {OO.ui.Layout|null}
8558 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
8559 return this.currentItem
;
8563 * Unset the current item.
8566 * @param {OO.ui.StackLayout} layout
8569 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
8570 var prevItem
= this.currentItem
;
8571 if ( prevItem
=== null ) {
8575 this.currentItem
= null;
8576 this.emit( 'set', null );
8582 * Adding an existing item (by value) will move it.
8584 * @param {OO.ui.Layout[]} items Items to add
8585 * @param {number} [index] Index to insert items after
8588 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
8589 // Update the visibility
8590 this.updateHiddenState( items
, this.currentItem
);
8593 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
8595 if ( !this.currentItem
&& items
.length
) {
8596 this.setItem( items
[ 0 ] );
8605 * Items will be detached, not removed, so they can be used later.
8607 * @param {OO.ui.Layout[]} items Items to remove
8611 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
8613 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
8615 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
8616 if ( this.items
.length
) {
8617 this.setItem( this.items
[ 0 ] );
8619 this.unsetCurrentItem();
8629 * Items will be detached, not removed, so they can be used later.
8634 OO
.ui
.StackLayout
.prototype.clearItems = function () {
8635 this.unsetCurrentItem();
8636 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
8644 * Any currently shown item will be hidden.
8646 * FIXME: If the passed item to show has not been added in the items list, then
8647 * this method drops it and unsets the current item.
8649 * @param {OO.ui.Layout} item Item to show
8653 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
8654 if ( item
!== this.currentItem
) {
8655 this.updateHiddenState( this.items
, item
);
8657 if ( $.inArray( item
, this.items
) !== -1 ) {
8658 this.currentItem
= item
;
8659 this.emit( 'set', item
);
8661 this.unsetCurrentItem();
8669 * Update the visibility of all items in case of non-continuous view.
8671 * Ensure all items are hidden except for the selected one.
8672 * This method does nothing when the stack is continuous.
8674 * @param {OO.ui.Layout[]} items Item list iterate over
8675 * @param {OO.ui.Layout} [selectedItem] Selected item to show
8677 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
8680 if ( !this.continuous
) {
8681 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
8682 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
8683 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
8686 if ( selectedItem
) {
8687 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
8693 * Horizontal bar layout of tools as icon buttons.
8696 * @extends OO.ui.ToolGroup
8699 * @param {OO.ui.Toolbar} toolbar
8700 * @param {Object} [config] Configuration options
8702 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
8703 // Parent constructor
8704 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
8707 this.$element
.addClass( 'oo-ui-barToolGroup' );
8712 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
8714 /* Static Properties */
8716 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
8718 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
8720 OO
.ui
.BarToolGroup
.static.name
= 'bar';
8723 * Popup list of tools with an icon and optional label.
8727 * @extends OO.ui.ToolGroup
8728 * @mixins OO.ui.IconElement
8729 * @mixins OO.ui.IndicatorElement
8730 * @mixins OO.ui.LabelElement
8731 * @mixins OO.ui.TitledElement
8732 * @mixins OO.ui.ClippableElement
8735 * @param {OO.ui.Toolbar} toolbar
8736 * @param {Object} [config] Configuration options
8737 * @cfg {string} [header] Text to display at the top of the pop-up
8739 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
8740 // Configuration initialization
8741 config
= config
|| {};
8743 // Parent constructor
8744 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
8746 // Mixin constructors
8747 OO
.ui
.IconElement
.call( this, config
);
8748 OO
.ui
.IndicatorElement
.call( this, config
);
8749 OO
.ui
.LabelElement
.call( this, config
);
8750 OO
.ui
.TitledElement
.call( this, config
);
8751 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
8754 this.active
= false;
8755 this.dragging
= false;
8756 this.onBlurHandler
= this.onBlur
.bind( this );
8757 this.$handle
= this.$( '<span>' );
8761 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
8762 'mouseup touchend': this.onHandlePointerUp
.bind( this )
8767 .addClass( 'oo-ui-popupToolGroup-handle' )
8768 .append( this.$icon
, this.$label
, this.$indicator
);
8769 // If the pop-up should have a header, add it to the top of the toolGroup.
8770 // Note: If this feature is useful for other widgets, we could abstract it into an
8771 // OO.ui.HeaderedElement mixin constructor.
8772 if ( config
.header
!== undefined ) {
8774 .prepend( this.$( '<span>' )
8775 .addClass( 'oo-ui-popupToolGroup-header' )
8776 .text( config
.header
)
8780 .addClass( 'oo-ui-popupToolGroup' )
8781 .prepend( this.$handle
);
8786 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
8787 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
8788 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
8789 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
8790 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
8791 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
8793 /* Static Properties */
8800 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
8802 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
8804 if ( this.isDisabled() && this.isElementAttached() ) {
8805 this.setActive( false );
8810 * Handle focus being lost.
8812 * The event is actually generated from a mouseup, so it is not a normal blur event object.
8814 * @param {jQuery.Event} e Mouse up event
8816 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
8817 // Only deactivate when clicking outside the dropdown element
8818 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[ 0 ] !== this.$element
[ 0 ] ) {
8819 this.setActive( false );
8826 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
8827 // e.which is 0 for touch events, 1 for left mouse button
8828 // Only close toolgroup when a tool was actually selected
8829 // FIXME: this duplicates logic from the parent class
8830 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== this.getTargetTool( e
) ) {
8831 this.setActive( false );
8833 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
8837 * Handle mouse up events.
8839 * @param {jQuery.Event} e Mouse up event
8841 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
8846 * Handle mouse down events.
8848 * @param {jQuery.Event} e Mouse down event
8850 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
8851 // e.which is 0 for touch events, 1 for left mouse button
8852 if ( !this.isDisabled() && e
.which
<= 1 ) {
8853 this.setActive( !this.active
);
8859 * Switch into active mode.
8861 * When active, mouseup events anywhere in the document will trigger deactivation.
8863 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
8865 if ( this.active
!== value
) {
8866 this.active
= value
;
8868 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
8870 // Try anchoring the popup to the left first
8871 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
8872 this.toggleClipping( true );
8873 if ( this.isClippedHorizontally() ) {
8874 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
8875 this.toggleClipping( false );
8877 .removeClass( 'oo-ui-popupToolGroup-left' )
8878 .addClass( 'oo-ui-popupToolGroup-right' );
8879 this.toggleClipping( true );
8882 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
8883 this.$element
.removeClass(
8884 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
8886 this.toggleClipping( false );
8892 * Drop down list layout of tools as labeled icon buttons.
8894 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
8895 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
8896 * may want to use the 'promote' and 'demote' configuration options to achieve this.
8899 * @extends OO.ui.PopupToolGroup
8902 * @param {OO.ui.Toolbar} toolbar
8903 * @param {Object} [config] Configuration options
8904 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
8906 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
8907 * allowed to be collapsed.
8908 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
8910 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
8911 // Configuration initialization
8912 config
= config
|| {};
8914 // Properties (must be set before parent constructor, which calls #populate)
8915 this.allowCollapse
= config
.allowCollapse
;
8916 this.forceExpand
= config
.forceExpand
;
8917 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
8918 this.collapsibleTools
= [];
8920 // Parent constructor
8921 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
8924 this.$element
.addClass( 'oo-ui-listToolGroup' );
8929 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
8931 /* Static Properties */
8933 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
8935 OO
.ui
.ListToolGroup
.static.name
= 'list';
8942 OO
.ui
.ListToolGroup
.prototype.populate = function () {
8943 var i
, len
, allowCollapse
= [];
8945 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
8947 // Update the list of collapsible tools
8948 if ( this.allowCollapse
!== undefined ) {
8949 allowCollapse
= this.allowCollapse
;
8950 } else if ( this.forceExpand
!== undefined ) {
8951 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
8954 this.collapsibleTools
= [];
8955 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
8956 if ( this.tools
[ allowCollapse
[ i
] ] !== undefined ) {
8957 this.collapsibleTools
.push( this.tools
[ allowCollapse
[ i
] ] );
8961 // Keep at the end, even when tools are added
8962 this.$group
.append( this.getExpandCollapseTool().$element
);
8964 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
8966 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
8967 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
8968 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
8969 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
8970 this.getExpandCollapseTool().$element
.css( 'display', 'block' );
8973 this.updateCollapsibleState();
8976 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
8977 if ( this.expandCollapseTool
=== undefined ) {
8978 var ExpandCollapseTool = function () {
8979 ExpandCollapseTool
.super.apply( this, arguments
);
8982 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
8984 ExpandCollapseTool
.prototype.onSelect = function () {
8985 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
8986 this.toolGroup
.updateCollapsibleState();
8987 this.setActive( false );
8989 ExpandCollapseTool
.prototype.onUpdateState = function () {
8990 // Do nothing. Tool interface requires an implementation of this function.
8993 ExpandCollapseTool
.static.name
= 'more-fewer';
8995 this.expandCollapseTool
= new ExpandCollapseTool( this );
8997 return this.expandCollapseTool
;
9003 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
9004 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
9006 // Do not close the popup when the user wants to show more/fewer tools
9007 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
9008 // Prevent the popup list from being hidden
9009 this.setActive( true );
9015 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
9018 this.getExpandCollapseTool()
9019 .setIcon( this.expanded
? 'collapse' : 'expand' )
9020 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
9022 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
9023 this.collapsibleTools
[ i
].toggle( this.expanded
);
9028 * Drop down menu layout of tools as selectable menu items.
9031 * @extends OO.ui.PopupToolGroup
9034 * @param {OO.ui.Toolbar} toolbar
9035 * @param {Object} [config] Configuration options
9037 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
9038 // Configuration initialization
9039 config
= config
|| {};
9041 // Parent constructor
9042 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
9045 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
9048 this.$element
.addClass( 'oo-ui-menuToolGroup' );
9053 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
9055 /* Static Properties */
9057 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
9059 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
9064 * Handle the toolbar state being updated.
9066 * When the state changes, the title of each active item in the menu will be joined together and
9067 * used as a label for the group. The label will be empty if none of the items are active.
9069 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
9073 for ( name
in this.tools
) {
9074 if ( this.tools
[ name
].isActive() ) {
9075 labelTexts
.push( this.tools
[ name
].getTitle() );
9079 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
9083 * Tool that shows a popup when selected.
9087 * @extends OO.ui.Tool
9088 * @mixins OO.ui.PopupElement
9091 * @param {OO.ui.Toolbar} toolbar
9092 * @param {Object} [config] Configuration options
9094 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
9095 // Parent constructor
9096 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
9098 // Mixin constructors
9099 OO
.ui
.PopupElement
.call( this, config
);
9103 .addClass( 'oo-ui-popupTool' )
9104 .append( this.popup
.$element
);
9109 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
9110 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
9115 * Handle the tool being selected.
9119 OO
.ui
.PopupTool
.prototype.onSelect = function () {
9120 if ( !this.isDisabled() ) {
9121 this.popup
.toggle();
9123 this.setActive( false );
9128 * Handle the toolbar state being updated.
9132 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
9133 this.setActive( false );
9137 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
9139 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
9143 * @extends OO.ui.GroupElement
9146 * @param {Object} [config] Configuration options
9148 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
9149 // Parent constructor
9150 OO
.ui
.GroupWidget
.super.call( this, config
);
9155 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
9160 * Set the disabled state of the widget.
9162 * This will also update the disabled state of child widgets.
9164 * @param {boolean} disabled Disable widget
9167 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
9171 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
9172 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
9174 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
9176 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9177 this.items
[ i
].updateDisabled();
9185 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
9187 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
9188 * allows bidirectional communication.
9190 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
9197 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
9204 * Check if widget is disabled.
9206 * Checks parent if present, making disabled state inheritable.
9208 * @return {boolean} Widget is disabled
9210 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
9211 return this.disabled
||
9212 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
9216 * Set group element is in.
9218 * @param {OO.ui.GroupElement|null} group Group element, null if none
9221 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
9223 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
9224 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
9226 // Initialize item disabled states
9227 this.updateDisabled();
9233 * Mixin that adds a menu showing suggested values for a text input.
9235 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
9237 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
9238 * be aware that this will cause new suggestions to be looked up for the new value. If this is
9239 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
9243 * @deprecated Use LookupElement instead.
9246 * @param {OO.ui.TextInputWidget} input Input widget
9247 * @param {Object} [config] Configuration options
9248 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
9249 * @cfg {jQuery} [$container=input.$element] Element to render menu under
9251 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
9252 // Configuration initialization
9253 config
= config
|| {};
9256 this.lookupInput
= input
;
9257 this.$overlay
= config
.$overlay
|| this.$element
;
9258 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
9259 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
9260 input
: this.lookupInput
,
9261 $container
: config
.$container
9263 this.lookupCache
= {};
9264 this.lookupQuery
= null;
9265 this.lookupRequest
= null;
9266 this.lookupsDisabled
= false;
9267 this.lookupInputFocused
= false;
9270 this.lookupInput
.$input
.on( {
9271 focus
: this.onLookupInputFocus
.bind( this ),
9272 blur
: this.onLookupInputBlur
.bind( this ),
9273 mousedown
: this.onLookupInputMouseDown
.bind( this )
9275 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
9276 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
9279 this.$element
.addClass( 'oo-ui-lookupWidget' );
9280 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
9281 this.$overlay
.append( this.lookupMenu
.$element
);
9287 * Handle input focus event.
9289 * @param {jQuery.Event} e Input focus event
9291 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
9292 this.lookupInputFocused
= true;
9293 this.populateLookupMenu();
9297 * Handle input blur event.
9299 * @param {jQuery.Event} e Input blur event
9301 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
9302 this.closeLookupMenu();
9303 this.lookupInputFocused
= false;
9307 * Handle input mouse down event.
9309 * @param {jQuery.Event} e Input mouse down event
9311 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
9312 // Only open the menu if the input was already focused.
9313 // This way we allow the user to open the menu again after closing it with Esc
9314 // by clicking in the input. Opening (and populating) the menu when initially
9315 // clicking into the input is handled by the focus handler.
9316 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
9317 this.populateLookupMenu();
9322 * Handle input change event.
9324 * @param {string} value New input value
9326 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
9327 if ( this.lookupInputFocused
) {
9328 this.populateLookupMenu();
9333 * Handle the lookup menu being shown/hidden.
9334 * @param {boolean} visible Whether the lookup menu is now visible.
9336 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
9338 // When the menu is hidden, abort any active request and clear the menu.
9339 // This has to be done here in addition to closeLookupMenu(), because
9340 // MenuSelectWidget will close itself when the user presses Esc.
9341 this.abortLookupRequest();
9342 this.lookupMenu
.clearItems();
9349 * @return {OO.ui.TextInputMenuSelectWidget}
9351 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
9352 return this.lookupMenu
;
9356 * Disable or re-enable lookups.
9358 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
9360 * @param {boolean} disabled Disable lookups
9362 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
9363 this.lookupsDisabled
= !!disabled
;
9367 * Open the menu. If there are no entries in the menu, this does nothing.
9371 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
9372 if ( !this.lookupMenu
.isEmpty() ) {
9373 this.lookupMenu
.toggle( true );
9379 * Close the menu, empty it, and abort any pending request.
9383 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
9384 this.lookupMenu
.toggle( false );
9385 this.abortLookupRequest();
9386 this.lookupMenu
.clearItems();
9391 * Request menu items based on the input's current value, and when they arrive,
9392 * populate the menu with these items and show the menu.
9394 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
9398 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
9400 value
= this.lookupInput
.getValue();
9402 if ( this.lookupsDisabled
) {
9406 // If the input is empty, clear the menu
9407 if ( value
=== '' ) {
9408 this.closeLookupMenu();
9409 // Skip population if there is already a request pending for the current value
9410 } else if ( value
!== this.lookupQuery
) {
9411 this.getLookupMenuItems()
9412 .done( function ( items
) {
9413 widget
.lookupMenu
.clearItems();
9414 if ( items
.length
) {
9418 widget
.initializeLookupMenuSelection();
9420 widget
.lookupMenu
.toggle( false );
9423 .fail( function () {
9424 widget
.lookupMenu
.clearItems();
9432 * Select and highlight the first selectable item in the menu.
9436 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
9437 if ( !this.lookupMenu
.getSelectedItem() ) {
9438 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
9440 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
9444 * Get lookup menu items for the current query.
9446 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
9447 * of the done event. If the request was aborted to make way for a subsequent request,
9448 * this promise will not be rejected: it will remain pending forever.
9450 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
9452 value
= this.lookupInput
.getValue(),
9453 deferred
= $.Deferred(),
9456 this.abortLookupRequest();
9457 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
9458 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[ value
] ) );
9460 this.lookupInput
.pushPending();
9461 this.lookupQuery
= value
;
9462 ourRequest
= this.lookupRequest
= this.getLookupRequest();
9464 .always( function () {
9465 // We need to pop pending even if this is an old request, otherwise
9466 // the widget will remain pending forever.
9467 // TODO: this assumes that an aborted request will fail or succeed soon after
9468 // being aborted, or at least eventually. It would be nice if we could popPending()
9469 // at abort time, but only if we knew that we hadn't already called popPending()
9470 // for that request.
9471 widget
.lookupInput
.popPending();
9473 .done( function ( data
) {
9474 // If this is an old request (and aborting it somehow caused it to still succeed),
9475 // ignore its success completely
9476 if ( ourRequest
=== widget
.lookupRequest
) {
9477 widget
.lookupQuery
= null;
9478 widget
.lookupRequest
= null;
9479 widget
.lookupCache
[ value
] = widget
.getLookupCacheItemFromData( data
);
9480 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[ value
] ) );
9483 .fail( function () {
9484 // If this is an old request (or a request failing because it's being aborted),
9485 // ignore its failure completely
9486 if ( ourRequest
=== widget
.lookupRequest
) {
9487 widget
.lookupQuery
= null;
9488 widget
.lookupRequest
= null;
9493 return deferred
.promise();
9497 * Abort the currently pending lookup request, if any.
9499 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
9500 var oldRequest
= this.lookupRequest
;
9502 // First unset this.lookupRequest to the fail handler will notice
9503 // that the request is no longer current
9504 this.lookupRequest
= null;
9505 this.lookupQuery
= null;
9511 * Get a new request object of the current lookup query value.
9514 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
9516 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
9517 // Stub, implemented in subclass
9522 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
9525 * @param {Mixed} data Cached result data, usually an array
9526 * @return {OO.ui.MenuOptionWidget[]} Menu items
9528 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
9529 // Stub, implemented in subclass
9534 * Get lookup cache item from server response data.
9537 * @param {Mixed} data Response from server
9538 * @return {Mixed} Cached result data
9540 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
9541 // Stub, implemented in subclass
9546 * Set of controls for an OO.ui.OutlineSelectWidget.
9548 * Controls include moving items up and down, removing items, and adding different kinds of items.
9551 * @extends OO.ui.Widget
9552 * @mixins OO.ui.GroupElement
9553 * @mixins OO.ui.IconElement
9556 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
9557 * @param {Object} [config] Configuration options
9559 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
9560 // Configuration initialization
9561 config
= $.extend( { icon
: 'add' }, config
);
9563 // Parent constructor
9564 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
9566 // Mixin constructors
9567 OO
.ui
.GroupElement
.call( this, config
);
9568 OO
.ui
.IconElement
.call( this, config
);
9571 this.outline
= outline
;
9572 this.$movers
= this.$( '<div>' );
9573 this.upButton
= new OO
.ui
.ButtonWidget( {
9577 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
9579 this.downButton
= new OO
.ui
.ButtonWidget( {
9583 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
9585 this.removeButton
= new OO
.ui
.ButtonWidget( {
9589 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
9593 outline
.connect( this, {
9594 select
: 'onOutlineChange',
9595 add
: 'onOutlineChange',
9596 remove
: 'onOutlineChange'
9598 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
9599 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
9600 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
9603 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
9604 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
9606 .addClass( 'oo-ui-outlineControlsWidget-movers' )
9607 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
9608 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
9613 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
9614 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
9615 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
9621 * @param {number} places Number of places to move
9631 * Handle outline change events.
9633 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
9634 var i
, len
, firstMovable
, lastMovable
,
9635 items
= this.outline
.getItems(),
9636 selectedItem
= this.outline
.getSelectedItem(),
9637 movable
= selectedItem
&& selectedItem
.isMovable(),
9638 removable
= selectedItem
&& selectedItem
.isRemovable();
9643 while ( ++i
< len
) {
9644 if ( items
[ i
].isMovable() ) {
9645 firstMovable
= items
[ i
];
9651 if ( items
[ i
].isMovable() ) {
9652 lastMovable
= items
[ i
];
9657 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
9658 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
9659 this.removeButton
.setDisabled( !removable
);
9663 * Mixin for widgets with a boolean on/off state.
9669 * @param {Object} [config] Configuration options
9670 * @cfg {boolean} [value=false] Initial value
9672 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
9673 // Configuration initialization
9674 config
= config
|| {};
9680 this.$element
.addClass( 'oo-ui-toggleWidget' );
9681 this.setValue( !!config
.value
);
9688 * @param {boolean} value Changed value
9694 * Get the value of the toggle.
9698 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
9703 * Set the value of the toggle.
9705 * @param {boolean} value New value
9709 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
9711 if ( this.value
!== value
) {
9713 this.emit( 'change', value
);
9714 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
9715 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
9716 this.$element
.attr( 'aria-checked', value
.toString() );
9722 * Group widget for multiple related buttons.
9724 * Use together with OO.ui.ButtonWidget.
9727 * @extends OO.ui.Widget
9728 * @mixins OO.ui.GroupElement
9731 * @param {Object} [config] Configuration options
9732 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
9734 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
9735 // Configuration initialization
9736 config
= config
|| {};
9738 // Parent constructor
9739 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
9741 // Mixin constructors
9742 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
9745 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
9746 if ( $.isArray( config
.items
) ) {
9747 this.addItems( config
.items
);
9753 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
9754 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
9757 * Generic widget for buttons.
9760 * @extends OO.ui.Widget
9761 * @mixins OO.ui.ButtonElement
9762 * @mixins OO.ui.IconElement
9763 * @mixins OO.ui.IndicatorElement
9764 * @mixins OO.ui.LabelElement
9765 * @mixins OO.ui.TitledElement
9766 * @mixins OO.ui.FlaggedElement
9767 * @mixins OO.ui.TabIndexedElement
9770 * @param {Object} [config] Configuration options
9771 * @cfg {string} [href] Hyperlink to visit when clicked
9772 * @cfg {string} [target] Target to open hyperlink in
9774 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
9775 // Configuration initialization
9776 config
= config
|| {};
9778 // Parent constructor
9779 OO
.ui
.ButtonWidget
.super.call( this, config
);
9781 // Mixin constructors
9782 OO
.ui
.ButtonElement
.call( this, config
);
9783 OO
.ui
.IconElement
.call( this, config
);
9784 OO
.ui
.IndicatorElement
.call( this, config
);
9785 OO
.ui
.LabelElement
.call( this, config
);
9786 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
9787 OO
.ui
.FlaggedElement
.call( this, config
);
9788 OO
.ui
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
9793 this.isHyperlink
= false;
9797 click
: this.onClick
.bind( this ),
9798 keypress
: this.onKeyPress
.bind( this )
9802 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
9804 .addClass( 'oo-ui-buttonWidget' )
9805 .append( this.$button
);
9806 this.setHref( config
.href
);
9807 this.setTarget( config
.target
);
9812 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
9813 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
9814 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
9815 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
9816 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
9817 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
9818 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
9819 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TabIndexedElement
);
9830 * Handles mouse click events.
9832 * @param {jQuery.Event} e Mouse click event
9835 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
9836 if ( !this.isDisabled() ) {
9837 this.emit( 'click' );
9838 if ( this.isHyperlink
) {
9848 OO
.ui
.ButtonWidget
.prototype.onMouseDown = function ( e
) {
9849 if ( !this.isDisabled() ) {
9850 // Remove the tab-index while the button is down to prevent the button from stealing focus
9851 this.$button
.removeAttr( 'tabindex' );
9854 return OO
.ui
.ButtonElement
.prototype.onMouseDown
.call( this, e
);
9860 OO
.ui
.ButtonWidget
.prototype.onMouseUp = function ( e
) {
9861 if ( !this.isDisabled() ) {
9862 // Restore the tab-index after the button is up to restore the button's accessibility
9863 this.$button
.attr( 'tabindex', this.tabIndex
);
9866 return OO
.ui
.ButtonElement
.prototype.onMouseUp
.call( this, e
);
9870 * Handles keypress events.
9872 * @param {jQuery.Event} e Keypress event
9875 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
9876 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9877 this.emit( 'click' );
9878 if ( this.isHyperlink
) {
9886 * Get hyperlink location.
9888 * @return {string} Hyperlink location
9890 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
9895 * Get hyperlink target.
9897 * @return {string} Hyperlink target
9899 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
9904 * Set hyperlink location.
9906 * @param {string|null} href Hyperlink location, null to remove
9908 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
9909 href
= typeof href
=== 'string' ? href
: null;
9911 if ( href
!== this.href
) {
9913 if ( href
!== null ) {
9914 this.$button
.attr( 'href', href
);
9915 this.isHyperlink
= true;
9917 this.$button
.removeAttr( 'href' );
9918 this.isHyperlink
= false;
9926 * Set hyperlink target.
9928 * @param {string|null} target Hyperlink target, null to remove
9930 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
9931 target
= typeof target
=== 'string' ? target
: null;
9933 if ( target
!== this.target
) {
9934 this.target
= target
;
9935 if ( target
!== null ) {
9936 this.$button
.attr( 'target', target
);
9938 this.$button
.removeAttr( 'target' );
9946 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
9949 * @extends OO.ui.ButtonWidget
9950 * @mixins OO.ui.PendingElement
9953 * @param {Object} [config] Configuration options
9954 * @cfg {string} [action] Symbolic action name
9955 * @cfg {string[]} [modes] Symbolic mode names
9956 * @cfg {boolean} [framed=false] Render button with a frame
9958 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
9959 // Configuration initialization
9960 config
= $.extend( { framed
: false }, config
);
9962 // Parent constructor
9963 OO
.ui
.ActionWidget
.super.call( this, config
);
9965 // Mixin constructors
9966 OO
.ui
.PendingElement
.call( this, config
);
9969 this.action
= config
.action
|| '';
9970 this.modes
= config
.modes
|| [];
9975 this.$element
.addClass( 'oo-ui-actionWidget' );
9980 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
9981 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
9992 * Check if action is available in a certain mode.
9994 * @param {string} mode Name of mode
9995 * @return {boolean} Has mode
9997 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
9998 return this.modes
.indexOf( mode
) !== -1;
10002 * Get symbolic action name.
10006 OO
.ui
.ActionWidget
.prototype.getAction = function () {
10007 return this.action
;
10011 * Get symbolic action name.
10015 OO
.ui
.ActionWidget
.prototype.getModes = function () {
10016 return this.modes
.slice();
10020 * Emit a resize event if the size has changed.
10024 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
10027 if ( this.isElementAttached() ) {
10028 width
= this.$element
.width();
10029 height
= this.$element
.height();
10031 if ( width
!== this.width
|| height
!== this.height
) {
10032 this.width
= width
;
10033 this.height
= height
;
10034 this.emit( 'resize' );
10044 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
10046 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
10047 this.propagateResize();
10055 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
10057 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
10058 this.propagateResize();
10066 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
10068 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
10069 this.propagateResize();
10077 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
10079 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
10080 this.propagateResize();
10086 * Toggle visibility of button.
10088 * @param {boolean} [show] Show button, omit to toggle visibility
10091 OO
.ui
.ActionWidget
.prototype.toggle = function () {
10093 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
10094 this.propagateResize();
10100 * Button that shows and hides a popup.
10103 * @extends OO.ui.ButtonWidget
10104 * @mixins OO.ui.PopupElement
10107 * @param {Object} [config] Configuration options
10109 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
10110 // Parent constructor
10111 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
10113 // Mixin constructors
10114 OO
.ui
.PopupElement
.call( this, config
);
10118 .addClass( 'oo-ui-popupButtonWidget' )
10119 .attr( 'aria-haspopup', 'true' )
10120 .append( this.popup
.$element
);
10125 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
10126 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
10131 * Handles mouse click events.
10133 * @param {jQuery.Event} e Mouse click event
10135 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
10136 // Skip clicks within the popup
10137 if ( $.contains( this.popup
.$element
[ 0 ], e
.target
) ) {
10141 if ( !this.isDisabled() ) {
10142 this.popup
.toggle();
10144 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
10150 * Button that toggles on and off.
10153 * @extends OO.ui.ButtonWidget
10154 * @mixins OO.ui.ToggleWidget
10157 * @param {Object} [config] Configuration options
10158 * @cfg {boolean} [value=false] Initial value
10160 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
10161 // Configuration initialization
10162 config
= config
|| {};
10164 // Parent constructor
10165 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
10167 // Mixin constructors
10168 OO
.ui
.ToggleWidget
.call( this, config
);
10171 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
10176 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
10177 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
10184 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
10185 if ( !this.isDisabled() ) {
10186 this.setValue( !this.value
);
10190 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
10196 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
10198 if ( value
!== this.value
) {
10199 this.$button
.attr( 'aria-pressed', value
.toString() );
10200 this.setActive( value
);
10203 // Parent method (from mixin)
10204 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
10210 * Dropdown menu of options.
10212 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
10213 * can be accessed using the #getMenu method.
10215 * Use with OO.ui.MenuOptionWidget.
10218 * @extends OO.ui.Widget
10219 * @mixins OO.ui.IconElement
10220 * @mixins OO.ui.IndicatorElement
10221 * @mixins OO.ui.LabelElement
10222 * @mixins OO.ui.TitledElement
10225 * @param {Object} [config] Configuration options
10226 * @cfg {Object} [menu] Configuration options to pass to menu widget
10228 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
10229 // Configuration initialization
10230 config
= $.extend( { indicator
: 'down' }, config
);
10232 // Parent constructor
10233 OO
.ui
.DropdownWidget
.super.call( this, config
);
10235 // Mixin constructors
10236 OO
.ui
.IconElement
.call( this, config
);
10237 OO
.ui
.IndicatorElement
.call( this, config
);
10238 OO
.ui
.LabelElement
.call( this, config
);
10239 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
10242 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
10243 this.$handle
= this.$( '<span>' );
10246 this.$element
.on( { click
: this.onClick
.bind( this ) } );
10247 this.menu
.connect( this, { select
: 'onMenuSelect' } );
10251 .addClass( 'oo-ui-dropdownWidget-handle' )
10252 .append( this.$icon
, this.$label
, this.$indicator
);
10254 .addClass( 'oo-ui-dropdownWidget' )
10255 .append( this.$handle
, this.menu
.$element
);
10260 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
10261 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
10262 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
10263 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
10264 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
10271 * @return {OO.ui.MenuSelectWidget} Menu of widget
10273 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
10278 * Handles menu select events.
10280 * @param {OO.ui.MenuOptionWidget} item Selected menu item
10282 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
10289 selectedLabel
= item
.getLabel();
10291 // If the label is a DOM element, clone it, because setLabel will append() it
10292 if ( selectedLabel
instanceof jQuery
) {
10293 selectedLabel
= selectedLabel
.clone();
10296 this.setLabel( selectedLabel
);
10300 * Handles mouse click events.
10302 * @param {jQuery.Event} e Mouse click event
10304 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
10305 // Skip clicks within the menu
10306 if ( $.contains( this.menu
.$element
[ 0 ], e
.target
) ) {
10310 if ( !this.isDisabled() ) {
10311 if ( this.menu
.isVisible() ) {
10312 this.menu
.toggle( false );
10314 this.menu
.toggle( true );
10323 * See OO.ui.IconElement for more information.
10326 * @extends OO.ui.Widget
10327 * @mixins OO.ui.IconElement
10328 * @mixins OO.ui.TitledElement
10331 * @param {Object} [config] Configuration options
10333 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
10334 // Configuration initialization
10335 config
= config
|| {};
10337 // Parent constructor
10338 OO
.ui
.IconWidget
.super.call( this, config
);
10340 // Mixin constructors
10341 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
10342 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
10345 this.$element
.addClass( 'oo-ui-iconWidget' );
10350 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
10351 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
10352 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
10354 /* Static Properties */
10356 OO
.ui
.IconWidget
.static.tagName
= 'span';
10359 * Indicator widget.
10361 * See OO.ui.IndicatorElement for more information.
10364 * @extends OO.ui.Widget
10365 * @mixins OO.ui.IndicatorElement
10366 * @mixins OO.ui.TitledElement
10369 * @param {Object} [config] Configuration options
10371 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
10372 // Configuration initialization
10373 config
= config
|| {};
10375 // Parent constructor
10376 OO
.ui
.IndicatorWidget
.super.call( this, config
);
10378 // Mixin constructors
10379 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
10380 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
10383 this.$element
.addClass( 'oo-ui-indicatorWidget' );
10388 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
10389 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
10390 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
10392 /* Static Properties */
10394 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
10397 * Base class for input widgets.
10401 * @extends OO.ui.Widget
10402 * @mixins OO.ui.FlaggedElement
10403 * @mixins OO.ui.TabIndexedElement
10406 * @param {Object} [config] Configuration options
10407 * @cfg {string} [name=''] HTML input name
10408 * @cfg {string} [value=''] Input value
10409 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
10411 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
10412 // Configuration initialization
10413 config
= config
|| {};
10415 // Parent constructor
10416 OO
.ui
.InputWidget
.super.call( this, config
);
10419 this.$input
= this.getInputElement( config
);
10421 this.inputFilter
= config
.inputFilter
;
10423 // Mixin constructors
10424 OO
.ui
.FlaggedElement
.call( this, config
);
10425 OO
.ui
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
10428 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
10432 .attr( 'name', config
.name
)
10433 .prop( 'disabled', this.isDisabled() );
10434 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
10435 this.setValue( config
.value
);
10440 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
10441 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
10442 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.TabIndexedElement
);
10448 * @param {string} value
10454 * Get input element.
10456 * Subclasses of OO.ui.InputWidget use the `config` parameter to produce different elements in
10457 * different circumstances. The element must have a `value` property (like form elements).
10460 * @param {Object} config Configuration options
10461 * @return {jQuery} Input element
10463 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
10464 return this.$( '<input>' );
10468 * Handle potentially value-changing events.
10470 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
10472 OO
.ui
.InputWidget
.prototype.onEdit = function () {
10474 if ( !this.isDisabled() ) {
10475 // Allow the stack to clear so the value will be updated
10476 setTimeout( function () {
10477 widget
.setValue( widget
.$input
.val() );
10483 * Get the value of the input.
10485 * @return {string} Input value
10487 OO
.ui
.InputWidget
.prototype.getValue = function () {
10488 // Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
10489 // it, and we won't know unless they're kind enough to trigger a 'change' event.
10490 var value
= this.$input
.val();
10491 if ( this.value
!== value
) {
10492 this.setValue( value
);
10498 * Sets the direction of the current input, either RTL or LTR
10500 * @param {boolean} isRTL
10502 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
10503 this.$input
.prop( 'dir', isRTL
? 'rtl' : 'ltr' );
10507 * Set the value of the input.
10509 * @param {string} value New value
10513 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
10514 value
= this.cleanUpValue( value
);
10515 // Update the DOM if it has changed. Note that with cleanUpValue, it
10516 // is possible for the DOM value to change without this.value changing.
10517 if ( this.$input
.val() !== value
) {
10518 this.$input
.val( value
);
10520 if ( this.value
!== value
) {
10521 this.value
= value
;
10522 this.emit( 'change', this.value
);
10528 * Clean up incoming value.
10530 * Ensures value is a string, and converts undefined and null to empty string.
10533 * @param {string} value Original value
10534 * @return {string} Cleaned up value
10536 OO
.ui
.InputWidget
.prototype.cleanUpValue = function ( value
) {
10537 if ( value
=== undefined || value
=== null ) {
10539 } else if ( this.inputFilter
) {
10540 return this.inputFilter( String( value
) );
10542 return String( value
);
10547 * Simulate the behavior of clicking on a label bound to this input.
10549 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
10550 if ( !this.isDisabled() ) {
10551 if ( this.$input
.is( ':checkbox,:radio' ) ) {
10552 this.$input
.click();
10553 } else if ( this.$input
.is( ':input' ) ) {
10554 this.$input
[ 0 ].focus();
10562 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
10563 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
10564 if ( this.$input
) {
10565 this.$input
.prop( 'disabled', this.isDisabled() );
10575 OO
.ui
.InputWidget
.prototype.focus = function () {
10576 this.$input
[ 0 ].focus();
10585 OO
.ui
.InputWidget
.prototype.blur = function () {
10586 this.$input
[ 0 ].blur();
10591 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
10594 * @extends OO.ui.InputWidget
10595 * @mixins OO.ui.ButtonElement
10596 * @mixins OO.ui.IconElement
10597 * @mixins OO.ui.IndicatorElement
10598 * @mixins OO.ui.LabelElement
10599 * @mixins OO.ui.TitledElement
10600 * @mixins OO.ui.FlaggedElement
10603 * @param {Object} [config] Configuration options
10604 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
10605 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
10606 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
10607 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
10608 * won't be possible to set a value (which will internally become identical to the label).
10610 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
10611 // Configuration initialization
10612 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
10614 // Properties (must be set before parent constructor, which calls #setValue)
10615 this.useInputTag
= config
.useInputTag
;
10617 // Parent constructor
10618 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
10620 // Mixin constructors
10621 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
10622 OO
.ui
.IconElement
.call( this, config
);
10623 OO
.ui
.IndicatorElement
.call( this, config
);
10624 OO
.ui
.LabelElement
.call( this, config
);
10625 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
10626 OO
.ui
.FlaggedElement
.call( this, config
);
10630 click
: this.onClick
.bind( this ),
10631 keypress
: this.onKeyPress
.bind( this )
10635 if ( !config
.useInputTag
) {
10636 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
10638 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
10643 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
10644 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
10645 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
10646 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
10647 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
10648 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
10649 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
10663 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
10664 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
10665 return this.$( html
);
10671 * Overridden to support setting the 'value' of `<input/>` elements.
10673 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
10674 * text; or null for no label
10677 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
10678 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
10680 if ( this.useInputTag
) {
10681 if ( typeof label
=== 'function' ) {
10682 label
= OO
.ui
.resolveMsg( label
);
10684 if ( label
instanceof jQuery
) {
10685 label
= label
.text();
10690 this.$input
.val( label
);
10697 * Set the value of the input.
10699 * Overridden to disable for `<input/>` elements, which have value identical to the label.
10701 * @param {string} value New value
10704 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
10705 if ( !this.useInputTag
) {
10706 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
10712 * Handles mouse click events.
10714 * @param {jQuery.Event} e Mouse click event
10717 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
10718 if ( !this.isDisabled() ) {
10719 this.emit( 'click' );
10725 * Handles keypress events.
10727 * @param {jQuery.Event} e Keypress event
10730 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
10731 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
10732 this.emit( 'click' );
10738 * Checkbox input widget.
10741 * @extends OO.ui.InputWidget
10744 * @param {Object} [config] Configuration options
10745 * @cfg {boolean} [selected=false] Whether the checkbox is initially selected
10747 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
10748 // Parent constructor
10749 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
10752 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
10753 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
10758 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
10766 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
10767 return this.$( '<input type="checkbox" />' );
10773 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
10775 if ( !this.isDisabled() ) {
10776 // Allow the stack to clear so the value will be updated
10777 setTimeout( function () {
10778 widget
.setSelected( widget
.$input
.prop( 'checked' ) );
10784 * Set selection state of this checkbox.
10786 * @param {boolean} state Whether the checkbox is selected
10789 OO
.ui
.CheckboxInputWidget
.prototype.setSelected = function ( state
) {
10791 if ( this.selected
!== state
) {
10792 this.selected
= state
;
10793 this.$input
.prop( 'checked', this.selected
);
10794 this.emit( 'change', this.selected
);
10800 * Check if this checkbox is selected.
10802 * @return {boolean} Checkbox is selected
10804 OO
.ui
.CheckboxInputWidget
.prototype.isSelected = function () {
10805 // Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
10806 // it, and we won't know unless they're kind enough to trigger a 'change' event.
10807 var selected
= this.$input
.prop( 'checked' );
10808 if ( this.selected
!== selected
) {
10809 this.setSelected( selected
);
10811 return this.selected
;
10815 * A OO.ui.DropdownWidget synchronized with a `<input type=hidden>` for form submission. Intended to
10816 * be used within a OO.ui.FormLayout.
10819 * @extends OO.ui.InputWidget
10822 * @param {Object} [config] Configuration options
10823 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
10825 OO
.ui
.DropdownInputWidget
= function OoUiDropdownInputWidget( config
) {
10826 // Configuration initialization
10827 config
= config
|| {};
10829 // Properties (must be done before parent constructor which calls #setDisabled)
10830 this.dropdownWidget
= new OO
.ui
.DropdownWidget( {
10834 // Parent constructor
10835 OO
.ui
.DropdownInputWidget
.super.call( this, config
);
10838 this.dropdownWidget
.getMenu().connect( this, { select
: 'onMenuSelect' } );
10841 this.setOptions( config
.options
|| [] );
10843 .addClass( 'oo-ui-dropdownInputWidget' )
10844 .append( this.dropdownWidget
.$element
);
10849 OO
.inheritClass( OO
.ui
.DropdownInputWidget
, OO
.ui
.InputWidget
);
10857 OO
.ui
.DropdownInputWidget
.prototype.getInputElement = function () {
10858 return this.$( '<input type="hidden">' );
10862 * Handles menu select events.
10864 * @param {OO.ui.MenuOptionWidget} item Selected menu item
10866 OO
.ui
.DropdownInputWidget
.prototype.onMenuSelect = function ( item
) {
10867 this.setValue( item
.getData() );
10873 OO
.ui
.DropdownInputWidget
.prototype.setValue = function ( value
) {
10874 var item
= this.dropdownWidget
.getMenu().getItemFromData( value
);
10876 this.dropdownWidget
.getMenu().selectItem( item
);
10878 OO
.ui
.DropdownInputWidget
.super.prototype.setValue
.call( this, value
);
10885 OO
.ui
.DropdownInputWidget
.prototype.setDisabled = function ( state
) {
10886 this.dropdownWidget
.setDisabled( state
);
10887 OO
.ui
.DropdownInputWidget
.super.prototype.setDisabled
.call( this, state
);
10892 * Set the options available for this input.
10894 * @param {Object[]} options Array of menu options in the format `{ data: …, label: … }`
10897 OO
.ui
.DropdownInputWidget
.prototype.setOptions = function ( options
) {
10898 var value
= this.getValue();
10900 // Rebuild the dropdown menu
10901 this.dropdownWidget
.getMenu()
10903 .addItems( options
.map( function ( opt
) {
10904 return new OO
.ui
.MenuOptionWidget( {
10906 label
: opt
.label
!== undefined ? opt
.label
: opt
.data
10910 // Restore the previous value, or reset to something sensible
10911 if ( this.dropdownWidget
.getMenu().getItemFromData( value
) ) {
10912 // Previous value is still available, ensure consistency with the dropdown
10913 this.setValue( value
);
10915 // No longer valid, reset
10916 if ( options
.length
) {
10917 this.setValue( options
[ 0 ].data
);
10927 OO
.ui
.DropdownInputWidget
.prototype.focus = function () {
10928 this.dropdownWidget
.getMenu().toggle( true );
10935 OO
.ui
.DropdownInputWidget
.prototype.blur = function () {
10936 this.dropdownWidget
.getMenu().toggle( false );
10941 * Radio input widget.
10943 * Radio buttons only make sense as a set, and you probably want to use the OO.ui.RadioSelectWidget
10944 * class instead of using this class directly.
10947 * @extends OO.ui.InputWidget
10950 * @param {Object} [config] Configuration options
10951 * @cfg {boolean} [selected=false] Whether the radio button is initially selected
10953 OO
.ui
.RadioInputWidget
= function OoUiRadioInputWidget( config
) {
10954 // Parent constructor
10955 OO
.ui
.RadioInputWidget
.super.call( this, config
);
10958 this.$element
.addClass( 'oo-ui-radioInputWidget' );
10959 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
10964 OO
.inheritClass( OO
.ui
.RadioInputWidget
, OO
.ui
.InputWidget
);
10972 OO
.ui
.RadioInputWidget
.prototype.getInputElement = function () {
10973 return this.$( '<input type="radio" />' );
10979 OO
.ui
.RadioInputWidget
.prototype.onEdit = function () {
10980 // RadioInputWidget doesn't track its state.
10984 * Set selection state of this radio button.
10986 * @param {boolean} state Whether the button is selected
10989 OO
.ui
.RadioInputWidget
.prototype.setSelected = function ( state
) {
10990 // RadioInputWidget doesn't track its state.
10991 this.$input
.prop( 'checked', state
);
10996 * Check if this radio button is selected.
10998 * @return {boolean} Radio is selected
11000 OO
.ui
.RadioInputWidget
.prototype.isSelected = function () {
11001 return this.$input
.prop( 'checked' );
11005 * Input widget with a text field.
11008 * @extends OO.ui.InputWidget
11009 * @mixins OO.ui.IconElement
11010 * @mixins OO.ui.IndicatorElement
11011 * @mixins OO.ui.PendingElement
11014 * @param {Object} [config] Configuration options
11015 * @cfg {string} [type='text'] HTML tag `type` attribute
11016 * @cfg {string} [placeholder] Placeholder text
11017 * @cfg {boolean} [autofocus=false] Ask the browser to focus this widget, using the 'autofocus' HTML
11019 * @cfg {boolean} [readOnly=false] Prevent changes
11020 * @cfg {number} [maxLength] Maximum allowed number of characters to input
11021 * @cfg {boolean} [multiline=false] Allow multiple lines of text
11022 * @cfg {boolean} [autosize=false] Automatically resize to fit content
11023 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
11024 * @cfg {string} [labelPosition='after'] Label position, 'before' or 'after'
11025 * @cfg {RegExp|string} [validate] Regular expression to validate against (or symbolic name referencing
11026 * one, see #static-validationPatterns)
11028 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
11029 // Configuration initialization
11030 config
= $.extend( {
11032 labelPosition
: 'after',
11036 // Parent constructor
11037 OO
.ui
.TextInputWidget
.super.call( this, config
);
11039 // Mixin constructors
11040 OO
.ui
.IconElement
.call( this, config
);
11041 OO
.ui
.IndicatorElement
.call( this, config
);
11042 OO
.ui
.PendingElement
.call( this, config
);
11043 OO
.ui
.LabelElement
.call( this, config
);
11046 this.readOnly
= false;
11047 this.multiline
= !!config
.multiline
;
11048 this.autosize
= !!config
.autosize
;
11049 this.maxRows
= config
.maxRows
;
11050 this.validate
= null;
11052 // Clone for resizing
11053 if ( this.autosize
) {
11054 this.$clone
= this.$input
11056 .insertAfter( this.$input
)
11057 .addClass( 'oo-ui-element-hidden' );
11060 this.setValidation( config
.validate
);
11061 this.setPosition( config
.labelPosition
);
11065 keypress
: this.onKeyPress
.bind( this ),
11066 blur
: this.setValidityFlag
.bind( this )
11068 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
11069 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
11070 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
11071 this.on( 'labelChange', this.updatePosition
.bind( this ) );
11075 .addClass( 'oo-ui-textInputWidget' )
11076 .append( this.$icon
, this.$indicator
, this.$label
);
11077 this.setReadOnly( !!config
.readOnly
);
11078 if ( config
.placeholder
) {
11079 this.$input
.attr( 'placeholder', config
.placeholder
);
11081 if ( config
.maxLength
) {
11082 this.$input
.attr( 'maxlength', config
.maxLength
);
11084 if ( config
.autofocus
) {
11085 this.$input
.attr( 'autofocus', 'autofocus' );
11091 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
11092 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
11093 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
11094 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
11095 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.LabelElement
);
11097 /* Static properties */
11099 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
11107 * User presses enter inside the text box.
11109 * Not called if input is multiline.
11115 * User clicks the icon.
11121 * User clicks the indicator.
11129 * Handle icon mouse down events.
11131 * @param {jQuery.Event} e Mouse down event
11134 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
11135 if ( e
.which
=== 1 ) {
11136 this.$input
[ 0 ].focus();
11137 this.emit( 'icon' );
11143 * Handle indicator mouse down events.
11145 * @param {jQuery.Event} e Mouse down event
11148 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
11149 if ( e
.which
=== 1 ) {
11150 this.$input
[ 0 ].focus();
11151 this.emit( 'indicator' );
11157 * Handle key press events.
11159 * @param {jQuery.Event} e Key press event
11160 * @fires enter If enter key is pressed and input is not multiline
11162 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
11163 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
11164 this.emit( 'enter', e
);
11169 * Handle element attach events.
11171 * @param {jQuery.Event} e Element attach event
11173 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
11174 // Any previously calculated size is now probably invalid if we reattached elsewhere
11175 this.valCache
= null;
11177 this.positionLabel();
11183 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
11187 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
11193 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
11195 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
11197 this.setValidityFlag();
11203 * Check if the widget is read-only.
11205 * @return {boolean}
11207 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
11208 return this.readOnly
;
11212 * Set the read-only state of the widget.
11214 * This should probably change the widget's appearance and prevent it from being used.
11216 * @param {boolean} state Make input read-only
11219 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
11220 this.readOnly
= !!state
;
11221 this.$input
.prop( 'readOnly', this.readOnly
);
11226 * Automatically adjust the size of the text input.
11228 * This only affects multi-line inputs that are auto-sized.
11232 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
11233 var scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
11235 if ( this.multiline
&& this.autosize
&& this.$input
.val() !== this.valCache
) {
11237 .val( this.$input
.val() )
11238 .attr( 'rows', '' )
11239 // Set inline height property to 0 to measure scroll height
11240 .css( 'height', 0 );
11242 this.$clone
[ 0 ].style
.display
= 'block';
11244 this.valCache
= this.$input
.val();
11246 scrollHeight
= this.$clone
[ 0 ].scrollHeight
;
11248 // Remove inline height property to measure natural heights
11249 this.$clone
.css( 'height', '' );
11250 innerHeight
= this.$clone
.innerHeight();
11251 outerHeight
= this.$clone
.outerHeight();
11253 // Measure max rows height
11255 .attr( 'rows', this.maxRows
)
11256 .css( 'height', 'auto' )
11258 maxInnerHeight
= this.$clone
.innerHeight();
11260 // Difference between reported innerHeight and scrollHeight with no scrollbars present
11261 // Equals 1 on Blink-based browsers and 0 everywhere else
11262 measurementError
= maxInnerHeight
- this.$clone
[ 0 ].scrollHeight
;
11263 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
11265 this.$clone
[ 0 ].style
.display
= 'none';
11267 // Only apply inline height when expansion beyond natural height is needed
11268 if ( idealHeight
> innerHeight
) {
11269 // Use the difference between the inner and outer height as a buffer
11270 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
11272 this.$input
.css( 'height', '' );
11282 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
11283 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + config
.type
+ '" />' );
11287 * Check if input supports multiple lines.
11289 * @return {boolean}
11291 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
11292 return !!this.multiline
;
11296 * Check if input automatically adjusts its size.
11298 * @return {boolean}
11300 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
11301 return !!this.autosize
;
11305 * Select the contents of the input.
11309 OO
.ui
.TextInputWidget
.prototype.select = function () {
11310 this.$input
.select();
11315 * Sets the validation pattern to use.
11316 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
11317 * one, see #static-validationPatterns)
11319 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
11320 if ( validate
instanceof RegExp
) {
11321 this.validate
= validate
;
11323 this.validate
= this.constructor.static.validationPatterns
[ validate
] || /.*/;
11328 * Sets the 'invalid' flag appropriately.
11330 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
11332 this.isValid().done( function ( valid
) {
11333 widget
.setFlags( { invalid
: !valid
} );
11338 * Returns whether or not the current value is considered valid, according to the
11339 * supplied validation pattern.
11341 * @return {jQuery.Deferred}
11343 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
11344 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
11348 * Set the position of the inline label.
11350 * @param {string} labelPosition Label position, 'before' or 'after'
11353 OO
.ui
.TextInputWidget
.prototype.setPosition = function ( labelPosition
) {
11354 this.labelPosition
= labelPosition
;
11355 this.updatePosition();
11360 * Update the position of the inline label.
11364 OO
.ui
.TextInputWidget
.prototype.updatePosition = function () {
11365 var after
= this.labelPosition
=== 'after';
11368 .toggleClass( 'oo-ui-textInputWidget-labelPosition-after', this.label
&& after
)
11369 .toggleClass( 'oo-ui-textInputWidget-labelPosition-before', this.label
&& !after
);
11371 if ( this.label
) {
11372 this.positionLabel();
11379 * Position the label by setting the correct padding on the input.
11383 OO
.ui
.TextInputWidget
.prototype.positionLabel = function () {
11384 // Clear old values
11386 // Clear old values if present
11388 'padding-right': '',
11392 if ( !this.$label
.text() ) {
11396 var after
= this.labelPosition
=== 'after',
11397 rtl
= this.$element
.css( 'direction' ) === 'rtl',
11398 property
= after
=== rtl
? 'padding-left' : 'padding-right';
11400 this.$input
.css( property
, this.$label
.outerWidth() );
11406 * Text input with a menu of optional values.
11409 * @extends OO.ui.Widget
11412 * @param {Object} [config] Configuration options
11413 * @cfg {Object} [menu] Configuration options to pass to menu widget
11414 * @cfg {Object} [input] Configuration options to pass to input widget
11415 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
11417 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
11418 // Configuration initialization
11419 config
= config
|| {};
11421 // Parent constructor
11422 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
11425 this.$overlay
= config
.$overlay
|| this.$element
;
11426 this.input
= new OO
.ui
.TextInputWidget( $.extend(
11427 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
11430 this.menu
= new OO
.ui
.TextInputMenuSelectWidget( this.input
, $.extend(
11432 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
11435 disabled
: this.isDisabled()
11441 this.input
.connect( this, {
11442 change
: 'onInputChange',
11443 indicator
: 'onInputIndicator',
11444 enter
: 'onInputEnter'
11446 this.menu
.connect( this, {
11447 choose
: 'onMenuChoose',
11448 add
: 'onMenuItemsChange',
11449 remove
: 'onMenuItemsChange'
11453 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
11454 this.$overlay
.append( this.menu
.$element
);
11455 this.onMenuItemsChange();
11460 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
11465 * Get the combobox's menu.
11466 * @return {OO.ui.TextInputMenuSelectWidget} Menu widget
11468 OO
.ui
.ComboBoxWidget
.prototype.getMenu = function () {
11473 * Handle input change events.
11475 * @param {string} value New value
11477 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
11478 var match
= this.menu
.getItemFromData( value
);
11480 this.menu
.selectItem( match
);
11482 if ( !this.isDisabled() ) {
11483 this.menu
.toggle( true );
11488 * Handle input indicator events.
11490 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
11491 if ( !this.isDisabled() ) {
11492 this.menu
.toggle();
11497 * Handle input enter events.
11499 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
11500 if ( !this.isDisabled() ) {
11501 this.menu
.toggle( false );
11506 * Handle menu choose events.
11508 * @param {OO.ui.OptionWidget} item Chosen item
11510 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
11512 this.input
.setValue( item
.getData() );
11517 * Handle menu item change events.
11519 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
11520 var match
= this.menu
.getItemFromData( this.input
.getValue() );
11521 this.menu
.selectItem( match
);
11522 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
11528 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
11530 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
11532 if ( this.input
) {
11533 this.input
.setDisabled( this.isDisabled() );
11536 this.menu
.setDisabled( this.isDisabled() );
11546 * @extends OO.ui.Widget
11547 * @mixins OO.ui.LabelElement
11550 * @param {Object} [config] Configuration options
11551 * @cfg {OO.ui.InputWidget} [input] Input widget this label is for
11553 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
11554 // Configuration initialization
11555 config
= config
|| {};
11557 // Parent constructor
11558 OO
.ui
.LabelWidget
.super.call( this, config
);
11560 // Mixin constructors
11561 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
11562 OO
.ui
.TitledElement
.call( this, config
);
11565 this.input
= config
.input
;
11568 if ( this.input
instanceof OO
.ui
.InputWidget
) {
11569 this.$element
.on( 'click', this.onClick
.bind( this ) );
11573 this.$element
.addClass( 'oo-ui-labelWidget' );
11578 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
11579 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
11580 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
11582 /* Static Properties */
11584 OO
.ui
.LabelWidget
.static.tagName
= 'span';
11589 * Handles label mouse click events.
11591 * @param {jQuery.Event} e Mouse click event
11593 OO
.ui
.LabelWidget
.prototype.onClick = function () {
11594 this.input
.simulateLabelClick();
11599 * Generic option widget for use with OO.ui.SelectWidget.
11602 * @extends OO.ui.Widget
11603 * @mixins OO.ui.LabelElement
11604 * @mixins OO.ui.FlaggedElement
11607 * @param {Object} [config] Configuration options
11609 OO
.ui
.OptionWidget
= function OoUiOptionWidget( config
) {
11610 // Configuration initialization
11611 config
= config
|| {};
11613 // Parent constructor
11614 OO
.ui
.OptionWidget
.super.call( this, config
);
11616 // Mixin constructors
11617 OO
.ui
.ItemWidget
.call( this );
11618 OO
.ui
.LabelElement
.call( this, config
);
11619 OO
.ui
.FlaggedElement
.call( this, config
);
11622 this.selected
= false;
11623 this.highlighted
= false;
11624 this.pressed
= false;
11628 .data( 'oo-ui-optionWidget', this )
11629 .attr( 'role', 'option' )
11630 .addClass( 'oo-ui-optionWidget' )
11631 .append( this.$label
);
11636 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
11637 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
11638 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
11639 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
11641 /* Static Properties */
11643 OO
.ui
.OptionWidget
.static.selectable
= true;
11645 OO
.ui
.OptionWidget
.static.highlightable
= true;
11647 OO
.ui
.OptionWidget
.static.pressable
= true;
11649 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
11654 * Check if option can be selected.
11656 * @return {boolean} Item is selectable
11658 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
11659 return this.constructor.static.selectable
&& !this.isDisabled();
11663 * Check if option can be highlighted.
11665 * @return {boolean} Item is highlightable
11667 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
11668 return this.constructor.static.highlightable
&& !this.isDisabled();
11672 * Check if option can be pressed.
11674 * @return {boolean} Item is pressable
11676 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
11677 return this.constructor.static.pressable
&& !this.isDisabled();
11681 * Check if option is selected.
11683 * @return {boolean} Item is selected
11685 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
11686 return this.selected
;
11690 * Check if option is highlighted.
11692 * @return {boolean} Item is highlighted
11694 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
11695 return this.highlighted
;
11699 * Check if option is pressed.
11701 * @return {boolean} Item is pressed
11703 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
11704 return this.pressed
;
11708 * Set selected state.
11710 * @param {boolean} [state=false] Select option
11713 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
11714 if ( this.constructor.static.selectable
) {
11715 this.selected
= !!state
;
11716 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
11717 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
11718 this.scrollElementIntoView();
11720 this.updateThemeClasses();
11726 * Set highlighted state.
11728 * @param {boolean} [state=false] Highlight option
11731 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
11732 if ( this.constructor.static.highlightable
) {
11733 this.highlighted
= !!state
;
11734 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
11735 this.updateThemeClasses();
11741 * Set pressed state.
11743 * @param {boolean} [state=false] Press option
11746 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
11747 if ( this.constructor.static.pressable
) {
11748 this.pressed
= !!state
;
11749 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
11750 this.updateThemeClasses();
11756 * Option widget with an option icon and indicator.
11758 * Use together with OO.ui.SelectWidget.
11761 * @extends OO.ui.OptionWidget
11762 * @mixins OO.ui.IconElement
11763 * @mixins OO.ui.IndicatorElement
11766 * @param {Object} [config] Configuration options
11768 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( config
) {
11769 // Parent constructor
11770 OO
.ui
.DecoratedOptionWidget
.super.call( this, config
);
11772 // Mixin constructors
11773 OO
.ui
.IconElement
.call( this, config
);
11774 OO
.ui
.IndicatorElement
.call( this, config
);
11778 .addClass( 'oo-ui-decoratedOptionWidget' )
11779 .prepend( this.$icon
)
11780 .append( this.$indicator
);
11785 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
11786 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
11787 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
11790 * Option widget that looks like a button.
11792 * Use together with OO.ui.ButtonSelectWidget.
11795 * @extends OO.ui.DecoratedOptionWidget
11796 * @mixins OO.ui.ButtonElement
11797 * @mixins OO.ui.TabIndexedElement
11800 * @param {Object} [config] Configuration options
11802 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
11803 // Parent constructor
11804 OO
.ui
.ButtonOptionWidget
.super.call( this, config
);
11806 // Mixin constructors
11807 OO
.ui
.ButtonElement
.call( this, config
);
11808 OO
.ui
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
11811 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
11812 this.$button
.append( this.$element
.contents() );
11813 this.$element
.append( this.$button
);
11818 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11819 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
11820 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.TabIndexedElement
);
11822 /* Static Properties */
11824 // Allow button mouse down events to pass through so they can be handled by the parent select widget
11825 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
11832 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
11833 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
11835 if ( this.constructor.static.selectable
) {
11836 this.setActive( state
);
11843 * Option widget that looks like a radio button.
11845 * Use together with OO.ui.RadioSelectWidget.
11848 * @extends OO.ui.OptionWidget
11851 * @param {Object} [config] Configuration options
11853 OO
.ui
.RadioOptionWidget
= function OoUiRadioOptionWidget( config
) {
11854 // Parent constructor
11855 OO
.ui
.RadioOptionWidget
.super.call( this, config
);
11858 this.radio
= new OO
.ui
.RadioInputWidget( { value
: config
.data
} );
11862 .addClass( 'oo-ui-radioOptionWidget' )
11863 .prepend( this.radio
.$element
);
11868 OO
.inheritClass( OO
.ui
.RadioOptionWidget
, OO
.ui
.OptionWidget
);
11870 /* Static Properties */
11872 OO
.ui
.RadioOptionWidget
.static.highlightable
= false;
11874 OO
.ui
.RadioOptionWidget
.static.pressable
= false;
11881 OO
.ui
.RadioOptionWidget
.prototype.setSelected = function ( state
) {
11882 OO
.ui
.RadioOptionWidget
.super.prototype.setSelected
.call( this, state
);
11884 this.radio
.setSelected( state
);
11890 * Item of an OO.ui.MenuSelectWidget.
11893 * @extends OO.ui.DecoratedOptionWidget
11896 * @param {Object} [config] Configuration options
11898 OO
.ui
.MenuOptionWidget
= function OoUiMenuOptionWidget( config
) {
11899 // Configuration initialization
11900 config
= $.extend( { icon
: 'check' }, config
);
11902 // Parent constructor
11903 OO
.ui
.MenuOptionWidget
.super.call( this, config
);
11907 .attr( 'role', 'menuitem' )
11908 .addClass( 'oo-ui-menuOptionWidget' );
11913 OO
.inheritClass( OO
.ui
.MenuOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11916 * Section to group one or more items in a OO.ui.MenuSelectWidget.
11919 * @extends OO.ui.DecoratedOptionWidget
11922 * @param {Object} [config] Configuration options
11924 OO
.ui
.MenuSectionOptionWidget
= function OoUiMenuSectionOptionWidget( config
) {
11925 // Parent constructor
11926 OO
.ui
.MenuSectionOptionWidget
.super.call( this, config
);
11929 this.$element
.addClass( 'oo-ui-menuSectionOptionWidget' );
11934 OO
.inheritClass( OO
.ui
.MenuSectionOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11936 /* Static Properties */
11938 OO
.ui
.MenuSectionOptionWidget
.static.selectable
= false;
11940 OO
.ui
.MenuSectionOptionWidget
.static.highlightable
= false;
11943 * Items for an OO.ui.OutlineSelectWidget.
11946 * @extends OO.ui.DecoratedOptionWidget
11949 * @param {Object} [config] Configuration options
11950 * @cfg {number} [level] Indentation level
11951 * @cfg {boolean} [movable] Allow modification from outline controls
11953 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
11954 // Configuration initialization
11955 config
= config
|| {};
11957 // Parent constructor
11958 OO
.ui
.OutlineOptionWidget
.super.call( this, config
);
11962 this.movable
= !!config
.movable
;
11963 this.removable
= !!config
.removable
;
11966 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
11967 this.setLevel( config
.level
);
11972 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11974 /* Static Properties */
11976 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
11978 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
11980 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
11982 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
11987 * Check if item is movable.
11989 * Movability is used by outline controls.
11991 * @return {boolean} Item is movable
11993 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
11994 return this.movable
;
11998 * Check if item is removable.
12000 * Removability is used by outline controls.
12002 * @return {boolean} Item is removable
12004 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
12005 return this.removable
;
12009 * Get indentation level.
12011 * @return {number} Indentation level
12013 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
12020 * Movability is used by outline controls.
12022 * @param {boolean} movable Item is movable
12025 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
12026 this.movable
= !!movable
;
12027 this.updateThemeClasses();
12032 * Set removability.
12034 * Removability is used by outline controls.
12036 * @param {boolean} movable Item is removable
12039 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
12040 this.removable
= !!removable
;
12041 this.updateThemeClasses();
12046 * Set indentation level.
12048 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
12051 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
12052 var levels
= this.constructor.static.levels
,
12053 levelClass
= this.constructor.static.levelClass
,
12056 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
12058 if ( this.level
=== i
) {
12059 this.$element
.addClass( levelClass
+ i
);
12061 this.$element
.removeClass( levelClass
+ i
);
12064 this.updateThemeClasses();
12070 * Container for content that is overlaid and positioned absolutely.
12073 * @extends OO.ui.Widget
12074 * @mixins OO.ui.LabelElement
12077 * @param {Object} [config] Configuration options
12078 * @cfg {number} [width=320] Width of popup in pixels
12079 * @cfg {number} [height] Height of popup, omit to use automatic height
12080 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
12081 * @cfg {string} [align='center'] Alignment of popup to origin
12082 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
12083 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
12084 * @cfg {jQuery} [$content] Content to append to the popup's body
12085 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
12086 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
12087 * @cfg {boolean} [head] Show label and close button at the top
12088 * @cfg {boolean} [padded] Add padding to the body
12090 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
12091 // Configuration initialization
12092 config
= config
|| {};
12094 // Parent constructor
12095 OO
.ui
.PopupWidget
.super.call( this, config
);
12097 // Mixin constructors
12098 OO
.ui
.LabelElement
.call( this, config
);
12099 OO
.ui
.ClippableElement
.call( this, config
);
12102 this.visible
= false;
12103 this.$popup
= this.$( '<div>' );
12104 this.$head
= this.$( '<div>' );
12105 this.$body
= this.$( '<div>' );
12106 this.$anchor
= this.$( '<div>' );
12107 // If undefined, will be computed lazily in updateDimensions()
12108 this.$container
= config
.$container
;
12109 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
12110 this.autoClose
= !!config
.autoClose
;
12111 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
12112 this.transitionTimeout
= null;
12113 this.anchor
= null;
12114 this.width
= config
.width
!== undefined ? config
.width
: 320;
12115 this.height
= config
.height
!== undefined ? config
.height
: null;
12116 this.align
= config
.align
|| 'center';
12117 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
12118 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
12121 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
12124 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
12125 this.$body
.addClass( 'oo-ui-popupWidget-body' );
12126 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
12128 .addClass( 'oo-ui-popupWidget-head' )
12129 .append( this.$label
, this.closeButton
.$element
);
12130 if ( !config
.head
) {
12131 this.$head
.addClass( 'oo-ui-element-hidden' );
12134 .addClass( 'oo-ui-popupWidget-popup' )
12135 .append( this.$head
, this.$body
);
12137 .addClass( 'oo-ui-popupWidget oo-ui-element-hidden' )
12138 .append( this.$popup
, this.$anchor
);
12139 // Move content, which was added to #$element by OO.ui.Widget, to the body
12140 if ( config
.$content
instanceof jQuery
) {
12141 this.$body
.append( config
.$content
);
12143 if ( config
.padded
) {
12144 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
12146 this.setClippableElement( this.$body
);
12151 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
12152 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
12153 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
12158 * Handles mouse down events.
12160 * @param {jQuery.Event} e Mouse down event
12162 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
12164 this.isVisible() &&
12165 !$.contains( this.$element
[ 0 ], e
.target
) &&
12166 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
12168 this.toggle( false );
12173 * Bind mouse down listener.
12175 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
12176 // Capture clicks outside popup
12177 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
12181 * Handles close button click events.
12183 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
12184 if ( this.isVisible() ) {
12185 this.toggle( false );
12190 * Unbind mouse down listener.
12192 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
12193 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
12197 * Set whether to show a anchor.
12199 * @param {boolean} [show] Show anchor, omit to toggle
12201 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
12202 show
= show
=== undefined ? !this.anchored
: !!show
;
12204 if ( this.anchored
!== show
) {
12206 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
12208 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
12210 this.anchored
= show
;
12215 * Check if showing a anchor.
12217 * @return {boolean} anchor is visible
12219 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
12220 return this.anchor
;
12226 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
12227 show
= show
=== undefined ? !this.isVisible() : !!show
;
12229 var change
= show
!== this.isVisible();
12232 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
12236 if ( this.autoClose
) {
12237 this.bindMouseDownListener();
12239 this.updateDimensions();
12240 this.toggleClipping( true );
12242 this.toggleClipping( false );
12243 if ( this.autoClose
) {
12244 this.unbindMouseDownListener();
12253 * Set the size of the popup.
12255 * Changing the size may also change the popup's position depending on the alignment.
12257 * @param {number} width Width
12258 * @param {number} height Height
12259 * @param {boolean} [transition=false] Use a smooth transition
12262 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
12263 this.width
= width
;
12264 this.height
= height
!== undefined ? height
: null;
12265 if ( this.isVisible() ) {
12266 this.updateDimensions( transition
);
12271 * Update the size and position.
12273 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
12274 * be called automatically.
12276 * @param {boolean} [transition=false] Use a smooth transition
12279 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
12280 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
12281 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
12284 if ( !this.$container
) {
12285 // Lazy-initialize $container if not specified in constructor
12286 this.$container
= this.$( this.getClosestScrollableElementContainer() );
12289 // Set height and width before measuring things, since it might cause our measurements
12290 // to change (e.g. due to scrollbars appearing or disappearing)
12293 height
: this.height
!== null ? this.height
: 'auto'
12296 // Compute initial popupOffset based on alignment
12297 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[ this.align
];
12299 // Figure out if this will cause the popup to go beyond the edge of the container
12300 originOffset
= this.$element
.offset().left
;
12301 containerLeft
= this.$container
.offset().left
;
12302 containerWidth
= this.$container
.innerWidth();
12303 containerRight
= containerLeft
+ containerWidth
;
12304 popupLeft
= popupOffset
- this.containerPadding
;
12305 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
12306 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
12307 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
12309 // Adjust offset to make the popup not go beyond the edge, if needed
12310 if ( overlapRight
< 0 ) {
12311 popupOffset
+= overlapRight
;
12312 } else if ( overlapLeft
< 0 ) {
12313 popupOffset
-= overlapLeft
;
12316 // Adjust offset to avoid anchor being rendered too close to the edge
12317 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
12318 // TODO: Find a measurement that works for CSS anchors and image anchors
12319 anchorWidth
= this.$anchor
[ 0 ].scrollWidth
* 2;
12320 if ( popupOffset
+ this.width
< anchorWidth
) {
12321 popupOffset
= anchorWidth
- this.width
;
12322 } else if ( -popupOffset
< anchorWidth
) {
12323 popupOffset
= -anchorWidth
;
12326 // Prevent transition from being interrupted
12327 clearTimeout( this.transitionTimeout
);
12328 if ( transition
) {
12329 // Enable transition
12330 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
12333 // Position body relative to anchor
12334 this.$popup
.css( 'margin-left', popupOffset
);
12336 if ( transition
) {
12337 // Prevent transitioning after transition is complete
12338 this.transitionTimeout
= setTimeout( function () {
12339 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
12342 // Prevent transitioning immediately
12343 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
12346 // Reevaluate clipping state since we've relocated and resized the popup
12353 * Progress bar widget.
12356 * @extends OO.ui.Widget
12359 * @param {Object} [config] Configuration options
12360 * @cfg {number|boolean} [progress=false] Initial progress percent or false for indeterminate
12362 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
12363 // Configuration initialization
12364 config
= config
|| {};
12366 // Parent constructor
12367 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
12370 this.$bar
= this.$( '<div>' );
12371 this.progress
= null;
12374 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
12375 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar' );
12378 role
: 'progressbar',
12379 'aria-valuemin': 0,
12380 'aria-valuemax': 100
12382 .addClass( 'oo-ui-progressBarWidget' )
12383 .append( this.$bar
);
12388 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
12390 /* Static Properties */
12392 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
12397 * Get progress percent
12399 * @return {number} Progress percent
12401 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
12402 return this.progress
;
12406 * Set progress percent
12408 * @param {number|boolean} progress Progress percent or false for indeterminate
12410 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
12411 this.progress
= progress
;
12413 if ( progress
!== false ) {
12414 this.$bar
.css( 'width', this.progress
+ '%' );
12415 this.$element
.attr( 'aria-valuenow', this.progress
);
12417 this.$bar
.css( 'width', '' );
12418 this.$element
.removeAttr( 'aria-valuenow' );
12420 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
12426 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
12427 * Results are cleared and populated each time the query is changed.
12430 * @extends OO.ui.Widget
12433 * @param {Object} [config] Configuration options
12434 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
12435 * @cfg {string} [value] Initial query value
12437 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
12438 // Configuration initialization
12439 config
= config
|| {};
12441 // Parent constructor
12442 OO
.ui
.SearchWidget
.super.call( this, config
);
12445 this.query
= new OO
.ui
.TextInputWidget( {
12448 placeholder
: config
.placeholder
,
12449 value
: config
.value
12451 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
12452 this.$query
= this.$( '<div>' );
12453 this.$results
= this.$( '<div>' );
12456 this.query
.connect( this, {
12457 change
: 'onQueryChange',
12458 enter
: 'onQueryEnter'
12460 this.results
.connect( this, {
12461 highlight
: 'onResultsHighlight',
12462 select
: 'onResultsSelect'
12464 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
12468 .addClass( 'oo-ui-searchWidget-query' )
12469 .append( this.query
.$element
);
12471 .addClass( 'oo-ui-searchWidget-results' )
12472 .append( this.results
.$element
);
12474 .addClass( 'oo-ui-searchWidget' )
12475 .append( this.$results
, this.$query
);
12480 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
12486 * @param {Object|null} item Item data or null if no item is highlighted
12491 * @param {Object|null} item Item data or null if no item is selected
12497 * Handle query key down events.
12499 * @param {jQuery.Event} e Key down event
12501 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
12502 var highlightedItem
, nextItem
,
12503 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
12506 highlightedItem
= this.results
.getHighlightedItem();
12507 if ( !highlightedItem
) {
12508 highlightedItem
= this.results
.getSelectedItem();
12510 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
12511 this.results
.highlightItem( nextItem
);
12512 nextItem
.scrollElementIntoView();
12517 * Handle select widget select events.
12519 * Clears existing results. Subclasses should repopulate items according to new query.
12521 * @param {string} value New value
12523 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
12525 this.results
.clearItems();
12529 * Handle select widget enter key events.
12531 * Selects highlighted item.
12533 * @param {string} value New value
12535 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
12537 this.results
.selectItem( this.results
.getHighlightedItem() );
12541 * Handle select widget highlight events.
12543 * @param {OO.ui.OptionWidget} item Highlighted item
12546 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
12547 this.emit( 'highlight', item
? item
.getData() : null );
12551 * Handle select widget select events.
12553 * @param {OO.ui.OptionWidget} item Selected item
12556 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
12557 this.emit( 'select', item
? item
.getData() : null );
12561 * Get the query input.
12563 * @return {OO.ui.TextInputWidget} Query input
12565 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
12570 * Get the results list.
12572 * @return {OO.ui.SelectWidget} Select list
12574 OO
.ui
.SearchWidget
.prototype.getResults = function () {
12575 return this.results
;
12579 * Generic selection of options.
12581 * Items can contain any rendering. Any widget that provides options, from which the user must
12582 * choose one, should be built on this class.
12584 * Use together with OO.ui.OptionWidget.
12587 * @extends OO.ui.Widget
12588 * @mixins OO.ui.GroupElement
12591 * @param {Object} [config] Configuration options
12592 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
12594 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
12595 // Configuration initialization
12596 config
= config
|| {};
12598 // Parent constructor
12599 OO
.ui
.SelectWidget
.super.call( this, config
);
12601 // Mixin constructors
12602 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
12605 this.pressed
= false;
12606 this.selecting
= null;
12607 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
12608 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
12611 this.$element
.on( {
12612 mousedown
: this.onMouseDown
.bind( this ),
12613 mouseover
: this.onMouseOver
.bind( this ),
12614 mouseleave
: this.onMouseLeave
.bind( this )
12618 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
12619 if ( $.isArray( config
.items
) ) {
12620 this.addItems( config
.items
);
12626 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
12628 // Need to mixin base class as well
12629 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
12630 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
12636 * @param {OO.ui.OptionWidget|null} item Highlighted item
12641 * @param {OO.ui.OptionWidget|null} item Pressed item
12646 * @param {OO.ui.OptionWidget|null} item Selected item
12651 * @param {OO.ui.OptionWidget|null} item Chosen item
12656 * @param {OO.ui.OptionWidget[]} items Added items
12657 * @param {number} index Index items were added at
12662 * @param {OO.ui.OptionWidget[]} items Removed items
12668 * Handle mouse down events.
12671 * @param {jQuery.Event} e Mouse down event
12673 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
12676 if ( !this.isDisabled() && e
.which
=== 1 ) {
12677 this.togglePressed( true );
12678 item
= this.getTargetItem( e
);
12679 if ( item
&& item
.isSelectable() ) {
12680 this.pressItem( item
);
12681 this.selecting
= item
;
12682 this.getElementDocument().addEventListener(
12684 this.onMouseUpHandler
,
12687 this.getElementDocument().addEventListener(
12689 this.onMouseMoveHandler
,
12698 * Handle mouse up events.
12701 * @param {jQuery.Event} e Mouse up event
12703 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
12706 this.togglePressed( false );
12707 if ( !this.selecting
) {
12708 item
= this.getTargetItem( e
);
12709 if ( item
&& item
.isSelectable() ) {
12710 this.selecting
= item
;
12713 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
12714 this.pressItem( null );
12715 this.chooseItem( this.selecting
);
12716 this.selecting
= null;
12719 this.getElementDocument().removeEventListener(
12721 this.onMouseUpHandler
,
12724 this.getElementDocument().removeEventListener(
12726 this.onMouseMoveHandler
,
12734 * Handle mouse move events.
12737 * @param {jQuery.Event} e Mouse move event
12739 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
12742 if ( !this.isDisabled() && this.pressed
) {
12743 item
= this.getTargetItem( e
);
12744 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
12745 this.pressItem( item
);
12746 this.selecting
= item
;
12753 * Handle mouse over events.
12756 * @param {jQuery.Event} e Mouse over event
12758 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
12761 if ( !this.isDisabled() ) {
12762 item
= this.getTargetItem( e
);
12763 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
12769 * Handle mouse leave events.
12772 * @param {jQuery.Event} e Mouse over event
12774 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
12775 if ( !this.isDisabled() ) {
12776 this.highlightItem( null );
12782 * Get the closest item to a jQuery.Event.
12785 * @param {jQuery.Event} e
12786 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
12788 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
12789 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
12790 if ( $item
.length
) {
12791 return $item
.data( 'oo-ui-optionWidget' );
12797 * Get selected item.
12799 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
12801 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
12804 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12805 if ( this.items
[ i
].isSelected() ) {
12806 return this.items
[ i
];
12813 * Get highlighted item.
12815 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
12817 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
12820 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12821 if ( this.items
[ i
].isHighlighted() ) {
12822 return this.items
[ i
];
12829 * Toggle pressed state.
12831 * @param {boolean} pressed An option is being pressed
12833 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
12834 if ( pressed
=== undefined ) {
12835 pressed
= !this.pressed
;
12837 if ( pressed
!== this.pressed
) {
12839 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
12840 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
12841 this.pressed
= pressed
;
12846 * Highlight an item.
12848 * Highlighting is mutually exclusive.
12850 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
12854 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
12855 var i
, len
, highlighted
,
12858 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12859 highlighted
= this.items
[ i
] === item
;
12860 if ( this.items
[ i
].isHighlighted() !== highlighted
) {
12861 this.items
[ i
].setHighlighted( highlighted
);
12866 this.emit( 'highlight', item
);
12875 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
12879 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
12880 var i
, len
, selected
,
12883 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12884 selected
= this.items
[ i
] === item
;
12885 if ( this.items
[ i
].isSelected() !== selected
) {
12886 this.items
[ i
].setSelected( selected
);
12891 this.emit( 'select', item
);
12900 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
12904 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
12905 var i
, len
, pressed
,
12908 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12909 pressed
= this.items
[ i
] === item
;
12910 if ( this.items
[ i
].isPressed() !== pressed
) {
12911 this.items
[ i
].setPressed( pressed
);
12916 this.emit( 'press', item
);
12925 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
12926 * an item is selected using the keyboard or mouse.
12928 * @param {OO.ui.OptionWidget} item Item to choose
12932 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
12933 this.selectItem( item
);
12934 this.emit( 'choose', item
);
12940 * Get an item relative to another one.
12942 * @param {OO.ui.OptionWidget|null} item Item to start at, null to get relative to list start
12943 * @param {number} direction Direction to move in, -1 to move backward, 1 to move forward
12944 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
12946 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
12947 var currentIndex
, nextIndex
, i
,
12948 increase
= direction
> 0 ? 1 : -1,
12949 len
= this.items
.length
;
12951 if ( item
instanceof OO
.ui
.OptionWidget
) {
12952 currentIndex
= $.inArray( item
, this.items
);
12953 nextIndex
= ( currentIndex
+ increase
+ len
) % len
;
12955 // If no item is selected and moving forward, start at the beginning.
12956 // If moving backward, start at the end.
12957 nextIndex
= direction
> 0 ? 0 : len
- 1;
12960 for ( i
= 0; i
< len
; i
++ ) {
12961 item
= this.items
[ nextIndex
];
12962 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
12965 nextIndex
= ( nextIndex
+ increase
+ len
) % len
;
12971 * Get the next selectable item.
12973 * @return {OO.ui.OptionWidget|null} Item, `null` if there aren't any selectable items
12975 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
12978 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12979 item
= this.items
[ i
];
12980 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
12991 * @param {OO.ui.OptionWidget[]} items Items to add
12992 * @param {number} [index] Index to insert items after
12996 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
12998 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
13000 // Always provide an index, even if it was omitted
13001 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
13009 * Items will be detached, not removed, so they can be used later.
13011 * @param {OO.ui.OptionWidget[]} items Items to remove
13015 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
13018 // Deselect items being removed
13019 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
13021 if ( item
.isSelected() ) {
13022 this.selectItem( null );
13027 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
13029 this.emit( 'remove', items
);
13037 * Items will be detached, not removed, so they can be used later.
13042 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
13043 var items
= this.items
.slice();
13046 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
13049 this.selectItem( null );
13051 this.emit( 'remove', items
);
13057 * Select widget containing button options.
13059 * Use together with OO.ui.ButtonOptionWidget.
13062 * @extends OO.ui.SelectWidget
13065 * @param {Object} [config] Configuration options
13067 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
13068 // Parent constructor
13069 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
13072 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
13077 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
13080 * Select widget containing radio button options.
13082 * Use together with OO.ui.RadioOptionWidget.
13085 * @extends OO.ui.SelectWidget
13088 * @param {Object} [config] Configuration options
13090 OO
.ui
.RadioSelectWidget
= function OoUiRadioSelectWidget( config
) {
13091 // Parent constructor
13092 OO
.ui
.RadioSelectWidget
.super.call( this, config
);
13095 this.$element
.addClass( 'oo-ui-radioSelectWidget' );
13100 OO
.inheritClass( OO
.ui
.RadioSelectWidget
, OO
.ui
.SelectWidget
);
13103 * Overlaid menu of options.
13105 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
13108 * Use together with OO.ui.MenuOptionWidget.
13111 * @extends OO.ui.SelectWidget
13112 * @mixins OO.ui.ClippableElement
13115 * @param {Object} [config] Configuration options
13116 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
13117 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
13118 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
13120 OO
.ui
.MenuSelectWidget
= function OoUiMenuSelectWidget( config
) {
13121 // Configuration initialization
13122 config
= config
|| {};
13124 // Parent constructor
13125 OO
.ui
.MenuSelectWidget
.super.call( this, config
);
13127 // Mixin constructors
13128 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
13131 this.visible
= false;
13132 this.newItems
= null;
13133 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
13134 this.$input
= config
.input
? config
.input
.$input
: null;
13135 this.$widget
= config
.widget
? config
.widget
.$element
: null;
13136 this.$previousFocus
= null;
13137 this.isolated
= !config
.input
;
13138 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
13139 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
13143 .addClass( 'oo-ui-menuSelectWidget oo-ui-element-hidden' )
13144 .attr( 'role', 'menu' );
13149 OO
.inheritClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.SelectWidget
);
13150 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.ClippableElement
);
13155 * Handles document mouse down events.
13157 * @param {jQuery.Event} e Key down event
13159 OO
.ui
.MenuSelectWidget
.prototype.onDocumentMouseDown = function ( e
) {
13161 !OO
.ui
.contains( this.$element
[ 0 ], e
.target
, true ) &&
13162 ( !this.$widget
|| !OO
.ui
.contains( this.$widget
[ 0 ], e
.target
, true ) )
13164 this.toggle( false );
13169 * Handles key down events.
13171 * @param {jQuery.Event} e Key down event
13173 OO
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
13176 highlightItem
= this.getHighlightedItem();
13178 if ( !this.isDisabled() && this.isVisible() ) {
13179 if ( !highlightItem
) {
13180 highlightItem
= this.getSelectedItem();
13182 switch ( e
.keyCode
) {
13183 case OO
.ui
.Keys
.ENTER
:
13184 this.chooseItem( highlightItem
);
13187 case OO
.ui
.Keys
.UP
:
13188 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
13191 case OO
.ui
.Keys
.DOWN
:
13192 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
13195 case OO
.ui
.Keys
.ESCAPE
:
13196 if ( highlightItem
) {
13197 highlightItem
.setHighlighted( false );
13199 this.toggle( false );
13205 this.highlightItem( nextItem
);
13206 nextItem
.scrollElementIntoView();
13210 e
.preventDefault();
13211 e
.stopPropagation();
13218 * Bind key down listener.
13220 OO
.ui
.MenuSelectWidget
.prototype.bindKeyDownListener = function () {
13221 if ( this.$input
) {
13222 this.$input
.on( 'keydown', this.onKeyDownHandler
);
13224 // Capture menu navigation keys
13225 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
13230 * Unbind key down listener.
13232 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyDownListener = function () {
13233 if ( this.$input
) {
13234 this.$input
.off( 'keydown' );
13236 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
13243 * This will close the menu, unlike #selectItem which only changes selection.
13245 * @param {OO.ui.OptionWidget} item Item to choose
13248 OO
.ui
.MenuSelectWidget
.prototype.chooseItem = function ( item
) {
13249 OO
.ui
.MenuSelectWidget
.super.prototype.chooseItem
.call( this, item
);
13250 this.toggle( false );
13257 OO
.ui
.MenuSelectWidget
.prototype.addItems = function ( items
, index
) {
13261 OO
.ui
.MenuSelectWidget
.super.prototype.addItems
.call( this, items
, index
);
13264 if ( !this.newItems
) {
13265 this.newItems
= [];
13268 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
13270 if ( this.isVisible() ) {
13271 // Defer fitting label until item has been attached
13274 this.newItems
.push( item
);
13278 // Reevaluate clipping
13287 OO
.ui
.MenuSelectWidget
.prototype.removeItems = function ( items
) {
13289 OO
.ui
.MenuSelectWidget
.super.prototype.removeItems
.call( this, items
);
13291 // Reevaluate clipping
13300 OO
.ui
.MenuSelectWidget
.prototype.clearItems = function () {
13302 OO
.ui
.MenuSelectWidget
.super.prototype.clearItems
.call( this );
13304 // Reevaluate clipping
13313 OO
.ui
.MenuSelectWidget
.prototype.toggle = function ( visible
) {
13314 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
13317 change
= visible
!== this.isVisible(),
13318 elementDoc
= this.getElementDocument(),
13319 widgetDoc
= this.$widget
? this.$widget
[ 0 ].ownerDocument
: null;
13322 OO
.ui
.MenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
13326 this.bindKeyDownListener();
13328 // Change focus to enable keyboard navigation
13329 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
13330 this.$previousFocus
= this.$( ':focus' );
13331 this.$input
[ 0 ].focus();
13333 if ( this.newItems
&& this.newItems
.length
) {
13334 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
13335 this.newItems
[ i
].fitLabel();
13337 this.newItems
= null;
13339 this.toggleClipping( true );
13342 if ( this.autoHide
) {
13343 elementDoc
.addEventListener(
13344 'mousedown', this.onDocumentMouseDownHandler
, true
13346 // Support $widget being in a different document
13347 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
13348 widgetDoc
.addEventListener(
13349 'mousedown', this.onDocumentMouseDownHandler
, true
13354 this.unbindKeyDownListener();
13355 if ( this.isolated
&& this.$previousFocus
) {
13356 this.$previousFocus
[ 0 ].focus();
13357 this.$previousFocus
= null;
13359 elementDoc
.removeEventListener(
13360 'mousedown', this.onDocumentMouseDownHandler
, true
13362 // Support $widget being in a different document
13363 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
13364 widgetDoc
.removeEventListener(
13365 'mousedown', this.onDocumentMouseDownHandler
, true
13368 this.toggleClipping( false );
13376 * Menu for a text input widget.
13378 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
13379 * is in a different frame, the menu's position is automatically calculated and maintained when the
13380 * menu is toggled or the window is resized.
13383 * @extends OO.ui.MenuSelectWidget
13386 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
13387 * @param {Object} [config] Configuration options
13388 * @cfg {jQuery} [$container=input.$element] Element to render menu under
13390 OO
.ui
.TextInputMenuSelectWidget
= function OoUiTextInputMenuSelectWidget( input
, config
) {
13391 // Configuration initialization
13392 config
= config
|| {};
13394 // Parent constructor
13395 OO
.ui
.TextInputMenuSelectWidget
.super.call( this, config
);
13398 this.input
= input
;
13399 this.$container
= config
.$container
|| this.input
.$element
;
13400 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
13403 this.$element
.addClass( 'oo-ui-textInputMenuSelectWidget' );
13408 OO
.inheritClass( OO
.ui
.TextInputMenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
13413 * Handle window resize event.
13415 * @param {jQuery.Event} e Window resize event
13417 OO
.ui
.TextInputMenuSelectWidget
.prototype.onWindowResize = function () {
13424 OO
.ui
.TextInputMenuSelectWidget
.prototype.toggle = function ( visible
) {
13425 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
13427 var change
= visible
!== this.isVisible();
13429 if ( change
&& visible
) {
13430 // Make sure the width is set before the parent method runs.
13431 // After this we have to call this.position(); again to actually
13432 // position ourselves correctly.
13437 OO
.ui
.TextInputMenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
13440 if ( this.isVisible() ) {
13442 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
13444 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
13452 * Position the menu.
13456 OO
.ui
.TextInputMenuSelectWidget
.prototype.position = function () {
13457 var $container
= this.$container
,
13458 pos
= OO
.ui
.Element
.static.getRelativePosition( $container
, this.$element
.offsetParent() );
13460 // Position under input
13461 pos
.top
+= $container
.height();
13462 this.$element
.css( pos
);
13465 this.setIdealSize( $container
.width() );
13466 // We updated the position, so re-evaluate the clipping state
13473 * Structured list of items.
13475 * Use with OO.ui.OutlineOptionWidget.
13478 * @extends OO.ui.SelectWidget
13481 * @param {Object} [config] Configuration options
13483 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
13484 // Configuration initialization
13485 config
= config
|| {};
13487 // Parent constructor
13488 OO
.ui
.OutlineSelectWidget
.super.call( this, config
);
13491 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
13496 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
13499 * Switch that slides on and off.
13502 * @extends OO.ui.Widget
13503 * @mixins OO.ui.ToggleWidget
13506 * @param {Object} [config] Configuration options
13507 * @cfg {boolean} [value=false] Initial value
13509 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
13510 // Parent constructor
13511 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
13513 // Mixin constructors
13514 OO
.ui
.ToggleWidget
.call( this, config
);
13517 this.dragging
= false;
13518 this.dragStart
= null;
13519 this.sliding
= false;
13520 this.$glow
= this.$( '<span>' );
13521 this.$grip
= this.$( '<span>' );
13524 this.$element
.on( 'click', this.onClick
.bind( this ) );
13527 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
13528 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
13530 .addClass( 'oo-ui-toggleSwitchWidget' )
13531 .append( this.$glow
, this.$grip
);
13536 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
13537 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
13542 * Handle mouse down events.
13544 * @param {jQuery.Event} e Mouse down event
13546 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
13547 if ( !this.isDisabled() && e
.which
=== 1 ) {
13548 this.setValue( !this.value
);