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-08-03T19:36:51Z
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 .append( this.$status
)
281 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' );
285 OO
.mixinClass( OO
.ui
.mixin
.DraggableGroupElement
, OO
.ui
.mixin
.GroupElement
);
290 * An item has been dragged to a new position, but not yet dropped.
293 * @param {OO.ui.mixin.DraggableElement} item Dragged item
294 * @param {number} [newIndex] New index for the item
298 * An item has been dropped at a new position.
301 * @param {OO.ui.mixin.DraggableElement} item Reordered item
302 * @param {number} [newIndex] New index for the item
306 * Draggable state of this widget has changed.
309 * @param {boolean} [draggable] Widget is draggable
315 * Change the draggable state of this widget.
316 * This allows users to temporarily halt the dragging operations.
318 * @param {boolean} isDraggable Widget supports draggable operations
321 OO
.ui
.mixin
.DraggableGroupElement
.prototype.toggleDraggable = function ( isDraggable
) {
322 isDraggable
= isDraggable
!== undefined ? !!isDraggable
: !this.draggable
;
324 if ( this.draggable
!== isDraggable
) {
325 this.draggable
= isDraggable
;
327 // Tell the items their draggable state changed
328 this.getItems().forEach( function ( item
) {
329 item
.toggleDraggable( this.draggable
);
333 this.emit( 'draggable', this.draggable
);
338 * Check the draggable state of this widget
340 * @return {boolean} Widget supports draggable operations
342 OO
.ui
.mixin
.DraggableGroupElement
.prototype.isDraggable = function () {
343 return this.draggable
;
347 * Respond to item drag start event
350 * @param {OO.ui.mixin.DraggableElement} item Dragged item
352 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
353 if ( !this.isDraggable() ) {
356 // Make a shallow copy of this.items so we can re-order it during previews
357 // without affecting the original array.
358 this.itemsOrder
= this.items
.slice();
359 this.updateIndexes();
360 if ( this.orientation
=== 'horizontal' ) {
361 // Calculate and cache directionality on drag start - it's a little
362 // expensive and it shouldn't change while dragging.
363 this.dir
= this.$element
.css( 'direction' );
365 this.setDragItem( item
);
369 * Update the index properties of the items
371 OO
.ui
.mixin
.DraggableGroupElement
.prototype.updateIndexes = function () {
374 // Map the index of each object
375 for ( i
= 0, len
= this.itemsOrder
.length
; i
< len
; i
++ ) {
376 this.itemsOrder
[ i
].setIndex( i
);
381 * Handle drop or dragend event and switch the order of the items accordingly
384 * @param {OO.ui.mixin.DraggableElement} item Dropped item
386 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDropOrDragEnd = function () {
387 var targetIndex
, originalIndex
,
388 item
= this.getDragItem();
390 // TODO: Figure out a way to configure a list of legally droppable
391 // elements even if they are not yet in the list
393 originalIndex
= this.items
.indexOf( item
);
394 // If the item has moved forward, add one to the index to account for the left shift
395 targetIndex
= item
.getIndex() + ( item
.getIndex() > originalIndex
? 1 : 0 );
396 if ( targetIndex
!== originalIndex
) {
397 this.reorder( this.getDragItem(), targetIndex
);
398 this.emit( 'reorder', this.getDragItem(), targetIndex
);
400 this.updateIndexes();
402 this.unsetDragItem();
403 // Return false to prevent propogation
408 * Respond to dragover event
411 * @param {jQuery.Event} e Dragover event
414 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
415 var overIndex
, targetIndex
,
416 item
= this.getDragItem(),
417 dragItemIndex
= item
.getIndex();
419 // Get the OptionWidget item we are dragging over
420 overIndex
= $( e
.target
).closest( '.oo-ui-draggableElement' ).data( 'index' );
422 if ( overIndex
!== undefined && overIndex
!== dragItemIndex
) {
423 targetIndex
= overIndex
+ ( overIndex
> dragItemIndex
? 1 : 0 );
425 if ( targetIndex
> 0 ) {
426 this.$group
.children().eq( targetIndex
- 1 ).after( item
.$element
);
428 this.$group
.prepend( item
.$element
);
430 // Move item in itemsOrder array
431 this.itemsOrder
.splice( overIndex
, 0,
432 this.itemsOrder
.splice( dragItemIndex
, 1 )[ 0 ]
434 this.updateIndexes();
435 this.emit( 'drag', item
, targetIndex
);
442 * Reorder the items in the group
444 * @param {OO.ui.mixin.DraggableElement} item Reordered item
445 * @param {number} newIndex New index
447 OO
.ui
.mixin
.DraggableGroupElement
.prototype.reorder = function ( item
, newIndex
) {
448 this.addItems( [ item
], newIndex
);
454 * @param {OO.ui.mixin.DraggableElement} item Dragged item
456 OO
.ui
.mixin
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
457 if ( this.dragItem
!== item
) {
458 this.dragItem
= item
;
459 this.$element
.on( 'dragover', this.onDragOver
.bind( this ) );
460 this.$element
.addClass( 'oo-ui-draggableGroupElement-dragging' );
465 * Unset the current dragged item
467 OO
.ui
.mixin
.DraggableGroupElement
.prototype.unsetDragItem = function () {
468 if ( this.dragItem
) {
469 this.dragItem
= null;
470 this.$element
.off( 'dragover' );
471 this.$element
.removeClass( 'oo-ui-draggableGroupElement-dragging' );
476 * Get the item that is currently being dragged.
478 * @return {OO.ui.mixin.DraggableElement|null} The currently dragged item, or `null` if no item is being dragged
480 OO
.ui
.mixin
.DraggableGroupElement
.prototype.getDragItem = function () {
481 return this.dragItem
;
485 * RequestManager is a mixin that manages the lifecycle of a promise-backed request for a widget, such as
486 * the {@link OO.ui.mixin.LookupElement}.
493 OO
.ui
.mixin
.RequestManager
= function OoUiMixinRequestManager() {
494 this.requestCache
= {};
495 this.requestQuery
= null;
496 this.requestRequest
= null;
501 OO
.initClass( OO
.ui
.mixin
.RequestManager
);
504 * Get request results for the current query.
506 * @return {jQuery.Promise} Promise object which will be passed response data as the first argument of
507 * the done event. If the request was aborted to make way for a subsequent request, this promise
508 * may not be rejected, depending on what jQuery feels like doing.
510 OO
.ui
.mixin
.RequestManager
.prototype.getRequestData = function () {
512 value
= this.getRequestQuery(),
513 deferred
= $.Deferred(),
517 if ( Object
.prototype.hasOwnProperty
.call( this.requestCache
, value
) ) {
518 deferred
.resolve( this.requestCache
[ value
] );
520 if ( this.pushPending
) {
523 this.requestQuery
= value
;
524 ourRequest
= this.requestRequest
= this.getRequest();
526 .always( function () {
527 // We need to pop pending even if this is an old request, otherwise
528 // the widget will remain pending forever.
529 // TODO: this assumes that an aborted request will fail or succeed soon after
530 // being aborted, or at least eventually. It would be nice if we could popPending()
531 // at abort time, but only if we knew that we hadn't already called popPending()
533 if ( widget
.popPending
) {
537 .done( function ( response
) {
538 // If this is an old request (and aborting it somehow caused it to still succeed),
539 // ignore its success completely
540 if ( ourRequest
=== widget
.requestRequest
) {
541 widget
.requestQuery
= null;
542 widget
.requestRequest
= null;
543 widget
.requestCache
[ value
] = widget
.getRequestCacheDataFromResponse( response
);
544 deferred
.resolve( widget
.requestCache
[ value
] );
548 // If this is an old request (or a request failing because it's being aborted),
549 // ignore its failure completely
550 if ( ourRequest
=== widget
.requestRequest
) {
551 widget
.requestQuery
= null;
552 widget
.requestRequest
= null;
557 return deferred
.promise();
561 * Abort the currently pending request, if any.
565 OO
.ui
.mixin
.RequestManager
.prototype.abortRequest = function () {
566 var oldRequest
= this.requestRequest
;
568 // First unset this.requestRequest to the fail handler will notice
569 // that the request is no longer current
570 this.requestRequest
= null;
571 this.requestQuery
= null;
577 * Get the query to be made.
582 * @return {string} query to be used
584 OO
.ui
.mixin
.RequestManager
.prototype.getRequestQuery
= null;
587 * Get a new request object of the current query value.
592 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
594 OO
.ui
.mixin
.RequestManager
.prototype.getRequest
= null;
597 * Pre-process data returned by the request from #getRequest.
599 * The return value of this function will be cached, and any further queries for the given value
600 * will use the cache rather than doing API requests.
605 * @param {Mixed} response Response from server
606 * @return {Mixed} Cached result data
608 OO
.ui
.mixin
.RequestManager
.prototype.getRequestCacheDataFromResponse
= null;
611 * LookupElement is a mixin that creates a {@link OO.ui.MenuSelectWidget menu} of suggested values for
612 * a {@link OO.ui.TextInputWidget text input widget}. Suggested values are based on the characters the user types
613 * into the text input field and, in general, the menu is only displayed when the user types. If a suggested value is chosen
614 * from the lookup menu, that value becomes the value of the input field.
616 * Note that a new menu of suggested items is displayed when a value is chosen from the lookup menu. If this is
617 * not the desired behavior, disable lookup menus with the #setLookupsDisabled method, then set the value, then
620 * See the [OOjs UI demos][1] for an example.
622 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/index.html#widgets-apex-vector-ltr
626 * @mixins OO.ui.mixin.RequestManager
629 * @param {Object} [config] Configuration options
630 * @cfg {jQuery} [$overlay] Overlay for the lookup menu; defaults to relative positioning.
631 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
632 * @cfg {jQuery} [$container=this.$element] The container element. The lookup menu is rendered beneath the specified element.
633 * @cfg {boolean} [allowSuggestionsWhenEmpty=false] Request and display a lookup menu when the text input is empty.
634 * By default, the lookup menu is not generated and displayed until the user begins to type.
635 * @cfg {boolean} [highlightFirst=true] Whether the first lookup result should be highlighted (so, that the user can
636 * take it over into the input with simply pressing return) automatically or not.
638 OO
.ui
.mixin
.LookupElement
= function OoUiMixinLookupElement( config
) {
639 // Configuration initialization
640 config
= $.extend( { highlightFirst
: true }, config
);
642 // Mixin constructors
643 OO
.ui
.mixin
.RequestManager
.call( this, config
);
646 this.$overlay
= config
.$overlay
|| this.$element
;
647 this.lookupMenu
= new OO
.ui
.MenuSelectWidget( {
650 $floatableContainer
: config
.$container
|| this.$element
653 this.allowSuggestionsWhenEmpty
= config
.allowSuggestionsWhenEmpty
|| false;
655 this.lookupsDisabled
= false;
656 this.lookupInputFocused
= false;
657 this.lookupHighlightFirstItem
= config
.highlightFirst
;
661 focus
: this.onLookupInputFocus
.bind( this ),
662 blur
: this.onLookupInputBlur
.bind( this ),
663 mousedown
: this.onLookupInputMouseDown
.bind( this )
665 this.connect( this, { change
: 'onLookupInputChange' } );
666 this.lookupMenu
.connect( this, {
667 toggle
: 'onLookupMenuToggle',
668 choose
: 'onLookupMenuItemChoose'
674 'aria-owns': this.lookupMenu
.getElementId(),
675 'aria-autocomplete': 'list'
677 this.$element
.addClass( 'oo-ui-lookupElement' );
678 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
679 this.$overlay
.append( this.lookupMenu
.$element
);
684 OO
.mixinClass( OO
.ui
.mixin
.LookupElement
, OO
.ui
.mixin
.RequestManager
);
689 * Handle input focus event.
692 * @param {jQuery.Event} e Input focus event
694 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputFocus = function () {
695 this.lookupInputFocused
= true;
696 this.populateLookupMenu();
700 * Handle input blur event.
703 * @param {jQuery.Event} e Input blur event
705 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputBlur = function () {
706 this.closeLookupMenu();
707 this.lookupInputFocused
= false;
711 * Handle input mouse down event.
714 * @param {jQuery.Event} e Input mouse down event
716 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputMouseDown = function () {
717 // Only open the menu if the input was already focused.
718 // This way we allow the user to open the menu again after closing it with Esc
719 // by clicking in the input. Opening (and populating) the menu when initially
720 // clicking into the input is handled by the focus handler.
721 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
722 this.populateLookupMenu();
727 * Handle input change event.
730 * @param {string} value New input value
732 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputChange = function () {
733 if ( this.lookupInputFocused
) {
734 this.populateLookupMenu();
739 * Handle the lookup menu being shown/hidden.
742 * @param {boolean} visible Whether the lookup menu is now visible.
744 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
746 // When the menu is hidden, abort any active request and clear the menu.
747 // This has to be done here in addition to closeLookupMenu(), because
748 // MenuSelectWidget will close itself when the user presses Esc.
749 this.abortLookupRequest();
750 this.lookupMenu
.clearItems();
755 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
758 * @param {OO.ui.MenuOptionWidget} item Selected item
760 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
761 this.setValue( item
.getData() );
768 * @return {OO.ui.MenuSelectWidget}
770 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenu = function () {
771 return this.lookupMenu
;
775 * Disable or re-enable lookups.
777 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
779 * @param {boolean} disabled Disable lookups
781 OO
.ui
.mixin
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
782 this.lookupsDisabled
= !!disabled
;
786 * Open the menu. If there are no entries in the menu, this does nothing.
791 OO
.ui
.mixin
.LookupElement
.prototype.openLookupMenu = function () {
792 if ( !this.lookupMenu
.isEmpty() ) {
793 this.lookupMenu
.toggle( true );
799 * Close the menu, empty it, and abort any pending request.
804 OO
.ui
.mixin
.LookupElement
.prototype.closeLookupMenu = function () {
805 this.lookupMenu
.toggle( false );
806 this.abortLookupRequest();
807 this.lookupMenu
.clearItems();
812 * Request menu items based on the input's current value, and when they arrive,
813 * populate the menu with these items and show the menu.
815 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
820 OO
.ui
.mixin
.LookupElement
.prototype.populateLookupMenu = function () {
822 value
= this.getValue();
824 if ( this.lookupsDisabled
|| this.isReadOnly() ) {
828 // If the input is empty, clear the menu, unless suggestions when empty are allowed.
829 if ( !this.allowSuggestionsWhenEmpty
&& value
=== '' ) {
830 this.closeLookupMenu();
831 // Skip population if there is already a request pending for the current value
832 } else if ( value
!== this.lookupQuery
) {
833 this.getLookupMenuItems()
834 .done( function ( items
) {
835 widget
.lookupMenu
.clearItems();
836 if ( items
.length
) {
840 widget
.initializeLookupMenuSelection();
842 widget
.lookupMenu
.toggle( false );
846 widget
.lookupMenu
.clearItems();
854 * Highlight the first selectable item in the menu, if configured.
859 OO
.ui
.mixin
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
860 if ( this.lookupHighlightFirstItem
&& !this.lookupMenu
.getSelectedItem() ) {
861 this.lookupMenu
.highlightItem( this.lookupMenu
.getFirstSelectableItem() );
866 * Get lookup menu items for the current query.
869 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
870 * the done event. If the request was aborted to make way for a subsequent request, this promise
871 * will not be rejected: it will remain pending forever.
873 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuItems = function () {
874 return this.getRequestData().then( function ( data
) {
875 return this.getLookupMenuOptionsFromData( data
);
880 * Abort the currently pending lookup request, if any.
884 OO
.ui
.mixin
.LookupElement
.prototype.abortLookupRequest = function () {
889 * Get a new request object of the current lookup query value.
894 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
896 OO
.ui
.mixin
.LookupElement
.prototype.getLookupRequest
= null;
899 * Pre-process data returned by the request from #getLookupRequest.
901 * The return value of this function will be cached, and any further queries for the given value
902 * will use the cache rather than doing API requests.
907 * @param {Mixed} response Response from server
908 * @return {Mixed} Cached result data
910 OO
.ui
.mixin
.LookupElement
.prototype.getLookupCacheDataFromResponse
= null;
913 * Get a list of menu option widgets from the (possibly cached) data returned by
914 * #getLookupCacheDataFromResponse.
919 * @param {Mixed} data Cached result data, usually an array
920 * @return {OO.ui.MenuOptionWidget[]} Menu items
922 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuOptionsFromData
= null;
925 * Set the read-only state of the widget.
927 * This will also disable/enable the lookups functionality.
929 * @param {boolean} readOnly Make input read-only
932 OO
.ui
.mixin
.LookupElement
.prototype.setReadOnly = function ( readOnly
) {
934 // Note: Calling #setReadOnly this way assumes this is mixed into an OO.ui.TextInputWidget
935 OO
.ui
.TextInputWidget
.prototype.setReadOnly
.call( this, readOnly
);
937 // During construction, #setReadOnly is called before the OO.ui.mixin.LookupElement constructor
938 if ( this.isReadOnly() && this.lookupMenu
) {
939 this.closeLookupMenu();
946 * @inheritdoc OO.ui.mixin.RequestManager
948 OO
.ui
.mixin
.LookupElement
.prototype.getRequestQuery = function () {
949 return this.getValue();
953 * @inheritdoc OO.ui.mixin.RequestManager
955 OO
.ui
.mixin
.LookupElement
.prototype.getRequest = function () {
956 return this.getLookupRequest();
960 * @inheritdoc OO.ui.mixin.RequestManager
962 OO
.ui
.mixin
.LookupElement
.prototype.getRequestCacheDataFromResponse = function ( response
) {
963 return this.getLookupCacheDataFromResponse( response
);
967 * TabPanelLayouts are used within {@link OO.ui.IndexLayout index layouts} to create tab panels that
968 * users can select and display from the index's optional {@link OO.ui.TabSelectWidget tab}
969 * navigation. TabPanels are usually not instantiated directly, rather extended to include the
970 * required content and functionality.
972 * Each tab panel must have a unique symbolic name, which is passed to the constructor. In addition,
973 * the tab panel's tab item is customized (with a label) using the #setupTabItem method. See
974 * {@link OO.ui.IndexLayout IndexLayout} for an example.
977 * @extends OO.ui.PanelLayout
980 * @param {string} name Unique symbolic name of tab panel
981 * @param {Object} [config] Configuration options
982 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] Label for tab panel's tab
984 OO
.ui
.TabPanelLayout
= function OoUiTabPanelLayout( name
, config
) {
985 // Allow passing positional parameters inside the config object
986 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
991 // Configuration initialization
992 config
= $.extend( { scrollable
: true }, config
);
994 // Parent constructor
995 OO
.ui
.TabPanelLayout
.parent
.call( this, config
);
999 this.label
= config
.label
;
1000 this.tabItem
= null;
1001 this.active
= false;
1004 this.$element
.addClass( 'oo-ui-tabPanelLayout' );
1009 OO
.inheritClass( OO
.ui
.TabPanelLayout
, OO
.ui
.PanelLayout
);
1014 * An 'active' event is emitted when the tab panel becomes active. Tab panels become active when they are
1015 * shown in a index layout that is configured to display only one tab panel at a time.
1018 * @param {boolean} active Tab panel is active
1024 * Get the symbolic name of the tab panel.
1026 * @return {string} Symbolic name of tab panel
1028 OO
.ui
.TabPanelLayout
.prototype.getName = function () {
1033 * Check if tab panel is active.
1035 * Tab panels become active when they are shown in a {@link OO.ui.IndexLayout index layout} that is configured to
1036 * display only one tab panel at a time. Additional CSS is applied to the tab panel's tab item to reflect the
1039 * @return {boolean} Tab panel is active
1041 OO
.ui
.TabPanelLayout
.prototype.isActive = function () {
1048 * The tab item allows users to access the tab panel from the index's tab
1049 * navigation. The tab item itself can be customized (with a label, level, etc.) using the #setupTabItem method.
1051 * @return {OO.ui.TabOptionWidget|null} Tab option widget
1053 OO
.ui
.TabPanelLayout
.prototype.getTabItem = function () {
1054 return this.tabItem
;
1058 * Set or unset the tab item.
1060 * Specify a {@link OO.ui.TabOptionWidget tab option} to set it,
1061 * or `null` to clear the tab item. To customize the tab item itself (e.g., to set a label or tab
1062 * level), use #setupTabItem instead of this method.
1064 * @param {OO.ui.TabOptionWidget|null} tabItem Tab option widget, null to clear
1067 OO
.ui
.TabPanelLayout
.prototype.setTabItem = function ( tabItem
) {
1068 this.tabItem
= tabItem
|| null;
1070 this.setupTabItem();
1076 * Set up the tab item.
1078 * Use this method to customize the tab item (e.g., to add a label or tab level). To set or unset
1079 * the tab item itself (with a {@link OO.ui.TabOptionWidget tab option} or `null`), use
1080 * the #setTabItem method instead.
1082 * @param {OO.ui.TabOptionWidget} tabItem Tab option widget to set up
1085 OO
.ui
.TabPanelLayout
.prototype.setupTabItem = function () {
1087 this.tabItem
.setLabel( this.label
);
1093 * Set the tab panel to its 'active' state.
1095 * Tab panels become active when they are shown in a index layout that is configured to display only
1096 * one tab panel at a time. Additional CSS is applied to the tab item to reflect the tab panel's
1097 * active state. Outside of the index context, setting the active state on a tab panel does nothing.
1099 * @param {boolean} active Tab panel is active
1102 OO
.ui
.TabPanelLayout
.prototype.setActive = function ( active
) {
1105 if ( active
!== this.active
) {
1106 this.active
= active
;
1107 this.$element
.toggleClass( 'oo-ui-tabPanelLayout-active', this.active
);
1108 this.emit( 'active', this.active
);
1113 * The deprecated name for the TabPanelLayout, provided for backwards-compatibility.
1116 * @extends OO.ui.TabPanelLayout
1119 * @deprecated since v0.21.3
1121 OO
.ui
.CardLayout
= function OoUiCardLayout() {
1122 OO
.ui
.warnDeprecation( 'CardLayout has been renamed to TabPanel layout. Use that instead. See T155152' );
1123 // Parent constructor
1124 OO
.ui
.CardLayout
.parent
.apply( this, arguments
);
1127 OO
.inheritClass( OO
.ui
.CardLayout
, OO
.ui
.TabPanelLayout
);
1130 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that users can select and display
1131 * from the booklet's optional {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated directly,
1132 * rather extended to include the required content and functionality.
1134 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the page's outline
1135 * item is customized (with a label, outline level, etc.) using the #setupOutlineItem method. See
1136 * {@link OO.ui.BookletLayout BookletLayout} for an example.
1139 * @extends OO.ui.PanelLayout
1142 * @param {string} name Unique symbolic name of page
1143 * @param {Object} [config] Configuration options
1145 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
1146 // Allow passing positional parameters inside the config object
1147 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1152 // Configuration initialization
1153 config
= $.extend( { scrollable
: true }, config
);
1155 // Parent constructor
1156 OO
.ui
.PageLayout
.parent
.call( this, config
);
1160 this.outlineItem
= null;
1161 this.active
= false;
1164 this.$element
.addClass( 'oo-ui-pageLayout' );
1169 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
1174 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1175 * shown in a booklet layout that is configured to display only one page at a time.
1178 * @param {boolean} active Page is active
1184 * Get the symbolic name of the page.
1186 * @return {string} Symbolic name of page
1188 OO
.ui
.PageLayout
.prototype.getName = function () {
1193 * Check if page is active.
1195 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is configured to display
1196 * only one page at a time. Additional CSS is applied to the page's outline item to reflect the active state.
1198 * @return {boolean} Page is active
1200 OO
.ui
.PageLayout
.prototype.isActive = function () {
1207 * The outline item allows users to access the page from the booklet's outline
1208 * navigation. The outline item itself can be customized (with a label, level, etc.) using the #setupOutlineItem method.
1210 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1212 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
1213 return this.outlineItem
;
1217 * Set or unset the outline item.
1219 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1220 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label or outline
1221 * level), use #setupOutlineItem instead of this method.
1223 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1226 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
1227 this.outlineItem
= outlineItem
|| null;
1228 if ( outlineItem
) {
1229 this.setupOutlineItem();
1235 * Set up the outline item.
1237 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or unset
1238 * the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or `null`), use
1239 * the #setOutlineItem method instead.
1241 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1244 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
1249 * Set the page to its 'active' state.
1251 * Pages become active when they are shown in a booklet layout that is configured to display only one page at a time. Additional
1252 * CSS is applied to the outline item to reflect the page's active state. Outside of the booklet
1253 * context, setting the active state on a page does nothing.
1255 * @param {boolean} active Page is active
1258 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
1261 if ( active
!== this.active
) {
1262 this.active
= active
;
1263 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
1264 this.emit( 'active', this.active
);
1269 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one panel is displayed
1270 * at a time, though the stack layout can also be configured to show all contained panels, one after another,
1271 * by setting the #continuous option to 'true'.
1274 * // A stack layout with two panels, configured to be displayed continously
1275 * var myStack = new OO.ui.StackLayout( {
1277 * new OO.ui.PanelLayout( {
1278 * $content: $( '<p>Panel One</p>' ),
1282 * new OO.ui.PanelLayout( {
1283 * $content: $( '<p>Panel Two</p>' ),
1290 * $( 'body' ).append( myStack.$element );
1293 * @extends OO.ui.PanelLayout
1294 * @mixins OO.ui.mixin.GroupElement
1297 * @param {Object} [config] Configuration options
1298 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel is displayed at a time.
1299 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1301 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
1302 // Configuration initialization
1303 config
= $.extend( { scrollable
: true }, config
);
1305 // Parent constructor
1306 OO
.ui
.StackLayout
.parent
.call( this, config
);
1308 // Mixin constructors
1309 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
1312 this.currentItem
= null;
1313 this.continuous
= !!config
.continuous
;
1316 this.$element
.addClass( 'oo-ui-stackLayout' );
1317 if ( this.continuous
) {
1318 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
1319 this.$element
.on( 'scroll', OO
.ui
.debounce( this.onScroll
.bind( this ), 250 ) );
1321 if ( Array
.isArray( config
.items
) ) {
1322 this.addItems( config
.items
);
1328 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
1329 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.mixin
.GroupElement
);
1334 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1335 * {@link #clearItems cleared} or {@link #setItem displayed}.
1338 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1342 * When used in continuous mode, this event is emitted when the user scrolls down
1343 * far enough such that currentItem is no longer visible.
1345 * @event visibleItemChange
1346 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1352 * Handle scroll events from the layout element
1354 * @param {jQuery.Event} e
1355 * @fires visibleItemChange
1357 OO
.ui
.StackLayout
.prototype.onScroll = function () {
1359 len
= this.items
.length
,
1360 currentIndex
= this.items
.indexOf( this.currentItem
),
1361 newIndex
= currentIndex
,
1362 containerRect
= this.$element
[ 0 ].getBoundingClientRect();
1364 if ( !containerRect
|| ( !containerRect
.top
&& !containerRect
.bottom
) ) {
1365 // Can't get bounding rect, possibly not attached.
1369 function getRect( item
) {
1370 return item
.$element
[ 0 ].getBoundingClientRect();
1373 function isVisible( item
) {
1374 var rect
= getRect( item
);
1375 return rect
.bottom
> containerRect
.top
&& rect
.top
< containerRect
.bottom
;
1378 currentRect
= getRect( this.currentItem
);
1380 if ( currentRect
.bottom
< containerRect
.top
) {
1381 // Scrolled down past current item
1382 while ( ++newIndex
< len
) {
1383 if ( isVisible( this.items
[ newIndex
] ) ) {
1387 } else if ( currentRect
.top
> containerRect
.bottom
) {
1388 // Scrolled up past current item
1389 while ( --newIndex
>= 0 ) {
1390 if ( isVisible( this.items
[ newIndex
] ) ) {
1396 if ( newIndex
!== currentIndex
) {
1397 this.emit( 'visibleItemChange', this.items
[ newIndex
] );
1402 * Get the current panel.
1404 * @return {OO.ui.Layout|null}
1406 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
1407 return this.currentItem
;
1411 * Unset the current item.
1414 * @param {OO.ui.StackLayout} layout
1417 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
1418 var prevItem
= this.currentItem
;
1419 if ( prevItem
=== null ) {
1423 this.currentItem
= null;
1424 this.emit( 'set', null );
1428 * Add panel layouts to the stack layout.
1430 * Panels will be added to the end of the stack layout array unless the optional index parameter specifies a different
1431 * insertion point. Adding a panel that is already in the stack will move it to the end of the array or the point specified
1434 * @param {OO.ui.Layout[]} items Panels to add
1435 * @param {number} [index] Index of the insertion point
1438 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
1439 // Update the visibility
1440 this.updateHiddenState( items
, this.currentItem
);
1443 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
, index
);
1445 if ( !this.currentItem
&& items
.length
) {
1446 this.setItem( items
[ 0 ] );
1453 * Remove the specified panels from the stack layout.
1455 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all panels,
1456 * you may wish to use the #clearItems method instead.
1458 * @param {OO.ui.Layout[]} items Panels to remove
1462 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
1464 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
1466 if ( items
.indexOf( this.currentItem
) !== -1 ) {
1467 if ( this.items
.length
) {
1468 this.setItem( this.items
[ 0 ] );
1470 this.unsetCurrentItem();
1478 * Clear all panels from the stack layout.
1480 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1481 * a subset of panels, use the #removeItems method.
1486 OO
.ui
.StackLayout
.prototype.clearItems = function () {
1487 this.unsetCurrentItem();
1488 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
1494 * Show the specified panel.
1496 * If another panel is currently displayed, it will be hidden.
1498 * @param {OO.ui.Layout} item Panel to show
1502 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
1503 if ( item
!== this.currentItem
) {
1504 this.updateHiddenState( this.items
, item
);
1506 if ( this.items
.indexOf( item
) !== -1 ) {
1507 this.currentItem
= item
;
1508 this.emit( 'set', item
);
1510 this.unsetCurrentItem();
1518 * Update the visibility of all items in case of non-continuous view.
1520 * Ensure all items are hidden except for the selected one.
1521 * This method does nothing when the stack is continuous.
1524 * @param {OO.ui.Layout[]} items Item list iterate over
1525 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1527 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
1530 if ( !this.continuous
) {
1531 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
1532 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
1533 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
1534 items
[ i
].$element
.attr( 'aria-hidden', 'true' );
1537 if ( selectedItem
) {
1538 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
1539 selectedItem
.$element
.removeAttr( 'aria-hidden' );
1545 * 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)
1546 * and its size is customized with the #menuSize config. The content area will fill all remaining space.
1549 * var menuLayout = new OO.ui.MenuLayout( {
1552 * menuPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1553 * contentPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1554 * select = new OO.ui.SelectWidget( {
1556 * new OO.ui.OptionWidget( {
1560 * new OO.ui.OptionWidget( {
1564 * new OO.ui.OptionWidget( {
1568 * new OO.ui.OptionWidget( {
1573 * } ).on( 'select', function ( item ) {
1574 * menuLayout.setMenuPosition( item.getData() );
1577 * menuLayout.$menu.append(
1578 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element )
1580 * menuLayout.$content.append(
1581 * contentPanel.$element.append( '<b>Content panel</b>', '<p>Note that the menu is positioned relative to the content panel: top, bottom, after, before.</p>')
1583 * $( 'body' ).append( menuLayout.$element );
1585 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1586 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1587 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1590 * .oo-ui-menuLayout-menu {
1594 * .oo-ui-menuLayout-content {
1602 * @extends OO.ui.Layout
1605 * @param {Object} [config] Configuration options
1606 * @cfg {boolean} [showMenu=true] Show menu
1607 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1609 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
1610 // Configuration initialization
1611 config
= $.extend( {
1613 menuPosition
: 'before'
1616 // Parent constructor
1617 OO
.ui
.MenuLayout
.parent
.call( this, config
);
1622 * @property {jQuery}
1624 this.$menu
= $( '<div>' );
1628 * @property {jQuery}
1630 this.$content
= $( '<div>' );
1634 .addClass( 'oo-ui-menuLayout-menu' );
1635 this.$content
.addClass( 'oo-ui-menuLayout-content' );
1637 .addClass( 'oo-ui-menuLayout' )
1638 .append( this.$content
, this.$menu
);
1639 this.setMenuPosition( config
.menuPosition
);
1640 this.toggleMenu( config
.showMenu
);
1645 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
1652 * @param {boolean} showMenu Show menu, omit to toggle
1655 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
1656 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
1658 if ( this.showMenu
!== showMenu
) {
1659 this.showMenu
= showMenu
;
1661 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu
)
1662 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu
);
1663 this.$menu
.attr( 'aria-hidden', this.showMenu
? 'false' : 'true' );
1670 * Check if menu is visible
1672 * @return {boolean} Menu is visible
1674 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
1675 return this.showMenu
;
1679 * Set menu position.
1681 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1682 * @throws {Error} If position value is not supported
1685 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
1686 this.$element
.removeClass( 'oo-ui-menuLayout-' + this.menuPosition
);
1687 this.menuPosition
= position
;
1688 this.$element
.addClass( 'oo-ui-menuLayout-' + position
);
1694 * Get menu position.
1696 * @return {string} Menu position
1698 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
1699 return this.menuPosition
;
1703 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1704 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1705 * through the pages and select which one to display. By default, only one page is
1706 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1707 * the booklet layout automatically focuses on the first focusable element, unless the
1708 * default setting is changed. Optionally, booklets can be configured to show
1709 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1712 * // Example of a BookletLayout that contains two PageLayouts.
1714 * function PageOneLayout( name, config ) {
1715 * PageOneLayout.parent.call( this, name, config );
1716 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on the left)</p>' );
1718 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1719 * PageOneLayout.prototype.setupOutlineItem = function () {
1720 * this.outlineItem.setLabel( 'Page One' );
1723 * function PageTwoLayout( name, config ) {
1724 * PageTwoLayout.parent.call( this, name, config );
1725 * this.$element.append( '<p>Second page</p>' );
1727 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1728 * PageTwoLayout.prototype.setupOutlineItem = function () {
1729 * this.outlineItem.setLabel( 'Page Two' );
1732 * var page1 = new PageOneLayout( 'one' ),
1733 * page2 = new PageTwoLayout( 'two' );
1735 * var booklet = new OO.ui.BookletLayout( {
1739 * booklet.addPages ( [ page1, page2 ] );
1740 * $( 'body' ).append( booklet.$element );
1743 * @extends OO.ui.MenuLayout
1746 * @param {Object} [config] Configuration options
1747 * @cfg {boolean} [continuous=false] Show all pages, one after another
1748 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is displayed. Disabled on mobile.
1749 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the pages of the booklet.
1750 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
1752 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
1753 // Configuration initialization
1754 config
= config
|| {};
1756 // Parent constructor
1757 OO
.ui
.BookletLayout
.parent
.call( this, config
);
1760 this.currentPageName
= null;
1762 this.ignoreFocus
= false;
1763 this.stackLayout
= new OO
.ui
.StackLayout( { continuous
: !!config
.continuous
} );
1764 this.$content
.append( this.stackLayout
.$element
);
1765 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
1766 this.outlineVisible
= false;
1767 this.outlined
= !!config
.outlined
;
1768 if ( this.outlined
) {
1769 this.editable
= !!config
.editable
;
1770 this.outlineControlsWidget
= null;
1771 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget();
1772 this.outlinePanel
= new OO
.ui
.PanelLayout( { scrollable
: true } );
1773 this.$menu
.append( this.outlinePanel
.$element
);
1774 this.outlineVisible
= true;
1775 if ( this.editable
) {
1776 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
1777 this.outlineSelectWidget
1781 this.toggleMenu( this.outlined
);
1784 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
1785 if ( this.outlined
) {
1786 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
1787 this.scrolling
= false;
1788 this.stackLayout
.connect( this, { visibleItemChange
: 'onStackLayoutVisibleItemChange' } );
1790 if ( this.autoFocus
) {
1791 // Event 'focus' does not bubble, but 'focusin' does
1792 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
1796 this.$element
.addClass( 'oo-ui-bookletLayout' );
1797 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
1798 if ( this.outlined
) {
1799 this.outlinePanel
.$element
1800 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1801 .append( this.outlineSelectWidget
.$element
);
1802 if ( this.editable
) {
1803 this.outlinePanel
.$element
1804 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1805 .append( this.outlineControlsWidget
.$element
);
1812 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
1817 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the booklet layout.
1819 * @param {OO.ui.PageLayout} page Current page
1823 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
1826 * @param {OO.ui.PageLayout[]} page Added pages
1827 * @param {number} index Index pages were added at
1831 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
1832 * {@link #removePages removed} from the booklet.
1835 * @param {OO.ui.PageLayout[]} pages Removed pages
1841 * Handle stack layout focus.
1844 * @param {jQuery.Event} e Focusin event
1846 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
1849 // Find the page that an element was focused within
1850 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
1851 for ( name
in this.pages
) {
1852 // Check for page match, exclude current page to find only page changes
1853 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
1854 this.setPage( name
);
1861 * Handle visibleItemChange events from the stackLayout
1863 * The next visible page is set as the current page by selecting it
1866 * @param {OO.ui.PageLayout} page The next visible page in the layout
1868 OO
.ui
.BookletLayout
.prototype.onStackLayoutVisibleItemChange = function ( page
) {
1869 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
1870 // try and scroll the item into view again.
1871 this.scrolling
= true;
1872 this.outlineSelectWidget
.selectItemByData( page
.getName() );
1873 this.scrolling
= false;
1877 * Handle stack layout set events.
1880 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
1882 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
1884 if ( !this.scrolling
&& page
) {
1885 page
.scrollElementIntoView().done( function () {
1886 if ( layout
.autoFocus
&& !OO
.ui
.isMobile() ) {
1894 * Focus the first input in the current page.
1896 * If no page is selected, the first selectable page will be selected.
1897 * If the focus is already in an element on the current page, nothing will happen.
1899 * @param {number} [itemIndex] A specific item to focus on
1901 OO
.ui
.BookletLayout
.prototype.focus = function ( itemIndex
) {
1903 items
= this.stackLayout
.getItems();
1905 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
1906 page
= items
[ itemIndex
];
1908 page
= this.stackLayout
.getCurrentItem();
1911 if ( !page
&& this.outlined
) {
1912 this.selectFirstSelectablePage();
1913 page
= this.stackLayout
.getCurrentItem();
1918 // Only change the focus if is not already in the current page
1919 if ( !OO
.ui
.contains( page
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
1925 * Find the first focusable input in the booklet layout and focus
1928 OO
.ui
.BookletLayout
.prototype.focusFirstFocusable = function () {
1929 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
1933 * Handle outline widget select events.
1936 * @param {OO.ui.OptionWidget|null} item Selected item
1938 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
1940 this.setPage( item
.getData() );
1945 * Check if booklet has an outline.
1947 * @return {boolean} Booklet has an outline
1949 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
1950 return this.outlined
;
1954 * Check if booklet has editing controls.
1956 * @return {boolean} Booklet is editable
1958 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
1959 return this.editable
;
1963 * Check if booklet has a visible outline.
1965 * @return {boolean} Outline is visible
1967 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
1968 return this.outlined
&& this.outlineVisible
;
1972 * Hide or show the outline.
1974 * @param {boolean} [show] Show outline, omit to invert current state
1977 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
1980 if ( this.outlined
) {
1981 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
1982 this.outlineVisible
= show
;
1983 this.toggleMenu( show
);
1984 if ( show
&& this.editable
) {
1985 // HACK: When the sidebar stops animating, kill dumb scrollbars (T161798). Only necessary when
1986 // outline controls are present, The delay matches transition on `.oo-ui-menuLayout-menu`.
1987 setTimeout( function () {
1988 OO
.ui
.Element
.static.reconsiderScrollbars( booklet
.outlinePanel
.$element
[ 0 ] );
1997 * Get the page closest to the specified page.
1999 * @param {OO.ui.PageLayout} page Page to use as a reference point
2000 * @return {OO.ui.PageLayout|null} Page closest to the specified page
2002 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
2003 var next
, prev
, level
,
2004 pages
= this.stackLayout
.getItems(),
2005 index
= pages
.indexOf( page
);
2007 if ( index
!== -1 ) {
2008 next
= pages
[ index
+ 1 ];
2009 prev
= pages
[ index
- 1 ];
2010 // Prefer adjacent pages at the same level
2011 if ( this.outlined
) {
2012 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
2015 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
2021 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
2027 return prev
|| next
|| null;
2031 * Get the outline widget.
2033 * If the booklet is not outlined, the method will return `null`.
2035 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
2037 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
2038 return this.outlineSelectWidget
;
2042 * Get the outline controls widget.
2044 * If the outline is not editable, the method will return `null`.
2046 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
2048 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
2049 return this.outlineControlsWidget
;
2053 * Get a page by its symbolic name.
2055 * @param {string} name Symbolic name of page
2056 * @return {OO.ui.PageLayout|undefined} Page, if found
2058 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
2059 return this.pages
[ name
];
2063 * Get the current page.
2065 * @return {OO.ui.PageLayout|undefined} Current page, if found
2067 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
2068 var name
= this.getCurrentPageName();
2069 return name
? this.getPage( name
) : undefined;
2073 * Get the symbolic name of the current page.
2075 * @return {string|null} Symbolic name of the current page
2077 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
2078 return this.currentPageName
;
2082 * Add pages to the booklet layout
2084 * When pages are added with the same names as existing pages, the existing pages will be
2085 * automatically removed before the new pages are added.
2087 * @param {OO.ui.PageLayout[]} pages Pages to add
2088 * @param {number} index Index of the insertion point
2092 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
2093 var i
, len
, name
, page
, item
, currentIndex
,
2094 stackLayoutPages
= this.stackLayout
.getItems(),
2098 // Remove pages with same names
2099 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2101 name
= page
.getName();
2103 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
2104 // Correct the insertion index
2105 currentIndex
= stackLayoutPages
.indexOf( this.pages
[ name
] );
2106 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2109 remove
.push( this.pages
[ name
] );
2112 if ( remove
.length
) {
2113 this.removePages( remove
);
2117 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2119 name
= page
.getName();
2120 this.pages
[ page
.getName() ] = page
;
2121 if ( this.outlined
) {
2122 item
= new OO
.ui
.OutlineOptionWidget( { data
: name
} );
2123 page
.setOutlineItem( item
);
2128 if ( this.outlined
&& items
.length
) {
2129 this.outlineSelectWidget
.addItems( items
, index
);
2130 this.selectFirstSelectablePage();
2132 this.stackLayout
.addItems( pages
, index
);
2133 this.emit( 'add', pages
, index
);
2139 * Remove the specified pages from the booklet layout.
2141 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2143 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2147 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
2148 var i
, len
, name
, page
,
2151 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2153 name
= page
.getName();
2154 delete this.pages
[ name
];
2155 if ( this.outlined
) {
2156 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
2157 page
.setOutlineItem( null );
2160 if ( this.outlined
&& items
.length
) {
2161 this.outlineSelectWidget
.removeItems( items
);
2162 this.selectFirstSelectablePage();
2164 this.stackLayout
.removeItems( pages
);
2165 this.emit( 'remove', pages
);
2171 * Clear all pages from the booklet layout.
2173 * To remove only a subset of pages from the booklet, use the #removePages method.
2178 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
2180 pages
= this.stackLayout
.getItems();
2183 this.currentPageName
= null;
2184 if ( this.outlined
) {
2185 this.outlineSelectWidget
.clearItems();
2186 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2187 pages
[ i
].setOutlineItem( null );
2190 this.stackLayout
.clearItems();
2192 this.emit( 'remove', pages
);
2198 * Set the current page by symbolic name.
2201 * @param {string} name Symbolic name of page
2203 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
2206 page
= this.pages
[ name
],
2207 previousPage
= this.currentPageName
&& this.pages
[ this.currentPageName
];
2209 if ( name
!== this.currentPageName
) {
2210 if ( this.outlined
) {
2211 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
2212 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2213 this.outlineSelectWidget
.selectItemByData( name
);
2217 if ( previousPage
) {
2218 previousPage
.setActive( false );
2219 // Blur anything focused if the next page doesn't have anything focusable.
2220 // This is not needed if the next page has something focusable (because once it is focused
2221 // this blur happens automatically). If the layout is non-continuous, this check is
2222 // meaningless because the next page is not visible yet and thus can't hold focus.
2225 !OO
.ui
.isMobile() &&
2226 this.stackLayout
.continuous
&&
2227 OO
.ui
.findFocusable( page
.$element
).length
!== 0
2229 $focused
= previousPage
.$element
.find( ':focus' );
2230 if ( $focused
.length
) {
2231 $focused
[ 0 ].blur();
2235 this.currentPageName
= name
;
2236 page
.setActive( true );
2237 this.stackLayout
.setItem( page
);
2238 if ( !this.stackLayout
.continuous
&& previousPage
) {
2239 // This should not be necessary, since any inputs on the previous page should have been
2240 // blurred when it was hidden, but browsers are not very consistent about this.
2241 $focused
= previousPage
.$element
.find( ':focus' );
2242 if ( $focused
.length
) {
2243 $focused
[ 0 ].blur();
2246 this.emit( 'set', page
);
2252 * Select the first selectable page.
2256 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
2257 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
2258 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
2265 * IndexLayouts contain {@link OO.ui.TabPanelLayout tab panel layouts} as well as
2266 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the tab panels and
2267 * select which one to display. By default, only one tab panel is displayed at a time. When a user
2268 * navigates to a new tab panel, the index layout automatically focuses on the first focusable element,
2269 * unless the default setting is changed.
2271 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2274 * // Example of a IndexLayout that contains two TabPanelLayouts.
2276 * function TabPanelOneLayout( name, config ) {
2277 * TabPanelOneLayout.parent.call( this, name, config );
2278 * this.$element.append( '<p>First tab panel</p>' );
2280 * OO.inheritClass( TabPanelOneLayout, OO.ui.TabPanelLayout );
2281 * TabPanelOneLayout.prototype.setupTabItem = function () {
2282 * this.tabItem.setLabel( 'Tab panel one' );
2285 * var tabPanel1 = new TabPanelOneLayout( 'one' ),
2286 * tabPanel2 = new OO.ui.TabPanelLayout( 'two', { label: 'Tab panel two' } );
2288 * tabPanel2.$element.append( '<p>Second tab panel</p>' );
2290 * var index = new OO.ui.IndexLayout();
2292 * index.addTabPanels ( [ tabPanel1, tabPanel2 ] );
2293 * $( 'body' ).append( index.$element );
2296 * @extends OO.ui.MenuLayout
2299 * @param {Object} [config] Configuration options
2300 * @cfg {boolean} [continuous=false] Show all tab panels, one after another
2301 * @cfg {boolean} [expanded=true] Expand the content panel to fill the entire parent element.
2302 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new tab panel is displayed. Disabled on mobile.
2304 OO
.ui
.IndexLayout
= function OoUiIndexLayout( config
) {
2305 // Configuration initialization
2306 config
= $.extend( {}, config
, { menuPosition
: 'top' } );
2308 // Parent constructor
2309 OO
.ui
.IndexLayout
.parent
.call( this, config
);
2312 this.currentTabPanelName
= null;
2313 this.tabPanels
= {};
2315 Object
.defineProperty( this, 'currentCardName', {
2316 // TODO: read documentation
2320 OO
.ui
.warnDeprecation( 'IndexLayout\'s currentCardName property is deprecated. Use currentTabPanelName instead. See T155152' );
2321 return this.currentTabPanelName
;
2323 set: function ( value
) {
2324 OO
.ui
.warnDeprecation( 'IndexLayout\'s currentCardName property is deprecated. Use currentTabPanelName instead. See T155152' );
2325 this.currentTabPanelName
= value
;
2329 Object
.defineProperty( this, 'cards', {
2330 // TODO: read documentation
2334 OO
.ui
.warnDeprecation( 'IndexLayout\'s cards property is deprecated. Use tabPanels instead. See T155152' );
2335 return this.tabPanels
;
2337 set: function ( value
) {
2338 OO
.ui
.warnDeprecation( 'IndexLayout\'s cards property is deprecated. Use tabPanels instead. See T155152' );
2339 this.tabPanels
= value
;
2343 this.ignoreFocus
= false;
2344 this.stackLayout
= new OO
.ui
.StackLayout( {
2345 continuous
: !!config
.continuous
,
2346 expanded
: config
.expanded
2348 this.$content
.append( this.stackLayout
.$element
);
2349 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
2351 this.tabSelectWidget
= new OO
.ui
.TabSelectWidget();
2352 this.tabPanel
= new OO
.ui
.PanelLayout();
2353 this.$menu
.append( this.tabPanel
.$element
);
2355 this.toggleMenu( true );
2358 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
2359 this.tabSelectWidget
.connect( this, { select
: 'onTabSelectWidgetSelect' } );
2360 if ( this.autoFocus
) {
2361 // Event 'focus' does not bubble, but 'focusin' does
2362 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
2366 this.$element
.addClass( 'oo-ui-indexLayout' );
2367 this.stackLayout
.$element
.addClass( 'oo-ui-indexLayout-stackLayout' );
2368 this.tabPanel
.$element
2369 .addClass( 'oo-ui-indexLayout-tabPanel' )
2370 .append( this.tabSelectWidget
.$element
);
2375 OO
.inheritClass( OO
.ui
.IndexLayout
, OO
.ui
.MenuLayout
);
2380 * A 'set' event is emitted when a tab panel is {@link #setTabPanel set} to be displayed by the index layout.
2382 * @param {OO.ui.TabPanelLayout} tabPanel Current tab panel
2386 * An 'add' event is emitted when tab panels are {@link #addTabPanels added} to the index layout.
2389 * @param {OO.ui.TabPanelLayout[]} tabPanel Added tab panels
2390 * @param {number} index Index tab panels were added at
2394 * A 'remove' event is emitted when tab panels are {@link #clearTabPanels cleared} or
2395 * {@link #removeTabPanels removed} from the index.
2398 * @param {OO.ui.TabPanelLayout[]} tabPanel Removed tab panels
2404 * Handle stack layout focus.
2407 * @param {jQuery.Event} e Focusing event
2409 OO
.ui
.IndexLayout
.prototype.onStackLayoutFocus = function ( e
) {
2412 // Find the tab panel that an element was focused within
2413 $target
= $( e
.target
).closest( '.oo-ui-tabPanelLayout' );
2414 for ( name
in this.tabPanels
) {
2415 // Check for tab panel match, exclude current tab panel to find only tab panel changes
2416 if ( this.tabPanels
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentTabPanelName
) {
2417 this.setTabPanel( name
);
2424 * Handle stack layout set events.
2427 * @param {OO.ui.PanelLayout|null} tabPanel The tab panel that is now the current panel
2429 OO
.ui
.IndexLayout
.prototype.onStackLayoutSet = function ( tabPanel
) {
2432 tabPanel
.scrollElementIntoView().done( function () {
2433 if ( layout
.autoFocus
&& !OO
.ui
.isMobile() ) {
2441 * Focus the first input in the current tab panel.
2443 * If no tab panel is selected, the first selectable tab panel will be selected.
2444 * If the focus is already in an element on the current tab panel, nothing will happen.
2446 * @param {number} [itemIndex] A specific item to focus on
2448 OO
.ui
.IndexLayout
.prototype.focus = function ( itemIndex
) {
2450 items
= this.stackLayout
.getItems();
2452 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2453 tabPanel
= items
[ itemIndex
];
2455 tabPanel
= this.stackLayout
.getCurrentItem();
2459 this.selectFirstSelectableTabPanel();
2460 tabPanel
= this.stackLayout
.getCurrentItem();
2465 // Only change the focus if is not already in the current page
2466 if ( !OO
.ui
.contains( tabPanel
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
2472 * Find the first focusable input in the index layout and focus
2475 OO
.ui
.IndexLayout
.prototype.focusFirstFocusable = function () {
2476 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2480 * Handle tab widget select events.
2483 * @param {OO.ui.OptionWidget|null} item Selected item
2485 OO
.ui
.IndexLayout
.prototype.onTabSelectWidgetSelect = function ( item
) {
2487 this.setTabPanel( item
.getData() );
2492 * Get the tab panel closest to the specified tab panel.
2494 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2495 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2497 OO
.ui
.IndexLayout
.prototype.getClosestTabPanel = function ( tabPanel
) {
2498 var next
, prev
, level
,
2499 tabPanels
= this.stackLayout
.getItems(),
2500 index
= tabPanels
.indexOf( tabPanel
);
2502 if ( index
!== -1 ) {
2503 next
= tabPanels
[ index
+ 1 ];
2504 prev
= tabPanels
[ index
- 1 ];
2505 // Prefer adjacent tab panels at the same level
2506 level
= this.tabSelectWidget
.getItemFromData( tabPanel
.getName() ).getLevel();
2509 level
=== this.tabSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
2515 level
=== this.tabSelectWidget
.getItemFromData( next
.getName() ).getLevel()
2520 return prev
|| next
|| null;
2524 * Get the tab panel closest to the specified tab panel.
2526 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2527 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2528 * @deprecated since v0.21.3, use `getClosestTabPanel` instead
2530 OO
.ui
.IndexLayout
.prototype.getClosestCard = function ( tabPanel
) {
2531 OO
.ui
.warnDeprecation( 'IndexLayout\'s getClosestCard method is deprecated. Use getClosestTabPanel instead. See T155152' );
2532 return this.getClosestTabPanel( tabPanel
);
2536 * Get the tabs widget.
2538 * @return {OO.ui.TabSelectWidget} Tabs widget
2540 OO
.ui
.IndexLayout
.prototype.getTabs = function () {
2541 return this.tabSelectWidget
;
2545 * Get a tab panel by its symbolic name.
2547 * @param {string} name Symbolic name of tab panel
2548 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2550 OO
.ui
.IndexLayout
.prototype.getTabPanel = function ( name
) {
2551 return this.tabPanels
[ name
];
2555 * Get a tab panel by its symbolic name.
2557 * @param {string} name Symbolic name of tab panel
2558 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2559 * @deprecated since v0.21.3, use `getTabPanel` instead
2561 OO
.ui
.IndexLayout
.prototype.getCard = function ( name
) {
2562 OO
.ui
.warnDeprecation( 'IndexLayout\'s getCard method is deprecated. Use getTabPanel instead. See T155152' );
2563 return this.getTabPanel( name
);
2567 * Get the current tab panel.
2569 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2571 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanel = function () {
2572 var name
= this.getCurrentTabPanelName();
2573 return name
? this.getTabPanel( name
) : undefined;
2577 * Get the current tab panel.
2579 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2580 * @deprecated since v0.21.3, use `getCurrentTabPanel` instead
2582 OO
.ui
.IndexLayout
.prototype.getCurrentCard = function () {
2583 OO
.ui
.warnDeprecation( 'IndexLayout\'s getCurrentCard method is deprecated. Use getCurrentTabPanel instead. See T155152' );
2584 return this.getCurrentTabPanel();
2588 * Get the symbolic name of the current tab panel.
2590 * @return {string|null} Symbolic name of the current tab panel
2592 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanelName = function () {
2593 return this.currentTabPanelName
;
2597 * Get the symbolic name of the current tab panel.
2599 * @return {string|null} Symbolic name of the current tab panel
2600 * @deprecated since v0.21.3, use `getCurrentTabPanelName` instead
2602 OO
.ui
.IndexLayout
.prototype.getCurrentCardName = function () {
2603 OO
.ui
.warnDeprecation( 'IndexLayout\'s getCurrentCardName method is deprecated. Use getCurrentTabPanelName instead. See T155152' );
2604 return this.getCurrentTabPanelName();
2608 * Add tab panels to the index layout
2610 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2611 * will be automatically removed before the new tab panels are added.
2613 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2614 * @param {number} index Index of the insertion point
2618 OO
.ui
.IndexLayout
.prototype.addTabPanels = function ( tabPanels
, index
) {
2619 var i
, len
, name
, tabPanel
, item
, currentIndex
,
2620 stackLayoutTabPanels
= this.stackLayout
.getItems(),
2624 // Remove tab panels with same names
2625 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2626 tabPanel
= tabPanels
[ i
];
2627 name
= tabPanel
.getName();
2629 if ( Object
.prototype.hasOwnProperty
.call( this.tabPanels
, name
) ) {
2630 // Correct the insertion index
2631 currentIndex
= stackLayoutTabPanels
.indexOf( this.tabPanels
[ name
] );
2632 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2635 remove
.push( this.tabPanels
[ name
] );
2638 if ( remove
.length
) {
2639 this.removeTabPanels( remove
);
2642 // Add new tab panels
2643 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2644 tabPanel
= tabPanels
[ i
];
2645 name
= tabPanel
.getName();
2646 this.tabPanels
[ tabPanel
.getName() ] = tabPanel
;
2647 item
= new OO
.ui
.TabOptionWidget( { data
: name
} );
2648 tabPanel
.setTabItem( item
);
2652 if ( items
.length
) {
2653 this.tabSelectWidget
.addItems( items
, index
);
2654 this.selectFirstSelectableTabPanel();
2656 this.stackLayout
.addItems( tabPanels
, index
);
2657 this.emit( 'add', tabPanels
, index
);
2663 * Add tab panels to the index layout
2665 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2666 * will be automatically removed before the new tab panels are added.
2668 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2669 * @param {number} index Index of the insertion point
2672 * @deprecated since v0.21.3, use `addTabPanels` instead
2674 OO
.ui
.IndexLayout
.prototype.addCards = function ( tabPanels
, index
) {
2675 OO
.ui
.warnDeprecation( 'IndexLayout\'s addCards method is deprecated. Use addTabPanels instead. See T155152' );
2676 return this.addTabPanels( tabPanels
, index
);
2680 * Remove the specified tab panels from the index layout.
2682 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2684 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2688 OO
.ui
.IndexLayout
.prototype.removeTabPanels = function ( tabPanels
) {
2689 var i
, len
, name
, tabPanel
,
2692 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2693 tabPanel
= tabPanels
[ i
];
2694 name
= tabPanel
.getName();
2695 delete this.tabPanels
[ name
];
2696 items
.push( this.tabSelectWidget
.getItemFromData( name
) );
2697 tabPanel
.setTabItem( null );
2699 if ( items
.length
) {
2700 this.tabSelectWidget
.removeItems( items
);
2701 this.selectFirstSelectableTabPanel();
2703 this.stackLayout
.removeItems( tabPanels
);
2704 this.emit( 'remove', tabPanels
);
2710 * Remove the specified tab panels from the index layout.
2712 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2714 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2717 * @deprecated since v0.21.3, use `removeTabPanels` instead
2719 OO
.ui
.IndexLayout
.prototype.removeCards = function ( tabPanels
) {
2720 OO
.ui
.warnDeprecation( 'IndexLayout\'s removeCards method is deprecated. Use removeTabPanels instead. See T155152.' );
2721 return this.removeTabPanels( tabPanels
);
2725 * Clear all tab panels from the index layout.
2727 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2732 OO
.ui
.IndexLayout
.prototype.clearTabPanels = function () {
2734 tabPanels
= this.stackLayout
.getItems();
2736 this.tabPanels
= {};
2737 this.currentTabPanelName
= null;
2738 this.tabSelectWidget
.clearItems();
2739 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2740 tabPanels
[ i
].setTabItem( null );
2742 this.stackLayout
.clearItems();
2744 this.emit( 'remove', tabPanels
);
2750 * Clear all tab panels from the index layout.
2752 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2756 * @deprecated since v0.21.3, use `clearTabPanels` instead
2758 OO
.ui
.IndexLayout
.prototype.clearCards = function () {
2759 OO
.ui
.warnDeprecation( 'IndexLayout\'s clearCards method is deprecated. Use clearTabPanels instead. See T155152.' );
2760 return this.clearTabPanels();
2764 * Set the current tab panel by symbolic name.
2767 * @param {string} name Symbolic name of tab panel
2769 OO
.ui
.IndexLayout
.prototype.setTabPanel = function ( name
) {
2772 tabPanel
= this.tabPanels
[ name
],
2773 previousTabPanel
= this.currentTabPanelName
&& this.tabPanels
[ this.currentTabPanelName
];
2775 if ( name
!== this.currentTabPanelName
) {
2776 selectedItem
= this.tabSelectWidget
.getSelectedItem();
2777 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2778 this.tabSelectWidget
.selectItemByData( name
);
2781 if ( previousTabPanel
) {
2782 previousTabPanel
.setActive( false );
2783 // Blur anything focused if the next tab panel doesn't have anything focusable.
2784 // This is not needed if the next tab panel has something focusable (because once it is focused
2785 // this blur happens automatically). If the layout is non-continuous, this check is
2786 // meaningless because the next tab panel is not visible yet and thus can't hold focus.
2789 !OO
.ui
.isMobile() &&
2790 this.stackLayout
.continuous
&&
2791 OO
.ui
.findFocusable( tabPanel
.$element
).length
!== 0
2793 $focused
= previousTabPanel
.$element
.find( ':focus' );
2794 if ( $focused
.length
) {
2795 $focused
[ 0 ].blur();
2799 this.currentTabPanelName
= name
;
2800 tabPanel
.setActive( true );
2801 this.stackLayout
.setItem( tabPanel
);
2802 if ( !this.stackLayout
.continuous
&& previousTabPanel
) {
2803 // This should not be necessary, since any inputs on the previous tab panel should have been
2804 // blurred when it was hidden, but browsers are not very consistent about this.
2805 $focused
= previousTabPanel
.$element
.find( ':focus' );
2806 if ( $focused
.length
) {
2807 $focused
[ 0 ].blur();
2810 this.emit( 'set', tabPanel
);
2816 * Set the current tab panel by symbolic name.
2819 * @param {string} name Symbolic name of tab panel
2820 * @deprecated since v0.21.3, use `setTabPanel` instead
2822 OO
.ui
.IndexLayout
.prototype.setCard = function ( name
) {
2823 OO
.ui
.warnDeprecation( 'IndexLayout\'s setCard method is deprecated. Use setTabPanel instead. See T155152.' );
2824 return this.setTabPanel( name
);
2828 * Select the first selectable tab panel.
2832 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableTabPanel = function () {
2833 if ( !this.tabSelectWidget
.getSelectedItem() ) {
2834 this.tabSelectWidget
.selectItem( this.tabSelectWidget
.getFirstSelectableItem() );
2841 * Select the first selectable tab panel.
2844 * @deprecated since v0.21.3, use `selectFirstSelectableTabPanel` instead
2846 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableCard = function () {
2847 OO
.ui
.warnDeprecation( 'IndexLayout\'s selectFirstSelectableCard method is deprecated. Use selectFirestSelectableTabPanel instead. See T155152.' );
2848 return this.selectFirstSelectableTabPanel();
2852 * ToggleWidget implements basic behavior of widgets with an on/off state.
2853 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2857 * @extends OO.ui.Widget
2860 * @param {Object} [config] Configuration options
2861 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2862 * By default, the toggle is in the 'off' state.
2864 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
2865 // Configuration initialization
2866 config
= config
|| {};
2868 // Parent constructor
2869 OO
.ui
.ToggleWidget
.parent
.call( this, config
);
2875 this.$element
.addClass( 'oo-ui-toggleWidget' );
2876 this.setValue( !!config
.value
);
2881 OO
.inheritClass( OO
.ui
.ToggleWidget
, OO
.ui
.Widget
);
2888 * A change event is emitted when the on/off state of the toggle changes.
2890 * @param {boolean} value Value representing the new state of the toggle
2896 * Get the value representing the toggle’s state.
2898 * @return {boolean} The on/off state of the toggle
2900 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
2905 * Set the state of the toggle: `true` for 'on', `false` for 'off'.
2907 * @param {boolean} value The state of the toggle
2911 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
2913 if ( this.value
!== value
) {
2915 this.emit( 'change', value
);
2916 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
2917 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
2923 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
2924 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
2925 * configured with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators},
2926 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
2927 * and {@link OO.ui.mixin.LabelElement labels}. Please see
2928 * the [OOjs UI documentation][1] on MediaWiki for more information.
2931 * // Toggle buttons in the 'off' and 'on' state.
2932 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
2933 * label: 'Toggle Button off'
2935 * var toggleButton2 = new OO.ui.ToggleButtonWidget( {
2936 * label: 'Toggle Button on',
2939 * // Append the buttons to the DOM.
2940 * $( 'body' ).append( toggleButton1.$element, toggleButton2.$element );
2942 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Toggle_buttons
2945 * @extends OO.ui.ToggleWidget
2946 * @mixins OO.ui.mixin.ButtonElement
2947 * @mixins OO.ui.mixin.IconElement
2948 * @mixins OO.ui.mixin.IndicatorElement
2949 * @mixins OO.ui.mixin.LabelElement
2950 * @mixins OO.ui.mixin.TitledElement
2951 * @mixins OO.ui.mixin.FlaggedElement
2952 * @mixins OO.ui.mixin.TabIndexedElement
2955 * @param {Object} [config] Configuration options
2956 * @cfg {boolean} [value=false] The toggle button’s initial on/off
2957 * state. By default, the button is in the 'off' state.
2959 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
2960 // Configuration initialization
2961 config
= config
|| {};
2963 // Parent constructor
2964 OO
.ui
.ToggleButtonWidget
.parent
.call( this, config
);
2966 // Mixin constructors
2967 OO
.ui
.mixin
.ButtonElement
.call( this, $.extend( {}, config
, { active
: this.active
} ) );
2968 OO
.ui
.mixin
.IconElement
.call( this, config
);
2969 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
2970 OO
.ui
.mixin
.LabelElement
.call( this, config
);
2971 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
2972 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
2973 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
2976 this.connect( this, { click
: 'onAction' } );
2979 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
2981 .addClass( 'oo-ui-toggleButtonWidget' )
2982 .append( this.$button
);
2987 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
2988 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
2989 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IconElement
);
2990 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
2991 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.LabelElement
);
2992 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TitledElement
);
2993 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
2994 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2996 /* Static Properties */
3002 OO
.ui
.ToggleButtonWidget
.static.tagName
= 'span';
3007 * Handle the button action being triggered.
3011 OO
.ui
.ToggleButtonWidget
.prototype.onAction = function () {
3012 this.setValue( !this.value
);
3018 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
3020 if ( value
!== this.value
) {
3021 // Might be called from parent constructor before ButtonElement constructor
3022 if ( this.$button
) {
3023 this.$button
.attr( 'aria-pressed', value
.toString() );
3025 this.setActive( value
);
3029 OO
.ui
.ToggleButtonWidget
.parent
.prototype.setValue
.call( this, value
);
3037 OO
.ui
.ToggleButtonWidget
.prototype.setButtonElement = function ( $button
) {
3038 if ( this.$button
) {
3039 this.$button
.removeAttr( 'aria-pressed' );
3041 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement
.call( this, $button
);
3042 this.$button
.attr( 'aria-pressed', this.value
.toString() );
3046 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
3047 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
3048 * visually by a slider in the leftmost position.
3051 * // Toggle switches in the 'off' and 'on' position.
3052 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget();
3053 * var toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
3057 * // Create a FieldsetLayout to layout and label switches
3058 * var fieldset = new OO.ui.FieldsetLayout( {
3059 * label: 'Toggle switches'
3061 * fieldset.addItems( [
3062 * new OO.ui.FieldLayout( toggleSwitch1, { label: 'Off', align: 'top' } ),
3063 * new OO.ui.FieldLayout( toggleSwitch2, { label: 'On', align: 'top' } )
3065 * $( 'body' ).append( fieldset.$element );
3068 * @extends OO.ui.ToggleWidget
3069 * @mixins OO.ui.mixin.TabIndexedElement
3072 * @param {Object} [config] Configuration options
3073 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
3074 * By default, the toggle switch is in the 'off' position.
3076 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
3077 // Parent constructor
3078 OO
.ui
.ToggleSwitchWidget
.parent
.call( this, config
);
3080 // Mixin constructors
3081 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3084 this.dragging
= false;
3085 this.dragStart
= null;
3086 this.sliding
= false;
3087 this.$glow
= $( '<span>' );
3088 this.$grip
= $( '<span>' );
3092 click
: this.onClick
.bind( this ),
3093 keypress
: this.onKeyPress
.bind( this )
3097 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
3098 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
3100 .addClass( 'oo-ui-toggleSwitchWidget' )
3101 .attr( 'role', 'checkbox' )
3102 .append( this.$glow
, this.$grip
);
3107 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
3108 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3113 * Handle mouse click events.
3116 * @param {jQuery.Event} e Mouse click event
3118 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
3119 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
3120 this.setValue( !this.value
);
3126 * Handle key press events.
3129 * @param {jQuery.Event} e Key press event
3131 OO
.ui
.ToggleSwitchWidget
.prototype.onKeyPress = function ( e
) {
3132 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
3133 this.setValue( !this.value
);
3141 OO
.ui
.ToggleSwitchWidget
.prototype.setValue = function ( value
) {
3142 OO
.ui
.ToggleSwitchWidget
.parent
.prototype.setValue
.call( this, value
);
3143 this.$element
.attr( 'aria-checked', this.value
.toString() );
3150 OO
.ui
.ToggleSwitchWidget
.prototype.simulateLabelClick = function () {
3151 if ( !this.isDisabled() ) {
3152 this.setValue( !this.value
);
3158 * OutlineControlsWidget is a set of controls for an {@link OO.ui.OutlineSelectWidget outline select widget}.
3159 * Controls include moving items up and down, removing items, and adding different kinds of items.
3161 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3164 * @extends OO.ui.Widget
3165 * @mixins OO.ui.mixin.GroupElement
3166 * @mixins OO.ui.mixin.IconElement
3169 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
3170 * @param {Object} [config] Configuration options
3171 * @cfg {Object} [abilities] List of abilties
3172 * @cfg {boolean} [abilities.move=true] Allow moving movable items
3173 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
3175 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
3176 // Allow passing positional parameters inside the config object
3177 if ( OO
.isPlainObject( outline
) && config
=== undefined ) {
3179 outline
= config
.outline
;
3182 // Configuration initialization
3183 config
= $.extend( { icon
: 'add' }, config
);
3185 // Parent constructor
3186 OO
.ui
.OutlineControlsWidget
.parent
.call( this, config
);
3188 // Mixin constructors
3189 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3190 OO
.ui
.mixin
.IconElement
.call( this, config
);
3193 this.outline
= outline
;
3194 this.$movers
= $( '<div>' );
3195 this.upButton
= new OO
.ui
.ButtonWidget( {
3198 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
3200 this.downButton
= new OO
.ui
.ButtonWidget( {
3203 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
3205 this.removeButton
= new OO
.ui
.ButtonWidget( {
3208 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
3210 this.abilities
= { move: true, remove
: true };
3213 outline
.connect( this, {
3214 select
: 'onOutlineChange',
3215 add
: 'onOutlineChange',
3216 remove
: 'onOutlineChange'
3218 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
3219 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
3220 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
3223 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
3224 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
3226 .addClass( 'oo-ui-outlineControlsWidget-movers' )
3227 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
3228 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
3229 this.setAbilities( config
.abilities
|| {} );
3234 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
3235 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.GroupElement
);
3236 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.IconElement
);
3242 * @param {number} places Number of places to move
3254 * @param {Object} abilities List of abilties
3255 * @param {boolean} [abilities.move] Allow moving movable items
3256 * @param {boolean} [abilities.remove] Allow removing removable items
3258 OO
.ui
.OutlineControlsWidget
.prototype.setAbilities = function ( abilities
) {
3261 for ( ability
in this.abilities
) {
3262 if ( abilities
[ ability
] !== undefined ) {
3263 this.abilities
[ ability
] = !!abilities
[ ability
];
3267 this.onOutlineChange();
3271 * Handle outline change events.
3275 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
3276 var i
, len
, firstMovable
, lastMovable
,
3277 items
= this.outline
.getItems(),
3278 selectedItem
= this.outline
.getSelectedItem(),
3279 movable
= this.abilities
.move && selectedItem
&& selectedItem
.isMovable(),
3280 removable
= this.abilities
.remove
&& selectedItem
&& selectedItem
.isRemovable();
3285 while ( ++i
< len
) {
3286 if ( items
[ i
].isMovable() ) {
3287 firstMovable
= items
[ i
];
3293 if ( items
[ i
].isMovable() ) {
3294 lastMovable
= items
[ i
];
3299 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
3300 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
3301 this.removeButton
.setDisabled( !removable
);
3305 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3307 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3308 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3312 * @extends OO.ui.DecoratedOptionWidget
3315 * @param {Object} [config] Configuration options
3316 * @cfg {number} [level] Indentation level
3317 * @cfg {boolean} [movable] Allow modification from {@link OO.ui.OutlineControlsWidget outline controls}.
3319 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
3320 // Configuration initialization
3321 config
= config
|| {};
3323 // Parent constructor
3324 OO
.ui
.OutlineOptionWidget
.parent
.call( this, config
);
3328 this.movable
= !!config
.movable
;
3329 this.removable
= !!config
.removable
;
3332 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
3333 this.setLevel( config
.level
);
3338 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3340 /* Static Properties */
3346 OO
.ui
.OutlineOptionWidget
.static.highlightable
= true;
3352 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
3357 * @property {string}
3359 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
3364 * @property {number}
3366 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
3371 * Check if item is movable.
3373 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3375 * @return {boolean} Item is movable
3377 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
3378 return this.movable
;
3382 * Check if item is removable.
3384 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3386 * @return {boolean} Item is removable
3388 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
3389 return this.removable
;
3393 * Get indentation level.
3395 * @return {number} Indentation level
3397 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
3404 OO
.ui
.OutlineOptionWidget
.prototype.setPressed = function ( state
) {
3405 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setPressed
.call( this, state
);
3406 if ( this.pressed
) {
3407 this.setFlags( { progressive
: true } );
3408 } else if ( !this.selected
) {
3409 this.setFlags( { progressive
: false } );
3417 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3419 * @param {boolean} movable Item is movable
3422 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
3423 this.movable
= !!movable
;
3424 this.updateThemeClasses();
3431 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3433 * @param {boolean} removable Item is removable
3436 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
3437 this.removable
= !!removable
;
3438 this.updateThemeClasses();
3445 OO
.ui
.OutlineOptionWidget
.prototype.setSelected = function ( state
) {
3446 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3447 if ( this.selected
) {
3448 this.setFlags( { progressive
: true } );
3450 this.setFlags( { progressive
: false } );
3456 * Set indentation level.
3458 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3461 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
3462 var levels
= this.constructor.static.levels
,
3463 levelClass
= this.constructor.static.levelClass
,
3466 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
3468 if ( this.level
=== i
) {
3469 this.$element
.addClass( levelClass
+ i
);
3471 this.$element
.removeClass( levelClass
+ i
);
3474 this.updateThemeClasses();
3480 * OutlineSelectWidget is a structured list that contains {@link OO.ui.OutlineOptionWidget outline options}
3481 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls} widget.
3483 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3486 * @extends OO.ui.SelectWidget
3487 * @mixins OO.ui.mixin.TabIndexedElement
3490 * @param {Object} [config] Configuration options
3492 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
3493 // Parent constructor
3494 OO
.ui
.OutlineSelectWidget
.parent
.call( this, config
);
3496 // Mixin constructors
3497 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3501 focus
: this.bindKeyDownListener
.bind( this ),
3502 blur
: this.unbindKeyDownListener
.bind( this )
3506 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
3511 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
3512 OO
.mixinClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3515 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3516 * can be selected and configured with data. The class is
3517 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3518 * [OOjs UI documentation on MediaWiki] [1] for more information.
3520 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Button_selects_and_options
3523 * @extends OO.ui.OptionWidget
3524 * @mixins OO.ui.mixin.ButtonElement
3525 * @mixins OO.ui.mixin.IconElement
3526 * @mixins OO.ui.mixin.IndicatorElement
3527 * @mixins OO.ui.mixin.TitledElement
3530 * @param {Object} [config] Configuration options
3532 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
3533 // Configuration initialization
3534 config
= config
|| {};
3536 // Parent constructor
3537 OO
.ui
.ButtonOptionWidget
.parent
.call( this, config
);
3539 // Mixin constructors
3540 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3541 OO
.ui
.mixin
.IconElement
.call( this, config
);
3542 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3543 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
3546 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
3547 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3548 this.$element
.append( this.$button
);
3553 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
3554 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.ButtonElement
);
3555 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IconElement
);
3556 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IndicatorElement
);
3557 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.TitledElement
);
3559 /* Static Properties */
3562 * Allow button mouse down events to pass through so they can be handled by the parent select widget
3567 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
3573 OO
.ui
.ButtonOptionWidget
.static.highlightable
= false;
3580 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
3581 OO
.ui
.ButtonOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3583 if ( this.constructor.static.selectable
) {
3584 this.setActive( state
);
3591 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3592 * button options and is used together with
3593 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3594 * highlighting, choosing, and selecting mutually exclusive options. Please see
3595 * the [OOjs UI documentation on MediaWiki] [1] for more information.
3598 * // Example: A ButtonSelectWidget that contains three ButtonOptionWidgets
3599 * var option1 = new OO.ui.ButtonOptionWidget( {
3601 * label: 'Option 1',
3602 * title: 'Button option 1'
3605 * var option2 = new OO.ui.ButtonOptionWidget( {
3607 * label: 'Option 2',
3608 * title: 'Button option 2'
3611 * var option3 = new OO.ui.ButtonOptionWidget( {
3613 * label: 'Option 3',
3614 * title: 'Button option 3'
3617 * var buttonSelect=new OO.ui.ButtonSelectWidget( {
3618 * items: [ option1, option2, option3 ]
3620 * $( 'body' ).append( buttonSelect.$element );
3622 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
3625 * @extends OO.ui.SelectWidget
3626 * @mixins OO.ui.mixin.TabIndexedElement
3629 * @param {Object} [config] Configuration options
3631 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
3632 // Parent constructor
3633 OO
.ui
.ButtonSelectWidget
.parent
.call( this, config
);
3635 // Mixin constructors
3636 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3640 focus
: this.bindKeyDownListener
.bind( this ),
3641 blur
: this.unbindKeyDownListener
.bind( this )
3645 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
3650 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
3651 OO
.mixinClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3654 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3656 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3657 * {@link OO.ui.TabPanelLayout tab panel layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3661 * @extends OO.ui.OptionWidget
3664 * @param {Object} [config] Configuration options
3666 OO
.ui
.TabOptionWidget
= function OoUiTabOptionWidget( config
) {
3667 // Configuration initialization
3668 config
= config
|| {};
3670 // Parent constructor
3671 OO
.ui
.TabOptionWidget
.parent
.call( this, config
);
3674 this.$element
.addClass( 'oo-ui-tabOptionWidget' );
3679 OO
.inheritClass( OO
.ui
.TabOptionWidget
, OO
.ui
.OptionWidget
);
3681 /* Static Properties */
3687 OO
.ui
.TabOptionWidget
.static.highlightable
= false;
3690 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3692 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3695 * @extends OO.ui.SelectWidget
3696 * @mixins OO.ui.mixin.TabIndexedElement
3699 * @param {Object} [config] Configuration options
3701 OO
.ui
.TabSelectWidget
= function OoUiTabSelectWidget( config
) {
3702 // Parent constructor
3703 OO
.ui
.TabSelectWidget
.parent
.call( this, config
);
3705 // Mixin constructors
3706 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3710 focus
: this.bindKeyDownListener
.bind( this ),
3711 blur
: this.unbindKeyDownListener
.bind( this )
3715 this.$element
.addClass( 'oo-ui-tabSelectWidget' );
3720 OO
.inheritClass( OO
.ui
.TabSelectWidget
, OO
.ui
.SelectWidget
);
3721 OO
.mixinClass( OO
.ui
.TabSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3724 * CapsuleItemWidgets are used within a {@link OO.ui.CapsuleMultiselectWidget
3725 * CapsuleMultiselectWidget} to display the selected items.
3728 * @extends OO.ui.Widget
3729 * @mixins OO.ui.mixin.ItemWidget
3730 * @mixins OO.ui.mixin.LabelElement
3731 * @mixins OO.ui.mixin.FlaggedElement
3732 * @mixins OO.ui.mixin.TabIndexedElement
3735 * @param {Object} [config] Configuration options
3737 OO
.ui
.CapsuleItemWidget
= function OoUiCapsuleItemWidget( config
) {
3738 // Configuration initialization
3739 config
= config
|| {};
3741 // Parent constructor
3742 OO
.ui
.CapsuleItemWidget
.parent
.call( this, config
);
3744 // Mixin constructors
3745 OO
.ui
.mixin
.ItemWidget
.call( this );
3746 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3747 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3748 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3751 this.closeButton
= new OO
.ui
.ButtonWidget( {
3755 } ).on( 'click', this.onCloseClick
.bind( this ) );
3757 this.on( 'disable', function ( disabled
) {
3758 this.closeButton
.setDisabled( disabled
);
3764 click
: this.onClick
.bind( this ),
3765 keydown
: this.onKeyDown
.bind( this )
3767 .addClass( 'oo-ui-capsuleItemWidget' )
3768 .append( this.$label
, this.closeButton
.$element
);
3773 OO
.inheritClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.Widget
);
3774 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.ItemWidget
);
3775 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.LabelElement
);
3776 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
3777 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3782 * Handle close icon clicks
3784 OO
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
3785 var element
= this.getElementGroup();
3787 if ( element
&& $.isFunction( element
.removeItems
) ) {
3788 element
.removeItems( [ this ] );
3794 * Handle click event for the entire capsule
3796 OO
.ui
.CapsuleItemWidget
.prototype.onClick = function () {
3797 var element
= this.getElementGroup();
3799 if ( !this.isDisabled() && element
&& $.isFunction( element
.editItem
) ) {
3800 element
.editItem( this );
3805 * Handle keyDown event for the entire capsule
3807 * @param {jQuery.Event} e Key down event
3809 OO
.ui
.CapsuleItemWidget
.prototype.onKeyDown = function ( e
) {
3810 var element
= this.getElementGroup();
3812 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
) {
3813 element
.removeItems( [ this ] );
3816 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
3817 element
.editItem( this );
3819 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
3820 element
.getPreviousItem( this ).focus();
3821 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
3822 element
.getNextItem( this ).focus();
3827 * CapsuleMultiselectWidgets are something like a {@link OO.ui.ComboBoxInputWidget combo box widget}
3828 * that allows for selecting multiple values.
3830 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
3833 * // Example: A CapsuleMultiselectWidget.
3834 * var capsule = new OO.ui.CapsuleMultiselectWidget( {
3835 * label: 'CapsuleMultiselectWidget',
3836 * selected: [ 'Option 1', 'Option 3' ],
3839 * new OO.ui.MenuOptionWidget( {
3841 * label: 'Option One'
3843 * new OO.ui.MenuOptionWidget( {
3845 * label: 'Option Two'
3847 * new OO.ui.MenuOptionWidget( {
3849 * label: 'Option Three'
3851 * new OO.ui.MenuOptionWidget( {
3853 * label: 'Option Four'
3855 * new OO.ui.MenuOptionWidget( {
3857 * label: 'Option Five'
3862 * $( 'body' ).append( capsule.$element );
3864 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
3867 * @extends OO.ui.Widget
3868 * @mixins OO.ui.mixin.GroupElement
3869 * @mixins OO.ui.mixin.PopupElement
3870 * @mixins OO.ui.mixin.TabIndexedElement
3871 * @mixins OO.ui.mixin.IndicatorElement
3872 * @mixins OO.ui.mixin.IconElement
3873 * @uses OO.ui.CapsuleItemWidget
3874 * @uses OO.ui.MenuSelectWidget
3877 * @param {Object} [config] Configuration options
3878 * @cfg {string} [placeholder] Placeholder text
3879 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if not present in the menu.
3880 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added.
3881 * @cfg {Object} [menu] (required) Configuration options to pass to the
3882 * {@link OO.ui.MenuSelectWidget menu select widget}.
3883 * @cfg {Object} [popup] Configuration options to pass to the {@link OO.ui.PopupWidget popup widget}.
3884 * If specified, this popup will be shown instead of the menu (but the menu
3885 * will still be used for item labels and allowArbitrary=false). The widgets
3886 * in the popup should use {@link #addItemsFromData} or {@link #addItems} as necessary.
3887 * @cfg {jQuery} [$overlay=this.$element] Render the menu or popup into a separate layer.
3888 * This configuration is useful in cases where the expanded menu is larger than
3889 * its containing `<div>`. The specified overlay layer is usually on top of
3890 * the containing `<div>` and has a larger area. By default, the menu uses
3891 * relative positioning.
3892 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
3894 OO
.ui
.CapsuleMultiselectWidget
= function OoUiCapsuleMultiselectWidget( config
) {
3897 // Parent constructor
3898 OO
.ui
.CapsuleMultiselectWidget
.parent
.call( this, config
);
3900 // Configuration initialization
3901 config
= $.extend( {
3902 allowArbitrary
: false,
3903 allowDuplicates
: false,
3904 $overlay
: this.$element
3907 // Properties (must be set before mixin constructor calls)
3908 this.$handle
= $( '<div>' );
3909 this.$input
= config
.popup
? null : $( '<input>' );
3910 if ( config
.placeholder
!== undefined && config
.placeholder
!== '' ) {
3911 this.$input
.attr( 'placeholder', config
.placeholder
);
3914 // Mixin constructors
3915 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3916 if ( config
.popup
) {
3917 config
.popup
= $.extend( {}, config
.popup
, {
3921 OO
.ui
.mixin
.PopupElement
.call( this, config
);
3922 $tabFocus
= $( '<span>' );
3923 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: $tabFocus
} ) );
3927 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
3929 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3930 OO
.ui
.mixin
.IconElement
.call( this, config
);
3933 this.$content
= $( '<div>' );
3934 this.allowArbitrary
= config
.allowArbitrary
;
3935 this.allowDuplicates
= config
.allowDuplicates
;
3936 this.$overlay
= config
.$overlay
;
3937 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend(
3940 $input
: this.$input
,
3941 $floatableContainer
: this.$element
,
3942 filterFromInput
: true,
3943 disabled
: this.isDisabled()
3951 focus
: this.focus
.bind( this )
3953 this.popup
.$element
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3954 if ( this.popup
.$autoCloseIgnore
) {
3955 this.popup
.$autoCloseIgnore
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3957 this.popup
.connect( this, {
3958 toggle: function ( visible
) {
3959 $tabFocus
.toggle( !visible
);
3964 focus
: this.onInputFocus
.bind( this ),
3965 blur
: this.onInputBlur
.bind( this ),
3966 'propertychange change click mouseup keydown keyup input cut paste select focus':
3967 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
3968 keydown
: this.onKeyDown
.bind( this ),
3969 keypress
: this.onKeyPress
.bind( this )
3972 this.menu
.connect( this, {
3973 choose
: 'onMenuChoose',
3974 toggle
: 'onMenuToggle',
3975 add
: 'onMenuItemsChange',
3976 remove
: 'onMenuItemsChange'
3979 mousedown
: this.onMouseDown
.bind( this )
3983 if ( this.$input
) {
3984 this.$input
.prop( 'disabled', this.isDisabled() );
3987 'aria-owns': this.menu
.getElementId(),
3988 'aria-autocomplete': 'list'
3991 if ( config
.data
) {
3992 this.setItemsFromData( config
.data
);
3994 this.$content
.addClass( 'oo-ui-capsuleMultiselectWidget-content' )
3995 .append( this.$group
);
3996 this.$group
.addClass( 'oo-ui-capsuleMultiselectWidget-group' );
3997 this.$handle
.addClass( 'oo-ui-capsuleMultiselectWidget-handle' )
3998 .append( this.$indicator
, this.$icon
, this.$content
);
3999 this.$element
.addClass( 'oo-ui-capsuleMultiselectWidget' )
4000 .append( this.$handle
);
4002 this.popup
.$element
.addClass( 'oo-ui-capsuleMultiselectWidget-popup' );
4003 this.$content
.append( $tabFocus
);
4004 this.$overlay
.append( this.popup
.$element
);
4006 this.$content
.append( this.$input
);
4007 this.$overlay
.append( this.menu
.$element
);
4010 $tabFocus
.addClass( 'oo-ui-capsuleMultiselectWidget-focusTrap' );
4013 // Input size needs to be calculated after everything else is rendered
4014 setTimeout( function () {
4015 if ( this.$input
) {
4016 this.updateInputSize();
4020 this.onMenuItemsChange();
4025 OO
.inheritClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.Widget
);
4026 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.GroupElement
);
4027 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
4028 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4029 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
4030 OO
.mixinClass( OO
.ui
.CapsuleMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
4037 * A change event is emitted when the set of selected items changes.
4039 * @param {Mixed[]} datas Data of the now-selected items
4045 * A resize event is emitted when the widget's dimensions change to accomodate newly added items or
4046 * current user input.
4052 * Construct a OO.ui.CapsuleItemWidget (or a subclass thereof) from given label and data.
4053 * May return `null` if the given label and data are not valid.
4056 * @param {Mixed} data Custom data of any type.
4057 * @param {string} label The label text.
4058 * @return {OO.ui.CapsuleItemWidget|null}
4060 OO
.ui
.CapsuleMultiselectWidget
.prototype.createItemWidget = function ( data
, label
) {
4061 if ( label
=== '' ) {
4064 return new OO
.ui
.CapsuleItemWidget( { data
: data
, label
: label
} );
4070 OO
.ui
.CapsuleMultiselectWidget
.prototype.getInputId = function () {
4071 if ( !this.$input
) {
4074 return OO
.ui
.mixin
.TabIndexedElement
.prototype.getInputId
.call( this );
4078 * Get the data of the items in the capsule
4082 OO
.ui
.CapsuleMultiselectWidget
.prototype.getItemsData = function () {
4083 return this.getItems().map( function ( item
) {
4089 * Set the items in the capsule by providing data
4092 * @param {Mixed[]} datas
4093 * @return {OO.ui.CapsuleMultiselectWidget}
4095 OO
.ui
.CapsuleMultiselectWidget
.prototype.setItemsFromData = function ( datas
) {
4098 items
= this.getItems();
4100 $.each( datas
, function ( i
, data
) {
4102 item
= menu
.getItemFromData( data
);
4106 } else if ( widget
.allowArbitrary
) {
4107 label
= String( data
);
4113 for ( j
= 0; j
< items
.length
; j
++ ) {
4114 if ( items
[ j
].data
=== data
&& items
[ j
].label
=== label
) {
4116 items
.splice( j
, 1 );
4121 item
= widget
.createItemWidget( data
, label
);
4124 widget
.addItems( [ item
], i
);
4128 if ( items
.length
) {
4129 widget
.removeItems( items
);
4136 * Add items to the capsule by providing their data
4139 * @param {Mixed[]} datas
4140 * @return {OO.ui.CapsuleMultiselectWidget}
4142 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemsFromData = function ( datas
) {
4147 $.each( datas
, function ( i
, data
) {
4150 if ( !widget
.getItemFromData( data
) || widget
.allowDuplicates
) {
4151 item
= menu
.getItemFromData( data
);
4153 item
= widget
.createItemWidget( data
, item
.label
);
4154 } else if ( widget
.allowArbitrary
) {
4155 item
= widget
.createItemWidget( data
, String( data
) );
4163 if ( items
.length
) {
4164 this.addItems( items
);
4171 * Add items to the capsule by providing a label
4173 * @param {string} label
4174 * @return {boolean} Whether the item was added or not
4176 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItemFromLabel = function ( label
) {
4178 item
= this.menu
.getItemFromLabel( label
, true );
4180 this.addItemsFromData( [ item
.data
] );
4182 } else if ( this.allowArbitrary
) {
4183 items
= this.getItems();
4184 this.addItemsFromData( [ label
] );
4185 return !OO
.compare( this.getItems(), items
);
4191 * Remove items by data
4194 * @param {Mixed[]} datas
4195 * @return {OO.ui.CapsuleMultiselectWidget}
4197 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItemsFromData = function ( datas
) {
4201 $.each( datas
, function ( i
, data
) {
4202 var item
= widget
.getItemFromData( data
);
4208 if ( items
.length
) {
4209 this.removeItems( items
);
4218 OO
.ui
.CapsuleMultiselectWidget
.prototype.addItems = function ( items
) {
4220 oldItems
= this.items
.slice();
4222 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
);
4224 if ( this.items
.length
!== oldItems
.length
) {
4228 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
4229 same
= same
&& this.items
[ i
] === oldItems
[ i
];
4233 this.emit( 'change', this.getItemsData() );
4234 this.updateInputSize();
4241 * Removes the item from the list and copies its label to `this.$input`.
4243 * @param {Object} item
4245 OO
.ui
.CapsuleMultiselectWidget
.prototype.editItem = function ( item
) {
4246 this.addItemFromLabel( this.$input
.val() );
4248 this.$input
.val( item
.label
);
4249 this.updateInputSize();
4251 this.menu
.updateItemVisibility(); // Hack, we shouldn't be calling this method directly
4252 this.removeItems( [ item
] );
4258 OO
.ui
.CapsuleMultiselectWidget
.prototype.removeItems = function ( items
) {
4260 oldItems
= this.items
.slice();
4262 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
4264 if ( this.items
.length
!== oldItems
.length
) {
4268 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
4269 same
= same
&& this.items
[ i
] === oldItems
[ i
];
4273 this.emit( 'change', this.getItemsData() );
4274 this.updateInputSize();
4283 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearItems = function () {
4284 if ( this.items
.length
) {
4285 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
4286 this.emit( 'change', this.getItemsData() );
4287 this.updateInputSize();
4293 * Given an item, returns the item after it. If its the last item,
4294 * returns `this.$input`. If no item is passed, returns the very first
4297 * @param {OO.ui.CapsuleItemWidget} [item]
4298 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4300 OO
.ui
.CapsuleMultiselectWidget
.prototype.getNextItem = function ( item
) {
4303 if ( item
=== undefined ) {
4304 return this.items
[ 0 ];
4307 itemIndex
= this.items
.indexOf( item
);
4308 if ( itemIndex
< 0 ) { // Item not in list
4310 } else if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
4313 return this.items
[ itemIndex
+ 1 ];
4318 * Given an item, returns the item before it. If its the first item,
4319 * returns `this.$input`. If no item is passed, returns the very last
4322 * @param {OO.ui.CapsuleItemWidget} [item]
4323 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4325 OO
.ui
.CapsuleMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
4328 if ( item
=== undefined ) {
4329 return this.items
[ this.items
.length
- 1 ];
4332 itemIndex
= this.items
.indexOf( item
);
4333 if ( itemIndex
< 0 ) { // Item not in list
4335 } else if ( itemIndex
=== 0 ) { // First item
4338 return this.items
[ itemIndex
- 1 ];
4343 * Get the capsule widget's menu.
4345 * @return {OO.ui.MenuSelectWidget} Menu widget
4347 OO
.ui
.CapsuleMultiselectWidget
.prototype.getMenu = function () {
4352 * Handle focus events
4355 * @param {jQuery.Event} event
4357 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputFocus = function () {
4358 if ( !this.isDisabled() ) {
4359 this.updateInputSize();
4360 this.menu
.toggle( true );
4365 * Handle blur events
4368 * @param {jQuery.Event} event
4370 OO
.ui
.CapsuleMultiselectWidget
.prototype.onInputBlur = function () {
4371 this.addItemFromLabel( this.$input
.val() );
4376 * Handles popup focus out events.
4379 * @param {jQuery.Event} e Focus out event
4381 OO
.ui
.CapsuleMultiselectWidget
.prototype.onPopupFocusOut = function () {
4382 var widget
= this.popup
;
4384 setTimeout( function () {
4386 widget
.isVisible() &&
4387 !OO
.ui
.contains( widget
.$element
.add( widget
.$autoCloseIgnore
).get(), document
.activeElement
, true )
4389 widget
.toggle( false );
4395 * Handle mouse down events.
4398 * @param {jQuery.Event} e Mouse down event
4400 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMouseDown = function ( e
) {
4401 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
4405 this.updateInputSize();
4410 * Handle key press events.
4413 * @param {jQuery.Event} e Key press event
4415 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyPress = function ( e
) {
4416 if ( !this.isDisabled() ) {
4417 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
4422 if ( !this.popup
) {
4423 this.menu
.toggle( true );
4424 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
4425 if ( this.addItemFromLabel( this.$input
.val() ) ) {
4431 // Make sure the input gets resized.
4432 setTimeout( this.updateInputSize
.bind( this ), 0 );
4438 * Handle key down events.
4441 * @param {jQuery.Event} e Key down event
4443 OO
.ui
.CapsuleMultiselectWidget
.prototype.onKeyDown = function ( e
) {
4445 !this.isDisabled() &&
4446 this.$input
.val() === '' &&
4449 // 'keypress' event is not triggered for Backspace
4450 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
4451 if ( e
.metaKey
|| e
.ctrlKey
) {
4452 this.removeItems( this.items
.slice( -1 ) );
4454 this.editItem( this.items
[ this.items
.length
- 1 ] );
4457 } else if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
) {
4458 this.getPreviousItem().focus();
4459 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
) {
4460 this.getNextItem().focus();
4466 * Update the dimensions of the text input field to encompass all available area.
4469 * @param {jQuery.Event} e Event of some sort
4471 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateInputSize = function () {
4472 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
4473 if ( this.$input
&& !this.isDisabled() ) {
4474 this.$input
.css( 'width', '1em' );
4475 $lastItem
= this.$group
.children().last();
4476 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
4478 // Get the width of the input with the placeholder text as
4479 // the value and save it so that we don't keep recalculating
4481 this.contentWidthWithPlaceholder
=== undefined &&
4482 this.$input
.val() === '' &&
4483 this.$input
.attr( 'placeholder' ) !== undefined
4485 this.$input
.val( this.$input
.attr( 'placeholder' ) );
4486 this.contentWidthWithPlaceholder
= this.$input
[ 0 ].scrollWidth
;
4487 this.$input
.val( '' );
4491 // Always keep the input wide enough for the placeholder text
4492 contentWidth
= Math
.max(
4493 this.$input
[ 0 ].scrollWidth
,
4494 // undefined arguments in Math.max lead to NaN
4495 ( this.contentWidthWithPlaceholder
=== undefined ) ?
4496 0 : this.contentWidthWithPlaceholder
4498 currentWidth
= this.$input
.width();
4500 if ( contentWidth
< currentWidth
) {
4501 this.updateIfHeightChanged();
4502 // All is fine, don't perform expensive calculations
4506 if ( $lastItem
.length
=== 0 ) {
4507 bestWidth
= this.$content
.innerWidth();
4509 bestWidth
= direction
=== 'ltr' ?
4510 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
4511 $lastItem
.position().left
;
4514 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
4515 // pixels this is off by are coming from.
4517 if ( contentWidth
> bestWidth
) {
4518 // This will result in the input getting shifted to the next line
4519 bestWidth
= this.$content
.innerWidth() - 10;
4521 this.$input
.width( Math
.floor( bestWidth
) );
4522 this.updateIfHeightChanged();
4524 this.updateIfHeightChanged();
4529 * Determine if widget height changed, and if so, update menu position and emit 'resize' event.
4533 OO
.ui
.CapsuleMultiselectWidget
.prototype.updateIfHeightChanged = function () {
4534 var height
= this.$element
.height();
4535 if ( height
!== this.height
) {
4536 this.height
= height
;
4537 this.menu
.position();
4539 this.popup
.updateDimensions();
4541 this.emit( 'resize' );
4546 * Handle menu choose events.
4549 * @param {OO.ui.OptionWidget} item Chosen item
4551 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
4552 if ( item
&& item
.isVisible() ) {
4553 this.addItemsFromData( [ item
.getData() ] );
4559 * Handle menu toggle events.
4562 * @param {boolean} isVisible Menu toggle event
4564 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
4565 this.$element
.toggleClass( 'oo-ui-capsuleMultiselectWidget-open', isVisible
);
4569 * Handle menu item change events.
4573 OO
.ui
.CapsuleMultiselectWidget
.prototype.onMenuItemsChange = function () {
4574 this.setItemsFromData( this.getItemsData() );
4575 this.$element
.toggleClass( 'oo-ui-capsuleMultiselectWidget-empty', this.menu
.isEmpty() );
4579 * Clear the input field
4583 OO
.ui
.CapsuleMultiselectWidget
.prototype.clearInput = function () {
4584 if ( this.$input
) {
4585 this.$input
.val( '' );
4586 this.updateInputSize();
4589 this.popup
.toggle( false );
4591 this.menu
.toggle( false );
4592 this.menu
.selectItem();
4593 this.menu
.highlightItem();
4599 OO
.ui
.CapsuleMultiselectWidget
.prototype.setDisabled = function ( disabled
) {
4603 OO
.ui
.CapsuleMultiselectWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
4605 if ( this.$input
) {
4606 this.$input
.prop( 'disabled', this.isDisabled() );
4609 this.menu
.setDisabled( this.isDisabled() );
4612 this.popup
.setDisabled( this.isDisabled() );
4616 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4617 this.items
[ i
].updateDisabled();
4629 OO
.ui
.CapsuleMultiselectWidget
.prototype.focus = function () {
4630 if ( !this.isDisabled() ) {
4632 this.popup
.setSize( this.$handle
.outerWidth() );
4633 this.popup
.toggle( true );
4634 OO
.ui
.findFocusable( this.popup
.$element
).focus();
4636 OO
.ui
.mixin
.TabIndexedElement
.prototype.focus
.call( this );
4643 * TagItemWidgets are used within a {@link OO.ui.TagMultiselectWidget
4644 * TagMultiselectWidget} to display the selected items.
4647 * @extends OO.ui.Widget
4648 * @mixins OO.ui.mixin.ItemWidget
4649 * @mixins OO.ui.mixin.LabelElement
4650 * @mixins OO.ui.mixin.FlaggedElement
4651 * @mixins OO.ui.mixin.TabIndexedElement
4652 * @mixins OO.ui.mixin.DraggableElement
4655 * @param {Object} [config] Configuration object
4656 * @cfg {boolean} [valid=true] Item is valid
4658 OO
.ui
.TagItemWidget
= function OoUiTagItemWidget( config
) {
4659 config
= config
|| {};
4661 // Parent constructor
4662 OO
.ui
.TagItemWidget
.parent
.call( this, config
);
4664 // Mixin constructors
4665 OO
.ui
.mixin
.ItemWidget
.call( this );
4666 OO
.ui
.mixin
.LabelElement
.call( this, config
);
4667 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4668 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4669 OO
.ui
.mixin
.DraggableElement
.call( this, config
);
4671 this.valid
= config
.valid
=== undefined ? true : !!config
.valid
;
4673 this.closeButton
= new OO
.ui
.ButtonWidget( {
4678 this.closeButton
.setDisabled( this.isDisabled() );
4682 .connect( this, { click
: 'remove' } );
4684 .on( 'click', this.select
.bind( this ) )
4685 .on( 'keydown', this.onKeyDown
.bind( this ) )
4686 // Prevent propagation of mousedown; the tag item "lives" in the
4687 // clickable area of the TagMultiselectWidget, which listens to
4688 // mousedown to open the menu or popup. We want to prevent that
4689 // for clicks specifically on the tag itself, so the actions taken
4690 // are more deliberate. When the tag is clicked, it will emit the
4691 // selection event (similar to how #OO.ui.MultioptionWidget emits 'change')
4692 // and can be handled separately.
4693 .on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
4697 .addClass( 'oo-ui-tagItemWidget' )
4698 .append( this.$label
, this.closeButton
.$element
);
4701 /* Initialization */
4703 OO
.inheritClass( OO
.ui
.TagItemWidget
, OO
.ui
.Widget
);
4704 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.ItemWidget
);
4705 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.LabelElement
);
4706 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
4707 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4708 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.DraggableElement
);
4715 * A remove action was performed on the item
4720 * @param {string} direction Direction of the movement, forward or backwards
4722 * A navigate action was performed on the item
4728 * The tag widget was selected. This can occur when the widget
4729 * is either clicked or enter was pressed on it.
4734 * @param {boolean} isValid Item is valid
4736 * Item validity has changed
4744 OO
.ui
.TagItemWidget
.prototype.setDisabled = function ( state
) {
4746 OO
.ui
.TagItemWidget
.parent
.prototype.setDisabled
.call( this, state
);
4748 if ( this.closeButton
) {
4749 this.closeButton
.setDisabled( state
);
4755 * Handle removal of the item
4757 * This is mainly for extensibility concerns, so other children
4758 * of this class can change the behavior if they need to. This
4759 * is called by both clicking the 'remove' button but also
4760 * on keypress, which is harder to override if needed.
4764 OO
.ui
.TagItemWidget
.prototype.remove = function () {
4765 if ( !this.isDisabled() ) {
4766 this.emit( 'remove' );
4771 * Handle a keydown event on the widget
4775 * @param {jQuery.Event} e Key down event
4776 * @return {boolean|undefined} false to stop the operation
4778 OO
.ui
.TagItemWidget
.prototype.onKeyDown = function ( e
) {
4781 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
) {
4784 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
4788 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4789 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4791 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
4805 e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
4806 movement
.left
: movement
.right
4816 OO
.ui
.TagItemWidget
.prototype.select = function () {
4817 if ( !this.isDisabled() ) {
4818 this.emit( 'select' );
4823 * Set the valid state of this item
4825 * @param {boolean} [valid] Item is valid
4828 OO
.ui
.TagItemWidget
.prototype.toggleValid = function ( valid
) {
4829 valid
= valid
=== undefined ? !this.valid
: !!valid
;
4831 if ( this.valid
!== valid
) {
4834 this.setFlags( { invalid
: !this.valid
} );
4836 this.emit( 'valid', this.valid
);
4841 * Check whether the item is valid
4843 * @return {boolean} Item is valid
4845 OO
.ui
.TagItemWidget
.prototype.isValid = function () {
4850 * A basic tag multiselect widget, similar in concept to {@link OO.ui.ComboBoxInputWidget combo box widget}
4851 * that allows the user to add multiple values that are displayed in a tag area.
4853 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
4855 * This widget is a base widget; see {@link OO.ui.MenuTagMultiselectWidget MenuTagMultiselectWidget} and
4856 * {@link OO.ui.PopupTagMultiselectWidget PopupTagMultiselectWidget} for the implementations that use
4857 * a menu and a popup respectively.
4860 * // Example: A basic TagMultiselectWidget.
4861 * var widget = new OO.ui.TagMultiselectWidget( {
4862 * inputPosition: 'outline',
4863 * allowedValues: [ 'Option 1', 'Option 2', 'Option 3' ],
4864 * selected: [ 'Option 1' ]
4866 * $( 'body' ).append( widget.$element );
4868 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
4871 * @extends OO.ui.Widget
4872 * @mixins OO.ui.mixin.GroupWidget
4873 * @mixins OO.ui.mixin.DraggableGroupElement
4874 * @mixins OO.ui.mixin.IndicatorElement
4875 * @mixins OO.ui.mixin.IconElement
4876 * @mixins OO.ui.mixin.TabIndexedElement
4877 * @mixins OO.ui.mixin.FlaggedElement
4880 * @param {Object} config Configuration object
4881 * @cfg {Object} [input] Configuration options for the input widget
4882 * @cfg {OO.ui.InputWidget} [inputWidget] An optional input widget. If given, it will
4883 * replace the input widget used in the TagMultiselectWidget. If not given,
4884 * TagMultiselectWidget creates its own.
4885 * @cfg {boolean} [inputPosition='inline'] Position of the input. Options are:
4886 * - inline: The input is invisible, but exists inside the tag list, so
4887 * the user types into the tag groups to add tags.
4888 * - outline: The input is underneath the tag area.
4889 * - none: No input supplied
4890 * @cfg {boolean} [allowEditTags=true] Allow editing of the tags by clicking them
4891 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if
4892 * not present in the menu.
4893 * @cfg {Object[]} [allowedValues] An array representing the allowed items
4895 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added
4896 * @cfg {boolean} [allowDisplayInvalidTags=false] Allow the display of
4897 * invalid tags. These tags will display with an invalid state, and
4898 * the widget as a whole will have an invalid state if any invalid tags
4900 * @cfg {boolean} [allowReordering=true] Allow reordering of the items
4901 * @cfg {Object[]|String[]} [selected] A set of selected tags. If given,
4902 * these will appear in the tag list on initialization, as long as they
4903 * pass the validity tests.
4905 OO
.ui
.TagMultiselectWidget
= function OoUiTagMultiselectWidget( config
) {
4907 rAF
= window
.requestAnimationFrame
|| setTimeout
,
4909 $tabFocus
= $( '<span>' )
4910 .addClass( 'oo-ui-tagMultiselectWidget-focusTrap' );
4912 config
= config
|| {};
4914 // Parent constructor
4915 OO
.ui
.TagMultiselectWidget
.parent
.call( this, config
);
4917 // Mixin constructors
4918 OO
.ui
.mixin
.GroupWidget
.call( this, config
);
4919 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
4920 OO
.ui
.mixin
.IconElement
.call( this, config
);
4921 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4922 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4923 OO
.ui
.mixin
.DraggableGroupElement
.call( this, config
);
4925 this.toggleDraggable(
4926 config
.allowReordering
=== undefined ?
4927 true : !!config
.allowReordering
4930 this.inputPosition
= this.constructor.static.allowedInputPositions
.indexOf( config
.inputPosition
) > -1 ?
4931 config
.inputPosition
: 'inline';
4932 this.allowEditTags
= config
.allowEditTags
=== undefined ? true : !!config
.allowEditTags
;
4933 this.allowArbitrary
= !!config
.allowArbitrary
;
4934 this.allowDuplicates
= !!config
.allowDuplicates
;
4935 this.allowedValues
= config
.allowedValues
|| [];
4936 this.allowDisplayInvalidTags
= config
.allowDisplayInvalidTags
;
4937 this.hasInput
= this.inputPosition
!== 'none';
4941 this.$content
= $( '<div>' )
4942 .addClass( 'oo-ui-tagMultiselectWidget-content' );
4943 this.$handle
= $( '<div>' )
4944 .addClass( 'oo-ui-tagMultiselectWidget-handle' )
4951 .addClass( 'oo-ui-tagMultiselectWidget-group' )
4957 remove
: 'itemRemove',
4958 navigate
: 'itemNavigate',
4959 select
: 'itemSelect'
4961 this.connect( this, {
4962 itemRemove
: 'onTagRemove',
4963 itemSelect
: 'onTagSelect',
4964 itemNavigate
: 'onTagNavigate',
4965 change
: 'onChangeTags'
4968 mousedown
: this.onMouseDown
.bind( this )
4973 .addClass( 'oo-ui-tagMultiselectWidget' )
4974 .append( this.$handle
);
4976 if ( this.hasInput
) {
4977 if ( config
.inputWidget
) {
4978 this.input
= config
.inputWidget
;
4980 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
4981 placeholder
: config
.placeholder
,
4982 classes
: [ 'oo-ui-tagMultiselectWidget-input' ]
4983 }, config
.input
) );
4985 this.input
.setDisabled( this.isDisabled() );
4988 focus
: this.onInputFocus
.bind( this ),
4989 blur
: this.onInputBlur
.bind( this ),
4990 'propertychange change click mouseup keydown keyup input cut paste select focus':
4991 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
4992 keydown
: this.onInputKeyDown
.bind( this ),
4993 keypress
: this.onInputKeyPress
.bind( this )
4996 this.input
.$input
.on( inputEvents
);
4998 if ( this.inputPosition
=== 'outline' ) {
4999 // Override max-height for the input widget
5000 // in the case the widget is outline so it can
5001 // stretch all the way if the widet is wide
5002 this.input
.$element
.css( 'max-width', 'inherit' );
5004 .addClass( 'oo-ui-tagMultiselectWidget-outlined' )
5005 .append( this.input
.$element
);
5007 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-inlined' );
5008 // HACK: When the widget is using 'inline' input, the
5009 // behavior needs to only use the $input itself
5010 // so we style and size it accordingly (otherwise
5011 // the styling and sizing can get very convoluted
5012 // when the wrapping divs and other elements)
5013 // We are taking advantage of still being able to
5014 // call the widget itself for operations like
5015 // .getValue() and setDisabled() and .focus() but
5016 // having only the $input attached to the DOM
5017 this.$content
.append( this.input
.$input
);
5020 this.$content
.append( $tabFocus
);
5023 this.setTabIndexedElement(
5029 if ( config
.selected
) {
5030 this.setValue( config
.selected
);
5033 // HACK: Input size needs to be calculated after everything
5036 if ( widget
.hasInput
) {
5037 widget
.updateInputSize();
5042 /* Initialization */
5044 OO
.inheritClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.Widget
);
5045 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.GroupWidget
);
5046 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.DraggableGroupElement
);
5047 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
5048 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
5049 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
5050 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.FlaggedElement
);
5052 /* Static properties */
5055 * Allowed input positions.
5056 * - inline: The input is inside the tag list
5057 * - outline: The input is under the tag list
5058 * - none: There is no input
5062 OO
.ui
.TagMultiselectWidget
.static.allowedInputPositions
= [ 'inline', 'outline', 'none' ];
5067 * Handle mouse down events.
5070 * @param {jQuery.Event} e Mouse down event
5071 * @return {boolean} False to prevent defaults
5073 OO
.ui
.TagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
5074 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
5081 * Handle key press events.
5084 * @param {jQuery.Event} e Key press event
5085 * @return {boolean} Whether to prevent defaults
5087 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyPress = function ( e
) {
5089 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
5091 if ( !this.isDisabled() ) {
5092 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
5093 stopOrContinue
= this.doInputEnter( e
, withMetaKey
);
5096 // Make sure the input gets resized.
5097 setTimeout( this.updateInputSize
.bind( this ), 0 );
5098 return stopOrContinue
;
5103 * Handle key down events.
5106 * @param {jQuery.Event} e Key down event
5109 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyDown = function ( e
) {
5110 var movement
, direction
,
5111 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
5113 if ( !this.isDisabled() ) {
5114 // 'keypress' event is not triggered for Backspace
5115 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
5116 return this.doInputBackspace( e
, withMetaKey
);
5117 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ESCAPE
) {
5118 return this.doInputEscape( e
);
5120 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
5121 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
5123 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
5134 direction
= e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
5135 movement
.left
: movement
.right
;
5137 return this.doInputArrow( e
, direction
, withMetaKey
);
5143 * Respond to input focus event
5145 OO
.ui
.TagMultiselectWidget
.prototype.onInputFocus = function () {
5146 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-focus' );
5150 * Respond to input blur event
5152 OO
.ui
.TagMultiselectWidget
.prototype.onInputBlur = function () {
5153 this.$element
.removeClass( 'oo-ui-tagMultiselectWidget-focus' );
5157 * Perform an action after the enter key on the input
5159 * @param {jQuery.Event} e Event data
5160 * @param {boolean} [withMetaKey] Whether this key was pressed with
5161 * a meta key like 'ctrl'
5162 * @return {boolean} Whether to prevent defaults
5164 OO
.ui
.TagMultiselectWidget
.prototype.doInputEnter = function () {
5165 this.addTagFromInput();
5170 * Perform an action responding to the enter key on the input
5172 * @param {jQuery.Event} e Event data
5173 * @param {boolean} [withMetaKey] Whether this key was pressed with
5174 * a meta key like 'ctrl'
5175 * @return {boolean} Whether to prevent defaults
5177 OO
.ui
.TagMultiselectWidget
.prototype.doInputBackspace = function ( e
, withMetaKey
) {
5181 this.inputPosition
=== 'inline' &&
5182 this.input
.getValue() === '' &&
5185 // Delete the last item
5186 items
= this.getItems();
5187 item
= items
[ items
.length
- 1 ];
5188 this.removeItems( [ item
] );
5189 // If Ctrl/Cmd was pressed, delete item entirely.
5190 // Otherwise put it into the text field for editing.
5191 if ( !withMetaKey
) {
5192 this.input
.setValue( item
.getData() );
5200 * Perform an action after the escape key on the input
5202 * @param {jQuery.Event} e Event data
5204 OO
.ui
.TagMultiselectWidget
.prototype.doInputEscape = function () {
5209 * Perform an action after the arrow key on the input, select the previous
5210 * or next item from the input.
5211 * See #getPreviousItem and #getNextItem
5213 * @param {jQuery.Event} e Event data
5214 * @param {string} direction Direction of the movement; forwards or backwards
5215 * @param {boolean} [withMetaKey] Whether this key was pressed with
5216 * a meta key like 'ctrl'
5218 OO
.ui
.TagMultiselectWidget
.prototype.doInputArrow = function ( e
, direction
) {
5220 this.inputPosition
=== 'inline' &&
5223 if ( direction
=== 'backwards' ) {
5224 // Get previous item
5225 this.getPreviousItem().focus();
5228 this.getNextItem().focus();
5234 * Respond to item select event
5236 * @param {OO.ui.TagItemWidget} item Selected item
5238 OO
.ui
.TagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5239 if ( this.hasInput
&& this.allowEditTags
) {
5240 if ( this.input
.getValue() ) {
5241 this.addTagFromInput();
5243 // 1. Get the label of the tag into the input
5244 this.input
.setValue( item
.getData() );
5245 // 2. Remove the tag
5246 this.removeItems( [ item
] );
5247 // 3. Focus the input
5253 * Respond to change event, where items were added, removed, or cleared.
5255 OO
.ui
.TagMultiselectWidget
.prototype.onChangeTags = function () {
5256 this.toggleValid( this.checkValidity() );
5257 if ( this.hasInput
) {
5258 this.updateInputSize();
5260 this.updateIfHeightChanged();
5266 OO
.ui
.TagMultiselectWidget
.prototype.setDisabled = function ( isDisabled
) {
5268 OO
.ui
.TagMultiselectWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
5270 if ( this.hasInput
&& this.input
) {
5271 this.input
.setDisabled( !!isDisabled
);
5275 this.getItems().forEach( function ( item
) {
5276 item
.setDisabled( !!isDisabled
);
5282 * Respond to tag remove event
5283 * @param {OO.ui.TagItemWidget} item Removed tag
5285 OO
.ui
.TagMultiselectWidget
.prototype.onTagRemove = function ( item
) {
5286 this.removeTagByData( item
.getData() );
5290 * Respond to navigate event on the tag
5292 * @param {OO.ui.TagItemWidget} item Removed tag
5293 * @param {string} direction Direction of movement; 'forwards' or 'backwards'
5295 OO
.ui
.TagMultiselectWidget
.prototype.onTagNavigate = function ( item
, direction
) {
5296 if ( direction
=== 'forwards' ) {
5297 this.getNextItem( item
).focus();
5299 this.getPreviousItem( item
).focus();
5304 * Add tag from input value
5306 OO
.ui
.TagMultiselectWidget
.prototype.addTagFromInput = function () {
5307 var val
= this.input
.getValue(),
5308 isValid
= this.isAllowedData( val
);
5314 if ( isValid
|| this.allowDisplayInvalidTags
) {
5324 OO
.ui
.TagMultiselectWidget
.prototype.clearInput = function () {
5325 this.input
.setValue( '' );
5329 * Check whether the given value is a duplicate of an existing
5330 * tag already in the list.
5332 * @param {string|Object} data Requested value
5333 * @return {boolean} Value is duplicate
5335 OO
.ui
.TagMultiselectWidget
.prototype.isDuplicateData = function ( data
) {
5336 return !!this.getItemFromData( data
);
5340 * Check whether a given value is allowed to be added
5342 * @param {string|Object} data Requested value
5343 * @return {boolean} Value is allowed
5345 OO
.ui
.TagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
5347 !this.allowDuplicates
&&
5348 this.isDuplicateData( data
)
5353 if ( this.allowArbitrary
) {
5357 // Check with allowed values
5359 this.getAllowedValues().some( function ( value
) {
5360 return data
=== value
;
5370 * Get the allowed values list
5372 * @return {string[]} Allowed data values
5374 OO
.ui
.TagMultiselectWidget
.prototype.getAllowedValues = function () {
5375 return this.allowedValues
;
5379 * Add a value to the allowed values list
5381 * @param {string} value Allowed data value
5383 OO
.ui
.TagMultiselectWidget
.prototype.addAllowedValue = function ( value
) {
5384 if ( this.allowedValues
.indexOf( value
) === -1 ) {
5385 this.allowedValues
.push( value
);
5390 * Get the datas of the currently selected items
5392 * @return {string[]|Object[]} Datas of currently selected items
5394 OO
.ui
.TagMultiselectWidget
.prototype.getValue = function () {
5395 return this.getItems()
5396 .filter( function ( item
) {
5397 return item
.isValid();
5399 .map( function ( item
) {
5400 return item
.getData();
5405 * Set the value of this widget by datas.
5407 * @param {string|string[]|Object|Object[]} valueObject An object representing the data
5408 * and label of the value. If the widget allows arbitrary values,
5409 * the items will be added as-is. Otherwise, the data value will
5410 * be checked against allowedValues.
5411 * This object must contain at least a data key. Example:
5412 * { data: 'foo', label: 'Foo item' }
5413 * For multiple items, use an array of objects. For example:
5415 * { data: 'foo', label: 'Foo item' },
5416 * { data: 'bar', label: 'Bar item' }
5418 * Value can also be added with plaintext array, for example:
5419 * [ 'foo', 'bar', 'bla' ] or a single string, like 'foo'
5421 OO
.ui
.TagMultiselectWidget
.prototype.setValue = function ( valueObject
) {
5422 valueObject
= Array
.isArray( valueObject
) ? valueObject
: [ valueObject
];
5425 valueObject
.forEach( function ( obj
) {
5426 if ( typeof obj
=== 'string' ) {
5429 this.addTag( obj
.data
, obj
.label
);
5435 * Add tag to the display area
5437 * @param {string|Object} data Tag data
5438 * @param {string} [label] Tag label. If no label is provided, the
5439 * stringified version of the data will be used instead.
5440 * @return {boolean} Item was added successfully
5442 OO
.ui
.TagMultiselectWidget
.prototype.addTag = function ( data
, label
) {
5444 isValid
= this.isAllowedData( data
);
5446 if ( isValid
|| this.allowDisplayInvalidTags
) {
5447 newItemWidget
= this.createTagItemWidget( data
, label
);
5448 newItemWidget
.toggleValid( isValid
);
5449 this.addItems( [ newItemWidget
] );
5456 * Remove tag by its data property.
5458 * @param {string|Object} data Tag data
5460 OO
.ui
.TagMultiselectWidget
.prototype.removeTagByData = function ( data
) {
5461 var item
= this.getItemFromData( data
);
5463 this.removeItems( [ item
] );
5467 * Construct a OO.ui.TagItemWidget (or a subclass thereof) from given label and data.
5470 * @param {string} data Item data
5471 * @param {string} label The label text.
5472 * @return {OO.ui.TagItemWidget}
5474 OO
.ui
.TagMultiselectWidget
.prototype.createTagItemWidget = function ( data
, label
) {
5475 label
= label
|| data
;
5477 return new OO
.ui
.TagItemWidget( { data
: data
, label
: label
} );
5481 * Given an item, returns the item after it. If the item is already the
5482 * last item, return `this.input`. If no item is passed, returns the
5486 * @param {OO.ui.TagItemWidget} [item] Tag item
5487 * @return {OO.ui.Widget} The next widget available.
5489 OO
.ui
.TagMultiselectWidget
.prototype.getNextItem = function ( item
) {
5490 var itemIndex
= this.items
.indexOf( item
);
5492 if ( item
=== undefined || itemIndex
=== -1 ) {
5493 return this.items
[ 0 ];
5496 if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
5497 if ( this.hasInput
) {
5500 // Return first item
5501 return this.items
[ 0 ];
5504 return this.items
[ itemIndex
+ 1 ];
5509 * Given an item, returns the item before it. If the item is already the
5510 * first item, return `this.input`. If no item is passed, returns the
5514 * @param {OO.ui.TagItemWidget} [item] Tag item
5515 * @return {OO.ui.Widget} The previous widget available.
5517 OO
.ui
.TagMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
5518 var itemIndex
= this.items
.indexOf( item
);
5520 if ( item
=== undefined || itemIndex
=== -1 ) {
5521 return this.items
[ this.items
.length
- 1 ];
5524 if ( itemIndex
=== 0 ) {
5525 if ( this.hasInput
) {
5528 // Return the last item
5529 return this.items
[ this.items
.length
- 1 ];
5532 return this.items
[ itemIndex
- 1 ];
5537 * Update the dimensions of the text input field to encompass all available area.
5538 * This is especially relevant for when the input is at the edge of a line
5539 * and should get smaller. The usual operation (as an inline-block with min-width)
5540 * does not work in that case, pushing the input downwards to the next line.
5544 OO
.ui
.TagMultiselectWidget
.prototype.updateInputSize = function () {
5545 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
5546 if ( this.inputPosition
=== 'inline' && !this.isDisabled() ) {
5547 if ( this.input
.$input
[ 0 ].scrollWidth
=== 0 ) {
5548 // Input appears to be attached but not visible.
5549 // Don't attempt to adjust its size, because our measurements
5550 // are going to fail anyway.
5553 this.input
.$input
.css( 'width', '1em' );
5554 $lastItem
= this.$group
.children().last();
5555 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
5557 // Get the width of the input with the placeholder text as
5558 // the value and save it so that we don't keep recalculating
5560 this.contentWidthWithPlaceholder
=== undefined &&
5561 this.input
.getValue() === '' &&
5562 this.input
.$input
.attr( 'placeholder' ) !== undefined
5564 this.input
.setValue( this.input
.$input
.attr( 'placeholder' ) );
5565 this.contentWidthWithPlaceholder
= this.input
.$input
[ 0 ].scrollWidth
;
5566 this.input
.setValue( '' );
5570 // Always keep the input wide enough for the placeholder text
5571 contentWidth
= Math
.max(
5572 this.input
.$input
[ 0 ].scrollWidth
,
5573 // undefined arguments in Math.max lead to NaN
5574 ( this.contentWidthWithPlaceholder
=== undefined ) ?
5575 0 : this.contentWidthWithPlaceholder
5577 currentWidth
= this.input
.$input
.width();
5579 if ( contentWidth
< currentWidth
) {
5580 this.updateIfHeightChanged();
5581 // All is fine, don't perform expensive calculations
5585 if ( $lastItem
.length
=== 0 ) {
5586 bestWidth
= this.$content
.innerWidth();
5588 bestWidth
= direction
=== 'ltr' ?
5589 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
5590 $lastItem
.position().left
;
5593 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
5594 // pixels this is off by are coming from.
5596 if ( contentWidth
> bestWidth
) {
5597 // This will result in the input getting shifted to the next line
5598 bestWidth
= this.$content
.innerWidth() - 10;
5600 this.input
.$input
.width( Math
.floor( bestWidth
) );
5601 this.updateIfHeightChanged();
5603 this.updateIfHeightChanged();
5608 * Determine if widget height changed, and if so,
5609 * emit the resize event. This is useful for when there are either
5610 * menus or popups attached to the bottom of the widget, to allow
5611 * them to change their positioning in case the widget moved down
5616 OO
.ui
.TagMultiselectWidget
.prototype.updateIfHeightChanged = function () {
5617 var height
= this.$element
.height();
5618 if ( height
!== this.height
) {
5619 this.height
= height
;
5620 this.emit( 'resize' );
5625 * Check whether all items in the widget are valid
5627 * @return {boolean} Widget is valid
5629 OO
.ui
.TagMultiselectWidget
.prototype.checkValidity = function () {
5630 return this.getItems().every( function ( item
) {
5631 return item
.isValid();
5636 * Set the valid state of this item
5638 * @param {boolean} [valid] Item is valid
5641 OO
.ui
.TagMultiselectWidget
.prototype.toggleValid = function ( valid
) {
5642 valid
= valid
=== undefined ? !this.valid
: !!valid
;
5644 if ( this.valid
!== valid
) {
5647 this.setFlags( { invalid
: !this.valid
} );
5649 this.emit( 'valid', this.valid
);
5654 * Get the current valid state of the widget
5656 * @return {boolean} Widget is valid
5658 OO
.ui
.TagMultiselectWidget
.prototype.isValid = function () {
5663 * PopupTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5664 * to use a popup. The popup can be configured to have a default input to insert values into the widget.
5666 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
5669 * // Example: A basic PopupTagMultiselectWidget.
5670 * var widget = new OO.ui.PopupTagMultiselectWidget();
5671 * $( 'body' ).append( widget.$element );
5673 * // Example: A PopupTagMultiselectWidget with an external popup.
5674 * var popupInput = new OO.ui.TextInputWidget(),
5675 * widget = new OO.ui.PopupTagMultiselectWidget( {
5676 * popupInput: popupInput,
5678 * $content: popupInput.$element
5681 * $( 'body' ).append( widget.$element );
5683 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
5686 * @extends OO.ui.TagMultiselectWidget
5687 * @mixins OO.ui.mixin.PopupElement
5689 * @param {Object} config Configuration object
5690 * @cfg {jQuery} [$overlay] An overlay for the popup.
5691 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
5692 * @cfg {Object} [popup] Configuration options for the popup
5693 * @cfg {OO.ui.InputWidget} [popupInput] An input widget inside the popup that will be
5694 * focused when the popup is opened and will be used as replacement for the
5695 * general input in the widget.
5697 OO
.ui
.PopupTagMultiselectWidget
= function OoUiPopupTagMultiselectWidget( config
) {
5699 defaultConfig
= { popup
: {} };
5701 config
= config
|| {};
5703 // Parent constructor
5704 OO
.ui
.PopupTagMultiselectWidget
.parent
.call( this, $.extend( { inputPosition
: 'none' }, config
) );
5706 this.$overlay
= config
.$overlay
|| this.$element
;
5708 if ( !config
.popup
) {
5709 // For the default base implementation, we give a popup
5710 // with an input widget inside it. For any other use cases
5711 // the popup needs to be populated externally and the
5712 // event handled to add tags separately and manually
5713 defaultInput
= new OO
.ui
.TextInputWidget();
5715 defaultConfig
.popupInput
= defaultInput
;
5716 defaultConfig
.popup
.$content
= defaultInput
.$element
;
5718 this.$element
.addClass( 'oo-ui-popupTagMultiselectWidget-defaultPopup' );
5721 // Add overlay, and add that to the autoCloseIgnore
5722 defaultConfig
.popup
.$overlay
= this.$overlay
;
5723 defaultConfig
.popup
.$autoCloseIgnore
= this.hasInput
?
5724 this.input
.$element
.add( this.$overlay
) : this.$overlay
;
5726 // Allow extending any of the above
5727 config
= $.extend( defaultConfig
, config
);
5729 // Mixin constructors
5730 OO
.ui
.mixin
.PopupElement
.call( this, config
);
5732 if ( this.hasInput
) {
5733 this.input
.$input
.on( 'focus', this.popup
.toggle
.bind( this.popup
, true ) );
5736 // Configuration options
5737 this.popupInput
= config
.popupInput
;
5738 if ( this.popupInput
) {
5739 this.popupInput
.connect( this, {
5740 enter
: 'onPopupInputEnter'
5745 this.on( 'resize', this.popup
.updateDimensions
.bind( this.popup
) );
5746 this.popup
.connect( this, { toggle
: 'onPopupToggle' } );
5748 .on( 'focus', this.onFocus
.bind( this ) );
5752 .append( this.popup
.$element
)
5753 .addClass( 'oo-ui-popupTagMultiselectWidget' );
5756 /* Initialization */
5758 OO
.inheritClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5759 OO
.mixinClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
5764 * Focus event handler.
5768 OO
.ui
.PopupTagMultiselectWidget
.prototype.onFocus = function () {
5769 this.popup
.toggle( true );
5773 * Respond to popup toggle event
5775 * @param {boolean} isVisible Popup is visible
5777 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupToggle = function ( isVisible
) {
5778 if ( isVisible
&& this.popupInput
) {
5779 this.popupInput
.focus();
5784 * Respond to popup input enter event
5786 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupInputEnter = function () {
5787 if ( this.popupInput
) {
5788 this.addTagByPopupValue( this.popupInput
.getValue() );
5789 this.popupInput
.setValue( '' );
5796 OO
.ui
.PopupTagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5797 if ( this.popupInput
&& this.allowEditTags
) {
5798 this.popupInput
.setValue( item
.getData() );
5799 this.removeItems( [ item
] );
5801 this.popup
.toggle( true );
5802 this.popupInput
.focus();
5805 OO
.ui
.PopupTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, item
);
5810 * Add a tag by the popup value.
5811 * Whatever is responsible for setting the value in the popup should call
5812 * this method to add a tag, or use the regular methods like #addTag or
5813 * #setValue directly.
5815 * @param {string} data The value of item
5816 * @param {string} [label] The label of the tag. If not given, the data is used.
5818 OO
.ui
.PopupTagMultiselectWidget
.prototype.addTagByPopupValue = function ( data
, label
) {
5819 this.addTag( data
, label
);
5823 * MenuTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5824 * to use a menu of selectable options.
5826 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
5829 * // Example: A basic MenuTagMultiselectWidget.
5830 * var widget = new OO.ui.MenuTagMultiselectWidget( {
5831 * inputPosition: 'outline',
5833 * { data: 'option1', label: 'Option 1' },
5834 * { data: 'option2', label: 'Option 2' },
5835 * { data: 'option3', label: 'Option 3' },
5837 * selected: [ 'option1', 'option2' ]
5839 * $( 'body' ).append( widget.$element );
5841 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
5844 * @extends OO.ui.TagMultiselectWidget
5847 * @param {Object} [config] Configuration object
5848 * @cfg {Object} [menu] Configuration object for the menu widget
5849 * @cfg {jQuery} [$overlay] An overlay for the menu.
5850 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
5851 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
5853 OO
.ui
.MenuTagMultiselectWidget
= function OoUiMenuTagMultiselectWidget( config
) {
5854 config
= config
|| {};
5856 // Parent constructor
5857 OO
.ui
.MenuTagMultiselectWidget
.parent
.call( this, config
);
5859 this.$overlay
= config
.$overlay
|| this.$element
;
5861 this.menu
= this.createMenuWidget( $.extend( {
5863 input
: this.hasInput
? this.input
: null,
5864 $input
: this.hasInput
? this.input
.$input
: null,
5865 filterFromInput
: !!this.hasInput
,
5866 $autoCloseIgnore
: this.hasInput
?
5867 this.input
.$element
.add( this.$overlay
) : this.$overlay
,
5868 $floatableContainer
: this.hasInput
&& this.inputPosition
=== 'outline' ?
5869 this.input
.$element
: this.$element
,
5870 $overlay
: this.$overlay
,
5871 disabled
: this.isDisabled()
5873 this.addOptions( config
.options
|| [] );
5876 this.menu
.connect( this, {
5877 choose
: 'onMenuChoose',
5878 toggle
: 'onMenuToggle'
5880 if ( this.hasInput
) {
5881 this.input
.connect( this, { change
: 'onInputChange' } );
5883 this.connect( this, { resize
: 'onResize' } );
5887 .append( this.menu
.$element
);
5889 .addClass( 'oo-ui-menuTagMultiselectWidget' );
5890 // TagMultiselectWidget already does this, but it doesn't work right because this.menu is not yet
5891 // set up while the parent constructor runs, and #getAllowedValues rejects everything.
5892 if ( config
.selected
) {
5893 this.setValue( config
.selected
);
5897 /* Initialization */
5899 OO
.inheritClass( OO
.ui
.MenuTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5904 * Respond to resize event
5906 OO
.ui
.MenuTagMultiselectWidget
.prototype.onResize = function () {
5907 // Reposition the menu
5908 this.menu
.position();
5914 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputFocus = function () {
5916 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
5918 this.menu
.toggle( true );
5922 * Respond to input change event
5924 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputChange = function () {
5925 this.menu
.toggle( true );
5929 * Respond to menu choose event
5931 * @param {OO.ui.OptionWidget} menuItem Chosen menu item
5933 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuChoose = function ( menuItem
) {
5935 this.addTag( menuItem
.getData(), menuItem
.getLabel() );
5939 * Respond to menu toggle event. Reset item highlights on hide.
5941 * @param {boolean} isVisible The menu is visible
5943 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
5945 this.menu
.selectItem( null );
5946 this.menu
.highlightItem( null );
5953 OO
.ui
.MenuTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
5954 var menuItem
= this.menu
.getItemFromData( tagItem
.getData() );
5955 // Override the base behavior from TagMultiselectWidget; the base behavior
5956 // in TagMultiselectWidget is to remove the tag to edit it in the input,
5957 // but in our case, we want to utilize the menu selection behavior, and
5958 // definitely not remove the item.
5960 // Select the menu item
5961 this.menu
.selectItem( menuItem
);
5969 OO
.ui
.MenuTagMultiselectWidget
.prototype.addTagFromInput = function () {
5970 var inputValue
= this.input
.getValue(),
5972 highlightedItem
= this.menu
.getHighlightedItem(),
5973 item
= this.menu
.getItemFromData( inputValue
);
5975 // Override the parent method so we add from the menu
5976 // rather than directly from the input
5978 // Look for a highlighted item first
5979 if ( highlightedItem
) {
5980 validated
= this.addTag( highlightedItem
.getData(), highlightedItem
.getLabel() );
5981 } else if ( item
) {
5982 // Look for the element that fits the data
5983 validated
= this.addTag( item
.getData(), item
.getLabel() );
5985 // Otherwise, add the tag - the method will only add if the
5986 // tag is valid or if invalid tags are allowed
5987 validated
= this.addTag( inputValue
);
5997 * Return the visible items in the menu. This is mainly used for when
5998 * the menu is filtering results.
6000 * @return {OO.ui.MenuOptionWidget[]} Visible results
6002 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenuVisibleItems = function () {
6003 return this.menu
.getItems().filter( function ( menuItem
) {
6004 return menuItem
.isVisible();
6009 * Create the menu for this widget. This is in a separate method so that
6010 * child classes can override this without polluting the constructor with
6011 * unnecessary extra objects that will be overidden.
6013 * @param {Object} menuConfig Configuration options
6014 * @return {OO.ui.MenuSelectWidget} Menu widget
6016 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
6017 return new OO
.ui
.MenuSelectWidget( menuConfig
);
6021 * Add options to the menu
6023 * @param {Object[]} menuOptions Object defining options
6025 OO
.ui
.MenuTagMultiselectWidget
.prototype.addOptions = function ( menuOptions
) {
6027 items
= menuOptions
.map( function ( obj
) {
6028 return widget
.createMenuOptionWidget( obj
.data
, obj
.label
);
6031 this.menu
.addItems( items
);
6035 * Create a menu option widget.
6037 * @param {string} data Item data
6038 * @param {string} [label] Item label
6039 * @return {OO.ui.OptionWidget} Option widget
6041 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuOptionWidget = function ( data
, label
) {
6042 return new OO
.ui
.MenuOptionWidget( {
6044 label
: label
|| data
6051 * @return {OO.ui.MenuSelectWidget} Menu
6053 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenu = function () {
6058 * Get the allowed values list
6060 * @return {string[]} Allowed data values
6062 OO
.ui
.MenuTagMultiselectWidget
.prototype.getAllowedValues = function () {
6065 // If the parent constructor is calling us, we're not ready yet, this.menu is not set up.
6066 menuDatas
= this.menu
.getItems().map( function ( menuItem
) {
6067 return menuItem
.getData();
6070 return this.allowedValues
.concat( menuDatas
);
6074 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
6075 * widgets can be configured with {@link OO.ui.mixin.IconElement icons} and {@link
6076 * OO.ui.mixin.IndicatorElement indicators}.
6077 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
6080 * // Example of a file select widget
6081 * var selectFile = new OO.ui.SelectFileWidget();
6082 * $( 'body' ).append( selectFile.$element );
6084 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets
6087 * @extends OO.ui.Widget
6088 * @mixins OO.ui.mixin.IconElement
6089 * @mixins OO.ui.mixin.IndicatorElement
6090 * @mixins OO.ui.mixin.PendingElement
6091 * @mixins OO.ui.mixin.LabelElement
6094 * @param {Object} [config] Configuration options
6095 * @cfg {string[]|null} [accept=null] MIME types to accept. null accepts all types.
6096 * @cfg {string} [placeholder] Text to display when no file is selected.
6097 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
6098 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
6099 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be true.
6100 * @cfg {number} [thumbnailSizeLimit=20] File size limit in MiB above which to not try and show a
6101 * preview (for performance)
6103 OO
.ui
.SelectFileWidget
= function OoUiSelectFileWidget( config
) {
6106 // Configuration initialization
6107 config
= $.extend( {
6109 placeholder
: OO
.ui
.msg( 'ooui-selectfile-placeholder' ),
6110 notsupported
: OO
.ui
.msg( 'ooui-selectfile-not-supported' ),
6112 showDropTarget
: false,
6113 thumbnailSizeLimit
: 20
6116 // Parent constructor
6117 OO
.ui
.SelectFileWidget
.parent
.call( this, config
);
6119 // Mixin constructors
6120 OO
.ui
.mixin
.IconElement
.call( this, config
);
6121 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
6122 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {}, config
, { $pending
: this.$info
} ) );
6123 OO
.ui
.mixin
.LabelElement
.call( this, config
);
6126 this.$info
= $( '<span>' );
6127 this.showDropTarget
= config
.showDropTarget
;
6128 this.thumbnailSizeLimit
= config
.thumbnailSizeLimit
;
6129 this.isSupported
= this.constructor.static.isSupported();
6130 this.currentFile
= null;
6131 if ( Array
.isArray( config
.accept
) ) {
6132 this.accept
= config
.accept
;
6136 this.placeholder
= config
.placeholder
;
6137 this.notsupported
= config
.notsupported
;
6138 this.onFileSelectedHandler
= this.onFileSelected
.bind( this );
6140 this.selectButton
= new OO
.ui
.ButtonWidget( {
6141 classes
: [ 'oo-ui-selectFileWidget-selectButton' ],
6142 label
: OO
.ui
.msg( 'ooui-selectfile-button-select' ),
6143 disabled
: this.disabled
|| !this.isSupported
6146 this.clearButton
= new OO
.ui
.ButtonWidget( {
6147 classes
: [ 'oo-ui-selectFileWidget-clearButton' ],
6150 disabled
: this.disabled
6154 this.selectButton
.$button
.on( {
6155 keypress
: this.onKeyPress
.bind( this )
6157 this.clearButton
.connect( this, {
6158 click
: 'onClearClick'
6160 if ( config
.droppable
) {
6161 dragHandler
= this.onDragEnterOrOver
.bind( this );
6163 dragenter
: dragHandler
,
6164 dragover
: dragHandler
,
6165 dragleave
: this.onDragLeave
.bind( this ),
6166 drop
: this.onDrop
.bind( this )
6172 this.$label
.addClass( 'oo-ui-selectFileWidget-label' );
6174 .addClass( 'oo-ui-selectFileWidget-info' )
6175 .append( this.$icon
, this.$label
, this.clearButton
.$element
, this.$indicator
);
6177 if ( config
.droppable
&& config
.showDropTarget
) {
6178 this.selectButton
.setIcon( 'upload' );
6179 this.$thumbnail
= $( '<div>' ).addClass( 'oo-ui-selectFileWidget-thumbnail' );
6180 this.setPendingElement( this.$thumbnail
);
6182 .addClass( 'oo-ui-selectFileWidget-dropTarget oo-ui-selectFileWidget' )
6184 click
: this.onDropTargetClick
.bind( this )
6189 this.selectButton
.$element
,
6191 .addClass( 'oo-ui-selectFileWidget-dropLabel' )
6192 .text( OO
.ui
.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
6196 .addClass( 'oo-ui-selectFileWidget' )
6197 .append( this.$info
, this.selectButton
.$element
);
6204 OO
.inheritClass( OO
.ui
.SelectFileWidget
, OO
.ui
.Widget
);
6205 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IconElement
);
6206 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IndicatorElement
);
6207 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.PendingElement
);
6208 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.LabelElement
);
6210 /* Static Properties */
6213 * Check if this widget is supported
6218 OO
.ui
.SelectFileWidget
.static.isSupported = function () {
6220 if ( OO
.ui
.SelectFileWidget
.static.isSupportedCache
=== null ) {
6221 $input
= $( '<input>' ).attr( 'type', 'file' );
6222 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= $input
[ 0 ].files
!== undefined;
6224 return OO
.ui
.SelectFileWidget
.static.isSupportedCache
;
6227 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= null;
6234 * A change event is emitted when the on/off state of the toggle changes.
6236 * @param {File|null} value New value
6242 * Get the current value of the field
6244 * @return {File|null}
6246 OO
.ui
.SelectFileWidget
.prototype.getValue = function () {
6247 return this.currentFile
;
6251 * Set the current value of the field
6253 * @param {File|null} file File to select
6255 OO
.ui
.SelectFileWidget
.prototype.setValue = function ( file
) {
6256 if ( this.currentFile
!== file
) {
6257 this.currentFile
= file
;
6259 this.emit( 'change', this.currentFile
);
6266 * Focusses the select file button.
6270 OO
.ui
.SelectFileWidget
.prototype.focus = function () {
6271 this.selectButton
.focus();
6280 OO
.ui
.SelectFileWidget
.prototype.blur = function () {
6281 this.selectButton
.blur();
6288 OO
.ui
.SelectFileWidget
.prototype.simulateLabelClick = function () {
6293 * Update the user interface when a file is selected or unselected
6297 OO
.ui
.SelectFileWidget
.prototype.updateUI = function () {
6299 if ( !this.isSupported
) {
6300 this.$element
.addClass( 'oo-ui-selectFileWidget-notsupported' );
6301 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
6302 this.setLabel( this.notsupported
);
6304 this.$element
.addClass( 'oo-ui-selectFileWidget-supported' );
6305 if ( this.currentFile
) {
6306 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
6308 $label
= $label
.add(
6310 .addClass( 'oo-ui-selectFileWidget-fileName' )
6311 .text( this.currentFile
.name
)
6313 this.setLabel( $label
);
6315 if ( this.showDropTarget
) {
6317 this.loadAndGetImageUrl().done( function ( url
) {
6318 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
6319 }.bind( this ) ).fail( function () {
6320 this.$thumbnail
.append(
6321 new OO
.ui
.IconWidget( {
6323 classes
: [ 'oo-ui-selectFileWidget-noThumbnail-icon' ]
6326 }.bind( this ) ).always( function () {
6329 this.$element
.off( 'click' );
6332 if ( this.showDropTarget
) {
6333 this.$element
.off( 'click' );
6335 click
: this.onDropTargetClick
.bind( this )
6339 .css( 'background-image', '' );
6341 this.$element
.addClass( 'oo-ui-selectFileWidget-empty' );
6342 this.setLabel( this.placeholder
);
6348 * If the selected file is an image, get its URL and load it.
6350 * @return {jQuery.Promise} Promise resolves with the image URL after it has loaded
6352 OO
.ui
.SelectFileWidget
.prototype.loadAndGetImageUrl = function () {
6353 var deferred
= $.Deferred(),
6354 file
= this.currentFile
,
6355 reader
= new FileReader();
6359 ( OO
.getProp( file
, 'type' ) || '' ).indexOf( 'image/' ) === 0 &&
6360 file
.size
< this.thumbnailSizeLimit
* 1024 * 1024
6362 reader
.onload = function ( event
) {
6363 var img
= document
.createElement( 'img' );
6364 img
.addEventListener( 'load', function () {
6366 img
.naturalWidth
=== 0 ||
6367 img
.naturalHeight
=== 0 ||
6368 img
.complete
=== false
6372 deferred
.resolve( event
.target
.result
);
6375 img
.src
= event
.target
.result
;
6377 reader
.readAsDataURL( file
);
6382 return deferred
.promise();
6386 * Add the input to the widget
6390 OO
.ui
.SelectFileWidget
.prototype.addInput = function () {
6391 if ( this.$input
) {
6392 this.$input
.remove();
6395 if ( !this.isSupported
) {
6400 this.$input
= $( '<input>' ).attr( 'type', 'file' );
6401 this.$input
.on( 'change', this.onFileSelectedHandler
);
6402 this.$input
.on( 'click', function ( e
) {
6403 // Prevents dropTarget to get clicked which calls
6404 // a click on this input
6405 e
.stopPropagation();
6410 if ( this.accept
) {
6411 this.$input
.attr( 'accept', this.accept
.join( ', ' ) );
6413 this.selectButton
.$button
.append( this.$input
);
6417 * Determine if we should accept this file
6420 * @param {string} mimeType File MIME type
6423 OO
.ui
.SelectFileWidget
.prototype.isAllowedType = function ( mimeType
) {
6426 if ( !this.accept
|| !mimeType
) {
6430 for ( i
= 0; i
< this.accept
.length
; i
++ ) {
6431 mimeTest
= this.accept
[ i
];
6432 if ( mimeTest
=== mimeType
) {
6434 } else if ( mimeTest
.substr( -2 ) === '/*' ) {
6435 mimeTest
= mimeTest
.substr( 0, mimeTest
.length
- 1 );
6436 if ( mimeType
.substr( 0, mimeTest
.length
) === mimeTest
) {
6446 * Handle file selection from the input
6449 * @param {jQuery.Event} e
6451 OO
.ui
.SelectFileWidget
.prototype.onFileSelected = function ( e
) {
6452 var file
= OO
.getProp( e
.target
, 'files', 0 ) || null;
6454 if ( file
&& !this.isAllowedType( file
.type
) ) {
6458 this.setValue( file
);
6463 * Handle clear button click events.
6467 OO
.ui
.SelectFileWidget
.prototype.onClearClick = function () {
6468 this.setValue( null );
6473 * Handle key press events.
6476 * @param {jQuery.Event} e Key press event
6478 OO
.ui
.SelectFileWidget
.prototype.onKeyPress = function ( e
) {
6479 if ( this.isSupported
&& !this.isDisabled() && this.$input
&&
6480 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
6482 this.$input
.click();
6488 * Handle drop target click events.
6491 * @param {jQuery.Event} e Key press event
6493 OO
.ui
.SelectFileWidget
.prototype.onDropTargetClick = function () {
6494 if ( this.isSupported
&& !this.isDisabled() && this.$input
) {
6495 this.$input
.click();
6501 * Handle drag enter and over events
6504 * @param {jQuery.Event} e Drag event
6506 OO
.ui
.SelectFileWidget
.prototype.onDragEnterOrOver = function ( e
) {
6508 droppableFile
= false,
6509 dt
= e
.originalEvent
.dataTransfer
;
6512 e
.stopPropagation();
6514 if ( this.isDisabled() || !this.isSupported
) {
6515 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6516 dt
.dropEffect
= 'none';
6520 // DataTransferItem and File both have a type property, but in Chrome files
6521 // have no information at this point.
6522 itemOrFile
= OO
.getProp( dt
, 'items', 0 ) || OO
.getProp( dt
, 'files', 0 );
6524 if ( this.isAllowedType( itemOrFile
.type
) ) {
6525 droppableFile
= true;
6527 // dt.types is Array-like, but not an Array
6528 } else if ( Array
.prototype.indexOf
.call( OO
.getProp( dt
, 'types' ) || [], 'Files' ) !== -1 ) {
6529 // File information is not available at this point for security so just assume
6530 // it is acceptable for now.
6531 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
6532 droppableFile
= true;
6535 this.$element
.toggleClass( 'oo-ui-selectFileWidget-canDrop', droppableFile
);
6536 if ( !droppableFile
) {
6537 dt
.dropEffect
= 'none';
6544 * Handle drag leave events
6547 * @param {jQuery.Event} e Drag event
6549 OO
.ui
.SelectFileWidget
.prototype.onDragLeave = function () {
6550 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6554 * Handle drop events
6557 * @param {jQuery.Event} e Drop event
6559 OO
.ui
.SelectFileWidget
.prototype.onDrop = function ( e
) {
6561 dt
= e
.originalEvent
.dataTransfer
;
6564 e
.stopPropagation();
6565 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6567 if ( this.isDisabled() || !this.isSupported
) {
6571 file
= OO
.getProp( dt
, 'files', 0 );
6572 if ( file
&& !this.isAllowedType( file
.type
) ) {
6576 this.setValue( file
);
6585 OO
.ui
.SelectFileWidget
.prototype.setDisabled = function ( disabled
) {
6586 OO
.ui
.SelectFileWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
6587 if ( this.selectButton
) {
6588 this.selectButton
.setDisabled( disabled
);
6590 if ( this.clearButton
) {
6591 this.clearButton
.setDisabled( disabled
);
6597 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field}, where users can type a search query,
6598 * and a menu of search results, which is displayed beneath the query
6599 * field. Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible to the user.
6600 * Users can choose an item from the menu or type a query into the text field to search for a matching result item.
6601 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
6603 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
6604 * the [OOjs UI demos][1] for an example.
6606 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/#dialogs-mediawiki-vector-ltr
6609 * @extends OO.ui.Widget
6612 * @param {Object} [config] Configuration options
6613 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6614 * @cfg {string} [value] Initial query value
6616 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
6617 // Configuration initialization
6618 config
= config
|| {};
6620 // Parent constructor
6621 OO
.ui
.SearchWidget
.parent
.call( this, config
);
6624 this.query
= new OO
.ui
.TextInputWidget( {
6626 placeholder
: config
.placeholder
,
6629 this.results
= new OO
.ui
.SelectWidget();
6630 this.$query
= $( '<div>' );
6631 this.$results
= $( '<div>' );
6634 this.query
.connect( this, {
6635 change
: 'onQueryChange',
6636 enter
: 'onQueryEnter'
6638 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
6642 .addClass( 'oo-ui-searchWidget-query' )
6643 .append( this.query
.$element
);
6645 .addClass( 'oo-ui-searchWidget-results' )
6646 .append( this.results
.$element
);
6648 .addClass( 'oo-ui-searchWidget' )
6649 .append( this.$results
, this.$query
);
6654 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
6659 * Handle query key down events.
6662 * @param {jQuery.Event} e Key down event
6664 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
6665 var highlightedItem
, nextItem
,
6666 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
6669 highlightedItem
= this.results
.getHighlightedItem();
6670 if ( !highlightedItem
) {
6671 highlightedItem
= this.results
.getSelectedItem();
6673 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
6674 this.results
.highlightItem( nextItem
);
6675 nextItem
.scrollElementIntoView();
6680 * Handle select widget select events.
6682 * Clears existing results. Subclasses should repopulate items according to new query.
6685 * @param {string} value New value
6687 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
6689 this.results
.clearItems();
6693 * Handle select widget enter key events.
6695 * Chooses highlighted item.
6698 * @param {string} value New value
6700 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
6701 var highlightedItem
= this.results
.getHighlightedItem();
6702 if ( highlightedItem
) {
6703 this.results
.chooseItem( highlightedItem
);
6708 * Get the query input.
6710 * @return {OO.ui.TextInputWidget} Query input
6712 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
6717 * Get the search results menu.
6719 * @return {OO.ui.SelectWidget} Menu of search results
6721 OO
.ui
.SearchWidget
.prototype.getResults = function () {
6722 return this.results
;
6726 * NumberInputWidgets combine a {@link OO.ui.TextInputWidget text input} (where a value
6727 * can be entered manually) and two {@link OO.ui.ButtonWidget button widgets}
6728 * (to adjust the value in increments) to allow the user to enter a number.
6731 * // Example: A NumberInputWidget.
6732 * var numberInput = new OO.ui.NumberInputWidget( {
6733 * label: 'NumberInputWidget',
6734 * input: { value: 5 },
6738 * $( 'body' ).append( numberInput.$element );
6741 * @extends OO.ui.TextInputWidget
6744 * @param {Object} [config] Configuration options
6745 * @cfg {Object} [minusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget decrementing button widget}.
6746 * @cfg {Object} [plusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget incrementing button widget}.
6747 * @cfg {boolean} [allowInteger=false] Whether the field accepts only integer values.
6748 * @cfg {number} [min=-Infinity] Minimum allowed value
6749 * @cfg {number} [max=Infinity] Maximum allowed value
6750 * @cfg {number} [step=1] Delta when using the buttons or up/down arrow keys
6751 * @cfg {number|null} [pageStep] Delta when using the page-up/page-down keys. Defaults to 10 times #step.
6752 * @cfg {boolean} [showButtons=true] Whether to show the plus and minus buttons.
6754 OO
.ui
.NumberInputWidget
= function OoUiNumberInputWidget( config
) {
6755 var $field
= $( '<div>' )
6756 .addClass( 'oo-ui-numberInputWidget-field' );
6758 // Configuration initialization
6759 config
= $.extend( {
6760 allowInteger
: false,
6768 // For backward compatibility
6769 $.extend( config
, config
.input
);
6772 // Parent constructor
6773 OO
.ui
.NumberInputWidget
.parent
.call( this, $.extend( config
, {
6777 if ( config
.showButtons
) {
6778 this.minusButton
= new OO
.ui
.ButtonWidget( $.extend(
6780 disabled
: this.isDisabled(),
6782 classes
: [ 'oo-ui-numberInputWidget-minusButton' ],
6787 this.plusButton
= new OO
.ui
.ButtonWidget( $.extend(
6789 disabled
: this.isDisabled(),
6791 classes
: [ 'oo-ui-numberInputWidget-plusButton' ],
6800 keydown
: this.onKeyDown
.bind( this ),
6801 'wheel mousewheel DOMMouseScroll': this.onWheel
.bind( this )
6803 if ( config
.showButtons
) {
6804 this.plusButton
.connect( this, {
6805 click
: [ 'onButtonClick', +1 ]
6807 this.minusButton
.connect( this, {
6808 click
: [ 'onButtonClick', -1 ]
6813 $field
.append( this.$input
);
6814 if ( config
.showButtons
) {
6816 .prepend( this.minusButton
.$element
)
6817 .append( this.plusButton
.$element
);
6821 this.setAllowInteger( config
.allowInteger
|| config
.isInteger
);
6822 this.setRange( config
.min
, config
.max
);
6823 this.setStep( config
.step
, config
.pageStep
);
6824 // Set the validation method after we set allowInteger and range
6825 // so that it doesn't immediately call setValidityFlag
6826 this.setValidation( this.validateNumber
.bind( this ) );
6829 .addClass( 'oo-ui-numberInputWidget' )
6830 .toggleClass( 'oo-ui-numberInputWidget-buttoned', config
.showButtons
)
6836 OO
.inheritClass( OO
.ui
.NumberInputWidget
, OO
.ui
.TextInputWidget
);
6841 * Set whether only integers are allowed
6843 * @param {boolean} flag
6845 OO
.ui
.NumberInputWidget
.prototype.setAllowInteger = function ( flag
) {
6846 this.allowInteger
= !!flag
;
6847 this.setValidityFlag();
6849 // Backward compatibility
6850 OO
.ui
.NumberInputWidget
.prototype.setIsInteger
= OO
.ui
.NumberInputWidget
.prototype.setAllowInteger
;
6853 * Get whether only integers are allowed
6855 * @return {boolean} Flag value
6857 OO
.ui
.NumberInputWidget
.prototype.getAllowInteger = function () {
6858 return this.allowInteger
;
6860 // Backward compatibility
6861 OO
.ui
.NumberInputWidget
.prototype.getIsInteger
= OO
.ui
.NumberInputWidget
.prototype.getAllowInteger
;
6864 * Set the range of allowed values
6866 * @param {number} min Minimum allowed value
6867 * @param {number} max Maximum allowed value
6869 OO
.ui
.NumberInputWidget
.prototype.setRange = function ( min
, max
) {
6871 throw new Error( 'Minimum (' + min
+ ') must not be greater than maximum (' + max
+ ')' );
6875 this.setValidityFlag();
6879 * Get the current range
6881 * @return {number[]} Minimum and maximum values
6883 OO
.ui
.NumberInputWidget
.prototype.getRange = function () {
6884 return [ this.min
, this.max
];
6888 * Set the stepping deltas
6890 * @param {number} step Normal step
6891 * @param {number|null} pageStep Page step. If null, 10 * step will be used.
6893 OO
.ui
.NumberInputWidget
.prototype.setStep = function ( step
, pageStep
) {
6895 throw new Error( 'Step value must be positive' );
6897 if ( pageStep
=== null ) {
6898 pageStep
= step
* 10;
6899 } else if ( pageStep
<= 0 ) {
6900 throw new Error( 'Page step value must be positive' );
6903 this.pageStep
= pageStep
;
6907 * Get the current stepping values
6909 * @return {number[]} Step and page step
6911 OO
.ui
.NumberInputWidget
.prototype.getStep = function () {
6912 return [ this.step
, this.pageStep
];
6916 * Get the current value of the widget as a number
6918 * @return {number} May be NaN, or an invalid number
6920 OO
.ui
.NumberInputWidget
.prototype.getNumericValue = function () {
6921 return +this.getValue();
6925 * Adjust the value of the widget
6927 * @param {number} delta Adjustment amount
6929 OO
.ui
.NumberInputWidget
.prototype.adjustValue = function ( delta
) {
6930 var n
, v
= this.getNumericValue();
6933 if ( isNaN( delta
) || !isFinite( delta
) ) {
6934 throw new Error( 'Delta must be a finite number' );
6941 n
= Math
.max( Math
.min( n
, this.max
), this.min
);
6942 if ( this.allowInteger
) {
6943 n
= Math
.round( n
);
6955 * @param {string} value Field value
6958 OO
.ui
.NumberInputWidget
.prototype.validateNumber = function ( value
) {
6960 if ( value
=== '' ) {
6961 return !this.isRequired();
6964 if ( isNaN( n
) || !isFinite( n
) ) {
6968 if ( this.allowInteger
&& Math
.floor( n
) !== n
) {
6972 if ( n
< this.min
|| n
> this.max
) {
6980 * Handle mouse click events.
6983 * @param {number} dir +1 or -1
6985 OO
.ui
.NumberInputWidget
.prototype.onButtonClick = function ( dir
) {
6986 this.adjustValue( dir
* this.step
);
6990 * Handle mouse wheel events.
6993 * @param {jQuery.Event} event
6995 OO
.ui
.NumberInputWidget
.prototype.onWheel = function ( event
) {
6998 if ( !this.isDisabled() && this.$input
.is( ':focus' ) ) {
6999 // Standard 'wheel' event
7000 if ( event
.originalEvent
.deltaMode
!== undefined ) {
7001 this.sawWheelEvent
= true;
7003 if ( event
.originalEvent
.deltaY
) {
7004 delta
= -event
.originalEvent
.deltaY
;
7005 } else if ( event
.originalEvent
.deltaX
) {
7006 delta
= event
.originalEvent
.deltaX
;
7009 // Non-standard events
7010 if ( !this.sawWheelEvent
) {
7011 if ( event
.originalEvent
.wheelDeltaX
) {
7012 delta
= -event
.originalEvent
.wheelDeltaX
;
7013 } else if ( event
.originalEvent
.wheelDeltaY
) {
7014 delta
= event
.originalEvent
.wheelDeltaY
;
7015 } else if ( event
.originalEvent
.wheelDelta
) {
7016 delta
= event
.originalEvent
.wheelDelta
;
7017 } else if ( event
.originalEvent
.detail
) {
7018 delta
= -event
.originalEvent
.detail
;
7023 delta
= delta
< 0 ? -1 : 1;
7024 this.adjustValue( delta
* this.step
);
7032 * Handle key down events.
7035 * @param {jQuery.Event} e Key down event
7037 OO
.ui
.NumberInputWidget
.prototype.onKeyDown = function ( e
) {
7038 if ( !this.isDisabled() ) {
7039 switch ( e
.which
) {
7041 this.adjustValue( this.step
);
7043 case OO
.ui
.Keys
.DOWN
:
7044 this.adjustValue( -this.step
);
7046 case OO
.ui
.Keys
.PAGEUP
:
7047 this.adjustValue( this.pageStep
);
7049 case OO
.ui
.Keys
.PAGEDOWN
:
7050 this.adjustValue( -this.pageStep
);
7059 OO
.ui
.NumberInputWidget
.prototype.setDisabled = function ( disabled
) {
7061 OO
.ui
.NumberInputWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
7063 if ( this.minusButton
) {
7064 this.minusButton
.setDisabled( this.isDisabled() );
7066 if ( this.plusButton
) {
7067 this.plusButton
.setDisabled( this.isDisabled() );
7075 //# sourceMappingURL=oojs-ui-widgets.js.map