3 * https://www.mediawiki.org/wiki/OOUI
5 * Copyright 2011–2018 OOUI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2018-03-23T23:42:32Z
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
27 * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
28 * but the draggable state should be called from the DraggableGroupElement, which updates
31 OO
.ui
.mixin
.DraggableElement
= function OoUiMixinDraggableElement( config
) {
32 config
= config
|| {};
36 this.$handle
= config
.$handle
|| this.$element
;
37 this.wasHandleUsed
= null;
39 // Initialize and events
41 .addClass( 'oo-ui-draggableElement' )
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' );
50 this.toggleDraggable( config
.draggable
=== undefined ? true : !!config
.draggable
);
53 OO
.initClass( OO
.ui
.mixin
.DraggableElement
);
60 * A dragstart event is emitted when the user clicks and begins dragging an item.
61 * @param {OO.ui.mixin.DraggableElement} item The item the user has clicked and is dragging with the mouse.
66 * A dragend event is emitted when the user drags an item and releases the mouse,
67 * thus terminating the drag operation.
72 * A drop event is emitted when the user drags an item and then releases the mouse button
73 * over a valid target.
76 /* Static Properties */
79 * @inheritdoc OO.ui.mixin.ButtonElement
81 OO
.ui
.mixin
.DraggableElement
.static.cancelButtonMouseDownEvents
= false;
86 * Change the draggable state of this widget.
87 * This allows users to temporarily halt the dragging operations.
89 * @param {boolean} isDraggable Widget supports draggable operations
92 OO
.ui
.mixin
.DraggableElement
.prototype.toggleDraggable = function ( isDraggable
) {
93 isDraggable
= isDraggable
!== undefined ? !!isDraggable
: !this.draggable
;
95 if ( this.draggable
!== isDraggable
) {
96 this.draggable
= isDraggable
;
98 this.$handle
.toggleClass( 'oo-ui-draggableElement-undraggable', !this.draggable
);
100 // We make the entire element draggable, not just the handle, so that
101 // the whole element appears to move. wasHandleUsed prevents drags from
102 // starting outside the handle
103 this.$element
.prop( 'draggable', this.draggable
);
108 * Check the draggable state of this widget
110 * @return {boolean} Widget supports draggable operations
112 OO
.ui
.mixin
.DraggableElement
.prototype.isDraggable = function () {
113 return this.draggable
;
117 * Respond to mousedown event.
120 * @param {jQuery.Event} e Drag event
122 OO
.ui
.mixin
.DraggableElement
.prototype.onDragMouseDown = function ( e
) {
123 if ( !this.isDraggable() ) {
128 // Optimization: if the handle is the whole element this is always true
129 this.$handle
[ 0 ] === this.$element
[ 0 ] ||
130 // Check the mousedown occurred inside the handle
131 OO
.ui
.contains( this.$handle
[ 0 ], e
.target
, true );
135 * Respond to dragstart event.
138 * @param {jQuery.Event} e Drag event
139 * @return {boolean} False if the event is cancelled
142 OO
.ui
.mixin
.DraggableElement
.prototype.onDragStart = function ( e
) {
144 dataTransfer
= e
.originalEvent
.dataTransfer
;
146 if ( !this.wasHandleUsed
|| !this.isDraggable() ) {
150 // Define drop effect
151 dataTransfer
.dropEffect
= 'none';
152 dataTransfer
.effectAllowed
= 'move';
154 // We must set up a dataTransfer data property or Firefox seems to
155 // ignore the fact the element is draggable.
157 dataTransfer
.setData( 'application-x/OOUI-draggable', this.getIndex() );
159 // The above is only for Firefox. Move on if it fails.
161 // Briefly add a 'clone' class to style the browser's native drag image
162 this.$element
.addClass( 'oo-ui-draggableElement-clone' );
163 // Add placeholder class after the browser has rendered the clone
164 setTimeout( function () {
166 .removeClass( 'oo-ui-draggableElement-clone' )
167 .addClass( 'oo-ui-draggableElement-placeholder' );
170 this.emit( 'dragstart', this );
175 * Respond to dragend event.
180 OO
.ui
.mixin
.DraggableElement
.prototype.onDragEnd = function () {
181 this.$element
.removeClass( 'oo-ui-draggableElement-placeholder' );
182 this.emit( 'dragend' );
189 * @param {jQuery.Event} e Drop event
192 OO
.ui
.mixin
.DraggableElement
.prototype.onDrop = function ( e
) {
194 this.emit( 'drop', e
);
198 * In order for drag/drop to work, the dragover event must
199 * return false and stop propogation.
201 * @param {jQuery.Event} e Drag event
204 OO
.ui
.mixin
.DraggableElement
.prototype.onDragOver = function ( e
) {
210 * Store it in the DOM so we can access from the widget drag event
213 * @param {number} index Item index
215 OO
.ui
.mixin
.DraggableElement
.prototype.setIndex = function ( index
) {
216 if ( this.index
!== index
) {
218 this.$element
.data( 'index', index
);
226 * @return {number} Item index
228 OO
.ui
.mixin
.DraggableElement
.prototype.getIndex = function () {
233 * DraggableGroupElement is a mixin class used to create a group element to
234 * contain draggable elements, which are items that can be clicked and dragged by a mouse.
235 * The class is used with OO.ui.mixin.DraggableElement.
239 * @mixins OO.ui.mixin.GroupElement
242 * @param {Object} [config] Configuration options
243 * @cfg {string} [orientation] Item orientation: 'horizontal' or 'vertical'. The orientation
244 * should match the layout of the items. Items displayed in a single row
245 * or in several rows should use horizontal orientation. The vertical orientation should only be
246 * used when the items are displayed in a single column. Defaults to 'vertical'
247 * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
249 OO
.ui
.mixin
.DraggableGroupElement
= function OoUiMixinDraggableGroupElement( config
) {
250 // Configuration initialization
251 config
= config
|| {};
253 // Parent constructor
254 OO
.ui
.mixin
.GroupElement
.call( this, config
);
257 this.orientation
= config
.orientation
|| 'vertical';
258 this.dragItem
= null;
261 this.itemsOrder
= null;
262 this.draggable
= config
.draggable
=== undefined ? true : !!config
.draggable
;
266 dragstart
: 'itemDragStart',
267 dragend
: 'itemDragEnd',
270 this.connect( this, {
271 itemDragStart
: 'onItemDragStart',
272 itemDrop
: 'onItemDropOrDragEnd',
273 itemDragEnd
: 'onItemDropOrDragEnd'
277 if ( Array
.isArray( config
.items
) ) {
278 this.addItems( config
.items
);
281 .addClass( 'oo-ui-draggableGroupElement' )
282 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' );
286 OO
.mixinClass( OO
.ui
.mixin
.DraggableGroupElement
, OO
.ui
.mixin
.GroupElement
);
291 * An item has been dragged to a new position, but not yet dropped.
294 * @param {OO.ui.mixin.DraggableElement} item Dragged item
295 * @param {number} [newIndex] New index for the item
299 * An item has been dropped at a new position.
302 * @param {OO.ui.mixin.DraggableElement} item Reordered item
303 * @param {number} [newIndex] New index for the item
307 * Draggable state of this widget has changed.
310 * @param {boolean} [draggable] Widget is draggable
316 * Change the draggable state of this widget.
317 * This allows users to temporarily halt the dragging operations.
319 * @param {boolean} isDraggable Widget supports draggable operations
322 OO
.ui
.mixin
.DraggableGroupElement
.prototype.toggleDraggable = function ( isDraggable
) {
323 isDraggable
= isDraggable
!== undefined ? !!isDraggable
: !this.draggable
;
325 if ( this.draggable
!== isDraggable
) {
326 this.draggable
= isDraggable
;
328 // Tell the items their draggable state changed
329 this.getItems().forEach( function ( item
) {
330 item
.toggleDraggable( this.draggable
);
334 this.emit( 'draggable', this.draggable
);
339 * Check the draggable state of this widget
341 * @return {boolean} Widget supports draggable operations
343 OO
.ui
.mixin
.DraggableGroupElement
.prototype.isDraggable = function () {
344 return this.draggable
;
348 * Respond to item drag start event
351 * @param {OO.ui.mixin.DraggableElement} item Dragged item
353 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
354 if ( !this.isDraggable() ) {
357 // Make a shallow copy of this.items so we can re-order it during previews
358 // without affecting the original array.
359 this.itemsOrder
= this.items
.slice();
360 this.updateIndexes();
361 if ( this.orientation
=== 'horizontal' ) {
362 // Calculate and cache directionality on drag start - it's a little
363 // expensive and it shouldn't change while dragging.
364 this.dir
= this.$element
.css( 'direction' );
366 this.setDragItem( item
);
370 * Update the index properties of the items
372 OO
.ui
.mixin
.DraggableGroupElement
.prototype.updateIndexes = function () {
375 // Map the index of each object
376 for ( i
= 0, len
= this.itemsOrder
.length
; i
< len
; i
++ ) {
377 this.itemsOrder
[ i
].setIndex( i
);
382 * Handle drop or dragend event and switch the order of the items accordingly
385 * @param {OO.ui.mixin.DraggableElement} item Dropped item
387 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDropOrDragEnd = function () {
388 var targetIndex
, originalIndex
,
389 item
= this.getDragItem();
391 // TODO: Figure out a way to configure a list of legally droppable
392 // elements even if they are not yet in the list
394 originalIndex
= this.items
.indexOf( item
);
395 // If the item has moved forward, add one to the index to account for the left shift
396 targetIndex
= item
.getIndex() + ( item
.getIndex() > originalIndex
? 1 : 0 );
397 if ( targetIndex
!== originalIndex
) {
398 this.reorder( this.getDragItem(), targetIndex
);
399 this.emit( 'reorder', this.getDragItem(), targetIndex
);
401 this.updateIndexes();
403 this.unsetDragItem();
404 // Return false to prevent propogation
409 * Respond to dragover event
412 * @param {jQuery.Event} e Dragover event
415 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
416 var overIndex
, targetIndex
,
417 item
= this.getDragItem(),
418 dragItemIndex
= item
.getIndex();
420 // Get the OptionWidget item we are dragging over
421 overIndex
= $( e
.target
).closest( '.oo-ui-draggableElement' ).data( 'index' );
423 if ( overIndex
!== undefined && overIndex
!== dragItemIndex
) {
424 targetIndex
= overIndex
+ ( overIndex
> dragItemIndex
? 1 : 0 );
426 if ( targetIndex
> 0 ) {
427 this.$group
.children().eq( targetIndex
- 1 ).after( item
.$element
);
429 this.$group
.prepend( item
.$element
);
431 // Move item in itemsOrder array
432 this.itemsOrder
.splice( overIndex
, 0,
433 this.itemsOrder
.splice( dragItemIndex
, 1 )[ 0 ]
435 this.updateIndexes();
436 this.emit( 'drag', item
, targetIndex
);
443 * Reorder the items in the group
445 * @param {OO.ui.mixin.DraggableElement} item Reordered item
446 * @param {number} newIndex New index
448 OO
.ui
.mixin
.DraggableGroupElement
.prototype.reorder = function ( item
, newIndex
) {
449 this.addItems( [ item
], newIndex
);
455 * @param {OO.ui.mixin.DraggableElement} item Dragged item
457 OO
.ui
.mixin
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
458 if ( this.dragItem
!== item
) {
459 this.dragItem
= item
;
460 this.$element
.on( 'dragover', this.onDragOver
.bind( this ) );
461 this.$element
.addClass( 'oo-ui-draggableGroupElement-dragging' );
466 * Unset the current dragged item
468 OO
.ui
.mixin
.DraggableGroupElement
.prototype.unsetDragItem = function () {
469 if ( this.dragItem
) {
470 this.dragItem
= null;
471 this.$element
.off( 'dragover' );
472 this.$element
.removeClass( 'oo-ui-draggableGroupElement-dragging' );
477 * Get the item that is currently being dragged.
479 * @return {OO.ui.mixin.DraggableElement|null} The currently dragged item, or `null` if no item is being dragged
481 OO
.ui
.mixin
.DraggableGroupElement
.prototype.getDragItem = function () {
482 return this.dragItem
;
486 * RequestManager is a mixin that manages the lifecycle of a promise-backed request for a widget, such as
487 * the {@link OO.ui.mixin.LookupElement}.
494 OO
.ui
.mixin
.RequestManager
= function OoUiMixinRequestManager() {
495 this.requestCache
= {};
496 this.requestQuery
= null;
497 this.requestRequest
= null;
502 OO
.initClass( OO
.ui
.mixin
.RequestManager
);
505 * Get request results for the current query.
507 * @return {jQuery.Promise} Promise object which will be passed response data as the first argument of
508 * the done event. If the request was aborted to make way for a subsequent request, this promise
509 * may not be rejected, depending on what jQuery feels like doing.
511 OO
.ui
.mixin
.RequestManager
.prototype.getRequestData = function () {
513 value
= this.getRequestQuery(),
514 deferred
= $.Deferred(),
518 if ( Object
.prototype.hasOwnProperty
.call( this.requestCache
, value
) ) {
519 deferred
.resolve( this.requestCache
[ value
] );
521 if ( this.pushPending
) {
524 this.requestQuery
= value
;
525 ourRequest
= this.requestRequest
= this.getRequest();
527 .always( function () {
528 // We need to pop pending even if this is an old request, otherwise
529 // the widget will remain pending forever.
530 // TODO: this assumes that an aborted request will fail or succeed soon after
531 // being aborted, or at least eventually. It would be nice if we could popPending()
532 // at abort time, but only if we knew that we hadn't already called popPending()
534 if ( widget
.popPending
) {
538 .done( function ( response
) {
539 // If this is an old request (and aborting it somehow caused it to still succeed),
540 // ignore its success completely
541 if ( ourRequest
=== widget
.requestRequest
) {
542 widget
.requestQuery
= null;
543 widget
.requestRequest
= null;
544 widget
.requestCache
[ value
] = widget
.getRequestCacheDataFromResponse( response
);
545 deferred
.resolve( widget
.requestCache
[ value
] );
549 // If this is an old request (or a request failing because it's being aborted),
550 // ignore its failure completely
551 if ( ourRequest
=== widget
.requestRequest
) {
552 widget
.requestQuery
= null;
553 widget
.requestRequest
= null;
558 return deferred
.promise();
562 * Abort the currently pending request, if any.
566 OO
.ui
.mixin
.RequestManager
.prototype.abortRequest = function () {
567 var oldRequest
= this.requestRequest
;
569 // First unset this.requestRequest to the fail handler will notice
570 // that the request is no longer current
571 this.requestRequest
= null;
572 this.requestQuery
= null;
578 * Get the query to be made.
583 * @return {string} query to be used
585 OO
.ui
.mixin
.RequestManager
.prototype.getRequestQuery
= null;
588 * Get a new request object of the current query value.
593 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
595 OO
.ui
.mixin
.RequestManager
.prototype.getRequest
= null;
598 * Pre-process data returned by the request from #getRequest.
600 * The return value of this function will be cached, and any further queries for the given value
601 * will use the cache rather than doing API requests.
606 * @param {Mixed} response Response from server
607 * @return {Mixed} Cached result data
609 OO
.ui
.mixin
.RequestManager
.prototype.getRequestCacheDataFromResponse
= null;
612 * LookupElement is a mixin that creates a {@link OO.ui.MenuSelectWidget menu} of suggested values for
613 * a {@link OO.ui.TextInputWidget text input widget}. Suggested values are based on the characters the user types
614 * into the text input field and, in general, the menu is only displayed when the user types. If a suggested value is chosen
615 * from the lookup menu, that value becomes the value of the input field.
617 * Note that a new menu of suggested items is displayed when a value is chosen from the lookup menu. If this is
618 * not the desired behavior, disable lookup menus with the #setLookupsDisabled method, then set the value, then
621 * See the [OOUI demos][1] for an example.
623 * [1]: https://doc.wikimedia.org/oojs-ui/master/demos/#LookupElement-try-inputting-an-integer
627 * @mixins OO.ui.mixin.RequestManager
630 * @param {Object} [config] Configuration options
631 * @cfg {jQuery} [$overlay] Overlay for the lookup menu; defaults to relative positioning.
632 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
633 * @cfg {jQuery} [$container=this.$element] The container element. The lookup menu is rendered beneath the specified element.
634 * @cfg {boolean} [allowSuggestionsWhenEmpty=false] Request and display a lookup menu when the text input is empty.
635 * By default, the lookup menu is not generated and displayed until the user begins to type.
636 * @cfg {boolean} [highlightFirst=true] Whether the first lookup result should be highlighted (so, that the user can
637 * take it over into the input with simply pressing return) automatically or not.
639 OO
.ui
.mixin
.LookupElement
= function OoUiMixinLookupElement( config
) {
640 // Configuration initialization
641 config
= $.extend( { highlightFirst
: true }, config
);
643 // Mixin constructors
644 OO
.ui
.mixin
.RequestManager
.call( this, config
);
647 this.$overlay
= ( config
.$overlay
=== true ? OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
648 this.lookupMenu
= new OO
.ui
.MenuSelectWidget( {
651 $floatableContainer
: config
.$container
|| this.$element
654 this.allowSuggestionsWhenEmpty
= config
.allowSuggestionsWhenEmpty
|| false;
656 this.lookupsDisabled
= false;
657 this.lookupInputFocused
= false;
658 this.lookupHighlightFirstItem
= config
.highlightFirst
;
662 focus
: this.onLookupInputFocus
.bind( this ),
663 blur
: this.onLookupInputBlur
.bind( this ),
664 mousedown
: this.onLookupInputMouseDown
.bind( this )
666 this.connect( this, { change
: 'onLookupInputChange' } );
667 this.lookupMenu
.connect( this, {
668 toggle
: 'onLookupMenuToggle',
669 choose
: 'onLookupMenuItemChoose'
675 'aria-owns': this.lookupMenu
.getElementId(),
676 'aria-autocomplete': 'list'
678 this.$element
.addClass( 'oo-ui-lookupElement' );
679 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
680 this.$overlay
.append( this.lookupMenu
.$element
);
685 OO
.mixinClass( OO
.ui
.mixin
.LookupElement
, OO
.ui
.mixin
.RequestManager
);
690 * Handle input focus event.
693 * @param {jQuery.Event} e Input focus event
695 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputFocus = function () {
696 this.lookupInputFocused
= true;
697 this.populateLookupMenu();
701 * Handle input blur event.
704 * @param {jQuery.Event} e Input blur event
706 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputBlur = function () {
707 this.closeLookupMenu();
708 this.lookupInputFocused
= false;
712 * Handle input mouse down event.
715 * @param {jQuery.Event} e Input mouse down event
717 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputMouseDown = function () {
718 // Only open the menu if the input was already focused.
719 // This way we allow the user to open the menu again after closing it with Esc
720 // by clicking in the input. Opening (and populating) the menu when initially
721 // clicking into the input is handled by the focus handler.
722 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
723 this.populateLookupMenu();
728 * Handle input change event.
731 * @param {string} value New input value
733 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputChange = function () {
734 if ( this.lookupInputFocused
) {
735 this.populateLookupMenu();
740 * Handle the lookup menu being shown/hidden.
743 * @param {boolean} visible Whether the lookup menu is now visible.
745 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
747 // When the menu is hidden, abort any active request and clear the menu.
748 // This has to be done here in addition to closeLookupMenu(), because
749 // MenuSelectWidget will close itself when the user presses Esc.
750 this.abortLookupRequest();
751 this.lookupMenu
.clearItems();
756 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
759 * @param {OO.ui.MenuOptionWidget} item Selected item
761 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
762 this.setValue( item
.getData() );
769 * @return {OO.ui.MenuSelectWidget}
771 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenu = function () {
772 return this.lookupMenu
;
776 * Disable or re-enable lookups.
778 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
780 * @param {boolean} disabled Disable lookups
782 OO
.ui
.mixin
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
783 this.lookupsDisabled
= !!disabled
;
787 * Open the menu. If there are no entries in the menu, this does nothing.
792 OO
.ui
.mixin
.LookupElement
.prototype.openLookupMenu = function () {
793 if ( !this.lookupMenu
.isEmpty() ) {
794 this.lookupMenu
.toggle( true );
800 * Close the menu, empty it, and abort any pending request.
805 OO
.ui
.mixin
.LookupElement
.prototype.closeLookupMenu = function () {
806 this.lookupMenu
.toggle( false );
807 this.abortLookupRequest();
808 this.lookupMenu
.clearItems();
813 * Request menu items based on the input's current value, and when they arrive,
814 * populate the menu with these items and show the menu.
816 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
821 OO
.ui
.mixin
.LookupElement
.prototype.populateLookupMenu = function () {
823 value
= this.getValue();
825 if ( this.lookupsDisabled
|| this.isReadOnly() ) {
829 // If the input is empty, clear the menu, unless suggestions when empty are allowed.
830 if ( !this.allowSuggestionsWhenEmpty
&& value
=== '' ) {
831 this.closeLookupMenu();
832 // Skip population if there is already a request pending for the current value
833 } else if ( value
!== this.lookupQuery
) {
834 this.getLookupMenuItems()
835 .done( function ( items
) {
836 widget
.lookupMenu
.clearItems();
837 if ( items
.length
) {
841 widget
.initializeLookupMenuSelection();
843 widget
.lookupMenu
.toggle( false );
847 widget
.lookupMenu
.clearItems();
855 * Highlight the first selectable item in the menu, if configured.
860 OO
.ui
.mixin
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
861 if ( this.lookupHighlightFirstItem
&& !this.lookupMenu
.findSelectedItem() ) {
862 this.lookupMenu
.highlightItem( this.lookupMenu
.findFirstSelectableItem() );
867 * Get lookup menu items for the current query.
870 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
871 * the done event. If the request was aborted to make way for a subsequent request, this promise
872 * will not be rejected: it will remain pending forever.
874 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuItems = function () {
875 return this.getRequestData().then( function ( data
) {
876 return this.getLookupMenuOptionsFromData( data
);
881 * Abort the currently pending lookup request, if any.
885 OO
.ui
.mixin
.LookupElement
.prototype.abortLookupRequest = function () {
890 * Get a new request object of the current lookup query value.
895 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
897 OO
.ui
.mixin
.LookupElement
.prototype.getLookupRequest
= null;
900 * Pre-process data returned by the request from #getLookupRequest.
902 * The return value of this function will be cached, and any further queries for the given value
903 * will use the cache rather than doing API requests.
908 * @param {Mixed} response Response from server
909 * @return {Mixed} Cached result data
911 OO
.ui
.mixin
.LookupElement
.prototype.getLookupCacheDataFromResponse
= null;
914 * Get a list of menu option widgets from the (possibly cached) data returned by
915 * #getLookupCacheDataFromResponse.
920 * @param {Mixed} data Cached result data, usually an array
921 * @return {OO.ui.MenuOptionWidget[]} Menu items
923 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuOptionsFromData
= null;
926 * Set the read-only state of the widget.
928 * This will also disable/enable the lookups functionality.
930 * @param {boolean} readOnly Make input read-only
933 OO
.ui
.mixin
.LookupElement
.prototype.setReadOnly = function ( readOnly
) {
935 // Note: Calling #setReadOnly this way assumes this is mixed into an OO.ui.TextInputWidget
936 OO
.ui
.TextInputWidget
.prototype.setReadOnly
.call( this, readOnly
);
938 // During construction, #setReadOnly is called before the OO.ui.mixin.LookupElement constructor
939 if ( this.isReadOnly() && this.lookupMenu
) {
940 this.closeLookupMenu();
947 * @inheritdoc OO.ui.mixin.RequestManager
949 OO
.ui
.mixin
.LookupElement
.prototype.getRequestQuery = function () {
950 return this.getValue();
954 * @inheritdoc OO.ui.mixin.RequestManager
956 OO
.ui
.mixin
.LookupElement
.prototype.getRequest = function () {
957 return this.getLookupRequest();
961 * @inheritdoc OO.ui.mixin.RequestManager
963 OO
.ui
.mixin
.LookupElement
.prototype.getRequestCacheDataFromResponse = function ( response
) {
964 return this.getLookupCacheDataFromResponse( response
);
968 * TabPanelLayouts are used within {@link OO.ui.IndexLayout index layouts} to create tab panels that
969 * users can select and display from the index's optional {@link OO.ui.TabSelectWidget tab}
970 * navigation. TabPanels are usually not instantiated directly, rather extended to include the
971 * required content and functionality.
973 * Each tab panel must have a unique symbolic name, which is passed to the constructor. In addition,
974 * the tab panel's tab item is customized (with a label) using the #setupTabItem method. See
975 * {@link OO.ui.IndexLayout IndexLayout} for an example.
978 * @extends OO.ui.PanelLayout
981 * @param {string} name Unique symbolic name of tab panel
982 * @param {Object} [config] Configuration options
983 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] Label for tab panel's tab
985 OO
.ui
.TabPanelLayout
= function OoUiTabPanelLayout( name
, config
) {
986 // Allow passing positional parameters inside the config object
987 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
992 // Configuration initialization
993 config
= $.extend( { scrollable
: true }, config
);
995 // Parent constructor
996 OO
.ui
.TabPanelLayout
.parent
.call( this, config
);
1000 this.label
= config
.label
;
1001 this.tabItem
= null;
1002 this.active
= false;
1005 this.$element
.addClass( 'oo-ui-tabPanelLayout' );
1010 OO
.inheritClass( OO
.ui
.TabPanelLayout
, OO
.ui
.PanelLayout
);
1015 * An 'active' event is emitted when the tab panel becomes active. Tab panels become active when they are
1016 * shown in a index layout that is configured to display only one tab panel at a time.
1019 * @param {boolean} active Tab panel is active
1025 * Get the symbolic name of the tab panel.
1027 * @return {string} Symbolic name of tab panel
1029 OO
.ui
.TabPanelLayout
.prototype.getName = function () {
1034 * Check if tab panel is active.
1036 * Tab panels become active when they are shown in a {@link OO.ui.IndexLayout index layout} that is configured to
1037 * display only one tab panel at a time. Additional CSS is applied to the tab panel's tab item to reflect the
1040 * @return {boolean} Tab panel is active
1042 OO
.ui
.TabPanelLayout
.prototype.isActive = function () {
1049 * The tab item allows users to access the tab panel from the index's tab
1050 * navigation. The tab item itself can be customized (with a label, level, etc.) using the #setupTabItem method.
1052 * @return {OO.ui.TabOptionWidget|null} Tab option widget
1054 OO
.ui
.TabPanelLayout
.prototype.getTabItem = function () {
1055 return this.tabItem
;
1059 * Set or unset the tab item.
1061 * Specify a {@link OO.ui.TabOptionWidget tab option} to set it,
1062 * or `null` to clear the tab item. To customize the tab item itself (e.g., to set a label or tab
1063 * level), use #setupTabItem instead of this method.
1065 * @param {OO.ui.TabOptionWidget|null} tabItem Tab option widget, null to clear
1068 OO
.ui
.TabPanelLayout
.prototype.setTabItem = function ( tabItem
) {
1069 this.tabItem
= tabItem
|| null;
1071 this.setupTabItem();
1077 * Set up the tab item.
1079 * Use this method to customize the tab item (e.g., to add a label or tab level). To set or unset
1080 * the tab item itself (with a {@link OO.ui.TabOptionWidget tab option} or `null`), use
1081 * the #setTabItem method instead.
1083 * @param {OO.ui.TabOptionWidget} tabItem Tab option widget to set up
1086 OO
.ui
.TabPanelLayout
.prototype.setupTabItem = function () {
1088 this.tabItem
.setLabel( this.label
);
1094 * Set the tab panel to its 'active' state.
1096 * Tab panels become active when they are shown in a index layout that is configured to display only
1097 * one tab panel at a time. Additional CSS is applied to the tab item to reflect the tab panel's
1098 * active state. Outside of the index context, setting the active state on a tab panel does nothing.
1100 * @param {boolean} active Tab panel is active
1103 OO
.ui
.TabPanelLayout
.prototype.setActive = function ( active
) {
1106 if ( active
!== this.active
) {
1107 this.active
= active
;
1108 this.$element
.toggleClass( 'oo-ui-tabPanelLayout-active', this.active
);
1109 this.emit( 'active', this.active
);
1114 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that users can select and display
1115 * from the booklet's optional {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated directly,
1116 * rather extended to include the required content and functionality.
1118 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the page's outline
1119 * item is customized (with a label, outline level, etc.) using the #setupOutlineItem method. See
1120 * {@link OO.ui.BookletLayout BookletLayout} for an example.
1123 * @extends OO.ui.PanelLayout
1126 * @param {string} name Unique symbolic name of page
1127 * @param {Object} [config] Configuration options
1129 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
1130 // Allow passing positional parameters inside the config object
1131 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1136 // Configuration initialization
1137 config
= $.extend( { scrollable
: true }, config
);
1139 // Parent constructor
1140 OO
.ui
.PageLayout
.parent
.call( this, config
);
1144 this.outlineItem
= null;
1145 this.active
= false;
1148 this.$element
.addClass( 'oo-ui-pageLayout' );
1153 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
1158 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1159 * shown in a booklet layout that is configured to display only one page at a time.
1162 * @param {boolean} active Page is active
1168 * Get the symbolic name of the page.
1170 * @return {string} Symbolic name of page
1172 OO
.ui
.PageLayout
.prototype.getName = function () {
1177 * Check if page is active.
1179 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is configured to display
1180 * only one page at a time. Additional CSS is applied to the page's outline item to reflect the active state.
1182 * @return {boolean} Page is active
1184 OO
.ui
.PageLayout
.prototype.isActive = function () {
1191 * The outline item allows users to access the page from the booklet's outline
1192 * navigation. The outline item itself can be customized (with a label, level, etc.) using the #setupOutlineItem method.
1194 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1196 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
1197 return this.outlineItem
;
1201 * Set or unset the outline item.
1203 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1204 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label or outline
1205 * level), use #setupOutlineItem instead of this method.
1207 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1210 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
1211 this.outlineItem
= outlineItem
|| null;
1212 if ( outlineItem
) {
1213 this.setupOutlineItem();
1219 * Set up the outline item.
1221 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or unset
1222 * the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or `null`), use
1223 * the #setOutlineItem method instead.
1225 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1228 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
1233 * Set the page to its 'active' state.
1235 * Pages become active when they are shown in a booklet layout that is configured to display only one page at a time. Additional
1236 * CSS is applied to the outline item to reflect the page's active state. Outside of the booklet
1237 * context, setting the active state on a page does nothing.
1239 * @param {boolean} active Page is active
1242 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
1245 if ( active
!== this.active
) {
1246 this.active
= active
;
1247 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
1248 this.emit( 'active', this.active
);
1253 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one panel is displayed
1254 * at a time, though the stack layout can also be configured to show all contained panels, one after another,
1255 * by setting the #continuous option to 'true'.
1258 * // A stack layout with two panels, configured to be displayed continously
1259 * var myStack = new OO.ui.StackLayout( {
1261 * new OO.ui.PanelLayout( {
1262 * $content: $( '<p>Panel One</p>' ),
1266 * new OO.ui.PanelLayout( {
1267 * $content: $( '<p>Panel Two</p>' ),
1274 * $( 'body' ).append( myStack.$element );
1277 * @extends OO.ui.PanelLayout
1278 * @mixins OO.ui.mixin.GroupElement
1281 * @param {Object} [config] Configuration options
1282 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel is displayed at a time.
1283 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1285 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
1286 // Configuration initialization
1287 config
= $.extend( { scrollable
: true }, config
);
1289 // Parent constructor
1290 OO
.ui
.StackLayout
.parent
.call( this, config
);
1292 // Mixin constructors
1293 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
1296 this.currentItem
= null;
1297 this.continuous
= !!config
.continuous
;
1300 this.$element
.addClass( 'oo-ui-stackLayout' );
1301 if ( this.continuous
) {
1302 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
1303 this.$element
.on( 'scroll', OO
.ui
.debounce( this.onScroll
.bind( this ), 250 ) );
1305 if ( Array
.isArray( config
.items
) ) {
1306 this.addItems( config
.items
);
1312 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
1313 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.mixin
.GroupElement
);
1318 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1319 * {@link #clearItems cleared} or {@link #setItem displayed}.
1322 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1326 * When used in continuous mode, this event is emitted when the user scrolls down
1327 * far enough such that currentItem is no longer visible.
1329 * @event visibleItemChange
1330 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1336 * Handle scroll events from the layout element
1338 * @param {jQuery.Event} e
1339 * @fires visibleItemChange
1341 OO
.ui
.StackLayout
.prototype.onScroll = function () {
1343 len
= this.items
.length
,
1344 currentIndex
= this.items
.indexOf( this.currentItem
),
1345 newIndex
= currentIndex
,
1346 containerRect
= this.$element
[ 0 ].getBoundingClientRect();
1348 if ( !containerRect
|| ( !containerRect
.top
&& !containerRect
.bottom
) ) {
1349 // Can't get bounding rect, possibly not attached.
1353 function getRect( item
) {
1354 return item
.$element
[ 0 ].getBoundingClientRect();
1357 function isVisible( item
) {
1358 var rect
= getRect( item
);
1359 return rect
.bottom
> containerRect
.top
&& rect
.top
< containerRect
.bottom
;
1362 currentRect
= getRect( this.currentItem
);
1364 if ( currentRect
.bottom
< containerRect
.top
) {
1365 // Scrolled down past current item
1366 while ( ++newIndex
< len
) {
1367 if ( isVisible( this.items
[ newIndex
] ) ) {
1371 } else if ( currentRect
.top
> containerRect
.bottom
) {
1372 // Scrolled up past current item
1373 while ( --newIndex
>= 0 ) {
1374 if ( isVisible( this.items
[ newIndex
] ) ) {
1380 if ( newIndex
!== currentIndex
) {
1381 this.emit( 'visibleItemChange', this.items
[ newIndex
] );
1386 * Get the current panel.
1388 * @return {OO.ui.Layout|null}
1390 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
1391 return this.currentItem
;
1395 * Unset the current item.
1398 * @param {OO.ui.StackLayout} layout
1401 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
1402 var prevItem
= this.currentItem
;
1403 if ( prevItem
=== null ) {
1407 this.currentItem
= null;
1408 this.emit( 'set', null );
1412 * Add panel layouts to the stack layout.
1414 * Panels will be added to the end of the stack layout array unless the optional index parameter specifies a different
1415 * insertion point. Adding a panel that is already in the stack will move it to the end of the array or the point specified
1418 * @param {OO.ui.Layout[]} items Panels to add
1419 * @param {number} [index] Index of the insertion point
1422 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
1423 // Update the visibility
1424 this.updateHiddenState( items
, this.currentItem
);
1427 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
, index
);
1429 if ( !this.currentItem
&& items
.length
) {
1430 this.setItem( items
[ 0 ] );
1437 * Remove the specified panels from the stack layout.
1439 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all panels,
1440 * you may wish to use the #clearItems method instead.
1442 * @param {OO.ui.Layout[]} items Panels to remove
1446 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
1448 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
1450 if ( items
.indexOf( this.currentItem
) !== -1 ) {
1451 if ( this.items
.length
) {
1452 this.setItem( this.items
[ 0 ] );
1454 this.unsetCurrentItem();
1462 * Clear all panels from the stack layout.
1464 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1465 * a subset of panels, use the #removeItems method.
1470 OO
.ui
.StackLayout
.prototype.clearItems = function () {
1471 this.unsetCurrentItem();
1472 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
1478 * Show the specified panel.
1480 * If another panel is currently displayed, it will be hidden.
1482 * @param {OO.ui.Layout} item Panel to show
1486 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
1487 if ( item
!== this.currentItem
) {
1488 this.updateHiddenState( this.items
, item
);
1490 if ( this.items
.indexOf( item
) !== -1 ) {
1491 this.currentItem
= item
;
1492 this.emit( 'set', item
);
1494 this.unsetCurrentItem();
1502 * Update the visibility of all items in case of non-continuous view.
1504 * Ensure all items are hidden except for the selected one.
1505 * This method does nothing when the stack is continuous.
1508 * @param {OO.ui.Layout[]} items Item list iterate over
1509 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1511 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
1514 if ( !this.continuous
) {
1515 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
1516 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
1517 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
1518 items
[ i
].$element
.attr( 'aria-hidden', 'true' );
1521 if ( selectedItem
) {
1522 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
1523 selectedItem
.$element
.removeAttr( 'aria-hidden' );
1529 * 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)
1530 * and its size is customized with the #menuSize config. The content area will fill all remaining space.
1533 * var menuLayout = new OO.ui.MenuLayout( {
1536 * menuPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1537 * contentPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1538 * select = new OO.ui.SelectWidget( {
1540 * new OO.ui.OptionWidget( {
1544 * new OO.ui.OptionWidget( {
1548 * new OO.ui.OptionWidget( {
1552 * new OO.ui.OptionWidget( {
1557 * } ).on( 'select', function ( item ) {
1558 * menuLayout.setMenuPosition( item.getData() );
1561 * menuLayout.$menu.append(
1562 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element )
1564 * menuLayout.$content.append(
1565 * contentPanel.$element.append( '<b>Content panel</b>', '<p>Note that the menu is positioned relative to the content panel: top, bottom, after, before.</p>')
1567 * $( 'body' ).append( menuLayout.$element );
1569 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1570 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1571 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1574 * .oo-ui-menuLayout-menu {
1578 * .oo-ui-menuLayout-content {
1586 * @extends OO.ui.Layout
1589 * @param {Object} [config] Configuration options
1590 * @cfg {boolean} [expanded=true] Expand the layout to fill the entire parent element.
1591 * @cfg {boolean} [showMenu=true] Show menu
1592 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1594 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
1595 // Configuration initialization
1596 config
= $.extend( {
1599 menuPosition
: 'before'
1602 // Parent constructor
1603 OO
.ui
.MenuLayout
.parent
.call( this, config
);
1605 this.expanded
= !!config
.expanded
;
1609 * @property {jQuery}
1611 this.$menu
= $( '<div>' );
1615 * @property {jQuery}
1617 this.$content
= $( '<div>' );
1621 .addClass( 'oo-ui-menuLayout-menu' );
1622 this.$content
.addClass( 'oo-ui-menuLayout-content' );
1624 .addClass( 'oo-ui-menuLayout' );
1625 if ( config
.expanded
) {
1626 this.$element
.addClass( 'oo-ui-menuLayout-expanded' );
1628 this.$element
.addClass( 'oo-ui-menuLayout-static' );
1630 this.setMenuPosition( config
.menuPosition
);
1631 this.toggleMenu( config
.showMenu
);
1636 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
1643 * @param {boolean} showMenu Show menu, omit to toggle
1646 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
1647 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
1649 if ( this.showMenu
!== showMenu
) {
1650 this.showMenu
= showMenu
;
1652 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu
)
1653 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu
);
1654 this.$menu
.attr( 'aria-hidden', this.showMenu
? 'false' : 'true' );
1661 * Check if menu is visible
1663 * @return {boolean} Menu is visible
1665 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
1666 return this.showMenu
;
1670 * Set menu position.
1672 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1673 * @throws {Error} If position value is not supported
1676 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
1677 this.$element
.removeClass( 'oo-ui-menuLayout-' + this.menuPosition
);
1678 this.menuPosition
= position
;
1679 if ( this.menuPosition
=== 'top' || this.menuPosition
=== 'before' ) {
1680 this.$element
.append( this.$menu
, this.$content
);
1682 this.$element
.append( this.$content
, this.$menu
);
1684 this.$element
.addClass( 'oo-ui-menuLayout-' + position
);
1690 * Get menu position.
1692 * @return {string} Menu position
1694 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
1695 return this.menuPosition
;
1699 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1700 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1701 * through the pages and select which one to display. By default, only one page is
1702 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1703 * the booklet layout automatically focuses on the first focusable element, unless the
1704 * default setting is changed. Optionally, booklets can be configured to show
1705 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1708 * // Example of a BookletLayout that contains two PageLayouts.
1710 * function PageOneLayout( name, config ) {
1711 * PageOneLayout.parent.call( this, name, config );
1712 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on the left)</p>' );
1714 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1715 * PageOneLayout.prototype.setupOutlineItem = function () {
1716 * this.outlineItem.setLabel( 'Page One' );
1719 * function PageTwoLayout( name, config ) {
1720 * PageTwoLayout.parent.call( this, name, config );
1721 * this.$element.append( '<p>Second page</p>' );
1723 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1724 * PageTwoLayout.prototype.setupOutlineItem = function () {
1725 * this.outlineItem.setLabel( 'Page Two' );
1728 * var page1 = new PageOneLayout( 'one' ),
1729 * page2 = new PageTwoLayout( 'two' );
1731 * var booklet = new OO.ui.BookletLayout( {
1735 * booklet.addPages ( [ page1, page2 ] );
1736 * $( 'body' ).append( booklet.$element );
1739 * @extends OO.ui.MenuLayout
1742 * @param {Object} [config] Configuration options
1743 * @cfg {boolean} [continuous=false] Show all pages, one after another
1744 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is displayed. Disabled on mobile.
1745 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the pages of the booklet.
1746 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
1748 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
1749 // Configuration initialization
1750 config
= config
|| {};
1752 // Parent constructor
1753 OO
.ui
.BookletLayout
.parent
.call( this, config
);
1756 this.currentPageName
= null;
1758 this.ignoreFocus
= false;
1759 this.stackLayout
= new OO
.ui
.StackLayout( {
1760 continuous
: !!config
.continuous
,
1761 expanded
: this.expanded
1763 this.$content
.append( this.stackLayout
.$element
);
1764 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
1765 this.outlineVisible
= false;
1766 this.outlined
= !!config
.outlined
;
1767 if ( this.outlined
) {
1768 this.editable
= !!config
.editable
;
1769 this.outlineControlsWidget
= null;
1770 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget();
1771 this.outlinePanel
= new OO
.ui
.PanelLayout( {
1772 expanded
: this.expanded
,
1775 this.$menu
.append( this.outlinePanel
.$element
);
1776 this.outlineVisible
= true;
1777 if ( this.editable
) {
1778 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
1779 this.outlineSelectWidget
1783 this.toggleMenu( this.outlined
);
1786 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
1787 if ( this.outlined
) {
1788 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
1789 this.scrolling
= false;
1790 this.stackLayout
.connect( this, { visibleItemChange
: 'onStackLayoutVisibleItemChange' } );
1792 if ( this.autoFocus
) {
1793 // Event 'focus' does not bubble, but 'focusin' does
1794 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
1798 this.$element
.addClass( 'oo-ui-bookletLayout' );
1799 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
1800 if ( this.outlined
) {
1801 this.outlinePanel
.$element
1802 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1803 .append( this.outlineSelectWidget
.$element
);
1804 if ( this.editable
) {
1805 this.outlinePanel
.$element
1806 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1807 .append( this.outlineControlsWidget
.$element
);
1814 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
1819 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the booklet layout.
1821 * @param {OO.ui.PageLayout} page Current page
1825 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
1828 * @param {OO.ui.PageLayout[]} page Added pages
1829 * @param {number} index Index pages were added at
1833 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
1834 * {@link #removePages removed} from the booklet.
1837 * @param {OO.ui.PageLayout[]} pages Removed pages
1843 * Handle stack layout focus.
1846 * @param {jQuery.Event} e Focusin event
1848 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
1851 // Find the page that an element was focused within
1852 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
1853 for ( name
in this.pages
) {
1854 // Check for page match, exclude current page to find only page changes
1855 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
1856 this.setPage( name
);
1863 * Handle visibleItemChange events from the stackLayout
1865 * The next visible page is set as the current page by selecting it
1868 * @param {OO.ui.PageLayout} page The next visible page in the layout
1870 OO
.ui
.BookletLayout
.prototype.onStackLayoutVisibleItemChange = function ( page
) {
1871 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
1872 // try and scroll the item into view again.
1873 this.scrolling
= true;
1874 this.outlineSelectWidget
.selectItemByData( page
.getName() );
1875 this.scrolling
= false;
1879 * Handle stack layout set events.
1882 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
1884 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
1885 var promise
, layout
= this;
1886 // If everything is unselected, do nothing
1890 // For continuous BookletLayouts, scroll the selected page into view first
1891 if ( this.stackLayout
.continuous
&& !this.scrolling
) {
1892 promise
= page
.scrollElementIntoView();
1894 promise
= $.Deferred().resolve();
1896 // Focus the first element on the newly selected panel
1897 if ( this.autoFocus
&& !OO
.ui
.isMobile() ) {
1898 promise
.done( function () {
1905 * Focus the first input in the current page.
1907 * If no page is selected, the first selectable page will be selected.
1908 * If the focus is already in an element on the current page, nothing will happen.
1910 * @param {number} [itemIndex] A specific item to focus on
1912 OO
.ui
.BookletLayout
.prototype.focus = function ( itemIndex
) {
1914 items
= this.stackLayout
.getItems();
1916 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
1917 page
= items
[ itemIndex
];
1919 page
= this.stackLayout
.getCurrentItem();
1922 if ( !page
&& this.outlined
) {
1923 this.selectFirstSelectablePage();
1924 page
= this.stackLayout
.getCurrentItem();
1929 // Only change the focus if is not already in the current page
1930 if ( !OO
.ui
.contains( page
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
1936 * Find the first focusable input in the booklet layout and focus
1939 OO
.ui
.BookletLayout
.prototype.focusFirstFocusable = function () {
1940 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
1944 * Handle outline widget select events.
1947 * @param {OO.ui.OptionWidget|null} item Selected item
1949 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
1951 this.setPage( item
.getData() );
1956 * Check if booklet has an outline.
1958 * @return {boolean} Booklet has an outline
1960 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
1961 return this.outlined
;
1965 * Check if booklet has editing controls.
1967 * @return {boolean} Booklet is editable
1969 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
1970 return this.editable
;
1974 * Check if booklet has a visible outline.
1976 * @return {boolean} Outline is visible
1978 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
1979 return this.outlined
&& this.outlineVisible
;
1983 * Hide or show the outline.
1985 * @param {boolean} [show] Show outline, omit to invert current state
1988 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
1991 if ( this.outlined
) {
1992 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
1993 this.outlineVisible
= show
;
1994 this.toggleMenu( show
);
1995 if ( show
&& this.editable
) {
1996 // HACK: Kill dumb scrollbars when the sidebar stops animating, see T161798. Only necessary when
1997 // outline controls are present, delay matches transition on `.oo-ui-menuLayout-menu`.
1998 setTimeout( function () {
1999 OO
.ui
.Element
.static.reconsiderScrollbars( booklet
.outlinePanel
.$element
[ 0 ] );
2008 * Find the page closest to the specified page.
2010 * @param {OO.ui.PageLayout} page Page to use as a reference point
2011 * @return {OO.ui.PageLayout|null} Page closest to the specified page
2013 OO
.ui
.BookletLayout
.prototype.findClosestPage = function ( page
) {
2014 var next
, prev
, level
,
2015 pages
= this.stackLayout
.getItems(),
2016 index
= pages
.indexOf( page
);
2018 if ( index
!== -1 ) {
2019 next
= pages
[ index
+ 1 ];
2020 prev
= pages
[ index
- 1 ];
2021 // Prefer adjacent pages at the same level
2022 if ( this.outlined
) {
2023 level
= this.outlineSelectWidget
.findItemFromData( page
.getName() ).getLevel();
2026 level
=== this.outlineSelectWidget
.findItemFromData( prev
.getName() ).getLevel()
2032 level
=== this.outlineSelectWidget
.findItemFromData( next
.getName() ).getLevel()
2038 return prev
|| next
|| null;
2042 * Get the outline widget.
2044 * If the booklet is not outlined, the method will return `null`.
2046 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
2048 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
2049 return this.outlineSelectWidget
;
2053 * Get the outline controls widget.
2055 * If the outline is not editable, the method will return `null`.
2057 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
2059 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
2060 return this.outlineControlsWidget
;
2064 * Get a page by its symbolic name.
2066 * @param {string} name Symbolic name of page
2067 * @return {OO.ui.PageLayout|undefined} Page, if found
2069 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
2070 return this.pages
[ name
];
2074 * Get the current page.
2076 * @return {OO.ui.PageLayout|undefined} Current page, if found
2078 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
2079 var name
= this.getCurrentPageName();
2080 return name
? this.getPage( name
) : undefined;
2084 * Get the symbolic name of the current page.
2086 * @return {string|null} Symbolic name of the current page
2088 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
2089 return this.currentPageName
;
2093 * Add pages to the booklet layout
2095 * When pages are added with the same names as existing pages, the existing pages will be
2096 * automatically removed before the new pages are added.
2098 * @param {OO.ui.PageLayout[]} pages Pages to add
2099 * @param {number} index Index of the insertion point
2103 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
2104 var i
, len
, name
, page
, item
, currentIndex
,
2105 stackLayoutPages
= this.stackLayout
.getItems(),
2109 // Remove pages with same names
2110 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2112 name
= page
.getName();
2114 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
2115 // Correct the insertion index
2116 currentIndex
= stackLayoutPages
.indexOf( this.pages
[ name
] );
2117 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2120 remove
.push( this.pages
[ name
] );
2123 if ( remove
.length
) {
2124 this.removePages( remove
);
2128 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2130 name
= page
.getName();
2131 this.pages
[ page
.getName() ] = page
;
2132 if ( this.outlined
) {
2133 item
= new OO
.ui
.OutlineOptionWidget( { data
: name
} );
2134 page
.setOutlineItem( item
);
2139 if ( this.outlined
&& items
.length
) {
2140 this.outlineSelectWidget
.addItems( items
, index
);
2141 this.selectFirstSelectablePage();
2143 this.stackLayout
.addItems( pages
, index
);
2144 this.emit( 'add', pages
, index
);
2150 * Remove the specified pages from the booklet layout.
2152 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2154 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2158 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
2159 var i
, len
, name
, page
,
2162 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2164 name
= page
.getName();
2165 delete this.pages
[ name
];
2166 if ( this.outlined
) {
2167 items
.push( this.outlineSelectWidget
.findItemFromData( name
) );
2168 page
.setOutlineItem( null );
2171 if ( this.outlined
&& items
.length
) {
2172 this.outlineSelectWidget
.removeItems( items
);
2173 this.selectFirstSelectablePage();
2175 this.stackLayout
.removeItems( pages
);
2176 this.emit( 'remove', pages
);
2182 * Clear all pages from the booklet layout.
2184 * To remove only a subset of pages from the booklet, use the #removePages method.
2189 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
2191 pages
= this.stackLayout
.getItems();
2194 this.currentPageName
= null;
2195 if ( this.outlined
) {
2196 this.outlineSelectWidget
.clearItems();
2197 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2198 pages
[ i
].setOutlineItem( null );
2201 this.stackLayout
.clearItems();
2203 this.emit( 'remove', pages
);
2209 * Set the current page by symbolic name.
2212 * @param {string} name Symbolic name of page
2214 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
2217 page
= this.pages
[ name
],
2218 previousPage
= this.currentPageName
&& this.pages
[ this.currentPageName
];
2220 if ( name
!== this.currentPageName
) {
2221 if ( this.outlined
) {
2222 selectedItem
= this.outlineSelectWidget
.findSelectedItem();
2223 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2224 this.outlineSelectWidget
.selectItemByData( name
);
2228 if ( previousPage
) {
2229 previousPage
.setActive( false );
2230 // Blur anything focused if the next page doesn't have anything focusable.
2231 // This is not needed if the next page has something focusable (because once it is focused
2232 // this blur happens automatically). If the layout is non-continuous, this check is
2233 // meaningless because the next page is not visible yet and thus can't hold focus.
2236 !OO
.ui
.isMobile() &&
2237 this.stackLayout
.continuous
&&
2238 OO
.ui
.findFocusable( page
.$element
).length
!== 0
2240 $focused
= previousPage
.$element
.find( ':focus' );
2241 if ( $focused
.length
) {
2242 $focused
[ 0 ].blur();
2246 this.currentPageName
= name
;
2247 page
.setActive( true );
2248 this.stackLayout
.setItem( page
);
2249 if ( !this.stackLayout
.continuous
&& previousPage
) {
2250 // This should not be necessary, since any inputs on the previous page should have been
2251 // blurred when it was hidden, but browsers are not very consistent about this.
2252 $focused
= previousPage
.$element
.find( ':focus' );
2253 if ( $focused
.length
) {
2254 $focused
[ 0 ].blur();
2257 this.emit( 'set', page
);
2263 * Select the first selectable page.
2267 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
2268 if ( !this.outlineSelectWidget
.findSelectedItem() ) {
2269 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.findFirstSelectableItem() );
2276 * IndexLayouts contain {@link OO.ui.TabPanelLayout tab panel layouts} as well as
2277 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the tab panels and
2278 * select which one to display. By default, only one tab panel is displayed at a time. When a user
2279 * navigates to a new tab panel, the index layout automatically focuses on the first focusable element,
2280 * unless the default setting is changed.
2282 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2285 * // Example of a IndexLayout that contains two TabPanelLayouts.
2287 * function TabPanelOneLayout( name, config ) {
2288 * TabPanelOneLayout.parent.call( this, name, config );
2289 * this.$element.append( '<p>First tab panel</p>' );
2291 * OO.inheritClass( TabPanelOneLayout, OO.ui.TabPanelLayout );
2292 * TabPanelOneLayout.prototype.setupTabItem = function () {
2293 * this.tabItem.setLabel( 'Tab panel one' );
2296 * var tabPanel1 = new TabPanelOneLayout( 'one' ),
2297 * tabPanel2 = new OO.ui.TabPanelLayout( 'two', { label: 'Tab panel two' } );
2299 * tabPanel2.$element.append( '<p>Second tab panel</p>' );
2301 * var index = new OO.ui.IndexLayout();
2303 * index.addTabPanels ( [ tabPanel1, tabPanel2 ] );
2304 * $( 'body' ).append( index.$element );
2307 * @extends OO.ui.MenuLayout
2310 * @param {Object} [config] Configuration options
2311 * @cfg {boolean} [continuous=false] Show all tab panels, one after another
2312 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new tab panel is displayed. Disabled on mobile.
2314 OO
.ui
.IndexLayout
= function OoUiIndexLayout( config
) {
2315 // Configuration initialization
2316 config
= $.extend( {}, config
, { menuPosition
: 'top' } );
2318 // Parent constructor
2319 OO
.ui
.IndexLayout
.parent
.call( this, config
);
2322 this.currentTabPanelName
= null;
2323 this.tabPanels
= {};
2325 this.ignoreFocus
= false;
2326 this.stackLayout
= new OO
.ui
.StackLayout( {
2327 continuous
: !!config
.continuous
,
2328 expanded
: this.expanded
2330 this.$content
.append( this.stackLayout
.$element
);
2331 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
2333 this.tabSelectWidget
= new OO
.ui
.TabSelectWidget();
2334 this.tabPanel
= new OO
.ui
.PanelLayout( {
2335 expanded
: this.expanded
2337 this.$menu
.append( this.tabPanel
.$element
);
2339 this.toggleMenu( true );
2342 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
2343 this.tabSelectWidget
.connect( this, { select
: 'onTabSelectWidgetSelect' } );
2344 if ( this.autoFocus
) {
2345 // Event 'focus' does not bubble, but 'focusin' does
2346 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
2350 this.$element
.addClass( 'oo-ui-indexLayout' );
2351 this.stackLayout
.$element
.addClass( 'oo-ui-indexLayout-stackLayout' );
2352 this.tabPanel
.$element
2353 .addClass( 'oo-ui-indexLayout-tabPanel' )
2354 .append( this.tabSelectWidget
.$element
);
2359 OO
.inheritClass( OO
.ui
.IndexLayout
, OO
.ui
.MenuLayout
);
2364 * A 'set' event is emitted when a tab panel is {@link #setTabPanel set} to be displayed by the index layout.
2366 * @param {OO.ui.TabPanelLayout} tabPanel Current tab panel
2370 * An 'add' event is emitted when tab panels are {@link #addTabPanels added} to the index layout.
2373 * @param {OO.ui.TabPanelLayout[]} tabPanel Added tab panels
2374 * @param {number} index Index tab panels were added at
2378 * A 'remove' event is emitted when tab panels are {@link #clearTabPanels cleared} or
2379 * {@link #removeTabPanels removed} from the index.
2382 * @param {OO.ui.TabPanelLayout[]} tabPanel Removed tab panels
2388 * Handle stack layout focus.
2391 * @param {jQuery.Event} e Focusing event
2393 OO
.ui
.IndexLayout
.prototype.onStackLayoutFocus = function ( e
) {
2396 // Find the tab panel that an element was focused within
2397 $target
= $( e
.target
).closest( '.oo-ui-tabPanelLayout' );
2398 for ( name
in this.tabPanels
) {
2399 // Check for tab panel match, exclude current tab panel to find only tab panel changes
2400 if ( this.tabPanels
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentTabPanelName
) {
2401 this.setTabPanel( name
);
2408 * Handle stack layout set events.
2411 * @param {OO.ui.PanelLayout|null} tabPanel The tab panel that is now the current panel
2413 OO
.ui
.IndexLayout
.prototype.onStackLayoutSet = function ( tabPanel
) {
2414 // If everything is unselected, do nothing
2418 // Focus the first element on the newly selected panel
2419 if ( this.autoFocus
&& !OO
.ui
.isMobile() ) {
2425 * Focus the first input in the current tab panel.
2427 * If no tab panel is selected, the first selectable tab panel will be selected.
2428 * If the focus is already in an element on the current tab panel, nothing will happen.
2430 * @param {number} [itemIndex] A specific item to focus on
2432 OO
.ui
.IndexLayout
.prototype.focus = function ( itemIndex
) {
2434 items
= this.stackLayout
.getItems();
2436 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2437 tabPanel
= items
[ itemIndex
];
2439 tabPanel
= this.stackLayout
.getCurrentItem();
2443 this.selectFirstSelectableTabPanel();
2444 tabPanel
= this.stackLayout
.getCurrentItem();
2449 // Only change the focus if is not already in the current page
2450 if ( !OO
.ui
.contains( tabPanel
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
2456 * Find the first focusable input in the index layout and focus
2459 OO
.ui
.IndexLayout
.prototype.focusFirstFocusable = function () {
2460 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2464 * Handle tab widget select events.
2467 * @param {OO.ui.OptionWidget|null} item Selected item
2469 OO
.ui
.IndexLayout
.prototype.onTabSelectWidgetSelect = function ( item
) {
2471 this.setTabPanel( item
.getData() );
2476 * Get the tab panel closest to the specified tab panel.
2478 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2479 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2481 OO
.ui
.IndexLayout
.prototype.getClosestTabPanel = function ( tabPanel
) {
2482 var next
, prev
, level
,
2483 tabPanels
= this.stackLayout
.getItems(),
2484 index
= tabPanels
.indexOf( tabPanel
);
2486 if ( index
!== -1 ) {
2487 next
= tabPanels
[ index
+ 1 ];
2488 prev
= tabPanels
[ index
- 1 ];
2489 // Prefer adjacent tab panels at the same level
2490 level
= this.tabSelectWidget
.findItemFromData( tabPanel
.getName() ).getLevel();
2493 level
=== this.tabSelectWidget
.findItemFromData( prev
.getName() ).getLevel()
2499 level
=== this.tabSelectWidget
.findItemFromData( next
.getName() ).getLevel()
2504 return prev
|| next
|| null;
2508 * Get the tabs widget.
2510 * @return {OO.ui.TabSelectWidget} Tabs widget
2512 OO
.ui
.IndexLayout
.prototype.getTabs = function () {
2513 return this.tabSelectWidget
;
2517 * Get a tab panel by its symbolic name.
2519 * @param {string} name Symbolic name of tab panel
2520 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2522 OO
.ui
.IndexLayout
.prototype.getTabPanel = function ( name
) {
2523 return this.tabPanels
[ name
];
2527 * Get the current tab panel.
2529 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2531 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanel = function () {
2532 var name
= this.getCurrentTabPanelName();
2533 return name
? this.getTabPanel( name
) : undefined;
2537 * Get the symbolic name of the current tab panel.
2539 * @return {string|null} Symbolic name of the current tab panel
2541 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanelName = function () {
2542 return this.currentTabPanelName
;
2546 * Add tab panels to the index layout
2548 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2549 * will be automatically removed before the new tab panels are added.
2551 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2552 * @param {number} index Index of the insertion point
2556 OO
.ui
.IndexLayout
.prototype.addTabPanels = function ( tabPanels
, index
) {
2557 var i
, len
, name
, tabPanel
, item
, currentIndex
,
2558 stackLayoutTabPanels
= this.stackLayout
.getItems(),
2562 // Remove tab panels with same names
2563 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2564 tabPanel
= tabPanels
[ i
];
2565 name
= tabPanel
.getName();
2567 if ( Object
.prototype.hasOwnProperty
.call( this.tabPanels
, name
) ) {
2568 // Correct the insertion index
2569 currentIndex
= stackLayoutTabPanels
.indexOf( this.tabPanels
[ name
] );
2570 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2573 remove
.push( this.tabPanels
[ name
] );
2576 if ( remove
.length
) {
2577 this.removeTabPanels( remove
);
2580 // Add new tab panels
2581 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2582 tabPanel
= tabPanels
[ i
];
2583 name
= tabPanel
.getName();
2584 this.tabPanels
[ tabPanel
.getName() ] = tabPanel
;
2585 item
= new OO
.ui
.TabOptionWidget( { data
: name
} );
2586 tabPanel
.setTabItem( item
);
2590 if ( items
.length
) {
2591 this.tabSelectWidget
.addItems( items
, index
);
2592 this.selectFirstSelectableTabPanel();
2594 this.stackLayout
.addItems( tabPanels
, index
);
2595 this.emit( 'add', tabPanels
, index
);
2601 * Remove the specified tab panels from the index layout.
2603 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2605 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2609 OO
.ui
.IndexLayout
.prototype.removeTabPanels = function ( tabPanels
) {
2610 var i
, len
, name
, tabPanel
,
2613 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2614 tabPanel
= tabPanels
[ i
];
2615 name
= tabPanel
.getName();
2616 delete this.tabPanels
[ name
];
2617 items
.push( this.tabSelectWidget
.findItemFromData( name
) );
2618 tabPanel
.setTabItem( null );
2620 if ( items
.length
) {
2621 this.tabSelectWidget
.removeItems( items
);
2622 this.selectFirstSelectableTabPanel();
2624 this.stackLayout
.removeItems( tabPanels
);
2625 this.emit( 'remove', tabPanels
);
2631 * Clear all tab panels from the index layout.
2633 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2638 OO
.ui
.IndexLayout
.prototype.clearTabPanels = function () {
2640 tabPanels
= this.stackLayout
.getItems();
2642 this.tabPanels
= {};
2643 this.currentTabPanelName
= null;
2644 this.tabSelectWidget
.clearItems();
2645 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2646 tabPanels
[ i
].setTabItem( null );
2648 this.stackLayout
.clearItems();
2650 this.emit( 'remove', tabPanels
);
2656 * Set the current tab panel by symbolic name.
2659 * @param {string} name Symbolic name of tab panel
2661 OO
.ui
.IndexLayout
.prototype.setTabPanel = function ( name
) {
2664 tabPanel
= this.tabPanels
[ name
],
2665 previousTabPanel
= this.currentTabPanelName
&& this.tabPanels
[ this.currentTabPanelName
];
2667 if ( name
!== this.currentTabPanelName
) {
2668 selectedItem
= this.tabSelectWidget
.findSelectedItem();
2669 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2670 this.tabSelectWidget
.selectItemByData( name
);
2673 if ( previousTabPanel
) {
2674 previousTabPanel
.setActive( false );
2675 // Blur anything focused if the next tab panel doesn't have anything focusable.
2676 // This is not needed if the next tab panel has something focusable (because once it is focused
2677 // this blur happens automatically). If the layout is non-continuous, this check is
2678 // meaningless because the next tab panel is not visible yet and thus can't hold focus.
2681 !OO
.ui
.isMobile() &&
2682 this.stackLayout
.continuous
&&
2683 OO
.ui
.findFocusable( tabPanel
.$element
).length
!== 0
2685 $focused
= previousTabPanel
.$element
.find( ':focus' );
2686 if ( $focused
.length
) {
2687 $focused
[ 0 ].blur();
2691 this.currentTabPanelName
= name
;
2692 tabPanel
.setActive( true );
2693 this.stackLayout
.setItem( tabPanel
);
2694 if ( !this.stackLayout
.continuous
&& previousTabPanel
) {
2695 // This should not be necessary, since any inputs on the previous tab panel should have been
2696 // blurred when it was hidden, but browsers are not very consistent about this.
2697 $focused
= previousTabPanel
.$element
.find( ':focus' );
2698 if ( $focused
.length
) {
2699 $focused
[ 0 ].blur();
2702 this.emit( 'set', tabPanel
);
2708 * Select the first selectable tab panel.
2712 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableTabPanel = function () {
2713 if ( !this.tabSelectWidget
.findSelectedItem() ) {
2714 this.tabSelectWidget
.selectItem( this.tabSelectWidget
.findFirstSelectableItem() );
2721 * ToggleWidget implements basic behavior of widgets with an on/off state.
2722 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2726 * @extends OO.ui.Widget
2729 * @param {Object} [config] Configuration options
2730 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2731 * By default, the toggle is in the 'off' state.
2733 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
2734 // Configuration initialization
2735 config
= config
|| {};
2737 // Parent constructor
2738 OO
.ui
.ToggleWidget
.parent
.call( this, config
);
2744 this.$element
.addClass( 'oo-ui-toggleWidget' );
2745 this.setValue( !!config
.value
);
2750 OO
.inheritClass( OO
.ui
.ToggleWidget
, OO
.ui
.Widget
);
2757 * A change event is emitted when the on/off state of the toggle changes.
2759 * @param {boolean} value Value representing the new state of the toggle
2765 * Get the value representing the toggle’s state.
2767 * @return {boolean} The on/off state of the toggle
2769 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
2774 * Set the state of the toggle: `true` for 'on', `false` for 'off'.
2776 * @param {boolean} value The state of the toggle
2780 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
2782 if ( this.value
!== value
) {
2784 this.emit( 'change', value
);
2785 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
2786 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
2792 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
2793 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
2794 * configured with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators},
2795 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
2796 * and {@link OO.ui.mixin.LabelElement labels}. Please see
2797 * the [OOUI documentation][1] on MediaWiki for more information.
2800 * // Toggle buttons in the 'off' and 'on' state.
2801 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
2802 * label: 'Toggle Button off'
2804 * var toggleButton2 = new OO.ui.ToggleButtonWidget( {
2805 * label: 'Toggle Button on',
2808 * // Append the buttons to the DOM.
2809 * $( 'body' ).append( toggleButton1.$element, toggleButton2.$element );
2811 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Buttons_and_Switches#Toggle_buttons
2814 * @extends OO.ui.ToggleWidget
2815 * @mixins OO.ui.mixin.ButtonElement
2816 * @mixins OO.ui.mixin.IconElement
2817 * @mixins OO.ui.mixin.IndicatorElement
2818 * @mixins OO.ui.mixin.LabelElement
2819 * @mixins OO.ui.mixin.TitledElement
2820 * @mixins OO.ui.mixin.FlaggedElement
2821 * @mixins OO.ui.mixin.TabIndexedElement
2824 * @param {Object} [config] Configuration options
2825 * @cfg {boolean} [value=false] The toggle button’s initial on/off
2826 * state. By default, the button is in the 'off' state.
2828 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
2829 // Configuration initialization
2830 config
= config
|| {};
2832 // Parent constructor
2833 OO
.ui
.ToggleButtonWidget
.parent
.call( this, config
);
2835 // Mixin constructors
2836 OO
.ui
.mixin
.ButtonElement
.call( this, $.extend( {}, config
, { active
: this.active
} ) );
2837 OO
.ui
.mixin
.IconElement
.call( this, config
);
2838 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
2839 OO
.ui
.mixin
.LabelElement
.call( this, config
);
2840 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
2841 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
2842 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
2845 this.connect( this, { click
: 'onAction' } );
2848 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
2850 .addClass( 'oo-ui-toggleButtonWidget' )
2851 .append( this.$button
);
2856 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
2857 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
2858 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IconElement
);
2859 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
2860 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.LabelElement
);
2861 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TitledElement
);
2862 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
2863 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2865 /* Static Properties */
2871 OO
.ui
.ToggleButtonWidget
.static.tagName
= 'span';
2876 * Handle the button action being triggered.
2880 OO
.ui
.ToggleButtonWidget
.prototype.onAction = function () {
2881 this.setValue( !this.value
);
2887 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
2889 if ( value
!== this.value
) {
2890 // Might be called from parent constructor before ButtonElement constructor
2891 if ( this.$button
) {
2892 this.$button
.attr( 'aria-pressed', value
.toString() );
2894 this.setActive( value
);
2898 OO
.ui
.ToggleButtonWidget
.parent
.prototype.setValue
.call( this, value
);
2906 OO
.ui
.ToggleButtonWidget
.prototype.setButtonElement = function ( $button
) {
2907 if ( this.$button
) {
2908 this.$button
.removeAttr( 'aria-pressed' );
2910 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement
.call( this, $button
);
2911 this.$button
.attr( 'aria-pressed', this.value
.toString() );
2915 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
2916 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
2917 * visually by a slider in the leftmost position.
2920 * // Toggle switches in the 'off' and 'on' position.
2921 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget();
2922 * var toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
2926 * // Create a FieldsetLayout to layout and label switches
2927 * var fieldset = new OO.ui.FieldsetLayout( {
2928 * label: 'Toggle switches'
2930 * fieldset.addItems( [
2931 * new OO.ui.FieldLayout( toggleSwitch1, { label: 'Off', align: 'top' } ),
2932 * new OO.ui.FieldLayout( toggleSwitch2, { label: 'On', align: 'top' } )
2934 * $( 'body' ).append( fieldset.$element );
2937 * @extends OO.ui.ToggleWidget
2938 * @mixins OO.ui.mixin.TabIndexedElement
2941 * @param {Object} [config] Configuration options
2942 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
2943 * By default, the toggle switch is in the 'off' position.
2945 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
2946 // Parent constructor
2947 OO
.ui
.ToggleSwitchWidget
.parent
.call( this, config
);
2949 // Mixin constructors
2950 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
2953 this.dragging
= false;
2954 this.dragStart
= null;
2955 this.sliding
= false;
2956 this.$glow
= $( '<span>' );
2957 this.$grip
= $( '<span>' );
2961 click
: this.onClick
.bind( this ),
2962 keypress
: this.onKeyPress
.bind( this )
2966 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
2967 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
2969 .addClass( 'oo-ui-toggleSwitchWidget' )
2970 .attr( 'role', 'checkbox' )
2971 .append( this.$glow
, this.$grip
);
2976 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
2977 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2982 * Handle mouse click events.
2985 * @param {jQuery.Event} e Mouse click event
2987 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
2988 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
2989 this.setValue( !this.value
);
2995 * Handle key press events.
2998 * @param {jQuery.Event} e Key press event
3000 OO
.ui
.ToggleSwitchWidget
.prototype.onKeyPress = function ( e
) {
3001 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
3002 this.setValue( !this.value
);
3010 OO
.ui
.ToggleSwitchWidget
.prototype.setValue = function ( value
) {
3011 OO
.ui
.ToggleSwitchWidget
.parent
.prototype.setValue
.call( this, value
);
3012 this.$element
.attr( 'aria-checked', this.value
.toString() );
3019 OO
.ui
.ToggleSwitchWidget
.prototype.simulateLabelClick = function () {
3020 if ( !this.isDisabled() ) {
3021 this.setValue( !this.value
);
3027 * OutlineControlsWidget is a set of controls for an {@link OO.ui.OutlineSelectWidget outline select widget}.
3028 * Controls include moving items up and down, removing items, and adding different kinds of items.
3030 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3033 * @extends OO.ui.Widget
3034 * @mixins OO.ui.mixin.GroupElement
3035 * @mixins OO.ui.mixin.IconElement
3038 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
3039 * @param {Object} [config] Configuration options
3040 * @cfg {Object} [abilities] List of abilties
3041 * @cfg {boolean} [abilities.move=true] Allow moving movable items
3042 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
3044 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
3045 // Allow passing positional parameters inside the config object
3046 if ( OO
.isPlainObject( outline
) && config
=== undefined ) {
3048 outline
= config
.outline
;
3051 // Configuration initialization
3052 config
= $.extend( { icon
: 'add' }, config
);
3054 // Parent constructor
3055 OO
.ui
.OutlineControlsWidget
.parent
.call( this, config
);
3057 // Mixin constructors
3058 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3059 OO
.ui
.mixin
.IconElement
.call( this, config
);
3062 this.outline
= outline
;
3063 this.$movers
= $( '<div>' );
3064 this.upButton
= new OO
.ui
.ButtonWidget( {
3067 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
3069 this.downButton
= new OO
.ui
.ButtonWidget( {
3072 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
3074 this.removeButton
= new OO
.ui
.ButtonWidget( {
3077 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
3079 this.abilities
= { move: true, remove
: true };
3082 outline
.connect( this, {
3083 select
: 'onOutlineChange',
3084 add
: 'onOutlineChange',
3085 remove
: 'onOutlineChange'
3087 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
3088 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
3089 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
3092 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
3093 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
3095 .addClass( 'oo-ui-outlineControlsWidget-movers' )
3096 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
3097 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
3098 this.setAbilities( config
.abilities
|| {} );
3103 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
3104 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.GroupElement
);
3105 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.IconElement
);
3111 * @param {number} places Number of places to move
3123 * @param {Object} abilities List of abilties
3124 * @param {boolean} [abilities.move] Allow moving movable items
3125 * @param {boolean} [abilities.remove] Allow removing removable items
3127 OO
.ui
.OutlineControlsWidget
.prototype.setAbilities = function ( abilities
) {
3130 for ( ability
in this.abilities
) {
3131 if ( abilities
[ ability
] !== undefined ) {
3132 this.abilities
[ ability
] = !!abilities
[ ability
];
3136 this.onOutlineChange();
3140 * Handle outline change events.
3144 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
3145 var i
, len
, firstMovable
, lastMovable
,
3146 items
= this.outline
.getItems(),
3147 selectedItem
= this.outline
.findSelectedItem(),
3148 movable
= this.abilities
.move && selectedItem
&& selectedItem
.isMovable(),
3149 removable
= this.abilities
.remove
&& selectedItem
&& selectedItem
.isRemovable();
3154 while ( ++i
< len
) {
3155 if ( items
[ i
].isMovable() ) {
3156 firstMovable
= items
[ i
];
3162 if ( items
[ i
].isMovable() ) {
3163 lastMovable
= items
[ i
];
3168 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
3169 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
3170 this.removeButton
.setDisabled( !removable
);
3174 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3176 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3177 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3181 * @extends OO.ui.DecoratedOptionWidget
3184 * @param {Object} [config] Configuration options
3185 * @cfg {number} [level] Indentation level
3186 * @cfg {boolean} [movable] Allow modification from {@link OO.ui.OutlineControlsWidget outline controls}.
3188 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
3189 // Configuration initialization
3190 config
= config
|| {};
3192 // Parent constructor
3193 OO
.ui
.OutlineOptionWidget
.parent
.call( this, config
);
3197 this.movable
= !!config
.movable
;
3198 this.removable
= !!config
.removable
;
3201 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
3202 this.setLevel( config
.level
);
3207 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3209 /* Static Properties */
3215 OO
.ui
.OutlineOptionWidget
.static.highlightable
= true;
3221 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
3226 * @property {string}
3228 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
3233 * @property {number}
3235 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
3240 * Check if item is movable.
3242 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3244 * @return {boolean} Item is movable
3246 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
3247 return this.movable
;
3251 * Check if item is removable.
3253 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3255 * @return {boolean} Item is removable
3257 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
3258 return this.removable
;
3262 * Get indentation level.
3264 * @return {number} Indentation level
3266 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
3273 OO
.ui
.OutlineOptionWidget
.prototype.setPressed = function ( state
) {
3274 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setPressed
.call( this, state
);
3281 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3283 * @param {boolean} movable Item is movable
3286 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
3287 this.movable
= !!movable
;
3288 this.updateThemeClasses();
3295 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3297 * @param {boolean} removable Item is removable
3300 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
3301 this.removable
= !!removable
;
3302 this.updateThemeClasses();
3309 OO
.ui
.OutlineOptionWidget
.prototype.setSelected = function ( state
) {
3310 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3315 * Set indentation level.
3317 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3320 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
3321 var levels
= this.constructor.static.levels
,
3322 levelClass
= this.constructor.static.levelClass
,
3325 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
3327 if ( this.level
=== i
) {
3328 this.$element
.addClass( levelClass
+ i
);
3330 this.$element
.removeClass( levelClass
+ i
);
3333 this.updateThemeClasses();
3339 * OutlineSelectWidget is a structured list that contains {@link OO.ui.OutlineOptionWidget outline options}
3340 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls} widget.
3342 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3345 * @extends OO.ui.SelectWidget
3346 * @mixins OO.ui.mixin.TabIndexedElement
3349 * @param {Object} [config] Configuration options
3351 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
3352 // Parent constructor
3353 OO
.ui
.OutlineSelectWidget
.parent
.call( this, config
);
3355 // Mixin constructors
3356 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3360 focus
: this.bindKeyDownListener
.bind( this ),
3361 blur
: this.unbindKeyDownListener
.bind( this )
3365 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
3370 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
3371 OO
.mixinClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3374 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3375 * can be selected and configured with data. The class is
3376 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3377 * [OOUI documentation on MediaWiki] [1] for more information.
3379 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options#Button_selects_and_options
3382 * @extends OO.ui.OptionWidget
3383 * @mixins OO.ui.mixin.ButtonElement
3384 * @mixins OO.ui.mixin.IconElement
3385 * @mixins OO.ui.mixin.IndicatorElement
3386 * @mixins OO.ui.mixin.TitledElement
3389 * @param {Object} [config] Configuration options
3391 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
3392 // Configuration initialization
3393 config
= config
|| {};
3395 // Parent constructor
3396 OO
.ui
.ButtonOptionWidget
.parent
.call( this, config
);
3398 // Mixin constructors
3399 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3400 OO
.ui
.mixin
.IconElement
.call( this, config
);
3401 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3402 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
3405 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
3406 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3407 this.$element
.append( this.$button
);
3412 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
3413 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.ButtonElement
);
3414 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IconElement
);
3415 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IndicatorElement
);
3416 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.TitledElement
);
3418 /* Static Properties */
3421 * Allow button mouse down events to pass through so they can be handled by the parent select widget
3426 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
3432 OO
.ui
.ButtonOptionWidget
.static.highlightable
= false;
3439 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
3440 OO
.ui
.ButtonOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3442 if ( this.constructor.static.selectable
) {
3443 this.setActive( state
);
3450 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3451 * button options and is used together with
3452 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3453 * highlighting, choosing, and selecting mutually exclusive options. Please see
3454 * the [OOUI documentation on MediaWiki] [1] for more information.
3457 * // Example: A ButtonSelectWidget that contains three ButtonOptionWidgets
3458 * var option1 = new OO.ui.ButtonOptionWidget( {
3460 * label: 'Option 1',
3461 * title: 'Button option 1'
3464 * var option2 = new OO.ui.ButtonOptionWidget( {
3466 * label: 'Option 2',
3467 * title: 'Button option 2'
3470 * var option3 = new OO.ui.ButtonOptionWidget( {
3472 * label: 'Option 3',
3473 * title: 'Button option 3'
3476 * var buttonSelect=new OO.ui.ButtonSelectWidget( {
3477 * items: [ option1, option2, option3 ]
3479 * $( 'body' ).append( buttonSelect.$element );
3481 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options
3484 * @extends OO.ui.SelectWidget
3485 * @mixins OO.ui.mixin.TabIndexedElement
3488 * @param {Object} [config] Configuration options
3490 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
3491 // Parent constructor
3492 OO
.ui
.ButtonSelectWidget
.parent
.call( this, config
);
3494 // Mixin constructors
3495 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3499 focus
: this.bindKeyDownListener
.bind( this ),
3500 blur
: this.unbindKeyDownListener
.bind( this )
3504 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
3509 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
3510 OO
.mixinClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3513 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3515 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3516 * {@link OO.ui.TabPanelLayout tab panel layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3520 * @extends OO.ui.OptionWidget
3523 * @param {Object} [config] Configuration options
3525 OO
.ui
.TabOptionWidget
= function OoUiTabOptionWidget( config
) {
3526 // Configuration initialization
3527 config
= config
|| {};
3529 // Parent constructor
3530 OO
.ui
.TabOptionWidget
.parent
.call( this, config
);
3533 this.$element
.addClass( 'oo-ui-tabOptionWidget' );
3538 OO
.inheritClass( OO
.ui
.TabOptionWidget
, OO
.ui
.OptionWidget
);
3540 /* Static Properties */
3546 OO
.ui
.TabOptionWidget
.static.highlightable
= false;
3549 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3551 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3554 * @extends OO.ui.SelectWidget
3555 * @mixins OO.ui.mixin.TabIndexedElement
3558 * @param {Object} [config] Configuration options
3560 OO
.ui
.TabSelectWidget
= function OoUiTabSelectWidget( config
) {
3561 // Parent constructor
3562 OO
.ui
.TabSelectWidget
.parent
.call( this, config
);
3564 // Mixin constructors
3565 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3569 focus
: this.bindKeyDownListener
.bind( this ),
3570 blur
: this.unbindKeyDownListener
.bind( this )
3574 this.$element
.addClass( 'oo-ui-tabSelectWidget' );
3579 OO
.inheritClass( OO
.ui
.TabSelectWidget
, OO
.ui
.SelectWidget
);
3580 OO
.mixinClass( OO
.ui
.TabSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3583 * CapsuleItemWidgets are used within a {@link OO.ui.CapsuleMultiselectWidget
3584 * CapsuleMultiselectWidget} to display the selected items.
3587 * @extends OO.ui.Widget
3588 * @mixins OO.ui.mixin.ItemWidget
3589 * @mixins OO.ui.mixin.LabelElement
3590 * @mixins OO.ui.mixin.FlaggedElement
3591 * @mixins OO.ui.mixin.TabIndexedElement
3594 * @param {Object} [config] Configuration options
3596 OO
.ui
.CapsuleItemWidget
= function OoUiCapsuleItemWidget( config
) {
3597 // Configuration initialization
3598 config
= config
|| {};
3600 // Parent constructor
3601 OO
.ui
.CapsuleItemWidget
.parent
.call( this, config
);
3603 // Mixin constructors
3604 OO
.ui
.mixin
.ItemWidget
.call( this );
3605 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3606 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3607 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3610 this.closeButton
= new OO
.ui
.ButtonWidget( {
3614 title
: OO
.ui
.msg( 'ooui-item-remove' )
3615 } ).on( 'click', this.onCloseClick
.bind( this ) );
3617 this.on( 'disable', function ( disabled
) {
3618 this.closeButton
.setDisabled( disabled
);
3624 click
: this.onClick
.bind( this ),
3625 keydown
: this.onKeyDown
.bind( this )
3627 .addClass( 'oo-ui-capsuleItemWidget' )
3628 .append( this.$label
, this.closeButton
.$element
);
3633 OO
.inheritClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.Widget
);
3634 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.ItemWidget
);
3635 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.LabelElement
);
3636 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
3637 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3642 * Handle close icon clicks
3644 OO
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
3645 var element
= this.getElementGroup();
3647 if ( element
&& $.isFunction( element
.removeItems
) ) {
3648 element
.removeItems( [ this ] );
3654 * Handle click event for the entire capsule
3656 OO
.ui
.CapsuleItemWidget
.prototype.onClick = function () {
3657 var element
= this.getElementGroup();
3659 if ( !this.isDisabled() && element
&& $.isFunction( element
.editItem
) ) {
3660 element
.editItem( this );
3665 * Handle keyDown event for the entire capsule
3667 * @param {jQuery.Event} e Key down event
3669 OO
.ui
.CapsuleItemWidget
.prototype.onKeyDown = function ( e
) {
3670 var element
= this.getElementGroup();
3672 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
) {
3673 element
.removeItems( [ this ] );
3676 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
3677 element
.editItem( this );
3679 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
3680 element
.getPreviousItem( this ).focus();
3681 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
3682 element
.getNextItem( this ).focus();
3687 * CapsuleMultiselectWidgets are something like a {@link OO.ui.ComboBoxInputWidget combo box widget}
3688 * that allows for selecting multiple values.
3690 * For more information about menus and options, please see the [OOUI documentation on MediaWiki][1].
3693 * // Example: A CapsuleMultiselectWidget.
3694 * var capsule = new OO.ui.CapsuleMultiselectWidget( {
3695 * label: 'CapsuleMultiselectWidget',
3696 * selected: [ 'Option 1', 'Option 3' ],
3699 * new OO.ui.MenuOptionWidget( {
3701 * label: 'Option One'
3703 * new OO.ui.MenuOptionWidget( {
3705 * label: 'Option Two'
3707 * new OO.ui.MenuOptionWidget( {
3709 * label: 'Option Three'
3711 * new OO.ui.MenuOptionWidget( {
3713 * label: 'Option Four'
3715 * new OO.ui.MenuOptionWidget( {
3717 * label: 'Option Five'
3722 * $( 'body' ).append( capsule.$element );
3724 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options#Menu_selects_and_options
3727 * @extends OO.ui.Widget
3728 * @mixins OO.ui.mixin.GroupElement
3729 * @mixins OO.ui.mixin.PopupElement
3730 * @mixins OO.ui.mixin.TabIndexedElement
3731 * @mixins OO.ui.mixin.IndicatorElement
3732 * @mixins OO.ui.mixin.IconElement
3733 * @uses OO.ui.CapsuleItemWidget
3734 * @uses OO.ui.MenuSelectWidget
3737 * @param {Object} [config] Configuration options
3738 * @cfg {string} [placeholder] Placeholder text
3739 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if not present in the menu.
3740 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added.
3741 * @cfg {Object} [menu] (required) Configuration options to pass to the
3742 * {@link OO.ui.MenuSelectWidget menu select widget}.
3743 * @cfg {Object} [popup] Configuration options to pass to the {@link OO.ui.PopupWidget popup widget}.
3744 * If specified, this popup will be shown instead of the menu (but the menu
3745 * will still be used for item labels and allowArbitrary=false). The widgets
3746 * in the popup should use {@link #addItemsFromData} or {@link #addItems} as necessary.
3747 * @cfg {jQuery} [$overlay=this.$element] Render the menu or popup into a separate layer.
3748 * This configuration is useful in cases where the expanded menu is larger than
3749 * its containing `<div>`. The specified overlay layer is usually on top of
3750 * the containing `<div>` and has a larger area. By default, the menu uses
3751 * relative positioning.
3752 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
3754 OO
.ui
.CapsuleMultiselectWidget
= function OoUiCapsuleMultiselectWidget( config
) {
3757 // Parent constructor
3758 OO
.ui
.CapsuleMultiselectWidget
.parent
.call( this, config
);
3760 // Configuration initialization
3761 config
= $.extend( {
3762 allowArbitrary
: false,
3763 allowDuplicates
: false
3766 // Properties (must be set before mixin constructor calls)
3767 this.$handle
= $( '<div>' );
3768 this.$input
= config
.popup
? null : $( '<input>' );
3769 if ( config
.placeholder
!== undefined && config
.placeholder
!== '' ) {
3770 this.$input
.attr( 'placeholder', config
.placeholder
);
3773 // Mixin constructors
3774 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3775 if ( config
.popup
) {
3776 config
.popup
= $.extend( {}, config
.popup
, {
3780 OO
.ui
.mixin
.PopupElement
.call( this, config
);
3781 $tabFocus
= $( '<span>' );
3782 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: $tabFocus
} ) );
3786 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
3788 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3789 OO
.ui
.mixin
.IconElement
.call( this, config
);
3792 this.$content
= $( '<div>' );
3793 this.allowArbitrary
= config
.allowArbitrary
;
3794 this.allowDuplicates
= config
.allowDuplicates
;
3795 this.$overlay
= ( config
.$overlay
=== true ? OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
3796 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend(
3799 $input
: this.$input
,
3800 $floatableContainer
: this.$element
,
3801 filterFromInput
: true,
3802 disabled
: this.isDisabled()
3810 focus
: this.focus
.bind( this )
3812 this.popup
.$element
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3813 if ( this.popup
.$autoCloseIgnore
) {
3814 this.popup
.$autoCloseIgnore
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3816 this.popup
.connect( this, {
3817 toggle: function ( visible
) {
3818 $tabFocus
.toggle( !visible
);
3823 focus
: this.onInputFocus
.bind( this ),
3824 blur
: this.onInputBlur
.bind( this ),
3825 'propertychange change click mouseup keydown keyup input cut paste select focus':
3826 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
3827 keydown
: this.onKeyDown
.bind( this ),
3828 keypress
: this.onKeyPress
.bind( this )
3831 this.menu
.connect( this, {
3832 choose
: 'onMenuChoose',
3833 toggle
: 'onMenuToggle',
3834 add
: 'onMenuItemsChange',
3835 remove
: 'onMenuItemsChange'
3838 mousedown
: this.onMouseDown
.bind( this )
3842 if ( this.$input
) {
3843 this.$input
.prop( 'disabled', this.isDisabled() );
3846 'aria-owns': this.menu
.getElementId(),
3847 'aria-autocomplete': 'list'
3850 if ( config
.data
) {
3851 this.setItemsFromData( config
.data
);
3853 this.$content
.addClass( 'oo-ui-capsuleMultiselectWidget-content' )
3854 .append( this.$group
);
3855 this.$group
.addClass( 'oo-ui-capsuleMultiselectWidget-group' );
3856 this.$handle
.addClass( 'oo-ui-capsuleMultiselectWidget-handle' )
3857 .append( this.$indicator
, this.$icon
, this.$content
);
3858 this.$element
.addClass( 'oo-ui-capsuleMultiselectWidget' )
3859 .append( this.$handle
);
3861 this.popup
.$element
.addClass( 'oo-ui-capsuleMultiselectWidget-popup' );
3862 this.$content
.append( $tabFocus
);
3863 this.$overlay
.append( this.popup
.$element
);
3865 this.$content
.append( this.$input
);
3866 this.$overlay
.append( this.menu
.$element
);
3869 $tabFocus
.addClass( 'oo-ui-capsuleMultiselectWidget-focusTrap' );
3872 // Input size needs to be calculated after everything else is rendered
3873 setTimeout( function () {
3874 if ( this.$input
) {
3875 this.updateInputSize();
3879 this.onMenuItemsChange();
3884 OO
.inheritClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.Widget
);
3885 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.GroupElement
);
3886 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
3887 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3888 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
3889 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
3896 * A change event is emitted when the set of selected items changes.
3898 * @param {Mixed[]} datas Data of the now-selected items
3904 * A resize event is emitted when the widget's dimensions change to accomodate newly added items or
3905 * current user input.
3911 * Construct a OO.ui.CapsuleItemWidget (or a subclass thereof) from given label and data.
3912 * May return `null` if the given label and data are not valid.
3915 * @param {Mixed} data Custom data of any type.
3916 * @param {string} label The label text.
3917 * @return {OO.ui.CapsuleItemWidget|null}
3919 OO
.ui
.CapsuleMultiselectWidget
.prototype.createItemWidget = function ( data
, label
) {
3920 if ( label
=== '' ) {
3923 return new OO
.ui
.CapsuleItemWidget( { data
: data
, label
: label
} );
3929 OO
.ui
.CapsuleMultiselectWidget
.prototype.getInputId = function () {
3930 if ( !this.$input
) {
3933 return OO
.ui
.mixin
.TabIndexedElement
.prototype.getInputId
.call( this );
3937 * Get the data of the items in the capsule
3941 OO
.ui
.CapsuleMultiselectWidget
.prototype.getItemsData = function () {
3942 return this.getItems().map( function ( item
) {
3948 * Set the items in the capsule by providing data
3951 * @param {Mixed[]} datas
3952 * @return {OO.ui.CapsuleMultiselectWidget}
3954 OO
.ui
.CapsuleMultiselectWidget
.prototype.setItemsFromData = function ( datas
) {
3957 items
= this.getItems();
3959 $.each( datas
, function ( i
, data
) {
3961 item
= menu
.findItemFromData( data
);
3965 } else if ( widget
.allowArbitrary
) {
3966 label
= String( data
);
3972 for ( j
= 0; j
< items
.length
; j
++ ) {
3973 if ( items
[ j
].data
=== data
&& items
[ j
].label
=== label
) {
3975 items
.splice( j
, 1 );
3980 item
= widget
.createItemWidget( data
, label
);
3983 widget
.addItems( [ item
], i
);
3987 if ( items
.length
) {
3988 widget
.removeItems( items
);
3995 * Add items to the capsule by providing their data
3998 * @param {Mixed[]} datas
3999 * @return {OO.ui.CapsuleMultiselectWidget}
4001 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemsFromData = function ( datas
) {
4006 $.each( datas
, function ( i
, data
) {
4009 if ( !widget
.findItemFromData( data
) || widget
.allowDuplicates
) {
4010 item
= menu
.findItemFromData( data
);
4012 item
= widget
.createItemWidget( data
, item
.label
);
4013 } else if ( widget
.allowArbitrary
) {
4014 item
= widget
.createItemWidget( data
, String( data
) );
4022 if ( items
.length
) {
4023 this.addItems( items
);
4030 * Add items to the capsule by providing a label
4032 * @param {string} label
4033 * @return {boolean} Whether the item was added or not
4035 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemFromLabel = function ( label
) {
4037 item
= this.menu
.getItemFromLabel( label
, true );
4039 this.addItemsFromData( [ item
.data
] );
4041 } else if ( this.allowArbitrary
) {
4042 items
= this.getItems();
4043 this.addItemsFromData( [ label
] );
4044 return !OO
.compare( this.getItems(), items
);
4050 * Remove items by data
4053 * @param {Mixed[]} datas
4054 * @return {OO.ui.CapsuleMultiselectWidget}
4056 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItemsFromData = function ( datas
) {
4060 $.each( datas
, function ( i
, data
) {
4061 var item
= widget
.findItemFromData( data
);
4067 if ( items
.length
) {
4068 this.removeItems( items
);
4077 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItems = function ( items
) {
4079 oldItems
= this.items
.slice();
4081 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
);
4083 if ( this.items
.length
!== oldItems
.length
) {
4087 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
4088 same
= same
&& this.items
[ i
] === oldItems
[ i
];
4092 this.emit( 'change', this.getItemsData() );
4093 this.updateInputSize();
4100 * Removes the item from the list and copies its label to `this.$input`.
4102 * @param {Object} item
4104 OO
.ui
.CapsuleMultiselectWidget
.prototype.editItem = function ( item
) {
4105 this.addItemFromLabel( this.$input
.val() );
4107 this.$input
.val( item
.label
);
4108 this.updateInputSize();
4110 this.menu
.updateItemVisibility(); // Hack, we shouldn't be calling this method directly
4111 this.removeItems( [ item
] );
4117 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItems = function ( items
) {
4119 oldItems
= this.items
.slice();
4121 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
4123 if ( this.items
.length
!== oldItems
.length
) {
4127 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
4128 same
= same
&& this.items
[ i
] === oldItems
[ i
];
4132 this.emit( 'change', this.getItemsData() );
4133 this.updateInputSize();
4142 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearItems = function () {
4143 if ( this.items
.length
) {
4144 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
4145 this.emit( 'change', this.getItemsData() );
4146 this.updateInputSize();
4152 * Given an item, returns the item after it. If its the last item,
4153 * returns `this.$input`. If no item is passed, returns the very first
4156 * @param {OO.ui.CapsuleItemWidget} [item]
4157 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4159 OO
.ui
.CapsuleMultiselectWidget
.prototype.getNextItem = function ( item
) {
4162 if ( item
=== undefined ) {
4163 return this.items
[ 0 ];
4166 itemIndex
= this.items
.indexOf( item
);
4167 if ( itemIndex
< 0 ) { // Item not in list
4169 } else if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
4172 return this.items
[ itemIndex
+ 1 ];
4177 * Given an item, returns the item before it. If its the first item,
4178 * returns `this.$input`. If no item is passed, returns the very last
4181 * @param {OO.ui.CapsuleItemWidget} [item]
4182 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4184 OO
.ui
.CapsuleMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
4187 if ( item
=== undefined ) {
4188 return this.items
[ this.items
.length
- 1 ];
4191 itemIndex
= this.items
.indexOf( item
);
4192 if ( itemIndex
< 0 ) { // Item not in list
4194 } else if ( itemIndex
=== 0 ) { // First item
4197 return this.items
[ itemIndex
- 1 ];
4202 * Get the capsule widget's menu.
4204 * @return {OO.ui.MenuSelectWidget} Menu widget
4206 OO
.ui
.CapsuleMultiselectWidget
.prototype.getMenu = function () {
4211 * Handle focus events
4214 * @param {jQuery.Event} event
4216 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputFocus = function () {
4217 if ( !this.isDisabled() ) {
4218 this.updateInputSize();
4219 this.menu
.toggle( true );
4224 * Handle blur events
4227 * @param {jQuery.Event} event
4229 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputBlur = function () {
4230 this.addItemFromLabel( this.$input
.val() );
4235 * Handles popup focus out events.
4238 * @param {jQuery.Event} e Focus out event
4240 OO
.ui
.CapsuleMultiselectWidget
.prototype.onPopupFocusOut = function () {
4241 var widget
= this.popup
;
4243 setTimeout( function () {
4245 widget
.isVisible() &&
4246 !OO
.ui
.contains( widget
.$element
.add( widget
.$autoCloseIgnore
).get(), document
.activeElement
, true )
4248 widget
.toggle( false );
4254 * Handle mouse down events.
4257 * @param {jQuery.Event} e Mouse down event
4259 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMouseDown = function ( e
) {
4260 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
4264 this.updateInputSize();
4269 * Handle key press events.
4272 * @param {jQuery.Event} e Key press event
4274 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyPress = function ( e
) {
4275 if ( !this.isDisabled() ) {
4276 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
4281 if ( !this.popup
) {
4282 this.menu
.toggle( true );
4283 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
4284 if ( this.addItemFromLabel( this.$input
.val() ) ) {
4290 // Make sure the input gets resized.
4291 setTimeout( this.updateInputSize
.bind( this ), 0 );
4297 * Handle key down events.
4300 * @param {jQuery.Event} e Key down event
4302 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyDown = function ( e
) {
4304 !this.isDisabled() &&
4305 this.$input
.val() === '' &&
4308 // 'keypress' event is not triggered for Backspace
4309 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
4310 if ( e
.metaKey
|| e
.ctrlKey
) {
4311 this.removeItems( this.items
.slice( -1 ) );
4313 this.editItem( this.items
[ this.items
.length
- 1 ] );
4316 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
4317 this.getPreviousItem().focus();
4318 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
4319 this.getNextItem().focus();
4325 * Update the dimensions of the text input field to encompass all available area.
4328 * @param {jQuery.Event} e Event of some sort
4330 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateInputSize = function () {
4331 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
4332 if ( this.$input
&& !this.isDisabled() ) {
4333 this.$input
.css( 'width', '1em' );
4334 $lastItem
= this.$group
.children().last();
4335 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
4337 // Get the width of the input with the placeholder text as
4338 // the value and save it so that we don't keep recalculating
4340 this.contentWidthWithPlaceholder
=== undefined &&
4341 this.$input
.val() === '' &&
4342 this.$input
.attr( 'placeholder' ) !== undefined
4344 this.$input
.val( this.$input
.attr( 'placeholder' ) );
4345 this.contentWidthWithPlaceholder
= this.$input
[ 0 ].scrollWidth
;
4346 this.$input
.val( '' );
4350 // Always keep the input wide enough for the placeholder text
4351 contentWidth
= Math
.max(
4352 this.$input
[ 0 ].scrollWidth
,
4353 // undefined arguments in Math.max lead to NaN
4354 ( this.contentWidthWithPlaceholder
=== undefined ) ?
4355 0 : this.contentWidthWithPlaceholder
4357 currentWidth
= this.$input
.width();
4359 if ( contentWidth
< currentWidth
) {
4360 this.updateIfHeightChanged();
4361 // All is fine, don't perform expensive calculations
4365 if ( $lastItem
.length
=== 0 ) {
4366 bestWidth
= this.$content
.innerWidth();
4368 bestWidth
= direction
=== 'ltr' ?
4369 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
4370 $lastItem
.position().left
;
4373 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
4374 // pixels this is off by are coming from.
4376 if ( contentWidth
> bestWidth
) {
4377 // This will result in the input getting shifted to the next line
4378 bestWidth
= this.$content
.innerWidth() - 10;
4380 this.$input
.width( Math
.floor( bestWidth
) );
4381 this.updateIfHeightChanged();
4383 this.updateIfHeightChanged();
4388 * Determine if widget height changed, and if so, update menu position and emit 'resize' event.
4392 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateIfHeightChanged = function () {
4393 var height
= this.$element
.height();
4394 if ( height
!== this.height
) {
4395 this.height
= height
;
4396 this.menu
.position();
4398 this.popup
.updateDimensions();
4400 this.emit( 'resize' );
4405 * Handle menu choose events.
4408 * @param {OO.ui.OptionWidget} item Chosen item
4410 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
4411 if ( item
&& item
.isVisible() ) {
4412 this.addItemsFromData( [ item
.getData() ] );
4418 * Handle menu toggle events.
4421 * @param {boolean} isVisible Open state of the menu
4423 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
4424 this.$element
.toggleClass( 'oo-ui-capsuleMultiselectWidget-open', isVisible
);
4428 * Handle menu item change events.
4432 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuItemsChange = function () {
4433 this.setItemsFromData( this.getItemsData() );
4434 this.$element
.toggleClass( 'oo-ui-capsuleMultiselectWidget-empty', this.menu
.isEmpty() );
4438 * Clear the input field
4442 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearInput = function () {
4443 if ( this.$input
) {
4444 this.$input
.val( '' );
4445 this.updateInputSize();
4448 this.popup
.toggle( false );
4450 this.menu
.toggle( false );
4451 this.menu
.selectItem();
4452 this.menu
.highlightItem();
4458 OO
.ui
.CapsuleMultiselectWidget
.prototype.setDisabled = function ( disabled
) {
4462 OO
.ui
.CapsuleMultiselectWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
4464 if ( this.$input
) {
4465 this.$input
.prop( 'disabled', this.isDisabled() );
4468 this.menu
.setDisabled( this.isDisabled() );
4471 this.popup
.setDisabled( this.isDisabled() );
4475 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4476 this.items
[ i
].updateDisabled();
4488 OO
.ui
.CapsuleMultiselectWidget
.prototype.focus = function () {
4489 if ( !this.isDisabled() ) {
4491 this.popup
.setSize( this.$handle
.outerWidth() );
4492 this.popup
.toggle( true );
4493 OO
.ui
.findFocusable( this.popup
.$element
).focus();
4495 OO
.ui
.mixin
.TabIndexedElement
.prototype.focus
.call( this );
4502 * TagItemWidgets are used within a {@link OO.ui.TagMultiselectWidget
4503 * TagMultiselectWidget} to display the selected items.
4506 * @extends OO.ui.Widget
4507 * @mixins OO.ui.mixin.ItemWidget
4508 * @mixins OO.ui.mixin.LabelElement
4509 * @mixins OO.ui.mixin.FlaggedElement
4510 * @mixins OO.ui.mixin.TabIndexedElement
4511 * @mixins OO.ui.mixin.DraggableElement
4514 * @param {Object} [config] Configuration object
4515 * @cfg {boolean} [valid=true] Item is valid
4517 OO
.ui
.TagItemWidget
= function OoUiTagItemWidget( config
) {
4518 config
= config
|| {};
4520 // Parent constructor
4521 OO
.ui
.TagItemWidget
.parent
.call( this, config
);
4523 // Mixin constructors
4524 OO
.ui
.mixin
.ItemWidget
.call( this );
4525 OO
.ui
.mixin
.LabelElement
.call( this, config
);
4526 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4527 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4528 OO
.ui
.mixin
.DraggableElement
.call( this, config
);
4530 this.valid
= config
.valid
=== undefined ? true : !!config
.valid
;
4532 this.closeButton
= new OO
.ui
.ButtonWidget( {
4536 title
: OO
.ui
.msg( 'ooui-item-remove' )
4538 this.closeButton
.setDisabled( this.isDisabled() );
4542 .connect( this, { click
: 'remove' } );
4544 .on( 'click', this.select
.bind( this ) )
4545 .on( 'keydown', this.onKeyDown
.bind( this ) )
4546 // Prevent propagation of mousedown; the tag item "lives" in the
4547 // clickable area of the TagMultiselectWidget, which listens to
4548 // mousedown to open the menu or popup. We want to prevent that
4549 // for clicks specifically on the tag itself, so the actions taken
4550 // are more deliberate. When the tag is clicked, it will emit the
4551 // selection event (similar to how #OO.ui.MultioptionWidget emits 'change')
4552 // and can be handled separately.
4553 .on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
4557 .addClass( 'oo-ui-tagItemWidget' )
4558 .append( this.$label
, this.closeButton
.$element
);
4561 /* Initialization */
4563 OO
.inheritClass( OO
.ui
.TagItemWidget
, OO
.ui
.Widget
);
4564 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.ItemWidget
);
4565 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.LabelElement
);
4566 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
4567 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4568 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.DraggableElement
);
4575 * A remove action was performed on the item
4580 * @param {string} direction Direction of the movement, forward or backwards
4582 * A navigate action was performed on the item
4588 * The tag widget was selected. This can occur when the widget
4589 * is either clicked or enter was pressed on it.
4594 * @param {boolean} isValid Item is valid
4596 * Item validity has changed
4601 * @param {boolean} isDisabled Item is disabled
4603 * Item disabled state has changed
4612 OO
.ui
.TagItemWidget
.prototype.setDisabled = function ( state
) {
4614 OO
.ui
.TagItemWidget
.parent
.prototype.setDisabled
.call( this, state
);
4616 if ( this.closeButton
) {
4617 this.closeButton
.setDisabled( state
);
4620 this.emit( 'disabled', this.isDisabled() );
4625 * Handle removal of the item
4627 * This is mainly for extensibility concerns, so other children
4628 * of this class can change the behavior if they need to. This
4629 * is called by both clicking the 'remove' button but also
4630 * on keypress, which is harder to override if needed.
4634 OO
.ui
.TagItemWidget
.prototype.remove = function () {
4635 if ( !this.isDisabled() ) {
4636 this.emit( 'remove' );
4641 * Handle a keydown event on the widget
4645 * @param {jQuery.Event} e Key down event
4646 * @return {boolean|undefined} false to stop the operation
4648 OO
.ui
.TagItemWidget
.prototype.onKeyDown = function ( e
) {
4651 if ( !this.isDisabled() && e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
) {
4654 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
4658 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4659 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4661 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
4675 e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
4676 movement
.left
: movement
.right
4686 OO
.ui
.TagItemWidget
.prototype.select = function () {
4687 if ( !this.isDisabled() ) {
4688 this.emit( 'select' );
4693 * Set the valid state of this item
4695 * @param {boolean} [valid] Item is valid
4698 OO
.ui
.TagItemWidget
.prototype.toggleValid = function ( valid
) {
4699 valid
= valid
=== undefined ? !this.valid
: !!valid
;
4701 if ( this.valid
!== valid
) {
4704 this.setFlags( { invalid
: !this.valid
} );
4706 this.emit( 'valid', this.valid
);
4711 * Check whether the item is valid
4713 * @return {boolean} Item is valid
4715 OO
.ui
.TagItemWidget
.prototype.isValid = function () {
4720 * A basic tag multiselect widget, similar in concept to {@link OO.ui.ComboBoxInputWidget combo box widget}
4721 * that allows the user to add multiple values that are displayed in a tag area.
4723 * This widget is a base widget; see {@link OO.ui.MenuTagMultiselectWidget MenuTagMultiselectWidget} and
4724 * {@link OO.ui.PopupTagMultiselectWidget PopupTagMultiselectWidget} for the implementations that use
4725 * a menu and a popup respectively.
4728 * // Example: A basic TagMultiselectWidget.
4729 * var widget = new OO.ui.TagMultiselectWidget( {
4730 * inputPosition: 'outline',
4731 * allowedValues: [ 'Option 1', 'Option 2', 'Option 3' ],
4732 * selected: [ 'Option 1' ]
4734 * $( 'body' ).append( widget.$element );
4737 * @extends OO.ui.Widget
4738 * @mixins OO.ui.mixin.GroupWidget
4739 * @mixins OO.ui.mixin.DraggableGroupElement
4740 * @mixins OO.ui.mixin.IndicatorElement
4741 * @mixins OO.ui.mixin.IconElement
4742 * @mixins OO.ui.mixin.TabIndexedElement
4743 * @mixins OO.ui.mixin.FlaggedElement
4746 * @param {Object} config Configuration object
4747 * @cfg {Object} [input] Configuration options for the input widget
4748 * @cfg {OO.ui.InputWidget} [inputWidget] An optional input widget. If given, it will
4749 * replace the input widget used in the TagMultiselectWidget. If not given,
4750 * TagMultiselectWidget creates its own.
4751 * @cfg {boolean} [inputPosition='inline'] Position of the input. Options are:
4752 * - inline: The input is invisible, but exists inside the tag list, so
4753 * the user types into the tag groups to add tags.
4754 * - outline: The input is underneath the tag area.
4755 * - none: No input supplied
4756 * @cfg {boolean} [allowEditTags=true] Allow editing of the tags by clicking them
4757 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if
4758 * not present in the menu.
4759 * @cfg {Object[]} [allowedValues] An array representing the allowed items
4761 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added
4762 * @cfg {boolean} [allowDisplayInvalidTags=false] Allow the display of
4763 * invalid tags. These tags will display with an invalid state, and
4764 * the widget as a whole will have an invalid state if any invalid tags
4766 * @cfg {boolean} [allowReordering=true] Allow reordering of the items
4767 * @cfg {Object[]|String[]} [selected] A set of selected tags. If given,
4768 * these will appear in the tag list on initialization, as long as they
4769 * pass the validity tests.
4771 OO
.ui
.TagMultiselectWidget
= function OoUiTagMultiselectWidget( config
) {
4773 rAF
= window
.requestAnimationFrame
|| setTimeout
,
4775 $tabFocus
= $( '<span>' )
4776 .addClass( 'oo-ui-tagMultiselectWidget-focusTrap' );
4778 config
= config
|| {};
4780 // Parent constructor
4781 OO
.ui
.TagMultiselectWidget
.parent
.call( this, config
);
4783 // Mixin constructors
4784 OO
.ui
.mixin
.GroupWidget
.call( this, config
);
4785 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
4786 OO
.ui
.mixin
.IconElement
.call( this, config
);
4787 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4788 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4789 OO
.ui
.mixin
.DraggableGroupElement
.call( this, config
);
4791 this.toggleDraggable(
4792 config
.allowReordering
=== undefined ?
4793 true : !!config
.allowReordering
4796 this.inputPosition
=
4797 this.constructor.static.allowedInputPositions
.indexOf( config
.inputPosition
) > -1 ?
4798 config
.inputPosition
: 'inline';
4799 this.allowEditTags
= config
.allowEditTags
=== undefined ? true : !!config
.allowEditTags
;
4800 this.allowArbitrary
= !!config
.allowArbitrary
;
4801 this.allowDuplicates
= !!config
.allowDuplicates
;
4802 this.allowedValues
= config
.allowedValues
|| [];
4803 this.allowDisplayInvalidTags
= config
.allowDisplayInvalidTags
;
4804 this.hasInput
= this.inputPosition
!== 'none';
4808 this.$content
= $( '<div>' )
4809 .addClass( 'oo-ui-tagMultiselectWidget-content' );
4810 this.$handle
= $( '<div>' )
4811 .addClass( 'oo-ui-tagMultiselectWidget-handle' )
4818 .addClass( 'oo-ui-tagMultiselectWidget-group' )
4824 remove
: 'itemRemove',
4825 navigate
: 'itemNavigate',
4826 select
: 'itemSelect',
4827 disabled
: 'itemDisabled'
4829 this.connect( this, {
4830 itemRemove
: 'onTagRemove',
4831 itemSelect
: 'onTagSelect',
4832 itemDisabled
: 'onTagDisabled',
4833 itemNavigate
: 'onTagNavigate',
4834 change
: 'onChangeTags'
4837 mousedown
: this.onMouseDown
.bind( this )
4842 .addClass( 'oo-ui-tagMultiselectWidget' )
4843 .append( this.$handle
);
4845 if ( this.hasInput
) {
4846 if ( config
.inputWidget
) {
4847 this.input
= config
.inputWidget
;
4849 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
4850 placeholder
: config
.placeholder
,
4851 classes
: [ 'oo-ui-tagMultiselectWidget-input' ]
4852 }, config
.input
) );
4854 this.input
.setDisabled( this.isDisabled() );
4857 focus
: this.onInputFocus
.bind( this ),
4858 blur
: this.onInputBlur
.bind( this ),
4859 'propertychange change click mouseup keydown keyup input cut paste select focus':
4860 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
4861 keydown
: this.onInputKeyDown
.bind( this ),
4862 keypress
: this.onInputKeyPress
.bind( this )
4865 this.input
.$input
.on( inputEvents
);
4867 if ( this.inputPosition
=== 'outline' ) {
4868 // Override max-height for the input widget
4869 // in the case the widget is outline so it can
4870 // stretch all the way if the widet is wide
4871 this.input
.$element
.css( 'max-width', 'inherit' );
4873 .addClass( 'oo-ui-tagMultiselectWidget-outlined' )
4874 .append( this.input
.$element
);
4876 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-inlined' );
4877 // HACK: When the widget is using 'inline' input, the
4878 // behavior needs to only use the $input itself
4879 // so we style and size it accordingly (otherwise
4880 // the styling and sizing can get very convoluted
4881 // when the wrapping divs and other elements)
4882 // We are taking advantage of still being able to
4883 // call the widget itself for operations like
4884 // .getValue() and setDisabled() and .focus() but
4885 // having only the $input attached to the DOM
4886 this.$content
.append( this.input
.$input
);
4889 this.$content
.append( $tabFocus
);
4892 this.setTabIndexedElement(
4898 if ( config
.selected
) {
4899 this.setValue( config
.selected
);
4902 // HACK: Input size needs to be calculated after everything
4905 if ( widget
.hasInput
) {
4906 widget
.updateInputSize();
4911 /* Initialization */
4913 OO
.inheritClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.Widget
);
4914 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.GroupWidget
);
4915 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.DraggableGroupElement
);
4916 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
4917 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
4918 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4919 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.FlaggedElement
);
4921 /* Static properties */
4924 * Allowed input positions.
4925 * - inline: The input is inside the tag list
4926 * - outline: The input is under the tag list
4927 * - none: There is no input
4931 OO
.ui
.TagMultiselectWidget
.static.allowedInputPositions
= [ 'inline', 'outline', 'none' ];
4936 * Handle mouse down events.
4939 * @param {jQuery.Event} e Mouse down event
4940 * @return {boolean} False to prevent defaults
4942 OO
.ui
.TagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
4944 !this.isDisabled() &&
4945 ( !this.hasInput
|| e
.target
!== this.input
.$input
[ 0 ] ) &&
4946 e
.which
=== OO
.ui
.MouseButtons
.LEFT
4954 * Handle key press events.
4957 * @param {jQuery.Event} e Key press event
4958 * @return {boolean} Whether to prevent defaults
4960 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyPress = function ( e
) {
4962 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
4964 if ( !this.isDisabled() ) {
4965 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
4966 stopOrContinue
= this.doInputEnter( e
, withMetaKey
);
4969 // Make sure the input gets resized.
4970 setTimeout( this.updateInputSize
.bind( this ), 0 );
4971 return stopOrContinue
;
4976 * Handle key down events.
4979 * @param {jQuery.Event} e Key down event
4982 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyDown = function ( e
) {
4983 var movement
, direction
,
4984 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
4986 if ( !this.isDisabled() ) {
4987 // 'keypress' event is not triggered for Backspace
4988 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
4989 return this.doInputBackspace( e
, withMetaKey
);
4990 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ESCAPE
) {
4991 return this.doInputEscape( e
);
4993 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4994 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4996 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
5007 direction
= e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
5008 movement
.left
: movement
.right
;
5010 return this.doInputArrow( e
, direction
, withMetaKey
);
5016 * Respond to input focus event
5018 OO
.ui
.TagMultiselectWidget
.prototype.onInputFocus = function () {
5019 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-focus' );
5023 * Respond to input blur event
5025 OO
.ui
.TagMultiselectWidget
.prototype.onInputBlur = function () {
5026 this.$element
.removeClass( 'oo-ui-tagMultiselectWidget-focus' );
5030 * Perform an action after the enter key on the input
5032 * @param {jQuery.Event} e Event data
5033 * @param {boolean} [withMetaKey] Whether this key was pressed with
5034 * a meta key like 'ctrl'
5035 * @return {boolean} Whether to prevent defaults
5037 OO
.ui
.TagMultiselectWidget
.prototype.doInputEnter = function () {
5038 this.addTagFromInput();
5043 * Perform an action responding to the enter key on the input
5045 * @param {jQuery.Event} e Event data
5046 * @param {boolean} [withMetaKey] Whether this key was pressed with
5047 * a meta key like 'ctrl'
5048 * @return {boolean} Whether to prevent defaults
5050 OO
.ui
.TagMultiselectWidget
.prototype.doInputBackspace = function ( e
, withMetaKey
) {
5054 this.inputPosition
=== 'inline' &&
5055 this.input
.getValue() === '' &&
5058 // Delete the last item
5059 items
= this.getItems();
5060 item
= items
[ items
.length
- 1 ];
5062 if ( !item
.isDisabled() ) {
5063 this.removeItems( [ item
] );
5064 // If Ctrl/Cmd was pressed, delete item entirely.
5065 // Otherwise put it into the text field for editing.
5066 if ( !withMetaKey
) {
5067 this.input
.setValue( item
.getData() );
5076 * Perform an action after the escape key on the input
5078 * @param {jQuery.Event} e Event data
5080 OO
.ui
.TagMultiselectWidget
.prototype.doInputEscape = function () {
5085 * Perform an action after the arrow key on the input, select the previous
5086 * or next item from the input.
5087 * See #getPreviousItem and #getNextItem
5089 * @param {jQuery.Event} e Event data
5090 * @param {string} direction Direction of the movement; forwards or backwards
5091 * @param {boolean} [withMetaKey] Whether this key was pressed with
5092 * a meta key like 'ctrl'
5094 OO
.ui
.TagMultiselectWidget
.prototype.doInputArrow = function ( e
, direction
) {
5096 this.inputPosition
=== 'inline' &&
5099 if ( direction
=== 'backwards' ) {
5100 // Get previous item
5101 this.getPreviousItem().focus();
5104 this.getNextItem().focus();
5110 * Respond to item select event
5112 * @param {OO.ui.TagItemWidget} item Selected item
5114 OO
.ui
.TagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5115 if ( this.hasInput
&& this.allowEditTags
) {
5116 if ( this.input
.getValue() ) {
5117 this.addTagFromInput();
5119 // 1. Get the label of the tag into the input
5120 this.input
.setValue( item
.getData() );
5121 // 2. Remove the tag
5122 this.removeItems( [ item
] );
5123 // 3. Focus the input
5129 * Respond to item disabled state change
5131 * @param {OO.ui.TagItemWidget} item Selected item
5132 * @param {boolean} isDisabled Item is disabled
5134 OO
.ui
.TagMultiselectWidget
.prototype.onTagDisabled = function ( item
, isDisabled
) {
5136 // Move item to start if it is disabled
5137 this.addItems( item
, 0 );
5141 * Respond to change event, where items were added, removed, or cleared.
5143 OO
.ui
.TagMultiselectWidget
.prototype.onChangeTags = function () {
5144 this.toggleValid( this.checkValidity() );
5145 if ( this.hasInput
) {
5146 this.updateInputSize();
5148 this.updateIfHeightChanged();
5154 OO
.ui
.TagMultiselectWidget
.prototype.setDisabled = function ( isDisabled
) {
5156 OO
.ui
.TagMultiselectWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
5158 if ( this.hasInput
&& this.input
) {
5159 this.input
.setDisabled( !!isDisabled
);
5163 this.getItems().forEach( function ( item
) {
5164 item
.setDisabled( !!isDisabled
);
5170 * Respond to tag remove event
5171 * @param {OO.ui.TagItemWidget} item Removed tag
5173 OO
.ui
.TagMultiselectWidget
.prototype.onTagRemove = function ( item
) {
5174 this.removeTagByData( item
.getData() );
5178 * Respond to navigate event on the tag
5180 * @param {OO.ui.TagItemWidget} item Removed tag
5181 * @param {string} direction Direction of movement; 'forwards' or 'backwards'
5183 OO
.ui
.TagMultiselectWidget
.prototype.onTagNavigate = function ( item
, direction
) {
5184 if ( direction
=== 'forwards' ) {
5185 this.getNextItem( item
).focus();
5187 this.getPreviousItem( item
).focus();
5192 * Add tag from input value
5194 OO
.ui
.TagMultiselectWidget
.prototype.addTagFromInput = function () {
5195 var val
= this.input
.getValue(),
5196 isValid
= this.isAllowedData( val
);
5202 if ( isValid
|| this.allowDisplayInvalidTags
) {
5212 OO
.ui
.TagMultiselectWidget
.prototype.clearInput = function () {
5213 this.input
.setValue( '' );
5217 * Check whether the given value is a duplicate of an existing
5218 * tag already in the list.
5220 * @param {string|Object} data Requested value
5221 * @return {boolean} Value is duplicate
5223 OO
.ui
.TagMultiselectWidget
.prototype.isDuplicateData = function ( data
) {
5224 return !!this.findItemFromData( data
);
5228 * Check whether a given value is allowed to be added
5230 * @param {string|Object} data Requested value
5231 * @return {boolean} Value is allowed
5233 OO
.ui
.TagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
5235 !this.allowDuplicates
&&
5236 this.isDuplicateData( data
)
5241 if ( this.allowArbitrary
) {
5245 // Check with allowed values
5247 this.getAllowedValues().some( function ( value
) {
5248 return data
=== value
;
5258 * Get the allowed values list
5260 * @return {string[]} Allowed data values
5262 OO
.ui
.TagMultiselectWidget
.prototype.getAllowedValues = function () {
5263 return this.allowedValues
;
5267 * Add a value to the allowed values list
5269 * @param {string} value Allowed data value
5271 OO
.ui
.TagMultiselectWidget
.prototype.addAllowedValue = function ( value
) {
5272 if ( this.allowedValues
.indexOf( value
) === -1 ) {
5273 this.allowedValues
.push( value
);
5278 * Get the datas of the currently selected items
5280 * @return {string[]|Object[]} Datas of currently selected items
5282 OO
.ui
.TagMultiselectWidget
.prototype.getValue = function () {
5283 return this.getItems()
5284 .filter( function ( item
) {
5285 return item
.isValid();
5287 .map( function ( item
) {
5288 return item
.getData();
5293 * Set the value of this widget by datas.
5295 * @param {string|string[]|Object|Object[]} valueObject An object representing the data
5296 * and label of the value. If the widget allows arbitrary values,
5297 * the items will be added as-is. Otherwise, the data value will
5298 * be checked against allowedValues.
5299 * This object must contain at least a data key. Example:
5300 * { data: 'foo', label: 'Foo item' }
5301 * For multiple items, use an array of objects. For example:
5303 * { data: 'foo', label: 'Foo item' },
5304 * { data: 'bar', label: 'Bar item' }
5306 * Value can also be added with plaintext array, for example:
5307 * [ 'foo', 'bar', 'bla' ] or a single string, like 'foo'
5309 OO
.ui
.TagMultiselectWidget
.prototype.setValue = function ( valueObject
) {
5310 valueObject
= Array
.isArray( valueObject
) ? valueObject
: [ valueObject
];
5313 valueObject
.forEach( function ( obj
) {
5314 if ( typeof obj
=== 'string' ) {
5317 this.addTag( obj
.data
, obj
.label
);
5323 * Add tag to the display area
5325 * @param {string|Object} data Tag data
5326 * @param {string} [label] Tag label. If no label is provided, the
5327 * stringified version of the data will be used instead.
5328 * @return {boolean} Item was added successfully
5330 OO
.ui
.TagMultiselectWidget
.prototype.addTag = function ( data
, label
) {
5332 isValid
= this.isAllowedData( data
);
5334 if ( isValid
|| this.allowDisplayInvalidTags
) {
5335 newItemWidget
= this.createTagItemWidget( data
, label
);
5336 newItemWidget
.toggleValid( isValid
);
5337 this.addItems( [ newItemWidget
] );
5344 * Remove tag by its data property.
5346 * @param {string|Object} data Tag data
5348 OO
.ui
.TagMultiselectWidget
.prototype.removeTagByData = function ( data
) {
5349 var item
= this.findItemFromData( data
);
5351 this.removeItems( [ item
] );
5355 * Construct a OO.ui.TagItemWidget (or a subclass thereof) from given label and data.
5358 * @param {string} data Item data
5359 * @param {string} label The label text.
5360 * @return {OO.ui.TagItemWidget}
5362 OO
.ui
.TagMultiselectWidget
.prototype.createTagItemWidget = function ( data
, label
) {
5363 label
= label
|| data
;
5365 return new OO
.ui
.TagItemWidget( { data
: data
, label
: label
} );
5369 * Given an item, returns the item after it. If the item is already the
5370 * last item, return `this.input`. If no item is passed, returns the
5374 * @param {OO.ui.TagItemWidget} [item] Tag item
5375 * @return {OO.ui.Widget} The next widget available.
5377 OO
.ui
.TagMultiselectWidget
.prototype.getNextItem = function ( item
) {
5378 var itemIndex
= this.items
.indexOf( item
);
5380 if ( item
=== undefined || itemIndex
=== -1 ) {
5381 return this.items
[ 0 ];
5384 if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
5385 if ( this.hasInput
) {
5388 // Return first item
5389 return this.items
[ 0 ];
5392 return this.items
[ itemIndex
+ 1 ];
5397 * Given an item, returns the item before it. If the item is already the
5398 * first item, return `this.input`. If no item is passed, returns the
5402 * @param {OO.ui.TagItemWidget} [item] Tag item
5403 * @return {OO.ui.Widget} The previous widget available.
5405 OO
.ui
.TagMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
5406 var itemIndex
= this.items
.indexOf( item
);
5408 if ( item
=== undefined || itemIndex
=== -1 ) {
5409 return this.items
[ this.items
.length
- 1 ];
5412 if ( itemIndex
=== 0 ) {
5413 if ( this.hasInput
) {
5416 // Return the last item
5417 return this.items
[ this.items
.length
- 1 ];
5420 return this.items
[ itemIndex
- 1 ];
5425 * Update the dimensions of the text input field to encompass all available area.
5426 * This is especially relevant for when the input is at the edge of a line
5427 * and should get smaller. The usual operation (as an inline-block with min-width)
5428 * does not work in that case, pushing the input downwards to the next line.
5432 OO
.ui
.TagMultiselectWidget
.prototype.updateInputSize = function () {
5433 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
5434 if ( this.inputPosition
=== 'inline' && !this.isDisabled() ) {
5435 if ( this.input
.$input
[ 0 ].scrollWidth
=== 0 ) {
5436 // Input appears to be attached but not visible.
5437 // Don't attempt to adjust its size, because our measurements
5438 // are going to fail anyway.
5441 this.input
.$input
.css( 'width', '1em' );
5442 $lastItem
= this.$group
.children().last();
5443 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
5445 // Get the width of the input with the placeholder text as
5446 // the value and save it so that we don't keep recalculating
5448 this.contentWidthWithPlaceholder
=== undefined &&
5449 this.input
.getValue() === '' &&
5450 this.input
.$input
.attr( 'placeholder' ) !== undefined
5452 this.input
.setValue( this.input
.$input
.attr( 'placeholder' ) );
5453 this.contentWidthWithPlaceholder
= this.input
.$input
[ 0 ].scrollWidth
;
5454 this.input
.setValue( '' );
5458 // Always keep the input wide enough for the placeholder text
5459 contentWidth
= Math
.max(
5460 this.input
.$input
[ 0 ].scrollWidth
,
5461 // undefined arguments in Math.max lead to NaN
5462 ( this.contentWidthWithPlaceholder
=== undefined ) ?
5463 0 : this.contentWidthWithPlaceholder
5465 currentWidth
= this.input
.$input
.width();
5467 if ( contentWidth
< currentWidth
) {
5468 this.updateIfHeightChanged();
5469 // All is fine, don't perform expensive calculations
5473 if ( $lastItem
.length
=== 0 ) {
5474 bestWidth
= this.$content
.innerWidth();
5476 bestWidth
= direction
=== 'ltr' ?
5477 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
5478 $lastItem
.position().left
;
5481 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
5482 // pixels this is off by are coming from.
5484 if ( contentWidth
> bestWidth
) {
5485 // This will result in the input getting shifted to the next line
5486 bestWidth
= this.$content
.innerWidth() - 10;
5488 this.input
.$input
.width( Math
.floor( bestWidth
) );
5489 this.updateIfHeightChanged();
5491 this.updateIfHeightChanged();
5496 * Determine if widget height changed, and if so,
5497 * emit the resize event. This is useful for when there are either
5498 * menus or popups attached to the bottom of the widget, to allow
5499 * them to change their positioning in case the widget moved down
5504 OO
.ui
.TagMultiselectWidget
.prototype.updateIfHeightChanged = function () {
5505 var height
= this.$element
.height();
5506 if ( height
!== this.height
) {
5507 this.height
= height
;
5508 this.emit( 'resize' );
5513 * Check whether all items in the widget are valid
5515 * @return {boolean} Widget is valid
5517 OO
.ui
.TagMultiselectWidget
.prototype.checkValidity = function () {
5518 return this.getItems().every( function ( item
) {
5519 return item
.isValid();
5524 * Set the valid state of this item
5526 * @param {boolean} [valid] Item is valid
5529 OO
.ui
.TagMultiselectWidget
.prototype.toggleValid = function ( valid
) {
5530 valid
= valid
=== undefined ? !this.valid
: !!valid
;
5532 if ( this.valid
!== valid
) {
5535 this.setFlags( { invalid
: !this.valid
} );
5537 this.emit( 'valid', this.valid
);
5542 * Get the current valid state of the widget
5544 * @return {boolean} Widget is valid
5546 OO
.ui
.TagMultiselectWidget
.prototype.isValid = function () {
5551 * PopupTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5552 * to use a popup. The popup can be configured to have a default input to insert values into the widget.
5555 * // Example: A basic PopupTagMultiselectWidget.
5556 * var widget = new OO.ui.PopupTagMultiselectWidget();
5557 * $( 'body' ).append( widget.$element );
5559 * // Example: A PopupTagMultiselectWidget with an external popup.
5560 * var popupInput = new OO.ui.TextInputWidget(),
5561 * widget = new OO.ui.PopupTagMultiselectWidget( {
5562 * popupInput: popupInput,
5564 * $content: popupInput.$element
5567 * $( 'body' ).append( widget.$element );
5570 * @extends OO.ui.TagMultiselectWidget
5571 * @mixins OO.ui.mixin.PopupElement
5573 * @param {Object} config Configuration object
5574 * @cfg {jQuery} [$overlay] An overlay for the popup.
5575 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
5576 * @cfg {Object} [popup] Configuration options for the popup
5577 * @cfg {OO.ui.InputWidget} [popupInput] An input widget inside the popup that will be
5578 * focused when the popup is opened and will be used as replacement for the
5579 * general input in the widget.
5581 OO
.ui
.PopupTagMultiselectWidget
= function OoUiPopupTagMultiselectWidget( config
) {
5583 defaultConfig
= { popup
: {} };
5585 config
= config
|| {};
5587 // Parent constructor
5588 OO
.ui
.PopupTagMultiselectWidget
.parent
.call( this, $.extend( { inputPosition
: 'none' }, config
) );
5590 this.$overlay
= ( config
.$overlay
=== true ? OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
5592 if ( !config
.popup
) {
5593 // For the default base implementation, we give a popup
5594 // with an input widget inside it. For any other use cases
5595 // the popup needs to be populated externally and the
5596 // event handled to add tags separately and manually
5597 defaultInput
= new OO
.ui
.TextInputWidget();
5599 defaultConfig
.popupInput
= defaultInput
;
5600 defaultConfig
.popup
.$content
= defaultInput
.$element
;
5602 this.$element
.addClass( 'oo-ui-popupTagMultiselectWidget-defaultPopup' );
5605 // Add overlay, and add that to the autoCloseIgnore
5606 defaultConfig
.popup
.$overlay
= this.$overlay
;
5607 defaultConfig
.popup
.$autoCloseIgnore
= this.hasInput
?
5608 this.input
.$element
.add( this.$overlay
) : this.$overlay
;
5610 // Allow extending any of the above
5611 config
= $.extend( defaultConfig
, config
);
5613 // Mixin constructors
5614 OO
.ui
.mixin
.PopupElement
.call( this, config
);
5616 if ( this.hasInput
) {
5617 this.input
.$input
.on( 'focus', this.popup
.toggle
.bind( this.popup
, true ) );
5620 // Configuration options
5621 this.popupInput
= config
.popupInput
;
5622 if ( this.popupInput
) {
5623 this.popupInput
.connect( this, {
5624 enter
: 'onPopupInputEnter'
5629 this.on( 'resize', this.popup
.updateDimensions
.bind( this.popup
) );
5630 this.popup
.connect( this, { toggle
: 'onPopupToggle' } );
5632 .on( 'focus', this.onFocus
.bind( this ) );
5636 .append( this.popup
.$element
)
5637 .addClass( 'oo-ui-popupTagMultiselectWidget' );
5640 /* Initialization */
5642 OO
.inheritClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5643 OO
.mixinClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
5648 * Focus event handler.
5652 OO
.ui
.PopupTagMultiselectWidget
.prototype.onFocus = function () {
5653 this.popup
.toggle( true );
5657 * Respond to popup toggle event
5659 * @param {boolean} isVisible Popup is visible
5661 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupToggle = function ( isVisible
) {
5662 if ( isVisible
&& this.popupInput
) {
5663 this.popupInput
.focus();
5668 * Respond to popup input enter event
5670 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupInputEnter = function () {
5671 if ( this.popupInput
) {
5672 this.addTagByPopupValue( this.popupInput
.getValue() );
5673 this.popupInput
.setValue( '' );
5680 OO
.ui
.PopupTagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5681 if ( this.popupInput
&& this.allowEditTags
) {
5682 this.popupInput
.setValue( item
.getData() );
5683 this.removeItems( [ item
] );
5685 this.popup
.toggle( true );
5686 this.popupInput
.focus();
5689 OO
.ui
.PopupTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, item
);
5694 * Add a tag by the popup value.
5695 * Whatever is responsible for setting the value in the popup should call
5696 * this method to add a tag, or use the regular methods like #addTag or
5697 * #setValue directly.
5699 * @param {string} data The value of item
5700 * @param {string} [label] The label of the tag. If not given, the data is used.
5702 OO
.ui
.PopupTagMultiselectWidget
.prototype.addTagByPopupValue = function ( data
, label
) {
5703 this.addTag( data
, label
);
5707 * MenuTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5708 * to use a menu of selectable options.
5711 * // Example: A basic MenuTagMultiselectWidget.
5712 * var widget = new OO.ui.MenuTagMultiselectWidget( {
5713 * inputPosition: 'outline',
5715 * { data: 'option1', label: 'Option 1' },
5716 * { data: 'option2', label: 'Option 2' },
5717 * { data: 'option3', label: 'Option 3' },
5719 * selected: [ 'option1', 'option2' ]
5721 * $( 'body' ).append( widget.$element );
5724 * @extends OO.ui.TagMultiselectWidget
5727 * @param {Object} [config] Configuration object
5728 * @cfg {boolean} [clearInputOnChoose=true] Clear the text input value when a menu option is chosen
5729 * @cfg {Object} [menu] Configuration object for the menu widget
5730 * @cfg {jQuery} [$overlay] An overlay for the menu.
5731 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
5732 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
5734 OO
.ui
.MenuTagMultiselectWidget
= function OoUiMenuTagMultiselectWidget( config
) {
5735 config
= config
|| {};
5737 // Parent constructor
5738 OO
.ui
.MenuTagMultiselectWidget
.parent
.call( this, config
);
5740 this.$overlay
= ( config
.$overlay
=== true ? OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
5741 this.clearInputOnChoose
= config
.clearInputOnChoose
=== undefined || !!config
.clearInputOnChoose
;
5742 this.menu
= this.createMenuWidget( $.extend( {
5744 input
: this.hasInput
? this.input
: null,
5745 $input
: this.hasInput
? this.input
.$input
: null,
5746 filterFromInput
: !!this.hasInput
,
5747 $autoCloseIgnore
: this.hasInput
?
5748 this.input
.$element
: $( [] ),
5749 $floatableContainer
: this.hasInput
&& this.inputPosition
=== 'outline' ?
5750 this.input
.$element
: this.$element
,
5751 $overlay
: this.$overlay
,
5752 disabled
: this.isDisabled()
5754 this.addOptions( config
.options
|| [] );
5757 this.menu
.connect( this, {
5758 choose
: 'onMenuChoose',
5759 toggle
: 'onMenuToggle'
5761 if ( this.hasInput
) {
5762 this.input
.connect( this, { change
: 'onInputChange' } );
5764 this.connect( this, { resize
: 'onResize' } );
5768 .append( this.menu
.$element
);
5770 .addClass( 'oo-ui-menuTagMultiselectWidget' );
5771 // TagMultiselectWidget already does this, but it doesn't work right because this.menu is not yet
5772 // set up while the parent constructor runs, and #getAllowedValues rejects everything.
5773 if ( config
.selected
) {
5774 this.setValue( config
.selected
);
5778 /* Initialization */
5780 OO
.inheritClass( OO
.ui
.MenuTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5785 * Respond to resize event
5787 OO
.ui
.MenuTagMultiselectWidget
.prototype.onResize = function () {
5788 // Reposition the menu
5789 this.menu
.position();
5795 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputFocus = function () {
5797 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
5799 this.menu
.toggle( true );
5803 * Respond to input change event
5805 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputChange = function () {
5806 this.menu
.toggle( true );
5807 this.initializeMenuSelection();
5811 * Respond to menu choose event
5813 * @param {OO.ui.OptionWidget} menuItem Chosen menu item
5815 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuChoose = function ( menuItem
) {
5817 this.addTag( menuItem
.getData(), menuItem
.getLabel() );
5818 if ( this.hasInput
&& this.clearInputOnChoose
) {
5819 this.input
.setValue( '' );
5824 * Respond to menu toggle event. Reset item highlights on hide.
5826 * @param {boolean} isVisible The menu is visible
5828 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
5830 this.menu
.selectItem( null );
5831 this.menu
.highlightItem( null );
5833 this.initializeMenuSelection();
5840 OO
.ui
.MenuTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
5841 var menuItem
= this.menu
.findItemFromData( tagItem
.getData() );
5842 // Override the base behavior from TagMultiselectWidget; the base behavior
5843 // in TagMultiselectWidget is to remove the tag to edit it in the input,
5844 // but in our case, we want to utilize the menu selection behavior, and
5845 // definitely not remove the item.
5847 // If there is an input that is used for filtering, erase the value so we don't filter
5848 if ( this.hasInput
&& this.menu
.filterFromInput
) {
5849 this.input
.setValue( '' );
5852 // Select the menu item
5853 this.menu
.selectItem( menuItem
);
5859 * Highlight the first selectable item in the menu, if configured.
5864 OO
.ui
.MenuTagMultiselectWidget
.prototype.initializeMenuSelection = function () {
5865 if ( !this.menu
.findSelectedItem() ) {
5866 this.menu
.highlightItem( this.menu
.findFirstSelectableItem() );
5873 OO
.ui
.MenuTagMultiselectWidget
.prototype.addTagFromInput = function () {
5874 var inputValue
= this.input
.getValue(),
5876 highlightedItem
= this.menu
.findHighlightedItem(),
5877 item
= this.menu
.findItemFromData( inputValue
);
5879 if ( !inputValue
) {
5883 // Override the parent method so we add from the menu
5884 // rather than directly from the input
5886 // Look for a highlighted item first
5887 if ( highlightedItem
) {
5888 validated
= this.addTag( highlightedItem
.getData(), highlightedItem
.getLabel() );
5889 } else if ( item
) {
5890 // Look for the element that fits the data
5891 validated
= this.addTag( item
.getData(), item
.getLabel() );
5893 // Otherwise, add the tag - the method will only add if the
5894 // tag is valid or if invalid tags are allowed
5895 validated
= this.addTag( inputValue
);
5905 * Return the visible items in the menu. This is mainly used for when
5906 * the menu is filtering results.
5908 * @return {OO.ui.MenuOptionWidget[]} Visible results
5910 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenuVisibleItems = function () {
5911 return this.menu
.getItems().filter( function ( menuItem
) {
5912 return menuItem
.isVisible();
5917 * Create the menu for this widget. This is in a separate method so that
5918 * child classes can override this without polluting the constructor with
5919 * unnecessary extra objects that will be overidden.
5921 * @param {Object} menuConfig Configuration options
5922 * @return {OO.ui.MenuSelectWidget} Menu widget
5924 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
5925 return new OO
.ui
.MenuSelectWidget( menuConfig
);
5929 * Add options to the menu
5931 * @param {Object[]} menuOptions Object defining options
5933 OO
.ui
.MenuTagMultiselectWidget
.prototype.addOptions = function ( menuOptions
) {
5935 items
= menuOptions
.map( function ( obj
) {
5936 return widget
.createMenuOptionWidget( obj
.data
, obj
.label
);
5939 this.menu
.addItems( items
);
5943 * Create a menu option widget.
5945 * @param {string} data Item data
5946 * @param {string} [label] Item label
5947 * @return {OO.ui.OptionWidget} Option widget
5949 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuOptionWidget = function ( data
, label
) {
5950 return new OO
.ui
.MenuOptionWidget( {
5952 label
: label
|| data
5959 * @return {OO.ui.MenuSelectWidget} Menu
5961 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenu = function () {
5966 * Get the allowed values list
5968 * @return {string[]} Allowed data values
5970 OO
.ui
.MenuTagMultiselectWidget
.prototype.getAllowedValues = function () {
5973 // If the parent constructor is calling us, we're not ready yet, this.menu is not set up.
5974 menuDatas
= this.menu
.getItems().map( function ( menuItem
) {
5975 return menuItem
.getData();
5978 return this.allowedValues
.concat( menuDatas
);
5982 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
5983 * widgets can be configured with {@link OO.ui.mixin.IconElement icons} and {@link
5984 * OO.ui.mixin.IndicatorElement indicators}.
5985 * Please see the [OOUI documentation on MediaWiki] [1] for more information and examples.
5988 * // Example of a file select widget
5989 * var selectFile = new OO.ui.SelectFileWidget();
5990 * $( 'body' ).append( selectFile.$element );
5992 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets
5995 * @extends OO.ui.Widget
5996 * @mixins OO.ui.mixin.IconElement
5997 * @mixins OO.ui.mixin.IndicatorElement
5998 * @mixins OO.ui.mixin.PendingElement
5999 * @mixins OO.ui.mixin.LabelElement
6002 * @param {Object} [config] Configuration options
6003 * @cfg {string[]|null} [accept=null] MIME types to accept. null accepts all types.
6004 * @cfg {string} [placeholder] Text to display when no file is selected.
6005 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
6006 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
6007 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be true.
6008 * @cfg {number} [thumbnailSizeLimit=20] File size limit in MiB above which to not try and show a
6009 * preview (for performance)
6011 OO
.ui
.SelectFileWidget
= function OoUiSelectFileWidget( config
) {
6014 // Configuration initialization
6015 config
= $.extend( {
6017 placeholder
: OO
.ui
.msg( 'ooui-selectfile-placeholder' ),
6018 notsupported
: OO
.ui
.msg( 'ooui-selectfile-not-supported' ),
6020 showDropTarget
: false,
6021 thumbnailSizeLimit
: 20
6024 // Parent constructor
6025 OO
.ui
.SelectFileWidget
.parent
.call( this, config
);
6027 // Mixin constructors
6028 OO
.ui
.mixin
.IconElement
.call( this, config
);
6029 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
6030 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {}, config
, { $pending
: this.$info
} ) );
6031 OO
.ui
.mixin
.LabelElement
.call( this, config
);
6034 this.$info
= $( '<span>' );
6035 this.showDropTarget
= config
.showDropTarget
;
6036 this.thumbnailSizeLimit
= config
.thumbnailSizeLimit
;
6037 this.isSupported
= this.constructor.static.isSupported();
6038 this.currentFile
= null;
6039 if ( Array
.isArray( config
.accept
) ) {
6040 this.accept
= config
.accept
;
6044 this.placeholder
= config
.placeholder
;
6045 this.notsupported
= config
.notsupported
;
6046 this.onFileSelectedHandler
= this.onFileSelected
.bind( this );
6048 this.selectButton
= new OO
.ui
.ButtonWidget( {
6049 classes
: [ 'oo-ui-selectFileWidget-selectButton' ],
6050 label
: OO
.ui
.msg( 'ooui-selectfile-button-select' ),
6051 disabled
: this.disabled
|| !this.isSupported
6054 this.clearButton
= new OO
.ui
.ButtonWidget( {
6055 classes
: [ 'oo-ui-selectFileWidget-clearButton' ],
6058 disabled
: this.disabled
6062 this.selectButton
.$button
.on( {
6063 keypress
: this.onKeyPress
.bind( this )
6065 this.clearButton
.connect( this, {
6066 click
: 'onClearClick'
6068 if ( config
.droppable
) {
6069 dragHandler
= this.onDragEnterOrOver
.bind( this );
6071 dragenter
: dragHandler
,
6072 dragover
: dragHandler
,
6073 dragleave
: this.onDragLeave
.bind( this ),
6074 drop
: this.onDrop
.bind( this )
6080 this.$label
.addClass( 'oo-ui-selectFileWidget-label' );
6082 .addClass( 'oo-ui-selectFileWidget-info' )
6083 .append( this.$icon
, this.$label
, this.clearButton
.$element
, this.$indicator
);
6085 if ( config
.droppable
&& config
.showDropTarget
) {
6086 this.selectButton
.setIcon( 'upload' );
6087 this.$thumbnail
= $( '<div>' ).addClass( 'oo-ui-selectFileWidget-thumbnail' );
6088 this.setPendingElement( this.$thumbnail
);
6090 .addClass( 'oo-ui-selectFileWidget-dropTarget oo-ui-selectFileWidget' )
6092 click
: this.onDropTargetClick
.bind( this )
6097 this.selectButton
.$element
,
6099 .addClass( 'oo-ui-selectFileWidget-dropLabel' )
6100 .text( OO
.ui
.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
6104 .addClass( 'oo-ui-selectFileWidget' )
6105 .append( this.$info
, this.selectButton
.$element
);
6112 OO
.inheritClass( OO
.ui
.SelectFileWidget
, OO
.ui
.Widget
);
6113 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IconElement
);
6114 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IndicatorElement
);
6115 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.PendingElement
);
6116 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.LabelElement
);
6118 /* Static Properties */
6121 * Check if this widget is supported
6126 OO
.ui
.SelectFileWidget
.static.isSupported = function () {
6128 if ( OO
.ui
.SelectFileWidget
.static.isSupportedCache
=== null ) {
6129 $input
= $( '<input>' ).attr( 'type', 'file' );
6130 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= $input
[ 0 ].files
!== undefined;
6132 return OO
.ui
.SelectFileWidget
.static.isSupportedCache
;
6135 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= null;
6142 * A change event is emitted when the on/off state of the toggle changes.
6144 * @param {File|null} value New value
6150 * Get the current value of the field
6152 * @return {File|null}
6154 OO
.ui
.SelectFileWidget
.prototype.getValue = function () {
6155 return this.currentFile
;
6159 * Set the current value of the field
6161 * @param {File|null} file File to select
6163 OO
.ui
.SelectFileWidget
.prototype.setValue = function ( file
) {
6164 if ( this.currentFile
!== file
) {
6165 this.currentFile
= file
;
6167 this.emit( 'change', this.currentFile
);
6174 * Focusses the select file button.
6178 OO
.ui
.SelectFileWidget
.prototype.focus = function () {
6179 this.selectButton
.focus();
6188 OO
.ui
.SelectFileWidget
.prototype.blur = function () {
6189 this.selectButton
.blur();
6196 OO
.ui
.SelectFileWidget
.prototype.simulateLabelClick = function () {
6201 * Update the user interface when a file is selected or unselected
6205 OO
.ui
.SelectFileWidget
.prototype.updateUI = function () {
6207 if ( !this.isSupported
) {
6208 this.$element
.addClass( 'oo-ui-selectFileWidget-notsupported' );
6209 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
6210 this.setLabel( this.notsupported
);
6212 this.$element
.addClass( 'oo-ui-selectFileWidget-supported' );
6213 if ( this.currentFile
) {
6214 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
6216 $label
= $label
.add(
6218 .addClass( 'oo-ui-selectFileWidget-fileName' )
6219 .text( this.currentFile
.name
)
6221 this.setLabel( $label
);
6223 if ( this.showDropTarget
) {
6225 this.loadAndGetImageUrl().done( function ( url
) {
6226 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
6227 }.bind( this ) ).fail( function () {
6228 this.$thumbnail
.append(
6229 new OO
.ui
.IconWidget( {
6231 classes
: [ 'oo-ui-selectFileWidget-noThumbnail-icon' ]
6234 }.bind( this ) ).always( function () {
6237 this.$element
.off( 'click' );
6240 if ( this.showDropTarget
) {
6241 this.$element
.off( 'click' );
6243 click
: this.onDropTargetClick
.bind( this )
6247 .css( 'background-image', '' );
6249 this.$element
.addClass( 'oo-ui-selectFileWidget-empty' );
6250 this.setLabel( this.placeholder
);
6256 * If the selected file is an image, get its URL and load it.
6258 * @return {jQuery.Promise} Promise resolves with the image URL after it has loaded
6260 OO
.ui
.SelectFileWidget
.prototype.loadAndGetImageUrl = function () {
6261 var deferred
= $.Deferred(),
6262 file
= this.currentFile
,
6263 reader
= new FileReader();
6267 ( OO
.getProp( file
, 'type' ) || '' ).indexOf( 'image/' ) === 0 &&
6268 file
.size
< this.thumbnailSizeLimit
* 1024 * 1024
6270 reader
.onload = function ( event
) {
6271 var img
= document
.createElement( 'img' );
6272 img
.addEventListener( 'load', function () {
6274 img
.naturalWidth
=== 0 ||
6275 img
.naturalHeight
=== 0 ||
6276 img
.complete
=== false
6280 deferred
.resolve( event
.target
.result
);
6283 img
.src
= event
.target
.result
;
6285 reader
.readAsDataURL( file
);
6290 return deferred
.promise();
6294 * Add the input to the widget
6298 OO
.ui
.SelectFileWidget
.prototype.addInput = function () {
6299 if ( this.$input
) {
6300 this.$input
.remove();
6303 if ( !this.isSupported
) {
6308 this.$input
= $( '<input>' ).attr( 'type', 'file' );
6309 this.$input
.on( 'change', this.onFileSelectedHandler
);
6310 this.$input
.on( 'click', function ( e
) {
6311 // Prevents dropTarget to get clicked which calls
6312 // a click on this input
6313 e
.stopPropagation();
6318 if ( this.accept
) {
6319 this.$input
.attr( 'accept', this.accept
.join( ', ' ) );
6321 this.selectButton
.$button
.append( this.$input
);
6325 * Determine if we should accept this file
6328 * @param {string} mimeType File MIME type
6331 OO
.ui
.SelectFileWidget
.prototype.isAllowedType = function ( mimeType
) {
6334 if ( !this.accept
|| !mimeType
) {
6338 for ( i
= 0; i
< this.accept
.length
; i
++ ) {
6339 mimeTest
= this.accept
[ i
];
6340 if ( mimeTest
=== mimeType
) {
6342 } else if ( mimeTest
.substr( -2 ) === '/*' ) {
6343 mimeTest
= mimeTest
.substr( 0, mimeTest
.length
- 1 );
6344 if ( mimeType
.substr( 0, mimeTest
.length
) === mimeTest
) {
6354 * Handle file selection from the input
6357 * @param {jQuery.Event} e
6359 OO
.ui
.SelectFileWidget
.prototype.onFileSelected = function ( e
) {
6360 var file
= OO
.getProp( e
.target
, 'files', 0 ) || null;
6362 if ( file
&& !this.isAllowedType( file
.type
) ) {
6366 this.setValue( file
);
6371 * Handle clear button click events.
6375 OO
.ui
.SelectFileWidget
.prototype.onClearClick = function () {
6376 this.setValue( null );
6381 * Handle key press events.
6384 * @param {jQuery.Event} e Key press event
6386 OO
.ui
.SelectFileWidget
.prototype.onKeyPress = function ( e
) {
6387 if ( this.isSupported
&& !this.isDisabled() && this.$input
&&
6388 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
6390 this.$input
.click();
6396 * Handle drop target click events.
6399 * @param {jQuery.Event} e Key press event
6401 OO
.ui
.SelectFileWidget
.prototype.onDropTargetClick = function () {
6402 if ( this.isSupported
&& !this.isDisabled() && this.$input
) {
6403 this.$input
.click();
6409 * Handle drag enter and over events
6412 * @param {jQuery.Event} e Drag event
6414 OO
.ui
.SelectFileWidget
.prototype.onDragEnterOrOver = function ( e
) {
6416 droppableFile
= false,
6417 dt
= e
.originalEvent
.dataTransfer
;
6420 e
.stopPropagation();
6422 if ( this.isDisabled() || !this.isSupported
) {
6423 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6424 dt
.dropEffect
= 'none';
6428 // DataTransferItem and File both have a type property, but in Chrome files
6429 // have no information at this point.
6430 itemOrFile
= OO
.getProp( dt
, 'items', 0 ) || OO
.getProp( dt
, 'files', 0 );
6432 if ( this.isAllowedType( itemOrFile
.type
) ) {
6433 droppableFile
= true;
6435 // dt.types is Array-like, but not an Array
6436 } else if ( Array
.prototype.indexOf
.call( OO
.getProp( dt
, 'types' ) || [], 'Files' ) !== -1 ) {
6437 // File information is not available at this point for security so just assume
6438 // it is acceptable for now.
6439 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
6440 droppableFile
= true;
6443 this.$element
.toggleClass( 'oo-ui-selectFileWidget-canDrop', droppableFile
);
6444 if ( !droppableFile
) {
6445 dt
.dropEffect
= 'none';
6452 * Handle drag leave events
6455 * @param {jQuery.Event} e Drag event
6457 OO
.ui
.SelectFileWidget
.prototype.onDragLeave = function () {
6458 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6462 * Handle drop events
6465 * @param {jQuery.Event} e Drop event
6467 OO
.ui
.SelectFileWidget
.prototype.onDrop = function ( e
) {
6469 dt
= e
.originalEvent
.dataTransfer
;
6472 e
.stopPropagation();
6473 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6475 if ( this.isDisabled() || !this.isSupported
) {
6479 file
= OO
.getProp( dt
, 'files', 0 );
6480 if ( file
&& !this.isAllowedType( file
.type
) ) {
6484 this.setValue( file
);
6493 OO
.ui
.SelectFileWidget
.prototype.setDisabled = function ( disabled
) {
6494 OO
.ui
.SelectFileWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
6495 if ( this.selectButton
) {
6496 this.selectButton
.setDisabled( disabled
);
6498 if ( this.clearButton
) {
6499 this.clearButton
.setDisabled( disabled
);
6505 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field}, where users can type a search query,
6506 * and a menu of search results, which is displayed beneath the query
6507 * field. Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible to the user.
6508 * Users can choose an item from the menu or type a query into the text field to search for a matching result item.
6509 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
6511 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
6512 * the [OOUI demos][1] for an example.
6514 * [1]: https://doc.wikimedia.org/oojs-ui/master/demos/#SearchInputWidget-type-search
6517 * @extends OO.ui.Widget
6520 * @param {Object} [config] Configuration options
6521 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6522 * @cfg {string} [value] Initial query value
6524 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
6525 // Configuration initialization
6526 config
= config
|| {};
6528 // Parent constructor
6529 OO
.ui
.SearchWidget
.parent
.call( this, config
);
6532 this.query
= new OO
.ui
.TextInputWidget( {
6534 placeholder
: config
.placeholder
,
6537 this.results
= new OO
.ui
.SelectWidget();
6538 this.$query
= $( '<div>' );
6539 this.$results
= $( '<div>' );
6542 this.query
.connect( this, {
6543 change
: 'onQueryChange',
6544 enter
: 'onQueryEnter'
6546 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
6550 .addClass( 'oo-ui-searchWidget-query' )
6551 .append( this.query
.$element
);
6553 .addClass( 'oo-ui-searchWidget-results' )
6554 .append( this.results
.$element
);
6556 .addClass( 'oo-ui-searchWidget' )
6557 .append( this.$results
, this.$query
);
6562 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
6567 * Handle query key down events.
6570 * @param {jQuery.Event} e Key down event
6572 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
6573 var highlightedItem
, nextItem
,
6574 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
6577 highlightedItem
= this.results
.findHighlightedItem();
6578 if ( !highlightedItem
) {
6579 highlightedItem
= this.results
.findSelectedItem();
6581 nextItem
= this.results
.findRelativeSelectableItem( highlightedItem
, dir
);
6582 this.results
.highlightItem( nextItem
);
6583 nextItem
.scrollElementIntoView();
6588 * Handle select widget select events.
6590 * Clears existing results. Subclasses should repopulate items according to new query.
6593 * @param {string} value New value
6595 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
6597 this.results
.clearItems();
6601 * Handle select widget enter key events.
6603 * Chooses highlighted item.
6606 * @param {string} value New value
6608 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
6609 var highlightedItem
= this.results
.findHighlightedItem();
6610 if ( highlightedItem
) {
6611 this.results
.chooseItem( highlightedItem
);
6616 * Get the query input.
6618 * @return {OO.ui.TextInputWidget} Query input
6620 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
6625 * Get the search results menu.
6627 * @return {OO.ui.SelectWidget} Menu of search results
6629 OO
.ui
.SearchWidget
.prototype.getResults = function () {
6630 return this.results
;
6634 * NumberInputWidgets combine a {@link OO.ui.TextInputWidget text input} (where a value
6635 * can be entered manually) and two {@link OO.ui.ButtonWidget button widgets}
6636 * (to adjust the value in increments) to allow the user to enter a number.
6639 * // Example: A NumberInputWidget.
6640 * var numberInput = new OO.ui.NumberInputWidget( {
6641 * label: 'NumberInputWidget',
6642 * input: { value: 5 },
6646 * $( 'body' ).append( numberInput.$element );
6649 * @extends OO.ui.TextInputWidget
6652 * @param {Object} [config] Configuration options
6653 * @cfg {Object} [minusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget decrementing button widget}.
6654 * @cfg {Object} [plusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget incrementing button widget}.
6655 * @cfg {boolean} [allowInteger=false] Whether the field accepts only integer values.
6656 * @cfg {number} [min=-Infinity] Minimum allowed value
6657 * @cfg {number} [max=Infinity] Maximum allowed value
6658 * @cfg {number} [step=1] Delta when using the buttons or up/down arrow keys
6659 * @cfg {number|null} [pageStep] Delta when using the page-up/page-down keys. Defaults to 10 times #step.
6660 * @cfg {boolean} [showButtons=true] Whether to show the plus and minus buttons.
6662 OO
.ui
.NumberInputWidget
= function OoUiNumberInputWidget( config
) {
6663 var $field
= $( '<div>' )
6664 .addClass( 'oo-ui-numberInputWidget-field' );
6666 // Configuration initialization
6667 config
= $.extend( {
6668 allowInteger
: false,
6676 // For backward compatibility
6677 $.extend( config
, config
.input
);
6680 // Parent constructor
6681 OO
.ui
.NumberInputWidget
.parent
.call( this, $.extend( config
, {
6685 if ( config
.showButtons
) {
6686 this.minusButton
= new OO
.ui
.ButtonWidget( $.extend(
6688 disabled
: this.isDisabled(),
6690 classes
: [ 'oo-ui-numberInputWidget-minusButton' ],
6695 this.plusButton
= new OO
.ui
.ButtonWidget( $.extend(
6697 disabled
: this.isDisabled(),
6699 classes
: [ 'oo-ui-numberInputWidget-plusButton' ],
6708 keydown
: this.onKeyDown
.bind( this ),
6709 'wheel mousewheel DOMMouseScroll': this.onWheel
.bind( this )
6711 if ( config
.showButtons
) {
6712 this.plusButton
.connect( this, {
6713 click
: [ 'onButtonClick', +1 ]
6715 this.minusButton
.connect( this, {
6716 click
: [ 'onButtonClick', -1 ]
6721 $field
.append( this.$input
);
6722 if ( config
.showButtons
) {
6724 .prepend( this.minusButton
.$element
)
6725 .append( this.plusButton
.$element
);
6729 this.setAllowInteger( config
.allowInteger
|| config
.isInteger
);
6730 this.setRange( config
.min
, config
.max
);
6731 this.setStep( config
.step
, config
.pageStep
);
6732 // Set the validation method after we set allowInteger and range
6733 // so that it doesn't immediately call setValidityFlag
6734 this.setValidation( this.validateNumber
.bind( this ) );
6737 .addClass( 'oo-ui-numberInputWidget' )
6738 .toggleClass( 'oo-ui-numberInputWidget-buttoned', config
.showButtons
)
6744 OO
.inheritClass( OO
.ui
.NumberInputWidget
, OO
.ui
.TextInputWidget
);
6749 * Set whether only integers are allowed
6751 * @param {boolean} flag
6753 OO
.ui
.NumberInputWidget
.prototype.setAllowInteger = function ( flag
) {
6754 this.allowInteger
= !!flag
;
6755 this.setValidityFlag();
6757 // Backward compatibility
6758 OO
.ui
.NumberInputWidget
.prototype.setIsInteger
= OO
.ui
.NumberInputWidget
.prototype.setAllowInteger
;
6761 * Get whether only integers are allowed
6763 * @return {boolean} Flag value
6765 OO
.ui
.NumberInputWidget
.prototype.getAllowInteger = function () {
6766 return this.allowInteger
;
6768 // Backward compatibility
6769 OO
.ui
.NumberInputWidget
.prototype.getIsInteger
= OO
.ui
.NumberInputWidget
.prototype.getAllowInteger
;
6772 * Set the range of allowed values
6774 * @param {number} min Minimum allowed value
6775 * @param {number} max Maximum allowed value
6777 OO
.ui
.NumberInputWidget
.prototype.setRange = function ( min
, max
) {
6779 throw new Error( 'Minimum (' + min
+ ') must not be greater than maximum (' + max
+ ')' );
6783 this.setValidityFlag();
6787 * Get the current range
6789 * @return {number[]} Minimum and maximum values
6791 OO
.ui
.NumberInputWidget
.prototype.getRange = function () {
6792 return [ this.min
, this.max
];
6796 * Set the stepping deltas
6798 * @param {number} step Normal step
6799 * @param {number|null} pageStep Page step. If null, 10 * step will be used.
6801 OO
.ui
.NumberInputWidget
.prototype.setStep = function ( step
, pageStep
) {
6803 throw new Error( 'Step value must be positive' );
6805 if ( pageStep
=== null ) {
6806 pageStep
= step
* 10;
6807 } else if ( pageStep
<= 0 ) {
6808 throw new Error( 'Page step value must be positive' );
6811 this.pageStep
= pageStep
;
6815 * Get the current stepping values
6817 * @return {number[]} Step and page step
6819 OO
.ui
.NumberInputWidget
.prototype.getStep = function () {
6820 return [ this.step
, this.pageStep
];
6824 * Get the current value of the widget as a number
6826 * @return {number} May be NaN, or an invalid number
6828 OO
.ui
.NumberInputWidget
.prototype.getNumericValue = function () {
6829 return +this.getValue();
6833 * Adjust the value of the widget
6835 * @param {number} delta Adjustment amount
6837 OO
.ui
.NumberInputWidget
.prototype.adjustValue = function ( delta
) {
6838 var n
, v
= this.getNumericValue();
6841 if ( isNaN( delta
) || !isFinite( delta
) ) {
6842 throw new Error( 'Delta must be a finite number' );
6849 n
= Math
.max( Math
.min( n
, this.max
), this.min
);
6850 if ( this.allowInteger
) {
6851 n
= Math
.round( n
);
6863 * @param {string} value Field value
6866 OO
.ui
.NumberInputWidget
.prototype.validateNumber = function ( value
) {
6868 if ( value
=== '' ) {
6869 return !this.isRequired();
6872 if ( isNaN( n
) || !isFinite( n
) ) {
6876 if ( this.allowInteger
&& Math
.floor( n
) !== n
) {
6880 if ( n
< this.min
|| n
> this.max
) {
6888 * Handle mouse click events.
6891 * @param {number} dir +1 or -1
6893 OO
.ui
.NumberInputWidget
.prototype.onButtonClick = function ( dir
) {
6894 this.adjustValue( dir
* this.step
);
6898 * Handle mouse wheel events.
6901 * @param {jQuery.Event} event
6903 OO
.ui
.NumberInputWidget
.prototype.onWheel = function ( event
) {
6906 if ( !this.isDisabled() && this.$input
.is( ':focus' ) ) {
6907 // Standard 'wheel' event
6908 if ( event
.originalEvent
.deltaMode
!== undefined ) {
6909 this.sawWheelEvent
= true;
6911 if ( event
.originalEvent
.deltaY
) {
6912 delta
= -event
.originalEvent
.deltaY
;
6913 } else if ( event
.originalEvent
.deltaX
) {
6914 delta
= event
.originalEvent
.deltaX
;
6917 // Non-standard events
6918 if ( !this.sawWheelEvent
) {
6919 if ( event
.originalEvent
.wheelDeltaX
) {
6920 delta
= -event
.originalEvent
.wheelDeltaX
;
6921 } else if ( event
.originalEvent
.wheelDeltaY
) {
6922 delta
= event
.originalEvent
.wheelDeltaY
;
6923 } else if ( event
.originalEvent
.wheelDelta
) {
6924 delta
= event
.originalEvent
.wheelDelta
;
6925 } else if ( event
.originalEvent
.detail
) {
6926 delta
= -event
.originalEvent
.detail
;
6931 delta
= delta
< 0 ? -1 : 1;
6932 this.adjustValue( delta
* this.step
);
6940 * Handle key down events.
6943 * @param {jQuery.Event} e Key down event
6945 OO
.ui
.NumberInputWidget
.prototype.onKeyDown = function ( e
) {
6946 if ( !this.isDisabled() ) {
6947 switch ( e
.which
) {
6949 this.adjustValue( this.step
);
6951 case OO
.ui
.Keys
.DOWN
:
6952 this.adjustValue( -this.step
);
6954 case OO
.ui
.Keys
.PAGEUP
:
6955 this.adjustValue( this.pageStep
);
6957 case OO
.ui
.Keys
.PAGEDOWN
:
6958 this.adjustValue( -this.pageStep
);
6967 OO
.ui
.NumberInputWidget
.prototype.setDisabled = function ( disabled
) {
6969 OO
.ui
.NumberInputWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
6971 if ( this.minusButton
) {
6972 this.minusButton
.setDisabled( this.isDisabled() );
6974 if ( this.plusButton
) {
6975 this.plusButton
.setDisabled( this.isDisabled() );
6983 //# sourceMappingURL=oojs-ui-widgets.js.map