2 * OOjs UI v0.1.0-pre (0e50d2e336)
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-08-06T12:56:55Z
16 * Namespace for all classes, static methods and static properties.
48 * Get the user's language and any fallback languages.
50 * These language codes are used to localize user interface elements in the user's language.
52 * In environments that provide a localization system, this function should be overridden to
53 * return the user's language(s). The default implementation returns English (en) only.
55 * @return {string[]} Language codes, in descending order of priority
57 OO
.ui
.getUserLanguages = function () {
62 * Get a value in an object keyed by language code.
64 * @param {Object.<string,Mixed>} obj Object keyed by language code
65 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
66 * @param {string} [fallback] Fallback code, used if no matching language can be found
67 * @return {Mixed} Local value
69 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
76 // Known user language
77 langs
= OO
.ui
.getUserLanguages();
78 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
85 if ( obj
[fallback
] ) {
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
,
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
;
1121 .addClass( 'oo-ui-frame' )
1122 .attr( { frameborder
: 0, scrolling
: 'no' } );
1128 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
1129 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
1131 /* Static Properties */
1137 OO
.ui
.Frame
.static.tagName
= 'iframe';
1145 /* Static Methods */
1148 * Transplant the CSS styles from as parent document to a frame's document.
1150 * This loops over the style sheets in the parent document, and copies their nodes to the
1151 * frame's document. It then polls the document to see when all styles have loaded, and once they
1152 * have, resolves the promise.
1154 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1155 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1156 * Firefox, where the styles won't load until the iframe becomes visible.
1158 * For details of how we arrived at the strategy used in this function, see #load.
1162 * @param {HTMLDocument} parentDoc Document to transplant styles from
1163 * @param {HTMLDocument} frameDoc Document to transplant styles to
1164 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1165 * @return {jQuery.Promise} Promise resolved when styles have loaded
1167 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1168 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1169 $pollNodes
= $( [] ),
1170 // Fake font-family value
1171 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1172 deferred
= $.Deferred();
1174 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1175 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1176 if ( styleNode
.disabled
) {
1179 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1180 // External stylesheet
1181 // Create a node with a unique ID that we're going to monitor to see when the CSS
1183 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
1184 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1185 .attr( 'id', pollNodeId
)
1186 .appendTo( frameDoc
.body
)
1189 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
1190 // The font-family rule will only take effect once the @import finishes
1191 newNode
= frameDoc
.createElement( 'style' );
1192 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
1193 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1195 // Not an external stylesheet, or no polling required; just copy the node over
1196 newNode
= frameDoc
.importNode( styleNode
, true );
1198 frameDoc
.head
.appendChild( newNode
);
1201 // Poll every 100ms until all external stylesheets have loaded
1202 $pendingPollNodes
= $pollNodes
;
1203 timeoutID
= setTimeout( function pollExternalStylesheets() {
1205 $pendingPollNodes
.length
> 0 &&
1206 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1208 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1211 if ( $pendingPollNodes
.length
=== 0 ) {
1213 if ( timeoutID
!== null ) {
1215 $pollNodes
.remove();
1219 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1222 // ...but give up after a while
1223 if ( timeout
!== 0 ) {
1224 setTimeout( function () {
1226 clearTimeout( timeoutID
);
1228 $pollNodes
.remove();
1231 }, timeout
|| 5000 );
1234 return deferred
.promise();
1240 * Load the frame contents.
1242 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
1243 * promise will be resolved. Calling while loading will return a promise but not trigger a new
1244 * loading cycle. Calling after loading is complete will return a promise that's already been
1247 * Sounds simple right? Read on...
1249 * When you create a dynamic iframe using open/write/close, the window.load event for the
1250 * iframe is triggered when you call close, and there's no further load event to indicate that
1251 * everything is actually loaded.
1253 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
1254 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
1255 * are added to document.styleSheets immediately, and the only way you can determine whether they've
1256 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
1257 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
1259 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
1260 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
1261 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
1262 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
1263 * .cssRules is allowed.
1265 * However, now that we control the styles we're injecting, we might as well do away with
1266 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
1267 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
1268 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
1269 * rule is not applied until after the `@import` finishes.
1271 * All this stylesheet injection and polling magic is in #transplantStyles.
1273 * @return {jQuery.Promise} Promise resolved when loading is complete
1276 OO
.ui
.Frame
.prototype.load = function () {
1280 // Return existing promise if already loading or loaded
1281 if ( this.loading
) {
1282 return this.loading
.promise();
1286 this.loading
= $.Deferred();
1288 win
= this.$element
.prop( 'contentWindow' );
1291 // Cache directionality
1292 this.dir
= OO
.ui
.Element
.getDir( this.$element
) || 'ltr';
1294 // Initialize contents
1296 // The following classes can be used here:
1302 '<body class="oo-ui-frame-content oo-ui-' + this.getDir() + '" dir="' + this.getDir() + '">' +
1309 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
1310 this.$content
= this.$( '.oo-ui-frame-content' ).attr( 'tabIndex', 0 );
1311 this.$document
= this.$( doc
);
1314 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
1315 .always( function () {
1316 frame
.emit( 'load' );
1317 frame
.loading
.resolve();
1320 return this.loading
.promise();
1324 * Set the size of the frame.
1326 * @param {number} width Frame width in pixels
1327 * @param {number} height Frame height in pixels
1330 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
1331 this.$element
.css( { width
: width
, height
: height
} );
1336 * Get the directionality of the frame
1338 * @return {string} Directionality, 'ltr' or 'rtl'
1340 OO
.ui
.Frame
.prototype.getDir = function () {
1345 * Container for elements.
1349 * @extends OO.ui.Element
1350 * @mixins OO.EventEmitter
1353 * @param {Object} [config] Configuration options
1355 OO
.ui
.Layout
= function OoUiLayout( config
) {
1356 // Initialize config
1357 config
= config
|| {};
1359 // Parent constructor
1360 OO
.ui
.Layout
.super.call( this, config
);
1362 // Mixin constructors
1363 OO
.EventEmitter
.call( this );
1366 this.$element
.addClass( 'oo-ui-layout' );
1371 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1372 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1375 * User interface control.
1379 * @extends OO.ui.Element
1380 * @mixins OO.EventEmitter
1383 * @param {Object} [config] Configuration options
1384 * @cfg {boolean} [disabled=false] Disable
1386 OO
.ui
.Widget
= function OoUiWidget( config
) {
1387 // Initialize config
1388 config
= $.extend( { disabled
: false }, config
);
1390 // Parent constructor
1391 OO
.ui
.Widget
.super.call( this, config
);
1393 // Mixin constructors
1394 OO
.EventEmitter
.call( this );
1397 this.visible
= true;
1398 this.disabled
= null;
1399 this.wasDisabled
= null;
1402 this.$element
.addClass( 'oo-ui-widget' );
1403 this.setDisabled( !!config
.disabled
);
1408 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1409 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1415 * @param {boolean} disabled Widget is disabled
1420 * @param {boolean} visible Widget is visible
1426 * Check if the widget is disabled.
1428 * @param {boolean} Button is disabled
1430 OO
.ui
.Widget
.prototype.isDisabled = function () {
1431 return this.disabled
;
1435 * Check if widget is visible.
1437 * @return {boolean} Widget is visible
1439 OO
.ui
.Widget
.prototype.isVisible = function () {
1440 return this.visible
;
1444 * Set the disabled state of the widget.
1446 * This should probably change the widgets' appearance and prevent it from being used.
1448 * @param {boolean} disabled Disable widget
1451 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1454 this.disabled
= !!disabled
;
1455 isDisabled
= this.isDisabled();
1456 if ( isDisabled
!== this.wasDisabled
) {
1457 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1458 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1459 this.emit( 'disable', isDisabled
);
1461 this.wasDisabled
= isDisabled
;
1467 * Toggle visibility of widget.
1469 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1473 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1474 show
= show
=== undefined ? !this.visible
: !!show
;
1476 if ( show
!== this.isVisible() ) {
1477 this.visible
= show
;
1478 this.$element
.toggle( show
);
1479 this.emit( 'toggle', show
);
1486 * Update the disabled state, in case of changes in parent widget.
1490 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1491 this.setDisabled( this.disabled
);
1496 * Container for elements in a child frame.
1498 * Use together with OO.ui.WindowManager.
1502 * @extends OO.ui.Element
1503 * @mixins OO.EventEmitter
1505 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1506 * teardown processes are executed when the window is closed.
1508 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1509 * - Window manager begins opening window
1510 * - {@link #getSetupProcess} method is called and its result executed
1511 * - {@link #getReadyProcess} method is called and its result executed
1512 * - Window is now open
1514 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1515 * - Window manager begins closing window
1516 * - {@link #getHoldProcess} method is called and its result executed
1517 * - {@link #getTeardownProcess} method is called and its result executed
1518 * - Window is now closed
1520 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1521 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1522 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1523 * processing can complete. Always assume window processes are executed asychronously. See
1524 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1525 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1527 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1528 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1531 * @param {OO.ui.WindowManager} manager Manager of window
1532 * @param {Object} [config] Configuration options
1533 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1537 OO
.ui
.Window
= function OoUiWindow( manager
, config
) {
1540 // Configuration initialization
1541 config
= config
|| {};
1543 // Parent constructor
1544 OO
.ui
.Window
.super.call( this, config
);
1546 // Mixin constructors
1547 OO
.EventEmitter
.call( this );
1549 if ( !( manager
instanceof OO
.ui
.WindowManager
) ) {
1550 throw new Error( 'Cannot construct window: window must have a manager' );
1554 this.manager
= manager
;
1555 this.initialized
= false;
1556 this.visible
= false;
1557 this.opening
= null;
1558 this.closing
= null;
1561 this.size
= config
.size
|| this.constructor.static.size
;
1562 this.frame
= new OO
.ui
.Frame( { $: this.$ } );
1563 this.$frame
= this.$( '<div>' );
1564 this.$ = function () {
1565 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1570 .addClass( 'oo-ui-window' )
1571 // Hide the window using visibility: hidden; while the iframe is still loading
1572 // Can't use display: none; because that prevents the iframe from loading in Firefox
1573 .css( 'visibility', 'hidden' )
1574 .append( this.$frame
);
1576 .addClass( 'oo-ui-window-frame' )
1577 .append( this.frame
.$element
);
1580 this.frame
.on( 'load', function () {
1582 win
.initialized
= true;
1583 // Undo the visibility: hidden; hack and apply display: none;
1584 // We can do this safely now that the iframe has initialized
1585 // (don't do this from within #initialize because it has to happen
1586 // after the all subclasses have been handled as well).
1587 win
.$element
.hide().css( 'visibility', '' );
1593 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1594 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1600 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1603 /* Static Properties */
1606 * Symbolic name of size.
1608 * Size is used if no size is configured during construction.
1612 * @property {string}
1614 OO
.ui
.Window
.static.size
= 'medium';
1619 * Check if window has been initialized.
1621 * @return {boolean} Window has been initialized
1623 OO
.ui
.Window
.prototype.isInitialized = function () {
1624 return this.initialized
;
1628 * Check if window is visible.
1630 * @return {boolean} Window is visible
1632 OO
.ui
.Window
.prototype.isVisible = function () {
1633 return this.visible
;
1637 * Check if window is opening.
1639 * This is a wrapper around OO.ui.WindowManager#isOpening.
1641 * @return {boolean} Window is opening
1643 OO
.ui
.Window
.prototype.isOpening = function () {
1644 return this.manager
.isOpening( this );
1648 * Check if window is closing.
1650 * This is a wrapper around OO.ui.WindowManager#isClosing.
1652 * @return {boolean} Window is closing
1654 OO
.ui
.Window
.prototype.isClosing = function () {
1655 return this.manager
.isClosing( this );
1659 * Check if window is opened.
1661 * This is a wrapper around OO.ui.WindowManager#isOpened.
1663 * @return {boolean} Window is opened
1665 OO
.ui
.Window
.prototype.isOpened = function () {
1666 return this.manager
.isOpened( this );
1670 * Get the window manager.
1672 * @return {OO.ui.WindowManager} Manager of window
1674 OO
.ui
.Window
.prototype.getManager = function () {
1675 return this.manager
;
1679 * Get the window frame.
1681 * @return {OO.ui.Frame} Frame of window
1683 OO
.ui
.Window
.prototype.getFrame = function () {
1688 * Get the window size.
1690 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1692 OO
.ui
.Window
.prototype.getSize = function () {
1697 * Get the height of the dialog contents.
1699 * @return {number} Content height
1701 OO
.ui
.Window
.prototype.getContentHeight = function () {
1703 // Add buffer for border
1704 ( ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) * 2 ) +
1705 // Height of contents
1706 ( this.$head
.outerHeight( true ) + this.getBodyHeight() + this.$foot
.outerHeight( true ) )
1711 * Get the height of the dialog contents.
1713 * @return {number} Height of content
1715 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1716 return this.$body
[0].scrollHeight
;
1720 * Get a process for setting up a window for use.
1722 * Each time the window is opened this process will set it up for use in a particular context, based
1723 * on the `data` argument.
1725 * When you override this method, you can add additional setup steps to the process the parent
1726 * method provides using the 'first' and 'next' methods.
1729 * @param {Object} [data] Window opening data
1730 * @return {OO.ui.Process} Setup process
1732 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1733 return new OO
.ui
.Process();
1737 * Get a process for readying a window for use.
1739 * Each time the window is open and setup, this process will ready it up for use in a particular
1740 * context, based on the `data` argument.
1742 * When you override this method, you can add additional setup steps to the process the parent
1743 * method provides using the 'first' and 'next' methods.
1746 * @param {Object} [data] Window opening data
1747 * @return {OO.ui.Process} Setup process
1749 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1750 return new OO
.ui
.Process();
1754 * Get a process for holding a window from use.
1756 * Each time the window is closed, this process will hold it from use in a particular context, based
1757 * on the `data` argument.
1759 * When you override this method, you can add additional setup steps to the process the parent
1760 * method provides using the 'first' and 'next' methods.
1763 * @param {Object} [data] Window closing data
1764 * @return {OO.ui.Process} Hold process
1766 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1767 return new OO
.ui
.Process();
1771 * Get a process for tearing down a window after use.
1773 * Each time the window is closed this process will tear it down and do something with the user's
1774 * interactions within the window, based on the `data` argument.
1776 * When you override this method, you can add additional teardown steps to the process the parent
1777 * method provides using the 'first' and 'next' methods.
1780 * @param {Object} [data] Window closing data
1781 * @return {OO.ui.Process} Teardown process
1783 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1784 return new OO
.ui
.Process();
1788 * Set the window size.
1790 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1793 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1795 this.manager
.updateWindowSize( this );
1800 * Set window dimensions.
1802 * Properties are applied to the frame container.
1804 * @param {Object} dim CSS dimension properties
1805 * @param {string|number} [dim.width] Width
1806 * @param {string|number} [dim.minWidth] Minimum width
1807 * @param {string|number} [dim.maxWidth] Maximum width
1808 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1809 * @param {string|number} [dim.minWidth] Minimum height
1810 * @param {string|number} [dim.maxWidth] Maximum height
1813 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1814 // Apply width before height so height is not based on wrapping content using the wrong width
1816 width
: dim
.width
|| '',
1817 minWidth
: dim
.minWidth
|| '',
1818 maxWidth
: dim
.maxWidth
|| ''
1821 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1822 minHeight
: dim
.minHeight
|| '',
1823 maxHeight
: dim
.maxHeight
|| ''
1829 * Initialize window contents.
1831 * The first time the window is opened, #initialize is called when it's safe to begin populating
1832 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1834 * Once this method is called, this.$ can be used to create elements within the frame.
1838 OO
.ui
.Window
.prototype.initialize = function () {
1840 this.$ = this.frame
.$;
1841 this.$head
= this.$( '<div>' );
1842 this.$body
= this.$( '<div>' );
1843 this.$foot
= this.$( '<div>' );
1844 this.$overlay
= this.$( '<div>' );
1847 this.$head
.addClass( 'oo-ui-window-head' );
1848 this.$body
.addClass( 'oo-ui-window-body' );
1849 this.$foot
.addClass( 'oo-ui-window-foot' );
1850 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1852 .addClass( 'oo-ui-window-content' )
1853 .append( this.$head
, this.$body
, this.$foot
, this.$overlay
);
1861 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1862 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1864 * @param {Object} [data] Window opening data
1865 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1866 * first argument will be a promise which will be resolved when the window begins closing
1868 OO
.ui
.Window
.prototype.open = function ( data
) {
1869 return this.manager
.openWindow( this, data
);
1875 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1876 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1878 * @param {Object} [data] Window closing data
1879 * @return {jQuery.Promise} Promise resolved when window is closed
1881 OO
.ui
.Window
.prototype.close = function ( data
) {
1882 return this.manager
.closeWindow( this, data
);
1888 * This is called by OO.ui.WindowManager durring window adding, and should not be called directly
1891 * @return {jQuery.Promise} Promise resolved when window is loaded
1893 OO
.ui
.Window
.prototype.load = function () {
1894 return this.frame
.load();
1900 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1903 * @param {Object} [data] Window opening data
1904 * @return {jQuery.Promise} Promise resolved when window is setup
1906 OO
.ui
.Window
.prototype.setup = function ( data
) {
1908 deferred
= $.Deferred();
1910 this.$element
.show();
1911 this.visible
= true;
1912 this.getSetupProcess( data
).execute().done( function () {
1913 win
.manager
.updateWindowSize( win
);
1914 // Force redraw by asking the browser to measure the elements' widths
1915 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
1916 win
.frame
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
1920 return deferred
.promise();
1926 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1929 * @param {Object} [data] Window opening data
1930 * @return {jQuery.Promise} Promise resolved when window is ready
1932 OO
.ui
.Window
.prototype.ready = function ( data
) {
1934 deferred
= $.Deferred();
1936 this.frame
.$content
[0].focus();
1937 this.getReadyProcess( data
).execute().done( function () {
1938 // Force redraw by asking the browser to measure the elements' widths
1939 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
1940 win
.frame
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
1944 return deferred
.promise();
1950 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1953 * @param {Object} [data] Window closing data
1954 * @return {jQuery.Promise} Promise resolved when window is held
1956 OO
.ui
.Window
.prototype.hold = function ( data
) {
1958 deferred
= $.Deferred();
1960 this.getHoldProcess( data
).execute().done( function () {
1961 var $focused
= win
.frame
.$content
.find( ':focus' );
1962 if ( $focused
.length
) {
1965 // Force redraw by asking the browser to measure the elements' widths
1966 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
1967 win
.frame
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
1971 return deferred
.promise();
1977 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1980 * @param {Object} [data] Window closing data
1981 * @return {jQuery.Promise} Promise resolved when window is torn down
1983 OO
.ui
.Window
.prototype.teardown = function ( data
) {
1985 deferred
= $.Deferred();
1987 this.getTeardownProcess( data
).execute().done( function () {
1988 // Force redraw by asking the browser to measure the elements' widths
1989 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
1990 win
.frame
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
1991 win
.$element
.hide();
1992 win
.visible
= false;
1996 return deferred
.promise();
2000 * Base class for all dialogs.
2003 * - Manage the window (open and close, etc.).
2004 * - Store the internal name and display title.
2005 * - A stack to track one or more pending actions.
2006 * - Manage a set of actions that can be performed.
2007 * - Configure and create action widgets.
2010 * - Close the dialog with Escape key.
2011 * - Visually lock the dialog while an action is in
2012 * progress (aka "pending").
2014 * Subclass responsibilities:
2015 * - Display the title somewhere.
2016 * - Add content to the dialog.
2017 * - Provide a UI to close the dialog.
2018 * - Display the action widgets somewhere.
2022 * @extends OO.ui.Window
2023 * @mixins OO.ui.LabeledElement
2026 * @param {Object} [config] Configuration options
2028 OO
.ui
.Dialog
= function OoUiDialog( manager
, config
) {
2029 // Parent constructor
2030 OO
.ui
.Dialog
.super.call( this, manager
, config
);
2033 this.actions
= new OO
.ui
.ActionSet();
2034 this.attachedActions
= [];
2035 this.currentAction
= null;
2039 this.actions
.connect( this, {
2040 click
: 'onActionClick',
2041 resize
: 'onActionResize',
2042 change
: 'onActionsChange'
2047 .addClass( 'oo-ui-dialog' )
2048 .attr( 'role', 'dialog' );
2053 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2055 /* Static Properties */
2058 * Symbolic name of dialog.
2063 * @property {string}
2065 OO
.ui
.Dialog
.static.name
= '';
2073 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2075 OO
.ui
.Dialog
.static.title
= '';
2078 * List of OO.ui.ActionWidget configuration options.
2082 * @property {Object[]}
2084 OO
.ui
.Dialog
.static.actions
= [];
2087 * Close dialog when the escape key is pressed.
2092 * @property {boolean}
2094 OO
.ui
.Dialog
.static.escapable
= true;
2099 * Handle frame document key down events.
2101 * @param {jQuery.Event} e Key down event
2103 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
2104 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2111 * Handle action resized events.
2113 * @param {OO.ui.ActionWidget} action Action that was resized
2115 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2116 // Override in subclass
2120 * Handle action click events.
2122 * @param {OO.ui.ActionWidget} action Action that was clicked
2124 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2125 if ( !this.isPending() ) {
2126 this.currentAction
= action
;
2127 this.executeAction( action
.getAction() );
2132 * Handle actions change event.
2134 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2135 this.detachActions();
2136 if ( !this.isClosing() ) {
2137 this.attachActions();
2142 * Check if input is pending.
2146 OO
.ui
.Dialog
.prototype.isPending = function () {
2147 return !!this.pending
;
2151 * Get set of actions.
2153 * @return {OO.ui.ActionSet}
2155 OO
.ui
.Dialog
.prototype.getActions = function () {
2156 return this.actions
;
2160 * Get a process for taking action.
2162 * When you override this method, you can add additional accept steps to the process the parent
2163 * method provides using the 'first' and 'next' methods.
2166 * @param {string} [action] Symbolic name of action
2167 * @return {OO.ui.Process} Action process
2169 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2170 return new OO
.ui
.Process()
2171 .next( function () {
2173 // An empty action always closes the dialog without data, which should always be
2174 // safe and make no changes
2183 * @param {Object} [data] Dialog opening data
2184 * @param {jQuery|string|Function|null} [data.label] Dialog label, omit to use #static-label
2185 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2186 * action item, omit to use #static-actions
2188 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2192 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2193 .next( function () {
2196 config
= this.constructor.static,
2197 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2199 this.title
.setLabel(
2200 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2202 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2204 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2207 this.actions
.add( items
);
2214 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2216 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2217 .first( function () {
2218 this.actions
.clear();
2219 this.currentAction
= null;
2226 OO
.ui
.Dialog
.prototype.initialize = function () {
2228 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2231 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2234 if ( this.constructor.static.escapable
) {
2235 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
2239 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
2243 * Attach action actions.
2245 OO
.ui
.Dialog
.prototype.attachActions = function () {
2246 // Remember the list of potentially attached actions
2247 this.attachedActions
= this.actions
.get();
2251 * Detach action actions.
2255 OO
.ui
.Dialog
.prototype.detachActions = function () {
2258 // Detach all actions that may have been previously attached
2259 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2260 this.attachedActions
[i
].$element
.detach();
2262 this.attachedActions
= [];
2266 * Execute an action.
2268 * @param {string} action Symbolic name of action to execute
2269 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2271 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2273 return this.getActionProcess( action
).execute()
2274 .always( OO
.ui
.bind( this.popPending
, this ) );
2278 * Increase the pending stack.
2282 OO
.ui
.Dialog
.prototype.pushPending = function () {
2283 if ( this.pending
=== 0 ) {
2284 this.frame
.$content
.addClass( 'oo-ui-actionDialog-content-pending' );
2285 this.$head
.addClass( 'oo-ui-texture-pending' );
2293 * Reduce the pending stack.
2299 OO
.ui
.Dialog
.prototype.popPending = function () {
2300 if ( this.pending
=== 1 ) {
2301 this.frame
.$content
.removeClass( 'oo-ui-actionDialog-content-pending' );
2302 this.$head
.removeClass( 'oo-ui-texture-pending' );
2304 this.pending
= Math
.max( 0, this.pending
- 1 );
2310 * Collection of windows.
2313 * @extends OO.ui.Element
2314 * @mixins OO.EventEmitter
2316 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2317 * already opening or opened, the current window will be closed without data. Empty closing data
2318 * should always result in the window being closed without causing constructive or destructive
2321 * As a window is opened and closed, it passes through several stages and the manager emits several
2322 * corresponding events.
2324 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2325 * - {@link #event-opening} is emitted with `opening` promise
2326 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2327 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2328 * - `setup` progress notification is emitted from opening promise
2329 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2330 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2331 * - `ready` progress notification is emitted from opening promise
2332 * - `opening` promise is resolved with `opened` promise
2333 * - Window is now open
2335 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2336 * - `opened` promise is resolved with `closing` promise
2337 * - {@link #event-closing} is emitted with `closing` promise
2338 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2339 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2340 * - `hold` progress notification is emitted from opening promise
2341 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2342 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2343 * - `teardown` progress notification is emitted from opening promise
2344 * - Closing promise is resolved
2345 * - Window is now closed
2348 * @param {Object} [config] Configuration options
2349 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2350 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2352 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2353 // Configuration initialization
2354 config
= config
|| {};
2356 // Parent constructor
2357 OO
.ui
.WindowManager
.super.call( this, config
);
2359 // Mixin constructors
2360 OO
.EventEmitter
.call( this );
2363 this.factory
= config
.factory
;
2364 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2366 this.opening
= null;
2368 this.closing
= null;
2370 this.currentWindow
= null;
2371 this.$ariaHidden
= null;
2372 this.requestedSize
= null;
2373 this.onWindowResizeTimeout
= null;
2374 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
2375 this.afterWindowResizeHandler
= OO
.ui
.bind( this.afterWindowResize
, this );
2376 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
2377 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
2380 this.$element
.on( 'mousedown', false );
2384 .addClass( 'oo-ui-windowManager' )
2385 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2390 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2391 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2396 * Window is opening.
2398 * Fired when the window begins to be opened.
2401 * @param {OO.ui.Window} win Window that's being opened
2402 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2403 * resolved the first argument will be a promise which will be resolved when the window begins
2404 * closing, the second argument will be the opening data; progress notifications will be fired on
2405 * the promise for `setup` and `ready` when those processes are completed respectively.
2406 * @param {Object} data Window opening data
2410 * Window is closing.
2412 * Fired when the window begins to be closed.
2415 * @param {OO.ui.Window} win Window that's being closed
2416 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2417 * is resolved the first argument will be a the closing data; progress notifications will be fired
2418 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2419 * @param {Object} data Window closing data
2422 /* Static Properties */
2425 * Map of symbolic size names and CSS properties.
2429 * @property {Object}
2431 OO
.ui
.WindowManager
.static.sizes
= {
2442 // These can be non-numeric because they are never used in calculations
2449 * Symbolic name of default size.
2451 * Default size is used if the window's requested size is not recognized.
2455 * @property {string}
2457 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2462 * Handle window resize events.
2464 * @param {jQuery.Event} e Window resize event
2466 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2467 clearTimeout( this.onWindowResizeTimeout
);
2468 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2472 * Handle window resize events.
2474 * @param {jQuery.Event} e Window resize event
2476 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2477 if ( this.currentWindow
) {
2478 this.updateWindowSize( this.currentWindow
);
2483 * Handle window mouse wheel events.
2485 * @param {jQuery.Event} e Mouse wheel event
2487 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2492 * Handle document key down events.
2494 * @param {jQuery.Event} e Key down event
2496 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2497 switch ( e
.which
) {
2498 case OO
.ui
.Keys
.PAGEUP
:
2499 case OO
.ui
.Keys
.PAGEDOWN
:
2500 case OO
.ui
.Keys
.END
:
2501 case OO
.ui
.Keys
.HOME
:
2502 case OO
.ui
.Keys
.LEFT
:
2504 case OO
.ui
.Keys
.RIGHT
:
2505 case OO
.ui
.Keys
.DOWN
:
2506 // Prevent any key events that might cause scrolling
2512 * Check if window is opening.
2514 * @return {boolean} Window is opening
2516 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2517 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2521 * Check if window is closing.
2523 * @return {boolean} Window is closing
2525 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2526 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2530 * Check if window is opened.
2532 * @return {boolean} Window is opened
2534 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2535 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2539 * Check if a window is being managed.
2541 * @param {OO.ui.Window} win Window to check
2542 * @return {boolean} Window is being managed
2544 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2547 for ( name
in this.windows
) {
2548 if ( this.windows
[name
] === win
) {
2557 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2559 * @param {OO.ui.Window} win Window being opened
2560 * @param {Object} [data] Window opening data
2561 * @return {number} Milliseconds to wait
2563 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2568 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2570 * @param {OO.ui.Window} win Window being opened
2571 * @param {Object} [data] Window opening data
2572 * @return {number} Milliseconds to wait
2574 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2579 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2581 * @param {OO.ui.Window} win Window being closed
2582 * @param {Object} [data] Window closing data
2583 * @return {number} Milliseconds to wait
2585 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2590 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2592 * @param {OO.ui.Window} win Window being closed
2593 * @param {Object} [data] Window closing data
2594 * @return {number} Milliseconds to wait
2596 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2597 return this.modal
? 250 : 0;
2601 * Get managed window by symbolic name.
2603 * If window is not yet instantiated, it will be instantiated and added automatically.
2605 * @param {string} name Symbolic window name
2606 * @return {jQuery.Promise} Promise resolved when window is ready to be accessed; when resolved the
2607 * first argument is an OO.ui.Window; when rejected the first argument is an OO.ui.Error
2608 * @throws {Error} If the symbolic name is unrecognized by the factory
2609 * @throws {Error} If the symbolic name unrecognized as a managed window
2611 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2612 var deferred
= $.Deferred(),
2613 win
= this.windows
[name
];
2615 if ( !( win
instanceof OO
.ui
.Window
) ) {
2616 if ( this.factory
) {
2617 if ( !this.factory
.lookup( name
) ) {
2618 deferred
.reject( new OO
.ui
.Error(
2619 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2622 win
= this.factory
.create( name
, this, { $: this.$ } );
2623 this.addWindows( [ win
] ).then(
2624 OO
.ui
.bind( deferred
.resolve
, deferred
, win
),
2629 deferred
.reject( new OO
.ui
.Error(
2630 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2634 deferred
.resolve( win
);
2637 return deferred
.promise();
2641 * Get current window.
2643 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2645 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2646 return this.currentWindow
;
2652 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2653 * @param {Object} [data] Window opening data
2654 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2655 * for more details about the `opening` promise
2658 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2661 opening
= $.Deferred();
2663 // Argument handling
2664 if ( typeof win
=== 'string' ) {
2665 return this.getWindow( win
).then( function ( win
) {
2666 return manager
.openWindow( win
, data
);
2671 if ( !this.hasWindow( win
) ) {
2672 opening
.reject( new OO
.ui
.Error(
2673 'Cannot open window: window is not attached to manager'
2678 if ( opening
.state() !== 'rejected' ) {
2679 // Begin loading the window if it's not loaded already - may take noticable time and we want
2680 // too do this in paralell with any preparatory actions
2681 preparing
.push( win
.load() );
2683 if ( this.opening
|| this.opened
) {
2684 // If a window is currently opening or opened, close it first
2685 preparing
.push( this.closeWindow( this.currentWindow
) );
2686 } else if ( this.closing
) {
2687 // If a window is currently closing, wait for it to complete
2688 preparing
.push( this.closing
);
2691 $.when
.apply( $, preparing
).done( function () {
2692 if ( manager
.modal
) {
2693 manager
.$( manager
.getElementDocument() ).on( {
2694 // Prevent scrolling by keys in top-level window
2695 keydown
: manager
.onDocumentKeyDownHandler
2697 manager
.$( manager
.getElementWindow() ).on( {
2698 // Prevent scrolling by wheel in top-level window
2699 mousewheel
: manager
.onWindowMouseWheelHandler
,
2700 // Start listening for top-level window dimension changes
2701 'orientationchange resize': manager
.onWindowResizeHandler
2703 // Hide other content from screen readers
2704 manager
.$ariaHidden
= $( 'body' )
2706 .not( manager
.$element
.parentsUntil( 'body' ).last() )
2707 .attr( 'aria-hidden', '' );
2709 manager
.currentWindow
= win
;
2710 manager
.opening
= opening
;
2711 manager
.emit( 'opening', win
, opening
, data
);
2712 manager
.updateWindowSize( win
);
2713 setTimeout( function () {
2714 win
.setup( data
).then( function () {
2715 manager
.opening
.notify( { state
: 'setup' } );
2716 setTimeout( function () {
2717 win
.ready( data
).then( function () {
2718 manager
.opening
.notify( { state
: 'ready' } );
2719 manager
.opening
= null;
2720 manager
.opened
= $.Deferred();
2721 opening
.resolve( manager
.opened
.promise(), data
);
2723 }, manager
.getReadyDelay() );
2725 }, manager
.getSetupDelay() );
2735 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2736 * @param {Object} [data] Window closing data
2737 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-closing}
2738 * for more details about the `closing` promise
2739 * @throws {Error} If no window by that name is being managed
2742 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2745 closing
= $.Deferred(),
2746 opened
= this.opened
;
2748 // Argument handling
2749 if ( typeof win
=== 'string' ) {
2750 win
= this.windows
[win
];
2751 } else if ( !this.hasWindow( win
) ) {
2757 closing
.reject( new OO
.ui
.Error(
2758 'Cannot close window: window is not attached to manager'
2760 } else if ( win
!== this.currentWindow
) {
2761 closing
.reject( new OO
.ui
.Error(
2762 'Cannot close window: window already closed with different data'
2764 } else if ( this.closing
) {
2765 closing
.reject( new OO
.ui
.Error(
2766 'Cannot close window: window already closing with different data'
2771 if ( closing
.state() !== 'rejected' ) {
2772 if ( this.opening
) {
2773 // If the window is currently opening, close it when it's done
2774 preparing
.push( this.opening
);
2778 $.when
.apply( $, preparing
).done( function () {
2779 manager
.closing
= closing
;
2780 manager
.emit( 'closing', win
, closing
, data
);
2781 manager
.opened
= null;
2782 opened
.resolve( closing
.promise(), data
);
2783 setTimeout( function () {
2784 win
.hold( data
).then( function () {
2785 closing
.notify( { state
: 'hold' } );
2786 setTimeout( function () {
2787 win
.teardown( data
).then( function () {
2788 closing
.notify( { state
: 'teardown' } );
2789 if ( manager
.modal
) {
2790 manager
.$( manager
.getElementDocument() ).off( {
2791 // Allow scrolling by keys in top-level window
2792 keydown
: manager
.onDocumentKeyDownHandler
2794 manager
.$( manager
.getElementWindow() ).off( {
2795 // Allow scrolling by wheel in top-level window
2796 mousewheel
: manager
.onWindowMouseWheelHandler
,
2797 // Stop listening for top-level window dimension changes
2798 'orientationchange resize': manager
.onWindowResizeHandler
2801 // Restore screen reader visiblity
2802 if ( manager
.$ariaHidden
) {
2803 manager
.$ariaHidden
.removeAttr( 'aria-hidden' );
2804 manager
.$ariaHidden
= null;
2806 manager
.closing
= null;
2807 manager
.currentWindow
= null;
2808 closing
.resolve( data
);
2810 }, manager
.getTeardownDelay() );
2812 }, manager
.getHoldDelay() );
2822 * If the window manager is attached to the DOM then windows will be automatically loaded as they
2825 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2826 * @return {jQuery.Promise} Promise resolved when all windows are added
2827 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2828 * a statically configured symbolic name
2830 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2831 var i
, len
, win
, name
, list
,
2834 if ( $.isArray( windows
) ) {
2835 // Convert to map of windows by looking up symbolic names from static configuration
2837 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2838 name
= windows
[i
].constructor.static.name
;
2839 if ( typeof name
!== 'string' ) {
2840 throw new Error( 'Cannot add window' );
2842 list
[name
] = windows
[i
];
2844 } else if ( $.isPlainObject( windows
) ) {
2849 for ( name
in list
) {
2851 this.windows
[name
] = win
;
2852 this.$element
.append( win
.$element
);
2854 if ( this.isElementAttached() ) {
2855 promises
.push( win
.load() );
2859 return $.when
.apply( $, promises
);
2865 * Windows will be closed before they are removed.
2867 * @param {string} name Symbolic name of window to remove
2868 * @return {jQuery.Promise} Promise resolved when window is closed and removed
2869 * @throws {Error} If windows being removed are not being managed
2871 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
2872 var i
, len
, win
, name
,
2875 cleanup = function ( name
, win
) {
2876 delete manager
.windows
[name
];
2877 win
.$element
.detach();
2880 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
2882 win
= this.windows
[name
];
2884 throw new Error( 'Cannot remove window' );
2886 promises
.push( this.closeWindow( name
).then( OO
.ui
.bind( cleanup
, null, name
, win
) ) );
2889 return $.when
.apply( $, promises
);
2893 * Remove all windows.
2895 * Windows will be closed before they are removed.
2897 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
2899 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
2900 return this.removeWindows( Object
.keys( this.windows
) );
2906 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
2910 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
2911 // Bypass for non-current, and thus invisible, windows
2912 if ( win
!== this.currentWindow
) {
2916 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
2917 sizes
= this.constructor.static.sizes
,
2918 size
= win
.getSize();
2920 if ( !sizes
[size
] ) {
2921 size
= this.constructor.static.defaultSize
;
2923 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
2927 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
2928 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
2929 win
.setDimensions( sizes
[size
] );
2939 * @param {string|jQuery} message Description of error
2940 * @param {Object} [config] Configuration options
2941 * @cfg {boolean} [recoverable=true] Error is recoverable
2943 OO
.ui
.Error
= function OoUiElement( message
, config
) {
2944 // Configuration initialization
2945 config
= config
|| {};
2948 this.message
= message
instanceof jQuery
? message
: String( message
);
2949 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
2954 OO
.initClass( OO
.ui
.Error
);
2959 * Check if error can be recovered from.
2961 * @return {boolean} Error is recoverable
2963 OO
.ui
.Error
.prototype.isRecoverable = function () {
2964 return this.recoverable
;
2968 * Get error message as DOM nodes.
2970 * @return {jQuery} Error message in DOM nodes
2972 OO
.ui
.Error
.prototype.getMessage = function () {
2973 return this.message
instanceof jQuery
?
2974 this.message
.clone() :
2975 $( '<div>' ).text( this.message
).contents();
2979 * Get error message as text.
2981 * @return {string} Error message
2983 OO
.ui
.Error
.prototype.getMessageText = function () {
2984 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
2988 * A list of functions, called in sequence.
2990 * If a function added to a process returns boolean false the process will stop; if it returns an
2991 * object with a `promise` method the process will use the promise to either continue to the next
2992 * step when the promise is resolved or stop when the promise is rejected.
2997 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
2998 * call, see #createStep for more information
2999 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3001 * @return {Object} Step object, with `callback` and `context` properties
3003 OO
.ui
.Process = function ( step
, context
) {
3008 if ( step
!== undefined ) {
3009 this.next( step
, context
);
3015 OO
.initClass( OO
.ui
.Process
);
3020 * Start the process.
3022 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3023 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3024 * process, the remaining steps will not be taken
3026 OO
.ui
.Process
.prototype.execute = function () {
3027 var i
, len
, promise
;
3030 * Continue execution.
3033 * @param {Array} step A function and the context it should be called in
3034 * @return {Function} Function that continues the process
3036 function proceed( step
) {
3037 return function () {
3038 // Execute step in the correct context
3040 result
= step
.callback
.call( step
.context
);
3042 if ( result
=== false ) {
3043 // Use rejected promise for boolean false results
3044 return $.Deferred().reject( [] ).promise();
3046 if ( typeof result
=== 'number' ) {
3048 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3050 // Use a delayed promise for numbers, expecting them to be in milliseconds
3051 deferred
= $.Deferred();
3052 setTimeout( deferred
.resolve
, result
);
3053 return deferred
.promise();
3055 if ( result
instanceof OO
.ui
.Error
) {
3056 // Use rejected promise for error
3057 return $.Deferred().reject( [ result
] ).promise();
3059 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3060 // Use rejected promise for list of errors
3061 return $.Deferred().reject( result
).promise();
3063 // Duck-type the object to see if it can produce a promise
3064 if ( result
&& $.isFunction( result
.promise
) ) {
3065 // Use a promise generated from the result
3066 return result
.promise();
3068 // Use resolved promise for other results
3069 return $.Deferred().resolve().promise();
3073 if ( this.steps
.length
) {
3074 // Generate a chain reaction of promises
3075 promise
= proceed( this.steps
[0] )();
3076 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3077 promise
= promise
.then( proceed( this.steps
[i
] ) );
3080 promise
= $.Deferred().resolve().promise();
3087 * Create a process step.
3090 * @param {number|jQuery.Promise|Function} step
3092 * - Number of milliseconds to wait; or
3093 * - Promise to wait to be resolved; or
3094 * - Function to execute
3095 * - If it returns boolean false the process will stop
3096 * - If it returns an object with a `promise` method the process will use the promise to either
3097 * continue to the next step when the promise is resolved or stop when the promise is rejected
3098 * - If it returns a number, the process will wait for that number of milliseconds before
3100 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3102 * @return {Object} Step object, with `callback` and `context` properties
3104 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3105 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3107 callback: function () {
3113 if ( $.isFunction( step
) ) {
3119 throw new Error( 'Cannot create process step: number, promise or function expected' );
3123 * Add step to the beginning of the process.
3125 * @inheritdoc #createStep
3126 * @return {OO.ui.Process} this
3129 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3130 this.steps
.unshift( this.createStep( step
, context
) );
3135 * Add step to the end of the process.
3137 * @inheritdoc #createStep
3138 * @return {OO.ui.Process} this
3141 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3142 this.steps
.push( this.createStep( step
, context
) );
3147 * Factory for tools.
3150 * @extends OO.Factory
3153 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3154 // Parent constructor
3155 OO
.ui
.ToolFactory
.super.call( this );
3160 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3165 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3166 var i
, len
, included
, promoted
, demoted
,
3170 // Collect included and not excluded tools
3171 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3174 promoted
= this.extract( promote
, used
);
3175 demoted
= this.extract( demote
, used
);
3178 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3179 if ( !used
[included
[i
]] ) {
3180 auto
.push( included
[i
] );
3184 return promoted
.concat( auto
).concat( demoted
);
3188 * Get a flat list of names from a list of names or groups.
3190 * Tools can be specified in the following ways:
3192 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3193 * - All tools in a group: `{ group: 'group-name' }`
3194 * - All tools: `'*'`
3197 * @param {Array|string} collection List of tools
3198 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3199 * names will be added as properties
3200 * @return {string[]} List of extracted names
3202 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3203 var i
, len
, item
, name
, tool
,
3206 if ( collection
=== '*' ) {
3207 for ( name
in this.registry
) {
3208 tool
= this.registry
[name
];
3210 // Only add tools by group name when auto-add is enabled
3211 tool
.static.autoAddToCatchall
&&
3212 // Exclude already used tools
3213 ( !used
|| !used
[name
] )
3221 } else if ( $.isArray( collection
) ) {
3222 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3223 item
= collection
[i
];
3224 // Allow plain strings as shorthand for named tools
3225 if ( typeof item
=== 'string' ) {
3226 item
= { name
: item
};
3228 if ( OO
.isPlainObject( item
) ) {
3230 for ( name
in this.registry
) {
3231 tool
= this.registry
[name
];
3233 // Include tools with matching group
3234 tool
.static.group
=== item
.group
&&
3235 // Only add tools by group name when auto-add is enabled
3236 tool
.static.autoAddToGroup
&&
3237 // Exclude already used tools
3238 ( !used
|| !used
[name
] )
3246 // Include tools with matching name and exclude already used tools
3247 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3248 names
.push( item
.name
);
3250 used
[item
.name
] = true;
3260 * Factory for tool groups.
3263 * @extends OO.Factory
3266 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3267 // Parent constructor
3268 OO
.Factory
.call( this );
3271 defaultClasses
= this.constructor.static.getDefaultClasses();
3273 // Register default toolgroups
3274 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3275 this.register( defaultClasses
[i
] );
3281 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3283 /* Static Methods */
3286 * Get a default set of classes to be registered on construction
3288 * @return {Function[]} Default classes
3290 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3293 OO
.ui
.ListToolGroup
,
3299 * Element with a button.
3301 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3302 * and access keys for accessibility purposes.
3308 * @param {jQuery} $button Button node, assigned to #$button
3309 * @param {Object} [config] Configuration options
3310 * @cfg {boolean} [framed=true] Render button with a frame
3311 * @cfg {number} [tabIndex=0] Button's tab index, use null to have no tabIndex
3312 * @cfg {string} [accessKey] Button's access key
3314 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
3315 // Configuration initialization
3316 config
= config
|| {};
3319 this.$button
= $button
;
3320 this.tabIndex
= null;
3322 this.active
= false;
3323 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
3326 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
3329 this.$element
.addClass( 'oo-ui-buttonedElement' );
3331 .addClass( 'oo-ui-buttonedElement-button' )
3332 .attr( 'role', 'button' );
3333 this.setTabIndex( config
.tabIndex
|| 0 );
3334 this.setAccessKey( config
.accessKey
);
3335 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3340 OO
.initClass( OO
.ui
.ButtonedElement
);
3342 /* Static Properties */
3345 * Cancel mouse down events.
3349 * @property {boolean}
3351 OO
.ui
.ButtonedElement
.static.cancelButtonMouseDownEvents
= true;
3356 * Handles mouse down events.
3358 * @param {jQuery.Event} e Mouse down event
3360 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function ( e
) {
3361 if ( this.isDisabled() || e
.which
!== 1 ) {
3364 // tabIndex should generally be interacted with via the property, but it's not possible to
3365 // reliably unset a tabIndex via a property so we use the (lowercase) "tabindex" attribute
3366 this.tabIndex
= this.$button
.attr( 'tabindex' );
3368 // Remove the tab-index while the button is down to prevent the button from stealing focus
3369 .removeAttr( 'tabindex' )
3370 .addClass( 'oo-ui-buttonedElement-pressed' );
3371 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3372 // reliably reapply the tabindex and remove the pressed class
3373 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3374 // Prevent change of focus unless specifically configured otherwise
3375 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3381 * Handles mouse up events.
3383 * @param {jQuery.Event} e Mouse up event
3385 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function ( e
) {
3386 if ( this.isDisabled() || e
.which
!== 1 ) {
3390 // Restore the tab-index after the button is up to restore the button's accesssibility
3391 .attr( 'tabindex', this.tabIndex
)
3392 .removeClass( 'oo-ui-buttonedElement-pressed' );
3393 // Stop listening for mouseup, since we only needed this once
3394 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3400 * @param {boolean} [framed] Make button framed, omit to toggle
3403 OO
.ui
.ButtonedElement
.prototype.toggleFramed = function ( framed
) {
3404 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3405 if ( framed
!== this.framed
) {
3406 this.framed
= framed
;
3408 .toggleClass( 'oo-ui-buttonedElement-frameless', !framed
)
3409 .toggleClass( 'oo-ui-buttonedElement-framed', framed
);
3418 * @param {number|null} tabIndex Button's tab index, use null to remove
3421 OO
.ui
.ButtonedElement
.prototype.setTabIndex = function ( tabIndex
) {
3422 if ( typeof tabIndex
=== 'number' && tabIndex
>= 0 ) {
3423 this.$button
.attr( 'tabindex', tabIndex
);
3425 this.$button
.removeAttr( 'tabindex' );
3433 * @param {string} accessKey Button's access key, use empty string to remove
3436 OO
.ui
.ButtonedElement
.prototype.setAccessKey = function ( accessKey
) {
3437 if ( typeof accessKey
=== 'string' && accessKey
.length
) {
3438 this.$button
.attr( 'accesskey', accessKey
);
3440 this.$button
.removeAttr( 'accesskey' );
3448 * @param {boolean} [value] Make button active
3451 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
3452 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
3457 * Element that can be automatically clipped to visible boundaies.
3463 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
3464 * @param {Object} [config] Configuration options
3466 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
3467 // Configuration initialization
3468 config
= config
|| {};
3471 this.$clippable
= $clippable
;
3472 this.clipping
= false;
3473 this.clipped
= false;
3474 this.$clippableContainer
= null;
3475 this.$clippableScroller
= null;
3476 this.$clippableWindow
= null;
3477 this.idealWidth
= null;
3478 this.idealHeight
= null;
3479 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
3480 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
3483 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
3491 * @param {boolean} value Enable clipping
3494 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
3497 if ( this.clipping
!== value
) {
3498 this.clipping
= value
;
3499 if ( this.clipping
) {
3500 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
3501 // If the clippable container is the body, we have to listen to scroll events and check
3502 // jQuery.scrollTop on the window because of browser inconsistencies
3503 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
3504 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
3505 this.$clippableContainer
;
3506 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
3507 this.$clippableWindow
= this.$( this.getElementWindow() )
3508 .on( 'resize', this.onClippableWindowResizeHandler
);
3509 // Initial clip after visible
3510 setTimeout( OO
.ui
.bind( this.clip
, this ) );
3512 this.$clippableContainer
= null;
3513 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
3514 this.$clippableScroller
= null;
3515 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
3516 this.$clippableWindow
= null;
3524 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
3526 * @return {boolean} Element will be clipped to the visible area
3528 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
3529 return this.clipping
;
3533 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
3535 * @return {boolean} Part of the element is being clipped
3537 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
3538 return this.clipped
;
3542 * Set the ideal size.
3544 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
3545 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
3547 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
3548 this.idealWidth
= width
;
3549 this.idealHeight
= height
;
3553 * Clip element to visible boundaries and allow scrolling when needed.
3555 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
3556 * overlapped by, the visible area of the nearest scrollable container.
3560 OO
.ui
.ClippableElement
.prototype.clip = function () {
3561 if ( !this.clipping
) {
3562 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
3567 cOffset
= this.$clippable
.offset(),
3568 $container
= this.$clippableContainer
.is( 'body' ) ? this.$clippableWindow
: this.$clippableContainer
,
3569 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
3570 ccHeight
= $container
.innerHeight() - buffer
,
3571 ccWidth
= $container
.innerWidth() - buffer
,
3572 scrollTop
= this.$clippableScroller
.scrollTop(),
3573 scrollLeft
= this.$clippableScroller
.scrollLeft(),
3574 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
3575 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
3576 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
3577 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
3578 clipWidth
= desiredWidth
< naturalWidth
,
3579 clipHeight
= desiredHeight
< naturalHeight
;
3582 this.$clippable
.css( { overflowX
: 'auto', width
: desiredWidth
} );
3584 this.$clippable
.css( 'width', this.idealWidth
|| '' );
3585 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
3586 this.$clippable
.css( 'overflowX', '' );
3589 this.$clippable
.css( { overflowY
: 'auto', height
: desiredHeight
} );
3591 this.$clippable
.css( 'height', this.idealHeight
|| '' );
3592 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
3593 this.$clippable
.css( 'overflowY', '' );
3596 this.clipped
= clipWidth
|| clipHeight
;
3602 * Element with named flags that can be added, removed, listed and checked.
3604 * A flag, when set, adds a CSS class on the `$element` by combing `oo-ui-flaggableElement-` with
3605 * the flag name. Flags are primarily useful for styling.
3611 * @param {Object} [config] Configuration options
3612 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
3614 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
3615 // Config initialization
3616 config
= config
|| {};
3622 this.setFlags( config
.flags
);
3629 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
3630 * added/removed properties
3636 * Check if a flag is set.
3638 * @param {string} flag Name of flag
3639 * @return {boolean} Has flag
3641 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
3642 return flag
in this.flags
;
3646 * Get the names of all flags set.
3648 * @return {string[]} flags Flag names
3650 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
3651 return Object
.keys( this.flags
);
3660 OO
.ui
.FlaggableElement
.prototype.clearFlags = function () {
3663 classPrefix
= 'oo-ui-flaggableElement-';
3665 for ( flag
in this.flags
) {
3666 changes
[flag
] = false;
3667 delete this.flags
[flag
];
3668 this.$element
.removeClass( classPrefix
+ flag
);
3671 this.emit( 'flag', changes
);
3677 * Add one or more flags.
3679 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
3680 * keyed by flag name containing boolean set/remove instructions.
3684 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
3687 classPrefix
= 'oo-ui-flaggableElement-';
3689 if ( typeof flags
=== 'string' ) {
3691 this.flags
[flags
] = true;
3692 this.$element
.addClass( classPrefix
+ flags
);
3693 } else if ( $.isArray( flags
) ) {
3694 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
3697 changes
[flag
] = true;
3698 this.flags
[flag
] = true;
3699 this.$element
.addClass( classPrefix
+ flag
);
3701 } else if ( OO
.isPlainObject( flags
) ) {
3702 for ( flag
in flags
) {
3703 if ( flags
[flag
] ) {
3705 changes
[flag
] = true;
3706 this.flags
[flag
] = true;
3707 this.$element
.addClass( classPrefix
+ flag
);
3710 changes
[flag
] = false;
3711 delete this.flags
[flag
];
3712 this.$element
.removeClass( classPrefix
+ flag
);
3717 this.emit( 'flag', changes
);
3723 * Element containing a sequence of child elements.
3729 * @param {jQuery} $group Container node, assigned to #$group
3730 * @param {Object} [config] Configuration options
3732 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
3734 config
= config
|| {};
3737 this.$group
= $group
;
3739 this.aggregateItemEvents
= {};
3747 * @return {OO.ui.Element[]} Items
3749 OO
.ui
.GroupElement
.prototype.getItems = function () {
3750 return this.items
.slice( 0 );
3754 * Add an aggregate item event.
3756 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3757 * and with an additional leading parameter containing the item that emitted the original event.
3758 * Other arguments that were emitted from the original event are passed through.
3760 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3761 * event, use null value to remove aggregation
3762 * @throws {Error} If aggregation already exists
3764 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3765 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3767 for ( itemEvent
in events
) {
3768 groupEvent
= events
[itemEvent
];
3770 // Remove existing aggregated event
3771 if ( itemEvent
in this.aggregateItemEvents
) {
3772 // Don't allow duplicate aggregations
3774 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3776 // Remove event aggregation from 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 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3782 item
.disconnect( this, remove
);
3785 // Prevent future items from aggregating event
3786 delete this.aggregateItemEvents
[itemEvent
];
3789 // Add new aggregate event
3791 // Make future items aggregate event
3792 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3793 // Add event aggregation to existing items
3794 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3795 item
= this.items
[i
];
3796 if ( item
.connect
&& item
.disconnect
) {
3798 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3799 item
.connect( this, add
);
3809 * @param {OO.ui.Element[]} items Item
3810 * @param {number} [index] Index to insert items at
3813 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3814 var i
, len
, item
, event
, events
, currentIndex
,
3817 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3820 // Check if item exists then remove it first, effectively "moving" it
3821 currentIndex
= $.inArray( item
, this.items
);
3822 if ( currentIndex
>= 0 ) {
3823 this.removeItems( [ item
] );
3824 // Adjust index to compensate for removal
3825 if ( currentIndex
< index
) {
3830 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3832 for ( event
in this.aggregateItemEvents
) {
3833 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3835 item
.connect( this, events
);
3837 item
.setElementGroup( this );
3838 itemElements
.push( item
.$element
.get( 0 ) );
3841 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3842 this.$group
.append( itemElements
);
3843 this.items
.push
.apply( this.items
, items
);
3844 } else if ( index
=== 0 ) {
3845 this.$group
.prepend( itemElements
);
3846 this.items
.unshift
.apply( this.items
, items
);
3848 this.items
[index
].$element
.before( itemElements
);
3849 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3858 * Items will be detached, not removed, so they can be used later.
3860 * @param {OO.ui.Element[]} items Items to remove
3863 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3864 var i
, len
, item
, index
, remove
, itemEvent
;
3866 // Remove specific items
3867 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3869 index
= $.inArray( item
, this.items
);
3870 if ( index
!== -1 ) {
3872 item
.connect
&& item
.disconnect
&&
3873 !$.isEmptyObject( this.aggregateItemEvents
)
3876 if ( itemEvent
in this.aggregateItemEvents
) {
3877 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3879 item
.disconnect( this, remove
);
3881 item
.setElementGroup( null );
3882 this.items
.splice( index
, 1 );
3883 item
.$element
.detach();
3893 * Items will be detached, not removed, so they can be used later.
3897 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3898 var i
, len
, item
, remove
, itemEvent
;
3901 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3902 item
= this.items
[i
];
3904 item
.connect
&& item
.disconnect
&&
3905 !$.isEmptyObject( this.aggregateItemEvents
)
3908 if ( itemEvent
in this.aggregateItemEvents
) {
3909 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3911 item
.disconnect( this, remove
);
3913 item
.setElementGroup( null );
3914 item
.$element
.detach();
3922 * Element containing an icon.
3924 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
3925 * a control or convey information in a more space efficient way. Icons should rarely be used
3926 * without labels; such as in a toolbar where space is at a premium or within a context where the
3927 * meaning is very clear to the user.
3933 * @param {jQuery} $icon Icon node, assigned to #$icon
3934 * @param {Object} [config] Configuration options
3935 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
3936 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3939 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
3940 // Config intialization
3941 config
= config
|| {};
3948 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
3949 this.setIcon( config
.icon
|| this.constructor.static.icon
);
3954 OO
.initClass( OO
.ui
.IconedElement
);
3956 /* Static Properties */
3961 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
3963 * For i18n purposes, this property can be an object containing a `default` icon name property and
3964 * additional icon names keyed by language code.
3966 * Example of i18n icon definition:
3967 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
3971 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
3972 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3975 OO
.ui
.IconedElement
.static.icon
= null;
3982 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
3983 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3987 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
3988 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
3991 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
3993 if ( typeof icon
=== 'string' ) {
3995 if ( icon
.length
) {
3996 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4000 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
4008 * @return {string} Icon
4010 OO
.ui
.IconedElement
.prototype.getIcon = function () {
4015 * Element containing an indicator.
4017 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4018 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4019 * instead of performing an action directly, or an item in a list which has errors that need to be
4026 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
4027 * @param {Object} [config] Configuration options
4028 * @cfg {string} [indicator] Symbolic indicator name
4029 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
4031 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
4032 // Config intialization
4033 config
= config
|| {};
4036 this.$indicator
= $indicator
;
4037 this.indicator
= null;
4038 this.indicatorLabel
= null;
4041 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
4042 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4043 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4048 OO
.initClass( OO
.ui
.IndicatedElement
);
4050 /* Static Properties */
4057 * @property {string|null} Symbolic indicator name or null for no indicator
4059 OO
.ui
.IndicatedElement
.static.indicator
= null;
4066 * @property {string|Function|null} Indicator title text, a function that return text or null for no
4069 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
4076 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4079 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
4080 if ( this.indicator
) {
4081 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4082 this.indicator
= null;
4084 if ( typeof indicator
=== 'string' ) {
4085 indicator
= indicator
.trim();
4086 if ( indicator
.length
) {
4087 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4088 this.indicator
= indicator
;
4091 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
4097 * Set indicator label.
4099 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
4100 * for no indicator title
4103 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4104 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
4106 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
4107 this.$indicator
.attr( 'title', indicatorTitle
);
4109 this.$indicator
.removeAttr( 'title' );
4118 * @return {string} title Symbolic name of indicator
4120 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
4121 return this.indicator
;
4125 * Get indicator title.
4127 * @return {string} Indicator title text
4129 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
4130 return this.indicatorTitle
;
4134 * Element containing a label.
4140 * @param {jQuery} $label Label node, assigned to #$label
4141 * @param {Object} [config] Configuration options
4142 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4143 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4145 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
4146 // Config intialization
4147 config
= config
|| {};
4150 this.$label
= $label
;
4154 this.$label
.addClass( 'oo-ui-labeledElement-label' );
4155 this.setLabel( config
.label
|| this.constructor.static.label
);
4156 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4161 OO
.initClass( OO
.ui
.LabeledElement
);
4163 /* Static Properties */
4170 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
4173 OO
.ui
.LabeledElement
.static.label
= null;
4180 * An empty string will result in the label being hidden. A string containing only whitespace will
4181 * be converted to a single
4183 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
4184 * text; or null for no label
4187 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
4190 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
4191 if ( typeof label
=== 'string' && label
.length
) {
4192 if ( label
.match( /^\s*$/ ) ) {
4193 // Convert whitespace only string to a single non-breaking space
4194 this.$label
.html( ' ' );
4196 this.$label
.text( label
);
4198 } else if ( label
instanceof jQuery
) {
4199 this.$label
.empty().append( label
);
4201 this.$label
.empty();
4204 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
4205 this.$label
.css( 'display', empty
? 'none' : '' );
4213 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4214 * text; or null for no label
4216 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
4225 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
4226 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4227 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4233 * Element containing an OO.ui.PopupWidget object.
4239 * @param {Object} [config] Configuration options
4240 * @cfg {Object} [popup] Configuration to pass to popup
4241 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4243 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
4244 // Configuration initialization
4245 config
= config
|| {};
4248 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4249 { autoClose
: true },
4251 { $: this.$, $autoCloseIgnore
: this.$element
}
4260 * @return {OO.ui.PopupWidget} Popup widget
4262 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
4267 * Element with a title.
4269 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4270 * not visible on touch devices.
4276 * @param {jQuery} $label Titled node, assigned to #$titled
4277 * @param {Object} [config] Configuration options
4278 * @cfg {string|Function} [title] Title text or a function that returns text
4280 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
4281 // Config intialization
4282 config
= config
|| {};
4285 this.$titled
= $titled
;
4289 this.setTitle( config
.title
|| this.constructor.static.title
);
4294 OO
.initClass( OO
.ui
.TitledElement
);
4296 /* Static Properties */
4303 * @property {string|Function} Title text or a function that returns text
4305 OO
.ui
.TitledElement
.static.title
= null;
4312 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4315 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4316 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
4318 if ( typeof title
=== 'string' && title
.length
) {
4319 this.$titled
.attr( 'title', title
);
4321 this.$titled
.removeAttr( 'title' );
4330 * @return {string} Title string
4332 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4337 * Generic toolbar tool.
4341 * @extends OO.ui.Widget
4342 * @mixins OO.ui.IconedElement
4345 * @param {OO.ui.ToolGroup} toolGroup
4346 * @param {Object} [config] Configuration options
4347 * @cfg {string|Function} [title] Title text or a function that returns text
4349 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4350 // Config intialization
4351 config
= config
|| {};
4353 // Parent constructor
4354 OO
.ui
.Tool
.super.call( this, config
);
4356 // Mixin constructors
4357 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4360 this.toolGroup
= toolGroup
;
4361 this.toolbar
= this.toolGroup
.getToolbar();
4362 this.active
= false;
4363 this.$title
= this.$( '<span>' );
4364 this.$link
= this.$( '<a>' );
4368 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
4371 this.$title
.addClass( 'oo-ui-tool-title' );
4373 .addClass( 'oo-ui-tool-link' )
4374 .append( this.$icon
, this.$title
)
4375 .prop( 'tabIndex', 0 )
4376 .attr( 'role', 'button' );
4378 .data( 'oo-ui-tool', this )
4380 'oo-ui-tool ' + 'oo-ui-tool-name-' +
4381 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
4383 .append( this.$link
);
4384 this.setTitle( config
.title
|| this.constructor.static.title
);
4389 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
4390 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
4398 /* Static Properties */
4404 OO
.ui
.Tool
.static.tagName
= 'span';
4407 * Symbolic name of tool.
4412 * @property {string}
4414 OO
.ui
.Tool
.static.name
= '';
4422 * @property {string}
4424 OO
.ui
.Tool
.static.group
= '';
4429 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
4430 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
4431 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
4432 * appended to the title if the tool is part of a bar tool group.
4437 * @property {string|Function} Title text or a function that returns text
4439 OO
.ui
.Tool
.static.title
= '';
4442 * Tool can be automatically added to catch-all groups.
4446 * @property {boolean}
4448 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
4451 * Tool can be automatically added to named groups.
4454 * @property {boolean}
4457 OO
.ui
.Tool
.static.autoAddToGroup
= true;
4460 * Check if this tool is compatible with given data.
4464 * @param {Mixed} data Data to check
4465 * @return {boolean} Tool can be used with data
4467 OO
.ui
.Tool
.static.isCompatibleWith = function () {
4474 * Handle the toolbar state being updated.
4476 * This is an abstract method that must be overridden in a concrete subclass.
4480 OO
.ui
.Tool
.prototype.onUpdateState = function () {
4482 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
4487 * Handle the tool being selected.
4489 * This is an abstract method that must be overridden in a concrete subclass.
4493 OO
.ui
.Tool
.prototype.onSelect = function () {
4495 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
4500 * Check if the button is active.
4502 * @param {boolean} Button is active
4504 OO
.ui
.Tool
.prototype.isActive = function () {
4509 * Make the button appear active or inactive.
4511 * @param {boolean} state Make button appear active
4513 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
4514 this.active
= !!state
;
4515 if ( this.active
) {
4516 this.$element
.addClass( 'oo-ui-tool-active' );
4518 this.$element
.removeClass( 'oo-ui-tool-active' );
4523 * Get the tool title.
4525 * @param {string|Function} title Title text or a function that returns text
4528 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
4529 this.title
= OO
.ui
.resolveMsg( title
);
4535 * Get the tool title.
4537 * @return {string} Title text
4539 OO
.ui
.Tool
.prototype.getTitle = function () {
4544 * Get the tool's symbolic name.
4546 * @return {string} Symbolic name of tool
4548 OO
.ui
.Tool
.prototype.getName = function () {
4549 return this.constructor.static.name
;
4555 OO
.ui
.Tool
.prototype.updateTitle = function () {
4556 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
4557 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
4558 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
4565 .addClass( 'oo-ui-tool-accel' )
4569 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
4570 tooltipParts
.push( this.title
);
4572 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
4573 tooltipParts
.push( accel
);
4575 if ( tooltipParts
.length
) {
4576 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
4578 this.$link
.removeAttr( 'title' );
4585 OO
.ui
.Tool
.prototype.destroy = function () {
4586 this.toolbar
.disconnect( this );
4587 this.$element
.remove();
4591 * Collection of tool groups.
4594 * @extends OO.ui.Element
4595 * @mixins OO.EventEmitter
4596 * @mixins OO.ui.GroupElement
4599 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
4600 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
4601 * @param {Object} [config] Configuration options
4602 * @cfg {boolean} [actions] Add an actions section opposite to the tools
4603 * @cfg {boolean} [shadow] Add a shadow below the toolbar
4605 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
4606 // Configuration initialization
4607 config
= config
|| {};
4609 // Parent constructor
4610 OO
.ui
.Toolbar
.super.call( this, config
);
4612 // Mixin constructors
4613 OO
.EventEmitter
.call( this );
4614 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4617 this.toolFactory
= toolFactory
;
4618 this.toolGroupFactory
= toolGroupFactory
;
4621 this.$bar
= this.$( '<div>' );
4622 this.$actions
= this.$( '<div>' );
4623 this.initialized
= false;
4627 .add( this.$bar
).add( this.$group
).add( this.$actions
)
4628 .on( 'mousedown touchstart', OO
.ui
.bind( this.onPointerDown
, this ) );
4631 this.$group
.addClass( 'oo-ui-toolbar-tools' );
4632 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
4633 if ( config
.actions
) {
4634 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
4635 this.$bar
.append( this.$actions
);
4637 this.$bar
.append( '<div style="clear:both"></div>' );
4638 if ( config
.shadow
) {
4639 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
4641 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
4646 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
4647 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
4648 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
4653 * Get the tool factory.
4655 * @return {OO.ui.ToolFactory} Tool factory
4657 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
4658 return this.toolFactory
;
4662 * Get the tool group factory.
4664 * @return {OO.Factory} Tool group factory
4666 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
4667 return this.toolGroupFactory
;
4671 * Handles mouse down events.
4673 * @param {jQuery.Event} e Mouse down event
4675 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
4676 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
4677 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
4678 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
4684 * Sets up handles and preloads required information for the toolbar to work.
4685 * This must be called immediately after it is attached to a visible document.
4687 OO
.ui
.Toolbar
.prototype.initialize = function () {
4688 this.initialized
= true;
4694 * Tools can be specified in the following ways:
4696 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
4697 * - All tools in a group: `{ group: 'group-name' }`
4698 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
4700 * @param {Object.<string,Array>} groups List of tool group configurations
4701 * @param {Array|string} [groups.include] Tools to include
4702 * @param {Array|string} [groups.exclude] Tools to exclude
4703 * @param {Array|string} [groups.promote] Tools to promote to the beginning
4704 * @param {Array|string} [groups.demote] Tools to demote to the end
4706 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
4707 var i
, len
, type
, group
,
4709 defaultType
= 'bar';
4711 // Cleanup previous groups
4714 // Build out new groups
4715 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
4717 if ( group
.include
=== '*' ) {
4718 // Apply defaults to catch-all groups
4719 if ( group
.type
=== undefined ) {
4720 group
.type
= 'list';
4722 if ( group
.label
=== undefined ) {
4723 group
.label
= 'ooui-toolbar-more';
4726 // Check type has been registered
4727 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
4729 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
4732 this.addItems( items
);
4736 * Remove all tools and groups from the toolbar.
4738 OO
.ui
.Toolbar
.prototype.reset = function () {
4743 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4744 this.items
[i
].destroy();
4750 * Destroys toolbar, removing event handlers and DOM elements.
4752 * Call this whenever you are done using a toolbar.
4754 OO
.ui
.Toolbar
.prototype.destroy = function () {
4756 this.$element
.remove();
4760 * Check if tool has not been used yet.
4762 * @param {string} name Symbolic name of tool
4763 * @return {boolean} Tool is available
4765 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
4766 return !this.tools
[name
];
4770 * Prevent tool from being used again.
4772 * @param {OO.ui.Tool} tool Tool to reserve
4774 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
4775 this.tools
[tool
.getName()] = tool
;
4779 * Allow tool to be used again.
4781 * @param {OO.ui.Tool} tool Tool to release
4783 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
4784 delete this.tools
[tool
.getName()];
4788 * Get accelerator label for tool.
4790 * This is a stub that should be overridden to provide access to accelerator information.
4792 * @param {string} name Symbolic name of tool
4793 * @return {string|undefined} Tool accelerator label if available
4795 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
4800 * Collection of tools.
4802 * Tools can be specified in the following ways:
4804 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
4805 * - All tools in a group: `{ group: 'group-name' }`
4806 * - All tools: `'*'`
4810 * @extends OO.ui.Widget
4811 * @mixins OO.ui.GroupElement
4814 * @param {OO.ui.Toolbar} toolbar
4815 * @param {Object} [config] Configuration options
4816 * @cfg {Array|string} [include=[]] List of tools to include
4817 * @cfg {Array|string} [exclude=[]] List of tools to exclude
4818 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
4819 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
4821 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
4822 // Configuration initialization
4823 config
= config
|| {};
4825 // Parent constructor
4826 OO
.ui
.ToolGroup
.super.call( this, config
);
4828 // Mixin constructors
4829 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4832 this.toolbar
= toolbar
;
4834 this.pressed
= null;
4835 this.autoDisabled
= false;
4836 this.include
= config
.include
|| [];
4837 this.exclude
= config
.exclude
|| [];
4838 this.promote
= config
.promote
|| [];
4839 this.demote
= config
.demote
|| [];
4840 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
4844 'mousedown touchstart': OO
.ui
.bind( this.onPointerDown
, this ),
4845 'mouseup touchend': OO
.ui
.bind( this.onPointerUp
, this ),
4846 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
4847 mouseout
: OO
.ui
.bind( this.onMouseOut
, this )
4849 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
4850 this.aggregate( { disable
: 'itemDisable' } );
4851 this.connect( this, { itemDisable
: 'updateDisabled' } );
4854 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
4856 .addClass( 'oo-ui-toolGroup' )
4857 .append( this.$group
);
4863 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
4864 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
4872 /* Static Properties */
4875 * Show labels in tooltips.
4879 * @property {boolean}
4881 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
4884 * Show acceleration labels in tooltips.
4888 * @property {boolean}
4890 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
4893 * Automatically disable the toolgroup when all tools are disabled
4897 * @property {boolean}
4899 OO
.ui
.ToolGroup
.static.autoDisable
= true;
4906 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
4907 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
4913 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
4914 var i
, item
, allDisabled
= true;
4916 if ( this.constructor.static.autoDisable
) {
4917 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
4918 item
= this.items
[i
];
4919 if ( !item
.isDisabled() ) {
4920 allDisabled
= false;
4924 this.autoDisabled
= allDisabled
;
4926 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
4930 * Handle mouse down events.
4932 * @param {jQuery.Event} e Mouse down event
4934 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
4935 // e.which is 0 for touch events, 1 for left mouse button
4936 if ( !this.isDisabled() && e
.which
<= 1 ) {
4937 this.pressed
= this.getTargetTool( e
);
4938 if ( this.pressed
) {
4939 this.pressed
.setActive( true );
4940 this.getElementDocument().addEventListener(
4941 'mouseup', this.onCapturedMouseUpHandler
, true
4949 * Handle captured mouse up events.
4951 * @param {Event} e Mouse up event
4953 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
4954 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
4955 // onPointerUp may be called a second time, depending on where the mouse is when the button is
4956 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
4957 this.onPointerUp( e
);
4961 * Handle mouse up events.
4963 * @param {jQuery.Event} e Mouse up event
4965 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
4966 var tool
= this.getTargetTool( e
);
4968 // e.which is 0 for touch events, 1 for left mouse button
4969 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
4970 this.pressed
.onSelect();
4973 this.pressed
= null;
4978 * Handle mouse over events.
4980 * @param {jQuery.Event} e Mouse over event
4982 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
4983 var tool
= this.getTargetTool( e
);
4985 if ( this.pressed
&& this.pressed
=== tool
) {
4986 this.pressed
.setActive( true );
4991 * Handle mouse out events.
4993 * @param {jQuery.Event} e Mouse out event
4995 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
4996 var tool
= this.getTargetTool( e
);
4998 if ( this.pressed
&& this.pressed
=== tool
) {
4999 this.pressed
.setActive( false );
5004 * Get the closest tool to a jQuery.Event.
5006 * Only tool links are considered, which prevents other elements in the tool such as popups from
5007 * triggering tool group interactions.
5010 * @param {jQuery.Event} e
5011 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5013 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5015 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5017 if ( $item
.length
) {
5018 tool
= $item
.parent().data( 'oo-ui-tool' );
5021 return tool
&& !tool
.isDisabled() ? tool
: null;
5025 * Handle tool registry register events.
5027 * If a tool is registered after the group is created, we must repopulate the list to account for:
5029 * - a tool being added that may be included
5030 * - a tool already included being overridden
5032 * @param {string} name Symbolic name of tool
5034 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5039 * Get the toolbar this group is in.
5041 * @return {OO.ui.Toolbar} Toolbar of group
5043 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5044 return this.toolbar
;
5048 * Add and remove tools based on configuration.
5050 OO
.ui
.ToolGroup
.prototype.populate = function () {
5051 var i
, len
, name
, tool
,
5052 toolFactory
= this.toolbar
.getToolFactory(),
5056 list
= this.toolbar
.getToolFactory().getTools(
5057 this.include
, this.exclude
, this.promote
, this.demote
5060 // Build a list of needed tools
5061 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5065 toolFactory
.lookup( name
) &&
5066 // Tool is available or is already in this group
5067 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5069 tool
= this.tools
[name
];
5071 // Auto-initialize tools on first use
5072 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5075 this.toolbar
.reserveTool( tool
);
5080 // Remove tools that are no longer needed
5081 for ( name
in this.tools
) {
5082 if ( !names
[name
] ) {
5083 this.tools
[name
].destroy();
5084 this.toolbar
.releaseTool( this.tools
[name
] );
5085 remove
.push( this.tools
[name
] );
5086 delete this.tools
[name
];
5089 if ( remove
.length
) {
5090 this.removeItems( remove
);
5092 // Update emptiness state
5094 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5096 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5098 // Re-add tools (moving existing ones to new locations)
5099 this.addItems( add
);
5100 // Disabled state may depend on items
5101 this.updateDisabled();
5105 * Destroy tool group.
5107 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5111 this.toolbar
.getToolFactory().disconnect( this );
5112 for ( name
in this.tools
) {
5113 this.toolbar
.releaseTool( this.tools
[name
] );
5114 this.tools
[name
].disconnect( this ).destroy();
5115 delete this.tools
[name
];
5117 this.$element
.remove();
5121 * Dialog for showing a message.
5124 * - Registers two actions by default (safe and primary).
5125 * - Renders action widgets in the footer.
5128 * @extends OO.ui.Dialog
5131 * @param {Object} [config] Configuration options
5133 OO
.ui
.MessageDialog
= function OoUiMessageDialog( manager
, config
) {
5134 // Parent constructor
5135 OO
.ui
.MessageDialog
.super.call( this, manager
, config
);
5138 this.verticalActionLayout
= null;
5141 this.$element
.addClass( 'oo-ui-messageDialog' );
5146 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5148 /* Static Properties */
5150 OO
.ui
.MessageDialog
.static.name
= 'message';
5152 OO
.ui
.MessageDialog
.static.size
= 'small';
5154 OO
.ui
.MessageDialog
.static.verbose
= false;
5159 * A confirmation dialog's title should describe what the progressive action will do. An alert
5160 * dialog's title should describe what event occured.
5164 * @property {jQuery|string|Function|null}
5166 OO
.ui
.MessageDialog
.static.title
= null;
5169 * A confirmation dialog's message should describe the consequences of the progressive action. An
5170 * alert dialog's message should describe why the event occured.
5174 * @property {jQuery|string|Function|null}
5176 OO
.ui
.MessageDialog
.static.message
= null;
5178 OO
.ui
.MessageDialog
.static.actions
= [
5179 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5180 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5188 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5190 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5194 * Toggle action layout between vertical and horizontal.
5196 * @param {boolean} [value] Layout actions vertically, omit to toggle
5199 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5200 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5202 if ( value
!== this.verticalActionLayout
) {
5203 this.verticalActionLayout
= value
;
5205 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5206 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5215 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5217 return new OO
.ui
.Process( function () {
5218 this.close( { action
: action
} );
5221 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5227 * @param {Object} [data] Dialog opening data
5228 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5229 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5230 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5231 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5234 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5238 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5239 .next( function () {
5240 this.title
.setLabel(
5241 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5243 this.message
.setLabel(
5244 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5246 this.message
.$element
.toggleClass(
5247 'oo-ui-messageDialog-message-verbose',
5248 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5256 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5257 return Math
.round( this.text
.$element
.outerHeight( true ) );
5263 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5265 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5268 this.$actions
= this.$( '<div>' );
5269 this.container
= new OO
.ui
.PanelLayout( {
5270 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5272 this.text
= new OO
.ui
.PanelLayout( {
5273 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5275 this.message
= new OO
.ui
.LabelWidget( {
5276 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5280 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5281 this.frame
.$content
.addClass( 'oo-ui-messageDialog-content' );
5282 this.container
.$element
.append( this.text
.$element
);
5283 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5284 this.$body
.append( this.container
.$element
);
5285 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5286 this.$foot
.append( this.$actions
);
5292 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5293 var i
, len
, other
, special
, others
;
5296 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5298 special
= this.actions
.getSpecial();
5299 others
= this.actions
.getOthers();
5300 if ( special
.safe
) {
5301 this.$actions
.append( special
.safe
.$element
);
5302 special
.safe
.toggleFramed( false );
5304 if ( others
.length
) {
5305 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5307 this.$actions
.append( other
.$element
);
5308 other
.toggleFramed( false );
5311 if ( special
.primary
) {
5312 this.$actions
.append( special
.primary
.$element
);
5313 special
.primary
.toggleFramed( false );
5317 if ( !this.isOpening() ) {
5318 this.manager
.updateWindowSize( this );
5320 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5324 * Fit action actions into columns or rows.
5326 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5328 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5330 actions
= this.actions
.get();
5333 this.toggleVerticalActionLayout( false );
5334 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5335 action
= actions
[i
];
5336 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5337 this.toggleVerticalActionLayout( true );
5344 * Navigation dialog window.
5347 * - Show and hide errors.
5348 * - Retry an action.
5351 * - Renders header with dialog title and one action widget on either side
5352 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5353 * which close the dialog).
5354 * - Displays any action widgets in the footer (none by default).
5355 * - Ability to dismiss errors.
5357 * Subclass responsibilities:
5358 * - Register a 'safe' action.
5359 * - Register a 'primary' action.
5360 * - Add content to the dialog.
5364 * @extends OO.ui.Dialog
5367 * @param {Object} [config] Configuration options
5369 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( manager
, config
) {
5370 // Parent constructor
5371 OO
.ui
.ProcessDialog
.super.call( this, manager
, config
);
5374 this.$element
.addClass( 'oo-ui-processDialog' );
5379 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
5384 * Handle dismiss button click events.
5388 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
5393 * Handle retry button click events.
5395 * Hides errors and then tries again.
5397 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
5399 this.executeAction( this.currentAction
.getAction() );
5405 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
5406 if ( this.actions
.isSpecial( action
) ) {
5409 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5415 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
5417 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
5420 this.$navigation
= this.$( '<div>' );
5421 this.$location
= this.$( '<div>' );
5422 this.$safeActions
= this.$( '<div>' );
5423 this.$primaryActions
= this.$( '<div>' );
5424 this.$otherActions
= this.$( '<div>' );
5425 this.dismissButton
= new OO
.ui
.ButtonWidget( {
5427 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
5429 this.retryButton
= new OO
.ui
.ButtonWidget( {
5431 label
: OO
.ui
.msg( 'ooui-dialog-process-retry' )
5433 this.$errors
= this.$( '<div>' );
5434 this.$errorsTitle
= this.$( '<div>' );
5437 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
5438 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
5441 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
5443 .append( this.title
.$element
)
5444 .addClass( 'oo-ui-processDialog-location' );
5445 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
5446 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
5447 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
5449 .addClass( 'oo-ui-processDialog-errors-title' )
5450 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
5452 .addClass( 'oo-ui-processDialog-errors' )
5453 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
5455 .addClass( 'oo-ui-processDialog-content' )
5456 .append( this.$errors
);
5458 .addClass( 'oo-ui-processDialog-navigation' )
5459 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
5460 this.$head
.append( this.$navigation
);
5461 this.$foot
.append( this.$otherActions
);
5467 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
5468 var i
, len
, other
, special
, others
;
5471 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
5473 special
= this.actions
.getSpecial();
5474 others
= this.actions
.getOthers();
5475 if ( special
.primary
) {
5476 this.$primaryActions
.append( special
.primary
.$element
);
5477 special
.primary
.toggleFramed( true );
5479 if ( others
.length
) {
5480 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5482 this.$otherActions
.append( other
.$element
);
5483 other
.toggleFramed( true );
5486 if ( special
.safe
) {
5487 this.$safeActions
.append( special
.safe
.$element
);
5488 special
.safe
.toggleFramed( true );
5492 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5498 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
5499 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
5500 .fail( OO
.ui
.bind( this.showErrors
, this ) );
5504 * Fit label between actions.
5508 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
5509 var width
= Math
.max(
5510 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
5511 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
5513 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
5519 * Handle errors that occured durring accept or reject processes.
5521 * @param {OO.ui.Error[]} errors Errors to be handled
5523 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
5528 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
5529 if ( !errors
[i
].isRecoverable() ) {
5530 recoverable
= false;
5532 $item
= this.$( '<div>' )
5533 .addClass( 'oo-ui-processDialog-error' )
5534 .append( errors
[i
].getMessage() );
5535 items
.push( $item
[0] );
5537 this.$errorItems
= this.$( items
);
5538 if ( recoverable
) {
5539 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
5541 this.currentAction
.setDisabled( true );
5543 this.retryButton
.toggle( recoverable
);
5544 this.$errorsTitle
.after( this.$errorItems
);
5545 this.$errors
.show().scrollTop( 0 );
5551 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
5552 this.$errors
.hide();
5553 this.$errorItems
.remove();
5554 this.$errorItems
= null;
5558 * Layout containing a series of pages.
5561 * @extends OO.ui.Layout
5564 * @param {Object} [config] Configuration options
5565 * @cfg {boolean} [continuous=false] Show all pages, one after another
5566 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
5567 * @cfg {boolean} [outlined=false] Show an outline
5568 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
5570 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
5571 // Initialize configuration
5572 config
= config
|| {};
5574 // Parent constructor
5575 OO
.ui
.BookletLayout
.super.call( this, config
);
5578 this.currentPageName
= null;
5580 this.ignoreFocus
= false;
5581 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
5582 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
5583 this.outlineVisible
= false;
5584 this.outlined
= !!config
.outlined
;
5585 if ( this.outlined
) {
5586 this.editable
= !!config
.editable
;
5587 this.outlineControlsWidget
= null;
5588 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
5589 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
5590 this.gridLayout
= new OO
.ui
.GridLayout(
5591 [ this.outlinePanel
, this.stackLayout
],
5592 { $: this.$, widths
: [ 1, 2 ] }
5594 this.outlineVisible
= true;
5595 if ( this.editable
) {
5596 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
5597 this.outlineWidget
, { $: this.$ }
5603 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
5604 if ( this.outlined
) {
5605 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
5607 if ( this.autoFocus
) {
5608 // Event 'focus' does not bubble, but 'focusin' does
5609 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
5613 this.$element
.addClass( 'oo-ui-bookletLayout' );
5614 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
5615 if ( this.outlined
) {
5616 this.outlinePanel
.$element
5617 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
5618 .append( this.outlineWidget
.$element
);
5619 if ( this.editable
) {
5620 this.outlinePanel
.$element
5621 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
5622 .append( this.outlineControlsWidget
.$element
);
5624 this.$element
.append( this.gridLayout
.$element
);
5626 this.$element
.append( this.stackLayout
.$element
);
5632 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
5638 * @param {OO.ui.PageLayout} page Current page
5643 * @param {OO.ui.PageLayout[]} page Added pages
5644 * @param {number} index Index pages were added at
5649 * @param {OO.ui.PageLayout[]} pages Removed pages
5655 * Handle stack layout focus.
5657 * @param {jQuery.Event} e Focusin event
5659 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
5662 // Find the page that an element was focused within
5663 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
5664 for ( name
in this.pages
) {
5665 // Check for page match, exclude current page to find only page changes
5666 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
5667 this.setPage( name
);
5674 * Handle stack layout set events.
5676 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
5678 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
5679 var $input
, layout
= this;
5681 page
.scrollElementIntoView( { complete: function () {
5682 if ( layout
.autoFocus
) {
5683 // Set focus to the first input if nothing on the page is focused yet
5684 if ( !page
.$element
.find( ':focus' ).length
) {
5685 $input
= page
.$element
.find( ':input:first' );
5686 if ( $input
.length
) {
5696 * Handle outline widget select events.
5698 * @param {OO.ui.OptionWidget|null} item Selected item
5700 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
5702 this.setPage( item
.getData() );
5707 * Check if booklet has an outline.
5711 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
5712 return this.outlined
;
5716 * Check if booklet has editing controls.
5720 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
5721 return this.editable
;
5725 * Check if booklet has a visible outline.
5729 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
5730 return this.outlined
&& this.outlineVisible
;
5734 * Hide or show the outline.
5736 * @param {boolean} [show] Show outline, omit to invert current state
5739 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
5740 if ( this.outlined
) {
5741 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
5742 this.outlineVisible
= show
;
5743 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
5750 * Get the outline widget.
5752 * @param {OO.ui.PageLayout} page Page to be selected
5753 * @return {OO.ui.PageLayout|null} Closest page to another
5755 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
5756 var next
, prev
, level
,
5757 pages
= this.stackLayout
.getItems(),
5758 index
= $.inArray( page
, pages
);
5760 if ( index
!== -1 ) {
5761 next
= pages
[index
+ 1];
5762 prev
= pages
[index
- 1];
5763 // Prefer adjacent pages at the same level
5764 if ( this.outlined
) {
5765 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
5768 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
5774 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
5780 return prev
|| next
|| null;
5784 * Get the outline widget.
5786 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
5788 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
5789 return this.outlineWidget
;
5793 * Get the outline controls widget. If the outline is not editable, null is returned.
5795 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
5797 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
5798 return this.outlineControlsWidget
;
5802 * Get a page by name.
5804 * @param {string} name Symbolic name of page
5805 * @return {OO.ui.PageLayout|undefined} Page, if found
5807 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
5808 return this.pages
[name
];
5812 * Get the current page name.
5814 * @return {string|null} Current page name
5816 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
5817 return this.currentPageName
;
5821 * Add a page to the layout.
5823 * When pages are added with the same names as existing pages, the existing pages will be
5824 * automatically removed before the new pages are added.
5826 * @param {OO.ui.PageLayout[]} pages Pages to add
5827 * @param {number} index Index to insert pages after
5831 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
5832 var i
, len
, name
, page
, item
, currentIndex
,
5833 stackLayoutPages
= this.stackLayout
.getItems(),
5837 // Remove pages with same names
5838 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5840 name
= page
.getName();
5842 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
5843 // Correct the insertion index
5844 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
5845 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
5848 remove
.push( this.pages
[name
] );
5851 if ( remove
.length
) {
5852 this.removePages( remove
);
5856 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5858 name
= page
.getName();
5859 this.pages
[page
.getName()] = page
;
5860 if ( this.outlined
) {
5861 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
5862 page
.setOutlineItem( item
);
5867 if ( this.outlined
&& items
.length
) {
5868 this.outlineWidget
.addItems( items
, index
);
5869 this.updateOutlineWidget();
5871 this.stackLayout
.addItems( pages
, index
);
5872 this.emit( 'add', pages
, index
);
5878 * Remove a page from the layout.
5883 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
5884 var i
, len
, name
, page
,
5887 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5889 name
= page
.getName();
5890 delete this.pages
[name
];
5891 if ( this.outlined
) {
5892 items
.push( this.outlineWidget
.getItemFromData( name
) );
5893 page
.setOutlineItem( null );
5896 if ( this.outlined
&& items
.length
) {
5897 this.outlineWidget
.removeItems( items
);
5898 this.updateOutlineWidget();
5900 this.stackLayout
.removeItems( pages
);
5901 this.emit( 'remove', pages
);
5907 * Clear all pages from the layout.
5912 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
5914 pages
= this.stackLayout
.getItems();
5917 this.currentPageName
= null;
5918 if ( this.outlined
) {
5919 this.outlineWidget
.clearItems();
5920 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5921 pages
[i
].setOutlineItem( null );
5924 this.stackLayout
.clearItems();
5926 this.emit( 'remove', pages
);
5932 * Set the current page by name.
5935 * @param {string} name Symbolic name of page
5937 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
5940 page
= this.pages
[name
];
5942 if ( name
!== this.currentPageName
) {
5943 if ( this.outlined
) {
5944 selectedItem
= this.outlineWidget
.getSelectedItem();
5945 if ( selectedItem
&& selectedItem
.getData() !== name
) {
5946 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
5950 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
5951 this.pages
[this.currentPageName
].setActive( false );
5952 // Blur anything focused if the next page doesn't have anything focusable - this
5953 // is not needed if the next page has something focusable because once it is focused
5954 // this blur happens automatically
5955 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
5956 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
5957 if ( $focused
.length
) {
5962 this.currentPageName
= name
;
5963 this.stackLayout
.setItem( page
);
5964 page
.setActive( true );
5965 this.emit( 'set', page
);
5971 * Call this after adding or removing items from the OutlineWidget.
5975 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
5976 // Auto-select first item when nothing is selected anymore
5977 if ( !this.outlineWidget
.getSelectedItem() ) {
5978 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
5985 * Layout made of a field and optional label.
5988 * @extends OO.ui.Layout
5989 * @mixins OO.ui.LabeledElement
5991 * Available label alignment modes include:
5992 * - left: Label is before the field and aligned away from it, best for when the user will be
5993 * scanning for a specific label in a form with many fields
5994 * - right: Label is before the field and aligned toward it, best for forms the user is very
5995 * familiar with and will tab through field checking quickly to verify which field they are in
5996 * - top: Label is before the field and above it, best for when the use will need to fill out all
5997 * fields from top to bottom in a form with few fields
5998 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
5999 * checkboxes or radio buttons
6002 * @param {OO.ui.Widget} field Field widget
6003 * @param {Object} [config] Configuration options
6004 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6005 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6007 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
6008 var popupButtonWidget
;
6009 // Config initialization
6010 config
= $.extend( { align
: 'left' }, config
);
6012 // Parent constructor
6013 OO
.ui
.FieldLayout
.super.call( this, config
);
6015 // Mixin constructors
6016 this.$help
= this.$( '<div>' );
6017 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
6018 if ( config
.help
) {
6019 popupButtonWidget
= new OO
.ui
.PopupButtonWidget( $.extend(
6029 popupButtonWidget
.getPopup().$body
.append( this.getElementDocument().createTextNode( config
.help
) );
6030 this.$help
= popupButtonWidget
.$element
;
6034 this.$field
= this.$( '<div>' );
6039 if ( this.field
instanceof OO
.ui
.InputWidget
) {
6040 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
6042 this.field
.connect( this, { disable
: 'onFieldDisable' } );
6045 this.$element
.addClass( 'oo-ui-fieldLayout' );
6047 .addClass( 'oo-ui-fieldLayout-field' )
6048 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
6049 .append( this.field
.$element
);
6050 this.setAlignment( config
.align
);
6055 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6056 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
6061 * Handle field disable events.
6063 * @param {boolean} value Field is disabled
6065 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6066 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6070 * Handle label mouse click events.
6072 * @param {jQuery.Event} e Mouse click event
6074 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6075 this.field
.simulateLabelClick();
6082 * @return {OO.ui.Widget} Field widget
6084 OO
.ui
.FieldLayout
.prototype.getField = function () {
6089 * Set the field alignment mode.
6091 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6094 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6095 if ( value
!== this.align
) {
6096 // Default to 'left'
6097 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6101 if ( value
=== 'inline' ) {
6102 this.$element
.append( this.$field
, this.$label
, this.$help
);
6104 this.$element
.append( this.$help
, this.$label
, this.$field
);
6108 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6111 // The following classes can be used here:
6112 // oo-ui-fieldLayout-align-left
6113 // oo-ui-fieldLayout-align-right
6114 // oo-ui-fieldLayout-align-top
6115 // oo-ui-fieldLayout-align-inline
6116 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
6123 * Layout made of a fieldset and optional legend.
6125 * Just add OO.ui.FieldLayout items.
6128 * @extends OO.ui.Layout
6129 * @mixins OO.ui.LabeledElement
6130 * @mixins OO.ui.IconedElement
6131 * @mixins OO.ui.GroupElement
6134 * @param {Object} [config] Configuration options
6135 * @cfg {string} [icon] Symbolic icon name
6136 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6138 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6139 // Config initialization
6140 config
= config
|| {};
6142 // Parent constructor
6143 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6145 // Mixin constructors
6146 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
6147 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
6148 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
6152 .addClass( 'oo-ui-fieldsetLayout' )
6153 .prepend( this.$icon
, this.$label
, this.$group
);
6154 if ( $.isArray( config
.items
) ) {
6155 this.addItems( config
.items
);
6161 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6162 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
6163 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
6164 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6166 /* Static Properties */
6168 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
6171 * Layout with an HTML form.
6174 * @extends OO.ui.Layout
6177 * @param {Object} [config] Configuration options
6179 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6180 // Configuration initialization
6181 config
= config
|| {};
6183 // Parent constructor
6184 OO
.ui
.FormLayout
.super.call( this, config
);
6187 this.$element
.on( 'submit', OO
.ui
.bind( this.onFormSubmit
, this ) );
6190 this.$element
.addClass( 'oo-ui-formLayout' );
6195 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6203 /* Static Properties */
6205 OO
.ui
.FormLayout
.static.tagName
= 'form';
6210 * Handle form submit events.
6212 * @param {jQuery.Event} e Submit event
6215 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6216 this.emit( 'submit' );
6221 * Layout made of proportionally sized columns and rows.
6224 * @extends OO.ui.Layout
6227 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6228 * @param {Object} [config] Configuration options
6229 * @cfg {number[]} [widths] Widths of columns as ratios
6230 * @cfg {number[]} [heights] Heights of columns as ratios
6232 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6235 // Config initialization
6236 config
= config
|| {};
6238 // Parent constructor
6239 OO
.ui
.GridLayout
.super.call( this, config
);
6247 this.$element
.addClass( 'oo-ui-gridLayout' );
6248 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6249 this.panels
.push( panels
[i
] );
6250 this.$element
.append( panels
[i
].$element
);
6252 if ( config
.widths
|| config
.heights
) {
6253 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6255 // Arrange in columns by default
6257 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
6260 this.layout( widths
, [ 1 ] );
6266 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6278 /* Static Properties */
6280 OO
.ui
.GridLayout
.static.tagName
= 'div';
6285 * Set grid dimensions.
6287 * @param {number[]} widths Widths of columns as ratios
6288 * @param {number[]} heights Heights of rows as ratios
6290 * @throws {Error} If grid is not large enough to fit all panels
6292 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6296 cols
= widths
.length
,
6297 rows
= heights
.length
;
6299 // Verify grid is big enough to fit panels
6300 if ( cols
* rows
< this.panels
.length
) {
6301 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6304 // Sum up denominators
6305 for ( x
= 0; x
< cols
; x
++ ) {
6308 for ( y
= 0; y
< rows
; y
++ ) {
6314 for ( x
= 0; x
< cols
; x
++ ) {
6315 this.widths
[x
] = widths
[x
] / xd
;
6317 for ( y
= 0; y
< rows
; y
++ ) {
6318 this.heights
[y
] = heights
[y
] / yd
;
6322 this.emit( 'layout' );
6326 * Update panel positions and sizes.
6330 OO
.ui
.GridLayout
.prototype.update = function () {
6338 cols
= this.widths
.length
,
6339 rows
= this.heights
.length
;
6341 for ( y
= 0; y
< rows
; y
++ ) {
6342 height
= this.heights
[y
];
6343 for ( x
= 0; x
< cols
; x
++ ) {
6344 panel
= this.panels
[i
];
6345 width
= this.widths
[x
];
6347 width
: Math
.round( width
* 100 ) + '%',
6348 height
: Math
.round( height
* 100 ) + '%',
6349 top
: Math
.round( top
* 100 ) + '%',
6350 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6351 visibility
: width
=== 0 || height
=== 0 ? 'hidden' : ''
6354 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6355 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6357 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6359 panel
.$element
.css( dimensions
);
6367 this.emit( 'update' );
6371 * Get a panel at a given position.
6373 * The x and y position is affected by the current grid layout.
6375 * @param {number} x Horizontal position
6376 * @param {number} y Vertical position
6377 * @return {OO.ui.PanelLayout} The panel at the given postion
6379 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
6380 return this.panels
[( x
* this.widths
.length
) + y
];
6384 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
6387 * @extends OO.ui.Layout
6390 * @param {Object} [config] Configuration options
6391 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
6392 * @cfg {boolean} [padded=false] Pad the content from the edges
6393 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
6395 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
6396 // Config initialization
6397 config
= config
|| {};
6399 // Parent constructor
6400 OO
.ui
.PanelLayout
.super.call( this, config
);
6403 this.$element
.addClass( 'oo-ui-panelLayout' );
6404 if ( config
.scrollable
) {
6405 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
6408 if ( config
.padded
) {
6409 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
6412 if ( config
.expanded
=== undefined || config
.expanded
) {
6413 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
6419 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
6422 * Page within an booklet layout.
6425 * @extends OO.ui.PanelLayout
6428 * @param {string} name Unique symbolic name of page
6429 * @param {Object} [config] Configuration options
6430 * @param {string} [outlineItem] Outline item widget
6432 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
6433 // Configuration initialization
6434 config
= $.extend( { scrollable
: true }, config
);
6436 // Parent constructor
6437 OO
.ui
.PageLayout
.super.call( this, config
);
6441 this.outlineItem
= config
.outlineItem
|| null;
6442 this.active
= false;
6445 this.$element
.addClass( 'oo-ui-pageLayout' );
6450 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
6456 * @param {boolean} active Page is active
6464 * @return {string} Symbolic name of page
6466 OO
.ui
.PageLayout
.prototype.getName = function () {
6471 * Check if page is active.
6473 * @return {boolean} Page is active
6475 OO
.ui
.PageLayout
.prototype.isActive = function () {
6482 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
6484 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
6485 return this.outlineItem
;
6491 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
6492 * outline item as desired; this method is called for setting (with an object) and unsetting
6493 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
6494 * operating on null instead of an OO.ui.OutlineItemWidget object.
6496 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
6499 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
6500 this.outlineItem
= outlineItem
|| null;
6501 if ( outlineItem
) {
6502 this.setupOutlineItem();
6508 * Setup outline item.
6510 * @localdoc Subclasses should override this method to adjust the outline item as desired.
6512 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
6515 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
6520 * Set page active state.
6522 * @param {boolean} Page is active
6525 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
6528 if ( active
!== this.active
) {
6529 this.active
= active
;
6530 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
6531 this.emit( 'active', this.active
);
6536 * Layout containing a series of mutually exclusive pages.
6539 * @extends OO.ui.PanelLayout
6540 * @mixins OO.ui.GroupElement
6543 * @param {Object} [config] Configuration options
6544 * @cfg {boolean} [continuous=false] Show all pages, one after another
6545 * @cfg {string} [icon=''] Symbolic icon name
6546 * @cfg {OO.ui.Layout[]} [items] Layouts to add
6548 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
6549 // Config initialization
6550 config
= $.extend( { scrollable
: true }, config
);
6552 // Parent constructor
6553 OO
.ui
.StackLayout
.super.call( this, config
);
6555 // Mixin constructors
6556 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
6559 this.currentItem
= null;
6560 this.continuous
= !!config
.continuous
;
6563 this.$element
.addClass( 'oo-ui-stackLayout' );
6564 if ( this.continuous
) {
6565 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
6567 if ( $.isArray( config
.items
) ) {
6568 this.addItems( config
.items
);
6574 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
6575 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
6581 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
6587 * Get the current item.
6589 * @return {OO.ui.Layout|null}
6591 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
6592 return this.currentItem
;
6596 * Unset the current item.
6599 * @param {OO.ui.StackLayout} layout
6602 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
6603 var prevItem
= this.currentItem
;
6604 if ( prevItem
=== null ) {
6608 this.currentItem
= null;
6609 this.emit( 'set', null );
6615 * Adding an existing item (by value) will move it.
6617 * @param {OO.ui.Layout[]} items Items to add
6618 * @param {number} [index] Index to insert items after
6621 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
6623 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
6625 if ( !this.currentItem
&& items
.length
) {
6626 this.setItem( items
[0] );
6635 * Items will be detached, not removed, so they can be used later.
6637 * @param {OO.ui.Layout[]} items Items to remove
6641 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
6643 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
6645 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
6646 if ( this.items
.length
) {
6647 this.setItem( this.items
[0] );
6649 this.unsetCurrentItem();
6659 * Items will be detached, not removed, so they can be used later.
6664 OO
.ui
.StackLayout
.prototype.clearItems = function () {
6665 this.unsetCurrentItem();
6666 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
6674 * Any currently shown item will be hidden.
6676 * FIXME: If the passed item to show has not been added in the items list, then
6677 * this method drops it and unsets the current item.
6679 * @param {OO.ui.Layout} item Item to show
6683 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
6686 if ( item
!== this.currentItem
) {
6687 if ( !this.continuous
) {
6688 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6689 this.items
[i
].$element
.css( 'display', '' );
6692 if ( $.inArray( item
, this.items
) !== -1 ) {
6693 if ( !this.continuous
) {
6694 item
.$element
.css( 'display', 'block' );
6696 this.currentItem
= item
;
6697 this.emit( 'set', item
);
6699 this.unsetCurrentItem();
6707 * Horizontal bar layout of tools as icon buttons.
6710 * @extends OO.ui.ToolGroup
6713 * @param {OO.ui.Toolbar} toolbar
6714 * @param {Object} [config] Configuration options
6716 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
6717 // Parent constructor
6718 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
6721 this.$element
.addClass( 'oo-ui-barToolGroup' );
6726 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
6728 /* Static Properties */
6730 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
6732 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
6734 OO
.ui
.BarToolGroup
.static.name
= 'bar';
6737 * Popup list of tools with an icon and optional label.
6741 * @extends OO.ui.ToolGroup
6742 * @mixins OO.ui.IconedElement
6743 * @mixins OO.ui.IndicatedElement
6744 * @mixins OO.ui.LabeledElement
6745 * @mixins OO.ui.TitledElement
6746 * @mixins OO.ui.ClippableElement
6749 * @param {OO.ui.Toolbar} toolbar
6750 * @param {Object} [config] Configuration options
6751 * @cfg {string} [header] Text to display at the top of the pop-up
6753 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
6754 // Configuration initialization
6755 config
= config
|| {};
6757 // Parent constructor
6758 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
6760 // Mixin constructors
6761 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6762 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6763 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6764 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
6765 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
6768 this.active
= false;
6769 this.dragging
= false;
6770 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
6771 this.$handle
= this.$( '<span>' );
6775 'mousedown touchstart': OO
.ui
.bind( this.onHandlePointerDown
, this ),
6776 'mouseup touchend': OO
.ui
.bind( this.onHandlePointerUp
, this )
6781 .addClass( 'oo-ui-popupToolGroup-handle' )
6782 .append( this.$icon
, this.$label
, this.$indicator
);
6783 // If the pop-up should have a header, add it to the top of the toolGroup.
6784 // Note: If this feature is useful for other widgets, we could abstract it into an
6785 // OO.ui.HeaderedElement mixin constructor.
6786 if ( config
.header
!== undefined ) {
6788 .prepend( this.$( '<span>' )
6789 .addClass( 'oo-ui-popupToolGroup-header' )
6790 .text( config
.header
)
6794 .addClass( 'oo-ui-popupToolGroup' )
6795 .prepend( this.$handle
);
6800 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
6801 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
6802 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
6803 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
6804 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
6805 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
6807 /* Static Properties */
6814 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
6816 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
6818 if ( this.isDisabled() && this.isElementAttached() ) {
6819 this.setActive( false );
6824 * Handle focus being lost.
6826 * The event is actually generated from a mouseup, so it is not a normal blur event object.
6828 * @param {jQuery.Event} e Mouse up event
6830 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
6831 // Only deactivate when clicking outside the dropdown element
6832 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
6833 this.setActive( false );
6840 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
6841 // e.which is 0 for touch events, 1 for left mouse button
6842 if ( !this.isDisabled() && e
.which
<= 1 ) {
6843 this.setActive( false );
6845 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
6849 * Handle mouse up events.
6851 * @param {jQuery.Event} e Mouse up event
6853 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
6858 * Handle mouse down events.
6860 * @param {jQuery.Event} e Mouse down event
6862 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
6863 // e.which is 0 for touch events, 1 for left mouse button
6864 if ( !this.isDisabled() && e
.which
<= 1 ) {
6865 this.setActive( !this.active
);
6871 * Switch into active mode.
6873 * When active, mouseup events anywhere in the document will trigger deactivation.
6875 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
6877 if ( this.active
!== value
) {
6878 this.active
= value
;
6880 this.setClipping( true );
6881 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
6882 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
6884 this.setClipping( false );
6885 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
6886 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
6892 * Drop down list layout of tools as labeled icon buttons.
6895 * @extends OO.ui.PopupToolGroup
6898 * @param {OO.ui.Toolbar} toolbar
6899 * @param {Object} [config] Configuration options
6901 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
6902 // Parent constructor
6903 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
6906 this.$element
.addClass( 'oo-ui-listToolGroup' );
6911 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
6913 /* Static Properties */
6915 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
6917 OO
.ui
.ListToolGroup
.static.name
= 'list';
6920 * Drop down menu layout of tools as selectable menu items.
6923 * @extends OO.ui.PopupToolGroup
6926 * @param {OO.ui.Toolbar} toolbar
6927 * @param {Object} [config] Configuration options
6929 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
6930 // Configuration initialization
6931 config
= config
|| {};
6933 // Parent constructor
6934 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
6937 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
6940 this.$element
.addClass( 'oo-ui-menuToolGroup' );
6945 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
6947 /* Static Properties */
6949 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
6951 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
6956 * Handle the toolbar state being updated.
6958 * When the state changes, the title of each active item in the menu will be joined together and
6959 * used as a label for the group. The label will be empty if none of the items are active.
6961 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
6965 for ( name
in this.tools
) {
6966 if ( this.tools
[name
].isActive() ) {
6967 labelTexts
.push( this.tools
[name
].getTitle() );
6971 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
6975 * Tool that shows a popup when selected.
6979 * @extends OO.ui.Tool
6980 * @mixins OO.ui.PopuppableElement
6983 * @param {OO.ui.Toolbar} toolbar
6984 * @param {Object} [config] Configuration options
6986 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
6987 // Parent constructor
6988 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
6990 // Mixin constructors
6991 OO
.ui
.PopuppableElement
.call( this, config
);
6995 .addClass( 'oo-ui-popupTool' )
6996 .append( this.popup
.$element
);
7001 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7002 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
7007 * Handle the tool being selected.
7011 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7012 if ( !this.isDisabled() ) {
7013 this.popup
.toggle();
7015 this.setActive( false );
7020 * Handle the toolbar state being updated.
7024 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7025 this.setActive( false );
7029 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7031 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7035 * @extends OO.ui.GroupElement
7038 * @param {jQuery} $group Container node, assigned to #$group
7039 * @param {Object} [config] Configuration options
7041 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
7042 // Parent constructor
7043 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
7048 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7053 * Set the disabled state of the widget.
7055 * This will also update the disabled state of child widgets.
7057 * @param {boolean} disabled Disable widget
7060 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7064 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7065 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7067 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7069 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7070 this.items
[i
].updateDisabled();
7078 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7080 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7081 * allows bidrectional communication.
7083 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7090 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7097 * Check if widget is disabled.
7099 * Checks parent if present, making disabled state inheritable.
7101 * @return {boolean} Widget is disabled
7103 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7104 return this.disabled
||
7105 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7109 * Set group element is in.
7111 * @param {OO.ui.GroupElement|null} group Group element, null if none
7114 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7116 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7117 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7119 // Initialize item disabled states
7120 this.updateDisabled();
7126 * Mixin that adds a menu showing suggested values for a text input.
7128 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7134 * @param {OO.ui.TextInputWidget} input Input widget
7135 * @param {Object} [config] Configuration options
7136 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
7138 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7139 // Config intialization
7140 config
= config
|| {};
7143 this.lookupInput
= input
;
7144 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
7145 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7146 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7147 input
: this.lookupInput
,
7148 $container
: config
.$container
7150 this.lookupCache
= {};
7151 this.lookupQuery
= null;
7152 this.lookupRequest
= null;
7153 this.populating
= false;
7156 this.$overlay
.append( this.lookupMenu
.$element
);
7158 this.lookupInput
.$input
.on( {
7159 focus
: OO
.ui
.bind( this.onLookupInputFocus
, this ),
7160 blur
: OO
.ui
.bind( this.onLookupInputBlur
, this ),
7161 mousedown
: OO
.ui
.bind( this.onLookupInputMouseDown
, this )
7163 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7166 this.$element
.addClass( 'oo-ui-lookupWidget' );
7167 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7173 * Handle input focus event.
7175 * @param {jQuery.Event} e Input focus event
7177 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7178 this.openLookupMenu();
7182 * Handle input blur event.
7184 * @param {jQuery.Event} e Input blur event
7186 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7187 this.lookupMenu
.toggle( false );
7191 * Handle input mouse down event.
7193 * @param {jQuery.Event} e Input mouse down event
7195 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7196 this.openLookupMenu();
7200 * Handle input change event.
7202 * @param {string} value New input value
7204 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7205 this.openLookupMenu();
7211 * @return {OO.ui.TextInputMenuWidget}
7213 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
7214 return this.lookupMenu
;
7222 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
7223 var value
= this.lookupInput
.getValue();
7225 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
7226 this.populateLookupMenu();
7227 this.lookupMenu
.toggle( true );
7238 * Populate lookup menu with current information.
7242 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
7245 if ( !this.populating
) {
7246 this.populating
= true;
7247 this.getLookupMenuItems()
7248 .done( function ( items
) {
7249 widget
.lookupMenu
.clearItems();
7250 if ( items
.length
) {
7254 widget
.initializeLookupMenuSelection();
7255 widget
.openLookupMenu();
7257 widget
.lookupMenu
.toggle( true );
7259 widget
.populating
= false;
7261 .fail( function () {
7262 widget
.lookupMenu
.clearItems();
7263 widget
.populating
= false;
7271 * Set selection in the lookup menu with current information.
7275 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
7276 if ( !this.lookupMenu
.getSelectedItem() ) {
7277 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
7279 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
7283 * Get lookup menu items for the current query.
7285 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
7288 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
7290 value
= this.lookupInput
.getValue(),
7291 deferred
= $.Deferred();
7293 if ( value
&& value
!== this.lookupQuery
) {
7294 // Abort current request if query has changed
7295 if ( this.lookupRequest
) {
7296 this.lookupRequest
.abort();
7297 this.lookupQuery
= null;
7298 this.lookupRequest
= null;
7300 if ( value
in this.lookupCache
) {
7301 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
7303 this.lookupQuery
= value
;
7304 this.lookupRequest
= this.getLookupRequest()
7305 .always( function () {
7306 widget
.lookupQuery
= null;
7307 widget
.lookupRequest
= null;
7309 .done( function ( data
) {
7310 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
7311 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
7313 .fail( function () {
7317 this.lookupRequest
.always( function () {
7318 widget
.popPending();
7322 return deferred
.promise();
7326 * Get a new request object of the current lookup query value.
7329 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
7331 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
7332 // Stub, implemented in subclass
7337 * Handle successful lookup request.
7339 * Overriding methods should call #populateLookupMenu when results are available and cache results
7340 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
7343 * @param {Mixed} data Response from server
7345 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
7346 // Stub, implemented in subclass
7350 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
7353 * @param {Mixed} data Cached result data, usually an array
7354 * @return {OO.ui.MenuItemWidget[]} Menu items
7356 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
7357 // Stub, implemented in subclass
7362 * Set of controls for an OO.ui.OutlineWidget.
7364 * Controls include moving items up and down, removing items, and adding different kinds of items.
7367 * @extends OO.ui.Widget
7368 * @mixins OO.ui.GroupElement
7369 * @mixins OO.ui.IconedElement
7372 * @param {OO.ui.OutlineWidget} outline Outline to control
7373 * @param {Object} [config] Configuration options
7375 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7376 // Configuration initialization
7377 config
= $.extend( { icon
: 'add-item' }, config
);
7379 // Parent constructor
7380 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7382 // Mixin constructors
7383 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
7384 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
7387 this.outline
= outline
;
7388 this.$movers
= this.$( '<div>' );
7389 this.upButton
= new OO
.ui
.ButtonWidget( {
7393 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
7395 this.downButton
= new OO
.ui
.ButtonWidget( {
7399 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
7401 this.removeButton
= new OO
.ui
.ButtonWidget( {
7405 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
7409 outline
.connect( this, {
7410 select
: 'onOutlineChange',
7411 add
: 'onOutlineChange',
7412 remove
: 'onOutlineChange'
7414 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
7415 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
7416 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
7419 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7420 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7422 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7423 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7424 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7429 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7430 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7431 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7437 * @param {number} places Number of places to move
7447 * Handle outline change events.
7449 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7450 var i
, len
, firstMovable
, lastMovable
,
7451 items
= this.outline
.getItems(),
7452 selectedItem
= this.outline
.getSelectedItem(),
7453 movable
= selectedItem
&& selectedItem
.isMovable(),
7454 removable
= selectedItem
&& selectedItem
.isRemovable();
7459 while ( ++i
< len
) {
7460 if ( items
[i
].isMovable() ) {
7461 firstMovable
= items
[i
];
7467 if ( items
[i
].isMovable() ) {
7468 lastMovable
= items
[i
];
7473 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7474 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7475 this.removeButton
.setDisabled( !removable
);
7479 * Mixin for widgets with a boolean on/off state.
7485 * @param {Object} [config] Configuration options
7486 * @cfg {boolean} [value=false] Initial value
7488 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7489 // Configuration initialization
7490 config
= config
|| {};
7496 this.$element
.addClass( 'oo-ui-toggleWidget' );
7497 this.setValue( !!config
.value
);
7504 * @param {boolean} value Changed value
7510 * Get the value of the toggle.
7514 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7519 * Set the value of the toggle.
7521 * @param {boolean} value New value
7525 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7527 if ( this.value
!== value
) {
7529 this.emit( 'change', value
);
7530 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7531 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7537 * Group widget for multiple related buttons.
7539 * Use together with OO.ui.ButtonWidget.
7542 * @extends OO.ui.Widget
7543 * @mixins OO.ui.GroupElement
7546 * @param {Object} [config] Configuration options
7547 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
7549 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
7550 // Parent constructor
7551 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
7553 // Mixin constructors
7554 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
7557 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
7558 if ( $.isArray( config
.items
) ) {
7559 this.addItems( config
.items
);
7565 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
7566 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
7569 * Generic widget for buttons.
7572 * @extends OO.ui.Widget
7573 * @mixins OO.ui.ButtonedElement
7574 * @mixins OO.ui.IconedElement
7575 * @mixins OO.ui.IndicatedElement
7576 * @mixins OO.ui.LabeledElement
7577 * @mixins OO.ui.TitledElement
7578 * @mixins OO.ui.FlaggableElement
7581 * @param {Object} [config] Configuration options
7582 * @cfg {string} [href] Hyperlink to visit when clicked
7583 * @cfg {string} [target] Target to open hyperlink in
7585 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
7586 // Configuration initialization
7587 config
= $.extend( { target
: '_blank' }, config
);
7589 // Parent constructor
7590 OO
.ui
.ButtonWidget
.super.call( this, config
);
7592 // Mixin constructors
7593 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7594 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
7595 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
7596 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
7597 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
7598 OO
.ui
.FlaggableElement
.call( this, config
);
7603 this.isHyperlink
= false;
7607 click
: OO
.ui
.bind( this.onClick
, this ),
7608 keypress
: OO
.ui
.bind( this.onKeyPress
, this )
7612 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
7614 .addClass( 'oo-ui-buttonWidget' )
7615 .append( this.$button
);
7616 this.setHref( config
.href
);
7617 this.setTarget( config
.target
);
7622 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
7623 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
7624 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
7625 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
7626 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
7627 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
7628 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
7639 * Handles mouse click events.
7641 * @param {jQuery.Event} e Mouse click event
7644 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
7645 if ( !this.isDisabled() ) {
7646 this.emit( 'click' );
7647 if ( this.isHyperlink
) {
7655 * Handles keypress events.
7657 * @param {jQuery.Event} e Keypress event
7660 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
7661 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
7663 if ( this.isHyperlink
) {
7671 * Get hyperlink location.
7673 * @return {string} Hyperlink location
7675 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
7680 * Get hyperlink target.
7682 * @return {string} Hyperlink target
7684 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
7689 * Set hyperlink location.
7691 * @param {string|null} href Hyperlink location, null to remove
7693 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
7694 href
= typeof href
=== 'string' ? href
: null;
7696 if ( href
!== this.href
) {
7698 if ( href
!== null ) {
7699 this.$button
.attr( 'href', href
);
7700 this.isHyperlink
= true;
7702 this.$button
.removeAttr( 'href' );
7703 this.isHyperlink
= false;
7711 * Set hyperlink target.
7713 * @param {string|null} target Hyperlink target, null to remove
7715 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
7716 target
= typeof target
=== 'string' ? target
: null;
7718 if ( target
!== this.target
) {
7719 this.target
= target
;
7720 if ( target
!== null ) {
7721 this.$button
.attr( 'target', target
);
7723 this.$button
.removeAttr( 'target' );
7731 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
7734 * @extends OO.ui.ButtonWidget
7737 * @param {Object} [config] Configuration options
7738 * @cfg {string} [action] Symbolic action name
7739 * @cfg {string[]} [modes] Symbolic mode names
7741 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
7742 // Config intialization
7743 config
= $.extend( { framed
: false }, config
);
7745 // Parent constructor
7746 OO
.ui
.ActionWidget
.super.call( this, config
);
7749 this.action
= config
.action
|| '';
7750 this.modes
= config
.modes
|| [];
7755 this.$element
.addClass( 'oo-ui-actionWidget' );
7760 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
7771 * Check if action is available in a certain mode.
7773 * @param {string} mode Name of mode
7774 * @return {boolean} Has mode
7776 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
7777 return this.modes
.indexOf( mode
) !== -1;
7781 * Get symbolic action name.
7785 OO
.ui
.ActionWidget
.prototype.getAction = function () {
7790 * Get symbolic action name.
7794 OO
.ui
.ActionWidget
.prototype.getModes = function () {
7795 return this.modes
.slice();
7799 * Emit a resize event if the size has changed.
7803 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
7806 if ( this.isElementAttached() ) {
7807 width
= this.$element
.width();
7808 height
= this.$element
.height();
7810 if ( width
!== this.width
|| height
!== this.height
) {
7812 this.height
= height
;
7813 this.emit( 'resize' );
7823 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
7825 OO
.ui
.IconedElement
.prototype.setIcon
.apply( this, arguments
);
7826 this.propagateResize();
7834 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
7836 OO
.ui
.LabeledElement
.prototype.setLabel
.apply( this, arguments
);
7837 this.propagateResize();
7845 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
7847 OO
.ui
.FlaggableElement
.prototype.setFlags
.apply( this, arguments
);
7848 this.propagateResize();
7856 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
7858 OO
.ui
.FlaggableElement
.prototype.clearFlags
.apply( this, arguments
);
7859 this.propagateResize();
7865 * Toggle visibility of button.
7867 * @param {boolean} [show] Show button, omit to toggle visibility
7870 OO
.ui
.ActionWidget
.prototype.toggle = function () {
7872 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
7873 this.propagateResize();
7879 * Button that shows and hides a popup.
7882 * @extends OO.ui.ButtonWidget
7883 * @mixins OO.ui.PopuppableElement
7886 * @param {Object} [config] Configuration options
7888 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7889 // Parent constructor
7890 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7892 // Mixin constructors
7893 OO
.ui
.PopuppableElement
.call( this, config
);
7897 .addClass( 'oo-ui-popupButtonWidget' )
7898 .append( this.popup
.$element
);
7903 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
7904 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
7909 * Handles mouse click events.
7911 * @param {jQuery.Event} e Mouse click event
7913 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
7914 // Skip clicks within the popup
7915 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
7919 if ( !this.isDisabled() ) {
7920 this.popup
.toggle();
7922 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
7928 * Button that toggles on and off.
7931 * @extends OO.ui.ButtonWidget
7932 * @mixins OO.ui.ToggleWidget
7935 * @param {Object} [config] Configuration options
7936 * @cfg {boolean} [value=false] Initial value
7938 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
7939 // Configuration initialization
7940 config
= config
|| {};
7942 // Parent constructor
7943 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
7945 // Mixin constructors
7946 OO
.ui
.ToggleWidget
.call( this, config
);
7949 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
7954 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
7955 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
7962 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
7963 if ( !this.isDisabled() ) {
7964 this.setValue( !this.value
);
7968 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
7974 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
7976 if ( value
!== this.value
) {
7977 this.setActive( value
);
7980 // Parent method (from mixin)
7981 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
7990 * @extends OO.ui.Widget
7991 * @mixins OO.ui.IconedElement
7992 * @mixins OO.ui.TitledElement
7995 * @param {Object} [config] Configuration options
7997 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
7998 // Config intialization
7999 config
= config
|| {};
8001 // Parent constructor
8002 OO
.ui
.IconWidget
.super.call( this, config
);
8004 // Mixin constructors
8005 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
8006 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
8009 this.$element
.addClass( 'oo-ui-iconWidget' );
8014 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8015 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
8016 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8018 /* Static Properties */
8020 OO
.ui
.IconWidget
.static.tagName
= 'span';
8025 * See OO.ui.IndicatedElement for more information.
8028 * @extends OO.ui.Widget
8029 * @mixins OO.ui.IndicatedElement
8030 * @mixins OO.ui.TitledElement
8033 * @param {Object} [config] Configuration options
8035 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8036 // Config intialization
8037 config
= config
|| {};
8039 // Parent constructor
8040 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8042 // Mixin constructors
8043 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
8044 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
8047 this.$element
.addClass( 'oo-ui-indicatorWidget' );
8052 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
8053 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
8054 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
8056 /* Static Properties */
8058 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
8061 * Inline menu of options.
8063 * Inline menus provide a control for accessing a menu and compose a menu within the widget, which
8064 * can be accessed using the #getMenu method.
8066 * Use with OO.ui.MenuOptionWidget.
8069 * @extends OO.ui.Widget
8070 * @mixins OO.ui.IconedElement
8071 * @mixins OO.ui.IndicatedElement
8072 * @mixins OO.ui.LabeledElement
8073 * @mixins OO.ui.TitledElement
8076 * @param {Object} [config] Configuration options
8077 * @cfg {Object} [menu] Configuration options to pass to menu widget
8079 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
8080 // Configuration initialization
8081 config
= $.extend( { indicator
: 'down' }, config
);
8083 // Parent constructor
8084 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
8086 // Mixin constructors
8087 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
8088 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
8089 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
8090 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
8093 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8094 this.$handle
= this.$( '<span>' );
8097 this.$element
.on( { click
: OO
.ui
.bind( this.onClick
, this ) } );
8098 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8102 .addClass( 'oo-ui-inlineMenuWidget-handle' )
8103 .append( this.$icon
, this.$label
, this.$indicator
);
8105 .addClass( 'oo-ui-inlineMenuWidget' )
8106 .append( this.$handle
, this.menu
.$element
);
8111 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
8112 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
8113 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
8114 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
8115 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
8122 * @return {OO.ui.MenuWidget} Menu of widget
8124 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
8129 * Handles menu select events.
8131 * @param {OO.ui.MenuItemWidget} item Selected menu item
8133 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
8140 selectedLabel
= item
.getLabel();
8142 // If the label is a DOM element, clone it, because setLabel will append() it
8143 if ( selectedLabel
instanceof jQuery
) {
8144 selectedLabel
= selectedLabel
.clone();
8147 this.setLabel( selectedLabel
);
8151 * Handles mouse click events.
8153 * @param {jQuery.Event} e Mouse click event
8155 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
8156 // Skip clicks within the menu
8157 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8161 if ( !this.isDisabled() ) {
8162 if ( this.menu
.isVisible() ) {
8163 this.menu
.toggle( false );
8165 this.menu
.toggle( true );
8172 * Base class for input widgets.
8176 * @extends OO.ui.Widget
8179 * @param {Object} [config] Configuration options
8180 * @cfg {string} [name=''] HTML input name
8181 * @cfg {string} [value=''] Input value
8182 * @cfg {boolean} [readOnly=false] Prevent changes
8183 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
8185 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8186 // Config intialization
8187 config
= $.extend( { readOnly
: false }, config
);
8189 // Parent constructor
8190 OO
.ui
.InputWidget
.super.call( this, config
);
8193 this.$input
= this.getInputElement( config
);
8195 this.readOnly
= false;
8196 this.inputFilter
= config
.inputFilter
;
8199 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
8203 .attr( 'name', config
.name
)
8204 .prop( 'disabled', this.isDisabled() );
8205 this.setReadOnly( config
.readOnly
);
8206 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
8207 this.setValue( config
.value
);
8212 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8224 * Get input element.
8226 * @param {Object} [config] Configuration options
8227 * @return {jQuery} Input element
8229 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8230 return this.$( '<input>' );
8234 * Handle potentially value-changing events.
8236 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8238 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8240 if ( !this.isDisabled() ) {
8241 // Allow the stack to clear so the value will be updated
8242 setTimeout( function () {
8243 widget
.setValue( widget
.$input
.val() );
8249 * Get the value of the input.
8251 * @return {string} Input value
8253 OO
.ui
.InputWidget
.prototype.getValue = function () {
8258 * Sets the direction of the current input, either RTL or LTR
8260 * @param {boolean} isRTL
8262 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
8264 this.$input
.removeClass( 'oo-ui-ltr' );
8265 this.$input
.addClass( 'oo-ui-rtl' );
8267 this.$input
.removeClass( 'oo-ui-rtl' );
8268 this.$input
.addClass( 'oo-ui-ltr' );
8273 * Set the value of the input.
8275 * @param {string} value New value
8279 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
8280 value
= this.sanitizeValue( value
);
8281 if ( this.value
!== value
) {
8283 this.emit( 'change', this.value
);
8285 // Update the DOM if it has changed. Note that with sanitizeValue, it
8286 // is possible for the DOM value to change without this.value changing.
8287 if ( this.$input
.val() !== this.value
) {
8288 this.$input
.val( this.value
);
8294 * Sanitize incoming value.
8296 * Ensures value is a string, and converts undefined and null to empty strings.
8298 * @param {string} value Original value
8299 * @return {string} Sanitized value
8301 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
8302 if ( value
=== undefined || value
=== null ) {
8304 } else if ( this.inputFilter
) {
8305 return this.inputFilter( String( value
) );
8307 return String( value
);
8312 * Simulate the behavior of clicking on a label bound to this input.
8314 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
8315 if ( !this.isDisabled() ) {
8316 if ( this.$input
.is( ':checkbox,:radio' ) ) {
8317 this.$input
.click();
8318 } else if ( this.$input
.is( ':input' ) ) {
8319 this.$input
[0].focus();
8325 * Check if the widget is read-only.
8329 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
8330 return this.readOnly
;
8334 * Set the read-only state of the widget.
8336 * This should probably change the widgets's appearance and prevent it from being used.
8338 * @param {boolean} state Make input read-only
8341 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
8342 this.readOnly
= !!state
;
8343 this.$input
.prop( 'readOnly', this.readOnly
);
8350 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
8351 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
8352 if ( this.$input
) {
8353 this.$input
.prop( 'disabled', this.isDisabled() );
8363 OO
.ui
.InputWidget
.prototype.focus = function () {
8364 this.$input
[0].focus();
8373 OO
.ui
.InputWidget
.prototype.blur = function () {
8374 this.$input
[0].blur();
8379 * Checkbox input widget.
8382 * @extends OO.ui.InputWidget
8385 * @param {Object} [config] Configuration options
8387 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
8388 // Parent constructor
8389 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
8392 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
8397 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
8404 * Get input element.
8406 * @return {jQuery} Input element
8408 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
8409 return this.$( '<input type="checkbox" />' );
8413 * Get checked state of the checkbox
8415 * @return {boolean} If the checkbox is checked
8417 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
8424 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
8426 if ( this.value
!== value
) {
8428 this.$input
.prop( 'checked', this.value
);
8429 this.emit( 'change', this.value
);
8436 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
8438 if ( !this.isDisabled() ) {
8439 // Allow the stack to clear so the value will be updated
8440 setTimeout( function () {
8441 widget
.setValue( widget
.$input
.prop( 'checked' ) );
8447 * Input widget with a text field.
8450 * @extends OO.ui.InputWidget
8453 * @param {Object} [config] Configuration options
8454 * @cfg {string} [placeholder] Placeholder text
8455 * @cfg {string} [icon] Symbolic name of icon
8456 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8457 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8458 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8460 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8462 config
= $.extend( { maxRows
: 10 }, config
);
8464 // Parent constructor
8465 OO
.ui
.TextInputWidget
.super.call( this, config
);
8469 this.multiline
= !!config
.multiline
;
8470 this.autosize
= !!config
.autosize
;
8471 this.maxRows
= config
.maxRows
;
8474 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8475 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8478 this.$element
.addClass( 'oo-ui-textInputWidget' );
8479 if ( config
.icon
) {
8480 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
8481 this.$element
.append(
8483 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
8484 .mousedown( function () {
8485 widget
.$input
[0].focus();
8490 if ( config
.placeholder
) {
8491 this.$input
.attr( 'placeholder', config
.placeholder
);
8493 this.$element
.attr( 'role', 'textbox' );
8498 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8503 * User presses enter inside the text box.
8505 * Not called if input is multiline.
8513 * Handle key press events.
8515 * @param {jQuery.Event} e Key press event
8516 * @fires enter If enter key is pressed and input is not multiline
8518 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8519 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8520 this.emit( 'enter' );
8525 * Handle element attach events.
8527 * @param {jQuery.Event} e Element attach event
8529 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8536 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8540 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
8546 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
8548 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
8555 * Automatically adjust the size of the text input.
8557 * This only affects multi-line inputs that are auto-sized.
8561 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
8562 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
8564 if ( this.multiline
&& this.autosize
) {
8565 $clone
= this.$input
.clone()
8566 .val( this.$input
.val() )
8567 .css( { height
: 0 } )
8568 .insertAfter( this.$input
);
8569 // Set inline height property to 0 to measure scroll height
8570 scrollHeight
= $clone
[0].scrollHeight
;
8571 // Remove inline height property to measure natural heights
8572 $clone
.css( 'height', '' );
8573 innerHeight
= $clone
.innerHeight();
8574 outerHeight
= $clone
.outerHeight();
8575 // Measure max rows height
8576 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
8577 maxInnerHeight
= $clone
.innerHeight();
8578 $clone
.removeAttr( 'rows' ).css( 'height', '' );
8580 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
8581 // Only apply inline height when expansion beyond natural height is needed
8584 // Use the difference between the inner and outer height as a buffer
8585 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
8592 * Get input element.
8594 * @param {Object} [config] Configuration options
8595 * @return {jQuery} Input element
8597 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
8598 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
8604 * Check if input supports multiple lines.
8608 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
8609 return !!this.multiline
;
8613 * Check if input automatically adjusts its size.
8617 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
8618 return !!this.autosize
;
8622 * Check if input is pending.
8626 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
8627 return !!this.pending
;
8631 * Increase the pending stack.
8635 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
8636 if ( this.pending
=== 0 ) {
8637 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
8638 this.$input
.addClass( 'oo-ui-texture-pending' );
8646 * Reduce the pending stack.
8652 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
8653 if ( this.pending
=== 1 ) {
8654 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
8655 this.$input
.removeClass( 'oo-ui-texture-pending' );
8657 this.pending
= Math
.max( 0, this.pending
- 1 );
8663 * Select the contents of the input.
8667 OO
.ui
.TextInputWidget
.prototype.select = function () {
8668 this.$input
.select();
8676 * @extends OO.ui.Widget
8677 * @mixins OO.ui.LabeledElement
8680 * @param {Object} [config] Configuration options
8682 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
8683 // Config intialization
8684 config
= config
|| {};
8686 // Parent constructor
8687 OO
.ui
.LabelWidget
.super.call( this, config
);
8689 // Mixin constructors
8690 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
8693 this.input
= config
.input
;
8696 if ( this.input
instanceof OO
.ui
.InputWidget
) {
8697 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8701 this.$element
.addClass( 'oo-ui-labelWidget' );
8706 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
8707 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
8709 /* Static Properties */
8711 OO
.ui
.LabelWidget
.static.tagName
= 'label';
8716 * Handles label mouse click events.
8718 * @param {jQuery.Event} e Mouse click event
8720 OO
.ui
.LabelWidget
.prototype.onClick = function () {
8721 this.input
.simulateLabelClick();
8726 * Generic option widget for use with OO.ui.SelectWidget.
8729 * @extends OO.ui.Widget
8730 * @mixins OO.ui.LabeledElement
8731 * @mixins OO.ui.FlaggableElement
8734 * @param {Mixed} data Option data
8735 * @param {Object} [config] Configuration options
8736 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
8738 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
8739 // Config intialization
8740 config
= config
|| {};
8742 // Parent constructor
8743 OO
.ui
.OptionWidget
.super.call( this, config
);
8745 // Mixin constructors
8746 OO
.ui
.ItemWidget
.call( this );
8747 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
8748 OO
.ui
.FlaggableElement
.call( this, config
);
8752 this.selected
= false;
8753 this.highlighted
= false;
8754 this.pressed
= false;
8758 .data( 'oo-ui-optionWidget', this )
8759 .attr( 'rel', config
.rel
)
8760 .attr( 'role', 'option' )
8761 .addClass( 'oo-ui-optionWidget' )
8762 .append( this.$label
);
8764 .prepend( this.$icon
)
8765 .append( this.$indicator
);
8770 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
8771 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
8772 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
8773 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
8775 /* Static Properties */
8777 OO
.ui
.OptionWidget
.static.tagName
= 'li';
8779 OO
.ui
.OptionWidget
.static.selectable
= true;
8781 OO
.ui
.OptionWidget
.static.highlightable
= true;
8783 OO
.ui
.OptionWidget
.static.pressable
= true;
8785 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
8790 * Check if option can be selected.
8792 * @return {boolean} Item is selectable
8794 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
8795 return this.constructor.static.selectable
&& !this.isDisabled();
8799 * Check if option can be highlighted.
8801 * @return {boolean} Item is highlightable
8803 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
8804 return this.constructor.static.highlightable
&& !this.isDisabled();
8808 * Check if option can be pressed.
8810 * @return {boolean} Item is pressable
8812 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
8813 return this.constructor.static.pressable
&& !this.isDisabled();
8817 * Check if option is selected.
8819 * @return {boolean} Item is selected
8821 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
8822 return this.selected
;
8826 * Check if option is highlighted.
8828 * @return {boolean} Item is highlighted
8830 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
8831 return this.highlighted
;
8835 * Check if option is pressed.
8837 * @return {boolean} Item is pressed
8839 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
8840 return this.pressed
;
8844 * Set selected state.
8846 * @param {boolean} [state=false] Select option
8849 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
8850 if ( this.constructor.static.selectable
) {
8851 this.selected
= !!state
;
8852 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
8853 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
8854 this.scrollElementIntoView();
8861 * Set highlighted state.
8863 * @param {boolean} [state=false] Highlight option
8866 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
8867 if ( this.constructor.static.highlightable
) {
8868 this.highlighted
= !!state
;
8869 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
8875 * Set pressed state.
8877 * @param {boolean} [state=false] Press option
8880 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
8881 if ( this.constructor.static.pressable
) {
8882 this.pressed
= !!state
;
8883 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
8889 * Make the option's highlight flash.
8891 * While flashing, the visual style of the pressed state is removed if present.
8893 * @return {jQuery.Promise} Promise resolved when flashing is done
8895 OO
.ui
.OptionWidget
.prototype.flash = function () {
8897 $element
= this.$element
,
8898 deferred
= $.Deferred();
8900 if ( !this.isDisabled() && this.constructor.static.pressable
) {
8901 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
8902 setTimeout( function () {
8903 // Restore original classes
8905 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
8906 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
8908 setTimeout( function () {
8915 return deferred
.promise();
8921 * @return {Mixed} Option data
8923 OO
.ui
.OptionWidget
.prototype.getData = function () {
8928 * Option widget with an option icon and indicator.
8930 * Use together with OO.ui.SelectWidget.
8933 * @extends OO.ui.OptionWidget
8934 * @mixins OO.ui.IconedElement
8935 * @mixins OO.ui.IndicatedElement
8938 * @param {Mixed} data Option data
8939 * @param {Object} [config] Configuration options
8941 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
8942 // Parent constructor
8943 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
8945 // Mixin constructors
8946 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
8947 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
8951 .addClass( 'oo-ui-decoratedOptionWidget' )
8952 .prepend( this.$icon
)
8953 .append( this.$indicator
);
8958 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
8959 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
8960 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
8963 * Option widget that looks like a button.
8965 * Use together with OO.ui.ButtonSelectWidget.
8968 * @extends OO.ui.DecoratedOptionWidget
8969 * @mixins OO.ui.ButtonedElement
8972 * @param {Mixed} data Option data
8973 * @param {Object} [config] Configuration options
8975 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
8976 // Parent constructor
8977 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
8979 // Mixin constructors
8980 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
8983 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
8984 this.$button
.append( this.$element
.contents() );
8985 this.$element
.append( this.$button
);
8990 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
8991 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
8993 /* Static Properties */
8995 // Allow button mouse down events to pass through so they can be handled by the parent select widget
8996 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
9003 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
9004 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
9006 if ( this.constructor.static.selectable
) {
9007 this.setActive( state
);
9014 * Item of an OO.ui.MenuWidget.
9017 * @extends OO.ui.DecoratedOptionWidget
9020 * @param {Mixed} data Item data
9021 * @param {Object} [config] Configuration options
9023 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
9024 // Configuration initialization
9025 config
= $.extend( { icon
: 'check' }, config
);
9027 // Parent constructor
9028 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
9032 .attr( 'role', 'menuitem' )
9033 .addClass( 'oo-ui-menuItemWidget' );
9038 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9041 * Section to group one or more items in a OO.ui.MenuWidget.
9044 * @extends OO.ui.DecoratedOptionWidget
9047 * @param {Mixed} data Item data
9048 * @param {Object} [config] Configuration options
9050 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
9051 // Parent constructor
9052 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
9055 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
9060 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9062 /* Static Properties */
9064 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
9066 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
9069 * Items for an OO.ui.OutlineWidget.
9072 * @extends OO.ui.DecoratedOptionWidget
9075 * @param {Mixed} data Item data
9076 * @param {Object} [config] Configuration options
9077 * @cfg {number} [level] Indentation level
9078 * @cfg {boolean} [movable] Allow modification from outline controls
9080 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
9081 // Config intialization
9082 config
= config
|| {};
9084 // Parent constructor
9085 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
9089 this.movable
= !!config
.movable
;
9090 this.removable
= !!config
.removable
;
9093 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
9094 this.setLevel( config
.level
);
9099 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9101 /* Static Properties */
9103 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
9105 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
9107 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
9109 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
9114 * Check if item is movable.
9116 * Movablilty is used by outline controls.
9118 * @return {boolean} Item is movable
9120 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
9121 return this.movable
;
9125 * Check if item is removable.
9127 * Removablilty is used by outline controls.
9129 * @return {boolean} Item is removable
9131 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
9132 return this.removable
;
9136 * Get indentation level.
9138 * @return {number} Indentation level
9140 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
9147 * Movablilty is used by outline controls.
9149 * @param {boolean} movable Item is movable
9152 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
9153 this.movable
= !!movable
;
9160 * Removablilty is used by outline controls.
9162 * @param {boolean} movable Item is removable
9165 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
9166 this.removable
= !!removable
;
9171 * Set indentation level.
9173 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
9176 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
9177 var levels
= this.constructor.static.levels
,
9178 levelClass
= this.constructor.static.levelClass
,
9181 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
9183 if ( this.level
=== i
) {
9184 this.$element
.addClass( levelClass
+ i
);
9186 this.$element
.removeClass( levelClass
+ i
);
9194 * Container for content that is overlaid and positioned absolutely.
9197 * @extends OO.ui.Widget
9198 * @mixins OO.ui.LabeledElement
9201 * @param {Object} [config] Configuration options
9202 * @cfg {number} [width=320] Width of popup in pixels
9203 * @cfg {number} [height] Height of popup, omit to use automatic height
9204 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
9205 * @cfg {string} [align='center'] Alignment of popup to origin
9206 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
9207 * @cfg {jQuery} [$content] Content to append to the popup's body
9208 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
9209 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
9210 * @cfg {boolean} [head] Show label and close button at the top
9211 * @cfg {boolean} [padded] Add padding to the body
9213 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
9214 // Config intialization
9215 config
= config
|| {};
9217 // Parent constructor
9218 OO
.ui
.PopupWidget
.super.call( this, config
);
9220 // Mixin constructors
9221 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
9222 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
9225 this.visible
= false;
9226 this.$popup
= this.$( '<div>' );
9227 this.$head
= this.$( '<div>' );
9228 this.$body
= this.$clippable
;
9229 this.$anchor
= this.$( '<div>' );
9230 this.$container
= config
.$container
|| this.$( 'body' );
9231 this.autoClose
= !!config
.autoClose
;
9232 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
9233 this.transitionTimeout
= null;
9235 this.width
= config
.width
!== undefined ? config
.width
: 320;
9236 this.height
= config
.height
!== undefined ? config
.height
: null;
9237 this.align
= config
.align
|| 'center';
9238 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
9239 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
9242 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
9245 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
9246 this.$body
.addClass( 'oo-ui-popupWidget-body' );
9247 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
9249 .addClass( 'oo-ui-popupWidget-head' )
9250 .append( this.$label
, this.closeButton
.$element
);
9251 if ( !config
.head
) {
9255 .addClass( 'oo-ui-popupWidget-popup' )
9256 .append( this.$head
, this.$body
);
9259 .addClass( 'oo-ui-popupWidget' )
9260 .append( this.$popup
, this.$anchor
);
9261 // Move content, which was added to #$element by OO.ui.Widget, to the body
9262 if ( config
.$content
instanceof jQuery
) {
9263 this.$body
.append( config
.$content
);
9265 if ( config
.padded
) {
9266 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
9272 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
9273 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
9274 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
9289 * Handles mouse down events.
9291 * @param {jQuery.Event} e Mouse down event
9293 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
9296 !$.contains( this.$element
[0], e
.target
) &&
9297 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
9299 this.toggle( false );
9304 * Bind mouse down listener.
9306 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
9307 // Capture clicks outside popup
9308 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
9312 * Handles close button click events.
9314 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
9315 if ( this.isVisible() ) {
9316 this.toggle( false );
9321 * Unbind mouse down listener.
9323 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
9324 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
9328 * Set whether to show a anchor.
9330 * @param {boolean} [show] Show anchor, omit to toggle
9332 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
9333 show
= show
=== undefined ? !this.anchored
: !!show
;
9335 if ( this.anchored
!== show
) {
9337 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
9339 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
9341 this.anchored
= show
;
9346 * Check if showing a anchor.
9348 * @return {boolean} anchor is visible
9350 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
9357 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
9358 show
= show
=== undefined ? !this.isVisible() : !!show
;
9360 var change
= show
!== this.isVisible();
9363 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
9367 this.setClipping( true );
9368 if ( this.autoClose
) {
9369 this.bindMouseDownListener();
9371 this.updateDimensions();
9373 this.setClipping( false );
9374 if ( this.autoClose
) {
9375 this.unbindMouseDownListener();
9384 * Set the size of the popup.
9386 * Changing the size may also change the popup's position depending on the alignment.
9388 * @param {number} width Width
9389 * @param {number} height Height
9390 * @param {boolean} [transition=false] Use a smooth transition
9393 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
9395 this.height
= height
!== undefined ? height
: null;
9396 if ( this.isVisible() ) {
9397 this.updateDimensions( transition
);
9402 * Update the size and position.
9404 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
9405 * be called automatically.
9407 * @param {boolean} [transition=false] Use a smooth transition
9410 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
9413 originOffset
= Math
.round( this.$element
.offset().left
),
9414 containerLeft
= Math
.round( this.$container
.offset().left
),
9415 containerWidth
= this.$container
.innerWidth(),
9416 containerRight
= containerLeft
+ containerWidth
,
9417 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
],
9418 anchorWidth
= this.$anchor
.width(),
9419 popupLeft
= popupOffset
- padding
,
9420 popupRight
= popupOffset
+ padding
+ this.width
+ padding
,
9421 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
9422 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
9424 // Prevent transition from being interrupted
9425 clearTimeout( this.transitionTimeout
);
9427 // Enable transition
9428 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
9431 if ( overlapRight
< 0 ) {
9432 popupOffset
+= overlapRight
;
9433 } else if ( overlapLeft
< 0 ) {
9434 popupOffset
-= overlapLeft
;
9437 // Adjust offset to avoid anchor being rendered too close to the edge
9438 if ( this.align
=== 'right' ) {
9439 popupOffset
+= anchorWidth
;
9440 } else if ( this.align
=== 'left' ) {
9441 popupOffset
-= anchorWidth
;
9444 // Position body relative to anchor and resize
9448 height
: this.height
!== null ? this.height
: 'auto'
9452 // Prevent transitioning after transition is complete
9453 this.transitionTimeout
= setTimeout( function () {
9454 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
9457 // Prevent transitioning immediately
9458 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
9467 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
9468 * Results are cleared and populated each time the query is changed.
9471 * @extends OO.ui.Widget
9474 * @param {Object} [config] Configuration options
9475 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
9476 * @cfg {string} [value] Initial query value
9478 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
9479 // Configuration intialization
9480 config
= config
|| {};
9482 // Parent constructor
9483 OO
.ui
.SearchWidget
.super.call( this, config
);
9486 this.query
= new OO
.ui
.TextInputWidget( {
9489 placeholder
: config
.placeholder
,
9492 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
9493 this.$query
= this.$( '<div>' );
9494 this.$results
= this.$( '<div>' );
9497 this.query
.connect( this, {
9498 change
: 'onQueryChange',
9499 enter
: 'onQueryEnter'
9501 this.results
.connect( this, {
9502 highlight
: 'onResultsHighlight',
9503 select
: 'onResultsSelect'
9505 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
9509 .addClass( 'oo-ui-searchWidget-query' )
9510 .append( this.query
.$element
);
9512 .addClass( 'oo-ui-searchWidget-results' )
9513 .append( this.results
.$element
);
9515 .addClass( 'oo-ui-searchWidget' )
9516 .append( this.$results
, this.$query
);
9521 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
9527 * @param {Object|null} item Item data or null if no item is highlighted
9532 * @param {Object|null} item Item data or null if no item is selected
9538 * Handle query key down events.
9540 * @param {jQuery.Event} e Key down event
9542 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
9543 var highlightedItem
, nextItem
,
9544 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
9547 highlightedItem
= this.results
.getHighlightedItem();
9548 if ( !highlightedItem
) {
9549 highlightedItem
= this.results
.getSelectedItem();
9551 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
9552 this.results
.highlightItem( nextItem
);
9553 nextItem
.scrollElementIntoView();
9558 * Handle select widget select events.
9560 * Clears existing results. Subclasses should repopulate items according to new query.
9562 * @param {string} value New value
9564 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
9566 this.results
.clearItems();
9570 * Handle select widget enter key events.
9572 * Selects highlighted item.
9574 * @param {string} value New value
9576 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
9578 this.results
.selectItem( this.results
.getHighlightedItem() );
9582 * Handle select widget highlight events.
9584 * @param {OO.ui.OptionWidget} item Highlighted item
9587 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
9588 this.emit( 'highlight', item
? item
.getData() : null );
9592 * Handle select widget select events.
9594 * @param {OO.ui.OptionWidget} item Selected item
9597 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
9598 this.emit( 'select', item
? item
.getData() : null );
9602 * Get the query input.
9604 * @return {OO.ui.TextInputWidget} Query input
9606 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
9611 * Get the results list.
9613 * @return {OO.ui.SelectWidget} Select list
9615 OO
.ui
.SearchWidget
.prototype.getResults = function () {
9616 return this.results
;
9620 * Generic selection of options.
9622 * Items can contain any rendering, and are uniquely identified by a has of thier data. Any widget
9623 * that provides options, from which the user must choose one, should be built on this class.
9625 * Use together with OO.ui.OptionWidget.
9628 * @extends OO.ui.Widget
9629 * @mixins OO.ui.GroupElement
9632 * @param {Object} [config] Configuration options
9633 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
9635 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
9636 // Config intialization
9637 config
= config
|| {};
9639 // Parent constructor
9640 OO
.ui
.SelectWidget
.super.call( this, config
);
9642 // Mixin constructors
9643 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
9646 this.pressed
= false;
9647 this.selecting
= null;
9649 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
9650 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
9654 mousedown
: OO
.ui
.bind( this.onMouseDown
, this ),
9655 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
9656 mouseleave
: OO
.ui
.bind( this.onMouseLeave
, this )
9660 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
9661 if ( $.isArray( config
.items
) ) {
9662 this.addItems( config
.items
);
9668 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
9670 // Need to mixin base class as well
9671 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
9672 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
9678 * @param {OO.ui.OptionWidget|null} item Highlighted item
9683 * @param {OO.ui.OptionWidget|null} item Pressed item
9688 * @param {OO.ui.OptionWidget|null} item Selected item
9693 * @param {OO.ui.OptionWidget|null} item Chosen item
9698 * @param {OO.ui.OptionWidget[]} items Added items
9699 * @param {number} index Index items were added at
9704 * @param {OO.ui.OptionWidget[]} items Removed items
9707 /* Static Properties */
9709 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
9714 * Handle mouse down events.
9717 * @param {jQuery.Event} e Mouse down event
9719 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
9722 if ( !this.isDisabled() && e
.which
=== 1 ) {
9723 this.togglePressed( true );
9724 item
= this.getTargetItem( e
);
9725 if ( item
&& item
.isSelectable() ) {
9726 this.pressItem( item
);
9727 this.selecting
= item
;
9728 this.getElementDocument().addEventListener(
9730 this.onMouseUpHandler
,
9733 this.getElementDocument().addEventListener(
9735 this.onMouseMoveHandler
,
9744 * Handle mouse up events.
9747 * @param {jQuery.Event} e Mouse up event
9749 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
9752 this.togglePressed( false );
9753 if ( !this.selecting
) {
9754 item
= this.getTargetItem( e
);
9755 if ( item
&& item
.isSelectable() ) {
9756 this.selecting
= item
;
9759 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
9760 this.pressItem( null );
9761 this.chooseItem( this.selecting
);
9762 this.selecting
= null;
9765 this.getElementDocument().removeEventListener(
9767 this.onMouseUpHandler
,
9770 this.getElementDocument().removeEventListener(
9772 this.onMouseMoveHandler
,
9780 * Handle mouse move events.
9783 * @param {jQuery.Event} e Mouse move event
9785 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
9788 if ( !this.isDisabled() && this.pressed
) {
9789 item
= this.getTargetItem( e
);
9790 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
9791 this.pressItem( item
);
9792 this.selecting
= item
;
9799 * Handle mouse over events.
9802 * @param {jQuery.Event} e Mouse over event
9804 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
9807 if ( !this.isDisabled() ) {
9808 item
= this.getTargetItem( e
);
9809 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
9815 * Handle mouse leave events.
9818 * @param {jQuery.Event} e Mouse over event
9820 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
9821 if ( !this.isDisabled() ) {
9822 this.highlightItem( null );
9828 * Get the closest item to a jQuery.Event.
9831 * @param {jQuery.Event} e
9832 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
9834 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
9835 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
9836 if ( $item
.length
) {
9837 return $item
.data( 'oo-ui-optionWidget' );
9843 * Get selected item.
9845 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
9847 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
9850 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9851 if ( this.items
[i
].isSelected() ) {
9852 return this.items
[i
];
9859 * Get highlighted item.
9861 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
9863 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
9866 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9867 if ( this.items
[i
].isHighlighted() ) {
9868 return this.items
[i
];
9875 * Get an existing item with equivilant data.
9877 * @param {Object} data Item data to search for
9878 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
9880 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
9881 var hash
= OO
.getHash( data
);
9883 if ( hash
in this.hashes
) {
9884 return this.hashes
[hash
];
9891 * Toggle pressed state.
9893 * @param {boolean} pressed An option is being pressed
9895 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
9896 if ( pressed
=== undefined ) {
9897 pressed
= !this.pressed
;
9899 if ( pressed
!== this.pressed
) {
9901 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
9902 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
9903 this.pressed
= pressed
;
9908 * Highlight an item.
9910 * Highlighting is mutually exclusive.
9912 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
9916 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
9917 var i
, len
, highlighted
,
9920 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9921 highlighted
= this.items
[i
] === item
;
9922 if ( this.items
[i
].isHighlighted() !== highlighted
) {
9923 this.items
[i
].setHighlighted( highlighted
);
9928 this.emit( 'highlight', item
);
9937 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
9941 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
9942 var i
, len
, selected
,
9945 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9946 selected
= this.items
[i
] === item
;
9947 if ( this.items
[i
].isSelected() !== selected
) {
9948 this.items
[i
].setSelected( selected
);
9953 this.emit( 'select', item
);
9962 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
9966 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
9967 var i
, len
, pressed
,
9970 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
9971 pressed
= this.items
[i
] === item
;
9972 if ( this.items
[i
].isPressed() !== pressed
) {
9973 this.items
[i
].setPressed( pressed
);
9978 this.emit( 'press', item
);
9987 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
9988 * an item is selected using the keyboard or mouse.
9990 * @param {OO.ui.OptionWidget} item Item to choose
9994 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
9995 this.selectItem( item
);
9996 this.emit( 'choose', item
);
10002 * Get an item relative to another one.
10004 * @param {OO.ui.OptionWidget} item Item to start at
10005 * @param {number} direction Direction to move in
10006 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
10008 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
10009 var inc
= direction
> 0 ? 1 : -1,
10010 len
= this.items
.length
,
10011 index
= item
instanceof OO
.ui
.OptionWidget
?
10012 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
10013 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
10015 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
10016 Math
.max( index
, -1 ) :
10017 // Default to n-1 instead of -1, if nothing is selected let's start at the end
10018 Math
.min( index
, len
);
10021 i
= ( i
+ inc
+ len
) % len
;
10022 item
= this.items
[i
];
10023 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10026 // Stop iterating when we've looped all the way around
10027 if ( i
=== stopAt
) {
10035 * Get the next selectable item.
10037 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
10039 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
10042 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10043 item
= this.items
[i
];
10044 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10055 * When items are added with the same values as existing items, the existing items will be
10056 * automatically removed before the new items are added.
10058 * @param {OO.ui.OptionWidget[]} items Items to add
10059 * @param {number} [index] Index to insert items after
10063 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
10064 var i
, len
, item
, hash
,
10067 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10069 hash
= OO
.getHash( item
.getData() );
10070 if ( hash
in this.hashes
) {
10071 // Remove item with same value
10072 remove
.push( this.hashes
[hash
] );
10074 this.hashes
[hash
] = item
;
10076 if ( remove
.length
) {
10077 this.removeItems( remove
);
10081 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
10083 // Always provide an index, even if it was omitted
10084 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
10092 * Items will be detached, not removed, so they can be used later.
10094 * @param {OO.ui.OptionWidget[]} items Items to remove
10098 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
10099 var i
, len
, item
, hash
;
10101 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10103 hash
= OO
.getHash( item
.getData() );
10104 if ( hash
in this.hashes
) {
10105 // Remove existing item
10106 delete this.hashes
[hash
];
10108 if ( item
.isSelected() ) {
10109 this.selectItem( null );
10114 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
10116 this.emit( 'remove', items
);
10124 * Items will be detached, not removed, so they can be used later.
10129 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
10130 var items
= this.items
.slice();
10135 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
10136 this.selectItem( null );
10138 this.emit( 'remove', items
);
10144 * Select widget containing button options.
10146 * Use together with OO.ui.ButtonOptionWidget.
10149 * @extends OO.ui.SelectWidget
10152 * @param {Object} [config] Configuration options
10154 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
10155 // Parent constructor
10156 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
10159 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
10164 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
10167 * Overlaid menu of options.
10169 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
10172 * Use together with OO.ui.MenuItemWidget.
10175 * @extends OO.ui.SelectWidget
10176 * @mixins OO.ui.ClippableElement
10179 * @param {Object} [config] Configuration options
10180 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
10181 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
10182 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
10184 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
10185 // Config intialization
10186 config
= config
|| {};
10188 // Parent constructor
10189 OO
.ui
.MenuWidget
.super.call( this, config
);
10191 // Mixin constructors
10192 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
10195 this.flashing
= false;
10196 this.visible
= false;
10197 this.newItems
= null;
10198 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
10199 this.$input
= config
.input
? config
.input
.$input
: null;
10200 this.$widget
= config
.widget
? config
.widget
.$element
: null;
10201 this.$previousFocus
= null;
10202 this.isolated
= !config
.input
;
10203 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
10204 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
10209 .attr( 'role', 'menu' )
10210 .addClass( 'oo-ui-menuWidget' );
10215 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
10216 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
10221 * Handles document mouse down events.
10223 * @param {jQuery.Event} e Key down event
10225 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
10226 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
10227 this.toggle( false );
10232 * Handles key down events.
10234 * @param {jQuery.Event} e Key down event
10236 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
10239 highlightItem
= this.getHighlightedItem();
10241 if ( !this.isDisabled() && this.isVisible() ) {
10242 if ( !highlightItem
) {
10243 highlightItem
= this.getSelectedItem();
10245 switch ( e
.keyCode
) {
10246 case OO
.ui
.Keys
.ENTER
:
10247 this.chooseItem( highlightItem
);
10250 case OO
.ui
.Keys
.UP
:
10251 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
10254 case OO
.ui
.Keys
.DOWN
:
10255 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
10258 case OO
.ui
.Keys
.ESCAPE
:
10259 if ( highlightItem
) {
10260 highlightItem
.setHighlighted( false );
10262 this.toggle( false );
10268 this.highlightItem( nextItem
);
10269 nextItem
.scrollElementIntoView();
10273 e
.preventDefault();
10274 e
.stopPropagation();
10281 * Bind key down listener.
10283 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
10284 if ( this.$input
) {
10285 this.$input
.on( 'keydown', this.onKeyDownHandler
);
10287 // Capture menu navigation keys
10288 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
10293 * Unbind key down listener.
10295 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
10296 if ( this.$input
) {
10297 this.$input
.off( 'keydown' );
10299 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
10306 * This will close the menu when done, unlike selectItem which only changes selection.
10308 * @param {OO.ui.OptionWidget} item Item to choose
10311 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
10315 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
10317 if ( item
&& !this.flashing
) {
10318 this.flashing
= true;
10319 item
.flash().done( function () {
10320 widget
.toggle( false );
10321 widget
.flashing
= false;
10324 this.toggle( false );
10333 * Adding an existing item (by value) will move it.
10335 * @param {OO.ui.MenuItemWidget[]} items Items to add
10336 * @param {number} [index] Index to insert items after
10339 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
10343 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
10346 if ( !this.newItems
) {
10347 this.newItems
= [];
10350 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10352 if ( this.isVisible() ) {
10353 // Defer fitting label until
10356 this.newItems
.push( item
);
10366 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
10367 visible
= !!visible
&& !!this.items
.length
;
10370 change
= visible
!== this.isVisible();
10373 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
10377 this.bindKeyDownListener();
10379 // Change focus to enable keyboard navigation
10380 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
10381 this.$previousFocus
= this.$( ':focus' );
10382 this.$input
[0].focus();
10384 if ( this.newItems
&& this.newItems
.length
) {
10385 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
10386 this.newItems
[i
].fitLabel();
10388 this.newItems
= null;
10390 this.setClipping( true );
10393 if ( this.autoHide
) {
10394 this.getElementDocument().addEventListener(
10395 'mousedown', this.onDocumentMouseDownHandler
, true
10399 this.unbindKeyDownListener();
10400 if ( this.isolated
&& this.$previousFocus
) {
10401 this.$previousFocus
[0].focus();
10402 this.$previousFocus
= null;
10404 this.getElementDocument().removeEventListener(
10405 'mousedown', this.onDocumentMouseDownHandler
, true
10407 this.setClipping( false );
10415 * Menu for a text input widget.
10417 * This menu is specially designed to be positioned beneeth the text input widget. Even if the input
10418 * is in a different frame, the menu's position is automatically calulated and maintained when the
10419 * menu is toggled or the window is resized.
10422 * @extends OO.ui.MenuWidget
10425 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
10426 * @param {Object} [config] Configuration options
10427 * @cfg {jQuery} [$container=input.$element] Element to render menu under
10429 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
10430 // Parent constructor
10431 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
10434 this.input
= input
;
10435 this.$container
= config
.$container
|| this.input
.$element
;
10436 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
10439 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
10444 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
10449 * Handle window resize event.
10451 * @param {jQuery.Event} e Window resize event
10453 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
10460 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
10461 visible
= !!visible
;
10463 var change
= visible
!== this.isVisible();
10466 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
10469 if ( this.isVisible() ) {
10471 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
10473 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
10480 * Position the menu.
10484 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
10486 $container
= this.$container
,
10487 dimensions
= $container
.offset();
10489 // Position under input
10490 dimensions
.top
+= $container
.height();
10492 // Compensate for frame position if in a differnt frame
10493 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
10494 frameOffset
= OO
.ui
.Element
.getRelativePosition(
10495 this.input
.$.frame
.$element
, this.$element
.offsetParent()
10497 dimensions
.left
+= frameOffset
.left
;
10498 dimensions
.top
+= frameOffset
.top
;
10500 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
10501 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
10502 dimensions
.right
= this.$element
.parent().position().left
-
10503 $container
.width() - dimensions
.left
;
10504 // Erase the value for 'left':
10505 delete dimensions
.left
;
10508 this.$element
.css( dimensions
);
10509 this.setIdealSize( $container
.width() );
10515 * Structured list of items.
10517 * Use with OO.ui.OutlineItemWidget.
10520 * @extends OO.ui.SelectWidget
10523 * @param {Object} [config] Configuration options
10525 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
10526 // Config intialization
10527 config
= config
|| {};
10529 // Parent constructor
10530 OO
.ui
.OutlineWidget
.super.call( this, config
);
10533 this.$element
.addClass( 'oo-ui-outlineWidget' );
10538 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
10541 * Switch that slides on and off.
10544 * @extends OO.ui.Widget
10545 * @mixins OO.ui.ToggleWidget
10548 * @param {Object} [config] Configuration options
10549 * @cfg {boolean} [value=false] Initial value
10551 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
10552 // Parent constructor
10553 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
10555 // Mixin constructors
10556 OO
.ui
.ToggleWidget
.call( this, config
);
10559 this.dragging
= false;
10560 this.dragStart
= null;
10561 this.sliding
= false;
10562 this.$glow
= this.$( '<span>' );
10563 this.$grip
= this.$( '<span>' );
10566 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
10569 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
10570 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
10572 .addClass( 'oo-ui-toggleSwitchWidget' )
10573 .append( this.$glow
, this.$grip
);
10578 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
10579 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
10584 * Handle mouse down events.
10586 * @param {jQuery.Event} e Mouse down event
10588 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
10589 if ( !this.isDisabled() && e
.which
=== 1 ) {
10590 this.setValue( !this.value
);