2 * OOjs UI v0.1.0-pre (a290673bbd)
3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: Wed Feb 12 2014 13:52:08 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() ) );
181 if ( Array
.isArray( config
.classes
) ) {
182 this.$element
.addClass( config
.classes
.join( ' ' ) );
184 if ( config
.$content
) {
185 this.$element
.append( config
.$content
);
189 /* Static Properties */
196 OO
.ui
.Element
.static = {};
201 * This may be ignored if getTagName is overridden.
207 OO
.ui
.Element
.static.tagName
= 'div';
212 * Gets a jQuery function within a specific document.
215 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
216 * @param {OO.ui.Frame} [frame] Frame of the document context
217 * @returns {Function} Bound jQuery function
219 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
220 function wrapper( selector
) {
221 return $( selector
, wrapper
.context
);
224 wrapper
.context
= this.getDocument( context
);
227 wrapper
.frame
= frame
;
234 * Get the document of an element.
237 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
238 * @returns {HTMLDocument} Document object
239 * @throws {Error} If context is invalid
241 OO
.ui
.Element
.getDocument = function ( obj
) {
243 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
244 ( obj
[0] && obj
[0].ownerDocument
) ||
245 // Empty jQuery selections might have a context
252 ( obj
.nodeType
=== 9 && obj
);
258 throw new Error( 'Invalid context' );
262 * Get the window of an element or document.
265 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
266 * @returns {Window} Window object
268 OO
.ui
.Element
.getWindow = function ( obj
) {
269 var doc
= this.getDocument( obj
);
270 return doc
.parentWindow
|| doc
.defaultView
;
274 * Get the direction of an element or document.
277 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
278 * @returns {string} Text direction, either `ltr` or `rtl`
280 OO
.ui
.Element
.getDir = function ( obj
) {
283 if ( obj
instanceof jQuery
) {
286 isDoc
= obj
.nodeType
=== 9;
287 isWin
= obj
.document
!== undefined;
288 if ( isDoc
|| isWin
) {
294 return $( obj
).css( 'direction' );
298 * Get the offset between two frames.
300 * TODO: Make this function not use recursion.
303 * @param {Window} from Window of the child frame
304 * @param {Window} [to=window] Window of the parent frame
305 * @param {Object} [offset] Offset to start with, used internally
306 * @returns {Object} Offset object, containing left and top properties
308 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
309 var i
, len
, frames
, frame
, rect
;
315 offset
= { 'top': 0, 'left': 0 };
317 if ( from.parent
=== from ) {
321 // Get iframe element
322 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
323 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
324 if ( frames
[i
].contentWindow
=== from ) {
330 // Recursively accumulate offset values
332 rect
= frame
.getBoundingClientRect();
333 offset
.left
+= rect
.left
;
334 offset
.top
+= rect
.top
;
336 this.getFrameOffset( from.parent
, offset
);
343 * Get the offset between two elements.
346 * @param {jQuery} $from
347 * @param {jQuery} $to
348 * @returns {Object} Translated position coordinates, containing top and left properties
350 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
351 var from = $from.offset(),
353 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
357 * Get element border sizes.
360 * @param {HTMLElement} el Element to measure
361 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
363 OO
.ui
.Element
.getBorders = function ( el
) {
364 var doc
= el
.ownerDocument
,
365 win
= doc
.parentWindow
|| doc
.defaultView
,
366 style
= win
&& win
.getComputedStyle
?
367 win
.getComputedStyle( el
, null ) :
370 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
371 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
372 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
373 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
376 'top': Math
.round( top
),
377 'left': Math
.round( left
),
378 'bottom': Math
.round( bottom
),
379 'right': Math
.round( right
)
384 * Get dimensions of an element or window.
387 * @param {HTMLElement|Window} el Element to measure
388 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
390 OO
.ui
.Element
.getDimensions = function ( el
) {
392 doc
= el
.ownerDocument
|| el
.document
,
393 win
= doc
.parentWindow
|| doc
.defaultView
;
395 if ( win
=== el
|| el
=== doc
.documentElement
) {
398 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
400 'top': $win
.scrollTop(),
401 'left': $win
.scrollLeft()
403 'scrollbar': { 'right': 0, 'bottom': 0 },
407 'bottom': $win
.innerHeight(),
408 'right': $win
.innerWidth()
414 'borders': this.getBorders( el
),
416 'top': $el
.scrollTop(),
417 'left': $el
.scrollLeft()
420 'right': $el
.innerWidth() - el
.clientWidth
,
421 'bottom': $el
.innerHeight() - el
.clientHeight
423 'rect': el
.getBoundingClientRect()
429 * Get closest scrollable container.
431 * Traverses up until either a scrollable element or the root is reached, in which case the window
435 * @param {HTMLElement} el Element to find scrollable container for
436 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
437 * @return {HTMLElement|Window} Closest scrollable container
439 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
441 props
= [ 'overflow' ],
442 $parent
= $( el
).parent();
444 if ( dimension
=== 'x' || dimension
=== 'y' ) {
445 props
.push( 'overflow-' + dimension
);
448 while ( $parent
.length
) {
449 if ( $parent
[0] === el
.ownerDocument
.body
) {
454 val
= $parent
.css( props
[i
] );
455 if ( val
=== 'auto' || val
=== 'scroll' ) {
459 $parent
= $parent
.parent();
461 return this.getDocument( el
).body
;
465 * Scroll element into view
468 * @param {HTMLElement} el Element to scroll into view
469 * @param {Object} [config={}] Configuration config
470 * @param {string} [config.duration] jQuery animation duration value
471 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
472 * to scroll in both directions
473 * @param {Function} [config.complete] Function to call when scrolling completes
475 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
476 // Configuration initialization
477 config
= config
|| {};
480 callback
= typeof config
.complete
=== 'function' && config
.complete
,
481 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
483 eld
= this.getDimensions( el
),
484 scd
= this.getDimensions( sc
),
486 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
487 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
488 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
489 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
492 if ( !config
.direction
|| config
.direction
=== 'y' ) {
494 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
495 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
496 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
499 if ( !config
.direction
|| config
.direction
=== 'x' ) {
500 if ( rel
.left
< 0 ) {
501 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
502 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
503 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
506 if ( !$.isEmptyObject( anim
) ) {
507 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
509 $sc
.queue( function ( next
) {
524 * Get the HTML tag name.
526 * Override this method to base the result on instance information.
528 * @returns {string} HTML tag name
530 OO
.ui
.Element
.prototype.getTagName = function () {
531 return this.constructor.static.tagName
;
535 * Get the DOM document.
537 * @returns {HTMLDocument} Document object
539 OO
.ui
.Element
.prototype.getElementDocument = function () {
540 return OO
.ui
.Element
.getDocument( this.$element
);
544 * Get the DOM window.
546 * @returns {Window} Window object
548 OO
.ui
.Element
.prototype.getElementWindow = function () {
549 return OO
.ui
.Element
.getWindow( this.$element
);
553 * Get closest scrollable container.
556 * @see #static-method-getClosestScrollableContainer
558 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
559 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
563 * Scroll element into view
566 * @see #static-method-scrollIntoView
567 * @param {Object} [config={}]
569 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
570 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
577 function handler( e
) {
578 jQuery
.event
.simulate( 'focusin', e
.target
, jQuery
.event
.fix( e
), /* bubble = */ true );
583 var doc
= this.ownerDocument
|| this,
584 attaches
= $.data( doc
, 'ooui-focusin-attaches' );
586 doc
.addEventListener( 'focus', handler
, true );
588 $.data( doc
, 'ooui-focusin-attaches', ( attaches
|| 0 ) + 1 );
590 teardown: function () {
591 var doc
= this.ownerDocument
|| this,
592 attaches
= $.data( doc
, 'ooui-focusin-attaches' ) - 1;
594 doc
.removeEventListener( 'focus', handler
, true );
595 $.removeData( doc
, 'ooui-focusin-attaches' );
597 $.data( doc
, 'ooui-focusin-attaches', attaches
);
603 * Bind a handler for an event on the DOM element.
605 * Uses jQuery internally for everything except for events which are
606 * known to have issues in the browser or in jQuery. This method
607 * should become obsolete eventually.
609 * @param {string} event
610 * @param {Function} callback
612 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
615 if ( event
=== 'focusin' ) {
616 // jQuery 1.8.3 has a bug with handling focusin events inside iframes.
617 // Firefox doesn't support focusin at all, so we listen for 'focus' on the
618 // document, and simulate a 'focusin' event on the target element and make
619 // it bubble from there.
621 // - http://jsfiddle.net/sw3hr/
622 // - http://bugs.jquery.com/ticket/14180
623 // - https://github.com/jquery/jquery/commit/1cecf64e5aa4153
625 // Replace jQuery's override with our own
626 orig
= $.event
.special
.focusin
;
627 $.event
.special
.focusin
= specialFocusin
;
629 this.$element
.on( event
, callback
);
632 $.event
.special
.focusin
= orig
;
635 this.$element
.on( event
, callback
);
640 * @param {string} event
641 * @param {Function} callback
643 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
645 if ( event
=== 'focusin' ) {
646 orig
= $.event
.special
.focusin
;
647 $.event
.special
.focusin
= specialFocusin
;
648 this.$element
.off( event
, callback
);
649 $.event
.special
.focusin
= orig
;
651 this.$element
.off( event
, callback
);
656 * Embedded iframe with the same styles as its parent.
659 * @extends OO.ui.Element
660 * @mixins OO.EventEmitter
663 * @param {Object} [config] Configuration options
665 OO
.ui
.Frame
= function OoUiFrame( config
) {
666 // Parent constructor
667 OO
.ui
.Element
.call( this, config
);
669 // Mixin constructors
670 OO
.EventEmitter
.call( this );
673 this.initialized
= false;
674 this.config
= config
;
678 .addClass( 'oo-ui-frame' )
679 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
685 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
687 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
689 /* Static Properties */
691 OO
.ui
.Frame
.static.tagName
= 'iframe';
702 * Transplant the CSS styles from as parent document to a frame's document.
704 * This loops over the style sheets in the parent document, and copies their nodes to the
705 * frame's document. It then polls the document to see when all styles have loaded, and once they
706 * have, invokes the callback.
708 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
709 * and invoke the callback anyway. This protects against cases like a display: none; iframe in
710 * Firefox, where the styles won't load until the iframe becomes visible.
712 * For details of how we arrived at the strategy used in this function, see #load.
717 * @param {HTMLDocument} parentDoc Document to transplant styles from
718 * @param {HTMLDocument} frameDoc Document to transplant styles to
719 * @param {Function} [callback] Callback to execute once styles have loaded
720 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
722 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, callback
, timeout
) {
723 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
724 $pollNodes
= $( [] ),
725 // Fake font-family value
726 fontFamily
= 'oo-ui-frame-transplantStyles-loaded';
728 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
729 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
730 if ( callback
&& styleNode
.nodeName
.toLowerCase() === 'link' ) {
731 // External stylesheet
732 // Create a node with a unique ID that we're going to monitor to see when the CSS
734 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
735 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
736 .attr( 'id', pollNodeId
)
737 .appendTo( frameDoc
.body
)
740 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
741 // The font-family rule will only take effect once the @import finishes
742 newNode
= frameDoc
.createElement( 'style' );
743 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
744 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
746 // Not an external stylesheet, or no polling required; just copy the node over
747 newNode
= frameDoc
.importNode( styleNode
, true );
749 frameDoc
.head
.appendChild( newNode
);
753 // Poll every 100ms until all external stylesheets have loaded
754 $pendingPollNodes
= $pollNodes
;
755 timeoutID
= setTimeout( function pollExternalStylesheets() {
757 $pendingPollNodes
.length
> 0 &&
758 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
760 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
763 if ( $pendingPollNodes
.length
=== 0 ) {
765 if ( timeoutID
!== null ) {
771 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
774 // ...but give up after a while
775 if ( timeout
!== 0 ) {
776 setTimeout( function () {
778 clearTimeout( timeoutID
);
783 }, timeout
|| 5000 );
791 * Load the frame contents.
793 * Once the iframe's stylesheets are loaded, the `initialize` event will be emitted.
795 * Sounds simple right? Read on...
797 * When you create a dynamic iframe using open/write/close, the window.load event for the
798 * iframe is triggered when you call close, and there's no further load event to indicate that
799 * everything is actually loaded.
801 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
802 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
803 * are added to document.styleSheets immediately, and the only way you can determine whether they've
804 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
805 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
807 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
808 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
809 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
810 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
811 * .cssRules is allowed.
813 * However, now that we control the styles we're injecting, we might as well do away with
814 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
815 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
816 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
817 * rule is not applied until after the `@import` finishes.
819 * All this stylesheet injection and polling magic is in #transplantStyles.
823 OO
.ui
.Frame
.prototype.load = function () {
824 var win
= this.$element
.prop( 'contentWindow' ),
828 // Figure out directionality:
829 this.dir
= this.$element
.closest( '[dir]' ).prop( 'dir' ) || 'ltr';
831 // Initialize contents
836 '<body class="oo-ui-frame-body oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
837 '<div class="oo-ui-frame-content"></div>' +
844 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
845 this.$content
= this.$( '.oo-ui-frame-content' );
846 this.$document
= this.$( doc
);
848 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0],
850 frame
.initialized
= true;
851 frame
.emit( 'initialize' );
857 * Run a callback as soon as the frame has been initialized.
859 * @param {Function} callback
861 OO
.ui
.Frame
.prototype.run = function ( callback
) {
862 if ( this.initialized
) {
865 this.once( 'initialize', callback
);
870 * Sets the size of the frame.
873 * @param {number} width Frame width in pixels
874 * @param {number} height Frame height in pixels
877 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
878 this.$element
.css( { 'width': width
, 'height': height
} );
882 * Container for elements in a child frame.
884 * There are two ways to specify a title: set the static `title` property or provide a `title`
885 * property in the configuration options. The latter will override the former.
889 * @extends OO.ui.Element
890 * @mixins OO.EventEmitter
893 * @param {OO.ui.WindowSet} windowSet Window set this dialog is part of
894 * @param {Object} [config] Configuration options
895 * @cfg {string|Function} [title] Title string or function that returns a string
896 * @cfg {string} [icon] Symbolic name of icon
899 OO
.ui
.Window
= function OoUiWindow( windowSet
, config
) {
900 // Parent constructor
901 OO
.ui
.Element
.call( this, config
);
903 // Mixin constructors
904 OO
.EventEmitter
.call( this );
907 this.windowSet
= windowSet
;
908 this.visible
= false;
909 this.opening
= false;
910 this.closing
= false;
911 this.title
= OO
.ui
.resolveMsg( config
.title
|| this.constructor.static.title
);
912 this.icon
= config
.icon
|| this.constructor.static.icon
;
913 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
914 this.$frame
= this.$( '<div>' );
915 this.$ = function () {
916 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
921 .addClass( 'oo-ui-window' )
922 // Hide the window using visibility: hidden; while the iframe is still loading
923 // Can't use display: none; because that prevents the iframe from loading in Firefox
924 .css( 'visibility', 'hidden' )
925 .append( this.$frame
);
927 .addClass( 'oo-ui-window-frame' )
928 .append( this.frame
.$element
);
931 this.frame
.connect( this, { 'initialize': 'initialize' } );
936 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
938 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
943 * Initialize contents.
945 * Fired asynchronously after construction when iframe is ready.
953 * Fired after window has been opened.
956 * @param {Object} data Window opening data
962 * Fired after window has been closed.
965 * @param {Object} data Window closing data
968 /* Static Properties */
971 * Symbolic name of icon.
977 OO
.ui
.Window
.static.icon
= 'window';
984 * @property {string|Function} Title string or function that returns a string
986 OO
.ui
.Window
.static.title
= null;
991 * Check if window is visible.
994 * @returns {boolean} Window is visible
996 OO
.ui
.Window
.prototype.isVisible = function () {
1001 * Check if window is opening.
1004 * @returns {boolean} Window is opening
1006 OO
.ui
.Window
.prototype.isOpening = function () {
1007 return this.opening
;
1011 * Check if window is closing.
1014 * @returns {boolean} Window is closing
1016 OO
.ui
.Window
.prototype.isClosing = function () {
1017 return this.closing
;
1021 * Get the window frame.
1024 * @returns {OO.ui.Frame} Frame of window
1026 OO
.ui
.Window
.prototype.getFrame = function () {
1031 * Get the window set.
1034 * @returns {OO.ui.WindowSet} Window set
1036 OO
.ui
.Window
.prototype.getWindowSet = function () {
1037 return this.windowSet
;
1041 * Get the window title.
1043 * @returns {string} Title text
1045 OO
.ui
.Window
.prototype.getTitle = function () {
1050 * Get the window icon.
1052 * @returns {string} Symbolic name of icon
1054 OO
.ui
.Window
.prototype.getIcon = function () {
1059 * Set the size of window frame.
1061 * @param {number} [width=auto] Custom width
1062 * @param {number} [height=auto] Custom height
1065 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1066 if ( !this.frame
.$content
) {
1070 this.frame
.$element
.css( {
1071 'width': width
=== undefined ? 'auto' : width
,
1072 'height': height
=== undefined ? 'auto' : height
1079 * Set the title of the window.
1081 * @param {string|Function} title Title text or a function that returns text
1084 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1085 this.title
= OO
.ui
.resolveMsg( title
);
1086 if ( this.$title
) {
1087 this.$title
.text( title
);
1093 * Set the icon of the window.
1095 * @param {string} icon Symbolic name of icon
1098 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1100 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1104 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1111 * Set the position of window to fit with contents..
1113 * @param {string} left Left offset
1114 * @param {string} top Top offset
1117 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1118 this.$element
.css( { 'left': left
, 'top': top
} );
1123 * Set the height of window to fit with contents.
1125 * @param {number} [min=0] Min height
1126 * @param {number} [max] Max height (defaults to content's outer height)
1129 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1130 var height
= this.frame
.$content
.outerHeight();
1132 this.frame
.$element
.css(
1133 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1140 * Set the width of window to fit with contents.
1142 * @param {number} [min=0] Min height
1143 * @param {number} [max] Max height (defaults to content's outer width)
1146 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1147 var width
= this.frame
.$content
.outerWidth();
1149 this.frame
.$element
.css(
1150 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1157 * Initialize window contents.
1159 * The first time the window is opened, #initialize is called when it's safe to begin populating
1160 * its contents. See #setup for a way to make changes each time the window opens.
1162 * Once this method is called, this.$$ can be used to create elements within the frame.
1168 OO
.ui
.Window
.prototype.initialize = function () {
1170 this.$ = this.frame
.$;
1171 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1172 .text( this.title
);
1173 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1174 .addClass( 'oo-ui-icon-' + this.icon
);
1175 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1176 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1177 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1178 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1181 this.frame
.$content
.append(
1182 this.$head
.append( this.$icon
, this.$title
),
1188 // Undo the visibility: hidden; hack from the constructor and apply display: none;
1189 // We can do this safely now that the iframe has initialized
1190 this.$element
.hide().css( 'visibility', '' );
1192 this.emit( 'initialize' );
1198 * Setup window for use.
1200 * Each time the window is opened, once it's ready to be interacted with, this will set it up for
1201 * use in a particular context, based on the `data` argument.
1203 * When you override this method, you must call the parent method at the very beginning.
1207 * @param {Object} [data] Window opening data
1209 OO
.ui
.Window
.prototype.setup = function () {
1210 // Override to do something
1214 * Tear down window after use.
1216 * Each time the window is closed, and it's done being interacted with, this will tear it down and
1217 * do something with the user's interactions within the window, based on the `data` argument.
1219 * When you override this method, you must call the parent method at the very end.
1223 * @param {Object} [data] Window closing data
1225 OO
.ui
.Window
.prototype.teardown = function () {
1226 // Override to do something
1232 * Do not override this method. See #setup for a way to make changes each time the window opens.
1235 * @param {Object} [data] Window opening data
1239 OO
.ui
.Window
.prototype.open = function ( data
) {
1240 if ( !this.opening
&& !this.closing
&& !this.visible
) {
1241 this.opening
= true;
1242 this.frame
.run( OO
.ui
.bind( function () {
1243 this.$element
.show();
1244 this.visible
= true;
1245 this.frame
.$element
.focus();
1246 this.emit( 'opening', data
);
1248 this.emit( 'open', data
);
1249 this.opening
= false;
1259 * See #teardown for a way to do something each time the window closes.
1262 * @param {Object} [data] Window closing data
1266 OO
.ui
.Window
.prototype.close = function ( data
) {
1267 if ( !this.opening
&& !this.closing
&& this.visible
) {
1268 this.frame
.$content
.find( ':focus' ).blur();
1269 this.closing
= true;
1270 this.$element
.hide();
1271 this.visible
= false;
1272 this.emit( 'closing', data
);
1273 this.teardown( data
);
1274 this.emit( 'close', data
);
1275 this.closing
= false;
1281 * Set of mutually exclusive windows.
1284 * @extends OO.ui.Element
1285 * @mixins OO.EventEmitter
1288 * @param {OO.Factory} factory Window factory
1289 * @param {Object} [config] Configuration options
1291 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1292 // Parent constructor
1293 OO
.ui
.Element
.call( this, config
);
1295 // Mixin constructors
1296 OO
.EventEmitter
.call( this );
1299 this.factory
= factory
;
1301 this.currentWindow
= null;
1304 this.$element
.addClass( 'oo-ui-windowSet' );
1309 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1311 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1317 * @param {OO.ui.Window} win Window that's being opened
1318 * @param {Object} config Window opening information
1323 * @param {OO.ui.Window} win Window that's been opened
1324 * @param {Object} config Window opening information
1329 * @param {OO.ui.Window} win Window that's being closed
1330 * @param {Object} config Window closing information
1335 * @param {OO.ui.Window} win Window that's been closed
1336 * @param {Object} config Window closing information
1342 * Handle a window that's being opened.
1345 * @param {OO.ui.Window} win Window that's being opened
1346 * @param {Object} [config] Window opening information
1349 OO
.ui
.WindowSet
.prototype.onWindowOpening = function ( win
, config
) {
1350 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1351 this.currentWindow
.close();
1353 this.currentWindow
= win
;
1354 this.emit( 'opening', win
, config
);
1358 * Handle a window that's been opened.
1361 * @param {OO.ui.Window} win Window that's been opened
1362 * @param {Object} [config] Window opening information
1365 OO
.ui
.WindowSet
.prototype.onWindowOpen = function ( win
, config
) {
1366 this.emit( 'open', win
, config
);
1370 * Handle a window that's being closed.
1373 * @param {OO.ui.Window} win Window that's being closed
1374 * @param {Object} [config] Window closing information
1377 OO
.ui
.WindowSet
.prototype.onWindowClosing = function ( win
, config
) {
1378 this.currentWindow
= null;
1379 this.emit( 'closing', win
, config
);
1383 * Handle a window that's been closed.
1386 * @param {OO.ui.Window} win Window that's been closed
1387 * @param {Object} [config] Window closing information
1390 OO
.ui
.WindowSet
.prototype.onWindowClose = function ( win
, config
) {
1391 this.emit( 'close', win
, config
);
1395 * Get the current window.
1398 * @returns {OO.ui.Window} Current window
1400 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1401 return this.currentWindow
;
1405 * Return a given window.
1407 * @param {string} name Symbolic name of window
1408 * @return {OO.ui.Window} Window with specified name
1410 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1413 if ( !this.factory
.lookup( name
) ) {
1414 throw new Error( 'Unknown window: ' + name
);
1416 if ( !( name
in this.windows
) ) {
1417 win
= this.windows
[name
] = this.factory
.create( name
, this, { '$': this.$ } );
1418 win
.connect( this, {
1419 'opening': [ 'onWindowOpening', win
],
1420 'open': [ 'onWindowOpen', win
],
1421 'closing': [ 'onWindowClosing', win
],
1422 'close': [ 'onWindowClose', win
]
1424 this.$element
.append( win
.$element
);
1425 win
.getFrame().load();
1427 return this.windows
[name
];
1434 * @extends OO.ui.Window
1437 * @param {OO.ui.WindowSet} windowSet Window set this dialog is part of
1438 * @param {Object} [config] Configuration options
1439 * @cfg {boolean} [footless] Hide foot
1440 * @cfg {boolean} [small] Make the dialog small
1442 OO
.ui
.Dialog
= function OoUiDialog( windowSet
, config
) {
1443 // Configuration initialization
1444 config
= config
|| {};
1446 // Parent constructor
1447 OO
.ui
.Window
.call( this, windowSet
, config
);
1450 this.visible
= false;
1451 this.footless
= !!config
.footless
;
1452 this.small
= !!config
.small
;
1453 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1454 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1457 this.$element
.on( 'mousedown', false );
1460 this.$element
.addClass( 'oo-ui-dialog' );
1465 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1467 /* Static Properties */
1470 * Symbolic name of dialog.
1474 * @property {string}
1477 OO
.ui
.Dialog
.static.name
= '';
1482 * Handle close button click events.
1486 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1487 this.close( { 'action': 'cancel' } );
1491 * Handle window mouse wheel events.
1494 * @param {jQuery.Event} e Mouse wheel event
1496 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1501 * Handle document key down events.
1504 * @param {jQuery.Event} e Key down event
1506 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1507 switch ( e
.which
) {
1508 case OO
.ui
.Keys
.PAGEUP
:
1509 case OO
.ui
.Keys
.PAGEDOWN
:
1510 case OO
.ui
.Keys
.END
:
1511 case OO
.ui
.Keys
.HOME
:
1512 case OO
.ui
.Keys
.LEFT
:
1514 case OO
.ui
.Keys
.RIGHT
:
1515 case OO
.ui
.Keys
.DOWN
:
1516 // Prevent any key events that might cause scrolling
1522 * Handle frame document key down events.
1525 * @param {jQuery.Event} e Key down event
1527 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1528 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1529 this.close( { 'action': 'cancel' } );
1537 OO
.ui
.Dialog
.prototype.initialize = function () {
1539 OO
.ui
.Window
.prototype.initialize
.call( this );
1542 this.closeButton
= new OO
.ui
.ButtonWidget( {
1546 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1550 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1551 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1554 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1555 if ( this.footless
) {
1556 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1559 this.$frame
.addClass( 'oo-ui-window-frame-small' );
1561 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1562 this.$head
.append( this.closeButton
.$element
);
1568 OO
.ui
.Dialog
.prototype.setup = function ( data
) {
1570 OO
.ui
.Window
.prototype.setup
.call( this, data
);
1572 // Prevent scrolling in top-level window
1573 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1574 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1580 OO
.ui
.Dialog
.prototype.teardown = function ( data
) {
1582 OO
.ui
.Window
.prototype.teardown
.call( this, data
);
1584 // Allow scrolling in top-level window
1585 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1586 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1592 OO
.ui
.Dialog
.prototype.close = function ( data
) {
1593 if ( !this.opening
&& !this.closing
&& this.visible
) {
1594 // Trigger transition
1595 this.$element
.addClass( 'oo-ui-dialog-closing' );
1596 // Allow transition to complete before actually closing
1597 setTimeout( OO
.ui
.bind( function () {
1598 this.$element
.removeClass( 'oo-ui-dialog-closing' );
1600 OO
.ui
.Window
.prototype.close
.call( this, data
);
1605 * Container for elements.
1609 * @extends OO.ui.Element
1610 * @mixin OO.EventEmitter
1613 * @param {Object} [config] Configuration options
1615 OO
.ui
.Layout
= function OoUiLayout( config
) {
1616 // Initialize config
1617 config
= config
|| {};
1619 // Parent constructor
1620 OO
.ui
.Element
.call( this, config
);
1622 // Mixin constructors
1623 OO
.EventEmitter
.call( this );
1626 this.$element
.addClass( 'oo-ui-layout' );
1631 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1633 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1635 * User interface control.
1639 * @extends OO.ui.Element
1640 * @mixin OO.EventEmitter
1643 * @param {Object} [config] Configuration options
1644 * @cfg {boolean} [disabled=false] Disable
1646 OO
.ui
.Widget
= function OoUiWidget( config
) {
1647 // Initialize config
1648 config
= $.extend( { 'disabled': false }, config
);
1650 // Parent constructor
1651 OO
.ui
.Element
.call( this, config
);
1653 // Mixin constructors
1654 OO
.EventEmitter
.call( this );
1657 this.disabled
= config
.disabled
;
1660 this.$element
.addClass( 'oo-ui-widget' );
1661 this.setDisabled( this.disabled
);
1666 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1668 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1673 * Check if the widget is disabled.
1676 * @param {boolean} Button is disabled
1678 OO
.ui
.Widget
.prototype.isDisabled = function () {
1679 return this.disabled
;
1683 * Set the disabled state of the widget.
1685 * This should probably change the widgets's appearance and prevent it from being used.
1688 * @param {boolean} disabled Disable button
1691 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1692 this.disabled
= !!disabled
;
1693 if ( this.disabled
) {
1694 this.$element
.addClass( 'oo-ui-widget-disabled' );
1696 this.$element
.removeClass( 'oo-ui-widget-disabled' );
1701 * Element with a button.
1707 * @param {jQuery} $button Button node, assigned to #$button
1708 * @param {Object} [config] Configuration options
1709 * @cfg {boolean} [frameless] Render button without a frame
1710 * @cfg {number} [tabIndex] Button's tab index
1712 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
1713 // Configuration initialization
1714 config
= config
|| {};
1717 this.$button
= $button
;
1718 this.tabIndex
= null;
1719 this.active
= false;
1720 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
1723 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
1726 this.$element
.addClass( 'oo-ui-buttonedElement' );
1728 .addClass( 'oo-ui-buttonedElement-button' )
1729 .attr( 'role', 'button' )
1730 .prop( 'tabIndex', config
.tabIndex
|| 0 );
1731 if ( config
.frameless
) {
1732 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
1734 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
1741 * Handles mouse down events.
1744 * @param {jQuery.Event} e Mouse down event
1746 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function () {
1747 this.tabIndex
= this.$button
.attr( 'tabIndex' );
1748 // Remove the tab-index while the button is down to prevent the button from stealing focus
1749 this.$button
.removeAttr( 'tabIndex' );
1750 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
1754 * Handles mouse up events.
1757 * @param {jQuery.Event} e Mouse up event
1759 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function () {
1760 // Restore the tab-index after the button is up to restore the button's accesssibility
1761 this.$button
.attr( 'tabIndex', this.tabIndex
);
1762 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
1769 * @param {boolean} [value] Make button active
1772 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
1773 this.$element
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
1777 * Element that can be automatically clipped to visible boundaies.
1783 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
1785 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
) {
1787 this.$clippable
= $clippable
;
1788 this.clipping
= false;
1789 this.clipped
= false;
1790 this.$clippableContainer
= null;
1791 this.$clippableScroller
= null;
1792 this.$clippableWindow
= null;
1793 this.idealWidth
= null;
1794 this.idealHeight
= null;
1795 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
1796 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
1799 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
1808 * @param {boolean} value Enable clipping
1811 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
1814 if ( this.clipping
!== value
) {
1815 this.clipping
= value
;
1816 if ( this.clipping
) {
1817 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
1818 // If the clippable container is the body, we have to listen to scroll events and check
1819 // jQuery.scrollTop on the window because of browser inconsistencies
1820 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
1821 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
1822 this.$clippableContainer
;
1823 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
1824 this.$clippableWindow
= this.$( this.getElementWindow() )
1825 .on( 'resize', this.onClippableWindowResizeHandler
);
1826 // Initial clip after visible
1827 setTimeout( OO
.ui
.bind( this.clip
, this ) );
1829 this.$clippableContainer
= null;
1830 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
1831 this.$clippableScroller
= null;
1832 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
1833 this.$clippableWindow
= null;
1841 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
1844 * @return {boolean} Element will be clipped to the visible area
1846 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
1847 return this.clipping
;
1851 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
1854 * @return {boolean} Part of the element is being clipped
1856 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
1857 return this.clipped
;
1861 * Set the ideal size.
1864 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
1865 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
1867 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
1868 this.idealWidth
= width
;
1869 this.idealHeight
= height
;
1873 * Clip element to visible boundaries and allow scrolling when needed.
1875 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
1876 * overlapped by, the visible area of the nearest scrollable container.
1881 OO
.ui
.ClippableElement
.prototype.clip = function () {
1882 if ( !this.clipping
) {
1883 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
1888 cOffset
= this.$clippable
.offset(),
1889 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
1890 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
1891 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
1892 scrollTop
= this.$clippableScroller
.scrollTop(),
1893 scrollLeft
= this.$clippableScroller
.scrollLeft(),
1894 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
1895 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
1896 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
1897 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
1898 clipWidth
= desiredWidth
< naturalWidth
,
1899 clipHeight
= desiredHeight
< naturalHeight
;
1902 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
1904 this.$clippable
.css( { 'overflow-x': '', 'width': this.idealWidth
|| '' } );
1907 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
1909 this.$clippable
.css( { 'overflow-y': '', 'height': this.idealHeight
|| '' } );
1912 this.clipped
= clipWidth
|| clipHeight
;
1917 * Element with named flags, used for styling, that can be added, removed and listed and checked.
1923 * @param {Object} [config] Configuration options
1924 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
1926 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
1927 // Config initialization
1928 config
= config
|| {};
1934 this.setFlags( config
.flags
);
1940 * Check if a flag is set.
1943 * @param {string} flag Flag name to check
1944 * @returns {boolean} Has flag
1946 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
1947 return flag
in this.flags
;
1951 * Get the names of all flags.
1954 * @returns {string[]} flags Flag names
1956 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
1957 return Object
.keys( this.flags
);
1961 * Add one or more flags.
1964 * @param {string[]|Object.<string, boolean>} flags List of flags to add, or list of set/remove
1965 * values, keyed by flag name
1968 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
1970 classPrefix
= 'oo-ui-flaggableElement-';
1972 if ( Array
.isArray( flags
) ) {
1973 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
1976 this.flags
[flag
] = true;
1977 this.$element
.addClass( classPrefix
+ flag
);
1979 } else if ( OO
.isPlainObject( flags
) ) {
1980 for ( flag
in flags
) {
1981 if ( flags
[flags
] ) {
1983 this.flags
[flag
] = true;
1984 this.$element
.addClass( classPrefix
+ flag
);
1987 delete this.flags
[flag
];
1988 this.$element
.removeClass( classPrefix
+ flag
);
1995 * Element containing a sequence of child elements.
2001 * @param {jQuery} $group Container node, assigned to #$group
2002 * @param {Object} [config] Configuration options
2003 * @cfg {Object.<string,string>} [aggregations] Events to aggregate, keyed by item event name
2005 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2007 config
= config
|| {};
2010 this.$group
= $group
;
2012 this.$items
= this.$( [] );
2013 this.aggregate
= !$.isEmptyObject( config
.aggregations
);
2014 this.aggregations
= config
.aggregations
|| {};
2023 * @returns {OO.ui.Element[]} Items
2025 OO
.ui
.GroupElement
.prototype.getItems = function () {
2026 return this.items
.slice( 0 );
2033 * @param {OO.ui.Element[]} items Item
2034 * @param {number} [index] Index to insert items at
2037 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2038 var i
, len
, item
, event
, events
, currentIndex
,
2039 $items
= this.$( [] );
2041 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2044 // Check if item exists then remove it first, effectively "moving" it
2045 currentIndex
= this.items
.indexOf( item
);
2046 if ( currentIndex
>= 0 ) {
2047 this.removeItems( [ item
] );
2048 // Adjust index to compensate for removal
2049 if ( currentIndex
< index
) {
2054 if ( this.aggregate
) {
2056 for ( event
in this.aggregations
) {
2057 events
[event
] = [ 'emit', this.aggregations
[event
], item
];
2059 item
.connect( this, events
);
2061 $items
= $items
.add( item
.$element
);
2064 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2065 this.$group
.append( $items
);
2066 this.items
.push
.apply( this.items
, items
);
2067 } else if ( index
=== 0 ) {
2068 this.$group
.prepend( $items
);
2069 this.items
.unshift
.apply( this.items
, items
);
2071 this.$items
.eq( index
).before( $items
);
2072 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2075 this.$items
= this.$items
.add( $items
);
2083 * Items will be detached, not removed, so they can be used later.
2086 * @param {OO.ui.Element[]} items Items to remove
2089 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2090 var i
, len
, item
, index
;
2092 // Remove specific items
2093 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2095 index
= this.items
.indexOf( item
);
2096 if ( index
!== -1 ) {
2097 if ( this.aggregate
) {
2098 item
.disconnect( this );
2100 this.items
.splice( index
, 1 );
2101 item
.$element
.detach();
2102 this.$items
= this.$items
.not( item
.$element
);
2112 * Items will be detached, not removed, so they can be used later.
2117 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2121 if ( this.aggregate
) {
2122 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2123 item
.disconnect( this );
2127 this.$items
.detach();
2128 this.$items
= this.$( [] );
2131 * Element containing an icon.
2137 * @param {jQuery} $icon Icon node, assigned to #$icon
2138 * @param {Object} [config] Configuration options
2139 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2140 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2143 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2144 // Config intialization
2145 config
= config
|| {};
2152 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2153 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2156 /* Static Properties */
2158 OO
.ui
.IconedElement
.static = {};
2163 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2165 * For i18n purposes, this property can be an object containing a `default` icon name property and
2166 * additional icon names keyed by language code.
2168 * Example of i18n icon definition:
2169 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2173 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2174 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2177 OO
.ui
.IconedElement
.static.icon
= null;
2185 * @param {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
2190 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2191 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2194 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2196 if ( typeof icon
=== 'string' ) {
2198 if ( icon
.length
) {
2199 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2203 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2212 * @returns {string} Icon
2214 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2218 * Element containing an indicator.
2224 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2225 * @param {Object} [config] Configuration options
2226 * @cfg {string} [indicator] Symbolic indicator name
2227 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2229 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2230 // Config intialization
2231 config
= config
|| {};
2234 this.$indicator
= $indicator
;
2235 this.indicator
= null;
2236 this.indicatorLabel
= null;
2239 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2240 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2241 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2244 /* Static Properties */
2246 OO
.ui
.IndicatedElement
.static = {};
2253 * @property {string|null} Symbolic indicator name or null for no indicator
2255 OO
.ui
.IndicatedElement
.static.indicator
= null;
2262 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2265 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2273 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2276 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2277 if ( this.indicator
) {
2278 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2279 this.indicator
= null;
2281 if ( typeof indicator
=== 'string' ) {
2282 indicator
= indicator
.trim();
2283 if ( indicator
.length
) {
2284 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2285 this.indicator
= indicator
;
2288 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2294 * Set indicator label.
2297 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2298 * for no indicator title
2301 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2302 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2304 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2305 this.$indicator
.attr( 'title', indicatorTitle
);
2307 this.$indicator
.removeAttr( 'title' );
2317 * @returns {string} title Symbolic name of indicator
2319 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2320 return this.indicator
;
2324 * Get indicator title.
2327 * @returns {string} Indicator title text
2329 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2330 return this.indicatorTitle
;
2333 * Element containing a label.
2339 * @param {jQuery} $label Label node, assigned to #$label
2340 * @param {Object} [config] Configuration options
2341 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2343 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2344 // Config intialization
2345 config
= config
|| {};
2348 this.$label
= $label
;
2352 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2353 this.setLabel( config
.label
|| this.constructor.static.label
);
2356 /* Static Properties */
2358 OO
.ui
.LabeledElement
.static = {};
2365 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2368 OO
.ui
.LabeledElement
.static.label
= null;
2376 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2377 * text; or null for no label
2380 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2383 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2384 if ( typeof label
=== 'string' && label
.trim() ) {
2385 this.$label
.text( label
);
2386 } else if ( label
instanceof jQuery
) {
2387 this.$label
.empty().append( label
);
2389 this.$label
.empty();
2392 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2393 this.$label
.css( 'display', empty
? 'none' : '' );
2402 * @returns {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2403 * text; or null for no label
2405 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2415 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
2416 if ( this.$label
.autoEllipsis
) {
2417 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
2422 * Popuppable element.
2428 * @param {Object} [config] Configuration options
2429 * @cfg {number} [popupWidth=320] Width of popup
2430 * @cfg {number} [popupHeight] Height of popup
2431 * @cfg {Object} [popup] Configuration to pass to popup
2433 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
2434 // Configuration initialization
2435 config
= $.extend( { 'popupWidth': 320 }, config
);
2438 this.popup
= new OO
.ui
.PopupWidget( $.extend(
2439 { 'align': 'center', 'autoClose': true },
2441 { '$': this.$, '$autoCloseIgnore': this.$element
}
2443 this.popupWidth
= config
.popupWidth
;
2444 this.popupHeight
= config
.popupHeight
;
2453 * @returns {OO.ui.PopupWidget} Popup widget
2455 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
2464 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
2465 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
2473 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
2477 * Element with a title.
2483 * @param {jQuery} $label Titled node, assigned to #$titled
2484 * @param {Object} [config] Configuration options
2485 * @cfg {string|Function} [title] Title text or a function that returns text
2487 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
2488 // Config intialization
2489 config
= config
|| {};
2492 this.$titled
= $titled
;
2496 this.setTitle( config
.title
|| this.constructor.static.title
);
2499 /* Static Properties */
2501 OO
.ui
.TitledElement
.static = {};
2508 * @property {string|Function} Title text or a function that returns text
2510 OO
.ui
.TitledElement
.static.title
= null;
2518 * @param {string|Function|null} title Title text, a function that returns text or null for no title
2521 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
2522 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
2524 if ( typeof title
=== 'string' && title
.length
) {
2525 this.$titled
.attr( 'title', title
);
2527 this.$titled
.removeAttr( 'title' );
2537 * @returns {string} Title string
2539 OO
.ui
.TitledElement
.prototype.getTitle = function () {
2543 * Generic toolbar tool.
2547 * @extends OO.ui.Widget
2548 * @mixins OO.ui.IconedElement
2551 * @param {OO.ui.ToolGroup} toolGroup
2552 * @param {Object} [config] Configuration options
2553 * @cfg {string|Function} [title] Title text or a function that returns text
2555 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
2556 // Config intialization
2557 config
= config
|| {};
2559 // Parent constructor
2560 OO
.ui
.Widget
.call( this, config
);
2562 // Mixin constructors
2563 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
2566 this.toolGroup
= toolGroup
;
2567 this.toolbar
= this.toolGroup
.getToolbar();
2568 this.active
= false;
2569 this.$title
= this.$( '<span>' );
2570 this.$link
= this.$( '<a>' );
2574 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
2577 this.$title
.addClass( 'oo-ui-tool-title' );
2579 .addClass( 'oo-ui-tool-link' )
2580 .append( this.$icon
, this.$title
);
2582 .data( 'oo-ui-tool', this )
2584 'oo-ui-tool ' + 'oo-ui-tool-name-' +
2585 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
2587 .append( this.$link
);
2588 this.setTitle( config
.title
|| this.constructor.static.title
);
2593 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
2595 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
2603 /* Static Properties */
2605 OO
.ui
.Tool
.static.tagName
= 'span';
2608 * Symbolic name of tool.
2612 * @property {string}
2615 OO
.ui
.Tool
.static.name
= '';
2622 * @property {string}
2625 OO
.ui
.Tool
.static.group
= '';
2630 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
2631 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
2632 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
2633 * appended to the title if the tool is part of a bar tool group.
2637 * @property {string|Function} Title text or a function that returns text
2640 OO
.ui
.Tool
.static.title
= '';
2643 * Tool can be automatically added to tool groups.
2646 * @property {boolean}
2649 OO
.ui
.Tool
.static.autoAdd
= true;
2652 * Check if this tool is compatible with given data.
2657 * @param {Mixed} data Data to check
2658 * @returns {boolean} Tool can be used with data
2660 OO
.ui
.Tool
.static.isCompatibleWith = function () {
2667 * Handle the toolbar state being updated.
2669 * This is an abstract method that must be overridden in a concrete subclass.
2674 OO
.ui
.Tool
.prototype.onUpdateState = function () {
2676 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
2681 * Handle the tool being selected.
2683 * This is an abstract method that must be overridden in a concrete subclass.
2688 OO
.ui
.Tool
.prototype.onSelect = function () {
2690 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
2695 * Check if the button is active.
2698 * @param {boolean} Button is active
2700 OO
.ui
.Tool
.prototype.isActive = function () {
2705 * Make the button appear active or inactive.
2708 * @param {boolean} state Make button appear active
2710 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
2711 this.active
= !!state
;
2712 if ( this.active
) {
2713 this.$element
.addClass( 'oo-ui-tool-active' );
2715 this.$element
.removeClass( 'oo-ui-tool-active' );
2720 * Get the tool title.
2723 * @param {string|Function} title Title text or a function that returns text
2726 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
2727 this.title
= OO
.ui
.resolveMsg( title
);
2733 * Get the tool title.
2736 * @returns {string} Title text
2738 OO
.ui
.Tool
.prototype.getTitle = function () {
2743 * Get the tool's symbolic name.
2746 * @returns {string} Symbolic name of tool
2748 OO
.ui
.Tool
.prototype.getName = function () {
2749 return this.constructor.static.name
;
2757 OO
.ui
.Tool
.prototype.updateTitle = function () {
2758 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
2759 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
2760 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
2767 .addClass( 'oo-ui-tool-accel' )
2771 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
2772 tooltipParts
.push( this.title
);
2774 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
2775 tooltipParts
.push( accel
);
2777 if ( tooltipParts
.length
) {
2778 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
2780 this.$link
.removeAttr( 'title' );
2789 OO
.ui
.Tool
.prototype.destroy = function () {
2790 this.toolbar
.disconnect( this );
2791 this.$element
.remove();
2794 * Collection of tool groups.
2797 * @extends OO.ui.Element
2798 * @mixins OO.EventEmitter
2799 * @mixins OO.ui.GroupElement
2802 * @param {OO.Factory} toolFactory Factory for creating tools
2803 * @param {Object} [options] Configuration options
2804 * @cfg {boolean} [actions] Add an actions section opposite to the tools
2805 * @cfg {boolean} [shadow] Add a shadow below the toolbar
2807 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, options
) {
2808 // Configuration initialization
2809 options
= options
|| {};
2811 // Parent constructor
2812 OO
.ui
.Element
.call( this, options
);
2814 // Mixin constructors
2815 OO
.EventEmitter
.call( this );
2816 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ) );
2819 this.toolFactory
= toolFactory
;
2822 this.$bar
= this.$( '<div>' );
2823 this.$actions
= this.$( '<div>' );
2824 this.initialized
= false;
2828 .add( this.$bar
).add( this.$group
).add( this.$actions
)
2829 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2832 this.$group
.addClass( 'oo-ui-toolbar-tools' );
2833 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
2834 if ( options
.actions
) {
2835 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
2836 this.$bar
.append( this.$actions
);
2838 this.$bar
.append( '<div style="clear:both"></div>' );
2839 if ( options
.shadow
) {
2840 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
2842 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
2847 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
2849 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
2850 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
2855 * Get the tool factory.
2858 * @returns {OO.Factory} Tool factory
2860 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
2861 return this.toolFactory
;
2865 * Handles mouse down events.
2868 * @param {jQuery.Event} e Mouse down event
2870 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
2871 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
2872 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
2873 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
2879 * Sets up handles and preloads required information for the toolbar to work.
2880 * This must be called immediately after it is attached to a visible document.
2882 OO
.ui
.Toolbar
.prototype.initialize = function () {
2883 this.initialized
= true;
2889 * Tools can be specified in the following ways:
2890 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
2891 * - All tools in a group: `{ 'group': 'group-name' }`
2892 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
2895 * @param {Object.<string,Array>} groups List of tool group configurations
2896 * @param {Array|string} [groups.include] Tools to include
2897 * @param {Array|string} [groups.exclude] Tools to exclude
2898 * @param {Array|string} [groups.promote] Tools to promote to the beginning
2899 * @param {Array|string} [groups.demote] Tools to demote to the end
2901 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
2902 var i
, len
, type
, group
,
2904 // TODO: Use a registry instead
2905 defaultType
= 'bar',
2907 'bar': OO
.ui
.BarToolGroup
,
2908 'list': OO
.ui
.ListToolGroup
,
2909 'menu': OO
.ui
.MenuToolGroup
2912 // Cleanup previous groups
2915 // Build out new groups
2916 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
2918 if ( group
.include
=== '*' ) {
2919 // Apply defaults to catch-all groups
2920 if ( group
.type
=== undefined ) {
2921 group
.type
= 'list';
2923 if ( group
.label
=== undefined ) {
2924 group
.label
= 'ooui-toolbar-more';
2927 type
= constructors
[group
.type
] ? group
.type
: defaultType
;
2929 new constructors
[type
]( this, $.extend( { '$': this.$ }, group
) )
2932 this.addItems( items
);
2936 * Remove all tools and groups from the toolbar.
2938 OO
.ui
.Toolbar
.prototype.reset = function () {
2943 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2944 this.items
[i
].destroy();
2950 * Destroys toolbar, removing event handlers and DOM elements.
2952 * Call this whenever you are done using a toolbar.
2954 OO
.ui
.Toolbar
.prototype.destroy = function () {
2956 this.$element
.remove();
2960 * Check if tool has not been used yet.
2962 * @param {string} name Symbolic name of tool
2963 * @return {boolean} Tool is available
2965 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
2966 return !this.tools
[name
];
2970 * Prevent tool from being used again.
2972 * @param {OO.ui.Tool} tool Tool to reserve
2974 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
2975 this.tools
[tool
.getName()] = tool
;
2979 * Allow tool to be used again.
2981 * @param {OO.ui.Tool} tool Tool to release
2983 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
2984 delete this.tools
[tool
.getName()];
2988 * Get accelerator label for tool.
2990 * This is a stub that should be overridden to provide access to accelerator information.
2992 * @param {string} name Symbolic name of tool
2993 * @returns {string|undefined} Tool accelerator label if available
2995 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
2999 * Factory for tools.
3002 * @extends OO.Factory
3005 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3006 // Parent constructor
3007 OO
.Factory
.call( this );
3012 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3016 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3017 var i
, len
, included
, promoted
, demoted
,
3021 // Collect included and not excluded tools
3022 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3025 promoted
= this.extract( promote
, used
);
3026 demoted
= this.extract( demote
, used
);
3029 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3030 if ( !used
[included
[i
]] ) {
3031 auto
.push( included
[i
] );
3035 return promoted
.concat( auto
).concat( demoted
);
3039 * Get a flat list of names from a list of names or groups.
3041 * Tools can be specified in the following ways:
3042 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3043 * - All tools in a group: `{ 'group': 'group-name' }`
3044 * - All tools: `'*'`
3047 * @param {Array|string} collection List of tools
3048 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3049 * names will be added as properties
3050 * @return {string[]} List of extracted names
3052 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3053 var i
, len
, item
, name
, tool
,
3056 if ( collection
=== '*' ) {
3057 for ( name
in this.registry
) {
3058 tool
= this.registry
[name
];
3060 // Only add tools by group name when auto-add is enabled
3061 tool
.static.autoAdd
&&
3062 // Exclude already used tools
3063 ( !used
|| !used
[name
] )
3071 } else if ( Array
.isArray( collection
) ) {
3072 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3073 item
= collection
[i
];
3074 // Allow plain strings as shorthand for named tools
3075 if ( typeof item
=== 'string' ) {
3076 item
= { 'name': item
};
3078 if ( OO
.isPlainObject( item
) ) {
3080 for ( name
in this.registry
) {
3081 tool
= this.registry
[name
];
3083 // Include tools with matching group
3084 tool
.static.group
=== item
.group
&&
3085 // Only add tools by group name when auto-add is enabled
3086 tool
.static.autoAdd
&&
3087 // Exclude already used tools
3088 ( !used
|| !used
[name
] )
3097 // Include tools with matching name and exclude already used tools
3098 else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3099 names
.push( item
.name
);
3101 used
[item
.name
] = true;
3110 * Collection of tools.
3114 * @extends OO.ui.Widget
3115 * @mixins OO.ui.GroupElement
3117 * Tools can be specified in the following ways:
3118 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3119 * - All tools in a group: `{ 'group': 'group-name' }`
3120 * - All tools: `'*'`
3123 * @param {OO.ui.Toolbar} toolbar
3124 * @param {Object} [config] Configuration options
3125 * @cfg {Array|string} [include=[]] List of tools to include
3126 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3127 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3128 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3130 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3131 // Configuration initialization
3132 config
= config
|| {};
3134 // Parent constructor
3135 OO
.ui
.Widget
.call( this, config
);
3137 // Mixin constructors
3138 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ) );
3141 this.toolbar
= toolbar
;
3143 this.pressed
= null;
3144 this.include
= config
.include
|| [];
3145 this.exclude
= config
.exclude
|| [];
3146 this.promote
= config
.promote
|| [];
3147 this.demote
= config
.demote
|| [];
3148 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3152 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3153 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3154 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3155 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3157 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3160 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3162 .addClass( 'oo-ui-toolGroup' )
3163 .append( this.$group
);
3169 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3171 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3179 /* Static Properties */
3182 * Show labels in tooltips.
3185 * @property {boolean}
3188 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3191 * Show acceleration labels in tooltips.
3194 * @property {boolean}
3197 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3202 * Handle mouse down events.
3205 * @param {jQuery.Event} e Mouse down event
3207 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3208 if ( !this.disabled
&& e
.which
=== 1 ) {
3209 this.pressed
= this.getTargetTool( e
);
3210 if ( this.pressed
) {
3211 this.pressed
.setActive( true );
3212 this.getElementDocument().addEventListener(
3213 'mouseup', this.onCapturedMouseUpHandler
, true
3221 * Handle captured mouse up events.
3224 * @param {Event} e Mouse up event
3226 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3227 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3228 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3229 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3230 this.onMouseUp( e
);
3234 * Handle mouse up events.
3237 * @param {jQuery.Event} e Mouse up event
3239 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3240 var tool
= this.getTargetTool( e
);
3242 if ( !this.disabled
&& e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3243 this.pressed
.onSelect();
3246 this.pressed
= null;
3251 * Handle mouse over events.
3254 * @param {jQuery.Event} e Mouse over event
3256 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3257 var tool
= this.getTargetTool( e
);
3259 if ( this.pressed
&& this.pressed
=== tool
) {
3260 this.pressed
.setActive( true );
3265 * Handle mouse out events.
3268 * @param {jQuery.Event} e Mouse out event
3270 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3271 var tool
= this.getTargetTool( e
);
3273 if ( this.pressed
&& this.pressed
=== tool
) {
3274 this.pressed
.setActive( false );
3279 * Get the closest tool to a jQuery.Event.
3281 * Only tool links are considered, which prevents other elements in the tool such as popups from
3282 * triggering tool group interactions.
3286 * @param {jQuery.Event} e
3287 * @returns {OO.ui.Tool|null} Tool, `null` if none was found
3289 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3291 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3293 if ( $item
.length
) {
3294 tool
= $item
.parent().data( 'oo-ui-tool' );
3297 return tool
&& !tool
.isDisabled() ? tool
: null;
3301 * Handle tool registry register events.
3303 * If a tool is registered after the group is created, we must repopulate the list to account for:
3304 * - a tool being added that may be included
3305 * - a tool already included being overridden
3307 * @param {string} name Symbolic name of tool
3309 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3314 * Get the toolbar this group is in.
3316 * @return {OO.ui.Toolbar} Toolbar of group
3318 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3319 return this.toolbar
;
3323 * Add and remove tools based on configuration.
3327 OO
.ui
.ToolGroup
.prototype.populate = function () {
3328 var i
, len
, name
, tool
,
3329 toolFactory
= this.toolbar
.getToolFactory(),
3333 list
= this.toolbar
.getToolFactory().getTools(
3334 this.include
, this.exclude
, this.promote
, this.demote
3337 // Build a list of needed tools
3338 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3342 toolFactory
.lookup( name
) &&
3343 // Tool is available or is already in this group
3344 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3346 tool
= this.tools
[name
];
3348 // Auto-initialize tools on first use
3349 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3352 this.toolbar
.reserveTool( tool
);
3357 // Remove tools that are no longer needed
3358 for ( name
in this.tools
) {
3359 if ( !names
[name
] ) {
3360 this.tools
[name
].destroy();
3361 this.toolbar
.releaseTool( this.tools
[name
] );
3362 remove
.push( this.tools
[name
] );
3363 delete this.tools
[name
];
3366 if ( remove
.length
) {
3367 this.removeItems( remove
);
3369 // Update emptiness state
3371 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3373 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3375 // Re-add tools (moving existing ones to new locations)
3376 this.addItems( add
);
3380 * Destroy tool group.
3384 OO
.ui
.ToolGroup
.prototype.destroy = function () {
3388 this.toolbar
.getToolFactory().disconnect( this );
3389 for ( name
in this.tools
) {
3390 this.toolbar
.releaseTool( this.tools
[name
] );
3391 this.tools
[name
].disconnect( this ).destroy();
3392 delete this.tools
[name
];
3394 this.$element
.remove();
3397 * Layout made of a fieldset and optional legend.
3400 * @extends OO.ui.Layout
3401 * @mixins OO.ui.LabeledElement
3404 * @param {Object} [config] Configuration options
3405 * @cfg {string} [icon] Symbolic icon name
3407 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
3408 // Config initialization
3409 config
= config
|| {};
3411 // Parent constructor
3412 OO
.ui
.Layout
.call( this, config
);
3414 // Mixin constructors
3415 OO
.ui
.LabeledElement
.call( this, this.$( '<legend>' ), config
);
3418 if ( config
.icon
) {
3419 this.$element
.addClass( 'oo-ui-fieldsetLayout-decorated' );
3420 this.$label
.addClass( 'oo-ui-icon-' + config
.icon
);
3422 this.$element
.addClass( 'oo-ui-fieldsetLayout' );
3423 if ( config
.icon
|| config
.label
) {
3425 .addClass( 'oo-ui-fieldsetLayout-labeled' )
3426 .append( this.$label
);
3432 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
3434 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
3436 /* Static Properties */
3438 OO
.ui
.FieldsetLayout
.static.tagName
= 'fieldset';
3440 * Layout made of proportionally sized columns and rows.
3443 * @extends OO.ui.Layout
3446 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
3447 * @param {Object} [config] Configuration options
3448 * @cfg {number[]} [widths] Widths of columns as ratios
3449 * @cfg {number[]} [heights] Heights of columns as ratios
3451 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
3454 // Config initialization
3455 config
= config
|| {};
3457 // Parent constructor
3458 OO
.ui
.Layout
.call( this, config
);
3466 this.$element
.addClass( 'oo-ui-gridLayout' );
3467 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
3468 this.panels
.push( panels
[i
] );
3469 this.$element
.append( panels
[i
].$element
);
3471 if ( config
.widths
|| config
.heights
) {
3472 this.layout( config
.widths
|| [1], config
.heights
|| [1] );
3474 // Arrange in columns by default
3476 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
3479 this.layout( widths
, [1] );
3485 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
3497 /* Static Properties */
3499 OO
.ui
.GridLayout
.static.tagName
= 'div';
3504 * Set grid dimensions.
3507 * @param {number[]} widths Widths of columns as ratios
3508 * @param {number[]} heights Heights of rows as ratios
3510 * @throws {Error} If grid is not large enough to fit all panels
3512 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
3516 cols
= widths
.length
,
3517 rows
= heights
.length
;
3519 // Verify grid is big enough to fit panels
3520 if ( cols
* rows
< this.panels
.length
) {
3521 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
3524 // Sum up denominators
3525 for ( x
= 0; x
< cols
; x
++ ) {
3528 for ( y
= 0; y
< rows
; y
++ ) {
3534 for ( x
= 0; x
< cols
; x
++ ) {
3535 this.widths
[x
] = widths
[x
] / xd
;
3537 for ( y
= 0; y
< rows
; y
++ ) {
3538 this.heights
[y
] = heights
[y
] / yd
;
3542 this.emit( 'layout' );
3546 * Update panel positions and sizes.
3551 OO
.ui
.GridLayout
.prototype.update = function () {
3559 cols
= this.widths
.length
,
3560 rows
= this.heights
.length
;
3562 for ( y
= 0; y
< rows
; y
++ ) {
3563 for ( x
= 0; x
< cols
; x
++ ) {
3564 panel
= this.panels
[i
];
3565 width
= this.widths
[x
];
3566 height
= this.heights
[y
];
3568 'width': Math
.round( width
* 100 ) + '%',
3569 'height': Math
.round( height
* 100 ) + '%',
3570 'top': Math
.round( top
* 100 ) + '%'
3573 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
3574 dimensions
.right
= Math
.round( left
* 100 ) + '%';
3576 dimensions
.left
= Math
.round( left
* 100 ) + '%';
3578 panel
.$element
.css( dimensions
);
3586 this.emit( 'update' );
3590 * Get a panel at a given position.
3592 * The x and y position is affected by the current grid layout.
3595 * @param {number} x Horizontal position
3596 * @param {number} y Vertical position
3597 * @returns {OO.ui.PanelLayout} The panel at the given postion
3599 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
3600 return this.panels
[( x
* this.widths
.length
) + y
];
3603 * Layout containing a series of pages.
3606 * @extends OO.ui.Layout
3609 * @param {Object} [config] Configuration options
3610 * @cfg {boolean} [continuous=false] Show all pages, one after another
3611 * @cfg {boolean} [autoFocus=false] Focus on the first focusable element when changing to a page
3612 * @cfg {boolean} [outlined=false] Show an outline
3613 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
3614 * @cfg {Object[]} [adders] List of adders for controls, each with name, icon and title properties
3616 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
3617 // Initialize configuration
3618 config
= config
|| {};
3620 // Parent constructor
3621 OO
.ui
.Layout
.call( this, config
);
3624 this.currentPageName
= null;
3626 this.ignoreFocus
= false;
3627 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
3628 this.autoFocus
= !!config
.autoFocus
;
3629 this.outlined
= !!config
.outlined
;
3630 if ( this.outlined
) {
3631 this.editable
= !!config
.editable
;
3632 this.adders
= config
.adders
|| null;
3633 this.outlineControlsWidget
= null;
3634 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
3635 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
3636 this.gridLayout
= new OO
.ui
.GridLayout(
3637 [this.outlinePanel
, this.stackLayout
], { '$': this.$, 'widths': [1, 2] }
3639 if ( this.editable
) {
3640 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
3642 { '$': this.$, 'adders': this.adders
}
3648 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
3649 if ( this.outlined
) {
3650 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
3651 // Event 'focus' does not bubble, but 'focusin' does
3652 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
3656 this.$element
.addClass( 'oo-ui-bookletLayout' );
3657 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
3658 if ( this.outlined
) {
3659 this.outlinePanel
.$element
3660 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
3661 .append( this.outlineWidget
.$element
);
3662 if ( this.editable
) {
3663 this.outlinePanel
.$element
3664 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
3665 .append( this.outlineControlsWidget
.$element
);
3667 this.$element
.append( this.gridLayout
.$element
);
3669 this.$element
.append( this.stackLayout
.$element
);
3675 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
3681 * @param {OO.ui.PageLayout} page Current page
3686 * @param {OO.ui.PageLayout[]} page Added pages
3687 * @param {number} index Index pages were added at
3692 * @param {OO.ui.PageLayout[]} pages Removed pages
3698 * Handle stack layout focus.
3701 * @param {jQuery.Event} e Focusin event
3703 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
3706 if ( this.ignoreFocus
) {
3707 // Avoid recursion from programmatic focus trigger in #onStackLayoutSet
3711 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
3712 for ( name
in this.pages
) {
3713 if ( this.pages
[ name
].$element
[0] === $target
[0] ) {
3714 this.setPage( name
);
3721 * Handle stack layout set events.
3724 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
3726 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
3728 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
3729 this.ignoreFocus
= true;
3730 if ( this.autoFocus
) {
3731 page
.$element
.find( ':input:first' ).focus();
3733 this.ignoreFocus
= false;
3739 * Handle outline widget select events.
3742 * @param {OO.ui.OptionWidget|null} item Selected item
3744 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
3746 this.setPage( item
.getData() );
3751 * Check if booklet has an outline.
3754 * @returns {boolean} Booklet is outlined
3756 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
3757 return this.outlined
;
3761 * Check if booklet has editing controls.
3764 * @returns {boolean} Booklet is outlined
3766 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
3767 return this.editable
;
3771 * Get the outline widget.
3774 * @returns {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
3776 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
3777 return this.outlineWidget
;
3781 * Get the outline controls widget. If the outline is not editable, null is returned.
3784 * @returns {OO.ui.OutlineControlsWidget|null} The outline controls widget.
3786 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
3787 return this.outlineControlsWidget
;
3791 * Get a page by name.
3794 * @param {string} name Symbolic name of page
3795 * @returns {OO.ui.PageLayout|undefined} Page, if found
3797 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
3798 return this.pages
[name
];
3802 * Get the current page name.
3805 * @returns {string|null} Current page name
3807 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
3808 return this.currentPageName
;
3812 * Add a page to the layout.
3814 * When pages are added with the same names as existing pages, the existing pages will be
3815 * automatically removed before the new pages are added.
3818 * @param {OO.ui.PageLayout[]} pages Pages to add
3819 * @param {number} index Index to insert pages after
3823 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
3824 var i
, len
, name
, page
,
3828 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
3830 name
= page
.getName();
3831 if ( name
in this.pages
) {
3832 // Remove page with same name
3833 remove
.push( this.pages
[name
] );
3835 this.pages
[page
.getName()] = page
;
3836 if ( this.outlined
) {
3837 items
.push( new OO
.ui
.BookletOutlineItemWidget( name
, page
, { '$': this.$ } ) );
3840 if ( remove
.length
) {
3841 this.removePages( remove
);
3844 if ( this.outlined
&& items
.length
) {
3845 this.outlineWidget
.addItems( items
, index
);
3846 this.updateOutlineWidget();
3848 this.stackLayout
.addItems( pages
, index
);
3849 this.emit( 'add', pages
, index
);
3855 * Remove a page from the layout.
3861 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
3862 var i
, len
, name
, page
,
3865 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
3867 name
= page
.getName();
3868 delete this.pages
[name
];
3869 if ( this.outlined
) {
3870 items
.push( this.outlineWidget
.getItemFromData( name
) );
3873 if ( this.outlined
&& items
.length
) {
3874 this.outlineWidget
.removeItems( items
);
3875 this.updateOutlineWidget();
3877 this.stackLayout
.removeItems( pages
);
3878 this.emit( 'remove', pages
);
3884 * Clear all pages from the layout.
3890 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
3891 var pages
= this.stackLayout
.getItems();
3894 this.currentPageName
= null;
3895 if ( this.outlined
) {
3896 this.outlineWidget
.clearItems();
3898 this.stackLayout
.clearItems();
3900 this.emit( 'remove', pages
);
3906 * Set the current page by name.
3910 * @param {string} name Symbolic name of page
3912 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
3914 page
= this.pages
[name
];
3916 if ( this.outlined
) {
3917 selectedItem
= this.outlineWidget
.getSelectedItem();
3918 if ( selectedItem
&& selectedItem
.getData() !== name
) {
3919 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
3924 this.currentPageName
= name
;
3925 this.stackLayout
.setItem( page
);
3926 this.emit( 'set', page
);
3931 * Call this after adding or removing items from the OutlineWidget.
3936 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
3937 // Auto-select first item when nothing is selected anymore
3938 if ( !this.outlineWidget
.getSelectedItem() ) {
3939 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
3945 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
3948 * @extends OO.ui.Layout
3951 * @param {Object} [config] Configuration options
3952 * @cfg {boolean} [scrollable] Allow vertical scrolling
3953 * @cfg {boolean} [padded] Pad the content from the edges
3955 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
3956 // Config initialization
3957 config
= config
|| {};
3959 // Parent constructor
3960 OO
.ui
.Layout
.call( this, config
);
3963 this.$element
.addClass( 'oo-ui-panelLayout' );
3964 if ( config
.scrollable
) {
3965 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
3968 if ( config
.padded
) {
3969 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
3972 // Add directionality class:
3973 this.$element
.addClass( 'oo-ui-' + OO
.ui
.Element
.getDir( this.$.context
) );
3978 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
3980 * Page within an OO.ui.BookletLayout.
3983 * @extends OO.ui.PanelLayout
3986 * @param {string} name Unique symbolic name of page
3987 * @param {Object} [config] Configuration options
3988 * @param {string} [icon=''] Symbolic name of icon to display in outline
3989 * @param {string} [indicator=''] Symbolic name of indicator to display in outline
3990 * @param {string} [indicatorTitle=''] Description of indicator meaning to display in outline
3991 * @param {string} [label=''] Label to display in outline
3992 * @param {number} [level=0] Indentation level of item in outline
3993 * @param {boolean} [movable=false] Page should be movable using outline controls
3995 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
3996 // Configuration initialization
3997 config
= $.extend( { 'scrollable': true }, config
);
3999 // Parent constructor
4000 OO
.ui
.PanelLayout
.call( this, config
);
4004 this.icon
= config
.icon
|| '';
4005 this.indicator
= config
.indicator
|| '';
4006 this.indicatorTitle
= OO
.ui
.resolveMsg( config
.indicatorTitle
) || '';
4007 this.label
= OO
.ui
.resolveMsg( config
.label
) || '';
4008 this.level
= config
.level
|| 0;
4009 this.movable
= !!config
.movable
;
4012 this.$element
.addClass( 'oo-ui-pageLayout' );
4017 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4024 * @returns {string} Symbolic name of page
4026 OO
.ui
.PageLayout
.prototype.getName = function () {
4033 * @returns {string} Symbolic name of icon
4035 OO
.ui
.PageLayout
.prototype.getIcon = function () {
4040 * Get page indicator.
4042 * @returns {string} Symbolic name of indicator
4044 OO
.ui
.PageLayout
.prototype.getIndicator = function () {
4045 return this.indicator
;
4049 * Get page indicator label.
4051 * @returns {string} Description of indicator meaning
4053 OO
.ui
.PageLayout
.prototype.getIndicatorTitle = function () {
4054 return this.indicatorTitle
;
4060 * @returns {string} Label text
4062 OO
.ui
.PageLayout
.prototype.getLabel = function () {
4067 * Get outline item indentation level.
4069 * @returns {number} Indentation level
4071 OO
.ui
.PageLayout
.prototype.getLevel = function () {
4076 * Check if page is movable using outline controls.
4078 * @returns {boolean} Page is movable
4080 OO
.ui
.PageLayout
.prototype.isMovable = function () {
4081 return this.movable
;
4084 * Layout containing a series of mutually exclusive pages.
4087 * @extends OO.ui.PanelLayout
4088 * @mixins OO.ui.GroupElement
4091 * @param {Object} [config] Configuration options
4092 * @cfg {boolean} [continuous=false] Show all pages, one after another
4093 * @cfg {string} [icon=''] Symbolic icon name
4095 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4096 // Config initialization
4097 config
= $.extend( { 'scrollable': true }, config
);
4099 // Parent constructor
4100 OO
.ui
.PanelLayout
.call( this, config
);
4102 // Mixin constructors
4103 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4106 this.currentItem
= null;
4107 this.continuous
= !!config
.continuous
;
4110 this.$element
.addClass( 'oo-ui-stackLayout' );
4111 if ( this.continuous
) {
4112 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4118 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4120 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4126 * @param {OO.ui.PanelLayout|null} [item] Current item
4134 * Adding an existing item (by value) will move it.
4137 * @param {OO.ui.PanelLayout[]} items Items to add
4138 * @param {number} [index] Index to insert items after
4141 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
4142 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
4144 if ( !this.currentItem
&& items
.length
) {
4145 this.setItem( items
[0] );
4154 * Items will be detached, not removed, so they can be used later.
4157 * @param {OO.ui.PanelLayout[]} items Items to remove
4160 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
4161 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
4162 if ( items
.indexOf( this.currentItem
) !== -1 ) {
4163 this.currentItem
= null;
4164 if ( !this.currentItem
&& this.items
.length
) {
4165 this.setItem( this.items
[0] );
4175 * Items will be detached, not removed, so they can be used later.
4180 OO
.ui
.StackLayout
.prototype.clearItems = function () {
4181 this.currentItem
= null;
4182 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
4190 * Any currently shown item will be hidden.
4193 * @param {OO.ui.PanelLayout} item Item to show
4196 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
4197 if ( !this.continuous
) {
4198 this.$items
.css( 'display', '' );
4200 if ( this.items
.indexOf( item
) !== -1 ) {
4201 if ( !this.continuous
) {
4202 item
.$element
.css( 'display', 'block' );
4207 this.currentItem
= item
;
4208 this.emit( 'set', item
);
4213 * Horizontal bar layout of tools as icon buttons.
4217 * @extends OO.ui.ToolGroup
4220 * @param {OO.ui.Toolbar} toolbar
4221 * @param {Object} [config] Configuration options
4223 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
4224 // Parent constructor
4225 OO
.ui
.ToolGroup
.call( this, toolbar
, config
);
4228 this.$element
.addClass( 'oo-ui-barToolGroup' );
4233 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
4235 /* Static Properties */
4237 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
4239 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
4241 * Popup list of tools with an icon and optional label.
4245 * @extends OO.ui.ToolGroup
4246 * @mixins OO.ui.IconedElement
4247 * @mixins OO.ui.IndicatedElement
4248 * @mixins OO.ui.LabeledElement
4249 * @mixins OO.ui.TitledElement
4250 * @mixins OO.ui.ClippableElement
4253 * @param {OO.ui.Toolbar} toolbar
4254 * @param {Object} [config] Configuration options
4256 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
4257 // Configuration initialization
4258 config
= config
|| {};
4260 // Parent constructor
4261 OO
.ui
.ToolGroup
.call( this, toolbar
, config
);
4263 // Mixin constructors
4264 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4265 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
4266 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
4267 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
4268 OO
.ui
.ClippableElement
.call( this, this.$group
);
4271 this.active
= false;
4272 this.dragging
= false;
4273 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
4274 this.$handle
= this.$( '<span>' );
4278 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
4279 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
4284 .addClass( 'oo-ui-popupToolGroup-handle' )
4285 .append( this.$icon
, this.$label
, this.$indicator
);
4287 .addClass( 'oo-ui-popupToolGroup' )
4288 .prepend( this.$handle
);
4293 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
4295 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
4296 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
4297 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
4298 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
4299 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
4301 /* Static Properties */
4306 * Handle focus being lost.
4308 * The event is actually generated from a mouseup, so it is not a normal blur event object.
4311 * @param {jQuery.Event} e Mouse up event
4313 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
4314 // Only deactivate when clicking outside the dropdown element
4315 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
4316 this.setActive( false );
4323 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
4324 this.setActive( false );
4325 return OO
.ui
.ToolGroup
.prototype.onMouseUp
.call( this, e
);
4331 OO
.ui
.PopupToolGroup
.prototype.onMouseDown = function ( e
) {
4332 return OO
.ui
.ToolGroup
.prototype.onMouseDown
.call( this, e
);
4336 * Handle mouse up events.
4339 * @param {jQuery.Event} e Mouse up event
4341 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
4346 * Handle mouse down events.
4349 * @param {jQuery.Event} e Mouse down event
4351 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
4352 if ( !this.disabled
&& e
.which
=== 1 ) {
4353 this.setActive( !this.active
);
4359 * Switch into active mode.
4361 * When active, mouseup events anywhere in the document will trigger deactivation.
4365 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
4367 if ( this.active
!== value
) {
4368 this.active
= value
;
4370 this.setClipping( true );
4371 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
4372 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
4374 this.setClipping( false );
4375 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
4376 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
4381 * Drop down list layout of tools as labeled icon buttons.
4385 * @extends OO.ui.PopupToolGroup
4388 * @param {OO.ui.Toolbar} toolbar
4389 * @param {Object} [config] Configuration options
4391 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
4392 // Parent constructor
4393 OO
.ui
.PopupToolGroup
.call( this, toolbar
, config
);
4396 this.$element
.addClass( 'oo-ui-listToolGroup' );
4401 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
4403 /* Static Properties */
4405 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
4407 * Drop down menu layout of tools as selectable menu items.
4411 * @extends OO.ui.PopupToolGroup
4414 * @param {OO.ui.Toolbar} toolbar
4415 * @param {Object} [config] Configuration options
4417 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
4418 // Configuration initialization
4419 config
= config
|| {};
4421 // Parent constructor
4422 OO
.ui
.PopupToolGroup
.call( this, toolbar
, config
);
4425 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
4428 this.$element
.addClass( 'oo-ui-menuToolGroup' );
4433 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
4435 /* Static Properties */
4437 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
4442 * Handle the toolbar state being updated.
4444 * When the state changes, the title of each active item in the menu will be joined together and
4445 * used as a label for the group. The label will be empty if none of the items are active.
4449 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
4453 for ( name
in this.tools
) {
4454 if ( this.tools
[name
].isActive() ) {
4455 labelTexts
.push( this.tools
[name
].getTitle() );
4459 this.setLabel( labelTexts
.join( ', ' ) );
4462 * UserInterface popup tool.
4466 * @extends OO.ui.Tool
4467 * @mixins OO.ui.PopuppableElement
4470 * @param {OO.ui.Toolbar} toolbar
4471 * @param {Object} [config] Configuration options
4473 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
4474 // Parent constructor
4475 OO
.ui
.Tool
.call( this, toolbar
, config
);
4477 // Mixin constructors
4478 OO
.ui
.PopuppableElement
.call( this, config
);
4482 .addClass( 'oo-ui-popupTool' )
4483 .append( this.popup
.$element
);
4488 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
4490 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
4495 * Handle the tool being selected.
4499 OO
.ui
.PopupTool
.prototype.onSelect = function () {
4500 if ( !this.disabled
) {
4501 if ( this.popup
.isVisible() ) {
4507 this.setActive( false );
4512 * Handle the toolbar state being updated.
4516 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
4517 this.setActive( false );
4520 * Container for multiple related buttons.
4523 * @extends OO.ui.Widget
4524 * @mixin OO.ui.GroupElement
4527 * @param {Object} [config] Configuration options
4529 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
4530 // Parent constructor
4531 OO
.ui
.Widget
.call( this, config
);
4533 // Mixin constructors
4534 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4537 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
4542 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
4544 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
4546 * Creates an OO.ui.ButtonWidget object.
4550 * @extends OO.ui.Widget
4551 * @mixins OO.ui.ButtonedElement
4552 * @mixins OO.ui.IconedElement
4553 * @mixins OO.ui.IndicatedElement
4554 * @mixins OO.ui.LabeledElement
4555 * @mixins OO.ui.TitledElement
4556 * @mixins OO.ui.FlaggableElement
4559 * @param {Object} [config] Configuration options
4560 * @cfg {string} [title=''] Title text
4561 * @cfg {string} [href] Hyperlink to visit when clicked
4562 * @cfg {string} [target] Target to open hyperlink in
4564 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
4565 // Configuration initialization
4566 config
= $.extend( { 'target': '_blank' }, config
);
4568 // Parent constructor
4569 OO
.ui
.Widget
.call( this, config
);
4571 // Mixin constructors
4572 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
4573 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4574 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
4575 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
4576 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
4577 OO
.ui
.FlaggableElement
.call( this, config
);
4580 this.isHyperlink
= typeof config
.href
=== 'string';
4584 'click': OO
.ui
.bind( this.onClick
, this ),
4585 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
4590 .append( this.$icon
, this.$label
, this.$indicator
)
4591 .attr( { 'href': config
.href
, 'target': config
.target
} );
4593 .addClass( 'oo-ui-buttonWidget' )
4594 .append( this.$button
);
4599 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
4601 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
4602 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
4603 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
4604 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
4605 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
4606 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
4617 * Handles mouse click events.
4620 * @param {jQuery.Event} e Mouse click event
4623 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
4624 if ( !this.disabled
) {
4625 this.emit( 'click' );
4626 if ( this.isHyperlink
) {
4634 * Handles keypress events.
4637 * @param {jQuery.Event} e Keypress event
4640 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
4641 if ( !this.disabled
&& e
.which
=== OO
.ui
.Keys
.SPACE
) {
4642 if ( this.isHyperlink
) {
4650 * Creates an OO.ui.InputWidget object.
4654 * @extends OO.ui.Widget
4657 * @param {Object} [config] Configuration options
4658 * @cfg {string} [name=''] HTML input name
4659 * @cfg {string} [value=''] Input value
4660 * @cfg {boolean} [readOnly=false] Prevent changes
4661 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
4663 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
4664 // Config intialization
4665 config
= $.extend( { 'readOnly': false }, config
);
4667 // Parent constructor
4668 OO
.ui
.Widget
.call( this, config
);
4671 this.$input
= this.getInputElement( config
);
4673 this.readOnly
= false;
4674 this.inputFilter
= config
.inputFilter
;
4677 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
4681 .attr( 'name', config
.name
)
4682 .prop( 'disabled', this.disabled
);
4683 this.setReadOnly( config
.readOnly
);
4684 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
4685 this.setValue( config
.value
);
4690 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
4702 * Get input element.
4705 * @param {Object} [config] Configuration options
4706 * @returns {jQuery} Input element
4708 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
4709 return this.$( '<input>' );
4713 * Handle potentially value-changing events.
4716 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
4718 OO
.ui
.InputWidget
.prototype.onEdit = function () {
4719 if ( !this.disabled
) {
4720 // Allow the stack to clear so the value will be updated
4721 setTimeout( OO
.ui
.bind( function () {
4722 this.setValue( this.$input
.val() );
4728 * Get the value of the input.
4731 * @returns {string} Input value
4733 OO
.ui
.InputWidget
.prototype.getValue = function () {
4738 * Sets the direction of the current input, either RTL or LTR
4741 * @param {boolean} isRTL
4743 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
4745 this.$input
.removeClass( 'oo-ui-ltr' );
4746 this.$input
.addClass( 'oo-ui-rtl' );
4748 this.$input
.removeClass( 'oo-ui-rtl' );
4749 this.$input
.addClass( 'oo-ui-ltr' );
4754 * Set the value of the input.
4757 * @param {string} value New value
4761 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
4762 value
= this.sanitizeValue( value
);
4763 if ( this.value
!== value
) {
4765 this.emit( 'change', this.value
);
4767 // Update the DOM if it has changed. Note that with sanitizeValue, it
4768 // is possible for the DOM value to change without this.value changing.
4769 if ( this.$input
.val() !== this.value
) {
4770 this.$input
.val( this.value
);
4776 * Sanitize incoming value.
4778 * Ensures value is a string, and converts undefined and null to empty strings.
4781 * @param {string} value Original value
4782 * @returns {string} Sanitized value
4784 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
4785 if ( value
=== undefined || value
=== null ) {
4787 } else if ( this.inputFilter
) {
4788 return this.inputFilter( String( value
) );
4790 return String( value
);
4795 * Check if the widget is read-only.
4798 * @param {boolean} Input is read-only
4800 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
4801 return this.readOnly
;
4805 * Set the read-only state of the widget.
4807 * This should probably change the widgets's appearance and prevent it from being used.
4810 * @param {boolean} state Make input read-only
4813 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
4814 this.readOnly
= !!state
;
4815 this.$input
.prop( 'readonly', this.readOnly
);
4822 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
4823 OO
.ui
.Widget
.prototype.setDisabled
.call( this, state
);
4824 if ( this.$input
) {
4825 this.$input
.prop( 'disabled', this.disabled
);
4829 * Creates an OO.ui.CheckboxInputWidget object.
4832 * @extends OO.ui.InputWidget
4835 * @param {Object} [config] Configuration options
4837 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
4838 // Parent constructor
4839 OO
.ui
.InputWidget
.call( this, config
);
4842 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
4847 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
4854 * Get input element.
4856 * @returns {jQuery} Input element
4858 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
4859 return this.$( '<input type="checkbox" />' );
4863 * Get checked state of the checkbox
4865 * @returns {boolean} If the checkbox is checked
4867 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
4874 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
4876 if ( this.value
!== value
) {
4878 this.$input
.prop( 'checked', this.value
);
4879 this.emit( 'change', this.value
);
4886 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
4887 if ( !this.disabled
) {
4888 // Allow the stack to clear so the value will be updated
4889 setTimeout( OO
.ui
.bind( function () {
4890 this.setValue( this.$input
.prop( 'checked' ) );
4895 * Creates an OO.ui.CheckboxWidget object.
4898 * @extends OO.ui.CheckboxInputWidget
4899 * @mixins OO.ui.LabeledElement
4902 * @param {Object} [config] Configuration options
4903 * @cfg {string} [label=''] Label
4905 OO
.ui
.CheckboxWidget
= function OoUiCheckboxWidget( config
) {
4906 // Configuration initialization
4907 config
= config
|| {};
4909 // Parent constructor
4910 OO
.ui
.CheckboxInputWidget
.call( this, config
);
4912 // Mixin constructors
4913 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ) , config
);
4917 .addClass( 'oo-ui-checkboxWidget' )
4918 .append( this.$( '<label>' ).append( this.$input
, this.$label
) );
4923 OO
.inheritClass( OO
.ui
.CheckboxWidget
, OO
.ui
.CheckboxInputWidget
);
4925 OO
.mixinClass( OO
.ui
.CheckboxWidget
, OO
.ui
.LabeledElement
);
4927 * Creates an OO.ui.InputLabelWidget object.
4929 * CSS classes will be added to the button for each flag, each prefixed with 'oo-ui-InputLabelWidget-'
4932 * @extends OO.ui.Widget
4933 * @mixins OO.ui.LabeledElement
4936 * @param {Object} [config] Configuration options
4937 * @cfg {OO.ui.InputWidget|null} [input] Related input widget
4939 OO
.ui
.InputLabelWidget
= function OoUiInputLabelWidget( config
) {
4940 // Config intialization
4941 config
= $.extend( { 'input': null }, config
);
4943 // Parent constructor
4944 OO
.ui
.Widget
.call( this, config
);
4946 // Mixin constructors
4947 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
4950 this.input
= config
.input
;
4953 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
4956 this.$element
.addClass( 'oo-ui-inputLabelWidget' );
4961 OO
.inheritClass( OO
.ui
.InputLabelWidget
, OO
.ui
.Widget
);
4963 OO
.mixinClass( OO
.ui
.InputLabelWidget
, OO
.ui
.LabeledElement
);
4965 /* Static Properties */
4967 OO
.ui
.InputLabelWidget
.static.tagName
= 'label';
4972 * Handles mouse click events.
4975 * @param {jQuery.Event} e Mouse click event
4977 OO
.ui
.InputLabelWidget
.prototype.onClick = function () {
4978 if ( !this.disabled
&& this.input
) {
4979 this.input
.$input
.focus();
4984 * Lookup input widget.
4986 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
4987 * events on #lookupMenu to make use of selections.
4993 * @param {OO.ui.TextInputWidget} input Input widget
4994 * @param {Object} [config] Configuration options
4995 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
4997 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
4998 // Config intialization
4999 config
= config
|| {};
5002 this.lookupInput
= input
;
5003 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
5004 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
5005 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
5006 'input': this.lookupInput
,
5007 '$container': config
.$container
5009 this.lookupCache
= {};
5010 this.lookupQuery
= null;
5011 this.lookupRequest
= null;
5012 this.populating
= false;
5015 this.$overlay
.append( this.lookupMenu
.$element
);
5017 this.lookupInput
.$input
.on( {
5018 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
5019 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
5020 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
5022 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
5025 this.$element
.addClass( 'oo-ui-lookupWidget' );
5026 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
5032 * Handle input focus event.
5035 * @param {jQuery.Event} e Input focus event
5037 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
5038 this.openLookupMenu();
5042 * Handle input blur event.
5045 * @param {jQuery.Event} e Input blur event
5047 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
5048 this.lookupMenu
.hide();
5052 * Handle input mouse down event.
5055 * @param {jQuery.Event} e Input mouse down event
5057 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
5058 this.openLookupMenu();
5062 * Handle input change event.
5065 * @param {string} value New input value
5067 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
5068 this.openLookupMenu();
5077 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
5078 var value
= this.lookupInput
.getValue();
5080 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
5081 this.populateLookupMenu();
5082 if ( !this.lookupMenu
.isVisible() ) {
5083 this.lookupMenu
.show();
5086 this.lookupMenu
.clearItems();
5087 this.lookupMenu
.hide();
5094 * Populate lookup menu with current information.
5099 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
5100 if ( !this.populating
) {
5101 this.populating
= true;
5102 this.getLookupMenuItems()
5103 .done( OO
.ui
.bind( function ( items
) {
5104 this.lookupMenu
.clearItems();
5105 if ( items
.length
) {
5106 this.lookupMenu
.show();
5107 this.lookupMenu
.addItems( items
);
5108 this.initializeLookupMenuSelection();
5109 this.openLookupMenu();
5111 this.lookupMenu
.hide();
5113 this.populating
= false;
5115 .fail( OO
.ui
.bind( function () {
5116 this.lookupMenu
.clearItems();
5117 this.populating
= false;
5125 * Set selection in the lookup menu with current information.
5130 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
5131 if ( !this.lookupMenu
.getSelectedItem() ) {
5132 this.lookupMenu
.intializeSelection( this.lookupMenu
.getFirstSelectableItem() );
5134 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
5138 * Get lookup menu items for the current query.
5141 * @returns {jQuery.Promise} Promise object which will be passed menu items as the first argument
5144 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
5145 var value
= this.lookupInput
.getValue(),
5146 deferred
= $.Deferred();
5148 if ( value
&& value
!== this.lookupQuery
) {
5149 // Abort current request if query has changed
5150 if ( this.lookupRequest
) {
5151 this.lookupRequest
.abort();
5152 this.lookupQuery
= null;
5153 this.lookupRequest
= null;
5155 if ( value
in this.lookupCache
) {
5156 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5158 this.lookupQuery
= value
;
5159 this.lookupRequest
= this.getLookupRequest()
5160 .always( OO
.ui
.bind( function () {
5161 this.lookupQuery
= null;
5162 this.lookupRequest
= null;
5164 .done( OO
.ui
.bind( function ( data
) {
5165 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
5166 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5168 .fail( function () {
5172 this.lookupRequest
.always( OO
.ui
.bind( function () {
5177 return deferred
.promise();
5181 * Get a new request object of the current lookup query value.
5185 * @returns {jqXHR} jQuery AJAX object, or promise object with an .abort() method
5187 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
5188 // Stub, implemented in subclass
5193 * Handle successful lookup request.
5195 * Overriding methods should call #populateLookupMenu when results are available and cache results
5196 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
5200 * @param {Mixed} data Response from server
5202 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
5203 // Stub, implemented in subclass
5207 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
5211 * @param {Mixed} data Cached result data, usually an array
5212 * @returns {OO.ui.MenuItemWidget[]} Menu items
5214 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
5215 // Stub, implemented in subclass
5219 * Creates an OO.ui.OptionWidget object.
5223 * @extends OO.ui.Widget
5224 * @mixins OO.ui.IconedElement
5225 * @mixins OO.ui.LabeledElement
5226 * @mixins OO.ui.IndicatedElement
5229 * @param {Mixed} data Option data
5230 * @param {Object} [config] Configuration options
5231 * @cfg {boolean} [selected=false] Select option
5232 * @cfg {boolean} [highlighted=false] Highlight option
5233 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
5235 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
5236 // Config intialization
5237 config
= config
|| {};
5239 // Parent constructor
5240 OO
.ui
.Widget
.call( this, config
);
5242 // Mixin constructors
5243 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5244 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5245 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5249 this.selected
= false;
5250 this.highlighted
= false;
5254 .data( 'oo-ui-optionWidget', this )
5255 .attr( 'rel', config
.rel
)
5256 .addClass( 'oo-ui-optionWidget' )
5257 .append( this.$label
);
5258 this.setSelected( config
.selected
);
5259 this.setHighlighted( config
.highlighted
);
5263 .prepend( this.$icon
)
5264 .append( this.$indicator
);
5269 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
5271 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
5272 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
5273 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
5275 /* Static Properties */
5277 OO
.ui
.OptionWidget
.static.tagName
= 'li';
5279 OO
.ui
.OptionWidget
.static.selectable
= true;
5281 OO
.ui
.OptionWidget
.static.highlightable
= true;
5283 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
5288 * Check if option can be selected.
5291 * @returns {boolean} Item is selectable
5293 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
5294 return this.constructor.static.selectable
&& !this.disabled
;
5298 * Check if option can be highlighted.
5301 * @returns {boolean} Item is highlightable
5303 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
5304 return this.constructor.static.highlightable
&& !this.disabled
;
5308 * Check if option is selected.
5311 * @returns {boolean} Item is selected
5313 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
5314 return this.selected
;
5318 * Check if option is highlighted.
5321 * @returns {boolean} Item is highlighted
5323 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
5324 return this.highlighted
;
5328 * Set selected state.
5331 * @param {boolean} [state=false] Select option
5334 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
5335 if ( !this.disabled
&& this.constructor.static.selectable
) {
5336 this.selected
= !!state
;
5337 if ( this.selected
) {
5338 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
5339 if ( this.constructor.static.scrollIntoViewOnSelect
) {
5340 this.scrollElementIntoView();
5343 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
5350 * Set highlighted state.
5353 * @param {boolean} [state=false] Highlight option
5356 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
5357 if ( !this.disabled
&& this.constructor.static.highlightable
) {
5358 this.highlighted
= !!state
;
5359 if ( this.highlighted
) {
5360 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
5362 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
5369 * Make the option's highlight flash.
5372 * @param {Function} [done] Callback to execute when flash effect is complete.
5374 OO
.ui
.OptionWidget
.prototype.flash = function ( done
) {
5375 var $this = this.$element
;
5377 if ( !this.disabled
&& this.constructor.static.highlightable
) {
5378 $this.removeClass( 'oo-ui-optionWidget-highlighted' );
5379 setTimeout( OO
.ui
.bind( function () {
5380 $this.addClass( 'oo-ui-optionWidget-highlighted' );
5382 setTimeout( done
, 100 );
5392 * @returns {Mixed} Option data
5394 OO
.ui
.OptionWidget
.prototype.getData = function () {
5398 * Create an OO.ui.SelectWidget object.
5402 * @extends OO.ui.Widget
5403 * @mixin OO.ui.GroupElement
5406 * @param {Object} [config] Configuration options
5408 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
5409 // Config intialization
5410 config
= config
|| {};
5412 // Parent constructor
5413 OO
.ui
.Widget
.call( this, config
);
5415 // Mixin constructors
5416 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
5419 this.pressed
= false;
5420 this.selecting
= null;
5425 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
5426 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
5427 'mousemove': OO
.ui
.bind( this.onMouseMove
, this ),
5428 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
5429 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
5433 this.$element
.addClass( 'oo-ui-selectWidget' );
5438 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
5440 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
5446 * @param {OO.ui.OptionWidget|null} item Highlighted item
5451 * @param {OO.ui.OptionWidget|null} item Selected item
5456 * @param {OO.ui.OptionWidget[]} items Added items
5457 * @param {number} index Index items were added at
5462 * @param {OO.ui.OptionWidget[]} items Removed items
5465 /* Static Properties */
5467 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
5472 * Handle mouse down events.
5476 * @param {jQuery.Event} e Mouse down event
5478 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
5481 if ( !this.disabled
&& e
.which
=== 1 ) {
5482 this.pressed
= true;
5483 item
= this.getTargetItem( e
);
5484 if ( item
&& item
.isSelectable() ) {
5485 this.intializeSelection( item
);
5486 this.selecting
= item
;
5487 this.$( this.$.context
).one( 'mouseup', OO
.ui
.bind( this.onMouseUp
, this ) );
5494 * Handle mouse up events.
5498 * @param {jQuery.Event} e Mouse up event
5500 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
5502 this.pressed
= false;
5503 if ( !this.selecting
) {
5504 item
= this.getTargetItem( e
);
5505 if ( item
&& item
.isSelectable() ) {
5506 this.selecting
= item
;
5509 if ( !this.disabled
&& e
.which
=== 1 && this.selecting
) {
5510 this.selectItem( this.selecting
);
5511 this.selecting
= null;
5517 * Handle mouse move events.
5521 * @param {jQuery.Event} e Mouse move event
5523 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
5526 if ( !this.disabled
&& this.pressed
) {
5527 item
= this.getTargetItem( e
);
5528 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
5529 this.intializeSelection( item
);
5530 this.selecting
= item
;
5537 * Handle mouse over events.
5541 * @param {jQuery.Event} e Mouse over event
5543 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
5546 if ( !this.disabled
) {
5547 item
= this.getTargetItem( e
);
5548 if ( item
&& item
.isHighlightable() ) {
5549 this.highlightItem( item
);
5556 * Handle mouse leave events.
5560 * @param {jQuery.Event} e Mouse over event
5562 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
5563 if ( !this.disabled
) {
5564 this.highlightItem();
5570 * Get the closest item to a jQuery.Event.
5574 * @param {jQuery.Event} e
5575 * @returns {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
5577 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
5578 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
5579 if ( $item
.length
) {
5580 return $item
.data( 'oo-ui-optionWidget' );
5586 * Get selected item.
5589 * @returns {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
5591 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
5594 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5595 if ( this.items
[i
].isSelected() ) {
5596 return this.items
[i
];
5603 * Get highlighted item.
5606 * @returns {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
5608 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
5611 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5612 if ( this.items
[i
].isHighlighted() ) {
5613 return this.items
[i
];
5620 * Get an existing item with equivilant data.
5623 * @param {Object} data Item data to search for
5624 * @returns {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
5626 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
5627 var hash
= OO
.getHash( data
);
5629 if ( hash
in this.hashes
) {
5630 return this.hashes
[hash
];
5637 * Highlight an item.
5639 * Highlighting is mutually exclusive.
5642 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
5646 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
5649 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5650 this.items
[i
].setHighlighted( this.items
[i
] === item
);
5652 this.emit( 'highlight', item
);
5661 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
5665 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
5668 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5669 this.items
[i
].setSelected( this.items
[i
] === item
);
5671 this.emit( 'select', item
);
5677 * Setup selection and highlighting.
5679 * This should be used to synchronize the UI with the model without emitting events that would in
5680 * turn update the model.
5682 * @param {OO.ui.OptionWidget} [item] Item to select
5685 OO
.ui
.SelectWidget
.prototype.intializeSelection = function( item
) {
5686 var i
, len
, selected
;
5688 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5689 selected
= this.items
[i
] === item
;
5690 this.items
[i
].setSelected( selected
);
5691 this.items
[i
].setHighlighted( selected
);
5698 * Get an item relative to another one.
5701 * @param {OO.ui.OptionWidget} item Item to start at
5702 * @param {number} direction Direction to move in
5703 * @returns {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
5705 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
5706 var inc
= direction
> 0 ? 1 : -1,
5707 len
= this.items
.length
,
5708 index
= item
instanceof OO
.ui
.OptionWidget
?
5709 this.items
.indexOf( item
) : ( inc
> 0 ? -1 : 0 ),
5710 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
5712 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
5713 Math
.max( index
, -1 ) :
5714 // Default to n-1 instead of -1, if nothing is selected let's start at the end
5715 Math
.min( index
, len
);
5718 i
= ( i
+ inc
+ len
) % len
;
5719 item
= this.items
[i
];
5720 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
5723 // Stop iterating when we've looped all the way around
5724 if ( i
=== stopAt
) {
5732 * Get the next selectable item.
5735 * @returns {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
5737 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
5740 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5741 item
= this.items
[i
];
5742 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
5753 * When items are added with the same values as existing items, the existing items will be
5754 * automatically removed before the new items are added.
5757 * @param {OO.ui.OptionWidget[]} items Items to add
5758 * @param {number} [index] Index to insert items after
5762 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
5763 var i
, len
, item
, hash
,
5766 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
5768 hash
= OO
.getHash( item
.getData() );
5769 if ( hash
in this.hashes
) {
5770 // Remove item with same value
5771 remove
.push( this.hashes
[hash
] );
5773 this.hashes
[hash
] = item
;
5775 if ( remove
.length
) {
5776 this.removeItems( remove
);
5779 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
5781 // Always provide an index, even if it was omitted
5782 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
5790 * Items will be detached, not removed, so they can be used later.
5793 * @param {OO.ui.OptionWidget[]} items Items to remove
5797 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
5798 var i
, len
, item
, hash
;
5800 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
5802 hash
= OO
.getHash( item
.getData() );
5803 if ( hash
in this.hashes
) {
5804 // Remove existing item
5805 delete this.hashes
[hash
];
5807 if ( item
.isSelected() ) {
5808 this.selectItem( null );
5811 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
5813 this.emit( 'remove', items
);
5821 * Items will be detached, not removed, so they can be used later.
5827 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
5828 var items
= this.items
.slice();
5832 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
5833 this.selectItem( null );
5835 this.emit( 'remove', items
);
5840 * Creates an OO.ui.MenuItemWidget object.
5843 * @extends OO.ui.OptionWidget
5846 * @param {Mixed} data Item data
5847 * @param {Object} [config] Configuration options
5849 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
5850 // Configuration initialization
5851 config
= $.extend( { 'icon': 'check' }, config
);
5853 // Parent constructor
5854 OO
.ui
.OptionWidget
.call( this, data
, config
);
5857 this.$element
.addClass( 'oo-ui-menuItemWidget' );
5862 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
5864 * Create an OO.ui.MenuWidget object.
5867 * @extends OO.ui.SelectWidget
5868 * @mixins OO.ui.ClippableElement
5871 * @param {Object} [config] Configuration options
5872 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
5874 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
5875 // Config intialization
5876 config
= config
|| {};
5878 // Parent constructor
5879 OO
.ui
.SelectWidget
.call( this, config
);
5881 // Mixin constructors
5882 OO
.ui
.ClippableElement
.call( this, this.$group
);
5886 this.$input
= config
.input
? config
.input
.$input
: null;
5887 this.$previousFocus
= null;
5888 this.isolated
= !config
.input
;
5889 this.visible
= false;
5890 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
5893 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
5898 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
5900 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
5905 * Handles key down events.
5908 * @param {jQuery.Event} e Key down event
5910 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
5913 highlightItem
= this.getHighlightedItem();
5915 if ( !this.disabled
&& this.visible
) {
5916 if ( !highlightItem
) {
5917 highlightItem
= this.getSelectedItem();
5919 switch ( e
.keyCode
) {
5920 case OO
.ui
.Keys
.ENTER
:
5921 this.selectItem( highlightItem
);
5925 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
5928 case OO
.ui
.Keys
.DOWN
:
5929 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
5932 case OO
.ui
.Keys
.ESCAPE
:
5933 if ( highlightItem
) {
5934 highlightItem
.setHighlighted( false );
5942 this.highlightItem( nextItem
);
5943 nextItem
.scrollElementIntoView();
5948 e
.stopPropagation();
5955 * Check if the menu is visible.
5958 * @returns {boolean} Menu is visible
5960 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
5961 return this.visible
;
5965 * Bind key down listener
5969 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
5970 if ( this.$input
) {
5971 this.$input
.on( 'keydown', this.onKeyDownHandler
);
5973 // Capture menu navigation keys
5974 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
5979 * Unbind key down listener
5983 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
5984 if ( this.$input
) {
5985 this.$input
.off( 'keydown' );
5987 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
5994 * The menu will stay open if an item is silently selected.
5997 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6000 OO
.ui
.MenuWidget
.prototype.selectItem = function ( item
) {
6002 OO
.ui
.SelectWidget
.prototype.selectItem
.call( this, item
);
6004 if ( !this.disabled
) {
6006 this.disabled
= true;
6007 item
.flash( OO
.ui
.bind( function () {
6009 this.disabled
= false;
6022 * Adding an existing item (by value) will move it.
6025 * @param {OO.ui.MenuItemWidget[]} items Items to add
6026 * @param {number} [index] Index to insert items after
6029 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
6033 OO
.ui
.SelectWidget
.prototype.addItems
.call( this, items
, index
);
6035 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6037 if ( this.visible
) {
6038 // Defer fitting label until
6041 this.newItems
.push( item
);
6054 OO
.ui
.MenuWidget
.prototype.show = function () {
6057 if ( this.items
.length
) {
6058 this.$element
.show();
6059 this.visible
= true;
6060 this.bindKeyDownListener();
6062 // Change focus to enable keyboard navigation
6063 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
6064 this.$previousFocus
= this.$( ':focus' );
6065 this.$input
.focus();
6067 if ( this.newItems
.length
) {
6068 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
6069 this.newItems
[i
].fitLabel();
6074 this.setClipping( true );
6086 OO
.ui
.MenuWidget
.prototype.hide = function () {
6087 this.$element
.hide();
6088 this.visible
= false;
6089 this.unbindKeyDownListener();
6091 if ( this.isolated
&& this.$previousFocus
) {
6092 this.$previousFocus
.focus();
6093 this.$previousFocus
= null;
6096 this.setClipping( false );
6101 * Creates an OO.ui.MenuSectionItemWidget object.
6104 * @extends OO.ui.OptionWidget
6107 * @param {Mixed} data Item data
6108 * @param {Object} [config] Configuration options
6110 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
6111 // Parent constructor
6112 OO
.ui
.OptionWidget
.call( this, data
, config
);
6115 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
6120 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
6122 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
6124 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
6126 * Create an OO.ui.OutlineWidget object.
6129 * @extends OO.ui.SelectWidget
6132 * @param {Object} [config] Configuration options
6134 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
6135 // Config intialization
6136 config
= config
|| {};
6138 // Parent constructor
6139 OO
.ui
.SelectWidget
.call( this, config
);
6142 this.$element
.addClass( 'oo-ui-outlineWidget' );
6147 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
6149 * Creates an OO.ui.OutlineControlsWidget object.
6154 * @param {OO.ui.OutlineWidget} outline Outline to control
6155 * @param {Object} [config] Configuration options
6156 * @cfg {Object[]} [adders] List of icons to show as addable item types, each an object with
6157 * name, title and icon properties
6159 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
6160 // Configuration initialization
6161 config
= config
|| {};
6163 // Parent constructor
6164 OO
.ui
.Widget
.call( this, config
);
6167 this.outline
= outline
;
6169 this.$adders
= this.$( '<div>' );
6170 this.$movers
= this.$( '<div>' );
6171 this.addButton
= new OO
.ui
.ButtonWidget( {
6176 this.upButton
= new OO
.ui
.ButtonWidget( {
6180 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
6182 this.downButton
= new OO
.ui
.ButtonWidget( {
6186 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
6190 outline
.connect( this, {
6191 'select': 'onOutlineChange',
6192 'add': 'onOutlineChange',
6193 'remove': 'onOutlineChange'
6195 this.upButton
.connect( this, { 'click': ['emit', 'move', -1] } );
6196 this.downButton
.connect( this, { 'click': ['emit', 'move', 1] } );
6199 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
6200 this.$adders
.addClass( 'oo-ui-outlineControlsWidget-adders' );
6202 .addClass( 'oo-ui-outlineControlsWidget-movers' )
6203 .append( this.upButton
.$element
, this.downButton
.$element
);
6204 this.$element
.append( this.$adders
, this.$movers
);
6205 if ( config
.adders
&& config
.adders
.length
) {
6206 this.setupAdders( config
.adders
);
6212 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
6218 * @param {number} places Number of places to move
6224 * Handle outline change events.
6228 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
6229 var i
, len
, firstMovable
, lastMovable
,
6231 items
= this.outline
.getItems(),
6232 selectedItem
= this.outline
.getSelectedItem();
6234 if ( selectedItem
&& selectedItem
.isMovable() ) {
6238 while ( ++i
< len
) {
6239 if ( items
[i
].isMovable() ) {
6240 firstMovable
= items
[i
];
6246 if ( items
[i
].isMovable() ) {
6247 lastMovable
= items
[i
];
6252 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
6253 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
6257 * Setup adders icons.
6260 * @param {Object[]} adders List of configuations for adder buttons, each containing a name, title
6263 OO
.ui
.OutlineControlsWidget
.prototype.setupAdders = function ( adders
) {
6264 var i
, len
, addition
, button
,
6265 $buttons
= this.$( [] );
6267 this.$adders
.append( this.addButton
.$element
);
6268 for ( i
= 0, len
= adders
.length
; i
< len
; i
++ ) {
6269 addition
= adders
[i
];
6270 button
= new OO
.ui
.ButtonWidget( {
6271 '$': this.$, 'frameless': true, 'icon': addition
.icon
, 'title': addition
.title
6273 button
.connect( this, { 'click': ['emit', 'add', addition
.name
] } );
6274 this.adders
[addition
.name
] = button
;
6275 this.$adders
.append( button
.$element
);
6276 $buttons
= $buttons
.add( button
.$element
);
6280 * Creates an OO.ui.OutlineItemWidget object.
6283 * @extends OO.ui.OptionWidget
6286 * @param {Mixed} data Item data
6287 * @param {Object} [config] Configuration options
6288 * @cfg {number} [level] Indentation level
6289 * @cfg {boolean} [movable] Allow modification from outline controls
6291 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
6292 // Config intialization
6293 config
= config
|| {};
6295 // Parent constructor
6296 OO
.ui
.OptionWidget
.call( this, data
, config
);
6300 this.movable
= !!config
.movable
;
6303 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
6304 this.setLevel( config
.level
);
6309 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
6311 /* Static Properties */
6313 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
6315 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
6317 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
6319 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
6324 * Check if item is movable.
6326 * Moveablilty is used by outline controls.
6328 * @returns {boolean} Item is movable
6330 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
6331 return this.movable
;
6335 * Get indentation level.
6337 * @returns {number} Indentation level
6339 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
6344 * Set indentation level.
6347 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
6350 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
6351 var levels
= this.constructor.static.levels
,
6352 levelClass
= this.constructor.static.levelClass
,
6355 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
6357 if ( this.level
=== i
) {
6358 this.$element
.addClass( levelClass
+ i
);
6360 this.$element
.removeClass( levelClass
+ i
);
6367 * Creates an OO.ui.BookletOutlineItemWidget object.
6370 * @extends OO.ui.OutlineItemWidget
6373 * @param {Mixed} data Item data
6374 * @param {Object} [config] Configuration options
6376 OO
.ui
.BookletOutlineItemWidget
= function OoUiBookletOutlineItemWidget( data
, page
, config
) {
6377 // Configuration intialization
6378 config
= $.extend( {
6379 'label': page
.getLabel() || data
,
6380 'level': page
.getLevel(),
6381 'icon': page
.getIcon(),
6382 'indicator': page
.getIndicator(),
6383 'indicatorTitle': page
.getIndicatorTitle(),
6384 'movable': page
.isMovable()
6387 // Parent constructor
6388 OO
.ui
.OutlineItemWidget
.call( this, data
, config
);
6391 this.$element
.addClass( 'oo-ui-bookletOutlineItemWidget' );
6396 OO
.inheritClass( OO
.ui
.BookletOutlineItemWidget
, OO
.ui
.OutlineItemWidget
);
6398 * Create an OO.ui.ButtonSelect object.
6401 * @extends OO.ui.OptionWidget
6402 * @mixins OO.ui.ButtonedElement
6403 * @mixins OO.ui.FlaggableElement
6406 * @param {Mixed} data Option data
6407 * @param {Object} [config] Configuration options
6409 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
6410 // Parent constructor
6411 OO
.ui
.OptionWidget
.call( this, data
, config
);
6413 // Mixin constructors
6414 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
6415 OO
.ui
.FlaggableElement
.call( this, config
);
6418 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
6419 this.$button
.append( this.$element
.contents() );
6420 this.$element
.append( this.$button
);
6425 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
6427 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
6428 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
6435 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
6436 OO
.ui
.OptionWidget
.prototype.setSelected
.call( this, state
);
6438 this.setActive( state
);
6443 * Create an OO.ui.ButtonSelect object.
6446 * @extends OO.ui.SelectWidget
6449 * @param {Object} [config] Configuration options
6451 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
6452 // Parent constructor
6453 OO
.ui
.SelectWidget
.call( this, config
);
6456 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
6461 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
6463 * Creates an OO.ui.PopupWidget object.
6466 * @extends OO.ui.Widget
6467 * @mixins OO.ui.LabeledElement
6470 * @param {Object} [config] Configuration options
6471 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
6472 * @cfg {string} [align='center'] Alignment of popup to origin
6473 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
6474 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
6475 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
6476 * @cfg {boolean} [head] Show label and close button at the top
6478 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
6479 // Config intialization
6480 config
= config
|| {};
6482 // Parent constructor
6483 OO
.ui
.Widget
.call( this, config
);
6485 // Mixin constructors
6486 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
6489 this.visible
= false;
6490 this.$popup
= this.$( '<div>' );
6491 this.$head
= this.$( '<div>' );
6492 this.$body
= this.$( '<div>' );
6493 this.$tail
= this.$( '<div>' );
6494 this.$container
= config
.$container
|| this.$( 'body' );
6495 this.autoClose
= !!config
.autoClose
;
6496 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
6497 this.transitionTimeout
= null;
6499 this.align
= config
.align
|| 'center';
6500 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
6501 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
6504 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
6507 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
6508 this.$body
.addClass( 'oo-ui-popupWidget-body' );
6509 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
6511 .addClass( 'oo-ui-popupWidget-head' )
6512 .append( this.$label
, this.closeButton
.$element
);
6513 if ( !config
.head
) {
6517 .addClass( 'oo-ui-popupWidget-popup' )
6518 .append( this.$head
, this.$body
);
6519 this.$element
.hide()
6520 .addClass( 'oo-ui-popupWidget' )
6521 .append( this.$popup
, this.$tail
);
6526 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
6528 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
6543 * Handles mouse down events.
6546 * @param {jQuery.Event} e Mouse down event
6548 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
6551 !$.contains( this.$element
[0], e
.target
) &&
6552 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
6559 * Bind mouse down listener
6563 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
6564 // Capture clicks outside popup
6565 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
6569 * Handles close button click events.
6573 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
6574 if ( this.visible
) {
6580 * Unbind mouse down listener
6584 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
6585 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
6589 * Check if the popup is visible.
6592 * @returns {boolean} Popup is visible
6594 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
6595 return this.visible
;
6599 * Set whether to show a tail.
6602 * @returns {boolean} Make tail visible
6604 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
6606 if ( this.tail
!== value
) {
6609 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
6611 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
6617 * Check if showing a tail.
6620 * @returns {boolean} tail is visible
6622 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
6633 OO
.ui
.PopupWidget
.prototype.show = function () {
6634 if ( !this.visible
) {
6635 this.$element
.show();
6636 this.visible
= true;
6637 this.emit( 'show' );
6638 if ( this.autoClose
) {
6639 this.bindMouseDownListener();
6652 OO
.ui
.PopupWidget
.prototype.hide = function () {
6653 if ( this.visible
) {
6654 this.$element
.hide();
6655 this.visible
= false;
6656 this.emit( 'hide' );
6657 if ( this.autoClose
) {
6658 this.unbindMouseDownListener();
6665 * Updates the position and size.
6668 * @param {number} width Width
6669 * @param {number} height Height
6670 * @param {boolean} [transition=false] Use a smooth transition
6673 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
6675 originOffset
= Math
.round( this.$element
.offset().left
),
6676 containerLeft
= Math
.round( this.$container
.offset().left
),
6677 containerWidth
= this.$container
.innerWidth(),
6678 containerRight
= containerLeft
+ containerWidth
,
6679 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
6680 popupLeft
= popupOffset
- padding
,
6681 popupRight
= popupOffset
+ padding
+ width
+ padding
,
6682 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
6683 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
6685 // Prevent transition from being interrupted
6686 clearTimeout( this.transitionTimeout
);
6688 // Enable transition
6689 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
6692 if ( overlapRight
< 0 ) {
6693 popupOffset
+= overlapRight
;
6694 } else if ( overlapLeft
< 0 ) {
6695 popupOffset
-= overlapLeft
;
6698 // Position body relative to anchor and resize
6700 'left': popupOffset
,
6702 'height': height
=== undefined ? 'auto' : height
6706 // Prevent transitioning after transition is complete
6707 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
6708 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
6711 // Prevent transitioning immediately
6712 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
6718 * Button that shows and hides a popup.
6721 * @extends OO.ui.ButtonWidget
6722 * @mixins OO.ui.PopuppableElement
6725 * @param {Object} [config] Configuration options
6727 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
6728 // Parent constructor
6729 OO
.ui
.ButtonWidget
.call( this, config
);
6731 // Mixin constructors
6732 OO
.ui
.PopuppableElement
.call( this, config
);
6736 .addClass( 'oo-ui-popupButtonWidget' )
6737 .append( this.popup
.$element
);
6742 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
6744 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
6749 * Handles mouse click events.
6752 * @param {jQuery.Event} e Mouse click event
6754 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
6755 // Skip clicks within the popup
6756 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
6760 if ( !this.disabled
) {
6761 if ( this.popup
.isVisible() ) {
6766 OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
6771 * Creates an OO.ui.SearchWidget object.
6774 * @extends OO.ui.Widget
6777 * @param {Object} [config] Configuration options
6778 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6779 * @cfg {string} [value] Initial query value
6781 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
6782 // Configuration intialization
6783 config
= config
|| {};
6785 // Parent constructor
6786 OO
.ui
.Widget
.call( this, config
);
6789 this.query
= new OO
.ui
.TextInputWidget( {
6792 'placeholder': config
.placeholder
,
6793 'value': config
.value
6795 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
6796 this.$query
= this.$( '<div>' );
6797 this.$results
= this.$( '<div>' );
6800 this.query
.connect( this, {
6801 'change': 'onQueryChange',
6802 'enter': 'onQueryEnter'
6804 this.results
.connect( this, {
6805 'highlight': 'onResultsHighlight',
6806 'select': 'onResultsSelect'
6808 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
6812 .addClass( 'oo-ui-searchWidget-query' )
6813 .append( this.query
.$element
);
6815 .addClass( 'oo-ui-searchWidget-results' )
6816 .append( this.results
.$element
);
6818 .addClass( 'oo-ui-searchWidget' )
6819 .append( this.$results
, this.$query
);
6824 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
6830 * @param {Object|null} item Item data or null if no item is highlighted
6835 * @param {Object|null} item Item data or null if no item is selected
6841 * Handle query key down events.
6844 * @param {jQuery.Event} e Key down event
6846 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
6847 var highlightedItem
, nextItem
,
6848 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
6851 highlightedItem
= this.results
.getHighlightedItem();
6852 if ( !highlightedItem
) {
6853 highlightedItem
= this.results
.getSelectedItem();
6855 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
6856 this.results
.highlightItem( nextItem
);
6857 nextItem
.scrollElementIntoView();
6862 * Handle select widget select events.
6864 * Clears existing results. Subclasses should repopulate items according to new query.
6867 * @param {string} value New value
6869 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
6871 this.results
.clearItems();
6875 * Handle select widget enter key events.
6877 * Selects highlighted item.
6880 * @param {string} value New value
6882 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
6884 this.results
.selectItem( this.results
.getHighlightedItem() );
6888 * Handle select widget highlight events.
6891 * @param {OO.ui.OptionWidget} item Highlighted item
6894 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
6895 this.emit( 'highlight', item
? item
.getData() : null );
6899 * Handle select widget select events.
6902 * @param {OO.ui.OptionWidget} item Selected item
6905 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
6906 this.emit( 'select', item
? item
.getData() : null );
6910 * Get the query input.
6913 * @returns {OO.ui.TextInputWidget} Query input
6915 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
6920 * Get the results list.
6923 * @returns {OO.ui.SelectWidget} Select list
6925 OO
.ui
.SearchWidget
.prototype.getResults = function () {
6926 return this.results
;
6929 * Creates an OO.ui.TextInputWidget object.
6932 * @extends OO.ui.InputWidget
6935 * @param {Object} [config] Configuration options
6936 * @cfg {string} [placeholder] Placeholder text
6937 * @cfg {string} [icon] Symbolic name of icon
6938 * @cfg {boolean} [multiline=false] Allow multiple lines of text
6940 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
6941 config
= config
|| {};
6943 // Parent constructor
6944 OO
.ui
.InputWidget
.call( this, config
);
6948 this.multiline
= !!config
.multiline
;
6951 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
6954 this.$element
.addClass( 'oo-ui-textInputWidget' );
6955 if ( config
.icon
) {
6956 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
6957 this.$element
.append(
6959 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
6960 .mousedown( OO
.ui
.bind( function () {
6961 this.$input
.focus();
6966 if ( config
.placeholder
) {
6967 this.$input
.attr( 'placeholder', config
.placeholder
);
6973 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
6978 * User presses enter inside the text box.
6980 * Not called if input is multiline.
6988 * Handles key press events.
6990 * @param {jQuery.Event} e Key press event
6991 * @fires enter If enter key is pressed and input is not multiline
6993 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
6994 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
6995 this.emit( 'enter' );
7000 * Get input element.
7003 * @param {Object} [config] Configuration options
7004 * @returns {jQuery} Input element
7006 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
7007 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
7013 * Checks if input is pending.
7016 * @returns {boolean} Input is pending
7018 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
7019 return !!this.pending
;
7023 * Increases the pending stack.
7028 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
7030 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
7031 this.$input
.addClass( 'oo-ui-texture-pending' );
7036 * Reduces the pending stack.
7043 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
7044 this.pending
= Math
.max( 0, this.pending
- 1 );
7045 if ( !this.pending
) {
7046 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
7047 this.$input
.removeClass( 'oo-ui-texture-pending' );
7052 * Creates an OO.ui.TextInputMenuWidget object.
7055 * @extends OO.ui.MenuWidget
7058 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
7059 * @param {Object} [config] Configuration options
7060 * @cfg {jQuery} [$container=input.$element] Element to render menu under
7062 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
7063 // Parent constructor
7064 OO
.ui
.MenuWidget
.call( this, config
);
7068 this.$container
= config
.$container
|| this.input
.$element
;
7069 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
7072 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
7077 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
7082 * Handle window resize event.
7085 * @param {jQuery.Event} e Window resize event
7087 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
7097 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
7099 OO
.ui
.MenuWidget
.prototype.show
.call( this );
7102 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
7112 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
7114 OO
.ui
.MenuWidget
.prototype.hide
.call( this );
7116 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
7121 * Positions the menu.
7126 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
7128 $container
= this.$container
,
7129 dimensions
= $container
.offset();
7131 // Position under input
7132 dimensions
.top
+= $container
.height();
7134 // Compensate for frame position if in a differnt frame
7135 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
7136 frameOffset
= OO
.ui
.Element
.getRelativePosition(
7137 this.input
.$.frame
.$element
, this.$element
.offsetParent()
7139 dimensions
.left
+= frameOffset
.left
;
7140 dimensions
.top
+= frameOffset
.top
;
7142 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
7143 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
7144 dimensions
.right
= this.$element
.parent().position().left
-
7145 dimensions
.width
- dimensions
.left
;
7146 // Erase the value for 'left':
7147 delete dimensions
.left
;
7151 this.$element
.css( dimensions
);
7152 this.setIdealSize( $container
.width() );
7156 * Mixin for widgets with a boolean state.
7162 * @param {Object} [config] Configuration options
7163 * @cfg {boolean} [value=false] Initial value
7165 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7166 // Configuration initialization
7167 config
= config
|| {};
7173 this.$element
.addClass( 'oo-ui-toggleWidget' );
7174 this.setValue( !!config
.value
);
7181 * @param {boolean} value Changed value
7187 * Get the value of the toggle.
7190 * @returns {boolean} Toggle value
7192 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7197 * Set the value of the toggle.
7200 * @param {boolean} value New value
7204 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7206 if ( this.value
!== value
) {
7208 this.emit( 'change', value
);
7209 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7210 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7216 * @extends OO.ui.ButtonWidget
7217 * @mixins OO.ui.ToggleWidget
7220 * @param {Object} [config] Configuration options
7221 * @cfg {boolean} [value=false] Initial value
7223 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
7224 // Configuration initialization
7225 config
= config
|| {};
7227 // Parent constructor
7228 OO
.ui
.ButtonWidget
.call( this, config
);
7230 // Mixin constructors
7231 OO
.ui
.ToggleWidget
.call( this, config
);
7234 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
7239 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
7241 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
7248 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
7249 if ( !this.disabled
) {
7250 this.setValue( !this.value
);
7254 return OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
7260 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
7262 if ( value
!== this.value
) {
7263 this.setActive( value
);
7267 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
7274 * @extends OO.ui.Widget
7275 * @mixins OO.ui.ToggleWidget
7278 * @param {Object} [config] Configuration options
7279 * @cfg {boolean} [value=false] Initial value
7281 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
7282 // Parent constructor
7283 OO
.ui
.Widget
.call( this, config
);
7285 // Mixin constructors
7286 OO
.ui
.ToggleWidget
.call( this, config
);
7289 this.dragging
= false;
7290 this.dragStart
= null;
7291 this.sliding
= false;
7292 this.$on
= this.$( '<span>' );
7293 this.$grip
= this.$( '<span>' );
7296 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
7299 this.$on
.addClass( 'oo-ui-toggleSwitchWidget-on' );
7300 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
7302 .addClass( 'oo-ui-toggleSwitchWidget' )
7303 .append( this.$on
, this.$grip
);
7308 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
7310 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
7315 * Handles mouse down events.
7318 * @param {jQuery.Event} e Mouse down event
7320 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
7321 if ( !this.disabled
&& e
.which
=== 1 ) {
7322 this.setValue( !this.value
);