2 * OOjs UI v0.1.0-pre (55b861b167)
3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2014-08-21T16:59:20Z
16 * Namespace for all classes, static methods and static properties.
48 * Get the user's language and any fallback languages.
50 * These language codes are used to localize user interface elements in the user's language.
52 * In environments that provide a localization system, this function should be overridden to
53 * return the user's language(s). The default implementation returns English (en) only.
55 * @return {string[]} Language codes, in descending order of priority
57 OO
.ui
.getUserLanguages = function () {
62 * Get a value in an object keyed by language code.
64 * @param {Object.<string,Mixed>} obj Object keyed by language code
65 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
66 * @param {string} [fallback] Fallback code, used if no matching language can be found
67 * @return {Mixed} Local value
69 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
76 // Known user language
77 langs
= OO
.ui
.getUserLanguages();
78 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
85 if ( obj
[fallback
] ) {
88 // First existing language
98 * Message store for the default implementation of OO.ui.msg
100 * Environments that provide a localization system should not use this, but should override
101 * OO.ui.msg altogether.
106 // Tool tip for a button that moves items in a list down one place
107 'ooui-outline-control-move-down': 'Move item down',
108 // Tool tip for a button that moves items in a list up one place
109 'ooui-outline-control-move-up': 'Move item up',
110 // Tool tip for a button that removes items from a list
111 'ooui-outline-control-remove': 'Remove item',
112 // Label for the toolbar group that contains a list of all other available tools
113 'ooui-toolbar-more': 'More',
114 // Default label for the accept button of a confirmation dialog
115 'ooui-dialog-message-accept': 'OK',
116 // Default label for the reject button of a confirmation dialog
117 'ooui-dialog-message-reject': 'Cancel',
118 // Title for process dialog error description
119 'ooui-dialog-process-error': 'Something went wrong',
120 // Label for process dialog dismiss error button, visible when describing errors
121 'ooui-dialog-process-dismiss': 'Dismiss',
122 // Label for process dialog retry action button, visible when describing recoverable errors
123 'ooui-dialog-process-retry': 'Try again'
127 * Get a localized message.
129 * In environments that provide a localization system, this function should be overridden to
130 * return the message translated in the user's language. The default implementation always returns
133 * After the message key, message parameters may optionally be passed. In the default implementation,
134 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
135 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
136 * they support unnamed, ordered message parameters.
139 * @param {string} key Message key
140 * @param {Mixed...} [params] Message parameters
141 * @return {string} Translated message with parameters substituted
143 OO
.ui
.msg = function ( key
) {
144 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
145 if ( typeof message
=== 'string' ) {
146 // Perform $1 substitution
147 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
148 var i
= parseInt( n
, 10 );
149 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
152 // Return placeholder if message not found
153 message
= '[' + key
+ ']';
159 * Package a message and arguments for deferred resolution.
161 * Use this when you are statically specifying a message and the message may not yet be present.
163 * @param {string} key Message key
164 * @param {Mixed...} [params] Message parameters
165 * @return {Function} Function that returns the resolved message when executed
167 OO
.ui
.deferMsg = function () {
168 var args
= arguments
;
170 return OO
.ui
.msg
.apply( OO
.ui
, args
);
177 * If the message is a function it will be executed, otherwise it will pass through directly.
179 * @param {Function|string} msg Deferred message, or message text
180 * @return {string} Resolved message
182 OO
.ui
.resolveMsg = function ( msg
) {
183 if ( $.isFunction( msg
) ) {
196 * @mixins OO.EventEmitter
199 * @param {Object} [config] Configuration options
201 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
202 // Configuration intialization
203 config
= config
|| {};
205 // Mixin constructors
206 OO
.EventEmitter
.call( this );
211 actions
: 'getAction',
215 this.categorized
= {};
218 this.organized
= false;
219 this.changing
= false;
220 this.changed
= false;
225 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
227 /* Static Properties */
230 * Symbolic name of dialog.
237 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
243 * @param {OO.ui.ActionWidget} action Action that was clicked
248 * @param {OO.ui.ActionWidget} action Action that was resized
253 * @param {OO.ui.ActionWidget[]} added Actions added
258 * @param {OO.ui.ActionWidget[]} added Actions removed
268 * Handle action change events.
272 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
273 this.organized
= false;
274 if ( this.changing
) {
277 this.emit( 'change' );
282 * Check if a action is one of the special actions.
284 * @param {OO.ui.ActionWidget} action Action to check
285 * @return {boolean} Action is special
287 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
290 for ( flag
in this.special
) {
291 if ( action
=== this.special
[flag
] ) {
302 * @param {Object} [filters] Filters to use, omit to get all actions
303 * @param {string|string[]} [filters.actions] Actions that actions must have
304 * @param {string|string[]} [filters.flags] Flags that actions must have
305 * @param {string|string[]} [filters.modes] Modes that actions must have
306 * @param {boolean} [filters.visible] Actions must be visible
307 * @param {boolean} [filters.disabled] Actions must be disabled
308 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
310 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
311 var i
, len
, list
, category
, actions
, index
, match
, matches
;
316 // Collect category candidates
318 for ( category
in this.categorized
) {
319 list
= filters
[category
];
321 if ( !Array
.isArray( list
) ) {
324 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
325 actions
= this.categorized
[category
][list
[i
]];
326 if ( Array
.isArray( actions
) ) {
327 matches
.push
.apply( matches
, actions
);
332 // Remove by boolean filters
333 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
336 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
337 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
339 matches
.splice( i
, 1 );
345 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
347 index
= matches
.lastIndexOf( match
);
348 while ( index
!== i
) {
349 matches
.splice( index
, 1 );
351 index
= matches
.lastIndexOf( match
);
356 return this.list
.slice();
360 * Get special actions.
362 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
363 * Special flags can be configured by changing #static-specialFlags in a subclass.
365 * @return {OO.ui.ActionWidget|null} Safe action
367 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
369 return $.extend( {}, this.special
);
375 * Other actions include all non-special visible actions.
377 * @return {OO.ui.ActionWidget[]} Other actions
379 OO
.ui
.ActionSet
.prototype.getOthers = function () {
381 return this.others
.slice();
385 * Toggle actions based on their modes.
387 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
388 * visibility; matching actions will be shown, non-matching actions will be hidden.
390 * @param {string} mode Mode actions must have
395 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
398 this.changing
= true;
399 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
400 action
= this.list
[i
];
401 action
.toggle( action
.hasMode( mode
) );
404 this.organized
= false;
405 this.changing
= false;
406 this.emit( 'change' );
412 * Change which actions are able to be performed.
414 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
416 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
417 * indicate actions are able to be performed
420 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
421 var i
, len
, action
, item
;
423 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
425 action
= item
.getAction();
426 if ( actions
[action
] !== undefined ) {
427 item
.setDisabled( !actions
[action
] );
435 * Executes a function once per action.
437 * When making changes to multiple actions, use this method instead of iterating over the actions
438 * manually to defer emitting a change event until after all actions have been changed.
440 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
441 * @param {Function} callback Callback to run for each action; callback is invoked with three
442 * arguments: the action, the action's index, the list of actions being iterated over
445 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
446 this.changed
= false;
447 this.changing
= true;
448 this.get( filter
).forEach( callback
);
449 this.changing
= false;
450 if ( this.changed
) {
451 this.emit( 'change' );
460 * @param {OO.ui.ActionWidget[]} actions Actions to add
465 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
468 this.changing
= true;
469 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
471 action
.connect( this, {
472 click
: [ 'emit', 'click', action
],
473 resize
: [ 'emit', 'resize', action
],
474 toggle
: [ 'onActionChange' ]
476 this.list
.push( action
);
478 this.organized
= false;
479 this.emit( 'add', actions
);
480 this.changing
= false;
481 this.emit( 'change' );
489 * @param {OO.ui.ActionWidget[]} actions Actions to remove
494 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
495 var i
, len
, index
, action
;
497 this.changing
= true;
498 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
500 index
= this.list
.indexOf( action
);
501 if ( index
!== -1 ) {
502 action
.disconnect( this );
503 this.list
.splice( index
, 1 );
506 this.organized
= false;
507 this.emit( 'remove', actions
);
508 this.changing
= false;
509 this.emit( 'change' );
515 * Remove all actions.
521 OO
.ui
.ActionSet
.prototype.clear = function () {
523 removed
= this.list
.slice();
525 this.changing
= true;
526 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
527 action
= this.list
[i
];
528 action
.disconnect( this );
533 this.organized
= false;
534 this.emit( 'remove', removed
);
535 this.changing
= false;
536 this.emit( 'change' );
544 * This is called whenver organized information is requested. It will only reorganize the actions
545 * if something has changed since the last time it ran.
550 OO
.ui
.ActionSet
.prototype.organize = function () {
551 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
552 specialFlags
= this.constructor.static.specialFlags
;
554 if ( !this.organized
) {
555 this.categorized
= {};
558 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
559 action
= this.list
[i
];
560 if ( action
.isVisible() ) {
561 // Populate catgeories
562 for ( category
in this.categories
) {
563 if ( !this.categorized
[category
] ) {
564 this.categorized
[category
] = {};
566 list
= action
[this.categories
[category
]]();
567 if ( !Array
.isArray( list
) ) {
570 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
572 if ( !this.categorized
[category
][item
] ) {
573 this.categorized
[category
][item
] = [];
575 this.categorized
[category
][item
].push( action
);
578 // Populate special/others
580 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
581 flag
= specialFlags
[j
];
582 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
583 this.special
[flag
] = action
;
589 this.others
.push( action
);
593 this.organized
= true;
600 * DOM element abstraction.
606 * @param {Object} [config] Configuration options
607 * @cfg {Function} [$] jQuery for the frame the widget is in
608 * @cfg {string[]} [classes] CSS class names
609 * @cfg {string} [text] Text to insert
610 * @cfg {jQuery} [$content] Content elements to append (after text)
612 OO
.ui
.Element
= function OoUiElement( config
) {
613 // Configuration initialization
614 config
= config
|| {};
617 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
618 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
619 this.elementGroup
= null;
622 if ( $.isArray( config
.classes
) ) {
623 this.$element
.addClass( config
.classes
.join( ' ' ) );
626 this.$element
.text( config
.text
);
628 if ( config
.$content
) {
629 this.$element
.append( config
.$content
);
635 OO
.initClass( OO
.ui
.Element
);
637 /* Static Properties */
642 * This may be ignored if getTagName is overridden.
648 OO
.ui
.Element
.static.tagName
= 'div';
653 * Get a jQuery function within a specific document.
656 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
657 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
659 * @return {Function} Bound jQuery function
661 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
662 function wrapper( selector
) {
663 return $( selector
, wrapper
.context
);
666 wrapper
.context
= this.getDocument( context
);
669 wrapper
.$iframe
= $iframe
;
676 * Get the document of an element.
679 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
680 * @return {HTMLDocument|null} Document object
682 OO
.ui
.Element
.getDocument = function ( obj
) {
683 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
684 return ( obj
[0] && obj
[0].ownerDocument
) ||
685 // Empty jQuery selections might have a context
692 ( obj
.nodeType
=== 9 && obj
) ||
697 * Get the window of an element or document.
700 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
701 * @return {Window} Window object
703 OO
.ui
.Element
.getWindow = function ( obj
) {
704 var doc
= this.getDocument( obj
);
705 return doc
.parentWindow
|| doc
.defaultView
;
709 * Get the direction of an element or document.
712 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
713 * @return {string} Text direction, either `ltr` or `rtl`
715 OO
.ui
.Element
.getDir = function ( obj
) {
718 if ( obj
instanceof jQuery
) {
721 isDoc
= obj
.nodeType
=== 9;
722 isWin
= obj
.document
!== undefined;
723 if ( isDoc
|| isWin
) {
729 return $( obj
).css( 'direction' );
733 * Get the offset between two frames.
735 * TODO: Make this function not use recursion.
738 * @param {Window} from Window of the child frame
739 * @param {Window} [to=window] Window of the parent frame
740 * @param {Object} [offset] Offset to start with, used internally
741 * @return {Object} Offset object, containing left and top properties
743 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
744 var i
, len
, frames
, frame
, rect
;
750 offset
= { top
: 0, left
: 0 };
752 if ( from.parent
=== from ) {
756 // Get iframe element
757 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
758 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
759 if ( frames
[i
].contentWindow
=== from ) {
765 // Recursively accumulate offset values
767 rect
= frame
.getBoundingClientRect();
768 offset
.left
+= rect
.left
;
769 offset
.top
+= rect
.top
;
771 this.getFrameOffset( from.parent
, offset
);
778 * Get the offset between two elements.
781 * @param {jQuery} $from
782 * @param {jQuery} $to
783 * @return {Object} Translated position coordinates, containing top and left properties
785 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
786 var from = $from.offset(),
788 return { top
: Math
.round( from.top
- to
.top
), left
: Math
.round( from.left
- to
.left
) };
792 * Get element border sizes.
795 * @param {HTMLElement} el Element to measure
796 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
798 OO
.ui
.Element
.getBorders = function ( el
) {
799 var doc
= el
.ownerDocument
,
800 win
= doc
.parentWindow
|| doc
.defaultView
,
801 style
= win
&& win
.getComputedStyle
?
802 win
.getComputedStyle( el
, null ) :
805 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
806 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
807 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
808 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
811 top
: Math
.round( top
),
812 left
: Math
.round( left
),
813 bottom
: Math
.round( bottom
),
814 right
: Math
.round( right
)
819 * Get dimensions of an element or window.
822 * @param {HTMLElement|Window} el Element to measure
823 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
825 OO
.ui
.Element
.getDimensions = function ( el
) {
827 doc
= el
.ownerDocument
|| el
.document
,
828 win
= doc
.parentWindow
|| doc
.defaultView
;
830 if ( win
=== el
|| el
=== doc
.documentElement
) {
833 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
835 top
: $win
.scrollTop(),
836 left
: $win
.scrollLeft()
838 scrollbar
: { right
: 0, bottom
: 0 },
842 bottom
: $win
.innerHeight(),
843 right
: $win
.innerWidth()
849 borders
: this.getBorders( el
),
851 top
: $el
.scrollTop(),
852 left
: $el
.scrollLeft()
855 right
: $el
.innerWidth() - el
.clientWidth
,
856 bottom
: $el
.innerHeight() - el
.clientHeight
858 rect
: el
.getBoundingClientRect()
864 * Get closest scrollable container.
866 * Traverses up until either a scrollable element or the root is reached, in which case the window
870 * @param {HTMLElement} el Element to find scrollable container for
871 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
872 * @return {HTMLElement|Window} Closest scrollable container
874 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
876 props
= [ 'overflow' ],
877 $parent
= $( el
).parent();
879 if ( dimension
=== 'x' || dimension
=== 'y' ) {
880 props
.push( 'overflow-' + dimension
);
883 while ( $parent
.length
) {
884 if ( $parent
[0] === el
.ownerDocument
.body
) {
889 val
= $parent
.css( props
[i
] );
890 if ( val
=== 'auto' || val
=== 'scroll' ) {
894 $parent
= $parent
.parent();
896 return this.getDocument( el
).body
;
900 * Scroll element into view.
903 * @param {HTMLElement} el Element to scroll into view
904 * @param {Object} [config={}] Configuration config
905 * @param {string} [config.duration] jQuery animation duration value
906 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
907 * to scroll in both directions
908 * @param {Function} [config.complete] Function to call when scrolling completes
910 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
911 // Configuration initialization
912 config
= config
|| {};
915 callback
= typeof config
.complete
=== 'function' && config
.complete
,
916 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
918 eld
= this.getDimensions( el
),
919 scd
= this.getDimensions( sc
),
920 $win
= $( this.getWindow( el
) );
922 // Compute the distances between the edges of el and the edges of the scroll viewport
923 if ( $sc
.is( 'body' ) ) {
924 // If the scrollable container is the <body> this is easy
927 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
929 right
: $win
.innerWidth() - eld
.rect
.right
932 // Otherwise, we have to subtract el's coordinates from sc's coordinates
934 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
935 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
936 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
937 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
941 if ( !config
.direction
|| config
.direction
=== 'y' ) {
943 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
944 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
945 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
948 if ( !config
.direction
|| config
.direction
=== 'x' ) {
949 if ( rel
.left
< 0 ) {
950 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
951 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
952 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
955 if ( !$.isEmptyObject( anim
) ) {
956 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
958 $sc
.queue( function ( next
) {
973 * Get the HTML tag name.
975 * Override this method to base the result on instance information.
977 * @return {string} HTML tag name
979 OO
.ui
.Element
.prototype.getTagName = function () {
980 return this.constructor.static.tagName
;
984 * Check if the element is attached to the DOM
985 * @return {boolean} The element is attached to the DOM
987 OO
.ui
.Element
.prototype.isElementAttached = function () {
988 return $.contains( this.getElementDocument(), this.$element
[0] );
992 * Get the DOM document.
994 * @return {HTMLDocument} Document object
996 OO
.ui
.Element
.prototype.getElementDocument = function () {
997 return OO
.ui
.Element
.getDocument( this.$element
);
1001 * Get the DOM window.
1003 * @return {Window} Window object
1005 OO
.ui
.Element
.prototype.getElementWindow = function () {
1006 return OO
.ui
.Element
.getWindow( this.$element
);
1010 * Get closest scrollable container.
1012 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1013 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1017 * Get group element is in.
1019 * @return {OO.ui.GroupElement|null} Group element, null if none
1021 OO
.ui
.Element
.prototype.getElementGroup = function () {
1022 return this.elementGroup
;
1026 * Set group element is in.
1028 * @param {OO.ui.GroupElement|null} group Group element, null if none
1031 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1032 this.elementGroup
= group
;
1037 * Scroll element into view.
1039 * @param {Object} [config={}]
1041 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1042 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1046 * Bind a handler for an event on this.$element
1048 * @deprecated Use jQuery#on instead.
1049 * @param {string} event
1050 * @param {Function} callback
1052 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1053 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1057 * Unbind a handler bound with #offDOMEvent
1059 * @deprecated Use jQuery#off instead.
1060 * @param {string} event
1061 * @param {Function} callback
1063 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1064 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1069 * Bind a handler for an event on a DOM element.
1071 * Used to be for working around a jQuery bug (jqbug.com/14180),
1072 * but obsolete as of jQuery 1.11.0.
1075 * @deprecated Use jQuery#on instead.
1076 * @param {HTMLElement|jQuery} el DOM element
1077 * @param {string} event Event to bind
1078 * @param {Function} callback Callback to call when the event fires
1080 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1081 $( el
).on( event
, callback
);
1085 * Unbind a handler bound with #static-method-onDOMEvent.
1087 * @deprecated Use jQuery#off instead.
1089 * @param {HTMLElement|jQuery} el DOM element
1090 * @param {string} event Event to unbind
1091 * @param {Function} [callback] Callback to unbind
1093 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1094 $( el
).off( event
, callback
);
1099 * Container for elements.
1103 * @extends OO.ui.Element
1104 * @mixins OO.EventEmitter
1107 * @param {Object} [config] Configuration options
1109 OO
.ui
.Layout
= function OoUiLayout( config
) {
1110 // Initialize config
1111 config
= config
|| {};
1113 // Parent constructor
1114 OO
.ui
.Layout
.super.call( this, config
);
1116 // Mixin constructors
1117 OO
.EventEmitter
.call( this );
1120 this.$element
.addClass( 'oo-ui-layout' );
1125 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1126 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1129 * User interface control.
1133 * @extends OO.ui.Element
1134 * @mixins OO.EventEmitter
1137 * @param {Object} [config] Configuration options
1138 * @cfg {boolean} [disabled=false] Disable
1140 OO
.ui
.Widget
= function OoUiWidget( config
) {
1141 // Initialize config
1142 config
= $.extend( { disabled
: false }, config
);
1144 // Parent constructor
1145 OO
.ui
.Widget
.super.call( this, config
);
1147 // Mixin constructors
1148 OO
.EventEmitter
.call( this );
1151 this.visible
= true;
1152 this.disabled
= null;
1153 this.wasDisabled
= null;
1156 this.$element
.addClass( 'oo-ui-widget' );
1157 this.setDisabled( !!config
.disabled
);
1162 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1163 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1169 * @param {boolean} disabled Widget is disabled
1174 * @param {boolean} visible Widget is visible
1180 * Check if the widget is disabled.
1182 * @param {boolean} Button is disabled
1184 OO
.ui
.Widget
.prototype.isDisabled = function () {
1185 return this.disabled
;
1189 * Check if widget is visible.
1191 * @return {boolean} Widget is visible
1193 OO
.ui
.Widget
.prototype.isVisible = function () {
1194 return this.visible
;
1198 * Set the disabled state of the widget.
1200 * This should probably change the widgets' appearance and prevent it from being used.
1202 * @param {boolean} disabled Disable widget
1205 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1208 this.disabled
= !!disabled
;
1209 isDisabled
= this.isDisabled();
1210 if ( isDisabled
!== this.wasDisabled
) {
1211 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1212 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1213 this.emit( 'disable', isDisabled
);
1215 this.wasDisabled
= isDisabled
;
1221 * Toggle visibility of widget.
1223 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1227 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1228 show
= show
=== undefined ? !this.visible
: !!show
;
1230 if ( show
!== this.isVisible() ) {
1231 this.visible
= show
;
1232 this.$element
.toggle( show
);
1233 this.emit( 'toggle', show
);
1240 * Update the disabled state, in case of changes in parent widget.
1244 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1245 this.setDisabled( this.disabled
);
1250 * Container for elements in a child frame.
1252 * Use together with OO.ui.WindowManager.
1256 * @extends OO.ui.Element
1257 * @mixins OO.EventEmitter
1259 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1260 * teardown processes are executed when the window is closed.
1262 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1263 * - Window manager begins opening window
1264 * - {@link #getSetupProcess} method is called and its result executed
1265 * - {@link #getReadyProcess} method is called and its result executed
1266 * - Window is now open
1268 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1269 * - Window manager begins closing window
1270 * - {@link #getHoldProcess} method is called and its result executed
1271 * - {@link #getTeardownProcess} method is called and its result executed
1272 * - Window is now closed
1274 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1275 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1276 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1277 * processing can complete. Always assume window processes are executed asychronously. See
1278 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1279 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1281 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1282 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1285 * @param {Object} [config] Configuration options
1286 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1290 OO
.ui
.Window
= function OoUiWindow( config
) {
1291 // Configuration initialization
1292 config
= config
|| {};
1294 // Parent constructor
1295 OO
.ui
.Window
.super.call( this, config
);
1297 // Mixin constructors
1298 OO
.EventEmitter
.call( this );
1301 this.manager
= null;
1302 this.initialized
= false;
1303 this.visible
= false;
1304 this.opening
= null;
1305 this.closing
= null;
1308 this.loading
= null;
1309 this.size
= config
.size
|| this.constructor.static.size
;
1310 this.$frame
= this.$( '<div>' );
1314 .addClass( 'oo-ui-window' )
1315 .append( this.$frame
);
1316 this.$frame
.addClass( 'oo-ui-window-frame' );
1318 // NOTE: Additional intitialization will occur when #setManager is called
1323 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1324 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1330 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1333 /* Static Properties */
1336 * Symbolic name of size.
1338 * Size is used if no size is configured during construction.
1342 * @property {string}
1344 OO
.ui
.Window
.static.size
= 'medium';
1346 /* Static Methods */
1349 * Transplant the CSS styles from as parent document to a frame's document.
1351 * This loops over the style sheets in the parent document, and copies their nodes to the
1352 * frame's document. It then polls the document to see when all styles have loaded, and once they
1353 * have, resolves the promise.
1355 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1356 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1357 * Firefox, where the styles won't load until the iframe becomes visible.
1359 * For details of how we arrived at the strategy used in this function, see #load.
1363 * @param {HTMLDocument} parentDoc Document to transplant styles from
1364 * @param {HTMLDocument} frameDoc Document to transplant styles to
1365 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1366 * @return {jQuery.Promise} Promise resolved when styles have loaded
1368 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1369 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1370 $pollNodes
= $( [] ),
1371 // Fake font-family value
1372 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1373 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1374 deferred
= $.Deferred();
1376 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1377 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1378 if ( styleNode
.disabled
) {
1382 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1383 // External stylesheet; use @import
1384 styleText
= '@import url(' + styleNode
.href
+ ');';
1386 // Internal stylesheet; just copy the text
1387 styleText
= styleNode
.textContent
;
1390 // Create a node with a unique ID that we're going to monitor to see when the CSS
1392 if ( styleNode
.oouiFrameTransplantStylesId
) {
1393 // If we're nesting transplantStyles operations and this node already has
1394 // a CSS rule to wait for loading, reuse it
1395 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1397 // Otherwise, create a new ID
1398 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1401 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1402 // The font-family rule will only take effect once the @import finishes
1403 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1406 // Create a node with id=pollNodeId
1407 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1408 .attr( 'id', pollNodeId
)
1409 .appendTo( frameDoc
.body
)
1412 // Add our modified CSS as a <style> tag
1413 newNode
= frameDoc
.createElement( 'style' );
1414 newNode
.textContent
= styleText
;
1415 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1416 frameDoc
.head
.appendChild( newNode
);
1418 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1420 // Poll every 100ms until all external stylesheets have loaded
1421 $pendingPollNodes
= $pollNodes
;
1422 timeoutID
= setTimeout( function pollExternalStylesheets() {
1424 $pendingPollNodes
.length
> 0 &&
1425 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1427 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1430 if ( $pendingPollNodes
.length
=== 0 ) {
1432 if ( timeoutID
!== null ) {
1434 $pollNodes
.remove();
1438 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1441 // ...but give up after a while
1442 if ( timeout
!== 0 ) {
1443 setTimeout( function () {
1445 clearTimeout( timeoutID
);
1447 $pollNodes
.remove();
1450 }, timeout
|| 5000 );
1453 return deferred
.promise();
1459 * Check if window has been initialized.
1461 * @return {boolean} Window has been initialized
1463 OO
.ui
.Window
.prototype.isInitialized = function () {
1464 return this.initialized
;
1468 * Check if window is visible.
1470 * @return {boolean} Window is visible
1472 OO
.ui
.Window
.prototype.isVisible = function () {
1473 return this.visible
;
1477 * Check if window is loading.
1479 * @return {boolean} Window is loading
1481 OO
.ui
.Window
.prototype.isLoading = function () {
1482 return this.loading
&& this.loading
.state() === 'pending';
1486 * Check if window is loaded.
1488 * @return {boolean} Window is loaded
1490 OO
.ui
.Window
.prototype.isLoaded = function () {
1491 return this.loading
&& this.loading
.state() === 'resolved';
1495 * Check if window is opening.
1497 * This is a wrapper around OO.ui.WindowManager#isOpening.
1499 * @return {boolean} Window is opening
1501 OO
.ui
.Window
.prototype.isOpening = function () {
1502 return this.manager
.isOpening( this );
1506 * Check if window is closing.
1508 * This is a wrapper around OO.ui.WindowManager#isClosing.
1510 * @return {boolean} Window is closing
1512 OO
.ui
.Window
.prototype.isClosing = function () {
1513 return this.manager
.isClosing( this );
1517 * Check if window is opened.
1519 * This is a wrapper around OO.ui.WindowManager#isOpened.
1521 * @return {boolean} Window is opened
1523 OO
.ui
.Window
.prototype.isOpened = function () {
1524 return this.manager
.isOpened( this );
1528 * Get the window manager.
1530 * @return {OO.ui.WindowManager} Manager of window
1532 OO
.ui
.Window
.prototype.getManager = function () {
1533 return this.manager
;
1537 * Get the window size.
1539 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1541 OO
.ui
.Window
.prototype.getSize = function () {
1546 * Get the height of the dialog contents.
1548 * @return {number} Content height
1550 OO
.ui
.Window
.prototype.getContentHeight = function () {
1552 // Add buffer for border
1553 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1554 // Use combined heights of children
1555 ( this.$head
.outerHeight( true ) + this.getBodyHeight() + this.$foot
.outerHeight( true ) )
1560 * Get the height of the dialog contents.
1562 * @return {number} Height of content
1564 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1565 return this.$body
[0].scrollHeight
;
1569 * Get the directionality of the frame
1571 * @return {string} Directionality, 'ltr' or 'rtl'
1573 OO
.ui
.Window
.prototype.getDir = function () {
1578 * Get a process for setting up a window for use.
1580 * Each time the window is opened this process will set it up for use in a particular context, based
1581 * on the `data` argument.
1583 * When you override this method, you can add additional setup steps to the process the parent
1584 * method provides using the 'first' and 'next' methods.
1587 * @param {Object} [data] Window opening data
1588 * @return {OO.ui.Process} Setup process
1590 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1591 return new OO
.ui
.Process();
1595 * Get a process for readying a window for use.
1597 * Each time the window is open and setup, this process will ready it up for use in a particular
1598 * context, based on the `data` argument.
1600 * When you override this method, you can add additional setup steps to the process the parent
1601 * method provides using the 'first' and 'next' methods.
1604 * @param {Object} [data] Window opening data
1605 * @return {OO.ui.Process} Setup process
1607 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1608 return new OO
.ui
.Process();
1612 * Get a process for holding a window from use.
1614 * Each time the window is closed, this process will hold it from use in a particular context, based
1615 * on the `data` argument.
1617 * When you override this method, you can add additional setup steps to the process the parent
1618 * method provides using the 'first' and 'next' methods.
1621 * @param {Object} [data] Window closing data
1622 * @return {OO.ui.Process} Hold process
1624 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1625 return new OO
.ui
.Process();
1629 * Get a process for tearing down a window after use.
1631 * Each time the window is closed this process will tear it down and do something with the user's
1632 * interactions within the window, based on the `data` argument.
1634 * When you override this method, you can add additional teardown steps to the process the parent
1635 * method provides using the 'first' and 'next' methods.
1638 * @param {Object} [data] Window closing data
1639 * @return {OO.ui.Process} Teardown process
1641 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1642 return new OO
.ui
.Process();
1646 * Toggle visibility of window.
1648 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1649 * instead of display.
1651 * @param {boolean} [show] Make window visible, omit to toggle visibility
1655 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1656 show
= show
=== undefined ? !this.visible
: !!show
;
1658 if ( show
!== this.isVisible() ) {
1659 this.visible
= show
;
1661 if ( this.isolated
&& !this.isLoaded() ) {
1662 // Hide the window using visibility instead of display until loading is complete
1663 // Can't use display: none; because that prevents the iframe from loading in Firefox
1664 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1666 this.$element
.toggle( show
).css( 'visibility', '' );
1668 this.emit( 'toggle', show
);
1675 * Set the window manager.
1677 * This must be called before initialize. Calling it more than once will cause an error.
1679 * @param {OO.ui.WindowManager} manager Manager for this window
1680 * @throws {Error} If called more than once
1683 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1684 if ( this.manager
) {
1685 throw new Error( 'Cannot set window manager, window already has a manager' );
1689 this.manager
= manager
;
1690 this.isolated
= manager
.shouldIsolate();
1693 if ( this.isolated
) {
1694 this.$iframe
= this.$( '<iframe>' );
1695 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1696 this.$frame
.append( this.$iframe
);
1697 this.$ = function () {
1698 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1700 // WARNING: Do not use this.$ again until #initialize is called
1702 this.$content
= this.$( '<div>' );
1703 this.$document
= $( this.getElementDocument() );
1704 this.$content
.addClass( 'oo-ui-window-content' );
1705 this.$frame
.append( this.$content
);
1707 this.toggle( false );
1709 // Figure out directionality:
1710 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1716 * Set the window size.
1718 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1721 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1723 this.manager
.updateWindowSize( this );
1728 * Set window dimensions.
1730 * Properties are applied to the frame container.
1732 * @param {Object} dim CSS dimension properties
1733 * @param {string|number} [dim.width] Width
1734 * @param {string|number} [dim.minWidth] Minimum width
1735 * @param {string|number} [dim.maxWidth] Maximum width
1736 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1737 * @param {string|number} [dim.minWidth] Minimum height
1738 * @param {string|number} [dim.maxWidth] Maximum height
1741 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1742 // Apply width before height so height is not based on wrapping content using the wrong width
1744 width
: dim
.width
|| '',
1745 minWidth
: dim
.minWidth
|| '',
1746 maxWidth
: dim
.maxWidth
|| ''
1749 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1750 minHeight
: dim
.minHeight
|| '',
1751 maxHeight
: dim
.maxHeight
|| ''
1757 * Initialize window contents.
1759 * The first time the window is opened, #initialize is called when it's safe to begin populating
1760 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1762 * Once this method is called, this.$ can be used to create elements within the frame.
1764 * @throws {Error} If not attached to a manager
1767 OO
.ui
.Window
.prototype.initialize = function () {
1768 if ( !this.manager
) {
1769 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1773 this.$head
= this.$( '<div>' );
1774 this.$body
= this.$( '<div>' );
1775 this.$foot
= this.$( '<div>' );
1776 this.$overlay
= this.$( '<div>' );
1779 this.$head
.addClass( 'oo-ui-window-head' );
1780 this.$body
.addClass( 'oo-ui-window-body' );
1781 this.$foot
.addClass( 'oo-ui-window-foot' );
1782 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1783 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$overlay
);
1791 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1792 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1794 * @param {Object} [data] Window opening data
1795 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1796 * first argument will be a promise which will be resolved when the window begins closing
1798 OO
.ui
.Window
.prototype.open = function ( data
) {
1799 return this.manager
.openWindow( this, data
);
1805 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1806 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1808 * @param {Object} [data] Window closing data
1809 * @return {jQuery.Promise} Promise resolved when window is closed
1811 OO
.ui
.Window
.prototype.close = function ( data
) {
1812 return this.manager
.closeWindow( this, data
);
1818 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1821 * @param {Object} [data] Window opening data
1822 * @return {jQuery.Promise} Promise resolved when window is setup
1824 OO
.ui
.Window
.prototype.setup = function ( data
) {
1826 deferred
= $.Deferred();
1828 this.$element
.show();
1829 this.visible
= true;
1830 this.getSetupProcess( data
).execute().done( function () {
1831 // Force redraw by asking the browser to measure the elements' widths
1832 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
1833 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
1837 return deferred
.promise();
1843 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1846 * @param {Object} [data] Window opening data
1847 * @return {jQuery.Promise} Promise resolved when window is ready
1849 OO
.ui
.Window
.prototype.ready = function ( data
) {
1851 deferred
= $.Deferred();
1853 this.$content
.focus();
1854 this.getReadyProcess( data
).execute().done( function () {
1855 // Force redraw by asking the browser to measure the elements' widths
1856 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
1857 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
1861 return deferred
.promise();
1867 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1870 * @param {Object} [data] Window closing data
1871 * @return {jQuery.Promise} Promise resolved when window is held
1873 OO
.ui
.Window
.prototype.hold = function ( data
) {
1875 deferred
= $.Deferred();
1877 this.getHoldProcess( data
).execute().done( function () {
1878 // Get the focused element within the window's content
1879 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
1881 // Blur the focused element
1882 if ( $focus
.length
) {
1886 // Force redraw by asking the browser to measure the elements' widths
1887 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
1888 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
1892 return deferred
.promise();
1898 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1901 * @param {Object} [data] Window closing data
1902 * @return {jQuery.Promise} Promise resolved when window is torn down
1904 OO
.ui
.Window
.prototype.teardown = function ( data
) {
1906 deferred
= $.Deferred();
1908 this.getTeardownProcess( data
).execute().done( function () {
1909 // Force redraw by asking the browser to measure the elements' widths
1910 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
1911 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
1912 win
.$element
.hide();
1913 win
.visible
= false;
1917 return deferred
.promise();
1921 * Load the frame contents.
1923 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
1924 * promise will be resolved. Calling while loading will return a promise but not trigger a new
1925 * loading cycle. Calling after loading is complete will return a promise that's already been
1928 * Sounds simple right? Read on...
1930 * When you create a dynamic iframe using open/write/close, the window.load event for the
1931 * iframe is triggered when you call close, and there's no further load event to indicate that
1932 * everything is actually loaded.
1934 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
1935 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
1936 * are added to document.styleSheets immediately, and the only way you can determine whether they've
1937 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
1938 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
1940 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
1941 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
1942 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
1943 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
1944 * accessing .cssRules is allowed.
1946 * However, now that we control the styles we're injecting, we might as well do away with
1947 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
1948 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
1949 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
1950 * font-family rule is not applied until after the `@import` finishes.
1952 * All this stylesheet injection and polling magic is in #transplantStyles.
1954 * @return {jQuery.Promise} Promise resolved when loading is complete
1957 OO
.ui
.Window
.prototype.load = function () {
1958 var sub
, doc
, loading
,
1961 // Non-isolated windows are already "loaded"
1962 if ( !this.loading
&& !this.isolated
) {
1963 this.loading
= $.Deferred().resolve();
1965 // Set initialized state after so sub-classes aren't confused by it being set by calling
1966 // their parent initialize method
1967 this.initialized
= true;
1970 // Return existing promise if already loading or loaded
1971 if ( this.loading
) {
1972 return this.loading
.promise();
1976 loading
= this.loading
= $.Deferred();
1977 sub
= this.$iframe
.prop( 'contentWindow' );
1980 // Initialize contents
1985 '<body class="oo-ui-window-isolated oo-ui-window-content oo-ui-' + this.dir
+ '"' +
1986 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
1993 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$element
);
1994 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
1995 this.$document
= this.$( doc
);
1998 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
1999 .always( function () {
2000 // Initialize isolated windows
2002 // Set initialized state after so sub-classes aren't confused by it being set by calling
2003 // their parent initialize method
2004 win
.initialized
= true;
2005 // Undo the visibility: hidden; hack and apply display: none;
2006 // We can do this safely now that the iframe has initialized
2007 // (don't do this from within #initialize because it has to happen
2008 // after the all subclasses have been handled as well).
2009 win
.toggle( win
.isVisible() );
2014 return loading
.promise();
2018 * Base class for all dialogs.
2021 * - Manage the window (open and close, etc.).
2022 * - Store the internal name and display title.
2023 * - A stack to track one or more pending actions.
2024 * - Manage a set of actions that can be performed.
2025 * - Configure and create action widgets.
2028 * - Close the dialog with Escape key.
2029 * - Visually lock the dialog while an action is in
2030 * progress (aka "pending").
2032 * Subclass responsibilities:
2033 * - Display the title somewhere.
2034 * - Add content to the dialog.
2035 * - Provide a UI to close the dialog.
2036 * - Display the action widgets somewhere.
2040 * @extends OO.ui.Window
2041 * @mixins OO.ui.LabeledElement
2044 * @param {Object} [config] Configuration options
2046 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2047 // Parent constructor
2048 OO
.ui
.Dialog
.super.call( this, config
);
2051 this.actions
= new OO
.ui
.ActionSet();
2052 this.attachedActions
= [];
2053 this.currentAction
= null;
2057 this.actions
.connect( this, {
2058 click
: 'onActionClick',
2059 resize
: 'onActionResize',
2060 change
: 'onActionsChange'
2065 .addClass( 'oo-ui-dialog' )
2066 .attr( 'role', 'dialog' );
2071 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2073 /* Static Properties */
2076 * Symbolic name of dialog.
2081 * @property {string}
2083 OO
.ui
.Dialog
.static.name
= '';
2091 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2093 OO
.ui
.Dialog
.static.title
= '';
2096 * List of OO.ui.ActionWidget configuration options.
2100 * @property {Object[]}
2102 OO
.ui
.Dialog
.static.actions
= [];
2105 * Close dialog when the escape key is pressed.
2110 * @property {boolean}
2112 OO
.ui
.Dialog
.static.escapable
= true;
2117 * Handle frame document key down events.
2119 * @param {jQuery.Event} e Key down event
2121 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2122 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2129 * Handle action resized events.
2131 * @param {OO.ui.ActionWidget} action Action that was resized
2133 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2134 // Override in subclass
2138 * Handle action click events.
2140 * @param {OO.ui.ActionWidget} action Action that was clicked
2142 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2143 if ( !this.isPending() ) {
2144 this.currentAction
= action
;
2145 this.executeAction( action
.getAction() );
2150 * Handle actions change event.
2152 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2153 this.detachActions();
2154 if ( !this.isClosing() ) {
2155 this.attachActions();
2160 * Check if input is pending.
2164 OO
.ui
.Dialog
.prototype.isPending = function () {
2165 return !!this.pending
;
2169 * Get set of actions.
2171 * @return {OO.ui.ActionSet}
2173 OO
.ui
.Dialog
.prototype.getActions = function () {
2174 return this.actions
;
2178 * Get a process for taking action.
2180 * When you override this method, you can add additional accept steps to the process the parent
2181 * method provides using the 'first' and 'next' methods.
2184 * @param {string} [action] Symbolic name of action
2185 * @return {OO.ui.Process} Action process
2187 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2188 return new OO
.ui
.Process()
2189 .next( function () {
2191 // An empty action always closes the dialog without data, which should always be
2192 // safe and make no changes
2201 * @param {Object} [data] Dialog opening data
2202 * @param {jQuery|string|Function|null} [data.label] Dialog label, omit to use #static-label
2203 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2204 * action item, omit to use #static-actions
2206 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2210 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2211 .next( function () {
2214 config
= this.constructor.static,
2215 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2217 this.title
.setLabel(
2218 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2220 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2222 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2225 this.actions
.add( items
);
2232 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2234 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2235 .first( function () {
2236 this.actions
.clear();
2237 this.currentAction
= null;
2244 OO
.ui
.Dialog
.prototype.initialize = function () {
2246 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2249 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2252 if ( this.constructor.static.escapable
) {
2253 this.$document
.on( 'keydown', OO
.ui
.bind( this.onDocumentKeyDown
, this ) );
2257 this.$content
.addClass( 'oo-ui-dialog-content' );
2261 * Attach action actions.
2263 OO
.ui
.Dialog
.prototype.attachActions = function () {
2264 // Remember the list of potentially attached actions
2265 this.attachedActions
= this.actions
.get();
2269 * Detach action actions.
2273 OO
.ui
.Dialog
.prototype.detachActions = function () {
2276 // Detach all actions that may have been previously attached
2277 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2278 this.attachedActions
[i
].$element
.detach();
2280 this.attachedActions
= [];
2284 * Execute an action.
2286 * @param {string} action Symbolic name of action to execute
2287 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2289 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2291 return this.getActionProcess( action
).execute()
2292 .always( OO
.ui
.bind( this.popPending
, this ) );
2296 * Increase the pending stack.
2300 OO
.ui
.Dialog
.prototype.pushPending = function () {
2301 if ( this.pending
=== 0 ) {
2302 this.$content
.addClass( 'oo-ui-actionDialog-content-pending' );
2303 this.$head
.addClass( 'oo-ui-texture-pending' );
2311 * Reduce the pending stack.
2317 OO
.ui
.Dialog
.prototype.popPending = function () {
2318 if ( this.pending
=== 1 ) {
2319 this.$content
.removeClass( 'oo-ui-actionDialog-content-pending' );
2320 this.$head
.removeClass( 'oo-ui-texture-pending' );
2322 this.pending
= Math
.max( 0, this.pending
- 1 );
2328 * Collection of windows.
2331 * @extends OO.ui.Element
2332 * @mixins OO.EventEmitter
2334 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2335 * already opening or opened, the current window will be closed without data. Empty closing data
2336 * should always result in the window being closed without causing constructive or destructive
2339 * As a window is opened and closed, it passes through several stages and the manager emits several
2340 * corresponding events.
2342 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2343 * - {@link #event-opening} is emitted with `opening` promise
2344 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2345 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2346 * - `setup` progress notification is emitted from opening promise
2347 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2348 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2349 * - `ready` progress notification is emitted from opening promise
2350 * - `opening` promise is resolved with `opened` promise
2351 * - Window is now open
2353 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2354 * - `opened` promise is resolved with `closing` promise
2355 * - {@link #event-closing} is emitted with `closing` promise
2356 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2357 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2358 * - `hold` progress notification is emitted from opening promise
2359 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2360 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2361 * - `teardown` progress notification is emitted from opening promise
2362 * - Closing promise is resolved
2363 * - Window is now closed
2366 * @param {Object} [config] Configuration options
2367 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2368 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2369 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2371 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2372 // Configuration initialization
2373 config
= config
|| {};
2375 // Parent constructor
2376 OO
.ui
.WindowManager
.super.call( this, config
);
2378 // Mixin constructors
2379 OO
.EventEmitter
.call( this );
2382 this.factory
= config
.factory
;
2383 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2384 this.isolate
= !!config
.isolate
;
2386 this.opening
= null;
2388 this.closing
= null;
2389 this.preparingToOpen
= null;
2390 this.preparingToClose
= null;
2392 this.currentWindow
= null;
2393 this.$ariaHidden
= null;
2394 this.requestedSize
= null;
2395 this.onWindowResizeTimeout
= null;
2396 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
2397 this.afterWindowResizeHandler
= OO
.ui
.bind( this.afterWindowResize
, this );
2398 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
2399 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
2402 this.$element
.on( 'mousedown', false );
2406 .addClass( 'oo-ui-windowManager' )
2407 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2412 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2413 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2418 * Window is opening.
2420 * Fired when the window begins to be opened.
2423 * @param {OO.ui.Window} win Window that's being opened
2424 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2425 * resolved the first argument will be a promise which will be resolved when the window begins
2426 * closing, the second argument will be the opening data; progress notifications will be fired on
2427 * the promise for `setup` and `ready` when those processes are completed respectively.
2428 * @param {Object} data Window opening data
2432 * Window is closing.
2434 * Fired when the window begins to be closed.
2437 * @param {OO.ui.Window} win Window that's being closed
2438 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2439 * is resolved the first argument will be a the closing data; progress notifications will be fired
2440 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2441 * @param {Object} data Window closing data
2444 /* Static Properties */
2447 * Map of symbolic size names and CSS properties.
2451 * @property {Object}
2453 OO
.ui
.WindowManager
.static.sizes
= {
2464 // These can be non-numeric because they are never used in calculations
2471 * Symbolic name of default size.
2473 * Default size is used if the window's requested size is not recognized.
2477 * @property {string}
2479 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2484 * Handle window resize events.
2486 * @param {jQuery.Event} e Window resize event
2488 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2489 clearTimeout( this.onWindowResizeTimeout
);
2490 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2494 * Handle window resize events.
2496 * @param {jQuery.Event} e Window resize event
2498 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2499 if ( this.currentWindow
) {
2500 this.updateWindowSize( this.currentWindow
);
2505 * Handle window mouse wheel events.
2507 * @param {jQuery.Event} e Mouse wheel event
2509 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2514 * Handle document key down events.
2516 * @param {jQuery.Event} e Key down event
2518 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2519 switch ( e
.which
) {
2520 case OO
.ui
.Keys
.PAGEUP
:
2521 case OO
.ui
.Keys
.PAGEDOWN
:
2522 case OO
.ui
.Keys
.END
:
2523 case OO
.ui
.Keys
.HOME
:
2524 case OO
.ui
.Keys
.LEFT
:
2526 case OO
.ui
.Keys
.RIGHT
:
2527 case OO
.ui
.Keys
.DOWN
:
2528 // Prevent any key events that might cause scrolling
2534 * Check if window is opening.
2536 * @return {boolean} Window is opening
2538 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2539 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2543 * Check if window is closing.
2545 * @return {boolean} Window is closing
2547 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2548 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2552 * Check if window is opened.
2554 * @return {boolean} Window is opened
2556 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2557 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2561 * Check if window contents should be isolated.
2563 * Window content isolation is done using inline frames.
2565 * @return {boolean} Window contents should be isolated
2567 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2568 return this.isolate
;
2572 * Check if a window is being managed.
2574 * @param {OO.ui.Window} win Window to check
2575 * @return {boolean} Window is being managed
2577 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2580 for ( name
in this.windows
) {
2581 if ( this.windows
[name
] === win
) {
2590 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2592 * @param {OO.ui.Window} win Window being opened
2593 * @param {Object} [data] Window opening data
2594 * @return {number} Milliseconds to wait
2596 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2601 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2603 * @param {OO.ui.Window} win Window being opened
2604 * @param {Object} [data] Window opening data
2605 * @return {number} Milliseconds to wait
2607 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2612 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2614 * @param {OO.ui.Window} win Window being closed
2615 * @param {Object} [data] Window closing data
2616 * @return {number} Milliseconds to wait
2618 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2623 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2625 * @param {OO.ui.Window} win Window being closed
2626 * @param {Object} [data] Window closing data
2627 * @return {number} Milliseconds to wait
2629 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2630 return this.modal
? 250 : 0;
2634 * Get managed window by symbolic name.
2636 * If window is not yet instantiated, it will be instantiated and added automatically.
2638 * @param {string} name Symbolic window name
2639 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2640 * @throws {Error} If the symbolic name is unrecognized by the factory
2641 * @throws {Error} If the symbolic name unrecognized as a managed window
2643 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2644 var deferred
= $.Deferred(),
2645 win
= this.windows
[name
];
2647 if ( !( win
instanceof OO
.ui
.Window
) ) {
2648 if ( this.factory
) {
2649 if ( !this.factory
.lookup( name
) ) {
2650 deferred
.reject( new OO
.ui
.Error(
2651 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2654 win
= this.factory
.create( name
, this, { $: this.$ } );
2655 this.addWindows( [ win
] );
2656 deferred
.resolve( win
);
2659 deferred
.reject( new OO
.ui
.Error(
2660 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2664 deferred
.resolve( win
);
2667 return deferred
.promise();
2671 * Get current window.
2673 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2675 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2676 return this.currentWindow
;
2682 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2683 * @param {Object} [data] Window opening data
2684 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2685 * for more details about the `opening` promise
2688 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2691 opening
= $.Deferred();
2693 // Argument handling
2694 if ( typeof win
=== 'string' ) {
2695 return this.getWindow( win
).then( function ( win
) {
2696 return manager
.openWindow( win
, data
);
2701 if ( !this.hasWindow( win
) ) {
2702 opening
.reject( new OO
.ui
.Error(
2703 'Cannot open window: window is not attached to manager'
2705 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2706 opening
.reject( new OO
.ui
.Error(
2707 'Cannot open window: another window is opening or open'
2712 if ( opening
.state() !== 'rejected' ) {
2713 // Begin loading the window if it's not loading or loaded already - may take noticable time
2714 // and we want to do this in paralell with any other preparatory actions
2715 if ( !win
.isLoading() && !win
.isLoaded() ) {
2716 // Finish initializing the window (must be done after manager is attached to DOM)
2717 win
.setManager( this );
2718 preparing
.push( win
.load() );
2721 if ( this.closing
) {
2722 // If a window is currently closing, wait for it to complete
2723 preparing
.push( this.closing
);
2726 this.preparingToOpen
= $.when
.apply( $, preparing
);
2727 // Ensure handlers get called after preparingToOpen is set
2728 this.preparingToOpen
.done( function () {
2729 if ( manager
.modal
) {
2730 manager
.$( manager
.getElementDocument() ).on( {
2731 // Prevent scrolling by keys in top-level window
2732 keydown
: manager
.onDocumentKeyDownHandler
2734 manager
.$( manager
.getElementWindow() ).on( {
2735 // Prevent scrolling by wheel in top-level window
2736 mousewheel
: manager
.onWindowMouseWheelHandler
,
2737 // Start listening for top-level window dimension changes
2738 'orientationchange resize': manager
.onWindowResizeHandler
2740 // Hide other content from screen readers
2741 manager
.$ariaHidden
= $( 'body' )
2743 .not( manager
.$element
.parentsUntil( 'body' ).last() )
2744 .attr( 'aria-hidden', '' );
2746 manager
.currentWindow
= win
;
2747 manager
.opening
= opening
;
2748 manager
.preparingToOpen
= null;
2749 manager
.emit( 'opening', win
, opening
, data
);
2750 setTimeout( function () {
2751 win
.setup( data
).then( function () {
2752 manager
.updateWindowSize( win
);
2753 manager
.opening
.notify( { state
: 'setup' } );
2754 setTimeout( function () {
2755 win
.ready( data
).then( function () {
2756 manager
.updateWindowSize( win
);
2757 manager
.opening
.notify( { state
: 'ready' } );
2758 manager
.opening
= null;
2759 manager
.opened
= $.Deferred();
2760 opening
.resolve( manager
.opened
.promise(), data
);
2762 }, manager
.getReadyDelay() );
2764 }, manager
.getSetupDelay() );
2768 return opening
.promise();
2774 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2775 * @param {Object} [data] Window closing data
2776 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-closing}
2777 * for more details about the `closing` promise
2778 * @throws {Error} If no window by that name is being managed
2781 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2784 closing
= $.Deferred(),
2785 opened
= this.opened
;
2787 // Argument handling
2788 if ( typeof win
=== 'string' ) {
2789 win
= this.windows
[win
];
2790 } else if ( !this.hasWindow( win
) ) {
2796 closing
.reject( new OO
.ui
.Error(
2797 'Cannot close window: window is not attached to manager'
2799 } else if ( win
!== this.currentWindow
) {
2800 closing
.reject( new OO
.ui
.Error(
2801 'Cannot close window: window already closed with different data'
2803 } else if ( this.preparingToClose
|| this.closing
) {
2804 closing
.reject( new OO
.ui
.Error(
2805 'Cannot close window: window already closing with different data'
2810 if ( closing
.state() !== 'rejected' ) {
2811 if ( this.opening
) {
2812 // If the window is currently opening, close it when it's done
2813 preparing
.push( this.opening
);
2816 this.preparingToClose
= $.when
.apply( $, preparing
);
2817 // Ensure handlers get called after preparingToClose is set
2818 this.preparingToClose
.done( function () {
2819 manager
.closing
= closing
;
2820 manager
.preparingToClose
= null;
2821 manager
.emit( 'closing', win
, closing
, data
);
2822 manager
.opened
= null;
2823 opened
.resolve( closing
.promise(), data
);
2824 setTimeout( function () {
2825 win
.hold( data
).then( function () {
2826 closing
.notify( { state
: 'hold' } );
2827 setTimeout( function () {
2828 win
.teardown( data
).then( function () {
2829 closing
.notify( { state
: 'teardown' } );
2830 if ( manager
.modal
) {
2831 manager
.$( manager
.getElementDocument() ).off( {
2832 // Allow scrolling by keys in top-level window
2833 keydown
: manager
.onDocumentKeyDownHandler
2835 manager
.$( manager
.getElementWindow() ).off( {
2836 // Allow scrolling by wheel in top-level window
2837 mousewheel
: manager
.onWindowMouseWheelHandler
,
2838 // Stop listening for top-level window dimension changes
2839 'orientationchange resize': manager
.onWindowResizeHandler
2842 // Restore screen reader visiblity
2843 if ( manager
.$ariaHidden
) {
2844 manager
.$ariaHidden
.removeAttr( 'aria-hidden' );
2845 manager
.$ariaHidden
= null;
2847 manager
.closing
= null;
2848 manager
.currentWindow
= null;
2849 closing
.resolve( data
);
2851 }, manager
.getTeardownDelay() );
2853 }, manager
.getHoldDelay() );
2857 return closing
.promise();
2863 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2864 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2865 * a statically configured symbolic name
2867 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2868 var i
, len
, win
, name
, list
;
2870 if ( $.isArray( windows
) ) {
2871 // Convert to map of windows by looking up symbolic names from static configuration
2873 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2874 name
= windows
[i
].constructor.static.name
;
2875 if ( typeof name
!== 'string' ) {
2876 throw new Error( 'Cannot add window' );
2878 list
[name
] = windows
[i
];
2880 } else if ( $.isPlainObject( windows
) ) {
2885 for ( name
in list
) {
2887 this.windows
[name
] = win
;
2888 this.$element
.append( win
.$element
);
2895 * Windows will be closed before they are removed.
2897 * @param {string} name Symbolic name of window to remove
2898 * @return {jQuery.Promise} Promise resolved when window is closed and removed
2899 * @throws {Error} If windows being removed are not being managed
2901 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
2902 var i
, len
, win
, name
,
2905 cleanup = function ( name
, win
) {
2906 delete manager
.windows
[name
];
2907 win
.$element
.detach();
2910 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
2912 win
= this.windows
[name
];
2914 throw new Error( 'Cannot remove window' );
2916 promises
.push( this.closeWindow( name
).then( OO
.ui
.bind( cleanup
, null, name
, win
) ) );
2919 return $.when
.apply( $, promises
);
2923 * Remove all windows.
2925 * Windows will be closed before they are removed.
2927 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
2929 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
2930 return this.removeWindows( Object
.keys( this.windows
) );
2936 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
2940 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
2941 // Bypass for non-current, and thus invisible, windows
2942 if ( win
!== this.currentWindow
) {
2946 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
2947 sizes
= this.constructor.static.sizes
,
2948 size
= win
.getSize();
2950 if ( !sizes
[size
] ) {
2951 size
= this.constructor.static.defaultSize
;
2953 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
2957 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
2958 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
2959 win
.setDimensions( sizes
[size
] );
2969 * @param {string|jQuery} message Description of error
2970 * @param {Object} [config] Configuration options
2971 * @cfg {boolean} [recoverable=true] Error is recoverable
2973 OO
.ui
.Error
= function OoUiElement( message
, config
) {
2974 // Configuration initialization
2975 config
= config
|| {};
2978 this.message
= message
instanceof jQuery
? message
: String( message
);
2979 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
2984 OO
.initClass( OO
.ui
.Error
);
2989 * Check if error can be recovered from.
2991 * @return {boolean} Error is recoverable
2993 OO
.ui
.Error
.prototype.isRecoverable = function () {
2994 return this.recoverable
;
2998 * Get error message as DOM nodes.
3000 * @return {jQuery} Error message in DOM nodes
3002 OO
.ui
.Error
.prototype.getMessage = function () {
3003 return this.message
instanceof jQuery
?
3004 this.message
.clone() :
3005 $( '<div>' ).text( this.message
).contents();
3009 * Get error message as text.
3011 * @return {string} Error message
3013 OO
.ui
.Error
.prototype.getMessageText = function () {
3014 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3018 * A list of functions, called in sequence.
3020 * If a function added to a process returns boolean false the process will stop; if it returns an
3021 * object with a `promise` method the process will use the promise to either continue to the next
3022 * step when the promise is resolved or stop when the promise is rejected.
3027 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3028 * call, see #createStep for more information
3029 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3031 * @return {Object} Step object, with `callback` and `context` properties
3033 OO
.ui
.Process = function ( step
, context
) {
3038 if ( step
!== undefined ) {
3039 this.next( step
, context
);
3045 OO
.initClass( OO
.ui
.Process
);
3050 * Start the process.
3052 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3053 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3054 * process, the remaining steps will not be taken
3056 OO
.ui
.Process
.prototype.execute = function () {
3057 var i
, len
, promise
;
3060 * Continue execution.
3063 * @param {Array} step A function and the context it should be called in
3064 * @return {Function} Function that continues the process
3066 function proceed( step
) {
3067 return function () {
3068 // Execute step in the correct context
3070 result
= step
.callback
.call( step
.context
);
3072 if ( result
=== false ) {
3073 // Use rejected promise for boolean false results
3074 return $.Deferred().reject( [] ).promise();
3076 if ( typeof result
=== 'number' ) {
3078 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3080 // Use a delayed promise for numbers, expecting them to be in milliseconds
3081 deferred
= $.Deferred();
3082 setTimeout( deferred
.resolve
, result
);
3083 return deferred
.promise();
3085 if ( result
instanceof OO
.ui
.Error
) {
3086 // Use rejected promise for error
3087 return $.Deferred().reject( [ result
] ).promise();
3089 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3090 // Use rejected promise for list of errors
3091 return $.Deferred().reject( result
).promise();
3093 // Duck-type the object to see if it can produce a promise
3094 if ( result
&& $.isFunction( result
.promise
) ) {
3095 // Use a promise generated from the result
3096 return result
.promise();
3098 // Use resolved promise for other results
3099 return $.Deferred().resolve().promise();
3103 if ( this.steps
.length
) {
3104 // Generate a chain reaction of promises
3105 promise
= proceed( this.steps
[0] )();
3106 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3107 promise
= promise
.then( proceed( this.steps
[i
] ) );
3110 promise
= $.Deferred().resolve().promise();
3117 * Create a process step.
3120 * @param {number|jQuery.Promise|Function} step
3122 * - Number of milliseconds to wait; or
3123 * - Promise to wait to be resolved; or
3124 * - Function to execute
3125 * - If it returns boolean false the process will stop
3126 * - If it returns an object with a `promise` method the process will use the promise to either
3127 * continue to the next step when the promise is resolved or stop when the promise is rejected
3128 * - If it returns a number, the process will wait for that number of milliseconds before
3130 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3132 * @return {Object} Step object, with `callback` and `context` properties
3134 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3135 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3137 callback: function () {
3143 if ( $.isFunction( step
) ) {
3149 throw new Error( 'Cannot create process step: number, promise or function expected' );
3153 * Add step to the beginning of the process.
3155 * @inheritdoc #createStep
3156 * @return {OO.ui.Process} this
3159 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3160 this.steps
.unshift( this.createStep( step
, context
) );
3165 * Add step to the end of the process.
3167 * @inheritdoc #createStep
3168 * @return {OO.ui.Process} this
3171 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3172 this.steps
.push( this.createStep( step
, context
) );
3177 * Factory for tools.
3180 * @extends OO.Factory
3183 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3184 // Parent constructor
3185 OO
.ui
.ToolFactory
.super.call( this );
3190 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3195 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3196 var i
, len
, included
, promoted
, demoted
,
3200 // Collect included and not excluded tools
3201 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3204 promoted
= this.extract( promote
, used
);
3205 demoted
= this.extract( demote
, used
);
3208 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3209 if ( !used
[included
[i
]] ) {
3210 auto
.push( included
[i
] );
3214 return promoted
.concat( auto
).concat( demoted
);
3218 * Get a flat list of names from a list of names or groups.
3220 * Tools can be specified in the following ways:
3222 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3223 * - All tools in a group: `{ group: 'group-name' }`
3224 * - All tools: `'*'`
3227 * @param {Array|string} collection List of tools
3228 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3229 * names will be added as properties
3230 * @return {string[]} List of extracted names
3232 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3233 var i
, len
, item
, name
, tool
,
3236 if ( collection
=== '*' ) {
3237 for ( name
in this.registry
) {
3238 tool
= this.registry
[name
];
3240 // Only add tools by group name when auto-add is enabled
3241 tool
.static.autoAddToCatchall
&&
3242 // Exclude already used tools
3243 ( !used
|| !used
[name
] )
3251 } else if ( $.isArray( collection
) ) {
3252 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3253 item
= collection
[i
];
3254 // Allow plain strings as shorthand for named tools
3255 if ( typeof item
=== 'string' ) {
3256 item
= { name
: item
};
3258 if ( OO
.isPlainObject( item
) ) {
3260 for ( name
in this.registry
) {
3261 tool
= this.registry
[name
];
3263 // Include tools with matching group
3264 tool
.static.group
=== item
.group
&&
3265 // Only add tools by group name when auto-add is enabled
3266 tool
.static.autoAddToGroup
&&
3267 // Exclude already used tools
3268 ( !used
|| !used
[name
] )
3276 // Include tools with matching name and exclude already used tools
3277 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3278 names
.push( item
.name
);
3280 used
[item
.name
] = true;
3290 * Factory for tool groups.
3293 * @extends OO.Factory
3296 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3297 // Parent constructor
3298 OO
.Factory
.call( this );
3301 defaultClasses
= this.constructor.static.getDefaultClasses();
3303 // Register default toolgroups
3304 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3305 this.register( defaultClasses
[i
] );
3311 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3313 /* Static Methods */
3316 * Get a default set of classes to be registered on construction
3318 * @return {Function[]} Default classes
3320 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3323 OO
.ui
.ListToolGroup
,
3329 * Element with a button.
3331 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3332 * and access keys for accessibility purposes.
3338 * @param {jQuery} $button Button node, assigned to #$button
3339 * @param {Object} [config] Configuration options
3340 * @cfg {boolean} [framed=true] Render button with a frame
3341 * @cfg {number} [tabIndex=0] Button's tab index, use null to have no tabIndex
3342 * @cfg {string} [accessKey] Button's access key
3344 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
3345 // Configuration initialization
3346 config
= config
|| {};
3349 this.$button
= $button
;
3350 this.tabIndex
= null;
3352 this.active
= false;
3353 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
3356 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
3359 this.$element
.addClass( 'oo-ui-buttonedElement' );
3361 .addClass( 'oo-ui-buttonedElement-button' )
3362 .attr( 'role', 'button' );
3363 this.setTabIndex( config
.tabIndex
|| 0 );
3364 this.setAccessKey( config
.accessKey
);
3365 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3370 OO
.initClass( OO
.ui
.ButtonedElement
);
3372 /* Static Properties */
3375 * Cancel mouse down events.
3379 * @property {boolean}
3381 OO
.ui
.ButtonedElement
.static.cancelButtonMouseDownEvents
= true;
3386 * Handles mouse down events.
3388 * @param {jQuery.Event} e Mouse down event
3390 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function ( e
) {
3391 if ( this.isDisabled() || e
.which
!== 1 ) {
3394 // tabIndex should generally be interacted with via the property, but it's not possible to
3395 // reliably unset a tabIndex via a property so we use the (lowercase) "tabindex" attribute
3396 this.tabIndex
= this.$button
.attr( 'tabindex' );
3397 // Remove the tab-index while the button is down to prevent the button from stealing focus
3398 this.$button
.removeAttr( 'tabindex' );
3399 this.$element
.addClass( 'oo-ui-buttonedElement-pressed' );
3400 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3401 // reliably reapply the tabindex and remove the pressed class
3402 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3403 // Prevent change of focus unless specifically configured otherwise
3404 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3410 * Handles mouse up events.
3412 * @param {jQuery.Event} e Mouse up event
3414 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function ( e
) {
3415 if ( this.isDisabled() || e
.which
!== 1 ) {
3418 // Restore the tab-index after the button is up to restore the button's accesssibility
3419 this.$button
.attr( 'tabindex', this.tabIndex
);
3420 this.$element
.removeClass( 'oo-ui-buttonedElement-pressed' );
3421 // Stop listening for mouseup, since we only needed this once
3422 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3428 * @param {boolean} [framed] Make button framed, omit to toggle
3431 OO
.ui
.ButtonedElement
.prototype.toggleFramed = function ( framed
) {
3432 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3433 if ( framed
!== this.framed
) {
3434 this.framed
= framed
;
3436 .toggleClass( 'oo-ui-buttonedElement-frameless', !framed
)
3437 .toggleClass( 'oo-ui-buttonedElement-framed', framed
);
3446 * @param {number|null} tabIndex Button's tab index, use null to remove
3449 OO
.ui
.ButtonedElement
.prototype.setTabIndex = function ( tabIndex
) {
3450 if ( typeof tabIndex
=== 'number' && tabIndex
>= 0 ) {
3451 this.$button
.attr( 'tabindex', tabIndex
);
3453 this.$button
.removeAttr( 'tabindex' );
3461 * @param {string} accessKey Button's access key, use empty string to remove
3464 OO
.ui
.ButtonedElement
.prototype.setAccessKey = function ( accessKey
) {
3465 if ( typeof accessKey
=== 'string' && accessKey
.length
) {
3466 this.$button
.attr( 'accesskey', accessKey
);
3468 this.$button
.removeAttr( 'accesskey' );
3476 * @param {boolean} [value] Make button active
3479 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
3480 this.$element
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
3485 * Element that can be automatically clipped to visible boundaies.
3491 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
3492 * @param {Object} [config] Configuration options
3494 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
3495 // Configuration initialization
3496 config
= config
|| {};
3499 this.$clippable
= $clippable
;
3500 this.clipping
= false;
3501 this.clipped
= false;
3502 this.$clippableContainer
= null;
3503 this.$clippableScroller
= null;
3504 this.$clippableWindow
= null;
3505 this.idealWidth
= null;
3506 this.idealHeight
= null;
3507 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
3508 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
3511 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
3519 * @param {boolean} value Enable clipping
3522 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
3525 if ( this.clipping
!== value
) {
3526 this.clipping
= value
;
3527 if ( this.clipping
) {
3528 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
3529 // If the clippable container is the body, we have to listen to scroll events and check
3530 // jQuery.scrollTop on the window because of browser inconsistencies
3531 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
3532 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
3533 this.$clippableContainer
;
3534 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
3535 this.$clippableWindow
= this.$( this.getElementWindow() )
3536 .on( 'resize', this.onClippableWindowResizeHandler
);
3537 // Initial clip after visible
3538 setTimeout( OO
.ui
.bind( this.clip
, this ) );
3540 this.$clippableContainer
= null;
3541 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
3542 this.$clippableScroller
= null;
3543 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
3544 this.$clippableWindow
= null;
3552 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
3554 * @return {boolean} Element will be clipped to the visible area
3556 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
3557 return this.clipping
;
3561 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
3563 * @return {boolean} Part of the element is being clipped
3565 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
3566 return this.clipped
;
3570 * Set the ideal size.
3572 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
3573 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
3575 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
3576 this.idealWidth
= width
;
3577 this.idealHeight
= height
;
3581 * Clip element to visible boundaries and allow scrolling when needed.
3583 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
3584 * overlapped by, the visible area of the nearest scrollable container.
3588 OO
.ui
.ClippableElement
.prototype.clip = function () {
3589 if ( !this.clipping
) {
3590 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
3595 cOffset
= this.$clippable
.offset(),
3596 $container
= this.$clippableContainer
.is( 'body' ) ? this.$clippableWindow
: this.$clippableContainer
,
3597 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
3598 ccHeight
= $container
.innerHeight() - buffer
,
3599 ccWidth
= $container
.innerWidth() - buffer
,
3600 scrollTop
= this.$clippableScroller
.scrollTop(),
3601 scrollLeft
= this.$clippableScroller
.scrollLeft(),
3602 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
3603 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
3604 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
3605 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
3606 clipWidth
= desiredWidth
< naturalWidth
,
3607 clipHeight
= desiredHeight
< naturalHeight
;
3610 this.$clippable
.css( { overflowX
: 'auto', width
: desiredWidth
} );
3612 this.$clippable
.css( 'width', this.idealWidth
|| '' );
3613 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
3614 this.$clippable
.css( 'overflowX', '' );
3617 this.$clippable
.css( { overflowY
: 'auto', height
: desiredHeight
} );
3619 this.$clippable
.css( 'height', this.idealHeight
|| '' );
3620 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
3621 this.$clippable
.css( 'overflowY', '' );
3624 this.clipped
= clipWidth
|| clipHeight
;
3630 * Element with named flags that can be added, removed, listed and checked.
3632 * A flag, when set, adds a CSS class on the `$element` by combing `oo-ui-flaggableElement-` with
3633 * the flag name. Flags are primarily useful for styling.
3639 * @param {Object} [config] Configuration options
3640 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
3642 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
3643 // Config initialization
3644 config
= config
|| {};
3650 this.setFlags( config
.flags
);
3657 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
3658 * added/removed properties
3664 * Check if a flag is set.
3666 * @param {string} flag Name of flag
3667 * @return {boolean} Has flag
3669 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
3670 return flag
in this.flags
;
3674 * Get the names of all flags set.
3676 * @return {string[]} flags Flag names
3678 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
3679 return Object
.keys( this.flags
);
3688 OO
.ui
.FlaggableElement
.prototype.clearFlags = function () {
3691 classPrefix
= 'oo-ui-flaggableElement-';
3693 for ( flag
in this.flags
) {
3694 changes
[flag
] = false;
3695 delete this.flags
[flag
];
3696 this.$element
.removeClass( classPrefix
+ flag
);
3699 this.emit( 'flag', changes
);
3705 * Add one or more flags.
3707 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
3708 * keyed by flag name containing boolean set/remove instructions.
3712 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
3715 classPrefix
= 'oo-ui-flaggableElement-';
3717 if ( typeof flags
=== 'string' ) {
3719 this.flags
[flags
] = true;
3720 this.$element
.addClass( classPrefix
+ flags
);
3721 } else if ( $.isArray( flags
) ) {
3722 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
3725 changes
[flag
] = true;
3726 this.flags
[flag
] = true;
3727 this.$element
.addClass( classPrefix
+ flag
);
3729 } else if ( OO
.isPlainObject( flags
) ) {
3730 for ( flag
in flags
) {
3731 if ( flags
[flag
] ) {
3733 changes
[flag
] = true;
3734 this.flags
[flag
] = true;
3735 this.$element
.addClass( classPrefix
+ flag
);
3738 changes
[flag
] = false;
3739 delete this.flags
[flag
];
3740 this.$element
.removeClass( classPrefix
+ flag
);
3745 this.emit( 'flag', changes
);
3751 * Element containing a sequence of child elements.
3757 * @param {jQuery} $group Container node, assigned to #$group
3758 * @param {Object} [config] Configuration options
3760 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
3762 config
= config
|| {};
3765 this.$group
= $group
;
3767 this.aggregateItemEvents
= {};
3773 * Check if there are no items.
3775 * @return {boolean} Group is empty
3777 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3778 return !this.items
.length
;
3784 * @return {OO.ui.Element[]} Items
3786 OO
.ui
.GroupElement
.prototype.getItems = function () {
3787 return this.items
.slice( 0 );
3791 * Add an aggregate item event.
3793 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3794 * and with an additional leading parameter containing the item that emitted the original event.
3795 * Other arguments that were emitted from the original event are passed through.
3797 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3798 * event, use null value to remove aggregation
3799 * @throws {Error} If aggregation already exists
3801 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3802 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3804 for ( itemEvent
in events
) {
3805 groupEvent
= events
[itemEvent
];
3807 // Remove existing aggregated event
3808 if ( itemEvent
in this.aggregateItemEvents
) {
3809 // Don't allow duplicate aggregations
3811 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3813 // Remove event aggregation from existing items
3814 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3815 item
= this.items
[i
];
3816 if ( item
.connect
&& item
.disconnect
) {
3818 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3819 item
.disconnect( this, remove
);
3822 // Prevent future items from aggregating event
3823 delete this.aggregateItemEvents
[itemEvent
];
3826 // Add new aggregate event
3828 // Make future items aggregate event
3829 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3830 // Add event aggregation to existing items
3831 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3832 item
= this.items
[i
];
3833 if ( item
.connect
&& item
.disconnect
) {
3835 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3836 item
.connect( this, add
);
3846 * @param {OO.ui.Element[]} items Item
3847 * @param {number} [index] Index to insert items at
3850 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3851 var i
, len
, item
, event
, events
, currentIndex
,
3854 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3857 // Check if item exists then remove it first, effectively "moving" it
3858 currentIndex
= $.inArray( item
, this.items
);
3859 if ( currentIndex
>= 0 ) {
3860 this.removeItems( [ item
] );
3861 // Adjust index to compensate for removal
3862 if ( currentIndex
< index
) {
3867 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3869 for ( event
in this.aggregateItemEvents
) {
3870 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3872 item
.connect( this, events
);
3874 item
.setElementGroup( this );
3875 itemElements
.push( item
.$element
.get( 0 ) );
3878 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3879 this.$group
.append( itemElements
);
3880 this.items
.push
.apply( this.items
, items
);
3881 } else if ( index
=== 0 ) {
3882 this.$group
.prepend( itemElements
);
3883 this.items
.unshift
.apply( this.items
, items
);
3885 this.items
[index
].$element
.before( itemElements
);
3886 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3895 * Items will be detached, not removed, so they can be used later.
3897 * @param {OO.ui.Element[]} items Items to remove
3900 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3901 var i
, len
, item
, index
, remove
, itemEvent
;
3903 // Remove specific items
3904 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3906 index
= $.inArray( item
, this.items
);
3907 if ( index
!== -1 ) {
3909 item
.connect
&& item
.disconnect
&&
3910 !$.isEmptyObject( this.aggregateItemEvents
)
3913 if ( itemEvent
in this.aggregateItemEvents
) {
3914 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3916 item
.disconnect( this, remove
);
3918 item
.setElementGroup( null );
3919 this.items
.splice( index
, 1 );
3920 item
.$element
.detach();
3930 * Items will be detached, not removed, so they can be used later.
3934 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3935 var i
, len
, item
, remove
, itemEvent
;
3938 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3939 item
= this.items
[i
];
3941 item
.connect
&& item
.disconnect
&&
3942 !$.isEmptyObject( this.aggregateItemEvents
)
3945 if ( itemEvent
in this.aggregateItemEvents
) {
3946 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3948 item
.disconnect( this, remove
);
3950 item
.setElementGroup( null );
3951 item
.$element
.detach();
3959 * Element containing an icon.
3961 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
3962 * a control or convey information in a more space efficient way. Icons should rarely be used
3963 * without labels; such as in a toolbar where space is at a premium or within a context where the
3964 * meaning is very clear to the user.
3970 * @param {jQuery} $icon Icon node, assigned to #$icon
3971 * @param {Object} [config] Configuration options
3972 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
3973 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3976 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
3977 // Config intialization
3978 config
= config
|| {};
3985 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
3986 this.setIcon( config
.icon
|| this.constructor.static.icon
);
3991 OO
.initClass( OO
.ui
.IconedElement
);
3993 /* Static Properties */
3998 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4000 * For i18n purposes, this property can be an object containing a `default` icon name property and
4001 * additional icon names keyed by language code.
4003 * Example of i18n icon definition:
4004 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4008 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4009 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4012 OO
.ui
.IconedElement
.static.icon
= null;
4019 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
4020 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4024 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
4025 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4028 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4030 if ( typeof icon
=== 'string' ) {
4032 if ( icon
.length
) {
4033 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4037 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
4045 * @return {string} Icon
4047 OO
.ui
.IconedElement
.prototype.getIcon = function () {
4052 * Element containing an indicator.
4054 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4055 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4056 * instead of performing an action directly, or an item in a list which has errors that need to be
4063 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
4064 * @param {Object} [config] Configuration options
4065 * @cfg {string} [indicator] Symbolic indicator name
4066 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
4068 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
4069 // Config intialization
4070 config
= config
|| {};
4073 this.$indicator
= $indicator
;
4074 this.indicator
= null;
4075 this.indicatorLabel
= null;
4078 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
4079 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4080 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4085 OO
.initClass( OO
.ui
.IndicatedElement
);
4087 /* Static Properties */
4094 * @property {string|null} Symbolic indicator name or null for no indicator
4096 OO
.ui
.IndicatedElement
.static.indicator
= null;
4103 * @property {string|Function|null} Indicator title text, a function that return text or null for no
4106 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
4113 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4116 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
4117 if ( this.indicator
) {
4118 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4119 this.indicator
= null;
4121 if ( typeof indicator
=== 'string' ) {
4122 indicator
= indicator
.trim();
4123 if ( indicator
.length
) {
4124 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4125 this.indicator
= indicator
;
4128 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
4134 * Set indicator label.
4136 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
4137 * for no indicator title
4140 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4141 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
4143 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
4144 this.$indicator
.attr( 'title', indicatorTitle
);
4146 this.$indicator
.removeAttr( 'title' );
4155 * @return {string} title Symbolic name of indicator
4157 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
4158 return this.indicator
;
4162 * Get indicator title.
4164 * @return {string} Indicator title text
4166 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
4167 return this.indicatorTitle
;
4171 * Element containing a label.
4177 * @param {jQuery} $label Label node, assigned to #$label
4178 * @param {Object} [config] Configuration options
4179 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4180 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4182 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
4183 // Config intialization
4184 config
= config
|| {};
4187 this.$label
= $label
;
4191 this.$label
.addClass( 'oo-ui-labeledElement-label' );
4192 this.setLabel( config
.label
|| this.constructor.static.label
);
4193 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4198 OO
.initClass( OO
.ui
.LabeledElement
);
4200 /* Static Properties */
4207 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
4210 OO
.ui
.LabeledElement
.static.label
= null;
4217 * An empty string will result in the label being hidden. A string containing only whitespace will
4218 * be converted to a single
4220 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
4221 * text; or null for no label
4224 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
4227 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
4228 if ( typeof label
=== 'string' && label
.length
) {
4229 if ( label
.match( /^\s*$/ ) ) {
4230 // Convert whitespace only string to a single non-breaking space
4231 this.$label
.html( ' ' );
4233 this.$label
.text( label
);
4235 } else if ( label
instanceof jQuery
) {
4236 this.$label
.empty().append( label
);
4238 this.$label
.empty();
4241 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
4242 this.$label
.css( 'display', empty
? 'none' : '' );
4250 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4251 * text; or null for no label
4253 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
4262 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
4263 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4264 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4270 * Element containing an OO.ui.PopupWidget object.
4276 * @param {Object} [config] Configuration options
4277 * @cfg {Object} [popup] Configuration to pass to popup
4278 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4280 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
4281 // Configuration initialization
4282 config
= config
|| {};
4285 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4286 { autoClose
: true },
4288 { $: this.$, $autoCloseIgnore
: this.$element
}
4297 * @return {OO.ui.PopupWidget} Popup widget
4299 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
4304 * Element with a title.
4306 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4307 * not visible on touch devices.
4313 * @param {jQuery} $label Titled node, assigned to #$titled
4314 * @param {Object} [config] Configuration options
4315 * @cfg {string|Function} [title] Title text or a function that returns text
4317 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
4318 // Config intialization
4319 config
= config
|| {};
4322 this.$titled
= $titled
;
4326 this.setTitle( config
.title
|| this.constructor.static.title
);
4331 OO
.initClass( OO
.ui
.TitledElement
);
4333 /* Static Properties */
4340 * @property {string|Function} Title text or a function that returns text
4342 OO
.ui
.TitledElement
.static.title
= null;
4349 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4352 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4353 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
4355 if ( typeof title
=== 'string' && title
.length
) {
4356 this.$titled
.attr( 'title', title
);
4358 this.$titled
.removeAttr( 'title' );
4367 * @return {string} Title string
4369 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4374 * Generic toolbar tool.
4378 * @extends OO.ui.Widget
4379 * @mixins OO.ui.IconedElement
4382 * @param {OO.ui.ToolGroup} toolGroup
4383 * @param {Object} [config] Configuration options
4384 * @cfg {string|Function} [title] Title text or a function that returns text
4386 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4387 // Config intialization
4388 config
= config
|| {};
4390 // Parent constructor
4391 OO
.ui
.Tool
.super.call( this, config
);
4393 // Mixin constructors
4394 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4397 this.toolGroup
= toolGroup
;
4398 this.toolbar
= this.toolGroup
.getToolbar();
4399 this.active
= false;
4400 this.$title
= this.$( '<span>' );
4401 this.$link
= this.$( '<a>' );
4405 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
4408 this.$title
.addClass( 'oo-ui-tool-title' );
4410 .addClass( 'oo-ui-tool-link' )
4411 .append( this.$icon
, this.$title
)
4412 .prop( 'tabIndex', 0 )
4413 .attr( 'role', 'button' );
4415 .data( 'oo-ui-tool', this )
4417 'oo-ui-tool ' + 'oo-ui-tool-name-' +
4418 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
4420 .append( this.$link
);
4421 this.setTitle( config
.title
|| this.constructor.static.title
);
4426 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
4427 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
4435 /* Static Properties */
4441 OO
.ui
.Tool
.static.tagName
= 'span';
4444 * Symbolic name of tool.
4449 * @property {string}
4451 OO
.ui
.Tool
.static.name
= '';
4459 * @property {string}
4461 OO
.ui
.Tool
.static.group
= '';
4466 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
4467 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
4468 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
4469 * appended to the title if the tool is part of a bar tool group.
4474 * @property {string|Function} Title text or a function that returns text
4476 OO
.ui
.Tool
.static.title
= '';
4479 * Tool can be automatically added to catch-all groups.
4483 * @property {boolean}
4485 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
4488 * Tool can be automatically added to named groups.
4491 * @property {boolean}
4494 OO
.ui
.Tool
.static.autoAddToGroup
= true;
4497 * Check if this tool is compatible with given data.
4501 * @param {Mixed} data Data to check
4502 * @return {boolean} Tool can be used with data
4504 OO
.ui
.Tool
.static.isCompatibleWith = function () {
4511 * Handle the toolbar state being updated.
4513 * This is an abstract method that must be overridden in a concrete subclass.
4517 OO
.ui
.Tool
.prototype.onUpdateState = function () {
4519 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
4524 * Handle the tool being selected.
4526 * This is an abstract method that must be overridden in a concrete subclass.
4530 OO
.ui
.Tool
.prototype.onSelect = function () {
4532 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
4537 * Check if the button is active.
4539 * @param {boolean} Button is active
4541 OO
.ui
.Tool
.prototype.isActive = function () {
4546 * Make the button appear active or inactive.
4548 * @param {boolean} state Make button appear active
4550 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
4551 this.active
= !!state
;
4552 if ( this.active
) {
4553 this.$element
.addClass( 'oo-ui-tool-active' );
4555 this.$element
.removeClass( 'oo-ui-tool-active' );
4560 * Get the tool title.
4562 * @param {string|Function} title Title text or a function that returns text
4565 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
4566 this.title
= OO
.ui
.resolveMsg( title
);
4572 * Get the tool title.
4574 * @return {string} Title text
4576 OO
.ui
.Tool
.prototype.getTitle = function () {
4581 * Get the tool's symbolic name.
4583 * @return {string} Symbolic name of tool
4585 OO
.ui
.Tool
.prototype.getName = function () {
4586 return this.constructor.static.name
;
4592 OO
.ui
.Tool
.prototype.updateTitle = function () {
4593 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
4594 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
4595 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
4602 .addClass( 'oo-ui-tool-accel' )
4606 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
4607 tooltipParts
.push( this.title
);
4609 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
4610 tooltipParts
.push( accel
);
4612 if ( tooltipParts
.length
) {
4613 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
4615 this.$link
.removeAttr( 'title' );
4622 OO
.ui
.Tool
.prototype.destroy = function () {
4623 this.toolbar
.disconnect( this );
4624 this.$element
.remove();
4628 * Collection of tool groups.
4631 * @extends OO.ui.Element
4632 * @mixins OO.EventEmitter
4633 * @mixins OO.ui.GroupElement
4636 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
4637 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
4638 * @param {Object} [config] Configuration options
4639 * @cfg {boolean} [actions] Add an actions section opposite to the tools
4640 * @cfg {boolean} [shadow] Add a shadow below the toolbar
4642 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
4643 // Configuration initialization
4644 config
= config
|| {};
4646 // Parent constructor
4647 OO
.ui
.Toolbar
.super.call( this, config
);
4649 // Mixin constructors
4650 OO
.EventEmitter
.call( this );
4651 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4654 this.toolFactory
= toolFactory
;
4655 this.toolGroupFactory
= toolGroupFactory
;
4658 this.$bar
= this.$( '<div>' );
4659 this.$actions
= this.$( '<div>' );
4660 this.initialized
= false;
4664 .add( this.$bar
).add( this.$group
).add( this.$actions
)
4665 .on( 'mousedown touchstart', OO
.ui
.bind( this.onPointerDown
, this ) );
4668 this.$group
.addClass( 'oo-ui-toolbar-tools' );
4669 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
4670 if ( config
.actions
) {
4671 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
4672 this.$bar
.append( this.$actions
);
4674 this.$bar
.append( '<div style="clear:both"></div>' );
4675 if ( config
.shadow
) {
4676 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
4678 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
4683 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
4684 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
4685 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
4690 * Get the tool factory.
4692 * @return {OO.ui.ToolFactory} Tool factory
4694 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
4695 return this.toolFactory
;
4699 * Get the tool group factory.
4701 * @return {OO.Factory} Tool group factory
4703 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
4704 return this.toolGroupFactory
;
4708 * Handles mouse down events.
4710 * @param {jQuery.Event} e Mouse down event
4712 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
4713 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
4714 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
4715 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
4721 * Sets up handles and preloads required information for the toolbar to work.
4722 * This must be called immediately after it is attached to a visible document.
4724 OO
.ui
.Toolbar
.prototype.initialize = function () {
4725 this.initialized
= true;
4731 * Tools can be specified in the following ways:
4733 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
4734 * - All tools in a group: `{ group: 'group-name' }`
4735 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
4737 * @param {Object.<string,Array>} groups List of tool group configurations
4738 * @param {Array|string} [groups.include] Tools to include
4739 * @param {Array|string} [groups.exclude] Tools to exclude
4740 * @param {Array|string} [groups.promote] Tools to promote to the beginning
4741 * @param {Array|string} [groups.demote] Tools to demote to the end
4743 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
4744 var i
, len
, type
, group
,
4746 defaultType
= 'bar';
4748 // Cleanup previous groups
4751 // Build out new groups
4752 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
4754 if ( group
.include
=== '*' ) {
4755 // Apply defaults to catch-all groups
4756 if ( group
.type
=== undefined ) {
4757 group
.type
= 'list';
4759 if ( group
.label
=== undefined ) {
4760 group
.label
= 'ooui-toolbar-more';
4763 // Check type has been registered
4764 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
4766 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
4769 this.addItems( items
);
4773 * Remove all tools and groups from the toolbar.
4775 OO
.ui
.Toolbar
.prototype.reset = function () {
4780 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4781 this.items
[i
].destroy();
4787 * Destroys toolbar, removing event handlers and DOM elements.
4789 * Call this whenever you are done using a toolbar.
4791 OO
.ui
.Toolbar
.prototype.destroy = function () {
4793 this.$element
.remove();
4797 * Check if tool has not been used yet.
4799 * @param {string} name Symbolic name of tool
4800 * @return {boolean} Tool is available
4802 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
4803 return !this.tools
[name
];
4807 * Prevent tool from being used again.
4809 * @param {OO.ui.Tool} tool Tool to reserve
4811 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
4812 this.tools
[tool
.getName()] = tool
;
4816 * Allow tool to be used again.
4818 * @param {OO.ui.Tool} tool Tool to release
4820 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
4821 delete this.tools
[tool
.getName()];
4825 * Get accelerator label for tool.
4827 * This is a stub that should be overridden to provide access to accelerator information.
4829 * @param {string} name Symbolic name of tool
4830 * @return {string|undefined} Tool accelerator label if available
4832 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
4837 * Collection of tools.
4839 * Tools can be specified in the following ways:
4841 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
4842 * - All tools in a group: `{ group: 'group-name' }`
4843 * - All tools: `'*'`
4847 * @extends OO.ui.Widget
4848 * @mixins OO.ui.GroupElement
4851 * @param {OO.ui.Toolbar} toolbar
4852 * @param {Object} [config] Configuration options
4853 * @cfg {Array|string} [include=[]] List of tools to include
4854 * @cfg {Array|string} [exclude=[]] List of tools to exclude
4855 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
4856 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
4858 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
4859 // Configuration initialization
4860 config
= config
|| {};
4862 // Parent constructor
4863 OO
.ui
.ToolGroup
.super.call( this, config
);
4865 // Mixin constructors
4866 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4869 this.toolbar
= toolbar
;
4871 this.pressed
= null;
4872 this.autoDisabled
= false;
4873 this.include
= config
.include
|| [];
4874 this.exclude
= config
.exclude
|| [];
4875 this.promote
= config
.promote
|| [];
4876 this.demote
= config
.demote
|| [];
4877 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
4881 'mousedown touchstart': OO
.ui
.bind( this.onPointerDown
, this ),
4882 'mouseup touchend': OO
.ui
.bind( this.onPointerUp
, this ),
4883 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
4884 mouseout
: OO
.ui
.bind( this.onMouseOut
, this )
4886 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
4887 this.aggregate( { disable
: 'itemDisable' } );
4888 this.connect( this, { itemDisable
: 'updateDisabled' } );
4891 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
4893 .addClass( 'oo-ui-toolGroup' )
4894 .append( this.$group
);
4900 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
4901 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
4909 /* Static Properties */
4912 * Show labels in tooltips.
4916 * @property {boolean}
4918 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
4921 * Show acceleration labels in tooltips.
4925 * @property {boolean}
4927 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
4930 * Automatically disable the toolgroup when all tools are disabled
4934 * @property {boolean}
4936 OO
.ui
.ToolGroup
.static.autoDisable
= true;
4943 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
4944 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
4950 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
4951 var i
, item
, allDisabled
= true;
4953 if ( this.constructor.static.autoDisable
) {
4954 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
4955 item
= this.items
[i
];
4956 if ( !item
.isDisabled() ) {
4957 allDisabled
= false;
4961 this.autoDisabled
= allDisabled
;
4963 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
4967 * Handle mouse down events.
4969 * @param {jQuery.Event} e Mouse down event
4971 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
4972 // e.which is 0 for touch events, 1 for left mouse button
4973 if ( !this.isDisabled() && e
.which
<= 1 ) {
4974 this.pressed
= this.getTargetTool( e
);
4975 if ( this.pressed
) {
4976 this.pressed
.setActive( true );
4977 this.getElementDocument().addEventListener(
4978 'mouseup', this.onCapturedMouseUpHandler
, true
4986 * Handle captured mouse up events.
4988 * @param {Event} e Mouse up event
4990 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
4991 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
4992 // onPointerUp may be called a second time, depending on where the mouse is when the button is
4993 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
4994 this.onPointerUp( e
);
4998 * Handle mouse up events.
5000 * @param {jQuery.Event} e Mouse up event
5002 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5003 var tool
= this.getTargetTool( e
);
5005 // e.which is 0 for touch events, 1 for left mouse button
5006 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5007 this.pressed
.onSelect();
5010 this.pressed
= null;
5015 * Handle mouse over events.
5017 * @param {jQuery.Event} e Mouse over event
5019 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5020 var tool
= this.getTargetTool( e
);
5022 if ( this.pressed
&& this.pressed
=== tool
) {
5023 this.pressed
.setActive( true );
5028 * Handle mouse out events.
5030 * @param {jQuery.Event} e Mouse out event
5032 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5033 var tool
= this.getTargetTool( e
);
5035 if ( this.pressed
&& this.pressed
=== tool
) {
5036 this.pressed
.setActive( false );
5041 * Get the closest tool to a jQuery.Event.
5043 * Only tool links are considered, which prevents other elements in the tool such as popups from
5044 * triggering tool group interactions.
5047 * @param {jQuery.Event} e
5048 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5050 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5052 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5054 if ( $item
.length
) {
5055 tool
= $item
.parent().data( 'oo-ui-tool' );
5058 return tool
&& !tool
.isDisabled() ? tool
: null;
5062 * Handle tool registry register events.
5064 * If a tool is registered after the group is created, we must repopulate the list to account for:
5066 * - a tool being added that may be included
5067 * - a tool already included being overridden
5069 * @param {string} name Symbolic name of tool
5071 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5076 * Get the toolbar this group is in.
5078 * @return {OO.ui.Toolbar} Toolbar of group
5080 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5081 return this.toolbar
;
5085 * Add and remove tools based on configuration.
5087 OO
.ui
.ToolGroup
.prototype.populate = function () {
5088 var i
, len
, name
, tool
,
5089 toolFactory
= this.toolbar
.getToolFactory(),
5093 list
= this.toolbar
.getToolFactory().getTools(
5094 this.include
, this.exclude
, this.promote
, this.demote
5097 // Build a list of needed tools
5098 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5102 toolFactory
.lookup( name
) &&
5103 // Tool is available or is already in this group
5104 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5106 tool
= this.tools
[name
];
5108 // Auto-initialize tools on first use
5109 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5112 this.toolbar
.reserveTool( tool
);
5117 // Remove tools that are no longer needed
5118 for ( name
in this.tools
) {
5119 if ( !names
[name
] ) {
5120 this.tools
[name
].destroy();
5121 this.toolbar
.releaseTool( this.tools
[name
] );
5122 remove
.push( this.tools
[name
] );
5123 delete this.tools
[name
];
5126 if ( remove
.length
) {
5127 this.removeItems( remove
);
5129 // Update emptiness state
5131 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5133 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5135 // Re-add tools (moving existing ones to new locations)
5136 this.addItems( add
);
5137 // Disabled state may depend on items
5138 this.updateDisabled();
5142 * Destroy tool group.
5144 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5148 this.toolbar
.getToolFactory().disconnect( this );
5149 for ( name
in this.tools
) {
5150 this.toolbar
.releaseTool( this.tools
[name
] );
5151 this.tools
[name
].disconnect( this ).destroy();
5152 delete this.tools
[name
];
5154 this.$element
.remove();
5158 * Dialog for showing a message.
5161 * - Registers two actions by default (safe and primary).
5162 * - Renders action widgets in the footer.
5165 * @extends OO.ui.Dialog
5168 * @param {Object} [config] Configuration options
5170 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5171 // Parent constructor
5172 OO
.ui
.MessageDialog
.super.call( this, config
);
5175 this.verticalActionLayout
= null;
5178 this.$element
.addClass( 'oo-ui-messageDialog' );
5183 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5185 /* Static Properties */
5187 OO
.ui
.MessageDialog
.static.name
= 'message';
5189 OO
.ui
.MessageDialog
.static.size
= 'small';
5191 OO
.ui
.MessageDialog
.static.verbose
= false;
5196 * A confirmation dialog's title should describe what the progressive action will do. An alert
5197 * dialog's title should describe what event occured.
5201 * @property {jQuery|string|Function|null}
5203 OO
.ui
.MessageDialog
.static.title
= null;
5206 * A confirmation dialog's message should describe the consequences of the progressive action. An
5207 * alert dialog's message should describe why the event occured.
5211 * @property {jQuery|string|Function|null}
5213 OO
.ui
.MessageDialog
.static.message
= null;
5215 OO
.ui
.MessageDialog
.static.actions
= [
5216 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5217 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5225 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5227 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5231 * Toggle action layout between vertical and horizontal.
5233 * @param {boolean} [value] Layout actions vertically, omit to toggle
5236 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5237 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5239 if ( value
!== this.verticalActionLayout
) {
5240 this.verticalActionLayout
= value
;
5242 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5243 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5252 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5254 return new OO
.ui
.Process( function () {
5255 this.close( { action
: action
} );
5258 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5264 * @param {Object} [data] Dialog opening data
5265 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5266 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5267 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5268 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5271 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5275 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5276 .next( function () {
5277 this.title
.setLabel(
5278 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5280 this.message
.setLabel(
5281 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5283 this.message
.$element
.toggleClass(
5284 'oo-ui-messageDialog-message-verbose',
5285 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5293 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5294 return Math
.round( this.text
.$element
.outerHeight( true ) );
5300 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5302 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5305 this.$actions
= this.$( '<div>' );
5306 this.container
= new OO
.ui
.PanelLayout( {
5307 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5309 this.text
= new OO
.ui
.PanelLayout( {
5310 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5312 this.message
= new OO
.ui
.LabelWidget( {
5313 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5317 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5318 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5319 this.container
.$element
.append( this.text
.$element
);
5320 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5321 this.$body
.append( this.container
.$element
);
5322 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5323 this.$foot
.append( this.$actions
);
5329 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5330 var i
, len
, other
, special
, others
;
5333 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5335 special
= this.actions
.getSpecial();
5336 others
= this.actions
.getOthers();
5337 if ( special
.safe
) {
5338 this.$actions
.append( special
.safe
.$element
);
5339 special
.safe
.toggleFramed( false );
5341 if ( others
.length
) {
5342 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5344 this.$actions
.append( other
.$element
);
5345 other
.toggleFramed( false );
5348 if ( special
.primary
) {
5349 this.$actions
.append( special
.primary
.$element
);
5350 special
.primary
.toggleFramed( false );
5354 if ( !this.isOpening() ) {
5355 this.manager
.updateWindowSize( this );
5357 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5361 * Fit action actions into columns or rows.
5363 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5365 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5367 actions
= this.actions
.get();
5370 this.toggleVerticalActionLayout( false );
5371 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5372 action
= actions
[i
];
5373 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5374 this.toggleVerticalActionLayout( true );
5381 * Navigation dialog window.
5384 * - Show and hide errors.
5385 * - Retry an action.
5388 * - Renders header with dialog title and one action widget on either side
5389 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5390 * which close the dialog).
5391 * - Displays any action widgets in the footer (none by default).
5392 * - Ability to dismiss errors.
5394 * Subclass responsibilities:
5395 * - Register a 'safe' action.
5396 * - Register a 'primary' action.
5397 * - Add content to the dialog.
5401 * @extends OO.ui.Dialog
5404 * @param {Object} [config] Configuration options
5406 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
5407 // Parent constructor
5408 OO
.ui
.ProcessDialog
.super.call( this, config
);
5411 this.$element
.addClass( 'oo-ui-processDialog' );
5416 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
5421 * Handle dismiss button click events.
5425 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
5430 * Handle retry button click events.
5432 * Hides errors and then tries again.
5434 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
5436 this.executeAction( this.currentAction
.getAction() );
5442 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
5443 if ( this.actions
.isSpecial( action
) ) {
5446 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5452 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
5454 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
5457 this.$navigation
= this.$( '<div>' );
5458 this.$location
= this.$( '<div>' );
5459 this.$safeActions
= this.$( '<div>' );
5460 this.$primaryActions
= this.$( '<div>' );
5461 this.$otherActions
= this.$( '<div>' );
5462 this.dismissButton
= new OO
.ui
.ButtonWidget( {
5464 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
5466 this.retryButton
= new OO
.ui
.ButtonWidget( {
5468 label
: OO
.ui
.msg( 'ooui-dialog-process-retry' )
5470 this.$errors
= this.$( '<div>' );
5471 this.$errorsTitle
= this.$( '<div>' );
5474 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
5475 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
5478 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
5480 .append( this.title
.$element
)
5481 .addClass( 'oo-ui-processDialog-location' );
5482 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
5483 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
5484 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
5486 .addClass( 'oo-ui-processDialog-errors-title' )
5487 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
5489 .addClass( 'oo-ui-processDialog-errors' )
5490 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
5492 .addClass( 'oo-ui-processDialog-content' )
5493 .append( this.$errors
);
5495 .addClass( 'oo-ui-processDialog-navigation' )
5496 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
5497 this.$head
.append( this.$navigation
);
5498 this.$foot
.append( this.$otherActions
);
5504 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
5505 var i
, len
, other
, special
, others
;
5508 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
5510 special
= this.actions
.getSpecial();
5511 others
= this.actions
.getOthers();
5512 if ( special
.primary
) {
5513 this.$primaryActions
.append( special
.primary
.$element
);
5514 special
.primary
.toggleFramed( true );
5516 if ( others
.length
) {
5517 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5519 this.$otherActions
.append( other
.$element
);
5520 other
.toggleFramed( true );
5523 if ( special
.safe
) {
5524 this.$safeActions
.append( special
.safe
.$element
);
5525 special
.safe
.toggleFramed( true );
5529 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5535 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
5536 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
5537 .fail( OO
.ui
.bind( this.showErrors
, this ) );
5541 * Fit label between actions.
5545 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
5546 var width
= Math
.max(
5547 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
5548 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
5550 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
5556 * Handle errors that occured durring accept or reject processes.
5558 * @param {OO.ui.Error[]} errors Errors to be handled
5560 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
5565 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
5566 if ( !errors
[i
].isRecoverable() ) {
5567 recoverable
= false;
5569 $item
= this.$( '<div>' )
5570 .addClass( 'oo-ui-processDialog-error' )
5571 .append( errors
[i
].getMessage() );
5572 items
.push( $item
[0] );
5574 this.$errorItems
= this.$( items
);
5575 if ( recoverable
) {
5576 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
5578 this.currentAction
.setDisabled( true );
5580 this.retryButton
.toggle( recoverable
);
5581 this.$errorsTitle
.after( this.$errorItems
);
5582 this.$errors
.show().scrollTop( 0 );
5588 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
5589 this.$errors
.hide();
5590 this.$errorItems
.remove();
5591 this.$errorItems
= null;
5595 * Layout containing a series of pages.
5598 * @extends OO.ui.Layout
5601 * @param {Object} [config] Configuration options
5602 * @cfg {boolean} [continuous=false] Show all pages, one after another
5603 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
5604 * @cfg {boolean} [outlined=false] Show an outline
5605 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
5607 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
5608 // Initialize configuration
5609 config
= config
|| {};
5611 // Parent constructor
5612 OO
.ui
.BookletLayout
.super.call( this, config
);
5615 this.currentPageName
= null;
5617 this.ignoreFocus
= false;
5618 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
5619 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
5620 this.outlineVisible
= false;
5621 this.outlined
= !!config
.outlined
;
5622 if ( this.outlined
) {
5623 this.editable
= !!config
.editable
;
5624 this.outlineControlsWidget
= null;
5625 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
5626 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
5627 this.gridLayout
= new OO
.ui
.GridLayout(
5628 [ this.outlinePanel
, this.stackLayout
],
5629 { $: this.$, widths
: [ 1, 2 ] }
5631 this.outlineVisible
= true;
5632 if ( this.editable
) {
5633 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
5634 this.outlineWidget
, { $: this.$ }
5640 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
5641 if ( this.outlined
) {
5642 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
5644 if ( this.autoFocus
) {
5645 // Event 'focus' does not bubble, but 'focusin' does
5646 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
5650 this.$element
.addClass( 'oo-ui-bookletLayout' );
5651 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
5652 if ( this.outlined
) {
5653 this.outlinePanel
.$element
5654 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
5655 .append( this.outlineWidget
.$element
);
5656 if ( this.editable
) {
5657 this.outlinePanel
.$element
5658 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
5659 .append( this.outlineControlsWidget
.$element
);
5661 this.$element
.append( this.gridLayout
.$element
);
5663 this.$element
.append( this.stackLayout
.$element
);
5669 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
5675 * @param {OO.ui.PageLayout} page Current page
5680 * @param {OO.ui.PageLayout[]} page Added pages
5681 * @param {number} index Index pages were added at
5686 * @param {OO.ui.PageLayout[]} pages Removed pages
5692 * Handle stack layout focus.
5694 * @param {jQuery.Event} e Focusin event
5696 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
5699 // Find the page that an element was focused within
5700 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
5701 for ( name
in this.pages
) {
5702 // Check for page match, exclude current page to find only page changes
5703 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
5704 this.setPage( name
);
5711 * Handle stack layout set events.
5713 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
5715 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
5716 var $input
, layout
= this;
5718 page
.scrollElementIntoView( { complete: function () {
5719 if ( layout
.autoFocus
) {
5720 // Set focus to the first input if nothing on the page is focused yet
5721 if ( !page
.$element
.find( ':focus' ).length
) {
5722 $input
= page
.$element
.find( ':input:first' );
5723 if ( $input
.length
) {
5733 * Handle outline widget select events.
5735 * @param {OO.ui.OptionWidget|null} item Selected item
5737 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
5739 this.setPage( item
.getData() );
5744 * Check if booklet has an outline.
5748 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
5749 return this.outlined
;
5753 * Check if booklet has editing controls.
5757 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
5758 return this.editable
;
5762 * Check if booklet has a visible outline.
5766 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
5767 return this.outlined
&& this.outlineVisible
;
5771 * Hide or show the outline.
5773 * @param {boolean} [show] Show outline, omit to invert current state
5776 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
5777 if ( this.outlined
) {
5778 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
5779 this.outlineVisible
= show
;
5780 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
5787 * Get the outline widget.
5789 * @param {OO.ui.PageLayout} page Page to be selected
5790 * @return {OO.ui.PageLayout|null} Closest page to another
5792 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
5793 var next
, prev
, level
,
5794 pages
= this.stackLayout
.getItems(),
5795 index
= $.inArray( page
, pages
);
5797 if ( index
!== -1 ) {
5798 next
= pages
[index
+ 1];
5799 prev
= pages
[index
- 1];
5800 // Prefer adjacent pages at the same level
5801 if ( this.outlined
) {
5802 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
5805 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
5811 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
5817 return prev
|| next
|| null;
5821 * Get the outline widget.
5823 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
5825 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
5826 return this.outlineWidget
;
5830 * Get the outline controls widget. If the outline is not editable, null is returned.
5832 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
5834 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
5835 return this.outlineControlsWidget
;
5839 * Get a page by name.
5841 * @param {string} name Symbolic name of page
5842 * @return {OO.ui.PageLayout|undefined} Page, if found
5844 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
5845 return this.pages
[name
];
5849 * Get the current page name.
5851 * @return {string|null} Current page name
5853 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
5854 return this.currentPageName
;
5858 * Add a page to the layout.
5860 * When pages are added with the same names as existing pages, the existing pages will be
5861 * automatically removed before the new pages are added.
5863 * @param {OO.ui.PageLayout[]} pages Pages to add
5864 * @param {number} index Index to insert pages after
5868 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
5869 var i
, len
, name
, page
, item
, currentIndex
,
5870 stackLayoutPages
= this.stackLayout
.getItems(),
5874 // Remove pages with same names
5875 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5877 name
= page
.getName();
5879 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
5880 // Correct the insertion index
5881 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
5882 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
5885 remove
.push( this.pages
[name
] );
5888 if ( remove
.length
) {
5889 this.removePages( remove
);
5893 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5895 name
= page
.getName();
5896 this.pages
[page
.getName()] = page
;
5897 if ( this.outlined
) {
5898 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
5899 page
.setOutlineItem( item
);
5904 if ( this.outlined
&& items
.length
) {
5905 this.outlineWidget
.addItems( items
, index
);
5906 this.updateOutlineWidget();
5908 this.stackLayout
.addItems( pages
, index
);
5909 this.emit( 'add', pages
, index
);
5915 * Remove a page from the layout.
5920 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
5921 var i
, len
, name
, page
,
5924 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5926 name
= page
.getName();
5927 delete this.pages
[name
];
5928 if ( this.outlined
) {
5929 items
.push( this.outlineWidget
.getItemFromData( name
) );
5930 page
.setOutlineItem( null );
5933 if ( this.outlined
&& items
.length
) {
5934 this.outlineWidget
.removeItems( items
);
5935 this.updateOutlineWidget();
5937 this.stackLayout
.removeItems( pages
);
5938 this.emit( 'remove', pages
);
5944 * Clear all pages from the layout.
5949 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
5951 pages
= this.stackLayout
.getItems();
5954 this.currentPageName
= null;
5955 if ( this.outlined
) {
5956 this.outlineWidget
.clearItems();
5957 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
5958 pages
[i
].setOutlineItem( null );
5961 this.stackLayout
.clearItems();
5963 this.emit( 'remove', pages
);
5969 * Set the current page by name.
5972 * @param {string} name Symbolic name of page
5974 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
5977 page
= this.pages
[name
];
5979 if ( name
!== this.currentPageName
) {
5980 if ( this.outlined
) {
5981 selectedItem
= this.outlineWidget
.getSelectedItem();
5982 if ( selectedItem
&& selectedItem
.getData() !== name
) {
5983 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
5987 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
5988 this.pages
[this.currentPageName
].setActive( false );
5989 // Blur anything focused if the next page doesn't have anything focusable - this
5990 // is not needed if the next page has something focusable because once it is focused
5991 // this blur happens automatically
5992 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
5993 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
5994 if ( $focused
.length
) {
5999 this.currentPageName
= name
;
6000 this.stackLayout
.setItem( page
);
6001 page
.setActive( true );
6002 this.emit( 'set', page
);
6008 * Call this after adding or removing items from the OutlineWidget.
6012 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
6013 // Auto-select first item when nothing is selected anymore
6014 if ( !this.outlineWidget
.getSelectedItem() ) {
6015 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
6022 * Layout made of a field and optional label.
6025 * @extends OO.ui.Layout
6026 * @mixins OO.ui.LabeledElement
6028 * Available label alignment modes include:
6029 * - left: Label is before the field and aligned away from it, best for when the user will be
6030 * scanning for a specific label in a form with many fields
6031 * - right: Label is before the field and aligned toward it, best for forms the user is very
6032 * familiar with and will tab through field checking quickly to verify which field they are in
6033 * - top: Label is before the field and above it, best for when the use will need to fill out all
6034 * fields from top to bottom in a form with few fields
6035 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6036 * checkboxes or radio buttons
6039 * @param {OO.ui.Widget} field Field widget
6040 * @param {Object} [config] Configuration options
6041 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6042 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6044 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
6045 var popupButtonWidget
;
6046 // Config initialization
6047 config
= $.extend( { align
: 'left' }, config
);
6049 // Parent constructor
6050 OO
.ui
.FieldLayout
.super.call( this, config
);
6052 // Mixin constructors
6053 this.$help
= this.$( '<div>' );
6054 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
6055 if ( config
.help
) {
6056 popupButtonWidget
= new OO
.ui
.PopupButtonWidget( $.extend(
6066 popupButtonWidget
.getPopup().$body
.append( this.getElementDocument().createTextNode( config
.help
) );
6067 this.$help
= popupButtonWidget
.$element
;
6071 this.$field
= this.$( '<div>' );
6076 if ( this.field
instanceof OO
.ui
.InputWidget
) {
6077 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
6079 this.field
.connect( this, { disable
: 'onFieldDisable' } );
6082 this.$element
.addClass( 'oo-ui-fieldLayout' );
6084 .addClass( 'oo-ui-fieldLayout-field' )
6085 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
6086 .append( this.field
.$element
);
6087 this.setAlignment( config
.align
);
6092 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6093 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
6098 * Handle field disable events.
6100 * @param {boolean} value Field is disabled
6102 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6103 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6107 * Handle label mouse click events.
6109 * @param {jQuery.Event} e Mouse click event
6111 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6112 this.field
.simulateLabelClick();
6119 * @return {OO.ui.Widget} Field widget
6121 OO
.ui
.FieldLayout
.prototype.getField = function () {
6126 * Set the field alignment mode.
6128 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6131 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6132 if ( value
!== this.align
) {
6133 // Default to 'left'
6134 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6138 if ( value
=== 'inline' ) {
6139 this.$element
.append( this.$field
, this.$label
, this.$help
);
6141 this.$element
.append( this.$help
, this.$label
, this.$field
);
6145 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6148 // The following classes can be used here:
6149 // oo-ui-fieldLayout-align-left
6150 // oo-ui-fieldLayout-align-right
6151 // oo-ui-fieldLayout-align-top
6152 // oo-ui-fieldLayout-align-inline
6153 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
6160 * Layout made of a fieldset and optional legend.
6162 * Just add OO.ui.FieldLayout items.
6165 * @extends OO.ui.Layout
6166 * @mixins OO.ui.LabeledElement
6167 * @mixins OO.ui.IconedElement
6168 * @mixins OO.ui.GroupElement
6171 * @param {Object} [config] Configuration options
6172 * @cfg {string} [icon] Symbolic icon name
6173 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6175 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6176 // Config initialization
6177 config
= config
|| {};
6179 // Parent constructor
6180 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6182 // Mixin constructors
6183 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
6184 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
6185 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
6189 .addClass( 'oo-ui-fieldsetLayout' )
6190 .prepend( this.$icon
, this.$label
, this.$group
);
6191 if ( $.isArray( config
.items
) ) {
6192 this.addItems( config
.items
);
6198 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6199 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
6200 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
6201 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6203 /* Static Properties */
6205 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
6208 * Layout with an HTML form.
6211 * @extends OO.ui.Layout
6214 * @param {Object} [config] Configuration options
6216 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6217 // Configuration initialization
6218 config
= config
|| {};
6220 // Parent constructor
6221 OO
.ui
.FormLayout
.super.call( this, config
);
6224 this.$element
.on( 'submit', OO
.ui
.bind( this.onFormSubmit
, this ) );
6227 this.$element
.addClass( 'oo-ui-formLayout' );
6232 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6240 /* Static Properties */
6242 OO
.ui
.FormLayout
.static.tagName
= 'form';
6247 * Handle form submit events.
6249 * @param {jQuery.Event} e Submit event
6252 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6253 this.emit( 'submit' );
6258 * Layout made of proportionally sized columns and rows.
6261 * @extends OO.ui.Layout
6264 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6265 * @param {Object} [config] Configuration options
6266 * @cfg {number[]} [widths] Widths of columns as ratios
6267 * @cfg {number[]} [heights] Heights of columns as ratios
6269 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6272 // Config initialization
6273 config
= config
|| {};
6275 // Parent constructor
6276 OO
.ui
.GridLayout
.super.call( this, config
);
6284 this.$element
.addClass( 'oo-ui-gridLayout' );
6285 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6286 this.panels
.push( panels
[i
] );
6287 this.$element
.append( panels
[i
].$element
);
6289 if ( config
.widths
|| config
.heights
) {
6290 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6292 // Arrange in columns by default
6294 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
6297 this.layout( widths
, [ 1 ] );
6303 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6315 /* Static Properties */
6317 OO
.ui
.GridLayout
.static.tagName
= 'div';
6322 * Set grid dimensions.
6324 * @param {number[]} widths Widths of columns as ratios
6325 * @param {number[]} heights Heights of rows as ratios
6327 * @throws {Error} If grid is not large enough to fit all panels
6329 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6333 cols
= widths
.length
,
6334 rows
= heights
.length
;
6336 // Verify grid is big enough to fit panels
6337 if ( cols
* rows
< this.panels
.length
) {
6338 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6341 // Sum up denominators
6342 for ( x
= 0; x
< cols
; x
++ ) {
6345 for ( y
= 0; y
< rows
; y
++ ) {
6351 for ( x
= 0; x
< cols
; x
++ ) {
6352 this.widths
[x
] = widths
[x
] / xd
;
6354 for ( y
= 0; y
< rows
; y
++ ) {
6355 this.heights
[y
] = heights
[y
] / yd
;
6359 this.emit( 'layout' );
6363 * Update panel positions and sizes.
6367 OO
.ui
.GridLayout
.prototype.update = function () {
6375 cols
= this.widths
.length
,
6376 rows
= this.heights
.length
;
6378 for ( y
= 0; y
< rows
; y
++ ) {
6379 height
= this.heights
[y
];
6380 for ( x
= 0; x
< cols
; x
++ ) {
6381 panel
= this.panels
[i
];
6382 width
= this.widths
[x
];
6384 width
: Math
.round( width
* 100 ) + '%',
6385 height
: Math
.round( height
* 100 ) + '%',
6386 top
: Math
.round( top
* 100 ) + '%',
6387 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6388 visibility
: width
=== 0 || height
=== 0 ? 'hidden' : ''
6391 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6392 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6394 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6396 panel
.$element
.css( dimensions
);
6404 this.emit( 'update' );
6408 * Get a panel at a given position.
6410 * The x and y position is affected by the current grid layout.
6412 * @param {number} x Horizontal position
6413 * @param {number} y Vertical position
6414 * @return {OO.ui.PanelLayout} The panel at the given postion
6416 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
6417 return this.panels
[( x
* this.widths
.length
) + y
];
6421 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
6424 * @extends OO.ui.Layout
6427 * @param {Object} [config] Configuration options
6428 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
6429 * @cfg {boolean} [padded=false] Pad the content from the edges
6430 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
6432 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
6433 // Config initialization
6434 config
= config
|| {};
6436 // Parent constructor
6437 OO
.ui
.PanelLayout
.super.call( this, config
);
6440 this.$element
.addClass( 'oo-ui-panelLayout' );
6441 if ( config
.scrollable
) {
6442 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
6445 if ( config
.padded
) {
6446 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
6449 if ( config
.expanded
=== undefined || config
.expanded
) {
6450 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
6456 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
6459 * Page within an booklet layout.
6462 * @extends OO.ui.PanelLayout
6465 * @param {string} name Unique symbolic name of page
6466 * @param {Object} [config] Configuration options
6467 * @param {string} [outlineItem] Outline item widget
6469 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
6470 // Configuration initialization
6471 config
= $.extend( { scrollable
: true }, config
);
6473 // Parent constructor
6474 OO
.ui
.PageLayout
.super.call( this, config
);
6478 this.outlineItem
= config
.outlineItem
|| null;
6479 this.active
= false;
6482 this.$element
.addClass( 'oo-ui-pageLayout' );
6487 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
6493 * @param {boolean} active Page is active
6501 * @return {string} Symbolic name of page
6503 OO
.ui
.PageLayout
.prototype.getName = function () {
6508 * Check if page is active.
6510 * @return {boolean} Page is active
6512 OO
.ui
.PageLayout
.prototype.isActive = function () {
6519 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
6521 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
6522 return this.outlineItem
;
6528 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
6529 * outline item as desired; this method is called for setting (with an object) and unsetting
6530 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
6531 * operating on null instead of an OO.ui.OutlineItemWidget object.
6533 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
6536 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
6537 this.outlineItem
= outlineItem
|| null;
6538 if ( outlineItem
) {
6539 this.setupOutlineItem();
6545 * Setup outline item.
6547 * @localdoc Subclasses should override this method to adjust the outline item as desired.
6549 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
6552 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
6557 * Set page active state.
6559 * @param {boolean} Page is active
6562 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
6565 if ( active
!== this.active
) {
6566 this.active
= active
;
6567 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
6568 this.emit( 'active', this.active
);
6573 * Layout containing a series of mutually exclusive pages.
6576 * @extends OO.ui.PanelLayout
6577 * @mixins OO.ui.GroupElement
6580 * @param {Object} [config] Configuration options
6581 * @cfg {boolean} [continuous=false] Show all pages, one after another
6582 * @cfg {string} [icon=''] Symbolic icon name
6583 * @cfg {OO.ui.Layout[]} [items] Layouts to add
6585 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
6586 // Config initialization
6587 config
= $.extend( { scrollable
: true }, config
);
6589 // Parent constructor
6590 OO
.ui
.StackLayout
.super.call( this, config
);
6592 // Mixin constructors
6593 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
6596 this.currentItem
= null;
6597 this.continuous
= !!config
.continuous
;
6600 this.$element
.addClass( 'oo-ui-stackLayout' );
6601 if ( this.continuous
) {
6602 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
6604 if ( $.isArray( config
.items
) ) {
6605 this.addItems( config
.items
);
6611 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
6612 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
6618 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
6624 * Get the current item.
6626 * @return {OO.ui.Layout|null}
6628 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
6629 return this.currentItem
;
6633 * Unset the current item.
6636 * @param {OO.ui.StackLayout} layout
6639 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
6640 var prevItem
= this.currentItem
;
6641 if ( prevItem
=== null ) {
6645 this.currentItem
= null;
6646 this.emit( 'set', null );
6652 * Adding an existing item (by value) will move it.
6654 * @param {OO.ui.Layout[]} items Items to add
6655 * @param {number} [index] Index to insert items after
6658 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
6660 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
6662 if ( !this.currentItem
&& items
.length
) {
6663 this.setItem( items
[0] );
6672 * Items will be detached, not removed, so they can be used later.
6674 * @param {OO.ui.Layout[]} items Items to remove
6678 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
6680 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
6682 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
6683 if ( this.items
.length
) {
6684 this.setItem( this.items
[0] );
6686 this.unsetCurrentItem();
6696 * Items will be detached, not removed, so they can be used later.
6701 OO
.ui
.StackLayout
.prototype.clearItems = function () {
6702 this.unsetCurrentItem();
6703 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
6711 * Any currently shown item will be hidden.
6713 * FIXME: If the passed item to show has not been added in the items list, then
6714 * this method drops it and unsets the current item.
6716 * @param {OO.ui.Layout} item Item to show
6720 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
6723 if ( item
!== this.currentItem
) {
6724 if ( !this.continuous
) {
6725 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6726 this.items
[i
].$element
.css( 'display', '' );
6729 if ( $.inArray( item
, this.items
) !== -1 ) {
6730 if ( !this.continuous
) {
6731 item
.$element
.css( 'display', 'block' );
6733 this.currentItem
= item
;
6734 this.emit( 'set', item
);
6736 this.unsetCurrentItem();
6744 * Horizontal bar layout of tools as icon buttons.
6747 * @extends OO.ui.ToolGroup
6750 * @param {OO.ui.Toolbar} toolbar
6751 * @param {Object} [config] Configuration options
6753 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
6754 // Parent constructor
6755 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
6758 this.$element
.addClass( 'oo-ui-barToolGroup' );
6763 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
6765 /* Static Properties */
6767 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
6769 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
6771 OO
.ui
.BarToolGroup
.static.name
= 'bar';
6774 * Popup list of tools with an icon and optional label.
6778 * @extends OO.ui.ToolGroup
6779 * @mixins OO.ui.IconedElement
6780 * @mixins OO.ui.IndicatedElement
6781 * @mixins OO.ui.LabeledElement
6782 * @mixins OO.ui.TitledElement
6783 * @mixins OO.ui.ClippableElement
6786 * @param {OO.ui.Toolbar} toolbar
6787 * @param {Object} [config] Configuration options
6788 * @cfg {string} [header] Text to display at the top of the pop-up
6790 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
6791 // Configuration initialization
6792 config
= config
|| {};
6794 // Parent constructor
6795 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
6797 // Mixin constructors
6798 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6799 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6800 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6801 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
6802 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
6805 this.active
= false;
6806 this.dragging
= false;
6807 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
6808 this.$handle
= this.$( '<span>' );
6812 'mousedown touchstart': OO
.ui
.bind( this.onHandlePointerDown
, this ),
6813 'mouseup touchend': OO
.ui
.bind( this.onHandlePointerUp
, this )
6818 .addClass( 'oo-ui-popupToolGroup-handle' )
6819 .append( this.$icon
, this.$label
, this.$indicator
);
6820 // If the pop-up should have a header, add it to the top of the toolGroup.
6821 // Note: If this feature is useful for other widgets, we could abstract it into an
6822 // OO.ui.HeaderedElement mixin constructor.
6823 if ( config
.header
!== undefined ) {
6825 .prepend( this.$( '<span>' )
6826 .addClass( 'oo-ui-popupToolGroup-header' )
6827 .text( config
.header
)
6831 .addClass( 'oo-ui-popupToolGroup' )
6832 .prepend( this.$handle
);
6837 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
6838 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
6839 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
6840 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
6841 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
6842 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
6844 /* Static Properties */
6851 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
6853 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
6855 if ( this.isDisabled() && this.isElementAttached() ) {
6856 this.setActive( false );
6861 * Handle focus being lost.
6863 * The event is actually generated from a mouseup, so it is not a normal blur event object.
6865 * @param {jQuery.Event} e Mouse up event
6867 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
6868 // Only deactivate when clicking outside the dropdown element
6869 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
6870 this.setActive( false );
6877 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
6878 // e.which is 0 for touch events, 1 for left mouse button
6879 if ( !this.isDisabled() && e
.which
<= 1 ) {
6880 this.setActive( false );
6882 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
6886 * Handle mouse up events.
6888 * @param {jQuery.Event} e Mouse up event
6890 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
6895 * Handle mouse down events.
6897 * @param {jQuery.Event} e Mouse down event
6899 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
6900 // e.which is 0 for touch events, 1 for left mouse button
6901 if ( !this.isDisabled() && e
.which
<= 1 ) {
6902 this.setActive( !this.active
);
6908 * Switch into active mode.
6910 * When active, mouseup events anywhere in the document will trigger deactivation.
6912 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
6914 if ( this.active
!== value
) {
6915 this.active
= value
;
6917 this.setClipping( true );
6918 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
6919 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
6921 this.setClipping( false );
6922 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
6923 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
6929 * Drop down list layout of tools as labeled icon buttons.
6932 * @extends OO.ui.PopupToolGroup
6935 * @param {OO.ui.Toolbar} toolbar
6936 * @param {Object} [config] Configuration options
6938 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
6939 // Parent constructor
6940 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
6943 this.$element
.addClass( 'oo-ui-listToolGroup' );
6948 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
6950 /* Static Properties */
6952 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
6954 OO
.ui
.ListToolGroup
.static.name
= 'list';
6957 * Drop down menu layout of tools as selectable menu items.
6960 * @extends OO.ui.PopupToolGroup
6963 * @param {OO.ui.Toolbar} toolbar
6964 * @param {Object} [config] Configuration options
6966 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
6967 // Configuration initialization
6968 config
= config
|| {};
6970 // Parent constructor
6971 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
6974 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
6977 this.$element
.addClass( 'oo-ui-menuToolGroup' );
6982 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
6984 /* Static Properties */
6986 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
6988 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
6993 * Handle the toolbar state being updated.
6995 * When the state changes, the title of each active item in the menu will be joined together and
6996 * used as a label for the group. The label will be empty if none of the items are active.
6998 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7002 for ( name
in this.tools
) {
7003 if ( this.tools
[name
].isActive() ) {
7004 labelTexts
.push( this.tools
[name
].getTitle() );
7008 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7012 * Tool that shows a popup when selected.
7016 * @extends OO.ui.Tool
7017 * @mixins OO.ui.PopuppableElement
7020 * @param {OO.ui.Toolbar} toolbar
7021 * @param {Object} [config] Configuration options
7023 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7024 // Parent constructor
7025 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7027 // Mixin constructors
7028 OO
.ui
.PopuppableElement
.call( this, config
);
7032 .addClass( 'oo-ui-popupTool' )
7033 .append( this.popup
.$element
);
7038 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7039 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
7044 * Handle the tool being selected.
7048 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7049 if ( !this.isDisabled() ) {
7050 this.popup
.toggle();
7052 this.setActive( false );
7057 * Handle the toolbar state being updated.
7061 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7062 this.setActive( false );
7066 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7068 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7072 * @extends OO.ui.GroupElement
7075 * @param {jQuery} $group Container node, assigned to #$group
7076 * @param {Object} [config] Configuration options
7078 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
7079 // Parent constructor
7080 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
7085 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7090 * Set the disabled state of the widget.
7092 * This will also update the disabled state of child widgets.
7094 * @param {boolean} disabled Disable widget
7097 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7101 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7102 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7104 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7106 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7107 this.items
[i
].updateDisabled();
7115 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7117 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7118 * allows bidrectional communication.
7120 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7127 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7134 * Check if widget is disabled.
7136 * Checks parent if present, making disabled state inheritable.
7138 * @return {boolean} Widget is disabled
7140 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7141 return this.disabled
||
7142 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7146 * Set group element is in.
7148 * @param {OO.ui.GroupElement|null} group Group element, null if none
7151 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7153 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7154 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7156 // Initialize item disabled states
7157 this.updateDisabled();
7163 * Mixin that adds a menu showing suggested values for a text input.
7165 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7171 * @param {OO.ui.TextInputWidget} input Input widget
7172 * @param {Object} [config] Configuration options
7173 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
7175 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7176 // Config intialization
7177 config
= config
|| {};
7180 this.lookupInput
= input
;
7181 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
7182 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7183 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7184 input
: this.lookupInput
,
7185 $container
: config
.$container
7187 this.lookupCache
= {};
7188 this.lookupQuery
= null;
7189 this.lookupRequest
= null;
7190 this.populating
= false;
7193 this.$overlay
.append( this.lookupMenu
.$element
);
7195 this.lookupInput
.$input
.on( {
7196 focus
: OO
.ui
.bind( this.onLookupInputFocus
, this ),
7197 blur
: OO
.ui
.bind( this.onLookupInputBlur
, this ),
7198 mousedown
: OO
.ui
.bind( this.onLookupInputMouseDown
, this )
7200 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7203 this.$element
.addClass( 'oo-ui-lookupWidget' );
7204 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7210 * Handle input focus event.
7212 * @param {jQuery.Event} e Input focus event
7214 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7215 this.openLookupMenu();
7219 * Handle input blur event.
7221 * @param {jQuery.Event} e Input blur event
7223 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7224 this.lookupMenu
.toggle( false );
7228 * Handle input mouse down event.
7230 * @param {jQuery.Event} e Input mouse down event
7232 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7233 this.openLookupMenu();
7237 * Handle input change event.
7239 * @param {string} value New input value
7241 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7242 this.openLookupMenu();
7248 * @return {OO.ui.TextInputMenuWidget}
7250 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
7251 return this.lookupMenu
;
7259 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
7260 var value
= this.lookupInput
.getValue();
7262 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
7263 this.populateLookupMenu();
7264 this.lookupMenu
.toggle( true );
7275 * Populate lookup menu with current information.
7279 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
7282 if ( !this.populating
) {
7283 this.populating
= true;
7284 this.getLookupMenuItems()
7285 .done( function ( items
) {
7286 widget
.lookupMenu
.clearItems();
7287 if ( items
.length
) {
7291 widget
.initializeLookupMenuSelection();
7292 widget
.openLookupMenu();
7294 widget
.lookupMenu
.toggle( true );
7296 widget
.populating
= false;
7298 .fail( function () {
7299 widget
.lookupMenu
.clearItems();
7300 widget
.populating
= false;
7308 * Set selection in the lookup menu with current information.
7312 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
7313 if ( !this.lookupMenu
.getSelectedItem() ) {
7314 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
7316 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
7320 * Get lookup menu items for the current query.
7322 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
7325 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
7327 value
= this.lookupInput
.getValue(),
7328 deferred
= $.Deferred();
7330 if ( value
&& value
!== this.lookupQuery
) {
7331 // Abort current request if query has changed
7332 if ( this.lookupRequest
) {
7333 this.lookupRequest
.abort();
7334 this.lookupQuery
= null;
7335 this.lookupRequest
= null;
7337 if ( value
in this.lookupCache
) {
7338 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
7340 this.lookupQuery
= value
;
7341 this.lookupRequest
= this.getLookupRequest()
7342 .always( function () {
7343 widget
.lookupQuery
= null;
7344 widget
.lookupRequest
= null;
7346 .done( function ( data
) {
7347 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
7348 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
7350 .fail( function () {
7354 this.lookupRequest
.always( function () {
7355 widget
.popPending();
7359 return deferred
.promise();
7363 * Get a new request object of the current lookup query value.
7366 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
7368 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
7369 // Stub, implemented in subclass
7374 * Handle successful lookup request.
7376 * Overriding methods should call #populateLookupMenu when results are available and cache results
7377 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
7380 * @param {Mixed} data Response from server
7382 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
7383 // Stub, implemented in subclass
7387 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
7390 * @param {Mixed} data Cached result data, usually an array
7391 * @return {OO.ui.MenuItemWidget[]} Menu items
7393 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
7394 // Stub, implemented in subclass
7399 * Set of controls for an OO.ui.OutlineWidget.
7401 * Controls include moving items up and down, removing items, and adding different kinds of items.
7404 * @extends OO.ui.Widget
7405 * @mixins OO.ui.GroupElement
7406 * @mixins OO.ui.IconedElement
7409 * @param {OO.ui.OutlineWidget} outline Outline to control
7410 * @param {Object} [config] Configuration options
7412 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7413 // Configuration initialization
7414 config
= $.extend( { icon
: 'add-item' }, config
);
7416 // Parent constructor
7417 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7419 // Mixin constructors
7420 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
7421 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
7424 this.outline
= outline
;
7425 this.$movers
= this.$( '<div>' );
7426 this.upButton
= new OO
.ui
.ButtonWidget( {
7430 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
7432 this.downButton
= new OO
.ui
.ButtonWidget( {
7436 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
7438 this.removeButton
= new OO
.ui
.ButtonWidget( {
7442 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
7446 outline
.connect( this, {
7447 select
: 'onOutlineChange',
7448 add
: 'onOutlineChange',
7449 remove
: 'onOutlineChange'
7451 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
7452 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
7453 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
7456 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7457 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7459 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7460 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7461 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7466 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7467 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7468 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7474 * @param {number} places Number of places to move
7484 * Handle outline change events.
7486 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7487 var i
, len
, firstMovable
, lastMovable
,
7488 items
= this.outline
.getItems(),
7489 selectedItem
= this.outline
.getSelectedItem(),
7490 movable
= selectedItem
&& selectedItem
.isMovable(),
7491 removable
= selectedItem
&& selectedItem
.isRemovable();
7496 while ( ++i
< len
) {
7497 if ( items
[i
].isMovable() ) {
7498 firstMovable
= items
[i
];
7504 if ( items
[i
].isMovable() ) {
7505 lastMovable
= items
[i
];
7510 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7511 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7512 this.removeButton
.setDisabled( !removable
);
7516 * Mixin for widgets with a boolean on/off state.
7522 * @param {Object} [config] Configuration options
7523 * @cfg {boolean} [value=false] Initial value
7525 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7526 // Configuration initialization
7527 config
= config
|| {};
7533 this.$element
.addClass( 'oo-ui-toggleWidget' );
7534 this.setValue( !!config
.value
);
7541 * @param {boolean} value Changed value
7547 * Get the value of the toggle.
7551 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7556 * Set the value of the toggle.
7558 * @param {boolean} value New value
7562 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7564 if ( this.value
!== value
) {
7566 this.emit( 'change', value
);
7567 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7568 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7574 * Group widget for multiple related buttons.
7576 * Use together with OO.ui.ButtonWidget.
7579 * @extends OO.ui.Widget
7580 * @mixins OO.ui.GroupElement
7583 * @param {Object} [config] Configuration options
7584 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
7586 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
7587 // Parent constructor
7588 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
7590 // Mixin constructors
7591 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
7594 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
7595 if ( $.isArray( config
.items
) ) {
7596 this.addItems( config
.items
);
7602 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
7603 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
7606 * Generic widget for buttons.
7609 * @extends OO.ui.Widget
7610 * @mixins OO.ui.ButtonedElement
7611 * @mixins OO.ui.IconedElement
7612 * @mixins OO.ui.IndicatedElement
7613 * @mixins OO.ui.LabeledElement
7614 * @mixins OO.ui.TitledElement
7615 * @mixins OO.ui.FlaggableElement
7618 * @param {Object} [config] Configuration options
7619 * @cfg {string} [href] Hyperlink to visit when clicked
7620 * @cfg {string} [target] Target to open hyperlink in
7622 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
7623 // Configuration initialization
7624 config
= $.extend( { target
: '_blank' }, config
);
7626 // Parent constructor
7627 OO
.ui
.ButtonWidget
.super.call( this, config
);
7629 // Mixin constructors
7630 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7631 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
7632 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
7633 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
7634 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
7635 OO
.ui
.FlaggableElement
.call( this, config
);
7640 this.isHyperlink
= false;
7644 click
: OO
.ui
.bind( this.onClick
, this ),
7645 keypress
: OO
.ui
.bind( this.onKeyPress
, this )
7649 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
7651 .addClass( 'oo-ui-buttonWidget' )
7652 .append( this.$button
);
7653 this.setHref( config
.href
);
7654 this.setTarget( config
.target
);
7659 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
7660 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
7661 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
7662 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
7663 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
7664 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
7665 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
7676 * Handles mouse click events.
7678 * @param {jQuery.Event} e Mouse click event
7681 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
7682 if ( !this.isDisabled() ) {
7683 this.emit( 'click' );
7684 if ( this.isHyperlink
) {
7692 * Handles keypress events.
7694 * @param {jQuery.Event} e Keypress event
7697 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
7698 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
7700 if ( this.isHyperlink
) {
7708 * Get hyperlink location.
7710 * @return {string} Hyperlink location
7712 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
7717 * Get hyperlink target.
7719 * @return {string} Hyperlink target
7721 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
7726 * Set hyperlink location.
7728 * @param {string|null} href Hyperlink location, null to remove
7730 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
7731 href
= typeof href
=== 'string' ? href
: null;
7733 if ( href
!== this.href
) {
7735 if ( href
!== null ) {
7736 this.$button
.attr( 'href', href
);
7737 this.isHyperlink
= true;
7739 this.$button
.removeAttr( 'href' );
7740 this.isHyperlink
= false;
7748 * Set hyperlink target.
7750 * @param {string|null} target Hyperlink target, null to remove
7752 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
7753 target
= typeof target
=== 'string' ? target
: null;
7755 if ( target
!== this.target
) {
7756 this.target
= target
;
7757 if ( target
!== null ) {
7758 this.$button
.attr( 'target', target
);
7760 this.$button
.removeAttr( 'target' );
7768 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
7771 * @extends OO.ui.ButtonWidget
7774 * @param {Object} [config] Configuration options
7775 * @cfg {string} [action] Symbolic action name
7776 * @cfg {string[]} [modes] Symbolic mode names
7778 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
7779 // Config intialization
7780 config
= $.extend( { framed
: false }, config
);
7782 // Parent constructor
7783 OO
.ui
.ActionWidget
.super.call( this, config
);
7786 this.action
= config
.action
|| '';
7787 this.modes
= config
.modes
|| [];
7792 this.$element
.addClass( 'oo-ui-actionWidget' );
7797 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
7808 * Check if action is available in a certain mode.
7810 * @param {string} mode Name of mode
7811 * @return {boolean} Has mode
7813 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
7814 return this.modes
.indexOf( mode
) !== -1;
7818 * Get symbolic action name.
7822 OO
.ui
.ActionWidget
.prototype.getAction = function () {
7827 * Get symbolic action name.
7831 OO
.ui
.ActionWidget
.prototype.getModes = function () {
7832 return this.modes
.slice();
7836 * Emit a resize event if the size has changed.
7840 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
7843 if ( this.isElementAttached() ) {
7844 width
= this.$element
.width();
7845 height
= this.$element
.height();
7847 if ( width
!== this.width
|| height
!== this.height
) {
7849 this.height
= height
;
7850 this.emit( 'resize' );
7860 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
7862 OO
.ui
.IconedElement
.prototype.setIcon
.apply( this, arguments
);
7863 this.propagateResize();
7871 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
7873 OO
.ui
.LabeledElement
.prototype.setLabel
.apply( this, arguments
);
7874 this.propagateResize();
7882 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
7884 OO
.ui
.FlaggableElement
.prototype.setFlags
.apply( this, arguments
);
7885 this.propagateResize();
7893 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
7895 OO
.ui
.FlaggableElement
.prototype.clearFlags
.apply( this, arguments
);
7896 this.propagateResize();
7902 * Toggle visibility of button.
7904 * @param {boolean} [show] Show button, omit to toggle visibility
7907 OO
.ui
.ActionWidget
.prototype.toggle = function () {
7909 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
7910 this.propagateResize();
7916 * Button that shows and hides a popup.
7919 * @extends OO.ui.ButtonWidget
7920 * @mixins OO.ui.PopuppableElement
7923 * @param {Object} [config] Configuration options
7925 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7926 // Parent constructor
7927 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7929 // Mixin constructors
7930 OO
.ui
.PopuppableElement
.call( this, config
);
7934 .addClass( 'oo-ui-popupButtonWidget' )
7935 .append( this.popup
.$element
);
7940 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
7941 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
7946 * Handles mouse click events.
7948 * @param {jQuery.Event} e Mouse click event
7950 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
7951 // Skip clicks within the popup
7952 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
7956 if ( !this.isDisabled() ) {
7957 this.popup
.toggle();
7959 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
7965 * Button that toggles on and off.
7968 * @extends OO.ui.ButtonWidget
7969 * @mixins OO.ui.ToggleWidget
7972 * @param {Object} [config] Configuration options
7973 * @cfg {boolean} [value=false] Initial value
7975 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
7976 // Configuration initialization
7977 config
= config
|| {};
7979 // Parent constructor
7980 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
7982 // Mixin constructors
7983 OO
.ui
.ToggleWidget
.call( this, config
);
7986 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
7991 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
7992 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
7999 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8000 if ( !this.isDisabled() ) {
8001 this.setValue( !this.value
);
8005 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8011 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8013 if ( value
!== this.value
) {
8014 this.setActive( value
);
8017 // Parent method (from mixin)
8018 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8027 * @extends OO.ui.Widget
8028 * @mixins OO.ui.IconedElement
8029 * @mixins OO.ui.TitledElement
8032 * @param {Object} [config] Configuration options
8034 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8035 // Config intialization
8036 config
= config
|| {};
8038 // Parent constructor
8039 OO
.ui
.IconWidget
.super.call( this, config
);
8041 // Mixin constructors
8042 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
8043 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
8046 this.$element
.addClass( 'oo-ui-iconWidget' );
8051 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8052 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
8053 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8055 /* Static Properties */
8057 OO
.ui
.IconWidget
.static.tagName
= 'span';
8062 * See OO.ui.IndicatedElement for more information.
8065 * @extends OO.ui.Widget
8066 * @mixins OO.ui.IndicatedElement
8067 * @mixins OO.ui.TitledElement
8070 * @param {Object} [config] Configuration options
8072 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8073 // Config intialization
8074 config
= config
|| {};
8076 // Parent constructor
8077 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8079 // Mixin constructors
8080 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
8081 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
8084 this.$element
.addClass( 'oo-ui-indicatorWidget' );
8089 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
8090 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
8091 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
8093 /* Static Properties */
8095 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
8098 * Inline menu of options.
8100 * Inline menus provide a control for accessing a menu and compose a menu within the widget, which
8101 * can be accessed using the #getMenu method.
8103 * Use with OO.ui.MenuOptionWidget.
8106 * @extends OO.ui.Widget
8107 * @mixins OO.ui.IconedElement
8108 * @mixins OO.ui.IndicatedElement
8109 * @mixins OO.ui.LabeledElement
8110 * @mixins OO.ui.TitledElement
8113 * @param {Object} [config] Configuration options
8114 * @cfg {Object} [menu] Configuration options to pass to menu widget
8116 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
8117 // Configuration initialization
8118 config
= $.extend( { indicator
: 'down' }, config
);
8120 // Parent constructor
8121 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
8123 // Mixin constructors
8124 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
8125 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
8126 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
8127 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
8130 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8131 this.$handle
= this.$( '<span>' );
8134 this.$element
.on( { click
: OO
.ui
.bind( this.onClick
, this ) } );
8135 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8139 .addClass( 'oo-ui-inlineMenuWidget-handle' )
8140 .append( this.$icon
, this.$label
, this.$indicator
);
8142 .addClass( 'oo-ui-inlineMenuWidget' )
8143 .append( this.$handle
, this.menu
.$element
);
8148 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
8149 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
8150 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
8151 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
8152 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
8159 * @return {OO.ui.MenuWidget} Menu of widget
8161 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
8166 * Handles menu select events.
8168 * @param {OO.ui.MenuItemWidget} item Selected menu item
8170 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
8177 selectedLabel
= item
.getLabel();
8179 // If the label is a DOM element, clone it, because setLabel will append() it
8180 if ( selectedLabel
instanceof jQuery
) {
8181 selectedLabel
= selectedLabel
.clone();
8184 this.setLabel( selectedLabel
);
8188 * Handles mouse click events.
8190 * @param {jQuery.Event} e Mouse click event
8192 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
8193 // Skip clicks within the menu
8194 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8198 if ( !this.isDisabled() ) {
8199 if ( this.menu
.isVisible() ) {
8200 this.menu
.toggle( false );
8202 this.menu
.toggle( true );
8209 * Base class for input widgets.
8213 * @extends OO.ui.Widget
8216 * @param {Object} [config] Configuration options
8217 * @cfg {string} [name=''] HTML input name
8218 * @cfg {string} [value=''] Input value
8219 * @cfg {boolean} [readOnly=false] Prevent changes
8220 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
8222 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8223 // Config intialization
8224 config
= $.extend( { readOnly
: false }, config
);
8226 // Parent constructor
8227 OO
.ui
.InputWidget
.super.call( this, config
);
8230 this.$input
= this.getInputElement( config
);
8232 this.readOnly
= false;
8233 this.inputFilter
= config
.inputFilter
;
8236 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
8240 .attr( 'name', config
.name
)
8241 .prop( 'disabled', this.isDisabled() );
8242 this.setReadOnly( config
.readOnly
);
8243 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
8244 this.setValue( config
.value
);
8249 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8261 * Get input element.
8263 * @param {Object} [config] Configuration options
8264 * @return {jQuery} Input element
8266 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8267 return this.$( '<input>' );
8271 * Handle potentially value-changing events.
8273 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8275 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8277 if ( !this.isDisabled() ) {
8278 // Allow the stack to clear so the value will be updated
8279 setTimeout( function () {
8280 widget
.setValue( widget
.$input
.val() );
8286 * Get the value of the input.
8288 * @return {string} Input value
8290 OO
.ui
.InputWidget
.prototype.getValue = function () {
8295 * Sets the direction of the current input, either RTL or LTR
8297 * @param {boolean} isRTL
8299 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
8301 this.$input
.removeClass( 'oo-ui-ltr' );
8302 this.$input
.addClass( 'oo-ui-rtl' );
8304 this.$input
.removeClass( 'oo-ui-rtl' );
8305 this.$input
.addClass( 'oo-ui-ltr' );
8310 * Set the value of the input.
8312 * @param {string} value New value
8316 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
8317 value
= this.sanitizeValue( value
);
8318 if ( this.value
!== value
) {
8320 this.emit( 'change', this.value
);
8322 // Update the DOM if it has changed. Note that with sanitizeValue, it
8323 // is possible for the DOM value to change without this.value changing.
8324 if ( this.$input
.val() !== this.value
) {
8325 this.$input
.val( this.value
);
8331 * Sanitize incoming value.
8333 * Ensures value is a string, and converts undefined and null to empty strings.
8335 * @param {string} value Original value
8336 * @return {string} Sanitized value
8338 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
8339 if ( value
=== undefined || value
=== null ) {
8341 } else if ( this.inputFilter
) {
8342 return this.inputFilter( String( value
) );
8344 return String( value
);
8349 * Simulate the behavior of clicking on a label bound to this input.
8351 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
8352 if ( !this.isDisabled() ) {
8353 if ( this.$input
.is( ':checkbox,:radio' ) ) {
8354 this.$input
.click();
8355 } else if ( this.$input
.is( ':input' ) ) {
8356 this.$input
[0].focus();
8362 * Check if the widget is read-only.
8366 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
8367 return this.readOnly
;
8371 * Set the read-only state of the widget.
8373 * This should probably change the widgets's appearance and prevent it from being used.
8375 * @param {boolean} state Make input read-only
8378 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
8379 this.readOnly
= !!state
;
8380 this.$input
.prop( 'readOnly', this.readOnly
);
8387 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
8388 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
8389 if ( this.$input
) {
8390 this.$input
.prop( 'disabled', this.isDisabled() );
8400 OO
.ui
.InputWidget
.prototype.focus = function () {
8401 this.$input
[0].focus();
8410 OO
.ui
.InputWidget
.prototype.blur = function () {
8411 this.$input
[0].blur();
8416 * Checkbox input widget.
8419 * @extends OO.ui.InputWidget
8422 * @param {Object} [config] Configuration options
8424 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
8425 // Parent constructor
8426 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
8429 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
8434 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
8441 * Get input element.
8443 * @return {jQuery} Input element
8445 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
8446 return this.$( '<input type="checkbox" />' );
8450 * Get checked state of the checkbox
8452 * @return {boolean} If the checkbox is checked
8454 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
8461 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
8463 if ( this.value
!== value
) {
8465 this.$input
.prop( 'checked', this.value
);
8466 this.emit( 'change', this.value
);
8473 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
8475 if ( !this.isDisabled() ) {
8476 // Allow the stack to clear so the value will be updated
8477 setTimeout( function () {
8478 widget
.setValue( widget
.$input
.prop( 'checked' ) );
8484 * Input widget with a text field.
8487 * @extends OO.ui.InputWidget
8488 * @mixins OO.ui.IconedElement
8489 * @mixins OO.ui.IndicatedElement
8492 * @param {Object} [config] Configuration options
8493 * @cfg {string} [placeholder] Placeholder text
8494 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8495 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8496 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8498 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8499 // Configuration initialization
8500 config
= config
|| {};
8502 // Parent constructor
8503 OO
.ui
.TextInputWidget
.super.call( this, config
);
8505 // Mixin constructors
8506 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
8507 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
8511 this.multiline
= !!config
.multiline
;
8512 this.autosize
= !!config
.autosize
;
8513 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
8516 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8517 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8518 this.$icon
.on( 'mousedown', OO
.ui
.bind( this.onIconMouseDown
, this ) );
8519 this.$indicator
.on( 'mousedown', OO
.ui
.bind( this.onIndicatorMouseDown
, this ) );
8523 .addClass( 'oo-ui-textInputWidget' )
8524 .append( this.$icon
, this.$indicator
);
8525 if ( config
.placeholder
) {
8526 this.$input
.attr( 'placeholder', config
.placeholder
);
8528 this.$element
.attr( 'role', 'textbox' );
8533 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8534 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconedElement
);
8535 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatedElement
);
8540 * User presses enter inside the text box.
8542 * Not called if input is multiline.
8548 * User clicks the icon.
8554 * User clicks the indicator.
8562 * Handle icon mouse down events.
8564 * @param {jQuery.Event} e Mouse down event
8567 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
8568 if ( e
.which
=== 1 ) {
8569 this.$input
[0].focus();
8570 this.emit( 'icon' );
8576 * Handle indicator mouse down events.
8578 * @param {jQuery.Event} e Mouse down event
8581 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
8582 if ( e
.which
=== 1 ) {
8583 this.$input
[0].focus();
8584 this.emit( 'indicator' );
8590 * Handle key press events.
8592 * @param {jQuery.Event} e Key press event
8593 * @fires enter If enter key is pressed and input is not multiline
8595 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8596 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8597 this.emit( 'enter' );
8602 * Handle element attach events.
8604 * @param {jQuery.Event} e Element attach event
8606 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8613 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8617 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
8623 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
8625 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
8632 * Automatically adjust the size of the text input.
8634 * This only affects multi-line inputs that are auto-sized.
8638 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
8639 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
8641 if ( this.multiline
&& this.autosize
) {
8642 $clone
= this.$input
.clone()
8643 .val( this.$input
.val() )
8644 .css( { height
: 0 } )
8645 .insertAfter( this.$input
);
8646 // Set inline height property to 0 to measure scroll height
8647 scrollHeight
= $clone
[0].scrollHeight
;
8648 // Remove inline height property to measure natural heights
8649 $clone
.css( 'height', '' );
8650 innerHeight
= $clone
.innerHeight();
8651 outerHeight
= $clone
.outerHeight();
8652 // Measure max rows height
8653 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
8654 maxInnerHeight
= $clone
.innerHeight();
8655 $clone
.removeAttr( 'rows' ).css( 'height', '' );
8657 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
8658 // Only apply inline height when expansion beyond natural height is needed
8661 // Use the difference between the inner and outer height as a buffer
8662 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
8669 * Get input element.
8671 * @param {Object} [config] Configuration options
8672 * @return {jQuery} Input element
8674 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
8675 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
8681 * Check if input supports multiple lines.
8685 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
8686 return !!this.multiline
;
8690 * Check if input automatically adjusts its size.
8694 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
8695 return !!this.autosize
;
8699 * Check if input is pending.
8703 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
8704 return !!this.pending
;
8708 * Increase the pending stack.
8712 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
8713 if ( this.pending
=== 0 ) {
8714 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
8715 this.$input
.addClass( 'oo-ui-texture-pending' );
8723 * Reduce the pending stack.
8729 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
8730 if ( this.pending
=== 1 ) {
8731 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
8732 this.$input
.removeClass( 'oo-ui-texture-pending' );
8734 this.pending
= Math
.max( 0, this.pending
- 1 );
8740 * Select the contents of the input.
8744 OO
.ui
.TextInputWidget
.prototype.select = function () {
8745 this.$input
.select();
8750 * Text input with a menu of optional values.
8753 * @extends OO.ui.Widget
8756 * @param {Object} [config] Configuration options
8757 * @cfg {Object} [menu] Configuration options to pass to menu widget
8758 * @cfg {Object} [input] Configuration options to pass to input widget
8760 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
8761 // Configuration initialization
8762 config
= config
|| {};
8764 // Parent constructor
8765 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
8768 this.input
= new OO
.ui
.TextInputWidget( $.extend(
8769 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
8772 this.menu
= new OO
.ui
.MenuWidget( $.extend(
8773 { $: this.$, widget
: this, input
: this.input
, disabled
: this.isDisabled() },
8778 this.input
.connect( this, {
8779 change
: 'onInputChange',
8780 indicator
: 'onInputIndicator',
8781 enter
: 'onInputEnter'
8783 this.menu
.connect( this, {
8784 choose
: 'onMenuChoose',
8785 add
: 'onMenuItemsChange',
8786 remove
: 'onMenuItemsChange'
8790 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append(
8791 this.input
.$element
,
8794 this.onMenuItemsChange();
8799 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
8804 * Handle input change events.
8806 * @param {string} value New value
8808 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
8809 var match
= this.menu
.getItemFromData( value
);
8811 this.menu
.selectItem( match
);
8813 if ( !this.isDisabled() ) {
8814 this.menu
.toggle( true );
8819 * Handle input indicator events.
8821 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
8822 if ( !this.isDisabled() ) {
8828 * Handle input enter events.
8830 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
8831 if ( !this.isDisabled() ) {
8832 this.menu
.toggle( false );
8837 * Handle menu choose events.
8839 * @param {OO.ui.OptionWidget} item Chosen item
8841 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
8843 this.input
.setValue( item
.getData() );
8848 * Handle menu item change events.
8850 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
8851 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
8857 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
8859 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
8862 this.input
.setDisabled( this.isDisabled() );
8865 this.menu
.setDisabled( this.isDisabled() );
8875 * @extends OO.ui.Widget
8876 * @mixins OO.ui.LabeledElement
8879 * @param {Object} [config] Configuration options
8881 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
8882 // Config intialization
8883 config
= config
|| {};
8885 // Parent constructor
8886 OO
.ui
.LabelWidget
.super.call( this, config
);
8888 // Mixin constructors
8889 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
8892 this.input
= config
.input
;
8895 if ( this.input
instanceof OO
.ui
.InputWidget
) {
8896 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8900 this.$element
.addClass( 'oo-ui-labelWidget' );
8905 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
8906 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
8908 /* Static Properties */
8910 OO
.ui
.LabelWidget
.static.tagName
= 'label';
8915 * Handles label mouse click events.
8917 * @param {jQuery.Event} e Mouse click event
8919 OO
.ui
.LabelWidget
.prototype.onClick = function () {
8920 this.input
.simulateLabelClick();
8925 * Generic option widget for use with OO.ui.SelectWidget.
8928 * @extends OO.ui.Widget
8929 * @mixins OO.ui.LabeledElement
8930 * @mixins OO.ui.FlaggableElement
8933 * @param {Mixed} data Option data
8934 * @param {Object} [config] Configuration options
8935 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
8937 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
8938 // Config intialization
8939 config
= config
|| {};
8941 // Parent constructor
8942 OO
.ui
.OptionWidget
.super.call( this, config
);
8944 // Mixin constructors
8945 OO
.ui
.ItemWidget
.call( this );
8946 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
8947 OO
.ui
.FlaggableElement
.call( this, config
);
8951 this.selected
= false;
8952 this.highlighted
= false;
8953 this.pressed
= false;
8957 .data( 'oo-ui-optionWidget', this )
8958 .attr( 'rel', config
.rel
)
8959 .attr( 'role', 'option' )
8960 .addClass( 'oo-ui-optionWidget' )
8961 .append( this.$label
);
8963 .prepend( this.$icon
)
8964 .append( this.$indicator
);
8969 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
8970 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
8971 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
8972 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
8974 /* Static Properties */
8976 OO
.ui
.OptionWidget
.static.tagName
= 'li';
8978 OO
.ui
.OptionWidget
.static.selectable
= true;
8980 OO
.ui
.OptionWidget
.static.highlightable
= true;
8982 OO
.ui
.OptionWidget
.static.pressable
= true;
8984 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
8989 * Check if option can be selected.
8991 * @return {boolean} Item is selectable
8993 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
8994 return this.constructor.static.selectable
&& !this.isDisabled();
8998 * Check if option can be highlighted.
9000 * @return {boolean} Item is highlightable
9002 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
9003 return this.constructor.static.highlightable
&& !this.isDisabled();
9007 * Check if option can be pressed.
9009 * @return {boolean} Item is pressable
9011 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
9012 return this.constructor.static.pressable
&& !this.isDisabled();
9016 * Check if option is selected.
9018 * @return {boolean} Item is selected
9020 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
9021 return this.selected
;
9025 * Check if option is highlighted.
9027 * @return {boolean} Item is highlighted
9029 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
9030 return this.highlighted
;
9034 * Check if option is pressed.
9036 * @return {boolean} Item is pressed
9038 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
9039 return this.pressed
;
9043 * Set selected state.
9045 * @param {boolean} [state=false] Select option
9048 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
9049 if ( this.constructor.static.selectable
) {
9050 this.selected
= !!state
;
9051 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
9052 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
9053 this.scrollElementIntoView();
9060 * Set highlighted state.
9062 * @param {boolean} [state=false] Highlight option
9065 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
9066 if ( this.constructor.static.highlightable
) {
9067 this.highlighted
= !!state
;
9068 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
9074 * Set pressed state.
9076 * @param {boolean} [state=false] Press option
9079 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
9080 if ( this.constructor.static.pressable
) {
9081 this.pressed
= !!state
;
9082 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
9088 * Make the option's highlight flash.
9090 * While flashing, the visual style of the pressed state is removed if present.
9092 * @return {jQuery.Promise} Promise resolved when flashing is done
9094 OO
.ui
.OptionWidget
.prototype.flash = function () {
9096 $element
= this.$element
,
9097 deferred
= $.Deferred();
9099 if ( !this.isDisabled() && this.constructor.static.pressable
) {
9100 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
9101 setTimeout( function () {
9102 // Restore original classes
9104 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
9105 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
9107 setTimeout( function () {
9114 return deferred
.promise();
9120 * @return {Mixed} Option data
9122 OO
.ui
.OptionWidget
.prototype.getData = function () {
9127 * Option widget with an option icon and indicator.
9129 * Use together with OO.ui.SelectWidget.
9132 * @extends OO.ui.OptionWidget
9133 * @mixins OO.ui.IconedElement
9134 * @mixins OO.ui.IndicatedElement
9137 * @param {Mixed} data Option data
9138 * @param {Object} [config] Configuration options
9140 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
9141 // Parent constructor
9142 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
9144 // Mixin constructors
9145 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
9146 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
9150 .addClass( 'oo-ui-decoratedOptionWidget' )
9151 .prepend( this.$icon
)
9152 .append( this.$indicator
);
9157 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
9158 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
9159 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
9162 * Option widget that looks like a button.
9164 * Use together with OO.ui.ButtonSelectWidget.
9167 * @extends OO.ui.DecoratedOptionWidget
9168 * @mixins OO.ui.ButtonedElement
9171 * @param {Mixed} data Option data
9172 * @param {Object} [config] Configuration options
9174 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
9175 // Parent constructor
9176 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
9178 // Mixin constructors
9179 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
9182 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
9183 this.$button
.append( this.$element
.contents() );
9184 this.$element
.append( this.$button
);
9189 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
9190 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
9192 /* Static Properties */
9194 // Allow button mouse down events to pass through so they can be handled by the parent select widget
9195 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
9202 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
9203 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
9205 if ( this.constructor.static.selectable
) {
9206 this.setActive( state
);
9213 * Item of an OO.ui.MenuWidget.
9216 * @extends OO.ui.DecoratedOptionWidget
9219 * @param {Mixed} data Item data
9220 * @param {Object} [config] Configuration options
9222 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
9223 // Configuration initialization
9224 config
= $.extend( { icon
: 'check' }, config
);
9226 // Parent constructor
9227 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
9231 .attr( 'role', 'menuitem' )
9232 .addClass( 'oo-ui-menuItemWidget' );
9237 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9240 * Section to group one or more items in a OO.ui.MenuWidget.
9243 * @extends OO.ui.DecoratedOptionWidget
9246 * @param {Mixed} data Item data
9247 * @param {Object} [config] Configuration options
9249 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
9250 // Parent constructor
9251 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
9254 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
9259 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9261 /* Static Properties */
9263 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
9265 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
9268 * Items for an OO.ui.OutlineWidget.
9271 * @extends OO.ui.DecoratedOptionWidget
9274 * @param {Mixed} data Item data
9275 * @param {Object} [config] Configuration options
9276 * @cfg {number} [level] Indentation level
9277 * @cfg {boolean} [movable] Allow modification from outline controls
9279 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
9280 // Config intialization
9281 config
= config
|| {};
9283 // Parent constructor
9284 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
9288 this.movable
= !!config
.movable
;
9289 this.removable
= !!config
.removable
;
9292 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
9293 this.setLevel( config
.level
);
9298 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9300 /* Static Properties */
9302 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
9304 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
9306 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
9308 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
9313 * Check if item is movable.
9315 * Movablilty is used by outline controls.
9317 * @return {boolean} Item is movable
9319 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
9320 return this.movable
;
9324 * Check if item is removable.
9326 * Removablilty is used by outline controls.
9328 * @return {boolean} Item is removable
9330 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
9331 return this.removable
;
9335 * Get indentation level.
9337 * @return {number} Indentation level
9339 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
9346 * Movablilty is used by outline controls.
9348 * @param {boolean} movable Item is movable
9351 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
9352 this.movable
= !!movable
;
9359 * Removablilty is used by outline controls.
9361 * @param {boolean} movable Item is removable
9364 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
9365 this.removable
= !!removable
;
9370 * Set indentation level.
9372 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
9375 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
9376 var levels
= this.constructor.static.levels
,
9377 levelClass
= this.constructor.static.levelClass
,
9380 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
9382 if ( this.level
=== i
) {
9383 this.$element
.addClass( levelClass
+ i
);
9385 this.$element
.removeClass( levelClass
+ i
);
9393 * Container for content that is overlaid and positioned absolutely.
9396 * @extends OO.ui.Widget
9397 * @mixins OO.ui.LabeledElement
9400 * @param {Object} [config] Configuration options
9401 * @cfg {number} [width=320] Width of popup in pixels
9402 * @cfg {number} [height] Height of popup, omit to use automatic height
9403 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
9404 * @cfg {string} [align='center'] Alignment of popup to origin
9405 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
9406 * @cfg {jQuery} [$content] Content to append to the popup's body
9407 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
9408 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
9409 * @cfg {boolean} [head] Show label and close button at the top
9410 * @cfg {boolean} [padded] Add padding to the body
9412 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
9413 // Config intialization
9414 config
= config
|| {};
9416 // Parent constructor
9417 OO
.ui
.PopupWidget
.super.call( this, config
);
9419 // Mixin constructors
9420 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
9421 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
9424 this.visible
= false;
9425 this.$popup
= this.$( '<div>' );
9426 this.$head
= this.$( '<div>' );
9427 this.$body
= this.$clippable
;
9428 this.$anchor
= this.$( '<div>' );
9429 this.$container
= config
.$container
|| this.$( 'body' );
9430 this.autoClose
= !!config
.autoClose
;
9431 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
9432 this.transitionTimeout
= null;
9434 this.width
= config
.width
!== undefined ? config
.width
: 320;
9435 this.height
= config
.height
!== undefined ? config
.height
: null;
9436 this.align
= config
.align
|| 'center';
9437 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
9438 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
9441 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
9444 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
9445 this.$body
.addClass( 'oo-ui-popupWidget-body' );
9446 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
9448 .addClass( 'oo-ui-popupWidget-head' )
9449 .append( this.$label
, this.closeButton
.$element
);
9450 if ( !config
.head
) {
9454 .addClass( 'oo-ui-popupWidget-popup' )
9455 .append( this.$head
, this.$body
);
9458 .addClass( 'oo-ui-popupWidget' )
9459 .append( this.$popup
, this.$anchor
);
9460 // Move content, which was added to #$element by OO.ui.Widget, to the body
9461 if ( config
.$content
instanceof jQuery
) {
9462 this.$body
.append( config
.$content
);
9464 if ( config
.padded
) {
9465 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
9471 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
9472 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
9473 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
9488 * Handles mouse down events.
9490 * @param {jQuery.Event} e Mouse down event
9492 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
9495 !$.contains( this.$element
[0], e
.target
) &&
9496 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
9498 this.toggle( false );
9503 * Bind mouse down listener.
9505 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
9506 // Capture clicks outside popup
9507 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
9511 * Handles close button click events.
9513 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
9514 if ( this.isVisible() ) {
9515 this.toggle( false );
9520 * Unbind mouse down listener.
9522 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
9523 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
9527 * Set whether to show a anchor.
9529 * @param {boolean} [show] Show anchor, omit to toggle
9531 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
9532 show
= show
=== undefined ? !this.anchored
: !!show
;
9534 if ( this.anchored
!== show
) {
9536 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
9538 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
9540 this.anchored
= show
;
9545 * Check if showing a anchor.
9547 * @return {boolean} anchor is visible
9549 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
9556 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
9557 show
= show
=== undefined ? !this.isVisible() : !!show
;
9559 var change
= show
!== this.isVisible();
9562 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
9566 this.setClipping( true );
9567 if ( this.autoClose
) {
9568 this.bindMouseDownListener();
9570 this.updateDimensions();
9572 this.setClipping( false );
9573 if ( this.autoClose
) {
9574 this.unbindMouseDownListener();
9583 * Set the size of the popup.
9585 * Changing the size may also change the popup's position depending on the alignment.
9587 * @param {number} width Width
9588 * @param {number} height Height
9589 * @param {boolean} [transition=false] Use a smooth transition
9592 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
9594 this.height
= height
!== undefined ? height
: null;
9595 if ( this.isVisible() ) {
9596 this.updateDimensions( transition
);
9601 * Update the size and position.
9603 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
9604 * be called automatically.
9606 * @param {boolean} [transition=false] Use a smooth transition
9609 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
9612 originOffset
= Math
.round( this.$element
.offset().left
),
9613 containerLeft
= Math
.round( this.$container
.offset().left
),
9614 containerWidth
= this.$container
.innerWidth(),
9615 containerRight
= containerLeft
+ containerWidth
,
9616 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
],
9617 anchorWidth
= this.$anchor
.width(),
9618 popupLeft
= popupOffset
- padding
,
9619 popupRight
= popupOffset
+ padding
+ this.width
+ padding
,
9620 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
9621 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
9623 // Prevent transition from being interrupted
9624 clearTimeout( this.transitionTimeout
);
9626 // Enable transition
9627 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
9630 if ( overlapRight
< 0 ) {
9631 popupOffset
+= overlapRight
;
9632 } else if ( overlapLeft
< 0 ) {
9633 popupOffset
-= overlapLeft
;
9636 // Adjust offset to avoid anchor being rendered too close to the edge
9637 if ( this.align
=== 'right' ) {
9638 popupOffset
+= anchorWidth
;
9639 } else if ( this.align
=== 'left' ) {
9640 popupOffset
-= anchorWidth
;
9643 // Position body relative to anchor and resize
9647 height
: this.height
!== null ? this.height
: 'auto'
9651 // Prevent transitioning after transition is complete
9652 this.transitionTimeout
= setTimeout( function () {
9653 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
9656 // Prevent transitioning immediately
9657 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
9666 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
9667 * Results are cleared and populated each time the query is changed.
9670 * @extends OO.ui.Widget
9673 * @param {Object} [config] Configuration options
9674 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
9675 * @cfg {string} [value] Initial query value
9677 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
9678 // Configuration intialization
9679 config
= config
|| {};
9681 // Parent constructor
9682 OO
.ui
.SearchWidget
.super.call( this, config
);
9685 this.query
= new OO
.ui
.TextInputWidget( {
9688 placeholder
: config
.placeholder
,
9691 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
9692 this.$query
= this.$( '<div>' );
9693 this.$results
= this.$( '<div>' );
9696 this.query
.connect( this, {
9697 change
: 'onQueryChange',
9698 enter
: 'onQueryEnter'
9700 this.results
.connect( this, {
9701 highlight
: 'onResultsHighlight',
9702 select
: 'onResultsSelect'
9704 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
9708 .addClass( 'oo-ui-searchWidget-query' )
9709 .append( this.query
.$element
);
9711 .addClass( 'oo-ui-searchWidget-results' )
9712 .append( this.results
.$element
);
9714 .addClass( 'oo-ui-searchWidget' )
9715 .append( this.$results
, this.$query
);
9720 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
9726 * @param {Object|null} item Item data or null if no item is highlighted
9731 * @param {Object|null} item Item data or null if no item is selected
9737 * Handle query key down events.
9739 * @param {jQuery.Event} e Key down event
9741 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
9742 var highlightedItem
, nextItem
,
9743 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
9746 highlightedItem
= this.results
.getHighlightedItem();
9747 if ( !highlightedItem
) {
9748 highlightedItem
= this.results
.getSelectedItem();
9750 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
9751 this.results
.highlightItem( nextItem
);
9752 nextItem
.scrollElementIntoView();
9757 * Handle select widget select events.
9759 * Clears existing results. Subclasses should repopulate items according to new query.
9761 * @param {string} value New value
9763 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
9765 this.results
.clearItems();
9769 * Handle select widget enter key events.
9771 * Selects highlighted item.
9773 * @param {string} value New value
9775 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
9777 this.results
.selectItem( this.results
.getHighlightedItem() );
9781 * Handle select widget highlight events.
9783 * @param {OO.ui.OptionWidget} item Highlighted item
9786 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
9787 this.emit( 'highlight', item
? item
.getData() : null );
9791 * Handle select widget select events.
9793 * @param {OO.ui.OptionWidget} item Selected item
9796 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
9797 this.emit( 'select', item
? item
.getData() : null );
9801 * Get the query input.
9803 * @return {OO.ui.TextInputWidget} Query input
9805 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
9810 * Get the results list.
9812 * @return {OO.ui.SelectWidget} Select list
9814 OO
.ui
.SearchWidget
.prototype.getResults = function () {
9815 return this.results
;
9819 * Generic selection of options.
9821 * Items can contain any rendering, and are uniquely identified by a has of thier data. Any widget
9822 * that provides options, from which the user must choose one, should be built on this class.
9824 * Use together with OO.ui.OptionWidget.
9827 * @extends OO.ui.Widget
9828 * @mixins OO.ui.GroupElement
9831 * @param {Object} [config] Configuration options
9832 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
9834 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
9835 // Config intialization
9836 config
= config
|| {};
9838 // Parent constructor
9839 OO
.ui
.SelectWidget
.super.call( this, config
);
9841 // Mixin constructors
9842 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
9845 this.pressed
= false;
9846 this.selecting
= null;
9848 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
9849 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
9853 mousedown
: OO
.ui
.bind( this.onMouseDown
, this ),
9854 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
9855 mouseleave
: OO
.ui
.bind( this.onMouseLeave
, this )
9859 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
9860 if ( $.isArray( config
.items
) ) {
9861 this.addItems( config
.items
);
9867 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
9869 // Need to mixin base class as well
9870 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
9871 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
9877 * @param {OO.ui.OptionWidget|null} item Highlighted item
9882 * @param {OO.ui.OptionWidget|null} item Pressed item
9887 * @param {OO.ui.OptionWidget|null} item Selected item
9892 * @param {OO.ui.OptionWidget|null} item Chosen item
9897 * @param {OO.ui.OptionWidget[]} items Added items
9898 * @param {number} index Index items were added at
9903 * @param {OO.ui.OptionWidget[]} items Removed items
9906 /* Static Properties */
9908 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
9913 * Handle mouse down events.
9916 * @param {jQuery.Event} e Mouse down event
9918 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
9921 if ( !this.isDisabled() && e
.which
=== 1 ) {
9922 this.togglePressed( true );
9923 item
= this.getTargetItem( e
);
9924 if ( item
&& item
.isSelectable() ) {
9925 this.pressItem( item
);
9926 this.selecting
= item
;
9927 this.getElementDocument().addEventListener(
9929 this.onMouseUpHandler
,
9932 this.getElementDocument().addEventListener(
9934 this.onMouseMoveHandler
,
9943 * Handle mouse up events.
9946 * @param {jQuery.Event} e Mouse up event
9948 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
9951 this.togglePressed( false );
9952 if ( !this.selecting
) {
9953 item
= this.getTargetItem( e
);
9954 if ( item
&& item
.isSelectable() ) {
9955 this.selecting
= item
;
9958 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
9959 this.pressItem( null );
9960 this.chooseItem( this.selecting
);
9961 this.selecting
= null;
9964 this.getElementDocument().removeEventListener(
9966 this.onMouseUpHandler
,
9969 this.getElementDocument().removeEventListener(
9971 this.onMouseMoveHandler
,
9979 * Handle mouse move events.
9982 * @param {jQuery.Event} e Mouse move event
9984 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
9987 if ( !this.isDisabled() && this.pressed
) {
9988 item
= this.getTargetItem( e
);
9989 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
9990 this.pressItem( item
);
9991 this.selecting
= item
;
9998 * Handle mouse over events.
10001 * @param {jQuery.Event} e Mouse over event
10003 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
10006 if ( !this.isDisabled() ) {
10007 item
= this.getTargetItem( e
);
10008 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
10014 * Handle mouse leave events.
10017 * @param {jQuery.Event} e Mouse over event
10019 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
10020 if ( !this.isDisabled() ) {
10021 this.highlightItem( null );
10027 * Get the closest item to a jQuery.Event.
10030 * @param {jQuery.Event} e
10031 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
10033 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
10034 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
10035 if ( $item
.length
) {
10036 return $item
.data( 'oo-ui-optionWidget' );
10042 * Get selected item.
10044 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
10046 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
10049 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10050 if ( this.items
[i
].isSelected() ) {
10051 return this.items
[i
];
10058 * Get highlighted item.
10060 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
10062 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
10065 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10066 if ( this.items
[i
].isHighlighted() ) {
10067 return this.items
[i
];
10074 * Get an existing item with equivilant data.
10076 * @param {Object} data Item data to search for
10077 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
10079 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
10080 var hash
= OO
.getHash( data
);
10082 if ( hash
in this.hashes
) {
10083 return this.hashes
[hash
];
10090 * Toggle pressed state.
10092 * @param {boolean} pressed An option is being pressed
10094 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
10095 if ( pressed
=== undefined ) {
10096 pressed
= !this.pressed
;
10098 if ( pressed
!== this.pressed
) {
10100 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
10101 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
10102 this.pressed
= pressed
;
10107 * Highlight an item.
10109 * Highlighting is mutually exclusive.
10111 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
10115 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
10116 var i
, len
, highlighted
,
10119 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10120 highlighted
= this.items
[i
] === item
;
10121 if ( this.items
[i
].isHighlighted() !== highlighted
) {
10122 this.items
[i
].setHighlighted( highlighted
);
10127 this.emit( 'highlight', item
);
10136 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
10140 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
10141 var i
, len
, selected
,
10144 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10145 selected
= this.items
[i
] === item
;
10146 if ( this.items
[i
].isSelected() !== selected
) {
10147 this.items
[i
].setSelected( selected
);
10152 this.emit( 'select', item
);
10161 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
10165 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
10166 var i
, len
, pressed
,
10169 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10170 pressed
= this.items
[i
] === item
;
10171 if ( this.items
[i
].isPressed() !== pressed
) {
10172 this.items
[i
].setPressed( pressed
);
10177 this.emit( 'press', item
);
10186 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
10187 * an item is selected using the keyboard or mouse.
10189 * @param {OO.ui.OptionWidget} item Item to choose
10193 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
10194 this.selectItem( item
);
10195 this.emit( 'choose', item
);
10201 * Get an item relative to another one.
10203 * @param {OO.ui.OptionWidget} item Item to start at
10204 * @param {number} direction Direction to move in
10205 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
10207 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
10208 var inc
= direction
> 0 ? 1 : -1,
10209 len
= this.items
.length
,
10210 index
= item
instanceof OO
.ui
.OptionWidget
?
10211 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
10212 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
10214 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
10215 Math
.max( index
, -1 ) :
10216 // Default to n-1 instead of -1, if nothing is selected let's start at the end
10217 Math
.min( index
, len
);
10220 i
= ( i
+ inc
+ len
) % len
;
10221 item
= this.items
[i
];
10222 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10225 // Stop iterating when we've looped all the way around
10226 if ( i
=== stopAt
) {
10234 * Get the next selectable item.
10236 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
10238 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
10241 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10242 item
= this.items
[i
];
10243 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10254 * When items are added with the same values as existing items, the existing items will be
10255 * automatically removed before the new items are added.
10257 * @param {OO.ui.OptionWidget[]} items Items to add
10258 * @param {number} [index] Index to insert items after
10262 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
10263 var i
, len
, item
, hash
,
10266 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10268 hash
= OO
.getHash( item
.getData() );
10269 if ( hash
in this.hashes
) {
10270 // Remove item with same value
10271 remove
.push( this.hashes
[hash
] );
10273 this.hashes
[hash
] = item
;
10275 if ( remove
.length
) {
10276 this.removeItems( remove
);
10280 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
10282 // Always provide an index, even if it was omitted
10283 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
10291 * Items will be detached, not removed, so they can be used later.
10293 * @param {OO.ui.OptionWidget[]} items Items to remove
10297 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
10298 var i
, len
, item
, hash
;
10300 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10302 hash
= OO
.getHash( item
.getData() );
10303 if ( hash
in this.hashes
) {
10304 // Remove existing item
10305 delete this.hashes
[hash
];
10307 if ( item
.isSelected() ) {
10308 this.selectItem( null );
10313 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
10315 this.emit( 'remove', items
);
10323 * Items will be detached, not removed, so they can be used later.
10328 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
10329 var items
= this.items
.slice();
10334 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
10335 this.selectItem( null );
10337 this.emit( 'remove', items
);
10343 * Select widget containing button options.
10345 * Use together with OO.ui.ButtonOptionWidget.
10348 * @extends OO.ui.SelectWidget
10351 * @param {Object} [config] Configuration options
10353 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
10354 // Parent constructor
10355 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
10358 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
10363 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
10366 * Overlaid menu of options.
10368 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
10371 * Use together with OO.ui.MenuItemWidget.
10374 * @extends OO.ui.SelectWidget
10375 * @mixins OO.ui.ClippableElement
10378 * @param {Object} [config] Configuration options
10379 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
10380 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
10381 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
10383 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
10384 // Config intialization
10385 config
= config
|| {};
10387 // Parent constructor
10388 OO
.ui
.MenuWidget
.super.call( this, config
);
10390 // Mixin constructors
10391 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
10394 this.flashing
= false;
10395 this.visible
= false;
10396 this.newItems
= null;
10397 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
10398 this.$input
= config
.input
? config
.input
.$input
: null;
10399 this.$widget
= config
.widget
? config
.widget
.$element
: null;
10400 this.$previousFocus
= null;
10401 this.isolated
= !config
.input
;
10402 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
10403 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
10408 .attr( 'role', 'menu' )
10409 .addClass( 'oo-ui-menuWidget' );
10414 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
10415 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
10420 * Handles document mouse down events.
10422 * @param {jQuery.Event} e Key down event
10424 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
10425 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
10426 this.toggle( false );
10431 * Handles key down events.
10433 * @param {jQuery.Event} e Key down event
10435 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
10438 highlightItem
= this.getHighlightedItem();
10440 if ( !this.isDisabled() && this.isVisible() ) {
10441 if ( !highlightItem
) {
10442 highlightItem
= this.getSelectedItem();
10444 switch ( e
.keyCode
) {
10445 case OO
.ui
.Keys
.ENTER
:
10446 this.chooseItem( highlightItem
);
10449 case OO
.ui
.Keys
.UP
:
10450 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
10453 case OO
.ui
.Keys
.DOWN
:
10454 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
10457 case OO
.ui
.Keys
.ESCAPE
:
10458 if ( highlightItem
) {
10459 highlightItem
.setHighlighted( false );
10461 this.toggle( false );
10467 this.highlightItem( nextItem
);
10468 nextItem
.scrollElementIntoView();
10472 e
.preventDefault();
10473 e
.stopPropagation();
10480 * Bind key down listener.
10482 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
10483 if ( this.$input
) {
10484 this.$input
.on( 'keydown', this.onKeyDownHandler
);
10486 // Capture menu navigation keys
10487 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
10492 * Unbind key down listener.
10494 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
10495 if ( this.$input
) {
10496 this.$input
.off( 'keydown' );
10498 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
10505 * This will close the menu when done, unlike selectItem which only changes selection.
10507 * @param {OO.ui.OptionWidget} item Item to choose
10510 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
10514 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
10516 if ( item
&& !this.flashing
) {
10517 this.flashing
= true;
10518 item
.flash().done( function () {
10519 widget
.toggle( false );
10520 widget
.flashing
= false;
10523 this.toggle( false );
10532 * Adding an existing item (by value) will move it.
10534 * @param {OO.ui.MenuItemWidget[]} items Items to add
10535 * @param {number} [index] Index to insert items after
10538 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
10542 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
10545 if ( !this.newItems
) {
10546 this.newItems
= [];
10549 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10551 if ( this.isVisible() ) {
10552 // Defer fitting label until
10555 this.newItems
.push( item
);
10565 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
10566 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
10569 change
= visible
!== this.isVisible();
10572 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
10576 this.bindKeyDownListener();
10578 // Change focus to enable keyboard navigation
10579 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
10580 this.$previousFocus
= this.$( ':focus' );
10581 this.$input
[0].focus();
10583 if ( this.newItems
&& this.newItems
.length
) {
10584 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
10585 this.newItems
[i
].fitLabel();
10587 this.newItems
= null;
10589 this.setClipping( true );
10592 if ( this.autoHide
) {
10593 this.getElementDocument().addEventListener(
10594 'mousedown', this.onDocumentMouseDownHandler
, true
10598 this.unbindKeyDownListener();
10599 if ( this.isolated
&& this.$previousFocus
) {
10600 this.$previousFocus
[0].focus();
10601 this.$previousFocus
= null;
10603 this.getElementDocument().removeEventListener(
10604 'mousedown', this.onDocumentMouseDownHandler
, true
10606 this.setClipping( false );
10614 * Menu for a text input widget.
10616 * This menu is specially designed to be positioned beneeth the text input widget. Even if the input
10617 * is in a different frame, the menu's position is automatically calulated and maintained when the
10618 * menu is toggled or the window is resized.
10621 * @extends OO.ui.MenuWidget
10624 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
10625 * @param {Object} [config] Configuration options
10626 * @cfg {jQuery} [$container=input.$element] Element to render menu under
10628 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
10629 // Parent constructor
10630 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
10633 this.input
= input
;
10634 this.$container
= config
.$container
|| this.input
.$element
;
10635 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
10638 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
10643 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
10648 * Handle window resize event.
10650 * @param {jQuery.Event} e Window resize event
10652 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
10659 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
10660 visible
= !!visible
;
10662 var change
= visible
!== this.isVisible();
10665 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
10668 if ( this.isVisible() ) {
10670 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
10672 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
10679 * Position the menu.
10683 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
10685 $container
= this.$container
,
10686 dimensions
= $container
.offset();
10688 // Position under input
10689 dimensions
.top
+= $container
.height();
10691 // Compensate for frame position if in a differnt frame
10692 if ( this.input
.$.$iframe
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
10693 frameOffset
= OO
.ui
.Element
.getRelativePosition(
10694 this.input
.$.$iframe
, this.$element
.offsetParent()
10696 dimensions
.left
+= frameOffset
.left
;
10697 dimensions
.top
+= frameOffset
.top
;
10699 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
10700 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
10701 dimensions
.right
= this.$element
.parent().position().left
-
10702 $container
.width() - dimensions
.left
;
10703 // Erase the value for 'left':
10704 delete dimensions
.left
;
10707 this.$element
.css( dimensions
);
10708 this.setIdealSize( $container
.width() );
10714 * Structured list of items.
10716 * Use with OO.ui.OutlineItemWidget.
10719 * @extends OO.ui.SelectWidget
10722 * @param {Object} [config] Configuration options
10724 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
10725 // Config intialization
10726 config
= config
|| {};
10728 // Parent constructor
10729 OO
.ui
.OutlineWidget
.super.call( this, config
);
10732 this.$element
.addClass( 'oo-ui-outlineWidget' );
10737 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
10740 * Switch that slides on and off.
10743 * @extends OO.ui.Widget
10744 * @mixins OO.ui.ToggleWidget
10747 * @param {Object} [config] Configuration options
10748 * @cfg {boolean} [value=false] Initial value
10750 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
10751 // Parent constructor
10752 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
10754 // Mixin constructors
10755 OO
.ui
.ToggleWidget
.call( this, config
);
10758 this.dragging
= false;
10759 this.dragStart
= null;
10760 this.sliding
= false;
10761 this.$glow
= this.$( '<span>' );
10762 this.$grip
= this.$( '<span>' );
10765 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
10768 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
10769 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
10771 .addClass( 'oo-ui-toggleSwitchWidget' )
10772 .append( this.$glow
, this.$grip
);
10777 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
10778 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
10783 * Handle mouse down events.
10785 * @param {jQuery.Event} e Mouse down event
10787 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
10788 if ( !this.isDisabled() && e
.which
=== 1 ) {
10789 this.setValue( !this.value
);