2 * OOjs UI v0.1.0-pre (432a76f7d4)
3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2014-07-02T16:19:00Z
16 * Namespace for all classes, static methods and static properties.
48 * Get the user's language and any fallback languages.
50 * These language codes are used to localize user interface elements in the user's language.
52 * In environments that provide a localization system, this function should be overridden to
53 * return the user's language(s). The default implementation returns English (en) only.
55 * @return {string[]} Language codes, in descending order of priority
57 OO
.ui
.getUserLanguages = function () {
62 * Get a value in an object keyed by language code.
64 * @param {Object.<string,Mixed>} obj Object keyed by language code
65 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
66 * @param {string} [fallback] Fallback code, used if no matching language can be found
67 * @return {Mixed} Local value
69 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
76 // Known user language
77 langs
= OO
.ui
.getUserLanguages();
78 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
85 if ( obj
[fallback
] ) {
88 // First existing language
99 * Message store for the default implementation of OO.ui.msg
101 * Environments that provide a localization system should not use this, but should override
102 * OO.ui.msg altogether.
107 // Label text for button to exit from dialog
108 'ooui-dialog-action-close': 'Close',
109 // Tool tip for a button that moves items in a list down one place
110 'ooui-outline-control-move-down': 'Move item down',
111 // Tool tip for a button that moves items in a list up one place
112 'ooui-outline-control-move-up': 'Move item up',
113 // Tool tip for a button that removes items from a list
114 'ooui-outline-control-remove': 'Remove item',
115 // Label for the toolbar group that contains a list of all other available tools
116 'ooui-toolbar-more': 'More',
118 // Label for the generic dialog used to confirm things
119 'ooui-dialog-confirm-title': 'Confirm',
120 // The default prompt of a confirmation dialog
121 'ooui-dialog-confirm-default-prompt': 'Are you sure?',
122 // The default OK button text on a confirmation dialog
123 'ooui-dialog-confirm-default-ok': 'OK',
124 // The default cancel button text on a confirmation dialog
125 'ooui-dialog-confirm-default-cancel': 'Cancel'
129 * Get a localized message.
131 * In environments that provide a localization system, this function should be overridden to
132 * return the message translated in the user's language. The default implementation always returns
135 * After the message key, message parameters may optionally be passed. In the default implementation,
136 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
137 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
138 * they support unnamed, ordered message parameters.
141 * @param {string} key Message key
142 * @param {Mixed...} [params] Message parameters
143 * @return {string} Translated message with parameters substituted
145 OO
.ui
.msg = function ( key
) {
146 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
147 if ( typeof message
=== 'string' ) {
148 // Perform $1 substitution
149 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
150 var i
= parseInt( n
, 10 );
151 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
154 // Return placeholder if message not found
155 message
= '[' + key
+ ']';
161 OO
.ui
.deferMsg = function ( key
) {
163 return OO
.ui
.msg( key
);
168 OO
.ui
.resolveMsg = function ( msg
) {
169 if ( $.isFunction( msg
) ) {
178 * DOM element abstraction.
184 * @param {Object} [config] Configuration options
185 * @cfg {Function} [$] jQuery for the frame the widget is in
186 * @cfg {string[]} [classes] CSS class names
187 * @cfg {string} [text] Text to insert
188 * @cfg {jQuery} [$content] Content elements to append (after text)
190 OO
.ui
.Element
= function OoUiElement( config
) {
191 // Configuration initialization
192 config
= config
|| {};
195 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
196 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
197 this.elementGroup
= null;
200 if ( $.isArray( config
.classes
) ) {
201 this.$element
.addClass( config
.classes
.join( ' ' ) );
204 this.$element
.text( config
.text
);
206 if ( config
.$content
) {
207 this.$element
.append( config
.$content
);
213 OO
.initClass( OO
.ui
.Element
);
215 /* Static Properties */
220 * This may be ignored if getTagName is overridden.
226 OO
.ui
.Element
.static.tagName
= 'div';
231 * Get a jQuery function within a specific document.
234 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
235 * @param {OO.ui.Frame} [frame] Frame of the document context
236 * @return {Function} Bound jQuery function
238 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
239 function wrapper( selector
) {
240 return $( selector
, wrapper
.context
);
243 wrapper
.context
= this.getDocument( context
);
246 wrapper
.frame
= frame
;
253 * Get the document of an element.
256 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
257 * @return {HTMLDocument|null} Document object
259 OO
.ui
.Element
.getDocument = function ( obj
) {
260 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
261 return ( obj
[0] && obj
[0].ownerDocument
) ||
262 // Empty jQuery selections might have a context
269 ( obj
.nodeType
=== 9 && obj
) ||
274 * Get the window of an element or document.
277 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
278 * @return {Window} Window object
280 OO
.ui
.Element
.getWindow = function ( obj
) {
281 var doc
= this.getDocument( obj
);
282 return doc
.parentWindow
|| doc
.defaultView
;
286 * Get the direction of an element or document.
289 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
290 * @return {string} Text direction, either `ltr` or `rtl`
292 OO
.ui
.Element
.getDir = function ( obj
) {
295 if ( obj
instanceof jQuery
) {
298 isDoc
= obj
.nodeType
=== 9;
299 isWin
= obj
.document
!== undefined;
300 if ( isDoc
|| isWin
) {
306 return $( obj
).css( 'direction' );
310 * Get the offset between two frames.
312 * TODO: Make this function not use recursion.
315 * @param {Window} from Window of the child frame
316 * @param {Window} [to=window] Window of the parent frame
317 * @param {Object} [offset] Offset to start with, used internally
318 * @return {Object} Offset object, containing left and top properties
320 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
321 var i
, len
, frames
, frame
, rect
;
327 offset
= { 'top': 0, 'left': 0 };
329 if ( from.parent
=== from ) {
333 // Get iframe element
334 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
335 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
336 if ( frames
[i
].contentWindow
=== from ) {
342 // Recursively accumulate offset values
344 rect
= frame
.getBoundingClientRect();
345 offset
.left
+= rect
.left
;
346 offset
.top
+= rect
.top
;
348 this.getFrameOffset( from.parent
, offset
);
355 * Get the offset between two elements.
358 * @param {jQuery} $from
359 * @param {jQuery} $to
360 * @return {Object} Translated position coordinates, containing top and left properties
362 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
363 var from = $from.offset(),
365 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
369 * Get element border sizes.
372 * @param {HTMLElement} el Element to measure
373 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
375 OO
.ui
.Element
.getBorders = function ( el
) {
376 var doc
= el
.ownerDocument
,
377 win
= doc
.parentWindow
|| doc
.defaultView
,
378 style
= win
&& win
.getComputedStyle
?
379 win
.getComputedStyle( el
, null ) :
382 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
383 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
384 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
385 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
388 'top': Math
.round( top
),
389 'left': Math
.round( left
),
390 'bottom': Math
.round( bottom
),
391 'right': Math
.round( right
)
396 * Get dimensions of an element or window.
399 * @param {HTMLElement|Window} el Element to measure
400 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
402 OO
.ui
.Element
.getDimensions = function ( el
) {
404 doc
= el
.ownerDocument
|| el
.document
,
405 win
= doc
.parentWindow
|| doc
.defaultView
;
407 if ( win
=== el
|| el
=== doc
.documentElement
) {
410 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
412 'top': $win
.scrollTop(),
413 'left': $win
.scrollLeft()
415 'scrollbar': { 'right': 0, 'bottom': 0 },
419 'bottom': $win
.innerHeight(),
420 'right': $win
.innerWidth()
426 'borders': this.getBorders( el
),
428 'top': $el
.scrollTop(),
429 'left': $el
.scrollLeft()
432 'right': $el
.innerWidth() - el
.clientWidth
,
433 'bottom': $el
.innerHeight() - el
.clientHeight
435 'rect': el
.getBoundingClientRect()
441 * Get closest scrollable container.
443 * Traverses up until either a scrollable element or the root is reached, in which case the window
447 * @param {HTMLElement} el Element to find scrollable container for
448 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
449 * @return {HTMLElement|Window} Closest scrollable container
451 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
453 props
= [ 'overflow' ],
454 $parent
= $( el
).parent();
456 if ( dimension
=== 'x' || dimension
=== 'y' ) {
457 props
.push( 'overflow-' + dimension
);
460 while ( $parent
.length
) {
461 if ( $parent
[0] === el
.ownerDocument
.body
) {
466 val
= $parent
.css( props
[i
] );
467 if ( val
=== 'auto' || val
=== 'scroll' ) {
471 $parent
= $parent
.parent();
473 return this.getDocument( el
).body
;
477 * Scroll element into view.
480 * @param {HTMLElement} el Element to scroll into view
481 * @param {Object} [config={}] Configuration config
482 * @param {string} [config.duration] jQuery animation duration value
483 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
484 * to scroll in both directions
485 * @param {Function} [config.complete] Function to call when scrolling completes
487 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
488 // Configuration initialization
489 config
= config
|| {};
492 callback
= typeof config
.complete
=== 'function' && config
.complete
,
493 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
495 eld
= this.getDimensions( el
),
496 scd
= this.getDimensions( sc
),
497 $win
= $( this.getWindow( el
) );
499 // Compute the distances between the edges of el and the edges of the scroll viewport
500 if ( $sc
.is( 'body' ) ) {
501 // If the scrollable container is the <body> this is easy
504 'bottom': $win
.innerHeight() - eld
.rect
.bottom
,
505 'left': eld
.rect
.left
,
506 'right': $win
.innerWidth() - eld
.rect
.right
509 // Otherwise, we have to subtract el's coordinates from sc's coordinates
511 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
512 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
513 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
514 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
518 if ( !config
.direction
|| config
.direction
=== 'y' ) {
520 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
521 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
522 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
525 if ( !config
.direction
|| config
.direction
=== 'x' ) {
526 if ( rel
.left
< 0 ) {
527 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
528 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
529 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
532 if ( !$.isEmptyObject( anim
) ) {
533 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
535 $sc
.queue( function ( next
) {
550 * Get the HTML tag name.
552 * Override this method to base the result on instance information.
554 * @return {string} HTML tag name
556 OO
.ui
.Element
.prototype.getTagName = function () {
557 return this.constructor.static.tagName
;
561 * Check if the element is attached to the DOM
562 * @return {boolean} The element is attached to the DOM
564 OO
.ui
.Element
.prototype.isElementAttached = function () {
565 return $.contains( this.getElementDocument(), this.$element
[0] );
569 * Get the DOM document.
571 * @return {HTMLDocument} Document object
573 OO
.ui
.Element
.prototype.getElementDocument = function () {
574 return OO
.ui
.Element
.getDocument( this.$element
);
578 * Get the DOM window.
580 * @return {Window} Window object
582 OO
.ui
.Element
.prototype.getElementWindow = function () {
583 return OO
.ui
.Element
.getWindow( this.$element
);
587 * Get closest scrollable container.
589 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
590 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
594 * Get group element is in.
596 * @return {OO.ui.GroupElement|null} Group element, null if none
598 OO
.ui
.Element
.prototype.getElementGroup = function () {
599 return this.elementGroup
;
603 * Set group element is in.
605 * @param {OO.ui.GroupElement|null} group Group element, null if none
608 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
609 this.elementGroup
= group
;
614 * Scroll element into view.
616 * @param {Object} [config={}]
618 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
619 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
623 * Bind a handler for an event on this.$element
625 * @deprecated Use jQuery#on instead.
626 * @param {string} event
627 * @param {Function} callback
629 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
630 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
634 * Unbind a handler bound with #offDOMEvent
636 * @deprecated Use jQuery#off instead.
637 * @param {string} event
638 * @param {Function} callback
640 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
641 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
646 * Bind a handler for an event on a DOM element.
648 * Used to be for working around a jQuery bug (jqbug.com/14180),
649 * but obsolete as of jQuery 1.11.0.
652 * @deprecated Use jQuery#on instead.
653 * @param {HTMLElement|jQuery} el DOM element
654 * @param {string} event Event to bind
655 * @param {Function} callback Callback to call when the event fires
657 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
658 $( el
).on( event
, callback
);
662 * Unbind a handler bound with #static-method-onDOMEvent.
664 * @deprecated Use jQuery#off instead.
666 * @param {HTMLElement|jQuery} el DOM element
667 * @param {string} event Event to unbind
668 * @param {Function} [callback] Callback to unbind
670 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
671 $( el
).off( event
, callback
);
676 * Embedded iframe with the same styles as its parent.
679 * @extends OO.ui.Element
680 * @mixins OO.EventEmitter
683 * @param {Object} [config] Configuration options
685 OO
.ui
.Frame
= function OoUiFrame( config
) {
686 // Parent constructor
687 OO
.ui
.Frame
.super.call( this, config
);
689 // Mixin constructors
690 OO
.EventEmitter
.call( this );
694 this.config
= config
;
698 .addClass( 'oo-ui-frame' )
699 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
705 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
706 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
708 /* Static Properties */
714 OO
.ui
.Frame
.static.tagName
= 'iframe';
725 * Transplant the CSS styles from as parent document to a frame's document.
727 * This loops over the style sheets in the parent document, and copies their nodes to the
728 * frame's document. It then polls the document to see when all styles have loaded, and once they
729 * have, resolves the promise.
731 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
732 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
733 * Firefox, where the styles won't load until the iframe becomes visible.
735 * For details of how we arrived at the strategy used in this function, see #load.
739 * @param {HTMLDocument} parentDoc Document to transplant styles from
740 * @param {HTMLDocument} frameDoc Document to transplant styles to
741 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
742 * @return {jQuery.Promise} Promise resolved when styles have loaded
744 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
745 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
746 $pollNodes
= $( [] ),
747 // Fake font-family value
748 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
749 deferred
= $.Deferred();
751 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
752 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
753 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
754 // External stylesheet
755 // Create a node with a unique ID that we're going to monitor to see when the CSS
757 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
758 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
759 .attr( 'id', pollNodeId
)
760 .appendTo( frameDoc
.body
)
763 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
764 // The font-family rule will only take effect once the @import finishes
765 newNode
= frameDoc
.createElement( 'style' );
766 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
767 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
769 // Not an external stylesheet, or no polling required; just copy the node over
770 newNode
= frameDoc
.importNode( styleNode
, true );
772 frameDoc
.head
.appendChild( newNode
);
775 // Poll every 100ms until all external stylesheets have loaded
776 $pendingPollNodes
= $pollNodes
;
777 timeoutID
= setTimeout( function pollExternalStylesheets() {
779 $pendingPollNodes
.length
> 0 &&
780 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
782 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
785 if ( $pendingPollNodes
.length
=== 0 ) {
787 if ( timeoutID
!== null ) {
793 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
796 // ...but give up after a while
797 if ( timeout
!== 0 ) {
798 setTimeout( function () {
800 clearTimeout( timeoutID
);
805 }, timeout
|| 5000 );
808 return deferred
.promise();
814 * Load the frame contents.
816 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
817 * promise will be resolved. Calling while loading will return a promise but not trigger a new
818 * loading cycle. Calling after loading is complete will return a promise that's already been
821 * Sounds simple right? Read on...
823 * When you create a dynamic iframe using open/write/close, the window.load event for the
824 * iframe is triggered when you call close, and there's no further load event to indicate that
825 * everything is actually loaded.
827 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
828 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
829 * are added to document.styleSheets immediately, and the only way you can determine whether they've
830 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
831 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
833 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
834 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
835 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
836 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
837 * .cssRules is allowed.
839 * However, now that we control the styles we're injecting, we might as well do away with
840 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
841 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
842 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
843 * rule is not applied until after the `@import` finishes.
845 * All this stylesheet injection and polling magic is in #transplantStyles.
847 * @return {jQuery.Promise} Promise resolved when loading is complete
850 OO
.ui
.Frame
.prototype.load = function () {
853 // Return existing promise if already loading or loaded
854 if ( this.loading
) {
855 return this.loading
.promise();
859 this.loading
= $.Deferred();
861 win
= this.$element
.prop( 'contentWindow' );
864 // Figure out directionality:
865 this.dir
= OO
.ui
.Element
.getDir( this.$element
) || 'ltr';
867 // Initialize contents
872 '<body class="oo-ui-frame-body oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
873 '<div class="oo-ui-frame-content"></div>' +
880 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
881 this.$content
= this.$( '.oo-ui-frame-content' ).attr( 'tabIndex', 0 );
882 this.$document
= this.$( doc
);
885 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
886 .always( OO
.ui
.bind( function () {
888 this.loading
.resolve();
891 return this.loading
.promise();
895 * Set the size of the frame.
897 * @param {number} width Frame width in pixels
898 * @param {number} height Frame height in pixels
901 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
902 this.$element
.css( { 'width': width
, 'height': height
} );
907 * Container for elements in a child frame.
909 * There are two ways to specify a title: set the static `title` property or provide a `title`
910 * property in the configuration options. The latter will override the former.
914 * @extends OO.ui.Element
915 * @mixins OO.EventEmitter
918 * @param {Object} [config] Configuration options
919 * @cfg {string|Function} [title] Title string or function that returns a string
920 * @cfg {string} [icon] Symbolic name of icon
923 OO
.ui
.Window
= function OoUiWindow( config
) {
925 // Parent constructor
926 OO
.ui
.Window
.super.call( this, config
);
928 // Mixin constructors
929 OO
.EventEmitter
.call( this );
932 this.visible
= false;
936 this.title
= OO
.ui
.resolveMsg( config
.title
|| this.constructor.static.title
);
937 this.icon
= config
.icon
|| this.constructor.static.icon
;
938 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
939 this.$frame
= this.$( '<div>' );
940 this.$ = function () {
941 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
946 .addClass( 'oo-ui-window' )
947 // Hide the window using visibility: hidden; while the iframe is still loading
948 // Can't use display: none; because that prevents the iframe from loading in Firefox
949 .css( 'visibility', 'hidden' )
950 .append( this.$frame
);
952 .addClass( 'oo-ui-window-frame' )
953 .append( this.frame
.$element
);
956 this.frame
.on( 'load', function () {
957 element
.initialize();
958 // Undo the visibility: hidden; hack and apply display: none;
959 // We can do this safely now that the iframe has initialized
960 // (don't do this from within #initialize because it has to happen
961 // after the all subclasses have been handled as well).
962 element
.$element
.hide().css( 'visibility', '' );
968 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
969 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
976 * Fired after the setup process has been executed.
979 * @param {Object} data Window opening data
985 * Fired after the ready process has been executed.
988 * @param {Object} data Window opening data
992 * Window is torn down
994 * Fired after the teardown process has been executed.
997 * @param {Object} data Window closing data
1000 /* Static Properties */
1003 * Symbolic name of icon.
1007 * @property {string}
1009 OO
.ui
.Window
.static.icon
= 'window';
1014 * Subclasses must implement this property before instantiating the window.
1015 * Alternatively, override #getTitle with an alternative implementation.
1020 * @property {string|Function} Title string or function that returns a string
1022 OO
.ui
.Window
.static.title
= null;
1027 * Check if window is visible.
1029 * @return {boolean} Window is visible
1031 OO
.ui
.Window
.prototype.isVisible = function () {
1032 return this.visible
;
1036 * Check if window is opening.
1038 * @return {boolean} Window is opening
1040 OO
.ui
.Window
.prototype.isOpening = function () {
1041 return !!this.opening
&& this.opening
.state() === 'pending';
1045 * Check if window is closing.
1047 * @return {boolean} Window is closing
1049 OO
.ui
.Window
.prototype.isClosing = function () {
1050 return !!this.closing
&& this.closing
.state() === 'pending';
1054 * Check if window is opened.
1056 * @return {boolean} Window is opened
1058 OO
.ui
.Window
.prototype.isOpened = function () {
1059 return !!this.opened
&& this.opened
.state() === 'pending';
1063 * Get the window frame.
1065 * @return {OO.ui.Frame} Frame of window
1067 OO
.ui
.Window
.prototype.getFrame = function () {
1072 * Get the title of the window.
1074 * @return {string} Title text
1076 OO
.ui
.Window
.prototype.getTitle = function () {
1081 * Get the window icon.
1083 * @return {string} Symbolic name of icon
1085 OO
.ui
.Window
.prototype.getIcon = function () {
1090 * Set the size of window frame.
1092 * @param {number} [width=auto] Custom width
1093 * @param {number} [height=auto] Custom height
1096 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1097 if ( !this.frame
.$content
) {
1101 this.frame
.$element
.css( {
1102 'width': width
=== undefined ? 'auto' : width
,
1103 'height': height
=== undefined ? 'auto' : height
1110 * Set the title of the window.
1112 * @param {string|Function} title Title text or a function that returns text
1115 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1116 this.title
= OO
.ui
.resolveMsg( title
);
1117 if ( this.$title
) {
1118 this.$title
.text( title
);
1124 * Set the icon of the window.
1126 * @param {string} icon Symbolic name of icon
1129 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1131 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1135 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1142 * Set the position of window to fit with contents.
1144 * @param {string} left Left offset
1145 * @param {string} top Top offset
1148 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1149 this.$element
.css( { 'left': left
, 'top': top
} );
1154 * Set the height of window to fit with contents.
1156 * @param {number} [min=0] Min height
1157 * @param {number} [max] Max height (defaults to content's outer height)
1160 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1161 var height
= this.frame
.$content
.outerHeight();
1163 this.frame
.$element
.css(
1164 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1171 * Set the width of window to fit with contents.
1173 * @param {number} [min=0] Min height
1174 * @param {number} [max] Max height (defaults to content's outer width)
1177 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1178 var width
= this.frame
.$content
.outerWidth();
1180 this.frame
.$element
.css(
1181 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1188 * Initialize window contents.
1190 * The first time the window is opened, #initialize is called when it's safe to begin populating
1191 * its contents. See #setup for a way to make changes each time the window opens.
1193 * Once this method is called, this.$$ can be used to create elements within the frame.
1197 OO
.ui
.Window
.prototype.initialize = function () {
1199 this.$ = this.frame
.$;
1200 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1201 .text( this.title
);
1202 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1203 .addClass( 'oo-ui-icon-' + this.icon
);
1204 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1205 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1206 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1207 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1210 this.frame
.$content
.append(
1211 this.$head
.append( this.$icon
, this.$title
),
1221 * Get a process for setting up a window for use.
1223 * Each time the window is opened this process will set it up for use in a particular context, based
1224 * on the `data` argument.
1226 * When you override this method, you can add additional setup steps to the process the parent
1227 * method provides using the 'first' and 'next' methods.
1230 * @param {Object} [data] Window opening data
1231 * @return {OO.ui.Process} Setup process
1233 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1234 return new OO
.ui
.Process();
1238 * Get a process for readying a window for use.
1240 * Each time the window is open and setup, this process will ready it up for use in a particular
1241 * context, based on the `data` argument.
1243 * When you override this method, you can add additional setup steps to the process the parent
1244 * method provides using the 'first' and 'next' methods.
1247 * @param {Object} [data] Window opening data
1248 * @return {OO.ui.Process} Setup process
1250 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1251 return new OO
.ui
.Process();
1255 * Get a process for tearing down a window after use.
1257 * Each time the window is closed this process will tear it down and do something with the user's
1258 * interactions within the window, based on the `data` argument.
1260 * When you override this method, you can add additional teardown steps to the process the parent
1261 * method provides using the 'first' and 'next' methods.
1264 * @param {Object} [data] Window closing data
1265 * @return {OO.ui.Process} Teardown process
1267 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1268 return new OO
.ui
.Process();
1274 * Do not override this method. Use #getSetupProcess to do something each time the window closes.
1276 * @param {Object} [data] Window opening data
1281 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1282 * first argument will be a promise which will be resolved when the window begins closing
1284 OO
.ui
.Window
.prototype.open = function ( data
) {
1285 // Return existing promise if already opening or open
1286 if ( this.opening
) {
1287 return this.opening
.promise();
1291 this.opening
= $.Deferred();
1293 this.$ariaHidden
= $( 'body' ).children().not( this.$element
.parentsUntil( 'body' ).last() )
1294 .attr( 'aria-hidden', '' );
1296 this.frame
.load().done( OO
.ui
.bind( function () {
1297 this.$element
.show();
1298 this.visible
= true;
1299 this.getSetupProcess( data
).execute().done( OO
.ui
.bind( function () {
1300 this.$element
.addClass( 'oo-ui-window-setup' );
1301 this.emit( 'setup', data
);
1302 setTimeout( OO
.ui
.bind( function () {
1303 this.frame
.$content
.focus();
1304 this.getReadyProcess( data
).execute().done( OO
.ui
.bind( function () {
1305 this.$element
.addClass( 'oo-ui-window-ready' );
1306 this.emit( 'ready', data
);
1307 this.opened
= $.Deferred();
1308 // Now that we are totally done opening, it's safe to allow closing
1309 this.closing
= null;
1310 this.opening
.resolve( this.opened
.promise() );
1316 return this.opening
.promise();
1322 * Do not override this method. Use #getTeardownProcess to do something each time the window closes.
1324 * @param {Object} [data] Window closing data
1327 * @return {jQuery.Promise} Promise resolved when window is closed
1329 OO
.ui
.Window
.prototype.close = function ( data
) {
1332 // Return existing promise if already closing or closed
1333 if ( this.closing
) {
1334 return this.closing
.promise();
1337 // Close after opening is done if opening is in progress
1338 if ( this.opening
&& this.opening
.state() === 'pending' ) {
1339 close
= OO
.ui
.bind( function () {
1340 return this.close( data
);
1342 return this.opening
.then( close
, close
);
1346 // This.closing needs to exist before we emit the closing event so that handlers can call
1347 // window.close() and trigger the safety check above
1348 this.closing
= $.Deferred();
1349 this.frame
.$content
.find( ':focus' ).blur();
1350 this.$element
.removeClass( 'oo-ui-window-ready' );
1351 this.getTeardownProcess( data
).execute().done( OO
.ui
.bind( function () {
1352 this.$element
.removeClass( 'oo-ui-window-setup' );
1353 this.emit( 'teardown', data
);
1354 // To do something different with #opened, resolve/reject #opened in the teardown process
1355 if ( this.opened
&& this.opened
.state() === 'pending' ) {
1356 this.opened
.resolve();
1358 this.$element
.hide();
1359 if ( this.$ariaHidden
) {
1360 this.$ariaHidden
.removeAttr( 'aria-hidden' );
1361 this.$ariaHidden
= undefined;
1363 this.visible
= false;
1364 this.closing
.resolve();
1365 // Now that we are totally done closing, it's safe to allow opening
1366 this.opening
= null;
1369 return this.closing
.promise();
1373 * Set of mutually exclusive windows.
1376 * @extends OO.ui.Element
1377 * @mixins OO.EventEmitter
1380 * @param {OO.Factory} factory Window factory
1381 * @param {Object} [config] Configuration options
1383 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1384 // Parent constructor
1385 OO
.ui
.WindowSet
.super.call( this, config
);
1387 // Mixin constructors
1388 OO
.EventEmitter
.call( this );
1391 this.factory
= factory
;
1394 * List of all windows associated with this window set.
1396 * @property {OO.ui.Window[]}
1398 this.windowList
= [];
1401 * Mapping of OO.ui.Window objects created by name from the #factory.
1403 * @property {Object}
1406 this.currentWindow
= null;
1409 this.$element
.addClass( 'oo-ui-windowSet' );
1414 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1415 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1421 * @param {OO.ui.Window} win Window that's been setup
1422 * @param {Object} config Window opening information
1427 * @param {OO.ui.Window} win Window that's ready
1428 * @param {Object} config Window opening information
1433 * @param {OO.ui.Window} win Window that's been torn down
1434 * @param {Object} config Window closing information
1440 * Handle a window setup event.
1442 * @param {OO.ui.Window} win Window that's been setup
1443 * @param {Object} [config] Window opening information
1446 OO
.ui
.WindowSet
.prototype.onWindowSetup = function ( win
, config
) {
1447 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1448 this.currentWindow
.close();
1450 this.currentWindow
= win
;
1451 this.emit( 'setup', win
, config
);
1455 * Handle a window ready event.
1457 * @param {OO.ui.Window} win Window that's ready
1458 * @param {Object} [config] Window opening information
1461 OO
.ui
.WindowSet
.prototype.onWindowReady = function ( win
, config
) {
1462 this.emit( 'ready', win
, config
);
1466 * Handle a window teardown event.
1468 * @param {OO.ui.Window} win Window that's been torn down
1469 * @param {Object} [config] Window closing information
1472 OO
.ui
.WindowSet
.prototype.onWindowTeardown = function ( win
, config
) {
1473 this.currentWindow
= null;
1474 this.emit( 'teardown', win
, config
);
1478 * Get the current window.
1480 * @return {OO.ui.Window|null} Current window or null if none open
1482 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1483 return this.currentWindow
;
1487 * Return a given window.
1489 * @param {string} name Symbolic name of window
1490 * @return {OO.ui.Window} Window with specified name
1492 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1495 if ( !this.factory
.lookup( name
) ) {
1496 throw new Error( 'Unknown window: ' + name
);
1498 if ( !( name
in this.windows
) ) {
1499 win
= this.windows
[name
] = this.createWindow( name
);
1500 this.addWindow( win
);
1502 return this.windows
[name
];
1506 * Create a window for use in this window set.
1508 * @param {string} name Symbolic name of window
1509 * @return {OO.ui.Window} Window with specified name
1511 OO
.ui
.WindowSet
.prototype.createWindow = function ( name
) {
1512 return this.factory
.create( name
, { '$': this.$ } );
1516 * Add a given window to this window set.
1518 * Connects event handlers and attaches it to the DOM. Calling
1519 * OO.ui.Window#open will not work until the window is added to the set.
1521 * @param {OO.ui.Window} win Window to add
1523 OO
.ui
.WindowSet
.prototype.addWindow = function ( win
) {
1524 if ( this.windowList
.indexOf( win
) !== -1 ) {
1528 this.windowList
.push( win
);
1530 win
.connect( this, {
1531 'setup': [ 'onWindowSetup', win
],
1532 'ready': [ 'onWindowReady', win
],
1533 'teardown': [ 'onWindowTeardown', win
]
1535 this.$element
.append( win
.$element
);
1539 * Modal dialog window.
1543 * @extends OO.ui.Window
1546 * @param {Object} [config] Configuration options
1547 * @cfg {boolean} [footless] Hide foot
1548 * @cfg {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1550 OO
.ui
.Dialog
= function OoUiDialog( config
) {
1551 // Configuration initialization
1552 config
= $.extend( { 'size': 'large' }, config
);
1554 // Parent constructor
1555 OO
.ui
.Dialog
.super.call( this, config
);
1558 this.visible
= false;
1559 this.footless
= !!config
.footless
;
1562 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1563 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1566 this.$element
.on( 'mousedown', false );
1569 this.$element
.addClass( 'oo-ui-dialog' ).attr( 'role', 'dialog' );
1570 this.setSize( config
.size
);
1575 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1577 /* Static Properties */
1580 * Symbolic name of dialog.
1585 * @property {string}
1587 OO
.ui
.Dialog
.static.name
= '';
1590 * Map of symbolic size names and CSS classes.
1594 * @property {Object}
1596 OO
.ui
.Dialog
.static.sizeCssClasses
= {
1597 'small': 'oo-ui-dialog-small',
1598 'medium': 'oo-ui-dialog-medium',
1599 'large': 'oo-ui-dialog-large'
1605 * Handle close button click events.
1607 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1608 this.close( { 'action': 'cancel' } );
1612 * Handle window mouse wheel events.
1614 * @param {jQuery.Event} e Mouse wheel event
1616 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1621 * Handle document key down events.
1623 * @param {jQuery.Event} e Key down event
1625 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1626 switch ( e
.which
) {
1627 case OO
.ui
.Keys
.PAGEUP
:
1628 case OO
.ui
.Keys
.PAGEDOWN
:
1629 case OO
.ui
.Keys
.END
:
1630 case OO
.ui
.Keys
.HOME
:
1631 case OO
.ui
.Keys
.LEFT
:
1633 case OO
.ui
.Keys
.RIGHT
:
1634 case OO
.ui
.Keys
.DOWN
:
1635 // Prevent any key events that might cause scrolling
1641 * Handle frame document key down events.
1643 * @param {jQuery.Event} e Key down event
1645 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1646 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1647 this.close( { 'action': 'cancel' } );
1655 * @param {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1657 OO
.ui
.Dialog
.prototype.setSize = function ( size
) {
1658 var name
, state
, cssClass
,
1659 sizeCssClasses
= OO
.ui
.Dialog
.static.sizeCssClasses
;
1661 if ( !sizeCssClasses
[size
] ) {
1665 for ( name
in sizeCssClasses
) {
1666 state
= name
=== size
;
1667 cssClass
= sizeCssClasses
[name
];
1668 this.$element
.toggleClass( cssClass
, state
);
1675 OO
.ui
.Dialog
.prototype.initialize = function () {
1677 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
1680 this.closeButton
= new OO
.ui
.ButtonWidget( {
1684 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1688 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1689 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1692 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1693 if ( this.footless
) {
1694 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1696 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1697 this.$head
.append( this.closeButton
.$element
);
1703 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
1704 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
1705 .next( function () {
1706 // Prevent scrolling in top-level window
1707 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1708 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1715 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
1716 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
1717 .first( function () {
1718 // Wait for closing transition
1719 return OO
.ui
.Process
.static.delay( 250 );
1721 .next( function () {
1722 // Allow scrolling in top-level window
1723 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1724 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1729 * Check if input is pending.
1733 OO
.ui
.Dialog
.prototype.isPending = function () {
1734 return !!this.pending
;
1738 * Increase the pending stack.
1742 OO
.ui
.Dialog
.prototype.pushPending = function () {
1743 if ( this.pending
=== 0 ) {
1744 this.frame
.$content
.addClass( 'oo-ui-dialog-pending' );
1745 this.$head
.addClass( 'oo-ui-texture-pending' );
1746 this.$foot
.addClass( 'oo-ui-texture-pending' );
1754 * Reduce the pending stack.
1760 OO
.ui
.Dialog
.prototype.popPending = function () {
1761 if ( this.pending
=== 1 ) {
1762 this.frame
.$content
.removeClass( 'oo-ui-dialog-pending' );
1763 this.$head
.removeClass( 'oo-ui-texture-pending' );
1764 this.$foot
.removeClass( 'oo-ui-texture-pending' );
1766 this.pending
= Math
.max( 0, this.pending
- 1 );
1772 * Container for elements.
1776 * @extends OO.ui.Element
1777 * @mixins OO.EventEmitter
1780 * @param {Object} [config] Configuration options
1782 OO
.ui
.Layout
= function OoUiLayout( config
) {
1783 // Initialize config
1784 config
= config
|| {};
1786 // Parent constructor
1787 OO
.ui
.Layout
.super.call( this, config
);
1789 // Mixin constructors
1790 OO
.EventEmitter
.call( this );
1793 this.$element
.addClass( 'oo-ui-layout' );
1798 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1799 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1802 * User interface control.
1806 * @extends OO.ui.Element
1807 * @mixins OO.EventEmitter
1810 * @param {Object} [config] Configuration options
1811 * @cfg {boolean} [disabled=false] Disable
1813 OO
.ui
.Widget
= function OoUiWidget( config
) {
1814 // Initialize config
1815 config
= $.extend( { 'disabled': false }, config
);
1817 // Parent constructor
1818 OO
.ui
.Widget
.super.call( this, config
);
1820 // Mixin constructors
1821 OO
.EventEmitter
.call( this );
1824 this.disabled
= null;
1825 this.wasDisabled
= null;
1828 this.$element
.addClass( 'oo-ui-widget' );
1829 this.setDisabled( !!config
.disabled
);
1834 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1835 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1841 * @param {boolean} disabled Widget is disabled
1847 * Check if the widget is disabled.
1849 * @param {boolean} Button is disabled
1851 OO
.ui
.Widget
.prototype.isDisabled = function () {
1852 return this.disabled
;
1856 * Update the disabled state, in case of changes in parent widget.
1860 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1861 this.setDisabled( this.disabled
);
1866 * Set the disabled state of the widget.
1868 * This should probably change the widgets' appearance and prevent it from being used.
1870 * @param {boolean} disabled Disable widget
1873 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1876 this.disabled
= !!disabled
;
1877 isDisabled
= this.isDisabled();
1878 if ( isDisabled
!== this.wasDisabled
) {
1879 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1880 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1881 this.emit( 'disable', isDisabled
);
1883 this.wasDisabled
= isDisabled
;
1888 * A list of functions, called in sequence.
1890 * If a function added to a process returns boolean false the process will stop; if it returns an
1891 * object with a `promise` method the process will use the promise to either continue to the next
1892 * step when the promise is resolved or stop when the promise is rejected.
1898 OO
.ui
.Process = function () {
1905 OO
.initClass( OO
.ui
.Process
);
1907 /* Static Methods */
1910 * Generate a promise which is resolved after a set amount of time.
1912 * @param {number} length Number of milliseconds before resolving the promise
1913 * @return {jQuery.Promise} Promise that will be resolved after a set amount of time
1915 OO
.ui
.Process
.static.delay = function ( length
) {
1916 var deferred
= $.Deferred();
1918 setTimeout( function () {
1922 return deferred
.promise();
1928 * Start the process.
1930 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
1931 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
1932 * process, the remaining steps will not be taken
1934 OO
.ui
.Process
.prototype.execute = function () {
1935 var i
, len
, promise
;
1938 * Continue execution.
1941 * @param {Array} step A function and the context it should be called in
1942 * @return {Function} Function that continues the process
1944 function proceed( step
) {
1945 return function () {
1946 // Execute step in the correct context
1947 var result
= step
[0].call( step
[1] );
1949 if ( result
=== false ) {
1950 // Use rejected promise for boolean false results
1951 return $.Deferred().reject().promise();
1953 // Duck-type the object to see if it can produce a promise
1954 if ( result
&& $.isFunction( result
.promise
) ) {
1955 // Use a promise generated from the result
1956 return result
.promise();
1958 // Use resolved promise for other results
1959 return $.Deferred().resolve().promise();
1963 if ( this.steps
.length
) {
1964 // Generate a chain reaction of promises
1965 promise
= proceed( this.steps
[0] )();
1966 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
1967 promise
= promise
.then( proceed( this.steps
[i
] ) );
1970 promise
= $.Deferred().resolve().promise();
1977 * Add step to the beginning of the process.
1979 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
1980 * it returns an object with a `promise` method the process will use the promise to either
1981 * continue to the next step when the promise is resolved or stop when the promise is rejected
1982 * @param {Object} [context=null] Context to call the step function in
1985 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
1986 this.steps
.unshift( [ step
, context
|| null ] );
1991 * Add step to the end of the process.
1993 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
1994 * it returns an object with a `promise` method the process will use the promise to either
1995 * continue to the next step when the promise is resolved or stop when the promise is rejected
1996 * @param {Object} [context=null] Context to call the step function in
1999 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
2000 this.steps
.push( [ step
, context
|| null ] );
2005 * Dialog for showing a confirmation/warning message.
2008 * @extends OO.ui.Dialog
2011 * @param {Object} [config] Configuration options
2013 OO
.ui
.ConfirmationDialog
= function OoUiConfirmationDialog( config
) {
2014 // Configuration initialization
2015 config
= $.extend( { 'size': 'small' }, config
);
2017 // Parent constructor
2018 OO
.ui
.Dialog
.call( this, config
);
2023 OO
.inheritClass( OO
.ui
.ConfirmationDialog
, OO
.ui
.Dialog
);
2025 /* Static Properties */
2027 OO
.ui
.ConfirmationDialog
.static.name
= 'confirm';
2029 OO
.ui
.ConfirmationDialog
.static.icon
= 'help';
2031 OO
.ui
.ConfirmationDialog
.static.title
= OO
.ui
.deferMsg( 'ooui-dialog-confirm-title' );
2038 OO
.ui
.ConfirmationDialog
.prototype.initialize = function () {
2040 OO
.ui
.Dialog
.prototype.initialize
.call( this );
2042 // Set up the layout
2043 var contentLayout
= new OO
.ui
.PanelLayout( {
2048 this.$promptContainer
= this.$( '<div>' ).addClass( 'oo-ui-dialog-confirm-promptContainer' );
2050 this.cancelButton
= new OO
.ui
.ButtonWidget();
2051 this.cancelButton
.connect( this, { 'click': [ 'close', 'cancel' ] } );
2053 this.okButton
= new OO
.ui
.ButtonWidget();
2054 this.okButton
.connect( this, { 'click': [ 'close', 'ok' ] } );
2057 contentLayout
.$element
.append( this.$promptContainer
);
2058 this.$body
.append( contentLayout
.$element
);
2061 this.okButton
.$element
,
2062 this.cancelButton
.$element
2067 * Setup a confirmation dialog.
2069 * @param {Object} [data] Window opening data including text of the dialog and text for the buttons
2070 * @param {jQuery|string} [data.prompt] Text to display or list of nodes to use as content of the dialog.
2071 * @param {jQuery|string|Function|null} [data.okLabel] Label of the OK button
2072 * @param {jQuery|string|Function|null} [data.cancelLabel] Label of the cancel button
2073 * @param {string|string[]} [data.okFlags="constructive"] Flags for the OK button
2074 * @param {string|string[]} [data.cancelFlags="destructive"] Flags for the cancel button
2075 * @return {OO.ui.Process} Setup process
2077 OO
.ui
.ConfirmationDialog
.prototype.getSetupProcess = function ( data
) {
2079 return OO
.ui
.ConfirmationDialog
.super.prototype.getSetupProcess
.call( this, data
)
2080 .next( function () {
2081 var prompt
= data
.prompt
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-prompt' ),
2082 okLabel
= data
.okLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-ok' ),
2083 cancelLabel
= data
.cancelLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-cancel' ),
2084 okFlags
= data
.okFlags
|| 'constructive',
2085 cancelFlags
= data
.cancelFlags
|| 'destructive';
2087 if ( typeof prompt
=== 'string' ) {
2088 this.$promptContainer
.text( prompt
);
2090 this.$promptContainer
.empty().append( prompt
);
2093 this.okButton
.setLabel( okLabel
).clearFlags().setFlags( okFlags
);
2094 this.cancelButton
.setLabel( cancelLabel
).clearFlags().setFlags( cancelFlags
);
2101 OO
.ui
.ConfirmationDialog
.prototype.getTeardownProcess = function ( data
) {
2103 return OO
.ui
.ConfirmationDialog
.super.prototype.getTeardownProcess
.call( this, data
)
2104 .first( function () {
2105 if ( data
=== 'ok' ) {
2106 this.opened
.resolve();
2107 } else { // data === 'cancel', or no data
2108 this.opened
.reject();
2114 * Element with a button.
2120 * @param {jQuery} $button Button node, assigned to #$button
2121 * @param {Object} [config] Configuration options
2122 * @cfg {boolean} [frameless] Render button without a frame
2123 * @cfg {number} [tabIndex=0] Button's tab index, use -1 to prevent tab focusing
2125 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
2126 // Configuration initialization
2127 config
= config
|| {};
2130 this.$button
= $button
;
2131 this.tabIndex
= null;
2132 this.active
= false;
2133 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
2136 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2140 .addClass( 'oo-ui-buttonedElement' )
2141 .prop( 'tabIndex', config
.tabIndex
|| 0 );
2143 .addClass( 'oo-ui-buttonedElement-button' )
2144 .attr( 'role', 'button' );
2145 if ( config
.frameless
) {
2146 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
2148 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
2154 OO
.initClass( OO
.ui
.ButtonedElement
);
2156 /* Static Properties */
2159 * Cancel mouse down events.
2163 * @property {boolean}
2165 OO
.ui
.ButtonedElement
.static.cancelButtonMouseDownEvents
= true;
2170 * Handles mouse down events.
2172 * @param {jQuery.Event} e Mouse down event
2174 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function ( e
) {
2175 if ( this.isDisabled() || e
.which
!== 1 ) {
2178 // tabIndex should generally be interacted with via the property, but it's not possible to
2179 // reliably unset a tabIndex via a property so we use the (lowercase) "tabindex" attribute
2180 this.tabIndex
= this.$button
.attr( 'tabindex' );
2182 // Remove the tab-index while the button is down to prevent the button from stealing focus
2183 .removeAttr( 'tabindex' )
2184 .addClass( 'oo-ui-buttonedElement-pressed' );
2185 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
2186 // reliably reapply the tabindex and remove the pressed class
2187 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
2188 // Prevent change of focus unless specifically configured otherwise
2189 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
2195 * Handles mouse up events.
2197 * @param {jQuery.Event} e Mouse up event
2199 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function ( e
) {
2200 if ( this.isDisabled() || e
.which
!== 1 ) {
2204 // Restore the tab-index after the button is up to restore the button's accesssibility
2205 .attr( 'tabindex', this.tabIndex
)
2206 .removeClass( 'oo-ui-buttonedElement-pressed' );
2207 // Stop listening for mouseup, since we only needed this once
2208 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
2214 * @param {boolean} [value] Make button active
2217 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
2218 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
2223 * Element that can be automatically clipped to visible boundaies.
2229 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
2230 * @param {Object} [config] Configuration options
2232 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
2233 // Configuration initialization
2234 config
= config
|| {};
2237 this.$clippable
= $clippable
;
2238 this.clipping
= false;
2239 this.clipped
= false;
2240 this.$clippableContainer
= null;
2241 this.$clippableScroller
= null;
2242 this.$clippableWindow
= null;
2243 this.idealWidth
= null;
2244 this.idealHeight
= null;
2245 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
2246 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
2249 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
2257 * @param {boolean} value Enable clipping
2260 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
2263 if ( this.clipping
!== value
) {
2264 this.clipping
= value
;
2265 if ( this.clipping
) {
2266 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
2267 // If the clippable container is the body, we have to listen to scroll events and check
2268 // jQuery.scrollTop on the window because of browser inconsistencies
2269 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
2270 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
2271 this.$clippableContainer
;
2272 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
2273 this.$clippableWindow
= this.$( this.getElementWindow() )
2274 .on( 'resize', this.onClippableWindowResizeHandler
);
2275 // Initial clip after visible
2276 setTimeout( OO
.ui
.bind( this.clip
, this ) );
2278 this.$clippableContainer
= null;
2279 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
2280 this.$clippableScroller
= null;
2281 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
2282 this.$clippableWindow
= null;
2290 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
2292 * @return {boolean} Element will be clipped to the visible area
2294 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
2295 return this.clipping
;
2299 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
2301 * @return {boolean} Part of the element is being clipped
2303 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
2304 return this.clipped
;
2308 * Set the ideal size.
2310 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
2311 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
2313 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
2314 this.idealWidth
= width
;
2315 this.idealHeight
= height
;
2319 * Clip element to visible boundaries and allow scrolling when needed.
2321 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
2322 * overlapped by, the visible area of the nearest scrollable container.
2326 OO
.ui
.ClippableElement
.prototype.clip = function () {
2327 if ( !this.clipping
) {
2328 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
2333 cOffset
= this.$clippable
.offset(),
2334 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
2335 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
2336 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
2337 scrollTop
= this.$clippableScroller
.scrollTop(),
2338 scrollLeft
= this.$clippableScroller
.scrollLeft(),
2339 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
2340 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
2341 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
2342 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
2343 clipWidth
= desiredWidth
< naturalWidth
,
2344 clipHeight
= desiredHeight
< naturalHeight
;
2347 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
2349 this.$clippable
.css( 'width', this.idealWidth
|| '' );
2350 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
2351 this.$clippable
.css( 'overflow-x', '' );
2354 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
2356 this.$clippable
.css( 'height', this.idealHeight
|| '' );
2357 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
2358 this.$clippable
.css( 'overflow-y', '' );
2361 this.clipped
= clipWidth
|| clipHeight
;
2367 * Element with named flags that can be added, removed, listed and checked.
2369 * A flag, when set, adds a CSS class on the `$element` by combing `oo-ui-flaggableElement-` with
2370 * the flag name. Flags are primarily useful for styling.
2376 * @param {Object} [config] Configuration options
2377 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
2379 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
2380 // Config initialization
2381 config
= config
|| {};
2387 this.setFlags( config
.flags
);
2393 * Check if a flag is set.
2395 * @param {string} flag Name of flag
2396 * @return {boolean} Has flag
2398 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
2399 return flag
in this.flags
;
2403 * Get the names of all flags set.
2405 * @return {string[]} flags Flag names
2407 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
2408 return Object
.keys( this.flags
);
2416 OO
.ui
.FlaggableElement
.prototype.clearFlags = function () {
2418 classPrefix
= 'oo-ui-flaggableElement-';
2420 for ( flag
in this.flags
) {
2421 delete this.flags
[flag
];
2422 this.$element
.removeClass( classPrefix
+ flag
);
2429 * Add one or more flags.
2431 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
2432 * keyed by flag name containing boolean set/remove instructions.
2435 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
2437 classPrefix
= 'oo-ui-flaggableElement-';
2439 if ( typeof flags
=== 'string' ) {
2441 this.flags
[flags
] = true;
2442 this.$element
.addClass( classPrefix
+ flags
);
2443 } else if ( $.isArray( flags
) ) {
2444 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
2447 this.flags
[flag
] = true;
2448 this.$element
.addClass( classPrefix
+ flag
);
2450 } else if ( OO
.isPlainObject( flags
) ) {
2451 for ( flag
in flags
) {
2452 if ( flags
[flag
] ) {
2454 this.flags
[flag
] = true;
2455 this.$element
.addClass( classPrefix
+ flag
);
2458 delete this.flags
[flag
];
2459 this.$element
.removeClass( classPrefix
+ flag
);
2467 * Element containing a sequence of child elements.
2473 * @param {jQuery} $group Container node, assigned to #$group
2474 * @param {Object} [config] Configuration options
2476 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2478 config
= config
|| {};
2481 this.$group
= $group
;
2483 this.aggregateItemEvents
= {};
2491 * @return {OO.ui.Element[]} Items
2493 OO
.ui
.GroupElement
.prototype.getItems = function () {
2494 return this.items
.slice( 0 );
2498 * Add an aggregate item event.
2500 * Aggregated events are listened to on each item and then emitted by the group under a new name,
2501 * and with an additional leading parameter containing the item that emitted the original event.
2502 * Other arguments that were emitted from the original event are passed through.
2504 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
2505 * event, use null value to remove aggregation
2506 * @throws {Error} If aggregation already exists
2508 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
2509 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
2511 for ( itemEvent
in events
) {
2512 groupEvent
= events
[itemEvent
];
2514 // Remove existing aggregated event
2515 if ( itemEvent
in this.aggregateItemEvents
) {
2516 // Don't allow duplicate aggregations
2518 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
2520 // Remove event aggregation from 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 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
2526 item
.disconnect( this, remove
);
2529 // Prevent future items from aggregating event
2530 delete this.aggregateItemEvents
[itemEvent
];
2533 // Add new aggregate event
2535 // Make future items aggregate event
2536 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
2537 // Add event aggregation to existing items
2538 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2539 item
= this.items
[i
];
2540 if ( item
.connect
&& item
.disconnect
) {
2542 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
2543 item
.connect( this, add
);
2553 * @param {OO.ui.Element[]} items Item
2554 * @param {number} [index] Index to insert items at
2557 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2558 var i
, len
, item
, event
, events
, currentIndex
,
2561 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2564 // Check if item exists then remove it first, effectively "moving" it
2565 currentIndex
= $.inArray( item
, this.items
);
2566 if ( currentIndex
>= 0 ) {
2567 this.removeItems( [ item
] );
2568 // Adjust index to compensate for removal
2569 if ( currentIndex
< index
) {
2574 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
2576 for ( event
in this.aggregateItemEvents
) {
2577 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
2579 item
.connect( this, events
);
2581 item
.setElementGroup( this );
2582 itemElements
.push( item
.$element
.get( 0 ) );
2585 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2586 this.$group
.append( itemElements
);
2587 this.items
.push
.apply( this.items
, items
);
2588 } else if ( index
=== 0 ) {
2589 this.$group
.prepend( itemElements
);
2590 this.items
.unshift
.apply( this.items
, items
);
2592 this.items
[index
].$element
.before( itemElements
);
2593 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2602 * Items will be detached, not removed, so they can be used later.
2604 * @param {OO.ui.Element[]} items Items to remove
2607 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2608 var i
, len
, item
, index
, remove
, itemEvent
;
2610 // Remove specific items
2611 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2613 index
= $.inArray( item
, this.items
);
2614 if ( index
!== -1 ) {
2616 item
.connect
&& item
.disconnect
&&
2617 !$.isEmptyObject( this.aggregateItemEvents
)
2620 if ( itemEvent
in this.aggregateItemEvents
) {
2621 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2623 item
.disconnect( this, remove
);
2625 item
.setElementGroup( null );
2626 this.items
.splice( index
, 1 );
2627 item
.$element
.detach();
2637 * Items will be detached, not removed, so they can be used later.
2641 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2642 var i
, len
, item
, remove
, itemEvent
;
2645 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2646 item
= this.items
[i
];
2648 item
.connect
&& item
.disconnect
&&
2649 !$.isEmptyObject( this.aggregateItemEvents
)
2652 if ( itemEvent
in this.aggregateItemEvents
) {
2653 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2655 item
.disconnect( this, remove
);
2657 item
.setElementGroup( null );
2658 item
.$element
.detach();
2666 * Element containing an icon.
2672 * @param {jQuery} $icon Icon node, assigned to #$icon
2673 * @param {Object} [config] Configuration options
2674 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2675 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2678 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2679 // Config intialization
2680 config
= config
|| {};
2687 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2688 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2693 OO
.initClass( OO
.ui
.IconedElement
);
2695 /* Static Properties */
2700 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2702 * For i18n purposes, this property can be an object containing a `default` icon name property and
2703 * additional icon names keyed by language code.
2705 * Example of i18n icon definition:
2706 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2710 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2711 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2714 OO
.ui
.IconedElement
.static.icon
= null;
2721 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
2722 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2726 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2727 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2730 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2732 if ( typeof icon
=== 'string' ) {
2734 if ( icon
.length
) {
2735 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2739 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2747 * @return {string} Icon
2749 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2754 * Element containing an indicator.
2760 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2761 * @param {Object} [config] Configuration options
2762 * @cfg {string} [indicator] Symbolic indicator name
2763 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2765 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2766 // Config intialization
2767 config
= config
|| {};
2770 this.$indicator
= $indicator
;
2771 this.indicator
= null;
2772 this.indicatorLabel
= null;
2775 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2776 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2777 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2782 OO
.initClass( OO
.ui
.IndicatedElement
);
2784 /* Static Properties */
2791 * @property {string|null} Symbolic indicator name or null for no indicator
2793 OO
.ui
.IndicatedElement
.static.indicator
= null;
2800 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2803 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2810 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2813 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2814 if ( this.indicator
) {
2815 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2816 this.indicator
= null;
2818 if ( typeof indicator
=== 'string' ) {
2819 indicator
= indicator
.trim();
2820 if ( indicator
.length
) {
2821 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2822 this.indicator
= indicator
;
2825 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2831 * Set indicator label.
2833 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2834 * for no indicator title
2837 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2838 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2840 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2841 this.$indicator
.attr( 'title', indicatorTitle
);
2843 this.$indicator
.removeAttr( 'title' );
2852 * @return {string} title Symbolic name of indicator
2854 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2855 return this.indicator
;
2859 * Get indicator title.
2861 * @return {string} Indicator title text
2863 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2864 return this.indicatorTitle
;
2868 * Element containing a label.
2874 * @param {jQuery} $label Label node, assigned to #$label
2875 * @param {Object} [config] Configuration options
2876 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2877 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
2879 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2880 // Config intialization
2881 config
= config
|| {};
2884 this.$label
= $label
;
2888 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2889 this.setLabel( config
.label
|| this.constructor.static.label
);
2890 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
2895 OO
.initClass( OO
.ui
.LabeledElement
);
2897 /* Static Properties */
2904 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2907 OO
.ui
.LabeledElement
.static.label
= null;
2914 * An empty string will result in the label being hidden. A string containing only whitespace will
2915 * be converted to a single
2917 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2918 * text; or null for no label
2921 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2924 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2925 if ( typeof label
=== 'string' && label
.length
) {
2926 if ( label
.match( /^\s*$/ ) ) {
2927 // Convert whitespace only string to a single non-breaking space
2928 this.$label
.html( ' ' );
2930 this.$label
.text( label
);
2932 } else if ( label
instanceof jQuery
) {
2933 this.$label
.empty().append( label
);
2935 this.$label
.empty();
2938 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2939 this.$label
.css( 'display', empty
? 'none' : '' );
2947 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2948 * text; or null for no label
2950 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2959 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
2960 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
2961 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
2967 * Popuppable element.
2973 * @param {Object} [config] Configuration options
2974 * @cfg {number} [popupWidth=320] Width of popup
2975 * @cfg {number} [popupHeight] Height of popup
2976 * @cfg {Object} [popup] Configuration to pass to popup
2978 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
2979 // Configuration initialization
2980 config
= $.extend( { 'popupWidth': 320 }, config
);
2983 this.popup
= new OO
.ui
.PopupWidget( $.extend(
2984 { 'align': 'center', 'autoClose': true },
2986 { '$': this.$, '$autoCloseIgnore': this.$element
}
2988 this.popupWidth
= config
.popupWidth
;
2989 this.popupHeight
= config
.popupHeight
;
2997 * @return {OO.ui.PopupWidget} Popup widget
2999 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
3006 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
3007 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
3013 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
3018 * Element with a title.
3024 * @param {jQuery} $label Titled node, assigned to #$titled
3025 * @param {Object} [config] Configuration options
3026 * @cfg {string|Function} [title] Title text or a function that returns text
3028 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
3029 // Config intialization
3030 config
= config
|| {};
3033 this.$titled
= $titled
;
3037 this.setTitle( config
.title
|| this.constructor.static.title
);
3042 OO
.initClass( OO
.ui
.TitledElement
);
3044 /* Static Properties */
3051 * @property {string|Function} Title text or a function that returns text
3053 OO
.ui
.TitledElement
.static.title
= null;
3060 * @param {string|Function|null} title Title text, a function that returns text or null for no title
3063 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
3064 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
3066 if ( typeof title
=== 'string' && title
.length
) {
3067 this.$titled
.attr( 'title', title
);
3069 this.$titled
.removeAttr( 'title' );
3078 * @return {string} Title string
3080 OO
.ui
.TitledElement
.prototype.getTitle = function () {
3085 * Generic toolbar tool.
3089 * @extends OO.ui.Widget
3090 * @mixins OO.ui.IconedElement
3093 * @param {OO.ui.ToolGroup} toolGroup
3094 * @param {Object} [config] Configuration options
3095 * @cfg {string|Function} [title] Title text or a function that returns text
3097 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
3098 // Config intialization
3099 config
= config
|| {};
3101 // Parent constructor
3102 OO
.ui
.Tool
.super.call( this, config
);
3104 // Mixin constructors
3105 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
3108 this.toolGroup
= toolGroup
;
3109 this.toolbar
= this.toolGroup
.getToolbar();
3110 this.active
= false;
3111 this.$title
= this.$( '<span>' );
3112 this.$link
= this.$( '<a>' );
3116 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
3119 this.$title
.addClass( 'oo-ui-tool-title' );
3121 .addClass( 'oo-ui-tool-link' )
3122 .append( this.$icon
, this.$title
)
3123 .prop( 'tabIndex', 0 )
3124 .attr( 'role', 'button' );
3126 .data( 'oo-ui-tool', this )
3128 'oo-ui-tool ' + 'oo-ui-tool-name-' +
3129 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
3131 .append( this.$link
);
3132 this.setTitle( config
.title
|| this.constructor.static.title
);
3137 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
3138 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
3146 /* Static Properties */
3152 OO
.ui
.Tool
.static.tagName
= 'span';
3155 * Symbolic name of tool.
3160 * @property {string}
3162 OO
.ui
.Tool
.static.name
= '';
3170 * @property {string}
3172 OO
.ui
.Tool
.static.group
= '';
3177 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
3178 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
3179 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
3180 * appended to the title if the tool is part of a bar tool group.
3185 * @property {string|Function} Title text or a function that returns text
3187 OO
.ui
.Tool
.static.title
= '';
3190 * Tool can be automatically added to catch-all groups.
3194 * @property {boolean}
3196 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
3199 * Tool can be automatically added to named groups.
3202 * @property {boolean}
3205 OO
.ui
.Tool
.static.autoAddToGroup
= true;
3208 * Check if this tool is compatible with given data.
3212 * @param {Mixed} data Data to check
3213 * @return {boolean} Tool can be used with data
3215 OO
.ui
.Tool
.static.isCompatibleWith = function () {
3222 * Handle the toolbar state being updated.
3224 * This is an abstract method that must be overridden in a concrete subclass.
3228 OO
.ui
.Tool
.prototype.onUpdateState = function () {
3230 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
3235 * Handle the tool being selected.
3237 * This is an abstract method that must be overridden in a concrete subclass.
3241 OO
.ui
.Tool
.prototype.onSelect = function () {
3243 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
3248 * Check if the button is active.
3250 * @param {boolean} Button is active
3252 OO
.ui
.Tool
.prototype.isActive = function () {
3257 * Make the button appear active or inactive.
3259 * @param {boolean} state Make button appear active
3261 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
3262 this.active
= !!state
;
3263 if ( this.active
) {
3264 this.$element
.addClass( 'oo-ui-tool-active' );
3266 this.$element
.removeClass( 'oo-ui-tool-active' );
3271 * Get the tool title.
3273 * @param {string|Function} title Title text or a function that returns text
3276 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
3277 this.title
= OO
.ui
.resolveMsg( title
);
3283 * Get the tool title.
3285 * @return {string} Title text
3287 OO
.ui
.Tool
.prototype.getTitle = function () {
3292 * Get the tool's symbolic name.
3294 * @return {string} Symbolic name of tool
3296 OO
.ui
.Tool
.prototype.getName = function () {
3297 return this.constructor.static.name
;
3303 OO
.ui
.Tool
.prototype.updateTitle = function () {
3304 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
3305 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
3306 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
3313 .addClass( 'oo-ui-tool-accel' )
3317 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
3318 tooltipParts
.push( this.title
);
3320 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
3321 tooltipParts
.push( accel
);
3323 if ( tooltipParts
.length
) {
3324 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
3326 this.$link
.removeAttr( 'title' );
3333 OO
.ui
.Tool
.prototype.destroy = function () {
3334 this.toolbar
.disconnect( this );
3335 this.$element
.remove();
3339 * Collection of tool groups.
3342 * @extends OO.ui.Element
3343 * @mixins OO.EventEmitter
3344 * @mixins OO.ui.GroupElement
3347 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
3348 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
3349 * @param {Object} [config] Configuration options
3350 * @cfg {boolean} [actions] Add an actions section opposite to the tools
3351 * @cfg {boolean} [shadow] Add a shadow below the toolbar
3353 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
3354 // Configuration initialization
3355 config
= config
|| {};
3357 // Parent constructor
3358 OO
.ui
.Toolbar
.super.call( this, config
);
3360 // Mixin constructors
3361 OO
.EventEmitter
.call( this );
3362 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3365 this.toolFactory
= toolFactory
;
3366 this.toolGroupFactory
= toolGroupFactory
;
3369 this.$bar
= this.$( '<div>' );
3370 this.$actions
= this.$( '<div>' );
3371 this.initialized
= false;
3375 .add( this.$bar
).add( this.$group
).add( this.$actions
)
3376 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
3379 this.$group
.addClass( 'oo-ui-toolbar-tools' );
3380 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
3381 if ( config
.actions
) {
3382 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
3383 this.$bar
.append( this.$actions
);
3385 this.$bar
.append( '<div style="clear:both"></div>' );
3386 if ( config
.shadow
) {
3387 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
3389 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
3394 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
3395 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
3396 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
3401 * Get the tool factory.
3403 * @return {OO.ui.ToolFactory} Tool factory
3405 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
3406 return this.toolFactory
;
3410 * Get the tool group factory.
3412 * @return {OO.Factory} Tool group factory
3414 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
3415 return this.toolGroupFactory
;
3419 * Handles mouse down events.
3421 * @param {jQuery.Event} e Mouse down event
3423 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
3424 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
3425 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
3426 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
3432 * Sets up handles and preloads required information for the toolbar to work.
3433 * This must be called immediately after it is attached to a visible document.
3435 OO
.ui
.Toolbar
.prototype.initialize = function () {
3436 this.initialized
= true;
3442 * Tools can be specified in the following ways:
3444 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3445 * - All tools in a group: `{ 'group': 'group-name' }`
3446 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
3448 * @param {Object.<string,Array>} groups List of tool group configurations
3449 * @param {Array|string} [groups.include] Tools to include
3450 * @param {Array|string} [groups.exclude] Tools to exclude
3451 * @param {Array|string} [groups.promote] Tools to promote to the beginning
3452 * @param {Array|string} [groups.demote] Tools to demote to the end
3454 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
3455 var i
, len
, type
, group
,
3457 defaultType
= 'bar';
3459 // Cleanup previous groups
3462 // Build out new groups
3463 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
3465 if ( group
.include
=== '*' ) {
3466 // Apply defaults to catch-all groups
3467 if ( group
.type
=== undefined ) {
3468 group
.type
= 'list';
3470 if ( group
.label
=== undefined ) {
3471 group
.label
= 'ooui-toolbar-more';
3474 // Check type has been registered
3475 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
3477 this.getToolGroupFactory().create( type
, this, $.extend( { '$': this.$ }, group
) )
3480 this.addItems( items
);
3484 * Remove all tools and groups from the toolbar.
3486 OO
.ui
.Toolbar
.prototype.reset = function () {
3491 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3492 this.items
[i
].destroy();
3498 * Destroys toolbar, removing event handlers and DOM elements.
3500 * Call this whenever you are done using a toolbar.
3502 OO
.ui
.Toolbar
.prototype.destroy = function () {
3504 this.$element
.remove();
3508 * Check if tool has not been used yet.
3510 * @param {string} name Symbolic name of tool
3511 * @return {boolean} Tool is available
3513 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
3514 return !this.tools
[name
];
3518 * Prevent tool from being used again.
3520 * @param {OO.ui.Tool} tool Tool to reserve
3522 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
3523 this.tools
[tool
.getName()] = tool
;
3527 * Allow tool to be used again.
3529 * @param {OO.ui.Tool} tool Tool to release
3531 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
3532 delete this.tools
[tool
.getName()];
3536 * Get accelerator label for tool.
3538 * This is a stub that should be overridden to provide access to accelerator information.
3540 * @param {string} name Symbolic name of tool
3541 * @return {string|undefined} Tool accelerator label if available
3543 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
3548 * Factory for tools.
3551 * @extends OO.Factory
3554 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3555 // Parent constructor
3556 OO
.ui
.ToolFactory
.super.call( this );
3561 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3566 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3567 var i
, len
, included
, promoted
, demoted
,
3571 // Collect included and not excluded tools
3572 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3575 promoted
= this.extract( promote
, used
);
3576 demoted
= this.extract( demote
, used
);
3579 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3580 if ( !used
[included
[i
]] ) {
3581 auto
.push( included
[i
] );
3585 return promoted
.concat( auto
).concat( demoted
);
3589 * Get a flat list of names from a list of names or groups.
3591 * Tools can be specified in the following ways:
3593 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3594 * - All tools in a group: `{ 'group': 'group-name' }`
3595 * - All tools: `'*'`
3598 * @param {Array|string} collection List of tools
3599 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3600 * names will be added as properties
3601 * @return {string[]} List of extracted names
3603 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3604 var i
, len
, item
, name
, tool
,
3607 if ( collection
=== '*' ) {
3608 for ( name
in this.registry
) {
3609 tool
= this.registry
[name
];
3611 // Only add tools by group name when auto-add is enabled
3612 tool
.static.autoAddToCatchall
&&
3613 // Exclude already used tools
3614 ( !used
|| !used
[name
] )
3622 } else if ( $.isArray( collection
) ) {
3623 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3624 item
= collection
[i
];
3625 // Allow plain strings as shorthand for named tools
3626 if ( typeof item
=== 'string' ) {
3627 item
= { 'name': item
};
3629 if ( OO
.isPlainObject( item
) ) {
3631 for ( name
in this.registry
) {
3632 tool
= this.registry
[name
];
3634 // Include tools with matching group
3635 tool
.static.group
=== item
.group
&&
3636 // Only add tools by group name when auto-add is enabled
3637 tool
.static.autoAddToGroup
&&
3638 // Exclude already used tools
3639 ( !used
|| !used
[name
] )
3647 // Include tools with matching name and exclude already used tools
3648 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3649 names
.push( item
.name
);
3651 used
[item
.name
] = true;
3661 * Collection of tools.
3663 * Tools can be specified in the following ways:
3665 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3666 * - All tools in a group: `{ 'group': 'group-name' }`
3667 * - All tools: `'*'`
3671 * @extends OO.ui.Widget
3672 * @mixins OO.ui.GroupElement
3675 * @param {OO.ui.Toolbar} toolbar
3676 * @param {Object} [config] Configuration options
3677 * @cfg {Array|string} [include=[]] List of tools to include
3678 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3679 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3680 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3682 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3683 // Configuration initialization
3684 config
= config
|| {};
3686 // Parent constructor
3687 OO
.ui
.ToolGroup
.super.call( this, config
);
3689 // Mixin constructors
3690 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3693 this.toolbar
= toolbar
;
3695 this.pressed
= null;
3696 this.autoDisabled
= false;
3697 this.include
= config
.include
|| [];
3698 this.exclude
= config
.exclude
|| [];
3699 this.promote
= config
.promote
|| [];
3700 this.demote
= config
.demote
|| [];
3701 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3705 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3706 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3707 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3708 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3710 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3711 this.aggregate( { 'disable': 'itemDisable' } );
3712 this.connect( this, { 'itemDisable': 'updateDisabled' } );
3715 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3717 .addClass( 'oo-ui-toolGroup' )
3718 .append( this.$group
);
3724 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3725 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3733 /* Static Properties */
3736 * Show labels in tooltips.
3740 * @property {boolean}
3742 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3745 * Show acceleration labels in tooltips.
3749 * @property {boolean}
3751 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3754 * Automatically disable the toolgroup when all tools are disabled
3758 * @property {boolean}
3760 OO
.ui
.ToolGroup
.static.autoDisable
= true;
3767 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
3768 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
3774 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
3775 var i
, item
, allDisabled
= true;
3777 if ( this.constructor.static.autoDisable
) {
3778 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
3779 item
= this.items
[i
];
3780 if ( !item
.isDisabled() ) {
3781 allDisabled
= false;
3785 this.autoDisabled
= allDisabled
;
3787 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
3791 * Handle mouse down events.
3793 * @param {jQuery.Event} e Mouse down event
3795 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3796 if ( !this.isDisabled() && e
.which
=== 1 ) {
3797 this.pressed
= this.getTargetTool( e
);
3798 if ( this.pressed
) {
3799 this.pressed
.setActive( true );
3800 this.getElementDocument().addEventListener(
3801 'mouseup', this.onCapturedMouseUpHandler
, true
3809 * Handle captured mouse up events.
3811 * @param {Event} e Mouse up event
3813 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3814 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3815 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3816 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3817 this.onMouseUp( e
);
3821 * Handle mouse up events.
3823 * @param {jQuery.Event} e Mouse up event
3825 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3826 var tool
= this.getTargetTool( e
);
3828 if ( !this.isDisabled() && e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3829 this.pressed
.onSelect();
3832 this.pressed
= null;
3837 * Handle mouse over events.
3839 * @param {jQuery.Event} e Mouse over event
3841 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3842 var tool
= this.getTargetTool( e
);
3844 if ( this.pressed
&& this.pressed
=== tool
) {
3845 this.pressed
.setActive( true );
3850 * Handle mouse out events.
3852 * @param {jQuery.Event} e Mouse out event
3854 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3855 var tool
= this.getTargetTool( e
);
3857 if ( this.pressed
&& this.pressed
=== tool
) {
3858 this.pressed
.setActive( false );
3863 * Get the closest tool to a jQuery.Event.
3865 * Only tool links are considered, which prevents other elements in the tool such as popups from
3866 * triggering tool group interactions.
3869 * @param {jQuery.Event} e
3870 * @return {OO.ui.Tool|null} Tool, `null` if none was found
3872 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3874 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3876 if ( $item
.length
) {
3877 tool
= $item
.parent().data( 'oo-ui-tool' );
3880 return tool
&& !tool
.isDisabled() ? tool
: null;
3884 * Handle tool registry register events.
3886 * If a tool is registered after the group is created, we must repopulate the list to account for:
3888 * - a tool being added that may be included
3889 * - a tool already included being overridden
3891 * @param {string} name Symbolic name of tool
3893 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3898 * Get the toolbar this group is in.
3900 * @return {OO.ui.Toolbar} Toolbar of group
3902 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3903 return this.toolbar
;
3907 * Add and remove tools based on configuration.
3909 OO
.ui
.ToolGroup
.prototype.populate = function () {
3910 var i
, len
, name
, tool
,
3911 toolFactory
= this.toolbar
.getToolFactory(),
3915 list
= this.toolbar
.getToolFactory().getTools(
3916 this.include
, this.exclude
, this.promote
, this.demote
3919 // Build a list of needed tools
3920 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3924 toolFactory
.lookup( name
) &&
3925 // Tool is available or is already in this group
3926 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3928 tool
= this.tools
[name
];
3930 // Auto-initialize tools on first use
3931 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3934 this.toolbar
.reserveTool( tool
);
3939 // Remove tools that are no longer needed
3940 for ( name
in this.tools
) {
3941 if ( !names
[name
] ) {
3942 this.tools
[name
].destroy();
3943 this.toolbar
.releaseTool( this.tools
[name
] );
3944 remove
.push( this.tools
[name
] );
3945 delete this.tools
[name
];
3948 if ( remove
.length
) {
3949 this.removeItems( remove
);
3951 // Update emptiness state
3953 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3955 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3957 // Re-add tools (moving existing ones to new locations)
3958 this.addItems( add
);
3959 // Disabled state may depend on items
3960 this.updateDisabled();
3964 * Destroy tool group.
3966 OO
.ui
.ToolGroup
.prototype.destroy = function () {
3970 this.toolbar
.getToolFactory().disconnect( this );
3971 for ( name
in this.tools
) {
3972 this.toolbar
.releaseTool( this.tools
[name
] );
3973 this.tools
[name
].disconnect( this ).destroy();
3974 delete this.tools
[name
];
3976 this.$element
.remove();
3980 * Factory for tool groups.
3983 * @extends OO.Factory
3986 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3987 // Parent constructor
3988 OO
.Factory
.call( this );
3991 defaultClasses
= this.constructor.static.getDefaultClasses();
3993 // Register default toolgroups
3994 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3995 this.register( defaultClasses
[i
] );
4001 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
4003 /* Static Methods */
4006 * Get a default set of classes to be registered on construction
4008 * @return {Function[]} Default classes
4010 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
4013 OO
.ui
.ListToolGroup
,
4019 * Layout made of a fieldset and optional legend.
4021 * Just add OO.ui.FieldLayout items.
4024 * @extends OO.ui.Layout
4025 * @mixins OO.ui.LabeledElement
4026 * @mixins OO.ui.IconedElement
4027 * @mixins OO.ui.GroupElement
4030 * @param {Object} [config] Configuration options
4031 * @cfg {string} [icon] Symbolic icon name
4032 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
4034 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
4035 // Config initialization
4036 config
= config
|| {};
4038 // Parent constructor
4039 OO
.ui
.FieldsetLayout
.super.call( this, config
);
4041 // Mixin constructors
4042 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
4043 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
4044 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4048 .addClass( 'oo-ui-fieldsetLayout' )
4049 .prepend( this.$icon
, this.$label
, this.$group
);
4050 if ( $.isArray( config
.items
) ) {
4051 this.addItems( config
.items
);
4057 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
4058 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
4059 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
4060 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
4062 /* Static Properties */
4064 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
4067 * Layout made of a field and optional label.
4070 * @extends OO.ui.Layout
4071 * @mixins OO.ui.LabeledElement
4073 * Available label alignment modes include:
4074 * - 'left': Label is before the field and aligned away from it, best for when the user will be
4075 * scanning for a specific label in a form with many fields
4076 * - 'right': Label is before the field and aligned toward it, best for forms the user is very
4077 * familiar with and will tab through field checking quickly to verify which field they are in
4078 * - 'top': Label is before the field and above it, best for when the use will need to fill out all
4079 * fields from top to bottom in a form with few fields
4080 * - 'inline': Label is after the field and aligned toward it, best for small boolean fields like
4081 * checkboxes or radio buttons
4084 * @param {OO.ui.Widget} field Field widget
4085 * @param {Object} [config] Configuration options
4086 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
4088 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
4089 // Config initialization
4090 config
= $.extend( { 'align': 'left' }, config
);
4092 // Parent constructor
4093 OO
.ui
.FieldLayout
.super.call( this, config
);
4095 // Mixin constructors
4096 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
4099 this.$field
= this.$( '<div>' );
4104 if ( this.field
instanceof OO
.ui
.InputWidget
) {
4105 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
4107 this.field
.connect( this, { 'disable': 'onFieldDisable' } );
4110 this.$element
.addClass( 'oo-ui-fieldLayout' );
4112 .addClass( 'oo-ui-fieldLayout-field' )
4113 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
4114 .append( this.field
.$element
);
4115 this.setAlignment( config
.align
);
4120 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
4121 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
4126 * Handle field disable events.
4128 * @param {boolean} value Field is disabled
4130 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
4131 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
4135 * Handle label mouse click events.
4137 * @param {jQuery.Event} e Mouse click event
4139 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
4140 this.field
.simulateLabelClick();
4147 * @return {OO.ui.Widget} Field widget
4149 OO
.ui
.FieldLayout
.prototype.getField = function () {
4154 * Set the field alignment mode.
4156 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
4159 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
4160 if ( value
!== this.align
) {
4161 // Default to 'left'
4162 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
4166 if ( value
=== 'inline' ) {
4167 this.$element
.append( this.$field
, this.$label
);
4169 this.$element
.append( this.$label
, this.$field
);
4173 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
4176 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
4183 * Layout made of proportionally sized columns and rows.
4186 * @extends OO.ui.Layout
4189 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
4190 * @param {Object} [config] Configuration options
4191 * @cfg {number[]} [widths] Widths of columns as ratios
4192 * @cfg {number[]} [heights] Heights of columns as ratios
4194 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
4197 // Config initialization
4198 config
= config
|| {};
4200 // Parent constructor
4201 OO
.ui
.GridLayout
.super.call( this, config
);
4209 this.$element
.addClass( 'oo-ui-gridLayout' );
4210 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
4211 this.panels
.push( panels
[i
] );
4212 this.$element
.append( panels
[i
].$element
);
4214 if ( config
.widths
|| config
.heights
) {
4215 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
4217 // Arrange in columns by default
4219 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
4222 this.layout( widths
, [ 1 ] );
4228 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
4240 /* Static Properties */
4242 OO
.ui
.GridLayout
.static.tagName
= 'div';
4247 * Set grid dimensions.
4249 * @param {number[]} widths Widths of columns as ratios
4250 * @param {number[]} heights Heights of rows as ratios
4252 * @throws {Error} If grid is not large enough to fit all panels
4254 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
4258 cols
= widths
.length
,
4259 rows
= heights
.length
;
4261 // Verify grid is big enough to fit panels
4262 if ( cols
* rows
< this.panels
.length
) {
4263 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
4266 // Sum up denominators
4267 for ( x
= 0; x
< cols
; x
++ ) {
4270 for ( y
= 0; y
< rows
; y
++ ) {
4276 for ( x
= 0; x
< cols
; x
++ ) {
4277 this.widths
[x
] = widths
[x
] / xd
;
4279 for ( y
= 0; y
< rows
; y
++ ) {
4280 this.heights
[y
] = heights
[y
] / yd
;
4284 this.emit( 'layout' );
4288 * Update panel positions and sizes.
4292 OO
.ui
.GridLayout
.prototype.update = function () {
4300 cols
= this.widths
.length
,
4301 rows
= this.heights
.length
;
4303 for ( y
= 0; y
< rows
; y
++ ) {
4304 for ( x
= 0; x
< cols
; x
++ ) {
4305 panel
= this.panels
[i
];
4306 width
= this.widths
[x
];
4307 height
= this.heights
[y
];
4309 'width': Math
.round( width
* 100 ) + '%',
4310 'height': Math
.round( height
* 100 ) + '%',
4311 'top': Math
.round( top
* 100 ) + '%'
4314 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
4315 dimensions
.right
= Math
.round( left
* 100 ) + '%';
4317 dimensions
.left
= Math
.round( left
* 100 ) + '%';
4319 panel
.$element
.css( dimensions
);
4327 this.emit( 'update' );
4331 * Get a panel at a given position.
4333 * The x and y position is affected by the current grid layout.
4335 * @param {number} x Horizontal position
4336 * @param {number} y Vertical position
4337 * @return {OO.ui.PanelLayout} The panel at the given postion
4339 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
4340 return this.panels
[( x
* this.widths
.length
) + y
];
4344 * Layout containing a series of pages.
4347 * @extends OO.ui.Layout
4350 * @param {Object} [config] Configuration options
4351 * @cfg {boolean} [continuous=false] Show all pages, one after another
4352 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
4353 * @cfg {boolean} [outlined=false] Show an outline
4354 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
4356 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
4357 // Initialize configuration
4358 config
= config
|| {};
4360 // Parent constructor
4361 OO
.ui
.BookletLayout
.super.call( this, config
);
4364 this.currentPageName
= null;
4366 this.ignoreFocus
= false;
4367 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
4368 this.autoFocus
= config
.autoFocus
=== undefined ? true : !!config
.autoFocus
;
4369 this.outlineVisible
= false;
4370 this.outlined
= !!config
.outlined
;
4371 if ( this.outlined
) {
4372 this.editable
= !!config
.editable
;
4373 this.outlineControlsWidget
= null;
4374 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
4375 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
4376 this.gridLayout
= new OO
.ui
.GridLayout(
4377 [ this.outlinePanel
, this.stackLayout
],
4378 { '$': this.$, 'widths': [ 1, 2 ] }
4380 this.outlineVisible
= true;
4381 if ( this.editable
) {
4382 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
4383 this.outlineWidget
, { '$': this.$ }
4389 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
4390 if ( this.outlined
) {
4391 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
4393 if ( this.autoFocus
) {
4394 // Event 'focus' does not bubble, but 'focusin' does
4395 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
4399 this.$element
.addClass( 'oo-ui-bookletLayout' );
4400 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
4401 if ( this.outlined
) {
4402 this.outlinePanel
.$element
4403 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
4404 .append( this.outlineWidget
.$element
);
4405 if ( this.editable
) {
4406 this.outlinePanel
.$element
4407 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
4408 .append( this.outlineControlsWidget
.$element
);
4410 this.$element
.append( this.gridLayout
.$element
);
4412 this.$element
.append( this.stackLayout
.$element
);
4418 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
4424 * @param {OO.ui.PageLayout} page Current page
4429 * @param {OO.ui.PageLayout[]} page Added pages
4430 * @param {number} index Index pages were added at
4435 * @param {OO.ui.PageLayout[]} pages Removed pages
4441 * Handle stack layout focus.
4443 * @param {jQuery.Event} e Focusin event
4445 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
4448 // Find the page that an element was focused within
4449 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
4450 for ( name
in this.pages
) {
4451 // Check for page match, exclude current page to find only page changes
4452 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
4453 this.setPage( name
);
4460 * Handle stack layout set events.
4462 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
4464 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
4466 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
4467 if ( this.autoFocus
) {
4468 // Set focus to the first input if nothing on the page is focused yet
4469 if ( !page
.$element
.find( ':focus' ).length
) {
4470 page
.$element
.find( ':input:first' ).focus();
4478 * Handle outline widget select events.
4480 * @param {OO.ui.OptionWidget|null} item Selected item
4482 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
4484 this.setPage( item
.getData() );
4489 * Check if booklet has an outline.
4493 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
4494 return this.outlined
;
4498 * Check if booklet has editing controls.
4502 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
4503 return this.editable
;
4507 * Check if booklet has a visible outline.
4511 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
4512 return this.outlined
&& this.outlineVisible
;
4516 * Hide or show the outline.
4518 * @param {boolean} [show] Show outline, omit to invert current state
4521 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
4522 if ( this.outlined
) {
4523 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
4524 this.outlineVisible
= show
;
4525 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
4532 * Get the outline widget.
4534 * @param {OO.ui.PageLayout} page Page to be selected
4535 * @return {OO.ui.PageLayout|null} Closest page to another
4537 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
4538 var next
, prev
, level
,
4539 pages
= this.stackLayout
.getItems(),
4540 index
= $.inArray( page
, pages
);
4542 if ( index
!== -1 ) {
4543 next
= pages
[index
+ 1];
4544 prev
= pages
[index
- 1];
4545 // Prefer adjacent pages at the same level
4546 if ( this.outlined
) {
4547 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
4550 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
4556 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
4562 return prev
|| next
|| null;
4566 * Get the outline widget.
4568 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
4570 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
4571 return this.outlineWidget
;
4575 * Get the outline controls widget. If the outline is not editable, null is returned.
4577 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
4579 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
4580 return this.outlineControlsWidget
;
4584 * Get a page by name.
4586 * @param {string} name Symbolic name of page
4587 * @return {OO.ui.PageLayout|undefined} Page, if found
4589 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
4590 return this.pages
[name
];
4594 * Get the current page name.
4596 * @return {string|null} Current page name
4598 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
4599 return this.currentPageName
;
4603 * Add a page to the layout.
4605 * When pages are added with the same names as existing pages, the existing pages will be
4606 * automatically removed before the new pages are added.
4608 * @param {OO.ui.PageLayout[]} pages Pages to add
4609 * @param {number} index Index to insert pages after
4613 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
4614 var i
, len
, name
, page
, item
, currentIndex
,
4615 stackLayoutPages
= this.stackLayout
.getItems(),
4619 // Remove pages with same names
4620 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4622 name
= page
.getName();
4624 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
4625 // Correct the insertion index
4626 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
4627 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
4630 remove
.push( this.pages
[name
] );
4633 if ( remove
.length
) {
4634 this.removePages( remove
);
4638 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4640 name
= page
.getName();
4641 this.pages
[page
.getName()] = page
;
4642 if ( this.outlined
) {
4643 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { '$': this.$ } );
4644 page
.setOutlineItem( item
);
4649 if ( this.outlined
&& items
.length
) {
4650 this.outlineWidget
.addItems( items
, index
);
4651 this.updateOutlineWidget();
4653 this.stackLayout
.addItems( pages
, index
);
4654 this.emit( 'add', pages
, index
);
4660 * Remove a page from the layout.
4665 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
4666 var i
, len
, name
, page
,
4669 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4671 name
= page
.getName();
4672 delete this.pages
[name
];
4673 if ( this.outlined
) {
4674 items
.push( this.outlineWidget
.getItemFromData( name
) );
4675 page
.setOutlineItem( null );
4678 if ( this.outlined
&& items
.length
) {
4679 this.outlineWidget
.removeItems( items
);
4680 this.updateOutlineWidget();
4682 this.stackLayout
.removeItems( pages
);
4683 this.emit( 'remove', pages
);
4689 * Clear all pages from the layout.
4694 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
4696 pages
= this.stackLayout
.getItems();
4699 this.currentPageName
= null;
4700 if ( this.outlined
) {
4701 this.outlineWidget
.clearItems();
4702 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4703 pages
[i
].setOutlineItem( null );
4706 this.stackLayout
.clearItems();
4708 this.emit( 'remove', pages
);
4714 * Set the current page by name.
4717 * @param {string} name Symbolic name of page
4719 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
4721 page
= this.pages
[name
];
4723 if ( name
!== this.currentPageName
) {
4724 if ( this.outlined
) {
4725 selectedItem
= this.outlineWidget
.getSelectedItem();
4726 if ( selectedItem
&& selectedItem
.getData() !== name
) {
4727 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
4731 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
4732 this.pages
[this.currentPageName
].setActive( false );
4733 // Blur anything focused if the next page doesn't have anything focusable - this
4734 // is not needed if the next page has something focusable because once it is focused
4735 // this blur happens automatically
4736 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
4737 this.pages
[this.currentPageName
].$element
.find( ':focus' ).blur();
4740 this.currentPageName
= name
;
4741 this.stackLayout
.setItem( page
);
4742 page
.setActive( true );
4743 this.emit( 'set', page
);
4749 * Call this after adding or removing items from the OutlineWidget.
4753 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
4754 // Auto-select first item when nothing is selected anymore
4755 if ( !this.outlineWidget
.getSelectedItem() ) {
4756 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
4763 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
4766 * @extends OO.ui.Layout
4769 * @param {Object} [config] Configuration options
4770 * @cfg {boolean} [scrollable] Allow vertical scrolling
4771 * @cfg {boolean} [padded] Pad the content from the edges
4773 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
4774 // Config initialization
4775 config
= config
|| {};
4777 // Parent constructor
4778 OO
.ui
.PanelLayout
.super.call( this, config
);
4781 this.$element
.addClass( 'oo-ui-panelLayout' );
4782 if ( config
.scrollable
) {
4783 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
4786 if ( config
.padded
) {
4787 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
4793 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
4796 * Page within an booklet layout.
4799 * @extends OO.ui.PanelLayout
4802 * @param {string} name Unique symbolic name of page
4803 * @param {Object} [config] Configuration options
4804 * @param {string} [outlineItem] Outline item widget
4806 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
4807 // Configuration initialization
4808 config
= $.extend( { 'scrollable': true }, config
);
4810 // Parent constructor
4811 OO
.ui
.PageLayout
.super.call( this, config
);
4815 this.outlineItem
= config
.outlineItem
|| null;
4816 this.active
= false;
4819 this.$element
.addClass( 'oo-ui-pageLayout' );
4824 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4830 * @param {boolean} active Page is active
4838 * @return {string} Symbolic name of page
4840 OO
.ui
.PageLayout
.prototype.getName = function () {
4845 * Check if page is active.
4847 * @return {boolean} Page is active
4849 OO
.ui
.PageLayout
.prototype.isActive = function () {
4856 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
4858 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
4859 return this.outlineItem
;
4865 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
4868 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
4869 this.outlineItem
= outlineItem
;
4874 * Set page active state.
4876 * @param {boolean} Page is active
4879 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
4882 if ( active
!== this.active
) {
4883 this.active
= active
;
4884 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
4885 this.emit( 'active', this.active
);
4890 * Layout containing a series of mutually exclusive pages.
4893 * @extends OO.ui.PanelLayout
4894 * @mixins OO.ui.GroupElement
4897 * @param {Object} [config] Configuration options
4898 * @cfg {boolean} [continuous=false] Show all pages, one after another
4899 * @cfg {string} [icon=''] Symbolic icon name
4900 * @cfg {OO.ui.Layout[]} [items] Layouts to add
4902 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4903 // Config initialization
4904 config
= $.extend( { 'scrollable': true }, config
);
4906 // Parent constructor
4907 OO
.ui
.StackLayout
.super.call( this, config
);
4909 // Mixin constructors
4910 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4913 this.currentItem
= null;
4914 this.continuous
= !!config
.continuous
;
4917 this.$element
.addClass( 'oo-ui-stackLayout' );
4918 if ( this.continuous
) {
4919 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4921 if ( $.isArray( config
.items
) ) {
4922 this.addItems( config
.items
);
4928 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4929 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4935 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
4941 * Get the current item.
4943 * @return {OO.ui.Layout|null}
4945 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
4946 return this.currentItem
;
4950 * Unset the current item.
4953 * @param {OO.ui.StackLayout} layout
4956 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
4957 var prevItem
= this.currentItem
;
4958 if ( prevItem
=== null ) {
4962 this.currentItem
= null;
4963 this.emit( 'set', null );
4969 * Adding an existing item (by value) will move it.
4971 * @param {OO.ui.Layout[]} items Items to add
4972 * @param {number} [index] Index to insert items after
4975 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
4977 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
4979 if ( !this.currentItem
&& items
.length
) {
4980 this.setItem( items
[0] );
4989 * Items will be detached, not removed, so they can be used later.
4991 * @param {OO.ui.Layout[]} items Items to remove
4995 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
4997 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
4999 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
5000 if ( this.items
.length
) {
5001 this.setItem( this.items
[0] );
5003 this.unsetCurrentItem();
5013 * Items will be detached, not removed, so they can be used later.
5018 OO
.ui
.StackLayout
.prototype.clearItems = function () {
5019 this.unsetCurrentItem();
5020 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
5028 * Any currently shown item will be hidden.
5030 * FIXME: If the passed item to show has not been added in the items list, then
5031 * this method drops it and unsets the current item.
5033 * @param {OO.ui.Layout} item Item to show
5037 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
5040 if ( item
!== this.currentItem
) {
5041 if ( !this.continuous
) {
5042 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5043 this.items
[i
].$element
.css( 'display', '' );
5046 if ( $.inArray( item
, this.items
) !== -1 ) {
5047 if ( !this.continuous
) {
5048 item
.$element
.css( 'display', 'block' );
5050 this.currentItem
= item
;
5051 this.emit( 'set', item
);
5053 this.unsetCurrentItem();
5061 * Horizontal bar layout of tools as icon buttons.
5064 * @extends OO.ui.ToolGroup
5067 * @param {OO.ui.Toolbar} toolbar
5068 * @param {Object} [config] Configuration options
5070 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
5071 // Parent constructor
5072 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
5075 this.$element
.addClass( 'oo-ui-barToolGroup' );
5080 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
5082 /* Static Properties */
5084 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
5086 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
5088 OO
.ui
.BarToolGroup
.static.name
= 'bar';
5091 * Popup list of tools with an icon and optional label.
5095 * @extends OO.ui.ToolGroup
5096 * @mixins OO.ui.IconedElement
5097 * @mixins OO.ui.IndicatedElement
5098 * @mixins OO.ui.LabeledElement
5099 * @mixins OO.ui.TitledElement
5100 * @mixins OO.ui.ClippableElement
5103 * @param {OO.ui.Toolbar} toolbar
5104 * @param {Object} [config] Configuration options
5105 * @cfg {string} [header] Text to display at the top of the pop-up
5107 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
5108 // Configuration initialization
5109 config
= config
|| {};
5111 // Parent constructor
5112 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
5114 // Mixin constructors
5115 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5116 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5117 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5118 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5119 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
5122 this.active
= false;
5123 this.dragging
= false;
5124 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
5125 this.$handle
= this.$( '<span>' );
5129 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
5130 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
5135 .addClass( 'oo-ui-popupToolGroup-handle' )
5136 .append( this.$icon
, this.$label
, this.$indicator
);
5137 // If the pop-up should have a header, add it to the top of the toolGroup.
5138 // Note: If this feature is useful for other widgets, we could abstract it into an
5139 // OO.ui.HeaderedElement mixin constructor.
5140 if ( config
.header
!== undefined ) {
5142 .prepend( this.$( '<span>' )
5143 .addClass( 'oo-ui-popupToolGroup-header' )
5144 .text( config
.header
)
5148 .addClass( 'oo-ui-popupToolGroup' )
5149 .prepend( this.$handle
);
5154 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
5155 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
5156 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
5157 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
5158 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
5159 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
5161 /* Static Properties */
5168 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
5170 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
5172 if ( this.isDisabled() && this.isElementAttached() ) {
5173 this.setActive( false );
5178 * Handle focus being lost.
5180 * The event is actually generated from a mouseup, so it is not a normal blur event object.
5182 * @param {jQuery.Event} e Mouse up event
5184 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
5185 // Only deactivate when clicking outside the dropdown element
5186 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
5187 this.setActive( false );
5194 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
5195 if ( !this.isDisabled() && e
.which
=== 1 ) {
5196 this.setActive( false );
5198 return OO
.ui
.PopupToolGroup
.super.prototype.onMouseUp
.call( this, e
);
5202 * Handle mouse up events.
5204 * @param {jQuery.Event} e Mouse up event
5206 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
5211 * Handle mouse down events.
5213 * @param {jQuery.Event} e Mouse down event
5215 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
5216 if ( !this.isDisabled() && e
.which
=== 1 ) {
5217 this.setActive( !this.active
);
5223 * Switch into active mode.
5225 * When active, mouseup events anywhere in the document will trigger deactivation.
5227 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
5229 if ( this.active
!== value
) {
5230 this.active
= value
;
5232 this.setClipping( true );
5233 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
5234 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
5236 this.setClipping( false );
5237 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
5238 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
5244 * Drop down list layout of tools as labeled icon buttons.
5247 * @extends OO.ui.PopupToolGroup
5250 * @param {OO.ui.Toolbar} toolbar
5251 * @param {Object} [config] Configuration options
5253 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
5254 // Parent constructor
5255 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
5258 this.$element
.addClass( 'oo-ui-listToolGroup' );
5263 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
5265 /* Static Properties */
5267 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
5269 OO
.ui
.ListToolGroup
.static.name
= 'list';
5272 * Drop down menu layout of tools as selectable menu items.
5275 * @extends OO.ui.PopupToolGroup
5278 * @param {OO.ui.Toolbar} toolbar
5279 * @param {Object} [config] Configuration options
5281 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
5282 // Configuration initialization
5283 config
= config
|| {};
5285 // Parent constructor
5286 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
5289 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
5292 this.$element
.addClass( 'oo-ui-menuToolGroup' );
5297 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
5299 /* Static Properties */
5301 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
5303 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
5308 * Handle the toolbar state being updated.
5310 * When the state changes, the title of each active item in the menu will be joined together and
5311 * used as a label for the group. The label will be empty if none of the items are active.
5313 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
5317 for ( name
in this.tools
) {
5318 if ( this.tools
[name
].isActive() ) {
5319 labelTexts
.push( this.tools
[name
].getTitle() );
5323 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
5327 * Tool that shows a popup when selected.
5331 * @extends OO.ui.Tool
5332 * @mixins OO.ui.PopuppableElement
5335 * @param {OO.ui.Toolbar} toolbar
5336 * @param {Object} [config] Configuration options
5338 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
5339 // Parent constructor
5340 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
5342 // Mixin constructors
5343 OO
.ui
.PopuppableElement
.call( this, config
);
5347 .addClass( 'oo-ui-popupTool' )
5348 .append( this.popup
.$element
);
5353 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
5354 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
5359 * Handle the tool being selected.
5363 OO
.ui
.PopupTool
.prototype.onSelect = function () {
5364 if ( !this.isDisabled() ) {
5365 if ( this.popup
.isVisible() ) {
5371 this.setActive( false );
5376 * Handle the toolbar state being updated.
5380 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
5381 this.setActive( false );
5387 * Mixin for OO.ui.Widget subclasses.
5389 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
5393 * @extends OO.ui.GroupElement
5396 * @param {jQuery} $group Container node, assigned to #$group
5397 * @param {Object} [config] Configuration options
5399 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
5400 // Parent constructor
5401 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
5406 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
5411 * Set the disabled state of the widget.
5413 * This will also update the disabled state of child widgets.
5415 * @param {boolean} disabled Disable widget
5418 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
5422 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
5423 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
5425 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
5427 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5428 this.items
[i
].updateDisabled();
5438 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
5445 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
5452 * Check if widget is disabled.
5454 * Checks parent if present, making disabled state inheritable.
5456 * @return {boolean} Widget is disabled
5458 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
5459 return this.disabled
||
5460 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
5464 * Set group element is in.
5466 * @param {OO.ui.GroupElement|null} group Group element, null if none
5469 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
5471 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
5472 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
5474 // Initialize item disabled states
5475 this.updateDisabled();
5484 * @extends OO.ui.Widget
5485 * @mixins OO.ui.IconedElement
5486 * @mixins OO.ui.TitledElement
5489 * @param {Object} [config] Configuration options
5491 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
5492 // Config intialization
5493 config
= config
|| {};
5495 // Parent constructor
5496 OO
.ui
.IconWidget
.super.call( this, config
);
5498 // Mixin constructors
5499 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
5500 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5503 this.$element
.addClass( 'oo-ui-iconWidget' );
5508 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
5509 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
5510 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
5512 /* Static Properties */
5514 OO
.ui
.IconWidget
.static.tagName
= 'span';
5520 * @extends OO.ui.Widget
5521 * @mixins OO.ui.IndicatedElement
5522 * @mixins OO.ui.TitledElement
5525 * @param {Object} [config] Configuration options
5527 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
5528 // Config intialization
5529 config
= config
|| {};
5531 // Parent constructor
5532 OO
.ui
.IndicatorWidget
.super.call( this, config
);
5534 // Mixin constructors
5535 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
5536 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5539 this.$element
.addClass( 'oo-ui-indicatorWidget' );
5544 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
5545 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
5546 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
5548 /* Static Properties */
5550 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
5553 * Container for multiple related buttons.
5555 * Use together with OO.ui.ButtonWidget.
5558 * @extends OO.ui.Widget
5559 * @mixins OO.ui.GroupElement
5562 * @param {Object} [config] Configuration options
5563 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
5565 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
5566 // Parent constructor
5567 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
5569 // Mixin constructors
5570 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
5573 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
5574 if ( $.isArray( config
.items
) ) {
5575 this.addItems( config
.items
);
5581 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
5582 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
5588 * @extends OO.ui.Widget
5589 * @mixins OO.ui.ButtonedElement
5590 * @mixins OO.ui.IconedElement
5591 * @mixins OO.ui.IndicatedElement
5592 * @mixins OO.ui.LabeledElement
5593 * @mixins OO.ui.TitledElement
5594 * @mixins OO.ui.FlaggableElement
5597 * @param {Object} [config] Configuration options
5598 * @cfg {string} [title=''] Title text
5599 * @cfg {string} [href] Hyperlink to visit when clicked
5600 * @cfg {string} [target] Target to open hyperlink in
5602 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
5603 // Configuration initialization
5604 config
= $.extend( { 'target': '_blank' }, config
);
5606 // Parent constructor
5607 OO
.ui
.ButtonWidget
.super.call( this, config
);
5609 // Mixin constructors
5610 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
5611 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5612 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5613 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5614 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
5615 OO
.ui
.FlaggableElement
.call( this, config
);
5618 this.isHyperlink
= typeof config
.href
=== 'string';
5622 'click': OO
.ui
.bind( this.onClick
, this ),
5623 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
5628 .append( this.$icon
, this.$label
, this.$indicator
)
5629 .attr( { 'href': config
.href
, 'target': config
.target
} );
5631 .addClass( 'oo-ui-buttonWidget' )
5632 .append( this.$button
);
5637 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
5638 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
5639 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
5640 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
5641 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
5642 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
5643 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
5654 * Handles mouse click events.
5656 * @param {jQuery.Event} e Mouse click event
5659 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
5660 if ( !this.isDisabled() ) {
5661 this.emit( 'click' );
5662 if ( this.isHyperlink
) {
5670 * Handles keypress events.
5672 * @param {jQuery.Event} e Keypress event
5675 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
5676 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
5678 if ( this.isHyperlink
) {
5690 * @extends OO.ui.Widget
5693 * @param {Object} [config] Configuration options
5694 * @cfg {string} [name=''] HTML input name
5695 * @cfg {string} [value=''] Input value
5696 * @cfg {boolean} [readOnly=false] Prevent changes
5697 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
5699 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
5700 // Config intialization
5701 config
= $.extend( { 'readOnly': false }, config
);
5703 // Parent constructor
5704 OO
.ui
.InputWidget
.super.call( this, config
);
5707 this.$input
= this.getInputElement( config
);
5709 this.readOnly
= false;
5710 this.inputFilter
= config
.inputFilter
;
5713 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
5717 .attr( 'name', config
.name
)
5718 .prop( 'disabled', this.isDisabled() );
5719 this.setReadOnly( config
.readOnly
);
5720 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
5721 this.setValue( config
.value
);
5726 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
5738 * Get input element.
5740 * @param {Object} [config] Configuration options
5741 * @return {jQuery} Input element
5743 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
5744 return this.$( '<input>' );
5748 * Handle potentially value-changing events.
5750 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
5752 OO
.ui
.InputWidget
.prototype.onEdit = function () {
5753 if ( !this.isDisabled() ) {
5754 // Allow the stack to clear so the value will be updated
5755 setTimeout( OO
.ui
.bind( function () {
5756 this.setValue( this.$input
.val() );
5762 * Get the value of the input.
5764 * @return {string} Input value
5766 OO
.ui
.InputWidget
.prototype.getValue = function () {
5771 * Sets the direction of the current input, either RTL or LTR
5773 * @param {boolean} isRTL
5775 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
5777 this.$input
.removeClass( 'oo-ui-ltr' );
5778 this.$input
.addClass( 'oo-ui-rtl' );
5780 this.$input
.removeClass( 'oo-ui-rtl' );
5781 this.$input
.addClass( 'oo-ui-ltr' );
5786 * Set the value of the input.
5788 * @param {string} value New value
5792 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
5793 value
= this.sanitizeValue( value
);
5794 if ( this.value
!== value
) {
5796 this.emit( 'change', this.value
);
5798 // Update the DOM if it has changed. Note that with sanitizeValue, it
5799 // is possible for the DOM value to change without this.value changing.
5800 if ( this.$input
.val() !== this.value
) {
5801 this.$input
.val( this.value
);
5807 * Sanitize incoming value.
5809 * Ensures value is a string, and converts undefined and null to empty strings.
5811 * @param {string} value Original value
5812 * @return {string} Sanitized value
5814 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
5815 if ( value
=== undefined || value
=== null ) {
5817 } else if ( this.inputFilter
) {
5818 return this.inputFilter( String( value
) );
5820 return String( value
);
5825 * Simulate the behavior of clicking on a label bound to this input.
5827 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
5828 if ( !this.isDisabled() ) {
5829 if ( this.$input
.is( ':checkbox,:radio' ) ) {
5830 this.$input
.click();
5831 } else if ( this.$input
.is( ':input' ) ) {
5832 this.$input
.focus();
5838 * Check if the widget is read-only.
5842 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
5843 return this.readOnly
;
5847 * Set the read-only state of the widget.
5849 * This should probably change the widgets's appearance and prevent it from being used.
5851 * @param {boolean} state Make input read-only
5854 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
5855 this.readOnly
= !!state
;
5856 this.$input
.prop( 'readOnly', this.readOnly
);
5863 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
5864 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
5865 if ( this.$input
) {
5866 this.$input
.prop( 'disabled', this.isDisabled() );
5876 OO
.ui
.InputWidget
.prototype.focus = function () {
5877 this.$input
.focus();
5885 * @extends OO.ui.InputWidget
5888 * @param {Object} [config] Configuration options
5890 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
5891 // Parent constructor
5892 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
5895 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
5900 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
5907 * Get input element.
5909 * @return {jQuery} Input element
5911 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
5912 return this.$( '<input type="checkbox" />' );
5916 * Get checked state of the checkbox
5918 * @return {boolean} If the checkbox is checked
5920 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
5927 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
5929 if ( this.value
!== value
) {
5931 this.$input
.prop( 'checked', this.value
);
5932 this.emit( 'change', this.value
);
5939 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
5940 if ( !this.isDisabled() ) {
5941 // Allow the stack to clear so the value will be updated
5942 setTimeout( OO
.ui
.bind( function () {
5943 this.setValue( this.$input
.prop( 'checked' ) );
5952 * @extends OO.ui.Widget
5953 * @mixins OO.ui.LabeledElement
5956 * @param {Object} [config] Configuration options
5958 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
5959 // Config intialization
5960 config
= config
|| {};
5962 // Parent constructor
5963 OO
.ui
.LabelWidget
.super.call( this, config
);
5965 // Mixin constructors
5966 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
5969 this.input
= config
.input
;
5972 if ( this.input
instanceof OO
.ui
.InputWidget
) {
5973 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
5977 this.$element
.addClass( 'oo-ui-labelWidget' );
5982 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
5983 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
5985 /* Static Properties */
5987 OO
.ui
.LabelWidget
.static.tagName
= 'label';
5992 * Handles label mouse click events.
5994 * @param {jQuery.Event} e Mouse click event
5996 OO
.ui
.LabelWidget
.prototype.onClick = function () {
5997 this.input
.simulateLabelClick();
6002 * Lookup input widget.
6004 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
6005 * and `choose` events on #lookupMenu to make use of selections.
6011 * @param {OO.ui.TextInputWidget} input Input widget
6012 * @param {Object} [config] Configuration options
6013 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
6015 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
6016 // Config intialization
6017 config
= config
|| {};
6020 this.lookupInput
= input
;
6021 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
6022 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
6023 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
6024 'input': this.lookupInput
,
6025 '$container': config
.$container
6027 this.lookupCache
= {};
6028 this.lookupQuery
= null;
6029 this.lookupRequest
= null;
6030 this.populating
= false;
6033 this.$overlay
.append( this.lookupMenu
.$element
);
6035 this.lookupInput
.$input
.on( {
6036 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
6037 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
6038 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
6040 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
6043 this.$element
.addClass( 'oo-ui-lookupWidget' );
6044 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
6050 * Handle input focus event.
6052 * @param {jQuery.Event} e Input focus event
6054 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
6055 this.openLookupMenu();
6059 * Handle input blur event.
6061 * @param {jQuery.Event} e Input blur event
6063 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
6064 this.lookupMenu
.hide();
6068 * Handle input mouse down event.
6070 * @param {jQuery.Event} e Input mouse down event
6072 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
6073 this.openLookupMenu();
6077 * Handle input change event.
6079 * @param {string} value New input value
6081 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
6082 this.openLookupMenu();
6088 * @return {OO.ui.TextInputMenuWidget}
6090 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
6091 return this.lookupMenu
;
6099 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
6100 var value
= this.lookupInput
.getValue();
6102 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
6103 this.populateLookupMenu();
6104 if ( !this.lookupMenu
.isVisible() ) {
6105 this.lookupMenu
.show();
6108 this.lookupMenu
.clearItems();
6109 this.lookupMenu
.hide();
6116 * Populate lookup menu with current information.
6120 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
6121 if ( !this.populating
) {
6122 this.populating
= true;
6123 this.getLookupMenuItems()
6124 .done( OO
.ui
.bind( function ( items
) {
6125 this.lookupMenu
.clearItems();
6126 if ( items
.length
) {
6127 this.lookupMenu
.show();
6128 this.lookupMenu
.addItems( items
);
6129 this.initializeLookupMenuSelection();
6130 this.openLookupMenu();
6132 this.lookupMenu
.hide();
6134 this.populating
= false;
6136 .fail( OO
.ui
.bind( function () {
6137 this.lookupMenu
.clearItems();
6138 this.populating
= false;
6146 * Set selection in the lookup menu with current information.
6150 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
6151 if ( !this.lookupMenu
.getSelectedItem() ) {
6152 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
6154 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
6158 * Get lookup menu items for the current query.
6160 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
6163 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
6164 var value
= this.lookupInput
.getValue(),
6165 deferred
= $.Deferred();
6167 if ( value
&& value
!== this.lookupQuery
) {
6168 // Abort current request if query has changed
6169 if ( this.lookupRequest
) {
6170 this.lookupRequest
.abort();
6171 this.lookupQuery
= null;
6172 this.lookupRequest
= null;
6174 if ( value
in this.lookupCache
) {
6175 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6177 this.lookupQuery
= value
;
6178 this.lookupRequest
= this.getLookupRequest()
6179 .always( OO
.ui
.bind( function () {
6180 this.lookupQuery
= null;
6181 this.lookupRequest
= null;
6183 .done( OO
.ui
.bind( function ( data
) {
6184 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
6185 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6187 .fail( function () {
6191 this.lookupRequest
.always( OO
.ui
.bind( function () {
6196 return deferred
.promise();
6200 * Get a new request object of the current lookup query value.
6203 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
6205 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
6206 // Stub, implemented in subclass
6211 * Handle successful lookup request.
6213 * Overriding methods should call #populateLookupMenu when results are available and cache results
6214 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
6217 * @param {Mixed} data Response from server
6219 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
6220 // Stub, implemented in subclass
6224 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
6227 * @param {Mixed} data Cached result data, usually an array
6228 * @return {OO.ui.MenuItemWidget[]} Menu items
6230 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
6231 // Stub, implemented in subclass
6238 * Use with OO.ui.SelectWidget.
6241 * @extends OO.ui.Widget
6242 * @mixins OO.ui.IconedElement
6243 * @mixins OO.ui.LabeledElement
6244 * @mixins OO.ui.IndicatedElement
6245 * @mixins OO.ui.FlaggableElement
6248 * @param {Mixed} data Option data
6249 * @param {Object} [config] Configuration options
6250 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
6252 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
6253 // Config intialization
6254 config
= config
|| {};
6256 // Parent constructor
6257 OO
.ui
.OptionWidget
.super.call( this, config
);
6259 // Mixin constructors
6260 OO
.ui
.ItemWidget
.call( this );
6261 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6262 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6263 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6264 OO
.ui
.FlaggableElement
.call( this, config
);
6268 this.selected
= false;
6269 this.highlighted
= false;
6270 this.pressed
= false;
6274 .data( 'oo-ui-optionWidget', this )
6275 .attr( 'rel', config
.rel
)
6276 .addClass( 'oo-ui-optionWidget' )
6277 .append( this.$label
);
6279 .prepend( this.$icon
)
6280 .append( this.$indicator
);
6285 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
6286 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
6287 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
6288 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
6289 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
6290 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
6292 /* Static Properties */
6294 OO
.ui
.OptionWidget
.static.tagName
= 'li';
6296 OO
.ui
.OptionWidget
.static.selectable
= true;
6298 OO
.ui
.OptionWidget
.static.highlightable
= true;
6300 OO
.ui
.OptionWidget
.static.pressable
= true;
6302 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
6307 * Check if option can be selected.
6309 * @return {boolean} Item is selectable
6311 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
6312 return this.constructor.static.selectable
&& !this.isDisabled();
6316 * Check if option can be highlighted.
6318 * @return {boolean} Item is highlightable
6320 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
6321 return this.constructor.static.highlightable
&& !this.isDisabled();
6325 * Check if option can be pressed.
6327 * @return {boolean} Item is pressable
6329 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
6330 return this.constructor.static.pressable
&& !this.isDisabled();
6334 * Check if option is selected.
6336 * @return {boolean} Item is selected
6338 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
6339 return this.selected
;
6343 * Check if option is highlighted.
6345 * @return {boolean} Item is highlighted
6347 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
6348 return this.highlighted
;
6352 * Check if option is pressed.
6354 * @return {boolean} Item is pressed
6356 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
6357 return this.pressed
;
6361 * Set selected state.
6363 * @param {boolean} [state=false] Select option
6366 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
6367 if ( this.constructor.static.selectable
) {
6368 this.selected
= !!state
;
6369 if ( this.selected
) {
6370 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
6371 if ( this.constructor.static.scrollIntoViewOnSelect
) {
6372 this.scrollElementIntoView();
6375 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
6382 * Set highlighted state.
6384 * @param {boolean} [state=false] Highlight option
6387 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
6388 if ( this.constructor.static.highlightable
) {
6389 this.highlighted
= !!state
;
6390 if ( this.highlighted
) {
6391 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
6393 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
6400 * Set pressed state.
6402 * @param {boolean} [state=false] Press option
6405 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
6406 if ( this.constructor.static.pressable
) {
6407 this.pressed
= !!state
;
6408 if ( this.pressed
) {
6409 this.$element
.addClass( 'oo-ui-optionWidget-pressed' );
6411 this.$element
.removeClass( 'oo-ui-optionWidget-pressed' );
6418 * Make the option's highlight flash.
6420 * While flashing, the visual style of the pressed state is removed if present.
6422 * @return {jQuery.Promise} Promise resolved when flashing is done
6424 OO
.ui
.OptionWidget
.prototype.flash = function () {
6425 var $this = this.$element
,
6426 deferred
= $.Deferred();
6428 if ( !this.isDisabled() && this.constructor.static.pressable
) {
6429 $this.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
6430 setTimeout( OO
.ui
.bind( function () {
6431 // Restore original classes
6433 .toggleClass( 'oo-ui-optionWidget-highlighted', this.highlighted
)
6434 .toggleClass( 'oo-ui-optionWidget-pressed', this.pressed
);
6435 setTimeout( function () {
6441 return deferred
.promise();
6447 * @return {Mixed} Option data
6449 OO
.ui
.OptionWidget
.prototype.getData = function () {
6454 * Selection of options.
6456 * Use together with OO.ui.OptionWidget.
6459 * @extends OO.ui.Widget
6460 * @mixins OO.ui.GroupElement
6463 * @param {Object} [config] Configuration options
6464 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
6466 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
6467 // Config intialization
6468 config
= config
|| {};
6470 // Parent constructor
6471 OO
.ui
.SelectWidget
.super.call( this, config
);
6473 // Mixin constructors
6474 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
6477 this.pressed
= false;
6478 this.selecting
= null;
6480 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
6481 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
6485 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
6486 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
6487 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
6491 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
6492 if ( $.isArray( config
.items
) ) {
6493 this.addItems( config
.items
);
6499 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
6501 // Need to mixin base class as well
6502 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
6503 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
6509 * @param {OO.ui.OptionWidget|null} item Highlighted item
6514 * @param {OO.ui.OptionWidget|null} item Pressed item
6519 * @param {OO.ui.OptionWidget|null} item Selected item
6524 * @param {OO.ui.OptionWidget|null} item Chosen item
6529 * @param {OO.ui.OptionWidget[]} items Added items
6530 * @param {number} index Index items were added at
6535 * @param {OO.ui.OptionWidget[]} items Removed items
6538 /* Static Properties */
6540 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
6545 * Handle mouse down events.
6548 * @param {jQuery.Event} e Mouse down event
6550 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
6553 if ( !this.isDisabled() && e
.which
=== 1 ) {
6554 this.togglePressed( true );
6555 item
= this.getTargetItem( e
);
6556 if ( item
&& item
.isSelectable() ) {
6557 this.pressItem( item
);
6558 this.selecting
= item
;
6559 this.getElementDocument().addEventListener(
6560 'mouseup', this.onMouseUpHandler
, true
6562 this.getElementDocument().addEventListener(
6563 'mousemove', this.onMouseMoveHandler
, true
6571 * Handle mouse up events.
6574 * @param {jQuery.Event} e Mouse up event
6576 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
6579 this.togglePressed( false );
6580 if ( !this.selecting
) {
6581 item
= this.getTargetItem( e
);
6582 if ( item
&& item
.isSelectable() ) {
6583 this.selecting
= item
;
6586 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
6587 this.pressItem( null );
6588 this.chooseItem( this.selecting
);
6589 this.selecting
= null;
6592 this.getElementDocument().removeEventListener(
6593 'mouseup', this.onMouseUpHandler
, true
6595 this.getElementDocument().removeEventListener(
6596 'mousemove', this.onMouseMoveHandler
, true
6603 * Handle mouse move events.
6606 * @param {jQuery.Event} e Mouse move event
6608 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
6611 if ( !this.isDisabled() && this.pressed
) {
6612 item
= this.getTargetItem( e
);
6613 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
6614 this.pressItem( item
);
6615 this.selecting
= item
;
6622 * Handle mouse over events.
6625 * @param {jQuery.Event} e Mouse over event
6627 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
6630 if ( !this.isDisabled() ) {
6631 item
= this.getTargetItem( e
);
6632 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
6638 * Handle mouse leave events.
6641 * @param {jQuery.Event} e Mouse over event
6643 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
6644 if ( !this.isDisabled() ) {
6645 this.highlightItem( null );
6651 * Get the closest item to a jQuery.Event.
6654 * @param {jQuery.Event} e
6655 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
6657 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
6658 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
6659 if ( $item
.length
) {
6660 return $item
.data( 'oo-ui-optionWidget' );
6666 * Get selected item.
6668 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
6670 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
6673 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6674 if ( this.items
[i
].isSelected() ) {
6675 return this.items
[i
];
6682 * Get highlighted item.
6684 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
6686 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
6689 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6690 if ( this.items
[i
].isHighlighted() ) {
6691 return this.items
[i
];
6698 * Get an existing item with equivilant data.
6700 * @param {Object} data Item data to search for
6701 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
6703 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
6704 var hash
= OO
.getHash( data
);
6706 if ( hash
in this.hashes
) {
6707 return this.hashes
[hash
];
6714 * Toggle pressed state.
6716 * @param {boolean} pressed An option is being pressed
6718 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
6719 if ( pressed
=== undefined ) {
6720 pressed
= !this.pressed
;
6722 if ( pressed
!== this.pressed
) {
6723 this.$element
.toggleClass( 'oo-ui-selectWidget-pressed', pressed
);
6724 this.$element
.toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
6725 this.pressed
= pressed
;
6730 * Highlight an item.
6732 * Highlighting is mutually exclusive.
6734 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
6738 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
6739 var i
, len
, highlighted
,
6742 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6743 highlighted
= this.items
[i
] === item
;
6744 if ( this.items
[i
].isHighlighted() !== highlighted
) {
6745 this.items
[i
].setHighlighted( highlighted
);
6750 this.emit( 'highlight', item
);
6759 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6763 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
6764 var i
, len
, selected
,
6767 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6768 selected
= this.items
[i
] === item
;
6769 if ( this.items
[i
].isSelected() !== selected
) {
6770 this.items
[i
].setSelected( selected
);
6775 this.emit( 'select', item
);
6784 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
6788 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
6789 var i
, len
, pressed
,
6792 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6793 pressed
= this.items
[i
] === item
;
6794 if ( this.items
[i
].isPressed() !== pressed
) {
6795 this.items
[i
].setPressed( pressed
);
6800 this.emit( 'press', item
);
6809 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
6810 * an item is selected using the keyboard or mouse.
6812 * @param {OO.ui.OptionWidget} item Item to choose
6816 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
6817 this.selectItem( item
);
6818 this.emit( 'choose', item
);
6824 * Get an item relative to another one.
6826 * @param {OO.ui.OptionWidget} item Item to start at
6827 * @param {number} direction Direction to move in
6828 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
6830 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
6831 var inc
= direction
> 0 ? 1 : -1,
6832 len
= this.items
.length
,
6833 index
= item
instanceof OO
.ui
.OptionWidget
?
6834 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
6835 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
6837 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
6838 Math
.max( index
, -1 ) :
6839 // Default to n-1 instead of -1, if nothing is selected let's start at the end
6840 Math
.min( index
, len
);
6843 i
= ( i
+ inc
+ len
) % len
;
6844 item
= this.items
[i
];
6845 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6848 // Stop iterating when we've looped all the way around
6849 if ( i
=== stopAt
) {
6857 * Get the next selectable item.
6859 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
6861 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
6864 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6865 item
= this.items
[i
];
6866 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6877 * When items are added with the same values as existing items, the existing items will be
6878 * automatically removed before the new items are added.
6880 * @param {OO.ui.OptionWidget[]} items Items to add
6881 * @param {number} [index] Index to insert items after
6885 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
6886 var i
, len
, item
, hash
,
6889 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6891 hash
= OO
.getHash( item
.getData() );
6892 if ( hash
in this.hashes
) {
6893 // Remove item with same value
6894 remove
.push( this.hashes
[hash
] );
6896 this.hashes
[hash
] = item
;
6898 if ( remove
.length
) {
6899 this.removeItems( remove
);
6903 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
6905 // Always provide an index, even if it was omitted
6906 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
6914 * Items will be detached, not removed, so they can be used later.
6916 * @param {OO.ui.OptionWidget[]} items Items to remove
6920 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
6921 var i
, len
, item
, hash
;
6923 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6925 hash
= OO
.getHash( item
.getData() );
6926 if ( hash
in this.hashes
) {
6927 // Remove existing item
6928 delete this.hashes
[hash
];
6930 if ( item
.isSelected() ) {
6931 this.selectItem( null );
6936 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
6938 this.emit( 'remove', items
);
6946 * Items will be detached, not removed, so they can be used later.
6951 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
6952 var items
= this.items
.slice();
6957 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
6958 this.selectItem( null );
6960 this.emit( 'remove', items
);
6968 * Use with OO.ui.MenuWidget.
6971 * @extends OO.ui.OptionWidget
6974 * @param {Mixed} data Item data
6975 * @param {Object} [config] Configuration options
6977 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
6978 // Configuration initialization
6979 config
= $.extend( { 'icon': 'check' }, config
);
6981 // Parent constructor
6982 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
6985 this.$element
.addClass( 'oo-ui-menuItemWidget' );
6990 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
6995 * Use together with OO.ui.MenuItemWidget.
6998 * @extends OO.ui.SelectWidget
6999 * @mixins OO.ui.ClippableElement
7002 * @param {Object} [config] Configuration options
7003 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
7004 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
7006 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
7007 // Config intialization
7008 config
= config
|| {};
7010 // Parent constructor
7011 OO
.ui
.MenuWidget
.super.call( this, config
);
7013 // Mixin constructors
7014 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
7017 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
7018 this.newItems
= null;
7019 this.$input
= config
.input
? config
.input
.$input
: null;
7020 this.$previousFocus
= null;
7021 this.isolated
= !config
.input
;
7022 this.visible
= false;
7023 this.flashing
= false;
7024 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
7025 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
7028 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
7033 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
7034 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
7039 * Handles document mouse down events.
7041 * @param {jQuery.Event} e Key down event
7043 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
7044 if ( !$.contains( this.$element
[0], e
.target
) ) {
7050 * Handles key down events.
7052 * @param {jQuery.Event} e Key down event
7054 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
7057 highlightItem
= this.getHighlightedItem();
7059 if ( !this.isDisabled() && this.visible
) {
7060 if ( !highlightItem
) {
7061 highlightItem
= this.getSelectedItem();
7063 switch ( e
.keyCode
) {
7064 case OO
.ui
.Keys
.ENTER
:
7065 this.chooseItem( highlightItem
);
7069 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
7072 case OO
.ui
.Keys
.DOWN
:
7073 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
7076 case OO
.ui
.Keys
.ESCAPE
:
7077 if ( highlightItem
) {
7078 highlightItem
.setHighlighted( false );
7086 this.highlightItem( nextItem
);
7087 nextItem
.scrollElementIntoView();
7092 e
.stopPropagation();
7099 * Check if the menu is visible.
7101 * @return {boolean} Menu is visible
7103 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
7104 return this.visible
;
7108 * Bind key down listener.
7110 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
7111 if ( this.$input
) {
7112 this.$input
.on( 'keydown', this.onKeyDownHandler
);
7114 // Capture menu navigation keys
7115 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
7120 * Unbind key down listener.
7122 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
7123 if ( this.$input
) {
7124 this.$input
.off( 'keydown' );
7126 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
7133 * This will close the menu when done, unlike selectItem which only changes selection.
7135 * @param {OO.ui.OptionWidget} item Item to choose
7138 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
7140 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
7142 if ( item
&& !this.flashing
) {
7143 this.flashing
= true;
7144 item
.flash().done( OO
.ui
.bind( function () {
7146 this.flashing
= false;
7158 * Adding an existing item (by value) will move it.
7160 * @param {OO.ui.MenuItemWidget[]} items Items to add
7161 * @param {number} [index] Index to insert items after
7164 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
7168 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
7171 if ( !this.newItems
) {
7175 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
7177 if ( this.visible
) {
7178 // Defer fitting label until
7181 this.newItems
.push( item
);
7193 OO
.ui
.MenuWidget
.prototype.show = function () {
7196 if ( this.items
.length
) {
7197 this.$element
.show();
7198 this.visible
= true;
7199 this.bindKeyDownListener();
7201 // Change focus to enable keyboard navigation
7202 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
7203 this.$previousFocus
= this.$( ':focus' );
7204 this.$input
.focus();
7206 if ( this.newItems
&& this.newItems
.length
) {
7207 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
7208 this.newItems
[i
].fitLabel();
7210 this.newItems
= null;
7213 this.setClipping( true );
7216 if ( this.autoHide
) {
7217 this.getElementDocument().addEventListener(
7218 'mousedown', this.onDocumentMouseDownHandler
, true
7231 OO
.ui
.MenuWidget
.prototype.hide = function () {
7232 this.$element
.hide();
7233 this.visible
= false;
7234 this.unbindKeyDownListener();
7236 if ( this.isolated
&& this.$previousFocus
) {
7237 this.$previousFocus
.focus();
7238 this.$previousFocus
= null;
7241 this.getElementDocument().removeEventListener(
7242 'mousedown', this.onDocumentMouseDownHandler
, true
7245 this.setClipping( false );
7251 * Inline menu of options.
7253 * Use with OO.ui.MenuOptionWidget.
7256 * @extends OO.ui.Widget
7257 * @mixins OO.ui.IconedElement
7258 * @mixins OO.ui.IndicatedElement
7259 * @mixins OO.ui.LabeledElement
7260 * @mixins OO.ui.TitledElement
7263 * @param {Object} [config] Configuration options
7264 * @cfg {Object} [menu] Configuration options to pass to menu widget
7266 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
7267 // Configuration initialization
7268 config
= $.extend( { 'indicator': 'down' }, config
);
7270 // Parent constructor
7271 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
7273 // Mixin constructors
7274 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
7275 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
7276 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
7277 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
7280 this.menu
= new OO
.ui
.MenuWidget( $.extend( { '$': this.$ }, config
.menu
) );
7281 this.$handle
= this.$( '<span>' );
7284 this.$element
.on( { 'click': OO
.ui
.bind( this.onClick
, this ) } );
7285 this.menu
.connect( this, { 'select': 'onMenuSelect' } );
7289 .addClass( 'oo-ui-inlineMenuWidget-handle' )
7290 .append( this.$icon
, this.$label
, this.$indicator
);
7292 .addClass( 'oo-ui-inlineMenuWidget' )
7293 .append( this.$handle
, this.menu
.$element
);
7298 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
7299 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
7300 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
7301 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
7302 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
7309 * @return {OO.ui.MenuWidget} Menu of widget
7311 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
7316 * Handles menu select events.
7318 * @param {OO.ui.MenuItemWidget} item Selected menu item
7320 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
7327 selectedLabel
= item
.getLabel();
7329 // If the label is a DOM element, clone it, because setLabel will append() it
7330 if ( selectedLabel
instanceof jQuery
) {
7331 selectedLabel
= selectedLabel
.clone();
7334 this.setLabel( selectedLabel
);
7338 * Handles mouse click events.
7340 * @param {jQuery.Event} e Mouse click event
7342 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
7343 // Skip clicks within the menu
7344 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
7348 if ( !this.isDisabled() ) {
7349 if ( this.menu
.isVisible() ) {
7359 * Menu section item widget.
7361 * Use with OO.ui.MenuWidget.
7364 * @extends OO.ui.OptionWidget
7367 * @param {Mixed} data Item data
7368 * @param {Object} [config] Configuration options
7370 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
7371 // Parent constructor
7372 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
7375 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
7380 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
7382 /* Static Properties */
7384 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
7386 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
7389 * Create an OO.ui.OutlineWidget object.
7391 * Use with OO.ui.OutlineItemWidget.
7394 * @extends OO.ui.SelectWidget
7397 * @param {Object} [config] Configuration options
7399 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
7400 // Config intialization
7401 config
= config
|| {};
7403 // Parent constructor
7404 OO
.ui
.OutlineWidget
.super.call( this, config
);
7407 this.$element
.addClass( 'oo-ui-outlineWidget' );
7412 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
7415 * Creates an OO.ui.OutlineControlsWidget object.
7417 * Use together with OO.ui.OutlineWidget.js
7422 * @param {OO.ui.OutlineWidget} outline Outline to control
7423 * @param {Object} [config] Configuration options
7425 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7426 // Configuration initialization
7427 config
= $.extend( { 'icon': 'add-item' }, config
);
7429 // Parent constructor
7430 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7432 // Mixin constructors
7433 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
7434 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
7437 this.outline
= outline
;
7438 this.$movers
= this.$( '<div>' );
7439 this.upButton
= new OO
.ui
.ButtonWidget( {
7443 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
7445 this.downButton
= new OO
.ui
.ButtonWidget( {
7449 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
7451 this.removeButton
= new OO
.ui
.ButtonWidget( {
7455 'title': OO
.ui
.msg( 'ooui-outline-control-remove' )
7459 outline
.connect( this, {
7460 'select': 'onOutlineChange',
7461 'add': 'onOutlineChange',
7462 'remove': 'onOutlineChange'
7464 this.upButton
.connect( this, { 'click': [ 'emit', 'move', -1 ] } );
7465 this.downButton
.connect( this, { 'click': [ 'emit', 'move', 1 ] } );
7466 this.removeButton
.connect( this, { 'click': [ 'emit', 'remove' ] } );
7469 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7470 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7472 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7473 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7474 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7479 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7480 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7481 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7487 * @param {number} places Number of places to move
7497 * Handle outline change events.
7499 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7500 var i
, len
, firstMovable
, lastMovable
,
7501 items
= this.outline
.getItems(),
7502 selectedItem
= this.outline
.getSelectedItem(),
7503 movable
= selectedItem
&& selectedItem
.isMovable(),
7504 removable
= selectedItem
&& selectedItem
.isRemovable();
7509 while ( ++i
< len
) {
7510 if ( items
[i
].isMovable() ) {
7511 firstMovable
= items
[i
];
7517 if ( items
[i
].isMovable() ) {
7518 lastMovable
= items
[i
];
7523 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7524 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7525 this.removeButton
.setDisabled( !removable
);
7529 * Creates an OO.ui.OutlineItemWidget object.
7531 * Use with OO.ui.OutlineWidget.
7534 * @extends OO.ui.OptionWidget
7537 * @param {Mixed} data Item data
7538 * @param {Object} [config] Configuration options
7539 * @cfg {number} [level] Indentation level
7540 * @cfg {boolean} [movable] Allow modification from outline controls
7542 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
7543 // Config intialization
7544 config
= config
|| {};
7546 // Parent constructor
7547 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
7551 this.movable
= !!config
.movable
;
7552 this.removable
= !!config
.removable
;
7555 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
7556 this.setLevel( config
.level
);
7561 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
7563 /* Static Properties */
7565 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
7567 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
7569 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
7571 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
7576 * Check if item is movable.
7578 * Movablilty is used by outline controls.
7580 * @return {boolean} Item is movable
7582 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
7583 return this.movable
;
7587 * Check if item is removable.
7589 * Removablilty is used by outline controls.
7591 * @return {boolean} Item is removable
7593 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
7594 return this.removable
;
7598 * Get indentation level.
7600 * @return {number} Indentation level
7602 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
7609 * Movablilty is used by outline controls.
7611 * @param {boolean} movable Item is movable
7614 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
7615 this.movable
= !!movable
;
7622 * Removablilty is used by outline controls.
7624 * @param {boolean} movable Item is removable
7627 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
7628 this.removable
= !!removable
;
7633 * Set indentation level.
7635 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
7638 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
7639 var levels
= this.constructor.static.levels
,
7640 levelClass
= this.constructor.static.levelClass
,
7643 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
7645 if ( this.level
=== i
) {
7646 this.$element
.addClass( levelClass
+ i
);
7648 this.$element
.removeClass( levelClass
+ i
);
7656 * Option widget that looks like a button.
7658 * Use together with OO.ui.ButtonSelectWidget.
7661 * @extends OO.ui.OptionWidget
7662 * @mixins OO.ui.ButtonedElement
7663 * @mixins OO.ui.FlaggableElement
7666 * @param {Mixed} data Option data
7667 * @param {Object} [config] Configuration options
7669 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
7670 // Parent constructor
7671 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
7673 // Mixin constructors
7674 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7675 OO
.ui
.FlaggableElement
.call( this, config
);
7678 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
7679 this.$button
.append( this.$element
.contents() );
7680 this.$element
.append( this.$button
);
7685 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
7686 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
7687 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
7689 /* Static Properties */
7691 // Allow button mouse down events to pass through so they can be handled by the parent select widget
7692 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
7699 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
7700 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
7702 if ( this.constructor.static.selectable
) {
7703 this.setActive( state
);
7710 * Select widget containing button options.
7712 * Use together with OO.ui.ButtonOptionWidget.
7715 * @extends OO.ui.SelectWidget
7718 * @param {Object} [config] Configuration options
7720 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
7721 // Parent constructor
7722 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
7725 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
7730 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
7733 * Container for content that is overlaid and positioned absolutely.
7736 * @extends OO.ui.Widget
7737 * @mixins OO.ui.LabeledElement
7740 * @param {Object} [config] Configuration options
7741 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
7742 * @cfg {string} [align='center'] Alignment of popup to origin
7743 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
7744 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
7745 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
7746 * @cfg {boolean} [head] Show label and close button at the top
7748 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
7749 // Config intialization
7750 config
= config
|| {};
7752 // Parent constructor
7753 OO
.ui
.PopupWidget
.super.call( this, config
);
7755 // Mixin constructors
7756 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
7757 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
7760 this.visible
= false;
7761 this.$popup
= this.$( '<div>' );
7762 this.$head
= this.$( '<div>' );
7763 this.$body
= this.$clippable
;
7764 this.$tail
= this.$( '<div>' );
7765 this.$container
= config
.$container
|| this.$( 'body' );
7766 this.autoClose
= !!config
.autoClose
;
7767 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
7768 this.transitionTimeout
= null;
7770 this.align
= config
.align
|| 'center';
7771 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
7772 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
7775 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
7778 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
7779 this.$body
.addClass( 'oo-ui-popupWidget-body' );
7780 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
7782 .addClass( 'oo-ui-popupWidget-head' )
7783 .append( this.$label
, this.closeButton
.$element
);
7784 if ( !config
.head
) {
7788 .addClass( 'oo-ui-popupWidget-popup' )
7789 .append( this.$head
, this.$body
);
7790 this.$element
.hide()
7791 .addClass( 'oo-ui-popupWidget' )
7792 .append( this.$popup
, this.$tail
);
7797 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
7798 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
7799 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
7814 * Handles mouse down events.
7816 * @param {jQuery.Event} e Mouse down event
7818 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
7821 !$.contains( this.$element
[0], e
.target
) &&
7822 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
7829 * Bind mouse down listener.
7831 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
7832 // Capture clicks outside popup
7833 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
7837 * Handles close button click events.
7839 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
7840 if ( this.visible
) {
7846 * Unbind mouse down listener.
7848 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
7849 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
7853 * Check if the popup is visible.
7855 * @return {boolean} Popup is visible
7857 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
7858 return this.visible
;
7862 * Set whether to show a tail.
7864 * @return {boolean} Make tail visible
7866 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
7868 if ( this.tail
!== value
) {
7871 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
7873 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
7879 * Check if showing a tail.
7881 * @return {boolean} tail is visible
7883 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
7893 OO
.ui
.PopupWidget
.prototype.show = function () {
7894 if ( !this.visible
) {
7895 this.setClipping( true );
7896 this.$element
.show();
7897 this.visible
= true;
7898 this.emit( 'show' );
7899 if ( this.autoClose
) {
7900 this.bindMouseDownListener();
7912 OO
.ui
.PopupWidget
.prototype.hide = function () {
7913 if ( this.visible
) {
7914 this.setClipping( false );
7915 this.$element
.hide();
7916 this.visible
= false;
7917 this.emit( 'hide' );
7918 if ( this.autoClose
) {
7919 this.unbindMouseDownListener();
7926 * Updates the position and size.
7928 * @param {number} width Width
7929 * @param {number} height Height
7930 * @param {boolean} [transition=false] Use a smooth transition
7933 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
7935 originOffset
= Math
.round( this.$element
.offset().left
),
7936 containerLeft
= Math
.round( this.$container
.offset().left
),
7937 containerWidth
= this.$container
.innerWidth(),
7938 containerRight
= containerLeft
+ containerWidth
,
7939 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
7940 popupLeft
= popupOffset
- padding
,
7941 popupRight
= popupOffset
+ padding
+ width
+ padding
,
7942 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
7943 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
7945 // Prevent transition from being interrupted
7946 clearTimeout( this.transitionTimeout
);
7948 // Enable transition
7949 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
7952 if ( overlapRight
< 0 ) {
7953 popupOffset
+= overlapRight
;
7954 } else if ( overlapLeft
< 0 ) {
7955 popupOffset
-= overlapLeft
;
7958 // Position body relative to anchor and resize
7960 'left': popupOffset
,
7962 'height': height
=== undefined ? 'auto' : height
7966 // Prevent transitioning after transition is complete
7967 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
7968 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7971 // Prevent transitioning immediately
7972 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7979 * Button that shows and hides a popup.
7982 * @extends OO.ui.ButtonWidget
7983 * @mixins OO.ui.PopuppableElement
7986 * @param {Object} [config] Configuration options
7988 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7989 // Parent constructor
7990 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7992 // Mixin constructors
7993 OO
.ui
.PopuppableElement
.call( this, config
);
7997 .addClass( 'oo-ui-popupButtonWidget' )
7998 .append( this.popup
.$element
);
8003 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8004 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
8009 * Handles mouse click events.
8011 * @param {jQuery.Event} e Mouse click event
8013 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8014 // Skip clicks within the popup
8015 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8019 if ( !this.isDisabled() ) {
8020 if ( this.popup
.isVisible() ) {
8025 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8033 * Combines query and results selection widgets.
8036 * @extends OO.ui.Widget
8039 * @param {Object} [config] Configuration options
8040 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
8041 * @cfg {string} [value] Initial query value
8043 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
8044 // Configuration intialization
8045 config
= config
|| {};
8047 // Parent constructor
8048 OO
.ui
.SearchWidget
.super.call( this, config
);
8051 this.query
= new OO
.ui
.TextInputWidget( {
8054 'placeholder': config
.placeholder
,
8055 'value': config
.value
8057 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
8058 this.$query
= this.$( '<div>' );
8059 this.$results
= this.$( '<div>' );
8062 this.query
.connect( this, {
8063 'change': 'onQueryChange',
8064 'enter': 'onQueryEnter'
8066 this.results
.connect( this, {
8067 'highlight': 'onResultsHighlight',
8068 'select': 'onResultsSelect'
8070 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
8074 .addClass( 'oo-ui-searchWidget-query' )
8075 .append( this.query
.$element
);
8077 .addClass( 'oo-ui-searchWidget-results' )
8078 .append( this.results
.$element
);
8080 .addClass( 'oo-ui-searchWidget' )
8081 .append( this.$results
, this.$query
);
8086 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
8092 * @param {Object|null} item Item data or null if no item is highlighted
8097 * @param {Object|null} item Item data or null if no item is selected
8103 * Handle query key down events.
8105 * @param {jQuery.Event} e Key down event
8107 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
8108 var highlightedItem
, nextItem
,
8109 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
8112 highlightedItem
= this.results
.getHighlightedItem();
8113 if ( !highlightedItem
) {
8114 highlightedItem
= this.results
.getSelectedItem();
8116 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
8117 this.results
.highlightItem( nextItem
);
8118 nextItem
.scrollElementIntoView();
8123 * Handle select widget select events.
8125 * Clears existing results. Subclasses should repopulate items according to new query.
8127 * @param {string} value New value
8129 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
8131 this.results
.clearItems();
8135 * Handle select widget enter key events.
8137 * Selects highlighted item.
8139 * @param {string} value New value
8141 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
8143 this.results
.selectItem( this.results
.getHighlightedItem() );
8147 * Handle select widget highlight events.
8149 * @param {OO.ui.OptionWidget} item Highlighted item
8152 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
8153 this.emit( 'highlight', item
? item
.getData() : null );
8157 * Handle select widget select events.
8159 * @param {OO.ui.OptionWidget} item Selected item
8162 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
8163 this.emit( 'select', item
? item
.getData() : null );
8167 * Get the query input.
8169 * @return {OO.ui.TextInputWidget} Query input
8171 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
8176 * Get the results list.
8178 * @return {OO.ui.SelectWidget} Select list
8180 OO
.ui
.SearchWidget
.prototype.getResults = function () {
8181 return this.results
;
8185 * Text input widget.
8188 * @extends OO.ui.InputWidget
8191 * @param {Object} [config] Configuration options
8192 * @cfg {string} [placeholder] Placeholder text
8193 * @cfg {string} [icon] Symbolic name of icon
8194 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8195 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8196 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8198 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8199 config
= $.extend( { 'maxRows': 10 }, config
);
8201 // Parent constructor
8202 OO
.ui
.TextInputWidget
.super.call( this, config
);
8206 this.multiline
= !!config
.multiline
;
8207 this.autosize
= !!config
.autosize
;
8208 this.maxRows
= config
.maxRows
;
8211 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8212 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8215 this.$element
.addClass( 'oo-ui-textInputWidget' );
8216 if ( config
.icon
) {
8217 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
8218 this.$element
.append(
8220 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
8221 .mousedown( OO
.ui
.bind( function () {
8222 this.$input
.focus();
8227 if ( config
.placeholder
) {
8228 this.$input
.attr( 'placeholder', config
.placeholder
);
8234 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8239 * User presses enter inside the text box.
8241 * Not called if input is multiline.
8249 * Handle key press events.
8251 * @param {jQuery.Event} e Key press event
8252 * @fires enter If enter key is pressed and input is not multiline
8254 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8255 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8256 this.emit( 'enter' );
8261 * Handle element attach events.
8263 * @param {jQuery.Event} e Element attach event
8265 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8272 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8276 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
8280 * Automatically adjust the size of the text input.
8282 * This only affects multi-line inputs that are auto-sized.
8286 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
8287 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
8289 if ( this.multiline
&& this.autosize
) {
8290 $clone
= this.$input
.clone()
8291 .val( this.$input
.val() )
8292 .css( { 'height': 0 } )
8293 .insertAfter( this.$input
);
8294 // Set inline height property to 0 to measure scroll height
8295 scrollHeight
= $clone
[0].scrollHeight
;
8296 // Remove inline height property to measure natural heights
8297 $clone
.css( 'height', '' );
8298 innerHeight
= $clone
.innerHeight();
8299 outerHeight
= $clone
.outerHeight();
8300 // Measure max rows height
8301 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
8302 maxInnerHeight
= $clone
.innerHeight();
8303 $clone
.removeAttr( 'rows' ).css( 'height', '' );
8305 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
8306 // Only apply inline height when expansion beyond natural height is needed
8309 // Use the difference between the inner and outer height as a buffer
8310 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
8317 * Get input element.
8319 * @param {Object} [config] Configuration options
8320 * @return {jQuery} Input element
8322 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
8323 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
8329 * Check if input supports multiple lines.
8333 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
8334 return !!this.multiline
;
8338 * Check if input automatically adjusts its size.
8342 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
8343 return !!this.autosize
;
8347 * Check if input is pending.
8351 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
8352 return !!this.pending
;
8356 * Increase the pending stack.
8360 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
8361 if ( this.pending
=== 0 ) {
8362 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
8363 this.$input
.addClass( 'oo-ui-texture-pending' );
8371 * Reduce the pending stack.
8377 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
8378 if ( this.pending
=== 1 ) {
8379 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
8380 this.$input
.removeClass( 'oo-ui-texture-pending' );
8382 this.pending
= Math
.max( 0, this.pending
- 1 );
8388 * Select the contents of the input.
8392 OO
.ui
.TextInputWidget
.prototype.select = function () {
8393 this.$input
.select();
8398 * Menu for a text input widget.
8401 * @extends OO.ui.MenuWidget
8404 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
8405 * @param {Object} [config] Configuration options
8406 * @cfg {jQuery} [$container=input.$element] Element to render menu under
8408 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
8409 // Parent constructor
8410 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
8414 this.$container
= config
.$container
|| this.input
.$element
;
8415 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
8418 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
8423 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
8428 * Handle window resize event.
8430 * @param {jQuery.Event} e Window resize event
8432 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
8441 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
8443 OO
.ui
.TextInputMenuWidget
.super.prototype.show
.call( this );
8446 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
8455 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
8457 OO
.ui
.TextInputMenuWidget
.super.prototype.hide
.call( this );
8459 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
8464 * Position the menu.
8468 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
8470 $container
= this.$container
,
8471 dimensions
= $container
.offset();
8473 // Position under input
8474 dimensions
.top
+= $container
.height();
8476 // Compensate for frame position if in a differnt frame
8477 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
8478 frameOffset
= OO
.ui
.Element
.getRelativePosition(
8479 this.input
.$.frame
.$element
, this.$element
.offsetParent()
8481 dimensions
.left
+= frameOffset
.left
;
8482 dimensions
.top
+= frameOffset
.top
;
8484 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
8485 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
8486 dimensions
.right
= this.$element
.parent().position().left
-
8487 $container
.width() - dimensions
.left
;
8488 // Erase the value for 'left':
8489 delete dimensions
.left
;
8493 this.$element
.css( dimensions
);
8494 this.setIdealSize( $container
.width() );
8499 * Width with on and off states.
8501 * Mixin for widgets with a boolean state.
8507 * @param {Object} [config] Configuration options
8508 * @cfg {boolean} [value=false] Initial value
8510 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8511 // Configuration initialization
8512 config
= config
|| {};
8518 this.$element
.addClass( 'oo-ui-toggleWidget' );
8519 this.setValue( !!config
.value
);
8526 * @param {boolean} value Changed value
8532 * Get the value of the toggle.
8536 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8541 * Set the value of the toggle.
8543 * @param {boolean} value New value
8547 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8549 if ( this.value
!== value
) {
8551 this.emit( 'change', value
);
8552 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8553 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8559 * Button that toggles on and off.
8562 * @extends OO.ui.ButtonWidget
8563 * @mixins OO.ui.ToggleWidget
8566 * @param {Object} [config] Configuration options
8567 * @cfg {boolean} [value=false] Initial value
8569 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8570 // Configuration initialization
8571 config
= config
|| {};
8573 // Parent constructor
8574 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8576 // Mixin constructors
8577 OO
.ui
.ToggleWidget
.call( this, config
);
8580 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8585 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8586 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8593 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8594 if ( !this.isDisabled() ) {
8595 this.setValue( !this.value
);
8599 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8605 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8607 if ( value
!== this.value
) {
8608 this.setActive( value
);
8611 // Parent method (from mixin)
8612 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8618 * Switch that slides on and off.
8621 * @extends OO.ui.Widget
8622 * @mixins OO.ui.ToggleWidget
8625 * @param {Object} [config] Configuration options
8626 * @cfg {boolean} [value=false] Initial value
8628 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
8629 // Parent constructor
8630 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
8632 // Mixin constructors
8633 OO
.ui
.ToggleWidget
.call( this, config
);
8636 this.dragging
= false;
8637 this.dragStart
= null;
8638 this.sliding
= false;
8639 this.$glow
= this.$( '<span>' );
8640 this.$grip
= this.$( '<span>' );
8643 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8646 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
8647 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
8649 .addClass( 'oo-ui-toggleSwitchWidget' )
8650 .append( this.$glow
, this.$grip
);
8655 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
8656 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
8661 * Handle mouse down events.
8663 * @param {jQuery.Event} e Mouse down event
8665 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
8666 if ( !this.isDisabled() && e
.which
=== 1 ) {
8667 this.setValue( !this.value
);