2 * OOjs UI v0.1.0-pre (8ac38a5c31)
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: Tue Mar 04 2014 11:26:29 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 // Tool tip for a button that removes items from a list
91 'ooui-outline-control-remove': 'Remove item',
92 // Label for the toolbar group that contains a list of all other available tools
93 'ooui-toolbar-more': 'More'
97 * Get a localized message.
99 * In environments that provide a localization system, this function should be overridden to
100 * return the message translated in the user's language. The default implementation always returns
103 * After the message key, message parameters may optionally be passed. In the default implementation,
104 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
105 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
106 * they support unnamed, ordered message parameters.
109 * @param {string} key Message key
110 * @param {Mixed...} [params] Message parameters
111 * @returns {string} Translated message with parameters substituted
113 OO
.ui
.msg = function ( key
) {
114 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
115 if ( typeof message
=== 'string' ) {
116 // Perform $1 substitution
117 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
118 var i
= parseInt( n
, 10 );
119 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
122 // Return placeholder if message not found
123 message
= '[' + key
+ ']';
128 OO
.ui
.deferMsg = function ( key
) {
130 return OO
.ui
.msg( key
);
134 OO
.ui
.resolveMsg = function ( msg
) {
135 if ( $.isFunction( msg
) ) {
143 // Add more as you need
163 * DOM element abstraction.
169 * @param {Object} [config] Configuration options
170 * @cfg {Function} [$] jQuery for the frame the widget is in
171 * @cfg {string[]} [classes] CSS class names
172 * @cfg {jQuery} [$content] Content elements to append
174 OO
.ui
.Element
= function OoUiElement( config
) {
175 // Configuration initialization
176 config
= config
|| {};
179 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
180 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
181 this.elementGroup
= null;
184 if ( $.isArray( config
.classes
) ) {
185 this.$element
.addClass( config
.classes
.join( ' ' ) );
187 if ( config
.$content
) {
188 this.$element
.append( config
.$content
);
192 /* Static Properties */
199 OO
.ui
.Element
.static = {};
204 * This may be ignored if getTagName is overridden.
210 OO
.ui
.Element
.static.tagName
= 'div';
215 * Gets a jQuery function within a specific document.
218 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
219 * @param {OO.ui.Frame} [frame] Frame of the document context
220 * @returns {Function} Bound jQuery function
222 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
223 function wrapper( selector
) {
224 return $( selector
, wrapper
.context
);
227 wrapper
.context
= this.getDocument( context
);
230 wrapper
.frame
= frame
;
237 * Get the document of an element.
240 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
241 * @returns {HTMLDocument} Document object
242 * @throws {Error} If context is invalid
244 OO
.ui
.Element
.getDocument = function ( obj
) {
246 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
247 ( obj
[0] && obj
[0].ownerDocument
) ||
248 // Empty jQuery selections might have a context
255 ( obj
.nodeType
=== 9 && obj
);
261 throw new Error( 'Invalid context' );
265 * Get the window of an element or document.
268 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
269 * @returns {Window} Window object
271 OO
.ui
.Element
.getWindow = function ( obj
) {
272 var doc
= this.getDocument( obj
);
273 return doc
.parentWindow
|| doc
.defaultView
;
277 * Get the direction of an element or document.
280 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
281 * @returns {string} Text direction, either `ltr` or `rtl`
283 OO
.ui
.Element
.getDir = function ( obj
) {
286 if ( obj
instanceof jQuery
) {
289 isDoc
= obj
.nodeType
=== 9;
290 isWin
= obj
.document
!== undefined;
291 if ( isDoc
|| isWin
) {
297 return $( obj
).css( 'direction' );
301 * Get the offset between two frames.
303 * TODO: Make this function not use recursion.
306 * @param {Window} from Window of the child frame
307 * @param {Window} [to=window] Window of the parent frame
308 * @param {Object} [offset] Offset to start with, used internally
309 * @returns {Object} Offset object, containing left and top properties
311 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
312 var i
, len
, frames
, frame
, rect
;
318 offset
= { 'top': 0, 'left': 0 };
320 if ( from.parent
=== from ) {
324 // Get iframe element
325 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
326 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
327 if ( frames
[i
].contentWindow
=== from ) {
333 // Recursively accumulate offset values
335 rect
= frame
.getBoundingClientRect();
336 offset
.left
+= rect
.left
;
337 offset
.top
+= rect
.top
;
339 this.getFrameOffset( from.parent
, offset
);
346 * Get the offset between two elements.
349 * @param {jQuery} $from
350 * @param {jQuery} $to
351 * @returns {Object} Translated position coordinates, containing top and left properties
353 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
354 var from = $from.offset(),
356 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
360 * Get element border sizes.
363 * @param {HTMLElement} el Element to measure
364 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
366 OO
.ui
.Element
.getBorders = function ( el
) {
367 var doc
= el
.ownerDocument
,
368 win
= doc
.parentWindow
|| doc
.defaultView
,
369 style
= win
&& win
.getComputedStyle
?
370 win
.getComputedStyle( el
, null ) :
373 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
374 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
375 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
376 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
379 'top': Math
.round( top
),
380 'left': Math
.round( left
),
381 'bottom': Math
.round( bottom
),
382 'right': Math
.round( right
)
387 * Get dimensions of an element or window.
390 * @param {HTMLElement|Window} el Element to measure
391 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
393 OO
.ui
.Element
.getDimensions = function ( el
) {
395 doc
= el
.ownerDocument
|| el
.document
,
396 win
= doc
.parentWindow
|| doc
.defaultView
;
398 if ( win
=== el
|| el
=== doc
.documentElement
) {
401 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
403 'top': $win
.scrollTop(),
404 'left': $win
.scrollLeft()
406 'scrollbar': { 'right': 0, 'bottom': 0 },
410 'bottom': $win
.innerHeight(),
411 'right': $win
.innerWidth()
417 'borders': this.getBorders( el
),
419 'top': $el
.scrollTop(),
420 'left': $el
.scrollLeft()
423 'right': $el
.innerWidth() - el
.clientWidth
,
424 'bottom': $el
.innerHeight() - el
.clientHeight
426 'rect': el
.getBoundingClientRect()
432 * Get closest scrollable container.
434 * Traverses up until either a scrollable element or the root is reached, in which case the window
438 * @param {HTMLElement} el Element to find scrollable container for
439 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
440 * @return {HTMLElement|Window} Closest scrollable container
442 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
444 props
= [ 'overflow' ],
445 $parent
= $( el
).parent();
447 if ( dimension
=== 'x' || dimension
=== 'y' ) {
448 props
.push( 'overflow-' + dimension
);
451 while ( $parent
.length
) {
452 if ( $parent
[0] === el
.ownerDocument
.body
) {
457 val
= $parent
.css( props
[i
] );
458 if ( val
=== 'auto' || val
=== 'scroll' ) {
462 $parent
= $parent
.parent();
464 return this.getDocument( el
).body
;
468 * Scroll element into view
471 * @param {HTMLElement} el Element to scroll into view
472 * @param {Object} [config={}] Configuration config
473 * @param {string} [config.duration] jQuery animation duration value
474 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
475 * to scroll in both directions
476 * @param {Function} [config.complete] Function to call when scrolling completes
478 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
479 // Configuration initialization
480 config
= config
|| {};
483 callback
= typeof config
.complete
=== 'function' && config
.complete
,
484 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
486 eld
= this.getDimensions( el
),
487 scd
= this.getDimensions( sc
),
489 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
490 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
491 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
492 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
495 if ( !config
.direction
|| config
.direction
=== 'y' ) {
497 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
498 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
499 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
502 if ( !config
.direction
|| config
.direction
=== 'x' ) {
503 if ( rel
.left
< 0 ) {
504 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
505 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
506 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
509 if ( !$.isEmptyObject( anim
) ) {
510 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
512 $sc
.queue( function ( next
) {
527 * Get the HTML tag name.
529 * Override this method to base the result on instance information.
531 * @returns {string} HTML tag name
533 OO
.ui
.Element
.prototype.getTagName = function () {
534 return this.constructor.static.tagName
;
538 * Get the DOM document.
540 * @returns {HTMLDocument} Document object
542 OO
.ui
.Element
.prototype.getElementDocument = function () {
543 return OO
.ui
.Element
.getDocument( this.$element
);
547 * Get the DOM window.
549 * @returns {Window} Window object
551 OO
.ui
.Element
.prototype.getElementWindow = function () {
552 return OO
.ui
.Element
.getWindow( this.$element
);
556 * Get closest scrollable container.
559 * @see #static-method-getClosestScrollableContainer
561 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
562 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
566 * Get group element is in.
568 * @returns {OO.ui.GroupElement|null} Group element, null if none
570 OO
.ui
.Element
.prototype.getElementGroup = function () {
571 return this.elementGroup
;
575 * Set group element is in.
577 * @param {OO.ui.GroupElement|null} group Group element, null if none
580 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
581 this.elementGroup
= group
;
586 * Scroll element into view
589 * @see #static-method-scrollIntoView
590 * @param {Object} [config={}]
592 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
593 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
600 function handler( e
) {
601 jQuery
.event
.simulate( 'focusin', e
.target
, jQuery
.event
.fix( e
), /* bubble = */ true );
606 var doc
= this.ownerDocument
|| this,
607 attaches
= $.data( doc
, 'ooui-focusin-attaches' );
609 doc
.addEventListener( 'focus', handler
, true );
611 $.data( doc
, 'ooui-focusin-attaches', ( attaches
|| 0 ) + 1 );
613 teardown: function () {
614 var doc
= this.ownerDocument
|| this,
615 attaches
= $.data( doc
, 'ooui-focusin-attaches' ) - 1;
617 doc
.removeEventListener( 'focus', handler
, true );
618 $.removeData( doc
, 'ooui-focusin-attaches' );
620 $.data( doc
, 'ooui-focusin-attaches', attaches
);
626 * Bind a handler for an event on the DOM element.
628 * Uses jQuery internally for everything except for events which are
629 * known to have issues in the browser or in jQuery. This method
630 * should become obsolete eventually.
632 * @param {string} event
633 * @param {Function} callback
635 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
638 if ( event
=== 'focusin' ) {
639 // jQuery 1.8.3 has a bug with handling focusin events inside iframes.
640 // Firefox doesn't support focusin at all, so we listen for 'focus' on the
641 // document, and simulate a 'focusin' event on the target element and make
642 // it bubble from there.
644 // - http://jsfiddle.net/sw3hr/
645 // - http://bugs.jquery.com/ticket/14180
646 // - https://github.com/jquery/jquery/commit/1cecf64e5aa4153
648 // Replace jQuery's override with our own
649 orig
= $.event
.special
.focusin
;
650 $.event
.special
.focusin
= specialFocusin
;
652 this.$element
.on( event
, callback
);
655 $.event
.special
.focusin
= orig
;
658 this.$element
.on( event
, callback
);
663 * @param {string} event
664 * @param {Function} callback
666 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
668 if ( event
=== 'focusin' ) {
669 orig
= $.event
.special
.focusin
;
670 $.event
.special
.focusin
= specialFocusin
;
671 this.$element
.off( event
, callback
);
672 $.event
.special
.focusin
= orig
;
674 this.$element
.off( event
, callback
);
679 * Embedded iframe with the same styles as its parent.
682 * @extends OO.ui.Element
683 * @mixins OO.EventEmitter
686 * @param {Object} [config] Configuration options
688 OO
.ui
.Frame
= function OoUiFrame( config
) {
689 // Parent constructor
690 OO
.ui
.Element
.call( this, config
);
692 // Mixin constructors
693 OO
.EventEmitter
.call( this );
696 this.loading
= false;
698 this.config
= config
;
702 .addClass( 'oo-ui-frame' )
703 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
709 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
711 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
713 /* Static Properties */
715 OO
.ui
.Frame
.static.tagName
= 'iframe';
726 * Transplant the CSS styles from as parent document to a frame's document.
728 * This loops over the style sheets in the parent document, and copies their nodes to the
729 * frame's document. It then polls the document to see when all styles have loaded, and once they
730 * have, invokes the callback.
732 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
733 * and invoke the callback anyway. This protects against cases like a display: none; iframe in
734 * Firefox, where the styles won't load until the iframe becomes visible.
736 * For details of how we arrived at the strategy used in this function, see #load.
740 * @param {HTMLDocument} parentDoc Document to transplant styles from
741 * @param {HTMLDocument} frameDoc Document to transplant styles to
742 * @param {Function} [callback] Callback to execute once styles have loaded
743 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
745 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, callback
, timeout
) {
746 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
747 $pollNodes
= $( [] ),
748 // Fake font-family value
749 fontFamily
= 'oo-ui-frame-transplantStyles-loaded';
751 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
752 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
753 if ( callback
&& styleNode
.nodeName
.toLowerCase() === 'link' ) {
754 // External stylesheet
755 // Create a node with a unique ID that we're going to monitor to see when the CSS
757 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
758 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
759 .attr( 'id', pollNodeId
)
760 .appendTo( frameDoc
.body
)
763 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
764 // The font-family rule will only take effect once the @import finishes
765 newNode
= frameDoc
.createElement( 'style' );
766 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
767 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
769 // Not an external stylesheet, or no polling required; just copy the node over
770 newNode
= frameDoc
.importNode( styleNode
, true );
772 frameDoc
.head
.appendChild( newNode
);
776 // Poll every 100ms until all external stylesheets have loaded
777 $pendingPollNodes
= $pollNodes
;
778 timeoutID
= setTimeout( function pollExternalStylesheets() {
780 $pendingPollNodes
.length
> 0 &&
781 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
783 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
786 if ( $pendingPollNodes
.length
=== 0 ) {
788 if ( timeoutID
!== null ) {
794 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
797 // ...but give up after a while
798 if ( timeout
!== 0 ) {
799 setTimeout( function () {
801 clearTimeout( timeoutID
);
806 }, timeout
|| 5000 );
814 * Load the frame contents.
816 * Once the iframe's stylesheets are loaded, the `initialize` event will be emitted.
818 * Sounds simple right? Read on...
820 * When you create a dynamic iframe using open/write/close, the window.load event for the
821 * iframe is triggered when you call close, and there's no further load event to indicate that
822 * everything is actually loaded.
824 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
825 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
826 * are added to document.styleSheets immediately, and the only way you can determine whether they've
827 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
828 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
830 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
831 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
832 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
833 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
834 * .cssRules is allowed.
836 * However, now that we control the styles we're injecting, we might as well do away with
837 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
838 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
839 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
840 * rule is not applied until after the `@import` finishes.
842 * All this stylesheet injection and polling magic is in #transplantStyles.
847 OO
.ui
.Frame
.prototype.load = function () {
848 var win
= this.$element
.prop( 'contentWindow' ),
854 // Figure out directionality:
855 this.dir
= this.$element
.closest( '[dir]' ).prop( 'dir' ) || 'ltr';
857 // Initialize contents
862 '<body class="oo-ui-frame-body oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
863 '<div class="oo-ui-frame-content"></div>' +
870 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
871 this.$content
= this.$( '.oo-ui-frame-content' );
872 this.$document
= this.$( doc
);
874 this.constructor.static.transplantStyles(
875 this.getElementDocument(),
878 frame
.loading
= false;
880 frame
.emit( 'load' );
886 * Run a callback as soon as the frame has been loaded.
889 * This will start loading if it hasn't already, and runs
890 * immediately if the frame is already loaded.
892 * Don't call this until the element is attached.
894 * @param {Function} callback
896 OO
.ui
.Frame
.prototype.run = function ( callback
) {
900 if ( !this.loading
) {
903 this.once( 'load', callback
);
908 * Sets the size of the frame.
910 * @param {number} width Frame width in pixels
911 * @param {number} height Frame height in pixels
914 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
915 this.$element
.css( { 'width': width
, 'height': height
} );
919 * Container for elements in a child frame.
921 * There are two ways to specify a title: set the static `title` property or provide a `title`
922 * property in the configuration options. The latter will override the former.
926 * @extends OO.ui.Element
927 * @mixins OO.EventEmitter
930 * @param {Object} [config] Configuration options
931 * @cfg {string|Function} [title] Title string or function that returns a string
932 * @cfg {string} [icon] Symbolic name of icon
935 OO
.ui
.Window
= function OoUiWindow( config
) {
936 // Parent constructor
937 OO
.ui
.Element
.call( this, config
);
939 // Mixin constructors
940 OO
.EventEmitter
.call( this );
943 this.visible
= false;
944 this.opening
= false;
945 this.closing
= false;
946 this.title
= OO
.ui
.resolveMsg( config
.title
|| this.constructor.static.title
);
947 this.icon
= config
.icon
|| this.constructor.static.icon
;
948 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
949 this.$frame
= this.$( '<div>' );
950 this.$ = function () {
951 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
956 .addClass( 'oo-ui-window' )
957 // Hide the window using visibility: hidden; while the iframe is still loading
958 // Can't use display: none; because that prevents the iframe from loading in Firefox
959 .css( 'visibility', 'hidden' )
960 .append( this.$frame
);
962 .addClass( 'oo-ui-window-frame' )
963 .append( this.frame
.$element
);
966 this.frame
.connect( this, { 'load': 'initialize' } );
971 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
973 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
978 * Initialize contents.
980 * Fired asynchronously after construction when iframe is ready.
988 * Fired after window has been opened.
991 * @param {Object} data Window opening data
997 * Fired after window has been closed.
1000 * @param {Object} data Window closing data
1003 /* Static Properties */
1006 * Symbolic name of icon.
1010 * @property {string}
1012 OO
.ui
.Window
.static.icon
= 'window';
1017 * Subclasses must implement this property before instantiating the window.
1018 * Alternatively, override #getTitle with an alternative implementation.
1023 * @property {string|Function} Title string or function that returns a string
1025 OO
.ui
.Window
.static.title
= null;
1030 * Check if window is visible.
1033 * @returns {boolean} Window is visible
1035 OO
.ui
.Window
.prototype.isVisible = function () {
1036 return this.visible
;
1040 * Check if window is opening.
1043 * @returns {boolean} Window is opening
1045 OO
.ui
.Window
.prototype.isOpening = function () {
1046 return this.opening
;
1050 * Check if window is closing.
1053 * @returns {boolean} Window is closing
1055 OO
.ui
.Window
.prototype.isClosing = function () {
1056 return this.closing
;
1060 * Get the window frame.
1063 * @returns {OO.ui.Frame} Frame of window
1065 OO
.ui
.Window
.prototype.getFrame = function () {
1070 * Get the title of the window.
1072 * @returns {string} Title text
1074 OO
.ui
.Window
.prototype.getTitle = function () {
1079 * Get the window icon.
1081 * @returns {string} Symbolic name of icon
1083 OO
.ui
.Window
.prototype.getIcon = function () {
1088 * Set the size of window frame.
1090 * @param {number} [width=auto] Custom width
1091 * @param {number} [height=auto] Custom height
1094 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1095 if ( !this.frame
.$content
) {
1099 this.frame
.$element
.css( {
1100 'width': width
=== undefined ? 'auto' : width
,
1101 'height': height
=== undefined ? 'auto' : height
1108 * Set the title of the window.
1110 * @param {string|Function} title Title text or a function that returns text
1113 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1114 this.title
= OO
.ui
.resolveMsg( title
);
1115 if ( this.$title
) {
1116 this.$title
.text( title
);
1122 * Set the icon of the window.
1124 * @param {string} icon Symbolic name of icon
1127 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1129 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1133 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1140 * Set the position of window to fit with contents..
1142 * @param {string} left Left offset
1143 * @param {string} top Top offset
1146 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1147 this.$element
.css( { 'left': left
, 'top': top
} );
1152 * Set the height of window to fit with contents.
1154 * @param {number} [min=0] Min height
1155 * @param {number} [max] Max height (defaults to content's outer height)
1158 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1159 var height
= this.frame
.$content
.outerHeight();
1161 this.frame
.$element
.css(
1162 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1169 * Set the width of window to fit with contents.
1171 * @param {number} [min=0] Min height
1172 * @param {number} [max] Max height (defaults to content's outer width)
1175 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1176 var width
= this.frame
.$content
.outerWidth();
1178 this.frame
.$element
.css(
1179 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1186 * Initialize window contents.
1188 * The first time the window is opened, #initialize is called when it's safe to begin populating
1189 * its contents. See #setup for a way to make changes each time the window opens.
1191 * Once this method is called, this.$$ can be used to create elements within the frame.
1197 OO
.ui
.Window
.prototype.initialize = function () {
1199 this.$ = this.frame
.$;
1200 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1201 .text( this.title
);
1202 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1203 .addClass( 'oo-ui-icon-' + this.icon
);
1204 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1205 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1206 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1207 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1210 this.frame
.$content
.append(
1211 this.$head
.append( this.$icon
, this.$title
),
1217 // Undo the visibility: hidden; hack from the constructor and apply display: none;
1218 // We can do this safely now that the iframe has initialized
1219 this.$element
.hide().css( 'visibility', '' );
1221 this.emit( 'initialize' );
1227 * Setup window for use.
1229 * Each time the window is opened, once it's ready to be interacted with, this will set it up for
1230 * use in a particular context, based on the `data` argument.
1232 * When you override this method, you must call the parent method at the very beginning.
1236 * @param {Object} [data] Window opening data
1238 OO
.ui
.Window
.prototype.setup = function () {
1239 // Override to do something
1243 * Tear down window after use.
1245 * Each time the window is closed, and it's done being interacted with, this will tear it down and
1246 * do something with the user's interactions within the window, based on the `data` argument.
1248 * When you override this method, you must call the parent method at the very end.
1252 * @param {Object} [data] Window closing data
1254 OO
.ui
.Window
.prototype.teardown = function () {
1255 // Override to do something
1261 * Do not override this method. See #setup for a way to make changes each time the window opens.
1264 * @param {Object} [data] Window opening data
1268 OO
.ui
.Window
.prototype.open = function ( data
) {
1269 if ( !this.opening
&& !this.closing
&& !this.visible
) {
1270 this.opening
= true;
1271 this.frame
.run( OO
.ui
.bind( function () {
1272 this.$element
.show();
1273 this.visible
= true;
1274 this.frame
.$element
.focus();
1275 this.emit( 'opening', data
);
1277 this.emit( 'open', data
);
1278 this.opening
= false;
1288 * See #teardown for a way to do something each time the window closes.
1291 * @param {Object} [data] Window closing data
1295 OO
.ui
.Window
.prototype.close = function ( data
) {
1296 if ( !this.opening
&& !this.closing
&& this.visible
) {
1297 this.frame
.$content
.find( ':focus' ).blur();
1298 this.closing
= true;
1299 this.$element
.hide();
1300 this.visible
= false;
1301 this.emit( 'closing', data
);
1302 this.teardown( data
);
1303 this.emit( 'close', data
);
1304 this.closing
= false;
1310 * Set of mutually exclusive windows.
1313 * @extends OO.ui.Element
1314 * @mixins OO.EventEmitter
1317 * @param {OO.Factory} factory Window factory
1318 * @param {Object} [config] Configuration options
1320 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1321 // Parent constructor
1322 OO
.ui
.Element
.call( this, config
);
1324 // Mixin constructors
1325 OO
.EventEmitter
.call( this );
1328 this.factory
= factory
;
1331 * List of all windows associated with this window set
1332 * @property {OO.ui.Window[]}
1334 this.windowList
= [];
1337 * Mapping of OO.ui.Window objects created by name from the #factory.
1338 * @property {Object}
1341 this.currentWindow
= null;
1344 this.$element
.addClass( 'oo-ui-windowSet' );
1349 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1351 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1357 * @param {OO.ui.Window} win Window that's being opened
1358 * @param {Object} config Window opening information
1363 * @param {OO.ui.Window} win Window that's been opened
1364 * @param {Object} config Window opening information
1369 * @param {OO.ui.Window} win Window that's being closed
1370 * @param {Object} config Window closing information
1375 * @param {OO.ui.Window} win Window that's been closed
1376 * @param {Object} config Window closing information
1382 * Handle a window that's being opened.
1385 * @param {OO.ui.Window} win Window that's being opened
1386 * @param {Object} [config] Window opening information
1389 OO
.ui
.WindowSet
.prototype.onWindowOpening = function ( win
, config
) {
1390 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1391 this.currentWindow
.close();
1393 this.currentWindow
= win
;
1394 this.emit( 'opening', win
, config
);
1398 * Handle a window that's been opened.
1401 * @param {OO.ui.Window} win Window that's been opened
1402 * @param {Object} [config] Window opening information
1405 OO
.ui
.WindowSet
.prototype.onWindowOpen = function ( win
, config
) {
1406 this.emit( 'open', win
, config
);
1410 * Handle a window that's being closed.
1413 * @param {OO.ui.Window} win Window that's being closed
1414 * @param {Object} [config] Window closing information
1417 OO
.ui
.WindowSet
.prototype.onWindowClosing = function ( win
, config
) {
1418 this.currentWindow
= null;
1419 this.emit( 'closing', win
, config
);
1423 * Handle a window that's been closed.
1426 * @param {OO.ui.Window} win Window that's been closed
1427 * @param {Object} [config] Window closing information
1430 OO
.ui
.WindowSet
.prototype.onWindowClose = function ( win
, config
) {
1431 this.emit( 'close', win
, config
);
1435 * Get the current window.
1438 * @returns {OO.ui.Window} Current window
1440 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1441 return this.currentWindow
;
1445 * Return a given window.
1447 * @param {string} name Symbolic name of window
1448 * @return {OO.ui.Window} Window with specified name
1450 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1453 if ( !this.factory
.lookup( name
) ) {
1454 throw new Error( 'Unknown window: ' + name
);
1456 if ( !( name
in this.windows
) ) {
1457 win
= this.windows
[name
] = this.factory
.create( name
, this, { '$': this.$ } );
1458 this.addWindow( win
);
1460 return this.windows
[name
];
1464 * Add a given window to this window set.
1466 * Connects event handlers and attaches it to the DOM. Calling
1467 * OO.ui.Window#open will not work until the window is added to the set.
1469 * @param {OO.ui.Window} win
1471 OO
.ui
.WindowSet
.prototype.addWindow = function ( win
) {
1472 if ( this.windowList
.indexOf( win
) !== -1 ) {
1476 this.windowList
.push( win
);
1478 win
.connect( this, {
1479 'opening': [ 'onWindowOpening', win
],
1480 'open': [ 'onWindowOpen', win
],
1481 'closing': [ 'onWindowClosing', win
],
1482 'close': [ 'onWindowClose', win
]
1484 this.$element
.append( win
.$element
);
1491 * @extends OO.ui.Window
1494 * @param {Object} [config] Configuration options
1495 * @cfg {boolean} [footless] Hide foot
1496 * @cfg {boolean} [small] Make the dialog small
1498 OO
.ui
.Dialog
= function OoUiDialog( config
) {
1499 // Configuration initialization
1500 config
= config
|| {};
1502 // Parent constructor
1503 OO
.ui
.Window
.call( this, config
);
1506 this.visible
= false;
1507 this.footless
= !!config
.footless
;
1508 this.small
= !!config
.small
;
1509 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1510 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1513 this.$element
.on( 'mousedown', false );
1514 this.connect( this, { 'opening': 'onOpening' } );
1517 this.$element
.addClass( 'oo-ui-dialog' );
1522 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1524 /* Static Properties */
1527 * Symbolic name of dialog.
1531 * @property {string}
1534 OO
.ui
.Dialog
.static.name
= '';
1539 * Handle close button click events.
1543 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1544 this.close( { 'action': 'cancel' } );
1548 * Handle window mouse wheel events.
1551 * @param {jQuery.Event} e Mouse wheel event
1553 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1558 * Handle document key down events.
1561 * @param {jQuery.Event} e Key down event
1563 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1564 switch ( e
.which
) {
1565 case OO
.ui
.Keys
.PAGEUP
:
1566 case OO
.ui
.Keys
.PAGEDOWN
:
1567 case OO
.ui
.Keys
.END
:
1568 case OO
.ui
.Keys
.HOME
:
1569 case OO
.ui
.Keys
.LEFT
:
1571 case OO
.ui
.Keys
.RIGHT
:
1572 case OO
.ui
.Keys
.DOWN
:
1573 // Prevent any key events that might cause scrolling
1579 * Handle frame document key down events.
1582 * @param {jQuery.Event} e Key down event
1584 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1585 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1586 this.close( { 'action': 'cancel' } );
1592 OO
.ui
.Dialog
.prototype.onOpening = function () {
1593 this.$element
.addClass( 'oo-ui-dialog-open' );
1599 OO
.ui
.Dialog
.prototype.initialize = function () {
1601 OO
.ui
.Window
.prototype.initialize
.call( this );
1604 this.closeButton
= new OO
.ui
.ButtonWidget( {
1608 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1612 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1613 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1616 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1617 if ( this.footless
) {
1618 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1621 this.$frame
.addClass( 'oo-ui-window-frame-small' );
1623 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1624 this.$head
.append( this.closeButton
.$element
);
1630 OO
.ui
.Dialog
.prototype.setup = function ( data
) {
1632 OO
.ui
.Window
.prototype.setup
.call( this, data
);
1634 // Prevent scrolling in top-level window
1635 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1636 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1642 OO
.ui
.Dialog
.prototype.teardown = function ( data
) {
1644 OO
.ui
.Window
.prototype.teardown
.call( this, data
);
1646 // Allow scrolling in top-level window
1647 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1648 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1654 OO
.ui
.Dialog
.prototype.close = function ( data
) {
1656 if ( !dialog
.opening
&& !dialog
.closing
&& dialog
.visible
) {
1657 // Trigger transition
1658 dialog
.$element
.removeClass( 'oo-ui-dialog-open' );
1659 // Allow transition to complete before actually closing
1660 setTimeout( function () {
1662 OO
.ui
.Window
.prototype.close
.call( dialog
, data
);
1667 * Container for elements.
1671 * @extends OO.ui.Element
1672 * @mixins OO.EventEmitter
1675 * @param {Object} [config] Configuration options
1677 OO
.ui
.Layout
= function OoUiLayout( config
) {
1678 // Initialize config
1679 config
= config
|| {};
1681 // Parent constructor
1682 OO
.ui
.Element
.call( this, config
);
1684 // Mixin constructors
1685 OO
.EventEmitter
.call( this );
1688 this.$element
.addClass( 'oo-ui-layout' );
1693 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1695 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1697 * User interface control.
1701 * @extends OO.ui.Element
1702 * @mixins OO.EventEmitter
1705 * @param {Object} [config] Configuration options
1706 * @cfg {boolean} [disabled=false] Disable
1708 OO
.ui
.Widget
= function OoUiWidget( config
) {
1709 // Initialize config
1710 config
= $.extend( { 'disabled': false }, config
);
1712 // Parent constructor
1713 OO
.ui
.Element
.call( this, config
);
1715 // Mixin constructors
1716 OO
.EventEmitter
.call( this );
1719 this.disabled
= null;
1720 this.wasDisabled
= null;
1723 this.$element
.addClass( 'oo-ui-widget' );
1724 this.setDisabled( !!config
.disabled
);
1729 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1731 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1737 * @param {boolean} disabled Widget is disabled
1743 * Check if the widget is disabled.
1746 * @param {boolean} Button is disabled
1748 OO
.ui
.Widget
.prototype.isDisabled = function () {
1749 return this.disabled
;
1753 * Update the disabled state, in case of changes in parent widget.
1758 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1759 this.setDisabled( this.disabled
);
1764 * Set the disabled state of the widget.
1766 * This should probably change the widgets's appearance and prevent it from being used.
1769 * @param {boolean} disabled Disable widget
1772 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1775 this.disabled
= !!disabled
;
1776 isDisabled
= this.isDisabled();
1777 if ( isDisabled
!== this.wasDisabled
) {
1778 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1779 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1780 this.emit( 'disable', isDisabled
);
1782 this.wasDisabled
= isDisabled
;
1786 * Element with a button.
1792 * @param {jQuery} $button Button node, assigned to #$button
1793 * @param {Object} [config] Configuration options
1794 * @cfg {boolean} [frameless] Render button without a frame
1795 * @cfg {number} [tabIndex=0] Button's tab index, use -1 to prevent tab focusing
1797 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
1798 // Configuration initialization
1799 config
= config
|| {};
1802 this.$button
= $button
;
1803 this.tabIndex
= null;
1804 this.active
= false;
1805 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
1808 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
1811 this.$element
.addClass( 'oo-ui-buttonedElement' );
1813 .addClass( 'oo-ui-buttonedElement-button' )
1814 .attr( 'role', 'button' )
1815 .prop( 'tabIndex', config
.tabIndex
|| 0 );
1816 if ( config
.frameless
) {
1817 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
1819 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
1826 * Handles mouse down events.
1829 * @param {jQuery.Event} e Mouse down event
1831 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function () {
1832 this.tabIndex
= this.$button
.attr( 'tabIndex' );
1833 // Remove the tab-index while the button is down to prevent the button from stealing focus
1835 .removeAttr( 'tabIndex' )
1836 .addClass( 'oo-ui-buttonedElement-pressed' );
1837 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
1841 * Handles mouse up events.
1844 * @param {jQuery.Event} e Mouse up event
1846 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function () {
1847 // Restore the tab-index after the button is up to restore the button's accesssibility
1849 .attr( 'tabIndex', this.tabIndex
)
1850 .removeClass( 'oo-ui-buttonedElement-pressed' );
1851 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
1858 * @param {boolean} [value] Make button active
1861 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
1862 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
1866 * Element that can be automatically clipped to visible boundaies.
1872 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
1873 * @param {Object} [config] Configuration options
1875 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
1876 // Configuration initialization
1877 config
= config
|| {};
1880 this.$clippable
= $clippable
;
1881 this.clipping
= false;
1882 this.clipped
= false;
1883 this.$clippableContainer
= null;
1884 this.$clippableScroller
= null;
1885 this.$clippableWindow
= null;
1886 this.idealWidth
= null;
1887 this.idealHeight
= null;
1888 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
1889 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
1892 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
1901 * @param {boolean} value Enable clipping
1904 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
1907 if ( this.clipping
!== value
) {
1908 this.clipping
= value
;
1909 if ( this.clipping
) {
1910 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
1911 // If the clippable container is the body, we have to listen to scroll events and check
1912 // jQuery.scrollTop on the window because of browser inconsistencies
1913 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
1914 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
1915 this.$clippableContainer
;
1916 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
1917 this.$clippableWindow
= this.$( this.getElementWindow() )
1918 .on( 'resize', this.onClippableWindowResizeHandler
);
1919 // Initial clip after visible
1920 setTimeout( OO
.ui
.bind( this.clip
, this ) );
1922 this.$clippableContainer
= null;
1923 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
1924 this.$clippableScroller
= null;
1925 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
1926 this.$clippableWindow
= null;
1934 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
1937 * @return {boolean} Element will be clipped to the visible area
1939 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
1940 return this.clipping
;
1944 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
1947 * @return {boolean} Part of the element is being clipped
1949 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
1950 return this.clipped
;
1954 * Set the ideal size.
1957 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
1958 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
1960 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
1961 this.idealWidth
= width
;
1962 this.idealHeight
= height
;
1966 * Clip element to visible boundaries and allow scrolling when needed.
1968 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
1969 * overlapped by, the visible area of the nearest scrollable container.
1974 OO
.ui
.ClippableElement
.prototype.clip = function () {
1975 if ( !this.clipping
) {
1976 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
1981 cOffset
= this.$clippable
.offset(),
1982 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
1983 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
1984 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
1985 scrollTop
= this.$clippableScroller
.scrollTop(),
1986 scrollLeft
= this.$clippableScroller
.scrollLeft(),
1987 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
1988 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
1989 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
1990 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
1991 clipWidth
= desiredWidth
< naturalWidth
,
1992 clipHeight
= desiredHeight
< naturalHeight
;
1995 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
1997 this.$clippable
.css( { 'overflow-x': '', 'width': this.idealWidth
|| '' } );
2000 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
2002 this.$clippable
.css( { 'overflow-y': '', 'height': this.idealHeight
|| '' } );
2005 this.clipped
= clipWidth
|| clipHeight
;
2010 * Element with named flags, used for styling, that can be added, removed and listed and checked.
2016 * @param {Object} [config] Configuration options
2017 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
2019 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
2020 // Config initialization
2021 config
= config
|| {};
2027 this.setFlags( config
.flags
);
2033 * Check if a flag is set.
2036 * @param {string} flag Flag name to check
2037 * @returns {boolean} Has flag
2039 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
2040 return flag
in this.flags
;
2044 * Get the names of all flags.
2047 * @returns {string[]} flags Flag names
2049 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
2050 return Object
.keys( this.flags
);
2054 * Add one or more flags.
2057 * @param {string[]|Object.<string, boolean>} flags List of flags to add, or list of set/remove
2058 * values, keyed by flag name
2061 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
2063 classPrefix
= 'oo-ui-flaggableElement-';
2065 if ( $.isArray( flags
) ) {
2066 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
2069 this.flags
[flag
] = true;
2070 this.$element
.addClass( classPrefix
+ flag
);
2072 } else if ( OO
.isPlainObject( flags
) ) {
2073 for ( flag
in flags
) {
2074 if ( flags
[flag
] ) {
2076 this.flags
[flag
] = true;
2077 this.$element
.addClass( classPrefix
+ flag
);
2080 delete this.flags
[flag
];
2081 this.$element
.removeClass( classPrefix
+ flag
);
2088 * Element containing a sequence of child elements.
2094 * @param {jQuery} $group Container node, assigned to #$group
2095 * @param {Object} [config] Configuration options
2096 * @cfg {Object.<string,string>} [aggregations] Events to aggregate, keyed by item event name
2098 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2100 config
= config
|| {};
2103 this.$group
= $group
;
2105 this.$items
= this.$( [] );
2106 this.aggregate
= !$.isEmptyObject( config
.aggregations
);
2107 this.aggregations
= config
.aggregations
|| {};
2116 * @returns {OO.ui.Element[]} Items
2118 OO
.ui
.GroupElement
.prototype.getItems = function () {
2119 return this.items
.slice( 0 );
2126 * @param {OO.ui.Element[]} items Item
2127 * @param {number} [index] Index to insert items at
2130 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2131 var i
, len
, item
, event
, events
, currentIndex
,
2132 $items
= this.$( [] );
2134 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2137 // Check if item exists then remove it first, effectively "moving" it
2138 currentIndex
= $.inArray( item
, this.items
);
2139 if ( currentIndex
>= 0 ) {
2140 this.removeItems( [ item
] );
2141 // Adjust index to compensate for removal
2142 if ( currentIndex
< index
) {
2147 if ( this.aggregate
) {
2149 for ( event
in this.aggregations
) {
2150 events
[event
] = [ 'emit', this.aggregations
[event
], item
];
2152 item
.connect( this, events
);
2154 item
.setElementGroup( this );
2155 $items
= $items
.add( item
.$element
);
2158 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2159 this.$group
.append( $items
);
2160 this.items
.push
.apply( this.items
, items
);
2161 } else if ( index
=== 0 ) {
2162 this.$group
.prepend( $items
);
2163 this.items
.unshift
.apply( this.items
, items
);
2165 this.$items
.eq( index
).before( $items
);
2166 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2169 this.$items
= this.$items
.add( $items
);
2177 * Items will be detached, not removed, so they can be used later.
2180 * @param {OO.ui.Element[]} items Items to remove
2183 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2184 var i
, len
, item
, index
;
2186 // Remove specific items
2187 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2189 index
= $.inArray( item
, this.items
);
2190 if ( index
!== -1 ) {
2191 if ( this.aggregate
) {
2192 item
.disconnect( this );
2194 item
.setElementGroup( null );
2195 this.items
.splice( index
, 1 );
2196 item
.$element
.detach();
2197 this.$items
= this.$items
.not( item
.$element
);
2207 * Items will be detached, not removed, so they can be used later.
2212 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2216 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2217 item
= this.items
[i
];
2218 if ( this.aggregate
) {
2219 item
.disconnect( this );
2221 item
.setElementGroup( null );
2224 this.$items
.detach();
2225 this.$items
= this.$( [] );
2228 * Element containing an icon.
2234 * @param {jQuery} $icon Icon node, assigned to #$icon
2235 * @param {Object} [config] Configuration options
2236 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2237 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2240 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2241 // Config intialization
2242 config
= config
|| {};
2249 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2250 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2253 /* Static Properties */
2255 OO
.ui
.IconedElement
.static = {};
2260 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2262 * For i18n purposes, this property can be an object containing a `default` icon name property and
2263 * additional icon names keyed by language code.
2265 * Example of i18n icon definition:
2266 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2270 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2271 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2274 OO
.ui
.IconedElement
.static.icon
= null;
2282 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
2283 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2287 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2288 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2291 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2293 if ( typeof icon
=== 'string' ) {
2295 if ( icon
.length
) {
2296 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2300 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2309 * @returns {string} Icon
2311 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2315 * Element containing an indicator.
2321 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2322 * @param {Object} [config] Configuration options
2323 * @cfg {string} [indicator] Symbolic indicator name
2324 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2326 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2327 // Config intialization
2328 config
= config
|| {};
2331 this.$indicator
= $indicator
;
2332 this.indicator
= null;
2333 this.indicatorLabel
= null;
2336 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2337 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2338 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2341 /* Static Properties */
2343 OO
.ui
.IndicatedElement
.static = {};
2350 * @property {string|null} Symbolic indicator name or null for no indicator
2352 OO
.ui
.IndicatedElement
.static.indicator
= null;
2359 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2362 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2370 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2373 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2374 if ( this.indicator
) {
2375 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2376 this.indicator
= null;
2378 if ( typeof indicator
=== 'string' ) {
2379 indicator
= indicator
.trim();
2380 if ( indicator
.length
) {
2381 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2382 this.indicator
= indicator
;
2385 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2391 * Set indicator label.
2394 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2395 * for no indicator title
2398 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2399 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2401 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2402 this.$indicator
.attr( 'title', indicatorTitle
);
2404 this.$indicator
.removeAttr( 'title' );
2414 * @returns {string} title Symbolic name of indicator
2416 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2417 return this.indicator
;
2421 * Get indicator title.
2424 * @returns {string} Indicator title text
2426 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2427 return this.indicatorTitle
;
2430 * Element containing a label.
2436 * @param {jQuery} $label Label node, assigned to #$label
2437 * @param {Object} [config] Configuration options
2438 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2440 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2441 // Config intialization
2442 config
= config
|| {};
2445 this.$label
= $label
;
2449 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2450 this.setLabel( config
.label
|| this.constructor.static.label
);
2453 /* Static Properties */
2455 OO
.ui
.LabeledElement
.static = {};
2462 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2465 OO
.ui
.LabeledElement
.static.label
= null;
2472 * An empty string will result in the label being hidden. A string containing only whitespace will
2473 * be converted to a single
2476 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2477 * text; or null for no label
2480 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2483 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2484 if ( typeof label
=== 'string' && label
.length
) {
2485 if ( label
.match( /^\s*$/ ) ) {
2486 // Convert whitespace only string to a single non-breaking space
2487 this.$label
.html( ' ' );
2489 this.$label
.text( label
);
2491 } else if ( label
instanceof jQuery
) {
2492 this.$label
.empty().append( label
);
2494 this.$label
.empty();
2497 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2498 this.$label
.css( 'display', empty
? 'none' : '' );
2507 * @returns {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2508 * text; or null for no label
2510 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2520 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
2521 if ( this.$label
.autoEllipsis
) {
2522 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
2527 * Popuppable element.
2533 * @param {Object} [config] Configuration options
2534 * @cfg {number} [popupWidth=320] Width of popup
2535 * @cfg {number} [popupHeight] Height of popup
2536 * @cfg {Object} [popup] Configuration to pass to popup
2538 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
2539 // Configuration initialization
2540 config
= $.extend( { 'popupWidth': 320 }, config
);
2543 this.popup
= new OO
.ui
.PopupWidget( $.extend(
2544 { 'align': 'center', 'autoClose': true },
2546 { '$': this.$, '$autoCloseIgnore': this.$element
}
2548 this.popupWidth
= config
.popupWidth
;
2549 this.popupHeight
= config
.popupHeight
;
2558 * @returns {OO.ui.PopupWidget} Popup widget
2560 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
2569 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
2570 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
2578 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
2582 * Element with a title.
2588 * @param {jQuery} $label Titled node, assigned to #$titled
2589 * @param {Object} [config] Configuration options
2590 * @cfg {string|Function} [title] Title text or a function that returns text
2592 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
2593 // Config intialization
2594 config
= config
|| {};
2597 this.$titled
= $titled
;
2601 this.setTitle( config
.title
|| this.constructor.static.title
);
2604 /* Static Properties */
2606 OO
.ui
.TitledElement
.static = {};
2613 * @property {string|Function} Title text or a function that returns text
2615 OO
.ui
.TitledElement
.static.title
= null;
2623 * @param {string|Function|null} title Title text, a function that returns text or null for no title
2626 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
2627 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
2629 if ( typeof title
=== 'string' && title
.length
) {
2630 this.$titled
.attr( 'title', title
);
2632 this.$titled
.removeAttr( 'title' );
2642 * @returns {string} Title string
2644 OO
.ui
.TitledElement
.prototype.getTitle = function () {
2648 * Generic toolbar tool.
2652 * @extends OO.ui.Widget
2653 * @mixins OO.ui.IconedElement
2656 * @param {OO.ui.ToolGroup} toolGroup
2657 * @param {Object} [config] Configuration options
2658 * @cfg {string|Function} [title] Title text or a function that returns text
2660 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
2661 // Config intialization
2662 config
= config
|| {};
2664 // Parent constructor
2665 OO
.ui
.Widget
.call( this, config
);
2667 // Mixin constructors
2668 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
2671 this.toolGroup
= toolGroup
;
2672 this.toolbar
= this.toolGroup
.getToolbar();
2673 this.active
= false;
2674 this.$title
= this.$( '<span>' );
2675 this.$link
= this.$( '<a>' );
2679 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
2682 this.$title
.addClass( 'oo-ui-tool-title' );
2684 .addClass( 'oo-ui-tool-link' )
2685 .append( this.$icon
, this.$title
);
2687 .data( 'oo-ui-tool', this )
2689 'oo-ui-tool ' + 'oo-ui-tool-name-' +
2690 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
2692 .append( this.$link
);
2693 this.setTitle( config
.title
|| this.constructor.static.title
);
2698 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
2700 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
2708 /* Static Properties */
2710 OO
.ui
.Tool
.static.tagName
= 'span';
2713 * Symbolic name of tool.
2717 * @property {string}
2720 OO
.ui
.Tool
.static.name
= '';
2727 * @property {string}
2730 OO
.ui
.Tool
.static.group
= '';
2735 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
2736 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
2737 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
2738 * appended to the title if the tool is part of a bar tool group.
2742 * @property {string|Function} Title text or a function that returns text
2745 OO
.ui
.Tool
.static.title
= '';
2748 * Tool can be automatically added to tool groups.
2751 * @property {boolean}
2754 OO
.ui
.Tool
.static.autoAdd
= true;
2757 * Check if this tool is compatible with given data.
2762 * @param {Mixed} data Data to check
2763 * @returns {boolean} Tool can be used with data
2765 OO
.ui
.Tool
.static.isCompatibleWith = function () {
2772 * Handle the toolbar state being updated.
2774 * This is an abstract method that must be overridden in a concrete subclass.
2779 OO
.ui
.Tool
.prototype.onUpdateState = function () {
2781 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
2786 * Handle the tool being selected.
2788 * This is an abstract method that must be overridden in a concrete subclass.
2793 OO
.ui
.Tool
.prototype.onSelect = function () {
2795 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
2800 * Check if the button is active.
2803 * @param {boolean} Button is active
2805 OO
.ui
.Tool
.prototype.isActive = function () {
2810 * Make the button appear active or inactive.
2813 * @param {boolean} state Make button appear active
2815 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
2816 this.active
= !!state
;
2817 if ( this.active
) {
2818 this.$element
.addClass( 'oo-ui-tool-active' );
2820 this.$element
.removeClass( 'oo-ui-tool-active' );
2825 * Get the tool title.
2828 * @param {string|Function} title Title text or a function that returns text
2831 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
2832 this.title
= OO
.ui
.resolveMsg( title
);
2838 * Get the tool title.
2841 * @returns {string} Title text
2843 OO
.ui
.Tool
.prototype.getTitle = function () {
2848 * Get the tool's symbolic name.
2851 * @returns {string} Symbolic name of tool
2853 OO
.ui
.Tool
.prototype.getName = function () {
2854 return this.constructor.static.name
;
2862 OO
.ui
.Tool
.prototype.updateTitle = function () {
2863 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
2864 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
2865 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
2872 .addClass( 'oo-ui-tool-accel' )
2876 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
2877 tooltipParts
.push( this.title
);
2879 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
2880 tooltipParts
.push( accel
);
2882 if ( tooltipParts
.length
) {
2883 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
2885 this.$link
.removeAttr( 'title' );
2894 OO
.ui
.Tool
.prototype.destroy = function () {
2895 this.toolbar
.disconnect( this );
2896 this.$element
.remove();
2899 * Collection of tool groups.
2902 * @extends OO.ui.Element
2903 * @mixins OO.EventEmitter
2904 * @mixins OO.ui.GroupElement
2907 * @param {OO.Factory} toolFactory Factory for creating tools
2908 * @param {Object} [config] Configuration options
2909 * @cfg {boolean} [actions] Add an actions section opposite to the tools
2910 * @cfg {boolean} [shadow] Add a shadow below the toolbar
2912 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, config
) {
2913 // Configuration initialization
2914 config
= config
|| {};
2916 // Parent constructor
2917 OO
.ui
.Element
.call( this, config
);
2919 // Mixin constructors
2920 OO
.EventEmitter
.call( this );
2921 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
2924 this.toolFactory
= toolFactory
;
2927 this.$bar
= this.$( '<div>' );
2928 this.$actions
= this.$( '<div>' );
2929 this.initialized
= false;
2933 .add( this.$bar
).add( this.$group
).add( this.$actions
)
2934 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2937 this.$group
.addClass( 'oo-ui-toolbar-tools' );
2938 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
2939 if ( config
.actions
) {
2940 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
2941 this.$bar
.append( this.$actions
);
2943 this.$bar
.append( '<div style="clear:both"></div>' );
2944 if ( config
.shadow
) {
2945 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
2947 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
2952 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
2954 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
2955 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
2960 * Get the tool factory.
2963 * @returns {OO.Factory} Tool factory
2965 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
2966 return this.toolFactory
;
2970 * Handles mouse down events.
2973 * @param {jQuery.Event} e Mouse down event
2975 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
2976 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
2977 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
2978 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
2984 * Sets up handles and preloads required information for the toolbar to work.
2985 * This must be called immediately after it is attached to a visible document.
2987 OO
.ui
.Toolbar
.prototype.initialize = function () {
2988 this.initialized
= true;
2994 * Tools can be specified in the following ways:
2995 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
2996 * - All tools in a group: `{ 'group': 'group-name' }`
2997 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
3000 * @param {Object.<string,Array>} groups List of tool group configurations
3001 * @param {Array|string} [groups.include] Tools to include
3002 * @param {Array|string} [groups.exclude] Tools to exclude
3003 * @param {Array|string} [groups.promote] Tools to promote to the beginning
3004 * @param {Array|string} [groups.demote] Tools to demote to the end
3006 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
3007 var i
, len
, type
, group
,
3009 // TODO: Use a registry instead
3010 defaultType
= 'bar',
3012 'bar': OO
.ui
.BarToolGroup
,
3013 'list': OO
.ui
.ListToolGroup
,
3014 'menu': OO
.ui
.MenuToolGroup
3017 // Cleanup previous groups
3020 // Build out new groups
3021 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
3023 if ( group
.include
=== '*' ) {
3024 // Apply defaults to catch-all groups
3025 if ( group
.type
=== undefined ) {
3026 group
.type
= 'list';
3028 if ( group
.label
=== undefined ) {
3029 group
.label
= 'ooui-toolbar-more';
3032 type
= constructors
[group
.type
] ? group
.type
: defaultType
;
3034 new constructors
[type
]( this, $.extend( { '$': this.$ }, group
) )
3037 this.addItems( items
);
3041 * Remove all tools and groups from the toolbar.
3043 OO
.ui
.Toolbar
.prototype.reset = function () {
3048 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3049 this.items
[i
].destroy();
3055 * Destroys toolbar, removing event handlers and DOM elements.
3057 * Call this whenever you are done using a toolbar.
3059 OO
.ui
.Toolbar
.prototype.destroy = function () {
3061 this.$element
.remove();
3065 * Check if tool has not been used yet.
3067 * @param {string} name Symbolic name of tool
3068 * @return {boolean} Tool is available
3070 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
3071 return !this.tools
[name
];
3075 * Prevent tool from being used again.
3077 * @param {OO.ui.Tool} tool Tool to reserve
3079 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
3080 this.tools
[tool
.getName()] = tool
;
3084 * Allow tool to be used again.
3086 * @param {OO.ui.Tool} tool Tool to release
3088 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
3089 delete this.tools
[tool
.getName()];
3093 * Get accelerator label for tool.
3095 * This is a stub that should be overridden to provide access to accelerator information.
3097 * @param {string} name Symbolic name of tool
3098 * @returns {string|undefined} Tool accelerator label if available
3100 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
3104 * Factory for tools.
3107 * @extends OO.Factory
3110 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3111 // Parent constructor
3112 OO
.Factory
.call( this );
3117 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3121 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3122 var i
, len
, included
, promoted
, demoted
,
3126 // Collect included and not excluded tools
3127 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3130 promoted
= this.extract( promote
, used
);
3131 demoted
= this.extract( demote
, used
);
3134 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3135 if ( !used
[included
[i
]] ) {
3136 auto
.push( included
[i
] );
3140 return promoted
.concat( auto
).concat( demoted
);
3144 * Get a flat list of names from a list of names or groups.
3146 * Tools can be specified in the following ways:
3147 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3148 * - All tools in a group: `{ 'group': 'group-name' }`
3149 * - All tools: `'*'`
3152 * @param {Array|string} collection List of tools
3153 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3154 * names will be added as properties
3155 * @return {string[]} List of extracted names
3157 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3158 var i
, len
, item
, name
, tool
,
3161 if ( collection
=== '*' ) {
3162 for ( name
in this.registry
) {
3163 tool
= this.registry
[name
];
3165 // Only add tools by group name when auto-add is enabled
3166 tool
.static.autoAdd
&&
3167 // Exclude already used tools
3168 ( !used
|| !used
[name
] )
3176 } else if ( $.isArray( collection
) ) {
3177 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3178 item
= collection
[i
];
3179 // Allow plain strings as shorthand for named tools
3180 if ( typeof item
=== 'string' ) {
3181 item
= { 'name': item
};
3183 if ( OO
.isPlainObject( item
) ) {
3185 for ( name
in this.registry
) {
3186 tool
= this.registry
[name
];
3188 // Include tools with matching group
3189 tool
.static.group
=== item
.group
&&
3190 // Only add tools by group name when auto-add is enabled
3191 tool
.static.autoAdd
&&
3192 // Exclude already used tools
3193 ( !used
|| !used
[name
] )
3202 // Include tools with matching name and exclude already used tools
3203 else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3204 names
.push( item
.name
);
3206 used
[item
.name
] = true;
3215 * Collection of tools.
3219 * @extends OO.ui.Widget
3220 * @mixins OO.ui.GroupElement
3222 * Tools can be specified in the following ways:
3223 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3224 * - All tools in a group: `{ 'group': 'group-name' }`
3225 * - All tools: `'*'`
3228 * @param {OO.ui.Toolbar} toolbar
3229 * @param {Object} [config] Configuration options
3230 * @cfg {Array|string} [include=[]] List of tools to include
3231 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3232 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3233 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3235 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3236 // Configuration initialization
3237 config
= config
|| {};
3239 // Parent constructor
3240 OO
.ui
.Widget
.call( this, config
);
3242 // Mixin constructors
3243 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3246 this.toolbar
= toolbar
;
3248 this.pressed
= null;
3249 this.include
= config
.include
|| [];
3250 this.exclude
= config
.exclude
|| [];
3251 this.promote
= config
.promote
|| [];
3252 this.demote
= config
.demote
|| [];
3253 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3257 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3258 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3259 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3260 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3262 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3265 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3267 .addClass( 'oo-ui-toolGroup' )
3268 .append( this.$group
);
3274 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3276 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3284 /* Static Properties */
3287 * Show labels in tooltips.
3290 * @property {boolean}
3293 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3296 * Show acceleration labels in tooltips.
3299 * @property {boolean}
3302 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3307 * Handle mouse down events.
3310 * @param {jQuery.Event} e Mouse down event
3312 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3313 if ( !this.disabled
&& e
.which
=== 1 ) {
3314 this.pressed
= this.getTargetTool( e
);
3315 if ( this.pressed
) {
3316 this.pressed
.setActive( true );
3317 this.getElementDocument().addEventListener(
3318 'mouseup', this.onCapturedMouseUpHandler
, true
3326 * Handle captured mouse up events.
3329 * @param {Event} e Mouse up event
3331 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3332 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3333 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3334 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3335 this.onMouseUp( e
);
3339 * Handle mouse up events.
3342 * @param {jQuery.Event} e Mouse up event
3344 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3345 var tool
= this.getTargetTool( e
);
3347 if ( !this.disabled
&& e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3348 this.pressed
.onSelect();
3351 this.pressed
= null;
3356 * Handle mouse over events.
3359 * @param {jQuery.Event} e Mouse over event
3361 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3362 var tool
= this.getTargetTool( e
);
3364 if ( this.pressed
&& this.pressed
=== tool
) {
3365 this.pressed
.setActive( true );
3370 * Handle mouse out events.
3373 * @param {jQuery.Event} e Mouse out event
3375 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3376 var tool
= this.getTargetTool( e
);
3378 if ( this.pressed
&& this.pressed
=== tool
) {
3379 this.pressed
.setActive( false );
3384 * Get the closest tool to a jQuery.Event.
3386 * Only tool links are considered, which prevents other elements in the tool such as popups from
3387 * triggering tool group interactions.
3391 * @param {jQuery.Event} e
3392 * @returns {OO.ui.Tool|null} Tool, `null` if none was found
3394 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3396 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3398 if ( $item
.length
) {
3399 tool
= $item
.parent().data( 'oo-ui-tool' );
3402 return tool
&& !tool
.isDisabled() ? tool
: null;
3406 * Handle tool registry register events.
3408 * If a tool is registered after the group is created, we must repopulate the list to account for:
3409 * - a tool being added that may be included
3410 * - a tool already included being overridden
3412 * @param {string} name Symbolic name of tool
3414 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3419 * Get the toolbar this group is in.
3421 * @return {OO.ui.Toolbar} Toolbar of group
3423 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3424 return this.toolbar
;
3428 * Add and remove tools based on configuration.
3432 OO
.ui
.ToolGroup
.prototype.populate = function () {
3433 var i
, len
, name
, tool
,
3434 toolFactory
= this.toolbar
.getToolFactory(),
3438 list
= this.toolbar
.getToolFactory().getTools(
3439 this.include
, this.exclude
, this.promote
, this.demote
3442 // Build a list of needed tools
3443 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3447 toolFactory
.lookup( name
) &&
3448 // Tool is available or is already in this group
3449 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3451 tool
= this.tools
[name
];
3453 // Auto-initialize tools on first use
3454 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3457 this.toolbar
.reserveTool( tool
);
3462 // Remove tools that are no longer needed
3463 for ( name
in this.tools
) {
3464 if ( !names
[name
] ) {
3465 this.tools
[name
].destroy();
3466 this.toolbar
.releaseTool( this.tools
[name
] );
3467 remove
.push( this.tools
[name
] );
3468 delete this.tools
[name
];
3471 if ( remove
.length
) {
3472 this.removeItems( remove
);
3474 // Update emptiness state
3476 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3478 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3480 // Re-add tools (moving existing ones to new locations)
3481 this.addItems( add
);
3485 * Destroy tool group.
3489 OO
.ui
.ToolGroup
.prototype.destroy = function () {
3493 this.toolbar
.getToolFactory().disconnect( this );
3494 for ( name
in this.tools
) {
3495 this.toolbar
.releaseTool( this.tools
[name
] );
3496 this.tools
[name
].disconnect( this ).destroy();
3497 delete this.tools
[name
];
3499 this.$element
.remove();
3502 * Layout made of a fieldset and optional legend.
3504 * Just add OO.ui.FieldLayout items.
3507 * @extends OO.ui.Layout
3508 * @mixins OO.ui.LabeledElement
3509 * @mixins OO.ui.IconedElement
3510 * @mixins OO.ui.GroupElement
3513 * @param {Object} [config] Configuration options
3514 * @cfg {string} [icon] Symbolic icon name
3515 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
3517 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
3518 // Config initialization
3519 config
= config
|| {};
3521 // Parent constructor
3522 OO
.ui
.Layout
.call( this, config
);
3524 // Mixin constructors
3525 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
3526 OO
.ui
.LabeledElement
.call( this, this.$( '<legend>' ), config
);
3527 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3531 .addClass( 'oo-ui-fieldsetLayout' )
3532 .append( this.$icon
, this.$label
, this.$group
);
3533 if ( $.isArray( config
.items
) ) {
3534 this.addItems( config
.items
);
3540 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
3542 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
3543 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
3544 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
3546 /* Static Properties */
3548 OO
.ui
.FieldsetLayout
.static.tagName
= 'fieldset';
3550 * Layout made of a field and optional label.
3553 * @extends OO.ui.Layout
3554 * @mixins OO.ui.LabeledElement
3556 * Available label alignment modes include:
3557 * - 'left': Label is before the field and aligned away from it, best for when the user will be
3558 * scanning for a specific label in a form with many fields
3559 * - 'right': Label is before the field and aligned toward it, best for forms the user is very
3560 * familiar with and will tab through field checking quickly to verify which field they are in
3561 * - 'top': Label is before the field and above it, best for when the use will need to fill out all
3562 * fields from top to bottom in a form with few fields
3563 * - 'inline': Label is after the field and aligned toward it, best for small boolean fields like
3564 * checkboxes or radio buttons
3567 * @param {OO.ui.Widget} field Field widget
3568 * @param {Object} [config] Configuration options
3569 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
3571 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
3572 // Config initialization
3573 config
= $.extend( { 'align': 'left' }, config
);
3575 // Parent constructor
3576 OO
.ui
.Layout
.call( this, config
);
3578 // Mixin constructors
3579 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
3582 this.$field
= this.$( '<div>' );
3587 if ( this.field
instanceof OO
.ui
.InputWidget
) {
3588 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
3592 this.$element
.addClass( 'oo-ui-fieldLayout' );
3594 .addClass( 'oo-ui-fieldLayout-field' )
3595 .append( this.field
.$element
);
3596 this.setAlignment( config
.align
);
3601 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
3603 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
3608 * Handles label mouse click events.
3611 * @param {jQuery.Event} e Mouse click event
3613 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
3614 this.field
.simulateLabelClick();
3621 * @returns {OO.ui.Widget} Field widget
3623 OO
.ui
.FieldLayout
.prototype.getField = function () {
3628 * Set the field alignment mode.
3630 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
3633 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
3634 if ( value
!== this.align
) {
3635 // Default to 'left'
3636 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
3640 if ( value
=== 'inline' ) {
3641 this.$element
.append( this.$field
, this.$label
);
3643 this.$element
.append( this.$label
, this.$field
);
3647 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
3650 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
3656 * Layout made of proportionally sized columns and rows.
3659 * @extends OO.ui.Layout
3662 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
3663 * @param {Object} [config] Configuration options
3664 * @cfg {number[]} [widths] Widths of columns as ratios
3665 * @cfg {number[]} [heights] Heights of columns as ratios
3667 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
3670 // Config initialization
3671 config
= config
|| {};
3673 // Parent constructor
3674 OO
.ui
.Layout
.call( this, config
);
3682 this.$element
.addClass( 'oo-ui-gridLayout' );
3683 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
3684 this.panels
.push( panels
[i
] );
3685 this.$element
.append( panels
[i
].$element
);
3687 if ( config
.widths
|| config
.heights
) {
3688 this.layout( config
.widths
|| [1], config
.heights
|| [1] );
3690 // Arrange in columns by default
3692 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
3695 this.layout( widths
, [1] );
3701 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
3713 /* Static Properties */
3715 OO
.ui
.GridLayout
.static.tagName
= 'div';
3720 * Set grid dimensions.
3723 * @param {number[]} widths Widths of columns as ratios
3724 * @param {number[]} heights Heights of rows as ratios
3726 * @throws {Error} If grid is not large enough to fit all panels
3728 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
3732 cols
= widths
.length
,
3733 rows
= heights
.length
;
3735 // Verify grid is big enough to fit panels
3736 if ( cols
* rows
< this.panels
.length
) {
3737 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
3740 // Sum up denominators
3741 for ( x
= 0; x
< cols
; x
++ ) {
3744 for ( y
= 0; y
< rows
; y
++ ) {
3750 for ( x
= 0; x
< cols
; x
++ ) {
3751 this.widths
[x
] = widths
[x
] / xd
;
3753 for ( y
= 0; y
< rows
; y
++ ) {
3754 this.heights
[y
] = heights
[y
] / yd
;
3758 this.emit( 'layout' );
3762 * Update panel positions and sizes.
3767 OO
.ui
.GridLayout
.prototype.update = function () {
3775 cols
= this.widths
.length
,
3776 rows
= this.heights
.length
;
3778 for ( y
= 0; y
< rows
; y
++ ) {
3779 for ( x
= 0; x
< cols
; x
++ ) {
3780 panel
= this.panels
[i
];
3781 width
= this.widths
[x
];
3782 height
= this.heights
[y
];
3784 'width': Math
.round( width
* 100 ) + '%',
3785 'height': Math
.round( height
* 100 ) + '%',
3786 'top': Math
.round( top
* 100 ) + '%'
3789 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
3790 dimensions
.right
= Math
.round( left
* 100 ) + '%';
3792 dimensions
.left
= Math
.round( left
* 100 ) + '%';
3794 panel
.$element
.css( dimensions
);
3802 this.emit( 'update' );
3806 * Get a panel at a given position.
3808 * The x and y position is affected by the current grid layout.
3811 * @param {number} x Horizontal position
3812 * @param {number} y Vertical position
3813 * @returns {OO.ui.PanelLayout} The panel at the given postion
3815 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
3816 return this.panels
[( x
* this.widths
.length
) + y
];
3819 * Layout containing a series of pages.
3822 * @extends OO.ui.Layout
3825 * @param {Object} [config] Configuration options
3826 * @cfg {boolean} [continuous=false] Show all pages, one after another
3827 * @cfg {boolean} [autoFocus=false] Focus on the first focusable element when changing to a page
3828 * @cfg {boolean} [outlined=false] Show an outline
3829 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
3830 * @cfg {Object[]} [adders] List of adders for controls, each with name, icon and title properties
3832 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
3833 // Initialize configuration
3834 config
= config
|| {};
3836 // Parent constructor
3837 OO
.ui
.Layout
.call( this, config
);
3840 this.currentPageName
= null;
3842 this.ignoreFocus
= false;
3843 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
3844 this.autoFocus
= !!config
.autoFocus
;
3845 this.outlined
= !!config
.outlined
;
3846 if ( this.outlined
) {
3847 this.editable
= !!config
.editable
;
3848 this.adders
= config
.adders
|| null;
3849 this.outlineControlsWidget
= null;
3850 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
3851 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
3852 this.gridLayout
= new OO
.ui
.GridLayout(
3853 [this.outlinePanel
, this.stackLayout
], { '$': this.$, 'widths': [1, 2] }
3855 if ( this.editable
) {
3856 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
3858 { '$': this.$, 'adders': this.adders
}
3864 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
3865 if ( this.outlined
) {
3866 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
3868 if ( this.autoFocus
) {
3869 // Event 'focus' does not bubble, but 'focusin' does
3870 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
3874 this.$element
.addClass( 'oo-ui-bookletLayout' );
3875 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
3876 if ( this.outlined
) {
3877 this.outlinePanel
.$element
3878 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
3879 .append( this.outlineWidget
.$element
);
3880 if ( this.editable
) {
3881 this.outlinePanel
.$element
3882 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
3883 .append( this.outlineControlsWidget
.$element
);
3885 this.$element
.append( this.gridLayout
.$element
);
3887 this.$element
.append( this.stackLayout
.$element
);
3893 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
3899 * @param {OO.ui.PageLayout} page Current page
3904 * @param {OO.ui.PageLayout[]} page Added pages
3905 * @param {number} index Index pages were added at
3910 * @param {OO.ui.PageLayout[]} pages Removed pages
3916 * Handle stack layout focus.
3919 * @param {jQuery.Event} e Focusin event
3921 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
3924 if ( this.ignoreFocus
) {
3925 // Avoid recursion from programmatic focus trigger in #onStackLayoutSet
3929 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
3930 for ( name
in this.pages
) {
3931 if ( this.pages
[ name
].$element
[0] === $target
[0] ) {
3932 this.setPage( name
);
3939 * Handle stack layout set events.
3942 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
3944 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
3946 this.stackLayout
.$element
.find( ':focus' ).blur();
3947 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
3948 this.ignoreFocus
= true;
3949 if ( this.autoFocus
) {
3950 page
.$element
.find( ':input:first' ).focus();
3952 this.ignoreFocus
= false;
3958 * Handle outline widget select events.
3961 * @param {OO.ui.OptionWidget|null} item Selected item
3963 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
3965 this.setPage( item
.getData() );
3970 * Check if booklet has an outline.
3973 * @returns {boolean} Booklet is outlined
3975 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
3976 return this.outlined
;
3980 * Check if booklet has editing controls.
3983 * @returns {boolean} Booklet is outlined
3985 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
3986 return this.editable
;
3990 * Get the outline widget.
3993 * @param {OO.ui.PageLayout} page Page to be selected
3994 * @returns {OO.ui.PageLayout|null} Closest page to another
3996 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
3997 var next
, prev
, level
,
3998 pages
= this.stackLayout
.getItems(),
3999 index
= $.inArray( page
, pages
);
4001 if ( index
!== -1 ) {
4002 next
= pages
[index
+ 1];
4003 prev
= pages
[index
- 1];
4004 // Prefer adjacent pages at the same level
4005 if ( this.outlined
) {
4006 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
4009 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
4015 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
4021 return prev
|| next
|| null;
4025 * Get the outline widget.
4028 * @returns {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
4030 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
4031 return this.outlineWidget
;
4035 * Get the outline controls widget. If the outline is not editable, null is returned.
4038 * @returns {OO.ui.OutlineControlsWidget|null} The outline controls widget.
4040 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
4041 return this.outlineControlsWidget
;
4045 * Get a page by name.
4048 * @param {string} name Symbolic name of page
4049 * @returns {OO.ui.PageLayout|undefined} Page, if found
4051 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
4052 return this.pages
[name
];
4056 * Get the current page name.
4059 * @returns {string|null} Current page name
4061 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
4062 return this.currentPageName
;
4066 * Add a page to the layout.
4068 * When pages are added with the same names as existing pages, the existing pages will be
4069 * automatically removed before the new pages are added.
4072 * @param {OO.ui.PageLayout[]} pages Pages to add
4073 * @param {number} index Index to insert pages after
4077 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
4078 var i
, len
, name
, page
, item
, currentIndex
,
4079 stackLayoutPages
= this.stackLayout
.getItems(),
4083 // Remove pages with same names
4084 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4086 name
= page
.getName();
4088 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
4089 // Correct the insertion index
4090 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
4091 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
4094 remove
.push( this.pages
[name
] );
4097 if ( remove
.length
) {
4098 this.removePages( remove
);
4102 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4104 name
= page
.getName();
4105 this.pages
[page
.getName()] = page
;
4106 if ( this.outlined
) {
4107 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { '$': this.$ } );
4108 page
.setOutlineItem( item
);
4113 if ( this.outlined
&& items
.length
) {
4114 this.outlineWidget
.addItems( items
, index
);
4115 this.updateOutlineWidget();
4117 this.stackLayout
.addItems( pages
, index
);
4118 this.emit( 'add', pages
, index
);
4124 * Remove a page from the layout.
4130 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
4131 var i
, len
, name
, page
,
4134 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4136 name
= page
.getName();
4137 delete this.pages
[name
];
4138 if ( this.outlined
) {
4139 items
.push( this.outlineWidget
.getItemFromData( name
) );
4140 page
.setOutlineItem( null );
4143 if ( this.outlined
&& items
.length
) {
4144 this.outlineWidget
.removeItems( items
);
4145 this.updateOutlineWidget();
4147 this.stackLayout
.removeItems( pages
);
4148 this.emit( 'remove', pages
);
4154 * Clear all pages from the layout.
4160 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
4162 pages
= this.stackLayout
.getItems();
4165 this.currentPageName
= null;
4166 if ( this.outlined
) {
4167 this.outlineWidget
.clearItems();
4168 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4169 pages
[i
].setOutlineItem( null );
4172 this.stackLayout
.clearItems();
4174 this.emit( 'remove', pages
);
4180 * Set the current page by name.
4184 * @param {string} name Symbolic name of page
4186 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
4188 page
= this.pages
[name
];
4190 if ( name
!== this.currentPageName
) {
4191 if ( this.outlined
) {
4192 selectedItem
= this.outlineWidget
.getSelectedItem();
4193 if ( selectedItem
&& selectedItem
.getData() !== name
) {
4194 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
4198 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
4199 this.pages
[this.currentPageName
].setActive( false );
4201 this.currentPageName
= name
;
4202 this.stackLayout
.setItem( page
);
4203 page
.setActive( true );
4204 this.emit( 'set', page
);
4210 * Call this after adding or removing items from the OutlineWidget.
4215 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
4216 // Auto-select first item when nothing is selected anymore
4217 if ( !this.outlineWidget
.getSelectedItem() ) {
4218 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
4224 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
4227 * @extends OO.ui.Layout
4230 * @param {Object} [config] Configuration options
4231 * @cfg {boolean} [scrollable] Allow vertical scrolling
4232 * @cfg {boolean} [padded] Pad the content from the edges
4234 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
4235 // Config initialization
4236 config
= config
|| {};
4238 // Parent constructor
4239 OO
.ui
.Layout
.call( this, config
);
4242 this.$element
.addClass( 'oo-ui-panelLayout' );
4243 if ( config
.scrollable
) {
4244 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
4247 if ( config
.padded
) {
4248 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
4251 // Add directionality class:
4252 this.$element
.addClass( 'oo-ui-' + OO
.ui
.Element
.getDir( this.$.context
) );
4257 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
4259 * Page within an OO.ui.BookletLayout.
4262 * @extends OO.ui.PanelLayout
4265 * @param {string} name Unique symbolic name of page
4266 * @param {Object} [config] Configuration options
4267 * @param {string} [outlineItem] Outline item widget
4269 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
4270 // Configuration initialization
4271 config
= $.extend( { 'scrollable': true }, config
);
4273 // Parent constructor
4274 OO
.ui
.PanelLayout
.call( this, config
);
4278 this.outlineItem
= config
.outlineItem
|| null;
4279 this.active
= false;
4282 this.$element
.addClass( 'oo-ui-pageLayout' );
4287 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4293 * @param {boolean} active Page is active
4301 * @returns {string} Symbolic name of page
4303 OO
.ui
.PageLayout
.prototype.getName = function () {
4308 * Check if page is active.
4310 * @returns {boolean} Page is active
4312 OO
.ui
.PageLayout
.prototype.isActive = function () {
4319 * @returns {OO.ui.OutlineItemWidget|null} Outline item widget
4321 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
4322 return this.outlineItem
;
4328 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
4331 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
4332 this.outlineItem
= outlineItem
;
4337 * Set page active state.
4339 * @param {boolean} Page is active
4342 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
4345 if ( active
!== this.active
) {
4346 this.active
= active
;
4347 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
4348 this.emit( 'active', this.active
);
4352 * Layout containing a series of mutually exclusive pages.
4355 * @extends OO.ui.PanelLayout
4356 * @mixins OO.ui.GroupElement
4359 * @param {Object} [config] Configuration options
4360 * @cfg {boolean} [continuous=false] Show all pages, one after another
4361 * @cfg {string} [icon=''] Symbolic icon name
4362 * @cfg {OO.ui.Layout[]} [items] Layouts to add
4364 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4365 // Config initialization
4366 config
= $.extend( { 'scrollable': true }, config
);
4368 // Parent constructor
4369 OO
.ui
.PanelLayout
.call( this, config
);
4371 // Mixin constructors
4372 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4375 this.currentItem
= null;
4376 this.continuous
= !!config
.continuous
;
4379 this.$element
.addClass( 'oo-ui-stackLayout' );
4380 if ( this.continuous
) {
4381 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4383 if ( $.isArray( config
.items
) ) {
4384 this.addItems( config
.items
);
4390 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4392 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4398 * @param {OO.ui.PanelLayout|null} [item] Current item
4406 * Adding an existing item (by value) will move it.
4409 * @param {OO.ui.PanelLayout[]} items Items to add
4410 * @param {number} [index] Index to insert items after
4413 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
4414 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
4416 if ( !this.currentItem
&& items
.length
) {
4417 this.setItem( items
[0] );
4426 * Items will be detached, not removed, so they can be used later.
4429 * @param {OO.ui.PanelLayout[]} items Items to remove
4432 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
4433 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
4434 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
4435 this.currentItem
= null;
4436 if ( !this.currentItem
&& this.items
.length
) {
4437 this.setItem( this.items
[0] );
4447 * Items will be detached, not removed, so they can be used later.
4452 OO
.ui
.StackLayout
.prototype.clearItems = function () {
4453 this.currentItem
= null;
4454 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
4462 * Any currently shown item will be hidden.
4465 * @param {OO.ui.PanelLayout} item Item to show
4468 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
4469 if ( item
!== this.currentItem
) {
4470 if ( !this.continuous
) {
4471 this.$items
.css( 'display', '' );
4473 if ( $.inArray( item
, this.items
) !== -1 ) {
4474 if ( !this.continuous
) {
4475 item
.$element
.css( 'display', 'block' );
4480 this.currentItem
= item
;
4481 this.emit( 'set', item
);
4487 * Horizontal bar layout of tools as icon buttons.
4491 * @extends OO.ui.ToolGroup
4494 * @param {OO.ui.Toolbar} toolbar
4495 * @param {Object} [config] Configuration options
4497 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
4498 // Parent constructor
4499 OO
.ui
.ToolGroup
.call( this, toolbar
, config
);
4502 this.$element
.addClass( 'oo-ui-barToolGroup' );
4507 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
4509 /* Static Properties */
4511 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
4513 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
4515 * Popup list of tools with an icon and optional label.
4519 * @extends OO.ui.ToolGroup
4520 * @mixins OO.ui.IconedElement
4521 * @mixins OO.ui.IndicatedElement
4522 * @mixins OO.ui.LabeledElement
4523 * @mixins OO.ui.TitledElement
4524 * @mixins OO.ui.ClippableElement
4527 * @param {OO.ui.Toolbar} toolbar
4528 * @param {Object} [config] Configuration options
4530 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
4531 // Configuration initialization
4532 config
= config
|| {};
4534 // Parent constructor
4535 OO
.ui
.ToolGroup
.call( this, toolbar
, config
);
4537 // Mixin constructors
4538 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4539 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
4540 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
4541 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
4542 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
4545 this.active
= false;
4546 this.dragging
= false;
4547 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
4548 this.$handle
= this.$( '<span>' );
4552 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
4553 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
4558 .addClass( 'oo-ui-popupToolGroup-handle' )
4559 .append( this.$icon
, this.$label
, this.$indicator
);
4561 .addClass( 'oo-ui-popupToolGroup' )
4562 .prepend( this.$handle
);
4567 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
4569 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
4570 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
4571 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
4572 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
4573 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
4575 /* Static Properties */
4580 * Handle focus being lost.
4582 * The event is actually generated from a mouseup, so it is not a normal blur event object.
4585 * @param {jQuery.Event} e Mouse up event
4587 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
4588 // Only deactivate when clicking outside the dropdown element
4589 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
4590 this.setActive( false );
4597 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
4598 if ( !this.disabled
&& e
.which
=== 1 ) {
4599 this.setActive( false );
4601 return OO
.ui
.ToolGroup
.prototype.onMouseUp
.call( this, e
);
4605 * Handle mouse up events.
4608 * @param {jQuery.Event} e Mouse up event
4610 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
4615 * Handle mouse down events.
4618 * @param {jQuery.Event} e Mouse down event
4620 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
4621 if ( !this.disabled
&& e
.which
=== 1 ) {
4622 this.setActive( !this.active
);
4628 * Switch into active mode.
4630 * When active, mouseup events anywhere in the document will trigger deactivation.
4634 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
4636 if ( this.active
!== value
) {
4637 this.active
= value
;
4639 this.setClipping( true );
4640 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
4641 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
4643 this.setClipping( false );
4644 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
4645 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
4650 * Drop down list layout of tools as labeled icon buttons.
4654 * @extends OO.ui.PopupToolGroup
4657 * @param {OO.ui.Toolbar} toolbar
4658 * @param {Object} [config] Configuration options
4660 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
4661 // Parent constructor
4662 OO
.ui
.PopupToolGroup
.call( this, toolbar
, config
);
4665 this.$element
.addClass( 'oo-ui-listToolGroup' );
4670 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
4672 /* Static Properties */
4674 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
4676 * Drop down menu layout of tools as selectable menu items.
4680 * @extends OO.ui.PopupToolGroup
4683 * @param {OO.ui.Toolbar} toolbar
4684 * @param {Object} [config] Configuration options
4686 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
4687 // Configuration initialization
4688 config
= config
|| {};
4690 // Parent constructor
4691 OO
.ui
.PopupToolGroup
.call( this, toolbar
, config
);
4694 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
4697 this.$element
.addClass( 'oo-ui-menuToolGroup' );
4702 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
4704 /* Static Properties */
4706 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
4711 * Handle the toolbar state being updated.
4713 * When the state changes, the title of each active item in the menu will be joined together and
4714 * used as a label for the group. The label will be empty if none of the items are active.
4718 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
4722 for ( name
in this.tools
) {
4723 if ( this.tools
[name
].isActive() ) {
4724 labelTexts
.push( this.tools
[name
].getTitle() );
4728 this.setLabel( labelTexts
.join( ', ' ) );
4731 * UserInterface popup tool.
4735 * @extends OO.ui.Tool
4736 * @mixins OO.ui.PopuppableElement
4739 * @param {OO.ui.Toolbar} toolbar
4740 * @param {Object} [config] Configuration options
4742 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
4743 // Parent constructor
4744 OO
.ui
.Tool
.call( this, toolbar
, config
);
4746 // Mixin constructors
4747 OO
.ui
.PopuppableElement
.call( this, config
);
4751 .addClass( 'oo-ui-popupTool' )
4752 .append( this.popup
.$element
);
4757 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
4759 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
4764 * Handle the tool being selected.
4768 OO
.ui
.PopupTool
.prototype.onSelect = function () {
4769 if ( !this.disabled
) {
4770 if ( this.popup
.isVisible() ) {
4776 this.setActive( false );
4781 * Handle the toolbar state being updated.
4785 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
4786 this.setActive( false );
4791 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
4795 * @extends OO.ui.GroupElement
4798 * @param {jQuery} $group Container node, assigned to #$group
4799 * @param {Object} [config] Configuration options
4801 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
4802 // Parent constructor
4803 OO
.ui
.GroupElement
.call( this, $element
, config
);
4808 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
4813 * Set the disabled state of the widget.
4815 * This will also update the disabled state of child widgets.
4818 * @param {boolean} disabled Disable widget
4821 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
4825 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
4827 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
4829 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4830 this.items
[i
].updateDisabled();
4839 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
4846 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
4853 * Check if widget is disabled.
4855 * Checks parent if present, making disabled state inheritable.
4857 * @returns {boolean} Widget is disabled
4859 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
4860 return this.disabled
||
4861 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
4865 * Set group element is in.
4867 * @param {OO.ui.GroupElement|null} group Group element, null if none
4870 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
4872 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
4874 // Initialize item disabled states
4875 this.updateDisabled();
4880 * Container for multiple related buttons.
4883 * @extends OO.ui.Widget
4884 * @mixins OO.ui.GroupElement
4887 * @param {Object} [config] Configuration options
4888 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
4890 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
4891 // Parent constructor
4892 OO
.ui
.Widget
.call( this, config
);
4894 // Mixin constructors
4895 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4898 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
4899 if ( $.isArray( config
.items
) ) {
4900 this.addItems( config
.items
);
4906 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
4908 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
4910 * Creates an OO.ui.ButtonWidget object.
4914 * @extends OO.ui.Widget
4915 * @mixins OO.ui.ButtonedElement
4916 * @mixins OO.ui.IconedElement
4917 * @mixins OO.ui.IndicatedElement
4918 * @mixins OO.ui.LabeledElement
4919 * @mixins OO.ui.TitledElement
4920 * @mixins OO.ui.FlaggableElement
4923 * @param {Object} [config] Configuration options
4924 * @cfg {string} [title=''] Title text
4925 * @cfg {string} [href] Hyperlink to visit when clicked
4926 * @cfg {string} [target] Target to open hyperlink in
4928 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
4929 // Configuration initialization
4930 config
= $.extend( { 'target': '_blank' }, config
);
4932 // Parent constructor
4933 OO
.ui
.Widget
.call( this, config
);
4935 // Mixin constructors
4936 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
4937 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
4938 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
4939 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
4940 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
4941 OO
.ui
.FlaggableElement
.call( this, config
);
4944 this.isHyperlink
= typeof config
.href
=== 'string';
4948 'click': OO
.ui
.bind( this.onClick
, this ),
4949 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
4954 .append( this.$icon
, this.$label
, this.$indicator
)
4955 .attr( { 'href': config
.href
, 'target': config
.target
} );
4957 .addClass( 'oo-ui-buttonWidget' )
4958 .append( this.$button
);
4963 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
4965 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
4966 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
4967 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
4968 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
4969 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
4970 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
4981 * Handles mouse click events.
4984 * @param {jQuery.Event} e Mouse click event
4987 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
4988 if ( !this.disabled
) {
4989 this.emit( 'click' );
4990 if ( this.isHyperlink
) {
4998 * Handles keypress events.
5001 * @param {jQuery.Event} e Keypress event
5004 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
5005 if ( !this.disabled
&& e
.which
=== OO
.ui
.Keys
.SPACE
) {
5006 if ( this.isHyperlink
) {
5014 * Creates an OO.ui.InputWidget object.
5018 * @extends OO.ui.Widget
5021 * @param {Object} [config] Configuration options
5022 * @cfg {string} [name=''] HTML input name
5023 * @cfg {string} [value=''] Input value
5024 * @cfg {boolean} [readOnly=false] Prevent changes
5025 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
5027 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
5028 // Config intialization
5029 config
= $.extend( { 'readOnly': false }, config
);
5031 // Parent constructor
5032 OO
.ui
.Widget
.call( this, config
);
5035 this.$input
= this.getInputElement( config
);
5037 this.readOnly
= false;
5038 this.inputFilter
= config
.inputFilter
;
5041 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
5045 .attr( 'name', config
.name
)
5046 .prop( 'disabled', this.disabled
);
5047 this.setReadOnly( config
.readOnly
);
5048 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
5049 this.setValue( config
.value
);
5054 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
5066 * Get input element.
5069 * @param {Object} [config] Configuration options
5070 * @returns {jQuery} Input element
5072 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
5073 return this.$( '<input>' );
5077 * Handle potentially value-changing events.
5080 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
5082 OO
.ui
.InputWidget
.prototype.onEdit = function () {
5083 if ( !this.disabled
) {
5084 // Allow the stack to clear so the value will be updated
5085 setTimeout( OO
.ui
.bind( function () {
5086 this.setValue( this.$input
.val() );
5092 * Get the value of the input.
5095 * @returns {string} Input value
5097 OO
.ui
.InputWidget
.prototype.getValue = function () {
5102 * Sets the direction of the current input, either RTL or LTR
5105 * @param {boolean} isRTL
5107 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
5109 this.$input
.removeClass( 'oo-ui-ltr' );
5110 this.$input
.addClass( 'oo-ui-rtl' );
5112 this.$input
.removeClass( 'oo-ui-rtl' );
5113 this.$input
.addClass( 'oo-ui-ltr' );
5118 * Set the value of the input.
5121 * @param {string} value New value
5125 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
5126 value
= this.sanitizeValue( value
);
5127 if ( this.value
!== value
) {
5129 this.emit( 'change', this.value
);
5131 // Update the DOM if it has changed. Note that with sanitizeValue, it
5132 // is possible for the DOM value to change without this.value changing.
5133 if ( this.$input
.val() !== this.value
) {
5134 this.$input
.val( this.value
);
5140 * Sanitize incoming value.
5142 * Ensures value is a string, and converts undefined and null to empty strings.
5145 * @param {string} value Original value
5146 * @returns {string} Sanitized value
5148 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
5149 if ( value
=== undefined || value
=== null ) {
5151 } else if ( this.inputFilter
) {
5152 return this.inputFilter( String( value
) );
5154 return String( value
);
5159 * Simulate the behavior of clicking on a label bound to this input.
5163 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
5164 if ( !this.isDisabled() ) {
5165 if ( this.$input
.is( ':checkbox,:radio' ) ) {
5166 this.$input
.click();
5167 } else if ( this.$input
.is( ':input' ) ) {
5168 this.$input
.focus();
5174 * Check if the widget is read-only.
5177 * @param {boolean} Input is read-only
5179 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
5180 return this.readOnly
;
5184 * Set the read-only state of the widget.
5186 * This should probably change the widgets's appearance and prevent it from being used.
5189 * @param {boolean} state Make input read-only
5192 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
5193 this.readOnly
= !!state
;
5194 this.$input
.prop( 'readonly', this.readOnly
);
5201 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
5202 OO
.ui
.Widget
.prototype.setDisabled
.call( this, state
);
5203 if ( this.$input
) {
5204 this.$input
.prop( 'disabled', this.disabled
);
5209 * Creates an OO.ui.CheckboxInputWidget object.
5212 * @extends OO.ui.InputWidget
5215 * @param {Object} [config] Configuration options
5217 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
5218 // Parent constructor
5219 OO
.ui
.InputWidget
.call( this, config
);
5222 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
5227 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
5234 * Get input element.
5236 * @returns {jQuery} Input element
5238 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
5239 return this.$( '<input type="checkbox" />' );
5243 * Get checked state of the checkbox
5245 * @returns {boolean} If the checkbox is checked
5247 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
5254 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
5256 if ( this.value
!== value
) {
5258 this.$input
.prop( 'checked', this.value
);
5259 this.emit( 'change', this.value
);
5266 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
5267 if ( !this.disabled
) {
5268 // Allow the stack to clear so the value will be updated
5269 setTimeout( OO
.ui
.bind( function () {
5270 this.setValue( this.$input
.prop( 'checked' ) );
5275 * Creates an OO.ui.LabelWidget object.
5278 * @extends OO.ui.Widget
5279 * @mixins OO.ui.LabeledElement
5282 * @param {Object} [config] Configuration options
5284 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
5285 // Config intialization
5286 config
= config
|| {};
5288 // Parent constructor
5289 OO
.ui
.Widget
.call( this, config
);
5291 // Mixin constructors
5292 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
5295 this.input
= config
.input
;
5298 if ( this.input
instanceof OO
.ui
.InputWidget
) {
5299 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
5303 this.$element
.addClass( 'oo-ui-labelWidget' );
5308 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
5310 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
5312 /* Static Properties */
5314 OO
.ui
.LabelWidget
.static.tagName
= 'label';
5319 * Handles label mouse click events.
5322 * @param {jQuery.Event} e Mouse click event
5324 OO
.ui
.LabelWidget
.prototype.onClick = function () {
5325 this.input
.simulateLabelClick();
5329 * Lookup input widget.
5331 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
5332 * events on #lookupMenu to make use of selections.
5338 * @param {OO.ui.TextInputWidget} input Input widget
5339 * @param {Object} [config] Configuration options
5340 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
5342 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
5343 // Config intialization
5344 config
= config
|| {};
5347 this.lookupInput
= input
;
5348 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
5349 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
5350 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
5351 'input': this.lookupInput
,
5352 '$container': config
.$container
5354 this.lookupCache
= {};
5355 this.lookupQuery
= null;
5356 this.lookupRequest
= null;
5357 this.populating
= false;
5360 this.$overlay
.append( this.lookupMenu
.$element
);
5362 this.lookupInput
.$input
.on( {
5363 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
5364 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
5365 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
5367 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
5370 this.$element
.addClass( 'oo-ui-lookupWidget' );
5371 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
5377 * Handle input focus event.
5380 * @param {jQuery.Event} e Input focus event
5382 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
5383 this.openLookupMenu();
5387 * Handle input blur event.
5390 * @param {jQuery.Event} e Input blur event
5392 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
5393 this.lookupMenu
.hide();
5397 * Handle input mouse down event.
5400 * @param {jQuery.Event} e Input mouse down event
5402 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
5403 this.openLookupMenu();
5407 * Handle input change event.
5410 * @param {string} value New input value
5412 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
5413 this.openLookupMenu();
5422 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
5423 var value
= this.lookupInput
.getValue();
5425 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
5426 this.populateLookupMenu();
5427 if ( !this.lookupMenu
.isVisible() ) {
5428 this.lookupMenu
.show();
5431 this.lookupMenu
.clearItems();
5432 this.lookupMenu
.hide();
5439 * Populate lookup menu with current information.
5444 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
5445 if ( !this.populating
) {
5446 this.populating
= true;
5447 this.getLookupMenuItems()
5448 .done( OO
.ui
.bind( function ( items
) {
5449 this.lookupMenu
.clearItems();
5450 if ( items
.length
) {
5451 this.lookupMenu
.show();
5452 this.lookupMenu
.addItems( items
);
5453 this.initializeLookupMenuSelection();
5454 this.openLookupMenu();
5456 this.lookupMenu
.hide();
5458 this.populating
= false;
5460 .fail( OO
.ui
.bind( function () {
5461 this.lookupMenu
.clearItems();
5462 this.populating
= false;
5470 * Set selection in the lookup menu with current information.
5475 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
5476 if ( !this.lookupMenu
.getSelectedItem() ) {
5477 this.lookupMenu
.intializeSelection( this.lookupMenu
.getFirstSelectableItem() );
5479 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
5483 * Get lookup menu items for the current query.
5486 * @returns {jQuery.Promise} Promise object which will be passed menu items as the first argument
5489 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
5490 var value
= this.lookupInput
.getValue(),
5491 deferred
= $.Deferred();
5493 if ( value
&& value
!== this.lookupQuery
) {
5494 // Abort current request if query has changed
5495 if ( this.lookupRequest
) {
5496 this.lookupRequest
.abort();
5497 this.lookupQuery
= null;
5498 this.lookupRequest
= null;
5500 if ( value
in this.lookupCache
) {
5501 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5503 this.lookupQuery
= value
;
5504 this.lookupRequest
= this.getLookupRequest()
5505 .always( OO
.ui
.bind( function () {
5506 this.lookupQuery
= null;
5507 this.lookupRequest
= null;
5509 .done( OO
.ui
.bind( function ( data
) {
5510 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
5511 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
5513 .fail( function () {
5517 this.lookupRequest
.always( OO
.ui
.bind( function () {
5522 return deferred
.promise();
5526 * Get a new request object of the current lookup query value.
5530 * @returns {jqXHR} jQuery AJAX object, or promise object with an .abort() method
5532 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
5533 // Stub, implemented in subclass
5538 * Handle successful lookup request.
5540 * Overriding methods should call #populateLookupMenu when results are available and cache results
5541 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
5545 * @param {Mixed} data Response from server
5547 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
5548 // Stub, implemented in subclass
5552 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
5556 * @param {Mixed} data Cached result data, usually an array
5557 * @returns {OO.ui.MenuItemWidget[]} Menu items
5559 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
5560 // Stub, implemented in subclass
5564 * Creates an OO.ui.OptionWidget object.
5568 * @extends OO.ui.Widget
5569 * @mixins OO.ui.IconedElement
5570 * @mixins OO.ui.LabeledElement
5571 * @mixins OO.ui.IndicatedElement
5572 * @mixins OO.ui.FlaggableElement
5575 * @param {Mixed} data Option data
5576 * @param {Object} [config] Configuration options
5577 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
5579 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
5580 // Config intialization
5581 config
= config
|| {};
5583 // Parent constructor
5584 OO
.ui
.Widget
.call( this, config
);
5586 // Mixin constructors
5587 OO
.ui
.ItemWidget
.call( this );
5588 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5589 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5590 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5591 OO
.ui
.FlaggableElement
.call( this, config
);
5595 this.selected
= false;
5596 this.highlighted
= false;
5600 .data( 'oo-ui-optionWidget', this )
5601 .attr( 'rel', config
.rel
)
5602 .addClass( 'oo-ui-optionWidget' )
5603 .append( this.$label
);
5605 .prepend( this.$icon
)
5606 .append( this.$indicator
);
5611 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
5613 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
5614 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
5615 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
5616 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
5617 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
5619 /* Static Properties */
5621 OO
.ui
.OptionWidget
.static.tagName
= 'li';
5623 OO
.ui
.OptionWidget
.static.selectable
= true;
5625 OO
.ui
.OptionWidget
.static.highlightable
= true;
5627 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
5632 * Check if option can be selected.
5635 * @returns {boolean} Item is selectable
5637 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
5638 return this.constructor.static.selectable
&& !this.disabled
;
5642 * Check if option can be highlighted.
5645 * @returns {boolean} Item is highlightable
5647 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
5648 return this.constructor.static.highlightable
&& !this.disabled
;
5652 * Check if option is selected.
5655 * @returns {boolean} Item is selected
5657 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
5658 return this.selected
;
5662 * Check if option is highlighted.
5665 * @returns {boolean} Item is highlighted
5667 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
5668 return this.highlighted
;
5672 * Set selected state.
5675 * @param {boolean} [state=false] Select option
5678 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
5679 if ( !this.disabled
&& this.constructor.static.selectable
) {
5680 this.selected
= !!state
;
5681 if ( this.selected
) {
5682 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
5683 if ( this.constructor.static.scrollIntoViewOnSelect
) {
5684 this.scrollElementIntoView();
5687 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
5694 * Set highlighted state.
5697 * @param {boolean} [state=false] Highlight option
5700 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
5701 if ( !this.disabled
&& this.constructor.static.highlightable
) {
5702 this.highlighted
= !!state
;
5703 if ( this.highlighted
) {
5704 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
5706 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
5713 * Make the option's highlight flash.
5716 * @param {Function} [done] Callback to execute when flash effect is complete.
5718 OO
.ui
.OptionWidget
.prototype.flash = function ( done
) {
5719 var $this = this.$element
;
5721 if ( !this.disabled
&& this.constructor.static.highlightable
) {
5722 $this.removeClass( 'oo-ui-optionWidget-highlighted' );
5723 setTimeout( OO
.ui
.bind( function () {
5724 $this.addClass( 'oo-ui-optionWidget-highlighted' );
5726 setTimeout( done
, 100 );
5736 * @returns {Mixed} Option data
5738 OO
.ui
.OptionWidget
.prototype.getData = function () {
5742 * Create an OO.ui.SelectWidget object.
5746 * @extends OO.ui.Widget
5747 * @mixins OO.ui.GroupElement
5750 * @param {Object} [config] Configuration options
5751 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
5753 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
5754 // Config intialization
5755 config
= config
|| {};
5757 // Parent constructor
5758 OO
.ui
.Widget
.call( this, config
);
5760 // Mixin constructors
5761 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
5764 this.pressed
= false;
5765 this.selecting
= null;
5770 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
5771 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
5772 'mousemove': OO
.ui
.bind( this.onMouseMove
, this ),
5773 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
5774 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
5778 this.$element
.addClass( 'oo-ui-selectWidget' );
5779 if ( $.isArray( config
.items
) ) {
5780 this.addItems( config
.items
);
5786 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
5788 // Need to mixin base class as well
5789 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
5791 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
5797 * @param {OO.ui.OptionWidget|null} item Highlighted item
5802 * @param {OO.ui.OptionWidget|null} item Selected item
5807 * @param {OO.ui.OptionWidget[]} items Added items
5808 * @param {number} index Index items were added at
5813 * @param {OO.ui.OptionWidget[]} items Removed items
5816 /* Static Properties */
5818 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
5823 * Handle mouse down events.
5827 * @param {jQuery.Event} e Mouse down event
5829 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
5832 if ( !this.disabled
&& e
.which
=== 1 ) {
5833 this.pressed
= true;
5834 item
= this.getTargetItem( e
);
5835 if ( item
&& item
.isSelectable() ) {
5836 this.intializeSelection( item
);
5837 this.selecting
= item
;
5838 this.$( this.$.context
).one( 'mouseup', OO
.ui
.bind( this.onMouseUp
, this ) );
5845 * Handle mouse up events.
5849 * @param {jQuery.Event} e Mouse up event
5851 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
5853 this.pressed
= false;
5854 if ( !this.selecting
) {
5855 item
= this.getTargetItem( e
);
5856 if ( item
&& item
.isSelectable() ) {
5857 this.selecting
= item
;
5860 if ( !this.disabled
&& e
.which
=== 1 && this.selecting
) {
5861 this.selectItem( this.selecting
);
5862 this.selecting
= null;
5868 * Handle mouse move events.
5872 * @param {jQuery.Event} e Mouse move event
5874 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
5877 if ( !this.disabled
&& this.pressed
) {
5878 item
= this.getTargetItem( e
);
5879 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
5880 this.intializeSelection( item
);
5881 this.selecting
= item
;
5888 * Handle mouse over events.
5892 * @param {jQuery.Event} e Mouse over event
5894 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
5897 if ( !this.disabled
) {
5898 item
= this.getTargetItem( e
);
5899 if ( item
&& item
.isHighlightable() ) {
5900 this.highlightItem( item
);
5907 * Handle mouse leave events.
5911 * @param {jQuery.Event} e Mouse over event
5913 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
5914 if ( !this.disabled
) {
5915 this.highlightItem();
5921 * Get the closest item to a jQuery.Event.
5925 * @param {jQuery.Event} e
5926 * @returns {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
5928 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
5929 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
5930 if ( $item
.length
) {
5931 return $item
.data( 'oo-ui-optionWidget' );
5937 * Get selected item.
5940 * @returns {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
5942 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
5945 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5946 if ( this.items
[i
].isSelected() ) {
5947 return this.items
[i
];
5954 * Get highlighted item.
5957 * @returns {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
5959 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
5962 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5963 if ( this.items
[i
].isHighlighted() ) {
5964 return this.items
[i
];
5971 * Get an existing item with equivilant data.
5974 * @param {Object} data Item data to search for
5975 * @returns {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
5977 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
5978 var hash
= OO
.getHash( data
);
5980 if ( hash
in this.hashes
) {
5981 return this.hashes
[hash
];
5988 * Highlight an item.
5990 * Highlighting is mutually exclusive.
5993 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
5997 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
6000 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6001 this.items
[i
].setHighlighted( this.items
[i
] === item
);
6003 this.emit( 'highlight', item
);
6012 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6016 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
6019 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6020 this.items
[i
].setSelected( this.items
[i
] === item
);
6022 this.emit( 'select', item
);
6028 * Setup selection and highlighting.
6030 * This should be used to synchronize the UI with the model without emitting events that would in
6031 * turn update the model.
6033 * @param {OO.ui.OptionWidget} [item] Item to select
6036 OO
.ui
.SelectWidget
.prototype.intializeSelection = function( item
) {
6037 var i
, len
, selected
;
6039 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6040 selected
= this.items
[i
] === item
;
6041 this.items
[i
].setSelected( selected
);
6042 this.items
[i
].setHighlighted( selected
);
6049 * Get an item relative to another one.
6052 * @param {OO.ui.OptionWidget} item Item to start at
6053 * @param {number} direction Direction to move in
6054 * @returns {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
6056 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
6057 var inc
= direction
> 0 ? 1 : -1,
6058 len
= this.items
.length
,
6059 index
= item
instanceof OO
.ui
.OptionWidget
?
6060 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
6061 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
6063 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
6064 Math
.max( index
, -1 ) :
6065 // Default to n-1 instead of -1, if nothing is selected let's start at the end
6066 Math
.min( index
, len
);
6069 i
= ( i
+ inc
+ len
) % len
;
6070 item
= this.items
[i
];
6071 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6074 // Stop iterating when we've looped all the way around
6075 if ( i
=== stopAt
) {
6083 * Get the next selectable item.
6086 * @returns {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
6088 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
6091 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6092 item
= this.items
[i
];
6093 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6104 * When items are added with the same values as existing items, the existing items will be
6105 * automatically removed before the new items are added.
6108 * @param {OO.ui.OptionWidget[]} items Items to add
6109 * @param {number} [index] Index to insert items after
6113 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
6114 var i
, len
, item
, hash
,
6117 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6119 hash
= OO
.getHash( item
.getData() );
6120 if ( hash
in this.hashes
) {
6121 // Remove item with same value
6122 remove
.push( this.hashes
[hash
] );
6124 this.hashes
[hash
] = item
;
6126 if ( remove
.length
) {
6127 this.removeItems( remove
);
6130 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
6132 // Always provide an index, even if it was omitted
6133 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
6141 * Items will be detached, not removed, so they can be used later.
6144 * @param {OO.ui.OptionWidget[]} items Items to remove
6148 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
6149 var i
, len
, item
, hash
;
6151 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6153 hash
= OO
.getHash( item
.getData() );
6154 if ( hash
in this.hashes
) {
6155 // Remove existing item
6156 delete this.hashes
[hash
];
6158 if ( item
.isSelected() ) {
6159 this.selectItem( null );
6162 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
6164 this.emit( 'remove', items
);
6172 * Items will be detached, not removed, so they can be used later.
6178 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
6179 var items
= this.items
.slice();
6183 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
6184 this.selectItem( null );
6186 this.emit( 'remove', items
);
6191 * Creates an OO.ui.MenuItemWidget object.
6194 * @extends OO.ui.OptionWidget
6197 * @param {Mixed} data Item data
6198 * @param {Object} [config] Configuration options
6200 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
6201 // Configuration initialization
6202 config
= $.extend( { 'icon': 'check' }, config
);
6204 // Parent constructor
6205 OO
.ui
.OptionWidget
.call( this, data
, config
);
6208 this.$element
.addClass( 'oo-ui-menuItemWidget' );
6213 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
6215 * Create an OO.ui.MenuWidget object.
6218 * @extends OO.ui.SelectWidget
6219 * @mixins OO.ui.ClippableElement
6222 * @param {Object} [config] Configuration options
6223 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
6225 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
6226 // Config intialization
6227 config
= config
|| {};
6229 // Parent constructor
6230 OO
.ui
.SelectWidget
.call( this, config
);
6232 // Mixin constructors
6233 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
6236 this.newItems
= null;
6237 this.$input
= config
.input
? config
.input
.$input
: null;
6238 this.$previousFocus
= null;
6239 this.isolated
= !config
.input
;
6240 this.visible
= false;
6241 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
6244 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
6249 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
6251 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
6256 * Handles key down events.
6259 * @param {jQuery.Event} e Key down event
6261 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
6264 highlightItem
= this.getHighlightedItem();
6266 if ( !this.disabled
&& this.visible
) {
6267 if ( !highlightItem
) {
6268 highlightItem
= this.getSelectedItem();
6270 switch ( e
.keyCode
) {
6271 case OO
.ui
.Keys
.ENTER
:
6272 this.selectItem( highlightItem
);
6276 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
6279 case OO
.ui
.Keys
.DOWN
:
6280 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
6283 case OO
.ui
.Keys
.ESCAPE
:
6284 if ( highlightItem
) {
6285 highlightItem
.setHighlighted( false );
6293 this.highlightItem( nextItem
);
6294 nextItem
.scrollElementIntoView();
6299 e
.stopPropagation();
6306 * Check if the menu is visible.
6309 * @returns {boolean} Menu is visible
6311 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
6312 return this.visible
;
6316 * Bind key down listener
6320 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
6321 if ( this.$input
) {
6322 this.$input
.on( 'keydown', this.onKeyDownHandler
);
6324 // Capture menu navigation keys
6325 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
6330 * Unbind key down listener
6334 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
6335 if ( this.$input
) {
6336 this.$input
.off( 'keydown' );
6338 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
6345 * The menu will stay open if an item is silently selected.
6348 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6351 OO
.ui
.MenuWidget
.prototype.selectItem = function ( item
) {
6353 OO
.ui
.SelectWidget
.prototype.selectItem
.call( this, item
);
6355 if ( !this.disabled
) {
6357 this.disabled
= true;
6358 item
.flash( OO
.ui
.bind( function () {
6360 this.disabled
= false;
6373 * Adding an existing item (by value) will move it.
6376 * @param {OO.ui.MenuItemWidget[]} items Items to add
6377 * @param {number} [index] Index to insert items after
6380 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
6384 OO
.ui
.SelectWidget
.prototype.addItems
.call( this, items
, index
);
6387 if ( !this.newItems
) {
6391 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6393 if ( this.visible
) {
6394 // Defer fitting label until
6397 this.newItems
.push( item
);
6410 OO
.ui
.MenuWidget
.prototype.show = function () {
6413 if ( this.items
.length
) {
6414 this.$element
.show();
6415 this.visible
= true;
6416 this.bindKeyDownListener();
6418 // Change focus to enable keyboard navigation
6419 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
6420 this.$previousFocus
= this.$( ':focus' );
6421 this.$input
.focus();
6423 if ( this.newItems
&& this.newItems
.length
) {
6424 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
6425 this.newItems
[i
].fitLabel();
6427 this.newItems
= null;
6430 this.setClipping( true );
6442 OO
.ui
.MenuWidget
.prototype.hide = function () {
6443 this.$element
.hide();
6444 this.visible
= false;
6445 this.unbindKeyDownListener();
6447 if ( this.isolated
&& this.$previousFocus
) {
6448 this.$previousFocus
.focus();
6449 this.$previousFocus
= null;
6452 this.setClipping( false );
6457 * Inline menu of options.
6460 * @extends OO.ui.Widget
6461 * @mixins OO.ui.IconedElement
6462 * @mixins OO.ui.IndicatedElement
6463 * @mixins OO.ui.LabeledElement
6464 * @mixins OO.ui.TitledElement
6467 * @param {Object} [config] Configuration options
6468 * @cfg {Object} [menu] Configuration options to pass to menu widget
6470 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
6471 // Configuration initialization
6472 config
= $.extend( { 'indicator': 'down' }, config
);
6474 // Parent constructor
6475 OO
.ui
.Widget
.call( this, config
);
6477 // Mixin constructors
6478 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6479 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6480 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6481 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
6484 this.menu
= new OO
.ui
.MenuWidget( $.extend( { '$': this.$ }, config
.menu
) );
6485 this.$handle
= this.$( '<span>' );
6488 this.$element
.on( { 'click': OO
.ui
.bind( this.onClick
, this ) } );
6489 this.menu
.connect( this, { 'select': 'onMenuSelect' } );
6493 .addClass( 'oo-ui-inlineMenuWidget-handle' )
6494 .append( this.$icon
, this.$label
, this.$indicator
);
6496 .addClass( 'oo-ui-inlineMenuWidget' )
6497 .append( this.$handle
, this.menu
.$element
);
6502 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
6504 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
6505 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
6506 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
6507 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
6514 * @return {OO.ui.MenuWidget} Menu of widget
6516 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
6521 * Handles menu select events.
6524 * @param {OO.ui.MenuItemWidget} item Selected menu item
6526 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
6527 this.setLabel( item
.getLabel() );
6531 * Handles mouse click events.
6534 * @param {jQuery.Event} e Mouse click event
6536 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
6537 // Skip clicks within the menu
6538 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
6542 if ( !this.disabled
) {
6543 if ( this.menu
.isVisible() ) {
6552 * Creates an OO.ui.MenuSectionItemWidget object.
6555 * @extends OO.ui.OptionWidget
6558 * @param {Mixed} data Item data
6559 * @param {Object} [config] Configuration options
6561 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
6562 // Parent constructor
6563 OO
.ui
.OptionWidget
.call( this, data
, config
);
6566 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
6571 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
6573 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
6575 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
6577 * Create an OO.ui.OutlineWidget object.
6580 * @extends OO.ui.SelectWidget
6583 * @param {Object} [config] Configuration options
6585 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
6586 // Config intialization
6587 config
= config
|| {};
6589 // Parent constructor
6590 OO
.ui
.SelectWidget
.call( this, config
);
6593 this.$element
.addClass( 'oo-ui-outlineWidget' );
6598 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
6600 * Creates an OO.ui.OutlineControlsWidget object.
6605 * @param {OO.ui.OutlineWidget} outline Outline to control
6606 * @param {Object} [config] Configuration options
6608 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
6609 // Configuration initialization
6610 config
= $.extend( { 'icon': 'add-item' }, config
);
6612 // Parent constructor
6613 OO
.ui
.Widget
.call( this, config
);
6615 // Mixin constructors
6616 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
6617 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
6620 this.outline
= outline
;
6621 this.$movers
= this.$( '<div>' );
6622 this.upButton
= new OO
.ui
.ButtonWidget( {
6626 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
6628 this.downButton
= new OO
.ui
.ButtonWidget( {
6632 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
6634 this.removeButton
= new OO
.ui
.ButtonWidget( {
6638 'title': OO
.ui
.msg( 'ooui-outline-control-remove' )
6642 outline
.connect( this, {
6643 'select': 'onOutlineChange',
6644 'add': 'onOutlineChange',
6645 'remove': 'onOutlineChange'
6647 this.upButton
.connect( this, { 'click': ['emit', 'move', -1] } );
6648 this.downButton
.connect( this, { 'click': ['emit', 'move', 1] } );
6649 this.removeButton
.connect( this, { 'click': ['emit', 'remove'] } );
6652 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
6653 this.$group
.addClass( 'oo-ui-outlineControlsWidget-adders' );
6655 .addClass( 'oo-ui-outlineControlsWidget-movers' )
6656 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
6657 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
6662 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
6664 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
6665 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
6671 * @param {number} places Number of places to move
6681 * Handle outline change events.
6685 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
6686 var i
, len
, firstMovable
, lastMovable
,
6687 items
= this.outline
.getItems(),
6688 selectedItem
= this.outline
.getSelectedItem(),
6689 movable
= selectedItem
&& selectedItem
.isMovable(),
6690 removable
= selectedItem
&& selectedItem
.isRemovable();
6695 while ( ++i
< len
) {
6696 if ( items
[i
].isMovable() ) {
6697 firstMovable
= items
[i
];
6703 if ( items
[i
].isMovable() ) {
6704 lastMovable
= items
[i
];
6709 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
6710 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
6711 this.removeButton
.setDisabled( !removable
);
6714 * Creates an OO.ui.OutlineItemWidget object.
6717 * @extends OO.ui.OptionWidget
6720 * @param {Mixed} data Item data
6721 * @param {Object} [config] Configuration options
6722 * @cfg {number} [level] Indentation level
6723 * @cfg {boolean} [movable] Allow modification from outline controls
6725 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
6726 // Config intialization
6727 config
= config
|| {};
6729 // Parent constructor
6730 OO
.ui
.OptionWidget
.call( this, data
, config
);
6734 this.movable
= !!config
.movable
;
6735 this.removable
= !!config
.removable
;
6738 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
6739 this.setLevel( config
.level
);
6744 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
6746 /* Static Properties */
6748 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
6750 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
6752 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
6754 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
6759 * Check if item is movable.
6761 * Movablilty is used by outline controls.
6763 * @returns {boolean} Item is movable
6765 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
6766 return this.movable
;
6770 * Check if item is removable.
6772 * Removablilty is used by outline controls.
6774 * @returns {boolean} Item is removable
6776 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
6777 return this.removable
;
6781 * Get indentation level.
6783 * @returns {number} Indentation level
6785 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
6792 * Movablilty is used by outline controls.
6794 * @param {boolean} movable Item is movable
6797 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
6798 this.movable
= !!movable
;
6805 * Removablilty is used by outline controls.
6807 * @param {boolean} movable Item is removable
6810 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
6811 this.removable
= !!removable
;
6816 * Set indentation level.
6819 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
6822 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
6823 var levels
= this.constructor.static.levels
,
6824 levelClass
= this.constructor.static.levelClass
,
6827 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
6829 if ( this.level
=== i
) {
6830 this.$element
.addClass( levelClass
+ i
);
6832 this.$element
.removeClass( levelClass
+ i
);
6839 * Create an OO.ui.ButtonSelect object.
6842 * @extends OO.ui.OptionWidget
6843 * @mixins OO.ui.ButtonedElement
6844 * @mixins OO.ui.FlaggableElement
6847 * @param {Mixed} data Option data
6848 * @param {Object} [config] Configuration options
6850 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
6851 // Parent constructor
6852 OO
.ui
.OptionWidget
.call( this, data
, config
);
6854 // Mixin constructors
6855 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
6856 OO
.ui
.FlaggableElement
.call( this, config
);
6859 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
6860 this.$button
.append( this.$element
.contents() );
6861 this.$element
.append( this.$button
);
6866 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
6868 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
6869 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
6876 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
6877 OO
.ui
.OptionWidget
.prototype.setSelected
.call( this, state
);
6879 this.setActive( state
);
6884 * Create an OO.ui.ButtonSelect object.
6887 * @extends OO.ui.SelectWidget
6890 * @param {Object} [config] Configuration options
6892 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
6893 // Parent constructor
6894 OO
.ui
.SelectWidget
.call( this, config
);
6897 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
6902 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
6904 * Creates an OO.ui.PopupWidget object.
6907 * @extends OO.ui.Widget
6908 * @mixins OO.ui.LabeledElement
6911 * @param {Object} [config] Configuration options
6912 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
6913 * @cfg {string} [align='center'] Alignment of popup to origin
6914 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
6915 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
6916 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
6917 * @cfg {boolean} [head] Show label and close button at the top
6919 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
6920 // Config intialization
6921 config
= config
|| {};
6923 // Parent constructor
6924 OO
.ui
.Widget
.call( this, config
);
6926 // Mixin constructors
6927 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
6930 this.visible
= false;
6931 this.$popup
= this.$( '<div>' );
6932 this.$head
= this.$( '<div>' );
6933 this.$body
= this.$( '<div>' );
6934 this.$tail
= this.$( '<div>' );
6935 this.$container
= config
.$container
|| this.$( 'body' );
6936 this.autoClose
= !!config
.autoClose
;
6937 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
6938 this.transitionTimeout
= null;
6940 this.align
= config
.align
|| 'center';
6941 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
6942 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
6945 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
6948 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
6949 this.$body
.addClass( 'oo-ui-popupWidget-body' );
6950 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
6952 .addClass( 'oo-ui-popupWidget-head' )
6953 .append( this.$label
, this.closeButton
.$element
);
6954 if ( !config
.head
) {
6958 .addClass( 'oo-ui-popupWidget-popup' )
6959 .append( this.$head
, this.$body
);
6960 this.$element
.hide()
6961 .addClass( 'oo-ui-popupWidget' )
6962 .append( this.$popup
, this.$tail
);
6967 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
6969 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
6984 * Handles mouse down events.
6987 * @param {jQuery.Event} e Mouse down event
6989 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
6992 !$.contains( this.$element
[0], e
.target
) &&
6993 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
7000 * Bind mouse down listener
7004 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
7005 // Capture clicks outside popup
7006 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
7010 * Handles close button click events.
7014 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
7015 if ( this.visible
) {
7021 * Unbind mouse down listener
7025 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
7026 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
7030 * Check if the popup is visible.
7033 * @returns {boolean} Popup is visible
7035 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
7036 return this.visible
;
7040 * Set whether to show a tail.
7043 * @returns {boolean} Make tail visible
7045 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
7047 if ( this.tail
!== value
) {
7050 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
7052 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
7058 * Check if showing a tail.
7061 * @returns {boolean} tail is visible
7063 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
7074 OO
.ui
.PopupWidget
.prototype.show = function () {
7075 if ( !this.visible
) {
7076 this.$element
.show();
7077 this.visible
= true;
7078 this.emit( 'show' );
7079 if ( this.autoClose
) {
7080 this.bindMouseDownListener();
7093 OO
.ui
.PopupWidget
.prototype.hide = function () {
7094 if ( this.visible
) {
7095 this.$element
.hide();
7096 this.visible
= false;
7097 this.emit( 'hide' );
7098 if ( this.autoClose
) {
7099 this.unbindMouseDownListener();
7106 * Updates the position and size.
7109 * @param {number} width Width
7110 * @param {number} height Height
7111 * @param {boolean} [transition=false] Use a smooth transition
7114 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
7116 originOffset
= Math
.round( this.$element
.offset().left
),
7117 containerLeft
= Math
.round( this.$container
.offset().left
),
7118 containerWidth
= this.$container
.innerWidth(),
7119 containerRight
= containerLeft
+ containerWidth
,
7120 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
7121 popupLeft
= popupOffset
- padding
,
7122 popupRight
= popupOffset
+ padding
+ width
+ padding
,
7123 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
7124 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
7126 // Prevent transition from being interrupted
7127 clearTimeout( this.transitionTimeout
);
7129 // Enable transition
7130 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
7133 if ( overlapRight
< 0 ) {
7134 popupOffset
+= overlapRight
;
7135 } else if ( overlapLeft
< 0 ) {
7136 popupOffset
-= overlapLeft
;
7139 // Position body relative to anchor and resize
7141 'left': popupOffset
,
7143 'height': height
=== undefined ? 'auto' : height
7147 // Prevent transitioning after transition is complete
7148 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
7149 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7152 // Prevent transitioning immediately
7153 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7159 * Button that shows and hides a popup.
7162 * @extends OO.ui.ButtonWidget
7163 * @mixins OO.ui.PopuppableElement
7166 * @param {Object} [config] Configuration options
7168 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7169 // Parent constructor
7170 OO
.ui
.ButtonWidget
.call( this, config
);
7172 // Mixin constructors
7173 OO
.ui
.PopuppableElement
.call( this, config
);
7177 .addClass( 'oo-ui-popupButtonWidget' )
7178 .append( this.popup
.$element
);
7183 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
7185 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
7190 * Handles mouse click events.
7193 * @param {jQuery.Event} e Mouse click event
7195 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
7196 // Skip clicks within the popup
7197 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
7201 if ( !this.disabled
) {
7202 if ( this.popup
.isVisible() ) {
7207 OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
7212 * Creates an OO.ui.SearchWidget object.
7215 * @extends OO.ui.Widget
7218 * @param {Object} [config] Configuration options
7219 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
7220 * @cfg {string} [value] Initial query value
7222 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
7223 // Configuration intialization
7224 config
= config
|| {};
7226 // Parent constructor
7227 OO
.ui
.Widget
.call( this, config
);
7230 this.query
= new OO
.ui
.TextInputWidget( {
7233 'placeholder': config
.placeholder
,
7234 'value': config
.value
7236 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
7237 this.$query
= this.$( '<div>' );
7238 this.$results
= this.$( '<div>' );
7241 this.query
.connect( this, {
7242 'change': 'onQueryChange',
7243 'enter': 'onQueryEnter'
7245 this.results
.connect( this, {
7246 'highlight': 'onResultsHighlight',
7247 'select': 'onResultsSelect'
7249 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
7253 .addClass( 'oo-ui-searchWidget-query' )
7254 .append( this.query
.$element
);
7256 .addClass( 'oo-ui-searchWidget-results' )
7257 .append( this.results
.$element
);
7259 .addClass( 'oo-ui-searchWidget' )
7260 .append( this.$results
, this.$query
);
7265 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
7271 * @param {Object|null} item Item data or null if no item is highlighted
7276 * @param {Object|null} item Item data or null if no item is selected
7282 * Handle query key down events.
7285 * @param {jQuery.Event} e Key down event
7287 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
7288 var highlightedItem
, nextItem
,
7289 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
7292 highlightedItem
= this.results
.getHighlightedItem();
7293 if ( !highlightedItem
) {
7294 highlightedItem
= this.results
.getSelectedItem();
7296 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
7297 this.results
.highlightItem( nextItem
);
7298 nextItem
.scrollElementIntoView();
7303 * Handle select widget select events.
7305 * Clears existing results. Subclasses should repopulate items according to new query.
7308 * @param {string} value New value
7310 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
7312 this.results
.clearItems();
7316 * Handle select widget enter key events.
7318 * Selects highlighted item.
7321 * @param {string} value New value
7323 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
7325 this.results
.selectItem( this.results
.getHighlightedItem() );
7329 * Handle select widget highlight events.
7332 * @param {OO.ui.OptionWidget} item Highlighted item
7335 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
7336 this.emit( 'highlight', item
? item
.getData() : null );
7340 * Handle select widget select events.
7343 * @param {OO.ui.OptionWidget} item Selected item
7346 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
7347 this.emit( 'select', item
? item
.getData() : null );
7351 * Get the query input.
7354 * @returns {OO.ui.TextInputWidget} Query input
7356 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
7361 * Get the results list.
7364 * @returns {OO.ui.SelectWidget} Select list
7366 OO
.ui
.SearchWidget
.prototype.getResults = function () {
7367 return this.results
;
7370 * Creates an OO.ui.TextInputWidget object.
7373 * @extends OO.ui.InputWidget
7376 * @param {Object} [config] Configuration options
7377 * @cfg {string} [placeholder] Placeholder text
7378 * @cfg {string} [icon] Symbolic name of icon
7379 * @cfg {boolean} [multiline=false] Allow multiple lines of text
7380 * @cfg {boolean} [autosize=false] Automatically resize to fit content
7381 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
7383 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
7384 config
= $.extend( { 'maxRows': 10 }, config
);
7386 // Parent constructor
7387 OO
.ui
.InputWidget
.call( this, config
);
7391 this.multiline
= !!config
.multiline
;
7392 this.autosize
= !!config
.autosize
;
7393 this.maxRows
= config
.maxRows
;
7396 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
7397 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
7400 this.$element
.addClass( 'oo-ui-textInputWidget' );
7401 if ( config
.icon
) {
7402 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
7403 this.$element
.append(
7405 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
7406 .mousedown( OO
.ui
.bind( function () {
7407 this.$input
.focus();
7412 if ( config
.placeholder
) {
7413 this.$input
.attr( 'placeholder', config
.placeholder
);
7419 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
7424 * User presses enter inside the text box.
7426 * Not called if input is multiline.
7434 * Handles key press events.
7436 * @param {jQuery.Event} e Key press event
7437 * @fires enter If enter key is pressed and input is not multiline
7439 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
7440 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
7441 this.emit( 'enter' );
7446 * Handles element attach events.
7448 * @param {jQuery.Event} e Element attach event
7450 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
7457 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
7461 return OO
.ui
.InputWidget
.prototype.onEdit
.call( this );
7465 * Automatically adjust the size of the text input.
7467 * This only affects multi-line inputs that are auto-sized.
7471 OO
.ui
.TextInputWidget
.prototype.adjustSize = function() {
7472 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
7474 if ( this.multiline
&& this.autosize
) {
7475 $clone
= this.$input
.clone()
7476 .val( this.$input
.val() )
7477 .css( { 'height': 0 } )
7478 .insertAfter( this.$input
);
7479 // Set inline height property to 0 to measure scroll height
7480 scrollHeight
= $clone
[0].scrollHeight
;
7481 // Remove inline height property to measure natural heights
7482 $clone
.css( 'height', '' );
7483 innerHeight
= $clone
.innerHeight();
7484 outerHeight
= $clone
.outerHeight();
7485 // Measure max rows height
7486 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
7487 maxInnerHeight
= $clone
.innerHeight();
7488 $clone
.removeAttr( 'rows' ).css( 'height', '' );
7490 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
7491 // Only apply inline height when expansion beyond natural height is needed
7494 // Use the difference between the inner and outer height as a buffer
7495 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
7502 * Get input element.
7505 * @param {Object} [config] Configuration options
7506 * @returns {jQuery} Input element
7508 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
7509 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
7515 * Checks if input supports multiple lines.
7518 * @returns {boolean} Input supports multiple lines
7520 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
7521 return !!this.multiline
;
7525 * Checks if input automatically adjusts its size.
7528 * @returns {boolean} Input automatically adjusts its size
7530 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
7531 return !!this.autosize
;
7535 * Checks if input is pending.
7538 * @returns {boolean} Input is pending
7540 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
7541 return !!this.pending
;
7545 * Increases the pending stack.
7550 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
7552 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
7553 this.$input
.addClass( 'oo-ui-texture-pending' );
7558 * Reduces the pending stack.
7565 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
7566 this.pending
= Math
.max( 0, this.pending
- 1 );
7567 if ( !this.pending
) {
7568 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
7569 this.$input
.removeClass( 'oo-ui-texture-pending' );
7574 * Creates an OO.ui.TextInputMenuWidget object.
7577 * @extends OO.ui.MenuWidget
7580 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
7581 * @param {Object} [config] Configuration options
7582 * @cfg {jQuery} [$container=input.$element] Element to render menu under
7584 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
7585 // Parent constructor
7586 OO
.ui
.MenuWidget
.call( this, config
);
7590 this.$container
= config
.$container
|| this.input
.$element
;
7591 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
7594 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
7599 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
7604 * Handle window resize event.
7607 * @param {jQuery.Event} e Window resize event
7609 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
7619 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
7621 OO
.ui
.MenuWidget
.prototype.show
.call( this );
7624 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
7634 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
7636 OO
.ui
.MenuWidget
.prototype.hide
.call( this );
7638 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
7643 * Positions the menu.
7648 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
7650 $container
= this.$container
,
7651 dimensions
= $container
.offset();
7653 // Position under input
7654 dimensions
.top
+= $container
.height();
7656 // Compensate for frame position if in a differnt frame
7657 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
7658 frameOffset
= OO
.ui
.Element
.getRelativePosition(
7659 this.input
.$.frame
.$element
, this.$element
.offsetParent()
7661 dimensions
.left
+= frameOffset
.left
;
7662 dimensions
.top
+= frameOffset
.top
;
7664 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
7665 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
7666 dimensions
.right
= this.$element
.parent().position().left
-
7667 dimensions
.width
- dimensions
.left
;
7668 // Erase the value for 'left':
7669 delete dimensions
.left
;
7673 this.$element
.css( dimensions
);
7674 this.setIdealSize( $container
.width() );
7678 * Mixin for widgets with a boolean state.
7684 * @param {Object} [config] Configuration options
7685 * @cfg {boolean} [value=false] Initial value
7687 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7688 // Configuration initialization
7689 config
= config
|| {};
7695 this.$element
.addClass( 'oo-ui-toggleWidget' );
7696 this.setValue( !!config
.value
);
7703 * @param {boolean} value Changed value
7709 * Get the value of the toggle.
7712 * @returns {boolean} Toggle value
7714 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7719 * Set the value of the toggle.
7722 * @param {boolean} value New value
7726 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7728 if ( this.value
!== value
) {
7730 this.emit( 'change', value
);
7731 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7732 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7738 * @extends OO.ui.ButtonWidget
7739 * @mixins OO.ui.ToggleWidget
7742 * @param {Object} [config] Configuration options
7743 * @cfg {boolean} [value=false] Initial value
7745 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
7746 // Configuration initialization
7747 config
= config
|| {};
7749 // Parent constructor
7750 OO
.ui
.ButtonWidget
.call( this, config
);
7752 // Mixin constructors
7753 OO
.ui
.ToggleWidget
.call( this, config
);
7756 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
7761 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
7763 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
7770 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
7771 if ( !this.disabled
) {
7772 this.setValue( !this.value
);
7776 return OO
.ui
.ButtonWidget
.prototype.onClick
.call( this );
7782 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
7784 if ( value
!== this.value
) {
7785 this.setActive( value
);
7789 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
7796 * @extends OO.ui.Widget
7797 * @mixins OO.ui.ToggleWidget
7800 * @param {Object} [config] Configuration options
7801 * @cfg {boolean} [value=false] Initial value
7803 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
7804 // Parent constructor
7805 OO
.ui
.Widget
.call( this, config
);
7807 // Mixin constructors
7808 OO
.ui
.ToggleWidget
.call( this, config
);
7811 this.dragging
= false;
7812 this.dragStart
= null;
7813 this.sliding
= false;
7814 this.$glow
= this.$( '<span>' );
7815 this.$grip
= this.$( '<span>' );
7818 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
7821 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
7822 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
7824 .addClass( 'oo-ui-toggleSwitchWidget' )
7825 .append( this.$glow
, this.$grip
);
7830 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
7832 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
7837 * Handles mouse down events.
7840 * @param {jQuery.Event} e Mouse down event
7842 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
7843 if ( !this.disabled
&& e
.which
=== 1 ) {
7844 this.setValue( !this.value
);