2 * OOjs UI v0.1.0-pre (db065e5a9f)
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-10-20T14:47:45Z
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 // Label for the fake tool that expands the full list of tools in a toolbar group
115 'ooui-toolgroup-expand': 'More',
116 // Label for the fake tool that collapses the full list of tools in a toolbar group
117 'ooui-toolgroup-collapse': 'Fewer',
118 // Default label for the accept button of a confirmation dialog
119 'ooui-dialog-message-accept': 'OK',
120 // Default label for the reject button of a confirmation dialog
121 'ooui-dialog-message-reject': 'Cancel',
122 // Title for process dialog error description
123 'ooui-dialog-process-error': 'Something went wrong',
124 // Label for process dialog dismiss error button, visible when describing errors
125 'ooui-dialog-process-dismiss': 'Dismiss',
126 // Label for process dialog retry action button, visible when describing recoverable errors
127 'ooui-dialog-process-retry': 'Try again'
131 * Get a localized message.
133 * In environments that provide a localization system, this function should be overridden to
134 * return the message translated in the user's language. The default implementation always returns
137 * After the message key, message parameters may optionally be passed. In the default implementation,
138 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
139 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
140 * they support unnamed, ordered message parameters.
143 * @param {string} key Message key
144 * @param {Mixed...} [params] Message parameters
145 * @return {string} Translated message with parameters substituted
147 OO
.ui
.msg = function ( key
) {
148 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
149 if ( typeof message
=== 'string' ) {
150 // Perform $1 substitution
151 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
152 var i
= parseInt( n
, 10 );
153 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
156 // Return placeholder if message not found
157 message
= '[' + key
+ ']';
163 * Package a message and arguments for deferred resolution.
165 * Use this when you are statically specifying a message and the message may not yet be present.
167 * @param {string} key Message key
168 * @param {Mixed...} [params] Message parameters
169 * @return {Function} Function that returns the resolved message when executed
171 OO
.ui
.deferMsg = function () {
172 var args
= arguments
;
174 return OO
.ui
.msg
.apply( OO
.ui
, args
);
181 * If the message is a function it will be executed, otherwise it will pass through directly.
183 * @param {Function|string} msg Deferred message, or message text
184 * @return {string} Resolved message
186 OO
.ui
.resolveMsg = function ( msg
) {
187 if ( $.isFunction( msg
) ) {
196 * Element that can be marked as pending.
202 * @param {Object} [config] Configuration options
204 OO
.ui
.PendingElement
= function OoUiPendingElement( config
) {
205 // Config initialisation
206 config
= config
|| {};
210 this.$pending
= null;
213 this.setPendingElement( config
.$pending
|| this.$element
);
218 OO
.initClass( OO
.ui
.PendingElement
);
223 * Set the pending element (and clean up any existing one).
225 * @param {jQuery} $pending The element to set to pending.
227 OO
.ui
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
228 if ( this.$pending
) {
229 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
232 this.$pending
= $pending
;
233 if ( this.pending
> 0 ) {
234 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
239 * Check if input is pending.
243 OO
.ui
.PendingElement
.prototype.isPending = function () {
244 return !!this.pending
;
248 * Increase the pending stack.
252 OO
.ui
.PendingElement
.prototype.pushPending = function () {
253 if ( this.pending
=== 0 ) {
254 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
255 this.updateThemeClasses();
263 * Reduce the pending stack.
269 OO
.ui
.PendingElement
.prototype.popPending = function () {
270 if ( this.pending
=== 1 ) {
271 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
272 this.updateThemeClasses();
274 this.pending
= Math
.max( 0, this.pending
- 1 );
284 * @mixins OO.EventEmitter
287 * @param {Object} [config] Configuration options
289 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
290 // Configuration intialization
291 config
= config
|| {};
293 // Mixin constructors
294 OO
.EventEmitter
.call( this );
299 actions
: 'getAction',
303 this.categorized
= {};
306 this.organized
= false;
307 this.changing
= false;
308 this.changed
= false;
313 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
315 /* Static Properties */
318 * Symbolic name of dialog.
325 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
331 * @param {OO.ui.ActionWidget} action Action that was clicked
336 * @param {OO.ui.ActionWidget} action Action that was resized
341 * @param {OO.ui.ActionWidget[]} added Actions added
346 * @param {OO.ui.ActionWidget[]} added Actions removed
356 * Handle action change events.
360 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
361 this.organized
= false;
362 if ( this.changing
) {
365 this.emit( 'change' );
370 * Check if a action is one of the special actions.
372 * @param {OO.ui.ActionWidget} action Action to check
373 * @return {boolean} Action is special
375 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
378 for ( flag
in this.special
) {
379 if ( action
=== this.special
[flag
] ) {
390 * @param {Object} [filters] Filters to use, omit to get all actions
391 * @param {string|string[]} [filters.actions] Actions that actions must have
392 * @param {string|string[]} [filters.flags] Flags that actions must have
393 * @param {string|string[]} [filters.modes] Modes that actions must have
394 * @param {boolean} [filters.visible] Actions must be visible
395 * @param {boolean} [filters.disabled] Actions must be disabled
396 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
398 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
399 var i
, len
, list
, category
, actions
, index
, match
, matches
;
404 // Collect category candidates
406 for ( category
in this.categorized
) {
407 list
= filters
[category
];
409 if ( !Array
.isArray( list
) ) {
412 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
413 actions
= this.categorized
[category
][list
[i
]];
414 if ( Array
.isArray( actions
) ) {
415 matches
.push
.apply( matches
, actions
);
420 // Remove by boolean filters
421 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
424 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
425 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
427 matches
.splice( i
, 1 );
433 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
435 index
= matches
.lastIndexOf( match
);
436 while ( index
!== i
) {
437 matches
.splice( index
, 1 );
439 index
= matches
.lastIndexOf( match
);
444 return this.list
.slice();
448 * Get special actions.
450 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
451 * Special flags can be configured by changing #static-specialFlags in a subclass.
453 * @return {OO.ui.ActionWidget|null} Safe action
455 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
457 return $.extend( {}, this.special
);
463 * Other actions include all non-special visible actions.
465 * @return {OO.ui.ActionWidget[]} Other actions
467 OO
.ui
.ActionSet
.prototype.getOthers = function () {
469 return this.others
.slice();
473 * Toggle actions based on their modes.
475 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
476 * visibility; matching actions will be shown, non-matching actions will be hidden.
478 * @param {string} mode Mode actions must have
483 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
486 this.changing
= true;
487 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
488 action
= this.list
[i
];
489 action
.toggle( action
.hasMode( mode
) );
492 this.organized
= false;
493 this.changing
= false;
494 this.emit( 'change' );
500 * Change which actions are able to be performed.
502 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
504 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
505 * indicate actions are able to be performed
508 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
509 var i
, len
, action
, item
;
511 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
513 action
= item
.getAction();
514 if ( actions
[action
] !== undefined ) {
515 item
.setDisabled( !actions
[action
] );
523 * Executes a function once per action.
525 * When making changes to multiple actions, use this method instead of iterating over the actions
526 * manually to defer emitting a change event until after all actions have been changed.
528 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
529 * @param {Function} callback Callback to run for each action; callback is invoked with three
530 * arguments: the action, the action's index, the list of actions being iterated over
533 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
534 this.changed
= false;
535 this.changing
= true;
536 this.get( filter
).forEach( callback
);
537 this.changing
= false;
538 if ( this.changed
) {
539 this.emit( 'change' );
548 * @param {OO.ui.ActionWidget[]} actions Actions to add
553 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
556 this.changing
= true;
557 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
559 action
.connect( this, {
560 click
: [ 'emit', 'click', action
],
561 resize
: [ 'emit', 'resize', action
],
562 toggle
: [ 'onActionChange' ]
564 this.list
.push( action
);
566 this.organized
= false;
567 this.emit( 'add', actions
);
568 this.changing
= false;
569 this.emit( 'change' );
577 * @param {OO.ui.ActionWidget[]} actions Actions to remove
582 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
583 var i
, len
, index
, action
;
585 this.changing
= true;
586 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
588 index
= this.list
.indexOf( action
);
589 if ( index
!== -1 ) {
590 action
.disconnect( this );
591 this.list
.splice( index
, 1 );
594 this.organized
= false;
595 this.emit( 'remove', actions
);
596 this.changing
= false;
597 this.emit( 'change' );
603 * Remove all actions.
609 OO
.ui
.ActionSet
.prototype.clear = function () {
611 removed
= this.list
.slice();
613 this.changing
= true;
614 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
615 action
= this.list
[i
];
616 action
.disconnect( this );
621 this.organized
= false;
622 this.emit( 'remove', removed
);
623 this.changing
= false;
624 this.emit( 'change' );
632 * This is called whenver organized information is requested. It will only reorganize the actions
633 * if something has changed since the last time it ran.
638 OO
.ui
.ActionSet
.prototype.organize = function () {
639 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
640 specialFlags
= this.constructor.static.specialFlags
;
642 if ( !this.organized
) {
643 this.categorized
= {};
646 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
647 action
= this.list
[i
];
648 if ( action
.isVisible() ) {
649 // Populate catgeories
650 for ( category
in this.categories
) {
651 if ( !this.categorized
[category
] ) {
652 this.categorized
[category
] = {};
654 list
= action
[this.categories
[category
]]();
655 if ( !Array
.isArray( list
) ) {
658 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
660 if ( !this.categorized
[category
][item
] ) {
661 this.categorized
[category
][item
] = [];
663 this.categorized
[category
][item
].push( action
);
666 // Populate special/others
668 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
669 flag
= specialFlags
[j
];
670 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
671 this.special
[flag
] = action
;
677 this.others
.push( action
);
681 this.organized
= true;
688 * DOM element abstraction.
694 * @param {Object} [config] Configuration options
695 * @cfg {Function} [$] jQuery for the frame the widget is in
696 * @cfg {string[]} [classes] CSS class names
697 * @cfg {string} [text] Text to insert
698 * @cfg {jQuery} [$content] Content elements to append (after text)
700 OO
.ui
.Element
= function OoUiElement( config
) {
701 // Configuration initialization
702 config
= config
|| {};
705 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
706 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
707 this.elementGroup
= null;
708 this.debouncedUpdateThemeClassesHandler
= this.debouncedUpdateThemeClasses
.bind( this );
709 this.updateThemeClassesPending
= false;
712 if ( $.isArray( config
.classes
) ) {
713 this.$element
.addClass( config
.classes
.join( ' ' ) );
716 this.$element
.text( config
.text
);
718 if ( config
.$content
) {
719 this.$element
.append( config
.$content
);
725 OO
.initClass( OO
.ui
.Element
);
727 /* Static Properties */
732 * This may be ignored if #getTagName is overridden.
738 OO
.ui
.Element
.static.tagName
= 'div';
743 * Get a jQuery function within a specific document.
746 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
747 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
749 * @return {Function} Bound jQuery function
751 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
752 function wrapper( selector
) {
753 return $( selector
, wrapper
.context
);
756 wrapper
.context
= this.getDocument( context
);
759 wrapper
.$iframe
= $iframe
;
766 * Get the document of an element.
769 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
770 * @return {HTMLDocument|null} Document object
772 OO
.ui
.Element
.getDocument = function ( obj
) {
773 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
774 return ( obj
[0] && obj
[0].ownerDocument
) ||
775 // Empty jQuery selections might have a context
782 ( obj
.nodeType
=== 9 && obj
) ||
787 * Get the window of an element or document.
790 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
791 * @return {Window} Window object
793 OO
.ui
.Element
.getWindow = function ( obj
) {
794 var doc
= this.getDocument( obj
);
795 return doc
.parentWindow
|| doc
.defaultView
;
799 * Get the direction of an element or document.
802 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
803 * @return {string} Text direction, either `ltr` or `rtl`
805 OO
.ui
.Element
.getDir = function ( obj
) {
808 if ( obj
instanceof jQuery
) {
811 isDoc
= obj
.nodeType
=== 9;
812 isWin
= obj
.document
!== undefined;
813 if ( isDoc
|| isWin
) {
819 return $( obj
).css( 'direction' );
823 * Get the offset between two frames.
825 * TODO: Make this function not use recursion.
828 * @param {Window} from Window of the child frame
829 * @param {Window} [to=window] Window of the parent frame
830 * @param {Object} [offset] Offset to start with, used internally
831 * @return {Object} Offset object, containing left and top properties
833 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
834 var i
, len
, frames
, frame
, rect
;
840 offset
= { top
: 0, left
: 0 };
842 if ( from.parent
=== from ) {
846 // Get iframe element
847 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
848 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
849 if ( frames
[i
].contentWindow
=== from ) {
855 // Recursively accumulate offset values
857 rect
= frame
.getBoundingClientRect();
858 offset
.left
+= rect
.left
;
859 offset
.top
+= rect
.top
;
861 this.getFrameOffset( from.parent
, offset
);
868 * Get the offset between two elements.
870 * The two elements may be in a different frame, but in that case the frame $element is in must
871 * be contained in the frame $anchor is in.
874 * @param {jQuery} $element Element whose position to get
875 * @param {jQuery} $anchor Element to get $element's position relative to
876 * @return {Object} Translated position coordinates, containing top and left properties
878 OO
.ui
.Element
.getRelativePosition = function ( $element
, $anchor
) {
879 var iframe
, iframePos
,
880 pos
= $element
.offset(),
881 anchorPos
= $anchor
.offset(),
882 elementDocument
= this.getDocument( $element
),
883 anchorDocument
= this.getDocument( $anchor
);
885 // If $element isn't in the same document as $anchor, traverse up
886 while ( elementDocument
!== anchorDocument
) {
887 iframe
= elementDocument
.defaultView
.frameElement
;
889 throw new Error( '$element frame is not contained in $anchor frame' );
891 iframePos
= $( iframe
).offset();
892 pos
.left
+= iframePos
.left
;
893 pos
.top
+= iframePos
.top
;
894 elementDocument
= iframe
.ownerDocument
;
896 pos
.left
-= anchorPos
.left
;
897 pos
.top
-= anchorPos
.top
;
902 * Get element border sizes.
905 * @param {HTMLElement} el Element to measure
906 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
908 OO
.ui
.Element
.getBorders = function ( el
) {
909 var doc
= el
.ownerDocument
,
910 win
= doc
.parentWindow
|| doc
.defaultView
,
911 style
= win
&& win
.getComputedStyle
?
912 win
.getComputedStyle( el
, null ) :
915 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
916 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
917 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
918 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
921 top
: Math
.round( top
),
922 left
: Math
.round( left
),
923 bottom
: Math
.round( bottom
),
924 right
: Math
.round( right
)
929 * Get dimensions of an element or window.
932 * @param {HTMLElement|Window} el Element to measure
933 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
935 OO
.ui
.Element
.getDimensions = function ( el
) {
937 doc
= el
.ownerDocument
|| el
.document
,
938 win
= doc
.parentWindow
|| doc
.defaultView
;
940 if ( win
=== el
|| el
=== doc
.documentElement
) {
943 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
945 top
: $win
.scrollTop(),
946 left
: $win
.scrollLeft()
948 scrollbar
: { right
: 0, bottom
: 0 },
952 bottom
: $win
.innerHeight(),
953 right
: $win
.innerWidth()
959 borders
: this.getBorders( el
),
961 top
: $el
.scrollTop(),
962 left
: $el
.scrollLeft()
965 right
: $el
.innerWidth() - el
.clientWidth
,
966 bottom
: $el
.innerHeight() - el
.clientHeight
968 rect
: el
.getBoundingClientRect()
974 * Get closest scrollable container.
976 * Traverses up until either a scrollable element or the root is reached, in which case the window
980 * @param {HTMLElement} el Element to find scrollable container for
981 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
982 * @return {HTMLElement} Closest scrollable container
984 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
986 props
= [ 'overflow' ],
987 $parent
= $( el
).parent();
989 if ( dimension
=== 'x' || dimension
=== 'y' ) {
990 props
.push( 'overflow-' + dimension
);
993 while ( $parent
.length
) {
994 if ( $parent
[0] === el
.ownerDocument
.body
) {
999 val
= $parent
.css( props
[i
] );
1000 if ( val
=== 'auto' || val
=== 'scroll' ) {
1004 $parent
= $parent
.parent();
1006 return this.getDocument( el
).body
;
1010 * Scroll element into view.
1013 * @param {HTMLElement} el Element to scroll into view
1014 * @param {Object} [config={}] Configuration config
1015 * @param {string} [config.duration] jQuery animation duration value
1016 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
1017 * to scroll in both directions
1018 * @param {Function} [config.complete] Function to call when scrolling completes
1020 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
1021 // Configuration initialization
1022 config
= config
|| {};
1025 callback
= typeof config
.complete
=== 'function' && config
.complete
,
1026 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
1028 eld
= this.getDimensions( el
),
1029 scd
= this.getDimensions( sc
),
1030 $win
= $( this.getWindow( el
) );
1032 // Compute the distances between the edges of el and the edges of the scroll viewport
1033 if ( $sc
.is( 'body' ) ) {
1034 // If the scrollable container is the <body> this is easy
1037 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
1038 left
: eld
.rect
.left
,
1039 right
: $win
.innerWidth() - eld
.rect
.right
1042 // Otherwise, we have to subtract el's coordinates from sc's coordinates
1044 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
1045 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
1046 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
1047 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
1051 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1052 if ( rel
.top
< 0 ) {
1053 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
1054 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
1055 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
1058 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1059 if ( rel
.left
< 0 ) {
1060 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
1061 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
1062 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
1065 if ( !$.isEmptyObject( anim
) ) {
1066 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
1068 $sc
.queue( function ( next
) {
1081 * Bind a handler for an event on a DOM element.
1083 * Used to be for working around a jQuery bug (jqbug.com/14180),
1084 * but obsolete as of jQuery 1.11.0.
1087 * @deprecated Use jQuery#on instead.
1088 * @param {HTMLElement|jQuery} el DOM element
1089 * @param {string} event Event to bind
1090 * @param {Function} callback Callback to call when the event fires
1092 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1093 $( el
).on( event
, callback
);
1097 * Unbind a handler bound with #static-method-onDOMEvent.
1099 * @deprecated Use jQuery#off instead.
1101 * @param {HTMLElement|jQuery} el DOM element
1102 * @param {string} event Event to unbind
1103 * @param {Function} [callback] Callback to unbind
1105 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1106 $( el
).off( event
, callback
);
1112 * Check if element supports one or more methods.
1114 * @param {string|string[]} methods Method or list of methods to check
1115 * @return boolean All methods are supported
1117 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1121 methods
= $.isArray( methods
) ? methods
: [ methods
];
1122 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1123 if ( $.isFunction( this[methods
[i
]] ) ) {
1128 return methods
.length
=== support
;
1132 * Update the theme-provided classes.
1134 * @localdoc This is called in element mixins and widget classes anytime state changes.
1135 * Updating is debounced, minimizing overhead of changing multiple attributes and
1136 * guaranteeing that theme updates do not occur within an element's constructor
1138 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1139 if ( !this.updateThemeClassesPending
) {
1140 this.updateThemeClassesPending
= true;
1141 setTimeout( this.debouncedUpdateThemeClassesHandler
);
1148 OO
.ui
.Element
.prototype.debouncedUpdateThemeClasses = function () {
1149 OO
.ui
.theme
.updateElementClasses( this );
1150 this.updateThemeClassesPending
= false;
1154 * Get the HTML tag name.
1156 * Override this method to base the result on instance information.
1158 * @return {string} HTML tag name
1160 OO
.ui
.Element
.prototype.getTagName = function () {
1161 return this.constructor.static.tagName
;
1165 * Check if the element is attached to the DOM
1166 * @return {boolean} The element is attached to the DOM
1168 OO
.ui
.Element
.prototype.isElementAttached = function () {
1169 return $.contains( this.getElementDocument(), this.$element
[0] );
1173 * Get the DOM document.
1175 * @return {HTMLDocument} Document object
1177 OO
.ui
.Element
.prototype.getElementDocument = function () {
1178 return OO
.ui
.Element
.getDocument( this.$element
);
1182 * Get the DOM window.
1184 * @return {Window} Window object
1186 OO
.ui
.Element
.prototype.getElementWindow = function () {
1187 return OO
.ui
.Element
.getWindow( this.$element
);
1191 * Get closest scrollable container.
1193 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1194 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1198 * Get group element is in.
1200 * @return {OO.ui.GroupElement|null} Group element, null if none
1202 OO
.ui
.Element
.prototype.getElementGroup = function () {
1203 return this.elementGroup
;
1207 * Set group element is in.
1209 * @param {OO.ui.GroupElement|null} group Group element, null if none
1212 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1213 this.elementGroup
= group
;
1218 * Scroll element into view.
1220 * @param {Object} [config={}]
1222 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1223 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1227 * Bind a handler for an event on this.$element
1229 * @deprecated Use jQuery#on instead.
1230 * @param {string} event
1231 * @param {Function} callback
1233 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1234 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1238 * Unbind a handler bound with #offDOMEvent
1240 * @deprecated Use jQuery#off instead.
1241 * @param {string} event
1242 * @param {Function} callback
1244 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1245 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1249 * Container for elements.
1253 * @extends OO.ui.Element
1254 * @mixins OO.EventEmitter
1257 * @param {Object} [config] Configuration options
1259 OO
.ui
.Layout
= function OoUiLayout( config
) {
1260 // Initialize config
1261 config
= config
|| {};
1263 // Parent constructor
1264 OO
.ui
.Layout
.super.call( this, config
);
1266 // Mixin constructors
1267 OO
.EventEmitter
.call( this );
1270 this.$element
.addClass( 'oo-ui-layout' );
1275 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1276 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1279 * User interface control.
1283 * @extends OO.ui.Element
1284 * @mixins OO.EventEmitter
1287 * @param {Object} [config] Configuration options
1288 * @cfg {boolean} [disabled=false] Disable
1290 OO
.ui
.Widget
= function OoUiWidget( config
) {
1291 // Initialize config
1292 config
= $.extend( { disabled
: false }, config
);
1294 // Parent constructor
1295 OO
.ui
.Widget
.super.call( this, config
);
1297 // Mixin constructors
1298 OO
.EventEmitter
.call( this );
1301 this.visible
= true;
1302 this.disabled
= null;
1303 this.wasDisabled
= null;
1306 this.$element
.addClass( 'oo-ui-widget' );
1307 this.setDisabled( !!config
.disabled
);
1312 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1313 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1319 * @param {boolean} disabled Widget is disabled
1324 * @param {boolean} visible Widget is visible
1330 * Check if the widget is disabled.
1332 * @param {boolean} Button is disabled
1334 OO
.ui
.Widget
.prototype.isDisabled = function () {
1335 return this.disabled
;
1339 * Check if widget is visible.
1341 * @return {boolean} Widget is visible
1343 OO
.ui
.Widget
.prototype.isVisible = function () {
1344 return this.visible
;
1348 * Set the disabled state of the widget.
1350 * This should probably change the widgets' appearance and prevent it from being used.
1352 * @param {boolean} disabled Disable widget
1355 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1358 this.disabled
= !!disabled
;
1359 isDisabled
= this.isDisabled();
1360 if ( isDisabled
!== this.wasDisabled
) {
1361 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1362 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1363 this.emit( 'disable', isDisabled
);
1364 this.updateThemeClasses();
1366 this.wasDisabled
= isDisabled
;
1372 * Toggle visibility of widget.
1374 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1378 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1379 show
= show
=== undefined ? !this.visible
: !!show
;
1381 if ( show
!== this.isVisible() ) {
1382 this.visible
= show
;
1383 this.$element
.toggle( show
);
1384 this.emit( 'toggle', show
);
1391 * Update the disabled state, in case of changes in parent widget.
1395 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1396 this.setDisabled( this.disabled
);
1401 * Container for elements in a child frame.
1403 * Use together with OO.ui.WindowManager.
1407 * @extends OO.ui.Element
1408 * @mixins OO.EventEmitter
1410 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1411 * teardown processes are executed when the window is closed.
1413 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1414 * - Window manager begins opening window
1415 * - {@link #getSetupProcess} method is called and its result executed
1416 * - {@link #getReadyProcess} method is called and its result executed
1417 * - Window is now open
1419 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1420 * - Window manager begins closing window
1421 * - {@link #getHoldProcess} method is called and its result executed
1422 * - {@link #getTeardownProcess} method is called and its result executed
1423 * - Window is now closed
1425 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1426 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1427 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1428 * processing can complete. Always assume window processes are executed asychronously. See
1429 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1430 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1432 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1433 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1436 * @param {Object} [config] Configuration options
1437 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1441 OO
.ui
.Window
= function OoUiWindow( config
) {
1442 // Configuration initialization
1443 config
= config
|| {};
1445 // Parent constructor
1446 OO
.ui
.Window
.super.call( this, config
);
1448 // Mixin constructors
1449 OO
.EventEmitter
.call( this );
1452 this.manager
= null;
1453 this.initialized
= false;
1454 this.visible
= false;
1455 this.opening
= null;
1456 this.closing
= null;
1459 this.loading
= null;
1460 this.size
= config
.size
|| this.constructor.static.size
;
1461 this.$frame
= this.$( '<div>' );
1462 this.$overlay
= this.$( '<div>' );
1466 .addClass( 'oo-ui-window' )
1467 .append( this.$frame
, this.$overlay
);
1468 this.$frame
.addClass( 'oo-ui-window-frame' );
1469 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1471 // NOTE: Additional intitialization will occur when #setManager is called
1476 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1477 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1479 /* Static Properties */
1482 * Symbolic name of size.
1484 * Size is used if no size is configured during construction.
1488 * @property {string}
1490 OO
.ui
.Window
.static.size
= 'medium';
1492 /* Static Methods */
1495 * Transplant the CSS styles from as parent document to a frame's document.
1497 * This loops over the style sheets in the parent document, and copies their nodes to the
1498 * frame's document. It then polls the document to see when all styles have loaded, and once they
1499 * have, resolves the promise.
1501 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1502 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1503 * Firefox, where the styles won't load until the iframe becomes visible.
1505 * For details of how we arrived at the strategy used in this function, see #load.
1509 * @param {HTMLDocument} parentDoc Document to transplant styles from
1510 * @param {HTMLDocument} frameDoc Document to transplant styles to
1511 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1512 * @return {jQuery.Promise} Promise resolved when styles have loaded
1514 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1515 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1516 $pollNodes
= $( [] ),
1517 // Fake font-family value
1518 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1519 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1520 deferred
= $.Deferred();
1522 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1523 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1524 if ( styleNode
.disabled
) {
1528 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1529 // External stylesheet; use @import
1530 styleText
= '@import url(' + styleNode
.href
+ ');';
1532 // Internal stylesheet; just copy the text
1533 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1534 // other browsers, so fall back to '' rather than 'undefined'
1535 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1538 // Create a node with a unique ID that we're going to monitor to see when the CSS
1540 if ( styleNode
.oouiFrameTransplantStylesId
) {
1541 // If we're nesting transplantStyles operations and this node already has
1542 // a CSS rule to wait for loading, reuse it
1543 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1545 // Otherwise, create a new ID
1546 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1549 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1550 // The font-family rule will only take effect once the @import finishes
1551 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1554 // Create a node with id=pollNodeId
1555 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1556 .attr( 'id', pollNodeId
)
1557 .appendTo( frameDoc
.body
)
1560 // Add our modified CSS as a <style> tag
1561 newNode
= frameDoc
.createElement( 'style' );
1562 newNode
.textContent
= styleText
;
1563 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1564 frameDoc
.head
.appendChild( newNode
);
1566 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1568 // Poll every 100ms until all external stylesheets have loaded
1569 $pendingPollNodes
= $pollNodes
;
1570 timeoutID
= setTimeout( function pollExternalStylesheets() {
1572 $pendingPollNodes
.length
> 0 &&
1573 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1575 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1578 if ( $pendingPollNodes
.length
=== 0 ) {
1580 if ( timeoutID
!== null ) {
1582 $pollNodes
.remove();
1586 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1589 // ...but give up after a while
1590 if ( timeout
!== 0 ) {
1591 setTimeout( function () {
1593 clearTimeout( timeoutID
);
1595 $pollNodes
.remove();
1598 }, timeout
|| 5000 );
1601 return deferred
.promise();
1607 * Handle mouse down events.
1609 * @param {jQuery.Event} e Mouse down event
1611 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1612 // Prevent clicking on the click-block from stealing focus
1613 if ( e
.target
=== this.$element
[0] ) {
1619 * Check if window has been initialized.
1621 * @return {boolean} Window has been initialized
1623 OO
.ui
.Window
.prototype.isInitialized = function () {
1624 return this.initialized
;
1628 * Check if window is visible.
1630 * @return {boolean} Window is visible
1632 OO
.ui
.Window
.prototype.isVisible = function () {
1633 return this.visible
;
1637 * Check if window is loading.
1639 * @return {boolean} Window is loading
1641 OO
.ui
.Window
.prototype.isLoading = function () {
1642 return this.loading
&& this.loading
.state() === 'pending';
1646 * Check if window is loaded.
1648 * @return {boolean} Window is loaded
1650 OO
.ui
.Window
.prototype.isLoaded = function () {
1651 return this.loading
&& this.loading
.state() === 'resolved';
1655 * Check if window is opening.
1657 * This is a wrapper around OO.ui.WindowManager#isOpening.
1659 * @return {boolean} Window is opening
1661 OO
.ui
.Window
.prototype.isOpening = function () {
1662 return this.manager
.isOpening( this );
1666 * Check if window is closing.
1668 * This is a wrapper around OO.ui.WindowManager#isClosing.
1670 * @return {boolean} Window is closing
1672 OO
.ui
.Window
.prototype.isClosing = function () {
1673 return this.manager
.isClosing( this );
1677 * Check if window is opened.
1679 * This is a wrapper around OO.ui.WindowManager#isOpened.
1681 * @return {boolean} Window is opened
1683 OO
.ui
.Window
.prototype.isOpened = function () {
1684 return this.manager
.isOpened( this );
1688 * Get the window manager.
1690 * @return {OO.ui.WindowManager} Manager of window
1692 OO
.ui
.Window
.prototype.getManager = function () {
1693 return this.manager
;
1697 * Get the window size.
1699 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1701 OO
.ui
.Window
.prototype.getSize = function () {
1706 * Get the height of the dialog contents.
1708 * @return {number} Content height
1710 OO
.ui
.Window
.prototype.getContentHeight = function () {
1711 // Temporarily resize the frame so getBodyHeight() can use scrollHeight measurements
1712 var bodyHeight
, oldHeight
= this.$frame
[0].style
.height
;
1713 this.$frame
[0].style
.height
= '1px';
1714 bodyHeight
= this.getBodyHeight();
1715 this.$frame
[0].style
.height
= oldHeight
;
1718 // Add buffer for border
1719 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1720 // Use combined heights of children
1721 ( this.$head
.outerHeight( true ) + bodyHeight
+ this.$foot
.outerHeight( true ) )
1726 * Get the height of the dialog contents.
1728 * When this function is called, the dialog will temporarily have been resized
1729 * to height=1px, so .scrollHeight measurements can be taken accurately.
1731 * @return {number} Height of content
1733 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1734 return this.$body
[0].scrollHeight
;
1738 * Get the directionality of the frame
1740 * @return {string} Directionality, 'ltr' or 'rtl'
1742 OO
.ui
.Window
.prototype.getDir = function () {
1747 * Get a process for setting up a window for use.
1749 * Each time the window is opened this process will set it up for use in a particular context, based
1750 * on the `data` argument.
1752 * When you override this method, you can add additional setup steps to the process the parent
1753 * method provides using the 'first' and 'next' methods.
1756 * @param {Object} [data] Window opening data
1757 * @return {OO.ui.Process} Setup process
1759 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1760 return new OO
.ui
.Process();
1764 * Get a process for readying a window for use.
1766 * Each time the window is open and setup, this process will ready it up for use in a particular
1767 * context, based on the `data` argument.
1769 * When you override this method, you can add additional setup steps to the process the parent
1770 * method provides using the 'first' and 'next' methods.
1773 * @param {Object} [data] Window opening data
1774 * @return {OO.ui.Process} Setup process
1776 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1777 return new OO
.ui
.Process();
1781 * Get a process for holding a window from use.
1783 * Each time the window is closed, this process will hold it from use in a particular context, based
1784 * on the `data` argument.
1786 * When you override this method, you can add additional setup steps to the process the parent
1787 * method provides using the 'first' and 'next' methods.
1790 * @param {Object} [data] Window closing data
1791 * @return {OO.ui.Process} Hold process
1793 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1794 return new OO
.ui
.Process();
1798 * Get a process for tearing down a window after use.
1800 * Each time the window is closed this process will tear it down and do something with the user's
1801 * interactions within the window, based on the `data` argument.
1803 * When you override this method, you can add additional teardown steps to the process the parent
1804 * method provides using the 'first' and 'next' methods.
1807 * @param {Object} [data] Window closing data
1808 * @return {OO.ui.Process} Teardown process
1810 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1811 return new OO
.ui
.Process();
1815 * Toggle visibility of window.
1817 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1818 * instead of display.
1820 * @param {boolean} [show] Make window visible, omit to toggle visibility
1824 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1825 show
= show
=== undefined ? !this.visible
: !!show
;
1827 if ( show
!== this.isVisible() ) {
1828 this.visible
= show
;
1830 if ( this.isolated
&& !this.isLoaded() ) {
1831 // Hide the window using visibility instead of display until loading is complete
1832 // Can't use display: none; because that prevents the iframe from loading in Firefox
1833 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1835 this.$element
.toggle( show
).css( 'visibility', '' );
1837 this.emit( 'toggle', show
);
1844 * Set the window manager.
1846 * This must be called before initialize. Calling it more than once will cause an error.
1848 * @param {OO.ui.WindowManager} manager Manager for this window
1849 * @throws {Error} If called more than once
1852 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1853 if ( this.manager
) {
1854 throw new Error( 'Cannot set window manager, window already has a manager' );
1858 this.manager
= manager
;
1859 this.isolated
= manager
.shouldIsolate();
1862 if ( this.isolated
) {
1863 this.$iframe
= this.$( '<iframe>' );
1864 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1865 this.$frame
.append( this.$iframe
);
1866 this.$ = function () {
1867 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1869 // WARNING: Do not use this.$ again until #initialize is called
1871 this.$content
= this.$( '<div>' );
1872 this.$document
= $( this.getElementDocument() );
1873 this.$content
.addClass( 'oo-ui-window-content' );
1874 this.$frame
.append( this.$content
);
1876 this.toggle( false );
1878 // Figure out directionality:
1879 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1885 * Set the window size.
1887 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1890 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1892 this.manager
.updateWindowSize( this );
1897 * Set window dimensions.
1899 * Properties are applied to the frame container.
1901 * @param {Object} dim CSS dimension properties
1902 * @param {string|number} [dim.width] Width
1903 * @param {string|number} [dim.minWidth] Minimum width
1904 * @param {string|number} [dim.maxWidth] Maximum width
1905 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1906 * @param {string|number} [dim.minWidth] Minimum height
1907 * @param {string|number} [dim.maxWidth] Maximum height
1910 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1911 // Apply width before height so height is not based on wrapping content using the wrong width
1913 width
: dim
.width
|| '',
1914 minWidth
: dim
.minWidth
|| '',
1915 maxWidth
: dim
.maxWidth
|| ''
1918 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1919 minHeight
: dim
.minHeight
|| '',
1920 maxHeight
: dim
.maxHeight
|| ''
1926 * Initialize window contents.
1928 * The first time the window is opened, #initialize is called when it's safe to begin populating
1929 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1931 * Once this method is called, this.$ can be used to create elements within the frame.
1933 * @throws {Error} If not attached to a manager
1936 OO
.ui
.Window
.prototype.initialize = function () {
1937 if ( !this.manager
) {
1938 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1942 this.$head
= this.$( '<div>' );
1943 this.$body
= this.$( '<div>' );
1944 this.$foot
= this.$( '<div>' );
1945 this.$innerOverlay
= this.$( '<div>' );
1948 this.$element
.on( 'mousedown', this.onMouseDown
.bind( this ) );
1951 this.$head
.addClass( 'oo-ui-window-head' );
1952 this.$body
.addClass( 'oo-ui-window-body' );
1953 this.$foot
.addClass( 'oo-ui-window-foot' );
1954 this.$innerOverlay
.addClass( 'oo-ui-window-inner-overlay' );
1955 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$innerOverlay
);
1963 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1964 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1966 * @param {Object} [data] Window opening data
1967 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1968 * first argument will be a promise which will be resolved when the window begins closing
1970 OO
.ui
.Window
.prototype.open = function ( data
) {
1971 return this.manager
.openWindow( this, data
);
1977 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1978 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1980 * @param {Object} [data] Window closing data
1981 * @return {jQuery.Promise} Promise resolved when window is closed
1983 OO
.ui
.Window
.prototype.close = function ( data
) {
1984 return this.manager
.closeWindow( this, data
);
1990 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1993 * @param {Object} [data] Window opening data
1994 * @return {jQuery.Promise} Promise resolved when window is setup
1996 OO
.ui
.Window
.prototype.setup = function ( data
) {
1998 deferred
= $.Deferred();
2000 this.$element
.show();
2001 this.visible
= true;
2002 this.getSetupProcess( data
).execute().done( function () {
2003 // Force redraw by asking the browser to measure the elements' widths
2004 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
2005 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
2009 return deferred
.promise();
2015 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
2018 * @param {Object} [data] Window opening data
2019 * @return {jQuery.Promise} Promise resolved when window is ready
2021 OO
.ui
.Window
.prototype.ready = function ( data
) {
2023 deferred
= $.Deferred();
2025 this.$content
.focus();
2026 this.getReadyProcess( data
).execute().done( function () {
2027 // Force redraw by asking the browser to measure the elements' widths
2028 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
2029 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
2033 return deferred
.promise();
2039 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2042 * @param {Object} [data] Window closing data
2043 * @return {jQuery.Promise} Promise resolved when window is held
2045 OO
.ui
.Window
.prototype.hold = function ( data
) {
2047 deferred
= $.Deferred();
2049 this.getHoldProcess( data
).execute().done( function () {
2050 // Get the focused element within the window's content
2051 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
2053 // Blur the focused element
2054 if ( $focus
.length
) {
2058 // Force redraw by asking the browser to measure the elements' widths
2059 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
2060 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
2064 return deferred
.promise();
2070 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
2073 * @param {Object} [data] Window closing data
2074 * @return {jQuery.Promise} Promise resolved when window is torn down
2076 OO
.ui
.Window
.prototype.teardown = function ( data
) {
2078 deferred
= $.Deferred();
2080 this.getTeardownProcess( data
).execute().done( function () {
2081 // Force redraw by asking the browser to measure the elements' widths
2082 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
2083 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
2084 win
.$element
.hide();
2085 win
.visible
= false;
2089 return deferred
.promise();
2093 * Load the frame contents.
2095 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
2096 * promise will be resolved. Calling while loading will return a promise but not trigger a new
2097 * loading cycle. Calling after loading is complete will return a promise that's already been
2100 * Sounds simple right? Read on...
2102 * When you create a dynamic iframe using open/write/close, the window.load event for the
2103 * iframe is triggered when you call close, and there's no further load event to indicate that
2104 * everything is actually loaded.
2106 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
2107 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
2108 * are added to document.styleSheets immediately, and the only way you can determine whether they've
2109 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
2110 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
2112 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
2113 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
2114 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
2115 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
2116 * accessing .cssRules is allowed.
2118 * However, now that we control the styles we're injecting, we might as well do away with
2119 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
2120 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
2121 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
2122 * font-family rule is not applied until after the `@import` finishes.
2124 * All this stylesheet injection and polling magic is in #transplantStyles.
2126 * @return {jQuery.Promise} Promise resolved when loading is complete
2129 OO
.ui
.Window
.prototype.load = function () {
2130 var sub
, doc
, loading
,
2133 // Non-isolated windows are already "loaded"
2134 if ( !this.loading
&& !this.isolated
) {
2135 this.loading
= $.Deferred().resolve();
2137 // Set initialized state after so sub-classes aren't confused by it being set by calling
2138 // their parent initialize method
2139 this.initialized
= true;
2142 // Return existing promise if already loading or loaded
2143 if ( this.loading
) {
2144 return this.loading
.promise();
2148 loading
= this.loading
= $.Deferred();
2149 sub
= this.$iframe
.prop( 'contentWindow' );
2152 // Initialize contents
2157 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2158 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2159 '<div class="oo-ui-window-content"></div>' +
2166 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$iframe
);
2167 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2168 this.$document
= this.$( doc
);
2171 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2172 .always( function () {
2173 // Initialize isolated windows
2175 // Set initialized state after so sub-classes aren't confused by it being set by calling
2176 // their parent initialize method
2177 win
.initialized
= true;
2178 // Undo the visibility: hidden; hack and apply display: none;
2179 // We can do this safely now that the iframe has initialized
2180 // (don't do this from within #initialize because it has to happen
2181 // after the all subclasses have been handled as well).
2182 win
.toggle( win
.isVisible() );
2187 return loading
.promise();
2191 * Base class for all dialogs.
2194 * - Manage the window (open and close, etc.).
2195 * - Store the internal name and display title.
2196 * - A stack to track one or more pending actions.
2197 * - Manage a set of actions that can be performed.
2198 * - Configure and create action widgets.
2201 * - Close the dialog with Escape key.
2202 * - Visually lock the dialog while an action is in
2203 * progress (aka "pending").
2205 * Subclass responsibilities:
2206 * - Display the title somewhere.
2207 * - Add content to the dialog.
2208 * - Provide a UI to close the dialog.
2209 * - Display the action widgets somewhere.
2213 * @extends OO.ui.Window
2214 * @mixins OO.ui.PendingElement
2217 * @param {Object} [config] Configuration options
2219 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2220 // Parent constructor
2221 OO
.ui
.Dialog
.super.call( this, config
);
2223 // Mixin constructors
2224 OO
.ui
.PendingElement
.call( this );
2227 this.actions
= new OO
.ui
.ActionSet();
2228 this.attachedActions
= [];
2229 this.currentAction
= null;
2232 this.actions
.connect( this, {
2233 click
: 'onActionClick',
2234 resize
: 'onActionResize',
2235 change
: 'onActionsChange'
2240 .addClass( 'oo-ui-dialog' )
2241 .attr( 'role', 'dialog' );
2246 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2247 OO
.mixinClass( OO
.ui
.Dialog
, OO
.ui
.PendingElement
);
2249 /* Static Properties */
2252 * Symbolic name of dialog.
2257 * @property {string}
2259 OO
.ui
.Dialog
.static.name
= '';
2267 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2269 OO
.ui
.Dialog
.static.title
= '';
2272 * List of OO.ui.ActionWidget configuration options.
2276 * @property {Object[]}
2278 OO
.ui
.Dialog
.static.actions
= [];
2281 * Close dialog when the escape key is pressed.
2286 * @property {boolean}
2288 OO
.ui
.Dialog
.static.escapable
= true;
2293 * Handle frame document key down events.
2295 * @param {jQuery.Event} e Key down event
2297 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2298 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2305 * Handle action resized events.
2307 * @param {OO.ui.ActionWidget} action Action that was resized
2309 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2310 // Override in subclass
2314 * Handle action click events.
2316 * @param {OO.ui.ActionWidget} action Action that was clicked
2318 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2319 if ( !this.isPending() ) {
2320 this.currentAction
= action
;
2321 this.executeAction( action
.getAction() );
2326 * Handle actions change event.
2328 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2329 this.detachActions();
2330 if ( !this.isClosing() ) {
2331 this.attachActions();
2336 * Get set of actions.
2338 * @return {OO.ui.ActionSet}
2340 OO
.ui
.Dialog
.prototype.getActions = function () {
2341 return this.actions
;
2345 * Get a process for taking action.
2347 * When you override this method, you can add additional accept steps to the process the parent
2348 * method provides using the 'first' and 'next' methods.
2351 * @param {string} [action] Symbolic name of action
2352 * @return {OO.ui.Process} Action process
2354 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2355 return new OO
.ui
.Process()
2356 .next( function () {
2358 // An empty action always closes the dialog without data, which should always be
2359 // safe and make no changes
2368 * @param {Object} [data] Dialog opening data
2369 * @param {jQuery|string|Function|null} [data.title] Dialog title, omit to use #static-title
2370 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2371 * action item, omit to use #static-actions
2373 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2377 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2378 .next( function () {
2381 config
= this.constructor.static,
2382 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2384 this.title
.setLabel(
2385 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2387 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2389 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2392 this.actions
.add( items
);
2399 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2401 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2402 .first( function () {
2403 this.actions
.clear();
2404 this.currentAction
= null;
2411 OO
.ui
.Dialog
.prototype.initialize = function () {
2413 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2416 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2419 if ( this.constructor.static.escapable
) {
2420 this.$document
.on( 'keydown', this.onDocumentKeyDown
.bind( this ) );
2424 this.$content
.addClass( 'oo-ui-dialog-content' );
2425 this.setPendingElement( this.$head
);
2429 * Attach action actions.
2431 OO
.ui
.Dialog
.prototype.attachActions = function () {
2432 // Remember the list of potentially attached actions
2433 this.attachedActions
= this.actions
.get();
2437 * Detach action actions.
2441 OO
.ui
.Dialog
.prototype.detachActions = function () {
2444 // Detach all actions that may have been previously attached
2445 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2446 this.attachedActions
[i
].$element
.detach();
2448 this.attachedActions
= [];
2452 * Execute an action.
2454 * @param {string} action Symbolic name of action to execute
2455 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2457 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2459 return this.getActionProcess( action
).execute()
2460 .always( this.popPending
.bind( this ) );
2464 * Collection of windows.
2467 * @extends OO.ui.Element
2468 * @mixins OO.EventEmitter
2470 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2471 * already opening or opened, the current window will be closed without data. Empty closing data
2472 * should always result in the window being closed without causing constructive or destructive
2475 * As a window is opened and closed, it passes through several stages and the manager emits several
2476 * corresponding events.
2478 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2479 * - {@link #event-opening} is emitted with `opening` promise
2480 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2481 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2482 * - `setup` progress notification is emitted from opening promise
2483 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2484 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2485 * - `ready` progress notification is emitted from opening promise
2486 * - `opening` promise is resolved with `opened` promise
2487 * - Window is now open
2489 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2490 * - `opened` promise is resolved with `closing` promise
2491 * - {@link #event-closing} is emitted with `closing` promise
2492 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2493 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2494 * - `hold` progress notification is emitted from opening promise
2495 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2496 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2497 * - `teardown` progress notification is emitted from opening promise
2498 * - Closing promise is resolved
2499 * - Window is now closed
2502 * @param {Object} [config] Configuration options
2503 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2504 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2505 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2507 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2508 // Configuration initialization
2509 config
= config
|| {};
2511 // Parent constructor
2512 OO
.ui
.WindowManager
.super.call( this, config
);
2514 // Mixin constructors
2515 OO
.EventEmitter
.call( this );
2518 this.factory
= config
.factory
;
2519 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2520 this.isolate
= !!config
.isolate
;
2522 this.opening
= null;
2524 this.closing
= null;
2525 this.preparingToOpen
= null;
2526 this.preparingToClose
= null;
2528 this.currentWindow
= null;
2529 this.$ariaHidden
= null;
2530 this.requestedSize
= null;
2531 this.onWindowResizeTimeout
= null;
2532 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
2533 this.afterWindowResizeHandler
= this.afterWindowResize
.bind( this );
2534 this.onWindowMouseWheelHandler
= this.onWindowMouseWheel
.bind( this );
2535 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
2539 .addClass( 'oo-ui-windowManager' )
2540 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2545 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2546 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2551 * Window is opening.
2553 * Fired when the window begins to be opened.
2556 * @param {OO.ui.Window} win Window that's being opened
2557 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2558 * resolved the first argument will be a promise which will be resolved when the window begins
2559 * closing, the second argument will be the opening data; progress notifications will be fired on
2560 * the promise for `setup` and `ready` when those processes are completed respectively.
2561 * @param {Object} data Window opening data
2565 * Window is closing.
2567 * Fired when the window begins to be closed.
2570 * @param {OO.ui.Window} win Window that's being closed
2571 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2572 * is resolved the first argument will be a the closing data; progress notifications will be fired
2573 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2574 * @param {Object} data Window closing data
2578 * Window was resized.
2581 * @param {OO.ui.Window} win Window that was resized
2584 /* Static Properties */
2587 * Map of symbolic size names and CSS properties.
2591 * @property {Object}
2593 OO
.ui
.WindowManager
.static.sizes
= {
2604 // These can be non-numeric because they are never used in calculations
2611 * Symbolic name of default size.
2613 * Default size is used if the window's requested size is not recognized.
2617 * @property {string}
2619 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2624 * Handle window resize events.
2626 * @param {jQuery.Event} e Window resize event
2628 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2629 clearTimeout( this.onWindowResizeTimeout
);
2630 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2634 * Handle window resize events.
2636 * @param {jQuery.Event} e Window resize event
2638 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2639 if ( this.currentWindow
) {
2640 this.updateWindowSize( this.currentWindow
);
2645 * Handle window mouse wheel events.
2647 * @param {jQuery.Event} e Mouse wheel event
2649 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function ( e
) {
2650 // Kill all events in the parent window if the child window is isolated,
2651 // or if the event didn't come from the child window
2652 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2656 * Handle document key down events.
2658 * @param {jQuery.Event} e Key down event
2660 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2661 switch ( e
.which
) {
2662 case OO
.ui
.Keys
.PAGEUP
:
2663 case OO
.ui
.Keys
.PAGEDOWN
:
2664 case OO
.ui
.Keys
.END
:
2665 case OO
.ui
.Keys
.HOME
:
2666 case OO
.ui
.Keys
.LEFT
:
2668 case OO
.ui
.Keys
.RIGHT
:
2669 case OO
.ui
.Keys
.DOWN
:
2670 // Kill all events in the parent window if the child window is isolated,
2671 // or if the event didn't come from the child window
2672 return !( this.shouldIsolate() || !$.contains( this.getCurrentWindow().$frame
[0], e
.target
) );
2677 * Check if window is opening.
2679 * @return {boolean} Window is opening
2681 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2682 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2686 * Check if window is closing.
2688 * @return {boolean} Window is closing
2690 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2691 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2695 * Check if window is opened.
2697 * @return {boolean} Window is opened
2699 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2700 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2704 * Check if window contents should be isolated.
2706 * Window content isolation is done using inline frames.
2708 * @return {boolean} Window contents should be isolated
2710 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2711 return this.isolate
;
2715 * Check if a window is being managed.
2717 * @param {OO.ui.Window} win Window to check
2718 * @return {boolean} Window is being managed
2720 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2723 for ( name
in this.windows
) {
2724 if ( this.windows
[name
] === win
) {
2733 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2735 * @param {OO.ui.Window} win Window being opened
2736 * @param {Object} [data] Window opening data
2737 * @return {number} Milliseconds to wait
2739 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2744 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2746 * @param {OO.ui.Window} win Window being opened
2747 * @param {Object} [data] Window opening data
2748 * @return {number} Milliseconds to wait
2750 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2755 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2757 * @param {OO.ui.Window} win Window being closed
2758 * @param {Object} [data] Window closing data
2759 * @return {number} Milliseconds to wait
2761 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2766 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2768 * @param {OO.ui.Window} win Window being closed
2769 * @param {Object} [data] Window closing data
2770 * @return {number} Milliseconds to wait
2772 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2773 return this.modal
? 250 : 0;
2777 * Get managed window by symbolic name.
2779 * If window is not yet instantiated, it will be instantiated and added automatically.
2781 * @param {string} name Symbolic window name
2782 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2783 * @throws {Error} If the symbolic name is unrecognized by the factory
2784 * @throws {Error} If the symbolic name unrecognized as a managed window
2786 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2787 var deferred
= $.Deferred(),
2788 win
= this.windows
[name
];
2790 if ( !( win
instanceof OO
.ui
.Window
) ) {
2791 if ( this.factory
) {
2792 if ( !this.factory
.lookup( name
) ) {
2793 deferred
.reject( new OO
.ui
.Error(
2794 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2797 win
= this.factory
.create( name
, this, { $: this.$ } );
2798 this.addWindows( [ win
] );
2799 deferred
.resolve( win
);
2802 deferred
.reject( new OO
.ui
.Error(
2803 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2807 deferred
.resolve( win
);
2810 return deferred
.promise();
2814 * Get current window.
2816 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2818 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2819 return this.currentWindow
;
2825 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2826 * @param {Object} [data] Window opening data
2827 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2828 * for more details about the `opening` promise
2831 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2834 opening
= $.Deferred();
2836 // Argument handling
2837 if ( typeof win
=== 'string' ) {
2838 return this.getWindow( win
).then( function ( win
) {
2839 return manager
.openWindow( win
, data
);
2844 if ( !this.hasWindow( win
) ) {
2845 opening
.reject( new OO
.ui
.Error(
2846 'Cannot open window: window is not attached to manager'
2848 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2849 opening
.reject( new OO
.ui
.Error(
2850 'Cannot open window: another window is opening or open'
2855 if ( opening
.state() !== 'rejected' ) {
2856 // Begin loading the window if it's not loading or loaded already - may take noticable time
2857 // and we want to do this in paralell with any other preparatory actions
2858 if ( !win
.isLoading() && !win
.isLoaded() ) {
2859 // Finish initializing the window (must be done after manager is attached to DOM)
2860 win
.setManager( this );
2861 preparing
.push( win
.load() );
2864 if ( this.closing
) {
2865 // If a window is currently closing, wait for it to complete
2866 preparing
.push( this.closing
);
2869 this.preparingToOpen
= $.when
.apply( $, preparing
);
2870 // Ensure handlers get called after preparingToOpen is set
2871 this.preparingToOpen
.done( function () {
2872 if ( manager
.modal
) {
2873 manager
.toggleGlobalEvents( true );
2874 manager
.toggleAriaIsolation( true );
2876 manager
.currentWindow
= win
;
2877 manager
.opening
= opening
;
2878 manager
.preparingToOpen
= null;
2879 manager
.emit( 'opening', win
, opening
, data
);
2880 setTimeout( function () {
2881 win
.setup( data
).then( function () {
2882 manager
.updateWindowSize( win
);
2883 manager
.opening
.notify( { state
: 'setup' } );
2884 setTimeout( function () {
2885 win
.ready( data
).then( function () {
2886 manager
.opening
.notify( { state
: 'ready' } );
2887 manager
.opening
= null;
2888 manager
.opened
= $.Deferred();
2889 opening
.resolve( manager
.opened
.promise(), data
);
2891 }, manager
.getReadyDelay() );
2893 }, manager
.getSetupDelay() );
2897 return opening
.promise();
2903 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2904 * @param {Object} [data] Window closing data
2905 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2906 * for more details about the `closing` promise
2907 * @throws {Error} If no window by that name is being managed
2910 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2913 closing
= $.Deferred(),
2916 // Argument handling
2917 if ( typeof win
=== 'string' ) {
2918 win
= this.windows
[win
];
2919 } else if ( !this.hasWindow( win
) ) {
2925 closing
.reject( new OO
.ui
.Error(
2926 'Cannot close window: window is not attached to manager'
2928 } else if ( win
!== this.currentWindow
) {
2929 closing
.reject( new OO
.ui
.Error(
2930 'Cannot close window: window already closed with different data'
2932 } else if ( this.preparingToClose
|| this.closing
) {
2933 closing
.reject( new OO
.ui
.Error(
2934 'Cannot close window: window already closing with different data'
2939 if ( closing
.state() !== 'rejected' ) {
2940 if ( this.opening
) {
2941 // If the window is currently opening, close it when it's done
2942 preparing
.push( this.opening
);
2945 this.preparingToClose
= $.when
.apply( $, preparing
);
2946 // Ensure handlers get called after preparingToClose is set
2947 this.preparingToClose
.done( function () {
2948 manager
.closing
= closing
;
2949 manager
.preparingToClose
= null;
2950 manager
.emit( 'closing', win
, closing
, data
);
2951 opened
= manager
.opened
;
2952 manager
.opened
= null;
2953 opened
.resolve( closing
.promise(), data
);
2954 setTimeout( function () {
2955 win
.hold( data
).then( function () {
2956 closing
.notify( { state
: 'hold' } );
2957 setTimeout( function () {
2958 win
.teardown( data
).then( function () {
2959 closing
.notify( { state
: 'teardown' } );
2960 if ( manager
.modal
) {
2961 manager
.toggleGlobalEvents( false );
2962 manager
.toggleAriaIsolation( false );
2964 manager
.closing
= null;
2965 manager
.currentWindow
= null;
2966 closing
.resolve( data
);
2968 }, manager
.getTeardownDelay() );
2970 }, manager
.getHoldDelay() );
2974 return closing
.promise();
2980 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2981 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2982 * a statically configured symbolic name
2984 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2985 var i
, len
, win
, name
, list
;
2987 if ( $.isArray( windows
) ) {
2988 // Convert to map of windows by looking up symbolic names from static configuration
2990 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2991 name
= windows
[i
].constructor.static.name
;
2992 if ( typeof name
!== 'string' ) {
2993 throw new Error( 'Cannot add window' );
2995 list
[name
] = windows
[i
];
2997 } else if ( $.isPlainObject( windows
) ) {
3002 for ( name
in list
) {
3004 this.windows
[name
] = win
;
3005 this.$element
.append( win
.$element
);
3012 * Windows will be closed before they are removed.
3014 * @param {string} name Symbolic name of window to remove
3015 * @return {jQuery.Promise} Promise resolved when window is closed and removed
3016 * @throws {Error} If windows being removed are not being managed
3018 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
3019 var i
, len
, win
, name
,
3022 cleanup = function ( name
, win
) {
3023 delete manager
.windows
[name
];
3024 win
.$element
.detach();
3027 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
3029 win
= this.windows
[name
];
3031 throw new Error( 'Cannot remove window' );
3033 promises
.push( this.closeWindow( name
).then( cleanup
.bind( null, name
, win
) ) );
3036 return $.when
.apply( $, promises
);
3040 * Remove all windows.
3042 * Windows will be closed before they are removed.
3044 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
3046 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
3047 return this.removeWindows( Object
.keys( this.windows
) );
3053 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
3057 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
3058 // Bypass for non-current, and thus invisible, windows
3059 if ( win
!== this.currentWindow
) {
3063 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
3064 sizes
= this.constructor.static.sizes
,
3065 size
= win
.getSize();
3067 if ( !sizes
[size
] ) {
3068 size
= this.constructor.static.defaultSize
;
3070 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
3074 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
3075 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
3076 win
.setDimensions( sizes
[size
] );
3078 this.emit( 'resize', win
);
3084 * Bind or unbind global events for scrolling.
3086 * @param {boolean} [on] Bind global events
3089 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
3090 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
3093 if ( !this.globalEvents
) {
3094 this.$( this.getElementDocument() ).on( {
3095 // Prevent scrolling by keys in top-level window
3096 keydown
: this.onDocumentKeyDownHandler
3098 this.$( this.getElementWindow() ).on( {
3099 // Prevent scrolling by wheel in top-level window
3100 mousewheel
: this.onWindowMouseWheelHandler
,
3101 // Start listening for top-level window dimension changes
3102 'orientationchange resize': this.onWindowResizeHandler
3104 this.globalEvents
= true;
3106 } else if ( this.globalEvents
) {
3107 // Unbind global events
3108 this.$( this.getElementDocument() ).off( {
3109 // Allow scrolling by keys in top-level window
3110 keydown
: this.onDocumentKeyDownHandler
3112 this.$( this.getElementWindow() ).off( {
3113 // Allow scrolling by wheel in top-level window
3114 mousewheel
: this.onWindowMouseWheelHandler
,
3115 // Stop listening for top-level window dimension changes
3116 'orientationchange resize': this.onWindowResizeHandler
3118 this.globalEvents
= false;
3125 * Toggle screen reader visibility of content other than the window manager.
3127 * @param {boolean} [isolate] Make only the window manager visible to screen readers
3130 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3131 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3134 if ( !this.$ariaHidden
) {
3135 // Hide everything other than the window manager from screen readers
3136 this.$ariaHidden
= $( 'body' )
3138 .not( this.$element
.parentsUntil( 'body' ).last() )
3139 .attr( 'aria-hidden', '' );
3141 } else if ( this.$ariaHidden
) {
3142 // Restore screen reader visiblity
3143 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3144 this.$ariaHidden
= null;
3151 * Destroy window manager.
3153 * Windows will not be closed, only removed from the DOM.
3155 OO
.ui
.WindowManager
.prototype.destroy = function () {
3156 this.toggleGlobalEvents( false );
3157 this.toggleAriaIsolation( false );
3158 this.$element
.remove();
3166 * @param {string|jQuery} message Description of error
3167 * @param {Object} [config] Configuration options
3168 * @cfg {boolean} [recoverable=true] Error is recoverable
3170 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3171 // Configuration initialization
3172 config
= config
|| {};
3175 this.message
= message
instanceof jQuery
? message
: String( message
);
3176 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3181 OO
.initClass( OO
.ui
.Error
);
3186 * Check if error can be recovered from.
3188 * @return {boolean} Error is recoverable
3190 OO
.ui
.Error
.prototype.isRecoverable = function () {
3191 return this.recoverable
;
3195 * Get error message as DOM nodes.
3197 * @return {jQuery} Error message in DOM nodes
3199 OO
.ui
.Error
.prototype.getMessage = function () {
3200 return this.message
instanceof jQuery
?
3201 this.message
.clone() :
3202 $( '<div>' ).text( this.message
).contents();
3206 * Get error message as text.
3208 * @return {string} Error message
3210 OO
.ui
.Error
.prototype.getMessageText = function () {
3211 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3215 * A list of functions, called in sequence.
3217 * If a function added to a process returns boolean false the process will stop; if it returns an
3218 * object with a `promise` method the process will use the promise to either continue to the next
3219 * step when the promise is resolved or stop when the promise is rejected.
3224 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3225 * call, see #createStep for more information
3226 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3228 * @return {Object} Step object, with `callback` and `context` properties
3230 OO
.ui
.Process = function ( step
, context
) {
3235 if ( step
!== undefined ) {
3236 this.next( step
, context
);
3242 OO
.initClass( OO
.ui
.Process
);
3247 * Start the process.
3249 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3250 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3251 * process, the remaining steps will not be taken
3253 OO
.ui
.Process
.prototype.execute = function () {
3254 var i
, len
, promise
;
3257 * Continue execution.
3260 * @param {Array} step A function and the context it should be called in
3261 * @return {Function} Function that continues the process
3263 function proceed( step
) {
3264 return function () {
3265 // Execute step in the correct context
3267 result
= step
.callback
.call( step
.context
);
3269 if ( result
=== false ) {
3270 // Use rejected promise for boolean false results
3271 return $.Deferred().reject( [] ).promise();
3273 if ( typeof result
=== 'number' ) {
3275 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3277 // Use a delayed promise for numbers, expecting them to be in milliseconds
3278 deferred
= $.Deferred();
3279 setTimeout( deferred
.resolve
, result
);
3280 return deferred
.promise();
3282 if ( result
instanceof OO
.ui
.Error
) {
3283 // Use rejected promise for error
3284 return $.Deferred().reject( [ result
] ).promise();
3286 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3287 // Use rejected promise for list of errors
3288 return $.Deferred().reject( result
).promise();
3290 // Duck-type the object to see if it can produce a promise
3291 if ( result
&& $.isFunction( result
.promise
) ) {
3292 // Use a promise generated from the result
3293 return result
.promise();
3295 // Use resolved promise for other results
3296 return $.Deferred().resolve().promise();
3300 if ( this.steps
.length
) {
3301 // Generate a chain reaction of promises
3302 promise
= proceed( this.steps
[0] )();
3303 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3304 promise
= promise
.then( proceed( this.steps
[i
] ) );
3307 promise
= $.Deferred().resolve().promise();
3314 * Create a process step.
3317 * @param {number|jQuery.Promise|Function} step
3319 * - Number of milliseconds to wait; or
3320 * - Promise to wait to be resolved; or
3321 * - Function to execute
3322 * - If it returns boolean false the process will stop
3323 * - If it returns an object with a `promise` method the process will use the promise to either
3324 * continue to the next step when the promise is resolved or stop when the promise is rejected
3325 * - If it returns a number, the process will wait for that number of milliseconds before
3327 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3329 * @return {Object} Step object, with `callback` and `context` properties
3331 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3332 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3334 callback: function () {
3340 if ( $.isFunction( step
) ) {
3346 throw new Error( 'Cannot create process step: number, promise or function expected' );
3350 * Add step to the beginning of the process.
3352 * @inheritdoc #createStep
3353 * @return {OO.ui.Process} this
3356 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3357 this.steps
.unshift( this.createStep( step
, context
) );
3362 * Add step to the end of the process.
3364 * @inheritdoc #createStep
3365 * @return {OO.ui.Process} this
3368 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3369 this.steps
.push( this.createStep( step
, context
) );
3374 * Factory for tools.
3377 * @extends OO.Factory
3380 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3381 // Parent constructor
3382 OO
.ui
.ToolFactory
.super.call( this );
3387 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3392 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3393 var i
, len
, included
, promoted
, demoted
,
3397 // Collect included and not excluded tools
3398 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3401 promoted
= this.extract( promote
, used
);
3402 demoted
= this.extract( demote
, used
);
3405 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3406 if ( !used
[included
[i
]] ) {
3407 auto
.push( included
[i
] );
3411 return promoted
.concat( auto
).concat( demoted
);
3415 * Get a flat list of names from a list of names or groups.
3417 * Tools can be specified in the following ways:
3419 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3420 * - All tools in a group: `{ group: 'group-name' }`
3421 * - All tools: `'*'`
3424 * @param {Array|string} collection List of tools
3425 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3426 * names will be added as properties
3427 * @return {string[]} List of extracted names
3429 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3430 var i
, len
, item
, name
, tool
,
3433 if ( collection
=== '*' ) {
3434 for ( name
in this.registry
) {
3435 tool
= this.registry
[name
];
3437 // Only add tools by group name when auto-add is enabled
3438 tool
.static.autoAddToCatchall
&&
3439 // Exclude already used tools
3440 ( !used
|| !used
[name
] )
3448 } else if ( $.isArray( collection
) ) {
3449 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3450 item
= collection
[i
];
3451 // Allow plain strings as shorthand for named tools
3452 if ( typeof item
=== 'string' ) {
3453 item
= { name
: item
};
3455 if ( OO
.isPlainObject( item
) ) {
3457 for ( name
in this.registry
) {
3458 tool
= this.registry
[name
];
3460 // Include tools with matching group
3461 tool
.static.group
=== item
.group
&&
3462 // Only add tools by group name when auto-add is enabled
3463 tool
.static.autoAddToGroup
&&
3464 // Exclude already used tools
3465 ( !used
|| !used
[name
] )
3473 // Include tools with matching name and exclude already used tools
3474 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3475 names
.push( item
.name
);
3477 used
[item
.name
] = true;
3487 * Factory for tool groups.
3490 * @extends OO.Factory
3493 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3494 // Parent constructor
3495 OO
.Factory
.call( this );
3498 defaultClasses
= this.constructor.static.getDefaultClasses();
3500 // Register default toolgroups
3501 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3502 this.register( defaultClasses
[i
] );
3508 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3510 /* Static Methods */
3513 * Get a default set of classes to be registered on construction
3515 * @return {Function[]} Default classes
3517 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3520 OO
.ui
.ListToolGroup
,
3532 * @param {Object} [config] Configuration options
3534 OO
.ui
.Theme
= function OoUiTheme( config
) {
3535 // Initialize config
3536 config
= config
|| {};
3541 OO
.initClass( OO
.ui
.Theme
);
3546 * Get a list of classes to be applied to a widget.
3548 * @localdoc The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or
3549 * removes, otherwise state transitions will not work properly.
3551 * @param {OO.ui.Element} element Element for which to get classes
3552 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3554 OO
.ui
.Theme
.prototype.getElementClasses = function ( /* element */ ) {
3555 return { on
: [], off
: [] };
3559 * Update CSS classes provided by the theme.
3561 * For elements with theme logic hooks, this should be called anytime there's a state change.
3563 * @param {OO.ui.Element} Element for which to update classes
3564 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
3566 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
3567 var classes
= this.getElementClasses( element
);
3570 .removeClass( classes
.off
.join( ' ' ) )
3571 .addClass( classes
.on
.join( ' ' ) );
3575 * Element with a button.
3577 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3578 * and access keys for accessibility purposes.
3584 * @param {Object} [config] Configuration options
3585 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3586 * @cfg {boolean} [framed=true] Render button with a frame
3587 * @cfg {number} [tabIndex=0] Button's tab index, use null to have no tabIndex
3588 * @cfg {string} [accessKey] Button's access key
3590 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3591 // Configuration initialization
3592 config
= config
|| {};
3595 this.$button
= null;
3597 this.tabIndex
= null;
3598 this.accessKey
= null;
3599 this.active
= false;
3600 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
3601 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
3604 this.$element
.addClass( 'oo-ui-buttonElement' );
3605 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3606 this.setTabIndex( config
.tabIndex
|| 0 );
3607 this.setAccessKey( config
.accessKey
);
3608 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3613 OO
.initClass( OO
.ui
.ButtonElement
);
3615 /* Static Properties */
3618 * Cancel mouse down events.
3622 * @property {boolean}
3624 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3629 * Set the button element.
3631 * If an element is already set, it will be cleaned up before setting up the new element.
3633 * @param {jQuery} $button Element to use as button
3635 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3636 if ( this.$button
) {
3638 .removeClass( 'oo-ui-buttonElement-button' )
3639 .removeAttr( 'role accesskey tabindex' )
3640 .off( this.onMouseDownHandler
);
3643 this.$button
= $button
3644 .addClass( 'oo-ui-buttonElement-button' )
3645 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3646 .on( 'mousedown', this.onMouseDownHandler
);
3650 * Handles mouse down events.
3652 * @param {jQuery.Event} e Mouse down event
3654 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3655 if ( this.isDisabled() || e
.which
!== 1 ) {
3658 // Remove the tab-index while the button is down to prevent the button from stealing focus
3659 this.$button
.removeAttr( 'tabindex' );
3660 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3661 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3662 // reliably reapply the tabindex and remove the pressed class
3663 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3664 // Prevent change of focus unless specifically configured otherwise
3665 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3671 * Handles mouse up events.
3673 * @param {jQuery.Event} e Mouse up event
3675 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3676 if ( this.isDisabled() || e
.which
!== 1 ) {
3679 // Restore the tab-index after the button is up to restore the button's accesssibility
3680 this.$button
.attr( 'tabindex', this.tabIndex
);
3681 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3682 // Stop listening for mouseup, since we only needed this once
3683 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3687 * Check if button has a frame.
3689 * @return {boolean} Button is framed
3691 OO
.ui
.ButtonElement
.prototype.isFramed = function () {
3698 * @param {boolean} [framed] Make button framed, omit to toggle
3701 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3702 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3703 if ( framed
!== this.framed
) {
3704 this.framed
= framed
;
3706 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3707 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3708 this.updateThemeClasses();
3717 * @param {number|null} tabIndex Button's tab index, use null to remove
3720 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3721 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3723 if ( this.tabIndex
!== tabIndex
) {
3724 if ( this.$button
) {
3725 if ( tabIndex
!== null ) {
3726 this.$button
.attr( 'tabindex', tabIndex
);
3728 this.$button
.removeAttr( 'tabindex' );
3731 this.tabIndex
= tabIndex
;
3740 * @param {string} accessKey Button's access key, use empty string to remove
3743 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3744 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3746 if ( this.accessKey
!== accessKey
) {
3747 if ( this.$button
) {
3748 if ( accessKey
!== null ) {
3749 this.$button
.attr( 'accesskey', accessKey
);
3751 this.$button
.removeAttr( 'accesskey' );
3754 this.accessKey
= accessKey
;
3763 * @param {boolean} [value] Make button active
3766 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3767 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3772 * Element containing a sequence of child elements.
3778 * @param {Object} [config] Configuration options
3779 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3781 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3783 config
= config
|| {};
3788 this.aggregateItemEvents
= {};
3791 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3797 * Set the group element.
3799 * If an element is already set, items will be moved to the new element.
3801 * @param {jQuery} $group Element to use as group
3803 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3806 this.$group
= $group
;
3807 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3808 this.$group
.append( this.items
[i
].$element
);
3813 * Check if there are no items.
3815 * @return {boolean} Group is empty
3817 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3818 return !this.items
.length
;
3824 * @return {OO.ui.Element[]} Items
3826 OO
.ui
.GroupElement
.prototype.getItems = function () {
3827 return this.items
.slice( 0 );
3831 * Add an aggregate item event.
3833 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3834 * and with an additional leading parameter containing the item that emitted the original event.
3835 * Other arguments that were emitted from the original event are passed through.
3837 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3838 * event, use null value to remove aggregation
3839 * @throws {Error} If aggregation already exists
3841 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3842 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3844 for ( itemEvent
in events
) {
3845 groupEvent
= events
[itemEvent
];
3847 // Remove existing aggregated event
3848 if ( itemEvent
in this.aggregateItemEvents
) {
3849 // Don't allow duplicate aggregations
3851 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3853 // Remove event aggregation from existing items
3854 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3855 item
= this.items
[i
];
3856 if ( item
.connect
&& item
.disconnect
) {
3858 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3859 item
.disconnect( this, remove
);
3862 // Prevent future items from aggregating event
3863 delete this.aggregateItemEvents
[itemEvent
];
3866 // Add new aggregate event
3868 // Make future items aggregate event
3869 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3870 // Add event aggregation to existing items
3871 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3872 item
= this.items
[i
];
3873 if ( item
.connect
&& item
.disconnect
) {
3875 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3876 item
.connect( this, add
);
3886 * Adding an existing item (by value) will move it.
3888 * @param {OO.ui.Element[]} items Items
3889 * @param {number} [index] Index to insert items at
3892 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3893 var i
, len
, item
, event
, events
, currentIndex
,
3896 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3899 // Check if item exists then remove it first, effectively "moving" it
3900 currentIndex
= $.inArray( item
, this.items
);
3901 if ( currentIndex
>= 0 ) {
3902 this.removeItems( [ item
] );
3903 // Adjust index to compensate for removal
3904 if ( currentIndex
< index
) {
3909 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3911 for ( event
in this.aggregateItemEvents
) {
3912 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3914 item
.connect( this, events
);
3916 item
.setElementGroup( this );
3917 itemElements
.push( item
.$element
.get( 0 ) );
3920 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3921 this.$group
.append( itemElements
);
3922 this.items
.push
.apply( this.items
, items
);
3923 } else if ( index
=== 0 ) {
3924 this.$group
.prepend( itemElements
);
3925 this.items
.unshift
.apply( this.items
, items
);
3927 this.items
[index
].$element
.before( itemElements
);
3928 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3937 * Items will be detached, not removed, so they can be used later.
3939 * @param {OO.ui.Element[]} items Items to remove
3942 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3943 var i
, len
, item
, index
, remove
, itemEvent
;
3945 // Remove specific items
3946 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3948 index
= $.inArray( item
, this.items
);
3949 if ( index
!== -1 ) {
3951 item
.connect
&& item
.disconnect
&&
3952 !$.isEmptyObject( this.aggregateItemEvents
)
3955 if ( itemEvent
in this.aggregateItemEvents
) {
3956 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3958 item
.disconnect( this, remove
);
3960 item
.setElementGroup( null );
3961 this.items
.splice( index
, 1 );
3962 item
.$element
.detach();
3972 * Items will be detached, not removed, so they can be used later.
3976 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3977 var i
, len
, item
, remove
, itemEvent
;
3980 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3981 item
= this.items
[i
];
3983 item
.connect
&& item
.disconnect
&&
3984 !$.isEmptyObject( this.aggregateItemEvents
)
3987 if ( itemEvent
in this.aggregateItemEvents
) {
3988 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3990 item
.disconnect( this, remove
);
3992 item
.setElementGroup( null );
3993 item
.$element
.detach();
4001 * Element containing an icon.
4003 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
4004 * a control or convey information in a more space efficient way. Icons should rarely be used
4005 * without labels; such as in a toolbar where space is at a premium or within a context where the
4006 * meaning is very clear to the user.
4012 * @param {Object} [config] Configuration options
4013 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
4014 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
4015 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4017 * @cfg {string} [iconTitle] Icon title text or a function that returns text
4019 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
4020 // Config intialization
4021 config
= config
|| {};
4026 this.iconTitle
= null;
4029 this.setIcon( config
.icon
|| this.constructor.static.icon
);
4030 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
4031 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
4036 OO
.initClass( OO
.ui
.IconElement
);
4038 /* Static Properties */
4043 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
4045 * For i18n purposes, this property can be an object containing a `default` icon name property and
4046 * additional icon names keyed by language code.
4048 * Example of i18n icon definition:
4049 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
4053 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
4054 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4057 OO
.ui
.IconElement
.static.icon
= null;
4064 * @property {string|Function|null} Icon title text, a function that returns text or null for no
4067 OO
.ui
.IconElement
.static.iconTitle
= null;
4072 * Set the icon element.
4074 * If an element is already set, it will be cleaned up before setting up the new element.
4076 * @param {jQuery} $icon Element to use as icon
4078 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
4081 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
4082 .removeAttr( 'title' );
4086 .addClass( 'oo-ui-iconElement-icon' )
4087 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
4088 if ( this.iconTitle
!== null ) {
4089 this.$icon
.attr( 'title', this.iconTitle
);
4096 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
4097 * use the 'default' key to specify the icon to be used when there is no icon in the user's
4098 * language, use null to remove icon
4101 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
4102 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
4103 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
4105 if ( this.icon
!== icon
) {
4107 if ( this.icon
!== null ) {
4108 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
4110 if ( icon
!== null ) {
4111 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
4117 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
4118 this.updateThemeClasses();
4126 * @param {string|Function|null} icon Icon title text, a function that returns text or null
4130 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
4131 iconTitle
= typeof iconTitle
=== 'function' ||
4132 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
4133 OO
.ui
.resolveMsg( iconTitle
) : null;
4135 if ( this.iconTitle
!== iconTitle
) {
4136 this.iconTitle
= iconTitle
;
4138 if ( this.iconTitle
!== null ) {
4139 this.$icon
.attr( 'title', iconTitle
);
4141 this.$icon
.removeAttr( 'title' );
4152 * @return {string} Icon
4154 OO
.ui
.IconElement
.prototype.getIcon = function () {
4159 * Element containing an indicator.
4161 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
4162 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
4163 * instead of performing an action directly, or an item in a list which has errors that need to be
4170 * @param {Object} [config] Configuration options
4171 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
4173 * @cfg {string} [indicator] Symbolic indicator name
4174 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
4176 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
4177 // Config intialization
4178 config
= config
|| {};
4181 this.$indicator
= null;
4182 this.indicator
= null;
4183 this.indicatorTitle
= null;
4186 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
4187 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
4188 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4193 OO
.initClass( OO
.ui
.IndicatorElement
);
4195 /* Static Properties */
4202 * @property {string|null} Symbolic indicator name or null for no indicator
4204 OO
.ui
.IndicatorElement
.static.indicator
= null;
4211 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4214 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4219 * Set the indicator element.
4221 * If an element is already set, it will be cleaned up before setting up the new element.
4223 * @param {jQuery} $indicator Element to use as indicator
4225 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4226 if ( this.$indicator
) {
4228 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4229 .removeAttr( 'title' );
4232 this.$indicator
= $indicator
4233 .addClass( 'oo-ui-indicatorElement-indicator' )
4234 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4235 if ( this.indicatorTitle
!== null ) {
4236 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4243 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4246 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4247 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4249 if ( this.indicator
!== indicator
) {
4250 if ( this.$indicator
) {
4251 if ( this.indicator
!== null ) {
4252 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4254 if ( indicator
!== null ) {
4255 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4258 this.indicator
= indicator
;
4261 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4262 this.updateThemeClasses();
4268 * Set indicator title.
4270 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4271 * null for no indicator title
4274 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4275 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4276 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4277 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4279 if ( this.indicatorTitle
!== indicatorTitle
) {
4280 this.indicatorTitle
= indicatorTitle
;
4281 if ( this.$indicator
) {
4282 if ( this.indicatorTitle
!== null ) {
4283 this.$indicator
.attr( 'title', indicatorTitle
);
4285 this.$indicator
.removeAttr( 'title' );
4296 * @return {string} title Symbolic name of indicator
4298 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4299 return this.indicator
;
4303 * Get indicator title.
4305 * @return {string} Indicator title text
4307 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4308 return this.indicatorTitle
;
4312 * Element containing a label.
4318 * @param {Object} [config] Configuration options
4319 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4320 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4321 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4323 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4324 // Config intialization
4325 config
= config
|| {};
4330 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4333 this.setLabel( config
.label
|| this.constructor.static.label
);
4334 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4339 OO
.initClass( OO
.ui
.LabelElement
);
4341 /* Static Properties */
4348 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4351 OO
.ui
.LabelElement
.static.label
= null;
4356 * Set the label element.
4358 * If an element is already set, it will be cleaned up before setting up the new element.
4360 * @param {jQuery} $label Element to use as label
4362 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4363 if ( this.$label
) {
4364 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4367 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4368 this.setLabelContent( this.label
);
4374 * An empty string will result in the label being hidden. A string containing only whitespace will
4375 * be converted to a single
4377 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4378 * text; or null for no label
4381 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4382 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4383 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4385 if ( this.label
!== label
) {
4386 if ( this.$label
) {
4387 this.setLabelContent( label
);
4392 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4400 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4401 * text; or null for no label
4403 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4412 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4413 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4414 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4421 * Set the content of the label.
4423 * Do not call this method until after the label element has been set by #setLabelElement.
4426 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4427 * text; or null for no label
4429 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4430 if ( typeof label
=== 'string' ) {
4431 if ( label
.match( /^\s*$/ ) ) {
4432 // Convert whitespace only string to a single non-breaking space
4433 this.$label
.html( ' ' );
4435 this.$label
.text( label
);
4437 } else if ( label
instanceof jQuery
) {
4438 this.$label
.empty().append( label
);
4440 this.$label
.empty();
4442 this.$label
.css( 'display', !label
? 'none' : '' );
4446 * Element containing an OO.ui.PopupWidget object.
4452 * @param {Object} [config] Configuration options
4453 * @cfg {Object} [popup] Configuration to pass to popup
4454 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4456 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4457 // Configuration initialization
4458 config
= config
|| {};
4461 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4462 { autoClose
: true },
4464 { $: this.$, $autoCloseIgnore
: this.$element
}
4473 * @return {OO.ui.PopupWidget} Popup widget
4475 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4480 * Element with named flags that can be added, removed, listed and checked.
4482 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4483 * the flag name. Flags are primarily useful for styling.
4489 * @param {Object} [config] Configuration options
4490 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4491 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4493 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4494 // Config initialization
4495 config
= config
|| {};
4499 this.$flagged
= null;
4502 this.setFlags( config
.flags
);
4503 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4510 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4511 * added/removed properties
4517 * Set the flagged element.
4519 * If an element is already set, it will be cleaned up before setting up the new element.
4521 * @param {jQuery} $flagged Element to add flags to
4523 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4524 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4525 return 'oo-ui-flaggedElement-' + flag
;
4528 if ( this.$flagged
) {
4529 this.$flagged
.removeClass( classNames
);
4532 this.$flagged
= $flagged
.addClass( classNames
);
4536 * Check if a flag is set.
4538 * @param {string} flag Name of flag
4539 * @return {boolean} Has flag
4541 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4542 return flag
in this.flags
;
4546 * Get the names of all flags set.
4548 * @return {string[]} flags Flag names
4550 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4551 return Object
.keys( this.flags
);
4560 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4561 var flag
, className
,
4564 classPrefix
= 'oo-ui-flaggedElement-';
4566 for ( flag
in this.flags
) {
4567 className
= classPrefix
+ flag
;
4568 changes
[flag
] = false;
4569 delete this.flags
[flag
];
4570 remove
.push( className
);
4573 if ( this.$flagged
) {
4574 this.$flagged
.removeClass( remove
.join( ' ' ) );
4577 this.updateThemeClasses();
4578 this.emit( 'flag', changes
);
4584 * Add one or more flags.
4586 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4587 * keyed by flag name containing boolean set/remove instructions.
4591 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4592 var i
, len
, flag
, className
,
4596 classPrefix
= 'oo-ui-flaggedElement-';
4598 if ( typeof flags
=== 'string' ) {
4599 className
= classPrefix
+ flags
;
4601 if ( !this.flags
[flags
] ) {
4602 this.flags
[flags
] = true;
4603 add
.push( className
);
4605 } else if ( $.isArray( flags
) ) {
4606 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4608 className
= classPrefix
+ flag
;
4610 if ( !this.flags
[flag
] ) {
4611 changes
[flag
] = true;
4612 this.flags
[flag
] = true;
4613 add
.push( className
);
4616 } else if ( OO
.isPlainObject( flags
) ) {
4617 for ( flag
in flags
) {
4618 className
= classPrefix
+ flag
;
4619 if ( flags
[flag
] ) {
4621 if ( !this.flags
[flag
] ) {
4622 changes
[flag
] = true;
4623 this.flags
[flag
] = true;
4624 add
.push( className
);
4628 if ( this.flags
[flag
] ) {
4629 changes
[flag
] = false;
4630 delete this.flags
[flag
];
4631 remove
.push( className
);
4637 if ( this.$flagged
) {
4639 .addClass( add
.join( ' ' ) )
4640 .removeClass( remove
.join( ' ' ) );
4643 this.updateThemeClasses();
4644 this.emit( 'flag', changes
);
4650 * Element with a title.
4652 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4653 * not visible on touch devices.
4659 * @param {Object} [config] Configuration options
4660 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4661 * @cfg {string|Function} [title] Title text or a function that returns text
4663 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4664 // Config intialization
4665 config
= config
|| {};
4668 this.$titled
= null;
4672 this.setTitle( config
.title
|| this.constructor.static.title
);
4673 this.setTitledElement( config
.$titled
|| this.$element
);
4678 OO
.initClass( OO
.ui
.TitledElement
);
4680 /* Static Properties */
4687 * @property {string|Function} Title text or a function that returns text
4689 OO
.ui
.TitledElement
.static.title
= null;
4694 * Set the titled element.
4696 * If an element is already set, it will be cleaned up before setting up the new element.
4698 * @param {jQuery} $titled Element to set title on
4700 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4701 if ( this.$titled
) {
4702 this.$titled
.removeAttr( 'title' );
4705 this.$titled
= $titled
;
4707 this.$titled
.attr( 'title', this.title
);
4714 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4717 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4718 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4720 if ( this.title
!== title
) {
4721 if ( this.$titled
) {
4722 if ( title
!== null ) {
4723 this.$titled
.attr( 'title', title
);
4725 this.$titled
.removeAttr( 'title' );
4737 * @return {string} Title string
4739 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4744 * Element that can be automatically clipped to visible boundaries.
4746 * Whenever the element's natural height changes, you have to call
4747 * #clip to make sure it's still clipping correctly.
4753 * @param {Object} [config] Configuration options
4754 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4756 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4757 // Configuration initialization
4758 config
= config
|| {};
4761 this.$clippable
= null;
4762 this.clipping
= false;
4763 this.clippedHorizontally
= false;
4764 this.clippedVertically
= false;
4765 this.$clippableContainer
= null;
4766 this.$clippableScroller
= null;
4767 this.$clippableWindow
= null;
4768 this.idealWidth
= null;
4769 this.idealHeight
= null;
4770 this.onClippableContainerScrollHandler
= this.clip
.bind( this );
4771 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
4774 this.setClippableElement( config
.$clippable
|| this.$element
);
4780 * Set clippable element.
4782 * If an element is already set, it will be cleaned up before setting up the new element.
4784 * @param {jQuery} $clippable Element to make clippable
4786 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4787 if ( this.$clippable
) {
4788 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4789 this.$clippable
.css( { width
: '', height
: '' } );
4790 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4791 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4794 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4801 * Do not turn clipping on until after the element is attached to the DOM and visible.
4803 * @param {boolean} [clipping] Enable clipping, omit to toggle
4806 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4807 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4809 if ( this.clipping
!== clipping
) {
4810 this.clipping
= clipping
;
4812 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4813 // If the clippable container is the body, we have to listen to scroll events and check
4814 // jQuery.scrollTop on the window because of browser inconsistencies
4815 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4816 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4817 this.$clippableContainer
;
4818 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4819 this.$clippableWindow
= this.$( this.getElementWindow() )
4820 .on( 'resize', this.onClippableWindowResizeHandler
);
4821 // Initial clip after visible
4824 this.$clippable
.css( { width
: '', height
: '' } );
4825 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4826 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4828 this.$clippableContainer
= null;
4829 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4830 this.$clippableScroller
= null;
4831 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4832 this.$clippableWindow
= null;
4840 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4842 * @return {boolean} Element will be clipped to the visible area
4844 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4845 return this.clipping
;
4849 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4851 * @return {boolean} Part of the element is being clipped
4853 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4854 return this.clippedHorizontally
|| this.clippedVertically
;
4858 * Check if the right of the element is being clipped by the nearest scrollable container.
4860 * @return {boolean} Part of the element is being clipped
4862 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4863 return this.clippedHorizontally
;
4867 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4869 * @return {boolean} Part of the element is being clipped
4871 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4872 return this.clippedVertically
;
4876 * Set the ideal size. These are the dimensions the element will have when it's not being clipped.
4878 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4879 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4881 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4882 this.idealWidth
= width
;
4883 this.idealHeight
= height
;
4885 if ( !this.clipping
) {
4886 // Update dimensions
4887 this.$clippable
.css( { width
: width
, height
: height
} );
4889 // While clipping, idealWidth and idealHeight are not considered
4893 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4894 * the element's natural height changes.
4896 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4897 * overlapped by, the visible area of the nearest scrollable container.
4901 OO
.ui
.ClippableElement
.prototype.clip = function () {
4902 if ( !this.clipping
) {
4903 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
4908 cOffset
= this.$clippable
.offset(),
4909 $container
= this.$clippableContainer
.is( 'body' ) ?
4910 this.$clippableWindow
: this.$clippableContainer
,
4911 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
4912 ccHeight
= $container
.innerHeight() - buffer
,
4913 ccWidth
= $container
.innerWidth() - buffer
,
4914 scrollTop
= this.$clippableScroller
.scrollTop(),
4915 scrollLeft
= this.$clippableScroller
.scrollLeft(),
4916 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
4917 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
4918 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
4919 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
4920 clipWidth
= desiredWidth
< naturalWidth
,
4921 clipHeight
= desiredHeight
< naturalHeight
;
4924 this.$clippable
.css( { overflowX
: 'scroll', width
: desiredWidth
} );
4926 this.$clippable
.css( 'width', this.idealWidth
|| '' );
4927 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4928 this.$clippable
.css( 'overflowX', '' );
4931 this.$clippable
.css( { overflowY
: 'scroll', height
: desiredHeight
} );
4933 this.$clippable
.css( 'height', this.idealHeight
|| '' );
4934 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4935 this.$clippable
.css( 'overflowY', '' );
4938 this.clippedHorizontally
= clipWidth
;
4939 this.clippedVertically
= clipHeight
;
4945 * Generic toolbar tool.
4949 * @extends OO.ui.Widget
4950 * @mixins OO.ui.IconElement
4951 * @mixins OO.ui.FlaggedElement
4954 * @param {OO.ui.ToolGroup} toolGroup
4955 * @param {Object} [config] Configuration options
4956 * @cfg {string|Function} [title] Title text or a function that returns text
4958 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4959 // Config intialization
4960 config
= config
|| {};
4962 // Parent constructor
4963 OO
.ui
.Tool
.super.call( this, config
);
4965 // Mixin constructors
4966 OO
.ui
.IconElement
.call( this, config
);
4967 OO
.ui
.FlaggedElement
.call( this, config
);
4970 this.toolGroup
= toolGroup
;
4971 this.toolbar
= this.toolGroup
.getToolbar();
4972 this.active
= false;
4973 this.$title
= this.$( '<span>' );
4974 this.$link
= this.$( '<a>' );
4978 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
4981 this.$title
.addClass( 'oo-ui-tool-title' );
4983 .addClass( 'oo-ui-tool-link' )
4984 .append( this.$icon
, this.$title
)
4985 .prop( 'tabIndex', 0 )
4986 .attr( 'role', 'button' );
4988 .data( 'oo-ui-tool', this )
4990 'oo-ui-tool ' + 'oo-ui-tool-name-' +
4991 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
4993 .append( this.$link
);
4994 this.setTitle( config
.title
|| this.constructor.static.title
);
4999 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
5000 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
5001 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.FlaggedElement
);
5009 /* Static Properties */
5015 OO
.ui
.Tool
.static.tagName
= 'span';
5018 * Symbolic name of tool.
5023 * @property {string}
5025 OO
.ui
.Tool
.static.name
= '';
5033 * @property {string}
5035 OO
.ui
.Tool
.static.group
= '';
5040 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
5041 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
5042 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
5043 * appended to the title if the tool is part of a bar tool group.
5048 * @property {string|Function} Title text or a function that returns text
5050 OO
.ui
.Tool
.static.title
= '';
5053 * Tool can be automatically added to catch-all groups.
5057 * @property {boolean}
5059 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
5062 * Tool can be automatically added to named groups.
5065 * @property {boolean}
5068 OO
.ui
.Tool
.static.autoAddToGroup
= true;
5071 * Check if this tool is compatible with given data.
5075 * @param {Mixed} data Data to check
5076 * @return {boolean} Tool can be used with data
5078 OO
.ui
.Tool
.static.isCompatibleWith = function () {
5085 * Handle the toolbar state being updated.
5087 * This is an abstract method that must be overridden in a concrete subclass.
5091 OO
.ui
.Tool
.prototype.onUpdateState = function () {
5093 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
5098 * Handle the tool being selected.
5100 * This is an abstract method that must be overridden in a concrete subclass.
5104 OO
.ui
.Tool
.prototype.onSelect = function () {
5106 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
5111 * Check if the button is active.
5113 * @param {boolean} Button is active
5115 OO
.ui
.Tool
.prototype.isActive = function () {
5120 * Make the button appear active or inactive.
5122 * @param {boolean} state Make button appear active
5124 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
5125 this.active
= !!state
;
5126 if ( this.active
) {
5127 this.$element
.addClass( 'oo-ui-tool-active' );
5129 this.$element
.removeClass( 'oo-ui-tool-active' );
5134 * Get the tool title.
5136 * @param {string|Function} title Title text or a function that returns text
5139 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
5140 this.title
= OO
.ui
.resolveMsg( title
);
5146 * Get the tool title.
5148 * @return {string} Title text
5150 OO
.ui
.Tool
.prototype.getTitle = function () {
5155 * Get the tool's symbolic name.
5157 * @return {string} Symbolic name of tool
5159 OO
.ui
.Tool
.prototype.getName = function () {
5160 return this.constructor.static.name
;
5166 OO
.ui
.Tool
.prototype.updateTitle = function () {
5167 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
5168 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
5169 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
5176 .addClass( 'oo-ui-tool-accel' )
5180 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
5181 tooltipParts
.push( this.title
);
5183 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
5184 tooltipParts
.push( accel
);
5186 if ( tooltipParts
.length
) {
5187 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
5189 this.$link
.removeAttr( 'title' );
5196 OO
.ui
.Tool
.prototype.destroy = function () {
5197 this.toolbar
.disconnect( this );
5198 this.$element
.remove();
5202 * Collection of tool groups.
5205 * @extends OO.ui.Element
5206 * @mixins OO.EventEmitter
5207 * @mixins OO.ui.GroupElement
5210 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5211 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5212 * @param {Object} [config] Configuration options
5213 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5214 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5216 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5217 // Configuration initialization
5218 config
= config
|| {};
5220 // Parent constructor
5221 OO
.ui
.Toolbar
.super.call( this, config
);
5223 // Mixin constructors
5224 OO
.EventEmitter
.call( this );
5225 OO
.ui
.GroupElement
.call( this, config
);
5228 this.toolFactory
= toolFactory
;
5229 this.toolGroupFactory
= toolGroupFactory
;
5232 this.$bar
= this.$( '<div>' );
5233 this.$actions
= this.$( '<div>' );
5234 this.initialized
= false;
5238 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5239 .on( 'mousedown touchstart', this.onPointerDown
.bind( this ) );
5242 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5243 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
5244 if ( config
.actions
) {
5245 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
5246 this.$bar
.append( this.$actions
);
5248 this.$bar
.append( '<div style="clear:both"></div>' );
5249 if ( config
.shadow
) {
5250 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5252 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5257 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5258 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5259 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5264 * Get the tool factory.
5266 * @return {OO.ui.ToolFactory} Tool factory
5268 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5269 return this.toolFactory
;
5273 * Get the tool group factory.
5275 * @return {OO.Factory} Tool group factory
5277 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5278 return this.toolGroupFactory
;
5282 * Handles mouse down events.
5284 * @param {jQuery.Event} e Mouse down event
5286 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5287 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5288 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5289 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5295 * Sets up handles and preloads required information for the toolbar to work.
5296 * This must be called immediately after it is attached to a visible document.
5298 OO
.ui
.Toolbar
.prototype.initialize = function () {
5299 this.initialized
= true;
5305 * Tools can be specified in the following ways:
5307 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5308 * - All tools in a group: `{ group: 'group-name' }`
5309 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5311 * @param {Object.<string,Array>} groups List of tool group configurations
5312 * @param {Array|string} [groups.include] Tools to include
5313 * @param {Array|string} [groups.exclude] Tools to exclude
5314 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5315 * @param {Array|string} [groups.demote] Tools to demote to the end
5317 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5318 var i
, len
, type
, group
,
5320 defaultType
= 'bar';
5322 // Cleanup previous groups
5325 // Build out new groups
5326 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5328 if ( group
.include
=== '*' ) {
5329 // Apply defaults to catch-all groups
5330 if ( group
.type
=== undefined ) {
5331 group
.type
= 'list';
5333 if ( group
.label
=== undefined ) {
5334 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
5337 // Check type has been registered
5338 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5340 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5343 this.addItems( items
);
5347 * Remove all tools and groups from the toolbar.
5349 OO
.ui
.Toolbar
.prototype.reset = function () {
5354 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5355 this.items
[i
].destroy();
5361 * Destroys toolbar, removing event handlers and DOM elements.
5363 * Call this whenever you are done using a toolbar.
5365 OO
.ui
.Toolbar
.prototype.destroy = function () {
5367 this.$element
.remove();
5371 * Check if tool has not been used yet.
5373 * @param {string} name Symbolic name of tool
5374 * @return {boolean} Tool is available
5376 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5377 return !this.tools
[name
];
5381 * Prevent tool from being used again.
5383 * @param {OO.ui.Tool} tool Tool to reserve
5385 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5386 this.tools
[tool
.getName()] = tool
;
5390 * Allow tool to be used again.
5392 * @param {OO.ui.Tool} tool Tool to release
5394 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5395 delete this.tools
[tool
.getName()];
5399 * Get accelerator label for tool.
5401 * This is a stub that should be overridden to provide access to accelerator information.
5403 * @param {string} name Symbolic name of tool
5404 * @return {string|undefined} Tool accelerator label if available
5406 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5411 * Collection of tools.
5413 * Tools can be specified in the following ways:
5415 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5416 * - All tools in a group: `{ group: 'group-name' }`
5417 * - All tools: `'*'`
5421 * @extends OO.ui.Widget
5422 * @mixins OO.ui.GroupElement
5425 * @param {OO.ui.Toolbar} toolbar
5426 * @param {Object} [config] Configuration options
5427 * @cfg {Array|string} [include=[]] List of tools to include
5428 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5429 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5430 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5432 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5433 // Configuration initialization
5434 config
= config
|| {};
5436 // Parent constructor
5437 OO
.ui
.ToolGroup
.super.call( this, config
);
5439 // Mixin constructors
5440 OO
.ui
.GroupElement
.call( this, config
);
5443 this.toolbar
= toolbar
;
5445 this.pressed
= null;
5446 this.autoDisabled
= false;
5447 this.include
= config
.include
|| [];
5448 this.exclude
= config
.exclude
|| [];
5449 this.promote
= config
.promote
|| [];
5450 this.demote
= config
.demote
|| [];
5451 this.onCapturedMouseUpHandler
= this.onCapturedMouseUp
.bind( this );
5455 'mousedown touchstart': this.onPointerDown
.bind( this ),
5456 'mouseup touchend': this.onPointerUp
.bind( this ),
5457 mouseover
: this.onMouseOver
.bind( this ),
5458 mouseout
: this.onMouseOut
.bind( this )
5460 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5461 this.aggregate( { disable
: 'itemDisable' } );
5462 this.connect( this, { itemDisable
: 'updateDisabled' } );
5465 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5467 .addClass( 'oo-ui-toolGroup' )
5468 .append( this.$group
);
5474 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5475 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5483 /* Static Properties */
5486 * Show labels in tooltips.
5490 * @property {boolean}
5492 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5495 * Show acceleration labels in tooltips.
5499 * @property {boolean}
5501 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5504 * Automatically disable the toolgroup when all tools are disabled
5508 * @property {boolean}
5510 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5517 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5518 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5524 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5525 var i
, item
, allDisabled
= true;
5527 if ( this.constructor.static.autoDisable
) {
5528 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5529 item
= this.items
[i
];
5530 if ( !item
.isDisabled() ) {
5531 allDisabled
= false;
5535 this.autoDisabled
= allDisabled
;
5537 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5541 * Handle mouse down events.
5543 * @param {jQuery.Event} e Mouse down event
5545 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5546 // e.which is 0 for touch events, 1 for left mouse button
5547 if ( !this.isDisabled() && e
.which
<= 1 ) {
5548 this.pressed
= this.getTargetTool( e
);
5549 if ( this.pressed
) {
5550 this.pressed
.setActive( true );
5551 this.getElementDocument().addEventListener(
5552 'mouseup', this.onCapturedMouseUpHandler
, true
5560 * Handle captured mouse up events.
5562 * @param {Event} e Mouse up event
5564 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5565 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5566 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5567 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5568 this.onPointerUp( e
);
5572 * Handle mouse up events.
5574 * @param {jQuery.Event} e Mouse up event
5576 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5577 var tool
= this.getTargetTool( e
);
5579 // e.which is 0 for touch events, 1 for left mouse button
5580 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5581 this.pressed
.onSelect();
5584 this.pressed
= null;
5589 * Handle mouse over events.
5591 * @param {jQuery.Event} e Mouse over event
5593 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5594 var tool
= this.getTargetTool( e
);
5596 if ( this.pressed
&& this.pressed
=== tool
) {
5597 this.pressed
.setActive( true );
5602 * Handle mouse out events.
5604 * @param {jQuery.Event} e Mouse out event
5606 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5607 var tool
= this.getTargetTool( e
);
5609 if ( this.pressed
&& this.pressed
=== tool
) {
5610 this.pressed
.setActive( false );
5615 * Get the closest tool to a jQuery.Event.
5617 * Only tool links are considered, which prevents other elements in the tool such as popups from
5618 * triggering tool group interactions.
5621 * @param {jQuery.Event} e
5622 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5624 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5626 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5628 if ( $item
.length
) {
5629 tool
= $item
.parent().data( 'oo-ui-tool' );
5632 return tool
&& !tool
.isDisabled() ? tool
: null;
5636 * Handle tool registry register events.
5638 * If a tool is registered after the group is created, we must repopulate the list to account for:
5640 * - a tool being added that may be included
5641 * - a tool already included being overridden
5643 * @param {string} name Symbolic name of tool
5645 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5650 * Get the toolbar this group is in.
5652 * @return {OO.ui.Toolbar} Toolbar of group
5654 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5655 return this.toolbar
;
5659 * Add and remove tools based on configuration.
5661 OO
.ui
.ToolGroup
.prototype.populate = function () {
5662 var i
, len
, name
, tool
,
5663 toolFactory
= this.toolbar
.getToolFactory(),
5667 list
= this.toolbar
.getToolFactory().getTools(
5668 this.include
, this.exclude
, this.promote
, this.demote
5671 // Build a list of needed tools
5672 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5676 toolFactory
.lookup( name
) &&
5677 // Tool is available or is already in this group
5678 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5680 tool
= this.tools
[name
];
5682 // Auto-initialize tools on first use
5683 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5686 this.toolbar
.reserveTool( tool
);
5691 // Remove tools that are no longer needed
5692 for ( name
in this.tools
) {
5693 if ( !names
[name
] ) {
5694 this.tools
[name
].destroy();
5695 this.toolbar
.releaseTool( this.tools
[name
] );
5696 remove
.push( this.tools
[name
] );
5697 delete this.tools
[name
];
5700 if ( remove
.length
) {
5701 this.removeItems( remove
);
5703 // Update emptiness state
5705 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5707 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5709 // Re-add tools (moving existing ones to new locations)
5710 this.addItems( add
);
5711 // Disabled state may depend on items
5712 this.updateDisabled();
5716 * Destroy tool group.
5718 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5722 this.toolbar
.getToolFactory().disconnect( this );
5723 for ( name
in this.tools
) {
5724 this.toolbar
.releaseTool( this.tools
[name
] );
5725 this.tools
[name
].disconnect( this ).destroy();
5726 delete this.tools
[name
];
5728 this.$element
.remove();
5732 * Dialog for showing a message.
5735 * - Registers two actions by default (safe and primary).
5736 * - Renders action widgets in the footer.
5739 * @extends OO.ui.Dialog
5742 * @param {Object} [config] Configuration options
5744 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5745 // Parent constructor
5746 OO
.ui
.MessageDialog
.super.call( this, config
);
5749 this.verticalActionLayout
= null;
5752 this.$element
.addClass( 'oo-ui-messageDialog' );
5757 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5759 /* Static Properties */
5761 OO
.ui
.MessageDialog
.static.name
= 'message';
5763 OO
.ui
.MessageDialog
.static.size
= 'small';
5765 OO
.ui
.MessageDialog
.static.verbose
= false;
5770 * A confirmation dialog's title should describe what the progressive action will do. An alert
5771 * dialog's title should describe what event occured.
5775 * @property {jQuery|string|Function|null}
5777 OO
.ui
.MessageDialog
.static.title
= null;
5780 * A confirmation dialog's message should describe the consequences of the progressive action. An
5781 * alert dialog's message should describe why the event occured.
5785 * @property {jQuery|string|Function|null}
5787 OO
.ui
.MessageDialog
.static.message
= null;
5789 OO
.ui
.MessageDialog
.static.actions
= [
5790 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5791 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5799 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5801 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5805 * Toggle action layout between vertical and horizontal.
5807 * @param {boolean} [value] Layout actions vertically, omit to toggle
5810 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5811 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5813 if ( value
!== this.verticalActionLayout
) {
5814 this.verticalActionLayout
= value
;
5816 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5817 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5826 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5828 return new OO
.ui
.Process( function () {
5829 this.close( { action
: action
} );
5832 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5838 * @param {Object} [data] Dialog opening data
5839 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5840 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5841 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5842 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5845 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5849 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5850 .next( function () {
5851 this.title
.setLabel(
5852 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5854 this.message
.setLabel(
5855 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5857 this.message
.$element
.toggleClass(
5858 'oo-ui-messageDialog-message-verbose',
5859 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5867 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5868 return Math
.round( this.text
.$element
.outerHeight( true ) );
5874 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5876 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5879 this.$actions
= this.$( '<div>' );
5880 this.container
= new OO
.ui
.PanelLayout( {
5881 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5883 this.text
= new OO
.ui
.PanelLayout( {
5884 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5886 this.message
= new OO
.ui
.LabelWidget( {
5887 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5891 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5892 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5893 this.container
.$element
.append( this.text
.$element
);
5894 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5895 this.$body
.append( this.container
.$element
);
5896 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5897 this.$foot
.append( this.$actions
);
5903 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5904 var i
, len
, other
, special
, others
;
5907 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5909 special
= this.actions
.getSpecial();
5910 others
= this.actions
.getOthers();
5911 if ( special
.safe
) {
5912 this.$actions
.append( special
.safe
.$element
);
5913 special
.safe
.toggleFramed( false );
5915 if ( others
.length
) {
5916 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5918 this.$actions
.append( other
.$element
);
5919 other
.toggleFramed( false );
5922 if ( special
.primary
) {
5923 this.$actions
.append( special
.primary
.$element
);
5924 special
.primary
.toggleFramed( false );
5928 if ( !this.isOpening() ) {
5929 this.manager
.updateWindowSize( this );
5931 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5935 * Fit action actions into columns or rows.
5937 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5939 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5941 actions
= this.actions
.get();
5944 this.toggleVerticalActionLayout( false );
5945 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5946 action
= actions
[i
];
5947 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5948 this.toggleVerticalActionLayout( true );
5955 * Navigation dialog window.
5958 * - Show and hide errors.
5959 * - Retry an action.
5962 * - Renders header with dialog title and one action widget on either side
5963 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5964 * which close the dialog).
5965 * - Displays any action widgets in the footer (none by default).
5966 * - Ability to dismiss errors.
5968 * Subclass responsibilities:
5969 * - Register a 'safe' action.
5970 * - Register a 'primary' action.
5971 * - Add content to the dialog.
5975 * @extends OO.ui.Dialog
5978 * @param {Object} [config] Configuration options
5980 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
5981 // Parent constructor
5982 OO
.ui
.ProcessDialog
.super.call( this, config
);
5985 this.$element
.addClass( 'oo-ui-processDialog' );
5990 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
5995 * Handle dismiss button click events.
5999 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
6004 * Handle retry button click events.
6006 * Hides errors and then tries again.
6008 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
6010 this.executeAction( this.currentAction
.getAction() );
6016 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
6017 if ( this.actions
.isSpecial( action
) ) {
6020 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
6026 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
6028 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
6031 this.$navigation
= this.$( '<div>' );
6032 this.$location
= this.$( '<div>' );
6033 this.$safeActions
= this.$( '<div>' );
6034 this.$primaryActions
= this.$( '<div>' );
6035 this.$otherActions
= this.$( '<div>' );
6036 this.dismissButton
= new OO
.ui
.ButtonWidget( {
6038 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
6040 this.retryButton
= new OO
.ui
.ButtonWidget( {
6042 label
: OO
.ui
.msg( 'ooui-dialog-process-retry' )
6044 this.$errors
= this.$( '<div>' );
6045 this.$errorsTitle
= this.$( '<div>' );
6048 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
6049 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
6052 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
6054 .append( this.title
.$element
)
6055 .addClass( 'oo-ui-processDialog-location' );
6056 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
6057 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
6058 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
6060 .addClass( 'oo-ui-processDialog-errors-title' )
6061 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
6063 .addClass( 'oo-ui-processDialog-errors' )
6064 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
6066 .addClass( 'oo-ui-processDialog-content' )
6067 .append( this.$errors
);
6069 .addClass( 'oo-ui-processDialog-navigation' )
6070 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
6071 this.$head
.append( this.$navigation
);
6072 this.$foot
.append( this.$otherActions
);
6078 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
6079 var i
, len
, other
, special
, others
;
6082 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
6084 special
= this.actions
.getSpecial();
6085 others
= this.actions
.getOthers();
6086 if ( special
.primary
) {
6087 this.$primaryActions
.append( special
.primary
.$element
);
6088 special
.primary
.toggleFramed( true );
6090 if ( others
.length
) {
6091 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
6093 this.$otherActions
.append( other
.$element
);
6094 other
.toggleFramed( true );
6097 if ( special
.safe
) {
6098 this.$safeActions
.append( special
.safe
.$element
);
6099 special
.safe
.toggleFramed( true );
6103 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
6109 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
6110 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
6111 .fail( this.showErrors
.bind( this ) );
6115 * Fit label between actions.
6119 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
6120 var width
= Math
.max(
6121 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
6122 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
6124 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
6130 * Handle errors that occured durring accept or reject processes.
6132 * @param {OO.ui.Error[]} errors Errors to be handled
6134 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
6139 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
6140 if ( !errors
[i
].isRecoverable() ) {
6141 recoverable
= false;
6143 $item
= this.$( '<div>' )
6144 .addClass( 'oo-ui-processDialog-error' )
6145 .append( errors
[i
].getMessage() );
6146 items
.push( $item
[0] );
6148 this.$errorItems
= this.$( items
);
6149 if ( recoverable
) {
6150 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
6152 this.currentAction
.setDisabled( true );
6154 this.retryButton
.toggle( recoverable
);
6155 this.$errorsTitle
.after( this.$errorItems
);
6156 this.$errors
.show().scrollTop( 0 );
6162 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
6163 this.$errors
.hide();
6164 this.$errorItems
.remove();
6165 this.$errorItems
= null;
6169 * Layout containing a series of pages.
6172 * @extends OO.ui.Layout
6175 * @param {Object} [config] Configuration options
6176 * @cfg {boolean} [continuous=false] Show all pages, one after another
6177 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
6178 * @cfg {boolean} [outlined=false] Show an outline
6179 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
6181 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
6182 // Initialize configuration
6183 config
= config
|| {};
6185 // Parent constructor
6186 OO
.ui
.BookletLayout
.super.call( this, config
);
6189 this.currentPageName
= null;
6191 this.ignoreFocus
= false;
6192 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
6193 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
6194 this.outlineVisible
= false;
6195 this.outlined
= !!config
.outlined
;
6196 if ( this.outlined
) {
6197 this.editable
= !!config
.editable
;
6198 this.outlineControlsWidget
= null;
6199 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
6200 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
6201 this.gridLayout
= new OO
.ui
.GridLayout(
6202 [ this.outlinePanel
, this.stackLayout
],
6203 { $: this.$, widths
: [ 1, 2 ] }
6205 this.outlineVisible
= true;
6206 if ( this.editable
) {
6207 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6208 this.outlineWidget
, { $: this.$ }
6214 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6215 if ( this.outlined
) {
6216 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
6218 if ( this.autoFocus
) {
6219 // Event 'focus' does not bubble, but 'focusin' does
6220 this.stackLayout
.onDOMEvent( 'focusin', this.onStackLayoutFocus
.bind( this ) );
6224 this.$element
.addClass( 'oo-ui-bookletLayout' );
6225 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6226 if ( this.outlined
) {
6227 this.outlinePanel
.$element
6228 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6229 .append( this.outlineWidget
.$element
);
6230 if ( this.editable
) {
6231 this.outlinePanel
.$element
6232 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6233 .append( this.outlineControlsWidget
.$element
);
6235 this.$element
.append( this.gridLayout
.$element
);
6237 this.$element
.append( this.stackLayout
.$element
);
6243 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6249 * @param {OO.ui.PageLayout} page Current page
6254 * @param {OO.ui.PageLayout[]} page Added pages
6255 * @param {number} index Index pages were added at
6260 * @param {OO.ui.PageLayout[]} pages Removed pages
6266 * Handle stack layout focus.
6268 * @param {jQuery.Event} e Focusin event
6270 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6273 // Find the page that an element was focused within
6274 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6275 for ( name
in this.pages
) {
6276 // Check for page match, exclude current page to find only page changes
6277 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6278 this.setPage( name
);
6285 * Handle stack layout set events.
6287 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6289 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6290 var $input
, layout
= this;
6292 page
.scrollElementIntoView( { complete: function () {
6293 if ( layout
.autoFocus
) {
6294 // Set focus to the first input if nothing on the page is focused yet
6295 if ( !page
.$element
.find( ':focus' ).length
) {
6296 $input
= page
.$element
.find( ':input:first' );
6297 if ( $input
.length
) {
6307 * Handle outline widget select events.
6309 * @param {OO.ui.OptionWidget|null} item Selected item
6311 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
6313 this.setPage( item
.getData() );
6318 * Check if booklet has an outline.
6322 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6323 return this.outlined
;
6327 * Check if booklet has editing controls.
6331 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6332 return this.editable
;
6336 * Check if booklet has a visible outline.
6340 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6341 return this.outlined
&& this.outlineVisible
;
6345 * Hide or show the outline.
6347 * @param {boolean} [show] Show outline, omit to invert current state
6350 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6351 if ( this.outlined
) {
6352 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6353 this.outlineVisible
= show
;
6354 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6361 * Get the outline widget.
6363 * @param {OO.ui.PageLayout} page Page to be selected
6364 * @return {OO.ui.PageLayout|null} Closest page to another
6366 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6367 var next
, prev
, level
,
6368 pages
= this.stackLayout
.getItems(),
6369 index
= $.inArray( page
, pages
);
6371 if ( index
!== -1 ) {
6372 next
= pages
[index
+ 1];
6373 prev
= pages
[index
- 1];
6374 // Prefer adjacent pages at the same level
6375 if ( this.outlined
) {
6376 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
6379 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
6385 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
6391 return prev
|| next
|| null;
6395 * Get the outline widget.
6397 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
6399 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6400 return this.outlineWidget
;
6404 * Get the outline controls widget. If the outline is not editable, null is returned.
6406 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6408 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6409 return this.outlineControlsWidget
;
6413 * Get a page by name.
6415 * @param {string} name Symbolic name of page
6416 * @return {OO.ui.PageLayout|undefined} Page, if found
6418 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6419 return this.pages
[name
];
6423 * Get the current page name.
6425 * @return {string|null} Current page name
6427 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6428 return this.currentPageName
;
6432 * Add a page to the layout.
6434 * When pages are added with the same names as existing pages, the existing pages will be
6435 * automatically removed before the new pages are added.
6437 * @param {OO.ui.PageLayout[]} pages Pages to add
6438 * @param {number} index Index to insert pages after
6442 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6443 var i
, len
, name
, page
, item
, currentIndex
,
6444 stackLayoutPages
= this.stackLayout
.getItems(),
6448 // Remove pages with same names
6449 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6451 name
= page
.getName();
6453 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6454 // Correct the insertion index
6455 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6456 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6459 remove
.push( this.pages
[name
] );
6462 if ( remove
.length
) {
6463 this.removePages( remove
);
6467 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6469 name
= page
.getName();
6470 this.pages
[page
.getName()] = page
;
6471 if ( this.outlined
) {
6472 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
6473 page
.setOutlineItem( item
);
6478 if ( this.outlined
&& items
.length
) {
6479 this.outlineWidget
.addItems( items
, index
);
6480 this.updateOutlineWidget();
6482 this.stackLayout
.addItems( pages
, index
);
6483 this.emit( 'add', pages
, index
);
6489 * Remove a page from the layout.
6494 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6495 var i
, len
, name
, page
,
6498 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6500 name
= page
.getName();
6501 delete this.pages
[name
];
6502 if ( this.outlined
) {
6503 items
.push( this.outlineWidget
.getItemFromData( name
) );
6504 page
.setOutlineItem( null );
6507 if ( this.outlined
&& items
.length
) {
6508 this.outlineWidget
.removeItems( items
);
6509 this.updateOutlineWidget();
6511 this.stackLayout
.removeItems( pages
);
6512 this.emit( 'remove', pages
);
6518 * Clear all pages from the layout.
6523 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6525 pages
= this.stackLayout
.getItems();
6528 this.currentPageName
= null;
6529 if ( this.outlined
) {
6530 this.outlineWidget
.clearItems();
6531 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6532 pages
[i
].setOutlineItem( null );
6535 this.stackLayout
.clearItems();
6537 this.emit( 'remove', pages
);
6543 * Set the current page by name.
6546 * @param {string} name Symbolic name of page
6548 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6551 page
= this.pages
[name
];
6553 if ( name
!== this.currentPageName
) {
6554 if ( this.outlined
) {
6555 selectedItem
= this.outlineWidget
.getSelectedItem();
6556 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6557 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
6561 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6562 this.pages
[this.currentPageName
].setActive( false );
6563 // Blur anything focused if the next page doesn't have anything focusable - this
6564 // is not needed if the next page has something focusable because once it is focused
6565 // this blur happens automatically
6566 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6567 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6568 if ( $focused
.length
) {
6573 this.currentPageName
= name
;
6574 this.stackLayout
.setItem( page
);
6575 page
.setActive( true );
6576 this.emit( 'set', page
);
6582 * Call this after adding or removing items from the OutlineWidget.
6586 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
6587 // Auto-select first item when nothing is selected anymore
6588 if ( !this.outlineWidget
.getSelectedItem() ) {
6589 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
6596 * Layout made of a field and optional label.
6599 * @extends OO.ui.Layout
6600 * @mixins OO.ui.LabelElement
6602 * Available label alignment modes include:
6603 * - left: Label is before the field and aligned away from it, best for when the user will be
6604 * scanning for a specific label in a form with many fields
6605 * - right: Label is before the field and aligned toward it, best for forms the user is very
6606 * familiar with and will tab through field checking quickly to verify which field they are in
6607 * - top: Label is before the field and above it, best for when the user will need to fill out all
6608 * fields from top to bottom in a form with few fields
6609 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6610 * checkboxes or radio buttons
6613 * @param {OO.ui.Widget} fieldWidget Field widget
6614 * @param {Object} [config] Configuration options
6615 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6616 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6618 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
6619 // Config initialization
6620 config
= $.extend( { align
: 'left' }, config
);
6622 // Parent constructor
6623 OO
.ui
.FieldLayout
.super.call( this, config
);
6625 // Mixin constructors
6626 OO
.ui
.LabelElement
.call( this, config
);
6629 this.$field
= this.$( '<div>' );
6630 this.fieldWidget
= fieldWidget
;
6632 if ( config
.help
) {
6633 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6635 classes
: [ 'oo-ui-fieldLayout-help' ],
6640 this.popupButtonWidget
.getPopup().$body
.append(
6642 .text( config
.help
)
6643 .addClass( 'oo-ui-fieldLayout-help-content' )
6645 this.$help
= this.popupButtonWidget
.$element
;
6647 this.$help
= this.$( [] );
6651 if ( this.fieldWidget
instanceof OO
.ui
.InputWidget
) {
6652 this.$label
.on( 'click', this.onLabelClick
.bind( this ) );
6654 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
6657 this.$element
.addClass( 'oo-ui-fieldLayout' );
6659 .addClass( 'oo-ui-fieldLayout-field' )
6660 .toggleClass( 'oo-ui-fieldLayout-disable', this.fieldWidget
.isDisabled() )
6661 .append( this.fieldWidget
.$element
);
6662 this.setAlignment( config
.align
);
6667 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6668 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6673 * Handle field disable events.
6675 * @param {boolean} value Field is disabled
6677 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6678 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6682 * Handle label mouse click events.
6684 * @param {jQuery.Event} e Mouse click event
6686 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6687 this.fieldWidget
.simulateLabelClick();
6694 * @return {OO.ui.Widget} Field widget
6696 OO
.ui
.FieldLayout
.prototype.getField = function () {
6697 return this.fieldWidget
;
6701 * Set the field alignment mode.
6703 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6706 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6707 if ( value
!== this.align
) {
6708 // Default to 'left'
6709 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6713 if ( value
=== 'inline' ) {
6714 this.$element
.append( this.$field
, this.$label
, this.$help
);
6716 this.$element
.append( this.$help
, this.$label
, this.$field
);
6718 // Set classes. The following classes can be used here:
6719 // * oo-ui-fieldLayout-align-left
6720 // * oo-ui-fieldLayout-align-right
6721 // * oo-ui-fieldLayout-align-top
6722 // * oo-ui-fieldLayout-align-inline
6724 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6726 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
6734 * Layout made of a fieldset and optional legend.
6736 * Just add OO.ui.FieldLayout items.
6739 * @extends OO.ui.Layout
6740 * @mixins OO.ui.LabelElement
6741 * @mixins OO.ui.IconElement
6742 * @mixins OO.ui.GroupElement
6745 * @param {Object} [config] Configuration options
6746 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6748 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6749 // Config initialization
6750 config
= config
|| {};
6752 // Parent constructor
6753 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6755 // Mixin constructors
6756 OO
.ui
.IconElement
.call( this, config
);
6757 OO
.ui
.LabelElement
.call( this, config
);
6758 OO
.ui
.GroupElement
.call( this, config
);
6762 .addClass( 'oo-ui-fieldsetLayout' )
6763 .prepend( this.$icon
, this.$label
, this.$group
);
6764 if ( $.isArray( config
.items
) ) {
6765 this.addItems( config
.items
);
6771 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6772 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6773 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6774 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6777 * Layout with an HTML form.
6780 * @extends OO.ui.Layout
6783 * @param {Object} [config] Configuration options
6785 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6786 // Configuration initialization
6787 config
= config
|| {};
6789 // Parent constructor
6790 OO
.ui
.FormLayout
.super.call( this, config
);
6793 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
6796 this.$element
.addClass( 'oo-ui-formLayout' );
6801 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6809 /* Static Properties */
6811 OO
.ui
.FormLayout
.static.tagName
= 'form';
6816 * Handle form submit events.
6818 * @param {jQuery.Event} e Submit event
6821 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6822 this.emit( 'submit' );
6827 * Layout made of proportionally sized columns and rows.
6830 * @extends OO.ui.Layout
6833 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6834 * @param {Object} [config] Configuration options
6835 * @cfg {number[]} [widths] Widths of columns as ratios
6836 * @cfg {number[]} [heights] Heights of rows as ratios
6838 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6841 // Config initialization
6842 config
= config
|| {};
6844 // Parent constructor
6845 OO
.ui
.GridLayout
.super.call( this, config
);
6853 this.$element
.addClass( 'oo-ui-gridLayout' );
6854 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6855 this.panels
.push( panels
[i
] );
6856 this.$element
.append( panels
[i
].$element
);
6858 if ( config
.widths
|| config
.heights
) {
6859 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6861 // Arrange in columns by default
6862 widths
= this.panels
.map( function () { return 1; } );
6863 this.layout( widths
, [ 1 ] );
6869 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6884 * Set grid dimensions.
6886 * @param {number[]} widths Widths of columns as ratios
6887 * @param {number[]} heights Heights of rows as ratios
6889 * @throws {Error} If grid is not large enough to fit all panels
6891 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6895 cols
= widths
.length
,
6896 rows
= heights
.length
;
6898 // Verify grid is big enough to fit panels
6899 if ( cols
* rows
< this.panels
.length
) {
6900 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6903 // Sum up denominators
6904 for ( x
= 0; x
< cols
; x
++ ) {
6907 for ( y
= 0; y
< rows
; y
++ ) {
6913 for ( x
= 0; x
< cols
; x
++ ) {
6914 this.widths
[x
] = widths
[x
] / xd
;
6916 for ( y
= 0; y
< rows
; y
++ ) {
6917 this.heights
[y
] = heights
[y
] / yd
;
6921 this.emit( 'layout' );
6925 * Update panel positions and sizes.
6929 OO
.ui
.GridLayout
.prototype.update = function () {
6930 var x
, y
, panel
, width
, height
, dimensions
,
6934 cols
= this.widths
.length
,
6935 rows
= this.heights
.length
;
6937 for ( y
= 0; y
< rows
; y
++ ) {
6938 height
= this.heights
[y
];
6939 for ( x
= 0; x
< cols
; x
++ ) {
6940 width
= this.widths
[x
];
6941 panel
= this.panels
[i
];
6943 width
: Math
.round( width
* 100 ) + '%',
6944 height
: Math
.round( height
* 100 ) + '%',
6945 top
: Math
.round( top
* 100 ) + '%'
6948 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6949 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6951 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6953 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6954 if ( width
=== 0 || height
=== 0 ) {
6955 dimensions
.visibility
= 'hidden';
6957 panel
.$element
.css( dimensions
);
6965 this.emit( 'update' );
6969 * Get a panel at a given position.
6971 * The x and y position is affected by the current grid layout.
6973 * @param {number} x Horizontal position
6974 * @param {number} y Vertical position
6975 * @return {OO.ui.PanelLayout} The panel at the given postion
6977 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
6978 return this.panels
[ ( x
* this.widths
.length
) + y
];
6982 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
6985 * @extends OO.ui.Layout
6988 * @param {Object} [config] Configuration options
6989 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
6990 * @cfg {boolean} [padded=false] Pad the content from the edges
6991 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
6993 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
6994 // Config initialization
6995 config
= $.extend( {
7001 // Parent constructor
7002 OO
.ui
.PanelLayout
.super.call( this, config
);
7005 this.$element
.addClass( 'oo-ui-panelLayout' );
7006 if ( config
.scrollable
) {
7007 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
7009 if ( config
.padded
) {
7010 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
7012 if ( config
.expanded
) {
7013 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
7019 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
7022 * Page within an booklet layout.
7025 * @extends OO.ui.PanelLayout
7028 * @param {string} name Unique symbolic name of page
7029 * @param {Object} [config] Configuration options
7030 * @param {string} [outlineItem] Outline item widget
7032 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
7033 // Configuration initialization
7034 config
= $.extend( { scrollable
: true }, config
);
7036 // Parent constructor
7037 OO
.ui
.PageLayout
.super.call( this, config
);
7041 this.outlineItem
= config
.outlineItem
|| null;
7042 this.active
= false;
7045 this.$element
.addClass( 'oo-ui-pageLayout' );
7050 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
7056 * @param {boolean} active Page is active
7064 * @return {string} Symbolic name of page
7066 OO
.ui
.PageLayout
.prototype.getName = function () {
7071 * Check if page is active.
7073 * @return {boolean} Page is active
7075 OO
.ui
.PageLayout
.prototype.isActive = function () {
7082 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
7084 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
7085 return this.outlineItem
;
7091 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
7092 * outline item as desired; this method is called for setting (with an object) and unsetting
7093 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
7094 * operating on null instead of an OO.ui.OutlineItemWidget object.
7096 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
7099 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
7100 this.outlineItem
= outlineItem
|| null;
7101 if ( outlineItem
) {
7102 this.setupOutlineItem();
7108 * Setup outline item.
7110 * @localdoc Subclasses should override this method to adjust the outline item as desired.
7112 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
7115 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
7120 * Set page active state.
7122 * @param {boolean} Page is active
7125 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
7128 if ( active
!== this.active
) {
7129 this.active
= active
;
7130 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
7131 this.emit( 'active', this.active
);
7136 * Layout containing a series of mutually exclusive pages.
7139 * @extends OO.ui.PanelLayout
7140 * @mixins OO.ui.GroupElement
7143 * @param {Object} [config] Configuration options
7144 * @cfg {boolean} [continuous=false] Show all pages, one after another
7145 * @cfg {string} [icon=''] Symbolic icon name
7146 * @cfg {OO.ui.Layout[]} [items] Layouts to add
7148 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
7149 // Config initialization
7150 config
= $.extend( { scrollable
: true }, config
);
7152 // Parent constructor
7153 OO
.ui
.StackLayout
.super.call( this, config
);
7155 // Mixin constructors
7156 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7159 this.currentItem
= null;
7160 this.continuous
= !!config
.continuous
;
7163 this.$element
.addClass( 'oo-ui-stackLayout' );
7164 if ( this.continuous
) {
7165 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
7167 if ( $.isArray( config
.items
) ) {
7168 this.addItems( config
.items
);
7174 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
7175 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
7181 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
7187 * Get the current item.
7189 * @return {OO.ui.Layout|null}
7191 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
7192 return this.currentItem
;
7196 * Unset the current item.
7199 * @param {OO.ui.StackLayout} layout
7202 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7203 var prevItem
= this.currentItem
;
7204 if ( prevItem
=== null ) {
7208 this.currentItem
= null;
7209 this.emit( 'set', null );
7215 * Adding an existing item (by value) will move it.
7217 * @param {OO.ui.Layout[]} items Items to add
7218 * @param {number} [index] Index to insert items after
7221 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7223 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7225 if ( !this.currentItem
&& items
.length
) {
7226 this.setItem( items
[0] );
7235 * Items will be detached, not removed, so they can be used later.
7237 * @param {OO.ui.Layout[]} items Items to remove
7241 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7243 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7245 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7246 if ( this.items
.length
) {
7247 this.setItem( this.items
[0] );
7249 this.unsetCurrentItem();
7259 * Items will be detached, not removed, so they can be used later.
7264 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7265 this.unsetCurrentItem();
7266 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7274 * Any currently shown item will be hidden.
7276 * FIXME: If the passed item to show has not been added in the items list, then
7277 * this method drops it and unsets the current item.
7279 * @param {OO.ui.Layout} item Item to show
7283 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7286 if ( item
!== this.currentItem
) {
7287 if ( !this.continuous
) {
7288 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7289 this.items
[i
].$element
.css( 'display', '' );
7292 if ( $.inArray( item
, this.items
) !== -1 ) {
7293 if ( !this.continuous
) {
7294 item
.$element
.css( 'display', 'block' );
7296 this.currentItem
= item
;
7297 this.emit( 'set', item
);
7299 this.unsetCurrentItem();
7307 * Horizontal bar layout of tools as icon buttons.
7310 * @extends OO.ui.ToolGroup
7313 * @param {OO.ui.Toolbar} toolbar
7314 * @param {Object} [config] Configuration options
7316 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7317 // Parent constructor
7318 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7321 this.$element
.addClass( 'oo-ui-barToolGroup' );
7326 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7328 /* Static Properties */
7330 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7332 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7334 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7337 * Popup list of tools with an icon and optional label.
7341 * @extends OO.ui.ToolGroup
7342 * @mixins OO.ui.IconElement
7343 * @mixins OO.ui.IndicatorElement
7344 * @mixins OO.ui.LabelElement
7345 * @mixins OO.ui.TitledElement
7346 * @mixins OO.ui.ClippableElement
7349 * @param {OO.ui.Toolbar} toolbar
7350 * @param {Object} [config] Configuration options
7351 * @cfg {string} [header] Text to display at the top of the pop-up
7353 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7354 // Configuration initialization
7355 config
= config
|| {};
7357 // Parent constructor
7358 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7360 // Mixin constructors
7361 OO
.ui
.IconElement
.call( this, config
);
7362 OO
.ui
.IndicatorElement
.call( this, config
);
7363 OO
.ui
.LabelElement
.call( this, config
);
7364 OO
.ui
.TitledElement
.call( this, config
);
7365 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7368 this.active
= false;
7369 this.dragging
= false;
7370 this.onBlurHandler
= this.onBlur
.bind( this );
7371 this.$handle
= this.$( '<span>' );
7375 'mousedown touchstart': this.onHandlePointerDown
.bind( this ),
7376 'mouseup touchend': this.onHandlePointerUp
.bind( this )
7381 .addClass( 'oo-ui-popupToolGroup-handle' )
7382 .append( this.$icon
, this.$label
, this.$indicator
);
7383 // If the pop-up should have a header, add it to the top of the toolGroup.
7384 // Note: If this feature is useful for other widgets, we could abstract it into an
7385 // OO.ui.HeaderedElement mixin constructor.
7386 if ( config
.header
!== undefined ) {
7388 .prepend( this.$( '<span>' )
7389 .addClass( 'oo-ui-popupToolGroup-header' )
7390 .text( config
.header
)
7394 .addClass( 'oo-ui-popupToolGroup' )
7395 .prepend( this.$handle
);
7400 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7401 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7402 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7403 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7404 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7405 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7407 /* Static Properties */
7414 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7416 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7418 if ( this.isDisabled() && this.isElementAttached() ) {
7419 this.setActive( false );
7424 * Handle focus being lost.
7426 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7428 * @param {jQuery.Event} e Mouse up event
7430 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7431 // Only deactivate when clicking outside the dropdown element
7432 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7433 this.setActive( false );
7440 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7441 // e.which is 0 for touch events, 1 for left mouse button
7442 if ( !this.isDisabled() && e
.which
<= 1 ) {
7443 this.setActive( false );
7445 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7449 * Handle mouse up events.
7451 * @param {jQuery.Event} e Mouse up event
7453 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7458 * Handle mouse down events.
7460 * @param {jQuery.Event} e Mouse down event
7462 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7463 // e.which is 0 for touch events, 1 for left mouse button
7464 if ( !this.isDisabled() && e
.which
<= 1 ) {
7465 this.setActive( !this.active
);
7471 * Switch into active mode.
7473 * When active, mouseup events anywhere in the document will trigger deactivation.
7475 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7477 if ( this.active
!== value
) {
7478 this.active
= value
;
7480 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7482 // Try anchoring the popup to the left first
7483 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7484 this.toggleClipping( true );
7485 if ( this.isClippedHorizontally() ) {
7486 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7487 this.toggleClipping( false );
7489 .removeClass( 'oo-ui-popupToolGroup-left' )
7490 .addClass( 'oo-ui-popupToolGroup-right' );
7491 this.toggleClipping( true );
7494 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7495 this.$element
.removeClass(
7496 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7498 this.toggleClipping( false );
7504 * Drop down list layout of tools as labeled icon buttons.
7506 * This layout allows some tools to be collapsible, controlled by a "More" / "Fewer" option at the
7507 * bottom of the main list. These are not automatically positioned at the bottom of the list; you
7508 * may want to use the 'promote' and 'demote' configuration options to achieve this.
7511 * @extends OO.ui.PopupToolGroup
7514 * @param {OO.ui.Toolbar} toolbar
7515 * @param {Object} [config] Configuration options
7516 * @cfg {Array} [allowCollapse] List of tools that can be collapsed. Remaining tools will be always
7518 * @cfg {Array} [forceExpand] List of tools that *may not* be collapsed. All remaining tools will be
7519 * allowed to be collapsed.
7520 * @cfg {boolean} [expanded=false] Whether the collapsible tools are expanded by default
7522 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7523 // Properties (must be set before parent constructor, which calls #populate)
7524 this.allowCollapse
= config
.allowCollapse
;
7525 this.forceExpand
= config
.forceExpand
;
7526 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
7527 this.collapsibleTools
= [];
7529 // Parent constructor
7530 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7533 this.$element
.addClass( 'oo-ui-listToolGroup' );
7538 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7540 /* Static Properties */
7542 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7544 OO
.ui
.ListToolGroup
.static.name
= 'list';
7551 OO
.ui
.ListToolGroup
.prototype.populate = function () {
7552 var i
, len
, allowCollapse
= [];
7554 OO
.ui
.ListToolGroup
.super.prototype.populate
.call( this );
7556 // Update the list of collapsible tools
7557 if ( this.allowCollapse
!== undefined ) {
7558 allowCollapse
= this.allowCollapse
;
7559 } else if ( this.forceExpand
!== undefined ) {
7560 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
7563 this.collapsibleTools
= [];
7564 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
7565 if ( this.tools
[ allowCollapse
[i
] ] !== undefined ) {
7566 this.collapsibleTools
.push( this.tools
[ allowCollapse
[i
] ] );
7570 // Keep at the end, even when tools are added
7571 this.$group
.append( this.getExpandCollapseTool().$element
);
7573 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
7575 // Calling jQuery's .hide() and then .show() on a detached element caches the default value of its
7576 // 'display' attribute and restores it, and the tool uses a <span> and can be hidden and re-shown.
7577 // Is this a jQuery bug? http://jsfiddle.net/gtj4hu3h/
7578 if ( this.getExpandCollapseTool().$element
.css( 'display' ) === 'inline' ) {
7579 this.getExpandCollapseTool().$element
.css( 'display', 'inline-block' );
7582 this.updateCollapsibleState();
7585 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
7586 if ( this.expandCollapseTool
=== undefined ) {
7587 var ExpandCollapseTool = function () {
7588 ExpandCollapseTool
.super.apply( this, arguments
);
7591 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
7593 ExpandCollapseTool
.prototype.onSelect = function () {
7594 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
7595 this.toolGroup
.updateCollapsibleState();
7596 this.setActive( false );
7598 ExpandCollapseTool
.prototype.onUpdateState = function () {
7599 // Do nothing. Tool interface requires an implementation of this function.
7602 ExpandCollapseTool
.static.name
= 'more-fewer';
7604 this.expandCollapseTool
= new ExpandCollapseTool( this );
7606 return this.expandCollapseTool
;
7612 OO
.ui
.ListToolGroup
.prototype.onPointerUp = function ( e
) {
7613 var ret
= OO
.ui
.ListToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7615 // Do not close the popup when the user wants to show more/fewer tools
7616 if ( this.$( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
) {
7617 // Prevent the popup list from being hidden
7618 this.setActive( true );
7624 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
7627 this.getExpandCollapseTool()
7628 .setIcon( this.expanded
? 'collapse' : 'expand' )
7629 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
7631 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
7632 this.collapsibleTools
[i
].toggle( this.expanded
);
7637 * Drop down menu layout of tools as selectable menu items.
7640 * @extends OO.ui.PopupToolGroup
7643 * @param {OO.ui.Toolbar} toolbar
7644 * @param {Object} [config] Configuration options
7646 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7647 // Configuration initialization
7648 config
= config
|| {};
7650 // Parent constructor
7651 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7654 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7657 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7662 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7664 /* Static Properties */
7666 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7668 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7673 * Handle the toolbar state being updated.
7675 * When the state changes, the title of each active item in the menu will be joined together and
7676 * used as a label for the group. The label will be empty if none of the items are active.
7678 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7682 for ( name
in this.tools
) {
7683 if ( this.tools
[name
].isActive() ) {
7684 labelTexts
.push( this.tools
[name
].getTitle() );
7688 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7692 * Tool that shows a popup when selected.
7696 * @extends OO.ui.Tool
7697 * @mixins OO.ui.PopupElement
7700 * @param {OO.ui.Toolbar} toolbar
7701 * @param {Object} [config] Configuration options
7703 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7704 // Parent constructor
7705 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7707 // Mixin constructors
7708 OO
.ui
.PopupElement
.call( this, config
);
7712 .addClass( 'oo-ui-popupTool' )
7713 .append( this.popup
.$element
);
7718 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7719 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7724 * Handle the tool being selected.
7728 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7729 if ( !this.isDisabled() ) {
7730 this.popup
.toggle();
7732 this.setActive( false );
7737 * Handle the toolbar state being updated.
7741 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7742 this.setActive( false );
7746 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7748 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7752 * @extends OO.ui.GroupElement
7755 * @param {Object} [config] Configuration options
7757 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7758 // Parent constructor
7759 OO
.ui
.GroupWidget
.super.call( this, config
);
7764 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7769 * Set the disabled state of the widget.
7771 * This will also update the disabled state of child widgets.
7773 * @param {boolean} disabled Disable widget
7776 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7780 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7781 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7783 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7785 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7786 this.items
[i
].updateDisabled();
7794 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7796 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7797 * allows bidrectional communication.
7799 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7806 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7813 * Check if widget is disabled.
7815 * Checks parent if present, making disabled state inheritable.
7817 * @return {boolean} Widget is disabled
7819 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7820 return this.disabled
||
7821 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7825 * Set group element is in.
7827 * @param {OO.ui.GroupElement|null} group Group element, null if none
7830 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7832 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7833 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7835 // Initialize item disabled states
7836 this.updateDisabled();
7842 * Mixin that adds a menu showing suggested values for a text input.
7844 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7850 * @param {OO.ui.TextInputWidget} input Input widget
7851 * @param {Object} [config] Configuration options
7852 * @cfg {jQuery} [$overlay] Overlay layer; defaults to the current window's overlay.
7854 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7855 // Config intialization
7856 config
= config
|| {};
7859 this.lookupInput
= input
;
7860 this.$overlay
= config
.$overlay
|| ( this.$.$iframe
|| this.$element
).closest( '.oo-ui-window' ).children( '.oo-ui-window-overlay' );
7861 if ( this.$overlay
.length
=== 0 ) {
7862 this.$overlay
= this.$( 'body' );
7864 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7865 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7866 input
: this.lookupInput
,
7867 $container
: config
.$container
7869 this.lookupCache
= {};
7870 this.lookupQuery
= null;
7871 this.lookupRequest
= null;
7872 this.populating
= false;
7875 this.$overlay
.append( this.lookupMenu
.$element
);
7877 this.lookupInput
.$input
.on( {
7878 focus
: this.onLookupInputFocus
.bind( this ),
7879 blur
: this.onLookupInputBlur
.bind( this ),
7880 mousedown
: this.onLookupInputMouseDown
.bind( this )
7882 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7885 this.$element
.addClass( 'oo-ui-lookupWidget' );
7886 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7892 * Handle input focus event.
7894 * @param {jQuery.Event} e Input focus event
7896 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7897 this.openLookupMenu();
7901 * Handle input blur event.
7903 * @param {jQuery.Event} e Input blur event
7905 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7906 this.lookupMenu
.toggle( false );
7910 * Handle input mouse down event.
7912 * @param {jQuery.Event} e Input mouse down event
7914 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7915 this.openLookupMenu();
7919 * Handle input change event.
7921 * @param {string} value New input value
7923 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7924 this.openLookupMenu();
7930 * @return {OO.ui.TextInputMenuWidget}
7932 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
7933 return this.lookupMenu
;
7941 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
7942 var value
= this.lookupInput
.getValue();
7944 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
7945 this.populateLookupMenu();
7946 this.lookupMenu
.toggle( true );
7957 * Populate lookup menu with current information.
7961 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
7964 if ( !this.populating
) {
7965 this.populating
= true;
7966 this.getLookupMenuItems()
7967 .done( function ( items
) {
7968 widget
.lookupMenu
.clearItems();
7969 if ( items
.length
) {
7973 widget
.initializeLookupMenuSelection();
7974 widget
.openLookupMenu();
7976 widget
.lookupMenu
.toggle( true );
7978 widget
.populating
= false;
7980 .fail( function () {
7981 widget
.lookupMenu
.clearItems();
7982 widget
.populating
= false;
7990 * Set selection in the lookup menu with current information.
7994 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
7995 if ( !this.lookupMenu
.getSelectedItem() ) {
7996 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
7998 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
8002 * Get lookup menu items for the current query.
8004 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
8007 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
8009 value
= this.lookupInput
.getValue(),
8010 deferred
= $.Deferred();
8012 if ( value
&& value
!== this.lookupQuery
) {
8013 // Abort current request if query has changed
8014 if ( this.lookupRequest
) {
8015 this.lookupRequest
.abort();
8016 this.lookupQuery
= null;
8017 this.lookupRequest
= null;
8019 if ( value
in this.lookupCache
) {
8020 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
8022 this.lookupQuery
= value
;
8023 this.lookupRequest
= this.getLookupRequest()
8024 .always( function () {
8025 widget
.lookupQuery
= null;
8026 widget
.lookupRequest
= null;
8028 .done( function ( data
) {
8029 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
8030 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
8032 .fail( function () {
8036 this.lookupRequest
.always( function () {
8037 widget
.popPending();
8041 return deferred
.promise();
8045 * Get a new request object of the current lookup query value.
8048 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
8050 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
8051 // Stub, implemented in subclass
8056 * Handle successful lookup request.
8058 * Overriding methods should call #populateLookupMenu when results are available and cache results
8059 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
8062 * @param {Mixed} data Response from server
8064 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
8065 // Stub, implemented in subclass
8069 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
8072 * @param {Mixed} data Cached result data, usually an array
8073 * @return {OO.ui.MenuItemWidget[]} Menu items
8075 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
8076 // Stub, implemented in subclass
8081 * Set of controls for an OO.ui.OutlineWidget.
8083 * Controls include moving items up and down, removing items, and adding different kinds of items.
8086 * @extends OO.ui.Widget
8087 * @mixins OO.ui.GroupElement
8088 * @mixins OO.ui.IconElement
8091 * @param {OO.ui.OutlineWidget} outline Outline to control
8092 * @param {Object} [config] Configuration options
8094 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
8095 // Configuration initialization
8096 config
= $.extend( { icon
: 'add' }, config
);
8098 // Parent constructor
8099 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
8101 // Mixin constructors
8102 OO
.ui
.GroupElement
.call( this, config
);
8103 OO
.ui
.IconElement
.call( this, config
);
8106 this.outline
= outline
;
8107 this.$movers
= this.$( '<div>' );
8108 this.upButton
= new OO
.ui
.ButtonWidget( {
8112 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
8114 this.downButton
= new OO
.ui
.ButtonWidget( {
8118 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
8120 this.removeButton
= new OO
.ui
.ButtonWidget( {
8124 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
8128 outline
.connect( this, {
8129 select
: 'onOutlineChange',
8130 add
: 'onOutlineChange',
8131 remove
: 'onOutlineChange'
8133 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
8134 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
8135 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
8138 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
8139 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
8141 .addClass( 'oo-ui-outlineControlsWidget-movers' )
8142 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
8143 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
8148 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
8149 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
8150 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
8156 * @param {number} places Number of places to move
8166 * Handle outline change events.
8168 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
8169 var i
, len
, firstMovable
, lastMovable
,
8170 items
= this.outline
.getItems(),
8171 selectedItem
= this.outline
.getSelectedItem(),
8172 movable
= selectedItem
&& selectedItem
.isMovable(),
8173 removable
= selectedItem
&& selectedItem
.isRemovable();
8178 while ( ++i
< len
) {
8179 if ( items
[i
].isMovable() ) {
8180 firstMovable
= items
[i
];
8186 if ( items
[i
].isMovable() ) {
8187 lastMovable
= items
[i
];
8192 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
8193 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
8194 this.removeButton
.setDisabled( !removable
);
8198 * Mixin for widgets with a boolean on/off state.
8204 * @param {Object} [config] Configuration options
8205 * @cfg {boolean} [value=false] Initial value
8207 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8208 // Configuration initialization
8209 config
= config
|| {};
8215 this.$element
.addClass( 'oo-ui-toggleWidget' );
8216 this.setValue( !!config
.value
);
8223 * @param {boolean} value Changed value
8229 * Get the value of the toggle.
8233 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8238 * Set the value of the toggle.
8240 * @param {boolean} value New value
8244 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8246 if ( this.value
!== value
) {
8248 this.emit( 'change', value
);
8249 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8250 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8256 * Group widget for multiple related buttons.
8258 * Use together with OO.ui.ButtonWidget.
8261 * @extends OO.ui.Widget
8262 * @mixins OO.ui.GroupElement
8265 * @param {Object} [config] Configuration options
8266 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
8268 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
8269 // Parent constructor
8270 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
8272 // Mixin constructors
8273 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
8276 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
8277 if ( $.isArray( config
.items
) ) {
8278 this.addItems( config
.items
);
8284 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
8285 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
8288 * Generic widget for buttons.
8291 * @extends OO.ui.Widget
8292 * @mixins OO.ui.ButtonElement
8293 * @mixins OO.ui.IconElement
8294 * @mixins OO.ui.IndicatorElement
8295 * @mixins OO.ui.LabelElement
8296 * @mixins OO.ui.TitledElement
8297 * @mixins OO.ui.FlaggedElement
8300 * @param {Object} [config] Configuration options
8301 * @cfg {string} [href] Hyperlink to visit when clicked
8302 * @cfg {string} [target] Target to open hyperlink in
8304 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8305 // Configuration initialization
8306 config
= $.extend( { target
: '_blank' }, config
);
8308 // Parent constructor
8309 OO
.ui
.ButtonWidget
.super.call( this, config
);
8311 // Mixin constructors
8312 OO
.ui
.ButtonElement
.call( this, config
);
8313 OO
.ui
.IconElement
.call( this, config
);
8314 OO
.ui
.IndicatorElement
.call( this, config
);
8315 OO
.ui
.LabelElement
.call( this, config
);
8316 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
8317 OO
.ui
.FlaggedElement
.call( this, config
);
8322 this.isHyperlink
= false;
8326 click
: this.onClick
.bind( this ),
8327 keypress
: this.onKeyPress
.bind( this )
8331 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8333 .addClass( 'oo-ui-buttonWidget' )
8334 .append( this.$button
);
8335 this.setHref( config
.href
);
8336 this.setTarget( config
.target
);
8341 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8342 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8343 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8344 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8345 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8346 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8347 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8358 * Handles mouse click events.
8360 * @param {jQuery.Event} e Mouse click event
8363 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8364 if ( !this.isDisabled() ) {
8365 this.emit( 'click' );
8366 if ( this.isHyperlink
) {
8374 * Handles keypress events.
8376 * @param {jQuery.Event} e Keypress event
8379 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8380 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8382 if ( this.isHyperlink
) {
8390 * Get hyperlink location.
8392 * @return {string} Hyperlink location
8394 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8399 * Get hyperlink target.
8401 * @return {string} Hyperlink target
8403 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8408 * Set hyperlink location.
8410 * @param {string|null} href Hyperlink location, null to remove
8412 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8413 href
= typeof href
=== 'string' ? href
: null;
8415 if ( href
!== this.href
) {
8417 if ( href
!== null ) {
8418 this.$button
.attr( 'href', href
);
8419 this.isHyperlink
= true;
8421 this.$button
.removeAttr( 'href' );
8422 this.isHyperlink
= false;
8430 * Set hyperlink target.
8432 * @param {string|null} target Hyperlink target, null to remove
8434 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8435 target
= typeof target
=== 'string' ? target
: null;
8437 if ( target
!== this.target
) {
8438 this.target
= target
;
8439 if ( target
!== null ) {
8440 this.$button
.attr( 'target', target
);
8442 this.$button
.removeAttr( 'target' );
8450 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8453 * @extends OO.ui.ButtonWidget
8454 * @mixins OO.ui.PendingElement
8457 * @param {Object} [config] Configuration options
8458 * @cfg {string} [action] Symbolic action name
8459 * @cfg {string[]} [modes] Symbolic mode names
8460 * @cfg {boolean} [framed=false] Render button with a frame
8462 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8463 // Config intialization
8464 config
= $.extend( { framed
: false }, config
);
8466 // Parent constructor
8467 OO
.ui
.ActionWidget
.super.call( this, config
);
8469 // Mixin constructors
8470 OO
.ui
.PendingElement
.call( this, config
);
8473 this.action
= config
.action
|| '';
8474 this.modes
= config
.modes
|| [];
8479 this.$element
.addClass( 'oo-ui-actionWidget' );
8484 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8485 OO
.mixinClass( OO
.ui
.ActionWidget
, OO
.ui
.PendingElement
);
8496 * Check if action is available in a certain mode.
8498 * @param {string} mode Name of mode
8499 * @return {boolean} Has mode
8501 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8502 return this.modes
.indexOf( mode
) !== -1;
8506 * Get symbolic action name.
8510 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8515 * Get symbolic action name.
8519 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8520 return this.modes
.slice();
8524 * Emit a resize event if the size has changed.
8528 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8531 if ( this.isElementAttached() ) {
8532 width
= this.$element
.width();
8533 height
= this.$element
.height();
8535 if ( width
!== this.width
|| height
!== this.height
) {
8537 this.height
= height
;
8538 this.emit( 'resize' );
8548 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8550 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8551 this.propagateResize();
8559 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8561 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8562 this.propagateResize();
8570 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8572 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8573 this.propagateResize();
8581 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8583 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8584 this.propagateResize();
8590 * Toggle visibility of button.
8592 * @param {boolean} [show] Show button, omit to toggle visibility
8595 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8597 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8598 this.propagateResize();
8604 * Button that shows and hides a popup.
8607 * @extends OO.ui.ButtonWidget
8608 * @mixins OO.ui.PopupElement
8611 * @param {Object} [config] Configuration options
8613 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8614 // Parent constructor
8615 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8617 // Mixin constructors
8618 OO
.ui
.PopupElement
.call( this, config
);
8622 .addClass( 'oo-ui-popupButtonWidget' )
8623 .append( this.popup
.$element
);
8628 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8629 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8634 * Handles mouse click events.
8636 * @param {jQuery.Event} e Mouse click event
8638 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8639 // Skip clicks within the popup
8640 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8644 if ( !this.isDisabled() ) {
8645 this.popup
.toggle();
8647 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8653 * Button that toggles on and off.
8656 * @extends OO.ui.ButtonWidget
8657 * @mixins OO.ui.ToggleWidget
8660 * @param {Object} [config] Configuration options
8661 * @cfg {boolean} [value=false] Initial value
8663 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8664 // Configuration initialization
8665 config
= config
|| {};
8667 // Parent constructor
8668 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8670 // Mixin constructors
8671 OO
.ui
.ToggleWidget
.call( this, config
);
8674 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8679 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8680 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8687 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8688 if ( !this.isDisabled() ) {
8689 this.setValue( !this.value
);
8693 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8699 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8701 if ( value
!== this.value
) {
8702 this.setActive( value
);
8705 // Parent method (from mixin)
8706 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8714 * See OO.ui.IconElement for more information.
8717 * @extends OO.ui.Widget
8718 * @mixins OO.ui.IconElement
8719 * @mixins OO.ui.TitledElement
8722 * @param {Object} [config] Configuration options
8724 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8725 // Config intialization
8726 config
= config
|| {};
8728 // Parent constructor
8729 OO
.ui
.IconWidget
.super.call( this, config
);
8731 // Mixin constructors
8732 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
8733 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8736 this.$element
.addClass( 'oo-ui-iconWidget' );
8741 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8742 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
8743 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8745 /* Static Properties */
8747 OO
.ui
.IconWidget
.static.tagName
= 'span';
8752 * See OO.ui.IndicatorElement for more information.
8755 * @extends OO.ui.Widget
8756 * @mixins OO.ui.IndicatorElement
8757 * @mixins OO.ui.TitledElement
8760 * @param {Object} [config] Configuration options
8762 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8763 // Config intialization
8764 config
= config
|| {};
8766 // Parent constructor
8767 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8769 // Mixin constructors
8770 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
8771 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8774 this.$element
.addClass( 'oo-ui-indicatorWidget' );
8779 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
8780 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
8781 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
8783 /* Static Properties */
8785 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
8788 * Inline menu of options.
8790 * Inline menus provide a control for accessing a menu and compose a menu within the widget, which
8791 * can be accessed using the #getMenu method.
8793 * Use with OO.ui.MenuItemWidget.
8796 * @extends OO.ui.Widget
8797 * @mixins OO.ui.IconElement
8798 * @mixins OO.ui.IndicatorElement
8799 * @mixins OO.ui.LabelElement
8800 * @mixins OO.ui.TitledElement
8803 * @param {Object} [config] Configuration options
8804 * @cfg {Object} [menu] Configuration options to pass to menu widget
8806 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
8807 // Configuration initialization
8808 config
= $.extend( { indicator
: 'down' }, config
);
8810 // Parent constructor
8811 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
8813 // Mixin constructors
8814 OO
.ui
.IconElement
.call( this, config
);
8815 OO
.ui
.IndicatorElement
.call( this, config
);
8816 OO
.ui
.LabelElement
.call( this, config
);
8817 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8820 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8821 this.$handle
= this.$( '<span>' );
8824 this.$element
.on( { click
: this.onClick
.bind( this ) } );
8825 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8829 .addClass( 'oo-ui-inlineMenuWidget-handle' )
8830 .append( this.$icon
, this.$label
, this.$indicator
);
8832 .addClass( 'oo-ui-inlineMenuWidget' )
8833 .append( this.$handle
, this.menu
.$element
);
8838 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
8839 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconElement
);
8840 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatorElement
);
8841 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabelElement
);
8842 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
8849 * @return {OO.ui.MenuWidget} Menu of widget
8851 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
8856 * Handles menu select events.
8858 * @param {OO.ui.MenuItemWidget} item Selected menu item
8860 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
8867 selectedLabel
= item
.getLabel();
8869 // If the label is a DOM element, clone it, because setLabel will append() it
8870 if ( selectedLabel
instanceof jQuery
) {
8871 selectedLabel
= selectedLabel
.clone();
8874 this.setLabel( selectedLabel
);
8878 * Handles mouse click events.
8880 * @param {jQuery.Event} e Mouse click event
8882 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
8883 // Skip clicks within the menu
8884 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8888 if ( !this.isDisabled() ) {
8889 if ( this.menu
.isVisible() ) {
8890 this.menu
.toggle( false );
8892 this.menu
.toggle( true );
8899 * Base class for input widgets.
8903 * @extends OO.ui.Widget
8904 * @mixins OO.ui.FlaggedElement
8907 * @param {Object} [config] Configuration options
8908 * @cfg {string} [name=''] HTML input name
8909 * @cfg {string} [value=''] Input value
8910 * @cfg {boolean} [readOnly=false] Prevent changes
8911 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
8913 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8914 // Config intialization
8915 config
= $.extend( { readOnly
: false }, config
);
8917 // Parent constructor
8918 OO
.ui
.InputWidget
.super.call( this, config
);
8920 // Mixin constructors
8921 OO
.ui
.FlaggedElement
.call( this, config
);
8924 this.$input
= this.getInputElement( config
);
8926 this.readOnly
= false;
8927 this.inputFilter
= config
.inputFilter
;
8930 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
8934 .attr( 'name', config
.name
)
8935 .prop( 'disabled', this.isDisabled() );
8936 this.setReadOnly( config
.readOnly
);
8937 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
8938 this.setValue( config
.value
);
8943 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8944 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.FlaggedElement
);
8956 * Get input element.
8958 * @param {Object} [config] Configuration options
8959 * @return {jQuery} Input element
8961 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8962 return this.$( '<input>' );
8966 * Handle potentially value-changing events.
8968 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8970 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8972 if ( !this.isDisabled() ) {
8973 // Allow the stack to clear so the value will be updated
8974 setTimeout( function () {
8975 widget
.setValue( widget
.$input
.val() );
8981 * Get the value of the input.
8983 * @return {string} Input value
8985 OO
.ui
.InputWidget
.prototype.getValue = function () {
8990 * Sets the direction of the current input, either RTL or LTR
8992 * @param {boolean} isRTL
8994 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
8996 this.$input
.removeClass( 'oo-ui-ltr' );
8997 this.$input
.addClass( 'oo-ui-rtl' );
8999 this.$input
.removeClass( 'oo-ui-rtl' );
9000 this.$input
.addClass( 'oo-ui-ltr' );
9005 * Set the value of the input.
9007 * @param {string} value New value
9011 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
9012 value
= this.sanitizeValue( value
);
9013 if ( this.value
!== value
) {
9015 this.emit( 'change', this.value
);
9017 // Update the DOM if it has changed. Note that with sanitizeValue, it
9018 // is possible for the DOM value to change without this.value changing.
9019 if ( this.$input
.val() !== this.value
) {
9020 this.$input
.val( this.value
);
9026 * Sanitize incoming value.
9028 * Ensures value is a string, and converts undefined and null to empty strings.
9030 * @param {string} value Original value
9031 * @return {string} Sanitized value
9033 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
9034 if ( value
=== undefined || value
=== null ) {
9036 } else if ( this.inputFilter
) {
9037 return this.inputFilter( String( value
) );
9039 return String( value
);
9044 * Simulate the behavior of clicking on a label bound to this input.
9046 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
9047 if ( !this.isDisabled() ) {
9048 if ( this.$input
.is( ':checkbox,:radio' ) ) {
9049 this.$input
.click();
9050 } else if ( this.$input
.is( ':input' ) ) {
9051 this.$input
[0].focus();
9057 * Check if the widget is read-only.
9061 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
9062 return this.readOnly
;
9066 * Set the read-only state of the widget.
9068 * This should probably change the widgets's appearance and prevent it from being used.
9070 * @param {boolean} state Make input read-only
9073 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
9074 this.readOnly
= !!state
;
9075 this.$input
.prop( 'readOnly', this.readOnly
);
9082 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
9083 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
9084 if ( this.$input
) {
9085 this.$input
.prop( 'disabled', this.isDisabled() );
9095 OO
.ui
.InputWidget
.prototype.focus = function () {
9096 this.$input
[0].focus();
9105 OO
.ui
.InputWidget
.prototype.blur = function () {
9106 this.$input
[0].blur();
9111 * A button that is an input widget. Intended to be used within FormLayouts.
9114 * @extends OO.ui.InputWidget
9115 * @mixins OO.ui.ButtonElement
9116 * @mixins OO.ui.IconElement
9117 * @mixins OO.ui.IndicatorElement
9118 * @mixins OO.ui.LabelElement
9119 * @mixins OO.ui.TitledElement
9120 * @mixins OO.ui.FlaggedElement
9123 * @param {Object} [config] Configuration options
9124 * @cfg {string} [type='button'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
9125 * @cfg {boolean} [useInputTag=false] Whether to use `<input/>` rather than `<button/>`. Only useful
9126 * if you need IE 6 support in a form with multiple buttons. By using this option, you sacrifice
9127 * icons and indicators, as well as the ability to have non-plaintext label or a label different
9130 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
9131 // Configuration initialization
9132 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
9134 // Parent constructor
9135 OO
.ui
.ButtonInputWidget
.super.call( this, config
);
9137 // Mixin constructors
9138 OO
.ui
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
9139 OO
.ui
.IconElement
.call( this, config
);
9140 OO
.ui
.IndicatorElement
.call( this, config
);
9141 OO
.ui
.LabelElement
.call( this, config
);
9142 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
9143 OO
.ui
.FlaggedElement
.call( this, config
);
9146 this.useInputTag
= config
.useInputTag
;
9150 click
: this.onClick
.bind( this ),
9151 keypress
: this.onKeyPress
.bind( this )
9155 if ( !config
.useInputTag
) {
9156 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
9158 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
9163 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
9164 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.ButtonElement
);
9165 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IconElement
);
9166 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.IndicatorElement
);
9167 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.LabelElement
);
9168 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.TitledElement
);
9169 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.FlaggedElement
);
9180 * Get input element.
9182 * @param {Object} [config] Configuration options
9183 * @return {jQuery} Input element
9185 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
9186 var html
= '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + config
.type
+ '">';
9187 return this.$( html
);
9193 * Overridden to support setting the 'value' of `<input/>` elements.
9195 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
9196 * text; or null for no label
9199 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
9200 OO
.ui
.LabelElement
.prototype.setLabel
.call( this, label
);
9202 if ( this.useInputTag
) {
9203 if ( typeof label
=== 'function' ) {
9204 label
= OO
.ui
.resolveMsg( label
);
9206 if ( label
instanceof jQuery
) {
9207 label
= label
.text();
9212 this.$input
.val( label
);
9219 * Handles mouse click events.
9221 * @param {jQuery.Event} e Mouse click event
9224 OO
.ui
.ButtonInputWidget
.prototype.onClick = function () {
9225 if ( !this.isDisabled() ) {
9226 this.emit( 'click' );
9232 * Handles keypress events.
9234 * @param {jQuery.Event} e Keypress event
9237 OO
.ui
.ButtonInputWidget
.prototype.onKeyPress = function ( e
) {
9238 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
9239 this.emit( 'click' );
9245 * Checkbox input widget.
9248 * @extends OO.ui.InputWidget
9251 * @param {Object} [config] Configuration options
9253 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
9254 // Parent constructor
9255 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
9258 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
9263 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
9268 * Get input element.
9270 * @return {jQuery} Input element
9272 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
9273 return this.$( '<input type="checkbox" />' );
9277 * Get checked state of the checkbox
9279 * @return {boolean} If the checkbox is checked
9281 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
9286 * Set checked state of the checkbox
9288 * @param {boolean} value New value
9290 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
9292 if ( this.value
!== value
) {
9294 this.$input
.prop( 'checked', this.value
);
9295 this.emit( 'change', this.value
);
9302 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
9304 if ( !this.isDisabled() ) {
9305 // Allow the stack to clear so the value will be updated
9306 setTimeout( function () {
9307 widget
.setValue( widget
.$input
.prop( 'checked' ) );
9313 * Input widget with a text field.
9316 * @extends OO.ui.InputWidget
9317 * @mixins OO.ui.IconElement
9318 * @mixins OO.ui.IndicatorElement
9319 * @mixins OO.ui.PendingElement
9322 * @param {Object} [config] Configuration options
9323 * @cfg {string} [placeholder] Placeholder text
9324 * @cfg {boolean} [multiline=false] Allow multiple lines of text
9325 * @cfg {boolean} [autosize=false] Automatically resize to fit content
9326 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
9327 * @cfg {RegExp|string} [validate] Regular expression (or symbolic name referencing
9328 * one, see #static-validationPatterns)
9330 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
9331 // Configuration initialization
9332 config
= config
|| {};
9334 // Parent constructor
9335 OO
.ui
.TextInputWidget
.super.call( this, config
);
9337 // Mixin constructors
9338 OO
.ui
.IconElement
.call( this, config
);
9339 OO
.ui
.IndicatorElement
.call( this, config
);
9340 OO
.ui
.PendingElement
.call( this, config
);
9343 this.multiline
= !!config
.multiline
;
9344 this.autosize
= !!config
.autosize
;
9345 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
9346 this.validate
= null;
9348 this.setValidation( config
.validate
);
9352 keypress
: this.onKeyPress
.bind( this ),
9353 blur
: this.setValidityFlag
.bind( this )
9355 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
9356 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
9357 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
9361 .addClass( 'oo-ui-textInputWidget' )
9362 .append( this.$icon
, this.$indicator
);
9363 if ( config
.placeholder
) {
9364 this.$input
.attr( 'placeholder', config
.placeholder
);
9366 this.$element
.attr( 'role', 'textbox' );
9371 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
9372 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
9373 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
9374 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.PendingElement
);
9376 /* Static properties */
9378 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
9386 * User presses enter inside the text box.
9388 * Not called if input is multiline.
9394 * User clicks the icon.
9400 * User clicks the indicator.
9408 * Handle icon mouse down events.
9410 * @param {jQuery.Event} e Mouse down event
9413 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
9414 if ( e
.which
=== 1 ) {
9415 this.$input
[0].focus();
9416 this.emit( 'icon' );
9422 * Handle indicator mouse down events.
9424 * @param {jQuery.Event} e Mouse down event
9427 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
9428 if ( e
.which
=== 1 ) {
9429 this.$input
[0].focus();
9430 this.emit( 'indicator' );
9436 * Handle key press events.
9438 * @param {jQuery.Event} e Key press event
9439 * @fires enter If enter key is pressed and input is not multiline
9441 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
9442 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
9443 this.emit( 'enter' );
9448 * Handle element attach events.
9450 * @param {jQuery.Event} e Element attach event
9452 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
9459 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
9463 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9469 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9471 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9473 this.setValidityFlag();
9479 * Automatically adjust the size of the text input.
9481 * This only affects multi-line inputs that are auto-sized.
9485 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9486 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
, idealHeight
;
9488 if ( this.multiline
&& this.autosize
) {
9489 $clone
= this.$input
.clone()
9490 .val( this.$input
.val() )
9491 // Set inline height property to 0 to measure scroll height
9492 .css( { height
: 0 } )
9493 .insertAfter( this.$input
);
9494 scrollHeight
= $clone
[0].scrollHeight
;
9495 // Remove inline height property to measure natural heights
9496 $clone
.css( 'height', '' );
9497 innerHeight
= $clone
.innerHeight();
9498 outerHeight
= $clone
.outerHeight();
9499 // Measure max rows height
9500 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' ).val( '' );
9501 maxInnerHeight
= $clone
.innerHeight();
9502 // Difference between reported innerHeight and scrollHeight with no scrollbars present
9503 // Equals 1 on Blink-based browsers and 0 everywhere else
9504 measurementError
= maxInnerHeight
- $clone
[0].scrollHeight
;
9506 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
9507 // Only apply inline height when expansion beyond natural height is needed
9508 if ( idealHeight
> innerHeight
) {
9509 // Use the difference between the inner and outer height as a buffer
9510 this.$input
.css( 'height', idealHeight
+ ( outerHeight
- innerHeight
) );
9512 this.$input
.css( 'height', '' );
9519 * Get input element.
9521 * @param {Object} [config] Configuration options
9522 * @return {jQuery} Input element
9524 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9525 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
9531 * Check if input supports multiple lines.
9535 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9536 return !!this.multiline
;
9540 * Check if input automatically adjusts its size.
9544 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9545 return !!this.autosize
;
9549 * Select the contents of the input.
9553 OO
.ui
.TextInputWidget
.prototype.select = function () {
9554 this.$input
.select();
9559 * Sets the validation pattern to use.
9560 * @param validate {RegExp|string|null} Regular expression (or symbolic name referencing
9561 * one, see #static-validationPatterns)
9563 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
9564 if ( validate
instanceof RegExp
) {
9565 this.validate
= validate
;
9567 this.validate
= this.constructor.static.validationPatterns
[validate
] || /.*/;
9572 * Sets the 'invalid' flag appropriately.
9574 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function () {
9576 this.isValid().done( function ( valid
) {
9577 widget
.setFlags( { invalid
: !valid
} );
9582 * Returns whether or not the current value is considered valid, according to the
9583 * supplied validation pattern.
9585 * @return {jQuery.Deferred}
9587 OO
.ui
.TextInputWidget
.prototype.isValid = function () {
9588 return $.Deferred().resolve( !!this.getValue().match( this.validate
) ).promise();
9592 * Text input with a menu of optional values.
9595 * @extends OO.ui.Widget
9598 * @param {Object} [config] Configuration options
9599 * @cfg {Object} [menu] Configuration options to pass to menu widget
9600 * @cfg {Object} [input] Configuration options to pass to input widget
9601 * @cfg {jQuery} [$overlay] Overlay layer; defaults to the current window's overlay.
9603 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9604 // Configuration initialization
9605 config
= config
|| {};
9607 // Parent constructor
9608 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9611 this.$overlay
= config
.$overlay
|| ( this.$.$iframe
|| this.$element
).closest( '.oo-ui-window' ).children( '.oo-ui-window-overlay' );
9612 if ( this.$overlay
.length
=== 0 ) {
9613 this.$overlay
= this.$( 'body' );
9615 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9616 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9619 this.menu
= new OO
.ui
.TextInputMenuWidget( this.input
, $.extend(
9620 { $: this.$, widget
: this, input
: this.input
, disabled
: this.isDisabled() },
9625 this.input
.connect( this, {
9626 change
: 'onInputChange',
9627 indicator
: 'onInputIndicator',
9628 enter
: 'onInputEnter'
9630 this.menu
.connect( this, {
9631 choose
: 'onMenuChoose',
9632 add
: 'onMenuItemsChange',
9633 remove
: 'onMenuItemsChange'
9637 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append( this.input
.$element
);
9638 this.$overlay
.append( this.menu
.$element
);
9639 this.onMenuItemsChange();
9644 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9649 * Handle input change events.
9651 * @param {string} value New value
9653 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9654 var match
= this.menu
.getItemFromData( value
);
9656 this.menu
.selectItem( match
);
9658 if ( !this.isDisabled() ) {
9659 this.menu
.toggle( true );
9664 * Handle input indicator events.
9666 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9667 if ( !this.isDisabled() ) {
9673 * Handle input enter events.
9675 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
9676 if ( !this.isDisabled() ) {
9677 this.menu
.toggle( false );
9682 * Handle menu choose events.
9684 * @param {OO.ui.OptionWidget} item Chosen item
9686 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
9688 this.input
.setValue( item
.getData() );
9693 * Handle menu item change events.
9695 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
9696 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
9702 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
9704 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
9707 this.input
.setDisabled( this.isDisabled() );
9710 this.menu
.setDisabled( this.isDisabled() );
9720 * @extends OO.ui.Widget
9721 * @mixins OO.ui.LabelElement
9724 * @param {Object} [config] Configuration options
9726 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
9727 // Config intialization
9728 config
= config
|| {};
9730 // Parent constructor
9731 OO
.ui
.LabelWidget
.super.call( this, config
);
9733 // Mixin constructors
9734 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
9735 OO
.ui
.TitledElement
.call( this, config
);
9738 this.input
= config
.input
;
9741 if ( this.input
instanceof OO
.ui
.InputWidget
) {
9742 this.$element
.on( 'click', this.onClick
.bind( this ) );
9746 this.$element
.addClass( 'oo-ui-labelWidget' );
9751 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
9752 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
9753 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.TitledElement
);
9755 /* Static Properties */
9757 OO
.ui
.LabelWidget
.static.tagName
= 'span';
9762 * Handles label mouse click events.
9764 * @param {jQuery.Event} e Mouse click event
9766 OO
.ui
.LabelWidget
.prototype.onClick = function () {
9767 this.input
.simulateLabelClick();
9772 * Generic option widget for use with OO.ui.SelectWidget.
9775 * @extends OO.ui.Widget
9776 * @mixins OO.ui.LabelElement
9777 * @mixins OO.ui.FlaggedElement
9780 * @param {Mixed} data Option data
9781 * @param {Object} [config] Configuration options
9782 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
9784 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
9785 // Config intialization
9786 config
= config
|| {};
9788 // Parent constructor
9789 OO
.ui
.OptionWidget
.super.call( this, config
);
9791 // Mixin constructors
9792 OO
.ui
.ItemWidget
.call( this );
9793 OO
.ui
.LabelElement
.call( this, config
);
9794 OO
.ui
.FlaggedElement
.call( this, config
);
9798 this.selected
= false;
9799 this.highlighted
= false;
9800 this.pressed
= false;
9804 .data( 'oo-ui-optionWidget', this )
9805 .attr( 'rel', config
.rel
)
9806 .attr( 'role', 'option' )
9807 .addClass( 'oo-ui-optionWidget' )
9808 .append( this.$label
);
9810 .prepend( this.$icon
)
9811 .append( this.$indicator
);
9816 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
9817 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
9818 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
9819 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
9821 /* Static Properties */
9823 OO
.ui
.OptionWidget
.static.selectable
= true;
9825 OO
.ui
.OptionWidget
.static.highlightable
= true;
9827 OO
.ui
.OptionWidget
.static.pressable
= true;
9829 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
9834 * Check if option can be selected.
9836 * @return {boolean} Item is selectable
9838 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
9839 return this.constructor.static.selectable
&& !this.isDisabled();
9843 * Check if option can be highlighted.
9845 * @return {boolean} Item is highlightable
9847 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
9848 return this.constructor.static.highlightable
&& !this.isDisabled();
9852 * Check if option can be pressed.
9854 * @return {boolean} Item is pressable
9856 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
9857 return this.constructor.static.pressable
&& !this.isDisabled();
9861 * Check if option is selected.
9863 * @return {boolean} Item is selected
9865 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
9866 return this.selected
;
9870 * Check if option is highlighted.
9872 * @return {boolean} Item is highlighted
9874 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
9875 return this.highlighted
;
9879 * Check if option is pressed.
9881 * @return {boolean} Item is pressed
9883 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
9884 return this.pressed
;
9888 * Set selected state.
9890 * @param {boolean} [state=false] Select option
9893 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
9894 if ( this.constructor.static.selectable
) {
9895 this.selected
= !!state
;
9896 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
9897 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
9898 this.scrollElementIntoView();
9900 this.updateThemeClasses();
9906 * Set highlighted state.
9908 * @param {boolean} [state=false] Highlight option
9911 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
9912 if ( this.constructor.static.highlightable
) {
9913 this.highlighted
= !!state
;
9914 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
9915 this.updateThemeClasses();
9921 * Set pressed state.
9923 * @param {boolean} [state=false] Press option
9926 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
9927 if ( this.constructor.static.pressable
) {
9928 this.pressed
= !!state
;
9929 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
9930 this.updateThemeClasses();
9936 * Make the option's highlight flash.
9938 * While flashing, the visual style of the pressed state is removed if present.
9940 * @return {jQuery.Promise} Promise resolved when flashing is done
9942 OO
.ui
.OptionWidget
.prototype.flash = function () {
9944 $element
= this.$element
,
9945 deferred
= $.Deferred();
9947 if ( !this.isDisabled() && this.constructor.static.pressable
) {
9948 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
9949 setTimeout( function () {
9950 // Restore original classes
9952 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
9953 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
9955 setTimeout( function () {
9962 return deferred
.promise();
9968 * @return {Mixed} Option data
9970 OO
.ui
.OptionWidget
.prototype.getData = function () {
9975 * Option widget with an option icon and indicator.
9977 * Use together with OO.ui.SelectWidget.
9980 * @extends OO.ui.OptionWidget
9981 * @mixins OO.ui.IconElement
9982 * @mixins OO.ui.IndicatorElement
9985 * @param {Mixed} data Option data
9986 * @param {Object} [config] Configuration options
9988 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
9989 // Parent constructor
9990 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
9992 // Mixin constructors
9993 OO
.ui
.IconElement
.call( this, config
);
9994 OO
.ui
.IndicatorElement
.call( this, config
);
9998 .addClass( 'oo-ui-decoratedOptionWidget' )
9999 .prepend( this.$icon
)
10000 .append( this.$indicator
);
10005 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
10006 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
10007 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
10010 * Option widget that looks like a button.
10012 * Use together with OO.ui.ButtonSelectWidget.
10015 * @extends OO.ui.DecoratedOptionWidget
10016 * @mixins OO.ui.ButtonElement
10019 * @param {Mixed} data Option data
10020 * @param {Object} [config] Configuration options
10022 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
10023 // Parent constructor
10024 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
10026 // Mixin constructors
10027 OO
.ui
.ButtonElement
.call( this, config
);
10030 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
10031 this.$button
.append( this.$element
.contents() );
10032 this.$element
.append( this.$button
);
10037 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
10038 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
10040 /* Static Properties */
10042 // Allow button mouse down events to pass through so they can be handled by the parent select widget
10043 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
10050 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
10051 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
10053 if ( this.constructor.static.selectable
) {
10054 this.setActive( state
);
10061 * Item of an OO.ui.MenuWidget.
10064 * @extends OO.ui.DecoratedOptionWidget
10067 * @param {Mixed} data Item data
10068 * @param {Object} [config] Configuration options
10070 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
10071 // Configuration initialization
10072 config
= $.extend( { icon
: 'check' }, config
);
10074 // Parent constructor
10075 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
10079 .attr( 'role', 'menuitem' )
10080 .addClass( 'oo-ui-menuItemWidget' );
10085 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10088 * Section to group one or more items in a OO.ui.MenuWidget.
10091 * @extends OO.ui.DecoratedOptionWidget
10094 * @param {Mixed} data Item data
10095 * @param {Object} [config] Configuration options
10097 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
10098 // Parent constructor
10099 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
10102 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
10107 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10109 /* Static Properties */
10111 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
10113 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
10116 * Items for an OO.ui.OutlineWidget.
10119 * @extends OO.ui.DecoratedOptionWidget
10122 * @param {Mixed} data Item data
10123 * @param {Object} [config] Configuration options
10124 * @cfg {number} [level] Indentation level
10125 * @cfg {boolean} [movable] Allow modification from outline controls
10127 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
10128 // Config intialization
10129 config
= config
|| {};
10131 // Parent constructor
10132 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
10136 this.movable
= !!config
.movable
;
10137 this.removable
= !!config
.removable
;
10140 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
10141 this.setLevel( config
.level
);
10146 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
10148 /* Static Properties */
10150 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
10152 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
10154 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
10156 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
10161 * Check if item is movable.
10163 * Movablilty is used by outline controls.
10165 * @return {boolean} Item is movable
10167 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
10168 return this.movable
;
10172 * Check if item is removable.
10174 * Removablilty is used by outline controls.
10176 * @return {boolean} Item is removable
10178 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
10179 return this.removable
;
10183 * Get indentation level.
10185 * @return {number} Indentation level
10187 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
10194 * Movablilty is used by outline controls.
10196 * @param {boolean} movable Item is movable
10199 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
10200 this.movable
= !!movable
;
10201 this.updateThemeClasses();
10206 * Set removability.
10208 * Removablilty is used by outline controls.
10210 * @param {boolean} movable Item is removable
10213 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
10214 this.removable
= !!removable
;
10215 this.updateThemeClasses();
10220 * Set indentation level.
10222 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
10225 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
10226 var levels
= this.constructor.static.levels
,
10227 levelClass
= this.constructor.static.levelClass
,
10230 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
10232 if ( this.level
=== i
) {
10233 this.$element
.addClass( levelClass
+ i
);
10235 this.$element
.removeClass( levelClass
+ i
);
10238 this.updateThemeClasses();
10244 * Container for content that is overlaid and positioned absolutely.
10247 * @extends OO.ui.Widget
10248 * @mixins OO.ui.LabelElement
10251 * @param {Object} [config] Configuration options
10252 * @cfg {number} [width=320] Width of popup in pixels
10253 * @cfg {number} [height] Height of popup, omit to use automatic height
10254 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
10255 * @cfg {string} [align='center'] Alignment of popup to origin
10256 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
10257 * @cfg {jQuery} [$content] Content to append to the popup's body
10258 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
10259 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
10260 * @cfg {boolean} [head] Show label and close button at the top
10261 * @cfg {boolean} [padded] Add padding to the body
10263 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
10264 // Config intialization
10265 config
= config
|| {};
10267 // Parent constructor
10268 OO
.ui
.PopupWidget
.super.call( this, config
);
10270 // Mixin constructors
10271 OO
.ui
.LabelElement
.call( this, config
);
10272 OO
.ui
.ClippableElement
.call( this, config
);
10275 this.visible
= false;
10276 this.$popup
= this.$( '<div>' );
10277 this.$head
= this.$( '<div>' );
10278 this.$body
= this.$( '<div>' );
10279 this.$anchor
= this.$( '<div>' );
10280 this.$container
= config
.$container
; // If undefined, will be computed lazily in updateDimensions()
10281 this.autoClose
= !!config
.autoClose
;
10282 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
10283 this.transitionTimeout
= null;
10284 this.anchor
= null;
10285 this.width
= config
.width
!== undefined ? config
.width
: 320;
10286 this.height
= config
.height
!== undefined ? config
.height
: null;
10287 this.align
= config
.align
|| 'center';
10288 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
10289 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
10292 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
10295 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
10296 this.$body
.addClass( 'oo-ui-popupWidget-body' );
10297 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
10299 .addClass( 'oo-ui-popupWidget-head' )
10300 .append( this.$label
, this.closeButton
.$element
);
10301 if ( !config
.head
) {
10305 .addClass( 'oo-ui-popupWidget-popup' )
10306 .append( this.$head
, this.$body
);
10309 .addClass( 'oo-ui-popupWidget' )
10310 .append( this.$popup
, this.$anchor
);
10311 // Move content, which was added to #$element by OO.ui.Widget, to the body
10312 if ( config
.$content
instanceof jQuery
) {
10313 this.$body
.append( config
.$content
);
10315 if ( config
.padded
) {
10316 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
10318 this.setClippableElement( this.$body
);
10323 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
10324 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
10325 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
10340 * Handles mouse down events.
10342 * @param {jQuery.Event} e Mouse down event
10344 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
10346 this.isVisible() &&
10347 !$.contains( this.$element
[0], e
.target
) &&
10348 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
10350 this.toggle( false );
10355 * Bind mouse down listener.
10357 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
10358 // Capture clicks outside popup
10359 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
10363 * Handles close button click events.
10365 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
10366 if ( this.isVisible() ) {
10367 this.toggle( false );
10372 * Unbind mouse down listener.
10374 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
10375 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
10379 * Set whether to show a anchor.
10381 * @param {boolean} [show] Show anchor, omit to toggle
10383 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
10384 show
= show
=== undefined ? !this.anchored
: !!show
;
10386 if ( this.anchored
!== show
) {
10388 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
10390 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
10392 this.anchored
= show
;
10397 * Check if showing a anchor.
10399 * @return {boolean} anchor is visible
10401 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
10402 return this.anchor
;
10408 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
10409 show
= show
=== undefined ? !this.isVisible() : !!show
;
10411 var change
= show
!== this.isVisible();
10414 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
10418 if ( this.autoClose
) {
10419 this.bindMouseDownListener();
10421 this.updateDimensions();
10422 this.toggleClipping( true );
10424 this.toggleClipping( false );
10425 if ( this.autoClose
) {
10426 this.unbindMouseDownListener();
10435 * Set the size of the popup.
10437 * Changing the size may also change the popup's position depending on the alignment.
10439 * @param {number} width Width
10440 * @param {number} height Height
10441 * @param {boolean} [transition=false] Use a smooth transition
10444 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
10445 this.width
= width
;
10446 this.height
= height
!== undefined ? height
: null;
10447 if ( this.isVisible() ) {
10448 this.updateDimensions( transition
);
10453 * Update the size and position.
10455 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
10456 * be called automatically.
10458 * @param {boolean} [transition=false] Use a smooth transition
10461 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
10462 var popupOffset
, originOffset
, containerLeft
, containerWidth
, containerRight
,
10463 popupLeft
, popupRight
, overlapLeft
, overlapRight
, anchorWidth
,
10467 if ( !this.$container
) {
10468 // Lazy-initialize $container if not specified in constructor
10469 this.$container
= this.$( this.getClosestScrollableElementContainer() );
10472 // Set height and width before measuring things, since it might cause our measurements
10473 // to change (e.g. due to scrollbars appearing or disappearing)
10476 height
: this.height
!== null ? this.height
: 'auto'
10479 // Compute initial popupOffset based on alignment
10480 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
];
10482 // Figure out if this will cause the popup to go beyond the edge of the container
10483 originOffset
= Math
.round( this.$element
.offset().left
);
10484 containerLeft
= Math
.round( this.$container
.offset().left
);
10485 containerWidth
= this.$container
.innerWidth();
10486 containerRight
= containerLeft
+ containerWidth
;
10487 popupLeft
= popupOffset
- padding
;
10488 popupRight
= popupOffset
+ padding
+ this.width
+ padding
;
10489 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
;
10490 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10492 // Adjust offset to make the popup not go beyond the edge, if needed
10493 if ( overlapRight
< 0 ) {
10494 popupOffset
+= overlapRight
;
10495 } else if ( overlapLeft
< 0 ) {
10496 popupOffset
-= overlapLeft
;
10499 // Adjust offset to avoid anchor being rendered too close to the edge
10500 anchorWidth
= this.$anchor
.width();
10501 if ( this.align
=== 'right' ) {
10502 popupOffset
+= anchorWidth
;
10503 } else if ( this.align
=== 'left' ) {
10504 popupOffset
-= anchorWidth
;
10507 // Prevent transition from being interrupted
10508 clearTimeout( this.transitionTimeout
);
10509 if ( transition
) {
10510 // Enable transition
10511 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10514 // Position body relative to anchor
10515 this.$popup
.css( 'margin-left', popupOffset
);
10517 if ( transition
) {
10518 // Prevent transitioning after transition is complete
10519 this.transitionTimeout
= setTimeout( function () {
10520 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10523 // Prevent transitioning immediately
10524 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10527 // Reevaluate clipping state since we've relocated and resized the popup
10536 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10537 * Results are cleared and populated each time the query is changed.
10540 * @extends OO.ui.Widget
10543 * @param {Object} [config] Configuration options
10544 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10545 * @cfg {string} [value] Initial query value
10547 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10548 // Configuration intialization
10549 config
= config
|| {};
10551 // Parent constructor
10552 OO
.ui
.SearchWidget
.super.call( this, config
);
10555 this.query
= new OO
.ui
.TextInputWidget( {
10558 placeholder
: config
.placeholder
,
10559 value
: config
.value
10561 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10562 this.$query
= this.$( '<div>' );
10563 this.$results
= this.$( '<div>' );
10566 this.query
.connect( this, {
10567 change
: 'onQueryChange',
10568 enter
: 'onQueryEnter'
10570 this.results
.connect( this, {
10571 highlight
: 'onResultsHighlight',
10572 select
: 'onResultsSelect'
10574 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
10578 .addClass( 'oo-ui-searchWidget-query' )
10579 .append( this.query
.$element
);
10581 .addClass( 'oo-ui-searchWidget-results' )
10582 .append( this.results
.$element
);
10584 .addClass( 'oo-ui-searchWidget' )
10585 .append( this.$results
, this.$query
);
10590 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
10596 * @param {Object|null} item Item data or null if no item is highlighted
10601 * @param {Object|null} item Item data or null if no item is selected
10607 * Handle query key down events.
10609 * @param {jQuery.Event} e Key down event
10611 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
10612 var highlightedItem
, nextItem
,
10613 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
10616 highlightedItem
= this.results
.getHighlightedItem();
10617 if ( !highlightedItem
) {
10618 highlightedItem
= this.results
.getSelectedItem();
10620 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
10621 this.results
.highlightItem( nextItem
);
10622 nextItem
.scrollElementIntoView();
10627 * Handle select widget select events.
10629 * Clears existing results. Subclasses should repopulate items according to new query.
10631 * @param {string} value New value
10633 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
10635 this.results
.clearItems();
10639 * Handle select widget enter key events.
10641 * Selects highlighted item.
10643 * @param {string} value New value
10645 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
10647 this.results
.selectItem( this.results
.getHighlightedItem() );
10651 * Handle select widget highlight events.
10653 * @param {OO.ui.OptionWidget} item Highlighted item
10656 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
10657 this.emit( 'highlight', item
? item
.getData() : null );
10661 * Handle select widget select events.
10663 * @param {OO.ui.OptionWidget} item Selected item
10666 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
10667 this.emit( 'select', item
? item
.getData() : null );
10671 * Get the query input.
10673 * @return {OO.ui.TextInputWidget} Query input
10675 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
10680 * Get the results list.
10682 * @return {OO.ui.SelectWidget} Select list
10684 OO
.ui
.SearchWidget
.prototype.getResults = function () {
10685 return this.results
;
10689 * Generic selection of options.
10691 * Items can contain any rendering, and are uniquely identified by a hash of their data. Any widget
10692 * that provides options, from which the user must choose one, should be built on this class.
10694 * Use together with OO.ui.OptionWidget.
10697 * @extends OO.ui.Widget
10698 * @mixins OO.ui.GroupElement
10701 * @param {Object} [config] Configuration options
10702 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
10704 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
10705 // Config intialization
10706 config
= config
|| {};
10708 // Parent constructor
10709 OO
.ui
.SelectWidget
.super.call( this, config
);
10711 // Mixin constructors
10712 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
10715 this.pressed
= false;
10716 this.selecting
= null;
10718 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
10719 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
10722 this.$element
.on( {
10723 mousedown
: this.onMouseDown
.bind( this ),
10724 mouseover
: this.onMouseOver
.bind( this ),
10725 mouseleave
: this.onMouseLeave
.bind( this )
10729 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
10730 if ( $.isArray( config
.items
) ) {
10731 this.addItems( config
.items
);
10737 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
10739 // Need to mixin base class as well
10740 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
10741 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
10747 * @param {OO.ui.OptionWidget|null} item Highlighted item
10752 * @param {OO.ui.OptionWidget|null} item Pressed item
10757 * @param {OO.ui.OptionWidget|null} item Selected item
10762 * @param {OO.ui.OptionWidget|null} item Chosen item
10767 * @param {OO.ui.OptionWidget[]} items Added items
10768 * @param {number} index Index items were added at
10773 * @param {OO.ui.OptionWidget[]} items Removed items
10779 * Handle mouse down events.
10782 * @param {jQuery.Event} e Mouse down event
10784 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
10787 if ( !this.isDisabled() && e
.which
=== 1 ) {
10788 this.togglePressed( true );
10789 item
= this.getTargetItem( e
);
10790 if ( item
&& item
.isSelectable() ) {
10791 this.pressItem( item
);
10792 this.selecting
= item
;
10793 this.getElementDocument().addEventListener(
10795 this.onMouseUpHandler
,
10798 this.getElementDocument().addEventListener(
10800 this.onMouseMoveHandler
,
10809 * Handle mouse up events.
10812 * @param {jQuery.Event} e Mouse up event
10814 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
10817 this.togglePressed( false );
10818 if ( !this.selecting
) {
10819 item
= this.getTargetItem( e
);
10820 if ( item
&& item
.isSelectable() ) {
10821 this.selecting
= item
;
10824 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
10825 this.pressItem( null );
10826 this.chooseItem( this.selecting
);
10827 this.selecting
= null;
10830 this.getElementDocument().removeEventListener(
10832 this.onMouseUpHandler
,
10835 this.getElementDocument().removeEventListener(
10837 this.onMouseMoveHandler
,
10845 * Handle mouse move events.
10848 * @param {jQuery.Event} e Mouse move event
10850 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
10853 if ( !this.isDisabled() && this.pressed
) {
10854 item
= this.getTargetItem( e
);
10855 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
10856 this.pressItem( item
);
10857 this.selecting
= item
;
10864 * Handle mouse over events.
10867 * @param {jQuery.Event} e Mouse over event
10869 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
10872 if ( !this.isDisabled() ) {
10873 item
= this.getTargetItem( e
);
10874 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
10880 * Handle mouse leave events.
10883 * @param {jQuery.Event} e Mouse over event
10885 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
10886 if ( !this.isDisabled() ) {
10887 this.highlightItem( null );
10893 * Get the closest item to a jQuery.Event.
10896 * @param {jQuery.Event} e
10897 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
10899 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
10900 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
10901 if ( $item
.length
) {
10902 return $item
.data( 'oo-ui-optionWidget' );
10908 * Get selected item.
10910 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
10912 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
10915 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10916 if ( this.items
[i
].isSelected() ) {
10917 return this.items
[i
];
10924 * Get highlighted item.
10926 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
10928 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
10931 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10932 if ( this.items
[i
].isHighlighted() ) {
10933 return this.items
[i
];
10940 * Get an existing item with equivilant data.
10942 * @param {Object} data Item data to search for
10943 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
10945 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
10946 var hash
= OO
.getHash( data
);
10948 if ( hash
in this.hashes
) {
10949 return this.hashes
[hash
];
10956 * Toggle pressed state.
10958 * @param {boolean} pressed An option is being pressed
10960 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
10961 if ( pressed
=== undefined ) {
10962 pressed
= !this.pressed
;
10964 if ( pressed
!== this.pressed
) {
10966 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
10967 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
10968 this.pressed
= pressed
;
10973 * Highlight an item.
10975 * Highlighting is mutually exclusive.
10977 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
10981 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
10982 var i
, len
, highlighted
,
10985 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10986 highlighted
= this.items
[i
] === item
;
10987 if ( this.items
[i
].isHighlighted() !== highlighted
) {
10988 this.items
[i
].setHighlighted( highlighted
);
10993 this.emit( 'highlight', item
);
11002 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
11006 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
11007 var i
, len
, selected
,
11010 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11011 selected
= this.items
[i
] === item
;
11012 if ( this.items
[i
].isSelected() !== selected
) {
11013 this.items
[i
].setSelected( selected
);
11018 this.emit( 'select', item
);
11027 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
11031 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
11032 var i
, len
, pressed
,
11035 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11036 pressed
= this.items
[i
] === item
;
11037 if ( this.items
[i
].isPressed() !== pressed
) {
11038 this.items
[i
].setPressed( pressed
);
11043 this.emit( 'press', item
);
11052 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
11053 * an item is selected using the keyboard or mouse.
11055 * @param {OO.ui.OptionWidget} item Item to choose
11059 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
11060 this.selectItem( item
);
11061 this.emit( 'choose', item
);
11067 * Get an item relative to another one.
11069 * @param {OO.ui.OptionWidget} item Item to start at
11070 * @param {number} direction Direction to move in
11071 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
11073 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
11074 var inc
= direction
> 0 ? 1 : -1,
11075 len
= this.items
.length
,
11076 index
= item
instanceof OO
.ui
.OptionWidget
?
11077 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
11078 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
11080 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
11081 Math
.max( index
, -1 ) :
11082 // Default to n-1 instead of -1, if nothing is selected let's start at the end
11083 Math
.min( index
, len
);
11085 while ( len
!== 0 ) {
11086 i
= ( i
+ inc
+ len
) % len
;
11087 item
= this.items
[i
];
11088 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11091 // Stop iterating when we've looped all the way around
11092 if ( i
=== stopAt
) {
11100 * Get the next selectable item.
11102 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
11104 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
11107 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
11108 item
= this.items
[i
];
11109 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
11120 * When items are added with the same values as existing items, the existing items will be
11121 * automatically removed before the new items are added.
11123 * @param {OO.ui.OptionWidget[]} items Items to add
11124 * @param {number} [index] Index to insert items after
11128 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
11129 var i
, len
, item
, hash
,
11132 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11134 hash
= OO
.getHash( item
.getData() );
11135 if ( hash
in this.hashes
) {
11136 // Remove item with same value
11137 remove
.push( this.hashes
[hash
] );
11139 this.hashes
[hash
] = item
;
11141 if ( remove
.length
) {
11142 this.removeItems( remove
);
11146 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
11148 // Always provide an index, even if it was omitted
11149 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
11157 * Items will be detached, not removed, so they can be used later.
11159 * @param {OO.ui.OptionWidget[]} items Items to remove
11163 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
11164 var i
, len
, item
, hash
;
11166 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11168 hash
= OO
.getHash( item
.getData() );
11169 if ( hash
in this.hashes
) {
11170 // Remove existing item
11171 delete this.hashes
[hash
];
11173 if ( item
.isSelected() ) {
11174 this.selectItem( null );
11179 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
11181 this.emit( 'remove', items
);
11189 * Items will be detached, not removed, so they can be used later.
11194 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
11195 var items
= this.items
.slice();
11200 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
11201 this.selectItem( null );
11203 this.emit( 'remove', items
);
11209 * Select widget containing button options.
11211 * Use together with OO.ui.ButtonOptionWidget.
11214 * @extends OO.ui.SelectWidget
11217 * @param {Object} [config] Configuration options
11219 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
11220 // Parent constructor
11221 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
11224 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
11229 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
11232 * Overlaid menu of options.
11234 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
11237 * Use together with OO.ui.MenuItemWidget.
11240 * @extends OO.ui.SelectWidget
11241 * @mixins OO.ui.ClippableElement
11244 * @param {Object} [config] Configuration options
11245 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
11246 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
11247 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
11249 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
11250 // Config intialization
11251 config
= config
|| {};
11253 // Parent constructor
11254 OO
.ui
.MenuWidget
.super.call( this, config
);
11256 // Mixin constructors
11257 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
11260 this.flashing
= false;
11261 this.visible
= false;
11262 this.newItems
= null;
11263 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
11264 this.$input
= config
.input
? config
.input
.$input
: null;
11265 this.$widget
= config
.widget
? config
.widget
.$element
: null;
11266 this.$previousFocus
= null;
11267 this.isolated
= !config
.input
;
11268 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
11269 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
11274 .attr( 'role', 'menu' )
11275 .addClass( 'oo-ui-menuWidget' );
11280 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
11281 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
11286 * Handles document mouse down events.
11288 * @param {jQuery.Event} e Key down event
11290 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
11291 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
11292 this.toggle( false );
11297 * Handles key down events.
11299 * @param {jQuery.Event} e Key down event
11301 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
11304 highlightItem
= this.getHighlightedItem();
11306 if ( !this.isDisabled() && this.isVisible() ) {
11307 if ( !highlightItem
) {
11308 highlightItem
= this.getSelectedItem();
11310 switch ( e
.keyCode
) {
11311 case OO
.ui
.Keys
.ENTER
:
11312 this.chooseItem( highlightItem
);
11315 case OO
.ui
.Keys
.UP
:
11316 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
11319 case OO
.ui
.Keys
.DOWN
:
11320 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
11323 case OO
.ui
.Keys
.ESCAPE
:
11324 if ( highlightItem
) {
11325 highlightItem
.setHighlighted( false );
11327 this.toggle( false );
11333 this.highlightItem( nextItem
);
11334 nextItem
.scrollElementIntoView();
11338 e
.preventDefault();
11339 e
.stopPropagation();
11346 * Bind key down listener.
11348 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
11349 if ( this.$input
) {
11350 this.$input
.on( 'keydown', this.onKeyDownHandler
);
11352 // Capture menu navigation keys
11353 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
11358 * Unbind key down listener.
11360 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
11361 if ( this.$input
) {
11362 this.$input
.off( 'keydown' );
11364 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
11371 * This will close the menu when done, unlike selectItem which only changes selection.
11373 * @param {OO.ui.OptionWidget} item Item to choose
11376 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
11380 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
11382 if ( item
&& !this.flashing
) {
11383 this.flashing
= true;
11384 item
.flash().done( function () {
11385 widget
.toggle( false );
11386 widget
.flashing
= false;
11389 this.toggle( false );
11398 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
11402 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
11405 if ( !this.newItems
) {
11406 this.newItems
= [];
11409 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
11411 if ( this.isVisible() ) {
11412 // Defer fitting label until item has been attached
11415 this.newItems
.push( item
);
11419 // Reevaluate clipping
11428 OO
.ui
.MenuWidget
.prototype.removeItems = function ( items
) {
11430 OO
.ui
.MenuWidget
.super.prototype.removeItems
.call( this, items
);
11432 // Reevaluate clipping
11441 OO
.ui
.MenuWidget
.prototype.clearItems = function () {
11443 OO
.ui
.MenuWidget
.super.prototype.clearItems
.call( this );
11445 // Reevaluate clipping
11454 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
11455 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
11458 change
= visible
!== this.isVisible(),
11459 elementDoc
= this.getElementDocument(),
11460 widgetDoc
= this.$widget
? this.$widget
[0].ownerDocument
: null;
11463 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
11467 this.bindKeyDownListener();
11469 // Change focus to enable keyboard navigation
11470 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
11471 this.$previousFocus
= this.$( ':focus' );
11472 this.$input
[0].focus();
11474 if ( this.newItems
&& this.newItems
.length
) {
11475 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
11476 this.newItems
[i
].fitLabel();
11478 this.newItems
= null;
11480 this.toggleClipping( true );
11483 if ( this.autoHide
) {
11484 elementDoc
.addEventListener(
11485 'mousedown', this.onDocumentMouseDownHandler
, true
11487 // Support $widget being in a different document
11488 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11489 widgetDoc
.addEventListener(
11490 'mousedown', this.onDocumentMouseDownHandler
, true
11495 this.unbindKeyDownListener();
11496 if ( this.isolated
&& this.$previousFocus
) {
11497 this.$previousFocus
[0].focus();
11498 this.$previousFocus
= null;
11500 elementDoc
.removeEventListener(
11501 'mousedown', this.onDocumentMouseDownHandler
, true
11503 // Support $widget being in a different document
11504 if ( widgetDoc
&& widgetDoc
!== elementDoc
) {
11505 widgetDoc
.removeEventListener(
11506 'mousedown', this.onDocumentMouseDownHandler
, true
11509 this.toggleClipping( false );
11517 * Menu for a text input widget.
11519 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11520 * is in a different frame, the menu's position is automatically calculated and maintained when the
11521 * menu is toggled or the window is resized.
11524 * @extends OO.ui.MenuWidget
11527 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11528 * @param {Object} [config] Configuration options
11529 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11531 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
11532 // Parent constructor
11533 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
11536 this.input
= input
;
11537 this.$container
= config
.$container
|| this.input
.$element
;
11538 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
11541 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
11546 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
11551 * Handle window resize event.
11553 * @param {jQuery.Event} e Window resize event
11555 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
11562 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
11563 visible
= visible
=== undefined ? !this.isVisible() : !!visible
;
11565 var change
= visible
!== this.isVisible();
11567 if ( change
&& visible
) {
11568 // Make sure the width is set before the parent method runs.
11569 // After this we have to call this.position(); again to actually
11570 // position ourselves correctly.
11575 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
11578 if ( this.isVisible() ) {
11580 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11582 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
11590 * Position the menu.
11594 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
11595 var $container
= this.$container
,
11596 pos
= OO
.ui
.Element
.getRelativePosition( $container
, this.$element
.offsetParent() );
11598 // Position under input
11599 pos
.top
+= $container
.height();
11600 this.$element
.css( pos
);
11603 this.setIdealSize( $container
.width() );
11604 // We updated the position, so re-evaluate the clipping state
11611 * Structured list of items.
11613 * Use with OO.ui.OutlineItemWidget.
11616 * @extends OO.ui.SelectWidget
11619 * @param {Object} [config] Configuration options
11621 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
11622 // Config intialization
11623 config
= config
|| {};
11625 // Parent constructor
11626 OO
.ui
.OutlineWidget
.super.call( this, config
);
11629 this.$element
.addClass( 'oo-ui-outlineWidget' );
11634 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
11637 * Switch that slides on and off.
11640 * @extends OO.ui.Widget
11641 * @mixins OO.ui.ToggleWidget
11644 * @param {Object} [config] Configuration options
11645 * @cfg {boolean} [value=false] Initial value
11647 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
11648 // Parent constructor
11649 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
11651 // Mixin constructors
11652 OO
.ui
.ToggleWidget
.call( this, config
);
11655 this.dragging
= false;
11656 this.dragStart
= null;
11657 this.sliding
= false;
11658 this.$glow
= this.$( '<span>' );
11659 this.$grip
= this.$( '<span>' );
11662 this.$element
.on( 'click', this.onClick
.bind( this ) );
11665 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
11666 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
11668 .addClass( 'oo-ui-toggleSwitchWidget' )
11669 .append( this.$glow
, this.$grip
);
11674 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
11675 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
11680 * Handle mouse down events.
11682 * @param {jQuery.Event} e Mouse down event
11684 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
11685 if ( !this.isDisabled() && e
.which
=== 1 ) {
11686 this.setValue( !this.value
);