2 * OOjs UI v0.1.0-pre (880100c45e)
3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2014-09-23T22:28:43Z
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
) ) {
192 * Element that can be marked as pending.
198 * @param {Object} [config] Configuration options
200 OO
.ui
.PendingElement
= function OoUiPendingElement( config
) {
201 // Config initialisation
202 config
= config
|| {};
206 this.$pending
= null;
209 this.setPendingElement( config
.$pending
|| this.$element
);
214 OO
.initClass( OO
.ui
.PendingElement
);
219 * Set the pending element (and clean up any existing one).
221 * @param {jQuery} $pending The element to set to pending.
223 OO
.ui
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
224 if ( this.$pending
) {
225 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
228 this.$pending
= $pending
;
229 if ( this.pending
> 0 ) {
230 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
235 * Check if input is pending.
239 OO
.ui
.PendingElement
.prototype.isPending = function () {
240 return !!this.pending
;
244 * Increase the pending stack.
248 OO
.ui
.PendingElement
.prototype.pushPending = function () {
249 if ( this.pending
=== 0 ) {
250 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
258 * Reduce the pending stack.
264 OO
.ui
.PendingElement
.prototype.popPending = function () {
265 if ( this.pending
=== 1 ) {
266 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
268 this.pending
= Math
.max( 0, this.pending
- 1 );
278 * @mixins OO.EventEmitter
281 * @param {Object} [config] Configuration options
283 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
284 // Configuration intialization
285 config
= config
|| {};
287 // Mixin constructors
288 OO
.EventEmitter
.call( this );
293 actions
: 'getAction',
297 this.categorized
= {};
300 this.organized
= false;
301 this.changing
= false;
302 this.changed
= false;
307 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
309 /* Static Properties */
312 * Symbolic name of dialog.
319 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
325 * @param {OO.ui.ActionWidget} action Action that was clicked
330 * @param {OO.ui.ActionWidget} action Action that was resized
335 * @param {OO.ui.ActionWidget[]} added Actions added
340 * @param {OO.ui.ActionWidget[]} added Actions removed
350 * Handle action change events.
354 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
355 this.organized
= false;
356 if ( this.changing
) {
359 this.emit( 'change' );
364 * Check if a action is one of the special actions.
366 * @param {OO.ui.ActionWidget} action Action to check
367 * @return {boolean} Action is special
369 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
372 for ( flag
in this.special
) {
373 if ( action
=== this.special
[flag
] ) {
384 * @param {Object} [filters] Filters to use, omit to get all actions
385 * @param {string|string[]} [filters.actions] Actions that actions must have
386 * @param {string|string[]} [filters.flags] Flags that actions must have
387 * @param {string|string[]} [filters.modes] Modes that actions must have
388 * @param {boolean} [filters.visible] Actions must be visible
389 * @param {boolean} [filters.disabled] Actions must be disabled
390 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
392 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
393 var i
, len
, list
, category
, actions
, index
, match
, matches
;
398 // Collect category candidates
400 for ( category
in this.categorized
) {
401 list
= filters
[category
];
403 if ( !Array
.isArray( list
) ) {
406 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
407 actions
= this.categorized
[category
][list
[i
]];
408 if ( Array
.isArray( actions
) ) {
409 matches
.push
.apply( matches
, actions
);
414 // Remove by boolean filters
415 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
418 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
419 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
421 matches
.splice( i
, 1 );
427 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
429 index
= matches
.lastIndexOf( match
);
430 while ( index
!== i
) {
431 matches
.splice( index
, 1 );
433 index
= matches
.lastIndexOf( match
);
438 return this.list
.slice();
442 * Get special actions.
444 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
445 * Special flags can be configured by changing #static-specialFlags in a subclass.
447 * @return {OO.ui.ActionWidget|null} Safe action
449 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
451 return $.extend( {}, this.special
);
457 * Other actions include all non-special visible actions.
459 * @return {OO.ui.ActionWidget[]} Other actions
461 OO
.ui
.ActionSet
.prototype.getOthers = function () {
463 return this.others
.slice();
467 * Toggle actions based on their modes.
469 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
470 * visibility; matching actions will be shown, non-matching actions will be hidden.
472 * @param {string} mode Mode actions must have
477 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
480 this.changing
= true;
481 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
482 action
= this.list
[i
];
483 action
.toggle( action
.hasMode( mode
) );
486 this.organized
= false;
487 this.changing
= false;
488 this.emit( 'change' );
494 * Change which actions are able to be performed.
496 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
498 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
499 * indicate actions are able to be performed
502 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
503 var i
, len
, action
, item
;
505 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
507 action
= item
.getAction();
508 if ( actions
[action
] !== undefined ) {
509 item
.setDisabled( !actions
[action
] );
517 * Executes a function once per action.
519 * When making changes to multiple actions, use this method instead of iterating over the actions
520 * manually to defer emitting a change event until after all actions have been changed.
522 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
523 * @param {Function} callback Callback to run for each action; callback is invoked with three
524 * arguments: the action, the action's index, the list of actions being iterated over
527 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
528 this.changed
= false;
529 this.changing
= true;
530 this.get( filter
).forEach( callback
);
531 this.changing
= false;
532 if ( this.changed
) {
533 this.emit( 'change' );
542 * @param {OO.ui.ActionWidget[]} actions Actions to add
547 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
550 this.changing
= true;
551 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
553 action
.connect( this, {
554 click
: [ 'emit', 'click', action
],
555 resize
: [ 'emit', 'resize', action
],
556 toggle
: [ 'onActionChange' ]
558 this.list
.push( action
);
560 this.organized
= false;
561 this.emit( 'add', actions
);
562 this.changing
= false;
563 this.emit( 'change' );
571 * @param {OO.ui.ActionWidget[]} actions Actions to remove
576 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
577 var i
, len
, index
, action
;
579 this.changing
= true;
580 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
582 index
= this.list
.indexOf( action
);
583 if ( index
!== -1 ) {
584 action
.disconnect( this );
585 this.list
.splice( index
, 1 );
588 this.organized
= false;
589 this.emit( 'remove', actions
);
590 this.changing
= false;
591 this.emit( 'change' );
597 * Remove all actions.
603 OO
.ui
.ActionSet
.prototype.clear = function () {
605 removed
= this.list
.slice();
607 this.changing
= true;
608 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
609 action
= this.list
[i
];
610 action
.disconnect( this );
615 this.organized
= false;
616 this.emit( 'remove', removed
);
617 this.changing
= false;
618 this.emit( 'change' );
626 * This is called whenver organized information is requested. It will only reorganize the actions
627 * if something has changed since the last time it ran.
632 OO
.ui
.ActionSet
.prototype.organize = function () {
633 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
634 specialFlags
= this.constructor.static.specialFlags
;
636 if ( !this.organized
) {
637 this.categorized
= {};
640 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
641 action
= this.list
[i
];
642 if ( action
.isVisible() ) {
643 // Populate catgeories
644 for ( category
in this.categories
) {
645 if ( !this.categorized
[category
] ) {
646 this.categorized
[category
] = {};
648 list
= action
[this.categories
[category
]]();
649 if ( !Array
.isArray( list
) ) {
652 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
654 if ( !this.categorized
[category
][item
] ) {
655 this.categorized
[category
][item
] = [];
657 this.categorized
[category
][item
].push( action
);
660 // Populate special/others
662 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
663 flag
= specialFlags
[j
];
664 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
665 this.special
[flag
] = action
;
671 this.others
.push( action
);
675 this.organized
= true;
682 * DOM element abstraction.
688 * @param {Object} [config] Configuration options
689 * @cfg {Function} [$] jQuery for the frame the widget is in
690 * @cfg {string[]} [classes] CSS class names
691 * @cfg {string} [text] Text to insert
692 * @cfg {jQuery} [$content] Content elements to append (after text)
694 OO
.ui
.Element
= function OoUiElement( config
) {
695 // Configuration initialization
696 config
= config
|| {};
699 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
700 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
701 this.elementGroup
= null;
704 if ( $.isArray( config
.classes
) ) {
705 this.$element
.addClass( config
.classes
.join( ' ' ) );
708 this.$element
.text( config
.text
);
710 if ( config
.$content
) {
711 this.$element
.append( config
.$content
);
717 OO
.initClass( OO
.ui
.Element
);
719 /* Static Properties */
724 * This may be ignored if getTagName is overridden.
730 OO
.ui
.Element
.static.tagName
= 'div';
735 * Get a jQuery function within a specific document.
738 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
739 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
741 * @return {Function} Bound jQuery function
743 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
744 function wrapper( selector
) {
745 return $( selector
, wrapper
.context
);
748 wrapper
.context
= this.getDocument( context
);
751 wrapper
.$iframe
= $iframe
;
758 * Get the document of an element.
761 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
762 * @return {HTMLDocument|null} Document object
764 OO
.ui
.Element
.getDocument = function ( obj
) {
765 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
766 return ( obj
[0] && obj
[0].ownerDocument
) ||
767 // Empty jQuery selections might have a context
774 ( obj
.nodeType
=== 9 && obj
) ||
779 * Get the window of an element or document.
782 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
783 * @return {Window} Window object
785 OO
.ui
.Element
.getWindow = function ( obj
) {
786 var doc
= this.getDocument( obj
);
787 return doc
.parentWindow
|| doc
.defaultView
;
791 * Get the direction of an element or document.
794 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
795 * @return {string} Text direction, either `ltr` or `rtl`
797 OO
.ui
.Element
.getDir = function ( obj
) {
800 if ( obj
instanceof jQuery
) {
803 isDoc
= obj
.nodeType
=== 9;
804 isWin
= obj
.document
!== undefined;
805 if ( isDoc
|| isWin
) {
811 return $( obj
).css( 'direction' );
815 * Get the offset between two frames.
817 * TODO: Make this function not use recursion.
820 * @param {Window} from Window of the child frame
821 * @param {Window} [to=window] Window of the parent frame
822 * @param {Object} [offset] Offset to start with, used internally
823 * @return {Object} Offset object, containing left and top properties
825 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
826 var i
, len
, frames
, frame
, rect
;
832 offset
= { top
: 0, left
: 0 };
834 if ( from.parent
=== from ) {
838 // Get iframe element
839 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
840 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
841 if ( frames
[i
].contentWindow
=== from ) {
847 // Recursively accumulate offset values
849 rect
= frame
.getBoundingClientRect();
850 offset
.left
+= rect
.left
;
851 offset
.top
+= rect
.top
;
853 this.getFrameOffset( from.parent
, offset
);
860 * Get the offset between two elements.
863 * @param {jQuery} $from
864 * @param {jQuery} $to
865 * @return {Object} Translated position coordinates, containing top and left properties
867 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
868 var from = $from.offset(),
870 return { top
: Math
.round( from.top
- to
.top
), left
: Math
.round( from.left
- to
.left
) };
874 * Get element border sizes.
877 * @param {HTMLElement} el Element to measure
878 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
880 OO
.ui
.Element
.getBorders = function ( el
) {
881 var doc
= el
.ownerDocument
,
882 win
= doc
.parentWindow
|| doc
.defaultView
,
883 style
= win
&& win
.getComputedStyle
?
884 win
.getComputedStyle( el
, null ) :
887 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
888 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
889 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
890 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
893 top
: Math
.round( top
),
894 left
: Math
.round( left
),
895 bottom
: Math
.round( bottom
),
896 right
: Math
.round( right
)
901 * Get dimensions of an element or window.
904 * @param {HTMLElement|Window} el Element to measure
905 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
907 OO
.ui
.Element
.getDimensions = function ( el
) {
909 doc
= el
.ownerDocument
|| el
.document
,
910 win
= doc
.parentWindow
|| doc
.defaultView
;
912 if ( win
=== el
|| el
=== doc
.documentElement
) {
915 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
917 top
: $win
.scrollTop(),
918 left
: $win
.scrollLeft()
920 scrollbar
: { right
: 0, bottom
: 0 },
924 bottom
: $win
.innerHeight(),
925 right
: $win
.innerWidth()
931 borders
: this.getBorders( el
),
933 top
: $el
.scrollTop(),
934 left
: $el
.scrollLeft()
937 right
: $el
.innerWidth() - el
.clientWidth
,
938 bottom
: $el
.innerHeight() - el
.clientHeight
940 rect
: el
.getBoundingClientRect()
946 * Get closest scrollable container.
948 * Traverses up until either a scrollable element or the root is reached, in which case the window
952 * @param {HTMLElement} el Element to find scrollable container for
953 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
954 * @return {HTMLElement} Closest scrollable container
956 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
958 props
= [ 'overflow' ],
959 $parent
= $( el
).parent();
961 if ( dimension
=== 'x' || dimension
=== 'y' ) {
962 props
.push( 'overflow-' + dimension
);
965 while ( $parent
.length
) {
966 if ( $parent
[0] === el
.ownerDocument
.body
) {
971 val
= $parent
.css( props
[i
] );
972 if ( val
=== 'auto' || val
=== 'scroll' ) {
976 $parent
= $parent
.parent();
978 return this.getDocument( el
).body
;
982 * Scroll element into view.
985 * @param {HTMLElement} el Element to scroll into view
986 * @param {Object} [config={}] Configuration config
987 * @param {string} [config.duration] jQuery animation duration value
988 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
989 * to scroll in both directions
990 * @param {Function} [config.complete] Function to call when scrolling completes
992 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
993 // Configuration initialization
994 config
= config
|| {};
997 callback
= typeof config
.complete
=== 'function' && config
.complete
,
998 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
1000 eld
= this.getDimensions( el
),
1001 scd
= this.getDimensions( sc
),
1002 $win
= $( this.getWindow( el
) );
1004 // Compute the distances between the edges of el and the edges of the scroll viewport
1005 if ( $sc
.is( 'body' ) ) {
1006 // If the scrollable container is the <body> this is easy
1009 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
1010 left
: eld
.rect
.left
,
1011 right
: $win
.innerWidth() - eld
.rect
.right
1014 // Otherwise, we have to subtract el's coordinates from sc's coordinates
1016 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
1017 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
1018 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
1019 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
1023 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1024 if ( rel
.top
< 0 ) {
1025 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
1026 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
1027 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
1030 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1031 if ( rel
.left
< 0 ) {
1032 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
1033 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
1034 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
1037 if ( !$.isEmptyObject( anim
) ) {
1038 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
1040 $sc
.queue( function ( next
) {
1055 * Get the HTML tag name.
1057 * Override this method to base the result on instance information.
1059 * @return {string} HTML tag name
1061 OO
.ui
.Element
.prototype.getTagName = function () {
1062 return this.constructor.static.tagName
;
1066 * Check if the element is attached to the DOM
1067 * @return {boolean} The element is attached to the DOM
1069 OO
.ui
.Element
.prototype.isElementAttached = function () {
1070 return $.contains( this.getElementDocument(), this.$element
[0] );
1074 * Get the DOM document.
1076 * @return {HTMLDocument} Document object
1078 OO
.ui
.Element
.prototype.getElementDocument = function () {
1079 return OO
.ui
.Element
.getDocument( this.$element
);
1083 * Get the DOM window.
1085 * @return {Window} Window object
1087 OO
.ui
.Element
.prototype.getElementWindow = function () {
1088 return OO
.ui
.Element
.getWindow( this.$element
);
1092 * Get closest scrollable container.
1094 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1095 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1099 * Get group element is in.
1101 * @return {OO.ui.GroupElement|null} Group element, null if none
1103 OO
.ui
.Element
.prototype.getElementGroup = function () {
1104 return this.elementGroup
;
1108 * Set group element is in.
1110 * @param {OO.ui.GroupElement|null} group Group element, null if none
1113 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1114 this.elementGroup
= group
;
1119 * Scroll element into view.
1121 * @param {Object} [config={}]
1123 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1124 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1128 * Bind a handler for an event on this.$element
1130 * @deprecated Use jQuery#on instead.
1131 * @param {string} event
1132 * @param {Function} callback
1134 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1135 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1139 * Unbind a handler bound with #offDOMEvent
1141 * @deprecated Use jQuery#off instead.
1142 * @param {string} event
1143 * @param {Function} callback
1145 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1146 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1151 * Bind a handler for an event on a DOM element.
1153 * Used to be for working around a jQuery bug (jqbug.com/14180),
1154 * but obsolete as of jQuery 1.11.0.
1157 * @deprecated Use jQuery#on instead.
1158 * @param {HTMLElement|jQuery} el DOM element
1159 * @param {string} event Event to bind
1160 * @param {Function} callback Callback to call when the event fires
1162 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1163 $( el
).on( event
, callback
);
1167 * Unbind a handler bound with #static-method-onDOMEvent.
1169 * @deprecated Use jQuery#off instead.
1171 * @param {HTMLElement|jQuery} el DOM element
1172 * @param {string} event Event to unbind
1173 * @param {Function} [callback] Callback to unbind
1175 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1176 $( el
).off( event
, callback
);
1181 * Container for elements.
1185 * @extends OO.ui.Element
1186 * @mixins OO.EventEmitter
1189 * @param {Object} [config] Configuration options
1191 OO
.ui
.Layout
= function OoUiLayout( config
) {
1192 // Initialize config
1193 config
= config
|| {};
1195 // Parent constructor
1196 OO
.ui
.Layout
.super.call( this, config
);
1198 // Mixin constructors
1199 OO
.EventEmitter
.call( this );
1202 this.$element
.addClass( 'oo-ui-layout' );
1207 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1208 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1211 * User interface control.
1215 * @extends OO.ui.Element
1216 * @mixins OO.EventEmitter
1219 * @param {Object} [config] Configuration options
1220 * @cfg {boolean} [disabled=false] Disable
1222 OO
.ui
.Widget
= function OoUiWidget( config
) {
1223 // Initialize config
1224 config
= $.extend( { disabled
: false }, config
);
1226 // Parent constructor
1227 OO
.ui
.Widget
.super.call( this, config
);
1229 // Mixin constructors
1230 OO
.EventEmitter
.call( this );
1233 this.visible
= true;
1234 this.disabled
= null;
1235 this.wasDisabled
= null;
1238 this.$element
.addClass( 'oo-ui-widget' );
1239 this.setDisabled( !!config
.disabled
);
1244 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1245 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1251 * @param {boolean} disabled Widget is disabled
1256 * @param {boolean} visible Widget is visible
1262 * Check if the widget is disabled.
1264 * @param {boolean} Button is disabled
1266 OO
.ui
.Widget
.prototype.isDisabled = function () {
1267 return this.disabled
;
1271 * Check if widget is visible.
1273 * @return {boolean} Widget is visible
1275 OO
.ui
.Widget
.prototype.isVisible = function () {
1276 return this.visible
;
1280 * Set the disabled state of the widget.
1282 * This should probably change the widgets' appearance and prevent it from being used.
1284 * @param {boolean} disabled Disable widget
1287 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1290 this.disabled
= !!disabled
;
1291 isDisabled
= this.isDisabled();
1292 if ( isDisabled
!== this.wasDisabled
) {
1293 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1294 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1295 this.emit( 'disable', isDisabled
);
1297 this.wasDisabled
= isDisabled
;
1303 * Toggle visibility of widget.
1305 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1309 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1310 show
= show
=== undefined ? !this.visible
: !!show
;
1312 if ( show
!== this.isVisible() ) {
1313 this.visible
= show
;
1314 this.$element
.toggle( show
);
1315 this.emit( 'toggle', show
);
1322 * Update the disabled state, in case of changes in parent widget.
1326 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1327 this.setDisabled( this.disabled
);
1332 * Container for elements in a child frame.
1334 * Use together with OO.ui.WindowManager.
1338 * @extends OO.ui.Element
1339 * @mixins OO.EventEmitter
1341 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1342 * teardown processes are executed when the window is closed.
1344 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1345 * - Window manager begins opening window
1346 * - {@link #getSetupProcess} method is called and its result executed
1347 * - {@link #getReadyProcess} method is called and its result executed
1348 * - Window is now open
1350 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1351 * - Window manager begins closing window
1352 * - {@link #getHoldProcess} method is called and its result executed
1353 * - {@link #getTeardownProcess} method is called and its result executed
1354 * - Window is now closed
1356 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1357 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1358 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1359 * processing can complete. Always assume window processes are executed asychronously. See
1360 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1361 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1363 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1364 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1367 * @param {Object} [config] Configuration options
1368 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1372 OO
.ui
.Window
= function OoUiWindow( config
) {
1373 // Configuration initialization
1374 config
= config
|| {};
1376 // Parent constructor
1377 OO
.ui
.Window
.super.call( this, config
);
1379 // Mixin constructors
1380 OO
.EventEmitter
.call( this );
1383 this.manager
= null;
1384 this.initialized
= false;
1385 this.visible
= false;
1386 this.opening
= null;
1387 this.closing
= null;
1390 this.loading
= null;
1391 this.size
= config
.size
|| this.constructor.static.size
;
1392 this.$frame
= this.$( '<div>' );
1396 .addClass( 'oo-ui-window' )
1397 .append( this.$frame
);
1398 this.$frame
.addClass( 'oo-ui-window-frame' );
1400 // NOTE: Additional intitialization will occur when #setManager is called
1405 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1406 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1412 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1415 /* Static Properties */
1418 * Symbolic name of size.
1420 * Size is used if no size is configured during construction.
1424 * @property {string}
1426 OO
.ui
.Window
.static.size
= 'medium';
1428 /* Static Methods */
1431 * Transplant the CSS styles from as parent document to a frame's document.
1433 * This loops over the style sheets in the parent document, and copies their nodes to the
1434 * frame's document. It then polls the document to see when all styles have loaded, and once they
1435 * have, resolves the promise.
1437 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1438 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1439 * Firefox, where the styles won't load until the iframe becomes visible.
1441 * For details of how we arrived at the strategy used in this function, see #load.
1445 * @param {HTMLDocument} parentDoc Document to transplant styles from
1446 * @param {HTMLDocument} frameDoc Document to transplant styles to
1447 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1448 * @return {jQuery.Promise} Promise resolved when styles have loaded
1450 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1451 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1452 $pollNodes
= $( [] ),
1453 // Fake font-family value
1454 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1455 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1456 deferred
= $.Deferred();
1458 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1459 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1460 if ( styleNode
.disabled
) {
1464 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1465 // External stylesheet; use @import
1466 styleText
= '@import url(' + styleNode
.href
+ ');';
1468 // Internal stylesheet; just copy the text
1469 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1470 // other browsers, so fall back to '' rather than 'undefined'
1471 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1474 // Create a node with a unique ID that we're going to monitor to see when the CSS
1476 if ( styleNode
.oouiFrameTransplantStylesId
) {
1477 // If we're nesting transplantStyles operations and this node already has
1478 // a CSS rule to wait for loading, reuse it
1479 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1481 // Otherwise, create a new ID
1482 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1485 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1486 // The font-family rule will only take effect once the @import finishes
1487 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1490 // Create a node with id=pollNodeId
1491 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1492 .attr( 'id', pollNodeId
)
1493 .appendTo( frameDoc
.body
)
1496 // Add our modified CSS as a <style> tag
1497 newNode
= frameDoc
.createElement( 'style' );
1498 newNode
.textContent
= styleText
;
1499 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1500 frameDoc
.head
.appendChild( newNode
);
1502 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1504 // Poll every 100ms until all external stylesheets have loaded
1505 $pendingPollNodes
= $pollNodes
;
1506 timeoutID
= setTimeout( function pollExternalStylesheets() {
1508 $pendingPollNodes
.length
> 0 &&
1509 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1511 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1514 if ( $pendingPollNodes
.length
=== 0 ) {
1516 if ( timeoutID
!== null ) {
1518 $pollNodes
.remove();
1522 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1525 // ...but give up after a while
1526 if ( timeout
!== 0 ) {
1527 setTimeout( function () {
1529 clearTimeout( timeoutID
);
1531 $pollNodes
.remove();
1534 }, timeout
|| 5000 );
1537 return deferred
.promise();
1543 * Handle mouse down events.
1545 * @param {jQuery.Event} e Mouse down event
1547 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1548 // Prevent clicking on the click-block from stealing focus
1549 if ( e
.target
=== this.$element
[0] ) {
1555 * Check if window has been initialized.
1557 * @return {boolean} Window has been initialized
1559 OO
.ui
.Window
.prototype.isInitialized = function () {
1560 return this.initialized
;
1564 * Check if window is visible.
1566 * @return {boolean} Window is visible
1568 OO
.ui
.Window
.prototype.isVisible = function () {
1569 return this.visible
;
1573 * Check if window is loading.
1575 * @return {boolean} Window is loading
1577 OO
.ui
.Window
.prototype.isLoading = function () {
1578 return this.loading
&& this.loading
.state() === 'pending';
1582 * Check if window is loaded.
1584 * @return {boolean} Window is loaded
1586 OO
.ui
.Window
.prototype.isLoaded = function () {
1587 return this.loading
&& this.loading
.state() === 'resolved';
1591 * Check if window is opening.
1593 * This is a wrapper around OO.ui.WindowManager#isOpening.
1595 * @return {boolean} Window is opening
1597 OO
.ui
.Window
.prototype.isOpening = function () {
1598 return this.manager
.isOpening( this );
1602 * Check if window is closing.
1604 * This is a wrapper around OO.ui.WindowManager#isClosing.
1606 * @return {boolean} Window is closing
1608 OO
.ui
.Window
.prototype.isClosing = function () {
1609 return this.manager
.isClosing( this );
1613 * Check if window is opened.
1615 * This is a wrapper around OO.ui.WindowManager#isOpened.
1617 * @return {boolean} Window is opened
1619 OO
.ui
.Window
.prototype.isOpened = function () {
1620 return this.manager
.isOpened( this );
1624 * Get the window manager.
1626 * @return {OO.ui.WindowManager} Manager of window
1628 OO
.ui
.Window
.prototype.getManager = function () {
1629 return this.manager
;
1633 * Get the window size.
1635 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1637 OO
.ui
.Window
.prototype.getSize = function () {
1642 * Get the height of the dialog contents.
1644 * @return {number} Content height
1646 OO
.ui
.Window
.prototype.getContentHeight = function () {
1647 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements
1648 var bodyHeight
, oldHeight
= this.$frame
[0].style
.height
;
1649 this.$frame
[0].style
.height
= '1px';
1650 bodyHeight
= this.getBodyHeight();
1651 this.$frame
[0].style
.height
= oldHeight
;
1654 // Add buffer for border
1655 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1656 // Use combined heights of children
1657 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1662 * Get the height of the dialog contents.
1664 * When this function is called, the dialog will temporarily have been resized
1665 * to height=1px, so .scrollHeight measurements can be taken accurately.
1667 * @return {number} Height of content
1669 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1670 return this.$body
[0].scrollHeight
;
1674 * Get the directionality of the frame
1676 * @return {string} Directionality, 'ltr' or 'rtl'
1678 OO
.ui
.Window
.prototype.getDir = function () {
1683 * Get a process for setting up a window for use.
1685 * Each time the window is opened this process will set it up for use in a particular context, based
1686 * on the `data` argument.
1688 * When you override this method, you can add additional setup steps to the process the parent
1689 * method provides using the 'first' and 'next' methods.
1692 * @param {Object} [data] Window opening data
1693 * @return {OO.ui.Process} Setup process
1695 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1696 return new OO
.ui
.Process();
1700 * Get a process for readying a window for use.
1702 * Each time the window is open and setup, this process will ready it up for use in a particular
1703 * context, based on the `data` argument.
1705 * When you override this method, you can add additional setup steps to the process the parent
1706 * method provides using the 'first' and 'next' methods.
1709 * @param {Object} [data] Window opening data
1710 * @return {OO.ui.Process} Setup process
1712 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1713 return new OO
.ui
.Process();
1717 * Get a process for holding a window from use.
1719 * Each time the window is closed, this process will hold it from use in a particular context, based
1720 * on the `data` argument.
1722 * When you override this method, you can add additional setup steps to the process the parent
1723 * method provides using the 'first' and 'next' methods.
1726 * @param {Object} [data] Window closing data
1727 * @return {OO.ui.Process} Hold process
1729 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1730 return new OO
.ui
.Process();
1734 * Get a process for tearing down a window after use.
1736 * Each time the window is closed this process will tear it down and do something with the user's
1737 * interactions within the window, based on the `data` argument.
1739 * When you override this method, you can add additional teardown steps to the process the parent
1740 * method provides using the 'first' and 'next' methods.
1743 * @param {Object} [data] Window closing data
1744 * @return {OO.ui.Process} Teardown process
1746 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1747 return new OO
.ui
.Process();
1751 * Toggle visibility of window.
1753 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1754 * instead of display.
1756 * @param {boolean} [show] Make window visible, omit to toggle visibility
1760 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1761 show
= show
=== undefined ? !this.visible
: !!show
;
1763 if ( show
!== this.isVisible() ) {
1764 this.visible
= show
;
1766 if ( this.isolated
&& !this.isLoaded() ) {
1767 // Hide the window using visibility instead of display until loading is complete
1768 // Can't use display: none; because that prevents the iframe from loading in Firefox
1769 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1771 this.$element
.toggle( show
).css( 'visibility', '' );
1773 this.emit( 'toggle', show
);
1780 * Set the window manager.
1782 * This must be called before initialize. Calling it more than once will cause an error.
1784 * @param {OO.ui.WindowManager} manager Manager for this window
1785 * @throws {Error} If called more than once
1788 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1789 if ( this.manager
) {
1790 throw new Error( 'Cannot set window manager, window already has a manager' );
1794 this.manager
= manager
;
1795 this.isolated
= manager
.shouldIsolate();
1798 if ( this.isolated
) {
1799 this.$iframe
= this.$( '<iframe>' );
1800 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1801 this.$frame
.append( this.$iframe
);
1802 this.$ = function () {
1803 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1805 // WARNING: Do not use this.$ again until #initialize is called
1807 this.$content
= this.$( '<div>' );
1808 this.$document
= $( this.getElementDocument() );
1809 this.$content
.addClass( 'oo-ui-window-content' );
1810 this.$frame
.append( this.$content
);
1812 this.toggle( false );
1814 // Figure out directionality:
1815 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1821 * Set the window size.
1823 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1826 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1828 this.manager
.updateWindowSize( this );
1833 * Set window dimensions.
1835 * Properties are applied to the frame container.
1837 * @param {Object} dim CSS dimension properties
1838 * @param {string|number} [dim.width] Width
1839 * @param {string|number} [dim.minWidth] Minimum width
1840 * @param {string|number} [dim.maxWidth] Maximum width
1841 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1842 * @param {string|number} [dim.minWidth] Minimum height
1843 * @param {string|number} [dim.maxWidth] Maximum height
1846 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1847 // Apply width before height so height is not based on wrapping content using the wrong width
1849 width
: dim
.width
|| '',
1850 minWidth
: dim
.minWidth
|| '',
1851 maxWidth
: dim
.maxWidth
|| ''
1854 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1855 minHeight
: dim
.minHeight
|| '',
1856 maxHeight
: dim
.maxHeight
|| ''
1862 * Initialize window contents.
1864 * The first time the window is opened, #initialize is called when it's safe to begin populating
1865 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1867 * Once this method is called, this.$ can be used to create elements within the frame.
1869 * @throws {Error} If not attached to a manager
1872 OO
.ui
.Window
.prototype.initialize = function () {
1873 if ( !this.manager
) {
1874 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1878 this.$head
= this.$( '<div>' );
1879 this.$body
= this.$( '<div>' );
1880 this.$foot
= this.$( '<div>' );
1881 this.$overlay
= this.$( '<div>' );
1884 this.$element
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
1887 this.$head
.addClass( 'oo-ui-window-head' );
1888 this.$body
.addClass( 'oo-ui-window-body' );
1889 this.$foot
.addClass( 'oo-ui-window-foot' );
1890 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1891 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$overlay
);
1899 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1900 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1902 * @param {Object} [data] Window opening data
1903 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1904 * first argument will be a promise which will be resolved when the window begins closing
1906 OO
.ui
.Window
.prototype.open = function ( data
) {
1907 return this.manager
.openWindow( this, data
);
1913 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1914 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1916 * @param {Object} [data] Window closing data
1917 * @return {jQuery.Promise} Promise resolved when window is closed
1919 OO
.ui
.Window
.prototype.close = function ( data
) {
1920 return this.manager
.closeWindow( this, data
);
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 setup
1932 OO
.ui
.Window
.prototype.setup = function ( data
) {
1934 deferred
= $.Deferred();
1936 this.$element
.show();
1937 this.visible
= true;
1938 this.getSetupProcess( data
).execute().done( function () {
1939 // Force redraw by asking the browser to measure the elements' widths
1940 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
1941 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
1945 return deferred
.promise();
1951 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1954 * @param {Object} [data] Window opening data
1955 * @return {jQuery.Promise} Promise resolved when window is ready
1957 OO
.ui
.Window
.prototype.ready = function ( data
) {
1959 deferred
= $.Deferred();
1961 this.$content
.focus();
1962 this.getReadyProcess( data
).execute().done( function () {
1963 // Force redraw by asking the browser to measure the elements' widths
1964 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
1965 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
1969 return deferred
.promise();
1975 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1978 * @param {Object} [data] Window closing data
1979 * @return {jQuery.Promise} Promise resolved when window is held
1981 OO
.ui
.Window
.prototype.hold = function ( data
) {
1983 deferred
= $.Deferred();
1985 this.getHoldProcess( data
).execute().done( function () {
1986 // Get the focused element within the window's content
1987 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
1989 // Blur the focused element
1990 if ( $focus
.length
) {
1994 // Force redraw by asking the browser to measure the elements' widths
1995 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
1996 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2000 return deferred
.promise();
2006 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2009 * @param {Object} [data] Window closing data
2010 * @return {jQuery.Promise} Promise resolved when window is torn down
2012 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2014 deferred
= $.Deferred();
2016 this.getTeardownProcess( data
).execute().done( function () {
2017 // Force redraw by asking the browser to measure the elements' widths
2018 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
2019 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2020 win
.$element
.hide();
2021 win
.visible
= false;
2025 return deferred
.promise();
2029 * Load the frame contents.
2031 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
2032 * promise will be resolved. Calling while loading will return a promise but not trigger a new
2033 * loading cycle. Calling after loading is complete will return a promise that's already been
2036 * Sounds simple right? Read on...
2038 * When you create a dynamic iframe using open/write/close, the window.load event for the
2039 * iframe is triggered when you call close, and there's no further load event to indicate that
2040 * everything is actually loaded.
2042 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2043 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2044 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2045 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2046 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2048 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2049 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2050 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2051 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2052 * accessing .cssRules is allowed.
2054 * However, now that we control the styles we're injecting, we might as well do away with
2055 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2056 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2057 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2058 * font-family rule is not applied until after the `@import` finishes.
2060 * All this stylesheet injection and polling magic is in #transplantStyles.
2062 * @return {jQuery.Promise} Promise resolved when loading is complete
2065 OO
.ui
.Window
.prototype.load = function () {
2066 var sub
, doc
, loading
,
2069 // Non-isolated windows are already "loaded"
2070 if ( !this.loading
&& !this.isolated
) {
2071 this.loading
= $.Deferred().resolve();
2073 // Set initialized state after so sub-classes aren't confused by it being set by calling
2074 // their parent initialize method
2075 this.initialized
= true;
2078 // Return existing promise if already loading or loaded
2079 if ( this.loading
) {
2080 return this.loading
.promise();
2084 loading
= this.loading
= $.Deferred();
2085 sub
= this.$iframe
.prop( 'contentWindow' );
2088 // Initialize contents
2093 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2094 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2095 '<div class="oo-ui-window-content"></div>' +
2102 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$element
);
2103 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2104 this.$document
= this.$( doc
);
2107 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2108 .always( function () {
2109 // Initialize isolated windows
2111 // Set initialized state after so sub-classes aren't confused by it being set by calling
2112 // their parent initialize method
2113 win
.initialized
= true;
2114 // Undo the visibility: hidden; hack and apply display: none;
2115 // We can do this safely now that the iframe has initialized
2116 // (don't do this from within #initialize because it has to happen
2117 // after the all subclasses have been handled as well).
2118 win
.toggle( win
.isVisible() );
2123 return loading
.promise();
2127 * Base class for all dialogs.
2130 * - Manage the window (open and close, etc.).
2131 * - Store the internal name and display title.
2132 * - A stack to track one or more pending actions.
2133 * - Manage a set of actions that can be performed.
2134 * - Configure and create action widgets.
2137 * - Close the dialog with Escape key.
2138 * - Visually lock the dialog while an action is in
2139 * progress (aka "pending").
2141 * Subclass responsibilities:
2142 * - Display the title somewhere.
2143 * - Add content to the dialog.
2144 * - Provide a UI to close the dialog.
2145 * - Display the action widgets somewhere.
2149 * @extends OO.ui.Window
2150 * @mixins OO.ui.PendingElement
2153 * @param {Object} [config] Configuration options
2155 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2156 // Parent constructor
2157 OO
.ui
.Dialog
.super.call( this, config
);
2159 // Mixin constructors
2160 OO
.ui
.PendingElement
.call( this );
2163 this.actions
= new OO
.ui
.ActionSet();
2164 this.attachedActions
= [];
2165 this.currentAction
= null;
2168 this.actions
.connect( this, {
2169 click
: 'onActionClick',
2170 resize
: 'onActionResize',
2171 change
: 'onActionsChange'
2176 .addClass( 'oo-ui-dialog' )
2177 .attr( 'role', 'dialog' );
2182 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2183 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2185 /* Static Properties */
2188 * Symbolic name of dialog.
2193 * @property {string}
2195 OO
.ui
.Dialog
.static.name
= '';
2203 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2205 OO
.ui
.Dialog
.static.title
= '';
2208 * List of OO.ui.ActionWidget configuration options.
2212 * @property {Object[]}
2214 OO
.ui
.Dialog
.static.actions
= [];
2217 * Close dialog when the escape key is pressed.
2222 * @property {boolean}
2224 OO
.ui
.Dialog
.static.escapable
= true;
2229 * Handle frame document key down events.
2231 * @param {jQuery.Event} e Key down event
2233 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2234 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2241 * Handle action resized events.
2243 * @param {OO.ui.ActionWidget} action Action that was resized
2245 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2246 // Override in subclass
2250 * Handle action click events.
2252 * @param {OO.ui.ActionWidget} action Action that was clicked
2254 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2255 if ( !this.isPending() ) {
2256 this.currentAction
= action
;
2257 this.executeAction( action
.getAction() );
2262 * Handle actions change event.
2264 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2265 this.detachActions();
2266 if ( !this.isClosing() ) {
2267 this.attachActions();
2272 * Get set of actions.
2274 * @return {OO.ui.ActionSet}
2276 OO
.ui
.Dialog
.prototype.getActions = function () {
2277 return this.actions
;
2281 * Get a process for taking action.
2283 * When you override this method, you can add additional accept steps to the process the parent
2284 * method provides using the 'first' and 'next' methods.
2287 * @param {string} [action] Symbolic name of action
2288 * @return {OO.ui.Process} Action process
2290 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2291 return new OO
.ui
.Process()
2292 .next( function () {
2294 // An empty action always closes the dialog without data, which should always be
2295 // safe and make no changes
2304 * @param {Object} [data] Dialog opening data
2305 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2306 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2307 * action item, omit to use #static-actions
2309 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2313 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2314 .next( function () {
2317 config
= this.constructor.static,
2318 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2320 this.title
.setLabel(
2321 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2323 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2325 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2328 this.actions
.add( items
);
2335 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2337 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2338 .first( function () {
2339 this.actions
.clear();
2340 this.currentAction
= null;
2347 OO
.ui
.Dialog
.prototype.initialize = function () {
2349 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2352 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2355 if ( this.constructor.static.escapable
) {
2356 this.$document
.on( 'keydown', OO
.ui
.bind( this.onDocumentKeyDown
, this ) );
2360 this.$content
.addClass( 'oo-ui-dialog-content' );
2361 this.setPendingElement( this.$head
);
2365 * Attach action actions.
2367 OO
.ui
.Dialog
.prototype.attachActions = function () {
2368 // Remember the list of potentially attached actions
2369 this.attachedActions
= this.actions
.get();
2373 * Detach action actions.
2377 OO
.ui
.Dialog
.prototype.detachActions = function () {
2380 // Detach all actions that may have been previously attached
2381 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2382 this.attachedActions
[i
].$element
.detach();
2384 this.attachedActions
= [];
2388 * Execute an action.
2390 * @param {string} action Symbolic name of action to execute
2391 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2393 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2395 return this.getActionProcess( action
).execute()
2396 .always( OO
.ui
.bind( this.popPending
, this ) );
2400 * Collection of windows.
2403 * @extends OO.ui.Element
2404 * @mixins OO.EventEmitter
2406 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2407 * already opening or opened, the current window will be closed without data. Empty closing data
2408 * should always result in the window being closed without causing constructive or destructive
2411 * As a window is opened and closed, it passes through several stages and the manager emits several
2412 * corresponding events.
2414 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2415 * - {@link #event-opening} is emitted with `opening` promise
2416 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2417 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2418 * - `setup` progress notification is emitted from opening promise
2419 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2420 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2421 * - `ready` progress notification is emitted from opening promise
2422 * - `opening` promise is resolved with `opened` promise
2423 * - Window is now open
2425 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2426 * - `opened` promise is resolved with `closing` promise
2427 * - {@link #event-closing} is emitted with `closing` promise
2428 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2429 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2430 * - `hold` progress notification is emitted from opening promise
2431 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2432 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2433 * - `teardown` progress notification is emitted from opening promise
2434 * - Closing promise is resolved
2435 * - Window is now closed
2438 * @param {Object} [config] Configuration options
2439 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2440 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2441 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2443 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2444 // Configuration initialization
2445 config
= config
|| {};
2447 // Parent constructor
2448 OO
.ui
.WindowManager
.super.call( this, config
);
2450 // Mixin constructors
2451 OO
.EventEmitter
.call( this );
2454 this.factory
= config
.factory
;
2455 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2456 this.isolate
= !!config
.isolate
;
2458 this.opening
= null;
2460 this.closing
= null;
2461 this.preparingToOpen
= null;
2462 this.preparingToClose
= null;
2464 this.currentWindow
= null;
2465 this.$ariaHidden
= null;
2466 this.requestedSize
= null;
2467 this.onWindowResizeTimeout
= null;
2468 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
2469 this.afterWindowResizeHandler
= OO
.ui
.bind( this.afterWindowResize
, this );
2470 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
2471 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
2475 .addClass( 'oo-ui-windowManager' )
2476 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2481 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2482 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2487 * Window is opening.
2489 * Fired when the window begins to be opened.
2492 * @param {OO.ui.Window} win Window that's being opened
2493 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2494 * resolved the first argument will be a promise which will be resolved when the window begins
2495 * closing, the second argument will be the opening data; progress notifications will be fired on
2496 * the promise for `setup` and `ready` when those processes are completed respectively.
2497 * @param {Object} data Window opening data
2501 * Window is closing.
2503 * Fired when the window begins to be closed.
2506 * @param {OO.ui.Window} win Window that's being closed
2507 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2508 * is resolved the first argument will be a the closing data; progress notifications will be fired
2509 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2510 * @param {Object} data Window closing data
2513 /* Static Properties */
2516 * Map of symbolic size names and CSS properties.
2520 * @property {Object}
2522 OO
.ui
.WindowManager
.static.sizes
= {
2533 // These can be non-numeric because they are never used in calculations
2540 * Symbolic name of default size.
2542 * Default size is used if the window's requested size is not recognized.
2546 * @property {string}
2548 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2553 * Handle window resize events.
2555 * @param {jQuery.Event} e Window resize event
2557 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2558 clearTimeout( this.onWindowResizeTimeout
);
2559 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2563 * Handle window resize events.
2565 * @param {jQuery.Event} e Window resize event
2567 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2568 if ( this.currentWindow
) {
2569 this.updateWindowSize( this.currentWindow
);
2574 * Handle window mouse wheel events.
2576 * @param {jQuery.Event} e Mouse wheel event
2578 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2583 * Handle document key down events.
2585 * @param {jQuery.Event} e Key down event
2587 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2588 switch ( e
.which
) {
2589 case OO
.ui
.Keys
.PAGEUP
:
2590 case OO
.ui
.Keys
.PAGEDOWN
:
2591 case OO
.ui
.Keys
.END
:
2592 case OO
.ui
.Keys
.HOME
:
2593 case OO
.ui
.Keys
.LEFT
:
2595 case OO
.ui
.Keys
.RIGHT
:
2596 case OO
.ui
.Keys
.DOWN
:
2597 // Prevent any key events that might cause scrolling
2603 * Check if window is opening.
2605 * @return {boolean} Window is opening
2607 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2608 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2612 * Check if window is closing.
2614 * @return {boolean} Window is closing
2616 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2617 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2621 * Check if window is opened.
2623 * @return {boolean} Window is opened
2625 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2626 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2630 * Check if window contents should be isolated.
2632 * Window content isolation is done using inline frames.
2634 * @return {boolean} Window contents should be isolated
2636 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2637 return this.isolate
;
2641 * Check if a window is being managed.
2643 * @param {OO.ui.Window} win Window to check
2644 * @return {boolean} Window is being managed
2646 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2649 for ( name
in this.windows
) {
2650 if ( this.windows
[name
] === win
) {
2659 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2661 * @param {OO.ui.Window} win Window being opened
2662 * @param {Object} [data] Window opening data
2663 * @return {number} Milliseconds to wait
2665 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2670 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2672 * @param {OO.ui.Window} win Window being opened
2673 * @param {Object} [data] Window opening data
2674 * @return {number} Milliseconds to wait
2676 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2681 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2683 * @param {OO.ui.Window} win Window being closed
2684 * @param {Object} [data] Window closing data
2685 * @return {number} Milliseconds to wait
2687 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2692 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2694 * @param {OO.ui.Window} win Window being closed
2695 * @param {Object} [data] Window closing data
2696 * @return {number} Milliseconds to wait
2698 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2699 return this.modal
? 250 : 0;
2703 * Get managed window by symbolic name.
2705 * If window is not yet instantiated, it will be instantiated and added automatically.
2707 * @param {string} name Symbolic window name
2708 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2709 * @throws {Error} If the symbolic name is unrecognized by the factory
2710 * @throws {Error} If the symbolic name unrecognized as a managed window
2712 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2713 var deferred
= $.Deferred(),
2714 win
= this.windows
[name
];
2716 if ( !( win
instanceof OO
.ui
.Window
) ) {
2717 if ( this.factory
) {
2718 if ( !this.factory
.lookup( name
) ) {
2719 deferred
.reject( new OO
.ui
.Error(
2720 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2723 win
= this.factory
.create( name
, this, { $: this.$ } );
2724 this.addWindows( [ win
] );
2725 deferred
.resolve( win
);
2728 deferred
.reject( new OO
.ui
.Error(
2729 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2733 deferred
.resolve( win
);
2736 return deferred
.promise();
2740 * Get current window.
2742 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2744 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2745 return this.currentWindow
;
2751 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2752 * @param {Object} [data] Window opening data
2753 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2754 * for more details about the `opening` promise
2757 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2760 opening
= $.Deferred();
2762 // Argument handling
2763 if ( typeof win
=== 'string' ) {
2764 return this.getWindow( win
).then( function ( win
) {
2765 return manager
.openWindow( win
, data
);
2770 if ( !this.hasWindow( win
) ) {
2771 opening
.reject( new OO
.ui
.Error(
2772 'Cannot open window: window is not attached to manager'
2774 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2775 opening
.reject( new OO
.ui
.Error(
2776 'Cannot open window: another window is opening or open'
2781 if ( opening
.state() !== 'rejected' ) {
2782 // Begin loading the window if it's not loading or loaded already - may take noticable time
2783 // and we want to do this in paralell with any other preparatory actions
2784 if ( !win
.isLoading() && !win
.isLoaded() ) {
2785 // Finish initializing the window (must be done after manager is attached to DOM)
2786 win
.setManager( this );
2787 preparing
.push( win
.load() );
2790 if ( this.closing
) {
2791 // If a window is currently closing, wait for it to complete
2792 preparing
.push( this.closing
);
2795 this.preparingToOpen
= $.when
.apply( $, preparing
);
2796 // Ensure handlers get called after preparingToOpen is set
2797 this.preparingToOpen
.done( function () {
2798 if ( manager
.modal
) {
2799 manager
.toggleGlobalEvents( true );
2800 manager
.toggleAriaIsolation( true );
2802 manager
.currentWindow
= win
;
2803 manager
.opening
= opening
;
2804 manager
.preparingToOpen
= null;
2805 manager
.emit( 'opening', win
, opening
, data
);
2806 setTimeout( function () {
2807 win
.setup( data
).then( function () {
2808 manager
.updateWindowSize( win
);
2809 manager
.opening
.notify( { state
: 'setup' } );
2810 setTimeout( function () {
2811 win
.ready( data
).then( function () {
2812 manager
.opening
.notify( { state
: 'ready' } );
2813 manager
.opening
= null;
2814 manager
.opened
= $.Deferred();
2815 opening
.resolve( manager
.opened
.promise(), data
);
2817 }, manager
.getReadyDelay() );
2819 }, manager
.getSetupDelay() );
2823 return opening
.promise();
2829 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2830 * @param {Object} [data] Window closing data
2831 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2832 * for more details about the `closing` promise
2833 * @throws {Error} If no window by that name is being managed
2836 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2839 closing
= $.Deferred(),
2840 opened
= this.opened
;
2842 // Argument handling
2843 if ( typeof win
=== 'string' ) {
2844 win
= this.windows
[win
];
2845 } else if ( !this.hasWindow( win
) ) {
2851 closing
.reject( new OO
.ui
.Error(
2852 'Cannot close window: window is not attached to manager'
2854 } else if ( win
!== this.currentWindow
) {
2855 closing
.reject( new OO
.ui
.Error(
2856 'Cannot close window: window already closed with different data'
2858 } else if ( this.preparingToClose
|| this.closing
) {
2859 closing
.reject( new OO
.ui
.Error(
2860 'Cannot close window: window already closing with different data'
2865 if ( closing
.state() !== 'rejected' ) {
2866 if ( this.opening
) {
2867 // If the window is currently opening, close it when it's done
2868 preparing
.push( this.opening
);
2871 this.preparingToClose
= $.when
.apply( $, preparing
);
2872 // Ensure handlers get called after preparingToClose is set
2873 this.preparingToClose
.done( function () {
2874 manager
.closing
= closing
;
2875 manager
.preparingToClose
= null;
2876 manager
.emit( 'closing', win
, closing
, data
);
2877 manager
.opened
= null;
2878 opened
.resolve( closing
.promise(), data
);
2879 setTimeout( function () {
2880 win
.hold( data
).then( function () {
2881 closing
.notify( { state
: 'hold' } );
2882 setTimeout( function () {
2883 win
.teardown( data
).then( function () {
2884 closing
.notify( { state
: 'teardown' } );
2885 if ( manager
.modal
) {
2886 manager
.toggleGlobalEvents( false );
2887 manager
.toggleAriaIsolation( false );
2889 manager
.closing
= null;
2890 manager
.currentWindow
= null;
2891 closing
.resolve( data
);
2893 }, manager
.getTeardownDelay() );
2895 }, manager
.getHoldDelay() );
2899 return closing
.promise();
2905 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2906 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2907 * a statically configured symbolic name
2909 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2910 var i
, len
, win
, name
, list
;
2912 if ( $.isArray( windows
) ) {
2913 // Convert to map of windows by looking up symbolic names from static configuration
2915 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2916 name
= windows
[i
].constructor.static.name
;
2917 if ( typeof name
!== 'string' ) {
2918 throw new Error( 'Cannot add window' );
2920 list
[name
] = windows
[i
];
2922 } else if ( $.isPlainObject( windows
) ) {
2927 for ( name
in list
) {
2929 this.windows
[name
] = win
;
2930 this.$element
.append( win
.$element
);
2937 * Windows will be closed before they are removed.
2939 * @param {string} name Symbolic name of window to remove
2940 * @return {jQuery.Promise} Promise resolved when window is closed and removed
2941 * @throws {Error} If windows being removed are not being managed
2943 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
2944 var i
, len
, win
, name
,
2947 cleanup = function ( name
, win
) {
2948 delete manager
.windows
[name
];
2949 win
.$element
.detach();
2952 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
2954 win
= this.windows
[name
];
2956 throw new Error( 'Cannot remove window' );
2958 promises
.push( this.closeWindow( name
).then( OO
.ui
.bind( cleanup
, null, name
, win
) ) );
2961 return $.when
.apply( $, promises
);
2965 * Remove all windows.
2967 * Windows will be closed before they are removed.
2969 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
2971 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
2972 return this.removeWindows( Object
.keys( this.windows
) );
2978 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
2982 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
2983 // Bypass for non-current, and thus invisible, windows
2984 if ( win
!== this.currentWindow
) {
2988 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
2989 sizes
= this.constructor.static.sizes
,
2990 size
= win
.getSize();
2992 if ( !sizes
[size
] ) {
2993 size
= this.constructor.static.defaultSize
;
2995 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
2999 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3000 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3001 win
.setDimensions( sizes
[size
] );
3007 * Bind or unbind global events for scrolling.
3009 * @param {boolean} [on] Bind global events
3012 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3013 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3016 if ( !this.globalEvents
) {
3017 this.$( this.getElementDocument() ).on( {
3018 // Prevent scrolling by keys in top-level window
3019 keydown
: this.onDocumentKeyDownHandler
3021 this.$( this.getElementWindow() ).on( {
3022 // Prevent scrolling by wheel in top-level window
3023 mousewheel
: this.onWindowMouseWheelHandler
,
3024 // Start listening for top-level window dimension changes
3025 'orientationchange resize': this.onWindowResizeHandler
3027 this.globalEvents
= true;
3029 } else if ( this.globalEvents
) {
3030 // Unbind global events
3031 this.$( this.getElementDocument() ).off( {
3032 // Allow scrolling by keys in top-level window
3033 keydown
: this.onDocumentKeyDownHandler
3035 this.$( this.getElementWindow() ).off( {
3036 // Allow scrolling by wheel in top-level window
3037 mousewheel
: this.onWindowMouseWheelHandler
,
3038 // Stop listening for top-level window dimension changes
3039 'orientationchange resize': this.onWindowResizeHandler
3041 this.globalEvents
= false;
3048 * Toggle screen reader visibility of content other than the window manager.
3050 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3053 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3054 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3057 if ( !this.$ariaHidden
) {
3058 // Hide everything other than the window manager from screen readers
3059 this.$ariaHidden
= $( 'body' )
3061 .not( this.$element
.parentsUntil( 'body' ).last() )
3062 .attr( 'aria-hidden', '' );
3064 } else if ( this.$ariaHidden
) {
3065 // Restore screen reader visiblity
3066 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3067 this.$ariaHidden
= null;
3074 * Destroy window manager.
3076 * Windows will not be closed, only removed from the DOM.
3078 OO
.ui
.WindowManager
.prototype.destroy = function () {
3079 this.toggleGlobalEvents( false );
3080 this.toggleAriaIsolation( false );
3081 this.$element
.remove();
3089 * @param {string|jQuery} message Description of error
3090 * @param {Object} [config] Configuration options
3091 * @cfg {boolean} [recoverable=true] Error is recoverable
3093 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3094 // Configuration initialization
3095 config
= config
|| {};
3098 this.message
= message
instanceof jQuery
? message
: String( message
);
3099 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3104 OO
.initClass( OO
.ui
.Error
);
3109 * Check if error can be recovered from.
3111 * @return {boolean} Error is recoverable
3113 OO
.ui
.Error
.prototype.isRecoverable = function () {
3114 return this.recoverable
;
3118 * Get error message as DOM nodes.
3120 * @return {jQuery} Error message in DOM nodes
3122 OO
.ui
.Error
.prototype.getMessage = function () {
3123 return this.message
instanceof jQuery
?
3124 this.message
.clone() :
3125 $( '<div>' ).text( this.message
).contents();
3129 * Get error message as text.
3131 * @return {string} Error message
3133 OO
.ui
.Error
.prototype.getMessageText = function () {
3134 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3138 * A list of functions, called in sequence.
3140 * If a function added to a process returns boolean false the process will stop; if it returns an
3141 * object with a `promise` method the process will use the promise to either continue to the next
3142 * step when the promise is resolved or stop when the promise is rejected.
3147 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3148 * call, see #createStep for more information
3149 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3151 * @return {Object} Step object, with `callback` and `context` properties
3153 OO
.ui
.Process = function ( step
, context
) {
3158 if ( step
!== undefined ) {
3159 this.next( step
, context
);
3165 OO
.initClass( OO
.ui
.Process
);
3170 * Start the process.
3172 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3173 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3174 * process, the remaining steps will not be taken
3176 OO
.ui
.Process
.prototype.execute = function () {
3177 var i
, len
, promise
;
3180 * Continue execution.
3183 * @param {Array} step A function and the context it should be called in
3184 * @return {Function} Function that continues the process
3186 function proceed( step
) {
3187 return function () {
3188 // Execute step in the correct context
3190 result
= step
.callback
.call( step
.context
);
3192 if ( result
=== false ) {
3193 // Use rejected promise for boolean false results
3194 return $.Deferred().reject( [] ).promise();
3196 if ( typeof result
=== 'number' ) {
3198 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3200 // Use a delayed promise for numbers, expecting them to be in milliseconds
3201 deferred
= $.Deferred();
3202 setTimeout( deferred
.resolve
, result
);
3203 return deferred
.promise();
3205 if ( result
instanceof OO
.ui
.Error
) {
3206 // Use rejected promise for error
3207 return $.Deferred().reject( [ result
] ).promise();
3209 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3210 // Use rejected promise for list of errors
3211 return $.Deferred().reject( result
).promise();
3213 // Duck-type the object to see if it can produce a promise
3214 if ( result
&& $.isFunction( result
.promise
) ) {
3215 // Use a promise generated from the result
3216 return result
.promise();
3218 // Use resolved promise for other results
3219 return $.Deferred().resolve().promise();
3223 if ( this.steps
.length
) {
3224 // Generate a chain reaction of promises
3225 promise
= proceed( this.steps
[0] )();
3226 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3227 promise
= promise
.then( proceed( this.steps
[i
] ) );
3230 promise
= $.Deferred().resolve().promise();
3237 * Create a process step.
3240 * @param {number|jQuery.Promise|Function} step
3242 * - Number of milliseconds to wait; or
3243 * - Promise to wait to be resolved; or
3244 * - Function to execute
3245 * - If it returns boolean false the process will stop
3246 * - If it returns an object with a `promise` method the process will use the promise to either
3247 * continue to the next step when the promise is resolved or stop when the promise is rejected
3248 * - If it returns a number, the process will wait for that number of milliseconds before
3250 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3252 * @return {Object} Step object, with `callback` and `context` properties
3254 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3255 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3257 callback: function () {
3263 if ( $.isFunction( step
) ) {
3269 throw new Error( 'Cannot create process step: number, promise or function expected' );
3273 * Add step to the beginning of the process.
3275 * @inheritdoc #createStep
3276 * @return {OO.ui.Process} this
3279 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3280 this.steps
.unshift( this.createStep( step
, context
) );
3285 * Add step to the end of the process.
3287 * @inheritdoc #createStep
3288 * @return {OO.ui.Process} this
3291 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3292 this.steps
.push( this.createStep( step
, context
) );
3297 * Factory for tools.
3300 * @extends OO.Factory
3303 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3304 // Parent constructor
3305 OO
.ui
.ToolFactory
.super.call( this );
3310 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3315 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3316 var i
, len
, included
, promoted
, demoted
,
3320 // Collect included and not excluded tools
3321 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3324 promoted
= this.extract( promote
, used
);
3325 demoted
= this.extract( demote
, used
);
3328 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3329 if ( !used
[included
[i
]] ) {
3330 auto
.push( included
[i
] );
3334 return promoted
.concat( auto
).concat( demoted
);
3338 * Get a flat list of names from a list of names or groups.
3340 * Tools can be specified in the following ways:
3342 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3343 * - All tools in a group: `{ group: 'group-name' }`
3344 * - All tools: `'*'`
3347 * @param {Array|string} collection List of tools
3348 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3349 * names will be added as properties
3350 * @return {string[]} List of extracted names
3352 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3353 var i
, len
, item
, name
, tool
,
3356 if ( collection
=== '*' ) {
3357 for ( name
in this.registry
) {
3358 tool
= this.registry
[name
];
3360 // Only add tools by group name when auto-add is enabled
3361 tool
.static.autoAddToCatchall
&&
3362 // Exclude already used tools
3363 ( !used
|| !used
[name
] )
3371 } else if ( $.isArray( collection
) ) {
3372 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3373 item
= collection
[i
];
3374 // Allow plain strings as shorthand for named tools
3375 if ( typeof item
=== 'string' ) {
3376 item
= { name
: item
};
3378 if ( OO
.isPlainObject( item
) ) {
3380 for ( name
in this.registry
) {
3381 tool
= this.registry
[name
];
3383 // Include tools with matching group
3384 tool
.static.group
=== item
.group
&&
3385 // Only add tools by group name when auto-add is enabled
3386 tool
.static.autoAddToGroup
&&
3387 // Exclude already used tools
3388 ( !used
|| !used
[name
] )
3396 // Include tools with matching name and exclude already used tools
3397 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3398 names
.push( item
.name
);
3400 used
[item
.name
] = true;
3410 * Factory for tool groups.
3413 * @extends OO.Factory
3416 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3417 // Parent constructor
3418 OO
.Factory
.call( this );
3421 defaultClasses
= this.constructor.static.getDefaultClasses();
3423 // Register default toolgroups
3424 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3425 this.register( defaultClasses
[i
] );
3431 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3433 /* Static Methods */
3436 * Get a default set of classes to be registered on construction
3438 * @return {Function[]} Default classes
3440 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3443 OO
.ui
.ListToolGroup
,
3449 * Element with a button.
3451 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3452 * and access keys for accessibility purposes.
3458 * @param {Object} [config] Configuration options
3459 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3460 * @cfg {boolean} [framed=true] Render button with a frame
3461 * @cfg {number} [tabIndex=0] Button's tab index, use null to have no tabIndex
3462 * @cfg {string} [accessKey] Button's access key
3464 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3465 // Configuration initialization
3466 config
= config
|| {};
3469 this.$button
= null;
3471 this.tabIndex
= null;
3472 this.accessKey
= null;
3473 this.active
= false;
3474 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
3475 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
3478 this.$element
.addClass( 'oo-ui-buttonElement' );
3479 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3480 this.setTabIndex( config
.tabIndex
|| 0 );
3481 this.setAccessKey( config
.accessKey
);
3482 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3487 OO
.initClass( OO
.ui
.ButtonElement
);
3489 /* Static Properties */
3492 * Cancel mouse down events.
3496 * @property {boolean}
3498 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3503 * Set the button element.
3505 * If an element is already set, it will be cleaned up before setting up the new element.
3507 * @param {jQuery} $button Element to use as button
3509 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3510 if ( this.$button
) {
3512 .removeClass( 'oo-ui-buttonElement-button' )
3513 .removeAttr( 'role accesskey tabindex' )
3514 .off( this.onMouseDownHandler
);
3517 this.$button
= $button
3518 .addClass( 'oo-ui-buttonElement-button' )
3519 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3520 .on( 'mousedown', this.onMouseDownHandler
);
3524 * Handles mouse down events.
3526 * @param {jQuery.Event} e Mouse down event
3528 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3529 if ( this.isDisabled() || e
.which
!== 1 ) {
3532 // Remove the tab-index while the button is down to prevent the button from stealing focus
3533 this.$button
.removeAttr( 'tabindex' );
3534 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3535 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3536 // reliably reapply the tabindex and remove the pressed class
3537 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3538 // Prevent change of focus unless specifically configured otherwise
3539 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3545 * Handles mouse up events.
3547 * @param {jQuery.Event} e Mouse up event
3549 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3550 if ( this.isDisabled() || e
.which
!== 1 ) {
3553 // Restore the tab-index after the button is up to restore the button's accesssibility
3554 this.$button
.attr( 'tabindex', this.tabIndex
);
3555 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3556 // Stop listening for mouseup, since we only needed this once
3557 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3563 * @param {boolean} [framed] Make button framed, omit to toggle
3566 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3567 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3568 if ( framed
!== this.framed
) {
3569 this.framed
= framed
;
3571 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3572 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3581 * @param {number|null} tabIndex Button's tab index, use null to remove
3584 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3585 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3587 if ( this.tabIndex
!== tabIndex
) {
3588 if ( this.$button
) {
3589 if ( tabIndex
!== null ) {
3590 this.$button
.attr( 'tabindex', tabIndex
);
3592 this.$button
.removeAttr( 'tabindex' );
3595 this.tabIndex
= tabIndex
;
3604 * @param {string} accessKey Button's access key, use empty string to remove
3607 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3608 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3610 if ( this.accessKey
!== accessKey
) {
3611 if ( this.$button
) {
3612 if ( accessKey
!== null ) {
3613 this.$button
.attr( 'accesskey', accessKey
);
3615 this.$button
.removeAttr( 'accesskey' );
3618 this.accessKey
= accessKey
;
3627 * @param {boolean} [value] Make button active
3630 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3631 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3636 * Element containing a sequence of child elements.
3642 * @param {Object} [config] Configuration options
3643 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3645 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3647 config
= config
|| {};
3652 this.aggregateItemEvents
= {};
3655 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3661 * Set the group element.
3663 * If an element is already set, items will be moved to the new element.
3665 * @param {jQuery} $group Element to use as group
3667 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3670 this.$group
= $group
;
3671 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3672 this.$group
.append( this.items
[i
].$element
);
3677 * Check if there are no items.
3679 * @return {boolean} Group is empty
3681 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3682 return !this.items
.length
;
3688 * @return {OO.ui.Element[]} Items
3690 OO
.ui
.GroupElement
.prototype.getItems = function () {
3691 return this.items
.slice( 0 );
3695 * Add an aggregate item event.
3697 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3698 * and with an additional leading parameter containing the item that emitted the original event.
3699 * Other arguments that were emitted from the original event are passed through.
3701 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3702 * event, use null value to remove aggregation
3703 * @throws {Error} If aggregation already exists
3705 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3706 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3708 for ( itemEvent
in events
) {
3709 groupEvent
= events
[itemEvent
];
3711 // Remove existing aggregated event
3712 if ( itemEvent
in this.aggregateItemEvents
) {
3713 // Don't allow duplicate aggregations
3715 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3717 // Remove event aggregation from existing items
3718 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3719 item
= this.items
[i
];
3720 if ( item
.connect
&& item
.disconnect
) {
3722 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3723 item
.disconnect( this, remove
);
3726 // Prevent future items from aggregating event
3727 delete this.aggregateItemEvents
[itemEvent
];
3730 // Add new aggregate event
3732 // Make future items aggregate event
3733 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3734 // Add event aggregation to existing items
3735 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3736 item
= this.items
[i
];
3737 if ( item
.connect
&& item
.disconnect
) {
3739 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3740 item
.connect( this, add
);
3750 * Adding an existing item (by value) will move it.
3752 * @param {OO.ui.Element[]} items Item
3753 * @param {number} [index] Index to insert items at
3756 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3757 var i
, len
, item
, event
, events
, currentIndex
,
3760 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3763 // Check if item exists then remove it first, effectively "moving" it
3764 currentIndex
= $.inArray( item
, this.items
);
3765 if ( currentIndex
>= 0 ) {
3766 this.removeItems( [ item
] );
3767 // Adjust index to compensate for removal
3768 if ( currentIndex
< index
) {
3773 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3775 for ( event
in this.aggregateItemEvents
) {
3776 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3778 item
.connect( this, events
);
3780 item
.setElementGroup( this );
3781 itemElements
.push( item
.$element
.get( 0 ) );
3784 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3785 this.$group
.append( itemElements
);
3786 this.items
.push
.apply( this.items
, items
);
3787 } else if ( index
=== 0 ) {
3788 this.$group
.prepend( itemElements
);
3789 this.items
.unshift
.apply( this.items
, items
);
3791 this.items
[index
].$element
.before( itemElements
);
3792 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3801 * Items will be detached, not removed, so they can be used later.
3803 * @param {OO.ui.Element[]} items Items to remove
3806 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3807 var i
, len
, item
, index
, remove
, itemEvent
;
3809 // Remove specific items
3810 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3812 index
= $.inArray( item
, this.items
);
3813 if ( index
!== -1 ) {
3815 item
.connect
&& item
.disconnect
&&
3816 !$.isEmptyObject( this.aggregateItemEvents
)
3819 if ( itemEvent
in this.aggregateItemEvents
) {
3820 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3822 item
.disconnect( this, remove
);
3824 item
.setElementGroup( null );
3825 this.items
.splice( index
, 1 );
3826 item
.$element
.detach();
3836 * Items will be detached, not removed, so they can be used later.
3840 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3841 var i
, len
, item
, remove
, itemEvent
;
3844 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3845 item
= this.items
[i
];
3847 item
.connect
&& item
.disconnect
&&
3848 !$.isEmptyObject( this.aggregateItemEvents
)
3851 if ( itemEvent
in this.aggregateItemEvents
) {
3852 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3854 item
.disconnect( this, remove
);
3856 item
.setElementGroup( null );
3857 item
.$element
.detach();
3865 * Element containing an icon.
3867 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
3868 * a control or convey information in a more space efficient way. Icons should rarely be used
3869 * without labels; such as in a toolbar where space is at a premium or within a context where the
3870 * meaning is very clear to the user.
3876 * @param {Object} [config] Configuration options
3877 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
3878 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
3879 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3881 * @cfg {string} [iconTitle] Icon title text or a function that returns text
3883 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
3884 // Config intialization
3885 config
= config
|| {};
3890 this.iconTitle
= null;
3893 this.setIcon( config
.icon
|| this.constructor.static.icon
);
3894 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
3895 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
3900 OO
.initClass( OO
.ui
.IconElement
);
3902 /* Static Properties */
3907 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
3909 * For i18n purposes, this property can be an object containing a `default` icon name property and
3910 * additional icon names keyed by language code.
3912 * Example of i18n icon definition:
3913 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
3917 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
3918 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3921 OO
.ui
.IconElement
.static.icon
= null;
3928 * @property {string|Function|null} Icon title text, a function that returns text or null for no
3931 OO
.ui
.IconElement
.static.iconTitle
= null;
3936 * Set the icon element.
3938 * If an element is already set, it will be cleaned up before setting up the new element.
3940 * @param {jQuery} $icon Element to use as icon
3942 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
3945 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
3946 .removeAttr( 'title' );
3950 .addClass( 'oo-ui-iconElement-icon' )
3951 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
3952 if ( this.iconTitle
!== null ) {
3953 this.$icon
.attr( 'title', this.iconTitle
);
3960 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
3961 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3962 * language, use null to remove icon
3965 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
3966 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
3967 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
3969 if ( this.icon
!== icon
) {
3971 if ( this.icon
!== null ) {
3972 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
3974 if ( icon
!== null ) {
3975 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
3981 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
3989 * @param {string|Function|null} icon Icon title text, a function that returns text or null
3993 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
3994 iconTitle
= typeof iconTitle
=== 'function' ||
3995 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
3996 OO
.ui
.resolveMsg( iconTitle
) : null;
3998 if ( this.iconTitle
!== iconTitle
) {
3999 this.iconTitle
= iconTitle
;
4001 if ( this.iconTitle
!== null ) {
4002 this.$icon
.attr( 'title', iconTitle
);
4004 this.$icon
.removeAttr( 'title' );
4015 * @return {string} Icon
4017 OO
.ui
.IconElement
.prototype.getIcon = function () {
4022 * Element containing an indicator.
4024 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4025 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4026 * instead of performing an action directly, or an item in a list which has errors that need to be
4033 * @param {Object} [config] Configuration options
4034 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4036 * @cfg {string} [indicator] Symbolic indicator name
4037 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4039 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4040 // Config intialization
4041 config
= config
|| {};
4044 this.$indicator
= null;
4045 this.indicator
= null;
4046 this.indicatorTitle
= null;
4049 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4050 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4051 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4056 OO
.initClass( OO
.ui
.IndicatorElement
);
4058 /* Static Properties */
4065 * @property {string|null} Symbolic indicator name or null for no indicator
4067 OO
.ui
.IndicatorElement
.static.indicator
= null;
4074 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4077 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4082 * Set the indicator element.
4084 * If an element is already set, it will be cleaned up before setting up the new element.
4086 * @param {jQuery} $indicator Element to use as indicator
4088 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4089 if ( this.$indicator
) {
4091 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4092 .removeAttr( 'title' );
4095 this.$indicator
= $indicator
4096 .addClass( 'oo-ui-indicatorElement-indicator' )
4097 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4098 if ( this.indicatorTitle
!== null ) {
4099 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4106 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4109 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4110 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4112 if ( this.indicator
!== indicator
) {
4113 if ( this.$indicator
) {
4114 if ( this.indicator
!== null ) {
4115 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4117 if ( indicator
!== null ) {
4118 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4121 this.indicator
= indicator
;
4124 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4130 * Set indicator title.
4132 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4133 * null for no indicator title
4136 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4137 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4138 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4139 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4141 if ( this.indicatorTitle
!== indicatorTitle
) {
4142 this.indicatorTitle
= indicatorTitle
;
4143 if ( this.$indicator
) {
4144 if ( this.indicatorTitle
!== null ) {
4145 this.$indicator
.attr( 'title', indicatorTitle
);
4147 this.$indicator
.removeAttr( 'title' );
4158 * @return {string} title Symbolic name of indicator
4160 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4161 return this.indicator
;
4165 * Get indicator title.
4167 * @return {string} Indicator title text
4169 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4170 return this.indicatorTitle
;
4174 * Element containing a label.
4180 * @param {Object} [config] Configuration options
4181 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4182 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4183 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4185 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4186 // Config intialization
4187 config
= config
|| {};
4192 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4195 this.setLabel( config
.label
|| this.constructor.static.label
);
4196 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4201 OO
.initClass( OO
.ui
.LabelElement
);
4203 /* Static Properties */
4210 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4213 OO
.ui
.LabelElement
.static.label
= null;
4218 * Set the label element.
4220 * If an element is already set, it will be cleaned up before setting up the new element.
4222 * @param {jQuery} $label Element to use as label
4224 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4225 if ( this.$label
) {
4226 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4229 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4230 this.setLabelContent( this.label
);
4236 * An empty string will result in the label being hidden. A string containing only whitespace will
4237 * be converted to a single
4239 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4240 * text; or null for no label
4243 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4244 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4245 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4247 if ( this.label
!== label
) {
4248 if ( this.$label
) {
4249 this.setLabelContent( label
);
4254 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4262 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4263 * text; or null for no label
4265 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4274 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4275 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4276 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4283 * Set the content of the label.
4285 * Do not call this method until after the label element has been set by #setLabelElement.
4288 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4289 * text; or null for no label
4291 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4292 if ( typeof label
=== 'string' ) {
4293 if ( label
.match( /^\s*$/ ) ) {
4294 // Convert whitespace only string to a single non-breaking space
4295 this.$label
.html( ' ' );
4297 this.$label
.text( label
);
4299 } else if ( label
instanceof jQuery
) {
4300 this.$label
.empty().append( label
);
4302 this.$label
.empty();
4304 this.$label
.css( 'display', !label
? 'none' : '' );
4308 * Element containing an OO.ui.PopupWidget object.
4314 * @param {Object} [config] Configuration options
4315 * @cfg {Object} [popup] Configuration to pass to popup
4316 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4318 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4319 // Configuration initialization
4320 config
= config
|| {};
4323 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4324 { autoClose
: true },
4326 { $: this.$, $autoCloseIgnore
: this.$element
}
4335 * @return {OO.ui.PopupWidget} Popup widget
4337 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4342 * Element with named flags that can be added, removed, listed and checked.
4344 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4345 * the flag name. Flags are primarily useful for styling.
4351 * @param {Object} [config] Configuration options
4352 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4353 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4355 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4356 // Config initialization
4357 config
= config
|| {};
4361 this.$flagged
= null;
4364 this.setFlags( config
.flags
);
4365 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4372 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4373 * added/removed properties
4379 * Set the flagged element.
4381 * If an element is already set, it will be cleaned up before setting up the new element.
4383 * @param {jQuery} $flagged Element to add flags to
4385 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4386 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4387 return 'oo-ui-flaggedElement-' + flag
;
4390 if ( this.$flagged
) {
4391 this.$flagged
.removeClass( classNames
);
4394 this.$flagged
= $flagged
.addClass( classNames
);
4398 * Check if a flag is set.
4400 * @param {string} flag Name of flag
4401 * @return {boolean} Has flag
4403 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4404 return flag
in this.flags
;
4408 * Get the names of all flags set.
4410 * @return {string[]} flags Flag names
4412 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4413 return Object
.keys( this.flags
);
4422 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4423 var flag
, className
,
4426 classPrefix
= 'oo-ui-flaggedElement-';
4428 for ( flag
in this.flags
) {
4429 className
= classPrefix
+ flag
;
4430 changes
[flag
] = false;
4431 delete this.flags
[flag
];
4432 remove
.push( className
);
4435 if ( this.$flagged
) {
4436 this.$flagged
.removeClass( remove
.join( ' ' ) );
4439 this.emit( 'flag', changes
);
4445 * Add one or more flags.
4447 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4448 * keyed by flag name containing boolean set/remove instructions.
4452 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4453 var i
, len
, flag
, className
,
4457 classPrefix
= 'oo-ui-flaggedElement-';
4459 if ( typeof flags
=== 'string' ) {
4460 className
= classPrefix
+ flags
;
4462 if ( !this.flags
[flags
] ) {
4463 this.flags
[flags
] = true;
4464 add
.push( className
);
4466 } else if ( $.isArray( flags
) ) {
4467 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4469 className
= classPrefix
+ flag
;
4471 if ( !this.flags
[flag
] ) {
4472 changes
[flag
] = true;
4473 this.flags
[flag
] = true;
4474 add
.push( className
);
4477 } else if ( OO
.isPlainObject( flags
) ) {
4478 for ( flag
in flags
) {
4479 className
= classPrefix
+ flag
;
4480 if ( flags
[flag
] ) {
4482 if ( !this.flags
[flag
] ) {
4483 changes
[flag
] = true;
4484 this.flags
[flag
] = true;
4485 add
.push( className
);
4489 if ( this.flags
[flag
] ) {
4490 changes
[flag
] = false;
4491 delete this.flags
[flag
];
4492 remove
.push( className
);
4498 if ( this.$flagged
) {
4500 .addClass( add
.join( ' ' ) )
4501 .removeClass( remove
.join( ' ' ) );
4504 this.emit( 'flag', changes
);
4510 * Element with a title.
4512 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4513 * not visible on touch devices.
4519 * @param {Object} [config] Configuration options
4520 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4521 * @cfg {string|Function} [title] Title text or a function that returns text
4523 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4524 // Config intialization
4525 config
= config
|| {};
4528 this.$titled
= null;
4532 this.setTitle( config
.title
|| this.constructor.static.title
);
4533 this.setTitledElement( config
.$titled
|| this.$element
);
4538 OO
.initClass( OO
.ui
.TitledElement
);
4540 /* Static Properties */
4547 * @property {string|Function} Title text or a function that returns text
4549 OO
.ui
.TitledElement
.static.title
= null;
4554 * Set the titled element.
4556 * If an element is already set, it will be cleaned up before setting up the new element.
4558 * @param {jQuery} $titled Element to set title on
4560 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4561 if ( this.$titled
) {
4562 this.$titled
.removeAttr( 'title' );
4565 this.$titled
= $titled
;
4567 this.$titled
.attr( 'title', this.title
);
4574 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4577 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4578 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4580 if ( this.title
!== title
) {
4581 if ( this.$titled
) {
4582 if ( title
!== null ) {
4583 this.$titled
.attr( 'title', title
);
4585 this.$titled
.removeAttr( 'title' );
4597 * @return {string} Title string
4599 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4604 * Element that can be automatically clipped to visible boundaries.
4606 * Whenever the element's natural height changes, you have to call
4607 * #clip to make sure it's still clipping correctly.
4613 * @param {Object} [config] Configuration options
4614 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4616 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4617 // Configuration initialization
4618 config
= config
|| {};
4621 this.$clippable
= null;
4622 this.clipping
= false;
4623 this.clippedHorizontally
= false;
4624 this.clippedVertically
= false;
4625 this.$clippableContainer
= null;
4626 this.$clippableScroller
= null;
4627 this.$clippableWindow
= null;
4628 this.idealWidth
= null;
4629 this.idealHeight
= null;
4630 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
4631 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
4634 this.setClippableElement( config
.$clippable
|| this.$element
);
4640 * Set clippable element.
4642 * If an element is already set, it will be cleaned up before setting up the new element.
4644 * @param {jQuery} $clippable Element to make clippable
4646 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4647 if ( this.$clippable
) {
4648 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4649 this.$clippable
.css( { width
: '', height
: '' } );
4650 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4651 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4654 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4661 * Do not turn clipping on until after the element is attached to the DOM and visible.
4663 * @param {boolean} [clipping] Enable clipping, omit to toggle
4666 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4667 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4669 if ( this.clipping
!== clipping
) {
4670 this.clipping
= clipping
;
4672 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4673 // If the clippable container is the body, we have to listen to scroll events and check
4674 // jQuery.scrollTop on the window because of browser inconsistencies
4675 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4676 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4677 this.$clippableContainer
;
4678 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4679 this.$clippableWindow
= this.$( this.getElementWindow() )
4680 .on( 'resize', this.onClippableWindowResizeHandler
);
4681 // Initial clip after visible
4684 this.$clippable
.css( { width
: '', height
: '' } );
4685 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4686 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4688 this.$clippableContainer
= null;
4689 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4690 this.$clippableScroller
= null;
4691 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4692 this.$clippableWindow
= null;
4700 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4702 * @return {boolean} Element will be clipped to the visible area
4704 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4705 return this.clipping
;
4709 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4711 * @return {boolean} Part of the element is being clipped
4713 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4714 return this.clippedHorizontally
|| this.clippedVertically
;
4718 * Check if the right of the element is being clipped by the nearest scrollable container.
4720 * @return {boolean} Part of the element is being clipped
4722 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4723 return this.clippedHorizontally
;
4727 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4729 * @return {boolean} Part of the element is being clipped
4731 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4732 return this.clippedVertically
;
4736 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
4738 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4739 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4741 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4742 this.idealWidth
= width
;
4743 this.idealHeight
= height
;
4745 if ( !this.clipping
) {
4746 // Update dimensions
4747 this.$clippable
.css( { width
: width
, height
: height
} );
4749 // While clipping, idealWidth and idealHeight are not considered
4753 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4754 * the element's natural height changes.
4756 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4757 * overlapped by, the visible area of the nearest scrollable container.
4761 OO
.ui
.ClippableElement
.prototype.clip = function () {
4762 if ( !this.clipping
) {
4763 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
4768 cOffset
= this.$clippable
.offset(),
4769 $container
= this.$clippableContainer
.is( 'body' ) ?
4770 this.$clippableWindow
: this.$clippableContainer
,
4771 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
4772 ccHeight
= $container
.innerHeight() - buffer
,
4773 ccWidth
= $container
.innerWidth() - buffer
,
4774 scrollTop
= this.$clippableScroller
.scrollTop(),
4775 scrollLeft
= this.$clippableScroller
.scrollLeft(),
4776 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
4777 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
4778 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
4779 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
4780 clipWidth
= desiredWidth
< naturalWidth
,
4781 clipHeight
= desiredHeight
< naturalHeight
;
4784 this.$clippable
.css( { overflowX
: 'auto', width
: desiredWidth
} );
4786 this.$clippable
.css( 'width', this.idealWidth
|| '' );
4787 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4788 this.$clippable
.css( 'overflowX', '' );
4791 this.$clippable
.css( { overflowY
: 'auto', height
: desiredHeight
} );
4793 this.$clippable
.css( 'height', this.idealHeight
|| '' );
4794 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4795 this.$clippable
.css( 'overflowY', '' );
4798 this.clippedHorizontally
= clipWidth
;
4799 this.clippedVertically
= clipHeight
;
4805 * Generic toolbar tool.
4809 * @extends OO.ui.Widget
4810 * @mixins OO.ui.IconElement
4813 * @param {OO.ui.ToolGroup} toolGroup
4814 * @param {Object} [config] Configuration options
4815 * @cfg {string|Function} [title] Title text or a function that returns text
4817 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4818 // Config intialization
4819 config
= config
|| {};
4821 // Parent constructor
4822 OO
.ui
.Tool
.super.call( this, config
);
4824 // Mixin constructors
4825 OO
.ui
.IconElement
.call( this, config
);
4828 this.toolGroup
= toolGroup
;
4829 this.toolbar
= this.toolGroup
.getToolbar();
4830 this.active
= false;
4831 this.$title
= this.$( '<span>' );
4832 this.$link
= this.$( '<a>' );
4836 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
4839 this.$title
.addClass( 'oo-ui-tool-title' );
4841 .addClass( 'oo-ui-tool-link' )
4842 .append( this.$icon
, this.$title
)
4843 .prop( 'tabIndex', 0 )
4844 .attr( 'role', 'button' );
4846 .data( 'oo-ui-tool', this )
4848 'oo-ui-tool ' + 'oo-ui-tool-name-' +
4849 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
4851 .append( this.$link
);
4852 this.setTitle( config
.title
|| this.constructor.static.title
);
4857 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
4858 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
4866 /* Static Properties */
4872 OO
.ui
.Tool
.static.tagName
= 'span';
4875 * Symbolic name of tool.
4880 * @property {string}
4882 OO
.ui
.Tool
.static.name
= '';
4890 * @property {string}
4892 OO
.ui
.Tool
.static.group
= '';
4897 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
4898 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
4899 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
4900 * appended to the title if the tool is part of a bar tool group.
4905 * @property {string|Function} Title text or a function that returns text
4907 OO
.ui
.Tool
.static.title
= '';
4910 * Tool can be automatically added to catch-all groups.
4914 * @property {boolean}
4916 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
4919 * Tool can be automatically added to named groups.
4922 * @property {boolean}
4925 OO
.ui
.Tool
.static.autoAddToGroup
= true;
4928 * Check if this tool is compatible with given data.
4932 * @param {Mixed} data Data to check
4933 * @return {boolean} Tool can be used with data
4935 OO
.ui
.Tool
.static.isCompatibleWith = function () {
4942 * Handle the toolbar state being updated.
4944 * This is an abstract method that must be overridden in a concrete subclass.
4948 OO
.ui
.Tool
.prototype.onUpdateState = function () {
4950 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
4955 * Handle the tool being selected.
4957 * This is an abstract method that must be overridden in a concrete subclass.
4961 OO
.ui
.Tool
.prototype.onSelect = function () {
4963 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
4968 * Check if the button is active.
4970 * @param {boolean} Button is active
4972 OO
.ui
.Tool
.prototype.isActive = function () {
4977 * Make the button appear active or inactive.
4979 * @param {boolean} state Make button appear active
4981 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
4982 this.active
= !!state
;
4983 if ( this.active
) {
4984 this.$element
.addClass( 'oo-ui-tool-active' );
4986 this.$element
.removeClass( 'oo-ui-tool-active' );
4991 * Get the tool title.
4993 * @param {string|Function} title Title text or a function that returns text
4996 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
4997 this.title
= OO
.ui
.resolveMsg( title
);
5003 * Get the tool title.
5005 * @return {string} Title text
5007 OO
.ui
.Tool
.prototype.getTitle = function () {
5012 * Get the tool's symbolic name.
5014 * @return {string} Symbolic name of tool
5016 OO
.ui
.Tool
.prototype.getName = function () {
5017 return this.constructor.static.name
;
5023 OO
.ui
.Tool
.prototype.updateTitle = function () {
5024 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
5025 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
5026 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
5033 .addClass( 'oo-ui-tool-accel' )
5037 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
5038 tooltipParts
.push( this.title
);
5040 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
5041 tooltipParts
.push( accel
);
5043 if ( tooltipParts
.length
) {
5044 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
5046 this.$link
.removeAttr( 'title' );
5053 OO
.ui
.Tool
.prototype.destroy = function () {
5054 this.toolbar
.disconnect( this );
5055 this.$element
.remove();
5059 * Collection of tool groups.
5062 * @extends OO.ui.Element
5063 * @mixins OO.EventEmitter
5064 * @mixins OO.ui.GroupElement
5067 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5068 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5069 * @param {Object} [config] Configuration options
5070 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5071 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5073 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5074 // Configuration initialization
5075 config
= config
|| {};
5077 // Parent constructor
5078 OO
.ui
.Toolbar
.super.call( this, config
);
5080 // Mixin constructors
5081 OO
.EventEmitter
.call( this );
5082 OO
.ui
.GroupElement
.call( this, config
);
5085 this.toolFactory
= toolFactory
;
5086 this.toolGroupFactory
= toolGroupFactory
;
5089 this.$bar
= this.$( '<div>' );
5090 this.$actions
= this.$( '<div>' );
5091 this.initialized
= false;
5095 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5096 .on( 'mousedown touchstart', OO
.ui
.bind( this.onPointerDown
, this ) );
5099 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5100 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
5101 if ( config
.actions
) {
5102 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
5103 this.$bar
.append( this.$actions
);
5105 this.$bar
.append( '<div style="clear:both"></div>' );
5106 if ( config
.shadow
) {
5107 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5109 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5114 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5115 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5116 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5121 * Get the tool factory.
5123 * @return {OO.ui.ToolFactory} Tool factory
5125 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5126 return this.toolFactory
;
5130 * Get the tool group factory.
5132 * @return {OO.Factory} Tool group factory
5134 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5135 return this.toolGroupFactory
;
5139 * Handles mouse down events.
5141 * @param {jQuery.Event} e Mouse down event
5143 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5144 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5145 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5146 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5152 * Sets up handles and preloads required information for the toolbar to work.
5153 * This must be called immediately after it is attached to a visible document.
5155 OO
.ui
.Toolbar
.prototype.initialize = function () {
5156 this.initialized
= true;
5162 * Tools can be specified in the following ways:
5164 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5165 * - All tools in a group: `{ group: 'group-name' }`
5166 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5168 * @param {Object.<string,Array>} groups List of tool group configurations
5169 * @param {Array|string} [groups.include] Tools to include
5170 * @param {Array|string} [groups.exclude] Tools to exclude
5171 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5172 * @param {Array|string} [groups.demote] Tools to demote to the end
5174 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5175 var i
, len
, type
, group
,
5177 defaultType
= 'bar';
5179 // Cleanup previous groups
5182 // Build out new groups
5183 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5185 if ( group
.include
=== '*' ) {
5186 // Apply defaults to catch-all groups
5187 if ( group
.type
=== undefined ) {
5188 group
.type
= 'list';
5190 if ( group
.label
=== undefined ) {
5191 group
.label
= 'ooui-toolbar-more';
5194 // Check type has been registered
5195 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5197 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5200 this.addItems( items
);
5204 * Remove all tools and groups from the toolbar.
5206 OO
.ui
.Toolbar
.prototype.reset = function () {
5211 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5212 this.items
[i
].destroy();
5218 * Destroys toolbar, removing event handlers and DOM elements.
5220 * Call this whenever you are done using a toolbar.
5222 OO
.ui
.Toolbar
.prototype.destroy = function () {
5224 this.$element
.remove();
5228 * Check if tool has not been used yet.
5230 * @param {string} name Symbolic name of tool
5231 * @return {boolean} Tool is available
5233 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5234 return !this.tools
[name
];
5238 * Prevent tool from being used again.
5240 * @param {OO.ui.Tool} tool Tool to reserve
5242 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5243 this.tools
[tool
.getName()] = tool
;
5247 * Allow tool to be used again.
5249 * @param {OO.ui.Tool} tool Tool to release
5251 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5252 delete this.tools
[tool
.getName()];
5256 * Get accelerator label for tool.
5258 * This is a stub that should be overridden to provide access to accelerator information.
5260 * @param {string} name Symbolic name of tool
5261 * @return {string|undefined} Tool accelerator label if available
5263 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5268 * Collection of tools.
5270 * Tools can be specified in the following ways:
5272 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5273 * - All tools in a group: `{ group: 'group-name' }`
5274 * - All tools: `'*'`
5278 * @extends OO.ui.Widget
5279 * @mixins OO.ui.GroupElement
5282 * @param {OO.ui.Toolbar} toolbar
5283 * @param {Object} [config] Configuration options
5284 * @cfg {Array|string} [include=[]] List of tools to include
5285 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5286 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5287 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5289 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5290 // Configuration initialization
5291 config
= config
|| {};
5293 // Parent constructor
5294 OO
.ui
.ToolGroup
.super.call( this, config
);
5296 // Mixin constructors
5297 OO
.ui
.GroupElement
.call( this, config
);
5300 this.toolbar
= toolbar
;
5302 this.pressed
= null;
5303 this.autoDisabled
= false;
5304 this.include
= config
.include
|| [];
5305 this.exclude
= config
.exclude
|| [];
5306 this.promote
= config
.promote
|| [];
5307 this.demote
= config
.demote
|| [];
5308 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
5312 'mousedown touchstart': OO
.ui
.bind( this.onPointerDown
, this ),
5313 'mouseup touchend': OO
.ui
.bind( this.onPointerUp
, this ),
5314 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
5315 mouseout
: OO
.ui
.bind( this.onMouseOut
, this )
5317 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5318 this.aggregate( { disable
: 'itemDisable' } );
5319 this.connect( this, { itemDisable
: 'updateDisabled' } );
5322 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5324 .addClass( 'oo-ui-toolGroup' )
5325 .append( this.$group
);
5331 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5332 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5340 /* Static Properties */
5343 * Show labels in tooltips.
5347 * @property {boolean}
5349 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5352 * Show acceleration labels in tooltips.
5356 * @property {boolean}
5358 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5361 * Automatically disable the toolgroup when all tools are disabled
5365 * @property {boolean}
5367 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5374 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5375 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5381 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5382 var i
, item
, allDisabled
= true;
5384 if ( this.constructor.static.autoDisable
) {
5385 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5386 item
= this.items
[i
];
5387 if ( !item
.isDisabled() ) {
5388 allDisabled
= false;
5392 this.autoDisabled
= allDisabled
;
5394 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5398 * Handle mouse down events.
5400 * @param {jQuery.Event} e Mouse down event
5402 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5403 // e.which is 0 for touch events, 1 for left mouse button
5404 if ( !this.isDisabled() && e
.which
<= 1 ) {
5405 this.pressed
= this.getTargetTool( e
);
5406 if ( this.pressed
) {
5407 this.pressed
.setActive( true );
5408 this.getElementDocument().addEventListener(
5409 'mouseup', this.onCapturedMouseUpHandler
, true
5417 * Handle captured mouse up events.
5419 * @param {Event} e Mouse up event
5421 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5422 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5423 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5424 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5425 this.onPointerUp( e
);
5429 * Handle mouse up events.
5431 * @param {jQuery.Event} e Mouse up event
5433 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5434 var tool
= this.getTargetTool( e
);
5436 // e.which is 0 for touch events, 1 for left mouse button
5437 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5438 this.pressed
.onSelect();
5441 this.pressed
= null;
5446 * Handle mouse over events.
5448 * @param {jQuery.Event} e Mouse over event
5450 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5451 var tool
= this.getTargetTool( e
);
5453 if ( this.pressed
&& this.pressed
=== tool
) {
5454 this.pressed
.setActive( true );
5459 * Handle mouse out events.
5461 * @param {jQuery.Event} e Mouse out event
5463 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5464 var tool
= this.getTargetTool( e
);
5466 if ( this.pressed
&& this.pressed
=== tool
) {
5467 this.pressed
.setActive( false );
5472 * Get the closest tool to a jQuery.Event.
5474 * Only tool links are considered, which prevents other elements in the tool such as popups from
5475 * triggering tool group interactions.
5478 * @param {jQuery.Event} e
5479 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5481 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5483 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5485 if ( $item
.length
) {
5486 tool
= $item
.parent().data( 'oo-ui-tool' );
5489 return tool
&& !tool
.isDisabled() ? tool
: null;
5493 * Handle tool registry register events.
5495 * If a tool is registered after the group is created, we must repopulate the list to account for:
5497 * - a tool being added that may be included
5498 * - a tool already included being overridden
5500 * @param {string} name Symbolic name of tool
5502 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5507 * Get the toolbar this group is in.
5509 * @return {OO.ui.Toolbar} Toolbar of group
5511 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5512 return this.toolbar
;
5516 * Add and remove tools based on configuration.
5518 OO
.ui
.ToolGroup
.prototype.populate = function () {
5519 var i
, len
, name
, tool
,
5520 toolFactory
= this.toolbar
.getToolFactory(),
5524 list
= this.toolbar
.getToolFactory().getTools(
5525 this.include
, this.exclude
, this.promote
, this.demote
5528 // Build a list of needed tools
5529 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5533 toolFactory
.lookup( name
) &&
5534 // Tool is available or is already in this group
5535 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5537 tool
= this.tools
[name
];
5539 // Auto-initialize tools on first use
5540 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5543 this.toolbar
.reserveTool( tool
);
5548 // Remove tools that are no longer needed
5549 for ( name
in this.tools
) {
5550 if ( !names
[name
] ) {
5551 this.tools
[name
].destroy();
5552 this.toolbar
.releaseTool( this.tools
[name
] );
5553 remove
.push( this.tools
[name
] );
5554 delete this.tools
[name
];
5557 if ( remove
.length
) {
5558 this.removeItems( remove
);
5560 // Update emptiness state
5562 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5564 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5566 // Re-add tools (moving existing ones to new locations)
5567 this.addItems( add
);
5568 // Disabled state may depend on items
5569 this.updateDisabled();
5573 * Destroy tool group.
5575 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5579 this.toolbar
.getToolFactory().disconnect( this );
5580 for ( name
in this.tools
) {
5581 this.toolbar
.releaseTool( this.tools
[name
] );
5582 this.tools
[name
].disconnect( this ).destroy();
5583 delete this.tools
[name
];
5585 this.$element
.remove();
5589 * Dialog for showing a message.
5592 * - Registers two actions by default (safe and primary).
5593 * - Renders action widgets in the footer.
5596 * @extends OO.ui.Dialog
5599 * @param {Object} [config] Configuration options
5601 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5602 // Parent constructor
5603 OO
.ui
.MessageDialog
.super.call( this, config
);
5606 this.verticalActionLayout
= null;
5609 this.$element
.addClass( 'oo-ui-messageDialog' );
5614 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5616 /* Static Properties */
5618 OO
.ui
.MessageDialog
.static.name
= 'message';
5620 OO
.ui
.MessageDialog
.static.size
= 'small';
5622 OO
.ui
.MessageDialog
.static.verbose
= false;
5627 * A confirmation dialog's title should describe what the progressive action will do. An alert
5628 * dialog's title should describe what event occured.
5632 * @property {jQuery|string|Function|null}
5634 OO
.ui
.MessageDialog
.static.title
= null;
5637 * A confirmation dialog's message should describe the consequences of the progressive action. An
5638 * alert dialog's message should describe why the event occured.
5642 * @property {jQuery|string|Function|null}
5644 OO
.ui
.MessageDialog
.static.message
= null;
5646 OO
.ui
.MessageDialog
.static.actions
= [
5647 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5648 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5656 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5658 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5662 * Toggle action layout between vertical and horizontal.
5664 * @param {boolean} [value] Layout actions vertically, omit to toggle
5667 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5668 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5670 if ( value
!== this.verticalActionLayout
) {
5671 this.verticalActionLayout
= value
;
5673 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5674 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5683 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5685 return new OO
.ui
.Process( function () {
5686 this.close( { action
: action
} );
5689 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5695 * @param {Object} [data] Dialog opening data
5696 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5697 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5698 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5699 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5702 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5706 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5707 .next( function () {
5708 this.title
.setLabel(
5709 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5711 this.message
.setLabel(
5712 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5714 this.message
.$element
.toggleClass(
5715 'oo-ui-messageDialog-message-verbose',
5716 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5724 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5725 return Math
.round( this.text
.$element
.outerHeight( true ) );
5731 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5733 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5736 this.$actions
= this.$( '<div>' );
5737 this.container
= new OO
.ui
.PanelLayout( {
5738 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5740 this.text
= new OO
.ui
.PanelLayout( {
5741 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5743 this.message
= new OO
.ui
.LabelWidget( {
5744 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5748 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5749 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5750 this.container
.$element
.append( this.text
.$element
);
5751 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5752 this.$body
.append( this.container
.$element
);
5753 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5754 this.$foot
.append( this.$actions
);
5760 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5761 var i
, len
, other
, special
, others
;
5764 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5766 special
= this.actions
.getSpecial();
5767 others
= this.actions
.getOthers();
5768 if ( special
.safe
) {
5769 this.$actions
.append( special
.safe
.$element
);
5770 special
.safe
.toggleFramed( false );
5772 if ( others
.length
) {
5773 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5775 this.$actions
.append( other
.$element
);
5776 other
.toggleFramed( false );
5779 if ( special
.primary
) {
5780 this.$actions
.append( special
.primary
.$element
);
5781 special
.primary
.toggleFramed( false );
5785 if ( !this.isOpening() ) {
5786 this.manager
.updateWindowSize( this );
5788 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5792 * Fit action actions into columns or rows.
5794 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5796 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5798 actions
= this.actions
.get();
5801 this.toggleVerticalActionLayout( false );
5802 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5803 action
= actions
[i
];
5804 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5805 this.toggleVerticalActionLayout( true );
5812 * Navigation dialog window.
5815 * - Show and hide errors.
5816 * - Retry an action.
5819 * - Renders header with dialog title and one action widget on either side
5820 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5821 * which close the dialog).
5822 * - Displays any action widgets in the footer (none by default).
5823 * - Ability to dismiss errors.
5825 * Subclass responsibilities:
5826 * - Register a 'safe' action.
5827 * - Register a 'primary' action.
5828 * - Add content to the dialog.
5832 * @extends OO.ui.Dialog
5835 * @param {Object} [config] Configuration options
5837 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
5838 // Parent constructor
5839 OO
.ui
.ProcessDialog
.super.call( this, config
);
5842 this.$element
.addClass( 'oo-ui-processDialog' );
5847 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
5852 * Handle dismiss button click events.
5856 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
5861 * Handle retry button click events.
5863 * Hides errors and then tries again.
5865 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
5867 this.executeAction( this.currentAction
.getAction() );
5873 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
5874 if ( this.actions
.isSpecial( action
) ) {
5877 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5883 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
5885 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
5888 this.$navigation
= this.$( '<div>' );
5889 this.$location
= this.$( '<div>' );
5890 this.$safeActions
= this.$( '<div>' );
5891 this.$primaryActions
= this.$( '<div>' );
5892 this.$otherActions
= this.$( '<div>' );
5893 this.dismissButton
= new OO
.ui
.ButtonWidget( {
5895 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
5897 this.retryButton
= new OO
.ui
.ButtonWidget( {
5899 label
: OO
.ui
.msg( 'ooui-dialog-process-retry' )
5901 this.$errors
= this.$( '<div>' );
5902 this.$errorsTitle
= this.$( '<div>' );
5905 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
5906 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
5909 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
5911 .append( this.title
.$element
)
5912 .addClass( 'oo-ui-processDialog-location' );
5913 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
5914 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
5915 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
5917 .addClass( 'oo-ui-processDialog-errors-title' )
5918 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
5920 .addClass( 'oo-ui-processDialog-errors' )
5921 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
5923 .addClass( 'oo-ui-processDialog-content' )
5924 .append( this.$errors
);
5926 .addClass( 'oo-ui-processDialog-navigation' )
5927 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
5928 this.$head
.append( this.$navigation
);
5929 this.$foot
.append( this.$otherActions
);
5935 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
5936 var i
, len
, other
, special
, others
;
5939 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
5941 special
= this.actions
.getSpecial();
5942 others
= this.actions
.getOthers();
5943 if ( special
.primary
) {
5944 this.$primaryActions
.append( special
.primary
.$element
);
5945 special
.primary
.toggleFramed( true );
5947 if ( others
.length
) {
5948 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5950 this.$otherActions
.append( other
.$element
);
5951 other
.toggleFramed( true );
5954 if ( special
.safe
) {
5955 this.$safeActions
.append( special
.safe
.$element
);
5956 special
.safe
.toggleFramed( true );
5960 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5966 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
5967 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
5968 .fail( OO
.ui
.bind( this.showErrors
, this ) );
5972 * Fit label between actions.
5976 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
5977 var width
= Math
.max(
5978 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
5979 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
5981 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
5987 * Handle errors that occured durring accept or reject processes.
5989 * @param {OO.ui.Error[]} errors Errors to be handled
5991 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
5996 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
5997 if ( !errors
[i
].isRecoverable() ) {
5998 recoverable
= false;
6000 $item
= this.$( '<div>' )
6001 .addClass( 'oo-ui-processDialog-error' )
6002 .append( errors
[i
].getMessage() );
6003 items
.push( $item
[0] );
6005 this.$errorItems
= this.$( items
);
6006 if ( recoverable
) {
6007 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
6009 this.currentAction
.setDisabled( true );
6011 this.retryButton
.toggle( recoverable
);
6012 this.$errorsTitle
.after( this.$errorItems
);
6013 this.$errors
.show().scrollTop( 0 );
6019 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
6020 this.$errors
.hide();
6021 this.$errorItems
.remove();
6022 this.$errorItems
= null;
6026 * Layout containing a series of pages.
6029 * @extends OO.ui.Layout
6032 * @param {Object} [config] Configuration options
6033 * @cfg {boolean} [continuous=false] Show all pages, one after another
6034 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
6035 * @cfg {boolean} [outlined=false] Show an outline
6036 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
6038 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
6039 // Initialize configuration
6040 config
= config
|| {};
6042 // Parent constructor
6043 OO
.ui
.BookletLayout
.super.call( this, config
);
6046 this.currentPageName
= null;
6048 this.ignoreFocus
= false;
6049 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
6050 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
6051 this.outlineVisible
= false;
6052 this.outlined
= !!config
.outlined
;
6053 if ( this.outlined
) {
6054 this.editable
= !!config
.editable
;
6055 this.outlineControlsWidget
= null;
6056 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
6057 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
6058 this.gridLayout
= new OO
.ui
.GridLayout(
6059 [ this.outlinePanel
, this.stackLayout
],
6060 { $: this.$, widths
: [ 1, 2 ] }
6062 this.outlineVisible
= true;
6063 if ( this.editable
) {
6064 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6065 this.outlineWidget
, { $: this.$ }
6071 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6072 if ( this.outlined
) {
6073 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
6075 if ( this.autoFocus
) {
6076 // Event 'focus' does not bubble, but 'focusin' does
6077 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
6081 this.$element
.addClass( 'oo-ui-bookletLayout' );
6082 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6083 if ( this.outlined
) {
6084 this.outlinePanel
.$element
6085 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6086 .append( this.outlineWidget
.$element
);
6087 if ( this.editable
) {
6088 this.outlinePanel
.$element
6089 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6090 .append( this.outlineControlsWidget
.$element
);
6092 this.$element
.append( this.gridLayout
.$element
);
6094 this.$element
.append( this.stackLayout
.$element
);
6100 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6106 * @param {OO.ui.PageLayout} page Current page
6111 * @param {OO.ui.PageLayout[]} page Added pages
6112 * @param {number} index Index pages were added at
6117 * @param {OO.ui.PageLayout[]} pages Removed pages
6123 * Handle stack layout focus.
6125 * @param {jQuery.Event} e Focusin event
6127 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6130 // Find the page that an element was focused within
6131 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6132 for ( name
in this.pages
) {
6133 // Check for page match, exclude current page to find only page changes
6134 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6135 this.setPage( name
);
6142 * Handle stack layout set events.
6144 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6146 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6147 var $input
, layout
= this;
6149 page
.scrollElementIntoView( { complete: function () {
6150 if ( layout
.autoFocus
) {
6151 // Set focus to the first input if nothing on the page is focused yet
6152 if ( !page
.$element
.find( ':focus' ).length
) {
6153 $input
= page
.$element
.find( ':input:first' );
6154 if ( $input
.length
) {
6164 * Handle outline widget select events.
6166 * @param {OO.ui.OptionWidget|null} item Selected item
6168 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
6170 this.setPage( item
.getData() );
6175 * Check if booklet has an outline.
6179 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6180 return this.outlined
;
6184 * Check if booklet has editing controls.
6188 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6189 return this.editable
;
6193 * Check if booklet has a visible outline.
6197 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6198 return this.outlined
&& this.outlineVisible
;
6202 * Hide or show the outline.
6204 * @param {boolean} [show] Show outline, omit to invert current state
6207 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6208 if ( this.outlined
) {
6209 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6210 this.outlineVisible
= show
;
6211 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6218 * Get the outline widget.
6220 * @param {OO.ui.PageLayout} page Page to be selected
6221 * @return {OO.ui.PageLayout|null} Closest page to another
6223 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6224 var next
, prev
, level
,
6225 pages
= this.stackLayout
.getItems(),
6226 index
= $.inArray( page
, pages
);
6228 if ( index
!== -1 ) {
6229 next
= pages
[index
+ 1];
6230 prev
= pages
[index
- 1];
6231 // Prefer adjacent pages at the same level
6232 if ( this.outlined
) {
6233 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
6236 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
6242 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
6248 return prev
|| next
|| null;
6252 * Get the outline widget.
6254 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
6256 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6257 return this.outlineWidget
;
6261 * Get the outline controls widget. If the outline is not editable, null is returned.
6263 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6265 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6266 return this.outlineControlsWidget
;
6270 * Get a page by name.
6272 * @param {string} name Symbolic name of page
6273 * @return {OO.ui.PageLayout|undefined} Page, if found
6275 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6276 return this.pages
[name
];
6280 * Get the current page name.
6282 * @return {string|null} Current page name
6284 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6285 return this.currentPageName
;
6289 * Add a page to the layout.
6291 * When pages are added with the same names as existing pages, the existing pages will be
6292 * automatically removed before the new pages are added.
6294 * @param {OO.ui.PageLayout[]} pages Pages to add
6295 * @param {number} index Index to insert pages after
6299 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6300 var i
, len
, name
, page
, item
, currentIndex
,
6301 stackLayoutPages
= this.stackLayout
.getItems(),
6305 // Remove pages with same names
6306 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6308 name
= page
.getName();
6310 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6311 // Correct the insertion index
6312 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6313 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6316 remove
.push( this.pages
[name
] );
6319 if ( remove
.length
) {
6320 this.removePages( remove
);
6324 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6326 name
= page
.getName();
6327 this.pages
[page
.getName()] = page
;
6328 if ( this.outlined
) {
6329 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
6330 page
.setOutlineItem( item
);
6335 if ( this.outlined
&& items
.length
) {
6336 this.outlineWidget
.addItems( items
, index
);
6337 this.updateOutlineWidget();
6339 this.stackLayout
.addItems( pages
, index
);
6340 this.emit( 'add', pages
, index
);
6346 * Remove a page from the layout.
6351 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6352 var i
, len
, name
, page
,
6355 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6357 name
= page
.getName();
6358 delete this.pages
[name
];
6359 if ( this.outlined
) {
6360 items
.push( this.outlineWidget
.getItemFromData( name
) );
6361 page
.setOutlineItem( null );
6364 if ( this.outlined
&& items
.length
) {
6365 this.outlineWidget
.removeItems( items
);
6366 this.updateOutlineWidget();
6368 this.stackLayout
.removeItems( pages
);
6369 this.emit( 'remove', pages
);
6375 * Clear all pages from the layout.
6380 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6382 pages
= this.stackLayout
.getItems();
6385 this.currentPageName
= null;
6386 if ( this.outlined
) {
6387 this.outlineWidget
.clearItems();
6388 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6389 pages
[i
].setOutlineItem( null );
6392 this.stackLayout
.clearItems();
6394 this.emit( 'remove', pages
);
6400 * Set the current page by name.
6403 * @param {string} name Symbolic name of page
6405 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6408 page
= this.pages
[name
];
6410 if ( name
!== this.currentPageName
) {
6411 if ( this.outlined
) {
6412 selectedItem
= this.outlineWidget
.getSelectedItem();
6413 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6414 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
6418 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6419 this.pages
[this.currentPageName
].setActive( false );
6420 // Blur anything focused if the next page doesn't have anything focusable - this
6421 // is not needed if the next page has something focusable because once it is focused
6422 // this blur happens automatically
6423 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6424 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6425 if ( $focused
.length
) {
6430 this.currentPageName
= name
;
6431 this.stackLayout
.setItem( page
);
6432 page
.setActive( true );
6433 this.emit( 'set', page
);
6439 * Call this after adding or removing items from the OutlineWidget.
6443 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
6444 // Auto-select first item when nothing is selected anymore
6445 if ( !this.outlineWidget
.getSelectedItem() ) {
6446 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
6453 * Layout made of a field and optional label.
6456 * @extends OO.ui.Layout
6457 * @mixins OO.ui.LabelElement
6459 * Available label alignment modes include:
6460 * - left: Label is before the field and aligned away from it, best for when the user will be
6461 * scanning for a specific label in a form with many fields
6462 * - right: Label is before the field and aligned toward it, best for forms the user is very
6463 * familiar with and will tab through field checking quickly to verify which field they are in
6464 * - top: Label is before the field and above it, best for when the use will need to fill out all
6465 * fields from top to bottom in a form with few fields
6466 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6467 * checkboxes or radio buttons
6470 * @param {OO.ui.Widget} field Field widget
6471 * @param {Object} [config] Configuration options
6472 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6473 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6475 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
6476 // Config initialization
6477 config
= $.extend( { align
: 'left' }, config
);
6479 // Parent constructor
6480 OO
.ui
.FieldLayout
.super.call( this, config
);
6482 // Mixin constructors
6483 OO
.ui
.LabelElement
.call( this, config
);
6486 this.$field
= this.$( '<div>' );
6489 if ( config
.help
) {
6490 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6492 classes
: [ 'oo-ui-fieldLayout-help' ],
6497 this.popupButtonWidget
.getPopup().$body
.append(
6499 .text( config
.help
)
6500 .addClass( 'oo-ui-fieldLayout-help-content' )
6502 this.$help
= this.popupButtonWidget
.$element
;
6504 this.$help
= this.$( [] );
6508 if ( this.field
instanceof OO
.ui
.InputWidget
) {
6509 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
6511 this.field
.connect( this, { disable
: 'onFieldDisable' } );
6514 this.$element
.addClass( 'oo-ui-fieldLayout' );
6516 .addClass( 'oo-ui-fieldLayout-field' )
6517 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
6518 .append( this.field
.$element
);
6519 this.setAlignment( config
.align
);
6524 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6525 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6530 * Handle field disable events.
6532 * @param {boolean} value Field is disabled
6534 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6535 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6539 * Handle label mouse click events.
6541 * @param {jQuery.Event} e Mouse click event
6543 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6544 this.field
.simulateLabelClick();
6551 * @return {OO.ui.Widget} Field widget
6553 OO
.ui
.FieldLayout
.prototype.getField = function () {
6558 * Set the field alignment mode.
6560 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6563 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6564 if ( value
!== this.align
) {
6565 // Default to 'left'
6566 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6570 if ( value
=== 'inline' ) {
6571 this.$element
.append( this.$field
, this.$label
, this.$help
);
6573 this.$element
.append( this.$help
, this.$label
, this.$field
);
6577 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6580 // The following classes can be used here:
6581 // oo-ui-fieldLayout-align-left
6582 // oo-ui-fieldLayout-align-right
6583 // oo-ui-fieldLayout-align-top
6584 // oo-ui-fieldLayout-align-inline
6585 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
6592 * Layout made of a fieldset and optional legend.
6594 * Just add OO.ui.FieldLayout items.
6597 * @extends OO.ui.Layout
6598 * @mixins OO.ui.LabelElement
6599 * @mixins OO.ui.IconElement
6600 * @mixins OO.ui.GroupElement
6603 * @param {Object} [config] Configuration options
6604 * @cfg {string} [icon] Symbolic icon name
6605 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6607 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6608 // Config initialization
6609 config
= config
|| {};
6611 // Parent constructor
6612 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6614 // Mixin constructors
6615 OO
.ui
.IconElement
.call( this, config
);
6616 OO
.ui
.LabelElement
.call( this, config
);
6617 OO
.ui
.GroupElement
.call( this, config
);
6621 .addClass( 'oo-ui-fieldsetLayout' )
6622 .prepend( this.$icon
, this.$label
, this.$group
);
6623 if ( $.isArray( config
.items
) ) {
6624 this.addItems( config
.items
);
6630 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6631 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6632 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6633 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6635 /* Static Properties */
6637 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
6640 * Layout with an HTML form.
6643 * @extends OO.ui.Layout
6646 * @param {Object} [config] Configuration options
6648 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6649 // Configuration initialization
6650 config
= config
|| {};
6652 // Parent constructor
6653 OO
.ui
.FormLayout
.super.call( this, config
);
6656 this.$element
.on( 'submit', OO
.ui
.bind( this.onFormSubmit
, this ) );
6659 this.$element
.addClass( 'oo-ui-formLayout' );
6664 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6672 /* Static Properties */
6674 OO
.ui
.FormLayout
.static.tagName
= 'form';
6679 * Handle form submit events.
6681 * @param {jQuery.Event} e Submit event
6684 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6685 this.emit( 'submit' );
6690 * Layout made of proportionally sized columns and rows.
6693 * @extends OO.ui.Layout
6696 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6697 * @param {Object} [config] Configuration options
6698 * @cfg {number[]} [widths] Widths of columns as ratios
6699 * @cfg {number[]} [heights] Heights of columns as ratios
6701 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6704 // Config initialization
6705 config
= config
|| {};
6707 // Parent constructor
6708 OO
.ui
.GridLayout
.super.call( this, config
);
6716 this.$element
.addClass( 'oo-ui-gridLayout' );
6717 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6718 this.panels
.push( panels
[i
] );
6719 this.$element
.append( panels
[i
].$element
);
6721 if ( config
.widths
|| config
.heights
) {
6722 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6724 // Arrange in columns by default
6726 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
6729 this.layout( widths
, [ 1 ] );
6735 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6747 /* Static Properties */
6749 OO
.ui
.GridLayout
.static.tagName
= 'div';
6754 * Set grid dimensions.
6756 * @param {number[]} widths Widths of columns as ratios
6757 * @param {number[]} heights Heights of rows as ratios
6759 * @throws {Error} If grid is not large enough to fit all panels
6761 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6765 cols
= widths
.length
,
6766 rows
= heights
.length
;
6768 // Verify grid is big enough to fit panels
6769 if ( cols
* rows
< this.panels
.length
) {
6770 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6773 // Sum up denominators
6774 for ( x
= 0; x
< cols
; x
++ ) {
6777 for ( y
= 0; y
< rows
; y
++ ) {
6783 for ( x
= 0; x
< cols
; x
++ ) {
6784 this.widths
[x
] = widths
[x
] / xd
;
6786 for ( y
= 0; y
< rows
; y
++ ) {
6787 this.heights
[y
] = heights
[y
] / yd
;
6791 this.emit( 'layout' );
6795 * Update panel positions and sizes.
6799 OO
.ui
.GridLayout
.prototype.update = function () {
6807 cols
= this.widths
.length
,
6808 rows
= this.heights
.length
;
6810 for ( y
= 0; y
< rows
; y
++ ) {
6811 height
= this.heights
[y
];
6812 for ( x
= 0; x
< cols
; x
++ ) {
6813 panel
= this.panels
[i
];
6814 width
= this.widths
[x
];
6816 width
: Math
.round( width
* 100 ) + '%',
6817 height
: Math
.round( height
* 100 ) + '%',
6818 top
: Math
.round( top
* 100 ) + '%',
6819 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6820 visibility
: width
=== 0 || height
=== 0 ? 'hidden' : ''
6823 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6824 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6826 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6828 panel
.$element
.css( dimensions
);
6836 this.emit( 'update' );
6840 * Get a panel at a given position.
6842 * The x and y position is affected by the current grid layout.
6844 * @param {number} x Horizontal position
6845 * @param {number} y Vertical position
6846 * @return {OO.ui.PanelLayout} The panel at the given postion
6848 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
6849 return this.panels
[( x
* this.widths
.length
) + y
];
6853 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
6856 * @extends OO.ui.Layout
6859 * @param {Object} [config] Configuration options
6860 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
6861 * @cfg {boolean} [padded=false] Pad the content from the edges
6862 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
6864 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
6865 // Config initialization
6866 config
= config
|| {};
6868 // Parent constructor
6869 OO
.ui
.PanelLayout
.super.call( this, config
);
6872 this.$element
.addClass( 'oo-ui-panelLayout' );
6873 if ( config
.scrollable
) {
6874 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
6877 if ( config
.padded
) {
6878 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
6881 if ( config
.expanded
=== undefined || config
.expanded
) {
6882 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
6888 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
6891 * Page within an booklet layout.
6894 * @extends OO.ui.PanelLayout
6897 * @param {string} name Unique symbolic name of page
6898 * @param {Object} [config] Configuration options
6899 * @param {string} [outlineItem] Outline item widget
6901 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
6902 // Configuration initialization
6903 config
= $.extend( { scrollable
: true }, config
);
6905 // Parent constructor
6906 OO
.ui
.PageLayout
.super.call( this, config
);
6910 this.outlineItem
= config
.outlineItem
|| null;
6911 this.active
= false;
6914 this.$element
.addClass( 'oo-ui-pageLayout' );
6919 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
6925 * @param {boolean} active Page is active
6933 * @return {string} Symbolic name of page
6935 OO
.ui
.PageLayout
.prototype.getName = function () {
6940 * Check if page is active.
6942 * @return {boolean} Page is active
6944 OO
.ui
.PageLayout
.prototype.isActive = function () {
6951 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
6953 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
6954 return this.outlineItem
;
6960 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
6961 * outline item as desired; this method is called for setting (with an object) and unsetting
6962 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
6963 * operating on null instead of an OO.ui.OutlineItemWidget object.
6965 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
6968 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
6969 this.outlineItem
= outlineItem
|| null;
6970 if ( outlineItem
) {
6971 this.setupOutlineItem();
6977 * Setup outline item.
6979 * @localdoc Subclasses should override this method to adjust the outline item as desired.
6981 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
6984 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
6989 * Set page active state.
6991 * @param {boolean} Page is active
6994 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
6997 if ( active
!== this.active
) {
6998 this.active
= active
;
6999 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
7000 this.emit( 'active', this.active
);
7005 * Layout containing a series of mutually exclusive pages.
7008 * @extends OO.ui.PanelLayout
7009 * @mixins OO.ui.GroupElement
7012 * @param {Object} [config] Configuration options
7013 * @cfg {boolean} [continuous=false] Show all pages, one after another
7014 * @cfg {string} [icon=''] Symbolic icon name
7015 * @cfg {OO.ui.Layout[]} [items] Layouts to add
7017 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
7018 // Config initialization
7019 config
= $.extend( { scrollable
: true }, config
);
7021 // Parent constructor
7022 OO
.ui
.StackLayout
.super.call( this, config
);
7024 // Mixin constructors
7025 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7028 this.currentItem
= null;
7029 this.continuous
= !!config
.continuous
;
7032 this.$element
.addClass( 'oo-ui-stackLayout' );
7033 if ( this.continuous
) {
7034 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
7036 if ( $.isArray( config
.items
) ) {
7037 this.addItems( config
.items
);
7043 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
7044 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
7050 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
7056 * Get the current item.
7058 * @return {OO.ui.Layout|null}
7060 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
7061 return this.currentItem
;
7065 * Unset the current item.
7068 * @param {OO.ui.StackLayout} layout
7071 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7072 var prevItem
= this.currentItem
;
7073 if ( prevItem
=== null ) {
7077 this.currentItem
= null;
7078 this.emit( 'set', null );
7084 * Adding an existing item (by value) will move it.
7086 * @param {OO.ui.Layout[]} items Items to add
7087 * @param {number} [index] Index to insert items after
7090 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7092 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7094 if ( !this.currentItem
&& items
.length
) {
7095 this.setItem( items
[0] );
7104 * Items will be detached, not removed, so they can be used later.
7106 * @param {OO.ui.Layout[]} items Items to remove
7110 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7112 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7114 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7115 if ( this.items
.length
) {
7116 this.setItem( this.items
[0] );
7118 this.unsetCurrentItem();
7128 * Items will be detached, not removed, so they can be used later.
7133 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7134 this.unsetCurrentItem();
7135 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7143 * Any currently shown item will be hidden.
7145 * FIXME: If the passed item to show has not been added in the items list, then
7146 * this method drops it and unsets the current item.
7148 * @param {OO.ui.Layout} item Item to show
7152 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7155 if ( item
!== this.currentItem
) {
7156 if ( !this.continuous
) {
7157 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7158 this.items
[i
].$element
.css( 'display', '' );
7161 if ( $.inArray( item
, this.items
) !== -1 ) {
7162 if ( !this.continuous
) {
7163 item
.$element
.css( 'display', 'block' );
7165 this.currentItem
= item
;
7166 this.emit( 'set', item
);
7168 this.unsetCurrentItem();
7176 * Horizontal bar layout of tools as icon buttons.
7179 * @extends OO.ui.ToolGroup
7182 * @param {OO.ui.Toolbar} toolbar
7183 * @param {Object} [config] Configuration options
7185 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7186 // Parent constructor
7187 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7190 this.$element
.addClass( 'oo-ui-barToolGroup' );
7195 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7197 /* Static Properties */
7199 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7201 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7203 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7206 * Popup list of tools with an icon and optional label.
7210 * @extends OO.ui.ToolGroup
7211 * @mixins OO.ui.IconElement
7212 * @mixins OO.ui.IndicatorElement
7213 * @mixins OO.ui.LabelElement
7214 * @mixins OO.ui.TitledElement
7215 * @mixins OO.ui.ClippableElement
7218 * @param {OO.ui.Toolbar} toolbar
7219 * @param {Object} [config] Configuration options
7220 * @cfg {string} [header] Text to display at the top of the pop-up
7222 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7223 // Configuration initialization
7224 config
= config
|| {};
7226 // Parent constructor
7227 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7229 // Mixin constructors
7230 OO
.ui
.IconElement
.call( this, config
);
7231 OO
.ui
.IndicatorElement
.call( this, config
);
7232 OO
.ui
.LabelElement
.call( this, config
);
7233 OO
.ui
.TitledElement
.call( this, config
);
7234 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7237 this.active
= false;
7238 this.dragging
= false;
7239 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
7240 this.$handle
= this.$( '<span>' );
7244 'mousedown touchstart': OO
.ui
.bind( this.onHandlePointerDown
, this ),
7245 'mouseup touchend': OO
.ui
.bind( this.onHandlePointerUp
, this )
7250 .addClass( 'oo-ui-popupToolGroup-handle' )
7251 .append( this.$icon
, this.$label
, this.$indicator
);
7252 // If the pop-up should have a header, add it to the top of the toolGroup.
7253 // Note: If this feature is useful for other widgets, we could abstract it into an
7254 // OO.ui.HeaderedElement mixin constructor.
7255 if ( config
.header
!== undefined ) {
7257 .prepend( this.$( '<span>' )
7258 .addClass( 'oo-ui-popupToolGroup-header' )
7259 .text( config
.header
)
7263 .addClass( 'oo-ui-popupToolGroup' )
7264 .prepend( this.$handle
);
7269 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7270 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7271 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7272 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7273 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7274 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7276 /* Static Properties */
7283 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7285 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7287 if ( this.isDisabled() && this.isElementAttached() ) {
7288 this.setActive( false );
7293 * Handle focus being lost.
7295 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7297 * @param {jQuery.Event} e Mouse up event
7299 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7300 // Only deactivate when clicking outside the dropdown element
7301 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7302 this.setActive( false );
7309 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7310 // e.which is 0 for touch events, 1 for left mouse button
7311 if ( !this.isDisabled() && e
.which
<= 1 ) {
7312 this.setActive( false );
7314 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7318 * Handle mouse up events.
7320 * @param {jQuery.Event} e Mouse up event
7322 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7327 * Handle mouse down events.
7329 * @param {jQuery.Event} e Mouse down event
7331 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7332 // e.which is 0 for touch events, 1 for left mouse button
7333 if ( !this.isDisabled() && e
.which
<= 1 ) {
7334 this.setActive( !this.active
);
7340 * Switch into active mode.
7342 * When active, mouseup events anywhere in the document will trigger deactivation.
7344 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7346 if ( this.active
!== value
) {
7347 this.active
= value
;
7349 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7351 // Try anchoring the popup to the left first
7352 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7353 this.toggleClipping( true );
7354 if ( this.isClippedHorizontally() ) {
7355 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7356 this.toggleClipping( false );
7358 .removeClass( 'oo-ui-popupToolGroup-left' )
7359 .addClass( 'oo-ui-popupToolGroup-right' );
7360 this.toggleClipping( true );
7363 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7364 this.$element
.removeClass(
7365 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7367 this.toggleClipping( false );
7373 * Drop down list layout of tools as labeled icon buttons.
7376 * @extends OO.ui.PopupToolGroup
7379 * @param {OO.ui.Toolbar} toolbar
7380 * @param {Object} [config] Configuration options
7382 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7383 // Parent constructor
7384 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7387 this.$element
.addClass( 'oo-ui-listToolGroup' );
7392 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7394 /* Static Properties */
7396 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7398 OO
.ui
.ListToolGroup
.static.name
= 'list';
7401 * Drop down menu layout of tools as selectable menu items.
7404 * @extends OO.ui.PopupToolGroup
7407 * @param {OO.ui.Toolbar} toolbar
7408 * @param {Object} [config] Configuration options
7410 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7411 // Configuration initialization
7412 config
= config
|| {};
7414 // Parent constructor
7415 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7418 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7421 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7426 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7428 /* Static Properties */
7430 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7432 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7437 * Handle the toolbar state being updated.
7439 * When the state changes, the title of each active item in the menu will be joined together and
7440 * used as a label for the group. The label will be empty if none of the items are active.
7442 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7446 for ( name
in this.tools
) {
7447 if ( this.tools
[name
].isActive() ) {
7448 labelTexts
.push( this.tools
[name
].getTitle() );
7452 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7456 * Tool that shows a popup when selected.
7460 * @extends OO.ui.Tool
7461 * @mixins OO.ui.PopupElement
7464 * @param {OO.ui.Toolbar} toolbar
7465 * @param {Object} [config] Configuration options
7467 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7468 // Parent constructor
7469 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7471 // Mixin constructors
7472 OO
.ui
.PopupElement
.call( this, config
);
7476 .addClass( 'oo-ui-popupTool' )
7477 .append( this.popup
.$element
);
7482 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7483 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7488 * Handle the tool being selected.
7492 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7493 if ( !this.isDisabled() ) {
7494 this.popup
.toggle();
7496 this.setActive( false );
7501 * Handle the toolbar state being updated.
7505 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7506 this.setActive( false );
7510 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7512 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7516 * @extends OO.ui.GroupElement
7519 * @param {Object} [config] Configuration options
7521 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7522 // Parent constructor
7523 OO
.ui
.GroupWidget
.super.call( this, config
);
7528 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7533 * Set the disabled state of the widget.
7535 * This will also update the disabled state of child widgets.
7537 * @param {boolean} disabled Disable widget
7540 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7544 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7545 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7547 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7549 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7550 this.items
[i
].updateDisabled();
7558 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7560 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7561 * allows bidrectional communication.
7563 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7570 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7577 * Check if widget is disabled.
7579 * Checks parent if present, making disabled state inheritable.
7581 * @return {boolean} Widget is disabled
7583 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7584 return this.disabled
||
7585 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7589 * Set group element is in.
7591 * @param {OO.ui.GroupElement|null} group Group element, null if none
7594 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7596 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7597 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7599 // Initialize item disabled states
7600 this.updateDisabled();
7606 * Mixin that adds a menu showing suggested values for a text input.
7608 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7614 * @param {OO.ui.TextInputWidget} input Input widget
7615 * @param {Object} [config] Configuration options
7616 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
7618 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7619 // Config intialization
7620 config
= config
|| {};
7623 this.lookupInput
= input
;
7624 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
7625 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7626 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7627 input
: this.lookupInput
,
7628 $container
: config
.$container
7630 this.lookupCache
= {};
7631 this.lookupQuery
= null;
7632 this.lookupRequest
= null;
7633 this.populating
= false;
7636 this.$overlay
.append( this.lookupMenu
.$element
);
7638 this.lookupInput
.$input
.on( {
7639 focus
: OO
.ui
.bind( this.onLookupInputFocus
, this ),
7640 blur
: OO
.ui
.bind( this.onLookupInputBlur
, this ),
7641 mousedown
: OO
.ui
.bind( this.onLookupInputMouseDown
, this )
7643 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7646 this.$element
.addClass( 'oo-ui-lookupWidget' );
7647 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7653 * Handle input focus event.
7655 * @param {jQuery.Event} e Input focus event
7657 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7658 this.openLookupMenu();
7662 * Handle input blur event.
7664 * @param {jQuery.Event} e Input blur event
7666 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7667 this.lookupMenu
.toggle( false );
7671 * Handle input mouse down event.
7673 * @param {jQuery.Event} e Input mouse down event
7675 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7676 this.openLookupMenu();
7680 * Handle input change event.
7682 * @param {string} value New input value
7684 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7685 this.openLookupMenu();
7691 * @return {OO.ui.TextInputMenuWidget}
7693 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
7694 return this.lookupMenu
;
7702 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
7703 var value
= this.lookupInput
.getValue();
7705 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
7706 this.populateLookupMenu();
7707 this.lookupMenu
.toggle( true );
7718 * Populate lookup menu with current information.
7722 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
7725 if ( !this.populating
) {
7726 this.populating
= true;
7727 this.getLookupMenuItems()
7728 .done( function ( items
) {
7729 widget
.lookupMenu
.clearItems();
7730 if ( items
.length
) {
7734 widget
.initializeLookupMenuSelection();
7735 widget
.openLookupMenu();
7737 widget
.lookupMenu
.toggle( true );
7739 widget
.populating
= false;
7741 .fail( function () {
7742 widget
.lookupMenu
.clearItems();
7743 widget
.populating
= false;
7751 * Set selection in the lookup menu with current information.
7755 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
7756 if ( !this.lookupMenu
.getSelectedItem() ) {
7757 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
7759 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
7763 * Get lookup menu items for the current query.
7765 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
7768 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
7770 value
= this.lookupInput
.getValue(),
7771 deferred
= $.Deferred();
7773 if ( value
&& value
!== this.lookupQuery
) {
7774 // Abort current request if query has changed
7775 if ( this.lookupRequest
) {
7776 this.lookupRequest
.abort();
7777 this.lookupQuery
= null;
7778 this.lookupRequest
= null;
7780 if ( value
in this.lookupCache
) {
7781 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
7783 this.lookupQuery
= value
;
7784 this.lookupRequest
= this.getLookupRequest()
7785 .always( function () {
7786 widget
.lookupQuery
= null;
7787 widget
.lookupRequest
= null;
7789 .done( function ( data
) {
7790 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
7791 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
7793 .fail( function () {
7797 this.lookupRequest
.always( function () {
7798 widget
.popPending();
7802 return deferred
.promise();
7806 * Get a new request object of the current lookup query value.
7809 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
7811 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
7812 // Stub, implemented in subclass
7817 * Handle successful lookup request.
7819 * Overriding methods should call #populateLookupMenu when results are available and cache results
7820 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
7823 * @param {Mixed} data Response from server
7825 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
7826 // Stub, implemented in subclass
7830 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
7833 * @param {Mixed} data Cached result data, usually an array
7834 * @return {OO.ui.MenuItemWidget[]} Menu items
7836 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
7837 // Stub, implemented in subclass
7842 * Set of controls for an OO.ui.OutlineWidget.
7844 * Controls include moving items up and down, removing items, and adding different kinds of items.
7847 * @extends OO.ui.Widget
7848 * @mixins OO.ui.GroupElement
7849 * @mixins OO.ui.IconElement
7852 * @param {OO.ui.OutlineWidget} outline Outline to control
7853 * @param {Object} [config] Configuration options
7855 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7856 // Configuration initialization
7857 config
= $.extend( { icon
: 'add-item' }, config
);
7859 // Parent constructor
7860 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7862 // Mixin constructors
7863 OO
.ui
.GroupElement
.call( this, config
);
7864 OO
.ui
.IconElement
.call( this, config
);
7867 this.outline
= outline
;
7868 this.$movers
= this.$( '<div>' );
7869 this.upButton
= new OO
.ui
.ButtonWidget( {
7873 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
7875 this.downButton
= new OO
.ui
.ButtonWidget( {
7879 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
7881 this.removeButton
= new OO
.ui
.ButtonWidget( {
7885 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
7889 outline
.connect( this, {
7890 select
: 'onOutlineChange',
7891 add
: 'onOutlineChange',
7892 remove
: 'onOutlineChange'
7894 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
7895 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
7896 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
7899 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7900 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7902 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7903 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7904 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7909 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7910 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7911 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
7917 * @param {number} places Number of places to move
7927 * Handle outline change events.
7929 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7930 var i
, len
, firstMovable
, lastMovable
,
7931 items
= this.outline
.getItems(),
7932 selectedItem
= this.outline
.getSelectedItem(),
7933 movable
= selectedItem
&& selectedItem
.isMovable(),
7934 removable
= selectedItem
&& selectedItem
.isRemovable();
7939 while ( ++i
< len
) {
7940 if ( items
[i
].isMovable() ) {
7941 firstMovable
= items
[i
];
7947 if ( items
[i
].isMovable() ) {
7948 lastMovable
= items
[i
];
7953 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7954 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7955 this.removeButton
.setDisabled( !removable
);
7959 * Mixin for widgets with a boolean on/off state.
7965 * @param {Object} [config] Configuration options
7966 * @cfg {boolean} [value=false] Initial value
7968 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7969 // Configuration initialization
7970 config
= config
|| {};
7976 this.$element
.addClass( 'oo-ui-toggleWidget' );
7977 this.setValue( !!config
.value
);
7984 * @param {boolean} value Changed value
7990 * Get the value of the toggle.
7994 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7999 * Set the value of the toggle.
8001 * @param {boolean} value New value
8005 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8007 if ( this.value
!== value
) {
8009 this.emit( 'change', value
);
8010 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8011 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8017 * Group widget for multiple related buttons.
8019 * Use together with OO.ui.ButtonWidget.
8022 * @extends OO.ui.Widget
8023 * @mixins OO.ui.GroupElement
8026 * @param {Object} [config] Configuration options
8027 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
8029 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
8030 // Parent constructor
8031 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
8033 // Mixin constructors
8034 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8037 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
8038 if ( $.isArray( config
.items
) ) {
8039 this.addItems( config
.items
);
8045 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
8046 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
8049 * Generic widget for buttons.
8052 * @extends OO.ui.Widget
8053 * @mixins OO.ui.ButtonElement
8054 * @mixins OO.ui.IconElement
8055 * @mixins OO.ui.IndicatorElement
8056 * @mixins OO.ui.LabelElement
8057 * @mixins OO.ui.TitledElement
8058 * @mixins OO.ui.FlaggedElement
8061 * @param {Object} [config] Configuration options
8062 * @cfg {string} [href] Hyperlink to visit when clicked
8063 * @cfg {string} [target] Target to open hyperlink in
8065 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8066 // Configuration initialization
8067 config
= $.extend( { target
: '_blank' }, config
);
8069 // Parent constructor
8070 OO
.ui
.ButtonWidget
.super.call( this, config
);
8072 // Mixin constructors
8073 OO
.ui
.ButtonElement
.call( this, config
);
8074 OO
.ui
.IconElement
.call( this, config
);
8075 OO
.ui
.IndicatorElement
.call( this, config
);
8076 OO
.ui
.LabelElement
.call( this, config
);
8077 OO
.ui
.TitledElement
.call( this, config
, $.extend( {}, config
, { $titled
: this.$button
} ) );
8078 OO
.ui
.FlaggedElement
.call( this, config
);
8083 this.isHyperlink
= false;
8087 click
: OO
.ui
.bind( this.onClick
, this ),
8088 keypress
: OO
.ui
.bind( this.onKeyPress
, this )
8092 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8094 .addClass( 'oo-ui-buttonWidget' )
8095 .append( this.$button
);
8096 this.setHref( config
.href
);
8097 this.setTarget( config
.target
);
8102 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8103 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8104 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8105 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8106 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8107 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8108 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8119 * Handles mouse click events.
8121 * @param {jQuery.Event} e Mouse click event
8124 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8125 if ( !this.isDisabled() ) {
8126 this.emit( 'click' );
8127 if ( this.isHyperlink
) {
8135 * Handles keypress events.
8137 * @param {jQuery.Event} e Keypress event
8140 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8141 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8143 if ( this.isHyperlink
) {
8151 * Get hyperlink location.
8153 * @return {string} Hyperlink location
8155 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8160 * Get hyperlink target.
8162 * @return {string} Hyperlink target
8164 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8169 * Set hyperlink location.
8171 * @param {string|null} href Hyperlink location, null to remove
8173 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8174 href
= typeof href
=== 'string' ? href
: null;
8176 if ( href
!== this.href
) {
8178 if ( href
!== null ) {
8179 this.$button
.attr( 'href', href
);
8180 this.isHyperlink
= true;
8182 this.$button
.removeAttr( 'href' );
8183 this.isHyperlink
= false;
8191 * Set hyperlink target.
8193 * @param {string|null} target Hyperlink target, null to remove
8195 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8196 target
= typeof target
=== 'string' ? target
: null;
8198 if ( target
!== this.target
) {
8199 this.target
= target
;
8200 if ( target
!== null ) {
8201 this.$button
.attr( 'target', target
);
8203 this.$button
.removeAttr( 'target' );
8211 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8214 * @extends OO.ui.ButtonWidget
8215 * @mixins OO.ui.PendingElement
8218 * @param {Object} [config] Configuration options
8219 * @cfg {string} [action] Symbolic action name
8220 * @cfg {string[]} [modes] Symbolic mode names
8221 * @cfg {boolean} [framed=false] Render button with a frame
8223 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8224 // Config intialization
8225 config
= $.extend( { framed
: false }, config
);
8227 // Parent constructor
8228 OO
.ui
.ActionWidget
.super.call( this, config
);
8230 // Mixin constructors
8231 OO
.ui
.PendingElement
.call( this, config
);
8234 this.action
= config
.action
|| '';
8235 this.modes
= config
.modes
|| [];
8240 this.$element
.addClass( 'oo-ui-actionWidget' );
8245 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8246 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
8257 * Check if action is available in a certain mode.
8259 * @param {string} mode Name of mode
8260 * @return {boolean} Has mode
8262 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8263 return this.modes
.indexOf( mode
) !== -1;
8267 * Get symbolic action name.
8271 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8276 * Get symbolic action name.
8280 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8281 return this.modes
.slice();
8285 * Emit a resize event if the size has changed.
8289 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8292 if ( this.isElementAttached() ) {
8293 width
= this.$element
.width();
8294 height
= this.$element
.height();
8296 if ( width
!== this.width
|| height
!== this.height
) {
8298 this.height
= height
;
8299 this.emit( 'resize' );
8309 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8311 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8312 this.propagateResize();
8320 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8322 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8323 this.propagateResize();
8331 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8333 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8334 this.propagateResize();
8342 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8344 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8345 this.propagateResize();
8351 * Toggle visibility of button.
8353 * @param {boolean} [show] Show button, omit to toggle visibility
8356 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8358 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8359 this.propagateResize();
8365 * Button that shows and hides a popup.
8368 * @extends OO.ui.ButtonWidget
8369 * @mixins OO.ui.PopupElement
8372 * @param {Object} [config] Configuration options
8374 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8375 // Parent constructor
8376 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8378 // Mixin constructors
8379 OO
.ui
.PopupElement
.call( this, config
);
8383 .addClass( 'oo-ui-popupButtonWidget' )
8384 .append( this.popup
.$element
);
8389 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8390 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8395 * Handles mouse click events.
8397 * @param {jQuery.Event} e Mouse click event
8399 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8400 // Skip clicks within the popup
8401 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8405 if ( !this.isDisabled() ) {
8406 this.popup
.toggle();
8408 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8414 * Button that toggles on and off.
8417 * @extends OO.ui.ButtonWidget
8418 * @mixins OO.ui.ToggleWidget
8421 * @param {Object} [config] Configuration options
8422 * @cfg {boolean} [value=false] Initial value
8424 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8425 // Configuration initialization
8426 config
= config
|| {};
8428 // Parent constructor
8429 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8431 // Mixin constructors
8432 OO
.ui
.ToggleWidget
.call( this, config
);
8435 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8440 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8441 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8448 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8449 if ( !this.isDisabled() ) {
8450 this.setValue( !this.value
);
8454 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8460 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8462 if ( value
!== this.value
) {
8463 this.setActive( value
);
8466 // Parent method (from mixin)
8467 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8475 * See OO.ui.IconElement for more information.
8478 * @extends OO.ui.Widget
8479 * @mixins OO.ui.IconElement
8480 * @mixins OO.ui.TitledElement
8483 * @param {Object} [config] Configuration options
8485 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8486 // Config intialization
8487 config
= config
|| {};
8489 // Parent constructor
8490 OO
.ui
.IconWidget
.super.call( this, config
);
8492 // Mixin constructors
8493 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
8494 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8497 this.$element
.addClass( 'oo-ui-iconWidget' );
8502 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8503 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
8504 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8506 /* Static Properties */
8508 OO
.ui
.IconWidget
.static.tagName
= 'span';
8513 * See OO.ui.IndicatorElement for more information.
8516 * @extends OO.ui.Widget
8517 * @mixins OO.ui.IndicatorElement
8518 * @mixins OO.ui.TitledElement
8521 * @param {Object} [config] Configuration options
8523 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8524 // Config intialization
8525 config
= config
|| {};
8527 // Parent constructor
8528 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8530 // Mixin constructors
8531 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
8532 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8535 this.$element
.addClass( 'oo-ui-indicatorWidget' );
8540 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
8541 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
8542 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
8544 /* Static Properties */
8546 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
8549 * Inline menu of options.
8551 * Inline menus provide a control for accessing a menu and compose a menu within the widget, which
8552 * can be accessed using the #getMenu method.
8554 * Use with OO.ui.MenuOptionWidget.
8557 * @extends OO.ui.Widget
8558 * @mixins OO.ui.IconElement
8559 * @mixins OO.ui.IndicatorElement
8560 * @mixins OO.ui.LabelElement
8561 * @mixins OO.ui.TitledElement
8564 * @param {Object} [config] Configuration options
8565 * @cfg {Object} [menu] Configuration options to pass to menu widget
8567 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
8568 // Configuration initialization
8569 config
= $.extend( { indicator
: 'down' }, config
);
8571 // Parent constructor
8572 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
8574 // Mixin constructors
8575 OO
.ui
.IconElement
.call( this, config
);
8576 OO
.ui
.IndicatorElement
.call( this, config
);
8577 OO
.ui
.LabelElement
.call( this, config
);
8578 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8581 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8582 this.$handle
= this.$( '<span>' );
8585 this.$element
.on( { click
: OO
.ui
.bind( this.onClick
, this ) } );
8586 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8590 .addClass( 'oo-ui-inlineMenuWidget-handle' )
8591 .append( this.$icon
, this.$label
, this.$indicator
);
8593 .addClass( 'oo-ui-inlineMenuWidget' )
8594 .append( this.$handle
, this.menu
.$element
);
8599 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
8600 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconElement
);
8601 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatorElement
);
8602 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabelElement
);
8603 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
8610 * @return {OO.ui.MenuWidget} Menu of widget
8612 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
8617 * Handles menu select events.
8619 * @param {OO.ui.MenuItemWidget} item Selected menu item
8621 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
8628 selectedLabel
= item
.getLabel();
8630 // If the label is a DOM element, clone it, because setLabel will append() it
8631 if ( selectedLabel
instanceof jQuery
) {
8632 selectedLabel
= selectedLabel
.clone();
8635 this.setLabel( selectedLabel
);
8639 * Handles mouse click events.
8641 * @param {jQuery.Event} e Mouse click event
8643 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
8644 // Skip clicks within the menu
8645 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8649 if ( !this.isDisabled() ) {
8650 if ( this.menu
.isVisible() ) {
8651 this.menu
.toggle( false );
8653 this.menu
.toggle( true );
8660 * Base class for input widgets.
8664 * @extends OO.ui.Widget
8665 * @mixins OO.ui.FlaggedElement
8668 * @param {Object} [config] Configuration options
8669 * @cfg {string} [name=''] HTML input name
8670 * @cfg {string} [value=''] Input value
8671 * @cfg {boolean} [readOnly=false] Prevent changes
8672 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
8674 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8675 // Config intialization
8676 config
= $.extend( { readOnly
: false }, config
);
8678 // Parent constructor
8679 OO
.ui
.InputWidget
.super.call( this, config
);
8681 // Mixin constructors
8682 OO
.ui
.FlaggedElement
.call( this, config
);
8685 this.$input
= this.getInputElement( config
);
8687 this.readOnly
= false;
8688 this.inputFilter
= config
.inputFilter
;
8691 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
8695 .attr( 'name', config
.name
)
8696 .prop( 'disabled', this.isDisabled() );
8697 this.setReadOnly( config
.readOnly
);
8698 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
8699 this.setValue( config
.value
);
8704 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8705 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
8717 * Get input element.
8719 * @param {Object} [config] Configuration options
8720 * @return {jQuery} Input element
8722 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8723 return this.$( '<input>' );
8727 * Handle potentially value-changing events.
8729 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8731 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8733 if ( !this.isDisabled() ) {
8734 // Allow the stack to clear so the value will be updated
8735 setTimeout( function () {
8736 widget
.setValue( widget
.$input
.val() );
8742 * Get the value of the input.
8744 * @return {string} Input value
8746 OO
.ui
.InputWidget
.prototype.getValue = function () {
8751 * Sets the direction of the current input, either RTL or LTR
8753 * @param {boolean} isRTL
8755 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
8757 this.$input
.removeClass( 'oo-ui-ltr' );
8758 this.$input
.addClass( 'oo-ui-rtl' );
8760 this.$input
.removeClass( 'oo-ui-rtl' );
8761 this.$input
.addClass( 'oo-ui-ltr' );
8766 * Set the value of the input.
8768 * @param {string} value New value
8772 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
8773 value
= this.sanitizeValue( value
);
8774 if ( this.value
!== value
) {
8776 this.emit( 'change', this.value
);
8778 // Update the DOM if it has changed. Note that with sanitizeValue, it
8779 // is possible for the DOM value to change without this.value changing.
8780 if ( this.$input
.val() !== this.value
) {
8781 this.$input
.val( this.value
);
8787 * Sanitize incoming value.
8789 * Ensures value is a string, and converts undefined and null to empty strings.
8791 * @param {string} value Original value
8792 * @return {string} Sanitized value
8794 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
8795 if ( value
=== undefined || value
=== null ) {
8797 } else if ( this.inputFilter
) {
8798 return this.inputFilter( String( value
) );
8800 return String( value
);
8805 * Simulate the behavior of clicking on a label bound to this input.
8807 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
8808 if ( !this.isDisabled() ) {
8809 if ( this.$input
.is( ':checkbox,:radio' ) ) {
8810 this.$input
.click();
8811 } else if ( this.$input
.is( ':input' ) ) {
8812 this.$input
[0].focus();
8818 * Check if the widget is read-only.
8822 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
8823 return this.readOnly
;
8827 * Set the read-only state of the widget.
8829 * This should probably change the widgets's appearance and prevent it from being used.
8831 * @param {boolean} state Make input read-only
8834 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
8835 this.readOnly
= !!state
;
8836 this.$input
.prop( 'readOnly', this.readOnly
);
8843 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
8844 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
8845 if ( this.$input
) {
8846 this.$input
.prop( 'disabled', this.isDisabled() );
8856 OO
.ui
.InputWidget
.prototype.focus = function () {
8857 this.$input
[0].focus();
8866 OO
.ui
.InputWidget
.prototype.blur = function () {
8867 this.$input
[0].blur();
8872 * Checkbox input widget.
8875 * @extends OO.ui.InputWidget
8878 * @param {Object} [config] Configuration options
8880 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
8881 // Parent constructor
8882 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
8885 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
8890 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
8897 * Get input element.
8899 * @return {jQuery} Input element
8901 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
8902 return this.$( '<input type="checkbox" />' );
8906 * Get checked state of the checkbox
8908 * @return {boolean} If the checkbox is checked
8910 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
8917 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
8919 if ( this.value
!== value
) {
8921 this.$input
.prop( 'checked', this.value
);
8922 this.emit( 'change', this.value
);
8929 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
8931 if ( !this.isDisabled() ) {
8932 // Allow the stack to clear so the value will be updated
8933 setTimeout( function () {
8934 widget
.setValue( widget
.$input
.prop( 'checked' ) );
8940 * Input widget with a text field.
8943 * @extends OO.ui.InputWidget
8944 * @mixins OO.ui.IconElement
8945 * @mixins OO.ui.IndicatorElement
8946 * @mixins OO.ui.PendingElement
8949 * @param {Object} [config] Configuration options
8950 * @cfg {string} [placeholder] Placeholder text
8951 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8952 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8953 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8954 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
8955 * one, see #static-validationPatterns)
8957 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8958 // Configuration initialization
8959 config
= config
|| {};
8961 // Parent constructor
8962 OO
.ui
.TextInputWidget
.super.call( this, config
);
8964 // Mixin constructors
8965 OO
.ui
.IconElement
.call( this, config
);
8966 OO
.ui
.IndicatorElement
.call( this, config
);
8967 OO
.ui
.PendingElement
.call( this, config
);
8970 this.multiline
= !!config
.multiline
;
8971 this.autosize
= !!config
.autosize
;
8972 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
8973 this.validate
= config
.validate
|| null;
8977 keypress
: OO
.ui
.bind( this.onKeyPress
, this ),
8978 blur
: OO
.ui
.bind( this.setValidityFlag
, this )
8980 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8981 this.$icon
.on( 'mousedown', OO
.ui
.bind( this.onIconMouseDown
, this ) );
8982 this.$indicator
.on( 'mousedown', OO
.ui
.bind( this.onIndicatorMouseDown
, this ) );
8986 .addClass( 'oo-ui-textInputWidget' )
8987 .append( this.$icon
, this.$indicator
);
8988 if ( config
.placeholder
) {
8989 this.$input
.attr( 'placeholder', config
.placeholder
);
8991 this.$element
.attr( 'role', 'textbox' );
8996 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8997 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
8998 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
8999 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
9001 /* Static properties */
9003 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
9011 * User presses enter inside the text box.
9013 * Not called if input is multiline.
9019 * User clicks the icon.
9025 * User clicks the indicator.
9033 * Handle icon mouse down events.
9035 * @param {jQuery.Event} e Mouse down event
9038 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
9039 if ( e
.which
=== 1 ) {
9040 this.$input
[0].focus();
9041 this.emit( 'icon' );
9047 * Handle indicator mouse down events.
9049 * @param {jQuery.Event} e Mouse down event
9052 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
9053 if ( e
.which
=== 1 ) {
9054 this.$input
[0].focus();
9055 this.emit( 'indicator' );
9061 * Handle key press events.
9063 * @param {jQuery.Event} e Key press event
9064 * @fires enter If enter key is pressed and input is not multiline
9066 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
9067 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
9068 this.emit( 'enter' );
9073 * Handle element attach events.
9075 * @param {jQuery.Event} e Element attach event
9077 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
9084 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
9088 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9094 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9096 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9098 this.setValidityFlag();
9104 * Automatically adjust the size of the text input.
9106 * This only affects multi-line inputs that are auto-sized.
9110 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9111 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
9113 if ( this.multiline
&& this.autosize
) {
9114 $clone
= this.$input
.clone()
9115 .val( this.$input
.val() )
9116 .css( { height
: 0 } )
9117 .insertAfter( this.$input
);
9118 // Set inline height property to 0 to measure scroll height
9119 scrollHeight
= $clone
[0].scrollHeight
;
9120 // Remove inline height property to measure natural heights
9121 $clone
.css( 'height', '' );
9122 innerHeight
= $clone
.innerHeight();
9123 outerHeight
= $clone
.outerHeight();
9124 // Measure max rows height
9125 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
9126 maxInnerHeight
= $clone
.innerHeight();
9127 $clone
.removeAttr( 'rows' ).css( 'height', '' );
9129 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
9130 // Only apply inline height when expansion beyond natural height is needed
9133 // Use the difference between the inner and outer height as a buffer
9134 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
9141 * Get input element.
9143 * @param {Object} [config] Configuration options
9144 * @return {jQuery} Input element
9146 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9147 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
9153 * Check if input supports multiple lines.
9157 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9158 return !!this.multiline
;
9162 * Check if input automatically adjusts its size.
9166 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9167 return !!this.autosize
;
9171 * Select the contents of the input.
9175 OO
.ui
.TextInputWidget
.prototype.select = function () {
9176 this.$input
.select();
9181 * Sets the 'invalid' flag appropriately.
9183 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
9184 this.isValid().done( OO
.ui
.bind( function ( valid
) {
9185 this.setFlags( { invalid
: !valid
} );
9190 * Returns whether or not the current value is considered valid, according to the
9191 * supplied validation pattern.
9193 * @return {jQuery.Deferred}
9195 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
9196 var validationRegexp
;
9197 if ( this.validate
instanceof RegExp
) {
9198 validationRegexp
= this.validate
;
9200 validationRegexp
= this.constructor.static.validationPatterns
[this.validate
];
9202 return $.Deferred().resolve( !!this.getValue().match( validationRegexp
) ).promise();
9206 * Text input with a menu of optional values.
9209 * @extends OO.ui.Widget
9212 * @param {Object} [config] Configuration options
9213 * @cfg {Object} [menu] Configuration options to pass to menu widget
9214 * @cfg {Object} [input] Configuration options to pass to input widget
9216 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9217 // Configuration initialization
9218 config
= config
|| {};
9220 // Parent constructor
9221 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9224 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9225 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9228 this.menu
= new OO
.ui
.MenuWidget( $.extend(
9229 { $: this.$, widget
: this, input
: this.input
, disabled
: this.isDisabled() },
9234 this.input
.connect( this, {
9235 change
: 'onInputChange',
9236 indicator
: 'onInputIndicator',
9237 enter
: 'onInputEnter'
9239 this.menu
.connect( this, {
9240 choose
: 'onMenuChoose',
9241 add
: 'onMenuItemsChange',
9242 remove
: 'onMenuItemsChange'
9246 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append(
9247 this.input
.$element
,
9250 this.onMenuItemsChange();
9255 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9260 * Handle input change events.
9262 * @param {string} value New value
9264 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9265 var match
= this.menu
.getItemFromData( value
);
9267 this.menu
.selectItem( match
);
9269 if ( !this.isDisabled() ) {
9270 this.menu
.toggle( true );
9275 * Handle input indicator events.
9277 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9278 if ( !this.isDisabled() ) {
9284 * Handle input enter events.
9286 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
9287 if ( !this.isDisabled() ) {
9288 this.menu
.toggle( false );
9293 * Handle menu choose events.
9295 * @param {OO.ui.OptionWidget} item Chosen item
9297 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
9299 this.input
.setValue( item
.getData() );
9304 * Handle menu item change events.
9306 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
9307 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
9313 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
9315 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
9318 this.input
.setDisabled( this.isDisabled() );
9321 this.menu
.setDisabled( this.isDisabled() );
9331 * @extends OO.ui.Widget
9332 * @mixins OO.ui.LabelElement
9335 * @param {Object} [config] Configuration options
9337 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
9338 // Config intialization
9339 config
= config
|| {};
9341 // Parent constructor
9342 OO
.ui
.LabelWidget
.super.call( this, config
);
9344 // Mixin constructors
9345 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
9348 this.input
= config
.input
;
9351 if ( this.input
instanceof OO
.ui
.InputWidget
) {
9352 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
9356 this.$element
.addClass( 'oo-ui-labelWidget' );
9361 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
9362 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
9364 /* Static Properties */
9366 OO
.ui
.LabelWidget
.static.tagName
= 'span';
9371 * Handles label mouse click events.
9373 * @param {jQuery.Event} e Mouse click event
9375 OO
.ui
.LabelWidget
.prototype.onClick = function () {
9376 this.input
.simulateLabelClick();
9381 * Generic option widget for use with OO.ui.SelectWidget.
9384 * @extends OO.ui.Widget
9385 * @mixins OO.ui.LabelElement
9386 * @mixins OO.ui.FlaggedElement
9389 * @param {Mixed} data Option data
9390 * @param {Object} [config] Configuration options
9391 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
9393 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
9394 // Config intialization
9395 config
= config
|| {};
9397 // Parent constructor
9398 OO
.ui
.OptionWidget
.super.call( this, config
);
9400 // Mixin constructors
9401 OO
.ui
.ItemWidget
.call( this );
9402 OO
.ui
.LabelElement
.call( this, config
);
9403 OO
.ui
.FlaggedElement
.call( this, config
);
9407 this.selected
= false;
9408 this.highlighted
= false;
9409 this.pressed
= false;
9413 .data( 'oo-ui-optionWidget', this )
9414 .attr( 'rel', config
.rel
)
9415 .attr( 'role', 'option' )
9416 .addClass( 'oo-ui-optionWidget' )
9417 .append( this.$label
);
9419 .prepend( this.$icon
)
9420 .append( this.$indicator
);
9425 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
9426 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
9427 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
9428 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
9430 /* Static Properties */
9432 OO
.ui
.OptionWidget
.static.selectable
= true;
9434 OO
.ui
.OptionWidget
.static.highlightable
= true;
9436 OO
.ui
.OptionWidget
.static.pressable
= true;
9438 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
9443 * Check if option can be selected.
9445 * @return {boolean} Item is selectable
9447 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
9448 return this.constructor.static.selectable
&& !this.isDisabled();
9452 * Check if option can be highlighted.
9454 * @return {boolean} Item is highlightable
9456 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
9457 return this.constructor.static.highlightable
&& !this.isDisabled();
9461 * Check if option can be pressed.
9463 * @return {boolean} Item is pressable
9465 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
9466 return this.constructor.static.pressable
&& !this.isDisabled();
9470 * Check if option is selected.
9472 * @return {boolean} Item is selected
9474 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
9475 return this.selected
;
9479 * Check if option is highlighted.
9481 * @return {boolean} Item is highlighted
9483 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
9484 return this.highlighted
;
9488 * Check if option is pressed.
9490 * @return {boolean} Item is pressed
9492 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
9493 return this.pressed
;
9497 * Set selected state.
9499 * @param {boolean} [state=false] Select option
9502 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
9503 if ( this.constructor.static.selectable
) {
9504 this.selected
= !!state
;
9505 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
9506 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
9507 this.scrollElementIntoView();
9514 * Set highlighted state.
9516 * @param {boolean} [state=false] Highlight option
9519 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
9520 if ( this.constructor.static.highlightable
) {
9521 this.highlighted
= !!state
;
9522 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
9528 * Set pressed state.
9530 * @param {boolean} [state=false] Press option
9533 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
9534 if ( this.constructor.static.pressable
) {
9535 this.pressed
= !!state
;
9536 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
9542 * Make the option's highlight flash.
9544 * While flashing, the visual style of the pressed state is removed if present.
9546 * @return {jQuery.Promise} Promise resolved when flashing is done
9548 OO
.ui
.OptionWidget
.prototype.flash = function () {
9550 $element
= this.$element
,
9551 deferred
= $.Deferred();
9553 if ( !this.isDisabled() && this.constructor.static.pressable
) {
9554 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
9555 setTimeout( function () {
9556 // Restore original classes
9558 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
9559 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
9561 setTimeout( function () {
9568 return deferred
.promise();
9574 * @return {Mixed} Option data
9576 OO
.ui
.OptionWidget
.prototype.getData = function () {
9581 * Option widget with an option icon and indicator.
9583 * Use together with OO.ui.SelectWidget.
9586 * @extends OO.ui.OptionWidget
9587 * @mixins OO.ui.IconElement
9588 * @mixins OO.ui.IndicatorElement
9591 * @param {Mixed} data Option data
9592 * @param {Object} [config] Configuration options
9594 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
9595 // Parent constructor
9596 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
9598 // Mixin constructors
9599 OO
.ui
.IconElement
.call( this, config
);
9600 OO
.ui
.IndicatorElement
.call( this, config
);
9604 .addClass( 'oo-ui-decoratedOptionWidget' )
9605 .prepend( this.$icon
)
9606 .append( this.$indicator
);
9611 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
9612 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
9613 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
9616 * Option widget that looks like a button.
9618 * Use together with OO.ui.ButtonSelectWidget.
9621 * @extends OO.ui.DecoratedOptionWidget
9622 * @mixins OO.ui.ButtonElement
9625 * @param {Mixed} data Option data
9626 * @param {Object} [config] Configuration options
9628 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
9629 // Parent constructor
9630 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
9632 // Mixin constructors
9633 OO
.ui
.ButtonElement
.call( this, config
);
9636 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
9637 this.$button
.append( this.$element
.contents() );
9638 this.$element
.append( this.$button
);
9643 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
9644 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
9646 /* Static Properties */
9648 // Allow button mouse down events to pass through so they can be handled by the parent select widget
9649 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
9656 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
9657 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
9659 if ( this.constructor.static.selectable
) {
9660 this.setActive( state
);
9667 * Item of an OO.ui.MenuWidget.
9670 * @extends OO.ui.DecoratedOptionWidget
9673 * @param {Mixed} data Item data
9674 * @param {Object} [config] Configuration options
9676 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
9677 // Configuration initialization
9678 config
= $.extend( { icon
: 'check' }, config
);
9680 // Parent constructor
9681 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
9685 .attr( 'role', 'menuitem' )
9686 .addClass( 'oo-ui-menuItemWidget' );
9691 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9694 * Section to group one or more items in a OO.ui.MenuWidget.
9697 * @extends OO.ui.DecoratedOptionWidget
9700 * @param {Mixed} data Item data
9701 * @param {Object} [config] Configuration options
9703 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
9704 // Parent constructor
9705 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
9708 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
9713 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9715 /* Static Properties */
9717 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
9719 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
9722 * Items for an OO.ui.OutlineWidget.
9725 * @extends OO.ui.DecoratedOptionWidget
9728 * @param {Mixed} data Item data
9729 * @param {Object} [config] Configuration options
9730 * @cfg {number} [level] Indentation level
9731 * @cfg {boolean} [movable] Allow modification from outline controls
9733 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
9734 // Config intialization
9735 config
= config
|| {};
9737 // Parent constructor
9738 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
9742 this.movable
= !!config
.movable
;
9743 this.removable
= !!config
.removable
;
9746 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
9747 this.setLevel( config
.level
);
9752 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9754 /* Static Properties */
9756 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
9758 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
9760 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
9762 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
9767 * Check if item is movable.
9769 * Movablilty is used by outline controls.
9771 * @return {boolean} Item is movable
9773 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
9774 return this.movable
;
9778 * Check if item is removable.
9780 * Removablilty is used by outline controls.
9782 * @return {boolean} Item is removable
9784 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
9785 return this.removable
;
9789 * Get indentation level.
9791 * @return {number} Indentation level
9793 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
9800 * Movablilty is used by outline controls.
9802 * @param {boolean} movable Item is movable
9805 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
9806 this.movable
= !!movable
;
9813 * Removablilty is used by outline controls.
9815 * @param {boolean} movable Item is removable
9818 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
9819 this.removable
= !!removable
;
9824 * Set indentation level.
9826 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
9829 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
9830 var levels
= this.constructor.static.levels
,
9831 levelClass
= this.constructor.static.levelClass
,
9834 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
9836 if ( this.level
=== i
) {
9837 this.$element
.addClass( levelClass
+ i
);
9839 this.$element
.removeClass( levelClass
+ i
);
9847 * Container for content that is overlaid and positioned absolutely.
9850 * @extends OO.ui.Widget
9851 * @mixins OO.ui.LabelElement
9854 * @param {Object} [config] Configuration options
9855 * @cfg {number} [width=320] Width of popup in pixels
9856 * @cfg {number} [height] Height of popup, omit to use automatic height
9857 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
9858 * @cfg {string} [align='center'] Alignment of popup to origin
9859 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
9860 * @cfg {jQuery} [$content] Content to append to the popup's body
9861 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
9862 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
9863 * @cfg {boolean} [head] Show label and close button at the top
9864 * @cfg {boolean} [padded] Add padding to the body
9866 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
9867 // Config intialization
9868 config
= config
|| {};
9870 // Parent constructor
9871 OO
.ui
.PopupWidget
.super.call( this, config
);
9873 // Mixin constructors
9874 OO
.ui
.LabelElement
.call( this, config
);
9875 OO
.ui
.ClippableElement
.call( this, config
);
9878 this.visible
= false;
9879 this.$popup
= this.$( '<div>' );
9880 this.$head
= this.$( '<div>' );
9881 this.$body
= this.$( '<div>' );
9882 this.$anchor
= this.$( '<div>' );
9883 this.$container
= config
.$container
; // If undefined, will be computed lazily in updateDimensions()
9884 this.autoClose
= !!config
.autoClose
;
9885 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
9886 this.transitionTimeout
= null;
9888 this.width
= config
.width
!== undefined ? config
.width
: 320;
9889 this.height
= config
.height
!== undefined ? config
.height
: null;
9890 this.align
= config
.align
|| 'center';
9891 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
9892 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
9895 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
9898 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
9899 this.$body
.addClass( 'oo-ui-popupWidget-body' );
9900 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
9902 .addClass( 'oo-ui-popupWidget-head' )
9903 .append( this.$label
, this.closeButton
.$element
);
9904 if ( !config
.head
) {
9908 .addClass( 'oo-ui-popupWidget-popup' )
9909 .append( this.$head
, this.$body
);
9912 .addClass( 'oo-ui-popupWidget' )
9913 .append( this.$popup
, this.$anchor
);
9914 // Move content, which was added to #$element by OO.ui.Widget, to the body
9915 if ( config
.$content
instanceof jQuery
) {
9916 this.$body
.append( config
.$content
);
9918 if ( config
.padded
) {
9919 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
9921 this.setClippableElement( this.$body
);
9926 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
9927 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
9928 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
9943 * Handles mouse down events.
9945 * @param {jQuery.Event} e Mouse down event
9947 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
9950 !$.contains( this.$element
[0], e
.target
) &&
9951 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
9953 this.toggle( false );
9958 * Bind mouse down listener.
9960 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
9961 // Capture clicks outside popup
9962 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
9966 * Handles close button click events.
9968 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
9969 if ( this.isVisible() ) {
9970 this.toggle( false );
9975 * Unbind mouse down listener.
9977 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
9978 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
9982 * Set whether to show a anchor.
9984 * @param {boolean} [show] Show anchor, omit to toggle
9986 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
9987 show
= show
=== undefined ? !this.anchored
: !!show
;
9989 if ( this.anchored
!== show
) {
9991 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
9993 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
9995 this.anchored
= show
;
10000 * Check if showing a anchor.
10002 * @return {boolean} anchor is visible
10004 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
10005 return this.anchor
;
10011 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
10012 show
= show
=== undefined ? !this.isVisible() : !!show
;
10014 var change
= show
!== this.isVisible();
10017 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
10021 if ( this.autoClose
) {
10022 this.bindMouseDownListener();
10024 this.updateDimensions();
10025 this.toggleClipping( true );
10027 this.toggleClipping( false );
10028 if ( this.autoClose
) {
10029 this.unbindMouseDownListener();
10038 * Set the size of the popup.
10040 * Changing the size may also change the popup's position depending on the alignment.
10042 * @param {number} width Width
10043 * @param {number} height Height
10044 * @param {boolean} [transition=false] Use a smooth transition
10047 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
10048 this.width
= width
;
10049 this.height
= height
!== undefined ? height
: null;
10050 if ( this.isVisible() ) {
10051 this.updateDimensions( transition
);
10056 * Update the size and position.
10058 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
10059 * be called automatically.
10061 * @param {boolean} [transition=false] Use a smooth transition
10064 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
10065 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
10066 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
10070 if ( !this.$container
) {
10071 // Lazy-initialize $container if not specified in constructor
10072 this.$container
= this.$( this.getClosestScrollableElementContainer() );
10075 // Set height and width before measuring things, since it might cause our measurements
10076 // to change (e.g. due to scrollbars appearing or disappearing)
10079 height
: this.height
!== null ? this.height
: 'auto'
10082 // Compute initial popupOffset based on alignment
10083 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
10085 // Figure out if this will cause the popup to go beyond the edge of the container
10086 originOffset
= Math
.round( this.$element
.offset().left
);
10087 containerLeft
= Math
.round( this.$container
.offset().left
);
10088 containerWidth
= this.$container
.innerWidth();
10089 containerRight
= containerLeft
+ containerWidth
;
10090 popupLeft
= popupOffset
- padding
;
10091 popupRight
= popupOffset
+ padding
+ this.width
+ padding
;
10092 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
10093 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10095 // Adjust offset to make the popup not go beyond the edge, if needed
10096 if ( overlapRight
< 0 ) {
10097 popupOffset
+= overlapRight
;
10098 } else if ( overlapLeft
< 0 ) {
10099 popupOffset
-= overlapLeft
;
10102 // Adjust offset to avoid anchor being rendered too close to the edge
10103 anchorWidth
= this.$anchor
.width();
10104 if ( this.align
=== 'right' ) {
10105 popupOffset
+= anchorWidth
;
10106 } else if ( this.align
=== 'left' ) {
10107 popupOffset
-= anchorWidth
;
10110 // Prevent transition from being interrupted
10111 clearTimeout( this.transitionTimeout
);
10112 if ( transition
) {
10113 // Enable transition
10114 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10117 // Position body relative to anchor
10118 this.$popup
.css( 'left', popupOffset
);
10120 if ( transition
) {
10121 // Prevent transitioning after transition is complete
10122 this.transitionTimeout
= setTimeout( function () {
10123 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10126 // Prevent transitioning immediately
10127 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10130 // Reevaluate clipping state since we've relocated and resized the popup
10139 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10140 * Results are cleared and populated each time the query is changed.
10143 * @extends OO.ui.Widget
10146 * @param {Object} [config] Configuration options
10147 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10148 * @cfg {string} [value] Initial query value
10150 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10151 // Configuration intialization
10152 config
= config
|| {};
10154 // Parent constructor
10155 OO
.ui
.SearchWidget
.super.call( this, config
);
10158 this.query
= new OO
.ui
.TextInputWidget( {
10161 placeholder
: config
.placeholder
,
10162 value
: config
.value
10164 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10165 this.$query
= this.$( '<div>' );
10166 this.$results
= this.$( '<div>' );
10169 this.query
.connect( this, {
10170 change
: 'onQueryChange',
10171 enter
: 'onQueryEnter'
10173 this.results
.connect( this, {
10174 highlight
: 'onResultsHighlight',
10175 select
: 'onResultsSelect'
10177 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
10181 .addClass( 'oo-ui-searchWidget-query' )
10182 .append( this.query
.$element
);
10184 .addClass( 'oo-ui-searchWidget-results' )
10185 .append( this.results
.$element
);
10187 .addClass( 'oo-ui-searchWidget' )
10188 .append( this.$results
, this.$query
);
10193 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
10199 * @param {Object|null} item Item data or null if no item is highlighted
10204 * @param {Object|null} item Item data or null if no item is selected
10210 * Handle query key down events.
10212 * @param {jQuery.Event} e Key down event
10214 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
10215 var highlightedItem
, nextItem
,
10216 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
10219 highlightedItem
= this.results
.getHighlightedItem();
10220 if ( !highlightedItem
) {
10221 highlightedItem
= this.results
.getSelectedItem();
10223 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
10224 this.results
.highlightItem( nextItem
);
10225 nextItem
.scrollElementIntoView();
10230 * Handle select widget select events.
10232 * Clears existing results. Subclasses should repopulate items according to new query.
10234 * @param {string} value New value
10236 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
10238 this.results
.clearItems();
10242 * Handle select widget enter key events.
10244 * Selects highlighted item.
10246 * @param {string} value New value
10248 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
10250 this.results
.selectItem( this.results
.getHighlightedItem() );
10254 * Handle select widget highlight events.
10256 * @param {OO.ui.OptionWidget} item Highlighted item
10259 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
10260 this.emit( 'highlight', item
? item
.getData() : null );
10264 * Handle select widget select events.
10266 * @param {OO.ui.OptionWidget} item Selected item
10269 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
10270 this.emit( 'select', item
? item
.getData() : null );
10274 * Get the query input.
10276 * @return {OO.ui.TextInputWidget} Query input
10278 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
10283 * Get the results list.
10285 * @return {OO.ui.SelectWidget} Select list
10287 OO
.ui
.SearchWidget
.prototype.getResults = function () {
10288 return this.results
;
10292 * Generic selection of options.
10294 * Items can contain any rendering, and are uniquely identified by a has of thier data. Any widget
10295 * that provides options, from which the user must choose one, should be built on this class.
10297 * Use together with OO.ui.OptionWidget.
10300 * @extends OO.ui.Widget
10301 * @mixins OO.ui.GroupElement
10304 * @param {Object} [config] Configuration options
10305 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
10307 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
10308 // Config intialization
10309 config
= config
|| {};
10311 // Parent constructor
10312 OO
.ui
.SelectWidget
.super.call( this, config
);
10314 // Mixin constructors
10315 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
10318 this.pressed
= false;
10319 this.selecting
= null;
10321 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
10322 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
10325 this.$element
.on( {
10326 mousedown
: OO
.ui
.bind( this.onMouseDown
, this ),
10327 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
10328 mouseleave
: OO
.ui
.bind( this.onMouseLeave
, this )
10332 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
10333 if ( $.isArray( config
.items
) ) {
10334 this.addItems( config
.items
);
10340 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
10342 // Need to mixin base class as well
10343 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
10344 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
10350 * @param {OO.ui.OptionWidget|null} item Highlighted item
10355 * @param {OO.ui.OptionWidget|null} item Pressed item
10360 * @param {OO.ui.OptionWidget|null} item Selected item
10365 * @param {OO.ui.OptionWidget|null} item Chosen item
10370 * @param {OO.ui.OptionWidget[]} items Added items
10371 * @param {number} index Index items were added at
10376 * @param {OO.ui.OptionWidget[]} items Removed items
10382 * Handle mouse down events.
10385 * @param {jQuery.Event} e Mouse down event
10387 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
10390 if ( !this.isDisabled() && e
.which
=== 1 ) {
10391 this.togglePressed( true );
10392 item
= this.getTargetItem( e
);
10393 if ( item
&& item
.isSelectable() ) {
10394 this.pressItem( item
);
10395 this.selecting
= item
;
10396 this.getElementDocument().addEventListener(
10398 this.onMouseUpHandler
,
10401 this.getElementDocument().addEventListener(
10403 this.onMouseMoveHandler
,
10412 * Handle mouse up events.
10415 * @param {jQuery.Event} e Mouse up event
10417 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
10420 this.togglePressed( false );
10421 if ( !this.selecting
) {
10422 item
= this.getTargetItem( e
);
10423 if ( item
&& item
.isSelectable() ) {
10424 this.selecting
= item
;
10427 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
10428 this.pressItem( null );
10429 this.chooseItem( this.selecting
);
10430 this.selecting
= null;
10433 this.getElementDocument().removeEventListener(
10435 this.onMouseUpHandler
,
10438 this.getElementDocument().removeEventListener(
10440 this.onMouseMoveHandler
,
10448 * Handle mouse move events.
10451 * @param {jQuery.Event} e Mouse move event
10453 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
10456 if ( !this.isDisabled() && this.pressed
) {
10457 item
= this.getTargetItem( e
);
10458 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
10459 this.pressItem( item
);
10460 this.selecting
= item
;
10467 * Handle mouse over events.
10470 * @param {jQuery.Event} e Mouse over event
10472 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
10475 if ( !this.isDisabled() ) {
10476 item
= this.getTargetItem( e
);
10477 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
10483 * Handle mouse leave events.
10486 * @param {jQuery.Event} e Mouse over event
10488 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
10489 if ( !this.isDisabled() ) {
10490 this.highlightItem( null );
10496 * Get the closest item to a jQuery.Event.
10499 * @param {jQuery.Event} e
10500 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
10502 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
10503 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
10504 if ( $item
.length
) {
10505 return $item
.data( 'oo-ui-optionWidget' );
10511 * Get selected item.
10513 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
10515 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
10518 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10519 if ( this.items
[i
].isSelected() ) {
10520 return this.items
[i
];
10527 * Get highlighted item.
10529 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
10531 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
10534 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10535 if ( this.items
[i
].isHighlighted() ) {
10536 return this.items
[i
];
10543 * Get an existing item with equivilant data.
10545 * @param {Object} data Item data to search for
10546 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
10548 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
10549 var hash
= OO
.getHash( data
);
10551 if ( hash
in this.hashes
) {
10552 return this.hashes
[hash
];
10559 * Toggle pressed state.
10561 * @param {boolean} pressed An option is being pressed
10563 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
10564 if ( pressed
=== undefined ) {
10565 pressed
= !this.pressed
;
10567 if ( pressed
!== this.pressed
) {
10569 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
10570 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
10571 this.pressed
= pressed
;
10576 * Highlight an item.
10578 * Highlighting is mutually exclusive.
10580 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
10584 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
10585 var i
, len
, highlighted
,
10588 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10589 highlighted
= this.items
[i
] === item
;
10590 if ( this.items
[i
].isHighlighted() !== highlighted
) {
10591 this.items
[i
].setHighlighted( highlighted
);
10596 this.emit( 'highlight', item
);
10605 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
10609 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
10610 var i
, len
, selected
,
10613 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10614 selected
= this.items
[i
] === item
;
10615 if ( this.items
[i
].isSelected() !== selected
) {
10616 this.items
[i
].setSelected( selected
);
10621 this.emit( 'select', item
);
10630 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
10634 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
10635 var i
, len
, pressed
,
10638 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10639 pressed
= this.items
[i
] === item
;
10640 if ( this.items
[i
].isPressed() !== pressed
) {
10641 this.items
[i
].setPressed( pressed
);
10646 this.emit( 'press', item
);
10655 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
10656 * an item is selected using the keyboard or mouse.
10658 * @param {OO.ui.OptionWidget} item Item to choose
10662 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
10663 this.selectItem( item
);
10664 this.emit( 'choose', item
);
10670 * Get an item relative to another one.
10672 * @param {OO.ui.OptionWidget} item Item to start at
10673 * @param {number} direction Direction to move in
10674 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
10676 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
10677 var inc
= direction
> 0 ? 1 : -1,
10678 len
= this.items
.length
,
10679 index
= item
instanceof OO
.ui
.OptionWidget
?
10680 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
10681 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
10683 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
10684 Math
.max( index
, -1 ) :
10685 // Default to n-1 instead of -1, if nothing is selected let's start at the end
10686 Math
.min( index
, len
);
10689 i
= ( i
+ inc
+ len
) % len
;
10690 item
= this.items
[i
];
10691 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10694 // Stop iterating when we've looped all the way around
10695 if ( i
=== stopAt
) {
10703 * Get the next selectable item.
10705 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
10707 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
10710 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10711 item
= this.items
[i
];
10712 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10723 * When items are added with the same values as existing items, the existing items will be
10724 * automatically removed before the new items are added.
10726 * @param {OO.ui.OptionWidget[]} items Items to add
10727 * @param {number} [index] Index to insert items after
10731 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
10732 var i
, len
, item
, hash
,
10735 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10737 hash
= OO
.getHash( item
.getData() );
10738 if ( hash
in this.hashes
) {
10739 // Remove item with same value
10740 remove
.push( this.hashes
[hash
] );
10742 this.hashes
[hash
] = item
;
10744 if ( remove
.length
) {
10745 this.removeItems( remove
);
10749 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
10751 // Always provide an index, even if it was omitted
10752 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
10760 * Items will be detached, not removed, so they can be used later.
10762 * @param {OO.ui.OptionWidget[]} items Items to remove
10766 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
10767 var i
, len
, item
, hash
;
10769 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10771 hash
= OO
.getHash( item
.getData() );
10772 if ( hash
in this.hashes
) {
10773 // Remove existing item
10774 delete this.hashes
[hash
];
10776 if ( item
.isSelected() ) {
10777 this.selectItem( null );
10782 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
10784 this.emit( 'remove', items
);
10792 * Items will be detached, not removed, so they can be used later.
10797 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
10798 var items
= this.items
.slice();
10803 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
10804 this.selectItem( null );
10806 this.emit( 'remove', items
);
10812 * Select widget containing button options.
10814 * Use together with OO.ui.ButtonOptionWidget.
10817 * @extends OO.ui.SelectWidget
10820 * @param {Object} [config] Configuration options
10822 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
10823 // Parent constructor
10824 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
10827 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
10832 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
10835 * Overlaid menu of options.
10837 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
10840 * Use together with OO.ui.MenuItemWidget.
10843 * @extends OO.ui.SelectWidget
10844 * @mixins OO.ui.ClippableElement
10847 * @param {Object} [config] Configuration options
10848 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
10849 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
10850 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
10852 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
10853 // Config intialization
10854 config
= config
|| {};
10856 // Parent constructor
10857 OO
.ui
.MenuWidget
.super.call( this, config
);
10859 // Mixin constructors
10860 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
10863 this.flashing
= false;
10864 this.visible
= false;
10865 this.newItems
= null;
10866 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
10867 this.$input
= config
.input
? config
.input
.$input
: null;
10868 this.$widget
= config
.widget
? config
.widget
.$element
: null;
10869 this.$previousFocus
= null;
10870 this.isolated
= !config
.input
;
10871 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
10872 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
10877 .attr( 'role', 'menu' )
10878 .addClass( 'oo-ui-menuWidget' );
10883 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
10884 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
10889 * Handles document mouse down events.
10891 * @param {jQuery.Event} e Key down event
10893 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
10894 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
10895 this.toggle( false );
10900 * Handles key down events.
10902 * @param {jQuery.Event} e Key down event
10904 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
10907 highlightItem
= this.getHighlightedItem();
10909 if ( !this.isDisabled() && this.isVisible() ) {
10910 if ( !highlightItem
) {
10911 highlightItem
= this.getSelectedItem();
10913 switch ( e
.keyCode
) {
10914 case OO
.ui
.Keys
.ENTER
:
10915 this.chooseItem( highlightItem
);
10918 case OO
.ui
.Keys
.UP
:
10919 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
10922 case OO
.ui
.Keys
.DOWN
:
10923 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
10926 case OO
.ui
.Keys
.ESCAPE
:
10927 if ( highlightItem
) {
10928 highlightItem
.setHighlighted( false );
10930 this.toggle( false );
10936 this.highlightItem( nextItem
);
10937 nextItem
.scrollElementIntoView();
10941 e
.preventDefault();
10942 e
.stopPropagation();
10949 * Bind key down listener.
10951 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
10952 if ( this.$input
) {
10953 this.$input
.on( 'keydown', this.onKeyDownHandler
);
10955 // Capture menu navigation keys
10956 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
10961 * Unbind key down listener.
10963 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
10964 if ( this.$input
) {
10965 this.$input
.off( 'keydown' );
10967 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
10974 * This will close the menu when done, unlike selectItem which only changes selection.
10976 * @param {OO.ui.OptionWidget} item Item to choose
10979 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
10983 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
10985 if ( item
&& !this.flashing
) {
10986 this.flashing
= true;
10987 item
.flash().done( function () {
10988 widget
.toggle( false );
10989 widget
.flashing
= false;
10992 this.toggle( false );
11001 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
11005 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
11008 if ( !this.newItems
) {
11009 this.newItems
= [];
11012 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11014 if ( this.isVisible() ) {
11015 // Defer fitting label until item has been attached
11018 this.newItems
.push( item
);
11022 // Reevaluate clipping
11031 OO
.ui
.MenuWidget
.prototype.removeItems = function ( items
) {
11033 OO
.ui
.MenuWidget
.super.prototype.removeItems
.call( this, items
);
11035 // Reevaluate clipping
11044 OO
.ui
.MenuWidget
.prototype.clearItems = function () {
11046 OO
.ui
.MenuWidget
.super.prototype.clearItems
.call( this );
11048 // Reevaluate clipping
11057 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
11058 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
11061 change
= visible
!== this.isVisible();
11064 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
11068 this.bindKeyDownListener();
11070 // Change focus to enable keyboard navigation
11071 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
11072 this.$previousFocus
= this.$( ':focus' );
11073 this.$input
[0].focus();
11075 if ( this.newItems
&& this.newItems
.length
) {
11076 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
11077 this.newItems
[i
].fitLabel();
11079 this.newItems
= null;
11081 this.toggleClipping( true );
11084 if ( this.autoHide
) {
11085 this.getElementDocument().addEventListener(
11086 'mousedown', this.onDocumentMouseDownHandler
, true
11090 this.unbindKeyDownListener();
11091 if ( this.isolated
&& this.$previousFocus
) {
11092 this.$previousFocus
[0].focus();
11093 this.$previousFocus
= null;
11095 this.getElementDocument().removeEventListener(
11096 'mousedown', this.onDocumentMouseDownHandler
, true
11098 this.toggleClipping( false );
11106 * Menu for a text input widget.
11108 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11109 * is in a different frame, the menu's position is automatically calulated and maintained when the
11110 * menu is toggled or the window is resized.
11113 * @extends OO.ui.MenuWidget
11116 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11117 * @param {Object} [config] Configuration options
11118 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11120 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
11121 // Parent constructor
11122 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
11125 this.input
= input
;
11126 this.$container
= config
.$container
|| this.input
.$element
;
11127 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
11130 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
11135 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
11140 * Handle window resize event.
11142 * @param {jQuery.Event} e Window resize event
11144 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
11151 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
11152 visible
= !!visible
;
11154 var change
= visible
!== this.isVisible();
11156 if ( change
&& visible
) {
11157 // Make sure the width is set before the parent method runs.
11158 // After this we have to call this.position(); again to actually
11159 // position ourselves correctly.
11164 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
11167 if ( this.isVisible() ) {
11169 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11171 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
11179 * Position the menu.
11183 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
11185 $container
= this.$container
,
11186 dimensions
= $container
.offset();
11188 // Position under input
11189 dimensions
.top
+= $container
.height();
11191 // Compensate for frame position if in a differnt frame
11192 if ( this.input
.$.$iframe
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
11193 frameOffset
= OO
.ui
.Element
.getRelativePosition(
11194 this.input
.$.$iframe
, this.$element
.offsetParent()
11196 dimensions
.left
+= frameOffset
.left
;
11197 dimensions
.top
+= frameOffset
.top
;
11199 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
11200 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
11201 dimensions
.right
= this.$element
.parent().position().left
-
11202 $container
.width() - dimensions
.left
;
11203 // Erase the value for 'left':
11204 delete dimensions
.left
;
11207 this.$element
.css( dimensions
);
11208 this.setIdealSize( $container
.width() );
11209 // We updated the position, so re-evaluate the clipping state
11216 * Structured list of items.
11218 * Use with OO.ui.OutlineItemWidget.
11221 * @extends OO.ui.SelectWidget
11224 * @param {Object} [config] Configuration options
11226 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
11227 // Config intialization
11228 config
= config
|| {};
11230 // Parent constructor
11231 OO
.ui
.OutlineWidget
.super.call( this, config
);
11234 this.$element
.addClass( 'oo-ui-outlineWidget' );
11239 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
11242 * Switch that slides on and off.
11245 * @extends OO.ui.Widget
11246 * @mixins OO.ui.ToggleWidget
11249 * @param {Object} [config] Configuration options
11250 * @cfg {boolean} [value=false] Initial value
11252 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
11253 // Parent constructor
11254 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
11256 // Mixin constructors
11257 OO
.ui
.ToggleWidget
.call( this, config
);
11260 this.dragging
= false;
11261 this.dragStart
= null;
11262 this.sliding
= false;
11263 this.$glow
= this.$( '<span>' );
11264 this.$grip
= this.$( '<span>' );
11267 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
11270 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
11271 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
11273 .addClass( 'oo-ui-toggleSwitchWidget' )
11274 .append( this.$glow
, this.$grip
);
11279 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
11280 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
11285 * Handle mouse down events.
11287 * @param {jQuery.Event} e Mouse down event
11289 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
11290 if ( !this.isDisabled() && e
.which
=== 1 ) {
11291 this.setValue( !this.value
);