2 * OOjs UI v0.1.0-pre (a1b99bb256)
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-07-17T19:17:19Z
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 {OO.ui.Frame} [frame] Frame of the document context
658 * @return {Function} Bound jQuery function
660 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
661 function wrapper( selector
) {
662 return $( selector
, wrapper
.context
);
665 wrapper
.context
= this.getDocument( context
);
668 wrapper
.frame
= frame
;
675 * Get the document of an element.
678 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
679 * @return {HTMLDocument|null} Document object
681 OO
.ui
.Element
.getDocument = function ( obj
) {
682 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
683 return ( obj
[0] && obj
[0].ownerDocument
) ||
684 // Empty jQuery selections might have a context
691 ( obj
.nodeType
=== 9 && obj
) ||
696 * Get the window of an element or document.
699 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
700 * @return {Window} Window object
702 OO
.ui
.Element
.getWindow = function ( obj
) {
703 var doc
= this.getDocument( obj
);
704 return doc
.parentWindow
|| doc
.defaultView
;
708 * Get the direction of an element or document.
711 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
712 * @return {string} Text direction, either `ltr` or `rtl`
714 OO
.ui
.Element
.getDir = function ( obj
) {
717 if ( obj
instanceof jQuery
) {
720 isDoc
= obj
.nodeType
=== 9;
721 isWin
= obj
.document
!== undefined;
722 if ( isDoc
|| isWin
) {
728 return $( obj
).css( 'direction' );
732 * Get the offset between two frames.
734 * TODO: Make this function not use recursion.
737 * @param {Window} from Window of the child frame
738 * @param {Window} [to=window] Window of the parent frame
739 * @param {Object} [offset] Offset to start with, used internally
740 * @return {Object} Offset object, containing left and top properties
742 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
743 var i
, len
, frames
, frame
, rect
;
749 offset
= { 'top': 0, 'left': 0 };
751 if ( from.parent
=== from ) {
755 // Get iframe element
756 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
757 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
758 if ( frames
[i
].contentWindow
=== from ) {
764 // Recursively accumulate offset values
766 rect
= frame
.getBoundingClientRect();
767 offset
.left
+= rect
.left
;
768 offset
.top
+= rect
.top
;
770 this.getFrameOffset( from.parent
, offset
);
777 * Get the offset between two elements.
780 * @param {jQuery} $from
781 * @param {jQuery} $to
782 * @return {Object} Translated position coordinates, containing top and left properties
784 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
785 var from = $from.offset(),
787 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
791 * Get element border sizes.
794 * @param {HTMLElement} el Element to measure
795 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
797 OO
.ui
.Element
.getBorders = function ( el
) {
798 var doc
= el
.ownerDocument
,
799 win
= doc
.parentWindow
|| doc
.defaultView
,
800 style
= win
&& win
.getComputedStyle
?
801 win
.getComputedStyle( el
, null ) :
804 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
805 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
806 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
807 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
810 'top': Math
.round( top
),
811 'left': Math
.round( left
),
812 'bottom': Math
.round( bottom
),
813 'right': Math
.round( right
)
818 * Get dimensions of an element or window.
821 * @param {HTMLElement|Window} el Element to measure
822 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
824 OO
.ui
.Element
.getDimensions = function ( el
) {
826 doc
= el
.ownerDocument
|| el
.document
,
827 win
= doc
.parentWindow
|| doc
.defaultView
;
829 if ( win
=== el
|| el
=== doc
.documentElement
) {
832 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
834 'top': $win
.scrollTop(),
835 'left': $win
.scrollLeft()
837 'scrollbar': { 'right': 0, 'bottom': 0 },
841 'bottom': $win
.innerHeight(),
842 'right': $win
.innerWidth()
848 'borders': this.getBorders( el
),
850 'top': $el
.scrollTop(),
851 'left': $el
.scrollLeft()
854 'right': $el
.innerWidth() - el
.clientWidth
,
855 'bottom': $el
.innerHeight() - el
.clientHeight
857 'rect': el
.getBoundingClientRect()
863 * Get closest scrollable container.
865 * Traverses up until either a scrollable element or the root is reached, in which case the window
869 * @param {HTMLElement} el Element to find scrollable container for
870 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
871 * @return {HTMLElement|Window} Closest scrollable container
873 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
875 props
= [ 'overflow' ],
876 $parent
= $( el
).parent();
878 if ( dimension
=== 'x' || dimension
=== 'y' ) {
879 props
.push( 'overflow-' + dimension
);
882 while ( $parent
.length
) {
883 if ( $parent
[0] === el
.ownerDocument
.body
) {
888 val
= $parent
.css( props
[i
] );
889 if ( val
=== 'auto' || val
=== 'scroll' ) {
893 $parent
= $parent
.parent();
895 return this.getDocument( el
).body
;
899 * Scroll element into view.
902 * @param {HTMLElement} el Element to scroll into view
903 * @param {Object} [config={}] Configuration config
904 * @param {string} [config.duration] jQuery animation duration value
905 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
906 * to scroll in both directions
907 * @param {Function} [config.complete] Function to call when scrolling completes
909 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
910 // Configuration initialization
911 config
= config
|| {};
914 callback
= typeof config
.complete
=== 'function' && config
.complete
,
915 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
917 eld
= this.getDimensions( el
),
918 scd
= this.getDimensions( sc
),
919 $win
= $( this.getWindow( el
) );
921 // Compute the distances between the edges of el and the edges of the scroll viewport
922 if ( $sc
.is( 'body' ) ) {
923 // If the scrollable container is the <body> this is easy
926 'bottom': $win
.innerHeight() - eld
.rect
.bottom
,
927 'left': eld
.rect
.left
,
928 'right': $win
.innerWidth() - eld
.rect
.right
931 // Otherwise, we have to subtract el's coordinates from sc's coordinates
933 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
934 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
935 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
936 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
940 if ( !config
.direction
|| config
.direction
=== 'y' ) {
942 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
943 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
944 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
947 if ( !config
.direction
|| config
.direction
=== 'x' ) {
948 if ( rel
.left
< 0 ) {
949 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
950 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
951 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
954 if ( !$.isEmptyObject( anim
) ) {
955 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
957 $sc
.queue( function ( next
) {
972 * Get the HTML tag name.
974 * Override this method to base the result on instance information.
976 * @return {string} HTML tag name
978 OO
.ui
.Element
.prototype.getTagName = function () {
979 return this.constructor.static.tagName
;
983 * Check if the element is attached to the DOM
984 * @return {boolean} The element is attached to the DOM
986 OO
.ui
.Element
.prototype.isElementAttached = function () {
987 return $.contains( this.getElementDocument(), this.$element
[0] );
991 * Get the DOM document.
993 * @return {HTMLDocument} Document object
995 OO
.ui
.Element
.prototype.getElementDocument = function () {
996 return OO
.ui
.Element
.getDocument( this.$element
);
1000 * Get the DOM window.
1002 * @return {Window} Window object
1004 OO
.ui
.Element
.prototype.getElementWindow = function () {
1005 return OO
.ui
.Element
.getWindow( this.$element
);
1009 * Get closest scrollable container.
1011 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1012 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1016 * Get group element is in.
1018 * @return {OO.ui.GroupElement|null} Group element, null if none
1020 OO
.ui
.Element
.prototype.getElementGroup = function () {
1021 return this.elementGroup
;
1025 * Set group element is in.
1027 * @param {OO.ui.GroupElement|null} group Group element, null if none
1030 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1031 this.elementGroup
= group
;
1036 * Scroll element into view.
1038 * @param {Object} [config={}]
1040 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1041 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1045 * Bind a handler for an event on this.$element
1047 * @deprecated Use jQuery#on instead.
1048 * @param {string} event
1049 * @param {Function} callback
1051 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1052 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1056 * Unbind a handler bound with #offDOMEvent
1058 * @deprecated Use jQuery#off instead.
1059 * @param {string} event
1060 * @param {Function} callback
1062 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1063 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1068 * Bind a handler for an event on a DOM element.
1070 * Used to be for working around a jQuery bug (jqbug.com/14180),
1071 * but obsolete as of jQuery 1.11.0.
1074 * @deprecated Use jQuery#on instead.
1075 * @param {HTMLElement|jQuery} el DOM element
1076 * @param {string} event Event to bind
1077 * @param {Function} callback Callback to call when the event fires
1079 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1080 $( el
).on( event
, callback
);
1084 * Unbind a handler bound with #static-method-onDOMEvent.
1086 * @deprecated Use jQuery#off instead.
1088 * @param {HTMLElement|jQuery} el DOM element
1089 * @param {string} event Event to unbind
1090 * @param {Function} [callback] Callback to unbind
1092 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1093 $( el
).off( event
, callback
);
1098 * Embedded iframe with the same styles as its parent.
1101 * @extends OO.ui.Element
1102 * @mixins OO.EventEmitter
1105 * @param {Object} [config] Configuration options
1107 OO
.ui
.Frame
= function OoUiFrame( config
) {
1108 // Parent constructor
1109 OO
.ui
.Frame
.super.call( this, config
);
1111 // Mixin constructors
1112 OO
.EventEmitter
.call( this );
1115 this.loading
= null;
1116 this.config
= config
;
1120 .addClass( 'oo-ui-frame' )
1121 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
1127 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
1128 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
1130 /* Static Properties */
1136 OO
.ui
.Frame
.static.tagName
= 'iframe';
1144 /* Static Methods */
1147 * Transplant the CSS styles from as parent document to a frame's document.
1149 * This loops over the style sheets in the parent document, and copies their nodes to the
1150 * frame's document. It then polls the document to see when all styles have loaded, and once they
1151 * have, resolves the promise.
1153 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1154 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1155 * Firefox, where the styles won't load until the iframe becomes visible.
1157 * For details of how we arrived at the strategy used in this function, see #load.
1161 * @param {HTMLDocument} parentDoc Document to transplant styles from
1162 * @param {HTMLDocument} frameDoc Document to transplant styles to
1163 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1164 * @return {jQuery.Promise} Promise resolved when styles have loaded
1166 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1167 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1168 $pollNodes
= $( [] ),
1169 // Fake font-family value
1170 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1171 deferred
= $.Deferred();
1173 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1174 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1175 if ( styleNode
.disabled
) {
1178 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1179 // External stylesheet
1180 // Create a node with a unique ID that we're going to monitor to see when the CSS
1182 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
1183 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1184 .attr( 'id', pollNodeId
)
1185 .appendTo( frameDoc
.body
)
1188 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
1189 // The font-family rule will only take effect once the @import finishes
1190 newNode
= frameDoc
.createElement( 'style' );
1191 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
1192 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1194 // Not an external stylesheet, or no polling required; just copy the node over
1195 newNode
= frameDoc
.importNode( styleNode
, true );
1197 frameDoc
.head
.appendChild( newNode
);
1200 // Poll every 100ms until all external stylesheets have loaded
1201 $pendingPollNodes
= $pollNodes
;
1202 timeoutID
= setTimeout( function pollExternalStylesheets() {
1204 $pendingPollNodes
.length
> 0 &&
1205 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1207 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1210 if ( $pendingPollNodes
.length
=== 0 ) {
1212 if ( timeoutID
!== null ) {
1214 $pollNodes
.remove();
1218 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1221 // ...but give up after a while
1222 if ( timeout
!== 0 ) {
1223 setTimeout( function () {
1225 clearTimeout( timeoutID
);
1227 $pollNodes
.remove();
1230 }, timeout
|| 5000 );
1233 return deferred
.promise();
1239 * Load the frame contents.
1241 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
1242 * promise will be resolved. Calling while loading will return a promise but not trigger a new
1243 * loading cycle. Calling after loading is complete will return a promise that's already been
1246 * Sounds simple right? Read on...
1248 * When you create a dynamic iframe using open/write/close, the window.load event for the
1249 * iframe is triggered when you call close, and there's no further load event to indicate that
1250 * everything is actually loaded.
1252 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
1253 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
1254 * are added to document.styleSheets immediately, and the only way you can determine whether they've
1255 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
1256 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
1258 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
1259 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
1260 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
1261 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
1262 * .cssRules is allowed.
1264 * However, now that we control the styles we're injecting, we might as well do away with
1265 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
1266 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
1267 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
1268 * rule is not applied until after the `@import` finishes.
1270 * All this stylesheet injection and polling magic is in #transplantStyles.
1272 * @return {jQuery.Promise} Promise resolved when loading is complete
1275 OO
.ui
.Frame
.prototype.load = function () {
1279 // Return existing promise if already loading or loaded
1280 if ( this.loading
) {
1281 return this.loading
.promise();
1285 this.loading
= $.Deferred();
1287 win
= this.$element
.prop( 'contentWindow' );
1290 // Figure out directionality:
1291 this.dir
= OO
.ui
.Element
.getDir( this.$element
) || 'ltr';
1293 // Initialize contents
1298 '<body class="oo-ui-frame-content oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
1305 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
1306 this.$content
= this.$( '.oo-ui-frame-content' ).attr( 'tabIndex', 0 );
1307 this.$document
= this.$( doc
);
1310 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
1311 .always( function () {
1312 frame
.emit( 'load' );
1313 frame
.loading
.resolve();
1316 return this.loading
.promise();
1320 * Set the size of the frame.
1322 * @param {number} width Frame width in pixels
1323 * @param {number} height Frame height in pixels
1326 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
1327 this.$element
.css( { 'width': width
, 'height': height
} );
1332 * Container for elements.
1336 * @extends OO.ui.Element
1337 * @mixins OO.EventEmitter
1340 * @param {Object} [config] Configuration options
1342 OO
.ui
.Layout
= function OoUiLayout( config
) {
1343 // Initialize config
1344 config
= config
|| {};
1346 // Parent constructor
1347 OO
.ui
.Layout
.super.call( this, config
);
1349 // Mixin constructors
1350 OO
.EventEmitter
.call( this );
1353 this.$element
.addClass( 'oo-ui-layout' );
1358 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1359 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1362 * User interface control.
1366 * @extends OO.ui.Element
1367 * @mixins OO.EventEmitter
1370 * @param {Object} [config] Configuration options
1371 * @cfg {boolean} [disabled=false] Disable
1373 OO
.ui
.Widget
= function OoUiWidget( config
) {
1374 // Initialize config
1375 config
= $.extend( { 'disabled': false }, config
);
1377 // Parent constructor
1378 OO
.ui
.Widget
.super.call( this, config
);
1380 // Mixin constructors
1381 OO
.EventEmitter
.call( this );
1384 this.visible
= true;
1385 this.disabled
= null;
1386 this.wasDisabled
= null;
1389 this.$element
.addClass( 'oo-ui-widget' );
1390 this.setDisabled( !!config
.disabled
);
1395 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1396 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1402 * @param {boolean} disabled Widget is disabled
1407 * @param {boolean} visible Widget is visible
1413 * Check if the widget is disabled.
1415 * @param {boolean} Button is disabled
1417 OO
.ui
.Widget
.prototype.isDisabled = function () {
1418 return this.disabled
;
1422 * Check if widget is visible.
1424 * @return {boolean} Widget is visible
1426 OO
.ui
.Widget
.prototype.isVisible = function () {
1427 return this.visible
;
1431 * Set the disabled state of the widget.
1433 * This should probably change the widgets' appearance and prevent it from being used.
1435 * @param {boolean} disabled Disable widget
1438 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1441 this.disabled
= !!disabled
;
1442 isDisabled
= this.isDisabled();
1443 if ( isDisabled
!== this.wasDisabled
) {
1444 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1445 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1446 this.emit( 'disable', isDisabled
);
1448 this.wasDisabled
= isDisabled
;
1454 * Toggle visibility of widget.
1456 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1460 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1461 show
= show
=== undefined ? !this.visible
: !!show
;
1463 if ( show
!== this.isVisible() ) {
1464 this.visible
= show
;
1465 this.$element
.toggle( show
);
1466 this.emit( 'toggle', show
);
1473 * Update the disabled state, in case of changes in parent widget.
1477 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1478 this.setDisabled( this.disabled
);
1483 * Container for elements in a child frame.
1485 * Use together with OO.ui.WindowManager.
1489 * @extends OO.ui.Element
1490 * @mixins OO.EventEmitter
1492 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1493 * teardown processes are executed when the window is closed.
1495 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1496 * - Window manager begins opening window
1497 * - {@link #getSetupProcess} method is called and its result executed
1498 * - {@link #getReadyProcess} method is called and its result executed
1499 * - Window is now open
1501 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1502 * - Window manager begins closing window
1503 * - {@link #getHoldProcess} method is called and its result executed
1504 * - {@link #getTeardownProcess} method is called and its result executed
1505 * - Window is now closed
1507 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1508 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1509 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1510 * processing can complete. Always assume window processes are executed asychronously. See
1511 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1512 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1514 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1515 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1518 * @param {OO.ui.WindowManager} manager Manager of window
1519 * @param {Object} [config] Configuration options
1520 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1524 OO
.ui
.Window
= function OoUiWindow( manager
, config
) {
1527 // Configuration initialization
1528 config
= config
|| {};
1530 // Parent constructor
1531 OO
.ui
.Window
.super.call( this, config
);
1533 // Mixin constructors
1534 OO
.EventEmitter
.call( this );
1536 if ( !( manager
instanceof OO
.ui
.WindowManager
) ) {
1537 throw new Error( 'Cannot construct window: window must have a manager' );
1541 this.manager
= manager
;
1542 this.initialized
= false;
1543 this.visible
= false;
1544 this.opening
= null;
1545 this.closing
= null;
1548 this.size
= config
.size
|| this.constructor.static.size
;
1549 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
1550 this.$frame
= this.$( '<div>' );
1551 this.$ = function () {
1552 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1557 .addClass( 'oo-ui-window' )
1558 // Hide the window using visibility: hidden; while the iframe is still loading
1559 // Can't use display: none; because that prevents the iframe from loading in Firefox
1560 .css( 'visibility', 'hidden' )
1561 .append( this.$frame
);
1563 .addClass( 'oo-ui-window-frame' )
1564 .append( this.frame
.$element
);
1567 this.frame
.on( 'load', function () {
1569 win
.initialized
= true;
1570 // Undo the visibility: hidden; hack and apply display: none;
1571 // We can do this safely now that the iframe has initialized
1572 // (don't do this from within #initialize because it has to happen
1573 // after the all subclasses have been handled as well).
1574 win
.$element
.hide().css( 'visibility', '' );
1580 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1581 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1587 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1590 /* Static Properties */
1593 * Symbolic name of size.
1595 * Size is used if no size is configured during construction.
1599 * @property {string}
1601 OO
.ui
.Window
.static.size
= 'medium';
1606 * Check if window has been initialized.
1608 * @return {boolean} Window has been initialized
1610 OO
.ui
.Window
.prototype.isInitialized = function () {
1611 return this.initialized
;
1615 * Check if window is visible.
1617 * @return {boolean} Window is visible
1619 OO
.ui
.Window
.prototype.isVisible = function () {
1620 return this.visible
;
1624 * Check if window is opening.
1626 * This is a wrapper around OO.ui.WindowManager#isOpening.
1628 * @return {boolean} Window is opening
1630 OO
.ui
.Window
.prototype.isOpening = function () {
1631 return this.manager
.isOpening( this );
1635 * Check if window is closing.
1637 * This is a wrapper around OO.ui.WindowManager#isClosing.
1639 * @return {boolean} Window is closing
1641 OO
.ui
.Window
.prototype.isClosing = function () {
1642 return this.manager
.isClosing( this );
1646 * Check if window is opened.
1648 * This is a wrapper around OO.ui.WindowManager#isOpened.
1650 * @return {boolean} Window is opened
1652 OO
.ui
.Window
.prototype.isOpened = function () {
1653 return this.manager
.isOpened( this );
1657 * Get the window manager.
1659 * @return {OO.ui.WindowManager} Manager of window
1661 OO
.ui
.Window
.prototype.getManager = function () {
1662 return this.manager
;
1666 * Get the window frame.
1668 * @return {OO.ui.Frame} Frame of window
1670 OO
.ui
.Window
.prototype.getFrame = function () {
1675 * Get the window size.
1677 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1679 OO
.ui
.Window
.prototype.getSize = function () {
1684 * Get the height of the dialog contents.
1686 * @return {number} Content height
1688 OO
.ui
.Window
.prototype.getContentHeight = function () {
1690 // Add buffer for border
1691 ( ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) * 2 ) +
1692 // Height of contents
1693 ( this.$head
.outerHeight( true ) + this.getBodyHeight() + this.$foot
.outerHeight( true ) )
1698 * Get the height of the dialog contents.
1700 * @return {number} Height of content
1702 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1703 return this.$body
[0].scrollHeight
;
1707 * Get a process for setting up a window for use.
1709 * Each time the window is opened this process will set it up for use in a particular context, based
1710 * on the `data` argument.
1712 * When you override this method, you can add additional setup steps to the process the parent
1713 * method provides using the 'first' and 'next' methods.
1716 * @param {Object} [data] Window opening data
1717 * @return {OO.ui.Process} Setup process
1719 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1720 return new OO
.ui
.Process();
1724 * Get a process for readying a window for use.
1726 * Each time the window is open and setup, this process will ready it up for use in a particular
1727 * context, based on the `data` argument.
1729 * When you override this method, you can add additional setup steps to the process the parent
1730 * method provides using the 'first' and 'next' methods.
1733 * @param {Object} [data] Window opening data
1734 * @return {OO.ui.Process} Setup process
1736 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1737 return new OO
.ui
.Process();
1741 * Get a process for holding a window from use.
1743 * Each time the window is closed, this process will hold it from use in a particular context, based
1744 * on the `data` argument.
1746 * When you override this method, you can add additional setup steps to the process the parent
1747 * method provides using the 'first' and 'next' methods.
1750 * @param {Object} [data] Window closing data
1751 * @return {OO.ui.Process} Hold process
1753 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1754 return new OO
.ui
.Process();
1758 * Get a process for tearing down a window after use.
1760 * Each time the window is closed this process will tear it down and do something with the user's
1761 * interactions within the window, based on the `data` argument.
1763 * When you override this method, you can add additional teardown steps to the process the parent
1764 * method provides using the 'first' and 'next' methods.
1767 * @param {Object} [data] Window closing data
1768 * @return {OO.ui.Process} Teardown process
1770 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1771 return new OO
.ui
.Process();
1775 * Set the window size.
1777 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1780 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1782 this.manager
.updateWindowSize( this );
1787 * Set window dimensions.
1789 * Properties are applied to the frame container.
1791 * @param {Object} dim CSS dimension properties
1792 * @param {string|number} [dim.width] Width
1793 * @param {string|number} [dim.minWidth] Minimum width
1794 * @param {string|number} [dim.maxWidth] Maximum width
1795 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1796 * @param {string|number} [dim.minWidth] Minimum height
1797 * @param {string|number} [dim.maxWidth] Maximum height
1800 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1801 // Apply width before height so height is not based on wrapping content using the wrong width
1803 'width': dim
.width
|| '',
1804 'min-width': dim
.minWidth
|| '',
1805 'max-width': dim
.maxWidth
|| ''
1808 'height': ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1809 'min-height': dim
.minHeight
|| '',
1810 'max-height': dim
.maxHeight
|| ''
1816 * Initialize window contents.
1818 * The first time the window is opened, #initialize is called when it's safe to begin populating
1819 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1821 * Once this method is called, this.$ can be used to create elements within the frame.
1825 OO
.ui
.Window
.prototype.initialize = function () {
1827 this.$ = this.frame
.$;
1828 this.$head
= this.$( '<div>' );
1829 this.$body
= this.$( '<div>' );
1830 this.$foot
= this.$( '<div>' );
1831 this.$overlay
= this.$( '<div>' );
1834 this.$head
.addClass( 'oo-ui-window-head' );
1835 this.$body
.addClass( 'oo-ui-window-body' );
1836 this.$foot
.addClass( 'oo-ui-window-foot' );
1837 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1839 .addClass( 'oo-ui-window-content' )
1840 .append( this.$head
, this.$body
, this.$foot
, this.$overlay
);
1848 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1849 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1851 * @param {Object} [data] Window opening data
1852 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1853 * first argument will be a promise which will be resolved when the window begins closing
1855 OO
.ui
.Window
.prototype.open = function ( data
) {
1856 return this.manager
.openWindow( this, data
);
1862 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1863 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1865 * @param {Object} [data] Window closing data
1866 * @return {jQuery.Promise} Promise resolved when window is closed
1868 OO
.ui
.Window
.prototype.close = function ( data
) {
1869 return this.manager
.closeWindow( this, data
);
1875 * This is called by OO.ui.WindowManager durring window adding, and should not be called directly
1878 * @return {jQuery.Promise} Promise resolved when window is loaded
1880 OO
.ui
.Window
.prototype.load = function () {
1881 return this.frame
.load();
1887 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1890 * @param {Object} [data] Window opening data
1891 * @return {jQuery.Promise} Promise resolved when window is setup
1893 OO
.ui
.Window
.prototype.setup = function ( data
) {
1895 deferred
= $.Deferred();
1897 this.$element
.show();
1898 this.visible
= true;
1899 this.getSetupProcess( data
).execute().done( function () {
1900 win
.manager
.updateWindowSize( win
);
1901 // Force redraw by asking the browser to measure the elements' widths
1902 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
1903 win
.frame
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
1907 return deferred
.promise();
1913 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1916 * @param {Object} [data] Window opening data
1917 * @return {jQuery.Promise} Promise resolved when window is ready
1919 OO
.ui
.Window
.prototype.ready = function ( data
) {
1921 deferred
= $.Deferred();
1923 this.frame
.$content
.focus();
1924 this.getReadyProcess( data
).execute().done( function () {
1925 // Force redraw by asking the browser to measure the elements' widths
1926 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
1927 win
.frame
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
1931 return deferred
.promise();
1937 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1940 * @param {Object} [data] Window closing data
1941 * @return {jQuery.Promise} Promise resolved when window is held
1943 OO
.ui
.Window
.prototype.hold = function ( data
) {
1945 deferred
= $.Deferred();
1947 this.getHoldProcess( data
).execute().done( function () {
1948 win
.frame
.$content
.find( ':focus' ).blur();
1949 // Force redraw by asking the browser to measure the elements' widths
1950 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
1951 win
.frame
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
1955 return deferred
.promise();
1961 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1964 * @param {Object} [data] Window closing data
1965 * @return {jQuery.Promise} Promise resolved when window is torn down
1967 OO
.ui
.Window
.prototype.teardown = function ( data
) {
1969 deferred
= $.Deferred();
1971 this.getTeardownProcess( data
).execute().done( function () {
1972 // Force redraw by asking the browser to measure the elements' widths
1973 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
1974 win
.frame
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
1975 win
.$element
.hide();
1976 win
.visible
= false;
1980 return deferred
.promise();
1984 * Base class for all dialogs.
1987 * - Manage the window (open and close, etc.).
1988 * - Store the internal name and display title.
1989 * - A stack to track one or more pending actions.
1990 * - Manage a set of actions that can be performed.
1991 * - Configure and create action widgets.
1994 * - Close the dialog with Escape key.
1995 * - Visually lock the dialog while an action is in
1996 * progress (aka "pending").
1998 * Subclass responsibilities:
1999 * - Display the title somewhere.
2000 * - Add content to the dialog.
2001 * - Provide a UI to close the dialog.
2002 * - Display the action widgets somewhere.
2006 * @extends OO.ui.Window
2007 * @mixins OO.ui.LabeledElement
2010 * @param {Object} [config] Configuration options
2012 OO
.ui
.Dialog
= function OoUiDialog( manager
, config
) {
2013 // Parent constructor
2014 OO
.ui
.Dialog
.super.call( this, manager
, config
);
2017 this.actions
= new OO
.ui
.ActionSet();
2018 this.attachedActions
= [];
2019 this.currentAction
= null;
2023 this.actions
.connect( this, {
2024 'click': 'onActionClick',
2025 'resize': 'onActionResize',
2026 'change': 'onActionsChange'
2031 .addClass( 'oo-ui-dialog' )
2032 .attr( 'role', 'dialog' );
2037 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2039 /* Static Properties */
2042 * Symbolic name of dialog.
2047 * @property {string}
2049 OO
.ui
.Dialog
.static.name
= '';
2057 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2059 OO
.ui
.Dialog
.static.title
= '';
2062 * List of OO.ui.ActionWidget configuration options.
2066 * @property {Object[]}
2068 OO
.ui
.Dialog
.static.actions
= [];
2071 * Close dialog when the escape key is pressed.
2076 * @property {boolean}
2078 OO
.ui
.Dialog
.static.escapable
= true;
2083 * Handle frame document key down events.
2085 * @param {jQuery.Event} e Key down event
2087 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
2088 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2095 * Handle action resized events.
2097 * @param {OO.ui.ActionWidget} action Action that was resized
2099 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2100 // Override in subclass
2104 * Handle action click events.
2106 * @param {OO.ui.ActionWidget} action Action that was clicked
2108 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2109 if ( !this.isPending() ) {
2110 this.currentAction
= action
;
2111 this.executeAction( action
.getAction() );
2116 * Handle actions change event.
2118 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2119 this.detachActions();
2120 if ( !this.isClosing() ) {
2121 this.attachActions();
2126 * Check if input is pending.
2130 OO
.ui
.Dialog
.prototype.isPending = function () {
2131 return !!this.pending
;
2135 * Get set of actions.
2137 * @return {OO.ui.ActionSet}
2139 OO
.ui
.Dialog
.prototype.getActions = function () {
2140 return this.actions
;
2144 * Get a process for taking action.
2146 * When you override this method, you can add additional accept steps to the process the parent
2147 * method provides using the 'first' and 'next' methods.
2150 * @param {string} [action] Symbolic name of action
2151 * @return {OO.ui.Process} Action process
2153 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2154 return new OO
.ui
.Process()
2155 .next( function () {
2157 // An empty action always closes the dialog without data, which should always be
2158 // safe and make no changes
2167 * @param {Object} [data] Dialog opening data
2168 * @param {jQuery|string|Function|null} [data.label] Dialog label, omit to use #static-label
2169 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2170 * action item, omit to use #static-actions
2172 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2176 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2177 .next( function () {
2180 config
= this.constructor.static,
2181 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2183 this.title
.setLabel(
2184 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2186 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2188 new OO
.ui
.ActionWidget( $.extend( { '$': this.$ }, actions
[i
] ) )
2191 this.actions
.add( items
);
2198 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2200 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2201 .first( function () {
2202 this.actions
.clear();
2203 this.currentAction
= null;
2210 OO
.ui
.Dialog
.prototype.initialize = function () {
2212 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2215 this.title
= new OO
.ui
.LabelWidget( { '$': this.$ } );
2218 if ( this.constructor.static.escapable
) {
2219 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
2223 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
2227 * Attach action actions.
2229 OO
.ui
.Dialog
.prototype.attachActions = function () {
2230 // Remember the list of potentially attached actions
2231 this.attachedActions
= this.actions
.get();
2235 * Detach action actions.
2239 OO
.ui
.Dialog
.prototype.detachActions = function () {
2242 // Detach all actions that may have been previously attached
2243 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2244 this.attachedActions
[i
].$element
.detach();
2246 this.attachedActions
= [];
2250 * Execute an action.
2252 * @param {string} action Symbolic name of action to execute
2253 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2255 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2257 return this.getActionProcess( action
).execute()
2258 .always( OO
.ui
.bind( this.popPending
, this ) );
2262 * Increase the pending stack.
2266 OO
.ui
.Dialog
.prototype.pushPending = function () {
2267 if ( this.pending
=== 0 ) {
2268 this.frame
.$content
.addClass( 'oo-ui-actionDialog-content-pending' );
2269 this.$head
.addClass( 'oo-ui-texture-pending' );
2277 * Reduce the pending stack.
2283 OO
.ui
.Dialog
.prototype.popPending = function () {
2284 if ( this.pending
=== 1 ) {
2285 this.frame
.$content
.removeClass( 'oo-ui-actionDialog-content-pending' );
2286 this.$head
.removeClass( 'oo-ui-texture-pending' );
2288 this.pending
= Math
.max( 0, this.pending
- 1 );
2294 * Collection of windows.
2297 * @extends OO.ui.Element
2298 * @mixins OO.EventEmitter
2300 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2301 * already opening or opened, the current window will be closed without data. Empty closing data
2302 * should always result in the window being closed without causing constructive or destructive
2305 * As a window is opened and closed, it passes through several stages and the manager emits several
2306 * corresponding events.
2308 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2309 * - {@link #event-opening} is emitted with `opening` promise
2310 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2311 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2312 * - `setup` progress notification is emitted from opening promise
2313 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2314 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2315 * - `ready` progress notification is emitted from opening promise
2316 * - `opening` promise is resolved with `opened` promise
2317 * - Window is now open
2319 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2320 * - `opened` promise is resolved with `closing` promise
2321 * - {@link #event-opening} is emitted with `closing` promise
2322 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2323 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2324 * - `hold` progress notification is emitted from opening promise
2325 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2326 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2327 * - `teardown` progress notification is emitted from opening promise
2328 * - Closing promise is resolved
2329 * - Window is now closed
2332 * @param {Object} [config] Configuration options
2333 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2334 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2336 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2337 // Configuration initialization
2338 config
= config
|| {};
2340 // Parent constructor
2341 OO
.ui
.WindowManager
.super.call( this, config
);
2343 // Mixin constructors
2344 OO
.EventEmitter
.call( this );
2347 this.factory
= config
.factory
;
2348 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2350 this.opening
= null;
2352 this.closing
= null;
2354 this.currentWindow
= null;
2355 this.$ariaHidden
= null;
2356 this.requestedSize
= null;
2357 this.onWindowResizeTimeout
= null;
2358 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
2359 this.afterWindowResizeHandler
= OO
.ui
.bind( this.afterWindowResize
, this );
2360 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
2361 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
2364 this.$element
.on( 'mousedown', false );
2368 .addClass( 'oo-ui-windowManager' )
2369 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2374 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2375 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2380 * Window is opening.
2382 * Fired when the window begins to be opened.
2385 * @param {OO.ui.Window} win Window that's being opened
2386 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2387 * resolved the first argument will be a promise which will be resolved when the window begins
2388 * closing, the second argument will be the opening data; progress notifications will be fired on
2389 * the promise for `setup` and `ready` when those processes are completed respectively.
2390 * @param {Object} data Window opening data
2394 * Window is closing.
2396 * Fired when the window begins to be closed.
2399 * @param {OO.ui.Window} win Window that's being closed
2400 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2401 * is resolved the first argument will be a the closing data; progress notifications will be fired
2402 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2403 * @param {Object} data Window closing data
2406 /* Static Properties */
2409 * Map of symbolic size names and CSS properties.
2413 * @property {Object}
2415 OO
.ui
.WindowManager
.static.sizes
= {
2426 // These can be non-numeric because they are never used in calculations
2433 * Symbolic name of default size.
2435 * Default size is used if the window's requested size is not recognized.
2439 * @property {string}
2441 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2446 * Handle window resize events.
2448 * @param {jQuery.Event} e Window resize event
2450 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2451 clearTimeout( this.onWindowResizeTimeout
);
2452 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2456 * Handle window resize events.
2458 * @param {jQuery.Event} e Window resize event
2460 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2461 if ( this.currentWindow
) {
2462 this.updateWindowSize( this.currentWindow
);
2467 * Handle window mouse wheel events.
2469 * @param {jQuery.Event} e Mouse wheel event
2471 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2476 * Handle document key down events.
2478 * @param {jQuery.Event} e Key down event
2480 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2481 switch ( e
.which
) {
2482 case OO
.ui
.Keys
.PAGEUP
:
2483 case OO
.ui
.Keys
.PAGEDOWN
:
2484 case OO
.ui
.Keys
.END
:
2485 case OO
.ui
.Keys
.HOME
:
2486 case OO
.ui
.Keys
.LEFT
:
2488 case OO
.ui
.Keys
.RIGHT
:
2489 case OO
.ui
.Keys
.DOWN
:
2490 // Prevent any key events that might cause scrolling
2496 * Check if window is opening.
2498 * @return {boolean} Window is opening
2500 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2501 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2505 * Check if window is closing.
2507 * @return {boolean} Window is closing
2509 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2510 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2514 * Check if window is opened.
2516 * @return {boolean} Window is opened
2518 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2519 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2523 * Check if a window is being managed.
2525 * @param {OO.ui.Window} win Window to check
2526 * @return {boolean} Window is being managed
2528 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2531 for ( name
in this.windows
) {
2532 if ( this.windows
[name
] === win
) {
2541 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2543 * @param {OO.ui.Window} win Window being opened
2544 * @param {Object} [data] Window opening data
2545 * @return {number} Milliseconds to wait
2547 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2552 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2554 * @param {OO.ui.Window} win Window being opened
2555 * @param {Object} [data] Window opening data
2556 * @return {number} Milliseconds to wait
2558 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2563 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2565 * @param {OO.ui.Window} win Window being closed
2566 * @param {Object} [data] Window closing data
2567 * @return {number} Milliseconds to wait
2569 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2574 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2576 * @param {OO.ui.Window} win Window being closed
2577 * @param {Object} [data] Window closing data
2578 * @return {number} Milliseconds to wait
2580 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2581 return this.modal
? 250 : 0;
2585 * Get managed window by symbolic name.
2587 * If window is not yet instantiated, it will be instantiated and added automatically.
2589 * @param {string} name Symbolic window name
2590 * @return {jQuery.Promise} Promise resolved when window is ready to be accessed; when resolved the
2591 * first argument is an OO.ui.Window; when rejected the first argument is an OO.ui.Error
2592 * @throws {Error} If the symbolic name is unrecognized by the factory
2593 * @throws {Error} If the symbolic name unrecognized as a managed window
2595 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2596 var deferred
= $.Deferred(),
2597 win
= this.windows
[name
];
2599 if ( !( win
instanceof OO
.ui
.Window
) ) {
2600 if ( this.factory
) {
2601 if ( !this.factory
.lookup( name
) ) {
2602 deferred
.reject( new OO
.ui
.Error(
2603 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2606 win
= this.factory
.create( name
, this, { '$': this.$ } );
2607 this.addWindows( [ win
] ).then(
2608 OO
.ui
.bind( deferred
.resolve
, deferred
, win
),
2613 deferred
.reject( new OO
.ui
.Error(
2614 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2618 deferred
.resolve( win
);
2621 return deferred
.promise();
2625 * Get current window.
2627 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2629 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2630 return this.currentWindow
;
2636 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2637 * @param {Object} [data] Window opening data
2638 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2639 * for more details about the `opening` promise
2642 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2645 opening
= $.Deferred();
2647 // Argument handling
2648 if ( typeof win
=== 'string' ) {
2649 return this.getWindow( win
).then( function ( win
) {
2650 return manager
.openWindow( win
, data
);
2655 if ( !this.hasWindow( win
) ) {
2656 opening
.reject( new OO
.ui
.Error(
2657 'Cannot open window: window is not attached to manager'
2662 if ( opening
.state() !== 'rejected' ) {
2663 // Begin loading the window if it's not loaded already - may take noticable time and we want
2664 // too do this in paralell with any preparatory actions
2665 preparing
.push( win
.load() );
2667 if ( this.opening
|| this.opened
) {
2668 // If a window is currently opening or opened, close it first
2669 preparing
.push( this.closeWindow( this.currentWindow
) );
2670 } else if ( this.closing
) {
2671 // If a window is currently closing, wait for it to complete
2672 preparing
.push( this.closing
);
2675 $.when
.apply( $, preparing
).done( function () {
2676 if ( manager
.modal
) {
2677 manager
.$( manager
.getElementDocument() ).on( {
2678 // Prevent scrolling by keys in top-level window
2679 'keydown': manager
.onDocumentKeyDownHandler
2681 manager
.$( manager
.getElementWindow() ).on( {
2682 // Prevent scrolling by wheel in top-level window
2683 'mousewheel': manager
.onWindowMouseWheelHandler
,
2684 // Start listening for top-level window dimension changes
2685 'orientationchange resize': manager
.onWindowResizeHandler
2687 // Hide other content from screen readers
2688 manager
.$ariaHidden
= $( 'body' )
2690 .not( manager
.$element
.parentsUntil( 'body' ).last() )
2691 .attr( 'aria-hidden', '' );
2693 manager
.currentWindow
= win
;
2694 manager
.opening
= opening
;
2695 manager
.emit( 'opening', win
, opening
, data
);
2696 manager
.updateWindowSize( win
);
2697 setTimeout( function () {
2698 win
.setup( data
).then( function () {
2699 manager
.opening
.notify( { 'state': 'setup' } );
2700 setTimeout( function () {
2701 win
.ready( data
).then( function () {
2702 manager
.opening
.notify( { 'state': 'ready' } );
2703 manager
.opening
= null;
2704 manager
.opened
= $.Deferred();
2705 opening
.resolve( manager
.opened
.promise(), data
);
2707 }, manager
.getReadyDelay() );
2709 }, manager
.getSetupDelay() );
2719 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2720 * @param {Object} [data] Window closing data
2721 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-closing}
2722 * for more details about the `closing` promise
2723 * @throws {Error} If no window by that name is being managed
2726 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2729 closing
= $.Deferred(),
2730 opened
= this.opened
;
2732 // Argument handling
2733 if ( typeof win
=== 'string' ) {
2734 win
= this.windows
[win
];
2735 } else if ( !this.hasWindow( win
) ) {
2741 closing
.reject( new OO
.ui
.Error(
2742 'Cannot close window: window is not attached to manager'
2744 } else if ( win
!== this.currentWindow
) {
2745 closing
.reject( new OO
.ui
.Error(
2746 'Cannot close window: window already closed with different data'
2748 } else if ( this.closing
) {
2749 closing
.reject( new OO
.ui
.Error(
2750 'Cannot close window: window already closing with different data'
2755 if ( closing
.state() !== 'rejected' ) {
2756 if ( this.opening
) {
2757 // If the window is currently opening, close it when it's done
2758 preparing
.push( this.opening
);
2762 $.when
.apply( $, preparing
).done( function () {
2763 manager
.closing
= closing
;
2764 manager
.emit( 'closing', win
, closing
, data
);
2765 manager
.opened
= null;
2766 opened
.resolve( closing
.promise(), data
);
2767 setTimeout( function () {
2768 win
.hold( data
).then( function () {
2769 closing
.notify( { 'state': 'hold' } );
2770 setTimeout( function () {
2771 win
.teardown( data
).then( function () {
2772 closing
.notify( { 'state': 'teardown' } );
2773 if ( manager
.modal
) {
2774 manager
.$( manager
.getElementDocument() ).off( {
2775 // Allow scrolling by keys in top-level window
2776 'keydown': manager
.onDocumentKeyDownHandler
2778 manager
.$( manager
.getElementWindow() ).off( {
2779 // Allow scrolling by wheel in top-level window
2780 'mousewheel': manager
.onWindowMouseWheelHandler
,
2781 // Stop listening for top-level window dimension changes
2782 'orientationchange resize': manager
.onWindowResizeHandler
2785 // Restore screen reader visiblity
2786 if ( manager
.$ariaHidden
) {
2787 manager
.$ariaHidden
.removeAttr( 'aria-hidden' );
2788 manager
.$ariaHidden
= null;
2790 manager
.closing
= null;
2791 manager
.currentWindow
= null;
2792 closing
.resolve( data
);
2794 }, manager
.getTeardownDelay() );
2796 }, manager
.getHoldDelay() );
2806 * If the window manager is attached to the DOM then windows will be automatically loaded as they
2809 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2810 * @return {jQuery.Promise} Promise resolved when all windows are added
2811 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2812 * a statically configured symbolic name
2814 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2815 var i
, len
, win
, name
, list
,
2818 if ( $.isArray( windows
) ) {
2819 // Convert to map of windows by looking up symbolic names from static configuration
2821 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2822 name
= windows
[i
].constructor.static.name
;
2823 if ( typeof name
!== 'string' ) {
2824 throw new Error( 'Cannot add window' );
2826 list
[name
] = windows
[i
];
2828 } else if ( $.isPlainObject( windows
) ) {
2833 for ( name
in list
) {
2835 this.windows
[name
] = win
;
2836 this.$element
.append( win
.$element
);
2838 if ( this.isElementAttached() ) {
2839 promises
.push( win
.load() );
2843 return $.when
.apply( $, promises
);
2849 * Windows will be closed before they are removed.
2851 * @param {string} name Symbolic name of window to remove
2852 * @return {jQuery.Promise} Promise resolved when window is closed and removed
2853 * @throws {Error} If windows being removed are not being managed
2855 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
2856 var i
, len
, win
, name
,
2859 cleanup = function ( name
, win
) {
2860 delete manager
.windows
[name
];
2861 win
.$element
.detach();
2864 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
2866 win
= this.windows
[name
];
2868 throw new Error( 'Cannot remove window' );
2870 promises
.push( this.closeWindow( name
).then( OO
.ui
.bind( cleanup
, null, name
, win
) ) );
2873 return $.when
.apply( $, promises
);
2877 * Remove all windows.
2879 * Windows will be closed before they are removed.
2881 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
2883 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
2884 return this.removeWindows( Object
.keys( this.windows
) );
2890 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
2894 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
2895 // Bypass for non-current, and thus invisible, windows
2896 if ( win
!== this.currentWindow
) {
2900 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
2901 sizes
= this.constructor.static.sizes
,
2902 size
= win
.getSize();
2904 if ( !sizes
[size
] ) {
2905 size
= this.constructor.static.defaultSize
;
2907 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
2911 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
2912 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
2913 win
.setDimensions( sizes
[size
] );
2923 * @param {string|jQuery} message Description of error
2924 * @param {Object} [config] Configuration options
2925 * @cfg {boolean} [recoverable=true] Error is recoverable
2927 OO
.ui
.Error
= function OoUiElement( message
, config
) {
2928 // Configuration initialization
2929 config
= config
|| {};
2932 this.message
= message
instanceof jQuery
? message
: String( message
);
2933 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
2938 OO
.initClass( OO
.ui
.Error
);
2943 * Check if error can be recovered from.
2945 * @return {boolean} Error is recoverable
2947 OO
.ui
.Error
.prototype.isRecoverable = function () {
2948 return this.recoverable
;
2952 * Get error message as DOM nodes.
2954 * @return {jQuery} Error message in DOM nodes
2956 OO
.ui
.Error
.prototype.getMessage = function () {
2957 return this.message
instanceof jQuery
?
2958 this.message
.clone() :
2959 $( '<div>' ).text( this.message
).contents();
2963 * Get error message as text.
2965 * @return {string} Error message
2967 OO
.ui
.Error
.prototype.getMessageText = function () {
2968 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
2972 * A list of functions, called in sequence.
2974 * If a function added to a process returns boolean false the process will stop; if it returns an
2975 * object with a `promise` method the process will use the promise to either continue to the next
2976 * step when the promise is resolved or stop when the promise is rejected.
2981 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
2982 * call, see #createStep for more information
2983 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
2985 * @return {Object} Step object, with `callback` and `context` properties
2987 OO
.ui
.Process = function ( step
, context
) {
2992 if ( step
!== undefined ) {
2993 this.next( step
, context
);
2999 OO
.initClass( OO
.ui
.Process
);
3004 * Start the process.
3006 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3007 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3008 * process, the remaining steps will not be taken
3010 OO
.ui
.Process
.prototype.execute = function () {
3011 var i
, len
, promise
;
3014 * Continue execution.
3017 * @param {Array} step A function and the context it should be called in
3018 * @return {Function} Function that continues the process
3020 function proceed( step
) {
3021 return function () {
3022 // Execute step in the correct context
3024 result
= step
.callback
.call( step
.context
);
3026 if ( result
=== false ) {
3027 // Use rejected promise for boolean false results
3028 return $.Deferred().reject( [] ).promise();
3030 if ( typeof result
=== 'number' ) {
3032 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3034 // Use a delayed promise for numbers, expecting them to be in milliseconds
3035 deferred
= $.Deferred();
3036 setTimeout( deferred
.resolve
, result
);
3037 return deferred
.promise();
3039 if ( result
instanceof OO
.ui
.Error
) {
3040 // Use rejected promise for error
3041 return $.Deferred().reject( [ result
] ).promise();
3043 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3044 // Use rejected promise for list of errors
3045 return $.Deferred().reject( result
).promise();
3047 // Duck-type the object to see if it can produce a promise
3048 if ( result
&& $.isFunction( result
.promise
) ) {
3049 // Use a promise generated from the result
3050 return result
.promise();
3052 // Use resolved promise for other results
3053 return $.Deferred().resolve().promise();
3057 if ( this.steps
.length
) {
3058 // Generate a chain reaction of promises
3059 promise
= proceed( this.steps
[0] )();
3060 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3061 promise
= promise
.then( proceed( this.steps
[i
] ) );
3064 promise
= $.Deferred().resolve().promise();
3071 * Create a process step.
3074 * @param {number|jQuery.Promise|Function} step
3076 * - Number of milliseconds to wait; or
3077 * - Promise to wait to be resolved; or
3078 * - Function to execute
3079 * - If it returns boolean false the process will stop
3080 * - If it returns an object with a `promise` method the process will use the promise to either
3081 * continue to the next step when the promise is resolved or stop when the promise is rejected
3082 * - If it returns a number, the process will wait for that number of milliseconds before
3084 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3086 * @return {Object} Step object, with `callback` and `context` properties
3088 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3089 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3091 'callback': function () {
3097 if ( $.isFunction( step
) ) {
3103 throw new Error( 'Cannot create process step: number, promise or function expected' );
3107 * Add step to the beginning of the process.
3109 * @inheritdoc #createStep
3110 * @return {OO.ui.Process} this
3113 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3114 this.steps
.unshift( this.createStep( step
, context
) );
3119 * Add step to the end of the process.
3121 * @inheritdoc #createStep
3122 * @return {OO.ui.Process} this
3125 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3126 this.steps
.push( this.createStep( step
, context
) );
3131 * Factory for tools.
3134 * @extends OO.Factory
3137 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3138 // Parent constructor
3139 OO
.ui
.ToolFactory
.super.call( this );
3144 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3149 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3150 var i
, len
, included
, promoted
, demoted
,
3154 // Collect included and not excluded tools
3155 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3158 promoted
= this.extract( promote
, used
);
3159 demoted
= this.extract( demote
, used
);
3162 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3163 if ( !used
[included
[i
]] ) {
3164 auto
.push( included
[i
] );
3168 return promoted
.concat( auto
).concat( demoted
);
3172 * Get a flat list of names from a list of names or groups.
3174 * Tools can be specified in the following ways:
3176 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3177 * - All tools in a group: `{ 'group': 'group-name' }`
3178 * - All tools: `'*'`
3181 * @param {Array|string} collection List of tools
3182 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3183 * names will be added as properties
3184 * @return {string[]} List of extracted names
3186 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3187 var i
, len
, item
, name
, tool
,
3190 if ( collection
=== '*' ) {
3191 for ( name
in this.registry
) {
3192 tool
= this.registry
[name
];
3194 // Only add tools by group name when auto-add is enabled
3195 tool
.static.autoAddToCatchall
&&
3196 // Exclude already used tools
3197 ( !used
|| !used
[name
] )
3205 } else if ( $.isArray( collection
) ) {
3206 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3207 item
= collection
[i
];
3208 // Allow plain strings as shorthand for named tools
3209 if ( typeof item
=== 'string' ) {
3210 item
= { 'name': item
};
3212 if ( OO
.isPlainObject( item
) ) {
3214 for ( name
in this.registry
) {
3215 tool
= this.registry
[name
];
3217 // Include tools with matching group
3218 tool
.static.group
=== item
.group
&&
3219 // Only add tools by group name when auto-add is enabled
3220 tool
.static.autoAddToGroup
&&
3221 // Exclude already used tools
3222 ( !used
|| !used
[name
] )
3230 // Include tools with matching name and exclude already used tools
3231 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3232 names
.push( item
.name
);
3234 used
[item
.name
] = true;
3244 * Factory for tool groups.
3247 * @extends OO.Factory
3250 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3251 // Parent constructor
3252 OO
.Factory
.call( this );
3255 defaultClasses
= this.constructor.static.getDefaultClasses();
3257 // Register default toolgroups
3258 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3259 this.register( defaultClasses
[i
] );
3265 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3267 /* Static Methods */
3270 * Get a default set of classes to be registered on construction
3272 * @return {Function[]} Default classes
3274 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3277 OO
.ui
.ListToolGroup
,
3283 * Element with a button.
3285 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3286 * and access keys for accessibility purposes.
3292 * @param {jQuery} $button Button node, assigned to #$button
3293 * @param {Object} [config] Configuration options
3294 * @cfg {boolean} [framed=true] Render button with a frame
3295 * @cfg {number} [tabIndex=0] Button's tab index, use null to have no tabIndex
3296 * @cfg {string} [accessKey] Button's access key
3298 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
3299 // Configuration initialization
3300 config
= config
|| {};
3303 this.$button
= $button
;
3304 this.tabIndex
= null;
3306 this.active
= false;
3307 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
3310 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
3313 this.$element
.addClass( 'oo-ui-buttonedElement' );
3315 .addClass( 'oo-ui-buttonedElement-button' )
3316 .attr( 'role', 'button' );
3317 this.setTabIndex( config
.tabIndex
|| 0 );
3318 this.setAccessKey( config
.accessKey
);
3319 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3324 OO
.initClass( OO
.ui
.ButtonedElement
);
3326 /* Static Properties */
3329 * Cancel mouse down events.
3333 * @property {boolean}
3335 OO
.ui
.ButtonedElement
.static.cancelButtonMouseDownEvents
= true;
3340 * Handles mouse down events.
3342 * @param {jQuery.Event} e Mouse down event
3344 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function ( e
) {
3345 if ( this.isDisabled() || e
.which
!== 1 ) {
3348 // tabIndex should generally be interacted with via the property, but it's not possible to
3349 // reliably unset a tabIndex via a property so we use the (lowercase) "tabindex" attribute
3350 this.tabIndex
= this.$button
.attr( 'tabindex' );
3352 // Remove the tab-index while the button is down to prevent the button from stealing focus
3353 .removeAttr( 'tabindex' )
3354 .addClass( 'oo-ui-buttonedElement-pressed' );
3355 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3356 // reliably reapply the tabindex and remove the pressed class
3357 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3358 // Prevent change of focus unless specifically configured otherwise
3359 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3365 * Handles mouse up events.
3367 * @param {jQuery.Event} e Mouse up event
3369 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function ( e
) {
3370 if ( this.isDisabled() || e
.which
!== 1 ) {
3374 // Restore the tab-index after the button is up to restore the button's accesssibility
3375 .attr( 'tabindex', this.tabIndex
)
3376 .removeClass( 'oo-ui-buttonedElement-pressed' );
3377 // Stop listening for mouseup, since we only needed this once
3378 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3384 * @param {boolean} [framed] Make button framed, omit to toggle
3387 OO
.ui
.ButtonedElement
.prototype.toggleFramed = function ( framed
) {
3388 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3389 if ( framed
!== this.framed
) {
3390 this.framed
= framed
;
3392 .toggleClass( 'oo-ui-buttonedElement-frameless', !framed
)
3393 .toggleClass( 'oo-ui-buttonedElement-framed', framed
);
3402 * @param {number|null} tabIndex Button's tab index, use null to remove
3405 OO
.ui
.ButtonedElement
.prototype.setTabIndex = function ( tabIndex
) {
3406 if ( typeof tabIndex
=== 'number' && tabIndex
>= 0 ) {
3407 this.$button
.attr( 'tabindex', tabIndex
);
3409 this.$button
.removeAttr( 'tabindex' );
3417 * @param {string} accessKey Button's access key, use empty string to remove
3420 OO
.ui
.ButtonedElement
.prototype.setAccessKey = function ( accessKey
) {
3421 if ( typeof accessKey
=== 'string' && accessKey
.length
) {
3422 this.$button
.attr( 'accesskey', accessKey
);
3424 this.$button
.removeAttr( 'accesskey' );
3432 * @param {boolean} [value] Make button active
3435 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
3436 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
3441 * Element that can be automatically clipped to visible boundaies.
3447 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
3448 * @param {Object} [config] Configuration options
3450 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
3451 // Configuration initialization
3452 config
= config
|| {};
3455 this.$clippable
= $clippable
;
3456 this.clipping
= false;
3457 this.clipped
= false;
3458 this.$clippableContainer
= null;
3459 this.$clippableScroller
= null;
3460 this.$clippableWindow
= null;
3461 this.idealWidth
= null;
3462 this.idealHeight
= null;
3463 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
3464 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
3467 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
3475 * @param {boolean} value Enable clipping
3478 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
3481 if ( this.clipping
!== value
) {
3482 this.clipping
= value
;
3483 if ( this.clipping
) {
3484 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
3485 // If the clippable container is the body, we have to listen to scroll events and check
3486 // jQuery.scrollTop on the window because of browser inconsistencies
3487 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
3488 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
3489 this.$clippableContainer
;
3490 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
3491 this.$clippableWindow
= this.$( this.getElementWindow() )
3492 .on( 'resize', this.onClippableWindowResizeHandler
);
3493 // Initial clip after visible
3494 setTimeout( OO
.ui
.bind( this.clip
, this ) );
3496 this.$clippableContainer
= null;
3497 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
3498 this.$clippableScroller
= null;
3499 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
3500 this.$clippableWindow
= null;
3508 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
3510 * @return {boolean} Element will be clipped to the visible area
3512 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
3513 return this.clipping
;
3517 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
3519 * @return {boolean} Part of the element is being clipped
3521 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
3522 return this.clipped
;
3526 * Set the ideal size.
3528 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
3529 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
3531 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
3532 this.idealWidth
= width
;
3533 this.idealHeight
= height
;
3537 * Clip element to visible boundaries and allow scrolling when needed.
3539 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
3540 * overlapped by, the visible area of the nearest scrollable container.
3544 OO
.ui
.ClippableElement
.prototype.clip = function () {
3545 if ( !this.clipping
) {
3546 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
3551 cOffset
= this.$clippable
.offset(),
3552 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
3553 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
3554 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
3555 scrollTop
= this.$clippableScroller
.scrollTop(),
3556 scrollLeft
= this.$clippableScroller
.scrollLeft(),
3557 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
3558 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
3559 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
3560 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
3561 clipWidth
= desiredWidth
< naturalWidth
,
3562 clipHeight
= desiredHeight
< naturalHeight
;
3565 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
3567 this.$clippable
.css( 'width', this.idealWidth
|| '' );
3568 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
3569 this.$clippable
.css( 'overflow-x', '' );
3572 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
3574 this.$clippable
.css( 'height', this.idealHeight
|| '' );
3575 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
3576 this.$clippable
.css( 'overflow-y', '' );
3579 this.clipped
= clipWidth
|| clipHeight
;
3585 * Element with named flags that can be added, removed, listed and checked.
3587 * A flag, when set, adds a CSS class on the `$element` by combing `oo-ui-flaggableElement-` with
3588 * the flag name. Flags are primarily useful for styling.
3594 * @param {Object} [config] Configuration options
3595 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
3597 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
3598 // Config initialization
3599 config
= config
|| {};
3605 this.setFlags( config
.flags
);
3612 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
3613 * added/removed properties
3619 * Check if a flag is set.
3621 * @param {string} flag Name of flag
3622 * @return {boolean} Has flag
3624 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
3625 return flag
in this.flags
;
3629 * Get the names of all flags set.
3631 * @return {string[]} flags Flag names
3633 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
3634 return Object
.keys( this.flags
);
3643 OO
.ui
.FlaggableElement
.prototype.clearFlags = function () {
3646 classPrefix
= 'oo-ui-flaggableElement-';
3648 for ( flag
in this.flags
) {
3649 changes
[flag
] = false;
3650 delete this.flags
[flag
];
3651 this.$element
.removeClass( classPrefix
+ flag
);
3654 this.emit( 'flag', changes
);
3660 * Add one or more flags.
3662 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
3663 * keyed by flag name containing boolean set/remove instructions.
3667 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
3670 classPrefix
= 'oo-ui-flaggableElement-';
3672 if ( typeof flags
=== 'string' ) {
3674 this.flags
[flags
] = true;
3675 this.$element
.addClass( classPrefix
+ flags
);
3676 } else if ( $.isArray( flags
) ) {
3677 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
3680 changes
[flag
] = true;
3681 this.flags
[flag
] = true;
3682 this.$element
.addClass( classPrefix
+ flag
);
3684 } else if ( OO
.isPlainObject( flags
) ) {
3685 for ( flag
in flags
) {
3686 if ( flags
[flag
] ) {
3688 changes
[flag
] = true;
3689 this.flags
[flag
] = true;
3690 this.$element
.addClass( classPrefix
+ flag
);
3693 changes
[flag
] = false;
3694 delete this.flags
[flag
];
3695 this.$element
.removeClass( classPrefix
+ flag
);
3700 this.emit( 'flag', changes
);
3706 * Element containing a sequence of child elements.
3712 * @param {jQuery} $group Container node, assigned to #$group
3713 * @param {Object} [config] Configuration options
3715 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
3717 config
= config
|| {};
3720 this.$group
= $group
;
3722 this.aggregateItemEvents
= {};
3730 * @return {OO.ui.Element[]} Items
3732 OO
.ui
.GroupElement
.prototype.getItems = function () {
3733 return this.items
.slice( 0 );
3737 * Add an aggregate item event.
3739 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3740 * and with an additional leading parameter containing the item that emitted the original event.
3741 * Other arguments that were emitted from the original event are passed through.
3743 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3744 * event, use null value to remove aggregation
3745 * @throws {Error} If aggregation already exists
3747 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3748 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3750 for ( itemEvent
in events
) {
3751 groupEvent
= events
[itemEvent
];
3753 // Remove existing aggregated event
3754 if ( itemEvent
in this.aggregateItemEvents
) {
3755 // Don't allow duplicate aggregations
3757 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3759 // Remove event aggregation from existing items
3760 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3761 item
= this.items
[i
];
3762 if ( item
.connect
&& item
.disconnect
) {
3764 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3765 item
.disconnect( this, remove
);
3768 // Prevent future items from aggregating event
3769 delete this.aggregateItemEvents
[itemEvent
];
3772 // Add new aggregate event
3774 // Make future items aggregate event
3775 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3776 // Add event aggregation to existing items
3777 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3778 item
= this.items
[i
];
3779 if ( item
.connect
&& item
.disconnect
) {
3781 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3782 item
.connect( this, add
);
3792 * @param {OO.ui.Element[]} items Item
3793 * @param {number} [index] Index to insert items at
3796 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3797 var i
, len
, item
, event
, events
, currentIndex
,
3800 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3803 // Check if item exists then remove it first, effectively "moving" it
3804 currentIndex
= $.inArray( item
, this.items
);
3805 if ( currentIndex
>= 0 ) {
3806 this.removeItems( [ item
] );
3807 // Adjust index to compensate for removal
3808 if ( currentIndex
< index
) {
3813 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3815 for ( event
in this.aggregateItemEvents
) {
3816 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3818 item
.connect( this, events
);
3820 item
.setElementGroup( this );
3821 itemElements
.push( item
.$element
.get( 0 ) );
3824 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3825 this.$group
.append( itemElements
);
3826 this.items
.push
.apply( this.items
, items
);
3827 } else if ( index
=== 0 ) {
3828 this.$group
.prepend( itemElements
);
3829 this.items
.unshift
.apply( this.items
, items
);
3831 this.items
[index
].$element
.before( itemElements
);
3832 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3841 * Items will be detached, not removed, so they can be used later.
3843 * @param {OO.ui.Element[]} items Items to remove
3846 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3847 var i
, len
, item
, index
, remove
, itemEvent
;
3849 // Remove specific items
3850 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3852 index
= $.inArray( item
, this.items
);
3853 if ( index
!== -1 ) {
3855 item
.connect
&& item
.disconnect
&&
3856 !$.isEmptyObject( this.aggregateItemEvents
)
3859 if ( itemEvent
in this.aggregateItemEvents
) {
3860 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3862 item
.disconnect( this, remove
);
3864 item
.setElementGroup( null );
3865 this.items
.splice( index
, 1 );
3866 item
.$element
.detach();
3876 * Items will be detached, not removed, so they can be used later.
3880 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3881 var i
, len
, item
, remove
, itemEvent
;
3884 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3885 item
= this.items
[i
];
3887 item
.connect
&& item
.disconnect
&&
3888 !$.isEmptyObject( this.aggregateItemEvents
)
3891 if ( itemEvent
in this.aggregateItemEvents
) {
3892 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3894 item
.disconnect( this, remove
);
3896 item
.setElementGroup( null );
3897 item
.$element
.detach();
3905 * Element containing an icon.
3907 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
3908 * a control or convey information in a more space efficient way. Icons should rarely be used
3909 * without labels; such as in a toolbar where space is at a premium or within a context where the
3910 * meaning is very clear to the user.
3916 * @param {jQuery} $icon Icon node, assigned to #$icon
3917 * @param {Object} [config] Configuration options
3918 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
3919 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3922 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
3923 // Config intialization
3924 config
= config
|| {};
3931 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
3932 this.setIcon( config
.icon
|| this.constructor.static.icon
);
3937 OO
.initClass( OO
.ui
.IconedElement
);
3939 /* Static Properties */
3944 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
3946 * For i18n purposes, this property can be an object containing a `default` icon name property and
3947 * additional icon names keyed by language code.
3949 * Example of i18n icon definition:
3950 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
3954 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
3955 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3958 OO
.ui
.IconedElement
.static.icon
= null;
3965 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
3966 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3970 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
3971 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
3974 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
3976 if ( typeof icon
=== 'string' ) {
3978 if ( icon
.length
) {
3979 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
3983 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
3991 * @return {string} Icon
3993 OO
.ui
.IconedElement
.prototype.getIcon = function () {
3998 * Element containing an indicator.
4000 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4001 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4002 * instead of performing an action directly, or an item in a list which has errors that need to be
4009 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
4010 * @param {Object} [config] Configuration options
4011 * @cfg {string} [indicator] Symbolic indicator name
4012 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
4014 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
4015 // Config intialization
4016 config
= config
|| {};
4019 this.$indicator
= $indicator
;
4020 this.indicator
= null;
4021 this.indicatorLabel
= null;
4024 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
4025 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4026 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4031 OO
.initClass( OO
.ui
.IndicatedElement
);
4033 /* Static Properties */
4040 * @property {string|null} Symbolic indicator name or null for no indicator
4042 OO
.ui
.IndicatedElement
.static.indicator
= null;
4049 * @property {string|Function|null} Indicator title text, a function that return text or null for no
4052 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
4059 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4062 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
4063 if ( this.indicator
) {
4064 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4065 this.indicator
= null;
4067 if ( typeof indicator
=== 'string' ) {
4068 indicator
= indicator
.trim();
4069 if ( indicator
.length
) {
4070 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4071 this.indicator
= indicator
;
4074 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
4080 * Set indicator label.
4082 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
4083 * for no indicator title
4086 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4087 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
4089 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
4090 this.$indicator
.attr( 'title', indicatorTitle
);
4092 this.$indicator
.removeAttr( 'title' );
4101 * @return {string} title Symbolic name of indicator
4103 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
4104 return this.indicator
;
4108 * Get indicator title.
4110 * @return {string} Indicator title text
4112 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
4113 return this.indicatorTitle
;
4117 * Element containing a label.
4123 * @param {jQuery} $label Label node, assigned to #$label
4124 * @param {Object} [config] Configuration options
4125 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4126 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4128 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
4129 // Config intialization
4130 config
= config
|| {};
4133 this.$label
= $label
;
4137 this.$label
.addClass( 'oo-ui-labeledElement-label' );
4138 this.setLabel( config
.label
|| this.constructor.static.label
);
4139 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4144 OO
.initClass( OO
.ui
.LabeledElement
);
4146 /* Static Properties */
4153 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
4156 OO
.ui
.LabeledElement
.static.label
= null;
4163 * An empty string will result in the label being hidden. A string containing only whitespace will
4164 * be converted to a single
4166 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
4167 * text; or null for no label
4170 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
4173 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
4174 if ( typeof label
=== 'string' && label
.length
) {
4175 if ( label
.match( /^\s*$/ ) ) {
4176 // Convert whitespace only string to a single non-breaking space
4177 this.$label
.html( ' ' );
4179 this.$label
.text( label
);
4181 } else if ( label
instanceof jQuery
) {
4182 this.$label
.empty().append( label
);
4184 this.$label
.empty();
4187 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
4188 this.$label
.css( 'display', empty
? 'none' : '' );
4196 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4197 * text; or null for no label
4199 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
4208 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
4209 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4210 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
4216 * Element containing an OO.ui.PopupWidget object.
4222 * @param {Object} [config] Configuration options
4223 * @cfg {Object} [popup] Configuration to pass to popup
4224 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4226 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
4227 // Configuration initialization
4228 config
= config
|| {};
4231 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4232 { 'autoClose': true },
4234 { '$': this.$, '$autoCloseIgnore': this.$element
}
4243 * @return {OO.ui.PopupWidget} Popup widget
4245 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
4250 * Element with a title.
4252 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4253 * not visible on touch devices.
4259 * @param {jQuery} $label Titled node, assigned to #$titled
4260 * @param {Object} [config] Configuration options
4261 * @cfg {string|Function} [title] Title text or a function that returns text
4263 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
4264 // Config intialization
4265 config
= config
|| {};
4268 this.$titled
= $titled
;
4272 this.setTitle( config
.title
|| this.constructor.static.title
);
4277 OO
.initClass( OO
.ui
.TitledElement
);
4279 /* Static Properties */
4286 * @property {string|Function} Title text or a function that returns text
4288 OO
.ui
.TitledElement
.static.title
= null;
4295 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4298 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4299 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
4301 if ( typeof title
=== 'string' && title
.length
) {
4302 this.$titled
.attr( 'title', title
);
4304 this.$titled
.removeAttr( 'title' );
4313 * @return {string} Title string
4315 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4320 * Generic toolbar tool.
4324 * @extends OO.ui.Widget
4325 * @mixins OO.ui.IconedElement
4328 * @param {OO.ui.ToolGroup} toolGroup
4329 * @param {Object} [config] Configuration options
4330 * @cfg {string|Function} [title] Title text or a function that returns text
4332 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4333 // Config intialization
4334 config
= config
|| {};
4336 // Parent constructor
4337 OO
.ui
.Tool
.super.call( this, config
);
4339 // Mixin constructors
4340 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4343 this.toolGroup
= toolGroup
;
4344 this.toolbar
= this.toolGroup
.getToolbar();
4345 this.active
= false;
4346 this.$title
= this.$( '<span>' );
4347 this.$link
= this.$( '<a>' );
4351 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
4354 this.$title
.addClass( 'oo-ui-tool-title' );
4356 .addClass( 'oo-ui-tool-link' )
4357 .append( this.$icon
, this.$title
)
4358 .prop( 'tabIndex', 0 )
4359 .attr( 'role', 'button' );
4361 .data( 'oo-ui-tool', this )
4363 'oo-ui-tool ' + 'oo-ui-tool-name-' +
4364 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
4366 .append( this.$link
);
4367 this.setTitle( config
.title
|| this.constructor.static.title
);
4372 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
4373 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
4381 /* Static Properties */
4387 OO
.ui
.Tool
.static.tagName
= 'span';
4390 * Symbolic name of tool.
4395 * @property {string}
4397 OO
.ui
.Tool
.static.name
= '';
4405 * @property {string}
4407 OO
.ui
.Tool
.static.group
= '';
4412 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
4413 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
4414 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
4415 * appended to the title if the tool is part of a bar tool group.
4420 * @property {string|Function} Title text or a function that returns text
4422 OO
.ui
.Tool
.static.title
= '';
4425 * Tool can be automatically added to catch-all groups.
4429 * @property {boolean}
4431 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
4434 * Tool can be automatically added to named groups.
4437 * @property {boolean}
4440 OO
.ui
.Tool
.static.autoAddToGroup
= true;
4443 * Check if this tool is compatible with given data.
4447 * @param {Mixed} data Data to check
4448 * @return {boolean} Tool can be used with data
4450 OO
.ui
.Tool
.static.isCompatibleWith = function () {
4457 * Handle the toolbar state being updated.
4459 * This is an abstract method that must be overridden in a concrete subclass.
4463 OO
.ui
.Tool
.prototype.onUpdateState = function () {
4465 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
4470 * Handle the tool being selected.
4472 * This is an abstract method that must be overridden in a concrete subclass.
4476 OO
.ui
.Tool
.prototype.onSelect = function () {
4478 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
4483 * Check if the button is active.
4485 * @param {boolean} Button is active
4487 OO
.ui
.Tool
.prototype.isActive = function () {
4492 * Make the button appear active or inactive.
4494 * @param {boolean} state Make button appear active
4496 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
4497 this.active
= !!state
;
4498 if ( this.active
) {
4499 this.$element
.addClass( 'oo-ui-tool-active' );
4501 this.$element
.removeClass( 'oo-ui-tool-active' );
4506 * Get the tool title.
4508 * @param {string|Function} title Title text or a function that returns text
4511 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
4512 this.title
= OO
.ui
.resolveMsg( title
);
4518 * Get the tool title.
4520 * @return {string} Title text
4522 OO
.ui
.Tool
.prototype.getTitle = function () {
4527 * Get the tool's symbolic name.
4529 * @return {string} Symbolic name of tool
4531 OO
.ui
.Tool
.prototype.getName = function () {
4532 return this.constructor.static.name
;
4538 OO
.ui
.Tool
.prototype.updateTitle = function () {
4539 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
4540 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
4541 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
4548 .addClass( 'oo-ui-tool-accel' )
4552 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
4553 tooltipParts
.push( this.title
);
4555 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
4556 tooltipParts
.push( accel
);
4558 if ( tooltipParts
.length
) {
4559 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
4561 this.$link
.removeAttr( 'title' );
4568 OO
.ui
.Tool
.prototype.destroy = function () {
4569 this.toolbar
.disconnect( this );
4570 this.$element
.remove();
4574 * Collection of tool groups.
4577 * @extends OO.ui.Element
4578 * @mixins OO.EventEmitter
4579 * @mixins OO.ui.GroupElement
4582 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
4583 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
4584 * @param {Object} [config] Configuration options
4585 * @cfg {boolean} [actions] Add an actions section opposite to the tools
4586 * @cfg {boolean} [shadow] Add a shadow below the toolbar
4588 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
4589 // Configuration initialization
4590 config
= config
|| {};
4592 // Parent constructor
4593 OO
.ui
.Toolbar
.super.call( this, config
);
4595 // Mixin constructors
4596 OO
.EventEmitter
.call( this );
4597 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4600 this.toolFactory
= toolFactory
;
4601 this.toolGroupFactory
= toolGroupFactory
;
4604 this.$bar
= this.$( '<div>' );
4605 this.$actions
= this.$( '<div>' );
4606 this.initialized
= false;
4610 .add( this.$bar
).add( this.$group
).add( this.$actions
)
4611 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
4614 this.$group
.addClass( 'oo-ui-toolbar-tools' );
4615 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
4616 if ( config
.actions
) {
4617 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
4618 this.$bar
.append( this.$actions
);
4620 this.$bar
.append( '<div style="clear:both"></div>' );
4621 if ( config
.shadow
) {
4622 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
4624 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
4629 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
4630 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
4631 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
4636 * Get the tool factory.
4638 * @return {OO.ui.ToolFactory} Tool factory
4640 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
4641 return this.toolFactory
;
4645 * Get the tool group factory.
4647 * @return {OO.Factory} Tool group factory
4649 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
4650 return this.toolGroupFactory
;
4654 * Handles mouse down events.
4656 * @param {jQuery.Event} e Mouse down event
4658 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
4659 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
4660 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
4661 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
4667 * Sets up handles and preloads required information for the toolbar to work.
4668 * This must be called immediately after it is attached to a visible document.
4670 OO
.ui
.Toolbar
.prototype.initialize = function () {
4671 this.initialized
= true;
4677 * Tools can be specified in the following ways:
4679 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
4680 * - All tools in a group: `{ 'group': 'group-name' }`
4681 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
4683 * @param {Object.<string,Array>} groups List of tool group configurations
4684 * @param {Array|string} [groups.include] Tools to include
4685 * @param {Array|string} [groups.exclude] Tools to exclude
4686 * @param {Array|string} [groups.promote] Tools to promote to the beginning
4687 * @param {Array|string} [groups.demote] Tools to demote to the end
4689 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
4690 var i
, len
, type
, group
,
4692 defaultType
= 'bar';
4694 // Cleanup previous groups
4697 // Build out new groups
4698 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
4700 if ( group
.include
=== '*' ) {
4701 // Apply defaults to catch-all groups
4702 if ( group
.type
=== undefined ) {
4703 group
.type
= 'list';
4705 if ( group
.label
=== undefined ) {
4706 group
.label
= 'ooui-toolbar-more';
4709 // Check type has been registered
4710 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
4712 this.getToolGroupFactory().create( type
, this, $.extend( { '$': this.$ }, group
) )
4715 this.addItems( items
);
4719 * Remove all tools and groups from the toolbar.
4721 OO
.ui
.Toolbar
.prototype.reset = function () {
4726 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4727 this.items
[i
].destroy();
4733 * Destroys toolbar, removing event handlers and DOM elements.
4735 * Call this whenever you are done using a toolbar.
4737 OO
.ui
.Toolbar
.prototype.destroy = function () {
4739 this.$element
.remove();
4743 * Check if tool has not been used yet.
4745 * @param {string} name Symbolic name of tool
4746 * @return {boolean} Tool is available
4748 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
4749 return !this.tools
[name
];
4753 * Prevent tool from being used again.
4755 * @param {OO.ui.Tool} tool Tool to reserve
4757 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
4758 this.tools
[tool
.getName()] = tool
;
4762 * Allow tool to be used again.
4764 * @param {OO.ui.Tool} tool Tool to release
4766 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
4767 delete this.tools
[tool
.getName()];
4771 * Get accelerator label for tool.
4773 * This is a stub that should be overridden to provide access to accelerator information.
4775 * @param {string} name Symbolic name of tool
4776 * @return {string|undefined} Tool accelerator label if available
4778 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
4783 * Collection of tools.
4785 * Tools can be specified in the following ways:
4787 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
4788 * - All tools in a group: `{ 'group': 'group-name' }`
4789 * - All tools: `'*'`
4793 * @extends OO.ui.Widget
4794 * @mixins OO.ui.GroupElement
4797 * @param {OO.ui.Toolbar} toolbar
4798 * @param {Object} [config] Configuration options
4799 * @cfg {Array|string} [include=[]] List of tools to include
4800 * @cfg {Array|string} [exclude=[]] List of tools to exclude
4801 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
4802 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
4804 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
4805 // Configuration initialization
4806 config
= config
|| {};
4808 // Parent constructor
4809 OO
.ui
.ToolGroup
.super.call( this, config
);
4811 // Mixin constructors
4812 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4815 this.toolbar
= toolbar
;
4817 this.pressed
= null;
4818 this.autoDisabled
= false;
4819 this.include
= config
.include
|| [];
4820 this.exclude
= config
.exclude
|| [];
4821 this.promote
= config
.promote
|| [];
4822 this.demote
= config
.demote
|| [];
4823 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
4827 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
4828 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
4829 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
4830 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
4832 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
4833 this.aggregate( { 'disable': 'itemDisable' } );
4834 this.connect( this, { 'itemDisable': 'updateDisabled' } );
4837 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
4839 .addClass( 'oo-ui-toolGroup' )
4840 .append( this.$group
);
4846 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
4847 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
4855 /* Static Properties */
4858 * Show labels in tooltips.
4862 * @property {boolean}
4864 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
4867 * Show acceleration labels in tooltips.
4871 * @property {boolean}
4873 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
4876 * Automatically disable the toolgroup when all tools are disabled
4880 * @property {boolean}
4882 OO
.ui
.ToolGroup
.static.autoDisable
= true;
4889 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
4890 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
4896 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
4897 var i
, item
, allDisabled
= true;
4899 if ( this.constructor.static.autoDisable
) {
4900 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
4901 item
= this.items
[i
];
4902 if ( !item
.isDisabled() ) {
4903 allDisabled
= false;
4907 this.autoDisabled
= allDisabled
;
4909 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
4913 * Handle mouse down events.
4915 * @param {jQuery.Event} e Mouse down event
4917 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
4918 if ( !this.isDisabled() && e
.which
=== 1 ) {
4919 this.pressed
= this.getTargetTool( e
);
4920 if ( this.pressed
) {
4921 this.pressed
.setActive( true );
4922 this.getElementDocument().addEventListener(
4923 'mouseup', this.onCapturedMouseUpHandler
, true
4931 * Handle captured mouse up events.
4933 * @param {Event} e Mouse up event
4935 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
4936 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
4937 // onMouseUp may be called a second time, depending on where the mouse is when the button is
4938 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
4939 this.onMouseUp( e
);
4943 * Handle mouse up events.
4945 * @param {jQuery.Event} e Mouse up event
4947 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
4948 var tool
= this.getTargetTool( e
);
4950 if ( !this.isDisabled() && e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
4951 this.pressed
.onSelect();
4954 this.pressed
= null;
4959 * Handle mouse over events.
4961 * @param {jQuery.Event} e Mouse over event
4963 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
4964 var tool
= this.getTargetTool( e
);
4966 if ( this.pressed
&& this.pressed
=== tool
) {
4967 this.pressed
.setActive( true );
4972 * Handle mouse out events.
4974 * @param {jQuery.Event} e Mouse out event
4976 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
4977 var tool
= this.getTargetTool( e
);
4979 if ( this.pressed
&& this.pressed
=== tool
) {
4980 this.pressed
.setActive( false );
4985 * Get the closest tool to a jQuery.Event.
4987 * Only tool links are considered, which prevents other elements in the tool such as popups from
4988 * triggering tool group interactions.
4991 * @param {jQuery.Event} e
4992 * @return {OO.ui.Tool|null} Tool, `null` if none was found
4994 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
4996 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
4998 if ( $item
.length
) {
4999 tool
= $item
.parent().data( 'oo-ui-tool' );
5002 return tool
&& !tool
.isDisabled() ? tool
: null;
5006 * Handle tool registry register events.
5008 * If a tool is registered after the group is created, we must repopulate the list to account for:
5010 * - a tool being added that may be included
5011 * - a tool already included being overridden
5013 * @param {string} name Symbolic name of tool
5015 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5020 * Get the toolbar this group is in.
5022 * @return {OO.ui.Toolbar} Toolbar of group
5024 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5025 return this.toolbar
;
5029 * Add and remove tools based on configuration.
5031 OO
.ui
.ToolGroup
.prototype.populate = function () {
5032 var i
, len
, name
, tool
,
5033 toolFactory
= this.toolbar
.getToolFactory(),
5037 list
= this.toolbar
.getToolFactory().getTools(
5038 this.include
, this.exclude
, this.promote
, this.demote
5041 // Build a list of needed tools
5042 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5046 toolFactory
.lookup( name
) &&
5047 // Tool is available or is already in this group
5048 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5050 tool
= this.tools
[name
];
5052 // Auto-initialize tools on first use
5053 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5056 this.toolbar
.reserveTool( tool
);
5061 // Remove tools that are no longer needed
5062 for ( name
in this.tools
) {
5063 if ( !names
[name
] ) {
5064 this.tools
[name
].destroy();
5065 this.toolbar
.releaseTool( this.tools
[name
] );
5066 remove
.push( this.tools
[name
] );
5067 delete this.tools
[name
];
5070 if ( remove
.length
) {
5071 this.removeItems( remove
);
5073 // Update emptiness state
5075 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5077 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5079 // Re-add tools (moving existing ones to new locations)
5080 this.addItems( add
);
5081 // Disabled state may depend on items
5082 this.updateDisabled();
5086 * Destroy tool group.
5088 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5092 this.toolbar
.getToolFactory().disconnect( this );
5093 for ( name
in this.tools
) {
5094 this.toolbar
.releaseTool( this.tools
[name
] );
5095 this.tools
[name
].disconnect( this ).destroy();
5096 delete this.tools
[name
];
5098 this.$element
.remove();
5102 * Dialog for showing a message.
5105 * - Registers two actions by default (safe and primary).
5106 * - Renders action widgets in the footer.
5109 * @extends OO.ui.Dialog
5112 * @param {Object} [config] Configuration options
5114 OO
.ui
.MessageDialog
= function OoUiMessageDialog( manager
, config
) {
5115 // Parent constructor
5116 OO
.ui
.MessageDialog
.super.call( this, manager
, config
);
5119 this.verticalActionLayout
= null;
5122 this.$element
.addClass( 'oo-ui-messageDialog' );
5127 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5129 /* Static Properties */
5131 OO
.ui
.MessageDialog
.static.name
= 'message';
5133 OO
.ui
.MessageDialog
.static.size
= 'small';
5135 OO
.ui
.MessageDialog
.static.verbose
= false;
5140 * A confirmation dialog's title should describe what the progressive action will do. An alert
5141 * dialog's title should describe what event occured.
5145 * @property {jQuery|string|Function|null}
5147 OO
.ui
.MessageDialog
.static.title
= null;
5150 * A confirmation dialog's message should describe the consequences of the progressive action. An
5151 * alert dialog's message should describe why the event occured.
5155 * @property {jQuery|string|Function|null}
5157 OO
.ui
.MessageDialog
.static.message
= null;
5159 OO
.ui
.MessageDialog
.static.actions
= [
5160 { 'label': OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), 'flags': 'primary' },
5161 { 'label': OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), 'flags': 'safe' }
5169 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5171 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5175 * Toggle action layout between vertical and horizontal.
5177 * @param {boolean} [value] Layout actions vertically, omit to toggle
5180 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5181 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5183 if ( value
!== this.verticalActionLayout
) {
5184 this.verticalActionLayout
= value
;
5186 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5187 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5196 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5198 return new OO
.ui
.Process( function () {
5199 this.close( { 'action': action
} );
5202 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5208 * @param {Object} [data] Dialog opening data
5209 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5210 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5211 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5212 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5215 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5219 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5220 .next( function () {
5221 this.title
.setLabel(
5222 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5224 this.message
.setLabel(
5225 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5227 this.message
.$element
.toggleClass(
5228 'oo-ui-messageDialog-message-verbose',
5229 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5237 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5238 return Math
.round( this.text
.$element
.outerHeight( true ) );
5244 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5246 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5249 this.$actions
= this.$( '<div>' );
5250 this.container
= new OO
.ui
.PanelLayout( {
5251 '$': this.$, 'scrollable': true, 'classes': [ 'oo-ui-messageDialog-container' ]
5253 this.text
= new OO
.ui
.PanelLayout( {
5254 '$': this.$, 'padded': true, 'expanded': false, 'classes': [ 'oo-ui-messageDialog-text' ]
5256 this.message
= new OO
.ui
.LabelWidget( {
5257 '$': this.$, 'classes': [ 'oo-ui-messageDialog-message' ]
5261 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5262 this.frame
.$content
.addClass( 'oo-ui-messageDialog-content' );
5263 this.container
.$element
.append( this.text
.$element
);
5264 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5265 this.$body
.append( this.container
.$element
);
5266 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5267 this.$foot
.append( this.$actions
);
5273 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5274 var i
, len
, other
, special
, others
;
5277 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5279 special
= this.actions
.getSpecial();
5280 others
= this.actions
.getOthers();
5281 if ( special
.safe
) {
5282 this.$actions
.append( special
.safe
.$element
);
5283 special
.safe
.toggleFramed( false );
5285 if ( others
.length
) {
5286 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5288 this.$actions
.append( other
.$element
);
5289 other
.toggleFramed( false );
5292 if ( special
.primary
) {
5293 this.$actions
.append( special
.primary
.$element
);
5294 special
.primary
.toggleFramed( false );
5298 if ( !this.isOpening() ) {
5299 this.manager
.updateWindowSize( this );
5301 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5305 * Fit action actions into columns or rows.
5307 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5309 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5311 actions
= this.actions
.get();
5314 this.toggleVerticalActionLayout( false );
5315 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5316 action
= actions
[i
];
5317 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5318 this.toggleVerticalActionLayout( true );
5325 * Navigation dialog window.
5328 * - Show and hide errors.
5329 * - Retry an action.
5332 * - Renders header with dialog title and one action widget on either side
5333 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5334 * which close the dialog).
5335 * - Displays any action widgets in the footer (none by default).
5336 * - Ability to dismiss errors.
5338 * Subclass responsibilities:
5339 * - Register a 'safe' action.
5340 * - Register a 'primary' action.
5341 * - Add content to the dialog.
5345 * @extends OO.ui.Dialog
5348 * @param {Object} [config] Configuration options
5350 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( manager
, config
) {
5351 // Parent constructor
5352 OO
.ui
.ProcessDialog
.super.call( this, manager
, config
);
5355 this.$element
.addClass( 'oo-ui-processDialog' );
5360 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
5365 * Handle dismiss button click events.
5369 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
5374 * Handle retry button click events.
5376 * Hides errors and then tries again.
5378 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
5380 this.executeAction( this.currentAction
.getAction() );
5386 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
5387 if ( this.actions
.isSpecial( action
) ) {
5390 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5396 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
5398 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
5401 this.$navigation
= this.$( '<div>' );
5402 this.$location
= this.$( '<div>' );
5403 this.$safeActions
= this.$( '<div>' );
5404 this.$primaryActions
= this.$( '<div>' );
5405 this.$otherActions
= this.$( '<div>' );
5406 this.dismissButton
= new OO
.ui
.ButtonWidget( {
5408 'label': OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
5410 this.retryButton
= new OO
.ui
.ButtonWidget( {
5412 'label': OO
.ui
.msg( 'ooui-dialog-process-retry' )
5414 this.$errors
= this.$( '<div>' );
5415 this.$errorsTitle
= this.$( '<div>' );
5418 this.dismissButton
.connect( this, { 'click': 'onDismissErrorButtonClick' } );
5419 this.retryButton
.connect( this, { 'click': 'onRetryButtonClick' } );
5422 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
5424 .append( this.title
.$element
)
5425 .addClass( 'oo-ui-processDialog-location' );
5426 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
5427 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
5428 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
5430 .addClass( 'oo-ui-processDialog-errors-title' )
5431 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
5433 .addClass( 'oo-ui-processDialog-errors' )
5434 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
5436 .addClass( 'oo-ui-processDialog-content' )
5437 .append( this.$errors
);
5439 .addClass( 'oo-ui-processDialog-navigation' )
5440 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
5441 this.$head
.append( this.$navigation
);
5442 this.$foot
.append( this.$otherActions
);
5448 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
5449 var i
, len
, other
, special
, others
;
5452 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
5454 special
= this.actions
.getSpecial();
5455 others
= this.actions
.getOthers();
5456 if ( special
.primary
) {
5457 this.$primaryActions
.append( special
.primary
.$element
);
5458 special
.primary
.toggleFramed( false );
5460 if ( others
.length
) {
5461 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5463 this.$otherActions
.append( other
.$element
);
5464 other
.toggleFramed( true );
5467 if ( special
.safe
) {
5468 this.$safeActions
.append( special
.safe
.$element
);
5469 special
.safe
.toggleFramed( false );
5473 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5479 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
5480 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
5481 .fail( OO
.ui
.bind( this.showErrors
, this ) );
5485 * Fit label between actions.
5489 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
5490 var width
= Math
.max(
5491 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
5492 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
5494 this.$location
.css( { 'padding-left': width
, 'padding-right': width
} );
5500 * Handle errors that occured durring accept or reject processes.
5502 * @param {OO.ui.Error[]} errors Errors to be handled
5504 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
5509 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
5510 if ( !errors
[i
].isRecoverable() ) {
5511 recoverable
= false;
5513 $item
= this.$( '<div>' )
5514 .addClass( 'oo-ui-processDialog-error' )
5515 .append( errors
[i
].getMessage() );
5516 items
.push( $item
[0] );
5518 this.$errorItems
= this.$( items
);
5519 if ( recoverable
) {
5520 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
5522 this.currentAction
.setDisabled( true );
5524 this.retryButton
.toggle( recoverable
);
5525 this.$errorsTitle
.after( this.$errorItems
);
5526 this.$errors
.show().scrollTop( 0 );
5532 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
5533 this.$errors
.hide();
5534 this.$errorItems
.remove();
5535 this.$errorItems
= null;
5539 * Layout containing a series of pages.
5542 * @extends OO.ui.Layout
5545 * @param {Object} [config] Configuration options
5546 * @cfg {boolean} [continuous=false] Show all pages, one after another
5547 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
5548 * @cfg {boolean} [outlined=false] Show an outline
5549 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
5551 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
5552 // Initialize configuration
5553 config
= config
|| {};
5555 // Parent constructor
5556 OO
.ui
.BookletLayout
.super.call( this, config
);
5559 this.currentPageName
= null;
5561 this.ignoreFocus
= false;
5562 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
5563 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
5564 this.outlineVisible
= false;
5565 this.outlined
= !!config
.outlined
;
5566 if ( this.outlined
) {
5567 this.editable
= !!config
.editable
;
5568 this.outlineControlsWidget
= null;
5569 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
5570 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
5571 this.gridLayout
= new OO
.ui
.GridLayout(
5572 [ this.outlinePanel
, this.stackLayout
],
5573 { '$': this.$, 'widths': [ 1, 2 ] }
5575 this.outlineVisible
= true;
5576 if ( this.editable
) {
5577 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
5578 this.outlineWidget
, { '$': this.$ }
5584 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
5585 if ( this.outlined
) {
5586 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
5588 if ( this.autoFocus
) {
5589 // Event 'focus' does not bubble, but 'focusin' does
5590 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
5594 this.$element
.addClass( 'oo-ui-bookletLayout' );
5595 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
5596 if ( this.outlined
) {
5597 this.outlinePanel
.$element
5598 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
5599 .append( this.outlineWidget
.$element
);
5600 if ( this.editable
) {
5601 this.outlinePanel
.$element
5602 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
5603 .append( this.outlineControlsWidget
.$element
);
5605 this.$element
.append( this.gridLayout
.$element
);
5607 this.$element
.append( this.stackLayout
.$element
);
5613 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
5619 * @param {OO.ui.PageLayout} page Current page
5624 * @param {OO.ui.PageLayout[]} page Added pages
5625 * @param {number} index Index pages were added at
5630 * @param {OO.ui.PageLayout[]} pages Removed pages
5636 * Handle stack layout focus.
5638 * @param {jQuery.Event} e Focusin event
5640 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
5643 // Find the page that an element was focused within
5644 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
5645 for ( name
in this.pages
) {
5646 // Check for page match, exclude current page to find only page changes
5647 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
5648 this.setPage( name
);
5655 * Handle stack layout set events.
5657 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
5659 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
5662 page
.scrollElementIntoView( { 'complete': function () {
5663 if ( layout
.autoFocus
) {
5664 // Set focus to the first input if nothing on the page is focused yet
5665 if ( !page
.$element
.find( ':focus' ).length
) {
5666 page
.$element
.find( ':input:first' ).focus();
5674 * Handle outline widget select events.
5676 * @param {OO.ui.OptionWidget|null} item Selected item
5678 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
5680 this.setPage( item
.getData() );
5685 * Check if booklet has an outline.
5689 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
5690 return this.outlined
;
5694 * Check if booklet has editing controls.
5698 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
5699 return this.editable
;
5703 * Check if booklet has a visible outline.
5707 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
5708 return this.outlined
&& this.outlineVisible
;
5712 * Hide or show the outline.
5714 * @param {boolean} [show] Show outline, omit to invert current state
5717 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
5718 if ( this.outlined
) {
5719 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
5720 this.outlineVisible
= show
;
5721 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
5728 * Get the outline widget.
5730 * @param {OO.ui.PageLayout} page Page to be selected
5731 * @return {OO.ui.PageLayout|null} Closest page to another
5733 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
5734 var next
, prev
, level
,
5735 pages
= this.stackLayout
.getItems(),
5736 index
= $.inArray( page
, pages
);
5738 if ( index
!== -1 ) {
5739 next
= pages
[index
+ 1];
5740 prev
= pages
[index
- 1];
5741 // Prefer adjacent pages at the same level
5742 if ( this.outlined
) {
5743 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
5746 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
5752 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
5758 return prev
|| next
|| null;
5762 * Get the outline widget.
5764 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
5766 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
5767 return this.outlineWidget
;
5771 * Get the outline controls widget. If the outline is not editable, null is returned.
5773 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
5775 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
5776 return this.outlineControlsWidget
;
5780 * Get a page by name.
5782 * @param {string} name Symbolic name of page
5783 * @return {OO.ui.PageLayout|undefined} Page, if found
5785 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
5786 return this.pages
[name
];
5790 * Get the current page name.
5792 * @return {string|null} Current page name
5794 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
5795 return this.currentPageName
;
5799 * Add a page to the layout.
5801 * When pages are added with the same names as existing pages, the existing pages will be
5802 * automatically removed before the new pages are added.
5804 * @param {OO.ui.PageLayout[]} pages Pages to add
5805 * @param {number} index Index to insert pages after
5809 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
5810 var i
, len
, name
, page
, item
, currentIndex
,
5811 stackLayoutPages
= this.stackLayout
.getItems(),
5815 // Remove pages with same names
5816 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5818 name
= page
.getName();
5820 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
5821 // Correct the insertion index
5822 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
5823 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
5826 remove
.push( this.pages
[name
] );
5829 if ( remove
.length
) {
5830 this.removePages( remove
);
5834 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5836 name
= page
.getName();
5837 this.pages
[page
.getName()] = page
;
5838 if ( this.outlined
) {
5839 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { '$': this.$ } );
5840 page
.setOutlineItem( item
);
5845 if ( this.outlined
&& items
.length
) {
5846 this.outlineWidget
.addItems( items
, index
);
5847 this.updateOutlineWidget();
5849 this.stackLayout
.addItems( pages
, index
);
5850 this.emit( 'add', pages
, index
);
5856 * Remove a page from the layout.
5861 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
5862 var i
, len
, name
, page
,
5865 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5867 name
= page
.getName();
5868 delete this.pages
[name
];
5869 if ( this.outlined
) {
5870 items
.push( this.outlineWidget
.getItemFromData( name
) );
5871 page
.setOutlineItem( null );
5874 if ( this.outlined
&& items
.length
) {
5875 this.outlineWidget
.removeItems( items
);
5876 this.updateOutlineWidget();
5878 this.stackLayout
.removeItems( pages
);
5879 this.emit( 'remove', pages
);
5885 * Clear all pages from the layout.
5890 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
5892 pages
= this.stackLayout
.getItems();
5895 this.currentPageName
= null;
5896 if ( this.outlined
) {
5897 this.outlineWidget
.clearItems();
5898 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5899 pages
[i
].setOutlineItem( null );
5902 this.stackLayout
.clearItems();
5904 this.emit( 'remove', pages
);
5910 * Set the current page by name.
5913 * @param {string} name Symbolic name of page
5915 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
5917 page
= this.pages
[name
];
5919 if ( name
!== this.currentPageName
) {
5920 if ( this.outlined
) {
5921 selectedItem
= this.outlineWidget
.getSelectedItem();
5922 if ( selectedItem
&& selectedItem
.getData() !== name
) {
5923 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
5927 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
5928 this.pages
[this.currentPageName
].setActive( false );
5929 // Blur anything focused if the next page doesn't have anything focusable - this
5930 // is not needed if the next page has something focusable because once it is focused
5931 // this blur happens automatically
5932 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
5933 this.pages
[this.currentPageName
].$element
.find( ':focus' ).blur();
5936 this.currentPageName
= name
;
5937 this.stackLayout
.setItem( page
);
5938 page
.setActive( true );
5939 this.emit( 'set', page
);
5945 * Call this after adding or removing items from the OutlineWidget.
5949 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
5950 // Auto-select first item when nothing is selected anymore
5951 if ( !this.outlineWidget
.getSelectedItem() ) {
5952 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
5959 * Layout made of a field and optional label.
5962 * @extends OO.ui.Layout
5963 * @mixins OO.ui.LabeledElement
5965 * Available label alignment modes include:
5966 * - 'left': Label is before the field and aligned away from it, best for when the user will be
5967 * scanning for a specific label in a form with many fields
5968 * - 'right': Label is before the field and aligned toward it, best for forms the user is very
5969 * familiar with and will tab through field checking quickly to verify which field they are in
5970 * - 'top': Label is before the field and above it, best for when the use will need to fill out all
5971 * fields from top to bottom in a form with few fields
5972 * - 'inline': Label is after the field and aligned toward it, best for small boolean fields like
5973 * checkboxes or radio buttons
5976 * @param {OO.ui.Widget} field Field widget
5977 * @param {Object} [config] Configuration options
5978 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
5979 * @cfg {string} [help] Explanatory text shown as a '?' icon.
5981 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
5982 var popupButtonWidget
;
5983 // Config initialization
5984 config
= $.extend( { 'align': 'left' }, config
);
5986 // Parent constructor
5987 OO
.ui
.FieldLayout
.super.call( this, config
);
5989 // Mixin constructors
5990 this.$help
= this.$( '<div>' );
5991 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
5992 if ( config
.help
) {
5993 popupButtonWidget
= new OO
.ui
.PopupButtonWidget( $.extend(
5998 'title': config
.help
6003 popupButtonWidget
.getPopup().$body
.append( this.getElementDocument().createTextNode( config
.help
) );
6004 this.$help
= popupButtonWidget
.$element
;
6008 this.$field
= this.$( '<div>' );
6013 if ( this.field
instanceof OO
.ui
.InputWidget
) {
6014 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
6016 this.field
.connect( this, { 'disable': 'onFieldDisable' } );
6019 this.$element
.addClass( 'oo-ui-fieldLayout' );
6021 .addClass( 'oo-ui-fieldLayout-field' )
6022 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
6023 .append( this.field
.$element
);
6024 this.setAlignment( config
.align
);
6029 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6030 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
6035 * Handle field disable events.
6037 * @param {boolean} value Field is disabled
6039 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6040 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6044 * Handle label mouse click events.
6046 * @param {jQuery.Event} e Mouse click event
6048 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6049 this.field
.simulateLabelClick();
6056 * @return {OO.ui.Widget} Field widget
6058 OO
.ui
.FieldLayout
.prototype.getField = function () {
6063 * Set the field alignment mode.
6065 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6068 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6069 if ( value
!== this.align
) {
6070 // Default to 'left'
6071 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6075 if ( value
=== 'inline' ) {
6076 this.$element
.append( this.$field
, this.$label
, this.$help
);
6078 this.$element
.append( this.$help
, this.$label
, this.$field
);
6082 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6085 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
6092 * Layout made of a fieldset and optional legend.
6094 * Just add OO.ui.FieldLayout items.
6097 * @extends OO.ui.Layout
6098 * @mixins OO.ui.LabeledElement
6099 * @mixins OO.ui.IconedElement
6100 * @mixins OO.ui.GroupElement
6103 * @param {Object} [config] Configuration options
6104 * @cfg {string} [icon] Symbolic icon name
6105 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6107 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6108 // Config initialization
6109 config
= config
|| {};
6111 // Parent constructor
6112 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6114 // Mixin constructors
6115 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
6116 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
6117 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
6121 .addClass( 'oo-ui-fieldsetLayout' )
6122 .prepend( this.$icon
, this.$label
, this.$group
);
6123 if ( $.isArray( config
.items
) ) {
6124 this.addItems( config
.items
);
6130 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6131 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
6132 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
6133 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6135 /* Static Properties */
6137 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
6140 * Layout with an HTML form.
6143 * @extends OO.ui.Layout
6146 * @param {Object} [config] Configuration options
6148 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6149 // Configuration initialization
6150 config
= config
|| {};
6152 // Parent constructor
6153 OO
.ui
.FormLayout
.super.call( this, config
);
6156 this.$element
.on( 'submit', OO
.ui
.bind( this.onFormSubmit
, this ) );
6159 this.$element
.addClass( 'oo-ui-formLayout' );
6164 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6172 /* Static Properties */
6174 OO
.ui
.FormLayout
.static.tagName
= 'form';
6179 * Handle form submit events.
6181 * @param {jQuery.Event} e Submit event
6184 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6185 this.emit( 'submit' );
6190 * Layout made of proportionally sized columns and rows.
6193 * @extends OO.ui.Layout
6196 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6197 * @param {Object} [config] Configuration options
6198 * @cfg {number[]} [widths] Widths of columns as ratios
6199 * @cfg {number[]} [heights] Heights of columns as ratios
6201 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6204 // Config initialization
6205 config
= config
|| {};
6207 // Parent constructor
6208 OO
.ui
.GridLayout
.super.call( this, config
);
6216 this.$element
.addClass( 'oo-ui-gridLayout' );
6217 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6218 this.panels
.push( panels
[i
] );
6219 this.$element
.append( panels
[i
].$element
);
6221 if ( config
.widths
|| config
.heights
) {
6222 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6224 // Arrange in columns by default
6226 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
6229 this.layout( widths
, [ 1 ] );
6235 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6247 /* Static Properties */
6249 OO
.ui
.GridLayout
.static.tagName
= 'div';
6254 * Set grid dimensions.
6256 * @param {number[]} widths Widths of columns as ratios
6257 * @param {number[]} heights Heights of rows as ratios
6259 * @throws {Error} If grid is not large enough to fit all panels
6261 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6265 cols
= widths
.length
,
6266 rows
= heights
.length
;
6268 // Verify grid is big enough to fit panels
6269 if ( cols
* rows
< this.panels
.length
) {
6270 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6273 // Sum up denominators
6274 for ( x
= 0; x
< cols
; x
++ ) {
6277 for ( y
= 0; y
< rows
; y
++ ) {
6283 for ( x
= 0; x
< cols
; x
++ ) {
6284 this.widths
[x
] = widths
[x
] / xd
;
6286 for ( y
= 0; y
< rows
; y
++ ) {
6287 this.heights
[y
] = heights
[y
] / yd
;
6291 this.emit( 'layout' );
6295 * Update panel positions and sizes.
6299 OO
.ui
.GridLayout
.prototype.update = function () {
6307 cols
= this.widths
.length
,
6308 rows
= this.heights
.length
;
6310 for ( y
= 0; y
< rows
; y
++ ) {
6311 height
= this.heights
[y
];
6312 for ( x
= 0; x
< cols
; x
++ ) {
6313 panel
= this.panels
[i
];
6314 width
= this.widths
[x
];
6316 'width': Math
.round( width
* 100 ) + '%',
6317 'height': Math
.round( height
* 100 ) + '%',
6318 'top': Math
.round( top
* 100 ) + '%',
6319 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6320 'visibility': width
=== 0 || height
=== 0 ? 'hidden' : ''
6323 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6324 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6326 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6328 panel
.$element
.css( dimensions
);
6336 this.emit( 'update' );
6340 * Get a panel at a given position.
6342 * The x and y position is affected by the current grid layout.
6344 * @param {number} x Horizontal position
6345 * @param {number} y Vertical position
6346 * @return {OO.ui.PanelLayout} The panel at the given postion
6348 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
6349 return this.panels
[( x
* this.widths
.length
) + y
];
6353 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
6356 * @extends OO.ui.Layout
6359 * @param {Object} [config] Configuration options
6360 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
6361 * @cfg {boolean} [padded=false] Pad the content from the edges
6362 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
6364 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
6365 // Config initialization
6366 config
= config
|| {};
6368 // Parent constructor
6369 OO
.ui
.PanelLayout
.super.call( this, config
);
6372 this.$element
.addClass( 'oo-ui-panelLayout' );
6373 if ( config
.scrollable
) {
6374 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
6377 if ( config
.padded
) {
6378 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
6381 if ( config
.expanded
=== undefined || config
.expanded
) {
6382 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
6388 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
6391 * Page within an booklet layout.
6394 * @extends OO.ui.PanelLayout
6397 * @param {string} name Unique symbolic name of page
6398 * @param {Object} [config] Configuration options
6399 * @param {string} [outlineItem] Outline item widget
6401 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
6402 // Configuration initialization
6403 config
= $.extend( { 'scrollable': true }, config
);
6405 // Parent constructor
6406 OO
.ui
.PageLayout
.super.call( this, config
);
6410 this.outlineItem
= config
.outlineItem
|| null;
6411 this.active
= false;
6414 this.$element
.addClass( 'oo-ui-pageLayout' );
6419 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
6425 * @param {boolean} active Page is active
6433 * @return {string} Symbolic name of page
6435 OO
.ui
.PageLayout
.prototype.getName = function () {
6440 * Check if page is active.
6442 * @return {boolean} Page is active
6444 OO
.ui
.PageLayout
.prototype.isActive = function () {
6451 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
6453 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
6454 return this.outlineItem
;
6460 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
6461 * outline item as desired; this method is called for setting (with an object) and unsetting
6462 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
6463 * operating on null instead of an OO.ui.OutlineItemWidget object.
6465 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
6468 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
6469 this.outlineItem
= outlineItem
|| null;
6470 if ( outlineItem
) {
6471 this.setupOutlineItem();
6477 * Setup outline item.
6479 * @localdoc Subclasses should override this method to adjust the outline item as desired.
6481 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
6484 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
6489 * Set page active state.
6491 * @param {boolean} Page is active
6494 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
6497 if ( active
!== this.active
) {
6498 this.active
= active
;
6499 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
6500 this.emit( 'active', this.active
);
6505 * Layout containing a series of mutually exclusive pages.
6508 * @extends OO.ui.PanelLayout
6509 * @mixins OO.ui.GroupElement
6512 * @param {Object} [config] Configuration options
6513 * @cfg {boolean} [continuous=false] Show all pages, one after another
6514 * @cfg {string} [icon=''] Symbolic icon name
6515 * @cfg {OO.ui.Layout[]} [items] Layouts to add
6517 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
6518 // Config initialization
6519 config
= $.extend( { 'scrollable': true }, config
);
6521 // Parent constructor
6522 OO
.ui
.StackLayout
.super.call( this, config
);
6524 // Mixin constructors
6525 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
6528 this.currentItem
= null;
6529 this.continuous
= !!config
.continuous
;
6532 this.$element
.addClass( 'oo-ui-stackLayout' );
6533 if ( this.continuous
) {
6534 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
6536 if ( $.isArray( config
.items
) ) {
6537 this.addItems( config
.items
);
6543 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
6544 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
6550 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
6556 * Get the current item.
6558 * @return {OO.ui.Layout|null}
6560 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
6561 return this.currentItem
;
6565 * Unset the current item.
6568 * @param {OO.ui.StackLayout} layout
6571 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
6572 var prevItem
= this.currentItem
;
6573 if ( prevItem
=== null ) {
6577 this.currentItem
= null;
6578 this.emit( 'set', null );
6584 * Adding an existing item (by value) will move it.
6586 * @param {OO.ui.Layout[]} items Items to add
6587 * @param {number} [index] Index to insert items after
6590 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
6592 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
6594 if ( !this.currentItem
&& items
.length
) {
6595 this.setItem( items
[0] );
6604 * Items will be detached, not removed, so they can be used later.
6606 * @param {OO.ui.Layout[]} items Items to remove
6610 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
6612 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
6614 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
6615 if ( this.items
.length
) {
6616 this.setItem( this.items
[0] );
6618 this.unsetCurrentItem();
6628 * Items will be detached, not removed, so they can be used later.
6633 OO
.ui
.StackLayout
.prototype.clearItems = function () {
6634 this.unsetCurrentItem();
6635 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
6643 * Any currently shown item will be hidden.
6645 * FIXME: If the passed item to show has not been added in the items list, then
6646 * this method drops it and unsets the current item.
6648 * @param {OO.ui.Layout} item Item to show
6652 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
6655 if ( item
!== this.currentItem
) {
6656 if ( !this.continuous
) {
6657 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6658 this.items
[i
].$element
.css( 'display', '' );
6661 if ( $.inArray( item
, this.items
) !== -1 ) {
6662 if ( !this.continuous
) {
6663 item
.$element
.css( 'display', 'block' );
6665 this.currentItem
= item
;
6666 this.emit( 'set', item
);
6668 this.unsetCurrentItem();
6676 * Horizontal bar layout of tools as icon buttons.
6679 * @extends OO.ui.ToolGroup
6682 * @param {OO.ui.Toolbar} toolbar
6683 * @param {Object} [config] Configuration options
6685 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
6686 // Parent constructor
6687 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
6690 this.$element
.addClass( 'oo-ui-barToolGroup' );
6695 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
6697 /* Static Properties */
6699 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
6701 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
6703 OO
.ui
.BarToolGroup
.static.name
= 'bar';
6706 * Popup list of tools with an icon and optional label.
6710 * @extends OO.ui.ToolGroup
6711 * @mixins OO.ui.IconedElement
6712 * @mixins OO.ui.IndicatedElement
6713 * @mixins OO.ui.LabeledElement
6714 * @mixins OO.ui.TitledElement
6715 * @mixins OO.ui.ClippableElement
6718 * @param {OO.ui.Toolbar} toolbar
6719 * @param {Object} [config] Configuration options
6720 * @cfg {string} [header] Text to display at the top of the pop-up
6722 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
6723 // Configuration initialization
6724 config
= config
|| {};
6726 // Parent constructor
6727 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
6729 // Mixin constructors
6730 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6731 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6732 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6733 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
6734 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
6737 this.active
= false;
6738 this.dragging
= false;
6739 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
6740 this.$handle
= this.$( '<span>' );
6744 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
6745 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
6750 .addClass( 'oo-ui-popupToolGroup-handle' )
6751 .append( this.$icon
, this.$label
, this.$indicator
);
6752 // If the pop-up should have a header, add it to the top of the toolGroup.
6753 // Note: If this feature is useful for other widgets, we could abstract it into an
6754 // OO.ui.HeaderedElement mixin constructor.
6755 if ( config
.header
!== undefined ) {
6757 .prepend( this.$( '<span>' )
6758 .addClass( 'oo-ui-popupToolGroup-header' )
6759 .text( config
.header
)
6763 .addClass( 'oo-ui-popupToolGroup' )
6764 .prepend( this.$handle
);
6769 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
6770 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
6771 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
6772 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
6773 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
6774 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
6776 /* Static Properties */
6783 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
6785 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
6787 if ( this.isDisabled() && this.isElementAttached() ) {
6788 this.setActive( false );
6793 * Handle focus being lost.
6795 * The event is actually generated from a mouseup, so it is not a normal blur event object.
6797 * @param {jQuery.Event} e Mouse up event
6799 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
6800 // Only deactivate when clicking outside the dropdown element
6801 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
6802 this.setActive( false );
6809 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
6810 if ( !this.isDisabled() && e
.which
=== 1 ) {
6811 this.setActive( false );
6813 return OO
.ui
.PopupToolGroup
.super.prototype.onMouseUp
.call( this, e
);
6817 * Handle mouse up events.
6819 * @param {jQuery.Event} e Mouse up event
6821 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
6826 * Handle mouse down events.
6828 * @param {jQuery.Event} e Mouse down event
6830 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
6831 if ( !this.isDisabled() && e
.which
=== 1 ) {
6832 this.setActive( !this.active
);
6838 * Switch into active mode.
6840 * When active, mouseup events anywhere in the document will trigger deactivation.
6842 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
6844 if ( this.active
!== value
) {
6845 this.active
= value
;
6847 this.setClipping( true );
6848 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
6849 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
6851 this.setClipping( false );
6852 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
6853 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
6859 * Drop down list layout of tools as labeled icon buttons.
6862 * @extends OO.ui.PopupToolGroup
6865 * @param {OO.ui.Toolbar} toolbar
6866 * @param {Object} [config] Configuration options
6868 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
6869 // Parent constructor
6870 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
6873 this.$element
.addClass( 'oo-ui-listToolGroup' );
6878 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
6880 /* Static Properties */
6882 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
6884 OO
.ui
.ListToolGroup
.static.name
= 'list';
6887 * Drop down menu layout of tools as selectable menu items.
6890 * @extends OO.ui.PopupToolGroup
6893 * @param {OO.ui.Toolbar} toolbar
6894 * @param {Object} [config] Configuration options
6896 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
6897 // Configuration initialization
6898 config
= config
|| {};
6900 // Parent constructor
6901 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
6904 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
6907 this.$element
.addClass( 'oo-ui-menuToolGroup' );
6912 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
6914 /* Static Properties */
6916 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
6918 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
6923 * Handle the toolbar state being updated.
6925 * When the state changes, the title of each active item in the menu will be joined together and
6926 * used as a label for the group. The label will be empty if none of the items are active.
6928 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
6932 for ( name
in this.tools
) {
6933 if ( this.tools
[name
].isActive() ) {
6934 labelTexts
.push( this.tools
[name
].getTitle() );
6938 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
6942 * Tool that shows a popup when selected.
6946 * @extends OO.ui.Tool
6947 * @mixins OO.ui.PopuppableElement
6950 * @param {OO.ui.Toolbar} toolbar
6951 * @param {Object} [config] Configuration options
6953 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
6954 // Parent constructor
6955 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
6957 // Mixin constructors
6958 OO
.ui
.PopuppableElement
.call( this, config
);
6962 .addClass( 'oo-ui-popupTool' )
6963 .append( this.popup
.$element
);
6968 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
6969 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
6974 * Handle the tool being selected.
6978 OO
.ui
.PopupTool
.prototype.onSelect = function () {
6979 if ( !this.isDisabled() ) {
6980 this.popup
.toggle();
6982 this.setActive( false );
6987 * Handle the toolbar state being updated.
6991 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
6992 this.setActive( false );
6996 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
6998 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7002 * @extends OO.ui.GroupElement
7005 * @param {jQuery} $group Container node, assigned to #$group
7006 * @param {Object} [config] Configuration options
7008 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
7009 // Parent constructor
7010 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
7015 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7020 * Set the disabled state of the widget.
7022 * This will also update the disabled state of child widgets.
7024 * @param {boolean} disabled Disable widget
7027 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7031 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7032 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7034 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7036 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7037 this.items
[i
].updateDisabled();
7045 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7047 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7048 * allows bidrectional communication.
7050 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7057 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7064 * Check if widget is disabled.
7066 * Checks parent if present, making disabled state inheritable.
7068 * @return {boolean} Widget is disabled
7070 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7071 return this.disabled
||
7072 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7076 * Set group element is in.
7078 * @param {OO.ui.GroupElement|null} group Group element, null if none
7081 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7083 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7084 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7086 // Initialize item disabled states
7087 this.updateDisabled();
7093 * Mixin that adds a menu showing suggested values for a text input.
7095 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7101 * @param {OO.ui.TextInputWidget} input Input widget
7102 * @param {Object} [config] Configuration options
7103 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
7105 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7106 // Config intialization
7107 config
= config
|| {};
7110 this.lookupInput
= input
;
7111 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
7112 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7113 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
7114 'input': this.lookupInput
,
7115 '$container': config
.$container
7117 this.lookupCache
= {};
7118 this.lookupQuery
= null;
7119 this.lookupRequest
= null;
7120 this.populating
= false;
7123 this.$overlay
.append( this.lookupMenu
.$element
);
7125 this.lookupInput
.$input
.on( {
7126 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
7127 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
7128 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
7130 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
7133 this.$element
.addClass( 'oo-ui-lookupWidget' );
7134 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7140 * Handle input focus event.
7142 * @param {jQuery.Event} e Input focus event
7144 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7145 this.openLookupMenu();
7149 * Handle input blur event.
7151 * @param {jQuery.Event} e Input blur event
7153 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7154 this.lookupMenu
.toggle( false );
7158 * Handle input mouse down event.
7160 * @param {jQuery.Event} e Input mouse down event
7162 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7163 this.openLookupMenu();
7167 * Handle input change event.
7169 * @param {string} value New input value
7171 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7172 this.openLookupMenu();
7178 * @return {OO.ui.TextInputMenuWidget}
7180 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
7181 return this.lookupMenu
;
7189 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
7190 var value
= this.lookupInput
.getValue();
7192 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
7193 this.populateLookupMenu();
7194 this.lookupMenu
.toggle( true );
7205 * Populate lookup menu with current information.
7209 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
7212 if ( !this.populating
) {
7213 this.populating
= true;
7214 this.getLookupMenuItems()
7215 .done( function ( items
) {
7216 widget
.lookupMenu
.clearItems();
7217 if ( items
.length
) {
7221 widget
.initializeLookupMenuSelection();
7222 widget
.openLookupMenu();
7224 widget
.lookupMenu
.toggle( true );
7226 widget
.populating
= false;
7228 .fail( function () {
7229 widget
.lookupMenu
.clearItems();
7230 widget
.populating
= false;
7238 * Set selection in the lookup menu with current information.
7242 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
7243 if ( !this.lookupMenu
.getSelectedItem() ) {
7244 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
7246 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
7250 * Get lookup menu items for the current query.
7252 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
7255 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
7257 value
= this.lookupInput
.getValue(),
7258 deferred
= $.Deferred();
7260 if ( value
&& value
!== this.lookupQuery
) {
7261 // Abort current request if query has changed
7262 if ( this.lookupRequest
) {
7263 this.lookupRequest
.abort();
7264 this.lookupQuery
= null;
7265 this.lookupRequest
= null;
7267 if ( value
in this.lookupCache
) {
7268 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
7270 this.lookupQuery
= value
;
7271 this.lookupRequest
= this.getLookupRequest()
7272 .always( function () {
7273 widget
.lookupQuery
= null;
7274 widget
.lookupRequest
= null;
7276 .done( function ( data
) {
7277 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
7278 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
7280 .fail( function () {
7284 this.lookupRequest
.always( function () {
7285 widget
.popPending();
7289 return deferred
.promise();
7293 * Get a new request object of the current lookup query value.
7296 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
7298 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
7299 // Stub, implemented in subclass
7304 * Handle successful lookup request.
7306 * Overriding methods should call #populateLookupMenu when results are available and cache results
7307 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
7310 * @param {Mixed} data Response from server
7312 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
7313 // Stub, implemented in subclass
7317 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
7320 * @param {Mixed} data Cached result data, usually an array
7321 * @return {OO.ui.MenuItemWidget[]} Menu items
7323 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
7324 // Stub, implemented in subclass
7329 * Set of controls for an OO.ui.OutlineWidget.
7331 * Controls include moving items up and down, removing items, and adding different kinds of items.
7334 * @extends OO.ui.Widget
7335 * @mixins OO.ui.GroupElement
7336 * @mixins OO.ui.IconedElement
7339 * @param {OO.ui.OutlineWidget} outline Outline to control
7340 * @param {Object} [config] Configuration options
7342 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7343 // Configuration initialization
7344 config
= $.extend( { 'icon': 'add-item' }, config
);
7346 // Parent constructor
7347 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7349 // Mixin constructors
7350 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
7351 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
7354 this.outline
= outline
;
7355 this.$movers
= this.$( '<div>' );
7356 this.upButton
= new OO
.ui
.ButtonWidget( {
7360 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
7362 this.downButton
= new OO
.ui
.ButtonWidget( {
7366 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
7368 this.removeButton
= new OO
.ui
.ButtonWidget( {
7372 'title': OO
.ui
.msg( 'ooui-outline-control-remove' )
7376 outline
.connect( this, {
7377 'select': 'onOutlineChange',
7378 'add': 'onOutlineChange',
7379 'remove': 'onOutlineChange'
7381 this.upButton
.connect( this, { 'click': [ 'emit', 'move', -1 ] } );
7382 this.downButton
.connect( this, { 'click': [ 'emit', 'move', 1 ] } );
7383 this.removeButton
.connect( this, { 'click': [ 'emit', 'remove' ] } );
7386 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7387 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7389 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7390 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7391 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7396 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7397 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7398 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7404 * @param {number} places Number of places to move
7414 * Handle outline change events.
7416 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7417 var i
, len
, firstMovable
, lastMovable
,
7418 items
= this.outline
.getItems(),
7419 selectedItem
= this.outline
.getSelectedItem(),
7420 movable
= selectedItem
&& selectedItem
.isMovable(),
7421 removable
= selectedItem
&& selectedItem
.isRemovable();
7426 while ( ++i
< len
) {
7427 if ( items
[i
].isMovable() ) {
7428 firstMovable
= items
[i
];
7434 if ( items
[i
].isMovable() ) {
7435 lastMovable
= items
[i
];
7440 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7441 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7442 this.removeButton
.setDisabled( !removable
);
7446 * Mixin for widgets with a boolean on/off state.
7452 * @param {Object} [config] Configuration options
7453 * @cfg {boolean} [value=false] Initial value
7455 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7456 // Configuration initialization
7457 config
= config
|| {};
7463 this.$element
.addClass( 'oo-ui-toggleWidget' );
7464 this.setValue( !!config
.value
);
7471 * @param {boolean} value Changed value
7477 * Get the value of the toggle.
7481 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7486 * Set the value of the toggle.
7488 * @param {boolean} value New value
7492 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7494 if ( this.value
!== value
) {
7496 this.emit( 'change', value
);
7497 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7498 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7504 * Group widget for multiple related buttons.
7506 * Use together with OO.ui.ButtonWidget.
7509 * @extends OO.ui.Widget
7510 * @mixins OO.ui.GroupElement
7513 * @param {Object} [config] Configuration options
7514 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
7516 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
7517 // Parent constructor
7518 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
7520 // Mixin constructors
7521 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
7524 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
7525 if ( $.isArray( config
.items
) ) {
7526 this.addItems( config
.items
);
7532 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
7533 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
7536 * Generic widget for buttons.
7539 * @extends OO.ui.Widget
7540 * @mixins OO.ui.ButtonedElement
7541 * @mixins OO.ui.IconedElement
7542 * @mixins OO.ui.IndicatedElement
7543 * @mixins OO.ui.LabeledElement
7544 * @mixins OO.ui.TitledElement
7545 * @mixins OO.ui.FlaggableElement
7548 * @param {Object} [config] Configuration options
7549 * @cfg {string} [href] Hyperlink to visit when clicked
7550 * @cfg {string} [target] Target to open hyperlink in
7552 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
7553 // Configuration initialization
7554 config
= $.extend( { 'target': '_blank' }, config
);
7556 // Parent constructor
7557 OO
.ui
.ButtonWidget
.super.call( this, config
);
7559 // Mixin constructors
7560 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7561 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
7562 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
7563 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
7564 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
7565 OO
.ui
.FlaggableElement
.call( this, config
);
7570 this.isHyperlink
= false;
7574 'click': OO
.ui
.bind( this.onClick
, this ),
7575 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
7579 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
7581 .addClass( 'oo-ui-buttonWidget' )
7582 .append( this.$button
);
7583 this.setHref( config
.href
);
7584 this.setTarget( config
.target
);
7589 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
7590 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
7591 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
7592 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
7593 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
7594 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
7595 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
7606 * Handles mouse click events.
7608 * @param {jQuery.Event} e Mouse click event
7611 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
7612 if ( !this.isDisabled() ) {
7613 this.emit( 'click' );
7614 if ( this.isHyperlink
) {
7622 * Handles keypress events.
7624 * @param {jQuery.Event} e Keypress event
7627 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
7628 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
7630 if ( this.isHyperlink
) {
7638 * Get hyperlink location.
7640 * @return {string} Hyperlink location
7642 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
7647 * Get hyperlink target.
7649 * @return {string} Hyperlink target
7651 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
7656 * Set hyperlink location.
7658 * @param {string|null} href Hyperlink location, null to remove
7660 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
7661 href
= typeof href
=== 'string' ? href
: null;
7663 if ( href
!== this.href
) {
7665 if ( href
!== null ) {
7666 this.$button
.attr( 'href', href
);
7667 this.isHyperlink
= true;
7669 this.$button
.removeAttr( 'href' );
7670 this.isHyperlink
= false;
7678 * Set hyperlink target.
7680 * @param {string|null} target Hyperlink target, null to remove
7682 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
7683 target
= typeof target
=== 'string' ? target
: null;
7685 if ( target
!== this.target
) {
7686 this.target
= target
;
7687 if ( target
!== null ) {
7688 this.$button
.attr( 'target', target
);
7690 this.$button
.removeAttr( 'target' );
7698 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
7701 * @extends OO.ui.ButtonWidget
7704 * @param {Object} [config] Configuration options
7705 * @cfg {string} [action] Symbolic action name
7706 * @cfg {string[]} [modes] Symbolic mode names
7708 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
7709 // Config intialization
7710 config
= $.extend( { 'framed': false }, config
);
7712 // Parent constructor
7713 OO
.ui
.ActionWidget
.super.call( this, config
);
7716 this.action
= config
.action
|| '';
7717 this.modes
= config
.modes
|| [];
7722 this.$element
.addClass( 'oo-ui-actionWidget' );
7727 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
7738 * Check if action is available in a certain mode.
7740 * @param {string} mode Name of mode
7741 * @return {boolean} Has mode
7743 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
7744 return this.modes
.indexOf( mode
) !== -1;
7748 * Get symbolic action name.
7752 OO
.ui
.ActionWidget
.prototype.getAction = function () {
7757 * Get symbolic action name.
7761 OO
.ui
.ActionWidget
.prototype.getModes = function () {
7762 return this.modes
.slice();
7766 * Emit a resize event if the size has changed.
7770 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
7773 if ( this.isElementAttached() ) {
7774 width
= this.$element
.width();
7775 height
= this.$element
.height();
7777 if ( width
!== this.width
|| height
!== this.height
) {
7779 this.height
= height
;
7780 this.emit( 'resize' );
7790 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
7792 OO
.ui
.IconedElement
.prototype.setIcon
.apply( this, arguments
);
7793 this.propagateResize();
7801 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
7803 OO
.ui
.LabeledElement
.prototype.setLabel
.apply( this, arguments
);
7804 this.propagateResize();
7812 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
7814 OO
.ui
.FlaggableElement
.prototype.setFlags
.apply( this, arguments
);
7815 this.propagateResize();
7823 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
7825 OO
.ui
.FlaggableElement
.prototype.clearFlags
.apply( this, arguments
);
7826 this.propagateResize();
7832 * Toggle visibility of button.
7834 * @param {boolean} [show] Show button, omit to toggle visibility
7837 OO
.ui
.ActionWidget
.prototype.toggle = function () {
7839 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
7840 this.propagateResize();
7846 * Button that shows and hides a popup.
7849 * @extends OO.ui.ButtonWidget
7850 * @mixins OO.ui.PopuppableElement
7853 * @param {Object} [config] Configuration options
7855 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7856 // Parent constructor
7857 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7859 // Mixin constructors
7860 OO
.ui
.PopuppableElement
.call( this, config
);
7864 .addClass( 'oo-ui-popupButtonWidget' )
7865 .append( this.popup
.$element
);
7870 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
7871 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
7876 * Handles mouse click events.
7878 * @param {jQuery.Event} e Mouse click event
7880 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
7881 // Skip clicks within the popup
7882 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
7886 if ( !this.isDisabled() ) {
7887 this.popup
.toggle();
7889 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
7895 * Button that toggles on and off.
7898 * @extends OO.ui.ButtonWidget
7899 * @mixins OO.ui.ToggleWidget
7902 * @param {Object} [config] Configuration options
7903 * @cfg {boolean} [value=false] Initial value
7905 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
7906 // Configuration initialization
7907 config
= config
|| {};
7909 // Parent constructor
7910 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
7912 // Mixin constructors
7913 OO
.ui
.ToggleWidget
.call( this, config
);
7916 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
7921 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
7922 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
7929 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
7930 if ( !this.isDisabled() ) {
7931 this.setValue( !this.value
);
7935 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
7941 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
7943 if ( value
!== this.value
) {
7944 this.setActive( value
);
7947 // Parent method (from mixin)
7948 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
7957 * @extends OO.ui.Widget
7958 * @mixins OO.ui.IconedElement
7959 * @mixins OO.ui.TitledElement
7962 * @param {Object} [config] Configuration options
7964 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
7965 // Config intialization
7966 config
= config
|| {};
7968 // Parent constructor
7969 OO
.ui
.IconWidget
.super.call( this, config
);
7971 // Mixin constructors
7972 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
7973 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
7976 this.$element
.addClass( 'oo-ui-iconWidget' );
7981 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
7982 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
7983 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
7985 /* Static Properties */
7987 OO
.ui
.IconWidget
.static.tagName
= 'span';
7992 * See OO.ui.IndicatedElement for more information.
7995 * @extends OO.ui.Widget
7996 * @mixins OO.ui.IndicatedElement
7997 * @mixins OO.ui.TitledElement
8000 * @param {Object} [config] Configuration options
8002 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8003 // Config intialization
8004 config
= config
|| {};
8006 // Parent constructor
8007 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8009 // Mixin constructors
8010 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
8011 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
8014 this.$element
.addClass( 'oo-ui-indicatorWidget' );
8019 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
8020 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
8021 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
8023 /* Static Properties */
8025 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
8028 * Inline menu of options.
8030 * Inline menus provide a control for accessing a menu and compose a menu within the widget, which
8031 * can be accessed using the #getMenu method.
8033 * Use with OO.ui.MenuOptionWidget.
8036 * @extends OO.ui.Widget
8037 * @mixins OO.ui.IconedElement
8038 * @mixins OO.ui.IndicatedElement
8039 * @mixins OO.ui.LabeledElement
8040 * @mixins OO.ui.TitledElement
8043 * @param {Object} [config] Configuration options
8044 * @cfg {Object} [menu] Configuration options to pass to menu widget
8046 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
8047 // Configuration initialization
8048 config
= $.extend( { 'indicator': 'down' }, config
);
8050 // Parent constructor
8051 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
8053 // Mixin constructors
8054 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
8055 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
8056 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
8057 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
8060 this.menu
= new OO
.ui
.MenuWidget( $.extend( { '$': this.$, 'widget': this }, config
.menu
) );
8061 this.$handle
= this.$( '<span>' );
8064 this.$element
.on( { 'click': OO
.ui
.bind( this.onClick
, this ) } );
8065 this.menu
.connect( this, { 'select': 'onMenuSelect' } );
8069 .addClass( 'oo-ui-inlineMenuWidget-handle' )
8070 .append( this.$icon
, this.$label
, this.$indicator
);
8072 .addClass( 'oo-ui-inlineMenuWidget' )
8073 .append( this.$handle
, this.menu
.$element
);
8078 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
8079 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
8080 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
8081 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
8082 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
8089 * @return {OO.ui.MenuWidget} Menu of widget
8091 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
8096 * Handles menu select events.
8098 * @param {OO.ui.MenuItemWidget} item Selected menu item
8100 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
8107 selectedLabel
= item
.getLabel();
8109 // If the label is a DOM element, clone it, because setLabel will append() it
8110 if ( selectedLabel
instanceof jQuery
) {
8111 selectedLabel
= selectedLabel
.clone();
8114 this.setLabel( selectedLabel
);
8118 * Handles mouse click events.
8120 * @param {jQuery.Event} e Mouse click event
8122 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
8123 // Skip clicks within the menu
8124 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8128 if ( !this.isDisabled() ) {
8129 if ( this.menu
.isVisible() ) {
8130 this.menu
.toggle( false );
8132 this.menu
.toggle( true );
8139 * Base class for input widgets.
8143 * @extends OO.ui.Widget
8146 * @param {Object} [config] Configuration options
8147 * @cfg {string} [name=''] HTML input name
8148 * @cfg {string} [value=''] Input value
8149 * @cfg {boolean} [readOnly=false] Prevent changes
8150 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
8152 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8153 // Config intialization
8154 config
= $.extend( { 'readOnly': false }, config
);
8156 // Parent constructor
8157 OO
.ui
.InputWidget
.super.call( this, config
);
8160 this.$input
= this.getInputElement( config
);
8162 this.readOnly
= false;
8163 this.inputFilter
= config
.inputFilter
;
8166 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
8170 .attr( 'name', config
.name
)
8171 .prop( 'disabled', this.isDisabled() );
8172 this.setReadOnly( config
.readOnly
);
8173 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
8174 this.setValue( config
.value
);
8179 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8191 * Get input element.
8193 * @param {Object} [config] Configuration options
8194 * @return {jQuery} Input element
8196 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8197 return this.$( '<input>' );
8201 * Handle potentially value-changing events.
8203 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8205 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8207 if ( !this.isDisabled() ) {
8208 // Allow the stack to clear so the value will be updated
8209 setTimeout( function () {
8210 widget
.setValue( widget
.$input
.val() );
8216 * Get the value of the input.
8218 * @return {string} Input value
8220 OO
.ui
.InputWidget
.prototype.getValue = function () {
8225 * Sets the direction of the current input, either RTL or LTR
8227 * @param {boolean} isRTL
8229 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
8231 this.$input
.removeClass( 'oo-ui-ltr' );
8232 this.$input
.addClass( 'oo-ui-rtl' );
8234 this.$input
.removeClass( 'oo-ui-rtl' );
8235 this.$input
.addClass( 'oo-ui-ltr' );
8240 * Set the value of the input.
8242 * @param {string} value New value
8246 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
8247 value
= this.sanitizeValue( value
);
8248 if ( this.value
!== value
) {
8250 this.emit( 'change', this.value
);
8252 // Update the DOM if it has changed. Note that with sanitizeValue, it
8253 // is possible for the DOM value to change without this.value changing.
8254 if ( this.$input
.val() !== this.value
) {
8255 this.$input
.val( this.value
);
8261 * Sanitize incoming value.
8263 * Ensures value is a string, and converts undefined and null to empty strings.
8265 * @param {string} value Original value
8266 * @return {string} Sanitized value
8268 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
8269 if ( value
=== undefined || value
=== null ) {
8271 } else if ( this.inputFilter
) {
8272 return this.inputFilter( String( value
) );
8274 return String( value
);
8279 * Simulate the behavior of clicking on a label bound to this input.
8281 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
8282 if ( !this.isDisabled() ) {
8283 if ( this.$input
.is( ':checkbox,:radio' ) ) {
8284 this.$input
.click();
8285 } else if ( this.$input
.is( ':input' ) ) {
8286 this.$input
.focus();
8292 * Check if the widget is read-only.
8296 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
8297 return this.readOnly
;
8301 * Set the read-only state of the widget.
8303 * This should probably change the widgets's appearance and prevent it from being used.
8305 * @param {boolean} state Make input read-only
8308 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
8309 this.readOnly
= !!state
;
8310 this.$input
.prop( 'readOnly', this.readOnly
);
8317 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
8318 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
8319 if ( this.$input
) {
8320 this.$input
.prop( 'disabled', this.isDisabled() );
8330 OO
.ui
.InputWidget
.prototype.focus = function () {
8331 this.$input
.focus();
8340 OO
.ui
.InputWidget
.prototype.blur = function () {
8346 * Checkbox input widget.
8349 * @extends OO.ui.InputWidget
8352 * @param {Object} [config] Configuration options
8354 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
8355 // Parent constructor
8356 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
8359 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
8364 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
8371 * Get input element.
8373 * @return {jQuery} Input element
8375 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
8376 return this.$( '<input type="checkbox" />' );
8380 * Get checked state of the checkbox
8382 * @return {boolean} If the checkbox is checked
8384 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
8391 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
8393 if ( this.value
!== value
) {
8395 this.$input
.prop( 'checked', this.value
);
8396 this.emit( 'change', this.value
);
8403 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
8405 if ( !this.isDisabled() ) {
8406 // Allow the stack to clear so the value will be updated
8407 setTimeout( function () {
8408 widget
.setValue( widget
.$input
.prop( 'checked' ) );
8414 * Input widget with a text field.
8417 * @extends OO.ui.InputWidget
8420 * @param {Object} [config] Configuration options
8421 * @cfg {string} [placeholder] Placeholder text
8422 * @cfg {string} [icon] Symbolic name of icon
8423 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8424 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8425 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8427 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8429 config
= $.extend( { 'maxRows': 10 }, config
);
8431 // Parent constructor
8432 OO
.ui
.TextInputWidget
.super.call( this, config
);
8436 this.multiline
= !!config
.multiline
;
8437 this.autosize
= !!config
.autosize
;
8438 this.maxRows
= config
.maxRows
;
8441 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8442 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8445 this.$element
.addClass( 'oo-ui-textInputWidget' );
8446 if ( config
.icon
) {
8447 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
8448 this.$element
.append(
8450 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
8451 .mousedown( function () {
8452 widget
.$input
.focus();
8457 if ( config
.placeholder
) {
8458 this.$input
.attr( 'placeholder', config
.placeholder
);
8464 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8469 * User presses enter inside the text box.
8471 * Not called if input is multiline.
8479 * Handle key press events.
8481 * @param {jQuery.Event} e Key press event
8482 * @fires enter If enter key is pressed and input is not multiline
8484 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8485 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8486 this.emit( 'enter' );
8491 * Handle element attach events.
8493 * @param {jQuery.Event} e Element attach event
8495 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8502 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8506 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
8510 * Automatically adjust the size of the text input.
8512 * This only affects multi-line inputs that are auto-sized.
8516 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
8517 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
8519 if ( this.multiline
&& this.autosize
) {
8520 $clone
= this.$input
.clone()
8521 .val( this.$input
.val() )
8522 .css( { 'height': 0 } )
8523 .insertAfter( this.$input
);
8524 // Set inline height property to 0 to measure scroll height
8525 scrollHeight
= $clone
[0].scrollHeight
;
8526 // Remove inline height property to measure natural heights
8527 $clone
.css( 'height', '' );
8528 innerHeight
= $clone
.innerHeight();
8529 outerHeight
= $clone
.outerHeight();
8530 // Measure max rows height
8531 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
8532 maxInnerHeight
= $clone
.innerHeight();
8533 $clone
.removeAttr( 'rows' ).css( 'height', '' );
8535 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
8536 // Only apply inline height when expansion beyond natural height is needed
8539 // Use the difference between the inner and outer height as a buffer
8540 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
8547 * Get input element.
8549 * @param {Object} [config] Configuration options
8550 * @return {jQuery} Input element
8552 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
8553 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
8559 * Check if input supports multiple lines.
8563 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
8564 return !!this.multiline
;
8568 * Check if input automatically adjusts its size.
8572 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
8573 return !!this.autosize
;
8577 * Check if input is pending.
8581 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
8582 return !!this.pending
;
8586 * Increase the pending stack.
8590 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
8591 if ( this.pending
=== 0 ) {
8592 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
8593 this.$input
.addClass( 'oo-ui-texture-pending' );
8601 * Reduce the pending stack.
8607 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
8608 if ( this.pending
=== 1 ) {
8609 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
8610 this.$input
.removeClass( 'oo-ui-texture-pending' );
8612 this.pending
= Math
.max( 0, this.pending
- 1 );
8618 * Select the contents of the input.
8622 OO
.ui
.TextInputWidget
.prototype.select = function () {
8623 this.$input
.select();
8631 * @extends OO.ui.Widget
8632 * @mixins OO.ui.LabeledElement
8635 * @param {Object} [config] Configuration options
8637 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
8638 // Config intialization
8639 config
= config
|| {};
8641 // Parent constructor
8642 OO
.ui
.LabelWidget
.super.call( this, config
);
8644 // Mixin constructors
8645 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
8648 this.input
= config
.input
;
8651 if ( this.input
instanceof OO
.ui
.InputWidget
) {
8652 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8656 this.$element
.addClass( 'oo-ui-labelWidget' );
8661 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
8662 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
8664 /* Static Properties */
8666 OO
.ui
.LabelWidget
.static.tagName
= 'label';
8671 * Handles label mouse click events.
8673 * @param {jQuery.Event} e Mouse click event
8675 OO
.ui
.LabelWidget
.prototype.onClick = function () {
8676 this.input
.simulateLabelClick();
8681 * Generic option widget for use with OO.ui.SelectWidget.
8684 * @extends OO.ui.Widget
8685 * @mixins OO.ui.LabeledElement
8686 * @mixins OO.ui.FlaggableElement
8689 * @param {Mixed} data Option data
8690 * @param {Object} [config] Configuration options
8691 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
8693 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
8694 // Config intialization
8695 config
= config
|| {};
8697 // Parent constructor
8698 OO
.ui
.OptionWidget
.super.call( this, config
);
8700 // Mixin constructors
8701 OO
.ui
.ItemWidget
.call( this );
8702 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
8703 OO
.ui
.FlaggableElement
.call( this, config
);
8707 this.selected
= false;
8708 this.highlighted
= false;
8709 this.pressed
= false;
8713 .data( 'oo-ui-optionWidget', this )
8714 .attr( 'rel', config
.rel
)
8715 .addClass( 'oo-ui-optionWidget' )
8716 .append( this.$label
);
8718 .prepend( this.$icon
)
8719 .append( this.$indicator
);
8724 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
8725 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
8726 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
8727 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
8729 /* Static Properties */
8731 OO
.ui
.OptionWidget
.static.tagName
= 'li';
8733 OO
.ui
.OptionWidget
.static.selectable
= true;
8735 OO
.ui
.OptionWidget
.static.highlightable
= true;
8737 OO
.ui
.OptionWidget
.static.pressable
= true;
8739 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
8744 * Check if option can be selected.
8746 * @return {boolean} Item is selectable
8748 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
8749 return this.constructor.static.selectable
&& !this.isDisabled();
8753 * Check if option can be highlighted.
8755 * @return {boolean} Item is highlightable
8757 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
8758 return this.constructor.static.highlightable
&& !this.isDisabled();
8762 * Check if option can be pressed.
8764 * @return {boolean} Item is pressable
8766 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
8767 return this.constructor.static.pressable
&& !this.isDisabled();
8771 * Check if option is selected.
8773 * @return {boolean} Item is selected
8775 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
8776 return this.selected
;
8780 * Check if option is highlighted.
8782 * @return {boolean} Item is highlighted
8784 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
8785 return this.highlighted
;
8789 * Check if option is pressed.
8791 * @return {boolean} Item is pressed
8793 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
8794 return this.pressed
;
8798 * Set selected state.
8800 * @param {boolean} [state=false] Select option
8803 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
8804 if ( this.constructor.static.selectable
) {
8805 this.selected
= !!state
;
8806 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
8807 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
8808 this.scrollElementIntoView();
8815 * Set highlighted state.
8817 * @param {boolean} [state=false] Highlight option
8820 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
8821 if ( this.constructor.static.highlightable
) {
8822 this.highlighted
= !!state
;
8823 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
8829 * Set pressed state.
8831 * @param {boolean} [state=false] Press option
8834 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
8835 if ( this.constructor.static.pressable
) {
8836 this.pressed
= !!state
;
8837 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
8843 * Make the option's highlight flash.
8845 * While flashing, the visual style of the pressed state is removed if present.
8847 * @return {jQuery.Promise} Promise resolved when flashing is done
8849 OO
.ui
.OptionWidget
.prototype.flash = function () {
8851 $element
= this.$element
,
8852 deferred
= $.Deferred();
8854 if ( !this.isDisabled() && this.constructor.static.pressable
) {
8855 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
8856 setTimeout( function () {
8857 // Restore original classes
8859 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
8860 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
8862 setTimeout( function () {
8869 return deferred
.promise();
8875 * @return {Mixed} Option data
8877 OO
.ui
.OptionWidget
.prototype.getData = function () {
8882 * Option widget with an option icon and indicator.
8884 * Use together with OO.ui.SelectWidget.
8887 * @extends OO.ui.OptionWidget
8888 * @mixins OO.ui.IconedElement
8889 * @mixins OO.ui.IndicatedElement
8892 * @param {Mixed} data Option data
8893 * @param {Object} [config] Configuration options
8895 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
8896 // Parent constructor
8897 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
8899 // Mixin constructors
8900 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
8901 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
8905 .addClass( 'oo-ui-decoratedOptionWidget' )
8906 .prepend( this.$icon
)
8907 .append( this.$indicator
);
8912 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
8913 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
8914 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
8917 * Option widget that looks like a button.
8919 * Use together with OO.ui.ButtonSelectWidget.
8922 * @extends OO.ui.DecoratedOptionWidget
8923 * @mixins OO.ui.ButtonedElement
8926 * @param {Mixed} data Option data
8927 * @param {Object} [config] Configuration options
8929 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
8930 // Parent constructor
8931 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
8933 // Mixin constructors
8934 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
8937 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
8938 this.$button
.append( this.$element
.contents() );
8939 this.$element
.append( this.$button
);
8944 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
8945 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
8947 /* Static Properties */
8949 // Allow button mouse down events to pass through so they can be handled by the parent select widget
8950 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
8957 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
8958 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
8960 if ( this.constructor.static.selectable
) {
8961 this.setActive( state
);
8968 * Item of an OO.ui.MenuWidget.
8971 * @extends OO.ui.DecoratedOptionWidget
8974 * @param {Mixed} data Item data
8975 * @param {Object} [config] Configuration options
8977 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
8978 // Configuration initialization
8979 config
= $.extend( { 'icon': 'check' }, config
);
8981 // Parent constructor
8982 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
8985 this.$element
.addClass( 'oo-ui-menuItemWidget' );
8990 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
8993 * Section to group one or more items in a OO.ui.MenuWidget.
8996 * @extends OO.ui.DecoratedOptionWidget
8999 * @param {Mixed} data Item data
9000 * @param {Object} [config] Configuration options
9002 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
9003 // Parent constructor
9004 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
9007 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
9012 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9014 /* Static Properties */
9016 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
9018 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
9021 * Items for an OO.ui.OutlineWidget.
9024 * @extends OO.ui.DecoratedOptionWidget
9027 * @param {Mixed} data Item data
9028 * @param {Object} [config] Configuration options
9029 * @cfg {number} [level] Indentation level
9030 * @cfg {boolean} [movable] Allow modification from outline controls
9032 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
9033 // Config intialization
9034 config
= config
|| {};
9036 // Parent constructor
9037 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
9041 this.movable
= !!config
.movable
;
9042 this.removable
= !!config
.removable
;
9045 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
9046 this.setLevel( config
.level
);
9051 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9053 /* Static Properties */
9055 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
9057 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
9059 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
9061 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
9066 * Check if item is movable.
9068 * Movablilty is used by outline controls.
9070 * @return {boolean} Item is movable
9072 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
9073 return this.movable
;
9077 * Check if item is removable.
9079 * Removablilty is used by outline controls.
9081 * @return {boolean} Item is removable
9083 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
9084 return this.removable
;
9088 * Get indentation level.
9090 * @return {number} Indentation level
9092 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
9099 * Movablilty is used by outline controls.
9101 * @param {boolean} movable Item is movable
9104 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
9105 this.movable
= !!movable
;
9112 * Removablilty is used by outline controls.
9114 * @param {boolean} movable Item is removable
9117 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
9118 this.removable
= !!removable
;
9123 * Set indentation level.
9125 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
9128 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
9129 var levels
= this.constructor.static.levels
,
9130 levelClass
= this.constructor.static.levelClass
,
9133 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
9135 if ( this.level
=== i
) {
9136 this.$element
.addClass( levelClass
+ i
);
9138 this.$element
.removeClass( levelClass
+ i
);
9146 * Container for content that is overlaid and positioned absolutely.
9149 * @extends OO.ui.Widget
9150 * @mixins OO.ui.LabeledElement
9153 * @param {Object} [config] Configuration options
9154 * @cfg {number} [width=320] Width of popup in pixels
9155 * @cfg {number} [height] Height of popup, omit to use automatic height
9156 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
9157 * @cfg {string} [align='center'] Alignment of popup to origin
9158 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
9159 * @cfg {jQuery} [$content] Content to append to the popup's body
9160 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
9161 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
9162 * @cfg {boolean} [head] Show label and close button at the top
9163 * @cfg {boolean} [padded] Add padding to the body
9165 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
9166 // Config intialization
9167 config
= config
|| {};
9169 // Parent constructor
9170 OO
.ui
.PopupWidget
.super.call( this, config
);
9172 // Mixin constructors
9173 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
9174 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
9177 this.visible
= false;
9178 this.$popup
= this.$( '<div>' );
9179 this.$head
= this.$( '<div>' );
9180 this.$body
= this.$clippable
;
9181 this.$anchor
= this.$( '<div>' );
9182 this.$container
= config
.$container
|| this.$( 'body' );
9183 this.autoClose
= !!config
.autoClose
;
9184 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
9185 this.transitionTimeout
= null;
9187 this.width
= config
.width
!== undefined ? config
.width
: 320;
9188 this.height
= config
.height
!== undefined ? config
.height
: null;
9189 this.align
= config
.align
|| 'center';
9190 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'framed': false, 'icon': 'close' } );
9191 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
9194 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
9197 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
9198 this.$body
.addClass( 'oo-ui-popupWidget-body' );
9199 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
9201 .addClass( 'oo-ui-popupWidget-head' )
9202 .append( this.$label
, this.closeButton
.$element
);
9203 if ( !config
.head
) {
9207 .addClass( 'oo-ui-popupWidget-popup' )
9208 .append( this.$head
, this.$body
);
9211 .addClass( 'oo-ui-popupWidget' )
9212 .append( this.$popup
, this.$anchor
);
9213 // Move content, which was added to #$element by OO.ui.Widget, to the body
9214 if ( config
.$content
instanceof jQuery
) {
9215 this.$body
.append( config
.$content
);
9217 if ( config
.padded
) {
9218 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
9224 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
9225 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
9226 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
9241 * Handles mouse down events.
9243 * @param {jQuery.Event} e Mouse down event
9245 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
9248 !$.contains( this.$element
[0], e
.target
) &&
9249 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
9251 this.toggle( false );
9256 * Bind mouse down listener.
9258 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
9259 // Capture clicks outside popup
9260 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
9264 * Handles close button click events.
9266 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
9267 if ( this.isVisible() ) {
9268 this.toggle( false );
9273 * Unbind mouse down listener.
9275 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
9276 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
9280 * Set whether to show a anchor.
9282 * @param {boolean} [show] Show anchor, omit to toggle
9284 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
9285 show
= show
=== undefined ? !this.anchored
: !!show
;
9287 if ( this.anchored
!== show
) {
9289 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
9291 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
9293 this.anchored
= show
;
9298 * Check if showing a anchor.
9300 * @return {boolean} anchor is visible
9302 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
9309 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
9310 show
= show
=== undefined ? !this.isVisible() : !!show
;
9312 var change
= show
!== this.isVisible();
9315 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
9319 this.setClipping( true );
9320 if ( this.autoClose
) {
9321 this.bindMouseDownListener();
9323 this.updateDimensions();
9325 this.setClipping( false );
9326 if ( this.autoClose
) {
9327 this.unbindMouseDownListener();
9336 * Set the size of the popup.
9338 * Changing the size may also change the popup's position depending on the alignment.
9340 * @param {number} width Width
9341 * @param {number} height Height
9342 * @param {boolean} [transition=false] Use a smooth transition
9345 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
9347 this.height
= height
!== undefined ? height
: null;
9348 if ( this.isVisible() ) {
9349 this.updateDimensions( transition
);
9354 * Update the size and position.
9356 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
9357 * be called automatically.
9359 * @param {boolean} [transition=false] Use a smooth transition
9362 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
9365 originOffset
= Math
.round( this.$element
.offset().left
),
9366 containerLeft
= Math
.round( this.$container
.offset().left
),
9367 containerWidth
= this.$container
.innerWidth(),
9368 containerRight
= containerLeft
+ containerWidth
,
9369 popupOffset
= this.width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
9370 anchorWidth
= this.$anchor
.width(),
9371 popupLeft
= popupOffset
- padding
,
9372 popupRight
= popupOffset
+ padding
+ this.width
+ padding
,
9373 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
9374 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
9376 // Prevent transition from being interrupted
9377 clearTimeout( this.transitionTimeout
);
9379 // Enable transition
9380 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
9383 if ( overlapRight
< 0 ) {
9384 popupOffset
+= overlapRight
;
9385 } else if ( overlapLeft
< 0 ) {
9386 popupOffset
-= overlapLeft
;
9389 // Adjust offset to avoid anchor being rendered too close to the edge
9390 if ( this.align
=== 'right' ) {
9391 popupOffset
+= anchorWidth
;
9392 } else if ( this.align
=== 'left' ) {
9393 popupOffset
-= anchorWidth
;
9396 // Position body relative to anchor and resize
9398 'left': popupOffset
,
9399 'width': this.width
,
9400 'height': this.height
!== null ? this.height
: 'auto'
9404 // Prevent transitioning after transition is complete
9405 this.transitionTimeout
= setTimeout( function () {
9406 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
9409 // Prevent transitioning immediately
9410 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
9419 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
9420 * Results are cleared and populated each time the query is changed.
9423 * @extends OO.ui.Widget
9426 * @param {Object} [config] Configuration options
9427 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
9428 * @cfg {string} [value] Initial query value
9430 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
9431 // Configuration intialization
9432 config
= config
|| {};
9434 // Parent constructor
9435 OO
.ui
.SearchWidget
.super.call( this, config
);
9438 this.query
= new OO
.ui
.TextInputWidget( {
9441 'placeholder': config
.placeholder
,
9442 'value': config
.value
9444 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
9445 this.$query
= this.$( '<div>' );
9446 this.$results
= this.$( '<div>' );
9449 this.query
.connect( this, {
9450 'change': 'onQueryChange',
9451 'enter': 'onQueryEnter'
9453 this.results
.connect( this, {
9454 'highlight': 'onResultsHighlight',
9455 'select': 'onResultsSelect'
9457 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
9461 .addClass( 'oo-ui-searchWidget-query' )
9462 .append( this.query
.$element
);
9464 .addClass( 'oo-ui-searchWidget-results' )
9465 .append( this.results
.$element
);
9467 .addClass( 'oo-ui-searchWidget' )
9468 .append( this.$results
, this.$query
);
9473 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
9479 * @param {Object|null} item Item data or null if no item is highlighted
9484 * @param {Object|null} item Item data or null if no item is selected
9490 * Handle query key down events.
9492 * @param {jQuery.Event} e Key down event
9494 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
9495 var highlightedItem
, nextItem
,
9496 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
9499 highlightedItem
= this.results
.getHighlightedItem();
9500 if ( !highlightedItem
) {
9501 highlightedItem
= this.results
.getSelectedItem();
9503 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
9504 this.results
.highlightItem( nextItem
);
9505 nextItem
.scrollElementIntoView();
9510 * Handle select widget select events.
9512 * Clears existing results. Subclasses should repopulate items according to new query.
9514 * @param {string} value New value
9516 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
9518 this.results
.clearItems();
9522 * Handle select widget enter key events.
9524 * Selects highlighted item.
9526 * @param {string} value New value
9528 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
9530 this.results
.selectItem( this.results
.getHighlightedItem() );
9534 * Handle select widget highlight events.
9536 * @param {OO.ui.OptionWidget} item Highlighted item
9539 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
9540 this.emit( 'highlight', item
? item
.getData() : null );
9544 * Handle select widget select events.
9546 * @param {OO.ui.OptionWidget} item Selected item
9549 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
9550 this.emit( 'select', item
? item
.getData() : null );
9554 * Get the query input.
9556 * @return {OO.ui.TextInputWidget} Query input
9558 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
9563 * Get the results list.
9565 * @return {OO.ui.SelectWidget} Select list
9567 OO
.ui
.SearchWidget
.prototype.getResults = function () {
9568 return this.results
;
9572 * Generic selection of options.
9574 * Items can contain any rendering, and are uniquely identified by a has of thier data. Any widget
9575 * that provides options, from which the user must choose one, should be built on this class.
9577 * Use together with OO.ui.OptionWidget.
9580 * @extends OO.ui.Widget
9581 * @mixins OO.ui.GroupElement
9584 * @param {Object} [config] Configuration options
9585 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
9587 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
9588 // Config intialization
9589 config
= config
|| {};
9591 // Parent constructor
9592 OO
.ui
.SelectWidget
.super.call( this, config
);
9594 // Mixin constructors
9595 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
9598 this.pressed
= false;
9599 this.selecting
= null;
9601 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
9602 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
9606 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
9607 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
9608 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
9612 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
9613 if ( $.isArray( config
.items
) ) {
9614 this.addItems( config
.items
);
9620 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
9622 // Need to mixin base class as well
9623 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
9624 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
9630 * @param {OO.ui.OptionWidget|null} item Highlighted item
9635 * @param {OO.ui.OptionWidget|null} item Pressed item
9640 * @param {OO.ui.OptionWidget|null} item Selected item
9645 * @param {OO.ui.OptionWidget|null} item Chosen item
9650 * @param {OO.ui.OptionWidget[]} items Added items
9651 * @param {number} index Index items were added at
9656 * @param {OO.ui.OptionWidget[]} items Removed items
9659 /* Static Properties */
9661 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
9666 * Handle mouse down events.
9669 * @param {jQuery.Event} e Mouse down event
9671 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
9674 if ( !this.isDisabled() && e
.which
=== 1 ) {
9675 this.togglePressed( true );
9676 item
= this.getTargetItem( e
);
9677 if ( item
&& item
.isSelectable() ) {
9678 this.pressItem( item
);
9679 this.selecting
= item
;
9680 this.getElementDocument().addEventListener(
9682 this.onMouseUpHandler
,
9685 this.getElementDocument().addEventListener(
9687 this.onMouseMoveHandler
,
9696 * Handle mouse up events.
9699 * @param {jQuery.Event} e Mouse up event
9701 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
9704 this.togglePressed( false );
9705 if ( !this.selecting
) {
9706 item
= this.getTargetItem( e
);
9707 if ( item
&& item
.isSelectable() ) {
9708 this.selecting
= item
;
9711 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
9712 this.pressItem( null );
9713 this.chooseItem( this.selecting
);
9714 this.selecting
= null;
9717 this.getElementDocument().removeEventListener(
9719 this.onMouseUpHandler
,
9722 this.getElementDocument().removeEventListener(
9724 this.onMouseMoveHandler
,
9732 * Handle mouse move events.
9735 * @param {jQuery.Event} e Mouse move event
9737 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
9740 if ( !this.isDisabled() && this.pressed
) {
9741 item
= this.getTargetItem( e
);
9742 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
9743 this.pressItem( item
);
9744 this.selecting
= item
;
9751 * Handle mouse over events.
9754 * @param {jQuery.Event} e Mouse over event
9756 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
9759 if ( !this.isDisabled() ) {
9760 item
= this.getTargetItem( e
);
9761 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
9767 * Handle mouse leave events.
9770 * @param {jQuery.Event} e Mouse over event
9772 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
9773 if ( !this.isDisabled() ) {
9774 this.highlightItem( null );
9780 * Get the closest item to a jQuery.Event.
9783 * @param {jQuery.Event} e
9784 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
9786 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
9787 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
9788 if ( $item
.length
) {
9789 return $item
.data( 'oo-ui-optionWidget' );
9795 * Get selected item.
9797 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
9799 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
9802 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9803 if ( this.items
[i
].isSelected() ) {
9804 return this.items
[i
];
9811 * Get highlighted item.
9813 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
9815 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
9818 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9819 if ( this.items
[i
].isHighlighted() ) {
9820 return this.items
[i
];
9827 * Get an existing item with equivilant data.
9829 * @param {Object} data Item data to search for
9830 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
9832 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
9833 var hash
= OO
.getHash( data
);
9835 if ( hash
in this.hashes
) {
9836 return this.hashes
[hash
];
9843 * Toggle pressed state.
9845 * @param {boolean} pressed An option is being pressed
9847 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
9848 if ( pressed
=== undefined ) {
9849 pressed
= !this.pressed
;
9851 if ( pressed
!== this.pressed
) {
9853 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
9854 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
9855 this.pressed
= pressed
;
9860 * Highlight an item.
9862 * Highlighting is mutually exclusive.
9864 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
9868 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
9869 var i
, len
, highlighted
,
9872 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9873 highlighted
= this.items
[i
] === item
;
9874 if ( this.items
[i
].isHighlighted() !== highlighted
) {
9875 this.items
[i
].setHighlighted( highlighted
);
9880 this.emit( 'highlight', item
);
9889 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
9893 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
9894 var i
, len
, selected
,
9897 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9898 selected
= this.items
[i
] === item
;
9899 if ( this.items
[i
].isSelected() !== selected
) {
9900 this.items
[i
].setSelected( selected
);
9905 this.emit( 'select', item
);
9914 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
9918 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
9919 var i
, len
, pressed
,
9922 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9923 pressed
= this.items
[i
] === item
;
9924 if ( this.items
[i
].isPressed() !== pressed
) {
9925 this.items
[i
].setPressed( pressed
);
9930 this.emit( 'press', item
);
9939 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
9940 * an item is selected using the keyboard or mouse.
9942 * @param {OO.ui.OptionWidget} item Item to choose
9946 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
9947 this.selectItem( item
);
9948 this.emit( 'choose', item
);
9954 * Get an item relative to another one.
9956 * @param {OO.ui.OptionWidget} item Item to start at
9957 * @param {number} direction Direction to move in
9958 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
9960 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
9961 var inc
= direction
> 0 ? 1 : -1,
9962 len
= this.items
.length
,
9963 index
= item
instanceof OO
.ui
.OptionWidget
?
9964 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
9965 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
9967 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
9968 Math
.max( index
, -1 ) :
9969 // Default to n-1 instead of -1, if nothing is selected let's start at the end
9970 Math
.min( index
, len
);
9973 i
= ( i
+ inc
+ len
) % len
;
9974 item
= this.items
[i
];
9975 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
9978 // Stop iterating when we've looped all the way around
9979 if ( i
=== stopAt
) {
9987 * Get the next selectable item.
9989 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
9991 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
9994 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9995 item
= this.items
[i
];
9996 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10007 * When items are added with the same values as existing items, the existing items will be
10008 * automatically removed before the new items are added.
10010 * @param {OO.ui.OptionWidget[]} items Items to add
10011 * @param {number} [index] Index to insert items after
10015 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
10016 var i
, len
, item
, hash
,
10019 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10021 hash
= OO
.getHash( item
.getData() );
10022 if ( hash
in this.hashes
) {
10023 // Remove item with same value
10024 remove
.push( this.hashes
[hash
] );
10026 this.hashes
[hash
] = item
;
10028 if ( remove
.length
) {
10029 this.removeItems( remove
);
10033 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
10035 // Always provide an index, even if it was omitted
10036 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
10044 * Items will be detached, not removed, so they can be used later.
10046 * @param {OO.ui.OptionWidget[]} items Items to remove
10050 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
10051 var i
, len
, item
, hash
;
10053 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10055 hash
= OO
.getHash( item
.getData() );
10056 if ( hash
in this.hashes
) {
10057 // Remove existing item
10058 delete this.hashes
[hash
];
10060 if ( item
.isSelected() ) {
10061 this.selectItem( null );
10066 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
10068 this.emit( 'remove', items
);
10076 * Items will be detached, not removed, so they can be used later.
10081 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
10082 var items
= this.items
.slice();
10087 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
10088 this.selectItem( null );
10090 this.emit( 'remove', items
);
10096 * Select widget containing button options.
10098 * Use together with OO.ui.ButtonOptionWidget.
10101 * @extends OO.ui.SelectWidget
10104 * @param {Object} [config] Configuration options
10106 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
10107 // Parent constructor
10108 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
10111 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
10116 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
10119 * Overlaid menu of options.
10121 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
10124 * Use together with OO.ui.MenuItemWidget.
10127 * @extends OO.ui.SelectWidget
10128 * @mixins OO.ui.ClippableElement
10131 * @param {Object} [config] Configuration options
10132 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
10133 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
10134 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
10136 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
10137 // Config intialization
10138 config
= config
|| {};
10140 // Parent constructor
10141 OO
.ui
.MenuWidget
.super.call( this, config
);
10143 // Mixin constructors
10144 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
10147 this.flashing
= false;
10148 this.visible
= false;
10149 this.newItems
= null;
10150 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
10151 this.$input
= config
.input
? config
.input
.$input
: null;
10152 this.$widget
= config
.widget
? config
.widget
.$element
: null;
10153 this.$previousFocus
= null;
10154 this.isolated
= !config
.input
;
10155 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
10156 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
10161 .addClass( 'oo-ui-menuWidget' );
10166 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
10167 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
10172 * Handles document mouse down events.
10174 * @param {jQuery.Event} e Key down event
10176 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
10177 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
10178 this.toggle( false );
10183 * Handles key down events.
10185 * @param {jQuery.Event} e Key down event
10187 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
10190 highlightItem
= this.getHighlightedItem();
10192 if ( !this.isDisabled() && this.isVisible() ) {
10193 if ( !highlightItem
) {
10194 highlightItem
= this.getSelectedItem();
10196 switch ( e
.keyCode
) {
10197 case OO
.ui
.Keys
.ENTER
:
10198 this.chooseItem( highlightItem
);
10201 case OO
.ui
.Keys
.UP
:
10202 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
10205 case OO
.ui
.Keys
.DOWN
:
10206 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
10209 case OO
.ui
.Keys
.ESCAPE
:
10210 if ( highlightItem
) {
10211 highlightItem
.setHighlighted( false );
10213 this.toggle( false );
10219 this.highlightItem( nextItem
);
10220 nextItem
.scrollElementIntoView();
10224 e
.preventDefault();
10225 e
.stopPropagation();
10232 * Bind key down listener.
10234 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
10235 if ( this.$input
) {
10236 this.$input
.on( 'keydown', this.onKeyDownHandler
);
10238 // Capture menu navigation keys
10239 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
10244 * Unbind key down listener.
10246 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
10247 if ( this.$input
) {
10248 this.$input
.off( 'keydown' );
10250 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
10257 * This will close the menu when done, unlike selectItem which only changes selection.
10259 * @param {OO.ui.OptionWidget} item Item to choose
10262 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
10266 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
10268 if ( item
&& !this.flashing
) {
10269 this.flashing
= true;
10270 item
.flash().done( function () {
10271 widget
.toggle( false );
10272 widget
.flashing
= false;
10275 this.toggle( false );
10284 * Adding an existing item (by value) will move it.
10286 * @param {OO.ui.MenuItemWidget[]} items Items to add
10287 * @param {number} [index] Index to insert items after
10290 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
10294 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
10297 if ( !this.newItems
) {
10298 this.newItems
= [];
10301 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10303 if ( this.isVisible() ) {
10304 // Defer fitting label until
10307 this.newItems
.push( item
);
10317 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
10318 visible
= !!visible
&& !!this.items
.length
;
10321 change
= visible
!== this.isVisible();
10324 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
10328 this.bindKeyDownListener();
10330 // Change focus to enable keyboard navigation
10331 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
10332 this.$previousFocus
= this.$( ':focus' );
10333 this.$input
.focus();
10335 if ( this.newItems
&& this.newItems
.length
) {
10336 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
10337 this.newItems
[i
].fitLabel();
10339 this.newItems
= null;
10341 this.setClipping( true );
10344 if ( this.autoHide
) {
10345 this.getElementDocument().addEventListener(
10346 'mousedown', this.onDocumentMouseDownHandler
, true
10350 this.unbindKeyDownListener();
10351 if ( this.isolated
&& this.$previousFocus
) {
10352 this.$previousFocus
.focus();
10353 this.$previousFocus
= null;
10355 this.getElementDocument().removeEventListener(
10356 'mousedown', this.onDocumentMouseDownHandler
, true
10358 this.setClipping( false );
10366 * Menu for a text input widget.
10368 * This menu is specially designed to be positioned beneeth the text input widget. Even if the input
10369 * is in a different frame, the menu's position is automatically calulated and maintained when the
10370 * menu is toggled or the window is resized.
10373 * @extends OO.ui.MenuWidget
10376 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
10377 * @param {Object} [config] Configuration options
10378 * @cfg {jQuery} [$container=input.$element] Element to render menu under
10380 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
10381 // Parent constructor
10382 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
10385 this.input
= input
;
10386 this.$container
= config
.$container
|| this.input
.$element
;
10387 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
10390 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
10395 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
10400 * Handle window resize event.
10402 * @param {jQuery.Event} e Window resize event
10404 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
10411 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
10412 visible
= !!visible
;
10414 var change
= visible
!== this.isVisible();
10417 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
10420 if ( this.isVisible() ) {
10422 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
10424 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
10431 * Position the menu.
10435 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
10437 $container
= this.$container
,
10438 dimensions
= $container
.offset();
10440 // Position under input
10441 dimensions
.top
+= $container
.height();
10443 // Compensate for frame position if in a differnt frame
10444 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
10445 frameOffset
= OO
.ui
.Element
.getRelativePosition(
10446 this.input
.$.frame
.$element
, this.$element
.offsetParent()
10448 dimensions
.left
+= frameOffset
.left
;
10449 dimensions
.top
+= frameOffset
.top
;
10451 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
10452 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
10453 dimensions
.right
= this.$element
.parent().position().left
-
10454 $container
.width() - dimensions
.left
;
10455 // Erase the value for 'left':
10456 delete dimensions
.left
;
10459 this.$element
.css( dimensions
);
10460 this.setIdealSize( $container
.width() );
10466 * Structured list of items.
10468 * Use with OO.ui.OutlineItemWidget.
10471 * @extends OO.ui.SelectWidget
10474 * @param {Object} [config] Configuration options
10476 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
10477 // Config intialization
10478 config
= config
|| {};
10480 // Parent constructor
10481 OO
.ui
.OutlineWidget
.super.call( this, config
);
10484 this.$element
.addClass( 'oo-ui-outlineWidget' );
10489 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
10492 * Switch that slides on and off.
10495 * @extends OO.ui.Widget
10496 * @mixins OO.ui.ToggleWidget
10499 * @param {Object} [config] Configuration options
10500 * @cfg {boolean} [value=false] Initial value
10502 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
10503 // Parent constructor
10504 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
10506 // Mixin constructors
10507 OO
.ui
.ToggleWidget
.call( this, config
);
10510 this.dragging
= false;
10511 this.dragStart
= null;
10512 this.sliding
= false;
10513 this.$glow
= this.$( '<span>' );
10514 this.$grip
= this.$( '<span>' );
10517 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
10520 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
10521 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
10523 .addClass( 'oo-ui-toggleSwitchWidget' )
10524 .append( this.$glow
, this.$grip
);
10529 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
10530 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
10535 * Handle mouse down events.
10537 * @param {jQuery.Event} e Mouse down event
10539 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
10540 if ( !this.isDisabled() && e
.which
=== 1 ) {
10541 this.setValue( !this.value
);