3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2017 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2017-05-16T22:31:39Z
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;
38 this.draggable
= config
.draggable
=== undefined ? true : !!config
.draggable
;
40 // Initialize and events
41 this.$element
.addClass( 'oo-ui-draggableElement' )
42 // We make the entire element draggable, not just the handle, so that
43 // the whole element appears to move. wasHandleUsed prevents drags from
44 // starting outside the handle
45 .attr( 'draggable', true )
47 mousedown
: this.onDragMouseDown
.bind( this ),
48 dragstart
: this.onDragStart
.bind( this ),
49 dragover
: this.onDragOver
.bind( this ),
50 dragend
: this.onDragEnd
.bind( this ),
51 drop
: this.onDrop
.bind( this )
53 this.$handle
.addClass( 'oo-ui-draggableElement-handle' );
56 OO
.initClass( OO
.ui
.mixin
.DraggableElement
);
63 * A dragstart event is emitted when the user clicks and begins dragging an item.
64 * @param {OO.ui.mixin.DraggableElement} item The item the user has clicked and is dragging with the mouse.
69 * A dragend event is emitted when the user drags an item and releases the mouse,
70 * thus terminating the drag operation.
75 * A drop event is emitted when the user drags an item and then releases the mouse button
76 * over a valid target.
79 /* Static Properties */
82 * @inheritdoc OO.ui.mixin.ButtonElement
84 OO
.ui
.mixin
.DraggableElement
.static.cancelButtonMouseDownEvents
= false;
89 * Change the draggable state of this widget.
90 * This allows users to temporarily halt the dragging operations.
92 * @param {boolean} isDraggable Widget supports draggable operations
95 OO
.ui
.mixin
.DraggableElement
.prototype.toggleDraggable = function ( isDraggable
) {
96 isDraggable
= isDraggable
!== undefined ? !!isDraggable
: !this.draggable
;
98 if ( this.draggable
!== isDraggable
) {
99 this.draggable
= isDraggable
;
101 this.$handle
.toggleClass( 'oo-ui-draggableElement-undraggable', !this.draggable
);
106 * Check the draggable state of this widget
108 * @return {boolean} Widget supports draggable operations
110 OO
.ui
.mixin
.DraggableElement
.prototype.isDraggable = function () {
111 return this.draggable
;
115 * Respond to mousedown event.
118 * @param {jQuery.Event} e Drag event
120 OO
.ui
.mixin
.DraggableElement
.prototype.onDragMouseDown = function ( e
) {
121 if ( !this.isDraggable() ) {
126 // Optimization: if the handle is the whole element this is always true
127 this.$handle
[ 0 ] === this.$element
[ 0 ] ||
128 // Check the mousedown occurred inside the handle
129 OO
.ui
.contains( this.$handle
[ 0 ], e
.target
, true );
133 * Respond to dragstart event.
136 * @param {jQuery.Event} e Drag event
137 * @return {boolean} False if the event is cancelled
140 OO
.ui
.mixin
.DraggableElement
.prototype.onDragStart = function ( e
) {
142 dataTransfer
= e
.originalEvent
.dataTransfer
;
144 if ( !this.wasHandleUsed
|| !this.isDraggable() ) {
148 // Define drop effect
149 dataTransfer
.dropEffect
= 'none';
150 dataTransfer
.effectAllowed
= 'move';
152 // We must set up a dataTransfer data property or Firefox seems to
153 // ignore the fact the element is draggable.
155 dataTransfer
.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
157 // The above is only for Firefox. Move on if it fails.
159 // Briefly add a 'clone' class to style the browser's native drag image
160 this.$element
.addClass( 'oo-ui-draggableElement-clone' );
161 // Add placeholder class after the browser has rendered the clone
162 setTimeout( function () {
164 .removeClass( 'oo-ui-draggableElement-clone' )
165 .addClass( 'oo-ui-draggableElement-placeholder' );
168 this.emit( 'dragstart', this );
173 * Respond to dragend event.
178 OO
.ui
.mixin
.DraggableElement
.prototype.onDragEnd = function () {
179 this.$element
.removeClass( 'oo-ui-draggableElement-placeholder' );
180 this.emit( 'dragend' );
187 * @param {jQuery.Event} e Drop event
190 OO
.ui
.mixin
.DraggableElement
.prototype.onDrop = function ( e
) {
192 this.emit( 'drop', e
);
196 * In order for drag/drop to work, the dragover event must
197 * return false and stop propogation.
199 * @param {jQuery.Event} e Drag event
202 OO
.ui
.mixin
.DraggableElement
.prototype.onDragOver = function ( e
) {
208 * Store it in the DOM so we can access from the widget drag event
211 * @param {number} index Item index
213 OO
.ui
.mixin
.DraggableElement
.prototype.setIndex = function ( index
) {
214 if ( this.index
!== index
) {
216 this.$element
.data( 'index', index
);
224 * @return {number} Item index
226 OO
.ui
.mixin
.DraggableElement
.prototype.getIndex = function () {
231 * DraggableGroupElement is a mixin class used to create a group element to
232 * contain draggable elements, which are items that can be clicked and dragged by a mouse.
233 * The class is used with OO.ui.mixin.DraggableElement.
237 * @mixins OO.ui.mixin.GroupElement
240 * @param {Object} [config] Configuration options
241 * @cfg {string} [orientation] Item orientation: 'horizontal' or 'vertical'. The orientation
242 * should match the layout of the items. Items displayed in a single row
243 * or in several rows should use horizontal orientation. The vertical orientation should only be
244 * used when the items are displayed in a single column. Defaults to 'vertical'
245 * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
247 OO
.ui
.mixin
.DraggableGroupElement
= function OoUiMixinDraggableGroupElement( config
) {
248 // Configuration initialization
249 config
= config
|| {};
251 // Parent constructor
252 OO
.ui
.mixin
.GroupElement
.call( this, config
);
255 this.orientation
= config
.orientation
|| 'vertical';
256 this.dragItem
= null;
259 this.itemsOrder
= null;
260 this.draggable
= config
.draggable
=== undefined ? true : !!config
.draggable
;
264 dragstart
: 'itemDragStart',
265 dragend
: 'itemDragEnd',
268 this.connect( this, {
269 itemDragStart
: 'onItemDragStart',
270 itemDrop
: 'onItemDropOrDragEnd',
271 itemDragEnd
: 'onItemDropOrDragEnd'
275 if ( Array
.isArray( config
.items
) ) {
276 this.addItems( config
.items
);
279 .addClass( 'oo-ui-draggableGroupElement' )
280 .attr( 'role', 'listbox' )
281 .append( this.$status
)
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 [OOjs UI demos][1] for an example.
623 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/index.html#widgets-apex-vector-ltr
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/OOjs_UI/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
|| 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
.getSelectedItem() ) {
862 this.lookupMenu
.highlightItem( this.lookupMenu
.getFirstSelectableItem() );
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 * The deprecated name for the TabPanelLayout, provided for backwards-compatibility.
1117 * @extends OO.ui.TabPanelLayout
1120 * @deprecated since v0.21.3
1122 OO
.ui
.CardLayout
= function OoUiCardLayout() {
1123 OO
.ui
.warnDeprecation( 'CardLayout has been renamed to TabPanel layout. Use that instead. See T155152' );
1124 // Parent constructor
1125 OO
.ui
.CardLayout
.parent
.apply( this, arguments
);
1128 OO
.inheritClass( OO
.ui
.CardLayout
, OO
.ui
.TabPanelLayout
);
1131 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that users can select and display
1132 * from the booklet's optional {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated directly,
1133 * rather extended to include the required content and functionality.
1135 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the page's outline
1136 * item is customized (with a label, outline level, etc.) using the #setupOutlineItem method. See
1137 * {@link OO.ui.BookletLayout BookletLayout} for an example.
1140 * @extends OO.ui.PanelLayout
1143 * @param {string} name Unique symbolic name of page
1144 * @param {Object} [config] Configuration options
1146 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
1147 // Allow passing positional parameters inside the config object
1148 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1153 // Configuration initialization
1154 config
= $.extend( { scrollable
: true }, config
);
1156 // Parent constructor
1157 OO
.ui
.PageLayout
.parent
.call( this, config
);
1161 this.outlineItem
= null;
1162 this.active
= false;
1165 this.$element
.addClass( 'oo-ui-pageLayout' );
1170 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
1175 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1176 * shown in a booklet layout that is configured to display only one page at a time.
1179 * @param {boolean} active Page is active
1185 * Get the symbolic name of the page.
1187 * @return {string} Symbolic name of page
1189 OO
.ui
.PageLayout
.prototype.getName = function () {
1194 * Check if page is active.
1196 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is configured to display
1197 * only one page at a time. Additional CSS is applied to the page's outline item to reflect the active state.
1199 * @return {boolean} Page is active
1201 OO
.ui
.PageLayout
.prototype.isActive = function () {
1208 * The outline item allows users to access the page from the booklet's outline
1209 * navigation. The outline item itself can be customized (with a label, level, etc.) using the #setupOutlineItem method.
1211 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1213 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
1214 return this.outlineItem
;
1218 * Set or unset the outline item.
1220 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1221 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label or outline
1222 * level), use #setupOutlineItem instead of this method.
1224 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1227 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
1228 this.outlineItem
= outlineItem
|| null;
1229 if ( outlineItem
) {
1230 this.setupOutlineItem();
1236 * Set up the outline item.
1238 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or unset
1239 * the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or `null`), use
1240 * the #setOutlineItem method instead.
1242 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1245 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
1250 * Set the page to its 'active' state.
1252 * Pages become active when they are shown in a booklet layout that is configured to display only one page at a time. Additional
1253 * CSS is applied to the outline item to reflect the page's active state. Outside of the booklet
1254 * context, setting the active state on a page does nothing.
1256 * @param {boolean} active Page is active
1259 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
1262 if ( active
!== this.active
) {
1263 this.active
= active
;
1264 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
1265 this.emit( 'active', this.active
);
1270 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one panel is displayed
1271 * at a time, though the stack layout can also be configured to show all contained panels, one after another,
1272 * by setting the #continuous option to 'true'.
1275 * // A stack layout with two panels, configured to be displayed continously
1276 * var myStack = new OO.ui.StackLayout( {
1278 * new OO.ui.PanelLayout( {
1279 * $content: $( '<p>Panel One</p>' ),
1283 * new OO.ui.PanelLayout( {
1284 * $content: $( '<p>Panel Two</p>' ),
1291 * $( 'body' ).append( myStack.$element );
1294 * @extends OO.ui.PanelLayout
1295 * @mixins OO.ui.mixin.GroupElement
1298 * @param {Object} [config] Configuration options
1299 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel is displayed at a time.
1300 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1302 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
1303 // Configuration initialization
1304 config
= $.extend( { scrollable
: true }, config
);
1306 // Parent constructor
1307 OO
.ui
.StackLayout
.parent
.call( this, config
);
1309 // Mixin constructors
1310 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
1313 this.currentItem
= null;
1314 this.continuous
= !!config
.continuous
;
1317 this.$element
.addClass( 'oo-ui-stackLayout' );
1318 if ( this.continuous
) {
1319 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
1320 this.$element
.on( 'scroll', OO
.ui
.debounce( this.onScroll
.bind( this ), 250 ) );
1322 if ( Array
.isArray( config
.items
) ) {
1323 this.addItems( config
.items
);
1329 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
1330 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.mixin
.GroupElement
);
1335 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1336 * {@link #clearItems cleared} or {@link #setItem displayed}.
1339 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1343 * When used in continuous mode, this event is emitted when the user scrolls down
1344 * far enough such that currentItem is no longer visible.
1346 * @event visibleItemChange
1347 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1353 * Handle scroll events from the layout element
1355 * @param {jQuery.Event} e
1356 * @fires visibleItemChange
1358 OO
.ui
.StackLayout
.prototype.onScroll = function () {
1360 len
= this.items
.length
,
1361 currentIndex
= this.items
.indexOf( this.currentItem
),
1362 newIndex
= currentIndex
,
1363 containerRect
= this.$element
[ 0 ].getBoundingClientRect();
1365 if ( !containerRect
|| ( !containerRect
.top
&& !containerRect
.bottom
) ) {
1366 // Can't get bounding rect, possibly not attached.
1370 function getRect( item
) {
1371 return item
.$element
[ 0 ].getBoundingClientRect();
1374 function isVisible( item
) {
1375 var rect
= getRect( item
);
1376 return rect
.bottom
> containerRect
.top
&& rect
.top
< containerRect
.bottom
;
1379 currentRect
= getRect( this.currentItem
);
1381 if ( currentRect
.bottom
< containerRect
.top
) {
1382 // Scrolled down past current item
1383 while ( ++newIndex
< len
) {
1384 if ( isVisible( this.items
[ newIndex
] ) ) {
1388 } else if ( currentRect
.top
> containerRect
.bottom
) {
1389 // Scrolled up past current item
1390 while ( --newIndex
>= 0 ) {
1391 if ( isVisible( this.items
[ newIndex
] ) ) {
1397 if ( newIndex
!== currentIndex
) {
1398 this.emit( 'visibleItemChange', this.items
[ newIndex
] );
1403 * Get the current panel.
1405 * @return {OO.ui.Layout|null}
1407 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
1408 return this.currentItem
;
1412 * Unset the current item.
1415 * @param {OO.ui.StackLayout} layout
1418 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
1419 var prevItem
= this.currentItem
;
1420 if ( prevItem
=== null ) {
1424 this.currentItem
= null;
1425 this.emit( 'set', null );
1429 * Add panel layouts to the stack layout.
1431 * Panels will be added to the end of the stack layout array unless the optional index parameter specifies a different
1432 * insertion point. Adding a panel that is already in the stack will move it to the end of the array or the point specified
1435 * @param {OO.ui.Layout[]} items Panels to add
1436 * @param {number} [index] Index of the insertion point
1439 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
1440 // Update the visibility
1441 this.updateHiddenState( items
, this.currentItem
);
1444 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
, index
);
1446 if ( !this.currentItem
&& items
.length
) {
1447 this.setItem( items
[ 0 ] );
1454 * Remove the specified panels from the stack layout.
1456 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all panels,
1457 * you may wish to use the #clearItems method instead.
1459 * @param {OO.ui.Layout[]} items Panels to remove
1463 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
1465 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
1467 if ( items
.indexOf( this.currentItem
) !== -1 ) {
1468 if ( this.items
.length
) {
1469 this.setItem( this.items
[ 0 ] );
1471 this.unsetCurrentItem();
1479 * Clear all panels from the stack layout.
1481 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1482 * a subset of panels, use the #removeItems method.
1487 OO
.ui
.StackLayout
.prototype.clearItems = function () {
1488 this.unsetCurrentItem();
1489 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
1495 * Show the specified panel.
1497 * If another panel is currently displayed, it will be hidden.
1499 * @param {OO.ui.Layout} item Panel to show
1503 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
1504 if ( item
!== this.currentItem
) {
1505 this.updateHiddenState( this.items
, item
);
1507 if ( this.items
.indexOf( item
) !== -1 ) {
1508 this.currentItem
= item
;
1509 this.emit( 'set', item
);
1511 this.unsetCurrentItem();
1519 * Update the visibility of all items in case of non-continuous view.
1521 * Ensure all items are hidden except for the selected one.
1522 * This method does nothing when the stack is continuous.
1525 * @param {OO.ui.Layout[]} items Item list iterate over
1526 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1528 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
1531 if ( !this.continuous
) {
1532 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
1533 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
1534 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
1535 items
[ i
].$element
.attr( 'aria-hidden', 'true' );
1538 if ( selectedItem
) {
1539 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
1540 selectedItem
.$element
.removeAttr( 'aria-hidden' );
1546 * 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)
1547 * and its size is customized with the #menuSize config. The content area will fill all remaining space.
1550 * var menuLayout = new OO.ui.MenuLayout( {
1553 * menuPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1554 * contentPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1555 * select = new OO.ui.SelectWidget( {
1557 * new OO.ui.OptionWidget( {
1561 * new OO.ui.OptionWidget( {
1565 * new OO.ui.OptionWidget( {
1569 * new OO.ui.OptionWidget( {
1574 * } ).on( 'select', function ( item ) {
1575 * menuLayout.setMenuPosition( item.getData() );
1578 * menuLayout.$menu.append(
1579 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element )
1581 * menuLayout.$content.append(
1582 * contentPanel.$element.append( '<b>Content panel</b>', '<p>Note that the menu is positioned relative to the content panel: top, bottom, after, before.</p>')
1584 * $( 'body' ).append( menuLayout.$element );
1586 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1587 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1588 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1591 * .oo-ui-menuLayout-menu {
1595 * .oo-ui-menuLayout-content {
1603 * @extends OO.ui.Layout
1606 * @param {Object} [config] Configuration options
1607 * @cfg {boolean} [showMenu=true] Show menu
1608 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1610 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
1611 // Configuration initialization
1612 config
= $.extend( {
1614 menuPosition
: 'before'
1617 // Parent constructor
1618 OO
.ui
.MenuLayout
.parent
.call( this, config
);
1623 * @property {jQuery}
1625 this.$menu
= $( '<div>' );
1629 * @property {jQuery}
1631 this.$content
= $( '<div>' );
1635 .addClass( 'oo-ui-menuLayout-menu' );
1636 this.$content
.addClass( 'oo-ui-menuLayout-content' );
1638 .addClass( 'oo-ui-menuLayout' )
1639 .append( this.$content
, this.$menu
);
1640 this.setMenuPosition( config
.menuPosition
);
1641 this.toggleMenu( config
.showMenu
);
1646 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
1653 * @param {boolean} showMenu Show menu, omit to toggle
1656 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
1657 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
1659 if ( this.showMenu
!== showMenu
) {
1660 this.showMenu
= showMenu
;
1662 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu
)
1663 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu
);
1664 this.$menu
.attr( 'aria-hidden', this.showMenu
? 'false' : 'true' );
1671 * Check if menu is visible
1673 * @return {boolean} Menu is visible
1675 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
1676 return this.showMenu
;
1680 * Set menu position.
1682 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1683 * @throws {Error} If position value is not supported
1686 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
1687 this.$element
.removeClass( 'oo-ui-menuLayout-' + this.menuPosition
);
1688 this.menuPosition
= position
;
1689 this.$element
.addClass( 'oo-ui-menuLayout-' + position
);
1695 * Get menu position.
1697 * @return {string} Menu position
1699 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
1700 return this.menuPosition
;
1704 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1705 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1706 * through the pages and select which one to display. By default, only one page is
1707 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1708 * the booklet layout automatically focuses on the first focusable element, unless the
1709 * default setting is changed. Optionally, booklets can be configured to show
1710 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1713 * // Example of a BookletLayout that contains two PageLayouts.
1715 * function PageOneLayout( name, config ) {
1716 * PageOneLayout.parent.call( this, name, config );
1717 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on the left)</p>' );
1719 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1720 * PageOneLayout.prototype.setupOutlineItem = function () {
1721 * this.outlineItem.setLabel( 'Page One' );
1724 * function PageTwoLayout( name, config ) {
1725 * PageTwoLayout.parent.call( this, name, config );
1726 * this.$element.append( '<p>Second page</p>' );
1728 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1729 * PageTwoLayout.prototype.setupOutlineItem = function () {
1730 * this.outlineItem.setLabel( 'Page Two' );
1733 * var page1 = new PageOneLayout( 'one' ),
1734 * page2 = new PageTwoLayout( 'two' );
1736 * var booklet = new OO.ui.BookletLayout( {
1740 * booklet.addPages ( [ page1, page2 ] );
1741 * $( 'body' ).append( booklet.$element );
1744 * @extends OO.ui.MenuLayout
1747 * @param {Object} [config] Configuration options
1748 * @cfg {boolean} [continuous=false] Show all pages, one after another
1749 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is displayed. Disabled on mobile.
1750 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the pages of the booklet.
1751 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
1753 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
1754 // Configuration initialization
1755 config
= config
|| {};
1757 // Parent constructor
1758 OO
.ui
.BookletLayout
.parent
.call( this, config
);
1761 this.currentPageName
= null;
1763 this.ignoreFocus
= false;
1764 this.stackLayout
= new OO
.ui
.StackLayout( { continuous
: !!config
.continuous
} );
1765 this.$content
.append( this.stackLayout
.$element
);
1766 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
1767 this.outlineVisible
= false;
1768 this.outlined
= !!config
.outlined
;
1769 if ( this.outlined
) {
1770 this.editable
= !!config
.editable
;
1771 this.outlineControlsWidget
= null;
1772 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget();
1773 this.outlinePanel
= new OO
.ui
.PanelLayout( { scrollable
: true } );
1774 this.$menu
.append( this.outlinePanel
.$element
);
1775 this.outlineVisible
= true;
1776 if ( this.editable
) {
1777 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
1778 this.outlineSelectWidget
1782 this.toggleMenu( this.outlined
);
1785 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
1786 if ( this.outlined
) {
1787 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
1788 this.scrolling
= false;
1789 this.stackLayout
.connect( this, { visibleItemChange
: 'onStackLayoutVisibleItemChange' } );
1791 if ( this.autoFocus
) {
1792 // Event 'focus' does not bubble, but 'focusin' does
1793 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
1797 this.$element
.addClass( 'oo-ui-bookletLayout' );
1798 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
1799 if ( this.outlined
) {
1800 this.outlinePanel
.$element
1801 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1802 .append( this.outlineSelectWidget
.$element
);
1803 if ( this.editable
) {
1804 this.outlinePanel
.$element
1805 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1806 .append( this.outlineControlsWidget
.$element
);
1813 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
1818 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the booklet layout.
1820 * @param {OO.ui.PageLayout} page Current page
1824 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
1827 * @param {OO.ui.PageLayout[]} page Added pages
1828 * @param {number} index Index pages were added at
1832 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
1833 * {@link #removePages removed} from the booklet.
1836 * @param {OO.ui.PageLayout[]} pages Removed pages
1842 * Handle stack layout focus.
1845 * @param {jQuery.Event} e Focusin event
1847 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
1850 // Find the page that an element was focused within
1851 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
1852 for ( name
in this.pages
) {
1853 // Check for page match, exclude current page to find only page changes
1854 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
1855 this.setPage( name
);
1862 * Handle visibleItemChange events from the stackLayout
1864 * The next visible page is set as the current page by selecting it
1867 * @param {OO.ui.PageLayout} page The next visible page in the layout
1869 OO
.ui
.BookletLayout
.prototype.onStackLayoutVisibleItemChange = function ( page
) {
1870 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
1871 // try and scroll the item into view again.
1872 this.scrolling
= true;
1873 this.outlineSelectWidget
.selectItemByData( page
.getName() );
1874 this.scrolling
= false;
1878 * Handle stack layout set events.
1881 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
1883 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
1885 if ( !this.scrolling
&& page
) {
1886 page
.scrollElementIntoView().done( function () {
1887 if ( layout
.autoFocus
&& !OO
.ui
.isMobile() ) {
1895 * Focus the first input in the current page.
1897 * If no page is selected, the first selectable page will be selected.
1898 * If the focus is already in an element on the current page, nothing will happen.
1900 * @param {number} [itemIndex] A specific item to focus on
1902 OO
.ui
.BookletLayout
.prototype.focus = function ( itemIndex
) {
1904 items
= this.stackLayout
.getItems();
1906 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
1907 page
= items
[ itemIndex
];
1909 page
= this.stackLayout
.getCurrentItem();
1912 if ( !page
&& this.outlined
) {
1913 this.selectFirstSelectablePage();
1914 page
= this.stackLayout
.getCurrentItem();
1919 // Only change the focus if is not already in the current page
1920 if ( !OO
.ui
.contains( page
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
1926 * Find the first focusable input in the booklet layout and focus
1929 OO
.ui
.BookletLayout
.prototype.focusFirstFocusable = function () {
1930 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
1934 * Handle outline widget select events.
1937 * @param {OO.ui.OptionWidget|null} item Selected item
1939 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
1941 this.setPage( item
.getData() );
1946 * Check if booklet has an outline.
1948 * @return {boolean} Booklet has an outline
1950 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
1951 return this.outlined
;
1955 * Check if booklet has editing controls.
1957 * @return {boolean} Booklet is editable
1959 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
1960 return this.editable
;
1964 * Check if booklet has a visible outline.
1966 * @return {boolean} Outline is visible
1968 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
1969 return this.outlined
&& this.outlineVisible
;
1973 * Hide or show the outline.
1975 * @param {boolean} [show] Show outline, omit to invert current state
1978 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
1979 if ( this.outlined
) {
1980 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
1981 this.outlineVisible
= show
;
1982 this.toggleMenu( show
);
1989 * Get the page closest to the specified page.
1991 * @param {OO.ui.PageLayout} page Page to use as a reference point
1992 * @return {OO.ui.PageLayout|null} Page closest to the specified page
1994 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
1995 var next
, prev
, level
,
1996 pages
= this.stackLayout
.getItems(),
1997 index
= pages
.indexOf( page
);
1999 if ( index
!== -1 ) {
2000 next
= pages
[ index
+ 1 ];
2001 prev
= pages
[ index
- 1 ];
2002 // Prefer adjacent pages at the same level
2003 if ( this.outlined
) {
2004 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
2007 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
2013 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
2019 return prev
|| next
|| null;
2023 * Get the outline widget.
2025 * If the booklet is not outlined, the method will return `null`.
2027 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
2029 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
2030 return this.outlineSelectWidget
;
2034 * Get the outline controls widget.
2036 * If the outline is not editable, the method will return `null`.
2038 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
2040 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
2041 return this.outlineControlsWidget
;
2045 * Get a page by its symbolic name.
2047 * @param {string} name Symbolic name of page
2048 * @return {OO.ui.PageLayout|undefined} Page, if found
2050 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
2051 return this.pages
[ name
];
2055 * Get the current page.
2057 * @return {OO.ui.PageLayout|undefined} Current page, if found
2059 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
2060 var name
= this.getCurrentPageName();
2061 return name
? this.getPage( name
) : undefined;
2065 * Get the symbolic name of the current page.
2067 * @return {string|null} Symbolic name of the current page
2069 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
2070 return this.currentPageName
;
2074 * Add pages to the booklet layout
2076 * When pages are added with the same names as existing pages, the existing pages will be
2077 * automatically removed before the new pages are added.
2079 * @param {OO.ui.PageLayout[]} pages Pages to add
2080 * @param {number} index Index of the insertion point
2084 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
2085 var i
, len
, name
, page
, item
, currentIndex
,
2086 stackLayoutPages
= this.stackLayout
.getItems(),
2090 // Remove pages with same names
2091 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2093 name
= page
.getName();
2095 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
2096 // Correct the insertion index
2097 currentIndex
= stackLayoutPages
.indexOf( this.pages
[ name
] );
2098 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2101 remove
.push( this.pages
[ name
] );
2104 if ( remove
.length
) {
2105 this.removePages( remove
);
2109 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2111 name
= page
.getName();
2112 this.pages
[ page
.getName() ] = page
;
2113 if ( this.outlined
) {
2114 item
= new OO
.ui
.OutlineOptionWidget( { data
: name
} );
2115 page
.setOutlineItem( item
);
2120 if ( this.outlined
&& items
.length
) {
2121 this.outlineSelectWidget
.addItems( items
, index
);
2122 this.selectFirstSelectablePage();
2124 this.stackLayout
.addItems( pages
, index
);
2125 this.emit( 'add', pages
, index
);
2131 * Remove the specified pages from the booklet layout.
2133 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2135 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2139 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
2140 var i
, len
, name
, page
,
2143 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2145 name
= page
.getName();
2146 delete this.pages
[ name
];
2147 if ( this.outlined
) {
2148 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
2149 page
.setOutlineItem( null );
2152 if ( this.outlined
&& items
.length
) {
2153 this.outlineSelectWidget
.removeItems( items
);
2154 this.selectFirstSelectablePage();
2156 this.stackLayout
.removeItems( pages
);
2157 this.emit( 'remove', pages
);
2163 * Clear all pages from the booklet layout.
2165 * To remove only a subset of pages from the booklet, use the #removePages method.
2170 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
2172 pages
= this.stackLayout
.getItems();
2175 this.currentPageName
= null;
2176 if ( this.outlined
) {
2177 this.outlineSelectWidget
.clearItems();
2178 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2179 pages
[ i
].setOutlineItem( null );
2182 this.stackLayout
.clearItems();
2184 this.emit( 'remove', pages
);
2190 * Set the current page by symbolic name.
2193 * @param {string} name Symbolic name of page
2195 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
2198 page
= this.pages
[ name
],
2199 previousPage
= this.currentPageName
&& this.pages
[ this.currentPageName
];
2201 if ( name
!== this.currentPageName
) {
2202 if ( this.outlined
) {
2203 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
2204 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2205 this.outlineSelectWidget
.selectItemByData( name
);
2209 if ( previousPage
) {
2210 previousPage
.setActive( false );
2211 // Blur anything focused if the next page doesn't have anything focusable.
2212 // This is not needed if the next page has something focusable (because once it is focused
2213 // this blur happens automatically). If the layout is non-continuous, this check is
2214 // meaningless because the next page is not visible yet and thus can't hold focus.
2217 !OO
.ui
.isMobile() &&
2218 this.stackLayout
.continuous
&&
2219 OO
.ui
.findFocusable( page
.$element
).length
!== 0
2221 $focused
= previousPage
.$element
.find( ':focus' );
2222 if ( $focused
.length
) {
2223 $focused
[ 0 ].blur();
2227 this.currentPageName
= name
;
2228 page
.setActive( true );
2229 this.stackLayout
.setItem( page
);
2230 if ( !this.stackLayout
.continuous
&& previousPage
) {
2231 // This should not be necessary, since any inputs on the previous page should have been
2232 // blurred when it was hidden, but browsers are not very consistent about this.
2233 $focused
= previousPage
.$element
.find( ':focus' );
2234 if ( $focused
.length
) {
2235 $focused
[ 0 ].blur();
2238 this.emit( 'set', page
);
2244 * Select the first selectable page.
2248 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
2249 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
2250 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
2257 * IndexLayouts contain {@link OO.ui.TabPanelLayout tab panel layouts} as well as
2258 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the tab panels and
2259 * select which one to display. By default, only one tab panel is displayed at a time. When a user
2260 * navigates to a new tab panel, the index layout automatically focuses on the first focusable element,
2261 * unless the default setting is changed.
2263 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2266 * // Example of a IndexLayout that contains two TabPanelLayouts.
2268 * function TabPanelOneLayout( name, config ) {
2269 * TabPanelOneLayout.parent.call( this, name, config );
2270 * this.$element.append( '<p>First tab panel</p>' );
2272 * OO.inheritClass( TabPanelOneLayout, OO.ui.TabPanelLayout );
2273 * TabPanelOneLayout.prototype.setupTabItem = function () {
2274 * this.tabItem.setLabel( 'Tab panel one' );
2277 * var tabPanel1 = new TabPanelOneLayout( 'one' ),
2278 * tabPanel2 = new OO.ui.TabPanelLayout( 'two', { label: 'Tab panel two' } );
2280 * tabPanel2.$element.append( '<p>Second tab panel</p>' );
2282 * var index = new OO.ui.IndexLayout();
2284 * index.addTabPanels ( [ tabPanel1, tabPanel2 ] );
2285 * $( 'body' ).append( index.$element );
2288 * @extends OO.ui.MenuLayout
2291 * @param {Object} [config] Configuration options
2292 * @cfg {boolean} [continuous=false] Show all tab panels, one after another
2293 * @cfg {boolean} [expanded=true] Expand the content panel to fill the entire parent element.
2294 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new tab panel is displayed. Disabled on mobile.
2296 OO
.ui
.IndexLayout
= function OoUiIndexLayout( config
) {
2297 // Configuration initialization
2298 config
= $.extend( {}, config
, { menuPosition
: 'top' } );
2300 // Parent constructor
2301 OO
.ui
.IndexLayout
.parent
.call( this, config
);
2304 this.currentTabPanelName
= null;
2305 this.tabPanels
= {};
2307 Object
.defineProperty( this, 'currentCardName', {
2308 // TODO: read documentation
2312 OO
.ui
.warnDeprecation( 'IndexLayout\'s currentCardName property is deprecated. Use currentTabPanelName instead. See T155152' );
2313 return this.currentTabPanelName
;
2315 set: function ( value
) {
2316 OO
.ui
.warnDeprecation( 'IndexLayout\'s currentCardName property is deprecated. Use currentTabPanelName instead. See T155152' );
2317 this.currentTabPanelName
= value
;
2321 Object
.defineProperty( this, 'cards', {
2322 // TODO: read documentation
2326 OO
.ui
.warnDeprecation( 'IndexLayout\'s cards property is deprecated. Use tabPanels instead. See T155152' );
2327 return this.tabPanels
;
2329 set: function ( value
) {
2330 OO
.ui
.warnDeprecation( 'IndexLayout\'s cards property is deprecated. Use tabPanels instead. See T155152' );
2331 this.tabPanels
= value
;
2335 this.ignoreFocus
= false;
2336 this.stackLayout
= new OO
.ui
.StackLayout( {
2337 continuous
: !!config
.continuous
,
2338 expanded
: config
.expanded
2340 this.$content
.append( this.stackLayout
.$element
);
2341 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
2343 this.tabSelectWidget
= new OO
.ui
.TabSelectWidget();
2344 this.tabPanel
= new OO
.ui
.PanelLayout();
2345 this.$menu
.append( this.tabPanel
.$element
);
2347 this.toggleMenu( true );
2350 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
2351 this.tabSelectWidget
.connect( this, { select
: 'onTabSelectWidgetSelect' } );
2352 if ( this.autoFocus
) {
2353 // Event 'focus' does not bubble, but 'focusin' does
2354 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
2358 this.$element
.addClass( 'oo-ui-indexLayout' );
2359 this.stackLayout
.$element
.addClass( 'oo-ui-indexLayout-stackLayout' );
2360 this.tabPanel
.$element
2361 .addClass( 'oo-ui-indexLayout-tabPanel' )
2362 .append( this.tabSelectWidget
.$element
);
2367 OO
.inheritClass( OO
.ui
.IndexLayout
, OO
.ui
.MenuLayout
);
2372 * A 'set' event is emitted when a tab panel is {@link #setTabPanel set} to be displayed by the index layout.
2374 * @param {OO.ui.TabPanelLayout} tabPanel Current tab panel
2378 * An 'add' event is emitted when tab panels are {@link #addTabPanels added} to the index layout.
2381 * @param {OO.ui.TabPanelLayout[]} tabPanel Added tab panels
2382 * @param {number} index Index tab panels were added at
2386 * A 'remove' event is emitted when tab panels are {@link #clearTabPanels cleared} or
2387 * {@link #removeTabPanels removed} from the index.
2390 * @param {OO.ui.TabPanelLayout[]} tabPanel Removed tab panels
2396 * Handle stack layout focus.
2399 * @param {jQuery.Event} e Focusing event
2401 OO
.ui
.IndexLayout
.prototype.onStackLayoutFocus = function ( e
) {
2404 // Find the tab panel that an element was focused within
2405 $target
= $( e
.target
).closest( '.oo-ui-tabPanelLayout' );
2406 for ( name
in this.tabPanels
) {
2407 // Check for tab panel match, exclude current tab panel to find only tab panel changes
2408 if ( this.tabPanels
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentTabPanelName
) {
2409 this.setTabPanel( name
);
2416 * Handle stack layout set events.
2419 * @param {OO.ui.PanelLayout|null} tabPanel The tab panel that is now the current panel
2421 OO
.ui
.IndexLayout
.prototype.onStackLayoutSet = function ( tabPanel
) {
2424 tabPanel
.scrollElementIntoView().done( function () {
2425 if ( layout
.autoFocus
&& !OO
.ui
.isMobile() ) {
2433 * Focus the first input in the current tab panel.
2435 * If no tab panel is selected, the first selectable tab panel will be selected.
2436 * If the focus is already in an element on the current tab panel, nothing will happen.
2438 * @param {number} [itemIndex] A specific item to focus on
2440 OO
.ui
.IndexLayout
.prototype.focus = function ( itemIndex
) {
2442 items
= this.stackLayout
.getItems();
2444 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2445 tabPanel
= items
[ itemIndex
];
2447 tabPanel
= this.stackLayout
.getCurrentItem();
2451 this.selectFirstSelectableTabPanel();
2452 tabPanel
= this.stackLayout
.getCurrentItem();
2457 // Only change the focus if is not already in the current page
2458 if ( !OO
.ui
.contains( tabPanel
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
2464 * Find the first focusable input in the index layout and focus
2467 OO
.ui
.IndexLayout
.prototype.focusFirstFocusable = function () {
2468 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2472 * Handle tab widget select events.
2475 * @param {OO.ui.OptionWidget|null} item Selected item
2477 OO
.ui
.IndexLayout
.prototype.onTabSelectWidgetSelect = function ( item
) {
2479 this.setTabPanel( item
.getData() );
2484 * Get the tab panel closest to the specified tab panel.
2486 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2487 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2489 OO
.ui
.IndexLayout
.prototype.getClosestTabPanel = function ( tabPanel
) {
2490 var next
, prev
, level
,
2491 tabPanels
= this.stackLayout
.getItems(),
2492 index
= tabPanels
.indexOf( tabPanel
);
2494 if ( index
!== -1 ) {
2495 next
= tabPanels
[ index
+ 1 ];
2496 prev
= tabPanels
[ index
- 1 ];
2497 // Prefer adjacent tab panels at the same level
2498 level
= this.tabSelectWidget
.getItemFromData( tabPanel
.getName() ).getLevel();
2501 level
=== this.tabSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
2507 level
=== this.tabSelectWidget
.getItemFromData( next
.getName() ).getLevel()
2512 return prev
|| next
|| null;
2516 * Get the tab panel closest to the specified tab panel.
2518 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2519 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2520 * @deprecated since v0.21.3, use `getClosestTabPanel` instead
2522 OO
.ui
.IndexLayout
.prototype.getClosestCard = function ( tabPanel
) {
2523 OO
.ui
.warnDeprecation( 'IndexLayout\'s getClosestCard method is deprecated. Use getClosestTabPanel instead. See T155152' );
2524 return this.getClosestTabPanel( tabPanel
);
2528 * Get the tabs widget.
2530 * @return {OO.ui.TabSelectWidget} Tabs widget
2532 OO
.ui
.IndexLayout
.prototype.getTabs = function () {
2533 return this.tabSelectWidget
;
2537 * Get a tab panel by its symbolic name.
2539 * @param {string} name Symbolic name of tab panel
2540 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2542 OO
.ui
.IndexLayout
.prototype.getTabPanel = function ( name
) {
2543 return this.tabPanels
[ name
];
2547 * Get a tab panel by its symbolic name.
2549 * @param {string} name Symbolic name of tab panel
2550 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2551 * @deprecated since v0.21.3, use `getTabPanel` instead
2553 OO
.ui
.IndexLayout
.prototype.getCard = function ( name
) {
2554 OO
.ui
.warnDeprecation( 'IndexLayout\'s getCard method is deprecated. Use getTabPanel instead. See T155152' );
2555 return this.getTabPanel( name
);
2559 * Get the current tab panel.
2561 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2563 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanel = function () {
2564 var name
= this.getCurrentTabPanelName();
2565 return name
? this.getTabPanel( name
) : undefined;
2569 * Get the current tab panel.
2571 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2572 * @deprecated since v0.21.3, use `getCurrentTabPanel` instead
2574 OO
.ui
.IndexLayout
.prototype.getCurrentCard = function () {
2575 OO
.ui
.warnDeprecation( 'IndexLayout\'s getCurrentCard method is deprecated. Use getCurrentTabPanel instead. See T155152' );
2576 return this.getCurrentTabPanel();
2580 * Get the symbolic name of the current tab panel.
2582 * @return {string|null} Symbolic name of the current tab panel
2584 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanelName = function () {
2585 return this.currentTabPanelName
;
2589 * Get the symbolic name of the current tab panel.
2591 * @return {string|null} Symbolic name of the current tab panel
2592 * @deprecated since v0.21.3, use `getCurrentTabPanelName` instead
2594 OO
.ui
.IndexLayout
.prototype.getCurrentCardName = function () {
2595 OO
.ui
.warnDeprecation( 'IndexLayout\'s getCurrentCardName method is deprecated. Use getCurrentTabPanelName instead. See T155152' );
2596 return this.getCurrentTabPanelName();
2600 * Add tab panels to the index layout
2602 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2603 * will be automatically removed before the new tab panels are added.
2605 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2606 * @param {number} index Index of the insertion point
2610 OO
.ui
.IndexLayout
.prototype.addTabPanels = function ( tabPanels
, index
) {
2611 var i
, len
, name
, tabPanel
, item
, currentIndex
,
2612 stackLayoutTabPanels
= this.stackLayout
.getItems(),
2616 // Remove tab panels with same names
2617 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2618 tabPanel
= tabPanels
[ i
];
2619 name
= tabPanel
.getName();
2621 if ( Object
.prototype.hasOwnProperty
.call( this.tabPanels
, name
) ) {
2622 // Correct the insertion index
2623 currentIndex
= stackLayoutTabPanels
.indexOf( this.tabPanels
[ name
] );
2624 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2627 remove
.push( this.tabPanels
[ name
] );
2630 if ( remove
.length
) {
2631 this.removeTabPanels( remove
);
2634 // Add new tab panels
2635 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2636 tabPanel
= tabPanels
[ i
];
2637 name
= tabPanel
.getName();
2638 this.tabPanels
[ tabPanel
.getName() ] = tabPanel
;
2639 item
= new OO
.ui
.TabOptionWidget( { data
: name
} );
2640 tabPanel
.setTabItem( item
);
2644 if ( items
.length
) {
2645 this.tabSelectWidget
.addItems( items
, index
);
2646 this.selectFirstSelectableTabPanel();
2648 this.stackLayout
.addItems( tabPanels
, index
);
2649 this.emit( 'add', tabPanels
, index
);
2655 * Add tab panels to the index layout
2657 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2658 * will be automatically removed before the new tab panels are added.
2660 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2661 * @param {number} index Index of the insertion point
2664 * @deprecated since v0.21.3, use `addTabPanels` instead
2666 OO
.ui
.IndexLayout
.prototype.addCards = function ( tabPanels
, index
) {
2667 OO
.ui
.warnDeprecation( 'IndexLayout\'s addCards method is deprecated. Use addTabPanels instead. See T155152' );
2668 return this.addTabPanels( tabPanels
, index
);
2672 * Remove the specified tab panels from the index layout.
2674 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2676 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2680 OO
.ui
.IndexLayout
.prototype.removeTabPanels = function ( tabPanels
) {
2681 var i
, len
, name
, tabPanel
,
2684 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2685 tabPanel
= tabPanels
[ i
];
2686 name
= tabPanel
.getName();
2687 delete this.tabPanels
[ name
];
2688 items
.push( this.tabSelectWidget
.getItemFromData( name
) );
2689 tabPanel
.setTabItem( null );
2691 if ( items
.length
) {
2692 this.tabSelectWidget
.removeItems( items
);
2693 this.selectFirstSelectableTabPanel();
2695 this.stackLayout
.removeItems( tabPanels
);
2696 this.emit( 'remove', tabPanels
);
2702 * Remove the specified tab panels from the index layout.
2704 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2706 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2709 * @deprecated since v0.21.3, use `removeTabPanels` instead
2711 OO
.ui
.IndexLayout
.prototype.removeCards = function ( tabPanels
) {
2712 OO
.ui
.warnDeprecation( 'IndexLayout\'s removeCards method is deprecated. Use removeTabPanels instead. See T155152.' );
2713 return this.removeTabPanels( tabPanels
);
2717 * Clear all tab panels from the index layout.
2719 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2724 OO
.ui
.IndexLayout
.prototype.clearTabPanels = function () {
2726 tabPanels
= this.stackLayout
.getItems();
2728 this.tabPanels
= {};
2729 this.currentTabPanelName
= null;
2730 this.tabSelectWidget
.clearItems();
2731 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2732 tabPanels
[ i
].setTabItem( null );
2734 this.stackLayout
.clearItems();
2736 this.emit( 'remove', tabPanels
);
2742 * Clear all tab panels from the index layout.
2744 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2748 * @deprecated since v0.21.3, use `clearTabPanels` instead
2750 OO
.ui
.IndexLayout
.prototype.clearCards = function () {
2751 OO
.ui
.warnDeprecation( 'IndexLayout\'s clearCards method is deprecated. Use clearTabPanels instead. See T155152.' );
2752 return this.clearTabPanels();
2756 * Set the current tab panel by symbolic name.
2759 * @param {string} name Symbolic name of tab panel
2761 OO
.ui
.IndexLayout
.prototype.setTabPanel = function ( name
) {
2764 tabPanel
= this.tabPanels
[ name
],
2765 previousTabPanel
= this.currentTabPanelName
&& this.tabPanels
[ this.currentTabPanelName
];
2767 if ( name
!== this.currentTabPanelName
) {
2768 selectedItem
= this.tabSelectWidget
.getSelectedItem();
2769 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2770 this.tabSelectWidget
.selectItemByData( name
);
2773 if ( previousTabPanel
) {
2774 previousTabPanel
.setActive( false );
2775 // Blur anything focused if the next tab panel doesn't have anything focusable.
2776 // This is not needed if the next tab panel has something focusable (because once it is focused
2777 // this blur happens automatically). If the layout is non-continuous, this check is
2778 // meaningless because the next tab panel is not visible yet and thus can't hold focus.
2781 !OO
.ui
.isMobile() &&
2782 this.stackLayout
.continuous
&&
2783 OO
.ui
.findFocusable( tabPanel
.$element
).length
!== 0
2785 $focused
= previousTabPanel
.$element
.find( ':focus' );
2786 if ( $focused
.length
) {
2787 $focused
[ 0 ].blur();
2791 this.currentTabPanelName
= name
;
2792 tabPanel
.setActive( true );
2793 this.stackLayout
.setItem( tabPanel
);
2794 if ( !this.stackLayout
.continuous
&& previousTabPanel
) {
2795 // This should not be necessary, since any inputs on the previous tab panel should have been
2796 // blurred when it was hidden, but browsers are not very consistent about this.
2797 $focused
= previousTabPanel
.$element
.find( ':focus' );
2798 if ( $focused
.length
) {
2799 $focused
[ 0 ].blur();
2802 this.emit( 'set', tabPanel
);
2808 * Set the current tab panel by symbolic name.
2811 * @param {string} name Symbolic name of tab panel
2812 * @deprecated since v0.21.3, use `setTabPanel` instead
2814 OO
.ui
.IndexLayout
.prototype.setCard = function ( name
) {
2815 OO
.ui
.warnDeprecation( 'IndexLayout\'s setCard method is deprecated. Use setTabPanel instead. See T155152.' );
2816 return this.setTabPanel( name
);
2820 * Select the first selectable tab panel.
2824 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableTabPanel = function () {
2825 if ( !this.tabSelectWidget
.getSelectedItem() ) {
2826 this.tabSelectWidget
.selectItem( this.tabSelectWidget
.getFirstSelectableItem() );
2833 * Select the first selectable tab panel.
2836 * @deprecated since v0.21.3, use `selectFirstSelectableTabPanel` instead
2838 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableCard = function () {
2839 OO
.ui
.warnDeprecation( 'IndexLayout\'s selectFirstSelectableCard method is deprecated. Use selectFirestSelectableTabPanel instead. See T155152.' );
2840 return this.selectFirstSelectableTabPanel();
2844 * ToggleWidget implements basic behavior of widgets with an on/off state.
2845 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2849 * @extends OO.ui.Widget
2852 * @param {Object} [config] Configuration options
2853 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2854 * By default, the toggle is in the 'off' state.
2856 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
2857 // Configuration initialization
2858 config
= config
|| {};
2860 // Parent constructor
2861 OO
.ui
.ToggleWidget
.parent
.call( this, config
);
2867 this.$element
.addClass( 'oo-ui-toggleWidget' );
2868 this.setValue( !!config
.value
);
2873 OO
.inheritClass( OO
.ui
.ToggleWidget
, OO
.ui
.Widget
);
2880 * A change event is emitted when the on/off state of the toggle changes.
2882 * @param {boolean} value Value representing the new state of the toggle
2888 * Get the value representing the toggle’s state.
2890 * @return {boolean} The on/off state of the toggle
2892 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
2897 * Set the state of the toggle: `true` for 'on', `false` for 'off'.
2899 * @param {boolean} value The state of the toggle
2903 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
2905 if ( this.value
!== value
) {
2907 this.emit( 'change', value
);
2908 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
2909 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
2915 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
2916 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
2917 * configured with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators},
2918 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
2919 * and {@link OO.ui.mixin.LabelElement labels}. Please see
2920 * the [OOjs UI documentation][1] on MediaWiki for more information.
2923 * // Toggle buttons in the 'off' and 'on' state.
2924 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
2925 * label: 'Toggle Button off'
2927 * var toggleButton2 = new OO.ui.ToggleButtonWidget( {
2928 * label: 'Toggle Button on',
2931 * // Append the buttons to the DOM.
2932 * $( 'body' ).append( toggleButton1.$element, toggleButton2.$element );
2934 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Toggle_buttons
2937 * @extends OO.ui.ToggleWidget
2938 * @mixins OO.ui.mixin.ButtonElement
2939 * @mixins OO.ui.mixin.IconElement
2940 * @mixins OO.ui.mixin.IndicatorElement
2941 * @mixins OO.ui.mixin.LabelElement
2942 * @mixins OO.ui.mixin.TitledElement
2943 * @mixins OO.ui.mixin.FlaggedElement
2944 * @mixins OO.ui.mixin.TabIndexedElement
2947 * @param {Object} [config] Configuration options
2948 * @cfg {boolean} [value=false] The toggle button’s initial on/off
2949 * state. By default, the button is in the 'off' state.
2951 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
2952 // Configuration initialization
2953 config
= config
|| {};
2955 // Parent constructor
2956 OO
.ui
.ToggleButtonWidget
.parent
.call( this, config
);
2958 // Mixin constructors
2959 OO
.ui
.mixin
.ButtonElement
.call( this, $.extend( {}, config
, { active
: this.active
} ) );
2960 OO
.ui
.mixin
.IconElement
.call( this, config
);
2961 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
2962 OO
.ui
.mixin
.LabelElement
.call( this, config
);
2963 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
2964 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
2965 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
2968 this.connect( this, { click
: 'onAction' } );
2971 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
2973 .addClass( 'oo-ui-toggleButtonWidget' )
2974 .append( this.$button
);
2979 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
2980 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
2981 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IconElement
);
2982 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
2983 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.LabelElement
);
2984 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TitledElement
);
2985 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
2986 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2988 /* Static Properties */
2994 OO
.ui
.ToggleButtonWidget
.static.tagName
= 'span';
2999 * Handle the button action being triggered.
3003 OO
.ui
.ToggleButtonWidget
.prototype.onAction = function () {
3004 this.setValue( !this.value
);
3010 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
3012 if ( value
!== this.value
) {
3013 // Might be called from parent constructor before ButtonElement constructor
3014 if ( this.$button
) {
3015 this.$button
.attr( 'aria-pressed', value
.toString() );
3017 this.setActive( value
);
3021 OO
.ui
.ToggleButtonWidget
.parent
.prototype.setValue
.call( this, value
);
3029 OO
.ui
.ToggleButtonWidget
.prototype.setButtonElement = function ( $button
) {
3030 if ( this.$button
) {
3031 this.$button
.removeAttr( 'aria-pressed' );
3033 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement
.call( this, $button
);
3034 this.$button
.attr( 'aria-pressed', this.value
.toString() );
3038 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
3039 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
3040 * visually by a slider in the leftmost position.
3043 * // Toggle switches in the 'off' and 'on' position.
3044 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget();
3045 * var toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
3049 * // Create a FieldsetLayout to layout and label switches
3050 * var fieldset = new OO.ui.FieldsetLayout( {
3051 * label: 'Toggle switches'
3053 * fieldset.addItems( [
3054 * new OO.ui.FieldLayout( toggleSwitch1, { label: 'Off', align: 'top' } ),
3055 * new OO.ui.FieldLayout( toggleSwitch2, { label: 'On', align: 'top' } )
3057 * $( 'body' ).append( fieldset.$element );
3060 * @extends OO.ui.ToggleWidget
3061 * @mixins OO.ui.mixin.TabIndexedElement
3064 * @param {Object} [config] Configuration options
3065 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
3066 * By default, the toggle switch is in the 'off' position.
3068 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
3069 // Parent constructor
3070 OO
.ui
.ToggleSwitchWidget
.parent
.call( this, config
);
3072 // Mixin constructors
3073 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3076 this.dragging
= false;
3077 this.dragStart
= null;
3078 this.sliding
= false;
3079 this.$glow
= $( '<span>' );
3080 this.$grip
= $( '<span>' );
3084 click
: this.onClick
.bind( this ),
3085 keypress
: this.onKeyPress
.bind( this )
3089 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
3090 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
3092 .addClass( 'oo-ui-toggleSwitchWidget' )
3093 .attr( 'role', 'checkbox' )
3094 .append( this.$glow
, this.$grip
);
3099 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
3100 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3105 * Handle mouse click events.
3108 * @param {jQuery.Event} e Mouse click event
3110 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
3111 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
3112 this.setValue( !this.value
);
3118 * Handle key press events.
3121 * @param {jQuery.Event} e Key press event
3123 OO
.ui
.ToggleSwitchWidget
.prototype.onKeyPress = function ( e
) {
3124 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
3125 this.setValue( !this.value
);
3133 OO
.ui
.ToggleSwitchWidget
.prototype.setValue = function ( value
) {
3134 OO
.ui
.ToggleSwitchWidget
.parent
.prototype.setValue
.call( this, value
);
3135 this.$element
.attr( 'aria-checked', this.value
.toString() );
3140 * OutlineControlsWidget is a set of controls for an {@link OO.ui.OutlineSelectWidget outline select widget}.
3141 * Controls include moving items up and down, removing items, and adding different kinds of items.
3143 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3146 * @extends OO.ui.Widget
3147 * @mixins OO.ui.mixin.GroupElement
3148 * @mixins OO.ui.mixin.IconElement
3151 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
3152 * @param {Object} [config] Configuration options
3153 * @cfg {Object} [abilities] List of abilties
3154 * @cfg {boolean} [abilities.move=true] Allow moving movable items
3155 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
3157 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
3158 // Allow passing positional parameters inside the config object
3159 if ( OO
.isPlainObject( outline
) && config
=== undefined ) {
3161 outline
= config
.outline
;
3164 // Configuration initialization
3165 config
= $.extend( { icon
: 'add' }, config
);
3167 // Parent constructor
3168 OO
.ui
.OutlineControlsWidget
.parent
.call( this, config
);
3170 // Mixin constructors
3171 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3172 OO
.ui
.mixin
.IconElement
.call( this, config
);
3175 this.outline
= outline
;
3176 this.$movers
= $( '<div>' );
3177 this.upButton
= new OO
.ui
.ButtonWidget( {
3180 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
3182 this.downButton
= new OO
.ui
.ButtonWidget( {
3185 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
3187 this.removeButton
= new OO
.ui
.ButtonWidget( {
3190 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
3192 this.abilities
= { move: true, remove
: true };
3195 outline
.connect( this, {
3196 select
: 'onOutlineChange',
3197 add
: 'onOutlineChange',
3198 remove
: 'onOutlineChange'
3200 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
3201 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
3202 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
3205 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
3206 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
3208 .addClass( 'oo-ui-outlineControlsWidget-movers' )
3209 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
3210 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
3211 this.setAbilities( config
.abilities
|| {} );
3216 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
3217 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.GroupElement
);
3218 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.IconElement
);
3224 * @param {number} places Number of places to move
3236 * @param {Object} abilities List of abilties
3237 * @param {boolean} [abilities.move] Allow moving movable items
3238 * @param {boolean} [abilities.remove] Allow removing removable items
3240 OO
.ui
.OutlineControlsWidget
.prototype.setAbilities = function ( abilities
) {
3243 for ( ability
in this.abilities
) {
3244 if ( abilities
[ ability
] !== undefined ) {
3245 this.abilities
[ ability
] = !!abilities
[ ability
];
3249 this.onOutlineChange();
3253 * Handle outline change events.
3257 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
3258 var i
, len
, firstMovable
, lastMovable
,
3259 items
= this.outline
.getItems(),
3260 selectedItem
= this.outline
.getSelectedItem(),
3261 movable
= this.abilities
.move && selectedItem
&& selectedItem
.isMovable(),
3262 removable
= this.abilities
.remove
&& selectedItem
&& selectedItem
.isRemovable();
3267 while ( ++i
< len
) {
3268 if ( items
[ i
].isMovable() ) {
3269 firstMovable
= items
[ i
];
3275 if ( items
[ i
].isMovable() ) {
3276 lastMovable
= items
[ i
];
3281 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
3282 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
3283 this.removeButton
.setDisabled( !removable
);
3287 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3289 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3290 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3294 * @extends OO.ui.DecoratedOptionWidget
3297 * @param {Object} [config] Configuration options
3298 * @cfg {number} [level] Indentation level
3299 * @cfg {boolean} [movable] Allow modification from {@link OO.ui.OutlineControlsWidget outline controls}.
3301 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
3302 // Configuration initialization
3303 config
= config
|| {};
3305 // Parent constructor
3306 OO
.ui
.OutlineOptionWidget
.parent
.call( this, config
);
3310 this.movable
= !!config
.movable
;
3311 this.removable
= !!config
.removable
;
3314 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
3315 this.setLevel( config
.level
);
3320 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3322 /* Static Properties */
3328 OO
.ui
.OutlineOptionWidget
.static.highlightable
= true;
3334 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
3339 * @property {string}
3341 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
3346 * @property {number}
3348 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
3353 * Check if item is movable.
3355 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3357 * @return {boolean} Item is movable
3359 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
3360 return this.movable
;
3364 * Check if item is removable.
3366 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3368 * @return {boolean} Item is removable
3370 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
3371 return this.removable
;
3375 * Get indentation level.
3377 * @return {number} Indentation level
3379 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
3386 OO
.ui
.OutlineOptionWidget
.prototype.setPressed = function ( state
) {
3387 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setPressed
.call( this, state
);
3388 if ( this.pressed
) {
3389 this.setFlags( { progressive
: true } );
3390 } else if ( !this.selected
) {
3391 this.setFlags( { progressive
: false } );
3399 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3401 * @param {boolean} movable Item is movable
3404 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
3405 this.movable
= !!movable
;
3406 this.updateThemeClasses();
3413 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3415 * @param {boolean} removable Item is removable
3418 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
3419 this.removable
= !!removable
;
3420 this.updateThemeClasses();
3427 OO
.ui
.OutlineOptionWidget
.prototype.setSelected = function ( state
) {
3428 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3429 if ( this.selected
) {
3430 this.setFlags( { progressive
: true } );
3432 this.setFlags( { progressive
: false } );
3438 * Set indentation level.
3440 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3443 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
3444 var levels
= this.constructor.static.levels
,
3445 levelClass
= this.constructor.static.levelClass
,
3448 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
3450 if ( this.level
=== i
) {
3451 this.$element
.addClass( levelClass
+ i
);
3453 this.$element
.removeClass( levelClass
+ i
);
3456 this.updateThemeClasses();
3462 * OutlineSelectWidget is a structured list that contains {@link OO.ui.OutlineOptionWidget outline options}
3463 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls} widget.
3465 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3468 * @extends OO.ui.SelectWidget
3469 * @mixins OO.ui.mixin.TabIndexedElement
3472 * @param {Object} [config] Configuration options
3474 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
3475 // Parent constructor
3476 OO
.ui
.OutlineSelectWidget
.parent
.call( this, config
);
3478 // Mixin constructors
3479 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3483 focus
: this.bindKeyDownListener
.bind( this ),
3484 blur
: this.unbindKeyDownListener
.bind( this )
3488 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
3493 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
3494 OO
.mixinClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3497 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3498 * can be selected and configured with data. The class is
3499 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3500 * [OOjs UI documentation on MediaWiki] [1] for more information.
3502 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Button_selects_and_options
3505 * @extends OO.ui.OptionWidget
3506 * @mixins OO.ui.mixin.ButtonElement
3507 * @mixins OO.ui.mixin.IconElement
3508 * @mixins OO.ui.mixin.IndicatorElement
3509 * @mixins OO.ui.mixin.TitledElement
3512 * @param {Object} [config] Configuration options
3514 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
3515 // Configuration initialization
3516 config
= config
|| {};
3518 // Parent constructor
3519 OO
.ui
.ButtonOptionWidget
.parent
.call( this, config
);
3521 // Mixin constructors
3522 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3523 OO
.ui
.mixin
.IconElement
.call( this, config
);
3524 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3525 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
3528 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
3529 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3530 this.$element
.append( this.$button
);
3535 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
3536 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.ButtonElement
);
3537 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IconElement
);
3538 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IndicatorElement
);
3539 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.TitledElement
);
3541 /* Static Properties */
3544 * Allow button mouse down events to pass through so they can be handled by the parent select widget
3549 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
3555 OO
.ui
.ButtonOptionWidget
.static.highlightable
= false;
3562 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
3563 OO
.ui
.ButtonOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3565 if ( this.constructor.static.selectable
) {
3566 this.setActive( state
);
3573 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3574 * button options and is used together with
3575 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3576 * highlighting, choosing, and selecting mutually exclusive options. Please see
3577 * the [OOjs UI documentation on MediaWiki] [1] for more information.
3580 * // Example: A ButtonSelectWidget that contains three ButtonOptionWidgets
3581 * var option1 = new OO.ui.ButtonOptionWidget( {
3583 * label: 'Option 1',
3584 * title: 'Button option 1'
3587 * var option2 = new OO.ui.ButtonOptionWidget( {
3589 * label: 'Option 2',
3590 * title: 'Button option 2'
3593 * var option3 = new OO.ui.ButtonOptionWidget( {
3595 * label: 'Option 3',
3596 * title: 'Button option 3'
3599 * var buttonSelect=new OO.ui.ButtonSelectWidget( {
3600 * items: [ option1, option2, option3 ]
3602 * $( 'body' ).append( buttonSelect.$element );
3604 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
3607 * @extends OO.ui.SelectWidget
3608 * @mixins OO.ui.mixin.TabIndexedElement
3611 * @param {Object} [config] Configuration options
3613 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
3614 // Parent constructor
3615 OO
.ui
.ButtonSelectWidget
.parent
.call( this, config
);
3617 // Mixin constructors
3618 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3622 focus
: this.bindKeyDownListener
.bind( this ),
3623 blur
: this.unbindKeyDownListener
.bind( this )
3627 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
3632 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
3633 OO
.mixinClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3636 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3638 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3639 * {@link OO.ui.TabPanelLayout tab panel layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3643 * @extends OO.ui.OptionWidget
3646 * @param {Object} [config] Configuration options
3648 OO
.ui
.TabOptionWidget
= function OoUiTabOptionWidget( config
) {
3649 // Configuration initialization
3650 config
= config
|| {};
3652 // Parent constructor
3653 OO
.ui
.TabOptionWidget
.parent
.call( this, config
);
3656 this.$element
.addClass( 'oo-ui-tabOptionWidget' );
3661 OO
.inheritClass( OO
.ui
.TabOptionWidget
, OO
.ui
.OptionWidget
);
3663 /* Static Properties */
3669 OO
.ui
.TabOptionWidget
.static.highlightable
= false;
3672 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3674 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3677 * @extends OO.ui.SelectWidget
3678 * @mixins OO.ui.mixin.TabIndexedElement
3681 * @param {Object} [config] Configuration options
3683 OO
.ui
.TabSelectWidget
= function OoUiTabSelectWidget( config
) {
3684 // Parent constructor
3685 OO
.ui
.TabSelectWidget
.parent
.call( this, config
);
3687 // Mixin constructors
3688 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3692 focus
: this.bindKeyDownListener
.bind( this ),
3693 blur
: this.unbindKeyDownListener
.bind( this )
3697 this.$element
.addClass( 'oo-ui-tabSelectWidget' );
3702 OO
.inheritClass( OO
.ui
.TabSelectWidget
, OO
.ui
.SelectWidget
);
3703 OO
.mixinClass( OO
.ui
.TabSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3706 * CapsuleItemWidgets are used within a {@link OO.ui.CapsuleMultiselectWidget
3707 * CapsuleMultiselectWidget} to display the selected items.
3710 * @extends OO.ui.Widget
3711 * @mixins OO.ui.mixin.ItemWidget
3712 * @mixins OO.ui.mixin.LabelElement
3713 * @mixins OO.ui.mixin.FlaggedElement
3714 * @mixins OO.ui.mixin.TabIndexedElement
3717 * @param {Object} [config] Configuration options
3719 OO
.ui
.CapsuleItemWidget
= function OoUiCapsuleItemWidget( config
) {
3720 // Configuration initialization
3721 config
= config
|| {};
3723 // Parent constructor
3724 OO
.ui
.CapsuleItemWidget
.parent
.call( this, config
);
3726 // Mixin constructors
3727 OO
.ui
.mixin
.ItemWidget
.call( this );
3728 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3729 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3730 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3733 this.closeButton
= new OO
.ui
.ButtonWidget( {
3737 } ).on( 'click', this.onCloseClick
.bind( this ) );
3739 this.on( 'disable', function ( disabled
) {
3740 this.closeButton
.setDisabled( disabled
);
3746 click
: this.onClick
.bind( this ),
3747 keydown
: this.onKeyDown
.bind( this )
3749 .addClass( 'oo-ui-capsuleItemWidget' )
3750 .append( this.$label
, this.closeButton
.$element
);
3755 OO
.inheritClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.Widget
);
3756 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.ItemWidget
);
3757 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.LabelElement
);
3758 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
3759 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3764 * Handle close icon clicks
3766 OO
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
3767 var element
= this.getElementGroup();
3769 if ( element
&& $.isFunction( element
.removeItems
) ) {
3770 element
.removeItems( [ this ] );
3776 * Handle click event for the entire capsule
3778 OO
.ui
.CapsuleItemWidget
.prototype.onClick = function () {
3779 var element
= this.getElementGroup();
3781 if ( !this.isDisabled() && element
&& $.isFunction( element
.editItem
) ) {
3782 element
.editItem( this );
3787 * Handle keyDown event for the entire capsule
3789 * @param {jQuery.Event} e Key down event
3791 OO
.ui
.CapsuleItemWidget
.prototype.onKeyDown = function ( e
) {
3792 var element
= this.getElementGroup();
3794 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
) {
3795 element
.removeItems( [ this ] );
3798 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
3799 element
.editItem( this );
3801 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
3802 element
.getPreviousItem( this ).focus();
3803 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
3804 element
.getNextItem( this ).focus();
3809 * CapsuleMultiselectWidgets are something like a {@link OO.ui.ComboBoxInputWidget combo box widget}
3810 * that allows for selecting multiple values.
3812 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
3815 * // Example: A CapsuleMultiselectWidget.
3816 * var capsule = new OO.ui.CapsuleMultiselectWidget( {
3817 * label: 'CapsuleMultiselectWidget',
3818 * selected: [ 'Option 1', 'Option 3' ],
3821 * new OO.ui.MenuOptionWidget( {
3823 * label: 'Option One'
3825 * new OO.ui.MenuOptionWidget( {
3827 * label: 'Option Two'
3829 * new OO.ui.MenuOptionWidget( {
3831 * label: 'Option Three'
3833 * new OO.ui.MenuOptionWidget( {
3835 * label: 'Option Four'
3837 * new OO.ui.MenuOptionWidget( {
3839 * label: 'Option Five'
3844 * $( 'body' ).append( capsule.$element );
3846 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
3849 * @extends OO.ui.Widget
3850 * @mixins OO.ui.mixin.GroupElement
3851 * @mixins OO.ui.mixin.PopupElement
3852 * @mixins OO.ui.mixin.TabIndexedElement
3853 * @mixins OO.ui.mixin.IndicatorElement
3854 * @mixins OO.ui.mixin.IconElement
3855 * @uses OO.ui.CapsuleItemWidget
3856 * @uses OO.ui.MenuSelectWidget
3859 * @param {Object} [config] Configuration options
3860 * @cfg {string} [placeholder] Placeholder text
3861 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if not present in the menu.
3862 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added.
3863 * @cfg {Object} [menu] (required) Configuration options to pass to the
3864 * {@link OO.ui.MenuSelectWidget menu select widget}.
3865 * @cfg {Object} [popup] Configuration options to pass to the {@link OO.ui.PopupWidget popup widget}.
3866 * If specified, this popup will be shown instead of the menu (but the menu
3867 * will still be used for item labels and allowArbitrary=false). The widgets
3868 * in the popup should use {@link #addItemsFromData} or {@link #addItems} as necessary.
3869 * @cfg {jQuery} [$overlay=this.$element] Render the menu or popup into a separate layer.
3870 * This configuration is useful in cases where the expanded menu is larger than
3871 * its containing `<div>`. The specified overlay layer is usually on top of
3872 * the containing `<div>` and has a larger area. By default, the menu uses
3873 * relative positioning.
3874 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
3876 OO
.ui
.CapsuleMultiselectWidget
= function OoUiCapsuleMultiselectWidget( config
) {
3879 // Parent constructor
3880 OO
.ui
.CapsuleMultiselectWidget
.parent
.call( this, config
);
3882 // Configuration initialization
3883 config
= $.extend( {
3884 allowArbitrary
: false,
3885 allowDuplicates
: false,
3886 $overlay
: this.$element
3889 // Properties (must be set before mixin constructor calls)
3890 this.$handle
= $( '<div>' );
3891 this.$input
= config
.popup
? null : $( '<input>' );
3892 if ( config
.placeholder
!== undefined && config
.placeholder
!== '' ) {
3893 this.$input
.attr( 'placeholder', config
.placeholder
);
3896 // Mixin constructors
3897 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3898 if ( config
.popup
) {
3899 config
.popup
= $.extend( {}, config
.popup
, {
3903 OO
.ui
.mixin
.PopupElement
.call( this, config
);
3904 $tabFocus
= $( '<span>' );
3905 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: $tabFocus
} ) );
3909 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
3911 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3912 OO
.ui
.mixin
.IconElement
.call( this, config
);
3915 this.$content
= $( '<div>' );
3916 this.allowArbitrary
= config
.allowArbitrary
;
3917 this.allowDuplicates
= config
.allowDuplicates
;
3918 this.$overlay
= config
.$overlay
;
3919 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend(
3922 $input
: this.$input
,
3923 $floatableContainer
: this.$element
,
3924 filterFromInput
: true,
3925 disabled
: this.isDisabled()
3933 focus
: this.focus
.bind( this )
3935 this.popup
.$element
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3936 if ( this.popup
.$autoCloseIgnore
) {
3937 this.popup
.$autoCloseIgnore
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3939 this.popup
.connect( this, {
3940 toggle: function ( visible
) {
3941 $tabFocus
.toggle( !visible
);
3946 focus
: this.onInputFocus
.bind( this ),
3947 blur
: this.onInputBlur
.bind( this ),
3948 'propertychange change click mouseup keydown keyup input cut paste select focus':
3949 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
3950 keydown
: this.onKeyDown
.bind( this ),
3951 keypress
: this.onKeyPress
.bind( this )
3954 this.menu
.connect( this, {
3955 choose
: 'onMenuChoose',
3956 toggle
: 'onMenuToggle',
3957 add
: 'onMenuItemsChange',
3958 remove
: 'onMenuItemsChange'
3961 mousedown
: this.onMouseDown
.bind( this )
3965 if ( this.$input
) {
3966 this.$input
.prop( 'disabled', this.isDisabled() );
3969 'aria-owns': this.menu
.getElementId(),
3970 'aria-autocomplete': 'list'
3973 if ( config
.data
) {
3974 this.setItemsFromData( config
.data
);
3976 this.$content
.addClass( 'oo-ui-capsuleMultiselectWidget-content' )
3977 .append( this.$group
);
3978 this.$group
.addClass( 'oo-ui-capsuleMultiselectWidget-group' );
3979 this.$handle
.addClass( 'oo-ui-capsuleMultiselectWidget-handle' )
3980 .append( this.$indicator
, this.$icon
, this.$content
);
3981 this.$element
.addClass( 'oo-ui-capsuleMultiselectWidget' )
3982 .append( this.$handle
);
3984 this.popup
.$element
.addClass( 'oo-ui-capsuleMultiselectWidget-popup' );
3985 this.$content
.append( $tabFocus
);
3986 this.$overlay
.append( this.popup
.$element
);
3988 this.$content
.append( this.$input
);
3989 this.$overlay
.append( this.menu
.$element
);
3992 $tabFocus
.addClass( 'oo-ui-capsuleMultiselectWidget-focusTrap' );
3995 // Input size needs to be calculated after everything else is rendered
3996 setTimeout( function () {
3997 if ( this.$input
) {
3998 this.updateInputSize();
4002 this.onMenuItemsChange();
4007 OO
.inheritClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.Widget
);
4008 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.GroupElement
);
4009 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
4010 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4011 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
4012 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
4019 * A change event is emitted when the set of selected items changes.
4021 * @param {Mixed[]} datas Data of the now-selected items
4027 * A resize event is emitted when the widget's dimensions change to accomodate newly added items or
4028 * current user input.
4034 * Construct a OO.ui.CapsuleItemWidget (or a subclass thereof) from given label and data.
4035 * May return `null` if the given label and data are not valid.
4038 * @param {Mixed} data Custom data of any type.
4039 * @param {string} label The label text.
4040 * @return {OO.ui.CapsuleItemWidget|null}
4042 OO
.ui
.CapsuleMultiselectWidget
.prototype.createItemWidget = function ( data
, label
) {
4043 if ( label
=== '' ) {
4046 return new OO
.ui
.CapsuleItemWidget( { data
: data
, label
: label
} );
4052 OO
.ui
.CapsuleMultiselectWidget
.prototype.getInputId = function () {
4053 if ( !this.$input
) {
4056 return OO
.ui
.mixin
.TabIndexedElement
.prototype.getInputId
.call( this );
4060 * Get the data of the items in the capsule
4064 OO
.ui
.CapsuleMultiselectWidget
.prototype.getItemsData = function () {
4065 return this.getItems().map( function ( item
) {
4071 * Set the items in the capsule by providing data
4074 * @param {Mixed[]} datas
4075 * @return {OO.ui.CapsuleMultiselectWidget}
4077 OO
.ui
.CapsuleMultiselectWidget
.prototype.setItemsFromData = function ( datas
) {
4080 items
= this.getItems();
4082 $.each( datas
, function ( i
, data
) {
4084 item
= menu
.getItemFromData( data
);
4088 } else if ( widget
.allowArbitrary
) {
4089 label
= String( data
);
4095 for ( j
= 0; j
< items
.length
; j
++ ) {
4096 if ( items
[ j
].data
=== data
&& items
[ j
].label
=== label
) {
4098 items
.splice( j
, 1 );
4103 item
= widget
.createItemWidget( data
, label
);
4106 widget
.addItems( [ item
], i
);
4110 if ( items
.length
) {
4111 widget
.removeItems( items
);
4118 * Add items to the capsule by providing their data
4121 * @param {Mixed[]} datas
4122 * @return {OO.ui.CapsuleMultiselectWidget}
4124 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemsFromData = function ( datas
) {
4129 $.each( datas
, function ( i
, data
) {
4132 if ( !widget
.getItemFromData( data
) || widget
.allowDuplicates
) {
4133 item
= menu
.getItemFromData( data
);
4135 item
= widget
.createItemWidget( data
, item
.label
);
4136 } else if ( widget
.allowArbitrary
) {
4137 item
= widget
.createItemWidget( data
, String( data
) );
4145 if ( items
.length
) {
4146 this.addItems( items
);
4153 * Add items to the capsule by providing a label
4155 * @param {string} label
4156 * @return {boolean} Whether the item was added or not
4158 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemFromLabel = function ( label
) {
4160 item
= this.menu
.getItemFromLabel( label
, true );
4162 this.addItemsFromData( [ item
.data
] );
4164 } else if ( this.allowArbitrary
) {
4165 items
= this.getItems();
4166 this.addItemsFromData( [ label
] );
4167 return !OO
.compare( this.getItems(), items
);
4173 * Remove items by data
4176 * @param {Mixed[]} datas
4177 * @return {OO.ui.CapsuleMultiselectWidget}
4179 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItemsFromData = function ( datas
) {
4183 $.each( datas
, function ( i
, data
) {
4184 var item
= widget
.getItemFromData( data
);
4190 if ( items
.length
) {
4191 this.removeItems( items
);
4200 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItems = function ( items
) {
4202 oldItems
= this.items
.slice();
4204 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
);
4206 if ( this.items
.length
!== oldItems
.length
) {
4210 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
4211 same
= same
&& this.items
[ i
] === oldItems
[ i
];
4215 this.emit( 'change', this.getItemsData() );
4216 this.updateInputSize();
4223 * Removes the item from the list and copies its label to `this.$input`.
4225 * @param {Object} item
4227 OO
.ui
.CapsuleMultiselectWidget
.prototype.editItem = function ( item
) {
4228 this.addItemFromLabel( this.$input
.val() );
4230 this.$input
.val( item
.label
);
4231 this.updateInputSize();
4233 this.menu
.updateItemVisibility(); // Hack, we shouldn't be calling this method directly
4234 this.removeItems( [ item
] );
4240 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItems = function ( items
) {
4242 oldItems
= this.items
.slice();
4244 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
4246 if ( this.items
.length
!== oldItems
.length
) {
4250 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
4251 same
= same
&& this.items
[ i
] === oldItems
[ i
];
4255 this.emit( 'change', this.getItemsData() );
4256 this.updateInputSize();
4265 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearItems = function () {
4266 if ( this.items
.length
) {
4267 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
4268 this.emit( 'change', this.getItemsData() );
4269 this.updateInputSize();
4275 * Given an item, returns the item after it. If its the last item,
4276 * returns `this.$input`. If no item is passed, returns the very first
4279 * @param {OO.ui.CapsuleItemWidget} [item]
4280 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4282 OO
.ui
.CapsuleMultiselectWidget
.prototype.getNextItem = function ( item
) {
4285 if ( item
=== undefined ) {
4286 return this.items
[ 0 ];
4289 itemIndex
= this.items
.indexOf( item
);
4290 if ( itemIndex
< 0 ) { // Item not in list
4292 } else if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
4295 return this.items
[ itemIndex
+ 1 ];
4300 * Given an item, returns the item before it. If its the first item,
4301 * returns `this.$input`. If no item is passed, returns the very last
4304 * @param {OO.ui.CapsuleItemWidget} [item]
4305 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4307 OO
.ui
.CapsuleMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
4310 if ( item
=== undefined ) {
4311 return this.items
[ this.items
.length
- 1 ];
4314 itemIndex
= this.items
.indexOf( item
);
4315 if ( itemIndex
< 0 ) { // Item not in list
4317 } else if ( itemIndex
=== 0 ) { // First item
4320 return this.items
[ itemIndex
- 1 ];
4325 * Get the capsule widget's menu.
4327 * @return {OO.ui.MenuSelectWidget} Menu widget
4329 OO
.ui
.CapsuleMultiselectWidget
.prototype.getMenu = function () {
4334 * Handle focus events
4337 * @param {jQuery.Event} event
4339 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputFocus = function () {
4340 if ( !this.isDisabled() ) {
4341 this.updateInputSize();
4342 this.menu
.toggle( true );
4347 * Handle blur events
4350 * @param {jQuery.Event} event
4352 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputBlur = function () {
4353 this.addItemFromLabel( this.$input
.val() );
4358 * Handles popup focus out events.
4361 * @param {jQuery.Event} e Focus out event
4363 OO
.ui
.CapsuleMultiselectWidget
.prototype.onPopupFocusOut = function () {
4364 var widget
= this.popup
;
4366 setTimeout( function () {
4368 widget
.isVisible() &&
4369 !OO
.ui
.contains( widget
.$element
.add( widget
.$autoCloseIgnore
).get(), document
.activeElement
, true )
4371 widget
.toggle( false );
4377 * Handle mouse down events.
4380 * @param {jQuery.Event} e Mouse down event
4382 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMouseDown = function ( e
) {
4383 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
4387 this.updateInputSize();
4392 * Handle key press events.
4395 * @param {jQuery.Event} e Key press event
4397 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyPress = function ( e
) {
4398 if ( !this.isDisabled() ) {
4399 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
4404 if ( !this.popup
) {
4405 this.menu
.toggle( true );
4406 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
4407 if ( this.addItemFromLabel( this.$input
.val() ) ) {
4413 // Make sure the input gets resized.
4414 setTimeout( this.updateInputSize
.bind( this ), 0 );
4420 * Handle key down events.
4423 * @param {jQuery.Event} e Key down event
4425 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyDown = function ( e
) {
4427 !this.isDisabled() &&
4428 this.$input
.val() === '' &&
4431 // 'keypress' event is not triggered for Backspace
4432 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
4433 if ( e
.metaKey
|| e
.ctrlKey
) {
4434 this.removeItems( this.items
.slice( -1 ) );
4436 this.editItem( this.items
[ this.items
.length
- 1 ] );
4439 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
4440 this.getPreviousItem().focus();
4441 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
4442 this.getNextItem().focus();
4448 * Update the dimensions of the text input field to encompass all available area.
4451 * @param {jQuery.Event} e Event of some sort
4453 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateInputSize = function () {
4454 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
4455 if ( this.$input
&& !this.isDisabled() ) {
4456 this.$input
.css( 'width', '1em' );
4457 $lastItem
= this.$group
.children().last();
4458 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
4460 // Get the width of the input with the placeholder text as
4461 // the value and save it so that we don't keep recalculating
4463 this.contentWidthWithPlaceholder
=== undefined &&
4464 this.$input
.val() === '' &&
4465 this.$input
.attr( 'placeholder' ) !== undefined
4467 this.$input
.val( this.$input
.attr( 'placeholder' ) );
4468 this.contentWidthWithPlaceholder
= this.$input
[ 0 ].scrollWidth
;
4469 this.$input
.val( '' );
4473 // Always keep the input wide enough for the placeholder text
4474 contentWidth
= Math
.max(
4475 this.$input
[ 0 ].scrollWidth
,
4476 // undefined arguments in Math.max lead to NaN
4477 ( this.contentWidthWithPlaceholder
=== undefined ) ?
4478 0 : this.contentWidthWithPlaceholder
4480 currentWidth
= this.$input
.width();
4482 if ( contentWidth
< currentWidth
) {
4483 this.updateIfHeightChanged();
4484 // All is fine, don't perform expensive calculations
4488 if ( $lastItem
.length
=== 0 ) {
4489 bestWidth
= this.$content
.innerWidth();
4491 bestWidth
= direction
=== 'ltr' ?
4492 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
4493 $lastItem
.position().left
;
4496 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
4497 // pixels this is off by are coming from.
4499 if ( contentWidth
> bestWidth
) {
4500 // This will result in the input getting shifted to the next line
4501 bestWidth
= this.$content
.innerWidth() - 10;
4503 this.$input
.width( Math
.floor( bestWidth
) );
4504 this.updateIfHeightChanged();
4506 this.updateIfHeightChanged();
4511 * Determine if widget height changed, and if so, update menu position and emit 'resize' event.
4515 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateIfHeightChanged = function () {
4516 var height
= this.$element
.height();
4517 if ( height
!== this.height
) {
4518 this.height
= height
;
4519 this.menu
.position();
4521 this.popup
.updateDimensions();
4523 this.emit( 'resize' );
4528 * Handle menu choose events.
4531 * @param {OO.ui.OptionWidget} item Chosen item
4533 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
4534 if ( item
&& item
.isVisible() ) {
4535 this.addItemsFromData( [ item
.getData() ] );
4541 * Handle menu toggle events.
4544 * @param {boolean} isVisible Menu toggle event
4546 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
4547 this.$element
.toggleClass( 'oo-ui-capsuleMultiselectWidget-open', isVisible
);
4551 * Handle menu item change events.
4555 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuItemsChange = function () {
4556 this.setItemsFromData( this.getItemsData() );
4557 this.$element
.toggleClass( 'oo-ui-capsuleMultiselectWidget-empty', this.menu
.isEmpty() );
4561 * Clear the input field
4565 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearInput = function () {
4566 if ( this.$input
) {
4567 this.$input
.val( '' );
4568 this.updateInputSize();
4571 this.popup
.toggle( false );
4573 this.menu
.toggle( false );
4574 this.menu
.selectItem();
4575 this.menu
.highlightItem();
4581 OO
.ui
.CapsuleMultiselectWidget
.prototype.setDisabled = function ( disabled
) {
4585 OO
.ui
.CapsuleMultiselectWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
4587 if ( this.$input
) {
4588 this.$input
.prop( 'disabled', this.isDisabled() );
4591 this.menu
.setDisabled( this.isDisabled() );
4594 this.popup
.setDisabled( this.isDisabled() );
4598 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4599 this.items
[ i
].updateDisabled();
4611 OO
.ui
.CapsuleMultiselectWidget
.prototype.focus = function () {
4612 if ( !this.isDisabled() ) {
4614 this.popup
.setSize( this.$handle
.outerWidth() );
4615 this.popup
.toggle( true );
4616 OO
.ui
.findFocusable( this.popup
.$element
).focus();
4618 OO
.ui
.mixin
.TabIndexedElement
.prototype.focus
.call( this );
4625 * TagItemWidgets are used within a {@link OO.ui.TagMultiselectWidget
4626 * TagMultiselectWidget} to display the selected items.
4629 * @extends OO.ui.Widget
4630 * @mixins OO.ui.mixin.ItemWidget
4631 * @mixins OO.ui.mixin.LabelElement
4632 * @mixins OO.ui.mixin.FlaggedElement
4633 * @mixins OO.ui.mixin.TabIndexedElement
4634 * @mixins OO.ui.mixin.DraggableElement
4637 * @param {Object} [config] Configuration object
4638 * @cfg {boolean} [valid=true] Item is valid
4640 OO
.ui
.TagItemWidget
= function OoUiTagItemWidget( config
) {
4641 config
= config
|| {};
4643 // Parent constructor
4644 OO
.ui
.TagItemWidget
.parent
.call( this, config
);
4646 // Mixin constructors
4647 OO
.ui
.mixin
.ItemWidget
.call( this );
4648 OO
.ui
.mixin
.LabelElement
.call( this, config
);
4649 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4650 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4651 OO
.ui
.mixin
.DraggableElement
.call( this, config
);
4653 this.valid
= config
.valid
=== undefined ? true : !!config
.valid
;
4655 this.closeButton
= new OO
.ui
.ButtonWidget( {
4660 this.closeButton
.setDisabled( this.isDisabled() );
4664 .connect( this, { click
: 'remove' } );
4666 .on( 'click', this.select
.bind( this ) )
4667 .on( 'keydown', this.onKeyDown
.bind( this ) )
4668 // Prevent propagation of mousedown; the tag item "lives" in the
4669 // clickable area of the TagMultiselectWidget, which listens to
4670 // mousedown to open the menu or popup. We want to prevent that
4671 // for clicks specifically on the tag itself, so the actions taken
4672 // are more deliberate. When the tag is clicked, it will emit the
4673 // selection event (similar to how #OO.ui.MultioptionWidget emits 'change')
4674 // and can be handled separately.
4675 .on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
4679 .addClass( 'oo-ui-tagItemWidget' )
4680 .append( this.$label
, this.closeButton
.$element
);
4683 /* Initialization */
4685 OO
.inheritClass( OO
.ui
.TagItemWidget
, OO
.ui
.Widget
);
4686 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.ItemWidget
);
4687 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.LabelElement
);
4688 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
4689 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4690 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.DraggableElement
);
4697 * A remove action was performed on the item
4702 * @param {string} direction Direction of the movement, forward or backwards
4704 * A navigate action was performed on the item
4710 * The tag widget was selected. This can occur when the widget
4711 * is either clicked or enter was pressed on it.
4716 * @param {boolean} isValid Item is valid
4718 * Item validity has changed
4726 OO
.ui
.TagItemWidget
.prototype.setDisabled = function ( state
) {
4728 OO
.ui
.TagItemWidget
.parent
.prototype.setDisabled
.call( this, state
);
4730 if ( this.closeButton
) {
4731 this.closeButton
.setDisabled( state
);
4737 * Handle removal of the item
4739 * This is mainly for extensibility concerns, so other children
4740 * of this class can change the behavior if they need to. This
4741 * is called by both clicking the 'remove' button but also
4742 * on keypress, which is harder to override if needed.
4746 OO
.ui
.TagItemWidget
.prototype.remove = function () {
4747 if ( !this.isDisabled() ) {
4748 this.emit( 'remove' );
4753 * Handle a keydown event on the widget
4757 * @param {jQuery.Event} e Key down event
4758 * @return {boolean|undefined} false to stop the operation
4760 OO
.ui
.TagItemWidget
.prototype.onKeyDown = function ( e
) {
4763 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
) {
4766 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
4770 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4771 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4773 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
4787 e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
4788 movement
.left
: movement
.right
4798 OO
.ui
.TagItemWidget
.prototype.select = function () {
4799 if ( !this.isDisabled() ) {
4800 this.emit( 'select' );
4805 * Set the valid state of this item
4807 * @param {boolean} [valid] Item is valid
4810 OO
.ui
.TagItemWidget
.prototype.toggleValid = function ( valid
) {
4811 valid
= valid
=== undefined ? !this.valid
: !!valid
;
4813 if ( this.valid
!== valid
) {
4816 this.setFlags( { invalid
: !this.valid
} );
4818 this.emit( 'valid', this.valid
);
4823 * Check whether the item is valid
4825 * @return {boolean} Item is valid
4827 OO
.ui
.TagItemWidget
.prototype.isValid = function () {
4832 * A basic tag multiselect widget, similar in concept to {@link OO.ui.ComboBoxInputWidget combo box widget}
4833 * that allows the user to add multiple values that are displayed in a tag area.
4835 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
4837 * This widget is a base widget; see {@link OO.ui.MenuTagMultiselectWidget MenuTagMultiselectWidget} and
4838 * {@link OO.ui.PopupTagMultiselectWidget PopupTagMultiselectWidget} for the implementations that use
4839 * a menu and a popup respectively.
4842 * // Example: A basic TagMultiselectWidget.
4843 * var widget = new OO.ui.TagMultiselectWidget( {
4844 * inputPosition: 'outline',
4845 * allowedValues: [ 'Option 1', 'Option 2', 'Option 3' ],
4846 * selected: [ 'Option 1' ]
4848 * $( 'body' ).append( widget.$element );
4850 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
4853 * @extends OO.ui.Widget
4854 * @mixins OO.ui.mixin.GroupWidget
4855 * @mixins OO.ui.mixin.DraggableGroupElement
4856 * @mixins OO.ui.mixin.IndicatorElement
4857 * @mixins OO.ui.mixin.IconElement
4858 * @mixins OO.ui.mixin.TabIndexedElement
4859 * @mixins OO.ui.mixin.FlaggedElement
4862 * @param {Object} config Configuration object
4863 * @cfg {Object} [input] Configuration options for the input widget
4864 * @cfg {OO.ui.InputWidget} [inputWidget] An optional input widget. If given, it will
4865 * replace the input widget used in the TagMultiselectWidget. If not given,
4866 * TagMultiselectWidget creates its own.
4867 * @cfg {boolean} [inputPosition='inline'] Position of the input. Options are:
4868 * - inline: The input is invisible, but exists inside the tag list, so
4869 * the user types into the tag groups to add tags.
4870 * - outline: The input is underneath the tag area.
4871 * - none: No input supplied
4872 * @cfg {boolean} [allowEditTags=true] Allow editing of the tags by clicking them
4873 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if
4874 * not present in the menu.
4875 * @cfg {Object[]} [allowedValues] An array representing the allowed items
4877 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added
4878 * @cfg {boolean} [allowDisplayInvalidTags=false] Allow the display of
4879 * invalid tags. These tags will display with an invalid state, and
4880 * the widget as a whole will have an invalid state if any invalid tags
4882 * @cfg {boolean} [allowReordering=true] Allow reordering of the items
4883 * @cfg {Object[]|String[]} [selected] A set of selected tags. If given,
4884 * these will appear in the tag list on initialization, as long as they
4885 * pass the validity tests.
4887 OO
.ui
.TagMultiselectWidget
= function OoUiTagMultiselectWidget( config
) {
4889 rAF
= window
.requestAnimationFrame
|| setTimeout
,
4891 $tabFocus
= $( '<span>' )
4892 .addClass( 'oo-ui-tagMultiselectWidget-focusTrap' );
4894 config
= config
|| {};
4896 // Parent constructor
4897 OO
.ui
.TagMultiselectWidget
.parent
.call( this, config
);
4899 // Mixin constructors
4900 OO
.ui
.mixin
.GroupWidget
.call( this, config
);
4901 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
4902 OO
.ui
.mixin
.IconElement
.call( this, config
);
4903 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4904 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4905 OO
.ui
.mixin
.DraggableGroupElement
.call( this, config
);
4907 this.toggleDraggable(
4908 config
.allowReordering
=== undefined ?
4909 true : !!config
.allowReordering
4912 this.inputPosition
= this.constructor.static.allowedInputPositions
.indexOf( config
.inputPosition
) > -1 ?
4913 config
.inputPosition
: 'inline';
4914 this.allowEditTags
= config
.allowEditTags
=== undefined ? true : !!config
.allowEditTags
;
4915 this.allowArbitrary
= !!config
.allowArbitrary
;
4916 this.allowDuplicates
= !!config
.allowDuplicates
;
4917 this.allowedValues
= config
.allowedValues
|| [];
4918 this.allowDisplayInvalidTags
= config
.allowDisplayInvalidTags
;
4919 this.hasInput
= this.inputPosition
!== 'none';
4923 this.$content
= $( '<div>' )
4924 .addClass( 'oo-ui-tagMultiselectWidget-content' );
4925 this.$handle
= $( '<div>' )
4926 .addClass( 'oo-ui-tagMultiselectWidget-handle' )
4933 .addClass( 'oo-ui-tagMultiselectWidget-group' )
4939 remove
: 'itemRemove',
4940 navigate
: 'itemNavigate',
4941 select
: 'itemSelect'
4943 this.connect( this, {
4944 itemRemove
: 'onTagRemove',
4945 itemSelect
: 'onTagSelect',
4946 itemNavigate
: 'onTagNavigate',
4947 change
: 'onChangeTags'
4950 mousedown
: this.onMouseDown
.bind( this )
4955 .addClass( 'oo-ui-tagMultiselectWidget' )
4956 .append( this.$handle
);
4958 if ( this.hasInput
) {
4959 if ( config
.inputWidget
) {
4960 this.input
= config
.inputWidget
;
4962 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
4963 placeholder
: config
.placeholder
,
4964 classes
: [ 'oo-ui-tagMultiselectWidget-input' ]
4965 }, config
.input
) );
4967 this.input
.setDisabled( this.isDisabled() );
4970 focus
: this.onInputFocus
.bind( this ),
4971 blur
: this.onInputBlur
.bind( this ),
4972 'propertychange change click mouseup keydown keyup input cut paste select focus':
4973 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
4974 keydown
: this.onInputKeyDown
.bind( this ),
4975 keypress
: this.onInputKeyPress
.bind( this )
4978 this.input
.$input
.on( inputEvents
);
4980 if ( this.inputPosition
=== 'outline' ) {
4981 // Override max-height for the input widget
4982 // in the case the widget is outline so it can
4983 // stretch all the way if the widet is wide
4984 this.input
.$element
.css( 'max-width', 'inherit' );
4986 .addClass( 'oo-ui-tagMultiselectWidget-outlined' )
4987 .append( this.input
.$element
);
4989 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-inlined' );
4990 // HACK: When the widget is using 'inline' input, the
4991 // behavior needs to only use the $input itself
4992 // so we style and size it accordingly (otherwise
4993 // the styling and sizing can get very convoluted
4994 // when the wrapping divs and other elements)
4995 // We are taking advantage of still being able to
4996 // call the widget itself for operations like
4997 // .getValue() and setDisabled() and .focus() but
4998 // having only the $input attached to the DOM
4999 this.$content
.append( this.input
.$input
);
5002 this.$content
.append( $tabFocus
);
5005 this.setTabIndexedElement(
5011 if ( config
.selected
) {
5012 this.setValue( config
.selected
);
5015 // HACK: Input size needs to be calculated after everything
5018 if ( widget
.hasInput
) {
5019 widget
.updateInputSize();
5024 /* Initialization */
5026 OO
.inheritClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.Widget
);
5027 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.GroupWidget
);
5028 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.DraggableGroupElement
);
5029 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
5030 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
5031 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
5032 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.FlaggedElement
);
5034 /* Static properties */
5037 * Allowed input positions.
5038 * - inline: The input is inside the tag list
5039 * - outline: The input is under the tag list
5040 * - none: There is no input
5044 OO
.ui
.TagMultiselectWidget
.static.allowedInputPositions
= [ 'inline', 'outline', 'none' ];
5049 * Handle mouse down events.
5052 * @param {jQuery.Event} e Mouse down event
5053 * @return {boolean} False to prevent defaults
5055 OO
.ui
.TagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
5056 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
5063 * Handle key press events.
5066 * @param {jQuery.Event} e Key press event
5067 * @return {boolean} Whether to prevent defaults
5069 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyPress = function ( e
) {
5071 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
5073 if ( !this.isDisabled() ) {
5074 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
5075 stopOrContinue
= this.doInputEnter( e
, withMetaKey
);
5078 // Make sure the input gets resized.
5079 setTimeout( this.updateInputSize
.bind( this ), 0 );
5080 return stopOrContinue
;
5085 * Handle key down events.
5088 * @param {jQuery.Event} e Key down event
5091 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyDown = function ( e
) {
5092 var movement
, direction
,
5093 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
5095 if ( !this.isDisabled() ) {
5096 // 'keypress' event is not triggered for Backspace
5097 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
5098 return this.doInputBackspace( e
, withMetaKey
);
5099 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ESCAPE
) {
5100 return this.doInputEscape( e
);
5102 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
5103 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
5105 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
5116 direction
= e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
5117 movement
.left
: movement
.right
;
5119 return this.doInputArrow( e
, direction
, withMetaKey
);
5125 * Respond to input focus event
5127 OO
.ui
.TagMultiselectWidget
.prototype.onInputFocus = function () {
5128 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-focus' );
5132 * Respond to input blur event
5134 OO
.ui
.TagMultiselectWidget
.prototype.onInputBlur = function () {
5135 this.$element
.removeClass( 'oo-ui-tagMultiselectWidget-focus' );
5139 * Perform an action after the enter key on the input
5141 * @param {jQuery.Event} e Event data
5142 * @param {boolean} [withMetaKey] Whether this key was pressed with
5143 * a meta key like 'ctrl'
5144 * @return {boolean} Whether to prevent defaults
5146 OO
.ui
.TagMultiselectWidget
.prototype.doInputEnter = function () {
5147 this.addTagFromInput();
5152 * Perform an action responding to the enter key on the input
5154 * @param {jQuery.Event} e Event data
5155 * @param {boolean} [withMetaKey] Whether this key was pressed with
5156 * a meta key like 'ctrl'
5157 * @return {boolean} Whether to prevent defaults
5159 OO
.ui
.TagMultiselectWidget
.prototype.doInputBackspace = function ( e
, withMetaKey
) {
5163 this.inputPosition
=== 'inline' &&
5164 this.input
.getValue() === '' &&
5167 // Delete the last item
5168 items
= this.getItems();
5169 item
= items
[ items
.length
- 1 ];
5170 this.removeItems( [ item
] );
5171 // If Ctrl/Cmd was pressed, delete item entirely.
5172 // Otherwise put it into the text field for editing.
5173 if ( !withMetaKey
) {
5174 this.input
.setValue( item
.getData() );
5182 * Perform an action after the escape key on the input
5184 * @param {jQuery.Event} e Event data
5186 OO
.ui
.TagMultiselectWidget
.prototype.doInputEscape = function () {
5191 * Perform an action after the arrow key on the input, select the previous
5192 * or next item from the input.
5193 * See #getPreviousItem and #getNextItem
5195 * @param {jQuery.Event} e Event data
5196 * @param {string} direction Direction of the movement; forwards or backwards
5197 * @param {boolean} [withMetaKey] Whether this key was pressed with
5198 * a meta key like 'ctrl'
5200 OO
.ui
.TagMultiselectWidget
.prototype.doInputArrow = function ( e
, direction
) {
5202 this.inputPosition
=== 'inline' &&
5205 if ( direction
=== 'backwards' ) {
5206 // Get previous item
5207 this.getPreviousItem().focus();
5210 this.getNextItem().focus();
5216 * Respond to item select event
5218 * @param {OO.ui.TagItemWidget} item Selected item
5220 OO
.ui
.TagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5221 if ( this.hasInput
&& this.allowEditTags
) {
5222 if ( this.input
.getValue() ) {
5223 this.addTagFromInput();
5225 // 1. Get the label of the tag into the input
5226 this.input
.setValue( item
.getData() );
5227 // 2. Remove the tag
5228 this.removeItems( [ item
] );
5229 // 3. Focus the input
5235 * Respond to change event, where items were added, removed, or cleared.
5237 OO
.ui
.TagMultiselectWidget
.prototype.onChangeTags = function () {
5238 this.toggleValid( this.checkValidity() );
5239 if ( this.hasInput
) {
5240 this.updateInputSize();
5242 this.updateIfHeightChanged();
5248 OO
.ui
.TagMultiselectWidget
.prototype.setDisabled = function ( isDisabled
) {
5250 OO
.ui
.TagMultiselectWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
5252 if ( this.hasInput
&& this.input
) {
5253 this.input
.setDisabled( !!isDisabled
);
5257 this.getItems().forEach( function ( item
) {
5258 item
.setDisabled( !!isDisabled
);
5264 * Respond to tag remove event
5265 * @param {OO.ui.TagItemWidget} item Removed tag
5267 OO
.ui
.TagMultiselectWidget
.prototype.onTagRemove = function ( item
) {
5268 this.removeTagByData( item
.getData() );
5272 * Respond to navigate event on the tag
5274 * @param {OO.ui.TagItemWidget} item Removed tag
5275 * @param {string} direction Direction of movement; 'forwards' or 'backwards'
5277 OO
.ui
.TagMultiselectWidget
.prototype.onTagNavigate = function ( item
, direction
) {
5278 if ( direction
=== 'forwards' ) {
5279 this.getNextItem( item
).focus();
5281 this.getPreviousItem( item
).focus();
5286 * Add tag from input value
5288 OO
.ui
.TagMultiselectWidget
.prototype.addTagFromInput = function () {
5289 var val
= this.input
.getValue(),
5290 isValid
= this.isAllowedData( val
);
5296 if ( isValid
|| this.allowDisplayInvalidTags
) {
5306 OO
.ui
.TagMultiselectWidget
.prototype.clearInput = function () {
5307 this.input
.setValue( '' );
5311 * Check whether the given value is a duplicate of an existing
5312 * tag already in the list.
5314 * @param {string|Object} data Requested value
5315 * @return {boolean} Value is duplicate
5317 OO
.ui
.TagMultiselectWidget
.prototype.isDuplicateData = function ( data
) {
5318 return !!this.getItemFromData( data
);
5322 * Check whether a given value is allowed to be added
5324 * @param {string|Object} data Requested value
5325 * @return {boolean} Value is allowed
5327 OO
.ui
.TagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
5329 !this.allowDuplicates
&&
5330 this.isDuplicateData( data
)
5335 if ( this.allowArbitrary
) {
5339 // Check with allowed values
5341 this.getAllowedValues().some( function ( value
) {
5342 return data
=== value
;
5352 * Get the allowed values list
5354 * @return {string[]} Allowed data values
5356 OO
.ui
.TagMultiselectWidget
.prototype.getAllowedValues = function () {
5357 return this.allowedValues
;
5361 * Add a value to the allowed values list
5363 * @param {string} value Allowed data value
5365 OO
.ui
.TagMultiselectWidget
.prototype.addAllowedValue = function ( value
) {
5366 if ( this.allowedValues
.indexOf( value
) === -1 ) {
5367 this.allowedValues
.push( value
);
5372 * Get the datas of the currently selected items
5374 * @return {string[]|Object[]} Datas of currently selected items
5376 OO
.ui
.TagMultiselectWidget
.prototype.getValue = function () {
5377 return this.getItems()
5378 .filter( function ( item
) {
5379 return item
.isValid();
5381 .map( function ( item
) {
5382 return item
.getData();
5387 * Set the value of this widget by datas.
5389 * @param {string|string[]|Object|Object[]} valueObject An object representing the data
5390 * and label of the value. If the widget allows arbitrary values,
5391 * the items will be added as-is. Otherwise, the data value will
5392 * be checked against allowedValues.
5393 * This object must contain at least a data key. Example:
5394 * { data: 'foo', label: 'Foo item' }
5395 * For multiple items, use an array of objects. For example:
5397 * { data: 'foo', label: 'Foo item' },
5398 * { data: 'bar', label: 'Bar item' }
5400 * Value can also be added with plaintext array, for example:
5401 * [ 'foo', 'bar', 'bla' ] or a single string, like 'foo'
5403 OO
.ui
.TagMultiselectWidget
.prototype.setValue = function ( valueObject
) {
5404 valueObject
= Array
.isArray( valueObject
) ? valueObject
: [ valueObject
];
5407 valueObject
.forEach( function ( obj
) {
5408 if ( typeof obj
=== 'string' ) {
5411 this.addTag( obj
.data
, obj
.label
);
5417 * Add tag to the display area
5419 * @param {string|Object} data Tag data
5420 * @param {string} [label] Tag label. If no label is provided, the
5421 * stringified version of the data will be used instead.
5422 * @return {boolean} Item was added successfully
5424 OO
.ui
.TagMultiselectWidget
.prototype.addTag = function ( data
, label
) {
5426 isValid
= this.isAllowedData( data
);
5428 if ( isValid
|| this.allowDisplayInvalidTags
) {
5429 newItemWidget
= this.createTagItemWidget( data
, label
);
5430 newItemWidget
.toggleValid( isValid
);
5431 this.addItems( [ newItemWidget
] );
5438 * Remove tag by its data property.
5440 * @param {string|Object} data Tag data
5442 OO
.ui
.TagMultiselectWidget
.prototype.removeTagByData = function ( data
) {
5443 var item
= this.getItemFromData( data
);
5445 this.removeItems( [ item
] );
5449 * Construct a OO.ui.TagItemWidget (or a subclass thereof) from given label and data.
5452 * @param {string} data Item data
5453 * @param {string} label The label text.
5454 * @return {OO.ui.TagItemWidget}
5456 OO
.ui
.TagMultiselectWidget
.prototype.createTagItemWidget = function ( data
, label
) {
5457 label
= label
|| data
;
5459 return new OO
.ui
.TagItemWidget( { data
: data
, label
: label
} );
5463 * Given an item, returns the item after it. If the item is already the
5464 * last item, return `this.input`. If no item is passed, returns the
5468 * @param {OO.ui.TagItemWidget} [item] Tag item
5469 * @return {OO.ui.Widget} The next widget available.
5471 OO
.ui
.TagMultiselectWidget
.prototype.getNextItem = function ( item
) {
5472 var itemIndex
= this.items
.indexOf( item
);
5474 if ( item
=== undefined || itemIndex
=== -1 ) {
5475 return this.items
[ 0 ];
5478 if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
5479 if ( this.hasInput
) {
5482 // Return first item
5483 return this.items
[ 0 ];
5486 return this.items
[ itemIndex
+ 1 ];
5491 * Given an item, returns the item before it. If the item is already the
5492 * first item, return `this.input`. If no item is passed, returns the
5496 * @param {OO.ui.TagItemWidget} [item] Tag item
5497 * @return {OO.ui.Widget} The previous widget available.
5499 OO
.ui
.TagMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
5500 var itemIndex
= this.items
.indexOf( item
);
5502 if ( item
=== undefined || itemIndex
=== -1 ) {
5503 return this.items
[ this.items
.length
- 1 ];
5506 if ( itemIndex
=== 0 ) {
5507 if ( this.hasInput
) {
5510 // Return the last item
5511 return this.items
[ this.items
.length
- 1 ];
5514 return this.items
[ itemIndex
- 1 ];
5519 * Update the dimensions of the text input field to encompass all available area.
5520 * This is especially relevant for when the input is at the edge of a line
5521 * and should get smaller. The usual operation (as an inline-block with min-width)
5522 * does not work in that case, pushing the input downwards to the next line.
5526 OO
.ui
.TagMultiselectWidget
.prototype.updateInputSize = function () {
5527 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
5528 if ( this.inputPosition
=== 'inline' && !this.isDisabled() ) {
5529 this.input
.$input
.css( 'width', '1em' );
5530 $lastItem
= this.$group
.children().last();
5531 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
5533 // Get the width of the input with the placeholder text as
5534 // the value and save it so that we don't keep recalculating
5536 this.contentWidthWithPlaceholder
=== undefined &&
5537 this.input
.getValue() === '' &&
5538 this.input
.$input
.attr( 'placeholder' ) !== undefined
5540 this.input
.setValue( this.input
.$input
.attr( 'placeholder' ) );
5541 this.contentWidthWithPlaceholder
= this.input
.$input
[ 0 ].scrollWidth
;
5542 this.input
.setValue( '' );
5546 // Always keep the input wide enough for the placeholder text
5547 contentWidth
= Math
.max(
5548 this.input
.$input
[ 0 ].scrollWidth
,
5549 // undefined arguments in Math.max lead to NaN
5550 ( this.contentWidthWithPlaceholder
=== undefined ) ?
5551 0 : this.contentWidthWithPlaceholder
5553 currentWidth
= this.input
.$input
.width();
5555 if ( contentWidth
< currentWidth
) {
5556 this.updateIfHeightChanged();
5557 // All is fine, don't perform expensive calculations
5561 if ( $lastItem
.length
=== 0 ) {
5562 bestWidth
= this.$content
.innerWidth();
5564 bestWidth
= direction
=== 'ltr' ?
5565 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
5566 $lastItem
.position().left
;
5569 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
5570 // pixels this is off by are coming from.
5572 if ( contentWidth
> bestWidth
) {
5573 // This will result in the input getting shifted to the next line
5574 bestWidth
= this.$content
.innerWidth() - 10;
5576 this.input
.$input
.width( Math
.floor( bestWidth
) );
5577 this.updateIfHeightChanged();
5579 this.updateIfHeightChanged();
5584 * Determine if widget height changed, and if so,
5585 * emit the resize event. This is useful for when there are either
5586 * menus or popups attached to the bottom of the widget, to allow
5587 * them to change their positioning in case the widget moved down
5592 OO
.ui
.TagMultiselectWidget
.prototype.updateIfHeightChanged = function () {
5593 var height
= this.$element
.height();
5594 if ( height
!== this.height
) {
5595 this.height
= height
;
5596 this.emit( 'resize' );
5601 * Check whether all items in the widget are valid
5603 * @return {boolean} Widget is valid
5605 OO
.ui
.TagMultiselectWidget
.prototype.checkValidity = function () {
5606 return this.getItems().every( function ( item
) {
5607 return item
.isValid();
5612 * Set the valid state of this item
5614 * @param {boolean} [valid] Item is valid
5617 OO
.ui
.TagMultiselectWidget
.prototype.toggleValid = function ( valid
) {
5618 valid
= valid
=== undefined ? !this.valid
: !!valid
;
5620 if ( this.valid
!== valid
) {
5623 this.setFlags( { invalid
: !this.valid
} );
5625 this.emit( 'valid', this.valid
);
5630 * Get the current valid state of the widget
5632 * @return {boolean} Widget is valid
5634 OO
.ui
.TagMultiselectWidget
.prototype.isValid = function () {
5639 * PopupTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5640 * to use a popup. The popup can be configured to have a default input to insert values into the widget.
5642 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
5645 * // Example: A basic PopupTagMultiselectWidget.
5646 * var widget = new OO.ui.PopupTagMultiselectWidget();
5647 * $( 'body' ).append( widget.$element );
5649 * // Example: A PopupTagMultiselectWidget with an external popup.
5650 * var popupInput = new OO.ui.TextInputWidget(),
5651 * widget = new OO.ui.PopupTagMultiselectWidget( {
5652 * popupInput: popupInput,
5654 * $content: popupInput.$element
5657 * $( 'body' ).append( widget.$element );
5659 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
5662 * @extends OO.ui.TagMultiselectWidget
5663 * @mixins OO.ui.mixin.PopupElement
5665 * @param {Object} config Configuration object
5666 * @cfg {jQuery} [$overlay] An overlay for the popup.
5667 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
5668 * @cfg {Object} [popup] Configuration options for the popup
5669 * @cfg {OO.ui.InputWidget} [popupInput] An input widget inside the popup that will be
5670 * focused when the popup is opened and will be used as replacement for the
5671 * general input in the widget.
5673 OO
.ui
.PopupTagMultiselectWidget
= function OoUiPopupTagMultiselectWidget( config
) {
5675 defaultConfig
= { popup
: {} };
5677 config
= config
|| {};
5679 // Parent constructor
5680 OO
.ui
.PopupTagMultiselectWidget
.parent
.call( this, $.extend( { inputPosition
: 'none' }, config
) );
5682 this.$overlay
= config
.$overlay
|| this.$element
;
5684 if ( !config
.popup
) {
5685 // For the default base implementation, we give a popup
5686 // with an input widget inside it. For any other use cases
5687 // the popup needs to be populated externally and the
5688 // event handled to add tags separately and manually
5689 defaultInput
= new OO
.ui
.TextInputWidget();
5691 defaultConfig
.popupInput
= defaultInput
;
5692 defaultConfig
.popup
.$content
= defaultInput
.$element
;
5694 this.$element
.addClass( 'oo-ui-popupTagMultiselectWidget-defaultPopup' );
5697 // Add overlay, and add that to the autoCloseIgnore
5698 defaultConfig
.popup
.$overlay
= this.$overlay
;
5699 defaultConfig
.popup
.$autoCloseIgnore
= this.hasInput
?
5700 this.input
.$element
.add( this.$overlay
) : this.$overlay
;
5702 // Allow extending any of the above
5703 config
= $.extend( defaultConfig
, config
);
5705 // Mixin constructors
5706 OO
.ui
.mixin
.PopupElement
.call( this, config
);
5708 if ( this.hasInput
) {
5709 this.input
.$input
.on( 'focus', this.popup
.toggle
.bind( this.popup
, true ) );
5712 // Configuration options
5713 this.popupInput
= config
.popupInput
;
5714 if ( this.popupInput
) {
5715 this.popupInput
.connect( this, {
5716 enter
: 'onPopupInputEnter'
5721 this.on( 'resize', this.popup
.updateDimensions
.bind( this.popup
) );
5722 this.popup
.connect( this, { toggle
: 'onPopupToggle' } );
5724 .on( 'focus', this.onFocus
.bind( this ) );
5728 .append( this.popup
.$element
)
5729 .addClass( 'oo-ui-popupTagMultiselectWidget' );
5732 /* Initialization */
5734 OO
.inheritClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5735 OO
.mixinClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
5740 * Focus event handler.
5744 OO
.ui
.PopupTagMultiselectWidget
.prototype.onFocus = function () {
5745 this.popup
.toggle( true );
5749 * Respond to popup toggle event
5751 * @param {boolean} isVisible Popup is visible
5753 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupToggle = function ( isVisible
) {
5754 if ( isVisible
&& this.popupInput
) {
5755 this.popupInput
.focus();
5760 * Respond to popup input enter event
5762 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupInputEnter = function () {
5763 if ( this.popupInput
) {
5764 this.addTagByPopupValue( this.popupInput
.getValue() );
5765 this.popupInput
.setValue( '' );
5772 OO
.ui
.PopupTagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5773 if ( this.popupInput
&& this.allowEditTags
) {
5774 this.popupInput
.setValue( item
.getData() );
5775 this.removeItems( [ item
] );
5777 this.popup
.toggle( true );
5778 this.popupInput
.focus();
5781 OO
.ui
.PopupTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, item
);
5786 * Add a tag by the popup value.
5787 * Whatever is responsible for setting the value in the popup should call
5788 * this method to add a tag, or use the regular methods like #addTag or
5789 * #setValue directly.
5791 * @param {string} data The value of item
5792 * @param {string} [label] The label of the tag. If not given, the data is used.
5794 OO
.ui
.PopupTagMultiselectWidget
.prototype.addTagByPopupValue = function ( data
, label
) {
5795 this.addTag( data
, label
);
5799 * MenuTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5800 * to use a menu of selectable options.
5802 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
5805 * // Example: A basic MenuTagMultiselectWidget.
5806 * var widget = new OO.ui.MenuTagMultiselectWidget( {
5807 * inputPosition: 'outline',
5809 * { data: 'option1', label: 'Option 1' },
5810 * { data: 'option2', label: 'Option 2' },
5811 * { data: 'option3', label: 'Option 3' },
5813 * selected: [ 'option1', 'option2' ]
5815 * $( 'body' ).append( widget.$element );
5817 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
5820 * @extends OO.ui.TagMultiselectWidget
5823 * @param {Object} [config] Configuration object
5824 * @cfg {Object} [menu] Configuration object for the menu widget
5825 * @cfg {jQuery} [$overlay] An overlay for the menu.
5826 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
5827 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
5829 OO
.ui
.MenuTagMultiselectWidget
= function OoUiMenuTagMultiselectWidget( config
) {
5830 config
= config
|| {};
5832 // Parent constructor
5833 OO
.ui
.MenuTagMultiselectWidget
.parent
.call( this, config
);
5835 this.$overlay
= config
.$overlay
|| this.$element
;
5837 this.menu
= this.createMenuWidget( $.extend( {
5839 input
: this.hasInput
? this.input
: null,
5840 $input
: this.hasInput
? this.input
.$input
: null,
5841 filterFromInput
: !!this.hasInput
,
5842 $autoCloseIgnore
: this.hasInput
?
5843 this.input
.$element
.add( this.$overlay
) : this.$overlay
,
5844 $floatableContainer
: this.hasInput
&& this.inputPosition
=== 'outline' ?
5845 this.input
.$element
: this.$element
,
5846 $overlay
: this.$overlay
,
5847 disabled
: this.isDisabled()
5849 this.addOptions( config
.options
|| [] );
5852 this.menu
.connect( this, {
5853 choose
: 'onMenuChoose',
5854 toggle
: 'onMenuToggle'
5856 if ( this.hasInput
) {
5857 this.input
.connect( this, { change
: 'onInputChange' } );
5859 this.connect( this, { resize
: 'onResize' } );
5863 .append( this.menu
.$element
);
5865 .addClass( 'oo-ui-menuTagMultiselectWidget' );
5868 /* Initialization */
5870 OO
.inheritClass( OO
.ui
.MenuTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5875 * Respond to resize event
5877 OO
.ui
.MenuTagMultiselectWidget
.prototype.onResize = function () {
5878 // Reposition the menu
5879 this.menu
.position();
5885 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputFocus = function () {
5887 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
5889 this.menu
.toggle( true );
5893 * Respond to input change event
5895 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputChange = function () {
5896 this.menu
.toggle( true );
5900 * Respond to menu choose event
5902 * @param {OO.ui.OptionWidget} menuItem Chosen menu item
5904 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuChoose = function ( menuItem
) {
5906 this.addTag( menuItem
.getData(), menuItem
.getLabel() );
5910 * Respond to menu toggle event. Reset item highlights on hide.
5912 * @param {boolean} isVisible The menu is visible
5914 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
5916 this.menu
.selectItem( null );
5917 this.menu
.highlightItem( null );
5924 OO
.ui
.MenuTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
5925 var menuItem
= this.menu
.getItemFromData( tagItem
.getData() );
5926 // Override the base behavior from TagMultiselectWidget; the base behavior
5927 // in TagMultiselectWidget is to remove the tag to edit it in the input,
5928 // but in our case, we want to utilize the menu selection behavior, and
5929 // definitely not remove the item.
5931 // Select the menu item
5932 this.menu
.selectItem( menuItem
);
5940 OO
.ui
.MenuTagMultiselectWidget
.prototype.addTagFromInput = function () {
5941 var inputValue
= this.input
.getValue(),
5942 highlightedItem
= this.menu
.getHighlightedItem(),
5943 item
= this.menu
.getItemFromData( inputValue
);
5945 // Override the parent method so we add from the menu
5946 // rather than directly from the input
5948 // Look for a highlighted item first
5949 if ( highlightedItem
) {
5950 this.addTag( highlightedItem
.getData(), highlightedItem
.getLabel() );
5951 } else if ( item
) {
5952 // Look for the element that fits the data
5953 this.addTag( item
.getData(), item
.getLabel() );
5955 // Otherwise, add the tag - the method will only add if the
5956 // tag is valid or if invalid tags are allowed
5957 this.addTag( inputValue
);
5962 * Return the visible items in the menu. This is mainly used for when
5963 * the menu is filtering results.
5965 * @return {OO.ui.MenuOptionWidget[]} Visible results
5967 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenuVisibleItems = function () {
5968 return this.menu
.getItems().filter( function ( menuItem
) {
5969 return menuItem
.isVisible();
5974 * Create the menu for this widget. This is in a separate method so that
5975 * child classes can override this without polluting the constructor with
5976 * unnecessary extra objects that will be overidden.
5978 * @param {Object} menuConfig Configuration options
5979 * @return {OO.ui.MenuSelectWidget} Menu widget
5981 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
5982 return new OO
.ui
.MenuSelectWidget( menuConfig
);
5986 * Add options to the menu
5988 * @param {Object[]} menuOptions Object defining options
5990 OO
.ui
.MenuTagMultiselectWidget
.prototype.addOptions = function ( menuOptions
) {
5992 items
= menuOptions
.map( function ( obj
) {
5993 return widget
.createMenuOptionWidget( obj
.data
, obj
.label
);
5996 this.menu
.addItems( items
);
6000 * Create a menu option widget.
6002 * @param {string} data Item data
6003 * @param {string} [label] Item label
6004 * @return {OO.ui.OptionWidget} Option widget
6006 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuOptionWidget = function ( data
, label
) {
6007 return new OO
.ui
.MenuOptionWidget( {
6009 label
: label
|| data
6016 * @return {OO.ui.MenuSelectWidget} Menu
6018 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenu = function () {
6023 * Get the allowed values list
6025 * @return {string[]} Allowed data values
6027 OO
.ui
.MenuTagMultiselectWidget
.prototype.getAllowedValues = function () {
6028 var menuDatas
= this.menu
.getItems().map( function ( menuItem
) {
6029 return menuItem
.getData();
6031 return this.allowedValues
.concat( menuDatas
);
6035 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
6036 * widgets can be configured with {@link OO.ui.mixin.IconElement icons} and {@link
6037 * OO.ui.mixin.IndicatorElement indicators}.
6038 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
6041 * // Example of a file select widget
6042 * var selectFile = new OO.ui.SelectFileWidget();
6043 * $( 'body' ).append( selectFile.$element );
6045 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets
6048 * @extends OO.ui.Widget
6049 * @mixins OO.ui.mixin.IconElement
6050 * @mixins OO.ui.mixin.IndicatorElement
6051 * @mixins OO.ui.mixin.PendingElement
6052 * @mixins OO.ui.mixin.LabelElement
6055 * @param {Object} [config] Configuration options
6056 * @cfg {string[]|null} [accept=null] MIME types to accept. null accepts all types.
6057 * @cfg {string} [placeholder] Text to display when no file is selected.
6058 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
6059 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
6060 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be true.
6061 * @cfg {number} [thumbnailSizeLimit=20] File size limit in MiB above which to not try and show a
6062 * preview (for performance)
6064 OO
.ui
.SelectFileWidget
= function OoUiSelectFileWidget( config
) {
6067 // Configuration initialization
6068 config
= $.extend( {
6070 placeholder
: OO
.ui
.msg( 'ooui-selectfile-placeholder' ),
6071 notsupported
: OO
.ui
.msg( 'ooui-selectfile-not-supported' ),
6073 showDropTarget
: false,
6074 thumbnailSizeLimit
: 20
6077 // Parent constructor
6078 OO
.ui
.SelectFileWidget
.parent
.call( this, config
);
6080 // Mixin constructors
6081 OO
.ui
.mixin
.IconElement
.call( this, config
);
6082 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
6083 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {}, config
, { $pending
: this.$info
} ) );
6084 OO
.ui
.mixin
.LabelElement
.call( this, config
);
6087 this.$info
= $( '<span>' );
6088 this.showDropTarget
= config
.showDropTarget
;
6089 this.thumbnailSizeLimit
= config
.thumbnailSizeLimit
;
6090 this.isSupported
= this.constructor.static.isSupported();
6091 this.currentFile
= null;
6092 if ( Array
.isArray( config
.accept
) ) {
6093 this.accept
= config
.accept
;
6097 this.placeholder
= config
.placeholder
;
6098 this.notsupported
= config
.notsupported
;
6099 this.onFileSelectedHandler
= this.onFileSelected
.bind( this );
6101 this.selectButton
= new OO
.ui
.ButtonWidget( {
6102 classes
: [ 'oo-ui-selectFileWidget-selectButton' ],
6103 label
: OO
.ui
.msg( 'ooui-selectfile-button-select' ),
6104 disabled
: this.disabled
|| !this.isSupported
6107 this.clearButton
= new OO
.ui
.ButtonWidget( {
6108 classes
: [ 'oo-ui-selectFileWidget-clearButton' ],
6111 disabled
: this.disabled
6115 this.selectButton
.$button
.on( {
6116 keypress
: this.onKeyPress
.bind( this )
6118 this.clearButton
.connect( this, {
6119 click
: 'onClearClick'
6121 if ( config
.droppable
) {
6122 dragHandler
= this.onDragEnterOrOver
.bind( this );
6124 dragenter
: dragHandler
,
6125 dragover
: dragHandler
,
6126 dragleave
: this.onDragLeave
.bind( this ),
6127 drop
: this.onDrop
.bind( this )
6133 this.$label
.addClass( 'oo-ui-selectFileWidget-label' );
6135 .addClass( 'oo-ui-selectFileWidget-info' )
6136 .append( this.$icon
, this.$label
, this.clearButton
.$element
, this.$indicator
);
6138 if ( config
.droppable
&& config
.showDropTarget
) {
6139 this.selectButton
.setIcon( 'upload' );
6140 this.$thumbnail
= $( '<div>' ).addClass( 'oo-ui-selectFileWidget-thumbnail' );
6141 this.setPendingElement( this.$thumbnail
);
6143 .addClass( 'oo-ui-selectFileWidget-dropTarget oo-ui-selectFileWidget' )
6145 click
: this.onDropTargetClick
.bind( this )
6150 this.selectButton
.$element
,
6152 .addClass( 'oo-ui-selectFileWidget-dropLabel' )
6153 .text( OO
.ui
.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
6157 .addClass( 'oo-ui-selectFileWidget' )
6158 .append( this.$info
, this.selectButton
.$element
);
6165 OO
.inheritClass( OO
.ui
.SelectFileWidget
, OO
.ui
.Widget
);
6166 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IconElement
);
6167 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IndicatorElement
);
6168 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.PendingElement
);
6169 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.LabelElement
);
6171 /* Static Properties */
6174 * Check if this widget is supported
6179 OO
.ui
.SelectFileWidget
.static.isSupported = function () {
6181 if ( OO
.ui
.SelectFileWidget
.static.isSupportedCache
=== null ) {
6182 $input
= $( '<input>' ).attr( 'type', 'file' );
6183 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= $input
[ 0 ].files
!== undefined;
6185 return OO
.ui
.SelectFileWidget
.static.isSupportedCache
;
6188 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= null;
6195 * A change event is emitted when the on/off state of the toggle changes.
6197 * @param {File|null} value New value
6203 * Get the current value of the field
6205 * @return {File|null}
6207 OO
.ui
.SelectFileWidget
.prototype.getValue = function () {
6208 return this.currentFile
;
6212 * Set the current value of the field
6214 * @param {File|null} file File to select
6216 OO
.ui
.SelectFileWidget
.prototype.setValue = function ( file
) {
6217 if ( this.currentFile
!== file
) {
6218 this.currentFile
= file
;
6220 this.emit( 'change', this.currentFile
);
6227 * Focusses the select file button.
6231 OO
.ui
.SelectFileWidget
.prototype.focus = function () {
6232 this.selectButton
.focus();
6241 OO
.ui
.SelectFileWidget
.prototype.blur = function () {
6242 this.selectButton
.blur();
6247 * Update the user interface when a file is selected or unselected
6251 OO
.ui
.SelectFileWidget
.prototype.updateUI = function () {
6253 if ( !this.isSupported
) {
6254 this.$element
.addClass( 'oo-ui-selectFileWidget-notsupported' );
6255 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
6256 this.setLabel( this.notsupported
);
6258 this.$element
.addClass( 'oo-ui-selectFileWidget-supported' );
6259 if ( this.currentFile
) {
6260 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
6262 $label
= $label
.add(
6264 .addClass( 'oo-ui-selectFileWidget-fileName' )
6265 .text( this.currentFile
.name
)
6267 this.setLabel( $label
);
6269 if ( this.showDropTarget
) {
6271 this.loadAndGetImageUrl().done( function ( url
) {
6272 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
6273 }.bind( this ) ).fail( function () {
6274 this.$thumbnail
.append(
6275 new OO
.ui
.IconWidget( {
6277 classes
: [ 'oo-ui-selectFileWidget-noThumbnail-icon' ]
6280 }.bind( this ) ).always( function () {
6283 this.$element
.off( 'click' );
6286 if ( this.showDropTarget
) {
6287 this.$element
.off( 'click' );
6289 click
: this.onDropTargetClick
.bind( this )
6293 .css( 'background-image', '' );
6295 this.$element
.addClass( 'oo-ui-selectFileWidget-empty' );
6296 this.setLabel( this.placeholder
);
6302 * If the selected file is an image, get its URL and load it.
6304 * @return {jQuery.Promise} Promise resolves with the image URL after it has loaded
6306 OO
.ui
.SelectFileWidget
.prototype.loadAndGetImageUrl = function () {
6307 var deferred
= $.Deferred(),
6308 file
= this.currentFile
,
6309 reader
= new FileReader();
6313 ( OO
.getProp( file
, 'type' ) || '' ).indexOf( 'image/' ) === 0 &&
6314 file
.size
< this.thumbnailSizeLimit
* 1024 * 1024
6316 reader
.onload = function ( event
) {
6317 var img
= document
.createElement( 'img' );
6318 img
.addEventListener( 'load', function () {
6320 img
.naturalWidth
=== 0 ||
6321 img
.naturalHeight
=== 0 ||
6322 img
.complete
=== false
6326 deferred
.resolve( event
.target
.result
);
6329 img
.src
= event
.target
.result
;
6331 reader
.readAsDataURL( file
);
6336 return deferred
.promise();
6340 * Add the input to the widget
6344 OO
.ui
.SelectFileWidget
.prototype.addInput = function () {
6345 if ( this.$input
) {
6346 this.$input
.remove();
6349 if ( !this.isSupported
) {
6354 this.$input
= $( '<input>' ).attr( 'type', 'file' );
6355 this.$input
.on( 'change', this.onFileSelectedHandler
);
6356 this.$input
.on( 'click', function ( e
) {
6357 // Prevents dropTarget to get clicked which calls
6358 // a click on this input
6359 e
.stopPropagation();
6364 if ( this.accept
) {
6365 this.$input
.attr( 'accept', this.accept
.join( ', ' ) );
6367 this.selectButton
.$button
.append( this.$input
);
6371 * Determine if we should accept this file
6374 * @param {string} mimeType File MIME type
6377 OO
.ui
.SelectFileWidget
.prototype.isAllowedType = function ( mimeType
) {
6380 if ( !this.accept
|| !mimeType
) {
6384 for ( i
= 0; i
< this.accept
.length
; i
++ ) {
6385 mimeTest
= this.accept
[ i
];
6386 if ( mimeTest
=== mimeType
) {
6388 } else if ( mimeTest
.substr( -2 ) === '/*' ) {
6389 mimeTest
= mimeTest
.substr( 0, mimeTest
.length
- 1 );
6390 if ( mimeType
.substr( 0, mimeTest
.length
) === mimeTest
) {
6400 * Handle file selection from the input
6403 * @param {jQuery.Event} e
6405 OO
.ui
.SelectFileWidget
.prototype.onFileSelected = function ( e
) {
6406 var file
= OO
.getProp( e
.target
, 'files', 0 ) || null;
6408 if ( file
&& !this.isAllowedType( file
.type
) ) {
6412 this.setValue( file
);
6417 * Handle clear button click events.
6421 OO
.ui
.SelectFileWidget
.prototype.onClearClick = function () {
6422 this.setValue( null );
6427 * Handle key press events.
6430 * @param {jQuery.Event} e Key press event
6432 OO
.ui
.SelectFileWidget
.prototype.onKeyPress = function ( e
) {
6433 if ( this.isSupported
&& !this.isDisabled() && this.$input
&&
6434 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
6436 this.$input
.click();
6442 * Handle drop target click events.
6445 * @param {jQuery.Event} e Key press event
6447 OO
.ui
.SelectFileWidget
.prototype.onDropTargetClick = function () {
6448 if ( this.isSupported
&& !this.isDisabled() && this.$input
) {
6449 this.$input
.click();
6455 * Handle drag enter and over events
6458 * @param {jQuery.Event} e Drag event
6460 OO
.ui
.SelectFileWidget
.prototype.onDragEnterOrOver = function ( e
) {
6462 droppableFile
= false,
6463 dt
= e
.originalEvent
.dataTransfer
;
6466 e
.stopPropagation();
6468 if ( this.isDisabled() || !this.isSupported
) {
6469 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6470 dt
.dropEffect
= 'none';
6474 // DataTransferItem and File both have a type property, but in Chrome files
6475 // have no information at this point.
6476 itemOrFile
= OO
.getProp( dt
, 'items', 0 ) || OO
.getProp( dt
, 'files', 0 );
6478 if ( this.isAllowedType( itemOrFile
.type
) ) {
6479 droppableFile
= true;
6481 // dt.types is Array-like, but not an Array
6482 } else if ( Array
.prototype.indexOf
.call( OO
.getProp( dt
, 'types' ) || [], 'Files' ) !== -1 ) {
6483 // File information is not available at this point for security so just assume
6484 // it is acceptable for now.
6485 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
6486 droppableFile
= true;
6489 this.$element
.toggleClass( 'oo-ui-selectFileWidget-canDrop', droppableFile
);
6490 if ( !droppableFile
) {
6491 dt
.dropEffect
= 'none';
6498 * Handle drag leave events
6501 * @param {jQuery.Event} e Drag event
6503 OO
.ui
.SelectFileWidget
.prototype.onDragLeave = function () {
6504 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6508 * Handle drop events
6511 * @param {jQuery.Event} e Drop event
6513 OO
.ui
.SelectFileWidget
.prototype.onDrop = function ( e
) {
6515 dt
= e
.originalEvent
.dataTransfer
;
6518 e
.stopPropagation();
6519 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6521 if ( this.isDisabled() || !this.isSupported
) {
6525 file
= OO
.getProp( dt
, 'files', 0 );
6526 if ( file
&& !this.isAllowedType( file
.type
) ) {
6530 this.setValue( file
);
6539 OO
.ui
.SelectFileWidget
.prototype.setDisabled = function ( disabled
) {
6540 OO
.ui
.SelectFileWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
6541 if ( this.selectButton
) {
6542 this.selectButton
.setDisabled( disabled
);
6544 if ( this.clearButton
) {
6545 this.clearButton
.setDisabled( disabled
);
6551 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field}, where users can type a search query,
6552 * and a menu of search results, which is displayed beneath the query
6553 * field. Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible to the user.
6554 * Users can choose an item from the menu or type a query into the text field to search for a matching result item.
6555 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
6557 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
6558 * the [OOjs UI demos][1] for an example.
6560 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/#dialogs-mediawiki-vector-ltr
6563 * @extends OO.ui.Widget
6566 * @param {Object} [config] Configuration options
6567 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6568 * @cfg {string} [value] Initial query value
6570 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
6571 // Configuration initialization
6572 config
= config
|| {};
6574 // Parent constructor
6575 OO
.ui
.SearchWidget
.parent
.call( this, config
);
6578 this.query
= new OO
.ui
.TextInputWidget( {
6580 placeholder
: config
.placeholder
,
6583 this.results
= new OO
.ui
.SelectWidget();
6584 this.$query
= $( '<div>' );
6585 this.$results
= $( '<div>' );
6588 this.query
.connect( this, {
6589 change
: 'onQueryChange',
6590 enter
: 'onQueryEnter'
6592 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
6596 .addClass( 'oo-ui-searchWidget-query' )
6597 .append( this.query
.$element
);
6599 .addClass( 'oo-ui-searchWidget-results' )
6600 .append( this.results
.$element
);
6602 .addClass( 'oo-ui-searchWidget' )
6603 .append( this.$results
, this.$query
);
6608 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
6613 * Handle query key down events.
6616 * @param {jQuery.Event} e Key down event
6618 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
6619 var highlightedItem
, nextItem
,
6620 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
6623 highlightedItem
= this.results
.getHighlightedItem();
6624 if ( !highlightedItem
) {
6625 highlightedItem
= this.results
.getSelectedItem();
6627 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
6628 this.results
.highlightItem( nextItem
);
6629 nextItem
.scrollElementIntoView();
6634 * Handle select widget select events.
6636 * Clears existing results. Subclasses should repopulate items according to new query.
6639 * @param {string} value New value
6641 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
6643 this.results
.clearItems();
6647 * Handle select widget enter key events.
6649 * Chooses highlighted item.
6652 * @param {string} value New value
6654 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
6655 var highlightedItem
= this.results
.getHighlightedItem();
6656 if ( highlightedItem
) {
6657 this.results
.chooseItem( highlightedItem
);
6662 * Get the query input.
6664 * @return {OO.ui.TextInputWidget} Query input
6666 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
6671 * Get the search results menu.
6673 * @return {OO.ui.SelectWidget} Menu of search results
6675 OO
.ui
.SearchWidget
.prototype.getResults = function () {
6676 return this.results
;
6680 * NumberInputWidgets combine a {@link OO.ui.TextInputWidget text input} (where a value
6681 * can be entered manually) and two {@link OO.ui.ButtonWidget button widgets}
6682 * (to adjust the value in increments) to allow the user to enter a number.
6685 * // Example: A NumberInputWidget.
6686 * var numberInput = new OO.ui.NumberInputWidget( {
6687 * label: 'NumberInputWidget',
6688 * input: { value: 5 },
6692 * $( 'body' ).append( numberInput.$element );
6695 * @extends OO.ui.TextInputWidget
6698 * @param {Object} [config] Configuration options
6699 * @cfg {Object} [minusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget decrementing button widget}.
6700 * @cfg {Object} [plusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget incrementing button widget}.
6701 * @cfg {boolean} [allowInteger=false] Whether the field accepts only integer values.
6702 * @cfg {number} [min=-Infinity] Minimum allowed value
6703 * @cfg {number} [max=Infinity] Maximum allowed value
6704 * @cfg {number} [step=1] Delta when using the buttons or up/down arrow keys
6705 * @cfg {number|null} [pageStep] Delta when using the page-up/page-down keys. Defaults to 10 times #step.
6706 * @cfg {boolean} [showButtons=true] Whether to show the plus and minus buttons.
6708 OO
.ui
.NumberInputWidget
= function OoUiNumberInputWidget( config
) {
6709 var $field
= $( '<div>' )
6710 .addClass( 'oo-ui-numberInputWidget-field' );
6712 // Configuration initialization
6713 config
= $.extend( {
6714 allowInteger
: false,
6722 // For backward compatibility
6723 $.extend( config
, config
.input
);
6726 // Parent constructor
6727 OO
.ui
.NumberInputWidget
.parent
.call( this, $.extend( config
, {
6731 if ( config
.showButtons
) {
6732 this.minusButton
= new OO
.ui
.ButtonWidget( $.extend(
6734 disabled
: this.isDisabled(),
6736 classes
: [ 'oo-ui-numberInputWidget-minusButton' ],
6741 this.plusButton
= new OO
.ui
.ButtonWidget( $.extend(
6743 disabled
: this.isDisabled(),
6745 classes
: [ 'oo-ui-numberInputWidget-plusButton' ],
6754 keydown
: this.onKeyDown
.bind( this ),
6755 'wheel mousewheel DOMMouseScroll': this.onWheel
.bind( this )
6757 if ( config
.showButtons
) {
6758 this.plusButton
.connect( this, {
6759 click
: [ 'onButtonClick', +1 ]
6761 this.minusButton
.connect( this, {
6762 click
: [ 'onButtonClick', -1 ]
6767 $field
.append( this.$input
);
6768 if ( config
.showButtons
) {
6770 .prepend( this.minusButton
.$element
)
6771 .append( this.plusButton
.$element
);
6775 this.setAllowInteger( config
.allowInteger
|| config
.isInteger
);
6776 this.setRange( config
.min
, config
.max
);
6777 this.setStep( config
.step
, config
.pageStep
);
6778 // Set the validation method after we set allowInteger and range
6779 // so that it doesn't immediately call setValidityFlag
6780 this.setValidation( this.validateNumber
.bind( this ) );
6783 .addClass( 'oo-ui-numberInputWidget' )
6784 .toggleClass( 'oo-ui-numberInputWidget-buttoned', config
.showButtons
)
6790 OO
.inheritClass( OO
.ui
.NumberInputWidget
, OO
.ui
.TextInputWidget
);
6795 * Set whether only integers are allowed
6797 * @param {boolean} flag
6799 OO
.ui
.NumberInputWidget
.prototype.setAllowInteger = function ( flag
) {
6800 this.allowInteger
= !!flag
;
6801 this.setValidityFlag();
6803 // Backward compatibility
6804 OO
.ui
.NumberInputWidget
.prototype.setIsInteger
= OO
.ui
.NumberInputWidget
.prototype.setAllowInteger
;
6807 * Get whether only integers are allowed
6809 * @return {boolean} Flag value
6811 OO
.ui
.NumberInputWidget
.prototype.getAllowInteger = function () {
6812 return this.allowInteger
;
6814 // Backward compatibility
6815 OO
.ui
.NumberInputWidget
.prototype.getIsInteger
= OO
.ui
.NumberInputWidget
.prototype.getAllowInteger
;
6818 * Set the range of allowed values
6820 * @param {number} min Minimum allowed value
6821 * @param {number} max Maximum allowed value
6823 OO
.ui
.NumberInputWidget
.prototype.setRange = function ( min
, max
) {
6825 throw new Error( 'Minimum (' + min
+ ') must not be greater than maximum (' + max
+ ')' );
6829 this.setValidityFlag();
6833 * Get the current range
6835 * @return {number[]} Minimum and maximum values
6837 OO
.ui
.NumberInputWidget
.prototype.getRange = function () {
6838 return [ this.min
, this.max
];
6842 * Set the stepping deltas
6844 * @param {number} step Normal step
6845 * @param {number|null} pageStep Page step. If null, 10 * step will be used.
6847 OO
.ui
.NumberInputWidget
.prototype.setStep = function ( step
, pageStep
) {
6849 throw new Error( 'Step value must be positive' );
6851 if ( pageStep
=== null ) {
6852 pageStep
= step
* 10;
6853 } else if ( pageStep
<= 0 ) {
6854 throw new Error( 'Page step value must be positive' );
6857 this.pageStep
= pageStep
;
6861 * Get the current stepping values
6863 * @return {number[]} Step and page step
6865 OO
.ui
.NumberInputWidget
.prototype.getStep = function () {
6866 return [ this.step
, this.pageStep
];
6870 * Get the current value of the widget as a number
6872 * @return {number} May be NaN, or an invalid number
6874 OO
.ui
.NumberInputWidget
.prototype.getNumericValue = function () {
6875 return +this.getValue();
6879 * Adjust the value of the widget
6881 * @param {number} delta Adjustment amount
6883 OO
.ui
.NumberInputWidget
.prototype.adjustValue = function ( delta
) {
6884 var n
, v
= this.getNumericValue();
6887 if ( isNaN( delta
) || !isFinite( delta
) ) {
6888 throw new Error( 'Delta must be a finite number' );
6895 n
= Math
.max( Math
.min( n
, this.max
), this.min
);
6896 if ( this.allowInteger
) {
6897 n
= Math
.round( n
);
6909 * @param {string} value Field value
6912 OO
.ui
.NumberInputWidget
.prototype.validateNumber = function ( value
) {
6914 if ( value
=== '' ) {
6915 return !this.isRequired();
6918 if ( isNaN( n
) || !isFinite( n
) ) {
6922 if ( this.allowInteger
&& Math
.floor( n
) !== n
) {
6926 if ( n
< this.min
|| n
> this.max
) {
6934 * Handle mouse click events.
6937 * @param {number} dir +1 or -1
6939 OO
.ui
.NumberInputWidget
.prototype.onButtonClick = function ( dir
) {
6940 this.adjustValue( dir
* this.step
);
6944 * Handle mouse wheel events.
6947 * @param {jQuery.Event} event
6949 OO
.ui
.NumberInputWidget
.prototype.onWheel = function ( event
) {
6952 if ( !this.isDisabled() && this.$input
.is( ':focus' ) ) {
6953 // Standard 'wheel' event
6954 if ( event
.originalEvent
.deltaMode
!== undefined ) {
6955 this.sawWheelEvent
= true;
6957 if ( event
.originalEvent
.deltaY
) {
6958 delta
= -event
.originalEvent
.deltaY
;
6959 } else if ( event
.originalEvent
.deltaX
) {
6960 delta
= event
.originalEvent
.deltaX
;
6963 // Non-standard events
6964 if ( !this.sawWheelEvent
) {
6965 if ( event
.originalEvent
.wheelDeltaX
) {
6966 delta
= -event
.originalEvent
.wheelDeltaX
;
6967 } else if ( event
.originalEvent
.wheelDeltaY
) {
6968 delta
= event
.originalEvent
.wheelDeltaY
;
6969 } else if ( event
.originalEvent
.wheelDelta
) {
6970 delta
= event
.originalEvent
.wheelDelta
;
6971 } else if ( event
.originalEvent
.detail
) {
6972 delta
= -event
.originalEvent
.detail
;
6977 delta
= delta
< 0 ? -1 : 1;
6978 this.adjustValue( delta
* this.step
);
6986 * Handle key down events.
6989 * @param {jQuery.Event} e Key down event
6991 OO
.ui
.NumberInputWidget
.prototype.onKeyDown = function ( e
) {
6992 if ( !this.isDisabled() ) {
6993 switch ( e
.which
) {
6995 this.adjustValue( this.step
);
6997 case OO
.ui
.Keys
.DOWN
:
6998 this.adjustValue( -this.step
);
7000 case OO
.ui
.Keys
.PAGEUP
:
7001 this.adjustValue( this.pageStep
);
7003 case OO
.ui
.Keys
.PAGEDOWN
:
7004 this.adjustValue( -this.pageStep
);
7013 OO
.ui
.NumberInputWidget
.prototype.setDisabled = function ( disabled
) {
7015 OO
.ui
.NumberInputWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
7017 if ( this.minusButton
) {
7018 this.minusButton
.setDisabled( this.isDisabled() );
7020 if ( this.plusButton
) {
7021 this.plusButton
.setDisabled( this.isDisabled() );
7029 //# sourceMappingURL=oojs-ui-widgets.js.map