2 * OOjs UI v0.1.0-pre (7788dc6700)
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 Feb 13 2014 13:56:07 GMT-0800 (PST)
15 * Namespace for all classes, static methods and static properties.
25 * Get the user's language and any fallback languages.
27 * These language codes are used to localize user interface elements in the user's language.
29 * In environments that provide a localization system, this function should be overridden to
30 * return the user's language(s). The default implementation returns English (en) only.
32 * @returns {string[]} Language codes, in descending order of priority
34 OO
.ui
.getUserLanguages = function () {
39 * Get a value in an object keyed by language code.
41 * @param {Object.<string,Mixed>} obj Object keyed by language code
42 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
43 * @param {string} [fallback] Fallback code, used if no matching language can be found
44 * @returns {Mixed} Local value
46 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
53 // Known user language
54 langs
= OO
.ui
.getUserLanguages();
55 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
62 if ( obj
[fallback
] ) {
65 // First existing language
76 * Message store for the default implementation of OO.ui.msg
78 * Environments that provide a localization system should not use this, but should override
79 * OO.ui.msg altogether.
84 // Label text for button to exit from dialog
85 'ooui-dialog-action-close': 'Close',
86 // Tool tip for a button that moves items in a list down one place
87 'ooui-outline-control-move-down': 'Move item down',
88 // Tool tip for a button that moves items in a list up one place
89 'ooui-outline-control-move-up': 'Move item up',
90 // Label for the toolbar group that contains a list of all other available tools
91 'ooui-toolbar-more': 'More'
95 * Get a localized message.
97 * In environments that provide a localization system, this function should be overridden to
98 * return the message translated in the user's language. The default implementation always returns
101 * After the message key, message parameters may optionally be passed. In the default implementation,
102 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
103 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
104 * they support unnamed, ordered message parameters.
107 * @param {string} key Message key
108 * @param {Mixed...} [params] Message parameters
109 * @returns {string} Translated message with parameters substituted
111 OO
.ui
.msg = function ( key
) {
112 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
113 if ( typeof message
=== 'string' ) {
114 // Perform $1 substitution
115 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
116 var i
= parseInt( n
, 10 );
117 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
120 // Return placeholder if message not found
121 message
= '[' + key
+ ']';
126 OO
.ui
.deferMsg = function ( key
) {
128 return OO
.ui
.msg( key
);
132 OO
.ui
.resolveMsg = function ( msg
) {
133 if ( $.isFunction( msg
) ) {
141 // Add more as you need
161 * DOM element abstraction.
167 * @param {Object} [config] Configuration options
168 * @cfg {Function} [$] jQuery for the frame the widget is in
169 * @cfg {string[]} [classes] CSS class names
170 * @cfg {jQuery} [$content] Content elements to append
172 OO
.ui
.Element
= function OoUiElement( config
) {
173 // Configuration initialization
174 config
= config
|| {};
177 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
178 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
179 this.elementGroup
= null;
182 if ( Array
.isArray( config
.classes
) ) {
183 this.$element
.addClass( config
.classes
.join( ' ' ) );
185 if ( config
.$content
) {
186 this.$element
.append( config
.$content
);
190 /* Static Properties */
197 OO
.ui
.Element
.static = {};
202 * This may be ignored if getTagName is overridden.
208 OO
.ui
.Element
.static.tagName
= 'div';
213 * Gets a jQuery function within a specific document.
216 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
217 * @param {OO.ui.Frame} [frame] Frame of the document context
218 * @returns {Function} Bound jQuery function
220 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
221 function wrapper( selector
) {
222 return $( selector
, wrapper
.context
);
225 wrapper
.context
= this.getDocument( context
);
228 wrapper
.frame
= frame
;
235 * Get the document of an element.
238 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
239 * @returns {HTMLDocument} Document object
240 * @throws {Error} If context is invalid
242 OO
.ui
.Element
.getDocument = function ( obj
) {
244 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
245 ( obj
[0] && obj
[0].ownerDocument
) ||
246 // Empty jQuery selections might have a context
253 ( obj
.nodeType
=== 9 && obj
);
259 throw new Error( 'Invalid context' );
263 * Get the window of an element or document.
266 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
267 * @returns {Window} Window object
269 OO
.ui
.Element
.getWindow = function ( obj
) {
270 var doc
= this.getDocument( obj
);
271 return doc
.parentWindow
|| doc
.defaultView
;
275 * Get the direction of an element or document.
278 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
279 * @returns {string} Text direction, either `ltr` or `rtl`
281 OO
.ui
.Element
.getDir = function ( obj
) {
284 if ( obj
instanceof jQuery
) {
287 isDoc
= obj
.nodeType
=== 9;
288 isWin
= obj
.document
!== undefined;
289 if ( isDoc
|| isWin
) {
295 return $( obj
).css( 'direction' );
299 * Get the offset between two frames.
301 * TODO: Make this function not use recursion.
304 * @param {Window} from Window of the child frame
305 * @param {Window} [to=window] Window of the parent frame
306 * @param {Object} [offset] Offset to start with, used internally
307 * @returns {Object} Offset object, containing left and top properties
309 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
310 var i
, len
, frames
, frame
, rect
;
316 offset
= { 'top': 0, 'left': 0 };
318 if ( from.parent
=== from ) {
322 // Get iframe element
323 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
324 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
325 if ( frames
[i
].contentWindow
=== from ) {
331 // Recursively accumulate offset values
333 rect
= frame
.getBoundingClientRect();
334 offset
.left
+= rect
.left
;
335 offset
.top
+= rect
.top
;
337 this.getFrameOffset( from.parent
, offset
);
344 * Get the offset between two elements.
347 * @param {jQuery} $from
348 * @param {jQuery} $to
349 * @returns {Object} Translated position coordinates, containing top and left properties
351 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
352 var from = $from.offset(),
354 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
358 * Get element border sizes.
361 * @param {HTMLElement} el Element to measure
362 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
364 OO
.ui
.Element
.getBorders = function ( el
) {
365 var doc
= el
.ownerDocument
,
366 win
= doc
.parentWindow
|| doc
.defaultView
,
367 style
= win
&& win
.getComputedStyle
?
368 win
.getComputedStyle( el
, null ) :
371 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
372 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
373 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
374 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
377 'top': Math
.round( top
),
378 'left': Math
.round( left
),
379 'bottom': Math
.round( bottom
),
380 'right': Math
.round( right
)
385 * Get dimensions of an element or window.
388 * @param {HTMLElement|Window} el Element to measure
389 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
391 OO
.ui
.Element
.getDimensions = function ( el
) {
393 doc
= el
.ownerDocument
|| el
.document
,
394 win
= doc
.parentWindow
|| doc
.defaultView
;
396 if ( win
=== el
|| el
=== doc
.documentElement
) {
399 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
401 'top': $win
.scrollTop(),
402 'left': $win
.scrollLeft()
404 'scrollbar': { 'right': 0, 'bottom': 0 },
408 'bottom': $win
.innerHeight(),
409 'right': $win
.innerWidth()
415 'borders': this.getBorders( el
),
417 'top': $el
.scrollTop(),
418 'left': $el
.scrollLeft()
421 'right': $el
.innerWidth() - el
.clientWidth
,
422 'bottom': $el
.innerHeight() - el
.clientHeight
424 'rect': el
.getBoundingClientRect()
430 * Get closest scrollable container.
432 * Traverses up until either a scrollable element or the root is reached, in which case the window
436 * @param {HTMLElement} el Element to find scrollable container for
437 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
438 * @return {HTMLElement|Window} Closest scrollable container
440 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
442 props
= [ 'overflow' ],
443 $parent
= $( el
).parent();
445 if ( dimension
=== 'x' || dimension
=== 'y' ) {
446 props
.push( 'overflow-' + dimension
);
449 while ( $parent
.length
) {
450 if ( $parent
[0] === el
.ownerDocument
.body
) {
455 val
= $parent
.css( props
[i
] );
456 if ( val
=== 'auto' || val
=== 'scroll' ) {
460 $parent
= $parent
.parent();
462 return this.getDocument( el
).body
;
466 * Scroll element into view
469 * @param {HTMLElement} el Element to scroll into view
470 * @param {Object} [config={}] Configuration config
471 * @param {string} [config.duration] jQuery animation duration value
472 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
473 * to scroll in both directions
474 * @param {Function} [config.complete] Function to call when scrolling completes
476 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
477 // Configuration initialization
478 config
= config
|| {};
481 callback
= typeof config
.complete
=== 'function' && config
.complete
,
482 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
484 eld
= this.getDimensions( el
),
485 scd
= this.getDimensions( sc
),
487 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
488 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
489 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
490 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
493 if ( !config
.direction
|| config
.direction
=== 'y' ) {
495 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
496 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
497 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
500 if ( !config
.direction
|| config
.direction
=== 'x' ) {
501 if ( rel
.left
< 0 ) {
502 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
503 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
504 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
507 if ( !$.isEmptyObject( anim
) ) {
508 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
510 $sc
.queue( function ( next
) {
525 * Get the HTML tag name.
527 * Override this method to base the result on instance information.
529 * @returns {string} HTML tag name
531 OO
.ui
.Element
.prototype.getTagName = function () {
532 return this.constructor.static.tagName
;
536 * Get the DOM document.
538 * @returns {HTMLDocument} Document object
540 OO
.ui
.Element
.prototype.getElementDocument = function () {
541 return OO
.ui
.Element
.getDocument( this.$element
);
545 * Get the DOM window.
547 * @returns {Window} Window object
549 OO
.ui
.Element
.prototype.getElementWindow = function () {
550 return OO
.ui
.Element
.getWindow( this.$element
);
554 * Get closest scrollable container.
557 * @see #static-method-getClosestScrollableContainer
559 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
560 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
564 * Get group element is in.
566 * @returns {OO.ui.GroupElement|null} Group element, null if none
568 OO
.ui
.Element
.prototype.getElementGroup = function () {
569 return this.elementGroup
;
573 * Set group element is in.
575 * @param {OO.ui.GroupElement|null} group Group element, null if none
578 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
579 this.elementGroup
= group
;
584 * 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
.Element
.call( this, config
);
690 // Mixin constructors
691 OO
.EventEmitter
.call( this );
694 this.initialized
= false;
695 this.config
= config
;
699 .addClass( 'oo-ui-frame' )
700 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
706 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
708 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
710 /* Static Properties */
712 OO
.ui
.Frame
.static.tagName
= 'iframe';
723 * Transplant the CSS styles from as parent document to a frame's document.
725 * This loops over the style sheets in the parent document, and copies their nodes to the
726 * frame's document. It then polls the document to see when all styles have loaded, and once they
727 * have, invokes the callback.
729 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
730 * and invoke the callback anyway. This protects against cases like a display: none; iframe in
731 * Firefox, where the styles won't load until the iframe becomes visible.
733 * For details of how we arrived at the strategy used in this function, see #load.
738 * @param {HTMLDocument} parentDoc Document to transplant styles from
739 * @param {HTMLDocument} frameDoc Document to transplant styles to
740 * @param {Function} [callback] Callback to execute once styles have loaded
741 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
743 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, callback
, timeout
) {
744 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
745 $pollNodes
= $( [] ),
746 // Fake font-family value
747 fontFamily
= 'oo-ui-frame-transplantStyles-loaded';
749 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
750 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
751 if ( callback
&& styleNode
.nodeName
.toLowerCase() === 'link' ) {
752 // External stylesheet
753 // Create a node with a unique ID that we're going to monitor to see when the CSS
755 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
756 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
757 .attr( 'id', pollNodeId
)
758 .appendTo( frameDoc
.body
)
761 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
762 // The font-family rule will only take effect once the @import finishes
763 newNode
= frameDoc
.createElement( 'style' );
764 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
765 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
767 // Not an external stylesheet, or no polling required; just copy the node over
768 newNode
= frameDoc
.importNode( styleNode
, true );
770 frameDoc
.head
.appendChild( newNode
);
774 // Poll every 100ms until all external stylesheets have loaded
775 $pendingPollNodes
= $pollNodes
;
776 timeoutID
= setTimeout( function pollExternalStylesheets() {
778 $pendingPollNodes
.length
> 0 &&
779 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
781 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
784 if ( $pendingPollNodes
.length
=== 0 ) {
786 if ( timeoutID
!== null ) {
792 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
795 // ...but give up after a while
796 if ( timeout
!== 0 ) {
797 setTimeout( function () {
799 clearTimeout( timeoutID
);
804 }, timeout
|| 5000 );
812 * Load the frame contents.
814 * Once the iframe's stylesheets are loaded, the `initialize` event will be emitted.
816 * Sounds simple right? Read on...
818 * When you create a dynamic iframe using open/write/close, the window.load event for the
819 * iframe is triggered when you call close, and there's no further load event to indicate that
820 * everything is actually loaded.
822 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
823 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
824 * are added to document.styleSheets immediately, and the only way you can determine whether they've
825 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
826 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
828 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
829 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
830 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
831 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
832 * .cssRules is allowed.
834 * However, now that we control the styles we're injecting, we might as well do away with
835 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
836 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
837 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
838 * rule is not applied until after the `@import` finishes.
840 * All this stylesheet injection and polling magic is in #transplantStyles.
844 OO
.ui
.Frame
.prototype.load = function () {
845 var win
= this.$element
.prop( 'contentWindow' ),
849 // Figure out directionality:
850 this.dir
= this.$element
.closest( '[dir]' ).prop( 'dir' ) || 'ltr';
852 // Initialize contents
857 '<body class="oo-ui-frame-body oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
858 '<div class="oo-ui-frame-content"></div>' +
865 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
866 this.$content
= this.$( '.oo-ui-frame-content' );
867 this.$document
= this.$( doc
);
869 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0],
871 frame
.initialized
= true;
872 frame
.emit( 'initialize' );
878 * Run a callback as soon as the frame has been initialized.
880 * @param {Function} callback
882 OO
.ui
.Frame
.prototype.run = function ( callback
) {
883 if ( this.initialized
) {
886 this.once( 'initialize', callback
);
891 * Sets the size of the frame.
894 * @param {number} width Frame width in pixels
895 * @param {number} height Frame height in pixels
898 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
899 this.$element
.css( { 'width': width
, 'height': height
} );
903 * Container for elements in a child frame.
905 * There are two ways to specify a title: set the static `title` property or provide a `title`
906 * property in the configuration options. The latter will override the former.
910 * @extends OO.ui.Element
911 * @mixins OO.EventEmitter
914 * @param {OO.ui.WindowSet} windowSet Window set this dialog is part of
915 * @param {Object} [config] Configuration options
916 * @cfg {string|Function} [title] Title string or function that returns a string
917 * @cfg {string} [icon] Symbolic name of icon
920 OO
.ui
.Window
= function OoUiWindow( windowSet
, config
) {
921 // Parent constructor
922 OO
.ui
.Element
.call( this, config
);
924 // Mixin constructors
925 OO
.EventEmitter
.call( this );
928 this.windowSet
= windowSet
;
929 this.visible
= false;
930 this.opening
= false;
931 this.closing
= false;
932 this.title
= OO
.ui
.resolveMsg( config
.title
|| this.constructor.static.title
);
933 this.icon
= config
.icon
|| this.constructor.static.icon
;
934 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
935 this.$frame
= this.$( '<div>' );
936 this.$ = function () {
937 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
942 .addClass( 'oo-ui-window' )
943 // Hide the window using visibility: hidden; while the iframe is still loading
944 // Can't use display: none; because that prevents the iframe from loading in Firefox
945 .css( 'visibility', 'hidden' )
946 .append( this.$frame
);
948 .addClass( 'oo-ui-window-frame' )
949 .append( this.frame
.$element
);
952 this.frame
.connect( this, { 'initialize': 'initialize' } );
957 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
959 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
964 * Initialize contents.
966 * Fired asynchronously after construction when iframe is ready.
974 * Fired after window has been opened.
977 * @param {Object} data Window opening data
983 * Fired after window has been closed.
986 * @param {Object} data Window closing data
989 /* Static Properties */
992 * Symbolic name of icon.
998 OO
.ui
.Window
.static.icon
= 'window';
1005 * @property {string|Function} Title string or function that returns a string
1007 OO
.ui
.Window
.static.title
= null;
1012 * Check if window is visible.
1015 * @returns {boolean} Window is visible
1017 OO
.ui
.Window
.prototype.isVisible = function () {
1018 return this.visible
;
1022 * Check if window is opening.
1025 * @returns {boolean} Window is opening
1027 OO
.ui
.Window
.prototype.isOpening = function () {
1028 return this.opening
;
1032 * Check if window is closing.
1035 * @returns {boolean} Window is closing
1037 OO
.ui
.Window
.prototype.isClosing = function () {
1038 return this.closing
;
1042 * Get the window frame.
1045 * @returns {OO.ui.Frame} Frame of window
1047 OO
.ui
.Window
.prototype.getFrame = function () {
1052 * Get the window set.
1055 * @returns {OO.ui.WindowSet} Window set
1057 OO
.ui
.Window
.prototype.getWindowSet = function () {
1058 return this.windowSet
;
1062 * Get the window title.
1064 * @returns {string} Title text
1066 OO
.ui
.Window
.prototype.getTitle = function () {
1071 * Get the window icon.
1073 * @returns {string} Symbolic name of icon
1075 OO
.ui
.Window
.prototype.getIcon = function () {
1080 * Set the size of window frame.
1082 * @param {number} [width=auto] Custom width
1083 * @param {number} [height=auto] Custom height
1086 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1087 if ( !this.frame
.$content
) {
1091 this.frame
.$element
.css( {
1092 'width': width
=== undefined ? 'auto' : width
,
1093 'height': height
=== undefined ? 'auto' : height
1100 * Set the title of the window.
1102 * @param {string|Function} title Title text or a function that returns text
1105 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1106 this.title
= OO
.ui
.resolveMsg( title
);
1107 if ( this.$title
) {
1108 this.$title
.text( title
);
1114 * Set the icon of the window.
1116 * @param {string} icon Symbolic name of icon
1119 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1121 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1125 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1132 * Set the position of window to fit with contents..
1134 * @param {string} left Left offset
1135 * @param {string} top Top offset
1138 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1139 this.$element
.css( { 'left': left
, 'top': top
} );
1144 * Set the height of window to fit with contents.
1146 * @param {number} [min=0] Min height
1147 * @param {number} [max] Max height (defaults to content's outer height)
1150 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1151 var height
= this.frame
.$content
.outerHeight();
1153 this.frame
.$element
.css(
1154 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1161 * Set the width of window to fit with contents.
1163 * @param {number} [min=0] Min height
1164 * @param {number} [max] Max height (defaults to content's outer width)
1167 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1168 var width
= this.frame
.$content
.outerWidth();
1170 this.frame
.$element
.css(
1171 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1178 * Initialize window contents.
1180 * The first time the window is opened, #initialize is called when it's safe to begin populating
1181 * its contents. See #setup for a way to make changes each time the window opens.
1183 * Once this method is called, this.$$ can be used to create elements within the frame.
1189 OO
.ui
.Window
.prototype.initialize = function () {
1191 this.$ = this.frame
.$;
1192 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1193 .text( this.title
);
1194 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1195 .addClass( 'oo-ui-icon-' + this.icon
);
1196 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1197 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1198 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1199 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1202 this.frame
.$content
.append(
1203 this.$head
.append( this.$icon
, this.$title
),
1209 // Undo the visibility: hidden; hack from the constructor and apply display: none;
1210 // We can do this safely now that the iframe has initialized
1211 this.$element
.hide().css( 'visibility', '' );
1213 this.emit( 'initialize' );
1219 * Setup window for use.
1221 * Each time the window is opened, once it's ready to be interacted with, this will set it up for
1222 * use in a particular context, based on the `data` argument.
1224 * When you override this method, you must call the parent method at the very beginning.
1228 * @param {Object} [data] Window opening data
1230 OO
.ui
.Window
.prototype.setup = function () {
1231 // Override to do something
1235 * Tear down window after use.
1237 * Each time the window is closed, and it's done being interacted with, this will tear it down and
1238 * do something with the user's interactions within the window, based on the `data` argument.
1240 * When you override this method, you must call the parent method at the very end.
1244 * @param {Object} [data] Window closing data
1246 OO
.ui
.Window
.prototype.teardown = function () {
1247 // Override to do something
1253 * Do not override this method. See #setup for a way to make changes each time the window opens.
1256 * @param {Object} [data] Window opening data
1260 OO
.ui
.Window
.prototype.open = function ( data
) {
1261 if ( !this.opening
&& !this.closing
&& !this.visible
) {
1262 this.opening
= true;
1263 this.frame
.run( OO
.ui
.bind( function () {
1264 this.$element
.show();
1265 this.visible
= true;
1266 this.frame
.$element
.focus();
1267 this.emit( 'opening', data
);
1269 this.emit( 'open', data
);
1270 this.opening
= false;
1280 * See #teardown for a way to do something each time the window closes.
1283 * @param {Object} [data] Window closing data
1287 OO
.ui
.Window
.prototype.close = function ( data
) {
1288 if ( !this.opening
&& !this.closing
&& this.visible
) {
1289 this.frame
.$content
.find( ':focus' ).blur();
1290 this.closing
= true;
1291 this.$element
.hide();
1292 this.visible
= false;
1293 this.emit( 'closing', data
);
1294 this.teardown( data
);
1295 this.emit( 'close', data
);
1296 this.closing
= false;
1302 * Set of mutually exclusive windows.
1305 * @extends OO.ui.Element
1306 * @mixins OO.EventEmitter
1309 * @param {OO.Factory} factory Window factory
1310 * @param {Object} [config] Configuration options
1312 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1313 // Parent constructor
1314 OO
.ui
.Element
.call( this, config
);
1316 // Mixin constructors
1317 OO
.EventEmitter
.call( this );
1320 this.factory
= factory
;
1322 this.currentWindow
= null;
1325 this.$element
.addClass( 'oo-ui-windowSet' );
1330 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1332 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1338 * @param {OO.ui.Window} win Window that's being opened
1339 * @param {Object} config Window opening information
1344 * @param {OO.ui.Window} win Window that's been opened
1345 * @param {Object} config Window opening information
1350 * @param {OO.ui.Window} win Window that's being closed
1351 * @param {Object} config Window closing information
1356 * @param {OO.ui.Window} win Window that's been closed
1357 * @param {Object} config Window closing information
1363 * Handle a window that's being opened.
1366 * @param {OO.ui.Window} win Window that's being opened
1367 * @param {Object} [config] Window opening information
1370 OO
.ui
.WindowSet
.prototype.onWindowOpening = function ( win
, config
) {
1371 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1372 this.currentWindow
.close();
1374 this.currentWindow
= win
;
1375 this.emit( 'opening', win
, config
);
1379 * Handle a window that's been opened.
1382 * @param {OO.ui.Window} win Window that's been opened
1383 * @param {Object} [config] Window opening information
1386 OO
.ui
.WindowSet
.prototype.onWindowOpen = function ( win
, config
) {
1387 this.emit( 'open', win
, config
);
1391 * Handle a window that's being closed.
1394 * @param {OO.ui.Window} win Window that's being closed
1395 * @param {Object} [config] Window closing information
1398 OO
.ui
.WindowSet
.prototype.onWindowClosing = function ( win
, config
) {
1399 this.currentWindow
= null;
1400 this.emit( 'closing', win
, config
);
1404 * Handle a window that's been closed.
1407 * @param {OO.ui.Window} win Window that's been closed
1408 * @param {Object} [config] Window closing information
1411 OO
.ui
.WindowSet
.prototype.onWindowClose = function ( win
, config
) {
1412 this.emit( 'close', win
, config
);
1416 * Get the current window.
1419 * @returns {OO.ui.Window} Current window
1421 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1422 return this.currentWindow
;
1426 * Return a given window.
1428 * @param {string} name Symbolic name of window
1429 * @return {OO.ui.Window} Window with specified name
1431 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1434 if ( !this.factory
.lookup( name
) ) {
1435 throw new Error( 'Unknown window: ' + name
);
1437 if ( !( name
in this.windows
) ) {
1438 win
= this.windows
[name
] = this.factory
.create( name
, this, { '$': this.$ } );
1439 win
.connect( this, {
1440 'opening': [ 'onWindowOpening', win
],
1441 'open': [ 'onWindowOpen', win
],
1442 'closing': [ 'onWindowClosing', win
],
1443 'close': [ 'onWindowClose', win
]
1445 this.$element
.append( win
.$element
);
1446 win
.getFrame().load();
1448 return this.windows
[name
];
1455 * @extends OO.ui.Window
1458 * @param {OO.ui.WindowSet} windowSet Window set this dialog is part of
1459 * @param {Object} [config] Configuration options
1460 * @cfg {boolean} [footless] Hide foot
1461 * @cfg {boolean} [small] Make the dialog small
1463 OO
.ui
.Dialog
= function OoUiDialog( windowSet
, config
) {
1464 // Configuration initialization
1465 config
= config
|| {};
1467 // Parent constructor
1468 OO
.ui
.Window
.call( this, windowSet
, config
);
1471 this.visible
= false;
1472 this.footless
= !!config
.footless
;
1473 this.small
= !!config
.small
;
1474 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1475 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1478 this.$element
.on( 'mousedown', false );
1481 this.$element
.addClass( 'oo-ui-dialog' );
1486 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1488 /* Static Properties */
1491 * Symbolic name of dialog.
1495 * @property {string}
1498 OO
.ui
.Dialog
.static.name
= '';
1503 * Handle close button click events.
1507 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1508 this.close( { 'action': 'cancel' } );
1512 * Handle window mouse wheel events.
1515 * @param {jQuery.Event} e Mouse wheel event
1517 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1522 * Handle document key down events.
1525 * @param {jQuery.Event} e Key down event
1527 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1528 switch ( e
.which
) {
1529 case OO
.ui
.Keys
.PAGEUP
:
1530 case OO
.ui
.Keys
.PAGEDOWN
:
1531 case OO
.ui
.Keys
.END
:
1532 case OO
.ui
.Keys
.HOME
:
1533 case OO
.ui
.Keys
.LEFT
:
1535 case OO
.ui
.Keys
.RIGHT
:
1536 case OO
.ui
.Keys
.DOWN
:
1537 // Prevent any key events that might cause scrolling
1543 * Handle frame document key down events.
1546 * @param {jQuery.Event} e Key down event
1548 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1549 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1550 this.close( { 'action': 'cancel' } );
1558 OO
.ui
.Dialog
.prototype.initialize = function () {
1560 OO
.ui
.Window
.prototype.initialize
.call( this );
1563 this.closeButton
= new OO
.ui
.ButtonWidget( {
1567 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1571 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1572 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1575 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1576 if ( this.footless
) {
1577 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1580 this.$frame
.addClass( 'oo-ui-window-frame-small' );
1582 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1583 this.$head
.append( this.closeButton
.$element
);
1589 OO
.ui
.Dialog
.prototype.setup = function ( data
) {
1591 OO
.ui
.Window
.prototype.setup
.call( this, data
);
1593 // Prevent scrolling in top-level window
1594 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1595 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1601 OO
.ui
.Dialog
.prototype.teardown = function ( data
) {
1603 OO
.ui
.Window
.prototype.teardown
.call( this, data
);
1605 // Allow scrolling in top-level window
1606 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1607 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1613 OO
.ui
.Dialog
.prototype.close = function ( data
) {
1614 if ( !this.opening
&& !this.closing
&& this.visible
) {
1615 // Trigger transition
1616 this.$element
.addClass( 'oo-ui-dialog-closing' );
1617 // Allow transition to complete before actually closing
1618 setTimeout( OO
.ui
.bind( function () {
1619 this.$element
.removeClass( 'oo-ui-dialog-closing' );
1621 OO
.ui
.Window
.prototype.close
.call( this, data
);
1626 * Container for elements.
1630 * @extends OO.ui.Element
1631 * @mixin OO.EventEmitter
1634 * @param {Object} [config] Configuration options
1636 OO
.ui
.Layout
= function OoUiLayout( config
) {
1637 // Initialize config
1638 config
= config
|| {};
1640 // Parent constructor
1641 OO
.ui
.Element
.call( this, config
);
1643 // Mixin constructors
1644 OO
.EventEmitter
.call( this );
1647 this.$element
.addClass( 'oo-ui-layout' );
1652 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1654 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1656 * User interface control.
1660 * @extends OO.ui.Element
1661 * @mixin OO.EventEmitter
1664 * @param {Object} [config] Configuration options
1665 * @cfg {boolean} [disabled=false] Disable
1667 OO
.ui
.Widget
= function OoUiWidget( config
) {
1668 // Initialize config
1669 config
= $.extend( { 'disabled': false }, config
);
1671 // Parent constructor
1672 OO
.ui
.Element
.call( this, config
);
1674 // Mixin constructors
1675 OO
.EventEmitter
.call( this );
1678 this.disabled
= null;
1679 this.wasDisabled
= null;
1682 this.$element
.addClass( 'oo-ui-widget' );
1683 this.setDisabled( !!config
.disabled
);
1688 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1690 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1696 * @param {boolean} disabled Widget is disabled
1702 * Check if the widget is disabled.
1705 * @param {boolean} Button is disabled
1707 OO
.ui
.Widget
.prototype.isDisabled = function () {
1708 return this.disabled
;
1712 * Update the disabled state, in case of changes in parent widget.
1717 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1718 this.setDisabled( this.disabled
);
1723 * Set the disabled state of the widget.
1725 * This should probably change the widgets's appearance and prevent it from being used.
1728 * @param {boolean} disabled Disable widget
1731 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1734 this.disabled
= !!disabled
;
1735 isDisabled
= this.isDisabled();
1736 if ( isDisabled
!== this.wasDisabled
) {
1737 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1738 this.emit( 'disable', isDisabled
);
1740 this.wasDisabled
= isDisabled
;
1744 * Element with a button.
1750 * @param {jQuery} $button Button node, assigned to #$button
1751 * @param {Object} [config] Configuration options
1752 * @cfg {boolean} [frameless] Render button without a frame
1753 * @cfg {number} [tabIndex] Button's tab index
1755 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
1756 // Configuration initialization
1757 config
= config
|| {};
1760 this.$button
= $button
;
1761 this.tabIndex
= null;
1762 this.active
= false;
1763 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
1766 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
1769 this.$element
.addClass( 'oo-ui-buttonedElement' );
1771 .addClass( 'oo-ui-buttonedElement-button' )
1772 .attr( 'role', 'button' )
1773 .prop( 'tabIndex', config
.tabIndex
|| 0 );
1774 if ( config
.frameless
) {
1775 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
1777 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
1784 * Handles mouse down events.
1787 * @param {jQuery.Event} e Mouse down event
1789 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function () {
1790 this.tabIndex
= this.$button
.attr( 'tabIndex' );
1791 // Remove the tab-index while the button is down to prevent the button from stealing focus
1792 this.$button
.removeAttr( 'tabIndex' );
1793 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
1797 * Handles mouse up events.
1800 * @param {jQuery.Event} e Mouse up event
1802 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function () {
1803 // Restore the tab-index after the button is up to restore the button's accesssibility
1804 this.$button
.attr( 'tabIndex', this.tabIndex
);
1805 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
1812 * @param {boolean} [value] Make button active
1815 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
1816 this.$element
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
1820 * Element that can be automatically clipped to visible boundaies.
1826 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
1828 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
) {
1830 this.$clippable
= $clippable
;
1831 this.clipping
= false;
1832 this.clipped
= false;
1833 this.$clippableContainer
= null;
1834 this.$clippableScroller
= null;
1835 this.$clippableWindow
= null;
1836 this.idealWidth
= null;
1837 this.idealHeight
= null;
1838 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
1839 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
1842 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
1851 * @param {boolean} value Enable clipping
1854 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
1857 if ( this.clipping
!== value
) {
1858 this.clipping
= value
;
1859 if ( this.clipping
) {
1860 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
1861 // If the clippable container is the body, we have to listen to scroll events and check
1862 // jQuery.scrollTop on the window because of browser inconsistencies
1863 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
1864 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
1865 this.$clippableContainer
;
1866 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
1867 this.$clippableWindow
= this.$( this.getElementWindow() )
1868 .on( 'resize', this.onClippableWindowResizeHandler
);
1869 // Initial clip after visible
1870 setTimeout( OO
.ui
.bind( this.clip
, this ) );
1872 this.$clippableContainer
= null;
1873 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
1874 this.$clippableScroller
= null;
1875 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
1876 this.$clippableWindow
= null;
1884 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
1887 * @return {boolean} Element will be clipped to the visible area
1889 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
1890 return this.clipping
;
1894 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
1897 * @return {boolean} Part of the element is being clipped
1899 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
1900 return this.clipped
;
1904 * Set the ideal size.
1907 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
1908 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
1910 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
1911 this.idealWidth
= width
;
1912 this.idealHeight
= height
;
1916 * Clip element to visible boundaries and allow scrolling when needed.
1918 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
1919 * overlapped by, the visible area of the nearest scrollable container.
1924 OO
.ui
.ClippableElement
.prototype.clip = function () {
1925 if ( !this.clipping
) {
1926 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
1931 cOffset
= this.$clippable
.offset(),
1932 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
1933 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
1934 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
1935 scrollTop
= this.$clippableScroller
.scrollTop(),
1936 scrollLeft
= this.$clippableScroller
.scrollLeft(),
1937 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
1938 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
1939 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
1940 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
1941 clipWidth
= desiredWidth
< naturalWidth
,
1942 clipHeight
= desiredHeight
< naturalHeight
;
1945 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
1947 this.$clippable
.css( { 'overflow-x': '', 'width': this.idealWidth
|| '' } );
1950 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
1952 this.$clippable
.css( { 'overflow-y': '', 'height': this.idealHeight
|| '' } );
1955 this.clipped
= clipWidth
|| clipHeight
;
1960 * Element with named flags, used for styling, that can be added, removed and listed and checked.
1966 * @param {Object} [config] Configuration options
1967 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
1969 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
1970 // Config initialization
1971 config
= config
|| {};
1977 this.setFlags( config
.flags
);
1983 * Check if a flag is set.
1986 * @param {string} flag Flag name to check
1987 * @returns {boolean} Has flag
1989 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
1990 return flag
in this.flags
;
1994 * Get the names of all flags.
1997 * @returns {string[]} flags Flag names
1999 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
2000 return Object
.keys( this.flags
);
2004 * Add one or more flags.
2007 * @param {string[]|Object.<string, boolean>} flags List of flags to add, or list of set/remove
2008 * values, keyed by flag name
2011 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
2013 classPrefix
= 'oo-ui-flaggableElement-';
2015 if ( Array
.isArray( flags
) ) {
2016 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
2019 this.flags
[flag
] = true;
2020 this.$element
.addClass( classPrefix
+ flag
);
2022 } else if ( OO
.isPlainObject( flags
) ) {
2023 for ( flag
in flags
) {
2024 if ( flags
[flags
] ) {
2026 this.flags
[flag
] = true;
2027 this.$element
.addClass( classPrefix
+ flag
);
2030 delete this.flags
[flag
];
2031 this.$element
.removeClass( classPrefix
+ flag
);
2038 * Element containing a sequence of child elements.
2044 * @param {jQuery} $group Container node, assigned to #$group
2045 * @param {Object} [config] Configuration options
2046 * @cfg {Object.<string,string>} [aggregations] Events to aggregate, keyed by item event name
2048 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2050 config
= config
|| {};
2053 this.$group
= $group
;
2055 this.$items
= this.$( [] );
2056 this.aggregate
= !$.isEmptyObject( config
.aggregations
);
2057 this.aggregations
= config
.aggregations
|| {};
2066 * @returns {OO.ui.Element[]} Items
2068 OO
.ui
.GroupElement
.prototype.getItems = function () {
2069 return this.items
.slice( 0 );
2076 * @param {OO.ui.Element[]} items Item
2077 * @param {number} [index] Index to insert items at
2080 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2081 var i
, len
, item
, event
, events
, currentIndex
,
2082 $items
= this.$( [] );
2084 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2087 // Check if item exists then remove it first, effectively "moving" it
2088 currentIndex
= this.items
.indexOf( item
);
2089 if ( currentIndex
>= 0 ) {
2090 this.removeItems( [ item
] );
2091 // Adjust index to compensate for removal
2092 if ( currentIndex
< index
) {
2097 if ( this.aggregate
) {
2099 for ( event
in this.aggregations
) {
2100 events
[event
] = [ 'emit', this.aggregations
[event
], item
];
2102 item
.connect( this, events
);
2104 item
.setElementGroup( this );
2105 $items
= $items
.add( item
.$element
);
2108 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2109 this.$group
.append( $items
);
2110 this.items
.push
.apply( this.items
, items
);
2111 } else if ( index
=== 0 ) {
2112 this.$group
.prepend( $items
);
2113 this.items
.unshift
.apply( this.items
, items
);
2115 this.$items
.eq( index
).before( $items
);
2116 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2119 this.$items
= this.$items
.add( $items
);
2127 * Items will be detached, not removed, so they can be used later.
2130 * @param {OO.ui.Element[]} items Items to remove
2133 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2134 var i
, len
, item
, index
;
2136 // Remove specific items
2137 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2139 index
= this.items
.indexOf( item
);
2140 if ( index
!== -1 ) {
2141 if ( this.aggregate
) {
2142 item
.disconnect( this );
2144 item
.setElementGroup( null );
2145 this.items
.splice( index
, 1 );
2146 item
.$element
.detach();
2147 this.$items
= this.$items
.not( item
.$element
);
2157 * Items will be detached, not removed, so they can be used later.
2162 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2166 if ( this.aggregate
) {
2167 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2168 item
.disconnect( this );
2171 item
.setElementGroup( null );
2173 this.$items
.detach();
2174 this.$items
= this.$( [] );
2177 * Element containing an icon.
2183 * @param {jQuery} $icon Icon node, assigned to #$icon
2184 * @param {Object} [config] Configuration options
2185 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2186 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2189 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2190 // Config intialization
2191 config
= config
|| {};
2198 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2199 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2202 /* Static Properties */
2204 OO
.ui
.IconedElement
.static = {};
2209 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2211 * For i18n purposes, this property can be an object containing a `default` icon name property and
2212 * additional icon names keyed by language code.
2214 * Example of i18n icon definition:
2215 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2219 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2220 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2223 OO
.ui
.IconedElement
.static.icon
= null;
2231 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
2232 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2236 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2237 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2240 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2242 if ( typeof icon
=== 'string' ) {
2244 if ( icon
.length
) {
2245 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2249 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2258 * @returns {string} Icon
2260 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2264 * Element containing an indicator.
2270 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2271 * @param {Object} [config] Configuration options
2272 * @cfg {string} [indicator] Symbolic indicator name
2273 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2275 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2276 // Config intialization
2277 config
= config
|| {};
2280 this.$indicator
= $indicator
;
2281 this.indicator
= null;
2282 this.indicatorLabel
= null;
2285 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2286 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2287 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2290 /* Static Properties */
2292 OO
.ui
.IndicatedElement
.static = {};
2299 * @property {string|null} Symbolic indicator name or null for no indicator
2301 OO
.ui
.IndicatedElement
.static.indicator
= null;
2308 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2311 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2319 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2322 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2323 if ( this.indicator
) {
2324 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2325 this.indicator
= null;
2327 if ( typeof indicator
=== 'string' ) {
2328 indicator
= indicator
.trim();
2329 if ( indicator
.length
) {
2330 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2331 this.indicator
= indicator
;
2334 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2340 * Set indicator label.
2343 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2344 * for no indicator title
2347 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2348 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2350 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2351 this.$indicator
.attr( 'title', indicatorTitle
);
2353 this.$indicator
.removeAttr( 'title' );
2363 * @returns {string} title Symbolic name of indicator
2365 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2366 return this.indicator
;
2370 * Get indicator title.
2373 * @returns {string} Indicator title text
2375 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2376 return this.indicatorTitle
;
2379 * Element containing a label.
2385 * @param {jQuery} $label Label node, assigned to #$label
2386 * @param {Object} [config] Configuration options
2387 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2389 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2390 // Config intialization
2391 config
= config
|| {};
2394 this.$label
= $label
;
2398 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2399 this.setLabel( config
.label
|| this.constructor.static.label
);
2402 /* Static Properties */
2404 OO
.ui
.LabeledElement
.static = {};
2411 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2414 OO
.ui
.LabeledElement
.static.label
= null;
2422 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2423 * text; or null for no label
2426 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2429 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2430 if ( typeof label
=== 'string' && label
.trim() ) {
2431 this.$label
.text( label
);
2432 } else if ( label
instanceof jQuery
) {
2433 this.$label
.empty().append( label
);
2435 this.$label
.empty();
2438 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2439 this.$label
.css( 'display', empty
? 'none' : '' );
2448 * @returns {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2449 * text; or null for no label
2451 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2461 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
2462 if ( this.$label
.autoEllipsis
) {
2463 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
2468 * Popuppable element.
2474 * @param {Object} [config] Configuration options
2475 * @cfg {number} [popupWidth=320] Width of popup
2476 * @cfg {number} [popupHeight] Height of popup
2477 * @cfg {Object} [popup] Configuration to pass to popup
2479 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
2480 // Configuration initialization
2481 config
= $.extend( { 'popupWidth': 320 }, config
);
2484 this.popup
= new OO
.ui
.PopupWidget( $.extend(
2485 { 'align': 'center', 'autoClose': true },
2487 { '$': this.$, '$autoCloseIgnore': this.$element
}
2489 this.popupWidth
= config
.popupWidth
;
2490 this.popupHeight
= config
.popupHeight
;
2499 * @returns {OO.ui.PopupWidget} Popup widget
2501 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
2510 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
2511 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
2519 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
2523 * Element with a title.
2529 * @param {jQuery} $label Titled node, assigned to #$titled
2530 * @param {Object} [config] Configuration options
2531 * @cfg {string|Function} [title] Title text or a function that returns text
2533 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
2534 // Config intialization
2535 config
= config
|| {};
2538 this.$titled
= $titled
;
2542 this.setTitle( config
.title
|| this.constructor.static.title
);
2545 /* Static Properties */
2547 OO
.ui
.TitledElement
.static = {};
2554 * @property {string|Function} Title text or a function that returns text
2556 OO
.ui
.TitledElement
.static.title
= null;
2564 * @param {string|Function|null} title Title text, a function that returns text or null for no title
2567 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
2568 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
2570 if ( typeof title
=== 'string' && title
.length
) {
2571 this.$titled
.attr( 'title', title
);
2573 this.$titled
.removeAttr( 'title' );
2583 * @returns {string} Title string
2585 OO
.ui
.TitledElement
.prototype.getTitle = function () {
2589 * Generic toolbar tool.
2593 * @extends OO.ui.Widget
2594 * @mixins OO.ui.IconedElement
2597 * @param {OO.ui.ToolGroup} toolGroup
2598 * @param {Object} [config] Configuration options
2599 * @cfg {string|Function} [title] Title text or a function that returns text
2601 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
2602 // Config intialization
2603 config
= config
|| {};
2605 // Parent constructor
2606 OO
.ui
.Widget
.call( this, config
);
2608 // Mixin constructors
2609 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
2612 this.toolGroup
= toolGroup
;
2613 this.toolbar
= this.toolGroup
.getToolbar();
2614 this.active
= false;
2615 this.$title
= this.$( '<span>' );
2616 this.$link
= this.$( '<a>' );
2620 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
2623 this.$title
.addClass( 'oo-ui-tool-title' );
2625 .addClass( 'oo-ui-tool-link' )
2626 .append( this.$icon
, this.$title
);
2628 .data( 'oo-ui-tool', this )
2630 'oo-ui-tool ' + 'oo-ui-tool-name-' +
2631 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
2633 .append( this.$link
);
2634 this.setTitle( config
.title
|| this.constructor.static.title
);
2639 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
2641 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
2649 /* Static Properties */
2651 OO
.ui
.Tool
.static.tagName
= 'span';
2654 * Symbolic name of tool.
2658 * @property {string}
2661 OO
.ui
.Tool
.static.name
= '';
2668 * @property {string}
2671 OO
.ui
.Tool
.static.group
= '';
2676 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
2677 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
2678 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
2679 * appended to the title if the tool is part of a bar tool group.
2683 * @property {string|Function} Title text or a function that returns text
2686 OO
.ui
.Tool
.static.title
= '';
2689 * Tool can be automatically added to tool groups.
2692 * @property {boolean}
2695 OO
.ui
.Tool
.static.autoAdd
= true;
2698 * Check if this tool is compatible with given data.
2703 * @param {Mixed} data Data to check
2704 * @returns {boolean} Tool can be used with data
2706 OO
.ui
.Tool
.static.isCompatibleWith = function () {
2713 * Handle the toolbar state being updated.
2715 * This is an abstract method that must be overridden in a concrete subclass.
2720 OO
.ui
.Tool
.prototype.onUpdateState = function () {
2722 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
2727 * Handle the tool being selected.
2729 * This is an abstract method that must be overridden in a concrete subclass.
2734 OO
.ui
.Tool
.prototype.onSelect = function () {
2736 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
2741 * Check if the button is active.
2744 * @param {boolean} Button is active
2746 OO
.ui
.Tool
.prototype.isActive = function () {
2751 * Make the button appear active or inactive.
2754 * @param {boolean} state Make button appear active
2756 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
2757 this.active
= !!state
;
2758 if ( this.active
) {
2759 this.$element
.addClass( 'oo-ui-tool-active' );
2761 this.$element
.removeClass( 'oo-ui-tool-active' );
2766 * Get the tool title.
2769 * @param {string|Function} title Title text or a function that returns text
2772 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
2773 this.title
= OO
.ui
.resolveMsg( title
);
2779 * Get the tool title.
2782 * @returns {string} Title text
2784 OO
.ui
.Tool
.prototype.getTitle = function () {
2789 * Get the tool's symbolic name.
2792 * @returns {string} Symbolic name of tool
2794 OO
.ui
.Tool
.prototype.getName = function () {
2795 return this.constructor.static.name
;
2803 OO
.ui
.Tool
.prototype.updateTitle = function () {
2804 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
2805 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
2806 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
2813 .addClass( 'oo-ui-tool-accel' )
2817 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
2818 tooltipParts
.push( this.title
);
2820 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
2821 tooltipParts
.push( accel
);
2823 if ( tooltipParts
.length
) {
2824 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
2826 this.$link
.removeAttr( 'title' );
2835 OO
.ui
.Tool
.prototype.destroy = function () {
2836 this.toolbar
.disconnect( this );
2837 this.$element
.remove();
2840 * Collection of tool groups.
2843 * @extends OO.ui.Element
2844 * @mixins OO.EventEmitter
2845 * @mixins OO.ui.GroupElement
2848 * @param {OO.Factory} toolFactory Factory for creating tools
2849 * @param {Object} [options] Configuration options
2850 * @cfg {boolean} [actions] Add an actions section opposite to the tools
2851 * @cfg {boolean} [shadow] Add a shadow below the toolbar
2853 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, options
) {
2854 // Configuration initialization
2855 options
= options
|| {};
2857 // Parent constructor
2858 OO
.ui
.Element
.call( this, options
);
2860 // Mixin constructors
2861 OO
.EventEmitter
.call( this );
2862 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ) );
2865 this.toolFactory
= toolFactory
;
2868 this.$bar
= this.$( '<div>' );
2869 this.$actions
= this.$( '<div>' );
2870 this.initialized
= false;
2874 .add( this.$bar
).add( this.$group
).add( this.$actions
)
2875 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2878 this.$group
.addClass( 'oo-ui-toolbar-tools' );
2879 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
2880 if ( options
.actions
) {
2881 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
2882 this.$bar
.append( this.$actions
);
2884 this.$bar
.append( '<div style="clear:both"></div>' );
2885 if ( options
.shadow
) {
2886 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
2888 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
2893 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
2895 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
2896 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
2901 * Get the tool factory.
2904 * @returns {OO.Factory} Tool factory
2906 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
2907 return this.toolFactory
;
2911 * Handles mouse down events.
2914 * @param {jQuery.Event} e Mouse down event
2916 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
2917 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
2918 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
2919 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
2925 * Sets up handles and preloads required information for the toolbar to work.
2926 * This must be called immediately after it is attached to a visible document.
2928 OO
.ui
.Toolbar
.prototype.initialize = function () {
2929 this.initialized
= true;
2935 * Tools can be specified in the following ways:
2936 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
2937 * - All tools in a group: `{ 'group': 'group-name' }`
2938 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
2941 * @param {Object.<string,Array>} groups List of tool group configurations
2942 * @param {Array|string} [groups.include] Tools to include
2943 * @param {Array|string} [groups.exclude] Tools to exclude
2944 * @param {Array|string} [groups.promote] Tools to promote to the beginning
2945 * @param {Array|string} [groups.demote] Tools to demote to the end
2947 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
2948 var i
, len
, type
, group
,
2950 // TODO: Use a registry instead
2951 defaultType
= 'bar',
2953 'bar': OO
.ui
.BarToolGroup
,
2954 'list': OO
.ui
.ListToolGroup
,
2955 'menu': OO
.ui
.MenuToolGroup
2958 // Cleanup previous groups
2961 // Build out new groups
2962 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
2964 if ( group
.include
=== '*' ) {
2965 // Apply defaults to catch-all groups
2966 if ( group
.type
=== undefined ) {
2967 group
.type
= 'list';
2969 if ( group
.label
=== undefined ) {
2970 group
.label
= 'ooui-toolbar-more';
2973 type
= constructors
[group
.type
] ? group
.type
: defaultType
;
2975 new constructors
[type
]( this, $.extend( { '$': this.$ }, group
) )
2978 this.addItems( items
);
2982 * Remove all tools and groups from the toolbar.
2984 OO
.ui
.Toolbar
.prototype.reset = function () {
2989 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2990 this.items
[i
].destroy();
2996 * Destroys toolbar, removing event handlers and DOM elements.
2998 * Call this whenever you are done using a toolbar.
3000 OO
.ui
.Toolbar
.prototype.destroy = function () {
3002 this.$element
.remove();
3006 * Check if tool has not been used yet.
3008 * @param {string} name Symbolic name of tool
3009 * @return {boolean} Tool is available
3011 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
3012 return !this.tools
[name
];
3016 * Prevent tool from being used again.
3018 * @param {OO.ui.Tool} tool Tool to reserve
3020 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
3021 this.tools
[tool
.getName()] = tool
;
3025 * Allow tool to be used again.
3027 * @param {OO.ui.Tool} tool Tool to release
3029 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
3030 delete this.tools
[tool
.getName()];
3034 * Get accelerator label for tool.
3036 * This is a stub that should be overridden to provide access to accelerator information.
3038 * @param {string} name Symbolic name of tool
3039 * @returns {string|undefined} Tool accelerator label if available
3041 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
3045 * Factory for tools.
3048 * @extends OO.Factory
3051 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3052 // Parent constructor
3053 OO
.Factory
.call( this );
3058 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3062 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3063 var i
, len
, included
, promoted
, demoted
,
3067 // Collect included and not excluded tools
3068 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3071 promoted
= this.extract( promote
, used
);
3072 demoted
= this.extract( demote
, used
);
3075 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3076 if ( !used
[included
[i
]] ) {
3077 auto
.push( included
[i
] );
3081 return promoted
.concat( auto
).concat( demoted
);
3085 * Get a flat list of names from a list of names or groups.
3087 * Tools can be specified in the following ways:
3088 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3089 * - All tools in a group: `{ 'group': 'group-name' }`
3090 * - All tools: `'*'`
3093 * @param {Array|string} collection List of tools
3094 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3095 * names will be added as properties
3096 * @return {string[]} List of extracted names
3098 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3099 var i
, len
, item
, name
, tool
,
3102 if ( collection
=== '*' ) {
3103 for ( name
in this.registry
) {
3104 tool
= this.registry
[name
];
3106 // Only add tools by group name when auto-add is enabled
3107 tool
.static.autoAdd
&&
3108 // Exclude already used tools
3109 ( !used
|| !used
[name
] )
3117 } else if ( Array
.isArray( collection
) ) {
3118 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3119 item
= collection
[i
];
3120 // Allow plain strings as shorthand for named tools
3121 if ( typeof item
=== 'string' ) {
3122 item
= { 'name': item
};
3124 if ( OO
.isPlainObject( item
) ) {
3126 for ( name
in this.registry
) {
3127 tool
= this.registry
[name
];
3129 // Include tools with matching group
3130 tool
.static.group
=== item
.group
&&
3131 // Only add tools by group name when auto-add is enabled
3132 tool
.static.autoAdd
&&
3133 // Exclude already used tools
3134 ( !used
|| !used
[name
] )
3143 // Include tools with matching name and exclude already used tools
3144 else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3145 names
.push( item
.name
);
3147 used
[item
.name
] = true;
3156 * Collection of tools.
3160 * @extends OO.ui.Widget
3161 * @mixins OO.ui.GroupElement
3163 * Tools can be specified in the following ways:
3164 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3165 * - All tools in a group: `{ 'group': 'group-name' }`
3166 * - All tools: `'*'`
3169 * @param {OO.ui.Toolbar} toolbar
3170 * @param {Object} [config] Configuration options
3171 * @cfg {Array|string} [include=[]] List of tools to include
3172 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3173 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3174 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3176 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3177 // Configuration initialization
3178 config
= config
|| {};
3180 // Parent constructor
3181 OO
.ui
.Widget
.call( this, config
);
3183 // Mixin constructors
3184 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ) );
3187 this.toolbar
= toolbar
;
3189 this.pressed
= null;
3190 this.include
= config
.include
|| [];
3191 this.exclude
= config
.exclude
|| [];
3192 this.promote
= config
.promote
|| [];
3193 this.demote
= config
.demote
|| [];
3194 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3198 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3199 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3200 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3201 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3203 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3206 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3208 .addClass( 'oo-ui-toolGroup' )
3209 .append( this.$group
);
3215 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3217 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3225 /* Static Properties */
3228 * Show labels in tooltips.
3231 * @property {boolean}
3234 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3237 * Show acceleration labels in tooltips.
3240 * @property {boolean}
3243 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3248 * Handle mouse down events.
3251 * @param {jQuery.Event} e Mouse down event
3253 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3254 if ( !this.disabled
&& e
.which
=== 1 ) {
3255 this.pressed
= this.getTargetTool( e
);
3256 if ( this.pressed
) {
3257 this.pressed
.setActive( true );
3258 this.getElementDocument().addEventListener(
3259 'mouseup', this.onCapturedMouseUpHandler
, true
3267 * Handle captured mouse up events.
3270 * @param {Event} e Mouse up event
3272 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3273 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3274 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3275 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3276 this.onMouseUp( e
);
3280 * Handle mouse up events.
3283 * @param {jQuery.Event} e Mouse up event
3285 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3286 var tool
= this.getTargetTool( e
);
3288 if ( !this.disabled
&& e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3289 this.pressed
.onSelect();
3292 this.pressed
= null;
3297 * Handle mouse over events.
3300 * @param {jQuery.Event} e Mouse over event
3302 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3303 var tool
= this.getTargetTool( e
);
3305 if ( this.pressed
&& this.pressed
=== tool
) {
3306 this.pressed
.setActive( true );
3311 * Handle mouse out events.
3314 * @param {jQuery.Event} e Mouse out event
3316 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3317 var tool
= this.getTargetTool( e
);
3319 if ( this.pressed
&& this.pressed
=== tool
) {
3320 this.pressed
.setActive( false );
3325 * Get the closest tool to a jQuery.Event.
3327 * Only tool links are considered, which prevents other elements in the tool such as popups from
3328 * triggering tool group interactions.
3332 * @param {jQuery.Event} e
3333 * @returns {OO.ui.Tool|null} Tool, `null` if none was found
3335 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3337 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3339 if ( $item
.length
) {
3340 tool
= $item
.parent().data( 'oo-ui-tool' );
3343 return tool
&& !tool
.isDisabled() ? tool
: null;
3347 * Handle tool registry register events.
3349 * If a tool is registered after the group is created, we must repopulate the list to account for:
3350 * - a tool being added that may be included
3351 * - a tool already included being overridden
3353 * @param {string} name Symbolic name of tool
3355 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3360 * Get the toolbar this group is in.
3362 * @return {OO.ui.Toolbar} Toolbar of group
3364 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3365 return this.toolbar
;
3369 * Add and remove tools based on configuration.
3373 OO
.ui
.ToolGroup
.prototype.populate = function () {
3374 var i
, len
, name
, tool
,
3375 toolFactory
= this.toolbar
.getToolFactory(),
3379 list
= this.toolbar
.getToolFactory().getTools(
3380 this.include
, this.exclude
, this.promote
, this.demote
3383 // Build a list of needed tools
3384 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3388 toolFactory
.lookup( name
) &&
3389 // Tool is available or is already in this group
3390 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3392 tool
= this.tools
[name
];
3394 // Auto-initialize tools on first use
3395 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3398 this.toolbar
.reserveTool( tool
);
3403 // Remove tools that are no longer needed
3404 for ( name
in this.tools
) {
3405 if ( !names
[name
] ) {
3406 this.tools
[name
].destroy();
3407 this.toolbar
.releaseTool( this.tools
[name
] );
3408 remove
.push( this.tools
[name
] );
3409 delete this.tools
[name
];
3412 if ( remove
.length
) {
3413 this.removeItems( remove
);
3415 // Update emptiness state
3417 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3419 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3421 // Re-add tools (moving existing ones to new locations)
3422 this.addItems( add
);
3426 * Destroy tool group.
3430 OO
.ui
.ToolGroup
.prototype.destroy = function () {
3434 this.toolbar
.getToolFactory().disconnect( this );
3435 for ( name
in this.tools
) {
3436 this.toolbar
.releaseTool( this.tools
[name
] );
3437 this.tools
[name
].disconnect( this ).destroy();
3438 delete this.tools
[name
];
3440 this.$element
.remove();
3443 * Layout made of a fieldset and optional legend.
3446 * @extends OO.ui.Layout
3447 * @mixins OO.ui.LabeledElement
3450 * @param {Object} [config] Configuration options
3451 * @cfg {string} [icon] Symbolic icon name
3453 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
3454 // Config initialization
3455 config
= config
|| {};
3457 // Parent constructor
3458 OO
.ui
.Layout
.call( this, config
);
3460 // Mixin constructors
3461 OO
.ui
.LabeledElement
.call( this, this.$( '<legend>' ), config
);
3464 if ( config
.icon
) {
3465 this.$element
.addClass( 'oo-ui-fieldsetLayout-decorated' );
3466 this.$label
.addClass( 'oo-ui-icon-' + config
.icon
);
3468 this.$element
.addClass( 'oo-ui-fieldsetLayout' );
3469 if ( config
.icon
|| config
.label
) {
3471 .addClass( 'oo-ui-fieldsetLayout-labeled' )
3472 .append( this.$label
);
3478 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
3480 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
3482 /* Static Properties */
3484 OO
.ui
.FieldsetLayout
.static.tagName
= 'fieldset';
3486 * Layout made of proportionally sized columns and rows.
3489 * @extends OO.ui.Layout
3492 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
3493 * @param {Object} [config] Configuration options
3494 * @cfg {number[]} [widths] Widths of columns as ratios
3495 * @cfg {number[]} [heights] Heights of columns as ratios
3497 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
3500 // Config initialization
3501 config
= config
|| {};
3503 // Parent constructor
3504 OO
.ui
.Layout
.call( this, config
);
3512 this.$element
.addClass( 'oo-ui-gridLayout' );
3513 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
3514 this.panels
.push( panels
[i
] );
3515 this.$element
.append( panels
[i
].$element
);
3517 if ( config
.widths
|| config
.heights
) {
3518 this.layout( config
.widths
|| [1], config
.heights
|| [1] );
3520 // Arrange in columns by default
3522 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
3525 this.layout( widths
, [1] );
3531 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
3543 /* Static Properties */
3545 OO
.ui
.GridLayout
.static.tagName
= 'div';
3550 * Set grid dimensions.
3553 * @param {number[]} widths Widths of columns as ratios
3554 * @param {number[]} heights Heights of rows as ratios
3556 * @throws {Error} If grid is not large enough to fit all panels
3558 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
3562 cols
= widths
.length
,
3563 rows
= heights
.length
;
3565 // Verify grid is big enough to fit panels
3566 if ( cols
* rows
< this.panels
.length
) {
3567 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
3570 // Sum up denominators
3571 for ( x
= 0; x
< cols
; x
++ ) {
3574 for ( y
= 0; y
< rows
; y
++ ) {
3580 for ( x
= 0; x
< cols
; x
++ ) {
3581 this.widths
[x
] = widths
[x
] / xd
;
3583 for ( y
= 0; y
< rows
; y
++ ) {
3584 this.heights
[y
] = heights
[y
] / yd
;
3588 this.emit( 'layout' );
3592 * Update panel positions and sizes.
3597 OO
.ui
.GridLayout
.prototype.update = function () {
3605 cols
= this.widths
.length
,
3606 rows
= this.heights
.length
;
3608 for ( y
= 0; y
< rows
; y
++ ) {
3609 for ( x
= 0; x
< cols
; x
++ ) {
3610 panel
= this.panels
[i
];
3611 width
= this.widths
[x
];
3612 height
= this.heights
[y
];
3614 'width': Math
.round( width
* 100 ) + '%',
3615 'height': Math
.round( height
* 100 ) + '%',
3616 'top': Math
.round( top
* 100 ) + '%'
3619 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
3620 dimensions
.right
= Math
.round( left
* 100 ) + '%';
3622 dimensions
.left
= Math
.round( left
* 100 ) + '%';
3624 panel
.$element
.css( dimensions
);
3632 this.emit( 'update' );
3636 * Get a panel at a given position.
3638 * The x and y position is affected by the current grid layout.
3641 * @param {number} x Horizontal position
3642 * @param {number} y Vertical position
3643 * @returns {OO.ui.PanelLayout} The panel at the given postion
3645 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
3646 return this.panels
[( x
* this.widths
.length
) + y
];
3649 * Layout containing a series of pages.
3652 * @extends OO.ui.Layout
3655 * @param {Object} [config] Configuration options
3656 * @cfg {boolean} [continuous=false] Show all pages, one after another
3657 * @cfg {boolean} [autoFocus=false] Focus on the first focusable element when changing to a page
3658 * @cfg {boolean} [outlined=false] Show an outline
3659 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
3660 * @cfg {Object[]} [adders] List of adders for controls, each with name, icon and title properties
3662 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
3663 // Initialize configuration
3664 config
= config
|| {};
3666 // Parent constructor
3667 OO
.ui
.Layout
.call( this, config
);
3670 this.currentPageName
= null;
3672 this.ignoreFocus
= false;
3673 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
3674 this.autoFocus
= !!config
.autoFocus
;
3675 this.outlined
= !!config
.outlined
;
3676 if ( this.outlined
) {
3677 this.editable
= !!config
.editable
;
3678 this.adders
= config
.adders
|| null;
3679 this.outlineControlsWidget
= null;
3680 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
3681 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
3682 this.gridLayout
= new OO
.ui
.GridLayout(
3683 [this.outlinePanel
, this.stackLayout
], { '$': this.$, 'widths': [1, 2] }
3685 if ( this.editable
) {
3686 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
3688 { '$': this.$, 'adders': this.adders
}
3694 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
3695 if ( this.outlined
) {
3696 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
3697 // Event 'focus' does not bubble, but 'focusin' does
3698 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
3702 this.$element
.addClass( 'oo-ui-bookletLayout' );
3703 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
3704 if ( this.outlined
) {
3705 this.outlinePanel
.$element
3706 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
3707 .append( this.outlineWidget
.$element
);
3708 if ( this.editable
) {
3709 this.outlinePanel
.$element
3710 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
3711 .append( this.outlineControlsWidget
.$element
);
3713 this.$element
.append( this.gridLayout
.$element
);
3715 this.$element
.append( this.stackLayout
.$element
);
3721 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
3727 * @param {OO.ui.PageLayout} page Current page
3732 * @param {OO.ui.PageLayout[]} page Added pages
3733 * @param {number} index Index pages were added at
3738 * @param {OO.ui.PageLayout[]} pages Removed pages
3744 * Handle stack layout focus.
3747 * @param {jQuery.Event} e Focusin event
3749 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
3752 if ( this.ignoreFocus
) {
3753 // Avoid recursion from programmatic focus trigger in #onStackLayoutSet
3757 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
3758 for ( name
in this.pages
) {
3759 if ( this.pages
[ name
].$element
[0] === $target
[0] ) {
3760 this.setPage( name
);
3767 * Handle stack layout set events.
3770 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
3772 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
3774 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
3775 this.ignoreFocus
= true;
3776 if ( this.autoFocus
) {
3777 page
.$element
.find( ':input:first' ).focus();
3779 this.ignoreFocus
= false;
3785 * Handle outline widget select events.
3788 * @param {OO.ui.OptionWidget|null} item Selected item
3790 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
3792 this.setPage( item
.getData() );
3797 * Check if booklet has an outline.
3800 * @returns {boolean} Booklet is outlined
3802 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
3803 return this.outlined
;
3807 * Check if booklet has editing controls.
3810 * @returns {boolean} Booklet is outlined
3812 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
3813 return this.editable
;
3817 * Get the outline widget.
3820 * @returns {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
3822 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
3823 return this.outlineWidget
;
3827 * Get the outline controls widget. If the outline is not editable, null is returned.
3830 * @returns {OO.ui.OutlineControlsWidget|null} The outline controls widget.
3832 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
3833 return this.outlineControlsWidget
;
3837 * Get a page by name.
3840 * @param {string} name Symbolic name of page
3841 * @returns {OO.ui.PageLayout|undefined} Page, if found
3843 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
3844 return this.pages
[name
];
3848 * Get the current page name.
3851 * @returns {string|null} Current page name
3853 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
3854 return this.currentPageName
;
3858 * Add a page to the layout.
3860 * When pages are added with the same names as existing pages, the existing pages will be
3861 * automatically removed before the new pages are added.
3864 * @param {OO.ui.PageLayout[]} pages Pages to add
3865 * @param {number} index Index to insert pages after
3869 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
3870 var i
, len
, name
, page
,
3874 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
3876 name
= page
.getName();
3877 if ( name
in this.pages
) {
3878 // Remove page with same name
3879 remove
.push( this.pages
[name
] );
3881 this.pages
[page
.getName()] = page
;
3882 if ( this.outlined
) {
3883 items
.push( new OO
.ui
.BookletOutlineItemWidget( name
, page
, { '$': this.$ } ) );
3886 if ( remove
.length
) {
3887 this.removePages( remove
);
3890 if ( this.outlined
&& items
.length
) {
3891 this.outlineWidget
.addItems( items
, index
);
3892 this.updateOutlineWidget();
3894 this.stackLayout
.addItems( pages
, index
);
3895 this.emit( 'add', pages
, index
);
3901 * Remove a page from the layout.
3907 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
3908 var i
, len
, name
, page
,
3911 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
3913 name
= page
.getName();
3914 delete this.pages
[name
];
3915 if ( this.outlined
) {
3916 items
.push( this.outlineWidget
.getItemFromData( name
) );
3919 if ( this.outlined
&& items
.length
) {
3920 this.outlineWidget
.removeItems( items
);
3921 this.updateOutlineWidget();
3923 this.stackLayout
.removeItems( pages
);
3924 this.emit( 'remove', pages
);
3930 * Clear all pages from the layout.
3936 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
3937 var pages
= this.stackLayout
.getItems();
3940 this.currentPageName
= null;
3941 if ( this.outlined
) {
3942 this.outlineWidget
.clearItems();
3944 this.stackLayout
.clearItems();
3946 this.emit( 'remove', pages
);
3952 * Set the current page by name.
3956 * @param {string} name Symbolic name of page
3958 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
3960 page
= this.pages
[name
];
3962 if ( this.outlined
) {
3963 selectedItem
= this.outlineWidget
.getSelectedItem();
3964 if ( selectedItem
&& selectedItem
.getData() !== name
) {
3965 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
3970 this.currentPageName
= name
;
3971 this.stackLayout
.setItem( page
);
3972 this.emit( 'set', page
);
3977 * Call this after adding or removing items from the OutlineWidget.
3982 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
3983 // Auto-select first item when nothing is selected anymore
3984 if ( !this.outlineWidget
.getSelectedItem() ) {
3985 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
3991 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
3994 * @extends OO.ui.Layout
3997 * @param {Object} [config] Configuration options
3998 * @cfg {boolean} [scrollable] Allow vertical scrolling
3999 * @cfg {boolean} [padded] Pad the content from the edges
4001 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
4002 // Config initialization
4003 config
= config
|| {};
4005 // Parent constructor
4006 OO
.ui
.Layout
.call( this, config
);
4009 this.$element
.addClass( 'oo-ui-panelLayout' );
4010 if ( config
.scrollable
) {
4011 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
4014 if ( config
.padded
) {
4015 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
4018 // Add directionality class:
4019 this.$element
.addClass( 'oo-ui-' + OO
.ui
.Element
.getDir( this.$.context
) );
4024 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
4026 * Page within an OO.ui.BookletLayout.
4029 * @extends OO.ui.PanelLayout
4032 * @param {string} name Unique symbolic name of page
4033 * @param {Object} [config] Configuration options
4034 * @param {string} [icon=''] Symbolic name of icon to display in outline
4035 * @param {string} [indicator=''] Symbolic name of indicator to display in outline
4036 * @param {string} [indicatorTitle=''] Description of indicator meaning to display in outline
4037 * @param {string} [label=''] Label to display in outline
4038 * @param {number} [level=0] Indentation level of item in outline
4039 * @param {boolean} [movable=false] Page should be movable using outline controls
4041 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
4042 // Configuration initialization
4043 config
= $.extend( { 'scrollable': true }, config
);
4045 // Parent constructor
4046 OO
.ui
.PanelLayout
.call( this, config
);
4050 this.icon
= config
.icon
|| '';
4051 this.indicator
= config
.indicator
|| '';
4052 this.indicatorTitle
= OO
.ui
.resolveMsg( config
.indicatorTitle
) || '';
4053 this.label
= OO
.ui
.resolveMsg( config
.label
) || '';
4054 this.level
= config
.level
|| 0;
4055 this.movable
= !!config
.movable
;
4058 this.$element
.addClass( 'oo-ui-pageLayout' );
4063 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4070 * @returns {string} Symbolic name of page
4072 OO
.ui
.PageLayout
.prototype.getName = function () {
4079 * @returns {string} Symbolic name of icon
4081 OO
.ui
.PageLayout
.prototype.getIcon = function () {
4086 * Get page indicator.
4088 * @returns {string} Symbolic name of indicator
4090 OO
.ui
.PageLayout
.prototype.getIndicator = function () {
4091 return this.indicator
;
4095 * Get page indicator label.
4097 * @returns {string} Description of indicator meaning
4099 OO
.ui
.PageLayout
.prototype.getIndicatorTitle = function () {
4100 return this.indicatorTitle
;
4106 * @returns {string} Label text
4108 OO
.ui
.PageLayout
.prototype.getLabel = function () {
4113 * Get outline item indentation level.
4115 * @returns {number} Indentation level
4117 OO
.ui
.PageLayout
.prototype.getLevel = function () {
4122 * Check if page is movable using outline controls.
4124 * @returns {boolean} Page is movable
4126 OO
.ui
.PageLayout
.prototype.isMovable = function () {
4127 return this.movable
;
4130 * Layout containing a series of mutually exclusive pages.
4133 * @extends OO.ui.PanelLayout
4134 * @mixins OO.ui.GroupElement
4137 * @param {Object} [config] Configuration options
4138 * @cfg {boolean} [continuous=false] Show all pages, one after another
4139 * @cfg {string} [icon=''] Symbolic icon name
4141 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4142 // Config initialization
4143 config
= $.extend( { 'scrollable': true }, config
);
4145 // Parent constructor
4146 OO
.ui
.PanelLayout
.call( this, config
);
4148 // Mixin constructors
4149 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4152 this.currentItem
= null;
4153 this.continuous
= !!config
.continuous
;
4156 this.$element
.addClass( 'oo-ui-stackLayout' );
4157 if ( this.continuous
) {
4158 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4164 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4166 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4172 * @param {OO.ui.PanelLayout|null} [item] Current item
4180 * Adding an existing item (by value) will move it.
4183 * @param {OO.ui.PanelLayout[]} items Items to add
4184 * @param {number} [index] Index to insert items after
4187 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
4188 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
4190 if ( !this.currentItem
&& items
.length
) {
4191 this.setItem( items
[0] );
4200 * Items will be detached, not removed, so they can be used later.
4203 * @param {OO.ui.PanelLayout[]} items Items to remove
4206 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
4207 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
4208 if ( items
.indexOf( this.currentItem
) !== -1 ) {
4209 this.currentItem
= null;
4210 if ( !this.currentItem
&& this.items
.length
) {
4211 this.setItem( this.items
[0] );
4221 * Items will be detached, not removed, so they can be used later.
4226 OO
.ui
.StackLayout
.prototype.clearItems = function () {
4227 this.currentItem
= null;
4228 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
4236 * Any currently shown item will be hidden.
4239 * @param {OO.ui.PanelLayout} item Item to show
4242 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
4243 if ( !this.continuous
) {
4244 this.$items
.css( 'display', '' );
4246 if ( this.items
.indexOf( item
) !== -1 ) {
4247 if ( !this.continuous
) {
4248 item
.$element
.css( 'display', 'block' );
4253 this.currentItem
= item
;
4254 this.emit( 'set', item
);
4259 * Horizontal bar layout of tools as icon buttons.
4263 * @extends OO.ui.ToolGroup
4266 * @param {OO.ui.Toolbar} toolbar
4267 * @param {Object} [config] Configuration options
4269 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
4270 // Parent constructor
4271 OO
.ui
.ToolGroup
.call( this, toolbar
, config
);
4274 this.$element
.addClass( 'oo-ui-barToolGroup' );
4279 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
4281 /* Static Properties */
4283 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
4285 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
4287 * Popup list of tools with an icon and optional label.
4291 * @extends OO.ui.ToolGroup
4292 * @mixins OO.ui.IconedElement
4293 * @mixins OO.ui.IndicatedElement
4294 * @mixins OO.ui.LabeledElement
4295 * @mixins OO.ui.TitledElement
4296 * @mixins OO.ui.ClippableElement
4299 * @param {OO.ui.Toolbar} toolbar
4300 * @param {Object} [config] Configuration options
4302 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
4303 // Configuration initialization
4304 config
= config
|| {};
4306 // Parent constructor
4307 OO
.ui
.ToolGroup
.call( this, toolbar
, config
);
4309 // Mixin constructors
4310 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4311 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
4312 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
4313 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
4314 OO
.ui
.ClippableElement
.call( this, this.$group
);
4317 this.active
= false;
4318 this.dragging
= false;
4319 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
4320 this.$handle
= this.$( '<span>' );
4324 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
4325 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
4330 .addClass( 'oo-ui-popupToolGroup-handle' )
4331 .append( this.$icon
, this.$label
, this.$indicator
);
4333 .addClass( 'oo-ui-popupToolGroup' )
4334 .prepend( this.$handle
);
4339 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
4341 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
4342 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
4343 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
4344 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
4345 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
4347 /* Static Properties */
4352 * Handle focus being lost.
4354 * The event is actually generated from a mouseup, so it is not a normal blur event object.
4357 * @param {jQuery.Event} e Mouse up event
4359 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
4360 // Only deactivate when clicking outside the dropdown element
4361 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
4362 this.setActive( false );
4369 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
4370 this.setActive( false );
4371 return OO
.ui
.ToolGroup
.prototype.onMouseUp
.call( this, e
);
4377 OO
.ui
.PopupToolGroup
.prototype.onMouseDown = function ( e
) {
4378 return OO
.ui
.ToolGroup
.prototype.onMouseDown
.call( this, e
);
4382 * Handle mouse up events.
4385 * @param {jQuery.Event} e Mouse up event
4387 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
4392 * Handle mouse down events.
4395 * @param {jQuery.Event} e Mouse down event
4397 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
4398 if ( !this.disabled
&& e
.which
=== 1 ) {
4399 this.setActive( !this.active
);
4405 * Switch into active mode.
4407 * When active, mouseup events anywhere in the document will trigger deactivation.
4411 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
4413 if ( this.active
!== value
) {
4414 this.active
= value
;
4416 this.setClipping( true );
4417 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
4418 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
4420 this.setClipping( false );
4421 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
4422 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
4427 * Drop down list layout of tools as labeled icon buttons.
4431 * @extends OO.ui.PopupToolGroup
4434 * @param {OO.ui.Toolbar} toolbar
4435 * @param {Object} [config] Configuration options
4437 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
4438 // Parent constructor
4439 OO
.ui
.PopupToolGroup
.call( this, toolbar
, config
);
4442 this.$element
.addClass( 'oo-ui-listToolGroup' );
4447 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
4449 /* Static Properties */
4451 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
4453 * Drop down menu layout of tools as selectable menu items.
4457 * @extends OO.ui.PopupToolGroup
4460 * @param {OO.ui.Toolbar} toolbar
4461 * @param {Object} [config] Configuration options
4463 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
4464 // Configuration initialization
4465 config
= config
|| {};
4467 // Parent constructor
4468 OO
.ui
.PopupToolGroup
.call( this, toolbar
, config
);
4471 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
4474 this.$element
.addClass( 'oo-ui-menuToolGroup' );
4479 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
4481 /* Static Properties */
4483 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
4488 * Handle the toolbar state being updated.
4490 * When the state changes, the title of each active item in the menu will be joined together and
4491 * used as a label for the group. The label will be empty if none of the items are active.
4495 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
4499 for ( name
in this.tools
) {
4500 if ( this.tools
[name
].isActive() ) {
4501 labelTexts
.push( this.tools
[name
].getTitle() );
4505 this.setLabel( labelTexts
.join( ', ' ) );
4508 * UserInterface popup tool.
4512 * @extends OO.ui.Tool
4513 * @mixins OO.ui.PopuppableElement
4516 * @param {OO.ui.Toolbar} toolbar
4517 * @param {Object} [config] Configuration options
4519 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
4520 // Parent constructor
4521 OO
.ui
.Tool
.call( this, toolbar
, config
);
4523 // Mixin constructors
4524 OO
.ui
.PopuppableElement
.call( this, config
);
4528 .addClass( 'oo-ui-popupTool' )
4529 .append( this.popup
.$element
);
4534 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
4536 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
4541 * Handle the tool being selected.
4545 OO
.ui
.PopupTool
.prototype.onSelect = function () {
4546 if ( !this.disabled
) {
4547 if ( this.popup
.isVisible() ) {
4553 this.setActive( false );
4558 * Handle the toolbar state being updated.
4562 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
4563 this.setActive( false );
4568 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
4572 * @extends OO.ui.GroupElement
4575 * @param {jQuery} $group Container node, assigned to #$group
4576 * @param {Object} [config] Configuration options
4578 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
4579 // Parent constructor
4580 OO
.ui
.GroupElement
.call( this, $element
, config
);
4585 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
4590 * Set the disabled state of the widget.
4592 * This will also update the disabled state of child widgets.
4595 * @param {boolean} disabled Disable widget
4598 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
4602 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
4604 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
4606 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4607 this.items
[i
].updateDisabled();
4616 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
4623 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
4630 * Check if widget is disabled.
4632 * Checks parent if present, making disabled state inheritable.
4634 * @returns {boolean} Widget is disabled
4636 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
4637 return this.disabled
||
4638 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
4642 * Set group element is in.
4644 * @param {OO.ui.GroupElement|null} group Group element, null if none
4647 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
4649 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
4651 // Initialize item disabled states
4652 this.updateDisabled();
4657 * Container for multiple related buttons.
4660 * @extends OO.ui.Widget
4661 * @mixin OO.ui.GroupElement
4664 * @param {Object} [config] Configuration options
4666 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
4667 // Parent constructor
4668 OO
.ui
.Widget
.call( this, config
);
4670 // Mixin constructors
4671 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4674 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
4679 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
4681 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
4683 * Creates an OO.ui.ButtonWidget object.
4687 * @extends OO.ui.Widget
4688 * @mixins OO.ui.ButtonedElement
4689 * @mixins OO.ui.IconedElement
4690 * @mixins OO.ui.IndicatedElement
4691 * @mixins OO.ui.LabeledElement
4692 * @mixins OO.ui.TitledElement
4693 * @mixins OO.ui.FlaggableElement
4696 * @param {Object} [config] Configuration options
4697 * @cfg {string} [title=''] Title text
4698 * @cfg {string} [href] Hyperlink to visit when clicked
4699 * @cfg {string} [target] Target to open hyperlink in
4701 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
4702 // Configuration initialization
4703 config
= $.extend( { 'target': '_blank' }, config
);
4705 // Parent constructor
4706 OO
.ui
.Widget
.call( this, config
);
4708 // Mixin constructors
4709 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
4710 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4711 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
4712 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
4713 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
4714 OO
.ui
.FlaggableElement
.call( this, config
);
4717 this.isHyperlink
= typeof config
.href
=== 'string';
4721 'click': OO
.ui
.bind( this.onClick
, this ),
4722 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
4727 .append( this.$icon
, this.$label
, this.$indicator
)
4728 .attr( { 'href': config
.href
, 'target': config
.target
} );
4730 .addClass( 'oo-ui-buttonWidget' )
4731 .append( this.$button
);
4736 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
4738 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
4739 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
4740 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
4741 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
4742 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
4743 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
4754 * Handles mouse click events.
4757 * @param {jQuery.Event} e Mouse click event
4760 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
4761 if ( !this.disabled
) {
4762 this.emit( 'click' );
4763 if ( this.isHyperlink
) {
4771 * Handles keypress events.
4774 * @param {jQuery.Event} e Keypress event
4777 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
4778 if ( !this.disabled
&& e
.which
=== OO
.ui
.Keys
.SPACE
) {
4779 if ( this.isHyperlink
) {
4787 * Creates an OO.ui.InputWidget object.
4791 * @extends OO.ui.Widget
4794 * @param {Object} [config] Configuration options
4795 * @cfg {string} [name=''] HTML input name
4796 * @cfg {string} [value=''] Input value
4797 * @cfg {boolean} [readOnly=false] Prevent changes
4798 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
4800 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
4801 // Config intialization
4802 config
= $.extend( { 'readOnly': false }, config
);
4804 // Parent constructor
4805 OO
.ui
.Widget
.call( this, config
);
4808 this.$input
= this.getInputElement( config
);
4810 this.readOnly
= false;
4811 this.inputFilter
= config
.inputFilter
;
4814 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
4818 .attr( 'name', config
.name
)
4819 .prop( 'disabled', this.disabled
);
4820 this.setReadOnly( config
.readOnly
);
4821 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
4822 this.setValue( config
.value
);
4827 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
4839 * Get input element.
4842 * @param {Object} [config] Configuration options
4843 * @returns {jQuery} Input element
4845 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
4846 return this.$( '<input>' );
4850 * Handle potentially value-changing events.
4853 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
4855 OO
.ui
.InputWidget
.prototype.onEdit = function () {
4856 if ( !this.disabled
) {
4857 // Allow the stack to clear so the value will be updated
4858 setTimeout( OO
.ui
.bind( function () {
4859 this.setValue( this.$input
.val() );
4865 * Get the value of the input.
4868 * @returns {string} Input value
4870 OO
.ui
.InputWidget
.prototype.getValue = function () {
4875 * Sets the direction of the current input, either RTL or LTR
4878 * @param {boolean} isRTL
4880 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
4882 this.$input
.removeClass( 'oo-ui-ltr' );
4883 this.$input
.addClass( 'oo-ui-rtl' );
4885 this.$input
.removeClass( 'oo-ui-rtl' );
4886 this.$input
.addClass( 'oo-ui-ltr' );
4891 * Set the value of the input.
4894 * @param {string} value New value
4898 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
4899 value
= this.sanitizeValue( value
);
4900 if ( this.value
!== value
) {
4902 this.emit( 'change', this.value
);
4904 // Update the DOM if it has changed. Note that with sanitizeValue, it
4905 // is possible for the DOM value to change without this.value changing.
4906 if ( this.$input
.val() !== this.value
) {
4907 this.$input
.val( this.value
);
4913 * Sanitize incoming value.
4915 * Ensures value is a string, and converts undefined and null to empty strings.
4918 * @param {string} value Original value
4919 * @returns {string} Sanitized value
4921 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
4922 if ( value
=== undefined || value
=== null ) {
4924 } else if ( this.inputFilter
) {
4925 return this.inputFilter( String( value
) );
4927 return String( value
);
4932 * Check if the widget is read-only.
4935 * @param {boolean} Input is read-only
4937 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
4938 return this.readOnly
;
4942 * Set the read-only state of the widget.
4944 * This should probably change the widgets's appearance and prevent it from being used.
4947 * @param {boolean} state Make input read-only
4950 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
4951 this.readOnly
= !!state
;
4952 this.$input
.prop( 'readonly', this.readOnly
);
4959 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
4960 OO
.ui
.Widget
.prototype.setDisabled
.call( this, state
);
4961 if ( this.$input
) {
4962 this.$input
.prop( 'disabled', this.disabled
);
4966 * Creates an OO.ui.CheckboxInputWidget object.
4969 * @extends OO.ui.InputWidget
4972 * @param {Object} [config] Configuration options
4974 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
4975 // Parent constructor
4976 OO
.ui
.InputWidget
.call( this, config
);
4979 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
4984 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
4991 * Get input element.
4993 * @returns {jQuery} Input element
4995 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
4996 return this.$( '<input type="checkbox" />' );
5000 * Get checked state of the checkbox
5002 * @returns {boolean} If the checkbox is checked
5004 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
5011 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
5013 if ( this.value
!== value
) {
5015 this.$input
.prop( 'checked', this.value
);
5016 this.emit( 'change', this.value
);
5023 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
5024 if ( !this.disabled
) {
5025 // Allow the stack to clear so the value will be updated
5026 setTimeout( OO
.ui
.bind( function () {
5027 this.setValue( this.$input
.prop( 'checked' ) );
5032 * Creates an OO.ui.CheckboxWidget object.
5035 * @extends OO.ui.CheckboxInputWidget
5036 * @mixins OO.ui.LabeledElement
5039 * @param {Object} [config] Configuration options
5040 * @cfg {string} [label=''] Label
5042 OO
.ui
.CheckboxWidget
= function OoUiCheckboxWidget( config
) {
5043 // Configuration initialization
5044 config
= config
|| {};
5046 // Parent constructor
5047 OO
.ui
.CheckboxInputWidget
.call( this, config
);
5049 // Mixin constructors
5050 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ) , config
);
5054 .addClass( 'oo-ui-checkboxWidget' )
5055 .append( this.$( '<label>' ).append( this.$input
, this.$label
) );
5060 OO
.inheritClass( OO
.ui
.CheckboxWidget
, OO
.ui
.CheckboxInputWidget
);
5062 OO
.mixinClass( OO
.ui
.CheckboxWidget
, OO
.ui
.LabeledElement
);
5064 * Creates an OO.ui.InputLabelWidget object.
5066 * CSS classes will be added to the button for each flag, each prefixed with 'oo-ui-InputLabelWidget-'
5069 * @extends OO.ui.Widget
5070 * @mixins OO.ui.LabeledElement
5073 * @param {Object} [config] Configuration options
5074 * @cfg {OO.ui.InputWidget|null} [input] Related input widget
5076 OO
.ui
.InputLabelWidget
= function OoUiInputLabelWidget( config
) {
5077 // Config intialization
5078 config
= $.extend( { 'input': null }, config
);
5080 // Parent constructor
5081 OO
.ui
.Widget
.call( this, config
);
5083 // Mixin constructors
5084 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
5087 this.input
= config
.input
;
5090 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
5093 this.$element
.addClass( 'oo-ui-inputLabelWidget' );
5098 OO
.inheritClass( OO
.ui
.InputLabelWidget
, OO
.ui
.Widget
);
5100 OO
.mixinClass( OO
.ui
.InputLabelWidget
, OO
.ui
.LabeledElement
);
5102 /* Static Properties */
5104 OO
.ui
.InputLabelWidget
.static.tagName
= 'label';
5109 * Handles mouse click events.
5112 * @param {jQuery.Event} e Mouse click event
5114 OO
.ui
.InputLabelWidget
.prototype.onClick = function () {
5115 if ( !this.disabled
&& this.input
) {
5116 this.input
.$input
.focus();
5121 * Lookup input widget.
5123 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
5124 * events on #lookupMenu to make use of selections.
5130 * @param {OO.ui.TextInputWidget} input Input widget
5131 * @param {Object} [config] Configuration options
5132 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
5134 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
5135 // Config intialization
5136 config
= config
|| {};
5139 this.lookupInput
= input
;
5140 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
5141 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
5142 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
5143 'input': this.lookupInput
,
5144 '$container': config
.$container
5146 this.lookupCache
= {};
5147 this.lookupQuery
= null;
5148 this.lookupRequest
= null;
5149 this.populating
= false;
5152 this.$overlay
.append( this.lookupMenu
.$element
);
5154 this.lookupInput
.$input
.on( {
5155 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
5156 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
5157 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
5159 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
5162 this.$element
.addClass( 'oo-ui-lookupWidget' );
5163 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
5169 * Handle input focus event.
5172 * @param {jQuery.Event} e Input focus event
5174 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
5175 this.openLookupMenu();
5179 * Handle input blur event.
5182 * @param {jQuery.Event} e Input blur event
5184 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
5185 this.lookupMenu
.hide();
5189 * Handle input mouse down event.
5192 * @param {jQuery.Event} e Input mouse down event
5194 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
5195 this.openLookupMenu();
5199 * Handle input change event.
5202 * @param {string} value New input value
5204 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
5205 this.openLookupMenu();
5214 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
5215 var value
= this.lookupInput
.getValue();
5217 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
5218 this.populateLookupMenu();
5219 if ( !this.lookupMenu
.isVisible() ) {
5220 this.lookupMenu
.show();
5223 this.lookupMenu
.clearItems();
5224 this.lookupMenu
.hide();
5231 * Populate lookup menu with current information.
5236 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
5237 if ( !this.populating
) {
5238 this.populating
= true;
5239 this.getLookupMenuItems()
5240 .done( OO
.ui
.bind( function ( items
) {
5241 this.lookupMenu
.clearItems();
5242 if ( items
.length
) {
5243 this.lookupMenu
.show();
5244 this.lookupMenu
.addItems( items
);
5245 this.initializeLookupMenuSelection();
5246 this.openLookupMenu();
5248 this.lookupMenu
.hide();
5250 this.populating
= false;
5252 .fail( OO
.ui
.bind( function () {
5253 this.lookupMenu
.clearItems();
5254 this.populating
= false;
5262 * Set selection in the lookup menu with current information.
5267 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
5268 if ( !this.lookupMenu
.getSelectedItem() ) {
5269 this.lookupMenu
.intializeSelection( this.lookupMenu
.getFirstSelectableItem() );
5271 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
5275 * Get lookup menu items for the current query.
5278 * @returns {jQuery.Promise} Promise object which will be passed menu items as the first argument
5281 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
5282 var value
= this.lookupInput
.getValue(),
5283 deferred
= $.Deferred();
5285 if ( value
&& value
!== this.lookupQuery
) {
5286 // Abort current request if query has changed
5287 if ( this.lookupRequest
) {
5288 this.lookupRequest
.abort();
5289 this.lookupQuery
= null;
5290 this.lookupRequest
= null;
5292 if ( value
in this.lookupCache
) {
5293 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5295 this.lookupQuery
= value
;
5296 this.lookupRequest
= this.getLookupRequest()
5297 .always( OO
.ui
.bind( function () {
5298 this.lookupQuery
= null;
5299 this.lookupRequest
= null;
5301 .done( OO
.ui
.bind( function ( data
) {
5302 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
5303 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5305 .fail( function () {
5309 this.lookupRequest
.always( OO
.ui
.bind( function () {
5314 return deferred
.promise();
5318 * Get a new request object of the current lookup query value.
5322 * @returns {jqXHR} jQuery AJAX object, or promise object with an .abort() method
5324 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
5325 // Stub, implemented in subclass
5330 * Handle successful lookup request.
5332 * Overriding methods should call #populateLookupMenu when results are available and cache results
5333 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
5337 * @param {Mixed} data Response from server
5339 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
5340 // Stub, implemented in subclass
5344 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
5348 * @param {Mixed} data Cached result data, usually an array
5349 * @returns {OO.ui.MenuItemWidget[]} Menu items
5351 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
5352 // Stub, implemented in subclass
5356 * Creates an OO.ui.OptionWidget object.
5360 * @extends OO.ui.Widget
5361 * @mixins OO.ui.IconedElement
5362 * @mixins OO.ui.LabeledElement
5363 * @mixins OO.ui.IndicatedElement
5366 * @param {Mixed} data Option data
5367 * @param {Object} [config] Configuration options
5368 * @cfg {boolean} [selected=false] Select option
5369 * @cfg {boolean} [highlighted=false] Highlight option
5370 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
5372 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
5373 // Config intialization
5374 config
= config
|| {};
5376 // Parent constructor
5377 OO
.ui
.Widget
.call( this, config
);
5379 // Mixin constructors
5380 OO
.ui
.ItemWidget
.call( this );
5381 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5382 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5383 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5387 this.selected
= false;
5388 this.highlighted
= false;
5392 .data( 'oo-ui-optionWidget', this )
5393 .attr( 'rel', config
.rel
)
5394 .addClass( 'oo-ui-optionWidget' )
5395 .append( this.$label
);
5396 this.setSelected( config
.selected
);
5397 this.setHighlighted( config
.highlighted
);
5401 .prepend( this.$icon
)
5402 .append( this.$indicator
);
5407 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
5409 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
5410 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
5411 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
5412 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
5414 /* Static Properties */
5416 OO
.ui
.OptionWidget
.static.tagName
= 'li';
5418 OO
.ui
.OptionWidget
.static.selectable
= true;
5420 OO
.ui
.OptionWidget
.static.highlightable
= true;
5422 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
5427 * Check if option can be selected.
5430 * @returns {boolean} Item is selectable
5432 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
5433 return this.constructor.static.selectable
&& !this.disabled
;
5437 * Check if option can be highlighted.
5440 * @returns {boolean} Item is highlightable
5442 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
5443 return this.constructor.static.highlightable
&& !this.disabled
;
5447 * Check if option is selected.
5450 * @returns {boolean} Item is selected
5452 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
5453 return this.selected
;
5457 * Check if option is highlighted.
5460 * @returns {boolean} Item is highlighted
5462 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
5463 return this.highlighted
;
5467 * Set selected state.
5470 * @param {boolean} [state=false] Select option
5473 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
5474 if ( !this.disabled
&& this.constructor.static.selectable
) {
5475 this.selected
= !!state
;
5476 if ( this.selected
) {
5477 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
5478 if ( this.constructor.static.scrollIntoViewOnSelect
) {
5479 this.scrollElementIntoView();
5482 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
5489 * Set highlighted state.
5492 * @param {boolean} [state=false] Highlight option
5495 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
5496 if ( !this.disabled
&& this.constructor.static.highlightable
) {
5497 this.highlighted
= !!state
;
5498 if ( this.highlighted
) {
5499 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
5501 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
5508 * Make the option's highlight flash.
5511 * @param {Function} [done] Callback to execute when flash effect is complete.
5513 OO
.ui
.OptionWidget
.prototype.flash = function ( done
) {
5514 var $this = this.$element
;
5516 if ( !this.disabled
&& this.constructor.static.highlightable
) {
5517 $this.removeClass( 'oo-ui-optionWidget-highlighted' );
5518 setTimeout( OO
.ui
.bind( function () {
5519 $this.addClass( 'oo-ui-optionWidget-highlighted' );
5521 setTimeout( done
, 100 );
5531 * @returns {Mixed} Option data
5533 OO
.ui
.OptionWidget
.prototype.getData = function () {
5537 * Create an OO.ui.SelectWidget object.
5541 * @extends OO.ui.Widget
5542 * @mixin OO.ui.GroupElement
5545 * @param {Object} [config] Configuration options
5547 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
5548 // Config intialization
5549 config
= config
|| {};
5551 // Parent constructor
5552 OO
.ui
.Widget
.call( this, config
);
5554 // Mixin constructors
5555 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
5558 this.pressed
= false;
5559 this.selecting
= null;
5564 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
5565 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
5566 'mousemove': OO
.ui
.bind( this.onMouseMove
, this ),
5567 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
5568 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
5572 this.$element
.addClass( 'oo-ui-selectWidget' );
5577 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
5579 // Need to mixin base class as well
5580 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
5582 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
5588 * @param {OO.ui.OptionWidget|null} item Highlighted item
5593 * @param {OO.ui.OptionWidget|null} item Selected item
5598 * @param {OO.ui.OptionWidget[]} items Added items
5599 * @param {number} index Index items were added at
5604 * @param {OO.ui.OptionWidget[]} items Removed items
5607 /* Static Properties */
5609 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
5614 * Handle mouse down events.
5618 * @param {jQuery.Event} e Mouse down event
5620 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
5623 if ( !this.disabled
&& e
.which
=== 1 ) {
5624 this.pressed
= true;
5625 item
= this.getTargetItem( e
);
5626 if ( item
&& item
.isSelectable() ) {
5627 this.intializeSelection( item
);
5628 this.selecting
= item
;
5629 this.$( this.$.context
).one( 'mouseup', OO
.ui
.bind( this.onMouseUp
, this ) );
5636 * Handle mouse up events.
5640 * @param {jQuery.Event} e Mouse up event
5642 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
5644 this.pressed
= false;
5645 if ( !this.selecting
) {
5646 item
= this.getTargetItem( e
);
5647 if ( item
&& item
.isSelectable() ) {
5648 this.selecting
= item
;
5651 if ( !this.disabled
&& e
.which
=== 1 && this.selecting
) {
5652 this.selectItem( this.selecting
);
5653 this.selecting
= null;
5659 * Handle mouse move events.
5663 * @param {jQuery.Event} e Mouse move event
5665 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
5668 if ( !this.disabled
&& this.pressed
) {
5669 item
= this.getTargetItem( e
);
5670 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
5671 this.intializeSelection( item
);
5672 this.selecting
= item
;
5679 * Handle mouse over events.
5683 * @param {jQuery.Event} e Mouse over event
5685 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
5688 if ( !this.disabled
) {
5689 item
= this.getTargetItem( e
);
5690 if ( item
&& item
.isHighlightable() ) {
5691 this.highlightItem( item
);
5698 * Handle mouse leave events.
5702 * @param {jQuery.Event} e Mouse over event
5704 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
5705 if ( !this.disabled
) {
5706 this.highlightItem();
5712 * Get the closest item to a jQuery.Event.
5716 * @param {jQuery.Event} e
5717 * @returns {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
5719 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
5720 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
5721 if ( $item
.length
) {
5722 return $item
.data( 'oo-ui-optionWidget' );
5728 * Get selected item.
5731 * @returns {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
5733 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
5736 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5737 if ( this.items
[i
].isSelected() ) {
5738 return this.items
[i
];
5745 * Get highlighted item.
5748 * @returns {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
5750 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
5753 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5754 if ( this.items
[i
].isHighlighted() ) {
5755 return this.items
[i
];
5762 * Get an existing item with equivilant data.
5765 * @param {Object} data Item data to search for
5766 * @returns {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
5768 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
5769 var hash
= OO
.getHash( data
);
5771 if ( hash
in this.hashes
) {
5772 return this.hashes
[hash
];
5779 * Highlight an item.
5781 * Highlighting is mutually exclusive.
5784 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
5788 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
5791 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5792 this.items
[i
].setHighlighted( this.items
[i
] === item
);
5794 this.emit( 'highlight', item
);
5803 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
5807 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
5810 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5811 this.items
[i
].setSelected( this.items
[i
] === item
);
5813 this.emit( 'select', item
);
5819 * Setup selection and highlighting.
5821 * This should be used to synchronize the UI with the model without emitting events that would in
5822 * turn update the model.
5824 * @param {OO.ui.OptionWidget} [item] Item to select
5827 OO
.ui
.SelectWidget
.prototype.intializeSelection = function( item
) {
5828 var i
, len
, selected
;
5830 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5831 selected
= this.items
[i
] === item
;
5832 this.items
[i
].setSelected( selected
);
5833 this.items
[i
].setHighlighted( selected
);
5840 * Get an item relative to another one.
5843 * @param {OO.ui.OptionWidget} item Item to start at
5844 * @param {number} direction Direction to move in
5845 * @returns {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
5847 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
5848 var inc
= direction
> 0 ? 1 : -1,
5849 len
= this.items
.length
,
5850 index
= item
instanceof OO
.ui
.OptionWidget
?
5851 this.items
.indexOf( item
) : ( inc
> 0 ? -1 : 0 ),
5852 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
5854 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
5855 Math
.max( index
, -1 ) :
5856 // Default to n-1 instead of -1, if nothing is selected let's start at the end
5857 Math
.min( index
, len
);
5860 i
= ( i
+ inc
+ len
) % len
;
5861 item
= this.items
[i
];
5862 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
5865 // Stop iterating when we've looped all the way around
5866 if ( i
=== stopAt
) {
5874 * Get the next selectable item.
5877 * @returns {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
5879 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
5882 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5883 item
= this.items
[i
];
5884 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
5895 * When items are added with the same values as existing items, the existing items will be
5896 * automatically removed before the new items are added.
5899 * @param {OO.ui.OptionWidget[]} items Items to add
5900 * @param {number} [index] Index to insert items after
5904 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
5905 var i
, len
, item
, hash
,
5908 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
5910 hash
= OO
.getHash( item
.getData() );
5911 if ( hash
in this.hashes
) {
5912 // Remove item with same value
5913 remove
.push( this.hashes
[hash
] );
5915 this.hashes
[hash
] = item
;
5917 if ( remove
.length
) {
5918 this.removeItems( remove
);
5921 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
5923 // Always provide an index, even if it was omitted
5924 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
5932 * Items will be detached, not removed, so they can be used later.
5935 * @param {OO.ui.OptionWidget[]} items Items to remove
5939 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
5940 var i
, len
, item
, hash
;
5942 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
5944 hash
= OO
.getHash( item
.getData() );
5945 if ( hash
in this.hashes
) {
5946 // Remove existing item
5947 delete this.hashes
[hash
];
5949 if ( item
.isSelected() ) {
5950 this.selectItem( null );
5953 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
5955 this.emit( 'remove', items
);
5963 * Items will be detached, not removed, so they can be used later.
5969 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
5970 var items
= this.items
.slice();
5974 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
5975 this.selectItem( null );
5977 this.emit( 'remove', items
);
5982 * Creates an OO.ui.MenuItemWidget object.
5985 * @extends OO.ui.OptionWidget
5988 * @param {Mixed} data Item data
5989 * @param {Object} [config] Configuration options
5991 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
5992 // Configuration initialization
5993 config
= $.extend( { 'icon': 'check' }, config
);
5995 // Parent constructor
5996 OO
.ui
.OptionWidget
.call( this, data
, config
);
5999 this.$element
.addClass( 'oo-ui-menuItemWidget' );
6004 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
6006 * Create an OO.ui.MenuWidget object.
6009 * @extends OO.ui.SelectWidget
6010 * @mixins OO.ui.ClippableElement
6013 * @param {Object} [config] Configuration options
6014 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
6016 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
6017 // Config intialization
6018 config
= config
|| {};
6020 // Parent constructor
6021 OO
.ui
.SelectWidget
.call( this, config
);
6023 // Mixin constructors
6024 OO
.ui
.ClippableElement
.call( this, this.$group
);
6028 this.$input
= config
.input
? config
.input
.$input
: null;
6029 this.$previousFocus
= null;
6030 this.isolated
= !config
.input
;
6031 this.visible
= false;
6032 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
6035 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
6040 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
6042 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
6047 * Handles key down events.
6050 * @param {jQuery.Event} e Key down event
6052 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
6055 highlightItem
= this.getHighlightedItem();
6057 if ( !this.disabled
&& this.visible
) {
6058 if ( !highlightItem
) {
6059 highlightItem
= this.getSelectedItem();
6061 switch ( e
.keyCode
) {
6062 case OO
.ui
.Keys
.ENTER
:
6063 this.selectItem( highlightItem
);
6067 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
6070 case OO
.ui
.Keys
.DOWN
:
6071 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
6074 case OO
.ui
.Keys
.ESCAPE
:
6075 if ( highlightItem
) {
6076 highlightItem
.setHighlighted( false );
6084 this.highlightItem( nextItem
);
6085 nextItem
.scrollElementIntoView();
6090 e
.stopPropagation();
6097 * Check if the menu is visible.
6100 * @returns {boolean} Menu is visible
6102 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
6103 return this.visible
;
6107 * Bind key down listener
6111 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
6112 if ( this.$input
) {
6113 this.$input
.on( 'keydown', this.onKeyDownHandler
);
6115 // Capture menu navigation keys
6116 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
6121 * Unbind key down listener
6125 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
6126 if ( this.$input
) {
6127 this.$input
.off( 'keydown' );
6129 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
6136 * The menu will stay open if an item is silently selected.
6139 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6142 OO
.ui
.MenuWidget
.prototype.selectItem = function ( item
) {
6144 OO
.ui
.SelectWidget
.prototype.selectItem
.call( this, item
);
6146 if ( !this.disabled
) {
6148 this.disabled
= true;
6149 item
.flash( OO
.ui
.bind( function () {
6151 this.disabled
= false;
6164 * Adding an existing item (by value) will move it.
6167 * @param {OO.ui.MenuItemWidget[]} items Items to add
6168 * @param {number} [index] Index to insert items after
6171 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
6175 OO
.ui
.SelectWidget
.prototype.addItems
.call( this, items
, index
);
6177 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6179 if ( this.visible
) {
6180 // Defer fitting label until
6183 this.newItems
.push( item
);
6196 OO
.ui
.MenuWidget
.prototype.show = function () {
6199 if ( this.items
.length
) {
6200 this.$element
.show();
6201 this.visible
= true;
6202 this.bindKeyDownListener();
6204 // Change focus to enable keyboard navigation
6205 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
6206 this.$previousFocus
= this.$( ':focus' );
6207 this.$input
.focus();
6209 if ( this.newItems
.length
) {
6210 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
6211 this.newItems
[i
].fitLabel();
6216 this.setClipping( true );
6228 OO
.ui
.MenuWidget
.prototype.hide = function () {
6229 this.$element
.hide();
6230 this.visible
= false;
6231 this.unbindKeyDownListener();
6233 if ( this.isolated
&& this.$previousFocus
) {
6234 this.$previousFocus
.focus();
6235 this.$previousFocus
= null;
6238 this.setClipping( false );
6243 * Creates an OO.ui.MenuSectionItemWidget object.
6246 * @extends OO.ui.OptionWidget
6249 * @param {Mixed} data Item data
6250 * @param {Object} [config] Configuration options
6252 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
6253 // Parent constructor
6254 OO
.ui
.OptionWidget
.call( this, data
, config
);
6257 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
6262 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
6264 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
6266 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
6268 * Create an OO.ui.OutlineWidget object.
6271 * @extends OO.ui.SelectWidget
6274 * @param {Object} [config] Configuration options
6276 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
6277 // Config intialization
6278 config
= config
|| {};
6280 // Parent constructor
6281 OO
.ui
.SelectWidget
.call( this, config
);
6284 this.$element
.addClass( 'oo-ui-outlineWidget' );
6289 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
6291 * Creates an OO.ui.OutlineControlsWidget object.
6296 * @param {OO.ui.OutlineWidget} outline Outline to control
6297 * @param {Object} [config] Configuration options
6298 * @cfg {Object[]} [adders] List of icons to show as addable item types, each an object with
6299 * name, title and icon properties
6301 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
6302 // Configuration initialization
6303 config
= config
|| {};
6305 // Parent constructor
6306 OO
.ui
.Widget
.call( this, config
);
6309 this.outline
= outline
;
6311 this.$adders
= this.$( '<div>' );
6312 this.$movers
= this.$( '<div>' );
6313 this.addButton
= new OO
.ui
.ButtonWidget( {
6318 this.upButton
= new OO
.ui
.ButtonWidget( {
6322 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
6324 this.downButton
= new OO
.ui
.ButtonWidget( {
6328 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
6332 outline
.connect( this, {
6333 'select': 'onOutlineChange',
6334 'add': 'onOutlineChange',
6335 'remove': 'onOutlineChange'
6337 this.upButton
.connect( this, { 'click': ['emit', 'move', -1] } );
6338 this.downButton
.connect( this, { 'click': ['emit', 'move', 1] } );
6341 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
6342 this.$adders
.addClass( 'oo-ui-outlineControlsWidget-adders' );
6344 .addClass( 'oo-ui-outlineControlsWidget-movers' )
6345 .append( this.upButton
.$element
, this.downButton
.$element
);
6346 this.$element
.append( this.$adders
, this.$movers
);
6347 if ( config
.adders
&& config
.adders
.length
) {
6348 this.setupAdders( config
.adders
);
6354 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
6360 * @param {number} places Number of places to move
6366 * Handle outline change events.
6370 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
6371 var i
, len
, firstMovable
, lastMovable
,
6373 items
= this.outline
.getItems(),
6374 selectedItem
= this.outline
.getSelectedItem();
6376 if ( selectedItem
&& selectedItem
.isMovable() ) {
6380 while ( ++i
< len
) {
6381 if ( items
[i
].isMovable() ) {
6382 firstMovable
= items
[i
];
6388 if ( items
[i
].isMovable() ) {
6389 lastMovable
= items
[i
];
6394 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
6395 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
6399 * Setup adders icons.
6402 * @param {Object[]} adders List of configuations for adder buttons, each containing a name, title
6405 OO
.ui
.OutlineControlsWidget
.prototype.setupAdders = function ( adders
) {
6406 var i
, len
, addition
, button
,
6407 $buttons
= this.$( [] );
6409 this.$adders
.append( this.addButton
.$element
);
6410 for ( i
= 0, len
= adders
.length
; i
< len
; i
++ ) {
6411 addition
= adders
[i
];
6412 button
= new OO
.ui
.ButtonWidget( {
6413 '$': this.$, 'frameless': true, 'icon': addition
.icon
, 'title': addition
.title
6415 button
.connect( this, { 'click': ['emit', 'add', addition
.name
] } );
6416 this.adders
[addition
.name
] = button
;
6417 this.$adders
.append( button
.$element
);
6418 $buttons
= $buttons
.add( button
.$element
);
6422 * Creates an OO.ui.OutlineItemWidget object.
6425 * @extends OO.ui.OptionWidget
6428 * @param {Mixed} data Item data
6429 * @param {Object} [config] Configuration options
6430 * @cfg {number} [level] Indentation level
6431 * @cfg {boolean} [movable] Allow modification from outline controls
6433 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
6434 // Config intialization
6435 config
= config
|| {};
6437 // Parent constructor
6438 OO
.ui
.OptionWidget
.call( this, data
, config
);
6442 this.movable
= !!config
.movable
;
6445 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
6446 this.setLevel( config
.level
);
6451 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
6453 /* Static Properties */
6455 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
6457 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
6459 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
6461 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
6466 * Check if item is movable.
6468 * Moveablilty is used by outline controls.
6470 * @returns {boolean} Item is movable
6472 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
6473 return this.movable
;
6477 * Get indentation level.
6479 * @returns {number} Indentation level
6481 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
6486 * Set indentation level.
6489 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
6492 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
6493 var levels
= this.constructor.static.levels
,
6494 levelClass
= this.constructor.static.levelClass
,
6497 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
6499 if ( this.level
=== i
) {
6500 this.$element
.addClass( levelClass
+ i
);
6502 this.$element
.removeClass( levelClass
+ i
);
6509 * Creates an OO.ui.BookletOutlineItemWidget object.
6512 * @extends OO.ui.OutlineItemWidget
6515 * @param {Mixed} data Item data
6516 * @param {Object} [config] Configuration options
6518 OO
.ui
.BookletOutlineItemWidget
= function OoUiBookletOutlineItemWidget( data
, page
, config
) {
6519 // Configuration intialization
6520 config
= $.extend( {
6521 'label': page
.getLabel() || data
,
6522 'level': page
.getLevel(),
6523 'icon': page
.getIcon(),
6524 'indicator': page
.getIndicator(),
6525 'indicatorTitle': page
.getIndicatorTitle(),
6526 'movable': page
.isMovable()
6529 // Parent constructor
6530 OO
.ui
.OutlineItemWidget
.call( this, data
, config
);
6533 this.$element
.addClass( 'oo-ui-bookletOutlineItemWidget' );
6538 OO
.inheritClass( OO
.ui
.BookletOutlineItemWidget
, OO
.ui
.OutlineItemWidget
);
6540 * Create an OO.ui.ButtonSelect object.
6543 * @extends OO.ui.OptionWidget
6544 * @mixins OO.ui.ButtonedElement
6545 * @mixins OO.ui.FlaggableElement
6548 * @param {Mixed} data Option data
6549 * @param {Object} [config] Configuration options
6551 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
6552 // Parent constructor
6553 OO
.ui
.OptionWidget
.call( this, data
, config
);
6555 // Mixin constructors
6556 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
6557 OO
.ui
.FlaggableElement
.call( this, config
);
6560 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
6561 this.$button
.append( this.$element
.contents() );
6562 this.$element
.append( this.$button
);
6567 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
6569 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
6570 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
6577 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
6578 OO
.ui
.OptionWidget
.prototype.setSelected
.call( this, state
);
6580 this.setActive( state
);
6585 * Create an OO.ui.ButtonSelect object.
6588 * @extends OO.ui.SelectWidget
6591 * @param {Object} [config] Configuration options
6593 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
6594 // Parent constructor
6595 OO
.ui
.SelectWidget
.call( this, config
);
6598 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
6603 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
6605 * Creates an OO.ui.PopupWidget object.
6608 * @extends OO.ui.Widget
6609 * @mixins OO.ui.LabeledElement
6612 * @param {Object} [config] Configuration options
6613 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
6614 * @cfg {string} [align='center'] Alignment of popup to origin
6615 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
6616 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
6617 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
6618 * @cfg {boolean} [head] Show label and close button at the top
6620 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
6621 // Config intialization
6622 config
= config
|| {};
6624 // Parent constructor
6625 OO
.ui
.Widget
.call( this, config
);
6627 // Mixin constructors
6628 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
6631 this.visible
= false;
6632 this.$popup
= this.$( '<div>' );
6633 this.$head
= this.$( '<div>' );
6634 this.$body
= this.$( '<div>' );
6635 this.$tail
= this.$( '<div>' );
6636 this.$container
= config
.$container
|| this.$( 'body' );
6637 this.autoClose
= !!config
.autoClose
;
6638 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
6639 this.transitionTimeout
= null;
6641 this.align
= config
.align
|| 'center';
6642 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
6643 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
6646 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
6649 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
6650 this.$body
.addClass( 'oo-ui-popupWidget-body' );
6651 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
6653 .addClass( 'oo-ui-popupWidget-head' )
6654 .append( this.$label
, this.closeButton
.$element
);
6655 if ( !config
.head
) {
6659 .addClass( 'oo-ui-popupWidget-popup' )
6660 .append( this.$head
, this.$body
);
6661 this.$element
.hide()
6662 .addClass( 'oo-ui-popupWidget' )
6663 .append( this.$popup
, this.$tail
);
6668 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
6670 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
6685 * Handles mouse down events.
6688 * @param {jQuery.Event} e Mouse down event
6690 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
6693 !$.contains( this.$element
[0], e
.target
) &&
6694 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
6701 * Bind mouse down listener
6705 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
6706 // Capture clicks outside popup
6707 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
6711 * Handles close button click events.
6715 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
6716 if ( this.visible
) {
6722 * Unbind mouse down listener
6726 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
6727 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
6731 * Check if the popup is visible.
6734 * @returns {boolean} Popup is visible
6736 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
6737 return this.visible
;
6741 * Set whether to show a tail.
6744 * @returns {boolean} Make tail visible
6746 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
6748 if ( this.tail
!== value
) {
6751 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
6753 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
6759 * Check if showing a tail.
6762 * @returns {boolean} tail is visible
6764 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
6775 OO
.ui
.PopupWidget
.prototype.show = function () {
6776 if ( !this.visible
) {
6777 this.$element
.show();
6778 this.visible
= true;
6779 this.emit( 'show' );
6780 if ( this.autoClose
) {
6781 this.bindMouseDownListener();
6794 OO
.ui
.PopupWidget
.prototype.hide = function () {
6795 if ( this.visible
) {
6796 this.$element
.hide();
6797 this.visible
= false;
6798 this.emit( 'hide' );
6799 if ( this.autoClose
) {
6800 this.unbindMouseDownListener();
6807 * Updates the position and size.
6810 * @param {number} width Width
6811 * @param {number} height Height
6812 * @param {boolean} [transition=false] Use a smooth transition
6815 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
6817 originOffset
= Math
.round( this.$element
.offset().left
),
6818 containerLeft
= Math
.round( this.$container
.offset().left
),
6819 containerWidth
= this.$container
.innerWidth(),
6820 containerRight
= containerLeft
+ containerWidth
,
6821 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
6822 popupLeft
= popupOffset
- padding
,
6823 popupRight
= popupOffset
+ padding
+ width
+ padding
,
6824 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
6825 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
6827 // Prevent transition from being interrupted
6828 clearTimeout( this.transitionTimeout
);
6830 // Enable transition
6831 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
6834 if ( overlapRight
< 0 ) {
6835 popupOffset
+= overlapRight
;
6836 } else if ( overlapLeft
< 0 ) {
6837 popupOffset
-= overlapLeft
;
6840 // Position body relative to anchor and resize
6842 'left': popupOffset
,
6844 'height': height
=== undefined ? 'auto' : height
6848 // Prevent transitioning after transition is complete
6849 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
6850 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
6853 // Prevent transitioning immediately
6854 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
6860 * Button that shows and hides a popup.
6863 * @extends OO.ui.ButtonWidget
6864 * @mixins OO.ui.PopuppableElement
6867 * @param {Object} [config] Configuration options
6869 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
6870 // Parent constructor
6871 OO
.ui
.ButtonWidget
.call( this, config
);
6873 // Mixin constructors
6874 OO
.ui
.PopuppableElement
.call( this, config
);
6878 .addClass( 'oo-ui-popupButtonWidget' )
6879 .append( this.popup
.$element
);
6884 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
6886 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
6891 * Handles mouse click events.
6894 * @param {jQuery.Event} e Mouse click event
6896 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
6897 // Skip clicks within the popup
6898 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
6902 if ( !this.disabled
) {
6903 if ( this.popup
.isVisible() ) {
6908 OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
6913 * Creates an OO.ui.SearchWidget object.
6916 * @extends OO.ui.Widget
6919 * @param {Object} [config] Configuration options
6920 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6921 * @cfg {string} [value] Initial query value
6923 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
6924 // Configuration intialization
6925 config
= config
|| {};
6927 // Parent constructor
6928 OO
.ui
.Widget
.call( this, config
);
6931 this.query
= new OO
.ui
.TextInputWidget( {
6934 'placeholder': config
.placeholder
,
6935 'value': config
.value
6937 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
6938 this.$query
= this.$( '<div>' );
6939 this.$results
= this.$( '<div>' );
6942 this.query
.connect( this, {
6943 'change': 'onQueryChange',
6944 'enter': 'onQueryEnter'
6946 this.results
.connect( this, {
6947 'highlight': 'onResultsHighlight',
6948 'select': 'onResultsSelect'
6950 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
6954 .addClass( 'oo-ui-searchWidget-query' )
6955 .append( this.query
.$element
);
6957 .addClass( 'oo-ui-searchWidget-results' )
6958 .append( this.results
.$element
);
6960 .addClass( 'oo-ui-searchWidget' )
6961 .append( this.$results
, this.$query
);
6966 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
6972 * @param {Object|null} item Item data or null if no item is highlighted
6977 * @param {Object|null} item Item data or null if no item is selected
6983 * Handle query key down events.
6986 * @param {jQuery.Event} e Key down event
6988 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
6989 var highlightedItem
, nextItem
,
6990 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
6993 highlightedItem
= this.results
.getHighlightedItem();
6994 if ( !highlightedItem
) {
6995 highlightedItem
= this.results
.getSelectedItem();
6997 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
6998 this.results
.highlightItem( nextItem
);
6999 nextItem
.scrollElementIntoView();
7004 * Handle select widget select events.
7006 * Clears existing results. Subclasses should repopulate items according to new query.
7009 * @param {string} value New value
7011 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
7013 this.results
.clearItems();
7017 * Handle select widget enter key events.
7019 * Selects highlighted item.
7022 * @param {string} value New value
7024 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
7026 this.results
.selectItem( this.results
.getHighlightedItem() );
7030 * Handle select widget highlight events.
7033 * @param {OO.ui.OptionWidget} item Highlighted item
7036 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
7037 this.emit( 'highlight', item
? item
.getData() : null );
7041 * Handle select widget select events.
7044 * @param {OO.ui.OptionWidget} item Selected item
7047 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
7048 this.emit( 'select', item
? item
.getData() : null );
7052 * Get the query input.
7055 * @returns {OO.ui.TextInputWidget} Query input
7057 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
7062 * Get the results list.
7065 * @returns {OO.ui.SelectWidget} Select list
7067 OO
.ui
.SearchWidget
.prototype.getResults = function () {
7068 return this.results
;
7071 * Creates an OO.ui.TextInputWidget object.
7074 * @extends OO.ui.InputWidget
7077 * @param {Object} [config] Configuration options
7078 * @cfg {string} [placeholder] Placeholder text
7079 * @cfg {string} [icon] Symbolic name of icon
7080 * @cfg {boolean} [multiline=false] Allow multiple lines of text
7082 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
7083 config
= config
|| {};
7085 // Parent constructor
7086 OO
.ui
.InputWidget
.call( this, config
);
7090 this.multiline
= !!config
.multiline
;
7093 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
7096 this.$element
.addClass( 'oo-ui-textInputWidget' );
7097 if ( config
.icon
) {
7098 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
7099 this.$element
.append(
7101 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
7102 .mousedown( OO
.ui
.bind( function () {
7103 this.$input
.focus();
7108 if ( config
.placeholder
) {
7109 this.$input
.attr( 'placeholder', config
.placeholder
);
7115 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
7120 * User presses enter inside the text box.
7122 * Not called if input is multiline.
7130 * Handles key press events.
7132 * @param {jQuery.Event} e Key press event
7133 * @fires enter If enter key is pressed and input is not multiline
7135 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
7136 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
7137 this.emit( 'enter' );
7142 * Get input element.
7145 * @param {Object} [config] Configuration options
7146 * @returns {jQuery} Input element
7148 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
7149 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
7155 * Checks if input is pending.
7158 * @returns {boolean} Input is pending
7160 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
7161 return !!this.pending
;
7165 * Increases the pending stack.
7170 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
7172 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
7173 this.$input
.addClass( 'oo-ui-texture-pending' );
7178 * Reduces the pending stack.
7185 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
7186 this.pending
= Math
.max( 0, this.pending
- 1 );
7187 if ( !this.pending
) {
7188 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
7189 this.$input
.removeClass( 'oo-ui-texture-pending' );
7194 * Creates an OO.ui.TextInputMenuWidget object.
7197 * @extends OO.ui.MenuWidget
7200 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
7201 * @param {Object} [config] Configuration options
7202 * @cfg {jQuery} [$container=input.$element] Element to render menu under
7204 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
7205 // Parent constructor
7206 OO
.ui
.MenuWidget
.call( this, config
);
7210 this.$container
= config
.$container
|| this.input
.$element
;
7211 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
7214 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
7219 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
7224 * Handle window resize event.
7227 * @param {jQuery.Event} e Window resize event
7229 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
7239 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
7241 OO
.ui
.MenuWidget
.prototype.show
.call( this );
7244 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
7254 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
7256 OO
.ui
.MenuWidget
.prototype.hide
.call( this );
7258 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
7263 * Positions the menu.
7268 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
7270 $container
= this.$container
,
7271 dimensions
= $container
.offset();
7273 // Position under input
7274 dimensions
.top
+= $container
.height();
7276 // Compensate for frame position if in a differnt frame
7277 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
7278 frameOffset
= OO
.ui
.Element
.getRelativePosition(
7279 this.input
.$.frame
.$element
, this.$element
.offsetParent()
7281 dimensions
.left
+= frameOffset
.left
;
7282 dimensions
.top
+= frameOffset
.top
;
7284 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
7285 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
7286 dimensions
.right
= this.$element
.parent().position().left
-
7287 dimensions
.width
- dimensions
.left
;
7288 // Erase the value for 'left':
7289 delete dimensions
.left
;
7293 this.$element
.css( dimensions
);
7294 this.setIdealSize( $container
.width() );
7298 * Mixin for widgets with a boolean state.
7304 * @param {Object} [config] Configuration options
7305 * @cfg {boolean} [value=false] Initial value
7307 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7308 // Configuration initialization
7309 config
= config
|| {};
7315 this.$element
.addClass( 'oo-ui-toggleWidget' );
7316 this.setValue( !!config
.value
);
7323 * @param {boolean} value Changed value
7329 * Get the value of the toggle.
7332 * @returns {boolean} Toggle value
7334 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7339 * Set the value of the toggle.
7342 * @param {boolean} value New value
7346 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7348 if ( this.value
!== value
) {
7350 this.emit( 'change', value
);
7351 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7352 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7358 * @extends OO.ui.ButtonWidget
7359 * @mixins OO.ui.ToggleWidget
7362 * @param {Object} [config] Configuration options
7363 * @cfg {boolean} [value=false] Initial value
7365 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
7366 // Configuration initialization
7367 config
= config
|| {};
7369 // Parent constructor
7370 OO
.ui
.ButtonWidget
.call( this, config
);
7372 // Mixin constructors
7373 OO
.ui
.ToggleWidget
.call( this, config
);
7376 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
7381 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
7383 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
7390 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
7391 if ( !this.disabled
) {
7392 this.setValue( !this.value
);
7396 return OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
7402 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
7404 if ( value
!== this.value
) {
7405 this.setActive( value
);
7409 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
7416 * @extends OO.ui.Widget
7417 * @mixins OO.ui.ToggleWidget
7420 * @param {Object} [config] Configuration options
7421 * @cfg {boolean} [value=false] Initial value
7423 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
7424 // Parent constructor
7425 OO
.ui
.Widget
.call( this, config
);
7427 // Mixin constructors
7428 OO
.ui
.ToggleWidget
.call( this, config
);
7431 this.dragging
= false;
7432 this.dragStart
= null;
7433 this.sliding
= false;
7434 this.$on
= this.$( '<span>' );
7435 this.$grip
= this.$( '<span>' );
7438 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
7441 this.$on
.addClass( 'oo-ui-toggleSwitchWidget-on' );
7442 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
7444 .addClass( 'oo-ui-toggleSwitchWidget' )
7445 .append( this.$on
, this.$grip
);
7450 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
7452 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
7457 * Handles mouse down events.
7460 * @param {jQuery.Event} e Mouse down event
7462 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
7463 if ( !this.disabled
&& e
.which
=== 1 ) {
7464 this.setValue( !this.value
);