3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2014-12-06T00:33:09Z
16 * Namespace for all classes, static methods and static properties.
48 * Get the user's language and any fallback languages.
50 * These language codes are used to localize user interface elements in the user's language.
52 * In environments that provide a localization system, this function should be overridden to
53 * return the user's language(s). The default implementation returns English (en) only.
55 * @return {string[]} Language codes, in descending order of priority
57 OO
.ui
.getUserLanguages = function () {
62 * Get a value in an object keyed by language code.
64 * @param {Object.<string,Mixed>} obj Object keyed by language code
65 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
66 * @param {string} [fallback] Fallback code, used if no matching language can be found
67 * @return {Mixed} Local value
69 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
76 // Known user language
77 langs
= OO
.ui
.getUserLanguages();
78 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
85 if ( obj
[fallback
] ) {
88 // First existing language
97 * Check if a node is contained within another node
99 * Similar to jQuery#contains except a list of containers can be supplied
100 * and a boolean argument allows you to include the container in the match list
102 * @param {HTMLElement|HTMLElement[]} containers Container node(s) to search in
103 * @param {HTMLElement} contained Node to find
104 * @param {boolean} [matchContainers] Include the container(s) in the list of nodes to match, otherwise only match descendants
105 * @return {boolean} The node is in the list of target nodes
107 OO
.ui
.contains = function ( containers
, contained
, matchContainers
) {
109 if ( !Array
.isArray( containers
) ) {
110 containers
= [ containers
];
112 for ( i
= containers
.length
- 1; i
>= 0; i
-- ) {
113 if ( ( matchContainers
&& contained
=== containers
[i
] ) || $.contains( containers
[i
], contained
) ) {
122 * Message store for the default implementation of OO.ui.msg
124 * Environments that provide a localization system should not use this, but should override
125 * OO.ui.msg altogether.
130 // Tool tip for a button that moves items in a list down one place
131 'ooui-outline-control-move-down': 'Move item down',
132 // Tool tip for a button that moves items in a list up one place
133 'ooui-outline-control-move-up': 'Move item up',
134 // Tool tip for a button that removes items from a list
135 'ooui-outline-control-remove': 'Remove item',
136 // Label for the toolbar group that contains a list of all other available tools
137 'ooui-toolbar-more': 'More',
138 // Label for the fake tool that expands the full list of tools in a toolbar group
139 'ooui-toolgroup-expand': 'More',
140 // Label for the fake tool that collapses the full list of tools in a toolbar group
141 'ooui-toolgroup-collapse': 'Fewer',
142 // Default label for the accept button of a confirmation dialog
143 'ooui-dialog-message-accept': 'OK',
144 // Default label for the reject button of a confirmation dialog
145 'ooui-dialog-message-reject': 'Cancel',
146 // Title for process dialog error description
147 'ooui-dialog-process-error': 'Something went wrong',
148 // Label for process dialog dismiss error button, visible when describing errors
149 'ooui-dialog-process-dismiss': 'Dismiss',
150 // Label for process dialog retry action button, visible when describing only recoverable errors
151 'ooui-dialog-process-retry': 'Try again',
152 // Label for process dialog retry action button, visible when describing only warnings
153 'ooui-dialog-process-continue': 'Continue'
157 * Get a localized message.
159 * In environments that provide a localization system, this function should be overridden to
160 * return the message translated in the user's language. The default implementation always returns
163 * After the message key, message parameters may optionally be passed. In the default implementation,
164 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
165 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
166 * they support unnamed, ordered message parameters.
169 * @param {string} key Message key
170 * @param {Mixed...} [params] Message parameters
171 * @return {string} Translated message with parameters substituted
173 OO
.ui
.msg = function ( key
) {
174 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
175 if ( typeof message
=== 'string' ) {
176 // Perform $1 substitution
177 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
178 var i
= parseInt( n
, 10 );
179 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
182 // Return placeholder if message not found
183 message
= '[' + key
+ ']';
189 * Package a message and arguments for deferred resolution.
191 * Use this when you are statically specifying a message and the message may not yet be present.
193 * @param {string} key Message key
194 * @param {Mixed...} [params] Message parameters
195 * @return {Function} Function that returns the resolved message when executed
197 OO
.ui
.deferMsg = function () {
198 var args
= arguments
;
200 return OO
.ui
.msg
.apply( OO
.ui
, args
);
207 * If the message is a function it will be executed, otherwise it will pass through directly.
209 * @param {Function|string} msg Deferred message, or message text
210 * @return {string} Resolved message
212 OO
.ui
.resolveMsg = function ( msg
) {
213 if ( $.isFunction( msg
) ) {
222 * Element that can be marked as pending.
228 * @param {Object} [config] Configuration options
229 * @cfg {jQuery} [$pending] Element to mark as pending, defaults to this.$element
231 OO
.ui
.PendingElement
= function OoUiPendingElement( config
) {
232 // Configuration initialization
233 config
= config
|| {};
237 this.$pending
= null;
240 this.setPendingElement( config
.$pending
|| this.$element
);
245 OO
.initClass( OO
.ui
.PendingElement
);
250 * Set the pending element (and clean up any existing one).
252 * @param {jQuery} $pending The element to set to pending.
254 OO
.ui
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
255 if ( this.$pending
) {
256 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
259 this.$pending
= $pending
;
260 if ( this.pending
> 0 ) {
261 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
266 * Check if input is pending.
270 OO
.ui
.PendingElement
.prototype.isPending = function () {
271 return !!this.pending
;
275 * Increase the pending stack.
279 OO
.ui
.PendingElement
.prototype.pushPending = function () {
280 if ( this.pending
=== 0 ) {
281 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
282 this.updateThemeClasses();
290 * Reduce the pending stack.
296 OO
.ui
.PendingElement
.prototype.popPending = function () {
297 if ( this.pending
=== 1 ) {
298 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
299 this.updateThemeClasses();
301 this.pending
= Math
.max( 0, this.pending
- 1 );
311 * @mixins OO.EventEmitter
314 * @param {Object} [config] Configuration options
316 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
317 // Configuration initialization
318 config
= config
|| {};
320 // Mixin constructors
321 OO
.EventEmitter
.call( this );
326 actions
: 'getAction',
330 this.categorized
= {};
333 this.organized
= false;
334 this.changing
= false;
335 this.changed
= false;
340 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
342 /* Static Properties */
345 * Symbolic name of dialog.
352 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
358 * @param {OO.ui.ActionWidget} action Action that was clicked
363 * @param {OO.ui.ActionWidget} action Action that was resized
368 * @param {OO.ui.ActionWidget[]} added Actions added
373 * @param {OO.ui.ActionWidget[]} added Actions removed
383 * Handle action change events.
387 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
388 this.organized
= false;
389 if ( this.changing
) {
392 this.emit( 'change' );
397 * Check if a action is one of the special actions.
399 * @param {OO.ui.ActionWidget} action Action to check
400 * @return {boolean} Action is special
402 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
405 for ( flag
in this.special
) {
406 if ( action
=== this.special
[flag
] ) {
417 * @param {Object} [filters] Filters to use, omit to get all actions
418 * @param {string|string[]} [filters.actions] Actions that actions must have
419 * @param {string|string[]} [filters.flags] Flags that actions must have
420 * @param {string|string[]} [filters.modes] Modes that actions must have
421 * @param {boolean} [filters.visible] Actions must be visible
422 * @param {boolean} [filters.disabled] Actions must be disabled
423 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
425 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
426 var i
, len
, list
, category
, actions
, index
, match
, matches
;
431 // Collect category candidates
433 for ( category
in this.categorized
) {
434 list
= filters
[category
];
436 if ( !Array
.isArray( list
) ) {
439 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
440 actions
= this.categorized
[category
][list
[i
]];
441 if ( Array
.isArray( actions
) ) {
442 matches
.push
.apply( matches
, actions
);
447 // Remove by boolean filters
448 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
451 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
452 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
454 matches
.splice( i
, 1 );
460 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
462 index
= matches
.lastIndexOf( match
);
463 while ( index
!== i
) {
464 matches
.splice( index
, 1 );
466 index
= matches
.lastIndexOf( match
);
471 return this.list
.slice();
475 * Get special actions.
477 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
478 * Special flags can be configured by changing #static-specialFlags in a subclass.
480 * @return {OO.ui.ActionWidget|null} Safe action
482 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
484 return $.extend( {}, this.special
);
490 * Other actions include all non-special visible actions.
492 * @return {OO.ui.ActionWidget[]} Other actions
494 OO
.ui
.ActionSet
.prototype.getOthers = function () {
496 return this.others
.slice();
500 * Toggle actions based on their modes.
502 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
503 * visibility; matching actions will be shown, non-matching actions will be hidden.
505 * @param {string} mode Mode actions must have
510 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
513 this.changing
= true;
514 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
515 action
= this.list
[i
];
516 action
.toggle( action
.hasMode( mode
) );
519 this.organized
= false;
520 this.changing
= false;
521 this.emit( 'change' );
527 * Change which actions are able to be performed.
529 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
531 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
532 * indicate actions are able to be performed
535 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
536 var i
, len
, action
, item
;
538 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
540 action
= item
.getAction();
541 if ( actions
[action
] !== undefined ) {
542 item
.setDisabled( !actions
[action
] );
550 * Executes a function once per action.
552 * When making changes to multiple actions, use this method instead of iterating over the actions
553 * manually to defer emitting a change event until after all actions have been changed.
555 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
556 * @param {Function} callback Callback to run for each action; callback is invoked with three
557 * arguments: the action, the action's index, the list of actions being iterated over
560 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
561 this.changed
= false;
562 this.changing
= true;
563 this.get( filter
).forEach( callback
);
564 this.changing
= false;
565 if ( this.changed
) {
566 this.emit( 'change' );
575 * @param {OO.ui.ActionWidget[]} actions Actions to add
580 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
583 this.changing
= true;
584 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
586 action
.connect( this, {
587 click
: [ 'emit', 'click', action
],
588 resize
: [ 'emit', 'resize', action
],
589 toggle
: [ 'onActionChange' ]
591 this.list
.push( action
);
593 this.organized
= false;
594 this.emit( 'add', actions
);
595 this.changing
= false;
596 this.emit( 'change' );
604 * @param {OO.ui.ActionWidget[]} actions Actions to remove
609 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
610 var i
, len
, index
, action
;
612 this.changing
= true;
613 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
615 index
= this.list
.indexOf( action
);
616 if ( index
!== -1 ) {
617 action
.disconnect( this );
618 this.list
.splice( index
, 1 );
621 this.organized
= false;
622 this.emit( 'remove', actions
);
623 this.changing
= false;
624 this.emit( 'change' );
630 * Remove all actions.
636 OO
.ui
.ActionSet
.prototype.clear = function () {
638 removed
= this.list
.slice();
640 this.changing
= true;
641 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
642 action
= this.list
[i
];
643 action
.disconnect( this );
648 this.organized
= false;
649 this.emit( 'remove', removed
);
650 this.changing
= false;
651 this.emit( 'change' );
659 * This is called whenever organized information is requested. It will only reorganize the actions
660 * if something has changed since the last time it ran.
665 OO
.ui
.ActionSet
.prototype.organize = function () {
666 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
667 specialFlags
= this.constructor.static.specialFlags
;
669 if ( !this.organized
) {
670 this.categorized
= {};
673 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
674 action
= this.list
[i
];
675 if ( action
.isVisible() ) {
676 // Populate categories
677 for ( category
in this.categories
) {
678 if ( !this.categorized
[category
] ) {
679 this.categorized
[category
] = {};
681 list
= action
[this.categories
[category
]]();
682 if ( !Array
.isArray( list
) ) {
685 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
687 if ( !this.categorized
[category
][item
] ) {
688 this.categorized
[category
][item
] = [];
690 this.categorized
[category
][item
].push( action
);
693 // Populate special/others
695 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
696 flag
= specialFlags
[j
];
697 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
698 this.special
[flag
] = action
;
704 this.others
.push( action
);
708 this.organized
= true;
715 * DOM element abstraction.
721 * @param {Object} [config] Configuration options
722 * @cfg {Function} [$] jQuery for the frame the widget is in
723 * @cfg {string[]} [classes] CSS class names to add
724 * @cfg {string} [text] Text to insert
725 * @cfg {jQuery} [$content] Content elements to append (after text)
726 * @cfg {Mixed} [data] Element data
728 OO
.ui
.Element
= function OoUiElement( config
) {
729 // Configuration initialization
730 config
= config
|| {};
733 this.$ = config
.$ || OO
.ui
.Element
.static.getJQuery( document
);
734 this.data
= config
.data
;
735 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
736 this.elementGroup
= null;
737 this.debouncedUpdateThemeClassesHandler
= this.debouncedUpdateThemeClasses
.bind( this );
738 this.updateThemeClassesPending
= false;
741 if ( $.isArray( config
.classes
) ) {
742 this.$element
.addClass( config
.classes
.join( ' ' ) );
745 this.$element
.text( config
.text
);
747 if ( config
.$content
) {
748 this.$element
.append( config
.$content
);
754 OO
.initClass( OO
.ui
.Element
);
756 /* Static Properties */
761 * This may be ignored if #getTagName is overridden.
767 OO
.ui
.Element
.static.tagName
= 'div';
772 * Get a jQuery function within a specific document.
775 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
776 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
778 * @return {Function} Bound jQuery function
780 OO
.ui
.Element
.static.getJQuery = function ( context
, $iframe
) {
781 function wrapper( selector
) {
782 return $( selector
, wrapper
.context
);
785 wrapper
.context
= this.getDocument( context
);
788 wrapper
.$iframe
= $iframe
;
795 * Get the document of an element.
798 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
799 * @return {HTMLDocument|null} Document object
801 OO
.ui
.Element
.static.getDocument = function ( obj
) {
802 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
803 return ( obj
[0] && obj
[0].ownerDocument
) ||
804 // Empty jQuery selections might have a context
811 ( obj
.nodeType
=== 9 && obj
) ||
816 * Get the window of an element or document.
819 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
820 * @return {Window} Window object
822 OO
.ui
.Element
.static.getWindow = function ( obj
) {
823 var doc
= this.getDocument( obj
);
824 return doc
.parentWindow
|| doc
.defaultView
;
828 * Get the direction of an element or document.
831 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
832 * @return {string} Text direction, either 'ltr' or 'rtl'
834 OO
.ui
.Element
.static.getDir = function ( obj
) {
837 if ( obj
instanceof jQuery
) {
840 isDoc
= obj
.nodeType
=== 9;
841 isWin
= obj
.document
!== undefined;
842 if ( isDoc
|| isWin
) {
848 return $( obj
).css( 'direction' );
852 * Get the offset between two frames.
854 * TODO: Make this function not use recursion.
857 * @param {Window} from Window of the child frame
858 * @param {Window} [to=window] Window of the parent frame
859 * @param {Object} [offset] Offset to start with, used internally
860 * @return {Object} Offset object, containing left and top properties
862 OO
.ui
.Element
.static.getFrameOffset = function ( from, to
, offset
) {
863 var i
, len
, frames
, frame
, rect
;
869 offset
= { top
: 0, left
: 0 };
871 if ( from.parent
=== from ) {
875 // Get iframe element
876 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
877 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
878 if ( frames
[i
].contentWindow
=== from ) {
884 // Recursively accumulate offset values
886 rect
= frame
.getBoundingClientRect();
887 offset
.left
+= rect
.left
;
888 offset
.top
+= rect
.top
;
890 this.getFrameOffset( from.parent
, offset
);
897 * Get the offset between two elements.
899 * The two elements may be in a different frame, but in that case the frame $element is in must
900 * be contained in the frame $anchor is in.
903 * @param {jQuery} $element Element whose position to get
904 * @param {jQuery} $anchor Element to get $element's position relative to
905 * @return {Object} Translated position coordinates, containing top and left properties
907 OO
.ui
.Element
.static.getRelativePosition = function ( $element
, $anchor
) {
908 var iframe
, iframePos
,
909 pos
= $element
.offset(),
910 anchorPos
= $anchor
.offset(),
911 elementDocument
= this.getDocument( $element
),
912 anchorDocument
= this.getDocument( $anchor
);
914 // If $element isn't in the same document as $anchor, traverse up
915 while ( elementDocument
!== anchorDocument
) {
916 iframe
= elementDocument
.defaultView
.frameElement
;
918 throw new Error( '$element frame is not contained in $anchor frame' );
920 iframePos
= $( iframe
).offset();
921 pos
.left
+= iframePos
.left
;
922 pos
.top
+= iframePos
.top
;
923 elementDocument
= iframe
.ownerDocument
;
925 pos
.left
-= anchorPos
.left
;
926 pos
.top
-= anchorPos
.top
;
931 * Get element border sizes.
934 * @param {HTMLElement} el Element to measure
935 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
937 OO
.ui
.Element
.static.getBorders = function ( el
) {
938 var doc
= el
.ownerDocument
,
939 win
= doc
.parentWindow
|| doc
.defaultView
,
940 style
= win
&& win
.getComputedStyle
?
941 win
.getComputedStyle( el
, null ) :
944 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
945 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
946 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
947 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
950 top
: Math
.round( top
),
951 left
: Math
.round( left
),
952 bottom
: Math
.round( bottom
),
953 right
: Math
.round( right
)
958 * Get dimensions of an element or window.
961 * @param {HTMLElement|Window} el Element to measure
962 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
964 OO
.ui
.Element
.static.getDimensions = function ( el
) {
966 doc
= el
.ownerDocument
|| el
.document
,
967 win
= doc
.parentWindow
|| doc
.defaultView
;
969 if ( win
=== el
|| el
=== doc
.documentElement
) {
972 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
974 top
: $win
.scrollTop(),
975 left
: $win
.scrollLeft()
977 scrollbar
: { right
: 0, bottom
: 0 },
981 bottom
: $win
.innerHeight(),
982 right
: $win
.innerWidth()
988 borders
: this.getBorders( el
),
990 top
: $el
.scrollTop(),
991 left
: $el
.scrollLeft()
994 right
: $el
.innerWidth() - el
.clientWidth
,
995 bottom
: $el
.innerHeight() - el
.clientHeight
997 rect
: el
.getBoundingClientRect()
1003 * Get closest scrollable container.
1005 * Traverses up until either a scrollable element or the root is reached, in which case the window
1009 * @param {HTMLElement} el Element to find scrollable container for
1010 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
1011 * @return {HTMLElement} Closest scrollable container
1013 OO
.ui
.Element
.static.getClosestScrollableContainer = function ( el
, dimension
) {
1015 props
= [ 'overflow' ],
1016 $parent
= $( el
).parent();
1018 if ( dimension
=== 'x' || dimension
=== 'y' ) {
1019 props
.push( 'overflow-' + dimension
);
1022 while ( $parent
.length
) {
1023 if ( $parent
[0] === el
.ownerDocument
.body
) {
1028 val
= $parent
.css( props
[i
] );
1029 if ( val
=== 'auto' || val
=== 'scroll' ) {
1033 $parent
= $parent
.parent();
1035 return this.getDocument( el
).body
;
1039 * Scroll element into view.
1042 * @param {HTMLElement} el Element to scroll into view
1043 * @param {Object} [config] Configuration options
1044 * @param {string} [config.duration] jQuery animation duration value
1045 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
1046 * to scroll in both directions
1047 * @param {Function} [config.complete] Function to call when scrolling completes
1049 OO
.ui
.Element
.static.scrollIntoView = function ( el
, config
) {
1050 // Configuration initialization
1051 config
= config
|| {};
1054 callback
= typeof config
.complete
=== 'function' && config
.complete
,
1055 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
1057 eld
= this.getDimensions( el
),
1058 scd
= this.getDimensions( sc
),
1059 $win
= $( this.getWindow( el
) );
1061 // Compute the distances between the edges of el and the edges of the scroll viewport
1062 if ( $sc
.is( 'body' ) ) {
1063 // If the scrollable container is the <body> this is easy
1066 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
1067 left
: eld
.rect
.left
,
1068 right
: $win
.innerWidth() - eld
.rect
.right
1071 // Otherwise, we have to subtract el's coordinates from sc's coordinates
1073 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
1074 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
1075 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
1076 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
1080 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1081 if ( rel
.top
< 0 ) {
1082 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
1083 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
1084 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
1087 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1088 if ( rel
.left
< 0 ) {
1089 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
1090 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
1091 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
1094 if ( !$.isEmptyObject( anim
) ) {
1095 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
1097 $sc
.queue( function ( next
) {
1114 * @return {Mixed} Element data
1116 OO
.ui
.Element
.prototype.getData = function () {
1123 * @param {Mixed} Element data
1126 OO
.ui
.Element
.prototype.setData = function ( data
) {
1132 * Check if element supports one or more methods.
1134 * @param {string|string[]} methods Method or list of methods to check
1135 * @return {boolean} All methods are supported
1137 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1141 methods
= $.isArray( methods
) ? methods
: [ methods
];
1142 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1143 if ( $.isFunction( this[methods
[i
]] ) ) {
1148 return methods
.length
=== support
;
1152 * Update the theme-provided classes.
1154 * @localdoc This is called in element mixins and widget classes any time state changes.
1155 * Updating is debounced, minimizing overhead of changing multiple attributes and
1156 * guaranteeing that theme updates do not occur within an element's constructor
1158 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1159 if ( !this.updateThemeClassesPending
) {
1160 this.updateThemeClassesPending
= true;
1161 setTimeout( this.debouncedUpdateThemeClassesHandler
);
1168 OO
.ui
.Element
.prototype.debouncedUpdateThemeClasses = function () {
1169 OO
.ui
.theme
.updateElementClasses( this );
1170 this.updateThemeClassesPending
= false;
1174 * Get the HTML tag name.
1176 * Override this method to base the result on instance information.
1178 * @return {string} HTML tag name
1180 OO
.ui
.Element
.prototype.getTagName = function () {
1181 return this.constructor.static.tagName
;
1185 * Check if the element is attached to the DOM
1186 * @return {boolean} The element is attached to the DOM
1188 OO
.ui
.Element
.prototype.isElementAttached = function () {
1189 return $.contains( this.getElementDocument(), this.$element
[0] );
1193 * Get the DOM document.
1195 * @return {HTMLDocument} Document object
1197 OO
.ui
.Element
.prototype.getElementDocument = function () {
1198 return OO
.ui
.Element
.static.getDocument( this.$element
);
1202 * Get the DOM window.
1204 * @return {Window} Window object
1206 OO
.ui
.Element
.prototype.getElementWindow = function () {
1207 return OO
.ui
.Element
.static.getWindow( this.$element
);
1211 * Get closest scrollable container.
1213 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1214 return OO
.ui
.Element
.static.getClosestScrollableContainer( this.$element
[0] );
1218 * Get group element is in.
1220 * @return {OO.ui.GroupElement|null} Group element, null if none
1222 OO
.ui
.Element
.prototype.getElementGroup = function () {
1223 return this.elementGroup
;
1227 * Set group element is in.
1229 * @param {OO.ui.GroupElement|null} group Group element, null if none
1232 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1233 this.elementGroup
= group
;
1238 * Scroll element into view.
1240 * @param {Object} [config] Configuration options
1242 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1243 return OO
.ui
.Element
.static.scrollIntoView( this.$element
[0], config
);
1247 * Container for elements.
1251 * @extends OO.ui.Element
1252 * @mixins OO.EventEmitter
1255 * @param {Object} [config] Configuration options
1257 OO
.ui
.Layout
= function OoUiLayout( config
) {
1258 // Configuration initialization
1259 config
= config
|| {};
1261 // Parent constructor
1262 OO
.ui
.Layout
.super.call( this, config
);
1264 // Mixin constructors
1265 OO
.EventEmitter
.call( this );
1268 this.$element
.addClass( 'oo-ui-layout' );
1273 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1274 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1277 * User interface control.
1281 * @extends OO.ui.Element
1282 * @mixins OO.EventEmitter
1285 * @param {Object} [config] Configuration options
1286 * @cfg {boolean} [disabled=false] Disable
1288 OO
.ui
.Widget
= function OoUiWidget( config
) {
1289 // Initialize config
1290 config
= $.extend( { disabled
: false }, config
);
1292 // Parent constructor
1293 OO
.ui
.Widget
.super.call( this, config
);
1295 // Mixin constructors
1296 OO
.EventEmitter
.call( this );
1299 this.visible
= true;
1300 this.disabled
= null;
1301 this.wasDisabled
= null;
1304 this.$element
.addClass( 'oo-ui-widget' );
1305 this.setDisabled( !!config
.disabled
);
1310 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1311 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1317 * @param {boolean} disabled Widget is disabled
1322 * @param {boolean} visible Widget is visible
1328 * Check if the widget is disabled.
1330 * @return {boolean} Button is disabled
1332 OO
.ui
.Widget
.prototype.isDisabled = function () {
1333 return this.disabled
;
1337 * Check if widget is visible.
1339 * @return {boolean} Widget is visible
1341 OO
.ui
.Widget
.prototype.isVisible = function () {
1342 return this.visible
;
1346 * Set the disabled state of the widget.
1348 * This should probably change the widgets' appearance and prevent it from being used.
1350 * @param {boolean} disabled Disable widget
1353 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1356 this.disabled
= !!disabled
;
1357 isDisabled
= this.isDisabled();
1358 if ( isDisabled
!== this.wasDisabled
) {
1359 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1360 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1361 this.emit( 'disable', isDisabled
);
1362 this.updateThemeClasses();
1364 this.wasDisabled
= isDisabled
;
1370 * Toggle visibility of widget.
1372 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1376 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1377 show
= show
=== undefined ? !this.visible
: !!show
;
1379 if ( show
!== this.isVisible() ) {
1380 this.visible
= show
;
1381 this.$element
.toggle( show
);
1382 this.emit( 'toggle', show
);
1389 * Update the disabled state, in case of changes in parent widget.
1393 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1394 this.setDisabled( this.disabled
);
1399 * Container for elements in a child frame.
1401 * Use together with OO.ui.WindowManager.
1405 * @extends OO.ui.Element
1406 * @mixins OO.EventEmitter
1408 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1409 * teardown processes are executed when the window is closed.
1411 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1412 * - Window manager begins opening window
1413 * - {@link #getSetupProcess} method is called and its result executed
1414 * - {@link #getReadyProcess} method is called and its result executed
1415 * - Window is now open
1417 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1418 * - Window manager begins closing window
1419 * - {@link #getHoldProcess} method is called and its result executed
1420 * - {@link #getTeardownProcess} method is called and its result executed
1421 * - Window is now closed
1423 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1424 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1425 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchronous
1426 * processing can complete. Always assume window processes are executed asynchronously. See
1427 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1428 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1430 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1431 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1434 * @param {Object} [config] Configuration options
1435 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1438 OO
.ui
.Window
= function OoUiWindow( config
) {
1439 // Configuration initialization
1440 config
= config
|| {};
1442 // Parent constructor
1443 OO
.ui
.Window
.super.call( this, config
);
1445 // Mixin constructors
1446 OO
.EventEmitter
.call( this );
1449 this.manager
= null;
1450 this.initialized
= false;
1451 this.visible
= false;
1452 this.opening
= null;
1453 this.closing
= null;
1456 this.loading
= null;
1457 this.size
= config
.size
|| this.constructor.static.size
;
1458 this.$frame
= this.$( '<div>' );
1459 this.$overlay
= this.$( '<div>' );
1463 .addClass( 'oo-ui-window' )
1464 .append( this.$frame
, this.$overlay
);
1465 this.$frame
.addClass( 'oo-ui-window-frame' );
1466 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1468 // NOTE: Additional initialization will occur when #setManager is called
1473 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1474 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1476 /* Static Properties */
1479 * Symbolic name of size.
1481 * Size is used if no size is configured during construction.
1485 * @property {string}
1487 OO
.ui
.Window
.static.size
= 'medium';
1489 /* Static Methods */
1492 * Transplant the CSS styles from as parent document to a frame's document.
1494 * This loops over the style sheets in the parent document, and copies their nodes to the
1495 * frame's document. It then polls the document to see when all styles have loaded, and once they
1496 * have, resolves the promise.
1498 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1499 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1500 * Firefox, where the styles won't load until the iframe becomes visible.
1502 * For details of how we arrived at the strategy used in this function, see #load.
1506 * @param {HTMLDocument} parentDoc Document to transplant styles from
1507 * @param {HTMLDocument} frameDoc Document to transplant styles to
1508 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1509 * @return {jQuery.Promise} Promise resolved when styles have loaded
1511 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1512 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1513 $pollNodes
= $( [] ),
1514 // Fake font-family value
1515 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1516 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1517 deferred
= $.Deferred();
1519 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1520 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1521 if ( styleNode
.disabled
) {
1525 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1526 // External stylesheet; use @import
1527 styleText
= '@import url(' + styleNode
.href
+ ');';
1529 // Internal stylesheet; just copy the text
1530 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1531 // other browsers, so fall back to '' rather than 'undefined'
1532 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1535 // Create a node with a unique ID that we're going to monitor to see when the CSS
1537 if ( styleNode
.oouiFrameTransplantStylesId
) {
1538 // If we're nesting transplantStyles operations and this node already has
1539 // a CSS rule to wait for loading, reuse it
1540 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1542 // Otherwise, create a new ID
1543 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1546 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1547 // The font-family rule will only take effect once the @import finishes
1548 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1551 // Create a node with id=pollNodeId
1552 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1553 .attr( 'id', pollNodeId
)
1554 .appendTo( frameDoc
.body
)
1557 // Add our modified CSS as a <style> tag
1558 newNode
= frameDoc
.createElement( 'style' );
1559 newNode
.textContent
= styleText
;
1560 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1561 frameDoc
.head
.appendChild( newNode
);
1563 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1565 // Poll every 100ms until all external stylesheets have loaded
1566 $pendingPollNodes
= $pollNodes
;
1567 timeoutID
= setTimeout( function pollExternalStylesheets() {
1569 $pendingPollNodes
.length
> 0 &&
1570 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1572 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1575 if ( $pendingPollNodes
.length
=== 0 ) {
1577 if ( timeoutID
!== null ) {
1579 $pollNodes
.remove();
1583 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1586 // ...but give up after a while
1587 if ( timeout
!== 0 ) {
1588 setTimeout( function () {
1590 clearTimeout( timeoutID
);
1592 $pollNodes
.remove();
1595 }, timeout
|| 5000 );
1598 return deferred
.promise();
1604 * Handle mouse down events.
1606 * @param {jQuery.Event} e Mouse down event
1608 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1609 // Prevent clicking on the click-block from stealing focus
1610 if ( e
.target
=== this.$element
[0] ) {
1616 * Check if window has been initialized.
1618 * @return {boolean} Window has been initialized
1620 OO
.ui
.Window
.prototype.isInitialized = function () {
1621 return this.initialized
;
1625 * Check if window is visible.
1627 * @return {boolean} Window is visible
1629 OO
.ui
.Window
.prototype.isVisible = function () {
1630 return this.visible
;
1634 * Check if window is loading.
1636 * @return {boolean} Window is loading
1638 OO
.ui
.Window
.prototype.isLoading = function () {
1639 return this.loading
&& this.loading
.state() === 'pending';
1643 * Check if window is loaded.
1645 * @return {boolean} Window is loaded
1647 OO
.ui
.Window
.prototype.isLoaded = function () {
1648 return this.loading
&& this.loading
.state() === 'resolved';
1652 * Check if window is opening.
1654 * This is a wrapper around OO.ui.WindowManager#isOpening.
1656 * @return {boolean} Window is opening
1658 OO
.ui
.Window
.prototype.isOpening = function () {
1659 return this.manager
.isOpening( this );
1663 * Check if window is closing.
1665 * This is a wrapper around OO.ui.WindowManager#isClosing.
1667 * @return {boolean} Window is closing
1669 OO
.ui
.Window
.prototype.isClosing = function () {
1670 return this.manager
.isClosing( this );
1674 * Check if window is opened.
1676 * This is a wrapper around OO.ui.WindowManager#isOpened.
1678 * @return {boolean} Window is opened
1680 OO
.ui
.Window
.prototype.isOpened = function () {
1681 return this.manager
.isOpened( this );
1685 * Get the window manager.
1687 * @return {OO.ui.WindowManager} Manager of window
1689 OO
.ui
.Window
.prototype.getManager = function () {
1690 return this.manager
;
1694 * Get the window size.
1696 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1698 OO
.ui
.Window
.prototype.getSize = function () {
1703 * Disable transitions on window's frame for the duration of the callback function, then enable them
1707 * @param {Function} callback Function to call while transitions are disabled
1709 OO
.ui
.Window
.prototype.withoutSizeTransitions = function ( callback
) {
1710 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1711 // Disable transitions first, otherwise we'll get values from when the window was animating.
1713 styleObj
= this.$frame
[0].style
;
1714 oldTransition
= styleObj
.transition
|| styleObj
.OTransition
|| styleObj
.MsTransition
||
1715 styleObj
.MozTransition
|| styleObj
.WebkitTransition
;
1716 styleObj
.transition
= styleObj
.OTransition
= styleObj
.MsTransition
=
1717 styleObj
.MozTransition
= styleObj
.WebkitTransition
= 'none';
1719 // Force reflow to make sure the style changes done inside callback really are not transitioned
1720 this.$frame
.height();
1721 styleObj
.transition
= styleObj
.OTransition
= styleObj
.MsTransition
=
1722 styleObj
.MozTransition
= styleObj
.WebkitTransition
= oldTransition
;
1726 * Get the height of the dialog contents.
1728 * @return {number} Content height
1730 OO
.ui
.Window
.prototype.getContentHeight = function () {
1733 styleObj
= this.$frame
[0].style
;
1735 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements.
1736 // Disable transitions first, otherwise we'll get values from when the window was animating.
1737 this.withoutSizeTransitions( function () {
1738 var oldHeight
= styleObj
.height
;
1739 styleObj
.height
= '1px';
1740 bodyHeight
= win
.getBodyHeight();
1741 styleObj
.height
= oldHeight
;
1745 // Add buffer for border
1746 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1747 // Use combined heights of children
1748 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1753 * Get the height of the dialog contents.
1755 * When this function is called, the dialog will temporarily have been resized
1756 * to height=1px, so .scrollHeight measurements can be taken accurately.
1758 * @return {number} Height of content
1760 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1761 return this.$body
[0].scrollHeight
;
1765 * Get the directionality of the frame
1767 * @return {string} Directionality, 'ltr' or 'rtl'
1769 OO
.ui
.Window
.prototype.getDir = function () {
1774 * Get a process for setting up a window for use.
1776 * Each time the window is opened this process will set it up for use in a particular context, based
1777 * on the `data` argument.
1779 * When you override this method, you can add additional setup steps to the process the parent
1780 * method provides using the 'first' and 'next' methods.
1783 * @param {Object} [data] Window opening data
1784 * @return {OO.ui.Process} Setup process
1786 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1787 return new OO
.ui
.Process();
1791 * Get a process for readying a window for use.
1793 * Each time the window is open and setup, this process will ready it up for use in a particular
1794 * context, based on the `data` argument.
1796 * When you override this method, you can add additional setup steps to the process the parent
1797 * method provides using the 'first' and 'next' methods.
1800 * @param {Object} [data] Window opening data
1801 * @return {OO.ui.Process} Setup process
1803 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1804 return new OO
.ui
.Process();
1808 * Get a process for holding a window from use.
1810 * Each time the window is closed, this process will hold it from use in a particular context, based
1811 * on the `data` argument.
1813 * When you override this method, you can add additional setup steps to the process the parent
1814 * method provides using the 'first' and 'next' methods.
1817 * @param {Object} [data] Window closing data
1818 * @return {OO.ui.Process} Hold process
1820 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1821 return new OO
.ui
.Process();
1825 * Get a process for tearing down a window after use.
1827 * Each time the window is closed this process will tear it down and do something with the user's
1828 * interactions within the window, based on the `data` argument.
1830 * When you override this method, you can add additional teardown steps to the process the parent
1831 * method provides using the 'first' and 'next' methods.
1834 * @param {Object} [data] Window closing data
1835 * @return {OO.ui.Process} Teardown process
1837 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1838 return new OO
.ui
.Process();
1842 * Toggle visibility of window.
1844 * If the window is isolated and hasn't fully loaded yet, the visibility property will be used
1845 * instead of display.
1847 * @param {boolean} [show] Make window visible, omit to toggle visibility
1851 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1852 show
= show
=== undefined ? !this.visible
: !!show
;
1854 if ( show
!== this.isVisible() ) {
1855 this.visible
= show
;
1857 if ( this.isolated
&& !this.isLoaded() ) {
1858 // Hide the window using visibility instead of display until loading is complete
1859 // Can't use display: none; because that prevents the iframe from loading in Firefox
1860 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1862 this.$element
.toggle( show
).css( 'visibility', '' );
1864 this.emit( 'toggle', show
);
1871 * Set the window manager.
1873 * This must be called before initialize. Calling it more than once will cause an error.
1875 * @param {OO.ui.WindowManager} manager Manager for this window
1876 * @throws {Error} If called more than once
1879 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1880 if ( this.manager
) {
1881 throw new Error( 'Cannot set window manager, window already has a manager' );
1885 this.manager
= manager
;
1886 this.isolated
= manager
.shouldIsolate();
1889 if ( this.isolated
) {
1890 this.$iframe
= this.$( '<iframe>' );
1891 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1892 this.$frame
.append( this.$iframe
);
1893 this.$ = function () {
1894 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1896 // WARNING: Do not use this.$ again until #initialize is called
1898 this.$content
= this.$( '<div>' );
1899 this.$document
= $( this.getElementDocument() );
1900 this.$content
.addClass( 'oo-ui-window-content' );
1901 this.$frame
.append( this.$content
);
1903 this.toggle( false );
1905 // Figure out directionality:
1906 this.dir
= OO
.ui
.Element
.static.getDir( this.$iframe
|| this.$content
) || 'ltr';
1912 * Set the window size.
1914 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1917 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1919 this.manager
.updateWindowSize( this );
1924 * Set window dimensions.
1926 * Properties are applied to the frame container.
1928 * @param {Object} dim CSS dimension properties
1929 * @param {string|number} [dim.width] Width
1930 * @param {string|number} [dim.minWidth] Minimum width
1931 * @param {string|number} [dim.maxWidth] Maximum width
1932 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1933 * @param {string|number} [dim.minWidth] Minimum height
1934 * @param {string|number} [dim.maxWidth] Maximum height
1937 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1940 styleObj
= this.$frame
[0].style
;
1942 // Calculate the height we need to set using the correct width
1943 if ( dim
.height
=== undefined ) {
1944 this.withoutSizeTransitions( function () {
1945 var oldWidth
= styleObj
.width
;
1946 win
.$frame
.css( 'width', dim
.width
|| '' );
1947 height
= win
.getContentHeight();
1948 styleObj
.width
= oldWidth
;
1951 height
= dim
.height
;
1955 width
: dim
.width
|| '',
1956 minWidth
: dim
.minWidth
|| '',
1957 maxWidth
: dim
.maxWidth
|| '',
1958 height
: height
|| '',
1959 minHeight
: dim
.minHeight
|| '',
1960 maxHeight
: dim
.maxHeight
|| ''
1967 * Initialize window contents.
1969 * The first time the window is opened, #initialize is called when it's safe to begin populating
1970 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1972 * Once this method is called, this.$ can be used to create elements within the frame.
1974 * @throws {Error} If not attached to a manager
1977 OO
.ui
.Window
.prototype.initialize = function () {
1978 if ( !this.manager
) {
1979 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1983 this.$head
= this.$( '<div>' );
1984 this.$body
= this.$( '<div>' );
1985 this.$foot
= this.$( '<div>' );
1986 this.$innerOverlay
= this.$( '<div>' );
1989 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
1992 this.$head
.addClass( 'oo-ui-window-head' );
1993 this.$body
.addClass( 'oo-ui-window-body' );
1994 this.$foot
.addClass( 'oo-ui-window-foot' );
1995 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
1996 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
2004 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
2005 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
2007 * @param {Object} [data] Window opening data
2008 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
2009 * first argument will be a promise which will be resolved when the window begins closing
2011 OO
.ui
.Window
.prototype.open = function ( data
) {
2012 return this.manager
.openWindow( this, data
);
2018 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
2019 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
2021 * @param {Object} [data] Window closing data
2022 * @return {jQuery.Promise} Promise resolved when window is closed
2024 OO
.ui
.Window
.prototype.close = function ( data
) {
2025 return this.manager
.closeWindow( this, data
);
2031 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2034 * @param {Object} [data] Window opening data
2035 * @return {jQuery.Promise} Promise resolved when window is setup
2037 OO
.ui
.Window
.prototype.setup = function ( data
) {
2039 deferred
= $.Deferred();
2041 this.$element
.show();
2042 this.visible
= true;
2043 this.getSetupProcess( data
).execute().done( function () {
2044 // Force redraw by asking the browser to measure the elements' widths
2045 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2046 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2050 return deferred
.promise();
2056 * This is called by OO.ui.WindowManager during window opening, and should not be called directly
2059 * @param {Object} [data] Window opening data
2060 * @return {jQuery.Promise} Promise resolved when window is ready
2062 OO
.ui
.Window
.prototype.ready = function ( data
) {
2064 deferred
= $.Deferred();
2066 this.$content
.focus();
2067 this.getReadyProcess( data
).execute().done( function () {
2068 // Force redraw by asking the browser to measure the elements' widths
2069 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2070 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2074 return deferred
.promise();
2080 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2083 * @param {Object} [data] Window closing data
2084 * @return {jQuery.Promise} Promise resolved when window is held
2086 OO
.ui
.Window
.prototype.hold = function ( data
) {
2088 deferred
= $.Deferred();
2090 this.getHoldProcess( data
).execute().done( function () {
2091 // Get the focused element within the window's content
2092 var $focus
= win
.$content
.find( OO
.ui
.Element
.static.getDocument( win
.$content
).activeElement
);
2094 // Blur the focused element
2095 if ( $focus
.length
) {
2099 // Force redraw by asking the browser to measure the elements' widths
2100 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2101 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2105 return deferred
.promise();
2111 * This is called by OO.ui.WindowManager during window closing, and should not be called directly
2114 * @param {Object} [data] Window closing data
2115 * @return {jQuery.Promise} Promise resolved when window is torn down
2117 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2119 deferred
= $.Deferred();
2121 this.getTeardownProcess( data
).execute().done( function () {
2122 // Force redraw by asking the browser to measure the elements' widths
2123 win
.$element
.removeClass( 'oo-ui-window-load oo-ui-window-setup' ).width();
2124 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2125 win
.$element
.hide();
2126 win
.visible
= false;
2130 return deferred
.promise();
2134 * Load the frame contents.
2136 * Once the iframe's stylesheets are loaded the returned promise will be resolved. Calling while
2137 * loading will return a promise but not trigger a new loading cycle. Calling after loading is
2138 * complete will return a promise that's already been resolved.
2140 * Sounds simple right? Read on...
2142 * When you create a dynamic iframe using open/write/close, the window.load event for the
2143 * iframe is triggered when you call close, and there's no further load event to indicate that
2144 * everything is actually loaded.
2146 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2147 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2148 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2149 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2150 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2152 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2153 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2154 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2155 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2156 * accessing .cssRules is allowed.
2158 * However, now that we control the styles we're injecting, we might as well do away with
2159 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2160 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2161 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2162 * font-family rule is not applied until after the `@import` finishes.
2164 * All this stylesheet injection and polling magic is in #transplantStyles.
2166 * @return {jQuery.Promise} Promise resolved when loading is complete
2168 OO
.ui
.Window
.prototype.load = function () {
2169 var sub
, doc
, loading
,
2172 this.$element
.addClass( 'oo-ui-window-load' );
2174 // Non-isolated windows are already "loaded"
2175 if ( !this.loading
&& !this.isolated
) {
2176 this.loading
= $.Deferred().resolve();
2178 // Set initialized state after so sub-classes aren't confused by it being set by calling
2179 // their parent initialize method
2180 this.initialized
= true;
2183 // Return existing promise if already loading or loaded
2184 if ( this.loading
) {
2185 return this.loading
.promise();
2189 loading
= this.loading
= $.Deferred();
2190 sub
= this.$iframe
.prop( 'contentWindow' );
2193 // Initialize contents
2198 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2199 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2200 '<div class="oo-ui-window-content"></div>' +
2207 this.$ = OO
.ui
.Element
.static.getJQuery( doc
, this.$iframe
);
2208 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2209 this.$document
= this.$( doc
);
2212 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2213 .always( function () {
2214 // Initialize isolated windows
2216 // Set initialized state after so sub-classes aren't confused by it being set by calling
2217 // their parent initialize method
2218 win
.initialized
= true;
2219 // Undo the visibility: hidden; hack and apply display: none;
2220 // We can do this safely now that the iframe has initialized
2221 // (don't do this from within #initialize because it has to happen
2222 // after the all subclasses have been handled as well).
2223 win
.toggle( win
.isVisible() );
2228 return loading
.promise();
2232 * Base class for all dialogs.
2235 * - Manage the window (open and close, etc.).
2236 * - Store the internal name and display title.
2237 * - A stack to track one or more pending actions.
2238 * - Manage a set of actions that can be performed.
2239 * - Configure and create action widgets.
2242 * - Close the dialog with Escape key.
2243 * - Visually lock the dialog while an action is in
2244 * progress (aka "pending").
2246 * Subclass responsibilities:
2247 * - Display the title somewhere.
2248 * - Add content to the dialog.
2249 * - Provide a UI to close the dialog.
2250 * - Display the action widgets somewhere.
2254 * @extends OO.ui.Window
2255 * @mixins OO.ui.PendingElement
2258 * @param {Object} [config] Configuration options
2260 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2261 // Parent constructor
2262 OO
.ui
.Dialog
.super.call( this, config
);
2264 // Mixin constructors
2265 OO
.ui
.PendingElement
.call( this );
2268 this.actions
= new OO
.ui
.ActionSet();
2269 this.attachedActions
= [];
2270 this.currentAction
= null;
2271 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2274 this.actions
.connect( this, {
2275 click
: 'onActionClick',
2276 resize
: 'onActionResize',
2277 change
: 'onActionsChange'
2282 .addClass( 'oo-ui-dialog' )
2283 .attr( 'role', 'dialog' );
2288 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2289 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2291 /* Static Properties */
2294 * Symbolic name of dialog.
2299 * @property {string}
2301 OO
.ui
.Dialog
.static.name
= '';
2309 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2311 OO
.ui
.Dialog
.static.title
= '';
2314 * List of OO.ui.ActionWidget configuration options.
2318 * @property {Object[]}
2320 OO
.ui
.Dialog
.static.actions
= [];
2323 * Close dialog when the escape key is pressed.
2328 * @property {boolean}
2330 OO
.ui
.Dialog
.static.escapable
= true;
2335 * Handle frame document key down events.
2337 * @param {jQuery.Event} e Key down event
2339 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2340 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2347 * Handle action resized events.
2349 * @param {OO.ui.ActionWidget} action Action that was resized
2351 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2352 // Override in subclass
2356 * Handle action click events.
2358 * @param {OO.ui.ActionWidget} action Action that was clicked
2360 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2361 if ( !this.isPending() ) {
2362 this.currentAction
= action
;
2363 this.executeAction( action
.getAction() );
2368 * Handle actions change event.
2370 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2371 this.detachActions();
2372 if ( !this.isClosing() ) {
2373 this.attachActions();
2378 * Get set of actions.
2380 * @return {OO.ui.ActionSet}
2382 OO
.ui
.Dialog
.prototype.getActions = function () {
2383 return this.actions
;
2387 * Get a process for taking action.
2389 * When you override this method, you can add additional accept steps to the process the parent
2390 * method provides using the 'first' and 'next' methods.
2393 * @param {string} [action] Symbolic name of action
2394 * @return {OO.ui.Process} Action process
2396 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2397 return new OO
.ui
.Process()
2398 .next( function () {
2400 // An empty action always closes the dialog without data, which should always be
2401 // safe and make no changes
2410 * @param {Object} [data] Dialog opening data
2411 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2412 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2413 * action item, omit to use #static-actions
2415 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2419 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2420 .next( function () {
2423 config
= this.constructor.static,
2424 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2426 this.title
.setLabel(
2427 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2429 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2431 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2434 this.actions
.add( items
);
2436 if ( this.constructor.static.escapable
) {
2437 this.$document
.on( 'keydown', this.onDocumentKeyDownHandler
);
2445 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2447 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2448 .first( function () {
2449 if ( this.constructor.static.escapable
) {
2450 this.$document
.off( 'keydown', this.onDocumentKeyDownHandler
);
2453 this.actions
.clear();
2454 this.currentAction
= null;
2461 OO
.ui
.Dialog
.prototype.initialize = function () {
2463 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2466 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2469 this.$content
.addClass( 'oo-ui-dialog-content' );
2470 this.setPendingElement( this.$head
);
2474 * Attach action actions.
2476 OO
.ui
.Dialog
.prototype.attachActions = function () {
2477 // Remember the list of potentially attached actions
2478 this.attachedActions
= this.actions
.get();
2482 * Detach action actions.
2486 OO
.ui
.Dialog
.prototype.detachActions = function () {
2489 // Detach all actions that may have been previously attached
2490 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2491 this.attachedActions
[i
].$element
.detach();
2493 this.attachedActions
= [];
2497 * Execute an action.
2499 * @param {string} action Symbolic name of action to execute
2500 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2502 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2504 return this.getActionProcess( action
).execute()
2505 .always( this.popPending
.bind( this ) );
2509 * Collection of windows.
2512 * @extends OO.ui.Element
2513 * @mixins OO.EventEmitter
2515 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2516 * already opening or opened, the current window will be closed without data. Empty closing data
2517 * should always result in the window being closed without causing constructive or destructive
2520 * As a window is opened and closed, it passes through several stages and the manager emits several
2521 * corresponding events.
2523 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2524 * - {@link #event-opening} is emitted with `opening` promise
2525 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2526 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2527 * - `setup` progress notification is emitted from opening promise
2528 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2529 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2530 * - `ready` progress notification is emitted from opening promise
2531 * - `opening` promise is resolved with `opened` promise
2532 * - Window is now open
2534 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2535 * - `opened` promise is resolved with `closing` promise
2536 * - {@link #event-closing} is emitted with `closing` promise
2537 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2538 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2539 * - `hold` progress notification is emitted from opening promise
2540 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2541 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2542 * - `teardown` progress notification is emitted from opening promise
2543 * - Closing promise is resolved
2544 * - Window is now closed
2547 * @param {Object} [config] Configuration options
2548 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2549 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2550 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2552 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2553 // Configuration initialization
2554 config
= config
|| {};
2556 // Parent constructor
2557 OO
.ui
.WindowManager
.super.call( this, config
);
2559 // Mixin constructors
2560 OO
.EventEmitter
.call( this );
2563 this.factory
= config
.factory
;
2564 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2565 this.isolate
= !!config
.isolate
;
2567 this.opening
= null;
2569 this.closing
= null;
2570 this.preparingToOpen
= null;
2571 this.preparingToClose
= null;
2573 this.currentWindow
= null;
2574 this.$ariaHidden
= null;
2575 this.requestedSize
= null;
2576 this.onWindowResizeTimeout
= null;
2577 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2578 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2579 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2580 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2584 .addClass( 'oo-ui-windowManager' )
2585 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2590 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2591 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2596 * Window is opening.
2598 * Fired when the window begins to be opened.
2601 * @param {OO.ui.Window} win Window that's being opened
2602 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2603 * resolved the first argument will be a promise which will be resolved when the window begins
2604 * closing, the second argument will be the opening data; progress notifications will be fired on
2605 * the promise for `setup` and `ready` when those processes are completed respectively.
2606 * @param {Object} data Window opening data
2610 * Window is closing.
2612 * Fired when the window begins to be closed.
2615 * @param {OO.ui.Window} win Window that's being closed
2616 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2617 * is resolved the first argument will be a the closing data; progress notifications will be fired
2618 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2619 * @param {Object} data Window closing data
2623 * Window was resized.
2626 * @param {OO.ui.Window} win Window that was resized
2629 /* Static Properties */
2632 * Map of symbolic size names and CSS properties.
2636 * @property {Object}
2638 OO
.ui
.WindowManager
.static.sizes
= {
2649 // These can be non-numeric because they are never used in calculations
2656 * Symbolic name of default size.
2658 * Default size is used if the window's requested size is not recognized.
2662 * @property {string}
2664 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2669 * Handle window resize events.
2671 * @param {jQuery.Event} e Window resize event
2673 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2674 clearTimeout( this.onWindowResizeTimeout
);
2675 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2679 * Handle window resize events.
2681 * @param {jQuery.Event} e Window resize event
2683 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2684 if ( this.currentWindow
) {
2685 this.updateWindowSize( this.currentWindow
);
2690 * Handle window mouse wheel events.
2692 * @param {jQuery.Event} e Mouse wheel event
2694 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function ( e
) {
2695 // Kill all events in the parent window if the child window is isolated,
2696 // or if the event didn't come from the child window
2697 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2701 * Handle document key down events.
2703 * @param {jQuery.Event} e Key down event
2705 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2706 switch ( e
.which
) {
2707 case OO
.ui
.Keys
.PAGEUP
:
2708 case OO
.ui
.Keys
.PAGEDOWN
:
2709 case OO
.ui
.Keys
.END
:
2710 case OO
.ui
.Keys
.HOME
:
2711 case OO
.ui
.Keys
.LEFT
:
2713 case OO
.ui
.Keys
.RIGHT
:
2714 case OO
.ui
.Keys
.DOWN
:
2715 // Kill all events in the parent window if the child window is isolated,
2716 // or if the event didn't come from the child window
2717 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2722 * Check if window is opening.
2724 * @return {boolean} Window is opening
2726 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2727 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2731 * Check if window is closing.
2733 * @return {boolean} Window is closing
2735 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2736 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2740 * Check if window is opened.
2742 * @return {boolean} Window is opened
2744 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2745 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2749 * Check if window contents should be isolated.
2751 * Window content isolation is done using inline frames.
2753 * @return {boolean} Window contents should be isolated
2755 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2756 return this.isolate
;
2760 * Check if a window is being managed.
2762 * @param {OO.ui.Window} win Window to check
2763 * @return {boolean} Window is being managed
2765 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2768 for ( name
in this.windows
) {
2769 if ( this.windows
[name
] === win
) {
2778 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2780 * @param {OO.ui.Window} win Window being opened
2781 * @param {Object} [data] Window opening data
2782 * @return {number} Milliseconds to wait
2784 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2789 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2791 * @param {OO.ui.Window} win Window being opened
2792 * @param {Object} [data] Window opening data
2793 * @return {number} Milliseconds to wait
2795 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2800 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2802 * @param {OO.ui.Window} win Window being closed
2803 * @param {Object} [data] Window closing data
2804 * @return {number} Milliseconds to wait
2806 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2811 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2813 * @param {OO.ui.Window} win Window being closed
2814 * @param {Object} [data] Window closing data
2815 * @return {number} Milliseconds to wait
2817 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2818 return this.modal
? 250 : 0;
2822 * Get managed window by symbolic name.
2824 * If window is not yet instantiated, it will be instantiated and added automatically.
2826 * @param {string} name Symbolic window name
2827 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2828 * @throws {Error} If the symbolic name is unrecognized by the factory
2829 * @throws {Error} If the symbolic name unrecognized as a managed window
2831 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2832 var deferred
= $.Deferred(),
2833 win
= this.windows
[name
];
2835 if ( !( win
instanceof OO
.ui
.Window
) ) {
2836 if ( this.factory
) {
2837 if ( !this.factory
.lookup( name
) ) {
2838 deferred
.reject( new OO
.ui
.Error(
2839 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2842 win
= this.factory
.create( name
, this, { $: this.$ } );
2843 this.addWindows( [ win
] );
2844 deferred
.resolve( win
);
2847 deferred
.reject( new OO
.ui
.Error(
2848 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2852 deferred
.resolve( win
);
2855 return deferred
.promise();
2859 * Get current window.
2861 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2863 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2864 return this.currentWindow
;
2870 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2871 * @param {Object} [data] Window opening data
2872 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2873 * for more details about the `opening` promise
2876 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2879 opening
= $.Deferred();
2881 // Argument handling
2882 if ( typeof win
=== 'string' ) {
2883 return this.getWindow( win
).then( function ( win
) {
2884 return manager
.openWindow( win
, data
);
2889 if ( !this.hasWindow( win
) ) {
2890 opening
.reject( new OO
.ui
.Error(
2891 'Cannot open window: window is not attached to manager'
2893 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2894 opening
.reject( new OO
.ui
.Error(
2895 'Cannot open window: another window is opening or open'
2900 if ( opening
.state() !== 'rejected' ) {
2901 if ( !win
.getManager() ) {
2902 win
.setManager( this );
2904 preparing
.push( win
.load() );
2906 if ( this.closing
) {
2907 // If a window is currently closing, wait for it to complete
2908 preparing
.push( this.closing
);
2911 this.preparingToOpen
= $.when
.apply( $, preparing
);
2912 // Ensure handlers get called after preparingToOpen is set
2913 this.preparingToOpen
.done( function () {
2914 if ( manager
.modal
) {
2915 manager
.toggleGlobalEvents( true );
2916 manager
.toggleAriaIsolation( true );
2918 manager
.currentWindow
= win
;
2919 manager
.opening
= opening
;
2920 manager
.preparingToOpen
= null;
2921 manager
.emit( 'opening', win
, opening
, data
);
2922 setTimeout( function () {
2923 win
.setup( data
).then( function () {
2924 manager
.updateWindowSize( win
);
2925 manager
.opening
.notify( { state
: 'setup' } );
2926 setTimeout( function () {
2927 win
.ready( data
).then( function () {
2928 manager
.opening
.notify( { state
: 'ready' } );
2929 manager
.opening
= null;
2930 manager
.opened
= $.Deferred();
2931 opening
.resolve( manager
.opened
.promise(), data
);
2933 }, manager
.getReadyDelay() );
2935 }, manager
.getSetupDelay() );
2939 return opening
.promise();
2945 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2946 * @param {Object} [data] Window closing data
2947 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2948 * for more details about the `closing` promise
2949 * @throws {Error} If no window by that name is being managed
2952 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2955 closing
= $.Deferred(),
2958 // Argument handling
2959 if ( typeof win
=== 'string' ) {
2960 win
= this.windows
[win
];
2961 } else if ( !this.hasWindow( win
) ) {
2967 closing
.reject( new OO
.ui
.Error(
2968 'Cannot close window: window is not attached to manager'
2970 } else if ( win
!== this.currentWindow
) {
2971 closing
.reject( new OO
.ui
.Error(
2972 'Cannot close window: window already closed with different data'
2974 } else if ( this.preparingToClose
|| this.closing
) {
2975 closing
.reject( new OO
.ui
.Error(
2976 'Cannot close window: window already closing with different data'
2981 if ( closing
.state() !== 'rejected' ) {
2982 if ( this.opening
) {
2983 // If the window is currently opening, close it when it's done
2984 preparing
.push( this.opening
);
2987 this.preparingToClose
= $.when
.apply( $, preparing
);
2988 // Ensure handlers get called after preparingToClose is set
2989 this.preparingToClose
.done( function () {
2990 manager
.closing
= closing
;
2991 manager
.preparingToClose
= null;
2992 manager
.emit( 'closing', win
, closing
, data
);
2993 opened
= manager
.opened
;
2994 manager
.opened
= null;
2995 opened
.resolve( closing
.promise(), data
);
2996 setTimeout( function () {
2997 win
.hold( data
).then( function () {
2998 closing
.notify( { state
: 'hold' } );
2999 setTimeout( function () {
3000 win
.teardown( data
).then( function () {
3001 closing
.notify( { state
: 'teardown' } );
3002 if ( manager
.modal
) {
3003 manager
.toggleGlobalEvents( false );
3004 manager
.toggleAriaIsolation( false );
3006 manager
.closing
= null;
3007 manager
.currentWindow
= null;
3008 closing
.resolve( data
);
3010 }, manager
.getTeardownDelay() );
3012 }, manager
.getHoldDelay() );
3016 return closing
.promise();
3022 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
3023 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
3024 * a statically configured symbolic name
3026 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
3027 var i
, len
, win
, name
, list
;
3029 if ( $.isArray( windows
) ) {
3030 // Convert to map of windows by looking up symbolic names from static configuration
3032 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
3033 name
= windows
[i
].constructor.static.name
;
3034 if ( typeof name
!== 'string' ) {
3035 throw new Error( 'Cannot add window' );
3037 list
[name
] = windows
[i
];
3039 } else if ( $.isPlainObject( windows
) ) {
3044 for ( name
in list
) {
3046 this.windows
[name
] = win
;
3047 this.$element
.append( win
.$element
);
3054 * Windows will be closed before they are removed.
3056 * @param {string} name Symbolic name of window to remove
3057 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3058 * @throws {Error} If windows being removed are not being managed
3060 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3061 var i
, len
, win
, name
, cleanupWindow
,
3064 cleanup = function ( name
, win
) {
3065 delete manager
.windows
[name
];
3066 win
.$element
.detach();
3069 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3071 win
= this.windows
[name
];
3073 throw new Error( 'Cannot remove window' );
3075 cleanupWindow
= cleanup
.bind( null, name
, win
);
3076 promises
.push( this.closeWindow( name
).then( cleanupWindow
, cleanupWindow
) );
3079 return $.when
.apply( $, promises
);
3083 * Remove all windows.
3085 * Windows will be closed before they are removed.
3087 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3089 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3090 return this.removeWindows( Object
.keys( this.windows
) );
3096 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3100 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3101 // Bypass for non-current, and thus invisible, windows
3102 if ( win
!== this.currentWindow
) {
3106 var viewport
= OO
.ui
.Element
.static.getDimensions( win
.getElementWindow() ),
3107 sizes
= this.constructor.static.sizes
,
3108 size
= win
.getSize();
3110 if ( !sizes
[size
] ) {
3111 size
= this.constructor.static.defaultSize
;
3113 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
3117 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3118 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3119 win
.setDimensions( sizes
[size
] );
3121 this.emit( 'resize', win
);
3127 * Bind or unbind global events for scrolling.
3129 * @param {boolean} [on] Bind global events
3132 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3133 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3136 if ( !this.globalEvents
) {
3137 this.$( this.getElementDocument() ).on( {
3138 // Prevent scrolling by keys in top-level window
3139 keydown
: this.onDocumentKeyDownHandler
3141 this.$( this.getElementWindow() ).on( {
3142 // Prevent scrolling by wheel in top-level window
3143 mousewheel
: this.onWindowMouseWheelHandler
,
3144 // Start listening for top-level window dimension changes
3145 'orientationchange resize': this.onWindowResizeHandler
3147 this.globalEvents
= true;
3149 } else if ( this.globalEvents
) {
3150 // Unbind global events
3151 this.$( this.getElementDocument() ).off( {
3152 // Allow scrolling by keys in top-level window
3153 keydown
: this.onDocumentKeyDownHandler
3155 this.$( this.getElementWindow() ).off( {
3156 // Allow scrolling by wheel in top-level window
3157 mousewheel
: this.onWindowMouseWheelHandler
,
3158 // Stop listening for top-level window dimension changes
3159 'orientationchange resize': this.onWindowResizeHandler
3161 this.globalEvents
= false;
3168 * Toggle screen reader visibility of content other than the window manager.
3170 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3173 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3174 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3177 if ( !this.$ariaHidden
) {
3178 // Hide everything other than the window manager from screen readers
3179 this.$ariaHidden
= $( 'body' )
3181 .not( this.$element
.parentsUntil( 'body' ).last() )
3182 .attr( 'aria-hidden', '' );
3184 } else if ( this.$ariaHidden
) {
3185 // Restore screen reader visibility
3186 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3187 this.$ariaHidden
= null;
3194 * Destroy window manager.
3196 * Windows will not be closed, only removed from the DOM.
3198 OO
.ui
.WindowManager
.prototype.destroy = function () {
3199 this.toggleGlobalEvents( false );
3200 this.toggleAriaIsolation( false );
3201 this.$element
.remove();
3209 * @param {string|jQuery} message Description of error
3210 * @param {Object} [config] Configuration options
3211 * @cfg {boolean} [recoverable=true] Error is recoverable
3212 * @cfg {boolean} [warning=false] Whether this error is a warning or not.
3214 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3215 // Configuration initialization
3216 config
= config
|| {};
3219 this.message
= message
instanceof jQuery
? message
: String( message
);
3220 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3221 this.warning
= !!config
.warning
;
3226 OO
.initClass( OO
.ui
.Error
);
3231 * Check if error can be recovered from.
3233 * @return {boolean} Error is recoverable
3235 OO
.ui
.Error
.prototype.isRecoverable = function () {
3236 return this.recoverable
;
3240 * Check if the error is a warning
3242 * @return {boolean} Error is warning
3244 OO
.ui
.Error
.prototype.isWarning = function () {
3245 return this.warning
;
3249 * Get error message as DOM nodes.
3251 * @return {jQuery} Error message in DOM nodes
3253 OO
.ui
.Error
.prototype.getMessage = function () {
3254 return this.message
instanceof jQuery
?
3255 this.message
.clone() :
3256 $( '<div>' ).text( this.message
).contents();
3260 * Get error message as text.
3262 * @return {string} Error message
3264 OO
.ui
.Error
.prototype.getMessageText = function () {
3265 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3269 * A list of functions, called in sequence.
3271 * If a function added to a process returns boolean false the process will stop; if it returns an
3272 * object with a `promise` method the process will use the promise to either continue to the next
3273 * step when the promise is resolved or stop when the promise is rejected.
3278 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3279 * call, see #createStep for more information
3280 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3282 * @return {Object} Step object, with `callback` and `context` properties
3284 OO
.ui
.Process = function ( step
, context
) {
3289 if ( step
!== undefined ) {
3290 this.next( step
, context
);
3296 OO
.initClass( OO
.ui
.Process
);
3301 * Start the process.
3303 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3304 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3305 * process, the remaining steps will not be taken
3307 OO
.ui
.Process
.prototype.execute = function () {
3308 var i
, len
, promise
;
3311 * Continue execution.
3314 * @param {Array} step A function and the context it should be called in
3315 * @return {Function} Function that continues the process
3317 function proceed( step
) {
3318 return function () {
3319 // Execute step in the correct context
3321 result
= step
.callback
.call( step
.context
);
3323 if ( result
=== false ) {
3324 // Use rejected promise for boolean false results
3325 return $.Deferred().reject( [] ).promise();
3327 if ( typeof result
=== 'number' ) {
3329 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3331 // Use a delayed promise for numbers, expecting them to be in milliseconds
3332 deferred
= $.Deferred();
3333 setTimeout( deferred
.resolve
, result
);
3334 return deferred
.promise();
3336 if ( result
instanceof OO
.ui
.Error
) {
3337 // Use rejected promise for error
3338 return $.Deferred().reject( [ result
] ).promise();
3340 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3341 // Use rejected promise for list of errors
3342 return $.Deferred().reject( result
).promise();
3344 // Duck-type the object to see if it can produce a promise
3345 if ( result
&& $.isFunction( result
.promise
) ) {
3346 // Use a promise generated from the result
3347 return result
.promise();
3349 // Use resolved promise for other results
3350 return $.Deferred().resolve().promise();
3354 if ( this.steps
.length
) {
3355 // Generate a chain reaction of promises
3356 promise
= proceed( this.steps
[0] )();
3357 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3358 promise
= promise
.then( proceed( this.steps
[i
] ) );
3361 promise
= $.Deferred().resolve().promise();
3368 * Create a process step.
3371 * @param {number|jQuery.Promise|Function} step
3373 * - Number of milliseconds to wait; or
3374 * - Promise to wait to be resolved; or
3375 * - Function to execute
3376 * - If it returns boolean false the process will stop
3377 * - If it returns an object with a `promise` method the process will use the promise to either
3378 * continue to the next step when the promise is resolved or stop when the promise is rejected
3379 * - If it returns a number, the process will wait for that number of milliseconds before
3381 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3383 * @return {Object} Step object, with `callback` and `context` properties
3385 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3386 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3388 callback: function () {
3394 if ( $.isFunction( step
) ) {
3400 throw new Error( 'Cannot create process step: number, promise or function expected' );
3404 * Add step to the beginning of the process.
3406 * @inheritdoc #createStep
3407 * @return {OO.ui.Process} this
3410 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3411 this.steps
.unshift( this.createStep( step
, context
) );
3416 * Add step to the end of the process.
3418 * @inheritdoc #createStep
3419 * @return {OO.ui.Process} this
3422 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3423 this.steps
.push( this.createStep( step
, context
) );
3428 * Factory for tools.
3431 * @extends OO.Factory
3434 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3435 // Parent constructor
3436 OO
.ui
.ToolFactory
.super.call( this );
3441 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3446 * Get tools from the factory
3448 * @param {Array} include Included tools
3449 * @param {Array} exclude Excluded tools
3450 * @param {Array} promote Promoted tools
3451 * @param {Array} demote Demoted tools
3452 * @return {string[]} List of tools
3454 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3455 var i
, len
, included
, promoted
, demoted
,
3459 // Collect included and not excluded tools
3460 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3463 promoted
= this.extract( promote
, used
);
3464 demoted
= this.extract( demote
, used
);
3467 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3468 if ( !used
[included
[i
]] ) {
3469 auto
.push( included
[i
] );
3473 return promoted
.concat( auto
).concat( demoted
);
3477 * Get a flat list of names from a list of names or groups.
3479 * Tools can be specified in the following ways:
3481 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3482 * - All tools in a group: `{ group: 'group-name' }`
3483 * - All tools: `'*'`
3486 * @param {Array|string} collection List of tools
3487 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3488 * names will be added as properties
3489 * @return {string[]} List of extracted names
3491 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3492 var i
, len
, item
, name
, tool
,
3495 if ( collection
=== '*' ) {
3496 for ( name
in this.registry
) {
3497 tool
= this.registry
[name
];
3499 // Only add tools by group name when auto-add is enabled
3500 tool
.static.autoAddToCatchall
&&
3501 // Exclude already used tools
3502 ( !used
|| !used
[name
] )
3510 } else if ( $.isArray( collection
) ) {
3511 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3512 item
= collection
[i
];
3513 // Allow plain strings as shorthand for named tools
3514 if ( typeof item
=== 'string' ) {
3515 item
= { name
: item
};
3517 if ( OO
.isPlainObject( item
) ) {
3519 for ( name
in this.registry
) {
3520 tool
= this.registry
[name
];
3522 // Include tools with matching group
3523 tool
.static.group
=== item
.group
&&
3524 // Only add tools by group name when auto-add is enabled
3525 tool
.static.autoAddToGroup
&&
3526 // Exclude already used tools
3527 ( !used
|| !used
[name
] )
3535 // Include tools with matching name and exclude already used tools
3536 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3537 names
.push( item
.name
);
3539 used
[item
.name
] = true;
3549 * Factory for tool groups.
3552 * @extends OO.Factory
3555 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3556 // Parent constructor
3557 OO
.Factory
.call( this );
3560 defaultClasses
= this.constructor.static.getDefaultClasses();
3562 // Register default toolgroups
3563 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3564 this.register( defaultClasses
[i
] );
3570 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3572 /* Static Methods */
3575 * Get a default set of classes to be registered on construction
3577 * @return {Function[]} Default classes
3579 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3582 OO
.ui
.ListToolGroup
,
3594 * @param {Object} [config] Configuration options
3596 OO
.ui
.Theme
= function OoUiTheme( config
) {
3597 // Configuration initialization
3598 config
= config
|| {};
3603 OO
.initClass( OO
.ui
.Theme
);
3608 * Get a list of classes to be applied to a widget.
3610 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
3611 * otherwise state transitions will not work properly.
3613 * @param {OO.ui.Element} element Element for which to get classes
3614 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3616 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3617 return { on
: [], off
: [] };
3621 * Update CSS classes provided by the theme.
3623 * For elements with theme logic hooks, this should be called any time there's a state change.
3625 * @param {OO.ui.Element} element Element for which to update classes
3626 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3628 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3629 var classes
= this.getElementClasses( element
);
3632 .removeClass( classes
.off
.join( ' ' ) )
3633 .addClass( classes
.on
.join( ' ' ) );
3637 * Element with a button.
3639 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3640 * and access keys for accessibility purposes.
3646 * @param {Object} [config] Configuration options
3647 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3648 * @cfg {boolean} [framed=true] Render button with a frame
3649 * @cfg {number} [tabIndex=0] Button's tab index. Use 0 to use default ordering, use -1 to prevent
3651 * @cfg {string} [accessKey] Button's access key
3653 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3654 // Configuration initialization
3655 config
= config
|| {};
3658 this.$button
= null;
3660 this.tabIndex
= null;
3661 this.accessKey
= null;
3662 this.active
= false;
3663 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3664 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3667 this.$element
.addClass( 'oo-ui-buttonElement' );
3668 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3669 this.setTabIndex( config
.tabIndex
|| 0 );
3670 this.setAccessKey( config
.accessKey
);
3671 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3676 OO
.initClass( OO
.ui
.ButtonElement
);
3678 /* Static Properties */
3681 * Cancel mouse down events.
3685 * @property {boolean}
3687 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3692 * Set the button element.
3694 * If an element is already set, it will be cleaned up before setting up the new element.
3696 * @param {jQuery} $button Element to use as button
3698 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3699 if ( this.$button
) {
3701 .removeClass( 'oo-ui-buttonElement-button' )
3702 .removeAttr( 'role accesskey tabindex' )
3703 .off( this.onMouseDownHandler
);
3706 this.$button
= $button
3707 .addClass( 'oo-ui-buttonElement-button' )
3708 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3709 .on( 'mousedown', this.onMouseDownHandler
);
3713 * Handles mouse down events.
3715 * @param {jQuery.Event} e Mouse down event
3717 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3718 if ( this.isDisabled() || e
.which
!== 1 ) {
3721 // Remove the tab-index while the button is down to prevent the button from stealing focus
3722 this.$button
.removeAttr( 'tabindex' );
3723 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3724 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3725 // reliably reapply the tabindex and remove the pressed class
3726 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3727 // Prevent change of focus unless specifically configured otherwise
3728 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3734 * Handles mouse up events.
3736 * @param {jQuery.Event} e Mouse up event
3738 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3739 if ( this.isDisabled() || e
.which
!== 1 ) {
3742 // Restore the tab-index after the button is up to restore the button's accessibility
3743 this.$button
.attr( 'tabindex', this.tabIndex
);
3744 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3745 // Stop listening for mouseup, since we only needed this once
3746 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3750 * Check if button has a frame.
3752 * @return {boolean} Button is framed
3754 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3761 * @param {boolean} [framed] Make button framed, omit to toggle
3764 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3765 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3766 if ( framed
!== this.framed
) {
3767 this.framed
= framed
;
3769 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3770 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3771 this.updateThemeClasses();
3780 * @param {number|null} tabIndex Button's tab index, use null to remove
3783 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3784 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3786 if ( this.tabIndex
!== tabIndex
) {
3787 if ( this.$button
) {
3788 if ( tabIndex
!== null ) {
3789 this.$button
.attr( 'tabindex', tabIndex
);
3791 this.$button
.removeAttr( 'tabindex' );
3794 this.tabIndex
= tabIndex
;
3803 * @param {string} accessKey Button's access key, use empty string to remove
3806 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3807 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3809 if ( this.accessKey
!== accessKey
) {
3810 if ( this.$button
) {
3811 if ( accessKey
!== null ) {
3812 this.$button
.attr( 'accesskey', accessKey
);
3814 this.$button
.removeAttr( 'accesskey' );
3817 this.accessKey
= accessKey
;
3826 * @param {boolean} [value] Make button active
3829 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3830 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3835 * Element containing a sequence of child elements.
3841 * @param {Object} [config] Configuration options
3842 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3844 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3845 // Configuration initialization
3846 config
= config
|| {};
3851 this.aggregateItemEvents
= {};
3854 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3860 * Set the group element.
3862 * If an element is already set, items will be moved to the new element.
3864 * @param {jQuery} $group Element to use as group
3866 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3869 this.$group
= $group
;
3870 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3871 this.$group
.append( this.items
[i
].$element
);
3876 * Check if there are no items.
3878 * @return {boolean} Group is empty
3880 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3881 return !this.items
.length
;
3887 * @return {OO.ui.Element[]} Items
3889 OO
.ui
.GroupElement
.prototype.getItems = function () {
3890 return this.items
.slice( 0 );
3894 * Get an item by its data.
3896 * Data is compared by a hash of its value. Only the first item with matching data will be returned.
3898 * @param {Object} data Item data to search for
3899 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
3901 OO
.ui
.GroupElement
.prototype.getItemFromData = function ( data
) {
3903 hash
= OO
.getHash( data
);
3905 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3906 item
= this.items
[i
];
3907 if ( hash
=== OO
.getHash( item
.getData() ) ) {
3916 * Get items by their data.
3918 * Data is compared by a hash of its value. All items with matching data will be returned.
3920 * @param {Object} data Item data to search for
3921 * @return {OO.ui.Element[]} Items with equivalent data
3923 OO
.ui
.GroupElement
.prototype.getItemsFromData = function ( data
) {
3925 hash
= OO
.getHash( data
),
3928 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3929 item
= this.items
[i
];
3930 if ( hash
=== OO
.getHash( item
.getData() ) ) {
3939 * Add an aggregate item event.
3941 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3942 * and with an additional leading parameter containing the item that emitted the original event.
3943 * Other arguments that were emitted from the original event are passed through.
3945 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3946 * event, use null value to remove aggregation
3947 * @throws {Error} If aggregation already exists
3949 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3950 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3952 for ( itemEvent
in events
) {
3953 groupEvent
= events
[itemEvent
];
3955 // Remove existing aggregated event
3956 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
3957 // Don't allow duplicate aggregations
3959 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3961 // Remove event aggregation from existing items
3962 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3963 item
= this.items
[i
];
3964 if ( item
.connect
&& item
.disconnect
) {
3966 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3967 item
.disconnect( this, remove
);
3970 // Prevent future items from aggregating event
3971 delete this.aggregateItemEvents
[itemEvent
];
3974 // Add new aggregate event
3976 // Make future items aggregate event
3977 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3978 // Add event aggregation to existing items
3979 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3980 item
= this.items
[i
];
3981 if ( item
.connect
&& item
.disconnect
) {
3983 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3984 item
.connect( this, add
);
3994 * Adding an existing item will move it.
3996 * @param {OO.ui.Element[]} items Items
3997 * @param {number} [index] Index to insert items at
4000 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
4001 var i
, len
, item
, event
, events
, currentIndex
,
4004 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4007 // Check if item exists then remove it first, effectively "moving" it
4008 currentIndex
= $.inArray( item
, this.items
);
4009 if ( currentIndex
>= 0 ) {
4010 this.removeItems( [ item
] );
4011 // Adjust index to compensate for removal
4012 if ( currentIndex
< index
) {
4017 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
4019 for ( event
in this.aggregateItemEvents
) {
4020 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
4022 item
.connect( this, events
);
4024 item
.setElementGroup( this );
4025 itemElements
.push( item
.$element
.get( 0 ) );
4028 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
4029 this.$group
.append( itemElements
);
4030 this.items
.push
.apply( this.items
, items
);
4031 } else if ( index
=== 0 ) {
4032 this.$group
.prepend( itemElements
);
4033 this.items
.unshift
.apply( this.items
, items
);
4035 this.items
[index
].$element
.before( itemElements
);
4036 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
4045 * Items will be detached, not removed, so they can be used later.
4047 * @param {OO.ui.Element[]} items Items to remove
4050 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
4051 var i
, len
, item
, index
, remove
, itemEvent
;
4053 // Remove specific items
4054 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
4056 index
= $.inArray( item
, this.items
);
4057 if ( index
!== -1 ) {
4059 item
.connect
&& item
.disconnect
&&
4060 !$.isEmptyObject( this.aggregateItemEvents
)
4063 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4064 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4066 item
.disconnect( this, remove
);
4068 item
.setElementGroup( null );
4069 this.items
.splice( index
, 1 );
4070 item
.$element
.detach();
4080 * Items will be detached, not removed, so they can be used later.
4084 OO
.ui
.GroupElement
.prototype.clearItems = function () {
4085 var i
, len
, item
, remove
, itemEvent
;
4088 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4089 item
= this.items
[i
];
4091 item
.connect
&& item
.disconnect
&&
4092 !$.isEmptyObject( this.aggregateItemEvents
)
4095 if ( Object
.prototype.hasOwnProperty
.call( this.aggregateItemEvents
, itemEvent
) ) {
4096 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
4098 item
.disconnect( this, remove
);
4100 item
.setElementGroup( null );
4101 item
.$element
.detach();
4109 * A mixin for an element that can be dragged and dropped.
4110 * Use in conjunction with DragGroupWidget
4117 OO
.ui
.DraggableElement
= function OoUiDraggableElement() {
4121 // Initialize and events
4123 .attr( 'draggable', true )
4124 .addClass( 'oo-ui-draggableElement' )
4126 dragstart
: this.onDragStart
.bind( this ),
4127 dragover
: this.onDragOver
.bind( this ),
4128 dragend
: this.onDragEnd
.bind( this ),
4129 drop
: this.onDrop
.bind( this )
4137 * @param {OO.ui.DraggableElement} item Dragging item
4151 * Respond to dragstart event.
4152 * @param {jQuery.Event} event jQuery event
4155 OO
.ui
.DraggableElement
.prototype.onDragStart = function ( e
) {
4156 var dataTransfer
= e
.originalEvent
.dataTransfer
;
4157 // Define drop effect
4158 dataTransfer
.dropEffect
= 'none';
4159 dataTransfer
.effectAllowed
= 'move';
4160 // We must set up a dataTransfer data property or Firefox seems to
4161 // ignore the fact the element is draggable.
4163 dataTransfer
.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
4165 // The above is only for firefox. No need to set a catch clause
4166 // if it fails, move on.
4168 // Add dragging class
4169 this.$element
.addClass( 'oo-ui-draggableElement-dragging' );
4171 this.emit( 'dragstart', this );
4176 * Respond to dragend event.
4179 OO
.ui
.DraggableElement
.prototype.onDragEnd = function () {
4180 this.$element
.removeClass( 'oo-ui-draggableElement-dragging' );
4181 this.emit( 'dragend' );
4185 * Handle drop event.
4186 * @param {jQuery.Event} event jQuery event
4189 OO
.ui
.DraggableElement
.prototype.onDrop = function ( e
) {
4191 this.emit( 'drop', e
);
4195 * In order for drag/drop to work, the dragover event must
4196 * return false and stop propogation.
4198 OO
.ui
.DraggableElement
.prototype.onDragOver = function ( e
) {
4204 * Store it in the DOM so we can access from the widget drag event
4205 * @param {number} Item index
4207 OO
.ui
.DraggableElement
.prototype.setIndex = function ( index
) {
4208 if ( this.index
!== index
) {
4210 this.$element
.data( 'index', index
);
4216 * @return {number} Item index
4218 OO
.ui
.DraggableElement
.prototype.getIndex = function () {
4223 * Element containing a sequence of child elements that can be dragged
4230 * @param {Object} [config] Configuration options
4231 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
4232 * @cfg {string} [orientation] Item orientation, 'horizontal' or 'vertical'. Defaults to 'vertical'
4234 OO
.ui
.DraggableGroupElement
= function OoUiDraggableGroupElement( config
) {
4235 // Configuration initialization
4236 config
= config
|| {};
4238 // Parent constructor
4239 OO
.ui
.GroupElement
.call( this, config
);
4242 this.orientation
= config
.orientation
|| 'vertical';
4243 this.dragItem
= null;
4244 this.itemDragOver
= null;
4246 this.sideInsertion
= '';
4250 dragstart
: 'itemDragStart',
4251 dragend
: 'itemDragEnd',
4254 this.connect( this, {
4255 itemDragStart
: 'onItemDragStart',
4256 itemDrop
: 'onItemDrop',
4257 itemDragEnd
: 'onItemDragEnd'
4260 dragover
: $.proxy( this.onDragOver
, this ),
4261 dragleave
: $.proxy( this.onDragLeave
, this )
4265 if ( $.isArray( config
.items
) ) {
4266 this.addItems( config
.items
);
4268 this.$placeholder
= $( '<div>' )
4269 .addClass( 'oo-ui-draggableGroupElement-placeholder' );
4271 .addClass( 'oo-ui-draggableGroupElement' )
4272 .append( this.$status
)
4273 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' )
4274 .prepend( this.$placeholder
);
4278 OO
.mixinClass( OO
.ui
.DraggableGroupElement
, OO
.ui
.GroupElement
);
4284 * @param {OO.ui.DraggableElement} item Reordered item
4285 * @param {number} [newIndex] New index for the item
4291 * Respond to item drag start event
4292 * @param {OO.ui.DraggableElement} item Dragged item
4294 OO
.ui
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
4297 // Map the index of each object
4298 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4299 this.items
[i
].setIndex( i
);
4302 if ( this.orientation
=== 'horizontal' ) {
4303 // Set the height of the indicator
4304 this.$placeholder
.css( {
4305 height
: item
.$element
.outerHeight(),
4309 // Set the width of the indicator
4310 this.$placeholder
.css( {
4312 width
: item
.$element
.outerWidth()
4315 this.setDragItem( item
);
4319 * Respond to item drag end event
4321 OO
.ui
.DraggableGroupElement
.prototype.onItemDragEnd = function () {
4322 this.unsetDragItem();
4327 * Handle drop event and switch the order of the items accordingly
4328 * @param {OO.ui.DraggableElement} item Dropped item
4331 OO
.ui
.DraggableGroupElement
.prototype.onItemDrop = function ( item
) {
4332 var toIndex
= item
.getIndex();
4333 // Check if the dropped item is from the current group
4334 // TODO: Figure out a way to configure a list of legally droppable
4335 // elements even if they are not yet in the list
4336 if ( this.getDragItem() ) {
4337 // If the insertion point is 'after', the insertion index
4338 // is shifted to the right (or to the left in RTL, hence 'after')
4339 if ( this.sideInsertion
=== 'after' ) {
4342 // Emit change event
4343 this.emit( 'reorder', this.getDragItem(), toIndex
);
4345 // Return false to prevent propogation
4350 * Handle dragleave event.
4352 OO
.ui
.DraggableGroupElement
.prototype.onDragLeave = function () {
4353 // This means the item was dragged outside the widget
4360 * Respond to dragover event
4361 * @param {jQuery.Event} event Event details
4363 OO
.ui
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
4364 var dragOverObj
, $optionWidget
, itemOffset
, itemMidpoint
, itemBoundingRect
,
4365 itemSize
, cssOutput
, dragPosition
, itemIndex
, itemPosition
,
4366 clientX
= e
.originalEvent
.clientX
,
4367 clientY
= e
.originalEvent
.clientY
;
4369 // Get the OptionWidget item we are dragging over
4370 dragOverObj
= this.getElementDocument().elementFromPoint( clientX
, clientY
);
4371 $optionWidget
= $( dragOverObj
).closest( '.oo-ui-draggableElement' );
4372 if ( $optionWidget
[0] ) {
4373 itemOffset
= $optionWidget
.offset();
4374 itemBoundingRect
= $optionWidget
[0].getBoundingClientRect();
4375 itemPosition
= $optionWidget
.position();
4376 itemIndex
= $optionWidget
.data( 'index' );
4381 this.isDragging() &&
4382 itemIndex
!== this.getDragItem().getIndex()
4384 if ( this.orientation
=== 'horizontal' ) {
4385 // Calculate where the mouse is relative to the item width
4386 itemSize
= itemBoundingRect
.width
;
4387 itemMidpoint
= itemBoundingRect
.left
+ itemSize
/ 2;
4388 dragPosition
= clientX
;
4389 // Which side of the item we hover over will dictate
4390 // where the placeholder will appear, on the left or
4393 left
: dragPosition
< itemMidpoint
? itemPosition
.left
: itemPosition
.left
+ itemSize
,
4394 top
: itemPosition
.top
4397 // Calculate where the mouse is relative to the item height
4398 itemSize
= itemBoundingRect
.height
;
4399 itemMidpoint
= itemBoundingRect
.top
+ itemSize
/ 2;
4400 dragPosition
= clientY
;
4401 // Which side of the item we hover over will dictate
4402 // where the placeholder will appear, on the top or
4405 top
: dragPosition
< itemMidpoint
? itemPosition
.top
: itemPosition
.top
+ itemSize
,
4406 left
: itemPosition
.left
4409 // Store whether we are before or after an item to rearrange
4410 // For horizontal layout, we need to account for RTL, as this is flipped
4411 if ( this.orientation
=== 'horizontal' && this.$element
.css( 'direction' ) === 'rtl' ) {
4412 this.sideInsertion
= dragPosition
< itemMidpoint
? 'after' : 'before';
4414 this.sideInsertion
= dragPosition
< itemMidpoint
? 'before' : 'after';
4416 // Add drop indicator between objects
4417 if ( this.sideInsertion
) {
4430 // This means the item was dragged outside the widget
4440 * Set a dragged item
4441 * @param {OO.ui.DraggableElement} item Dragged item
4443 OO
.ui
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
4444 this.dragItem
= item
;
4448 * Unset the current dragged item
4450 OO
.ui
.DraggableGroupElement
.prototype.unsetDragItem = function () {
4451 this.dragItem
= null;
4452 this.itemDragOver
= null;
4453 this.$placeholder
.hide();
4454 this.sideInsertion
= '';
4458 * Get the current dragged item
4459 * @return {OO.ui.DraggableElement|null} item Dragged item or null if no item is dragged
4461 OO
.ui
.DraggableGroupElement
.prototype.getDragItem = function () {
4462 return this.dragItem
;
4466 * Check if there's an item being dragged.
4467 * @return {Boolean} Item is being dragged
4469 OO
.ui
.DraggableGroupElement
.prototype.isDragging = function () {
4470 return this.getDragItem() !== null;
4474 * Element containing an icon.
4476 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4477 * a control or convey information in a more space efficient way. Icons should rarely be used
4478 * without labels; such as in a toolbar where space is at a premium or within a context where the
4479 * meaning is very clear to the user.
4485 * @param {Object} [config] Configuration options
4486 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4487 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4488 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4490 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4492 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4493 // Configuration initialization
4494 config
= config
|| {};
4499 this.iconTitle
= null;
4502 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4503 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4504 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4509 OO
.initClass( OO
.ui
.IconElement
);
4511 /* Static Properties */
4516 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4518 * For i18n purposes, this property can be an object containing a `default` icon name property and
4519 * additional icon names keyed by language code.
4521 * Example of i18n icon definition:
4522 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4526 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4527 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4530 OO
.ui
.IconElement
.static.icon
= null;
4537 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4540 OO
.ui
.IconElement
.static.iconTitle
= null;
4545 * Set the icon element.
4547 * If an element is already set, it will be cleaned up before setting up the new element.
4549 * @param {jQuery} $icon Element to use as icon
4551 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4554 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4555 .removeAttr( 'title' );
4559 .addClass( 'oo-ui-iconElement-icon' )
4560 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4561 if ( this.iconTitle
!== null ) {
4562 this.$icon
.attr( 'title', this.iconTitle
);
4569 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4570 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4571 * language, use null to remove icon
4574 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4575 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4576 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4578 if ( this.icon
!== icon
) {
4580 if ( this.icon
!== null ) {
4581 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4583 if ( icon
!== null ) {
4584 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4590 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4591 this.updateThemeClasses();
4599 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4603 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4604 iconTitle
= typeof iconTitle
=== 'function' ||
4605 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4606 OO
.ui
.resolveMsg( iconTitle
) : null;
4608 if ( this.iconTitle
!== iconTitle
) {
4609 this.iconTitle
= iconTitle
;
4611 if ( this.iconTitle
!== null ) {
4612 this.$icon
.attr( 'title', iconTitle
);
4614 this.$icon
.removeAttr( 'title' );
4625 * @return {string} Icon name
4627 OO
.ui
.IconElement
.prototype.getIcon = function () {
4634 * @return {string} Icon title text
4636 OO
.ui
.IconElement
.prototype.getIconTitle = function () {
4637 return this.iconTitle
;
4641 * Element containing an indicator.
4643 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4644 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4645 * instead of performing an action directly, or an item in a list which has errors that need to be
4652 * @param {Object} [config] Configuration options
4653 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4655 * @cfg {string} [indicator] Symbolic indicator name
4656 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4658 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4659 // Configuration initialization
4660 config
= config
|| {};
4663 this.$indicator
= null;
4664 this.indicator
= null;
4665 this.indicatorTitle
= null;
4668 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4669 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4670 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4675 OO
.initClass( OO
.ui
.IndicatorElement
);
4677 /* Static Properties */
4684 * @property {string|null} Symbolic indicator name
4686 OO
.ui
.IndicatorElement
.static.indicator
= null;
4693 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4696 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4701 * Set the indicator element.
4703 * If an element is already set, it will be cleaned up before setting up the new element.
4705 * @param {jQuery} $indicator Element to use as indicator
4707 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4708 if ( this.$indicator
) {
4710 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4711 .removeAttr( 'title' );
4714 this.$indicator
= $indicator
4715 .addClass( 'oo-ui-indicatorElement-indicator' )
4716 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4717 if ( this.indicatorTitle
!== null ) {
4718 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4723 * Set indicator name.
4725 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4728 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4729 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4731 if ( this.indicator
!== indicator
) {
4732 if ( this.$indicator
) {
4733 if ( this.indicator
!== null ) {
4734 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4736 if ( indicator
!== null ) {
4737 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4740 this.indicator
= indicator
;
4743 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4744 this.updateThemeClasses();
4750 * Set indicator title.
4752 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4753 * null for no indicator title
4756 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4757 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4758 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4759 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4761 if ( this.indicatorTitle
!== indicatorTitle
) {
4762 this.indicatorTitle
= indicatorTitle
;
4763 if ( this.$indicator
) {
4764 if ( this.indicatorTitle
!== null ) {
4765 this.$indicator
.attr( 'title', indicatorTitle
);
4767 this.$indicator
.removeAttr( 'title' );
4776 * Get indicator name.
4778 * @return {string} Symbolic name of indicator
4780 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4781 return this.indicator
;
4785 * Get indicator title.
4787 * @return {string} Indicator title text
4789 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4790 return this.indicatorTitle
;
4794 * Element containing a label.
4800 * @param {Object} [config] Configuration options
4801 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4802 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4803 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4805 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4806 // Configuration initialization
4807 config
= config
|| {};
4812 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4815 this.setLabel( config
.label
|| this.constructor.static.label
);
4816 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4821 OO
.initClass( OO
.ui
.LabelElement
);
4823 /* Static Properties */
4830 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4833 OO
.ui
.LabelElement
.static.label
= null;
4838 * Set the label element.
4840 * If an element is already set, it will be cleaned up before setting up the new element.
4842 * @param {jQuery} $label Element to use as label
4844 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4845 if ( this.$label
) {
4846 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4849 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4850 this.setLabelContent( this.label
);
4856 * An empty string will result in the label being hidden. A string containing only whitespace will
4857 * be converted to a single ` `.
4859 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4860 * text; or null for no label
4863 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4864 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4865 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4867 if ( this.label
!== label
) {
4868 if ( this.$label
) {
4869 this.setLabelContent( label
);
4874 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4882 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
4883 * text; or null for no label
4885 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4894 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4895 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4896 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4903 * Set the content of the label.
4905 * Do not call this method until after the label element has been set by #setLabelElement.
4908 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4909 * text; or null for no label
4911 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4912 if ( typeof label
=== 'string' ) {
4913 if ( label
.match( /^\s*$/ ) ) {
4914 // Convert whitespace only string to a single non-breaking space
4915 this.$label
.html( ' ' );
4917 this.$label
.text( label
);
4919 } else if ( label
instanceof jQuery
) {
4920 this.$label
.empty().append( label
);
4922 this.$label
.empty();
4927 * Element containing an OO.ui.PopupWidget object.
4933 * @param {Object} [config] Configuration options
4934 * @cfg {Object} [popup] Configuration to pass to popup
4935 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4937 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4938 // Configuration initialization
4939 config
= config
|| {};
4942 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4943 { autoClose
: true },
4945 { $: this.$, $autoCloseIgnore
: this.$element
}
4954 * @return {OO.ui.PopupWidget} Popup widget
4956 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4961 * Element with named flags that can be added, removed, listed and checked.
4963 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4964 * the flag name. Flags are primarily useful for styling.
4970 * @param {Object} [config] Configuration options
4971 * @cfg {string|string[]} [flags] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4972 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4974 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4975 // Configuration initialization
4976 config
= config
|| {};
4980 this.$flagged
= null;
4983 this.setFlags( config
.flags
);
4984 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4991 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4992 * added/removed properties
4998 * Set the flagged element.
5000 * If an element is already set, it will be cleaned up before setting up the new element.
5002 * @param {jQuery} $flagged Element to add flags to
5004 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
5005 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
5006 return 'oo-ui-flaggedElement-' + flag
;
5009 if ( this.$flagged
) {
5010 this.$flagged
.removeClass( classNames
);
5013 this.$flagged
= $flagged
.addClass( classNames
);
5017 * Check if a flag is set.
5019 * @param {string} flag Name of flag
5020 * @return {boolean} Has flag
5022 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
5023 return flag
in this.flags
;
5027 * Get the names of all flags set.
5029 * @return {string[]} Flag names
5031 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
5032 return Object
.keys( this.flags
);
5041 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
5042 var flag
, className
,
5045 classPrefix
= 'oo-ui-flaggedElement-';
5047 for ( flag
in this.flags
) {
5048 className
= classPrefix
+ flag
;
5049 changes
[flag
] = false;
5050 delete this.flags
[flag
];
5051 remove
.push( className
);
5054 if ( this.$flagged
) {
5055 this.$flagged
.removeClass( remove
.join( ' ' ) );
5058 this.updateThemeClasses();
5059 this.emit( 'flag', changes
);
5065 * Add one or more flags.
5067 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
5068 * keyed by flag name containing boolean set/remove instructions.
5072 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
5073 var i
, len
, flag
, className
,
5077 classPrefix
= 'oo-ui-flaggedElement-';
5079 if ( typeof flags
=== 'string' ) {
5080 className
= classPrefix
+ flags
;
5082 if ( !this.flags
[flags
] ) {
5083 this.flags
[flags
] = true;
5084 add
.push( className
);
5086 } else if ( $.isArray( flags
) ) {
5087 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
5089 className
= classPrefix
+ flag
;
5091 if ( !this.flags
[flag
] ) {
5092 changes
[flag
] = true;
5093 this.flags
[flag
] = true;
5094 add
.push( className
);
5097 } else if ( OO
.isPlainObject( flags
) ) {
5098 for ( flag
in flags
) {
5099 className
= classPrefix
+ flag
;
5100 if ( flags
[flag
] ) {
5102 if ( !this.flags
[flag
] ) {
5103 changes
[flag
] = true;
5104 this.flags
[flag
] = true;
5105 add
.push( className
);
5109 if ( this.flags
[flag
] ) {
5110 changes
[flag
] = false;
5111 delete this.flags
[flag
];
5112 remove
.push( className
);
5118 if ( this.$flagged
) {
5120 .addClass( add
.join( ' ' ) )
5121 .removeClass( remove
.join( ' ' ) );
5124 this.updateThemeClasses();
5125 this.emit( 'flag', changes
);
5131 * Element with a title.
5133 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
5134 * not visible on touch devices.
5140 * @param {Object} [config] Configuration options
5141 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
5142 * @cfg {string|Function} [title] Title text or a function that returns text. If not provided, the
5143 * static property 'title' is used.
5145 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
5146 // Configuration initialization
5147 config
= config
|| {};
5150 this.$titled
= null;
5154 this.setTitle( config
.title
|| this.constructor.static.title
);
5155 this.setTitledElement( config
.$titled
|| this.$element
);
5160 OO
.initClass( OO
.ui
.TitledElement
);
5162 /* Static Properties */
5169 * @property {string|Function} Title text or a function that returns text
5171 OO
.ui
.TitledElement
.static.title
= null;
5176 * Set the titled element.
5178 * If an element is already set, it will be cleaned up before setting up the new element.
5180 * @param {jQuery} $titled Element to set title on
5182 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
5183 if ( this.$titled
) {
5184 this.$titled
.removeAttr( 'title' );
5187 this.$titled
= $titled
;
5189 this.$titled
.attr( 'title', this.title
);
5196 * @param {string|Function|null} title Title text, a function that returns text or null for no title
5199 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
5200 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
5202 if ( this.title
!== title
) {
5203 if ( this.$titled
) {
5204 if ( title
!== null ) {
5205 this.$titled
.attr( 'title', title
);
5207 this.$titled
.removeAttr( 'title' );
5219 * @return {string} Title string
5221 OO
.ui
.TitledElement
.prototype.getTitle = function () {
5226 * Element that can be automatically clipped to visible boundaries.
5228 * Whenever the element's natural height changes, you have to call
5229 * #clip to make sure it's still clipping correctly.
5235 * @param {Object} [config] Configuration options
5236 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
5238 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
5239 // Configuration initialization
5240 config
= config
|| {};
5243 this.$clippable
= null;
5244 this.clipping
= false;
5245 this.clippedHorizontally
= false;
5246 this.clippedVertically
= false;
5247 this.$clippableContainer
= null;
5248 this.$clippableScroller
= null;
5249 this.$clippableWindow
= null;
5250 this.idealWidth
= null;
5251 this.idealHeight
= null;
5252 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
5253 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
5256 this.setClippableElement( config
.$clippable
|| this.$element
);
5262 * Set clippable element.
5264 * If an element is already set, it will be cleaned up before setting up the new element.
5266 * @param {jQuery} $clippable Element to make clippable
5268 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
5269 if ( this.$clippable
) {
5270 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
5271 this.$clippable
.css( { width
: '', height
: '' } );
5272 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5273 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
5276 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
5283 * Do not turn clipping on until after the element is attached to the DOM and visible.
5285 * @param {boolean} [clipping] Enable clipping, omit to toggle
5288 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
5289 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
5291 if ( this.clipping
!== clipping
) {
5292 this.clipping
= clipping
;
5294 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
5295 // If the clippable container is the body, we have to listen to scroll events and check
5296 // jQuery.scrollTop on the window because of browser inconsistencies
5297 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
5298 this.$( OO
.ui
.Element
.static.getWindow( this.$clippableContainer
) ) :
5299 this.$clippableContainer
;
5300 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
5301 this.$clippableWindow
= this.$( this.getElementWindow() )
5302 .on( 'resize', this.onClippableWindowResizeHandler
);
5303 // Initial clip after visible
5306 this.$clippable
.css( { width
: '', height
: '' } );
5307 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5308 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
5310 this.$clippableContainer
= null;
5311 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
5312 this.$clippableScroller
= null;
5313 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
5314 this.$clippableWindow
= null;
5322 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
5324 * @return {boolean} Element will be clipped to the visible area
5326 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
5327 return this.clipping
;
5331 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
5333 * @return {boolean} Part of the element is being clipped
5335 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
5336 return this.clippedHorizontally
|| this.clippedVertically
;
5340 * Check if the right of the element is being clipped by the nearest scrollable container.
5342 * @return {boolean} Part of the element is being clipped
5344 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
5345 return this.clippedHorizontally
;
5349 * Check if the bottom of the element is being clipped by the nearest scrollable container.
5351 * @return {boolean} Part of the element is being clipped
5353 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
5354 return this.clippedVertically
;
5358 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
5360 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
5361 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
5363 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
5364 this.idealWidth
= width
;
5365 this.idealHeight
= height
;
5367 if ( !this.clipping
) {
5368 // Update dimensions
5369 this.$clippable
.css( { width
: width
, height
: height
} );
5371 // While clipping, idealWidth and idealHeight are not considered
5375 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
5376 * the element's natural height changes.
5378 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
5379 * overlapped by, the visible area of the nearest scrollable container.
5383 OO
.ui
.ClippableElement
.prototype.clip = function () {
5384 if ( !this.clipping
) {
5385 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
5390 cOffset
= this.$clippable
.offset(),
5391 $container
= this.$clippableContainer
.is( 'body' ) ?
5392 this.$clippableWindow
: this.$clippableContainer
,
5393 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
5394 ccHeight
= $container
.innerHeight() - buffer
,
5395 ccWidth
= $container
.innerWidth() - buffer
,
5396 scrollTop
= this.$clippableScroller
.scrollTop(),
5397 scrollLeft
= this.$clippableScroller
.scrollLeft(),
5398 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
5399 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
5400 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
5401 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
5402 clipWidth
= desiredWidth
< naturalWidth
,
5403 clipHeight
= desiredHeight
< naturalHeight
;
5406 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
5408 this.$clippable
.css( 'width', this.idealWidth
|| '' );
5409 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5410 this.$clippable
.css( 'overflowX', '' );
5413 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
5415 this.$clippable
.css( 'height', this.idealHeight
|| '' );
5416 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
5417 this.$clippable
.css( 'overflowY', '' );
5420 this.clippedHorizontally
= clipWidth
;
5421 this.clippedVertically
= clipHeight
;
5427 * Generic toolbar tool.
5431 * @extends OO.ui.Widget
5432 * @mixins OO.ui.IconElement
5433 * @mixins OO.ui.FlaggedElement
5436 * @param {OO.ui.ToolGroup} toolGroup
5437 * @param {Object} [config] Configuration options
5438 * @cfg {string|Function} [title] Title text or a function that returns text
5440 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
5441 // Configuration initialization
5442 config
= config
|| {};
5444 // Parent constructor
5445 OO
.ui
.Tool
.super.call( this, config
);
5447 // Mixin constructors
5448 OO
.ui
.IconElement
.call( this, config
);
5449 OO
.ui
.FlaggedElement
.call( this, config
);
5452 this.toolGroup
= toolGroup
;
5453 this.toolbar
= this.toolGroup
.getToolbar();
5454 this.active
= false;
5455 this.$title
= this.$( '<span>' );
5456 this.$titleText
= this.$( '<span>' );
5457 this.$accel
= this.$( '<span>' );
5458 this.$link
= this.$( '<a>' );
5462 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
5465 this.$titleText
.addClass( 'oo-ui-tool-title-text' );
5467 .addClass( 'oo-ui-tool-accel' )
5469 // This may need to be changed if the key names are ever localized,
5470 // but for now they are essentially written in English
5475 .addClass( 'oo-ui-tool-title' )
5476 .append( this.$titleText
, this.$accel
);
5478 .addClass( 'oo-ui-tool-link' )
5479 .append( this.$icon
, this.$title
)
5480 .prop( 'tabIndex', 0 )
5481 .attr( 'role', 'button' );
5483 .data( 'oo-ui-tool', this )
5485 'oo-ui-tool ' + 'oo-ui-tool-name-' +
5486 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
5488 .append( this.$link
);
5489 this.setTitle( config
.title
|| this.constructor.static.title
);
5494 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5495 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5496 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5504 /* Static Properties */
5510 OO
.ui
.Tool
.static.tagName
= 'span';
5513 * Symbolic name of tool.
5518 * @property {string}
5520 OO
.ui
.Tool
.static.name
= '';
5528 * @property {string}
5530 OO
.ui
.Tool
.static.group
= '';
5535 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
5536 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
5537 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
5538 * appended to the title if the tool is part of a bar tool group.
5543 * @property {string|Function} Title text or a function that returns text
5545 OO
.ui
.Tool
.static.title
= '';
5548 * Tool can be automatically added to catch-all groups.
5552 * @property {boolean}
5554 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
5557 * Tool can be automatically added to named groups.
5560 * @property {boolean}
5563 OO
.ui
.Tool
.static.autoAddToGroup
= true;
5566 * Check if this tool is compatible with given data.
5570 * @param {Mixed} data Data to check
5571 * @return {boolean} Tool can be used with data
5573 OO
.ui
.Tool
.static.isCompatibleWith = function () {
5580 * Handle the toolbar state being updated.
5582 * This is an abstract method that must be overridden in a concrete subclass.
5586 OO
.ui
.Tool
.prototype.onUpdateState = function () {
5588 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
5593 * Handle the tool being selected.
5595 * This is an abstract method that must be overridden in a concrete subclass.
5599 OO
.ui
.Tool
.prototype.onSelect = function () {
5601 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
5606 * Check if the button is active.
5608 * @return {boolean} Button is active
5610 OO
.ui
.Tool
.prototype.isActive = function () {
5615 * Make the button appear active or inactive.
5617 * @param {boolean} state Make button appear active
5619 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
5620 this.active
= !!state
;
5621 if ( this.active
) {
5622 this.$element
.addClass( 'oo-ui-tool-active' );
5624 this.$element
.removeClass( 'oo-ui-tool-active' );
5629 * Get the tool title.
5631 * @param {string|Function} title Title text or a function that returns text
5634 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
5635 this.title
= OO
.ui
.resolveMsg( title
);
5641 * Get the tool title.
5643 * @return {string} Title text
5645 OO
.ui
.Tool
.prototype.getTitle = function () {
5650 * Get the tool's symbolic name.
5652 * @return {string} Symbolic name of tool
5654 OO
.ui
.Tool
.prototype.getName = function () {
5655 return this.constructor.static.name
;
5661 OO
.ui
.Tool
.prototype.updateTitle = function () {
5662 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
5663 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
5664 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
5667 this.$titleText
.text( this.title
);
5668 this.$accel
.text( accel
);
5670 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
5671 tooltipParts
.push( this.title
);
5673 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
5674 tooltipParts
.push( accel
);
5676 if ( tooltipParts
.length
) {
5677 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
5679 this.$link
.removeAttr( 'title' );
5686 OO
.ui
.Tool
.prototype.destroy = function () {
5687 this.toolbar
.disconnect( this );
5688 this.$element
.remove();
5692 * Collection of tool groups.
5695 * @extends OO.ui.Element
5696 * @mixins OO.EventEmitter
5697 * @mixins OO.ui.GroupElement
5700 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5701 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5702 * @param {Object} [config] Configuration options
5703 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5704 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5706 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5707 // Configuration initialization
5708 config
= config
|| {};
5710 // Parent constructor
5711 OO
.ui
.Toolbar
.super.call( this, config
);
5713 // Mixin constructors
5714 OO
.EventEmitter
.call( this );
5715 OO
.ui
.GroupElement
.call( this, config
);
5718 this.toolFactory
= toolFactory
;
5719 this.toolGroupFactory
= toolGroupFactory
;
5722 this.$bar
= this.$( '<div>' );
5723 this.$actions
= this.$( '<div>' );
5724 this.initialized
= false;
5728 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5729 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
5732 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5733 if ( config
.actions
) {
5734 this.$bar
.append( this.$actions
.addClass( 'oo-ui-toolbar-actions' ) );
5737 .addClass( 'oo-ui-toolbar-bar' )
5738 .append( this.$group
, '<div style="clear:both"></div>' );
5739 if ( config
.shadow
) {
5740 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5742 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5747 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5748 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5749 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5754 * Get the tool factory.
5756 * @return {OO.ui.ToolFactory} Tool factory
5758 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5759 return this.toolFactory
;
5763 * Get the tool group factory.
5765 * @return {OO.Factory} Tool group factory
5767 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5768 return this.toolGroupFactory
;
5772 * Handles mouse down events.
5774 * @param {jQuery.Event} e Mouse down event
5776 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5777 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5778 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5779 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5785 * Sets up handles and preloads required information for the toolbar to work.
5786 * This must be called immediately after it is attached to a visible document.
5788 OO
.ui
.Toolbar
.prototype.initialize = function () {
5789 this.initialized
= true;
5795 * Tools can be specified in the following ways:
5797 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5798 * - All tools in a group: `{ group: 'group-name' }`
5799 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5801 * @param {Object.<string,Array>} groups List of tool group configurations
5802 * @param {Array|string} [groups.include] Tools to include
5803 * @param {Array|string} [groups.exclude] Tools to exclude
5804 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5805 * @param {Array|string} [groups.demote] Tools to demote to the end
5807 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5808 var i
, len
, type
, group
,
5810 defaultType
= 'bar';
5812 // Cleanup previous groups
5815 // Build out new groups
5816 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5818 if ( group
.include
=== '*' ) {
5819 // Apply defaults to catch-all groups
5820 if ( group
.type
=== undefined ) {
5821 group
.type
= 'list';
5823 if ( group
.label
=== undefined ) {
5824 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
5827 // Check type has been registered
5828 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5830 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5833 this.addItems( items
);
5837 * Remove all tools and groups from the toolbar.
5839 OO
.ui
.Toolbar
.prototype.reset = function () {
5844 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5845 this.items
[i
].destroy();
5851 * Destroys toolbar, removing event handlers and DOM elements.
5853 * Call this whenever you are done using a toolbar.
5855 OO
.ui
.Toolbar
.prototype.destroy = function () {
5857 this.$element
.remove();
5861 * Check if tool has not been used yet.
5863 * @param {string} name Symbolic name of tool
5864 * @return {boolean} Tool is available
5866 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5867 return !this.tools
[name
];
5871 * Prevent tool from being used again.
5873 * @param {OO.ui.Tool} tool Tool to reserve
5875 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5876 this.tools
[tool
.getName()] = tool
;
5880 * Allow tool to be used again.
5882 * @param {OO.ui.Tool} tool Tool to release
5884 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5885 delete this.tools
[tool
.getName()];
5889 * Get accelerator label for tool.
5891 * This is a stub that should be overridden to provide access to accelerator information.
5893 * @param {string} name Symbolic name of tool
5894 * @return {string|undefined} Tool accelerator label if available
5896 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5901 * Collection of tools.
5903 * Tools can be specified in the following ways:
5905 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5906 * - All tools in a group: `{ group: 'group-name' }`
5907 * - All tools: `'*'`
5911 * @extends OO.ui.Widget
5912 * @mixins OO.ui.GroupElement
5915 * @param {OO.ui.Toolbar} toolbar
5916 * @param {Object} [config] Configuration options
5917 * @cfg {Array|string} [include=[]] List of tools to include
5918 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5919 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5920 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5922 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5923 // Configuration initialization
5924 config
= config
|| {};
5926 // Parent constructor
5927 OO
.ui
.ToolGroup
.super.call( this, config
);
5929 // Mixin constructors
5930 OO
.ui
.GroupElement
.call( this, config
);
5933 this.toolbar
= toolbar
;
5935 this.pressed
= null;
5936 this.autoDisabled
= false;
5937 this.include
= config
.include
|| [];
5938 this.exclude
= config
.exclude
|| [];
5939 this.promote
= config
.promote
|| [];
5940 this.demote
= config
.demote
|| [];
5941 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
5945 'mousedown touchstart': this.onPointerDown
.bind( this ),
5946 'mouseup touchend': this.onPointerUp
.bind( this ),
5947 mouseover
: this.onMouseOver
.bind( this ),
5948 mouseout
: this.onMouseOut
.bind( this )
5950 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5951 this.aggregate( { disable
: 'itemDisable' } );
5952 this.connect( this, { itemDisable
: 'updateDisabled' } );
5955 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5957 .addClass( 'oo-ui-toolGroup' )
5958 .append( this.$group
);
5964 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5965 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5973 /* Static Properties */
5976 * Show labels in tooltips.
5980 * @property {boolean}
5982 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5985 * Show acceleration labels in tooltips.
5989 * @property {boolean}
5991 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5994 * Automatically disable the toolgroup when all tools are disabled
5998 * @property {boolean}
6000 OO
.ui
.ToolGroup
.static.autoDisable
= true;
6007 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
6008 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
6014 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
6015 var i
, item
, allDisabled
= true;
6017 if ( this.constructor.static.autoDisable
) {
6018 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
6019 item
= this.items
[i
];
6020 if ( !item
.isDisabled() ) {
6021 allDisabled
= false;
6025 this.autoDisabled
= allDisabled
;
6027 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
6031 * Handle mouse down events.
6033 * @param {jQuery.Event} e Mouse down event
6035 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
6036 // e.which is 0 for touch events, 1 for left mouse button
6037 if ( !this.isDisabled() && e
.which
<= 1 ) {
6038 this.pressed
= this.getTargetTool( e
);
6039 if ( this.pressed
) {
6040 this.pressed
.setActive( true );
6041 this.getElementDocument().addEventListener(
6042 'mouseup', this.onCapturedMouseUpHandler
, true
6050 * Handle captured mouse up events.
6052 * @param {Event} e Mouse up event
6054 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
6055 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
6056 // onPointerUp may be called a second time, depending on where the mouse is when the button is
6057 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
6058 this.onPointerUp( e
);
6062 * Handle mouse up events.
6064 * @param {jQuery.Event} e Mouse up event
6066 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
6067 var tool
= this.getTargetTool( e
);
6069 // e.which is 0 for touch events, 1 for left mouse button
6070 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
6071 this.pressed
.onSelect();
6074 this.pressed
= null;
6079 * Handle mouse over events.
6081 * @param {jQuery.Event} e Mouse over event
6083 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
6084 var tool
= this.getTargetTool( e
);
6086 if ( this.pressed
&& this.pressed
=== tool
) {
6087 this.pressed
.setActive( true );
6092 * Handle mouse out events.
6094 * @param {jQuery.Event} e Mouse out event
6096 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
6097 var tool
= this.getTargetTool( e
);
6099 if ( this.pressed
&& this.pressed
=== tool
) {
6100 this.pressed
.setActive( false );
6105 * Get the closest tool to a jQuery.Event.
6107 * Only tool links are considered, which prevents other elements in the tool such as popups from
6108 * triggering tool group interactions.
6111 * @param {jQuery.Event} e
6112 * @return {OO.ui.Tool|null} Tool, `null` if none was found
6114 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
6116 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
6118 if ( $item
.length
) {
6119 tool
= $item
.parent().data( 'oo-ui-tool' );
6122 return tool
&& !tool
.isDisabled() ? tool
: null;
6126 * Handle tool registry register events.
6128 * If a tool is registered after the group is created, we must repopulate the list to account for:
6130 * - a tool being added that may be included
6131 * - a tool already included being overridden
6133 * @param {string} name Symbolic name of tool
6135 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
6140 * Get the toolbar this group is in.
6142 * @return {OO.ui.Toolbar} Toolbar of group
6144 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
6145 return this.toolbar
;
6149 * Add and remove tools based on configuration.
6151 OO
.ui
.ToolGroup
.prototype.populate = function () {
6152 var i
, len
, name
, tool
,
6153 toolFactory
= this.toolbar
.getToolFactory(),
6157 list
= this.toolbar
.getToolFactory().getTools(
6158 this.include
, this.exclude
, this.promote
, this.demote
6161 // Build a list of needed tools
6162 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
6166 toolFactory
.lookup( name
) &&
6167 // Tool is available or is already in this group
6168 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
6170 tool
= this.tools
[name
];
6172 // Auto-initialize tools on first use
6173 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
6176 this.toolbar
.reserveTool( tool
);
6181 // Remove tools that are no longer needed
6182 for ( name
in this.tools
) {
6183 if ( !names
[name
] ) {
6184 this.tools
[name
].destroy();
6185 this.toolbar
.releaseTool( this.tools
[name
] );
6186 remove
.push( this.tools
[name
] );
6187 delete this.tools
[name
];
6190 if ( remove
.length
) {
6191 this.removeItems( remove
);
6193 // Update emptiness state
6195 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
6197 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
6199 // Re-add tools (moving existing ones to new locations)
6200 this.addItems( add
);
6201 // Disabled state may depend on items
6202 this.updateDisabled();
6206 * Destroy tool group.
6208 OO
.ui
.ToolGroup
.prototype.destroy = function () {
6212 this.toolbar
.getToolFactory().disconnect( this );
6213 for ( name
in this.tools
) {
6214 this.toolbar
.releaseTool( this.tools
[name
] );
6215 this.tools
[name
].disconnect( this ).destroy();
6216 delete this.tools
[name
];
6218 this.$element
.remove();
6222 * Dialog for showing a message.
6225 * - Registers two actions by default (safe and primary).
6226 * - Renders action widgets in the footer.
6229 * @extends OO.ui.Dialog
6232 * @param {Object} [config] Configuration options
6234 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
6235 // Parent constructor
6236 OO
.ui
.MessageDialog
.super.call( this, config
);
6239 this.verticalActionLayout
= null;
6242 this.$element
.addClass( 'oo-ui-messageDialog' );
6247 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
6249 /* Static Properties */
6251 OO
.ui
.MessageDialog
.static.name
= 'message';
6253 OO
.ui
.MessageDialog
.static.size
= 'small';
6255 OO
.ui
.MessageDialog
.static.verbose
= false;
6260 * A confirmation dialog's title should describe what the progressive action will do. An alert
6261 * dialog's title should describe what event occurred.
6265 * @property {jQuery|string|Function|null}
6267 OO
.ui
.MessageDialog
.static.title
= null;
6270 * A confirmation dialog's message should describe the consequences of the progressive action. An
6271 * alert dialog's message should describe why the event occurred.
6275 * @property {jQuery|string|Function|null}
6277 OO
.ui
.MessageDialog
.static.message
= null;
6279 OO
.ui
.MessageDialog
.static.actions
= [
6280 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
6281 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
6289 OO
.ui
.MessageDialog
.prototype.setManager = function ( manager
) {
6290 OO
.ui
.MessageDialog
.super.prototype.setManager
.call( this, manager
);
6293 this.manager
.connect( this, {
6303 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
6305 return OO
.ui
.MessageDialog
.super.prototype.onActionResize
.call( this, action
);
6309 * Handle window resized events.
6311 OO
.ui
.MessageDialog
.prototype.onResize = function () {
6313 dialog
.fitActions();
6314 // Wait for CSS transition to finish and do it again :(
6315 setTimeout( function () {
6316 dialog
.fitActions();
6321 * Toggle action layout between vertical and horizontal.
6323 * @param {boolean} [value] Layout actions vertically, omit to toggle
6326 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
6327 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
6329 if ( value
!== this.verticalActionLayout
) {
6330 this.verticalActionLayout
= value
;
6332 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
6333 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
6342 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
6344 return new OO
.ui
.Process( function () {
6345 this.close( { action
: action
} );
6348 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
6354 * @param {Object} [data] Dialog opening data
6355 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
6356 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
6357 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
6358 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
6361 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
6365 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
6366 .next( function () {
6367 this.title
.setLabel(
6368 data
.title
!== undefined ? data
.title
: this.constructor.static.title
6370 this.message
.setLabel(
6371 data
.message
!== undefined ? data
.message
: this.constructor.static.message
6373 this.message
.$element
.toggleClass(
6374 'oo-ui-messageDialog-message-verbose',
6375 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
6383 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
6384 var bodyHeight
, oldOverflow
,
6385 $scrollable
= this.container
.$element
;
6387 oldOverflow
= $scrollable
[0].style
.overflow
;
6388 $scrollable
[0].style
.overflow
= 'hidden';
6390 // Force… ugh… something to happen
6391 $scrollable
.contents().hide();
6392 $scrollable
.height();
6393 $scrollable
.contents().show();
6395 bodyHeight
= Math
.round( this.text
.$element
.outerHeight( true ) );
6396 $scrollable
[0].style
.overflow
= oldOverflow
;
6404 OO
.ui
.MessageDialog
.prototype.setDimensions = function ( dim
) {
6405 var $scrollable
= this.container
.$element
;
6406 OO
.ui
.MessageDialog
.super.prototype.setDimensions
.call( this, dim
);
6408 // Twiddle the overflow property, otherwise an unnecessary scrollbar will be produced.
6409 // Need to do it after transition completes (250ms), add 50ms just in case.
6410 setTimeout( function () {
6411 var oldOverflow
= $scrollable
[0].style
.overflow
;
6412 $scrollable
[0].style
.overflow
= 'hidden';
6414 // Force… ugh… something to happen
6415 $scrollable
.contents().hide();
6416 $scrollable
.height();
6417 $scrollable
.contents().show();
6419 $scrollable
[0].style
.overflow
= oldOverflow
;
6428 OO
.ui
.MessageDialog
.prototype.initialize = function () {
6430 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
6433 this.$actions
= this.$( '<div>' );
6434 this.container
= new OO
.ui
.PanelLayout( {
6435 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
6437 this.text
= new OO
.ui
.PanelLayout( {
6438 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
6440 this.message
= new OO
.ui
.LabelWidget( {
6441 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
6445 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
6446 this.$content
.addClass( 'oo-ui-messageDialog-content' );
6447 this.container
.$element
.append( this.text
.$element
);
6448 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
6449 this.$body
.append( this.container
.$element
);
6450 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
6451 this.$foot
.append( this.$actions
);
6457 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
6458 var i
, len
, other
, special
, others
;
6461 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
6463 special
= this.actions
.getSpecial();
6464 others
= this.actions
.getOthers();
6465 if ( special
.safe
) {
6466 this.$actions
.append( special
.safe
.$element
);
6467 special
.safe
.toggleFramed( false );
6469 if ( others
.length
) {
6470 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6472 this.$actions
.append( other
.$element
);
6473 other
.toggleFramed( false );
6476 if ( special
.primary
) {
6477 this.$actions
.append( special
.primary
.$element
);
6478 special
.primary
.toggleFramed( false );
6481 if ( !this.isOpening() ) {
6482 // If the dialog is currently opening, this will be called automatically soon.
6483 // This also calls #fitActions.
6484 this.manager
.updateWindowSize( this );
6489 * Fit action actions into columns or rows.
6491 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
6493 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
6495 previous
= this.verticalActionLayout
,
6496 actions
= this.actions
.get();
6499 this.toggleVerticalActionLayout( false );
6500 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
6501 action
= actions
[i
];
6502 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
6503 this.toggleVerticalActionLayout( true );
6508 if ( this.verticalActionLayout
!== previous
) {
6509 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6510 // We changed the layout, window height might need to be updated.
6511 this.manager
.updateWindowSize( this );
6516 * Navigation dialog window.
6519 * - Show and hide errors.
6520 * - Retry an action.
6523 * - Renders header with dialog title and one action widget on either side
6524 * (a 'safe' button on the left, and a 'primary' button on the right, both of
6525 * which close the dialog).
6526 * - Displays any action widgets in the footer (none by default).
6527 * - Ability to dismiss errors.
6529 * Subclass responsibilities:
6530 * - Register a 'safe' action.
6531 * - Register a 'primary' action.
6532 * - Add content to the dialog.
6536 * @extends OO.ui.Dialog
6539 * @param {Object} [config] Configuration options
6541 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
6542 // Parent constructor
6543 OO
.ui
.ProcessDialog
.super.call( this, config
);
6546 this.$element
.addClass( 'oo-ui-processDialog' );
6551 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
6556 * Handle dismiss button click events.
6560 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
6565 * Handle retry button click events.
6567 * Hides errors and then tries again.
6569 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
6571 this.executeAction( this.currentAction
.getAction() );
6577 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
6578 if ( this.actions
.isSpecial( action
) ) {
6581 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
6587 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
6589 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
6592 this.$navigation
= this.$( '<div>' );
6593 this.$location
= this.$( '<div>' );
6594 this.$safeActions
= this.$( '<div>' );
6595 this.$primaryActions
= this.$( '<div>' );
6596 this.$otherActions
= this.$( '<div>' );
6597 this.dismissButton
= new OO
.ui
.ButtonWidget( {
6599 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
6601 this.retryButton
= new OO
.ui
.ButtonWidget( { $: this.$ } );
6602 this.$errors
= this.$( '<div>' );
6603 this.$errorsTitle
= this.$( '<div>' );
6606 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
6607 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
6610 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
6612 .append( this.title
.$element
)
6613 .addClass( 'oo-ui-processDialog-location' );
6614 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
6615 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
6616 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
6618 .addClass( 'oo-ui-processDialog-errors-title' )
6619 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
6621 .addClass( 'oo-ui-processDialog-errors' )
6622 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
6624 .addClass( 'oo-ui-processDialog-content' )
6625 .append( this.$errors
);
6627 .addClass( 'oo-ui-processDialog-navigation' )
6628 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
6629 this.$head
.append( this.$navigation
);
6630 this.$foot
.append( this.$otherActions
);
6636 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
6637 var i
, len
, other
, special
, others
;
6640 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
6642 special
= this.actions
.getSpecial();
6643 others
= this.actions
.getOthers();
6644 if ( special
.primary
) {
6645 this.$primaryActions
.append( special
.primary
.$element
);
6646 special
.primary
.toggleFramed( true );
6648 if ( others
.length
) {
6649 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6651 this.$otherActions
.append( other
.$element
);
6652 other
.toggleFramed( true );
6655 if ( special
.safe
) {
6656 this.$safeActions
.append( special
.safe
.$element
);
6657 special
.safe
.toggleFramed( true );
6661 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6667 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
6668 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
6669 .fail( this.showErrors
.bind( this ) );
6673 * Fit label between actions.
6677 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
6678 var width
= Math
.max(
6679 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
6680 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
6682 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
6688 * Handle errors that occurred during accept or reject processes.
6690 * @param {OO.ui.Error[]} errors Errors to be handled
6692 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
6698 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
6699 if ( !errors
[i
].isRecoverable() ) {
6700 recoverable
= false;
6702 if ( errors
[i
].isWarning() ) {
6705 $item
= this.$( '<div>' )
6706 .addClass( 'oo-ui-processDialog-error' )
6707 .append( errors
[i
].getMessage() );
6708 items
.push( $item
[0] );
6710 this.$errorItems
= this.$( items
);
6711 if ( recoverable
) {
6712 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
6714 this.currentAction
.setDisabled( true );
6717 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-continue' ) );
6719 this.retryButton
.setLabel( OO
.ui
.msg( 'ooui-dialog-process-retry' ) );
6721 this.retryButton
.toggle( recoverable
);
6722 this.$errorsTitle
.after( this.$errorItems
);
6723 this.$errors
.show().scrollTop( 0 );
6729 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
6730 this.$errors
.hide();
6731 this.$errorItems
.remove();
6732 this.$errorItems
= null;
6736 * Layout containing a series of pages.
6739 * @extends OO.ui.Layout
6742 * @param {Object} [config] Configuration options
6743 * @cfg {boolean} [continuous=false] Show all pages, one after another
6744 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
6745 * @cfg {boolean} [outlined=false] Show an outline
6746 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
6748 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
6749 // Configuration initialization
6750 config
= config
|| {};
6752 // Parent constructor
6753 OO
.ui
.BookletLayout
.super.call( this, config
);
6756 this.currentPageName
= null;
6758 this.ignoreFocus
= false;
6759 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
6760 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
6761 this.outlineVisible
= false;
6762 this.outlined
= !!config
.outlined
;
6763 if ( this.outlined
) {
6764 this.editable
= !!config
.editable
;
6765 this.outlineControlsWidget
= null;
6766 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget( { $: this.$ } );
6767 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
6768 this.gridLayout
= new OO
.ui
.GridLayout(
6769 [ this.outlinePanel
, this.stackLayout
],
6770 { $: this.$, widths
: [ 1, 2 ] }
6772 this.outlineVisible
= true;
6773 if ( this.editable
) {
6774 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6775 this.outlineSelectWidget
, { $: this.$ }
6781 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6782 if ( this.outlined
) {
6783 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
6785 if ( this.autoFocus
) {
6786 // Event 'focus' does not bubble, but 'focusin' does
6787 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
6791 this.$element
.addClass( 'oo-ui-bookletLayout' );
6792 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6793 if ( this.outlined
) {
6794 this.outlinePanel
.$element
6795 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6796 .append( this.outlineSelectWidget
.$element
);
6797 if ( this.editable
) {
6798 this.outlinePanel
.$element
6799 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6800 .append( this.outlineControlsWidget
.$element
);
6802 this.$element
.append( this.gridLayout
.$element
);
6804 this.$element
.append( this.stackLayout
.$element
);
6810 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6816 * @param {OO.ui.PageLayout} page Current page
6821 * @param {OO.ui.PageLayout[]} page Added pages
6822 * @param {number} index Index pages were added at
6827 * @param {OO.ui.PageLayout[]} pages Removed pages
6833 * Handle stack layout focus.
6835 * @param {jQuery.Event} e Focusin event
6837 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6840 // Find the page that an element was focused within
6841 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6842 for ( name
in this.pages
) {
6843 // Check for page match, exclude current page to find only page changes
6844 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6845 this.setPage( name
);
6852 * Handle stack layout set events.
6854 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6856 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6859 page
.scrollElementIntoView( { complete: function () {
6860 if ( layout
.autoFocus
) {
6868 * Focus the first input in the current page.
6870 * If no page is selected, the first selectable page will be selected.
6871 * If the focus is already in an element on the current page, nothing will happen.
6873 OO
.ui
.BookletLayout
.prototype.focus = function () {
6874 var $input
, page
= this.stackLayout
.getCurrentItem();
6875 if ( !page
&& this.outlined
) {
6876 this.selectFirstSelectablePage();
6877 page
= this.stackLayout
.getCurrentItem();
6882 // Only change the focus if is not already in the current page
6883 if ( !page
.$element
.find( ':focus' ).length
) {
6884 $input
= page
.$element
.find( ':input:first' );
6885 if ( $input
.length
) {
6892 * Handle outline widget select events.
6894 * @param {OO.ui.OptionWidget|null} item Selected item
6896 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
6898 this.setPage( item
.getData() );
6903 * Check if booklet has an outline.
6907 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6908 return this.outlined
;
6912 * Check if booklet has editing controls.
6916 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6917 return this.editable
;
6921 * Check if booklet has a visible outline.
6925 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6926 return this.outlined
&& this.outlineVisible
;
6930 * Hide or show the outline.
6932 * @param {boolean} [show] Show outline, omit to invert current state
6935 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6936 if ( this.outlined
) {
6937 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6938 this.outlineVisible
= show
;
6939 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6946 * Get the outline widget.
6948 * @param {OO.ui.PageLayout} page Page to be selected
6949 * @return {OO.ui.PageLayout|null} Closest page to another
6951 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6952 var next
, prev
, level
,
6953 pages
= this.stackLayout
.getItems(),
6954 index
= $.inArray( page
, pages
);
6956 if ( index
!== -1 ) {
6957 next
= pages
[index
+ 1];
6958 prev
= pages
[index
- 1];
6959 // Prefer adjacent pages at the same level
6960 if ( this.outlined
) {
6961 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
6964 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
6970 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
6976 return prev
|| next
|| null;
6980 * Get the outline widget.
6982 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if booklet has no outline
6984 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6985 return this.outlineSelectWidget
;
6989 * Get the outline controls widget. If the outline is not editable, null is returned.
6991 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6993 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6994 return this.outlineControlsWidget
;
6998 * Get a page by name.
7000 * @param {string} name Symbolic name of page
7001 * @return {OO.ui.PageLayout|undefined} Page, if found
7003 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
7004 return this.pages
[name
];
7008 * Get the current page name.
7010 * @return {string|null} Current page name
7012 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
7013 return this.currentPageName
;
7017 * Add a page to the layout.
7019 * When pages are added with the same names as existing pages, the existing pages will be
7020 * automatically removed before the new pages are added.
7022 * @param {OO.ui.PageLayout[]} pages Pages to add
7023 * @param {number} index Index to insert pages after
7027 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
7028 var i
, len
, name
, page
, item
, currentIndex
,
7029 stackLayoutPages
= this.stackLayout
.getItems(),
7033 // Remove pages with same names
7034 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7036 name
= page
.getName();
7038 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
7039 // Correct the insertion index
7040 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
7041 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
7044 remove
.push( this.pages
[name
] );
7047 if ( remove
.length
) {
7048 this.removePages( remove
);
7052 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7054 name
= page
.getName();
7055 this.pages
[page
.getName()] = page
;
7056 if ( this.outlined
) {
7057 item
= new OO
.ui
.OutlineOptionWidget( { $: this.$, data
: name
} );
7058 page
.setOutlineItem( item
);
7063 if ( this.outlined
&& items
.length
) {
7064 this.outlineSelectWidget
.addItems( items
, index
);
7065 this.selectFirstSelectablePage();
7067 this.stackLayout
.addItems( pages
, index
);
7068 this.emit( 'add', pages
, index
);
7074 * Remove a page from the layout.
7079 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
7080 var i
, len
, name
, page
,
7083 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7085 name
= page
.getName();
7086 delete this.pages
[name
];
7087 if ( this.outlined
) {
7088 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
7089 page
.setOutlineItem( null );
7092 if ( this.outlined
&& items
.length
) {
7093 this.outlineSelectWidget
.removeItems( items
);
7094 this.selectFirstSelectablePage();
7096 this.stackLayout
.removeItems( pages
);
7097 this.emit( 'remove', pages
);
7103 * Clear all pages from the layout.
7108 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
7110 pages
= this.stackLayout
.getItems();
7113 this.currentPageName
= null;
7114 if ( this.outlined
) {
7115 this.outlineSelectWidget
.clearItems();
7116 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
7117 pages
[i
].setOutlineItem( null );
7120 this.stackLayout
.clearItems();
7122 this.emit( 'remove', pages
);
7128 * Set the current page by name.
7131 * @param {string} name Symbolic name of page
7133 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
7136 page
= this.pages
[name
];
7138 if ( name
!== this.currentPageName
) {
7139 if ( this.outlined
) {
7140 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
7141 if ( selectedItem
&& selectedItem
.getData() !== name
) {
7142 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getItemFromData( name
) );
7146 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
7147 this.pages
[this.currentPageName
].setActive( false );
7148 // Blur anything focused if the next page doesn't have anything focusable - this
7149 // is not needed if the next page has something focusable because once it is focused
7150 // this blur happens automatically
7151 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
7152 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
7153 if ( $focused
.length
) {
7158 this.currentPageName
= name
;
7159 this.stackLayout
.setItem( page
);
7160 page
.setActive( true );
7161 this.emit( 'set', page
);
7167 * Select the first selectable page.
7171 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
7172 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
7173 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
7180 * Layout made of a field and optional label.
7182 * Available label alignment modes include:
7183 * - left: Label is before the field and aligned away from it, best for when the user will be
7184 * scanning for a specific label in a form with many fields
7185 * - right: Label is before the field and aligned toward it, best for forms the user is very
7186 * familiar with and will tab through field checking quickly to verify which field they are in
7187 * - top: Label is before the field and above it, best for when the user will need to fill out all
7188 * fields from top to bottom in a form with few fields
7189 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
7190 * checkboxes or radio buttons
7193 * @extends OO.ui.Layout
7194 * @mixins OO.ui.LabelElement
7197 * @param {OO.ui.Widget} fieldWidget Field widget
7198 * @param {Object} [config] Configuration options
7199 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
7200 * @cfg {string} [help] Explanatory text shown as a '?' icon.
7202 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
7203 // Configuration initialization
7204 config
= $.extend( { align
: 'left' }, config
);
7206 // Properties (must be set before parent constructor, which calls #getTagName)
7207 this.fieldWidget
= fieldWidget
;
7209 // Parent constructor
7210 OO
.ui
.FieldLayout
.super.call( this, config
);
7212 // Mixin constructors
7213 OO
.ui
.LabelElement
.call( this, config
);
7216 this.$field
= this.$( '<div>' );
7218 if ( config
.help
) {
7219 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
7221 classes
: [ 'oo-ui-fieldLayout-help' ],
7226 this.popupButtonWidget
.getPopup().$body
.append(
7228 .text( config
.help
)
7229 .addClass( 'oo-ui-fieldLayout-help-content' )
7231 this.$help
= this.popupButtonWidget
.$element
;
7233 this.$help
= this.$( [] );
7237 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
7238 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
7240 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
7243 this.$element
.addClass( 'oo-ui-fieldLayout' );
7245 .addClass( 'oo-ui-fieldLayout-field' )
7246 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
7247 .append( this.fieldWidget
.$element
);
7248 this.setAlignment( config
.align
);
7253 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
7254 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
7261 OO
.ui
.FieldLayout
.prototype.getTagName = function () {
7262 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
7270 * Handle field disable events.
7272 * @param {boolean} value Field is disabled
7274 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
7275 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
7279 * Handle label mouse click events.
7281 * @param {jQuery.Event} e Mouse click event
7283 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
7284 this.fieldWidget
.simulateLabelClick();
7291 * @return {OO.ui.Widget} Field widget
7293 OO
.ui
.FieldLayout
.prototype.getField = function () {
7294 return this.fieldWidget
;
7298 * Set the field alignment mode.
7301 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
7304 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
7305 if ( value
!== this.align
) {
7306 // Default to 'left'
7307 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
7311 if ( value
=== 'inline' ) {
7312 this.$element
.append( this.$field
, this.$label
, this.$help
);
7314 this.$element
.append( this.$help
, this.$label
, this.$field
);
7316 // Set classes. The following classes can be used here:
7317 // * oo-ui-fieldLayout-align-left
7318 // * oo-ui-fieldLayout-align-right
7319 // * oo-ui-fieldLayout-align-top
7320 // * oo-ui-fieldLayout-align-inline
7322 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
7324 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
7332 * Layout made of a fieldset and optional legend.
7334 * Just add OO.ui.FieldLayout items.
7337 * @extends OO.ui.Layout
7338 * @mixins OO.ui.IconElement
7339 * @mixins OO.ui.LabelElement
7340 * @mixins OO.ui.GroupElement
7343 * @param {Object} [config] Configuration options
7344 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
7346 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
7347 // Configuration initialization
7348 config
= config
|| {};
7350 // Parent constructor
7351 OO
.ui
.FieldsetLayout
.super.call( this, config
);
7353 // Mixin constructors
7354 OO
.ui
.IconElement
.call( this, config
);
7355 OO
.ui
.LabelElement
.call( this, config
);
7356 OO
.ui
.GroupElement
.call( this, config
);
7360 .addClass( 'oo-ui-fieldsetLayout' )
7361 .prepend( this.$icon
, this.$label
, this.$group
);
7362 if ( $.isArray( config
.items
) ) {
7363 this.addItems( config
.items
);
7369 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
7370 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
7371 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
7372 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
7375 * Layout with an HTML form.
7378 * @extends OO.ui.Layout
7381 * @param {Object} [config] Configuration options
7382 * @cfg {string} [method] HTML form `method` attribute
7383 * @cfg {string} [action] HTML form `action` attribute
7384 * @cfg {string} [enctype] HTML form `enctype` attribute
7386 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
7387 // Configuration initialization
7388 config
= config
|| {};
7390 // Parent constructor
7391 OO
.ui
.FormLayout
.super.call( this, config
);
7394 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
7398 .addClass( 'oo-ui-formLayout' )
7400 method
: config
.method
,
7401 action
: config
.action
,
7402 enctype
: config
.enctype
7408 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
7416 /* Static Properties */
7418 OO
.ui
.FormLayout
.static.tagName
= 'form';
7423 * Handle form submit events.
7425 * @param {jQuery.Event} e Submit event
7428 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
7429 this.emit( 'submit' );
7434 * Layout made of proportionally sized columns and rows.
7437 * @extends OO.ui.Layout
7440 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
7441 * @param {Object} [config] Configuration options
7442 * @cfg {number[]} [widths] Widths of columns as ratios
7443 * @cfg {number[]} [heights] Heights of rows as ratios
7445 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
7448 // Configuration initialization
7449 config
= config
|| {};
7451 // Parent constructor
7452 OO
.ui
.GridLayout
.super.call( this, config
);
7460 this.$element
.addClass( 'oo-ui-gridLayout' );
7461 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
7462 this.panels
.push( panels
[i
] );
7463 this.$element
.append( panels
[i
].$element
);
7465 if ( config
.widths
|| config
.heights
) {
7466 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
7468 // Arrange in columns by default
7469 widths
= this.panels
.map( function () { return 1; } );
7470 this.layout( widths
, [ 1 ] );
7476 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
7491 * Set grid dimensions.
7493 * @param {number[]} widths Widths of columns as ratios
7494 * @param {number[]} heights Heights of rows as ratios
7496 * @throws {Error} If grid is not large enough to fit all panels
7498 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
7502 cols
= widths
.length
,
7503 rows
= heights
.length
;
7505 // Verify grid is big enough to fit panels
7506 if ( cols
* rows
< this.panels
.length
) {
7507 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
7510 // Sum up denominators
7511 for ( x
= 0; x
< cols
; x
++ ) {
7514 for ( y
= 0; y
< rows
; y
++ ) {
7520 for ( x
= 0; x
< cols
; x
++ ) {
7521 this.widths
[x
] = widths
[x
] / xd
;
7523 for ( y
= 0; y
< rows
; y
++ ) {
7524 this.heights
[y
] = heights
[y
] / yd
;
7528 this.emit( 'layout' );
7532 * Update panel positions and sizes.
7536 OO
.ui
.GridLayout
.prototype.update = function () {
7537 var x
, y
, panel
, width
, height
, dimensions
,
7541 cols
= this.widths
.length
,
7542 rows
= this.heights
.length
;
7544 for ( y
= 0; y
< rows
; y
++ ) {
7545 height
= this.heights
[y
];
7546 for ( x
= 0; x
< cols
; x
++ ) {
7547 width
= this.widths
[x
];
7548 panel
= this.panels
[i
];
7550 width
: Math
.round( width
* 100 ) + '%',
7551 height
: Math
.round( height
* 100 ) + '%',
7552 top
: Math
.round( top
* 100 ) + '%'
7555 if ( OO
.ui
.Element
.static.getDir( this.$.context
) === 'rtl' ) {
7556 dimensions
.right
= Math
.round( left
* 100 ) + '%';
7558 dimensions
.left
= Math
.round( left
* 100 ) + '%';
7560 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
7561 if ( width
=== 0 || height
=== 0 ) {
7562 dimensions
.visibility
= 'hidden';
7564 dimensions
.visibility
= '';
7566 panel
.$element
.css( dimensions
);
7574 this.emit( 'update' );
7578 * Get a panel at a given position.
7580 * The x and y position is affected by the current grid layout.
7582 * @param {number} x Horizontal position
7583 * @param {number} y Vertical position
7584 * @return {OO.ui.PanelLayout} The panel at the given position
7586 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
7587 return this.panels
[ ( x
* this.widths
.length
) + y
];
7591 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
7594 * @extends OO.ui.Layout
7597 * @param {Object} [config] Configuration options
7598 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
7599 * @cfg {boolean} [padded=false] Pad the content from the edges
7600 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
7602 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
7603 // Configuration initialization
7604 config
= $.extend( {
7610 // Parent constructor
7611 OO
.ui
.PanelLayout
.super.call( this, config
);
7614 this.$element
.addClass( 'oo-ui-panelLayout' );
7615 if ( config
.scrollable
) {
7616 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
7618 if ( config
.padded
) {
7619 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
7621 if ( config
.expanded
) {
7622 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
7628 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
7631 * Page within an booklet layout.
7634 * @extends OO.ui.PanelLayout
7637 * @param {string} name Unique symbolic name of page
7638 * @param {Object} [config] Configuration options
7640 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
7641 // Configuration initialization
7642 config
= $.extend( { scrollable
: true }, config
);
7644 // Parent constructor
7645 OO
.ui
.PageLayout
.super.call( this, config
);
7649 this.outlineItem
= null;
7650 this.active
= false;
7653 this.$element
.addClass( 'oo-ui-pageLayout' );
7658 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
7664 * @param {boolean} active Page is active
7672 * @return {string} Symbolic name of page
7674 OO
.ui
.PageLayout
.prototype.getName = function () {
7679 * Check if page is active.
7681 * @return {boolean} Page is active
7683 OO
.ui
.PageLayout
.prototype.isActive = function () {
7690 * @return {OO.ui.OutlineOptionWidget|null} Outline item widget
7692 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
7693 return this.outlineItem
;
7699 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
7700 * outline item as desired; this method is called for setting (with an object) and unsetting
7701 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
7702 * operating on null instead of an OO.ui.OutlineOptionWidget object.
7704 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline item widget, null to clear
7707 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
7708 this.outlineItem
= outlineItem
|| null;
7709 if ( outlineItem
) {
7710 this.setupOutlineItem();
7716 * Setup outline item.
7718 * @localdoc Subclasses should override this method to adjust the outline item as desired.
7720 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline item widget to setup
7723 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
7728 * Set page active state.
7730 * @param {boolean} Page is active
7733 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
7736 if ( active
!== this.active
) {
7737 this.active
= active
;
7738 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
7739 this.emit( 'active', this.active
);
7744 * Layout containing a series of mutually exclusive pages.
7747 * @extends OO.ui.PanelLayout
7748 * @mixins OO.ui.GroupElement
7751 * @param {Object} [config] Configuration options
7752 * @cfg {boolean} [continuous=false] Show all pages, one after another
7753 * @cfg {OO.ui.Layout[]} [items] Layouts to add
7755 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
7756 // Configuration initialization
7757 config
= $.extend( { scrollable
: true }, config
);
7759 // Parent constructor
7760 OO
.ui
.StackLayout
.super.call( this, config
);
7762 // Mixin constructors
7763 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7766 this.currentItem
= null;
7767 this.continuous
= !!config
.continuous
;
7770 this.$element
.addClass( 'oo-ui-stackLayout' );
7771 if ( this.continuous
) {
7772 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
7774 if ( $.isArray( config
.items
) ) {
7775 this.addItems( config
.items
);
7781 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
7782 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
7788 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
7794 * Get the current item.
7796 * @return {OO.ui.Layout|null}
7798 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
7799 return this.currentItem
;
7803 * Unset the current item.
7806 * @param {OO.ui.StackLayout} layout
7809 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7810 var prevItem
= this.currentItem
;
7811 if ( prevItem
=== null ) {
7815 this.currentItem
= null;
7816 this.emit( 'set', null );
7822 * Adding an existing item (by value) will move it.
7824 * @param {OO.ui.Layout[]} items Items to add
7825 * @param {number} [index] Index to insert items after
7828 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7830 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7832 if ( !this.currentItem
&& items
.length
) {
7833 this.setItem( items
[0] );
7842 * Items will be detached, not removed, so they can be used later.
7844 * @param {OO.ui.Layout[]} items Items to remove
7848 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7850 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7852 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7853 if ( this.items
.length
) {
7854 this.setItem( this.items
[0] );
7856 this.unsetCurrentItem();
7866 * Items will be detached, not removed, so they can be used later.
7871 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7872 this.unsetCurrentItem();
7873 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7881 * Any currently shown item will be hidden.
7883 * FIXME: If the passed item to show has not been added in the items list, then
7884 * this method drops it and unsets the current item.
7886 * @param {OO.ui.Layout} item Item to show
7890 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7893 if ( item
!== this.currentItem
) {
7894 if ( !this.continuous
) {
7895 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7896 this.items
[i
].$element
.css( 'display', '' );
7899 if ( $.inArray( item
, this.items
) !== -1 ) {
7900 if ( !this.continuous
) {
7901 item
.$element
.css( 'display', 'block' );
7903 this.currentItem
= item
;
7904 this.emit( 'set', item
);
7906 this.unsetCurrentItem();
7914 * Horizontal bar layout of tools as icon buttons.
7917 * @extends OO.ui.ToolGroup
7920 * @param {OO.ui.Toolbar} toolbar
7921 * @param {Object} [config] Configuration options
7923 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7924 // Parent constructor
7925 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7928 this.$element
.addClass( 'oo-ui-barToolGroup' );
7933 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7935 /* Static Properties */
7937 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7939 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7941 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7944 * Popup list of tools with an icon and optional label.
7948 * @extends OO.ui.ToolGroup
7949 * @mixins OO.ui.IconElement
7950 * @mixins OO.ui.IndicatorElement
7951 * @mixins OO.ui.LabelElement
7952 * @mixins OO.ui.TitledElement
7953 * @mixins OO.ui.ClippableElement
7956 * @param {OO.ui.Toolbar} toolbar
7957 * @param {Object} [config] Configuration options
7958 * @cfg {string} [header] Text to display at the top of the pop-up
7960 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7961 // Configuration initialization
7962 config
= config
|| {};
7964 // Parent constructor
7965 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7967 // Mixin constructors
7968 OO
.ui
.IconElement
.call( this, config
);
7969 OO
.ui
.IndicatorElement
.call( this, config
);
7970 OO
.ui
.LabelElement
.call( this, config
);
7971 OO
.ui
.TitledElement
.call( this, config
);
7972 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7975 this.active
= false;
7976 this.dragging
= false;
7977 this.onBlurHandler
= this.onBlur
.bind( this );
7978 this.$handle
= this.$( '<span>' );
7982 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
7983 'mouseup touchend': this.onHandlePointerUp
.bind( this )
7988 .addClass( 'oo-ui-popupToolGroup-handle' )
7989 .append( this.$icon
, this.$label
, this.$indicator
);
7990 // If the pop-up should have a header, add it to the top of the toolGroup.
7991 // Note: If this feature is useful for other widgets, we could abstract it into an
7992 // OO.ui.HeaderedElement mixin constructor.
7993 if ( config
.header
!== undefined ) {
7995 .prepend( this.$( '<span>' )
7996 .addClass( 'oo-ui-popupToolGroup-header' )
7997 .text( config
.header
)
8001 .addClass( 'oo-ui-popupToolGroup' )
8002 .prepend( this.$handle
);
8007 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
8008 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
8009 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
8010 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
8011 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
8012 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
8014 /* Static Properties */
8021 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
8023 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
8025 if ( this.isDisabled() && this.isElementAttached() ) {
8026 this.setActive( false );
8031 * Handle focus being lost.
8033 * The event is actually generated from a mouseup, so it is not a normal blur event object.
8035 * @param {jQuery.Event} e Mouse up event
8037 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
8038 // Only deactivate when clicking outside the dropdown element
8039 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
8040 this.setActive( false );
8047 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
8048 // e.which is 0 for touch events, 1 for left mouse button
8049 // Only close toolgroup when a tool was actually selected
8050 // FIXME: this duplicates logic from the parent class
8051 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== this.getTargetTool( e
) ) {
8052 this.setActive( false );
8054 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
8058 * Handle mouse up events.
8060 * @param {jQuery.Event} e Mouse up event
8062 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
8067 * Handle mouse down events.
8069 * @param {jQuery.Event} e Mouse down event
8071 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
8072 // e.which is 0 for touch events, 1 for left mouse button
8073 if ( !this.isDisabled() && e
.which
<= 1 ) {
8074 this.setActive( !this.active
);
8080 * Switch into active mode.
8082 * When active, mouseup events anywhere in the document will trigger deactivation.
8084 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
8086 if ( this.active
!== value
) {
8087 this.active
= value
;
8089 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
8091 // Try anchoring the popup to the left first
8092 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
8093 this.toggleClipping( true );
8094 if ( this.isClippedHorizontally() ) {
8095 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
8096 this.toggleClipping( false );
8098 .removeClass( 'oo-ui-popupToolGroup-left' )
8099 .addClass( 'oo-ui-popupToolGroup-right' );
8100 this.toggleClipping( true );
8103 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
8104 this.$element
.removeClass(
8105 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
8107 this.toggleClipping( false );
8113 * Drop down list layout of tools as labeled icon buttons.
8115 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
8116 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
8117 * may want to use the 'promote' and 'demote' configuration options to achieve this.
8120 * @extends OO.ui.PopupToolGroup
8123 * @param {OO.ui.Toolbar} toolbar
8124 * @param {Object} [config] Configuration options
8125 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
8127 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
8128 * allowed to be collapsed.
8129 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
8131 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
8132 // Configuration initialization
8133 config
= config
|| {};
8135 // Properties (must be set before parent constructor, which calls #populate)
8136 this.allowCollapse
= config
.allowCollapse
;
8137 this.forceExpand
= config
.forceExpand
;
8138 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
8139 this.collapsibleTools
= [];
8141 // Parent constructor
8142 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
8145 this.$element
.addClass( 'oo-ui-listToolGroup' );
8150 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
8152 /* Static Properties */
8154 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
8156 OO
.ui
.ListToolGroup
.static.name
= 'list';
8163 OO
.ui
.ListToolGroup
.prototype.populate = function () {
8164 var i
, len
, allowCollapse
= [];
8166 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
8168 // Update the list of collapsible tools
8169 if ( this.allowCollapse
!== undefined ) {
8170 allowCollapse
= this.allowCollapse
;
8171 } else if ( this.forceExpand
!== undefined ) {
8172 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
8175 this.collapsibleTools
= [];
8176 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
8177 if ( this.tools
[ allowCollapse
[i
] ] !== undefined ) {
8178 this.collapsibleTools
.push( this.tools
[ allowCollapse
[i
] ] );
8182 // Keep at the end, even when tools are added
8183 this.$group
.append( this.getExpandCollapseTool().$element
);
8185 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
8187 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
8188 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
8189 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
8190 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
8191 this.getExpandCollapseTool().$element
.css( 'display', 'inline-block' );
8194 this.updateCollapsibleState();
8197 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
8198 if ( this.expandCollapseTool
=== undefined ) {
8199 var ExpandCollapseTool = function () {
8200 ExpandCollapseTool
.super.apply( this, arguments
);
8203 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
8205 ExpandCollapseTool
.prototype.onSelect = function () {
8206 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
8207 this.toolGroup
.updateCollapsibleState();
8208 this.setActive( false );
8210 ExpandCollapseTool
.prototype.onUpdateState = function () {
8211 // Do nothing. Tool interface requires an implementation of this function.
8214 ExpandCollapseTool
.static.name
= 'more-fewer';
8216 this.expandCollapseTool
= new ExpandCollapseTool( this );
8218 return this.expandCollapseTool
;
8224 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
8225 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
8227 // Do not close the popup when the user wants to show more/fewer tools
8228 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
8229 // Prevent the popup list from being hidden
8230 this.setActive( true );
8236 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
8239 this.getExpandCollapseTool()
8240 .setIcon( this.expanded
? 'collapse' : 'expand' )
8241 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
8243 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
8244 this.collapsibleTools
[i
].toggle( this.expanded
);
8249 * Drop down menu layout of tools as selectable menu items.
8252 * @extends OO.ui.PopupToolGroup
8255 * @param {OO.ui.Toolbar} toolbar
8256 * @param {Object} [config] Configuration options
8258 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
8259 // Configuration initialization
8260 config
= config
|| {};
8262 // Parent constructor
8263 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
8266 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
8269 this.$element
.addClass( 'oo-ui-menuToolGroup' );
8274 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
8276 /* Static Properties */
8278 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
8280 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
8285 * Handle the toolbar state being updated.
8287 * When the state changes, the title of each active item in the menu will be joined together and
8288 * used as a label for the group. The label will be empty if none of the items are active.
8290 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
8294 for ( name
in this.tools
) {
8295 if ( this.tools
[name
].isActive() ) {
8296 labelTexts
.push( this.tools
[name
].getTitle() );
8300 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
8304 * Tool that shows a popup when selected.
8308 * @extends OO.ui.Tool
8309 * @mixins OO.ui.PopupElement
8312 * @param {OO.ui.Toolbar} toolbar
8313 * @param {Object} [config] Configuration options
8315 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
8316 // Parent constructor
8317 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
8319 // Mixin constructors
8320 OO
.ui
.PopupElement
.call( this, config
);
8324 .addClass( 'oo-ui-popupTool' )
8325 .append( this.popup
.$element
);
8330 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
8331 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
8336 * Handle the tool being selected.
8340 OO
.ui
.PopupTool
.prototype.onSelect = function () {
8341 if ( !this.isDisabled() ) {
8342 this.popup
.toggle();
8344 this.setActive( false );
8349 * Handle the toolbar state being updated.
8353 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
8354 this.setActive( false );
8358 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
8360 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
8364 * @extends OO.ui.GroupElement
8367 * @param {Object} [config] Configuration options
8369 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
8370 // Parent constructor
8371 OO
.ui
.GroupWidget
.super.call( this, config
);
8376 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
8381 * Set the disabled state of the widget.
8383 * This will also update the disabled state of child widgets.
8385 * @param {boolean} disabled Disable widget
8388 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
8392 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
8393 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
8395 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
8397 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
8398 this.items
[i
].updateDisabled();
8406 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
8408 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
8409 * allows bidirectional communication.
8411 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
8418 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
8425 * Check if widget is disabled.
8427 * Checks parent if present, making disabled state inheritable.
8429 * @return {boolean} Widget is disabled
8431 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
8432 return this.disabled
||
8433 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
8437 * Set group element is in.
8439 * @param {OO.ui.GroupElement|null} group Group element, null if none
8442 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
8444 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
8445 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
8447 // Initialize item disabled states
8448 this.updateDisabled();
8454 * Mixin that adds a menu showing suggested values for a text input.
8456 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
8458 * Subclasses that set the value of #lookupInput from their `choose` or `select` handler should
8459 * be aware that this will cause new suggestions to be looked up for the new value. If this is
8460 * not desired, disable lookups with #setLookupsDisabled, then set the value, then re-enable lookups.
8466 * @param {OO.ui.TextInputWidget} input Input widget
8467 * @param {Object} [config] Configuration options
8468 * @cfg {jQuery} [$overlay] Overlay for dropdown; defaults to relative positioning
8469 * @cfg {jQuery} [$container=input.$element] Element to render menu under
8471 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
8472 // Configuration initialization
8473 config
= config
|| {};
8476 this.lookupInput
= input
;
8477 this.$overlay
= config
.$overlay
|| this.$element
;
8478 this.lookupMenu
= new OO
.ui
.TextInputMenuSelectWidget( this, {
8479 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
8480 input
: this.lookupInput
,
8481 $container
: config
.$container
8483 this.lookupCache
= {};
8484 this.lookupQuery
= null;
8485 this.lookupRequest
= null;
8486 this.lookupsDisabled
= false;
8487 this.lookupInputFocused
= false;
8490 this.lookupInput
.$input
.on( {
8491 focus
: this.onLookupInputFocus
.bind( this ),
8492 blur
: this.onLookupInputBlur
.bind( this ),
8493 mousedown
: this.onLookupInputMouseDown
.bind( this )
8495 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
8496 this.lookupMenu
.connect( this, { toggle
: 'onLookupMenuToggle' } );
8499 this.$element
.addClass( 'oo-ui-lookupWidget' );
8500 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
8501 this.$overlay
.append( this.lookupMenu
.$element
);
8507 * Handle input focus event.
8509 * @param {jQuery.Event} e Input focus event
8511 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
8512 this.lookupInputFocused
= true;
8513 this.populateLookupMenu();
8517 * Handle input blur event.
8519 * @param {jQuery.Event} e Input blur event
8521 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
8522 this.closeLookupMenu();
8523 this.lookupInputFocused
= false;
8527 * Handle input mouse down event.
8529 * @param {jQuery.Event} e Input mouse down event
8531 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
8532 // Only open the menu if the input was already focused.
8533 // This way we allow the user to open the menu again after closing it with Esc
8534 // by clicking in the input. Opening (and populating) the menu when initially
8535 // clicking into the input is handled by the focus handler.
8536 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
8537 this.populateLookupMenu();
8542 * Handle input change event.
8544 * @param {string} value New input value
8546 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
8547 if ( this.lookupInputFocused
) {
8548 this.populateLookupMenu();
8553 * Handle the lookup menu being shown/hidden.
8554 * @param {boolean} visible Whether the lookup menu is now visible.
8556 OO
.ui
.LookupInputWidget
.prototype.onLookupMenuToggle = function ( visible
) {
8558 // When the menu is hidden, abort any active request and clear the menu.
8559 // This has to be done here in addition to closeLookupMenu(), because
8560 // MenuSelectWidget will close itself when the user presses Esc.
8561 this.abortLookupRequest();
8562 this.lookupMenu
.clearItems();
8569 * @return {OO.ui.TextInputMenuSelectWidget}
8571 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
8572 return this.lookupMenu
;
8576 * Disable or re-enable lookups.
8578 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
8580 * @param {boolean} disabled Disable lookups
8582 OO
.ui
.LookupInputWidget
.prototype.setLookupsDisabled = function ( disabled
) {
8583 this.lookupsDisabled
= !!disabled
;
8587 * Open the menu. If there are no entries in the menu, this does nothing.
8591 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
8592 if ( !this.lookupMenu
.isEmpty() ) {
8593 this.lookupMenu
.toggle( true );
8599 * Close the menu, empty it, and abort any pending request.
8603 OO
.ui
.LookupInputWidget
.prototype.closeLookupMenu = function () {
8604 this.lookupMenu
.toggle( false );
8605 this.abortLookupRequest();
8606 this.lookupMenu
.clearItems();
8611 * Request menu items based on the input's current value, and when they arrive,
8612 * populate the menu with these items and show the menu.
8614 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
8618 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
8620 value
= this.lookupInput
.getValue();
8622 if ( this.lookupsDisabled
) {
8626 // If the input is empty, clear the menu
8627 if ( value
=== '' ) {
8628 this.closeLookupMenu();
8629 // Skip population if there is already a request pending for the current value
8630 } else if ( value
!== this.lookupQuery
) {
8631 this.getLookupMenuItems()
8632 .done( function ( items
) {
8633 widget
.lookupMenu
.clearItems();
8634 if ( items
.length
) {
8638 widget
.initializeLookupMenuSelection();
8640 widget
.lookupMenu
.toggle( false );
8643 .fail( function () {
8644 widget
.lookupMenu
.clearItems();
8652 * Select and highlight the first selectable item in the menu.
8656 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
8657 if ( !this.lookupMenu
.getSelectedItem() ) {
8658 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
8660 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
8664 * Get lookup menu items for the current query.
8666 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
8667 * of the done event. If the request was aborted to make way for a subsequent request,
8668 * this promise will not be rejected: it will remain pending forever.
8670 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
8672 value
= this.lookupInput
.getValue(),
8673 deferred
= $.Deferred(),
8676 this.abortLookupRequest();
8677 if ( Object
.prototype.hasOwnProperty
.call( this.lookupCache
, value
) ) {
8678 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
8680 this.lookupInput
.pushPending();
8681 this.lookupQuery
= value
;
8682 ourRequest
= this.lookupRequest
= this.getLookupRequest();
8684 .always( function () {
8685 // We need to pop pending even if this is an old request, otherwise
8686 // the widget will remain pending forever.
8687 // TODO: this assumes that an aborted request will fail or succeed soon after
8688 // being aborted, or at least eventually. It would be nice if we could popPending()
8689 // at abort time, but only if we knew that we hadn't already called popPending()
8690 // for that request.
8691 widget
.lookupInput
.popPending();
8693 .done( function ( data
) {
8694 // If this is an old request (and aborting it somehow caused it to still succeed),
8695 // ignore its success completely
8696 if ( ourRequest
=== widget
.lookupRequest
) {
8697 widget
.lookupQuery
= null;
8698 widget
.lookupRequest
= null;
8699 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
8700 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
8703 .fail( function () {
8704 // If this is an old request (or a request failing because it's being aborted),
8705 // ignore its failure completely
8706 if ( ourRequest
=== widget
.lookupRequest
) {
8707 widget
.lookupQuery
= null;
8708 widget
.lookupRequest
= null;
8713 return deferred
.promise();
8717 * Abort the currently pending lookup request, if any.
8719 OO
.ui
.LookupInputWidget
.prototype.abortLookupRequest = function () {
8720 var oldRequest
= this.lookupRequest
;
8722 // First unset this.lookupRequest to the fail handler will notice
8723 // that the request is no longer current
8724 this.lookupRequest
= null;
8725 this.lookupQuery
= null;
8731 * Get a new request object of the current lookup query value.
8734 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
8736 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
8737 // Stub, implemented in subclass
8742 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
8745 * @param {Mixed} data Cached result data, usually an array
8746 * @return {OO.ui.MenuOptionWidget[]} Menu items
8748 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
8749 // Stub, implemented in subclass
8754 * Get lookup cache item from server response data.
8757 * @param {Mixed} data Response from server
8758 * @return {Mixed} Cached result data
8760 OO
.ui
.LookupInputWidget
.prototype.getLookupCacheItemFromData = function () {
8761 // Stub, implemented in subclass
8766 * Set of controls for an OO.ui.OutlineSelectWidget.
8768 * Controls include moving items up and down, removing items, and adding different kinds of items.
8771 * @extends OO.ui.Widget
8772 * @mixins OO.ui.GroupElement
8773 * @mixins OO.ui.IconElement
8776 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
8777 * @param {Object} [config] Configuration options
8779 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
8780 // Configuration initialization
8781 config
= $.extend( { icon
: 'add' }, config
);
8783 // Parent constructor
8784 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
8786 // Mixin constructors
8787 OO
.ui
.GroupElement
.call( this, config
);
8788 OO
.ui
.IconElement
.call( this, config
);
8791 this.outline
= outline
;
8792 this.$movers
= this.$( '<div>' );
8793 this.upButton
= new OO
.ui
.ButtonWidget( {
8797 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
8799 this.downButton
= new OO
.ui
.ButtonWidget( {
8803 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
8805 this.removeButton
= new OO
.ui
.ButtonWidget( {
8809 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
8813 outline
.connect( this, {
8814 select
: 'onOutlineChange',
8815 add
: 'onOutlineChange',
8816 remove
: 'onOutlineChange'
8818 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
8819 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
8820 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
8823 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
8824 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
8826 .addClass( 'oo-ui-outlineControlsWidget-movers' )
8827 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
8828 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
8833 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
8834 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
8835 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
8841 * @param {number} places Number of places to move
8851 * Handle outline change events.
8853 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
8854 var i
, len
, firstMovable
, lastMovable
,
8855 items
= this.outline
.getItems(),
8856 selectedItem
= this.outline
.getSelectedItem(),
8857 movable
= selectedItem
&& selectedItem
.isMovable(),
8858 removable
= selectedItem
&& selectedItem
.isRemovable();
8863 while ( ++i
< len
) {
8864 if ( items
[i
].isMovable() ) {
8865 firstMovable
= items
[i
];
8871 if ( items
[i
].isMovable() ) {
8872 lastMovable
= items
[i
];
8877 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
8878 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
8879 this.removeButton
.setDisabled( !removable
);
8883 * Mixin for widgets with a boolean on/off state.
8889 * @param {Object} [config] Configuration options
8890 * @cfg {boolean} [value=false] Initial value
8892 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8893 // Configuration initialization
8894 config
= config
|| {};
8900 this.$element
.addClass( 'oo-ui-toggleWidget' );
8901 this.setValue( !!config
.value
);
8908 * @param {boolean} value Changed value
8914 * Get the value of the toggle.
8918 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8923 * Set the value of the toggle.
8925 * @param {boolean} value New value
8929 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8931 if ( this.value
!== value
) {
8933 this.emit( 'change', value
);
8934 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8935 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8941 * Group widget for multiple related buttons.
8943 * Use together with OO.ui.ButtonWidget.
8946 * @extends OO.ui.Widget
8947 * @mixins OO.ui.GroupElement
8950 * @param {Object} [config] Configuration options
8951 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
8953 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
8954 // Configuration initialization
8955 config
= config
|| {};
8957 // Parent constructor
8958 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
8960 // Mixin constructors
8961 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8964 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
8965 if ( $.isArray( config
.items
) ) {
8966 this.addItems( config
.items
);
8972 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
8973 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
8976 * Generic widget for buttons.
8979 * @extends OO.ui.Widget
8980 * @mixins OO.ui.ButtonElement
8981 * @mixins OO.ui.IconElement
8982 * @mixins OO.ui.IndicatorElement
8983 * @mixins OO.ui.LabelElement
8984 * @mixins OO.ui.TitledElement
8985 * @mixins OO.ui.FlaggedElement
8988 * @param {Object} [config] Configuration options
8989 * @cfg {string} [href] Hyperlink to visit when clicked
8990 * @cfg {string} [target] Target to open hyperlink in
8992 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8993 // Configuration initialization
8994 config
= config
|| {};
8996 // Parent constructor
8997 OO
.ui
.ButtonWidget
.super.call( this, config
);
8999 // Mixin constructors
9000 OO
.ui
.ButtonElement
.call( this, config
);
9001 OO
.ui
.IconElement
.call( this, config
);
9002 OO
.ui
.IndicatorElement
.call( this, config
);
9003 OO
.ui
.LabelElement
.call( this, config
);
9004 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
9005 OO
.ui
.FlaggedElement
.call( this, config
);
9010 this.isHyperlink
= false;
9014 click
: this.onClick
.bind( this ),
9015 keypress
: this.onKeyPress
.bind( this )
9019 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
9021 .addClass( 'oo-ui-buttonWidget' )
9022 .append( this.$button
);
9023 this.setHref( config
.href
);
9024 this.setTarget( config
.target
);
9029 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
9030 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
9031 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
9032 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
9033 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
9034 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
9035 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
9046 * Handles mouse click events.
9048 * @param {jQuery.Event} e Mouse click event
9051 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
9052 if ( !this.isDisabled() ) {
9053 this.emit( 'click' );
9054 if ( this.isHyperlink
) {
9062 * Handles keypress events.
9064 * @param {jQuery.Event} e Keypress event
9067 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
9068 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9069 this.emit( 'click' );
9070 if ( this.isHyperlink
) {
9078 * Get hyperlink location.
9080 * @return {string} Hyperlink location
9082 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
9087 * Get hyperlink target.
9089 * @return {string} Hyperlink target
9091 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
9096 * Set hyperlink location.
9098 * @param {string|null} href Hyperlink location, null to remove
9100 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
9101 href
= typeof href
=== 'string' ? href
: null;
9103 if ( href
!== this.href
) {
9105 if ( href
!== null ) {
9106 this.$button
.attr( 'href', href
);
9107 this.isHyperlink
= true;
9109 this.$button
.removeAttr( 'href' );
9110 this.isHyperlink
= false;
9118 * Set hyperlink target.
9120 * @param {string|null} target Hyperlink target, null to remove
9122 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
9123 target
= typeof target
=== 'string' ? target
: null;
9125 if ( target
!== this.target
) {
9126 this.target
= target
;
9127 if ( target
!== null ) {
9128 this.$button
.attr( 'target', target
);
9130 this.$button
.removeAttr( 'target' );
9138 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
9141 * @extends OO.ui.ButtonWidget
9142 * @mixins OO.ui.PendingElement
9145 * @param {Object} [config] Configuration options
9146 * @cfg {string} [action] Symbolic action name
9147 * @cfg {string[]} [modes] Symbolic mode names
9148 * @cfg {boolean} [framed=false] Render button with a frame
9150 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
9151 // Configuration initialization
9152 config
= $.extend( { framed
: false }, config
);
9154 // Parent constructor
9155 OO
.ui
.ActionWidget
.super.call( this, config
);
9157 // Mixin constructors
9158 OO
.ui
.PendingElement
.call( this, config
);
9161 this.action
= config
.action
|| '';
9162 this.modes
= config
.modes
|| [];
9167 this.$element
.addClass( 'oo-ui-actionWidget' );
9172 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
9173 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
9184 * Check if action is available in a certain mode.
9186 * @param {string} mode Name of mode
9187 * @return {boolean} Has mode
9189 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
9190 return this.modes
.indexOf( mode
) !== -1;
9194 * Get symbolic action name.
9198 OO
.ui
.ActionWidget
.prototype.getAction = function () {
9203 * Get symbolic action name.
9207 OO
.ui
.ActionWidget
.prototype.getModes = function () {
9208 return this.modes
.slice();
9212 * Emit a resize event if the size has changed.
9216 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
9219 if ( this.isElementAttached() ) {
9220 width
= this.$element
.width();
9221 height
= this.$element
.height();
9223 if ( width
!== this.width
|| height
!== this.height
) {
9225 this.height
= height
;
9226 this.emit( 'resize' );
9236 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
9238 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
9239 this.propagateResize();
9247 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
9249 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
9250 this.propagateResize();
9258 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
9260 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
9261 this.propagateResize();
9269 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
9271 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
9272 this.propagateResize();
9278 * Toggle visibility of button.
9280 * @param {boolean} [show] Show button, omit to toggle visibility
9283 OO
.ui
.ActionWidget
.prototype.toggle = function () {
9285 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
9286 this.propagateResize();
9292 * Button that shows and hides a popup.
9295 * @extends OO.ui.ButtonWidget
9296 * @mixins OO.ui.PopupElement
9299 * @param {Object} [config] Configuration options
9301 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
9302 // Parent constructor
9303 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
9305 // Mixin constructors
9306 OO
.ui
.PopupElement
.call( this, config
);
9310 .addClass( 'oo-ui-popupButtonWidget' )
9311 .append( this.popup
.$element
);
9316 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
9317 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
9322 * Handles mouse click events.
9324 * @param {jQuery.Event} e Mouse click event
9326 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
9327 // Skip clicks within the popup
9328 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
9332 if ( !this.isDisabled() ) {
9333 this.popup
.toggle();
9335 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
9341 * Button that toggles on and off.
9344 * @extends OO.ui.ButtonWidget
9345 * @mixins OO.ui.ToggleWidget
9348 * @param {Object} [config] Configuration options
9349 * @cfg {boolean} [value=false] Initial value
9351 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
9352 // Configuration initialization
9353 config
= config
|| {};
9355 // Parent constructor
9356 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
9358 // Mixin constructors
9359 OO
.ui
.ToggleWidget
.call( this, config
);
9362 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
9367 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
9368 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
9375 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
9376 if ( !this.isDisabled() ) {
9377 this.setValue( !this.value
);
9381 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
9387 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
9389 if ( value
!== this.value
) {
9390 this.setActive( value
);
9393 // Parent method (from mixin)
9394 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
9400 * Dropdown menu of options.
9402 * Dropdown menus provide a control for accessing a menu and compose a menu within the widget, which
9403 * can be accessed using the #getMenu method.
9405 * Use with OO.ui.MenuOptionWidget.
9408 * @extends OO.ui.Widget
9409 * @mixins OO.ui.IconElement
9410 * @mixins OO.ui.IndicatorElement
9411 * @mixins OO.ui.LabelElement
9412 * @mixins OO.ui.TitledElement
9415 * @param {Object} [config] Configuration options
9416 * @cfg {Object} [menu] Configuration options to pass to menu widget
9418 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
9419 // Configuration initialization
9420 config
= $.extend( { indicator
: 'down' }, config
);
9422 // Parent constructor
9423 OO
.ui
.DropdownWidget
.super.call( this, config
);
9425 // Mixin constructors
9426 OO
.ui
.IconElement
.call( this, config
);
9427 OO
.ui
.IndicatorElement
.call( this, config
);
9428 OO
.ui
.LabelElement
.call( this, config
);
9429 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
9432 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
9433 this.$handle
= this.$( '<span>' );
9436 this.$element
.on( { click
: this.onClick
.bind( this ) } );
9437 this.menu
.connect( this, { select
: 'onMenuSelect' } );
9441 .addClass( 'oo-ui-dropdownWidget-handle' )
9442 .append( this.$icon
, this.$label
, this.$indicator
);
9444 .addClass( 'oo-ui-dropdownWidget' )
9445 .append( this.$handle
, this.menu
.$element
);
9450 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
9451 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IconElement
);
9452 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.IndicatorElement
);
9453 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.LabelElement
);
9454 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.TitledElement
);
9461 * @return {OO.ui.MenuSelectWidget} Menu of widget
9463 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
9468 * Handles menu select events.
9470 * @param {OO.ui.MenuOptionWidget} item Selected menu item
9472 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
9479 selectedLabel
= item
.getLabel();
9481 // If the label is a DOM element, clone it, because setLabel will append() it
9482 if ( selectedLabel
instanceof jQuery
) {
9483 selectedLabel
= selectedLabel
.clone();
9486 this.setLabel( selectedLabel
);
9490 * Handles mouse click events.
9492 * @param {jQuery.Event} e Mouse click event
9494 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
9495 // Skip clicks within the menu
9496 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
9500 if ( !this.isDisabled() ) {
9501 if ( this.menu
.isVisible() ) {
9502 this.menu
.toggle( false );
9504 this.menu
.toggle( true );
9513 * See OO.ui.IconElement for more information.
9516 * @extends OO.ui.Widget
9517 * @mixins OO.ui.IconElement
9518 * @mixins OO.ui.TitledElement
9521 * @param {Object} [config] Configuration options
9523 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
9524 // Configuration initialization
9525 config
= config
|| {};
9527 // Parent constructor
9528 OO
.ui
.IconWidget
.super.call( this, config
);
9530 // Mixin constructors
9531 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
9532 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9535 this.$element
.addClass( 'oo-ui-iconWidget' );
9540 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
9541 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
9542 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
9544 /* Static Properties */
9546 OO
.ui
.IconWidget
.static.tagName
= 'span';
9551 * See OO.ui.IndicatorElement for more information.
9554 * @extends OO.ui.Widget
9555 * @mixins OO.ui.IndicatorElement
9556 * @mixins OO.ui.TitledElement
9559 * @param {Object} [config] Configuration options
9561 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
9562 // Configuration initialization
9563 config
= config
|| {};
9565 // Parent constructor
9566 OO
.ui
.IndicatorWidget
.super.call( this, config
);
9568 // Mixin constructors
9569 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
9570 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
9573 this.$element
.addClass( 'oo-ui-indicatorWidget' );
9578 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
9579 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
9580 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
9582 /* Static Properties */
9584 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
9587 * Base class for input widgets.
9591 * @extends OO.ui.Widget
9592 * @mixins OO.ui.FlaggedElement
9595 * @param {Object} [config] Configuration options
9596 * @cfg {string} [name=''] HTML input name
9597 * @cfg {string} [value=''] Input value
9598 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
9600 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
9601 // Configuration initialization
9602 config
= config
|| {};
9604 // Parent constructor
9605 OO
.ui
.InputWidget
.super.call( this, config
);
9607 // Mixin constructors
9608 OO
.ui
.FlaggedElement
.call( this, config
);
9611 this.$input
= this.getInputElement( config
);
9613 this.inputFilter
= config
.inputFilter
;
9616 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
9620 .attr( 'name', config
.name
)
9621 .prop( 'disabled', this.isDisabled() );
9622 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
, $( '<span>' ) );
9623 this.setValue( config
.value
);
9628 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
9629 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
9635 * @param {string} value
9641 * Get input element.
9644 * @param {Object} [config] Configuration options
9645 * @return {jQuery} Input element
9647 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
9648 return this.$( '<input>' );
9652 * Handle potentially value-changing events.
9654 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
9656 OO
.ui
.InputWidget
.prototype.onEdit = function () {
9658 if ( !this.isDisabled() ) {
9659 // Allow the stack to clear so the value will be updated
9660 setTimeout( function () {
9661 widget
.setValue( widget
.$input
.val() );
9667 * Get the value of the input.
9669 * @return {string} Input value
9671 OO
.ui
.InputWidget
.prototype.getValue = function () {
9676 * Sets the direction of the current input, either RTL or LTR
9678 * @param {boolean} isRTL
9680 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
9682 this.$input
.removeClass( 'oo-ui-ltr' );
9683 this.$input
.addClass( 'oo-ui-rtl' );
9685 this.$input
.removeClass( 'oo-ui-rtl' );
9686 this.$input
.addClass( 'oo-ui-ltr' );
9691 * Set the value of the input.
9693 * @param {string} value New value
9697 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
9698 value
= this.cleanUpValue( value
);
9699 // Update the DOM if it has changed. Note that with cleanUpValue, it
9700 // is possible for the DOM value to change without this.value changing.
9701 if ( this.$input
.val() !== value
) {
9702 this.$input
.val( value
);
9704 if ( this.value
!== value
) {
9706 this.emit( 'change', this.value
);
9712 * Clean up incoming value.
9714 * Ensures value is a string, and converts undefined and null to empty string.
9717 * @param {string} value Original value
9718 * @return {string} Cleaned up value
9720 OO
.ui
.InputWidget
.prototype.cleanUpValue = function ( value
) {
9721 if ( value
=== undefined || value
=== null ) {
9723 } else if ( this.inputFilter
) {
9724 return this.inputFilter( String( value
) );
9726 return String( value
);
9731 * Simulate the behavior of clicking on a label bound to this input.
9733 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
9734 if ( !this.isDisabled() ) {
9735 if ( this.$input
.is( ':checkbox,:radio' ) ) {
9736 this.$input
.click();
9737 } else if ( this.$input
.is( ':input' ) ) {
9738 this.$input
[0].focus();
9746 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
9747 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
9748 if ( this.$input
) {
9749 this.$input
.prop( 'disabled', this.isDisabled() );
9759 OO
.ui
.InputWidget
.prototype.focus = function () {
9760 this.$input
[0].focus();
9769 OO
.ui
.InputWidget
.prototype.blur = function () {
9770 this.$input
[0].blur();
9775 * A button that is an input widget. Intended to be used within a OO.ui.FormLayout.
9778 * @extends OO.ui.InputWidget
9779 * @mixins OO.ui.ButtonElement
9780 * @mixins OO.ui.IconElement
9781 * @mixins OO.ui.IndicatorElement
9782 * @mixins OO.ui.LabelElement
9783 * @mixins OO.ui.TitledElement
9784 * @mixins OO.ui.FlaggedElement
9787 * @param {Object} [config] Configuration options
9788 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
9789 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
9790 * if you need IE 6 support in a form with multiple buttons. If you use this option, icons and
9791 * indicators will not be displayed, it won't be possible to have a non-plaintext label, and it
9792 * won't be possible to set a value (which will internally become identical to the label).
9794 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
9795 // Configuration initialization
9796 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
9798 // Properties (must be set before parent constructor, which calls #setValue)
9799 this.useInputTag
= config
.useInputTag
;
9801 // Parent constructor
9802 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
9804 // Mixin constructors
9805 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
9806 OO
.ui
.IconElement
.call( this, config
);
9807 OO
.ui
.IndicatorElement
.call( this, config
);
9808 OO
.ui
.LabelElement
.call( this, config
);
9809 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
9810 OO
.ui
.FlaggedElement
.call( this, config
);
9814 click
: this.onClick
.bind( this ),
9815 keypress
: this.onKeyPress
.bind( this )
9819 if ( !config
.useInputTag
) {
9820 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
9822 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
9827 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
9828 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
9829 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
9830 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
9831 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
9832 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
9833 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
9844 * Get input element.
9847 * @param {Object} [config] Configuration options
9848 * @return {jQuery} Input element
9850 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
9851 // Configuration initialization
9852 config
= config
|| {};
9854 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
9856 return this.$( html
);
9862 * Overridden to support setting the 'value' of `<input/>` elements.
9864 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
9865 * text; or null for no label
9868 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
9869 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
9871 if ( this.useInputTag
) {
9872 if ( typeof label
=== 'function' ) {
9873 label
= OO
.ui
.resolveMsg( label
);
9875 if ( label
instanceof jQuery
) {
9876 label
= label
.text();
9881 this.$input
.val( label
);
9888 * Set the value of the input.
9890 * Overridden to disable for `<input/>` elements, which have value identical to the label.
9892 * @param {string} value New value
9895 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
9896 if ( !this.useInputTag
) {
9897 OO
.ui
.ButtonInputWidget
.super.prototype.setValue
.call( this, value
);
9903 * Handles mouse click events.
9905 * @param {jQuery.Event} e Mouse click event
9908 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
9909 if ( !this.isDisabled() ) {
9910 this.emit( 'click' );
9916 * Handles keypress events.
9918 * @param {jQuery.Event} e Keypress event
9921 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
9922 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9923 this.emit( 'click' );
9929 * Checkbox input widget.
9932 * @extends OO.ui.InputWidget
9935 * @param {Object} [config] Configuration options
9937 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
9938 // Parent constructor
9939 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
9942 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
9947 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
9952 * Get input element.
9955 * @return {jQuery} Input element
9957 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
9958 return this.$( '<input type="checkbox" />' );
9962 * Get checked state of the checkbox
9964 * @return {boolean} If the checkbox is checked
9966 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
9971 * Set checked state of the checkbox
9973 * @param {boolean} value New value
9975 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
9977 if ( this.value
!== value
) {
9979 this.$input
.prop( 'checked', this.value
);
9980 this.emit( 'change', this.value
);
9987 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
9989 if ( !this.isDisabled() ) {
9990 // Allow the stack to clear so the value will be updated
9991 setTimeout( function () {
9992 widget
.setValue( widget
.$input
.prop( 'checked' ) );
9998 * Radio input widget.
10000 * Radio buttons only make sense as a set, and you probably want to use the OO.ui.RadioSelectWidget
10001 * class instead of using this class directly.
10003 * This class doesn't make it possible to learn whether the radio button is selected ("pressed").
10006 * @extends OO.ui.InputWidget
10009 * @param {Object} [config] Configuration options
10010 * @param {boolean} [config.selected=false] Whether the radio button is initially selected
10012 OO
.ui
.RadioInputWidget
= function OoUiRadioInputWidget( config
) {
10013 // Parent constructor
10014 OO
.ui
.RadioInputWidget
.super.call( this, config
);
10017 this.$element
.addClass( 'oo-ui-radioInputWidget' );
10018 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
10023 OO
.inheritClass( OO
.ui
.RadioInputWidget
, OO
.ui
.InputWidget
);
10028 * Get input element.
10031 * @return {jQuery} Input element
10033 OO
.ui
.RadioInputWidget
.prototype.getInputElement = function () {
10034 return this.$( '<input type="radio" />' );
10040 OO
.ui
.RadioInputWidget
.prototype.onEdit = function () {
10041 // RadioInputWidget doesn't track its state.
10045 * Set selection state of this radio button.
10047 * @param {boolean} state Whether the button is selected
10050 OO
.ui
.RadioInputWidget
.prototype.setSelected = function ( state
) {
10051 // RadioInputWidget doesn't track its state.
10052 this.$input
.prop( 'checked', state
);
10057 * Check if this radio button is selected.
10059 * @return {boolean} Radio is selected
10061 OO
.ui
.RadioInputWidget
.prototype.isSelected = function () {
10062 return this.$input
.prop( 'checked' );
10066 * Input widget with a text field.
10069 * @extends OO.ui.InputWidget
10070 * @mixins OO.ui.IconElement
10071 * @mixins OO.ui.IndicatorElement
10072 * @mixins OO.ui.PendingElement
10075 * @param {Object} [config] Configuration options
10076 * @cfg {string} [type='text'] HTML tag `type` attribute
10077 * @cfg {string} [placeholder] Placeholder text
10078 * @cfg {boolean} [readOnly=false] Prevent changes
10079 * @cfg {boolean} [multiline=false] Allow multiple lines of text
10080 * @cfg {boolean} [autosize=false] Automatically resize to fit content
10081 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
10082 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
10083 * one, see #static-validationPatterns)
10085 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
10086 // Configuration initialization
10087 config
= $.extend( { readOnly
: false }, config
);
10089 // Parent constructor
10090 OO
.ui
.TextInputWidget
.super.call( this, config
);
10092 // Mixin constructors
10093 OO
.ui
.IconElement
.call( this, config
);
10094 OO
.ui
.IndicatorElement
.call( this, config
);
10095 OO
.ui
.PendingElement
.call( this, config
);
10098 this.readOnly
= false;
10099 this.multiline
= !!config
.multiline
;
10100 this.autosize
= !!config
.autosize
;
10101 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
10102 this.validate
= null;
10104 // Clone for resizing
10105 if ( this.autosize
) {
10106 this.$clone
= this.$input
10108 .insertAfter( this.$input
)
10112 this.setValidation( config
.validate
);
10116 keypress
: this.onKeyPress
.bind( this ),
10117 blur
: this.setValidityFlag
.bind( this )
10119 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
10120 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
10121 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
10125 .addClass( 'oo-ui-textInputWidget' )
10126 .append( this.$icon
, this.$indicator
);
10127 this.setReadOnly( config
.readOnly
);
10128 if ( config
.placeholder
) {
10129 this.$input
.attr( 'placeholder', config
.placeholder
);
10131 this.$element
.attr( 'role', 'textbox' );
10136 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
10137 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
10138 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
10139 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
10141 /* Static properties */
10143 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
10151 * User presses enter inside the text box.
10153 * Not called if input is multiline.
10159 * User clicks the icon.
10165 * User clicks the indicator.
10173 * Handle icon mouse down events.
10175 * @param {jQuery.Event} e Mouse down event
10178 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
10179 if ( e
.which
=== 1 ) {
10180 this.$input
[0].focus();
10181 this.emit( 'icon' );
10187 * Handle indicator mouse down events.
10189 * @param {jQuery.Event} e Mouse down event
10192 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
10193 if ( e
.which
=== 1 ) {
10194 this.$input
[0].focus();
10195 this.emit( 'indicator' );
10201 * Handle key press events.
10203 * @param {jQuery.Event} e Key press event
10204 * @fires enter If enter key is pressed and input is not multiline
10206 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
10207 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
10208 this.emit( 'enter', e
);
10213 * Handle element attach events.
10215 * @param {jQuery.Event} e Element attach event
10217 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
10224 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
10228 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
10234 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
10236 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
10238 this.setValidityFlag();
10244 * Check if the widget is read-only.
10246 * @return {boolean}
10248 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
10249 return this.readOnly
;
10253 * Set the read-only state of the widget.
10255 * This should probably change the widget's appearance and prevent it from being used.
10257 * @param {boolean} state Make input read-only
10260 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
10261 this.readOnly
= !!state
;
10262 this.$input
.prop( 'readOnly', this.readOnly
);
10267 * Automatically adjust the size of the text input.
10269 * This only affects multi-line inputs that are auto-sized.
10273 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
10274 var scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
10276 if ( this.multiline
&& this.autosize
&& this.$input
.val() !== this.valCache
) {
10278 .val( this.$input
.val() )
10279 .attr( 'rows', '' )
10280 // Set inline height property to 0 to measure scroll height
10281 .css( 'height', 0 );
10283 this.$clone
[0].style
.display
= 'block';
10285 this.valCache
= this.$input
.val();
10287 scrollHeight
= this.$clone
[0].scrollHeight
;
10289 // Remove inline height property to measure natural heights
10290 this.$clone
.css( 'height', '' );
10291 innerHeight
= this.$clone
.innerHeight();
10292 outerHeight
= this.$clone
.outerHeight();
10294 // Measure max rows height
10296 .attr( 'rows', this.maxRows
)
10297 .css( 'height', 'auto' )
10299 maxInnerHeight
= this.$clone
.innerHeight();
10301 // Difference between reported innerHeight and scrollHeight with no scrollbars present
10302 // Equals 1 on Blink-based browsers and 0 everywhere else
10303 measurementError
= maxInnerHeight
- this.$clone
[0].scrollHeight
;
10304 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
10306 this.$clone
[0].style
.display
= 'none';
10308 // Only apply inline height when expansion beyond natural height is needed
10309 if ( idealHeight
> innerHeight
) {
10310 // Use the difference between the inner and outer height as a buffer
10311 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
10313 this.$input
.css( 'height', '' );
10320 * Get input element.
10323 * @param {Object} [config] Configuration options
10324 * @return {jQuery} Input element
10326 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
10327 // Configuration initialization
10328 config
= config
|| {};
10330 var type
= config
.type
|| 'text';
10332 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="' + type
+ '" />' );
10336 * Check if input supports multiple lines.
10338 * @return {boolean}
10340 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
10341 return !!this.multiline
;
10345 * Check if input automatically adjusts its size.
10347 * @return {boolean}
10349 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
10350 return !!this.autosize
;
10354 * Select the contents of the input.
10358 OO
.ui
.TextInputWidget
.prototype.select = function () {
10359 this.$input
.select();
10364 * Sets the validation pattern to use.
10365 * @param {RegExp|string|null} validate Regular expression (or symbolic name referencing
10366 * one, see #static-validationPatterns)
10368 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
10369 if ( validate
instanceof RegExp
) {
10370 this.validate
= validate
;
10372 this.validate
= this.constructor.static.validationPatterns
[validate
] || /.*/;
10377 * Sets the 'invalid' flag appropriately.
10379 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
10381 this.isValid().done( function ( valid
) {
10382 widget
.setFlags( { invalid
: !valid
} );
10387 * Returns whether or not the current value is considered valid, according to the
10388 * supplied validation pattern.
10390 * @return {jQuery.Deferred}
10392 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
10393 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
10397 * Text input with a menu of optional values.
10400 * @extends OO.ui.Widget
10403 * @param {Object} [config] Configuration options
10404 * @cfg {Object} [menu] Configuration options to pass to menu widget
10405 * @cfg {Object} [input] Configuration options to pass to input widget
10406 * @cfg {jQuery} [$overlay] Overlay layer; defaults to relative positioning
10408 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
10409 // Configuration initialization
10410 config
= config
|| {};
10412 // Parent constructor
10413 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
10416 this.$overlay
= config
.$overlay
|| this.$element
;
10417 this.input
= new OO
.ui
.TextInputWidget( $.extend(
10418 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
10421 this.menu
= new OO
.ui
.TextInputMenuSelectWidget( this.input
, $.extend(
10423 $: OO
.ui
.Element
.static.getJQuery( this.$overlay
),
10426 disabled
: this.isDisabled()
10432 this.input
.connect( this, {
10433 change
: 'onInputChange',
10434 indicator
: 'onInputIndicator',
10435 enter
: 'onInputEnter'
10437 this.menu
.connect( this, {
10438 choose
: 'onMenuChoose',
10439 add
: 'onMenuItemsChange',
10440 remove
: 'onMenuItemsChange'
10444 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
10445 this.$overlay
.append( this.menu
.$element
);
10446 this.onMenuItemsChange();
10451 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
10456 * Get the combobox's menu.
10457 * @return {OO.ui.TextInputMenuSelectWidget} Menu widget
10459 OO
.ui
.ComboBoxWidget
.prototype.getMenu = function () {
10464 * Handle input change events.
10466 * @param {string} value New value
10468 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
10469 var match
= this.menu
.getItemFromData( value
);
10471 this.menu
.selectItem( match
);
10473 if ( !this.isDisabled() ) {
10474 this.menu
.toggle( true );
10479 * Handle input indicator events.
10481 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
10482 if ( !this.isDisabled() ) {
10483 this.menu
.toggle();
10488 * Handle input enter events.
10490 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
10491 if ( !this.isDisabled() ) {
10492 this.menu
.toggle( false );
10497 * Handle menu choose events.
10499 * @param {OO.ui.OptionWidget} item Chosen item
10501 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
10503 this.input
.setValue( item
.getData() );
10508 * Handle menu item change events.
10510 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
10511 var match
= this.menu
.getItemFromData( this.input
.getValue() );
10512 this.menu
.selectItem( match
);
10513 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
10519 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
10521 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
10523 if ( this.input
) {
10524 this.input
.setDisabled( this.isDisabled() );
10527 this.menu
.setDisabled( this.isDisabled() );
10537 * @extends OO.ui.Widget
10538 * @mixins OO.ui.LabelElement
10541 * @param {Object} [config] Configuration options
10542 * @cfg {OO.ui.InputWidget} [input] Input widget this label is for
10544 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
10545 // Configuration initialization
10546 config
= config
|| {};
10548 // Parent constructor
10549 OO
.ui
.LabelWidget
.super.call( this, config
);
10551 // Mixin constructors
10552 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
10553 OO
.ui
.TitledElement
.call( this, config
);
10556 this.input
= config
.input
;
10559 if ( this.input
instanceof OO
.ui
.InputWidget
) {
10560 this.$element
.on( 'click', this.onClick
.bind( this ) );
10564 this.$element
.addClass( 'oo-ui-labelWidget' );
10569 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
10570 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
10571 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
10573 /* Static Properties */
10575 OO
.ui
.LabelWidget
.static.tagName
= 'span';
10580 * Handles label mouse click events.
10582 * @param {jQuery.Event} e Mouse click event
10584 OO
.ui
.LabelWidget
.prototype.onClick = function () {
10585 this.input
.simulateLabelClick();
10590 * Generic option widget for use with OO.ui.SelectWidget.
10593 * @extends OO.ui.Widget
10594 * @mixins OO.ui.LabelElement
10595 * @mixins OO.ui.FlaggedElement
10598 * @param {Object} [config] Configuration options
10600 OO
.ui
.OptionWidget
= function OoUiOptionWidget( config
) {
10601 // Configuration initialization
10602 config
= config
|| {};
10604 // Parent constructor
10605 OO
.ui
.OptionWidget
.super.call( this, config
);
10607 // Mixin constructors
10608 OO
.ui
.ItemWidget
.call( this );
10609 OO
.ui
.LabelElement
.call( this, config
);
10610 OO
.ui
.FlaggedElement
.call( this, config
);
10613 this.selected
= false;
10614 this.highlighted
= false;
10615 this.pressed
= false;
10619 .data( 'oo-ui-optionWidget', this )
10620 .attr( 'role', 'option' )
10621 .addClass( 'oo-ui-optionWidget' )
10622 .append( this.$label
);
10627 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
10628 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
10629 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
10630 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
10632 /* Static Properties */
10634 OO
.ui
.OptionWidget
.static.selectable
= true;
10636 OO
.ui
.OptionWidget
.static.highlightable
= true;
10638 OO
.ui
.OptionWidget
.static.pressable
= true;
10640 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
10645 * Check if option can be selected.
10647 * @return {boolean} Item is selectable
10649 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
10650 return this.constructor.static.selectable
&& !this.isDisabled();
10654 * Check if option can be highlighted.
10656 * @return {boolean} Item is highlightable
10658 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
10659 return this.constructor.static.highlightable
&& !this.isDisabled();
10663 * Check if option can be pressed.
10665 * @return {boolean} Item is pressable
10667 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
10668 return this.constructor.static.pressable
&& !this.isDisabled();
10672 * Check if option is selected.
10674 * @return {boolean} Item is selected
10676 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
10677 return this.selected
;
10681 * Check if option is highlighted.
10683 * @return {boolean} Item is highlighted
10685 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
10686 return this.highlighted
;
10690 * Check if option is pressed.
10692 * @return {boolean} Item is pressed
10694 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
10695 return this.pressed
;
10699 * Set selected state.
10701 * @param {boolean} [state=false] Select option
10704 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
10705 if ( this.constructor.static.selectable
) {
10706 this.selected
= !!state
;
10707 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
10708 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
10709 this.scrollElementIntoView();
10711 this.updateThemeClasses();
10717 * Set highlighted state.
10719 * @param {boolean} [state=false] Highlight option
10722 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
10723 if ( this.constructor.static.highlightable
) {
10724 this.highlighted
= !!state
;
10725 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
10726 this.updateThemeClasses();
10732 * Set pressed state.
10734 * @param {boolean} [state=false] Press option
10737 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
10738 if ( this.constructor.static.pressable
) {
10739 this.pressed
= !!state
;
10740 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
10741 this.updateThemeClasses();
10747 * Make the option's highlight flash.
10749 * While flashing, the visual style of the pressed state is removed if present.
10751 * @return {jQuery.Promise} Promise resolved when flashing is done
10753 OO
.ui
.OptionWidget
.prototype.flash = function () {
10755 $element
= this.$element
,
10756 deferred
= $.Deferred();
10758 if ( !this.isDisabled() && this.constructor.static.pressable
) {
10759 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
10760 setTimeout( function () {
10761 // Restore original classes
10763 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
10764 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
10766 setTimeout( function () {
10767 deferred
.resolve();
10773 return deferred
.promise();
10777 * Option widget with an option icon and indicator.
10779 * Use together with OO.ui.SelectWidget.
10782 * @extends OO.ui.OptionWidget
10783 * @mixins OO.ui.IconElement
10784 * @mixins OO.ui.IndicatorElement
10787 * @param {Object} [config] Configuration options
10789 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( config
) {
10790 // Parent constructor
10791 OO
.ui
.DecoratedOptionWidget
.super.call( this, config
);
10793 // Mixin constructors
10794 OO
.ui
.IconElement
.call( this, config
);
10795 OO
.ui
.IndicatorElement
.call( this, config
);
10799 .addClass( 'oo-ui-decoratedOptionWidget' )
10800 .prepend( this.$icon
)
10801 .append( this.$indicator
);
10806 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
10807 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
10808 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
10811 * Option widget that looks like a button.
10813 * Use together with OO.ui.ButtonSelectWidget.
10816 * @extends OO.ui.DecoratedOptionWidget
10817 * @mixins OO.ui.ButtonElement
10820 * @param {Object} [config] Configuration options
10822 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
10823 // Parent constructor
10824 OO
.ui
.ButtonOptionWidget
.super.call( this, config
);
10826 // Mixin constructors
10827 OO
.ui
.ButtonElement
.call( this, config
);
10830 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
10831 this.$button
.append( this.$element
.contents() );
10832 this.$element
.append( this.$button
);
10837 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10838 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
10840 /* Static Properties */
10842 // Allow button mouse down events to pass through so they can be handled by the parent select widget
10843 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
10850 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
10851 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
10853 if ( this.constructor.static.selectable
) {
10854 this.setActive( state
);
10861 * Option widget that looks like a radio button.
10863 * Use together with OO.ui.RadioSelectWidget.
10866 * @extends OO.ui.OptionWidget
10869 * @param {Object} [config] Configuration options
10871 OO
.ui
.RadioOptionWidget
= function OoUiRadioOptionWidget( config
) {
10872 // Parent constructor
10873 OO
.ui
.RadioOptionWidget
.super.call( this, config
);
10876 this.radio
= new OO
.ui
.RadioInputWidget( { value
: config
.data
} );
10880 .addClass( 'oo-ui-radioOptionWidget' )
10881 .prepend( this.radio
.$element
);
10886 OO
.inheritClass( OO
.ui
.RadioOptionWidget
, OO
.ui
.OptionWidget
);
10888 /* Static Properties */
10890 OO
.ui
.RadioOptionWidget
.static.highlightable
= false;
10892 OO
.ui
.RadioOptionWidget
.static.pressable
= false;
10899 OO
.ui
.RadioOptionWidget
.prototype.setSelected = function ( state
) {
10900 OO
.ui
.RadioOptionWidget
.super.prototype.setSelected
.call( this, state
);
10902 this.radio
.setSelected( state
);
10908 * Item of an OO.ui.MenuSelectWidget.
10911 * @extends OO.ui.DecoratedOptionWidget
10914 * @param {Object} [config] Configuration options
10916 OO
.ui
.MenuOptionWidget
= function OoUiMenuOptionWidget( config
) {
10917 // Configuration initialization
10918 config
= $.extend( { icon
: 'check' }, config
);
10920 // Parent constructor
10921 OO
.ui
.MenuOptionWidget
.super.call( this, config
);
10925 .attr( 'role', 'menuitem' )
10926 .addClass( 'oo-ui-menuOptionWidget' );
10931 OO
.inheritClass( OO
.ui
.MenuOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10934 * Section to group one or more items in a OO.ui.MenuSelectWidget.
10937 * @extends OO.ui.DecoratedOptionWidget
10940 * @param {Object} [config] Configuration options
10942 OO
.ui
.MenuSectionOptionWidget
= function OoUiMenuSectionOptionWidget( config
) {
10943 // Parent constructor
10944 OO
.ui
.MenuSectionOptionWidget
.super.call( this, config
);
10947 this.$element
.addClass( 'oo-ui-menuSectionOptionWidget' );
10952 OO
.inheritClass( OO
.ui
.MenuSectionOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10954 /* Static Properties */
10956 OO
.ui
.MenuSectionOptionWidget
.static.selectable
= false;
10958 OO
.ui
.MenuSectionOptionWidget
.static.highlightable
= false;
10961 * Items for an OO.ui.OutlineSelectWidget.
10964 * @extends OO.ui.DecoratedOptionWidget
10967 * @param {Object} [config] Configuration options
10968 * @cfg {number} [level] Indentation level
10969 * @cfg {boolean} [movable] Allow modification from outline controls
10971 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
10972 // Configuration initialization
10973 config
= config
|| {};
10975 // Parent constructor
10976 OO
.ui
.OutlineOptionWidget
.super.call( this, config
);
10980 this.movable
= !!config
.movable
;
10981 this.removable
= !!config
.removable
;
10984 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
10985 this.setLevel( config
.level
);
10990 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10992 /* Static Properties */
10994 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
10996 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
10998 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
11000 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
11005 * Check if item is movable.
11007 * Movability is used by outline controls.
11009 * @return {boolean} Item is movable
11011 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
11012 return this.movable
;
11016 * Check if item is removable.
11018 * Removability is used by outline controls.
11020 * @return {boolean} Item is removable
11022 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
11023 return this.removable
;
11027 * Get indentation level.
11029 * @return {number} Indentation level
11031 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
11038 * Movability is used by outline controls.
11040 * @param {boolean} movable Item is movable
11043 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
11044 this.movable
= !!movable
;
11045 this.updateThemeClasses();
11050 * Set removability.
11052 * Removability is used by outline controls.
11054 * @param {boolean} movable Item is removable
11057 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
11058 this.removable
= !!removable
;
11059 this.updateThemeClasses();
11064 * Set indentation level.
11066 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
11069 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
11070 var levels
= this.constructor.static.levels
,
11071 levelClass
= this.constructor.static.levelClass
,
11074 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
11076 if ( this.level
=== i
) {
11077 this.$element
.addClass( levelClass
+ i
);
11079 this.$element
.removeClass( levelClass
+ i
);
11082 this.updateThemeClasses();
11088 * Container for content that is overlaid and positioned absolutely.
11091 * @extends OO.ui.Widget
11092 * @mixins OO.ui.LabelElement
11095 * @param {Object} [config] Configuration options
11096 * @cfg {number} [width=320] Width of popup in pixels
11097 * @cfg {number} [height] Height of popup, omit to use automatic height
11098 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
11099 * @cfg {string} [align='center'] Alignment of popup to origin
11100 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
11101 * @cfg {number} [containerPadding=10] How much padding to keep between popup and container
11102 * @cfg {jQuery} [$content] Content to append to the popup's body
11103 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
11104 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
11105 * @cfg {boolean} [head] Show label and close button at the top
11106 * @cfg {boolean} [padded] Add padding to the body
11108 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
11109 // Configuration initialization
11110 config
= config
|| {};
11112 // Parent constructor
11113 OO
.ui
.PopupWidget
.super.call( this, config
);
11115 // Mixin constructors
11116 OO
.ui
.LabelElement
.call( this, config
);
11117 OO
.ui
.ClippableElement
.call( this, config
);
11120 this.visible
= false;
11121 this.$popup
= this.$( '<div>' );
11122 this.$head
= this.$( '<div>' );
11123 this.$body
= this.$( '<div>' );
11124 this.$anchor
= this.$( '<div>' );
11125 // If undefined, will be computed lazily in updateDimensions()
11126 this.$container
= config
.$container
;
11127 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
11128 this.autoClose
= !!config
.autoClose
;
11129 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
11130 this.transitionTimeout
= null;
11131 this.anchor
= null;
11132 this.width
= config
.width
!== undefined ? config
.width
: 320;
11133 this.height
= config
.height
!== undefined ? config
.height
: null;
11134 this.align
= config
.align
|| 'center';
11135 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
11136 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
11139 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
11142 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
11143 this.$body
.addClass( 'oo-ui-popupWidget-body' );
11144 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
11146 .addClass( 'oo-ui-popupWidget-head' )
11147 .append( this.$label
, this.closeButton
.$element
);
11148 if ( !config
.head
) {
11152 .addClass( 'oo-ui-popupWidget-popup' )
11153 .append( this.$head
, this.$body
);
11156 .addClass( 'oo-ui-popupWidget' )
11157 .append( this.$popup
, this.$anchor
);
11158 // Move content, which was added to #$element by OO.ui.Widget, to the body
11159 if ( config
.$content
instanceof jQuery
) {
11160 this.$body
.append( config
.$content
);
11162 if ( config
.padded
) {
11163 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
11165 this.setClippableElement( this.$body
);
11170 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
11171 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
11172 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
11177 * Handles mouse down events.
11179 * @param {jQuery.Event} e Mouse down event
11181 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
11183 this.isVisible() &&
11184 !$.contains( this.$element
[0], e
.target
) &&
11185 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
11187 this.toggle( false );
11192 * Bind mouse down listener.
11194 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
11195 // Capture clicks outside popup
11196 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
11200 * Handles close button click events.
11202 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
11203 if ( this.isVisible() ) {
11204 this.toggle( false );
11209 * Unbind mouse down listener.
11211 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
11212 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
11216 * Set whether to show a anchor.
11218 * @param {boolean} [show] Show anchor, omit to toggle
11220 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
11221 show
= show
=== undefined ? !this.anchored
: !!show
;
11223 if ( this.anchored
!== show
) {
11225 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
11227 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
11229 this.anchored
= show
;
11234 * Check if showing a anchor.
11236 * @return {boolean} anchor is visible
11238 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
11239 return this.anchor
;
11245 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
11246 show
= show
=== undefined ? !this.isVisible() : !!show
;
11248 var change
= show
!== this.isVisible();
11251 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
11255 if ( this.autoClose
) {
11256 this.bindMouseDownListener();
11258 this.updateDimensions();
11259 this.toggleClipping( true );
11261 this.toggleClipping( false );
11262 if ( this.autoClose
) {
11263 this.unbindMouseDownListener();
11272 * Set the size of the popup.
11274 * Changing the size may also change the popup's position depending on the alignment.
11276 * @param {number} width Width
11277 * @param {number} height Height
11278 * @param {boolean} [transition=false] Use a smooth transition
11281 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
11282 this.width
= width
;
11283 this.height
= height
!== undefined ? height
: null;
11284 if ( this.isVisible() ) {
11285 this.updateDimensions( transition
);
11290 * Update the size and position.
11292 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
11293 * be called automatically.
11295 * @param {boolean} [transition=false] Use a smooth transition
11298 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
11299 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
11300 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
11303 if ( !this.$container
) {
11304 // Lazy-initialize $container if not specified in constructor
11305 this.$container
= this.$( this.getClosestScrollableElementContainer() );
11308 // Set height and width before measuring things, since it might cause our measurements
11309 // to change (e.g. due to scrollbars appearing or disappearing)
11312 height
: this.height
!== null ? this.height
: 'auto'
11315 // Compute initial popupOffset based on alignment
11316 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
11318 // Figure out if this will cause the popup to go beyond the edge of the container
11319 originOffset
= Math
.round( this.$element
.offset().left
);
11320 containerLeft
= Math
.round( this.$container
.offset().left
);
11321 containerWidth
= this.$container
.innerWidth();
11322 containerRight
= containerLeft
+ containerWidth
;
11323 popupLeft
= popupOffset
- this.containerPadding
;
11324 popupRight
= popupOffset
+ this.containerPadding
+ this.width
+ this.containerPadding
;
11325 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
11326 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
11328 // Adjust offset to make the popup not go beyond the edge, if needed
11329 if ( overlapRight
< 0 ) {
11330 popupOffset
+= overlapRight
;
11331 } else if ( overlapLeft
< 0 ) {
11332 popupOffset
-= overlapLeft
;
11335 // Adjust offset to avoid anchor being rendered too close to the edge
11336 // $anchor.width() doesn't work with the pure CSS anchor (returns 0)
11337 // TODO: Find a measurement that works for CSS anchors and image anchors
11338 anchorWidth
= this.$anchor
[0].scrollWidth
* 2;
11339 if ( popupOffset
+ this.width
< anchorWidth
) {
11340 popupOffset
= anchorWidth
- this.width
;
11341 } else if ( -popupOffset
< anchorWidth
) {
11342 popupOffset
= -anchorWidth
;
11345 // Prevent transition from being interrupted
11346 clearTimeout( this.transitionTimeout
);
11347 if ( transition
) {
11348 // Enable transition
11349 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
11352 // Position body relative to anchor
11353 this.$popup
.css( 'margin-left', popupOffset
);
11355 if ( transition
) {
11356 // Prevent transitioning after transition is complete
11357 this.transitionTimeout
= setTimeout( function () {
11358 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
11361 // Prevent transitioning immediately
11362 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
11365 // Reevaluate clipping state since we've relocated and resized the popup
11372 * Progress bar widget.
11375 * @extends OO.ui.Widget
11378 * @param {Object} [config] Configuration options
11379 * @cfg {number|boolean} [progress=false] Initial progress percent or false for indeterminate
11381 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
11382 // Configuration initialization
11383 config
= config
|| {};
11385 // Parent constructor
11386 OO
.ui
.ProgressBarWidget
.super.call( this, config
);
11389 this.$bar
= this.$( '<div>' );
11390 this.progress
= null;
11393 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
11394 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar');
11397 role
: 'progressbar',
11398 'aria-valuemin': 0,
11399 'aria-valuemax': 100
11401 .addClass( 'oo-ui-progressBarWidget' )
11402 .append( this.$bar
);
11407 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
11409 /* Static Properties */
11411 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
11416 * Get progress percent
11418 * @return {number} Progress percent
11420 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
11421 return this.progress
;
11425 * Set progress percent
11427 * @param {number|boolean} progress Progress percent or false for indeterminate
11429 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
11430 this.progress
= progress
;
11432 if ( progress
!== false ) {
11433 this.$bar
.css( 'width', this.progress
+ '%' );
11434 this.$element
.attr( 'aria-valuenow', this.progress
);
11436 this.$bar
.css( 'width', '' );
11437 this.$element
.removeAttr( 'aria-valuenow' );
11439 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
11445 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
11446 * Results are cleared and populated each time the query is changed.
11449 * @extends OO.ui.Widget
11452 * @param {Object} [config] Configuration options
11453 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
11454 * @cfg {string} [value] Initial query value
11456 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
11457 // Configuration initialization
11458 config
= config
|| {};
11460 // Parent constructor
11461 OO
.ui
.SearchWidget
.super.call( this, config
);
11464 this.query
= new OO
.ui
.TextInputWidget( {
11467 placeholder
: config
.placeholder
,
11468 value
: config
.value
11470 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
11471 this.$query
= this.$( '<div>' );
11472 this.$results
= this.$( '<div>' );
11475 this.query
.connect( this, {
11476 change
: 'onQueryChange',
11477 enter
: 'onQueryEnter'
11479 this.results
.connect( this, {
11480 highlight
: 'onResultsHighlight',
11481 select
: 'onResultsSelect'
11483 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
11487 .addClass( 'oo-ui-searchWidget-query' )
11488 .append( this.query
.$element
);
11490 .addClass( 'oo-ui-searchWidget-results' )
11491 .append( this.results
.$element
);
11493 .addClass( 'oo-ui-searchWidget' )
11494 .append( this.$results
, this.$query
);
11499 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
11505 * @param {Object|null} item Item data or null if no item is highlighted
11510 * @param {Object|null} item Item data or null if no item is selected
11516 * Handle query key down events.
11518 * @param {jQuery.Event} e Key down event
11520 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
11521 var highlightedItem
, nextItem
,
11522 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
11525 highlightedItem
= this.results
.getHighlightedItem();
11526 if ( !highlightedItem
) {
11527 highlightedItem
= this.results
.getSelectedItem();
11529 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
11530 this.results
.highlightItem( nextItem
);
11531 nextItem
.scrollElementIntoView();
11536 * Handle select widget select events.
11538 * Clears existing results. Subclasses should repopulate items according to new query.
11540 * @param {string} value New value
11542 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
11544 this.results
.clearItems();
11548 * Handle select widget enter key events.
11550 * Selects highlighted item.
11552 * @param {string} value New value
11554 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
11556 this.results
.selectItem( this.results
.getHighlightedItem() );
11560 * Handle select widget highlight events.
11562 * @param {OO.ui.OptionWidget} item Highlighted item
11565 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
11566 this.emit( 'highlight', item
? item
.getData() : null );
11570 * Handle select widget select events.
11572 * @param {OO.ui.OptionWidget} item Selected item
11575 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
11576 this.emit( 'select', item
? item
.getData() : null );
11580 * Get the query input.
11582 * @return {OO.ui.TextInputWidget} Query input
11584 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
11589 * Get the results list.
11591 * @return {OO.ui.SelectWidget} Select list
11593 OO
.ui
.SearchWidget
.prototype.getResults = function () {
11594 return this.results
;
11598 * Generic selection of options.
11600 * Items can contain any rendering. Any widget that provides options, from which the user must
11601 * choose one, should be built on this class.
11603 * Use together with OO.ui.OptionWidget.
11606 * @extends OO.ui.Widget
11607 * @mixins OO.ui.GroupElement
11610 * @param {Object} [config] Configuration options
11611 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
11613 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
11614 // Configuration initialization
11615 config
= config
|| {};
11617 // Parent constructor
11618 OO
.ui
.SelectWidget
.super.call( this, config
);
11620 // Mixin constructors
11621 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
11624 this.pressed
= false;
11625 this.selecting
= null;
11626 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
11627 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
11630 this.$element
.on( {
11631 mousedown
: this.onMouseDown
.bind( this ),
11632 mouseover
: this.onMouseOver
.bind( this ),
11633 mouseleave
: this.onMouseLeave
.bind( this )
11637 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
11638 if ( $.isArray( config
.items
) ) {
11639 this.addItems( config
.items
);
11645 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
11647 // Need to mixin base class as well
11648 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
11649 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
11655 * @param {OO.ui.OptionWidget|null} item Highlighted item
11660 * @param {OO.ui.OptionWidget|null} item Pressed item
11665 * @param {OO.ui.OptionWidget|null} item Selected item
11670 * @param {OO.ui.OptionWidget|null} item Chosen item
11675 * @param {OO.ui.OptionWidget[]} items Added items
11676 * @param {number} index Index items were added at
11681 * @param {OO.ui.OptionWidget[]} items Removed items
11687 * Handle mouse down events.
11690 * @param {jQuery.Event} e Mouse down event
11692 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
11695 if ( !this.isDisabled() && e
.which
=== 1 ) {
11696 this.togglePressed( true );
11697 item
= this.getTargetItem( e
);
11698 if ( item
&& item
.isSelectable() ) {
11699 this.pressItem( item
);
11700 this.selecting
= item
;
11701 this.getElementDocument().addEventListener(
11703 this.onMouseUpHandler
,
11706 this.getElementDocument().addEventListener(
11708 this.onMouseMoveHandler
,
11717 * Handle mouse up events.
11720 * @param {jQuery.Event} e Mouse up event
11722 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
11725 this.togglePressed( false );
11726 if ( !this.selecting
) {
11727 item
= this.getTargetItem( e
);
11728 if ( item
&& item
.isSelectable() ) {
11729 this.selecting
= item
;
11732 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
11733 this.pressItem( null );
11734 this.chooseItem( this.selecting
);
11735 this.selecting
= null;
11738 this.getElementDocument().removeEventListener(
11740 this.onMouseUpHandler
,
11743 this.getElementDocument().removeEventListener(
11745 this.onMouseMoveHandler
,
11753 * Handle mouse move events.
11756 * @param {jQuery.Event} e Mouse move event
11758 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
11761 if ( !this.isDisabled() && this.pressed
) {
11762 item
= this.getTargetItem( e
);
11763 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
11764 this.pressItem( item
);
11765 this.selecting
= item
;
11772 * Handle mouse over events.
11775 * @param {jQuery.Event} e Mouse over event
11777 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
11780 if ( !this.isDisabled() ) {
11781 item
= this.getTargetItem( e
);
11782 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
11788 * Handle mouse leave events.
11791 * @param {jQuery.Event} e Mouse over event
11793 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
11794 if ( !this.isDisabled() ) {
11795 this.highlightItem( null );
11801 * Get the closest item to a jQuery.Event.
11804 * @param {jQuery.Event} e
11805 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
11807 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
11808 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
11809 if ( $item
.length
) {
11810 return $item
.data( 'oo-ui-optionWidget' );
11816 * Get selected item.
11818 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
11820 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
11823 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11824 if ( this.items
[i
].isSelected() ) {
11825 return this.items
[i
];
11832 * Get highlighted item.
11834 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
11836 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
11839 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11840 if ( this.items
[i
].isHighlighted() ) {
11841 return this.items
[i
];
11848 * Toggle pressed state.
11850 * @param {boolean} pressed An option is being pressed
11852 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
11853 if ( pressed
=== undefined ) {
11854 pressed
= !this.pressed
;
11856 if ( pressed
!== this.pressed
) {
11858 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
11859 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
11860 this.pressed
= pressed
;
11865 * Highlight an item.
11867 * Highlighting is mutually exclusive.
11869 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
11873 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
11874 var i
, len
, highlighted
,
11877 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11878 highlighted
= this.items
[i
] === item
;
11879 if ( this.items
[i
].isHighlighted() !== highlighted
) {
11880 this.items
[i
].setHighlighted( highlighted
);
11885 this.emit( 'highlight', item
);
11894 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
11898 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
11899 var i
, len
, selected
,
11902 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11903 selected
= this.items
[i
] === item
;
11904 if ( this.items
[i
].isSelected() !== selected
) {
11905 this.items
[i
].setSelected( selected
);
11910 this.emit( 'select', item
);
11919 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
11923 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
11924 var i
, len
, pressed
,
11927 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11928 pressed
= this.items
[i
] === item
;
11929 if ( this.items
[i
].isPressed() !== pressed
) {
11930 this.items
[i
].setPressed( pressed
);
11935 this.emit( 'press', item
);
11944 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
11945 * an item is selected using the keyboard or mouse.
11947 * @param {OO.ui.OptionWidget} item Item to choose
11951 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
11952 this.selectItem( item
);
11953 this.emit( 'choose', item
);
11959 * Get an item relative to another one.
11961 * @param {OO.ui.OptionWidget|null} item Item to start at, null to get relative to list start
11962 * @param {number} direction Direction to move in, -1 to move backward, 1 to move forward
11963 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
11965 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
11966 var currentIndex
, nextIndex
, i
,
11967 increase
= direction
> 0 ? 1 : -1,
11968 len
= this.items
.length
;
11970 if ( item
instanceof OO
.ui
.OptionWidget
) {
11971 currentIndex
= $.inArray( item
, this.items
);
11972 nextIndex
= ( currentIndex
+ increase
+ len
) % len
;
11974 // If no item is selected and moving forward, start at the beginning.
11975 // If moving backward, start at the end.
11976 nextIndex
= direction
> 0 ? 0 : len
- 1;
11979 for ( i
= 0; i
< len
; i
++ ) {
11980 item
= this.items
[nextIndex
];
11981 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11984 nextIndex
= ( nextIndex
+ increase
+ len
) % len
;
11990 * Get the next selectable item.
11992 * @return {OO.ui.OptionWidget|null} Item, `null` if there aren't any selectable items
11994 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
11997 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11998 item
= this.items
[i
];
11999 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
12010 * @param {OO.ui.OptionWidget[]} items Items to add
12011 * @param {number} [index] Index to insert items after
12015 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
12017 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
12019 // Always provide an index, even if it was omitted
12020 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
12028 * Items will be detached, not removed, so they can be used later.
12030 * @param {OO.ui.OptionWidget[]} items Items to remove
12034 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
12037 // Deselect items being removed
12038 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
12040 if ( item
.isSelected() ) {
12041 this.selectItem( null );
12046 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
12048 this.emit( 'remove', items
);
12056 * Items will be detached, not removed, so they can be used later.
12061 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
12062 var items
= this.items
.slice();
12065 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
12068 this.selectItem( null );
12070 this.emit( 'remove', items
);
12076 * Select widget containing button options.
12078 * Use together with OO.ui.ButtonOptionWidget.
12081 * @extends OO.ui.SelectWidget
12084 * @param {Object} [config] Configuration options
12086 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
12087 // Parent constructor
12088 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
12091 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
12096 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
12099 * Select widget containing radio button options.
12101 * Use together with OO.ui.RadioOptionWidget.
12104 * @extends OO.ui.SelectWidget
12107 * @param {Object} [config] Configuration options
12109 OO
.ui
.RadioSelectWidget
= function OoUiRadioSelectWidget( config
) {
12110 // Parent constructor
12111 OO
.ui
.RadioSelectWidget
.super.call( this, config
);
12114 this.$element
.addClass( 'oo-ui-radioSelectWidget' );
12119 OO
.inheritClass( OO
.ui
.RadioSelectWidget
, OO
.ui
.SelectWidget
);
12122 * Overlaid menu of options.
12124 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
12127 * Use together with OO.ui.MenuOptionWidget.
12130 * @extends OO.ui.SelectWidget
12131 * @mixins OO.ui.ClippableElement
12134 * @param {Object} [config] Configuration options
12135 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
12136 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
12137 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
12139 OO
.ui
.MenuSelectWidget
= function OoUiMenuSelectWidget( config
) {
12140 // Configuration initialization
12141 config
= config
|| {};
12143 // Parent constructor
12144 OO
.ui
.MenuSelectWidget
.super.call( this, config
);
12146 // Mixin constructors
12147 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
12150 this.flashing
= false;
12151 this.visible
= false;
12152 this.newItems
= null;
12153 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
12154 this.$input
= config
.input
? config
.input
.$input
: null;
12155 this.$widget
= config
.widget
? config
.widget
.$element
: null;
12156 this.$previousFocus
= null;
12157 this.isolated
= !config
.input
;
12158 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
12159 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
12164 .attr( 'role', 'menu' )
12165 .addClass( 'oo-ui-menuSelectWidget' );
12170 OO
.inheritClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.SelectWidget
);
12171 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.ClippableElement
);
12176 * Handles document mouse down events.
12178 * @param {jQuery.Event} e Key down event
12180 OO
.ui
.MenuSelectWidget
.prototype.onDocumentMouseDown = function ( e
) {
12182 !OO
.ui
.contains( this.$element
[0], e
.target
, true ) &&
12183 ( !this.$widget
|| !OO
.ui
.contains( this.$widget
[0], e
.target
, true ) )
12185 this.toggle( false );
12190 * Handles key down events.
12192 * @param {jQuery.Event} e Key down event
12194 OO
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
12197 highlightItem
= this.getHighlightedItem();
12199 if ( !this.isDisabled() && this.isVisible() ) {
12200 if ( !highlightItem
) {
12201 highlightItem
= this.getSelectedItem();
12203 switch ( e
.keyCode
) {
12204 case OO
.ui
.Keys
.ENTER
:
12205 this.chooseItem( highlightItem
);
12208 case OO
.ui
.Keys
.UP
:
12209 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
12212 case OO
.ui
.Keys
.DOWN
:
12213 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
12216 case OO
.ui
.Keys
.ESCAPE
:
12217 if ( highlightItem
) {
12218 highlightItem
.setHighlighted( false );
12220 this.toggle( false );
12226 this.highlightItem( nextItem
);
12227 nextItem
.scrollElementIntoView();
12231 e
.preventDefault();
12232 e
.stopPropagation();
12239 * Bind key down listener.
12241 OO
.ui
.MenuSelectWidget
.prototype.bindKeyDownListener = function () {
12242 if ( this.$input
) {
12243 this.$input
.on( 'keydown', this.onKeyDownHandler
);
12245 // Capture menu navigation keys
12246 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
12251 * Unbind key down listener.
12253 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyDownListener = function () {
12254 if ( this.$input
) {
12255 this.$input
.off( 'keydown' );
12257 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
12264 * This will close the menu when done, unlike selectItem which only changes selection.
12266 * @param {OO.ui.OptionWidget} item Item to choose
12269 OO
.ui
.MenuSelectWidget
.prototype.chooseItem = function ( item
) {
12273 OO
.ui
.MenuSelectWidget
.super.prototype.chooseItem
.call( this, item
);
12275 if ( item
&& !this.flashing
) {
12276 this.flashing
= true;
12277 item
.flash().done( function () {
12278 widget
.toggle( false );
12279 widget
.flashing
= false;
12282 this.toggle( false );
12291 OO
.ui
.MenuSelectWidget
.prototype.addItems = function ( items
, index
) {
12295 OO
.ui
.MenuSelectWidget
.super.prototype.addItems
.call( this, items
, index
);
12298 if ( !this.newItems
) {
12299 this.newItems
= [];
12302 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
12304 if ( this.isVisible() ) {
12305 // Defer fitting label until item has been attached
12308 this.newItems
.push( item
);
12312 // Reevaluate clipping
12321 OO
.ui
.MenuSelectWidget
.prototype.removeItems = function ( items
) {
12323 OO
.ui
.MenuSelectWidget
.super.prototype.removeItems
.call( this, items
);
12325 // Reevaluate clipping
12334 OO
.ui
.MenuSelectWidget
.prototype.clearItems = function () {
12336 OO
.ui
.MenuSelectWidget
.super.prototype.clearItems
.call( this );
12338 // Reevaluate clipping
12347 OO
.ui
.MenuSelectWidget
.prototype.toggle = function ( visible
) {
12348 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
12351 change
= visible
!== this.isVisible(),
12352 elementDoc
= this.getElementDocument(),
12353 widgetDoc
= this.$widget
? this.$widget
[0].ownerDocument
: null;
12356 OO
.ui
.MenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
12360 this.bindKeyDownListener();
12362 // Change focus to enable keyboard navigation
12363 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
12364 this.$previousFocus
= this.$( ':focus' );
12365 this.$input
[0].focus();
12367 if ( this.newItems
&& this.newItems
.length
) {
12368 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
12369 this.newItems
[i
].fitLabel();
12371 this.newItems
= null;
12373 this.toggleClipping( true );
12376 if ( this.autoHide
) {
12377 elementDoc
.addEventListener(
12378 'mousedown', this.onDocumentMouseDownHandler
, true
12380 // Support $widget being in a different document
12381 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
12382 widgetDoc
.addEventListener(
12383 'mousedown', this.onDocumentMouseDownHandler
, true
12388 this.unbindKeyDownListener();
12389 if ( this.isolated
&& this.$previousFocus
) {
12390 this.$previousFocus
[0].focus();
12391 this.$previousFocus
= null;
12393 elementDoc
.removeEventListener(
12394 'mousedown', this.onDocumentMouseDownHandler
, true
12396 // Support $widget being in a different document
12397 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
12398 widgetDoc
.removeEventListener(
12399 'mousedown', this.onDocumentMouseDownHandler
, true
12402 this.toggleClipping( false );
12410 * Menu for a text input widget.
12412 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
12413 * is in a different frame, the menu's position is automatically calculated and maintained when the
12414 * menu is toggled or the window is resized.
12417 * @extends OO.ui.MenuSelectWidget
12420 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
12421 * @param {Object} [config] Configuration options
12422 * @cfg {jQuery} [$container=input.$element] Element to render menu under
12424 OO
.ui
.TextInputMenuSelectWidget
= function OoUiTextInputMenuSelectWidget( input
, config
) {
12425 // Configuration initialization
12426 config
= config
|| {};
12428 // Parent constructor
12429 OO
.ui
.TextInputMenuSelectWidget
.super.call( this, config
);
12432 this.input
= input
;
12433 this.$container
= config
.$container
|| this.input
.$element
;
12434 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
12437 this.$element
.addClass( 'oo-ui-textInputMenuSelectWidget' );
12442 OO
.inheritClass( OO
.ui
.TextInputMenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
12447 * Handle window resize event.
12449 * @param {jQuery.Event} e Window resize event
12451 OO
.ui
.TextInputMenuSelectWidget
.prototype.onWindowResize = function () {
12458 OO
.ui
.TextInputMenuSelectWidget
.prototype.toggle = function ( visible
) {
12459 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
12461 var change
= visible
!== this.isVisible();
12463 if ( change
&& visible
) {
12464 // Make sure the width is set before the parent method runs.
12465 // After this we have to call this.position(); again to actually
12466 // position ourselves correctly.
12471 OO
.ui
.TextInputMenuSelectWidget
.super.prototype.toggle
.call( this, visible
);
12474 if ( this.isVisible() ) {
12476 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
12478 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
12486 * Position the menu.
12490 OO
.ui
.TextInputMenuSelectWidget
.prototype.position = function () {
12491 var $container
= this.$container
,
12492 pos
= OO
.ui
.Element
.static.getRelativePosition( $container
, this.$element
.offsetParent() );
12494 // Position under input
12495 pos
.top
+= $container
.height();
12496 this.$element
.css( pos
);
12499 this.setIdealSize( $container
.width() );
12500 // We updated the position, so re-evaluate the clipping state
12507 * Structured list of items.
12509 * Use with OO.ui.OutlineOptionWidget.
12512 * @extends OO.ui.SelectWidget
12515 * @param {Object} [config] Configuration options
12517 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
12518 // Configuration initialization
12519 config
= config
|| {};
12521 // Parent constructor
12522 OO
.ui
.OutlineSelectWidget
.super.call( this, config
);
12525 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
12530 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
12533 * Switch that slides on and off.
12536 * @extends OO.ui.Widget
12537 * @mixins OO.ui.ToggleWidget
12540 * @param {Object} [config] Configuration options
12541 * @cfg {boolean} [value=false] Initial value
12543 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
12544 // Parent constructor
12545 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
12547 // Mixin constructors
12548 OO
.ui
.ToggleWidget
.call( this, config
);
12551 this.dragging
= false;
12552 this.dragStart
= null;
12553 this.sliding
= false;
12554 this.$glow
= this.$( '<span>' );
12555 this.$grip
= this.$( '<span>' );
12558 this.$element
.on( 'click', this.onClick
.bind( this ) );
12561 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
12562 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
12564 .addClass( 'oo-ui-toggleSwitchWidget' )
12565 .append( this.$glow
, this.$grip
);
12570 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
12571 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
12576 * Handle mouse down events.
12578 * @param {jQuery.Event} e Mouse down event
12580 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
12581 if ( !this.isDisabled() && e
.which
=== 1 ) {
12582 this.setValue( !this.value
);