2 * OOjs UI v0.1.0-pre (098f84f8a0)
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: Mon Jun 09 2014 14:32:26 GMT-0700 (PDT)
15 * Namespace for all classes, static methods and static properties.
47 * Get the user's language and any fallback languages.
49 * These language codes are used to localize user interface elements in the user's language.
51 * In environments that provide a localization system, this function should be overridden to
52 * return the user's language(s). The default implementation returns English (en) only.
54 * @return {string[]} Language codes, in descending order of priority
56 OO
.ui
.getUserLanguages = function () {
61 * Get a value in an object keyed by language code.
63 * @param {Object.<string,Mixed>} obj Object keyed by language code
64 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
65 * @param {string} [fallback] Fallback code, used if no matching language can be found
66 * @return {Mixed} Local value
68 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
75 // Known user language
76 langs
= OO
.ui
.getUserLanguages();
77 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
84 if ( obj
[fallback
] ) {
87 // First existing language
98 * Message store for the default implementation of OO.ui.msg
100 * Environments that provide a localization system should not use this, but should override
101 * OO.ui.msg altogether.
106 // Label text for button to exit from dialog
107 'ooui-dialog-action-close': 'Close',
108 // Tool tip for a button that moves items in a list down one place
109 'ooui-outline-control-move-down': 'Move item down',
110 // Tool tip for a button that moves items in a list up one place
111 'ooui-outline-control-move-up': 'Move item up',
112 // Tool tip for a button that removes items from a list
113 'ooui-outline-control-remove': 'Remove item',
114 // Label for the toolbar group that contains a list of all other available tools
115 'ooui-toolbar-more': 'More',
117 // Label for the generic dialog used to confirm things
118 'ooui-dialog-confirm-title': 'Confirm',
119 // The default prompt of a confirmation dialog
120 'ooui-dialog-confirm-default-prompt': 'Are you sure?',
121 // The default OK button text on a confirmation dialog
122 'ooui-dialog-confirm-default-ok': 'OK',
123 // The default cancel button text on a confirmation dialog
124 'ooui-dialog-confirm-default-cancel': 'Cancel'
128 * Get a localized message.
130 * In environments that provide a localization system, this function should be overridden to
131 * return the message translated in the user's language. The default implementation always returns
134 * After the message key, message parameters may optionally be passed. In the default implementation,
135 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
136 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
137 * they support unnamed, ordered message parameters.
140 * @param {string} key Message key
141 * @param {Mixed...} [params] Message parameters
142 * @return {string} Translated message with parameters substituted
144 OO
.ui
.msg = function ( key
) {
145 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
146 if ( typeof message
=== 'string' ) {
147 // Perform $1 substitution
148 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
149 var i
= parseInt( n
, 10 );
150 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
153 // Return placeholder if message not found
154 message
= '[' + key
+ ']';
160 OO
.ui
.deferMsg = function ( key
) {
162 return OO
.ui
.msg( key
);
167 OO
.ui
.resolveMsg = function ( msg
) {
168 if ( $.isFunction( msg
) ) {
176 * DOM element abstraction.
182 * @param {Object} [config] Configuration options
183 * @cfg {Function} [$] jQuery for the frame the widget is in
184 * @cfg {string[]} [classes] CSS class names
185 * @cfg {string} [text] Text to insert
186 * @cfg {jQuery} [$content] Content elements to append (after text)
188 OO
.ui
.Element
= function OoUiElement( config
) {
189 // Configuration initialization
190 config
= config
|| {};
193 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
194 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
195 this.elementGroup
= null;
198 if ( $.isArray( config
.classes
) ) {
199 this.$element
.addClass( config
.classes
.join( ' ' ) );
202 this.$element
.text( config
.text
);
204 if ( config
.$content
) {
205 this.$element
.append( config
.$content
);
211 OO
.initClass( OO
.ui
.Element
);
213 /* Static Properties */
218 * This may be ignored if getTagName is overridden.
224 OO
.ui
.Element
.static.tagName
= 'div';
229 * Get a jQuery function within a specific document.
232 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
233 * @param {OO.ui.Frame} [frame] Frame of the document context
234 * @return {Function} Bound jQuery function
236 OO
.ui
.Element
.getJQuery = function ( context
, frame
) {
237 function wrapper( selector
) {
238 return $( selector
, wrapper
.context
);
241 wrapper
.context
= this.getDocument( context
);
244 wrapper
.frame
= frame
;
251 * Get the document of an element.
254 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
255 * @return {HTMLDocument|null} Document object
257 OO
.ui
.Element
.getDocument = function ( obj
) {
258 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
259 return ( obj
[0] && obj
[0].ownerDocument
) ||
260 // Empty jQuery selections might have a context
267 ( obj
.nodeType
=== 9 && obj
) ||
272 * Get the window of an element or document.
275 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
276 * @return {Window} Window object
278 OO
.ui
.Element
.getWindow = function ( obj
) {
279 var doc
= this.getDocument( obj
);
280 return doc
.parentWindow
|| doc
.defaultView
;
284 * Get the direction of an element or document.
287 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
288 * @return {string} Text direction, either `ltr` or `rtl`
290 OO
.ui
.Element
.getDir = function ( obj
) {
293 if ( obj
instanceof jQuery
) {
296 isDoc
= obj
.nodeType
=== 9;
297 isWin
= obj
.document
!== undefined;
298 if ( isDoc
|| isWin
) {
304 return $( obj
).css( 'direction' );
308 * Get the offset between two frames.
310 * TODO: Make this function not use recursion.
313 * @param {Window} from Window of the child frame
314 * @param {Window} [to=window] Window of the parent frame
315 * @param {Object} [offset] Offset to start with, used internally
316 * @return {Object} Offset object, containing left and top properties
318 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
319 var i
, len
, frames
, frame
, rect
;
325 offset
= { 'top': 0, 'left': 0 };
327 if ( from.parent
=== from ) {
331 // Get iframe element
332 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
333 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
334 if ( frames
[i
].contentWindow
=== from ) {
340 // Recursively accumulate offset values
342 rect
= frame
.getBoundingClientRect();
343 offset
.left
+= rect
.left
;
344 offset
.top
+= rect
.top
;
346 this.getFrameOffset( from.parent
, offset
);
353 * Get the offset between two elements.
356 * @param {jQuery} $from
357 * @param {jQuery} $to
358 * @return {Object} Translated position coordinates, containing top and left properties
360 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
361 var from = $from.offset(),
363 return { 'top': Math
.round( from.top
- to
.top
), 'left': Math
.round( from.left
- to
.left
) };
367 * Get element border sizes.
370 * @param {HTMLElement} el Element to measure
371 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
373 OO
.ui
.Element
.getBorders = function ( el
) {
374 var doc
= el
.ownerDocument
,
375 win
= doc
.parentWindow
|| doc
.defaultView
,
376 style
= win
&& win
.getComputedStyle
?
377 win
.getComputedStyle( el
, null ) :
380 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
381 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
382 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
383 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
386 'top': Math
.round( top
),
387 'left': Math
.round( left
),
388 'bottom': Math
.round( bottom
),
389 'right': Math
.round( right
)
394 * Get dimensions of an element or window.
397 * @param {HTMLElement|Window} el Element to measure
398 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
400 OO
.ui
.Element
.getDimensions = function ( el
) {
402 doc
= el
.ownerDocument
|| el
.document
,
403 win
= doc
.parentWindow
|| doc
.defaultView
;
405 if ( win
=== el
|| el
=== doc
.documentElement
) {
408 'borders': { 'top': 0, 'left': 0, 'bottom': 0, 'right': 0 },
410 'top': $win
.scrollTop(),
411 'left': $win
.scrollLeft()
413 'scrollbar': { 'right': 0, 'bottom': 0 },
417 'bottom': $win
.innerHeight(),
418 'right': $win
.innerWidth()
424 'borders': this.getBorders( el
),
426 'top': $el
.scrollTop(),
427 'left': $el
.scrollLeft()
430 'right': $el
.innerWidth() - el
.clientWidth
,
431 'bottom': $el
.innerHeight() - el
.clientHeight
433 'rect': el
.getBoundingClientRect()
439 * Get closest scrollable container.
441 * Traverses up until either a scrollable element or the root is reached, in which case the window
445 * @param {HTMLElement} el Element to find scrollable container for
446 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
447 * @return {HTMLElement|Window} Closest scrollable container
449 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
451 props
= [ 'overflow' ],
452 $parent
= $( el
).parent();
454 if ( dimension
=== 'x' || dimension
=== 'y' ) {
455 props
.push( 'overflow-' + dimension
);
458 while ( $parent
.length
) {
459 if ( $parent
[0] === el
.ownerDocument
.body
) {
464 val
= $parent
.css( props
[i
] );
465 if ( val
=== 'auto' || val
=== 'scroll' ) {
469 $parent
= $parent
.parent();
471 return this.getDocument( el
).body
;
475 * Scroll element into view.
478 * @param {HTMLElement} el Element to scroll into view
479 * @param {Object} [config={}] Configuration config
480 * @param {string} [config.duration] jQuery animation duration value
481 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
482 * to scroll in both directions
483 * @param {Function} [config.complete] Function to call when scrolling completes
485 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
486 // Configuration initialization
487 config
= config
|| {};
490 callback
= typeof config
.complete
=== 'function' && config
.complete
,
491 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
493 eld
= this.getDimensions( el
),
494 scd
= this.getDimensions( sc
),
496 'top': eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
497 'bottom': scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
498 'left': eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
499 'right': scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
502 if ( !config
.direction
|| config
.direction
=== 'y' ) {
504 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
505 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
506 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
509 if ( !config
.direction
|| config
.direction
=== 'x' ) {
510 if ( rel
.left
< 0 ) {
511 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
512 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
513 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
516 if ( !$.isEmptyObject( anim
) ) {
517 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
519 $sc
.queue( function ( next
) {
534 * Get the HTML tag name.
536 * Override this method to base the result on instance information.
538 * @return {string} HTML tag name
540 OO
.ui
.Element
.prototype.getTagName = function () {
541 return this.constructor.static.tagName
;
545 * Check if the element is attached to the DOM
546 * @return {boolean} The element is attached to the DOM
548 OO
.ui
.Element
.prototype.isElementAttached = function () {
549 return $.contains( this.getElementDocument(), this.$element
[0] );
553 * Get the DOM document.
555 * @return {HTMLDocument} Document object
557 OO
.ui
.Element
.prototype.getElementDocument = function () {
558 return OO
.ui
.Element
.getDocument( this.$element
);
562 * Get the DOM window.
564 * @return {Window} Window object
566 OO
.ui
.Element
.prototype.getElementWindow = function () {
567 return OO
.ui
.Element
.getWindow( this.$element
);
571 * Get closest scrollable container.
573 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
574 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
578 * Get group element is in.
580 * @return {OO.ui.GroupElement|null} Group element, null if none
582 OO
.ui
.Element
.prototype.getElementGroup = function () {
583 return this.elementGroup
;
587 * Set group element is in.
589 * @param {OO.ui.GroupElement|null} group Group element, null if none
592 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
593 this.elementGroup
= group
;
598 * Scroll element into view.
600 * @param {Object} [config={}]
602 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
603 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
607 * Bind a handler for an event on this.$element
609 * @param {string} event
610 * @param {Function} callback
612 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
613 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
617 * Unbind a handler bound with #offDOMEvent
619 * @param {string} event
620 * @param {Function} callback
622 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
623 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
629 // jQuery 1.8.3 has a bug with handling focusin/focusout events inside iframes.
630 // Firefox doesn't support focusin/focusout at all, so we listen for 'focus'/'blur' on the
631 // document, and simulate a 'focusin'/'focusout' event on the target element and make
632 // it bubble from there.
634 // - http://jsfiddle.net/sw3hr/
635 // - http://bugs.jquery.com/ticket/14180
636 // - https://github.com/jquery/jquery/commit/1cecf64e5aa4153
637 function specialEvent( simulatedName
, realName
) {
638 function handler( e
) {
639 jQuery
.event
.simulate(
642 jQuery
.event
.fix( e
),
649 var doc
= this.ownerDocument
|| this,
650 attaches
= $.data( doc
, 'ooui-' + simulatedName
+ '-attaches' );
652 doc
.addEventListener( realName
, handler
, true );
654 $.data( doc
, 'ooui-' + simulatedName
+ '-attaches', ( attaches
|| 0 ) + 1 );
656 teardown: function () {
657 var doc
= this.ownerDocument
|| this,
658 attaches
= $.data( doc
, 'ooui-' + simulatedName
+ '-attaches' ) - 1;
660 doc
.removeEventListener( realName
, handler
, true );
661 $.removeData( doc
, 'ooui-' + simulatedName
+ '-attaches' );
663 $.data( doc
, 'ooui-' + simulatedName
+ '-attaches', attaches
);
669 var hasOwn
= Object
.prototype.hasOwnProperty
,
671 focusin
: specialEvent( 'focusin', 'focus' ),
672 focusout
: specialEvent( 'focusout', 'blur' )
676 * Bind a handler for an event on a DOM element.
678 * Uses jQuery internally for everything except for events which are
679 * known to have issues in the browser or in jQuery. This method
680 * should become obsolete eventually.
683 * @param {HTMLElement|jQuery} el DOM element
684 * @param {string} event Event to bind
685 * @param {Function} callback Callback to call when the event fires
687 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
690 if ( hasOwn
.call( specialEvents
, event
) ) {
691 // Replace jQuery's override with our own
692 orig
= $.event
.special
[event
];
693 $.event
.special
[event
] = specialEvents
[event
];
695 $( el
).on( event
, callback
);
698 $.event
.special
[event
] = orig
;
700 $( el
).on( event
, callback
);
705 * Unbind a handler bound with #static-method-onDOMEvent.
708 * @param {HTMLElement|jQuery} el DOM element
709 * @param {string} event Event to unbind
710 * @param {Function} [callback] Callback to unbind
712 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
714 if ( hasOwn
.call( specialEvents
, event
) ) {
715 // Replace jQuery's override with our own
716 orig
= $.event
.special
[event
];
717 $.event
.special
[event
] = specialEvents
[event
];
719 $( el
).off( event
, callback
);
722 $.event
.special
[event
] = orig
;
724 $( el
).off( event
, callback
);
729 * Embedded iframe with the same styles as its parent.
732 * @extends OO.ui.Element
733 * @mixins OO.EventEmitter
736 * @param {Object} [config] Configuration options
738 OO
.ui
.Frame
= function OoUiFrame( config
) {
739 // Parent constructor
740 OO
.ui
.Frame
.super.call( this, config
);
742 // Mixin constructors
743 OO
.EventEmitter
.call( this );
747 this.config
= config
;
751 .addClass( 'oo-ui-frame' )
752 .attr( { 'frameborder': 0, 'scrolling': 'no' } );
758 OO
.inheritClass( OO
.ui
.Frame
, OO
.ui
.Element
);
759 OO
.mixinClass( OO
.ui
.Frame
, OO
.EventEmitter
);
761 /* Static Properties */
767 OO
.ui
.Frame
.static.tagName
= 'iframe';
778 * Transplant the CSS styles from as parent document to a frame's document.
780 * This loops over the style sheets in the parent document, and copies their nodes to the
781 * frame's document. It then polls the document to see when all styles have loaded, and once they
782 * have, resolves the promise.
784 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
785 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
786 * Firefox, where the styles won't load until the iframe becomes visible.
788 * For details of how we arrived at the strategy used in this function, see #load.
792 * @param {HTMLDocument} parentDoc Document to transplant styles from
793 * @param {HTMLDocument} frameDoc Document to transplant styles to
794 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
795 * @return {jQuery.Promise} Promise resolved when styles have loaded
797 OO
.ui
.Frame
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
798 var i
, numSheets
, styleNode
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
799 $pollNodes
= $( [] ),
800 // Fake font-family value
801 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
802 deferred
= $.Deferred();
804 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
805 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
806 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
807 // External stylesheet
808 // Create a node with a unique ID that we're going to monitor to see when the CSS
810 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + i
;
811 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
812 .attr( 'id', pollNodeId
)
813 .appendTo( frameDoc
.body
)
816 // Add <style>@import url(...); #pollNodeId { font-family: ... }</style>
817 // The font-family rule will only take effect once the @import finishes
818 newNode
= frameDoc
.createElement( 'style' );
819 newNode
.textContent
= '@import url(' + styleNode
.href
+ ');\n' +
820 '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
822 // Not an external stylesheet, or no polling required; just copy the node over
823 newNode
= frameDoc
.importNode( styleNode
, true );
825 frameDoc
.head
.appendChild( newNode
);
828 // Poll every 100ms until all external stylesheets have loaded
829 $pendingPollNodes
= $pollNodes
;
830 timeoutID
= setTimeout( function pollExternalStylesheets() {
832 $pendingPollNodes
.length
> 0 &&
833 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
835 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
838 if ( $pendingPollNodes
.length
=== 0 ) {
840 if ( timeoutID
!== null ) {
846 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
849 // ...but give up after a while
850 if ( timeout
!== 0 ) {
851 setTimeout( function () {
853 clearTimeout( timeoutID
);
858 }, timeout
|| 5000 );
861 return deferred
.promise();
867 * Load the frame contents.
869 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
870 * promise will be resolved. Calling while loading will return a promise but not trigger a new
871 * loading cycle. Calling after loading is complete will return a promise that's already been
874 * Sounds simple right? Read on...
876 * When you create a dynamic iframe using open/write/close, the window.load event for the
877 * iframe is triggered when you call close, and there's no further load event to indicate that
878 * everything is actually loaded.
880 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
881 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
882 * are added to document.styleSheets immediately, and the only way you can determine whether they've
883 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
884 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
886 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>` tags.
887 * Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets until
888 * the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the `@import`
889 * has finished. And because the contents of the `<style>` tag are from the same origin, accessing
890 * .cssRules is allowed.
892 * However, now that we control the styles we're injecting, we might as well do away with
893 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
894 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
895 * and wait for its font-family to change to someValue. Because `@import` is blocking, the font-family
896 * rule is not applied until after the `@import` finishes.
898 * All this stylesheet injection and polling magic is in #transplantStyles.
900 * @return {jQuery.Promise} Promise resolved when loading is complete
903 OO
.ui
.Frame
.prototype.load = function () {
906 // Return existing promise if already loading or loaded
907 if ( this.loading
) {
908 return this.loading
.promise();
912 this.loading
= $.Deferred();
914 win
= this.$element
.prop( 'contentWindow' );
917 // Figure out directionality:
918 this.dir
= OO
.ui
.Element
.getDir( this.$element
) || 'ltr';
920 // Initialize contents
925 '<body class="oo-ui-frame-body oo-ui-' + this.dir
+ '" style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
926 '<div class="oo-ui-frame-content"></div>' +
933 this.$ = OO
.ui
.Element
.getJQuery( doc
, this );
934 this.$content
= this.$( '.oo-ui-frame-content' ).attr( 'tabIndex', 0 );
935 this.$document
= this.$( doc
);
938 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
939 .always( OO
.ui
.bind( function () {
941 this.loading
.resolve();
944 return this.loading
.promise();
948 * Set the size of the frame.
950 * @param {number} width Frame width in pixels
951 * @param {number} height Frame height in pixels
954 OO
.ui
.Frame
.prototype.setSize = function ( width
, height
) {
955 this.$element
.css( { 'width': width
, 'height': height
} );
959 * Container for elements in a child frame.
961 * There are two ways to specify a title: set the static `title` property or provide a `title`
962 * property in the configuration options. The latter will override the former.
966 * @extends OO.ui.Element
967 * @mixins OO.EventEmitter
970 * @param {Object} [config] Configuration options
971 * @cfg {string|Function} [title] Title string or function that returns a string
972 * @cfg {string} [icon] Symbolic name of icon
975 OO
.ui
.Window
= function OoUiWindow( config
) {
977 // Parent constructor
978 OO
.ui
.Window
.super.call( this, config
);
980 // Mixin constructors
981 OO
.EventEmitter
.call( this );
984 this.visible
= false;
988 this.title
= OO
.ui
.resolveMsg( config
.title
|| this.constructor.static.title
);
989 this.icon
= config
.icon
|| this.constructor.static.icon
;
990 this.frame
= new OO
.ui
.Frame( { '$': this.$ } );
991 this.$frame
= this.$( '<div>' );
992 this.$ = function () {
993 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
998 .addClass( 'oo-ui-window' )
999 // Hide the window using visibility: hidden; while the iframe is still loading
1000 // Can't use display: none; because that prevents the iframe from loading in Firefox
1001 .css( 'visibility', 'hidden' )
1002 .append( this.$frame
);
1004 .addClass( 'oo-ui-window-frame' )
1005 .append( this.frame
.$element
);
1008 this.frame
.on( 'load', function () {
1009 element
.initialize();
1010 // Undo the visibility: hidden; hack and apply display: none;
1011 // We can do this safely now that the iframe has initialized
1012 // (don't do this from within #initialize because it has to happen
1013 // after the all subclasses have been handled as well).
1014 element
.$element
.hide().css( 'visibility', '' );
1020 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1021 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1028 * Fired after window has been opened.
1031 * @param {Object} data Window opening data
1037 * Fired after window has been closed.
1040 * @param {Object} data Window closing data
1043 /* Static Properties */
1046 * Symbolic name of icon.
1050 * @property {string}
1052 OO
.ui
.Window
.static.icon
= 'window';
1057 * Subclasses must implement this property before instantiating the window.
1058 * Alternatively, override #getTitle with an alternative implementation.
1063 * @property {string|Function} Title string or function that returns a string
1065 OO
.ui
.Window
.static.title
= null;
1070 * Check if window is visible.
1072 * @return {boolean} Window is visible
1074 OO
.ui
.Window
.prototype.isVisible = function () {
1075 return this.visible
;
1079 * Check if window is opening.
1081 * @return {boolean} Window is opening
1083 OO
.ui
.Window
.prototype.isOpening = function () {
1084 return !!this.opening
&& this.opening
.state() !== 'resolved';
1088 * Check if window is closing.
1090 * @return {boolean} Window is closing
1092 OO
.ui
.Window
.prototype.isClosing = function () {
1093 return !!this.closing
&& this.closing
.state() !== 'resolved';
1097 * Check if window is opened.
1099 * @return {boolean} Window is opened
1101 OO
.ui
.Window
.prototype.isOpened = function () {
1102 return !!this.opened
&& this.opened
.state() !== 'resolved';
1106 * Get the window frame.
1108 * @return {OO.ui.Frame} Frame of window
1110 OO
.ui
.Window
.prototype.getFrame = function () {
1115 * Get the title of the window.
1117 * @return {string} Title text
1119 OO
.ui
.Window
.prototype.getTitle = function () {
1124 * Get the window icon.
1126 * @return {string} Symbolic name of icon
1128 OO
.ui
.Window
.prototype.getIcon = function () {
1133 * Set the size of window frame.
1135 * @param {number} [width=auto] Custom width
1136 * @param {number} [height=auto] Custom height
1139 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1140 if ( !this.frame
.$content
) {
1144 this.frame
.$element
.css( {
1145 'width': width
=== undefined ? 'auto' : width
,
1146 'height': height
=== undefined ? 'auto' : height
1153 * Set the title of the window.
1155 * @param {string|Function} title Title text or a function that returns text
1158 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1159 this.title
= OO
.ui
.resolveMsg( title
);
1160 if ( this.$title
) {
1161 this.$title
.text( title
);
1167 * Set the icon of the window.
1169 * @param {string} icon Symbolic name of icon
1172 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1174 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1178 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1185 * Set the position of window to fit with contents.
1187 * @param {string} left Left offset
1188 * @param {string} top Top offset
1191 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1192 this.$element
.css( { 'left': left
, 'top': top
} );
1197 * Set the height of window to fit with contents.
1199 * @param {number} [min=0] Min height
1200 * @param {number} [max] Max height (defaults to content's outer height)
1203 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1204 var height
= this.frame
.$content
.outerHeight();
1206 this.frame
.$element
.css(
1207 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1214 * Set the width of window to fit with contents.
1216 * @param {number} [min=0] Min height
1217 * @param {number} [max] Max height (defaults to content's outer width)
1220 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1221 var width
= this.frame
.$content
.outerWidth();
1223 this.frame
.$element
.css(
1224 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1231 * Initialize window contents.
1233 * The first time the window is opened, #initialize is called when it's safe to begin populating
1234 * its contents. See #setup for a way to make changes each time the window opens.
1236 * Once this method is called, this.$$ can be used to create elements within the frame.
1240 OO
.ui
.Window
.prototype.initialize = function () {
1242 this.$ = this.frame
.$;
1243 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1244 .text( this.title
);
1245 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1246 .addClass( 'oo-ui-icon-' + this.icon
);
1247 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1248 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1249 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1250 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1253 this.frame
.$content
.append(
1254 this.$head
.append( this.$icon
, this.$title
),
1264 * Get a process for setting up a window for use.
1266 * Each time the window is opened this process will set it up for use in a particular context, based
1267 * on the `data` argument.
1269 * When you override this method, you can add additional setup steps to the process the parent
1270 * method provides using the 'first' and 'next' methods.
1273 * @param {Object} [data] Window opening data
1274 * @return {OO.ui.Process} Setup process
1276 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1277 return new OO
.ui
.Process();
1281 * Get a process for readying a window for use.
1283 * Each time the window is open and setup, this process will ready it up for use in a particular
1284 * context, based on the `data` argument.
1286 * When you override this method, you can add additional setup steps to the process the parent
1287 * method provides using the 'first' and 'next' methods.
1290 * @param {Object} [data] Window opening data
1291 * @return {OO.ui.Process} Setup process
1293 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1294 return new OO
.ui
.Process();
1298 * Get a process for tearing down a window after use.
1300 * Each time the window is closed this process will tear it down and do something with the user's
1301 * interactions within the window, based on the `data` argument.
1303 * When you override this method, you can add additional teardown steps to the process the parent
1304 * method provides using the 'first' and 'next' methods.
1307 * @param {Object} [data] Window closing data
1308 * @return {OO.ui.Process} Teardown process
1310 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1311 return new OO
.ui
.Process();
1317 * Do not override this method. Use #getSetupProcess to do something each time the window closes.
1319 * @param {Object} [data] Window opening data
1324 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1325 * first argument will be a promise which will be resolved when the window begins closing
1327 OO
.ui
.Window
.prototype.open = function ( data
) {
1328 // Return existing promise if already opening or open
1329 if ( this.opening
) {
1330 return this.opening
.promise();
1334 this.opening
= $.Deferred();
1335 this.frame
.load().done( OO
.ui
.bind( function () {
1336 this.$element
.show();
1337 this.visible
= true;
1338 this.emit( 'opening', data
);
1339 this.getSetupProcess( data
).execute().done( OO
.ui
.bind( function () {
1340 this.emit( 'open', data
);
1341 setTimeout( OO
.ui
.bind( function () {
1342 // Focus the content div (which has a tabIndex) to inactivate
1343 // (but not clear) selections in the parent frame.
1344 // Must happen after 'open' is emitted (to ensure it is visible)
1345 // but before 'ready' is emitted (so subclasses can give focus to something
1347 this.frame
.$content
.focus();
1348 this.getReadyProcess( data
).execute().done( OO
.ui
.bind( function () {
1349 this.emit( 'ready', data
);
1350 this.opened
= $.Deferred();
1351 this.opening
.resolve( this.opened
.promise() );
1352 // Now that we are totally done opening, it's safe to allow closing
1353 this.closing
= null;
1359 return this.opening
.promise();
1365 * Do not override this method. Use #getTeardownProcess to do something each time the window closes.
1367 * @param {Object} [data] Window closing data
1370 * @return {jQuery.Promise} Promise resolved when window is closed
1372 OO
.ui
.Window
.prototype.close = function ( data
) {
1373 // Return existing promise if already closing or closed
1374 if ( this.closing
) {
1375 return this.closing
.promise();
1379 // This.closing needs to exist before we emit the closing event so that handlers can call
1380 // window.close() and trigger the safety check above
1381 this.closing
= $.Deferred();
1382 this.frame
.$content
.find( ':focus' ).blur();
1383 this.emit( 'closing', data
);
1384 this.getTeardownProcess( data
).execute().done( OO
.ui
.bind( function () {
1385 // To do something different with #opened, resolve/reject #opened in the teardown process
1386 if ( this.opened
.state() === 'pending' ) {
1387 this.opened
.resolve();
1389 this.emit( 'close', data
);
1390 this.$element
.hide();
1391 this.visible
= false;
1392 this.closing
.resolve();
1393 // Now that we are totally done closing, it's safe to allow opening
1394 this.opening
= null;
1397 return this.closing
.promise();
1400 * Set of mutually exclusive windows.
1403 * @extends OO.ui.Element
1404 * @mixins OO.EventEmitter
1407 * @param {OO.Factory} factory Window factory
1408 * @param {Object} [config] Configuration options
1410 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1411 // Parent constructor
1412 OO
.ui
.WindowSet
.super.call( this, config
);
1414 // Mixin constructors
1415 OO
.EventEmitter
.call( this );
1418 this.factory
= factory
;
1421 * List of all windows associated with this window set.
1423 * @property {OO.ui.Window[]}
1425 this.windowList
= [];
1428 * Mapping of OO.ui.Window objects created by name from the #factory.
1430 * @property {Object}
1433 this.currentWindow
= null;
1436 this.$element
.addClass( 'oo-ui-windowSet' );
1441 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1442 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1448 * @param {OO.ui.Window} win Window that's being opened
1449 * @param {Object} config Window opening information
1454 * @param {OO.ui.Window} win Window that's been opened
1455 * @param {Object} config Window opening information
1460 * @param {OO.ui.Window} win Window that's being closed
1461 * @param {Object} config Window closing information
1466 * @param {OO.ui.Window} win Window that's been closed
1467 * @param {Object} config Window closing information
1473 * Handle a window that's being opened.
1475 * @param {OO.ui.Window} win Window that's being opened
1476 * @param {Object} [config] Window opening information
1479 OO
.ui
.WindowSet
.prototype.onWindowOpening = function ( win
, config
) {
1480 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1481 this.currentWindow
.close();
1483 this.currentWindow
= win
;
1484 this.emit( 'opening', win
, config
);
1488 * Handle a window that's been opened.
1490 * @param {OO.ui.Window} win Window that's been opened
1491 * @param {Object} [config] Window opening information
1494 OO
.ui
.WindowSet
.prototype.onWindowOpen = function ( win
, config
) {
1495 this.emit( 'open', win
, config
);
1499 * Handle a window that's being closed.
1501 * @param {OO.ui.Window} win Window that's being closed
1502 * @param {Object} [config] Window closing information
1505 OO
.ui
.WindowSet
.prototype.onWindowClosing = function ( win
, config
) {
1506 this.currentWindow
= null;
1507 this.emit( 'closing', win
, config
);
1511 * Handle a window that's been closed.
1513 * @param {OO.ui.Window} win Window that's been closed
1514 * @param {Object} [config] Window closing information
1517 OO
.ui
.WindowSet
.prototype.onWindowClose = function ( win
, config
) {
1518 this.emit( 'close', win
, config
);
1522 * Get the current window.
1524 * @return {OO.ui.Window|null} Current window or null if none open
1526 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1527 return this.currentWindow
;
1531 * Return a given window.
1533 * @param {string} name Symbolic name of window
1534 * @return {OO.ui.Window} Window with specified name
1536 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1539 if ( !this.factory
.lookup( name
) ) {
1540 throw new Error( 'Unknown window: ' + name
);
1542 if ( !( name
in this.windows
) ) {
1543 win
= this.windows
[name
] = this.createWindow( name
);
1544 this.addWindow( win
);
1546 return this.windows
[name
];
1550 * Create a window for use in this window set.
1552 * @param {string} name Symbolic name of window
1553 * @return {OO.ui.Window} Window with specified name
1555 OO
.ui
.WindowSet
.prototype.createWindow = function ( name
) {
1556 return this.factory
.create( name
, { '$': this.$ } );
1560 * Add a given window to this window set.
1562 * Connects event handlers and attaches it to the DOM. Calling
1563 * OO.ui.Window#open will not work until the window is added to the set.
1565 * @param {OO.ui.Window} win
1567 OO
.ui
.WindowSet
.prototype.addWindow = function ( win
) {
1568 if ( this.windowList
.indexOf( win
) !== -1 ) {
1572 this.windowList
.push( win
);
1574 win
.connect( this, {
1575 'opening': [ 'onWindowOpening', win
],
1576 'open': [ 'onWindowOpen', win
],
1577 'closing': [ 'onWindowClosing', win
],
1578 'close': [ 'onWindowClose', win
]
1580 this.$element
.append( win
.$element
);
1583 * Modal dialog window.
1587 * @extends OO.ui.Window
1590 * @param {Object} [config] Configuration options
1591 * @cfg {boolean} [footless] Hide foot
1592 * @cfg {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1594 OO
.ui
.Dialog
= function OoUiDialog( config
) {
1595 // Configuration initialization
1596 config
= $.extend( { 'size': 'large' }, config
);
1598 // Parent constructor
1599 OO
.ui
.Dialog
.super.call( this, config
);
1602 this.visible
= false;
1603 this.footless
= !!config
.footless
;
1606 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1607 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1610 this.$element
.on( 'mousedown', false );
1611 this.connect( this, { 'open': 'onOpen' } );
1614 this.$element
.addClass( 'oo-ui-dialog' );
1615 this.setSize( config
.size
);
1620 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1622 /* Static Properties */
1625 * Symbolic name of dialog.
1630 * @property {string}
1632 OO
.ui
.Dialog
.static.name
= '';
1635 * Map of symbolic size names and CSS classes.
1639 * @property {Object}
1641 OO
.ui
.Dialog
.static.sizeCssClasses
= {
1642 'small': 'oo-ui-dialog-small',
1643 'medium': 'oo-ui-dialog-medium',
1644 'large': 'oo-ui-dialog-large'
1650 * Handle close button click events.
1652 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1653 this.close( { 'action': 'cancel' } );
1657 * Handle window mouse wheel events.
1659 * @param {jQuery.Event} e Mouse wheel event
1661 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1666 * Handle document key down events.
1668 * @param {jQuery.Event} e Key down event
1670 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1671 switch ( e
.which
) {
1672 case OO
.ui
.Keys
.PAGEUP
:
1673 case OO
.ui
.Keys
.PAGEDOWN
:
1674 case OO
.ui
.Keys
.END
:
1675 case OO
.ui
.Keys
.HOME
:
1676 case OO
.ui
.Keys
.LEFT
:
1678 case OO
.ui
.Keys
.RIGHT
:
1679 case OO
.ui
.Keys
.DOWN
:
1680 // Prevent any key events that might cause scrolling
1686 * Handle frame document key down events.
1688 * @param {jQuery.Event} e Key down event
1690 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1691 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1692 this.close( { 'action': 'cancel' } );
1698 * Handle window open events.
1700 OO
.ui
.Dialog
.prototype.onOpen = function () {
1701 this.$element
.addClass( 'oo-ui-dialog-open' );
1707 * @param {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1709 OO
.ui
.Dialog
.prototype.setSize = function ( size
) {
1710 var name
, state
, cssClass
,
1711 sizeCssClasses
= OO
.ui
.Dialog
.static.sizeCssClasses
;
1713 if ( !sizeCssClasses
[size
] ) {
1717 for ( name
in sizeCssClasses
) {
1718 state
= name
=== size
;
1719 cssClass
= sizeCssClasses
[name
];
1720 this.$element
.toggleClass( cssClass
, state
);
1721 if ( this.frame
.$content
) {
1722 this.frame
.$content
.toggleClass( cssClass
, state
);
1730 OO
.ui
.Dialog
.prototype.initialize = function () {
1732 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
1735 this.closeButton
= new OO
.ui
.ButtonWidget( {
1739 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1743 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1744 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1747 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1748 if ( this.footless
) {
1749 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1751 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1752 this.$head
.append( this.closeButton
.$element
);
1758 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
1759 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
1760 .next( function () {
1761 // Prevent scrolling in top-level window
1762 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1763 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1770 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
1771 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
1772 .first( function () {
1773 this.$element
.removeClass( 'oo-ui-dialog-open' );
1774 return OO
.ui
.Process
.static.delay( 250 );
1776 .next( function () {
1777 // Allow scrolling in top-level window
1778 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1779 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1784 * Check if input is pending.
1788 OO
.ui
.Dialog
.prototype.isPending = function () {
1789 return !!this.pending
;
1793 * Increase the pending stack.
1797 OO
.ui
.Dialog
.prototype.pushPending = function () {
1798 if ( this.pending
=== 0 ) {
1799 this.frame
.$content
.addClass( 'oo-ui-dialog-pending' );
1800 this.$head
.addClass( 'oo-ui-texture-pending' );
1801 this.$foot
.addClass( 'oo-ui-texture-pending' );
1809 * Reduce the pending stack.
1815 OO
.ui
.Dialog
.prototype.popPending = function () {
1816 if ( this.pending
=== 1 ) {
1817 this.frame
.$content
.removeClass( 'oo-ui-dialog-pending' );
1818 this.$head
.removeClass( 'oo-ui-texture-pending' );
1819 this.$foot
.removeClass( 'oo-ui-texture-pending' );
1821 this.pending
= Math
.max( 0, this.pending
- 1 );
1826 * Container for elements.
1830 * @extends OO.ui.Element
1831 * @mixins OO.EventEmitter
1834 * @param {Object} [config] Configuration options
1836 OO
.ui
.Layout
= function OoUiLayout( config
) {
1837 // Initialize config
1838 config
= config
|| {};
1840 // Parent constructor
1841 OO
.ui
.Layout
.super.call( this, config
);
1843 // Mixin constructors
1844 OO
.EventEmitter
.call( this );
1847 this.$element
.addClass( 'oo-ui-layout' );
1852 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1853 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1855 * User interface control.
1859 * @extends OO.ui.Element
1860 * @mixins OO.EventEmitter
1863 * @param {Object} [config] Configuration options
1864 * @cfg {boolean} [disabled=false] Disable
1866 OO
.ui
.Widget
= function OoUiWidget( config
) {
1867 // Initialize config
1868 config
= $.extend( { 'disabled': false }, config
);
1870 // Parent constructor
1871 OO
.ui
.Widget
.super.call( this, config
);
1873 // Mixin constructors
1874 OO
.EventEmitter
.call( this );
1877 this.disabled
= null;
1878 this.wasDisabled
= null;
1881 this.$element
.addClass( 'oo-ui-widget' );
1882 this.setDisabled( !!config
.disabled
);
1887 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1888 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1894 * @param {boolean} disabled Widget is disabled
1900 * Check if the widget is disabled.
1902 * @param {boolean} Button is disabled
1904 OO
.ui
.Widget
.prototype.isDisabled = function () {
1905 return this.disabled
;
1909 * Update the disabled state, in case of changes in parent widget.
1913 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1914 this.setDisabled( this.disabled
);
1919 * Set the disabled state of the widget.
1921 * This should probably change the widgets' appearance and prevent it from being used.
1923 * @param {boolean} disabled Disable widget
1926 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1929 this.disabled
= !!disabled
;
1930 isDisabled
= this.isDisabled();
1931 if ( isDisabled
!== this.wasDisabled
) {
1932 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1933 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1934 this.emit( 'disable', isDisabled
);
1936 this.wasDisabled
= isDisabled
;
1940 * A list of functions, called in sequence.
1942 * If a function added to a process returns boolean false the process will stop; if it returns an
1943 * object with a `promise` method the process will use the promise to either continue to the next
1944 * step when the promise is resolved or stop when the promise is rejected.
1950 OO
.ui
.Process = function () {
1957 OO
.initClass( OO
.ui
.Process
);
1959 /* Static Methods */
1962 * Generate a promise which is resolved after a set amount of time.
1964 * @param {number} length Number of milliseconds before resolving the promise
1965 * @return {jQuery.Promise} Promise that will be resolved after a set amount of time
1967 OO
.ui
.Process
.static.delay = function ( length
) {
1968 var deferred
= $.Deferred();
1970 setTimeout( function () {
1974 return deferred
.promise();
1980 * Start the process.
1982 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
1983 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
1984 * process, the remaining steps will not be taken
1986 OO
.ui
.Process
.prototype.execute = function () {
1987 var i
, len
, promise
;
1990 * Continue execution.
1993 * @param {Array} step A function and the context it should be called in
1994 * @return {Function} Function that continues the process
1996 function proceed( step
) {
1997 return function () {
1998 // Execute step in the correct context
1999 var result
= step
[0].call( step
[1] );
2001 if ( result
=== false ) {
2002 // Use rejected promise for boolean false results
2003 return $.Deferred().reject().promise();
2005 // Duck-type the object to see if it can produce a promise
2006 if ( result
&& $.isFunction( result
.promise
) ) {
2007 // Use a promise generated from the result
2008 return result
.promise();
2010 // Use resolved promise for other results
2011 return $.Deferred().resolve().promise();
2015 if ( this.steps
.length
) {
2016 // Generate a chain reaction of promises
2017 promise
= proceed( this.steps
[0] )();
2018 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
2019 promise
= promise
.then( proceed( this.steps
[i
] ) );
2022 promise
= $.Deferred().resolve().promise();
2029 * Add step to the beginning of the process.
2031 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
2032 * it returns an object with a `promise` method the process will use the promise to either
2033 * continue to the next step when the promise is resolved or stop when the promise is rejected
2034 * @param {Object} [context=null] Context to call the step function in
2037 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
2038 this.steps
.unshift( [ step
, context
|| null ] );
2043 * Add step to the end of the process.
2045 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
2046 * it returns an object with a `promise` method the process will use the promise to either
2047 * continue to the next step when the promise is resolved or stop when the promise is rejected
2048 * @param {Object} [context=null] Context to call the step function in
2051 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
2052 this.steps
.push( [ step
, context
|| null ] );
2056 * Dialog for showing a confirmation/warning message.
2059 * @extends OO.ui.Dialog
2062 * @param {Object} [config] Configuration options
2064 OO
.ui
.ConfirmationDialog
= function OoUiConfirmationDialog( config
) {
2065 // Configuration initialization
2066 config
= $.extend( { 'size': 'small' }, config
);
2068 // Parent constructor
2069 OO
.ui
.Dialog
.call( this, config
);
2074 OO
.inheritClass( OO
.ui
.ConfirmationDialog
, OO
.ui
.Dialog
);
2076 /* Static Properties */
2078 OO
.ui
.ConfirmationDialog
.static.name
= 'confirm';
2080 OO
.ui
.ConfirmationDialog
.static.icon
= 'help';
2082 OO
.ui
.ConfirmationDialog
.static.title
= OO
.ui
.deferMsg( 'ooui-dialog-confirm-title' );
2089 OO
.ui
.ConfirmationDialog
.prototype.initialize = function () {
2091 OO
.ui
.Dialog
.prototype.initialize
.call( this );
2093 // Set up the layout
2094 var contentLayout
= new OO
.ui
.PanelLayout( {
2099 this.$promptContainer
= this.$( '<div>' ).addClass( 'oo-ui-dialog-confirm-promptContainer' );
2101 this.cancelButton
= new OO
.ui
.ButtonWidget();
2102 this.cancelButton
.connect( this, { 'click': [ 'close', 'cancel' ] } );
2104 this.okButton
= new OO
.ui
.ButtonWidget();
2105 this.okButton
.connect( this, { 'click': [ 'close', 'ok' ] } );
2108 contentLayout
.$element
.append( this.$promptContainer
);
2109 this.$body
.append( contentLayout
.$element
);
2112 this.okButton
.$element
,
2113 this.cancelButton
.$element
2116 this.connect( this, { 'close': [ 'close', 'cancel' ] } );
2120 * Setup a confirmation dialog.
2122 * @param {Object} [data] Window opening data including text of the dialog and text for the buttons
2123 * @param {jQuery|string} [data.prompt] Text to display or list of nodes to use as content of the dialog.
2124 * @param {jQuery|string|Function|null} [data.okLabel] Label of the OK button
2125 * @param {jQuery|string|Function|null} [data.cancelLabel] Label of the cancel button
2126 * @param {string|string[]} [data.okFlags="constructive"] Flags for the OK button
2127 * @param {string|string[]} [data.cancelFlags="destructive"] Flags for the cancel button
2128 * @return {OO.ui.Process} Setup process
2130 OO
.ui
.ConfirmationDialog
.prototype.getSetupProcess = function ( data
) {
2132 return OO
.ui
.ConfirmationDialog
.super.prototype.getSetupProcess
.call( this, data
)
2133 .next( function () {
2134 var prompt
= data
.prompt
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-prompt' ),
2135 okLabel
= data
.okLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-ok' ),
2136 cancelLabel
= data
.cancelLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-cancel' ),
2137 okFlags
= data
.okFlags
|| 'constructive',
2138 cancelFlags
= data
.cancelFlags
|| 'destructive';
2140 if ( typeof prompt
=== 'string' ) {
2141 this.$promptContainer
.text( prompt
);
2143 this.$promptContainer
.empty().append( prompt
);
2146 this.okButton
.setLabel( okLabel
).clearFlags().setFlags( okFlags
);
2147 this.cancelButton
.setLabel( cancelLabel
).clearFlags().setFlags( cancelFlags
);
2154 OO
.ui
.ConfirmationDialog
.prototype.getTeardownProcess = function ( data
) {
2156 return OO
.ui
.ConfirmationDialog
.super.prototype.getTeardownProcess
.call( this, data
)
2157 .first( function () {
2158 if ( data
=== 'ok' ) {
2159 this.opened
.resolve();
2160 } else if ( data
=== 'cancel' ) {
2161 this.opened
.reject();
2166 * Element with a button.
2172 * @param {jQuery} $button Button node, assigned to #$button
2173 * @param {Object} [config] Configuration options
2174 * @cfg {boolean} [frameless] Render button without a frame
2175 * @cfg {number} [tabIndex=0] Button's tab index, use -1 to prevent tab focusing
2177 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
2178 // Configuration initialization
2179 config
= config
|| {};
2182 this.$button
= $button
;
2183 this.tabIndex
= null;
2184 this.active
= false;
2185 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
2188 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2191 this.$element
.addClass( 'oo-ui-buttonedElement' );
2193 .addClass( 'oo-ui-buttonedElement-button' )
2194 .attr( 'role', 'button' )
2195 .prop( 'tabIndex', config
.tabIndex
|| 0 );
2196 if ( config
.frameless
) {
2197 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
2199 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
2205 OO
.initClass( OO
.ui
.ButtonedElement
);
2207 /* Static Properties */
2210 * Cancel mouse down events.
2214 * @property {boolean}
2216 OO
.ui
.ButtonedElement
.static.cancelButtonMouseDownEvents
= true;
2221 * Handles mouse down events.
2223 * @param {jQuery.Event} e Mouse down event
2225 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function ( e
) {
2226 if ( this.isDisabled() || e
.which
!== 1 ) {
2229 // tabIndex should generally be interacted with via the property, but it's not possible to
2230 // reliably unset a tabIndex via a property so we use the (lowercase) "tabindex" attribute
2231 this.tabIndex
= this.$button
.attr( 'tabindex' );
2233 // Remove the tab-index while the button is down to prevent the button from stealing focus
2234 .removeAttr( 'tabindex' )
2235 .addClass( 'oo-ui-buttonedElement-pressed' );
2236 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
2237 // reliably reapply the tabindex and remove the pressed class
2238 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
2239 // Prevent change of focus unless specifically configured otherwise
2240 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
2246 * Handles mouse up events.
2248 * @param {jQuery.Event} e Mouse up event
2250 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function ( e
) {
2251 if ( this.isDisabled() || e
.which
!== 1 ) {
2255 // Restore the tab-index after the button is up to restore the button's accesssibility
2256 .attr( 'tabindex', this.tabIndex
)
2257 .removeClass( 'oo-ui-buttonedElement-pressed' );
2258 // Stop listening for mouseup, since we only needed this once
2259 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
2265 * @param {boolean} [value] Make button active
2268 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
2269 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
2273 * Element that can be automatically clipped to visible boundaies.
2279 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
2280 * @param {Object} [config] Configuration options
2282 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
2283 // Configuration initialization
2284 config
= config
|| {};
2287 this.$clippable
= $clippable
;
2288 this.clipping
= false;
2289 this.clipped
= false;
2290 this.$clippableContainer
= null;
2291 this.$clippableScroller
= null;
2292 this.$clippableWindow
= null;
2293 this.idealWidth
= null;
2294 this.idealHeight
= null;
2295 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
2296 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
2299 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
2307 * @param {boolean} value Enable clipping
2310 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
2313 if ( this.clipping
!== value
) {
2314 this.clipping
= value
;
2315 if ( this.clipping
) {
2316 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
2317 // If the clippable container is the body, we have to listen to scroll events and check
2318 // jQuery.scrollTop on the window because of browser inconsistencies
2319 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
2320 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
2321 this.$clippableContainer
;
2322 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
2323 this.$clippableWindow
= this.$( this.getElementWindow() )
2324 .on( 'resize', this.onClippableWindowResizeHandler
);
2325 // Initial clip after visible
2326 setTimeout( OO
.ui
.bind( this.clip
, this ) );
2328 this.$clippableContainer
= null;
2329 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
2330 this.$clippableScroller
= null;
2331 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
2332 this.$clippableWindow
= null;
2340 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
2342 * @return {boolean} Element will be clipped to the visible area
2344 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
2345 return this.clipping
;
2349 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
2351 * @return {boolean} Part of the element is being clipped
2353 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
2354 return this.clipped
;
2358 * Set the ideal size.
2360 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
2361 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
2363 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
2364 this.idealWidth
= width
;
2365 this.idealHeight
= height
;
2369 * Clip element to visible boundaries and allow scrolling when needed.
2371 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
2372 * overlapped by, the visible area of the nearest scrollable container.
2376 OO
.ui
.ClippableElement
.prototype.clip = function () {
2377 if ( !this.clipping
) {
2378 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
2383 cOffset
= this.$clippable
.offset(),
2384 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
2385 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
2386 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
2387 scrollTop
= this.$clippableScroller
.scrollTop(),
2388 scrollLeft
= this.$clippableScroller
.scrollLeft(),
2389 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
2390 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
2391 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
2392 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
2393 clipWidth
= desiredWidth
< naturalWidth
,
2394 clipHeight
= desiredHeight
< naturalHeight
;
2397 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
2399 this.$clippable
.css( { 'overflow-x': '', 'width': this.idealWidth
|| '' } );
2402 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
2404 this.$clippable
.css( { 'overflow-y': '', 'height': this.idealHeight
|| '' } );
2407 this.clipped
= clipWidth
|| clipHeight
;
2412 * Element with named flags that can be added, removed, listed and checked.
2414 * A flag, when set, adds a CSS class on the `$element` by combing `oo-ui-flaggableElement-` with
2415 * the flag name. Flags are primarily useful for styling.
2421 * @param {Object} [config] Configuration options
2422 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
2424 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
2425 // Config initialization
2426 config
= config
|| {};
2432 this.setFlags( config
.flags
);
2438 * Check if a flag is set.
2440 * @param {string} flag Name of flag
2441 * @return {boolean} Has flag
2443 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
2444 return flag
in this.flags
;
2448 * Get the names of all flags set.
2450 * @return {string[]} flags Flag names
2452 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
2453 return Object
.keys( this.flags
);
2461 OO
.ui
.FlaggableElement
.prototype.clearFlags = function () {
2463 classPrefix
= 'oo-ui-flaggableElement-';
2465 for ( flag
in this.flags
) {
2466 delete this.flags
[flag
];
2467 this.$element
.removeClass( classPrefix
+ flag
);
2474 * Add one or more flags.
2476 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
2477 * keyed by flag name containing boolean set/remove instructions.
2480 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
2482 classPrefix
= 'oo-ui-flaggableElement-';
2484 if ( typeof flags
=== 'string' ) {
2486 this.flags
[flags
] = true;
2487 this.$element
.addClass( classPrefix
+ flags
);
2488 } else if ( $.isArray( flags
) ) {
2489 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
2492 this.flags
[flag
] = true;
2493 this.$element
.addClass( classPrefix
+ flag
);
2495 } else if ( OO
.isPlainObject( flags
) ) {
2496 for ( flag
in flags
) {
2497 if ( flags
[flag
] ) {
2499 this.flags
[flag
] = true;
2500 this.$element
.addClass( classPrefix
+ flag
);
2503 delete this.flags
[flag
];
2504 this.$element
.removeClass( classPrefix
+ flag
);
2511 * Element containing a sequence of child elements.
2517 * @param {jQuery} $group Container node, assigned to #$group
2518 * @param {Object} [config] Configuration options
2520 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2522 config
= config
|| {};
2525 this.$group
= $group
;
2527 this.aggregateItemEvents
= {};
2535 * @return {OO.ui.Element[]} Items
2537 OO
.ui
.GroupElement
.prototype.getItems = function () {
2538 return this.items
.slice( 0 );
2542 * Add an aggregate item event.
2544 * Aggregated events are listened to on each item and then emitted by the group under a new name,
2545 * and with an additional leading parameter containing the item that emitted the original event.
2546 * Other arguments that were emitted from the original event are passed through.
2548 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
2549 * event, use null value to remove aggregation
2550 * @throws {Error} If aggregation already exists
2552 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
2553 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
2555 for ( itemEvent
in events
) {
2556 groupEvent
= events
[itemEvent
];
2558 // Remove existing aggregated event
2559 if ( itemEvent
in this.aggregateItemEvents
) {
2560 // Don't allow duplicate aggregations
2562 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
2564 // Remove event aggregation from existing items
2565 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2566 item
= this.items
[i
];
2567 if ( item
.connect
&& item
.disconnect
) {
2569 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
2570 item
.disconnect( this, remove
);
2573 // Prevent future items from aggregating event
2574 delete this.aggregateItemEvents
[itemEvent
];
2577 // Add new aggregate event
2579 // Make future items aggregate event
2580 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
2581 // Add event aggregation to existing items
2582 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2583 item
= this.items
[i
];
2584 if ( item
.connect
&& item
.disconnect
) {
2586 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
2587 item
.connect( this, add
);
2597 * @param {OO.ui.Element[]} items Item
2598 * @param {number} [index] Index to insert items at
2601 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2602 var i
, len
, item
, event
, events
, currentIndex
,
2605 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2608 // Check if item exists then remove it first, effectively "moving" it
2609 currentIndex
= $.inArray( item
, this.items
);
2610 if ( currentIndex
>= 0 ) {
2611 this.removeItems( [ item
] );
2612 // Adjust index to compensate for removal
2613 if ( currentIndex
< index
) {
2618 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
2620 for ( event
in this.aggregateItemEvents
) {
2621 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
2623 item
.connect( this, events
);
2625 item
.setElementGroup( this );
2626 itemElements
.push( item
.$element
.get( 0 ) );
2629 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2630 this.$group
.append( itemElements
);
2631 this.items
.push
.apply( this.items
, items
);
2632 } else if ( index
=== 0 ) {
2633 this.$group
.prepend( itemElements
);
2634 this.items
.unshift
.apply( this.items
, items
);
2636 this.items
[index
].$element
.before( itemElements
);
2637 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2646 * Items will be detached, not removed, so they can be used later.
2648 * @param {OO.ui.Element[]} items Items to remove
2651 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2652 var i
, len
, item
, index
, remove
, itemEvent
;
2654 // Remove specific items
2655 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2657 index
= $.inArray( item
, this.items
);
2658 if ( index
!== -1 ) {
2660 item
.connect
&& item
.disconnect
&&
2661 !$.isEmptyObject( this.aggregateItemEvents
)
2664 if ( itemEvent
in this.aggregateItemEvents
) {
2665 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2667 item
.disconnect( this, remove
);
2669 item
.setElementGroup( null );
2670 this.items
.splice( index
, 1 );
2671 item
.$element
.detach();
2681 * Items will be detached, not removed, so they can be used later.
2685 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2686 var i
, len
, item
, remove
, itemEvent
;
2689 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2690 item
= this.items
[i
];
2692 item
.connect
&& item
.disconnect
&&
2693 !$.isEmptyObject( this.aggregateItemEvents
)
2696 if ( itemEvent
in this.aggregateItemEvents
) {
2697 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2699 item
.disconnect( this, remove
);
2701 item
.setElementGroup( null );
2702 item
.$element
.detach();
2708 * Element containing an icon.
2714 * @param {jQuery} $icon Icon node, assigned to #$icon
2715 * @param {Object} [config] Configuration options
2716 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2717 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2720 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2721 // Config intialization
2722 config
= config
|| {};
2729 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2730 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2735 OO
.initClass( OO
.ui
.IconedElement
);
2737 /* Static Properties */
2742 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2744 * For i18n purposes, this property can be an object containing a `default` icon name property and
2745 * additional icon names keyed by language code.
2747 * Example of i18n icon definition:
2748 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2752 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2753 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2756 OO
.ui
.IconedElement
.static.icon
= null;
2763 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
2764 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2768 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2769 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2772 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2774 if ( typeof icon
=== 'string' ) {
2776 if ( icon
.length
) {
2777 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2781 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2789 * @return {string} Icon
2791 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2795 * Element containing an indicator.
2801 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2802 * @param {Object} [config] Configuration options
2803 * @cfg {string} [indicator] Symbolic indicator name
2804 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2806 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2807 // Config intialization
2808 config
= config
|| {};
2811 this.$indicator
= $indicator
;
2812 this.indicator
= null;
2813 this.indicatorLabel
= null;
2816 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2817 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2818 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2823 OO
.initClass( OO
.ui
.IndicatedElement
);
2825 /* Static Properties */
2832 * @property {string|null} Symbolic indicator name or null for no indicator
2834 OO
.ui
.IndicatedElement
.static.indicator
= null;
2841 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2844 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2851 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2854 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2855 if ( this.indicator
) {
2856 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2857 this.indicator
= null;
2859 if ( typeof indicator
=== 'string' ) {
2860 indicator
= indicator
.trim();
2861 if ( indicator
.length
) {
2862 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2863 this.indicator
= indicator
;
2866 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2872 * Set indicator label.
2874 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2875 * for no indicator title
2878 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2879 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2881 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2882 this.$indicator
.attr( 'title', indicatorTitle
);
2884 this.$indicator
.removeAttr( 'title' );
2893 * @return {string} title Symbolic name of indicator
2895 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2896 return this.indicator
;
2900 * Get indicator title.
2902 * @return {string} Indicator title text
2904 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2905 return this.indicatorTitle
;
2908 * Element containing a label.
2914 * @param {jQuery} $label Label node, assigned to #$label
2915 * @param {Object} [config] Configuration options
2916 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2917 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
2919 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2920 // Config intialization
2921 config
= config
|| {};
2924 this.$label
= $label
;
2928 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2929 this.setLabel( config
.label
|| this.constructor.static.label
);
2930 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
2935 OO
.initClass( OO
.ui
.LabeledElement
);
2937 /* Static Properties */
2944 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2947 OO
.ui
.LabeledElement
.static.label
= null;
2954 * An empty string will result in the label being hidden. A string containing only whitespace will
2955 * be converted to a single
2957 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2958 * text; or null for no label
2961 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2964 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2965 if ( typeof label
=== 'string' && label
.length
) {
2966 if ( label
.match( /^\s*$/ ) ) {
2967 // Convert whitespace only string to a single non-breaking space
2968 this.$label
.html( ' ' );
2970 this.$label
.text( label
);
2972 } else if ( label
instanceof jQuery
) {
2973 this.$label
.empty().append( label
);
2975 this.$label
.empty();
2978 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2979 this.$label
.css( 'display', empty
? 'none' : '' );
2987 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2988 * text; or null for no label
2990 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2999 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
3000 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
3001 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
3006 * Popuppable element.
3012 * @param {Object} [config] Configuration options
3013 * @cfg {number} [popupWidth=320] Width of popup
3014 * @cfg {number} [popupHeight] Height of popup
3015 * @cfg {Object} [popup] Configuration to pass to popup
3017 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
3018 // Configuration initialization
3019 config
= $.extend( { 'popupWidth': 320 }, config
);
3022 this.popup
= new OO
.ui
.PopupWidget( $.extend(
3023 { 'align': 'center', 'autoClose': true },
3025 { '$': this.$, '$autoCloseIgnore': this.$element
}
3027 this.popupWidth
= config
.popupWidth
;
3028 this.popupHeight
= config
.popupHeight
;
3036 * @return {OO.ui.PopupWidget} Popup widget
3038 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
3045 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
3046 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
3052 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
3056 * Element with a title.
3062 * @param {jQuery} $label Titled node, assigned to #$titled
3063 * @param {Object} [config] Configuration options
3064 * @cfg {string|Function} [title] Title text or a function that returns text
3066 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
3067 // Config intialization
3068 config
= config
|| {};
3071 this.$titled
= $titled
;
3075 this.setTitle( config
.title
|| this.constructor.static.title
);
3080 OO
.initClass( OO
.ui
.TitledElement
);
3082 /* Static Properties */
3089 * @property {string|Function} Title text or a function that returns text
3091 OO
.ui
.TitledElement
.static.title
= null;
3098 * @param {string|Function|null} title Title text, a function that returns text or null for no title
3101 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
3102 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
3104 if ( typeof title
=== 'string' && title
.length
) {
3105 this.$titled
.attr( 'title', title
);
3107 this.$titled
.removeAttr( 'title' );
3116 * @return {string} Title string
3118 OO
.ui
.TitledElement
.prototype.getTitle = function () {
3122 * Generic toolbar tool.
3126 * @extends OO.ui.Widget
3127 * @mixins OO.ui.IconedElement
3130 * @param {OO.ui.ToolGroup} toolGroup
3131 * @param {Object} [config] Configuration options
3132 * @cfg {string|Function} [title] Title text or a function that returns text
3134 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
3135 // Config intialization
3136 config
= config
|| {};
3138 // Parent constructor
3139 OO
.ui
.Tool
.super.call( this, config
);
3141 // Mixin constructors
3142 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
3145 this.toolGroup
= toolGroup
;
3146 this.toolbar
= this.toolGroup
.getToolbar();
3147 this.active
= false;
3148 this.$title
= this.$( '<span>' );
3149 this.$link
= this.$( '<a>' );
3153 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
3156 this.$title
.addClass( 'oo-ui-tool-title' );
3158 .addClass( 'oo-ui-tool-link' )
3159 .append( this.$icon
, this.$title
);
3161 .data( 'oo-ui-tool', this )
3163 'oo-ui-tool ' + 'oo-ui-tool-name-' +
3164 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
3166 .append( this.$link
);
3167 this.setTitle( config
.title
|| this.constructor.static.title
);
3172 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
3173 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
3181 /* Static Properties */
3187 OO
.ui
.Tool
.static.tagName
= 'span';
3190 * Symbolic name of tool.
3195 * @property {string}
3197 OO
.ui
.Tool
.static.name
= '';
3205 * @property {string}
3207 OO
.ui
.Tool
.static.group
= '';
3212 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
3213 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
3214 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
3215 * appended to the title if the tool is part of a bar tool group.
3220 * @property {string|Function} Title text or a function that returns text
3222 OO
.ui
.Tool
.static.title
= '';
3225 * Tool can be automatically added to catch-all groups.
3229 * @property {boolean}
3231 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
3234 * Tool can be automatically added to named groups.
3237 * @property {boolean}
3240 OO
.ui
.Tool
.static.autoAddToGroup
= true;
3243 * Check if this tool is compatible with given data.
3247 * @param {Mixed} data Data to check
3248 * @return {boolean} Tool can be used with data
3250 OO
.ui
.Tool
.static.isCompatibleWith = function () {
3257 * Handle the toolbar state being updated.
3259 * This is an abstract method that must be overridden in a concrete subclass.
3263 OO
.ui
.Tool
.prototype.onUpdateState = function () {
3265 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
3270 * Handle the tool being selected.
3272 * This is an abstract method that must be overridden in a concrete subclass.
3276 OO
.ui
.Tool
.prototype.onSelect = function () {
3278 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
3283 * Check if the button is active.
3285 * @param {boolean} Button is active
3287 OO
.ui
.Tool
.prototype.isActive = function () {
3292 * Make the button appear active or inactive.
3294 * @param {boolean} state Make button appear active
3296 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
3297 this.active
= !!state
;
3298 if ( this.active
) {
3299 this.$element
.addClass( 'oo-ui-tool-active' );
3301 this.$element
.removeClass( 'oo-ui-tool-active' );
3306 * Get the tool title.
3308 * @param {string|Function} title Title text or a function that returns text
3311 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
3312 this.title
= OO
.ui
.resolveMsg( title
);
3318 * Get the tool title.
3320 * @return {string} Title text
3322 OO
.ui
.Tool
.prototype.getTitle = function () {
3327 * Get the tool's symbolic name.
3329 * @return {string} Symbolic name of tool
3331 OO
.ui
.Tool
.prototype.getName = function () {
3332 return this.constructor.static.name
;
3338 OO
.ui
.Tool
.prototype.updateTitle = function () {
3339 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
3340 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
3341 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
3348 .addClass( 'oo-ui-tool-accel' )
3352 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
3353 tooltipParts
.push( this.title
);
3355 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
3356 tooltipParts
.push( accel
);
3358 if ( tooltipParts
.length
) {
3359 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
3361 this.$link
.removeAttr( 'title' );
3368 OO
.ui
.Tool
.prototype.destroy = function () {
3369 this.toolbar
.disconnect( this );
3370 this.$element
.remove();
3373 * Collection of tool groups.
3376 * @extends OO.ui.Element
3377 * @mixins OO.EventEmitter
3378 * @mixins OO.ui.GroupElement
3381 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
3382 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
3383 * @param {Object} [config] Configuration options
3384 * @cfg {boolean} [actions] Add an actions section opposite to the tools
3385 * @cfg {boolean} [shadow] Add a shadow below the toolbar
3387 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
3388 // Configuration initialization
3389 config
= config
|| {};
3391 // Parent constructor
3392 OO
.ui
.Toolbar
.super.call( this, config
);
3394 // Mixin constructors
3395 OO
.EventEmitter
.call( this );
3396 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3399 this.toolFactory
= toolFactory
;
3400 this.toolGroupFactory
= toolGroupFactory
;
3403 this.$bar
= this.$( '<div>' );
3404 this.$actions
= this.$( '<div>' );
3405 this.initialized
= false;
3409 .add( this.$bar
).add( this.$group
).add( this.$actions
)
3410 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
3413 this.$group
.addClass( 'oo-ui-toolbar-tools' );
3414 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
3415 if ( config
.actions
) {
3416 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
3417 this.$bar
.append( this.$actions
);
3419 this.$bar
.append( '<div style="clear:both"></div>' );
3420 if ( config
.shadow
) {
3421 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
3423 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
3428 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
3429 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
3430 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
3435 * Get the tool factory.
3437 * @return {OO.ui.ToolFactory} Tool factory
3439 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
3440 return this.toolFactory
;
3444 * Get the tool group factory.
3446 * @return {OO.Factory} Tool group factory
3448 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
3449 return this.toolGroupFactory
;
3453 * Handles mouse down events.
3455 * @param {jQuery.Event} e Mouse down event
3457 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
3458 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
3459 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
3460 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
3466 * Sets up handles and preloads required information for the toolbar to work.
3467 * This must be called immediately after it is attached to a visible document.
3469 OO
.ui
.Toolbar
.prototype.initialize = function () {
3470 this.initialized
= true;
3476 * Tools can be specified in the following ways:
3478 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3479 * - All tools in a group: `{ 'group': 'group-name' }`
3480 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
3482 * @param {Object.<string,Array>} groups List of tool group configurations
3483 * @param {Array|string} [groups.include] Tools to include
3484 * @param {Array|string} [groups.exclude] Tools to exclude
3485 * @param {Array|string} [groups.promote] Tools to promote to the beginning
3486 * @param {Array|string} [groups.demote] Tools to demote to the end
3488 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
3489 var i
, len
, type
, group
,
3491 defaultType
= 'bar';
3493 // Cleanup previous groups
3496 // Build out new groups
3497 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
3499 if ( group
.include
=== '*' ) {
3500 // Apply defaults to catch-all groups
3501 if ( group
.type
=== undefined ) {
3502 group
.type
= 'list';
3504 if ( group
.label
=== undefined ) {
3505 group
.label
= 'ooui-toolbar-more';
3508 // Check type has been registered
3509 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
3511 this.getToolGroupFactory().create( type
, this, $.extend( { '$': this.$ }, group
) )
3514 this.addItems( items
);
3518 * Remove all tools and groups from the toolbar.
3520 OO
.ui
.Toolbar
.prototype.reset = function () {
3525 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3526 this.items
[i
].destroy();
3532 * Destroys toolbar, removing event handlers and DOM elements.
3534 * Call this whenever you are done using a toolbar.
3536 OO
.ui
.Toolbar
.prototype.destroy = function () {
3538 this.$element
.remove();
3542 * Check if tool has not been used yet.
3544 * @param {string} name Symbolic name of tool
3545 * @return {boolean} Tool is available
3547 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
3548 return !this.tools
[name
];
3552 * Prevent tool from being used again.
3554 * @param {OO.ui.Tool} tool Tool to reserve
3556 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
3557 this.tools
[tool
.getName()] = tool
;
3561 * Allow tool to be used again.
3563 * @param {OO.ui.Tool} tool Tool to release
3565 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
3566 delete this.tools
[tool
.getName()];
3570 * Get accelerator label for tool.
3572 * This is a stub that should be overridden to provide access to accelerator information.
3574 * @param {string} name Symbolic name of tool
3575 * @return {string|undefined} Tool accelerator label if available
3577 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
3581 * Factory for tools.
3584 * @extends OO.Factory
3587 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3588 // Parent constructor
3589 OO
.ui
.ToolFactory
.super.call( this );
3594 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3599 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3600 var i
, len
, included
, promoted
, demoted
,
3604 // Collect included and not excluded tools
3605 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3608 promoted
= this.extract( promote
, used
);
3609 demoted
= this.extract( demote
, used
);
3612 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3613 if ( !used
[included
[i
]] ) {
3614 auto
.push( included
[i
] );
3618 return promoted
.concat( auto
).concat( demoted
);
3622 * Get a flat list of names from a list of names or groups.
3624 * Tools can be specified in the following ways:
3626 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3627 * - All tools in a group: `{ 'group': 'group-name' }`
3628 * - All tools: `'*'`
3631 * @param {Array|string} collection List of tools
3632 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3633 * names will be added as properties
3634 * @return {string[]} List of extracted names
3636 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3637 var i
, len
, item
, name
, tool
,
3640 if ( collection
=== '*' ) {
3641 for ( name
in this.registry
) {
3642 tool
= this.registry
[name
];
3644 // Only add tools by group name when auto-add is enabled
3645 tool
.static.autoAddToCatchall
&&
3646 // Exclude already used tools
3647 ( !used
|| !used
[name
] )
3655 } else if ( $.isArray( collection
) ) {
3656 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3657 item
= collection
[i
];
3658 // Allow plain strings as shorthand for named tools
3659 if ( typeof item
=== 'string' ) {
3660 item
= { 'name': item
};
3662 if ( OO
.isPlainObject( item
) ) {
3664 for ( name
in this.registry
) {
3665 tool
= this.registry
[name
];
3667 // Include tools with matching group
3668 tool
.static.group
=== item
.group
&&
3669 // Only add tools by group name when auto-add is enabled
3670 tool
.static.autoAddToGroup
&&
3671 // Exclude already used tools
3672 ( !used
|| !used
[name
] )
3680 // Include tools with matching name and exclude already used tools
3681 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3682 names
.push( item
.name
);
3684 used
[item
.name
] = true;
3693 * Collection of tools.
3695 * Tools can be specified in the following ways:
3697 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3698 * - All tools in a group: `{ 'group': 'group-name' }`
3699 * - All tools: `'*'`
3703 * @extends OO.ui.Widget
3704 * @mixins OO.ui.GroupElement
3707 * @param {OO.ui.Toolbar} toolbar
3708 * @param {Object} [config] Configuration options
3709 * @cfg {Array|string} [include=[]] List of tools to include
3710 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3711 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3712 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3714 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3715 // Configuration initialization
3716 config
= config
|| {};
3718 // Parent constructor
3719 OO
.ui
.ToolGroup
.super.call( this, config
);
3721 // Mixin constructors
3722 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3725 this.toolbar
= toolbar
;
3727 this.pressed
= null;
3728 this.autoDisabled
= false;
3729 this.include
= config
.include
|| [];
3730 this.exclude
= config
.exclude
|| [];
3731 this.promote
= config
.promote
|| [];
3732 this.demote
= config
.demote
|| [];
3733 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3737 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3738 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3739 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3740 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3742 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3743 this.aggregate( { 'disable': 'itemDisable' } );
3744 this.connect( this, { 'itemDisable': 'updateDisabled' } );
3747 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3749 .addClass( 'oo-ui-toolGroup' )
3750 .append( this.$group
);
3756 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3757 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3765 /* Static Properties */
3768 * Show labels in tooltips.
3772 * @property {boolean}
3774 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3777 * Show acceleration labels in tooltips.
3781 * @property {boolean}
3783 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3786 * Automatically disable the toolgroup when all tools are disabled
3790 * @property {boolean}
3792 OO
.ui
.ToolGroup
.static.autoDisable
= true;
3799 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
3800 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
3806 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
3807 var i
, item
, allDisabled
= true;
3809 if ( this.constructor.static.autoDisable
) {
3810 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
3811 item
= this.items
[i
];
3812 if ( !item
.isDisabled() ) {
3813 allDisabled
= false;
3817 this.autoDisabled
= allDisabled
;
3819 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
3823 * Handle mouse down events.
3825 * @param {jQuery.Event} e Mouse down event
3827 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3828 if ( !this.isDisabled() && e
.which
=== 1 ) {
3829 this.pressed
= this.getTargetTool( e
);
3830 if ( this.pressed
) {
3831 this.pressed
.setActive( true );
3832 this.getElementDocument().addEventListener(
3833 'mouseup', this.onCapturedMouseUpHandler
, true
3841 * Handle captured mouse up events.
3843 * @param {Event} e Mouse up event
3845 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3846 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3847 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3848 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3849 this.onMouseUp( e
);
3853 * Handle mouse up events.
3855 * @param {jQuery.Event} e Mouse up event
3857 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3858 var tool
= this.getTargetTool( e
);
3860 if ( !this.isDisabled() && e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3861 this.pressed
.onSelect();
3864 this.pressed
= null;
3869 * Handle mouse over events.
3871 * @param {jQuery.Event} e Mouse over event
3873 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3874 var tool
= this.getTargetTool( e
);
3876 if ( this.pressed
&& this.pressed
=== tool
) {
3877 this.pressed
.setActive( true );
3882 * Handle mouse out events.
3884 * @param {jQuery.Event} e Mouse out event
3886 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3887 var tool
= this.getTargetTool( e
);
3889 if ( this.pressed
&& this.pressed
=== tool
) {
3890 this.pressed
.setActive( false );
3895 * Get the closest tool to a jQuery.Event.
3897 * Only tool links are considered, which prevents other elements in the tool such as popups from
3898 * triggering tool group interactions.
3901 * @param {jQuery.Event} e
3902 * @return {OO.ui.Tool|null} Tool, `null` if none was found
3904 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3906 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3908 if ( $item
.length
) {
3909 tool
= $item
.parent().data( 'oo-ui-tool' );
3912 return tool
&& !tool
.isDisabled() ? tool
: null;
3916 * Handle tool registry register events.
3918 * If a tool is registered after the group is created, we must repopulate the list to account for:
3920 * - a tool being added that may be included
3921 * - a tool already included being overridden
3923 * @param {string} name Symbolic name of tool
3925 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3930 * Get the toolbar this group is in.
3932 * @return {OO.ui.Toolbar} Toolbar of group
3934 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3935 return this.toolbar
;
3939 * Add and remove tools based on configuration.
3941 OO
.ui
.ToolGroup
.prototype.populate = function () {
3942 var i
, len
, name
, tool
,
3943 toolFactory
= this.toolbar
.getToolFactory(),
3947 list
= this.toolbar
.getToolFactory().getTools(
3948 this.include
, this.exclude
, this.promote
, this.demote
3951 // Build a list of needed tools
3952 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3956 toolFactory
.lookup( name
) &&
3957 // Tool is available or is already in this group
3958 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3960 tool
= this.tools
[name
];
3962 // Auto-initialize tools on first use
3963 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3966 this.toolbar
.reserveTool( tool
);
3971 // Remove tools that are no longer needed
3972 for ( name
in this.tools
) {
3973 if ( !names
[name
] ) {
3974 this.tools
[name
].destroy();
3975 this.toolbar
.releaseTool( this.tools
[name
] );
3976 remove
.push( this.tools
[name
] );
3977 delete this.tools
[name
];
3980 if ( remove
.length
) {
3981 this.removeItems( remove
);
3983 // Update emptiness state
3985 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3987 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3989 // Re-add tools (moving existing ones to new locations)
3990 this.addItems( add
);
3991 // Disabled state may depend on items
3992 this.updateDisabled();
3996 * Destroy tool group.
3998 OO
.ui
.ToolGroup
.prototype.destroy = function () {
4002 this.toolbar
.getToolFactory().disconnect( this );
4003 for ( name
in this.tools
) {
4004 this.toolbar
.releaseTool( this.tools
[name
] );
4005 this.tools
[name
].disconnect( this ).destroy();
4006 delete this.tools
[name
];
4008 this.$element
.remove();
4011 * Factory for tool groups.
4014 * @extends OO.Factory
4017 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
4018 // Parent constructor
4019 OO
.Factory
.call( this );
4022 defaultClasses
= this.constructor.static.getDefaultClasses();
4024 // Register default toolgroups
4025 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
4026 this.register( defaultClasses
[i
] );
4032 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
4034 /* Static Methods */
4037 * Get a default set of classes to be registered on construction
4039 * @return {Function[]} Default classes
4041 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
4044 OO
.ui
.ListToolGroup
,
4049 * Layout made of a fieldset and optional legend.
4051 * Just add OO.ui.FieldLayout items.
4054 * @extends OO.ui.Layout
4055 * @mixins OO.ui.LabeledElement
4056 * @mixins OO.ui.IconedElement
4057 * @mixins OO.ui.GroupElement
4060 * @param {Object} [config] Configuration options
4061 * @cfg {string} [icon] Symbolic icon name
4062 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
4064 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
4065 // Config initialization
4066 config
= config
|| {};
4068 // Parent constructor
4069 OO
.ui
.FieldsetLayout
.super.call( this, config
);
4071 // Mixin constructors
4072 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
4073 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
4074 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4078 .addClass( 'oo-ui-fieldsetLayout' )
4079 .prepend( this.$icon
, this.$label
, this.$group
);
4080 if ( $.isArray( config
.items
) ) {
4081 this.addItems( config
.items
);
4087 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
4088 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
4089 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
4090 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
4092 /* Static Properties */
4094 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
4096 * Layout made of a field and optional label.
4099 * @extends OO.ui.Layout
4100 * @mixins OO.ui.LabeledElement
4102 * Available label alignment modes include:
4103 * - 'left': Label is before the field and aligned away from it, best for when the user will be
4104 * scanning for a specific label in a form with many fields
4105 * - 'right': Label is before the field and aligned toward it, best for forms the user is very
4106 * familiar with and will tab through field checking quickly to verify which field they are in
4107 * - 'top': Label is before the field and above it, best for when the use will need to fill out all
4108 * fields from top to bottom in a form with few fields
4109 * - 'inline': Label is after the field and aligned toward it, best for small boolean fields like
4110 * checkboxes or radio buttons
4113 * @param {OO.ui.Widget} field Field widget
4114 * @param {Object} [config] Configuration options
4115 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
4117 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
4118 // Config initialization
4119 config
= $.extend( { 'align': 'left' }, config
);
4121 // Parent constructor
4122 OO
.ui
.FieldLayout
.super.call( this, config
);
4124 // Mixin constructors
4125 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
4128 this.$field
= this.$( '<div>' );
4133 if ( this.field
instanceof OO
.ui
.InputWidget
) {
4134 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
4136 this.field
.connect( this, { 'disable': 'onFieldDisable' } );
4139 this.$element
.addClass( 'oo-ui-fieldLayout' );
4141 .addClass( 'oo-ui-fieldLayout-field' )
4142 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
4143 .append( this.field
.$element
);
4144 this.setAlignment( config
.align
);
4149 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
4150 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
4155 * Handle field disable events.
4157 * @param {boolean} value Field is disabled
4159 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
4160 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
4164 * Handle label mouse click events.
4166 * @param {jQuery.Event} e Mouse click event
4168 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
4169 this.field
.simulateLabelClick();
4176 * @return {OO.ui.Widget} Field widget
4178 OO
.ui
.FieldLayout
.prototype.getField = function () {
4183 * Set the field alignment mode.
4185 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
4188 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
4189 if ( value
!== this.align
) {
4190 // Default to 'left'
4191 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
4195 if ( value
=== 'inline' ) {
4196 this.$element
.append( this.$field
, this.$label
);
4198 this.$element
.append( this.$label
, this.$field
);
4202 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
4205 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
4211 * Layout made of proportionally sized columns and rows.
4214 * @extends OO.ui.Layout
4217 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
4218 * @param {Object} [config] Configuration options
4219 * @cfg {number[]} [widths] Widths of columns as ratios
4220 * @cfg {number[]} [heights] Heights of columns as ratios
4222 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
4225 // Config initialization
4226 config
= config
|| {};
4228 // Parent constructor
4229 OO
.ui
.GridLayout
.super.call( this, config
);
4237 this.$element
.addClass( 'oo-ui-gridLayout' );
4238 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
4239 this.panels
.push( panels
[i
] );
4240 this.$element
.append( panels
[i
].$element
);
4242 if ( config
.widths
|| config
.heights
) {
4243 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
4245 // Arrange in columns by default
4247 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
4250 this.layout( widths
, [ 1 ] );
4256 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
4268 /* Static Properties */
4270 OO
.ui
.GridLayout
.static.tagName
= 'div';
4275 * Set grid dimensions.
4277 * @param {number[]} widths Widths of columns as ratios
4278 * @param {number[]} heights Heights of rows as ratios
4280 * @throws {Error} If grid is not large enough to fit all panels
4282 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
4286 cols
= widths
.length
,
4287 rows
= heights
.length
;
4289 // Verify grid is big enough to fit panels
4290 if ( cols
* rows
< this.panels
.length
) {
4291 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
4294 // Sum up denominators
4295 for ( x
= 0; x
< cols
; x
++ ) {
4298 for ( y
= 0; y
< rows
; y
++ ) {
4304 for ( x
= 0; x
< cols
; x
++ ) {
4305 this.widths
[x
] = widths
[x
] / xd
;
4307 for ( y
= 0; y
< rows
; y
++ ) {
4308 this.heights
[y
] = heights
[y
] / yd
;
4312 this.emit( 'layout' );
4316 * Update panel positions and sizes.
4320 OO
.ui
.GridLayout
.prototype.update = function () {
4328 cols
= this.widths
.length
,
4329 rows
= this.heights
.length
;
4331 for ( y
= 0; y
< rows
; y
++ ) {
4332 for ( x
= 0; x
< cols
; x
++ ) {
4333 panel
= this.panels
[i
];
4334 width
= this.widths
[x
];
4335 height
= this.heights
[y
];
4337 'width': Math
.round( width
* 100 ) + '%',
4338 'height': Math
.round( height
* 100 ) + '%',
4339 'top': Math
.round( top
* 100 ) + '%'
4342 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
4343 dimensions
.right
= Math
.round( left
* 100 ) + '%';
4345 dimensions
.left
= Math
.round( left
* 100 ) + '%';
4347 panel
.$element
.css( dimensions
);
4355 this.emit( 'update' );
4359 * Get a panel at a given position.
4361 * The x and y position is affected by the current grid layout.
4363 * @param {number} x Horizontal position
4364 * @param {number} y Vertical position
4365 * @return {OO.ui.PanelLayout} The panel at the given postion
4367 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
4368 return this.panels
[( x
* this.widths
.length
) + y
];
4371 * Layout containing a series of pages.
4374 * @extends OO.ui.Layout
4377 * @param {Object} [config] Configuration options
4378 * @cfg {boolean} [continuous=false] Show all pages, one after another
4379 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
4380 * @cfg {boolean} [outlined=false] Show an outline
4381 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
4382 * @cfg {Object[]} [adders] List of adders for controls, each with name, icon and title properties
4384 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
4385 // Initialize configuration
4386 config
= config
|| {};
4388 // Parent constructor
4389 OO
.ui
.BookletLayout
.super.call( this, config
);
4392 this.currentPageName
= null;
4394 this.ignoreFocus
= false;
4395 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
4396 this.autoFocus
= config
.autoFocus
=== undefined ? true : !!config
.autoFocus
;
4397 this.outlineVisible
= false;
4398 this.outlined
= !!config
.outlined
;
4399 if ( this.outlined
) {
4400 this.editable
= !!config
.editable
;
4401 this.adders
= config
.adders
|| null;
4402 this.outlineControlsWidget
= null;
4403 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
4404 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
4405 this.gridLayout
= new OO
.ui
.GridLayout(
4406 [ this.outlinePanel
, this.stackLayout
],
4407 { '$': this.$, 'widths': [ 1, 2 ] }
4409 this.outlineVisible
= true;
4410 if ( this.editable
) {
4411 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
4413 { '$': this.$, 'adders': this.adders
}
4419 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
4420 if ( this.outlined
) {
4421 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
4423 if ( this.autoFocus
) {
4424 // Event 'focus' does not bubble, but 'focusin' does
4425 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
4429 this.$element
.addClass( 'oo-ui-bookletLayout' );
4430 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
4431 if ( this.outlined
) {
4432 this.outlinePanel
.$element
4433 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
4434 .append( this.outlineWidget
.$element
);
4435 if ( this.editable
) {
4436 this.outlinePanel
.$element
4437 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
4438 .append( this.outlineControlsWidget
.$element
);
4440 this.$element
.append( this.gridLayout
.$element
);
4442 this.$element
.append( this.stackLayout
.$element
);
4448 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
4454 * @param {OO.ui.PageLayout} page Current page
4459 * @param {OO.ui.PageLayout[]} page Added pages
4460 * @param {number} index Index pages were added at
4465 * @param {OO.ui.PageLayout[]} pages Removed pages
4471 * Handle stack layout focus.
4473 * @param {jQuery.Event} e Focusin event
4475 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
4478 // Find the page that an element was focused within
4479 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
4480 for ( name
in this.pages
) {
4481 // Check for page match, exclude current page to find only page changes
4482 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
4483 this.setPage( name
);
4490 * Handle stack layout set events.
4492 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
4494 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
4496 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
4497 if ( this.autoFocus
) {
4498 // Set focus to the first input if nothing on the page is focused yet
4499 if ( !page
.$element
.find( ':focus' ).length
) {
4500 page
.$element
.find( ':input:first' ).focus();
4508 * Handle outline widget select events.
4510 * @param {OO.ui.OptionWidget|null} item Selected item
4512 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
4514 this.setPage( item
.getData() );
4519 * Check if booklet has an outline.
4523 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
4524 return this.outlined
;
4528 * Check if booklet has editing controls.
4532 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
4533 return this.editable
;
4537 * Check if booklet has a visible outline.
4541 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
4542 return this.outlined
&& this.outlineVisible
;
4546 * Hide or show the outline.
4548 * @param {boolean} [show] Show outline, omit to invert current state
4551 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
4552 if ( this.outlined
) {
4553 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
4554 this.outlineVisible
= show
;
4555 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
4562 * Get the outline widget.
4564 * @param {OO.ui.PageLayout} page Page to be selected
4565 * @return {OO.ui.PageLayout|null} Closest page to another
4567 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
4568 var next
, prev
, level
,
4569 pages
= this.stackLayout
.getItems(),
4570 index
= $.inArray( page
, pages
);
4572 if ( index
!== -1 ) {
4573 next
= pages
[index
+ 1];
4574 prev
= pages
[index
- 1];
4575 // Prefer adjacent pages at the same level
4576 if ( this.outlined
) {
4577 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
4580 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
4586 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
4592 return prev
|| next
|| null;
4596 * Get the outline widget.
4598 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
4600 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
4601 return this.outlineWidget
;
4605 * Get the outline controls widget. If the outline is not editable, null is returned.
4607 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
4609 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
4610 return this.outlineControlsWidget
;
4614 * Get a page by name.
4616 * @param {string} name Symbolic name of page
4617 * @return {OO.ui.PageLayout|undefined} Page, if found
4619 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
4620 return this.pages
[name
];
4624 * Get the current page name.
4626 * @return {string|null} Current page name
4628 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
4629 return this.currentPageName
;
4633 * Add a page to the layout.
4635 * When pages are added with the same names as existing pages, the existing pages will be
4636 * automatically removed before the new pages are added.
4638 * @param {OO.ui.PageLayout[]} pages Pages to add
4639 * @param {number} index Index to insert pages after
4643 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
4644 var i
, len
, name
, page
, item
, currentIndex
,
4645 stackLayoutPages
= this.stackLayout
.getItems(),
4649 // Remove pages with same names
4650 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4652 name
= page
.getName();
4654 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
4655 // Correct the insertion index
4656 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
4657 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
4660 remove
.push( this.pages
[name
] );
4663 if ( remove
.length
) {
4664 this.removePages( remove
);
4668 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4670 name
= page
.getName();
4671 this.pages
[page
.getName()] = page
;
4672 if ( this.outlined
) {
4673 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { '$': this.$ } );
4674 page
.setOutlineItem( item
);
4679 if ( this.outlined
&& items
.length
) {
4680 this.outlineWidget
.addItems( items
, index
);
4681 this.updateOutlineWidget();
4683 this.stackLayout
.addItems( pages
, index
);
4684 this.emit( 'add', pages
, index
);
4690 * Remove a page from the layout.
4695 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
4696 var i
, len
, name
, page
,
4699 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4701 name
= page
.getName();
4702 delete this.pages
[name
];
4703 if ( this.outlined
) {
4704 items
.push( this.outlineWidget
.getItemFromData( name
) );
4705 page
.setOutlineItem( null );
4708 if ( this.outlined
&& items
.length
) {
4709 this.outlineWidget
.removeItems( items
);
4710 this.updateOutlineWidget();
4712 this.stackLayout
.removeItems( pages
);
4713 this.emit( 'remove', pages
);
4719 * Clear all pages from the layout.
4724 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
4726 pages
= this.stackLayout
.getItems();
4729 this.currentPageName
= null;
4730 if ( this.outlined
) {
4731 this.outlineWidget
.clearItems();
4732 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4733 pages
[i
].setOutlineItem( null );
4736 this.stackLayout
.clearItems();
4738 this.emit( 'remove', pages
);
4744 * Set the current page by name.
4747 * @param {string} name Symbolic name of page
4749 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
4751 page
= this.pages
[name
];
4753 if ( name
!== this.currentPageName
) {
4754 if ( this.outlined
) {
4755 selectedItem
= this.outlineWidget
.getSelectedItem();
4756 if ( selectedItem
&& selectedItem
.getData() !== name
) {
4757 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
4761 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
4762 this.pages
[this.currentPageName
].setActive( false );
4763 // Blur anything focused if the next page doesn't have anything focusable - this
4764 // is not needed if the next page has something focusable because once it is focused
4765 // this blur happens automatically
4766 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
4767 this.pages
[this.currentPageName
].$element
.find( ':focus' ).blur();
4770 this.currentPageName
= name
;
4771 this.stackLayout
.setItem( page
);
4772 page
.setActive( true );
4773 this.emit( 'set', page
);
4779 * Call this after adding or removing items from the OutlineWidget.
4783 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
4784 // Auto-select first item when nothing is selected anymore
4785 if ( !this.outlineWidget
.getSelectedItem() ) {
4786 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
4792 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
4795 * @extends OO.ui.Layout
4798 * @param {Object} [config] Configuration options
4799 * @cfg {boolean} [scrollable] Allow vertical scrolling
4800 * @cfg {boolean} [padded] Pad the content from the edges
4802 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
4803 // Config initialization
4804 config
= config
|| {};
4806 // Parent constructor
4807 OO
.ui
.PanelLayout
.super.call( this, config
);
4810 this.$element
.addClass( 'oo-ui-panelLayout' );
4811 if ( config
.scrollable
) {
4812 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
4815 if ( config
.padded
) {
4816 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
4822 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
4824 * Page within an booklet layout.
4827 * @extends OO.ui.PanelLayout
4830 * @param {string} name Unique symbolic name of page
4831 * @param {Object} [config] Configuration options
4832 * @param {string} [outlineItem] Outline item widget
4834 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
4835 // Configuration initialization
4836 config
= $.extend( { 'scrollable': true }, config
);
4838 // Parent constructor
4839 OO
.ui
.PageLayout
.super.call( this, config
);
4843 this.outlineItem
= config
.outlineItem
|| null;
4844 this.active
= false;
4847 this.$element
.addClass( 'oo-ui-pageLayout' );
4852 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4858 * @param {boolean} active Page is active
4866 * @return {string} Symbolic name of page
4868 OO
.ui
.PageLayout
.prototype.getName = function () {
4873 * Check if page is active.
4875 * @return {boolean} Page is active
4877 OO
.ui
.PageLayout
.prototype.isActive = function () {
4884 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
4886 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
4887 return this.outlineItem
;
4893 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
4896 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
4897 this.outlineItem
= outlineItem
;
4902 * Set page active state.
4904 * @param {boolean} Page is active
4907 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
4910 if ( active
!== this.active
) {
4911 this.active
= active
;
4912 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
4913 this.emit( 'active', this.active
);
4917 * Layout containing a series of mutually exclusive pages.
4920 * @extends OO.ui.PanelLayout
4921 * @mixins OO.ui.GroupElement
4924 * @param {Object} [config] Configuration options
4925 * @cfg {boolean} [continuous=false] Show all pages, one after another
4926 * @cfg {string} [icon=''] Symbolic icon name
4927 * @cfg {OO.ui.Layout[]} [items] Layouts to add
4929 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4930 // Config initialization
4931 config
= $.extend( { 'scrollable': true }, config
);
4933 // Parent constructor
4934 OO
.ui
.StackLayout
.super.call( this, config
);
4936 // Mixin constructors
4937 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4940 this.currentItem
= null;
4941 this.continuous
= !!config
.continuous
;
4944 this.$element
.addClass( 'oo-ui-stackLayout' );
4945 if ( this.continuous
) {
4946 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4948 if ( $.isArray( config
.items
) ) {
4949 this.addItems( config
.items
);
4955 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4956 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4962 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
4968 * Get the current item.
4970 * @return {OO.ui.Layout|null}
4972 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
4973 return this.currentItem
;
4977 * Unset the current item.
4980 * @param {OO.ui.StackLayout} layout
4983 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
4984 var prevItem
= this.currentItem
;
4985 if ( prevItem
=== null ) {
4989 this.currentItem
= null;
4990 this.emit( 'set', null );
4996 * Adding an existing item (by value) will move it.
4998 * @param {OO.ui.Layout[]} items Items to add
4999 * @param {number} [index] Index to insert items after
5002 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
5004 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
5006 if ( !this.currentItem
&& items
.length
) {
5007 this.setItem( items
[0] );
5016 * Items will be detached, not removed, so they can be used later.
5018 * @param {OO.ui.Layout[]} items Items to remove
5022 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
5024 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
5026 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
5027 if ( this.items
.length
) {
5028 this.setItem( this.items
[0] );
5030 this.unsetCurrentItem();
5040 * Items will be detached, not removed, so they can be used later.
5045 OO
.ui
.StackLayout
.prototype.clearItems = function () {
5046 this.unsetCurrentItem();
5047 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
5055 * Any currently shown item will be hidden.
5057 * FIXME: If the passed item to show has not been added in the items list, then
5058 * this method drops it and unsets the current item.
5060 * @param {OO.ui.Layout} item Item to show
5064 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
5067 if ( item
!== this.currentItem
) {
5068 if ( !this.continuous
) {
5069 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5070 this.items
[i
].$element
.css( 'display', '' );
5073 if ( $.inArray( item
, this.items
) !== -1 ) {
5074 if ( !this.continuous
) {
5075 item
.$element
.css( 'display', 'block' );
5077 this.currentItem
= item
;
5078 this.emit( 'set', item
);
5080 this.unsetCurrentItem();
5087 * Horizontal bar layout of tools as icon buttons.
5090 * @extends OO.ui.ToolGroup
5093 * @param {OO.ui.Toolbar} toolbar
5094 * @param {Object} [config] Configuration options
5096 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
5097 // Parent constructor
5098 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
5101 this.$element
.addClass( 'oo-ui-barToolGroup' );
5106 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
5108 /* Static Properties */
5110 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
5112 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
5114 OO
.ui
.BarToolGroup
.static.name
= 'bar';
5116 * Popup list of tools with an icon and optional label.
5120 * @extends OO.ui.ToolGroup
5121 * @mixins OO.ui.IconedElement
5122 * @mixins OO.ui.IndicatedElement
5123 * @mixins OO.ui.LabeledElement
5124 * @mixins OO.ui.TitledElement
5125 * @mixins OO.ui.ClippableElement
5128 * @param {OO.ui.Toolbar} toolbar
5129 * @param {Object} [config] Configuration options
5130 * @cfg {string} [header] Text to display at the top of the pop-up
5132 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
5133 // Configuration initialization
5134 config
= config
|| {};
5136 // Parent constructor
5137 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
5139 // Mixin constructors
5140 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5141 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5142 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5143 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5144 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
5147 this.active
= false;
5148 this.dragging
= false;
5149 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
5150 this.$handle
= this.$( '<span>' );
5154 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
5155 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
5160 .addClass( 'oo-ui-popupToolGroup-handle' )
5161 .append( this.$icon
, this.$label
, this.$indicator
);
5162 // If the pop-up should have a header, add it to the top of the toolGroup.
5163 // Note: If this feature is useful for other widgets, we could abstract it into an
5164 // OO.ui.HeaderedElement mixin constructor.
5165 if ( config
.header
!== undefined ) {
5167 .prepend( this.$( '<span>' )
5168 .addClass( 'oo-ui-popupToolGroup-header' )
5169 .text( config
.header
)
5173 .addClass( 'oo-ui-popupToolGroup' )
5174 .prepend( this.$handle
);
5179 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
5180 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
5181 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
5182 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
5183 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
5184 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
5186 /* Static Properties */
5193 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
5195 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
5197 if ( this.isDisabled() && this.isElementAttached() ) {
5198 this.setActive( false );
5203 * Handle focus being lost.
5205 * The event is actually generated from a mouseup, so it is not a normal blur event object.
5207 * @param {jQuery.Event} e Mouse up event
5209 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
5210 // Only deactivate when clicking outside the dropdown element
5211 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
5212 this.setActive( false );
5219 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
5220 if ( !this.isDisabled() && e
.which
=== 1 ) {
5221 this.setActive( false );
5223 return OO
.ui
.PopupToolGroup
.super.prototype.onMouseUp
.call( this, e
);
5227 * Handle mouse up events.
5229 * @param {jQuery.Event} e Mouse up event
5231 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
5236 * Handle mouse down events.
5238 * @param {jQuery.Event} e Mouse down event
5240 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
5241 if ( !this.isDisabled() && e
.which
=== 1 ) {
5242 this.setActive( !this.active
);
5248 * Switch into active mode.
5250 * When active, mouseup events anywhere in the document will trigger deactivation.
5252 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
5254 if ( this.active
!== value
) {
5255 this.active
= value
;
5257 this.setClipping( true );
5258 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
5259 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
5261 this.setClipping( false );
5262 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
5263 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
5268 * Drop down list layout of tools as labeled icon buttons.
5271 * @extends OO.ui.PopupToolGroup
5274 * @param {OO.ui.Toolbar} toolbar
5275 * @param {Object} [config] Configuration options
5277 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
5278 // Parent constructor
5279 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
5282 this.$element
.addClass( 'oo-ui-listToolGroup' );
5287 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
5289 /* Static Properties */
5291 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
5293 OO
.ui
.ListToolGroup
.static.name
= 'list';
5295 * Drop down menu layout of tools as selectable menu items.
5298 * @extends OO.ui.PopupToolGroup
5301 * @param {OO.ui.Toolbar} toolbar
5302 * @param {Object} [config] Configuration options
5304 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
5305 // Configuration initialization
5306 config
= config
|| {};
5308 // Parent constructor
5309 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
5312 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
5315 this.$element
.addClass( 'oo-ui-menuToolGroup' );
5320 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
5322 /* Static Properties */
5324 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
5326 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
5331 * Handle the toolbar state being updated.
5333 * When the state changes, the title of each active item in the menu will be joined together and
5334 * used as a label for the group. The label will be empty if none of the items are active.
5336 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
5340 for ( name
in this.tools
) {
5341 if ( this.tools
[name
].isActive() ) {
5342 labelTexts
.push( this.tools
[name
].getTitle() );
5346 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
5349 * Tool that shows a popup when selected.
5353 * @extends OO.ui.Tool
5354 * @mixins OO.ui.PopuppableElement
5357 * @param {OO.ui.Toolbar} toolbar
5358 * @param {Object} [config] Configuration options
5360 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
5361 // Parent constructor
5362 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
5364 // Mixin constructors
5365 OO
.ui
.PopuppableElement
.call( this, config
);
5369 .addClass( 'oo-ui-popupTool' )
5370 .append( this.popup
.$element
);
5375 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
5376 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
5381 * Handle the tool being selected.
5385 OO
.ui
.PopupTool
.prototype.onSelect = function () {
5386 if ( !this.isDisabled() ) {
5387 if ( this.popup
.isVisible() ) {
5393 this.setActive( false );
5398 * Handle the toolbar state being updated.
5402 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
5403 this.setActive( false );
5408 * Mixin for OO.ui.Widget subclasses.
5410 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
5414 * @extends OO.ui.GroupElement
5417 * @param {jQuery} $group Container node, assigned to #$group
5418 * @param {Object} [config] Configuration options
5420 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
5421 // Parent constructor
5422 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
5427 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
5432 * Set the disabled state of the widget.
5434 * This will also update the disabled state of child widgets.
5436 * @param {boolean} disabled Disable widget
5439 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
5443 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
5444 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
5446 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
5448 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5449 this.items
[i
].updateDisabled();
5458 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
5465 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
5472 * Check if widget is disabled.
5474 * Checks parent if present, making disabled state inheritable.
5476 * @return {boolean} Widget is disabled
5478 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
5479 return this.disabled
||
5480 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
5484 * Set group element is in.
5486 * @param {OO.ui.GroupElement|null} group Group element, null if none
5489 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
5491 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
5492 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
5494 // Initialize item disabled states
5495 this.updateDisabled();
5503 * @extends OO.ui.Widget
5504 * @mixins OO.ui.IconedElement
5505 * @mixins OO.ui.TitledElement
5508 * @param {Object} [config] Configuration options
5510 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
5511 // Config intialization
5512 config
= config
|| {};
5514 // Parent constructor
5515 OO
.ui
.IconWidget
.super.call( this, config
);
5517 // Mixin constructors
5518 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
5519 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5522 this.$element
.addClass( 'oo-ui-iconWidget' );
5527 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
5528 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
5529 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
5531 /* Static Properties */
5533 OO
.ui
.IconWidget
.static.tagName
= 'span';
5538 * @extends OO.ui.Widget
5539 * @mixins OO.ui.IndicatedElement
5540 * @mixins OO.ui.TitledElement
5543 * @param {Object} [config] Configuration options
5545 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
5546 // Config intialization
5547 config
= config
|| {};
5549 // Parent constructor
5550 OO
.ui
.IndicatorWidget
.super.call( this, config
);
5552 // Mixin constructors
5553 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
5554 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5557 this.$element
.addClass( 'oo-ui-indicatorWidget' );
5562 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
5563 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
5564 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
5566 /* Static Properties */
5568 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
5570 * Container for multiple related buttons.
5572 * Use together with OO.ui.ButtonWidget.
5575 * @extends OO.ui.Widget
5576 * @mixins OO.ui.GroupElement
5579 * @param {Object} [config] Configuration options
5580 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
5582 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
5583 // Parent constructor
5584 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
5586 // Mixin constructors
5587 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
5590 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
5591 if ( $.isArray( config
.items
) ) {
5592 this.addItems( config
.items
);
5598 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
5599 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
5604 * @extends OO.ui.Widget
5605 * @mixins OO.ui.ButtonedElement
5606 * @mixins OO.ui.IconedElement
5607 * @mixins OO.ui.IndicatedElement
5608 * @mixins OO.ui.LabeledElement
5609 * @mixins OO.ui.TitledElement
5610 * @mixins OO.ui.FlaggableElement
5613 * @param {Object} [config] Configuration options
5614 * @cfg {string} [title=''] Title text
5615 * @cfg {string} [href] Hyperlink to visit when clicked
5616 * @cfg {string} [target] Target to open hyperlink in
5618 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
5619 // Configuration initialization
5620 config
= $.extend( { 'target': '_blank' }, config
);
5622 // Parent constructor
5623 OO
.ui
.ButtonWidget
.super.call( this, config
);
5625 // Mixin constructors
5626 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
5627 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5628 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5629 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5630 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
5631 OO
.ui
.FlaggableElement
.call( this, config
);
5634 this.isHyperlink
= typeof config
.href
=== 'string';
5638 'click': OO
.ui
.bind( this.onClick
, this ),
5639 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
5644 .append( this.$icon
, this.$label
, this.$indicator
)
5645 .attr( { 'href': config
.href
, 'target': config
.target
} );
5647 .addClass( 'oo-ui-buttonWidget' )
5648 .append( this.$button
);
5653 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
5654 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
5655 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
5656 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
5657 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
5658 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
5659 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
5670 * Handles mouse click events.
5672 * @param {jQuery.Event} e Mouse click event
5675 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
5676 if ( !this.isDisabled() ) {
5677 this.emit( 'click' );
5678 if ( this.isHyperlink
) {
5686 * Handles keypress events.
5688 * @param {jQuery.Event} e Keypress event
5691 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
5692 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
5694 if ( this.isHyperlink
) {
5705 * @extends OO.ui.Widget
5708 * @param {Object} [config] Configuration options
5709 * @cfg {string} [name=''] HTML input name
5710 * @cfg {string} [value=''] Input value
5711 * @cfg {boolean} [readOnly=false] Prevent changes
5712 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
5714 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
5715 // Config intialization
5716 config
= $.extend( { 'readOnly': false }, config
);
5718 // Parent constructor
5719 OO
.ui
.InputWidget
.super.call( this, config
);
5722 this.$input
= this.getInputElement( config
);
5724 this.readOnly
= false;
5725 this.inputFilter
= config
.inputFilter
;
5728 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
5732 .attr( 'name', config
.name
)
5733 .prop( 'disabled', this.isDisabled() );
5734 this.setReadOnly( config
.readOnly
);
5735 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
5736 this.setValue( config
.value
);
5741 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
5753 * Get input element.
5755 * @param {Object} [config] Configuration options
5756 * @return {jQuery} Input element
5758 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
5759 return this.$( '<input>' );
5763 * Handle potentially value-changing events.
5765 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
5767 OO
.ui
.InputWidget
.prototype.onEdit = function () {
5768 if ( !this.isDisabled() ) {
5769 // Allow the stack to clear so the value will be updated
5770 setTimeout( OO
.ui
.bind( function () {
5771 this.setValue( this.$input
.val() );
5777 * Get the value of the input.
5779 * @return {string} Input value
5781 OO
.ui
.InputWidget
.prototype.getValue = function () {
5786 * Sets the direction of the current input, either RTL or LTR
5788 * @param {boolean} isRTL
5790 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
5792 this.$input
.removeClass( 'oo-ui-ltr' );
5793 this.$input
.addClass( 'oo-ui-rtl' );
5795 this.$input
.removeClass( 'oo-ui-rtl' );
5796 this.$input
.addClass( 'oo-ui-ltr' );
5801 * Set the value of the input.
5803 * @param {string} value New value
5807 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
5808 value
= this.sanitizeValue( value
);
5809 if ( this.value
!== value
) {
5811 this.emit( 'change', this.value
);
5813 // Update the DOM if it has changed. Note that with sanitizeValue, it
5814 // is possible for the DOM value to change without this.value changing.
5815 if ( this.$input
.val() !== this.value
) {
5816 this.$input
.val( this.value
);
5822 * Sanitize incoming value.
5824 * Ensures value is a string, and converts undefined and null to empty strings.
5826 * @param {string} value Original value
5827 * @return {string} Sanitized value
5829 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
5830 if ( value
=== undefined || value
=== null ) {
5832 } else if ( this.inputFilter
) {
5833 return this.inputFilter( String( value
) );
5835 return String( value
);
5840 * Simulate the behavior of clicking on a label bound to this input.
5842 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
5843 if ( !this.isDisabled() ) {
5844 if ( this.$input
.is( ':checkbox,:radio' ) ) {
5845 this.$input
.click();
5846 } else if ( this.$input
.is( ':input' ) ) {
5847 this.$input
.focus();
5853 * Check if the widget is read-only.
5857 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
5858 return this.readOnly
;
5862 * Set the read-only state of the widget.
5864 * This should probably change the widgets's appearance and prevent it from being used.
5866 * @param {boolean} state Make input read-only
5869 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
5870 this.readOnly
= !!state
;
5871 this.$input
.prop( 'readOnly', this.readOnly
);
5878 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
5879 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
5880 if ( this.$input
) {
5881 this.$input
.prop( 'disabled', this.isDisabled() );
5891 OO
.ui
.InputWidget
.prototype.focus = function () {
5892 this.$input
.focus();
5899 * @extends OO.ui.InputWidget
5902 * @param {Object} [config] Configuration options
5904 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
5905 // Parent constructor
5906 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
5909 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
5914 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
5921 * Get input element.
5923 * @return {jQuery} Input element
5925 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
5926 return this.$( '<input type="checkbox" />' );
5930 * Get checked state of the checkbox
5932 * @return {boolean} If the checkbox is checked
5934 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
5941 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
5943 if ( this.value
!== value
) {
5945 this.$input
.prop( 'checked', this.value
);
5946 this.emit( 'change', this.value
);
5953 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
5954 if ( !this.isDisabled() ) {
5955 // Allow the stack to clear so the value will be updated
5956 setTimeout( OO
.ui
.bind( function () {
5957 this.setValue( this.$input
.prop( 'checked' ) );
5965 * @extends OO.ui.Widget
5966 * @mixins OO.ui.LabeledElement
5969 * @param {Object} [config] Configuration options
5971 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
5972 // Config intialization
5973 config
= config
|| {};
5975 // Parent constructor
5976 OO
.ui
.LabelWidget
.super.call( this, config
);
5978 // Mixin constructors
5979 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
5982 this.input
= config
.input
;
5985 if ( this.input
instanceof OO
.ui
.InputWidget
) {
5986 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
5990 this.$element
.addClass( 'oo-ui-labelWidget' );
5995 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
5996 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
5998 /* Static Properties */
6000 OO
.ui
.LabelWidget
.static.tagName
= 'label';
6005 * Handles label mouse click events.
6007 * @param {jQuery.Event} e Mouse click event
6009 OO
.ui
.LabelWidget
.prototype.onClick = function () {
6010 this.input
.simulateLabelClick();
6014 * Lookup input widget.
6016 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
6017 * and `choose` events on #lookupMenu to make use of selections.
6023 * @param {OO.ui.TextInputWidget} input Input widget
6024 * @param {Object} [config] Configuration options
6025 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
6027 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
6028 // Config intialization
6029 config
= config
|| {};
6032 this.lookupInput
= input
;
6033 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
6034 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
6035 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
6036 'input': this.lookupInput
,
6037 '$container': config
.$container
6039 this.lookupCache
= {};
6040 this.lookupQuery
= null;
6041 this.lookupRequest
= null;
6042 this.populating
= false;
6045 this.$overlay
.append( this.lookupMenu
.$element
);
6047 this.lookupInput
.$input
.on( {
6048 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
6049 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
6050 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
6052 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
6055 this.$element
.addClass( 'oo-ui-lookupWidget' );
6056 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
6062 * Handle input focus event.
6064 * @param {jQuery.Event} e Input focus event
6066 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
6067 this.openLookupMenu();
6071 * Handle input blur event.
6073 * @param {jQuery.Event} e Input blur event
6075 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
6076 this.lookupMenu
.hide();
6080 * Handle input mouse down event.
6082 * @param {jQuery.Event} e Input mouse down event
6084 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
6085 this.openLookupMenu();
6089 * Handle input change event.
6091 * @param {string} value New input value
6093 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
6094 this.openLookupMenu();
6100 * @return {OO.ui.TextInputMenuWidget}
6102 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
6103 return this.lookupMenu
;
6111 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
6112 var value
= this.lookupInput
.getValue();
6114 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
6115 this.populateLookupMenu();
6116 if ( !this.lookupMenu
.isVisible() ) {
6117 this.lookupMenu
.show();
6120 this.lookupMenu
.clearItems();
6121 this.lookupMenu
.hide();
6128 * Populate lookup menu with current information.
6132 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
6133 if ( !this.populating
) {
6134 this.populating
= true;
6135 this.getLookupMenuItems()
6136 .done( OO
.ui
.bind( function ( items
) {
6137 this.lookupMenu
.clearItems();
6138 if ( items
.length
) {
6139 this.lookupMenu
.show();
6140 this.lookupMenu
.addItems( items
);
6141 this.initializeLookupMenuSelection();
6142 this.openLookupMenu();
6144 this.lookupMenu
.hide();
6146 this.populating
= false;
6148 .fail( OO
.ui
.bind( function () {
6149 this.lookupMenu
.clearItems();
6150 this.populating
= false;
6158 * Set selection in the lookup menu with current information.
6162 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
6163 if ( !this.lookupMenu
.getSelectedItem() ) {
6164 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
6166 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
6170 * Get lookup menu items for the current query.
6172 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
6175 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
6176 var value
= this.lookupInput
.getValue(),
6177 deferred
= $.Deferred();
6179 if ( value
&& value
!== this.lookupQuery
) {
6180 // Abort current request if query has changed
6181 if ( this.lookupRequest
) {
6182 this.lookupRequest
.abort();
6183 this.lookupQuery
= null;
6184 this.lookupRequest
= null;
6186 if ( value
in this.lookupCache
) {
6187 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6189 this.lookupQuery
= value
;
6190 this.lookupRequest
= this.getLookupRequest()
6191 .always( OO
.ui
.bind( function () {
6192 this.lookupQuery
= null;
6193 this.lookupRequest
= null;
6195 .done( OO
.ui
.bind( function ( data
) {
6196 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
6197 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6199 .fail( function () {
6203 this.lookupRequest
.always( OO
.ui
.bind( function () {
6208 return deferred
.promise();
6212 * Get a new request object of the current lookup query value.
6215 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
6217 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
6218 // Stub, implemented in subclass
6223 * Handle successful lookup request.
6225 * Overriding methods should call #populateLookupMenu when results are available and cache results
6226 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
6229 * @param {Mixed} data Response from server
6231 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
6232 // Stub, implemented in subclass
6236 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
6239 * @param {Mixed} data Cached result data, usually an array
6240 * @return {OO.ui.MenuItemWidget[]} Menu items
6242 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
6243 // Stub, implemented in subclass
6249 * Use with OO.ui.SelectWidget.
6252 * @extends OO.ui.Widget
6253 * @mixins OO.ui.IconedElement
6254 * @mixins OO.ui.LabeledElement
6255 * @mixins OO.ui.IndicatedElement
6256 * @mixins OO.ui.FlaggableElement
6259 * @param {Mixed} data Option data
6260 * @param {Object} [config] Configuration options
6261 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
6263 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
6264 // Config intialization
6265 config
= config
|| {};
6267 // Parent constructor
6268 OO
.ui
.OptionWidget
.super.call( this, config
);
6270 // Mixin constructors
6271 OO
.ui
.ItemWidget
.call( this );
6272 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6273 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6274 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6275 OO
.ui
.FlaggableElement
.call( this, config
);
6279 this.selected
= false;
6280 this.highlighted
= false;
6281 this.pressed
= false;
6285 .data( 'oo-ui-optionWidget', this )
6286 .attr( 'rel', config
.rel
)
6287 .addClass( 'oo-ui-optionWidget' )
6288 .append( this.$label
);
6290 .prepend( this.$icon
)
6291 .append( this.$indicator
);
6296 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
6297 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
6298 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
6299 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
6300 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
6301 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
6303 /* Static Properties */
6305 OO
.ui
.OptionWidget
.static.tagName
= 'li';
6307 OO
.ui
.OptionWidget
.static.selectable
= true;
6309 OO
.ui
.OptionWidget
.static.highlightable
= true;
6311 OO
.ui
.OptionWidget
.static.pressable
= true;
6313 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
6318 * Check if option can be selected.
6320 * @return {boolean} Item is selectable
6322 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
6323 return this.constructor.static.selectable
&& !this.isDisabled();
6327 * Check if option can be highlighted.
6329 * @return {boolean} Item is highlightable
6331 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
6332 return this.constructor.static.highlightable
&& !this.isDisabled();
6336 * Check if option can be pressed.
6338 * @return {boolean} Item is pressable
6340 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
6341 return this.constructor.static.pressable
&& !this.isDisabled();
6345 * Check if option is selected.
6347 * @return {boolean} Item is selected
6349 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
6350 return this.selected
;
6354 * Check if option is highlighted.
6356 * @return {boolean} Item is highlighted
6358 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
6359 return this.highlighted
;
6363 * Check if option is pressed.
6365 * @return {boolean} Item is pressed
6367 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
6368 return this.pressed
;
6372 * Set selected state.
6374 * @param {boolean} [state=false] Select option
6377 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
6378 if ( this.constructor.static.selectable
) {
6379 this.selected
= !!state
;
6380 if ( this.selected
) {
6381 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
6382 if ( this.constructor.static.scrollIntoViewOnSelect
) {
6383 this.scrollElementIntoView();
6386 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
6393 * Set highlighted state.
6395 * @param {boolean} [state=false] Highlight option
6398 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
6399 if ( this.constructor.static.highlightable
) {
6400 this.highlighted
= !!state
;
6401 if ( this.highlighted
) {
6402 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
6404 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
6411 * Set pressed state.
6413 * @param {boolean} [state=false] Press option
6416 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
6417 if ( this.constructor.static.pressable
) {
6418 this.pressed
= !!state
;
6419 if ( this.pressed
) {
6420 this.$element
.addClass( 'oo-ui-optionWidget-pressed' );
6422 this.$element
.removeClass( 'oo-ui-optionWidget-pressed' );
6429 * Make the option's highlight flash.
6431 * While flashing, the visual style of the pressed state is removed if present.
6433 * @return {jQuery.Promise} Promise resolved when flashing is done
6435 OO
.ui
.OptionWidget
.prototype.flash = function () {
6436 var $this = this.$element
,
6437 deferred
= $.Deferred();
6439 if ( !this.isDisabled() && this.constructor.static.pressable
) {
6440 $this.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
6441 setTimeout( OO
.ui
.bind( function () {
6442 // Restore original classes
6444 .toggleClass( 'oo-ui-optionWidget-highlighted', this.highlighted
)
6445 .toggleClass( 'oo-ui-optionWidget-pressed', this.pressed
);
6446 setTimeout( function () {
6452 return deferred
.promise();
6458 * @return {Mixed} Option data
6460 OO
.ui
.OptionWidget
.prototype.getData = function () {
6464 * Selection of options.
6466 * Use together with OO.ui.OptionWidget.
6469 * @extends OO.ui.Widget
6470 * @mixins OO.ui.GroupElement
6473 * @param {Object} [config] Configuration options
6474 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
6476 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
6477 // Config intialization
6478 config
= config
|| {};
6480 // Parent constructor
6481 OO
.ui
.SelectWidget
.super.call( this, config
);
6483 // Mixin constructors
6484 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
6487 this.pressed
= false;
6488 this.selecting
= null;
6490 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
6491 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
6495 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
6496 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
6497 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
6501 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
6502 if ( $.isArray( config
.items
) ) {
6503 this.addItems( config
.items
);
6509 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
6511 // Need to mixin base class as well
6512 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
6513 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
6519 * @param {OO.ui.OptionWidget|null} item Highlighted item
6524 * @param {OO.ui.OptionWidget|null} item Pressed item
6529 * @param {OO.ui.OptionWidget|null} item Selected item
6534 * @param {OO.ui.OptionWidget|null} item Chosen item
6539 * @param {OO.ui.OptionWidget[]} items Added items
6540 * @param {number} index Index items were added at
6545 * @param {OO.ui.OptionWidget[]} items Removed items
6548 /* Static Properties */
6550 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
6555 * Handle mouse down events.
6558 * @param {jQuery.Event} e Mouse down event
6560 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
6563 if ( !this.isDisabled() && e
.which
=== 1 ) {
6564 this.togglePressed( true );
6565 item
= this.getTargetItem( e
);
6566 if ( item
&& item
.isSelectable() ) {
6567 this.pressItem( item
);
6568 this.selecting
= item
;
6569 this.getElementDocument().addEventListener(
6570 'mouseup', this.onMouseUpHandler
, true
6572 this.getElementDocument().addEventListener(
6573 'mousemove', this.onMouseMoveHandler
, true
6581 * Handle mouse up events.
6584 * @param {jQuery.Event} e Mouse up event
6586 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
6589 this.togglePressed( false );
6590 if ( !this.selecting
) {
6591 item
= this.getTargetItem( e
);
6592 if ( item
&& item
.isSelectable() ) {
6593 this.selecting
= item
;
6596 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
6597 this.pressItem( null );
6598 this.chooseItem( this.selecting
);
6599 this.selecting
= null;
6602 this.getElementDocument().removeEventListener(
6603 'mouseup', this.onMouseUpHandler
, true
6605 this.getElementDocument().removeEventListener(
6606 'mousemove', this.onMouseMoveHandler
, true
6613 * Handle mouse move events.
6616 * @param {jQuery.Event} e Mouse move event
6618 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
6621 if ( !this.isDisabled() && this.pressed
) {
6622 item
= this.getTargetItem( e
);
6623 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
6624 this.pressItem( item
);
6625 this.selecting
= item
;
6632 * Handle mouse over events.
6635 * @param {jQuery.Event} e Mouse over event
6637 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
6640 if ( !this.isDisabled() ) {
6641 item
= this.getTargetItem( e
);
6642 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
6648 * Handle mouse leave events.
6651 * @param {jQuery.Event} e Mouse over event
6653 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
6654 if ( !this.isDisabled() ) {
6655 this.highlightItem( null );
6661 * Get the closest item to a jQuery.Event.
6664 * @param {jQuery.Event} e
6665 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
6667 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
6668 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
6669 if ( $item
.length
) {
6670 return $item
.data( 'oo-ui-optionWidget' );
6676 * Get selected item.
6678 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
6680 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
6683 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6684 if ( this.items
[i
].isSelected() ) {
6685 return this.items
[i
];
6692 * Get highlighted item.
6694 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
6696 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
6699 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6700 if ( this.items
[i
].isHighlighted() ) {
6701 return this.items
[i
];
6708 * Get an existing item with equivilant data.
6710 * @param {Object} data Item data to search for
6711 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
6713 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
6714 var hash
= OO
.getHash( data
);
6716 if ( hash
in this.hashes
) {
6717 return this.hashes
[hash
];
6724 * Toggle pressed state.
6726 * @param {boolean} pressed An option is being pressed
6728 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
6729 if ( pressed
=== undefined ) {
6730 pressed
= !this.pressed
;
6732 if ( pressed
!== this.pressed
) {
6733 this.$element
.toggleClass( 'oo-ui-selectWidget-pressed', pressed
);
6734 this.$element
.toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
6735 this.pressed
= pressed
;
6740 * Highlight an item.
6742 * Highlighting is mutually exclusive.
6744 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
6748 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
6749 var i
, len
, highlighted
,
6752 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6753 highlighted
= this.items
[i
] === item
;
6754 if ( this.items
[i
].isHighlighted() !== highlighted
) {
6755 this.items
[i
].setHighlighted( highlighted
);
6760 this.emit( 'highlight', item
);
6769 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6773 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
6774 var i
, len
, selected
,
6777 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6778 selected
= this.items
[i
] === item
;
6779 if ( this.items
[i
].isSelected() !== selected
) {
6780 this.items
[i
].setSelected( selected
);
6785 this.emit( 'select', item
);
6794 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
6798 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
6799 var i
, len
, pressed
,
6802 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6803 pressed
= this.items
[i
] === item
;
6804 if ( this.items
[i
].isPressed() !== pressed
) {
6805 this.items
[i
].setPressed( pressed
);
6810 this.emit( 'press', item
);
6819 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
6820 * an item is selected using the keyboard or mouse.
6822 * @param {OO.ui.OptionWidget} item Item to choose
6826 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
6827 this.selectItem( item
);
6828 this.emit( 'choose', item
);
6834 * Get an item relative to another one.
6836 * @param {OO.ui.OptionWidget} item Item to start at
6837 * @param {number} direction Direction to move in
6838 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
6840 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
6841 var inc
= direction
> 0 ? 1 : -1,
6842 len
= this.items
.length
,
6843 index
= item
instanceof OO
.ui
.OptionWidget
?
6844 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
6845 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
6847 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
6848 Math
.max( index
, -1 ) :
6849 // Default to n-1 instead of -1, if nothing is selected let's start at the end
6850 Math
.min( index
, len
);
6853 i
= ( i
+ inc
+ len
) % len
;
6854 item
= this.items
[i
];
6855 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6858 // Stop iterating when we've looped all the way around
6859 if ( i
=== stopAt
) {
6867 * Get the next selectable item.
6869 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
6871 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
6874 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6875 item
= this.items
[i
];
6876 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6887 * When items are added with the same values as existing items, the existing items will be
6888 * automatically removed before the new items are added.
6890 * @param {OO.ui.OptionWidget[]} items Items to add
6891 * @param {number} [index] Index to insert items after
6895 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
6896 var i
, len
, item
, hash
,
6899 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6901 hash
= OO
.getHash( item
.getData() );
6902 if ( hash
in this.hashes
) {
6903 // Remove item with same value
6904 remove
.push( this.hashes
[hash
] );
6906 this.hashes
[hash
] = item
;
6908 if ( remove
.length
) {
6909 this.removeItems( remove
);
6913 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
6915 // Always provide an index, even if it was omitted
6916 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
6924 * Items will be detached, not removed, so they can be used later.
6926 * @param {OO.ui.OptionWidget[]} items Items to remove
6930 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
6931 var i
, len
, item
, hash
;
6933 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6935 hash
= OO
.getHash( item
.getData() );
6936 if ( hash
in this.hashes
) {
6937 // Remove existing item
6938 delete this.hashes
[hash
];
6940 if ( item
.isSelected() ) {
6941 this.selectItem( null );
6946 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
6948 this.emit( 'remove', items
);
6956 * Items will be detached, not removed, so they can be used later.
6961 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
6962 var items
= this.items
.slice();
6967 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
6968 this.selectItem( null );
6970 this.emit( 'remove', items
);
6977 * Use with OO.ui.MenuWidget.
6980 * @extends OO.ui.OptionWidget
6983 * @param {Mixed} data Item data
6984 * @param {Object} [config] Configuration options
6986 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
6987 // Configuration initialization
6988 config
= $.extend( { 'icon': 'check' }, config
);
6990 // Parent constructor
6991 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
6994 this.$element
.addClass( 'oo-ui-menuItemWidget' );
6999 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
7003 * Use together with OO.ui.MenuItemWidget.
7006 * @extends OO.ui.SelectWidget
7007 * @mixins OO.ui.ClippableElement
7010 * @param {Object} [config] Configuration options
7011 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
7012 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
7014 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
7015 // Config intialization
7016 config
= config
|| {};
7018 // Parent constructor
7019 OO
.ui
.MenuWidget
.super.call( this, config
);
7021 // Mixin constructors
7022 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
7025 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
7026 this.newItems
= null;
7027 this.$input
= config
.input
? config
.input
.$input
: null;
7028 this.$previousFocus
= null;
7029 this.isolated
= !config
.input
;
7030 this.visible
= false;
7031 this.flashing
= false;
7032 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
7033 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
7036 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
7041 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
7042 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
7047 * Handles document mouse down events.
7049 * @param {jQuery.Event} e Key down event
7051 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
7052 if ( !$.contains( this.$element
[0], e
.target
) ) {
7058 * Handles key down events.
7060 * @param {jQuery.Event} e Key down event
7062 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
7065 highlightItem
= this.getHighlightedItem();
7067 if ( !this.isDisabled() && this.visible
) {
7068 if ( !highlightItem
) {
7069 highlightItem
= this.getSelectedItem();
7071 switch ( e
.keyCode
) {
7072 case OO
.ui
.Keys
.ENTER
:
7073 this.chooseItem( highlightItem
);
7077 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
7080 case OO
.ui
.Keys
.DOWN
:
7081 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
7084 case OO
.ui
.Keys
.ESCAPE
:
7085 if ( highlightItem
) {
7086 highlightItem
.setHighlighted( false );
7094 this.highlightItem( nextItem
);
7095 nextItem
.scrollElementIntoView();
7100 e
.stopPropagation();
7107 * Check if the menu is visible.
7109 * @return {boolean} Menu is visible
7111 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
7112 return this.visible
;
7116 * Bind key down listener.
7118 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
7119 if ( this.$input
) {
7120 this.$input
.on( 'keydown', this.onKeyDownHandler
);
7122 // Capture menu navigation keys
7123 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
7128 * Unbind key down listener.
7130 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
7131 if ( this.$input
) {
7132 this.$input
.off( 'keydown' );
7134 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
7141 * This will close the menu when done, unlike selectItem which only changes selection.
7143 * @param {OO.ui.OptionWidget} item Item to choose
7146 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
7148 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
7150 if ( item
&& !this.flashing
) {
7151 this.flashing
= true;
7152 item
.flash().done( OO
.ui
.bind( function () {
7154 this.flashing
= false;
7166 * Adding an existing item (by value) will move it.
7168 * @param {OO.ui.MenuItemWidget[]} items Items to add
7169 * @param {number} [index] Index to insert items after
7172 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
7176 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
7179 if ( !this.newItems
) {
7183 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
7185 if ( this.visible
) {
7186 // Defer fitting label until
7189 this.newItems
.push( item
);
7201 OO
.ui
.MenuWidget
.prototype.show = function () {
7204 if ( this.items
.length
) {
7205 this.$element
.show();
7206 this.visible
= true;
7207 this.bindKeyDownListener();
7209 // Change focus to enable keyboard navigation
7210 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
7211 this.$previousFocus
= this.$( ':focus' );
7212 this.$input
.focus();
7214 if ( this.newItems
&& this.newItems
.length
) {
7215 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
7216 this.newItems
[i
].fitLabel();
7218 this.newItems
= null;
7221 this.setClipping( true );
7224 if ( this.autoHide
) {
7225 this.getElementDocument().addEventListener(
7226 'mousedown', this.onDocumentMouseDownHandler
, true
7239 OO
.ui
.MenuWidget
.prototype.hide = function () {
7240 this.$element
.hide();
7241 this.visible
= false;
7242 this.unbindKeyDownListener();
7244 if ( this.isolated
&& this.$previousFocus
) {
7245 this.$previousFocus
.focus();
7246 this.$previousFocus
= null;
7249 this.getElementDocument().removeEventListener(
7250 'mousedown', this.onDocumentMouseDownHandler
, true
7253 this.setClipping( false );
7258 * Inline menu of options.
7260 * Use with OO.ui.MenuOptionWidget.
7263 * @extends OO.ui.Widget
7264 * @mixins OO.ui.IconedElement
7265 * @mixins OO.ui.IndicatedElement
7266 * @mixins OO.ui.LabeledElement
7267 * @mixins OO.ui.TitledElement
7270 * @param {Object} [config] Configuration options
7271 * @cfg {Object} [menu] Configuration options to pass to menu widget
7273 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
7274 // Configuration initialization
7275 config
= $.extend( { 'indicator': 'down' }, config
);
7277 // Parent constructor
7278 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
7280 // Mixin constructors
7281 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
7282 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
7283 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
7284 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
7287 this.menu
= new OO
.ui
.MenuWidget( $.extend( { '$': this.$ }, config
.menu
) );
7288 this.$handle
= this.$( '<span>' );
7291 this.$element
.on( { 'click': OO
.ui
.bind( this.onClick
, this ) } );
7292 this.menu
.connect( this, { 'select': 'onMenuSelect' } );
7296 .addClass( 'oo-ui-inlineMenuWidget-handle' )
7297 .append( this.$icon
, this.$label
, this.$indicator
);
7299 .addClass( 'oo-ui-inlineMenuWidget' )
7300 .append( this.$handle
, this.menu
.$element
);
7305 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
7306 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
7307 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
7308 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
7309 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
7316 * @return {OO.ui.MenuWidget} Menu of widget
7318 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
7323 * Handles menu select events.
7325 * @param {OO.ui.MenuItemWidget} item Selected menu item
7327 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
7334 selectedLabel
= item
.getLabel();
7336 // If the label is a DOM element, clone it, because setLabel will append() it
7337 if ( selectedLabel
instanceof jQuery
) {
7338 selectedLabel
= selectedLabel
.clone();
7341 this.setLabel( selectedLabel
);
7345 * Handles mouse click events.
7347 * @param {jQuery.Event} e Mouse click event
7349 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
7350 // Skip clicks within the menu
7351 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
7355 if ( !this.isDisabled() ) {
7356 if ( this.menu
.isVisible() ) {
7365 * Menu section item widget.
7367 * Use with OO.ui.MenuWidget.
7370 * @extends OO.ui.OptionWidget
7373 * @param {Mixed} data Item data
7374 * @param {Object} [config] Configuration options
7376 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
7377 // Parent constructor
7378 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
7381 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
7386 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
7388 /* Static Properties */
7390 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
7392 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
7394 * Create an OO.ui.OutlineWidget object.
7396 * Use with OO.ui.OutlineItemWidget.
7399 * @extends OO.ui.SelectWidget
7402 * @param {Object} [config] Configuration options
7404 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
7405 // Config intialization
7406 config
= config
|| {};
7408 // Parent constructor
7409 OO
.ui
.OutlineWidget
.super.call( this, config
);
7412 this.$element
.addClass( 'oo-ui-outlineWidget' );
7417 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
7419 * Creates an OO.ui.OutlineControlsWidget object.
7421 * Use together with OO.ui.OutlineWidget.js
7426 * @param {OO.ui.OutlineWidget} outline Outline to control
7427 * @param {Object} [config] Configuration options
7429 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7430 // Configuration initialization
7431 config
= $.extend( { 'icon': 'add-item' }, config
);
7433 // Parent constructor
7434 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7436 // Mixin constructors
7437 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
7438 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
7441 this.outline
= outline
;
7442 this.$movers
= this.$( '<div>' );
7443 this.upButton
= new OO
.ui
.ButtonWidget( {
7447 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
7449 this.downButton
= new OO
.ui
.ButtonWidget( {
7453 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
7455 this.removeButton
= new OO
.ui
.ButtonWidget( {
7459 'title': OO
.ui
.msg( 'ooui-outline-control-remove' )
7463 outline
.connect( this, {
7464 'select': 'onOutlineChange',
7465 'add': 'onOutlineChange',
7466 'remove': 'onOutlineChange'
7468 this.upButton
.connect( this, { 'click': [ 'emit', 'move', -1 ] } );
7469 this.downButton
.connect( this, { 'click': [ 'emit', 'move', 1 ] } );
7470 this.removeButton
.connect( this, { 'click': [ 'emit', 'remove' ] } );
7473 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7474 this.$group
.addClass( 'oo-ui-outlineControlsWidget-adders' );
7476 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7477 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7478 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7483 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7484 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7485 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7491 * @param {number} places Number of places to move
7501 * Handle outline change events.
7503 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7504 var i
, len
, firstMovable
, lastMovable
,
7505 items
= this.outline
.getItems(),
7506 selectedItem
= this.outline
.getSelectedItem(),
7507 movable
= selectedItem
&& selectedItem
.isMovable(),
7508 removable
= selectedItem
&& selectedItem
.isRemovable();
7513 while ( ++i
< len
) {
7514 if ( items
[i
].isMovable() ) {
7515 firstMovable
= items
[i
];
7521 if ( items
[i
].isMovable() ) {
7522 lastMovable
= items
[i
];
7527 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7528 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7529 this.removeButton
.setDisabled( !removable
);
7532 * Creates an OO.ui.OutlineItemWidget object.
7534 * Use with OO.ui.OutlineWidget.
7537 * @extends OO.ui.OptionWidget
7540 * @param {Mixed} data Item data
7541 * @param {Object} [config] Configuration options
7542 * @cfg {number} [level] Indentation level
7543 * @cfg {boolean} [movable] Allow modification from outline controls
7545 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
7546 // Config intialization
7547 config
= config
|| {};
7549 // Parent constructor
7550 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
7554 this.movable
= !!config
.movable
;
7555 this.removable
= !!config
.removable
;
7558 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
7559 this.setLevel( config
.level
);
7564 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
7566 /* Static Properties */
7568 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
7570 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
7572 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
7574 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
7579 * Check if item is movable.
7581 * Movablilty is used by outline controls.
7583 * @return {boolean} Item is movable
7585 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
7586 return this.movable
;
7590 * Check if item is removable.
7592 * Removablilty is used by outline controls.
7594 * @return {boolean} Item is removable
7596 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
7597 return this.removable
;
7601 * Get indentation level.
7603 * @return {number} Indentation level
7605 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
7612 * Movablilty is used by outline controls.
7614 * @param {boolean} movable Item is movable
7617 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
7618 this.movable
= !!movable
;
7625 * Removablilty is used by outline controls.
7627 * @param {boolean} movable Item is removable
7630 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
7631 this.removable
= !!removable
;
7636 * Set indentation level.
7638 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
7641 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
7642 var levels
= this.constructor.static.levels
,
7643 levelClass
= this.constructor.static.levelClass
,
7646 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
7648 if ( this.level
=== i
) {
7649 this.$element
.addClass( levelClass
+ i
);
7651 this.$element
.removeClass( levelClass
+ i
);
7658 * Option widget that looks like a button.
7660 * Use together with OO.ui.ButtonSelectWidget.
7663 * @extends OO.ui.OptionWidget
7664 * @mixins OO.ui.ButtonedElement
7665 * @mixins OO.ui.FlaggableElement
7668 * @param {Mixed} data Option data
7669 * @param {Object} [config] Configuration options
7671 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
7672 // Parent constructor
7673 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
7675 // Mixin constructors
7676 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7677 OO
.ui
.FlaggableElement
.call( this, config
);
7680 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
7681 this.$button
.append( this.$element
.contents() );
7682 this.$element
.append( this.$button
);
7687 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
7688 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
7689 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
7691 /* Static Properties */
7693 // Allow button mouse down events to pass through so they can be handled by the parent select widget
7694 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
7701 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
7702 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
7704 if ( this.constructor.static.selectable
) {
7705 this.setActive( state
);
7711 * Select widget containing button options.
7713 * Use together with OO.ui.ButtonOptionWidget.
7716 * @extends OO.ui.SelectWidget
7719 * @param {Object} [config] Configuration options
7721 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
7722 // Parent constructor
7723 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
7726 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
7731 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
7733 * Container for content that is overlaid and positioned absolutely.
7736 * @extends OO.ui.Widget
7737 * @mixins OO.ui.LabeledElement
7740 * @param {Object} [config] Configuration options
7741 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
7742 * @cfg {string} [align='center'] Alignment of popup to origin
7743 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
7744 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
7745 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
7746 * @cfg {boolean} [head] Show label and close button at the top
7748 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
7749 // Config intialization
7750 config
= config
|| {};
7752 // Parent constructor
7753 OO
.ui
.PopupWidget
.super.call( this, config
);
7755 // Mixin constructors
7756 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
7757 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
7760 this.visible
= false;
7761 this.$popup
= this.$( '<div>' );
7762 this.$head
= this.$( '<div>' );
7763 this.$body
= this.$clippable
;
7764 this.$tail
= this.$( '<div>' );
7765 this.$container
= config
.$container
|| this.$( 'body' );
7766 this.autoClose
= !!config
.autoClose
;
7767 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
7768 this.transitionTimeout
= null;
7770 this.align
= config
.align
|| 'center';
7771 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
7772 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
7775 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
7778 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
7779 this.$body
.addClass( 'oo-ui-popupWidget-body' );
7780 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
7782 .addClass( 'oo-ui-popupWidget-head' )
7783 .append( this.$label
, this.closeButton
.$element
);
7784 if ( !config
.head
) {
7788 .addClass( 'oo-ui-popupWidget-popup' )
7789 .append( this.$head
, this.$body
);
7790 this.$element
.hide()
7791 .addClass( 'oo-ui-popupWidget' )
7792 .append( this.$popup
, this.$tail
);
7797 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
7798 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
7799 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
7814 * Handles mouse down events.
7816 * @param {jQuery.Event} e Mouse down event
7818 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
7821 !$.contains( this.$element
[0], e
.target
) &&
7822 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
7829 * Bind mouse down listener.
7831 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
7832 // Capture clicks outside popup
7833 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
7837 * Handles close button click events.
7839 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
7840 if ( this.visible
) {
7846 * Unbind mouse down listener.
7848 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
7849 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
7853 * Check if the popup is visible.
7855 * @return {boolean} Popup is visible
7857 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
7858 return this.visible
;
7862 * Set whether to show a tail.
7864 * @return {boolean} Make tail visible
7866 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
7868 if ( this.tail
!== value
) {
7871 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
7873 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
7879 * Check if showing a tail.
7881 * @return {boolean} tail is visible
7883 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
7893 OO
.ui
.PopupWidget
.prototype.show = function () {
7894 if ( !this.visible
) {
7895 this.setClipping( true );
7896 this.$element
.show();
7897 this.visible
= true;
7898 this.emit( 'show' );
7899 if ( this.autoClose
) {
7900 this.bindMouseDownListener();
7912 OO
.ui
.PopupWidget
.prototype.hide = function () {
7913 if ( this.visible
) {
7914 this.setClipping( false );
7915 this.$element
.hide();
7916 this.visible
= false;
7917 this.emit( 'hide' );
7918 if ( this.autoClose
) {
7919 this.unbindMouseDownListener();
7926 * Updates the position and size.
7928 * @param {number} width Width
7929 * @param {number} height Height
7930 * @param {boolean} [transition=false] Use a smooth transition
7933 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
7935 originOffset
= Math
.round( this.$element
.offset().left
),
7936 containerLeft
= Math
.round( this.$container
.offset().left
),
7937 containerWidth
= this.$container
.innerWidth(),
7938 containerRight
= containerLeft
+ containerWidth
,
7939 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
7940 popupLeft
= popupOffset
- padding
,
7941 popupRight
= popupOffset
+ padding
+ width
+ padding
,
7942 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
7943 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
7945 // Prevent transition from being interrupted
7946 clearTimeout( this.transitionTimeout
);
7948 // Enable transition
7949 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
7952 if ( overlapRight
< 0 ) {
7953 popupOffset
+= overlapRight
;
7954 } else if ( overlapLeft
< 0 ) {
7955 popupOffset
-= overlapLeft
;
7958 // Position body relative to anchor and resize
7960 'left': popupOffset
,
7962 'height': height
=== undefined ? 'auto' : height
7966 // Prevent transitioning after transition is complete
7967 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
7968 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7971 // Prevent transitioning immediately
7972 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7978 * Button that shows and hides a popup.
7981 * @extends OO.ui.ButtonWidget
7982 * @mixins OO.ui.PopuppableElement
7985 * @param {Object} [config] Configuration options
7987 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7988 // Parent constructor
7989 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7991 // Mixin constructors
7992 OO
.ui
.PopuppableElement
.call( this, config
);
7996 .addClass( 'oo-ui-popupButtonWidget' )
7997 .append( this.popup
.$element
);
8002 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8003 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
8008 * Handles mouse click events.
8010 * @param {jQuery.Event} e Mouse click event
8012 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8013 // Skip clicks within the popup
8014 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8018 if ( !this.isDisabled() ) {
8019 if ( this.popup
.isVisible() ) {
8024 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8031 * Combines query and results selection widgets.
8034 * @extends OO.ui.Widget
8037 * @param {Object} [config] Configuration options
8038 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
8039 * @cfg {string} [value] Initial query value
8041 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
8042 // Configuration intialization
8043 config
= config
|| {};
8045 // Parent constructor
8046 OO
.ui
.SearchWidget
.super.call( this, config
);
8049 this.query
= new OO
.ui
.TextInputWidget( {
8052 'placeholder': config
.placeholder
,
8053 'value': config
.value
8055 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
8056 this.$query
= this.$( '<div>' );
8057 this.$results
= this.$( '<div>' );
8060 this.query
.connect( this, {
8061 'change': 'onQueryChange',
8062 'enter': 'onQueryEnter'
8064 this.results
.connect( this, {
8065 'highlight': 'onResultsHighlight',
8066 'select': 'onResultsSelect'
8068 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
8072 .addClass( 'oo-ui-searchWidget-query' )
8073 .append( this.query
.$element
);
8075 .addClass( 'oo-ui-searchWidget-results' )
8076 .append( this.results
.$element
);
8078 .addClass( 'oo-ui-searchWidget' )
8079 .append( this.$results
, this.$query
);
8084 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
8090 * @param {Object|null} item Item data or null if no item is highlighted
8095 * @param {Object|null} item Item data or null if no item is selected
8101 * Handle query key down events.
8103 * @param {jQuery.Event} e Key down event
8105 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
8106 var highlightedItem
, nextItem
,
8107 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
8110 highlightedItem
= this.results
.getHighlightedItem();
8111 if ( !highlightedItem
) {
8112 highlightedItem
= this.results
.getSelectedItem();
8114 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
8115 this.results
.highlightItem( nextItem
);
8116 nextItem
.scrollElementIntoView();
8121 * Handle select widget select events.
8123 * Clears existing results. Subclasses should repopulate items according to new query.
8125 * @param {string} value New value
8127 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
8129 this.results
.clearItems();
8133 * Handle select widget enter key events.
8135 * Selects highlighted item.
8137 * @param {string} value New value
8139 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
8141 this.results
.selectItem( this.results
.getHighlightedItem() );
8145 * Handle select widget highlight events.
8147 * @param {OO.ui.OptionWidget} item Highlighted item
8150 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
8151 this.emit( 'highlight', item
? item
.getData() : null );
8155 * Handle select widget select events.
8157 * @param {OO.ui.OptionWidget} item Selected item
8160 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
8161 this.emit( 'select', item
? item
.getData() : null );
8165 * Get the query input.
8167 * @return {OO.ui.TextInputWidget} Query input
8169 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
8174 * Get the results list.
8176 * @return {OO.ui.SelectWidget} Select list
8178 OO
.ui
.SearchWidget
.prototype.getResults = function () {
8179 return this.results
;
8182 * Text input widget.
8185 * @extends OO.ui.InputWidget
8188 * @param {Object} [config] Configuration options
8189 * @cfg {string} [placeholder] Placeholder text
8190 * @cfg {string} [icon] Symbolic name of icon
8191 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8192 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8193 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8195 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8196 config
= $.extend( { 'maxRows': 10 }, config
);
8198 // Parent constructor
8199 OO
.ui
.TextInputWidget
.super.call( this, config
);
8203 this.multiline
= !!config
.multiline
;
8204 this.autosize
= !!config
.autosize
;
8205 this.maxRows
= config
.maxRows
;
8208 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8209 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8212 this.$element
.addClass( 'oo-ui-textInputWidget' );
8213 if ( config
.icon
) {
8214 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
8215 this.$element
.append(
8217 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
8218 .mousedown( OO
.ui
.bind( function () {
8219 this.$input
.focus();
8224 if ( config
.placeholder
) {
8225 this.$input
.attr( 'placeholder', config
.placeholder
);
8231 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8236 * User presses enter inside the text box.
8238 * Not called if input is multiline.
8246 * Handle key press events.
8248 * @param {jQuery.Event} e Key press event
8249 * @fires enter If enter key is pressed and input is not multiline
8251 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8252 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8253 this.emit( 'enter' );
8258 * Handle element attach events.
8260 * @param {jQuery.Event} e Element attach event
8262 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8269 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8273 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
8277 * Automatically adjust the size of the text input.
8279 * This only affects multi-line inputs that are auto-sized.
8283 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
8284 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
8286 if ( this.multiline
&& this.autosize
) {
8287 $clone
= this.$input
.clone()
8288 .val( this.$input
.val() )
8289 .css( { 'height': 0 } )
8290 .insertAfter( this.$input
);
8291 // Set inline height property to 0 to measure scroll height
8292 scrollHeight
= $clone
[0].scrollHeight
;
8293 // Remove inline height property to measure natural heights
8294 $clone
.css( 'height', '' );
8295 innerHeight
= $clone
.innerHeight();
8296 outerHeight
= $clone
.outerHeight();
8297 // Measure max rows height
8298 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
8299 maxInnerHeight
= $clone
.innerHeight();
8300 $clone
.removeAttr( 'rows' ).css( 'height', '' );
8302 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
8303 // Only apply inline height when expansion beyond natural height is needed
8306 // Use the difference between the inner and outer height as a buffer
8307 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
8314 * Get input element.
8316 * @param {Object} [config] Configuration options
8317 * @return {jQuery} Input element
8319 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
8320 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
8326 * Check if input supports multiple lines.
8330 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
8331 return !!this.multiline
;
8335 * Check if input automatically adjusts its size.
8339 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
8340 return !!this.autosize
;
8344 * Check if input is pending.
8348 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
8349 return !!this.pending
;
8353 * Increase the pending stack.
8357 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
8358 if ( this.pending
=== 0 ) {
8359 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
8360 this.$input
.addClass( 'oo-ui-texture-pending' );
8368 * Reduce the pending stack.
8374 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
8375 if ( this.pending
=== 1 ) {
8376 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
8377 this.$input
.removeClass( 'oo-ui-texture-pending' );
8379 this.pending
= Math
.max( 0, this.pending
- 1 );
8385 * Select the contents of the input.
8389 OO
.ui
.TextInputWidget
.prototype.select = function () {
8390 this.$input
.select();
8394 * Menu for a text input widget.
8397 * @extends OO.ui.MenuWidget
8400 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
8401 * @param {Object} [config] Configuration options
8402 * @cfg {jQuery} [$container=input.$element] Element to render menu under
8404 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
8405 // Parent constructor
8406 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
8410 this.$container
= config
.$container
|| this.input
.$element
;
8411 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
8414 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
8419 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
8424 * Handle window resize event.
8426 * @param {jQuery.Event} e Window resize event
8428 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
8437 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
8439 OO
.ui
.TextInputMenuWidget
.super.prototype.show
.call( this );
8442 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
8451 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
8453 OO
.ui
.TextInputMenuWidget
.super.prototype.hide
.call( this );
8455 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
8460 * Position the menu.
8464 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
8466 $container
= this.$container
,
8467 dimensions
= $container
.offset();
8469 // Position under input
8470 dimensions
.top
+= $container
.height();
8472 // Compensate for frame position if in a differnt frame
8473 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
8474 frameOffset
= OO
.ui
.Element
.getRelativePosition(
8475 this.input
.$.frame
.$element
, this.$element
.offsetParent()
8477 dimensions
.left
+= frameOffset
.left
;
8478 dimensions
.top
+= frameOffset
.top
;
8480 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
8481 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
8482 dimensions
.right
= this.$element
.parent().position().left
-
8483 $container
.width() - dimensions
.left
;
8484 // Erase the value for 'left':
8485 delete dimensions
.left
;
8489 this.$element
.css( dimensions
);
8490 this.setIdealSize( $container
.width() );
8494 * Width with on and off states.
8496 * Mixin for widgets with a boolean state.
8502 * @param {Object} [config] Configuration options
8503 * @cfg {boolean} [value=false] Initial value
8505 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8506 // Configuration initialization
8507 config
= config
|| {};
8513 this.$element
.addClass( 'oo-ui-toggleWidget' );
8514 this.setValue( !!config
.value
);
8521 * @param {boolean} value Changed value
8527 * Get the value of the toggle.
8531 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8536 * Set the value of the toggle.
8538 * @param {boolean} value New value
8542 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8544 if ( this.value
!== value
) {
8546 this.emit( 'change', value
);
8547 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8548 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8553 * Button that toggles on and off.
8556 * @extends OO.ui.ButtonWidget
8557 * @mixins OO.ui.ToggleWidget
8560 * @param {Object} [config] Configuration options
8561 * @cfg {boolean} [value=false] Initial value
8563 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8564 // Configuration initialization
8565 config
= config
|| {};
8567 // Parent constructor
8568 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8570 // Mixin constructors
8571 OO
.ui
.ToggleWidget
.call( this, config
);
8574 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8579 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8580 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8587 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8588 if ( !this.isDisabled() ) {
8589 this.setValue( !this.value
);
8593 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8599 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8601 if ( value
!== this.value
) {
8602 this.setActive( value
);
8605 // Parent method (from mixin)
8606 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8611 * Switch that slides on and off.
8614 * @extends OO.ui.Widget
8615 * @mixins OO.ui.ToggleWidget
8618 * @param {Object} [config] Configuration options
8619 * @cfg {boolean} [value=false] Initial value
8621 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
8622 // Parent constructor
8623 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
8625 // Mixin constructors
8626 OO
.ui
.ToggleWidget
.call( this, config
);
8629 this.dragging
= false;
8630 this.dragStart
= null;
8631 this.sliding
= false;
8632 this.$glow
= this.$( '<span>' );
8633 this.$grip
= this.$( '<span>' );
8636 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8639 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
8640 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
8642 .addClass( 'oo-ui-toggleSwitchWidget' )
8643 .append( this.$glow
, this.$grip
);
8648 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
8649 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
8654 * Handle mouse down events.
8656 * @param {jQuery.Event} e Mouse down event
8658 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
8659 if ( !this.isDisabled() && e
.which
=== 1 ) {
8660 this.setValue( !this.value
);