3 * https://www.mediawiki.org/wiki/OOUI
5 * Copyright 2011–2019 OOUI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2019-05-29T00:38:42Z
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
28 * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
29 * but the draggable state should be called from the DraggableGroupElement, which updates
32 OO
.ui
.mixin
.DraggableElement
= function OoUiMixinDraggableElement( config
) {
33 config
= config
|| {};
37 this.$handle
= config
.$handle
|| this.$element
;
38 this.wasHandleUsed
= null;
40 // Initialize and events
42 .addClass( 'oo-ui-draggableElement' )
44 mousedown
: this.onDragMouseDown
.bind( this ),
45 dragstart
: this.onDragStart
.bind( this ),
46 dragover
: this.onDragOver
.bind( this ),
47 dragend
: this.onDragEnd
.bind( this ),
48 drop
: this.onDrop
.bind( this )
50 this.$handle
.addClass( 'oo-ui-draggableElement-handle' );
51 this.toggleDraggable( config
.draggable
=== undefined ? true : !!config
.draggable
);
54 OO
.initClass( OO
.ui
.mixin
.DraggableElement
);
61 * A dragstart event is emitted when the user clicks and begins dragging an item.
62 * @param {OO.ui.mixin.DraggableElement} item The item the user has clicked and is dragging with
68 * A dragend event is emitted when the user drags an item and releases the mouse,
69 * thus terminating the drag operation.
74 * A drop event is emitted when the user drags an item and then releases the mouse button
75 * over a valid target.
78 /* Static Properties */
81 * @inheritdoc OO.ui.mixin.ButtonElement
83 OO
.ui
.mixin
.DraggableElement
.static.cancelButtonMouseDownEvents
= false;
88 * Change the draggable state of this widget.
89 * This allows users to temporarily halt the dragging operations.
91 * @param {boolean} isDraggable Widget supports draggable operations
94 OO
.ui
.mixin
.DraggableElement
.prototype.toggleDraggable = function ( isDraggable
) {
95 isDraggable
= isDraggable
!== undefined ? !!isDraggable
: !this.draggable
;
97 if ( this.draggable
!== isDraggable
) {
98 this.draggable
= isDraggable
;
100 this.$handle
.toggleClass( 'oo-ui-draggableElement-undraggable', !this.draggable
);
102 // We make the entire element draggable, not just the handle, so that
103 // the whole element appears to move. wasHandleUsed prevents drags from
104 // starting outside the handle
105 this.$element
.prop( 'draggable', this.draggable
);
110 * Check the draggable state of this widget.
112 * @return {boolean} Widget supports draggable operations
114 OO
.ui
.mixin
.DraggableElement
.prototype.isDraggable = function () {
115 return this.draggable
;
119 * Respond to mousedown event.
122 * @param {jQuery.Event} e Drag event
124 OO
.ui
.mixin
.DraggableElement
.prototype.onDragMouseDown = function ( e
) {
125 if ( !this.isDraggable() ) {
130 // Optimization: if the handle is the whole element this is always true
131 this.$handle
[ 0 ] === this.$element
[ 0 ] ||
132 // Check the mousedown occurred inside the handle
133 OO
.ui
.contains( this.$handle
[ 0 ], e
.target
, true );
137 * Respond to dragstart event.
140 * @param {jQuery.Event} e Drag event
141 * @return {boolean} False if the event is cancelled
144 OO
.ui
.mixin
.DraggableElement
.prototype.onDragStart = function ( e
) {
146 dataTransfer
= e
.originalEvent
.dataTransfer
;
148 if ( !this.wasHandleUsed
|| !this.isDraggable() ) {
152 // Define drop effect
153 dataTransfer
.dropEffect
= 'none';
154 dataTransfer
.effectAllowed
= 'move';
156 // We must set up a dataTransfer data property or Firefox seems to
157 // ignore the fact the element is draggable.
159 dataTransfer
.setData( 'application-x/OOUI-draggable', this.getIndex() );
161 // The above is only for Firefox. Move on if it fails.
163 // Briefly add a 'clone' class to style the browser's native drag image
164 this.$element
.addClass( 'oo-ui-draggableElement-clone' );
165 // Add placeholder class after the browser has rendered the clone
166 setTimeout( function () {
168 .removeClass( 'oo-ui-draggableElement-clone' )
169 .addClass( 'oo-ui-draggableElement-placeholder' );
172 this.emit( 'dragstart', this );
177 * Respond to dragend event.
182 OO
.ui
.mixin
.DraggableElement
.prototype.onDragEnd = function () {
183 this.$element
.removeClass( 'oo-ui-draggableElement-placeholder' );
184 this.emit( 'dragend' );
191 * @param {jQuery.Event} e Drop event
194 OO
.ui
.mixin
.DraggableElement
.prototype.onDrop = function ( e
) {
196 this.emit( 'drop', e
);
200 * In order for drag/drop to work, the dragover event must
201 * return false and stop propogation.
203 * @param {jQuery.Event} e Drag event
206 OO
.ui
.mixin
.DraggableElement
.prototype.onDragOver = function ( e
) {
212 * Store it in the DOM so we can access from the widget drag event.
215 * @param {number} index Item index
217 OO
.ui
.mixin
.DraggableElement
.prototype.setIndex = function ( index
) {
218 if ( this.index
!== index
) {
220 this.$element
.data( 'index', index
);
228 * @return {number} Item index
230 OO
.ui
.mixin
.DraggableElement
.prototype.getIndex = function () {
235 * DraggableGroupElement is a mixin class used to create a group element to
236 * contain draggable elements, which are items that can be clicked and dragged by a mouse.
237 * The class is used with OO.ui.mixin.DraggableElement.
241 * @mixins OO.ui.mixin.GroupElement
244 * @param {Object} [config] Configuration options
245 * @cfg {string} [orientation] Item orientation: 'horizontal' or 'vertical'. The orientation
246 * should match the layout of the items. Items displayed in a single row
247 * or in several rows should use horizontal orientation. The vertical orientation should only be
248 * used when the items are displayed in a single column. Defaults to 'vertical'
249 * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
251 OO
.ui
.mixin
.DraggableGroupElement
= function OoUiMixinDraggableGroupElement( config
) {
252 // Configuration initialization
253 config
= config
|| {};
255 // Parent constructor
256 OO
.ui
.mixin
.GroupElement
.call( this, config
);
259 this.orientation
= config
.orientation
|| 'vertical';
260 this.dragItem
= null;
263 this.itemsOrder
= null;
264 this.draggable
= config
.draggable
=== undefined ? true : !!config
.draggable
;
268 dragstart
: 'itemDragStart',
269 dragend
: 'itemDragEnd',
272 this.connect( this, {
273 itemDragStart
: 'onItemDragStart',
274 itemDrop
: 'onItemDropOrDragEnd',
275 itemDragEnd
: 'onItemDropOrDragEnd'
279 if ( Array
.isArray( config
.items
) ) {
280 this.addItems( config
.items
);
283 .addClass( 'oo-ui-draggableGroupElement' )
284 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' );
288 OO
.mixinClass( OO
.ui
.mixin
.DraggableGroupElement
, OO
.ui
.mixin
.GroupElement
);
293 * An item has been dragged to a new position, but not yet dropped.
296 * @param {OO.ui.mixin.DraggableElement} item Dragged item
297 * @param {number} [newIndex] New index for the item
301 * An item has been dropped at a new position.
304 * @param {OO.ui.mixin.DraggableElement} item Reordered item
305 * @param {number} [newIndex] New index for the item
309 * Draggable state of this widget has changed.
312 * @param {boolean} [draggable] Widget is draggable
318 * Change the draggable state of this widget.
319 * This allows users to temporarily halt the dragging operations.
321 * @param {boolean} isDraggable Widget supports draggable operations
324 OO
.ui
.mixin
.DraggableGroupElement
.prototype.toggleDraggable = function ( isDraggable
) {
325 isDraggable
= isDraggable
!== undefined ? !!isDraggable
: !this.draggable
;
327 if ( this.draggable
!== isDraggable
) {
328 this.draggable
= isDraggable
;
330 // Tell the items their draggable state changed
331 this.getItems().forEach( function ( item
) {
332 item
.toggleDraggable( this.draggable
);
336 this.emit( 'draggable', this.draggable
);
341 * Check the draggable state of this widget
343 * @return {boolean} Widget supports draggable operations
345 OO
.ui
.mixin
.DraggableGroupElement
.prototype.isDraggable = function () {
346 return this.draggable
;
350 * Respond to item drag start event
353 * @param {OO.ui.mixin.DraggableElement} item Dragged item
355 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
356 if ( !this.isDraggable() ) {
359 // Make a shallow copy of this.items so we can re-order it during previews
360 // without affecting the original array.
361 this.itemsOrder
= this.items
.slice();
362 this.updateIndexes();
363 if ( this.orientation
=== 'horizontal' ) {
364 // Calculate and cache directionality on drag start - it's a little
365 // expensive and it shouldn't change while dragging.
366 this.dir
= this.$element
.css( 'direction' );
368 this.setDragItem( item
);
372 * Update the index properties of the items
374 OO
.ui
.mixin
.DraggableGroupElement
.prototype.updateIndexes = function () {
377 // Map the index of each object
378 for ( i
= 0, len
= this.itemsOrder
.length
; i
< len
; i
++ ) {
379 this.itemsOrder
[ i
].setIndex( i
);
384 * Handle drop or dragend event and switch the order of the items accordingly
387 * @param {OO.ui.mixin.DraggableElement} item Dropped item
388 * @return {OO.ui.Element} The element, for chaining
390 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDropOrDragEnd = function () {
391 var targetIndex
, originalIndex
,
392 item
= this.getDragItem();
394 // TODO: Figure out a way to configure a list of legally droppable
395 // elements even if they are not yet in the list
397 originalIndex
= this.items
.indexOf( item
);
398 // If the item has moved forward, add one to the index to account for the left shift
399 targetIndex
= item
.getIndex() + ( item
.getIndex() > originalIndex
? 1 : 0 );
400 if ( targetIndex
!== originalIndex
) {
401 this.reorder( this.getDragItem(), targetIndex
);
402 this.emit( 'reorder', this.getDragItem(), targetIndex
);
404 this.updateIndexes();
406 this.unsetDragItem();
407 // Return false to prevent propogation
412 * Respond to dragover event
415 * @param {jQuery.Event} e Dragover event
418 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
419 var overIndex
, targetIndex
,
420 item
= this.getDragItem(),
421 dragItemIndex
= item
.getIndex();
423 // Get the OptionWidget item we are dragging over
424 overIndex
= $( e
.target
).closest( '.oo-ui-draggableElement' ).data( 'index' );
426 if ( overIndex
!== undefined && overIndex
!== dragItemIndex
) {
427 targetIndex
= overIndex
+ ( overIndex
> dragItemIndex
? 1 : 0 );
429 if ( targetIndex
> 0 ) {
430 this.$group
.children().eq( targetIndex
- 1 ).after( item
.$element
);
432 this.$group
.prepend( item
.$element
);
434 // Move item in itemsOrder array
435 this.itemsOrder
.splice( overIndex
, 0,
436 this.itemsOrder
.splice( dragItemIndex
, 1 )[ 0 ]
438 this.updateIndexes();
439 this.emit( 'drag', item
, targetIndex
);
446 * Reorder the items in the group
448 * @param {OO.ui.mixin.DraggableElement} item Reordered item
449 * @param {number} newIndex New index
451 OO
.ui
.mixin
.DraggableGroupElement
.prototype.reorder = function ( item
, newIndex
) {
452 this.addItems( [ item
], newIndex
);
458 * @param {OO.ui.mixin.DraggableElement} item Dragged item
460 OO
.ui
.mixin
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
461 if ( this.dragItem
!== item
) {
462 this.dragItem
= item
;
463 this.$element
.on( 'dragover', this.onDragOver
.bind( this ) );
464 this.$element
.addClass( 'oo-ui-draggableGroupElement-dragging' );
469 * Unset the current dragged item
471 OO
.ui
.mixin
.DraggableGroupElement
.prototype.unsetDragItem = function () {
472 if ( this.dragItem
) {
473 this.dragItem
= null;
474 this.$element
.off( 'dragover' );
475 this.$element
.removeClass( 'oo-ui-draggableGroupElement-dragging' );
480 * Get the item that is currently being dragged.
482 * @return {OO.ui.mixin.DraggableElement|null} The currently dragged item, or `null` if no item is
485 OO
.ui
.mixin
.DraggableGroupElement
.prototype.getDragItem = function () {
486 return this.dragItem
;
490 * RequestManager is a mixin that manages the lifecycle of a promise-backed request for a widget,
491 * such as the {@link OO.ui.mixin.LookupElement}.
497 * @param {Object} [config] Configuration options
498 * @cfg {boolean} [showPendingRequest=true] Show pending state while request data is being fetched.
499 * Requires widget to have also mixed in {@link OO.ui.mixin.PendingElement}.
501 OO
.ui
.mixin
.RequestManager
= function OoUiMixinRequestManager( config
) {
502 this.requestCache
= {};
503 this.requestQuery
= null;
504 this.requestRequest
= null;
505 this.showPendingRequest
= !!this.pushPending
&& config
.showPendingRequest
!== false;
510 OO
.initClass( OO
.ui
.mixin
.RequestManager
);
513 * Get request results for the current query.
515 * @return {jQuery.Promise} Promise object which will be passed response data as the first argument
516 * of the done event. If the request was aborted to make way for a subsequent request, this
517 * promise may not be rejected, depending on what jQuery feels like doing.
519 OO
.ui
.mixin
.RequestManager
.prototype.getRequestData = function () {
521 value
= this.getRequestQuery(),
522 deferred
= $.Deferred(),
526 if ( Object
.prototype.hasOwnProperty
.call( this.requestCache
, value
) ) {
527 deferred
.resolve( this.requestCache
[ value
] );
529 if ( this.showPendingRequest
) {
532 this.requestQuery
= value
;
533 ourRequest
= this.requestRequest
= this.getRequest();
535 .always( function () {
536 // We need to pop pending even if this is an old request, otherwise
537 // the widget will remain pending forever.
538 // TODO: this assumes that an aborted request will fail or succeed soon after
539 // being aborted, or at least eventually. It would be nice if we could popPending()
540 // at abort time, but only if we knew that we hadn't already called popPending()
542 if ( widget
.showPendingRequest
) {
546 .done( function ( response
) {
547 // If this is an old request (and aborting it somehow caused it to still succeed),
548 // ignore its success completely
549 if ( ourRequest
=== widget
.requestRequest
) {
550 widget
.requestQuery
= null;
551 widget
.requestRequest
= null;
552 widget
.requestCache
[ value
] =
553 widget
.getRequestCacheDataFromResponse( response
);
554 deferred
.resolve( widget
.requestCache
[ value
] );
558 // If this is an old request (or a request failing because it's being aborted),
559 // ignore its failure completely
560 if ( ourRequest
=== widget
.requestRequest
) {
561 widget
.requestQuery
= null;
562 widget
.requestRequest
= null;
567 return deferred
.promise();
571 * Abort the currently pending request, if any.
575 OO
.ui
.mixin
.RequestManager
.prototype.abortRequest = function () {
576 var oldRequest
= this.requestRequest
;
578 // First unset this.requestRequest to the fail handler will notice
579 // that the request is no longer current
580 this.requestRequest
= null;
581 this.requestQuery
= null;
587 * Get the query to be made.
592 * @return {string} query to be used
594 OO
.ui
.mixin
.RequestManager
.prototype.getRequestQuery
= null;
597 * Get a new request object of the current query value.
602 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
604 OO
.ui
.mixin
.RequestManager
.prototype.getRequest
= null;
607 * Pre-process data returned by the request from #getRequest.
609 * The return value of this function will be cached, and any further queries for the given value
610 * will use the cache rather than doing API requests.
615 * @param {Mixed} response Response from server
616 * @return {Mixed} Cached result data
618 OO
.ui
.mixin
.RequestManager
.prototype.getRequestCacheDataFromResponse
= null;
621 * LookupElement is a mixin that creates a {@link OO.ui.MenuSelectWidget menu} of suggested
622 * values for a {@link OO.ui.TextInputWidget text input widget}. Suggested values are based on
623 * the characters the user types into the text input field and, in general, the menu is only
624 * displayed when the user types. If a suggested value is chosen from the lookup menu, that value
625 * becomes the value of the input field.
627 * Note that a new menu of suggested items is displayed when a value is chosen from the
628 * lookup menu. If this is not the desired behavior, disable lookup menus with the
629 * #setLookupsDisabled method, then set the value, then re-enable lookups.
631 * See the [OOUI demos][1] for an example.
633 * [1]: https://doc.wikimedia.org/oojs-ui/master/demos/#LookupElement-try-inputting-an-integer
637 * @mixins OO.ui.mixin.RequestManager
640 * @param {Object} [config] Configuration options
641 * @cfg {jQuery} [$overlay] Overlay for the lookup menu; defaults to relative positioning.
642 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
643 * @cfg {jQuery} [$container=this.$element] The container element. The lookup menu is rendered
644 * beneath the specified element.
645 * @cfg {Object} [menu] Configuration options to pass to
646 * {@link OO.ui.MenuSelectWidget menu select widget}
647 * @cfg {boolean} [allowSuggestionsWhenEmpty=false] Request and display a lookup menu when the
648 * text input is empty.
649 * By default, the lookup menu is not generated and displayed until the user begins to type.
650 * @cfg {boolean} [highlightFirst=true] Whether the first lookup result should be highlighted
651 * (so, that the user can take it over into the input with simply pressing return) automatically
653 * @cfg {boolean} [showSuggestionsOnFocus=true] Show suggestions when focusing the input. If this
654 * is set to false, suggestions will still be shown on a mousedown triggered focus. This matches
655 * browser autocomplete behavior.
657 OO
.ui
.mixin
.LookupElement
= function OoUiMixinLookupElement( config
) {
658 // Configuration initialization
659 config
= $.extend( { highlightFirst
: true }, config
);
661 // Mixin constructors
662 OO
.ui
.mixin
.RequestManager
.call( this, config
);
665 this.$overlay
= ( config
.$overlay
=== true ?
666 OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
667 this.lookupMenu
= new OO
.ui
.MenuSelectWidget( $.extend( {
670 $floatableContainer
: config
.$container
|| this.$element
673 this.allowSuggestionsWhenEmpty
= config
.allowSuggestionsWhenEmpty
|| false;
675 this.lookupsDisabled
= false;
676 this.lookupInputFocused
= false;
677 this.lookupHighlightFirstItem
= config
.highlightFirst
;
678 this.showSuggestionsOnFocus
= config
.showSuggestionsOnFocus
!== false;
682 focus
: this.onLookupInputFocus
.bind( this ),
683 blur
: this.onLookupInputBlur
.bind( this ),
684 mousedown
: this.onLookupInputMouseDown
.bind( this )
686 this.connect( this, {
687 change
: 'onLookupInputChange'
689 this.lookupMenu
.connect( this, {
690 toggle
: 'onLookupMenuToggle',
691 choose
: 'onLookupMenuItemChoose'
697 'aria-owns': this.lookupMenu
.getElementId(),
698 'aria-autocomplete': 'list'
700 this.$element
.addClass( 'oo-ui-lookupElement' );
701 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
702 this.$overlay
.append( this.lookupMenu
.$element
);
707 OO
.mixinClass( OO
.ui
.mixin
.LookupElement
, OO
.ui
.mixin
.RequestManager
);
712 * Handle input focus event.
715 * @param {jQuery.Event} e Input focus event
717 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputFocus = function () {
718 this.lookupInputFocused
= true;
719 if ( this.showSuggestionsOnFocus
) {
720 this.populateLookupMenu();
725 * Handle input blur event.
728 * @param {jQuery.Event} e Input blur event
730 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputBlur = function () {
731 this.closeLookupMenu();
732 this.lookupInputFocused
= false;
736 * Handle input mouse down event.
739 * @param {jQuery.Event} e Input mouse down event
741 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputMouseDown = function () {
743 !this.lookupMenu
.isVisible() &&
745 // Open the menu if the input was already focused.
746 // This way we allow the user to open the menu again after closing it with Escape (esc)
747 // by clicking in the input.
748 this.lookupInputFocused
||
749 // If showSuggestionsOnFocus is disabled, still open the menu on mousedown.
750 !this.showSuggestionsOnFocus
753 this.populateLookupMenu();
758 * Handle input change event.
761 * @param {string} value New input value
763 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputChange = function () {
764 if ( this.lookupInputFocused
) {
765 this.populateLookupMenu();
770 * Handle the lookup menu being shown/hidden.
773 * @param {boolean} visible Whether the lookup menu is now visible.
775 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
777 // When the menu is hidden, abort any active request and clear the menu.
778 // This has to be done here in addition to closeLookupMenu(), because
779 // MenuSelectWidget will close itself when the user presses Escape (esc).
780 this.abortLookupRequest();
781 this.lookupMenu
.clearItems();
786 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
789 * @param {OO.ui.MenuOptionWidget} item Selected item
791 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
792 this.setValue( item
.getData() );
799 * @return {OO.ui.MenuSelectWidget}
801 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenu = function () {
802 return this.lookupMenu
;
806 * Disable or re-enable lookups.
808 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
810 * @param {boolean} disabled Disable lookups
812 OO
.ui
.mixin
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
813 this.lookupsDisabled
= !!disabled
;
817 * Open the menu. If there are no entries in the menu, this does nothing.
821 * @return {OO.ui.Element} The element, for chaining
823 OO
.ui
.mixin
.LookupElement
.prototype.openLookupMenu = function () {
824 if ( !this.lookupMenu
.isEmpty() ) {
825 this.lookupMenu
.toggle( true );
831 * Close the menu, empty it, and abort any pending request.
835 * @return {OO.ui.Element} The element, for chaining
837 OO
.ui
.mixin
.LookupElement
.prototype.closeLookupMenu = function () {
838 this.lookupMenu
.toggle( false );
839 this.abortLookupRequest();
840 this.lookupMenu
.clearItems();
845 * Request menu items based on the input's current value, and when they arrive,
846 * populate the menu with these items and show the menu.
848 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
852 * @return {OO.ui.Element} The element, for chaining
854 OO
.ui
.mixin
.LookupElement
.prototype.populateLookupMenu = function () {
856 value
= this.getValue();
858 if ( this.lookupsDisabled
|| this.isReadOnly() ) {
862 // If the input is empty, clear the menu, unless suggestions when empty are allowed.
863 if ( !this.allowSuggestionsWhenEmpty
&& value
=== '' ) {
864 this.closeLookupMenu();
865 // Skip population if there is already a request pending for the current value
866 } else if ( value
!== this.lookupQuery
) {
867 this.getLookupMenuItems()
868 .done( function ( items
) {
869 widget
.lookupMenu
.clearItems();
870 if ( items
.length
) {
874 widget
.initializeLookupMenuSelection();
876 widget
.lookupMenu
.toggle( false );
880 widget
.lookupMenu
.clearItems();
881 widget
.lookupMenu
.toggle( false );
889 * Highlight the first selectable item in the menu, if configured.
894 OO
.ui
.mixin
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
895 if ( this.lookupHighlightFirstItem
&& !this.lookupMenu
.findSelectedItem() ) {
896 this.lookupMenu
.highlightItem( this.lookupMenu
.findFirstSelectableItem() );
901 * Get lookup menu items for the current query.
904 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
905 * the done event. If the request was aborted to make way for a subsequent request, this promise
906 * will not be rejected: it will remain pending forever.
908 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuItems = function () {
909 return this.getRequestData().then( function ( data
) {
910 return this.getLookupMenuOptionsFromData( data
);
915 * Abort the currently pending lookup request, if any.
919 OO
.ui
.mixin
.LookupElement
.prototype.abortLookupRequest = function () {
924 * Get a new request object of the current lookup query value.
929 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
931 OO
.ui
.mixin
.LookupElement
.prototype.getLookupRequest
= null;
934 * Pre-process data returned by the request from #getLookupRequest.
936 * The return value of this function will be cached, and any further queries for the given value
937 * will use the cache rather than doing API requests.
942 * @param {Mixed} response Response from server
943 * @return {Mixed} Cached result data
945 OO
.ui
.mixin
.LookupElement
.prototype.getLookupCacheDataFromResponse
= null;
948 * Get a list of menu option widgets from the (possibly cached) data returned by
949 * #getLookupCacheDataFromResponse.
954 * @param {Mixed} data Cached result data, usually an array
955 * @return {OO.ui.MenuOptionWidget[]} Menu items
957 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuOptionsFromData
= null;
960 * Set the read-only state of the widget.
962 * This will also disable/enable the lookups functionality.
964 * @param {boolean} readOnly Make input read-only
966 * @return {OO.ui.Element} The element, for chaining
968 OO
.ui
.mixin
.LookupElement
.prototype.setReadOnly = function ( readOnly
) {
970 // Note: Calling #setReadOnly this way assumes this is mixed into an OO.ui.TextInputWidget
971 OO
.ui
.TextInputWidget
.prototype.setReadOnly
.call( this, readOnly
);
973 // During construction, #setReadOnly is called before the OO.ui.mixin.LookupElement constructor.
974 if ( this.isReadOnly() && this.lookupMenu
) {
975 this.closeLookupMenu();
982 * @inheritdoc OO.ui.mixin.RequestManager
984 OO
.ui
.mixin
.LookupElement
.prototype.getRequestQuery = function () {
985 return this.getValue();
989 * @inheritdoc OO.ui.mixin.RequestManager
991 OO
.ui
.mixin
.LookupElement
.prototype.getRequest = function () {
992 return this.getLookupRequest();
996 * @inheritdoc OO.ui.mixin.RequestManager
998 OO
.ui
.mixin
.LookupElement
.prototype.getRequestCacheDataFromResponse = function ( response
) {
999 return this.getLookupCacheDataFromResponse( response
);
1003 * TabPanelLayouts are used within {@link OO.ui.IndexLayout index layouts} to create tab panels that
1004 * users can select and display from the index's optional {@link OO.ui.TabSelectWidget tab}
1005 * navigation. TabPanels are usually not instantiated directly, rather extended to include the
1006 * required content and functionality.
1008 * Each tab panel must have a unique symbolic name, which is passed to the constructor. In addition,
1009 * the tab panel's tab item is customized (with a label) using the #setupTabItem method. See
1010 * {@link OO.ui.IndexLayout IndexLayout} for an example.
1013 * @extends OO.ui.PanelLayout
1016 * @param {string} name Unique symbolic name of tab panel
1017 * @param {Object} [config] Configuration options
1018 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] Label for tab panel's tab
1020 OO
.ui
.TabPanelLayout
= function OoUiTabPanelLayout( name
, config
) {
1021 // Allow passing positional parameters inside the config object
1022 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1027 // Configuration initialization
1028 config
= $.extend( { scrollable
: true }, config
);
1030 // Parent constructor
1031 OO
.ui
.TabPanelLayout
.parent
.call( this, config
);
1035 this.label
= config
.label
;
1036 this.tabItem
= null;
1037 this.active
= false;
1041 .addClass( 'oo-ui-tabPanelLayout' )
1042 .attr( 'role', 'tabpanel' );
1047 OO
.inheritClass( OO
.ui
.TabPanelLayout
, OO
.ui
.PanelLayout
);
1052 * An 'active' event is emitted when the tab panel becomes active. Tab panels become active when
1053 * they are shown in a index layout that is configured to display only one tab panel at a time.
1056 * @param {boolean} active Tab panel is active
1062 * Get the symbolic name of the tab panel.
1064 * @return {string} Symbolic name of tab panel
1066 OO
.ui
.TabPanelLayout
.prototype.getName = function () {
1071 * Check if tab panel is active.
1073 * Tab panels become active when they are shown in a {@link OO.ui.IndexLayout index layout} that is
1074 * configured to display only one tab panel at a time. Additional CSS is applied to the tab panel's
1075 * tab item to reflect the active state.
1077 * @return {boolean} Tab panel is active
1079 OO
.ui
.TabPanelLayout
.prototype.isActive = function () {
1086 * The tab item allows users to access the tab panel from the index's tab
1087 * navigation. The tab item itself can be customized (with a label, level, etc.) using the
1088 * #setupTabItem method.
1090 * @return {OO.ui.TabOptionWidget|null} Tab option widget
1092 OO
.ui
.TabPanelLayout
.prototype.getTabItem = function () {
1093 return this.tabItem
;
1097 * Set or unset the tab item.
1099 * Specify a {@link OO.ui.TabOptionWidget tab option} to set it,
1100 * or `null` to clear the tab item. To customize the tab item itself (e.g., to set a label or tab
1101 * level), use #setupTabItem instead of this method.
1103 * @param {OO.ui.TabOptionWidget|null} tabItem Tab option widget, null to clear
1105 * @return {OO.ui.TabPanelLayout} The layout, for chaining
1107 OO
.ui
.TabPanelLayout
.prototype.setTabItem = function ( tabItem
) {
1108 this.tabItem
= tabItem
|| null;
1110 this.setupTabItem();
1116 * Set up the tab item.
1118 * Use this method to customize the tab item (e.g., to add a label or tab level). To set or unset
1119 * the tab item itself (with a {@link OO.ui.TabOptionWidget tab option} or `null`), use
1120 * the #setTabItem method instead.
1122 * @param {OO.ui.TabOptionWidget} tabItem Tab option widget to set up
1124 * @return {OO.ui.TabPanelLayout} The layout, for chaining
1126 OO
.ui
.TabPanelLayout
.prototype.setupTabItem = function () {
1127 this.$element
.attr( 'aria-labelledby', this.tabItem
.getElementId() );
1129 this.tabItem
.$element
.attr( 'aria-controls', this.getElementId() );
1132 this.tabItem
.setLabel( this.label
);
1138 * Set the tab panel to its 'active' state.
1140 * Tab panels become active when they are shown in a index layout that is configured to display only
1141 * one tab panel at a time. Additional CSS is applied to the tab item to reflect the tab panel's
1142 * active state. Outside of the index context, setting the active state on a tab panel does nothing.
1144 * @param {boolean} active Tab panel is active
1147 OO
.ui
.TabPanelLayout
.prototype.setActive = function ( active
) {
1150 if ( active
!== this.active
) {
1151 this.active
= active
;
1152 this.$element
.toggleClass( 'oo-ui-tabPanelLayout-active', this.active
);
1153 this.emit( 'active', this.active
);
1158 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that
1159 * users can select and display from the booklet's optional
1160 * {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated
1161 * directly, rather extended to include the required content and functionality.
1163 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the
1164 * page's outline item is customized (with a label, outline level, etc.) using the
1165 * #setupOutlineItem method. See {@link OO.ui.BookletLayout BookletLayout} for an example.
1168 * @extends OO.ui.PanelLayout
1171 * @param {string} name Unique symbolic name of page
1172 * @param {Object} [config] Configuration options
1174 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
1175 // Allow passing positional parameters inside the config object
1176 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1181 // Configuration initialization
1182 config
= $.extend( { scrollable
: true }, config
);
1184 // Parent constructor
1185 OO
.ui
.PageLayout
.parent
.call( this, config
);
1189 this.outlineItem
= null;
1190 this.active
= false;
1193 this.$element
.addClass( 'oo-ui-pageLayout' );
1198 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
1203 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1204 * shown in a booklet layout that is configured to display only one page at a time.
1207 * @param {boolean} active Page is active
1213 * Get the symbolic name of the page.
1215 * @return {string} Symbolic name of page
1217 OO
.ui
.PageLayout
.prototype.getName = function () {
1222 * Check if page is active.
1224 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is
1225 * configured to display only one page at a time. Additional CSS is applied to the page's outline
1226 * item to reflect the active state.
1228 * @return {boolean} Page is active
1230 OO
.ui
.PageLayout
.prototype.isActive = function () {
1237 * The outline item allows users to access the page from the booklet's outline
1238 * navigation. The outline item itself can be customized (with a label, level, etc.) using the
1239 * #setupOutlineItem method.
1241 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1243 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
1244 return this.outlineItem
;
1248 * Set or unset the outline item.
1250 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1251 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label
1252 * or outline level), use #setupOutlineItem instead of this method.
1254 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1256 * @return {OO.ui.PageLayout} The layout, for chaining
1258 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
1259 this.outlineItem
= outlineItem
|| null;
1260 if ( outlineItem
) {
1261 this.setupOutlineItem();
1267 * Set up the outline item.
1269 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or
1270 * unset the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or
1271 * `null`), use the #setOutlineItem method instead.
1273 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1275 * @return {OO.ui.PageLayout} The layout, for chaining
1277 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
1282 * Set the page to its 'active' state.
1284 * Pages become active when they are shown in a booklet layout that is configured to display only
1285 * one page at a time. Additional CSS is applied to the outline item to reflect the page's active
1286 * state. Outside of the booklet context, setting the active state on a page does nothing.
1288 * @param {boolean} active Page is active
1291 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
1294 if ( active
!== this.active
) {
1295 this.active
= active
;
1296 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
1297 this.emit( 'active', this.active
);
1302 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one
1303 * panel is displayed at a time, though the stack layout can also be configured to show all
1304 * contained panels, one after another, by setting the #continuous option to 'true'.
1307 * // A stack layout with two panels, configured to be displayed continuously
1308 * var myStack = new OO.ui.StackLayout( {
1310 * new OO.ui.PanelLayout( {
1311 * $content: $( '<p>Panel One</p>' ),
1315 * new OO.ui.PanelLayout( {
1316 * $content: $( '<p>Panel Two</p>' ),
1323 * $( document.body ).append( myStack.$element );
1326 * @extends OO.ui.PanelLayout
1327 * @mixins OO.ui.mixin.GroupElement
1330 * @param {Object} [config] Configuration options
1331 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel
1332 * is displayed at a time.
1333 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1335 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
1336 // Configuration initialization
1337 // Make the layout scrollable in continuous mode, otherwise each
1338 // panel is responsible for its own scrolling.
1339 config
= $.extend( { scrollable
: !!( config
&& config
.continuous
) }, config
);
1341 // Parent constructor
1342 OO
.ui
.StackLayout
.parent
.call( this, config
);
1344 // Mixin constructors
1345 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( { $group
: this.$element
}, config
) );
1348 this.currentItem
= null;
1349 this.continuous
= !!config
.continuous
;
1352 this.$element
.addClass( 'oo-ui-stackLayout' );
1353 if ( this.continuous
) {
1354 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
1355 this.$element
.on( 'scroll', OO
.ui
.debounce( this.onScroll
.bind( this ), 250 ) );
1357 if ( Array
.isArray( config
.items
) ) {
1358 this.addItems( config
.items
);
1364 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
1365 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.mixin
.GroupElement
);
1370 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1371 * {@link #clearItems cleared} or {@link #setItem displayed}.
1374 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1378 * When used in continuous mode, this event is emitted when the user scrolls down
1379 * far enough such that currentItem is no longer visible.
1381 * @event visibleItemChange
1382 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1388 * Handle scroll events from the layout element
1390 * @param {jQuery.Event} e
1391 * @fires visibleItemChange
1393 OO
.ui
.StackLayout
.prototype.onScroll = function () {
1395 len
= this.items
.length
,
1396 currentIndex
= this.items
.indexOf( this.currentItem
),
1397 newIndex
= currentIndex
,
1398 containerRect
= this.$element
[ 0 ].getBoundingClientRect();
1400 if ( !containerRect
|| ( !containerRect
.top
&& !containerRect
.bottom
) ) {
1401 // Can't get bounding rect, possibly not attached.
1405 function getRect( item
) {
1406 return item
.$element
[ 0 ].getBoundingClientRect();
1409 function isVisible( item
) {
1410 var rect
= getRect( item
);
1411 return rect
.bottom
> containerRect
.top
&& rect
.top
< containerRect
.bottom
;
1414 currentRect
= getRect( this.currentItem
);
1416 if ( currentRect
.bottom
< containerRect
.top
) {
1417 // Scrolled down past current item
1418 while ( ++newIndex
< len
) {
1419 if ( isVisible( this.items
[ newIndex
] ) ) {
1423 } else if ( currentRect
.top
> containerRect
.bottom
) {
1424 // Scrolled up past current item
1425 while ( --newIndex
>= 0 ) {
1426 if ( isVisible( this.items
[ newIndex
] ) ) {
1432 if ( newIndex
!== currentIndex
) {
1433 this.emit( 'visibleItemChange', this.items
[ newIndex
] );
1438 * Get the current panel.
1440 * @return {OO.ui.Layout|null}
1442 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
1443 return this.currentItem
;
1447 * Unset the current item.
1450 * @param {OO.ui.StackLayout} layout
1453 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
1454 var prevItem
= this.currentItem
;
1455 if ( prevItem
=== null ) {
1459 this.currentItem
= null;
1460 this.emit( 'set', null );
1464 * Add panel layouts to the stack layout.
1466 * Panels will be added to the end of the stack layout array unless the optional index parameter
1467 * specifies a different insertion point. Adding a panel that is already in the stack will move it
1468 * to the end of the array or the point specified by the index.
1470 * @param {OO.ui.Layout[]} items Panels to add
1471 * @param {number} [index] Index of the insertion point
1473 * @return {OO.ui.StackLayout} The layout, for chaining
1475 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
1476 // Update the visibility
1477 this.updateHiddenState( items
, this.currentItem
);
1480 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
, index
);
1482 if ( !this.currentItem
&& items
.length
) {
1483 this.setItem( items
[ 0 ] );
1490 * Remove the specified panels from the stack layout.
1492 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all
1493 * panels, you may wish to use the #clearItems method instead.
1495 * @param {OO.ui.Layout[]} items Panels to remove
1497 * @return {OO.ui.StackLayout} The layout, for chaining
1500 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
1502 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
1504 if ( items
.indexOf( this.currentItem
) !== -1 ) {
1505 if ( this.items
.length
) {
1506 this.setItem( this.items
[ 0 ] );
1508 this.unsetCurrentItem();
1516 * Clear all panels from the stack layout.
1518 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1519 * a subset of panels, use the #removeItems method.
1522 * @return {OO.ui.StackLayout} The layout, for chaining
1525 OO
.ui
.StackLayout
.prototype.clearItems = function () {
1526 this.unsetCurrentItem();
1527 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
1533 * Show the specified panel.
1535 * If another panel is currently displayed, it will be hidden.
1537 * @param {OO.ui.Layout} item Panel to show
1539 * @return {OO.ui.StackLayout} The layout, for chaining
1542 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
1543 if ( item
!== this.currentItem
) {
1544 this.updateHiddenState( this.items
, item
);
1546 if ( this.items
.indexOf( item
) !== -1 ) {
1547 this.currentItem
= item
;
1548 this.emit( 'set', item
);
1550 this.unsetCurrentItem();
1558 * Reset the scroll offset of all panels, or the container if continuous
1562 OO
.ui
.StackLayout
.prototype.resetScroll = function () {
1563 if ( this.continuous
) {
1565 return OO
.ui
.StackLayout
.parent
.prototype.resetScroll
.call( this );
1568 this.getItems().forEach( function ( panel
) {
1569 var hidden
= panel
.$element
.hasClass( 'oo-ui-element-hidden' );
1570 // Scroll can only be reset when panel is visible
1571 panel
.$element
.removeClass( 'oo-ui-element-hidden' );
1572 panel
.resetScroll();
1574 panel
.$element
.addClass( 'oo-ui-element-hidden' );
1582 * Update the visibility of all items in case of non-continuous view.
1584 * Ensure all items are hidden except for the selected one.
1585 * This method does nothing when the stack is continuous.
1588 * @param {OO.ui.Layout[]} items Item list iterate over
1589 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1591 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
1594 if ( !this.continuous
) {
1595 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
1596 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
1597 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
1598 items
[ i
].$element
.attr( 'aria-hidden', 'true' );
1601 if ( selectedItem
) {
1602 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
1603 selectedItem
.$element
.removeAttr( 'aria-hidden' );
1609 * MenuLayouts combine a menu and a content {@link OO.ui.PanelLayout panel}. The menu is positioned
1610 * relative to the content (after, before, top, or bottom) and its size is customized with the
1611 * #menuSize config. The content area will fill all remaining space.
1615 * menuPanel = new OO.ui.PanelLayout( {
1620 * contentPanel = new OO.ui.PanelLayout( {
1625 * select = new OO.ui.SelectWidget( {
1627 * new OO.ui.OptionWidget( {
1631 * new OO.ui.OptionWidget( {
1635 * new OO.ui.OptionWidget( {
1639 * new OO.ui.OptionWidget( {
1644 * } ).on( 'select', function ( item ) {
1645 * menuLayout.setMenuPosition( item.getData() );
1648 * menuLayout = new OO.ui.MenuLayout( {
1650 * menuPanel: menuPanel,
1651 * contentPanel: contentPanel
1653 * menuLayout.$menu.append(
1654 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element )
1656 * menuLayout.$content.append(
1657 * contentPanel.$element.append(
1658 * '<b>Content panel</b>',
1659 * '<p>Note that the menu is positioned relative to the content panel: ' +
1660 * 'top, bottom, after, before.</p>'
1663 * $( document.body ).append( menuLayout.$element );
1665 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1666 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1667 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1670 * .oo-ui-menuLayout-menu {
1675 * .oo-ui-menuLayout-content {
1683 * @extends OO.ui.Layout
1686 * @param {Object} [config] Configuration options
1687 * @cfg {OO.ui.PanelLayout} [menuPanel] Menu panel
1688 * @cfg {OO.ui.PanelLayout} [contentPanel] Content panel
1689 * @cfg {boolean} [expanded=true] Expand the layout to fill the entire parent element.
1690 * @cfg {boolean} [showMenu=true] Show menu
1691 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1693 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
1694 // Configuration initialization
1695 config
= $.extend( {
1698 menuPosition
: 'before'
1701 // Parent constructor
1702 OO
.ui
.MenuLayout
.parent
.call( this, config
);
1704 this.menuPanel
= null;
1705 this.contentPanel
= null;
1706 this.expanded
= !!config
.expanded
;
1710 * @property {jQuery}
1712 this.$menu
= $( '<div>' );
1716 * @property {jQuery}
1718 this.$content
= $( '<div>' );
1721 this.$menu
.addClass( 'oo-ui-menuLayout-menu' );
1722 this.$content
.addClass( 'oo-ui-menuLayout-content' );
1723 this.$element
.addClass( 'oo-ui-menuLayout' );
1724 if ( config
.expanded
) {
1725 this.$element
.addClass( 'oo-ui-menuLayout-expanded' );
1727 this.$element
.addClass( 'oo-ui-menuLayout-static' );
1729 if ( config
.menuPanel
) {
1730 this.setMenuPanel( config
.menuPanel
);
1732 if ( config
.contentPanel
) {
1733 this.setContentPanel( config
.contentPanel
);
1735 this.setMenuPosition( config
.menuPosition
);
1736 this.toggleMenu( config
.showMenu
);
1741 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
1748 * @param {boolean} showMenu Show menu, omit to toggle
1750 * @return {OO.ui.MenuLayout} The layout, for chaining
1752 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
1753 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
1755 if ( this.showMenu
!== showMenu
) {
1756 this.showMenu
= showMenu
;
1758 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu
)
1759 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu
);
1760 this.$menu
.attr( 'aria-hidden', this.showMenu
? 'false' : 'true' );
1767 * Check if menu is visible
1769 * @return {boolean} Menu is visible
1771 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
1772 return this.showMenu
;
1776 * Set menu position.
1778 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1780 * @return {OO.ui.MenuLayout} The layout, for chaining
1782 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
1783 if ( [ 'top', 'bottom', 'before', 'after' ].indexOf( position
) === -1 ) {
1784 position
= 'before';
1787 this.$element
.removeClass( 'oo-ui-menuLayout-' + this.menuPosition
);
1788 this.menuPosition
= position
;
1789 if ( this.menuPosition
=== 'top' || this.menuPosition
=== 'before' ) {
1790 this.$element
.append( this.$menu
, this.$content
);
1792 this.$element
.append( this.$content
, this.$menu
);
1794 this.$element
.addClass( 'oo-ui-menuLayout-' + position
);
1800 * Get menu position.
1802 * @return {string} Menu position
1804 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
1805 return this.menuPosition
;
1809 * Set the menu panel.
1811 * @param {OO.ui.PanelLayout} menuPanel Menu panel
1813 OO
.ui
.MenuLayout
.prototype.setMenuPanel = function ( menuPanel
) {
1814 this.menuPanel
= menuPanel
;
1815 this.$menu
.append( this.menuPanel
.$element
);
1819 * Set the content panel.
1821 * @param {OO.ui.PanelLayout} contentPanel Content panel
1823 OO
.ui
.MenuLayout
.prototype.setContentPanel = function ( contentPanel
) {
1824 this.contentPanel
= contentPanel
;
1825 this.$content
.append( this.contentPanel
.$element
);
1829 * Clear the menu panel.
1831 OO
.ui
.MenuLayout
.prototype.clearMenuPanel = function () {
1832 this.menuPanel
= null;
1837 * Clear the content panel.
1839 OO
.ui
.MenuLayout
.prototype.clearContentPanel = function () {
1840 this.contentPanel
= null;
1841 this.$content
.empty();
1845 * Reset the scroll offset of all panels and the tab select widget
1849 OO
.ui
.MenuLayout
.prototype.resetScroll = function () {
1850 if ( this.menuPanel
) {
1851 this.menuPanel
.resetScroll();
1853 if ( this.contentPanel
) {
1854 this.contentPanel
.resetScroll();
1861 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1862 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1863 * through the pages and select which one to display. By default, only one page is
1864 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1865 * the booklet layout automatically focuses on the first focusable element, unless the
1866 * default setting is changed. Optionally, booklets can be configured to show
1867 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1870 * // Example of a BookletLayout that contains two PageLayouts.
1872 * function PageOneLayout( name, config ) {
1873 * PageOneLayout.parent.call( this, name, config );
1874 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on ' +
1875 * 'the left)</p>' );
1877 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1878 * PageOneLayout.prototype.setupOutlineItem = function () {
1879 * this.outlineItem.setLabel( 'Page One' );
1882 * function PageTwoLayout( name, config ) {
1883 * PageTwoLayout.parent.call( this, name, config );
1884 * this.$element.append( '<p>Second page</p>' );
1886 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1887 * PageTwoLayout.prototype.setupOutlineItem = function () {
1888 * this.outlineItem.setLabel( 'Page Two' );
1891 * var page1 = new PageOneLayout( 'one' ),
1892 * page2 = new PageTwoLayout( 'two' );
1894 * var booklet = new OO.ui.BookletLayout( {
1898 * booklet.addPages( [ page1, page2 ] );
1899 * $( document.body ).append( booklet.$element );
1902 * @extends OO.ui.MenuLayout
1905 * @param {Object} [config] Configuration options
1906 * @cfg {boolean} [continuous=false] Show all pages, one after another
1907 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is
1908 * displayed. Disabled on mobile.
1909 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the
1910 * pages of the booklet.
1911 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages.
1913 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
1914 // Configuration initialization
1915 config
= config
|| {};
1917 // Parent constructor
1918 OO
.ui
.BookletLayout
.parent
.call( this, config
);
1921 this.currentPageName
= null;
1923 this.ignoreFocus
= false;
1924 this.stackLayout
= new OO
.ui
.StackLayout( {
1925 continuous
: !!config
.continuous
,
1926 expanded
: this.expanded
1928 this.setContentPanel( this.stackLayout
);
1929 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
1930 this.outlineVisible
= false;
1931 this.outlined
= !!config
.outlined
;
1932 if ( this.outlined
) {
1933 this.editable
= !!config
.editable
;
1934 this.outlineControlsWidget
= null;
1935 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget();
1936 this.outlinePanel
= new OO
.ui
.PanelLayout( {
1937 expanded
: this.expanded
,
1940 this.setMenuPanel( this.outlinePanel
);
1941 this.outlineVisible
= true;
1942 if ( this.editable
) {
1943 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
1944 this.outlineSelectWidget
1948 this.toggleMenu( this.outlined
);
1951 this.stackLayout
.connect( this, {
1952 set: 'onStackLayoutSet'
1954 if ( this.outlined
) {
1955 this.outlineSelectWidget
.connect( this, {
1956 select
: 'onOutlineSelectWidgetSelect'
1958 this.scrolling
= false;
1959 this.stackLayout
.connect( this, {
1960 visibleItemChange
: 'onStackLayoutVisibleItemChange'
1963 if ( this.autoFocus
) {
1964 // Event 'focus' does not bubble, but 'focusin' does
1965 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
1969 this.$element
.addClass( 'oo-ui-bookletLayout' );
1970 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
1971 if ( this.outlined
) {
1972 this.outlinePanel
.$element
1973 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1974 .append( this.outlineSelectWidget
.$element
);
1975 if ( this.editable
) {
1976 this.outlinePanel
.$element
1977 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1978 .append( this.outlineControlsWidget
.$element
);
1985 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
1990 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the
1993 * @param {OO.ui.PageLayout} page Current page
1997 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
2000 * @param {OO.ui.PageLayout[]} page Added pages
2001 * @param {number} index Index pages were added at
2005 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
2006 * {@link #removePages removed} from the booklet.
2009 * @param {OO.ui.PageLayout[]} pages Removed pages
2015 * Handle stack layout focus.
2018 * @param {jQuery.Event} e Focusin event
2020 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
2023 // Find the page that an element was focused within
2024 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
2025 for ( name
in this.pages
) {
2026 // Check for page match, exclude current page to find only page changes
2027 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
2028 this.setPage( name
);
2035 * Handle visibleItemChange events from the stackLayout
2037 * The next visible page is set as the current page by selecting it
2040 * @param {OO.ui.PageLayout} page The next visible page in the layout
2042 OO
.ui
.BookletLayout
.prototype.onStackLayoutVisibleItemChange = function ( page
) {
2043 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
2044 // try and scroll the item into view again.
2045 this.scrolling
= true;
2046 this.outlineSelectWidget
.selectItemByData( page
.getName() );
2047 this.scrolling
= false;
2051 * Handle stack layout set events.
2054 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
2056 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
2057 var promise
, layout
= this;
2058 // If everything is unselected, do nothing
2062 // For continuous BookletLayouts, scroll the selected page into view first
2063 if ( this.stackLayout
.continuous
&& !this.scrolling
) {
2064 promise
= page
.scrollElementIntoView();
2066 promise
= $.Deferred().resolve();
2068 // Focus the first element on the newly selected panel.
2069 // Don't focus if the page was set by scrolling.
2070 if ( this.autoFocus
&& !OO
.ui
.isMobile() && !this.scrolling
) {
2071 promise
.done( function () {
2078 * Focus the first input in the current page.
2080 * If no page is selected, the first selectable page will be selected.
2081 * If the focus is already in an element on the current page, nothing will happen.
2083 * @param {number} [itemIndex] A specific item to focus on
2085 OO
.ui
.BookletLayout
.prototype.focus = function ( itemIndex
) {
2087 items
= this.stackLayout
.getItems();
2089 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2090 page
= items
[ itemIndex
];
2092 page
= this.stackLayout
.getCurrentItem();
2095 if ( !page
&& this.outlined
) {
2096 this.selectFirstSelectablePage();
2097 page
= this.stackLayout
.getCurrentItem();
2102 // Only change the focus if is not already in the current page
2103 if ( !OO
.ui
.contains( page
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
2109 * Find the first focusable input in the booklet layout and focus
2112 OO
.ui
.BookletLayout
.prototype.focusFirstFocusable = function () {
2113 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2117 * Handle outline widget select events.
2120 * @param {OO.ui.OptionWidget|null} item Selected item
2122 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
2124 this.setPage( item
.getData() );
2129 * Check if booklet has an outline.
2131 * @return {boolean} Booklet has an outline
2133 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
2134 return this.outlined
;
2138 * Check if booklet has editing controls.
2140 * @return {boolean} Booklet is editable
2142 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
2143 return this.editable
;
2147 * Check if booklet has a visible outline.
2149 * @return {boolean} Outline is visible
2151 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
2152 return this.outlined
&& this.outlineVisible
;
2156 * Hide or show the outline.
2158 * @param {boolean} [show] Show outline, omit to invert current state
2160 * @return {OO.ui.BookletLayout} The layout, for chaining
2162 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
2165 if ( this.outlined
) {
2166 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
2167 this.outlineVisible
= show
;
2168 this.toggleMenu( show
);
2169 if ( show
&& this.editable
) {
2170 // HACK: Kill dumb scrollbars when the sidebar stops animating, see T161798.
2171 // Only necessary when outline controls are present, delay matches transition on
2172 // `.oo-ui-menuLayout-menu`.
2173 setTimeout( function () {
2174 OO
.ui
.Element
.static.reconsiderScrollbars( booklet
.outlinePanel
.$element
[ 0 ] );
2175 }, OO
.ui
.theme
.getDialogTransitionDuration() );
2183 * Find the page closest to the specified page.
2185 * @param {OO.ui.PageLayout} page Page to use as a reference point
2186 * @return {OO.ui.PageLayout|null} Page closest to the specified page
2188 OO
.ui
.BookletLayout
.prototype.findClosestPage = function ( page
) {
2189 var next
, prev
, level
,
2190 pages
= this.stackLayout
.getItems(),
2191 index
= pages
.indexOf( page
);
2193 if ( index
!== -1 ) {
2194 next
= pages
[ index
+ 1 ];
2195 prev
= pages
[ index
- 1 ];
2196 // Prefer adjacent pages at the same level
2197 if ( this.outlined
) {
2198 level
= this.outlineSelectWidget
.findItemFromData( page
.getName() ).getLevel();
2201 level
=== this.outlineSelectWidget
.findItemFromData( prev
.getName() ).getLevel()
2207 level
=== this.outlineSelectWidget
.findItemFromData( next
.getName() ).getLevel()
2213 return prev
|| next
|| null;
2217 * Get the outline widget.
2219 * If the booklet is not outlined, the method will return `null`.
2221 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
2223 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
2224 return this.outlineSelectWidget
;
2228 * Get the outline controls widget.
2230 * If the outline is not editable, the method will return `null`.
2232 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
2234 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
2235 return this.outlineControlsWidget
;
2239 * Get a page by its symbolic name.
2241 * @param {string} name Symbolic name of page
2242 * @return {OO.ui.PageLayout|undefined} Page, if found
2244 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
2245 return this.pages
[ name
];
2249 * Get the current page.
2251 * @return {OO.ui.PageLayout|undefined} Current page, if found
2253 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
2254 var name
= this.getCurrentPageName();
2255 return name
? this.getPage( name
) : undefined;
2259 * Get the symbolic name of the current page.
2261 * @return {string|null} Symbolic name of the current page
2263 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
2264 return this.currentPageName
;
2268 * Add pages to the booklet layout
2270 * When pages are added with the same names as existing pages, the existing pages will be
2271 * automatically removed before the new pages are added.
2273 * @param {OO.ui.PageLayout[]} pages Pages to add
2274 * @param {number} index Index of the insertion point
2277 * @return {OO.ui.BookletLayout} The layout, for chaining
2279 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
2280 var i
, len
, name
, page
, item
, currentIndex
,
2281 stackLayoutPages
= this.stackLayout
.getItems(),
2285 // Remove pages with same names
2286 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2288 name
= page
.getName();
2290 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
2291 // Correct the insertion index
2292 currentIndex
= stackLayoutPages
.indexOf( this.pages
[ name
] );
2293 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2296 remove
.push( this.pages
[ name
] );
2299 if ( remove
.length
) {
2300 this.removePages( remove
);
2304 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2306 name
= page
.getName();
2307 this.pages
[ page
.getName() ] = page
;
2308 if ( this.outlined
) {
2309 item
= new OO
.ui
.OutlineOptionWidget( { data
: name
} );
2310 page
.setOutlineItem( item
);
2315 if ( this.outlined
&& items
.length
) {
2316 this.outlineSelectWidget
.addItems( items
, index
);
2317 this.selectFirstSelectablePage();
2319 this.stackLayout
.addItems( pages
, index
);
2320 this.emit( 'add', pages
, index
);
2326 * Remove the specified pages from the booklet layout.
2328 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2330 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2333 * @return {OO.ui.BookletLayout} The layout, for chaining
2335 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
2336 var i
, len
, name
, page
,
2339 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2341 name
= page
.getName();
2342 delete this.pages
[ name
];
2343 if ( this.outlined
) {
2344 items
.push( this.outlineSelectWidget
.findItemFromData( name
) );
2345 page
.setOutlineItem( null );
2348 if ( this.outlined
&& items
.length
) {
2349 this.outlineSelectWidget
.removeItems( items
);
2350 this.selectFirstSelectablePage();
2352 this.stackLayout
.removeItems( pages
);
2353 this.emit( 'remove', pages
);
2359 * Clear all pages from the booklet layout.
2361 * To remove only a subset of pages from the booklet, use the #removePages method.
2365 * @return {OO.ui.BookletLayout} The layout, for chaining
2367 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
2369 pages
= this.stackLayout
.getItems();
2372 this.currentPageName
= null;
2373 if ( this.outlined
) {
2374 this.outlineSelectWidget
.clearItems();
2375 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2376 pages
[ i
].setOutlineItem( null );
2379 this.stackLayout
.clearItems();
2381 this.emit( 'remove', pages
);
2387 * Set the current page by symbolic name.
2390 * @param {string} name Symbolic name of page
2392 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
2395 page
= this.pages
[ name
],
2396 previousPage
= this.currentPageName
&& this.pages
[ this.currentPageName
];
2398 if ( name
!== this.currentPageName
) {
2399 if ( this.outlined
) {
2400 selectedItem
= this.outlineSelectWidget
.findSelectedItem();
2401 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2402 this.outlineSelectWidget
.selectItemByData( name
);
2406 if ( previousPage
) {
2407 previousPage
.setActive( false );
2408 // Blur anything focused if the next page doesn't have anything focusable.
2409 // This is not needed if the next page has something focusable (because once it is
2410 // focused this blur happens automatically). If the layout is non-continuous, this
2411 // check is meaningless because the next page is not visible yet and thus can't
2415 !OO
.ui
.isMobile() &&
2416 this.stackLayout
.continuous
&&
2417 OO
.ui
.findFocusable( page
.$element
).length
!== 0
2419 $focused
= previousPage
.$element
.find( ':focus' );
2420 if ( $focused
.length
) {
2421 $focused
[ 0 ].blur();
2425 this.currentPageName
= name
;
2426 page
.setActive( true );
2427 this.stackLayout
.setItem( page
);
2428 if ( !this.stackLayout
.continuous
&& previousPage
) {
2429 // This should not be necessary, since any inputs on the previous page should have
2430 // been blurred when it was hidden, but browsers are not very consistent about
2432 $focused
= previousPage
.$element
.find( ':focus' );
2433 if ( $focused
.length
) {
2434 $focused
[ 0 ].blur();
2437 this.emit( 'set', page
);
2443 * For outlined-continuous booklets, also reset the outlineSelectWidget to the first item.
2447 OO
.ui
.BookletLayout
.prototype.resetScroll = function () {
2449 OO
.ui
.BookletLayout
.parent
.prototype.resetScroll
.call( this );
2453 this.stackLayout
.continuous
&&
2454 this.outlineSelectWidget
.findFirstSelectableItem()
2456 this.scrolling
= true;
2457 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.findFirstSelectableItem() );
2458 this.scrolling
= false;
2464 * Select the first selectable page.
2467 * @return {OO.ui.BookletLayout} The layout, for chaining
2469 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
2470 if ( !this.outlineSelectWidget
.findSelectedItem() ) {
2471 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.findFirstSelectableItem() );
2478 * IndexLayouts contain {@link OO.ui.TabPanelLayout tab panel layouts} as well as
2479 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the tab panels
2480 * and select which one to display. By default, only one tab panel is displayed at a time. When a
2481 * user navigates to a new tab panel, the index layout automatically focuses on the first focusable
2482 * element, unless the default setting is changed.
2484 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2487 * // Example of a IndexLayout that contains two TabPanelLayouts.
2489 * function TabPanelOneLayout( name, config ) {
2490 * TabPanelOneLayout.parent.call( this, name, config );
2491 * this.$element.append( '<p>First tab panel</p>' );
2493 * OO.inheritClass( TabPanelOneLayout, OO.ui.TabPanelLayout );
2494 * TabPanelOneLayout.prototype.setupTabItem = function () {
2495 * this.tabItem.setLabel( 'Tab panel one' );
2498 * var tabPanel1 = new TabPanelOneLayout( 'one' ),
2499 * tabPanel2 = new OO.ui.TabPanelLayout( 'two', { label: 'Tab panel two' } );
2501 * tabPanel2.$element.append( '<p>Second tab panel</p>' );
2503 * var index = new OO.ui.IndexLayout();
2505 * index.addTabPanels( [ tabPanel1, tabPanel2 ] );
2506 * $( document.body ).append( index.$element );
2509 * @extends OO.ui.MenuLayout
2512 * @param {Object} [config] Configuration options
2513 * @cfg {OO.ui.StackLayout} [contentPanel] Content stack (see MenuLayout)
2514 * @cfg {boolean} [continuous=false] Show all tab panels, one after another
2515 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new tab panel is
2516 * displayed. Disabled on mobile.
2517 * @cfg {boolean} [framed=true] Render the tabs with frames
2519 OO
.ui
.IndexLayout
= function OoUiIndexLayout( config
) {
2520 // Configuration initialization
2521 config
= $.extend( {}, config
, { menuPosition
: 'top' } );
2523 // Parent constructor
2524 OO
.ui
.IndexLayout
.parent
.call( this, config
);
2527 this.currentTabPanelName
= null;
2528 // Allow infused widgets to pass existing tabPanels
2529 this.tabPanels
= config
.tabPanels
|| {};
2531 this.ignoreFocus
= false;
2532 this.stackLayout
= this.contentPanel
|| new OO
.ui
.StackLayout( {
2533 continuous
: !!config
.continuous
,
2534 expanded
: this.expanded
2536 this.setContentPanel( this.stackLayout
);
2537 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
2539 // Allow infused widgets to pass an existing tabSelectWidget
2540 this.tabSelectWidget
= config
.tabSelectWidget
|| new OO
.ui
.TabSelectWidget( {
2541 framed
: config
.framed
=== undefined || config
.framed
2543 this.tabPanel
= this.menuPanel
|| new OO
.ui
.PanelLayout( {
2544 expanded
: this.expanded
2546 this.setMenuPanel( this.tabPanel
);
2548 this.toggleMenu( true );
2551 this.stackLayout
.connect( this, {
2552 set: 'onStackLayoutSet'
2554 this.tabSelectWidget
.connect( this, {
2555 select
: 'onTabSelectWidgetSelect'
2557 if ( this.autoFocus
) {
2558 // Event 'focus' does not bubble, but 'focusin' does.
2559 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
2563 this.$element
.addClass( 'oo-ui-indexLayout' );
2564 this.stackLayout
.$element
.addClass( 'oo-ui-indexLayout-stackLayout' );
2565 this.tabPanel
.$element
2566 .addClass( 'oo-ui-indexLayout-tabPanel' )
2567 .append( this.tabSelectWidget
.$element
);
2569 this.selectFirstSelectableTabPanel();
2574 OO
.inheritClass( OO
.ui
.IndexLayout
, OO
.ui
.MenuLayout
);
2579 * A 'set' event is emitted when a tab panel is {@link #setTabPanel set} to be displayed by the
2583 * @param {OO.ui.TabPanelLayout} tabPanel Current tab panel
2587 * An 'add' event is emitted when tab panels are {@link #addTabPanels added} to the index layout.
2590 * @param {OO.ui.TabPanelLayout[]} tabPanel Added tab panels
2591 * @param {number} index Index tab panels were added at
2595 * A 'remove' event is emitted when tab panels are {@link #clearTabPanels cleared} or
2596 * {@link #removeTabPanels removed} from the index.
2599 * @param {OO.ui.TabPanelLayout[]} tabPanel Removed tab panels
2605 * Handle stack layout focus.
2608 * @param {jQuery.Event} e Focusing event
2610 OO
.ui
.IndexLayout
.prototype.onStackLayoutFocus = function ( e
) {
2613 // Find the tab panel that an element was focused within
2614 $target
= $( e
.target
).closest( '.oo-ui-tabPanelLayout' );
2615 for ( name
in this.tabPanels
) {
2616 // Check for tab panel match, exclude current tab panel to find only tab panel changes
2617 if ( this.tabPanels
[ name
].$element
[ 0 ] === $target
[ 0 ] &&
2618 name
!== this.currentTabPanelName
) {
2619 this.setTabPanel( name
);
2626 * Handle stack layout set events.
2629 * @param {OO.ui.PanelLayout|null} tabPanel The tab panel that is now the current panel
2631 OO
.ui
.IndexLayout
.prototype.onStackLayoutSet = function ( tabPanel
) {
2632 // If everything is unselected, do nothing
2636 // Focus the first element on the newly selected panel
2637 if ( this.autoFocus
&& !OO
.ui
.isMobile() ) {
2643 * Focus the first input in the current tab panel.
2645 * If no tab panel is selected, the first selectable tab panel will be selected.
2646 * If the focus is already in an element on the current tab panel, nothing will happen.
2648 * @param {number} [itemIndex] A specific item to focus on
2650 OO
.ui
.IndexLayout
.prototype.focus = function ( itemIndex
) {
2652 items
= this.stackLayout
.getItems();
2654 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2655 tabPanel
= items
[ itemIndex
];
2657 tabPanel
= this.stackLayout
.getCurrentItem();
2661 this.selectFirstSelectableTabPanel();
2662 tabPanel
= this.stackLayout
.getCurrentItem();
2667 // Only change the focus if is not already in the current page
2668 if ( !OO
.ui
.contains(
2669 tabPanel
.$element
[ 0 ],
2670 this.getElementDocument().activeElement
,
2678 * Find the first focusable input in the index layout and focus
2681 OO
.ui
.IndexLayout
.prototype.focusFirstFocusable = function () {
2682 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2686 * Handle tab widget select events.
2689 * @param {OO.ui.OptionWidget|null} item Selected item
2691 OO
.ui
.IndexLayout
.prototype.onTabSelectWidgetSelect = function ( item
) {
2693 this.setTabPanel( item
.getData() );
2698 * Get the tab panel closest to the specified tab panel.
2700 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2701 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2703 OO
.ui
.IndexLayout
.prototype.getClosestTabPanel = function ( tabPanel
) {
2704 var next
, prev
, level
,
2705 tabPanels
= this.stackLayout
.getItems(),
2706 index
= tabPanels
.indexOf( tabPanel
);
2708 if ( index
!== -1 ) {
2709 next
= tabPanels
[ index
+ 1 ];
2710 prev
= tabPanels
[ index
- 1 ];
2711 // Prefer adjacent tab panels at the same level
2712 level
= this.tabSelectWidget
.findItemFromData( tabPanel
.getName() ).getLevel();
2715 level
=== this.tabSelectWidget
.findItemFromData( prev
.getName() ).getLevel()
2721 level
=== this.tabSelectWidget
.findItemFromData( next
.getName() ).getLevel()
2726 return prev
|| next
|| null;
2730 * Get the tabs widget.
2732 * @return {OO.ui.TabSelectWidget} Tabs widget
2734 OO
.ui
.IndexLayout
.prototype.getTabs = function () {
2735 return this.tabSelectWidget
;
2739 * Get a tab panel by its symbolic name.
2741 * @param {string} name Symbolic name of tab panel
2742 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2744 OO
.ui
.IndexLayout
.prototype.getTabPanel = function ( name
) {
2745 return this.tabPanels
[ name
];
2749 * Get the current tab panel.
2751 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2753 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanel = function () {
2754 var name
= this.getCurrentTabPanelName();
2755 return name
? this.getTabPanel( name
) : undefined;
2759 * Get the symbolic name of the current tab panel.
2761 * @return {string|null} Symbolic name of the current tab panel
2763 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanelName = function () {
2764 return this.currentTabPanelName
;
2768 * Add tab panels to the index layout.
2770 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2771 * will be automatically removed before the new tab panels are added.
2773 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2774 * @param {number} index Index of the insertion point
2777 * @return {OO.ui.BookletLayout} The layout, for chaining
2779 OO
.ui
.IndexLayout
.prototype.addTabPanels = function ( tabPanels
, index
) {
2780 var i
, len
, name
, tabPanel
, item
, currentIndex
,
2781 stackLayoutTabPanels
= this.stackLayout
.getItems(),
2785 // Remove tab panels with same names
2786 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2787 tabPanel
= tabPanels
[ i
];
2788 name
= tabPanel
.getName();
2790 if ( Object
.prototype.hasOwnProperty
.call( this.tabPanels
, name
) ) {
2791 // Correct the insertion index
2792 currentIndex
= stackLayoutTabPanels
.indexOf( this.tabPanels
[ name
] );
2793 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2796 remove
.push( this.tabPanels
[ name
] );
2799 if ( remove
.length
) {
2800 this.removeTabPanels( remove
);
2803 // Add new tab panels
2804 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2805 tabPanel
= tabPanels
[ i
];
2806 name
= tabPanel
.getName();
2807 this.tabPanels
[ tabPanel
.getName() ] = tabPanel
;
2808 item
= new OO
.ui
.TabOptionWidget( { data
: name
} );
2809 tabPanel
.setTabItem( item
);
2813 if ( items
.length
) {
2814 this.tabSelectWidget
.addItems( items
, index
);
2815 this.selectFirstSelectableTabPanel();
2817 this.stackLayout
.addItems( tabPanels
, index
);
2818 this.emit( 'add', tabPanels
, index
);
2824 * Remove the specified tab panels from the index layout.
2826 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2828 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2831 * @return {OO.ui.BookletLayout} The layout, for chaining
2833 OO
.ui
.IndexLayout
.prototype.removeTabPanels = function ( tabPanels
) {
2834 var i
, len
, name
, tabPanel
,
2837 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2838 tabPanel
= tabPanels
[ i
];
2839 name
= tabPanel
.getName();
2840 delete this.tabPanels
[ name
];
2841 items
.push( this.tabSelectWidget
.findItemFromData( name
) );
2842 tabPanel
.setTabItem( null );
2844 if ( items
.length
) {
2845 this.tabSelectWidget
.removeItems( items
);
2846 this.selectFirstSelectableTabPanel();
2848 this.stackLayout
.removeItems( tabPanels
);
2849 this.emit( 'remove', tabPanels
);
2855 * Clear all tab panels from the index layout.
2857 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2861 * @return {OO.ui.BookletLayout} The layout, for chaining
2863 OO
.ui
.IndexLayout
.prototype.clearTabPanels = function () {
2865 tabPanels
= this.stackLayout
.getItems();
2867 this.tabPanels
= {};
2868 this.currentTabPanelName
= null;
2869 this.tabSelectWidget
.clearItems();
2870 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2871 tabPanels
[ i
].setTabItem( null );
2873 this.stackLayout
.clearItems();
2875 this.emit( 'remove', tabPanels
);
2881 * Set the current tab panel by symbolic name.
2884 * @param {string} name Symbolic name of tab panel
2886 OO
.ui
.IndexLayout
.prototype.setTabPanel = function ( name
) {
2890 tabPanel
= this.tabPanels
[ name
];
2892 if ( name
!== this.currentTabPanelName
) {
2893 previousTabPanel
= this.getCurrentTabPanel();
2894 selectedItem
= this.tabSelectWidget
.findSelectedItem();
2895 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2896 this.tabSelectWidget
.selectItemByData( name
);
2899 if ( previousTabPanel
) {
2900 previousTabPanel
.setActive( false );
2901 // Blur anything focused if the next tab panel doesn't have anything focusable.
2902 // This is not needed if the next tab panel has something focusable (because once
2903 // it is focused this blur happens automatically). If the layout is non-continuous,
2904 // this check is meaningless because the next tab panel is not visible yet and thus
2905 // can't hold focus.
2908 !OO
.ui
.isMobile() &&
2909 this.stackLayout
.continuous
&&
2910 OO
.ui
.findFocusable( tabPanel
.$element
).length
!== 0
2912 $focused
= previousTabPanel
.$element
.find( ':focus' );
2913 if ( $focused
.length
) {
2914 $focused
[ 0 ].blur();
2918 this.currentTabPanelName
= name
;
2919 tabPanel
.setActive( true );
2920 this.stackLayout
.setItem( tabPanel
);
2921 if ( !this.stackLayout
.continuous
&& previousTabPanel
) {
2922 // This should not be necessary, since any inputs on the previous tab panel should
2923 // have been blurred when it was hidden, but browsers are not very consistent about
2925 $focused
= previousTabPanel
.$element
.find( ':focus' );
2926 if ( $focused
.length
) {
2927 $focused
[ 0 ].blur();
2930 this.emit( 'set', tabPanel
);
2936 * Select the first selectable tab panel.
2939 * @return {OO.ui.BookletLayout} The layout, for chaining
2941 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableTabPanel = function () {
2942 if ( !this.tabSelectWidget
.findSelectedItem() ) {
2943 this.tabSelectWidget
.selectItem( this.tabSelectWidget
.findFirstSelectableItem() );
2950 * ToggleWidget implements basic behavior of widgets with an on/off state.
2951 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2955 * @extends OO.ui.Widget
2956 * @mixins OO.ui.mixin.TitledElement
2959 * @param {Object} [config] Configuration options
2960 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2961 * By default, the toggle is in the 'off' state.
2963 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
2964 // Configuration initialization
2965 config
= config
|| {};
2967 // Parent constructor
2968 OO
.ui
.ToggleWidget
.parent
.call( this, config
);
2970 // Mixin constructor
2971 OO
.ui
.mixin
.TitledElement
.call( this, config
);
2977 this.$element
.addClass( 'oo-ui-toggleWidget' );
2978 this.setValue( !!config
.value
);
2983 OO
.inheritClass( OO
.ui
.ToggleWidget
, OO
.ui
.Widget
);
2984 OO
.mixinClass( OO
.ui
.ToggleWidget
, OO
.ui
.mixin
.TitledElement
);
2991 * A change event is emitted when the on/off state of the toggle changes.
2993 * @param {boolean} value Value representing the new state of the toggle
2999 * Get the value representing the toggle’s state.
3001 * @return {boolean} The on/off state of the toggle
3003 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
3008 * Set the state of the toggle: `true` for 'on', `false` for 'off'.
3010 * @param {boolean} value The state of the toggle
3013 * @return {OO.ui.Widget} The widget, for chaining
3015 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
3017 if ( this.value
!== value
) {
3019 this.emit( 'change', value
);
3020 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
3021 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
3027 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
3028 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
3029 * configured with {@link OO.ui.mixin.IconElement icons},
3030 * {@link OO.ui.mixin.IndicatorElement indicators},
3031 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
3032 * and {@link OO.ui.mixin.LabelElement labels}. Please see
3033 * the [OOUI documentation][1] on MediaWiki for more information.
3036 * // Toggle buttons in the 'off' and 'on' state.
3037 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
3038 * label: 'Toggle Button off'
3040 * toggleButton2 = new OO.ui.ToggleButtonWidget( {
3041 * label: 'Toggle Button on',
3044 * // Append the buttons to the DOM.
3045 * $( document.body ).append( toggleButton1.$element, toggleButton2.$element );
3047 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Buttons_and_Switches#Toggle_buttons
3050 * @extends OO.ui.ToggleWidget
3051 * @mixins OO.ui.mixin.ButtonElement
3052 * @mixins OO.ui.mixin.IconElement
3053 * @mixins OO.ui.mixin.IndicatorElement
3054 * @mixins OO.ui.mixin.LabelElement
3055 * @mixins OO.ui.mixin.FlaggedElement
3056 * @mixins OO.ui.mixin.TabIndexedElement
3059 * @param {Object} [config] Configuration options
3060 * @cfg {boolean} [value=false] The toggle button’s initial on/off
3061 * state. By default, the button is in the 'off' state.
3063 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
3064 // Configuration initialization
3065 config
= config
|| {};
3067 // Parent constructor
3068 OO
.ui
.ToggleButtonWidget
.parent
.call( this, config
);
3070 // Mixin constructors
3071 OO
.ui
.mixin
.ButtonElement
.call( this, $.extend( {
3074 OO
.ui
.mixin
.IconElement
.call( this, config
);
3075 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3076 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3077 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3078 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {
3079 $tabIndexed
: this.$button
3083 this.connect( this, {
3088 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3090 .addClass( 'oo-ui-toggleButtonWidget' )
3091 .append( this.$button
);
3092 this.setTitledElement( this.$button
);
3097 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
3098 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
3099 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IconElement
);
3100 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
3101 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.LabelElement
);
3102 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
3103 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3105 /* Static Properties */
3111 OO
.ui
.ToggleButtonWidget
.static.tagName
= 'span';
3116 * Handle the button action being triggered.
3120 OO
.ui
.ToggleButtonWidget
.prototype.onAction = function () {
3121 this.setValue( !this.value
);
3127 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
3129 if ( value
!== this.value
) {
3130 // Might be called from parent constructor before ButtonElement constructor
3131 if ( this.$button
) {
3132 this.$button
.attr( 'aria-pressed', value
.toString() );
3134 this.setActive( value
);
3138 OO
.ui
.ToggleButtonWidget
.parent
.prototype.setValue
.call( this, value
);
3146 OO
.ui
.ToggleButtonWidget
.prototype.setButtonElement = function ( $button
) {
3147 if ( this.$button
) {
3148 this.$button
.removeAttr( 'aria-pressed' );
3150 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement
.call( this, $button
);
3151 this.$button
.attr( 'aria-pressed', this.value
.toString() );
3155 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
3156 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
3157 * visually by a slider in the leftmost position.
3160 * // Toggle switches in the 'off' and 'on' position.
3161 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget(),
3162 * toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
3165 * // Create a FieldsetLayout to layout and label switches.
3166 * fieldset = new OO.ui.FieldsetLayout( {
3167 * label: 'Toggle switches'
3169 * fieldset.addItems( [
3170 * new OO.ui.FieldLayout( toggleSwitch1, {
3174 * new OO.ui.FieldLayout( toggleSwitch2, {
3179 * $( document.body ).append( fieldset.$element );
3182 * @extends OO.ui.ToggleWidget
3183 * @mixins OO.ui.mixin.TabIndexedElement
3186 * @param {Object} [config] Configuration options
3187 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
3188 * By default, the toggle switch is in the 'off' position.
3190 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
3191 // Parent constructor
3192 OO
.ui
.ToggleSwitchWidget
.parent
.call( this, config
);
3194 // Mixin constructors
3195 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3198 this.dragging
= false;
3199 this.dragStart
= null;
3200 this.sliding
= false;
3201 this.$glow
= $( '<span>' );
3202 this.$grip
= $( '<span>' );
3206 click
: this.onClick
.bind( this ),
3207 keypress
: this.onKeyPress
.bind( this )
3211 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
3212 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
3214 .addClass( 'oo-ui-toggleSwitchWidget' )
3215 .attr( 'role', 'checkbox' )
3216 .append( this.$glow
, this.$grip
);
3221 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
3222 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3227 * Handle mouse click events.
3230 * @param {jQuery.Event} e Mouse click event
3231 * @return {undefined|boolean} False to prevent default if event is handled
3233 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
3234 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
3235 this.setValue( !this.value
);
3241 * Handle key press events.
3244 * @param {jQuery.Event} e Key press event
3245 * @return {undefined|boolean} False to prevent default if event is handled
3247 OO
.ui
.ToggleSwitchWidget
.prototype.onKeyPress = function ( e
) {
3248 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
3249 this.setValue( !this.value
);
3257 OO
.ui
.ToggleSwitchWidget
.prototype.setValue = function ( value
) {
3258 OO
.ui
.ToggleSwitchWidget
.parent
.prototype.setValue
.call( this, value
);
3259 this.$element
.attr( 'aria-checked', this.value
.toString() );
3266 OO
.ui
.ToggleSwitchWidget
.prototype.simulateLabelClick = function () {
3267 if ( !this.isDisabled() ) {
3268 this.setValue( !this.value
);
3274 * OutlineControlsWidget is a set of controls for an
3275 * {@link OO.ui.OutlineSelectWidget outline select widget}.
3276 * Controls include moving items up and down, removing items, and adding different kinds of items.
3278 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3281 * @extends OO.ui.Widget
3282 * @mixins OO.ui.mixin.GroupElement
3285 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
3286 * @param {Object} [config] Configuration options
3287 * @cfg {Object} [abilities] List of abilties
3288 * @cfg {boolean} [abilities.move=true] Allow moving movable items
3289 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
3291 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
3292 // Allow passing positional parameters inside the config object
3293 if ( OO
.isPlainObject( outline
) && config
=== undefined ) {
3295 outline
= config
.outline
;
3298 // Configuration initialization
3299 config
= config
|| {};
3301 // Parent constructor
3302 OO
.ui
.OutlineControlsWidget
.parent
.call( this, config
);
3304 // Mixin constructors
3305 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3308 this.outline
= outline
;
3309 this.$movers
= $( '<div>' );
3310 this.upButton
= new OO
.ui
.ButtonWidget( {
3313 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
3315 this.downButton
= new OO
.ui
.ButtonWidget( {
3318 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
3320 this.removeButton
= new OO
.ui
.ButtonWidget( {
3323 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
3325 this.abilities
= { move: true, remove
: true };
3328 outline
.connect( this, {
3329 select
: 'onOutlineChange',
3330 add
: 'onOutlineChange',
3331 remove
: 'onOutlineChange'
3333 this.upButton
.connect( this, {
3334 click
: [ 'emit', 'move', -1 ]
3336 this.downButton
.connect( this, {
3337 click
: [ 'emit', 'move', 1 ]
3339 this.removeButton
.connect( this, {
3340 click
: [ 'emit', 'remove' ]
3344 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
3345 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
3347 .addClass( 'oo-ui-outlineControlsWidget-movers' )
3348 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
3349 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
3350 this.setAbilities( config
.abilities
|| {} );
3355 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
3356 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.GroupElement
);
3362 * @param {number} places Number of places to move
3374 * @param {Object} abilities List of abilties
3375 * @param {boolean} [abilities.move] Allow moving movable items
3376 * @param {boolean} [abilities.remove] Allow removing removable items
3378 OO
.ui
.OutlineControlsWidget
.prototype.setAbilities = function ( abilities
) {
3381 for ( ability
in this.abilities
) {
3382 if ( abilities
[ ability
] !== undefined ) {
3383 this.abilities
[ ability
] = !!abilities
[ ability
];
3387 this.onOutlineChange();
3391 * Handle outline change events.
3395 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
3396 var i
, len
, firstMovable
, lastMovable
,
3397 items
= this.outline
.getItems(),
3398 selectedItem
= this.outline
.findSelectedItem(),
3399 movable
= this.abilities
.move && selectedItem
&& selectedItem
.isMovable(),
3400 removable
= this.abilities
.remove
&& selectedItem
&& selectedItem
.isRemovable();
3405 while ( ++i
< len
) {
3406 if ( items
[ i
].isMovable() ) {
3407 firstMovable
= items
[ i
];
3413 if ( items
[ i
].isMovable() ) {
3414 lastMovable
= items
[ i
];
3419 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
3420 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
3421 this.removeButton
.setDisabled( !removable
);
3425 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3427 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3428 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3432 * @extends OO.ui.DecoratedOptionWidget
3435 * @param {Object} [config] Configuration options
3436 * @cfg {number} [level] Indentation level
3437 * @cfg {boolean} [movable] Allow modification from
3438 * {@link OO.ui.OutlineControlsWidget outline controls}.
3440 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
3441 // Configuration initialization
3442 config
= config
|| {};
3444 // Parent constructor
3445 OO
.ui
.OutlineOptionWidget
.parent
.call( this, config
);
3449 this.movable
= !!config
.movable
;
3450 this.removable
= !!config
.removable
;
3453 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
3454 this.setLevel( config
.level
);
3459 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3461 /* Static Properties */
3467 OO
.ui
.OutlineOptionWidget
.static.highlightable
= true;
3473 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
3478 * @property {string}
3480 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
3485 * @property {number}
3487 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
3492 * Check if item is movable.
3494 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3496 * @return {boolean} Item is movable
3498 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
3499 return this.movable
;
3503 * Check if item is removable.
3505 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3507 * @return {boolean} Item is removable
3509 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
3510 return this.removable
;
3514 * Get indentation level.
3516 * @return {number} Indentation level
3518 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
3525 OO
.ui
.OutlineOptionWidget
.prototype.setPressed = function ( state
) {
3526 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setPressed
.call( this, state
);
3533 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3535 * @param {boolean} movable Item is movable
3537 * @return {OO.ui.Widget} The widget, for chaining
3539 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
3540 this.movable
= !!movable
;
3541 this.updateThemeClasses();
3548 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3550 * @param {boolean} removable Item is removable
3552 * @return {OO.ui.Widget} The widget, for chaining
3554 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
3555 this.removable
= !!removable
;
3556 this.updateThemeClasses();
3563 OO
.ui
.OutlineOptionWidget
.prototype.setSelected = function ( state
) {
3564 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3569 * Set indentation level.
3571 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3573 * @return {OO.ui.Widget} The widget, for chaining
3575 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
3576 var levels
= this.constructor.static.levels
,
3577 levelClass
= this.constructor.static.levelClass
,
3580 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
3582 if ( this.level
=== i
) {
3583 this.$element
.addClass( levelClass
+ i
);
3585 this.$element
.removeClass( levelClass
+ i
);
3588 this.updateThemeClasses();
3594 * OutlineSelectWidget is a structured list that contains
3595 * {@link OO.ui.OutlineOptionWidget outline options}
3596 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls}
3599 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3602 * @extends OO.ui.SelectWidget
3603 * @mixins OO.ui.mixin.TabIndexedElement
3606 * @param {Object} [config] Configuration options
3608 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
3609 // Parent constructor
3610 OO
.ui
.OutlineSelectWidget
.parent
.call( this, config
);
3612 // Mixin constructors
3613 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3617 focus
: this.bindDocumentKeyDownListener
.bind( this ),
3618 blur
: this.unbindDocumentKeyDownListener
.bind( this )
3622 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
3627 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
3628 OO
.mixinClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3631 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3632 * can be selected and configured with data. The class is
3633 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3634 * [OOUI documentation on MediaWiki] [1] for more information.
3636 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options#Button_selects_and_options
3639 * @extends OO.ui.OptionWidget
3640 * @mixins OO.ui.mixin.ButtonElement
3641 * @mixins OO.ui.mixin.IconElement
3642 * @mixins OO.ui.mixin.IndicatorElement
3645 * @param {Object} [config] Configuration options
3647 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
3648 // Configuration initialization
3649 config
= config
|| {};
3651 // Parent constructor
3652 OO
.ui
.ButtonOptionWidget
.parent
.call( this, config
);
3654 // Mixin constructors
3655 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3656 OO
.ui
.mixin
.IconElement
.call( this, config
);
3657 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3660 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
3661 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3662 this.$element
.append( this.$button
);
3663 this.setTitledElement( this.$button
);
3668 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
3669 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.ButtonElement
);
3670 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IconElement
);
3671 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IndicatorElement
);
3673 /* Static Properties */
3676 * Allow button mouse down events to pass through so they can be handled by the parent select widget
3681 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
3687 OO
.ui
.ButtonOptionWidget
.static.highlightable
= false;
3694 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
3695 OO
.ui
.ButtonOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3697 if ( this.constructor.static.selectable
) {
3698 this.setActive( state
);
3705 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3706 * button options and is used together with
3707 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3708 * highlighting, choosing, and selecting mutually exclusive options. Please see
3709 * the [OOUI documentation on MediaWiki] [1] for more information.
3712 * // A ButtonSelectWidget that contains three ButtonOptionWidgets.
3713 * var option1 = new OO.ui.ButtonOptionWidget( {
3715 * label: 'Option 1',
3716 * title: 'Button option 1'
3718 * option2 = new OO.ui.ButtonOptionWidget( {
3720 * label: 'Option 2',
3721 * title: 'Button option 2'
3723 * option3 = new OO.ui.ButtonOptionWidget( {
3725 * label: 'Option 3',
3726 * title: 'Button option 3'
3728 * buttonSelect = new OO.ui.ButtonSelectWidget( {
3729 * items: [ option1, option2, option3 ]
3731 * $( document.body ).append( buttonSelect.$element );
3733 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options
3736 * @extends OO.ui.SelectWidget
3737 * @mixins OO.ui.mixin.TabIndexedElement
3740 * @param {Object} [config] Configuration options
3742 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
3743 // Parent constructor
3744 OO
.ui
.ButtonSelectWidget
.parent
.call( this, config
);
3746 // Mixin constructors
3747 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3751 focus
: this.bindDocumentKeyDownListener
.bind( this ),
3752 blur
: this.unbindDocumentKeyDownListener
.bind( this )
3756 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
3761 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
3762 OO
.mixinClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3765 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3767 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3768 * {@link OO.ui.TabPanelLayout tab panel layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3772 * @extends OO.ui.OptionWidget
3775 * @param {Object} [config] Configuration options
3777 OO
.ui
.TabOptionWidget
= function OoUiTabOptionWidget( config
) {
3778 // Configuration initialization
3779 config
= config
|| {};
3781 // Parent constructor
3782 OO
.ui
.TabOptionWidget
.parent
.call( this, config
);
3786 .addClass( 'oo-ui-tabOptionWidget' )
3787 .attr( 'role', 'tab' );
3792 OO
.inheritClass( OO
.ui
.TabOptionWidget
, OO
.ui
.OptionWidget
);
3794 /* Static Properties */
3800 OO
.ui
.TabOptionWidget
.static.highlightable
= false;
3803 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3805 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3808 * @extends OO.ui.SelectWidget
3809 * @mixins OO.ui.mixin.TabIndexedElement
3812 * @param {Object} [config] Configuration options
3813 * @cfg {boolean} [framed=true] Use framed tabs
3815 OO
.ui
.TabSelectWidget
= function OoUiTabSelectWidget( config
) {
3816 // Parent constructor
3817 OO
.ui
.TabSelectWidget
.parent
.call( this, config
);
3819 // Mixin constructors
3820 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3824 focus
: this.bindDocumentKeyDownListener
.bind( this ),
3825 blur
: this.unbindDocumentKeyDownListener
.bind( this )
3830 .addClass( 'oo-ui-tabSelectWidget' )
3831 .attr( 'role', 'tablist' );
3833 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3835 if ( OO
.ui
.isMobile() ) {
3836 this.$element
.addClass( 'oo-ui-tabSelectWidget-mobile' );
3842 OO
.inheritClass( OO
.ui
.TabSelectWidget
, OO
.ui
.SelectWidget
);
3843 OO
.mixinClass( OO
.ui
.TabSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3848 * Check if tabs are framed.
3850 * @return {boolean} Tabs are framed
3852 OO
.ui
.TabSelectWidget
.prototype.isFramed = function () {
3857 * Render the tabs with or without frames.
3859 * @param {boolean} [framed] Make tabs framed, omit to toggle
3861 * @return {OO.ui.Element} The element, for chaining
3863 OO
.ui
.TabSelectWidget
.prototype.toggleFramed = function ( framed
) {
3864 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3865 if ( framed
!== this.framed
) {
3866 this.framed
= framed
;
3868 .toggleClass( 'oo-ui-tabSelectWidget-frameless', !framed
)
3869 .toggleClass( 'oo-ui-tabSelectWidget-framed', framed
);
3876 * TagItemWidgets are used within a {@link OO.ui.TagMultiselectWidget
3877 * TagMultiselectWidget} to display the selected items.
3880 * @extends OO.ui.Widget
3881 * @mixins OO.ui.mixin.ItemWidget
3882 * @mixins OO.ui.mixin.LabelElement
3883 * @mixins OO.ui.mixin.FlaggedElement
3884 * @mixins OO.ui.mixin.TabIndexedElement
3885 * @mixins OO.ui.mixin.DraggableElement
3888 * @param {Object} [config] Configuration object
3889 * @cfg {boolean} [valid=true] Item is valid
3890 * @cfg {boolean} [fixed] Item is fixed. This means the item is
3891 * always included in the values and cannot be removed.
3893 OO
.ui
.TagItemWidget
= function OoUiTagItemWidget( config
) {
3894 config
= config
|| {};
3896 // Parent constructor
3897 OO
.ui
.TagItemWidget
.parent
.call( this, config
);
3899 // Mixin constructors
3900 OO
.ui
.mixin
.ItemWidget
.call( this );
3901 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3902 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3903 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3904 OO
.ui
.mixin
.DraggableElement
.call( this, config
);
3906 this.valid
= config
.valid
=== undefined ? true : !!config
.valid
;
3907 this.fixed
= !!config
.fixed
;
3909 this.closeButton
= new OO
.ui
.ButtonWidget( {
3913 title
: OO
.ui
.msg( 'ooui-item-remove' )
3915 this.closeButton
.setDisabled( this.isDisabled() );
3918 this.closeButton
.connect( this, {
3922 .on( 'click', this.select
.bind( this ) )
3923 .on( 'keydown', this.onKeyDown
.bind( this ) )
3924 // Prevent propagation of mousedown; the tag item "lives" in the
3925 // clickable area of the TagMultiselectWidget, which listens to
3926 // mousedown to open the menu or popup. We want to prevent that
3927 // for clicks specifically on the tag itself, so the actions taken
3928 // are more deliberate. When the tag is clicked, it will emit the
3929 // selection event (similar to how #OO.ui.MultioptionWidget emits 'change')
3930 // and can be handled separately.
3931 .on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
3935 .addClass( 'oo-ui-tagItemWidget' )
3936 .append( this.$label
, this.closeButton
.$element
);
3939 /* Initialization */
3941 OO
.inheritClass( OO
.ui
.TagItemWidget
, OO
.ui
.Widget
);
3942 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.ItemWidget
);
3943 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.LabelElement
);
3944 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
3945 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3946 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.DraggableElement
);
3953 * A remove action was performed on the item
3958 * @param {string} direction Direction of the movement, forward or backwards
3960 * A navigate action was performed on the item
3966 * The tag widget was selected. This can occur when the widget
3967 * is either clicked or enter was pressed on it.
3972 * @param {boolean} isValid Item is valid
3974 * Item validity has changed
3979 * @param {boolean} isFixed Item is fixed
3981 * Item fixed state has changed
3987 * Set this item as fixed, meaning it cannot be removed
3989 * @param {string} [state] Item is fixed
3991 * @return {OO.ui.Widget} The widget, for chaining
3993 OO
.ui
.TagItemWidget
.prototype.setFixed = function ( state
) {
3994 state
= state
=== undefined ? !this.fixed
: !!state
;
3996 if ( this.fixed
!== state
) {
3998 if ( this.closeButton
) {
3999 this.closeButton
.toggle( !this.fixed
);
4002 if ( !this.fixed
&& this.elementGroup
&& !this.elementGroup
.isDraggable() ) {
4003 // Only enable the state of the item if the
4004 // entire group is draggable
4005 this.toggleDraggable( !this.fixed
);
4007 this.$element
.toggleClass( 'oo-ui-tagItemWidget-fixed', this.fixed
);
4009 this.emit( 'fixed', this.isFixed() );
4015 * Check whether the item is fixed
4018 OO
.ui
.TagItemWidget
.prototype.isFixed = function () {
4025 OO
.ui
.TagItemWidget
.prototype.setDisabled = function ( state
) {
4026 if ( state
&& this.elementGroup
&& !this.elementGroup
.isDisabled() ) {
4027 OO
.ui
.warnDeprecation( 'TagItemWidget#setDisabled: Disabling individual items is deprecated and will result in inconsistent behavior. Use #setFixed instead. See T193571.' );
4030 OO
.ui
.TagItemWidget
.parent
.prototype.setDisabled
.call( this, state
);
4033 // Verify we have a group, and that the widget is ready
4034 this.toggleDraggable
&& this.elementGroup
&&
4036 !this.elementGroup
.isDraggable()
4038 // Only enable the draggable state of the item if the
4039 // entire group is draggable to begin with, and if the
4040 // item is not fixed
4041 this.toggleDraggable( !state
);
4044 if ( this.closeButton
) {
4045 this.closeButton
.setDisabled( state
);
4052 * Handle removal of the item
4054 * This is mainly for extensibility concerns, so other children
4055 * of this class can change the behavior if they need to. This
4056 * is called by both clicking the 'remove' button but also
4057 * on keypress, which is harder to override if needed.
4061 OO
.ui
.TagItemWidget
.prototype.remove = function () {
4062 if ( !this.isDisabled() && !this.isFixed() ) {
4063 this.emit( 'remove' );
4068 * Handle a keydown event on the widget
4072 * @param {jQuery.Event} e Key down event
4073 * @return {boolean|undefined} false to stop the operation
4075 OO
.ui
.TagItemWidget
.prototype.onKeyDown = function ( e
) {
4079 !this.isDisabled() &&
4081 ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
)
4085 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
4089 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4090 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4092 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
4106 e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
4107 movement
.left
: movement
.right
4118 OO
.ui
.TagItemWidget
.prototype.select = function () {
4119 if ( !this.isDisabled() ) {
4120 this.emit( 'select' );
4125 * Set the valid state of this item
4127 * @param {boolean} [valid] Item is valid
4130 OO
.ui
.TagItemWidget
.prototype.toggleValid = function ( valid
) {
4131 valid
= valid
=== undefined ? !this.valid
: !!valid
;
4133 if ( this.valid
!== valid
) {
4136 this.setFlags( { invalid
: !this.valid
} );
4138 this.emit( 'valid', this.valid
);
4143 * Check whether the item is valid
4145 * @return {boolean} Item is valid
4147 OO
.ui
.TagItemWidget
.prototype.isValid = function () {
4152 * A basic tag multiselect widget, similar in concept to
4153 * {@link OO.ui.ComboBoxInputWidget combo box widget} that allows the user to add multiple values
4154 * that are displayed in a tag area.
4156 * This widget is a base widget; see {@link OO.ui.MenuTagMultiselectWidget MenuTagMultiselectWidget}
4157 * and {@link OO.ui.PopupTagMultiselectWidget PopupTagMultiselectWidget} for the implementations
4158 * that use a menu and a popup respectively.
4161 * // A TagMultiselectWidget.
4162 * var widget = new OO.ui.TagMultiselectWidget( {
4163 * inputPosition: 'outline',
4164 * allowedValues: [ 'Option 1', 'Option 2', 'Option 3' ],
4165 * selected: [ 'Option 1' ]
4167 * $( document.body ).append( widget.$element );
4170 * @extends OO.ui.Widget
4171 * @mixins OO.ui.mixin.GroupWidget
4172 * @mixins OO.ui.mixin.DraggableGroupElement
4173 * @mixins OO.ui.mixin.IndicatorElement
4174 * @mixins OO.ui.mixin.IconElement
4175 * @mixins OO.ui.mixin.TabIndexedElement
4176 * @mixins OO.ui.mixin.FlaggedElement
4177 * @mixins OO.ui.mixin.TitledElement
4180 * @param {Object} config Configuration object
4181 * @cfg {Object} [input] Configuration options for the input widget
4182 * @cfg {OO.ui.InputWidget} [inputWidget] An optional input widget. If given, it will
4183 * replace the input widget used in the TagMultiselectWidget. If not given,
4184 * TagMultiselectWidget creates its own.
4185 * @cfg {boolean} [inputPosition='inline'] Position of the input. Options are:
4186 * - inline: The input is invisible, but exists inside the tag list, so
4187 * the user types into the tag groups to add tags.
4188 * - outline: The input is underneath the tag area.
4189 * - none: No input supplied
4190 * @cfg {boolean} [allowEditTags=true] Allow editing of the tags by clicking them
4191 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if
4192 * not present in the menu.
4193 * @cfg {Object[]} [allowedValues] An array representing the allowed items
4195 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added
4196 * @cfg {boolean} [allowDisplayInvalidTags=false] Allow the display of
4197 * invalid tags. These tags will display with an invalid state, and
4198 * the widget as a whole will have an invalid state if any invalid tags
4200 * @cfg {number} [tagLimit] An optional limit on the number of selected options.
4201 * If 'tagLimit' is set and is reached, the input is disabled, not allowing any
4202 * additions. If 'tagLimit' is unset or is 0, an unlimited number of items can be
4204 * @cfg {boolean} [allowReordering=true] Allow reordering of the items
4205 * @cfg {Object[]|String[]} [selected] A set of selected tags. If given,
4206 * these will appear in the tag list on initialization, as long as they
4207 * pass the validity tests.
4209 OO
.ui
.TagMultiselectWidget
= function OoUiTagMultiselectWidget( config
) {
4211 rAF
= window
.requestAnimationFrame
|| setTimeout
,
4213 $tabFocus
= $( '<span>' ).addClass( 'oo-ui-tagMultiselectWidget-focusTrap' );
4215 config
= config
|| {};
4217 // Parent constructor
4218 OO
.ui
.TagMultiselectWidget
.parent
.call( this, config
);
4220 // Mixin constructors
4221 OO
.ui
.mixin
.GroupWidget
.call( this, config
);
4222 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
4223 OO
.ui
.mixin
.IconElement
.call( this, config
);
4224 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4225 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4226 OO
.ui
.mixin
.DraggableGroupElement
.call( this, config
);
4227 OO
.ui
.mixin
.TitledElement
.call( this, config
);
4229 this.toggleDraggable(
4230 config
.allowReordering
=== undefined ?
4231 true : !!config
.allowReordering
4234 this.inputPosition
=
4235 this.constructor.static.allowedInputPositions
.indexOf( config
.inputPosition
) > -1 ?
4236 config
.inputPosition
: 'inline';
4237 this.allowEditTags
= config
.allowEditTags
=== undefined ? true : !!config
.allowEditTags
;
4238 this.allowArbitrary
= !!config
.allowArbitrary
;
4239 this.allowDuplicates
= !!config
.allowDuplicates
;
4240 this.allowedValues
= config
.allowedValues
|| [];
4241 this.allowDisplayInvalidTags
= config
.allowDisplayInvalidTags
;
4242 this.hasInput
= this.inputPosition
!== 'none';
4243 this.tagLimit
= config
.tagLimit
;
4247 this.$content
= $( '<div>' ).addClass( 'oo-ui-tagMultiselectWidget-content' );
4248 this.$handle
= $( '<div>' )
4249 .addClass( 'oo-ui-tagMultiselectWidget-handle' )
4255 this.$group
.addClass( 'oo-ui-tagMultiselectWidget-group' )
4261 remove
: 'itemRemove',
4262 navigate
: 'itemNavigate',
4263 select
: 'itemSelect',
4266 this.connect( this, {
4267 itemRemove
: 'onTagRemove',
4268 itemSelect
: 'onTagSelect',
4269 itemFixed
: 'onTagFixed',
4270 itemNavigate
: 'onTagNavigate',
4271 change
: 'onChangeTags'
4274 mousedown
: this.onMouseDown
.bind( this )
4279 .addClass( 'oo-ui-tagMultiselectWidget' )
4280 .append( this.$handle
);
4282 if ( this.hasInput
) {
4283 if ( config
.inputWidget
) {
4284 this.input
= config
.inputWidget
;
4286 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
4287 placeholder
: config
.placeholder
,
4288 classes
: [ 'oo-ui-tagMultiselectWidget-input' ]
4289 }, config
.input
) );
4291 this.input
.setDisabled( this.isDisabled() );
4294 focus
: this.onInputFocus
.bind( this ),
4295 blur
: this.onInputBlur
.bind( this ),
4296 'propertychange change click mouseup keydown keyup input cut paste select focus':
4297 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
4298 keydown
: this.onInputKeyDown
.bind( this ),
4299 keypress
: this.onInputKeyPress
.bind( this )
4302 this.input
.$input
.on( inputEvents
);
4303 this.inputPlaceholder
= this.input
.$input
.attr( 'placeholder' );
4305 if ( this.inputPosition
=== 'outline' ) {
4306 // Override max-height for the input widget
4307 // in the case the widget is outline so it can
4308 // stretch all the way if the widget is wide
4309 this.input
.$element
.css( 'max-width', 'inherit' );
4311 .addClass( 'oo-ui-tagMultiselectWidget-outlined' )
4312 .append( this.input
.$element
);
4314 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-inlined' );
4315 // HACK: When the widget is using 'inline' input, the
4316 // behavior needs to only use the $input itself
4317 // so we style and size it accordingly (otherwise
4318 // the styling and sizing can get very convoluted
4319 // when the wrapping divs and other elements)
4320 // We are taking advantage of still being able to
4321 // call the widget itself for operations like
4322 // .getValue() and setDisabled() and .focus() but
4323 // having only the $input attached to the DOM
4324 this.$content
.append( this.input
.$input
);
4327 this.$content
.append( $tabFocus
);
4330 this.setTabIndexedElement(
4336 if ( config
.selected
) {
4337 this.setValue( config
.selected
);
4340 // HACK: Input size needs to be calculated after everything
4343 if ( widget
.hasInput
) {
4344 widget
.updateInputSize();
4349 /* Initialization */
4351 OO
.inheritClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.Widget
);
4352 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.GroupWidget
);
4353 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.DraggableGroupElement
);
4354 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
4355 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
4356 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4357 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.FlaggedElement
);
4358 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.TitledElement
);
4360 /* Static properties */
4363 * Allowed input positions.
4364 * - inline: The input is inside the tag list
4365 * - outline: The input is under the tag list
4366 * - none: There is no input
4370 OO
.ui
.TagMultiselectWidget
.static.allowedInputPositions
= [ 'inline', 'outline', 'none' ];
4375 * Handle mouse down events.
4378 * @param {jQuery.Event} e Mouse down event
4379 * @return {boolean} False to prevent defaults
4381 OO
.ui
.TagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
4383 !this.isDisabled() &&
4384 ( !this.hasInput
|| e
.target
!== this.input
.$input
[ 0 ] ) &&
4385 e
.which
=== OO
.ui
.MouseButtons
.LEFT
4393 * Handle key press events.
4396 * @param {jQuery.Event} e Key press event
4397 * @return {boolean} Whether to prevent defaults
4399 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyPress = function ( e
) {
4401 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
4403 if ( !this.isDisabled() ) {
4404 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
4405 stopOrContinue
= this.doInputEnter( e
, withMetaKey
);
4408 // Make sure the input gets resized.
4409 setTimeout( this.updateInputSize
.bind( this ), 0 );
4410 return stopOrContinue
;
4415 * Handle key down events.
4418 * @param {jQuery.Event} e Key down event
4421 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyDown = function ( e
) {
4422 var movement
, direction
,
4424 withMetaKey
= e
.metaKey
|| e
.ctrlKey
,
4425 isMovementInsideInput = function ( direction
) {
4426 var inputRange
= widget
.input
.getRange(),
4427 inputValue
= widget
.hasInput
&& widget
.input
.getValue();
4429 if ( direction
=== 'forwards' && inputRange
.to
> inputValue
.length
- 1 ) {
4433 if ( direction
=== 'backwards' && inputRange
.from <= 0 ) {
4440 if ( !this.isDisabled() ) {
4441 // 'keypress' event is not triggered for Backspace key
4442 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
4443 return this.doInputBackspace( e
, withMetaKey
);
4444 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ESCAPE
) {
4445 return this.doInputEscape( e
);
4447 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4448 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4450 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
4461 direction
= e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
4462 movement
.left
: movement
.right
;
4464 if ( !this.hasInput
|| !isMovementInsideInput( direction
) ) {
4465 return this.doInputArrow( e
, direction
, withMetaKey
);
4472 * Respond to input focus event
4474 OO
.ui
.TagMultiselectWidget
.prototype.onInputFocus = function () {
4475 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-focus' );
4477 this.toggleValid( true );
4481 * Respond to input blur event
4483 OO
.ui
.TagMultiselectWidget
.prototype.onInputBlur = function () {
4484 this.$element
.removeClass( 'oo-ui-tagMultiselectWidget-focus' );
4486 // Set the widget as invalid if there's text in the input
4487 this.addTagFromInput();
4488 this.toggleValid( this.checkValidity() && ( !this.hasInput
|| !this.input
.getValue() ) );
4492 * Perform an action after the Enter key on the input
4494 * @param {jQuery.Event} e Event data
4495 * @param {boolean} [withMetaKey] Whether this key was pressed with
4496 * a meta key like Control
4497 * @return {boolean} Whether to prevent defaults
4499 OO
.ui
.TagMultiselectWidget
.prototype.doInputEnter = function () {
4500 this.addTagFromInput();
4505 * Perform an action responding to the Enter key on the input
4507 * @param {jQuery.Event} e Event data
4508 * @param {boolean} [withMetaKey] Whether this key was pressed with
4509 * a meta key like Control
4510 * @return {boolean} Whether to prevent defaults
4512 OO
.ui
.TagMultiselectWidget
.prototype.doInputBackspace = function ( e
, withMetaKey
) {
4516 this.inputPosition
=== 'inline' &&
4517 this.input
.getValue() === '' &&
4520 // Delete the last item
4521 items
= this.getItems();
4522 item
= items
[ items
.length
- 1 ];
4524 if ( !item
.isDisabled() && !item
.isFixed() ) {
4525 this.removeItems( [ item
] );
4526 // If Ctrl/Cmd was pressed, delete item entirely.
4527 // Otherwise put it into the text field for editing.
4528 if ( !withMetaKey
) {
4529 this.input
.setValue( item
.getLabel() );
4538 * Perform an action after the Escape key on the input
4540 * @param {jQuery.Event} e Event data
4542 OO
.ui
.TagMultiselectWidget
.prototype.doInputEscape = function () {
4547 * Perform an action after the Left/Right arrow key on the input, select the previous
4548 * item from the input.
4549 * See #getPreviousItem
4551 * @param {jQuery.Event} e Event data
4552 * @param {string} direction Direction of the movement; forwards or backwards
4553 * @param {boolean} [withMetaKey] Whether this key was pressed with
4554 * a meta key like Control
4556 OO
.ui
.TagMultiselectWidget
.prototype.doInputArrow = function ( e
, direction
) {
4558 this.inputPosition
=== 'inline' &&
4560 direction
=== 'backwards'
4562 // Get previous item
4563 this.getPreviousItem().focus();
4568 * Respond to item select event
4570 * @param {OO.ui.TagItemWidget} item Selected item
4572 OO
.ui
.TagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
4573 if ( this.hasInput
&& this.allowEditTags
&& !item
.isFixed() ) {
4574 if ( this.input
.getValue() ) {
4575 this.addTagFromInput();
4577 // 1. Get the label of the tag into the input
4578 this.input
.setValue( item
.getLabel() );
4579 // 2. Remove the tag
4580 this.removeItems( [ item
] );
4581 // 3. Focus the input
4587 * Respond to item fixed state change
4589 * @param {OO.ui.TagItemWidget} item Selected item
4591 OO
.ui
.TagMultiselectWidget
.prototype.onTagFixed = function ( item
) {
4593 items
= this.getItems();
4595 // Move item to the end of the static items
4596 for ( i
= 0; i
< items
.length
; i
++ ) {
4597 if ( items
[ i
] !== item
&& !items
[ i
].isFixed() ) {
4601 this.addItems( item
, i
);
4604 * Respond to change event, where items were added, removed, or cleared.
4606 OO
.ui
.TagMultiselectWidget
.prototype.onChangeTags = function () {
4607 var isUnderLimit
= this.isUnderLimit();
4611 this.checkValidity() &&
4612 !( this.hasInput
&& this.input
.getValue() )
4615 if ( this.hasInput
) {
4616 this.updateInputSize();
4617 if ( !isUnderLimit
) {
4619 this.input
.setValue( '' );
4621 if ( this.inputPosition
=== 'outline' ) {
4622 // Show/clear the placeholder and enable/disable the input
4623 // based on whether we are/aren't under the specified limit
4624 this.input
.$input
.attr( 'placeholder', isUnderLimit
? this.inputPlaceholder
: '' );
4625 this.input
.setDisabled( !isUnderLimit
);
4627 // Show/hide the input
4628 this.input
.$input
.toggleClass( 'oo-ui-element-hidden', !isUnderLimit
);
4631 this.updateIfHeightChanged();
4637 OO
.ui
.TagMultiselectWidget
.prototype.setDisabled = function ( isDisabled
) {
4639 OO
.ui
.TagMultiselectWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
4641 if ( this.hasInput
&& this.input
) {
4642 if ( !isDisabled
) {
4643 this.updateInputSize();
4645 this.input
.setDisabled( !!isDisabled
&& !this.isUnderLimit() );
4649 this.getItems().forEach( function ( item
) {
4650 item
.setDisabled( !!isDisabled
);
4656 * Respond to tag remove event
4657 * @param {OO.ui.TagItemWidget} item Removed tag
4659 OO
.ui
.TagMultiselectWidget
.prototype.onTagRemove = function ( item
) {
4660 this.removeTagByData( item
.getData() );
4664 * Respond to navigate event on the tag
4666 * @param {OO.ui.TagItemWidget} item Removed tag
4667 * @param {string} direction Direction of movement; 'forwards' or 'backwards'
4669 OO
.ui
.TagMultiselectWidget
.prototype.onTagNavigate = function ( item
, direction
) {
4670 var firstItem
= this.getItems()[ 0 ];
4672 if ( direction
=== 'forwards' ) {
4673 this.getNextItem( item
).focus();
4674 } else if ( !this.inputPosition
=== 'inline' || item
!== firstItem
) {
4675 // If the widget has an inline input, we want to stop at the starting edge
4677 this.getPreviousItem( item
).focus();
4682 * Add tag from input value
4684 OO
.ui
.TagMultiselectWidget
.prototype.addTagFromInput = function () {
4685 var val
= this.input
.getValue(),
4686 isValid
= this.isAllowedData( val
);
4692 if ( isValid
|| this.allowDisplayInvalidTags
) {
4701 OO
.ui
.TagMultiselectWidget
.prototype.clearInput = function () {
4702 this.input
.setValue( '' );
4706 * Check whether the given value is a duplicate of an existing
4707 * tag already in the list.
4709 * @param {string|Object} data Requested value
4710 * @return {boolean} Value is duplicate
4712 OO
.ui
.TagMultiselectWidget
.prototype.isDuplicateData = function ( data
) {
4713 return !!this.findItemFromData( data
);
4717 * Check whether a given value is allowed to be added
4719 * @param {string|Object} data Requested value
4720 * @return {boolean} Value is allowed
4722 OO
.ui
.TagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
4724 !this.allowDuplicates
&&
4725 this.isDuplicateData( data
)
4730 if ( this.allowArbitrary
) {
4734 // Check with allowed values
4736 this.getAllowedValues().some( function ( value
) {
4737 return data
=== value
;
4747 * Get the allowed values list
4749 * @return {string[]} Allowed data values
4751 OO
.ui
.TagMultiselectWidget
.prototype.getAllowedValues = function () {
4752 return this.allowedValues
;
4756 * Add a value to the allowed values list
4758 * @param {string} value Allowed data value
4760 OO
.ui
.TagMultiselectWidget
.prototype.addAllowedValue = function ( value
) {
4761 if ( this.allowedValues
.indexOf( value
) === -1 ) {
4762 this.allowedValues
.push( value
);
4767 * Get the datas of the currently selected items
4769 * @return {string[]|Object[]} Datas of currently selected items
4771 OO
.ui
.TagMultiselectWidget
.prototype.getValue = function () {
4772 return this.getItems()
4773 .filter( function ( item
) {
4774 return item
.isValid();
4776 .map( function ( item
) {
4777 return item
.getData();
4782 * Set the value of this widget by datas.
4784 * @param {string|string[]|Object|Object[]} valueObject An object representing the data
4785 * and label of the value. If the widget allows arbitrary values,
4786 * the items will be added as-is. Otherwise, the data value will
4787 * be checked against allowedValues.
4788 * This object must contain at least a data key. Example:
4789 * { data: 'foo', label: 'Foo item' }
4790 * For multiple items, use an array of objects. For example:
4792 * { data: 'foo', label: 'Foo item' },
4793 * { data: 'bar', label: 'Bar item' }
4795 * Value can also be added with plaintext array, for example:
4796 * [ 'foo', 'bar', 'bla' ] or a single string, like 'foo'
4798 OO
.ui
.TagMultiselectWidget
.prototype.setValue = function ( valueObject
) {
4799 valueObject
= Array
.isArray( valueObject
) ? valueObject
: [ valueObject
];
4802 valueObject
.forEach( function ( obj
) {
4803 if ( typeof obj
=== 'string' ) {
4806 this.addTag( obj
.data
, obj
.label
);
4812 * Add tag to the display area
4814 * @param {string|Object} data Tag data
4815 * @param {string} [label] Tag label. If no label is provided, the
4816 * stringified version of the data will be used instead.
4817 * @return {boolean} Item was added successfully
4819 OO
.ui
.TagMultiselectWidget
.prototype.addTag = function ( data
, label
) {
4821 isValid
= this.isAllowedData( data
);
4823 if ( this.isUnderLimit() && ( isValid
|| this.allowDisplayInvalidTags
) ) {
4824 newItemWidget
= this.createTagItemWidget( data
, label
);
4825 newItemWidget
.toggleValid( isValid
);
4826 this.addItems( [ newItemWidget
] );
4834 * Check whether the number of current tags is within the limit.
4836 * @return {boolean} True if current tag count is within the limit or
4837 * if 'tagLimit' is not set
4839 OO
.ui
.TagMultiselectWidget
.prototype.isUnderLimit = function () {
4840 return !this.tagLimit
||
4841 this.getItemCount() < this.tagLimit
;
4845 * Remove tag by its data property.
4847 * @param {string|Object} data Tag data
4849 OO
.ui
.TagMultiselectWidget
.prototype.removeTagByData = function ( data
) {
4850 var item
= this.findItemFromData( data
);
4852 this.removeItems( [ item
] );
4856 * Construct a OO.ui.TagItemWidget (or a subclass thereof) from given label and data.
4859 * @param {string} data Item data
4860 * @param {string} label The label text.
4861 * @return {OO.ui.TagItemWidget}
4863 OO
.ui
.TagMultiselectWidget
.prototype.createTagItemWidget = function ( data
, label
) {
4864 label
= label
|| data
;
4866 return new OO
.ui
.TagItemWidget( { data
: data
, label
: label
} );
4870 * Given an item, returns the item after it. If the item is already the
4871 * last item, return `this.input`. If no item is passed, returns the
4875 * @param {OO.ui.TagItemWidget} [item] Tag item
4876 * @return {OO.ui.Widget} The next widget available.
4878 OO
.ui
.TagMultiselectWidget
.prototype.getNextItem = function ( item
) {
4879 var itemIndex
= this.items
.indexOf( item
);
4881 if ( item
=== undefined || itemIndex
=== -1 ) {
4882 return this.items
[ 0 ];
4885 if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
4886 if ( this.hasInput
) {
4889 // Return first item
4890 return this.items
[ 0 ];
4893 return this.items
[ itemIndex
+ 1 ];
4898 * Given an item, returns the item before it. If the item is already the
4899 * first item, return `this.input`. If no item is passed, returns the
4903 * @param {OO.ui.TagItemWidget} [item] Tag item
4904 * @return {OO.ui.Widget} The previous widget available.
4906 OO
.ui
.TagMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
4907 var itemIndex
= this.items
.indexOf( item
);
4909 if ( item
=== undefined || itemIndex
=== -1 ) {
4910 return this.items
[ this.items
.length
- 1 ];
4913 if ( itemIndex
=== 0 ) {
4914 if ( this.hasInput
) {
4917 // Return the last item
4918 return this.items
[ this.items
.length
- 1 ];
4921 return this.items
[ itemIndex
- 1 ];
4926 * Update the dimensions of the text input field to encompass all available area.
4927 * This is especially relevant for when the input is at the edge of a line
4928 * and should get smaller. The usual operation (as an inline-block with min-width)
4929 * does not work in that case, pushing the input downwards to the next line.
4933 OO
.ui
.TagMultiselectWidget
.prototype.updateInputSize = function () {
4934 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
4935 if ( this.inputPosition
=== 'inline' && !this.isDisabled() ) {
4936 if ( this.input
.$input
[ 0 ].scrollWidth
=== 0 ) {
4937 // Input appears to be attached but not visible.
4938 // Don't attempt to adjust its size, because our measurements
4939 // are going to fail anyway.
4942 this.input
.$input
.css( 'width', '1em' );
4943 $lastItem
= this.$group
.children().last();
4944 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
4946 // Get the width of the input with the placeholder text as
4947 // the value and save it so that we don't keep recalculating
4949 this.contentWidthWithPlaceholder
=== undefined &&
4950 this.input
.getValue() === '' &&
4951 this.input
.$input
.attr( 'placeholder' ) !== undefined
4953 this.input
.setValue( this.input
.$input
.attr( 'placeholder' ) );
4954 this.contentWidthWithPlaceholder
= this.input
.$input
[ 0 ].scrollWidth
;
4955 this.input
.setValue( '' );
4959 // Always keep the input wide enough for the placeholder text
4960 contentWidth
= Math
.max(
4961 this.input
.$input
[ 0 ].scrollWidth
,
4962 // undefined arguments in Math.max lead to NaN
4963 ( this.contentWidthWithPlaceholder
=== undefined ) ?
4964 0 : this.contentWidthWithPlaceholder
4966 currentWidth
= this.input
.$input
.width();
4968 if ( contentWidth
< currentWidth
) {
4969 this.updateIfHeightChanged();
4970 // All is fine, don't perform expensive calculations
4974 if ( $lastItem
.length
=== 0 ) {
4975 bestWidth
= this.$content
.innerWidth();
4977 bestWidth
= direction
=== 'ltr' ?
4978 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
4979 $lastItem
.position().left
;
4982 // Some safety margin for sanity, because I *really* don't feel like finding out where the
4983 // few pixels this is off by are coming from.
4985 if ( contentWidth
> bestWidth
) {
4986 // This will result in the input getting shifted to the next line
4987 bestWidth
= this.$content
.innerWidth() - 13;
4989 this.input
.$input
.width( Math
.floor( bestWidth
) );
4990 this.updateIfHeightChanged();
4992 this.updateIfHeightChanged();
4997 * Determine if widget height changed, and if so,
4998 * emit the resize event. This is useful for when there are either
4999 * menus or popups attached to the bottom of the widget, to allow
5000 * them to change their positioning in case the widget moved down
5005 OO
.ui
.TagMultiselectWidget
.prototype.updateIfHeightChanged = function () {
5006 var height
= this.$element
.height();
5007 if ( height
!== this.height
) {
5008 this.height
= height
;
5009 this.emit( 'resize' );
5014 * Check whether all items in the widget are valid
5016 * @return {boolean} Widget is valid
5018 OO
.ui
.TagMultiselectWidget
.prototype.checkValidity = function () {
5019 return this.getItems().every( function ( item
) {
5020 return item
.isValid();
5025 * Set the valid state of this item
5027 * @param {boolean} [valid] Item is valid
5030 OO
.ui
.TagMultiselectWidget
.prototype.toggleValid = function ( valid
) {
5031 valid
= valid
=== undefined ? !this.valid
: !!valid
;
5033 if ( this.valid
!== valid
) {
5036 this.setFlags( { invalid
: !this.valid
} );
5038 this.emit( 'valid', this.valid
);
5043 * Get the current valid state of the widget
5045 * @return {boolean} Widget is valid
5047 OO
.ui
.TagMultiselectWidget
.prototype.isValid = function () {
5052 * PopupTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget}
5053 * intended to use a popup. The popup can be configured to have a default input to insert values
5057 * // A PopupTagMultiselectWidget.
5058 * var widget = new OO.ui.PopupTagMultiselectWidget();
5059 * $( document.body ).append( widget.$element );
5061 * // Example: A PopupTagMultiselectWidget with an external popup.
5062 * var popupInput = new OO.ui.TextInputWidget(),
5063 * widget = new OO.ui.PopupTagMultiselectWidget( {
5064 * popupInput: popupInput,
5066 * $content: popupInput.$element
5069 * $( document.body ).append( widget.$element );
5072 * @extends OO.ui.TagMultiselectWidget
5073 * @mixins OO.ui.mixin.PopupElement
5075 * @param {Object} config Configuration object
5076 * @cfg {jQuery} [$overlay] An overlay for the popup.
5077 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
5078 * @cfg {Object} [popup] Configuration options for the popup
5079 * @cfg {OO.ui.InputWidget} [popupInput] An input widget inside the popup that will be
5080 * focused when the popup is opened and will be used as replacement for the
5081 * general input in the widget.
5084 OO
.ui
.PopupTagMultiselectWidget
= function OoUiPopupTagMultiselectWidget( config
) {
5086 defaultConfig
= { popup
: {} };
5088 config
= config
|| {};
5090 // Parent constructor
5091 OO
.ui
.PopupTagMultiselectWidget
.parent
.call( this, $.extend( {
5092 inputPosition
: 'none'
5095 this.$overlay
= ( config
.$overlay
=== true ?
5096 OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
5098 if ( !config
.popup
) {
5099 // For the default base implementation, we give a popup
5100 // with an input widget inside it. For any other use cases
5101 // the popup needs to be populated externally and the
5102 // event handled to add tags separately and manually
5103 defaultInput
= new OO
.ui
.TextInputWidget();
5105 defaultConfig
.popupInput
= defaultInput
;
5106 defaultConfig
.popup
.$content
= defaultInput
.$element
;
5107 defaultConfig
.popup
.padded
= true;
5109 this.$element
.addClass( 'oo-ui-popupTagMultiselectWidget-defaultPopup' );
5112 // Add overlay, and add that to the autoCloseIgnore
5113 defaultConfig
.popup
.$overlay
= this.$overlay
;
5114 defaultConfig
.popup
.$autoCloseIgnore
= this.hasInput
?
5115 this.input
.$element
.add( this.$overlay
) : this.$overlay
;
5117 // Allow extending any of the above
5118 config
= $.extend( defaultConfig
, config
);
5120 // Mixin constructors
5121 OO
.ui
.mixin
.PopupElement
.call( this, config
);
5123 if ( this.hasInput
) {
5124 this.input
.$input
.on( 'focus', this.popup
.toggle
.bind( this.popup
, true ) );
5127 // Configuration options
5128 this.popupInput
= config
.popupInput
;
5129 if ( this.popupInput
) {
5130 this.popupInput
.connect( this, {
5131 enter
: 'onPopupInputEnter'
5136 this.on( 'resize', this.popup
.updateDimensions
.bind( this.popup
) );
5137 this.popup
.connect( this, {
5138 toggle
: 'onPopupToggle'
5140 this.$tabIndexed
.on( 'focus', this.onFocus
.bind( this ) );
5144 .append( this.popup
.$element
)
5145 .addClass( 'oo-ui-popupTagMultiselectWidget' );
5147 // Deprecation warning
5148 OO
.ui
.warnDeprecation( 'PopupTagMultiselectWidget: Deprecated widget. Use MenuTagMultiselectWidget instead. See T208821.' );
5151 /* Initialization */
5153 OO
.inheritClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5154 OO
.mixinClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
5159 * Focus event handler.
5163 OO
.ui
.PopupTagMultiselectWidget
.prototype.onFocus = function () {
5164 this.popup
.toggle( true );
5168 * Respond to popup toggle event
5170 * @param {boolean} isVisible Popup is visible
5172 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupToggle = function ( isVisible
) {
5173 if ( isVisible
&& this.popupInput
) {
5174 this.popupInput
.focus();
5179 * Respond to popup input enter event
5181 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupInputEnter = function () {
5182 if ( this.popupInput
) {
5183 this.addTagByPopupValue( this.popupInput
.getValue() );
5184 this.popupInput
.setValue( '' );
5191 OO
.ui
.PopupTagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5192 if ( this.popupInput
&& this.allowEditTags
) {
5193 this.popupInput
.setValue( item
.getData() );
5194 this.removeItems( [ item
] );
5196 this.popup
.toggle( true );
5197 this.popupInput
.focus();
5200 OO
.ui
.PopupTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, item
);
5205 * Add a tag by the popup value.
5206 * Whatever is responsible for setting the value in the popup should call
5207 * this method to add a tag, or use the regular methods like #addTag or
5208 * #setValue directly.
5210 * @param {string} data The value of item
5211 * @param {string} [label] The label of the tag. If not given, the data is used.
5213 OO
.ui
.PopupTagMultiselectWidget
.prototype.addTagByPopupValue = function ( data
, label
) {
5214 this.addTag( data
, label
);
5218 * MenuTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget}
5219 * intended to use a menu of selectable options.
5222 * // A basic MenuTagMultiselectWidget.
5223 * var widget = new OO.ui.MenuTagMultiselectWidget( {
5224 * inputPosition: 'outline',
5226 * { data: 'option1', label: 'Option 1', icon: 'tag' },
5227 * { data: 'option2', label: 'Option 2' },
5228 * { data: 'option3', label: 'Option 3' },
5230 * selected: [ 'option1', 'option2' ]
5232 * $( document.body ).append( widget.$element );
5235 * @extends OO.ui.TagMultiselectWidget
5238 * @param {Object} [config] Configuration object
5239 * @cfg {boolean} [clearInputOnChoose=true] Clear the text input value when a menu option is chosen
5240 * @cfg {Object} [menu] Configuration object for the menu widget
5241 * @cfg {jQuery} [$overlay] An overlay for the menu.
5242 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
5243 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
5245 OO
.ui
.MenuTagMultiselectWidget
= function OoUiMenuTagMultiselectWidget( config
) {
5246 var $autoCloseIgnore
= $( [] );
5247 config
= config
|| {};
5249 // Parent constructor
5250 OO
.ui
.MenuTagMultiselectWidget
.parent
.call( this, config
);
5252 $autoCloseIgnore
= $autoCloseIgnore
.add( this.$group
);
5253 if ( this.hasInput
) {
5254 $autoCloseIgnore
= $autoCloseIgnore
.add( this.input
.$element
);
5257 this.$overlay
= ( config
.$overlay
=== true ?
5258 OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
5259 this.clearInputOnChoose
= config
.clearInputOnChoose
=== undefined ||
5260 !!config
.clearInputOnChoose
;
5261 this.menu
= this.createMenuWidget( $.extend( {
5263 hideOnChoose
: false,
5264 input
: this.hasInput
? this.input
: null,
5265 $input
: this.hasInput
? this.input
.$input
: null,
5266 filterFromInput
: !!this.hasInput
,
5267 highlightOnFilter
: !this.allowArbitrary
,
5269 $autoCloseIgnore
: $autoCloseIgnore
,
5270 $floatableContainer
: this.hasInput
&& this.inputPosition
=== 'outline' ?
5271 this.input
.$element
: this.$element
,
5272 $overlay
: this.$overlay
,
5273 disabled
: this.isDisabled()
5275 this.addOptions( config
.options
|| [] );
5278 this.menu
.connect( this, {
5279 choose
: 'onMenuChoose',
5280 toggle
: 'onMenuToggle'
5282 if ( this.hasInput
) {
5283 this.input
.connect( this, {
5284 change
: 'onInputChange'
5287 this.connect( this, {
5292 this.$overlay
.append( this.menu
.$element
);
5293 this.$element
.addClass( 'oo-ui-menuTagMultiselectWidget' );
5294 // Remove MenuSelectWidget's generic focus owner ARIA attribute
5295 // TODO: Should this widget have a `role` that is compatible with this attribute?
5296 this.menu
.$focusOwner
.removeAttr( 'aria-expanded' );
5297 // TagMultiselectWidget already does this, but it doesn't work right because this.menu is
5298 // not yet set up while the parent constructor runs, and #getAllowedValues rejects everything.
5299 if ( config
.selected
) {
5300 this.setValue( config
.selected
);
5304 /* Initialization */
5306 OO
.inheritClass( OO
.ui
.MenuTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5311 * Respond to resize event
5313 OO
.ui
.MenuTagMultiselectWidget
.prototype.onResize = function () {
5314 // Reposition the menu
5315 this.menu
.position();
5321 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputFocus = function () {
5323 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
5325 this.menu
.toggle( true );
5329 * Respond to input change event
5331 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputChange = function () {
5332 this.menu
.toggle( true );
5336 * Respond to menu choose event, which is intentional by the user.
5338 * @param {OO.ui.OptionWidget} menuItem Selected menu items
5339 * @param {boolean} selected Item is selected
5341 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuChoose = function ( menuItem
, selected
) {
5342 if ( this.hasInput
&& this.clearInputOnChoose
) {
5343 this.input
.setValue( '' );
5346 if ( selected
&& !this.findItemFromData( menuItem
.getData() ) ) {
5347 // The menu item is selected, add it to the tags
5348 this.addTag( menuItem
.getData(), menuItem
.getLabel() );
5350 // The menu item was unselected, remove the tag
5351 this.removeTagByData( menuItem
.getData() );
5356 * Respond to menu toggle event. Reset item highlights on hide.
5358 * @param {boolean} isVisible The menu is visible
5360 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
5362 this.menu
.highlightItem( null );
5363 this.menu
.scrollToTop();
5365 setTimeout( function () {
5366 // Remove MenuSelectWidget's generic focus owner ARIA attribute
5367 // TODO: Should this widget have a `role` that is compatible with this attribute?
5368 this.menu
.$focusOwner
.removeAttr( 'aria-expanded' );
5375 OO
.ui
.MenuTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
5376 var menuItem
= this.menu
.findItemFromData( tagItem
.getData() );
5377 if ( !this.allowArbitrary
) {
5378 // Override the base behavior from TagMultiselectWidget; the base behavior
5379 // in TagMultiselectWidget is to remove the tag to edit it in the input,
5380 // but in our case, we want to utilize the menu selection behavior, and
5381 // definitely not remove the item.
5383 // If there is an input that is used for filtering, erase the value so we don't filter
5384 if ( this.hasInput
&& this.menu
.filterFromInput
) {
5385 this.input
.setValue( '' );
5390 // Highlight the menu item
5391 this.menu
.highlightItem( menuItem
);
5392 this.menu
.scrollItemIntoView( menuItem
);
5396 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
5403 OO
.ui
.MenuTagMultiselectWidget
.prototype.removeItems = function ( items
) {
5407 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.removeItems
.call( this, items
);
5409 items
.forEach( function ( tagItem
) {
5410 var menuItem
= widget
.menu
.findItemFromData( tagItem
.getData() );
5412 // Synchronize the menu selection - unselect the removed tag
5413 widget
.menu
.unselectItem( menuItem
);
5421 OO
.ui
.MenuTagMultiselectWidget
.prototype.setValue = function ( valueObject
) {
5422 valueObject
= Array
.isArray( valueObject
) ? valueObject
: [ valueObject
];
5424 // We override this method from the parent, to make sure we are adding proper
5425 // menu items, and are accounting for cases where we have this widget with
5426 // a menu but also 'allowArbitrary'
5432 valueObject
.forEach( function ( obj
) {
5433 var data
, label
, menuItem
;
5435 if ( typeof obj
=== 'string' ) {
5442 // Check if the item is in the menu
5443 menuItem
= this.menu
.getItemFromLabel( label
) || this.menu
.findItemFromData( data
);
5445 // Menu item found, add the menu item
5446 this.addTag( menuItem
.getData(), menuItem
.getLabel() );
5447 // Make sure that item is also selected
5448 this.menu
.selectItem( menuItem
);
5449 } else if ( this.allowArbitrary
) {
5450 // If the item isn't in the menu, only add it if we
5451 // allow for arbitrary values
5452 this.addTag( data
, label
);
5460 OO
.ui
.MenuTagMultiselectWidget
.prototype.setDisabled = function ( isDisabled
) {
5462 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
5465 // Protect against calling setDisabled() before the menu was initialized
5466 this.menu
.setDisabled( isDisabled
);
5471 * Highlight the first selectable item in the menu, if configured.
5476 OO
.ui
.MenuTagMultiselectWidget
.prototype.initializeMenuSelection = function () {
5477 var highlightedItem
;
5478 this.menu
.highlightItem(
5479 this.allowArbitrary
?
5481 this.menu
.findFirstSelectableItem()
5484 highlightedItem
= this.menu
.findHighlightedItem();
5485 // Scroll to the highlighted item, if it exists
5486 if ( highlightedItem
) {
5487 this.menu
.scrollItemIntoView( highlightedItem
);
5494 OO
.ui
.MenuTagMultiselectWidget
.prototype.addTagFromInput = function () {
5495 var val
= this.input
.getValue(),
5496 // Look for a highlighted item first
5497 // Then look for the element that fits the data
5498 item
= this.menu
.findHighlightedItem() || this.menu
.findItemFromData( val
),
5499 data
= item
? item
.getData() : val
,
5500 isValid
= this.isAllowedData( data
);
5502 // Override the parent method so we add from the menu
5503 // rather than directly from the input
5509 if ( isValid
|| this.allowDisplayInvalidTags
) {
5512 this.addTag( data
, item
.getLabel() );
5520 * Return the visible items in the menu. This is mainly used for when
5521 * the menu is filtering results.
5523 * @return {OO.ui.MenuOptionWidget[]} Visible results
5525 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenuVisibleItems = function () {
5526 return this.menu
.getItems().filter( function ( menuItem
) {
5527 return menuItem
.isVisible();
5532 * Create the menu for this widget. This is in a separate method so that
5533 * child classes can override this without polluting the constructor with
5534 * unnecessary extra objects that will be overidden.
5536 * @param {Object} menuConfig Configuration options
5537 * @return {OO.ui.MenuSelectWidget} Menu widget
5539 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
5540 return new OO
.ui
.MenuSelectWidget( menuConfig
);
5544 * Add options to the menu
5546 * @param {Object[]} menuOptions Object defining options
5548 OO
.ui
.MenuTagMultiselectWidget
.prototype.addOptions = function ( menuOptions
) {
5550 items
= menuOptions
.map( function ( obj
) {
5551 return widget
.createMenuOptionWidget( obj
.data
, obj
.label
, obj
.icon
);
5554 this.menu
.addItems( items
);
5558 * Create a menu option widget.
5560 * @param {string} data Item data
5561 * @param {string} [label] Item label
5562 * @param {string} [icon] Symbolic icon name
5563 * @return {OO.ui.OptionWidget} Option widget
5565 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuOptionWidget = function ( data
, label
, icon
) {
5566 return new OO
.ui
.MenuOptionWidget( {
5568 label
: label
|| data
,
5576 * @return {OO.ui.MenuSelectWidget} Menu
5578 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenu = function () {
5583 * Get the allowed values list
5585 * @return {string[]} Allowed data values
5587 OO
.ui
.MenuTagMultiselectWidget
.prototype.getAllowedValues = function () {
5590 // If the parent constructor is calling us, we're not ready yet, this.menu is not set up.
5591 menuDatas
= this.menu
.getItems().map( function ( menuItem
) {
5592 return menuItem
.getData();
5595 return this.allowedValues
.concat( menuDatas
);
5599 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
5600 * widgets can be configured with {@link OO.ui.mixin.IconElement icons}, {@link
5601 * OO.ui.mixin.IndicatorElement indicators} and {@link OO.ui.mixin.TitledElement titles}.
5602 * Please see the [OOUI documentation on MediaWiki] [1] for more information and examples.
5604 * Although SelectFileWidget inherits from SelectFileInputWidget, it does not
5605 * behave as an InputWidget, and can't be used in HTML forms.
5608 * // A file select widget.
5609 * var selectFile = new OO.ui.SelectFileWidget();
5610 * $( document.body ).append( selectFile.$element );
5612 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets
5615 * @extends OO.ui.SelectFileInputWidget
5616 * @mixins OO.ui.mixin.PendingElement
5619 * @param {Object} [config] Configuration options
5620 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
5621 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
5622 * @cfg {boolean} [buttonOnly=false] Show only the select file button, no info field. Requires
5623 * showDropTarget to be false.
5624 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be
5625 * true. Not yet supported in multiple file mode.
5626 * @cfg {number} [thumbnailSizeLimit=20] File size limit in MiB above which to not try and show a
5627 * preview (for performance).
5629 OO
.ui
.SelectFileWidget
= function OoUiSelectFileWidget( config
) {
5630 var dragHandler
, droppable
,
5631 isSupported
= this.constructor.static.isSupported();
5633 // Configuration initialization
5634 config
= $.extend( {
5635 notsupported
: OO
.ui
.msg( 'ooui-selectfile-not-supported' ),
5638 showDropTarget
: false,
5639 thumbnailSizeLimit
: 20
5642 if ( !isSupported
) {
5643 config
.disabled
= true;
5646 // Parent constructor
5647 OO
.ui
.SelectFileWidget
.parent
.call( this, config
);
5649 // Mixin constructors
5650 OO
.ui
.mixin
.PendingElement
.call( this );
5652 if ( !isSupported
) {
5653 this.info
.setValue( config
.notsupported
);
5657 droppable
= config
.droppable
&& isSupported
;
5658 // TODO: Support drop target when multiple is set
5659 this.showDropTarget
= droppable
&& config
.showDropTarget
&& !this.multiple
;
5660 this.thumbnailSizeLimit
= config
.thumbnailSizeLimit
;
5664 dragHandler
= this.onDragEnterOrOver
.bind( this );
5666 dragenter
: dragHandler
,
5667 dragover
: dragHandler
,
5668 dragleave
: this.onDragLeave
.bind( this ),
5669 drop
: this.onDrop
.bind( this )
5674 if ( this.showDropTarget
) {
5675 this.selectButton
.setIcon( 'upload' );
5676 this.$thumbnail
= $( '<div>' ).addClass( 'oo-ui-selectFileWidget-thumbnail' );
5677 this.setPendingElement( this.$thumbnail
);
5679 .addClass( 'oo-ui-selectFileWidget-dropTarget' )
5681 click
: this.onDropTargetClick
.bind( this )
5686 this.selectButton
.$element
,
5688 .addClass( 'oo-ui-selectFileWidget-dropLabel' )
5689 .text( OO
.ui
.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
5691 this.fieldLayout
.$element
.remove();
5692 } else if ( config
.buttonOnly
) {
5693 this.$element
.append( this.selectButton
.$element
);
5694 this.fieldLayout
.$element
.remove();
5698 .on( 'click', function ( e
) {
5699 // Prevents dropTarget to get clicked which calls
5700 // a click on this input
5701 e
.stopPropagation();
5704 this.$element
.addClass( 'oo-ui-selectFileWidget' );
5711 OO
.inheritClass( OO
.ui
.SelectFileWidget
, OO
.ui
.SelectFileInputWidget
);
5712 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.PendingElement
);
5714 /* Static Properties */
5717 * Check if this widget is supported
5722 OO
.ui
.SelectFileWidget
.static.isSupported = function () {
5724 if ( OO
.ui
.SelectFileWidget
.static.isSupportedCache
=== null ) {
5725 $input
= $( '<input>' ).attr( 'type', 'file' );
5726 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= $input
[ 0 ].files
!== undefined;
5728 return OO
.ui
.SelectFileWidget
.static.isSupportedCache
;
5731 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= null;
5738 * A change event is emitted when the on/off state of the toggle changes.
5740 * @param {File|null} value New value
5746 * Get the current value of the field
5748 * For single file widgets returns a File or null.
5749 * For multiple file widgets returns a list of Files.
5751 * @return {File|File[]|null}
5753 OO
.ui
.SelectFileWidget
.prototype.getValue = function () {
5754 return this.multiple
? this.currentFiles
: this.currentFiles
[ 0 ];
5758 * Set the current value of the field
5760 * @param {File[]|null} files Files to select
5762 OO
.ui
.SelectFileWidget
.prototype.setValue = function ( files
) {
5763 if ( files
&& !this.multiple
) {
5764 files
= files
.slice( 0, 1 );
5767 function comparableFile( file
) {
5768 // Use extend to convert to plain objects so they can be compared.
5769 return $.extend( {}, file
);
5773 files
&& files
.map( comparableFile
),
5774 this.currentFiles
&& this.currentFiles
.map( comparableFile
)
5776 this.currentFiles
= files
|| [];
5777 this.emit( 'change', this.currentFiles
);
5784 OO
.ui
.SelectFileWidget
.prototype.getFilename = function () {
5785 return this.currentFiles
.map( function ( file
) {
5791 * Disable InputWidget#onEdit listener, onFileSelected is used instead.
5794 OO
.ui
.SelectFileWidget
.prototype.onEdit = function () {};
5799 OO
.ui
.SelectFileWidget
.prototype.updateUI = function () {
5800 // Too early, or not supported
5801 if ( !this.selectButton
|| !this.constructor.static.isSupported() ) {
5806 OO
.ui
.SelectFileWidget
.super.prototype.updateUI
.call( this );
5808 if ( this.currentFiles
.length
) {
5809 this.$element
.removeClass( 'oo-ui-selectFileInputWidget-empty' );
5811 if ( this.showDropTarget
) {
5813 this.loadAndGetImageUrl( this.currentFiles
[ 0 ] ).done( function ( url
) {
5814 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
5815 }.bind( this ) ).fail( function () {
5816 this.$thumbnail
.append(
5817 new OO
.ui
.IconWidget( {
5819 classes
: [ 'oo-ui-selectFileWidget-noThumbnail-icon' ]
5822 }.bind( this ) ).always( function () {
5825 this.$element
.off( 'click' );
5828 if ( this.showDropTarget
) {
5829 this.$element
.off( 'click' );
5831 click
: this.onDropTargetClick
.bind( this )
5835 .css( 'background-image', '' );
5837 this.$element
.addClass( 'oo-ui-selectFileInputWidget-empty' );
5842 * If the selected file is an image, get its URL and load it.
5844 * @param {File} file File
5845 * @return {jQuery.Promise} Promise resolves with the image URL after it has loaded
5847 OO
.ui
.SelectFileWidget
.prototype.loadAndGetImageUrl = function ( file
) {
5848 var deferred
= $.Deferred(),
5849 reader
= new FileReader();
5852 ( OO
.getProp( file
, 'type' ) || '' ).indexOf( 'image/' ) === 0 &&
5853 file
.size
< this.thumbnailSizeLimit
* 1024 * 1024
5855 reader
.onload = function ( event
) {
5856 var img
= document
.createElement( 'img' );
5857 img
.addEventListener( 'load', function () {
5859 img
.naturalWidth
=== 0 ||
5860 img
.naturalHeight
=== 0 ||
5861 img
.complete
=== false
5865 deferred
.resolve( event
.target
.result
);
5868 img
.src
= event
.target
.result
;
5870 reader
.readAsDataURL( file
);
5875 return deferred
.promise();
5881 OO
.ui
.SelectFileWidget
.prototype.onFileSelected = function ( e
) {
5884 if ( this.inputClearing
) {
5888 files
= this.filterFiles( e
.target
.files
|| [] );
5890 // After a file is selected clear the native widget to avoid confusion
5891 this.inputClearing
= true;
5892 this.$input
[ 0 ].value
= '';
5893 this.inputClearing
= false;
5895 this.setValue( files
);
5899 * Handle drop target click events.
5902 * @param {jQuery.Event} e Key press event
5903 * @return {undefined|boolean} False to prevent default if event is handled
5905 OO
.ui
.SelectFileWidget
.prototype.onDropTargetClick = function () {
5906 if ( !this.isDisabled() && this.$input
) {
5907 this.$input
.trigger( 'click' );
5913 * Handle drag enter and over events
5916 * @param {jQuery.Event} e Drag event
5917 * @return {undefined|boolean} False to prevent default if event is handled
5919 OO
.ui
.SelectFileWidget
.prototype.onDragEnterOrOver = function ( e
) {
5921 hasDroppableFile
= false,
5922 dt
= e
.originalEvent
.dataTransfer
;
5925 e
.stopPropagation();
5927 if ( this.isDisabled() ) {
5928 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
5929 dt
.dropEffect
= 'none';
5933 // DataTransferItem and File both have a type property, but in Chrome files
5934 // have no information at this point.
5935 itemsOrFiles
= dt
.items
|| dt
.files
;
5936 if ( itemsOrFiles
&& itemsOrFiles
.length
) {
5937 if ( this.filterFiles( itemsOrFiles
).length
) {
5938 hasDroppableFile
= true;
5940 // dt.types is Array-like, but not an Array
5941 } else if ( Array
.prototype.indexOf
.call( OO
.getProp( dt
, 'types' ) || [], 'Files' ) !== -1 ) {
5942 // File information is not available at this point for security so just assume
5943 // it is acceptable for now.
5944 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
5945 hasDroppableFile
= true;
5948 this.$element
.toggleClass( 'oo-ui-selectFileWidget-canDrop', hasDroppableFile
);
5949 if ( !hasDroppableFile
) {
5950 dt
.dropEffect
= 'none';
5957 * Handle drag leave events
5960 * @param {jQuery.Event} e Drag event
5962 OO
.ui
.SelectFileWidget
.prototype.onDragLeave = function () {
5963 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
5967 * Handle drop events
5970 * @param {jQuery.Event} e Drop event
5971 * @return {undefined|boolean} False to prevent default if event is handled
5973 OO
.ui
.SelectFileWidget
.prototype.onDrop = function ( e
) {
5975 dt
= e
.originalEvent
.dataTransfer
;
5978 e
.stopPropagation();
5979 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
5981 if ( this.isDisabled() ) {
5985 files
= this.filterFiles( dt
.files
|| [] );
5986 this.setValue( files
);
5994 OO
.ui
.SelectFileWidget
.prototype.setDisabled = function ( disabled
) {
5995 disabled
= disabled
|| !this.constructor.static.isSupported();
5998 OO
.ui
.SelectFileWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
6002 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field},
6003 * where users can type a search query, and a menu of search results,
6004 * which is displayed beneath the query field.
6005 * Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible
6006 * to the user. Users can choose an item from the menu or type a query into the text field to
6007 * search for a matching result item.
6008 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
6010 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
6011 * the [OOUI demos][1] for an example.
6013 * [1]: https://doc.wikimedia.org/oojs-ui/master/demos/#SearchInputWidget-type-search
6016 * @extends OO.ui.Widget
6019 * @param {Object} [config] Configuration options
6020 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6021 * @cfg {string} [value] Initial query value
6023 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
6024 // Configuration initialization
6025 config
= config
|| {};
6027 // Parent constructor
6028 OO
.ui
.SearchWidget
.parent
.call( this, config
);
6031 this.query
= new OO
.ui
.TextInputWidget( {
6033 placeholder
: config
.placeholder
,
6036 this.results
= new OO
.ui
.SelectWidget();
6037 this.$query
= $( '<div>' );
6038 this.$results
= $( '<div>' );
6041 this.query
.connect( this, {
6042 change
: 'onQueryChange',
6043 enter
: 'onQueryEnter'
6045 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
6049 .addClass( 'oo-ui-searchWidget-query' )
6050 .append( this.query
.$element
);
6052 .addClass( 'oo-ui-searchWidget-results' )
6053 .append( this.results
.$element
);
6055 .addClass( 'oo-ui-searchWidget' )
6056 .append( this.$results
, this.$query
);
6061 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
6066 * Handle query key down events.
6069 * @param {jQuery.Event} e Key down event
6071 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
6072 var highlightedItem
, nextItem
,
6073 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
6076 highlightedItem
= this.results
.findHighlightedItem();
6077 if ( !highlightedItem
) {
6078 highlightedItem
= this.results
.findSelectedItem();
6080 nextItem
= this.results
.findRelativeSelectableItem( highlightedItem
, dir
);
6081 this.results
.highlightItem( nextItem
);
6082 nextItem
.scrollElementIntoView();
6087 * Handle select widget select events.
6089 * Clears existing results. Subclasses should repopulate items according to new query.
6092 * @param {string} value New value
6094 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
6096 this.results
.clearItems();
6100 * Handle select widget enter key events.
6102 * Chooses highlighted item.
6105 * @param {string} value New value
6107 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
6108 var highlightedItem
= this.results
.findHighlightedItem();
6109 if ( highlightedItem
) {
6110 this.results
.chooseItem( highlightedItem
);
6115 * Get the query input.
6117 * @return {OO.ui.TextInputWidget} Query input
6119 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
6124 * Get the search results menu.
6126 * @return {OO.ui.SelectWidget} Menu of search results
6128 OO
.ui
.SearchWidget
.prototype.getResults = function () {
6129 return this.results
;
6134 //# sourceMappingURL=oojs-ui-widgets.js.map.json