2 * OOjs UI v0.1.0-pre (9aad824bd6)
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: Wed Jun 25 2014 10:59:56 GMT-0700 (PDT)
15 * Namespace for all classes, static methods and static properties.
47 * Get the user's language and any fallback languages.
49 * These language codes are used to localize user interface elements in the user's language.
51 * In environments that provide a localization system, this function should be overridden to
52 * return the user's language(s). The default implementation returns English (en) only.
54 * @return {string[]} Language codes, in descending order of priority
56 OO
.ui
.getUserLanguages = function () {
61 * Get a value in an object keyed by language code.
63 * @param {Object.<string,Mixed>} obj Object keyed by language code
64 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
65 * @param {string} [fallback] Fallback code, used if no matching language can be found
66 * @return {Mixed} Local value
68 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
75 // Known user language
76 langs
= OO
.ui
.getUserLanguages();
77 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
84 if ( obj
[fallback
] ) {
87 // 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 // Label text for button to exit from dialog
107 'ooui-dialog-action-close': 'Close',
108 // Tool tip for a button that moves items in a list down one place
109 'ooui-outline-control-move-down': 'Move item down',
110 // Tool tip for a button that moves items in a list up one place
111 'ooui-outline-control-move-up': 'Move item up',
112 // Tool tip for a button that removes items from a list
113 'ooui-outline-control-remove': 'Remove item',
114 // Label for the toolbar group that contains a list of all other available tools
115 'ooui-toolbar-more': 'More',
117 // Label for the generic dialog used to confirm things
118 'ooui-dialog-confirm-title': 'Confirm',
119 // The default prompt of a confirmation dialog
120 'ooui-dialog-confirm-default-prompt': 'Are you sure?',
121 // The default OK button text on a confirmation dialog
122 'ooui-dialog-confirm-default-ok': 'OK',
123 // The default cancel button text on a confirmation dialog
124 'ooui-dialog-confirm-default-cancel': 'Cancel'
128 * Get a localized message.
130 * In environments that provide a localization system, this function should be overridden to
131 * return the message translated in the user's language. The default implementation always returns
134 * After the message key, message parameters may optionally be passed. In the default implementation,
135 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
136 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
137 * they support unnamed, ordered message parameters.
140 * @param {string} key Message key
141 * @param {Mixed...} [params] Message parameters
142 * @return {string} Translated message with parameters substituted
144 OO
.ui
.msg = function ( key
) {
145 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
146 if ( typeof message
=== 'string' ) {
147 // Perform $1 substitution
148 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
149 var i
= parseInt( n
, 10 );
150 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
153 // Return placeholder if message not found
154 message
= '[' + key
+ ']';
160 OO
.ui
.deferMsg = function ( key
) {
162 return OO
.ui
.msg( key
);
167 OO
.ui
.resolveMsg = function ( msg
) {
168 if ( $.isFunction( msg
) ) {
176 * DOM element abstraction.
182 * @param {Object} [config] Configuration options
183 * @cfg {Function} [$] jQuery for the frame the widget is in
184 * @cfg {string[]} [classes] CSS class names
185 * @cfg {string} [text] Text to insert
186 * @cfg {jQuery} [$content] Content elements to append (after text)
188 OO
.ui
.Element
= function OoUiElement( config
) {
189 // Configuration initialization
190 config
= config
|| {};
193 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
194 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
195 this.elementGroup
= null;
198 if ( $.isArray( config
.classes
) ) {
199 this.$element
.addClass( config
.classes
.join( ' ' ) );
202 this.$element
.text( config
.text
);
204 if ( config
.$content
) {
205 this.$element
.append( config
.$content
);
211 OO
.initClass( OO
.ui
.Element
);
213 /* Static Properties */
218 * This may be ignored if getTagName is overridden.
224 OO
.ui
.Element
.static.tagName
= 'div';
229 * Get a jQuery function within a specific document.
232 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
233 * @param {OO.ui.Frame} [frame] Frame of the document context
234 * @return {Function} Bound jQuery function
236 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
237 function wrapper( selector
) {
238 return $( selector
, wrapper
.context
);
241 wrapper
.context
= this.getDocument( context
);
244 wrapper
.frame
= frame
;
251 * Get the document of an element.
254 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
255 * @return {HTMLDocument|null} Document object
257 OO
.ui
.Element
.getDocument = function ( obj
) {
258 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
259 return ( obj
[0] && obj
[0].ownerDocument
) ||
260 // Empty jQuery selections might have a context
267 ( obj
.nodeType
=== 9 && obj
) ||
272 * Get the window of an element or document.
275 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
276 * @return {Window} Window object
278 OO
.ui
.Element
.getWindow = function ( obj
) {
279 var doc
= this.getDocument( obj
);
280 return doc
.parentWindow
|| doc
.defaultView
;
284 * Get the direction of an element or document.
287 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
288 * @return {string} Text direction, either `ltr` or `rtl`
290 OO
.ui
.Element
.getDir = function ( obj
) {
293 if ( obj
instanceof jQuery
) {
296 isDoc
= obj
.nodeType
=== 9;
297 isWin
= obj
.document
!== undefined;
298 if ( isDoc
|| isWin
) {
304 return $( obj
).css( 'direction' );
308 * Get the offset between two frames.
310 * TODO: Make this function not use recursion.
313 * @param {Window} from Window of the child frame
314 * @param {Window} [to=window] Window of the parent frame
315 * @param {Object} [offset] Offset to start with, used internally
316 * @return {Object} Offset object, containing left and top properties
318 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
319 var i
, len
, frames
, frame
, rect
;
325 offset
= { 'top': 0, 'left': 0 };
327 if ( from.parent
=== from ) {
331 // Get iframe element
332 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
333 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
334 if ( frames
[i
].contentWindow
=== from ) {
340 // Recursively accumulate offset values
342 rect
= frame
.getBoundingClientRect();
343 offset
.left
+= rect
.left
;
344 offset
.top
+= rect
.top
;
346 this.getFrameOffset( from.parent
, offset
);
353 * Get the offset between two elements.
356 * @param {jQuery} $from
357 * @param {jQuery} $to
358 * @return {Object} Translated position coordinates, containing top and left properties
360 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
361 var from = $from.offset(),
363 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
367 * Get element border sizes.
370 * @param {HTMLElement} el Element to measure
371 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
373 OO
.ui
.Element
.getBorders = function ( el
) {
374 var doc
= el
.ownerDocument
,
375 win
= doc
.parentWindow
|| doc
.defaultView
,
376 style
= win
&& win
.getComputedStyle
?
377 win
.getComputedStyle( el
, null ) :
380 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
381 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
382 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
383 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
386 'top': Math
.round( top
),
387 'left': Math
.round( left
),
388 'bottom': Math
.round( bottom
),
389 'right': Math
.round( right
)
394 * Get dimensions of an element or window.
397 * @param {HTMLElement|Window} el Element to measure
398 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
400 OO
.ui
.Element
.getDimensions = function ( el
) {
402 doc
= el
.ownerDocument
|| el
.document
,
403 win
= doc
.parentWindow
|| doc
.defaultView
;
405 if ( win
=== el
|| el
=== doc
.documentElement
) {
408 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
410 'top': $win
.scrollTop(),
411 'left': $win
.scrollLeft()
413 'scrollbar': { 'right': 0, 'bottom': 0 },
417 'bottom': $win
.innerHeight(),
418 'right': $win
.innerWidth()
424 'borders': this.getBorders( el
),
426 'top': $el
.scrollTop(),
427 'left': $el
.scrollLeft()
430 'right': $el
.innerWidth() - el
.clientWidth
,
431 'bottom': $el
.innerHeight() - el
.clientHeight
433 'rect': el
.getBoundingClientRect()
439 * Get closest scrollable container.
441 * Traverses up until either a scrollable element or the root is reached, in which case the window
445 * @param {HTMLElement} el Element to find scrollable container for
446 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
447 * @return {HTMLElement|Window} Closest scrollable container
449 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
451 props
= [ 'overflow' ],
452 $parent
= $( el
).parent();
454 if ( dimension
=== 'x' || dimension
=== 'y' ) {
455 props
.push( 'overflow-' + dimension
);
458 while ( $parent
.length
) {
459 if ( $parent
[0] === el
.ownerDocument
.body
) {
464 val
= $parent
.css( props
[i
] );
465 if ( val
=== 'auto' || val
=== 'scroll' ) {
469 $parent
= $parent
.parent();
471 return this.getDocument( el
).body
;
475 * Scroll element into view.
478 * @param {HTMLElement} el Element to scroll into view
479 * @param {Object} [config={}] Configuration config
480 * @param {string} [config.duration] jQuery animation duration value
481 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
482 * to scroll in both directions
483 * @param {Function} [config.complete] Function to call when scrolling completes
485 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
486 // Configuration initialization
487 config
= config
|| {};
490 callback
= typeof config
.complete
=== 'function' && config
.complete
,
491 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
493 eld
= this.getDimensions( el
),
494 scd
= this.getDimensions( sc
),
495 $win
= $( this.getWindow( el
) );
497 // Compute the distances between the edges of el and the edges of the scroll viewport
498 if ( $sc
.is( 'body' ) ) {
499 // If the scrollable container is the <body> this is easy
502 'bottom': $win
.innerHeight() - eld
.rect
.bottom
,
503 'left': eld
.rect
.left
,
504 'right': $win
.innerWidth() - eld
.rect
.right
507 // Otherwise, we have to subtract el's coordinates from sc's coordinates
509 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
510 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
511 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
512 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
516 if ( !config
.direction
|| config
.direction
=== 'y' ) {
518 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
519 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
520 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
523 if ( !config
.direction
|| config
.direction
=== 'x' ) {
524 if ( rel
.left
< 0 ) {
525 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
526 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
527 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
530 if ( !$.isEmptyObject( anim
) ) {
531 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
533 $sc
.queue( function ( next
) {
548 * Get the HTML tag name.
550 * Override this method to base the result on instance information.
552 * @return {string} HTML tag name
554 OO
.ui
.Element
.prototype.getTagName = function () {
555 return this.constructor.static.tagName
;
559 * Check if the element is attached to the DOM
560 * @return {boolean} The element is attached to the DOM
562 OO
.ui
.Element
.prototype.isElementAttached = function () {
563 return $.contains( this.getElementDocument(), this.$element
[0] );
567 * Get the DOM document.
569 * @return {HTMLDocument} Document object
571 OO
.ui
.Element
.prototype.getElementDocument = function () {
572 return OO
.ui
.Element
.getDocument( this.$element
);
576 * Get the DOM window.
578 * @return {Window} Window object
580 OO
.ui
.Element
.prototype.getElementWindow = function () {
581 return OO
.ui
.Element
.getWindow( this.$element
);
585 * Get closest scrollable container.
587 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
588 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
592 * Get group element is in.
594 * @return {OO.ui.GroupElement|null} Group element, null if none
596 OO
.ui
.Element
.prototype.getElementGroup = function () {
597 return this.elementGroup
;
601 * Set group element is in.
603 * @param {OO.ui.GroupElement|null} group Group element, null if none
606 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
607 this.elementGroup
= group
;
612 * Scroll element into view.
614 * @param {Object} [config={}]
616 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
617 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
621 * Bind a handler for an event on this.$element
623 * @deprecated Use jQuery#on instead.
624 * @param {string} event
625 * @param {Function} callback
627 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
628 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
632 * Unbind a handler bound with #offDOMEvent
634 * @deprecated Use jQuery#off instead.
635 * @param {string} event
636 * @param {Function} callback
638 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
639 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
644 * Bind a handler for an event on a DOM element.
646 * Used to be for working around a jQuery bug (jqbug.com/14180),
647 * but obsolete as of jQuery 1.11.0.
650 * @deprecated Use jQuery#on instead.
651 * @param {HTMLElement|jQuery} el DOM element
652 * @param {string} event Event to bind
653 * @param {Function} callback Callback to call when the event fires
655 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
656 $( el
).on( event
, callback
);
660 * Unbind a handler bound with #static-method-onDOMEvent.
662 * @deprecated Use jQuery#off instead.
664 * @param {HTMLElement|jQuery} el DOM element
665 * @param {string} event Event to unbind
666 * @param {Function} [callback] Callback to unbind
668 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
669 $( el
).off( event
, callback
);
673 * Embedded iframe with the same styles as its parent.
676 * @extends OO.ui.Element
677 * @mixins OO.EventEmitter
680 * @param {Object} [config] Configuration options
682 OO
.ui
.Frame
= function OoUiFrame( config
) {
683 // Parent constructor
684 OO
.ui
.Frame
.super.call( this, config
);
686 // Mixin constructors
687 OO
.EventEmitter
.call( this );
691 this.config
= config
;
695 .addClass( 'oo-ui-frame' )
696 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
702 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
703 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
705 /* Static Properties */
711 OO
.ui
.Frame
.static.tagName
= 'iframe';
722 * Transplant the CSS styles from as parent document to a frame's document.
724 * This loops over the style sheets in the parent document, and copies their nodes to the
725 * frame's document. It then polls the document to see when all styles have loaded, and once they
726 * have, resolves the promise.
728 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
729 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
730 * Firefox, where the styles won't load until the iframe becomes visible.
732 * For details of how we arrived at the strategy used in this function, see #load.
736 * @param {HTMLDocument} parentDoc Document to transplant styles from
737 * @param {HTMLDocument} frameDoc Document to transplant styles to
738 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
739 * @return {jQuery.Promise} Promise resolved when styles have loaded
741 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
742 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
743 $pollNodes
= $( [] ),
744 // Fake font-family value
745 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
746 deferred
= $.Deferred();
748 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
749 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
750 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
751 // External stylesheet
752 // Create a node with a unique ID that we're going to monitor to see when the CSS
754 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
755 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
756 .attr( 'id', pollNodeId
)
757 .appendTo( frameDoc
.body
)
760 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
761 // The font-family rule will only take effect once the @import finishes
762 newNode
= frameDoc
.createElement( 'style' );
763 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
764 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
766 // Not an external stylesheet, or no polling required; just copy the node over
767 newNode
= frameDoc
.importNode( styleNode
, true );
769 frameDoc
.head
.appendChild( newNode
);
772 // Poll every 100ms until all external stylesheets have loaded
773 $pendingPollNodes
= $pollNodes
;
774 timeoutID
= setTimeout( function pollExternalStylesheets() {
776 $pendingPollNodes
.length
> 0 &&
777 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
779 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
782 if ( $pendingPollNodes
.length
=== 0 ) {
784 if ( timeoutID
!== null ) {
790 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
793 // ...but give up after a while
794 if ( timeout
!== 0 ) {
795 setTimeout( function () {
797 clearTimeout( timeoutID
);
802 }, timeout
|| 5000 );
805 return deferred
.promise();
811 * Load the frame contents.
813 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
814 * promise will be resolved. Calling while loading will return a promise but not trigger a new
815 * loading cycle. Calling after loading is complete will return a promise that's already been
818 * Sounds simple right? Read on...
820 * When you create a dynamic iframe using open/write/close, the window.load event for the
821 * iframe is triggered when you call close, and there's no further load event to indicate that
822 * everything is actually loaded.
824 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
825 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
826 * are added to document.styleSheets immediately, and the only way you can determine whether they've
827 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
828 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
830 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
831 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
832 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
833 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
834 * .cssRules is allowed.
836 * However, now that we control the styles we're injecting, we might as well do away with
837 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
838 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
839 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
840 * rule is not applied until after the `@import` finishes.
842 * All this stylesheet injection and polling magic is in #transplantStyles.
844 * @return {jQuery.Promise} Promise resolved when loading is complete
847 OO
.ui
.Frame
.prototype.load = function () {
850 // Return existing promise if already loading or loaded
851 if ( this.loading
) {
852 return this.loading
.promise();
856 this.loading
= $.Deferred();
858 win
= this.$element
.prop( 'contentWindow' );
861 // Figure out directionality:
862 this.dir
= OO
.ui
.Element
.getDir( this.$element
) || 'ltr';
864 // Initialize contents
869 '<body class="oo-ui-frame-body oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
870 '<div class="oo-ui-frame-content"></div>' +
877 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
878 this.$content
= this.$( '.oo-ui-frame-content' ).attr( 'tabIndex', 0 );
879 this.$document
= this.$( doc
);
882 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
883 .always( OO
.ui
.bind( function () {
885 this.loading
.resolve();
888 return this.loading
.promise();
892 * Set the size of the frame.
894 * @param {number} width Frame width in pixels
895 * @param {number} height Frame height in pixels
898 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
899 this.$element
.css( { 'width': width
, 'height': height
} );
903 * Container for elements in a child frame.
905 * There are two ways to specify a title: set the static `title` property or provide a `title`
906 * property in the configuration options. The latter will override the former.
910 * @extends OO.ui.Element
911 * @mixins OO.EventEmitter
914 * @param {Object} [config] Configuration options
915 * @cfg {string|Function} [title] Title string or function that returns a string
916 * @cfg {string} [icon] Symbolic name of icon
919 OO
.ui
.Window
= function OoUiWindow( config
) {
921 // Parent constructor
922 OO
.ui
.Window
.super.call( this, config
);
924 // Mixin constructors
925 OO
.EventEmitter
.call( this );
928 this.visible
= false;
932 this.title
= OO
.ui
.resolveMsg( config
.title
|| this.constructor.static.title
);
933 this.icon
= config
.icon
|| this.constructor.static.icon
;
934 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
935 this.$frame
= this.$( '<div>' );
936 this.$ = function () {
937 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
942 .addClass( 'oo-ui-window' )
943 // Hide the window using visibility: hidden; while the iframe is still loading
944 // Can't use display: none; because that prevents the iframe from loading in Firefox
945 .css( 'visibility', 'hidden' )
946 .append( this.$frame
);
948 .addClass( 'oo-ui-window-frame' )
949 .append( this.frame
.$element
);
952 this.frame
.on( 'load', function () {
953 element
.initialize();
954 // Undo the visibility: hidden; hack and apply display: none;
955 // We can do this safely now that the iframe has initialized
956 // (don't do this from within #initialize because it has to happen
957 // after the all subclasses have been handled as well).
958 element
.$element
.hide().css( 'visibility', '' );
964 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
965 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
972 * Fired after the setup process has been executed.
975 * @param {Object} data Window opening data
981 * Fired after the ready process has been executed.
984 * @param {Object} data Window opening data
988 * Window is torn down
990 * Fired after the teardown process has been executed.
993 * @param {Object} data Window closing data
996 /* Static Properties */
999 * Symbolic name of icon.
1003 * @property {string}
1005 OO
.ui
.Window
.static.icon
= 'window';
1010 * Subclasses must implement this property before instantiating the window.
1011 * Alternatively, override #getTitle with an alternative implementation.
1016 * @property {string|Function} Title string or function that returns a string
1018 OO
.ui
.Window
.static.title
= null;
1023 * Check if window is visible.
1025 * @return {boolean} Window is visible
1027 OO
.ui
.Window
.prototype.isVisible = function () {
1028 return this.visible
;
1032 * Check if window is opening.
1034 * @return {boolean} Window is opening
1036 OO
.ui
.Window
.prototype.isOpening = function () {
1037 return !!this.opening
&& this.opening
.state() === 'pending';
1041 * Check if window is closing.
1043 * @return {boolean} Window is closing
1045 OO
.ui
.Window
.prototype.isClosing = function () {
1046 return !!this.closing
&& this.closing
.state() === 'pending';
1050 * Check if window is opened.
1052 * @return {boolean} Window is opened
1054 OO
.ui
.Window
.prototype.isOpened = function () {
1055 return !!this.opened
&& this.opened
.state() === 'pending';
1059 * Get the window frame.
1061 * @return {OO.ui.Frame} Frame of window
1063 OO
.ui
.Window
.prototype.getFrame = function () {
1068 * Get the title of the window.
1070 * @return {string} Title text
1072 OO
.ui
.Window
.prototype.getTitle = function () {
1077 * Get the window icon.
1079 * @return {string} Symbolic name of icon
1081 OO
.ui
.Window
.prototype.getIcon = function () {
1086 * Set the size of window frame.
1088 * @param {number} [width=auto] Custom width
1089 * @param {number} [height=auto] Custom height
1092 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1093 if ( !this.frame
.$content
) {
1097 this.frame
.$element
.css( {
1098 'width': width
=== undefined ? 'auto' : width
,
1099 'height': height
=== undefined ? 'auto' : height
1106 * Set the title of the window.
1108 * @param {string|Function} title Title text or a function that returns text
1111 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1112 this.title
= OO
.ui
.resolveMsg( title
);
1113 if ( this.$title
) {
1114 this.$title
.text( title
);
1120 * Set the icon of the window.
1122 * @param {string} icon Symbolic name of icon
1125 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1127 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1131 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1138 * Set the position of window to fit with contents.
1140 * @param {string} left Left offset
1141 * @param {string} top Top offset
1144 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1145 this.$element
.css( { 'left': left
, 'top': top
} );
1150 * Set the height of window to fit with contents.
1152 * @param {number} [min=0] Min height
1153 * @param {number} [max] Max height (defaults to content's outer height)
1156 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1157 var height
= this.frame
.$content
.outerHeight();
1159 this.frame
.$element
.css(
1160 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1167 * Set the width of window to fit with contents.
1169 * @param {number} [min=0] Min height
1170 * @param {number} [max] Max height (defaults to content's outer width)
1173 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1174 var width
= this.frame
.$content
.outerWidth();
1176 this.frame
.$element
.css(
1177 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1184 * Initialize window contents.
1186 * The first time the window is opened, #initialize is called when it's safe to begin populating
1187 * its contents. See #setup for a way to make changes each time the window opens.
1189 * Once this method is called, this.$$ can be used to create elements within the frame.
1193 OO
.ui
.Window
.prototype.initialize = function () {
1195 this.$ = this.frame
.$;
1196 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1197 .text( this.title
);
1198 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1199 .addClass( 'oo-ui-icon-' + this.icon
);
1200 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1201 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1202 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1203 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1206 this.frame
.$content
.append(
1207 this.$head
.append( this.$icon
, this.$title
),
1217 * Get a process for setting up a window for use.
1219 * Each time the window is opened this process will set it up for use in a particular context, based
1220 * on the `data` argument.
1222 * When you override this method, you can add additional setup steps to the process the parent
1223 * method provides using the 'first' and 'next' methods.
1226 * @param {Object} [data] Window opening data
1227 * @return {OO.ui.Process} Setup process
1229 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1230 return new OO
.ui
.Process();
1234 * Get a process for readying a window for use.
1236 * Each time the window is open and setup, this process will ready it up for use in a particular
1237 * context, based on the `data` argument.
1239 * When you override this method, you can add additional setup steps to the process the parent
1240 * method provides using the 'first' and 'next' methods.
1243 * @param {Object} [data] Window opening data
1244 * @return {OO.ui.Process} Setup process
1246 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1247 return new OO
.ui
.Process();
1251 * Get a process for tearing down a window after use.
1253 * Each time the window is closed this process will tear it down and do something with the user's
1254 * interactions within the window, based on the `data` argument.
1256 * When you override this method, you can add additional teardown steps to the process the parent
1257 * method provides using the 'first' and 'next' methods.
1260 * @param {Object} [data] Window closing data
1261 * @return {OO.ui.Process} Teardown process
1263 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1264 return new OO
.ui
.Process();
1270 * Do not override this method. Use #getSetupProcess to do something each time the window closes.
1272 * @param {Object} [data] Window opening data
1277 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1278 * first argument will be a promise which will be resolved when the window begins closing
1280 OO
.ui
.Window
.prototype.open = function ( data
) {
1281 // Return existing promise if already opening or open
1282 if ( this.opening
) {
1283 return this.opening
.promise();
1287 this.opening
= $.Deferred();
1289 this.$ariaHidden
= $( 'body' ).children().not( this.$element
.parentsUntil( 'body' ).last() )
1290 .attr( 'aria-hidden', '' );
1292 this.frame
.load().done( OO
.ui
.bind( function () {
1293 this.$element
.show();
1294 this.visible
= true;
1295 this.getSetupProcess( data
).execute().done( OO
.ui
.bind( function () {
1296 this.$element
.addClass( 'oo-ui-window-setup' );
1297 this.emit( 'setup', data
);
1298 setTimeout( OO
.ui
.bind( function () {
1299 this.frame
.$content
.focus();
1300 this.getReadyProcess( data
).execute().done( OO
.ui
.bind( function () {
1301 this.$element
.addClass( 'oo-ui-window-ready' );
1302 this.emit( 'ready', data
);
1303 this.opened
= $.Deferred();
1304 // Now that we are totally done opening, it's safe to allow closing
1305 this.closing
= null;
1306 this.opening
.resolve( this.opened
.promise() );
1312 return this.opening
.promise();
1318 * Do not override this method. Use #getTeardownProcess to do something each time the window closes.
1320 * @param {Object} [data] Window closing data
1323 * @return {jQuery.Promise} Promise resolved when window is closed
1325 OO
.ui
.Window
.prototype.close = function ( data
) {
1328 // Return existing promise if already closing or closed
1329 if ( this.closing
) {
1330 return this.closing
.promise();
1333 // Close after opening is done if opening is in progress
1334 if ( this.opening
&& this.opening
.state() === 'pending' ) {
1335 close
= OO
.ui
.bind( function () {
1336 return this.close( data
);
1338 return this.opening
.then( close
, close
);
1342 // This.closing needs to exist before we emit the closing event so that handlers can call
1343 // window.close() and trigger the safety check above
1344 this.closing
= $.Deferred();
1345 this.frame
.$content
.find( ':focus' ).blur();
1346 this.$element
.removeClass( 'oo-ui-window-ready' );
1347 this.getTeardownProcess( data
).execute().done( OO
.ui
.bind( function () {
1348 this.$element
.removeClass( 'oo-ui-window-setup' );
1349 this.emit( 'teardown', data
);
1350 // To do something different with #opened, resolve/reject #opened in the teardown process
1351 if ( this.opened
&& this.opened
.state() === 'pending' ) {
1352 this.opened
.resolve();
1354 this.$element
.hide();
1355 if ( this.$ariaHidden
) {
1356 this.$ariaHidden
.removeAttr( 'aria-hidden' );
1357 this.$ariaHidden
= undefined;
1359 this.visible
= false;
1360 this.closing
.resolve();
1361 // Now that we are totally done closing, it's safe to allow opening
1362 this.opening
= null;
1365 return this.closing
.promise();
1368 * Set of mutually exclusive windows.
1371 * @extends OO.ui.Element
1372 * @mixins OO.EventEmitter
1375 * @param {OO.Factory} factory Window factory
1376 * @param {Object} [config] Configuration options
1378 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1379 // Parent constructor
1380 OO
.ui
.WindowSet
.super.call( this, config
);
1382 // Mixin constructors
1383 OO
.EventEmitter
.call( this );
1386 this.factory
= factory
;
1389 * List of all windows associated with this window set.
1391 * @property {OO.ui.Window[]}
1393 this.windowList
= [];
1396 * Mapping of OO.ui.Window objects created by name from the #factory.
1398 * @property {Object}
1401 this.currentWindow
= null;
1404 this.$element
.addClass( 'oo-ui-windowSet' );
1409 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1410 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1416 * @param {OO.ui.Window} win Window that's been setup
1417 * @param {Object} config Window opening information
1422 * @param {OO.ui.Window} win Window that's ready
1423 * @param {Object} config Window opening information
1428 * @param {OO.ui.Window} win Window that's been torn down
1429 * @param {Object} config Window closing information
1435 * Handle a window setup event.
1437 * @param {OO.ui.Window} win Window that's been setup
1438 * @param {Object} [config] Window opening information
1441 OO
.ui
.WindowSet
.prototype.onWindowSetup = function ( win
, config
) {
1442 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1443 this.currentWindow
.close();
1445 this.currentWindow
= win
;
1446 this.emit( 'setup', win
, config
);
1450 * Handle a window ready event.
1452 * @param {OO.ui.Window} win Window that's ready
1453 * @param {Object} [config] Window opening information
1456 OO
.ui
.WindowSet
.prototype.onWindowReady = function ( win
, config
) {
1457 this.emit( 'ready', win
, config
);
1461 * Handle a window teardown event.
1463 * @param {OO.ui.Window} win Window that's been torn down
1464 * @param {Object} [config] Window closing information
1467 OO
.ui
.WindowSet
.prototype.onWindowTeardown = function ( win
, config
) {
1468 this.currentWindow
= null;
1469 this.emit( 'teardown', win
, config
);
1473 * Get the current window.
1475 * @return {OO.ui.Window|null} Current window or null if none open
1477 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1478 return this.currentWindow
;
1482 * Return a given window.
1484 * @param {string} name Symbolic name of window
1485 * @return {OO.ui.Window} Window with specified name
1487 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1490 if ( !this.factory
.lookup( name
) ) {
1491 throw new Error( 'Unknown window: ' + name
);
1493 if ( !( name
in this.windows
) ) {
1494 win
= this.windows
[name
] = this.createWindow( name
);
1495 this.addWindow( win
);
1497 return this.windows
[name
];
1501 * Create a window for use in this window set.
1503 * @param {string} name Symbolic name of window
1504 * @return {OO.ui.Window} Window with specified name
1506 OO
.ui
.WindowSet
.prototype.createWindow = function ( name
) {
1507 return this.factory
.create( name
, { '$': this.$ } );
1511 * Add a given window to this window set.
1513 * Connects event handlers and attaches it to the DOM. Calling
1514 * OO.ui.Window#open will not work until the window is added to the set.
1516 * @param {OO.ui.Window} win Window to add
1518 OO
.ui
.WindowSet
.prototype.addWindow = function ( win
) {
1519 if ( this.windowList
.indexOf( win
) !== -1 ) {
1523 this.windowList
.push( win
);
1525 win
.connect( this, {
1526 'setup': [ 'onWindowSetup', win
],
1527 'ready': [ 'onWindowReady', win
],
1528 'teardown': [ 'onWindowTeardown', win
]
1530 this.$element
.append( win
.$element
);
1533 * Modal dialog window.
1537 * @extends OO.ui.Window
1540 * @param {Object} [config] Configuration options
1541 * @cfg {boolean} [footless] Hide foot
1542 * @cfg {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1544 OO
.ui
.Dialog
= function OoUiDialog( config
) {
1545 // Configuration initialization
1546 config
= $.extend( { 'size': 'large' }, config
);
1548 // Parent constructor
1549 OO
.ui
.Dialog
.super.call( this, config
);
1552 this.visible
= false;
1553 this.footless
= !!config
.footless
;
1556 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1557 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1560 this.$element
.on( 'mousedown', false );
1563 this.$element
.addClass( 'oo-ui-dialog' ).attr( 'role', 'dialog' );
1564 this.setSize( config
.size
);
1569 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1571 /* Static Properties */
1574 * Symbolic name of dialog.
1579 * @property {string}
1581 OO
.ui
.Dialog
.static.name
= '';
1584 * Map of symbolic size names and CSS classes.
1588 * @property {Object}
1590 OO
.ui
.Dialog
.static.sizeCssClasses
= {
1591 'small': 'oo-ui-dialog-small',
1592 'medium': 'oo-ui-dialog-medium',
1593 'large': 'oo-ui-dialog-large'
1599 * Handle close button click events.
1601 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1602 this.close( { 'action': 'cancel' } );
1606 * Handle window mouse wheel events.
1608 * @param {jQuery.Event} e Mouse wheel event
1610 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1615 * Handle document key down events.
1617 * @param {jQuery.Event} e Key down event
1619 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1620 switch ( e
.which
) {
1621 case OO
.ui
.Keys
.PAGEUP
:
1622 case OO
.ui
.Keys
.PAGEDOWN
:
1623 case OO
.ui
.Keys
.END
:
1624 case OO
.ui
.Keys
.HOME
:
1625 case OO
.ui
.Keys
.LEFT
:
1627 case OO
.ui
.Keys
.RIGHT
:
1628 case OO
.ui
.Keys
.DOWN
:
1629 // Prevent any key events that might cause scrolling
1635 * Handle frame document key down events.
1637 * @param {jQuery.Event} e Key down event
1639 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1640 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1641 this.close( { 'action': 'cancel' } );
1649 * @param {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1651 OO
.ui
.Dialog
.prototype.setSize = function ( size
) {
1652 var name
, state
, cssClass
,
1653 sizeCssClasses
= OO
.ui
.Dialog
.static.sizeCssClasses
;
1655 if ( !sizeCssClasses
[size
] ) {
1659 for ( name
in sizeCssClasses
) {
1660 state
= name
=== size
;
1661 cssClass
= sizeCssClasses
[name
];
1662 this.$element
.toggleClass( cssClass
, state
);
1669 OO
.ui
.Dialog
.prototype.initialize = function () {
1671 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
1674 this.closeButton
= new OO
.ui
.ButtonWidget( {
1678 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1682 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1683 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1686 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1687 if ( this.footless
) {
1688 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1690 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1691 this.$head
.append( this.closeButton
.$element
);
1697 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
1698 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
1699 .next( function () {
1700 // Prevent scrolling in top-level window
1701 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1702 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1709 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
1710 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
1711 .first( function () {
1712 // Wait for closing transition
1713 return OO
.ui
.Process
.static.delay( 250 );
1715 .next( function () {
1716 // Allow scrolling in top-level window
1717 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1718 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1723 * Check if input is pending.
1727 OO
.ui
.Dialog
.prototype.isPending = function () {
1728 return !!this.pending
;
1732 * Increase the pending stack.
1736 OO
.ui
.Dialog
.prototype.pushPending = function () {
1737 if ( this.pending
=== 0 ) {
1738 this.frame
.$content
.addClass( 'oo-ui-dialog-pending' );
1739 this.$head
.addClass( 'oo-ui-texture-pending' );
1740 this.$foot
.addClass( 'oo-ui-texture-pending' );
1748 * Reduce the pending stack.
1754 OO
.ui
.Dialog
.prototype.popPending = function () {
1755 if ( this.pending
=== 1 ) {
1756 this.frame
.$content
.removeClass( 'oo-ui-dialog-pending' );
1757 this.$head
.removeClass( 'oo-ui-texture-pending' );
1758 this.$foot
.removeClass( 'oo-ui-texture-pending' );
1760 this.pending
= Math
.max( 0, this.pending
- 1 );
1765 * Container for elements.
1769 * @extends OO.ui.Element
1770 * @mixins OO.EventEmitter
1773 * @param {Object} [config] Configuration options
1775 OO
.ui
.Layout
= function OoUiLayout( config
) {
1776 // Initialize config
1777 config
= config
|| {};
1779 // Parent constructor
1780 OO
.ui
.Layout
.super.call( this, config
);
1782 // Mixin constructors
1783 OO
.EventEmitter
.call( this );
1786 this.$element
.addClass( 'oo-ui-layout' );
1791 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1792 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1794 * User interface control.
1798 * @extends OO.ui.Element
1799 * @mixins OO.EventEmitter
1802 * @param {Object} [config] Configuration options
1803 * @cfg {boolean} [disabled=false] Disable
1805 OO
.ui
.Widget
= function OoUiWidget( config
) {
1806 // Initialize config
1807 config
= $.extend( { 'disabled': false }, config
);
1809 // Parent constructor
1810 OO
.ui
.Widget
.super.call( this, config
);
1812 // Mixin constructors
1813 OO
.EventEmitter
.call( this );
1816 this.disabled
= null;
1817 this.wasDisabled
= null;
1820 this.$element
.addClass( 'oo-ui-widget' );
1821 this.setDisabled( !!config
.disabled
);
1826 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1827 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1833 * @param {boolean} disabled Widget is disabled
1839 * Check if the widget is disabled.
1841 * @param {boolean} Button is disabled
1843 OO
.ui
.Widget
.prototype.isDisabled = function () {
1844 return this.disabled
;
1848 * Update the disabled state, in case of changes in parent widget.
1852 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1853 this.setDisabled( this.disabled
);
1858 * Set the disabled state of the widget.
1860 * This should probably change the widgets' appearance and prevent it from being used.
1862 * @param {boolean} disabled Disable widget
1865 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1868 this.disabled
= !!disabled
;
1869 isDisabled
= this.isDisabled();
1870 if ( isDisabled
!== this.wasDisabled
) {
1871 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1872 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1873 this.emit( 'disable', isDisabled
);
1875 this.wasDisabled
= isDisabled
;
1879 * A list of functions, called in sequence.
1881 * If a function added to a process returns boolean false the process will stop; if it returns an
1882 * object with a `promise` method the process will use the promise to either continue to the next
1883 * step when the promise is resolved or stop when the promise is rejected.
1889 OO
.ui
.Process = function () {
1896 OO
.initClass( OO
.ui
.Process
);
1898 /* Static Methods */
1901 * Generate a promise which is resolved after a set amount of time.
1903 * @param {number} length Number of milliseconds before resolving the promise
1904 * @return {jQuery.Promise} Promise that will be resolved after a set amount of time
1906 OO
.ui
.Process
.static.delay = function ( length
) {
1907 var deferred
= $.Deferred();
1909 setTimeout( function () {
1913 return deferred
.promise();
1919 * Start the process.
1921 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
1922 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
1923 * process, the remaining steps will not be taken
1925 OO
.ui
.Process
.prototype.execute = function () {
1926 var i
, len
, promise
;
1929 * Continue execution.
1932 * @param {Array} step A function and the context it should be called in
1933 * @return {Function} Function that continues the process
1935 function proceed( step
) {
1936 return function () {
1937 // Execute step in the correct context
1938 var result
= step
[0].call( step
[1] );
1940 if ( result
=== false ) {
1941 // Use rejected promise for boolean false results
1942 return $.Deferred().reject().promise();
1944 // Duck-type the object to see if it can produce a promise
1945 if ( result
&& $.isFunction( result
.promise
) ) {
1946 // Use a promise generated from the result
1947 return result
.promise();
1949 // Use resolved promise for other results
1950 return $.Deferred().resolve().promise();
1954 if ( this.steps
.length
) {
1955 // Generate a chain reaction of promises
1956 promise
= proceed( this.steps
[0] )();
1957 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
1958 promise
= promise
.then( proceed( this.steps
[i
] ) );
1961 promise
= $.Deferred().resolve().promise();
1968 * Add step to the beginning of the process.
1970 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
1971 * it returns an object with a `promise` method the process will use the promise to either
1972 * continue to the next step when the promise is resolved or stop when the promise is rejected
1973 * @param {Object} [context=null] Context to call the step function in
1976 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
1977 this.steps
.unshift( [ step
, context
|| null ] );
1982 * Add step to the end of the process.
1984 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
1985 * it returns an object with a `promise` method the process will use the promise to either
1986 * continue to the next step when the promise is resolved or stop when the promise is rejected
1987 * @param {Object} [context=null] Context to call the step function in
1990 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
1991 this.steps
.push( [ step
, context
|| null ] );
1995 * Dialog for showing a confirmation/warning message.
1998 * @extends OO.ui.Dialog
2001 * @param {Object} [config] Configuration options
2003 OO
.ui
.ConfirmationDialog
= function OoUiConfirmationDialog( config
) {
2004 // Configuration initialization
2005 config
= $.extend( { 'size': 'small' }, config
);
2007 // Parent constructor
2008 OO
.ui
.Dialog
.call( this, config
);
2013 OO
.inheritClass( OO
.ui
.ConfirmationDialog
, OO
.ui
.Dialog
);
2015 /* Static Properties */
2017 OO
.ui
.ConfirmationDialog
.static.name
= 'confirm';
2019 OO
.ui
.ConfirmationDialog
.static.icon
= 'help';
2021 OO
.ui
.ConfirmationDialog
.static.title
= OO
.ui
.deferMsg( 'ooui-dialog-confirm-title' );
2028 OO
.ui
.ConfirmationDialog
.prototype.initialize = function () {
2030 OO
.ui
.Dialog
.prototype.initialize
.call( this );
2032 // Set up the layout
2033 var contentLayout
= new OO
.ui
.PanelLayout( {
2038 this.$promptContainer
= this.$( '<div>' ).addClass( 'oo-ui-dialog-confirm-promptContainer' );
2040 this.cancelButton
= new OO
.ui
.ButtonWidget();
2041 this.cancelButton
.connect( this, { 'click': [ 'close', 'cancel' ] } );
2043 this.okButton
= new OO
.ui
.ButtonWidget();
2044 this.okButton
.connect( this, { 'click': [ 'close', 'ok' ] } );
2047 contentLayout
.$element
.append( this.$promptContainer
);
2048 this.$body
.append( contentLayout
.$element
);
2051 this.okButton
.$element
,
2052 this.cancelButton
.$element
2055 this.connect( this, { 'teardown': [ 'close', 'cancel' ] } );
2059 * Setup a confirmation dialog.
2061 * @param {Object} [data] Window opening data including text of the dialog and text for the buttons
2062 * @param {jQuery|string} [data.prompt] Text to display or list of nodes to use as content of the dialog.
2063 * @param {jQuery|string|Function|null} [data.okLabel] Label of the OK button
2064 * @param {jQuery|string|Function|null} [data.cancelLabel] Label of the cancel button
2065 * @param {string|string[]} [data.okFlags="constructive"] Flags for the OK button
2066 * @param {string|string[]} [data.cancelFlags="destructive"] Flags for the cancel button
2067 * @return {OO.ui.Process} Setup process
2069 OO
.ui
.ConfirmationDialog
.prototype.getSetupProcess = function ( data
) {
2071 return OO
.ui
.ConfirmationDialog
.super.prototype.getSetupProcess
.call( this, data
)
2072 .next( function () {
2073 var prompt
= data
.prompt
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-prompt' ),
2074 okLabel
= data
.okLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-ok' ),
2075 cancelLabel
= data
.cancelLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-cancel' ),
2076 okFlags
= data
.okFlags
|| 'constructive',
2077 cancelFlags
= data
.cancelFlags
|| 'destructive';
2079 if ( typeof prompt
=== 'string' ) {
2080 this.$promptContainer
.text( prompt
);
2082 this.$promptContainer
.empty().append( prompt
);
2085 this.okButton
.setLabel( okLabel
).clearFlags().setFlags( okFlags
);
2086 this.cancelButton
.setLabel( cancelLabel
).clearFlags().setFlags( cancelFlags
);
2093 OO
.ui
.ConfirmationDialog
.prototype.getTeardownProcess = function ( data
) {
2095 return OO
.ui
.ConfirmationDialog
.super.prototype.getTeardownProcess
.call( this, data
)
2096 .first( function () {
2097 if ( data
=== 'ok' ) {
2098 this.opened
.resolve();
2099 } else if ( data
=== 'cancel' ) {
2100 this.opened
.reject();
2105 * Element with a button.
2111 * @param {jQuery} $button Button node, assigned to #$button
2112 * @param {Object} [config] Configuration options
2113 * @cfg {boolean} [frameless] Render button without a frame
2114 * @cfg {number} [tabIndex=0] Button's tab index, use -1 to prevent tab focusing
2116 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
2117 // Configuration initialization
2118 config
= config
|| {};
2121 this.$button
= $button
;
2122 this.tabIndex
= null;
2123 this.active
= false;
2124 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
2127 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2130 this.$element
.addClass( 'oo-ui-buttonedElement' );
2132 .addClass( 'oo-ui-buttonedElement-button' )
2133 .attr( 'role', 'button' )
2134 .prop( 'tabIndex', config
.tabIndex
|| 0 );
2135 if ( config
.frameless
) {
2136 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
2138 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
2144 OO
.initClass( OO
.ui
.ButtonedElement
);
2146 /* Static Properties */
2149 * Cancel mouse down events.
2153 * @property {boolean}
2155 OO
.ui
.ButtonedElement
.static.cancelButtonMouseDownEvents
= true;
2160 * Handles mouse down events.
2162 * @param {jQuery.Event} e Mouse down event
2164 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function ( e
) {
2165 if ( this.isDisabled() || e
.which
!== 1 ) {
2168 // tabIndex should generally be interacted with via the property, but it's not possible to
2169 // reliably unset a tabIndex via a property so we use the (lowercase) "tabindex" attribute
2170 this.tabIndex
= this.$button
.attr( 'tabindex' );
2172 // Remove the tab-index while the button is down to prevent the button from stealing focus
2173 .removeAttr( 'tabindex' )
2174 .addClass( 'oo-ui-buttonedElement-pressed' );
2175 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
2176 // reliably reapply the tabindex and remove the pressed class
2177 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
2178 // Prevent change of focus unless specifically configured otherwise
2179 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
2185 * Handles mouse up events.
2187 * @param {jQuery.Event} e Mouse up event
2189 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function ( e
) {
2190 if ( this.isDisabled() || e
.which
!== 1 ) {
2194 // Restore the tab-index after the button is up to restore the button's accesssibility
2195 .attr( 'tabindex', this.tabIndex
)
2196 .removeClass( 'oo-ui-buttonedElement-pressed' );
2197 // Stop listening for mouseup, since we only needed this once
2198 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
2204 * @param {boolean} [value] Make button active
2207 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
2208 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
2212 * Element that can be automatically clipped to visible boundaies.
2218 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
2219 * @param {Object} [config] Configuration options
2221 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
2222 // Configuration initialization
2223 config
= config
|| {};
2226 this.$clippable
= $clippable
;
2227 this.clipping
= false;
2228 this.clipped
= false;
2229 this.$clippableContainer
= null;
2230 this.$clippableScroller
= null;
2231 this.$clippableWindow
= null;
2232 this.idealWidth
= null;
2233 this.idealHeight
= null;
2234 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
2235 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
2238 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
2246 * @param {boolean} value Enable clipping
2249 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
2252 if ( this.clipping
!== value
) {
2253 this.clipping
= value
;
2254 if ( this.clipping
) {
2255 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
2256 // If the clippable container is the body, we have to listen to scroll events and check
2257 // jQuery.scrollTop on the window because of browser inconsistencies
2258 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
2259 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
2260 this.$clippableContainer
;
2261 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
2262 this.$clippableWindow
= this.$( this.getElementWindow() )
2263 .on( 'resize', this.onClippableWindowResizeHandler
);
2264 // Initial clip after visible
2265 setTimeout( OO
.ui
.bind( this.clip
, this ) );
2267 this.$clippableContainer
= null;
2268 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
2269 this.$clippableScroller
= null;
2270 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
2271 this.$clippableWindow
= null;
2279 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
2281 * @return {boolean} Element will be clipped to the visible area
2283 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
2284 return this.clipping
;
2288 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
2290 * @return {boolean} Part of the element is being clipped
2292 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
2293 return this.clipped
;
2297 * Set the ideal size.
2299 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
2300 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
2302 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
2303 this.idealWidth
= width
;
2304 this.idealHeight
= height
;
2308 * Clip element to visible boundaries and allow scrolling when needed.
2310 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
2311 * overlapped by, the visible area of the nearest scrollable container.
2315 OO
.ui
.ClippableElement
.prototype.clip = function () {
2316 if ( !this.clipping
) {
2317 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
2322 cOffset
= this.$clippable
.offset(),
2323 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
2324 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
2325 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
2326 scrollTop
= this.$clippableScroller
.scrollTop(),
2327 scrollLeft
= this.$clippableScroller
.scrollLeft(),
2328 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
2329 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
2330 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
2331 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
2332 clipWidth
= desiredWidth
< naturalWidth
,
2333 clipHeight
= desiredHeight
< naturalHeight
;
2336 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
2338 this.$clippable
.css( { 'overflow-x': '', 'width': this.idealWidth
|| '' } );
2341 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
2343 this.$clippable
.css( { 'overflow-y': '', 'height': this.idealHeight
|| '' } );
2346 this.clipped
= clipWidth
|| clipHeight
;
2351 * Element with named flags that can be added, removed, listed and checked.
2353 * A flag, when set, adds a CSS class on the `$element` by combing `oo-ui-flaggableElement-` with
2354 * the flag name. Flags are primarily useful for styling.
2360 * @param {Object} [config] Configuration options
2361 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
2363 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
2364 // Config initialization
2365 config
= config
|| {};
2371 this.setFlags( config
.flags
);
2377 * Check if a flag is set.
2379 * @param {string} flag Name of flag
2380 * @return {boolean} Has flag
2382 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
2383 return flag
in this.flags
;
2387 * Get the names of all flags set.
2389 * @return {string[]} flags Flag names
2391 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
2392 return Object
.keys( this.flags
);
2400 OO
.ui
.FlaggableElement
.prototype.clearFlags = function () {
2402 classPrefix
= 'oo-ui-flaggableElement-';
2404 for ( flag
in this.flags
) {
2405 delete this.flags
[flag
];
2406 this.$element
.removeClass( classPrefix
+ flag
);
2413 * Add one or more flags.
2415 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
2416 * keyed by flag name containing boolean set/remove instructions.
2419 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
2421 classPrefix
= 'oo-ui-flaggableElement-';
2423 if ( typeof flags
=== 'string' ) {
2425 this.flags
[flags
] = true;
2426 this.$element
.addClass( classPrefix
+ flags
);
2427 } else if ( $.isArray( flags
) ) {
2428 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
2431 this.flags
[flag
] = true;
2432 this.$element
.addClass( classPrefix
+ flag
);
2434 } else if ( OO
.isPlainObject( flags
) ) {
2435 for ( flag
in flags
) {
2436 if ( flags
[flag
] ) {
2438 this.flags
[flag
] = true;
2439 this.$element
.addClass( classPrefix
+ flag
);
2442 delete this.flags
[flag
];
2443 this.$element
.removeClass( classPrefix
+ flag
);
2450 * Element containing a sequence of child elements.
2456 * @param {jQuery} $group Container node, assigned to #$group
2457 * @param {Object} [config] Configuration options
2459 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2461 config
= config
|| {};
2464 this.$group
= $group
;
2466 this.aggregateItemEvents
= {};
2474 * @return {OO.ui.Element[]} Items
2476 OO
.ui
.GroupElement
.prototype.getItems = function () {
2477 return this.items
.slice( 0 );
2481 * Add an aggregate item event.
2483 * Aggregated events are listened to on each item and then emitted by the group under a new name,
2484 * and with an additional leading parameter containing the item that emitted the original event.
2485 * Other arguments that were emitted from the original event are passed through.
2487 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
2488 * event, use null value to remove aggregation
2489 * @throws {Error} If aggregation already exists
2491 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
2492 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
2494 for ( itemEvent
in events
) {
2495 groupEvent
= events
[itemEvent
];
2497 // Remove existing aggregated event
2498 if ( itemEvent
in this.aggregateItemEvents
) {
2499 // Don't allow duplicate aggregations
2501 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
2503 // Remove event aggregation from existing items
2504 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2505 item
= this.items
[i
];
2506 if ( item
.connect
&& item
.disconnect
) {
2508 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
2509 item
.disconnect( this, remove
);
2512 // Prevent future items from aggregating event
2513 delete this.aggregateItemEvents
[itemEvent
];
2516 // Add new aggregate event
2518 // Make future items aggregate event
2519 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
2520 // Add event aggregation to existing items
2521 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2522 item
= this.items
[i
];
2523 if ( item
.connect
&& item
.disconnect
) {
2525 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
2526 item
.connect( this, add
);
2536 * @param {OO.ui.Element[]} items Item
2537 * @param {number} [index] Index to insert items at
2540 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2541 var i
, len
, item
, event
, events
, currentIndex
,
2544 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2547 // Check if item exists then remove it first, effectively "moving" it
2548 currentIndex
= $.inArray( item
, this.items
);
2549 if ( currentIndex
>= 0 ) {
2550 this.removeItems( [ item
] );
2551 // Adjust index to compensate for removal
2552 if ( currentIndex
< index
) {
2557 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
2559 for ( event
in this.aggregateItemEvents
) {
2560 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
2562 item
.connect( this, events
);
2564 item
.setElementGroup( this );
2565 itemElements
.push( item
.$element
.get( 0 ) );
2568 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2569 this.$group
.append( itemElements
);
2570 this.items
.push
.apply( this.items
, items
);
2571 } else if ( index
=== 0 ) {
2572 this.$group
.prepend( itemElements
);
2573 this.items
.unshift
.apply( this.items
, items
);
2575 this.items
[index
].$element
.before( itemElements
);
2576 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2585 * Items will be detached, not removed, so they can be used later.
2587 * @param {OO.ui.Element[]} items Items to remove
2590 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2591 var i
, len
, item
, index
, remove
, itemEvent
;
2593 // Remove specific items
2594 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2596 index
= $.inArray( item
, this.items
);
2597 if ( index
!== -1 ) {
2599 item
.connect
&& item
.disconnect
&&
2600 !$.isEmptyObject( this.aggregateItemEvents
)
2603 if ( itemEvent
in this.aggregateItemEvents
) {
2604 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2606 item
.disconnect( this, remove
);
2608 item
.setElementGroup( null );
2609 this.items
.splice( index
, 1 );
2610 item
.$element
.detach();
2620 * Items will be detached, not removed, so they can be used later.
2624 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2625 var i
, len
, item
, remove
, itemEvent
;
2628 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2629 item
= this.items
[i
];
2631 item
.connect
&& item
.disconnect
&&
2632 !$.isEmptyObject( this.aggregateItemEvents
)
2635 if ( itemEvent
in this.aggregateItemEvents
) {
2636 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2638 item
.disconnect( this, remove
);
2640 item
.setElementGroup( null );
2641 item
.$element
.detach();
2648 * Element containing an icon.
2654 * @param {jQuery} $icon Icon node, assigned to #$icon
2655 * @param {Object} [config] Configuration options
2656 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2657 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2660 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2661 // Config intialization
2662 config
= config
|| {};
2669 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2670 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2675 OO
.initClass( OO
.ui
.IconedElement
);
2677 /* Static Properties */
2682 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2684 * For i18n purposes, this property can be an object containing a `default` icon name property and
2685 * additional icon names keyed by language code.
2687 * Example of i18n icon definition:
2688 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2692 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2693 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2696 OO
.ui
.IconedElement
.static.icon
= null;
2703 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
2704 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2708 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2709 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2712 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2714 if ( typeof icon
=== 'string' ) {
2716 if ( icon
.length
) {
2717 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2721 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2729 * @return {string} Icon
2731 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2735 * Element containing an indicator.
2741 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2742 * @param {Object} [config] Configuration options
2743 * @cfg {string} [indicator] Symbolic indicator name
2744 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2746 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2747 // Config intialization
2748 config
= config
|| {};
2751 this.$indicator
= $indicator
;
2752 this.indicator
= null;
2753 this.indicatorLabel
= null;
2756 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2757 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2758 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2763 OO
.initClass( OO
.ui
.IndicatedElement
);
2765 /* Static Properties */
2772 * @property {string|null} Symbolic indicator name or null for no indicator
2774 OO
.ui
.IndicatedElement
.static.indicator
= null;
2781 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2784 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2791 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2794 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2795 if ( this.indicator
) {
2796 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2797 this.indicator
= null;
2799 if ( typeof indicator
=== 'string' ) {
2800 indicator
= indicator
.trim();
2801 if ( indicator
.length
) {
2802 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2803 this.indicator
= indicator
;
2806 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2812 * Set indicator label.
2814 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2815 * for no indicator title
2818 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2819 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2821 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2822 this.$indicator
.attr( 'title', indicatorTitle
);
2824 this.$indicator
.removeAttr( 'title' );
2833 * @return {string} title Symbolic name of indicator
2835 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2836 return this.indicator
;
2840 * Get indicator title.
2842 * @return {string} Indicator title text
2844 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2845 return this.indicatorTitle
;
2848 * Element containing a label.
2854 * @param {jQuery} $label Label node, assigned to #$label
2855 * @param {Object} [config] Configuration options
2856 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2857 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
2859 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2860 // Config intialization
2861 config
= config
|| {};
2864 this.$label
= $label
;
2868 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2869 this.setLabel( config
.label
|| this.constructor.static.label
);
2870 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
2875 OO
.initClass( OO
.ui
.LabeledElement
);
2877 /* Static Properties */
2884 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2887 OO
.ui
.LabeledElement
.static.label
= null;
2894 * An empty string will result in the label being hidden. A string containing only whitespace will
2895 * be converted to a single
2897 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2898 * text; or null for no label
2901 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2904 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2905 if ( typeof label
=== 'string' && label
.length
) {
2906 if ( label
.match( /^\s*$/ ) ) {
2907 // Convert whitespace only string to a single non-breaking space
2908 this.$label
.html( ' ' );
2910 this.$label
.text( label
);
2912 } else if ( label
instanceof jQuery
) {
2913 this.$label
.empty().append( label
);
2915 this.$label
.empty();
2918 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2919 this.$label
.css( 'display', empty
? 'none' : '' );
2927 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2928 * text; or null for no label
2930 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2939 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
2940 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
2941 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
2946 * Popuppable element.
2952 * @param {Object} [config] Configuration options
2953 * @cfg {number} [popupWidth=320] Width of popup
2954 * @cfg {number} [popupHeight] Height of popup
2955 * @cfg {Object} [popup] Configuration to pass to popup
2957 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
2958 // Configuration initialization
2959 config
= $.extend( { 'popupWidth': 320 }, config
);
2962 this.popup
= new OO
.ui
.PopupWidget( $.extend(
2963 { 'align': 'center', 'autoClose': true },
2965 { '$': this.$, '$autoCloseIgnore': this.$element
}
2967 this.popupWidth
= config
.popupWidth
;
2968 this.popupHeight
= config
.popupHeight
;
2976 * @return {OO.ui.PopupWidget} Popup widget
2978 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
2985 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
2986 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
2992 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
2996 * Element with a title.
3002 * @param {jQuery} $label Titled node, assigned to #$titled
3003 * @param {Object} [config] Configuration options
3004 * @cfg {string|Function} [title] Title text or a function that returns text
3006 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
3007 // Config intialization
3008 config
= config
|| {};
3011 this.$titled
= $titled
;
3015 this.setTitle( config
.title
|| this.constructor.static.title
);
3020 OO
.initClass( OO
.ui
.TitledElement
);
3022 /* Static Properties */
3029 * @property {string|Function} Title text or a function that returns text
3031 OO
.ui
.TitledElement
.static.title
= null;
3038 * @param {string|Function|null} title Title text, a function that returns text or null for no title
3041 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
3042 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
3044 if ( typeof title
=== 'string' && title
.length
) {
3045 this.$titled
.attr( 'title', title
);
3047 this.$titled
.removeAttr( 'title' );
3056 * @return {string} Title string
3058 OO
.ui
.TitledElement
.prototype.getTitle = function () {
3062 * Generic toolbar tool.
3066 * @extends OO.ui.Widget
3067 * @mixins OO.ui.IconedElement
3070 * @param {OO.ui.ToolGroup} toolGroup
3071 * @param {Object} [config] Configuration options
3072 * @cfg {string|Function} [title] Title text or a function that returns text
3074 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
3075 // Config intialization
3076 config
= config
|| {};
3078 // Parent constructor
3079 OO
.ui
.Tool
.super.call( this, config
);
3081 // Mixin constructors
3082 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
3085 this.toolGroup
= toolGroup
;
3086 this.toolbar
= this.toolGroup
.getToolbar();
3087 this.active
= false;
3088 this.$title
= this.$( '<span>' );
3089 this.$link
= this.$( '<a>' );
3093 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
3096 this.$title
.addClass( 'oo-ui-tool-title' );
3098 .addClass( 'oo-ui-tool-link' )
3099 .append( this.$icon
, this.$title
);
3101 .data( 'oo-ui-tool', this )
3103 'oo-ui-tool ' + 'oo-ui-tool-name-' +
3104 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
3106 .append( this.$link
);
3107 this.setTitle( config
.title
|| this.constructor.static.title
);
3112 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
3113 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
3121 /* Static Properties */
3127 OO
.ui
.Tool
.static.tagName
= 'span';
3130 * Symbolic name of tool.
3135 * @property {string}
3137 OO
.ui
.Tool
.static.name
= '';
3145 * @property {string}
3147 OO
.ui
.Tool
.static.group
= '';
3152 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
3153 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
3154 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
3155 * appended to the title if the tool is part of a bar tool group.
3160 * @property {string|Function} Title text or a function that returns text
3162 OO
.ui
.Tool
.static.title
= '';
3165 * Tool can be automatically added to catch-all groups.
3169 * @property {boolean}
3171 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
3174 * Tool can be automatically added to named groups.
3177 * @property {boolean}
3180 OO
.ui
.Tool
.static.autoAddToGroup
= true;
3183 * Check if this tool is compatible with given data.
3187 * @param {Mixed} data Data to check
3188 * @return {boolean} Tool can be used with data
3190 OO
.ui
.Tool
.static.isCompatibleWith = function () {
3197 * Handle the toolbar state being updated.
3199 * This is an abstract method that must be overridden in a concrete subclass.
3203 OO
.ui
.Tool
.prototype.onUpdateState = function () {
3205 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
3210 * Handle the tool being selected.
3212 * This is an abstract method that must be overridden in a concrete subclass.
3216 OO
.ui
.Tool
.prototype.onSelect = function () {
3218 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
3223 * Check if the button is active.
3225 * @param {boolean} Button is active
3227 OO
.ui
.Tool
.prototype.isActive = function () {
3232 * Make the button appear active or inactive.
3234 * @param {boolean} state Make button appear active
3236 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
3237 this.active
= !!state
;
3238 if ( this.active
) {
3239 this.$element
.addClass( 'oo-ui-tool-active' );
3241 this.$element
.removeClass( 'oo-ui-tool-active' );
3246 * Get the tool title.
3248 * @param {string|Function} title Title text or a function that returns text
3251 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
3252 this.title
= OO
.ui
.resolveMsg( title
);
3258 * Get the tool title.
3260 * @return {string} Title text
3262 OO
.ui
.Tool
.prototype.getTitle = function () {
3267 * Get the tool's symbolic name.
3269 * @return {string} Symbolic name of tool
3271 OO
.ui
.Tool
.prototype.getName = function () {
3272 return this.constructor.static.name
;
3278 OO
.ui
.Tool
.prototype.updateTitle = function () {
3279 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
3280 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
3281 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
3288 .addClass( 'oo-ui-tool-accel' )
3292 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
3293 tooltipParts
.push( this.title
);
3295 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
3296 tooltipParts
.push( accel
);
3298 if ( tooltipParts
.length
) {
3299 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
3301 this.$link
.removeAttr( 'title' );
3308 OO
.ui
.Tool
.prototype.destroy = function () {
3309 this.toolbar
.disconnect( this );
3310 this.$element
.remove();
3313 * Collection of tool groups.
3316 * @extends OO.ui.Element
3317 * @mixins OO.EventEmitter
3318 * @mixins OO.ui.GroupElement
3321 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
3322 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
3323 * @param {Object} [config] Configuration options
3324 * @cfg {boolean} [actions] Add an actions section opposite to the tools
3325 * @cfg {boolean} [shadow] Add a shadow below the toolbar
3327 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
3328 // Configuration initialization
3329 config
= config
|| {};
3331 // Parent constructor
3332 OO
.ui
.Toolbar
.super.call( this, config
);
3334 // Mixin constructors
3335 OO
.EventEmitter
.call( this );
3336 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3339 this.toolFactory
= toolFactory
;
3340 this.toolGroupFactory
= toolGroupFactory
;
3343 this.$bar
= this.$( '<div>' );
3344 this.$actions
= this.$( '<div>' );
3345 this.initialized
= false;
3349 .add( this.$bar
).add( this.$group
).add( this.$actions
)
3350 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
3353 this.$group
.addClass( 'oo-ui-toolbar-tools' );
3354 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
3355 if ( config
.actions
) {
3356 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
3357 this.$bar
.append( this.$actions
);
3359 this.$bar
.append( '<div style="clear:both"></div>' );
3360 if ( config
.shadow
) {
3361 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
3363 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
3368 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
3369 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
3370 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
3375 * Get the tool factory.
3377 * @return {OO.ui.ToolFactory} Tool factory
3379 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
3380 return this.toolFactory
;
3384 * Get the tool group factory.
3386 * @return {OO.Factory} Tool group factory
3388 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
3389 return this.toolGroupFactory
;
3393 * Handles mouse down events.
3395 * @param {jQuery.Event} e Mouse down event
3397 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
3398 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
3399 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
3400 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
3406 * Sets up handles and preloads required information for the toolbar to work.
3407 * This must be called immediately after it is attached to a visible document.
3409 OO
.ui
.Toolbar
.prototype.initialize = function () {
3410 this.initialized
= true;
3416 * Tools can be specified in the following ways:
3418 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3419 * - All tools in a group: `{ 'group': 'group-name' }`
3420 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
3422 * @param {Object.<string,Array>} groups List of tool group configurations
3423 * @param {Array|string} [groups.include] Tools to include
3424 * @param {Array|string} [groups.exclude] Tools to exclude
3425 * @param {Array|string} [groups.promote] Tools to promote to the beginning
3426 * @param {Array|string} [groups.demote] Tools to demote to the end
3428 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
3429 var i
, len
, type
, group
,
3431 defaultType
= 'bar';
3433 // Cleanup previous groups
3436 // Build out new groups
3437 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
3439 if ( group
.include
=== '*' ) {
3440 // Apply defaults to catch-all groups
3441 if ( group
.type
=== undefined ) {
3442 group
.type
= 'list';
3444 if ( group
.label
=== undefined ) {
3445 group
.label
= 'ooui-toolbar-more';
3448 // Check type has been registered
3449 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
3451 this.getToolGroupFactory().create( type
, this, $.extend( { '$': this.$ }, group
) )
3454 this.addItems( items
);
3458 * Remove all tools and groups from the toolbar.
3460 OO
.ui
.Toolbar
.prototype.reset = function () {
3465 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3466 this.items
[i
].destroy();
3472 * Destroys toolbar, removing event handlers and DOM elements.
3474 * Call this whenever you are done using a toolbar.
3476 OO
.ui
.Toolbar
.prototype.destroy = function () {
3478 this.$element
.remove();
3482 * Check if tool has not been used yet.
3484 * @param {string} name Symbolic name of tool
3485 * @return {boolean} Tool is available
3487 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
3488 return !this.tools
[name
];
3492 * Prevent tool from being used again.
3494 * @param {OO.ui.Tool} tool Tool to reserve
3496 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
3497 this.tools
[tool
.getName()] = tool
;
3501 * Allow tool to be used again.
3503 * @param {OO.ui.Tool} tool Tool to release
3505 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
3506 delete this.tools
[tool
.getName()];
3510 * Get accelerator label for tool.
3512 * This is a stub that should be overridden to provide access to accelerator information.
3514 * @param {string} name Symbolic name of tool
3515 * @return {string|undefined} Tool accelerator label if available
3517 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
3521 * Factory for tools.
3524 * @extends OO.Factory
3527 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3528 // Parent constructor
3529 OO
.ui
.ToolFactory
.super.call( this );
3534 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3539 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3540 var i
, len
, included
, promoted
, demoted
,
3544 // Collect included and not excluded tools
3545 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3548 promoted
= this.extract( promote
, used
);
3549 demoted
= this.extract( demote
, used
);
3552 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3553 if ( !used
[included
[i
]] ) {
3554 auto
.push( included
[i
] );
3558 return promoted
.concat( auto
).concat( demoted
);
3562 * Get a flat list of names from a list of names or groups.
3564 * Tools can be specified in the following ways:
3566 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3567 * - All tools in a group: `{ 'group': 'group-name' }`
3568 * - All tools: `'*'`
3571 * @param {Array|string} collection List of tools
3572 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3573 * names will be added as properties
3574 * @return {string[]} List of extracted names
3576 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3577 var i
, len
, item
, name
, tool
,
3580 if ( collection
=== '*' ) {
3581 for ( name
in this.registry
) {
3582 tool
= this.registry
[name
];
3584 // Only add tools by group name when auto-add is enabled
3585 tool
.static.autoAddToCatchall
&&
3586 // Exclude already used tools
3587 ( !used
|| !used
[name
] )
3595 } else if ( $.isArray( collection
) ) {
3596 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3597 item
= collection
[i
];
3598 // Allow plain strings as shorthand for named tools
3599 if ( typeof item
=== 'string' ) {
3600 item
= { 'name': item
};
3602 if ( OO
.isPlainObject( item
) ) {
3604 for ( name
in this.registry
) {
3605 tool
= this.registry
[name
];
3607 // Include tools with matching group
3608 tool
.static.group
=== item
.group
&&
3609 // Only add tools by group name when auto-add is enabled
3610 tool
.static.autoAddToGroup
&&
3611 // Exclude already used tools
3612 ( !used
|| !used
[name
] )
3620 // Include tools with matching name and exclude already used tools
3621 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3622 names
.push( item
.name
);
3624 used
[item
.name
] = true;
3633 * Collection of tools.
3635 * Tools can be specified in the following ways:
3637 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3638 * - All tools in a group: `{ 'group': 'group-name' }`
3639 * - All tools: `'*'`
3643 * @extends OO.ui.Widget
3644 * @mixins OO.ui.GroupElement
3647 * @param {OO.ui.Toolbar} toolbar
3648 * @param {Object} [config] Configuration options
3649 * @cfg {Array|string} [include=[]] List of tools to include
3650 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3651 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3652 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3654 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3655 // Configuration initialization
3656 config
= config
|| {};
3658 // Parent constructor
3659 OO
.ui
.ToolGroup
.super.call( this, config
);
3661 // Mixin constructors
3662 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3665 this.toolbar
= toolbar
;
3667 this.pressed
= null;
3668 this.autoDisabled
= false;
3669 this.include
= config
.include
|| [];
3670 this.exclude
= config
.exclude
|| [];
3671 this.promote
= config
.promote
|| [];
3672 this.demote
= config
.demote
|| [];
3673 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3677 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3678 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3679 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3680 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3682 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3683 this.aggregate( { 'disable': 'itemDisable' } );
3684 this.connect( this, { 'itemDisable': 'updateDisabled' } );
3687 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3689 .addClass( 'oo-ui-toolGroup' )
3690 .append( this.$group
);
3696 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3697 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3705 /* Static Properties */
3708 * Show labels in tooltips.
3712 * @property {boolean}
3714 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3717 * Show acceleration labels in tooltips.
3721 * @property {boolean}
3723 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3726 * Automatically disable the toolgroup when all tools are disabled
3730 * @property {boolean}
3732 OO
.ui
.ToolGroup
.static.autoDisable
= true;
3739 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
3740 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
3746 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
3747 var i
, item
, allDisabled
= true;
3749 if ( this.constructor.static.autoDisable
) {
3750 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
3751 item
= this.items
[i
];
3752 if ( !item
.isDisabled() ) {
3753 allDisabled
= false;
3757 this.autoDisabled
= allDisabled
;
3759 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
3763 * Handle mouse down events.
3765 * @param {jQuery.Event} e Mouse down event
3767 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3768 if ( !this.isDisabled() && e
.which
=== 1 ) {
3769 this.pressed
= this.getTargetTool( e
);
3770 if ( this.pressed
) {
3771 this.pressed
.setActive( true );
3772 this.getElementDocument().addEventListener(
3773 'mouseup', this.onCapturedMouseUpHandler
, true
3781 * Handle captured mouse up events.
3783 * @param {Event} e Mouse up event
3785 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3786 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3787 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3788 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3789 this.onMouseUp( e
);
3793 * Handle mouse up events.
3795 * @param {jQuery.Event} e Mouse up event
3797 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3798 var tool
= this.getTargetTool( e
);
3800 if ( !this.isDisabled() && e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3801 this.pressed
.onSelect();
3804 this.pressed
= null;
3809 * Handle mouse over events.
3811 * @param {jQuery.Event} e Mouse over event
3813 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3814 var tool
= this.getTargetTool( e
);
3816 if ( this.pressed
&& this.pressed
=== tool
) {
3817 this.pressed
.setActive( true );
3822 * Handle mouse out events.
3824 * @param {jQuery.Event} e Mouse out event
3826 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3827 var tool
= this.getTargetTool( e
);
3829 if ( this.pressed
&& this.pressed
=== tool
) {
3830 this.pressed
.setActive( false );
3835 * Get the closest tool to a jQuery.Event.
3837 * Only tool links are considered, which prevents other elements in the tool such as popups from
3838 * triggering tool group interactions.
3841 * @param {jQuery.Event} e
3842 * @return {OO.ui.Tool|null} Tool, `null` if none was found
3844 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3846 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3848 if ( $item
.length
) {
3849 tool
= $item
.parent().data( 'oo-ui-tool' );
3852 return tool
&& !tool
.isDisabled() ? tool
: null;
3856 * Handle tool registry register events.
3858 * If a tool is registered after the group is created, we must repopulate the list to account for:
3860 * - a tool being added that may be included
3861 * - a tool already included being overridden
3863 * @param {string} name Symbolic name of tool
3865 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3870 * Get the toolbar this group is in.
3872 * @return {OO.ui.Toolbar} Toolbar of group
3874 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3875 return this.toolbar
;
3879 * Add and remove tools based on configuration.
3881 OO
.ui
.ToolGroup
.prototype.populate = function () {
3882 var i
, len
, name
, tool
,
3883 toolFactory
= this.toolbar
.getToolFactory(),
3887 list
= this.toolbar
.getToolFactory().getTools(
3888 this.include
, this.exclude
, this.promote
, this.demote
3891 // Build a list of needed tools
3892 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3896 toolFactory
.lookup( name
) &&
3897 // Tool is available or is already in this group
3898 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3900 tool
= this.tools
[name
];
3902 // Auto-initialize tools on first use
3903 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3906 this.toolbar
.reserveTool( tool
);
3911 // Remove tools that are no longer needed
3912 for ( name
in this.tools
) {
3913 if ( !names
[name
] ) {
3914 this.tools
[name
].destroy();
3915 this.toolbar
.releaseTool( this.tools
[name
] );
3916 remove
.push( this.tools
[name
] );
3917 delete this.tools
[name
];
3920 if ( remove
.length
) {
3921 this.removeItems( remove
);
3923 // Update emptiness state
3925 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3927 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3929 // Re-add tools (moving existing ones to new locations)
3930 this.addItems( add
);
3931 // Disabled state may depend on items
3932 this.updateDisabled();
3936 * Destroy tool group.
3938 OO
.ui
.ToolGroup
.prototype.destroy = function () {
3942 this.toolbar
.getToolFactory().disconnect( this );
3943 for ( name
in this.tools
) {
3944 this.toolbar
.releaseTool( this.tools
[name
] );
3945 this.tools
[name
].disconnect( this ).destroy();
3946 delete this.tools
[name
];
3948 this.$element
.remove();
3951 * Factory for tool groups.
3954 * @extends OO.Factory
3957 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3958 // Parent constructor
3959 OO
.Factory
.call( this );
3962 defaultClasses
= this.constructor.static.getDefaultClasses();
3964 // Register default toolgroups
3965 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3966 this.register( defaultClasses
[i
] );
3972 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3974 /* Static Methods */
3977 * Get a default set of classes to be registered on construction
3979 * @return {Function[]} Default classes
3981 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3984 OO
.ui
.ListToolGroup
,
3989 * Layout made of a fieldset and optional legend.
3991 * Just add OO.ui.FieldLayout items.
3994 * @extends OO.ui.Layout
3995 * @mixins OO.ui.LabeledElement
3996 * @mixins OO.ui.IconedElement
3997 * @mixins OO.ui.GroupElement
4000 * @param {Object} [config] Configuration options
4001 * @cfg {string} [icon] Symbolic icon name
4002 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
4004 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
4005 // Config initialization
4006 config
= config
|| {};
4008 // Parent constructor
4009 OO
.ui
.FieldsetLayout
.super.call( this, config
);
4011 // Mixin constructors
4012 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
4013 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
4014 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4018 .addClass( 'oo-ui-fieldsetLayout' )
4019 .prepend( this.$icon
, this.$label
, this.$group
);
4020 if ( $.isArray( config
.items
) ) {
4021 this.addItems( config
.items
);
4027 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
4028 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
4029 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
4030 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
4032 /* Static Properties */
4034 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
4036 * Layout made of a field and optional label.
4039 * @extends OO.ui.Layout
4040 * @mixins OO.ui.LabeledElement
4042 * Available label alignment modes include:
4043 * - 'left': Label is before the field and aligned away from it, best for when the user will be
4044 * scanning for a specific label in a form with many fields
4045 * - 'right': Label is before the field and aligned toward it, best for forms the user is very
4046 * familiar with and will tab through field checking quickly to verify which field they are in
4047 * - 'top': Label is before the field and above it, best for when the use will need to fill out all
4048 * fields from top to bottom in a form with few fields
4049 * - 'inline': Label is after the field and aligned toward it, best for small boolean fields like
4050 * checkboxes or radio buttons
4053 * @param {OO.ui.Widget} field Field widget
4054 * @param {Object} [config] Configuration options
4055 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
4057 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
4058 // Config initialization
4059 config
= $.extend( { 'align': 'left' }, config
);
4061 // Parent constructor
4062 OO
.ui
.FieldLayout
.super.call( this, config
);
4064 // Mixin constructors
4065 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
4068 this.$field
= this.$( '<div>' );
4073 if ( this.field
instanceof OO
.ui
.InputWidget
) {
4074 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
4076 this.field
.connect( this, { 'disable': 'onFieldDisable' } );
4079 this.$element
.addClass( 'oo-ui-fieldLayout' );
4081 .addClass( 'oo-ui-fieldLayout-field' )
4082 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
4083 .append( this.field
.$element
);
4084 this.setAlignment( config
.align
);
4089 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
4090 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
4095 * Handle field disable events.
4097 * @param {boolean} value Field is disabled
4099 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
4100 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
4104 * Handle label mouse click events.
4106 * @param {jQuery.Event} e Mouse click event
4108 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
4109 this.field
.simulateLabelClick();
4116 * @return {OO.ui.Widget} Field widget
4118 OO
.ui
.FieldLayout
.prototype.getField = function () {
4123 * Set the field alignment mode.
4125 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
4128 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
4129 if ( value
!== this.align
) {
4130 // Default to 'left'
4131 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
4135 if ( value
=== 'inline' ) {
4136 this.$element
.append( this.$field
, this.$label
);
4138 this.$element
.append( this.$label
, this.$field
);
4142 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
4145 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
4151 * Layout made of proportionally sized columns and rows.
4154 * @extends OO.ui.Layout
4157 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
4158 * @param {Object} [config] Configuration options
4159 * @cfg {number[]} [widths] Widths of columns as ratios
4160 * @cfg {number[]} [heights] Heights of columns as ratios
4162 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
4165 // Config initialization
4166 config
= config
|| {};
4168 // Parent constructor
4169 OO
.ui
.GridLayout
.super.call( this, config
);
4177 this.$element
.addClass( 'oo-ui-gridLayout' );
4178 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
4179 this.panels
.push( panels
[i
] );
4180 this.$element
.append( panels
[i
].$element
);
4182 if ( config
.widths
|| config
.heights
) {
4183 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
4185 // Arrange in columns by default
4187 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
4190 this.layout( widths
, [ 1 ] );
4196 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
4208 /* Static Properties */
4210 OO
.ui
.GridLayout
.static.tagName
= 'div';
4215 * Set grid dimensions.
4217 * @param {number[]} widths Widths of columns as ratios
4218 * @param {number[]} heights Heights of rows as ratios
4220 * @throws {Error} If grid is not large enough to fit all panels
4222 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
4226 cols
= widths
.length
,
4227 rows
= heights
.length
;
4229 // Verify grid is big enough to fit panels
4230 if ( cols
* rows
< this.panels
.length
) {
4231 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
4234 // Sum up denominators
4235 for ( x
= 0; x
< cols
; x
++ ) {
4238 for ( y
= 0; y
< rows
; y
++ ) {
4244 for ( x
= 0; x
< cols
; x
++ ) {
4245 this.widths
[x
] = widths
[x
] / xd
;
4247 for ( y
= 0; y
< rows
; y
++ ) {
4248 this.heights
[y
] = heights
[y
] / yd
;
4252 this.emit( 'layout' );
4256 * Update panel positions and sizes.
4260 OO
.ui
.GridLayout
.prototype.update = function () {
4268 cols
= this.widths
.length
,
4269 rows
= this.heights
.length
;
4271 for ( y
= 0; y
< rows
; y
++ ) {
4272 for ( x
= 0; x
< cols
; x
++ ) {
4273 panel
= this.panels
[i
];
4274 width
= this.widths
[x
];
4275 height
= this.heights
[y
];
4277 'width': Math
.round( width
* 100 ) + '%',
4278 'height': Math
.round( height
* 100 ) + '%',
4279 'top': Math
.round( top
* 100 ) + '%'
4282 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
4283 dimensions
.right
= Math
.round( left
* 100 ) + '%';
4285 dimensions
.left
= Math
.round( left
* 100 ) + '%';
4287 panel
.$element
.css( dimensions
);
4295 this.emit( 'update' );
4299 * Get a panel at a given position.
4301 * The x and y position is affected by the current grid layout.
4303 * @param {number} x Horizontal position
4304 * @param {number} y Vertical position
4305 * @return {OO.ui.PanelLayout} The panel at the given postion
4307 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
4308 return this.panels
[( x
* this.widths
.length
) + y
];
4311 * Layout containing a series of pages.
4314 * @extends OO.ui.Layout
4317 * @param {Object} [config] Configuration options
4318 * @cfg {boolean} [continuous=false] Show all pages, one after another
4319 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
4320 * @cfg {boolean} [outlined=false] Show an outline
4321 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
4323 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
4324 // Initialize configuration
4325 config
= config
|| {};
4327 // Parent constructor
4328 OO
.ui
.BookletLayout
.super.call( this, config
);
4331 this.currentPageName
= null;
4333 this.ignoreFocus
= false;
4334 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
4335 this.autoFocus
= config
.autoFocus
=== undefined ? true : !!config
.autoFocus
;
4336 this.outlineVisible
= false;
4337 this.outlined
= !!config
.outlined
;
4338 if ( this.outlined
) {
4339 this.editable
= !!config
.editable
;
4340 this.outlineControlsWidget
= null;
4341 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
4342 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
4343 this.gridLayout
= new OO
.ui
.GridLayout(
4344 [ this.outlinePanel
, this.stackLayout
],
4345 { '$': this.$, 'widths': [ 1, 2 ] }
4347 this.outlineVisible
= true;
4348 if ( this.editable
) {
4349 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
4350 this.outlineWidget
, { '$': this.$ }
4356 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
4357 if ( this.outlined
) {
4358 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
4360 if ( this.autoFocus
) {
4361 // Event 'focus' does not bubble, but 'focusin' does
4362 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
4366 this.$element
.addClass( 'oo-ui-bookletLayout' );
4367 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
4368 if ( this.outlined
) {
4369 this.outlinePanel
.$element
4370 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
4371 .append( this.outlineWidget
.$element
);
4372 if ( this.editable
) {
4373 this.outlinePanel
.$element
4374 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
4375 .append( this.outlineControlsWidget
.$element
);
4377 this.$element
.append( this.gridLayout
.$element
);
4379 this.$element
.append( this.stackLayout
.$element
);
4385 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
4391 * @param {OO.ui.PageLayout} page Current page
4396 * @param {OO.ui.PageLayout[]} page Added pages
4397 * @param {number} index Index pages were added at
4402 * @param {OO.ui.PageLayout[]} pages Removed pages
4408 * Handle stack layout focus.
4410 * @param {jQuery.Event} e Focusin event
4412 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
4415 // Find the page that an element was focused within
4416 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
4417 for ( name
in this.pages
) {
4418 // Check for page match, exclude current page to find only page changes
4419 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
4420 this.setPage( name
);
4427 * Handle stack layout set events.
4429 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
4431 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
4433 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
4434 if ( this.autoFocus
) {
4435 // Set focus to the first input if nothing on the page is focused yet
4436 if ( !page
.$element
.find( ':focus' ).length
) {
4437 page
.$element
.find( ':input:first' ).focus();
4445 * Handle outline widget select events.
4447 * @param {OO.ui.OptionWidget|null} item Selected item
4449 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
4451 this.setPage( item
.getData() );
4456 * Check if booklet has an outline.
4460 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
4461 return this.outlined
;
4465 * Check if booklet has editing controls.
4469 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
4470 return this.editable
;
4474 * Check if booklet has a visible outline.
4478 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
4479 return this.outlined
&& this.outlineVisible
;
4483 * Hide or show the outline.
4485 * @param {boolean} [show] Show outline, omit to invert current state
4488 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
4489 if ( this.outlined
) {
4490 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
4491 this.outlineVisible
= show
;
4492 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
4499 * Get the outline widget.
4501 * @param {OO.ui.PageLayout} page Page to be selected
4502 * @return {OO.ui.PageLayout|null} Closest page to another
4504 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
4505 var next
, prev
, level
,
4506 pages
= this.stackLayout
.getItems(),
4507 index
= $.inArray( page
, pages
);
4509 if ( index
!== -1 ) {
4510 next
= pages
[index
+ 1];
4511 prev
= pages
[index
- 1];
4512 // Prefer adjacent pages at the same level
4513 if ( this.outlined
) {
4514 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
4517 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
4523 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
4529 return prev
|| next
|| null;
4533 * Get the outline widget.
4535 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
4537 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
4538 return this.outlineWidget
;
4542 * Get the outline controls widget. If the outline is not editable, null is returned.
4544 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
4546 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
4547 return this.outlineControlsWidget
;
4551 * Get a page by name.
4553 * @param {string} name Symbolic name of page
4554 * @return {OO.ui.PageLayout|undefined} Page, if found
4556 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
4557 return this.pages
[name
];
4561 * Get the current page name.
4563 * @return {string|null} Current page name
4565 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
4566 return this.currentPageName
;
4570 * Add a page to the layout.
4572 * When pages are added with the same names as existing pages, the existing pages will be
4573 * automatically removed before the new pages are added.
4575 * @param {OO.ui.PageLayout[]} pages Pages to add
4576 * @param {number} index Index to insert pages after
4580 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
4581 var i
, len
, name
, page
, item
, currentIndex
,
4582 stackLayoutPages
= this.stackLayout
.getItems(),
4586 // Remove pages with same names
4587 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4589 name
= page
.getName();
4591 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
4592 // Correct the insertion index
4593 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
4594 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
4597 remove
.push( this.pages
[name
] );
4600 if ( remove
.length
) {
4601 this.removePages( remove
);
4605 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4607 name
= page
.getName();
4608 this.pages
[page
.getName()] = page
;
4609 if ( this.outlined
) {
4610 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { '$': this.$ } );
4611 page
.setOutlineItem( item
);
4616 if ( this.outlined
&& items
.length
) {
4617 this.outlineWidget
.addItems( items
, index
);
4618 this.updateOutlineWidget();
4620 this.stackLayout
.addItems( pages
, index
);
4621 this.emit( 'add', pages
, index
);
4627 * Remove a page from the layout.
4632 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
4633 var i
, len
, name
, page
,
4636 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4638 name
= page
.getName();
4639 delete this.pages
[name
];
4640 if ( this.outlined
) {
4641 items
.push( this.outlineWidget
.getItemFromData( name
) );
4642 page
.setOutlineItem( null );
4645 if ( this.outlined
&& items
.length
) {
4646 this.outlineWidget
.removeItems( items
);
4647 this.updateOutlineWidget();
4649 this.stackLayout
.removeItems( pages
);
4650 this.emit( 'remove', pages
);
4656 * Clear all pages from the layout.
4661 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
4663 pages
= this.stackLayout
.getItems();
4666 this.currentPageName
= null;
4667 if ( this.outlined
) {
4668 this.outlineWidget
.clearItems();
4669 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4670 pages
[i
].setOutlineItem( null );
4673 this.stackLayout
.clearItems();
4675 this.emit( 'remove', pages
);
4681 * Set the current page by name.
4684 * @param {string} name Symbolic name of page
4686 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
4688 page
= this.pages
[name
];
4690 if ( name
!== this.currentPageName
) {
4691 if ( this.outlined
) {
4692 selectedItem
= this.outlineWidget
.getSelectedItem();
4693 if ( selectedItem
&& selectedItem
.getData() !== name
) {
4694 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
4698 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
4699 this.pages
[this.currentPageName
].setActive( false );
4700 // Blur anything focused if the next page doesn't have anything focusable - this
4701 // is not needed if the next page has something focusable because once it is focused
4702 // this blur happens automatically
4703 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
4704 this.pages
[this.currentPageName
].$element
.find( ':focus' ).blur();
4707 this.currentPageName
= name
;
4708 this.stackLayout
.setItem( page
);
4709 page
.setActive( true );
4710 this.emit( 'set', page
);
4716 * Call this after adding or removing items from the OutlineWidget.
4720 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
4721 // Auto-select first item when nothing is selected anymore
4722 if ( !this.outlineWidget
.getSelectedItem() ) {
4723 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
4729 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
4732 * @extends OO.ui.Layout
4735 * @param {Object} [config] Configuration options
4736 * @cfg {boolean} [scrollable] Allow vertical scrolling
4737 * @cfg {boolean} [padded] Pad the content from the edges
4739 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
4740 // Config initialization
4741 config
= config
|| {};
4743 // Parent constructor
4744 OO
.ui
.PanelLayout
.super.call( this, config
);
4747 this.$element
.addClass( 'oo-ui-panelLayout' );
4748 if ( config
.scrollable
) {
4749 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
4752 if ( config
.padded
) {
4753 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
4759 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
4761 * Page within an booklet layout.
4764 * @extends OO.ui.PanelLayout
4767 * @param {string} name Unique symbolic name of page
4768 * @param {Object} [config] Configuration options
4769 * @param {string} [outlineItem] Outline item widget
4771 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
4772 // Configuration initialization
4773 config
= $.extend( { 'scrollable': true }, config
);
4775 // Parent constructor
4776 OO
.ui
.PageLayout
.super.call( this, config
);
4780 this.outlineItem
= config
.outlineItem
|| null;
4781 this.active
= false;
4784 this.$element
.addClass( 'oo-ui-pageLayout' );
4789 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4795 * @param {boolean} active Page is active
4803 * @return {string} Symbolic name of page
4805 OO
.ui
.PageLayout
.prototype.getName = function () {
4810 * Check if page is active.
4812 * @return {boolean} Page is active
4814 OO
.ui
.PageLayout
.prototype.isActive = function () {
4821 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
4823 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
4824 return this.outlineItem
;
4830 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
4833 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
4834 this.outlineItem
= outlineItem
;
4839 * Set page active state.
4841 * @param {boolean} Page is active
4844 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
4847 if ( active
!== this.active
) {
4848 this.active
= active
;
4849 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
4850 this.emit( 'active', this.active
);
4854 * Layout containing a series of mutually exclusive pages.
4857 * @extends OO.ui.PanelLayout
4858 * @mixins OO.ui.GroupElement
4861 * @param {Object} [config] Configuration options
4862 * @cfg {boolean} [continuous=false] Show all pages, one after another
4863 * @cfg {string} [icon=''] Symbolic icon name
4864 * @cfg {OO.ui.Layout[]} [items] Layouts to add
4866 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4867 // Config initialization
4868 config
= $.extend( { 'scrollable': true }, config
);
4870 // Parent constructor
4871 OO
.ui
.StackLayout
.super.call( this, config
);
4873 // Mixin constructors
4874 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4877 this.currentItem
= null;
4878 this.continuous
= !!config
.continuous
;
4881 this.$element
.addClass( 'oo-ui-stackLayout' );
4882 if ( this.continuous
) {
4883 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4885 if ( $.isArray( config
.items
) ) {
4886 this.addItems( config
.items
);
4892 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4893 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4899 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
4905 * Get the current item.
4907 * @return {OO.ui.Layout|null}
4909 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
4910 return this.currentItem
;
4914 * Unset the current item.
4917 * @param {OO.ui.StackLayout} layout
4920 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
4921 var prevItem
= this.currentItem
;
4922 if ( prevItem
=== null ) {
4926 this.currentItem
= null;
4927 this.emit( 'set', null );
4933 * Adding an existing item (by value) will move it.
4935 * @param {OO.ui.Layout[]} items Items to add
4936 * @param {number} [index] Index to insert items after
4939 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
4941 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
4943 if ( !this.currentItem
&& items
.length
) {
4944 this.setItem( items
[0] );
4953 * Items will be detached, not removed, so they can be used later.
4955 * @param {OO.ui.Layout[]} items Items to remove
4959 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
4961 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
4963 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
4964 if ( this.items
.length
) {
4965 this.setItem( this.items
[0] );
4967 this.unsetCurrentItem();
4977 * Items will be detached, not removed, so they can be used later.
4982 OO
.ui
.StackLayout
.prototype.clearItems = function () {
4983 this.unsetCurrentItem();
4984 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
4992 * Any currently shown item will be hidden.
4994 * FIXME: If the passed item to show has not been added in the items list, then
4995 * this method drops it and unsets the current item.
4997 * @param {OO.ui.Layout} item Item to show
5001 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
5004 if ( item
!== this.currentItem
) {
5005 if ( !this.continuous
) {
5006 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5007 this.items
[i
].$element
.css( 'display', '' );
5010 if ( $.inArray( item
, this.items
) !== -1 ) {
5011 if ( !this.continuous
) {
5012 item
.$element
.css( 'display', 'block' );
5014 this.currentItem
= item
;
5015 this.emit( 'set', item
);
5017 this.unsetCurrentItem();
5024 * Horizontal bar layout of tools as icon buttons.
5027 * @extends OO.ui.ToolGroup
5030 * @param {OO.ui.Toolbar} toolbar
5031 * @param {Object} [config] Configuration options
5033 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
5034 // Parent constructor
5035 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
5038 this.$element
.addClass( 'oo-ui-barToolGroup' );
5043 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
5045 /* Static Properties */
5047 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
5049 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
5051 OO
.ui
.BarToolGroup
.static.name
= 'bar';
5053 * Popup list of tools with an icon and optional label.
5057 * @extends OO.ui.ToolGroup
5058 * @mixins OO.ui.IconedElement
5059 * @mixins OO.ui.IndicatedElement
5060 * @mixins OO.ui.LabeledElement
5061 * @mixins OO.ui.TitledElement
5062 * @mixins OO.ui.ClippableElement
5065 * @param {OO.ui.Toolbar} toolbar
5066 * @param {Object} [config] Configuration options
5067 * @cfg {string} [header] Text to display at the top of the pop-up
5069 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
5070 // Configuration initialization
5071 config
= config
|| {};
5073 // Parent constructor
5074 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
5076 // Mixin constructors
5077 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5078 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5079 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5080 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5081 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
5084 this.active
= false;
5085 this.dragging
= false;
5086 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
5087 this.$handle
= this.$( '<span>' );
5091 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
5092 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
5097 .addClass( 'oo-ui-popupToolGroup-handle' )
5098 .append( this.$icon
, this.$label
, this.$indicator
);
5099 // If the pop-up should have a header, add it to the top of the toolGroup.
5100 // Note: If this feature is useful for other widgets, we could abstract it into an
5101 // OO.ui.HeaderedElement mixin constructor.
5102 if ( config
.header
!== undefined ) {
5104 .prepend( this.$( '<span>' )
5105 .addClass( 'oo-ui-popupToolGroup-header' )
5106 .text( config
.header
)
5110 .addClass( 'oo-ui-popupToolGroup' )
5111 .prepend( this.$handle
);
5116 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
5117 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
5118 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
5119 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
5120 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
5121 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
5123 /* Static Properties */
5130 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
5132 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
5134 if ( this.isDisabled() && this.isElementAttached() ) {
5135 this.setActive( false );
5140 * Handle focus being lost.
5142 * The event is actually generated from a mouseup, so it is not a normal blur event object.
5144 * @param {jQuery.Event} e Mouse up event
5146 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
5147 // Only deactivate when clicking outside the dropdown element
5148 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
5149 this.setActive( false );
5156 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
5157 if ( !this.isDisabled() && e
.which
=== 1 ) {
5158 this.setActive( false );
5160 return OO
.ui
.PopupToolGroup
.super.prototype.onMouseUp
.call( this, e
);
5164 * Handle mouse up events.
5166 * @param {jQuery.Event} e Mouse up event
5168 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
5173 * Handle mouse down events.
5175 * @param {jQuery.Event} e Mouse down event
5177 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
5178 if ( !this.isDisabled() && e
.which
=== 1 ) {
5179 this.setActive( !this.active
);
5185 * Switch into active mode.
5187 * When active, mouseup events anywhere in the document will trigger deactivation.
5189 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
5191 if ( this.active
!== value
) {
5192 this.active
= value
;
5194 this.setClipping( true );
5195 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
5196 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
5198 this.setClipping( false );
5199 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
5200 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
5205 * Drop down list layout of tools as labeled icon buttons.
5208 * @extends OO.ui.PopupToolGroup
5211 * @param {OO.ui.Toolbar} toolbar
5212 * @param {Object} [config] Configuration options
5214 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
5215 // Parent constructor
5216 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
5219 this.$element
.addClass( 'oo-ui-listToolGroup' );
5224 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
5226 /* Static Properties */
5228 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
5230 OO
.ui
.ListToolGroup
.static.name
= 'list';
5232 * Drop down menu layout of tools as selectable menu items.
5235 * @extends OO.ui.PopupToolGroup
5238 * @param {OO.ui.Toolbar} toolbar
5239 * @param {Object} [config] Configuration options
5241 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
5242 // Configuration initialization
5243 config
= config
|| {};
5245 // Parent constructor
5246 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
5249 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
5252 this.$element
.addClass( 'oo-ui-menuToolGroup' );
5257 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
5259 /* Static Properties */
5261 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
5263 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
5268 * Handle the toolbar state being updated.
5270 * When the state changes, the title of each active item in the menu will be joined together and
5271 * used as a label for the group. The label will be empty if none of the items are active.
5273 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
5277 for ( name
in this.tools
) {
5278 if ( this.tools
[name
].isActive() ) {
5279 labelTexts
.push( this.tools
[name
].getTitle() );
5283 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
5286 * Tool that shows a popup when selected.
5290 * @extends OO.ui.Tool
5291 * @mixins OO.ui.PopuppableElement
5294 * @param {OO.ui.Toolbar} toolbar
5295 * @param {Object} [config] Configuration options
5297 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
5298 // Parent constructor
5299 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
5301 // Mixin constructors
5302 OO
.ui
.PopuppableElement
.call( this, config
);
5306 .addClass( 'oo-ui-popupTool' )
5307 .append( this.popup
.$element
);
5312 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
5313 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
5318 * Handle the tool being selected.
5322 OO
.ui
.PopupTool
.prototype.onSelect = function () {
5323 if ( !this.isDisabled() ) {
5324 if ( this.popup
.isVisible() ) {
5330 this.setActive( false );
5335 * Handle the toolbar state being updated.
5339 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
5340 this.setActive( false );
5345 * Mixin for OO.ui.Widget subclasses.
5347 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
5351 * @extends OO.ui.GroupElement
5354 * @param {jQuery} $group Container node, assigned to #$group
5355 * @param {Object} [config] Configuration options
5357 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
5358 // Parent constructor
5359 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
5364 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
5369 * Set the disabled state of the widget.
5371 * This will also update the disabled state of child widgets.
5373 * @param {boolean} disabled Disable widget
5376 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
5380 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
5381 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
5383 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
5385 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5386 this.items
[i
].updateDisabled();
5395 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
5402 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
5409 * Check if widget is disabled.
5411 * Checks parent if present, making disabled state inheritable.
5413 * @return {boolean} Widget is disabled
5415 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
5416 return this.disabled
||
5417 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
5421 * Set group element is in.
5423 * @param {OO.ui.GroupElement|null} group Group element, null if none
5426 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
5428 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
5429 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
5431 // Initialize item disabled states
5432 this.updateDisabled();
5440 * @extends OO.ui.Widget
5441 * @mixins OO.ui.IconedElement
5442 * @mixins OO.ui.TitledElement
5445 * @param {Object} [config] Configuration options
5447 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
5448 // Config intialization
5449 config
= config
|| {};
5451 // Parent constructor
5452 OO
.ui
.IconWidget
.super.call( this, config
);
5454 // Mixin constructors
5455 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
5456 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5459 this.$element
.addClass( 'oo-ui-iconWidget' );
5464 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
5465 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
5466 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
5468 /* Static Properties */
5470 OO
.ui
.IconWidget
.static.tagName
= 'span';
5475 * @extends OO.ui.Widget
5476 * @mixins OO.ui.IndicatedElement
5477 * @mixins OO.ui.TitledElement
5480 * @param {Object} [config] Configuration options
5482 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
5483 // Config intialization
5484 config
= config
|| {};
5486 // Parent constructor
5487 OO
.ui
.IndicatorWidget
.super.call( this, config
);
5489 // Mixin constructors
5490 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
5491 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5494 this.$element
.addClass( 'oo-ui-indicatorWidget' );
5499 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
5500 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
5501 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
5503 /* Static Properties */
5505 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
5507 * Container for multiple related buttons.
5509 * Use together with OO.ui.ButtonWidget.
5512 * @extends OO.ui.Widget
5513 * @mixins OO.ui.GroupElement
5516 * @param {Object} [config] Configuration options
5517 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
5519 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
5520 // Parent constructor
5521 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
5523 // Mixin constructors
5524 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
5527 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
5528 if ( $.isArray( config
.items
) ) {
5529 this.addItems( config
.items
);
5535 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
5536 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
5541 * @extends OO.ui.Widget
5542 * @mixins OO.ui.ButtonedElement
5543 * @mixins OO.ui.IconedElement
5544 * @mixins OO.ui.IndicatedElement
5545 * @mixins OO.ui.LabeledElement
5546 * @mixins OO.ui.TitledElement
5547 * @mixins OO.ui.FlaggableElement
5550 * @param {Object} [config] Configuration options
5551 * @cfg {string} [title=''] Title text
5552 * @cfg {string} [href] Hyperlink to visit when clicked
5553 * @cfg {string} [target] Target to open hyperlink in
5555 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
5556 // Configuration initialization
5557 config
= $.extend( { 'target': '_blank' }, config
);
5559 // Parent constructor
5560 OO
.ui
.ButtonWidget
.super.call( this, config
);
5562 // Mixin constructors
5563 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
5564 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5565 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5566 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5567 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
5568 OO
.ui
.FlaggableElement
.call( this, config
);
5571 this.isHyperlink
= typeof config
.href
=== 'string';
5575 'click': OO
.ui
.bind( this.onClick
, this ),
5576 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
5581 .append( this.$icon
, this.$label
, this.$indicator
)
5582 .attr( { 'href': config
.href
, 'target': config
.target
} );
5584 .addClass( 'oo-ui-buttonWidget' )
5585 .append( this.$button
);
5590 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
5591 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
5592 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
5593 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
5594 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
5595 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
5596 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
5607 * Handles mouse click events.
5609 * @param {jQuery.Event} e Mouse click event
5612 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
5613 if ( !this.isDisabled() ) {
5614 this.emit( 'click' );
5615 if ( this.isHyperlink
) {
5623 * Handles keypress events.
5625 * @param {jQuery.Event} e Keypress event
5628 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
5629 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
5631 if ( this.isHyperlink
) {
5642 * @extends OO.ui.Widget
5645 * @param {Object} [config] Configuration options
5646 * @cfg {string} [name=''] HTML input name
5647 * @cfg {string} [value=''] Input value
5648 * @cfg {boolean} [readOnly=false] Prevent changes
5649 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
5651 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
5652 // Config intialization
5653 config
= $.extend( { 'readOnly': false }, config
);
5655 // Parent constructor
5656 OO
.ui
.InputWidget
.super.call( this, config
);
5659 this.$input
= this.getInputElement( config
);
5661 this.readOnly
= false;
5662 this.inputFilter
= config
.inputFilter
;
5665 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
5669 .attr( 'name', config
.name
)
5670 .prop( 'disabled', this.isDisabled() );
5671 this.setReadOnly( config
.readOnly
);
5672 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
5673 this.setValue( config
.value
);
5678 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
5690 * Get input element.
5692 * @param {Object} [config] Configuration options
5693 * @return {jQuery} Input element
5695 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
5696 return this.$( '<input>' );
5700 * Handle potentially value-changing events.
5702 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
5704 OO
.ui
.InputWidget
.prototype.onEdit = function () {
5705 if ( !this.isDisabled() ) {
5706 // Allow the stack to clear so the value will be updated
5707 setTimeout( OO
.ui
.bind( function () {
5708 this.setValue( this.$input
.val() );
5714 * Get the value of the input.
5716 * @return {string} Input value
5718 OO
.ui
.InputWidget
.prototype.getValue = function () {
5723 * Sets the direction of the current input, either RTL or LTR
5725 * @param {boolean} isRTL
5727 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
5729 this.$input
.removeClass( 'oo-ui-ltr' );
5730 this.$input
.addClass( 'oo-ui-rtl' );
5732 this.$input
.removeClass( 'oo-ui-rtl' );
5733 this.$input
.addClass( 'oo-ui-ltr' );
5738 * Set the value of the input.
5740 * @param {string} value New value
5744 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
5745 value
= this.sanitizeValue( value
);
5746 if ( this.value
!== value
) {
5748 this.emit( 'change', this.value
);
5750 // Update the DOM if it has changed. Note that with sanitizeValue, it
5751 // is possible for the DOM value to change without this.value changing.
5752 if ( this.$input
.val() !== this.value
) {
5753 this.$input
.val( this.value
);
5759 * Sanitize incoming value.
5761 * Ensures value is a string, and converts undefined and null to empty strings.
5763 * @param {string} value Original value
5764 * @return {string} Sanitized value
5766 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
5767 if ( value
=== undefined || value
=== null ) {
5769 } else if ( this.inputFilter
) {
5770 return this.inputFilter( String( value
) );
5772 return String( value
);
5777 * Simulate the behavior of clicking on a label bound to this input.
5779 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
5780 if ( !this.isDisabled() ) {
5781 if ( this.$input
.is( ':checkbox,:radio' ) ) {
5782 this.$input
.click();
5783 } else if ( this.$input
.is( ':input' ) ) {
5784 this.$input
.focus();
5790 * Check if the widget is read-only.
5794 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
5795 return this.readOnly
;
5799 * Set the read-only state of the widget.
5801 * This should probably change the widgets's appearance and prevent it from being used.
5803 * @param {boolean} state Make input read-only
5806 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
5807 this.readOnly
= !!state
;
5808 this.$input
.prop( 'readOnly', this.readOnly
);
5815 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
5816 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
5817 if ( this.$input
) {
5818 this.$input
.prop( 'disabled', this.isDisabled() );
5828 OO
.ui
.InputWidget
.prototype.focus = function () {
5829 this.$input
.focus();
5836 * @extends OO.ui.InputWidget
5839 * @param {Object} [config] Configuration options
5841 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
5842 // Parent constructor
5843 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
5846 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
5851 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
5858 * Get input element.
5860 * @return {jQuery} Input element
5862 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
5863 return this.$( '<input type="checkbox" />' );
5867 * Get checked state of the checkbox
5869 * @return {boolean} If the checkbox is checked
5871 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
5878 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
5880 if ( this.value
!== value
) {
5882 this.$input
.prop( 'checked', this.value
);
5883 this.emit( 'change', this.value
);
5890 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
5891 if ( !this.isDisabled() ) {
5892 // Allow the stack to clear so the value will be updated
5893 setTimeout( OO
.ui
.bind( function () {
5894 this.setValue( this.$input
.prop( 'checked' ) );
5902 * @extends OO.ui.Widget
5903 * @mixins OO.ui.LabeledElement
5906 * @param {Object} [config] Configuration options
5908 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
5909 // Config intialization
5910 config
= config
|| {};
5912 // Parent constructor
5913 OO
.ui
.LabelWidget
.super.call( this, config
);
5915 // Mixin constructors
5916 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
5919 this.input
= config
.input
;
5922 if ( this.input
instanceof OO
.ui
.InputWidget
) {
5923 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
5927 this.$element
.addClass( 'oo-ui-labelWidget' );
5932 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
5933 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
5935 /* Static Properties */
5937 OO
.ui
.LabelWidget
.static.tagName
= 'label';
5942 * Handles label mouse click events.
5944 * @param {jQuery.Event} e Mouse click event
5946 OO
.ui
.LabelWidget
.prototype.onClick = function () {
5947 this.input
.simulateLabelClick();
5951 * Lookup input widget.
5953 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
5954 * and `choose` events on #lookupMenu to make use of selections.
5960 * @param {OO.ui.TextInputWidget} input Input widget
5961 * @param {Object} [config] Configuration options
5962 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
5964 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
5965 // Config intialization
5966 config
= config
|| {};
5969 this.lookupInput
= input
;
5970 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
5971 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
5972 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
5973 'input': this.lookupInput
,
5974 '$container': config
.$container
5976 this.lookupCache
= {};
5977 this.lookupQuery
= null;
5978 this.lookupRequest
= null;
5979 this.populating
= false;
5982 this.$overlay
.append( this.lookupMenu
.$element
);
5984 this.lookupInput
.$input
.on( {
5985 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
5986 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
5987 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
5989 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
5992 this.$element
.addClass( 'oo-ui-lookupWidget' );
5993 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
5999 * Handle input focus event.
6001 * @param {jQuery.Event} e Input focus event
6003 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
6004 this.openLookupMenu();
6008 * Handle input blur event.
6010 * @param {jQuery.Event} e Input blur event
6012 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
6013 this.lookupMenu
.hide();
6017 * Handle input mouse down event.
6019 * @param {jQuery.Event} e Input mouse down event
6021 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
6022 this.openLookupMenu();
6026 * Handle input change event.
6028 * @param {string} value New input value
6030 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
6031 this.openLookupMenu();
6037 * @return {OO.ui.TextInputMenuWidget}
6039 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
6040 return this.lookupMenu
;
6048 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
6049 var value
= this.lookupInput
.getValue();
6051 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
6052 this.populateLookupMenu();
6053 if ( !this.lookupMenu
.isVisible() ) {
6054 this.lookupMenu
.show();
6057 this.lookupMenu
.clearItems();
6058 this.lookupMenu
.hide();
6065 * Populate lookup menu with current information.
6069 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
6070 if ( !this.populating
) {
6071 this.populating
= true;
6072 this.getLookupMenuItems()
6073 .done( OO
.ui
.bind( function ( items
) {
6074 this.lookupMenu
.clearItems();
6075 if ( items
.length
) {
6076 this.lookupMenu
.show();
6077 this.lookupMenu
.addItems( items
);
6078 this.initializeLookupMenuSelection();
6079 this.openLookupMenu();
6081 this.lookupMenu
.hide();
6083 this.populating
= false;
6085 .fail( OO
.ui
.bind( function () {
6086 this.lookupMenu
.clearItems();
6087 this.populating
= false;
6095 * Set selection in the lookup menu with current information.
6099 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
6100 if ( !this.lookupMenu
.getSelectedItem() ) {
6101 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
6103 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
6107 * Get lookup menu items for the current query.
6109 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
6112 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
6113 var value
= this.lookupInput
.getValue(),
6114 deferred
= $.Deferred();
6116 if ( value
&& value
!== this.lookupQuery
) {
6117 // Abort current request if query has changed
6118 if ( this.lookupRequest
) {
6119 this.lookupRequest
.abort();
6120 this.lookupQuery
= null;
6121 this.lookupRequest
= null;
6123 if ( value
in this.lookupCache
) {
6124 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6126 this.lookupQuery
= value
;
6127 this.lookupRequest
= this.getLookupRequest()
6128 .always( OO
.ui
.bind( function () {
6129 this.lookupQuery
= null;
6130 this.lookupRequest
= null;
6132 .done( OO
.ui
.bind( function ( data
) {
6133 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
6134 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6136 .fail( function () {
6140 this.lookupRequest
.always( OO
.ui
.bind( function () {
6145 return deferred
.promise();
6149 * Get a new request object of the current lookup query value.
6152 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
6154 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
6155 // Stub, implemented in subclass
6160 * Handle successful lookup request.
6162 * Overriding methods should call #populateLookupMenu when results are available and cache results
6163 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
6166 * @param {Mixed} data Response from server
6168 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
6169 // Stub, implemented in subclass
6173 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
6176 * @param {Mixed} data Cached result data, usually an array
6177 * @return {OO.ui.MenuItemWidget[]} Menu items
6179 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
6180 // Stub, implemented in subclass
6186 * Use with OO.ui.SelectWidget.
6189 * @extends OO.ui.Widget
6190 * @mixins OO.ui.IconedElement
6191 * @mixins OO.ui.LabeledElement
6192 * @mixins OO.ui.IndicatedElement
6193 * @mixins OO.ui.FlaggableElement
6196 * @param {Mixed} data Option data
6197 * @param {Object} [config] Configuration options
6198 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
6200 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
6201 // Config intialization
6202 config
= config
|| {};
6204 // Parent constructor
6205 OO
.ui
.OptionWidget
.super.call( this, config
);
6207 // Mixin constructors
6208 OO
.ui
.ItemWidget
.call( this );
6209 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6210 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6211 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6212 OO
.ui
.FlaggableElement
.call( this, config
);
6216 this.selected
= false;
6217 this.highlighted
= false;
6218 this.pressed
= false;
6222 .data( 'oo-ui-optionWidget', this )
6223 .attr( 'rel', config
.rel
)
6224 .addClass( 'oo-ui-optionWidget' )
6225 .append( this.$label
);
6227 .prepend( this.$icon
)
6228 .append( this.$indicator
);
6233 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
6234 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
6235 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
6236 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
6237 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
6238 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
6240 /* Static Properties */
6242 OO
.ui
.OptionWidget
.static.tagName
= 'li';
6244 OO
.ui
.OptionWidget
.static.selectable
= true;
6246 OO
.ui
.OptionWidget
.static.highlightable
= true;
6248 OO
.ui
.OptionWidget
.static.pressable
= true;
6250 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
6255 * Check if option can be selected.
6257 * @return {boolean} Item is selectable
6259 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
6260 return this.constructor.static.selectable
&& !this.isDisabled();
6264 * Check if option can be highlighted.
6266 * @return {boolean} Item is highlightable
6268 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
6269 return this.constructor.static.highlightable
&& !this.isDisabled();
6273 * Check if option can be pressed.
6275 * @return {boolean} Item is pressable
6277 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
6278 return this.constructor.static.pressable
&& !this.isDisabled();
6282 * Check if option is selected.
6284 * @return {boolean} Item is selected
6286 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
6287 return this.selected
;
6291 * Check if option is highlighted.
6293 * @return {boolean} Item is highlighted
6295 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
6296 return this.highlighted
;
6300 * Check if option is pressed.
6302 * @return {boolean} Item is pressed
6304 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
6305 return this.pressed
;
6309 * Set selected state.
6311 * @param {boolean} [state=false] Select option
6314 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
6315 if ( this.constructor.static.selectable
) {
6316 this.selected
= !!state
;
6317 if ( this.selected
) {
6318 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
6319 if ( this.constructor.static.scrollIntoViewOnSelect
) {
6320 this.scrollElementIntoView();
6323 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
6330 * Set highlighted state.
6332 * @param {boolean} [state=false] Highlight option
6335 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
6336 if ( this.constructor.static.highlightable
) {
6337 this.highlighted
= !!state
;
6338 if ( this.highlighted
) {
6339 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
6341 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
6348 * Set pressed state.
6350 * @param {boolean} [state=false] Press option
6353 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
6354 if ( this.constructor.static.pressable
) {
6355 this.pressed
= !!state
;
6356 if ( this.pressed
) {
6357 this.$element
.addClass( 'oo-ui-optionWidget-pressed' );
6359 this.$element
.removeClass( 'oo-ui-optionWidget-pressed' );
6366 * Make the option's highlight flash.
6368 * While flashing, the visual style of the pressed state is removed if present.
6370 * @return {jQuery.Promise} Promise resolved when flashing is done
6372 OO
.ui
.OptionWidget
.prototype.flash = function () {
6373 var $this = this.$element
,
6374 deferred
= $.Deferred();
6376 if ( !this.isDisabled() && this.constructor.static.pressable
) {
6377 $this.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
6378 setTimeout( OO
.ui
.bind( function () {
6379 // Restore original classes
6381 .toggleClass( 'oo-ui-optionWidget-highlighted', this.highlighted
)
6382 .toggleClass( 'oo-ui-optionWidget-pressed', this.pressed
);
6383 setTimeout( function () {
6389 return deferred
.promise();
6395 * @return {Mixed} Option data
6397 OO
.ui
.OptionWidget
.prototype.getData = function () {
6401 * Selection of options.
6403 * Use together with OO.ui.OptionWidget.
6406 * @extends OO.ui.Widget
6407 * @mixins OO.ui.GroupElement
6410 * @param {Object} [config] Configuration options
6411 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
6413 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
6414 // Config intialization
6415 config
= config
|| {};
6417 // Parent constructor
6418 OO
.ui
.SelectWidget
.super.call( this, config
);
6420 // Mixin constructors
6421 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
6424 this.pressed
= false;
6425 this.selecting
= null;
6427 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
6428 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
6432 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
6433 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
6434 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
6438 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
6439 if ( $.isArray( config
.items
) ) {
6440 this.addItems( config
.items
);
6446 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
6448 // Need to mixin base class as well
6449 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
6450 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
6456 * @param {OO.ui.OptionWidget|null} item Highlighted item
6461 * @param {OO.ui.OptionWidget|null} item Pressed item
6466 * @param {OO.ui.OptionWidget|null} item Selected item
6471 * @param {OO.ui.OptionWidget|null} item Chosen item
6476 * @param {OO.ui.OptionWidget[]} items Added items
6477 * @param {number} index Index items were added at
6482 * @param {OO.ui.OptionWidget[]} items Removed items
6485 /* Static Properties */
6487 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
6492 * Handle mouse down events.
6495 * @param {jQuery.Event} e Mouse down event
6497 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
6500 if ( !this.isDisabled() && e
.which
=== 1 ) {
6501 this.togglePressed( true );
6502 item
= this.getTargetItem( e
);
6503 if ( item
&& item
.isSelectable() ) {
6504 this.pressItem( item
);
6505 this.selecting
= item
;
6506 this.getElementDocument().addEventListener(
6507 'mouseup', this.onMouseUpHandler
, true
6509 this.getElementDocument().addEventListener(
6510 'mousemove', this.onMouseMoveHandler
, true
6518 * Handle mouse up events.
6521 * @param {jQuery.Event} e Mouse up event
6523 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
6526 this.togglePressed( false );
6527 if ( !this.selecting
) {
6528 item
= this.getTargetItem( e
);
6529 if ( item
&& item
.isSelectable() ) {
6530 this.selecting
= item
;
6533 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
6534 this.pressItem( null );
6535 this.chooseItem( this.selecting
);
6536 this.selecting
= null;
6539 this.getElementDocument().removeEventListener(
6540 'mouseup', this.onMouseUpHandler
, true
6542 this.getElementDocument().removeEventListener(
6543 'mousemove', this.onMouseMoveHandler
, true
6550 * Handle mouse move events.
6553 * @param {jQuery.Event} e Mouse move event
6555 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
6558 if ( !this.isDisabled() && this.pressed
) {
6559 item
= this.getTargetItem( e
);
6560 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
6561 this.pressItem( item
);
6562 this.selecting
= item
;
6569 * Handle mouse over events.
6572 * @param {jQuery.Event} e Mouse over event
6574 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
6577 if ( !this.isDisabled() ) {
6578 item
= this.getTargetItem( e
);
6579 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
6585 * Handle mouse leave events.
6588 * @param {jQuery.Event} e Mouse over event
6590 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
6591 if ( !this.isDisabled() ) {
6592 this.highlightItem( null );
6598 * Get the closest item to a jQuery.Event.
6601 * @param {jQuery.Event} e
6602 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
6604 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
6605 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
6606 if ( $item
.length
) {
6607 return $item
.data( 'oo-ui-optionWidget' );
6613 * Get selected item.
6615 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
6617 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
6620 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6621 if ( this.items
[i
].isSelected() ) {
6622 return this.items
[i
];
6629 * Get highlighted item.
6631 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
6633 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
6636 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6637 if ( this.items
[i
].isHighlighted() ) {
6638 return this.items
[i
];
6645 * Get an existing item with equivilant data.
6647 * @param {Object} data Item data to search for
6648 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
6650 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
6651 var hash
= OO
.getHash( data
);
6653 if ( hash
in this.hashes
) {
6654 return this.hashes
[hash
];
6661 * Toggle pressed state.
6663 * @param {boolean} pressed An option is being pressed
6665 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
6666 if ( pressed
=== undefined ) {
6667 pressed
= !this.pressed
;
6669 if ( pressed
!== this.pressed
) {
6670 this.$element
.toggleClass( 'oo-ui-selectWidget-pressed', pressed
);
6671 this.$element
.toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
6672 this.pressed
= pressed
;
6677 * Highlight an item.
6679 * Highlighting is mutually exclusive.
6681 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
6685 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
6686 var i
, len
, highlighted
,
6689 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6690 highlighted
= this.items
[i
] === item
;
6691 if ( this.items
[i
].isHighlighted() !== highlighted
) {
6692 this.items
[i
].setHighlighted( highlighted
);
6697 this.emit( 'highlight', item
);
6706 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6710 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
6711 var i
, len
, selected
,
6714 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6715 selected
= this.items
[i
] === item
;
6716 if ( this.items
[i
].isSelected() !== selected
) {
6717 this.items
[i
].setSelected( selected
);
6722 this.emit( 'select', item
);
6731 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
6735 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
6736 var i
, len
, pressed
,
6739 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6740 pressed
= this.items
[i
] === item
;
6741 if ( this.items
[i
].isPressed() !== pressed
) {
6742 this.items
[i
].setPressed( pressed
);
6747 this.emit( 'press', item
);
6756 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
6757 * an item is selected using the keyboard or mouse.
6759 * @param {OO.ui.OptionWidget} item Item to choose
6763 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
6764 this.selectItem( item
);
6765 this.emit( 'choose', item
);
6771 * Get an item relative to another one.
6773 * @param {OO.ui.OptionWidget} item Item to start at
6774 * @param {number} direction Direction to move in
6775 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
6777 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
6778 var inc
= direction
> 0 ? 1 : -1,
6779 len
= this.items
.length
,
6780 index
= item
instanceof OO
.ui
.OptionWidget
?
6781 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
6782 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
6784 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
6785 Math
.max( index
, -1 ) :
6786 // Default to n-1 instead of -1, if nothing is selected let's start at the end
6787 Math
.min( index
, len
);
6790 i
= ( i
+ inc
+ len
) % len
;
6791 item
= this.items
[i
];
6792 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6795 // Stop iterating when we've looped all the way around
6796 if ( i
=== stopAt
) {
6804 * Get the next selectable item.
6806 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
6808 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
6811 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6812 item
= this.items
[i
];
6813 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6824 * When items are added with the same values as existing items, the existing items will be
6825 * automatically removed before the new items are added.
6827 * @param {OO.ui.OptionWidget[]} items Items to add
6828 * @param {number} [index] Index to insert items after
6832 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
6833 var i
, len
, item
, hash
,
6836 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6838 hash
= OO
.getHash( item
.getData() );
6839 if ( hash
in this.hashes
) {
6840 // Remove item with same value
6841 remove
.push( this.hashes
[hash
] );
6843 this.hashes
[hash
] = item
;
6845 if ( remove
.length
) {
6846 this.removeItems( remove
);
6850 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
6852 // Always provide an index, even if it was omitted
6853 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
6861 * Items will be detached, not removed, so they can be used later.
6863 * @param {OO.ui.OptionWidget[]} items Items to remove
6867 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
6868 var i
, len
, item
, hash
;
6870 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6872 hash
= OO
.getHash( item
.getData() );
6873 if ( hash
in this.hashes
) {
6874 // Remove existing item
6875 delete this.hashes
[hash
];
6877 if ( item
.isSelected() ) {
6878 this.selectItem( null );
6883 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
6885 this.emit( 'remove', items
);
6893 * Items will be detached, not removed, so they can be used later.
6898 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
6899 var items
= this.items
.slice();
6904 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
6905 this.selectItem( null );
6907 this.emit( 'remove', items
);
6914 * Use with OO.ui.MenuWidget.
6917 * @extends OO.ui.OptionWidget
6920 * @param {Mixed} data Item data
6921 * @param {Object} [config] Configuration options
6923 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
6924 // Configuration initialization
6925 config
= $.extend( { 'icon': 'check' }, config
);
6927 // Parent constructor
6928 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
6931 this.$element
.addClass( 'oo-ui-menuItemWidget' );
6936 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
6940 * Use together with OO.ui.MenuItemWidget.
6943 * @extends OO.ui.SelectWidget
6944 * @mixins OO.ui.ClippableElement
6947 * @param {Object} [config] Configuration options
6948 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
6949 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
6951 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
6952 // Config intialization
6953 config
= config
|| {};
6955 // Parent constructor
6956 OO
.ui
.MenuWidget
.super.call( this, config
);
6958 // Mixin constructors
6959 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
6962 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
6963 this.newItems
= null;
6964 this.$input
= config
.input
? config
.input
.$input
: null;
6965 this.$previousFocus
= null;
6966 this.isolated
= !config
.input
;
6967 this.visible
= false;
6968 this.flashing
= false;
6969 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
6970 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
6973 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
6978 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
6979 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
6984 * Handles document mouse down events.
6986 * @param {jQuery.Event} e Key down event
6988 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
6989 if ( !$.contains( this.$element
[0], e
.target
) ) {
6995 * Handles key down events.
6997 * @param {jQuery.Event} e Key down event
6999 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
7002 highlightItem
= this.getHighlightedItem();
7004 if ( !this.isDisabled() && this.visible
) {
7005 if ( !highlightItem
) {
7006 highlightItem
= this.getSelectedItem();
7008 switch ( e
.keyCode
) {
7009 case OO
.ui
.Keys
.ENTER
:
7010 this.chooseItem( highlightItem
);
7014 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
7017 case OO
.ui
.Keys
.DOWN
:
7018 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
7021 case OO
.ui
.Keys
.ESCAPE
:
7022 if ( highlightItem
) {
7023 highlightItem
.setHighlighted( false );
7031 this.highlightItem( nextItem
);
7032 nextItem
.scrollElementIntoView();
7037 e
.stopPropagation();
7044 * Check if the menu is visible.
7046 * @return {boolean} Menu is visible
7048 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
7049 return this.visible
;
7053 * Bind key down listener.
7055 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
7056 if ( this.$input
) {
7057 this.$input
.on( 'keydown', this.onKeyDownHandler
);
7059 // Capture menu navigation keys
7060 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
7065 * Unbind key down listener.
7067 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
7068 if ( this.$input
) {
7069 this.$input
.off( 'keydown' );
7071 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
7078 * This will close the menu when done, unlike selectItem which only changes selection.
7080 * @param {OO.ui.OptionWidget} item Item to choose
7083 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
7085 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
7087 if ( item
&& !this.flashing
) {
7088 this.flashing
= true;
7089 item
.flash().done( OO
.ui
.bind( function () {
7091 this.flashing
= false;
7103 * Adding an existing item (by value) will move it.
7105 * @param {OO.ui.MenuItemWidget[]} items Items to add
7106 * @param {number} [index] Index to insert items after
7109 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
7113 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
7116 if ( !this.newItems
) {
7120 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
7122 if ( this.visible
) {
7123 // Defer fitting label until
7126 this.newItems
.push( item
);
7138 OO
.ui
.MenuWidget
.prototype.show = function () {
7141 if ( this.items
.length
) {
7142 this.$element
.show();
7143 this.visible
= true;
7144 this.bindKeyDownListener();
7146 // Change focus to enable keyboard navigation
7147 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
7148 this.$previousFocus
= this.$( ':focus' );
7149 this.$input
.focus();
7151 if ( this.newItems
&& this.newItems
.length
) {
7152 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
7153 this.newItems
[i
].fitLabel();
7155 this.newItems
= null;
7158 this.setClipping( true );
7161 if ( this.autoHide
) {
7162 this.getElementDocument().addEventListener(
7163 'mousedown', this.onDocumentMouseDownHandler
, true
7176 OO
.ui
.MenuWidget
.prototype.hide = function () {
7177 this.$element
.hide();
7178 this.visible
= false;
7179 this.unbindKeyDownListener();
7181 if ( this.isolated
&& this.$previousFocus
) {
7182 this.$previousFocus
.focus();
7183 this.$previousFocus
= null;
7186 this.getElementDocument().removeEventListener(
7187 'mousedown', this.onDocumentMouseDownHandler
, true
7190 this.setClipping( false );
7195 * Inline menu of options.
7197 * Use with OO.ui.MenuOptionWidget.
7200 * @extends OO.ui.Widget
7201 * @mixins OO.ui.IconedElement
7202 * @mixins OO.ui.IndicatedElement
7203 * @mixins OO.ui.LabeledElement
7204 * @mixins OO.ui.TitledElement
7207 * @param {Object} [config] Configuration options
7208 * @cfg {Object} [menu] Configuration options to pass to menu widget
7210 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
7211 // Configuration initialization
7212 config
= $.extend( { 'indicator': 'down' }, config
);
7214 // Parent constructor
7215 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
7217 // Mixin constructors
7218 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
7219 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
7220 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
7221 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
7224 this.menu
= new OO
.ui
.MenuWidget( $.extend( { '$': this.$ }, config
.menu
) );
7225 this.$handle
= this.$( '<span>' );
7228 this.$element
.on( { 'click': OO
.ui
.bind( this.onClick
, this ) } );
7229 this.menu
.connect( this, { 'select': 'onMenuSelect' } );
7233 .addClass( 'oo-ui-inlineMenuWidget-handle' )
7234 .append( this.$icon
, this.$label
, this.$indicator
);
7236 .addClass( 'oo-ui-inlineMenuWidget' )
7237 .append( this.$handle
, this.menu
.$element
);
7242 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
7243 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
7244 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
7245 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
7246 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
7253 * @return {OO.ui.MenuWidget} Menu of widget
7255 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
7260 * Handles menu select events.
7262 * @param {OO.ui.MenuItemWidget} item Selected menu item
7264 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
7271 selectedLabel
= item
.getLabel();
7273 // If the label is a DOM element, clone it, because setLabel will append() it
7274 if ( selectedLabel
instanceof jQuery
) {
7275 selectedLabel
= selectedLabel
.clone();
7278 this.setLabel( selectedLabel
);
7282 * Handles mouse click events.
7284 * @param {jQuery.Event} e Mouse click event
7286 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
7287 // Skip clicks within the menu
7288 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
7292 if ( !this.isDisabled() ) {
7293 if ( this.menu
.isVisible() ) {
7302 * Menu section item widget.
7304 * Use with OO.ui.MenuWidget.
7307 * @extends OO.ui.OptionWidget
7310 * @param {Mixed} data Item data
7311 * @param {Object} [config] Configuration options
7313 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
7314 // Parent constructor
7315 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
7318 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
7323 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
7325 /* Static Properties */
7327 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
7329 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
7331 * Create an OO.ui.OutlineWidget object.
7333 * Use with OO.ui.OutlineItemWidget.
7336 * @extends OO.ui.SelectWidget
7339 * @param {Object} [config] Configuration options
7341 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
7342 // Config intialization
7343 config
= config
|| {};
7345 // Parent constructor
7346 OO
.ui
.OutlineWidget
.super.call( this, config
);
7349 this.$element
.addClass( 'oo-ui-outlineWidget' );
7354 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
7356 * Creates an OO.ui.OutlineControlsWidget object.
7358 * Use together with OO.ui.OutlineWidget.js
7363 * @param {OO.ui.OutlineWidget} outline Outline to control
7364 * @param {Object} [config] Configuration options
7366 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7367 // Configuration initialization
7368 config
= $.extend( { 'icon': 'add-item' }, config
);
7370 // Parent constructor
7371 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7373 // Mixin constructors
7374 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
7375 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
7378 this.outline
= outline
;
7379 this.$movers
= this.$( '<div>' );
7380 this.upButton
= new OO
.ui
.ButtonWidget( {
7384 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
7386 this.downButton
= new OO
.ui
.ButtonWidget( {
7390 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
7392 this.removeButton
= new OO
.ui
.ButtonWidget( {
7396 'title': OO
.ui
.msg( 'ooui-outline-control-remove' )
7400 outline
.connect( this, {
7401 'select': 'onOutlineChange',
7402 'add': 'onOutlineChange',
7403 'remove': 'onOutlineChange'
7405 this.upButton
.connect( this, { 'click': [ 'emit', 'move', -1 ] } );
7406 this.downButton
.connect( this, { 'click': [ 'emit', 'move', 1 ] } );
7407 this.removeButton
.connect( this, { 'click': [ 'emit', 'remove' ] } );
7410 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7411 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7413 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7414 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7415 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7420 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7421 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7422 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7428 * @param {number} places Number of places to move
7438 * Handle outline change events.
7440 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7441 var i
, len
, firstMovable
, lastMovable
,
7442 items
= this.outline
.getItems(),
7443 selectedItem
= this.outline
.getSelectedItem(),
7444 movable
= selectedItem
&& selectedItem
.isMovable(),
7445 removable
= selectedItem
&& selectedItem
.isRemovable();
7450 while ( ++i
< len
) {
7451 if ( items
[i
].isMovable() ) {
7452 firstMovable
= items
[i
];
7458 if ( items
[i
].isMovable() ) {
7459 lastMovable
= items
[i
];
7464 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7465 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7466 this.removeButton
.setDisabled( !removable
);
7469 * Creates an OO.ui.OutlineItemWidget object.
7471 * Use with OO.ui.OutlineWidget.
7474 * @extends OO.ui.OptionWidget
7477 * @param {Mixed} data Item data
7478 * @param {Object} [config] Configuration options
7479 * @cfg {number} [level] Indentation level
7480 * @cfg {boolean} [movable] Allow modification from outline controls
7482 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
7483 // Config intialization
7484 config
= config
|| {};
7486 // Parent constructor
7487 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
7491 this.movable
= !!config
.movable
;
7492 this.removable
= !!config
.removable
;
7495 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
7496 this.setLevel( config
.level
);
7501 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
7503 /* Static Properties */
7505 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
7507 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
7509 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
7511 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
7516 * Check if item is movable.
7518 * Movablilty is used by outline controls.
7520 * @return {boolean} Item is movable
7522 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
7523 return this.movable
;
7527 * Check if item is removable.
7529 * Removablilty is used by outline controls.
7531 * @return {boolean} Item is removable
7533 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
7534 return this.removable
;
7538 * Get indentation level.
7540 * @return {number} Indentation level
7542 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
7549 * Movablilty is used by outline controls.
7551 * @param {boolean} movable Item is movable
7554 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
7555 this.movable
= !!movable
;
7562 * Removablilty is used by outline controls.
7564 * @param {boolean} movable Item is removable
7567 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
7568 this.removable
= !!removable
;
7573 * Set indentation level.
7575 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
7578 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
7579 var levels
= this.constructor.static.levels
,
7580 levelClass
= this.constructor.static.levelClass
,
7583 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
7585 if ( this.level
=== i
) {
7586 this.$element
.addClass( levelClass
+ i
);
7588 this.$element
.removeClass( levelClass
+ i
);
7595 * Option widget that looks like a button.
7597 * Use together with OO.ui.ButtonSelectWidget.
7600 * @extends OO.ui.OptionWidget
7601 * @mixins OO.ui.ButtonedElement
7602 * @mixins OO.ui.FlaggableElement
7605 * @param {Mixed} data Option data
7606 * @param {Object} [config] Configuration options
7608 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
7609 // Parent constructor
7610 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
7612 // Mixin constructors
7613 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7614 OO
.ui
.FlaggableElement
.call( this, config
);
7617 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
7618 this.$button
.append( this.$element
.contents() );
7619 this.$element
.append( this.$button
);
7624 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
7625 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
7626 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
7628 /* Static Properties */
7630 // Allow button mouse down events to pass through so they can be handled by the parent select widget
7631 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
7638 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
7639 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
7641 if ( this.constructor.static.selectable
) {
7642 this.setActive( state
);
7648 * Select widget containing button options.
7650 * Use together with OO.ui.ButtonOptionWidget.
7653 * @extends OO.ui.SelectWidget
7656 * @param {Object} [config] Configuration options
7658 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
7659 // Parent constructor
7660 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
7663 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
7668 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
7670 * Container for content that is overlaid and positioned absolutely.
7673 * @extends OO.ui.Widget
7674 * @mixins OO.ui.LabeledElement
7677 * @param {Object} [config] Configuration options
7678 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
7679 * @cfg {string} [align='center'] Alignment of popup to origin
7680 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
7681 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
7682 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
7683 * @cfg {boolean} [head] Show label and close button at the top
7685 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
7686 // Config intialization
7687 config
= config
|| {};
7689 // Parent constructor
7690 OO
.ui
.PopupWidget
.super.call( this, config
);
7692 // Mixin constructors
7693 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
7694 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
7697 this.visible
= false;
7698 this.$popup
= this.$( '<div>' );
7699 this.$head
= this.$( '<div>' );
7700 this.$body
= this.$clippable
;
7701 this.$tail
= this.$( '<div>' );
7702 this.$container
= config
.$container
|| this.$( 'body' );
7703 this.autoClose
= !!config
.autoClose
;
7704 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
7705 this.transitionTimeout
= null;
7707 this.align
= config
.align
|| 'center';
7708 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
7709 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
7712 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
7715 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
7716 this.$body
.addClass( 'oo-ui-popupWidget-body' );
7717 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
7719 .addClass( 'oo-ui-popupWidget-head' )
7720 .append( this.$label
, this.closeButton
.$element
);
7721 if ( !config
.head
) {
7725 .addClass( 'oo-ui-popupWidget-popup' )
7726 .append( this.$head
, this.$body
);
7727 this.$element
.hide()
7728 .addClass( 'oo-ui-popupWidget' )
7729 .append( this.$popup
, this.$tail
);
7734 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
7735 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
7736 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
7751 * Handles mouse down events.
7753 * @param {jQuery.Event} e Mouse down event
7755 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
7758 !$.contains( this.$element
[0], e
.target
) &&
7759 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
7766 * Bind mouse down listener.
7768 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
7769 // Capture clicks outside popup
7770 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
7774 * Handles close button click events.
7776 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
7777 if ( this.visible
) {
7783 * Unbind mouse down listener.
7785 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
7786 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
7790 * Check if the popup is visible.
7792 * @return {boolean} Popup is visible
7794 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
7795 return this.visible
;
7799 * Set whether to show a tail.
7801 * @return {boolean} Make tail visible
7803 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
7805 if ( this.tail
!== value
) {
7808 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
7810 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
7816 * Check if showing a tail.
7818 * @return {boolean} tail is visible
7820 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
7830 OO
.ui
.PopupWidget
.prototype.show = function () {
7831 if ( !this.visible
) {
7832 this.setClipping( true );
7833 this.$element
.show();
7834 this.visible
= true;
7835 this.emit( 'show' );
7836 if ( this.autoClose
) {
7837 this.bindMouseDownListener();
7849 OO
.ui
.PopupWidget
.prototype.hide = function () {
7850 if ( this.visible
) {
7851 this.setClipping( false );
7852 this.$element
.hide();
7853 this.visible
= false;
7854 this.emit( 'hide' );
7855 if ( this.autoClose
) {
7856 this.unbindMouseDownListener();
7863 * Updates the position and size.
7865 * @param {number} width Width
7866 * @param {number} height Height
7867 * @param {boolean} [transition=false] Use a smooth transition
7870 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
7872 originOffset
= Math
.round( this.$element
.offset().left
),
7873 containerLeft
= Math
.round( this.$container
.offset().left
),
7874 containerWidth
= this.$container
.innerWidth(),
7875 containerRight
= containerLeft
+ containerWidth
,
7876 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
7877 popupLeft
= popupOffset
- padding
,
7878 popupRight
= popupOffset
+ padding
+ width
+ padding
,
7879 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
7880 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
7882 // Prevent transition from being interrupted
7883 clearTimeout( this.transitionTimeout
);
7885 // Enable transition
7886 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
7889 if ( overlapRight
< 0 ) {
7890 popupOffset
+= overlapRight
;
7891 } else if ( overlapLeft
< 0 ) {
7892 popupOffset
-= overlapLeft
;
7895 // Position body relative to anchor and resize
7897 'left': popupOffset
,
7899 'height': height
=== undefined ? 'auto' : height
7903 // Prevent transitioning after transition is complete
7904 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
7905 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7908 // Prevent transitioning immediately
7909 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7915 * Button that shows and hides a popup.
7918 * @extends OO.ui.ButtonWidget
7919 * @mixins OO.ui.PopuppableElement
7922 * @param {Object} [config] Configuration options
7924 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7925 // Parent constructor
7926 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7928 // Mixin constructors
7929 OO
.ui
.PopuppableElement
.call( this, config
);
7933 .addClass( 'oo-ui-popupButtonWidget' )
7934 .append( this.popup
.$element
);
7939 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
7940 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
7945 * Handles mouse click events.
7947 * @param {jQuery.Event} e Mouse click event
7949 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
7950 // Skip clicks within the popup
7951 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
7955 if ( !this.isDisabled() ) {
7956 if ( this.popup
.isVisible() ) {
7961 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
7968 * Combines query and results selection widgets.
7971 * @extends OO.ui.Widget
7974 * @param {Object} [config] Configuration options
7975 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
7976 * @cfg {string} [value] Initial query value
7978 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
7979 // Configuration intialization
7980 config
= config
|| {};
7982 // Parent constructor
7983 OO
.ui
.SearchWidget
.super.call( this, config
);
7986 this.query
= new OO
.ui
.TextInputWidget( {
7989 'placeholder': config
.placeholder
,
7990 'value': config
.value
7992 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
7993 this.$query
= this.$( '<div>' );
7994 this.$results
= this.$( '<div>' );
7997 this.query
.connect( this, {
7998 'change': 'onQueryChange',
7999 'enter': 'onQueryEnter'
8001 this.results
.connect( this, {
8002 'highlight': 'onResultsHighlight',
8003 'select': 'onResultsSelect'
8005 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
8009 .addClass( 'oo-ui-searchWidget-query' )
8010 .append( this.query
.$element
);
8012 .addClass( 'oo-ui-searchWidget-results' )
8013 .append( this.results
.$element
);
8015 .addClass( 'oo-ui-searchWidget' )
8016 .append( this.$results
, this.$query
);
8021 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
8027 * @param {Object|null} item Item data or null if no item is highlighted
8032 * @param {Object|null} item Item data or null if no item is selected
8038 * Handle query key down events.
8040 * @param {jQuery.Event} e Key down event
8042 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
8043 var highlightedItem
, nextItem
,
8044 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
8047 highlightedItem
= this.results
.getHighlightedItem();
8048 if ( !highlightedItem
) {
8049 highlightedItem
= this.results
.getSelectedItem();
8051 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
8052 this.results
.highlightItem( nextItem
);
8053 nextItem
.scrollElementIntoView();
8058 * Handle select widget select events.
8060 * Clears existing results. Subclasses should repopulate items according to new query.
8062 * @param {string} value New value
8064 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
8066 this.results
.clearItems();
8070 * Handle select widget enter key events.
8072 * Selects highlighted item.
8074 * @param {string} value New value
8076 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
8078 this.results
.selectItem( this.results
.getHighlightedItem() );
8082 * Handle select widget highlight events.
8084 * @param {OO.ui.OptionWidget} item Highlighted item
8087 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
8088 this.emit( 'highlight', item
? item
.getData() : null );
8092 * Handle select widget select events.
8094 * @param {OO.ui.OptionWidget} item Selected item
8097 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
8098 this.emit( 'select', item
? item
.getData() : null );
8102 * Get the query input.
8104 * @return {OO.ui.TextInputWidget} Query input
8106 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
8111 * Get the results list.
8113 * @return {OO.ui.SelectWidget} Select list
8115 OO
.ui
.SearchWidget
.prototype.getResults = function () {
8116 return this.results
;
8119 * Text input widget.
8122 * @extends OO.ui.InputWidget
8125 * @param {Object} [config] Configuration options
8126 * @cfg {string} [placeholder] Placeholder text
8127 * @cfg {string} [icon] Symbolic name of icon
8128 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8129 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8130 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8132 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8133 config
= $.extend( { 'maxRows': 10 }, config
);
8135 // Parent constructor
8136 OO
.ui
.TextInputWidget
.super.call( this, config
);
8140 this.multiline
= !!config
.multiline
;
8141 this.autosize
= !!config
.autosize
;
8142 this.maxRows
= config
.maxRows
;
8145 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8146 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8149 this.$element
.addClass( 'oo-ui-textInputWidget' );
8150 if ( config
.icon
) {
8151 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
8152 this.$element
.append(
8154 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
8155 .mousedown( OO
.ui
.bind( function () {
8156 this.$input
.focus();
8161 if ( config
.placeholder
) {
8162 this.$input
.attr( 'placeholder', config
.placeholder
);
8168 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8173 * User presses enter inside the text box.
8175 * Not called if input is multiline.
8183 * Handle key press events.
8185 * @param {jQuery.Event} e Key press event
8186 * @fires enter If enter key is pressed and input is not multiline
8188 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8189 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8190 this.emit( 'enter' );
8195 * Handle element attach events.
8197 * @param {jQuery.Event} e Element attach event
8199 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8206 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8210 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
8214 * Automatically adjust the size of the text input.
8216 * This only affects multi-line inputs that are auto-sized.
8220 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
8221 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
8223 if ( this.multiline
&& this.autosize
) {
8224 $clone
= this.$input
.clone()
8225 .val( this.$input
.val() )
8226 .css( { 'height': 0 } )
8227 .insertAfter( this.$input
);
8228 // Set inline height property to 0 to measure scroll height
8229 scrollHeight
= $clone
[0].scrollHeight
;
8230 // Remove inline height property to measure natural heights
8231 $clone
.css( 'height', '' );
8232 innerHeight
= $clone
.innerHeight();
8233 outerHeight
= $clone
.outerHeight();
8234 // Measure max rows height
8235 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
8236 maxInnerHeight
= $clone
.innerHeight();
8237 $clone
.removeAttr( 'rows' ).css( 'height', '' );
8239 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
8240 // Only apply inline height when expansion beyond natural height is needed
8243 // Use the difference between the inner and outer height as a buffer
8244 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
8251 * Get input element.
8253 * @param {Object} [config] Configuration options
8254 * @return {jQuery} Input element
8256 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
8257 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
8263 * Check if input supports multiple lines.
8267 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
8268 return !!this.multiline
;
8272 * Check if input automatically adjusts its size.
8276 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
8277 return !!this.autosize
;
8281 * Check if input is pending.
8285 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
8286 return !!this.pending
;
8290 * Increase the pending stack.
8294 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
8295 if ( this.pending
=== 0 ) {
8296 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
8297 this.$input
.addClass( 'oo-ui-texture-pending' );
8305 * Reduce the pending stack.
8311 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
8312 if ( this.pending
=== 1 ) {
8313 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
8314 this.$input
.removeClass( 'oo-ui-texture-pending' );
8316 this.pending
= Math
.max( 0, this.pending
- 1 );
8322 * Select the contents of the input.
8326 OO
.ui
.TextInputWidget
.prototype.select = function () {
8327 this.$input
.select();
8331 * Menu for a text input widget.
8334 * @extends OO.ui.MenuWidget
8337 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
8338 * @param {Object} [config] Configuration options
8339 * @cfg {jQuery} [$container=input.$element] Element to render menu under
8341 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
8342 // Parent constructor
8343 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
8347 this.$container
= config
.$container
|| this.input
.$element
;
8348 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
8351 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
8356 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
8361 * Handle window resize event.
8363 * @param {jQuery.Event} e Window resize event
8365 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
8374 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
8376 OO
.ui
.TextInputMenuWidget
.super.prototype.show
.call( this );
8379 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
8388 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
8390 OO
.ui
.TextInputMenuWidget
.super.prototype.hide
.call( this );
8392 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
8397 * Position the menu.
8401 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
8403 $container
= this.$container
,
8404 dimensions
= $container
.offset();
8406 // Position under input
8407 dimensions
.top
+= $container
.height();
8409 // Compensate for frame position if in a differnt frame
8410 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
8411 frameOffset
= OO
.ui
.Element
.getRelativePosition(
8412 this.input
.$.frame
.$element
, this.$element
.offsetParent()
8414 dimensions
.left
+= frameOffset
.left
;
8415 dimensions
.top
+= frameOffset
.top
;
8417 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
8418 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
8419 dimensions
.right
= this.$element
.parent().position().left
-
8420 $container
.width() - dimensions
.left
;
8421 // Erase the value for 'left':
8422 delete dimensions
.left
;
8426 this.$element
.css( dimensions
);
8427 this.setIdealSize( $container
.width() );
8431 * Width with on and off states.
8433 * Mixin for widgets with a boolean state.
8439 * @param {Object} [config] Configuration options
8440 * @cfg {boolean} [value=false] Initial value
8442 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8443 // Configuration initialization
8444 config
= config
|| {};
8450 this.$element
.addClass( 'oo-ui-toggleWidget' );
8451 this.setValue( !!config
.value
);
8458 * @param {boolean} value Changed value
8464 * Get the value of the toggle.
8468 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8473 * Set the value of the toggle.
8475 * @param {boolean} value New value
8479 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8481 if ( this.value
!== value
) {
8483 this.emit( 'change', value
);
8484 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8485 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8490 * Button that toggles on and off.
8493 * @extends OO.ui.ButtonWidget
8494 * @mixins OO.ui.ToggleWidget
8497 * @param {Object} [config] Configuration options
8498 * @cfg {boolean} [value=false] Initial value
8500 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8501 // Configuration initialization
8502 config
= config
|| {};
8504 // Parent constructor
8505 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8507 // Mixin constructors
8508 OO
.ui
.ToggleWidget
.call( this, config
);
8511 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8516 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8517 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8524 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8525 if ( !this.isDisabled() ) {
8526 this.setValue( !this.value
);
8530 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8536 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8538 if ( value
!== this.value
) {
8539 this.setActive( value
);
8542 // Parent method (from mixin)
8543 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8548 * Switch that slides on and off.
8551 * @extends OO.ui.Widget
8552 * @mixins OO.ui.ToggleWidget
8555 * @param {Object} [config] Configuration options
8556 * @cfg {boolean} [value=false] Initial value
8558 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
8559 // Parent constructor
8560 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
8562 // Mixin constructors
8563 OO
.ui
.ToggleWidget
.call( this, config
);
8566 this.dragging
= false;
8567 this.dragStart
= null;
8568 this.sliding
= false;
8569 this.$glow
= this.$( '<span>' );
8570 this.$grip
= this.$( '<span>' );
8573 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8576 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
8577 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
8579 .addClass( 'oo-ui-toggleSwitchWidget' )
8580 .append( this.$glow
, this.$grip
);
8585 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
8586 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
8591 * Handle mouse down events.
8593 * @param {jQuery.Event} e Mouse down event
8595 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
8596 if ( !this.isDisabled() && e
.which
=== 1 ) {
8597 this.setValue( !this.value
);