3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2015 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2015-01-31T01:15:57Z
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
.toggle( show
);
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
1932 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1934 this.$element
.toggle( show
).css( 'visibility', '' );
1936 this.emit( 'toggle', show
);
1943 * Set the window manager.
1945 * This must be called before initialize. Calling it more than once will cause an error.
1947 * @param {OO.ui.WindowManager} manager Manager for this window
1948 * @throws {Error} If called more than once
1951 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1952 if ( this.manager
) {
1953 throw new Error( 'Cannot set window manager, window already has a manager' );
1957 this.manager
= manager
;
1958 this.isolated
= manager
.shouldIsolate();
1961 if ( this.isolated
) {
1962 this.$iframe
= this.$( '<iframe>' );
1963 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1964 this.$frame
.append( this.$iframe
);
1965 this.$ = function () {
1966 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1968 // WARNING: Do not use this.$ again until #initialize is called
1970 this.$content
= this.$( '<div>' );
1971 this.$document
= $( this.getElementDocument() );
1972 this.$content
.addClass( 'oo-ui-window-content' ).attr( 'tabIndex', 0 );
1973 this.$frame
.append( this.$content
);
1975 this.toggle( false );
1977 // Figure out directionality:
1978 this.dir
= OO
.ui
.Element
.static.getDir( this.$iframe
|| this.$content
) || 'ltr';
1984 * Set the window size.
1986 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1989 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1996 * Update the window size.
2000 OO
.ui
.Window
.prototype.updateSize = function () {
2001 this.manager
.updateWindowSize( this );
2006 * Set window dimensions.
2008 * Properties are applied to the frame container.
2010 * @param {Object} dim CSS dimension properties
2011 * @param {string|number} [dim.width] Width
2012 * @param {string|number} [dim.minWidth] Minimum width
2013 * @param {string|number} [dim.maxWidth] Maximum width
2014 * @param {string|number} [dim.width] Height, omit to set based on height of contents
2015 * @param {string|number} [dim.minWidth] Minimum height
2016 * @param {string|number} [dim.maxWidth] Maximum height
2019 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
2022 styleObj
= this.$frame
[ 0 ].style
;
2024 // Calculate the height we need to set using the correct width
2025 if ( dim
.height
=== undefined ) {
2026 this.withoutSizeTransitions( function () {
2027 var oldWidth
= styleObj
.width
;
2028 win
.$frame
.css( 'width', dim
.width
|| '' );
2029 height
= win
.getContentHeight();
2030 styleObj
.width
= oldWidth
;
2033 height
= dim
.height
;
2037 width
: dim
.width
|| '',
2038 minWidth
: dim
.minWidth
|| '',
2039 maxWidth
: dim
.maxWidth
|| '',
2040 height
: height
|| '',
2041 minHeight
: dim
.minHeight
|| '',
2042 maxHeight
: dim
.maxHeight
|| ''
2049 * Initialize window contents.
2051 * The first time the window is opened, #initialize is called when it's safe to begin populating
2052 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
2054 * Once this method is called, this.$ can be used to create elements within the frame.
2056 * @throws {Error} If not attached to a manager
2059 OO
.ui
.Window
.prototype.initialize = function () {
2060 if ( !this.manager
) {
2061 throw new Error( 'Cannot initialize window, must be attached to a manager' );
2065 this.$head
= this.$( '<div>' );
2066 this.$body
= this.$( '<div>' );
2067 this.$foot
= this.$( '<div>' );
2068 this.$innerOverlay
= this.$( '<div>' );
2071 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
2074 this.$head
.addClass( 'oo-ui-window-head' );
2075 this.$body
.addClass( 'oo-ui-window-body' );
2076 this.$foot
.addClass( 'oo-ui-window-foot' );
2077 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
2078 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
2086 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
2087 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
2089 * @param {Object} [data] Window opening data
2090 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
2091 * first argument will be a promise which will be resolved when the window begins closing
2093 OO
.ui
.Window
.prototype.open = function ( data
) {
2094 return this.manager
.openWindow( this, data
);
2100 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
2101 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
2103 * @param {Object} [data] Window closing data
2104 * @return {jQuery.Promise} Promise resolved when window is closed
2106 OO
.ui
.Window
.prototype.close = function ( data
) {
2107 return this.manager
.closeWindow( this, data
);
2113 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2116 * @param {Object} [data] Window opening data
2117 * @return {jQuery.Promise} Promise resolved when window is setup
2119 OO
.ui
.Window
.prototype.setup = function ( data
) {
2121 deferred
= $.Deferred();
2123 this.$element
.show();
2124 this.visible
= true;
2125 this.getSetupProcess( data
).execute().done( function () {
2126 // Force redraw by asking the browser to measure the elements' widths
2127 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2128 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2132 return deferred
.promise();
2138 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2141 * @param {Object} [data] Window opening data
2142 * @return {jQuery.Promise} Promise resolved when window is ready
2144 OO
.ui
.Window
.prototype.ready = function ( data
) {
2146 deferred
= $.Deferred();
2148 this.$content
.focus();
2149 this.getReadyProcess( data
).execute().done( function () {
2150 // Force redraw by asking the browser to measure the elements' widths
2151 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2152 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2156 return deferred
.promise();
2162 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2165 * @param {Object} [data] Window closing data
2166 * @return {jQuery.Promise} Promise resolved when window is held
2168 OO
.ui
.Window
.prototype.hold = function ( data
) {
2170 deferred
= $.Deferred();
2172 this.getHoldProcess( data
).execute().done( function () {
2173 // Get the focused element within the window's content
2174 var $focus
= win
.$content
.find( OO
.ui
.Element
.static.getDocument( win
.$content
).activeElement
);
2176 // Blur the focused element
2177 if ( $focus
.length
) {
2181 // Force redraw by asking the browser to measure the elements' widths
2182 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2183 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2187 return deferred
.promise();
2193 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2196 * @param {Object} [data] Window closing data
2197 * @return {jQuery.Promise} Promise resolved when window is torn down
2199 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2201 deferred
= $.Deferred();
2203 this.getTeardownProcess( data
).execute().done( function () {
2204 // Force redraw by asking the browser to measure the elements' widths
2205 win
.$element
.removeClass( 'oo-ui-window-load oo-ui-window-setup' ).width();
2206 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2207 win
.$element
.hide();
2208 win
.visible
= false;
2212 return deferred
.promise();
2216 * Load the frame contents.
2218 * Once the iframe's stylesheets are loaded the returned promise will be resolved. Calling while
2219 * loading will return a promise but not trigger a new loading cycle. Calling after loading is
2220 * complete will return a promise that's already been resolved.
2222 * Sounds simple right? Read on...
2224 * When you create a dynamic iframe using open/write/close, the window.load event for the
2225 * iframe is triggered when you call close, and there's no further load event to indicate that
2226 * everything is actually loaded.
2228 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2229 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2230 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2231 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2232 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2234 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2235 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2236 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2237 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2238 * accessing .cssRules is allowed.
2240 * However, now that we control the styles we're injecting, we might as well do away with
2241 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2242 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2243 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2244 * font-family rule is not applied until after the `@import` finishes.
2246 * All this stylesheet injection and polling magic is in #transplantStyles.
2248 * @return {jQuery.Promise} Promise resolved when loading is complete
2250 OO
.ui
.Window
.prototype.load = function () {
2251 var sub
, doc
, loading
,
2254 this.$element
.addClass( 'oo-ui-window-load' );
2256 // Non-isolated windows are already "loaded"
2257 if ( !this.loading
&& !this.isolated
) {
2258 this.loading
= $.Deferred().resolve();
2260 // Set initialized state after so sub-classes aren't confused by it being set by calling
2261 // their parent initialize method
2262 this.initialized
= true;
2265 // Return existing promise if already loading or loaded
2266 if ( this.loading
) {
2267 return this.loading
.promise();
2271 loading
= this.loading
= $.Deferred();
2272 sub
= this.$iframe
.prop( 'contentWindow' );
2275 // Initialize contents
2280 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2281 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2282 '<div class="oo-ui-window-content"></div>' +
2289 this.$ = OO
.ui
.Element
.static.getJQuery( doc
, this.$iframe
);
2290 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2291 this.$document
= this.$( doc
);
2294 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[ 0 ] )
2295 .always( function () {
2296 // Initialize isolated windows
2298 // Set initialized state after so sub-classes aren't confused by it being set by calling
2299 // their parent initialize method
2300 win
.initialized
= true;
2301 // Undo the visibility: hidden; hack and apply display: none;
2302 // We can do this safely now that the iframe has initialized
2303 // (don't do this from within #initialize because it has to happen
2304 // after the all subclasses have been handled as well).
2305 win
.toggle( win
.isVisible() );
2310 return loading
.promise();
2314 * Base class for all dialogs.
2317 * - Manage the window (open and close, etc.).
2318 * - Store the internal name and display title.
2319 * - A stack to track one or more pending actions.
2320 * - Manage a set of actions that can be performed.
2321 * - Configure and create action widgets.
2324 * - Close the dialog with Escape key.
2325 * - Visually lock the dialog while an action is in
2326 * progress (aka "pending").
2328 * Subclass responsibilities:
2329 * - Display the title somewhere.
2330 * - Add content to the dialog.
2331 * - Provide a UI to close the dialog.
2332 * - Display the action widgets somewhere.
2336 * @extends OO.ui.Window
2337 * @mixins OO.ui.PendingElement
2340 * @param {Object} [config] Configuration options
2342 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2343 // Parent constructor
2344 OO
.ui
.Dialog
.super.call( this, config
);
2346 // Mixin constructors
2347 OO
.ui
.PendingElement
.call( this );
2350 this.actions
= new OO
.ui
.ActionSet();
2351 this.attachedActions
= [];
2352 this.currentAction
= null;
2353 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2356 this.actions
.connect( this, {
2357 click
: 'onActionClick',
2358 resize
: 'onActionResize',
2359 change
: 'onActionsChange'
2364 .addClass( 'oo-ui-dialog' )
2365 .attr( 'role', 'dialog' );
2370 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2371 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2373 /* Static Properties */
2376 * Symbolic name of dialog.
2381 * @property {string}
2383 OO
.ui
.Dialog
.static.name
= '';
2391 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2393 OO
.ui
.Dialog
.static.title
= '';
2396 * List of OO.ui.ActionWidget configuration options.
2400 * @property {Object[]}
2402 OO
.ui
.Dialog
.static.actions
= [];
2405 * Close dialog when the escape key is pressed.
2410 * @property {boolean}
2412 OO
.ui
.Dialog
.static.escapable
= true;
2417 * Handle frame document key down events.
2419 * @param {jQuery.Event} e Key down event
2421 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2422 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2425 e
.stopPropagation();
2430 * Handle action resized events.
2432 * @param {OO.ui.ActionWidget} action Action that was resized
2434 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2435 // Override in subclass
2439 * Handle action click events.
2441 * @param {OO.ui.ActionWidget} action Action that was clicked
2443 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2444 if ( !this.isPending() ) {
2445 this.currentAction
= action
;
2446 this.executeAction( action
.getAction() );
2451 * Handle actions change event.
2453 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2454 this.detachActions();
2455 if ( !this.isClosing() ) {
2456 this.attachActions();
2461 * Get set of actions.
2463 * @return {OO.ui.ActionSet}
2465 OO
.ui
.Dialog
.prototype.getActions = function () {
2466 return this.actions
;
2470 * Get a process for taking action.
2472 * When you override this method, you can add additional accept steps to the process the parent
2473 * method provides using the 'first' and 'next' methods.
2476 * @param {string} [action] Symbolic name of action
2477 * @return {OO.ui.Process} Action process
2479 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2480 return new OO
.ui
.Process()
2481 .next( function () {
2483 // An empty action always closes the dialog without data, which should always be
2484 // safe and make no changes
2493 * @param {Object} [data] Dialog opening data
2494 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2495 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2496 * action item, omit to use #static-actions
2498 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2502 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2503 .next( function () {
2506 config
= this.constructor.static,
2507 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2509 this.title
.setLabel(
2510 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2512 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2514 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[ i
] ) )
2517 this.actions
.add( items
);
2519 if ( this.constructor.static.escapable
) {
2520 this.$document
.on( 'keydown', this.onDocumentKeyDownHandler
);
2528 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2530 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2531 .first( function () {
2532 if ( this.constructor.static.escapable
) {
2533 this.$document
.off( 'keydown', this.onDocumentKeyDownHandler
);
2536 this.actions
.clear();
2537 this.currentAction
= null;
2544 OO
.ui
.Dialog
.prototype.initialize = function () {
2546 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2549 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2552 this.$content
.addClass( 'oo-ui-dialog-content' );
2553 this.setPendingElement( this.$head
);
2557 * Attach action actions.
2559 OO
.ui
.Dialog
.prototype.attachActions = function () {
2560 // Remember the list of potentially attached actions
2561 this.attachedActions
= this.actions
.get();
2565 * Detach action actions.
2569 OO
.ui
.Dialog
.prototype.detachActions = function () {
2572 // Detach all actions that may have been previously attached
2573 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2574 this.attachedActions
[ i
].$element
.detach();
2576 this.attachedActions
= [];
2580 * Execute an action.
2582 * @param {string} action Symbolic name of action to execute
2583 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2585 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2587 return this.getActionProcess( action
).execute()
2588 .always( this.popPending
.bind( this ) );
2592 * Collection of windows.
2595 * @extends OO.ui.Element
2596 * @mixins OO.EventEmitter
2598 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2599 * already opening or opened, the current window will be closed without data. Empty closing data
2600 * should always result in the window being closed without causing constructive or destructive
2603 * As a window is opened and closed, it passes through several stages and the manager emits several
2604 * corresponding events.
2606 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2607 * - {@link #event-opening} is emitted with `opening` promise
2608 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2609 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2610 * - `setup` progress notification is emitted from opening promise
2611 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2612 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2613 * - `ready` progress notification is emitted from opening promise
2614 * - `opening` promise is resolved with `opened` promise
2615 * - Window is now open
2617 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2618 * - `opened` promise is resolved with `closing` promise
2619 * - {@link #event-closing} is emitted with `closing` promise
2620 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2621 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2622 * - `hold` progress notification is emitted from opening promise
2623 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2624 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2625 * - `teardown` progress notification is emitted from opening promise
2626 * - Closing promise is resolved
2627 * - Window is now closed
2630 * @param {Object} [config] Configuration options
2631 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2632 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2633 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2635 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2636 // Configuration initialization
2637 config
= config
|| {};
2639 // Parent constructor
2640 OO
.ui
.WindowManager
.super.call( this, config
);
2642 // Mixin constructors
2643 OO
.EventEmitter
.call( this );
2646 this.factory
= config
.factory
;
2647 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2648 this.isolate
= !!config
.isolate
;
2650 this.opening
= null;
2652 this.closing
= null;
2653 this.preparingToOpen
= null;
2654 this.preparingToClose
= null;
2656 this.currentWindow
= null;
2657 this.$ariaHidden
= null;
2658 this.requestedSize
= null;
2659 this.onWindowResizeTimeout
= null;
2660 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2661 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2662 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2663 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2667 .addClass( 'oo-ui-windowManager' )
2668 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2673 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2674 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2679 * Window is opening.
2681 * Fired when the window begins to be opened.
2684 * @param {OO.ui.Window} win Window that's being opened
2685 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2686 * resolved the first argument will be a promise which will be resolved when the window begins
2687 * closing, the second argument will be the opening data; progress notifications will be fired on
2688 * the promise for `setup` and `ready` when those processes are completed respectively.
2689 * @param {Object} data Window opening data
2693 * Window is closing.
2695 * Fired when the window begins to be closed.
2698 * @param {OO.ui.Window} win Window that's being closed
2699 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2700 * is resolved the first argument will be a the closing data; progress notifications will be fired
2701 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2702 * @param {Object} data Window closing data
2706 * Window was resized.
2709 * @param {OO.ui.Window} win Window that was resized
2712 /* Static Properties */
2715 * Map of symbolic size names and CSS properties.
2719 * @property {Object}
2721 OO
.ui
.WindowManager
.static.sizes
= {
2735 // These can be non-numeric because they are never used in calculations
2742 * Symbolic name of default size.
2744 * Default size is used if the window's requested size is not recognized.
2748 * @property {string}
2750 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2755 * Handle window resize events.
2757 * @param {jQuery.Event} e Window resize event
2759 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2760 clearTimeout( this.onWindowResizeTimeout
);
2761 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2765 * Handle window resize events.
2767 * @param {jQuery.Event} e Window resize event
2769 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2770 if ( this.currentWindow
) {
2771 this.updateWindowSize( this.currentWindow
);
2776 * Handle window mouse wheel events.
2778 * @param {jQuery.Event} e Mouse wheel event
2780 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2781 // Kill all events in the parent window if the child window is isolated
2782 return !this.shouldIsolate();
2786 * Handle document key down events.
2788 * @param {jQuery.Event} e Key down event
2790 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2791 switch ( e
.which
) {
2792 case OO
.ui
.Keys
.PAGEUP
:
2793 case OO
.ui
.Keys
.PAGEDOWN
:
2794 case OO
.ui
.Keys
.END
:
2795 case OO
.ui
.Keys
.HOME
:
2796 case OO
.ui
.Keys
.LEFT
:
2798 case OO
.ui
.Keys
.RIGHT
:
2799 case OO
.ui
.Keys
.DOWN
:
2800 // Kill all events in the parent window if the child window is isolated
2801 return !this.shouldIsolate();
2806 * Check if window is opening.
2808 * @return {boolean} Window is opening
2810 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2811 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2815 * Check if window is closing.
2817 * @return {boolean} Window is closing
2819 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2820 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2824 * Check if window is opened.
2826 * @return {boolean} Window is opened
2828 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2829 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2833 * Check if window contents should be isolated.
2835 * Window content isolation is done using inline frames.
2837 * @return {boolean} Window contents should be isolated
2839 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2840 return this.isolate
;
2844 * Check if a window is being managed.
2846 * @param {OO.ui.Window} win Window to check
2847 * @return {boolean} Window is being managed
2849 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2852 for ( name
in this.windows
) {
2853 if ( this.windows
[ name
] === win
) {
2862 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2864 * @param {OO.ui.Window} win Window being opened
2865 * @param {Object} [data] Window opening data
2866 * @return {number} Milliseconds to wait
2868 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2873 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2875 * @param {OO.ui.Window} win Window being opened
2876 * @param {Object} [data] Window opening data
2877 * @return {number} Milliseconds to wait
2879 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2884 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2886 * @param {OO.ui.Window} win Window being closed
2887 * @param {Object} [data] Window closing data
2888 * @return {number} Milliseconds to wait
2890 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2895 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2897 * @param {OO.ui.Window} win Window being closed
2898 * @param {Object} [data] Window closing data
2899 * @return {number} Milliseconds to wait
2901 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2902 return this.modal
? 250 : 0;
2906 * Get managed window by symbolic name.
2908 * If window is not yet instantiated, it will be instantiated and added automatically.
2910 * @param {string} name Symbolic window name
2911 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2912 * @throws {Error} If the symbolic name is unrecognized by the factory
2913 * @throws {Error} If the symbolic name unrecognized as a managed window
2915 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2916 var deferred
= $.Deferred(),
2917 win
= this.windows
[ name
];
2919 if ( !( win
instanceof OO
.ui
.Window
) ) {
2920 if ( this.factory
) {
2921 if ( !this.factory
.lookup( name
) ) {
2922 deferred
.reject( new OO
.ui
.Error(
2923 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2926 win
= this.factory
.create( name
, this, { $: this.$ } );
2927 this.addWindows( [ win
] );
2928 deferred
.resolve( win
);
2931 deferred
.reject( new OO
.ui
.Error(
2932 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2936 deferred
.resolve( win
);
2939 return deferred
.promise();
2943 * Get current window.
2945 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2947 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2948 return this.currentWindow
;
2954 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2955 * @param {Object} [data] Window opening data
2956 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2957 * for more details about the `opening` promise
2960 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2963 opening
= $.Deferred();
2965 // Argument handling
2966 if ( typeof win
=== 'string' ) {
2967 return this.getWindow( win
).then( function ( win
) {
2968 return manager
.openWindow( win
, data
);
2973 if ( !this.hasWindow( win
) ) {
2974 opening
.reject( new OO
.ui
.Error(
2975 'Cannot open window: window is not attached to manager'
2977 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2978 opening
.reject( new OO
.ui
.Error(
2979 'Cannot open window: another window is opening or open'
2984 if ( opening
.state() !== 'rejected' ) {
2985 if ( !win
.getManager() ) {
2986 win
.setManager( this );
2988 preparing
.push( win
.load() );
2990 if ( this.closing
) {
2991 // If a window is currently closing, wait for it to complete
2992 preparing
.push( this.closing
);
2995 this.preparingToOpen
= $.when
.apply( $, preparing
);
2996 // Ensure handlers get called after preparingToOpen is set
2997 this.preparingToOpen
.done( function () {
2998 if ( manager
.modal
) {
2999 manager
.toggleGlobalEvents( true );
3000 manager
.toggleAriaIsolation( true );
3002 manager
.currentWindow
= win
;
3003 manager
.opening
= opening
;
3004 manager
.preparingToOpen
= null;
3005 manager
.emit( 'opening', win
, opening
, data
);
3006 setTimeout( function () {
3007 win
.setup( data
).then( function () {
3008 manager
.updateWindowSize( win
);
3009 manager
.opening
.notify( { state
: 'setup' } );
3010 setTimeout( function () {
3011 win
.ready( data
).then( function () {
3012 manager
.opening
.notify( { state
: 'ready' } );
3013 manager
.opening
= null;
3014 manager
.opened
= $.Deferred();
3015 opening
.resolve( manager
.opened
.promise(), data
);
3017 }, manager
.getReadyDelay() );
3019 }, manager
.getSetupDelay() );
3023 return opening
.promise();
3029 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
3030 * @param {Object} [data] Window closing data
3031 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
3032 * for more details about the `closing` promise
3033 * @throws {Error} If no window by that name is being managed
3036 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
3039 closing
= $.Deferred(),
3042 // Argument handling
3043 if ( typeof win
=== 'string' ) {
3044 win
= this.windows
[ win
];
3045 } else if ( !this.hasWindow( win
) ) {
3051 closing
.reject( new OO
.ui
.Error(
3052 'Cannot close window: window is not attached to manager'
3054 } else if ( win
!== this.currentWindow
) {
3055 closing
.reject( new OO
.ui
.Error(
3056 'Cannot close window: window already closed with different data'
3058 } else if ( this.preparingToClose
|| this.closing
) {
3059 closing
.reject( new OO
.ui
.Error(
3060 'Cannot close window: window already closing with different data'
3065 if ( closing
.state() !== 'rejected' ) {
3066 if ( this.opening
) {
3067 // If the window is currently opening, close it when it's done
3068 preparing
.push( this.opening
);
3071 this.preparingToClose
= $.when
.apply( $, preparing
);
3072 // Ensure handlers get called after preparingToClose is set
3073 this.preparingToClose
.done( function () {
3074 manager
.closing
= closing
;
3075 manager
.preparingToClose
= null;
3076 manager
.emit( 'closing', win
, closing
, data
);
3077 opened
= manager
.opened
;
3078 manager
.opened
= null;
3079 opened
.resolve( closing
.promise(), data
);
3080 setTimeout( function () {
3081 win
.hold( data
).then( function () {
3082 closing
.notify( { state
: 'hold' } );
3083 setTimeout( function () {
3084 win
.teardown( data
).then( function () {
3085 closing
.notify( { state
: 'teardown' } );
3086 if ( manager
.modal
) {
3087 manager
.toggleGlobalEvents( false );
3088 manager
.toggleAriaIsolation( false );
3090 manager
.closing
= null;
3091 manager
.currentWindow
= null;
3092 closing
.resolve( data
);
3094 }, manager
.getTeardownDelay() );
3096 }, manager
.getHoldDelay() );
3100 return closing
.promise();
3106 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
3107 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
3108 * a statically configured symbolic name
3110 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
3111 var i
, len
, win
, name
, list
;
3113 if ( $.isArray( windows
) ) {
3114 // Convert to map of windows by looking up symbolic names from static configuration
3116 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
3117 name
= windows
[ i
].constructor.static.name
;
3118 if ( typeof name
!== 'string' ) {
3119 throw new Error( 'Cannot add window' );
3121 list
[ name
] = windows
[ i
];
3123 } else if ( $.isPlainObject( windows
) ) {
3128 for ( name
in list
) {
3130 this.windows
[ name
] = win
;
3131 this.$element
.append( win
.$element
);
3138 * Windows will be closed before they are removed.
3140 * @param {string[]} names Symbolic names of windows to remove
3141 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3142 * @throws {Error} If windows being removed are not being managed
3144 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3145 var i
, len
, win
, name
, cleanupWindow
,
3148 cleanup = function ( name
, win
) {
3149 delete manager
.windows
[ name
];
3150 win
.$element
.detach();
3153 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3155 win
= this.windows
[ name
];
3157 throw new Error( 'Cannot remove window' );
3159 cleanupWindow
= cleanup
.bind( null, name
, win
);
3160 promises
.push( this.closeWindow( name
).then( cleanupWindow
, cleanupWindow
) );
3163 return $.when
.apply( $, promises
);
3167 * Remove all windows.
3169 * Windows will be closed before they are removed.
3171 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3173 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3174 return this.removeWindows( Object
.keys( this.windows
) );
3180 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3184 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3185 // Bypass for non-current, and thus invisible, windows
3186 if ( win
!== this.currentWindow
) {
3190 var viewport
= OO
.ui
.Element
.static.getDimensions( win
.getElementWindow() ),
3191 sizes
= this.constructor.static.sizes
,
3192 size
= win
.getSize();
3194 if ( !sizes
[ size
] ) {
3195 size
= this.constructor.static.defaultSize
;
3197 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[ size
].width
) {
3201 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3202 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3203 win
.setDimensions( sizes
[ size
] );
3205 this.emit( 'resize', win
);
3211 * Bind or unbind global events for scrolling.
3213 * @param {boolean} [on] Bind global events
3216 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3217 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3220 if ( !this.globalEvents
) {
3221 this.$( this.getElementDocument() ).on( {
3222 // Prevent scrolling by keys in top-level window
3223 keydown
: this.onDocumentKeyDownHandler
3225 this.$( this.getElementWindow() ).on( {
3226 // Prevent scrolling by wheel in top-level window
3227 mousewheel
: this.onWindowMouseWheelHandler
,
3228 // Start listening for top-level window dimension changes
3229 'orientationchange resize': this.onWindowResizeHandler
3231 // Disable window scrolling in isolated windows
3232 if ( !this.shouldIsolate() ) {
3233 $( this.getElementDocument().body
).css( 'overflow', 'hidden' );
3235 this.globalEvents
= true;
3237 } else if ( this.globalEvents
) {
3238 // Unbind global events
3239 this.$( this.getElementDocument() ).off( {
3240 // Allow scrolling by keys in top-level window
3241 keydown
: this.onDocumentKeyDownHandler
3243 this.$( this.getElementWindow() ).off( {
3244 // Allow scrolling by wheel in top-level window
3245 mousewheel
: this.onWindowMouseWheelHandler
,
3246 // Stop listening for top-level window dimension changes
3247 'orientationchange resize': this.onWindowResizeHandler
3249 if ( !this.shouldIsolate() ) {
3250 $( this.getElementDocument().body
).css( 'overflow', '' );
3252 this.globalEvents
= false;
3259 * Toggle screen reader visibility of content other than the window manager.
3261 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3264 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3265 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3268 if ( !this.$ariaHidden
) {
3269 // Hide everything other than the window manager from screen readers
3270 this.$ariaHidden
= $( 'body' )
3272 .not( this.$element
.parentsUntil( 'body' ).last() )
3273 .attr( 'aria-hidden', '' );
3275 } else if ( this.$ariaHidden
) {
3276 // Restore screen reader visibility
3277 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3278 this.$ariaHidden
= null;
3285 * Destroy window manager.
3287 OO
.ui
.WindowManager
.prototype.destroy = function () {
3288 this.toggleGlobalEvents( false );
3289 this.toggleAriaIsolation( false );
3290 this.clearWindows();
3291 this.$element
.remove();
3298 * @param {string|jQuery} message Description of error
3299 * @param {Object} [config] Configuration options
3300 * @cfg {boolean} [recoverable=true] Error is recoverable
3301 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3303 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3304 // Configuration initialization
3305 config
= config
|| {};
3308 this.message
= message
instanceof jQuery
? message
: String( message
);
3309 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3310 this.warning
= !!config
.warning
;
3315 OO
.initClass( OO
.ui
.Error
);
3320 * Check if error can be recovered from.
3322 * @return {boolean} Error is recoverable
3324 OO
.ui
.Error
.prototype.isRecoverable = function () {
3325 return this.recoverable
;
3329 * Check if the error is a warning
3331 * @return {boolean} Error is warning
3333 OO
.ui
.Error
.prototype.isWarning = function () {
3334 return this.warning
;
3338 * Get error message as DOM nodes.
3340 * @return {jQuery} Error message in DOM nodes
3342 OO
.ui
.Error
.prototype.getMessage = function () {
3343 return this.message
instanceof jQuery
?
3344 this.message
.clone() :
3345 $( '<div>' ).text( this.message
).contents();
3349 * Get error message as text.
3351 * @return {string} Error message
3353 OO
.ui
.Error
.prototype.getMessageText = function () {
3354 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3358 * A list of functions, called in sequence.
3360 * If a function added to a process returns boolean false the process will stop; if it returns an
3361 * object with a `promise` method the process will use the promise to either continue to the next
3362 * step when the promise is resolved or stop when the promise is rejected.
3367 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3368 * call, see #createStep for more information
3369 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3371 * @return {Object} Step object, with `callback` and `context` properties
3373 OO
.ui
.Process = function ( step
, context
) {
3378 if ( step
!== undefined ) {
3379 this.next( step
, context
);
3385 OO
.initClass( OO
.ui
.Process
);
3390 * Start the process.
3392 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3393 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3394 * process, the remaining steps will not be taken
3396 OO
.ui
.Process
.prototype.execute = function () {
3397 var i
, len
, promise
;
3400 * Continue execution.
3403 * @param {Array} step A function and the context it should be called in
3404 * @return {Function} Function that continues the process
3406 function proceed( step
) {
3407 return function () {
3408 // Execute step in the correct context
3410 result
= step
.callback
.call( step
.context
);
3412 if ( result
=== false ) {
3413 // Use rejected promise for boolean false results
3414 return $.Deferred().reject( [] ).promise();
3416 if ( typeof result
=== 'number' ) {
3418 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3420 // Use a delayed promise for numbers, expecting them to be in milliseconds
3421 deferred
= $.Deferred();
3422 setTimeout( deferred
.resolve
, result
);
3423 return deferred
.promise();
3425 if ( result
instanceof OO
.ui
.Error
) {
3426 // Use rejected promise for error
3427 return $.Deferred().reject( [ result
] ).promise();
3429 if ( $.isArray( result
) && result
.length
&& result
[ 0 ] instanceof OO
.ui
.Error
) {
3430 // Use rejected promise for list of errors
3431 return $.Deferred().reject( result
).promise();
3433 // Duck-type the object to see if it can produce a promise
3434 if ( result
&& $.isFunction( result
.promise
) ) {
3435 // Use a promise generated from the result
3436 return result
.promise();
3438 // Use resolved promise for other results
3439 return $.Deferred().resolve().promise();
3443 if ( this.steps
.length
) {
3444 // Generate a chain reaction of promises
3445 promise
= proceed( this.steps
[ 0 ] )();
3446 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3447 promise
= promise
.then( proceed( this.steps
[ i
] ) );
3450 promise
= $.Deferred().resolve().promise();
3457 * Create a process step.
3460 * @param {number|jQuery.Promise|Function} step
3462 * - Number of milliseconds to wait; or
3463 * - Promise to wait to be resolved; or
3464 * - Function to execute
3465 * - If it returns boolean false the process will stop
3466 * - If it returns an object with a `promise` method the process will use the promise to either
3467 * continue to the next step when the promise is resolved or stop when the promise is rejected
3468 * - If it returns a number, the process will wait for that number of milliseconds before
3470 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3472 * @return {Object} Step object, with `callback` and `context` properties
3474 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3475 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3477 callback: function () {
3483 if ( $.isFunction( step
) ) {
3489 throw new Error( 'Cannot create process step: number, promise or function expected' );
3493 * Add step to the beginning of the process.
3495 * @inheritdoc #createStep
3496 * @return {OO.ui.Process} this
3499 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3500 this.steps
.unshift( this.createStep( step
, context
) );
3505 * Add step to the end of the process.
3507 * @inheritdoc #createStep
3508 * @return {OO.ui.Process} this
3511 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3512 this.steps
.push( this.createStep( step
, context
) );
3517 * Factory for tools.
3520 * @extends OO.Factory
3523 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3524 // Parent constructor
3525 OO
.ui
.ToolFactory
.super.call( this );
3530 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3535 * Get tools from the factory
3537 * @param {Array} include Included tools
3538 * @param {Array} exclude Excluded tools
3539 * @param {Array} promote Promoted tools
3540 * @param {Array} demote Demoted tools
3541 * @return {string[]} List of tools
3543 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3544 var i
, len
, included
, promoted
, demoted
,
3548 // Collect included and not excluded tools
3549 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3552 promoted
= this.extract( promote
, used
);
3553 demoted
= this.extract( demote
, used
);
3556 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3557 if ( !used
[ included
[ i
] ] ) {
3558 auto
.push( included
[ i
] );
3562 return promoted
.concat( auto
).concat( demoted
);
3566 * Get a flat list of names from a list of names or groups.
3568 * Tools can be specified in the following ways:
3570 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3571 * - All tools in a group: `{ group: 'group-name' }`
3572 * - All tools: `'*'`
3575 * @param {Array|string} collection List of tools
3576 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3577 * names will be added as properties
3578 * @return {string[]} List of extracted names
3580 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3581 var i
, len
, item
, name
, tool
,
3584 if ( collection
=== '*' ) {
3585 for ( name
in this.registry
) {
3586 tool
= this.registry
[ name
];
3588 // Only add tools by group name when auto-add is enabled
3589 tool
.static.autoAddToCatchall
&&
3590 // Exclude already used tools
3591 ( !used
|| !used
[ name
] )
3595 used
[ name
] = true;
3599 } else if ( $.isArray( collection
) ) {
3600 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3601 item
= collection
[ i
];
3602 // Allow plain strings as shorthand for named tools
3603 if ( typeof item
=== 'string' ) {
3604 item
= { name
: item
};
3606 if ( OO
.isPlainObject( item
) ) {
3608 for ( name
in this.registry
) {
3609 tool
= this.registry
[ name
];
3611 // Include tools with matching group
3612 tool
.static.group
=== item
.group
&&
3613 // Only add tools by group name when auto-add is enabled
3614 tool
.static.autoAddToGroup
&&
3615 // Exclude already used tools
3616 ( !used
|| !used
[ name
] )
3620 used
[ name
] = true;
3624 // Include tools with matching name and exclude already used tools
3625 } else if ( item
.name
&& ( !used
|| !used
[ item
.name
] ) ) {
3626 names
.push( item
.name
);
3628 used
[ item
.name
] = true;
3638 * Factory for tool groups.
3641 * @extends OO.Factory
3644 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3645 // Parent constructor
3646 OO
.Factory
.call( this );
3649 defaultClasses
= this.constructor.static.getDefaultClasses();
3651 // Register default toolgroups
3652 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3653 this.register( defaultClasses
[ i
] );
3659 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3661 /* Static Methods */
3664 * Get a default set of classes to be registered on construction
3666 * @return {Function[]} Default classes
3668 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3671 OO
.ui
.ListToolGroup
,
3683 * @param {Object} [config] Configuration options
3685 OO
.ui
.Theme
= function OoUiTheme( config
) {
3686 // Configuration initialization
3687 config
= config
|| {};
3692 OO
.initClass( OO
.ui
.Theme
);
3697 * Get a list of classes to be applied to a widget.
3699 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3700 * otherwise state transitions will not work properly.
3702 * @param {OO.ui.Element} element Element for which to get classes
3703 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3705 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3706 return { on
: [], off
: [] };
3710 * Update CSS classes provided by the theme.
3712 * For elements with theme logic hooks, this should be called any time there's a state change.
3714 * @param {OO.ui.Element} element Element for which to update classes
3715 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3717 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3718 var classes
= this.getElementClasses( element
);
3721 .removeClass( classes
.off
.join( ' ' ) )
3722 .addClass( classes
.on
.join( ' ' ) );
3726 * Element supporting "sequential focus navigation" using the 'tabindex' attribute.
3732 * @param {Object} [config] Configuration options
3733 * @cfg {jQuery} [$tabIndexed] tabIndexed node, assigned to #$tabIndexed, omit to use #$element
3734 * @cfg {number|Function} [tabIndex=0] Tab index value. Use 0 to use default ordering, use -1 to
3735 * prevent tab focusing. (default: 0)
3737 OO
.ui
.TabIndexedElement
= function OoUiTabIndexedElement( config
) {
3738 // Configuration initialization
3739 config
= config
|| {};
3742 this.$tabIndexed
= null;
3743 this.tabIndex
= null;
3746 this.setTabIndex( config
.tabIndex
|| 0 );
3747 this.setTabIndexedElement( config
.$tabIndexed
|| this.$element
);
3752 OO
.initClass( OO
.ui
.TabIndexedElement
);
3757 * Set the element with 'tabindex' attribute.
3759 * If an element is already set, it will be cleaned up before setting up the new element.
3761 * @param {jQuery} $tabIndexed Element to set tab index on
3763 OO
.ui
.TabIndexedElement
.prototype.setTabIndexedElement = function ( $tabIndexed
) {
3764 if ( this.$tabIndexed
) {
3765 this.$tabIndexed
.removeAttr( 'tabindex' );
3768 this.$tabIndexed
= $tabIndexed
;
3769 if ( this.tabIndex
!== null ) {
3770 this.$tabIndexed
.attr( 'tabindex', this.tabIndex
);
3775 * Set tab index value.
3777 * @param {number|null} tabIndex Tab index value or null for no tabIndex
3780 OO
.ui
.TabIndexedElement
.prototype.setTabIndex = function ( tabIndex
) {
3781 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3783 if ( this.tabIndex
!== tabIndex
) {
3784 if ( this.$tabIndexed
) {
3785 if ( tabIndex
!== null ) {
3786 this.$tabIndexed
.attr( 'tabindex', tabIndex
);
3788 this.$tabIndexed
.removeAttr( 'tabindex' );
3791 this.tabIndex
= tabIndex
;
3798 * Get tab index value.
3800 * @return {number} Tab index value
3802 OO
.ui
.TabIndexedElement
.prototype.getTabIndex = function () {
3803 return this.tabIndex
;
3807 * Element with a button.
3809 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3810 * and access keys for accessibility purposes.
3816 * @param {Object} [config] Configuration options
3817 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3818 * @cfg {boolean} [framed=true] Render button with a frame
3819 * @cfg {string} [accessKey] Button's access key
3821 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3822 // Configuration initialization
3823 config
= config
|| {};
3826 this.$button
= config
.$button
|| this.$( '<a>' );
3828 this.accessKey
= null;
3829 this.active
= false;
3830 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3831 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3834 this.$element
.addClass( 'oo-ui-buttonElement' );
3835 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3836 this.setAccessKey( config
.accessKey
);
3837 this.setButtonElement( this.$button
);
3842 OO
.initClass( OO
.ui
.ButtonElement
);
3844 /* Static Properties */
3847 * Cancel mouse down events.
3851 * @property {boolean}
3853 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3858 * Set the button element.
3860 * If an element is already set, it will be cleaned up before setting up the new element.
3862 * @param {jQuery} $button Element to use as button
3864 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3865 if ( this.$button
) {
3867 .removeClass( 'oo-ui-buttonElement-button' )
3868 .removeAttr( 'role accesskey' )
3869 .off( 'mousedown', this.onMouseDownHandler
);
3872 this.$button
= $button
3873 .addClass( 'oo-ui-buttonElement-button' )
3874 .attr( { role
: 'button', accesskey
: this.accessKey
} )
3875 .on( 'mousedown', this.onMouseDownHandler
);
3879 * Handles mouse down events.
3881 * @param {jQuery.Event} e Mouse down event
3883 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3884 if ( this.isDisabled() || e
.which
!== 1 ) {
3887 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3888 // Prevent change of focus unless specifically configured otherwise
3889 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3895 * Handles mouse up events.
3897 * @param {jQuery.Event} e Mouse up event
3899 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3900 if ( this.isDisabled() || e
.which
!== 1 ) {
3903 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3907 * Check if button has a frame.
3909 * @return {boolean} Button is framed
3911 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3918 * @param {boolean} [framed] Make button framed, omit to toggle
3921 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3922 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3923 if ( framed
!== this.framed
) {
3924 this.framed
= framed
;
3926 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3927 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3928 this.updateThemeClasses();
3937 * @param {string} accessKey Button's access key, use empty string to remove
3940 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3941 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3943 if ( this.accessKey
!== accessKey
) {
3944 if ( this.$button
) {
3945 if ( accessKey
!== null ) {
3946 this.$button
.attr( 'accesskey', accessKey
);
3948 this.$button
.removeAttr( 'accesskey' );
3951 this.accessKey
= accessKey
;
3960 * @param {boolean} [value] Make button active
3963 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3964 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3969 * Element containing a sequence of child elements.
3975 * @param {Object} [config] Configuration options
3976 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3978 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3979 // Configuration initialization
3980 config
= config
|| {};
3985 this.aggregateItemEvents
= {};
3988 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3994 * Set the group element.
3996 * If an element is already set, items will be moved to the new element.
3998 * @param {jQuery} $group Element to use as group
4000 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
4003 this.$group
= $group
;
4004 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4005 this.$group
.append( this.items
[ i
].$element
);
4010 * Check if there are no items.
4012 * @return {boolean} Group is empty
4014 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
4015 return !this.items
.length
;
4021 * @return {OO.ui.Element[]} Items
4023 OO
.ui
.GroupElement
.prototype.getItems = function () {
4024 return this.items
.slice( 0 );
4028 * Get an item by its data.
4030 * Data is compared by a hash of its value. Only the first item with matching data will be returned.
4032 * @param {Object} data Item data to search for
4033 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
4035 OO
.ui
.GroupElement
.prototype.getItemFromData = function ( data
) {
4037 hash
= OO
.getHash( data
);
4039 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4040 item
= this.items
[ i
];
4041 if ( hash
=== OO
.getHash( item
.getData() ) ) {
4050 * Get items by their data.
4052 * Data is compared by a hash of its value. All items with matching data will be returned.
4054 * @param {Object} data Item data to search for
4055 * @return {OO.ui.Element[]} Items with equivalent data
4057 OO
.ui
.GroupElement
.prototype.getItemsFromData = function ( data
) {
4059 hash
= OO
.getHash( data
),
4062 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4063 item
= this.items
[ i
];
4064 if ( hash
=== OO
.getHash( item
.getData() ) ) {
4073 * Add an aggregate item event.
4075 * Aggregated events are listened to on each item and then emitted by the group under a new name,
4076 * and with an additional leading parameter containing the item that emitted the original event.
4077 * Other arguments that were emitted from the original event are passed through.
4079 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
4080 * event, use null value to remove aggregation
4081 * @throws {Error} If aggregation already exists
4083 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
4084 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
4086 for ( itemEvent
in events
) {
4087 groupEvent
= events
[ itemEvent
];
4089 // Remove existing aggregated event
4090 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4091 // Don't allow duplicate aggregations
4093 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
4095 // Remove event aggregation from existing items
4096 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4097 item
= this.items
[ i
];
4098 if ( item
.connect
&& item
.disconnect
) {
4100 remove
[ itemEvent
] = [ 'emit', groupEvent
, item
];
4101 item
.disconnect( this, remove
);
4104 // Prevent future items from aggregating event
4105 delete this.aggregateItemEvents
[ itemEvent
];
4108 // Add new aggregate event
4110 // Make future items aggregate event
4111 this.aggregateItemEvents
[ itemEvent
] = groupEvent
;
4112 // Add event aggregation to existing items
4113 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4114 item
= this.items
[ i
];
4115 if ( item
.connect
&& item
.disconnect
) {
4117 add
[ itemEvent
] = [ 'emit', groupEvent
, item
];
4118 item
.connect( this, add
);
4128 * Adding an existing item will move it.
4130 * @param {OO.ui.Element[]} items Items
4131 * @param {number} [index] Index to insert items at
4134 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
4135 var i
, len
, item
, event
, events
, currentIndex
,
4138 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4141 // Check if item exists then remove it first, effectively "moving" it
4142 currentIndex
= $.inArray( item
, this.items
);
4143 if ( currentIndex
>= 0 ) {
4144 this.removeItems( [ item
] );
4145 // Adjust index to compensate for removal
4146 if ( currentIndex
< index
) {
4151 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
4153 for ( event
in this.aggregateItemEvents
) {
4154 events
[ event
] = [ 'emit', this.aggregateItemEvents
[ event
], item
];
4156 item
.connect( this, events
);
4158 item
.setElementGroup( this );
4159 itemElements
.push( item
.$element
.get( 0 ) );
4162 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
4163 this.$group
.append( itemElements
);
4164 this.items
.push
.apply( this.items
, items
);
4165 } else if ( index
=== 0 ) {
4166 this.$group
.prepend( itemElements
);
4167 this.items
.unshift
.apply( this.items
, items
);
4169 this.items
[ index
].$element
.before( itemElements
);
4170 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
4179 * Items will be detached, not removed, so they can be used later.
4181 * @param {OO.ui.Element[]} items Items to remove
4184 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
4185 var i
, len
, item
, index
, remove
, itemEvent
;
4187 // Remove specific items
4188 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4190 index
= $.inArray( item
, this.items
);
4191 if ( index
!== -1 ) {
4193 item
.connect
&& item
.disconnect
&&
4194 !$.isEmptyObject( this.aggregateItemEvents
)
4197 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4198 remove
[ itemEvent
] = [ 'emit', this.aggregateItemEvents
[ itemEvent
], item
];
4200 item
.disconnect( this, remove
);
4202 item
.setElementGroup( null );
4203 this.items
.splice( index
, 1 );
4204 item
.$element
.detach();
4214 * Items will be detached, not removed, so they can be used later.
4218 OO
.ui
.GroupElement
.prototype.clearItems = function () {
4219 var i
, len
, item
, remove
, itemEvent
;
4222 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4223 item
= this.items
[ i
];
4225 item
.connect
&& item
.disconnect
&&
4226 !$.isEmptyObject( this.aggregateItemEvents
)
4229 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4230 remove
[ itemEvent
] = [ 'emit', this.aggregateItemEvents
[ itemEvent
], item
];
4232 item
.disconnect( this, remove
);
4234 item
.setElementGroup( null );
4235 item
.$element
.detach();
4243 * A mixin for an element that can be dragged and dropped.
4244 * Use in conjunction with DragGroupWidget
4251 OO
.ui
.DraggableElement
= function OoUiDraggableElement() {
4255 // Initialize and events
4257 .attr( 'draggable', true )
4258 .addClass( 'oo-ui-draggableElement' )
4260 dragstart
: this.onDragStart
.bind( this ),
4261 dragover
: this.onDragOver
.bind( this ),
4262 dragend
: this.onDragEnd
.bind( this ),
4263 drop
: this.onDrop
.bind( this )
4271 * @param {OO.ui.DraggableElement} item Dragging item
4285 * Respond to dragstart event.
4286 * @param {jQuery.Event} event jQuery event
4289 OO
.ui
.DraggableElement
.prototype.onDragStart = function ( e
) {
4290 var dataTransfer
= e
.originalEvent
.dataTransfer
;
4291 // Define drop effect
4292 dataTransfer
.dropEffect
= 'none';
4293 dataTransfer
.effectAllowed
= 'move';
4294 // We must set up a dataTransfer data property or Firefox seems to
4295 // ignore the fact the element is draggable.
4297 dataTransfer
.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
4299 // The above is only for firefox. No need to set a catch clause
4300 // if it fails, move on.
4302 // Add dragging class
4303 this.$element
.addClass( 'oo-ui-draggableElement-dragging' );
4305 this.emit( 'dragstart', this );
4310 * Respond to dragend event.
4313 OO
.ui
.DraggableElement
.prototype.onDragEnd = function () {
4314 this.$element
.removeClass( 'oo-ui-draggableElement-dragging' );
4315 this.emit( 'dragend' );
4319 * Handle drop event.
4320 * @param {jQuery.Event} event jQuery event
4323 OO
.ui
.DraggableElement
.prototype.onDrop = function ( e
) {
4325 this.emit( 'drop', e
);
4329 * In order for drag/drop to work, the dragover event must
4330 * return false and stop propogation.
4332 OO
.ui
.DraggableElement
.prototype.onDragOver = function ( e
) {
4338 * Store it in the DOM so we can access from the widget drag event
4339 * @param {number} Item index
4341 OO
.ui
.DraggableElement
.prototype.setIndex = function ( index
) {
4342 if ( this.index
!== index
) {
4344 this.$element
.data( 'index', index
);
4350 * @return {number} Item index
4352 OO
.ui
.DraggableElement
.prototype.getIndex = function () {
4357 * Element containing a sequence of child elements that can be dragged
4364 * @param {Object} [config] Configuration options
4365 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
4366 * @cfg {string} [orientation] Item orientation, 'horizontal' or 'vertical'. Defaults to 'vertical'
4368 OO
.ui
.DraggableGroupElement
= function OoUiDraggableGroupElement( config
) {
4369 // Configuration initialization
4370 config
= config
|| {};
4372 // Parent constructor
4373 OO
.ui
.GroupElement
.call( this, config
);
4376 this.orientation
= config
.orientation
|| 'vertical';
4377 this.dragItem
= null;
4378 this.itemDragOver
= null;
4380 this.sideInsertion
= '';
4384 dragstart
: 'itemDragStart',
4385 dragend
: 'itemDragEnd',
4388 this.connect( this, {
4389 itemDragStart
: 'onItemDragStart',
4390 itemDrop
: 'onItemDrop',
4391 itemDragEnd
: 'onItemDragEnd'
4394 dragover
: $.proxy( this.onDragOver
, this ),
4395 dragleave
: $.proxy( this.onDragLeave
, this )
4399 if ( $.isArray( config
.items
) ) {
4400 this.addItems( config
.items
);
4402 this.$placeholder
= $( '<div>' )
4403 .addClass( 'oo-ui-draggableGroupElement-placeholder' );
4405 .addClass( 'oo-ui-draggableGroupElement' )
4406 .append( this.$status
)
4407 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' )
4408 .prepend( this.$placeholder
);
4412 OO
.mixinClass( OO
.ui
.DraggableGroupElement
, OO
.ui
.GroupElement
);
4418 * @param {OO.ui.DraggableElement} item Reordered item
4419 * @param {number} [newIndex] New index for the item
4425 * Respond to item drag start event
4426 * @param {OO.ui.DraggableElement} item Dragged item
4428 OO
.ui
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
4431 // Map the index of each object
4432 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4433 this.items
[ i
].setIndex( i
);
4436 if ( this.orientation
=== 'horizontal' ) {
4437 // Set the height of the indicator
4438 this.$placeholder
.css( {
4439 height
: item
.$element
.outerHeight(),
4443 // Set the width of the indicator
4444 this.$placeholder
.css( {
4446 width
: item
.$element
.outerWidth()
4449 this.setDragItem( item
);
4453 * Respond to item drag end event
4455 OO
.ui
.DraggableGroupElement
.prototype.onItemDragEnd = function () {
4456 this.unsetDragItem();
4461 * Handle drop event and switch the order of the items accordingly
4462 * @param {OO.ui.DraggableElement} item Dropped item
4465 OO
.ui
.DraggableGroupElement
.prototype.onItemDrop = function ( item
) {
4466 var toIndex
= item
.getIndex();
4467 // Check if the dropped item is from the current group
4468 // TODO: Figure out a way to configure a list of legally droppable
4469 // elements even if they are not yet in the list
4470 if ( this.getDragItem() ) {
4471 // If the insertion point is 'after', the insertion index
4472 // is shifted to the right (or to the left in RTL, hence 'after')
4473 if ( this.sideInsertion
=== 'after' ) {
4476 // Emit change event
4477 this.emit( 'reorder', this.getDragItem(), toIndex
);
4479 // Return false to prevent propogation
4484 * Handle dragleave event.
4486 OO
.ui
.DraggableGroupElement
.prototype.onDragLeave = function () {
4487 // This means the item was dragged outside the widget
4494 * Respond to dragover event
4495 * @param {jQuery.Event} event Event details
4497 OO
.ui
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
4498 var dragOverObj
, $optionWidget
, itemOffset
, itemMidpoint
, itemBoundingRect
,
4499 itemSize
, cssOutput
, dragPosition
, itemIndex
, itemPosition
,
4500 clientX
= e
.originalEvent
.clientX
,
4501 clientY
= e
.originalEvent
.clientY
;
4503 // Get the OptionWidget item we are dragging over
4504 dragOverObj
= this.getElementDocument().elementFromPoint( clientX
, clientY
);
4505 $optionWidget
= $( dragOverObj
).closest( '.oo-ui-draggableElement' );
4506 if ( $optionWidget
[ 0 ] ) {
4507 itemOffset
= $optionWidget
.offset();
4508 itemBoundingRect
= $optionWidget
[ 0 ].getBoundingClientRect();
4509 itemPosition
= $optionWidget
.position();
4510 itemIndex
= $optionWidget
.data( 'index' );
4515 this.isDragging() &&
4516 itemIndex
!== this.getDragItem().getIndex()
4518 if ( this.orientation
=== 'horizontal' ) {
4519 // Calculate where the mouse is relative to the item width
4520 itemSize
= itemBoundingRect
.width
;
4521 itemMidpoint
= itemBoundingRect
.left
+ itemSize
/ 2;
4522 dragPosition
= clientX
;
4523 // Which side of the item we hover over will dictate
4524 // where the placeholder will appear, on the left or
4527 left
: dragPosition
< itemMidpoint
? itemPosition
.left
: itemPosition
.left
+ itemSize
,
4528 top
: itemPosition
.top
4531 // Calculate where the mouse is relative to the item height
4532 itemSize
= itemBoundingRect
.height
;
4533 itemMidpoint
= itemBoundingRect
.top
+ itemSize
/ 2;
4534 dragPosition
= clientY
;
4535 // Which side of the item we hover over will dictate
4536 // where the placeholder will appear, on the top or
4539 top
: dragPosition
< itemMidpoint
? itemPosition
.top
: itemPosition
.top
+ itemSize
,
4540 left
: itemPosition
.left
4543 // Store whether we are before or after an item to rearrange
4544 // For horizontal layout, we need to account for RTL, as this is flipped
4545 if ( this.orientation
=== 'horizontal' && this.$element
.css( 'direction' ) === 'rtl' ) {
4546 this.sideInsertion
= dragPosition
< itemMidpoint
? 'after' : 'before';
4548 this.sideInsertion
= dragPosition
< itemMidpoint
? 'before' : 'after';
4550 // Add drop indicator between objects
4551 if ( this.sideInsertion
) {
4564 // This means the item was dragged outside the widget
4574 * Set a dragged item
4575 * @param {OO.ui.DraggableElement} item Dragged item
4577 OO
.ui
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
4578 this.dragItem
= item
;
4582 * Unset the current dragged item
4584 OO
.ui
.DraggableGroupElement
.prototype.unsetDragItem = function () {
4585 this.dragItem
= null;
4586 this.itemDragOver
= null;
4587 this.$placeholder
.hide();
4588 this.sideInsertion
= '';
4592 * Get the current dragged item
4593 * @return {OO.ui.DraggableElement|null} item Dragged item or null if no item is dragged
4595 OO
.ui
.DraggableGroupElement
.prototype.getDragItem = function () {
4596 return this.dragItem
;
4600 * Check if there's an item being dragged.
4601 * @return {Boolean} Item is being dragged
4603 OO
.ui
.DraggableGroupElement
.prototype.isDragging = function () {
4604 return this.getDragItem() !== null;
4608 * Element containing an icon.
4610 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4611 * a control or convey information in a more space efficient way. Icons should rarely be used
4612 * without labels; such as in a toolbar where space is at a premium or within a context where the
4613 * meaning is very clear to the user.
4619 * @param {Object} [config] Configuration options
4620 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4621 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4622 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4624 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4626 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4627 // Configuration initialization
4628 config
= config
|| {};
4633 this.iconTitle
= null;
4636 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4637 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4638 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4643 OO
.initClass( OO
.ui
.IconElement
);
4645 /* Static Properties */
4650 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4652 * For i18n purposes, this property can be an object containing a `default` icon name property and
4653 * additional icon names keyed by language code.
4655 * Example of i18n icon definition:
4656 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4660 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4661 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4664 OO
.ui
.IconElement
.static.icon
= null;
4671 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4674 OO
.ui
.IconElement
.static.iconTitle
= null;
4679 * Set the icon element.
4681 * If an element is already set, it will be cleaned up before setting up the new element.
4683 * @param {jQuery} $icon Element to use as icon
4685 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4688 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4689 .removeAttr( 'title' );
4693 .addClass( 'oo-ui-iconElement-icon' )
4694 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4695 if ( this.iconTitle
!== null ) {
4696 this.$icon
.attr( 'title', this.iconTitle
);
4703 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4704 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4705 * language, use null to remove icon
4708 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4709 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4710 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4712 if ( this.icon
!== icon
) {
4714 if ( this.icon
!== null ) {
4715 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4717 if ( icon
!== null ) {
4718 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4724 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4725 this.updateThemeClasses();
4733 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4737 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4738 iconTitle
= typeof iconTitle
=== 'function' ||
4739 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4740 OO
.ui
.resolveMsg( iconTitle
) : null;
4742 if ( this.iconTitle
!== iconTitle
) {
4743 this.iconTitle
= iconTitle
;
4745 if ( this.iconTitle
!== null ) {
4746 this.$icon
.attr( 'title', iconTitle
);
4748 this.$icon
.removeAttr( 'title' );
4759 * @return {string} Icon name
4761 OO
.ui
.IconElement
.prototype.getIcon = function () {
4768 * @return {string} Icon title text
4770 OO
.ui
.IconElement
.prototype.getIconTitle = function () {
4771 return this.iconTitle
;
4775 * Element containing an indicator.
4777 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4778 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4779 * instead of performing an action directly, or an item in a list which has errors that need to be
4786 * @param {Object} [config] Configuration options
4787 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4789 * @cfg {string} [indicator] Symbolic indicator name
4790 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4792 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4793 // Configuration initialization
4794 config
= config
|| {};
4797 this.$indicator
= null;
4798 this.indicator
= null;
4799 this.indicatorTitle
= null;
4802 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4803 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4804 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4809 OO
.initClass( OO
.ui
.IndicatorElement
);
4811 /* Static Properties */
4818 * @property {string|null} Symbolic indicator name
4820 OO
.ui
.IndicatorElement
.static.indicator
= null;
4827 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4830 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4835 * Set the indicator element.
4837 * If an element is already set, it will be cleaned up before setting up the new element.
4839 * @param {jQuery} $indicator Element to use as indicator
4841 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4842 if ( this.$indicator
) {
4844 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4845 .removeAttr( 'title' );
4848 this.$indicator
= $indicator
4849 .addClass( 'oo-ui-indicatorElement-indicator' )
4850 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4851 if ( this.indicatorTitle
!== null ) {
4852 this.$indicator
.attr( 'title', this.indicatorTitle
);
4857 * Set indicator name.
4859 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4862 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4863 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4865 if ( this.indicator
!== indicator
) {
4866 if ( this.$indicator
) {
4867 if ( this.indicator
!== null ) {
4868 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4870 if ( indicator
!== null ) {
4871 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4874 this.indicator
= indicator
;
4877 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4878 this.updateThemeClasses();
4884 * Set indicator title.
4886 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4887 * null for no indicator title
4890 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4891 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4892 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4893 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4895 if ( this.indicatorTitle
!== indicatorTitle
) {
4896 this.indicatorTitle
= indicatorTitle
;
4897 if ( this.$indicator
) {
4898 if ( this.indicatorTitle
!== null ) {
4899 this.$indicator
.attr( 'title', indicatorTitle
);
4901 this.$indicator
.removeAttr( 'title' );
4910 * Get indicator name.
4912 * @return {string} Symbolic name of indicator
4914 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4915 return this.indicator
;
4919 * Get indicator title.
4921 * @return {string} Indicator title text
4923 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4924 return this.indicatorTitle
;
4928 * Element containing a label.
4934 * @param {Object} [config] Configuration options
4935 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4936 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4937 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4939 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4940 // Configuration initialization
4941 config
= config
|| {};
4946 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4949 this.setLabel( config
.label
|| this.constructor.static.label
);
4950 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4955 OO
.initClass( OO
.ui
.LabelElement
);
4960 * @event labelChange
4961 * @param {string} value
4964 /* Static Properties */
4971 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4974 OO
.ui
.LabelElement
.static.label
= null;
4979 * Set the label element.
4981 * If an element is already set, it will be cleaned up before setting up the new element.
4983 * @param {jQuery} $label Element to use as label
4985 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4986 if ( this.$label
) {
4987 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4990 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4991 this.setLabelContent( this.label
);
4997 * An empty string will result in the label being hidden. A string containing only whitespace will
4998 * be converted to a single ` `.
5000 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
5001 * text; or null for no label
5004 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
5005 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
5006 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
5008 this.$element
.toggleClass( 'oo-ui-labelElement', !!label
);
5010 if ( this.label
!== label
) {
5011 if ( this.$label
) {
5012 this.setLabelContent( label
);
5015 this.emit( 'labelChange' );
5024 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
5025 * text; or null for no label
5027 OO
.ui
.LabelElement
.prototype.getLabel = function () {
5036 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
5037 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
5038 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
5045 * Set the content of the label.
5047 * Do not call this method until after the label element has been set by #setLabelElement.
5050 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
5051 * text; or null for no label
5053 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
5054 if ( typeof label
=== 'string' ) {
5055 if ( label
.match( /^\s*$/ ) ) {
5056 // Convert whitespace only string to a single non-breaking space
5057 this.$label
.html( ' ' );
5059 this.$label
.text( label
);
5061 } else if ( label
instanceof jQuery
) {
5062 this.$label
.empty().append( label
);
5064 this.$label
.empty();
5069 * Mixin that adds a menu showing suggested values for a OO.ui.TextInputWidget.
5071 * Subclasses that set the value of #lookupInput from #onLookupMenuItemChoose should
5072 * be aware that this will cause new suggestions to be looked up for the new value. If this is
5073 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
5079 * @param {Object} [config] Configuration options
5080 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
5081 * @cfg {jQuery} [$container=this.$element] Element to render menu under
5083 OO
.ui
.LookupElement
= function OoUiLookupElement( config
) {
5084 // Configuration initialization
5085 config
= config
|| {};
5088 this.$overlay
= config
.$overlay
|| this.$element
;
5089 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
5090 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
5091 $container
: config
.$container
5093 this.lookupCache
= {};
5094 this.lookupQuery
= null;
5095 this.lookupRequest
= null;
5096 this.lookupsDisabled
= false;
5097 this.lookupInputFocused
= false;
5101 focus
: this.onLookupInputFocus
.bind( this ),
5102 blur
: this.onLookupInputBlur
.bind( this ),
5103 mousedown
: this.onLookupInputMouseDown
.bind( this )
5105 this.connect( this, { change
: 'onLookupInputChange' } );
5106 this.lookupMenu
.connect( this, {
5107 toggle
: 'onLookupMenuToggle',
5108 choose
: 'onLookupMenuItemChoose'
5112 this.$element
.addClass( 'oo-ui-lookupElement' );
5113 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
5114 this.$overlay
.append( this.lookupMenu
.$element
);
5120 * Handle input focus event.
5122 * @param {jQuery.Event} e Input focus event
5124 OO
.ui
.LookupElement
.prototype.onLookupInputFocus = function () {
5125 this.lookupInputFocused
= true;
5126 this.populateLookupMenu();
5130 * Handle input blur event.
5132 * @param {jQuery.Event} e Input blur event
5134 OO
.ui
.LookupElement
.prototype.onLookupInputBlur = function () {
5135 this.closeLookupMenu();
5136 this.lookupInputFocused
= false;
5140 * Handle input mouse down event.
5142 * @param {jQuery.Event} e Input mouse down event
5144 OO
.ui
.LookupElement
.prototype.onLookupInputMouseDown = function () {
5145 // Only open the menu if the input was already focused.
5146 // This way we allow the user to open the menu again after closing it with Esc
5147 // by clicking in the input. Opening (and populating) the menu when initially
5148 // clicking into the input is handled by the focus handler.
5149 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
5150 this.populateLookupMenu();
5155 * Handle input change event.
5157 * @param {string} value New input value
5159 OO
.ui
.LookupElement
.prototype.onLookupInputChange = function () {
5160 if ( this.lookupInputFocused
) {
5161 this.populateLookupMenu();
5166 * Handle the lookup menu being shown/hidden.
5168 * @param {boolean} visible Whether the lookup menu is now visible.
5170 OO
.ui
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
5172 // When the menu is hidden, abort any active request and clear the menu.
5173 // This has to be done here in addition to closeLookupMenu(), because
5174 // MenuSelectWidget will close itself when the user presses Esc.
5175 this.abortLookupRequest();
5176 this.lookupMenu
.clearItems();
5181 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
5183 * @param {OO.ui.MenuOptionWidget|null} item Selected item
5185 OO
.ui
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
5187 this.setValue( item
.getData() );
5194 * @return {OO.ui.TextInputMenuSelectWidget}
5196 OO
.ui
.LookupElement
.prototype.getLookupMenu = function () {
5197 return this.lookupMenu
;
5201 * Disable or re-enable lookups.
5203 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
5205 * @param {boolean} disabled Disable lookups
5207 OO
.ui
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
5208 this.lookupsDisabled
= !!disabled
;
5212 * Open the menu. If there are no entries in the menu, this does nothing.
5216 OO
.ui
.LookupElement
.prototype.openLookupMenu = function () {
5217 if ( !this.lookupMenu
.isEmpty() ) {
5218 this.lookupMenu
.toggle( true );
5224 * Close the menu, empty it, and abort any pending request.
5228 OO
.ui
.LookupElement
.prototype.closeLookupMenu = function () {
5229 this.lookupMenu
.toggle( false );
5230 this.abortLookupRequest();
5231 this.lookupMenu
.clearItems();
5236 * Request menu items based on the input's current value, and when they arrive,
5237 * populate the menu with these items and show the menu.
5239 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
5243 OO
.ui
.LookupElement
.prototype.populateLookupMenu = function () {
5245 value
= this.getValue();
5247 if ( this.lookupsDisabled
) {
5251 // If the input is empty, clear the menu
5252 if ( value
=== '' ) {
5253 this.closeLookupMenu();
5254 // Skip population if there is already a request pending for the current value
5255 } else if ( value
!== this.lookupQuery
) {
5256 this.getLookupMenuItems()
5257 .done( function ( items
) {
5258 widget
.lookupMenu
.clearItems();
5259 if ( items
.length
) {
5263 widget
.initializeLookupMenuSelection();
5265 widget
.lookupMenu
.toggle( false );
5268 .fail( function () {
5269 widget
.lookupMenu
.clearItems();
5277 * Select and highlight the first selectable item in the menu.
5281 OO
.ui
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
5282 if ( !this.lookupMenu
.getSelectedItem() ) {
5283 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
5285 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
5289 * Get lookup menu items for the current query.
5291 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
5292 * the done event. If the request was aborted to make way for a subsequent request, this promise
5293 * will not be rejected: it will remain pending forever.
5295 OO
.ui
.LookupElement
.prototype.getLookupMenuItems = function () {
5297 value
= this.getValue(),
5298 deferred
= $.Deferred(),
5301 this.abortLookupRequest();
5302 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
5303 deferred
.resolve( this.getLookupMenuOptionsFromData( this.lookupCache
[ value
] ) );
5306 this.lookupQuery
= value
;
5307 ourRequest
= this.lookupRequest
= this.getLookupRequest();
5309 .always( function () {
5310 // We need to pop pending even if this is an old request, otherwise
5311 // the widget will remain pending forever.
5312 // TODO: this assumes that an aborted request will fail or succeed soon after
5313 // being aborted, or at least eventually. It would be nice if we could popPending()
5314 // at abort time, but only if we knew that we hadn't already called popPending()
5315 // for that request.
5316 widget
.popPending();
5318 .done( function ( data
) {
5319 // If this is an old request (and aborting it somehow caused it to still succeed),
5320 // ignore its success completely
5321 if ( ourRequest
=== widget
.lookupRequest
) {
5322 widget
.lookupQuery
= null;
5323 widget
.lookupRequest
= null;
5324 widget
.lookupCache
[ value
] = widget
.getLookupCacheDataFromResponse( data
);
5325 deferred
.resolve( widget
.getLookupMenuOptionsFromData( widget
.lookupCache
[ value
] ) );
5328 .fail( function () {
5329 // If this is an old request (or a request failing because it's being aborted),
5330 // ignore its failure completely
5331 if ( ourRequest
=== widget
.lookupRequest
) {
5332 widget
.lookupQuery
= null;
5333 widget
.lookupRequest
= null;
5338 return deferred
.promise();
5342 * Abort the currently pending lookup request, if any.
5344 OO
.ui
.LookupElement
.prototype.abortLookupRequest = function () {
5345 var oldRequest
= this.lookupRequest
;
5347 // First unset this.lookupRequest to the fail handler will notice
5348 // that the request is no longer current
5349 this.lookupRequest
= null;
5350 this.lookupQuery
= null;
5356 * Get a new request object of the current lookup query value.
5359 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
5361 OO
.ui
.LookupElement
.prototype.getLookupRequest = function () {
5362 // Stub, implemented in subclass
5367 * Pre-process data returned by the request from #getLookupRequest.
5369 * The return value of this function will be cached, and any further queries for the given value
5370 * will use the cache rather than doing API requests.
5373 * @param {Mixed} data Response from server
5374 * @return {Mixed} Cached result data
5376 OO
.ui
.LookupElement
.prototype.getLookupCacheDataFromResponse = function () {
5377 // Stub, implemented in subclass
5382 * Get a list of menu option widgets from the (possibly cached) data returned by
5383 * #getLookupCacheDataFromResponse.
5386 * @param {Mixed} data Cached result data, usually an array
5387 * @return {OO.ui.MenuOptionWidget[]} Menu items
5389 OO
.ui
.LookupElement
.prototype.getLookupMenuOptionsFromData = function () {
5390 // Stub, implemented in subclass
5395 * Element containing an OO.ui.PopupWidget object.
5401 * @param {Object} [config] Configuration options
5402 * @cfg {Object} [popup] Configuration to pass to popup
5403 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
5405 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
5406 // Configuration initialization
5407 config
= config
|| {};
5410 this.popup
= new OO
.ui
.PopupWidget( $.extend(
5411 { autoClose
: true },
5413 { $: this.$, $autoCloseIgnore
: this.$element
}
5422 * @return {OO.ui.PopupWidget} Popup widget
5424 OO
.ui
.PopupElement
.prototype.getPopup = function () {
5429 * Element with named flags that can be added, removed, listed and checked.
5431 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
5432 * the flag name. Flags are primarily useful for styling.
5438 * @param {Object} [config] Configuration options
5439 * @cfg {string|string[]} [flags] Flags describing importance and functionality, e.g. 'primary',
5440 * 'safe', 'progressive', 'destructive' or 'constructive'
5441 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
5443 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
5444 // Configuration initialization
5445 config
= config
|| {};
5449 this.$flagged
= null;
5452 this.setFlags( config
.flags
);
5453 this.setFlaggedElement( config
.$flagged
|| this.$element
);
5460 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
5461 * added/removed properties
5467 * Set the flagged element.
5469 * If an element is already set, it will be cleaned up before setting up the new element.
5471 * @param {jQuery} $flagged Element to add flags to
5473 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
5474 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
5475 return 'oo-ui-flaggedElement-' + flag
;
5478 if ( this.$flagged
) {
5479 this.$flagged
.removeClass( classNames
);
5482 this.$flagged
= $flagged
.addClass( classNames
);
5486 * Check if a flag is set.
5488 * @param {string} flag Name of flag
5489 * @return {boolean} Has flag
5491 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
5492 return flag
in this.flags
;
5496 * Get the names of all flags set.
5498 * @return {string[]} Flag names
5500 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
5501 return Object
.keys( this.flags
);
5510 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
5511 var flag
, className
,
5514 classPrefix
= 'oo-ui-flaggedElement-';
5516 for ( flag
in this.flags
) {
5517 className
= classPrefix
+ flag
;
5518 changes
[ flag
] = false;
5519 delete this.flags
[ flag
];
5520 remove
.push( className
);
5523 if ( this.$flagged
) {
5524 this.$flagged
.removeClass( remove
.join( ' ' ) );
5527 this.updateThemeClasses();
5528 this.emit( 'flag', changes
);
5534 * Add one or more flags.
5536 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
5537 * keyed by flag name containing boolean set/remove instructions.
5541 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
5542 var i
, len
, flag
, className
,
5546 classPrefix
= 'oo-ui-flaggedElement-';
5548 if ( typeof flags
=== 'string' ) {
5549 className
= classPrefix
+ flags
;
5551 if ( !this.flags
[ flags
] ) {
5552 this.flags
[ flags
] = true;
5553 add
.push( className
);
5555 } else if ( $.isArray( flags
) ) {
5556 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
5558 className
= classPrefix
+ flag
;
5560 if ( !this.flags
[ flag
] ) {
5561 changes
[ flag
] = true;
5562 this.flags
[ flag
] = true;
5563 add
.push( className
);
5566 } else if ( OO
.isPlainObject( flags
) ) {
5567 for ( flag
in flags
) {
5568 className
= classPrefix
+ flag
;
5569 if ( flags
[ flag
] ) {
5571 if ( !this.flags
[ flag
] ) {
5572 changes
[ flag
] = true;
5573 this.flags
[ flag
] = true;
5574 add
.push( className
);
5578 if ( this.flags
[ flag
] ) {
5579 changes
[ flag
] = false;
5580 delete this.flags
[ flag
];
5581 remove
.push( className
);
5587 if ( this.$flagged
) {
5589 .addClass( add
.join( ' ' ) )
5590 .removeClass( remove
.join( ' ' ) );
5593 this.updateThemeClasses();
5594 this.emit( 'flag', changes
);
5600 * Element with a title.
5602 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
5603 * not visible on touch devices.
5609 * @param {Object} [config] Configuration options
5610 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
5611 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
5612 * static property 'title' is used.
5614 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
5615 // Configuration initialization
5616 config
= config
|| {};
5619 this.$titled
= null;
5623 this.setTitle( config
.title
|| this.constructor.static.title
);
5624 this.setTitledElement( config
.$titled
|| this.$element
);
5629 OO
.initClass( OO
.ui
.TitledElement
);
5631 /* Static Properties */
5638 * @property {string|Function} Title text or a function that returns text
5640 OO
.ui
.TitledElement
.static.title
= null;
5645 * Set the titled element.
5647 * If an element is already set, it will be cleaned up before setting up the new element.
5649 * @param {jQuery} $titled Element to set title on
5651 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
5652 if ( this.$titled
) {
5653 this.$titled
.removeAttr( 'title' );
5656 this.$titled
= $titled
;
5658 this.$titled
.attr( 'title', this.title
);
5665 * @param {string|Function|null} title Title text, a function that returns text or null for no title
5668 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
5669 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
5671 if ( this.title
!== title
) {
5672 if ( this.$titled
) {
5673 if ( title
!== null ) {
5674 this.$titled
.attr( 'title', title
);
5676 this.$titled
.removeAttr( 'title' );
5688 * @return {string} Title string
5690 OO
.ui
.TitledElement
.prototype.getTitle = function () {
5695 * Element that can be automatically clipped to visible boundaries.
5697 * Whenever the element's natural height changes, you have to call
5698 * #clip to make sure it's still clipping correctly.
5704 * @param {Object} [config] Configuration options
5705 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
5707 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
5708 // Configuration initialization
5709 config
= config
|| {};
5712 this.$clippable
= null;
5713 this.clipping
= false;
5714 this.clippedHorizontally
= false;
5715 this.clippedVertically
= false;
5716 this.$clippableContainer
= null;
5717 this.$clippableScroller
= null;
5718 this.$clippableWindow
= null;
5719 this.idealWidth
= null;
5720 this.idealHeight
= null;
5721 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
5722 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
5725 this.setClippableElement( config
.$clippable
|| this.$element
);
5731 * Set clippable element.
5733 * If an element is already set, it will be cleaned up before setting up the new element.
5735 * @param {jQuery} $clippable Element to make clippable
5737 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
5738 if ( this.$clippable
) {
5739 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
5740 this.$clippable
.css( { width
: '', height
: '', overflowX
: '', overflowY
: '' } );
5741 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
5744 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
5751 * Do not turn clipping on until after the element is attached to the DOM and visible.
5753 * @param {boolean} [clipping] Enable clipping, omit to toggle
5756 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
5757 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
5759 if ( this.clipping
!== clipping
) {
5760 this.clipping
= clipping
;
5762 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
5763 // If the clippable container is the root, we have to listen to scroll events and check
5764 // jQuery.scrollTop on the window because of browser inconsistencies
5765 this.$clippableScroller
= this.$clippableContainer
.is( 'html, body' ) ?
5766 this.$( OO
.ui
.Element
.static.getWindow( this.$clippableContainer
) ) :
5767 this.$clippableContainer
;
5768 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
5769 this.$clippableWindow
= this.$( this.getElementWindow() )
5770 .on( 'resize', this.onClippableWindowResizeHandler
);
5771 // Initial clip after visible
5774 this.$clippable
.css( { width
: '', height
: '', overflowX
: '', overflowY
: '' } );
5775 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
5777 this.$clippableContainer
= null;
5778 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
5779 this.$clippableScroller
= null;
5780 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
5781 this.$clippableWindow
= null;
5789 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
5791 * @return {boolean} Element will be clipped to the visible area
5793 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
5794 return this.clipping
;
5798 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
5800 * @return {boolean} Part of the element is being clipped
5802 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
5803 return this.clippedHorizontally
|| this.clippedVertically
;
5807 * Check if the right of the element is being clipped by the nearest scrollable container.
5809 * @return {boolean} Part of the element is being clipped
5811 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
5812 return this.clippedHorizontally
;
5816 * Check if the bottom of the element is being clipped by the nearest scrollable container.
5818 * @return {boolean} Part of the element is being clipped
5820 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
5821 return this.clippedVertically
;
5825 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
5827 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
5828 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
5830 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
5831 this.idealWidth
= width
;
5832 this.idealHeight
= height
;
5834 if ( !this.clipping
) {
5835 // Update dimensions
5836 this.$clippable
.css( { width
: width
, height
: height
} );
5838 // While clipping, idealWidth and idealHeight are not considered
5842 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
5843 * the element's natural height changes.
5845 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
5846 * overlapped by, the visible area of the nearest scrollable container.
5850 OO
.ui
.ClippableElement
.prototype.clip = function () {
5851 if ( !this.clipping
) {
5852 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
5856 var buffer
= 7, // Chosen by fair dice roll
5857 cOffset
= this.$clippable
.offset(),
5858 $container
= this.$clippableContainer
.is( 'html, body' ) ?
5859 this.$clippableWindow
: this.$clippableContainer
,
5860 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
5861 ccHeight
= $container
.innerHeight() - buffer
,
5862 ccWidth
= $container
.innerWidth() - buffer
,
5863 cHeight
= this.$clippable
.outerHeight() + buffer
,
5864 cWidth
= this.$clippable
.outerWidth() + buffer
,
5865 scrollTop
= this.$clippableScroller
.scrollTop(),
5866 scrollLeft
= this.$clippableScroller
.scrollLeft(),
5867 desiredWidth
= cOffset
.left
< 0 ?
5868 cWidth
+ cOffset
.left
:
5869 ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
5870 desiredHeight
= cOffset
.top
< 0 ?
5871 cHeight
+ cOffset
.top
:
5872 ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
5873 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
5874 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
5875 clipWidth
= desiredWidth
< naturalWidth
,
5876 clipHeight
= desiredHeight
< naturalHeight
;
5879 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
5881 this.$clippable
.css( { width
: this.idealWidth
|| '', overflowX
: '' } );
5884 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
5886 this.$clippable
.css( { height
: this.idealHeight
|| '', overflowY
: '' } );
5889 // If we stopped clipping in at least one of the dimensions
5890 if ( !clipWidth
|| !clipHeight
) {
5891 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
5894 this.clippedHorizontally
= clipWidth
;
5895 this.clippedVertically
= clipHeight
;
5901 * Generic toolbar tool.
5905 * @extends OO.ui.Widget
5906 * @mixins OO.ui.IconElement
5907 * @mixins OO.ui.FlaggedElement
5910 * @param {OO.ui.ToolGroup} toolGroup
5911 * @param {Object} [config] Configuration options
5912 * @cfg {string|Function} [title] Title text or a function that returns text
5914 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
5915 // Configuration initialization
5916 config
= config
|| {};
5918 // Parent constructor
5919 OO
.ui
.Tool
.super.call( this, config
);
5921 // Mixin constructors
5922 OO
.ui
.IconElement
.call( this, config
);
5923 OO
.ui
.FlaggedElement
.call( this, config
);
5926 this.toolGroup
= toolGroup
;
5927 this.toolbar
= this.toolGroup
.getToolbar();
5928 this.active
= false;
5929 this.$title
= this.$( '<span>' );
5930 this.$accel
= this.$( '<span>' );
5931 this.$link
= this.$( '<a>' );
5935 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
5938 this.$title
.addClass( 'oo-ui-tool-title' );
5940 .addClass( 'oo-ui-tool-accel' )
5942 // This may need to be changed if the key names are ever localized,
5943 // but for now they are essentially written in English
5948 .addClass( 'oo-ui-tool-link' )
5949 .append( this.$icon
, this.$title
, this.$accel
)
5950 .prop( 'tabIndex', 0 )
5951 .attr( 'role', 'button' );
5953 .data( 'oo-ui-tool', this )
5955 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5956 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5958 .append( this.$link
);
5959 this.setTitle( config
.title
|| this.constructor.static.title
);
5964 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5965 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5966 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5974 /* Static Properties */
5980 OO
.ui
.Tool
.static.tagName
= 'span';
5983 * Symbolic name of tool.
5988 * @property {string}
5990 OO
.ui
.Tool
.static.name
= '';
5998 * @property {string}
6000 OO
.ui
.Tool
.static.group
= '';
6005 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
6006 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
6007 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
6008 * appended to the title if the tool is part of a bar tool group.
6013 * @property {string|Function} Title text or a function that returns text
6015 OO
.ui
.Tool
.static.title
= '';
6018 * Tool can be automatically added to catch-all groups.
6022 * @property {boolean}
6024 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
6027 * Tool can be automatically added to named groups.
6030 * @property {boolean}
6033 OO
.ui
.Tool
.static.autoAddToGroup
= true;
6036 * Check if this tool is compatible with given data.
6040 * @param {Mixed} data Data to check
6041 * @return {boolean} Tool can be used with data
6043 OO
.ui
.Tool
.static.isCompatibleWith = function () {
6050 * Handle the toolbar state being updated.
6052 * This is an abstract method that must be overridden in a concrete subclass.
6056 OO
.ui
.Tool
.prototype.onUpdateState = function () {
6058 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
6063 * Handle the tool being selected.
6065 * This is an abstract method that must be overridden in a concrete subclass.
6069 OO
.ui
.Tool
.prototype.onSelect = function () {
6071 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
6076 * Check if the button is active.
6078 * @return {boolean} Button is active
6080 OO
.ui
.Tool
.prototype.isActive = function () {
6085 * Make the button appear active or inactive.
6087 * @param {boolean} state Make button appear active
6089 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
6090 this.active
= !!state
;
6091 if ( this.active
) {
6092 this.$element
.addClass( 'oo-ui-tool-active' );
6094 this.$element
.removeClass( 'oo-ui-tool-active' );
6099 * Get the tool title.
6101 * @param {string|Function} title Title text or a function that returns text
6104 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
6105 this.title
= OO
.ui
.resolveMsg( title
);
6111 * Get the tool title.
6113 * @return {string} Title text
6115 OO
.ui
.Tool
.prototype.getTitle = function () {
6120 * Get the tool's symbolic name.
6122 * @return {string} Symbolic name of tool
6124 OO
.ui
.Tool
.prototype.getName = function () {
6125 return this.constructor.static.name
;
6131 OO
.ui
.Tool
.prototype.updateTitle = function () {
6132 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
6133 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
6134 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
6137 this.$title
.text( this.title
);
6138 this.$accel
.text( accel
);
6140 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
6141 tooltipParts
.push( this.title
);
6143 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
6144 tooltipParts
.push( accel
);
6146 if ( tooltipParts
.length
) {
6147 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
6149 this.$link
.removeAttr( 'title' );
6156 OO
.ui
.Tool
.prototype.destroy = function () {
6157 this.toolbar
.disconnect( this );
6158 this.$element
.remove();
6162 * Collection of tool groups.
6165 * @extends OO.ui.Element
6166 * @mixins OO.EventEmitter
6167 * @mixins OO.ui.GroupElement
6170 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
6171 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
6172 * @param {Object} [config] Configuration options
6173 * @cfg {boolean} [actions] Add an actions section opposite to the tools
6174 * @cfg {boolean} [shadow] Add a shadow below the toolbar
6176 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
6177 // Configuration initialization
6178 config
= config
|| {};
6180 // Parent constructor
6181 OO
.ui
.Toolbar
.super.call( this, config
);
6183 // Mixin constructors
6184 OO
.EventEmitter
.call( this );
6185 OO
.ui
.GroupElement
.call( this, config
);
6188 this.toolFactory
= toolFactory
;
6189 this.toolGroupFactory
= toolGroupFactory
;
6192 this.$bar
= this.$( '<div>' );
6193 this.$actions
= this.$( '<div>' );
6194 this.initialized
= false;
6198 .add( this.$bar
).add( this.$group
).add( this.$actions
)
6199 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
6202 this.$group
.addClass( 'oo-ui-toolbar-tools' );
6203 if ( config
.actions
) {
6204 this.$bar
.append( this.$actions
.addClass( 'oo-ui-toolbar-actions' ) );
6207 .addClass( 'oo-ui-toolbar-bar' )
6208 .append( this.$group
, '<div style="clear:both"></div>' );
6209 if ( config
.shadow
) {
6210 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
6212 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
6217 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
6218 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
6219 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
6224 * Get the tool factory.
6226 * @return {OO.ui.ToolFactory} Tool factory
6228 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
6229 return this.toolFactory
;
6233 * Get the tool group factory.
6235 * @return {OO.Factory} Tool group factory
6237 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
6238 return this.toolGroupFactory
;
6242 * Handles mouse down events.
6244 * @param {jQuery.Event} e Mouse down event
6246 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
6247 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
6248 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
6249 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[ 0 ] === $closestWidgetToToolbar
[ 0 ] ) {
6255 * Sets up handles and preloads required information for the toolbar to work.
6256 * This must be called after it is attached to a visible document and before doing anything else.
6258 OO
.ui
.Toolbar
.prototype.initialize = function () {
6259 this.initialized
= true;
6265 * Tools can be specified in the following ways:
6267 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
6268 * - All tools in a group: `{ group: 'group-name' }`
6269 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
6271 * @param {Object.<string,Array>} groups List of tool group configurations
6272 * @param {Array|string} [groups.include] Tools to include
6273 * @param {Array|string} [groups.exclude] Tools to exclude
6274 * @param {Array|string} [groups.promote] Tools to promote to the beginning
6275 * @param {Array|string} [groups.demote] Tools to demote to the end
6277 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
6278 var i
, len
, type
, group
,
6280 defaultType
= 'bar';
6282 // Cleanup previous groups
6285 // Build out new groups
6286 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
6287 group
= groups
[ i
];
6288 if ( group
.include
=== '*' ) {
6289 // Apply defaults to catch-all groups
6290 if ( group
.type
=== undefined ) {
6291 group
.type
= 'list';
6293 if ( group
.label
=== undefined ) {
6294 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
6297 // Check type has been registered
6298 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
6300 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
6303 this.addItems( items
);
6307 * Remove all tools and groups from the toolbar.
6309 OO
.ui
.Toolbar
.prototype.reset = function () {
6314 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6315 this.items
[ i
].destroy();
6321 * Destroys toolbar, removing event handlers and DOM elements.
6323 * Call this whenever you are done using a toolbar.
6325 OO
.ui
.Toolbar
.prototype.destroy = function () {
6327 this.$element
.remove();
6331 * Check if tool has not been used yet.
6333 * @param {string} name Symbolic name of tool
6334 * @return {boolean} Tool is available
6336 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
6337 return !this.tools
[ name
];
6341 * Prevent tool from being used again.
6343 * @param {OO.ui.Tool} tool Tool to reserve
6345 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
6346 this.tools
[ tool
.getName() ] = tool
;
6350 * Allow tool to be used again.
6352 * @param {OO.ui.Tool} tool Tool to release
6354 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
6355 delete this.tools
[ tool
.getName() ];
6359 * Get accelerator label for tool.
6361 * This is a stub that should be overridden to provide access to accelerator information.
6363 * @param {string} name Symbolic name of tool
6364 * @return {string|undefined} Tool accelerator label if available
6366 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
6371 * Collection of tools.
6373 * Tools can be specified in the following ways:
6375 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
6376 * - All tools in a group: `{ group: 'group-name' }`
6377 * - All tools: `'*'`
6381 * @extends OO.ui.Widget
6382 * @mixins OO.ui.GroupElement
6385 * @param {OO.ui.Toolbar} toolbar
6386 * @param {Object} [config] Configuration options
6387 * @cfg {Array|string} [include=[]] List of tools to include
6388 * @cfg {Array|string} [exclude=[]] List of tools to exclude
6389 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
6390 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
6392 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
6393 // Configuration initialization
6394 config
= config
|| {};
6396 // Parent constructor
6397 OO
.ui
.ToolGroup
.super.call( this, config
);
6399 // Mixin constructors
6400 OO
.ui
.GroupElement
.call( this, config
);
6403 this.toolbar
= toolbar
;
6405 this.pressed
= null;
6406 this.autoDisabled
= false;
6407 this.include
= config
.include
|| [];
6408 this.exclude
= config
.exclude
|| [];
6409 this.promote
= config
.promote
|| [];
6410 this.demote
= config
.demote
|| [];
6411 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
6415 'mousedown touchstart': this.onPointerDown
.bind( this ),
6416 'mouseup touchend': this.onPointerUp
.bind( this ),
6417 mouseover
: this.onMouseOver
.bind( this ),
6418 mouseout
: this.onMouseOut
.bind( this )
6420 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
6421 this.aggregate( { disable
: 'itemDisable' } );
6422 this.connect( this, { itemDisable
: 'updateDisabled' } );
6425 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
6427 .addClass( 'oo-ui-toolGroup' )
6428 .append( this.$group
);
6434 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
6435 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
6443 /* Static Properties */
6446 * Show labels in tooltips.
6450 * @property {boolean}
6452 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
6455 * Show acceleration labels in tooltips.
6459 * @property {boolean}
6461 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
6464 * Automatically disable the toolgroup when all tools are disabled
6468 * @property {boolean}
6470 OO
.ui
.ToolGroup
.static.autoDisable
= true;
6477 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
6478 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
6484 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
6485 var i
, item
, allDisabled
= true;
6487 if ( this.constructor.static.autoDisable
) {
6488 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
6489 item
= this.items
[ i
];
6490 if ( !item
.isDisabled() ) {
6491 allDisabled
= false;
6495 this.autoDisabled
= allDisabled
;
6497 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
6501 * Handle mouse down events.
6503 * @param {jQuery.Event} e Mouse down event
6505 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
6506 // e.which is 0 for touch events, 1 for left mouse button
6507 if ( !this.isDisabled() && e
.which
<= 1 ) {
6508 this.pressed
= this.getTargetTool( e
);
6509 if ( this.pressed
) {
6510 this.pressed
.setActive( true );
6511 this.getElementDocument().addEventListener(
6512 'mouseup', this.onCapturedMouseUpHandler
, true
6520 * Handle captured mouse up events.
6522 * @param {Event} e Mouse up event
6524 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
6525 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
6526 // onPointerUp may be called a second time, depending on where the mouse is when the button is
6527 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
6528 this.onPointerUp( e
);
6532 * Handle mouse up events.
6534 * @param {jQuery.Event} e Mouse up event
6536 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
6537 var tool
= this.getTargetTool( e
);
6539 // e.which is 0 for touch events, 1 for left mouse button
6540 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
6541 this.pressed
.onSelect();
6544 this.pressed
= null;
6549 * Handle mouse over events.
6551 * @param {jQuery.Event} e Mouse over event
6553 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
6554 var tool
= this.getTargetTool( e
);
6556 if ( this.pressed
&& this.pressed
=== tool
) {
6557 this.pressed
.setActive( true );
6562 * Handle mouse out events.
6564 * @param {jQuery.Event} e Mouse out event
6566 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
6567 var tool
= this.getTargetTool( e
);
6569 if ( this.pressed
&& this.pressed
=== tool
) {
6570 this.pressed
.setActive( false );
6575 * Get the closest tool to a jQuery.Event.
6577 * Only tool links are considered, which prevents other elements in the tool such as popups from
6578 * triggering tool group interactions.
6581 * @param {jQuery.Event} e
6582 * @return {OO.ui.Tool|null} Tool, `null` if none was found
6584 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
6586 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
6588 if ( $item
.length
) {
6589 tool
= $item
.parent().data( 'oo-ui-tool' );
6592 return tool
&& !tool
.isDisabled() ? tool
: null;
6596 * Handle tool registry register events.
6598 * If a tool is registered after the group is created, we must repopulate the list to account for:
6600 * - a tool being added that may be included
6601 * - a tool already included being overridden
6603 * @param {string} name Symbolic name of tool
6605 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
6610 * Get the toolbar this group is in.
6612 * @return {OO.ui.Toolbar} Toolbar of group
6614 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
6615 return this.toolbar
;
6619 * Add and remove tools based on configuration.
6621 OO
.ui
.ToolGroup
.prototype.populate = function () {
6622 var i
, len
, name
, tool
,
6623 toolFactory
= this.toolbar
.getToolFactory(),
6627 list
= this.toolbar
.getToolFactory().getTools(
6628 this.include
, this.exclude
, this.promote
, this.demote
6631 // Build a list of needed tools
6632 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
6636 toolFactory
.lookup( name
) &&
6637 // Tool is available or is already in this group
6638 ( this.toolbar
.isToolAvailable( name
) || this.tools
[ name
] )
6640 tool
= this.tools
[ name
];
6642 // Auto-initialize tools on first use
6643 this.tools
[ name
] = tool
= toolFactory
.create( name
, this );
6646 this.toolbar
.reserveTool( tool
);
6648 names
[ name
] = true;
6651 // Remove tools that are no longer needed
6652 for ( name
in this.tools
) {
6653 if ( !names
[ name
] ) {
6654 this.tools
[ name
].destroy();
6655 this.toolbar
.releaseTool( this.tools
[ name
] );
6656 remove
.push( this.tools
[ name
] );
6657 delete this.tools
[ name
];
6660 if ( remove
.length
) {
6661 this.removeItems( remove
);
6663 // Update emptiness state
6665 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
6667 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
6669 // Re-add tools (moving existing ones to new locations)
6670 this.addItems( add
);
6671 // Disabled state may depend on items
6672 this.updateDisabled();
6676 * Destroy tool group.
6678 OO
.ui
.ToolGroup
.prototype.destroy = function () {
6682 this.toolbar
.getToolFactory().disconnect( this );
6683 for ( name
in this.tools
) {
6684 this.toolbar
.releaseTool( this.tools
[ name
] );
6685 this.tools
[ name
].disconnect( this ).destroy();
6686 delete this.tools
[ name
];
6688 this.$element
.remove();
6692 * Dialog for showing a message.
6695 * - Registers two actions by default (safe and primary).
6696 * - Renders action widgets in the footer.
6699 * @extends OO.ui.Dialog
6702 * @param {Object} [config] Configuration options
6704 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
6705 // Parent constructor
6706 OO
.ui
.MessageDialog
.super.call( this, config
);
6709 this.verticalActionLayout
= null;
6712 this.$element
.addClass( 'oo-ui-messageDialog' );
6717 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
6719 /* Static Properties */
6721 OO
.ui
.MessageDialog
.static.name
= 'message';
6723 OO
.ui
.MessageDialog
.static.size
= 'small';
6725 OO
.ui
.MessageDialog
.static.verbose
= false;
6730 * A confirmation dialog's title should describe what the progressive action will do. An alert
6731 * dialog's title should describe what event occurred.
6735 * @property {jQuery|string|Function|null}
6737 OO
.ui
.MessageDialog
.static.title
= null;
6740 * A confirmation dialog's message should describe the consequences of the progressive action. An
6741 * alert dialog's message should describe why the event occurred.
6745 * @property {jQuery|string|Function|null}
6747 OO
.ui
.MessageDialog
.static.message
= null;
6749 OO
.ui
.MessageDialog
.static.actions
= [
6750 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
6751 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
6759 OO
.ui
.MessageDialog
.prototype.setManager = function ( manager
) {
6760 OO
.ui
.MessageDialog
.super.prototype.setManager
.call( this, manager
);
6763 this.manager
.connect( this, {
6773 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
6775 return OO
.ui
.MessageDialog
.super.prototype.onActionResize
.call( this, action
);
6779 * Handle window resized events.
6781 OO
.ui
.MessageDialog
.prototype.onResize = function () {
6783 dialog
.fitActions();
6784 // Wait for CSS transition to finish and do it again :(
6785 setTimeout( function () {
6786 dialog
.fitActions();
6791 * Toggle action layout between vertical and horizontal.
6793 * @param {boolean} [value] Layout actions vertically, omit to toggle
6796 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
6797 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
6799 if ( value
!== this.verticalActionLayout
) {
6800 this.verticalActionLayout
= value
;
6802 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
6803 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
6812 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
6814 return new OO
.ui
.Process( function () {
6815 this.close( { action
: action
} );
6818 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
6824 * @param {Object} [data] Dialog opening data
6825 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
6826 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
6827 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
6828 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
6831 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
6835 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
6836 .next( function () {
6837 this.title
.setLabel(
6838 data
.title
!== undefined ? data
.title
: this.constructor.static.title
6840 this.message
.setLabel(
6841 data
.message
!== undefined ? data
.message
: this.constructor.static.message
6843 this.message
.$element
.toggleClass(
6844 'oo-ui-messageDialog-message-verbose',
6845 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
6853 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
6854 var bodyHeight
, oldOverflow
,
6855 $scrollable
= this.container
.$element
;
6857 oldOverflow
= $scrollable
[ 0 ].style
.overflow
;
6858 $scrollable
[ 0 ].style
.overflow
= 'hidden';
6860 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
6862 bodyHeight
= this.text
.$element
.outerHeight( true );
6863 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
6871 OO
.ui
.MessageDialog
.prototype.setDimensions = function ( dim
) {
6872 var $scrollable
= this.container
.$element
;
6873 OO
.ui
.MessageDialog
.super.prototype.setDimensions
.call( this, dim
);
6875 // Twiddle the overflow property, otherwise an unnecessary scrollbar will be produced.
6876 // Need to do it after transition completes (250ms), add 50ms just in case.
6877 setTimeout( function () {
6878 var oldOverflow
= $scrollable
[ 0 ].style
.overflow
;
6879 $scrollable
[ 0 ].style
.overflow
= 'hidden';
6881 OO
.ui
.Element
.static.reconsiderScrollbars( $scrollable
[ 0 ] );
6883 $scrollable
[ 0 ].style
.overflow
= oldOverflow
;
6892 OO
.ui
.MessageDialog
.prototype.initialize = function () {
6894 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
6897 this.$actions
= this.$( '<div>' );
6898 this.container
= new OO
.ui
.PanelLayout( {
6899 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
6901 this.text
= new OO
.ui
.PanelLayout( {
6902 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
6904 this.message
= new OO
.ui
.LabelWidget( {
6905 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
6909 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
6910 this.$content
.addClass( 'oo-ui-messageDialog-content' );
6911 this.container
.$element
.append( this.text
.$element
);
6912 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
6913 this.$body
.append( this.container
.$element
);
6914 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
6915 this.$foot
.append( this.$actions
);
6921 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
6922 var i
, len
, other
, special
, others
;
6925 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
6927 special
= this.actions
.getSpecial();
6928 others
= this.actions
.getOthers();
6929 if ( special
.safe
) {
6930 this.$actions
.append( special
.safe
.$element
);
6931 special
.safe
.toggleFramed( false );
6933 if ( others
.length
) {
6934 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6935 other
= others
[ i
];
6936 this.$actions
.append( other
.$element
);
6937 other
.toggleFramed( false );
6940 if ( special
.primary
) {
6941 this.$actions
.append( special
.primary
.$element
);
6942 special
.primary
.toggleFramed( false );
6945 if ( !this.isOpening() ) {
6946 // If the dialog is currently opening, this will be called automatically soon.
6947 // This also calls #fitActions.
6953 * Fit action actions into columns or rows.
6955 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
6957 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
6959 previous
= this.verticalActionLayout
,
6960 actions
= this.actions
.get();
6963 this.toggleVerticalActionLayout( false );
6964 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
6965 action
= actions
[ i
];
6966 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
6967 this.toggleVerticalActionLayout( true );
6972 if ( this.verticalActionLayout
!== previous
) {
6973 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6974 // We changed the layout, window height might need to be updated.
6980 * Navigation dialog window.
6983 * - Show and hide errors.
6984 * - Retry an action.
6987 * - Renders header with dialog title and one action widget on either side
6988 * (a 'safe' button on the left, and a 'primary' button on the right, both of
6989 * which close the dialog).
6990 * - Displays any action widgets in the footer (none by default).
6991 * - Ability to dismiss errors.
6993 * Subclass responsibilities:
6994 * - Register a 'safe' action.
6995 * - Register a 'primary' action.
6996 * - Add content to the dialog.
7000 * @extends OO.ui.Dialog
7003 * @param {Object} [config] Configuration options
7005 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
7006 // Parent constructor
7007 OO
.ui
.ProcessDialog
.super.call( this, config
);
7010 this.$element
.addClass( 'oo-ui-processDialog' );
7015 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
7020 * Handle dismiss button click events.
7024 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
7029 * Handle retry button click events.
7031 * Hides errors and then tries again.
7033 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
7035 this.executeAction( this.currentAction
.getAction() );
7041 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
7042 if ( this.actions
.isSpecial( action
) ) {
7045 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
7051 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
7053 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
7056 this.$navigation
= this.$( '<div>' );
7057 this.$location
= this.$( '<div>' );
7058 this.$safeActions
= this.$( '<div>' );
7059 this.$primaryActions
= this.$( '<div>' );
7060 this.$otherActions
= this.$( '<div>' );
7061 this.dismissButton
= new OO
.ui
.ButtonWidget( {
7063 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
7065 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
7066 this.$errors
= this.$( '<div>' );
7067 this.$errorsTitle
= this.$( '<div>' );
7070 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
7071 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
7074 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
7076 .append( this.title
.$element
)
7077 .addClass( 'oo-ui-processDialog-location' );
7078 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
7079 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
7080 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
7082 .addClass( 'oo-ui-processDialog-errors-title' )
7083 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
7085 .addClass( 'oo-ui-processDialog-errors' )
7086 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
7088 .addClass( 'oo-ui-processDialog-content' )
7089 .append( this.$errors
);
7091 .addClass( 'oo-ui-processDialog-navigation' )
7092 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
7093 this.$head
.append( this.$navigation
);
7094 this.$foot
.append( this.$otherActions
);
7100 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
7101 var i
, len
, other
, special
, others
;
7104 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
7106 special
= this.actions
.getSpecial();
7107 others
= this.actions
.getOthers();
7108 if ( special
.primary
) {
7109 this.$primaryActions
.append( special
.primary
.$element
);
7110 special
.primary
.toggleFramed( true );
7112 if ( others
.length
) {
7113 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
7114 other
= others
[ i
];
7115 this.$otherActions
.append( other
.$element
);
7116 other
.toggleFramed( true );
7119 if ( special
.safe
) {
7120 this.$safeActions
.append( special
.safe
.$element
);
7121 special
.safe
.toggleFramed( true );
7125 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
7131 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
7132 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
7133 .fail( this.showErrors
.bind( this ) );
7137 * Fit label between actions.
7141 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
7142 var width
= Math
.max(
7143 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
7144 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
7146 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
7152 * Handle errors that occurred during accept or reject processes.
7154 * @param {OO.ui.Error[]} errors Errors to be handled
7156 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
7162 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
7163 if ( !errors
[ i
].isRecoverable() ) {
7164 recoverable
= false;
7166 if ( errors
[ i
].isWarning() ) {
7169 $item
= this.$( '<div>' )
7170 .addClass( 'oo-ui-processDialog-error' )
7171 .append( errors
[ i
].getMessage() );
7172 items
.push( $item
[ 0 ] );
7174 this.$errorItems
= this.$( items
);
7175 if ( recoverable
) {
7176 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
7178 this.currentAction
.setDisabled( true );
7181 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
7183 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
7185 this.retryButton
.toggle( recoverable
);
7186 this.$errorsTitle
.after( this.$errorItems
);
7187 this.$errors
.show().scrollTop( 0 );
7193 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
7194 this.$errors
.hide();
7195 this.$errorItems
.remove();
7196 this.$errorItems
= null;
7200 * Layout containing a series of pages.
7203 * @extends OO.ui.Layout
7206 * @param {Object} [config] Configuration options
7207 * @cfg {boolean} [continuous=false] Show all pages, one after another
7208 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
7209 * @cfg {boolean} [outlined=false] Show an outline
7210 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
7212 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
7213 // Configuration initialization
7214 config
= config
|| {};
7216 // Parent constructor
7217 OO
.ui
.BookletLayout
.super.call( this, config
);
7220 this.currentPageName
= null;
7222 this.ignoreFocus
= false;
7223 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
7224 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
7225 this.outlineVisible
= false;
7226 this.outlined
= !!config
.outlined
;
7227 if ( this.outlined
) {
7228 this.editable
= !!config
.editable
;
7229 this.outlineControlsWidget
= null;
7230 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget( { $: this.$ } );
7231 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
7232 this.gridLayout
= new OO
.ui
.GridLayout(
7233 [ this.outlinePanel
, this.stackLayout
],
7234 { $: this.$, widths
: [ 1, 2 ] }
7236 this.outlineVisible
= true;
7237 if ( this.editable
) {
7238 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
7239 this.outlineSelectWidget
, { $: this.$ }
7245 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
7246 if ( this.outlined
) {
7247 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
7249 if ( this.autoFocus
) {
7250 // Event 'focus' does not bubble, but 'focusin' does
7251 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
7255 this.$element
.addClass( 'oo-ui-bookletLayout' );
7256 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
7257 if ( this.outlined
) {
7258 this.outlinePanel
.$element
7259 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
7260 .append( this.outlineSelectWidget
.$element
);
7261 if ( this.editable
) {
7262 this.outlinePanel
.$element
7263 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
7264 .append( this.outlineControlsWidget
.$element
);
7266 this.$element
.append( this.gridLayout
.$element
);
7268 this.$element
.append( this.stackLayout
.$element
);
7274 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
7280 * @param {OO.ui.PageLayout} page Current page
7285 * @param {OO.ui.PageLayout[]} page Added pages
7286 * @param {number} index Index pages were added at
7291 * @param {OO.ui.PageLayout[]} pages Removed pages
7297 * Handle stack layout focus.
7299 * @param {jQuery.Event} e Focusin event
7301 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
7304 // Find the page that an element was focused within
7305 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
7306 for ( name
in this.pages
) {
7307 // Check for page match, exclude current page to find only page changes
7308 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
7309 this.setPage( name
);
7316 * Handle stack layout set events.
7318 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
7320 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
7323 page
.scrollElementIntoView( { complete: function () {
7324 if ( layout
.autoFocus
) {
7332 * Focus the first input in the current page.
7334 * If no page is selected, the first selectable page will be selected.
7335 * If the focus is already in an element on the current page, nothing will happen.
7337 OO
.ui
.BookletLayout
.prototype.focus = function () {
7338 var $input
, page
= this.stackLayout
.getCurrentItem();
7339 if ( !page
&& this.outlined
) {
7340 this.selectFirstSelectablePage();
7341 page
= this.stackLayout
.getCurrentItem();
7346 // Only change the focus if is not already in the current page
7347 if ( !page
.$element
.find( ':focus' ).length
) {
7348 $input
= page
.$element
.find( ':input:first' );
7349 if ( $input
.length
) {
7350 $input
[ 0 ].focus();
7356 * Handle outline widget select events.
7358 * @param {OO.ui.OptionWidget|null} item Selected item
7360 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
7362 this.setPage( item
.getData() );
7367 * Check if booklet has an outline.
7371 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
7372 return this.outlined
;
7376 * Check if booklet has editing controls.
7380 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
7381 return this.editable
;
7385 * Check if booklet has a visible outline.
7389 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
7390 return this.outlined
&& this.outlineVisible
;
7394 * Hide or show the outline.
7396 * @param {boolean} [show] Show outline, omit to invert current state
7399 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
7400 if ( this.outlined
) {
7401 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
7402 this.outlineVisible
= show
;
7403 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
7410 * Get the outline widget.
7412 * @param {OO.ui.PageLayout} page Page to be selected
7413 * @return {OO.ui.PageLayout|null} Closest page to another
7415 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
7416 var next
, prev
, level
,
7417 pages
= this.stackLayout
.getItems(),
7418 index
= $.inArray( page
, pages
);
7420 if ( index
!== -1 ) {
7421 next
= pages
[ index
+ 1 ];
7422 prev
= pages
[ index
- 1 ];
7423 // Prefer adjacent pages at the same level
7424 if ( this.outlined
) {
7425 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
7428 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
7434 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
7440 return prev
|| next
|| null;
7444 * Get the outline widget.
7446 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if booklet has no outline
7448 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
7449 return this.outlineSelectWidget
;
7453 * Get the outline controls widget. If the outline is not editable, null is returned.
7455 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
7457 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
7458 return this.outlineControlsWidget
;
7462 * Get a page by name.
7464 * @param {string} name Symbolic name of page
7465 * @return {OO.ui.PageLayout|undefined} Page, if found
7467 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
7468 return this.pages
[ name
];
7472 * Get the current page name.
7474 * @return {string|null} Current page name
7476 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
7477 return this.currentPageName
;
7481 * Add a page to the layout.
7483 * When pages are added with the same names as existing pages, the existing pages will be
7484 * automatically removed before the new pages are added.
7486 * @param {OO.ui.PageLayout[]} pages Pages to add
7487 * @param {number} index Index to insert pages after
7491 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
7492 var i
, len
, name
, page
, item
, currentIndex
,
7493 stackLayoutPages
= this.stackLayout
.getItems(),
7497 // Remove pages with same names
7498 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7500 name
= page
.getName();
7502 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
7503 // Correct the insertion index
7504 currentIndex
= $.inArray( this.pages
[ name
], stackLayoutPages
);
7505 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
7508 remove
.push( this.pages
[ name
] );
7511 if ( remove
.length
) {
7512 this.removePages( remove
);
7516 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7518 name
= page
.getName();
7519 this.pages
[ page
.getName() ] = page
;
7520 if ( this.outlined
) {
7521 item
= new OO
.ui
.OutlineOptionWidget( { $: this.$, data
: name
} );
7522 page
.setOutlineItem( item
);
7527 if ( this.outlined
&& items
.length
) {
7528 this.outlineSelectWidget
.addItems( items
, index
);
7529 this.selectFirstSelectablePage();
7531 this.stackLayout
.addItems( pages
, index
);
7532 this.emit( 'add', pages
, index
);
7538 * Remove a page from the layout.
7543 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
7544 var i
, len
, name
, page
,
7547 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7549 name
= page
.getName();
7550 delete this.pages
[ name
];
7551 if ( this.outlined
) {
7552 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
7553 page
.setOutlineItem( null );
7556 if ( this.outlined
&& items
.length
) {
7557 this.outlineSelectWidget
.removeItems( items
);
7558 this.selectFirstSelectablePage();
7560 this.stackLayout
.removeItems( pages
);
7561 this.emit( 'remove', pages
);
7567 * Clear all pages from the layout.
7572 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
7574 pages
= this.stackLayout
.getItems();
7577 this.currentPageName
= null;
7578 if ( this.outlined
) {
7579 this.outlineSelectWidget
.clearItems();
7580 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7581 pages
[ i
].setOutlineItem( null );
7584 this.stackLayout
.clearItems();
7586 this.emit( 'remove', pages
);
7592 * Set the current page by name.
7595 * @param {string} name Symbolic name of page
7597 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
7600 page
= this.pages
[ name
];
7602 if ( name
!== this.currentPageName
) {
7603 if ( this.outlined
) {
7604 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
7605 if ( selectedItem
&& selectedItem
.getData() !== name
) {
7606 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getItemFromData( name
) );
7610 if ( this.currentPageName
&& this.pages
[ this.currentPageName
] ) {
7611 this.pages
[ this.currentPageName
].setActive( false );
7612 // Blur anything focused if the next page doesn't have anything focusable - this
7613 // is not needed if the next page has something focusable because once it is focused
7614 // this blur happens automatically
7615 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
7616 $focused
= this.pages
[ this.currentPageName
].$element
.find( ':focus' );
7617 if ( $focused
.length
) {
7618 $focused
[ 0 ].blur();
7622 this.currentPageName
= name
;
7623 this.stackLayout
.setItem( page
);
7624 page
.setActive( true );
7625 this.emit( 'set', page
);
7631 * Select the first selectable page.
7635 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
7636 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
7637 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
7644 * Layout made of a field and optional label.
7646 * Available label alignment modes include:
7647 * - left: Label is before the field and aligned away from it, best for when the user will be
7648 * scanning for a specific label in a form with many fields
7649 * - right: Label is before the field and aligned toward it, best for forms the user is very
7650 * familiar with and will tab through field checking quickly to verify which field they are in
7651 * - top: Label is before the field and above it, best for when the user will need to fill out all
7652 * fields from top to bottom in a form with few fields
7653 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
7654 * checkboxes or radio buttons
7657 * @extends OO.ui.Layout
7658 * @mixins OO.ui.LabelElement
7661 * @param {OO.ui.Widget} fieldWidget Field widget
7662 * @param {Object} [config] Configuration options
7663 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
7664 * @cfg {string} [help] Explanatory text shown as a '?' icon.
7666 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
7667 var hasInputWidget
= fieldWidget
instanceof OO
.ui
.InputWidget
;
7669 // Configuration initialization
7670 config
= $.extend( { align
: 'left' }, config
);
7672 // Properties (must be set before parent constructor, which calls #getTagName)
7673 this.fieldWidget
= fieldWidget
;
7675 // Parent constructor
7676 OO
.ui
.FieldLayout
.super.call( this, config
);
7678 // Mixin constructors
7679 OO
.ui
.LabelElement
.call( this, config
);
7682 this.$field
= this.$( '<div>' );
7683 this.$body
= this.$( '<' + ( hasInputWidget
? 'label' : 'div' ) + '>' );
7685 if ( config
.help
) {
7686 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
7688 classes
: [ 'oo-ui-fieldLayout-help' ],
7693 this.popupButtonWidget
.getPopup().$body
.append(
7695 .text( config
.help
)
7696 .addClass( 'oo-ui-fieldLayout-help-content' )
7698 this.$help
= this.popupButtonWidget
.$element
;
7700 this.$help
= this.$( [] );
7704 if ( hasInputWidget
) {
7705 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
7707 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
7711 .addClass( 'oo-ui-fieldLayout' )
7712 .append( this.$help
, this.$body
);
7713 this.$body
.addClass( 'oo-ui-fieldLayout-body' );
7715 .addClass( 'oo-ui-fieldLayout-field' )
7716 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
7717 .append( this.fieldWidget
.$element
);
7719 this.setAlignment( config
.align
);
7724 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
7725 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
7730 * Handle field disable events.
7732 * @param {boolean} value Field is disabled
7734 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
7735 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
7739 * Handle label mouse click events.
7741 * @param {jQuery.Event} e Mouse click event
7743 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
7744 this.fieldWidget
.simulateLabelClick();
7751 * @return {OO.ui.Widget} Field widget
7753 OO
.ui
.FieldLayout
.prototype.getField = function () {
7754 return this.fieldWidget
;
7758 * Set the field alignment mode.
7761 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
7764 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
7765 if ( value
!== this.align
) {
7766 // Default to 'left'
7767 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
7771 if ( value
=== 'inline' ) {
7772 this.$body
.append( this.$field
, this.$label
);
7774 this.$body
.append( this.$label
, this.$field
);
7776 // Set classes. The following classes can be used here:
7777 // * oo-ui-fieldLayout-align-left
7778 // * oo-ui-fieldLayout-align-right
7779 // * oo-ui-fieldLayout-align-top
7780 // * oo-ui-fieldLayout-align-inline
7782 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
7784 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
7792 * Layout made of a field, a button, and an optional label.
7795 * @extends OO.ui.FieldLayout
7798 * @param {OO.ui.Widget} fieldWidget Field widget
7799 * @param {OO.ui.ButtonWidget} buttonWidget Button widget
7800 * @param {Object} [config] Configuration options
7801 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
7802 * @cfg {string} [help] Explanatory text shown as a '?' icon.
7804 OO
.ui
.ActionFieldLayout
= function OoUiActionFieldLayout( fieldWidget
, buttonWidget
, config
) {
7805 // Configuration initialization
7806 config
= $.extend( { align
: 'left' }, config
);
7808 // Properties (must be set before parent constructor, which calls #getTagName)
7809 this.fieldWidget
= fieldWidget
;
7810 this.buttonWidget
= buttonWidget
;
7812 // Parent constructor
7813 OO
.ui
.ActionFieldLayout
.super.call( this, fieldWidget
, config
);
7815 // Mixin constructors
7816 OO
.ui
.LabelElement
.call( this, config
);
7819 this.$button
= this.$( '<div>' )
7820 .addClass( 'oo-ui-actionFieldLayout-button' )
7821 .append( this.buttonWidget
.$element
);
7823 this.$input
= this.$( '<div>' )
7824 .addClass( 'oo-ui-actionFieldLayout-input' )
7825 .append( this.fieldWidget
.$element
);
7828 .addClass( 'oo-ui-actionFieldLayout' )
7829 .append( this.$input
, this.$button
);
7834 OO
.inheritClass( OO
.ui
.ActionFieldLayout
, OO
.ui
.FieldLayout
);
7837 * Layout made of a fieldset and optional legend.
7839 * Just add OO.ui.FieldLayout items.
7842 * @extends OO.ui.Layout
7843 * @mixins OO.ui.IconElement
7844 * @mixins OO.ui.LabelElement
7845 * @mixins OO.ui.GroupElement
7848 * @param {Object} [config] Configuration options
7849 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
7851 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
7852 // Configuration initialization
7853 config
= config
|| {};
7855 // Parent constructor
7856 OO
.ui
.FieldsetLayout
.super.call( this, config
);
7858 // Mixin constructors
7859 OO
.ui
.IconElement
.call( this, config
);
7860 OO
.ui
.LabelElement
.call( this, config
);
7861 OO
.ui
.GroupElement
.call( this, config
);
7863 if ( config
.help
) {
7864 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
7866 classes
: [ 'oo-ui-fieldsetLayout-help' ],
7871 this.popupButtonWidget
.getPopup().$body
.append(
7873 .text( config
.help
)
7874 .addClass( 'oo-ui-fieldsetLayout-help-content' )
7876 this.$help
= this.popupButtonWidget
.$element
;
7878 this.$help
= this.$( [] );
7883 .addClass( 'oo-ui-fieldsetLayout' )
7884 .prepend( this.$help
, this.$icon
, this.$label
, this.$group
);
7885 if ( $.isArray( config
.items
) ) {
7886 this.addItems( config
.items
);
7892 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
7893 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
7894 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
7895 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
7898 * Layout with an HTML form.
7901 * @extends OO.ui.Layout
7904 * @param {Object} [config] Configuration options
7905 * @cfg {string} [method] HTML form `method` attribute
7906 * @cfg {string} [action] HTML form `action` attribute
7907 * @cfg {string} [enctype] HTML form `enctype` attribute
7909 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
7910 // Configuration initialization
7911 config
= config
|| {};
7913 // Parent constructor
7914 OO
.ui
.FormLayout
.super.call( this, config
);
7917 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
7921 .addClass( 'oo-ui-formLayout' )
7923 method
: config
.method
,
7924 action
: config
.action
,
7925 enctype
: config
.enctype
7931 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
7939 /* Static Properties */
7941 OO
.ui
.FormLayout
.static.tagName
= 'form';
7946 * Handle form submit events.
7948 * @param {jQuery.Event} e Submit event
7951 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
7952 this.emit( 'submit' );
7957 * Layout made of proportionally sized columns and rows.
7960 * @extends OO.ui.Layout
7963 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
7964 * @param {Object} [config] Configuration options
7965 * @cfg {number[]} [widths] Widths of columns as ratios
7966 * @cfg {number[]} [heights] Heights of rows as ratios
7968 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
7971 // Configuration initialization
7972 config
= config
|| {};
7974 // Parent constructor
7975 OO
.ui
.GridLayout
.super.call( this, config
);
7983 this.$element
.addClass( 'oo-ui-gridLayout' );
7984 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
7985 this.panels
.push( panels
[ i
] );
7986 this.$element
.append( panels
[ i
].$element
);
7988 if ( config
.widths
|| config
.heights
) {
7989 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
7991 // Arrange in columns by default
7992 widths
= this.panels
.map( function () { return 1; } );
7993 this.layout( widths
, [ 1 ] );
7999 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
8014 * Set grid dimensions.
8016 * @param {number[]} widths Widths of columns as ratios
8017 * @param {number[]} heights Heights of rows as ratios
8019 * @throws {Error} If grid is not large enough to fit all panels
8021 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
8025 cols
= widths
.length
,
8026 rows
= heights
.length
;
8028 // Verify grid is big enough to fit panels
8029 if ( cols
* rows
< this.panels
.length
) {
8030 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
8033 // Sum up denominators
8034 for ( x
= 0; x
< cols
; x
++ ) {
8037 for ( y
= 0; y
< rows
; y
++ ) {
8043 for ( x
= 0; x
< cols
; x
++ ) {
8044 this.widths
[ x
] = widths
[ x
] / xd
;
8046 for ( y
= 0; y
< rows
; y
++ ) {
8047 this.heights
[ y
] = heights
[ y
] / yd
;
8051 this.emit( 'layout' );
8055 * Update panel positions and sizes.
8059 OO
.ui
.GridLayout
.prototype.update = function () {
8060 var x
, y
, panel
, width
, height
, dimensions
,
8064 cols
= this.widths
.length
,
8065 rows
= this.heights
.length
;
8067 for ( y
= 0; y
< rows
; y
++ ) {
8068 height
= this.heights
[ y
];
8069 for ( x
= 0; x
< cols
; x
++ ) {
8070 width
= this.widths
[ x
];
8071 panel
= this.panels
[ i
];
8073 width
: ( width
* 100 ) + '%',
8074 height
: ( height
* 100 ) + '%',
8075 top
: ( top
* 100 ) + '%'
8078 if ( OO
.ui
.Element
.static.getDir( this.$.context
) === 'rtl' ) {
8079 dimensions
.right
= ( left
* 100 ) + '%';
8081 dimensions
.left
= ( left
* 100 ) + '%';
8083 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
8084 if ( width
=== 0 || height
=== 0 ) {
8085 dimensions
.visibility
= 'hidden';
8087 dimensions
.visibility
= '';
8089 panel
.$element
.css( dimensions
);
8097 this.emit( 'update' );
8101 * Get a panel at a given position.
8103 * The x and y position is affected by the current grid layout.
8105 * @param {number} x Horizontal position
8106 * @param {number} y Vertical position
8107 * @return {OO.ui.PanelLayout} The panel at the given position
8109 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
8110 return this.panels
[ ( x
* this.widths
.length
) + y
];
8114 * Layout with a content and menu area.
8116 * The menu area can be positioned at the top, after, bottom or before. The content area will fill
8117 * all remaining space.
8120 * @extends OO.ui.Layout
8123 * @param {Object} [config] Configuration options
8124 * @cfg {number|string} [menuSize='18em'] Size of menu in pixels or any CSS unit
8125 * @cfg {boolean} [showMenu=true] Show menu
8126 * @cfg {string} [position='before'] Position of menu, either `top`, `after`, `bottom` or `before`
8127 * @cfg {boolean} [collapse] Collapse the menu out of view
8129 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
8130 var positions
= this.constructor.static.menuPositions
;
8132 // Configuration initialization
8133 config
= config
|| {};
8135 // Parent constructor
8136 OO
.ui
.MenuLayout
.super.call( this, config
);
8139 this.showMenu
= config
.showMenu
!== false;
8140 this.menuSize
= config
.menuSize
|| '18em';
8141 this.menuPosition
= positions
[ config
.menuPosition
] || positions
.before
;
8146 * @property {jQuery}
8148 this.$menu
= this.$( '<div>' );
8152 * @property {jQuery}
8154 this.$content
= this.$( '<div>' );
8157 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
8160 this.toggleMenu( this.showMenu
);
8162 .addClass( 'oo-ui-menuLayout-menu' )
8163 .css( this.menuPosition
.sizeProperty
, this.menuSize
);
8164 this.$content
.addClass( 'oo-ui-menuLayout-content' );
8166 .addClass( 'oo-ui-menuLayout ' + this.menuPosition
.className
)
8167 .append( this.$content
, this.$menu
);
8172 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
8174 /* Static Properties */
8176 OO
.ui
.MenuLayout
.static.menuPositions
= {
8178 sizeProperty
: 'height',
8179 positionProperty
: 'top',
8180 className
: 'oo-ui-menuLayout-top'
8183 sizeProperty
: 'width',
8184 positionProperty
: 'right',
8185 rtlPositionProperty
: 'left',
8186 className
: 'oo-ui-menuLayout-after'
8189 sizeProperty
: 'height',
8190 positionProperty
: 'bottom',
8191 className
: 'oo-ui-menuLayout-bottom'
8194 sizeProperty
: 'width',
8195 positionProperty
: 'left',
8196 rtlPositionProperty
: 'right',
8197 className
: 'oo-ui-menuLayout-before'
8204 * Handle DOM attachment events
8206 OO
.ui
.MenuLayout
.prototype.onElementAttach = function () {
8207 // getPositionProperty won't know about directionality until the layout is attached
8208 if ( this.showMenu
) {
8209 this.$content
.css( this.getPositionProperty(), this.menuSize
);
8216 * @param {boolean} showMenu Show menu, omit to toggle
8219 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
8220 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
8222 if ( this.showMenu
!== showMenu
) {
8223 this.showMenu
= showMenu
;
8231 * Check if menu is visible
8233 * @return {boolean} Menu is visible
8235 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
8236 return this.showMenu
;
8242 * @param {number|string} size Size of menu in pixels or any CSS unit
8245 OO
.ui
.MenuLayout
.prototype.setMenuSize = function ( size
) {
8246 this.menuSize
= size
;
8253 * Update menu and content CSS based on current menu size and visibility
8255 OO
.ui
.MenuLayout
.prototype.updateSizes = function () {
8256 if ( this.showMenu
) {
8258 .css( this.menuPosition
.sizeProperty
, this.menuSize
)
8259 .css( 'overflow', '' );
8260 this.$content
.css( this.getPositionProperty(), this.menuSize
);
8263 .css( this.menuPosition
.sizeProperty
, 0 )
8264 .css( 'overflow', 'hidden' );
8265 this.$content
.css( this.getPositionProperty(), 0 );
8272 * @return {number|string} Menu size
8274 OO
.ui
.MenuLayout
.prototype.getMenuSize = function () {
8275 return this.menuSize
;
8279 * Set menu position.
8281 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
8282 * @throws {Error} If position value is not supported
8285 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
8286 var positionProperty
, positions
= this.constructor.static.menuPositions
;
8288 if ( !positions
[ position
] ) {
8289 throw new Error( 'Cannot set position; unsupported position value: ' + position
);
8292 positionProperty
= this.getPositionProperty();
8293 this.$menu
.css( this.menuPosition
.sizeProperty
, '' );
8294 this.$content
.css( positionProperty
, '' );
8295 this.$element
.removeClass( this.menuPosition
.className
);
8297 this.menuPosition
= positions
[ position
];
8300 this.$element
.addClass( this.menuPosition
.className
);
8306 * Get menu position.
8308 * @return {string} Menu position
8310 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
8311 return this.menuPosition
;
8315 * Get the menu position property.
8317 * @return {string} Menu position CSS property
8319 OO
.ui
.MenuLayout
.prototype.getPositionProperty = function () {
8320 if ( this.menuPosition
.rtlPositionProperty
&& this.$element
.css( 'direction' ) === 'rtl' ) {
8321 return this.menuPosition
.rtlPositionProperty
;
8323 return this.menuPosition
.positionProperty
;
8328 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
8331 * @extends OO.ui.Layout
8334 * @param {Object} [config] Configuration options
8335 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
8336 * @cfg {boolean} [padded=false] Pad the content from the edges
8337 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
8339 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
8340 // Configuration initialization
8341 config
= $.extend( {
8347 // Parent constructor
8348 OO
.ui
.PanelLayout
.super.call( this, config
);
8351 this.$element
.addClass( 'oo-ui-panelLayout' );
8352 if ( config
.scrollable
) {
8353 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
8355 if ( config
.padded
) {
8356 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
8358 if ( config
.expanded
) {
8359 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
8365 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
8368 * Page within an booklet layout.
8371 * @extends OO.ui.PanelLayout
8374 * @param {string} name Unique symbolic name of page
8375 * @param {Object} [config] Configuration options
8377 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
8378 // Configuration initialization
8379 config
= $.extend( { scrollable
: true }, config
);
8381 // Parent constructor
8382 OO
.ui
.PageLayout
.super.call( this, config
);
8386 this.outlineItem
= null;
8387 this.active
= false;
8390 this.$element
.addClass( 'oo-ui-pageLayout' );
8395 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
8401 * @param {boolean} active Page is active
8409 * @return {string} Symbolic name of page
8411 OO
.ui
.PageLayout
.prototype.getName = function () {
8416 * Check if page is active.
8418 * @return {boolean} Page is active
8420 OO
.ui
.PageLayout
.prototype.isActive = function () {
8427 * @return {OO.ui.OutlineOptionWidget|null} Outline item widget
8429 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
8430 return this.outlineItem
;
8436 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
8437 * outline item as desired; this method is called for setting (with an object) and unsetting
8438 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
8439 * operating on null instead of an OO.ui.OutlineOptionWidget object.
8441 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline item widget, null to clear
8444 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
8445 this.outlineItem
= outlineItem
|| null;
8446 if ( outlineItem
) {
8447 this.setupOutlineItem();
8453 * Setup outline item.
8455 * @localdoc Subclasses should override this method to adjust the outline item as desired.
8457 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline item widget to setup
8460 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
8465 * Set page active state.
8467 * @param {boolean} Page is active
8470 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
8473 if ( active
!== this.active
) {
8474 this.active
= active
;
8475 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
8476 this.emit( 'active', this.active
);
8481 * Layout containing a series of mutually exclusive pages.
8484 * @extends OO.ui.PanelLayout
8485 * @mixins OO.ui.GroupElement
8488 * @param {Object} [config] Configuration options
8489 * @cfg {boolean} [continuous=false] Show all pages, one after another
8490 * @cfg {OO.ui.Layout[]} [items] Layouts to add
8492 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
8493 // Configuration initialization
8494 config
= $.extend( { scrollable
: true }, config
);
8496 // Parent constructor
8497 OO
.ui
.StackLayout
.super.call( this, config
);
8499 // Mixin constructors
8500 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8503 this.currentItem
= null;
8504 this.continuous
= !!config
.continuous
;
8507 this.$element
.addClass( 'oo-ui-stackLayout' );
8508 if ( this.continuous
) {
8509 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
8511 if ( $.isArray( config
.items
) ) {
8512 this.addItems( config
.items
);
8518 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
8519 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
8525 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
8531 * Get the current item.
8533 * @return {OO.ui.Layout|null}
8535 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
8536 return this.currentItem
;
8540 * Unset the current item.
8543 * @param {OO.ui.StackLayout} layout
8546 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
8547 var prevItem
= this.currentItem
;
8548 if ( prevItem
=== null ) {
8552 this.currentItem
= null;
8553 this.emit( 'set', null );
8559 * Adding an existing item (by value) will move it.
8561 * @param {OO.ui.Layout[]} items Items to add
8562 * @param {number} [index] Index to insert items after
8565 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
8567 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
8569 if ( !this.currentItem
&& items
.length
) {
8570 this.setItem( items
[ 0 ] );
8579 * Items will be detached, not removed, so they can be used later.
8581 * @param {OO.ui.Layout[]} items Items to remove
8585 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
8587 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
8589 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
8590 if ( this.items
.length
) {
8591 this.setItem( this.items
[ 0 ] );
8593 this.unsetCurrentItem();
8603 * Items will be detached, not removed, so they can be used later.
8608 OO
.ui
.StackLayout
.prototype.clearItems = function () {
8609 this.unsetCurrentItem();
8610 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
8618 * Any currently shown item will be hidden.
8620 * FIXME: If the passed item to show has not been added in the items list, then
8621 * this method drops it and unsets the current item.
8623 * @param {OO.ui.Layout} item Item to show
8627 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
8630 if ( item
!== this.currentItem
) {
8631 if ( !this.continuous
) {
8632 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
8633 this.items
[ i
].$element
.css( 'display', '' );
8636 if ( $.inArray( item
, this.items
) !== -1 ) {
8637 if ( !this.continuous
) {
8638 item
.$element
.css( 'display', 'block' );
8640 this.currentItem
= item
;
8641 this.emit( 'set', item
);
8643 this.unsetCurrentItem();
8651 * Horizontal bar layout of tools as icon buttons.
8654 * @extends OO.ui.ToolGroup
8657 * @param {OO.ui.Toolbar} toolbar
8658 * @param {Object} [config] Configuration options
8660 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
8661 // Parent constructor
8662 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
8665 this.$element
.addClass( 'oo-ui-barToolGroup' );
8670 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
8672 /* Static Properties */
8674 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
8676 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
8678 OO
.ui
.BarToolGroup
.static.name
= 'bar';
8681 * Popup list of tools with an icon and optional label.
8685 * @extends OO.ui.ToolGroup
8686 * @mixins OO.ui.IconElement
8687 * @mixins OO.ui.IndicatorElement
8688 * @mixins OO.ui.LabelElement
8689 * @mixins OO.ui.TitledElement
8690 * @mixins OO.ui.ClippableElement
8693 * @param {OO.ui.Toolbar} toolbar
8694 * @param {Object} [config] Configuration options
8695 * @cfg {string} [header] Text to display at the top of the pop-up
8697 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
8698 // Configuration initialization
8699 config
= config
|| {};
8701 // Parent constructor
8702 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
8704 // Mixin constructors
8705 OO
.ui
.IconElement
.call( this, config
);
8706 OO
.ui
.IndicatorElement
.call( this, config
);
8707 OO
.ui
.LabelElement
.call( this, config
);
8708 OO
.ui
.TitledElement
.call( this, config
);
8709 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
8712 this.active
= false;
8713 this.dragging
= false;
8714 this.onBlurHandler
= this.onBlur
.bind( this );
8715 this.$handle
= this.$( '<span>' );
8719 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
8720 'mouseup touchend': this.onHandlePointerUp
.bind( this )
8725 .addClass( 'oo-ui-popupToolGroup-handle' )
8726 .append( this.$icon
, this.$label
, this.$indicator
);
8727 // If the pop-up should have a header, add it to the top of the toolGroup.
8728 // Note: If this feature is useful for other widgets, we could abstract it into an
8729 // OO.ui.HeaderedElement mixin constructor.
8730 if ( config
.header
!== undefined ) {
8732 .prepend( this.$( '<span>' )
8733 .addClass( 'oo-ui-popupToolGroup-header' )
8734 .text( config
.header
)
8738 .addClass( 'oo-ui-popupToolGroup' )
8739 .prepend( this.$handle
);
8744 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
8745 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
8746 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
8747 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
8748 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
8749 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
8751 /* Static Properties */
8758 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
8760 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
8762 if ( this.isDisabled() && this.isElementAttached() ) {
8763 this.setActive( false );
8768 * Handle focus being lost.
8770 * The event is actually generated from a mouseup, so it is not a normal blur event object.
8772 * @param {jQuery.Event} e Mouse up event
8774 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
8775 // Only deactivate when clicking outside the dropdown element
8776 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[ 0 ] !== this.$element
[ 0 ] ) {
8777 this.setActive( false );
8784 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
8785 // e.which is 0 for touch events, 1 for left mouse button
8786 // Only close toolgroup when a tool was actually selected
8787 // FIXME: this duplicates logic from the parent class
8788 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== this.getTargetTool( e
) ) {
8789 this.setActive( false );
8791 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
8795 * Handle mouse up events.
8797 * @param {jQuery.Event} e Mouse up event
8799 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
8804 * Handle mouse down events.
8806 * @param {jQuery.Event} e Mouse down event
8808 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
8809 // e.which is 0 for touch events, 1 for left mouse button
8810 if ( !this.isDisabled() && e
.which
<= 1 ) {
8811 this.setActive( !this.active
);
8817 * Switch into active mode.
8819 * When active, mouseup events anywhere in the document will trigger deactivation.
8821 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
8823 if ( this.active
!== value
) {
8824 this.active
= value
;
8826 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
8828 // Try anchoring the popup to the left first
8829 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
8830 this.toggleClipping( true );
8831 if ( this.isClippedHorizontally() ) {
8832 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
8833 this.toggleClipping( false );
8835 .removeClass( 'oo-ui-popupToolGroup-left' )
8836 .addClass( 'oo-ui-popupToolGroup-right' );
8837 this.toggleClipping( true );
8840 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
8841 this.$element
.removeClass(
8842 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
8844 this.toggleClipping( false );
8850 * Drop down list layout of tools as labeled icon buttons.
8852 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
8853 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
8854 * may want to use the 'promote' and 'demote' configuration options to achieve this.
8857 * @extends OO.ui.PopupToolGroup
8860 * @param {OO.ui.Toolbar} toolbar
8861 * @param {Object} [config] Configuration options
8862 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
8864 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
8865 * allowed to be collapsed.
8866 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
8868 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
8869 // Configuration initialization
8870 config
= config
|| {};
8872 // Properties (must be set before parent constructor, which calls #populate)
8873 this.allowCollapse
= config
.allowCollapse
;
8874 this.forceExpand
= config
.forceExpand
;
8875 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
8876 this.collapsibleTools
= [];
8878 // Parent constructor
8879 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
8882 this.$element
.addClass( 'oo-ui-listToolGroup' );
8887 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
8889 /* Static Properties */
8891 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
8893 OO
.ui
.ListToolGroup
.static.name
= 'list';
8900 OO
.ui
.ListToolGroup
.prototype.populate = function () {
8901 var i
, len
, allowCollapse
= [];
8903 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
8905 // Update the list of collapsible tools
8906 if ( this.allowCollapse
!== undefined ) {
8907 allowCollapse
= this.allowCollapse
;
8908 } else if ( this.forceExpand
!== undefined ) {
8909 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
8912 this.collapsibleTools
= [];
8913 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
8914 if ( this.tools
[ allowCollapse
[ i
] ] !== undefined ) {
8915 this.collapsibleTools
.push( this.tools
[ allowCollapse
[ i
] ] );
8919 // Keep at the end, even when tools are added
8920 this.$group
.append( this.getExpandCollapseTool().$element
);
8922 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
8924 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
8925 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
8926 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
8927 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
8928 this.getExpandCollapseTool().$element
.css( 'display', 'block' );
8931 this.updateCollapsibleState();
8934 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
8935 if ( this.expandCollapseTool
=== undefined ) {
8936 var ExpandCollapseTool = function () {
8937 ExpandCollapseTool
.super.apply( this, arguments
);
8940 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
8942 ExpandCollapseTool
.prototype.onSelect = function () {
8943 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
8944 this.toolGroup
.updateCollapsibleState();
8945 this.setActive( false );
8947 ExpandCollapseTool
.prototype.onUpdateState = function () {
8948 // Do nothing. Tool interface requires an implementation of this function.
8951 ExpandCollapseTool
.static.name
= 'more-fewer';
8953 this.expandCollapseTool
= new ExpandCollapseTool( this );
8955 return this.expandCollapseTool
;
8961 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
8962 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
8964 // Do not close the popup when the user wants to show more/fewer tools
8965 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
8966 // Prevent the popup list from being hidden
8967 this.setActive( true );
8973 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
8976 this.getExpandCollapseTool()
8977 .setIcon( this.expanded
? 'collapse' : 'expand' )
8978 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
8980 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
8981 this.collapsibleTools
[ i
].toggle( this.expanded
);
8986 * Drop down menu layout of tools as selectable menu items.
8989 * @extends OO.ui.PopupToolGroup
8992 * @param {OO.ui.Toolbar} toolbar
8993 * @param {Object} [config] Configuration options
8995 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
8996 // Configuration initialization
8997 config
= config
|| {};
8999 // Parent constructor
9000 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
9003 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
9006 this.$element
.addClass( 'oo-ui-menuToolGroup' );
9011 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
9013 /* Static Properties */
9015 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
9017 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
9022 * Handle the toolbar state being updated.
9024 * When the state changes, the title of each active item in the menu will be joined together and
9025 * used as a label for the group. The label will be empty if none of the items are active.
9027 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
9031 for ( name
in this.tools
) {
9032 if ( this.tools
[ name
].isActive() ) {
9033 labelTexts
.push( this.tools
[ name
].getTitle() );
9037 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
9041 * Tool that shows a popup when selected.
9045 * @extends OO.ui.Tool
9046 * @mixins OO.ui.PopupElement
9049 * @param {OO.ui.Toolbar} toolbar
9050 * @param {Object} [config] Configuration options
9052 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
9053 // Parent constructor
9054 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
9056 // Mixin constructors
9057 OO
.ui
.PopupElement
.call( this, config
);
9061 .addClass( 'oo-ui-popupTool' )
9062 .append( this.popup
.$element
);
9067 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
9068 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
9073 * Handle the tool being selected.
9077 OO
.ui
.PopupTool
.prototype.onSelect = function () {
9078 if ( !this.isDisabled() ) {
9079 this.popup
.toggle();
9081 this.setActive( false );
9086 * Handle the toolbar state being updated.
9090 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
9091 this.setActive( false );
9095 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
9097 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
9101 * @extends OO.ui.GroupElement
9104 * @param {Object} [config] Configuration options
9106 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
9107 // Parent constructor
9108 OO
.ui
.GroupWidget
.super.call( this, config
);
9113 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
9118 * Set the disabled state of the widget.
9120 * This will also update the disabled state of child widgets.
9122 * @param {boolean} disabled Disable widget
9125 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
9129 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
9130 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
9132 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
9134 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9135 this.items
[ i
].updateDisabled();
9143 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
9145 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
9146 * allows bidirectional communication.
9148 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
9155 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
9162 * Check if widget is disabled.
9164 * Checks parent if present, making disabled state inheritable.
9166 * @return {boolean} Widget is disabled
9168 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
9169 return this.disabled
||
9170 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
9174 * Set group element is in.
9176 * @param {OO.ui.GroupElement|null} group Group element, null if none
9179 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
9181 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
9182 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
9184 // Initialize item disabled states
9185 this.updateDisabled();
9191 * Mixin that adds a menu showing suggested values for a text input.
9193 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
9195 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
9196 * be aware that this will cause new suggestions to be looked up for the new value. If this is
9197 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
9201 * @deprecated Use LookupElement instead.
9204 * @param {OO.ui.TextInputWidget} input Input widget
9205 * @param {Object} [config] Configuration options
9206 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
9207 * @cfg {jQuery} [$container=input.$element] Element to render menu under
9209 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
9210 // Configuration initialization
9211 config
= config
|| {};
9214 this.lookupInput
= input
;
9215 this.$overlay
= config
.$overlay
|| this.$element
;
9216 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
9217 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
9218 input
: this.lookupInput
,
9219 $container
: config
.$container
9221 this.lookupCache
= {};
9222 this.lookupQuery
= null;
9223 this.lookupRequest
= null;
9224 this.lookupsDisabled
= false;
9225 this.lookupInputFocused
= false;
9228 this.lookupInput
.$input
.on( {
9229 focus
: this.onLookupInputFocus
.bind( this ),
9230 blur
: this.onLookupInputBlur
.bind( this ),
9231 mousedown
: this.onLookupInputMouseDown
.bind( this )
9233 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
9234 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
9237 this.$element
.addClass( 'oo-ui-lookupWidget' );
9238 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
9239 this.$overlay
.append( this.lookupMenu
.$element
);
9245 * Handle input focus event.
9247 * @param {jQuery.Event} e Input focus event
9249 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
9250 this.lookupInputFocused
= true;
9251 this.populateLookupMenu();
9255 * Handle input blur event.
9257 * @param {jQuery.Event} e Input blur event
9259 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
9260 this.closeLookupMenu();
9261 this.lookupInputFocused
= false;
9265 * Handle input mouse down event.
9267 * @param {jQuery.Event} e Input mouse down event
9269 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
9270 // Only open the menu if the input was already focused.
9271 // This way we allow the user to open the menu again after closing it with Esc
9272 // by clicking in the input. Opening (and populating) the menu when initially
9273 // clicking into the input is handled by the focus handler.
9274 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
9275 this.populateLookupMenu();
9280 * Handle input change event.
9282 * @param {string} value New input value
9284 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
9285 if ( this.lookupInputFocused
) {
9286 this.populateLookupMenu();
9291 * Handle the lookup menu being shown/hidden.
9292 * @param {boolean} visible Whether the lookup menu is now visible.
9294 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
9296 // When the menu is hidden, abort any active request and clear the menu.
9297 // This has to be done here in addition to closeLookupMenu(), because
9298 // MenuSelectWidget will close itself when the user presses Esc.
9299 this.abortLookupRequest();
9300 this.lookupMenu
.clearItems();
9307 * @return {OO.ui.TextInputMenuSelectWidget}
9309 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
9310 return this.lookupMenu
;
9314 * Disable or re-enable lookups.
9316 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
9318 * @param {boolean} disabled Disable lookups
9320 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
9321 this.lookupsDisabled
= !!disabled
;
9325 * Open the menu. If there are no entries in the menu, this does nothing.
9329 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
9330 if ( !this.lookupMenu
.isEmpty() ) {
9331 this.lookupMenu
.toggle( true );
9337 * Close the menu, empty it, and abort any pending request.
9341 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
9342 this.lookupMenu
.toggle( false );
9343 this.abortLookupRequest();
9344 this.lookupMenu
.clearItems();
9349 * Request menu items based on the input's current value, and when they arrive,
9350 * populate the menu with these items and show the menu.
9352 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
9356 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
9358 value
= this.lookupInput
.getValue();
9360 if ( this.lookupsDisabled
) {
9364 // If the input is empty, clear the menu
9365 if ( value
=== '' ) {
9366 this.closeLookupMenu();
9367 // Skip population if there is already a request pending for the current value
9368 } else if ( value
!== this.lookupQuery
) {
9369 this.getLookupMenuItems()
9370 .done( function ( items
) {
9371 widget
.lookupMenu
.clearItems();
9372 if ( items
.length
) {
9376 widget
.initializeLookupMenuSelection();
9378 widget
.lookupMenu
.toggle( false );
9381 .fail( function () {
9382 widget
.lookupMenu
.clearItems();
9390 * Select and highlight the first selectable item in the menu.
9394 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
9395 if ( !this.lookupMenu
.getSelectedItem() ) {
9396 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
9398 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
9402 * Get lookup menu items for the current query.
9404 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
9405 * of the done event. If the request was aborted to make way for a subsequent request,
9406 * this promise will not be rejected: it will remain pending forever.
9408 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
9410 value
= this.lookupInput
.getValue(),
9411 deferred
= $.Deferred(),
9414 this.abortLookupRequest();
9415 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
9416 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[ value
] ) );
9418 this.lookupInput
.pushPending();
9419 this.lookupQuery
= value
;
9420 ourRequest
= this.lookupRequest
= this.getLookupRequest();
9422 .always( function () {
9423 // We need to pop pending even if this is an old request, otherwise
9424 // the widget will remain pending forever.
9425 // TODO: this assumes that an aborted request will fail or succeed soon after
9426 // being aborted, or at least eventually. It would be nice if we could popPending()
9427 // at abort time, but only if we knew that we hadn't already called popPending()
9428 // for that request.
9429 widget
.lookupInput
.popPending();
9431 .done( function ( data
) {
9432 // If this is an old request (and aborting it somehow caused it to still succeed),
9433 // ignore its success completely
9434 if ( ourRequest
=== widget
.lookupRequest
) {
9435 widget
.lookupQuery
= null;
9436 widget
.lookupRequest
= null;
9437 widget
.lookupCache
[ value
] = widget
.getLookupCacheItemFromData( data
);
9438 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[ value
] ) );
9441 .fail( function () {
9442 // If this is an old request (or a request failing because it's being aborted),
9443 // ignore its failure completely
9444 if ( ourRequest
=== widget
.lookupRequest
) {
9445 widget
.lookupQuery
= null;
9446 widget
.lookupRequest
= null;
9451 return deferred
.promise();
9455 * Abort the currently pending lookup request, if any.
9457 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
9458 var oldRequest
= this.lookupRequest
;
9460 // First unset this.lookupRequest to the fail handler will notice
9461 // that the request is no longer current
9462 this.lookupRequest
= null;
9463 this.lookupQuery
= null;
9469 * Get a new request object of the current lookup query value.
9472 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
9474 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
9475 // Stub, implemented in subclass
9480 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
9483 * @param {Mixed} data Cached result data, usually an array
9484 * @return {OO.ui.MenuOptionWidget[]} Menu items
9486 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
9487 // Stub, implemented in subclass
9492 * Get lookup cache item from server response data.
9495 * @param {Mixed} data Response from server
9496 * @return {Mixed} Cached result data
9498 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
9499 // Stub, implemented in subclass
9504 * Set of controls for an OO.ui.OutlineSelectWidget.
9506 * Controls include moving items up and down, removing items, and adding different kinds of items.
9509 * @extends OO.ui.Widget
9510 * @mixins OO.ui.GroupElement
9511 * @mixins OO.ui.IconElement
9514 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
9515 * @param {Object} [config] Configuration options
9517 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
9518 // Configuration initialization
9519 config
= $.extend( { icon
: 'add' }, config
);
9521 // Parent constructor
9522 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
9524 // Mixin constructors
9525 OO
.ui
.GroupElement
.call( this, config
);
9526 OO
.ui
.IconElement
.call( this, config
);
9529 this.outline
= outline
;
9530 this.$movers
= this.$( '<div>' );
9531 this.upButton
= new OO
.ui
.ButtonWidget( {
9535 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
9537 this.downButton
= new OO
.ui
.ButtonWidget( {
9541 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
9543 this.removeButton
= new OO
.ui
.ButtonWidget( {
9547 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
9551 outline
.connect( this, {
9552 select
: 'onOutlineChange',
9553 add
: 'onOutlineChange',
9554 remove
: 'onOutlineChange'
9556 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
9557 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
9558 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
9561 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
9562 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
9564 .addClass( 'oo-ui-outlineControlsWidget-movers' )
9565 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
9566 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
9571 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
9572 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
9573 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
9579 * @param {number} places Number of places to move
9589 * Handle outline change events.
9591 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
9592 var i
, len
, firstMovable
, lastMovable
,
9593 items
= this.outline
.getItems(),
9594 selectedItem
= this.outline
.getSelectedItem(),
9595 movable
= selectedItem
&& selectedItem
.isMovable(),
9596 removable
= selectedItem
&& selectedItem
.isRemovable();
9601 while ( ++i
< len
) {
9602 if ( items
[ i
].isMovable() ) {
9603 firstMovable
= items
[ i
];
9609 if ( items
[ i
].isMovable() ) {
9610 lastMovable
= items
[ i
];
9615 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
9616 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
9617 this.removeButton
.setDisabled( !removable
);
9621 * Mixin for widgets with a boolean on/off state.
9627 * @param {Object} [config] Configuration options
9628 * @cfg {boolean} [value=false] Initial value
9630 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
9631 // Configuration initialization
9632 config
= config
|| {};
9638 this.$element
.addClass( 'oo-ui-toggleWidget' );
9639 this.setValue( !!config
.value
);
9646 * @param {boolean} value Changed value
9652 * Get the value of the toggle.
9656 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
9661 * Set the value of the toggle.
9663 * @param {boolean} value New value
9667 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
9669 if ( this.value
!== value
) {
9671 this.emit( 'change', value
);
9672 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
9673 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
9674 this.$element
.attr( 'aria-checked', value
.toString() );
9680 * Group widget for multiple related buttons.
9682 * Use together with OO.ui.ButtonWidget.
9685 * @extends OO.ui.Widget
9686 * @mixins OO.ui.GroupElement
9689 * @param {Object} [config] Configuration options
9690 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
9692 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
9693 // Configuration initialization
9694 config
= config
|| {};
9696 // Parent constructor
9697 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
9699 // Mixin constructors
9700 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
9703 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
9704 if ( $.isArray( config
.items
) ) {
9705 this.addItems( config
.items
);
9711 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
9712 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
9715 * Generic widget for buttons.
9718 * @extends OO.ui.Widget
9719 * @mixins OO.ui.ButtonElement
9720 * @mixins OO.ui.IconElement
9721 * @mixins OO.ui.IndicatorElement
9722 * @mixins OO.ui.LabelElement
9723 * @mixins OO.ui.TitledElement
9724 * @mixins OO.ui.FlaggedElement
9725 * @mixins OO.ui.TabIndexedElement
9728 * @param {Object} [config] Configuration options
9729 * @cfg {string} [href] Hyperlink to visit when clicked
9730 * @cfg {string} [target] Target to open hyperlink in
9732 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
9733 // Configuration initialization
9734 config
= config
|| {};
9736 // Parent constructor
9737 OO
.ui
.ButtonWidget
.super.call( this, config
);
9739 // Mixin constructors
9740 OO
.ui
.ButtonElement
.call( this, config
);
9741 OO
.ui
.IconElement
.call( this, config
);
9742 OO
.ui
.IndicatorElement
.call( this, config
);
9743 OO
.ui
.LabelElement
.call( this, config
);
9744 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
9745 OO
.ui
.FlaggedElement
.call( this, config
);
9746 OO
.ui
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
9751 this.isHyperlink
= false;
9755 click
: this.onClick
.bind( this ),
9756 keypress
: this.onKeyPress
.bind( this )
9760 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
9762 .addClass( 'oo-ui-buttonWidget' )
9763 .append( this.$button
);
9764 this.setHref( config
.href
);
9765 this.setTarget( config
.target
);
9770 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
9771 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
9772 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
9773 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
9774 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
9775 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
9776 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
9777 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TabIndexedElement
);
9788 * Handles mouse click events.
9790 * @param {jQuery.Event} e Mouse click event
9793 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
9794 if ( !this.isDisabled() ) {
9795 this.emit( 'click' );
9796 if ( this.isHyperlink
) {
9806 OO
.ui
.ButtonWidget
.prototype.onMouseDown = function ( e
) {
9807 // Remove the tab-index while the button is down to prevent the button from stealing focus
9808 this.$button
.removeAttr( 'tabindex' );
9809 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
9810 // reliably reapply the tabindex and remove the pressed class
9811 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
9813 return OO
.ui
.ButtonElement
.prototype.onMouseDown
.call( this, e
);
9819 OO
.ui
.ButtonWidget
.prototype.onMouseUp = function ( e
) {
9820 // Restore the tab-index after the button is up to restore the button's accessibility
9821 this.$button
.attr( 'tabindex', this.tabIndex
);
9822 // Stop listening for mouseup, since we only needed this once
9823 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
9825 return OO
.ui
.ButtonElement
.prototype.onMouseUp
.call( this, e
);
9829 * Handles keypress events.
9831 * @param {jQuery.Event} e Keypress event
9834 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
9835 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9836 this.emit( 'click' );
9837 if ( this.isHyperlink
) {
9845 * Get hyperlink location.
9847 * @return {string} Hyperlink location
9849 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
9854 * Get hyperlink target.
9856 * @return {string} Hyperlink target
9858 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
9863 * Set hyperlink location.
9865 * @param {string|null} href Hyperlink location, null to remove
9867 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
9868 href
= typeof href
=== 'string' ? href
: null;
9870 if ( href
!== this.href
) {
9872 if ( href
!== null ) {
9873 this.$button
.attr( 'href', href
);
9874 this.isHyperlink
= true;
9876 this.$button
.removeAttr( 'href' );
9877 this.isHyperlink
= false;
9885 * Set hyperlink target.
9887 * @param {string|null} target Hyperlink target, null to remove
9889 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
9890 target
= typeof target
=== 'string' ? target
: null;
9892 if ( target
!== this.target
) {
9893 this.target
= target
;
9894 if ( target
!== null ) {
9895 this.$button
.attr( 'target', target
);
9897 this.$button
.removeAttr( 'target' );
9905 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
9908 * @extends OO.ui.ButtonWidget
9909 * @mixins OO.ui.PendingElement
9912 * @param {Object} [config] Configuration options
9913 * @cfg {string} [action] Symbolic action name
9914 * @cfg {string[]} [modes] Symbolic mode names
9915 * @cfg {boolean} [framed=false] Render button with a frame
9917 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
9918 // Configuration initialization
9919 config
= $.extend( { framed
: false }, config
);
9921 // Parent constructor
9922 OO
.ui
.ActionWidget
.super.call( this, config
);
9924 // Mixin constructors
9925 OO
.ui
.PendingElement
.call( this, config
);
9928 this.action
= config
.action
|| '';
9929 this.modes
= config
.modes
|| [];
9934 this.$element
.addClass( 'oo-ui-actionWidget' );
9939 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
9940 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
9951 * Check if action is available in a certain mode.
9953 * @param {string} mode Name of mode
9954 * @return {boolean} Has mode
9956 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
9957 return this.modes
.indexOf( mode
) !== -1;
9961 * Get symbolic action name.
9965 OO
.ui
.ActionWidget
.prototype.getAction = function () {
9970 * Get symbolic action name.
9974 OO
.ui
.ActionWidget
.prototype.getModes = function () {
9975 return this.modes
.slice();
9979 * Emit a resize event if the size has changed.
9983 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
9986 if ( this.isElementAttached() ) {
9987 width
= this.$element
.width();
9988 height
= this.$element
.height();
9990 if ( width
!== this.width
|| height
!== this.height
) {
9992 this.height
= height
;
9993 this.emit( 'resize' );
10003 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
10005 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
10006 this.propagateResize();
10014 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
10016 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
10017 this.propagateResize();
10025 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
10027 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
10028 this.propagateResize();
10036 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
10038 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
10039 this.propagateResize();
10045 * Toggle visibility of button.
10047 * @param {boolean} [show] Show button, omit to toggle visibility
10050 OO
.ui
.ActionWidget
.prototype.toggle = function () {
10052 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
10053 this.propagateResize();
10059 * Button that shows and hides a popup.
10062 * @extends OO.ui.ButtonWidget
10063 * @mixins OO.ui.PopupElement
10066 * @param {Object} [config] Configuration options
10068 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
10069 // Parent constructor
10070 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
10072 // Mixin constructors
10073 OO
.ui
.PopupElement
.call( this, config
);
10077 .addClass( 'oo-ui-popupButtonWidget' )
10078 .attr( 'aria-haspopup', 'true' )
10079 .append( this.popup
.$element
);
10084 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
10085 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
10090 * Handles mouse click events.
10092 * @param {jQuery.Event} e Mouse click event
10094 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
10095 // Skip clicks within the popup
10096 if ( $.contains( this.popup
.$element
[ 0 ], e
.target
) ) {
10100 if ( !this.isDisabled() ) {
10101 this.popup
.toggle();
10103 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
10109 * Button that toggles on and off.
10112 * @extends OO.ui.ButtonWidget
10113 * @mixins OO.ui.ToggleWidget
10116 * @param {Object} [config] Configuration options
10117 * @cfg {boolean} [value=false] Initial value
10119 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
10120 // Configuration initialization
10121 config
= config
|| {};
10123 // Parent constructor
10124 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
10126 // Mixin constructors
10127 OO
.ui
.ToggleWidget
.call( this, config
);
10130 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
10135 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
10136 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
10143 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
10144 if ( !this.isDisabled() ) {
10145 this.setValue( !this.value
);
10149 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
10155 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
10157 if ( value
!== this.value
) {
10158 this.$button
.attr( 'aria-pressed', value
.toString() );
10159 this.setActive( value
);
10162 // Parent method (from mixin)
10163 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
10169 * Dropdown menu of options.
10171 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
10172 * can be accessed using the #getMenu method.
10174 * Use with OO.ui.MenuOptionWidget.
10177 * @extends OO.ui.Widget
10178 * @mixins OO.ui.IconElement
10179 * @mixins OO.ui.IndicatorElement
10180 * @mixins OO.ui.LabelElement
10181 * @mixins OO.ui.TitledElement
10184 * @param {Object} [config] Configuration options
10185 * @cfg {Object} [menu] Configuration options to pass to menu widget
10187 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
10188 // Configuration initialization
10189 config
= $.extend( { indicator
: 'down' }, config
);
10191 // Parent constructor
10192 OO
.ui
.DropdownWidget
.super.call( this, config
);
10194 // Mixin constructors
10195 OO
.ui
.IconElement
.call( this, config
);
10196 OO
.ui
.IndicatorElement
.call( this, config
);
10197 OO
.ui
.LabelElement
.call( this, config
);
10198 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
10201 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
10202 this.$handle
= this.$( '<span>' );
10205 this.$element
.on( { click
: this.onClick
.bind( this ) } );
10206 this.menu
.connect( this, { select
: 'onMenuSelect' } );
10210 .addClass( 'oo-ui-dropdownWidget-handle' )
10211 .append( this.$icon
, this.$label
, this.$indicator
);
10213 .addClass( 'oo-ui-dropdownWidget' )
10214 .append( this.$handle
, this.menu
.$element
);
10219 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
10220 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
10221 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
10222 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
10223 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
10230 * @return {OO.ui.MenuSelectWidget} Menu of widget
10232 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
10237 * Handles menu select events.
10239 * @param {OO.ui.MenuOptionWidget} item Selected menu item
10241 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
10248 selectedLabel
= item
.getLabel();
10250 // If the label is a DOM element, clone it, because setLabel will append() it
10251 if ( selectedLabel
instanceof jQuery
) {
10252 selectedLabel
= selectedLabel
.clone();
10255 this.setLabel( selectedLabel
);
10259 * Handles mouse click events.
10261 * @param {jQuery.Event} e Mouse click event
10263 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
10264 // Skip clicks within the menu
10265 if ( $.contains( this.menu
.$element
[ 0 ], e
.target
) ) {
10269 if ( !this.isDisabled() ) {
10270 if ( this.menu
.isVisible() ) {
10271 this.menu
.toggle( false );
10273 this.menu
.toggle( true );
10282 * See OO.ui.IconElement for more information.
10285 * @extends OO.ui.Widget
10286 * @mixins OO.ui.IconElement
10287 * @mixins OO.ui.TitledElement
10290 * @param {Object} [config] Configuration options
10292 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
10293 // Configuration initialization
10294 config
= config
|| {};
10296 // Parent constructor
10297 OO
.ui
.IconWidget
.super.call( this, config
);
10299 // Mixin constructors
10300 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
10301 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
10304 this.$element
.addClass( 'oo-ui-iconWidget' );
10309 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
10310 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
10311 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
10313 /* Static Properties */
10315 OO
.ui
.IconWidget
.static.tagName
= 'span';
10318 * Indicator widget.
10320 * See OO.ui.IndicatorElement for more information.
10323 * @extends OO.ui.Widget
10324 * @mixins OO.ui.IndicatorElement
10325 * @mixins OO.ui.TitledElement
10328 * @param {Object} [config] Configuration options
10330 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
10331 // Configuration initialization
10332 config
= config
|| {};
10334 // Parent constructor
10335 OO
.ui
.IndicatorWidget
.super.call( this, config
);
10337 // Mixin constructors
10338 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
10339 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
10342 this.$element
.addClass( 'oo-ui-indicatorWidget' );
10347 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
10348 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
10349 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
10351 /* Static Properties */
10353 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
10356 * Base class for input widgets.
10360 * @extends OO.ui.Widget
10361 * @mixins OO.ui.FlaggedElement
10362 * @mixins OO.ui.TabIndexedElement
10365 * @param {Object} [config] Configuration options
10366 * @cfg {string} [name=''] HTML input name
10367 * @cfg {string} [value=''] Input value
10368 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
10370 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
10371 // Configuration initialization
10372 config
= config
|| {};
10374 // Parent constructor
10375 OO
.ui
.InputWidget
.super.call( this, config
);
10378 this.$input
= this.getInputElement( config
);
10380 this.inputFilter
= config
.inputFilter
;
10382 // Mixin constructors
10383 OO
.ui
.FlaggedElement
.call( this, config
);
10384 OO
.ui
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
10387 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
10391 .attr( 'name', config
.name
)
10392 .prop( 'disabled', this.isDisabled() );
10393 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
10394 this.setValue( config
.value
);
10399 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
10400 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
10401 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.TabIndexedElement
);
10407 * @param {string} value
10413 * Get input element.
10415 * Subclasses of OO.ui.InputWidget use the `config` parameter to produce different elements in
10416 * different circumstances. The element must have a `value` property (like form elements).
10419 * @param {Object} config Configuration options
10420 * @return {jQuery} Input element
10422 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
10423 return this.$( '<input>' );
10427 * Handle potentially value-changing events.
10429 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
10431 OO
.ui
.InputWidget
.prototype.onEdit = function () {
10433 if ( !this.isDisabled() ) {
10434 // Allow the stack to clear so the value will be updated
10435 setTimeout( function () {
10436 widget
.setValue( widget
.$input
.val() );
10442 * Get the value of the input.
10444 * @return {string} Input value
10446 OO
.ui
.InputWidget
.prototype.getValue = function () {
10447 // Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
10448 // it, and we won't know unless they're kind enough to trigger a 'change' event.
10449 var value
= this.$input
.val();
10450 if ( this.value
!== value
) {
10451 this.setValue( value
);
10457 * Sets the direction of the current input, either RTL or LTR
10459 * @param {boolean} isRTL
10461 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
10462 this.$input
.prop( 'dir', isRTL
? 'rtl' : 'ltr' );
10466 * Set the value of the input.
10468 * @param {string} value New value
10472 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
10473 value
= this.cleanUpValue( value
);
10474 // Update the DOM if it has changed. Note that with cleanUpValue, it
10475 // is possible for the DOM value to change without this.value changing.
10476 if ( this.$input
.val() !== value
) {
10477 this.$input
.val( value
);
10479 if ( this.value
!== value
) {
10480 this.value
= value
;
10481 this.emit( 'change', this.value
);
10487 * Clean up incoming value.
10489 * Ensures value is a string, and converts undefined and null to empty string.
10492 * @param {string} value Original value
10493 * @return {string} Cleaned up value
10495 OO
.ui
.InputWidget
.prototype.cleanUpValue = function ( value
) {
10496 if ( value
=== undefined || value
=== null ) {
10498 } else if ( this.inputFilter
) {
10499 return this.inputFilter( String( value
) );
10501 return String( value
);
10506 * Simulate the behavior of clicking on a label bound to this input.
10508 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
10509 if ( !this.isDisabled() ) {
10510 if ( this.$input
.is( ':checkbox,:radio' ) ) {
10511 this.$input
.click();
10512 } else if ( this.$input
.is( ':input' ) ) {
10513 this.$input
[ 0 ].focus();
10521 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
10522 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
10523 if ( this.$input
) {
10524 this.$input
.prop( 'disabled', this.isDisabled() );
10534 OO
.ui
.InputWidget
.prototype.focus = function () {
10535 this.$input
[ 0 ].focus();
10544 OO
.ui
.InputWidget
.prototype.blur = function () {
10545 this.$input
[ 0 ].blur();
10550 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
10553 * @extends OO.ui.InputWidget
10554 * @mixins OO.ui.ButtonElement
10555 * @mixins OO.ui.IconElement
10556 * @mixins OO.ui.IndicatorElement
10557 * @mixins OO.ui.LabelElement
10558 * @mixins OO.ui.TitledElement
10559 * @mixins OO.ui.FlaggedElement
10562 * @param {Object} [config] Configuration options
10563 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
10564 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
10565 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
10566 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
10567 * won't be possible to set a value (which will internally become identical to the label).
10569 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
10570 // Configuration initialization
10571 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
10573 // Properties (must be set before parent constructor, which calls #setValue)
10574 this.useInputTag
= config
.useInputTag
;
10576 // Parent constructor
10577 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
10579 // Mixin constructors
10580 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
10581 OO
.ui
.IconElement
.call( this, config
);
10582 OO
.ui
.IndicatorElement
.call( this, config
);
10583 OO
.ui
.LabelElement
.call( this, config
);
10584 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
10585 OO
.ui
.FlaggedElement
.call( this, config
);
10589 click
: this.onClick
.bind( this ),
10590 keypress
: this.onKeyPress
.bind( this )
10594 if ( !config
.useInputTag
) {
10595 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
10597 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
10602 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
10603 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
10604 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
10605 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
10606 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
10607 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
10608 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
10622 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
10623 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
10624 return this.$( html
);
10630 * Overridden to support setting the 'value' of `<input/>` elements.
10632 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
10633 * text; or null for no label
10636 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
10637 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
10639 if ( this.useInputTag
) {
10640 if ( typeof label
=== 'function' ) {
10641 label
= OO
.ui
.resolveMsg( label
);
10643 if ( label
instanceof jQuery
) {
10644 label
= label
.text();
10649 this.$input
.val( label
);
10656 * Set the value of the input.
10658 * Overridden to disable for `<input/>` elements, which have value identical to the label.
10660 * @param {string} value New value
10663 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
10664 if ( !this.useInputTag
) {
10665 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
10671 * Handles mouse click events.
10673 * @param {jQuery.Event} e Mouse click event
10676 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
10677 if ( !this.isDisabled() ) {
10678 this.emit( 'click' );
10684 * Handles keypress events.
10686 * @param {jQuery.Event} e Keypress event
10689 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
10690 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
10691 this.emit( 'click' );
10697 * Checkbox input widget.
10700 * @extends OO.ui.InputWidget
10703 * @param {Object} [config] Configuration options
10704 * @cfg {boolean} [selected=false] Whether the checkbox is initially selected
10706 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
10707 // Parent constructor
10708 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
10711 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
10712 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
10717 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
10725 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
10726 return this.$( '<input type="checkbox" />' );
10732 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
10734 if ( !this.isDisabled() ) {
10735 // Allow the stack to clear so the value will be updated
10736 setTimeout( function () {
10737 widget
.setSelected( widget
.$input
.prop( 'checked' ) );
10743 * Set selection state of this checkbox.
10745 * @param {boolean} state Whether the checkbox is selected
10748 OO
.ui
.CheckboxInputWidget
.prototype.setSelected = function ( state
) {
10750 if ( this.selected
!== state
) {
10751 this.selected
= state
;
10752 this.$input
.prop( 'checked', this.selected
);
10753 this.emit( 'change', this.selected
);
10759 * Check if this checkbox is selected.
10761 * @return {boolean} Checkbox is selected
10763 OO
.ui
.CheckboxInputWidget
.prototype.isSelected = function () {
10764 // Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
10765 // it, and we won't know unless they're kind enough to trigger a 'change' event.
10766 var selected
= this.$input
.prop( 'checked' );
10767 if ( this.selected
!== selected
) {
10768 this.setSelected( selected
);
10770 return this.selected
;
10774 * A OO.ui.DropdownWidget synchronized with a `<input type=hidden>` for form submission. Intended to
10775 * be used within a OO.ui.FormLayout.
10778 * @extends OO.ui.InputWidget
10781 * @param {Object} [config] Configuration options
10782 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
10784 OO
.ui
.DropdownInputWidget
= function OoUiDropdownInputWidget( config
) {
10785 // Configuration initialization
10786 config
= config
|| {};
10788 // Properties (must be done before parent constructor which calls #setDisabled)
10789 this.dropdownWidget
= new OO
.ui
.DropdownWidget( {
10793 // Parent constructor
10794 OO
.ui
.DropdownInputWidget
.super.call( this, config
);
10797 this.dropdownWidget
.getMenu().connect( this, { select
: 'onMenuSelect' } );
10800 this.setOptions( config
.options
|| [] );
10802 .addClass( 'oo-ui-dropdownInputWidget' )
10803 .append( this.dropdownWidget
.$element
);
10808 OO
.inheritClass( OO
.ui
.DropdownInputWidget
, OO
.ui
.InputWidget
);
10816 OO
.ui
.DropdownInputWidget
.prototype.getInputElement = function () {
10817 return this.$( '<input type="hidden">' );
10821 * Handles menu select events.
10823 * @param {OO.ui.MenuOptionWidget} item Selected menu item
10825 OO
.ui
.DropdownInputWidget
.prototype.onMenuSelect = function ( item
) {
10826 this.setValue( item
.getData() );
10832 OO
.ui
.DropdownInputWidget
.prototype.setValue = function ( value
) {
10833 var item
= this.dropdownWidget
.getMenu().getItemFromData( value
);
10835 this.dropdownWidget
.getMenu().selectItem( item
);
10837 OO
.ui
.DropdownInputWidget
.super.prototype.setValue
.call( this, value
);
10844 OO
.ui
.DropdownInputWidget
.prototype.setDisabled = function ( state
) {
10845 this.dropdownWidget
.setDisabled( state
);
10846 OO
.ui
.DropdownInputWidget
.super.prototype.setDisabled
.call( this, state
);
10851 * Set the options available for this input.
10853 * @param {Object[]} options Array of menu options in the format `{ data: …, label: … }`
10856 OO
.ui
.DropdownInputWidget
.prototype.setOptions = function ( options
) {
10857 var value
= this.getValue();
10859 // Rebuild the dropdown menu
10860 this.dropdownWidget
.getMenu()
10862 .addItems( options
.map( function ( opt
) {
10863 return new OO
.ui
.MenuOptionWidget( {
10865 label
: opt
.label
!== undefined ? opt
.label
: opt
.data
10869 // Restore the previous value, or reset to something sensible
10870 if ( this.dropdownWidget
.getMenu().getItemFromData( value
) ) {
10871 // Previous value is still available, ensure consistency with the dropdown
10872 this.setValue( value
);
10874 // No longer valid, reset
10875 if ( options
.length
) {
10876 this.setValue( options
[ 0 ].data
);
10886 OO
.ui
.DropdownInputWidget
.prototype.focus = function () {
10887 this.dropdownWidget
.getMenu().toggle( true );
10894 OO
.ui
.DropdownInputWidget
.prototype.blur = function () {
10895 this.dropdownWidget
.getMenu().toggle( false );
10900 * Radio input widget.
10902 * Radio buttons only make sense as a set, and you probably want to use the OO.ui.RadioSelectWidget
10903 * class instead of using this class directly.
10906 * @extends OO.ui.InputWidget
10909 * @param {Object} [config] Configuration options
10910 * @cfg {boolean} [selected=false] Whether the radio button is initially selected
10912 OO
.ui
.RadioInputWidget
= function OoUiRadioInputWidget( config
) {
10913 // Parent constructor
10914 OO
.ui
.RadioInputWidget
.super.call( this, config
);
10917 this.$element
.addClass( 'oo-ui-radioInputWidget' );
10918 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
10923 OO
.inheritClass( OO
.ui
.RadioInputWidget
, OO
.ui
.InputWidget
);
10931 OO
.ui
.RadioInputWidget
.prototype.getInputElement = function () {
10932 return this.$( '<input type="radio" />' );
10938 OO
.ui
.RadioInputWidget
.prototype.onEdit = function () {
10939 // RadioInputWidget doesn't track its state.
10943 * Set selection state of this radio button.
10945 * @param {boolean} state Whether the button is selected
10948 OO
.ui
.RadioInputWidget
.prototype.setSelected = function ( state
) {
10949 // RadioInputWidget doesn't track its state.
10950 this.$input
.prop( 'checked', state
);
10955 * Check if this radio button is selected.
10957 * @return {boolean} Radio is selected
10959 OO
.ui
.RadioInputWidget
.prototype.isSelected = function () {
10960 return this.$input
.prop( 'checked' );
10964 * Input widget with a text field.
10967 * @extends OO.ui.InputWidget
10968 * @mixins OO.ui.IconElement
10969 * @mixins OO.ui.IndicatorElement
10970 * @mixins OO.ui.PendingElement
10973 * @param {Object} [config] Configuration options
10974 * @cfg {string} [type='text'] HTML tag `type` attribute
10975 * @cfg {string} [placeholder] Placeholder text
10976 * @cfg {boolean} [autofocus=false] Ask the browser to focus this widget, using the 'autofocus' HTML
10978 * @cfg {boolean} [readOnly=false] Prevent changes
10979 * @cfg {number} [maxLength] Maximum allowed number of characters to input
10980 * @cfg {boolean} [multiline=false] Allow multiple lines of text
10981 * @cfg {boolean} [autosize=false] Automatically resize to fit content
10982 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
10983 * @cfg {string} [labelPosition='after'] Label position, 'before' or 'after'
10984 * @cfg {RegExp|string} [validate] Regular expression to validate against (or symbolic name referencing
10985 * one, see #static-validationPatterns)
10987 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
10988 // Configuration initialization
10989 config
= $.extend( {
10991 labelPosition
: 'after',
10995 // Parent constructor
10996 OO
.ui
.TextInputWidget
.super.call( this, config
);
10998 // Mixin constructors
10999 OO
.ui
.IconElement
.call( this, config
);
11000 OO
.ui
.IndicatorElement
.call( this, config
);
11001 OO
.ui
.PendingElement
.call( this, config
);
11002 OO
.ui
.LabelElement
.call( this, config
);
11005 this.readOnly
= false;
11006 this.multiline
= !!config
.multiline
;
11007 this.autosize
= !!config
.autosize
;
11008 this.maxRows
= config
.maxRows
;
11009 this.validate
= null;
11010 this.attached
= false;
11012 // Clone for resizing
11013 if ( this.autosize
) {
11014 this.$clone
= this.$input
11016 .insertAfter( this.$input
)
11020 this.setValidation( config
.validate
);
11021 this.setPosition( config
.labelPosition
);
11025 keypress
: this.onKeyPress
.bind( this ),
11026 blur
: this.setValidityFlag
.bind( this )
11028 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
11029 this.$element
.on( 'DOMNodeRemovedFromDocument', this.onElementDetach
.bind( this ) );
11030 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
11031 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
11032 this.on( 'labelChange', this.updatePosition
.bind( this ) );
11036 .addClass( 'oo-ui-textInputWidget' )
11037 .append( this.$icon
, this.$indicator
, this.$label
);
11038 this.setReadOnly( !!config
.readOnly
);
11039 if ( config
.placeholder
) {
11040 this.$input
.attr( 'placeholder', config
.placeholder
);
11042 if ( config
.maxLength
) {
11043 this.$input
.attr( 'maxlength', config
.maxLength
);
11045 if ( config
.autofocus
) {
11046 this.$input
.attr( 'autofocus', 'autofocus' );
11052 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
11053 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
11054 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
11055 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
11056 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.LabelElement
);
11058 /* Static properties */
11060 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
11068 * User presses enter inside the text box.
11070 * Not called if input is multiline.
11076 * User clicks the icon.
11082 * User clicks the indicator.
11090 * Handle icon mouse down events.
11092 * @param {jQuery.Event} e Mouse down event
11095 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
11096 if ( e
.which
=== 1 ) {
11097 this.$input
[ 0 ].focus();
11098 this.emit( 'icon' );
11104 * Handle indicator mouse down events.
11106 * @param {jQuery.Event} e Mouse down event
11109 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
11110 if ( e
.which
=== 1 ) {
11111 this.$input
[ 0 ].focus();
11112 this.emit( 'indicator' );
11118 * Handle key press events.
11120 * @param {jQuery.Event} e Key press event
11121 * @fires enter If enter key is pressed and input is not multiline
11123 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
11124 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
11125 this.emit( 'enter', e
);
11130 * Handle element attach events.
11132 * @param {jQuery.Event} e Element attach event
11134 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
11135 this.attached
= true;
11136 // If we reattached elsewhere, the valCache is now invalid
11137 this.valCache
= null;
11139 this.positionLabel();
11143 * Handle element detach events.
11145 * @param {jQuery.Event} e Element detach event
11147 OO
.ui
.TextInputWidget
.prototype.onElementDetach = function () {
11148 this.attached
= false;
11154 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
11158 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
11164 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
11166 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
11168 this.setValidityFlag();
11174 * Check if the widget is read-only.
11176 * @return {boolean}
11178 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
11179 return this.readOnly
;
11183 * Set the read-only state of the widget.
11185 * This should probably change the widget's appearance and prevent it from being used.
11187 * @param {boolean} state Make input read-only
11190 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
11191 this.readOnly
= !!state
;
11192 this.$input
.prop( 'readOnly', this.readOnly
);
11197 * Automatically adjust the size of the text input.
11199 * This only affects multi-line inputs that are auto-sized.
11203 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
11204 var scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
11206 if ( this.multiline
&& this.autosize
&& this.attached
&& this.$input
.val() !== this.valCache
) {
11208 .val( this.$input
.val() )
11209 .attr( 'rows', '' )
11210 // Set inline height property to 0 to measure scroll height
11211 .css( 'height', 0 );
11213 this.$clone
[ 0 ].style
.display
= 'block';
11215 this.valCache
= this.$input
.val();
11217 scrollHeight
= this.$clone
[ 0 ].scrollHeight
;
11219 // Remove inline height property to measure natural heights
11220 this.$clone
.css( 'height', '' );
11221 innerHeight
= this.$clone
.innerHeight();
11222 outerHeight
= this.$clone
.outerHeight();
11224 // Measure max rows height
11226 .attr( 'rows', this.maxRows
)
11227 .css( 'height', 'auto' )
11229 maxInnerHeight
= this.$clone
.innerHeight();
11231 // Difference between reported innerHeight and scrollHeight with no scrollbars present
11232 // Equals 1 on Blink-based browsers and 0 everywhere else
11233 measurementError
= maxInnerHeight
- this.$clone
[ 0 ].scrollHeight
;
11234 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
11236 this.$clone
[ 0 ].style
.display
= 'none';
11238 // Only apply inline height when expansion beyond natural height is needed
11239 if ( idealHeight
> innerHeight
) {
11240 // Use the difference between the inner and outer height as a buffer
11241 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
11243 this.$input
.css( 'height', '' );
11253 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
11254 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + config
.type
+ '" />' );
11258 * Check if input supports multiple lines.
11260 * @return {boolean}
11262 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
11263 return !!this.multiline
;
11267 * Check if input automatically adjusts its size.
11269 * @return {boolean}
11271 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
11272 return !!this.autosize
;
11276 * Select the contents of the input.
11280 OO
.ui
.TextInputWidget
.prototype.select = function () {
11281 this.$input
.select();
11286 * Sets the validation pattern to use.
11287 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
11288 * one, see #static-validationPatterns)
11290 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
11291 if ( validate
instanceof RegExp
) {
11292 this.validate
= validate
;
11294 this.validate
= this.constructor.static.validationPatterns
[ validate
] || /.*/;
11299 * Sets the 'invalid' flag appropriately.
11301 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
11303 this.isValid().done( function ( valid
) {
11304 widget
.setFlags( { invalid
: !valid
} );
11309 * Returns whether or not the current value is considered valid, according to the
11310 * supplied validation pattern.
11312 * @return {jQuery.Deferred}
11314 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
11315 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
11319 * Set the position of the inline label.
11321 * @param {string} labelPosition Label position, 'before' or 'after'
11324 OO
.ui
.TextInputWidget
.prototype.setPosition = function ( labelPosition
) {
11325 this.labelPosition
= labelPosition
;
11326 this.updatePosition();
11331 * Update the position of the inline label.
11335 OO
.ui
.TextInputWidget
.prototype.updatePosition = function () {
11336 var after
= this.labelPosition
=== 'after';
11339 .toggleClass( 'oo-ui-textInputWidget-labelPosition-after', this.label
&& after
)
11340 .toggleClass( 'oo-ui-textInputWidget-labelPosition-before', this.label
&& !after
);
11342 if ( this.label
) {
11343 this.positionLabel();
11350 * Position the label by setting the correct padding on the input.
11354 OO
.ui
.TextInputWidget
.prototype.positionLabel = function () {
11355 // Clear old values
11357 // Clear old values if present
11359 'padding-right': '',
11363 if ( !this.$label
.text() ) {
11367 var after
= this.labelPosition
=== 'after',
11368 rtl
= this.$element
.css( 'direction' ) === 'rtl',
11369 property
= after
=== rtl
? 'padding-left' : 'padding-right';
11371 this.$input
.css( property
, this.$label
.outerWidth() );
11377 * Text input with a menu of optional values.
11380 * @extends OO.ui.Widget
11383 * @param {Object} [config] Configuration options
11384 * @cfg {Object} [menu] Configuration options to pass to menu widget
11385 * @cfg {Object} [input] Configuration options to pass to input widget
11386 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
11388 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
11389 // Configuration initialization
11390 config
= config
|| {};
11392 // Parent constructor
11393 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
11396 this.$overlay
= config
.$overlay
|| this.$element
;
11397 this.input
= new OO
.ui
.TextInputWidget( $.extend(
11398 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
11401 this.menu
= new OO
.ui
.TextInputMenuSelectWidget( this.input
, $.extend(
11403 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
11406 disabled
: this.isDisabled()
11412 this.input
.connect( this, {
11413 change
: 'onInputChange',
11414 indicator
: 'onInputIndicator',
11415 enter
: 'onInputEnter'
11417 this.menu
.connect( this, {
11418 choose
: 'onMenuChoose',
11419 add
: 'onMenuItemsChange',
11420 remove
: 'onMenuItemsChange'
11424 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
11425 this.$overlay
.append( this.menu
.$element
);
11426 this.onMenuItemsChange();
11431 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
11436 * Get the combobox's menu.
11437 * @return {OO.ui.TextInputMenuSelectWidget} Menu widget
11439 OO
.ui
.ComboBoxWidget
.prototype.getMenu = function () {
11444 * Handle input change events.
11446 * @param {string} value New value
11448 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
11449 var match
= this.menu
.getItemFromData( value
);
11451 this.menu
.selectItem( match
);
11453 if ( !this.isDisabled() ) {
11454 this.menu
.toggle( true );
11459 * Handle input indicator events.
11461 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
11462 if ( !this.isDisabled() ) {
11463 this.menu
.toggle();
11468 * Handle input enter events.
11470 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
11471 if ( !this.isDisabled() ) {
11472 this.menu
.toggle( false );
11477 * Handle menu choose events.
11479 * @param {OO.ui.OptionWidget} item Chosen item
11481 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
11483 this.input
.setValue( item
.getData() );
11488 * Handle menu item change events.
11490 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
11491 var match
= this.menu
.getItemFromData( this.input
.getValue() );
11492 this.menu
.selectItem( match
);
11493 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
11499 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
11501 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
11503 if ( this.input
) {
11504 this.input
.setDisabled( this.isDisabled() );
11507 this.menu
.setDisabled( this.isDisabled() );
11517 * @extends OO.ui.Widget
11518 * @mixins OO.ui.LabelElement
11521 * @param {Object} [config] Configuration options
11522 * @cfg {OO.ui.InputWidget} [input] Input widget this label is for
11524 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
11525 // Configuration initialization
11526 config
= config
|| {};
11528 // Parent constructor
11529 OO
.ui
.LabelWidget
.super.call( this, config
);
11531 // Mixin constructors
11532 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
11533 OO
.ui
.TitledElement
.call( this, config
);
11536 this.input
= config
.input
;
11539 if ( this.input
instanceof OO
.ui
.InputWidget
) {
11540 this.$element
.on( 'click', this.onClick
.bind( this ) );
11544 this.$element
.addClass( 'oo-ui-labelWidget' );
11549 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
11550 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
11551 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
11553 /* Static Properties */
11555 OO
.ui
.LabelWidget
.static.tagName
= 'span';
11560 * Handles label mouse click events.
11562 * @param {jQuery.Event} e Mouse click event
11564 OO
.ui
.LabelWidget
.prototype.onClick = function () {
11565 this.input
.simulateLabelClick();
11570 * Generic option widget for use with OO.ui.SelectWidget.
11573 * @extends OO.ui.Widget
11574 * @mixins OO.ui.LabelElement
11575 * @mixins OO.ui.FlaggedElement
11578 * @param {Object} [config] Configuration options
11580 OO
.ui
.OptionWidget
= function OoUiOptionWidget( config
) {
11581 // Configuration initialization
11582 config
= config
|| {};
11584 // Parent constructor
11585 OO
.ui
.OptionWidget
.super.call( this, config
);
11587 // Mixin constructors
11588 OO
.ui
.ItemWidget
.call( this );
11589 OO
.ui
.LabelElement
.call( this, config
);
11590 OO
.ui
.FlaggedElement
.call( this, config
);
11593 this.selected
= false;
11594 this.highlighted
= false;
11595 this.pressed
= false;
11599 .data( 'oo-ui-optionWidget', this )
11600 .attr( 'role', 'option' )
11601 .addClass( 'oo-ui-optionWidget' )
11602 .append( this.$label
);
11607 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
11608 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
11609 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
11610 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
11612 /* Static Properties */
11614 OO
.ui
.OptionWidget
.static.selectable
= true;
11616 OO
.ui
.OptionWidget
.static.highlightable
= true;
11618 OO
.ui
.OptionWidget
.static.pressable
= true;
11620 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
11625 * Check if option can be selected.
11627 * @return {boolean} Item is selectable
11629 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
11630 return this.constructor.static.selectable
&& !this.isDisabled();
11634 * Check if option can be highlighted.
11636 * @return {boolean} Item is highlightable
11638 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
11639 return this.constructor.static.highlightable
&& !this.isDisabled();
11643 * Check if option can be pressed.
11645 * @return {boolean} Item is pressable
11647 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
11648 return this.constructor.static.pressable
&& !this.isDisabled();
11652 * Check if option is selected.
11654 * @return {boolean} Item is selected
11656 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
11657 return this.selected
;
11661 * Check if option is highlighted.
11663 * @return {boolean} Item is highlighted
11665 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
11666 return this.highlighted
;
11670 * Check if option is pressed.
11672 * @return {boolean} Item is pressed
11674 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
11675 return this.pressed
;
11679 * Set selected state.
11681 * @param {boolean} [state=false] Select option
11684 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
11685 if ( this.constructor.static.selectable
) {
11686 this.selected
= !!state
;
11687 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
11688 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
11689 this.scrollElementIntoView();
11691 this.updateThemeClasses();
11697 * Set highlighted state.
11699 * @param {boolean} [state=false] Highlight option
11702 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
11703 if ( this.constructor.static.highlightable
) {
11704 this.highlighted
= !!state
;
11705 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
11706 this.updateThemeClasses();
11712 * Set pressed state.
11714 * @param {boolean} [state=false] Press option
11717 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
11718 if ( this.constructor.static.pressable
) {
11719 this.pressed
= !!state
;
11720 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
11721 this.updateThemeClasses();
11727 * Option widget with an option icon and indicator.
11729 * Use together with OO.ui.SelectWidget.
11732 * @extends OO.ui.OptionWidget
11733 * @mixins OO.ui.IconElement
11734 * @mixins OO.ui.IndicatorElement
11737 * @param {Object} [config] Configuration options
11739 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( config
) {
11740 // Parent constructor
11741 OO
.ui
.DecoratedOptionWidget
.super.call( this, config
);
11743 // Mixin constructors
11744 OO
.ui
.IconElement
.call( this, config
);
11745 OO
.ui
.IndicatorElement
.call( this, config
);
11749 .addClass( 'oo-ui-decoratedOptionWidget' )
11750 .prepend( this.$icon
)
11751 .append( this.$indicator
);
11756 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
11757 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
11758 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
11761 * Option widget that looks like a button.
11763 * Use together with OO.ui.ButtonSelectWidget.
11766 * @extends OO.ui.DecoratedOptionWidget
11767 * @mixins OO.ui.ButtonElement
11768 * @mixins OO.ui.TabIndexedElement
11771 * @param {Object} [config] Configuration options
11773 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
11774 // Parent constructor
11775 OO
.ui
.ButtonOptionWidget
.super.call( this, config
);
11777 // Mixin constructors
11778 OO
.ui
.ButtonElement
.call( this, config
);
11779 OO
.ui
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
11782 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
11783 this.$button
.append( this.$element
.contents() );
11784 this.$element
.append( this.$button
);
11789 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11790 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
11791 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.TabIndexedElement
);
11793 /* Static Properties */
11795 // Allow button mouse down events to pass through so they can be handled by the parent select widget
11796 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
11803 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
11804 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
11806 if ( this.constructor.static.selectable
) {
11807 this.setActive( state
);
11814 * Option widget that looks like a radio button.
11816 * Use together with OO.ui.RadioSelectWidget.
11819 * @extends OO.ui.OptionWidget
11822 * @param {Object} [config] Configuration options
11824 OO
.ui
.RadioOptionWidget
= function OoUiRadioOptionWidget( config
) {
11825 // Parent constructor
11826 OO
.ui
.RadioOptionWidget
.super.call( this, config
);
11829 this.radio
= new OO
.ui
.RadioInputWidget( { value
: config
.data
} );
11833 .addClass( 'oo-ui-radioOptionWidget' )
11834 .prepend( this.radio
.$element
);
11839 OO
.inheritClass( OO
.ui
.RadioOptionWidget
, OO
.ui
.OptionWidget
);
11841 /* Static Properties */
11843 OO
.ui
.RadioOptionWidget
.static.highlightable
= false;
11845 OO
.ui
.RadioOptionWidget
.static.pressable
= false;
11852 OO
.ui
.RadioOptionWidget
.prototype.setSelected = function ( state
) {
11853 OO
.ui
.RadioOptionWidget
.super.prototype.setSelected
.call( this, state
);
11855 this.radio
.setSelected( state
);
11861 * Item of an OO.ui.MenuSelectWidget.
11864 * @extends OO.ui.DecoratedOptionWidget
11867 * @param {Object} [config] Configuration options
11869 OO
.ui
.MenuOptionWidget
= function OoUiMenuOptionWidget( config
) {
11870 // Configuration initialization
11871 config
= $.extend( { icon
: 'check' }, config
);
11873 // Parent constructor
11874 OO
.ui
.MenuOptionWidget
.super.call( this, config
);
11878 .attr( 'role', 'menuitem' )
11879 .addClass( 'oo-ui-menuOptionWidget' );
11884 OO
.inheritClass( OO
.ui
.MenuOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11887 * Section to group one or more items in a OO.ui.MenuSelectWidget.
11890 * @extends OO.ui.DecoratedOptionWidget
11893 * @param {Object} [config] Configuration options
11895 OO
.ui
.MenuSectionOptionWidget
= function OoUiMenuSectionOptionWidget( config
) {
11896 // Parent constructor
11897 OO
.ui
.MenuSectionOptionWidget
.super.call( this, config
);
11900 this.$element
.addClass( 'oo-ui-menuSectionOptionWidget' );
11905 OO
.inheritClass( OO
.ui
.MenuSectionOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11907 /* Static Properties */
11909 OO
.ui
.MenuSectionOptionWidget
.static.selectable
= false;
11911 OO
.ui
.MenuSectionOptionWidget
.static.highlightable
= false;
11914 * Items for an OO.ui.OutlineSelectWidget.
11917 * @extends OO.ui.DecoratedOptionWidget
11920 * @param {Object} [config] Configuration options
11921 * @cfg {number} [level] Indentation level
11922 * @cfg {boolean} [movable] Allow modification from outline controls
11924 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
11925 // Configuration initialization
11926 config
= config
|| {};
11928 // Parent constructor
11929 OO
.ui
.OutlineOptionWidget
.super.call( this, config
);
11933 this.movable
= !!config
.movable
;
11934 this.removable
= !!config
.removable
;
11937 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
11938 this.setLevel( config
.level
);
11943 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
11945 /* Static Properties */
11947 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
11949 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
11951 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
11953 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
11958 * Check if item is movable.
11960 * Movability is used by outline controls.
11962 * @return {boolean} Item is movable
11964 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
11965 return this.movable
;
11969 * Check if item is removable.
11971 * Removability is used by outline controls.
11973 * @return {boolean} Item is removable
11975 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
11976 return this.removable
;
11980 * Get indentation level.
11982 * @return {number} Indentation level
11984 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
11991 * Movability is used by outline controls.
11993 * @param {boolean} movable Item is movable
11996 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
11997 this.movable
= !!movable
;
11998 this.updateThemeClasses();
12003 * Set removability.
12005 * Removability is used by outline controls.
12007 * @param {boolean} movable Item is removable
12010 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
12011 this.removable
= !!removable
;
12012 this.updateThemeClasses();
12017 * Set indentation level.
12019 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
12022 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
12023 var levels
= this.constructor.static.levels
,
12024 levelClass
= this.constructor.static.levelClass
,
12027 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
12029 if ( this.level
=== i
) {
12030 this.$element
.addClass( levelClass
+ i
);
12032 this.$element
.removeClass( levelClass
+ i
);
12035 this.updateThemeClasses();
12041 * Container for content that is overlaid and positioned absolutely.
12044 * @extends OO.ui.Widget
12045 * @mixins OO.ui.LabelElement
12048 * @param {Object} [config] Configuration options
12049 * @cfg {number} [width=320] Width of popup in pixels
12050 * @cfg {number} [height] Height of popup, omit to use automatic height
12051 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
12052 * @cfg {string} [align='center'] Alignment of popup to origin
12053 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
12054 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
12055 * @cfg {jQuery} [$content] Content to append to the popup's body
12056 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
12057 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
12058 * @cfg {boolean} [head] Show label and close button at the top
12059 * @cfg {boolean} [padded] Add padding to the body
12061 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
12062 // Configuration initialization
12063 config
= config
|| {};
12065 // Parent constructor
12066 OO
.ui
.PopupWidget
.super.call( this, config
);
12068 // Mixin constructors
12069 OO
.ui
.LabelElement
.call( this, config
);
12070 OO
.ui
.ClippableElement
.call( this, config
);
12073 this.visible
= false;
12074 this.$popup
= this.$( '<div>' );
12075 this.$head
= this.$( '<div>' );
12076 this.$body
= this.$( '<div>' );
12077 this.$anchor
= this.$( '<div>' );
12078 // If undefined, will be computed lazily in updateDimensions()
12079 this.$container
= config
.$container
;
12080 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
12081 this.autoClose
= !!config
.autoClose
;
12082 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
12083 this.transitionTimeout
= null;
12084 this.anchor
= null;
12085 this.width
= config
.width
!== undefined ? config
.width
: 320;
12086 this.height
= config
.height
!== undefined ? config
.height
: null;
12087 this.align
= config
.align
|| 'center';
12088 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
12089 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
12092 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
12095 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
12096 this.$body
.addClass( 'oo-ui-popupWidget-body' );
12097 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
12099 .addClass( 'oo-ui-popupWidget-head' )
12100 .append( this.$label
, this.closeButton
.$element
);
12101 if ( !config
.head
) {
12105 .addClass( 'oo-ui-popupWidget-popup' )
12106 .append( this.$head
, this.$body
);
12109 .addClass( 'oo-ui-popupWidget' )
12110 .append( this.$popup
, this.$anchor
);
12111 // Move content, which was added to #$element by OO.ui.Widget, to the body
12112 if ( config
.$content
instanceof jQuery
) {
12113 this.$body
.append( config
.$content
);
12115 if ( config
.padded
) {
12116 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
12118 this.setClippableElement( this.$body
);
12123 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
12124 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
12125 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
12130 * Handles mouse down events.
12132 * @param {jQuery.Event} e Mouse down event
12134 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
12136 this.isVisible() &&
12137 !$.contains( this.$element
[ 0 ], e
.target
) &&
12138 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
12140 this.toggle( false );
12145 * Bind mouse down listener.
12147 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
12148 // Capture clicks outside popup
12149 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
12153 * Handles close button click events.
12155 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
12156 if ( this.isVisible() ) {
12157 this.toggle( false );
12162 * Unbind mouse down listener.
12164 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
12165 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
12169 * Set whether to show a anchor.
12171 * @param {boolean} [show] Show anchor, omit to toggle
12173 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
12174 show
= show
=== undefined ? !this.anchored
: !!show
;
12176 if ( this.anchored
!== show
) {
12178 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
12180 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
12182 this.anchored
= show
;
12187 * Check if showing a anchor.
12189 * @return {boolean} anchor is visible
12191 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
12192 return this.anchor
;
12198 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
12199 show
= show
=== undefined ? !this.isVisible() : !!show
;
12201 var change
= show
!== this.isVisible();
12204 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
12208 if ( this.autoClose
) {
12209 this.bindMouseDownListener();
12211 this.updateDimensions();
12212 this.toggleClipping( true );
12214 this.toggleClipping( false );
12215 if ( this.autoClose
) {
12216 this.unbindMouseDownListener();
12225 * Set the size of the popup.
12227 * Changing the size may also change the popup's position depending on the alignment.
12229 * @param {number} width Width
12230 * @param {number} height Height
12231 * @param {boolean} [transition=false] Use a smooth transition
12234 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
12235 this.width
= width
;
12236 this.height
= height
!== undefined ? height
: null;
12237 if ( this.isVisible() ) {
12238 this.updateDimensions( transition
);
12243 * Update the size and position.
12245 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
12246 * be called automatically.
12248 * @param {boolean} [transition=false] Use a smooth transition
12251 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
12252 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
12253 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
12256 if ( !this.$container
) {
12257 // Lazy-initialize $container if not specified in constructor
12258 this.$container
= this.$( this.getClosestScrollableElementContainer() );
12261 // Set height and width before measuring things, since it might cause our measurements
12262 // to change (e.g. due to scrollbars appearing or disappearing)
12265 height
: this.height
!== null ? this.height
: 'auto'
12268 // Compute initial popupOffset based on alignment
12269 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[ this.align
];
12271 // Figure out if this will cause the popup to go beyond the edge of the container
12272 originOffset
= this.$element
.offset().left
;
12273 containerLeft
= this.$container
.offset().left
;
12274 containerWidth
= this.$container
.innerWidth();
12275 containerRight
= containerLeft
+ containerWidth
;
12276 popupLeft
= popupOffset
- this.containerPadding
;
12277 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
12278 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
12279 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
12281 // Adjust offset to make the popup not go beyond the edge, if needed
12282 if ( overlapRight
< 0 ) {
12283 popupOffset
+= overlapRight
;
12284 } else if ( overlapLeft
< 0 ) {
12285 popupOffset
-= overlapLeft
;
12288 // Adjust offset to avoid anchor being rendered too close to the edge
12289 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
12290 // TODO: Find a measurement that works for CSS anchors and image anchors
12291 anchorWidth
= this.$anchor
[ 0 ].scrollWidth
* 2;
12292 if ( popupOffset
+ this.width
< anchorWidth
) {
12293 popupOffset
= anchorWidth
- this.width
;
12294 } else if ( -popupOffset
< anchorWidth
) {
12295 popupOffset
= -anchorWidth
;
12298 // Prevent transition from being interrupted
12299 clearTimeout( this.transitionTimeout
);
12300 if ( transition
) {
12301 // Enable transition
12302 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
12305 // Position body relative to anchor
12306 this.$popup
.css( 'margin-left', popupOffset
);
12308 if ( transition
) {
12309 // Prevent transitioning after transition is complete
12310 this.transitionTimeout
= setTimeout( function () {
12311 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
12314 // Prevent transitioning immediately
12315 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
12318 // Reevaluate clipping state since we've relocated and resized the popup
12325 * Progress bar widget.
12328 * @extends OO.ui.Widget
12331 * @param {Object} [config] Configuration options
12332 * @cfg {number|boolean} [progress=false] Initial progress percent or false for indeterminate
12334 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
12335 // Configuration initialization
12336 config
= config
|| {};
12338 // Parent constructor
12339 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
12342 this.$bar
= this.$( '<div>' );
12343 this.progress
= null;
12346 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
12347 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar' );
12350 role
: 'progressbar',
12351 'aria-valuemin': 0,
12352 'aria-valuemax': 100
12354 .addClass( 'oo-ui-progressBarWidget' )
12355 .append( this.$bar
);
12360 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
12362 /* Static Properties */
12364 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
12369 * Get progress percent
12371 * @return {number} Progress percent
12373 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
12374 return this.progress
;
12378 * Set progress percent
12380 * @param {number|boolean} progress Progress percent or false for indeterminate
12382 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
12383 this.progress
= progress
;
12385 if ( progress
!== false ) {
12386 this.$bar
.css( 'width', this.progress
+ '%' );
12387 this.$element
.attr( 'aria-valuenow', this.progress
);
12389 this.$bar
.css( 'width', '' );
12390 this.$element
.removeAttr( 'aria-valuenow' );
12392 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
12398 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
12399 * Results are cleared and populated each time the query is changed.
12402 * @extends OO.ui.Widget
12405 * @param {Object} [config] Configuration options
12406 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
12407 * @cfg {string} [value] Initial query value
12409 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
12410 // Configuration initialization
12411 config
= config
|| {};
12413 // Parent constructor
12414 OO
.ui
.SearchWidget
.super.call( this, config
);
12417 this.query
= new OO
.ui
.TextInputWidget( {
12420 placeholder
: config
.placeholder
,
12421 value
: config
.value
12423 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
12424 this.$query
= this.$( '<div>' );
12425 this.$results
= this.$( '<div>' );
12428 this.query
.connect( this, {
12429 change
: 'onQueryChange',
12430 enter
: 'onQueryEnter'
12432 this.results
.connect( this, {
12433 highlight
: 'onResultsHighlight',
12434 select
: 'onResultsSelect'
12436 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
12440 .addClass( 'oo-ui-searchWidget-query' )
12441 .append( this.query
.$element
);
12443 .addClass( 'oo-ui-searchWidget-results' )
12444 .append( this.results
.$element
);
12446 .addClass( 'oo-ui-searchWidget' )
12447 .append( this.$results
, this.$query
);
12452 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
12458 * @param {Object|null} item Item data or null if no item is highlighted
12463 * @param {Object|null} item Item data or null if no item is selected
12469 * Handle query key down events.
12471 * @param {jQuery.Event} e Key down event
12473 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
12474 var highlightedItem
, nextItem
,
12475 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
12478 highlightedItem
= this.results
.getHighlightedItem();
12479 if ( !highlightedItem
) {
12480 highlightedItem
= this.results
.getSelectedItem();
12482 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
12483 this.results
.highlightItem( nextItem
);
12484 nextItem
.scrollElementIntoView();
12489 * Handle select widget select events.
12491 * Clears existing results. Subclasses should repopulate items according to new query.
12493 * @param {string} value New value
12495 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
12497 this.results
.clearItems();
12501 * Handle select widget enter key events.
12503 * Selects highlighted item.
12505 * @param {string} value New value
12507 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
12509 this.results
.selectItem( this.results
.getHighlightedItem() );
12513 * Handle select widget highlight events.
12515 * @param {OO.ui.OptionWidget} item Highlighted item
12518 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
12519 this.emit( 'highlight', item
? item
.getData() : null );
12523 * Handle select widget select events.
12525 * @param {OO.ui.OptionWidget} item Selected item
12528 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
12529 this.emit( 'select', item
? item
.getData() : null );
12533 * Get the query input.
12535 * @return {OO.ui.TextInputWidget} Query input
12537 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
12542 * Get the results list.
12544 * @return {OO.ui.SelectWidget} Select list
12546 OO
.ui
.SearchWidget
.prototype.getResults = function () {
12547 return this.results
;
12551 * Generic selection of options.
12553 * Items can contain any rendering. Any widget that provides options, from which the user must
12554 * choose one, should be built on this class.
12556 * Use together with OO.ui.OptionWidget.
12559 * @extends OO.ui.Widget
12560 * @mixins OO.ui.GroupElement
12563 * @param {Object} [config] Configuration options
12564 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
12566 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
12567 // Configuration initialization
12568 config
= config
|| {};
12570 // Parent constructor
12571 OO
.ui
.SelectWidget
.super.call( this, config
);
12573 // Mixin constructors
12574 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
12577 this.pressed
= false;
12578 this.selecting
= null;
12579 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
12580 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
12583 this.$element
.on( {
12584 mousedown
: this.onMouseDown
.bind( this ),
12585 mouseover
: this.onMouseOver
.bind( this ),
12586 mouseleave
: this.onMouseLeave
.bind( this )
12590 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
12591 if ( $.isArray( config
.items
) ) {
12592 this.addItems( config
.items
);
12598 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
12600 // Need to mixin base class as well
12601 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
12602 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
12608 * @param {OO.ui.OptionWidget|null} item Highlighted item
12613 * @param {OO.ui.OptionWidget|null} item Pressed item
12618 * @param {OO.ui.OptionWidget|null} item Selected item
12623 * @param {OO.ui.OptionWidget|null} item Chosen item
12628 * @param {OO.ui.OptionWidget[]} items Added items
12629 * @param {number} index Index items were added at
12634 * @param {OO.ui.OptionWidget[]} items Removed items
12640 * Handle mouse down events.
12643 * @param {jQuery.Event} e Mouse down event
12645 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
12648 if ( !this.isDisabled() && e
.which
=== 1 ) {
12649 this.togglePressed( true );
12650 item
= this.getTargetItem( e
);
12651 if ( item
&& item
.isSelectable() ) {
12652 this.pressItem( item
);
12653 this.selecting
= item
;
12654 this.getElementDocument().addEventListener(
12656 this.onMouseUpHandler
,
12659 this.getElementDocument().addEventListener(
12661 this.onMouseMoveHandler
,
12670 * Handle mouse up events.
12673 * @param {jQuery.Event} e Mouse up event
12675 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
12678 this.togglePressed( false );
12679 if ( !this.selecting
) {
12680 item
= this.getTargetItem( e
);
12681 if ( item
&& item
.isSelectable() ) {
12682 this.selecting
= item
;
12685 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
12686 this.pressItem( null );
12687 this.chooseItem( this.selecting
);
12688 this.selecting
= null;
12691 this.getElementDocument().removeEventListener(
12693 this.onMouseUpHandler
,
12696 this.getElementDocument().removeEventListener(
12698 this.onMouseMoveHandler
,
12706 * Handle mouse move events.
12709 * @param {jQuery.Event} e Mouse move event
12711 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
12714 if ( !this.isDisabled() && this.pressed
) {
12715 item
= this.getTargetItem( e
);
12716 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
12717 this.pressItem( item
);
12718 this.selecting
= item
;
12725 * Handle mouse over events.
12728 * @param {jQuery.Event} e Mouse over event
12730 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
12733 if ( !this.isDisabled() ) {
12734 item
= this.getTargetItem( e
);
12735 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
12741 * Handle mouse leave events.
12744 * @param {jQuery.Event} e Mouse over event
12746 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
12747 if ( !this.isDisabled() ) {
12748 this.highlightItem( null );
12754 * Get the closest item to a jQuery.Event.
12757 * @param {jQuery.Event} e
12758 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
12760 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
12761 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
12762 if ( $item
.length
) {
12763 return $item
.data( 'oo-ui-optionWidget' );
12769 * Get selected item.
12771 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
12773 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
12776 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12777 if ( this.items
[ i
].isSelected() ) {
12778 return this.items
[ i
];
12785 * Get highlighted item.
12787 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
12789 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
12792 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12793 if ( this.items
[ i
].isHighlighted() ) {
12794 return this.items
[ i
];
12801 * Toggle pressed state.
12803 * @param {boolean} pressed An option is being pressed
12805 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
12806 if ( pressed
=== undefined ) {
12807 pressed
= !this.pressed
;
12809 if ( pressed
!== this.pressed
) {
12811 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
12812 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
12813 this.pressed
= pressed
;
12818 * Highlight an item.
12820 * Highlighting is mutually exclusive.
12822 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
12826 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
12827 var i
, len
, highlighted
,
12830 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12831 highlighted
= this.items
[ i
] === item
;
12832 if ( this.items
[ i
].isHighlighted() !== highlighted
) {
12833 this.items
[ i
].setHighlighted( highlighted
);
12838 this.emit( 'highlight', item
);
12847 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
12851 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
12852 var i
, len
, selected
,
12855 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12856 selected
= this.items
[ i
] === item
;
12857 if ( this.items
[ i
].isSelected() !== selected
) {
12858 this.items
[ i
].setSelected( selected
);
12863 this.emit( 'select', item
);
12872 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
12876 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
12877 var i
, len
, pressed
,
12880 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12881 pressed
= this.items
[ i
] === item
;
12882 if ( this.items
[ i
].isPressed() !== pressed
) {
12883 this.items
[ i
].setPressed( pressed
);
12888 this.emit( 'press', item
);
12897 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
12898 * an item is selected using the keyboard or mouse.
12900 * @param {OO.ui.OptionWidget} item Item to choose
12904 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
12905 this.selectItem( item
);
12906 this.emit( 'choose', item
);
12912 * Get an item relative to another one.
12914 * @param {OO.ui.OptionWidget|null} item Item to start at, null to get relative to list start
12915 * @param {number} direction Direction to move in, -1 to move backward, 1 to move forward
12916 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
12918 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
12919 var currentIndex
, nextIndex
, i
,
12920 increase
= direction
> 0 ? 1 : -1,
12921 len
= this.items
.length
;
12923 if ( item
instanceof OO
.ui
.OptionWidget
) {
12924 currentIndex
= $.inArray( item
, this.items
);
12925 nextIndex
= ( currentIndex
+ increase
+ len
) % len
;
12927 // If no item is selected and moving forward, start at the beginning.
12928 // If moving backward, start at the end.
12929 nextIndex
= direction
> 0 ? 0 : len
- 1;
12932 for ( i
= 0; i
< len
; i
++ ) {
12933 item
= this.items
[ nextIndex
];
12934 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
12937 nextIndex
= ( nextIndex
+ increase
+ len
) % len
;
12943 * Get the next selectable item.
12945 * @return {OO.ui.OptionWidget|null} Item, `null` if there aren't any selectable items
12947 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
12950 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
12951 item
= this.items
[ i
];
12952 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
12963 * @param {OO.ui.OptionWidget[]} items Items to add
12964 * @param {number} [index] Index to insert items after
12968 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
12970 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
12972 // Always provide an index, even if it was omitted
12973 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
12981 * Items will be detached, not removed, so they can be used later.
12983 * @param {OO.ui.OptionWidget[]} items Items to remove
12987 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
12990 // Deselect items being removed
12991 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
12993 if ( item
.isSelected() ) {
12994 this.selectItem( null );
12999 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
13001 this.emit( 'remove', items
);
13009 * Items will be detached, not removed, so they can be used later.
13014 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
13015 var items
= this.items
.slice();
13018 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
13021 this.selectItem( null );
13023 this.emit( 'remove', items
);
13029 * Select widget containing button options.
13031 * Use together with OO.ui.ButtonOptionWidget.
13034 * @extends OO.ui.SelectWidget
13037 * @param {Object} [config] Configuration options
13039 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
13040 // Parent constructor
13041 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
13044 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
13049 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
13052 * Select widget containing radio button options.
13054 * Use together with OO.ui.RadioOptionWidget.
13057 * @extends OO.ui.SelectWidget
13060 * @param {Object} [config] Configuration options
13062 OO
.ui
.RadioSelectWidget
= function OoUiRadioSelectWidget( config
) {
13063 // Parent constructor
13064 OO
.ui
.RadioSelectWidget
.super.call( this, config
);
13067 this.$element
.addClass( 'oo-ui-radioSelectWidget' );
13072 OO
.inheritClass( OO
.ui
.RadioSelectWidget
, OO
.ui
.SelectWidget
);
13075 * Overlaid menu of options.
13077 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
13080 * Use together with OO.ui.MenuOptionWidget.
13083 * @extends OO.ui.SelectWidget
13084 * @mixins OO.ui.ClippableElement
13087 * @param {Object} [config] Configuration options
13088 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
13089 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
13090 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
13092 OO
.ui
.MenuSelectWidget
= function OoUiMenuSelectWidget( config
) {
13093 // Configuration initialization
13094 config
= config
|| {};
13096 // Parent constructor
13097 OO
.ui
.MenuSelectWidget
.super.call( this, config
);
13099 // Mixin constructors
13100 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
13103 this.visible
= false;
13104 this.newItems
= null;
13105 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
13106 this.$input
= config
.input
? config
.input
.$input
: null;
13107 this.$widget
= config
.widget
? config
.widget
.$element
: null;
13108 this.$previousFocus
= null;
13109 this.isolated
= !config
.input
;
13110 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
13111 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
13116 .attr( 'role', 'menu' )
13117 .addClass( 'oo-ui-menuSelectWidget' );
13122 OO
.inheritClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.SelectWidget
);
13123 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.ClippableElement
);
13128 * Handles document mouse down events.
13130 * @param {jQuery.Event} e Key down event
13132 OO
.ui
.MenuSelectWidget
.prototype.onDocumentMouseDown = function ( e
) {
13134 !OO
.ui
.contains( this.$element
[ 0 ], e
.target
, true ) &&
13135 ( !this.$widget
|| !OO
.ui
.contains( this.$widget
[ 0 ], e
.target
, true ) )
13137 this.toggle( false );
13142 * Handles key down events.
13144 * @param {jQuery.Event} e Key down event
13146 OO
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
13149 highlightItem
= this.getHighlightedItem();
13151 if ( !this.isDisabled() && this.isVisible() ) {
13152 if ( !highlightItem
) {
13153 highlightItem
= this.getSelectedItem();
13155 switch ( e
.keyCode
) {
13156 case OO
.ui
.Keys
.ENTER
:
13157 this.chooseItem( highlightItem
);
13160 case OO
.ui
.Keys
.UP
:
13161 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
13164 case OO
.ui
.Keys
.DOWN
:
13165 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
13168 case OO
.ui
.Keys
.ESCAPE
:
13169 if ( highlightItem
) {
13170 highlightItem
.setHighlighted( false );
13172 this.toggle( false );
13178 this.highlightItem( nextItem
);
13179 nextItem
.scrollElementIntoView();
13183 e
.preventDefault();
13184 e
.stopPropagation();
13191 * Bind key down listener.
13193 OO
.ui
.MenuSelectWidget
.prototype.bindKeyDownListener = function () {
13194 if ( this.$input
) {
13195 this.$input
.on( 'keydown', this.onKeyDownHandler
);
13197 // Capture menu navigation keys
13198 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
13203 * Unbind key down listener.
13205 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyDownListener = function () {
13206 if ( this.$input
) {
13207 this.$input
.off( 'keydown' );
13209 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
13216 * This will close the menu, unlike #selectItem which only changes selection.
13218 * @param {OO.ui.OptionWidget} item Item to choose
13221 OO
.ui
.MenuSelectWidget
.prototype.chooseItem = function ( item
) {
13222 OO
.ui
.MenuSelectWidget
.super.prototype.chooseItem
.call( this, item
);
13223 this.toggle( false );
13230 OO
.ui
.MenuSelectWidget
.prototype.addItems = function ( items
, index
) {
13234 OO
.ui
.MenuSelectWidget
.super.prototype.addItems
.call( this, items
, index
);
13237 if ( !this.newItems
) {
13238 this.newItems
= [];
13241 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
13243 if ( this.isVisible() ) {
13244 // Defer fitting label until item has been attached
13247 this.newItems
.push( item
);
13251 // Reevaluate clipping
13260 OO
.ui
.MenuSelectWidget
.prototype.removeItems = function ( items
) {
13262 OO
.ui
.MenuSelectWidget
.super.prototype.removeItems
.call( this, items
);
13264 // Reevaluate clipping
13273 OO
.ui
.MenuSelectWidget
.prototype.clearItems = function () {
13275 OO
.ui
.MenuSelectWidget
.super.prototype.clearItems
.call( this );
13277 // Reevaluate clipping
13286 OO
.ui
.MenuSelectWidget
.prototype.toggle = function ( visible
) {
13287 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
13290 change
= visible
!== this.isVisible(),
13291 elementDoc
= this.getElementDocument(),
13292 widgetDoc
= this.$widget
? this.$widget
[ 0 ].ownerDocument
: null;
13295 OO
.ui
.MenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
13299 this.bindKeyDownListener();
13301 // Change focus to enable keyboard navigation
13302 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
13303 this.$previousFocus
= this.$( ':focus' );
13304 this.$input
[ 0 ].focus();
13306 if ( this.newItems
&& this.newItems
.length
) {
13307 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
13308 this.newItems
[ i
].fitLabel();
13310 this.newItems
= null;
13312 this.toggleClipping( true );
13315 if ( this.autoHide
) {
13316 elementDoc
.addEventListener(
13317 'mousedown', this.onDocumentMouseDownHandler
, true
13319 // Support $widget being in a different document
13320 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
13321 widgetDoc
.addEventListener(
13322 'mousedown', this.onDocumentMouseDownHandler
, true
13327 this.unbindKeyDownListener();
13328 if ( this.isolated
&& this.$previousFocus
) {
13329 this.$previousFocus
[ 0 ].focus();
13330 this.$previousFocus
= null;
13332 elementDoc
.removeEventListener(
13333 'mousedown', this.onDocumentMouseDownHandler
, true
13335 // Support $widget being in a different document
13336 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
13337 widgetDoc
.removeEventListener(
13338 'mousedown', this.onDocumentMouseDownHandler
, true
13341 this.toggleClipping( false );
13349 * Menu for a text input widget.
13351 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
13352 * is in a different frame, the menu's position is automatically calculated and maintained when the
13353 * menu is toggled or the window is resized.
13356 * @extends OO.ui.MenuSelectWidget
13359 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
13360 * @param {Object} [config] Configuration options
13361 * @cfg {jQuery} [$container=input.$element] Element to render menu under
13363 OO
.ui
.TextInputMenuSelectWidget
= function OoUiTextInputMenuSelectWidget( input
, config
) {
13364 // Configuration initialization
13365 config
= config
|| {};
13367 // Parent constructor
13368 OO
.ui
.TextInputMenuSelectWidget
.super.call( this, config
);
13371 this.input
= input
;
13372 this.$container
= config
.$container
|| this.input
.$element
;
13373 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
13376 this.$element
.addClass( 'oo-ui-textInputMenuSelectWidget' );
13381 OO
.inheritClass( OO
.ui
.TextInputMenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
13386 * Handle window resize event.
13388 * @param {jQuery.Event} e Window resize event
13390 OO
.ui
.TextInputMenuSelectWidget
.prototype.onWindowResize = function () {
13397 OO
.ui
.TextInputMenuSelectWidget
.prototype.toggle = function ( visible
) {
13398 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
13400 var change
= visible
!== this.isVisible();
13402 if ( change
&& visible
) {
13403 // Make sure the width is set before the parent method runs.
13404 // After this we have to call this.position(); again to actually
13405 // position ourselves correctly.
13410 OO
.ui
.TextInputMenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
13413 if ( this.isVisible() ) {
13415 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
13417 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
13425 * Position the menu.
13429 OO
.ui
.TextInputMenuSelectWidget
.prototype.position = function () {
13430 var $container
= this.$container
,
13431 pos
= OO
.ui
.Element
.static.getRelativePosition( $container
, this.$element
.offsetParent() );
13433 // Position under input
13434 pos
.top
+= $container
.height();
13435 this.$element
.css( pos
);
13438 this.setIdealSize( $container
.width() );
13439 // We updated the position, so re-evaluate the clipping state
13446 * Structured list of items.
13448 * Use with OO.ui.OutlineOptionWidget.
13451 * @extends OO.ui.SelectWidget
13454 * @param {Object} [config] Configuration options
13456 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
13457 // Configuration initialization
13458 config
= config
|| {};
13460 // Parent constructor
13461 OO
.ui
.OutlineSelectWidget
.super.call( this, config
);
13464 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
13469 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
13472 * Switch that slides on and off.
13475 * @extends OO.ui.Widget
13476 * @mixins OO.ui.ToggleWidget
13479 * @param {Object} [config] Configuration options
13480 * @cfg {boolean} [value=false] Initial value
13482 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
13483 // Parent constructor
13484 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
13486 // Mixin constructors
13487 OO
.ui
.ToggleWidget
.call( this, config
);
13490 this.dragging
= false;
13491 this.dragStart
= null;
13492 this.sliding
= false;
13493 this.$glow
= this.$( '<span>' );
13494 this.$grip
= this.$( '<span>' );
13497 this.$element
.on( 'click', this.onClick
.bind( this ) );
13500 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
13501 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
13503 .addClass( 'oo-ui-toggleSwitchWidget' )
13504 .append( this.$glow
, this.$grip
);
13509 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
13510 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
13515 * Handle mouse down events.
13517 * @param {jQuery.Event} e Mouse down event
13519 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
13520 if ( !this.isDisabled() && e
.which
=== 1 ) {
13521 this.setValue( !this.value
);