3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2016 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2016-06-29T13:27:08Z
16 * DraggableElement is a mixin class used to create elements that can be clicked
17 * and dragged by a mouse to a new position within a group. This class must be used
18 * in conjunction with OO.ui.mixin.DraggableGroupElement, which provides a container for
19 * the draggable elements.
25 * @param {Object} [config] Configuration options
26 * @cfg {jQuery} [$handle] The part of the element which can be used for dragging, defaults to the whole element
28 OO
.ui
.mixin
.DraggableElement
= function OoUiMixinDraggableElement( config
) {
29 config
= config
|| {};
33 this.$handle
= config
.$handle
|| this.$element
;
34 this.wasHandleUsed
= null;
36 // Initialize and events
37 this.$element
.addClass( 'oo-ui-draggableElement' )
38 // We make the entire element draggable, not just the handle, so that
39 // the whole element appears to move. wasHandleUsed prevents drags from
40 // starting outside the handle
41 .attr( 'draggable', true )
43 mousedown
: this.onDragMouseDown
.bind( this ),
44 dragstart
: this.onDragStart
.bind( this ),
45 dragover
: this.onDragOver
.bind( this ),
46 dragend
: this.onDragEnd
.bind( this ),
47 drop
: this.onDrop
.bind( this )
49 this.$handle
.addClass( 'oo-ui-draggableElement-handle' );
52 OO
.initClass( OO
.ui
.mixin
.DraggableElement
);
59 * A dragstart event is emitted when the user clicks and begins dragging an item.
60 * @param {OO.ui.mixin.DraggableElement} item The item the user has clicked and is dragging with the mouse.
65 * A dragend event is emitted when the user drags an item and releases the mouse,
66 * thus terminating the drag operation.
71 * A drop event is emitted when the user drags an item and then releases the mouse button
72 * over a valid target.
75 /* Static Properties */
78 * @inheritdoc OO.ui.mixin.ButtonElement
80 OO
.ui
.mixin
.DraggableElement
.static.cancelButtonMouseDownEvents
= false;
85 * Respond to mousedown event.
88 * @param {jQuery.Event} e jQuery event
90 OO
.ui
.mixin
.DraggableElement
.prototype.onDragMouseDown = function ( e
) {
92 // Optimization: if the handle is the whole element this is always true
93 this.$handle
[ 0 ] === this.$element
[ 0 ] ||
94 // Check the mousedown occurred inside the handle
95 OO
.ui
.contains( this.$handle
[ 0 ], e
.target
, true );
99 * Respond to dragstart event.
102 * @param {jQuery.Event} e jQuery event
105 OO
.ui
.mixin
.DraggableElement
.prototype.onDragStart = function ( e
) {
107 dataTransfer
= e
.originalEvent
.dataTransfer
;
109 if ( !this.wasHandleUsed
) {
113 // Define drop effect
114 dataTransfer
.dropEffect
= 'none';
115 dataTransfer
.effectAllowed
= 'move';
117 // We must set up a dataTransfer data property or Firefox seems to
118 // ignore the fact the element is draggable.
120 dataTransfer
.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
122 // The above is only for Firefox. Move on if it fails.
124 // Briefly add a 'clone' class to style the browser's native drag image
125 this.$element
.addClass( 'oo-ui-draggableElement-clone' );
126 // Add placeholder class after the browser has rendered the clone
127 setTimeout( function () {
129 .removeClass( 'oo-ui-draggableElement-clone' )
130 .addClass( 'oo-ui-draggableElement-placeholder' );
133 this.emit( 'dragstart', this );
138 * Respond to dragend event.
143 OO
.ui
.mixin
.DraggableElement
.prototype.onDragEnd = function () {
144 this.$element
.removeClass( 'oo-ui-draggableElement-placeholder' );
145 this.emit( 'dragend' );
152 * @param {jQuery.Event} e jQuery event
155 OO
.ui
.mixin
.DraggableElement
.prototype.onDrop = function ( e
) {
157 this.emit( 'drop', e
);
161 * In order for drag/drop to work, the dragover event must
162 * return false and stop propogation.
166 OO
.ui
.mixin
.DraggableElement
.prototype.onDragOver = function ( e
) {
172 * Store it in the DOM so we can access from the widget drag event
175 * @param {number} index Item index
177 OO
.ui
.mixin
.DraggableElement
.prototype.setIndex = function ( index
) {
178 if ( this.index
!== index
) {
180 this.$element
.data( 'index', index
);
188 * @return {number} Item index
190 OO
.ui
.mixin
.DraggableElement
.prototype.getIndex = function () {
195 * DraggableGroupElement is a mixin class used to create a group element to
196 * contain draggable elements, which are items that can be clicked and dragged by a mouse.
197 * The class is used with OO.ui.mixin.DraggableElement.
201 * @mixins OO.ui.mixin.GroupElement
204 * @param {Object} [config] Configuration options
205 * @cfg {string} [orientation] Item orientation: 'horizontal' or 'vertical'. The orientation
206 * should match the layout of the items. Items displayed in a single row
207 * or in several rows should use horizontal orientation. The vertical orientation should only be
208 * used when the items are displayed in a single column. Defaults to 'vertical'
210 OO
.ui
.mixin
.DraggableGroupElement
= function OoUiMixinDraggableGroupElement( config
) {
211 // Configuration initialization
212 config
= config
|| {};
214 // Parent constructor
215 OO
.ui
.mixin
.GroupElement
.call( this, config
);
218 this.orientation
= config
.orientation
|| 'vertical';
219 this.dragItem
= null;
222 this.itemsOrder
= null;
226 dragstart
: 'itemDragStart',
227 dragend
: 'itemDragEnd',
230 this.connect( this, {
231 itemDragStart
: 'onItemDragStart',
232 itemDrop
: 'onItemDropOrDragEnd',
233 itemDragEnd
: 'onItemDropOrDragEnd'
237 if ( Array
.isArray( config
.items
) ) {
238 this.addItems( config
.items
);
241 .addClass( 'oo-ui-draggableGroupElement' )
242 .append( this.$status
)
243 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' );
247 OO
.mixinClass( OO
.ui
.mixin
.DraggableGroupElement
, OO
.ui
.mixin
.GroupElement
);
252 * An item has been dragged to a new position, but not yet dropped.
255 * @param {OO.ui.mixin.DraggableElement} item Dragged item
256 * @param {number} [newIndex] New index for the item
260 * And item has been dropped at a new position.
263 * @param {OO.ui.mixin.DraggableElement} item Reordered item
264 * @param {number} [newIndex] New index for the item
270 * Respond to item drag start event
273 * @param {OO.ui.mixin.DraggableElement} item Dragged item
275 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
276 // Make a shallow copy of this.items so we can re-order it during previews
277 // without affecting the original array.
278 this.itemsOrder
= this.items
.slice();
279 this.updateIndexes();
280 if ( this.orientation
=== 'horizontal' ) {
281 // Calculate and cache directionality on drag start - it's a little
282 // expensive and it shouldn't change while dragging.
283 this.dir
= this.$element
.css( 'direction' );
285 this.setDragItem( item
);
289 * Update the index properties of the items
291 OO
.ui
.mixin
.DraggableGroupElement
.prototype.updateIndexes = function () {
294 // Map the index of each object
295 for ( i
= 0, len
= this.itemsOrder
.length
; i
< len
; i
++ ) {
296 this.itemsOrder
[ i
].setIndex( i
);
301 * Handle drop or dragend event and switch the order of the items accordingly
304 * @param {OO.ui.mixin.DraggableElement} item Dropped item
306 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDropOrDragEnd = function () {
307 var targetIndex
, originalIndex
,
308 item
= this.getDragItem();
310 // TODO: Figure out a way to configure a list of legally droppable
311 // elements even if they are not yet in the list
313 originalIndex
= this.items
.indexOf( item
);
314 // If the item has moved forward, add one to the index to account for the left shift
315 targetIndex
= item
.getIndex() + ( item
.getIndex() > originalIndex
? 1 : 0 );
316 if ( targetIndex
!== originalIndex
) {
317 this.reorder( this.getDragItem(), targetIndex
);
318 this.emit( 'reorder', this.getDragItem(), targetIndex
);
320 this.updateIndexes();
322 this.unsetDragItem();
323 // Return false to prevent propogation
328 * Respond to dragover event
331 * @param {jQuery.Event} e Dragover event
334 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
335 var overIndex
, targetIndex
,
336 item
= this.getDragItem(),
337 dragItemIndex
= item
.getIndex();
339 // Get the OptionWidget item we are dragging over
340 overIndex
= $( e
.target
).closest( '.oo-ui-draggableElement' ).data( 'index' );
342 if ( overIndex
!== undefined && overIndex
!== dragItemIndex
) {
343 targetIndex
= overIndex
+ ( overIndex
> dragItemIndex
? 1 : 0 );
345 if ( targetIndex
> 0 ) {
346 this.$group
.children().eq( targetIndex
- 1 ).after( item
.$element
);
348 this.$group
.prepend( item
.$element
);
350 // Move item in itemsOrder array
351 this.itemsOrder
.splice( overIndex
, 0,
352 this.itemsOrder
.splice( dragItemIndex
, 1 )[ 0 ]
354 this.updateIndexes();
355 this.emit( 'drag', item
, targetIndex
);
362 * Reorder the items in the group
364 * @param {OO.ui.mixin.DraggableElement} item Reordered item
365 * @param {number} newIndex New index
367 OO
.ui
.mixin
.DraggableGroupElement
.prototype.reorder = function ( item
, newIndex
) {
368 this.addItems( [ item
], newIndex
);
374 * @param {OO.ui.mixin.DraggableElement} item Dragged item
376 OO
.ui
.mixin
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
377 if ( this.dragItem
!== item
) {
378 this.dragItem
= item
;
379 this.$element
.on( 'dragover', this.onDragOver
.bind( this ) );
380 this.$element
.addClass( 'oo-ui-draggableGroupElement-dragging' );
385 * Unset the current dragged item
387 OO
.ui
.mixin
.DraggableGroupElement
.prototype.unsetDragItem = function () {
388 if ( this.dragItem
) {
389 this.dragItem
= null;
390 this.$element
.off( 'dragover' );
391 this.$element
.removeClass( 'oo-ui-draggableGroupElement-dragging' );
396 * Get the item that is currently being dragged.
398 * @return {OO.ui.mixin.DraggableElement|null} The currently dragged item, or `null` if no item is being dragged
400 OO
.ui
.mixin
.DraggableGroupElement
.prototype.getDragItem = function () {
401 return this.dragItem
;
405 * RequestManager is a mixin that manages the lifecycle of a promise-backed request for a widget, such as
406 * the {@link OO.ui.mixin.LookupElement}.
413 OO
.ui
.mixin
.RequestManager
= function OoUiMixinRequestManager() {
414 this.requestCache
= {};
415 this.requestQuery
= null;
416 this.requestRequest
= null;
421 OO
.initClass( OO
.ui
.mixin
.RequestManager
);
424 * Get request results for the current query.
426 * @return {jQuery.Promise} Promise object which will be passed response data as the first argument of
427 * the done event. If the request was aborted to make way for a subsequent request, this promise
428 * may not be rejected, depending on what jQuery feels like doing.
430 OO
.ui
.mixin
.RequestManager
.prototype.getRequestData = function () {
432 value
= this.getRequestQuery(),
433 deferred
= $.Deferred(),
437 if ( Object
.prototype.hasOwnProperty
.call( this.requestCache
, value
) ) {
438 deferred
.resolve( this.requestCache
[ value
] );
440 if ( this.pushPending
) {
443 this.requestQuery
= value
;
444 ourRequest
= this.requestRequest
= this.getRequest();
446 .always( function () {
447 // We need to pop pending even if this is an old request, otherwise
448 // the widget will remain pending forever.
449 // TODO: this assumes that an aborted request will fail or succeed soon after
450 // being aborted, or at least eventually. It would be nice if we could popPending()
451 // at abort time, but only if we knew that we hadn't already called popPending()
453 if ( widget
.popPending
) {
457 .done( function ( response
) {
458 // If this is an old request (and aborting it somehow caused it to still succeed),
459 // ignore its success completely
460 if ( ourRequest
=== widget
.requestRequest
) {
461 widget
.requestQuery
= null;
462 widget
.requestRequest
= null;
463 widget
.requestCache
[ value
] = widget
.getRequestCacheDataFromResponse( response
);
464 deferred
.resolve( widget
.requestCache
[ value
] );
468 // If this is an old request (or a request failing because it's being aborted),
469 // ignore its failure completely
470 if ( ourRequest
=== widget
.requestRequest
) {
471 widget
.requestQuery
= null;
472 widget
.requestRequest
= null;
477 return deferred
.promise();
481 * Abort the currently pending request, if any.
485 OO
.ui
.mixin
.RequestManager
.prototype.abortRequest = function () {
486 var oldRequest
= this.requestRequest
;
488 // First unset this.requestRequest to the fail handler will notice
489 // that the request is no longer current
490 this.requestRequest
= null;
491 this.requestQuery
= null;
497 * Get the query to be made.
502 * @return {string} query to be used
504 OO
.ui
.mixin
.RequestManager
.prototype.getRequestQuery
= null;
507 * Get a new request object of the current query value.
512 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
514 OO
.ui
.mixin
.RequestManager
.prototype.getRequest
= null;
517 * Pre-process data returned by the request from #getRequest.
519 * The return value of this function will be cached, and any further queries for the given value
520 * will use the cache rather than doing API requests.
525 * @param {Mixed} response Response from server
526 * @return {Mixed} Cached result data
528 OO
.ui
.mixin
.RequestManager
.prototype.getRequestCacheDataFromResponse
= null;
531 * LookupElement is a mixin that creates a {@link OO.ui.FloatingMenuSelectWidget menu} of suggested values for
532 * a {@link OO.ui.TextInputWidget text input widget}. Suggested values are based on the characters the user types
533 * into the text input field and, in general, the menu is only displayed when the user types. If a suggested value is chosen
534 * from the lookup menu, that value becomes the value of the input field.
536 * Note that a new menu of suggested items is displayed when a value is chosen from the lookup menu. If this is
537 * not the desired behavior, disable lookup menus with the #setLookupsDisabled method, then set the value, then
540 * See the [OOjs UI demos][1] for an example.
542 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/index.html#widgets-apex-vector-ltr
546 * @mixins OO.ui.mixin.RequestManager
549 * @param {Object} [config] Configuration options
550 * @cfg {jQuery} [$overlay] Overlay for the lookup menu; defaults to relative positioning
551 * @cfg {jQuery} [$container=this.$element] The container element. The lookup menu is rendered beneath the specified element.
552 * @cfg {boolean} [allowSuggestionsWhenEmpty=false] Request and display a lookup menu when the text input is empty.
553 * By default, the lookup menu is not generated and displayed until the user begins to type.
554 * @cfg {boolean} [highlightFirst=true] Whether the first lookup result should be highlighted (so, that the user can
555 * take it over into the input with simply pressing return) automatically or not.
557 OO
.ui
.mixin
.LookupElement
= function OoUiMixinLookupElement( config
) {
558 // Configuration initialization
559 config
= $.extend( { highlightFirst
: true }, config
);
561 // Mixin constructors
562 OO
.ui
.mixin
.RequestManager
.call( this, config
);
565 this.$overlay
= config
.$overlay
|| this.$element
;
566 this.lookupMenu
= new OO
.ui
.FloatingMenuSelectWidget( {
569 $container
: config
.$container
|| this.$element
572 this.allowSuggestionsWhenEmpty
= config
.allowSuggestionsWhenEmpty
|| false;
574 this.lookupsDisabled
= false;
575 this.lookupInputFocused
= false;
576 this.lookupHighlightFirstItem
= config
.highlightFirst
;
580 focus
: this.onLookupInputFocus
.bind( this ),
581 blur
: this.onLookupInputBlur
.bind( this ),
582 mousedown
: this.onLookupInputMouseDown
.bind( this )
584 this.connect( this, { change
: 'onLookupInputChange' } );
585 this.lookupMenu
.connect( this, {
586 toggle
: 'onLookupMenuToggle',
587 choose
: 'onLookupMenuItemChoose'
591 this.$element
.addClass( 'oo-ui-lookupElement' );
592 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
593 this.$overlay
.append( this.lookupMenu
.$element
);
598 OO
.mixinClass( OO
.ui
.mixin
.LookupElement
, OO
.ui
.mixin
.RequestManager
);
603 * Handle input focus event.
606 * @param {jQuery.Event} e Input focus event
608 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputFocus = function () {
609 this.lookupInputFocused
= true;
610 this.populateLookupMenu();
614 * Handle input blur event.
617 * @param {jQuery.Event} e Input blur event
619 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputBlur = function () {
620 this.closeLookupMenu();
621 this.lookupInputFocused
= false;
625 * Handle input mouse down event.
628 * @param {jQuery.Event} e Input mouse down event
630 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputMouseDown = function () {
631 // Only open the menu if the input was already focused.
632 // This way we allow the user to open the menu again after closing it with Esc
633 // by clicking in the input. Opening (and populating) the menu when initially
634 // clicking into the input is handled by the focus handler.
635 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
636 this.populateLookupMenu();
641 * Handle input change event.
644 * @param {string} value New input value
646 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputChange = function () {
647 if ( this.lookupInputFocused
) {
648 this.populateLookupMenu();
653 * Handle the lookup menu being shown/hidden.
656 * @param {boolean} visible Whether the lookup menu is now visible.
658 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
660 // When the menu is hidden, abort any active request and clear the menu.
661 // This has to be done here in addition to closeLookupMenu(), because
662 // MenuSelectWidget will close itself when the user presses Esc.
663 this.abortLookupRequest();
664 this.lookupMenu
.clearItems();
669 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
672 * @param {OO.ui.MenuOptionWidget} item Selected item
674 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
675 this.setValue( item
.getData() );
682 * @return {OO.ui.FloatingMenuSelectWidget}
684 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenu = function () {
685 return this.lookupMenu
;
689 * Disable or re-enable lookups.
691 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
693 * @param {boolean} disabled Disable lookups
695 OO
.ui
.mixin
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
696 this.lookupsDisabled
= !!disabled
;
700 * Open the menu. If there are no entries in the menu, this does nothing.
705 OO
.ui
.mixin
.LookupElement
.prototype.openLookupMenu = function () {
706 if ( !this.lookupMenu
.isEmpty() ) {
707 this.lookupMenu
.toggle( true );
713 * Close the menu, empty it, and abort any pending request.
718 OO
.ui
.mixin
.LookupElement
.prototype.closeLookupMenu = function () {
719 this.lookupMenu
.toggle( false );
720 this.abortLookupRequest();
721 this.lookupMenu
.clearItems();
726 * Request menu items based on the input's current value, and when they arrive,
727 * populate the menu with these items and show the menu.
729 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
734 OO
.ui
.mixin
.LookupElement
.prototype.populateLookupMenu = function () {
736 value
= this.getValue();
738 if ( this.lookupsDisabled
|| this.isReadOnly() ) {
742 // If the input is empty, clear the menu, unless suggestions when empty are allowed.
743 if ( !this.allowSuggestionsWhenEmpty
&& value
=== '' ) {
744 this.closeLookupMenu();
745 // Skip population if there is already a request pending for the current value
746 } else if ( value
!== this.lookupQuery
) {
747 this.getLookupMenuItems()
748 .done( function ( items
) {
749 widget
.lookupMenu
.clearItems();
750 if ( items
.length
) {
754 widget
.initializeLookupMenuSelection();
756 widget
.lookupMenu
.toggle( false );
760 widget
.lookupMenu
.clearItems();
768 * Highlight the first selectable item in the menu, if configured.
773 OO
.ui
.mixin
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
774 if ( this.lookupHighlightFirstItem
&& !this.lookupMenu
.getSelectedItem() ) {
775 this.lookupMenu
.highlightItem( this.lookupMenu
.getFirstSelectableItem() );
780 * Get lookup menu items for the current query.
783 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
784 * the done event. If the request was aborted to make way for a subsequent request, this promise
785 * will not be rejected: it will remain pending forever.
787 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuItems = function () {
788 return this.getRequestData().then( function ( data
) {
789 return this.getLookupMenuOptionsFromData( data
);
794 * Abort the currently pending lookup request, if any.
798 OO
.ui
.mixin
.LookupElement
.prototype.abortLookupRequest = function () {
803 * Get a new request object of the current lookup query value.
808 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
810 OO
.ui
.mixin
.LookupElement
.prototype.getLookupRequest
= null;
813 * Pre-process data returned by the request from #getLookupRequest.
815 * The return value of this function will be cached, and any further queries for the given value
816 * will use the cache rather than doing API requests.
821 * @param {Mixed} response Response from server
822 * @return {Mixed} Cached result data
824 OO
.ui
.mixin
.LookupElement
.prototype.getLookupCacheDataFromResponse
= null;
827 * Get a list of menu option widgets from the (possibly cached) data returned by
828 * #getLookupCacheDataFromResponse.
833 * @param {Mixed} data Cached result data, usually an array
834 * @return {OO.ui.MenuOptionWidget[]} Menu items
836 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuOptionsFromData
= null;
839 * Set the read-only state of the widget.
841 * This will also disable/enable the lookups functionality.
843 * @param {boolean} readOnly Make input read-only
846 OO
.ui
.mixin
.LookupElement
.prototype.setReadOnly = function ( readOnly
) {
848 // Note: Calling #setReadOnly this way assumes this is mixed into an OO.ui.TextInputWidget
849 OO
.ui
.TextInputWidget
.prototype.setReadOnly
.call( this, readOnly
);
851 // During construction, #setReadOnly is called before the OO.ui.mixin.LookupElement constructor
852 if ( this.isReadOnly() && this.lookupMenu
) {
853 this.closeLookupMenu();
860 * @inheritdoc OO.ui.mixin.RequestManager
862 OO
.ui
.mixin
.LookupElement
.prototype.getRequestQuery = function () {
863 return this.getValue();
867 * @inheritdoc OO.ui.mixin.RequestManager
869 OO
.ui
.mixin
.LookupElement
.prototype.getRequest = function () {
870 return this.getLookupRequest();
874 * @inheritdoc OO.ui.mixin.RequestManager
876 OO
.ui
.mixin
.LookupElement
.prototype.getRequestCacheDataFromResponse = function ( response
) {
877 return this.getLookupCacheDataFromResponse( response
);
881 * CardLayouts are used within {@link OO.ui.IndexLayout index layouts} to create cards that users can select and display
882 * from the index's optional {@link OO.ui.TabSelectWidget tab} navigation. Cards are usually not instantiated directly,
883 * rather extended to include the required content and functionality.
885 * Each card must have a unique symbolic name, which is passed to the constructor. In addition, the card's tab
886 * item is customized (with a label) using the #setupTabItem method. See
887 * {@link OO.ui.IndexLayout IndexLayout} for an example.
890 * @extends OO.ui.PanelLayout
893 * @param {string} name Unique symbolic name of card
894 * @param {Object} [config] Configuration options
895 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] Label for card's tab
897 OO
.ui
.CardLayout
= function OoUiCardLayout( name
, config
) {
898 // Allow passing positional parameters inside the config object
899 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
904 // Configuration initialization
905 config
= $.extend( { scrollable
: true }, config
);
907 // Parent constructor
908 OO
.ui
.CardLayout
.parent
.call( this, config
);
912 this.label
= config
.label
;
917 this.$element
.addClass( 'oo-ui-cardLayout' );
922 OO
.inheritClass( OO
.ui
.CardLayout
, OO
.ui
.PanelLayout
);
927 * An 'active' event is emitted when the card becomes active. Cards become active when they are
928 * shown in a index layout that is configured to display only one card at a time.
931 * @param {boolean} active Card is active
937 * Get the symbolic name of the card.
939 * @return {string} Symbolic name of card
941 OO
.ui
.CardLayout
.prototype.getName = function () {
946 * Check if card is active.
948 * Cards become active when they are shown in a {@link OO.ui.IndexLayout index layout} that is configured to display
949 * only one card at a time. Additional CSS is applied to the card's tab item to reflect the active state.
951 * @return {boolean} Card is active
953 OO
.ui
.CardLayout
.prototype.isActive = function () {
960 * The tab item allows users to access the card from the index's tab
961 * navigation. The tab item itself can be customized (with a label, level, etc.) using the #setupTabItem method.
963 * @return {OO.ui.TabOptionWidget|null} Tab option widget
965 OO
.ui
.CardLayout
.prototype.getTabItem = function () {
970 * Set or unset the tab item.
972 * Specify a {@link OO.ui.TabOptionWidget tab option} to set it,
973 * or `null` to clear the tab item. To customize the tab item itself (e.g., to set a label or tab
974 * level), use #setupTabItem instead of this method.
976 * @param {OO.ui.TabOptionWidget|null} tabItem Tab option widget, null to clear
979 OO
.ui
.CardLayout
.prototype.setTabItem = function ( tabItem
) {
980 this.tabItem
= tabItem
|| null;
988 * Set up the tab item.
990 * Use this method to customize the tab item (e.g., to add a label or tab level). To set or unset
991 * the tab item itself (with a {@link OO.ui.TabOptionWidget tab option} or `null`), use
992 * the #setTabItem method instead.
994 * @param {OO.ui.TabOptionWidget} tabItem Tab option widget to set up
997 OO
.ui
.CardLayout
.prototype.setupTabItem = function () {
999 this.tabItem
.setLabel( this.label
);
1005 * Set the card to its 'active' state.
1007 * Cards become active when they are shown in a index layout that is configured to display only one card at a time. Additional
1008 * CSS is applied to the tab item to reflect the card's active state. Outside of the index
1009 * context, setting the active state on a card does nothing.
1011 * @param {boolean} active Card is active
1014 OO
.ui
.CardLayout
.prototype.setActive = function ( active
) {
1017 if ( active
!== this.active
) {
1018 this.active
= active
;
1019 this.$element
.toggleClass( 'oo-ui-cardLayout-active', this.active
);
1020 this.emit( 'active', this.active
);
1025 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that users can select and display
1026 * from the booklet's optional {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated directly,
1027 * rather extended to include the required content and functionality.
1029 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the page's outline
1030 * item is customized (with a label, outline level, etc.) using the #setupOutlineItem method. See
1031 * {@link OO.ui.BookletLayout BookletLayout} for an example.
1034 * @extends OO.ui.PanelLayout
1037 * @param {string} name Unique symbolic name of page
1038 * @param {Object} [config] Configuration options
1040 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
1041 // Allow passing positional parameters inside the config object
1042 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1047 // Configuration initialization
1048 config
= $.extend( { scrollable
: true }, config
);
1050 // Parent constructor
1051 OO
.ui
.PageLayout
.parent
.call( this, config
);
1055 this.outlineItem
= null;
1056 this.active
= false;
1059 this.$element
.addClass( 'oo-ui-pageLayout' );
1064 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
1069 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1070 * shown in a booklet layout that is configured to display only one page at a time.
1073 * @param {boolean} active Page is active
1079 * Get the symbolic name of the page.
1081 * @return {string} Symbolic name of page
1083 OO
.ui
.PageLayout
.prototype.getName = function () {
1088 * Check if page is active.
1090 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is configured to display
1091 * only one page at a time. Additional CSS is applied to the page's outline item to reflect the active state.
1093 * @return {boolean} Page is active
1095 OO
.ui
.PageLayout
.prototype.isActive = function () {
1102 * The outline item allows users to access the page from the booklet's outline
1103 * navigation. The outline item itself can be customized (with a label, level, etc.) using the #setupOutlineItem method.
1105 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1107 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
1108 return this.outlineItem
;
1112 * Set or unset the outline item.
1114 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1115 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label or outline
1116 * level), use #setupOutlineItem instead of this method.
1118 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1121 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
1122 this.outlineItem
= outlineItem
|| null;
1123 if ( outlineItem
) {
1124 this.setupOutlineItem();
1130 * Set up the outline item.
1132 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or unset
1133 * the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or `null`), use
1134 * the #setOutlineItem method instead.
1136 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1139 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
1144 * Set the page to its 'active' state.
1146 * Pages become active when they are shown in a booklet layout that is configured to display only one page at a time. Additional
1147 * CSS is applied to the outline item to reflect the page's active state. Outside of the booklet
1148 * context, setting the active state on a page does nothing.
1150 * @param {boolean} active Page is active
1153 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
1156 if ( active
!== this.active
) {
1157 this.active
= active
;
1158 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
1159 this.emit( 'active', this.active
);
1164 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one panel is displayed
1165 * at a time, though the stack layout can also be configured to show all contained panels, one after another,
1166 * by setting the #continuous option to 'true'.
1169 * // A stack layout with two panels, configured to be displayed continously
1170 * var myStack = new OO.ui.StackLayout( {
1172 * new OO.ui.PanelLayout( {
1173 * $content: $( '<p>Panel One</p>' ),
1177 * new OO.ui.PanelLayout( {
1178 * $content: $( '<p>Panel Two</p>' ),
1185 * $( 'body' ).append( myStack.$element );
1188 * @extends OO.ui.PanelLayout
1189 * @mixins OO.ui.mixin.GroupElement
1192 * @param {Object} [config] Configuration options
1193 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel is displayed at a time.
1194 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1196 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
1197 // Configuration initialization
1198 config
= $.extend( { scrollable
: true }, config
);
1200 // Parent constructor
1201 OO
.ui
.StackLayout
.parent
.call( this, config
);
1203 // Mixin constructors
1204 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
1207 this.currentItem
= null;
1208 this.continuous
= !!config
.continuous
;
1211 this.$element
.addClass( 'oo-ui-stackLayout' );
1212 if ( this.continuous
) {
1213 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
1214 this.$element
.on( 'scroll', OO
.ui
.debounce( this.onScroll
.bind( this ), 250 ) );
1216 if ( Array
.isArray( config
.items
) ) {
1217 this.addItems( config
.items
);
1223 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
1224 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.mixin
.GroupElement
);
1229 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1230 * {@link #clearItems cleared} or {@link #setItem displayed}.
1233 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1237 * When used in continuous mode, this event is emitted when the user scrolls down
1238 * far enough such that currentItem is no longer visible.
1240 * @event visibleItemChange
1241 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1247 * Handle scroll events from the layout element
1249 * @param {jQuery.Event} e
1250 * @fires visibleItemChange
1252 OO
.ui
.StackLayout
.prototype.onScroll = function () {
1254 len
= this.items
.length
,
1255 currentIndex
= this.items
.indexOf( this.currentItem
),
1256 newIndex
= currentIndex
,
1257 containerRect
= this.$element
[ 0 ].getBoundingClientRect();
1259 if ( !containerRect
|| ( !containerRect
.top
&& !containerRect
.bottom
) ) {
1260 // Can't get bounding rect, possibly not attached.
1264 function getRect( item
) {
1265 return item
.$element
[ 0 ].getBoundingClientRect();
1268 function isVisible( item
) {
1269 var rect
= getRect( item
);
1270 return rect
.bottom
> containerRect
.top
&& rect
.top
< containerRect
.bottom
;
1273 currentRect
= getRect( this.currentItem
);
1275 if ( currentRect
.bottom
< containerRect
.top
) {
1276 // Scrolled down past current item
1277 while ( ++newIndex
< len
) {
1278 if ( isVisible( this.items
[ newIndex
] ) ) {
1282 } else if ( currentRect
.top
> containerRect
.bottom
) {
1283 // Scrolled up past current item
1284 while ( --newIndex
>= 0 ) {
1285 if ( isVisible( this.items
[ newIndex
] ) ) {
1291 if ( newIndex
!== currentIndex
) {
1292 this.emit( 'visibleItemChange', this.items
[ newIndex
] );
1297 * Get the current panel.
1299 * @return {OO.ui.Layout|null}
1301 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
1302 return this.currentItem
;
1306 * Unset the current item.
1309 * @param {OO.ui.StackLayout} layout
1312 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
1313 var prevItem
= this.currentItem
;
1314 if ( prevItem
=== null ) {
1318 this.currentItem
= null;
1319 this.emit( 'set', null );
1323 * Add panel layouts to the stack layout.
1325 * Panels will be added to the end of the stack layout array unless the optional index parameter specifies a different
1326 * insertion point. Adding a panel that is already in the stack will move it to the end of the array or the point specified
1329 * @param {OO.ui.Layout[]} items Panels to add
1330 * @param {number} [index] Index of the insertion point
1333 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
1334 // Update the visibility
1335 this.updateHiddenState( items
, this.currentItem
);
1338 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
, index
);
1340 if ( !this.currentItem
&& items
.length
) {
1341 this.setItem( items
[ 0 ] );
1348 * Remove the specified panels from the stack layout.
1350 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all panels,
1351 * you may wish to use the #clearItems method instead.
1353 * @param {OO.ui.Layout[]} items Panels to remove
1357 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
1359 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
1361 if ( items
.indexOf( this.currentItem
) !== -1 ) {
1362 if ( this.items
.length
) {
1363 this.setItem( this.items
[ 0 ] );
1365 this.unsetCurrentItem();
1373 * Clear all panels from the stack layout.
1375 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1376 * a subset of panels, use the #removeItems method.
1381 OO
.ui
.StackLayout
.prototype.clearItems = function () {
1382 this.unsetCurrentItem();
1383 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
1389 * Show the specified panel.
1391 * If another panel is currently displayed, it will be hidden.
1393 * @param {OO.ui.Layout} item Panel to show
1397 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
1398 if ( item
!== this.currentItem
) {
1399 this.updateHiddenState( this.items
, item
);
1401 if ( this.items
.indexOf( item
) !== -1 ) {
1402 this.currentItem
= item
;
1403 this.emit( 'set', item
);
1405 this.unsetCurrentItem();
1413 * Update the visibility of all items in case of non-continuous view.
1415 * Ensure all items are hidden except for the selected one.
1416 * This method does nothing when the stack is continuous.
1419 * @param {OO.ui.Layout[]} items Item list iterate over
1420 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1422 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
1425 if ( !this.continuous
) {
1426 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
1427 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
1428 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
1431 if ( selectedItem
) {
1432 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
1438 * MenuLayouts combine a menu and a content {@link OO.ui.PanelLayout panel}. The menu is positioned relative to the content (after, before, top, or bottom)
1439 * and its size is customized with the #menuSize config. The content area will fill all remaining space.
1442 * var menuLayout = new OO.ui.MenuLayout( {
1445 * menuPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1446 * contentPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1447 * select = new OO.ui.SelectWidget( {
1449 * new OO.ui.OptionWidget( {
1453 * new OO.ui.OptionWidget( {
1457 * new OO.ui.OptionWidget( {
1461 * new OO.ui.OptionWidget( {
1466 * } ).on( 'select', function ( item ) {
1467 * menuLayout.setMenuPosition( item.getData() );
1470 * menuLayout.$menu.append(
1471 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element )
1473 * menuLayout.$content.append(
1474 * contentPanel.$element.append( '<b>Content panel</b>', '<p>Note that the menu is positioned relative to the content panel: top, bottom, after, before.</p>')
1476 * $( 'body' ).append( menuLayout.$element );
1478 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1479 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1480 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1483 * .oo-ui-menuLayout-menu {
1487 * .oo-ui-menuLayout-content {
1495 * @extends OO.ui.Layout
1498 * @param {Object} [config] Configuration options
1499 * @cfg {boolean} [showMenu=true] Show menu
1500 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1502 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
1503 // Configuration initialization
1504 config
= $.extend( {
1506 menuPosition
: 'before'
1509 // Parent constructor
1510 OO
.ui
.MenuLayout
.parent
.call( this, config
);
1515 * @property {jQuery}
1517 this.$menu
= $( '<div>' );
1521 * @property {jQuery}
1523 this.$content
= $( '<div>' );
1527 .addClass( 'oo-ui-menuLayout-menu' );
1528 this.$content
.addClass( 'oo-ui-menuLayout-content' );
1530 .addClass( 'oo-ui-menuLayout' )
1531 .append( this.$content
, this.$menu
);
1532 this.setMenuPosition( config
.menuPosition
);
1533 this.toggleMenu( config
.showMenu
);
1538 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
1545 * @param {boolean} showMenu Show menu, omit to toggle
1548 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
1549 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
1551 if ( this.showMenu
!== showMenu
) {
1552 this.showMenu
= showMenu
;
1554 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu
)
1555 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu
);
1562 * Check if menu is visible
1564 * @return {boolean} Menu is visible
1566 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
1567 return this.showMenu
;
1571 * Set menu position.
1573 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1574 * @throws {Error} If position value is not supported
1577 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
1578 this.$element
.removeClass( 'oo-ui-menuLayout-' + this.menuPosition
);
1579 this.menuPosition
= position
;
1580 this.$element
.addClass( 'oo-ui-menuLayout-' + position
);
1586 * Get menu position.
1588 * @return {string} Menu position
1590 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
1591 return this.menuPosition
;
1595 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1596 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1597 * through the pages and select which one to display. By default, only one page is
1598 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1599 * the booklet layout automatically focuses on the first focusable element, unless the
1600 * default setting is changed. Optionally, booklets can be configured to show
1601 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1604 * // Example of a BookletLayout that contains two PageLayouts.
1606 * function PageOneLayout( name, config ) {
1607 * PageOneLayout.parent.call( this, name, config );
1608 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on the left)</p>' );
1610 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1611 * PageOneLayout.prototype.setupOutlineItem = function () {
1612 * this.outlineItem.setLabel( 'Page One' );
1615 * function PageTwoLayout( name, config ) {
1616 * PageTwoLayout.parent.call( this, name, config );
1617 * this.$element.append( '<p>Second page</p>' );
1619 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1620 * PageTwoLayout.prototype.setupOutlineItem = function () {
1621 * this.outlineItem.setLabel( 'Page Two' );
1624 * var page1 = new PageOneLayout( 'one' ),
1625 * page2 = new PageTwoLayout( 'two' );
1627 * var booklet = new OO.ui.BookletLayout( {
1631 * booklet.addPages ( [ page1, page2 ] );
1632 * $( 'body' ).append( booklet.$element );
1635 * @extends OO.ui.MenuLayout
1638 * @param {Object} [config] Configuration options
1639 * @cfg {boolean} [continuous=false] Show all pages, one after another
1640 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is displayed.
1641 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the pages of the booklet.
1642 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
1644 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
1645 // Configuration initialization
1646 config
= config
|| {};
1648 // Parent constructor
1649 OO
.ui
.BookletLayout
.parent
.call( this, config
);
1652 this.currentPageName
= null;
1654 this.ignoreFocus
= false;
1655 this.stackLayout
= new OO
.ui
.StackLayout( { continuous
: !!config
.continuous
} );
1656 this.$content
.append( this.stackLayout
.$element
);
1657 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
1658 this.outlineVisible
= false;
1659 this.outlined
= !!config
.outlined
;
1660 if ( this.outlined
) {
1661 this.editable
= !!config
.editable
;
1662 this.outlineControlsWidget
= null;
1663 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget();
1664 this.outlinePanel
= new OO
.ui
.PanelLayout( { scrollable
: true } );
1665 this.$menu
.append( this.outlinePanel
.$element
);
1666 this.outlineVisible
= true;
1667 if ( this.editable
) {
1668 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
1669 this.outlineSelectWidget
1673 this.toggleMenu( this.outlined
);
1676 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
1677 if ( this.outlined
) {
1678 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
1679 this.scrolling
= false;
1680 this.stackLayout
.connect( this, { visibleItemChange
: 'onStackLayoutVisibleItemChange' } );
1682 if ( this.autoFocus
) {
1683 // Event 'focus' does not bubble, but 'focusin' does
1684 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
1688 this.$element
.addClass( 'oo-ui-bookletLayout' );
1689 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
1690 if ( this.outlined
) {
1691 this.outlinePanel
.$element
1692 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1693 .append( this.outlineSelectWidget
.$element
);
1694 if ( this.editable
) {
1695 this.outlinePanel
.$element
1696 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1697 .append( this.outlineControlsWidget
.$element
);
1704 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
1709 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the booklet layout.
1711 * @param {OO.ui.PageLayout} page Current page
1715 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
1718 * @param {OO.ui.PageLayout[]} page Added pages
1719 * @param {number} index Index pages were added at
1723 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
1724 * {@link #removePages removed} from the booklet.
1727 * @param {OO.ui.PageLayout[]} pages Removed pages
1733 * Handle stack layout focus.
1736 * @param {jQuery.Event} e Focusin event
1738 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
1741 // Find the page that an element was focused within
1742 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
1743 for ( name
in this.pages
) {
1744 // Check for page match, exclude current page to find only page changes
1745 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
1746 this.setPage( name
);
1753 * Handle visibleItemChange events from the stackLayout
1755 * The next visible page is set as the current page by selecting it
1758 * @param {OO.ui.PageLayout} page The next visible page in the layout
1760 OO
.ui
.BookletLayout
.prototype.onStackLayoutVisibleItemChange = function ( page
) {
1761 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
1762 // try and scroll the item into view again.
1763 this.scrolling
= true;
1764 this.outlineSelectWidget
.selectItemByData( page
.getName() );
1765 this.scrolling
= false;
1769 * Handle stack layout set events.
1772 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
1774 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
1776 if ( !this.scrolling
&& page
) {
1777 page
.scrollElementIntoView( {
1778 complete: function () {
1779 if ( layout
.autoFocus
) {
1788 * Focus the first input in the current page.
1790 * If no page is selected, the first selectable page will be selected.
1791 * If the focus is already in an element on the current page, nothing will happen.
1793 * @param {number} [itemIndex] A specific item to focus on
1795 OO
.ui
.BookletLayout
.prototype.focus = function ( itemIndex
) {
1797 items
= this.stackLayout
.getItems();
1799 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
1800 page
= items
[ itemIndex
];
1802 page
= this.stackLayout
.getCurrentItem();
1805 if ( !page
&& this.outlined
) {
1806 this.selectFirstSelectablePage();
1807 page
= this.stackLayout
.getCurrentItem();
1812 // Only change the focus if is not already in the current page
1813 if ( !OO
.ui
.contains( page
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
1819 * Find the first focusable input in the booklet layout and focus
1822 OO
.ui
.BookletLayout
.prototype.focusFirstFocusable = function () {
1823 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
1827 * Handle outline widget select events.
1830 * @param {OO.ui.OptionWidget|null} item Selected item
1832 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
1834 this.setPage( item
.getData() );
1839 * Check if booklet has an outline.
1841 * @return {boolean} Booklet has an outline
1843 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
1844 return this.outlined
;
1848 * Check if booklet has editing controls.
1850 * @return {boolean} Booklet is editable
1852 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
1853 return this.editable
;
1857 * Check if booklet has a visible outline.
1859 * @return {boolean} Outline is visible
1861 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
1862 return this.outlined
&& this.outlineVisible
;
1866 * Hide or show the outline.
1868 * @param {boolean} [show] Show outline, omit to invert current state
1871 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
1872 if ( this.outlined
) {
1873 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
1874 this.outlineVisible
= show
;
1875 this.toggleMenu( show
);
1882 * Get the page closest to the specified page.
1884 * @param {OO.ui.PageLayout} page Page to use as a reference point
1885 * @return {OO.ui.PageLayout|null} Page closest to the specified page
1887 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
1888 var next
, prev
, level
,
1889 pages
= this.stackLayout
.getItems(),
1890 index
= pages
.indexOf( page
);
1892 if ( index
!== -1 ) {
1893 next
= pages
[ index
+ 1 ];
1894 prev
= pages
[ index
- 1 ];
1895 // Prefer adjacent pages at the same level
1896 if ( this.outlined
) {
1897 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
1900 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
1906 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
1912 return prev
|| next
|| null;
1916 * Get the outline widget.
1918 * If the booklet is not outlined, the method will return `null`.
1920 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
1922 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
1923 return this.outlineSelectWidget
;
1927 * Get the outline controls widget.
1929 * If the outline is not editable, the method will return `null`.
1931 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
1933 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
1934 return this.outlineControlsWidget
;
1938 * Get a page by its symbolic name.
1940 * @param {string} name Symbolic name of page
1941 * @return {OO.ui.PageLayout|undefined} Page, if found
1943 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
1944 return this.pages
[ name
];
1948 * Get the current page.
1950 * @return {OO.ui.PageLayout|undefined} Current page, if found
1952 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
1953 var name
= this.getCurrentPageName();
1954 return name
? this.getPage( name
) : undefined;
1958 * Get the symbolic name of the current page.
1960 * @return {string|null} Symbolic name of the current page
1962 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
1963 return this.currentPageName
;
1967 * Add pages to the booklet layout
1969 * When pages are added with the same names as existing pages, the existing pages will be
1970 * automatically removed before the new pages are added.
1972 * @param {OO.ui.PageLayout[]} pages Pages to add
1973 * @param {number} index Index of the insertion point
1977 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
1978 var i
, len
, name
, page
, item
, currentIndex
,
1979 stackLayoutPages
= this.stackLayout
.getItems(),
1983 // Remove pages with same names
1984 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
1986 name
= page
.getName();
1988 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
1989 // Correct the insertion index
1990 currentIndex
= stackLayoutPages
.indexOf( this.pages
[ name
] );
1991 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
1994 remove
.push( this.pages
[ name
] );
1997 if ( remove
.length
) {
1998 this.removePages( remove
);
2002 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2004 name
= page
.getName();
2005 this.pages
[ page
.getName() ] = page
;
2006 if ( this.outlined
) {
2007 item
= new OO
.ui
.OutlineOptionWidget( { data
: name
} );
2008 page
.setOutlineItem( item
);
2013 if ( this.outlined
&& items
.length
) {
2014 this.outlineSelectWidget
.addItems( items
, index
);
2015 this.selectFirstSelectablePage();
2017 this.stackLayout
.addItems( pages
, index
);
2018 this.emit( 'add', pages
, index
);
2024 * Remove the specified pages from the booklet layout.
2026 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2028 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2032 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
2033 var i
, len
, name
, page
,
2036 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2038 name
= page
.getName();
2039 delete this.pages
[ name
];
2040 if ( this.outlined
) {
2041 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
2042 page
.setOutlineItem( null );
2045 if ( this.outlined
&& items
.length
) {
2046 this.outlineSelectWidget
.removeItems( items
);
2047 this.selectFirstSelectablePage();
2049 this.stackLayout
.removeItems( pages
);
2050 this.emit( 'remove', pages
);
2056 * Clear all pages from the booklet layout.
2058 * To remove only a subset of pages from the booklet, use the #removePages method.
2063 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
2065 pages
= this.stackLayout
.getItems();
2068 this.currentPageName
= null;
2069 if ( this.outlined
) {
2070 this.outlineSelectWidget
.clearItems();
2071 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2072 pages
[ i
].setOutlineItem( null );
2075 this.stackLayout
.clearItems();
2077 this.emit( 'remove', pages
);
2083 * Set the current page by symbolic name.
2086 * @param {string} name Symbolic name of page
2088 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
2091 page
= this.pages
[ name
],
2092 previousPage
= this.currentPageName
&& this.pages
[ this.currentPageName
];
2094 if ( name
!== this.currentPageName
) {
2095 if ( this.outlined
) {
2096 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
2097 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2098 this.outlineSelectWidget
.selectItemByData( name
);
2102 if ( previousPage
) {
2103 previousPage
.setActive( false );
2104 // Blur anything focused if the next page doesn't have anything focusable.
2105 // This is not needed if the next page has something focusable (because once it is focused
2106 // this blur happens automatically). If the layout is non-continuous, this check is
2107 // meaningless because the next page is not visible yet and thus can't hold focus.
2110 this.stackLayout
.continuous
&&
2111 OO
.ui
.findFocusable( page
.$element
).length
!== 0
2113 $focused
= previousPage
.$element
.find( ':focus' );
2114 if ( $focused
.length
) {
2115 $focused
[ 0 ].blur();
2119 this.currentPageName
= name
;
2120 page
.setActive( true );
2121 this.stackLayout
.setItem( page
);
2122 if ( !this.stackLayout
.continuous
&& previousPage
) {
2123 // This should not be necessary, since any inputs on the previous page should have been
2124 // blurred when it was hidden, but browsers are not very consistent about this.
2125 $focused
= previousPage
.$element
.find( ':focus' );
2126 if ( $focused
.length
) {
2127 $focused
[ 0 ].blur();
2130 this.emit( 'set', page
);
2136 * Select the first selectable page.
2140 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
2141 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
2142 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
2149 * IndexLayouts contain {@link OO.ui.CardLayout card layouts} as well as
2150 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the cards and
2151 * select which one to display. By default, only one card is displayed at a time. When a user
2152 * navigates to a new card, the index layout automatically focuses on the first focusable element,
2153 * unless the default setting is changed.
2155 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2158 * // Example of a IndexLayout that contains two CardLayouts.
2160 * function CardOneLayout( name, config ) {
2161 * CardOneLayout.parent.call( this, name, config );
2162 * this.$element.append( '<p>First card</p>' );
2164 * OO.inheritClass( CardOneLayout, OO.ui.CardLayout );
2165 * CardOneLayout.prototype.setupTabItem = function () {
2166 * this.tabItem.setLabel( 'Card one' );
2169 * var card1 = new CardOneLayout( 'one' ),
2170 * card2 = new CardLayout( 'two', { label: 'Card two' } );
2172 * card2.$element.append( '<p>Second card</p>' );
2174 * var index = new OO.ui.IndexLayout();
2176 * index.addCards ( [ card1, card2 ] );
2177 * $( 'body' ).append( index.$element );
2180 * @extends OO.ui.MenuLayout
2183 * @param {Object} [config] Configuration options
2184 * @cfg {boolean} [continuous=false] Show all cards, one after another
2185 * @cfg {boolean} [expanded=true] Expand the content panel to fill the entire parent element.
2186 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new card is displayed.
2188 OO
.ui
.IndexLayout
= function OoUiIndexLayout( config
) {
2189 // Configuration initialization
2190 config
= $.extend( {}, config
, { menuPosition
: 'top' } );
2192 // Parent constructor
2193 OO
.ui
.IndexLayout
.parent
.call( this, config
);
2196 this.currentCardName
= null;
2198 this.ignoreFocus
= false;
2199 this.stackLayout
= new OO
.ui
.StackLayout( {
2200 continuous
: !!config
.continuous
,
2201 expanded
: config
.expanded
2203 this.$content
.append( this.stackLayout
.$element
);
2204 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
2206 this.tabSelectWidget
= new OO
.ui
.TabSelectWidget();
2207 this.tabPanel
= new OO
.ui
.PanelLayout();
2208 this.$menu
.append( this.tabPanel
.$element
);
2210 this.toggleMenu( true );
2213 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
2214 this.tabSelectWidget
.connect( this, { select
: 'onTabSelectWidgetSelect' } );
2215 if ( this.autoFocus
) {
2216 // Event 'focus' does not bubble, but 'focusin' does
2217 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
2221 this.$element
.addClass( 'oo-ui-indexLayout' );
2222 this.stackLayout
.$element
.addClass( 'oo-ui-indexLayout-stackLayout' );
2223 this.tabPanel
.$element
2224 .addClass( 'oo-ui-indexLayout-tabPanel' )
2225 .append( this.tabSelectWidget
.$element
);
2230 OO
.inheritClass( OO
.ui
.IndexLayout
, OO
.ui
.MenuLayout
);
2235 * A 'set' event is emitted when a card is {@link #setCard set} to be displayed by the index layout.
2237 * @param {OO.ui.CardLayout} card Current card
2241 * An 'add' event is emitted when cards are {@link #addCards added} to the index layout.
2244 * @param {OO.ui.CardLayout[]} card Added cards
2245 * @param {number} index Index cards were added at
2249 * A 'remove' event is emitted when cards are {@link #clearCards cleared} or
2250 * {@link #removeCards removed} from the index.
2253 * @param {OO.ui.CardLayout[]} cards Removed cards
2259 * Handle stack layout focus.
2262 * @param {jQuery.Event} e Focusin event
2264 OO
.ui
.IndexLayout
.prototype.onStackLayoutFocus = function ( e
) {
2267 // Find the card that an element was focused within
2268 $target
= $( e
.target
).closest( '.oo-ui-cardLayout' );
2269 for ( name
in this.cards
) {
2270 // Check for card match, exclude current card to find only card changes
2271 if ( this.cards
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentCardName
) {
2272 this.setCard( name
);
2279 * Handle stack layout set events.
2282 * @param {OO.ui.PanelLayout|null} card The card panel that is now the current panel
2284 OO
.ui
.IndexLayout
.prototype.onStackLayoutSet = function ( card
) {
2287 card
.scrollElementIntoView( {
2288 complete: function () {
2289 if ( layout
.autoFocus
) {
2298 * Focus the first input in the current card.
2300 * If no card is selected, the first selectable card will be selected.
2301 * If the focus is already in an element on the current card, nothing will happen.
2303 * @param {number} [itemIndex] A specific item to focus on
2305 OO
.ui
.IndexLayout
.prototype.focus = function ( itemIndex
) {
2307 items
= this.stackLayout
.getItems();
2309 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2310 card
= items
[ itemIndex
];
2312 card
= this.stackLayout
.getCurrentItem();
2316 this.selectFirstSelectableCard();
2317 card
= this.stackLayout
.getCurrentItem();
2322 // Only change the focus if is not already in the current page
2323 if ( !OO
.ui
.contains( card
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
2329 * Find the first focusable input in the index layout and focus
2332 OO
.ui
.IndexLayout
.prototype.focusFirstFocusable = function () {
2333 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2337 * Handle tab widget select events.
2340 * @param {OO.ui.OptionWidget|null} item Selected item
2342 OO
.ui
.IndexLayout
.prototype.onTabSelectWidgetSelect = function ( item
) {
2344 this.setCard( item
.getData() );
2349 * Get the card closest to the specified card.
2351 * @param {OO.ui.CardLayout} card Card to use as a reference point
2352 * @return {OO.ui.CardLayout|null} Card closest to the specified card
2354 OO
.ui
.IndexLayout
.prototype.getClosestCard = function ( card
) {
2355 var next
, prev
, level
,
2356 cards
= this.stackLayout
.getItems(),
2357 index
= cards
.indexOf( card
);
2359 if ( index
!== -1 ) {
2360 next
= cards
[ index
+ 1 ];
2361 prev
= cards
[ index
- 1 ];
2362 // Prefer adjacent cards at the same level
2363 level
= this.tabSelectWidget
.getItemFromData( card
.getName() ).getLevel();
2366 level
=== this.tabSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
2372 level
=== this.tabSelectWidget
.getItemFromData( next
.getName() ).getLevel()
2377 return prev
|| next
|| null;
2381 * Get the tabs widget.
2383 * @return {OO.ui.TabSelectWidget} Tabs widget
2385 OO
.ui
.IndexLayout
.prototype.getTabs = function () {
2386 return this.tabSelectWidget
;
2390 * Get a card by its symbolic name.
2392 * @param {string} name Symbolic name of card
2393 * @return {OO.ui.CardLayout|undefined} Card, if found
2395 OO
.ui
.IndexLayout
.prototype.getCard = function ( name
) {
2396 return this.cards
[ name
];
2400 * Get the current card.
2402 * @return {OO.ui.CardLayout|undefined} Current card, if found
2404 OO
.ui
.IndexLayout
.prototype.getCurrentCard = function () {
2405 var name
= this.getCurrentCardName();
2406 return name
? this.getCard( name
) : undefined;
2410 * Get the symbolic name of the current card.
2412 * @return {string|null} Symbolic name of the current card
2414 OO
.ui
.IndexLayout
.prototype.getCurrentCardName = function () {
2415 return this.currentCardName
;
2419 * Add cards to the index layout
2421 * When cards are added with the same names as existing cards, the existing cards will be
2422 * automatically removed before the new cards are added.
2424 * @param {OO.ui.CardLayout[]} cards Cards to add
2425 * @param {number} index Index of the insertion point
2429 OO
.ui
.IndexLayout
.prototype.addCards = function ( cards
, index
) {
2430 var i
, len
, name
, card
, item
, currentIndex
,
2431 stackLayoutCards
= this.stackLayout
.getItems(),
2435 // Remove cards with same names
2436 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2438 name
= card
.getName();
2440 if ( Object
.prototype.hasOwnProperty
.call( this.cards
, name
) ) {
2441 // Correct the insertion index
2442 currentIndex
= stackLayoutCards
.indexOf( this.cards
[ name
] );
2443 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2446 remove
.push( this.cards
[ name
] );
2449 if ( remove
.length
) {
2450 this.removeCards( remove
);
2454 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2456 name
= card
.getName();
2457 this.cards
[ card
.getName() ] = card
;
2458 item
= new OO
.ui
.TabOptionWidget( { data
: name
} );
2459 card
.setTabItem( item
);
2463 if ( items
.length
) {
2464 this.tabSelectWidget
.addItems( items
, index
);
2465 this.selectFirstSelectableCard();
2467 this.stackLayout
.addItems( cards
, index
);
2468 this.emit( 'add', cards
, index
);
2474 * Remove the specified cards from the index layout.
2476 * To remove all cards from the index, you may wish to use the #clearCards method instead.
2478 * @param {OO.ui.CardLayout[]} cards An array of cards to remove
2482 OO
.ui
.IndexLayout
.prototype.removeCards = function ( cards
) {
2483 var i
, len
, name
, card
,
2486 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2488 name
= card
.getName();
2489 delete this.cards
[ name
];
2490 items
.push( this.tabSelectWidget
.getItemFromData( name
) );
2491 card
.setTabItem( null );
2493 if ( items
.length
) {
2494 this.tabSelectWidget
.removeItems( items
);
2495 this.selectFirstSelectableCard();
2497 this.stackLayout
.removeItems( cards
);
2498 this.emit( 'remove', cards
);
2504 * Clear all cards from the index layout.
2506 * To remove only a subset of cards from the index, use the #removeCards method.
2511 OO
.ui
.IndexLayout
.prototype.clearCards = function () {
2513 cards
= this.stackLayout
.getItems();
2516 this.currentCardName
= null;
2517 this.tabSelectWidget
.clearItems();
2518 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2519 cards
[ i
].setTabItem( null );
2521 this.stackLayout
.clearItems();
2523 this.emit( 'remove', cards
);
2529 * Set the current card by symbolic name.
2532 * @param {string} name Symbolic name of card
2534 OO
.ui
.IndexLayout
.prototype.setCard = function ( name
) {
2537 card
= this.cards
[ name
],
2538 previousCard
= this.currentCardName
&& this.cards
[ this.currentCardName
];
2540 if ( name
!== this.currentCardName
) {
2541 selectedItem
= this.tabSelectWidget
.getSelectedItem();
2542 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2543 this.tabSelectWidget
.selectItemByData( name
);
2546 if ( previousCard
) {
2547 previousCard
.setActive( false );
2548 // Blur anything focused if the next card doesn't have anything focusable.
2549 // This is not needed if the next card has something focusable (because once it is focused
2550 // this blur happens automatically). If the layout is non-continuous, this check is
2551 // meaningless because the next card is not visible yet and thus can't hold focus.
2554 this.stackLayout
.continuous
&&
2555 OO
.ui
.findFocusable( card
.$element
).length
!== 0
2557 $focused
= previousCard
.$element
.find( ':focus' );
2558 if ( $focused
.length
) {
2559 $focused
[ 0 ].blur();
2563 this.currentCardName
= name
;
2564 card
.setActive( true );
2565 this.stackLayout
.setItem( card
);
2566 if ( !this.stackLayout
.continuous
&& previousCard
) {
2567 // This should not be necessary, since any inputs on the previous card should have been
2568 // blurred when it was hidden, but browsers are not very consistent about this.
2569 $focused
= previousCard
.$element
.find( ':focus' );
2570 if ( $focused
.length
) {
2571 $focused
[ 0 ].blur();
2574 this.emit( 'set', card
);
2580 * Select the first selectable card.
2584 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableCard = function () {
2585 if ( !this.tabSelectWidget
.getSelectedItem() ) {
2586 this.tabSelectWidget
.selectItem( this.tabSelectWidget
.getFirstSelectableItem() );
2593 * ToggleWidget implements basic behavior of widgets with an on/off state.
2594 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2598 * @extends OO.ui.Widget
2601 * @param {Object} [config] Configuration options
2602 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2603 * By default, the toggle is in the 'off' state.
2605 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
2606 // Configuration initialization
2607 config
= config
|| {};
2609 // Parent constructor
2610 OO
.ui
.ToggleWidget
.parent
.call( this, config
);
2616 this.$element
.addClass( 'oo-ui-toggleWidget' );
2617 this.setValue( !!config
.value
);
2622 OO
.inheritClass( OO
.ui
.ToggleWidget
, OO
.ui
.Widget
);
2629 * A change event is emitted when the on/off state of the toggle changes.
2631 * @param {boolean} value Value representing the new state of the toggle
2637 * Get the value representing the toggle’s state.
2639 * @return {boolean} The on/off state of the toggle
2641 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
2646 * Set the state of the toggle: `true` for 'on', `false' for 'off'.
2648 * @param {boolean} value The state of the toggle
2652 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
2654 if ( this.value
!== value
) {
2656 this.emit( 'change', value
);
2657 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
2658 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
2659 this.$element
.attr( 'aria-checked', value
.toString() );
2665 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
2666 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
2667 * configured with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators},
2668 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
2669 * and {@link OO.ui.mixin.LabelElement labels}. Please see
2670 * the [OOjs UI documentation][1] on MediaWiki for more information.
2673 * // Toggle buttons in the 'off' and 'on' state.
2674 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
2675 * label: 'Toggle Button off'
2677 * var toggleButton2 = new OO.ui.ToggleButtonWidget( {
2678 * label: 'Toggle Button on',
2681 * // Append the buttons to the DOM.
2682 * $( 'body' ).append( toggleButton1.$element, toggleButton2.$element );
2684 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Toggle_buttons
2687 * @extends OO.ui.ToggleWidget
2688 * @mixins OO.ui.mixin.ButtonElement
2689 * @mixins OO.ui.mixin.IconElement
2690 * @mixins OO.ui.mixin.IndicatorElement
2691 * @mixins OO.ui.mixin.LabelElement
2692 * @mixins OO.ui.mixin.TitledElement
2693 * @mixins OO.ui.mixin.FlaggedElement
2694 * @mixins OO.ui.mixin.TabIndexedElement
2697 * @param {Object} [config] Configuration options
2698 * @cfg {boolean} [value=false] The toggle button’s initial on/off
2699 * state. By default, the button is in the 'off' state.
2701 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
2702 // Configuration initialization
2703 config
= config
|| {};
2705 // Parent constructor
2706 OO
.ui
.ToggleButtonWidget
.parent
.call( this, config
);
2708 // Mixin constructors
2709 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
2710 OO
.ui
.mixin
.IconElement
.call( this, config
);
2711 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
2712 OO
.ui
.mixin
.LabelElement
.call( this, config
);
2713 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
2714 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
2715 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
2718 this.connect( this, { click
: 'onAction' } );
2721 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
2723 .addClass( 'oo-ui-toggleButtonWidget' )
2724 .append( this.$button
);
2729 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
2730 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
2731 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IconElement
);
2732 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
2733 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.LabelElement
);
2734 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TitledElement
);
2735 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
2736 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2741 * Handle the button action being triggered.
2745 OO
.ui
.ToggleButtonWidget
.prototype.onAction = function () {
2746 this.setValue( !this.value
);
2752 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
2754 if ( value
!== this.value
) {
2755 // Might be called from parent constructor before ButtonElement constructor
2756 if ( this.$button
) {
2757 this.$button
.attr( 'aria-pressed', value
.toString() );
2759 this.setActive( value
);
2763 OO
.ui
.ToggleButtonWidget
.parent
.prototype.setValue
.call( this, value
);
2771 OO
.ui
.ToggleButtonWidget
.prototype.setButtonElement = function ( $button
) {
2772 if ( this.$button
) {
2773 this.$button
.removeAttr( 'aria-pressed' );
2775 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement
.call( this, $button
);
2776 this.$button
.attr( 'aria-pressed', this.value
.toString() );
2780 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
2781 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
2782 * visually by a slider in the leftmost position.
2785 * // Toggle switches in the 'off' and 'on' position.
2786 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget();
2787 * var toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
2791 * // Create a FieldsetLayout to layout and label switches
2792 * var fieldset = new OO.ui.FieldsetLayout( {
2793 * label: 'Toggle switches'
2795 * fieldset.addItems( [
2796 * new OO.ui.FieldLayout( toggleSwitch1, { label: 'Off', align: 'top' } ),
2797 * new OO.ui.FieldLayout( toggleSwitch2, { label: 'On', align: 'top' } )
2799 * $( 'body' ).append( fieldset.$element );
2802 * @extends OO.ui.ToggleWidget
2803 * @mixins OO.ui.mixin.TabIndexedElement
2806 * @param {Object} [config] Configuration options
2807 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
2808 * By default, the toggle switch is in the 'off' position.
2810 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
2811 // Parent constructor
2812 OO
.ui
.ToggleSwitchWidget
.parent
.call( this, config
);
2814 // Mixin constructors
2815 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
2818 this.dragging
= false;
2819 this.dragStart
= null;
2820 this.sliding
= false;
2821 this.$glow
= $( '<span>' );
2822 this.$grip
= $( '<span>' );
2826 click
: this.onClick
.bind( this ),
2827 keypress
: this.onKeyPress
.bind( this )
2831 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
2832 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
2834 .addClass( 'oo-ui-toggleSwitchWidget' )
2835 .attr( 'role', 'checkbox' )
2836 .append( this.$glow
, this.$grip
);
2841 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
2842 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2847 * Handle mouse click events.
2850 * @param {jQuery.Event} e Mouse click event
2852 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
2853 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
2854 this.setValue( !this.value
);
2860 * Handle key press events.
2863 * @param {jQuery.Event} e Key press event
2865 OO
.ui
.ToggleSwitchWidget
.prototype.onKeyPress = function ( e
) {
2866 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
2867 this.setValue( !this.value
);
2873 * OutlineControlsWidget is a set of controls for an {@link OO.ui.OutlineSelectWidget outline select widget}.
2874 * Controls include moving items up and down, removing items, and adding different kinds of items.
2876 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
2879 * @extends OO.ui.Widget
2880 * @mixins OO.ui.mixin.GroupElement
2881 * @mixins OO.ui.mixin.IconElement
2884 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
2885 * @param {Object} [config] Configuration options
2886 * @cfg {Object} [abilities] List of abilties
2887 * @cfg {boolean} [abilities.move=true] Allow moving movable items
2888 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
2890 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
2891 // Allow passing positional parameters inside the config object
2892 if ( OO
.isPlainObject( outline
) && config
=== undefined ) {
2894 outline
= config
.outline
;
2897 // Configuration initialization
2898 config
= $.extend( { icon
: 'add' }, config
);
2900 // Parent constructor
2901 OO
.ui
.OutlineControlsWidget
.parent
.call( this, config
);
2903 // Mixin constructors
2904 OO
.ui
.mixin
.GroupElement
.call( this, config
);
2905 OO
.ui
.mixin
.IconElement
.call( this, config
);
2908 this.outline
= outline
;
2909 this.$movers
= $( '<div>' );
2910 this.upButton
= new OO
.ui
.ButtonWidget( {
2913 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
2915 this.downButton
= new OO
.ui
.ButtonWidget( {
2918 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
2920 this.removeButton
= new OO
.ui
.ButtonWidget( {
2923 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
2925 this.abilities
= { move: true, remove
: true };
2928 outline
.connect( this, {
2929 select
: 'onOutlineChange',
2930 add
: 'onOutlineChange',
2931 remove
: 'onOutlineChange'
2933 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
2934 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
2935 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
2938 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
2939 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
2941 .addClass( 'oo-ui-outlineControlsWidget-movers' )
2942 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
2943 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
2944 this.setAbilities( config
.abilities
|| {} );
2949 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
2950 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.GroupElement
);
2951 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.IconElement
);
2957 * @param {number} places Number of places to move
2969 * @param {Object} abilities List of abilties
2970 * @param {boolean} [abilities.move] Allow moving movable items
2971 * @param {boolean} [abilities.remove] Allow removing removable items
2973 OO
.ui
.OutlineControlsWidget
.prototype.setAbilities = function ( abilities
) {
2976 for ( ability
in this.abilities
) {
2977 if ( abilities
[ ability
] !== undefined ) {
2978 this.abilities
[ ability
] = !!abilities
[ ability
];
2982 this.onOutlineChange();
2986 * Handle outline change events.
2990 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
2991 var i
, len
, firstMovable
, lastMovable
,
2992 items
= this.outline
.getItems(),
2993 selectedItem
= this.outline
.getSelectedItem(),
2994 movable
= this.abilities
.move && selectedItem
&& selectedItem
.isMovable(),
2995 removable
= this.abilities
.remove
&& selectedItem
&& selectedItem
.isRemovable();
3000 while ( ++i
< len
) {
3001 if ( items
[ i
].isMovable() ) {
3002 firstMovable
= items
[ i
];
3008 if ( items
[ i
].isMovable() ) {
3009 lastMovable
= items
[ i
];
3014 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
3015 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
3016 this.removeButton
.setDisabled( !removable
);
3020 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3022 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3023 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3027 * @extends OO.ui.DecoratedOptionWidget
3030 * @param {Object} [config] Configuration options
3031 * @cfg {number} [level] Indentation level
3032 * @cfg {boolean} [movable] Allow modification from {@link OO.ui.OutlineControlsWidget outline controls}.
3034 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
3035 // Configuration initialization
3036 config
= config
|| {};
3038 // Parent constructor
3039 OO
.ui
.OutlineOptionWidget
.parent
.call( this, config
);
3043 this.movable
= !!config
.movable
;
3044 this.removable
= !!config
.removable
;
3047 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
3048 this.setLevel( config
.level
);
3053 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3055 /* Static Properties */
3057 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
3059 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
3061 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
3063 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
3068 * Check if item is movable.
3070 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3072 * @return {boolean} Item is movable
3074 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
3075 return this.movable
;
3079 * Check if item is removable.
3081 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3083 * @return {boolean} Item is removable
3085 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
3086 return this.removable
;
3090 * Get indentation level.
3092 * @return {number} Indentation level
3094 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
3101 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3103 * @param {boolean} movable Item is movable
3106 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
3107 this.movable
= !!movable
;
3108 this.updateThemeClasses();
3115 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3117 * @param {boolean} removable Item is removable
3120 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
3121 this.removable
= !!removable
;
3122 this.updateThemeClasses();
3127 * Set indentation level.
3129 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3132 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
3133 var levels
= this.constructor.static.levels
,
3134 levelClass
= this.constructor.static.levelClass
,
3137 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
3139 if ( this.level
=== i
) {
3140 this.$element
.addClass( levelClass
+ i
);
3142 this.$element
.removeClass( levelClass
+ i
);
3145 this.updateThemeClasses();
3151 * OutlineSelectWidget is a structured list that contains {@link OO.ui.OutlineOptionWidget outline options}
3152 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls} widget.
3154 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3157 * @extends OO.ui.SelectWidget
3158 * @mixins OO.ui.mixin.TabIndexedElement
3161 * @param {Object} [config] Configuration options
3163 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
3164 // Parent constructor
3165 OO
.ui
.OutlineSelectWidget
.parent
.call( this, config
);
3167 // Mixin constructors
3168 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3172 focus
: this.bindKeyDownListener
.bind( this ),
3173 blur
: this.unbindKeyDownListener
.bind( this )
3177 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
3182 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
3183 OO
.mixinClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3186 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3187 * can be selected and configured with data. The class is
3188 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3189 * [OOjs UI documentation on MediaWiki] [1] for more information.
3191 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Button_selects_and_options
3194 * @extends OO.ui.OptionWidget
3195 * @mixins OO.ui.mixin.ButtonElement
3196 * @mixins OO.ui.mixin.IconElement
3197 * @mixins OO.ui.mixin.IndicatorElement
3198 * @mixins OO.ui.mixin.TitledElement
3201 * @param {Object} [config] Configuration options
3203 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
3204 // Configuration initialization
3205 config
= config
|| {};
3207 // Parent constructor
3208 OO
.ui
.ButtonOptionWidget
.parent
.call( this, config
);
3210 // Mixin constructors
3211 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3212 OO
.ui
.mixin
.IconElement
.call( this, config
);
3213 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3214 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
3217 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
3218 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3219 this.$element
.append( this.$button
);
3224 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
3225 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.ButtonElement
);
3226 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IconElement
);
3227 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IndicatorElement
);
3228 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.TitledElement
);
3230 /* Static Properties */
3232 // Allow button mouse down events to pass through so they can be handled by the parent select widget
3233 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
3235 OO
.ui
.ButtonOptionWidget
.static.highlightable
= false;
3242 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
3243 OO
.ui
.ButtonOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3245 if ( this.constructor.static.selectable
) {
3246 this.setActive( state
);
3253 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3254 * button options and is used together with
3255 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3256 * highlighting, choosing, and selecting mutually exclusive options. Please see
3257 * the [OOjs UI documentation on MediaWiki] [1] for more information.
3260 * // Example: A ButtonSelectWidget that contains three ButtonOptionWidgets
3261 * var option1 = new OO.ui.ButtonOptionWidget( {
3263 * label: 'Option 1',
3264 * title: 'Button option 1'
3267 * var option2 = new OO.ui.ButtonOptionWidget( {
3269 * label: 'Option 2',
3270 * title: 'Button option 2'
3273 * var option3 = new OO.ui.ButtonOptionWidget( {
3275 * label: 'Option 3',
3276 * title: 'Button option 3'
3279 * var buttonSelect=new OO.ui.ButtonSelectWidget( {
3280 * items: [ option1, option2, option3 ]
3282 * $( 'body' ).append( buttonSelect.$element );
3284 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
3287 * @extends OO.ui.SelectWidget
3288 * @mixins OO.ui.mixin.TabIndexedElement
3291 * @param {Object} [config] Configuration options
3293 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
3294 // Parent constructor
3295 OO
.ui
.ButtonSelectWidget
.parent
.call( this, config
);
3297 // Mixin constructors
3298 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3302 focus
: this.bindKeyDownListener
.bind( this ),
3303 blur
: this.unbindKeyDownListener
.bind( this )
3307 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
3312 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
3313 OO
.mixinClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3316 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3318 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3319 * {@link OO.ui.CardLayout card layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3323 * @extends OO.ui.OptionWidget
3326 * @param {Object} [config] Configuration options
3328 OO
.ui
.TabOptionWidget
= function OoUiTabOptionWidget( config
) {
3329 // Configuration initialization
3330 config
= config
|| {};
3332 // Parent constructor
3333 OO
.ui
.TabOptionWidget
.parent
.call( this, config
);
3336 this.$element
.addClass( 'oo-ui-tabOptionWidget' );
3341 OO
.inheritClass( OO
.ui
.TabOptionWidget
, OO
.ui
.OptionWidget
);
3343 /* Static Properties */
3345 OO
.ui
.TabOptionWidget
.static.highlightable
= false;
3348 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3350 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3353 * @extends OO.ui.SelectWidget
3354 * @mixins OO.ui.mixin.TabIndexedElement
3357 * @param {Object} [config] Configuration options
3359 OO
.ui
.TabSelectWidget
= function OoUiTabSelectWidget( config
) {
3360 // Parent constructor
3361 OO
.ui
.TabSelectWidget
.parent
.call( this, config
);
3363 // Mixin constructors
3364 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3368 focus
: this.bindKeyDownListener
.bind( this ),
3369 blur
: this.unbindKeyDownListener
.bind( this )
3373 this.$element
.addClass( 'oo-ui-tabSelectWidget' );
3378 OO
.inheritClass( OO
.ui
.TabSelectWidget
, OO
.ui
.SelectWidget
);
3379 OO
.mixinClass( OO
.ui
.TabSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3382 * CapsuleItemWidgets are used within a {@link OO.ui.CapsuleMultiselectWidget
3383 * CapsuleMultiselectWidget} to display the selected items.
3386 * @extends OO.ui.Widget
3387 * @mixins OO.ui.mixin.ItemWidget
3388 * @mixins OO.ui.mixin.LabelElement
3389 * @mixins OO.ui.mixin.FlaggedElement
3390 * @mixins OO.ui.mixin.TabIndexedElement
3393 * @param {Object} [config] Configuration options
3395 OO
.ui
.CapsuleItemWidget
= function OoUiCapsuleItemWidget( config
) {
3396 // Configuration initialization
3397 config
= config
|| {};
3399 // Parent constructor
3400 OO
.ui
.CapsuleItemWidget
.parent
.call( this, config
);
3402 // Mixin constructors
3403 OO
.ui
.mixin
.ItemWidget
.call( this );
3404 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3405 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3406 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3409 this.closeButton
= new OO
.ui
.ButtonWidget( {
3413 } ).on( 'click', this.onCloseClick
.bind( this ) );
3415 this.on( 'disable', function ( disabled
) {
3416 this.closeButton
.setDisabled( disabled
);
3422 click
: this.onClick
.bind( this ),
3423 keydown
: this.onKeyDown
.bind( this )
3425 .addClass( 'oo-ui-capsuleItemWidget' )
3426 .append( this.$label
, this.closeButton
.$element
);
3431 OO
.inheritClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.Widget
);
3432 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.ItemWidget
);
3433 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.LabelElement
);
3434 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
3435 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3440 * Handle close icon clicks
3442 OO
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
3443 var element
= this.getElementGroup();
3445 if ( element
&& $.isFunction( element
.removeItems
) ) {
3446 element
.removeItems( [ this ] );
3452 * Handle click event for the entire capsule
3454 OO
.ui
.CapsuleItemWidget
.prototype.onClick = function () {
3455 var element
= this.getElementGroup();
3457 if ( !this.isDisabled() && element
&& $.isFunction( element
.editItem
) ) {
3458 element
.editItem( this );
3463 * Handle keyDown event for the entire capsule
3465 OO
.ui
.CapsuleItemWidget
.prototype.onKeyDown = function ( e
) {
3466 var element
= this.getElementGroup();
3468 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
) {
3469 element
.removeItems( [ this ] );
3472 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
3473 element
.editItem( this );
3475 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
3476 element
.getPreviousItem( this ).focus();
3477 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
3478 element
.getNextItem( this ).focus();
3483 * Focuses the capsule
3485 OO
.ui
.CapsuleItemWidget
.prototype.focus = function () {
3486 this.$element
.focus();
3490 * CapsuleMultiselectWidgets are something like a {@link OO.ui.ComboBoxInputWidget combo box widget}
3491 * that allows for selecting multiple values.
3493 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
3496 * // Example: A CapsuleMultiselectWidget.
3497 * var capsule = new OO.ui.CapsuleMultiselectWidget( {
3498 * label: 'CapsuleMultiselectWidget',
3499 * selected: [ 'Option 1', 'Option 3' ],
3502 * new OO.ui.MenuOptionWidget( {
3504 * label: 'Option One'
3506 * new OO.ui.MenuOptionWidget( {
3508 * label: 'Option Two'
3510 * new OO.ui.MenuOptionWidget( {
3512 * label: 'Option Three'
3514 * new OO.ui.MenuOptionWidget( {
3516 * label: 'Option Four'
3518 * new OO.ui.MenuOptionWidget( {
3520 * label: 'Option Five'
3525 * $( 'body' ).append( capsule.$element );
3527 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
3530 * @extends OO.ui.Widget
3531 * @mixins OO.ui.mixin.GroupElement
3532 * @mixins OO.ui.mixin.PopupElement
3533 * @mixins OO.ui.mixin.TabIndexedElement
3534 * @mixins OO.ui.mixin.IndicatorElement
3535 * @mixins OO.ui.mixin.IconElement
3536 * @uses OO.ui.CapsuleItemWidget
3537 * @uses OO.ui.FloatingMenuSelectWidget
3540 * @param {Object} [config] Configuration options
3541 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if not present in the menu.
3542 * @cfg {Object} [menu] (required) Configuration options to pass to the
3543 * {@link OO.ui.MenuSelectWidget menu select widget}.
3544 * @cfg {Object} [popup] Configuration options to pass to the {@link OO.ui.PopupWidget popup widget}.
3545 * If specified, this popup will be shown instead of the menu (but the menu
3546 * will still be used for item labels and allowArbitrary=false). The widgets
3547 * in the popup should use {@link #addItemsFromData} or {@link #addItems} as necessary.
3548 * @cfg {jQuery} [$overlay=this.$element] Render the menu or popup into a separate layer.
3549 * This configuration is useful in cases where the expanded menu is larger than
3550 * its containing `<div>`. The specified overlay layer is usually on top of
3551 * the containing `<div>` and has a larger area. By default, the menu uses
3552 * relative positioning.
3554 OO
.ui
.CapsuleMultiselectWidget
= function OoUiCapsuleMultiselectWidget( config
) {
3557 // Parent constructor
3558 OO
.ui
.CapsuleMultiselectWidget
.parent
.call( this, config
);
3560 // Configuration initialization
3561 config
= $.extend( {
3562 allowArbitrary
: false,
3563 $overlay
: this.$element
3566 // Properties (must be set before mixin constructor calls)
3567 this.$input
= config
.popup
? null : $( '<input>' );
3568 this.$handle
= $( '<div>' );
3570 // Mixin constructors
3571 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3572 if ( config
.popup
) {
3573 config
.popup
= $.extend( {}, config
.popup
, {
3577 OO
.ui
.mixin
.PopupElement
.call( this, config
);
3578 $tabFocus
= $( '<span>' );
3579 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: $tabFocus
} ) );
3583 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
3585 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3586 OO
.ui
.mixin
.IconElement
.call( this, config
);
3589 this.$content
= $( '<div>' );
3590 this.allowArbitrary
= config
.allowArbitrary
;
3591 this.$overlay
= config
.$overlay
;
3592 this.menu
= new OO
.ui
.FloatingMenuSelectWidget( $.extend(
3595 $input
: this.$input
,
3596 $container
: this.$element
,
3597 filterFromInput
: true,
3598 disabled
: this.isDisabled()
3606 focus
: this.onFocusForPopup
.bind( this )
3608 this.popup
.$element
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3609 if ( this.popup
.$autoCloseIgnore
) {
3610 this.popup
.$autoCloseIgnore
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3612 this.popup
.connect( this, {
3613 toggle: function ( visible
) {
3614 $tabFocus
.toggle( !visible
);
3619 focus
: this.onInputFocus
.bind( this ),
3620 blur
: this.onInputBlur
.bind( this ),
3621 'propertychange change click mouseup keydown keyup input cut paste select focus':
3622 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
3623 keydown
: this.onKeyDown
.bind( this ),
3624 keypress
: this.onKeyPress
.bind( this )
3627 this.menu
.connect( this, {
3628 choose
: 'onMenuChoose',
3629 add
: 'onMenuItemsChange',
3630 remove
: 'onMenuItemsChange'
3633 mousedown
: this.onMouseDown
.bind( this )
3637 if ( this.$input
) {
3638 this.$input
.prop( 'disabled', this.isDisabled() );
3641 'aria-autocomplete': 'list'
3643 this.updateInputSize();
3645 if ( config
.data
) {
3646 this.setItemsFromData( config
.data
);
3648 this.$content
.addClass( 'oo-ui-capsuleMultiselectWidget-content' )
3649 .append( this.$group
);
3650 this.$group
.addClass( 'oo-ui-capsuleMultiselectWidget-group' );
3651 this.$handle
.addClass( 'oo-ui-capsuleMultiselectWidget-handle' )
3652 .append( this.$indicator
, this.$icon
, this.$content
);
3653 this.$element
.addClass( 'oo-ui-capsuleMultiselectWidget' )
3654 .append( this.$handle
);
3656 this.$content
.append( $tabFocus
);
3657 this.$overlay
.append( this.popup
.$element
);
3659 this.$content
.append( this.$input
);
3660 this.$overlay
.append( this.menu
.$element
);
3662 this.onMenuItemsChange();
3667 OO
.inheritClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.Widget
);
3668 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.GroupElement
);
3669 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
3670 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3671 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
3672 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
3679 * A change event is emitted when the set of selected items changes.
3681 * @param {Mixed[]} datas Data of the now-selected items
3687 * A resize event is emitted when the widget's dimensions change to accomodate newly added items or
3688 * current user input.
3694 * Construct a OO.ui.CapsuleItemWidget (or a subclass thereof) from given label and data.
3697 * @param {Mixed} data Custom data of any type.
3698 * @param {string} label The label text.
3699 * @return {OO.ui.CapsuleItemWidget}
3701 OO
.ui
.CapsuleMultiselectWidget
.prototype.createItemWidget = function ( data
, label
) {
3702 return new OO
.ui
.CapsuleItemWidget( { data
: data
, label
: label
} );
3706 * Get the data of the items in the capsule
3710 OO
.ui
.CapsuleMultiselectWidget
.prototype.getItemsData = function () {
3711 return this.getItems().map( function ( item
) {
3717 * Set the items in the capsule by providing data
3720 * @param {Mixed[]} datas
3721 * @return {OO.ui.CapsuleMultiselectWidget}
3723 OO
.ui
.CapsuleMultiselectWidget
.prototype.setItemsFromData = function ( datas
) {
3726 items
= this.getItems();
3728 $.each( datas
, function ( i
, data
) {
3730 item
= menu
.getItemFromData( data
);
3734 } else if ( widget
.allowArbitrary
) {
3735 label
= String( data
);
3741 for ( j
= 0; j
< items
.length
; j
++ ) {
3742 if ( items
[ j
].data
=== data
&& items
[ j
].label
=== label
) {
3744 items
.splice( j
, 1 );
3749 item
= widget
.createItemWidget( data
, label
);
3751 widget
.addItems( [ item
], i
);
3754 if ( items
.length
) {
3755 widget
.removeItems( items
);
3762 * Add items to the capsule by providing their data
3765 * @param {Mixed[]} datas
3766 * @return {OO.ui.CapsuleMultiselectWidget}
3768 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemsFromData = function ( datas
) {
3773 $.each( datas
, function ( i
, data
) {
3776 if ( !widget
.getItemFromData( data
) ) {
3777 item
= menu
.getItemFromData( data
);
3779 items
.push( widget
.createItemWidget( data
, item
.label
) );
3780 } else if ( widget
.allowArbitrary
) {
3781 items
.push( widget
.createItemWidget( data
, String( data
) ) );
3786 if ( items
.length
) {
3787 this.addItems( items
);
3794 * Add items to the capsule by providing a label
3796 * @param {string} label
3797 * @return {boolean} Whether the item was added or not
3799 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemFromLabel = function ( label
) {
3800 var item
= this.menu
.getItemFromLabel( label
, true );
3802 this.addItemsFromData( [ item
.data
] );
3804 } else if ( this.allowArbitrary
&& this.$input
.val().trim() !== '' ) {
3805 this.addItemsFromData( [ label
] );
3812 * Remove items by data
3815 * @param {Mixed[]} datas
3816 * @return {OO.ui.CapsuleMultiselectWidget}
3818 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItemsFromData = function ( datas
) {
3822 $.each( datas
, function ( i
, data
) {
3823 var item
= widget
.getItemFromData( data
);
3829 if ( items
.length
) {
3830 this.removeItems( items
);
3839 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItems = function ( items
) {
3841 oldItems
= this.items
.slice();
3843 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
);
3845 if ( this.items
.length
!== oldItems
.length
) {
3849 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
3850 same
= same
&& this.items
[ i
] === oldItems
[ i
];
3854 this.emit( 'change', this.getItemsData() );
3855 this.updateIfHeightChanged();
3862 * Removes the item from the list and copies its label to `this.$input`.
3864 * @param {Object} item
3866 OO
.ui
.CapsuleMultiselectWidget
.prototype.editItem = function ( item
) {
3867 this.$input
.val( item
.label
);
3868 this.updateInputSize();
3870 this.removeItems( [ item
] );
3876 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItems = function ( items
) {
3878 oldItems
= this.items
.slice();
3880 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
3882 if ( this.items
.length
!== oldItems
.length
) {
3886 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
3887 same
= same
&& this.items
[ i
] === oldItems
[ i
];
3891 this.emit( 'change', this.getItemsData() );
3892 this.updateIfHeightChanged();
3901 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearItems = function () {
3902 if ( this.items
.length
) {
3903 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
3904 this.emit( 'change', this.getItemsData() );
3905 this.updateIfHeightChanged();
3911 * Given an item, returns the item after it. If its the last item,
3912 * returns `this.$input`. If no item is passed, returns the very first
3915 * @param {OO.ui.CapsuleItemWidget} [item]
3916 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
3918 OO
.ui
.CapsuleMultiselectWidget
.prototype.getNextItem = function ( item
) {
3921 if ( item
=== undefined ) {
3922 return this.items
[ 0 ];
3925 itemIndex
= this.items
.indexOf( item
);
3926 if ( itemIndex
< 0 ) { // Item not in list
3928 } else if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
3931 return this.items
[ itemIndex
+ 1 ];
3936 * Given an item, returns the item before it. If its the first item,
3937 * returns `this.$input`. If no item is passed, returns the very last
3940 * @param {OO.ui.CapsuleItemWidget} [item]
3941 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
3943 OO
.ui
.CapsuleMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
3946 if ( item
=== undefined ) {
3947 return this.items
[ this.items
.length
- 1 ];
3950 itemIndex
= this.items
.indexOf( item
);
3951 if ( itemIndex
< 0 ) { // Item not in list
3953 } else if ( itemIndex
=== 0 ) { // First item
3956 return this.items
[ itemIndex
- 1 ];
3961 * Get the capsule widget's menu.
3963 * @return {OO.ui.MenuSelectWidget} Menu widget
3965 OO
.ui
.CapsuleMultiselectWidget
.prototype.getMenu = function () {
3970 * Handle focus events
3973 * @param {jQuery.Event} event
3975 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputFocus = function () {
3976 if ( !this.isDisabled() ) {
3977 this.menu
.toggle( true );
3982 * Handle blur events
3985 * @param {jQuery.Event} event
3987 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputBlur = function () {
3988 this.addItemFromLabel( this.$input
.val() );
3993 * Handle focus events
3996 * @param {jQuery.Event} event
3998 OO
.ui
.CapsuleMultiselectWidget
.prototype.onFocusForPopup = function () {
3999 if ( !this.isDisabled() ) {
4000 this.popup
.setSize( this.$handle
.width() );
4001 this.popup
.toggle( true );
4002 OO
.ui
.findFocusable( this.popup
.$element
).focus();
4007 * Handles popup focus out events.
4010 * @param {jQuery.Event} e Focus out event
4012 OO
.ui
.CapsuleMultiselectWidget
.prototype.onPopupFocusOut = function () {
4013 var widget
= this.popup
;
4015 setTimeout( function () {
4017 widget
.isVisible() &&
4018 !OO
.ui
.contains( widget
.$element
[ 0 ], document
.activeElement
, true ) &&
4019 ( !widget
.$autoCloseIgnore
|| !widget
.$autoCloseIgnore
.has( document
.activeElement
).length
)
4021 widget
.toggle( false );
4027 * Handle mouse down events.
4030 * @param {jQuery.Event} e Mouse down event
4032 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMouseDown = function ( e
) {
4033 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
4037 this.updateInputSize();
4042 * Handle key press events.
4045 * @param {jQuery.Event} e Key press event
4047 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyPress = function ( e
) {
4048 if ( !this.isDisabled() ) {
4049 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
4054 if ( !this.popup
) {
4055 this.menu
.toggle( true );
4056 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
4057 if ( this.addItemFromLabel( this.$input
.val() ) ) {
4063 // Make sure the input gets resized.
4064 setTimeout( this.updateInputSize
.bind( this ), 0 );
4070 * Handle key down events.
4073 * @param {jQuery.Event} e Key down event
4075 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyDown = function ( e
) {
4077 !this.isDisabled() &&
4078 this.$input
.val() === '' &&
4081 // 'keypress' event is not triggered for Backspace
4082 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
4083 if ( e
.metaKey
|| e
.ctrlKey
) {
4084 this.removeItems( this.items
.slice( -1 ) );
4086 this.editItem( this.items
[ this.items
.length
- 1 ] );
4089 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
4090 this.getPreviousItem().focus();
4091 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
4092 this.getNextItem().focus();
4098 * Update the dimensions of the text input field to encompass all available area.
4101 * @param {jQuery.Event} e Event of some sort
4103 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateInputSize = function () {
4104 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
4105 if ( !this.isDisabled() ) {
4106 this.$input
.css( 'width', '1em' );
4107 $lastItem
= this.$group
.children().last();
4108 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
4109 contentWidth
= this.$input
[ 0 ].scrollWidth
;
4110 currentWidth
= this.$input
.width();
4112 if ( contentWidth
< currentWidth
) {
4113 // All is fine, don't perform expensive calculations
4117 if ( !$lastItem
.length
) {
4118 bestWidth
= this.$content
.innerWidth();
4120 bestWidth
= direction
=== 'ltr' ?
4121 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
4122 $lastItem
.position().left
;
4124 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
4125 // pixels this is off by are coming from.
4127 if ( contentWidth
> bestWidth
) {
4128 // This will result in the input getting shifted to the next line
4129 bestWidth
= this.$content
.innerWidth() - 10;
4131 this.$input
.width( Math
.floor( bestWidth
) );
4132 this.updateIfHeightChanged();
4137 * Determine if widget height changed, and if so, update menu position and emit 'resize' event.
4141 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateIfHeightChanged = function () {
4142 var height
= this.$element
.height();
4143 if ( height
!== this.height
) {
4144 this.height
= height
;
4145 this.menu
.position();
4146 this.emit( 'resize' );
4151 * Handle menu choose events.
4154 * @param {OO.ui.OptionWidget} item Chosen item
4156 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
4157 if ( item
&& item
.isVisible() ) {
4158 this.addItemsFromData( [ item
.getData() ] );
4164 * Handle menu item change events.
4168 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuItemsChange = function () {
4169 this.setItemsFromData( this.getItemsData() );
4170 this.$element
.toggleClass( 'oo-ui-capsuleMultiselectWidget-empty', this.menu
.isEmpty() );
4174 * Clear the input field
4178 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearInput = function () {
4179 if ( this.$input
) {
4180 this.$input
.val( '' );
4181 this.updateInputSize();
4184 this.popup
.toggle( false );
4186 this.menu
.toggle( false );
4187 this.menu
.selectItem();
4188 this.menu
.highlightItem();
4194 OO
.ui
.CapsuleMultiselectWidget
.prototype.setDisabled = function ( disabled
) {
4198 OO
.ui
.CapsuleMultiselectWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
4200 if ( this.$input
) {
4201 this.$input
.prop( 'disabled', this.isDisabled() );
4204 this.menu
.setDisabled( this.isDisabled() );
4207 this.popup
.setDisabled( this.isDisabled() );
4211 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4212 this.items
[ i
].updateDisabled();
4223 * @return {OO.ui.CapsuleMultiselectWidget}
4225 OO
.ui
.CapsuleMultiselectWidget
.prototype.focus = function () {
4226 if ( !this.isDisabled() ) {
4228 this.popup
.setSize( this.$handle
.width() );
4229 this.popup
.toggle( true );
4230 OO
.ui
.findFocusable( this.popup
.$element
).focus();
4232 this.updateInputSize();
4233 this.menu
.toggle( true );
4234 this.$input
.focus();
4242 * @deprecated since 0.17.3; use OO.ui.CapsuleMultiselectWidget instead
4244 OO
.ui
.CapsuleMultiSelectWidget
= OO
.ui
.CapsuleMultiselectWidget
;
4247 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
4248 * widgets can be configured with {@link OO.ui.mixin.IconElement icons} and {@link
4249 * OO.ui.mixin.IndicatorElement indicators}.
4250 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
4253 * // Example of a file select widget
4254 * var selectFile = new OO.ui.SelectFileWidget();
4255 * $( 'body' ).append( selectFile.$element );
4257 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets
4260 * @extends OO.ui.Widget
4261 * @mixins OO.ui.mixin.IconElement
4262 * @mixins OO.ui.mixin.IndicatorElement
4263 * @mixins OO.ui.mixin.PendingElement
4264 * @mixins OO.ui.mixin.LabelElement
4267 * @param {Object} [config] Configuration options
4268 * @cfg {string[]|null} [accept=null] MIME types to accept. null accepts all types.
4269 * @cfg {string} [placeholder] Text to display when no file is selected.
4270 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
4271 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
4272 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be true.
4273 * @cfg {number} [thumbnailSizeLimit=20] File size limit in MiB above which to not try and show a
4274 * preview (for performance)
4276 OO
.ui
.SelectFileWidget
= function OoUiSelectFileWidget( config
) {
4279 // Configuration initialization
4280 config
= $.extend( {
4282 placeholder
: OO
.ui
.msg( 'ooui-selectfile-placeholder' ),
4283 notsupported
: OO
.ui
.msg( 'ooui-selectfile-not-supported' ),
4285 showDropTarget
: false,
4286 thumbnailSizeLimit
: 20
4289 // Parent constructor
4290 OO
.ui
.SelectFileWidget
.parent
.call( this, config
);
4292 // Mixin constructors
4293 OO
.ui
.mixin
.IconElement
.call( this, config
);
4294 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
4295 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {}, config
, { $pending
: this.$info
} ) );
4296 OO
.ui
.mixin
.LabelElement
.call( this, config
);
4299 this.$info
= $( '<span>' );
4300 this.showDropTarget
= config
.showDropTarget
;
4301 this.thumbnailSizeLimit
= config
.thumbnailSizeLimit
;
4302 this.isSupported
= this.constructor.static.isSupported();
4303 this.currentFile
= null;
4304 if ( Array
.isArray( config
.accept
) ) {
4305 this.accept
= config
.accept
;
4309 this.placeholder
= config
.placeholder
;
4310 this.notsupported
= config
.notsupported
;
4311 this.onFileSelectedHandler
= this.onFileSelected
.bind( this );
4313 this.selectButton
= new OO
.ui
.ButtonWidget( {
4314 classes
: [ 'oo-ui-selectFileWidget-selectButton' ],
4315 label
: OO
.ui
.msg( 'ooui-selectfile-button-select' ),
4316 disabled
: this.disabled
|| !this.isSupported
4319 this.clearButton
= new OO
.ui
.ButtonWidget( {
4320 classes
: [ 'oo-ui-selectFileWidget-clearButton' ],
4323 disabled
: this.disabled
4327 this.selectButton
.$button
.on( {
4328 keypress
: this.onKeyPress
.bind( this )
4330 this.clearButton
.connect( this, {
4331 click
: 'onClearClick'
4333 if ( config
.droppable
) {
4334 dragHandler
= this.onDragEnterOrOver
.bind( this );
4336 dragenter
: dragHandler
,
4337 dragover
: dragHandler
,
4338 dragleave
: this.onDragLeave
.bind( this ),
4339 drop
: this.onDrop
.bind( this )
4345 this.$label
.addClass( 'oo-ui-selectFileWidget-label' );
4347 .addClass( 'oo-ui-selectFileWidget-info' )
4348 .append( this.$icon
, this.$label
, this.clearButton
.$element
, this.$indicator
);
4350 if ( config
.droppable
&& config
.showDropTarget
) {
4351 this.selectButton
.setIcon( 'upload' );
4352 this.$thumbnail
= $( '<div>' ).addClass( 'oo-ui-selectFileWidget-thumbnail' );
4353 this.setPendingElement( this.$thumbnail
);
4354 this.$dropTarget
= $( '<div>' )
4355 .addClass( 'oo-ui-selectFileWidget-dropTarget' )
4357 click
: this.onDropTargetClick
.bind( this )
4362 this.selectButton
.$element
,
4364 .addClass( 'oo-ui-selectFileWidget-dropLabel' )
4365 .text( OO
.ui
.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
4367 this.$element
.append( this.$dropTarget
);
4370 .addClass( 'oo-ui-selectFileWidget' )
4371 .append( this.$info
, this.selectButton
.$element
);
4378 OO
.inheritClass( OO
.ui
.SelectFileWidget
, OO
.ui
.Widget
);
4379 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IconElement
);
4380 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IndicatorElement
);
4381 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.PendingElement
);
4382 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.LabelElement
);
4384 /* Static Properties */
4387 * Check if this widget is supported
4392 OO
.ui
.SelectFileWidget
.static.isSupported = function () {
4394 if ( OO
.ui
.SelectFileWidget
.static.isSupportedCache
=== null ) {
4395 $input
= $( '<input>' ).attr( 'type', 'file' );
4396 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= $input
[ 0 ].files
!== undefined;
4398 return OO
.ui
.SelectFileWidget
.static.isSupportedCache
;
4401 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= null;
4408 * A change event is emitted when the on/off state of the toggle changes.
4410 * @param {File|null} value New value
4416 * Get the current value of the field
4418 * @return {File|null}
4420 OO
.ui
.SelectFileWidget
.prototype.getValue = function () {
4421 return this.currentFile
;
4425 * Set the current value of the field
4427 * @param {File|null} file File to select
4429 OO
.ui
.SelectFileWidget
.prototype.setValue = function ( file
) {
4430 if ( this.currentFile
!== file
) {
4431 this.currentFile
= file
;
4433 this.emit( 'change', this.currentFile
);
4440 * Focusses the select file button.
4444 OO
.ui
.SelectFileWidget
.prototype.focus = function () {
4445 this.selectButton
.$button
[ 0 ].focus();
4450 * Update the user interface when a file is selected or unselected
4454 OO
.ui
.SelectFileWidget
.prototype.updateUI = function () {
4456 if ( !this.isSupported
) {
4457 this.$element
.addClass( 'oo-ui-selectFileWidget-notsupported' );
4458 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
4459 this.setLabel( this.notsupported
);
4461 this.$element
.addClass( 'oo-ui-selectFileWidget-supported' );
4462 if ( this.currentFile
) {
4463 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
4465 $label
= $label
.add(
4467 .addClass( 'oo-ui-selectFileWidget-fileName' )
4468 .text( this.currentFile
.name
)
4470 if ( this.currentFile
.type
!== '' ) {
4471 $label
= $label
.add(
4473 .addClass( 'oo-ui-selectFileWidget-fileType' )
4474 .text( this.currentFile
.type
)
4477 this.setLabel( $label
);
4479 if ( this.showDropTarget
) {
4481 this.loadAndGetImageUrl().done( function ( url
) {
4482 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
4483 }.bind( this ) ).fail( function () {
4484 this.$thumbnail
.append(
4485 new OO
.ui
.IconWidget( {
4487 classes
: [ 'oo-ui-selectFileWidget-noThumbnail-icon' ]
4490 }.bind( this ) ).always( function () {
4493 this.$dropTarget
.off( 'click' );
4496 if ( this.showDropTarget
) {
4497 this.$dropTarget
.off( 'click' );
4498 this.$dropTarget
.on( {
4499 click
: this.onDropTargetClick
.bind( this )
4503 .css( 'background-image', '' );
4505 this.$element
.addClass( 'oo-ui-selectFileWidget-empty' );
4506 this.setLabel( this.placeholder
);
4512 * If the selected file is an image, get its URL and load it.
4514 * @return {jQuery.Promise} Promise resolves with the image URL after it has loaded
4516 OO
.ui
.SelectFileWidget
.prototype.loadAndGetImageUrl = function () {
4517 var deferred
= $.Deferred(),
4518 file
= this.currentFile
,
4519 reader
= new FileReader();
4523 ( OO
.getProp( file
, 'type' ) || '' ).indexOf( 'image/' ) === 0 &&
4524 file
.size
< this.thumbnailSizeLimit
* 1024 * 1024
4526 reader
.onload = function ( event
) {
4527 var img
= document
.createElement( 'img' );
4528 img
.addEventListener( 'load', function () {
4530 img
.naturalWidth
=== 0 ||
4531 img
.naturalHeight
=== 0 ||
4532 img
.complete
=== false
4536 deferred
.resolve( event
.target
.result
);
4539 img
.src
= event
.target
.result
;
4541 reader
.readAsDataURL( file
);
4546 return deferred
.promise();
4550 * Add the input to the widget
4554 OO
.ui
.SelectFileWidget
.prototype.addInput = function () {
4555 if ( this.$input
) {
4556 this.$input
.remove();
4559 if ( !this.isSupported
) {
4564 this.$input
= $( '<input>' ).attr( 'type', 'file' );
4565 this.$input
.on( 'change', this.onFileSelectedHandler
);
4566 this.$input
.on( 'click', function ( e
) {
4567 // Prevents dropTarget to get clicked which calls
4568 // a click on this input
4569 e
.stopPropagation();
4574 if ( this.accept
) {
4575 this.$input
.attr( 'accept', this.accept
.join( ', ' ) );
4577 this.selectButton
.$button
.append( this.$input
);
4581 * Determine if we should accept this file
4584 * @param {string} mimeType File MIME type
4587 OO
.ui
.SelectFileWidget
.prototype.isAllowedType = function ( mimeType
) {
4590 if ( !this.accept
|| !mimeType
) {
4594 for ( i
= 0; i
< this.accept
.length
; i
++ ) {
4595 mimeTest
= this.accept
[ i
];
4596 if ( mimeTest
=== mimeType
) {
4598 } else if ( mimeTest
.substr( -2 ) === '/*' ) {
4599 mimeTest
= mimeTest
.substr( 0, mimeTest
.length
- 1 );
4600 if ( mimeType
.substr( 0, mimeTest
.length
) === mimeTest
) {
4610 * Handle file selection from the input
4613 * @param {jQuery.Event} e
4615 OO
.ui
.SelectFileWidget
.prototype.onFileSelected = function ( e
) {
4616 var file
= OO
.getProp( e
.target
, 'files', 0 ) || null;
4618 if ( file
&& !this.isAllowedType( file
.type
) ) {
4622 this.setValue( file
);
4627 * Handle clear button click events.
4631 OO
.ui
.SelectFileWidget
.prototype.onClearClick = function () {
4632 this.setValue( null );
4637 * Handle key press events.
4640 * @param {jQuery.Event} e Key press event
4642 OO
.ui
.SelectFileWidget
.prototype.onKeyPress = function ( e
) {
4643 if ( this.isSupported
&& !this.isDisabled() && this.$input
&&
4644 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
4646 this.$input
.click();
4652 * Handle drop target click events.
4655 * @param {jQuery.Event} e Key press event
4657 OO
.ui
.SelectFileWidget
.prototype.onDropTargetClick = function () {
4658 if ( this.isSupported
&& !this.isDisabled() && this.$input
) {
4659 this.$input
.click();
4665 * Handle drag enter and over events
4668 * @param {jQuery.Event} e Drag event
4670 OO
.ui
.SelectFileWidget
.prototype.onDragEnterOrOver = function ( e
) {
4672 droppableFile
= false,
4673 dt
= e
.originalEvent
.dataTransfer
;
4676 e
.stopPropagation();
4678 if ( this.isDisabled() || !this.isSupported
) {
4679 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
4680 dt
.dropEffect
= 'none';
4684 // DataTransferItem and File both have a type property, but in Chrome files
4685 // have no information at this point.
4686 itemOrFile
= OO
.getProp( dt
, 'items', 0 ) || OO
.getProp( dt
, 'files', 0 );
4688 if ( this.isAllowedType( itemOrFile
.type
) ) {
4689 droppableFile
= true;
4691 // dt.types is Array-like, but not an Array
4692 } else if ( Array
.prototype.indexOf
.call( OO
.getProp( dt
, 'types' ) || [], 'Files' ) !== -1 ) {
4693 // File information is not available at this point for security so just assume
4694 // it is acceptable for now.
4695 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
4696 droppableFile
= true;
4699 this.$element
.toggleClass( 'oo-ui-selectFileWidget-canDrop', droppableFile
);
4700 if ( !droppableFile
) {
4701 dt
.dropEffect
= 'none';
4708 * Handle drag leave events
4711 * @param {jQuery.Event} e Drag event
4713 OO
.ui
.SelectFileWidget
.prototype.onDragLeave = function () {
4714 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
4718 * Handle drop events
4721 * @param {jQuery.Event} e Drop event
4723 OO
.ui
.SelectFileWidget
.prototype.onDrop = function ( e
) {
4725 dt
= e
.originalEvent
.dataTransfer
;
4728 e
.stopPropagation();
4729 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
4731 if ( this.isDisabled() || !this.isSupported
) {
4735 file
= OO
.getProp( dt
, 'files', 0 );
4736 if ( file
&& !this.isAllowedType( file
.type
) ) {
4740 this.setValue( file
);
4749 OO
.ui
.SelectFileWidget
.prototype.setDisabled = function ( disabled
) {
4750 OO
.ui
.SelectFileWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
4751 if ( this.selectButton
) {
4752 this.selectButton
.setDisabled( disabled
);
4754 if ( this.clearButton
) {
4755 this.clearButton
.setDisabled( disabled
);
4761 * Progress bars visually display the status of an operation, such as a download,
4762 * and can be either determinate or indeterminate:
4764 * - **determinate** process bars show the percent of an operation that is complete.
4766 * - **indeterminate** process bars use a visual display of motion to indicate that an operation
4767 * is taking place. Because the extent of an indeterminate operation is unknown, the bar does
4768 * not use percentages.
4770 * The value of the `progress` configuration determines whether the bar is determinate or indeterminate.
4773 * // Examples of determinate and indeterminate progress bars.
4774 * var progressBar1 = new OO.ui.ProgressBarWidget( {
4777 * var progressBar2 = new OO.ui.ProgressBarWidget();
4779 * // Create a FieldsetLayout to layout progress bars
4780 * var fieldset = new OO.ui.FieldsetLayout;
4781 * fieldset.addItems( [
4782 * new OO.ui.FieldLayout( progressBar1, {label: 'Determinate', align: 'top'}),
4783 * new OO.ui.FieldLayout( progressBar2, {label: 'Indeterminate', align: 'top'})
4785 * $( 'body' ).append( fieldset.$element );
4788 * @extends OO.ui.Widget
4791 * @param {Object} [config] Configuration options
4792 * @cfg {number|boolean} [progress=false] The type of progress bar (determinate or indeterminate).
4793 * To create a determinate progress bar, specify a number that reflects the initial percent complete.
4794 * By default, the progress bar is indeterminate.
4796 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
4797 // Configuration initialization
4798 config
= config
|| {};
4800 // Parent constructor
4801 OO
.ui
.ProgressBarWidget
.parent
.call( this, config
);
4804 this.$bar
= $( '<div>' );
4805 this.progress
= null;
4808 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
4809 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar' );
4812 role
: 'progressbar',
4814 'aria-valuemax': 100
4816 .addClass( 'oo-ui-progressBarWidget' )
4817 .append( this.$bar
);
4822 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
4824 /* Static Properties */
4826 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
4831 * Get the percent of the progress that has been completed. Indeterminate progresses will return `false`.
4833 * @return {number|boolean} Progress percent
4835 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
4836 return this.progress
;
4840 * Set the percent of the process completed or `false` for an indeterminate process.
4842 * @param {number|boolean} progress Progress percent or `false` for indeterminate
4844 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
4845 this.progress
= progress
;
4847 if ( progress
!== false ) {
4848 this.$bar
.css( 'width', this.progress
+ '%' );
4849 this.$element
.attr( 'aria-valuenow', this.progress
);
4851 this.$bar
.css( 'width', '' );
4852 this.$element
.removeAttr( 'aria-valuenow' );
4854 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
4858 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field}, where users can type a search query,
4859 * and a menu of search results, which is displayed beneath the query
4860 * field. Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible to the user.
4861 * Users can choose an item from the menu or type a query into the text field to search for a matching result item.
4862 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
4864 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
4865 * the [OOjs UI demos][1] for an example.
4867 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/#dialogs-mediawiki-vector-ltr
4870 * @extends OO.ui.Widget
4873 * @param {Object} [config] Configuration options
4874 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
4875 * @cfg {string} [value] Initial query value
4877 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
4878 // Configuration initialization
4879 config
= config
|| {};
4881 // Parent constructor
4882 OO
.ui
.SearchWidget
.parent
.call( this, config
);
4885 this.query
= new OO
.ui
.TextInputWidget( {
4887 placeholder
: config
.placeholder
,
4890 this.results
= new OO
.ui
.SelectWidget();
4891 this.$query
= $( '<div>' );
4892 this.$results
= $( '<div>' );
4895 this.query
.connect( this, {
4896 change
: 'onQueryChange',
4897 enter
: 'onQueryEnter'
4899 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
4903 .addClass( 'oo-ui-searchWidget-query' )
4904 .append( this.query
.$element
);
4906 .addClass( 'oo-ui-searchWidget-results' )
4907 .append( this.results
.$element
);
4909 .addClass( 'oo-ui-searchWidget' )
4910 .append( this.$results
, this.$query
);
4915 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
4920 * Handle query key down events.
4923 * @param {jQuery.Event} e Key down event
4925 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
4926 var highlightedItem
, nextItem
,
4927 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
4930 highlightedItem
= this.results
.getHighlightedItem();
4931 if ( !highlightedItem
) {
4932 highlightedItem
= this.results
.getSelectedItem();
4934 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
4935 this.results
.highlightItem( nextItem
);
4936 nextItem
.scrollElementIntoView();
4941 * Handle select widget select events.
4943 * Clears existing results. Subclasses should repopulate items according to new query.
4946 * @param {string} value New value
4948 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
4950 this.results
.clearItems();
4954 * Handle select widget enter key events.
4956 * Chooses highlighted item.
4959 * @param {string} value New value
4961 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
4962 var highlightedItem
= this.results
.getHighlightedItem();
4963 if ( highlightedItem
) {
4964 this.results
.chooseItem( highlightedItem
);
4969 * Get the query input.
4971 * @return {OO.ui.TextInputWidget} Query input
4973 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
4978 * Get the search results menu.
4980 * @return {OO.ui.SelectWidget} Menu of search results
4982 OO
.ui
.SearchWidget
.prototype.getResults = function () {
4983 return this.results
;
4987 * NumberInputWidgets combine a {@link OO.ui.TextInputWidget text input} (where a value
4988 * can be entered manually) and two {@link OO.ui.ButtonWidget button widgets}
4989 * (to adjust the value in increments) to allow the user to enter a number.
4992 * // Example: A NumberInputWidget.
4993 * var numberInput = new OO.ui.NumberInputWidget( {
4994 * label: 'NumberInputWidget',
4995 * input: { value: 5 },
4999 * $( 'body' ).append( numberInput.$element );
5002 * @extends OO.ui.Widget
5005 * @param {Object} [config] Configuration options
5006 * @cfg {Object} [input] Configuration options to pass to the {@link OO.ui.TextInputWidget text input widget}.
5007 * @cfg {Object} [minusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget decrementing button widget}.
5008 * @cfg {Object} [plusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget incrementing button widget}.
5009 * @cfg {boolean} [isInteger=false] Whether the field accepts only integer values.
5010 * @cfg {number} [min=-Infinity] Minimum allowed value
5011 * @cfg {number} [max=Infinity] Maximum allowed value
5012 * @cfg {number} [step=1] Delta when using the buttons or up/down arrow keys
5013 * @cfg {number|null} [pageStep] Delta when using the page-up/page-down keys. Defaults to 10 times #step.
5014 * @cfg {boolean} [showButtons=true] Whether to show the plus and minus buttons.
5016 OO
.ui
.NumberInputWidget
= function OoUiNumberInputWidget( config
) {
5017 // Configuration initialization
5018 config
= $.extend( {
5027 // Parent constructor
5028 OO
.ui
.NumberInputWidget
.parent
.call( this, config
);
5031 this.input
= new OO
.ui
.TextInputWidget( $.extend(
5033 disabled
: this.isDisabled(),
5038 if ( config
.showButtons
) {
5039 this.minusButton
= new OO
.ui
.ButtonWidget( $.extend(
5041 disabled
: this.isDisabled(),
5046 classes
: [ 'oo-ui-numberInputWidget-minusButton' ],
5050 this.plusButton
= new OO
.ui
.ButtonWidget( $.extend(
5052 disabled
: this.isDisabled(),
5057 classes
: [ 'oo-ui-numberInputWidget-plusButton' ],
5064 this.input
.connect( this, {
5065 change
: this.emit
.bind( this, 'change' ),
5066 enter
: this.emit
.bind( this, 'enter' )
5068 this.input
.$input
.on( {
5069 keydown
: this.onKeyDown
.bind( this ),
5070 'wheel mousewheel DOMMouseScroll': this.onWheel
.bind( this )
5072 if ( config
.showButtons
) {
5073 this.plusButton
.connect( this, {
5074 click
: [ 'onButtonClick', +1 ]
5076 this.minusButton
.connect( this, {
5077 click
: [ 'onButtonClick', -1 ]
5082 this.setIsInteger( !!config
.isInteger
);
5083 this.setRange( config
.min
, config
.max
);
5084 this.setStep( config
.step
, config
.pageStep
);
5086 this.$field
= $( '<div>' ).addClass( 'oo-ui-numberInputWidget-field' )
5087 .append( this.input
.$element
);
5088 this.$element
.addClass( 'oo-ui-numberInputWidget' ).append( this.$field
);
5089 if ( config
.showButtons
) {
5091 .prepend( this.minusButton
.$element
)
5092 .append( this.plusButton
.$element
);
5093 this.$element
.addClass( 'oo-ui-numberInputWidget-buttoned' );
5095 this.input
.setValidation( this.validateNumber
.bind( this ) );
5100 OO
.inheritClass( OO
.ui
.NumberInputWidget
, OO
.ui
.Widget
);
5105 * A `change` event is emitted when the value of the input changes.
5111 * An `enter` event is emitted when the user presses 'enter' inside the text box.
5119 * Set whether only integers are allowed
5121 * @param {boolean} flag
5123 OO
.ui
.NumberInputWidget
.prototype.setIsInteger = function ( flag
) {
5124 this.isInteger
= !!flag
;
5125 this.input
.setValidityFlag();
5129 * Get whether only integers are allowed
5131 * @return {boolean} Flag value
5133 OO
.ui
.NumberInputWidget
.prototype.getIsInteger = function () {
5134 return this.isInteger
;
5138 * Set the range of allowed values
5140 * @param {number} min Minimum allowed value
5141 * @param {number} max Maximum allowed value
5143 OO
.ui
.NumberInputWidget
.prototype.setRange = function ( min
, max
) {
5145 throw new Error( 'Minimum (' + min
+ ') must not be greater than maximum (' + max
+ ')' );
5149 this.input
.setValidityFlag();
5153 * Get the current range
5155 * @return {number[]} Minimum and maximum values
5157 OO
.ui
.NumberInputWidget
.prototype.getRange = function () {
5158 return [ this.min
, this.max
];
5162 * Set the stepping deltas
5164 * @param {number} step Normal step
5165 * @param {number|null} pageStep Page step. If null, 10 * step will be used.
5167 OO
.ui
.NumberInputWidget
.prototype.setStep = function ( step
, pageStep
) {
5169 throw new Error( 'Step value must be positive' );
5171 if ( pageStep
=== null ) {
5172 pageStep
= step
* 10;
5173 } else if ( pageStep
<= 0 ) {
5174 throw new Error( 'Page step value must be positive' );
5177 this.pageStep
= pageStep
;
5181 * Get the current stepping values
5183 * @return {number[]} Step and page step
5185 OO
.ui
.NumberInputWidget
.prototype.getStep = function () {
5186 return [ this.step
, this.pageStep
];
5190 * Get the current value of the widget
5194 OO
.ui
.NumberInputWidget
.prototype.getValue = function () {
5195 return this.input
.getValue();
5199 * Get the current value of the widget as a number
5201 * @return {number} May be NaN, or an invalid number
5203 OO
.ui
.NumberInputWidget
.prototype.getNumericValue = function () {
5204 return +this.input
.getValue();
5208 * Set the value of the widget
5210 * @param {string} value Invalid values are allowed
5212 OO
.ui
.NumberInputWidget
.prototype.setValue = function ( value
) {
5213 this.input
.setValue( value
);
5217 * Adjust the value of the widget
5219 * @param {number} delta Adjustment amount
5221 OO
.ui
.NumberInputWidget
.prototype.adjustValue = function ( delta
) {
5222 var n
, v
= this.getNumericValue();
5225 if ( isNaN( delta
) || !isFinite( delta
) ) {
5226 throw new Error( 'Delta must be a finite number' );
5233 n
= Math
.max( Math
.min( n
, this.max
), this.min
);
5234 if ( this.isInteger
) {
5235 n
= Math
.round( n
);
5248 * @param {string} value Field value
5251 OO
.ui
.NumberInputWidget
.prototype.validateNumber = function ( value
) {
5253 if ( isNaN( n
) || !isFinite( n
) ) {
5257 /*jshint bitwise: false */
5258 if ( this.isInteger
&& ( n
| 0 ) !== n
) {
5261 /*jshint bitwise: true */
5263 if ( n
< this.min
|| n
> this.max
) {
5271 * Handle mouse click events.
5274 * @param {number} dir +1 or -1
5276 OO
.ui
.NumberInputWidget
.prototype.onButtonClick = function ( dir
) {
5277 this.adjustValue( dir
* this.step
);
5281 * Handle mouse wheel events.
5284 * @param {jQuery.Event} event
5286 OO
.ui
.NumberInputWidget
.prototype.onWheel = function ( event
) {
5289 if ( !this.isDisabled() && this.input
.$input
.is( ':focus' ) ) {
5290 // Standard 'wheel' event
5291 if ( event
.originalEvent
.deltaMode
!== undefined ) {
5292 this.sawWheelEvent
= true;
5294 if ( event
.originalEvent
.deltaY
) {
5295 delta
= -event
.originalEvent
.deltaY
;
5296 } else if ( event
.originalEvent
.deltaX
) {
5297 delta
= event
.originalEvent
.deltaX
;
5300 // Non-standard events
5301 if ( !this.sawWheelEvent
) {
5302 if ( event
.originalEvent
.wheelDeltaX
) {
5303 delta
= -event
.originalEvent
.wheelDeltaX
;
5304 } else if ( event
.originalEvent
.wheelDeltaY
) {
5305 delta
= event
.originalEvent
.wheelDeltaY
;
5306 } else if ( event
.originalEvent
.wheelDelta
) {
5307 delta
= event
.originalEvent
.wheelDelta
;
5308 } else if ( event
.originalEvent
.detail
) {
5309 delta
= -event
.originalEvent
.detail
;
5314 delta
= delta
< 0 ? -1 : 1;
5315 this.adjustValue( delta
* this.step
);
5323 * Handle key down events.
5326 * @param {jQuery.Event} e Key down event
5328 OO
.ui
.NumberInputWidget
.prototype.onKeyDown = function ( e
) {
5329 if ( !this.isDisabled() ) {
5330 switch ( e
.which
) {
5332 this.adjustValue( this.step
);
5334 case OO
.ui
.Keys
.DOWN
:
5335 this.adjustValue( -this.step
);
5337 case OO
.ui
.Keys
.PAGEUP
:
5338 this.adjustValue( this.pageStep
);
5340 case OO
.ui
.Keys
.PAGEDOWN
:
5341 this.adjustValue( -this.pageStep
);
5350 OO
.ui
.NumberInputWidget
.prototype.setDisabled = function ( disabled
) {
5352 OO
.ui
.NumberInputWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
5355 this.input
.setDisabled( this.isDisabled() );
5357 if ( this.minusButton
) {
5358 this.minusButton
.setDisabled( this.isDisabled() );
5360 if ( this.plusButton
) {
5361 this.plusButton
.setDisabled( this.isDisabled() );