2 * OOjs UI v0.1.0-pre (8ad150e4e7)
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-09-05T21:30:32Z
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
98 * Message store for the default implementation of OO.ui.msg
100 * Environments that provide a localization system should not use this, but should override
101 * OO.ui.msg altogether.
106 // Tool tip for a button that moves items in a list down one place
107 'ooui-outline-control-move-down': 'Move item down',
108 // Tool tip for a button that moves items in a list up one place
109 'ooui-outline-control-move-up': 'Move item up',
110 // Tool tip for a button that removes items from a list
111 'ooui-outline-control-remove': 'Remove item',
112 // Label for the toolbar group that contains a list of all other available tools
113 'ooui-toolbar-more': 'More',
114 // Default label for the accept button of a confirmation dialog
115 'ooui-dialog-message-accept': 'OK',
116 // Default label for the reject button of a confirmation dialog
117 'ooui-dialog-message-reject': 'Cancel',
118 // Title for process dialog error description
119 'ooui-dialog-process-error': 'Something went wrong',
120 // Label for process dialog dismiss error button, visible when describing errors
121 'ooui-dialog-process-dismiss': 'Dismiss',
122 // Label for process dialog retry action button, visible when describing recoverable errors
123 'ooui-dialog-process-retry': 'Try again'
127 * Get a localized message.
129 * In environments that provide a localization system, this function should be overridden to
130 * return the message translated in the user's language. The default implementation always returns
133 * After the message key, message parameters may optionally be passed. In the default implementation,
134 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
135 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
136 * they support unnamed, ordered message parameters.
139 * @param {string} key Message key
140 * @param {Mixed...} [params] Message parameters
141 * @return {string} Translated message with parameters substituted
143 OO
.ui
.msg = function ( key
) {
144 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
145 if ( typeof message
=== 'string' ) {
146 // Perform $1 substitution
147 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
148 var i
= parseInt( n
, 10 );
149 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
152 // Return placeholder if message not found
153 message
= '[' + key
+ ']';
159 * Package a message and arguments for deferred resolution.
161 * Use this when you are statically specifying a message and the message may not yet be present.
163 * @param {string} key Message key
164 * @param {Mixed...} [params] Message parameters
165 * @return {Function} Function that returns the resolved message when executed
167 OO
.ui
.deferMsg = function () {
168 var args
= arguments
;
170 return OO
.ui
.msg
.apply( OO
.ui
, args
);
177 * If the message is a function it will be executed, otherwise it will pass through directly.
179 * @param {Function|string} msg Deferred message, or message text
180 * @return {string} Resolved message
182 OO
.ui
.resolveMsg = function ( msg
) {
183 if ( $.isFunction( msg
) ) {
196 * @mixins OO.EventEmitter
199 * @param {Object} [config] Configuration options
201 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
202 // Configuration intialization
203 config
= config
|| {};
205 // Mixin constructors
206 OO
.EventEmitter
.call( this );
211 actions
: 'getAction',
215 this.categorized
= {};
218 this.organized
= false;
219 this.changing
= false;
220 this.changed
= false;
225 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
227 /* Static Properties */
230 * Symbolic name of dialog.
237 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
243 * @param {OO.ui.ActionWidget} action Action that was clicked
248 * @param {OO.ui.ActionWidget} action Action that was resized
253 * @param {OO.ui.ActionWidget[]} added Actions added
258 * @param {OO.ui.ActionWidget[]} added Actions removed
268 * Handle action change events.
272 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
273 this.organized
= false;
274 if ( this.changing
) {
277 this.emit( 'change' );
282 * Check if a action is one of the special actions.
284 * @param {OO.ui.ActionWidget} action Action to check
285 * @return {boolean} Action is special
287 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
290 for ( flag
in this.special
) {
291 if ( action
=== this.special
[flag
] ) {
302 * @param {Object} [filters] Filters to use, omit to get all actions
303 * @param {string|string[]} [filters.actions] Actions that actions must have
304 * @param {string|string[]} [filters.flags] Flags that actions must have
305 * @param {string|string[]} [filters.modes] Modes that actions must have
306 * @param {boolean} [filters.visible] Actions must be visible
307 * @param {boolean} [filters.disabled] Actions must be disabled
308 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
310 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
311 var i
, len
, list
, category
, actions
, index
, match
, matches
;
316 // Collect category candidates
318 for ( category
in this.categorized
) {
319 list
= filters
[category
];
321 if ( !Array
.isArray( list
) ) {
324 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
325 actions
= this.categorized
[category
][list
[i
]];
326 if ( Array
.isArray( actions
) ) {
327 matches
.push
.apply( matches
, actions
);
332 // Remove by boolean filters
333 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
336 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
337 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
339 matches
.splice( i
, 1 );
345 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
347 index
= matches
.lastIndexOf( match
);
348 while ( index
!== i
) {
349 matches
.splice( index
, 1 );
351 index
= matches
.lastIndexOf( match
);
356 return this.list
.slice();
360 * Get special actions.
362 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
363 * Special flags can be configured by changing #static-specialFlags in a subclass.
365 * @return {OO.ui.ActionWidget|null} Safe action
367 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
369 return $.extend( {}, this.special
);
375 * Other actions include all non-special visible actions.
377 * @return {OO.ui.ActionWidget[]} Other actions
379 OO
.ui
.ActionSet
.prototype.getOthers = function () {
381 return this.others
.slice();
385 * Toggle actions based on their modes.
387 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
388 * visibility; matching actions will be shown, non-matching actions will be hidden.
390 * @param {string} mode Mode actions must have
395 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
398 this.changing
= true;
399 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
400 action
= this.list
[i
];
401 action
.toggle( action
.hasMode( mode
) );
404 this.organized
= false;
405 this.changing
= false;
406 this.emit( 'change' );
412 * Change which actions are able to be performed.
414 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
416 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
417 * indicate actions are able to be performed
420 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
421 var i
, len
, action
, item
;
423 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
425 action
= item
.getAction();
426 if ( actions
[action
] !== undefined ) {
427 item
.setDisabled( !actions
[action
] );
435 * Executes a function once per action.
437 * When making changes to multiple actions, use this method instead of iterating over the actions
438 * manually to defer emitting a change event until after all actions have been changed.
440 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
441 * @param {Function} callback Callback to run for each action; callback is invoked with three
442 * arguments: the action, the action's index, the list of actions being iterated over
445 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
446 this.changed
= false;
447 this.changing
= true;
448 this.get( filter
).forEach( callback
);
449 this.changing
= false;
450 if ( this.changed
) {
451 this.emit( 'change' );
460 * @param {OO.ui.ActionWidget[]} actions Actions to add
465 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
468 this.changing
= true;
469 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
471 action
.connect( this, {
472 click
: [ 'emit', 'click', action
],
473 resize
: [ 'emit', 'resize', action
],
474 toggle
: [ 'onActionChange' ]
476 this.list
.push( action
);
478 this.organized
= false;
479 this.emit( 'add', actions
);
480 this.changing
= false;
481 this.emit( 'change' );
489 * @param {OO.ui.ActionWidget[]} actions Actions to remove
494 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
495 var i
, len
, index
, action
;
497 this.changing
= true;
498 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
500 index
= this.list
.indexOf( action
);
501 if ( index
!== -1 ) {
502 action
.disconnect( this );
503 this.list
.splice( index
, 1 );
506 this.organized
= false;
507 this.emit( 'remove', actions
);
508 this.changing
= false;
509 this.emit( 'change' );
515 * Remove all actions.
521 OO
.ui
.ActionSet
.prototype.clear = function () {
523 removed
= this.list
.slice();
525 this.changing
= true;
526 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
527 action
= this.list
[i
];
528 action
.disconnect( this );
533 this.organized
= false;
534 this.emit( 'remove', removed
);
535 this.changing
= false;
536 this.emit( 'change' );
544 * This is called whenver organized information is requested. It will only reorganize the actions
545 * if something has changed since the last time it ran.
550 OO
.ui
.ActionSet
.prototype.organize = function () {
551 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
552 specialFlags
= this.constructor.static.specialFlags
;
554 if ( !this.organized
) {
555 this.categorized
= {};
558 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
559 action
= this.list
[i
];
560 if ( action
.isVisible() ) {
561 // Populate catgeories
562 for ( category
in this.categories
) {
563 if ( !this.categorized
[category
] ) {
564 this.categorized
[category
] = {};
566 list
= action
[this.categories
[category
]]();
567 if ( !Array
.isArray( list
) ) {
570 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
572 if ( !this.categorized
[category
][item
] ) {
573 this.categorized
[category
][item
] = [];
575 this.categorized
[category
][item
].push( action
);
578 // Populate special/others
580 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
581 flag
= specialFlags
[j
];
582 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
583 this.special
[flag
] = action
;
589 this.others
.push( action
);
593 this.organized
= true;
600 * DOM element abstraction.
606 * @param {Object} [config] Configuration options
607 * @cfg {Function} [$] jQuery for the frame the widget is in
608 * @cfg {string[]} [classes] CSS class names
609 * @cfg {string} [text] Text to insert
610 * @cfg {jQuery} [$content] Content elements to append (after text)
612 OO
.ui
.Element
= function OoUiElement( config
) {
613 // Configuration initialization
614 config
= config
|| {};
617 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
618 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
619 this.elementGroup
= null;
622 if ( $.isArray( config
.classes
) ) {
623 this.$element
.addClass( config
.classes
.join( ' ' ) );
626 this.$element
.text( config
.text
);
628 if ( config
.$content
) {
629 this.$element
.append( config
.$content
);
635 OO
.initClass( OO
.ui
.Element
);
637 /* Static Properties */
642 * This may be ignored if getTagName is overridden.
648 OO
.ui
.Element
.static.tagName
= 'div';
653 * Get a jQuery function within a specific document.
656 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
657 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
659 * @return {Function} Bound jQuery function
661 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
662 function wrapper( selector
) {
663 return $( selector
, wrapper
.context
);
666 wrapper
.context
= this.getDocument( context
);
669 wrapper
.$iframe
= $iframe
;
676 * Get the document of an element.
679 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
680 * @return {HTMLDocument|null} Document object
682 OO
.ui
.Element
.getDocument = function ( obj
) {
683 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
684 return ( obj
[0] && obj
[0].ownerDocument
) ||
685 // Empty jQuery selections might have a context
692 ( obj
.nodeType
=== 9 && obj
) ||
697 * Get the window of an element or document.
700 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
701 * @return {Window} Window object
703 OO
.ui
.Element
.getWindow = function ( obj
) {
704 var doc
= this.getDocument( obj
);
705 return doc
.parentWindow
|| doc
.defaultView
;
709 * Get the direction of an element or document.
712 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
713 * @return {string} Text direction, either `ltr` or `rtl`
715 OO
.ui
.Element
.getDir = function ( obj
) {
718 if ( obj
instanceof jQuery
) {
721 isDoc
= obj
.nodeType
=== 9;
722 isWin
= obj
.document
!== undefined;
723 if ( isDoc
|| isWin
) {
729 return $( obj
).css( 'direction' );
733 * Get the offset between two frames.
735 * TODO: Make this function not use recursion.
738 * @param {Window} from Window of the child frame
739 * @param {Window} [to=window] Window of the parent frame
740 * @param {Object} [offset] Offset to start with, used internally
741 * @return {Object} Offset object, containing left and top properties
743 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
744 var i
, len
, frames
, frame
, rect
;
750 offset
= { top
: 0, left
: 0 };
752 if ( from.parent
=== from ) {
756 // Get iframe element
757 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
758 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
759 if ( frames
[i
].contentWindow
=== from ) {
765 // Recursively accumulate offset values
767 rect
= frame
.getBoundingClientRect();
768 offset
.left
+= rect
.left
;
769 offset
.top
+= rect
.top
;
771 this.getFrameOffset( from.parent
, offset
);
778 * Get the offset between two elements.
781 * @param {jQuery} $from
782 * @param {jQuery} $to
783 * @return {Object} Translated position coordinates, containing top and left properties
785 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
786 var from = $from.offset(),
788 return { top
: Math
.round( from.top
- to
.top
), left
: Math
.round( from.left
- to
.left
) };
792 * Get element border sizes.
795 * @param {HTMLElement} el Element to measure
796 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
798 OO
.ui
.Element
.getBorders = function ( el
) {
799 var doc
= el
.ownerDocument
,
800 win
= doc
.parentWindow
|| doc
.defaultView
,
801 style
= win
&& win
.getComputedStyle
?
802 win
.getComputedStyle( el
, null ) :
805 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
806 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
807 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
808 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
811 top
: Math
.round( top
),
812 left
: Math
.round( left
),
813 bottom
: Math
.round( bottom
),
814 right
: Math
.round( right
)
819 * Get dimensions of an element or window.
822 * @param {HTMLElement|Window} el Element to measure
823 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
825 OO
.ui
.Element
.getDimensions = function ( el
) {
827 doc
= el
.ownerDocument
|| el
.document
,
828 win
= doc
.parentWindow
|| doc
.defaultView
;
830 if ( win
=== el
|| el
=== doc
.documentElement
) {
833 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
835 top
: $win
.scrollTop(),
836 left
: $win
.scrollLeft()
838 scrollbar
: { right
: 0, bottom
: 0 },
842 bottom
: $win
.innerHeight(),
843 right
: $win
.innerWidth()
849 borders
: this.getBorders( el
),
851 top
: $el
.scrollTop(),
852 left
: $el
.scrollLeft()
855 right
: $el
.innerWidth() - el
.clientWidth
,
856 bottom
: $el
.innerHeight() - el
.clientHeight
858 rect
: el
.getBoundingClientRect()
864 * Get closest scrollable container.
866 * Traverses up until either a scrollable element or the root is reached, in which case the window
870 * @param {HTMLElement} el Element to find scrollable container for
871 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
872 * @return {HTMLElement|Window} Closest scrollable container
874 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
876 props
= [ 'overflow' ],
877 $parent
= $( el
).parent();
879 if ( dimension
=== 'x' || dimension
=== 'y' ) {
880 props
.push( 'overflow-' + dimension
);
883 while ( $parent
.length
) {
884 if ( $parent
[0] === el
.ownerDocument
.body
) {
889 val
= $parent
.css( props
[i
] );
890 if ( val
=== 'auto' || val
=== 'scroll' ) {
894 $parent
= $parent
.parent();
896 return this.getDocument( el
).body
;
900 * Scroll element into view.
903 * @param {HTMLElement} el Element to scroll into view
904 * @param {Object} [config={}] Configuration config
905 * @param {string} [config.duration] jQuery animation duration value
906 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
907 * to scroll in both directions
908 * @param {Function} [config.complete] Function to call when scrolling completes
910 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
911 // Configuration initialization
912 config
= config
|| {};
915 callback
= typeof config
.complete
=== 'function' && config
.complete
,
916 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
918 eld
= this.getDimensions( el
),
919 scd
= this.getDimensions( sc
),
920 $win
= $( this.getWindow( el
) );
922 // Compute the distances between the edges of el and the edges of the scroll viewport
923 if ( $sc
.is( 'body' ) ) {
924 // If the scrollable container is the <body> this is easy
927 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
929 right
: $win
.innerWidth() - eld
.rect
.right
932 // Otherwise, we have to subtract el's coordinates from sc's coordinates
934 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
935 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
936 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
937 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
941 if ( !config
.direction
|| config
.direction
=== 'y' ) {
943 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
944 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
945 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
948 if ( !config
.direction
|| config
.direction
=== 'x' ) {
949 if ( rel
.left
< 0 ) {
950 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
951 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
952 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
955 if ( !$.isEmptyObject( anim
) ) {
956 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
958 $sc
.queue( function ( next
) {
973 * Get the HTML tag name.
975 * Override this method to base the result on instance information.
977 * @return {string} HTML tag name
979 OO
.ui
.Element
.prototype.getTagName = function () {
980 return this.constructor.static.tagName
;
984 * Check if the element is attached to the DOM
985 * @return {boolean} The element is attached to the DOM
987 OO
.ui
.Element
.prototype.isElementAttached = function () {
988 return $.contains( this.getElementDocument(), this.$element
[0] );
992 * Get the DOM document.
994 * @return {HTMLDocument} Document object
996 OO
.ui
.Element
.prototype.getElementDocument = function () {
997 return OO
.ui
.Element
.getDocument( this.$element
);
1001 * Get the DOM window.
1003 * @return {Window} Window object
1005 OO
.ui
.Element
.prototype.getElementWindow = function () {
1006 return OO
.ui
.Element
.getWindow( this.$element
);
1010 * Get closest scrollable container.
1012 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1013 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1017 * Get group element is in.
1019 * @return {OO.ui.GroupElement|null} Group element, null if none
1021 OO
.ui
.Element
.prototype.getElementGroup = function () {
1022 return this.elementGroup
;
1026 * Set group element is in.
1028 * @param {OO.ui.GroupElement|null} group Group element, null if none
1031 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1032 this.elementGroup
= group
;
1037 * Scroll element into view.
1039 * @param {Object} [config={}]
1041 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1042 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1046 * Bind a handler for an event on this.$element
1048 * @deprecated Use jQuery#on instead.
1049 * @param {string} event
1050 * @param {Function} callback
1052 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1053 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1057 * Unbind a handler bound with #offDOMEvent
1059 * @deprecated Use jQuery#off instead.
1060 * @param {string} event
1061 * @param {Function} callback
1063 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1064 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1069 * Bind a handler for an event on a DOM element.
1071 * Used to be for working around a jQuery bug (jqbug.com/14180),
1072 * but obsolete as of jQuery 1.11.0.
1075 * @deprecated Use jQuery#on instead.
1076 * @param {HTMLElement|jQuery} el DOM element
1077 * @param {string} event Event to bind
1078 * @param {Function} callback Callback to call when the event fires
1080 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1081 $( el
).on( event
, callback
);
1085 * Unbind a handler bound with #static-method-onDOMEvent.
1087 * @deprecated Use jQuery#off instead.
1089 * @param {HTMLElement|jQuery} el DOM element
1090 * @param {string} event Event to unbind
1091 * @param {Function} [callback] Callback to unbind
1093 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1094 $( el
).off( event
, callback
);
1099 * Container for elements.
1103 * @extends OO.ui.Element
1104 * @mixins OO.EventEmitter
1107 * @param {Object} [config] Configuration options
1109 OO
.ui
.Layout
= function OoUiLayout( config
) {
1110 // Initialize config
1111 config
= config
|| {};
1113 // Parent constructor
1114 OO
.ui
.Layout
.super.call( this, config
);
1116 // Mixin constructors
1117 OO
.EventEmitter
.call( this );
1120 this.$element
.addClass( 'oo-ui-layout' );
1125 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1126 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1129 * User interface control.
1133 * @extends OO.ui.Element
1134 * @mixins OO.EventEmitter
1137 * @param {Object} [config] Configuration options
1138 * @cfg {boolean} [disabled=false] Disable
1140 OO
.ui
.Widget
= function OoUiWidget( config
) {
1141 // Initialize config
1142 config
= $.extend( { disabled
: false }, config
);
1144 // Parent constructor
1145 OO
.ui
.Widget
.super.call( this, config
);
1147 // Mixin constructors
1148 OO
.EventEmitter
.call( this );
1151 this.visible
= true;
1152 this.disabled
= null;
1153 this.wasDisabled
= null;
1156 this.$element
.addClass( 'oo-ui-widget' );
1157 this.setDisabled( !!config
.disabled
);
1162 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1163 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1169 * @param {boolean} disabled Widget is disabled
1174 * @param {boolean} visible Widget is visible
1180 * Check if the widget is disabled.
1182 * @param {boolean} Button is disabled
1184 OO
.ui
.Widget
.prototype.isDisabled = function () {
1185 return this.disabled
;
1189 * Check if widget is visible.
1191 * @return {boolean} Widget is visible
1193 OO
.ui
.Widget
.prototype.isVisible = function () {
1194 return this.visible
;
1198 * Set the disabled state of the widget.
1200 * This should probably change the widgets' appearance and prevent it from being used.
1202 * @param {boolean} disabled Disable widget
1205 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1208 this.disabled
= !!disabled
;
1209 isDisabled
= this.isDisabled();
1210 if ( isDisabled
!== this.wasDisabled
) {
1211 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1212 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1213 this.emit( 'disable', isDisabled
);
1215 this.wasDisabled
= isDisabled
;
1221 * Toggle visibility of widget.
1223 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1227 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1228 show
= show
=== undefined ? !this.visible
: !!show
;
1230 if ( show
!== this.isVisible() ) {
1231 this.visible
= show
;
1232 this.$element
.toggle( show
);
1233 this.emit( 'toggle', show
);
1240 * Update the disabled state, in case of changes in parent widget.
1244 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1245 this.setDisabled( this.disabled
);
1250 * Container for elements in a child frame.
1252 * Use together with OO.ui.WindowManager.
1256 * @extends OO.ui.Element
1257 * @mixins OO.EventEmitter
1259 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1260 * teardown processes are executed when the window is closed.
1262 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1263 * - Window manager begins opening window
1264 * - {@link #getSetupProcess} method is called and its result executed
1265 * - {@link #getReadyProcess} method is called and its result executed
1266 * - Window is now open
1268 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1269 * - Window manager begins closing window
1270 * - {@link #getHoldProcess} method is called and its result executed
1271 * - {@link #getTeardownProcess} method is called and its result executed
1272 * - Window is now closed
1274 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1275 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1276 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1277 * processing can complete. Always assume window processes are executed asychronously. See
1278 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1279 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1281 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1282 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1285 * @param {Object} [config] Configuration options
1286 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1290 OO
.ui
.Window
= function OoUiWindow( config
) {
1291 // Configuration initialization
1292 config
= config
|| {};
1294 // Parent constructor
1295 OO
.ui
.Window
.super.call( this, config
);
1297 // Mixin constructors
1298 OO
.EventEmitter
.call( this );
1301 this.manager
= null;
1302 this.initialized
= false;
1303 this.visible
= false;
1304 this.opening
= null;
1305 this.closing
= null;
1308 this.loading
= null;
1309 this.size
= config
.size
|| this.constructor.static.size
;
1310 this.$frame
= this.$( '<div>' );
1314 .addClass( 'oo-ui-window' )
1315 .append( this.$frame
);
1316 this.$frame
.addClass( 'oo-ui-window-frame' );
1318 // NOTE: Additional intitialization will occur when #setManager is called
1323 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1324 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1330 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1333 /* Static Properties */
1336 * Symbolic name of size.
1338 * Size is used if no size is configured during construction.
1342 * @property {string}
1344 OO
.ui
.Window
.static.size
= 'medium';
1346 /* Static Methods */
1349 * Transplant the CSS styles from as parent document to a frame's document.
1351 * This loops over the style sheets in the parent document, and copies their nodes to the
1352 * frame's document. It then polls the document to see when all styles have loaded, and once they
1353 * have, resolves the promise.
1355 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1356 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1357 * Firefox, where the styles won't load until the iframe becomes visible.
1359 * For details of how we arrived at the strategy used in this function, see #load.
1363 * @param {HTMLDocument} parentDoc Document to transplant styles from
1364 * @param {HTMLDocument} frameDoc Document to transplant styles to
1365 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1366 * @return {jQuery.Promise} Promise resolved when styles have loaded
1368 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1369 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1370 $pollNodes
= $( [] ),
1371 // Fake font-family value
1372 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1373 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1374 deferred
= $.Deferred();
1376 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1377 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1378 if ( styleNode
.disabled
) {
1382 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1383 // External stylesheet; use @import
1384 styleText
= '@import url(' + styleNode
.href
+ ');';
1386 // Internal stylesheet; just copy the text
1387 // For IE10 we need to fall back to .cssText
1388 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
;
1391 // Create a node with a unique ID that we're going to monitor to see when the CSS
1393 if ( styleNode
.oouiFrameTransplantStylesId
) {
1394 // If we're nesting transplantStyles operations and this node already has
1395 // a CSS rule to wait for loading, reuse it
1396 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1398 // Otherwise, create a new ID
1399 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1402 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1403 // The font-family rule will only take effect once the @import finishes
1404 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1407 // Create a node with id=pollNodeId
1408 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1409 .attr( 'id', pollNodeId
)
1410 .appendTo( frameDoc
.body
)
1413 // Add our modified CSS as a <style> tag
1414 newNode
= frameDoc
.createElement( 'style' );
1415 newNode
.textContent
= styleText
;
1416 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1417 frameDoc
.head
.appendChild( newNode
);
1419 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1421 // Poll every 100ms until all external stylesheets have loaded
1422 $pendingPollNodes
= $pollNodes
;
1423 timeoutID
= setTimeout( function pollExternalStylesheets() {
1425 $pendingPollNodes
.length
> 0 &&
1426 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1428 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1431 if ( $pendingPollNodes
.length
=== 0 ) {
1433 if ( timeoutID
!== null ) {
1435 $pollNodes
.remove();
1439 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1442 // ...but give up after a while
1443 if ( timeout
!== 0 ) {
1444 setTimeout( function () {
1446 clearTimeout( timeoutID
);
1448 $pollNodes
.remove();
1451 }, timeout
|| 5000 );
1454 return deferred
.promise();
1460 * Handle mouse down events.
1462 * @param {jQuery.Event} e Mouse down event
1464 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1465 // Prevent clicking on the click-block from stealing focus
1466 if ( e
.target
=== this.$element
[0] ) {
1472 * Check if window has been initialized.
1474 * @return {boolean} Window has been initialized
1476 OO
.ui
.Window
.prototype.isInitialized = function () {
1477 return this.initialized
;
1481 * Check if window is visible.
1483 * @return {boolean} Window is visible
1485 OO
.ui
.Window
.prototype.isVisible = function () {
1486 return this.visible
;
1490 * Check if window is loading.
1492 * @return {boolean} Window is loading
1494 OO
.ui
.Window
.prototype.isLoading = function () {
1495 return this.loading
&& this.loading
.state() === 'pending';
1499 * Check if window is loaded.
1501 * @return {boolean} Window is loaded
1503 OO
.ui
.Window
.prototype.isLoaded = function () {
1504 return this.loading
&& this.loading
.state() === 'resolved';
1508 * Check if window is opening.
1510 * This is a wrapper around OO.ui.WindowManager#isOpening.
1512 * @return {boolean} Window is opening
1514 OO
.ui
.Window
.prototype.isOpening = function () {
1515 return this.manager
.isOpening( this );
1519 * Check if window is closing.
1521 * This is a wrapper around OO.ui.WindowManager#isClosing.
1523 * @return {boolean} Window is closing
1525 OO
.ui
.Window
.prototype.isClosing = function () {
1526 return this.manager
.isClosing( this );
1530 * Check if window is opened.
1532 * This is a wrapper around OO.ui.WindowManager#isOpened.
1534 * @return {boolean} Window is opened
1536 OO
.ui
.Window
.prototype.isOpened = function () {
1537 return this.manager
.isOpened( this );
1541 * Get the window manager.
1543 * @return {OO.ui.WindowManager} Manager of window
1545 OO
.ui
.Window
.prototype.getManager = function () {
1546 return this.manager
;
1550 * Get the window size.
1552 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1554 OO
.ui
.Window
.prototype.getSize = function () {
1559 * Get the height of the dialog contents.
1561 * @return {number} Content height
1563 OO
.ui
.Window
.prototype.getContentHeight = function () {
1565 // Add buffer for border
1566 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1567 // Use combined heights of children
1568 ( this.$head
.outerHeight( true ) + this.getBodyHeight() + this.$foot
.outerHeight( true ) )
1573 * Get the height of the dialog contents.
1575 * @return {number} Height of content
1577 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1578 return this.$body
[0].scrollHeight
;
1582 * Get the directionality of the frame
1584 * @return {string} Directionality, 'ltr' or 'rtl'
1586 OO
.ui
.Window
.prototype.getDir = function () {
1591 * Get a process for setting up a window for use.
1593 * Each time the window is opened this process will set it up for use in a particular context, based
1594 * on the `data` argument.
1596 * When you override this method, you can add additional setup steps to the process the parent
1597 * method provides using the 'first' and 'next' methods.
1600 * @param {Object} [data] Window opening data
1601 * @return {OO.ui.Process} Setup process
1603 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1604 return new OO
.ui
.Process();
1608 * Get a process for readying a window for use.
1610 * Each time the window is open and setup, this process will ready it up for use in a particular
1611 * context, based on the `data` argument.
1613 * When you override this method, you can add additional setup steps to the process the parent
1614 * method provides using the 'first' and 'next' methods.
1617 * @param {Object} [data] Window opening data
1618 * @return {OO.ui.Process} Setup process
1620 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1621 return new OO
.ui
.Process();
1625 * Get a process for holding a window from use.
1627 * Each time the window is closed, this process will hold it from use in a particular context, based
1628 * on the `data` argument.
1630 * When you override this method, you can add additional setup steps to the process the parent
1631 * method provides using the 'first' and 'next' methods.
1634 * @param {Object} [data] Window closing data
1635 * @return {OO.ui.Process} Hold process
1637 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1638 return new OO
.ui
.Process();
1642 * Get a process for tearing down a window after use.
1644 * Each time the window is closed this process will tear it down and do something with the user's
1645 * interactions within the window, based on the `data` argument.
1647 * When you override this method, you can add additional teardown steps to the process the parent
1648 * method provides using the 'first' and 'next' methods.
1651 * @param {Object} [data] Window closing data
1652 * @return {OO.ui.Process} Teardown process
1654 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1655 return new OO
.ui
.Process();
1659 * Toggle visibility of window.
1661 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1662 * instead of display.
1664 * @param {boolean} [show] Make window visible, omit to toggle visibility
1668 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1669 show
= show
=== undefined ? !this.visible
: !!show
;
1671 if ( show
!== this.isVisible() ) {
1672 this.visible
= show
;
1674 if ( this.isolated
&& !this.isLoaded() ) {
1675 // Hide the window using visibility instead of display until loading is complete
1676 // Can't use display: none; because that prevents the iframe from loading in Firefox
1677 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1679 this.$element
.toggle( show
).css( 'visibility', '' );
1681 this.emit( 'toggle', show
);
1688 * Set the window manager.
1690 * This must be called before initialize. Calling it more than once will cause an error.
1692 * @param {OO.ui.WindowManager} manager Manager for this window
1693 * @throws {Error} If called more than once
1696 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1697 if ( this.manager
) {
1698 throw new Error( 'Cannot set window manager, window already has a manager' );
1702 this.manager
= manager
;
1703 this.isolated
= manager
.shouldIsolate();
1706 if ( this.isolated
) {
1707 this.$iframe
= this.$( '<iframe>' );
1708 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1709 this.$frame
.append( this.$iframe
);
1710 this.$ = function () {
1711 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1713 // WARNING: Do not use this.$ again until #initialize is called
1715 this.$content
= this.$( '<div>' );
1716 this.$document
= $( this.getElementDocument() );
1717 this.$content
.addClass( 'oo-ui-window-content' );
1718 this.$frame
.append( this.$content
);
1720 this.toggle( false );
1722 // Figure out directionality:
1723 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1729 * Set the window size.
1731 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1734 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1736 this.manager
.updateWindowSize( this );
1741 * Set window dimensions.
1743 * Properties are applied to the frame container.
1745 * @param {Object} dim CSS dimension properties
1746 * @param {string|number} [dim.width] Width
1747 * @param {string|number} [dim.minWidth] Minimum width
1748 * @param {string|number} [dim.maxWidth] Maximum width
1749 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1750 * @param {string|number} [dim.minWidth] Minimum height
1751 * @param {string|number} [dim.maxWidth] Maximum height
1754 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1755 // Apply width before height so height is not based on wrapping content using the wrong width
1757 width
: dim
.width
|| '',
1758 minWidth
: dim
.minWidth
|| '',
1759 maxWidth
: dim
.maxWidth
|| ''
1762 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1763 minHeight
: dim
.minHeight
|| '',
1764 maxHeight
: dim
.maxHeight
|| ''
1770 * Initialize window contents.
1772 * The first time the window is opened, #initialize is called when it's safe to begin populating
1773 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1775 * Once this method is called, this.$ can be used to create elements within the frame.
1777 * @throws {Error} If not attached to a manager
1780 OO
.ui
.Window
.prototype.initialize = function () {
1781 if ( !this.manager
) {
1782 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1786 this.$head
= this.$( '<div>' );
1787 this.$body
= this.$( '<div>' );
1788 this.$foot
= this.$( '<div>' );
1789 this.$overlay
= this.$( '<div>' );
1792 this.$element
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
1795 this.$head
.addClass( 'oo-ui-window-head' );
1796 this.$body
.addClass( 'oo-ui-window-body' );
1797 this.$foot
.addClass( 'oo-ui-window-foot' );
1798 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1799 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$overlay
);
1807 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1808 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1810 * @param {Object} [data] Window opening data
1811 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1812 * first argument will be a promise which will be resolved when the window begins closing
1814 OO
.ui
.Window
.prototype.open = function ( data
) {
1815 return this.manager
.openWindow( this, data
);
1821 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1822 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1824 * @param {Object} [data] Window closing data
1825 * @return {jQuery.Promise} Promise resolved when window is closed
1827 OO
.ui
.Window
.prototype.close = function ( data
) {
1828 return this.manager
.closeWindow( this, data
);
1834 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1837 * @param {Object} [data] Window opening data
1838 * @return {jQuery.Promise} Promise resolved when window is setup
1840 OO
.ui
.Window
.prototype.setup = function ( data
) {
1842 deferred
= $.Deferred();
1844 this.$element
.show();
1845 this.visible
= true;
1846 this.getSetupProcess( data
).execute().done( function () {
1847 // Force redraw by asking the browser to measure the elements' widths
1848 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
1849 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
1853 return deferred
.promise();
1859 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1862 * @param {Object} [data] Window opening data
1863 * @return {jQuery.Promise} Promise resolved when window is ready
1865 OO
.ui
.Window
.prototype.ready = function ( data
) {
1867 deferred
= $.Deferred();
1869 this.$content
.focus();
1870 this.getReadyProcess( data
).execute().done( function () {
1871 // Force redraw by asking the browser to measure the elements' widths
1872 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
1873 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
1877 return deferred
.promise();
1883 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1886 * @param {Object} [data] Window closing data
1887 * @return {jQuery.Promise} Promise resolved when window is held
1889 OO
.ui
.Window
.prototype.hold = function ( data
) {
1891 deferred
= $.Deferred();
1893 this.getHoldProcess( data
).execute().done( function () {
1894 // Get the focused element within the window's content
1895 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
1897 // Blur the focused element
1898 if ( $focus
.length
) {
1902 // Force redraw by asking the browser to measure the elements' widths
1903 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
1904 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
1908 return deferred
.promise();
1914 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1917 * @param {Object} [data] Window closing data
1918 * @return {jQuery.Promise} Promise resolved when window is torn down
1920 OO
.ui
.Window
.prototype.teardown = function ( data
) {
1922 deferred
= $.Deferred();
1924 this.getTeardownProcess( data
).execute().done( function () {
1925 // Force redraw by asking the browser to measure the elements' widths
1926 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
1927 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
1928 win
.$element
.hide();
1929 win
.visible
= false;
1933 return deferred
.promise();
1937 * Load the frame contents.
1939 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
1940 * promise will be resolved. Calling while loading will return a promise but not trigger a new
1941 * loading cycle. Calling after loading is complete will return a promise that's already been
1944 * Sounds simple right? Read on...
1946 * When you create a dynamic iframe using open/write/close, the window.load event for the
1947 * iframe is triggered when you call close, and there's no further load event to indicate that
1948 * everything is actually loaded.
1950 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
1951 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
1952 * are added to document.styleSheets immediately, and the only way you can determine whether they've
1953 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
1954 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
1956 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
1957 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
1958 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
1959 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
1960 * accessing .cssRules is allowed.
1962 * However, now that we control the styles we're injecting, we might as well do away with
1963 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
1964 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
1965 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
1966 * font-family rule is not applied until after the `@import` finishes.
1968 * All this stylesheet injection and polling magic is in #transplantStyles.
1970 * @return {jQuery.Promise} Promise resolved when loading is complete
1973 OO
.ui
.Window
.prototype.load = function () {
1974 var sub
, doc
, loading
,
1977 // Non-isolated windows are already "loaded"
1978 if ( !this.loading
&& !this.isolated
) {
1979 this.loading
= $.Deferred().resolve();
1981 // Set initialized state after so sub-classes aren't confused by it being set by calling
1982 // their parent initialize method
1983 this.initialized
= true;
1986 // Return existing promise if already loading or loaded
1987 if ( this.loading
) {
1988 return this.loading
.promise();
1992 loading
= this.loading
= $.Deferred();
1993 sub
= this.$iframe
.prop( 'contentWindow' );
1996 // Initialize contents
2001 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2002 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2003 '<div class="oo-ui-window-content"></div>' +
2010 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$element
);
2011 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2012 this.$document
= this.$( doc
);
2015 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2016 .always( function () {
2017 // Initialize isolated windows
2019 // Set initialized state after so sub-classes aren't confused by it being set by calling
2020 // their parent initialize method
2021 win
.initialized
= true;
2022 // Undo the visibility: hidden; hack and apply display: none;
2023 // We can do this safely now that the iframe has initialized
2024 // (don't do this from within #initialize because it has to happen
2025 // after the all subclasses have been handled as well).
2026 win
.toggle( win
.isVisible() );
2031 return loading
.promise();
2035 * Base class for all dialogs.
2038 * - Manage the window (open and close, etc.).
2039 * - Store the internal name and display title.
2040 * - A stack to track one or more pending actions.
2041 * - Manage a set of actions that can be performed.
2042 * - Configure and create action widgets.
2045 * - Close the dialog with Escape key.
2046 * - Visually lock the dialog while an action is in
2047 * progress (aka "pending").
2049 * Subclass responsibilities:
2050 * - Display the title somewhere.
2051 * - Add content to the dialog.
2052 * - Provide a UI to close the dialog.
2053 * - Display the action widgets somewhere.
2057 * @extends OO.ui.Window
2060 * @param {Object} [config] Configuration options
2062 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2063 // Parent constructor
2064 OO
.ui
.Dialog
.super.call( this, config
);
2067 this.actions
= new OO
.ui
.ActionSet();
2068 this.attachedActions
= [];
2069 this.currentAction
= null;
2073 this.actions
.connect( this, {
2074 click
: 'onActionClick',
2075 resize
: 'onActionResize',
2076 change
: 'onActionsChange'
2081 .addClass( 'oo-ui-dialog' )
2082 .attr( 'role', 'dialog' );
2087 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2089 /* Static Properties */
2092 * Symbolic name of dialog.
2097 * @property {string}
2099 OO
.ui
.Dialog
.static.name
= '';
2107 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2109 OO
.ui
.Dialog
.static.title
= '';
2112 * List of OO.ui.ActionWidget configuration options.
2116 * @property {Object[]}
2118 OO
.ui
.Dialog
.static.actions
= [];
2121 * Close dialog when the escape key is pressed.
2126 * @property {boolean}
2128 OO
.ui
.Dialog
.static.escapable
= true;
2133 * Handle frame document key down events.
2135 * @param {jQuery.Event} e Key down event
2137 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2138 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2145 * Handle action resized events.
2147 * @param {OO.ui.ActionWidget} action Action that was resized
2149 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2150 // Override in subclass
2154 * Handle action click events.
2156 * @param {OO.ui.ActionWidget} action Action that was clicked
2158 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2159 if ( !this.isPending() ) {
2160 this.currentAction
= action
;
2161 this.executeAction( action
.getAction() );
2166 * Handle actions change event.
2168 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2169 this.detachActions();
2170 if ( !this.isClosing() ) {
2171 this.attachActions();
2176 * Check if input is pending.
2180 OO
.ui
.Dialog
.prototype.isPending = function () {
2181 return !!this.pending
;
2185 * Get set of actions.
2187 * @return {OO.ui.ActionSet}
2189 OO
.ui
.Dialog
.prototype.getActions = function () {
2190 return this.actions
;
2194 * Get a process for taking action.
2196 * When you override this method, you can add additional accept steps to the process the parent
2197 * method provides using the 'first' and 'next' methods.
2200 * @param {string} [action] Symbolic name of action
2201 * @return {OO.ui.Process} Action process
2203 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2204 return new OO
.ui
.Process()
2205 .next( function () {
2207 // An empty action always closes the dialog without data, which should always be
2208 // safe and make no changes
2217 * @param {Object} [data] Dialog opening data
2218 * @param {jQuery|string|Function|null} [data.label] Dialog label, omit to use #static-label
2219 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2220 * action item, omit to use #static-actions
2222 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2226 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2227 .next( function () {
2230 config
= this.constructor.static,
2231 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2233 this.title
.setLabel(
2234 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2236 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2238 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2241 this.actions
.add( items
);
2248 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2250 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2251 .first( function () {
2252 this.actions
.clear();
2253 this.currentAction
= null;
2260 OO
.ui
.Dialog
.prototype.initialize = function () {
2262 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2265 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2268 if ( this.constructor.static.escapable
) {
2269 this.$document
.on( 'keydown', OO
.ui
.bind( this.onDocumentKeyDown
, this ) );
2273 this.$content
.addClass( 'oo-ui-dialog-content' );
2277 * Attach action actions.
2279 OO
.ui
.Dialog
.prototype.attachActions = function () {
2280 // Remember the list of potentially attached actions
2281 this.attachedActions
= this.actions
.get();
2285 * Detach action actions.
2289 OO
.ui
.Dialog
.prototype.detachActions = function () {
2292 // Detach all actions that may have been previously attached
2293 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2294 this.attachedActions
[i
].$element
.detach();
2296 this.attachedActions
= [];
2300 * Execute an action.
2302 * @param {string} action Symbolic name of action to execute
2303 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2305 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2307 return this.getActionProcess( action
).execute()
2308 .always( OO
.ui
.bind( this.popPending
, this ) );
2312 * Increase the pending stack.
2316 OO
.ui
.Dialog
.prototype.pushPending = function () {
2317 if ( this.pending
=== 0 ) {
2318 this.$content
.addClass( 'oo-ui-actionDialog-content-pending' );
2319 this.$head
.addClass( 'oo-ui-texture-pending' );
2327 * Reduce the pending stack.
2333 OO
.ui
.Dialog
.prototype.popPending = function () {
2334 if ( this.pending
=== 1 ) {
2335 this.$content
.removeClass( 'oo-ui-actionDialog-content-pending' );
2336 this.$head
.removeClass( 'oo-ui-texture-pending' );
2338 this.pending
= Math
.max( 0, this.pending
- 1 );
2344 * Collection of windows.
2347 * @extends OO.ui.Element
2348 * @mixins OO.EventEmitter
2350 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2351 * already opening or opened, the current window will be closed without data. Empty closing data
2352 * should always result in the window being closed without causing constructive or destructive
2355 * As a window is opened and closed, it passes through several stages and the manager emits several
2356 * corresponding events.
2358 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2359 * - {@link #event-opening} is emitted with `opening` promise
2360 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2361 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2362 * - `setup` progress notification is emitted from opening promise
2363 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2364 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2365 * - `ready` progress notification is emitted from opening promise
2366 * - `opening` promise is resolved with `opened` promise
2367 * - Window is now open
2369 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2370 * - `opened` promise is resolved with `closing` promise
2371 * - {@link #event-closing} is emitted with `closing` promise
2372 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2373 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2374 * - `hold` progress notification is emitted from opening promise
2375 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2376 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2377 * - `teardown` progress notification is emitted from opening promise
2378 * - Closing promise is resolved
2379 * - Window is now closed
2382 * @param {Object} [config] Configuration options
2383 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2384 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2385 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2387 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2388 // Configuration initialization
2389 config
= config
|| {};
2391 // Parent constructor
2392 OO
.ui
.WindowManager
.super.call( this, config
);
2394 // Mixin constructors
2395 OO
.EventEmitter
.call( this );
2398 this.factory
= config
.factory
;
2399 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2400 this.isolate
= !!config
.isolate
;
2402 this.opening
= null;
2404 this.closing
= null;
2405 this.preparingToOpen
= null;
2406 this.preparingToClose
= null;
2408 this.currentWindow
= null;
2409 this.$ariaHidden
= null;
2410 this.requestedSize
= null;
2411 this.onWindowResizeTimeout
= null;
2412 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
2413 this.afterWindowResizeHandler
= OO
.ui
.bind( this.afterWindowResize
, this );
2414 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
2415 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
2419 .addClass( 'oo-ui-windowManager' )
2420 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2425 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2426 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2431 * Window is opening.
2433 * Fired when the window begins to be opened.
2436 * @param {OO.ui.Window} win Window that's being opened
2437 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2438 * resolved the first argument will be a promise which will be resolved when the window begins
2439 * closing, the second argument will be the opening data; progress notifications will be fired on
2440 * the promise for `setup` and `ready` when those processes are completed respectively.
2441 * @param {Object} data Window opening data
2445 * Window is closing.
2447 * Fired when the window begins to be closed.
2450 * @param {OO.ui.Window} win Window that's being closed
2451 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2452 * is resolved the first argument will be a the closing data; progress notifications will be fired
2453 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2454 * @param {Object} data Window closing data
2457 /* Static Properties */
2460 * Map of symbolic size names and CSS properties.
2464 * @property {Object}
2466 OO
.ui
.WindowManager
.static.sizes
= {
2477 // These can be non-numeric because they are never used in calculations
2484 * Symbolic name of default size.
2486 * Default size is used if the window's requested size is not recognized.
2490 * @property {string}
2492 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2497 * Handle window resize events.
2499 * @param {jQuery.Event} e Window resize event
2501 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2502 clearTimeout( this.onWindowResizeTimeout
);
2503 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2507 * Handle window resize events.
2509 * @param {jQuery.Event} e Window resize event
2511 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2512 if ( this.currentWindow
) {
2513 this.updateWindowSize( this.currentWindow
);
2518 * Handle window mouse wheel events.
2520 * @param {jQuery.Event} e Mouse wheel event
2522 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2527 * Handle document key down events.
2529 * @param {jQuery.Event} e Key down event
2531 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2532 switch ( e
.which
) {
2533 case OO
.ui
.Keys
.PAGEUP
:
2534 case OO
.ui
.Keys
.PAGEDOWN
:
2535 case OO
.ui
.Keys
.END
:
2536 case OO
.ui
.Keys
.HOME
:
2537 case OO
.ui
.Keys
.LEFT
:
2539 case OO
.ui
.Keys
.RIGHT
:
2540 case OO
.ui
.Keys
.DOWN
:
2541 // Prevent any key events that might cause scrolling
2547 * Check if window is opening.
2549 * @return {boolean} Window is opening
2551 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2552 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2556 * Check if window is closing.
2558 * @return {boolean} Window is closing
2560 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2561 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2565 * Check if window is opened.
2567 * @return {boolean} Window is opened
2569 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2570 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2574 * Check if window contents should be isolated.
2576 * Window content isolation is done using inline frames.
2578 * @return {boolean} Window contents should be isolated
2580 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2581 return this.isolate
;
2585 * Check if a window is being managed.
2587 * @param {OO.ui.Window} win Window to check
2588 * @return {boolean} Window is being managed
2590 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2593 for ( name
in this.windows
) {
2594 if ( this.windows
[name
] === win
) {
2603 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2605 * @param {OO.ui.Window} win Window being opened
2606 * @param {Object} [data] Window opening data
2607 * @return {number} Milliseconds to wait
2609 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2614 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2616 * @param {OO.ui.Window} win Window being opened
2617 * @param {Object} [data] Window opening data
2618 * @return {number} Milliseconds to wait
2620 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2625 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2627 * @param {OO.ui.Window} win Window being closed
2628 * @param {Object} [data] Window closing data
2629 * @return {number} Milliseconds to wait
2631 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2636 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2638 * @param {OO.ui.Window} win Window being closed
2639 * @param {Object} [data] Window closing data
2640 * @return {number} Milliseconds to wait
2642 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2643 return this.modal
? 250 : 0;
2647 * Get managed window by symbolic name.
2649 * If window is not yet instantiated, it will be instantiated and added automatically.
2651 * @param {string} name Symbolic window name
2652 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2653 * @throws {Error} If the symbolic name is unrecognized by the factory
2654 * @throws {Error} If the symbolic name unrecognized as a managed window
2656 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2657 var deferred
= $.Deferred(),
2658 win
= this.windows
[name
];
2660 if ( !( win
instanceof OO
.ui
.Window
) ) {
2661 if ( this.factory
) {
2662 if ( !this.factory
.lookup( name
) ) {
2663 deferred
.reject( new OO
.ui
.Error(
2664 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2667 win
= this.factory
.create( name
, this, { $: this.$ } );
2668 this.addWindows( [ win
] );
2669 deferred
.resolve( win
);
2672 deferred
.reject( new OO
.ui
.Error(
2673 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2677 deferred
.resolve( win
);
2680 return deferred
.promise();
2684 * Get current window.
2686 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2688 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2689 return this.currentWindow
;
2695 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2696 * @param {Object} [data] Window opening data
2697 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2698 * for more details about the `opening` promise
2701 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2704 opening
= $.Deferred();
2706 // Argument handling
2707 if ( typeof win
=== 'string' ) {
2708 return this.getWindow( win
).then( function ( win
) {
2709 return manager
.openWindow( win
, data
);
2714 if ( !this.hasWindow( win
) ) {
2715 opening
.reject( new OO
.ui
.Error(
2716 'Cannot open window: window is not attached to manager'
2718 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2719 opening
.reject( new OO
.ui
.Error(
2720 'Cannot open window: another window is opening or open'
2725 if ( opening
.state() !== 'rejected' ) {
2726 // Begin loading the window if it's not loading or loaded already - may take noticable time
2727 // and we want to do this in paralell with any other preparatory actions
2728 if ( !win
.isLoading() && !win
.isLoaded() ) {
2729 // Finish initializing the window (must be done after manager is attached to DOM)
2730 win
.setManager( this );
2731 preparing
.push( win
.load() );
2734 if ( this.closing
) {
2735 // If a window is currently closing, wait for it to complete
2736 preparing
.push( this.closing
);
2739 this.preparingToOpen
= $.when
.apply( $, preparing
);
2740 // Ensure handlers get called after preparingToOpen is set
2741 this.preparingToOpen
.done( function () {
2742 if ( manager
.modal
) {
2743 manager
.toggleGlobalEvents( true );
2744 manager
.toggleAriaIsolation( true );
2746 manager
.currentWindow
= win
;
2747 manager
.opening
= opening
;
2748 manager
.preparingToOpen
= null;
2749 manager
.emit( 'opening', win
, opening
, data
);
2750 setTimeout( function () {
2751 win
.setup( data
).then( function () {
2752 manager
.updateWindowSize( win
);
2753 manager
.opening
.notify( { state
: 'setup' } );
2754 setTimeout( function () {
2755 win
.ready( data
).then( function () {
2756 manager
.updateWindowSize( win
);
2757 manager
.opening
.notify( { state
: 'ready' } );
2758 manager
.opening
= null;
2759 manager
.opened
= $.Deferred();
2760 opening
.resolve( manager
.opened
.promise(), data
);
2762 }, manager
.getReadyDelay() );
2764 }, manager
.getSetupDelay() );
2768 return opening
.promise();
2774 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2775 * @param {Object} [data] Window closing data
2776 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2777 * for more details about the `closing` promise
2778 * @throws {Error} If no window by that name is being managed
2781 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2784 closing
= $.Deferred(),
2785 opened
= this.opened
;
2787 // Argument handling
2788 if ( typeof win
=== 'string' ) {
2789 win
= this.windows
[win
];
2790 } else if ( !this.hasWindow( win
) ) {
2796 closing
.reject( new OO
.ui
.Error(
2797 'Cannot close window: window is not attached to manager'
2799 } else if ( win
!== this.currentWindow
) {
2800 closing
.reject( new OO
.ui
.Error(
2801 'Cannot close window: window already closed with different data'
2803 } else if ( this.preparingToClose
|| this.closing
) {
2804 closing
.reject( new OO
.ui
.Error(
2805 'Cannot close window: window already closing with different data'
2810 if ( closing
.state() !== 'rejected' ) {
2811 if ( this.opening
) {
2812 // If the window is currently opening, close it when it's done
2813 preparing
.push( this.opening
);
2816 this.preparingToClose
= $.when
.apply( $, preparing
);
2817 // Ensure handlers get called after preparingToClose is set
2818 this.preparingToClose
.done( function () {
2819 manager
.closing
= closing
;
2820 manager
.preparingToClose
= null;
2821 manager
.emit( 'closing', win
, closing
, data
);
2822 manager
.opened
= null;
2823 opened
.resolve( closing
.promise(), data
);
2824 setTimeout( function () {
2825 win
.hold( data
).then( function () {
2826 closing
.notify( { state
: 'hold' } );
2827 setTimeout( function () {
2828 win
.teardown( data
).then( function () {
2829 closing
.notify( { state
: 'teardown' } );
2830 if ( manager
.modal
) {
2831 manager
.toggleGlobalEvents( false );
2832 manager
.toggleAriaIsolation( false );
2834 manager
.closing
= null;
2835 manager
.currentWindow
= null;
2836 closing
.resolve( data
);
2838 }, manager
.getTeardownDelay() );
2840 }, manager
.getHoldDelay() );
2844 return closing
.promise();
2850 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2851 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2852 * a statically configured symbolic name
2854 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2855 var i
, len
, win
, name
, list
;
2857 if ( $.isArray( windows
) ) {
2858 // Convert to map of windows by looking up symbolic names from static configuration
2860 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2861 name
= windows
[i
].constructor.static.name
;
2862 if ( typeof name
!== 'string' ) {
2863 throw new Error( 'Cannot add window' );
2865 list
[name
] = windows
[i
];
2867 } else if ( $.isPlainObject( windows
) ) {
2872 for ( name
in list
) {
2874 this.windows
[name
] = win
;
2875 this.$element
.append( win
.$element
);
2882 * Windows will be closed before they are removed.
2884 * @param {string} name Symbolic name of window to remove
2885 * @return {jQuery.Promise} Promise resolved when window is closed and removed
2886 * @throws {Error} If windows being removed are not being managed
2888 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
2889 var i
, len
, win
, name
,
2892 cleanup = function ( name
, win
) {
2893 delete manager
.windows
[name
];
2894 win
.$element
.detach();
2897 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
2899 win
= this.windows
[name
];
2901 throw new Error( 'Cannot remove window' );
2903 promises
.push( this.closeWindow( name
).then( OO
.ui
.bind( cleanup
, null, name
, win
) ) );
2906 return $.when
.apply( $, promises
);
2910 * Remove all windows.
2912 * Windows will be closed before they are removed.
2914 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
2916 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
2917 return this.removeWindows( Object
.keys( this.windows
) );
2923 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
2927 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
2928 // Bypass for non-current, and thus invisible, windows
2929 if ( win
!== this.currentWindow
) {
2933 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
2934 sizes
= this.constructor.static.sizes
,
2935 size
= win
.getSize();
2937 if ( !sizes
[size
] ) {
2938 size
= this.constructor.static.defaultSize
;
2940 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
2944 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
2945 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
2946 win
.setDimensions( sizes
[size
] );
2952 * Bind or unbind global events for scrolling.
2954 * @param {boolean} [on] Bind global events
2957 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
2958 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
2961 if ( !this.globalEvents
) {
2962 this.$( this.getElementDocument() ).on( {
2963 // Prevent scrolling by keys in top-level window
2964 keydown
: this.onDocumentKeyDownHandler
2966 this.$( this.getElementWindow() ).on( {
2967 // Prevent scrolling by wheel in top-level window
2968 mousewheel
: this.onWindowMouseWheelHandler
,
2969 // Start listening for top-level window dimension changes
2970 'orientationchange resize': this.onWindowResizeHandler
2972 this.globalEvents
= true;
2974 } else if ( this.globalEvents
) {
2975 // Unbind global events
2976 this.$( this.getElementDocument() ).off( {
2977 // Allow scrolling by keys in top-level window
2978 keydown
: this.onDocumentKeyDownHandler
2980 this.$( this.getElementWindow() ).off( {
2981 // Allow scrolling by wheel in top-level window
2982 mousewheel
: this.onWindowMouseWheelHandler
,
2983 // Stop listening for top-level window dimension changes
2984 'orientationchange resize': this.onWindowResizeHandler
2986 this.globalEvents
= false;
2993 * Toggle screen reader visibility of content other than the window manager.
2995 * @param {boolean} [isolate] Make only the window manager visible to screen readers
2998 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
2999 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3002 if ( !this.$ariaHidden
) {
3003 // Hide everything other than the window manager from screen readers
3004 this.$ariaHidden
= $( 'body' )
3006 .not( this.$element
.parentsUntil( 'body' ).last() )
3007 .attr( 'aria-hidden', '' );
3009 } else if ( this.$ariaHidden
) {
3010 // Restore screen reader visiblity
3011 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3012 this.$ariaHidden
= null;
3019 * Destroy window manager.
3021 * Windows will not be closed, only removed from the DOM.
3023 OO
.ui
.WindowManager
.prototype.destroy = function () {
3024 this.toggleGlobalEvents( false );
3025 this.toggleAriaIsolation( false );
3026 this.$element
.remove();
3034 * @param {string|jQuery} message Description of error
3035 * @param {Object} [config] Configuration options
3036 * @cfg {boolean} [recoverable=true] Error is recoverable
3038 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3039 // Configuration initialization
3040 config
= config
|| {};
3043 this.message
= message
instanceof jQuery
? message
: String( message
);
3044 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3049 OO
.initClass( OO
.ui
.Error
);
3054 * Check if error can be recovered from.
3056 * @return {boolean} Error is recoverable
3058 OO
.ui
.Error
.prototype.isRecoverable = function () {
3059 return this.recoverable
;
3063 * Get error message as DOM nodes.
3065 * @return {jQuery} Error message in DOM nodes
3067 OO
.ui
.Error
.prototype.getMessage = function () {
3068 return this.message
instanceof jQuery
?
3069 this.message
.clone() :
3070 $( '<div>' ).text( this.message
).contents();
3074 * Get error message as text.
3076 * @return {string} Error message
3078 OO
.ui
.Error
.prototype.getMessageText = function () {
3079 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3083 * A list of functions, called in sequence.
3085 * If a function added to a process returns boolean false the process will stop; if it returns an
3086 * object with a `promise` method the process will use the promise to either continue to the next
3087 * step when the promise is resolved or stop when the promise is rejected.
3092 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3093 * call, see #createStep for more information
3094 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3096 * @return {Object} Step object, with `callback` and `context` properties
3098 OO
.ui
.Process = function ( step
, context
) {
3103 if ( step
!== undefined ) {
3104 this.next( step
, context
);
3110 OO
.initClass( OO
.ui
.Process
);
3115 * Start the process.
3117 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3118 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3119 * process, the remaining steps will not be taken
3121 OO
.ui
.Process
.prototype.execute = function () {
3122 var i
, len
, promise
;
3125 * Continue execution.
3128 * @param {Array} step A function and the context it should be called in
3129 * @return {Function} Function that continues the process
3131 function proceed( step
) {
3132 return function () {
3133 // Execute step in the correct context
3135 result
= step
.callback
.call( step
.context
);
3137 if ( result
=== false ) {
3138 // Use rejected promise for boolean false results
3139 return $.Deferred().reject( [] ).promise();
3141 if ( typeof result
=== 'number' ) {
3143 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3145 // Use a delayed promise for numbers, expecting them to be in milliseconds
3146 deferred
= $.Deferred();
3147 setTimeout( deferred
.resolve
, result
);
3148 return deferred
.promise();
3150 if ( result
instanceof OO
.ui
.Error
) {
3151 // Use rejected promise for error
3152 return $.Deferred().reject( [ result
] ).promise();
3154 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3155 // Use rejected promise for list of errors
3156 return $.Deferred().reject( result
).promise();
3158 // Duck-type the object to see if it can produce a promise
3159 if ( result
&& $.isFunction( result
.promise
) ) {
3160 // Use a promise generated from the result
3161 return result
.promise();
3163 // Use resolved promise for other results
3164 return $.Deferred().resolve().promise();
3168 if ( this.steps
.length
) {
3169 // Generate a chain reaction of promises
3170 promise
= proceed( this.steps
[0] )();
3171 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3172 promise
= promise
.then( proceed( this.steps
[i
] ) );
3175 promise
= $.Deferred().resolve().promise();
3182 * Create a process step.
3185 * @param {number|jQuery.Promise|Function} step
3187 * - Number of milliseconds to wait; or
3188 * - Promise to wait to be resolved; or
3189 * - Function to execute
3190 * - If it returns boolean false the process will stop
3191 * - If it returns an object with a `promise` method the process will use the promise to either
3192 * continue to the next step when the promise is resolved or stop when the promise is rejected
3193 * - If it returns a number, the process will wait for that number of milliseconds before
3195 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3197 * @return {Object} Step object, with `callback` and `context` properties
3199 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3200 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3202 callback: function () {
3208 if ( $.isFunction( step
) ) {
3214 throw new Error( 'Cannot create process step: number, promise or function expected' );
3218 * Add step to the beginning of the process.
3220 * @inheritdoc #createStep
3221 * @return {OO.ui.Process} this
3224 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3225 this.steps
.unshift( this.createStep( step
, context
) );
3230 * Add step to the end of the process.
3232 * @inheritdoc #createStep
3233 * @return {OO.ui.Process} this
3236 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3237 this.steps
.push( this.createStep( step
, context
) );
3242 * Factory for tools.
3245 * @extends OO.Factory
3248 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3249 // Parent constructor
3250 OO
.ui
.ToolFactory
.super.call( this );
3255 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3260 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3261 var i
, len
, included
, promoted
, demoted
,
3265 // Collect included and not excluded tools
3266 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3269 promoted
= this.extract( promote
, used
);
3270 demoted
= this.extract( demote
, used
);
3273 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3274 if ( !used
[included
[i
]] ) {
3275 auto
.push( included
[i
] );
3279 return promoted
.concat( auto
).concat( demoted
);
3283 * Get a flat list of names from a list of names or groups.
3285 * Tools can be specified in the following ways:
3287 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3288 * - All tools in a group: `{ group: 'group-name' }`
3289 * - All tools: `'*'`
3292 * @param {Array|string} collection List of tools
3293 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3294 * names will be added as properties
3295 * @return {string[]} List of extracted names
3297 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3298 var i
, len
, item
, name
, tool
,
3301 if ( collection
=== '*' ) {
3302 for ( name
in this.registry
) {
3303 tool
= this.registry
[name
];
3305 // Only add tools by group name when auto-add is enabled
3306 tool
.static.autoAddToCatchall
&&
3307 // Exclude already used tools
3308 ( !used
|| !used
[name
] )
3316 } else if ( $.isArray( collection
) ) {
3317 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3318 item
= collection
[i
];
3319 // Allow plain strings as shorthand for named tools
3320 if ( typeof item
=== 'string' ) {
3321 item
= { name
: item
};
3323 if ( OO
.isPlainObject( item
) ) {
3325 for ( name
in this.registry
) {
3326 tool
= this.registry
[name
];
3328 // Include tools with matching group
3329 tool
.static.group
=== item
.group
&&
3330 // Only add tools by group name when auto-add is enabled
3331 tool
.static.autoAddToGroup
&&
3332 // Exclude already used tools
3333 ( !used
|| !used
[name
] )
3341 // Include tools with matching name and exclude already used tools
3342 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3343 names
.push( item
.name
);
3345 used
[item
.name
] = true;
3355 * Factory for tool groups.
3358 * @extends OO.Factory
3361 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3362 // Parent constructor
3363 OO
.Factory
.call( this );
3366 defaultClasses
= this.constructor.static.getDefaultClasses();
3368 // Register default toolgroups
3369 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3370 this.register( defaultClasses
[i
] );
3376 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3378 /* Static Methods */
3381 * Get a default set of classes to be registered on construction
3383 * @return {Function[]} Default classes
3385 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3388 OO
.ui
.ListToolGroup
,
3394 * Element with a button.
3396 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3397 * and access keys for accessibility purposes.
3403 * @param {Object} [config] Configuration options
3404 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3405 * @cfg {boolean} [framed=true] Render button with a frame
3406 * @cfg {number} [tabIndex=0] Button's tab index, use null to have no tabIndex
3407 * @cfg {string} [accessKey] Button's access key
3409 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3410 // Configuration initialization
3411 config
= config
|| {};
3414 this.$button
= null;
3416 this.tabIndex
= null;
3417 this.accessKey
= null;
3418 this.active
= false;
3419 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
3420 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
3423 this.$element
.addClass( 'oo-ui-buttonElement' );
3424 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3425 this.setTabIndex( config
.tabIndex
|| 0 );
3426 this.setAccessKey( config
.accessKey
);
3427 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3432 OO
.initClass( OO
.ui
.ButtonElement
);
3434 /* Static Properties */
3437 * Cancel mouse down events.
3441 * @property {boolean}
3443 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3448 * Set the button element.
3450 * If an element is already set, it will be cleaned up before setting up the new element.
3452 * @param {jQuery} $button Element to use as button
3454 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3455 if ( this.$button
) {
3457 .removeClass( 'oo-ui-buttonElement-button' )
3458 .removeAttr( 'role accesskey tabindex' )
3459 .off( this.onMouseDownHandler
);
3462 this.$button
= $button
3463 .addClass( 'oo-ui-buttonElement-button' )
3464 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3465 .on( 'mousedown', this.onMouseDownHandler
);
3469 * Handles mouse down events.
3471 * @param {jQuery.Event} e Mouse down event
3473 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3474 if ( this.isDisabled() || e
.which
!== 1 ) {
3477 // Remove the tab-index while the button is down to prevent the button from stealing focus
3478 this.$button
.removeAttr( 'tabindex' );
3479 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3480 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3481 // reliably reapply the tabindex and remove the pressed class
3482 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3483 // Prevent change of focus unless specifically configured otherwise
3484 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3490 * Handles mouse up events.
3492 * @param {jQuery.Event} e Mouse up event
3494 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3495 if ( this.isDisabled() || e
.which
!== 1 ) {
3498 // Restore the tab-index after the button is up to restore the button's accesssibility
3499 this.$button
.attr( 'tabindex', this.tabIndex
);
3500 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3501 // Stop listening for mouseup, since we only needed this once
3502 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3508 * @param {boolean} [framed] Make button framed, omit to toggle
3511 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3512 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3513 if ( framed
!== this.framed
) {
3514 this.framed
= framed
;
3516 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3517 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3526 * @param {number|null} tabIndex Button's tab index, use null to remove
3529 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3530 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3532 if ( this.tabIndex
!== tabIndex
) {
3533 if ( this.$button
) {
3534 if ( tabIndex
!== null ) {
3535 this.$button
.attr( 'tabindex', tabIndex
);
3537 this.$button
.removeAttr( 'tabindex' );
3540 this.tabIndex
= tabIndex
;
3549 * @param {string} accessKey Button's access key, use empty string to remove
3552 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3553 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3555 if ( this.accessKey
!== accessKey
) {
3556 if ( this.$button
) {
3557 if ( accessKey
!== null ) {
3558 this.$button
.attr( 'accesskey', accessKey
);
3560 this.$button
.removeAttr( 'accesskey' );
3563 this.accessKey
= accessKey
;
3572 * @param {boolean} [value] Make button active
3575 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3576 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3581 * Element containing a sequence of child elements.
3587 * @param {Object} [config] Configuration options
3588 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3590 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3592 config
= config
|| {};
3597 this.aggregateItemEvents
= {};
3600 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3606 * Set the group element.
3608 * If an element is already set, items will be moved to the new element.
3610 * @param {jQuery} $group Element to use as group
3612 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3615 this.$group
= $group
;
3616 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3617 this.$group
.append( this.items
[i
].$element
);
3622 * Check if there are no items.
3624 * @return {boolean} Group is empty
3626 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3627 return !this.items
.length
;
3633 * @return {OO.ui.Element[]} Items
3635 OO
.ui
.GroupElement
.prototype.getItems = function () {
3636 return this.items
.slice( 0 );
3640 * Add an aggregate item event.
3642 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3643 * and with an additional leading parameter containing the item that emitted the original event.
3644 * Other arguments that were emitted from the original event are passed through.
3646 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3647 * event, use null value to remove aggregation
3648 * @throws {Error} If aggregation already exists
3650 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3651 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3653 for ( itemEvent
in events
) {
3654 groupEvent
= events
[itemEvent
];
3656 // Remove existing aggregated event
3657 if ( itemEvent
in this.aggregateItemEvents
) {
3658 // Don't allow duplicate aggregations
3660 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3662 // Remove event aggregation from existing items
3663 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3664 item
= this.items
[i
];
3665 if ( item
.connect
&& item
.disconnect
) {
3667 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3668 item
.disconnect( this, remove
);
3671 // Prevent future items from aggregating event
3672 delete this.aggregateItemEvents
[itemEvent
];
3675 // Add new aggregate event
3677 // Make future items aggregate event
3678 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3679 // Add event aggregation to existing items
3680 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3681 item
= this.items
[i
];
3682 if ( item
.connect
&& item
.disconnect
) {
3684 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3685 item
.connect( this, add
);
3695 * Adding an existing item (by value) will move it.
3697 * @param {OO.ui.Element[]} items Item
3698 * @param {number} [index] Index to insert items at
3701 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3702 var i
, len
, item
, event
, events
, currentIndex
,
3705 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3708 // Check if item exists then remove it first, effectively "moving" it
3709 currentIndex
= $.inArray( item
, this.items
);
3710 if ( currentIndex
>= 0 ) {
3711 this.removeItems( [ item
] );
3712 // Adjust index to compensate for removal
3713 if ( currentIndex
< index
) {
3718 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3720 for ( event
in this.aggregateItemEvents
) {
3721 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3723 item
.connect( this, events
);
3725 item
.setElementGroup( this );
3726 itemElements
.push( item
.$element
.get( 0 ) );
3729 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3730 this.$group
.append( itemElements
);
3731 this.items
.push
.apply( this.items
, items
);
3732 } else if ( index
=== 0 ) {
3733 this.$group
.prepend( itemElements
);
3734 this.items
.unshift
.apply( this.items
, items
);
3736 this.items
[index
].$element
.before( itemElements
);
3737 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3746 * Items will be detached, not removed, so they can be used later.
3748 * @param {OO.ui.Element[]} items Items to remove
3751 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3752 var i
, len
, item
, index
, remove
, itemEvent
;
3754 // Remove specific items
3755 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3757 index
= $.inArray( item
, this.items
);
3758 if ( index
!== -1 ) {
3760 item
.connect
&& item
.disconnect
&&
3761 !$.isEmptyObject( this.aggregateItemEvents
)
3764 if ( itemEvent
in this.aggregateItemEvents
) {
3765 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3767 item
.disconnect( this, remove
);
3769 item
.setElementGroup( null );
3770 this.items
.splice( index
, 1 );
3771 item
.$element
.detach();
3781 * Items will be detached, not removed, so they can be used later.
3785 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3786 var i
, len
, item
, remove
, itemEvent
;
3789 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3790 item
= this.items
[i
];
3792 item
.connect
&& item
.disconnect
&&
3793 !$.isEmptyObject( this.aggregateItemEvents
)
3796 if ( itemEvent
in this.aggregateItemEvents
) {
3797 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3799 item
.disconnect( this, remove
);
3801 item
.setElementGroup( null );
3802 item
.$element
.detach();
3810 * Element containing an icon.
3812 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
3813 * a control or convey information in a more space efficient way. Icons should rarely be used
3814 * without labels; such as in a toolbar where space is at a premium or within a context where the
3815 * meaning is very clear to the user.
3821 * @param {Object} [config] Configuration options
3822 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
3823 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
3824 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3826 * @cfg {string} [iconTitle] Icon title text or a function that returns text
3828 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
3829 // Config intialization
3830 config
= config
|| {};
3835 this.iconTitle
= null;
3838 this.setIcon( config
.icon
|| this.constructor.static.icon
);
3839 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
3840 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
3845 OO
.initClass( OO
.ui
.IconElement
);
3847 /* Static Properties */
3852 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
3854 * For i18n purposes, this property can be an object containing a `default` icon name property and
3855 * additional icon names keyed by language code.
3857 * Example of i18n icon definition:
3858 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
3862 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
3863 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3866 OO
.ui
.IconElement
.static.icon
= null;
3873 * @property {string|Function|null} Icon title text, a function that returns text or null for no
3876 OO
.ui
.IconElement
.static.iconTitle
= null;
3881 * Set the icon element.
3883 * If an element is already set, it will be cleaned up before setting up the new element.
3885 * @param {jQuery} $icon Element to use as icon
3887 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
3890 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
3891 .removeAttr( 'title' );
3895 .addClass( 'oo-ui-iconElement-icon' )
3896 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
3897 if ( this.iconTitle
!== null ) {
3898 this.$icon
.attr( 'title', this.iconTitle
);
3905 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
3906 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3907 * language, use null to remove icon
3910 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
3911 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
3912 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
3914 if ( this.icon
!== icon
) {
3916 if ( this.icon
!== null ) {
3917 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
3919 if ( icon
!== null ) {
3920 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
3926 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
3934 * @param {string|Function|null} icon Icon title text, a function that returns text or null
3938 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
3939 iconTitle
= typeof iconTitle
=== 'function' ||
3940 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
3941 OO
.ui
.resolveMsg( iconTitle
) : null;
3943 if ( this.iconTitle
!== iconTitle
) {
3944 this.iconTitle
= iconTitle
;
3946 if ( this.iconTitle
!== null ) {
3947 this.$icon
.attr( 'title', iconTitle
);
3949 this.$icon
.removeAttr( 'title' );
3960 * @return {string} Icon
3962 OO
.ui
.IconElement
.prototype.getIcon = function () {
3967 * Element containing an indicator.
3969 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
3970 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
3971 * instead of performing an action directly, or an item in a list which has errors that need to be
3978 * @param {Object} [config] Configuration options
3979 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
3981 * @cfg {string} [indicator] Symbolic indicator name
3982 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
3984 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
3985 // Config intialization
3986 config
= config
|| {};
3989 this.$indicator
= null;
3990 this.indicator
= null;
3991 this.indicatorTitle
= null;
3994 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
3995 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
3996 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4001 OO
.initClass( OO
.ui
.IndicatorElement
);
4003 /* Static Properties */
4010 * @property {string|null} Symbolic indicator name or null for no indicator
4012 OO
.ui
.IndicatorElement
.static.indicator
= null;
4019 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4022 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4027 * Set the indicator element.
4029 * If an element is already set, it will be cleaned up before setting up the new element.
4031 * @param {jQuery} $indicator Element to use as indicator
4033 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4034 if ( this.$indicator
) {
4036 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4037 .removeAttr( 'title' );
4040 this.$indicator
= $indicator
4041 .addClass( 'oo-ui-indicatorElement-indicator' )
4042 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4043 if ( this.indicatorTitle
!== null ) {
4044 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4051 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4054 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4055 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4057 if ( this.indicator
!== indicator
) {
4058 if ( this.$indicator
) {
4059 if ( this.indicator
!== null ) {
4060 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4062 if ( indicator
!== null ) {
4063 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4066 this.indicator
= indicator
;
4069 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4075 * Set indicator title.
4077 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4078 * null for no indicator title
4081 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4082 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4083 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4084 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4086 if ( this.indicatorTitle
!== indicatorTitle
) {
4087 this.indicatorTitle
= indicatorTitle
;
4088 if ( this.$indicator
) {
4089 if ( this.indicatorTitle
!== null ) {
4090 this.$indicator
.attr( 'title', indicatorTitle
);
4092 this.$indicator
.removeAttr( 'title' );
4103 * @return {string} title Symbolic name of indicator
4105 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4106 return this.indicator
;
4110 * Get indicator title.
4112 * @return {string} Indicator title text
4114 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4115 return this.indicatorTitle
;
4119 * Element containing a label.
4125 * @param {Object} [config] Configuration options
4126 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4127 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4128 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4130 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4131 // Config intialization
4132 config
= config
|| {};
4137 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4140 this.setLabel( config
.label
|| this.constructor.static.label
);
4141 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4146 OO
.initClass( OO
.ui
.LabelElement
);
4148 /* Static Properties */
4155 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4158 OO
.ui
.LabelElement
.static.label
= null;
4163 * Set the label element.
4165 * If an element is already set, it will be cleaned up before setting up the new element.
4167 * @param {jQuery} $label Element to use as label
4169 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4170 if ( this.$label
) {
4171 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4174 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4175 this.setLabelContent( this.label
);
4181 * An empty string will result in the label being hidden. A string containing only whitespace will
4182 * be converted to a single
4184 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4185 * text; or null for no label
4188 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4189 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4190 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4192 if ( this.label
!== label
) {
4193 if ( this.$label
) {
4194 this.setLabelContent( label
);
4199 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4207 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4208 * text; or null for no label
4210 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4219 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4220 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4221 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4228 * Set the content of the label.
4230 * Do not call this method until after the label element has been set by #setLabelElement.
4233 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4234 * text; or null for no label
4236 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4237 if ( typeof label
=== 'string' ) {
4238 if ( label
.match( /^\s*$/ ) ) {
4239 // Convert whitespace only string to a single non-breaking space
4240 this.$label
.html( ' ' );
4242 this.$label
.text( label
);
4244 } else if ( label
instanceof jQuery
) {
4245 this.$label
.empty().append( label
);
4247 this.$label
.empty();
4249 this.$label
.css( 'display', !label
? 'none' : '' );
4253 * Element containing an OO.ui.PopupWidget object.
4259 * @param {Object} [config] Configuration options
4260 * @cfg {Object} [popup] Configuration to pass to popup
4261 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4263 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4264 // Configuration initialization
4265 config
= config
|| {};
4268 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4269 { autoClose
: true },
4271 { $: this.$, $autoCloseIgnore
: this.$element
}
4280 * @return {OO.ui.PopupWidget} Popup widget
4282 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4287 * Element with named flags that can be added, removed, listed and checked.
4289 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4290 * the flag name. Flags are primarily useful for styling.
4296 * @param {Object} [config] Configuration options
4297 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4298 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4300 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4301 // Config initialization
4302 config
= config
|| {};
4306 this.$flagged
= null;
4309 this.setFlags( config
.flags
);
4310 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4317 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4318 * added/removed properties
4324 * Set the flagged element.
4326 * If an element is already set, it will be cleaned up before setting up the new element.
4328 * @param {jQuery} $flagged Element to add flags to
4330 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4331 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4332 return 'oo-ui-flaggedElement-' + flag
;
4335 if ( this.$flagged
) {
4336 this.$flagged
.removeClass( classNames
);
4339 this.$flagged
= $flagged
.addClass( classNames
);
4343 * Check if a flag is set.
4345 * @param {string} flag Name of flag
4346 * @return {boolean} Has flag
4348 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4349 return flag
in this.flags
;
4353 * Get the names of all flags set.
4355 * @return {string[]} flags Flag names
4357 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4358 return Object
.keys( this.flags
);
4367 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4368 var flag
, className
,
4371 classPrefix
= 'oo-ui-flaggedElement-';
4373 for ( flag
in this.flags
) {
4374 className
= classPrefix
+ flag
;
4375 changes
[flag
] = false;
4376 delete this.flags
[flag
];
4377 remove
.push( className
);
4380 if ( this.$flagged
) {
4381 this.$flagged
.removeClass( remove
.join( ' ' ) );
4384 this.emit( 'flag', changes
);
4390 * Add one or more flags.
4392 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4393 * keyed by flag name containing boolean set/remove instructions.
4397 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4398 var i
, len
, flag
, className
,
4402 classPrefix
= 'oo-ui-flaggedElement-';
4404 if ( typeof flags
=== 'string' ) {
4405 className
= classPrefix
+ flags
;
4407 if ( !this.flags
[flags
] ) {
4408 this.flags
[flags
] = true;
4409 add
.push( className
);
4411 } else if ( $.isArray( flags
) ) {
4412 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4414 className
= classPrefix
+ flag
;
4416 if ( !this.flags
[flag
] ) {
4417 changes
[flag
] = true;
4418 this.flags
[flag
] = true;
4419 add
.push( className
);
4422 } else if ( OO
.isPlainObject( flags
) ) {
4423 for ( flag
in flags
) {
4424 className
= classPrefix
+ flag
;
4425 if ( flags
[flag
] ) {
4427 if ( !this.flags
[flag
] ) {
4428 changes
[flag
] = true;
4429 this.flags
[flag
] = true;
4430 add
.push( className
);
4434 if ( this.flags
[flag
] ) {
4435 changes
[flag
] = false;
4436 delete this.flags
[flag
];
4437 remove
.push( className
);
4443 if ( this.$flagged
) {
4445 .addClass( add
.join( ' ' ) )
4446 .removeClass( remove
.join( ' ' ) );
4449 this.emit( 'flag', changes
);
4455 * Element with a title.
4457 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4458 * not visible on touch devices.
4464 * @param {Object} [config] Configuration options
4465 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4466 * @cfg {string|Function} [title] Title text or a function that returns text
4468 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4469 // Config intialization
4470 config
= config
|| {};
4473 this.$titled
= null;
4477 this.setTitle( config
.title
|| this.constructor.static.title
);
4478 this.setTitledElement( config
.$titled
|| this.$element
);
4483 OO
.initClass( OO
.ui
.TitledElement
);
4485 /* Static Properties */
4492 * @property {string|Function} Title text or a function that returns text
4494 OO
.ui
.TitledElement
.static.title
= null;
4499 * Set the titled element.
4501 * If an element is already set, it will be cleaned up before setting up the new element.
4503 * @param {jQuery} $titled Element to set title on
4505 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4506 if ( this.$titled
) {
4507 this.$titled
.removeAttr( 'title' );
4510 this.$titled
= $titled
;
4512 this.$titled
.attr( 'title', this.title
);
4519 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4522 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4523 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4525 if ( this.title
!== title
) {
4526 if ( this.$titled
) {
4527 if ( title
!== null ) {
4528 this.$titled
.attr( 'title', title
);
4530 this.$titled
.removeAttr( 'title' );
4542 * @return {string} Title string
4544 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4549 * Element that can be automatically clipped to visible boundaries.
4551 * Whenever the element's natural height changes, you have to call
4552 * #clip to make sure it's still clipping correctly.
4558 * @param {Object} [config] Configuration options
4559 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4561 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4562 // Configuration initialization
4563 config
= config
|| {};
4566 this.$clippable
= null;
4567 this.clipping
= false;
4568 this.clippedHorizontally
= false;
4569 this.clippedVertically
= false;
4570 this.$clippableContainer
= null;
4571 this.$clippableScroller
= null;
4572 this.$clippableWindow
= null;
4573 this.idealWidth
= null;
4574 this.idealHeight
= null;
4575 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
4576 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
4579 this.setClippableElement( config
.$clippable
|| this.$element
);
4585 * Set clippable element.
4587 * If an element is already set, it will be cleaned up before setting up the new element.
4589 * @param {jQuery} $clippable Element to make clippable
4591 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4592 if ( this.$clippable
) {
4593 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4594 this.$clippable
.css( { width
: '', height
: '' } );
4595 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4596 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4599 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4606 * Do not turn clipping on until after the element is attached to the DOM and visible.
4608 * @param {boolean} [clipping] Enable clipping, omit to toggle
4611 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4612 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4614 if ( this.clipping
!== clipping
) {
4615 this.clipping
= clipping
;
4617 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4618 // If the clippable container is the body, we have to listen to scroll events and check
4619 // jQuery.scrollTop on the window because of browser inconsistencies
4620 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4621 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4622 this.$clippableContainer
;
4623 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4624 this.$clippableWindow
= this.$( this.getElementWindow() )
4625 .on( 'resize', this.onClippableWindowResizeHandler
);
4626 // Initial clip after visible
4629 this.$clippable
.css( { width
: '', height
: '' } );
4630 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4631 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4633 this.$clippableContainer
= null;
4634 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4635 this.$clippableScroller
= null;
4636 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4637 this.$clippableWindow
= null;
4645 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4647 * @return {boolean} Element will be clipped to the visible area
4649 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4650 return this.clipping
;
4654 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4656 * @return {boolean} Part of the element is being clipped
4658 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4659 return this.clippedHorizontally
|| this.clippedVertically
;
4663 * Check if the right of the element is being clipped by the nearest scrollable container.
4665 * @return {boolean} Part of the element is being clipped
4667 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4668 return this.clippedHorizontally
;
4672 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4674 * @return {boolean} Part of the element is being clipped
4676 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4677 return this.clippedVertically
;
4681 * Set the ideal size.
4683 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4684 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4686 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4687 this.idealWidth
= width
;
4688 this.idealHeight
= height
;
4692 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4693 * the element's natural height changes.
4695 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4696 * overlapped by, the visible area of the nearest scrollable container.
4700 OO
.ui
.ClippableElement
.prototype.clip = function () {
4701 if ( !this.clipping
) {
4702 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
4707 cOffset
= this.$clippable
.offset(),
4708 $container
= this.$clippableContainer
.is( 'body' ) ?
4709 this.$clippableWindow
: this.$clippableContainer
,
4710 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
4711 ccHeight
= $container
.innerHeight() - buffer
,
4712 ccWidth
= $container
.innerWidth() - buffer
,
4713 scrollTop
= this.$clippableScroller
.scrollTop(),
4714 scrollLeft
= this.$clippableScroller
.scrollLeft(),
4715 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
4716 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
4717 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
4718 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
4719 clipWidth
= desiredWidth
< naturalWidth
,
4720 clipHeight
= desiredHeight
< naturalHeight
;
4723 this.$clippable
.css( { overflowX
: 'auto', width
: desiredWidth
} );
4725 this.$clippable
.css( 'width', this.idealWidth
|| '' );
4726 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4727 this.$clippable
.css( 'overflowX', '' );
4730 this.$clippable
.css( { overflowY
: 'auto', height
: desiredHeight
} );
4732 this.$clippable
.css( 'height', this.idealHeight
|| '' );
4733 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4734 this.$clippable
.css( 'overflowY', '' );
4737 this.clippedHorizontally
= clipWidth
;
4738 this.clippedVertically
= clipHeight
;
4744 * Generic toolbar tool.
4748 * @extends OO.ui.Widget
4749 * @mixins OO.ui.IconElement
4752 * @param {OO.ui.ToolGroup} toolGroup
4753 * @param {Object} [config] Configuration options
4754 * @cfg {string|Function} [title] Title text or a function that returns text
4756 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4757 // Config intialization
4758 config
= config
|| {};
4760 // Parent constructor
4761 OO
.ui
.Tool
.super.call( this, config
);
4763 // Mixin constructors
4764 OO
.ui
.IconElement
.call( this, config
);
4767 this.toolGroup
= toolGroup
;
4768 this.toolbar
= this.toolGroup
.getToolbar();
4769 this.active
= false;
4770 this.$title
= this.$( '<span>' );
4771 this.$link
= this.$( '<a>' );
4775 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
4778 this.$title
.addClass( 'oo-ui-tool-title' );
4780 .addClass( 'oo-ui-tool-link' )
4781 .append( this.$icon
, this.$title
)
4782 .prop( 'tabIndex', 0 )
4783 .attr( 'role', 'button' );
4785 .data( 'oo-ui-tool', this )
4787 'oo-ui-tool ' + 'oo-ui-tool-name-' +
4788 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
4790 .append( this.$link
);
4791 this.setTitle( config
.title
|| this.constructor.static.title
);
4796 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
4797 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
4805 /* Static Properties */
4811 OO
.ui
.Tool
.static.tagName
= 'span';
4814 * Symbolic name of tool.
4819 * @property {string}
4821 OO
.ui
.Tool
.static.name
= '';
4829 * @property {string}
4831 OO
.ui
.Tool
.static.group
= '';
4836 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
4837 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
4838 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
4839 * appended to the title if the tool is part of a bar tool group.
4844 * @property {string|Function} Title text or a function that returns text
4846 OO
.ui
.Tool
.static.title
= '';
4849 * Tool can be automatically added to catch-all groups.
4853 * @property {boolean}
4855 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
4858 * Tool can be automatically added to named groups.
4861 * @property {boolean}
4864 OO
.ui
.Tool
.static.autoAddToGroup
= true;
4867 * Check if this tool is compatible with given data.
4871 * @param {Mixed} data Data to check
4872 * @return {boolean} Tool can be used with data
4874 OO
.ui
.Tool
.static.isCompatibleWith = function () {
4881 * Handle the toolbar state being updated.
4883 * This is an abstract method that must be overridden in a concrete subclass.
4887 OO
.ui
.Tool
.prototype.onUpdateState = function () {
4889 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
4894 * Handle the tool being selected.
4896 * This is an abstract method that must be overridden in a concrete subclass.
4900 OO
.ui
.Tool
.prototype.onSelect = function () {
4902 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
4907 * Check if the button is active.
4909 * @param {boolean} Button is active
4911 OO
.ui
.Tool
.prototype.isActive = function () {
4916 * Make the button appear active or inactive.
4918 * @param {boolean} state Make button appear active
4920 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
4921 this.active
= !!state
;
4922 if ( this.active
) {
4923 this.$element
.addClass( 'oo-ui-tool-active' );
4925 this.$element
.removeClass( 'oo-ui-tool-active' );
4930 * Get the tool title.
4932 * @param {string|Function} title Title text or a function that returns text
4935 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
4936 this.title
= OO
.ui
.resolveMsg( title
);
4942 * Get the tool title.
4944 * @return {string} Title text
4946 OO
.ui
.Tool
.prototype.getTitle = function () {
4951 * Get the tool's symbolic name.
4953 * @return {string} Symbolic name of tool
4955 OO
.ui
.Tool
.prototype.getName = function () {
4956 return this.constructor.static.name
;
4962 OO
.ui
.Tool
.prototype.updateTitle = function () {
4963 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
4964 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
4965 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
4972 .addClass( 'oo-ui-tool-accel' )
4976 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
4977 tooltipParts
.push( this.title
);
4979 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
4980 tooltipParts
.push( accel
);
4982 if ( tooltipParts
.length
) {
4983 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
4985 this.$link
.removeAttr( 'title' );
4992 OO
.ui
.Tool
.prototype.destroy = function () {
4993 this.toolbar
.disconnect( this );
4994 this.$element
.remove();
4998 * Collection of tool groups.
5001 * @extends OO.ui.Element
5002 * @mixins OO.EventEmitter
5003 * @mixins OO.ui.GroupElement
5006 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5007 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5008 * @param {Object} [config] Configuration options
5009 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5010 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5012 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5013 // Configuration initialization
5014 config
= config
|| {};
5016 // Parent constructor
5017 OO
.ui
.Toolbar
.super.call( this, config
);
5019 // Mixin constructors
5020 OO
.EventEmitter
.call( this );
5021 OO
.ui
.GroupElement
.call( this, config
);
5024 this.toolFactory
= toolFactory
;
5025 this.toolGroupFactory
= toolGroupFactory
;
5028 this.$bar
= this.$( '<div>' );
5029 this.$actions
= this.$( '<div>' );
5030 this.initialized
= false;
5034 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5035 .on( 'mousedown touchstart', OO
.ui
.bind( this.onPointerDown
, this ) );
5038 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5039 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
5040 if ( config
.actions
) {
5041 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
5042 this.$bar
.append( this.$actions
);
5044 this.$bar
.append( '<div style="clear:both"></div>' );
5045 if ( config
.shadow
) {
5046 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5048 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5053 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5054 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5055 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5060 * Get the tool factory.
5062 * @return {OO.ui.ToolFactory} Tool factory
5064 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5065 return this.toolFactory
;
5069 * Get the tool group factory.
5071 * @return {OO.Factory} Tool group factory
5073 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5074 return this.toolGroupFactory
;
5078 * Handles mouse down events.
5080 * @param {jQuery.Event} e Mouse down event
5082 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5083 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5084 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5085 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5091 * Sets up handles and preloads required information for the toolbar to work.
5092 * This must be called immediately after it is attached to a visible document.
5094 OO
.ui
.Toolbar
.prototype.initialize = function () {
5095 this.initialized
= true;
5101 * Tools can be specified in the following ways:
5103 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5104 * - All tools in a group: `{ group: 'group-name' }`
5105 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5107 * @param {Object.<string,Array>} groups List of tool group configurations
5108 * @param {Array|string} [groups.include] Tools to include
5109 * @param {Array|string} [groups.exclude] Tools to exclude
5110 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5111 * @param {Array|string} [groups.demote] Tools to demote to the end
5113 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5114 var i
, len
, type
, group
,
5116 defaultType
= 'bar';
5118 // Cleanup previous groups
5121 // Build out new groups
5122 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5124 if ( group
.include
=== '*' ) {
5125 // Apply defaults to catch-all groups
5126 if ( group
.type
=== undefined ) {
5127 group
.type
= 'list';
5129 if ( group
.label
=== undefined ) {
5130 group
.label
= 'ooui-toolbar-more';
5133 // Check type has been registered
5134 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5136 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5139 this.addItems( items
);
5143 * Remove all tools and groups from the toolbar.
5145 OO
.ui
.Toolbar
.prototype.reset = function () {
5150 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5151 this.items
[i
].destroy();
5157 * Destroys toolbar, removing event handlers and DOM elements.
5159 * Call this whenever you are done using a toolbar.
5161 OO
.ui
.Toolbar
.prototype.destroy = function () {
5163 this.$element
.remove();
5167 * Check if tool has not been used yet.
5169 * @param {string} name Symbolic name of tool
5170 * @return {boolean} Tool is available
5172 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5173 return !this.tools
[name
];
5177 * Prevent tool from being used again.
5179 * @param {OO.ui.Tool} tool Tool to reserve
5181 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5182 this.tools
[tool
.getName()] = tool
;
5186 * Allow tool to be used again.
5188 * @param {OO.ui.Tool} tool Tool to release
5190 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5191 delete this.tools
[tool
.getName()];
5195 * Get accelerator label for tool.
5197 * This is a stub that should be overridden to provide access to accelerator information.
5199 * @param {string} name Symbolic name of tool
5200 * @return {string|undefined} Tool accelerator label if available
5202 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5207 * Collection of tools.
5209 * Tools can be specified in the following ways:
5211 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5212 * - All tools in a group: `{ group: 'group-name' }`
5213 * - All tools: `'*'`
5217 * @extends OO.ui.Widget
5218 * @mixins OO.ui.GroupElement
5221 * @param {OO.ui.Toolbar} toolbar
5222 * @param {Object} [config] Configuration options
5223 * @cfg {Array|string} [include=[]] List of tools to include
5224 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5225 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5226 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5228 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5229 // Configuration initialization
5230 config
= config
|| {};
5232 // Parent constructor
5233 OO
.ui
.ToolGroup
.super.call( this, config
);
5235 // Mixin constructors
5236 OO
.ui
.GroupElement
.call( this, config
);
5239 this.toolbar
= toolbar
;
5241 this.pressed
= null;
5242 this.autoDisabled
= false;
5243 this.include
= config
.include
|| [];
5244 this.exclude
= config
.exclude
|| [];
5245 this.promote
= config
.promote
|| [];
5246 this.demote
= config
.demote
|| [];
5247 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
5251 'mousedown touchstart': OO
.ui
.bind( this.onPointerDown
, this ),
5252 'mouseup touchend': OO
.ui
.bind( this.onPointerUp
, this ),
5253 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
5254 mouseout
: OO
.ui
.bind( this.onMouseOut
, this )
5256 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5257 this.aggregate( { disable
: 'itemDisable' } );
5258 this.connect( this, { itemDisable
: 'updateDisabled' } );
5261 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5263 .addClass( 'oo-ui-toolGroup' )
5264 .append( this.$group
);
5270 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5271 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5279 /* Static Properties */
5282 * Show labels in tooltips.
5286 * @property {boolean}
5288 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5291 * Show acceleration labels in tooltips.
5295 * @property {boolean}
5297 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5300 * Automatically disable the toolgroup when all tools are disabled
5304 * @property {boolean}
5306 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5313 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5314 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5320 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5321 var i
, item
, allDisabled
= true;
5323 if ( this.constructor.static.autoDisable
) {
5324 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5325 item
= this.items
[i
];
5326 if ( !item
.isDisabled() ) {
5327 allDisabled
= false;
5331 this.autoDisabled
= allDisabled
;
5333 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5337 * Handle mouse down events.
5339 * @param {jQuery.Event} e Mouse down event
5341 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5342 // e.which is 0 for touch events, 1 for left mouse button
5343 if ( !this.isDisabled() && e
.which
<= 1 ) {
5344 this.pressed
= this.getTargetTool( e
);
5345 if ( this.pressed
) {
5346 this.pressed
.setActive( true );
5347 this.getElementDocument().addEventListener(
5348 'mouseup', this.onCapturedMouseUpHandler
, true
5356 * Handle captured mouse up events.
5358 * @param {Event} e Mouse up event
5360 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5361 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5362 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5363 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5364 this.onPointerUp( e
);
5368 * Handle mouse up events.
5370 * @param {jQuery.Event} e Mouse up event
5372 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5373 var tool
= this.getTargetTool( e
);
5375 // e.which is 0 for touch events, 1 for left mouse button
5376 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5377 this.pressed
.onSelect();
5380 this.pressed
= null;
5385 * Handle mouse over events.
5387 * @param {jQuery.Event} e Mouse over event
5389 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5390 var tool
= this.getTargetTool( e
);
5392 if ( this.pressed
&& this.pressed
=== tool
) {
5393 this.pressed
.setActive( true );
5398 * Handle mouse out events.
5400 * @param {jQuery.Event} e Mouse out event
5402 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5403 var tool
= this.getTargetTool( e
);
5405 if ( this.pressed
&& this.pressed
=== tool
) {
5406 this.pressed
.setActive( false );
5411 * Get the closest tool to a jQuery.Event.
5413 * Only tool links are considered, which prevents other elements in the tool such as popups from
5414 * triggering tool group interactions.
5417 * @param {jQuery.Event} e
5418 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5420 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5422 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5424 if ( $item
.length
) {
5425 tool
= $item
.parent().data( 'oo-ui-tool' );
5428 return tool
&& !tool
.isDisabled() ? tool
: null;
5432 * Handle tool registry register events.
5434 * If a tool is registered after the group is created, we must repopulate the list to account for:
5436 * - a tool being added that may be included
5437 * - a tool already included being overridden
5439 * @param {string} name Symbolic name of tool
5441 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5446 * Get the toolbar this group is in.
5448 * @return {OO.ui.Toolbar} Toolbar of group
5450 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5451 return this.toolbar
;
5455 * Add and remove tools based on configuration.
5457 OO
.ui
.ToolGroup
.prototype.populate = function () {
5458 var i
, len
, name
, tool
,
5459 toolFactory
= this.toolbar
.getToolFactory(),
5463 list
= this.toolbar
.getToolFactory().getTools(
5464 this.include
, this.exclude
, this.promote
, this.demote
5467 // Build a list of needed tools
5468 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5472 toolFactory
.lookup( name
) &&
5473 // Tool is available or is already in this group
5474 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5476 tool
= this.tools
[name
];
5478 // Auto-initialize tools on first use
5479 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5482 this.toolbar
.reserveTool( tool
);
5487 // Remove tools that are no longer needed
5488 for ( name
in this.tools
) {
5489 if ( !names
[name
] ) {
5490 this.tools
[name
].destroy();
5491 this.toolbar
.releaseTool( this.tools
[name
] );
5492 remove
.push( this.tools
[name
] );
5493 delete this.tools
[name
];
5496 if ( remove
.length
) {
5497 this.removeItems( remove
);
5499 // Update emptiness state
5501 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5503 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5505 // Re-add tools (moving existing ones to new locations)
5506 this.addItems( add
);
5507 // Disabled state may depend on items
5508 this.updateDisabled();
5512 * Destroy tool group.
5514 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5518 this.toolbar
.getToolFactory().disconnect( this );
5519 for ( name
in this.tools
) {
5520 this.toolbar
.releaseTool( this.tools
[name
] );
5521 this.tools
[name
].disconnect( this ).destroy();
5522 delete this.tools
[name
];
5524 this.$element
.remove();
5528 * Dialog for showing a message.
5531 * - Registers two actions by default (safe and primary).
5532 * - Renders action widgets in the footer.
5535 * @extends OO.ui.Dialog
5538 * @param {Object} [config] Configuration options
5540 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5541 // Parent constructor
5542 OO
.ui
.MessageDialog
.super.call( this, config
);
5545 this.verticalActionLayout
= null;
5548 this.$element
.addClass( 'oo-ui-messageDialog' );
5553 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5555 /* Static Properties */
5557 OO
.ui
.MessageDialog
.static.name
= 'message';
5559 OO
.ui
.MessageDialog
.static.size
= 'small';
5561 OO
.ui
.MessageDialog
.static.verbose
= false;
5566 * A confirmation dialog's title should describe what the progressive action will do. An alert
5567 * dialog's title should describe what event occured.
5571 * @property {jQuery|string|Function|null}
5573 OO
.ui
.MessageDialog
.static.title
= null;
5576 * A confirmation dialog's message should describe the consequences of the progressive action. An
5577 * alert dialog's message should describe why the event occured.
5581 * @property {jQuery|string|Function|null}
5583 OO
.ui
.MessageDialog
.static.message
= null;
5585 OO
.ui
.MessageDialog
.static.actions
= [
5586 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5587 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5595 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5597 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5601 * Toggle action layout between vertical and horizontal.
5603 * @param {boolean} [value] Layout actions vertically, omit to toggle
5606 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5607 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5609 if ( value
!== this.verticalActionLayout
) {
5610 this.verticalActionLayout
= value
;
5612 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5613 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5622 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5624 return new OO
.ui
.Process( function () {
5625 this.close( { action
: action
} );
5628 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5634 * @param {Object} [data] Dialog opening data
5635 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5636 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5637 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5638 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5641 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5645 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5646 .next( function () {
5647 this.title
.setLabel(
5648 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5650 this.message
.setLabel(
5651 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5653 this.message
.$element
.toggleClass(
5654 'oo-ui-messageDialog-message-verbose',
5655 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5663 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5664 return Math
.round( this.text
.$element
.outerHeight( true ) );
5670 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5672 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5675 this.$actions
= this.$( '<div>' );
5676 this.container
= new OO
.ui
.PanelLayout( {
5677 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5679 this.text
= new OO
.ui
.PanelLayout( {
5680 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5682 this.message
= new OO
.ui
.LabelWidget( {
5683 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5687 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5688 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5689 this.container
.$element
.append( this.text
.$element
);
5690 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5691 this.$body
.append( this.container
.$element
);
5692 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5693 this.$foot
.append( this.$actions
);
5699 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5700 var i
, len
, other
, special
, others
;
5703 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5705 special
= this.actions
.getSpecial();
5706 others
= this.actions
.getOthers();
5707 if ( special
.safe
) {
5708 this.$actions
.append( special
.safe
.$element
);
5709 special
.safe
.toggleFramed( false );
5711 if ( others
.length
) {
5712 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5714 this.$actions
.append( other
.$element
);
5715 other
.toggleFramed( false );
5718 if ( special
.primary
) {
5719 this.$actions
.append( special
.primary
.$element
);
5720 special
.primary
.toggleFramed( false );
5724 if ( !this.isOpening() ) {
5725 this.manager
.updateWindowSize( this );
5727 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5731 * Fit action actions into columns or rows.
5733 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5735 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5737 actions
= this.actions
.get();
5740 this.toggleVerticalActionLayout( false );
5741 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5742 action
= actions
[i
];
5743 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5744 this.toggleVerticalActionLayout( true );
5751 * Navigation dialog window.
5754 * - Show and hide errors.
5755 * - Retry an action.
5758 * - Renders header with dialog title and one action widget on either side
5759 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5760 * which close the dialog).
5761 * - Displays any action widgets in the footer (none by default).
5762 * - Ability to dismiss errors.
5764 * Subclass responsibilities:
5765 * - Register a 'safe' action.
5766 * - Register a 'primary' action.
5767 * - Add content to the dialog.
5771 * @extends OO.ui.Dialog
5774 * @param {Object} [config] Configuration options
5776 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
5777 // Parent constructor
5778 OO
.ui
.ProcessDialog
.super.call( this, config
);
5781 this.$element
.addClass( 'oo-ui-processDialog' );
5786 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
5791 * Handle dismiss button click events.
5795 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
5800 * Handle retry button click events.
5802 * Hides errors and then tries again.
5804 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
5806 this.executeAction( this.currentAction
.getAction() );
5812 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
5813 if ( this.actions
.isSpecial( action
) ) {
5816 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5822 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
5824 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
5827 this.$navigation
= this.$( '<div>' );
5828 this.$location
= this.$( '<div>' );
5829 this.$safeActions
= this.$( '<div>' );
5830 this.$primaryActions
= this.$( '<div>' );
5831 this.$otherActions
= this.$( '<div>' );
5832 this.dismissButton
= new OO
.ui
.ButtonWidget( {
5834 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
5836 this.retryButton
= new OO
.ui
.ButtonWidget( {
5838 label
: OO
.ui
.msg( 'ooui-dialog-process-retry' )
5840 this.$errors
= this.$( '<div>' );
5841 this.$errorsTitle
= this.$( '<div>' );
5844 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
5845 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
5848 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
5850 .append( this.title
.$element
)
5851 .addClass( 'oo-ui-processDialog-location' );
5852 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
5853 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
5854 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
5856 .addClass( 'oo-ui-processDialog-errors-title' )
5857 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
5859 .addClass( 'oo-ui-processDialog-errors' )
5860 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
5862 .addClass( 'oo-ui-processDialog-content' )
5863 .append( this.$errors
);
5865 .addClass( 'oo-ui-processDialog-navigation' )
5866 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
5867 this.$head
.append( this.$navigation
);
5868 this.$foot
.append( this.$otherActions
);
5874 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
5875 var i
, len
, other
, special
, others
;
5878 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
5880 special
= this.actions
.getSpecial();
5881 others
= this.actions
.getOthers();
5882 if ( special
.primary
) {
5883 this.$primaryActions
.append( special
.primary
.$element
);
5884 special
.primary
.toggleFramed( true );
5886 if ( others
.length
) {
5887 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5889 this.$otherActions
.append( other
.$element
);
5890 other
.toggleFramed( true );
5893 if ( special
.safe
) {
5894 this.$safeActions
.append( special
.safe
.$element
);
5895 special
.safe
.toggleFramed( true );
5899 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5905 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
5906 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
5907 .fail( OO
.ui
.bind( this.showErrors
, this ) );
5911 * Fit label between actions.
5915 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
5916 var width
= Math
.max(
5917 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
5918 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
5920 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
5926 * Handle errors that occured durring accept or reject processes.
5928 * @param {OO.ui.Error[]} errors Errors to be handled
5930 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
5935 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
5936 if ( !errors
[i
].isRecoverable() ) {
5937 recoverable
= false;
5939 $item
= this.$( '<div>' )
5940 .addClass( 'oo-ui-processDialog-error' )
5941 .append( errors
[i
].getMessage() );
5942 items
.push( $item
[0] );
5944 this.$errorItems
= this.$( items
);
5945 if ( recoverable
) {
5946 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
5948 this.currentAction
.setDisabled( true );
5950 this.retryButton
.toggle( recoverable
);
5951 this.$errorsTitle
.after( this.$errorItems
);
5952 this.$errors
.show().scrollTop( 0 );
5958 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
5959 this.$errors
.hide();
5960 this.$errorItems
.remove();
5961 this.$errorItems
= null;
5965 * Layout containing a series of pages.
5968 * @extends OO.ui.Layout
5971 * @param {Object} [config] Configuration options
5972 * @cfg {boolean} [continuous=false] Show all pages, one after another
5973 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
5974 * @cfg {boolean} [outlined=false] Show an outline
5975 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
5977 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
5978 // Initialize configuration
5979 config
= config
|| {};
5981 // Parent constructor
5982 OO
.ui
.BookletLayout
.super.call( this, config
);
5985 this.currentPageName
= null;
5987 this.ignoreFocus
= false;
5988 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
5989 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
5990 this.outlineVisible
= false;
5991 this.outlined
= !!config
.outlined
;
5992 if ( this.outlined
) {
5993 this.editable
= !!config
.editable
;
5994 this.outlineControlsWidget
= null;
5995 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
5996 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
5997 this.gridLayout
= new OO
.ui
.GridLayout(
5998 [ this.outlinePanel
, this.stackLayout
],
5999 { $: this.$, widths
: [ 1, 2 ] }
6001 this.outlineVisible
= true;
6002 if ( this.editable
) {
6003 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6004 this.outlineWidget
, { $: this.$ }
6010 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6011 if ( this.outlined
) {
6012 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
6014 if ( this.autoFocus
) {
6015 // Event 'focus' does not bubble, but 'focusin' does
6016 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
6020 this.$element
.addClass( 'oo-ui-bookletLayout' );
6021 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6022 if ( this.outlined
) {
6023 this.outlinePanel
.$element
6024 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6025 .append( this.outlineWidget
.$element
);
6026 if ( this.editable
) {
6027 this.outlinePanel
.$element
6028 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6029 .append( this.outlineControlsWidget
.$element
);
6031 this.$element
.append( this.gridLayout
.$element
);
6033 this.$element
.append( this.stackLayout
.$element
);
6039 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6045 * @param {OO.ui.PageLayout} page Current page
6050 * @param {OO.ui.PageLayout[]} page Added pages
6051 * @param {number} index Index pages were added at
6056 * @param {OO.ui.PageLayout[]} pages Removed pages
6062 * Handle stack layout focus.
6064 * @param {jQuery.Event} e Focusin event
6066 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6069 // Find the page that an element was focused within
6070 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6071 for ( name
in this.pages
) {
6072 // Check for page match, exclude current page to find only page changes
6073 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6074 this.setPage( name
);
6081 * Handle stack layout set events.
6083 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6085 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6086 var $input
, layout
= this;
6088 page
.scrollElementIntoView( { complete: function () {
6089 if ( layout
.autoFocus
) {
6090 // Set focus to the first input if nothing on the page is focused yet
6091 if ( !page
.$element
.find( ':focus' ).length
) {
6092 $input
= page
.$element
.find( ':input:first' );
6093 if ( $input
.length
) {
6103 * Handle outline widget select events.
6105 * @param {OO.ui.OptionWidget|null} item Selected item
6107 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
6109 this.setPage( item
.getData() );
6114 * Check if booklet has an outline.
6118 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6119 return this.outlined
;
6123 * Check if booklet has editing controls.
6127 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6128 return this.editable
;
6132 * Check if booklet has a visible outline.
6136 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6137 return this.outlined
&& this.outlineVisible
;
6141 * Hide or show the outline.
6143 * @param {boolean} [show] Show outline, omit to invert current state
6146 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6147 if ( this.outlined
) {
6148 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6149 this.outlineVisible
= show
;
6150 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6157 * Get the outline widget.
6159 * @param {OO.ui.PageLayout} page Page to be selected
6160 * @return {OO.ui.PageLayout|null} Closest page to another
6162 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6163 var next
, prev
, level
,
6164 pages
= this.stackLayout
.getItems(),
6165 index
= $.inArray( page
, pages
);
6167 if ( index
!== -1 ) {
6168 next
= pages
[index
+ 1];
6169 prev
= pages
[index
- 1];
6170 // Prefer adjacent pages at the same level
6171 if ( this.outlined
) {
6172 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
6175 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
6181 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
6187 return prev
|| next
|| null;
6191 * Get the outline widget.
6193 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
6195 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6196 return this.outlineWidget
;
6200 * Get the outline controls widget. If the outline is not editable, null is returned.
6202 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6204 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6205 return this.outlineControlsWidget
;
6209 * Get a page by name.
6211 * @param {string} name Symbolic name of page
6212 * @return {OO.ui.PageLayout|undefined} Page, if found
6214 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6215 return this.pages
[name
];
6219 * Get the current page name.
6221 * @return {string|null} Current page name
6223 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6224 return this.currentPageName
;
6228 * Add a page to the layout.
6230 * When pages are added with the same names as existing pages, the existing pages will be
6231 * automatically removed before the new pages are added.
6233 * @param {OO.ui.PageLayout[]} pages Pages to add
6234 * @param {number} index Index to insert pages after
6238 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6239 var i
, len
, name
, page
, item
, currentIndex
,
6240 stackLayoutPages
= this.stackLayout
.getItems(),
6244 // Remove pages with same names
6245 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6247 name
= page
.getName();
6249 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6250 // Correct the insertion index
6251 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6252 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6255 remove
.push( this.pages
[name
] );
6258 if ( remove
.length
) {
6259 this.removePages( remove
);
6263 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6265 name
= page
.getName();
6266 this.pages
[page
.getName()] = page
;
6267 if ( this.outlined
) {
6268 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
6269 page
.setOutlineItem( item
);
6274 if ( this.outlined
&& items
.length
) {
6275 this.outlineWidget
.addItems( items
, index
);
6276 this.updateOutlineWidget();
6278 this.stackLayout
.addItems( pages
, index
);
6279 this.emit( 'add', pages
, index
);
6285 * Remove a page from the layout.
6290 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6291 var i
, len
, name
, page
,
6294 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6296 name
= page
.getName();
6297 delete this.pages
[name
];
6298 if ( this.outlined
) {
6299 items
.push( this.outlineWidget
.getItemFromData( name
) );
6300 page
.setOutlineItem( null );
6303 if ( this.outlined
&& items
.length
) {
6304 this.outlineWidget
.removeItems( items
);
6305 this.updateOutlineWidget();
6307 this.stackLayout
.removeItems( pages
);
6308 this.emit( 'remove', pages
);
6314 * Clear all pages from the layout.
6319 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6321 pages
= this.stackLayout
.getItems();
6324 this.currentPageName
= null;
6325 if ( this.outlined
) {
6326 this.outlineWidget
.clearItems();
6327 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6328 pages
[i
].setOutlineItem( null );
6331 this.stackLayout
.clearItems();
6333 this.emit( 'remove', pages
);
6339 * Set the current page by name.
6342 * @param {string} name Symbolic name of page
6344 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6347 page
= this.pages
[name
];
6349 if ( name
!== this.currentPageName
) {
6350 if ( this.outlined
) {
6351 selectedItem
= this.outlineWidget
.getSelectedItem();
6352 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6353 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
6357 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6358 this.pages
[this.currentPageName
].setActive( false );
6359 // Blur anything focused if the next page doesn't have anything focusable - this
6360 // is not needed if the next page has something focusable because once it is focused
6361 // this blur happens automatically
6362 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6363 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6364 if ( $focused
.length
) {
6369 this.currentPageName
= name
;
6370 this.stackLayout
.setItem( page
);
6371 page
.setActive( true );
6372 this.emit( 'set', page
);
6378 * Call this after adding or removing items from the OutlineWidget.
6382 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
6383 // Auto-select first item when nothing is selected anymore
6384 if ( !this.outlineWidget
.getSelectedItem() ) {
6385 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
6392 * Layout made of a field and optional label.
6395 * @extends OO.ui.Layout
6396 * @mixins OO.ui.LabelElement
6398 * Available label alignment modes include:
6399 * - left: Label is before the field and aligned away from it, best for when the user will be
6400 * scanning for a specific label in a form with many fields
6401 * - right: Label is before the field and aligned toward it, best for forms the user is very
6402 * familiar with and will tab through field checking quickly to verify which field they are in
6403 * - top: Label is before the field and above it, best for when the use will need to fill out all
6404 * fields from top to bottom in a form with few fields
6405 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6406 * checkboxes or radio buttons
6409 * @param {OO.ui.Widget} field Field widget
6410 * @param {Object} [config] Configuration options
6411 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6412 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6414 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
6415 // Config initialization
6416 config
= $.extend( { align
: 'left' }, config
);
6418 // Parent constructor
6419 OO
.ui
.FieldLayout
.super.call( this, config
);
6421 // Mixin constructors
6422 OO
.ui
.LabelElement
.call( this, config
);
6425 this.$field
= this.$( '<div>' );
6428 if ( config
.help
) {
6429 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6436 this.popupButtonWidget
.getPopup().$body
.append( this.$( '<span>' ).text( config
.help
) );
6437 this.$help
= this.popupButtonWidget
.$element
;
6439 this.$help
= this.$( '<div>' );
6443 if ( this.field
instanceof OO
.ui
.InputWidget
) {
6444 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
6446 this.field
.connect( this, { disable
: 'onFieldDisable' } );
6449 this.$element
.addClass( 'oo-ui-fieldLayout' );
6451 .addClass( 'oo-ui-fieldLayout-field' )
6452 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
6453 .append( this.field
.$element
);
6454 this.setAlignment( config
.align
);
6459 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6460 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6465 * Handle field disable events.
6467 * @param {boolean} value Field is disabled
6469 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6470 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6474 * Handle label mouse click events.
6476 * @param {jQuery.Event} e Mouse click event
6478 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6479 this.field
.simulateLabelClick();
6486 * @return {OO.ui.Widget} Field widget
6488 OO
.ui
.FieldLayout
.prototype.getField = function () {
6493 * Set the field alignment mode.
6495 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6498 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6499 if ( value
!== this.align
) {
6500 // Default to 'left'
6501 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6505 if ( value
=== 'inline' ) {
6506 this.$element
.append( this.$field
, this.$label
, this.$help
);
6508 this.$element
.append( this.$help
, this.$label
, this.$field
);
6512 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6515 // The following classes can be used here:
6516 // oo-ui-fieldLayout-align-left
6517 // oo-ui-fieldLayout-align-right
6518 // oo-ui-fieldLayout-align-top
6519 // oo-ui-fieldLayout-align-inline
6520 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
6527 * Layout made of a fieldset and optional legend.
6529 * Just add OO.ui.FieldLayout items.
6532 * @extends OO.ui.Layout
6533 * @mixins OO.ui.LabelElement
6534 * @mixins OO.ui.IconElement
6535 * @mixins OO.ui.GroupElement
6538 * @param {Object} [config] Configuration options
6539 * @cfg {string} [icon] Symbolic icon name
6540 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6542 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6543 // Config initialization
6544 config
= config
|| {};
6546 // Parent constructor
6547 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6549 // Mixin constructors
6550 OO
.ui
.IconElement
.call( this, config
);
6551 OO
.ui
.LabelElement
.call( this, config
);
6552 OO
.ui
.GroupElement
.call( this, config
);
6556 .addClass( 'oo-ui-fieldsetLayout' )
6557 .prepend( this.$icon
, this.$label
, this.$group
);
6558 if ( $.isArray( config
.items
) ) {
6559 this.addItems( config
.items
);
6565 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6566 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6567 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6568 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6570 /* Static Properties */
6572 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
6575 * Layout with an HTML form.
6578 * @extends OO.ui.Layout
6581 * @param {Object} [config] Configuration options
6583 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6584 // Configuration initialization
6585 config
= config
|| {};
6587 // Parent constructor
6588 OO
.ui
.FormLayout
.super.call( this, config
);
6591 this.$element
.on( 'submit', OO
.ui
.bind( this.onFormSubmit
, this ) );
6594 this.$element
.addClass( 'oo-ui-formLayout' );
6599 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6607 /* Static Properties */
6609 OO
.ui
.FormLayout
.static.tagName
= 'form';
6614 * Handle form submit events.
6616 * @param {jQuery.Event} e Submit event
6619 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6620 this.emit( 'submit' );
6625 * Layout made of proportionally sized columns and rows.
6628 * @extends OO.ui.Layout
6631 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6632 * @param {Object} [config] Configuration options
6633 * @cfg {number[]} [widths] Widths of columns as ratios
6634 * @cfg {number[]} [heights] Heights of columns as ratios
6636 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6639 // Config initialization
6640 config
= config
|| {};
6642 // Parent constructor
6643 OO
.ui
.GridLayout
.super.call( this, config
);
6651 this.$element
.addClass( 'oo-ui-gridLayout' );
6652 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6653 this.panels
.push( panels
[i
] );
6654 this.$element
.append( panels
[i
].$element
);
6656 if ( config
.widths
|| config
.heights
) {
6657 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6659 // Arrange in columns by default
6661 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
6664 this.layout( widths
, [ 1 ] );
6670 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6682 /* Static Properties */
6684 OO
.ui
.GridLayout
.static.tagName
= 'div';
6689 * Set grid dimensions.
6691 * @param {number[]} widths Widths of columns as ratios
6692 * @param {number[]} heights Heights of rows as ratios
6694 * @throws {Error} If grid is not large enough to fit all panels
6696 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6700 cols
= widths
.length
,
6701 rows
= heights
.length
;
6703 // Verify grid is big enough to fit panels
6704 if ( cols
* rows
< this.panels
.length
) {
6705 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6708 // Sum up denominators
6709 for ( x
= 0; x
< cols
; x
++ ) {
6712 for ( y
= 0; y
< rows
; y
++ ) {
6718 for ( x
= 0; x
< cols
; x
++ ) {
6719 this.widths
[x
] = widths
[x
] / xd
;
6721 for ( y
= 0; y
< rows
; y
++ ) {
6722 this.heights
[y
] = heights
[y
] / yd
;
6726 this.emit( 'layout' );
6730 * Update panel positions and sizes.
6734 OO
.ui
.GridLayout
.prototype.update = function () {
6742 cols
= this.widths
.length
,
6743 rows
= this.heights
.length
;
6745 for ( y
= 0; y
< rows
; y
++ ) {
6746 height
= this.heights
[y
];
6747 for ( x
= 0; x
< cols
; x
++ ) {
6748 panel
= this.panels
[i
];
6749 width
= this.widths
[x
];
6751 width
: Math
.round( width
* 100 ) + '%',
6752 height
: Math
.round( height
* 100 ) + '%',
6753 top
: Math
.round( top
* 100 ) + '%',
6754 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6755 visibility
: width
=== 0 || height
=== 0 ? 'hidden' : ''
6758 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6759 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6761 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6763 panel
.$element
.css( dimensions
);
6771 this.emit( 'update' );
6775 * Get a panel at a given position.
6777 * The x and y position is affected by the current grid layout.
6779 * @param {number} x Horizontal position
6780 * @param {number} y Vertical position
6781 * @return {OO.ui.PanelLayout} The panel at the given postion
6783 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
6784 return this.panels
[( x
* this.widths
.length
) + y
];
6788 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
6791 * @extends OO.ui.Layout
6794 * @param {Object} [config] Configuration options
6795 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
6796 * @cfg {boolean} [padded=false] Pad the content from the edges
6797 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
6799 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
6800 // Config initialization
6801 config
= config
|| {};
6803 // Parent constructor
6804 OO
.ui
.PanelLayout
.super.call( this, config
);
6807 this.$element
.addClass( 'oo-ui-panelLayout' );
6808 if ( config
.scrollable
) {
6809 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
6812 if ( config
.padded
) {
6813 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
6816 if ( config
.expanded
=== undefined || config
.expanded
) {
6817 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
6823 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
6826 * Page within an booklet layout.
6829 * @extends OO.ui.PanelLayout
6832 * @param {string} name Unique symbolic name of page
6833 * @param {Object} [config] Configuration options
6834 * @param {string} [outlineItem] Outline item widget
6836 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
6837 // Configuration initialization
6838 config
= $.extend( { scrollable
: true }, config
);
6840 // Parent constructor
6841 OO
.ui
.PageLayout
.super.call( this, config
);
6845 this.outlineItem
= config
.outlineItem
|| null;
6846 this.active
= false;
6849 this.$element
.addClass( 'oo-ui-pageLayout' );
6854 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
6860 * @param {boolean} active Page is active
6868 * @return {string} Symbolic name of page
6870 OO
.ui
.PageLayout
.prototype.getName = function () {
6875 * Check if page is active.
6877 * @return {boolean} Page is active
6879 OO
.ui
.PageLayout
.prototype.isActive = function () {
6886 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
6888 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
6889 return this.outlineItem
;
6895 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
6896 * outline item as desired; this method is called for setting (with an object) and unsetting
6897 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
6898 * operating on null instead of an OO.ui.OutlineItemWidget object.
6900 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
6903 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
6904 this.outlineItem
= outlineItem
|| null;
6905 if ( outlineItem
) {
6906 this.setupOutlineItem();
6912 * Setup outline item.
6914 * @localdoc Subclasses should override this method to adjust the outline item as desired.
6916 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
6919 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
6924 * Set page active state.
6926 * @param {boolean} Page is active
6929 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
6932 if ( active
!== this.active
) {
6933 this.active
= active
;
6934 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
6935 this.emit( 'active', this.active
);
6940 * Layout containing a series of mutually exclusive pages.
6943 * @extends OO.ui.PanelLayout
6944 * @mixins OO.ui.GroupElement
6947 * @param {Object} [config] Configuration options
6948 * @cfg {boolean} [continuous=false] Show all pages, one after another
6949 * @cfg {string} [icon=''] Symbolic icon name
6950 * @cfg {OO.ui.Layout[]} [items] Layouts to add
6952 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
6953 // Config initialization
6954 config
= $.extend( { scrollable
: true }, config
);
6956 // Parent constructor
6957 OO
.ui
.StackLayout
.super.call( this, config
);
6959 // Mixin constructors
6960 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
6963 this.currentItem
= null;
6964 this.continuous
= !!config
.continuous
;
6967 this.$element
.addClass( 'oo-ui-stackLayout' );
6968 if ( this.continuous
) {
6969 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
6971 if ( $.isArray( config
.items
) ) {
6972 this.addItems( config
.items
);
6978 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
6979 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
6985 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
6991 * Get the current item.
6993 * @return {OO.ui.Layout|null}
6995 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
6996 return this.currentItem
;
7000 * Unset the current item.
7003 * @param {OO.ui.StackLayout} layout
7006 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7007 var prevItem
= this.currentItem
;
7008 if ( prevItem
=== null ) {
7012 this.currentItem
= null;
7013 this.emit( 'set', null );
7019 * Adding an existing item (by value) will move it.
7021 * @param {OO.ui.Layout[]} items Items to add
7022 * @param {number} [index] Index to insert items after
7025 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7027 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7029 if ( !this.currentItem
&& items
.length
) {
7030 this.setItem( items
[0] );
7039 * Items will be detached, not removed, so they can be used later.
7041 * @param {OO.ui.Layout[]} items Items to remove
7045 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7047 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7049 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7050 if ( this.items
.length
) {
7051 this.setItem( this.items
[0] );
7053 this.unsetCurrentItem();
7063 * Items will be detached, not removed, so they can be used later.
7068 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7069 this.unsetCurrentItem();
7070 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7078 * Any currently shown item will be hidden.
7080 * FIXME: If the passed item to show has not been added in the items list, then
7081 * this method drops it and unsets the current item.
7083 * @param {OO.ui.Layout} item Item to show
7087 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7090 if ( item
!== this.currentItem
) {
7091 if ( !this.continuous
) {
7092 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7093 this.items
[i
].$element
.css( 'display', '' );
7096 if ( $.inArray( item
, this.items
) !== -1 ) {
7097 if ( !this.continuous
) {
7098 item
.$element
.css( 'display', 'block' );
7100 this.currentItem
= item
;
7101 this.emit( 'set', item
);
7103 this.unsetCurrentItem();
7111 * Horizontal bar layout of tools as icon buttons.
7114 * @extends OO.ui.ToolGroup
7117 * @param {OO.ui.Toolbar} toolbar
7118 * @param {Object} [config] Configuration options
7120 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7121 // Parent constructor
7122 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7125 this.$element
.addClass( 'oo-ui-barToolGroup' );
7130 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7132 /* Static Properties */
7134 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7136 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7138 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7141 * Popup list of tools with an icon and optional label.
7145 * @extends OO.ui.ToolGroup
7146 * @mixins OO.ui.IconElement
7147 * @mixins OO.ui.IndicatorElement
7148 * @mixins OO.ui.LabelElement
7149 * @mixins OO.ui.TitledElement
7150 * @mixins OO.ui.ClippableElement
7153 * @param {OO.ui.Toolbar} toolbar
7154 * @param {Object} [config] Configuration options
7155 * @cfg {string} [header] Text to display at the top of the pop-up
7157 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7158 // Configuration initialization
7159 config
= config
|| {};
7161 // Parent constructor
7162 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7164 // Mixin constructors
7165 OO
.ui
.IconElement
.call( this, config
);
7166 OO
.ui
.IndicatorElement
.call( this, config
);
7167 OO
.ui
.LabelElement
.call( this, config
);
7168 OO
.ui
.TitledElement
.call( this, config
);
7169 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7172 this.active
= false;
7173 this.dragging
= false;
7174 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
7175 this.$handle
= this.$( '<span>' );
7179 'mousedown touchstart': OO
.ui
.bind( this.onHandlePointerDown
, this ),
7180 'mouseup touchend': OO
.ui
.bind( this.onHandlePointerUp
, this )
7185 .addClass( 'oo-ui-popupToolGroup-handle' )
7186 .append( this.$icon
, this.$label
, this.$indicator
);
7187 // If the pop-up should have a header, add it to the top of the toolGroup.
7188 // Note: If this feature is useful for other widgets, we could abstract it into an
7189 // OO.ui.HeaderedElement mixin constructor.
7190 if ( config
.header
!== undefined ) {
7192 .prepend( this.$( '<span>' )
7193 .addClass( 'oo-ui-popupToolGroup-header' )
7194 .text( config
.header
)
7198 .addClass( 'oo-ui-popupToolGroup' )
7199 .prepend( this.$handle
);
7204 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7205 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7206 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7207 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7208 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7209 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7211 /* Static Properties */
7218 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7220 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7222 if ( this.isDisabled() && this.isElementAttached() ) {
7223 this.setActive( false );
7228 * Handle focus being lost.
7230 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7232 * @param {jQuery.Event} e Mouse up event
7234 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7235 // Only deactivate when clicking outside the dropdown element
7236 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7237 this.setActive( false );
7244 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7245 // e.which is 0 for touch events, 1 for left mouse button
7246 if ( !this.isDisabled() && e
.which
<= 1 ) {
7247 this.setActive( false );
7249 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7253 * Handle mouse up events.
7255 * @param {jQuery.Event} e Mouse up event
7257 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7262 * Handle mouse down events.
7264 * @param {jQuery.Event} e Mouse down event
7266 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7267 // e.which is 0 for touch events, 1 for left mouse button
7268 if ( !this.isDisabled() && e
.which
<= 1 ) {
7269 this.setActive( !this.active
);
7275 * Switch into active mode.
7277 * When active, mouseup events anywhere in the document will trigger deactivation.
7279 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7281 if ( this.active
!== value
) {
7282 this.active
= value
;
7284 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7286 // Try anchoring the popup to the left first
7287 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7288 this.toggleClipping( true );
7289 if ( this.isClippedHorizontally() ) {
7290 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7291 this.toggleClipping( false );
7293 .removeClass( 'oo-ui-popupToolGroup-left' )
7294 .addClass( 'oo-ui-popupToolGroup-right' );
7295 this.toggleClipping( true );
7298 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7299 this.$element
.removeClass(
7300 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7302 this.toggleClipping( false );
7308 * Drop down list layout of tools as labeled icon buttons.
7311 * @extends OO.ui.PopupToolGroup
7314 * @param {OO.ui.Toolbar} toolbar
7315 * @param {Object} [config] Configuration options
7317 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7318 // Parent constructor
7319 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7322 this.$element
.addClass( 'oo-ui-listToolGroup' );
7327 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7329 /* Static Properties */
7331 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7333 OO
.ui
.ListToolGroup
.static.name
= 'list';
7336 * Drop down menu layout of tools as selectable menu items.
7339 * @extends OO.ui.PopupToolGroup
7342 * @param {OO.ui.Toolbar} toolbar
7343 * @param {Object} [config] Configuration options
7345 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7346 // Configuration initialization
7347 config
= config
|| {};
7349 // Parent constructor
7350 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7353 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7356 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7361 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7363 /* Static Properties */
7365 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7367 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7372 * Handle the toolbar state being updated.
7374 * When the state changes, the title of each active item in the menu will be joined together and
7375 * used as a label for the group. The label will be empty if none of the items are active.
7377 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7381 for ( name
in this.tools
) {
7382 if ( this.tools
[name
].isActive() ) {
7383 labelTexts
.push( this.tools
[name
].getTitle() );
7387 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7391 * Tool that shows a popup when selected.
7395 * @extends OO.ui.Tool
7396 * @mixins OO.ui.PopupElement
7399 * @param {OO.ui.Toolbar} toolbar
7400 * @param {Object} [config] Configuration options
7402 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7403 // Parent constructor
7404 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7406 // Mixin constructors
7407 OO
.ui
.PopupElement
.call( this, config
);
7411 .addClass( 'oo-ui-popupTool' )
7412 .append( this.popup
.$element
);
7417 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7418 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7423 * Handle the tool being selected.
7427 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7428 if ( !this.isDisabled() ) {
7429 this.popup
.toggle();
7431 this.setActive( false );
7436 * Handle the toolbar state being updated.
7440 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7441 this.setActive( false );
7445 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7447 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7451 * @extends OO.ui.GroupElement
7454 * @param {Object} [config] Configuration options
7456 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7457 // Parent constructor
7458 OO
.ui
.GroupWidget
.super.call( this, config
);
7463 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7468 * Set the disabled state of the widget.
7470 * This will also update the disabled state of child widgets.
7472 * @param {boolean} disabled Disable widget
7475 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7479 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7480 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7482 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7484 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7485 this.items
[i
].updateDisabled();
7493 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7495 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7496 * allows bidrectional communication.
7498 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7505 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7512 * Check if widget is disabled.
7514 * Checks parent if present, making disabled state inheritable.
7516 * @return {boolean} Widget is disabled
7518 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7519 return this.disabled
||
7520 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7524 * Set group element is in.
7526 * @param {OO.ui.GroupElement|null} group Group element, null if none
7529 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7531 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7532 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7534 // Initialize item disabled states
7535 this.updateDisabled();
7541 * Mixin that adds a menu showing suggested values for a text input.
7543 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7549 * @param {OO.ui.TextInputWidget} input Input widget
7550 * @param {Object} [config] Configuration options
7551 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
7553 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7554 // Config intialization
7555 config
= config
|| {};
7558 this.lookupInput
= input
;
7559 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
7560 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7561 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7562 input
: this.lookupInput
,
7563 $container
: config
.$container
7565 this.lookupCache
= {};
7566 this.lookupQuery
= null;
7567 this.lookupRequest
= null;
7568 this.populating
= false;
7571 this.$overlay
.append( this.lookupMenu
.$element
);
7573 this.lookupInput
.$input
.on( {
7574 focus
: OO
.ui
.bind( this.onLookupInputFocus
, this ),
7575 blur
: OO
.ui
.bind( this.onLookupInputBlur
, this ),
7576 mousedown
: OO
.ui
.bind( this.onLookupInputMouseDown
, this )
7578 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7581 this.$element
.addClass( 'oo-ui-lookupWidget' );
7582 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7588 * Handle input focus event.
7590 * @param {jQuery.Event} e Input focus event
7592 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7593 this.openLookupMenu();
7597 * Handle input blur event.
7599 * @param {jQuery.Event} e Input blur event
7601 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7602 this.lookupMenu
.toggle( false );
7606 * Handle input mouse down event.
7608 * @param {jQuery.Event} e Input mouse down event
7610 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7611 this.openLookupMenu();
7615 * Handle input change event.
7617 * @param {string} value New input value
7619 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7620 this.openLookupMenu();
7626 * @return {OO.ui.TextInputMenuWidget}
7628 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
7629 return this.lookupMenu
;
7637 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
7638 var value
= this.lookupInput
.getValue();
7640 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
7641 this.populateLookupMenu();
7642 this.lookupMenu
.toggle( true );
7653 * Populate lookup menu with current information.
7657 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
7660 if ( !this.populating
) {
7661 this.populating
= true;
7662 this.getLookupMenuItems()
7663 .done( function ( items
) {
7664 widget
.lookupMenu
.clearItems();
7665 if ( items
.length
) {
7669 widget
.initializeLookupMenuSelection();
7670 widget
.openLookupMenu();
7672 widget
.lookupMenu
.toggle( true );
7674 widget
.populating
= false;
7676 .fail( function () {
7677 widget
.lookupMenu
.clearItems();
7678 widget
.populating
= false;
7686 * Set selection in the lookup menu with current information.
7690 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
7691 if ( !this.lookupMenu
.getSelectedItem() ) {
7692 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
7694 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
7698 * Get lookup menu items for the current query.
7700 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
7703 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
7705 value
= this.lookupInput
.getValue(),
7706 deferred
= $.Deferred();
7708 if ( value
&& value
!== this.lookupQuery
) {
7709 // Abort current request if query has changed
7710 if ( this.lookupRequest
) {
7711 this.lookupRequest
.abort();
7712 this.lookupQuery
= null;
7713 this.lookupRequest
= null;
7715 if ( value
in this.lookupCache
) {
7716 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
7718 this.lookupQuery
= value
;
7719 this.lookupRequest
= this.getLookupRequest()
7720 .always( function () {
7721 widget
.lookupQuery
= null;
7722 widget
.lookupRequest
= null;
7724 .done( function ( data
) {
7725 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
7726 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
7728 .fail( function () {
7732 this.lookupRequest
.always( function () {
7733 widget
.popPending();
7737 return deferred
.promise();
7741 * Get a new request object of the current lookup query value.
7744 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
7746 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
7747 // Stub, implemented in subclass
7752 * Handle successful lookup request.
7754 * Overriding methods should call #populateLookupMenu when results are available and cache results
7755 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
7758 * @param {Mixed} data Response from server
7760 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
7761 // Stub, implemented in subclass
7765 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
7768 * @param {Mixed} data Cached result data, usually an array
7769 * @return {OO.ui.MenuItemWidget[]} Menu items
7771 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
7772 // Stub, implemented in subclass
7777 * Set of controls for an OO.ui.OutlineWidget.
7779 * Controls include moving items up and down, removing items, and adding different kinds of items.
7782 * @extends OO.ui.Widget
7783 * @mixins OO.ui.GroupElement
7784 * @mixins OO.ui.IconElement
7787 * @param {OO.ui.OutlineWidget} outline Outline to control
7788 * @param {Object} [config] Configuration options
7790 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7791 // Configuration initialization
7792 config
= $.extend( { icon
: 'add-item' }, config
);
7794 // Parent constructor
7795 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7797 // Mixin constructors
7798 OO
.ui
.GroupElement
.call( this, config
);
7799 OO
.ui
.IconElement
.call( this, config
);
7802 this.outline
= outline
;
7803 this.$movers
= this.$( '<div>' );
7804 this.upButton
= new OO
.ui
.ButtonWidget( {
7808 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
7810 this.downButton
= new OO
.ui
.ButtonWidget( {
7814 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
7816 this.removeButton
= new OO
.ui
.ButtonWidget( {
7820 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
7824 outline
.connect( this, {
7825 select
: 'onOutlineChange',
7826 add
: 'onOutlineChange',
7827 remove
: 'onOutlineChange'
7829 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
7830 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
7831 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
7834 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7835 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7837 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7838 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7839 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7844 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7845 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7846 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
7852 * @param {number} places Number of places to move
7862 * Handle outline change events.
7864 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7865 var i
, len
, firstMovable
, lastMovable
,
7866 items
= this.outline
.getItems(),
7867 selectedItem
= this.outline
.getSelectedItem(),
7868 movable
= selectedItem
&& selectedItem
.isMovable(),
7869 removable
= selectedItem
&& selectedItem
.isRemovable();
7874 while ( ++i
< len
) {
7875 if ( items
[i
].isMovable() ) {
7876 firstMovable
= items
[i
];
7882 if ( items
[i
].isMovable() ) {
7883 lastMovable
= items
[i
];
7888 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7889 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7890 this.removeButton
.setDisabled( !removable
);
7894 * Mixin for widgets with a boolean on/off state.
7900 * @param {Object} [config] Configuration options
7901 * @cfg {boolean} [value=false] Initial value
7903 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7904 // Configuration initialization
7905 config
= config
|| {};
7911 this.$element
.addClass( 'oo-ui-toggleWidget' );
7912 this.setValue( !!config
.value
);
7919 * @param {boolean} value Changed value
7925 * Get the value of the toggle.
7929 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7934 * Set the value of the toggle.
7936 * @param {boolean} value New value
7940 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7942 if ( this.value
!== value
) {
7944 this.emit( 'change', value
);
7945 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7946 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7952 * Group widget for multiple related buttons.
7954 * Use together with OO.ui.ButtonWidget.
7957 * @extends OO.ui.Widget
7958 * @mixins OO.ui.GroupElement
7961 * @param {Object} [config] Configuration options
7962 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
7964 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
7965 // Parent constructor
7966 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
7968 // Mixin constructors
7969 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7972 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
7973 if ( $.isArray( config
.items
) ) {
7974 this.addItems( config
.items
);
7980 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
7981 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
7984 * Generic widget for buttons.
7987 * @extends OO.ui.Widget
7988 * @mixins OO.ui.ButtonElement
7989 * @mixins OO.ui.IconElement
7990 * @mixins OO.ui.IndicatorElement
7991 * @mixins OO.ui.LabelElement
7992 * @mixins OO.ui.TitledElement
7993 * @mixins OO.ui.FlaggedElement
7996 * @param {Object} [config] Configuration options
7997 * @cfg {string} [href] Hyperlink to visit when clicked
7998 * @cfg {string} [target] Target to open hyperlink in
8000 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8001 // Configuration initialization
8002 config
= $.extend( { target
: '_blank' }, config
);
8004 // Parent constructor
8005 OO
.ui
.ButtonWidget
.super.call( this, config
);
8007 // Mixin constructors
8008 OO
.ui
.ButtonElement
.call( this, config
);
8009 OO
.ui
.IconElement
.call( this, config
);
8010 OO
.ui
.IndicatorElement
.call( this, config
);
8011 OO
.ui
.LabelElement
.call( this, config
);
8012 OO
.ui
.TitledElement
.call( this, config
, $.extend( {}, config
, { $titled
: this.$button
} ) );
8013 OO
.ui
.FlaggedElement
.call( this, config
);
8018 this.isHyperlink
= false;
8022 click
: OO
.ui
.bind( this.onClick
, this ),
8023 keypress
: OO
.ui
.bind( this.onKeyPress
, this )
8027 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8029 .addClass( 'oo-ui-buttonWidget' )
8030 .append( this.$button
);
8031 this.setHref( config
.href
);
8032 this.setTarget( config
.target
);
8037 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8038 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8039 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8040 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8041 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8042 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8043 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8054 * Handles mouse click events.
8056 * @param {jQuery.Event} e Mouse click event
8059 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8060 if ( !this.isDisabled() ) {
8061 this.emit( 'click' );
8062 if ( this.isHyperlink
) {
8070 * Handles keypress events.
8072 * @param {jQuery.Event} e Keypress event
8075 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8076 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8078 if ( this.isHyperlink
) {
8086 * Get hyperlink location.
8088 * @return {string} Hyperlink location
8090 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8095 * Get hyperlink target.
8097 * @return {string} Hyperlink target
8099 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8104 * Set hyperlink location.
8106 * @param {string|null} href Hyperlink location, null to remove
8108 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8109 href
= typeof href
=== 'string' ? href
: null;
8111 if ( href
!== this.href
) {
8113 if ( href
!== null ) {
8114 this.$button
.attr( 'href', href
);
8115 this.isHyperlink
= true;
8117 this.$button
.removeAttr( 'href' );
8118 this.isHyperlink
= false;
8126 * Set hyperlink target.
8128 * @param {string|null} target Hyperlink target, null to remove
8130 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8131 target
= typeof target
=== 'string' ? target
: null;
8133 if ( target
!== this.target
) {
8134 this.target
= target
;
8135 if ( target
!== null ) {
8136 this.$button
.attr( 'target', target
);
8138 this.$button
.removeAttr( 'target' );
8146 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8149 * @extends OO.ui.ButtonWidget
8152 * @param {Object} [config] Configuration options
8153 * @cfg {string} [action] Symbolic action name
8154 * @cfg {string[]} [modes] Symbolic mode names
8156 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8157 // Config intialization
8158 config
= $.extend( { framed
: false }, config
);
8160 // Parent constructor
8161 OO
.ui
.ActionWidget
.super.call( this, config
);
8164 this.action
= config
.action
|| '';
8165 this.modes
= config
.modes
|| [];
8170 this.$element
.addClass( 'oo-ui-actionWidget' );
8175 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8186 * Check if action is available in a certain mode.
8188 * @param {string} mode Name of mode
8189 * @return {boolean} Has mode
8191 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8192 return this.modes
.indexOf( mode
) !== -1;
8196 * Get symbolic action name.
8200 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8205 * Get symbolic action name.
8209 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8210 return this.modes
.slice();
8214 * Emit a resize event if the size has changed.
8218 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8221 if ( this.isElementAttached() ) {
8222 width
= this.$element
.width();
8223 height
= this.$element
.height();
8225 if ( width
!== this.width
|| height
!== this.height
) {
8227 this.height
= height
;
8228 this.emit( 'resize' );
8238 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8240 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8241 this.propagateResize();
8249 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8251 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8252 this.propagateResize();
8260 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8262 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8263 this.propagateResize();
8271 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8273 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8274 this.propagateResize();
8280 * Toggle visibility of button.
8282 * @param {boolean} [show] Show button, omit to toggle visibility
8285 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8287 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8288 this.propagateResize();
8294 * Button that shows and hides a popup.
8297 * @extends OO.ui.ButtonWidget
8298 * @mixins OO.ui.PopupElement
8301 * @param {Object} [config] Configuration options
8303 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8304 // Parent constructor
8305 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8307 // Mixin constructors
8308 OO
.ui
.PopupElement
.call( this, config
);
8312 .addClass( 'oo-ui-popupButtonWidget' )
8313 .append( this.popup
.$element
);
8318 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8319 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8324 * Handles mouse click events.
8326 * @param {jQuery.Event} e Mouse click event
8328 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8329 // Skip clicks within the popup
8330 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8334 if ( !this.isDisabled() ) {
8335 this.popup
.toggle();
8337 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8343 * Button that toggles on and off.
8346 * @extends OO.ui.ButtonWidget
8347 * @mixins OO.ui.ToggleWidget
8350 * @param {Object} [config] Configuration options
8351 * @cfg {boolean} [value=false] Initial value
8353 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8354 // Configuration initialization
8355 config
= config
|| {};
8357 // Parent constructor
8358 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8360 // Mixin constructors
8361 OO
.ui
.ToggleWidget
.call( this, config
);
8364 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8369 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8370 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8377 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8378 if ( !this.isDisabled() ) {
8379 this.setValue( !this.value
);
8383 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8389 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8391 if ( value
!== this.value
) {
8392 this.setActive( value
);
8395 // Parent method (from mixin)
8396 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8404 * See OO.ui.IconElement for more information.
8407 * @extends OO.ui.Widget
8408 * @mixins OO.ui.IconElement
8409 * @mixins OO.ui.TitledElement
8412 * @param {Object} [config] Configuration options
8414 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8415 // Config intialization
8416 config
= config
|| {};
8418 // Parent constructor
8419 OO
.ui
.IconWidget
.super.call( this, config
);
8421 // Mixin constructors
8422 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
8423 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8426 this.$element
.addClass( 'oo-ui-iconWidget' );
8431 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8432 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
8433 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8435 /* Static Properties */
8437 OO
.ui
.IconWidget
.static.tagName
= 'span';
8442 * See OO.ui.IndicatorElement for more information.
8445 * @extends OO.ui.Widget
8446 * @mixins OO.ui.IndicatorElement
8447 * @mixins OO.ui.TitledElement
8450 * @param {Object} [config] Configuration options
8452 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8453 // Config intialization
8454 config
= config
|| {};
8456 // Parent constructor
8457 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8459 // Mixin constructors
8460 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
8461 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8464 this.$element
.addClass( 'oo-ui-indicatorWidget' );
8469 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
8470 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
8471 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
8473 /* Static Properties */
8475 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
8478 * Inline menu of options.
8480 * Inline menus provide a control for accessing a menu and compose a menu within the widget, which
8481 * can be accessed using the #getMenu method.
8483 * Use with OO.ui.MenuOptionWidget.
8486 * @extends OO.ui.Widget
8487 * @mixins OO.ui.IconElement
8488 * @mixins OO.ui.IndicatorElement
8489 * @mixins OO.ui.LabelElement
8490 * @mixins OO.ui.TitledElement
8493 * @param {Object} [config] Configuration options
8494 * @cfg {Object} [menu] Configuration options to pass to menu widget
8496 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
8497 // Configuration initialization
8498 config
= $.extend( { indicator
: 'down' }, config
);
8500 // Parent constructor
8501 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
8503 // Mixin constructors
8504 OO
.ui
.IconElement
.call( this, config
);
8505 OO
.ui
.IndicatorElement
.call( this, config
);
8506 OO
.ui
.LabelElement
.call( this, config
);
8507 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8510 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8511 this.$handle
= this.$( '<span>' );
8514 this.$element
.on( { click
: OO
.ui
.bind( this.onClick
, this ) } );
8515 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8519 .addClass( 'oo-ui-inlineMenuWidget-handle' )
8520 .append( this.$icon
, this.$label
, this.$indicator
);
8522 .addClass( 'oo-ui-inlineMenuWidget' )
8523 .append( this.$handle
, this.menu
.$element
);
8528 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
8529 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconElement
);
8530 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatorElement
);
8531 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabelElement
);
8532 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
8539 * @return {OO.ui.MenuWidget} Menu of widget
8541 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
8546 * Handles menu select events.
8548 * @param {OO.ui.MenuItemWidget} item Selected menu item
8550 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
8557 selectedLabel
= item
.getLabel();
8559 // If the label is a DOM element, clone it, because setLabel will append() it
8560 if ( selectedLabel
instanceof jQuery
) {
8561 selectedLabel
= selectedLabel
.clone();
8564 this.setLabel( selectedLabel
);
8568 * Handles mouse click events.
8570 * @param {jQuery.Event} e Mouse click event
8572 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
8573 // Skip clicks within the menu
8574 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8578 if ( !this.isDisabled() ) {
8579 if ( this.menu
.isVisible() ) {
8580 this.menu
.toggle( false );
8582 this.menu
.toggle( true );
8589 * Base class for input widgets.
8593 * @extends OO.ui.Widget
8596 * @param {Object} [config] Configuration options
8597 * @cfg {string} [name=''] HTML input name
8598 * @cfg {string} [value=''] Input value
8599 * @cfg {boolean} [readOnly=false] Prevent changes
8600 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
8602 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8603 // Config intialization
8604 config
= $.extend( { readOnly
: false }, config
);
8606 // Parent constructor
8607 OO
.ui
.InputWidget
.super.call( this, config
);
8610 this.$input
= this.getInputElement( config
);
8612 this.readOnly
= false;
8613 this.inputFilter
= config
.inputFilter
;
8616 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
8620 .attr( 'name', config
.name
)
8621 .prop( 'disabled', this.isDisabled() );
8622 this.setReadOnly( config
.readOnly
);
8623 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
8624 this.setValue( config
.value
);
8629 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8641 * Get input element.
8643 * @param {Object} [config] Configuration options
8644 * @return {jQuery} Input element
8646 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8647 return this.$( '<input>' );
8651 * Handle potentially value-changing events.
8653 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8655 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8657 if ( !this.isDisabled() ) {
8658 // Allow the stack to clear so the value will be updated
8659 setTimeout( function () {
8660 widget
.setValue( widget
.$input
.val() );
8666 * Get the value of the input.
8668 * @return {string} Input value
8670 OO
.ui
.InputWidget
.prototype.getValue = function () {
8675 * Sets the direction of the current input, either RTL or LTR
8677 * @param {boolean} isRTL
8679 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
8681 this.$input
.removeClass( 'oo-ui-ltr' );
8682 this.$input
.addClass( 'oo-ui-rtl' );
8684 this.$input
.removeClass( 'oo-ui-rtl' );
8685 this.$input
.addClass( 'oo-ui-ltr' );
8690 * Set the value of the input.
8692 * @param {string} value New value
8696 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
8697 value
= this.sanitizeValue( value
);
8698 if ( this.value
!== value
) {
8700 this.emit( 'change', this.value
);
8702 // Update the DOM if it has changed. Note that with sanitizeValue, it
8703 // is possible for the DOM value to change without this.value changing.
8704 if ( this.$input
.val() !== this.value
) {
8705 this.$input
.val( this.value
);
8711 * Sanitize incoming value.
8713 * Ensures value is a string, and converts undefined and null to empty strings.
8715 * @param {string} value Original value
8716 * @return {string} Sanitized value
8718 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
8719 if ( value
=== undefined || value
=== null ) {
8721 } else if ( this.inputFilter
) {
8722 return this.inputFilter( String( value
) );
8724 return String( value
);
8729 * Simulate the behavior of clicking on a label bound to this input.
8731 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
8732 if ( !this.isDisabled() ) {
8733 if ( this.$input
.is( ':checkbox,:radio' ) ) {
8734 this.$input
.click();
8735 } else if ( this.$input
.is( ':input' ) ) {
8736 this.$input
[0].focus();
8742 * Check if the widget is read-only.
8746 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
8747 return this.readOnly
;
8751 * Set the read-only state of the widget.
8753 * This should probably change the widgets's appearance and prevent it from being used.
8755 * @param {boolean} state Make input read-only
8758 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
8759 this.readOnly
= !!state
;
8760 this.$input
.prop( 'readOnly', this.readOnly
);
8767 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
8768 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
8769 if ( this.$input
) {
8770 this.$input
.prop( 'disabled', this.isDisabled() );
8780 OO
.ui
.InputWidget
.prototype.focus = function () {
8781 this.$input
[0].focus();
8790 OO
.ui
.InputWidget
.prototype.blur = function () {
8791 this.$input
[0].blur();
8796 * Checkbox input widget.
8799 * @extends OO.ui.InputWidget
8802 * @param {Object} [config] Configuration options
8804 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
8805 // Parent constructor
8806 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
8809 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
8814 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
8821 * Get input element.
8823 * @return {jQuery} Input element
8825 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
8826 return this.$( '<input type="checkbox" />' );
8830 * Get checked state of the checkbox
8832 * @return {boolean} If the checkbox is checked
8834 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
8841 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
8843 if ( this.value
!== value
) {
8845 this.$input
.prop( 'checked', this.value
);
8846 this.emit( 'change', this.value
);
8853 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
8855 if ( !this.isDisabled() ) {
8856 // Allow the stack to clear so the value will be updated
8857 setTimeout( function () {
8858 widget
.setValue( widget
.$input
.prop( 'checked' ) );
8864 * Input widget with a text field.
8867 * @extends OO.ui.InputWidget
8868 * @mixins OO.ui.IconElement
8869 * @mixins OO.ui.IndicatorElement
8872 * @param {Object} [config] Configuration options
8873 * @cfg {string} [placeholder] Placeholder text
8874 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8875 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8876 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8878 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8879 // Configuration initialization
8880 config
= config
|| {};
8882 // Parent constructor
8883 OO
.ui
.TextInputWidget
.super.call( this, config
);
8885 // Mixin constructors
8886 OO
.ui
.IconElement
.call( this, config
);
8887 OO
.ui
.IndicatorElement
.call( this, config
);
8891 this.multiline
= !!config
.multiline
;
8892 this.autosize
= !!config
.autosize
;
8893 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
8896 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8897 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8898 this.$icon
.on( 'mousedown', OO
.ui
.bind( this.onIconMouseDown
, this ) );
8899 this.$indicator
.on( 'mousedown', OO
.ui
.bind( this.onIndicatorMouseDown
, this ) );
8903 .addClass( 'oo-ui-textInputWidget' )
8904 .append( this.$icon
, this.$indicator
);
8905 if ( config
.placeholder
) {
8906 this.$input
.attr( 'placeholder', config
.placeholder
);
8908 this.$element
.attr( 'role', 'textbox' );
8913 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8914 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
8915 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
8920 * User presses enter inside the text box.
8922 * Not called if input is multiline.
8928 * User clicks the icon.
8934 * User clicks the indicator.
8942 * Handle icon mouse down events.
8944 * @param {jQuery.Event} e Mouse down event
8947 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
8948 if ( e
.which
=== 1 ) {
8949 this.$input
[0].focus();
8950 this.emit( 'icon' );
8956 * Handle indicator mouse down events.
8958 * @param {jQuery.Event} e Mouse down event
8961 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
8962 if ( e
.which
=== 1 ) {
8963 this.$input
[0].focus();
8964 this.emit( 'indicator' );
8970 * Handle key press events.
8972 * @param {jQuery.Event} e Key press event
8973 * @fires enter If enter key is pressed and input is not multiline
8975 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8976 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8977 this.emit( 'enter' );
8982 * Handle element attach events.
8984 * @param {jQuery.Event} e Element attach event
8986 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8993 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8997 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9003 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9005 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9012 * Automatically adjust the size of the text input.
9014 * This only affects multi-line inputs that are auto-sized.
9018 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9019 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
9021 if ( this.multiline
&& this.autosize
) {
9022 $clone
= this.$input
.clone()
9023 .val( this.$input
.val() )
9024 .css( { height
: 0 } )
9025 .insertAfter( this.$input
);
9026 // Set inline height property to 0 to measure scroll height
9027 scrollHeight
= $clone
[0].scrollHeight
;
9028 // Remove inline height property to measure natural heights
9029 $clone
.css( 'height', '' );
9030 innerHeight
= $clone
.innerHeight();
9031 outerHeight
= $clone
.outerHeight();
9032 // Measure max rows height
9033 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
9034 maxInnerHeight
= $clone
.innerHeight();
9035 $clone
.removeAttr( 'rows' ).css( 'height', '' );
9037 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
9038 // Only apply inline height when expansion beyond natural height is needed
9041 // Use the difference between the inner and outer height as a buffer
9042 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
9049 * Get input element.
9051 * @param {Object} [config] Configuration options
9052 * @return {jQuery} Input element
9054 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9055 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
9061 * Check if input supports multiple lines.
9065 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9066 return !!this.multiline
;
9070 * Check if input automatically adjusts its size.
9074 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9075 return !!this.autosize
;
9079 * Check if input is pending.
9083 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
9084 return !!this.pending
;
9088 * Increase the pending stack.
9092 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
9093 if ( this.pending
=== 0 ) {
9094 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
9095 this.$input
.addClass( 'oo-ui-texture-pending' );
9103 * Reduce the pending stack.
9109 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
9110 if ( this.pending
=== 1 ) {
9111 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
9112 this.$input
.removeClass( 'oo-ui-texture-pending' );
9114 this.pending
= Math
.max( 0, this.pending
- 1 );
9120 * Select the contents of the input.
9124 OO
.ui
.TextInputWidget
.prototype.select = function () {
9125 this.$input
.select();
9130 * Text input with a menu of optional values.
9133 * @extends OO.ui.Widget
9136 * @param {Object} [config] Configuration options
9137 * @cfg {Object} [menu] Configuration options to pass to menu widget
9138 * @cfg {Object} [input] Configuration options to pass to input widget
9140 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9141 // Configuration initialization
9142 config
= config
|| {};
9144 // Parent constructor
9145 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9148 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9149 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9152 this.menu
= new OO
.ui
.MenuWidget( $.extend(
9153 { $: this.$, widget
: this, input
: this.input
, disabled
: this.isDisabled() },
9158 this.input
.connect( this, {
9159 change
: 'onInputChange',
9160 indicator
: 'onInputIndicator',
9161 enter
: 'onInputEnter'
9163 this.menu
.connect( this, {
9164 choose
: 'onMenuChoose',
9165 add
: 'onMenuItemsChange',
9166 remove
: 'onMenuItemsChange'
9170 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append(
9171 this.input
.$element
,
9174 this.onMenuItemsChange();
9179 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9184 * Handle input change events.
9186 * @param {string} value New value
9188 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9189 var match
= this.menu
.getItemFromData( value
);
9191 this.menu
.selectItem( match
);
9193 if ( !this.isDisabled() ) {
9194 this.menu
.toggle( true );
9199 * Handle input indicator events.
9201 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9202 if ( !this.isDisabled() ) {
9208 * Handle input enter events.
9210 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
9211 if ( !this.isDisabled() ) {
9212 this.menu
.toggle( false );
9217 * Handle menu choose events.
9219 * @param {OO.ui.OptionWidget} item Chosen item
9221 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
9223 this.input
.setValue( item
.getData() );
9228 * Handle menu item change events.
9230 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
9231 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
9237 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
9239 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
9242 this.input
.setDisabled( this.isDisabled() );
9245 this.menu
.setDisabled( this.isDisabled() );
9255 * @extends OO.ui.Widget
9256 * @mixins OO.ui.LabelElement
9259 * @param {Object} [config] Configuration options
9261 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
9262 // Config intialization
9263 config
= config
|| {};
9265 // Parent constructor
9266 OO
.ui
.LabelWidget
.super.call( this, config
);
9268 // Mixin constructors
9269 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
9272 this.input
= config
.input
;
9275 if ( this.input
instanceof OO
.ui
.InputWidget
) {
9276 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
9280 this.$element
.addClass( 'oo-ui-labelWidget' );
9285 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
9286 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
9288 /* Static Properties */
9290 OO
.ui
.LabelWidget
.static.tagName
= 'span';
9295 * Handles label mouse click events.
9297 * @param {jQuery.Event} e Mouse click event
9299 OO
.ui
.LabelWidget
.prototype.onClick = function () {
9300 this.input
.simulateLabelClick();
9305 * Generic option widget for use with OO.ui.SelectWidget.
9308 * @extends OO.ui.Widget
9309 * @mixins OO.ui.LabelElement
9310 * @mixins OO.ui.FlaggedElement
9313 * @param {Mixed} data Option data
9314 * @param {Object} [config] Configuration options
9315 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
9317 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
9318 // Config intialization
9319 config
= config
|| {};
9321 // Parent constructor
9322 OO
.ui
.OptionWidget
.super.call( this, config
);
9324 // Mixin constructors
9325 OO
.ui
.ItemWidget
.call( this );
9326 OO
.ui
.LabelElement
.call( this, config
);
9327 OO
.ui
.FlaggedElement
.call( this, config
);
9331 this.selected
= false;
9332 this.highlighted
= false;
9333 this.pressed
= false;
9337 .data( 'oo-ui-optionWidget', this )
9338 .attr( 'rel', config
.rel
)
9339 .attr( 'role', 'option' )
9340 .addClass( 'oo-ui-optionWidget' )
9341 .append( this.$label
);
9343 .prepend( this.$icon
)
9344 .append( this.$indicator
);
9349 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
9350 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
9351 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
9352 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
9354 /* Static Properties */
9356 OO
.ui
.OptionWidget
.static.selectable
= true;
9358 OO
.ui
.OptionWidget
.static.highlightable
= true;
9360 OO
.ui
.OptionWidget
.static.pressable
= true;
9362 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
9367 * Check if option can be selected.
9369 * @return {boolean} Item is selectable
9371 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
9372 return this.constructor.static.selectable
&& !this.isDisabled();
9376 * Check if option can be highlighted.
9378 * @return {boolean} Item is highlightable
9380 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
9381 return this.constructor.static.highlightable
&& !this.isDisabled();
9385 * Check if option can be pressed.
9387 * @return {boolean} Item is pressable
9389 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
9390 return this.constructor.static.pressable
&& !this.isDisabled();
9394 * Check if option is selected.
9396 * @return {boolean} Item is selected
9398 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
9399 return this.selected
;
9403 * Check if option is highlighted.
9405 * @return {boolean} Item is highlighted
9407 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
9408 return this.highlighted
;
9412 * Check if option is pressed.
9414 * @return {boolean} Item is pressed
9416 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
9417 return this.pressed
;
9421 * Set selected state.
9423 * @param {boolean} [state=false] Select option
9426 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
9427 if ( this.constructor.static.selectable
) {
9428 this.selected
= !!state
;
9429 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
9430 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
9431 this.scrollElementIntoView();
9438 * Set highlighted state.
9440 * @param {boolean} [state=false] Highlight option
9443 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
9444 if ( this.constructor.static.highlightable
) {
9445 this.highlighted
= !!state
;
9446 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
9452 * Set pressed state.
9454 * @param {boolean} [state=false] Press option
9457 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
9458 if ( this.constructor.static.pressable
) {
9459 this.pressed
= !!state
;
9460 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
9466 * Make the option's highlight flash.
9468 * While flashing, the visual style of the pressed state is removed if present.
9470 * @return {jQuery.Promise} Promise resolved when flashing is done
9472 OO
.ui
.OptionWidget
.prototype.flash = function () {
9474 $element
= this.$element
,
9475 deferred
= $.Deferred();
9477 if ( !this.isDisabled() && this.constructor.static.pressable
) {
9478 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
9479 setTimeout( function () {
9480 // Restore original classes
9482 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
9483 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
9485 setTimeout( function () {
9492 return deferred
.promise();
9498 * @return {Mixed} Option data
9500 OO
.ui
.OptionWidget
.prototype.getData = function () {
9505 * Option widget with an option icon and indicator.
9507 * Use together with OO.ui.SelectWidget.
9510 * @extends OO.ui.OptionWidget
9511 * @mixins OO.ui.IconElement
9512 * @mixins OO.ui.IndicatorElement
9515 * @param {Mixed} data Option data
9516 * @param {Object} [config] Configuration options
9518 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
9519 // Parent constructor
9520 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
9522 // Mixin constructors
9523 OO
.ui
.IconElement
.call( this, config
);
9524 OO
.ui
.IndicatorElement
.call( this, config
);
9528 .addClass( 'oo-ui-decoratedOptionWidget' )
9529 .prepend( this.$icon
)
9530 .append( this.$indicator
);
9535 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
9536 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
9537 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
9540 * Option widget that looks like a button.
9542 * Use together with OO.ui.ButtonSelectWidget.
9545 * @extends OO.ui.DecoratedOptionWidget
9546 * @mixins OO.ui.ButtonElement
9549 * @param {Mixed} data Option data
9550 * @param {Object} [config] Configuration options
9552 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
9553 // Parent constructor
9554 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
9556 // Mixin constructors
9557 OO
.ui
.ButtonElement
.call( this, config
);
9560 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
9561 this.$button
.append( this.$element
.contents() );
9562 this.$element
.append( this.$button
);
9567 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
9568 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
9570 /* Static Properties */
9572 // Allow button mouse down events to pass through so they can be handled by the parent select widget
9573 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
9580 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
9581 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
9583 if ( this.constructor.static.selectable
) {
9584 this.setActive( state
);
9591 * Item of an OO.ui.MenuWidget.
9594 * @extends OO.ui.DecoratedOptionWidget
9597 * @param {Mixed} data Item data
9598 * @param {Object} [config] Configuration options
9600 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
9601 // Configuration initialization
9602 config
= $.extend( { icon
: 'check' }, config
);
9604 // Parent constructor
9605 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
9609 .attr( 'role', 'menuitem' )
9610 .addClass( 'oo-ui-menuItemWidget' );
9615 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9618 * Section to group one or more items in a OO.ui.MenuWidget.
9621 * @extends OO.ui.DecoratedOptionWidget
9624 * @param {Mixed} data Item data
9625 * @param {Object} [config] Configuration options
9627 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
9628 // Parent constructor
9629 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
9632 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
9637 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9639 /* Static Properties */
9641 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
9643 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
9646 * Items for an OO.ui.OutlineWidget.
9649 * @extends OO.ui.DecoratedOptionWidget
9652 * @param {Mixed} data Item data
9653 * @param {Object} [config] Configuration options
9654 * @cfg {number} [level] Indentation level
9655 * @cfg {boolean} [movable] Allow modification from outline controls
9657 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
9658 // Config intialization
9659 config
= config
|| {};
9661 // Parent constructor
9662 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
9666 this.movable
= !!config
.movable
;
9667 this.removable
= !!config
.removable
;
9670 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
9671 this.setLevel( config
.level
);
9676 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9678 /* Static Properties */
9680 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
9682 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
9684 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
9686 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
9691 * Check if item is movable.
9693 * Movablilty is used by outline controls.
9695 * @return {boolean} Item is movable
9697 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
9698 return this.movable
;
9702 * Check if item is removable.
9704 * Removablilty is used by outline controls.
9706 * @return {boolean} Item is removable
9708 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
9709 return this.removable
;
9713 * Get indentation level.
9715 * @return {number} Indentation level
9717 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
9724 * Movablilty is used by outline controls.
9726 * @param {boolean} movable Item is movable
9729 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
9730 this.movable
= !!movable
;
9737 * Removablilty is used by outline controls.
9739 * @param {boolean} movable Item is removable
9742 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
9743 this.removable
= !!removable
;
9748 * Set indentation level.
9750 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
9753 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
9754 var levels
= this.constructor.static.levels
,
9755 levelClass
= this.constructor.static.levelClass
,
9758 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
9760 if ( this.level
=== i
) {
9761 this.$element
.addClass( levelClass
+ i
);
9763 this.$element
.removeClass( levelClass
+ i
);
9771 * Container for content that is overlaid and positioned absolutely.
9774 * @extends OO.ui.Widget
9775 * @mixins OO.ui.LabelElement
9778 * @param {Object} [config] Configuration options
9779 * @cfg {number} [width=320] Width of popup in pixels
9780 * @cfg {number} [height] Height of popup, omit to use automatic height
9781 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
9782 * @cfg {string} [align='center'] Alignment of popup to origin
9783 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
9784 * @cfg {jQuery} [$content] Content to append to the popup's body
9785 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
9786 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
9787 * @cfg {boolean} [head] Show label and close button at the top
9788 * @cfg {boolean} [padded] Add padding to the body
9790 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
9791 // Config intialization
9792 config
= config
|| {};
9794 // Parent constructor
9795 OO
.ui
.PopupWidget
.super.call( this, config
);
9797 // Mixin constructors
9798 OO
.ui
.LabelElement
.call( this, config
);
9799 OO
.ui
.ClippableElement
.call( this, config
);
9802 this.visible
= false;
9803 this.$popup
= this.$( '<div>' );
9804 this.$head
= this.$( '<div>' );
9805 this.$body
= this.$( '<div>' );
9806 this.$anchor
= this.$( '<div>' );
9807 this.$container
= config
.$container
|| this.$( 'body' );
9808 this.autoClose
= !!config
.autoClose
;
9809 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
9810 this.transitionTimeout
= null;
9812 this.width
= config
.width
!== undefined ? config
.width
: 320;
9813 this.height
= config
.height
!== undefined ? config
.height
: null;
9814 this.align
= config
.align
|| 'center';
9815 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
9816 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
9819 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
9822 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
9823 this.$body
.addClass( 'oo-ui-popupWidget-body' );
9824 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
9826 .addClass( 'oo-ui-popupWidget-head' )
9827 .append( this.$label
, this.closeButton
.$element
);
9828 if ( !config
.head
) {
9832 .addClass( 'oo-ui-popupWidget-popup' )
9833 .append( this.$head
, this.$body
);
9836 .addClass( 'oo-ui-popupWidget' )
9837 .append( this.$popup
, this.$anchor
);
9838 // Move content, which was added to #$element by OO.ui.Widget, to the body
9839 if ( config
.$content
instanceof jQuery
) {
9840 this.$body
.append( config
.$content
);
9842 if ( config
.padded
) {
9843 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
9845 this.setClippableElement( this.$body
);
9850 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
9851 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
9852 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
9867 * Handles mouse down events.
9869 * @param {jQuery.Event} e Mouse down event
9871 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
9874 !$.contains( this.$element
[0], e
.target
) &&
9875 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
9877 this.toggle( false );
9882 * Bind mouse down listener.
9884 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
9885 // Capture clicks outside popup
9886 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
9890 * Handles close button click events.
9892 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
9893 if ( this.isVisible() ) {
9894 this.toggle( false );
9899 * Unbind mouse down listener.
9901 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
9902 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
9906 * Set whether to show a anchor.
9908 * @param {boolean} [show] Show anchor, omit to toggle
9910 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
9911 show
= show
=== undefined ? !this.anchored
: !!show
;
9913 if ( this.anchored
!== show
) {
9915 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
9917 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
9919 this.anchored
= show
;
9924 * Check if showing a anchor.
9926 * @return {boolean} anchor is visible
9928 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
9935 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
9936 show
= show
=== undefined ? !this.isVisible() : !!show
;
9938 var change
= show
!== this.isVisible();
9941 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
9945 this.toggleClipping( true );
9946 if ( this.autoClose
) {
9947 this.bindMouseDownListener();
9949 this.updateDimensions();
9951 this.toggleClipping( false );
9952 if ( this.autoClose
) {
9953 this.unbindMouseDownListener();
9962 * Set the size of the popup.
9964 * Changing the size may also change the popup's position depending on the alignment.
9966 * @param {number} width Width
9967 * @param {number} height Height
9968 * @param {boolean} [transition=false] Use a smooth transition
9971 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
9973 this.height
= height
!== undefined ? height
: null;
9974 if ( this.isVisible() ) {
9975 this.updateDimensions( transition
);
9980 * Update the size and position.
9982 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
9983 * be called automatically.
9985 * @param {boolean} [transition=false] Use a smooth transition
9988 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
9991 originOffset
= Math
.round( this.$element
.offset().left
),
9992 containerLeft
= Math
.round( this.$container
.offset().left
),
9993 containerWidth
= this.$container
.innerWidth(),
9994 containerRight
= containerLeft
+ containerWidth
,
9995 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
],
9996 anchorWidth
= this.$anchor
.width(),
9997 popupLeft
= popupOffset
- padding
,
9998 popupRight
= popupOffset
+ padding
+ this.width
+ padding
,
9999 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
10000 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10002 // Prevent transition from being interrupted
10003 clearTimeout( this.transitionTimeout
);
10004 if ( transition
) {
10005 // Enable transition
10006 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10009 if ( overlapRight
< 0 ) {
10010 popupOffset
+= overlapRight
;
10011 } else if ( overlapLeft
< 0 ) {
10012 popupOffset
-= overlapLeft
;
10015 // Adjust offset to avoid anchor being rendered too close to the edge
10016 if ( this.align
=== 'right' ) {
10017 popupOffset
+= anchorWidth
;
10018 } else if ( this.align
=== 'left' ) {
10019 popupOffset
-= anchorWidth
;
10022 // Position body relative to anchor and resize
10026 height
: this.height
!== null ? this.height
: 'auto'
10029 if ( transition
) {
10030 // Prevent transitioning after transition is complete
10031 this.transitionTimeout
= setTimeout( function () {
10032 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10035 // Prevent transitioning immediately
10036 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10045 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10046 * Results are cleared and populated each time the query is changed.
10049 * @extends OO.ui.Widget
10052 * @param {Object} [config] Configuration options
10053 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10054 * @cfg {string} [value] Initial query value
10056 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10057 // Configuration intialization
10058 config
= config
|| {};
10060 // Parent constructor
10061 OO
.ui
.SearchWidget
.super.call( this, config
);
10064 this.query
= new OO
.ui
.TextInputWidget( {
10067 placeholder
: config
.placeholder
,
10068 value
: config
.value
10070 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10071 this.$query
= this.$( '<div>' );
10072 this.$results
= this.$( '<div>' );
10075 this.query
.connect( this, {
10076 change
: 'onQueryChange',
10077 enter
: 'onQueryEnter'
10079 this.results
.connect( this, {
10080 highlight
: 'onResultsHighlight',
10081 select
: 'onResultsSelect'
10083 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
10087 .addClass( 'oo-ui-searchWidget-query' )
10088 .append( this.query
.$element
);
10090 .addClass( 'oo-ui-searchWidget-results' )
10091 .append( this.results
.$element
);
10093 .addClass( 'oo-ui-searchWidget' )
10094 .append( this.$results
, this.$query
);
10099 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
10105 * @param {Object|null} item Item data or null if no item is highlighted
10110 * @param {Object|null} item Item data or null if no item is selected
10116 * Handle query key down events.
10118 * @param {jQuery.Event} e Key down event
10120 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
10121 var highlightedItem
, nextItem
,
10122 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
10125 highlightedItem
= this.results
.getHighlightedItem();
10126 if ( !highlightedItem
) {
10127 highlightedItem
= this.results
.getSelectedItem();
10129 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
10130 this.results
.highlightItem( nextItem
);
10131 nextItem
.scrollElementIntoView();
10136 * Handle select widget select events.
10138 * Clears existing results. Subclasses should repopulate items according to new query.
10140 * @param {string} value New value
10142 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
10144 this.results
.clearItems();
10148 * Handle select widget enter key events.
10150 * Selects highlighted item.
10152 * @param {string} value New value
10154 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
10156 this.results
.selectItem( this.results
.getHighlightedItem() );
10160 * Handle select widget highlight events.
10162 * @param {OO.ui.OptionWidget} item Highlighted item
10165 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
10166 this.emit( 'highlight', item
? item
.getData() : null );
10170 * Handle select widget select events.
10172 * @param {OO.ui.OptionWidget} item Selected item
10175 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
10176 this.emit( 'select', item
? item
.getData() : null );
10180 * Get the query input.
10182 * @return {OO.ui.TextInputWidget} Query input
10184 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
10189 * Get the results list.
10191 * @return {OO.ui.SelectWidget} Select list
10193 OO
.ui
.SearchWidget
.prototype.getResults = function () {
10194 return this.results
;
10198 * Generic selection of options.
10200 * Items can contain any rendering, and are uniquely identified by a has of thier data. Any widget
10201 * that provides options, from which the user must choose one, should be built on this class.
10203 * Use together with OO.ui.OptionWidget.
10206 * @extends OO.ui.Widget
10207 * @mixins OO.ui.GroupElement
10210 * @param {Object} [config] Configuration options
10211 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
10213 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
10214 // Config intialization
10215 config
= config
|| {};
10217 // Parent constructor
10218 OO
.ui
.SelectWidget
.super.call( this, config
);
10220 // Mixin constructors
10221 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
10224 this.pressed
= false;
10225 this.selecting
= null;
10227 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
10228 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
10231 this.$element
.on( {
10232 mousedown
: OO
.ui
.bind( this.onMouseDown
, this ),
10233 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
10234 mouseleave
: OO
.ui
.bind( this.onMouseLeave
, this )
10238 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
10239 if ( $.isArray( config
.items
) ) {
10240 this.addItems( config
.items
);
10246 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
10248 // Need to mixin base class as well
10249 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
10250 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
10256 * @param {OO.ui.OptionWidget|null} item Highlighted item
10261 * @param {OO.ui.OptionWidget|null} item Pressed item
10266 * @param {OO.ui.OptionWidget|null} item Selected item
10271 * @param {OO.ui.OptionWidget|null} item Chosen item
10276 * @param {OO.ui.OptionWidget[]} items Added items
10277 * @param {number} index Index items were added at
10282 * @param {OO.ui.OptionWidget[]} items Removed items
10288 * Handle mouse down events.
10291 * @param {jQuery.Event} e Mouse down event
10293 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
10296 if ( !this.isDisabled() && e
.which
=== 1 ) {
10297 this.togglePressed( true );
10298 item
= this.getTargetItem( e
);
10299 if ( item
&& item
.isSelectable() ) {
10300 this.pressItem( item
);
10301 this.selecting
= item
;
10302 this.getElementDocument().addEventListener(
10304 this.onMouseUpHandler
,
10307 this.getElementDocument().addEventListener(
10309 this.onMouseMoveHandler
,
10318 * Handle mouse up events.
10321 * @param {jQuery.Event} e Mouse up event
10323 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
10326 this.togglePressed( false );
10327 if ( !this.selecting
) {
10328 item
= this.getTargetItem( e
);
10329 if ( item
&& item
.isSelectable() ) {
10330 this.selecting
= item
;
10333 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
10334 this.pressItem( null );
10335 this.chooseItem( this.selecting
);
10336 this.selecting
= null;
10339 this.getElementDocument().removeEventListener(
10341 this.onMouseUpHandler
,
10344 this.getElementDocument().removeEventListener(
10346 this.onMouseMoveHandler
,
10354 * Handle mouse move events.
10357 * @param {jQuery.Event} e Mouse move event
10359 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
10362 if ( !this.isDisabled() && this.pressed
) {
10363 item
= this.getTargetItem( e
);
10364 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
10365 this.pressItem( item
);
10366 this.selecting
= item
;
10373 * Handle mouse over events.
10376 * @param {jQuery.Event} e Mouse over event
10378 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
10381 if ( !this.isDisabled() ) {
10382 item
= this.getTargetItem( e
);
10383 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
10389 * Handle mouse leave events.
10392 * @param {jQuery.Event} e Mouse over event
10394 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
10395 if ( !this.isDisabled() ) {
10396 this.highlightItem( null );
10402 * Get the closest item to a jQuery.Event.
10405 * @param {jQuery.Event} e
10406 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
10408 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
10409 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
10410 if ( $item
.length
) {
10411 return $item
.data( 'oo-ui-optionWidget' );
10417 * Get selected item.
10419 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
10421 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
10424 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10425 if ( this.items
[i
].isSelected() ) {
10426 return this.items
[i
];
10433 * Get highlighted item.
10435 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
10437 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
10440 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10441 if ( this.items
[i
].isHighlighted() ) {
10442 return this.items
[i
];
10449 * Get an existing item with equivilant data.
10451 * @param {Object} data Item data to search for
10452 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
10454 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
10455 var hash
= OO
.getHash( data
);
10457 if ( hash
in this.hashes
) {
10458 return this.hashes
[hash
];
10465 * Toggle pressed state.
10467 * @param {boolean} pressed An option is being pressed
10469 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
10470 if ( pressed
=== undefined ) {
10471 pressed
= !this.pressed
;
10473 if ( pressed
!== this.pressed
) {
10475 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
10476 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
10477 this.pressed
= pressed
;
10482 * Highlight an item.
10484 * Highlighting is mutually exclusive.
10486 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
10490 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
10491 var i
, len
, highlighted
,
10494 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10495 highlighted
= this.items
[i
] === item
;
10496 if ( this.items
[i
].isHighlighted() !== highlighted
) {
10497 this.items
[i
].setHighlighted( highlighted
);
10502 this.emit( 'highlight', item
);
10511 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
10515 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
10516 var i
, len
, selected
,
10519 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10520 selected
= this.items
[i
] === item
;
10521 if ( this.items
[i
].isSelected() !== selected
) {
10522 this.items
[i
].setSelected( selected
);
10527 this.emit( 'select', item
);
10536 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
10540 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
10541 var i
, len
, pressed
,
10544 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10545 pressed
= this.items
[i
] === item
;
10546 if ( this.items
[i
].isPressed() !== pressed
) {
10547 this.items
[i
].setPressed( pressed
);
10552 this.emit( 'press', item
);
10561 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
10562 * an item is selected using the keyboard or mouse.
10564 * @param {OO.ui.OptionWidget} item Item to choose
10568 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
10569 this.selectItem( item
);
10570 this.emit( 'choose', item
);
10576 * Get an item relative to another one.
10578 * @param {OO.ui.OptionWidget} item Item to start at
10579 * @param {number} direction Direction to move in
10580 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
10582 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
10583 var inc
= direction
> 0 ? 1 : -1,
10584 len
= this.items
.length
,
10585 index
= item
instanceof OO
.ui
.OptionWidget
?
10586 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
10587 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
10589 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
10590 Math
.max( index
, -1 ) :
10591 // Default to n-1 instead of -1, if nothing is selected let's start at the end
10592 Math
.min( index
, len
);
10595 i
= ( i
+ inc
+ len
) % len
;
10596 item
= this.items
[i
];
10597 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10600 // Stop iterating when we've looped all the way around
10601 if ( i
=== stopAt
) {
10609 * Get the next selectable item.
10611 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
10613 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
10616 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10617 item
= this.items
[i
];
10618 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10629 * When items are added with the same values as existing items, the existing items will be
10630 * automatically removed before the new items are added.
10632 * @param {OO.ui.OptionWidget[]} items Items to add
10633 * @param {number} [index] Index to insert items after
10637 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
10638 var i
, len
, item
, hash
,
10641 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10643 hash
= OO
.getHash( item
.getData() );
10644 if ( hash
in this.hashes
) {
10645 // Remove item with same value
10646 remove
.push( this.hashes
[hash
] );
10648 this.hashes
[hash
] = item
;
10650 if ( remove
.length
) {
10651 this.removeItems( remove
);
10655 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
10657 // Always provide an index, even if it was omitted
10658 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
10666 * Items will be detached, not removed, so they can be used later.
10668 * @param {OO.ui.OptionWidget[]} items Items to remove
10672 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
10673 var i
, len
, item
, hash
;
10675 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10677 hash
= OO
.getHash( item
.getData() );
10678 if ( hash
in this.hashes
) {
10679 // Remove existing item
10680 delete this.hashes
[hash
];
10682 if ( item
.isSelected() ) {
10683 this.selectItem( null );
10688 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
10690 this.emit( 'remove', items
);
10698 * Items will be detached, not removed, so they can be used later.
10703 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
10704 var items
= this.items
.slice();
10709 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
10710 this.selectItem( null );
10712 this.emit( 'remove', items
);
10718 * Select widget containing button options.
10720 * Use together with OO.ui.ButtonOptionWidget.
10723 * @extends OO.ui.SelectWidget
10726 * @param {Object} [config] Configuration options
10728 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
10729 // Parent constructor
10730 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
10733 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
10738 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
10741 * Overlaid menu of options.
10743 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
10746 * Use together with OO.ui.MenuItemWidget.
10749 * @extends OO.ui.SelectWidget
10750 * @mixins OO.ui.ClippableElement
10753 * @param {Object} [config] Configuration options
10754 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
10755 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
10756 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
10758 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
10759 // Config intialization
10760 config
= config
|| {};
10762 // Parent constructor
10763 OO
.ui
.MenuWidget
.super.call( this, config
);
10765 // Mixin constructors
10766 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
10769 this.flashing
= false;
10770 this.visible
= false;
10771 this.newItems
= null;
10772 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
10773 this.$input
= config
.input
? config
.input
.$input
: null;
10774 this.$widget
= config
.widget
? config
.widget
.$element
: null;
10775 this.$previousFocus
= null;
10776 this.isolated
= !config
.input
;
10777 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
10778 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
10783 .attr( 'role', 'menu' )
10784 .addClass( 'oo-ui-menuWidget' );
10789 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
10790 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
10795 * Handles document mouse down events.
10797 * @param {jQuery.Event} e Key down event
10799 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
10800 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
10801 this.toggle( false );
10806 * Handles key down events.
10808 * @param {jQuery.Event} e Key down event
10810 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
10813 highlightItem
= this.getHighlightedItem();
10815 if ( !this.isDisabled() && this.isVisible() ) {
10816 if ( !highlightItem
) {
10817 highlightItem
= this.getSelectedItem();
10819 switch ( e
.keyCode
) {
10820 case OO
.ui
.Keys
.ENTER
:
10821 this.chooseItem( highlightItem
);
10824 case OO
.ui
.Keys
.UP
:
10825 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
10828 case OO
.ui
.Keys
.DOWN
:
10829 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
10832 case OO
.ui
.Keys
.ESCAPE
:
10833 if ( highlightItem
) {
10834 highlightItem
.setHighlighted( false );
10836 this.toggle( false );
10842 this.highlightItem( nextItem
);
10843 nextItem
.scrollElementIntoView();
10847 e
.preventDefault();
10848 e
.stopPropagation();
10855 * Bind key down listener.
10857 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
10858 if ( this.$input
) {
10859 this.$input
.on( 'keydown', this.onKeyDownHandler
);
10861 // Capture menu navigation keys
10862 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
10867 * Unbind key down listener.
10869 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
10870 if ( this.$input
) {
10871 this.$input
.off( 'keydown' );
10873 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
10880 * This will close the menu when done, unlike selectItem which only changes selection.
10882 * @param {OO.ui.OptionWidget} item Item to choose
10885 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
10889 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
10891 if ( item
&& !this.flashing
) {
10892 this.flashing
= true;
10893 item
.flash().done( function () {
10894 widget
.toggle( false );
10895 widget
.flashing
= false;
10898 this.toggle( false );
10907 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
10911 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
10914 if ( !this.newItems
) {
10915 this.newItems
= [];
10918 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10920 if ( this.isVisible() ) {
10921 // Defer fitting label until item has been attached
10924 this.newItems
.push( item
);
10928 // Reevaluate clipping
10937 OO
.ui
.MenuWidget
.prototype.removeItems = function ( items
) {
10939 OO
.ui
.MenuWidget
.super.prototype.removeItems
.call( this, items
);
10941 // Reevaluate clipping
10950 OO
.ui
.MenuWidget
.prototype.clearItems = function () {
10952 OO
.ui
.MenuWidget
.super.prototype.clearItems
.call( this );
10954 // Reevaluate clipping
10963 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
10964 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
10967 change
= visible
!== this.isVisible();
10970 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
10974 this.bindKeyDownListener();
10976 // Change focus to enable keyboard navigation
10977 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
10978 this.$previousFocus
= this.$( ':focus' );
10979 this.$input
[0].focus();
10981 if ( this.newItems
&& this.newItems
.length
) {
10982 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
10983 this.newItems
[i
].fitLabel();
10985 this.newItems
= null;
10987 this.toggleClipping( true );
10990 if ( this.autoHide
) {
10991 this.getElementDocument().addEventListener(
10992 'mousedown', this.onDocumentMouseDownHandler
, true
10996 this.unbindKeyDownListener();
10997 if ( this.isolated
&& this.$previousFocus
) {
10998 this.$previousFocus
[0].focus();
10999 this.$previousFocus
= null;
11001 this.getElementDocument().removeEventListener(
11002 'mousedown', this.onDocumentMouseDownHandler
, true
11004 this.toggleClipping( false );
11012 * Menu for a text input widget.
11014 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11015 * is in a different frame, the menu's position is automatically calulated and maintained when the
11016 * menu is toggled or the window is resized.
11019 * @extends OO.ui.MenuWidget
11022 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11023 * @param {Object} [config] Configuration options
11024 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11026 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
11027 // Parent constructor
11028 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
11031 this.input
= input
;
11032 this.$container
= config
.$container
|| this.input
.$element
;
11033 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
11036 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
11041 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
11046 * Handle window resize event.
11048 * @param {jQuery.Event} e Window resize event
11050 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
11057 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
11058 visible
= !!visible
;
11060 var change
= visible
!== this.isVisible();
11063 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
11066 if ( this.isVisible() ) {
11068 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11070 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
11077 * Position the menu.
11081 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
11083 $container
= this.$container
,
11084 dimensions
= $container
.offset();
11086 // Position under input
11087 dimensions
.top
+= $container
.height();
11089 // Compensate for frame position if in a differnt frame
11090 if ( this.input
.$.$iframe
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
11091 frameOffset
= OO
.ui
.Element
.getRelativePosition(
11092 this.input
.$.$iframe
, this.$element
.offsetParent()
11094 dimensions
.left
+= frameOffset
.left
;
11095 dimensions
.top
+= frameOffset
.top
;
11097 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
11098 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
11099 dimensions
.right
= this.$element
.parent().position().left
-
11100 $container
.width() - dimensions
.left
;
11101 // Erase the value for 'left':
11102 delete dimensions
.left
;
11105 this.$element
.css( dimensions
);
11106 this.setIdealSize( $container
.width() );
11112 * Structured list of items.
11114 * Use with OO.ui.OutlineItemWidget.
11117 * @extends OO.ui.SelectWidget
11120 * @param {Object} [config] Configuration options
11122 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
11123 // Config intialization
11124 config
= config
|| {};
11126 // Parent constructor
11127 OO
.ui
.OutlineWidget
.super.call( this, config
);
11130 this.$element
.addClass( 'oo-ui-outlineWidget' );
11135 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
11138 * Switch that slides on and off.
11141 * @extends OO.ui.Widget
11142 * @mixins OO.ui.ToggleWidget
11145 * @param {Object} [config] Configuration options
11146 * @cfg {boolean} [value=false] Initial value
11148 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
11149 // Parent constructor
11150 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
11152 // Mixin constructors
11153 OO
.ui
.ToggleWidget
.call( this, config
);
11156 this.dragging
= false;
11157 this.dragStart
= null;
11158 this.sliding
= false;
11159 this.$glow
= this.$( '<span>' );
11160 this.$grip
= this.$( '<span>' );
11163 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
11166 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
11167 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
11169 .addClass( 'oo-ui-toggleSwitchWidget' )
11170 .append( this.$glow
, this.$grip
);
11175 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
11176 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
11181 * Handle mouse down events.
11183 * @param {jQuery.Event} e Mouse down event
11185 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
11186 if ( !this.isDisabled() && e
.which
=== 1 ) {
11187 this.setValue( !this.value
);