2 * OOjs UI v0.1.0-pre (c5ae888f67)
3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: Wed Jun 11 2014 18:10:09 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 the setup process has been executed.
1031 * @param {Object} data Window opening data
1037 * Fired after the ready process has been executed.
1040 * @param {Object} data Window opening data
1044 * Window is torn down
1046 * Fired after the teardown process has been executed.
1049 * @param {Object} data Window closing data
1052 /* Static Properties */
1055 * Symbolic name of icon.
1059 * @property {string}
1061 OO
.ui
.Window
.static.icon
= 'window';
1066 * Subclasses must implement this property before instantiating the window.
1067 * Alternatively, override #getTitle with an alternative implementation.
1072 * @property {string|Function} Title string or function that returns a string
1074 OO
.ui
.Window
.static.title
= null;
1079 * Check if window is visible.
1081 * @return {boolean} Window is visible
1083 OO
.ui
.Window
.prototype.isVisible = function () {
1084 return this.visible
;
1088 * Check if window is opening.
1090 * @return {boolean} Window is opening
1092 OO
.ui
.Window
.prototype.isOpening = function () {
1093 return !!this.opening
&& this.opening
.state() === 'pending';
1097 * Check if window is closing.
1099 * @return {boolean} Window is closing
1101 OO
.ui
.Window
.prototype.isClosing = function () {
1102 return !!this.closing
&& this.closing
.state() === 'pending';
1106 * Check if window is opened.
1108 * @return {boolean} Window is opened
1110 OO
.ui
.Window
.prototype.isOpened = function () {
1111 return !!this.opened
&& this.opened
.state() === 'pending';
1115 * Get the window frame.
1117 * @return {OO.ui.Frame} Frame of window
1119 OO
.ui
.Window
.prototype.getFrame = function () {
1124 * Get the title of the window.
1126 * @return {string} Title text
1128 OO
.ui
.Window
.prototype.getTitle = function () {
1133 * Get the window icon.
1135 * @return {string} Symbolic name of icon
1137 OO
.ui
.Window
.prototype.getIcon = function () {
1142 * Set the size of window frame.
1144 * @param {number} [width=auto] Custom width
1145 * @param {number} [height=auto] Custom height
1148 OO
.ui
.Window
.prototype.setSize = function ( width
, height
) {
1149 if ( !this.frame
.$content
) {
1153 this.frame
.$element
.css( {
1154 'width': width
=== undefined ? 'auto' : width
,
1155 'height': height
=== undefined ? 'auto' : height
1162 * Set the title of the window.
1164 * @param {string|Function} title Title text or a function that returns text
1167 OO
.ui
.Window
.prototype.setTitle = function ( title
) {
1168 this.title
= OO
.ui
.resolveMsg( title
);
1169 if ( this.$title
) {
1170 this.$title
.text( title
);
1176 * Set the icon of the window.
1178 * @param {string} icon Symbolic name of icon
1181 OO
.ui
.Window
.prototype.setIcon = function ( icon
) {
1183 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
1187 this.$icon
.addClass( 'oo-ui-icon-' + this.icon
);
1194 * Set the position of window to fit with contents.
1196 * @param {string} left Left offset
1197 * @param {string} top Top offset
1200 OO
.ui
.Window
.prototype.setPosition = function ( left
, top
) {
1201 this.$element
.css( { 'left': left
, 'top': top
} );
1206 * Set the height of window to fit with contents.
1208 * @param {number} [min=0] Min height
1209 * @param {number} [max] Max height (defaults to content's outer height)
1212 OO
.ui
.Window
.prototype.fitHeightToContents = function ( min
, max
) {
1213 var height
= this.frame
.$content
.outerHeight();
1215 this.frame
.$element
.css(
1216 'height', Math
.max( min
|| 0, max
=== undefined ? height
: Math
.min( max
, height
) )
1223 * Set the width of window to fit with contents.
1225 * @param {number} [min=0] Min height
1226 * @param {number} [max] Max height (defaults to content's outer width)
1229 OO
.ui
.Window
.prototype.fitWidthToContents = function ( min
, max
) {
1230 var width
= this.frame
.$content
.outerWidth();
1232 this.frame
.$element
.css(
1233 'width', Math
.max( min
|| 0, max
=== undefined ? width
: Math
.min( max
, width
) )
1240 * Initialize window contents.
1242 * The first time the window is opened, #initialize is called when it's safe to begin populating
1243 * its contents. See #setup for a way to make changes each time the window opens.
1245 * Once this method is called, this.$$ can be used to create elements within the frame.
1249 OO
.ui
.Window
.prototype.initialize = function () {
1251 this.$ = this.frame
.$;
1252 this.$title
= this.$( '<div class="oo-ui-window-title"></div>' )
1253 .text( this.title
);
1254 this.$icon
= this.$( '<div class="oo-ui-window-icon"></div>' )
1255 .addClass( 'oo-ui-icon-' + this.icon
);
1256 this.$head
= this.$( '<div class="oo-ui-window-head"></div>' );
1257 this.$body
= this.$( '<div class="oo-ui-window-body"></div>' );
1258 this.$foot
= this.$( '<div class="oo-ui-window-foot"></div>' );
1259 this.$overlay
= this.$( '<div class="oo-ui-window-overlay"></div>' );
1262 this.frame
.$content
.append(
1263 this.$head
.append( this.$icon
, this.$title
),
1273 * Get a process for setting up a window for use.
1275 * Each time the window is opened this process will set it up for use in a particular context, based
1276 * on the `data` argument.
1278 * When you override this method, you can add additional setup steps to the process the parent
1279 * method provides using the 'first' and 'next' methods.
1282 * @param {Object} [data] Window opening data
1283 * @return {OO.ui.Process} Setup process
1285 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1286 return new OO
.ui
.Process();
1290 * Get a process for readying a window for use.
1292 * Each time the window is open and setup, this process will ready it up for use in a particular
1293 * context, based on the `data` argument.
1295 * When you override this method, you can add additional setup steps to the process the parent
1296 * method provides using the 'first' and 'next' methods.
1299 * @param {Object} [data] Window opening data
1300 * @return {OO.ui.Process} Setup process
1302 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1303 return new OO
.ui
.Process();
1307 * Get a process for tearing down a window after use.
1309 * Each time the window is closed this process will tear it down and do something with the user's
1310 * interactions within the window, based on the `data` argument.
1312 * When you override this method, you can add additional teardown steps to the process the parent
1313 * method provides using the 'first' and 'next' methods.
1316 * @param {Object} [data] Window closing data
1317 * @return {OO.ui.Process} Teardown process
1319 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1320 return new OO
.ui
.Process();
1326 * Do not override this method. Use #getSetupProcess to do something each time the window closes.
1328 * @param {Object} [data] Window opening data
1333 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1334 * first argument will be a promise which will be resolved when the window begins closing
1336 OO
.ui
.Window
.prototype.open = function ( data
) {
1337 // Return existing promise if already opening or open
1338 if ( this.opening
) {
1339 return this.opening
.promise();
1343 this.opening
= $.Deferred();
1344 this.frame
.load().done( OO
.ui
.bind( function () {
1345 this.$element
.show();
1346 this.visible
= true;
1347 this.getSetupProcess( data
).execute().done( OO
.ui
.bind( function () {
1348 this.$element
.addClass( 'oo-ui-window-setup' );
1349 this.emit( 'setup', data
);
1350 setTimeout( OO
.ui
.bind( function () {
1351 this.frame
.$content
.focus();
1352 this.getReadyProcess( data
).execute().done( OO
.ui
.bind( function () {
1353 this.$element
.addClass( 'oo-ui-window-ready' );
1354 this.emit( 'ready', data
);
1355 this.opened
= $.Deferred();
1356 // Now that we are totally done opening, it's safe to allow closing
1357 this.closing
= null;
1358 this.opening
.resolve( this.opened
.promise() );
1364 return this.opening
.promise();
1370 * Do not override this method. Use #getTeardownProcess to do something each time the window closes.
1372 * @param {Object} [data] Window closing data
1375 * @return {jQuery.Promise} Promise resolved when window is closed
1377 OO
.ui
.Window
.prototype.close = function ( data
) {
1380 // Return existing promise if already closing or closed
1381 if ( this.closing
) {
1382 return this.closing
.promise();
1385 // Close after opening is done if opening is in progress
1386 if ( this.opening
&& this.opening
.state() === 'pending' ) {
1387 close
= OO
.ui
.bind( function () {
1388 return this.close( data
);
1390 return this.opening
.then( close
, close
);
1394 // This.closing needs to exist before we emit the closing event so that handlers can call
1395 // window.close() and trigger the safety check above
1396 this.closing
= $.Deferred();
1397 this.frame
.$content
.find( ':focus' ).blur();
1398 this.$element
.removeClass( 'oo-ui-window-ready' );
1399 this.getTeardownProcess( data
).execute().done( OO
.ui
.bind( function () {
1400 this.$element
.removeClass( 'oo-ui-window-setup' );
1401 this.emit( 'teardown', data
);
1402 // To do something different with #opened, resolve/reject #opened in the teardown process
1403 if ( this.opened
&& this.opened
.state() === 'pending' ) {
1404 this.opened
.resolve();
1406 this.$element
.hide();
1407 this.visible
= false;
1408 this.closing
.resolve();
1409 // Now that we are totally done closing, it's safe to allow opening
1410 this.opening
= null;
1413 return this.closing
.promise();
1416 * Set of mutually exclusive windows.
1419 * @extends OO.ui.Element
1420 * @mixins OO.EventEmitter
1423 * @param {OO.Factory} factory Window factory
1424 * @param {Object} [config] Configuration options
1426 OO
.ui
.WindowSet
= function OoUiWindowSet( factory
, config
) {
1427 // Parent constructor
1428 OO
.ui
.WindowSet
.super.call( this, config
);
1430 // Mixin constructors
1431 OO
.EventEmitter
.call( this );
1434 this.factory
= factory
;
1437 * List of all windows associated with this window set.
1439 * @property {OO.ui.Window[]}
1441 this.windowList
= [];
1444 * Mapping of OO.ui.Window objects created by name from the #factory.
1446 * @property {Object}
1449 this.currentWindow
= null;
1452 this.$element
.addClass( 'oo-ui-windowSet' );
1457 OO
.inheritClass( OO
.ui
.WindowSet
, OO
.ui
.Element
);
1458 OO
.mixinClass( OO
.ui
.WindowSet
, OO
.EventEmitter
);
1464 * @param {OO.ui.Window} win Window that's been setup
1465 * @param {Object} config Window opening information
1470 * @param {OO.ui.Window} win Window that's ready
1471 * @param {Object} config Window opening information
1476 * @param {OO.ui.Window} win Window that's been torn down
1477 * @param {Object} config Window closing information
1483 * Handle a window setup event.
1485 * @param {OO.ui.Window} win Window that's been setup
1486 * @param {Object} [config] Window opening information
1489 OO
.ui
.WindowSet
.prototype.onWindowSetup = function ( win
, config
) {
1490 if ( this.currentWindow
&& this.currentWindow
!== win
) {
1491 this.currentWindow
.close();
1493 this.currentWindow
= win
;
1494 this.emit( 'setup', win
, config
);
1498 * Handle a window ready event.
1500 * @param {OO.ui.Window} win Window that's ready
1501 * @param {Object} [config] Window opening information
1504 OO
.ui
.WindowSet
.prototype.onWindowReady = function ( win
, config
) {
1505 this.emit( 'ready', win
, config
);
1509 * Handle a window teardown event.
1511 * @param {OO.ui.Window} win Window that's been torn down
1512 * @param {Object} [config] Window closing information
1515 OO
.ui
.WindowSet
.prototype.onWindowTeardown = function ( win
, config
) {
1516 this.currentWindow
= null;
1517 this.emit( 'teardown', win
, config
);
1521 * Get the current window.
1523 * @return {OO.ui.Window|null} Current window or null if none open
1525 OO
.ui
.WindowSet
.prototype.getCurrentWindow = function () {
1526 return this.currentWindow
;
1530 * Return a given window.
1532 * @param {string} name Symbolic name of window
1533 * @return {OO.ui.Window} Window with specified name
1535 OO
.ui
.WindowSet
.prototype.getWindow = function ( name
) {
1538 if ( !this.factory
.lookup( name
) ) {
1539 throw new Error( 'Unknown window: ' + name
);
1541 if ( !( name
in this.windows
) ) {
1542 win
= this.windows
[name
] = this.createWindow( name
);
1543 this.addWindow( win
);
1545 return this.windows
[name
];
1549 * Create a window for use in this window set.
1551 * @param {string} name Symbolic name of window
1552 * @return {OO.ui.Window} Window with specified name
1554 OO
.ui
.WindowSet
.prototype.createWindow = function ( name
) {
1555 return this.factory
.create( name
, { '$': this.$ } );
1559 * Add a given window to this window set.
1561 * Connects event handlers and attaches it to the DOM. Calling
1562 * OO.ui.Window#open will not work until the window is added to the set.
1564 * @param {OO.ui.Window} win Window to add
1566 OO
.ui
.WindowSet
.prototype.addWindow = function ( win
) {
1567 if ( this.windowList
.indexOf( win
) !== -1 ) {
1571 this.windowList
.push( win
);
1573 win
.connect( this, {
1574 'setup': [ 'onWindowSetup', win
],
1575 'ready': [ 'onWindowReady', win
],
1576 'teardown': [ 'onWindowTeardown', win
]
1578 this.$element
.append( win
.$element
);
1581 * Modal dialog window.
1585 * @extends OO.ui.Window
1588 * @param {Object} [config] Configuration options
1589 * @cfg {boolean} [footless] Hide foot
1590 * @cfg {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1592 OO
.ui
.Dialog
= function OoUiDialog( config
) {
1593 // Configuration initialization
1594 config
= $.extend( { 'size': 'large' }, config
);
1596 // Parent constructor
1597 OO
.ui
.Dialog
.super.call( this, config
);
1600 this.visible
= false;
1601 this.footless
= !!config
.footless
;
1604 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
1605 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
1608 this.$element
.on( 'mousedown', false );
1611 this.$element
.addClass( 'oo-ui-dialog' );
1612 this.setSize( config
.size
);
1617 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
1619 /* Static Properties */
1622 * Symbolic name of dialog.
1627 * @property {string}
1629 OO
.ui
.Dialog
.static.name
= '';
1632 * Map of symbolic size names and CSS classes.
1636 * @property {Object}
1638 OO
.ui
.Dialog
.static.sizeCssClasses
= {
1639 'small': 'oo-ui-dialog-small',
1640 'medium': 'oo-ui-dialog-medium',
1641 'large': 'oo-ui-dialog-large'
1647 * Handle close button click events.
1649 OO
.ui
.Dialog
.prototype.onCloseButtonClick = function () {
1650 this.close( { 'action': 'cancel' } );
1654 * Handle window mouse wheel events.
1656 * @param {jQuery.Event} e Mouse wheel event
1658 OO
.ui
.Dialog
.prototype.onWindowMouseWheel = function () {
1663 * Handle document key down events.
1665 * @param {jQuery.Event} e Key down event
1667 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
1668 switch ( e
.which
) {
1669 case OO
.ui
.Keys
.PAGEUP
:
1670 case OO
.ui
.Keys
.PAGEDOWN
:
1671 case OO
.ui
.Keys
.END
:
1672 case OO
.ui
.Keys
.HOME
:
1673 case OO
.ui
.Keys
.LEFT
:
1675 case OO
.ui
.Keys
.RIGHT
:
1676 case OO
.ui
.Keys
.DOWN
:
1677 // Prevent any key events that might cause scrolling
1683 * Handle frame document key down events.
1685 * @param {jQuery.Event} e Key down event
1687 OO
.ui
.Dialog
.prototype.onFrameDocumentKeyDown = function ( e
) {
1688 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
1689 this.close( { 'action': 'cancel' } );
1697 * @param {string} [size='large'] Symbolic name of dialog size, `small`, `medium` or `large`
1699 OO
.ui
.Dialog
.prototype.setSize = function ( size
) {
1700 var name
, state
, cssClass
,
1701 sizeCssClasses
= OO
.ui
.Dialog
.static.sizeCssClasses
;
1703 if ( !sizeCssClasses
[size
] ) {
1707 for ( name
in sizeCssClasses
) {
1708 state
= name
=== size
;
1709 cssClass
= sizeCssClasses
[name
];
1710 this.$element
.toggleClass( cssClass
, state
);
1717 OO
.ui
.Dialog
.prototype.initialize = function () {
1719 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
1722 this.closeButton
= new OO
.ui
.ButtonWidget( {
1726 'title': OO
.ui
.msg( 'ooui-dialog-action-close' )
1730 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
1731 this.frame
.$document
.on( 'keydown', OO
.ui
.bind( this.onFrameDocumentKeyDown
, this ) );
1734 this.frame
.$content
.addClass( 'oo-ui-dialog-content' );
1735 if ( this.footless
) {
1736 this.frame
.$content
.addClass( 'oo-ui-dialog-content-footless' );
1738 this.closeButton
.$element
.addClass( 'oo-ui-window-closeButton' );
1739 this.$head
.append( this.closeButton
.$element
);
1745 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
1746 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
1747 .next( function () {
1748 // Prevent scrolling in top-level window
1749 this.$( window
).on( 'mousewheel', this.onWindowMouseWheelHandler
);
1750 this.$( document
).on( 'keydown', this.onDocumentKeyDownHandler
);
1757 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
1758 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
1759 .first( function () {
1760 // Wait for closing transition
1761 return OO
.ui
.Process
.static.delay( 250 );
1763 .next( function () {
1764 // Allow scrolling in top-level window
1765 this.$( window
).off( 'mousewheel', this.onWindowMouseWheelHandler
);
1766 this.$( document
).off( 'keydown', this.onDocumentKeyDownHandler
);
1771 * Check if input is pending.
1775 OO
.ui
.Dialog
.prototype.isPending = function () {
1776 return !!this.pending
;
1780 * Increase the pending stack.
1784 OO
.ui
.Dialog
.prototype.pushPending = function () {
1785 if ( this.pending
=== 0 ) {
1786 this.frame
.$content
.addClass( 'oo-ui-dialog-pending' );
1787 this.$head
.addClass( 'oo-ui-texture-pending' );
1788 this.$foot
.addClass( 'oo-ui-texture-pending' );
1796 * Reduce the pending stack.
1802 OO
.ui
.Dialog
.prototype.popPending = function () {
1803 if ( this.pending
=== 1 ) {
1804 this.frame
.$content
.removeClass( 'oo-ui-dialog-pending' );
1805 this.$head
.removeClass( 'oo-ui-texture-pending' );
1806 this.$foot
.removeClass( 'oo-ui-texture-pending' );
1808 this.pending
= Math
.max( 0, this.pending
- 1 );
1813 * Container for elements.
1817 * @extends OO.ui.Element
1818 * @mixins OO.EventEmitter
1821 * @param {Object} [config] Configuration options
1823 OO
.ui
.Layout
= function OoUiLayout( config
) {
1824 // Initialize config
1825 config
= config
|| {};
1827 // Parent constructor
1828 OO
.ui
.Layout
.super.call( this, config
);
1830 // Mixin constructors
1831 OO
.EventEmitter
.call( this );
1834 this.$element
.addClass( 'oo-ui-layout' );
1839 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1840 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1842 * User interface control.
1846 * @extends OO.ui.Element
1847 * @mixins OO.EventEmitter
1850 * @param {Object} [config] Configuration options
1851 * @cfg {boolean} [disabled=false] Disable
1853 OO
.ui
.Widget
= function OoUiWidget( config
) {
1854 // Initialize config
1855 config
= $.extend( { 'disabled': false }, config
);
1857 // Parent constructor
1858 OO
.ui
.Widget
.super.call( this, config
);
1860 // Mixin constructors
1861 OO
.EventEmitter
.call( this );
1864 this.disabled
= null;
1865 this.wasDisabled
= null;
1868 this.$element
.addClass( 'oo-ui-widget' );
1869 this.setDisabled( !!config
.disabled
);
1874 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1875 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1881 * @param {boolean} disabled Widget is disabled
1887 * Check if the widget is disabled.
1889 * @param {boolean} Button is disabled
1891 OO
.ui
.Widget
.prototype.isDisabled = function () {
1892 return this.disabled
;
1896 * Update the disabled state, in case of changes in parent widget.
1900 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1901 this.setDisabled( this.disabled
);
1906 * Set the disabled state of the widget.
1908 * This should probably change the widgets' appearance and prevent it from being used.
1910 * @param {boolean} disabled Disable widget
1913 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1916 this.disabled
= !!disabled
;
1917 isDisabled
= this.isDisabled();
1918 if ( isDisabled
!== this.wasDisabled
) {
1919 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1920 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1921 this.emit( 'disable', isDisabled
);
1923 this.wasDisabled
= isDisabled
;
1927 * A list of functions, called in sequence.
1929 * If a function added to a process returns boolean false the process will stop; if it returns an
1930 * object with a `promise` method the process will use the promise to either continue to the next
1931 * step when the promise is resolved or stop when the promise is rejected.
1937 OO
.ui
.Process = function () {
1944 OO
.initClass( OO
.ui
.Process
);
1946 /* Static Methods */
1949 * Generate a promise which is resolved after a set amount of time.
1951 * @param {number} length Number of milliseconds before resolving the promise
1952 * @return {jQuery.Promise} Promise that will be resolved after a set amount of time
1954 OO
.ui
.Process
.static.delay = function ( length
) {
1955 var deferred
= $.Deferred();
1957 setTimeout( function () {
1961 return deferred
.promise();
1967 * Start the process.
1969 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
1970 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
1971 * process, the remaining steps will not be taken
1973 OO
.ui
.Process
.prototype.execute = function () {
1974 var i
, len
, promise
;
1977 * Continue execution.
1980 * @param {Array} step A function and the context it should be called in
1981 * @return {Function} Function that continues the process
1983 function proceed( step
) {
1984 return function () {
1985 // Execute step in the correct context
1986 var result
= step
[0].call( step
[1] );
1988 if ( result
=== false ) {
1989 // Use rejected promise for boolean false results
1990 return $.Deferred().reject().promise();
1992 // Duck-type the object to see if it can produce a promise
1993 if ( result
&& $.isFunction( result
.promise
) ) {
1994 // Use a promise generated from the result
1995 return result
.promise();
1997 // Use resolved promise for other results
1998 return $.Deferred().resolve().promise();
2002 if ( this.steps
.length
) {
2003 // Generate a chain reaction of promises
2004 promise
= proceed( this.steps
[0] )();
2005 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
2006 promise
= promise
.then( proceed( this.steps
[i
] ) );
2009 promise
= $.Deferred().resolve().promise();
2016 * Add step to the beginning of the process.
2018 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
2019 * it returns an object with a `promise` method the process will use the promise to either
2020 * continue to the next step when the promise is resolved or stop when the promise is rejected
2021 * @param {Object} [context=null] Context to call the step function in
2024 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
2025 this.steps
.unshift( [ step
, context
|| null ] );
2030 * Add step to the end of the process.
2032 * @param {Function} step Function to execute; if it returns boolean false the process will stop; if
2033 * it returns an object with a `promise` method the process will use the promise to either
2034 * continue to the next step when the promise is resolved or stop when the promise is rejected
2035 * @param {Object} [context=null] Context to call the step function in
2038 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
2039 this.steps
.push( [ step
, context
|| null ] );
2043 * Dialog for showing a confirmation/warning message.
2046 * @extends OO.ui.Dialog
2049 * @param {Object} [config] Configuration options
2051 OO
.ui
.ConfirmationDialog
= function OoUiConfirmationDialog( config
) {
2052 // Configuration initialization
2053 config
= $.extend( { 'size': 'small' }, config
);
2055 // Parent constructor
2056 OO
.ui
.Dialog
.call( this, config
);
2061 OO
.inheritClass( OO
.ui
.ConfirmationDialog
, OO
.ui
.Dialog
);
2063 /* Static Properties */
2065 OO
.ui
.ConfirmationDialog
.static.name
= 'confirm';
2067 OO
.ui
.ConfirmationDialog
.static.icon
= 'help';
2069 OO
.ui
.ConfirmationDialog
.static.title
= OO
.ui
.deferMsg( 'ooui-dialog-confirm-title' );
2076 OO
.ui
.ConfirmationDialog
.prototype.initialize = function () {
2078 OO
.ui
.Dialog
.prototype.initialize
.call( this );
2080 // Set up the layout
2081 var contentLayout
= new OO
.ui
.PanelLayout( {
2086 this.$promptContainer
= this.$( '<div>' ).addClass( 'oo-ui-dialog-confirm-promptContainer' );
2088 this.cancelButton
= new OO
.ui
.ButtonWidget();
2089 this.cancelButton
.connect( this, { 'click': [ 'close', 'cancel' ] } );
2091 this.okButton
= new OO
.ui
.ButtonWidget();
2092 this.okButton
.connect( this, { 'click': [ 'close', 'ok' ] } );
2095 contentLayout
.$element
.append( this.$promptContainer
);
2096 this.$body
.append( contentLayout
.$element
);
2099 this.okButton
.$element
,
2100 this.cancelButton
.$element
2103 this.connect( this, { 'teardown': [ 'close', 'cancel' ] } );
2107 * Setup a confirmation dialog.
2109 * @param {Object} [data] Window opening data including text of the dialog and text for the buttons
2110 * @param {jQuery|string} [data.prompt] Text to display or list of nodes to use as content of the dialog.
2111 * @param {jQuery|string|Function|null} [data.okLabel] Label of the OK button
2112 * @param {jQuery|string|Function|null} [data.cancelLabel] Label of the cancel button
2113 * @param {string|string[]} [data.okFlags="constructive"] Flags for the OK button
2114 * @param {string|string[]} [data.cancelFlags="destructive"] Flags for the cancel button
2115 * @return {OO.ui.Process} Setup process
2117 OO
.ui
.ConfirmationDialog
.prototype.getSetupProcess = function ( data
) {
2119 return OO
.ui
.ConfirmationDialog
.super.prototype.getSetupProcess
.call( this, data
)
2120 .next( function () {
2121 var prompt
= data
.prompt
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-prompt' ),
2122 okLabel
= data
.okLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-ok' ),
2123 cancelLabel
= data
.cancelLabel
|| OO
.ui
.deferMsg( 'ooui-dialog-confirm-default-cancel' ),
2124 okFlags
= data
.okFlags
|| 'constructive',
2125 cancelFlags
= data
.cancelFlags
|| 'destructive';
2127 if ( typeof prompt
=== 'string' ) {
2128 this.$promptContainer
.text( prompt
);
2130 this.$promptContainer
.empty().append( prompt
);
2133 this.okButton
.setLabel( okLabel
).clearFlags().setFlags( okFlags
);
2134 this.cancelButton
.setLabel( cancelLabel
).clearFlags().setFlags( cancelFlags
);
2141 OO
.ui
.ConfirmationDialog
.prototype.getTeardownProcess = function ( data
) {
2143 return OO
.ui
.ConfirmationDialog
.super.prototype.getTeardownProcess
.call( this, data
)
2144 .first( function () {
2145 if ( data
=== 'ok' ) {
2146 this.opened
.resolve();
2147 } else if ( data
=== 'cancel' ) {
2148 this.opened
.reject();
2153 * Element with a button.
2159 * @param {jQuery} $button Button node, assigned to #$button
2160 * @param {Object} [config] Configuration options
2161 * @cfg {boolean} [frameless] Render button without a frame
2162 * @cfg {number} [tabIndex=0] Button's tab index, use -1 to prevent tab focusing
2164 OO
.ui
.ButtonedElement
= function OoUiButtonedElement( $button
, config
) {
2165 // Configuration initialization
2166 config
= config
|| {};
2169 this.$button
= $button
;
2170 this.tabIndex
= null;
2171 this.active
= false;
2172 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
2175 this.$button
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
2178 this.$element
.addClass( 'oo-ui-buttonedElement' );
2180 .addClass( 'oo-ui-buttonedElement-button' )
2181 .attr( 'role', 'button' )
2182 .prop( 'tabIndex', config
.tabIndex
|| 0 );
2183 if ( config
.frameless
) {
2184 this.$element
.addClass( 'oo-ui-buttonedElement-frameless' );
2186 this.$element
.addClass( 'oo-ui-buttonedElement-framed' );
2192 OO
.initClass( OO
.ui
.ButtonedElement
);
2194 /* Static Properties */
2197 * Cancel mouse down events.
2201 * @property {boolean}
2203 OO
.ui
.ButtonedElement
.static.cancelButtonMouseDownEvents
= true;
2208 * Handles mouse down events.
2210 * @param {jQuery.Event} e Mouse down event
2212 OO
.ui
.ButtonedElement
.prototype.onMouseDown = function ( e
) {
2213 if ( this.isDisabled() || e
.which
!== 1 ) {
2216 // tabIndex should generally be interacted with via the property, but it's not possible to
2217 // reliably unset a tabIndex via a property so we use the (lowercase) "tabindex" attribute
2218 this.tabIndex
= this.$button
.attr( 'tabindex' );
2220 // Remove the tab-index while the button is down to prevent the button from stealing focus
2221 .removeAttr( 'tabindex' )
2222 .addClass( 'oo-ui-buttonedElement-pressed' );
2223 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
2224 // reliably reapply the tabindex and remove the pressed class
2225 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
2226 // Prevent change of focus unless specifically configured otherwise
2227 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
2233 * Handles mouse up events.
2235 * @param {jQuery.Event} e Mouse up event
2237 OO
.ui
.ButtonedElement
.prototype.onMouseUp = function ( e
) {
2238 if ( this.isDisabled() || e
.which
!== 1 ) {
2242 // Restore the tab-index after the button is up to restore the button's accesssibility
2243 .attr( 'tabindex', this.tabIndex
)
2244 .removeClass( 'oo-ui-buttonedElement-pressed' );
2245 // Stop listening for mouseup, since we only needed this once
2246 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
2252 * @param {boolean} [value] Make button active
2255 OO
.ui
.ButtonedElement
.prototype.setActive = function ( value
) {
2256 this.$button
.toggleClass( 'oo-ui-buttonedElement-active', !!value
);
2260 * Element that can be automatically clipped to visible boundaies.
2266 * @param {jQuery} $clippable Nodes to clip, assigned to #$clippable
2267 * @param {Object} [config] Configuration options
2269 OO
.ui
.ClippableElement
= function OoUiClippableElement( $clippable
, config
) {
2270 // Configuration initialization
2271 config
= config
|| {};
2274 this.$clippable
= $clippable
;
2275 this.clipping
= false;
2276 this.clipped
= false;
2277 this.$clippableContainer
= null;
2278 this.$clippableScroller
= null;
2279 this.$clippableWindow
= null;
2280 this.idealWidth
= null;
2281 this.idealHeight
= null;
2282 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
2283 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
2286 this.$clippable
.addClass( 'oo-ui-clippableElement-clippable' );
2294 * @param {boolean} value Enable clipping
2297 OO
.ui
.ClippableElement
.prototype.setClipping = function ( value
) {
2300 if ( this.clipping
!== value
) {
2301 this.clipping
= value
;
2302 if ( this.clipping
) {
2303 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
2304 // If the clippable container is the body, we have to listen to scroll events and check
2305 // jQuery.scrollTop on the window because of browser inconsistencies
2306 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
2307 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
2308 this.$clippableContainer
;
2309 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
2310 this.$clippableWindow
= this.$( this.getElementWindow() )
2311 .on( 'resize', this.onClippableWindowResizeHandler
);
2312 // Initial clip after visible
2313 setTimeout( OO
.ui
.bind( this.clip
, this ) );
2315 this.$clippableContainer
= null;
2316 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
2317 this.$clippableScroller
= null;
2318 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
2319 this.$clippableWindow
= null;
2327 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
2329 * @return {boolean} Element will be clipped to the visible area
2331 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
2332 return this.clipping
;
2336 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
2338 * @return {boolean} Part of the element is being clipped
2340 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
2341 return this.clipped
;
2345 * Set the ideal size.
2347 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
2348 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
2350 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
2351 this.idealWidth
= width
;
2352 this.idealHeight
= height
;
2356 * Clip element to visible boundaries and allow scrolling when needed.
2358 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
2359 * overlapped by, the visible area of the nearest scrollable container.
2363 OO
.ui
.ClippableElement
.prototype.clip = function () {
2364 if ( !this.clipping
) {
2365 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
2370 cOffset
= this.$clippable
.offset(),
2371 ccOffset
= this.$clippableContainer
.offset() || { 'top': 0, 'left': 0 },
2372 ccHeight
= this.$clippableContainer
.innerHeight() - buffer
,
2373 ccWidth
= this.$clippableContainer
.innerWidth() - buffer
,
2374 scrollTop
= this.$clippableScroller
.scrollTop(),
2375 scrollLeft
= this.$clippableScroller
.scrollLeft(),
2376 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
2377 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
2378 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
2379 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
2380 clipWidth
= desiredWidth
< naturalWidth
,
2381 clipHeight
= desiredHeight
< naturalHeight
;
2384 this.$clippable
.css( { 'overflow-x': 'auto', 'width': desiredWidth
} );
2386 this.$clippable
.css( { 'overflow-x': '', 'width': this.idealWidth
|| '' } );
2389 this.$clippable
.css( { 'overflow-y': 'auto', 'height': desiredHeight
} );
2391 this.$clippable
.css( { 'overflow-y': '', 'height': this.idealHeight
|| '' } );
2394 this.clipped
= clipWidth
|| clipHeight
;
2399 * Element with named flags that can be added, removed, listed and checked.
2401 * A flag, when set, adds a CSS class on the `$element` by combing `oo-ui-flaggableElement-` with
2402 * the flag name. Flags are primarily useful for styling.
2408 * @param {Object} [config] Configuration options
2409 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
2411 OO
.ui
.FlaggableElement
= function OoUiFlaggableElement( config
) {
2412 // Config initialization
2413 config
= config
|| {};
2419 this.setFlags( config
.flags
);
2425 * Check if a flag is set.
2427 * @param {string} flag Name of flag
2428 * @return {boolean} Has flag
2430 OO
.ui
.FlaggableElement
.prototype.hasFlag = function ( flag
) {
2431 return flag
in this.flags
;
2435 * Get the names of all flags set.
2437 * @return {string[]} flags Flag names
2439 OO
.ui
.FlaggableElement
.prototype.getFlags = function () {
2440 return Object
.keys( this.flags
);
2448 OO
.ui
.FlaggableElement
.prototype.clearFlags = function () {
2450 classPrefix
= 'oo-ui-flaggableElement-';
2452 for ( flag
in this.flags
) {
2453 delete this.flags
[flag
];
2454 this.$element
.removeClass( classPrefix
+ flag
);
2461 * Add one or more flags.
2463 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
2464 * keyed by flag name containing boolean set/remove instructions.
2467 OO
.ui
.FlaggableElement
.prototype.setFlags = function ( flags
) {
2469 classPrefix
= 'oo-ui-flaggableElement-';
2471 if ( typeof flags
=== 'string' ) {
2473 this.flags
[flags
] = true;
2474 this.$element
.addClass( classPrefix
+ flags
);
2475 } else if ( $.isArray( flags
) ) {
2476 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
2479 this.flags
[flag
] = true;
2480 this.$element
.addClass( classPrefix
+ flag
);
2482 } else if ( OO
.isPlainObject( flags
) ) {
2483 for ( flag
in flags
) {
2484 if ( flags
[flag
] ) {
2486 this.flags
[flag
] = true;
2487 this.$element
.addClass( classPrefix
+ flag
);
2490 delete this.flags
[flag
];
2491 this.$element
.removeClass( classPrefix
+ flag
);
2498 * Element containing a sequence of child elements.
2504 * @param {jQuery} $group Container node, assigned to #$group
2505 * @param {Object} [config] Configuration options
2507 OO
.ui
.GroupElement
= function OoUiGroupElement( $group
, config
) {
2509 config
= config
|| {};
2512 this.$group
= $group
;
2514 this.aggregateItemEvents
= {};
2522 * @return {OO.ui.Element[]} Items
2524 OO
.ui
.GroupElement
.prototype.getItems = function () {
2525 return this.items
.slice( 0 );
2529 * Add an aggregate item event.
2531 * Aggregated events are listened to on each item and then emitted by the group under a new name,
2532 * and with an additional leading parameter containing the item that emitted the original event.
2533 * Other arguments that were emitted from the original event are passed through.
2535 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
2536 * event, use null value to remove aggregation
2537 * @throws {Error} If aggregation already exists
2539 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
2540 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
2542 for ( itemEvent
in events
) {
2543 groupEvent
= events
[itemEvent
];
2545 // Remove existing aggregated event
2546 if ( itemEvent
in this.aggregateItemEvents
) {
2547 // Don't allow duplicate aggregations
2549 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
2551 // Remove event aggregation from existing items
2552 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2553 item
= this.items
[i
];
2554 if ( item
.connect
&& item
.disconnect
) {
2556 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
2557 item
.disconnect( this, remove
);
2560 // Prevent future items from aggregating event
2561 delete this.aggregateItemEvents
[itemEvent
];
2564 // Add new aggregate event
2566 // Make future items aggregate event
2567 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
2568 // Add event aggregation to existing items
2569 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2570 item
= this.items
[i
];
2571 if ( item
.connect
&& item
.disconnect
) {
2573 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
2574 item
.connect( this, add
);
2584 * @param {OO.ui.Element[]} items Item
2585 * @param {number} [index] Index to insert items at
2588 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
2589 var i
, len
, item
, event
, events
, currentIndex
,
2592 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2595 // Check if item exists then remove it first, effectively "moving" it
2596 currentIndex
= $.inArray( item
, this.items
);
2597 if ( currentIndex
>= 0 ) {
2598 this.removeItems( [ item
] );
2599 // Adjust index to compensate for removal
2600 if ( currentIndex
< index
) {
2605 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
2607 for ( event
in this.aggregateItemEvents
) {
2608 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
2610 item
.connect( this, events
);
2612 item
.setElementGroup( this );
2613 itemElements
.push( item
.$element
.get( 0 ) );
2616 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2617 this.$group
.append( itemElements
);
2618 this.items
.push
.apply( this.items
, items
);
2619 } else if ( index
=== 0 ) {
2620 this.$group
.prepend( itemElements
);
2621 this.items
.unshift
.apply( this.items
, items
);
2623 this.items
[index
].$element
.before( itemElements
);
2624 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
2633 * Items will be detached, not removed, so they can be used later.
2635 * @param {OO.ui.Element[]} items Items to remove
2638 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
2639 var i
, len
, item
, index
, remove
, itemEvent
;
2641 // Remove specific items
2642 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2644 index
= $.inArray( item
, this.items
);
2645 if ( index
!== -1 ) {
2647 item
.connect
&& item
.disconnect
&&
2648 !$.isEmptyObject( this.aggregateItemEvents
)
2651 if ( itemEvent
in this.aggregateItemEvents
) {
2652 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2654 item
.disconnect( this, remove
);
2656 item
.setElementGroup( null );
2657 this.items
.splice( index
, 1 );
2658 item
.$element
.detach();
2668 * Items will be detached, not removed, so they can be used later.
2672 OO
.ui
.GroupElement
.prototype.clearItems = function () {
2673 var i
, len
, item
, remove
, itemEvent
;
2676 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2677 item
= this.items
[i
];
2679 item
.connect
&& item
.disconnect
&&
2680 !$.isEmptyObject( this.aggregateItemEvents
)
2683 if ( itemEvent
in this.aggregateItemEvents
) {
2684 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
2686 item
.disconnect( this, remove
);
2688 item
.setElementGroup( null );
2689 item
.$element
.detach();
2696 * Element containing an icon.
2702 * @param {jQuery} $icon Icon node, assigned to #$icon
2703 * @param {Object} [config] Configuration options
2704 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
2705 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2708 OO
.ui
.IconedElement
= function OoUiIconedElement( $icon
, config
) {
2709 // Config intialization
2710 config
= config
|| {};
2717 this.$icon
.addClass( 'oo-ui-iconedElement-icon' );
2718 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2723 OO
.initClass( OO
.ui
.IconedElement
);
2725 /* Static Properties */
2730 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
2732 * For i18n purposes, this property can be an object containing a `default` icon name property and
2733 * additional icon names keyed by language code.
2735 * Example of i18n icon definition:
2736 * { 'default': 'bold-a', 'en': 'bold-b', 'de': 'bold-f' }
2740 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
2741 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2744 OO
.ui
.IconedElement
.static.icon
= null;
2751 * @param {Object|string} icon Symbolic icon name, or map of icon names keyed by language ID;
2752 * use the 'default' key to specify the icon to be used when there is no icon in the user's
2756 OO
.ui
.IconedElement
.prototype.setIcon = function ( icon
) {
2757 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2760 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2762 if ( typeof icon
=== 'string' ) {
2764 if ( icon
.length
) {
2765 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2769 this.$element
.toggleClass( 'oo-ui-iconedElement', !!this.icon
);
2777 * @return {string} Icon
2779 OO
.ui
.IconedElement
.prototype.getIcon = function () {
2783 * Element containing an indicator.
2789 * @param {jQuery} $indicator Indicator node, assigned to #$indicator
2790 * @param {Object} [config] Configuration options
2791 * @cfg {string} [indicator] Symbolic indicator name
2792 * @cfg {string} [indicatorTitle] Indicator title text or a function that return text
2794 OO
.ui
.IndicatedElement
= function OoUiIndicatedElement( $indicator
, config
) {
2795 // Config intialization
2796 config
= config
|| {};
2799 this.$indicator
= $indicator
;
2800 this.indicator
= null;
2801 this.indicatorLabel
= null;
2804 this.$indicator
.addClass( 'oo-ui-indicatedElement-indicator' );
2805 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2806 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2811 OO
.initClass( OO
.ui
.IndicatedElement
);
2813 /* Static Properties */
2820 * @property {string|null} Symbolic indicator name or null for no indicator
2822 OO
.ui
.IndicatedElement
.static.indicator
= null;
2829 * @property {string|Function|null} Indicator title text, a function that return text or null for no
2832 OO
.ui
.IndicatedElement
.static.indicatorTitle
= null;
2839 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
2842 OO
.ui
.IndicatedElement
.prototype.setIndicator = function ( indicator
) {
2843 if ( this.indicator
) {
2844 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2845 this.indicator
= null;
2847 if ( typeof indicator
=== 'string' ) {
2848 indicator
= indicator
.trim();
2849 if ( indicator
.length
) {
2850 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2851 this.indicator
= indicator
;
2854 this.$element
.toggleClass( 'oo-ui-indicatedElement', !!this.indicator
);
2860 * Set indicator label.
2862 * @param {string|Function|null} indicator Indicator title text, a function that return text or null
2863 * for no indicator title
2866 OO
.ui
.IndicatedElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2867 this.indicatorTitle
= indicatorTitle
= OO
.ui
.resolveMsg( indicatorTitle
);
2869 if ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) {
2870 this.$indicator
.attr( 'title', indicatorTitle
);
2872 this.$indicator
.removeAttr( 'title' );
2881 * @return {string} title Symbolic name of indicator
2883 OO
.ui
.IndicatedElement
.prototype.getIndicator = function () {
2884 return this.indicator
;
2888 * Get indicator title.
2890 * @return {string} Indicator title text
2892 OO
.ui
.IndicatedElement
.prototype.getIndicatorTitle = function () {
2893 return this.indicatorTitle
;
2896 * Element containing a label.
2902 * @param {jQuery} $label Label node, assigned to #$label
2903 * @param {Object} [config] Configuration options
2904 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
2905 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
2907 OO
.ui
.LabeledElement
= function OoUiLabeledElement( $label
, config
) {
2908 // Config intialization
2909 config
= config
|| {};
2912 this.$label
= $label
;
2916 this.$label
.addClass( 'oo-ui-labeledElement-label' );
2917 this.setLabel( config
.label
|| this.constructor.static.label
);
2918 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
2923 OO
.initClass( OO
.ui
.LabeledElement
);
2925 /* Static Properties */
2932 * @property {string|Function|null} Label text; a function that returns a nodes or text; or null for
2935 OO
.ui
.LabeledElement
.static.label
= null;
2942 * An empty string will result in the label being hidden. A string containing only whitespace will
2943 * be converted to a single
2945 * @param {jQuery|string|Function|null} label Label nodes; text; a function that retuns nodes or
2946 * text; or null for no label
2949 OO
.ui
.LabeledElement
.prototype.setLabel = function ( label
) {
2952 this.label
= label
= OO
.ui
.resolveMsg( label
) || null;
2953 if ( typeof label
=== 'string' && label
.length
) {
2954 if ( label
.match( /^\s*$/ ) ) {
2955 // Convert whitespace only string to a single non-breaking space
2956 this.$label
.html( ' ' );
2958 this.$label
.text( label
);
2960 } else if ( label
instanceof jQuery
) {
2961 this.$label
.empty().append( label
);
2963 this.$label
.empty();
2966 this.$element
.toggleClass( 'oo-ui-labeledElement', !empty
);
2967 this.$label
.css( 'display', empty
? 'none' : '' );
2975 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
2976 * text; or null for no label
2978 OO
.ui
.LabeledElement
.prototype.getLabel = function () {
2987 OO
.ui
.LabeledElement
.prototype.fitLabel = function () {
2988 if ( this.$label
.autoEllipsis
&& this.autoFitLabel
) {
2989 this.$label
.autoEllipsis( { 'hasSpan': false, 'tooltip': true } );
2994 * Popuppable element.
3000 * @param {Object} [config] Configuration options
3001 * @cfg {number} [popupWidth=320] Width of popup
3002 * @cfg {number} [popupHeight] Height of popup
3003 * @cfg {Object} [popup] Configuration to pass to popup
3005 OO
.ui
.PopuppableElement
= function OoUiPopuppableElement( config
) {
3006 // Configuration initialization
3007 config
= $.extend( { 'popupWidth': 320 }, config
);
3010 this.popup
= new OO
.ui
.PopupWidget( $.extend(
3011 { 'align': 'center', 'autoClose': true },
3013 { '$': this.$, '$autoCloseIgnore': this.$element
}
3015 this.popupWidth
= config
.popupWidth
;
3016 this.popupHeight
= config
.popupHeight
;
3024 * @return {OO.ui.PopupWidget} Popup widget
3026 OO
.ui
.PopuppableElement
.prototype.getPopup = function () {
3033 OO
.ui
.PopuppableElement
.prototype.showPopup = function () {
3034 this.popup
.show().display( this.popupWidth
, this.popupHeight
);
3040 OO
.ui
.PopuppableElement
.prototype.hidePopup = function () {
3044 * Element with a title.
3050 * @param {jQuery} $label Titled node, assigned to #$titled
3051 * @param {Object} [config] Configuration options
3052 * @cfg {string|Function} [title] Title text or a function that returns text
3054 OO
.ui
.TitledElement
= function OoUiTitledElement( $titled
, config
) {
3055 // Config intialization
3056 config
= config
|| {};
3059 this.$titled
= $titled
;
3063 this.setTitle( config
.title
|| this.constructor.static.title
);
3068 OO
.initClass( OO
.ui
.TitledElement
);
3070 /* Static Properties */
3077 * @property {string|Function} Title text or a function that returns text
3079 OO
.ui
.TitledElement
.static.title
= null;
3086 * @param {string|Function|null} title Title text, a function that returns text or null for no title
3089 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
3090 this.title
= title
= OO
.ui
.resolveMsg( title
) || null;
3092 if ( typeof title
=== 'string' && title
.length
) {
3093 this.$titled
.attr( 'title', title
);
3095 this.$titled
.removeAttr( 'title' );
3104 * @return {string} Title string
3106 OO
.ui
.TitledElement
.prototype.getTitle = function () {
3110 * Generic toolbar tool.
3114 * @extends OO.ui.Widget
3115 * @mixins OO.ui.IconedElement
3118 * @param {OO.ui.ToolGroup} toolGroup
3119 * @param {Object} [config] Configuration options
3120 * @cfg {string|Function} [title] Title text or a function that returns text
3122 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
3123 // Config intialization
3124 config
= config
|| {};
3126 // Parent constructor
3127 OO
.ui
.Tool
.super.call( this, config
);
3129 // Mixin constructors
3130 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
3133 this.toolGroup
= toolGroup
;
3134 this.toolbar
= this.toolGroup
.getToolbar();
3135 this.active
= false;
3136 this.$title
= this.$( '<span>' );
3137 this.$link
= this.$( '<a>' );
3141 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
3144 this.$title
.addClass( 'oo-ui-tool-title' );
3146 .addClass( 'oo-ui-tool-link' )
3147 .append( this.$icon
, this.$title
);
3149 .data( 'oo-ui-tool', this )
3151 'oo-ui-tool ' + 'oo-ui-tool-name-' +
3152 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
3154 .append( this.$link
);
3155 this.setTitle( config
.title
|| this.constructor.static.title
);
3160 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
3161 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconedElement
);
3169 /* Static Properties */
3175 OO
.ui
.Tool
.static.tagName
= 'span';
3178 * Symbolic name of tool.
3183 * @property {string}
3185 OO
.ui
.Tool
.static.name
= '';
3193 * @property {string}
3195 OO
.ui
.Tool
.static.group
= '';
3200 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
3201 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
3202 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
3203 * appended to the title if the tool is part of a bar tool group.
3208 * @property {string|Function} Title text or a function that returns text
3210 OO
.ui
.Tool
.static.title
= '';
3213 * Tool can be automatically added to catch-all groups.
3217 * @property {boolean}
3219 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
3222 * Tool can be automatically added to named groups.
3225 * @property {boolean}
3228 OO
.ui
.Tool
.static.autoAddToGroup
= true;
3231 * Check if this tool is compatible with given data.
3235 * @param {Mixed} data Data to check
3236 * @return {boolean} Tool can be used with data
3238 OO
.ui
.Tool
.static.isCompatibleWith = function () {
3245 * Handle the toolbar state being updated.
3247 * This is an abstract method that must be overridden in a concrete subclass.
3251 OO
.ui
.Tool
.prototype.onUpdateState = function () {
3253 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
3258 * Handle the tool being selected.
3260 * This is an abstract method that must be overridden in a concrete subclass.
3264 OO
.ui
.Tool
.prototype.onSelect = function () {
3266 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
3271 * Check if the button is active.
3273 * @param {boolean} Button is active
3275 OO
.ui
.Tool
.prototype.isActive = function () {
3280 * Make the button appear active or inactive.
3282 * @param {boolean} state Make button appear active
3284 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
3285 this.active
= !!state
;
3286 if ( this.active
) {
3287 this.$element
.addClass( 'oo-ui-tool-active' );
3289 this.$element
.removeClass( 'oo-ui-tool-active' );
3294 * Get the tool title.
3296 * @param {string|Function} title Title text or a function that returns text
3299 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
3300 this.title
= OO
.ui
.resolveMsg( title
);
3306 * Get the tool title.
3308 * @return {string} Title text
3310 OO
.ui
.Tool
.prototype.getTitle = function () {
3315 * Get the tool's symbolic name.
3317 * @return {string} Symbolic name of tool
3319 OO
.ui
.Tool
.prototype.getName = function () {
3320 return this.constructor.static.name
;
3326 OO
.ui
.Tool
.prototype.updateTitle = function () {
3327 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
3328 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
3329 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
3336 .addClass( 'oo-ui-tool-accel' )
3340 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
3341 tooltipParts
.push( this.title
);
3343 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
3344 tooltipParts
.push( accel
);
3346 if ( tooltipParts
.length
) {
3347 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
3349 this.$link
.removeAttr( 'title' );
3356 OO
.ui
.Tool
.prototype.destroy = function () {
3357 this.toolbar
.disconnect( this );
3358 this.$element
.remove();
3361 * Collection of tool groups.
3364 * @extends OO.ui.Element
3365 * @mixins OO.EventEmitter
3366 * @mixins OO.ui.GroupElement
3369 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
3370 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
3371 * @param {Object} [config] Configuration options
3372 * @cfg {boolean} [actions] Add an actions section opposite to the tools
3373 * @cfg {boolean} [shadow] Add a shadow below the toolbar
3375 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
3376 // Configuration initialization
3377 config
= config
|| {};
3379 // Parent constructor
3380 OO
.ui
.Toolbar
.super.call( this, config
);
3382 // Mixin constructors
3383 OO
.EventEmitter
.call( this );
3384 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3387 this.toolFactory
= toolFactory
;
3388 this.toolGroupFactory
= toolGroupFactory
;
3391 this.$bar
= this.$( '<div>' );
3392 this.$actions
= this.$( '<div>' );
3393 this.initialized
= false;
3397 .add( this.$bar
).add( this.$group
).add( this.$actions
)
3398 .on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
3401 this.$group
.addClass( 'oo-ui-toolbar-tools' );
3402 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
3403 if ( config
.actions
) {
3404 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
3405 this.$bar
.append( this.$actions
);
3407 this.$bar
.append( '<div style="clear:both"></div>' );
3408 if ( config
.shadow
) {
3409 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
3411 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
3416 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
3417 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
3418 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
3423 * Get the tool factory.
3425 * @return {OO.ui.ToolFactory} Tool factory
3427 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
3428 return this.toolFactory
;
3432 * Get the tool group factory.
3434 * @return {OO.Factory} Tool group factory
3436 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
3437 return this.toolGroupFactory
;
3441 * Handles mouse down events.
3443 * @param {jQuery.Event} e Mouse down event
3445 OO
.ui
.Toolbar
.prototype.onMouseDown = function ( e
) {
3446 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
3447 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
3448 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
3454 * Sets up handles and preloads required information for the toolbar to work.
3455 * This must be called immediately after it is attached to a visible document.
3457 OO
.ui
.Toolbar
.prototype.initialize = function () {
3458 this.initialized
= true;
3464 * Tools can be specified in the following ways:
3466 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3467 * - All tools in a group: `{ 'group': 'group-name' }`
3468 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
3470 * @param {Object.<string,Array>} groups List of tool group configurations
3471 * @param {Array|string} [groups.include] Tools to include
3472 * @param {Array|string} [groups.exclude] Tools to exclude
3473 * @param {Array|string} [groups.promote] Tools to promote to the beginning
3474 * @param {Array|string} [groups.demote] Tools to demote to the end
3476 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
3477 var i
, len
, type
, group
,
3479 defaultType
= 'bar';
3481 // Cleanup previous groups
3484 // Build out new groups
3485 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
3487 if ( group
.include
=== '*' ) {
3488 // Apply defaults to catch-all groups
3489 if ( group
.type
=== undefined ) {
3490 group
.type
= 'list';
3492 if ( group
.label
=== undefined ) {
3493 group
.label
= 'ooui-toolbar-more';
3496 // Check type has been registered
3497 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
3499 this.getToolGroupFactory().create( type
, this, $.extend( { '$': this.$ }, group
) )
3502 this.addItems( items
);
3506 * Remove all tools and groups from the toolbar.
3508 OO
.ui
.Toolbar
.prototype.reset = function () {
3513 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3514 this.items
[i
].destroy();
3520 * Destroys toolbar, removing event handlers and DOM elements.
3522 * Call this whenever you are done using a toolbar.
3524 OO
.ui
.Toolbar
.prototype.destroy = function () {
3526 this.$element
.remove();
3530 * Check if tool has not been used yet.
3532 * @param {string} name Symbolic name of tool
3533 * @return {boolean} Tool is available
3535 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
3536 return !this.tools
[name
];
3540 * Prevent tool from being used again.
3542 * @param {OO.ui.Tool} tool Tool to reserve
3544 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
3545 this.tools
[tool
.getName()] = tool
;
3549 * Allow tool to be used again.
3551 * @param {OO.ui.Tool} tool Tool to release
3553 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
3554 delete this.tools
[tool
.getName()];
3558 * Get accelerator label for tool.
3560 * This is a stub that should be overridden to provide access to accelerator information.
3562 * @param {string} name Symbolic name of tool
3563 * @return {string|undefined} Tool accelerator label if available
3565 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
3569 * Factory for tools.
3572 * @extends OO.Factory
3575 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3576 // Parent constructor
3577 OO
.ui
.ToolFactory
.super.call( this );
3582 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3587 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3588 var i
, len
, included
, promoted
, demoted
,
3592 // Collect included and not excluded tools
3593 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3596 promoted
= this.extract( promote
, used
);
3597 demoted
= this.extract( demote
, used
);
3600 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3601 if ( !used
[included
[i
]] ) {
3602 auto
.push( included
[i
] );
3606 return promoted
.concat( auto
).concat( demoted
);
3610 * Get a flat list of names from a list of names or groups.
3612 * Tools can be specified in the following ways:
3614 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3615 * - All tools in a group: `{ 'group': 'group-name' }`
3616 * - All tools: `'*'`
3619 * @param {Array|string} collection List of tools
3620 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3621 * names will be added as properties
3622 * @return {string[]} List of extracted names
3624 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3625 var i
, len
, item
, name
, tool
,
3628 if ( collection
=== '*' ) {
3629 for ( name
in this.registry
) {
3630 tool
= this.registry
[name
];
3632 // Only add tools by group name when auto-add is enabled
3633 tool
.static.autoAddToCatchall
&&
3634 // Exclude already used tools
3635 ( !used
|| !used
[name
] )
3643 } else if ( $.isArray( collection
) ) {
3644 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3645 item
= collection
[i
];
3646 // Allow plain strings as shorthand for named tools
3647 if ( typeof item
=== 'string' ) {
3648 item
= { 'name': item
};
3650 if ( OO
.isPlainObject( item
) ) {
3652 for ( name
in this.registry
) {
3653 tool
= this.registry
[name
];
3655 // Include tools with matching group
3656 tool
.static.group
=== item
.group
&&
3657 // Only add tools by group name when auto-add is enabled
3658 tool
.static.autoAddToGroup
&&
3659 // Exclude already used tools
3660 ( !used
|| !used
[name
] )
3668 // Include tools with matching name and exclude already used tools
3669 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3670 names
.push( item
.name
);
3672 used
[item
.name
] = true;
3681 * Collection of tools.
3683 * Tools can be specified in the following ways:
3685 * - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
3686 * - All tools in a group: `{ 'group': 'group-name' }`
3687 * - All tools: `'*'`
3691 * @extends OO.ui.Widget
3692 * @mixins OO.ui.GroupElement
3695 * @param {OO.ui.Toolbar} toolbar
3696 * @param {Object} [config] Configuration options
3697 * @cfg {Array|string} [include=[]] List of tools to include
3698 * @cfg {Array|string} [exclude=[]] List of tools to exclude
3699 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
3700 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
3702 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
3703 // Configuration initialization
3704 config
= config
|| {};
3706 // Parent constructor
3707 OO
.ui
.ToolGroup
.super.call( this, config
);
3709 // Mixin constructors
3710 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
3713 this.toolbar
= toolbar
;
3715 this.pressed
= null;
3716 this.autoDisabled
= false;
3717 this.include
= config
.include
|| [];
3718 this.exclude
= config
.exclude
|| [];
3719 this.promote
= config
.promote
|| [];
3720 this.demote
= config
.demote
|| [];
3721 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
3725 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
3726 'mouseup': OO
.ui
.bind( this.onMouseUp
, this ),
3727 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
3728 'mouseout': OO
.ui
.bind( this.onMouseOut
, this )
3730 this.toolbar
.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
3731 this.aggregate( { 'disable': 'itemDisable' } );
3732 this.connect( this, { 'itemDisable': 'updateDisabled' } );
3735 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
3737 .addClass( 'oo-ui-toolGroup' )
3738 .append( this.$group
);
3744 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
3745 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
3753 /* Static Properties */
3756 * Show labels in tooltips.
3760 * @property {boolean}
3762 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
3765 * Show acceleration labels in tooltips.
3769 * @property {boolean}
3771 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
3774 * Automatically disable the toolgroup when all tools are disabled
3778 * @property {boolean}
3780 OO
.ui
.ToolGroup
.static.autoDisable
= true;
3787 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
3788 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
3794 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
3795 var i
, item
, allDisabled
= true;
3797 if ( this.constructor.static.autoDisable
) {
3798 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
3799 item
= this.items
[i
];
3800 if ( !item
.isDisabled() ) {
3801 allDisabled
= false;
3805 this.autoDisabled
= allDisabled
;
3807 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
3811 * Handle mouse down events.
3813 * @param {jQuery.Event} e Mouse down event
3815 OO
.ui
.ToolGroup
.prototype.onMouseDown = function ( e
) {
3816 if ( !this.isDisabled() && e
.which
=== 1 ) {
3817 this.pressed
= this.getTargetTool( e
);
3818 if ( this.pressed
) {
3819 this.pressed
.setActive( true );
3820 this.getElementDocument().addEventListener(
3821 'mouseup', this.onCapturedMouseUpHandler
, true
3829 * Handle captured mouse up events.
3831 * @param {Event} e Mouse up event
3833 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
3834 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
3835 // onMouseUp may be called a second time, depending on where the mouse is when the button is
3836 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
3837 this.onMouseUp( e
);
3841 * Handle mouse up events.
3843 * @param {jQuery.Event} e Mouse up event
3845 OO
.ui
.ToolGroup
.prototype.onMouseUp = function ( e
) {
3846 var tool
= this.getTargetTool( e
);
3848 if ( !this.isDisabled() && e
.which
=== 1 && this.pressed
&& this.pressed
=== tool
) {
3849 this.pressed
.onSelect();
3852 this.pressed
= null;
3857 * Handle mouse over events.
3859 * @param {jQuery.Event} e Mouse over event
3861 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
3862 var tool
= this.getTargetTool( e
);
3864 if ( this.pressed
&& this.pressed
=== tool
) {
3865 this.pressed
.setActive( true );
3870 * Handle mouse out events.
3872 * @param {jQuery.Event} e Mouse out event
3874 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
3875 var tool
= this.getTargetTool( e
);
3877 if ( this.pressed
&& this.pressed
=== tool
) {
3878 this.pressed
.setActive( false );
3883 * Get the closest tool to a jQuery.Event.
3885 * Only tool links are considered, which prevents other elements in the tool such as popups from
3886 * triggering tool group interactions.
3889 * @param {jQuery.Event} e
3890 * @return {OO.ui.Tool|null} Tool, `null` if none was found
3892 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
3894 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
3896 if ( $item
.length
) {
3897 tool
= $item
.parent().data( 'oo-ui-tool' );
3900 return tool
&& !tool
.isDisabled() ? tool
: null;
3904 * Handle tool registry register events.
3906 * If a tool is registered after the group is created, we must repopulate the list to account for:
3908 * - a tool being added that may be included
3909 * - a tool already included being overridden
3911 * @param {string} name Symbolic name of tool
3913 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
3918 * Get the toolbar this group is in.
3920 * @return {OO.ui.Toolbar} Toolbar of group
3922 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
3923 return this.toolbar
;
3927 * Add and remove tools based on configuration.
3929 OO
.ui
.ToolGroup
.prototype.populate = function () {
3930 var i
, len
, name
, tool
,
3931 toolFactory
= this.toolbar
.getToolFactory(),
3935 list
= this.toolbar
.getToolFactory().getTools(
3936 this.include
, this.exclude
, this.promote
, this.demote
3939 // Build a list of needed tools
3940 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
3944 toolFactory
.lookup( name
) &&
3945 // Tool is available or is already in this group
3946 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
3948 tool
= this.tools
[name
];
3950 // Auto-initialize tools on first use
3951 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
3954 this.toolbar
.reserveTool( tool
);
3959 // Remove tools that are no longer needed
3960 for ( name
in this.tools
) {
3961 if ( !names
[name
] ) {
3962 this.tools
[name
].destroy();
3963 this.toolbar
.releaseTool( this.tools
[name
] );
3964 remove
.push( this.tools
[name
] );
3965 delete this.tools
[name
];
3968 if ( remove
.length
) {
3969 this.removeItems( remove
);
3971 // Update emptiness state
3973 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
3975 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
3977 // Re-add tools (moving existing ones to new locations)
3978 this.addItems( add
);
3979 // Disabled state may depend on items
3980 this.updateDisabled();
3984 * Destroy tool group.
3986 OO
.ui
.ToolGroup
.prototype.destroy = function () {
3990 this.toolbar
.getToolFactory().disconnect( this );
3991 for ( name
in this.tools
) {
3992 this.toolbar
.releaseTool( this.tools
[name
] );
3993 this.tools
[name
].disconnect( this ).destroy();
3994 delete this.tools
[name
];
3996 this.$element
.remove();
3999 * Factory for tool groups.
4002 * @extends OO.Factory
4005 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
4006 // Parent constructor
4007 OO
.Factory
.call( this );
4010 defaultClasses
= this.constructor.static.getDefaultClasses();
4012 // Register default toolgroups
4013 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
4014 this.register( defaultClasses
[i
] );
4020 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
4022 /* Static Methods */
4025 * Get a default set of classes to be registered on construction
4027 * @return {Function[]} Default classes
4029 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
4032 OO
.ui
.ListToolGroup
,
4037 * Layout made of a fieldset and optional legend.
4039 * Just add OO.ui.FieldLayout items.
4042 * @extends OO.ui.Layout
4043 * @mixins OO.ui.LabeledElement
4044 * @mixins OO.ui.IconedElement
4045 * @mixins OO.ui.GroupElement
4048 * @param {Object} [config] Configuration options
4049 * @cfg {string} [icon] Symbolic icon name
4050 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
4052 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
4053 // Config initialization
4054 config
= config
|| {};
4056 // Parent constructor
4057 OO
.ui
.FieldsetLayout
.super.call( this, config
);
4059 // Mixin constructors
4060 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
4061 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
4062 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
4066 .addClass( 'oo-ui-fieldsetLayout' )
4067 .prepend( this.$icon
, this.$label
, this.$group
);
4068 if ( $.isArray( config
.items
) ) {
4069 this.addItems( config
.items
);
4075 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
4076 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconedElement
);
4077 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabeledElement
);
4078 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
4080 /* Static Properties */
4082 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
4084 * Layout made of a field and optional label.
4087 * @extends OO.ui.Layout
4088 * @mixins OO.ui.LabeledElement
4090 * Available label alignment modes include:
4091 * - 'left': Label is before the field and aligned away from it, best for when the user will be
4092 * scanning for a specific label in a form with many fields
4093 * - 'right': Label is before the field and aligned toward it, best for forms the user is very
4094 * familiar with and will tab through field checking quickly to verify which field they are in
4095 * - 'top': Label is before the field and above it, best for when the use will need to fill out all
4096 * fields from top to bottom in a form with few fields
4097 * - 'inline': Label is after the field and aligned toward it, best for small boolean fields like
4098 * checkboxes or radio buttons
4101 * @param {OO.ui.Widget} field Field widget
4102 * @param {Object} [config] Configuration options
4103 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
4105 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
4106 // Config initialization
4107 config
= $.extend( { 'align': 'left' }, config
);
4109 // Parent constructor
4110 OO
.ui
.FieldLayout
.super.call( this, config
);
4112 // Mixin constructors
4113 OO
.ui
.LabeledElement
.call( this, this.$( '<label>' ), config
);
4116 this.$field
= this.$( '<div>' );
4121 if ( this.field
instanceof OO
.ui
.InputWidget
) {
4122 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
4124 this.field
.connect( this, { 'disable': 'onFieldDisable' } );
4127 this.$element
.addClass( 'oo-ui-fieldLayout' );
4129 .addClass( 'oo-ui-fieldLayout-field' )
4130 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
4131 .append( this.field
.$element
);
4132 this.setAlignment( config
.align
);
4137 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
4138 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabeledElement
);
4143 * Handle field disable events.
4145 * @param {boolean} value Field is disabled
4147 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
4148 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
4152 * Handle label mouse click events.
4154 * @param {jQuery.Event} e Mouse click event
4156 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
4157 this.field
.simulateLabelClick();
4164 * @return {OO.ui.Widget} Field widget
4166 OO
.ui
.FieldLayout
.prototype.getField = function () {
4171 * Set the field alignment mode.
4173 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
4176 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
4177 if ( value
!== this.align
) {
4178 // Default to 'left'
4179 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
4183 if ( value
=== 'inline' ) {
4184 this.$element
.append( this.$field
, this.$label
);
4186 this.$element
.append( this.$label
, this.$field
);
4190 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
4193 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
4199 * Layout made of proportionally sized columns and rows.
4202 * @extends OO.ui.Layout
4205 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
4206 * @param {Object} [config] Configuration options
4207 * @cfg {number[]} [widths] Widths of columns as ratios
4208 * @cfg {number[]} [heights] Heights of columns as ratios
4210 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
4213 // Config initialization
4214 config
= config
|| {};
4216 // Parent constructor
4217 OO
.ui
.GridLayout
.super.call( this, config
);
4225 this.$element
.addClass( 'oo-ui-gridLayout' );
4226 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
4227 this.panels
.push( panels
[i
] );
4228 this.$element
.append( panels
[i
].$element
);
4230 if ( config
.widths
|| config
.heights
) {
4231 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
4233 // Arrange in columns by default
4235 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
4238 this.layout( widths
, [ 1 ] );
4244 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
4256 /* Static Properties */
4258 OO
.ui
.GridLayout
.static.tagName
= 'div';
4263 * Set grid dimensions.
4265 * @param {number[]} widths Widths of columns as ratios
4266 * @param {number[]} heights Heights of rows as ratios
4268 * @throws {Error} If grid is not large enough to fit all panels
4270 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
4274 cols
= widths
.length
,
4275 rows
= heights
.length
;
4277 // Verify grid is big enough to fit panels
4278 if ( cols
* rows
< this.panels
.length
) {
4279 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
4282 // Sum up denominators
4283 for ( x
= 0; x
< cols
; x
++ ) {
4286 for ( y
= 0; y
< rows
; y
++ ) {
4292 for ( x
= 0; x
< cols
; x
++ ) {
4293 this.widths
[x
] = widths
[x
] / xd
;
4295 for ( y
= 0; y
< rows
; y
++ ) {
4296 this.heights
[y
] = heights
[y
] / yd
;
4300 this.emit( 'layout' );
4304 * Update panel positions and sizes.
4308 OO
.ui
.GridLayout
.prototype.update = function () {
4316 cols
= this.widths
.length
,
4317 rows
= this.heights
.length
;
4319 for ( y
= 0; y
< rows
; y
++ ) {
4320 for ( x
= 0; x
< cols
; x
++ ) {
4321 panel
= this.panels
[i
];
4322 width
= this.widths
[x
];
4323 height
= this.heights
[y
];
4325 'width': Math
.round( width
* 100 ) + '%',
4326 'height': Math
.round( height
* 100 ) + '%',
4327 'top': Math
.round( top
* 100 ) + '%'
4330 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
4331 dimensions
.right
= Math
.round( left
* 100 ) + '%';
4333 dimensions
.left
= Math
.round( left
* 100 ) + '%';
4335 panel
.$element
.css( dimensions
);
4343 this.emit( 'update' );
4347 * Get a panel at a given position.
4349 * The x and y position is affected by the current grid layout.
4351 * @param {number} x Horizontal position
4352 * @param {number} y Vertical position
4353 * @return {OO.ui.PanelLayout} The panel at the given postion
4355 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
4356 return this.panels
[( x
* this.widths
.length
) + y
];
4359 * Layout containing a series of pages.
4362 * @extends OO.ui.Layout
4365 * @param {Object} [config] Configuration options
4366 * @cfg {boolean} [continuous=false] Show all pages, one after another
4367 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
4368 * @cfg {boolean} [outlined=false] Show an outline
4369 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
4371 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
4372 // Initialize configuration
4373 config
= config
|| {};
4375 // Parent constructor
4376 OO
.ui
.BookletLayout
.super.call( this, config
);
4379 this.currentPageName
= null;
4381 this.ignoreFocus
= false;
4382 this.stackLayout
= new OO
.ui
.StackLayout( { '$': this.$, 'continuous': !!config
.continuous
} );
4383 this.autoFocus
= config
.autoFocus
=== undefined ? true : !!config
.autoFocus
;
4384 this.outlineVisible
= false;
4385 this.outlined
= !!config
.outlined
;
4386 if ( this.outlined
) {
4387 this.editable
= !!config
.editable
;
4388 this.outlineControlsWidget
= null;
4389 this.outlineWidget
= new OO
.ui
.OutlineWidget( { '$': this.$ } );
4390 this.outlinePanel
= new OO
.ui
.PanelLayout( { '$': this.$, 'scrollable': true } );
4391 this.gridLayout
= new OO
.ui
.GridLayout(
4392 [ this.outlinePanel
, this.stackLayout
],
4393 { '$': this.$, 'widths': [ 1, 2 ] }
4395 this.outlineVisible
= true;
4396 if ( this.editable
) {
4397 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
4398 this.outlineWidget
, { '$': this.$ }
4404 this.stackLayout
.connect( this, { 'set': 'onStackLayoutSet' } );
4405 if ( this.outlined
) {
4406 this.outlineWidget
.connect( this, { 'select': 'onOutlineWidgetSelect' } );
4408 if ( this.autoFocus
) {
4409 // Event 'focus' does not bubble, but 'focusin' does
4410 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
4414 this.$element
.addClass( 'oo-ui-bookletLayout' );
4415 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
4416 if ( this.outlined
) {
4417 this.outlinePanel
.$element
4418 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
4419 .append( this.outlineWidget
.$element
);
4420 if ( this.editable
) {
4421 this.outlinePanel
.$element
4422 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
4423 .append( this.outlineControlsWidget
.$element
);
4425 this.$element
.append( this.gridLayout
.$element
);
4427 this.$element
.append( this.stackLayout
.$element
);
4433 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
4439 * @param {OO.ui.PageLayout} page Current page
4444 * @param {OO.ui.PageLayout[]} page Added pages
4445 * @param {number} index Index pages were added at
4450 * @param {OO.ui.PageLayout[]} pages Removed pages
4456 * Handle stack layout focus.
4458 * @param {jQuery.Event} e Focusin event
4460 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
4463 // Find the page that an element was focused within
4464 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
4465 for ( name
in this.pages
) {
4466 // Check for page match, exclude current page to find only page changes
4467 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
4468 this.setPage( name
);
4475 * Handle stack layout set events.
4477 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
4479 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
4481 page
.scrollElementIntoView( { 'complete': OO
.ui
.bind( function () {
4482 if ( this.autoFocus
) {
4483 // Set focus to the first input if nothing on the page is focused yet
4484 if ( !page
.$element
.find( ':focus' ).length
) {
4485 page
.$element
.find( ':input:first' ).focus();
4493 * Handle outline widget select events.
4495 * @param {OO.ui.OptionWidget|null} item Selected item
4497 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
4499 this.setPage( item
.getData() );
4504 * Check if booklet has an outline.
4508 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
4509 return this.outlined
;
4513 * Check if booklet has editing controls.
4517 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
4518 return this.editable
;
4522 * Check if booklet has a visible outline.
4526 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
4527 return this.outlined
&& this.outlineVisible
;
4531 * Hide or show the outline.
4533 * @param {boolean} [show] Show outline, omit to invert current state
4536 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
4537 if ( this.outlined
) {
4538 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
4539 this.outlineVisible
= show
;
4540 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
4547 * Get the outline widget.
4549 * @param {OO.ui.PageLayout} page Page to be selected
4550 * @return {OO.ui.PageLayout|null} Closest page to another
4552 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
4553 var next
, prev
, level
,
4554 pages
= this.stackLayout
.getItems(),
4555 index
= $.inArray( page
, pages
);
4557 if ( index
!== -1 ) {
4558 next
= pages
[index
+ 1];
4559 prev
= pages
[index
- 1];
4560 // Prefer adjacent pages at the same level
4561 if ( this.outlined
) {
4562 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
4565 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
4571 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
4577 return prev
|| next
|| null;
4581 * Get the outline widget.
4583 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
4585 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
4586 return this.outlineWidget
;
4590 * Get the outline controls widget. If the outline is not editable, null is returned.
4592 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
4594 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
4595 return this.outlineControlsWidget
;
4599 * Get a page by name.
4601 * @param {string} name Symbolic name of page
4602 * @return {OO.ui.PageLayout|undefined} Page, if found
4604 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
4605 return this.pages
[name
];
4609 * Get the current page name.
4611 * @return {string|null} Current page name
4613 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
4614 return this.currentPageName
;
4618 * Add a page to the layout.
4620 * When pages are added with the same names as existing pages, the existing pages will be
4621 * automatically removed before the new pages are added.
4623 * @param {OO.ui.PageLayout[]} pages Pages to add
4624 * @param {number} index Index to insert pages after
4628 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
4629 var i
, len
, name
, page
, item
, currentIndex
,
4630 stackLayoutPages
= this.stackLayout
.getItems(),
4634 // Remove pages with same names
4635 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4637 name
= page
.getName();
4639 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
4640 // Correct the insertion index
4641 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
4642 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
4645 remove
.push( this.pages
[name
] );
4648 if ( remove
.length
) {
4649 this.removePages( remove
);
4653 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4655 name
= page
.getName();
4656 this.pages
[page
.getName()] = page
;
4657 if ( this.outlined
) {
4658 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { '$': this.$ } );
4659 page
.setOutlineItem( item
);
4664 if ( this.outlined
&& items
.length
) {
4665 this.outlineWidget
.addItems( items
, index
);
4666 this.updateOutlineWidget();
4668 this.stackLayout
.addItems( pages
, index
);
4669 this.emit( 'add', pages
, index
);
4675 * Remove a page from the layout.
4680 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
4681 var i
, len
, name
, page
,
4684 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4686 name
= page
.getName();
4687 delete this.pages
[name
];
4688 if ( this.outlined
) {
4689 items
.push( this.outlineWidget
.getItemFromData( name
) );
4690 page
.setOutlineItem( null );
4693 if ( this.outlined
&& items
.length
) {
4694 this.outlineWidget
.removeItems( items
);
4695 this.updateOutlineWidget();
4697 this.stackLayout
.removeItems( pages
);
4698 this.emit( 'remove', pages
);
4704 * Clear all pages from the layout.
4709 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
4711 pages
= this.stackLayout
.getItems();
4714 this.currentPageName
= null;
4715 if ( this.outlined
) {
4716 this.outlineWidget
.clearItems();
4717 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
4718 pages
[i
].setOutlineItem( null );
4721 this.stackLayout
.clearItems();
4723 this.emit( 'remove', pages
);
4729 * Set the current page by name.
4732 * @param {string} name Symbolic name of page
4734 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
4736 page
= this.pages
[name
];
4738 if ( name
!== this.currentPageName
) {
4739 if ( this.outlined
) {
4740 selectedItem
= this.outlineWidget
.getSelectedItem();
4741 if ( selectedItem
&& selectedItem
.getData() !== name
) {
4742 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
4746 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
4747 this.pages
[this.currentPageName
].setActive( false );
4748 // Blur anything focused if the next page doesn't have anything focusable - this
4749 // is not needed if the next page has something focusable because once it is focused
4750 // this blur happens automatically
4751 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
4752 this.pages
[this.currentPageName
].$element
.find( ':focus' ).blur();
4755 this.currentPageName
= name
;
4756 this.stackLayout
.setItem( page
);
4757 page
.setActive( true );
4758 this.emit( 'set', page
);
4764 * Call this after adding or removing items from the OutlineWidget.
4768 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
4769 // Auto-select first item when nothing is selected anymore
4770 if ( !this.outlineWidget
.getSelectedItem() ) {
4771 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
4777 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
4780 * @extends OO.ui.Layout
4783 * @param {Object} [config] Configuration options
4784 * @cfg {boolean} [scrollable] Allow vertical scrolling
4785 * @cfg {boolean} [padded] Pad the content from the edges
4787 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
4788 // Config initialization
4789 config
= config
|| {};
4791 // Parent constructor
4792 OO
.ui
.PanelLayout
.super.call( this, config
);
4795 this.$element
.addClass( 'oo-ui-panelLayout' );
4796 if ( config
.scrollable
) {
4797 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
4800 if ( config
.padded
) {
4801 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
4807 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
4809 * Page within an booklet layout.
4812 * @extends OO.ui.PanelLayout
4815 * @param {string} name Unique symbolic name of page
4816 * @param {Object} [config] Configuration options
4817 * @param {string} [outlineItem] Outline item widget
4819 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
4820 // Configuration initialization
4821 config
= $.extend( { 'scrollable': true }, config
);
4823 // Parent constructor
4824 OO
.ui
.PageLayout
.super.call( this, config
);
4828 this.outlineItem
= config
.outlineItem
|| null;
4829 this.active
= false;
4832 this.$element
.addClass( 'oo-ui-pageLayout' );
4837 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
4843 * @param {boolean} active Page is active
4851 * @return {string} Symbolic name of page
4853 OO
.ui
.PageLayout
.prototype.getName = function () {
4858 * Check if page is active.
4860 * @return {boolean} Page is active
4862 OO
.ui
.PageLayout
.prototype.isActive = function () {
4869 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
4871 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
4872 return this.outlineItem
;
4878 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
4881 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
4882 this.outlineItem
= outlineItem
;
4887 * Set page active state.
4889 * @param {boolean} Page is active
4892 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
4895 if ( active
!== this.active
) {
4896 this.active
= active
;
4897 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
4898 this.emit( 'active', this.active
);
4902 * Layout containing a series of mutually exclusive pages.
4905 * @extends OO.ui.PanelLayout
4906 * @mixins OO.ui.GroupElement
4909 * @param {Object} [config] Configuration options
4910 * @cfg {boolean} [continuous=false] Show all pages, one after another
4911 * @cfg {string} [icon=''] Symbolic icon name
4912 * @cfg {OO.ui.Layout[]} [items] Layouts to add
4914 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
4915 // Config initialization
4916 config
= $.extend( { 'scrollable': true }, config
);
4918 // Parent constructor
4919 OO
.ui
.StackLayout
.super.call( this, config
);
4921 // Mixin constructors
4922 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
4925 this.currentItem
= null;
4926 this.continuous
= !!config
.continuous
;
4929 this.$element
.addClass( 'oo-ui-stackLayout' );
4930 if ( this.continuous
) {
4931 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
4933 if ( $.isArray( config
.items
) ) {
4934 this.addItems( config
.items
);
4940 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
4941 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
4947 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
4953 * Get the current item.
4955 * @return {OO.ui.Layout|null}
4957 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
4958 return this.currentItem
;
4962 * Unset the current item.
4965 * @param {OO.ui.StackLayout} layout
4968 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
4969 var prevItem
= this.currentItem
;
4970 if ( prevItem
=== null ) {
4974 this.currentItem
= null;
4975 this.emit( 'set', null );
4981 * Adding an existing item (by value) will move it.
4983 * @param {OO.ui.Layout[]} items Items to add
4984 * @param {number} [index] Index to insert items after
4987 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
4989 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
4991 if ( !this.currentItem
&& items
.length
) {
4992 this.setItem( items
[0] );
5001 * Items will be detached, not removed, so they can be used later.
5003 * @param {OO.ui.Layout[]} items Items to remove
5007 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
5009 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
5011 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
5012 if ( this.items
.length
) {
5013 this.setItem( this.items
[0] );
5015 this.unsetCurrentItem();
5025 * Items will be detached, not removed, so they can be used later.
5030 OO
.ui
.StackLayout
.prototype.clearItems = function () {
5031 this.unsetCurrentItem();
5032 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
5040 * Any currently shown item will be hidden.
5042 * FIXME: If the passed item to show has not been added in the items list, then
5043 * this method drops it and unsets the current item.
5045 * @param {OO.ui.Layout} item Item to show
5049 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
5052 if ( item
!== this.currentItem
) {
5053 if ( !this.continuous
) {
5054 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5055 this.items
[i
].$element
.css( 'display', '' );
5058 if ( $.inArray( item
, this.items
) !== -1 ) {
5059 if ( !this.continuous
) {
5060 item
.$element
.css( 'display', 'block' );
5062 this.currentItem
= item
;
5063 this.emit( 'set', item
);
5065 this.unsetCurrentItem();
5072 * Horizontal bar layout of tools as icon buttons.
5075 * @extends OO.ui.ToolGroup
5078 * @param {OO.ui.Toolbar} toolbar
5079 * @param {Object} [config] Configuration options
5081 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
5082 // Parent constructor
5083 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
5086 this.$element
.addClass( 'oo-ui-barToolGroup' );
5091 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
5093 /* Static Properties */
5095 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
5097 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
5099 OO
.ui
.BarToolGroup
.static.name
= 'bar';
5101 * Popup list of tools with an icon and optional label.
5105 * @extends OO.ui.ToolGroup
5106 * @mixins OO.ui.IconedElement
5107 * @mixins OO.ui.IndicatedElement
5108 * @mixins OO.ui.LabeledElement
5109 * @mixins OO.ui.TitledElement
5110 * @mixins OO.ui.ClippableElement
5113 * @param {OO.ui.Toolbar} toolbar
5114 * @param {Object} [config] Configuration options
5115 * @cfg {string} [header] Text to display at the top of the pop-up
5117 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
5118 // Configuration initialization
5119 config
= config
|| {};
5121 // Parent constructor
5122 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
5124 // Mixin constructors
5125 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5126 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5127 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5128 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5129 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
5132 this.active
= false;
5133 this.dragging
= false;
5134 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
5135 this.$handle
= this.$( '<span>' );
5139 'mousedown': OO
.ui
.bind( this.onHandleMouseDown
, this ),
5140 'mouseup': OO
.ui
.bind( this.onHandleMouseUp
, this )
5145 .addClass( 'oo-ui-popupToolGroup-handle' )
5146 .append( this.$icon
, this.$label
, this.$indicator
);
5147 // If the pop-up should have a header, add it to the top of the toolGroup.
5148 // Note: If this feature is useful for other widgets, we could abstract it into an
5149 // OO.ui.HeaderedElement mixin constructor.
5150 if ( config
.header
!== undefined ) {
5152 .prepend( this.$( '<span>' )
5153 .addClass( 'oo-ui-popupToolGroup-header' )
5154 .text( config
.header
)
5158 .addClass( 'oo-ui-popupToolGroup' )
5159 .prepend( this.$handle
);
5164 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
5165 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconedElement
);
5166 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatedElement
);
5167 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabeledElement
);
5168 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
5169 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
5171 /* Static Properties */
5178 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
5180 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
5182 if ( this.isDisabled() && this.isElementAttached() ) {
5183 this.setActive( false );
5188 * Handle focus being lost.
5190 * The event is actually generated from a mouseup, so it is not a normal blur event object.
5192 * @param {jQuery.Event} e Mouse up event
5194 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
5195 // Only deactivate when clicking outside the dropdown element
5196 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
5197 this.setActive( false );
5204 OO
.ui
.PopupToolGroup
.prototype.onMouseUp = function ( e
) {
5205 if ( !this.isDisabled() && e
.which
=== 1 ) {
5206 this.setActive( false );
5208 return OO
.ui
.PopupToolGroup
.super.prototype.onMouseUp
.call( this, e
);
5212 * Handle mouse up events.
5214 * @param {jQuery.Event} e Mouse up event
5216 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseUp = function () {
5221 * Handle mouse down events.
5223 * @param {jQuery.Event} e Mouse down event
5225 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseDown = function ( e
) {
5226 if ( !this.isDisabled() && e
.which
=== 1 ) {
5227 this.setActive( !this.active
);
5233 * Switch into active mode.
5235 * When active, mouseup events anywhere in the document will trigger deactivation.
5237 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
5239 if ( this.active
!== value
) {
5240 this.active
= value
;
5242 this.setClipping( true );
5243 this.$element
.addClass( 'oo-ui-popupToolGroup-active' );
5244 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
5246 this.setClipping( false );
5247 this.$element
.removeClass( 'oo-ui-popupToolGroup-active' );
5248 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
5253 * Drop down list layout of tools as labeled icon buttons.
5256 * @extends OO.ui.PopupToolGroup
5259 * @param {OO.ui.Toolbar} toolbar
5260 * @param {Object} [config] Configuration options
5262 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
5263 // Parent constructor
5264 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
5267 this.$element
.addClass( 'oo-ui-listToolGroup' );
5272 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
5274 /* Static Properties */
5276 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
5278 OO
.ui
.ListToolGroup
.static.name
= 'list';
5280 * Drop down menu layout of tools as selectable menu items.
5283 * @extends OO.ui.PopupToolGroup
5286 * @param {OO.ui.Toolbar} toolbar
5287 * @param {Object} [config] Configuration options
5289 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
5290 // Configuration initialization
5291 config
= config
|| {};
5293 // Parent constructor
5294 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
5297 this.toolbar
.connect( this, { 'updateState': 'onUpdateState' } );
5300 this.$element
.addClass( 'oo-ui-menuToolGroup' );
5305 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
5307 /* Static Properties */
5309 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
5311 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
5316 * Handle the toolbar state being updated.
5318 * When the state changes, the title of each active item in the menu will be joined together and
5319 * used as a label for the group. The label will be empty if none of the items are active.
5321 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
5325 for ( name
in this.tools
) {
5326 if ( this.tools
[name
].isActive() ) {
5327 labelTexts
.push( this.tools
[name
].getTitle() );
5331 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
5334 * Tool that shows a popup when selected.
5338 * @extends OO.ui.Tool
5339 * @mixins OO.ui.PopuppableElement
5342 * @param {OO.ui.Toolbar} toolbar
5343 * @param {Object} [config] Configuration options
5345 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
5346 // Parent constructor
5347 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
5349 // Mixin constructors
5350 OO
.ui
.PopuppableElement
.call( this, config
);
5354 .addClass( 'oo-ui-popupTool' )
5355 .append( this.popup
.$element
);
5360 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
5361 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopuppableElement
);
5366 * Handle the tool being selected.
5370 OO
.ui
.PopupTool
.prototype.onSelect = function () {
5371 if ( !this.isDisabled() ) {
5372 if ( this.popup
.isVisible() ) {
5378 this.setActive( false );
5383 * Handle the toolbar state being updated.
5387 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
5388 this.setActive( false );
5393 * Mixin for OO.ui.Widget subclasses.
5395 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
5399 * @extends OO.ui.GroupElement
5402 * @param {jQuery} $group Container node, assigned to #$group
5403 * @param {Object} [config] Configuration options
5405 OO
.ui
.GroupWidget
= function OoUiGroupWidget( $element
, config
) {
5406 // Parent constructor
5407 OO
.ui
.GroupWidget
.super.call( this, $element
, config
);
5412 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
5417 * Set the disabled state of the widget.
5419 * This will also update the disabled state of child widgets.
5421 * @param {boolean} disabled Disable widget
5424 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
5428 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
5429 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
5431 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
5433 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5434 this.items
[i
].updateDisabled();
5443 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
5450 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
5457 * Check if widget is disabled.
5459 * Checks parent if present, making disabled state inheritable.
5461 * @return {boolean} Widget is disabled
5463 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
5464 return this.disabled
||
5465 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
5469 * Set group element is in.
5471 * @param {OO.ui.GroupElement|null} group Group element, null if none
5474 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
5476 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
5477 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
5479 // Initialize item disabled states
5480 this.updateDisabled();
5488 * @extends OO.ui.Widget
5489 * @mixins OO.ui.IconedElement
5490 * @mixins OO.ui.TitledElement
5493 * @param {Object} [config] Configuration options
5495 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
5496 // Config intialization
5497 config
= config
|| {};
5499 // Parent constructor
5500 OO
.ui
.IconWidget
.super.call( this, config
);
5502 // Mixin constructors
5503 OO
.ui
.IconedElement
.call( this, this.$element
, config
);
5504 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5507 this.$element
.addClass( 'oo-ui-iconWidget' );
5512 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
5513 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconedElement
);
5514 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
5516 /* Static Properties */
5518 OO
.ui
.IconWidget
.static.tagName
= 'span';
5523 * @extends OO.ui.Widget
5524 * @mixins OO.ui.IndicatedElement
5525 * @mixins OO.ui.TitledElement
5528 * @param {Object} [config] Configuration options
5530 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
5531 // Config intialization
5532 config
= config
|| {};
5534 // Parent constructor
5535 OO
.ui
.IndicatorWidget
.super.call( this, config
);
5537 // Mixin constructors
5538 OO
.ui
.IndicatedElement
.call( this, this.$element
, config
);
5539 OO
.ui
.TitledElement
.call( this, this.$element
, config
);
5542 this.$element
.addClass( 'oo-ui-indicatorWidget' );
5547 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
5548 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatedElement
);
5549 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
5551 /* Static Properties */
5553 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
5555 * Container for multiple related buttons.
5557 * Use together with OO.ui.ButtonWidget.
5560 * @extends OO.ui.Widget
5561 * @mixins OO.ui.GroupElement
5564 * @param {Object} [config] Configuration options
5565 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
5567 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
5568 // Parent constructor
5569 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
5571 // Mixin constructors
5572 OO
.ui
.GroupElement
.call( this, this.$element
, config
);
5575 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
5576 if ( $.isArray( config
.items
) ) {
5577 this.addItems( config
.items
);
5583 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
5584 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
5589 * @extends OO.ui.Widget
5590 * @mixins OO.ui.ButtonedElement
5591 * @mixins OO.ui.IconedElement
5592 * @mixins OO.ui.IndicatedElement
5593 * @mixins OO.ui.LabeledElement
5594 * @mixins OO.ui.TitledElement
5595 * @mixins OO.ui.FlaggableElement
5598 * @param {Object} [config] Configuration options
5599 * @cfg {string} [title=''] Title text
5600 * @cfg {string} [href] Hyperlink to visit when clicked
5601 * @cfg {string} [target] Target to open hyperlink in
5603 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
5604 // Configuration initialization
5605 config
= $.extend( { 'target': '_blank' }, config
);
5607 // Parent constructor
5608 OO
.ui
.ButtonWidget
.super.call( this, config
);
5610 // Mixin constructors
5611 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
5612 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
5613 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
5614 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
5615 OO
.ui
.TitledElement
.call( this, this.$button
, config
);
5616 OO
.ui
.FlaggableElement
.call( this, config
);
5619 this.isHyperlink
= typeof config
.href
=== 'string';
5623 'click': OO
.ui
.bind( this.onClick
, this ),
5624 'keypress': OO
.ui
.bind( this.onKeyPress
, this )
5629 .append( this.$icon
, this.$label
, this.$indicator
)
5630 .attr( { 'href': config
.href
, 'target': config
.target
} );
5632 .addClass( 'oo-ui-buttonWidget' )
5633 .append( this.$button
);
5638 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
5639 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonedElement
);
5640 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconedElement
);
5641 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatedElement
);
5642 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabeledElement
);
5643 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
5644 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggableElement
);
5655 * Handles mouse click events.
5657 * @param {jQuery.Event} e Mouse click event
5660 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
5661 if ( !this.isDisabled() ) {
5662 this.emit( 'click' );
5663 if ( this.isHyperlink
) {
5671 * Handles keypress events.
5673 * @param {jQuery.Event} e Keypress event
5676 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
5677 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
5679 if ( this.isHyperlink
) {
5690 * @extends OO.ui.Widget
5693 * @param {Object} [config] Configuration options
5694 * @cfg {string} [name=''] HTML input name
5695 * @cfg {string} [value=''] Input value
5696 * @cfg {boolean} [readOnly=false] Prevent changes
5697 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
5699 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
5700 // Config intialization
5701 config
= $.extend( { 'readOnly': false }, config
);
5703 // Parent constructor
5704 OO
.ui
.InputWidget
.super.call( this, config
);
5707 this.$input
= this.getInputElement( config
);
5709 this.readOnly
= false;
5710 this.inputFilter
= config
.inputFilter
;
5713 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
5717 .attr( 'name', config
.name
)
5718 .prop( 'disabled', this.isDisabled() );
5719 this.setReadOnly( config
.readOnly
);
5720 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
5721 this.setValue( config
.value
);
5726 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
5738 * Get input element.
5740 * @param {Object} [config] Configuration options
5741 * @return {jQuery} Input element
5743 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
5744 return this.$( '<input>' );
5748 * Handle potentially value-changing events.
5750 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
5752 OO
.ui
.InputWidget
.prototype.onEdit = function () {
5753 if ( !this.isDisabled() ) {
5754 // Allow the stack to clear so the value will be updated
5755 setTimeout( OO
.ui
.bind( function () {
5756 this.setValue( this.$input
.val() );
5762 * Get the value of the input.
5764 * @return {string} Input value
5766 OO
.ui
.InputWidget
.prototype.getValue = function () {
5771 * Sets the direction of the current input, either RTL or LTR
5773 * @param {boolean} isRTL
5775 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
5777 this.$input
.removeClass( 'oo-ui-ltr' );
5778 this.$input
.addClass( 'oo-ui-rtl' );
5780 this.$input
.removeClass( 'oo-ui-rtl' );
5781 this.$input
.addClass( 'oo-ui-ltr' );
5786 * Set the value of the input.
5788 * @param {string} value New value
5792 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
5793 value
= this.sanitizeValue( value
);
5794 if ( this.value
!== value
) {
5796 this.emit( 'change', this.value
);
5798 // Update the DOM if it has changed. Note that with sanitizeValue, it
5799 // is possible for the DOM value to change without this.value changing.
5800 if ( this.$input
.val() !== this.value
) {
5801 this.$input
.val( this.value
);
5807 * Sanitize incoming value.
5809 * Ensures value is a string, and converts undefined and null to empty strings.
5811 * @param {string} value Original value
5812 * @return {string} Sanitized value
5814 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
5815 if ( value
=== undefined || value
=== null ) {
5817 } else if ( this.inputFilter
) {
5818 return this.inputFilter( String( value
) );
5820 return String( value
);
5825 * Simulate the behavior of clicking on a label bound to this input.
5827 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
5828 if ( !this.isDisabled() ) {
5829 if ( this.$input
.is( ':checkbox,:radio' ) ) {
5830 this.$input
.click();
5831 } else if ( this.$input
.is( ':input' ) ) {
5832 this.$input
.focus();
5838 * Check if the widget is read-only.
5842 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
5843 return this.readOnly
;
5847 * Set the read-only state of the widget.
5849 * This should probably change the widgets's appearance and prevent it from being used.
5851 * @param {boolean} state Make input read-only
5854 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
5855 this.readOnly
= !!state
;
5856 this.$input
.prop( 'readOnly', this.readOnly
);
5863 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
5864 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
5865 if ( this.$input
) {
5866 this.$input
.prop( 'disabled', this.isDisabled() );
5876 OO
.ui
.InputWidget
.prototype.focus = function () {
5877 this.$input
.focus();
5884 * @extends OO.ui.InputWidget
5887 * @param {Object} [config] Configuration options
5889 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
5890 // Parent constructor
5891 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
5894 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
5899 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
5906 * Get input element.
5908 * @return {jQuery} Input element
5910 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
5911 return this.$( '<input type="checkbox" />' );
5915 * Get checked state of the checkbox
5917 * @return {boolean} If the checkbox is checked
5919 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
5926 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
5928 if ( this.value
!== value
) {
5930 this.$input
.prop( 'checked', this.value
);
5931 this.emit( 'change', this.value
);
5938 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
5939 if ( !this.isDisabled() ) {
5940 // Allow the stack to clear so the value will be updated
5941 setTimeout( OO
.ui
.bind( function () {
5942 this.setValue( this.$input
.prop( 'checked' ) );
5950 * @extends OO.ui.Widget
5951 * @mixins OO.ui.LabeledElement
5954 * @param {Object} [config] Configuration options
5956 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
5957 // Config intialization
5958 config
= config
|| {};
5960 // Parent constructor
5961 OO
.ui
.LabelWidget
.super.call( this, config
);
5963 // Mixin constructors
5964 OO
.ui
.LabeledElement
.call( this, this.$element
, config
);
5967 this.input
= config
.input
;
5970 if ( this.input
instanceof OO
.ui
.InputWidget
) {
5971 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
5975 this.$element
.addClass( 'oo-ui-labelWidget' );
5980 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
5981 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabeledElement
);
5983 /* Static Properties */
5985 OO
.ui
.LabelWidget
.static.tagName
= 'label';
5990 * Handles label mouse click events.
5992 * @param {jQuery.Event} e Mouse click event
5994 OO
.ui
.LabelWidget
.prototype.onClick = function () {
5995 this.input
.simulateLabelClick();
5999 * Lookup input widget.
6001 * Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
6002 * and `choose` events on #lookupMenu to make use of selections.
6008 * @param {OO.ui.TextInputWidget} input Input widget
6009 * @param {Object} [config] Configuration options
6010 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
6012 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
6013 // Config intialization
6014 config
= config
|| {};
6017 this.lookupInput
= input
;
6018 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
6019 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
6020 '$': OO
.ui
.Element
.getJQuery( this.$overlay
),
6021 'input': this.lookupInput
,
6022 '$container': config
.$container
6024 this.lookupCache
= {};
6025 this.lookupQuery
= null;
6026 this.lookupRequest
= null;
6027 this.populating
= false;
6030 this.$overlay
.append( this.lookupMenu
.$element
);
6032 this.lookupInput
.$input
.on( {
6033 'focus': OO
.ui
.bind( this.onLookupInputFocus
, this ),
6034 'blur': OO
.ui
.bind( this.onLookupInputBlur
, this ),
6035 'mousedown': OO
.ui
.bind( this.onLookupInputMouseDown
, this )
6037 this.lookupInput
.connect( this, { 'change': 'onLookupInputChange' } );
6040 this.$element
.addClass( 'oo-ui-lookupWidget' );
6041 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
6047 * Handle input focus event.
6049 * @param {jQuery.Event} e Input focus event
6051 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
6052 this.openLookupMenu();
6056 * Handle input blur event.
6058 * @param {jQuery.Event} e Input blur event
6060 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
6061 this.lookupMenu
.hide();
6065 * Handle input mouse down event.
6067 * @param {jQuery.Event} e Input mouse down event
6069 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
6070 this.openLookupMenu();
6074 * Handle input change event.
6076 * @param {string} value New input value
6078 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
6079 this.openLookupMenu();
6085 * @return {OO.ui.TextInputMenuWidget}
6087 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
6088 return this.lookupMenu
;
6096 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
6097 var value
= this.lookupInput
.getValue();
6099 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
6100 this.populateLookupMenu();
6101 if ( !this.lookupMenu
.isVisible() ) {
6102 this.lookupMenu
.show();
6105 this.lookupMenu
.clearItems();
6106 this.lookupMenu
.hide();
6113 * Populate lookup menu with current information.
6117 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
6118 if ( !this.populating
) {
6119 this.populating
= true;
6120 this.getLookupMenuItems()
6121 .done( OO
.ui
.bind( function ( items
) {
6122 this.lookupMenu
.clearItems();
6123 if ( items
.length
) {
6124 this.lookupMenu
.show();
6125 this.lookupMenu
.addItems( items
);
6126 this.initializeLookupMenuSelection();
6127 this.openLookupMenu();
6129 this.lookupMenu
.hide();
6131 this.populating
= false;
6133 .fail( OO
.ui
.bind( function () {
6134 this.lookupMenu
.clearItems();
6135 this.populating
= false;
6143 * Set selection in the lookup menu with current information.
6147 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
6148 if ( !this.lookupMenu
.getSelectedItem() ) {
6149 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
6151 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
6155 * Get lookup menu items for the current query.
6157 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
6160 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
6161 var value
= this.lookupInput
.getValue(),
6162 deferred
= $.Deferred();
6164 if ( value
&& value
!== this.lookupQuery
) {
6165 // Abort current request if query has changed
6166 if ( this.lookupRequest
) {
6167 this.lookupRequest
.abort();
6168 this.lookupQuery
= null;
6169 this.lookupRequest
= null;
6171 if ( value
in this.lookupCache
) {
6172 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6174 this.lookupQuery
= value
;
6175 this.lookupRequest
= this.getLookupRequest()
6176 .always( OO
.ui
.bind( function () {
6177 this.lookupQuery
= null;
6178 this.lookupRequest
= null;
6180 .done( OO
.ui
.bind( function ( data
) {
6181 this.lookupCache
[value
] = this.getLookupCacheItemFromData( data
);
6182 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
6184 .fail( function () {
6188 this.lookupRequest
.always( OO
.ui
.bind( function () {
6193 return deferred
.promise();
6197 * Get a new request object of the current lookup query value.
6200 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
6202 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
6203 // Stub, implemented in subclass
6208 * Handle successful lookup request.
6210 * Overriding methods should call #populateLookupMenu when results are available and cache results
6211 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
6214 * @param {Mixed} data Response from server
6216 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
6217 // Stub, implemented in subclass
6221 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
6224 * @param {Mixed} data Cached result data, usually an array
6225 * @return {OO.ui.MenuItemWidget[]} Menu items
6227 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
6228 // Stub, implemented in subclass
6234 * Use with OO.ui.SelectWidget.
6237 * @extends OO.ui.Widget
6238 * @mixins OO.ui.IconedElement
6239 * @mixins OO.ui.LabeledElement
6240 * @mixins OO.ui.IndicatedElement
6241 * @mixins OO.ui.FlaggableElement
6244 * @param {Mixed} data Option data
6245 * @param {Object} [config] Configuration options
6246 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
6248 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
6249 // Config intialization
6250 config
= config
|| {};
6252 // Parent constructor
6253 OO
.ui
.OptionWidget
.super.call( this, config
);
6255 // Mixin constructors
6256 OO
.ui
.ItemWidget
.call( this );
6257 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
6258 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
6259 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
6260 OO
.ui
.FlaggableElement
.call( this, config
);
6264 this.selected
= false;
6265 this.highlighted
= false;
6266 this.pressed
= false;
6270 .data( 'oo-ui-optionWidget', this )
6271 .attr( 'rel', config
.rel
)
6272 .addClass( 'oo-ui-optionWidget' )
6273 .append( this.$label
);
6275 .prepend( this.$icon
)
6276 .append( this.$indicator
);
6281 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
6282 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
6283 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconedElement
);
6284 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabeledElement
);
6285 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatedElement
);
6286 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggableElement
);
6288 /* Static Properties */
6290 OO
.ui
.OptionWidget
.static.tagName
= 'li';
6292 OO
.ui
.OptionWidget
.static.selectable
= true;
6294 OO
.ui
.OptionWidget
.static.highlightable
= true;
6296 OO
.ui
.OptionWidget
.static.pressable
= true;
6298 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
6303 * Check if option can be selected.
6305 * @return {boolean} Item is selectable
6307 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
6308 return this.constructor.static.selectable
&& !this.isDisabled();
6312 * Check if option can be highlighted.
6314 * @return {boolean} Item is highlightable
6316 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
6317 return this.constructor.static.highlightable
&& !this.isDisabled();
6321 * Check if option can be pressed.
6323 * @return {boolean} Item is pressable
6325 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
6326 return this.constructor.static.pressable
&& !this.isDisabled();
6330 * Check if option is selected.
6332 * @return {boolean} Item is selected
6334 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
6335 return this.selected
;
6339 * Check if option is highlighted.
6341 * @return {boolean} Item is highlighted
6343 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
6344 return this.highlighted
;
6348 * Check if option is pressed.
6350 * @return {boolean} Item is pressed
6352 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
6353 return this.pressed
;
6357 * Set selected state.
6359 * @param {boolean} [state=false] Select option
6362 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
6363 if ( this.constructor.static.selectable
) {
6364 this.selected
= !!state
;
6365 if ( this.selected
) {
6366 this.$element
.addClass( 'oo-ui-optionWidget-selected' );
6367 if ( this.constructor.static.scrollIntoViewOnSelect
) {
6368 this.scrollElementIntoView();
6371 this.$element
.removeClass( 'oo-ui-optionWidget-selected' );
6378 * Set highlighted state.
6380 * @param {boolean} [state=false] Highlight option
6383 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
6384 if ( this.constructor.static.highlightable
) {
6385 this.highlighted
= !!state
;
6386 if ( this.highlighted
) {
6387 this.$element
.addClass( 'oo-ui-optionWidget-highlighted' );
6389 this.$element
.removeClass( 'oo-ui-optionWidget-highlighted' );
6396 * Set pressed state.
6398 * @param {boolean} [state=false] Press option
6401 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
6402 if ( this.constructor.static.pressable
) {
6403 this.pressed
= !!state
;
6404 if ( this.pressed
) {
6405 this.$element
.addClass( 'oo-ui-optionWidget-pressed' );
6407 this.$element
.removeClass( 'oo-ui-optionWidget-pressed' );
6414 * Make the option's highlight flash.
6416 * While flashing, the visual style of the pressed state is removed if present.
6418 * @return {jQuery.Promise} Promise resolved when flashing is done
6420 OO
.ui
.OptionWidget
.prototype.flash = function () {
6421 var $this = this.$element
,
6422 deferred
= $.Deferred();
6424 if ( !this.isDisabled() && this.constructor.static.pressable
) {
6425 $this.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
6426 setTimeout( OO
.ui
.bind( function () {
6427 // Restore original classes
6429 .toggleClass( 'oo-ui-optionWidget-highlighted', this.highlighted
)
6430 .toggleClass( 'oo-ui-optionWidget-pressed', this.pressed
);
6431 setTimeout( function () {
6437 return deferred
.promise();
6443 * @return {Mixed} Option data
6445 OO
.ui
.OptionWidget
.prototype.getData = function () {
6449 * Selection of options.
6451 * Use together with OO.ui.OptionWidget.
6454 * @extends OO.ui.Widget
6455 * @mixins OO.ui.GroupElement
6458 * @param {Object} [config] Configuration options
6459 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
6461 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
6462 // Config intialization
6463 config
= config
|| {};
6465 // Parent constructor
6466 OO
.ui
.SelectWidget
.super.call( this, config
);
6468 // Mixin constructors
6469 OO
.ui
.GroupWidget
.call( this, this.$element
, config
);
6472 this.pressed
= false;
6473 this.selecting
= null;
6475 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
6476 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
6480 'mousedown': OO
.ui
.bind( this.onMouseDown
, this ),
6481 'mouseover': OO
.ui
.bind( this.onMouseOver
, this ),
6482 'mouseleave': OO
.ui
.bind( this.onMouseLeave
, this )
6486 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
6487 if ( $.isArray( config
.items
) ) {
6488 this.addItems( config
.items
);
6494 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
6496 // Need to mixin base class as well
6497 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
6498 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
6504 * @param {OO.ui.OptionWidget|null} item Highlighted item
6509 * @param {OO.ui.OptionWidget|null} item Pressed item
6514 * @param {OO.ui.OptionWidget|null} item Selected item
6519 * @param {OO.ui.OptionWidget|null} item Chosen item
6524 * @param {OO.ui.OptionWidget[]} items Added items
6525 * @param {number} index Index items were added at
6530 * @param {OO.ui.OptionWidget[]} items Removed items
6533 /* Static Properties */
6535 OO
.ui
.SelectWidget
.static.tagName
= 'ul';
6540 * Handle mouse down events.
6543 * @param {jQuery.Event} e Mouse down event
6545 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
6548 if ( !this.isDisabled() && e
.which
=== 1 ) {
6549 this.togglePressed( true );
6550 item
= this.getTargetItem( e
);
6551 if ( item
&& item
.isSelectable() ) {
6552 this.pressItem( item
);
6553 this.selecting
= item
;
6554 this.getElementDocument().addEventListener(
6555 'mouseup', this.onMouseUpHandler
, true
6557 this.getElementDocument().addEventListener(
6558 'mousemove', this.onMouseMoveHandler
, true
6566 * Handle mouse up events.
6569 * @param {jQuery.Event} e Mouse up event
6571 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
6574 this.togglePressed( false );
6575 if ( !this.selecting
) {
6576 item
= this.getTargetItem( e
);
6577 if ( item
&& item
.isSelectable() ) {
6578 this.selecting
= item
;
6581 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
6582 this.pressItem( null );
6583 this.chooseItem( this.selecting
);
6584 this.selecting
= null;
6587 this.getElementDocument().removeEventListener(
6588 'mouseup', this.onMouseUpHandler
, true
6590 this.getElementDocument().removeEventListener(
6591 'mousemove', this.onMouseMoveHandler
, true
6598 * Handle mouse move events.
6601 * @param {jQuery.Event} e Mouse move event
6603 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
6606 if ( !this.isDisabled() && this.pressed
) {
6607 item
= this.getTargetItem( e
);
6608 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
6609 this.pressItem( item
);
6610 this.selecting
= item
;
6617 * Handle mouse over events.
6620 * @param {jQuery.Event} e Mouse over event
6622 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
6625 if ( !this.isDisabled() ) {
6626 item
= this.getTargetItem( e
);
6627 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
6633 * Handle mouse leave events.
6636 * @param {jQuery.Event} e Mouse over event
6638 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
6639 if ( !this.isDisabled() ) {
6640 this.highlightItem( null );
6646 * Get the closest item to a jQuery.Event.
6649 * @param {jQuery.Event} e
6650 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
6652 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
6653 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
6654 if ( $item
.length
) {
6655 return $item
.data( 'oo-ui-optionWidget' );
6661 * Get selected item.
6663 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
6665 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
6668 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6669 if ( this.items
[i
].isSelected() ) {
6670 return this.items
[i
];
6677 * Get highlighted item.
6679 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
6681 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
6684 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6685 if ( this.items
[i
].isHighlighted() ) {
6686 return this.items
[i
];
6693 * Get an existing item with equivilant data.
6695 * @param {Object} data Item data to search for
6696 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
6698 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
6699 var hash
= OO
.getHash( data
);
6701 if ( hash
in this.hashes
) {
6702 return this.hashes
[hash
];
6709 * Toggle pressed state.
6711 * @param {boolean} pressed An option is being pressed
6713 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
6714 if ( pressed
=== undefined ) {
6715 pressed
= !this.pressed
;
6717 if ( pressed
!== this.pressed
) {
6718 this.$element
.toggleClass( 'oo-ui-selectWidget-pressed', pressed
);
6719 this.$element
.toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
6720 this.pressed
= pressed
;
6725 * Highlight an item.
6727 * Highlighting is mutually exclusive.
6729 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
6733 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
6734 var i
, len
, highlighted
,
6737 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6738 highlighted
= this.items
[i
] === item
;
6739 if ( this.items
[i
].isHighlighted() !== highlighted
) {
6740 this.items
[i
].setHighlighted( highlighted
);
6745 this.emit( 'highlight', item
);
6754 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6758 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
6759 var i
, len
, selected
,
6762 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6763 selected
= this.items
[i
] === item
;
6764 if ( this.items
[i
].isSelected() !== selected
) {
6765 this.items
[i
].setSelected( selected
);
6770 this.emit( 'select', item
);
6779 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
6783 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
6784 var i
, len
, pressed
,
6787 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6788 pressed
= this.items
[i
] === item
;
6789 if ( this.items
[i
].isPressed() !== pressed
) {
6790 this.items
[i
].setPressed( pressed
);
6795 this.emit( 'press', item
);
6804 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
6805 * an item is selected using the keyboard or mouse.
6807 * @param {OO.ui.OptionWidget} item Item to choose
6811 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
6812 this.selectItem( item
);
6813 this.emit( 'choose', item
);
6819 * Get an item relative to another one.
6821 * @param {OO.ui.OptionWidget} item Item to start at
6822 * @param {number} direction Direction to move in
6823 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
6825 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
6826 var inc
= direction
> 0 ? 1 : -1,
6827 len
= this.items
.length
,
6828 index
= item
instanceof OO
.ui
.OptionWidget
?
6829 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
6830 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
6832 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
6833 Math
.max( index
, -1 ) :
6834 // Default to n-1 instead of -1, if nothing is selected let's start at the end
6835 Math
.min( index
, len
);
6838 i
= ( i
+ inc
+ len
) % len
;
6839 item
= this.items
[i
];
6840 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6843 // Stop iterating when we've looped all the way around
6844 if ( i
=== stopAt
) {
6852 * Get the next selectable item.
6854 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
6856 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
6859 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6860 item
= this.items
[i
];
6861 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
6872 * When items are added with the same values as existing items, the existing items will be
6873 * automatically removed before the new items are added.
6875 * @param {OO.ui.OptionWidget[]} items Items to add
6876 * @param {number} [index] Index to insert items after
6880 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
6881 var i
, len
, item
, hash
,
6884 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6886 hash
= OO
.getHash( item
.getData() );
6887 if ( hash
in this.hashes
) {
6888 // Remove item with same value
6889 remove
.push( this.hashes
[hash
] );
6891 this.hashes
[hash
] = item
;
6893 if ( remove
.length
) {
6894 this.removeItems( remove
);
6898 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
6900 // Always provide an index, even if it was omitted
6901 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
6909 * Items will be detached, not removed, so they can be used later.
6911 * @param {OO.ui.OptionWidget[]} items Items to remove
6915 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
6916 var i
, len
, item
, hash
;
6918 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6920 hash
= OO
.getHash( item
.getData() );
6921 if ( hash
in this.hashes
) {
6922 // Remove existing item
6923 delete this.hashes
[hash
];
6925 if ( item
.isSelected() ) {
6926 this.selectItem( null );
6931 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
6933 this.emit( 'remove', items
);
6941 * Items will be detached, not removed, so they can be used later.
6946 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
6947 var items
= this.items
.slice();
6952 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
6953 this.selectItem( null );
6955 this.emit( 'remove', items
);
6962 * Use with OO.ui.MenuWidget.
6965 * @extends OO.ui.OptionWidget
6968 * @param {Mixed} data Item data
6969 * @param {Object} [config] Configuration options
6971 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
6972 // Configuration initialization
6973 config
= $.extend( { 'icon': 'check' }, config
);
6975 // Parent constructor
6976 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
6979 this.$element
.addClass( 'oo-ui-menuItemWidget' );
6984 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.OptionWidget
);
6988 * Use together with OO.ui.MenuItemWidget.
6991 * @extends OO.ui.SelectWidget
6992 * @mixins OO.ui.ClippableElement
6995 * @param {Object} [config] Configuration options
6996 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
6997 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
6999 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
7000 // Config intialization
7001 config
= config
|| {};
7003 // Parent constructor
7004 OO
.ui
.MenuWidget
.super.call( this, config
);
7006 // Mixin constructors
7007 OO
.ui
.ClippableElement
.call( this, this.$group
, config
);
7010 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
7011 this.newItems
= null;
7012 this.$input
= config
.input
? config
.input
.$input
: null;
7013 this.$previousFocus
= null;
7014 this.isolated
= !config
.input
;
7015 this.visible
= false;
7016 this.flashing
= false;
7017 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
7018 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
7021 this.$element
.hide().addClass( 'oo-ui-menuWidget' );
7026 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
7027 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
7032 * Handles document mouse down events.
7034 * @param {jQuery.Event} e Key down event
7036 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
7037 if ( !$.contains( this.$element
[0], e
.target
) ) {
7043 * Handles key down events.
7045 * @param {jQuery.Event} e Key down event
7047 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
7050 highlightItem
= this.getHighlightedItem();
7052 if ( !this.isDisabled() && this.visible
) {
7053 if ( !highlightItem
) {
7054 highlightItem
= this.getSelectedItem();
7056 switch ( e
.keyCode
) {
7057 case OO
.ui
.Keys
.ENTER
:
7058 this.chooseItem( highlightItem
);
7062 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
7065 case OO
.ui
.Keys
.DOWN
:
7066 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
7069 case OO
.ui
.Keys
.ESCAPE
:
7070 if ( highlightItem
) {
7071 highlightItem
.setHighlighted( false );
7079 this.highlightItem( nextItem
);
7080 nextItem
.scrollElementIntoView();
7085 e
.stopPropagation();
7092 * Check if the menu is visible.
7094 * @return {boolean} Menu is visible
7096 OO
.ui
.MenuWidget
.prototype.isVisible = function () {
7097 return this.visible
;
7101 * Bind key down listener.
7103 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
7104 if ( this.$input
) {
7105 this.$input
.on( 'keydown', this.onKeyDownHandler
);
7107 // Capture menu navigation keys
7108 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
7113 * Unbind key down listener.
7115 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
7116 if ( this.$input
) {
7117 this.$input
.off( 'keydown' );
7119 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
7126 * This will close the menu when done, unlike selectItem which only changes selection.
7128 * @param {OO.ui.OptionWidget} item Item to choose
7131 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
7133 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
7135 if ( item
&& !this.flashing
) {
7136 this.flashing
= true;
7137 item
.flash().done( OO
.ui
.bind( function () {
7139 this.flashing
= false;
7151 * Adding an existing item (by value) will move it.
7153 * @param {OO.ui.MenuItemWidget[]} items Items to add
7154 * @param {number} [index] Index to insert items after
7157 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
7161 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
7164 if ( !this.newItems
) {
7168 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
7170 if ( this.visible
) {
7171 // Defer fitting label until
7174 this.newItems
.push( item
);
7186 OO
.ui
.MenuWidget
.prototype.show = function () {
7189 if ( this.items
.length
) {
7190 this.$element
.show();
7191 this.visible
= true;
7192 this.bindKeyDownListener();
7194 // Change focus to enable keyboard navigation
7195 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
7196 this.$previousFocus
= this.$( ':focus' );
7197 this.$input
.focus();
7199 if ( this.newItems
&& this.newItems
.length
) {
7200 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
7201 this.newItems
[i
].fitLabel();
7203 this.newItems
= null;
7206 this.setClipping( true );
7209 if ( this.autoHide
) {
7210 this.getElementDocument().addEventListener(
7211 'mousedown', this.onDocumentMouseDownHandler
, true
7224 OO
.ui
.MenuWidget
.prototype.hide = function () {
7225 this.$element
.hide();
7226 this.visible
= false;
7227 this.unbindKeyDownListener();
7229 if ( this.isolated
&& this.$previousFocus
) {
7230 this.$previousFocus
.focus();
7231 this.$previousFocus
= null;
7234 this.getElementDocument().removeEventListener(
7235 'mousedown', this.onDocumentMouseDownHandler
, true
7238 this.setClipping( false );
7243 * Inline menu of options.
7245 * Use with OO.ui.MenuOptionWidget.
7248 * @extends OO.ui.Widget
7249 * @mixins OO.ui.IconedElement
7250 * @mixins OO.ui.IndicatedElement
7251 * @mixins OO.ui.LabeledElement
7252 * @mixins OO.ui.TitledElement
7255 * @param {Object} [config] Configuration options
7256 * @cfg {Object} [menu] Configuration options to pass to menu widget
7258 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
7259 // Configuration initialization
7260 config
= $.extend( { 'indicator': 'down' }, config
);
7262 // Parent constructor
7263 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
7265 // Mixin constructors
7266 OO
.ui
.IconedElement
.call( this, this.$( '<span>' ), config
);
7267 OO
.ui
.IndicatedElement
.call( this, this.$( '<span>' ), config
);
7268 OO
.ui
.LabeledElement
.call( this, this.$( '<span>' ), config
);
7269 OO
.ui
.TitledElement
.call( this, this.$label
, config
);
7272 this.menu
= new OO
.ui
.MenuWidget( $.extend( { '$': this.$ }, config
.menu
) );
7273 this.$handle
= this.$( '<span>' );
7276 this.$element
.on( { 'click': OO
.ui
.bind( this.onClick
, this ) } );
7277 this.menu
.connect( this, { 'select': 'onMenuSelect' } );
7281 .addClass( 'oo-ui-inlineMenuWidget-handle' )
7282 .append( this.$icon
, this.$label
, this.$indicator
);
7284 .addClass( 'oo-ui-inlineMenuWidget' )
7285 .append( this.$handle
, this.menu
.$element
);
7290 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
7291 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconedElement
);
7292 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatedElement
);
7293 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabeledElement
);
7294 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
7301 * @return {OO.ui.MenuWidget} Menu of widget
7303 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
7308 * Handles menu select events.
7310 * @param {OO.ui.MenuItemWidget} item Selected menu item
7312 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
7319 selectedLabel
= item
.getLabel();
7321 // If the label is a DOM element, clone it, because setLabel will append() it
7322 if ( selectedLabel
instanceof jQuery
) {
7323 selectedLabel
= selectedLabel
.clone();
7326 this.setLabel( selectedLabel
);
7330 * Handles mouse click events.
7332 * @param {jQuery.Event} e Mouse click event
7334 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
7335 // Skip clicks within the menu
7336 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
7340 if ( !this.isDisabled() ) {
7341 if ( this.menu
.isVisible() ) {
7350 * Menu section item widget.
7352 * Use with OO.ui.MenuWidget.
7355 * @extends OO.ui.OptionWidget
7358 * @param {Mixed} data Item data
7359 * @param {Object} [config] Configuration options
7361 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
7362 // Parent constructor
7363 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
7366 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
7371 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.OptionWidget
);
7373 /* Static Properties */
7375 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
7377 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
7379 * Create an OO.ui.OutlineWidget object.
7381 * Use with OO.ui.OutlineItemWidget.
7384 * @extends OO.ui.SelectWidget
7387 * @param {Object} [config] Configuration options
7389 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
7390 // Config intialization
7391 config
= config
|| {};
7393 // Parent constructor
7394 OO
.ui
.OutlineWidget
.super.call( this, config
);
7397 this.$element
.addClass( 'oo-ui-outlineWidget' );
7402 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
7404 * Creates an OO.ui.OutlineControlsWidget object.
7406 * Use together with OO.ui.OutlineWidget.js
7411 * @param {OO.ui.OutlineWidget} outline Outline to control
7412 * @param {Object} [config] Configuration options
7414 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7415 // Configuration initialization
7416 config
= $.extend( { 'icon': 'add-item' }, config
);
7418 // Parent constructor
7419 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7421 // Mixin constructors
7422 OO
.ui
.GroupElement
.call( this, this.$( '<div>' ), config
);
7423 OO
.ui
.IconedElement
.call( this, this.$( '<div>' ), config
);
7426 this.outline
= outline
;
7427 this.$movers
= this.$( '<div>' );
7428 this.upButton
= new OO
.ui
.ButtonWidget( {
7432 'title': OO
.ui
.msg( 'ooui-outline-control-move-up' )
7434 this.downButton
= new OO
.ui
.ButtonWidget( {
7438 'title': OO
.ui
.msg( 'ooui-outline-control-move-down' )
7440 this.removeButton
= new OO
.ui
.ButtonWidget( {
7444 'title': OO
.ui
.msg( 'ooui-outline-control-remove' )
7448 outline
.connect( this, {
7449 'select': 'onOutlineChange',
7450 'add': 'onOutlineChange',
7451 'remove': 'onOutlineChange'
7453 this.upButton
.connect( this, { 'click': [ 'emit', 'move', -1 ] } );
7454 this.downButton
.connect( this, { 'click': [ 'emit', 'move', 1 ] } );
7455 this.removeButton
.connect( this, { 'click': [ 'emit', 'remove' ] } );
7458 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7459 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7461 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7462 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7463 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7468 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7469 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7470 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconedElement
);
7476 * @param {number} places Number of places to move
7486 * Handle outline change events.
7488 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7489 var i
, len
, firstMovable
, lastMovable
,
7490 items
= this.outline
.getItems(),
7491 selectedItem
= this.outline
.getSelectedItem(),
7492 movable
= selectedItem
&& selectedItem
.isMovable(),
7493 removable
= selectedItem
&& selectedItem
.isRemovable();
7498 while ( ++i
< len
) {
7499 if ( items
[i
].isMovable() ) {
7500 firstMovable
= items
[i
];
7506 if ( items
[i
].isMovable() ) {
7507 lastMovable
= items
[i
];
7512 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7513 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7514 this.removeButton
.setDisabled( !removable
);
7517 * Creates an OO.ui.OutlineItemWidget object.
7519 * Use with OO.ui.OutlineWidget.
7522 * @extends OO.ui.OptionWidget
7525 * @param {Mixed} data Item data
7526 * @param {Object} [config] Configuration options
7527 * @cfg {number} [level] Indentation level
7528 * @cfg {boolean} [movable] Allow modification from outline controls
7530 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
7531 // Config intialization
7532 config
= config
|| {};
7534 // Parent constructor
7535 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
7539 this.movable
= !!config
.movable
;
7540 this.removable
= !!config
.removable
;
7543 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
7544 this.setLevel( config
.level
);
7549 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.OptionWidget
);
7551 /* Static Properties */
7553 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
7555 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
7557 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
7559 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
7564 * Check if item is movable.
7566 * Movablilty is used by outline controls.
7568 * @return {boolean} Item is movable
7570 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
7571 return this.movable
;
7575 * Check if item is removable.
7577 * Removablilty is used by outline controls.
7579 * @return {boolean} Item is removable
7581 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
7582 return this.removable
;
7586 * Get indentation level.
7588 * @return {number} Indentation level
7590 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
7597 * Movablilty is used by outline controls.
7599 * @param {boolean} movable Item is movable
7602 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
7603 this.movable
= !!movable
;
7610 * Removablilty is used by outline controls.
7612 * @param {boolean} movable Item is removable
7615 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
7616 this.removable
= !!removable
;
7621 * Set indentation level.
7623 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
7626 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
7627 var levels
= this.constructor.static.levels
,
7628 levelClass
= this.constructor.static.levelClass
,
7631 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
7633 if ( this.level
=== i
) {
7634 this.$element
.addClass( levelClass
+ i
);
7636 this.$element
.removeClass( levelClass
+ i
);
7643 * Option widget that looks like a button.
7645 * Use together with OO.ui.ButtonSelectWidget.
7648 * @extends OO.ui.OptionWidget
7649 * @mixins OO.ui.ButtonedElement
7650 * @mixins OO.ui.FlaggableElement
7653 * @param {Mixed} data Option data
7654 * @param {Object} [config] Configuration options
7656 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
7657 // Parent constructor
7658 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
7660 // Mixin constructors
7661 OO
.ui
.ButtonedElement
.call( this, this.$( '<a>' ), config
);
7662 OO
.ui
.FlaggableElement
.call( this, config
);
7665 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
7666 this.$button
.append( this.$element
.contents() );
7667 this.$element
.append( this.$button
);
7672 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
7673 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonedElement
);
7674 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.FlaggableElement
);
7676 /* Static Properties */
7678 // Allow button mouse down events to pass through so they can be handled by the parent select widget
7679 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
7686 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
7687 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
7689 if ( this.constructor.static.selectable
) {
7690 this.setActive( state
);
7696 * Select widget containing button options.
7698 * Use together with OO.ui.ButtonOptionWidget.
7701 * @extends OO.ui.SelectWidget
7704 * @param {Object} [config] Configuration options
7706 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
7707 // Parent constructor
7708 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
7711 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
7716 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
7718 * Container for content that is overlaid and positioned absolutely.
7721 * @extends OO.ui.Widget
7722 * @mixins OO.ui.LabeledElement
7725 * @param {Object} [config] Configuration options
7726 * @cfg {boolean} [tail=true] Show tail pointing to origin of popup
7727 * @cfg {string} [align='center'] Alignment of popup to origin
7728 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
7729 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
7730 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
7731 * @cfg {boolean} [head] Show label and close button at the top
7733 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
7734 // Config intialization
7735 config
= config
|| {};
7737 // Parent constructor
7738 OO
.ui
.PopupWidget
.super.call( this, config
);
7740 // Mixin constructors
7741 OO
.ui
.LabeledElement
.call( this, this.$( '<div>' ), config
);
7742 OO
.ui
.ClippableElement
.call( this, this.$( '<div>' ), config
);
7745 this.visible
= false;
7746 this.$popup
= this.$( '<div>' );
7747 this.$head
= this.$( '<div>' );
7748 this.$body
= this.$clippable
;
7749 this.$tail
= this.$( '<div>' );
7750 this.$container
= config
.$container
|| this.$( 'body' );
7751 this.autoClose
= !!config
.autoClose
;
7752 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
7753 this.transitionTimeout
= null;
7755 this.align
= config
.align
|| 'center';
7756 this.closeButton
= new OO
.ui
.ButtonWidget( { '$': this.$, 'frameless': true, 'icon': 'close' } );
7757 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
7760 this.closeButton
.connect( this, { 'click': 'onCloseButtonClick' } );
7763 this.useTail( config
.tail
!== undefined ? !!config
.tail
: true );
7764 this.$body
.addClass( 'oo-ui-popupWidget-body' );
7765 this.$tail
.addClass( 'oo-ui-popupWidget-tail' );
7767 .addClass( 'oo-ui-popupWidget-head' )
7768 .append( this.$label
, this.closeButton
.$element
);
7769 if ( !config
.head
) {
7773 .addClass( 'oo-ui-popupWidget-popup' )
7774 .append( this.$head
, this.$body
);
7775 this.$element
.hide()
7776 .addClass( 'oo-ui-popupWidget' )
7777 .append( this.$popup
, this.$tail
);
7782 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
7783 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabeledElement
);
7784 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
7799 * Handles mouse down events.
7801 * @param {jQuery.Event} e Mouse down event
7803 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
7806 !$.contains( this.$element
[0], e
.target
) &&
7807 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
7814 * Bind mouse down listener.
7816 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
7817 // Capture clicks outside popup
7818 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
7822 * Handles close button click events.
7824 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
7825 if ( this.visible
) {
7831 * Unbind mouse down listener.
7833 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
7834 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
7838 * Check if the popup is visible.
7840 * @return {boolean} Popup is visible
7842 OO
.ui
.PopupWidget
.prototype.isVisible = function () {
7843 return this.visible
;
7847 * Set whether to show a tail.
7849 * @return {boolean} Make tail visible
7851 OO
.ui
.PopupWidget
.prototype.useTail = function ( value
) {
7853 if ( this.tail
!== value
) {
7856 this.$element
.addClass( 'oo-ui-popupWidget-tailed' );
7858 this.$element
.removeClass( 'oo-ui-popupWidget-tailed' );
7864 * Check if showing a tail.
7866 * @return {boolean} tail is visible
7868 OO
.ui
.PopupWidget
.prototype.hasTail = function () {
7878 OO
.ui
.PopupWidget
.prototype.show = function () {
7879 if ( !this.visible
) {
7880 this.setClipping( true );
7881 this.$element
.show();
7882 this.visible
= true;
7883 this.emit( 'show' );
7884 if ( this.autoClose
) {
7885 this.bindMouseDownListener();
7897 OO
.ui
.PopupWidget
.prototype.hide = function () {
7898 if ( this.visible
) {
7899 this.setClipping( false );
7900 this.$element
.hide();
7901 this.visible
= false;
7902 this.emit( 'hide' );
7903 if ( this.autoClose
) {
7904 this.unbindMouseDownListener();
7911 * Updates the position and size.
7913 * @param {number} width Width
7914 * @param {number} height Height
7915 * @param {boolean} [transition=false] Use a smooth transition
7918 OO
.ui
.PopupWidget
.prototype.display = function ( width
, height
, transition
) {
7920 originOffset
= Math
.round( this.$element
.offset().left
),
7921 containerLeft
= Math
.round( this.$container
.offset().left
),
7922 containerWidth
= this.$container
.innerWidth(),
7923 containerRight
= containerLeft
+ containerWidth
,
7924 popupOffset
= width
* ( { 'left': 0, 'center': -0.5, 'right': -1 } )[this.align
],
7925 popupLeft
= popupOffset
- padding
,
7926 popupRight
= popupOffset
+ padding
+ width
+ padding
,
7927 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
7928 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
7930 // Prevent transition from being interrupted
7931 clearTimeout( this.transitionTimeout
);
7933 // Enable transition
7934 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
7937 if ( overlapRight
< 0 ) {
7938 popupOffset
+= overlapRight
;
7939 } else if ( overlapLeft
< 0 ) {
7940 popupOffset
-= overlapLeft
;
7943 // Position body relative to anchor and resize
7945 'left': popupOffset
,
7947 'height': height
=== undefined ? 'auto' : height
7951 // Prevent transitioning after transition is complete
7952 this.transitionTimeout
= setTimeout( OO
.ui
.bind( function () {
7953 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7956 // Prevent transitioning immediately
7957 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
7963 * Button that shows and hides a popup.
7966 * @extends OO.ui.ButtonWidget
7967 * @mixins OO.ui.PopuppableElement
7970 * @param {Object} [config] Configuration options
7972 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
7973 // Parent constructor
7974 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
7976 // Mixin constructors
7977 OO
.ui
.PopuppableElement
.call( this, config
);
7981 .addClass( 'oo-ui-popupButtonWidget' )
7982 .append( this.popup
.$element
);
7987 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
7988 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopuppableElement
);
7993 * Handles mouse click events.
7995 * @param {jQuery.Event} e Mouse click event
7997 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
7998 // Skip clicks within the popup
7999 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8003 if ( !this.isDisabled() ) {
8004 if ( this.popup
.isVisible() ) {
8009 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8016 * Combines query and results selection widgets.
8019 * @extends OO.ui.Widget
8022 * @param {Object} [config] Configuration options
8023 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
8024 * @cfg {string} [value] Initial query value
8026 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
8027 // Configuration intialization
8028 config
= config
|| {};
8030 // Parent constructor
8031 OO
.ui
.SearchWidget
.super.call( this, config
);
8034 this.query
= new OO
.ui
.TextInputWidget( {
8037 'placeholder': config
.placeholder
,
8038 'value': config
.value
8040 this.results
= new OO
.ui
.SelectWidget( { '$': this.$ } );
8041 this.$query
= this.$( '<div>' );
8042 this.$results
= this.$( '<div>' );
8045 this.query
.connect( this, {
8046 'change': 'onQueryChange',
8047 'enter': 'onQueryEnter'
8049 this.results
.connect( this, {
8050 'highlight': 'onResultsHighlight',
8051 'select': 'onResultsSelect'
8053 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
8057 .addClass( 'oo-ui-searchWidget-query' )
8058 .append( this.query
.$element
);
8060 .addClass( 'oo-ui-searchWidget-results' )
8061 .append( this.results
.$element
);
8063 .addClass( 'oo-ui-searchWidget' )
8064 .append( this.$results
, this.$query
);
8069 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
8075 * @param {Object|null} item Item data or null if no item is highlighted
8080 * @param {Object|null} item Item data or null if no item is selected
8086 * Handle query key down events.
8088 * @param {jQuery.Event} e Key down event
8090 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
8091 var highlightedItem
, nextItem
,
8092 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
8095 highlightedItem
= this.results
.getHighlightedItem();
8096 if ( !highlightedItem
) {
8097 highlightedItem
= this.results
.getSelectedItem();
8099 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
8100 this.results
.highlightItem( nextItem
);
8101 nextItem
.scrollElementIntoView();
8106 * Handle select widget select events.
8108 * Clears existing results. Subclasses should repopulate items according to new query.
8110 * @param {string} value New value
8112 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
8114 this.results
.clearItems();
8118 * Handle select widget enter key events.
8120 * Selects highlighted item.
8122 * @param {string} value New value
8124 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
8126 this.results
.selectItem( this.results
.getHighlightedItem() );
8130 * Handle select widget highlight events.
8132 * @param {OO.ui.OptionWidget} item Highlighted item
8135 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
8136 this.emit( 'highlight', item
? item
.getData() : null );
8140 * Handle select widget select events.
8142 * @param {OO.ui.OptionWidget} item Selected item
8145 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
8146 this.emit( 'select', item
? item
.getData() : null );
8150 * Get the query input.
8152 * @return {OO.ui.TextInputWidget} Query input
8154 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
8159 * Get the results list.
8161 * @return {OO.ui.SelectWidget} Select list
8163 OO
.ui
.SearchWidget
.prototype.getResults = function () {
8164 return this.results
;
8167 * Text input widget.
8170 * @extends OO.ui.InputWidget
8173 * @param {Object} [config] Configuration options
8174 * @cfg {string} [placeholder] Placeholder text
8175 * @cfg {string} [icon] Symbolic name of icon
8176 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8177 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8178 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8180 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8181 config
= $.extend( { 'maxRows': 10 }, config
);
8183 // Parent constructor
8184 OO
.ui
.TextInputWidget
.super.call( this, config
);
8188 this.multiline
= !!config
.multiline
;
8189 this.autosize
= !!config
.autosize
;
8190 this.maxRows
= config
.maxRows
;
8193 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8194 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8197 this.$element
.addClass( 'oo-ui-textInputWidget' );
8198 if ( config
.icon
) {
8199 this.$element
.addClass( 'oo-ui-textInputWidget-decorated' );
8200 this.$element
.append(
8202 .addClass( 'oo-ui-textInputWidget-icon oo-ui-icon-' + config
.icon
)
8203 .mousedown( OO
.ui
.bind( function () {
8204 this.$input
.focus();
8209 if ( config
.placeholder
) {
8210 this.$input
.attr( 'placeholder', config
.placeholder
);
8216 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8221 * User presses enter inside the text box.
8223 * Not called if input is multiline.
8231 * Handle key press events.
8233 * @param {jQuery.Event} e Key press event
8234 * @fires enter If enter key is pressed and input is not multiline
8236 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8237 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8238 this.emit( 'enter' );
8243 * Handle element attach events.
8245 * @param {jQuery.Event} e Element attach event
8247 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8254 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8258 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
8262 * Automatically adjust the size of the text input.
8264 * This only affects multi-line inputs that are auto-sized.
8268 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
8269 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
8271 if ( this.multiline
&& this.autosize
) {
8272 $clone
= this.$input
.clone()
8273 .val( this.$input
.val() )
8274 .css( { 'height': 0 } )
8275 .insertAfter( this.$input
);
8276 // Set inline height property to 0 to measure scroll height
8277 scrollHeight
= $clone
[0].scrollHeight
;
8278 // Remove inline height property to measure natural heights
8279 $clone
.css( 'height', '' );
8280 innerHeight
= $clone
.innerHeight();
8281 outerHeight
= $clone
.outerHeight();
8282 // Measure max rows height
8283 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
8284 maxInnerHeight
= $clone
.innerHeight();
8285 $clone
.removeAttr( 'rows' ).css( 'height', '' );
8287 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
8288 // Only apply inline height when expansion beyond natural height is needed
8291 // Use the difference between the inner and outer height as a buffer
8292 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
8299 * Get input element.
8301 * @param {Object} [config] Configuration options
8302 * @return {jQuery} Input element
8304 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
8305 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
8311 * Check if input supports multiple lines.
8315 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
8316 return !!this.multiline
;
8320 * Check if input automatically adjusts its size.
8324 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
8325 return !!this.autosize
;
8329 * Check if input is pending.
8333 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
8334 return !!this.pending
;
8338 * Increase the pending stack.
8342 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
8343 if ( this.pending
=== 0 ) {
8344 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
8345 this.$input
.addClass( 'oo-ui-texture-pending' );
8353 * Reduce the pending stack.
8359 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
8360 if ( this.pending
=== 1 ) {
8361 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
8362 this.$input
.removeClass( 'oo-ui-texture-pending' );
8364 this.pending
= Math
.max( 0, this.pending
- 1 );
8370 * Select the contents of the input.
8374 OO
.ui
.TextInputWidget
.prototype.select = function () {
8375 this.$input
.select();
8379 * Menu for a text input widget.
8382 * @extends OO.ui.MenuWidget
8385 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
8386 * @param {Object} [config] Configuration options
8387 * @cfg {jQuery} [$container=input.$element] Element to render menu under
8389 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
8390 // Parent constructor
8391 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
8395 this.$container
= config
.$container
|| this.input
.$element
;
8396 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
8399 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
8404 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
8409 * Handle window resize event.
8411 * @param {jQuery.Event} e Window resize event
8413 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
8422 OO
.ui
.TextInputMenuWidget
.prototype.show = function () {
8424 OO
.ui
.TextInputMenuWidget
.super.prototype.show
.call( this );
8427 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
8436 OO
.ui
.TextInputMenuWidget
.prototype.hide = function () {
8438 OO
.ui
.TextInputMenuWidget
.super.prototype.hide
.call( this );
8440 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
8445 * Position the menu.
8449 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
8451 $container
= this.$container
,
8452 dimensions
= $container
.offset();
8454 // Position under input
8455 dimensions
.top
+= $container
.height();
8457 // Compensate for frame position if in a differnt frame
8458 if ( this.input
.$.frame
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
8459 frameOffset
= OO
.ui
.Element
.getRelativePosition(
8460 this.input
.$.frame
.$element
, this.$element
.offsetParent()
8462 dimensions
.left
+= frameOffset
.left
;
8463 dimensions
.top
+= frameOffset
.top
;
8465 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
8466 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
8467 dimensions
.right
= this.$element
.parent().position().left
-
8468 $container
.width() - dimensions
.left
;
8469 // Erase the value for 'left':
8470 delete dimensions
.left
;
8474 this.$element
.css( dimensions
);
8475 this.setIdealSize( $container
.width() );
8479 * Width with on and off states.
8481 * Mixin for widgets with a boolean state.
8487 * @param {Object} [config] Configuration options
8488 * @cfg {boolean} [value=false] Initial value
8490 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
8491 // Configuration initialization
8492 config
= config
|| {};
8498 this.$element
.addClass( 'oo-ui-toggleWidget' );
8499 this.setValue( !!config
.value
);
8506 * @param {boolean} value Changed value
8512 * Get the value of the toggle.
8516 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
8521 * Set the value of the toggle.
8523 * @param {boolean} value New value
8527 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
8529 if ( this.value
!== value
) {
8531 this.emit( 'change', value
);
8532 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
8533 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
8538 * Button that toggles on and off.
8541 * @extends OO.ui.ButtonWidget
8542 * @mixins OO.ui.ToggleWidget
8545 * @param {Object} [config] Configuration options
8546 * @cfg {boolean} [value=false] Initial value
8548 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8549 // Configuration initialization
8550 config
= config
|| {};
8552 // Parent constructor
8553 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8555 // Mixin constructors
8556 OO
.ui
.ToggleWidget
.call( this, config
);
8559 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8564 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8565 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8572 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8573 if ( !this.isDisabled() ) {
8574 this.setValue( !this.value
);
8578 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8584 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8586 if ( value
!== this.value
) {
8587 this.setActive( value
);
8590 // Parent method (from mixin)
8591 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8596 * Switch that slides on and off.
8599 * @extends OO.ui.Widget
8600 * @mixins OO.ui.ToggleWidget
8603 * @param {Object} [config] Configuration options
8604 * @cfg {boolean} [value=false] Initial value
8606 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
8607 // Parent constructor
8608 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
8610 // Mixin constructors
8611 OO
.ui
.ToggleWidget
.call( this, config
);
8614 this.dragging
= false;
8615 this.dragStart
= null;
8616 this.sliding
= false;
8617 this.$glow
= this.$( '<span>' );
8618 this.$grip
= this.$( '<span>' );
8621 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
8624 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
8625 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
8627 .addClass( 'oo-ui-toggleSwitchWidget' )
8628 .append( this.$glow
, this.$grip
);
8633 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
8634 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
8639 * Handle mouse down events.
8641 * @param {jQuery.Event} e Mouse down event
8643 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
8644 if ( !this.isDisabled() && e
.which
=== 1 ) {
8645 this.setValue( !this.value
);