2 * OOjs UI v0.1.0-pre (eaa1b7f06d)
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: Thu Apr 03 2014 16:56:21 GMT-0700 (PDT)
15 * Namespace for all classes, static methods and static properties.
47 * Get the user's language and any fallback languages.
49 * These language codes are used to localize user interface elements in the user's language.
51 * In environments that provide a localization system, this function should be overridden to
52 * return the user's language(s). The default implementation returns English (en) only.
54 * @return {string[]} Language codes, in descending order of priority
56 OO
.ui
.getUserLanguages = function () {
61 * Get a value in an object keyed by language code.
63 * @param {Object.<string,Mixed>} obj Object keyed by language code
64 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
65 * @param {string} [fallback] Fallback code, used if no matching language can be found
66 * @return {Mixed} Local value
68 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
75 // Known user language
76 langs
= OO
.ui
.getUserLanguages();
77 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
84 if ( obj
[fallback
] ) {
87 // First existing language
98 * Message store for the default implementation of OO.ui.msg
100 * Environments that provide a localization system should not use this, but should override
101 * OO.ui.msg altogether.
106 // Label text for button to exit from dialog
107 'ooui-dialog-action-close': 'Close',
108 // Tool tip for a button that moves items in a list down one place
109 'ooui-outline-control-move-down': 'Move item down',
110 // Tool tip for a button that moves items in a list up one place
111 'ooui-outline-control-move-up': 'Move item up',
112 // Tool tip for a button that removes items from a list
113 'ooui-outline-control-remove': 'Remove item',
114 // Label for the toolbar group that contains a list of all other available tools
115 'ooui-toolbar-more': 'More'
119 * Get a localized message.
121 * In environments that provide a localization system, this function should be overridden to
122 * return the message translated in the user's language. The default implementation always returns
125 * After the message key, message parameters may optionally be passed. In the default implementation,
126 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
127 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
128 * they support unnamed, ordered message parameters.
131 * @param {string} key Message key
132 * @param {Mixed...} [params] Message parameters
133 * @return {string} Translated message with parameters substituted
135 OO
.ui
.msg = function ( key
) {
136 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
137 if ( typeof message
=== 'string' ) {
138 // Perform $1 substitution
139 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
140 var i
= parseInt( n
, 10 );
141 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
144 // Return placeholder if message not found
145 message
= '[' + key
+ ']';
151 OO
.ui
.deferMsg = function ( key
) {
153 return OO
.ui
.msg( key
);
158 OO
.ui
.resolveMsg = function ( msg
) {
159 if ( $.isFunction( msg
) ) {
167 * DOM element abstraction.
173 * @param {Object} [config] Configuration options
174 * @cfg {Function} [$] jQuery for the frame the widget is in
175 * @cfg {string[]} [classes] CSS class names
176 * @cfg {jQuery} [$content] Content elements to append
178 OO
.ui
.Element
= function OoUiElement( config
) {
179 // Configuration initialization
180 config
= config
|| {};
183 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
184 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
185 this.elementGroup
= null;
188 if ( $.isArray( config
.classes
) ) {
189 this.$element
.addClass( config
.classes
.join( ' ' ) );
191 if ( config
.$content
) {
192 this.$element
.append( config
.$content
);
196 /* Static Properties */
198 OO
.ui
.Element
.static = {};
203 * This may be ignored if getTagName is overridden.
209 OO
.ui
.Element
.static.tagName
= 'div';
214 * Get a jQuery function within a specific document.
217 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
218 * @param {OO.ui.Frame} [frame] Frame of the document context
219 * @return {Function} Bound jQuery function
221 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
222 function wrapper( selector
) {
223 return $( selector
, wrapper
.context
);
226 wrapper
.context
= this.getDocument( context
);
229 wrapper
.frame
= frame
;
236 * Get the document of an element.
239 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
240 * @return {HTMLDocument|null} Document object
242 OO
.ui
.Element
.getDocument = function ( obj
) {
243 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
244 return ( obj
[0] && obj
[0].ownerDocument
) ||
245 // Empty jQuery selections might have a context
252 ( obj
.nodeType
=== 9 && obj
) ||
257 * Get the window of an element or document.
260 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
261 * @return {Window} Window object
263 OO
.ui
.Element
.getWindow = function ( obj
) {
264 var doc
= this.getDocument( obj
);
265 return doc
.parentWindow
|| doc
.defaultView
;
269 * Get the direction of an element or document.
272 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
273 * @return {string} Text direction, either `ltr` or `rtl`
275 OO
.ui
.Element
.getDir = function ( obj
) {
278 if ( obj
instanceof jQuery
) {
281 isDoc
= obj
.nodeType
=== 9;
282 isWin
= obj
.document
!== undefined;
283 if ( isDoc
|| isWin
) {
289 return $( obj
).css( 'direction' );
293 * Get the offset between two frames.
295 * TODO: Make this function not use recursion.
298 * @param {Window} from Window of the child frame
299 * @param {Window} [to=window] Window of the parent frame
300 * @param {Object} [offset] Offset to start with, used internally
301 * @return {Object} Offset object, containing left and top properties
303 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
304 var i
, len
, frames
, frame
, rect
;
310 offset
= { 'top': 0, 'left': 0 };
312 if ( from.parent
=== from ) {
316 // Get iframe element
317 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
318 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
319 if ( frames
[i
].contentWindow
=== from ) {
325 // Recursively accumulate offset values
327 rect
= frame
.getBoundingClientRect();
328 offset
.left
+= rect
.left
;
329 offset
.top
+= rect
.top
;
331 this.getFrameOffset( from.parent
, offset
);
338 * Get the offset between two elements.
341 * @param {jQuery} $from
342 * @param {jQuery} $to
343 * @return {Object} Translated position coordinates, containing top and left properties
345 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
346 var from = $from.offset(),
348 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
352 * Get element border sizes.
355 * @param {HTMLElement} el Element to measure
356 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
358 OO
.ui
.Element
.getBorders = function ( el
) {
359 var doc
= el
.ownerDocument
,
360 win
= doc
.parentWindow
|| doc
.defaultView
,
361 style
= win
&& win
.getComputedStyle
?
362 win
.getComputedStyle( el
, null ) :
365 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
366 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
367 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
368 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
371 'top': Math
.round( top
),
372 'left': Math
.round( left
),
373 'bottom': Math
.round( bottom
),
374 'right': Math
.round( right
)
379 * Get dimensions of an element or window.
382 * @param {HTMLElement|Window} el Element to measure
383 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
385 OO
.ui
.Element
.getDimensions = function ( el
) {
387 doc
= el
.ownerDocument
|| el
.document
,
388 win
= doc
.parentWindow
|| doc
.defaultView
;
390 if ( win
=== el
|| el
=== doc
.documentElement
) {
393 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
395 'top': $win
.scrollTop(),
396 'left': $win
.scrollLeft()
398 'scrollbar': { 'right': 0, 'bottom': 0 },
402 'bottom': $win
.innerHeight(),
403 'right': $win
.innerWidth()
409 'borders': this.getBorders( el
),
411 'top': $el
.scrollTop(),
412 'left': $el
.scrollLeft()
415 'right': $el
.innerWidth() - el
.clientWidth
,
416 'bottom': $el
.innerHeight() - el
.clientHeight
418 'rect': el
.getBoundingClientRect()
424 * Get closest scrollable container.
426 * Traverses up until either a scrollable element or the root is reached, in which case the window
430 * @param {HTMLElement} el Element to find scrollable container for
431 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
432 * @return {HTMLElement|Window} Closest scrollable container
434 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
436 props
= [ 'overflow' ],
437 $parent
= $( el
).parent();
439 if ( dimension
=== 'x' || dimension
=== 'y' ) {
440 props
.push( 'overflow-' + dimension
);
443 while ( $parent
.length
) {
444 if ( $parent
[0] === el
.ownerDocument
.body
) {
449 val
= $parent
.css( props
[i
] );
450 if ( val
=== 'auto' || val
=== 'scroll' ) {
454 $parent
= $parent
.parent();
456 return this.getDocument( el
).body
;
460 * Scroll element into view.
463 * @param {HTMLElement} el Element to scroll into view
464 * @param {Object} [config={}] Configuration config
465 * @param {string} [config.duration] jQuery animation duration value
466 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
467 * to scroll in both directions
468 * @param {Function} [config.complete] Function to call when scrolling completes
470 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
471 // Configuration initialization
472 config
= config
|| {};
475 callback
= typeof config
.complete
=== 'function' && config
.complete
,
476 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
478 eld
= this.getDimensions( el
),
479 scd
= this.getDimensions( sc
),
481 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
482 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
483 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
484 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
487 if ( !config
.direction
|| config
.direction
=== 'y' ) {
489 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
490 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
491 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
494 if ( !config
.direction
|| config
.direction
=== 'x' ) {
495 if ( rel
.left
< 0 ) {
496 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
497 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
498 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
501 if ( !$.isEmptyObject( anim
) ) {
502 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
504 $sc
.queue( function ( next
) {
519 * Get the HTML tag name.
521 * Override this method to base the result on instance information.
523 * @return {string} HTML tag name
525 OO
.ui
.Element
.prototype.getTagName = function () {
526 return this.constructor.static.tagName
;
530 * Check if the element is attached to the DOM
531 * @return {boolean} The element is attached to the DOM
533 OO
.ui
.Element
.prototype.isElementAttached = function () {
534 return $.contains( this.getElementDocument(), this.$element
[0] );
538 * Get the DOM document.
540 * @return {HTMLDocument} Document object
542 OO
.ui
.Element
.prototype.getElementDocument = function () {
543 return OO
.ui
.Element
.getDocument( this.$element
);
547 * Get the DOM window.
549 * @return {Window} Window object
551 OO
.ui
.Element
.prototype.getElementWindow = function () {
552 return OO
.ui
.Element
.getWindow( this.$element
);
556 * Get closest scrollable container.
558 * @see #static-method-getClosestScrollableContainer
560 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
561 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
565 * Get group element is in.
567 * @return {OO.ui.GroupElement|null} Group element, null if none
569 OO
.ui
.Element
.prototype.getElementGroup = function () {
570 return this.elementGroup
;
574 * Set group element is in.
576 * @param {OO.ui.GroupElement|null} group Group element, null if none
579 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
580 this.elementGroup
= group
;
585 * Scroll element into view.
587 * @see #static-method-scrollIntoView
588 * @param {Object} [config={}]
590 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
591 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
598 function handler( e
) {
599 jQuery
.event
.simulate( 'focusin', e
.target
, jQuery
.event
.fix( e
), /* bubble = */ true );
604 var doc
= this.ownerDocument
|| this,
605 attaches
= $.data( doc
, 'ooui-focusin-attaches' );
607 doc
.addEventListener( 'focus', handler
, true );
609 $.data( doc
, 'ooui-focusin-attaches', ( attaches
|| 0 ) + 1 );
611 teardown: function () {
612 var doc
= this.ownerDocument
|| this,
613 attaches
= $.data( doc
, 'ooui-focusin-attaches' ) - 1;
615 doc
.removeEventListener( 'focus', handler
, true );
616 $.removeData( doc
, 'ooui-focusin-attaches' );
618 $.data( doc
, 'ooui-focusin-attaches', attaches
);
624 * Bind a handler for an event on the DOM element.
626 * Uses jQuery internally for everything except for events which are
627 * known to have issues in the browser or in jQuery. This method
628 * should become obsolete eventually.
630 * @param {string} event
631 * @param {Function} callback
633 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
636 if ( event
=== 'focusin' ) {
637 // jQuery 1.8.3 has a bug with handling focusin events inside iframes.
638 // Firefox doesn't support focusin at all, so we listen for 'focus' on the
639 // document, and simulate a 'focusin' event on the target element and make
640 // it bubble from there.
642 // - http://jsfiddle.net/sw3hr/
643 // - http://bugs.jquery.com/ticket/14180
644 // - https://github.com/jquery/jquery/commit/1cecf64e5aa4153
646 // Replace jQuery's override with our own
647 orig
= $.event
.special
.focusin
;
648 $.event
.special
.focusin
= specialFocusin
;
650 this.$element
.on( event
, callback
);
653 $.event
.special
.focusin
= orig
;
656 this.$element
.on( event
, callback
);
661 * @param {string} event
662 * @param {Function} callback
664 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
666 if ( event
=== 'focusin' ) {
667 orig
= $.event
.special
.focusin
;
668 $.event
.special
.focusin
= specialFocusin
;
669 this.$element
.off( event
, callback
);
670 $.event
.special
.focusin
= orig
;
672 this.$element
.off( event
, callback
);
677 * Embedded iframe with the same styles as its parent.
680 * @extends OO.ui.Element
681 * @mixins OO.EventEmitter
684 * @param {Object} [config] Configuration options
686 OO
.ui
.Frame
= function OoUiFrame( config
) {
687 // Parent constructor
688 OO
.ui
.Frame
.super.call( this, config
);
690 // Mixin constructors
691 OO
.EventEmitter
.call( this );
694 this.loading
= false;
696 this.config
= config
;
700 .addClass( 'oo-ui-frame' )
701 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
707 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
709 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
711 /* Static Properties */
717 OO
.ui
.Frame
.static.tagName
= 'iframe';
728 * Transplant the CSS styles from as parent document to a frame's document.
730 * This loops over the style sheets in the parent document, and copies their nodes to the
731 * frame's document. It then polls the document to see when all styles have loaded, and once they
732 * have, invokes the callback.
734 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
735 * and invoke the callback anyway. This protects against cases like a display: none; iframe in
736 * Firefox, where the styles won't load until the iframe becomes visible.
738 * For details of how we arrived at the strategy used in this function, see #load.
742 * @param {HTMLDocument} parentDoc Document to transplant styles from
743 * @param {HTMLDocument} frameDoc Document to transplant styles to
744 * @param {Function} [callback] Callback to execute once styles have loaded
745 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
747 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, callback
, timeout
) {
748 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
749 $pollNodes
= $( [] ),
750 // Fake font-family value
751 fontFamily
= 'oo-ui-frame-transplantStyles-loaded';
753 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
754 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
755 if ( callback
&& styleNode
.nodeName
.toLowerCase() === 'link' ) {
756 // External stylesheet
757 // Create a node with a unique ID that we're going to monitor to see when the CSS
759 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
760 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
761 .attr( 'id', pollNodeId
)
762 .appendTo( frameDoc
.body
)
765 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
766 // The font-family rule will only take effect once the @import finishes
767 newNode
= frameDoc
.createElement( 'style' );
768 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
769 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
771 // Not an external stylesheet, or no polling required; just copy the node over
772 newNode
= frameDoc
.importNode( styleNode
, true );
774 frameDoc
.head
.appendChild( newNode
);
778 // Poll every 100ms until all external stylesheets have loaded
779 $pendingPollNodes
= $pollNodes
;
780 timeoutID
= setTimeout( function pollExternalStylesheets() {
782 $pendingPollNodes
.length
> 0 &&
783 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
785 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
788 if ( $pendingPollNodes
.length
=== 0 ) {
790 if ( timeoutID
!== null ) {
796 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
799 // ...but give up after a while
800 if ( timeout
!== 0 ) {
801 setTimeout( function () {
803 clearTimeout( timeoutID
);
808 }, timeout
|| 5000 );
816 * Load the frame contents.
818 * Once the iframe's stylesheets are loaded, the `initialize` event will be emitted.
820 * Sounds simple right? Read on...
822 * When you create a dynamic iframe using open/write/close, the window.load event for the
823 * iframe is triggered when you call close, and there's no further load event to indicate that
824 * everything is actually loaded.
826 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
827 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
828 * are added to document.styleSheets immediately, and the only way you can determine whether they've
829 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
830 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
832 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
833 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
834 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
835 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
836 * .cssRules is allowed.
838 * However, now that we control the styles we're injecting, we might as well do away with
839 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
840 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
841 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
842 * rule is not applied until after the `@import` finishes.
844 * All this stylesheet injection and polling magic is in #transplantStyles.
849 OO
.ui
.Frame
.prototype.load = function () {
850 var win
= this.$element
.prop( 'contentWindow' ),
856 // Figure out directionality:
857 this.dir
= this.$element
.closest( '[dir]' ).prop( 'dir' ) || 'ltr';
859 // Initialize contents
864 '<body class="oo-ui-frame-body oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
865 '<div class="oo-ui-frame-content"></div>' +
872 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
873 this.$content
= this.$( '.oo-ui-frame-content' );
874 this.$document
= this.$( doc
);
876 this.constructor.static.transplantStyles(
877 this.getElementDocument(),
880 frame
.loading
= false;
882 frame
.emit( 'load' );
888 * Run a callback as soon as the frame has been loaded.
891 * This will start loading if it hasn't already, and runs
892 * immediately if the frame is already loaded.
894 * Don't call this until the element is attached.
896 * @param {Function} callback
898 OO
.ui
.Frame
.prototype.run = function ( callback
) {
902 if ( !this.loading
) {
905 this.once( 'load', callback
);
910 * Sets the size of the frame.
912 * @param {number} width Frame width in pixels
913 * @param {number} height Frame height in pixels
916 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
917 this.$element
.css( { 'width': width
, 'height': height
} );
921 * Container for elements in a child frame.
923 * There are two ways to specify a title: set the static `title` property or provide a `title`
924 * property in the configuration options. The latter will override the former.
928 * @extends OO.ui.Element
929 * @mixins OO.EventEmitter
932 * @param {Object} [config] Configuration options
933 * @cfg {string|Function} [title] Title string or function that returns a string
934 * @cfg {string} [icon] Symbolic name of icon
937 OO
.ui
.Window
= function OoUiWindow( config
) {
938 // Parent constructor
939 OO
.ui
.Window
.super.call( this, config
);
941 // Mixin constructors
942 OO
.EventEmitter
.call( this );
945 this.visible
= false;
946 this.opening
= false;
947 this.closing
= false;
948 this.title
= OO
.ui
.resolveMsg( config
.title
|| this.constructor.static.title
);
949 this.icon
= config
.icon
|| this.constructor.static.icon
;
950 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
951 this.$frame
= this.$( '<div>' );
952 this.$ = function () {
953 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
958 .addClass( 'oo-ui-window' )
959 // Hide the window using visibility: hidden; while the iframe is still loading
960 // Can't use display: none; because that prevents the iframe from loading in Firefox
961 .css( 'visibility', 'hidden' )
962 .append( this.$frame
);
964 .addClass( 'oo-ui-window-frame' )
965 .append( this.frame
.$element
);
968 this.frame
.connect( this, { 'load': 'initialize' } );
973 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
975 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
980 * Initialize contents.
982 * Fired asynchronously after construction when iframe is ready.
990 * Fired after window has been opened.
993 * @param {Object} data Window opening data
999 * Fired after window has been closed.
1002 * @param {Object} data Window closing data
1005 /* Static Properties */
1008 * Symbolic name of icon.
1012 * @property {string}
1014 OO
.ui
.Window
.static.icon
= 'window';
1019 * Subclasses must implement this property before instantiating the window.
1020 * Alternatively, override #getTitle with an alternative implementation.
1025 * @property {string|Function} Title string or function that returns a string
1027 OO
.ui
.Window
.static.title
= null;
1032 * Check if window is visible.
1034 * @return {boolean} Window is visible
1036 OO
.ui
.Window
.prototype.isVisible = function () {
1037 return this.visible
;
1041 * Check if window is opening.
1043 * @return {boolean} Window is opening
1045 OO
.ui
.Window
.prototype.isOpening = function () {
1046 return this.opening
;
1050 * Check if window is closing.
1052 * @return {boolean} Window is closing
1054 OO
.ui
.Window
.prototype.isClosing = function () {
1055 return this.closing
;
1059 * Get the window frame.
1061 * @return {OO.ui.Frame} Frame of window
1063 OO
.ui
.Window
.prototype.getFrame = function () {
1068 * Get the title of the window.
1070 * @return {string} Title text
1072 OO
.ui
.Window
.prototype.getTitle = function () {
1077 * Get the window icon.
1079 * @return {string} Symbolic name of icon
1081 OO
.ui
.Window
.prototype.getIcon = function () {
1086 * Set the size of window frame.
1088 * @param {number} [width=auto] Custom width
1089 * @param {number} [height=auto] Custom height
1092 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1093 if ( !this.frame
.$content
) {
1097 this.frame
.$element
.css( {
1098 'width': width
=== undefined ? 'auto' : width
,
1099 'height': height
=== undefined ? 'auto' : height
1106 * Set the title of the window.
1108 * @param {string|Function} title Title text or a function that returns text
1111 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1112 this.title
= OO
.ui
.resolveMsg( title
);
1113 if ( this.$title
) {
1114 this.$title
.text( title
);
1120 * Set the icon of the window.
1122 * @param {string} icon Symbolic name of icon
1125 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1127 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1131 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1138 * Set the position of window to fit with contents.
1140 * @param {string} left Left offset
1141 * @param {string} top Top offset
1144 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1145 this.$element
.css( { 'left': left
, 'top': top
} );
1150 * Set the height of window to fit with contents.
1152 * @param {number} [min=0] Min height
1153 * @param {number} [max] Max height (defaults to content's outer height)
1156 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1157 var height
= this.frame
.$content
.outerHeight();
1159 this.frame
.$element
.css(
1160 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1167 * Set the width of window to fit with contents.
1169 * @param {number} [min=0] Min height
1170 * @param {number} [max] Max height (defaults to content's outer width)
1173 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1174 var width
= this.frame
.$content
.outerWidth();
1176 this.frame
.$element
.css(
1177 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1184 * Initialize window contents.
1186 * The first time the window is opened, #initialize is called when it's safe to begin populating
1187 * its contents. See #setup for a way to make changes each time the window opens.
1189 * Once this method is called, this.$$ can be used to create elements within the frame.
1194 OO
.ui
.Window
.prototype.initialize = function () {
1196 this.$ = this.frame
.$;
1197 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1198 .text( this.title
);
1199 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1200 .addClass( 'oo-ui-icon-' + this.icon
);
1201 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1202 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1203 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1204 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1207 this.frame
.$content
.append(
1208 this.$head
.append( this.$icon
, this.$title
),
1214 // Undo the visibility: hidden; hack from the constructor and apply display: none;
1215 // We can do this safely now that the iframe has initialized
1216 this.$element
.hide().css( 'visibility', '' );
1218 this.emit( 'initialize' );
1224 * Setup window for use.
1226 * Each time the window is opened, once it's ready to be interacted with, this will set it up for
1227 * use in a particular context, based on the `data` argument.
1229 * When you override this method, you must call the parent method at the very beginning.
1232 * @param {Object} [data] Window opening data
1234 OO
.ui
.Window
.prototype.setup = function () {
1235 // Override to do something
1239 * Tear down window after use.
1241 * Each time the window is closed, and it's done being interacted with, this will tear it down and
1242 * do something with the user's interactions within the window, based on the `data` argument.
1244 * When you override this method, you must call the parent method at the very end.
1247 * @param {Object} [data] Window closing data
1249 OO
.ui
.Window
.prototype.teardown = function () {
1250 // Override to do something
1256 * Do not override this method. See #setup for a way to make changes each time the window opens.
1258 * @param {Object} [data] Window opening data
1262 OO
.ui
.Window
.prototype.open = function ( data
) {
1263 if ( !this.opening
&& !this.closing
&& !this.visible
) {
1264 this.opening
= true;
1265 this.frame
.run( OO
.ui
.bind( function () {
1266 this.$element
.show();
1267 this.visible
= true;
1268 this.frame
.$element
.focus();
1269 this.emit( 'opening', data
);
1271 this.emit( 'open', data
);
1272 this.opening
= false;
1282 * See #teardown for a way to do something each time the window closes.
1284 * @param {Object} [data] Window closing data
1288 OO
.ui
.Window
.prototype.close = function ( data
) {
1289 if ( !this.opening
&& !this.closing
&& this.visible
) {
1290 this.frame
.$content
.find( ':focus' ).blur();
1291 this.closing
= true;
1292 this.$element
.hide();
1293 this.visible
= false;
1294 this.emit( 'closing', data
);
1295 this.teardown( data
);
1296 this.emit( 'close', data
);
1297 this.closing
= false;
1303 * Set of mutually exclusive windows.
1306 * @extends OO.ui.Element
1307 * @mixins OO.EventEmitter
1310 * @param {OO.Factory} factory Window factory
1311 * @param {Object} [config] Configuration options
1313 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1314 // Parent constructor
1315 OO
.ui
.WindowSet
.super.call( this, config
);
1317 // Mixin constructors
1318 OO
.EventEmitter
.call( this );
1321 this.factory
= factory
;
1324 * List of all windows associated with this window set.
1326 * @property {OO.ui.Window[]}
1328 this.windowList
= [];
1331 * Mapping of OO.ui.Window objects created by name from the #factory.
1333 * @property {Object}
1336 this.currentWindow
= null;
1339 this.$element
.addClass( 'oo-ui-windowSet' );
1344 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1346 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1352 * @param {OO.ui.Window} win Window that's being opened
1353 * @param {Object} config Window opening information
1358 * @param {OO.ui.Window} win Window that's been opened
1359 * @param {Object} config Window opening information
1364 * @param {OO.ui.Window} win Window that's being closed
1365 * @param {Object} config Window closing information
1370 * @param {OO.ui.Window} win Window that's been closed
1371 * @param {Object} config Window closing information
1377 * Handle a window that's being opened.
1379 * @param {OO.ui.Window} win Window that's being opened
1380 * @param {Object} [config] Window opening information
1383 OO
.ui
.WindowSet
.prototype.onWindowOpening = function ( win
, config
) {
1384 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1385 this.currentWindow
.close();
1387 this.currentWindow
= win
;
1388 this.emit( 'opening', win
, config
);
1392 * Handle a window that's been opened.
1394 * @param {OO.ui.Window} win Window that's been opened
1395 * @param {Object} [config] Window opening information
1398 OO
.ui
.WindowSet
.prototype.onWindowOpen = function ( win
, config
) {
1399 this.emit( 'open', win
, config
);
1403 * Handle a window that's being closed.
1405 * @param {OO.ui.Window} win Window that's being closed
1406 * @param {Object} [config] Window closing information
1409 OO
.ui
.WindowSet
.prototype.onWindowClosing = function ( win
, config
) {
1410 this.currentWindow
= null;
1411 this.emit( 'closing', win
, config
);
1415 * Handle a window that's been closed.
1417 * @param {OO.ui.Window} win Window that's been closed
1418 * @param {Object} [config] Window closing information
1421 OO
.ui
.WindowSet
.prototype.onWindowClose = function ( win
, config
) {
1422 this.emit( 'close', win
, config
);
1426 * Get the current window.
1428 * @return {OO.ui.Window} Current window
1430 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1431 return this.currentWindow
;
1435 * Return a given window.
1437 * @param {string} name Symbolic name of window
1438 * @return {OO.ui.Window} Window with specified name
1440 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1443 if ( !this.factory
.lookup( name
) ) {
1444 throw new Error( 'Unknown window: ' + name
);
1446 if ( !( name
in this.windows
) ) {
1447 win
= this.windows
[name
] = this.createWindow( name
);
1448 this.addWindow( win
);
1450 return this.windows
[name
];
1454 * Create a window for use in this window set.
1456 * @param {string} name Symbolic name of window
1457 * @return {OO.ui.Window} Window with specified name
1459 OO
.ui
.WindowSet
.prototype.createWindow = function ( name
) {
1460 return this.factory
.create( name
, { '$': this.$ } );
1464 * Add a given window to this window set.
1466 * Connects event handlers and attaches it to the DOM. Calling
1467 * OO.ui.Window#open will not work until the window is added to the set.
1469 * @param {OO.ui.Window} win
1471 OO
.ui
.WindowSet
.prototype.addWindow = function ( win
) {
1472 if ( this.windowList
.indexOf( win
) !== -1 ) {
1476 this.windowList
.push( win
);
1478 win
.connect( this, {
1479 'opening': [ 'onWindowOpening', win
],
1480 'open': [ 'onWindowOpen', win
],
1481 'closing': [ 'onWindowClosing', win
],
1482 'close': [ 'onWindowClose', win
]
1484 this.$element
.append( win
.$element
);
1489 * @extends OO.ui.Window
1492 * @param {Object} [config] Configuration options
1493 * @cfg {boolean} [footless] Hide foot
1494 * @cfg {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1496 OO
.ui
.Dialog
= function OoUiDialog( config
) {
1497 // Configuration initialization
1498 config
= $.extend( { 'size': 'large' }, config
);
1500 // Parent constructor
1501 OO
.ui
.Dialog
.super.call( this, config
);
1504 this.visible
= false;
1505 this.footless
= !!config
.footless
;
1507 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1508 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1511 this.$element
.on( 'mousedown', false );
1512 this.connect( this, { 'opening': 'onOpening' } );
1515 this.$element
.addClass( 'oo-ui-dialog' );
1516 this.setSize( config
.size
);
1521 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1523 /* Static Properties */
1526 * Symbolic name of dialog.
1530 * @property {string}
1533 OO
.ui
.Dialog
.static.name
= '';
1536 * Map of symbolic size names and CSS classes.
1539 * @property {Object}
1542 OO
.ui
.Dialog
.static.sizeCssClasses
= {
1543 'small': 'oo-ui-dialog-small',
1544 'medium': 'oo-ui-dialog-medium',
1545 'large': 'oo-ui-dialog-large'
1551 * Handle close button click events.
1553 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1554 this.close( { 'action': 'cancel' } );
1558 * Handle window mouse wheel events.
1560 * @param {jQuery.Event} e Mouse wheel event
1562 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1567 * Handle document key down events.
1569 * @param {jQuery.Event} e Key down event
1571 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1572 switch ( e
.which
) {
1573 case OO
.ui
.Keys
.PAGEUP
:
1574 case OO
.ui
.Keys
.PAGEDOWN
:
1575 case OO
.ui
.Keys
.END
:
1576 case OO
.ui
.Keys
.HOME
:
1577 case OO
.ui
.Keys
.LEFT
:
1579 case OO
.ui
.Keys
.RIGHT
:
1580 case OO
.ui
.Keys
.DOWN
:
1581 // Prevent any key events that might cause scrolling
1587 * Handle frame document key down events.
1589 * @param {jQuery.Event} e Key down event
1591 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1592 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1593 this.close( { 'action': 'cancel' } );
1599 OO
.ui
.Dialog
.prototype.onOpening = function () {
1600 this.$element
.addClass( 'oo-ui-dialog-open' );
1606 * @param {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1608 OO
.ui
.Dialog
.prototype.setSize = function ( size
) {
1609 var name
, state
, cssClass
,
1610 sizeCssClasses
= OO
.ui
.Dialog
.static.sizeCssClasses
;
1612 if ( !sizeCssClasses
[size
] ) {
1616 for ( name
in sizeCssClasses
) {
1617 state
= name
=== size
;
1618 cssClass
= sizeCssClasses
[name
];
1619 this.$element
.toggleClass( cssClass
, state
);
1620 if ( this.frame
.$content
) {
1621 this.frame
.$content
.toggleClass( cssClass
, state
);
1629 OO
.ui
.Dialog
.prototype.initialize = function () {
1631 OO
.ui
.Window
.prototype.initialize
.call( this );
1634 this.closeButton
= new OO
.ui
.ButtonWidget( {
1638 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1642 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1643 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1646 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1647 if ( this.footless
) {
1648 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1650 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1651 this.$head
.append( this.closeButton
.$element
);
1657 OO
.ui
.Dialog
.prototype.setup = function ( data
) {
1659 OO
.ui
.Window
.prototype.setup
.call( this, data
);
1661 // Prevent scrolling in top-level window
1662 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1663 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1669 OO
.ui
.Dialog
.prototype.teardown = function ( data
) {
1671 OO
.ui
.Window
.prototype.teardown
.call( this, data
);
1673 // Allow scrolling in top-level window
1674 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1675 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1681 OO
.ui
.Dialog
.prototype.close = function ( data
) {
1683 if ( !dialog
.opening
&& !dialog
.closing
&& dialog
.visible
) {
1684 // Trigger transition
1685 dialog
.$element
.removeClass( 'oo-ui-dialog-open' );
1686 // Allow transition to complete before actually closing
1687 setTimeout( function () {
1689 OO
.ui
.Window
.prototype.close
.call( dialog
, data
);
1694 * Container for elements.
1698 * @extends OO.ui.Element
1699 * @mixins OO.EventEmitter
1702 * @param {Object} [config] Configuration options
1704 OO
.ui
.Layout
= function OoUiLayout( config
) {
1705 // Initialize config
1706 config
= config
|| {};
1708 // Parent constructor
1709 OO
.ui
.Layout
.super.call( this, config
);
1711 // Mixin constructors
1712 OO
.EventEmitter
.call( this );
1715 this.$element
.addClass( 'oo-ui-layout' );
1720 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1722 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1724 * User interface control.
1728 * @extends OO.ui.Element
1729 * @mixins OO.EventEmitter
1732 * @param {Object} [config] Configuration options
1733 * @cfg {boolean} [disabled=false] Disable
1735 OO
.ui
.Widget
= function OoUiWidget( config
) {
1736 // Initialize config
1737 config
= $.extend( { 'disabled': false }, config
);
1739 // Parent constructor
1740 OO
.ui
.Widget
.super.call( this, config
);
1742 // Mixin constructors
1743 OO
.EventEmitter
.call( this );
1746 this.disabled
= null;
1747 this.wasDisabled
= null;
1750 this.$element
.addClass( 'oo-ui-widget' );
1751 this.setDisabled( !!config
.disabled
);
1756 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1758 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1764 * @param {boolean} disabled Widget is disabled
1770 * Check if the widget is disabled.
1772 * @param {boolean} Button is disabled
1774 OO
.ui
.Widget
.prototype.isDisabled = function () {
1775 return this.disabled
;
1779 * Update the disabled state, in case of changes in parent widget.
1783 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1784 this.setDisabled( this.disabled
);
1789 * Set the disabled state of the widget.
1791 * This should probably change the widgets' appearance and prevent it from being used.
1793 * @param {boolean} disabled Disable widget
1796 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1799 this.disabled
= !!disabled
;
1800 isDisabled
= this.isDisabled();
1801 if ( isDisabled
!== this.wasDisabled
) {
1802 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1803 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1804 this.emit( 'disable', isDisabled
);
1806 this.wasDisabled
= isDisabled
;
1810 * Element with a button.
1816 * @param {jQuery} $button Button node, assigned to #$button
1817 * @param {Object} [config] Configuration options
1818 * @cfg {boolean} [frameless] Render button without a frame
1819 * @cfg {number} [tabIndex=0] Button's tab index, use -1 to prevent tab focusing
1821 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
1822 // Configuration initialization
1823 config
= config
|| {};
1826 this.$button
= $button
;
1827 this.tabIndex
= null;
1828 this.active
= false;
1829 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
1832 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
1835 this.$element
.addClass( 'oo-ui-buttonedElement' );
1837 .addClass( 'oo-ui-buttonedElement-button' )
1838 .attr( 'role', 'button' )
1839 .prop( 'tabIndex', config
.tabIndex
|| 0 );
1840 if ( config
.frameless
) {
1841 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
1843 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
1850 * Handles mouse down events.
1853 * @param {jQuery.Event} e Mouse down event
1855 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function () {
1856 this.tabIndex
= this.$button
.attr( 'tabIndex' );
1857 // Remove the tab-index while the button is down to prevent the button from stealing focus
1859 .removeAttr( 'tabIndex' )
1860 .addClass( 'oo-ui-buttonedElement-pressed' );
1861 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
1865 * Handles mouse up events.
1868 * @param {jQuery.Event} e Mouse up event
1870 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function () {
1871 // Restore the tab-index after the button is up to restore the button's accesssibility
1873 .attr( 'tabIndex', this.tabIndex
)
1874 .removeClass( 'oo-ui-buttonedElement-pressed' );
1875 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
1882 * @param {boolean} [value] Make button active
1885 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
1886 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
1890 * Element that can be automatically clipped to visible boundaies.
1896 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
1897 * @param {Object} [config] Configuration options
1899 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
1900 // Configuration initialization
1901 config
= config
|| {};
1904 this.$clippable
= $clippable
;
1905 this.clipping
= false;
1906 this.clipped
= false;
1907 this.$clippableContainer
= null;
1908 this.$clippableScroller
= null;
1909 this.$clippableWindow
= null;
1910 this.idealWidth
= null;
1911 this.idealHeight
= null;
1912 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
1913 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
1916 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
1925 * @param {boolean} value Enable clipping
1928 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
1931 if ( this.clipping
!== value
) {
1932 this.clipping
= value
;
1933 if ( this.clipping
) {
1934 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
1935 // If the clippable container is the body, we have to listen to scroll events and check
1936 // jQuery.scrollTop on the window because of browser inconsistencies
1937 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
1938 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
1939 this.$clippableContainer
;
1940 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
1941 this.$clippableWindow
= this.$( this.getElementWindow() )
1942 .on( 'resize', this.onClippableWindowResizeHandler
);
1943 // Initial clip after visible
1944 setTimeout( OO
.ui
.bind( this.clip
, this ) );
1946 this.$clippableContainer
= null;
1947 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
1948 this.$clippableScroller
= null;
1949 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
1950 this.$clippableWindow
= null;
1958 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
1961 * @return {boolean} Element will be clipped to the visible area
1963 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
1964 return this.clipping
;
1968 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
1971 * @return {boolean} Part of the element is being clipped
1973 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
1974 return this.clipped
;
1978 * Set the ideal size.
1981 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
1982 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
1984 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
1985 this.idealWidth
= width
;
1986 this.idealHeight
= height
;
1990 * Clip element to visible boundaries and allow scrolling when needed.
1992 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
1993 * overlapped by, the visible area of the nearest scrollable container.
1998 OO
.ui
.ClippableElement
.prototype.clip = function () {
1999 if ( !this.clipping
) {
2000 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
2005 cOffset
= this.$clippable
.offset(),
2006 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
2007 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
2008 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
2009 scrollTop
= this.$clippableScroller
.scrollTop(),
2010 scrollLeft
= this.$clippableScroller
.scrollLeft(),
2011 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
2012 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
2013 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
2014 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
2015 clipWidth
= desiredWidth
< naturalWidth
,
2016 clipHeight
= desiredHeight
< naturalHeight
;
2019 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
2021 this.$clippable
.css( { 'overflow-x': '', 'width': this.idealWidth
|| '' } );
2024 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
2026 this.$clippable
.css( { 'overflow-y': '', 'height': this.idealHeight
|| '' } );
2029 this.clipped
= clipWidth
|| clipHeight
;
2034 * Element with named flags, used for styling, that can be added, removed and listed and checked.
2040 * @param {Object} [config] Configuration options
2041 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
2043 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
2044 // Config initialization
2045 config
= config
|| {};
2051 this.setFlags( config
.flags
);
2057 * Check if a flag is set.
2060 * @param {string} flag Flag name to check
2061 * @returns {boolean} Has flag
2063 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
2064 return flag
in this.flags
;
2068 * Get the names of all flags.
2071 * @returns {string[]} flags Flag names
2073 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
2074 return Object
.keys( this.flags
);
2078 * Add one or more flags.
2081 * @param {string[]|Object.<string, boolean>} flags List of flags to add, or list of set/remove
2082 * values, keyed by flag name
2085 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
2087 classPrefix
= 'oo-ui-flaggableElement-';
2089 if ( $.isArray( flags
) ) {
2090 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
2093 this.flags
[flag
] = true;
2094 this.$element
.addClass( classPrefix
+ flag
);
2096 } else if ( OO
.isPlainObject( flags
) ) {
2097 for ( flag
in flags
) {
2098 if ( flags
[flag
] ) {
2100 this.flags
[flag
] = true;
2101 this.$element
.addClass( classPrefix
+ flag
);
2104 delete this.flags
[flag
];
2105 this.$element
.removeClass( classPrefix
+ flag
);
2112 * Element containing a sequence of child elements.
2118 * @param {jQuery} $group Container node, assigned to #$group
2119 * @param {Object} [config] Configuration options
2120 * @cfg {Object.<string,string>} [aggregations] Events to aggregate, keyed by item event name
2122 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2124 config
= config
|| {};
2127 this.$group
= $group
;
2129 this.$items
= this.$( [] );
2130 this.aggregate
= !$.isEmptyObject( config
.aggregations
);
2131 this.aggregations
= config
.aggregations
|| {};
2140 * @returns {OO.ui.Element[]} Items
2142 OO
.ui
.GroupElement
.prototype.getItems = function () {
2143 return this.items
.slice( 0 );
2150 * @param {OO.ui.Element[]} items Item
2151 * @param {number} [index] Index to insert items at
2154 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2155 var i
, len
, item
, event
, events
, currentIndex
,
2156 $items
= this.$( [] );
2158 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2161 // Check if item exists then remove it first, effectively "moving" it
2162 currentIndex
= $.inArray( item
, this.items
);
2163 if ( currentIndex
>= 0 ) {
2164 this.removeItems( [ item
] );
2165 // Adjust index to compensate for removal
2166 if ( currentIndex
< index
) {
2171 if ( this.aggregate
) {
2173 for ( event
in this.aggregations
) {
2174 events
[event
] = [ 'emit', this.aggregations
[event
], item
];
2176 item
.connect( this, events
);
2178 item
.setElementGroup( this );
2179 $items
= $items
.add( item
.$element
);
2182 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2183 this.$group
.append( $items
);
2184 this.items
.push
.apply( this.items
, items
);
2185 } else if ( index
=== 0 ) {
2186 this.$group
.prepend( $items
);
2187 this.items
.unshift
.apply( this.items
, items
);
2189 this.$items
.eq( index
).before( $items
);
2190 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2193 this.$items
= this.$items
.add( $items
);
2201 * Items will be detached, not removed, so they can be used later.
2204 * @param {OO.ui.Element[]} items Items to remove
2207 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2208 var i
, len
, item
, index
;
2210 // Remove specific items
2211 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2213 index
= $.inArray( item
, this.items
);
2214 if ( index
!== -1 ) {
2215 if ( this.aggregate
) {
2216 item
.disconnect( this );
2218 item
.setElementGroup( null );
2219 this.items
.splice( index
, 1 );
2220 item
.$element
.detach();
2221 this.$items
= this.$items
.not( item
.$element
);
2231 * Items will be detached, not removed, so they can be used later.
2236 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2240 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2241 item
= this.items
[i
];
2242 if ( this.aggregate
) {
2243 item
.disconnect( this );
2245 item
.setElementGroup( null );
2248 this.$items
.detach();
2249 this.$items
= this.$( [] );
2252 * Element containing an icon.
2258 * @param {jQuery} $icon Icon node, assigned to #$icon
2259 * @param {Object} [config] Configuration options
2260 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2261 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2264 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2265 // Config intialization
2266 config
= config
|| {};
2273 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2274 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2277 /* Static Properties */
2279 OO
.ui
.IconedElement
.static = {};
2284 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2286 * For i18n purposes, this property can be an object containing a `default` icon name property and
2287 * additional icon names keyed by language code.
2289 * Example of i18n icon definition:
2290 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2294 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2295 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2298 OO
.ui
.IconedElement
.static.icon
= null;
2306 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
2307 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2311 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2312 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2315 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2317 if ( typeof icon
=== 'string' ) {
2319 if ( icon
.length
) {
2320 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2324 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2333 * @returns {string} Icon
2335 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2339 * Element containing an indicator.
2345 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2346 * @param {Object} [config] Configuration options
2347 * @cfg {string} [indicator] Symbolic indicator name
2348 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2350 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2351 // Config intialization
2352 config
= config
|| {};
2355 this.$indicator
= $indicator
;
2356 this.indicator
= null;
2357 this.indicatorLabel
= null;
2360 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2361 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2362 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2365 /* Static Properties */
2367 OO
.ui
.IndicatedElement
.static = {};
2374 * @property {string|null} Symbolic indicator name or null for no indicator
2376 OO
.ui
.IndicatedElement
.static.indicator
= null;
2383 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2386 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2394 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2397 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2398 if ( this.indicator
) {
2399 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2400 this.indicator
= null;
2402 if ( typeof indicator
=== 'string' ) {
2403 indicator
= indicator
.trim();
2404 if ( indicator
.length
) {
2405 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2406 this.indicator
= indicator
;
2409 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2415 * Set indicator label.
2418 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2419 * for no indicator title
2422 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2423 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2425 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2426 this.$indicator
.attr( 'title', indicatorTitle
);
2428 this.$indicator
.removeAttr( 'title' );
2438 * @returns {string} title Symbolic name of indicator
2440 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2441 return this.indicator
;
2445 * Get indicator title.
2448 * @returns {string} Indicator title text
2450 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2451 return this.indicatorTitle
;
2454 * Element containing a label.
2460 * @param {jQuery} $label Label node, assigned to #$label
2461 * @param {Object} [config] Configuration options
2462 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2463 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
2465 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2466 // Config intialization
2467 config
= config
|| {};
2470 this.$label
= $label
;
2474 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2475 this.setLabel( config
.label
|| this.constructor.static.label
);
2476 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
2479 /* Static Properties */
2481 OO
.ui
.LabeledElement
.static = {};
2488 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2491 OO
.ui
.LabeledElement
.static.label
= null;
2498 * An empty string will result in the label being hidden. A string containing only whitespace will
2499 * be converted to a single
2502 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2503 * text; or null for no label
2506 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2509 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2510 if ( typeof label
=== 'string' && label
.length
) {
2511 if ( label
.match( /^\s*$/ ) ) {
2512 // Convert whitespace only string to a single non-breaking space
2513 this.$label
.html( ' ' );
2515 this.$label
.text( label
);
2517 } else if ( label
instanceof jQuery
) {
2518 this.$label
.empty().append( label
);
2520 this.$label
.empty();
2523 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2524 this.$label
.css( 'display', empty
? 'none' : '' );
2533 * @returns {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2534 * text; or null for no label
2536 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2546 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
2547 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
2548 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
2553 * Popuppable element.
2559 * @param {Object} [config] Configuration options
2560 * @cfg {number} [popupWidth=320] Width of popup
2561 * @cfg {number} [popupHeight] Height of popup
2562 * @cfg {Object} [popup] Configuration to pass to popup
2564 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
2565 // Configuration initialization
2566 config
= $.extend( { 'popupWidth': 320 }, config
);
2569 this.popup
= new OO
.ui
.PopupWidget( $.extend(
2570 { 'align': 'center', 'autoClose': true },
2572 { '$': this.$, '$autoCloseIgnore': this.$element
}
2574 this.popupWidth
= config
.popupWidth
;
2575 this.popupHeight
= config
.popupHeight
;
2584 * @returns {OO.ui.PopupWidget} Popup widget
2586 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
2595 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
2596 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
2604 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
2608 * Element with a title.
2614 * @param {jQuery} $label Titled node, assigned to #$titled
2615 * @param {Object} [config] Configuration options
2616 * @cfg {string|Function} [title] Title text or a function that returns text
2618 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
2619 // Config intialization
2620 config
= config
|| {};
2623 this.$titled
= $titled
;
2627 this.setTitle( config
.title
|| this.constructor.static.title
);
2630 /* Static Properties */
2632 OO
.ui
.TitledElement
.static = {};
2639 * @property {string|Function} Title text or a function that returns text
2641 OO
.ui
.TitledElement
.static.title
= null;
2649 * @param {string|Function|null} title Title text, a function that returns text or null for no title
2652 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
2653 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
2655 if ( typeof title
=== 'string' && title
.length
) {
2656 this.$titled
.attr( 'title', title
);
2658 this.$titled
.removeAttr( 'title' );
2668 * @returns {string} Title string
2670 OO
.ui
.TitledElement
.prototype.getTitle = function () {
2674 * Generic toolbar tool.
2678 * @extends OO.ui.Widget
2679 * @mixins OO.ui.IconedElement
2682 * @param {OO.ui.ToolGroup} toolGroup
2683 * @param {Object} [config] Configuration options
2684 * @cfg {string|Function} [title] Title text or a function that returns text
2686 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
2687 // Config intialization
2688 config
= config
|| {};
2690 // Parent constructor
2691 OO
.ui
.Tool
.super.call( this, config
);
2693 // Mixin constructors
2694 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
2697 this.toolGroup
= toolGroup
;
2698 this.toolbar
= this.toolGroup
.getToolbar();
2699 this.active
= false;
2700 this.$title
= this.$( '<span>' );
2701 this.$link
= this.$( '<a>' );
2705 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
2708 this.$title
.addClass( 'oo-ui-tool-title' );
2710 .addClass( 'oo-ui-tool-link' )
2711 .append( this.$icon
, this.$title
);
2713 .data( 'oo-ui-tool', this )
2715 'oo-ui-tool ' + 'oo-ui-tool-name-' +
2716 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
2718 .append( this.$link
);
2719 this.setTitle( config
.title
|| this.constructor.static.title
);
2724 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
2726 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
2734 /* Static Properties */
2740 OO
.ui
.Tool
.static.tagName
= 'span';
2743 * Symbolic name of tool.
2747 * @property {string}
2750 OO
.ui
.Tool
.static.name
= '';
2757 * @property {string}
2760 OO
.ui
.Tool
.static.group
= '';
2765 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
2766 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
2767 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
2768 * appended to the title if the tool is part of a bar tool group.
2772 * @property {string|Function} Title text or a function that returns text
2775 OO
.ui
.Tool
.static.title
= '';
2778 * Tool can be automatically added to catch-all groups.
2781 * @property {boolean}
2784 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
2787 * Tool can be automatically added to named groups.
2790 * @property {boolean}
2793 OO
.ui
.Tool
.static.autoAddToGroup
= true;
2796 * Check if this tool is compatible with given data.
2801 * @param {Mixed} data Data to check
2802 * @return {boolean} Tool can be used with data
2804 OO
.ui
.Tool
.static.isCompatibleWith = function () {
2811 * Handle the toolbar state being updated.
2813 * This is an abstract method that must be overridden in a concrete subclass.
2817 OO
.ui
.Tool
.prototype.onUpdateState = function () {
2819 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
2824 * Handle the tool being selected.
2826 * This is an abstract method that must be overridden in a concrete subclass.
2830 OO
.ui
.Tool
.prototype.onSelect = function () {
2832 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
2837 * Check if the button is active.
2839 * @param {boolean} Button is active
2841 OO
.ui
.Tool
.prototype.isActive = function () {
2846 * Make the button appear active or inactive.
2848 * @param {boolean} state Make button appear active
2850 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
2851 this.active
= !!state
;
2852 if ( this.active
) {
2853 this.$element
.addClass( 'oo-ui-tool-active' );
2855 this.$element
.removeClass( 'oo-ui-tool-active' );
2860 * Get the tool title.
2862 * @param {string|Function} title Title text or a function that returns text
2865 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
2866 this.title
= OO
.ui
.resolveMsg( title
);
2872 * Get the tool title.
2874 * @return {string} Title text
2876 OO
.ui
.Tool
.prototype.getTitle = function () {
2881 * Get the tool's symbolic name.
2883 * @return {string} Symbolic name of tool
2885 OO
.ui
.Tool
.prototype.getName = function () {
2886 return this.constructor.static.name
;
2892 OO
.ui
.Tool
.prototype.updateTitle = function () {
2893 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
2894 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
2895 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
2902 .addClass( 'oo-ui-tool-accel' )
2906 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
2907 tooltipParts
.push( this.title
);
2909 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
2910 tooltipParts
.push( accel
);
2912 if ( tooltipParts
.length
) {
2913 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
2915 this.$link
.removeAttr( 'title' );
2922 OO
.ui
.Tool
.prototype.destroy = function () {
2923 this.toolbar
.disconnect( this );
2924 this.$element
.remove();
2927 * Collection of tool groups.
2930 * @extends OO.ui.Element
2931 * @mixins OO.EventEmitter
2932 * @mixins OO.ui.GroupElement
2935 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
2936 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
2937 * @param {Object} [config] Configuration options
2938 * @cfg {boolean} [actions] Add an actions section opposite to the tools
2939 * @cfg {boolean} [shadow] Add a shadow below the toolbar
2941 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
2942 // Configuration initialization
2943 config
= config
|| {};
2945 // Parent constructor
2946 OO
.ui
.Toolbar
.super.call( this, config
);
2948 // Mixin constructors
2949 OO
.EventEmitter
.call( this );
2950 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
2953 this.toolFactory
= toolFactory
;
2954 this.toolGroupFactory
= toolGroupFactory
;
2957 this.$bar
= this.$( '<div>' );
2958 this.$actions
= this.$( '<div>' );
2959 this.initialized
= false;
2963 .add( this.$bar
).add( this.$group
).add( this.$actions
)
2964 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2967 this.$group
.addClass( 'oo-ui-toolbar-tools' );
2968 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
2969 if ( config
.actions
) {
2970 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
2971 this.$bar
.append( this.$actions
);
2973 this.$bar
.append( '<div style="clear:both"></div>' );
2974 if ( config
.shadow
) {
2975 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
2977 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
2982 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
2984 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
2985 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
2990 * Get the tool factory.
2992 * @return {OO.ui.ToolFactory} Tool factory
2994 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
2995 return this.toolFactory
;
2999 * Get the tool group factory.
3001 * @return {OO.Factory} Tool group factory
3003 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
3004 return this.toolGroupFactory
;
3008 * Handles mouse down events.
3010 * @param {jQuery.Event} e Mouse down event
3012 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
3013 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
3014 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
3015 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
3021 * Sets up handles and preloads required information for the toolbar to work.
3022 * This must be called immediately after it is attached to a visible document.
3024 OO
.ui
.Toolbar
.prototype.initialize = function () {
3025 this.initialized
= true;
3031 * Tools can be specified in the following ways:
3033 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3034 * - All tools in a group: `{ 'group': 'group-name' }`
3035 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
3037 * @param {Object.<string,Array>} groups List of tool group configurations
3038 * @param {Array|string} [groups.include] Tools to include
3039 * @param {Array|string} [groups.exclude] Tools to exclude
3040 * @param {Array|string} [groups.promote] Tools to promote to the beginning
3041 * @param {Array|string} [groups.demote] Tools to demote to the end
3043 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
3044 var i
, len
, type
, group
,
3046 defaultType
= 'bar';
3048 // Cleanup previous groups
3051 // Build out new groups
3052 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
3054 if ( group
.include
=== '*' ) {
3055 // Apply defaults to catch-all groups
3056 if ( group
.type
=== undefined ) {
3057 group
.type
= 'list';
3059 if ( group
.label
=== undefined ) {
3060 group
.label
= 'ooui-toolbar-more';
3063 // Check type has been registered
3064 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
3066 this.getToolGroupFactory().create( type
, this, $.extend( { '$': this.$ }, group
) )
3069 this.addItems( items
);
3073 * Remove all tools and groups from the toolbar.
3075 OO
.ui
.Toolbar
.prototype.reset = function () {
3080 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3081 this.items
[i
].destroy();
3087 * Destroys toolbar, removing event handlers and DOM elements.
3089 * Call this whenever you are done using a toolbar.
3091 OO
.ui
.Toolbar
.prototype.destroy = function () {
3093 this.$element
.remove();
3097 * Check if tool has not been used yet.
3099 * @param {string} name Symbolic name of tool
3100 * @return {boolean} Tool is available
3102 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
3103 return !this.tools
[name
];
3107 * Prevent tool from being used again.
3109 * @param {OO.ui.Tool} tool Tool to reserve
3111 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
3112 this.tools
[tool
.getName()] = tool
;
3116 * Allow tool to be used again.
3118 * @param {OO.ui.Tool} tool Tool to release
3120 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
3121 delete this.tools
[tool
.getName()];
3125 * Get accelerator label for tool.
3127 * This is a stub that should be overridden to provide access to accelerator information.
3129 * @param {string} name Symbolic name of tool
3130 * @return {string|undefined} Tool accelerator label if available
3132 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
3136 * Factory for tools.
3139 * @extends OO.Factory
3142 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3143 // Parent constructor
3144 OO
.ui
.ToolFactory
.super.call( this );
3149 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3154 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3155 var i
, len
, included
, promoted
, demoted
,
3159 // Collect included and not excluded tools
3160 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3163 promoted
= this.extract( promote
, used
);
3164 demoted
= this.extract( demote
, used
);
3167 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3168 if ( !used
[included
[i
]] ) {
3169 auto
.push( included
[i
] );
3173 return promoted
.concat( auto
).concat( demoted
);
3177 * Get a flat list of names from a list of names or groups.
3179 * Tools can be specified in the following ways:
3181 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3182 * - All tools in a group: `{ 'group': 'group-name' }`
3183 * - All tools: `'*'`
3186 * @param {Array|string} collection List of tools
3187 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3188 * names will be added as properties
3189 * @return {string[]} List of extracted names
3191 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3192 var i
, len
, item
, name
, tool
,
3195 if ( collection
=== '*' ) {
3196 for ( name
in this.registry
) {
3197 tool
= this.registry
[name
];
3199 // Only add tools by group name when auto-add is enabled
3200 tool
.static.autoAddToCatchall
&&
3201 // Exclude already used tools
3202 ( !used
|| !used
[name
] )
3210 } else if ( $.isArray( collection
) ) {
3211 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3212 item
= collection
[i
];
3213 // Allow plain strings as shorthand for named tools
3214 if ( typeof item
=== 'string' ) {
3215 item
= { 'name': item
};
3217 if ( OO
.isPlainObject( item
) ) {
3219 for ( name
in this.registry
) {
3220 tool
= this.registry
[name
];
3222 // Include tools with matching group
3223 tool
.static.group
=== item
.group
&&
3224 // Only add tools by group name when auto-add is enabled
3225 tool
.static.autoAddToGroup
&&
3226 // Exclude already used tools
3227 ( !used
|| !used
[name
] )
3235 // Include tools with matching name and exclude already used tools
3236 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3237 names
.push( item
.name
);
3239 used
[item
.name
] = true;
3248 * Collection of tools.
3250 * Tools can be specified in the following ways:
3252 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3253 * - All tools in a group: `{ 'group': 'group-name' }`
3254 * - All tools: `'*'`
3258 * @extends OO.ui.Widget
3259 * @mixins OO.ui.GroupElement
3262 * @param {OO.ui.Toolbar} toolbar
3263 * @param {Object} [config] Configuration options
3264 * @cfg {Array|string} [include=[]] List of tools to include
3265 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3266 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3267 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3269 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3270 // Configuration initialization
3271 config
= $.extend( true, {
3272 'aggregations': { 'disable': 'itemDisable' }
3275 // Parent constructor
3276 OO
.ui
.ToolGroup
.super.call( this, config
);
3278 // Mixin constructors
3279 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3282 this.toolbar
= toolbar
;
3284 this.pressed
= null;
3285 this.autoDisabled
= false;
3286 this.include
= config
.include
|| [];
3287 this.exclude
= config
.exclude
|| [];
3288 this.promote
= config
.promote
|| [];
3289 this.demote
= config
.demote
|| [];
3290 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3294 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3295 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3296 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3297 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3299 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3300 this.connect( this, { 'itemDisable': 'updateDisabled' } );
3303 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3305 .addClass( 'oo-ui-toolGroup' )
3306 .append( this.$group
);
3312 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3314 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3322 /* Static Properties */
3325 * Show labels in tooltips.
3328 * @property {boolean}
3331 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3334 * Show acceleration labels in tooltips.
3337 * @property {boolean}
3340 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3343 * Automatically disable the toolgroup when all tools are disabled
3346 * @property {boolean}
3349 OO
.ui
.ToolGroup
.static.autoDisable
= true;
3356 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
3357 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
3363 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
3364 var i
, item
, allDisabled
= true;
3366 if ( this.constructor.static.autoDisable
) {
3367 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
3368 item
= this.items
[i
];
3369 if ( !item
.isDisabled() ) {
3370 allDisabled
= false;
3374 this.autoDisabled
= allDisabled
;
3376 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
3380 * Handle mouse down events.
3382 * @param {jQuery.Event} e Mouse down event
3384 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3385 if ( !this.disabled
&& e
.which
=== 1 ) {
3386 this.pressed
= this.getTargetTool( e
);
3387 if ( this.pressed
) {
3388 this.pressed
.setActive( true );
3389 this.getElementDocument().addEventListener(
3390 'mouseup', this.onCapturedMouseUpHandler
, true
3398 * Handle captured mouse up events.
3400 * @param {Event} e Mouse up event
3402 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3403 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3404 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3405 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3406 this.onMouseUp( e
);
3410 * Handle mouse up events.
3412 * @param {jQuery.Event} e Mouse up event
3414 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3415 var tool
= this.getTargetTool( e
);
3417 if ( !this.disabled
&& e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3418 this.pressed
.onSelect();
3421 this.pressed
= null;
3426 * Handle mouse over events.
3428 * @param {jQuery.Event} e Mouse over event
3430 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3431 var tool
= this.getTargetTool( e
);
3433 if ( this.pressed
&& this.pressed
=== tool
) {
3434 this.pressed
.setActive( true );
3439 * Handle mouse out events.
3441 * @param {jQuery.Event} e Mouse out event
3443 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3444 var tool
= this.getTargetTool( e
);
3446 if ( this.pressed
&& this.pressed
=== tool
) {
3447 this.pressed
.setActive( false );
3452 * Get the closest tool to a jQuery.Event.
3454 * Only tool links are considered, which prevents other elements in the tool such as popups from
3455 * triggering tool group interactions.
3458 * @param {jQuery.Event} e
3459 * @return {OO.ui.Tool|null} Tool, `null` if none was found
3461 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3463 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3465 if ( $item
.length
) {
3466 tool
= $item
.parent().data( 'oo-ui-tool' );
3469 return tool
&& !tool
.isDisabled() ? tool
: null;
3473 * Handle tool registry register events.
3475 * If a tool is registered after the group is created, we must repopulate the list to account for:
3477 * - a tool being added that may be included
3478 * - a tool already included being overridden
3480 * @param {string} name Symbolic name of tool
3482 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3487 * Get the toolbar this group is in.
3489 * @return {OO.ui.Toolbar} Toolbar of group
3491 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3492 return this.toolbar
;
3496 * Add and remove tools based on configuration.
3498 OO
.ui
.ToolGroup
.prototype.populate = function () {
3499 var i
, len
, name
, tool
,
3500 toolFactory
= this.toolbar
.getToolFactory(),
3504 list
= this.toolbar
.getToolFactory().getTools(
3505 this.include
, this.exclude
, this.promote
, this.demote
3508 // Build a list of needed tools
3509 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3513 toolFactory
.lookup( name
) &&
3514 // Tool is available or is already in this group
3515 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3517 tool
= this.tools
[name
];
3519 // Auto-initialize tools on first use
3520 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3523 this.toolbar
.reserveTool( tool
);
3528 // Remove tools that are no longer needed
3529 for ( name
in this.tools
) {
3530 if ( !names
[name
] ) {
3531 this.tools
[name
].destroy();
3532 this.toolbar
.releaseTool( this.tools
[name
] );
3533 remove
.push( this.tools
[name
] );
3534 delete this.tools
[name
];
3537 if ( remove
.length
) {
3538 this.removeItems( remove
);
3540 // Update emptiness state
3542 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3544 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3546 // Re-add tools (moving existing ones to new locations)
3547 this.addItems( add
);
3548 // Disabled state may depend on items
3549 this.updateDisabled();
3553 * Destroy tool group.
3555 OO
.ui
.ToolGroup
.prototype.destroy = function () {
3559 this.toolbar
.getToolFactory().disconnect( this );
3560 for ( name
in this.tools
) {
3561 this.toolbar
.releaseTool( this.tools
[name
] );
3562 this.tools
[name
].disconnect( this ).destroy();
3563 delete this.tools
[name
];
3565 this.$element
.remove();
3568 * Factory for tools.
3571 * @extends OO.Factory
3574 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3575 // Parent constructor
3576 OO
.Factory
.call( this );
3579 defaultClasses
= this.constructor.static.getDefaultClasses();
3581 // Register default toolgroups
3582 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3583 this.register( defaultClasses
[i
] );
3589 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3591 /* Static Methods */
3594 * Get a default set of classes to be registered on construction
3595 * @return {Function[]} Default classes
3597 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3600 OO
.ui
.ListToolGroup
,
3605 * Layout made of a fieldset and optional legend.
3607 * Just add OO.ui.FieldLayout items.
3610 * @extends OO.ui.Layout
3611 * @mixins OO.ui.LabeledElement
3612 * @mixins OO.ui.IconedElement
3613 * @mixins OO.ui.GroupElement
3616 * @param {Object} [config] Configuration options
3617 * @cfg {string} [icon] Symbolic icon name
3618 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
3620 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
3621 // Config initialization
3622 config
= config
|| {};
3624 // Parent constructor
3625 OO
.ui
.FieldsetLayout
.super.call( this, config
);
3627 // Mixin constructors
3628 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
3629 OO
.ui
.LabeledElement
.call( this, this.$( '<legend>' ), config
);
3630 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3634 .addClass( 'oo-ui-fieldsetLayout' )
3635 .append( this.$icon
, this.$label
, this.$group
);
3636 if ( $.isArray( config
.items
) ) {
3637 this.addItems( config
.items
);
3643 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
3645 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
3646 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
3647 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
3649 /* Static Properties */
3651 OO
.ui
.FieldsetLayout
.static.tagName
= 'fieldset';
3653 * Layout made of a field and optional label.
3656 * @extends OO.ui.Layout
3657 * @mixins OO.ui.LabeledElement
3659 * Available label alignment modes include:
3660 * - 'left': Label is before the field and aligned away from it, best for when the user will be
3661 * scanning for a specific label in a form with many fields
3662 * - 'right': Label is before the field and aligned toward it, best for forms the user is very
3663 * familiar with and will tab through field checking quickly to verify which field they are in
3664 * - 'top': Label is before the field and above it, best for when the use will need to fill out all
3665 * fields from top to bottom in a form with few fields
3666 * - 'inline': Label is after the field and aligned toward it, best for small boolean fields like
3667 * checkboxes or radio buttons
3670 * @param {OO.ui.Widget} field Field widget
3671 * @param {Object} [config] Configuration options
3672 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
3674 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
3675 // Config initialization
3676 config
= $.extend( { 'align': 'left' }, config
);
3678 // Parent constructor
3679 OO
.ui
.FieldLayout
.super.call( this, config
);
3681 // Mixin constructors
3682 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
3685 this.$field
= this.$( '<div>' );
3690 if ( this.field
instanceof OO
.ui
.InputWidget
) {
3691 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
3695 this.$element
.addClass( 'oo-ui-fieldLayout' );
3697 .addClass( 'oo-ui-fieldLayout-field' )
3698 .append( this.field
.$element
);
3699 this.setAlignment( config
.align
);
3704 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
3706 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
3711 * Handles label mouse click events.
3714 * @param {jQuery.Event} e Mouse click event
3716 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
3717 this.field
.simulateLabelClick();
3724 * @returns {OO.ui.Widget} Field widget
3726 OO
.ui
.FieldLayout
.prototype.getField = function () {
3731 * Set the field alignment mode.
3733 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
3736 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
3737 if ( value
!== this.align
) {
3738 // Default to 'left'
3739 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
3743 if ( value
=== 'inline' ) {
3744 this.$element
.append( this.$field
, this.$label
);
3746 this.$element
.append( this.$label
, this.$field
);
3750 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
3753 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
3759 * Layout made of proportionally sized columns and rows.
3762 * @extends OO.ui.Layout
3765 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
3766 * @param {Object} [config] Configuration options
3767 * @cfg {number[]} [widths] Widths of columns as ratios
3768 * @cfg {number[]} [heights] Heights of columns as ratios
3770 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
3773 // Config initialization
3774 config
= config
|| {};
3776 // Parent constructor
3777 OO
.ui
.GridLayout
.super.call( this, config
);
3785 this.$element
.addClass( 'oo-ui-gridLayout' );
3786 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
3787 this.panels
.push( panels
[i
] );
3788 this.$element
.append( panels
[i
].$element
);
3790 if ( config
.widths
|| config
.heights
) {
3791 this.layout( config
.widths
|| [1], config
.heights
|| [1] );
3793 // Arrange in columns by default
3795 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
3798 this.layout( widths
, [1] );
3804 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
3816 /* Static Properties */
3818 OO
.ui
.GridLayout
.static.tagName
= 'div';
3823 * Set grid dimensions.
3826 * @param {number[]} widths Widths of columns as ratios
3827 * @param {number[]} heights Heights of rows as ratios
3829 * @throws {Error} If grid is not large enough to fit all panels
3831 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
3835 cols
= widths
.length
,
3836 rows
= heights
.length
;
3838 // Verify grid is big enough to fit panels
3839 if ( cols
* rows
< this.panels
.length
) {
3840 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
3843 // Sum up denominators
3844 for ( x
= 0; x
< cols
; x
++ ) {
3847 for ( y
= 0; y
< rows
; y
++ ) {
3853 for ( x
= 0; x
< cols
; x
++ ) {
3854 this.widths
[x
] = widths
[x
] / xd
;
3856 for ( y
= 0; y
< rows
; y
++ ) {
3857 this.heights
[y
] = heights
[y
] / yd
;
3861 this.emit( 'layout' );
3865 * Update panel positions and sizes.
3870 OO
.ui
.GridLayout
.prototype.update = function () {
3878 cols
= this.widths
.length
,
3879 rows
= this.heights
.length
;
3881 for ( y
= 0; y
< rows
; y
++ ) {
3882 for ( x
= 0; x
< cols
; x
++ ) {
3883 panel
= this.panels
[i
];
3884 width
= this.widths
[x
];
3885 height
= this.heights
[y
];
3887 'width': Math
.round( width
* 100 ) + '%',
3888 'height': Math
.round( height
* 100 ) + '%',
3889 'top': Math
.round( top
* 100 ) + '%'
3892 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
3893 dimensions
.right
= Math
.round( left
* 100 ) + '%';
3895 dimensions
.left
= Math
.round( left
* 100 ) + '%';
3897 panel
.$element
.css( dimensions
);
3905 this.emit( 'update' );
3909 * Get a panel at a given position.
3911 * The x and y position is affected by the current grid layout.
3914 * @param {number} x Horizontal position
3915 * @param {number} y Vertical position
3916 * @returns {OO.ui.PanelLayout} The panel at the given postion
3918 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
3919 return this.panels
[( x
* this.widths
.length
) + y
];
3922 * Layout containing a series of pages.
3925 * @extends OO.ui.Layout
3928 * @param {Object} [config] Configuration options
3929 * @cfg {boolean} [continuous=false] Show all pages, one after another
3930 * @cfg {boolean} [autoFocus=false] Focus on the first focusable element when changing to a page
3931 * @cfg {boolean} [outlined=false] Show an outline
3932 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
3933 * @cfg {Object[]} [adders] List of adders for controls, each with name, icon and title properties
3935 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
3936 // Initialize configuration
3937 config
= config
|| {};
3939 // Parent constructor
3940 OO
.ui
.BookletLayout
.super.call( this, config
);
3943 this.currentPageName
= null;
3945 this.ignoreFocus
= false;
3946 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
3947 this.autoFocus
= !!config
.autoFocus
;
3948 this.outlineVisible
= false;
3949 this.outlined
= !!config
.outlined
;
3950 if ( this.outlined
) {
3951 this.editable
= !!config
.editable
;
3952 this.adders
= config
.adders
|| null;
3953 this.outlineControlsWidget
= null;
3954 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
3955 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
3956 this.gridLayout
= new OO
.ui
.GridLayout(
3957 [this.outlinePanel
, this.stackLayout
], { '$': this.$, 'widths': [1, 2] }
3959 this.outlineVisible
= true;
3960 if ( this.editable
) {
3961 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
3963 { '$': this.$, 'adders': this.adders
}
3969 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
3970 if ( this.outlined
) {
3971 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
3973 if ( this.autoFocus
) {
3974 // Event 'focus' does not bubble, but 'focusin' does
3975 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
3979 this.$element
.addClass( 'oo-ui-bookletLayout' );
3980 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
3981 if ( this.outlined
) {
3982 this.outlinePanel
.$element
3983 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
3984 .append( this.outlineWidget
.$element
);
3985 if ( this.editable
) {
3986 this.outlinePanel
.$element
3987 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
3988 .append( this.outlineControlsWidget
.$element
);
3990 this.$element
.append( this.gridLayout
.$element
);
3992 this.$element
.append( this.stackLayout
.$element
);
3998 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
4004 * @param {OO.ui.PageLayout} page Current page
4009 * @param {OO.ui.PageLayout[]} page Added pages
4010 * @param {number} index Index pages were added at
4015 * @param {OO.ui.PageLayout[]} pages Removed pages
4021 * Handle stack layout focus.
4024 * @param {jQuery.Event} e Focusin event
4026 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
4029 if ( this.ignoreFocus
) {
4030 // Avoid recursion from programmatic focus trigger in #onStackLayoutSet
4034 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
4035 for ( name
in this.pages
) {
4036 if ( this.pages
[ name
].$element
[0] === $target
[0] ) {
4037 this.setPage( name
);
4044 * Handle stack layout set events.
4047 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
4049 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
4051 this.stackLayout
.$element
.find( ':focus' ).blur();
4052 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
4053 this.ignoreFocus
= true;
4054 if ( this.autoFocus
) {
4055 page
.$element
.find( ':input:first' ).focus();
4057 this.ignoreFocus
= false;
4063 * Handle outline widget select events.
4066 * @param {OO.ui.OptionWidget|null} item Selected item
4068 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
4070 this.setPage( item
.getData() );
4075 * Check if booklet has an outline.
4078 * @returns {boolean} Booklet is outlined
4080 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
4081 return this.outlined
;
4085 * Check if booklet has editing controls.
4088 * @returns {boolean} Booklet is outlined
4090 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
4091 return this.editable
;
4095 * Check if booklet has editing controls.
4098 * @returns {boolean} Booklet is outlined
4100 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
4101 return this.outlined
&& this.outlineVisible
;
4105 * Hide or show the outline.
4107 * @param {boolean} [show] Show outline, omit to invert current state
4110 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
4111 if ( this.outlined
) {
4112 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
4113 this.outlineVisible
= show
;
4114 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
4121 * Get the outline widget.
4124 * @param {OO.ui.PageLayout} page Page to be selected
4125 * @returns {OO.ui.PageLayout|null} Closest page to another
4127 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
4128 var next
, prev
, level
,
4129 pages
= this.stackLayout
.getItems(),
4130 index
= $.inArray( page
, pages
);
4132 if ( index
!== -1 ) {
4133 next
= pages
[index
+ 1];
4134 prev
= pages
[index
- 1];
4135 // Prefer adjacent pages at the same level
4136 if ( this.outlined
) {
4137 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
4140 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
4146 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
4152 return prev
|| next
|| null;
4156 * Get the outline widget.
4159 * @returns {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
4161 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
4162 return this.outlineWidget
;
4166 * Get the outline controls widget. If the outline is not editable, null is returned.
4169 * @returns {OO.ui.OutlineControlsWidget|null} The outline controls widget.
4171 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
4172 return this.outlineControlsWidget
;
4176 * Get a page by name.
4179 * @param {string} name Symbolic name of page
4180 * @returns {OO.ui.PageLayout|undefined} Page, if found
4182 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
4183 return this.pages
[name
];
4187 * Get the current page name.
4190 * @returns {string|null} Current page name
4192 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
4193 return this.currentPageName
;
4197 * Add a page to the layout.
4199 * When pages are added with the same names as existing pages, the existing pages will be
4200 * automatically removed before the new pages are added.
4203 * @param {OO.ui.PageLayout[]} pages Pages to add
4204 * @param {number} index Index to insert pages after
4208 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
4209 var i
, len
, name
, page
, item
, currentIndex
,
4210 stackLayoutPages
= this.stackLayout
.getItems(),
4214 // Remove pages with same names
4215 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4217 name
= page
.getName();
4219 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
4220 // Correct the insertion index
4221 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
4222 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
4225 remove
.push( this.pages
[name
] );
4228 if ( remove
.length
) {
4229 this.removePages( remove
);
4233 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4235 name
= page
.getName();
4236 this.pages
[page
.getName()] = page
;
4237 if ( this.outlined
) {
4238 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { '$': this.$ } );
4239 page
.setOutlineItem( item
);
4244 if ( this.outlined
&& items
.length
) {
4245 this.outlineWidget
.addItems( items
, index
);
4246 this.updateOutlineWidget();
4248 this.stackLayout
.addItems( pages
, index
);
4249 this.emit( 'add', pages
, index
);
4255 * Remove a page from the layout.
4261 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
4262 var i
, len
, name
, page
,
4265 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4267 name
= page
.getName();
4268 delete this.pages
[name
];
4269 if ( this.outlined
) {
4270 items
.push( this.outlineWidget
.getItemFromData( name
) );
4271 page
.setOutlineItem( null );
4274 if ( this.outlined
&& items
.length
) {
4275 this.outlineWidget
.removeItems( items
);
4276 this.updateOutlineWidget();
4278 this.stackLayout
.removeItems( pages
);
4279 this.emit( 'remove', pages
);
4285 * Clear all pages from the layout.
4291 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
4293 pages
= this.stackLayout
.getItems();
4296 this.currentPageName
= null;
4297 if ( this.outlined
) {
4298 this.outlineWidget
.clearItems();
4299 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4300 pages
[i
].setOutlineItem( null );
4303 this.stackLayout
.clearItems();
4305 this.emit( 'remove', pages
);
4311 * Set the current page by name.
4315 * @param {string} name Symbolic name of page
4317 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
4319 page
= this.pages
[name
];
4321 if ( name
!== this.currentPageName
) {
4322 if ( this.outlined
) {
4323 selectedItem
= this.outlineWidget
.getSelectedItem();
4324 if ( selectedItem
&& selectedItem
.getData() !== name
) {
4325 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
4329 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
4330 this.pages
[this.currentPageName
].setActive( false );
4332 this.currentPageName
= name
;
4333 this.stackLayout
.setItem( page
);
4334 page
.setActive( true );
4335 this.emit( 'set', page
);
4341 * Call this after adding or removing items from the OutlineWidget.
4346 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
4347 // Auto-select first item when nothing is selected anymore
4348 if ( !this.outlineWidget
.getSelectedItem() ) {
4349 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
4355 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
4358 * @extends OO.ui.Layout
4361 * @param {Object} [config] Configuration options
4362 * @cfg {boolean} [scrollable] Allow vertical scrolling
4363 * @cfg {boolean} [padded] Pad the content from the edges
4365 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
4366 // Config initialization
4367 config
= config
|| {};
4369 // Parent constructor
4370 OO
.ui
.PanelLayout
.super.call( this, config
);
4373 this.$element
.addClass( 'oo-ui-panelLayout' );
4374 if ( config
.scrollable
) {
4375 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
4378 if ( config
.padded
) {
4379 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
4382 // Add directionality class:
4383 this.$element
.addClass( 'oo-ui-' + OO
.ui
.Element
.getDir( this.$.context
) );
4388 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
4390 * Page within an OO.ui.BookletLayout.
4393 * @extends OO.ui.PanelLayout
4396 * @param {string} name Unique symbolic name of page
4397 * @param {Object} [config] Configuration options
4398 * @param {string} [outlineItem] Outline item widget
4400 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
4401 // Configuration initialization
4402 config
= $.extend( { 'scrollable': true }, config
);
4404 // Parent constructor
4405 OO
.ui
.PageLayout
.super.call( this, config
);
4409 this.outlineItem
= config
.outlineItem
|| null;
4410 this.active
= false;
4413 this.$element
.addClass( 'oo-ui-pageLayout' );
4418 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4424 * @param {boolean} active Page is active
4432 * @returns {string} Symbolic name of page
4434 OO
.ui
.PageLayout
.prototype.getName = function () {
4439 * Check if page is active.
4441 * @returns {boolean} Page is active
4443 OO
.ui
.PageLayout
.prototype.isActive = function () {
4450 * @returns {OO.ui.OutlineItemWidget|null} Outline item widget
4452 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
4453 return this.outlineItem
;
4459 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
4462 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
4463 this.outlineItem
= outlineItem
;
4468 * Set page active state.
4470 * @param {boolean} Page is active
4473 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
4476 if ( active
!== this.active
) {
4477 this.active
= active
;
4478 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
4479 this.emit( 'active', this.active
);
4483 * Layout containing a series of mutually exclusive pages.
4486 * @extends OO.ui.PanelLayout
4487 * @mixins OO.ui.GroupElement
4490 * @param {Object} [config] Configuration options
4491 * @cfg {boolean} [continuous=false] Show all pages, one after another
4492 * @cfg {string} [icon=''] Symbolic icon name
4493 * @cfg {OO.ui.Layout[]} [items] Layouts to add
4495 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4496 // Config initialization
4497 config
= $.extend( { 'scrollable': true }, config
);
4499 // Parent constructor
4500 OO
.ui
.StackLayout
.super.call( this, config
);
4502 // Mixin constructors
4503 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4506 this.currentItem
= null;
4507 this.continuous
= !!config
.continuous
;
4510 this.$element
.addClass( 'oo-ui-stackLayout' );
4511 if ( this.continuous
) {
4512 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4514 if ( $.isArray( config
.items
) ) {
4515 this.addItems( config
.items
);
4521 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4523 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4529 * @param {OO.ui.PanelLayout|null} [item] Current item
4537 * Adding an existing item (by value) will move it.
4540 * @param {OO.ui.PanelLayout[]} items Items to add
4541 * @param {number} [index] Index to insert items after
4544 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
4545 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
4547 if ( !this.currentItem
&& items
.length
) {
4548 this.setItem( items
[0] );
4557 * Items will be detached, not removed, so they can be used later.
4560 * @param {OO.ui.PanelLayout[]} items Items to remove
4563 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
4564 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
4565 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
4566 this.currentItem
= null;
4567 if ( !this.currentItem
&& this.items
.length
) {
4568 this.setItem( this.items
[0] );
4578 * Items will be detached, not removed, so they can be used later.
4583 OO
.ui
.StackLayout
.prototype.clearItems = function () {
4584 this.currentItem
= null;
4585 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
4593 * Any currently shown item will be hidden.
4596 * @param {OO.ui.PanelLayout} item Item to show
4599 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
4600 if ( item
!== this.currentItem
) {
4601 if ( !this.continuous
) {
4602 this.$items
.css( 'display', '' );
4604 if ( $.inArray( item
, this.items
) !== -1 ) {
4605 if ( !this.continuous
) {
4606 item
.$element
.css( 'display', 'block' );
4611 this.currentItem
= item
;
4612 this.emit( 'set', item
);
4618 * Horizontal bar layout of tools as icon buttons.
4622 * @extends OO.ui.ToolGroup
4625 * @param {OO.ui.Toolbar} toolbar
4626 * @param {Object} [config] Configuration options
4628 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
4629 // Parent constructor
4630 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
4633 this.$element
.addClass( 'oo-ui-barToolGroup' );
4638 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
4640 /* Static Properties */
4642 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
4644 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
4646 OO
.ui
.BarToolGroup
.static.name
= 'bar';
4648 * Popup list of tools with an icon and optional label.
4652 * @extends OO.ui.ToolGroup
4653 * @mixins OO.ui.IconedElement
4654 * @mixins OO.ui.IndicatedElement
4655 * @mixins OO.ui.LabeledElement
4656 * @mixins OO.ui.TitledElement
4657 * @mixins OO.ui.ClippableElement
4660 * @param {OO.ui.Toolbar} toolbar
4661 * @param {Object} [config] Configuration options
4663 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
4664 // Configuration initialization
4665 config
= config
|| {};
4667 // Parent constructor
4668 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
4670 // Mixin constructors
4671 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4672 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
4673 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
4674 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
4675 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
4678 this.active
= false;
4679 this.dragging
= false;
4680 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
4681 this.$handle
= this.$( '<span>' );
4685 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
4686 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
4691 .addClass( 'oo-ui-popupToolGroup-handle' )
4692 .append( this.$icon
, this.$label
, this.$indicator
);
4694 .addClass( 'oo-ui-popupToolGroup' )
4695 .prepend( this.$handle
);
4700 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
4702 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
4703 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
4704 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
4705 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
4706 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
4708 /* Static Properties */
4715 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
4717 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
4719 if ( this.isDisabled() && this.isElementAttached() ) {
4720 this.setActive( false );
4725 * Handle focus being lost.
4727 * The event is actually generated from a mouseup, so it is not a normal blur event object.
4730 * @param {jQuery.Event} e Mouse up event
4732 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
4733 // Only deactivate when clicking outside the dropdown element
4734 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
4735 this.setActive( false );
4742 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
4743 if ( !this.disabled
&& e
.which
=== 1 ) {
4744 this.setActive( false );
4746 return OO
.ui
.ToolGroup
.prototype.onMouseUp
.call( this, e
);
4750 * Handle mouse up events.
4753 * @param {jQuery.Event} e Mouse up event
4755 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
4760 * Handle mouse down events.
4763 * @param {jQuery.Event} e Mouse down event
4765 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
4766 if ( !this.disabled
&& e
.which
=== 1 ) {
4767 this.setActive( !this.active
);
4773 * Switch into active mode.
4775 * When active, mouseup events anywhere in the document will trigger deactivation.
4779 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
4781 if ( this.active
!== value
) {
4782 this.active
= value
;
4784 this.setClipping( true );
4785 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
4786 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
4788 this.setClipping( false );
4789 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
4790 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
4795 * Drop down list layout of tools as labeled icon buttons.
4799 * @extends OO.ui.PopupToolGroup
4802 * @param {OO.ui.Toolbar} toolbar
4803 * @param {Object} [config] Configuration options
4805 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
4806 // Parent constructor
4807 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
4810 this.$element
.addClass( 'oo-ui-listToolGroup' );
4815 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
4817 /* Static Properties */
4819 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
4821 OO
.ui
.ListToolGroup
.static.name
= 'list';
4823 * Drop down menu layout of tools as selectable menu items.
4827 * @extends OO.ui.PopupToolGroup
4830 * @param {OO.ui.Toolbar} toolbar
4831 * @param {Object} [config] Configuration options
4833 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
4834 // Configuration initialization
4835 config
= config
|| {};
4837 // Parent constructor
4838 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
4841 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
4844 this.$element
.addClass( 'oo-ui-menuToolGroup' );
4849 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
4851 /* Static Properties */
4853 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
4855 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
4860 * Handle the toolbar state being updated.
4862 * When the state changes, the title of each active item in the menu will be joined together and
4863 * used as a label for the group. The label will be empty if none of the items are active.
4867 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
4871 for ( name
in this.tools
) {
4872 if ( this.tools
[name
].isActive() ) {
4873 labelTexts
.push( this.tools
[name
].getTitle() );
4877 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
4880 * UserInterface popup tool.
4884 * @extends OO.ui.Tool
4885 * @mixins OO.ui.PopuppableElement
4888 * @param {OO.ui.Toolbar} toolbar
4889 * @param {Object} [config] Configuration options
4891 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
4892 // Parent constructor
4893 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
4895 // Mixin constructors
4896 OO
.ui
.PopuppableElement
.call( this, config
);
4900 .addClass( 'oo-ui-popupTool' )
4901 .append( this.popup
.$element
);
4906 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
4908 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
4913 * Handle the tool being selected.
4917 OO
.ui
.PopupTool
.prototype.onSelect = function () {
4918 if ( !this.disabled
) {
4919 if ( this.popup
.isVisible() ) {
4925 this.setActive( false );
4930 * Handle the toolbar state being updated.
4934 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
4935 this.setActive( false );
4940 * Mixin for OO.ui.Widget subclasses.
4942 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
4946 * @extends OO.ui.GroupElement
4949 * @param {jQuery} $group Container node, assigned to #$group
4950 * @param {Object} [config] Configuration options
4952 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
4953 // Parent constructor
4954 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
4959 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
4964 * Set the disabled state of the widget.
4966 * This will also update the disabled state of child widgets.
4969 * @param {boolean} disabled Disable widget
4972 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
4976 // Note this is calling OO.ui.Widget; we're assuming the class this is mixed into
4977 // is a subclass of OO.ui.Widget.
4978 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
4980 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
4982 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4983 this.items
[i
].updateDisabled();
4992 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
4999 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
5006 * Check if widget is disabled.
5008 * Checks parent if present, making disabled state inheritable.
5010 * @returns {boolean} Widget is disabled
5012 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
5013 return this.disabled
||
5014 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
5018 * Set group element is in.
5020 * @param {OO.ui.GroupElement|null} group Group element, null if none
5023 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
5025 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
5027 // Initialize item disabled states
5028 this.updateDisabled();
5033 * Creates an OO.ui.IconWidget object.
5036 * @extends OO.ui.Widget
5037 * @mixins OO.ui.IconedElement
5038 * @mixins OO.ui.TitledElement
5041 * @param {Object} [config] Configuration options
5043 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
5044 // Config intialization
5045 config
= config
|| {};
5047 // Parent constructor
5048 OO
.ui
.IconWidget
.super.call( this, config
);
5050 // Mixin constructors
5051 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
5052 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5055 this.$element
.addClass( 'oo-ui-iconWidget' );
5060 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
5062 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
5063 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
5065 /* Static Properties */
5067 OO
.ui
.IconWidget
.static.tagName
= 'span';
5069 * Creates an OO.ui.IndicatorWidget object.
5072 * @extends OO.ui.Widget
5073 * @mixins OO.ui.IndicatedElement
5074 * @mixins OO.ui.TitledElement
5077 * @param {Object} [config] Configuration options
5079 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
5080 // Config intialization
5081 config
= config
|| {};
5083 // Parent constructor
5084 OO
.ui
.IndicatorWidget
.super.call( this, config
);
5086 // Mixin constructors
5087 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
5088 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5091 this.$element
.addClass( 'oo-ui-indicatorWidget' );
5096 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
5098 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
5099 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
5101 /* Static Properties */
5103 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
5105 * Container for multiple related buttons.
5108 * @extends OO.ui.Widget
5109 * @mixins OO.ui.GroupElement
5112 * @param {Object} [config] Configuration options
5113 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
5115 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
5116 // Parent constructor
5117 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
5119 // Mixin constructors
5120 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
5123 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
5124 if ( $.isArray( config
.items
) ) {
5125 this.addItems( config
.items
);
5131 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
5133 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
5135 * Creates an OO.ui.ButtonWidget object.
5139 * @extends OO.ui.Widget
5140 * @mixins OO.ui.ButtonedElement
5141 * @mixins OO.ui.IconedElement
5142 * @mixins OO.ui.IndicatedElement
5143 * @mixins OO.ui.LabeledElement
5144 * @mixins OO.ui.TitledElement
5145 * @mixins OO.ui.FlaggableElement
5148 * @param {Object} [config] Configuration options
5149 * @cfg {string} [title=''] Title text
5150 * @cfg {string} [href] Hyperlink to visit when clicked
5151 * @cfg {string} [target] Target to open hyperlink in
5153 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
5154 // Configuration initialization
5155 config
= $.extend( { 'target': '_blank' }, config
);
5157 // Parent constructor
5158 OO
.ui
.ButtonWidget
.super.call( this, config
);
5160 // Mixin constructors
5161 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
5162 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5163 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5164 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5165 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
5166 OO
.ui
.FlaggableElement
.call( this, config
);
5169 this.isHyperlink
= typeof config
.href
=== 'string';
5173 'click': OO
.ui
.bind( this.onClick
, this ),
5174 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
5179 .append( this.$icon
, this.$label
, this.$indicator
)
5180 .attr( { 'href': config
.href
, 'target': config
.target
} );
5182 .addClass( 'oo-ui-buttonWidget' )
5183 .append( this.$button
);
5188 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
5190 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
5191 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
5192 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
5193 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
5194 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
5195 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
5206 * Handles mouse click events.
5209 * @param {jQuery.Event} e Mouse click event
5212 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
5213 if ( !this.disabled
) {
5214 this.emit( 'click' );
5215 if ( this.isHyperlink
) {
5223 * Handles keypress events.
5226 * @param {jQuery.Event} e Keypress event
5229 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
5230 if ( !this.disabled
&& e
.which
=== OO
.ui
.Keys
.SPACE
) {
5231 if ( this.isHyperlink
) {
5239 * Creates an OO.ui.InputWidget object.
5243 * @extends OO.ui.Widget
5246 * @param {Object} [config] Configuration options
5247 * @cfg {string} [name=''] HTML input name
5248 * @cfg {string} [value=''] Input value
5249 * @cfg {boolean} [readOnly=false] Prevent changes
5250 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
5252 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
5253 // Config intialization
5254 config
= $.extend( { 'readOnly': false }, config
);
5256 // Parent constructor
5257 OO
.ui
.InputWidget
.super.call( this, config
);
5260 this.$input
= this.getInputElement( config
);
5262 this.readOnly
= false;
5263 this.inputFilter
= config
.inputFilter
;
5266 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
5270 .attr( 'name', config
.name
)
5271 .prop( 'disabled', this.disabled
);
5272 this.setReadOnly( config
.readOnly
);
5273 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
5274 this.setValue( config
.value
);
5279 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
5291 * Get input element.
5294 * @param {Object} [config] Configuration options
5295 * @returns {jQuery} Input element
5297 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
5298 return this.$( '<input>' );
5302 * Handle potentially value-changing events.
5305 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
5307 OO
.ui
.InputWidget
.prototype.onEdit = function () {
5308 if ( !this.disabled
) {
5309 // Allow the stack to clear so the value will be updated
5310 setTimeout( OO
.ui
.bind( function () {
5311 this.setValue( this.$input
.val() );
5317 * Get the value of the input.
5320 * @returns {string} Input value
5322 OO
.ui
.InputWidget
.prototype.getValue = function () {
5327 * Sets the direction of the current input, either RTL or LTR
5330 * @param {boolean} isRTL
5332 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
5334 this.$input
.removeClass( 'oo-ui-ltr' );
5335 this.$input
.addClass( 'oo-ui-rtl' );
5337 this.$input
.removeClass( 'oo-ui-rtl' );
5338 this.$input
.addClass( 'oo-ui-ltr' );
5343 * Set the value of the input.
5346 * @param {string} value New value
5350 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
5351 value
= this.sanitizeValue( value
);
5352 if ( this.value
!== value
) {
5354 this.emit( 'change', this.value
);
5356 // Update the DOM if it has changed. Note that with sanitizeValue, it
5357 // is possible for the DOM value to change without this.value changing.
5358 if ( this.$input
.val() !== this.value
) {
5359 this.$input
.val( this.value
);
5365 * Sanitize incoming value.
5367 * Ensures value is a string, and converts undefined and null to empty strings.
5370 * @param {string} value Original value
5371 * @returns {string} Sanitized value
5373 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
5374 if ( value
=== undefined || value
=== null ) {
5376 } else if ( this.inputFilter
) {
5377 return this.inputFilter( String( value
) );
5379 return String( value
);
5384 * Simulate the behavior of clicking on a label bound to this input.
5388 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
5389 if ( !this.isDisabled() ) {
5390 if ( this.$input
.is( ':checkbox,:radio' ) ) {
5391 this.$input
.click();
5392 } else if ( this.$input
.is( ':input' ) ) {
5393 this.$input
.focus();
5399 * Check if the widget is read-only.
5402 * @param {boolean} Input is read-only
5404 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
5405 return this.readOnly
;
5409 * Set the read-only state of the widget.
5411 * This should probably change the widgets's appearance and prevent it from being used.
5414 * @param {boolean} state Make input read-only
5417 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
5418 this.readOnly
= !!state
;
5419 this.$input
.prop( 'readonly', this.readOnly
);
5426 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
5427 OO
.ui
.Widget
.prototype.setDisabled
.call( this, state
);
5428 if ( this.$input
) {
5429 this.$input
.prop( 'disabled', this.disabled
);
5434 * Creates an OO.ui.CheckboxInputWidget object.
5437 * @extends OO.ui.InputWidget
5440 * @param {Object} [config] Configuration options
5442 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
5443 // Parent constructor
5444 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
5447 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
5452 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
5459 * Get input element.
5461 * @returns {jQuery} Input element
5463 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
5464 return this.$( '<input type="checkbox" />' );
5468 * Get checked state of the checkbox
5470 * @returns {boolean} If the checkbox is checked
5472 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
5479 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
5481 if ( this.value
!== value
) {
5483 this.$input
.prop( 'checked', this.value
);
5484 this.emit( 'change', this.value
);
5491 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
5492 if ( !this.disabled
) {
5493 // Allow the stack to clear so the value will be updated
5494 setTimeout( OO
.ui
.bind( function () {
5495 this.setValue( this.$input
.prop( 'checked' ) );
5500 * Creates an OO.ui.LabelWidget object.
5503 * @extends OO.ui.Widget
5504 * @mixins OO.ui.LabeledElement
5507 * @param {Object} [config] Configuration options
5509 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
5510 // Config intialization
5511 config
= config
|| {};
5513 // Parent constructor
5514 OO
.ui
.LabelWidget
.super.call( this, config
);
5516 // Mixin constructors
5517 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
5520 this.input
= config
.input
;
5523 if ( this.input
instanceof OO
.ui
.InputWidget
) {
5524 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
5528 this.$element
.addClass( 'oo-ui-labelWidget' );
5533 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
5535 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
5537 /* Static Properties */
5539 OO
.ui
.LabelWidget
.static.tagName
= 'label';
5544 * Handles label mouse click events.
5547 * @param {jQuery.Event} e Mouse click event
5549 OO
.ui
.LabelWidget
.prototype.onClick = function () {
5550 this.input
.simulateLabelClick();
5554 * Lookup input widget.
5556 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
5557 * events on #lookupMenu to make use of selections.
5563 * @param {OO.ui.TextInputWidget} input Input widget
5564 * @param {Object} [config] Configuration options
5565 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
5567 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
5568 // Config intialization
5569 config
= config
|| {};
5572 this.lookupInput
= input
;
5573 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
5574 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
5575 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
5576 'input': this.lookupInput
,
5577 '$container': config
.$container
5579 this.lookupCache
= {};
5580 this.lookupQuery
= null;
5581 this.lookupRequest
= null;
5582 this.populating
= false;
5585 this.$overlay
.append( this.lookupMenu
.$element
);
5587 this.lookupInput
.$input
.on( {
5588 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
5589 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
5590 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
5592 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
5595 this.$element
.addClass( 'oo-ui-lookupWidget' );
5596 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
5602 * Handle input focus event.
5605 * @param {jQuery.Event} e Input focus event
5607 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
5608 this.openLookupMenu();
5612 * Handle input blur event.
5615 * @param {jQuery.Event} e Input blur event
5617 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
5618 this.lookupMenu
.hide();
5622 * Handle input mouse down event.
5625 * @param {jQuery.Event} e Input mouse down event
5627 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
5628 this.openLookupMenu();
5632 * Handle input change event.
5635 * @param {string} value New input value
5637 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
5638 this.openLookupMenu();
5647 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
5648 var value
= this.lookupInput
.getValue();
5650 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
5651 this.populateLookupMenu();
5652 if ( !this.lookupMenu
.isVisible() ) {
5653 this.lookupMenu
.show();
5656 this.lookupMenu
.clearItems();
5657 this.lookupMenu
.hide();
5664 * Populate lookup menu with current information.
5669 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
5670 if ( !this.populating
) {
5671 this.populating
= true;
5672 this.getLookupMenuItems()
5673 .done( OO
.ui
.bind( function ( items
) {
5674 this.lookupMenu
.clearItems();
5675 if ( items
.length
) {
5676 this.lookupMenu
.show();
5677 this.lookupMenu
.addItems( items
);
5678 this.initializeLookupMenuSelection();
5679 this.openLookupMenu();
5681 this.lookupMenu
.hide();
5683 this.populating
= false;
5685 .fail( OO
.ui
.bind( function () {
5686 this.lookupMenu
.clearItems();
5687 this.populating
= false;
5695 * Set selection in the lookup menu with current information.
5700 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
5701 if ( !this.lookupMenu
.getSelectedItem() ) {
5702 this.lookupMenu
.intializeSelection( this.lookupMenu
.getFirstSelectableItem() );
5704 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
5708 * Get lookup menu items for the current query.
5711 * @returns {jQuery.Promise} Promise object which will be passed menu items as the first argument
5714 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
5715 var value
= this.lookupInput
.getValue(),
5716 deferred
= $.Deferred();
5718 if ( value
&& value
!== this.lookupQuery
) {
5719 // Abort current request if query has changed
5720 if ( this.lookupRequest
) {
5721 this.lookupRequest
.abort();
5722 this.lookupQuery
= null;
5723 this.lookupRequest
= null;
5725 if ( value
in this.lookupCache
) {
5726 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5728 this.lookupQuery
= value
;
5729 this.lookupRequest
= this.getLookupRequest()
5730 .always( OO
.ui
.bind( function () {
5731 this.lookupQuery
= null;
5732 this.lookupRequest
= null;
5734 .done( OO
.ui
.bind( function ( data
) {
5735 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
5736 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5738 .fail( function () {
5742 this.lookupRequest
.always( OO
.ui
.bind( function () {
5747 return deferred
.promise();
5751 * Get a new request object of the current lookup query value.
5755 * @returns {jqXHR} jQuery AJAX object, or promise object with an .abort() method
5757 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
5758 // Stub, implemented in subclass
5763 * Handle successful lookup request.
5765 * Overriding methods should call #populateLookupMenu when results are available and cache results
5766 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
5770 * @param {Mixed} data Response from server
5772 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
5773 // Stub, implemented in subclass
5777 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
5781 * @param {Mixed} data Cached result data, usually an array
5782 * @returns {OO.ui.MenuItemWidget[]} Menu items
5784 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
5785 // Stub, implemented in subclass
5789 * Creates an OO.ui.OptionWidget object.
5793 * @extends OO.ui.Widget
5794 * @mixins OO.ui.IconedElement
5795 * @mixins OO.ui.LabeledElement
5796 * @mixins OO.ui.IndicatedElement
5797 * @mixins OO.ui.FlaggableElement
5800 * @param {Mixed} data Option data
5801 * @param {Object} [config] Configuration options
5802 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
5804 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
5805 // Config intialization
5806 config
= config
|| {};
5808 // Parent constructor
5809 OO
.ui
.OptionWidget
.super.call( this, config
);
5811 // Mixin constructors
5812 OO
.ui
.ItemWidget
.call( this );
5813 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5814 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5815 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5816 OO
.ui
.FlaggableElement
.call( this, config
);
5820 this.selected
= false;
5821 this.highlighted
= false;
5822 this.pressed
= false;
5826 .data( 'oo-ui-optionWidget', this )
5827 .attr( 'rel', config
.rel
)
5828 .addClass( 'oo-ui-optionWidget' )
5829 .append( this.$label
);
5831 .prepend( this.$icon
)
5832 .append( this.$indicator
);
5837 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
5839 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
5840 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
5841 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
5842 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
5843 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
5845 /* Static Properties */
5847 OO
.ui
.OptionWidget
.static.tagName
= 'li';
5849 OO
.ui
.OptionWidget
.static.selectable
= true;
5851 OO
.ui
.OptionWidget
.static.highlightable
= true;
5853 OO
.ui
.OptionWidget
.static.pressable
= true;
5855 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
5860 * Check if option can be selected.
5863 * @returns {boolean} Item is selectable
5865 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
5866 return this.constructor.static.selectable
&& !this.disabled
;
5870 * Check if option can be highlighted.
5873 * @returns {boolean} Item is highlightable
5875 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
5876 return this.constructor.static.highlightable
&& !this.disabled
;
5880 * Check if option can be pressed.
5883 * @returns {boolean} Item is pressable
5885 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
5886 return this.constructor.static.pressable
&& !this.disabled
;
5890 * Check if option is selected.
5893 * @returns {boolean} Item is selected
5895 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
5896 return this.selected
;
5900 * Check if option is highlighted.
5903 * @returns {boolean} Item is highlighted
5905 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
5906 return this.highlighted
;
5910 * Check if option is pressed.
5913 * @returns {boolean} Item is pressed
5915 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
5916 return this.pressed
;
5920 * Set selected state.
5923 * @param {boolean} [state=false] Select option
5926 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
5927 if ( !this.disabled
&& this.constructor.static.selectable
) {
5928 this.selected
= !!state
;
5929 if ( this.selected
) {
5930 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
5931 if ( this.constructor.static.scrollIntoViewOnSelect
) {
5932 this.scrollElementIntoView();
5935 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
5942 * Set highlighted state.
5945 * @param {boolean} [state=false] Highlight option
5948 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
5949 if ( !this.disabled
&& this.constructor.static.highlightable
) {
5950 this.highlighted
= !!state
;
5951 if ( this.highlighted
) {
5952 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
5954 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
5961 * Set pressed state.
5964 * @param {boolean} [state=false] Press option
5967 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
5968 if ( !this.disabled
&& this.constructor.static.pressable
) {
5969 this.pressed
= !!state
;
5970 if ( this.pressed
) {
5971 this.$element
.addClass( 'oo-ui-optionWidget-pressed' );
5973 this.$element
.removeClass( 'oo-ui-optionWidget-pressed' );
5980 * Make the option's highlight flash.
5982 * While flashing, the visual style of the pressed state is removed if present.
5985 * @param {Function} [done] Callback to execute when flash effect is complete.
5987 OO
.ui
.OptionWidget
.prototype.flash = function ( done
) {
5988 var $this = this.$element
;
5990 if ( !this.disabled
&& this.constructor.static.pressable
) {
5991 $this.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
5992 setTimeout( OO
.ui
.bind( function () {
5993 $this.addClass( 'oo-ui-optionWidget-highlighted' );
5995 // Restore original classes
5997 .toggleClass( 'oo-ui-optionWidget-highlighted', this.highlighted
)
5998 .toggleClass( 'oo-ui-optionWidget-pressed', this.pressed
);
5999 setTimeout( done
, 100 );
6009 * @returns {Mixed} Option data
6011 OO
.ui
.OptionWidget
.prototype.getData = function () {
6015 * Create an OO.ui.SelectWidget object.
6019 * @extends OO.ui.Widget
6020 * @mixins OO.ui.GroupElement
6023 * @param {Object} [config] Configuration options
6024 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
6026 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
6027 // Config intialization
6028 config
= config
|| {};
6030 // Parent constructor
6031 OO
.ui
.SelectWidget
.super.call( this, config
);
6033 // Mixin constructors
6034 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
6037 this.pressed
= false;
6038 this.selecting
= null;
6043 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
6044 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
6045 'mousemove': OO
.ui
.bind( this.onMouseMove
, this ),
6046 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
6047 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
6051 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
6052 if ( $.isArray( config
.items
) ) {
6053 this.addItems( config
.items
);
6059 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
6061 // Need to mixin base class as well
6062 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
6064 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
6070 * @param {OO.ui.OptionWidget|null} item Highlighted item
6075 * @param {OO.ui.OptionWidget|null} item Pressed item
6080 * @param {OO.ui.OptionWidget|null} item Selected item
6085 * @param {OO.ui.OptionWidget[]} items Added items
6086 * @param {number} index Index items were added at
6091 * @param {OO.ui.OptionWidget[]} items Removed items
6094 /* Static Properties */
6096 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
6101 * Handle mouse down events.
6105 * @param {jQuery.Event} e Mouse down event
6107 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
6110 if ( !this.disabled
&& e
.which
=== 1 ) {
6111 this.togglePressed( true );
6112 item
= this.getTargetItem( e
);
6113 if ( item
&& item
.isSelectable() ) {
6114 this.pressItem( item
);
6115 this.selecting
= item
;
6116 this.$( this.$.context
).one( 'mouseup', OO
.ui
.bind( this.onMouseUp
, this ) );
6123 * Handle mouse up events.
6127 * @param {jQuery.Event} e Mouse up event
6129 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
6132 this.togglePressed( false );
6133 if ( !this.selecting
) {
6134 item
= this.getTargetItem( e
);
6135 if ( item
&& item
.isSelectable() ) {
6136 this.selecting
= item
;
6139 if ( !this.disabled
&& e
.which
=== 1 && this.selecting
) {
6140 this.pressItem( null );
6141 this.selectItem( this.selecting
);
6142 this.selecting
= null;
6149 * Handle mouse move events.
6153 * @param {jQuery.Event} e Mouse move event
6155 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
6158 if ( !this.disabled
&& this.pressed
) {
6159 item
= this.getTargetItem( e
);
6160 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
6161 this.pressItem( item
);
6162 this.selecting
= item
;
6169 * Handle mouse over events.
6173 * @param {jQuery.Event} e Mouse over event
6175 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
6178 if ( !this.disabled
) {
6179 item
= this.getTargetItem( e
);
6180 if ( item
&& item
.isHighlightable() ) {
6181 this.highlightItem( item
);
6188 * Handle mouse leave events.
6192 * @param {jQuery.Event} e Mouse over event
6194 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
6195 if ( !this.disabled
) {
6196 this.highlightItem( null );
6202 * Get the closest item to a jQuery.Event.
6206 * @param {jQuery.Event} e
6207 * @returns {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
6209 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
6210 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
6211 if ( $item
.length
) {
6212 return $item
.data( 'oo-ui-optionWidget' );
6218 * Get selected item.
6221 * @returns {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
6223 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
6226 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6227 if ( this.items
[i
].isSelected() ) {
6228 return this.items
[i
];
6235 * Get highlighted item.
6238 * @returns {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
6240 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
6243 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6244 if ( this.items
[i
].isHighlighted() ) {
6245 return this.items
[i
];
6252 * Get an existing item with equivilant data.
6255 * @param {Object} data Item data to search for
6256 * @returns {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
6258 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
6259 var hash
= OO
.getHash( data
);
6261 if ( hash
in this.hashes
) {
6262 return this.hashes
[hash
];
6269 * Toggle pressed state.
6271 * @param {boolean} pressed An option is being pressed
6273 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
6274 if ( pressed
=== undefined ) {
6275 pressed
= !this.pressed
;
6277 if ( pressed
!== this.pressed
) {
6278 this.$element
.toggleClass( 'oo-ui-selectWidget-pressed', pressed
);
6279 this.$element
.toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
6280 this.pressed
= pressed
;
6285 * Highlight an item.
6287 * Highlighting is mutually exclusive.
6290 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
6294 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
6295 var i
, len
, highlighted
,
6298 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6299 highlighted
= this.items
[i
] === item
;
6300 if ( this.items
[i
].isHighlighted() !== highlighted
) {
6301 this.items
[i
].setHighlighted( highlighted
);
6306 this.emit( 'highlight', item
);
6316 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6320 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
6321 var i
, len
, selected
,
6324 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6325 selected
= this.items
[i
] === item
;
6326 if ( this.items
[i
].isSelected() !== selected
) {
6327 this.items
[i
].setSelected( selected
);
6332 this.emit( 'select', item
);
6342 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
6346 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
6347 var i
, len
, pressed
,
6350 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6351 pressed
= this.items
[i
] === item
;
6352 if ( this.items
[i
].isPressed() !== pressed
) {
6353 this.items
[i
].setPressed( pressed
);
6358 this.emit( 'press', item
);
6365 * Setup selection and highlighting.
6367 * This should be used to synchronize the UI with the model without emitting events that would in
6368 * turn update the model.
6370 * @param {OO.ui.OptionWidget} [item] Item to select
6373 OO
.ui
.SelectWidget
.prototype.intializeSelection = function ( item
) {
6374 var i
, len
, selected
;
6376 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6377 selected
= this.items
[i
] === item
;
6378 this.items
[i
].setSelected( selected
);
6379 this.items
[i
].setHighlighted( selected
);
6386 * Get an item relative to another one.
6389 * @param {OO.ui.OptionWidget} item Item to start at
6390 * @param {number} direction Direction to move in
6391 * @returns {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
6393 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
6394 var inc
= direction
> 0 ? 1 : -1,
6395 len
= this.items
.length
,
6396 index
= item
instanceof OO
.ui
.OptionWidget
?
6397 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
6398 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
6400 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
6401 Math
.max( index
, -1 ) :
6402 // Default to n-1 instead of -1, if nothing is selected let's start at the end
6403 Math
.min( index
, len
);
6406 i
= ( i
+ inc
+ len
) % len
;
6407 item
= this.items
[i
];
6408 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6411 // Stop iterating when we've looped all the way around
6412 if ( i
=== stopAt
) {
6420 * Get the next selectable item.
6423 * @returns {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
6425 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
6428 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6429 item
= this.items
[i
];
6430 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6441 * When items are added with the same values as existing items, the existing items will be
6442 * automatically removed before the new items are added.
6445 * @param {OO.ui.OptionWidget[]} items Items to add
6446 * @param {number} [index] Index to insert items after
6450 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
6451 var i
, len
, item
, hash
,
6454 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6456 hash
= OO
.getHash( item
.getData() );
6457 if ( hash
in this.hashes
) {
6458 // Remove item with same value
6459 remove
.push( this.hashes
[hash
] );
6461 this.hashes
[hash
] = item
;
6463 if ( remove
.length
) {
6464 this.removeItems( remove
);
6467 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
6469 // Always provide an index, even if it was omitted
6470 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
6478 * Items will be detached, not removed, so they can be used later.
6481 * @param {OO.ui.OptionWidget[]} items Items to remove
6485 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
6486 var i
, len
, item
, hash
;
6488 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6490 hash
= OO
.getHash( item
.getData() );
6491 if ( hash
in this.hashes
) {
6492 // Remove existing item
6493 delete this.hashes
[hash
];
6495 if ( item
.isSelected() ) {
6496 this.selectItem( null );
6499 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
6501 this.emit( 'remove', items
);
6509 * Items will be detached, not removed, so they can be used later.
6515 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
6516 var items
= this.items
.slice();
6520 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
6521 this.selectItem( null );
6523 this.emit( 'remove', items
);
6528 * Creates an OO.ui.MenuItemWidget object.
6531 * @extends OO.ui.OptionWidget
6534 * @param {Mixed} data Item data
6535 * @param {Object} [config] Configuration options
6537 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
6538 // Configuration initialization
6539 config
= $.extend( { 'icon': 'check' }, config
);
6541 // Parent constructor
6542 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
6545 this.$element
.addClass( 'oo-ui-menuItemWidget' );
6550 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
6552 * Create an OO.ui.MenuWidget object.
6555 * @extends OO.ui.SelectWidget
6556 * @mixins OO.ui.ClippableElement
6559 * @param {Object} [config] Configuration options
6560 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
6562 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
6563 // Config intialization
6564 config
= config
|| {};
6566 // Parent constructor
6567 OO
.ui
.MenuWidget
.super.call( this, config
);
6569 // Mixin constructors
6570 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
6573 this.newItems
= null;
6574 this.$input
= config
.input
? config
.input
.$input
: null;
6575 this.$previousFocus
= null;
6576 this.isolated
= !config
.input
;
6577 this.visible
= false;
6578 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
6581 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
6586 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
6588 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
6593 * Handles key down events.
6596 * @param {jQuery.Event} e Key down event
6598 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
6601 highlightItem
= this.getHighlightedItem();
6603 if ( !this.disabled
&& this.visible
) {
6604 if ( !highlightItem
) {
6605 highlightItem
= this.getSelectedItem();
6607 switch ( e
.keyCode
) {
6608 case OO
.ui
.Keys
.ENTER
:
6609 this.selectItem( highlightItem
);
6613 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
6616 case OO
.ui
.Keys
.DOWN
:
6617 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
6620 case OO
.ui
.Keys
.ESCAPE
:
6621 if ( highlightItem
) {
6622 highlightItem
.setHighlighted( false );
6630 this.highlightItem( nextItem
);
6631 nextItem
.scrollElementIntoView();
6636 e
.stopPropagation();
6643 * Check if the menu is visible.
6646 * @returns {boolean} Menu is visible
6648 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
6649 return this.visible
;
6653 * Bind key down listener
6657 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
6658 if ( this.$input
) {
6659 this.$input
.on( 'keydown', this.onKeyDownHandler
);
6661 // Capture menu navigation keys
6662 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
6667 * Unbind key down listener
6671 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
6672 if ( this.$input
) {
6673 this.$input
.off( 'keydown' );
6675 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
6682 * The menu will stay open if an item is silently selected.
6685 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6688 OO
.ui
.MenuWidget
.prototype.selectItem = function ( item
) {
6690 OO
.ui
.SelectWidget
.prototype.selectItem
.call( this, item
);
6692 if ( !this.disabled
) {
6694 this.disabled
= true;
6695 item
.flash( OO
.ui
.bind( function () {
6697 this.disabled
= false;
6710 * Adding an existing item (by value) will move it.
6713 * @param {OO.ui.MenuItemWidget[]} items Items to add
6714 * @param {number} [index] Index to insert items after
6717 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
6721 OO
.ui
.SelectWidget
.prototype.addItems
.call( this, items
, index
);
6724 if ( !this.newItems
) {
6728 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6730 if ( this.visible
) {
6731 // Defer fitting label until
6734 this.newItems
.push( item
);
6747 OO
.ui
.MenuWidget
.prototype.show = function () {
6750 if ( this.items
.length
) {
6751 this.$element
.show();
6752 this.visible
= true;
6753 this.bindKeyDownListener();
6755 // Change focus to enable keyboard navigation
6756 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
6757 this.$previousFocus
= this.$( ':focus' );
6758 this.$input
.focus();
6760 if ( this.newItems
&& this.newItems
.length
) {
6761 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
6762 this.newItems
[i
].fitLabel();
6764 this.newItems
= null;
6767 this.setClipping( true );
6779 OO
.ui
.MenuWidget
.prototype.hide = function () {
6780 this.$element
.hide();
6781 this.visible
= false;
6782 this.unbindKeyDownListener();
6784 if ( this.isolated
&& this.$previousFocus
) {
6785 this.$previousFocus
.focus();
6786 this.$previousFocus
= null;
6789 this.setClipping( false );
6794 * Inline menu of options.
6797 * @extends OO.ui.Widget
6798 * @mixins OO.ui.IconedElement
6799 * @mixins OO.ui.IndicatedElement
6800 * @mixins OO.ui.LabeledElement
6801 * @mixins OO.ui.TitledElement
6804 * @param {Object} [config] Configuration options
6805 * @cfg {Object} [menu] Configuration options to pass to menu widget
6807 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
6808 // Configuration initialization
6809 config
= $.extend( { 'indicator': 'down' }, config
);
6811 // Parent constructor
6812 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
6814 // Mixin constructors
6815 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6816 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6817 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6818 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
6821 this.menu
= new OO
.ui
.MenuWidget( $.extend( { '$': this.$ }, config
.menu
) );
6822 this.$handle
= this.$( '<span>' );
6825 this.$element
.on( { 'click': OO
.ui
.bind( this.onClick
, this ) } );
6826 this.menu
.connect( this, { 'select': 'onMenuSelect' } );
6830 .addClass( 'oo-ui-inlineMenuWidget-handle' )
6831 .append( this.$icon
, this.$label
, this.$indicator
);
6833 .addClass( 'oo-ui-inlineMenuWidget' )
6834 .append( this.$handle
, this.menu
.$element
);
6839 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
6841 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
6842 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
6843 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
6844 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
6851 * @return {OO.ui.MenuWidget} Menu of widget
6853 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
6858 * Handles menu select events.
6861 * @param {OO.ui.MenuItemWidget} item Selected menu item
6863 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
6864 var selectedLabel
= item
.getLabel();
6866 // If the label is a DOM element, clone it, because setLabel will append() it
6867 if ( selectedLabel
instanceof jQuery
) {
6868 selectedLabel
= selectedLabel
.clone();
6871 this.setLabel( selectedLabel
);
6875 * Handles mouse click events.
6878 * @param {jQuery.Event} e Mouse click event
6880 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
6881 // Skip clicks within the menu
6882 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
6886 if ( !this.disabled
) {
6887 if ( this.menu
.isVisible() ) {
6896 * Creates an OO.ui.MenuSectionItemWidget object.
6899 * @extends OO.ui.OptionWidget
6902 * @param {Mixed} data Item data
6903 * @param {Object} [config] Configuration options
6905 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
6906 // Parent constructor
6907 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
6910 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
6915 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
6917 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
6919 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
6921 * Create an OO.ui.OutlineWidget object.
6924 * @extends OO.ui.SelectWidget
6927 * @param {Object} [config] Configuration options
6929 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
6930 // Config intialization
6931 config
= config
|| {};
6933 // Parent constructor
6934 OO
.ui
.OutlineWidget
.super.call( this, config
);
6937 this.$element
.addClass( 'oo-ui-outlineWidget' );
6942 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
6944 * Creates an OO.ui.OutlineControlsWidget object.
6949 * @param {OO.ui.OutlineWidget} outline Outline to control
6950 * @param {Object} [config] Configuration options
6952 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
6953 // Configuration initialization
6954 config
= $.extend( { 'icon': 'add-item' }, config
);
6956 // Parent constructor
6957 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
6959 // Mixin constructors
6960 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
6961 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
6964 this.outline
= outline
;
6965 this.$movers
= this.$( '<div>' );
6966 this.upButton
= new OO
.ui
.ButtonWidget( {
6970 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
6972 this.downButton
= new OO
.ui
.ButtonWidget( {
6976 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
6978 this.removeButton
= new OO
.ui
.ButtonWidget( {
6982 'title': OO
.ui
.msg( 'ooui-outline-control-remove' )
6986 outline
.connect( this, {
6987 'select': 'onOutlineChange',
6988 'add': 'onOutlineChange',
6989 'remove': 'onOutlineChange'
6991 this.upButton
.connect( this, { 'click': ['emit', 'move', -1] } );
6992 this.downButton
.connect( this, { 'click': ['emit', 'move', 1] } );
6993 this.removeButton
.connect( this, { 'click': ['emit', 'remove'] } );
6996 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
6997 this.$group
.addClass( 'oo-ui-outlineControlsWidget-adders' );
6999 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7000 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7001 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7006 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7008 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7009 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7015 * @param {number} places Number of places to move
7025 * Handle outline change events.
7029 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7030 var i
, len
, firstMovable
, lastMovable
,
7031 items
= this.outline
.getItems(),
7032 selectedItem
= this.outline
.getSelectedItem(),
7033 movable
= selectedItem
&& selectedItem
.isMovable(),
7034 removable
= selectedItem
&& selectedItem
.isRemovable();
7039 while ( ++i
< len
) {
7040 if ( items
[i
].isMovable() ) {
7041 firstMovable
= items
[i
];
7047 if ( items
[i
].isMovable() ) {
7048 lastMovable
= items
[i
];
7053 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7054 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7055 this.removeButton
.setDisabled( !removable
);
7058 * Creates an OO.ui.OutlineItemWidget object.
7061 * @extends OO.ui.OptionWidget
7064 * @param {Mixed} data Item data
7065 * @param {Object} [config] Configuration options
7066 * @cfg {number} [level] Indentation level
7067 * @cfg {boolean} [movable] Allow modification from outline controls
7069 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
7070 // Config intialization
7071 config
= config
|| {};
7073 // Parent constructor
7074 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
7078 this.movable
= !!config
.movable
;
7079 this.removable
= !!config
.removable
;
7082 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
7083 this.setLevel( config
.level
);
7088 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
7090 /* Static Properties */
7092 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
7094 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
7096 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
7098 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
7103 * Check if item is movable.
7105 * Movablilty is used by outline controls.
7107 * @returns {boolean} Item is movable
7109 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
7110 return this.movable
;
7114 * Check if item is removable.
7116 * Removablilty is used by outline controls.
7118 * @returns {boolean} Item is removable
7120 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
7121 return this.removable
;
7125 * Get indentation level.
7127 * @returns {number} Indentation level
7129 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
7136 * Movablilty is used by outline controls.
7138 * @param {boolean} movable Item is movable
7141 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
7142 this.movable
= !!movable
;
7149 * Removablilty is used by outline controls.
7151 * @param {boolean} movable Item is removable
7154 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
7155 this.removable
= !!removable
;
7160 * Set indentation level.
7163 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
7166 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
7167 var levels
= this.constructor.static.levels
,
7168 levelClass
= this.constructor.static.levelClass
,
7171 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
7173 if ( this.level
=== i
) {
7174 this.$element
.addClass( levelClass
+ i
);
7176 this.$element
.removeClass( levelClass
+ i
);
7183 * Create an OO.ui.ButtonOptionWidget object.
7186 * @extends OO.ui.OptionWidget
7187 * @mixins OO.ui.ButtonedElement
7188 * @mixins OO.ui.FlaggableElement
7191 * @param {Mixed} data Option data
7192 * @param {Object} [config] Configuration options
7194 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
7195 // Parent constructor
7196 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
7198 // Mixin constructors
7199 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7200 OO
.ui
.FlaggableElement
.call( this, config
);
7203 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
7204 this.$button
.append( this.$element
.contents() );
7205 this.$element
.append( this.$button
);
7210 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
7212 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
7213 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
7220 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
7221 OO
.ui
.OptionWidget
.prototype.setSelected
.call( this, state
);
7223 this.setActive( state
);
7228 * Create an OO.ui.ButtonSelect object.
7231 * @extends OO.ui.SelectWidget
7234 * @param {Object} [config] Configuration options
7236 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
7237 // Parent constructor
7238 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
7241 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
7246 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
7248 * Creates an OO.ui.PopupWidget object.
7251 * @extends OO.ui.Widget
7252 * @mixins OO.ui.LabeledElement
7255 * @param {Object} [config] Configuration options
7256 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
7257 * @cfg {string} [align='center'] Alignment of popup to origin
7258 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
7259 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
7260 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
7261 * @cfg {boolean} [head] Show label and close button at the top
7263 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
7264 // Config intialization
7265 config
= config
|| {};
7267 // Parent constructor
7268 OO
.ui
.PopupWidget
.super.call( this, config
);
7270 // Mixin constructors
7271 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
7272 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
7275 this.visible
= false;
7276 this.$popup
= this.$( '<div>' );
7277 this.$head
= this.$( '<div>' );
7278 this.$body
= this.$clippable
;
7279 this.$tail
= this.$( '<div>' );
7280 this.$container
= config
.$container
|| this.$( 'body' );
7281 this.autoClose
= !!config
.autoClose
;
7282 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
7283 this.transitionTimeout
= null;
7285 this.align
= config
.align
|| 'center';
7286 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
7287 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
7290 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
7293 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
7294 this.$body
.addClass( 'oo-ui-popupWidget-body' );
7295 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
7297 .addClass( 'oo-ui-popupWidget-head' )
7298 .append( this.$label
, this.closeButton
.$element
);
7299 if ( !config
.head
) {
7303 .addClass( 'oo-ui-popupWidget-popup' )
7304 .append( this.$head
, this.$body
);
7305 this.$element
.hide()
7306 .addClass( 'oo-ui-popupWidget' )
7307 .append( this.$popup
, this.$tail
);
7312 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
7314 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
7316 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
7331 * Handles mouse down events.
7334 * @param {jQuery.Event} e Mouse down event
7336 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
7339 !$.contains( this.$element
[0], e
.target
) &&
7340 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
7347 * Bind mouse down listener
7351 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
7352 // Capture clicks outside popup
7353 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
7357 * Handles close button click events.
7361 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
7362 if ( this.visible
) {
7368 * Unbind mouse down listener
7372 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
7373 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
7377 * Check if the popup is visible.
7380 * @returns {boolean} Popup is visible
7382 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
7383 return this.visible
;
7387 * Set whether to show a tail.
7390 * @returns {boolean} Make tail visible
7392 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
7394 if ( this.tail
!== value
) {
7397 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
7399 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
7405 * Check if showing a tail.
7408 * @returns {boolean} tail is visible
7410 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
7421 OO
.ui
.PopupWidget
.prototype.show = function () {
7422 if ( !this.visible
) {
7423 this.setClipping( true );
7424 this.$element
.show();
7425 this.visible
= true;
7426 this.emit( 'show' );
7427 if ( this.autoClose
) {
7428 this.bindMouseDownListener();
7441 OO
.ui
.PopupWidget
.prototype.hide = function () {
7442 if ( this.visible
) {
7443 this.setClipping( false );
7444 this.$element
.hide();
7445 this.visible
= false;
7446 this.emit( 'hide' );
7447 if ( this.autoClose
) {
7448 this.unbindMouseDownListener();
7455 * Updates the position and size.
7458 * @param {number} width Width
7459 * @param {number} height Height
7460 * @param {boolean} [transition=false] Use a smooth transition
7463 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
7465 originOffset
= Math
.round( this.$element
.offset().left
),
7466 containerLeft
= Math
.round( this.$container
.offset().left
),
7467 containerWidth
= this.$container
.innerWidth(),
7468 containerRight
= containerLeft
+ containerWidth
,
7469 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
7470 popupLeft
= popupOffset
- padding
,
7471 popupRight
= popupOffset
+ padding
+ width
+ padding
,
7472 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
7473 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
7475 // Prevent transition from being interrupted
7476 clearTimeout( this.transitionTimeout
);
7478 // Enable transition
7479 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
7482 if ( overlapRight
< 0 ) {
7483 popupOffset
+= overlapRight
;
7484 } else if ( overlapLeft
< 0 ) {
7485 popupOffset
-= overlapLeft
;
7488 // Position body relative to anchor and resize
7490 'left': popupOffset
,
7492 'height': height
=== undefined ? 'auto' : height
7496 // Prevent transitioning after transition is complete
7497 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
7498 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7501 // Prevent transitioning immediately
7502 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7508 * Button that shows and hides a popup.
7511 * @extends OO.ui.ButtonWidget
7512 * @mixins OO.ui.PopuppableElement
7515 * @param {Object} [config] Configuration options
7517 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7518 // Parent constructor
7519 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7521 // Mixin constructors
7522 OO
.ui
.PopuppableElement
.call( this, config
);
7526 .addClass( 'oo-ui-popupButtonWidget' )
7527 .append( this.popup
.$element
);
7532 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
7534 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
7539 * Handles mouse click events.
7542 * @param {jQuery.Event} e Mouse click event
7544 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
7545 // Skip clicks within the popup
7546 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
7550 if ( !this.disabled
) {
7551 if ( this.popup
.isVisible() ) {
7556 OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
7561 * Creates an OO.ui.SearchWidget object.
7564 * @extends OO.ui.Widget
7567 * @param {Object} [config] Configuration options
7568 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
7569 * @cfg {string} [value] Initial query value
7571 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
7572 // Configuration intialization
7573 config
= config
|| {};
7575 // Parent constructor
7576 OO
.ui
.SearchWidget
.super.call( this, config
);
7579 this.query
= new OO
.ui
.TextInputWidget( {
7582 'placeholder': config
.placeholder
,
7583 'value': config
.value
7585 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
7586 this.$query
= this.$( '<div>' );
7587 this.$results
= this.$( '<div>' );
7590 this.query
.connect( this, {
7591 'change': 'onQueryChange',
7592 'enter': 'onQueryEnter'
7594 this.results
.connect( this, {
7595 'highlight': 'onResultsHighlight',
7596 'select': 'onResultsSelect'
7598 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
7602 .addClass( 'oo-ui-searchWidget-query' )
7603 .append( this.query
.$element
);
7605 .addClass( 'oo-ui-searchWidget-results' )
7606 .append( this.results
.$element
);
7608 .addClass( 'oo-ui-searchWidget' )
7609 .append( this.$results
, this.$query
);
7614 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
7620 * @param {Object|null} item Item data or null if no item is highlighted
7625 * @param {Object|null} item Item data or null if no item is selected
7631 * Handle query key down events.
7634 * @param {jQuery.Event} e Key down event
7636 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
7637 var highlightedItem
, nextItem
,
7638 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
7641 highlightedItem
= this.results
.getHighlightedItem();
7642 if ( !highlightedItem
) {
7643 highlightedItem
= this.results
.getSelectedItem();
7645 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
7646 this.results
.highlightItem( nextItem
);
7647 nextItem
.scrollElementIntoView();
7652 * Handle select widget select events.
7654 * Clears existing results. Subclasses should repopulate items according to new query.
7657 * @param {string} value New value
7659 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
7661 this.results
.clearItems();
7665 * Handle select widget enter key events.
7667 * Selects highlighted item.
7670 * @param {string} value New value
7672 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
7674 this.results
.selectItem( this.results
.getHighlightedItem() );
7678 * Handle select widget highlight events.
7681 * @param {OO.ui.OptionWidget} item Highlighted item
7684 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
7685 this.emit( 'highlight', item
? item
.getData() : null );
7689 * Handle select widget select events.
7692 * @param {OO.ui.OptionWidget} item Selected item
7695 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
7696 this.emit( 'select', item
? item
.getData() : null );
7700 * Get the query input.
7703 * @returns {OO.ui.TextInputWidget} Query input
7705 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
7710 * Reset the widget to initial value.
7712 OO
.ui
.SearchWidget
.prototype.clear = function () {
7713 this.query
.setValue( '' );
7717 * Get the results list.
7720 * @returns {OO.ui.SelectWidget} Select list
7722 OO
.ui
.SearchWidget
.prototype.getResults = function () {
7723 return this.results
;
7726 * Creates an OO.ui.TextInputWidget object.
7729 * @extends OO.ui.InputWidget
7732 * @param {Object} [config] Configuration options
7733 * @cfg {string} [placeholder] Placeholder text
7734 * @cfg {string} [icon] Symbolic name of icon
7735 * @cfg {boolean} [multiline=false] Allow multiple lines of text
7736 * @cfg {boolean} [autosize=false] Automatically resize to fit content
7737 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
7739 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
7740 config
= $.extend( { 'maxRows': 10 }, config
);
7742 // Parent constructor
7743 OO
.ui
.TextInputWidget
.super.call( this, config
);
7747 this.multiline
= !!config
.multiline
;
7748 this.autosize
= !!config
.autosize
;
7749 this.maxRows
= config
.maxRows
;
7752 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
7753 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
7756 this.$element
.addClass( 'oo-ui-textInputWidget' );
7757 if ( config
.icon
) {
7758 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
7759 this.$element
.append(
7761 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
7762 .mousedown( OO
.ui
.bind( function () {
7763 this.$input
.focus();
7768 if ( config
.placeholder
) {
7769 this.$input
.attr( 'placeholder', config
.placeholder
);
7775 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
7780 * User presses enter inside the text box.
7782 * Not called if input is multiline.
7790 * Handles key press events.
7792 * @param {jQuery.Event} e Key press event
7793 * @fires enter If enter key is pressed and input is not multiline
7795 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
7796 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
7797 this.emit( 'enter' );
7802 * Handles element attach events.
7804 * @param {jQuery.Event} e Element attach event
7806 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
7813 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
7817 return OO
.ui
.InputWidget
.prototype.onEdit
.call( this );
7821 * Automatically adjust the size of the text input.
7823 * This only affects multi-line inputs that are auto-sized.
7827 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
7828 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
7830 if ( this.multiline
&& this.autosize
) {
7831 $clone
= this.$input
.clone()
7832 .val( this.$input
.val() )
7833 .css( { 'height': 0 } )
7834 .insertAfter( this.$input
);
7835 // Set inline height property to 0 to measure scroll height
7836 scrollHeight
= $clone
[0].scrollHeight
;
7837 // Remove inline height property to measure natural heights
7838 $clone
.css( 'height', '' );
7839 innerHeight
= $clone
.innerHeight();
7840 outerHeight
= $clone
.outerHeight();
7841 // Measure max rows height
7842 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
7843 maxInnerHeight
= $clone
.innerHeight();
7844 $clone
.removeAttr( 'rows' ).css( 'height', '' );
7846 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
7847 // Only apply inline height when expansion beyond natural height is needed
7850 // Use the difference between the inner and outer height as a buffer
7851 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
7858 * Get input element.
7861 * @param {Object} [config] Configuration options
7862 * @returns {jQuery} Input element
7864 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
7865 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
7871 * Checks if input supports multiple lines.
7874 * @returns {boolean} Input supports multiple lines
7876 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
7877 return !!this.multiline
;
7881 * Checks if input automatically adjusts its size.
7884 * @returns {boolean} Input automatically adjusts its size
7886 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
7887 return !!this.autosize
;
7891 * Checks if input is pending.
7894 * @returns {boolean} Input is pending
7896 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
7897 return !!this.pending
;
7901 * Increases the pending stack.
7906 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
7908 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
7909 this.$input
.addClass( 'oo-ui-texture-pending' );
7914 * Reduces the pending stack.
7921 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
7922 this.pending
= Math
.max( 0, this.pending
- 1 );
7923 if ( !this.pending
) {
7924 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
7925 this.$input
.removeClass( 'oo-ui-texture-pending' );
7930 * Creates an OO.ui.TextInputMenuWidget object.
7933 * @extends OO.ui.MenuWidget
7936 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
7937 * @param {Object} [config] Configuration options
7938 * @cfg {jQuery} [$container=input.$element] Element to render menu under
7940 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
7941 // Parent constructor
7942 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
7946 this.$container
= config
.$container
|| this.input
.$element
;
7947 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
7950 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
7955 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
7960 * Handle window resize event.
7963 * @param {jQuery.Event} e Window resize event
7965 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
7975 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
7977 OO
.ui
.MenuWidget
.prototype.show
.call( this );
7980 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
7990 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
7992 OO
.ui
.MenuWidget
.prototype.hide
.call( this );
7994 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
7999 * Positions the menu.
8004 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
8006 $container
= this.$container
,
8007 dimensions
= $container
.offset();
8009 // Position under input
8010 dimensions
.top
+= $container
.height();
8012 // Compensate for frame position if in a differnt frame
8013 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
8014 frameOffset
= OO
.ui
.Element
.getRelativePosition(
8015 this.input
.$.frame
.$element
, this.$element
.offsetParent()
8017 dimensions
.left
+= frameOffset
.left
;
8018 dimensions
.top
+= frameOffset
.top
;
8020 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
8021 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
8022 dimensions
.right
= this.$element
.parent().position().left
-
8023 dimensions
.width
- dimensions
.left
;
8024 // Erase the value for 'left':
8025 delete dimensions
.left
;
8029 this.$element
.css( dimensions
);
8030 this.setIdealSize( $container
.width() );
8034 * Mixin for widgets with a boolean state.
8040 * @param {Object} [config] Configuration options
8041 * @cfg {boolean} [value=false] Initial value
8043 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8044 // Configuration initialization
8045 config
= config
|| {};
8051 this.$element
.addClass( 'oo-ui-toggleWidget' );
8052 this.setValue( !!config
.value
);
8059 * @param {boolean} value Changed value
8065 * Get the value of the toggle.
8068 * @returns {boolean} Toggle value
8070 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8075 * Set the value of the toggle.
8078 * @param {boolean} value New value
8082 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8084 if ( this.value
!== value
) {
8086 this.emit( 'change', value
);
8087 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8088 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8094 * @extends OO.ui.ButtonWidget
8095 * @mixins OO.ui.ToggleWidget
8098 * @param {Object} [config] Configuration options
8099 * @cfg {boolean} [value=false] Initial value
8101 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8102 // Configuration initialization
8103 config
= config
|| {};
8105 // Parent constructor
8106 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8108 // Mixin constructors
8109 OO
.ui
.ToggleWidget
.call( this, config
);
8112 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8117 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8119 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8126 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8127 if ( !this.disabled
) {
8128 this.setValue( !this.value
);
8132 return OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
8138 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8140 if ( value
!== this.value
) {
8141 this.setActive( value
);
8145 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8152 * @extends OO.ui.Widget
8153 * @mixins OO.ui.ToggleWidget
8156 * @param {Object} [config] Configuration options
8157 * @cfg {boolean} [value=false] Initial value
8159 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
8160 // Parent constructor
8161 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
8163 // Mixin constructors
8164 OO
.ui
.ToggleWidget
.call( this, config
);
8167 this.dragging
= false;
8168 this.dragStart
= null;
8169 this.sliding
= false;
8170 this.$glow
= this.$( '<span>' );
8171 this.$grip
= this.$( '<span>' );
8174 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8177 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
8178 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
8180 .addClass( 'oo-ui-toggleSwitchWidget' )
8181 .append( this.$glow
, this.$grip
);
8186 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
8188 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
8193 * Handles mouse down events.
8196 * @param {jQuery.Event} e Mouse down event
8198 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
8199 if ( !this.disabled
&& e
.which
=== 1 ) {
8200 this.setValue( !this.value
);