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-03-14T00:52:20Z
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}.
498 OO
.ui
.mixin
.RequestManager
= function OoUiMixinRequestManager() {
499 this.requestCache
= {};
500 this.requestQuery
= null;
501 this.requestRequest
= null;
506 OO
.initClass( OO
.ui
.mixin
.RequestManager
);
509 * Get request results for the current query.
511 * @return {jQuery.Promise} Promise object which will be passed response data as the first argument
512 * of the done event. If the request was aborted to make way for a subsequent request, this
513 * promise may not be rejected, depending on what jQuery feels like doing.
515 OO
.ui
.mixin
.RequestManager
.prototype.getRequestData = function () {
517 value
= this.getRequestQuery(),
518 deferred
= $.Deferred(),
522 if ( Object
.prototype.hasOwnProperty
.call( this.requestCache
, value
) ) {
523 deferred
.resolve( this.requestCache
[ value
] );
525 if ( this.pushPending
) {
528 this.requestQuery
= value
;
529 ourRequest
= this.requestRequest
= this.getRequest();
531 .always( function () {
532 // We need to pop pending even if this is an old request, otherwise
533 // the widget will remain pending forever.
534 // TODO: this assumes that an aborted request will fail or succeed soon after
535 // being aborted, or at least eventually. It would be nice if we could popPending()
536 // at abort time, but only if we knew that we hadn't already called popPending()
538 if ( widget
.popPending
) {
542 .done( function ( response
) {
543 // If this is an old request (and aborting it somehow caused it to still succeed),
544 // ignore its success completely
545 if ( ourRequest
=== widget
.requestRequest
) {
546 widget
.requestQuery
= null;
547 widget
.requestRequest
= null;
548 widget
.requestCache
[ value
] =
549 widget
.getRequestCacheDataFromResponse( response
);
550 deferred
.resolve( widget
.requestCache
[ value
] );
554 // If this is an old request (or a request failing because it's being aborted),
555 // ignore its failure completely
556 if ( ourRequest
=== widget
.requestRequest
) {
557 widget
.requestQuery
= null;
558 widget
.requestRequest
= null;
563 return deferred
.promise();
567 * Abort the currently pending request, if any.
571 OO
.ui
.mixin
.RequestManager
.prototype.abortRequest = function () {
572 var oldRequest
= this.requestRequest
;
574 // First unset this.requestRequest to the fail handler will notice
575 // that the request is no longer current
576 this.requestRequest
= null;
577 this.requestQuery
= null;
583 * Get the query to be made.
588 * @return {string} query to be used
590 OO
.ui
.mixin
.RequestManager
.prototype.getRequestQuery
= null;
593 * Get a new request object of the current query value.
598 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
600 OO
.ui
.mixin
.RequestManager
.prototype.getRequest
= null;
603 * Pre-process data returned by the request from #getRequest.
605 * The return value of this function will be cached, and any further queries for the given value
606 * will use the cache rather than doing API requests.
611 * @param {Mixed} response Response from server
612 * @return {Mixed} Cached result data
614 OO
.ui
.mixin
.RequestManager
.prototype.getRequestCacheDataFromResponse
= null;
617 * LookupElement is a mixin that creates a {@link OO.ui.MenuSelectWidget menu} of suggested
618 * values for a {@link OO.ui.TextInputWidget text input widget}. Suggested values are based on
619 * the characters the user types into the text input field and, in general, the menu is only
620 * displayed when the user types. If a suggested value is chosen from the lookup menu, that value
621 * becomes the value of the input field.
623 * Note that a new menu of suggested items is displayed when a value is chosen from the
624 * lookup menu. If this is not the desired behavior, disable lookup menus with the
625 * #setLookupsDisabled method, then set the value, then re-enable lookups.
627 * See the [OOUI demos][1] for an example.
629 * [1]: https://doc.wikimedia.org/oojs-ui/master/demos/#LookupElement-try-inputting-an-integer
633 * @mixins OO.ui.mixin.RequestManager
636 * @param {Object} [config] Configuration options
637 * @cfg {jQuery} [$overlay] Overlay for the lookup menu; defaults to relative positioning.
638 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
639 * @cfg {jQuery} [$container=this.$element] The container element. The lookup menu is rendered
640 * beneath the specified element.
641 * @cfg {Object} [menu] Configuration options to pass to
642 * {@link OO.ui.MenuSelectWidget menu select widget}
643 * @cfg {boolean} [allowSuggestionsWhenEmpty=false] Request and display a lookup menu when the
644 * text input is empty.
645 * By default, the lookup menu is not generated and displayed until the user begins to type.
646 * @cfg {boolean} [highlightFirst=true] Whether the first lookup result should be highlighted
647 * (so, that the user can take it over into the input with simply pressing return) automatically
650 OO
.ui
.mixin
.LookupElement
= function OoUiMixinLookupElement( config
) {
651 // Configuration initialization
652 config
= $.extend( { highlightFirst
: true }, config
);
654 // Mixin constructors
655 OO
.ui
.mixin
.RequestManager
.call( this, config
);
658 this.$overlay
= ( config
.$overlay
=== true ?
659 OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
660 this.lookupMenu
= new OO
.ui
.MenuSelectWidget( $.extend( {
663 $floatableContainer
: config
.$container
|| this.$element
666 this.allowSuggestionsWhenEmpty
= config
.allowSuggestionsWhenEmpty
|| false;
668 this.lookupsDisabled
= false;
669 this.lookupInputFocused
= false;
670 this.lookupHighlightFirstItem
= config
.highlightFirst
;
674 focus
: this.onLookupInputFocus
.bind( this ),
675 blur
: this.onLookupInputBlur
.bind( this ),
676 mousedown
: this.onLookupInputMouseDown
.bind( this )
678 this.connect( this, {
679 change
: 'onLookupInputChange'
681 this.lookupMenu
.connect( this, {
682 toggle
: 'onLookupMenuToggle',
683 choose
: 'onLookupMenuItemChoose'
689 'aria-owns': this.lookupMenu
.getElementId(),
690 'aria-autocomplete': 'list'
692 this.$element
.addClass( 'oo-ui-lookupElement' );
693 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
694 this.$overlay
.append( this.lookupMenu
.$element
);
699 OO
.mixinClass( OO
.ui
.mixin
.LookupElement
, OO
.ui
.mixin
.RequestManager
);
704 * Handle input focus event.
707 * @param {jQuery.Event} e Input focus event
709 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputFocus = function () {
710 this.lookupInputFocused
= true;
711 this.populateLookupMenu();
715 * Handle input blur event.
718 * @param {jQuery.Event} e Input blur event
720 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputBlur = function () {
721 this.closeLookupMenu();
722 this.lookupInputFocused
= false;
726 * Handle input mouse down event.
729 * @param {jQuery.Event} e Input mouse down event
731 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputMouseDown = function () {
732 // Only open the menu if the input was already focused.
733 // This way we allow the user to open the menu again after closing it with Escape (esc)
734 // by clicking in the input. Opening (and populating) the menu when initially
735 // clicking into the input is handled by the focus handler.
736 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
737 this.populateLookupMenu();
742 * Handle input change event.
745 * @param {string} value New input value
747 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputChange = function () {
748 if ( this.lookupInputFocused
) {
749 this.populateLookupMenu();
754 * Handle the lookup menu being shown/hidden.
757 * @param {boolean} visible Whether the lookup menu is now visible.
759 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
761 // When the menu is hidden, abort any active request and clear the menu.
762 // This has to be done here in addition to closeLookupMenu(), because
763 // MenuSelectWidget will close itself when the user presses Escape (esc).
764 this.abortLookupRequest();
765 this.lookupMenu
.clearItems();
770 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
773 * @param {OO.ui.MenuOptionWidget} item Selected item
775 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
776 this.setValue( item
.getData() );
783 * @return {OO.ui.MenuSelectWidget}
785 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenu = function () {
786 return this.lookupMenu
;
790 * Disable or re-enable lookups.
792 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
794 * @param {boolean} disabled Disable lookups
796 OO
.ui
.mixin
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
797 this.lookupsDisabled
= !!disabled
;
801 * Open the menu. If there are no entries in the menu, this does nothing.
805 * @return {OO.ui.Element} The element, for chaining
807 OO
.ui
.mixin
.LookupElement
.prototype.openLookupMenu = function () {
808 if ( !this.lookupMenu
.isEmpty() ) {
809 this.lookupMenu
.toggle( true );
815 * Close the menu, empty it, and abort any pending request.
819 * @return {OO.ui.Element} The element, for chaining
821 OO
.ui
.mixin
.LookupElement
.prototype.closeLookupMenu = function () {
822 this.lookupMenu
.toggle( false );
823 this.abortLookupRequest();
824 this.lookupMenu
.clearItems();
829 * Request menu items based on the input's current value, and when they arrive,
830 * populate the menu with these items and show the menu.
832 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
836 * @return {OO.ui.Element} The element, for chaining
838 OO
.ui
.mixin
.LookupElement
.prototype.populateLookupMenu = function () {
840 value
= this.getValue();
842 if ( this.lookupsDisabled
|| this.isReadOnly() ) {
846 // If the input is empty, clear the menu, unless suggestions when empty are allowed.
847 if ( !this.allowSuggestionsWhenEmpty
&& value
=== '' ) {
848 this.closeLookupMenu();
849 // Skip population if there is already a request pending for the current value
850 } else if ( value
!== this.lookupQuery
) {
851 this.getLookupMenuItems()
852 .done( function ( items
) {
853 widget
.lookupMenu
.clearItems();
854 if ( items
.length
) {
858 widget
.initializeLookupMenuSelection();
860 widget
.lookupMenu
.toggle( false );
864 widget
.lookupMenu
.clearItems();
865 widget
.lookupMenu
.toggle( false );
873 * Highlight the first selectable item in the menu, if configured.
878 OO
.ui
.mixin
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
879 if ( this.lookupHighlightFirstItem
&& !this.lookupMenu
.findSelectedItem() ) {
880 this.lookupMenu
.highlightItem( this.lookupMenu
.findFirstSelectableItem() );
885 * Get lookup menu items for the current query.
888 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
889 * the done event. If the request was aborted to make way for a subsequent request, this promise
890 * will not be rejected: it will remain pending forever.
892 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuItems = function () {
893 return this.getRequestData().then( function ( data
) {
894 return this.getLookupMenuOptionsFromData( data
);
899 * Abort the currently pending lookup request, if any.
903 OO
.ui
.mixin
.LookupElement
.prototype.abortLookupRequest = function () {
908 * Get a new request object of the current lookup query value.
913 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
915 OO
.ui
.mixin
.LookupElement
.prototype.getLookupRequest
= null;
918 * Pre-process data returned by the request from #getLookupRequest.
920 * The return value of this function will be cached, and any further queries for the given value
921 * will use the cache rather than doing API requests.
926 * @param {Mixed} response Response from server
927 * @return {Mixed} Cached result data
929 OO
.ui
.mixin
.LookupElement
.prototype.getLookupCacheDataFromResponse
= null;
932 * Get a list of menu option widgets from the (possibly cached) data returned by
933 * #getLookupCacheDataFromResponse.
938 * @param {Mixed} data Cached result data, usually an array
939 * @return {OO.ui.MenuOptionWidget[]} Menu items
941 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuOptionsFromData
= null;
944 * Set the read-only state of the widget.
946 * This will also disable/enable the lookups functionality.
948 * @param {boolean} readOnly Make input read-only
950 * @return {OO.ui.Element} The element, for chaining
952 OO
.ui
.mixin
.LookupElement
.prototype.setReadOnly = function ( readOnly
) {
954 // Note: Calling #setReadOnly this way assumes this is mixed into an OO.ui.TextInputWidget
955 OO
.ui
.TextInputWidget
.prototype.setReadOnly
.call( this, readOnly
);
957 // During construction, #setReadOnly is called before the OO.ui.mixin.LookupElement constructor.
958 if ( this.isReadOnly() && this.lookupMenu
) {
959 this.closeLookupMenu();
966 * @inheritdoc OO.ui.mixin.RequestManager
968 OO
.ui
.mixin
.LookupElement
.prototype.getRequestQuery = function () {
969 return this.getValue();
973 * @inheritdoc OO.ui.mixin.RequestManager
975 OO
.ui
.mixin
.LookupElement
.prototype.getRequest = function () {
976 return this.getLookupRequest();
980 * @inheritdoc OO.ui.mixin.RequestManager
982 OO
.ui
.mixin
.LookupElement
.prototype.getRequestCacheDataFromResponse = function ( response
) {
983 return this.getLookupCacheDataFromResponse( response
);
987 * TabPanelLayouts are used within {@link OO.ui.IndexLayout index layouts} to create tab panels that
988 * users can select and display from the index's optional {@link OO.ui.TabSelectWidget tab}
989 * navigation. TabPanels are usually not instantiated directly, rather extended to include the
990 * required content and functionality.
992 * Each tab panel must have a unique symbolic name, which is passed to the constructor. In addition,
993 * the tab panel's tab item is customized (with a label) using the #setupTabItem method. See
994 * {@link OO.ui.IndexLayout IndexLayout} for an example.
997 * @extends OO.ui.PanelLayout
1000 * @param {string} name Unique symbolic name of tab panel
1001 * @param {Object} [config] Configuration options
1002 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] Label for tab panel's tab
1004 OO
.ui
.TabPanelLayout
= function OoUiTabPanelLayout( name
, config
) {
1005 // Allow passing positional parameters inside the config object
1006 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1011 // Configuration initialization
1012 config
= $.extend( { scrollable
: true }, config
);
1014 // Parent constructor
1015 OO
.ui
.TabPanelLayout
.parent
.call( this, config
);
1019 this.label
= config
.label
;
1020 this.tabItem
= null;
1021 this.active
= false;
1025 .addClass( 'oo-ui-tabPanelLayout' )
1026 .attr( 'role', 'tabpanel' );
1031 OO
.inheritClass( OO
.ui
.TabPanelLayout
, OO
.ui
.PanelLayout
);
1036 * An 'active' event is emitted when the tab panel becomes active. Tab panels become active when
1037 * they are shown in a index layout that is configured to display only one tab panel at a time.
1040 * @param {boolean} active Tab panel is active
1046 * Get the symbolic name of the tab panel.
1048 * @return {string} Symbolic name of tab panel
1050 OO
.ui
.TabPanelLayout
.prototype.getName = function () {
1055 * Check if tab panel is active.
1057 * Tab panels become active when they are shown in a {@link OO.ui.IndexLayout index layout} that is
1058 * configured to display only one tab panel at a time. Additional CSS is applied to the tab panel's
1059 * tab item to reflect the active state.
1061 * @return {boolean} Tab panel is active
1063 OO
.ui
.TabPanelLayout
.prototype.isActive = function () {
1070 * The tab item allows users to access the tab panel from the index's tab
1071 * navigation. The tab item itself can be customized (with a label, level, etc.) using the
1072 * #setupTabItem method.
1074 * @return {OO.ui.TabOptionWidget|null} Tab option widget
1076 OO
.ui
.TabPanelLayout
.prototype.getTabItem = function () {
1077 return this.tabItem
;
1081 * Set or unset the tab item.
1083 * Specify a {@link OO.ui.TabOptionWidget tab option} to set it,
1084 * or `null` to clear the tab item. To customize the tab item itself (e.g., to set a label or tab
1085 * level), use #setupTabItem instead of this method.
1087 * @param {OO.ui.TabOptionWidget|null} tabItem Tab option widget, null to clear
1089 * @return {OO.ui.TabPanelLayout} The layout, for chaining
1091 OO
.ui
.TabPanelLayout
.prototype.setTabItem = function ( tabItem
) {
1092 this.tabItem
= tabItem
|| null;
1094 this.setupTabItem();
1100 * Set up the tab item.
1102 * Use this method to customize the tab item (e.g., to add a label or tab level). To set or unset
1103 * the tab item itself (with a {@link OO.ui.TabOptionWidget tab option} or `null`), use
1104 * the #setTabItem method instead.
1106 * @param {OO.ui.TabOptionWidget} tabItem Tab option widget to set up
1108 * @return {OO.ui.TabPanelLayout} The layout, for chaining
1110 OO
.ui
.TabPanelLayout
.prototype.setupTabItem = function () {
1111 this.$element
.attr( 'aria-labelledby', this.tabItem
.getElementId() );
1113 this.tabItem
.$element
.attr( 'aria-controls', this.getElementId() );
1116 this.tabItem
.setLabel( this.label
);
1122 * Set the tab panel to its 'active' state.
1124 * Tab panels become active when they are shown in a index layout that is configured to display only
1125 * one tab panel at a time. Additional CSS is applied to the tab item to reflect the tab panel's
1126 * active state. Outside of the index context, setting the active state on a tab panel does nothing.
1128 * @param {boolean} active Tab panel is active
1131 OO
.ui
.TabPanelLayout
.prototype.setActive = function ( active
) {
1134 if ( active
!== this.active
) {
1135 this.active
= active
;
1136 this.$element
.toggleClass( 'oo-ui-tabPanelLayout-active', this.active
);
1137 this.emit( 'active', this.active
);
1142 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that
1143 * users can select and display from the booklet's optional
1144 * {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated
1145 * directly, rather extended to include the required content and functionality.
1147 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the
1148 * page's outline item is customized (with a label, outline level, etc.) using the
1149 * #setupOutlineItem method. See {@link OO.ui.BookletLayout BookletLayout} for an example.
1152 * @extends OO.ui.PanelLayout
1155 * @param {string} name Unique symbolic name of page
1156 * @param {Object} [config] Configuration options
1158 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
1159 // Allow passing positional parameters inside the config object
1160 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1165 // Configuration initialization
1166 config
= $.extend( { scrollable
: true }, config
);
1168 // Parent constructor
1169 OO
.ui
.PageLayout
.parent
.call( this, config
);
1173 this.outlineItem
= null;
1174 this.active
= false;
1177 this.$element
.addClass( 'oo-ui-pageLayout' );
1182 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
1187 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1188 * shown in a booklet layout that is configured to display only one page at a time.
1191 * @param {boolean} active Page is active
1197 * Get the symbolic name of the page.
1199 * @return {string} Symbolic name of page
1201 OO
.ui
.PageLayout
.prototype.getName = function () {
1206 * Check if page is active.
1208 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is
1209 * configured to display only one page at a time. Additional CSS is applied to the page's outline
1210 * item to reflect the active state.
1212 * @return {boolean} Page is active
1214 OO
.ui
.PageLayout
.prototype.isActive = function () {
1221 * The outline item allows users to access the page from the booklet's outline
1222 * navigation. The outline item itself can be customized (with a label, level, etc.) using the
1223 * #setupOutlineItem method.
1225 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1227 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
1228 return this.outlineItem
;
1232 * Set or unset the outline item.
1234 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1235 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label
1236 * or outline level), use #setupOutlineItem instead of this method.
1238 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1240 * @return {OO.ui.PageLayout} The layout, for chaining
1242 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
1243 this.outlineItem
= outlineItem
|| null;
1244 if ( outlineItem
) {
1245 this.setupOutlineItem();
1251 * Set up the outline item.
1253 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or
1254 * unset the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or
1255 * `null`), use the #setOutlineItem method instead.
1257 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1259 * @return {OO.ui.PageLayout} The layout, for chaining
1261 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
1266 * Set the page to its 'active' state.
1268 * Pages become active when they are shown in a booklet layout that is configured to display only
1269 * one page at a time. Additional CSS is applied to the outline item to reflect the page's active
1270 * state. Outside of the booklet context, setting the active state on a page does nothing.
1272 * @param {boolean} active Page is active
1275 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
1278 if ( active
!== this.active
) {
1279 this.active
= active
;
1280 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
1281 this.emit( 'active', this.active
);
1286 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one
1287 * panel is displayed at a time, though the stack layout can also be configured to show all
1288 * contained panels, one after another, by setting the #continuous option to 'true'.
1291 * // A stack layout with two panels, configured to be displayed continuously
1292 * var myStack = new OO.ui.StackLayout( {
1294 * new OO.ui.PanelLayout( {
1295 * $content: $( '<p>Panel One</p>' ),
1299 * new OO.ui.PanelLayout( {
1300 * $content: $( '<p>Panel Two</p>' ),
1307 * $( document.body ).append( myStack.$element );
1310 * @extends OO.ui.PanelLayout
1311 * @mixins OO.ui.mixin.GroupElement
1314 * @param {Object} [config] Configuration options
1315 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel
1316 * is displayed at a time.
1317 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1319 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
1320 // Configuration initialization
1321 // Make the layout scrollable in continuous mode, otherwise each
1322 // panel is responsible for its own scrolling.
1323 config
= $.extend( { scrollable
: !!( config
&& config
.continuous
) }, config
);
1325 // Parent constructor
1326 OO
.ui
.StackLayout
.parent
.call( this, config
);
1328 // Mixin constructors
1329 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( { $group
: this.$element
}, config
) );
1332 this.currentItem
= null;
1333 this.continuous
= !!config
.continuous
;
1336 this.$element
.addClass( 'oo-ui-stackLayout' );
1337 if ( this.continuous
) {
1338 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
1339 this.$element
.on( 'scroll', OO
.ui
.debounce( this.onScroll
.bind( this ), 250 ) );
1341 if ( Array
.isArray( config
.items
) ) {
1342 this.addItems( config
.items
);
1348 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
1349 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.mixin
.GroupElement
);
1354 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1355 * {@link #clearItems cleared} or {@link #setItem displayed}.
1358 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1362 * When used in continuous mode, this event is emitted when the user scrolls down
1363 * far enough such that currentItem is no longer visible.
1365 * @event visibleItemChange
1366 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1372 * Handle scroll events from the layout element
1374 * @param {jQuery.Event} e
1375 * @fires visibleItemChange
1377 OO
.ui
.StackLayout
.prototype.onScroll = function () {
1379 len
= this.items
.length
,
1380 currentIndex
= this.items
.indexOf( this.currentItem
),
1381 newIndex
= currentIndex
,
1382 containerRect
= this.$element
[ 0 ].getBoundingClientRect();
1384 if ( !containerRect
|| ( !containerRect
.top
&& !containerRect
.bottom
) ) {
1385 // Can't get bounding rect, possibly not attached.
1389 function getRect( item
) {
1390 return item
.$element
[ 0 ].getBoundingClientRect();
1393 function isVisible( item
) {
1394 var rect
= getRect( item
);
1395 return rect
.bottom
> containerRect
.top
&& rect
.top
< containerRect
.bottom
;
1398 currentRect
= getRect( this.currentItem
);
1400 if ( currentRect
.bottom
< containerRect
.top
) {
1401 // Scrolled down past current item
1402 while ( ++newIndex
< len
) {
1403 if ( isVisible( this.items
[ newIndex
] ) ) {
1407 } else if ( currentRect
.top
> containerRect
.bottom
) {
1408 // Scrolled up past current item
1409 while ( --newIndex
>= 0 ) {
1410 if ( isVisible( this.items
[ newIndex
] ) ) {
1416 if ( newIndex
!== currentIndex
) {
1417 this.emit( 'visibleItemChange', this.items
[ newIndex
] );
1422 * Get the current panel.
1424 * @return {OO.ui.Layout|null}
1426 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
1427 return this.currentItem
;
1431 * Unset the current item.
1434 * @param {OO.ui.StackLayout} layout
1437 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
1438 var prevItem
= this.currentItem
;
1439 if ( prevItem
=== null ) {
1443 this.currentItem
= null;
1444 this.emit( 'set', null );
1448 * Add panel layouts to the stack layout.
1450 * Panels will be added to the end of the stack layout array unless the optional index parameter
1451 * specifies a different insertion point. Adding a panel that is already in the stack will move it
1452 * to the end of the array or the point specified by the index.
1454 * @param {OO.ui.Layout[]} items Panels to add
1455 * @param {number} [index] Index of the insertion point
1457 * @return {OO.ui.StackLayout} The layout, for chaining
1459 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
1460 // Update the visibility
1461 this.updateHiddenState( items
, this.currentItem
);
1464 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
, index
);
1466 if ( !this.currentItem
&& items
.length
) {
1467 this.setItem( items
[ 0 ] );
1474 * Remove the specified panels from the stack layout.
1476 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all
1477 * panels, you may wish to use the #clearItems method instead.
1479 * @param {OO.ui.Layout[]} items Panels to remove
1481 * @return {OO.ui.StackLayout} The layout, for chaining
1484 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
1486 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
1488 if ( items
.indexOf( this.currentItem
) !== -1 ) {
1489 if ( this.items
.length
) {
1490 this.setItem( this.items
[ 0 ] );
1492 this.unsetCurrentItem();
1500 * Clear all panels from the stack layout.
1502 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1503 * a subset of panels, use the #removeItems method.
1506 * @return {OO.ui.StackLayout} The layout, for chaining
1509 OO
.ui
.StackLayout
.prototype.clearItems = function () {
1510 this.unsetCurrentItem();
1511 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
1517 * Show the specified panel.
1519 * If another panel is currently displayed, it will be hidden.
1521 * @param {OO.ui.Layout} item Panel to show
1523 * @return {OO.ui.StackLayout} The layout, for chaining
1526 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
1527 if ( item
!== this.currentItem
) {
1528 this.updateHiddenState( this.items
, item
);
1530 if ( this.items
.indexOf( item
) !== -1 ) {
1531 this.currentItem
= item
;
1532 this.emit( 'set', item
);
1534 this.unsetCurrentItem();
1542 * Reset the scroll offset of all panels, or the container if continuous
1546 OO
.ui
.StackLayout
.prototype.resetScroll = function () {
1547 if ( this.continuous
) {
1549 return OO
.ui
.StackLayout
.parent
.prototype.resetScroll
.call( this );
1552 this.getItems().forEach( function ( panel
) {
1553 var hidden
= panel
.$element
.hasClass( 'oo-ui-element-hidden' );
1554 // Scroll can only be reset when panel is visible
1555 panel
.$element
.removeClass( 'oo-ui-element-hidden' );
1556 panel
.resetScroll();
1558 panel
.$element
.addClass( 'oo-ui-element-hidden' );
1566 * Update the visibility of all items in case of non-continuous view.
1568 * Ensure all items are hidden except for the selected one.
1569 * This method does nothing when the stack is continuous.
1572 * @param {OO.ui.Layout[]} items Item list iterate over
1573 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1575 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
1578 if ( !this.continuous
) {
1579 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
1580 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
1581 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
1582 items
[ i
].$element
.attr( 'aria-hidden', 'true' );
1585 if ( selectedItem
) {
1586 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
1587 selectedItem
.$element
.removeAttr( 'aria-hidden' );
1593 * MenuLayouts combine a menu and a content {@link OO.ui.PanelLayout panel}. The menu is positioned
1594 * relative to the content (after, before, top, or bottom) and its size is customized with the
1595 * #menuSize config. The content area will fill all remaining space.
1599 * menuPanel = new OO.ui.PanelLayout( {
1604 * contentPanel = new OO.ui.PanelLayout( {
1609 * select = new OO.ui.SelectWidget( {
1611 * new OO.ui.OptionWidget( {
1615 * new OO.ui.OptionWidget( {
1619 * new OO.ui.OptionWidget( {
1623 * new OO.ui.OptionWidget( {
1628 * } ).on( 'select', function ( item ) {
1629 * menuLayout.setMenuPosition( item.getData() );
1632 * menuLayout = new OO.ui.MenuLayout( {
1634 * menuPanel: menuPanel,
1635 * contentPanel: contentPanel
1637 * menuLayout.$menu.append(
1638 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element );
1640 * menuLayout.$content.append(
1641 * contentPanel.$element.append(
1642 * '<b>Content panel</b>',
1643 * '<p>Note that the menu is positioned relative to the content panel: ' +
1644 * 'top, bottom, after, before.</p>'
1647 * $( document.body ).append( menuLayout.$element );
1649 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1650 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1651 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1654 * .oo-ui-menuLayout-menu {
1659 * .oo-ui-menuLayout-content {
1667 * @extends OO.ui.Layout
1670 * @param {Object} [config] Configuration options
1671 * @cfg {OO.ui.PanelLayout} [menuPanel] Menu panel
1672 * @cfg {OO.ui.PanelLayout} [contentPanel] Content panel
1673 * @cfg {boolean} [expanded=true] Expand the layout to fill the entire parent element.
1674 * @cfg {boolean} [showMenu=true] Show menu
1675 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1677 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
1678 // Configuration initialization
1679 config
= $.extend( {
1682 menuPosition
: 'before'
1685 // Parent constructor
1686 OO
.ui
.MenuLayout
.parent
.call( this, config
);
1688 this.menuPanel
= null;
1689 this.contentPanel
= null;
1690 this.expanded
= !!config
.expanded
;
1694 * @property {jQuery}
1696 this.$menu
= $( '<div>' );
1700 * @property {jQuery}
1702 this.$content
= $( '<div>' );
1705 this.$menu
.addClass( 'oo-ui-menuLayout-menu' );
1706 this.$content
.addClass( 'oo-ui-menuLayout-content' );
1707 this.$element
.addClass( 'oo-ui-menuLayout' );
1708 if ( config
.expanded
) {
1709 this.$element
.addClass( 'oo-ui-menuLayout-expanded' );
1711 this.$element
.addClass( 'oo-ui-menuLayout-static' );
1713 if ( config
.menuPanel
) {
1714 this.setMenuPanel( config
.menuPanel
);
1716 if ( config
.contentPanel
) {
1717 this.setContentPanel( config
.contentPanel
);
1719 this.setMenuPosition( config
.menuPosition
);
1720 this.toggleMenu( config
.showMenu
);
1725 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
1732 * @param {boolean} showMenu Show menu, omit to toggle
1734 * @return {OO.ui.MenuLayout} The layout, for chaining
1736 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
1737 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
1739 if ( this.showMenu
!== showMenu
) {
1740 this.showMenu
= showMenu
;
1742 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu
)
1743 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu
);
1744 this.$menu
.attr( 'aria-hidden', this.showMenu
? 'false' : 'true' );
1751 * Check if menu is visible
1753 * @return {boolean} Menu is visible
1755 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
1756 return this.showMenu
;
1760 * Set menu position.
1762 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1764 * @return {OO.ui.MenuLayout} The layout, for chaining
1766 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
1767 if ( [ 'top', 'bottom', 'before', 'after' ].indexOf( position
) === -1 ) {
1768 position
= 'before';
1771 this.$element
.removeClass( 'oo-ui-menuLayout-' + this.menuPosition
);
1772 this.menuPosition
= position
;
1773 if ( this.menuPosition
=== 'top' || this.menuPosition
=== 'before' ) {
1774 this.$element
.append( this.$menu
, this.$content
);
1776 this.$element
.append( this.$content
, this.$menu
);
1778 this.$element
.addClass( 'oo-ui-menuLayout-' + position
);
1784 * Get menu position.
1786 * @return {string} Menu position
1788 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
1789 return this.menuPosition
;
1793 * Set the menu panel.
1795 * @param {OO.ui.PanelLayout} menuPanel Menu panel
1797 OO
.ui
.MenuLayout
.prototype.setMenuPanel = function ( menuPanel
) {
1798 this.menuPanel
= menuPanel
;
1799 this.$menu
.append( this.menuPanel
.$element
);
1803 * Set the content panel.
1805 * @param {OO.ui.PanelLayout} contentPanel Content panel
1807 OO
.ui
.MenuLayout
.prototype.setContentPanel = function ( contentPanel
) {
1808 this.contentPanel
= contentPanel
;
1809 this.$content
.append( this.contentPanel
.$element
);
1813 * Clear the menu panel.
1815 OO
.ui
.MenuLayout
.prototype.clearMenuPanel = function () {
1816 this.menuPanel
= null;
1821 * Clear the content panel.
1823 OO
.ui
.MenuLayout
.prototype.clearContentPanel = function () {
1824 this.contentPanel
= null;
1825 this.$content
.empty();
1829 * Reset the scroll offset of all panels and the tab select widget
1833 OO
.ui
.MenuLayout
.prototype.resetScroll = function () {
1834 if ( this.menuPanel
) {
1835 this.menuPanel
.resetScroll();
1837 if ( this.contentPanel
) {
1838 this.contentPanel
.resetScroll();
1845 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1846 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1847 * through the pages and select which one to display. By default, only one page is
1848 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1849 * the booklet layout automatically focuses on the first focusable element, unless the
1850 * default setting is changed. Optionally, booklets can be configured to show
1851 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1854 * // Example of a BookletLayout that contains two PageLayouts.
1856 * function PageOneLayout( name, config ) {
1857 * PageOneLayout.parent.call( this, name, config );
1858 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on ' +
1859 * 'the left)</p>' );
1861 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1862 * PageOneLayout.prototype.setupOutlineItem = function () {
1863 * this.outlineItem.setLabel( 'Page One' );
1866 * function PageTwoLayout( name, config ) {
1867 * PageTwoLayout.parent.call( this, name, config );
1868 * this.$element.append( '<p>Second page</p>' );
1870 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1871 * PageTwoLayout.prototype.setupOutlineItem = function () {
1872 * this.outlineItem.setLabel( 'Page Two' );
1875 * var page1 = new PageOneLayout( 'one' ),
1876 * page2 = new PageTwoLayout( 'two' );
1878 * var booklet = new OO.ui.BookletLayout( {
1882 * booklet.addPages( [ page1, page2 ] );
1883 * $( document.body ).append( booklet.$element );
1886 * @extends OO.ui.MenuLayout
1889 * @param {Object} [config] Configuration options
1890 * @cfg {boolean} [continuous=false] Show all pages, one after another
1891 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is
1892 * displayed. Disabled on mobile.
1893 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the
1894 * pages of the booklet.
1895 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages.
1897 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
1898 // Configuration initialization
1899 config
= config
|| {};
1901 // Parent constructor
1902 OO
.ui
.BookletLayout
.parent
.call( this, config
);
1905 this.currentPageName
= null;
1907 this.ignoreFocus
= false;
1908 this.stackLayout
= new OO
.ui
.StackLayout( {
1909 continuous
: !!config
.continuous
,
1910 expanded
: this.expanded
1912 this.setContentPanel( this.stackLayout
);
1913 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
1914 this.outlineVisible
= false;
1915 this.outlined
= !!config
.outlined
;
1916 if ( this.outlined
) {
1917 this.editable
= !!config
.editable
;
1918 this.outlineControlsWidget
= null;
1919 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget();
1920 this.outlinePanel
= new OO
.ui
.PanelLayout( {
1921 expanded
: this.expanded
,
1924 this.setMenuPanel( this.outlinePanel
);
1925 this.outlineVisible
= true;
1926 if ( this.editable
) {
1927 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
1928 this.outlineSelectWidget
1932 this.toggleMenu( this.outlined
);
1935 this.stackLayout
.connect( this, {
1936 set: 'onStackLayoutSet'
1938 if ( this.outlined
) {
1939 this.outlineSelectWidget
.connect( this, {
1940 select
: 'onOutlineSelectWidgetSelect'
1942 this.scrolling
= false;
1943 this.stackLayout
.connect( this, {
1944 visibleItemChange
: 'onStackLayoutVisibleItemChange'
1947 if ( this.autoFocus
) {
1948 // Event 'focus' does not bubble, but 'focusin' does
1949 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
1953 this.$element
.addClass( 'oo-ui-bookletLayout' );
1954 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
1955 if ( this.outlined
) {
1956 this.outlinePanel
.$element
1957 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1958 .append( this.outlineSelectWidget
.$element
);
1959 if ( this.editable
) {
1960 this.outlinePanel
.$element
1961 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1962 .append( this.outlineControlsWidget
.$element
);
1969 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
1974 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the
1977 * @param {OO.ui.PageLayout} page Current page
1981 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
1984 * @param {OO.ui.PageLayout[]} page Added pages
1985 * @param {number} index Index pages were added at
1989 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
1990 * {@link #removePages removed} from the booklet.
1993 * @param {OO.ui.PageLayout[]} pages Removed pages
1999 * Handle stack layout focus.
2002 * @param {jQuery.Event} e Focusin event
2004 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
2007 // Find the page that an element was focused within
2008 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
2009 for ( name
in this.pages
) {
2010 // Check for page match, exclude current page to find only page changes
2011 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
2012 this.setPage( name
);
2019 * Handle visibleItemChange events from the stackLayout
2021 * The next visible page is set as the current page by selecting it
2024 * @param {OO.ui.PageLayout} page The next visible page in the layout
2026 OO
.ui
.BookletLayout
.prototype.onStackLayoutVisibleItemChange = function ( page
) {
2027 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
2028 // try and scroll the item into view again.
2029 this.scrolling
= true;
2030 this.outlineSelectWidget
.selectItemByData( page
.getName() );
2031 this.scrolling
= false;
2035 * Handle stack layout set events.
2038 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
2040 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
2041 var promise
, layout
= this;
2042 // If everything is unselected, do nothing
2046 // For continuous BookletLayouts, scroll the selected page into view first
2047 if ( this.stackLayout
.continuous
&& !this.scrolling
) {
2048 promise
= page
.scrollElementIntoView();
2050 promise
= $.Deferred().resolve();
2052 // Focus the first element on the newly selected panel.
2053 // Don't focus if the page was set by scrolling.
2054 if ( this.autoFocus
&& !OO
.ui
.isMobile() && !this.scrolling
) {
2055 promise
.done( function () {
2062 * Focus the first input in the current page.
2064 * If no page is selected, the first selectable page will be selected.
2065 * If the focus is already in an element on the current page, nothing will happen.
2067 * @param {number} [itemIndex] A specific item to focus on
2069 OO
.ui
.BookletLayout
.prototype.focus = function ( itemIndex
) {
2071 items
= this.stackLayout
.getItems();
2073 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2074 page
= items
[ itemIndex
];
2076 page
= this.stackLayout
.getCurrentItem();
2079 if ( !page
&& this.outlined
) {
2080 this.selectFirstSelectablePage();
2081 page
= this.stackLayout
.getCurrentItem();
2086 // Only change the focus if is not already in the current page
2087 if ( !OO
.ui
.contains( page
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
2093 * Find the first focusable input in the booklet layout and focus
2096 OO
.ui
.BookletLayout
.prototype.focusFirstFocusable = function () {
2097 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2101 * Handle outline widget select events.
2104 * @param {OO.ui.OptionWidget|null} item Selected item
2106 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
2108 this.setPage( item
.getData() );
2113 * Check if booklet has an outline.
2115 * @return {boolean} Booklet has an outline
2117 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
2118 return this.outlined
;
2122 * Check if booklet has editing controls.
2124 * @return {boolean} Booklet is editable
2126 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
2127 return this.editable
;
2131 * Check if booklet has a visible outline.
2133 * @return {boolean} Outline is visible
2135 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
2136 return this.outlined
&& this.outlineVisible
;
2140 * Hide or show the outline.
2142 * @param {boolean} [show] Show outline, omit to invert current state
2144 * @return {OO.ui.BookletLayout} The layout, for chaining
2146 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
2149 if ( this.outlined
) {
2150 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
2151 this.outlineVisible
= show
;
2152 this.toggleMenu( show
);
2153 if ( show
&& this.editable
) {
2154 // HACK: Kill dumb scrollbars when the sidebar stops animating, see T161798.
2155 // Only necessary when outline controls are present, delay matches transition on
2156 // `.oo-ui-menuLayout-menu`.
2157 setTimeout( function () {
2158 OO
.ui
.Element
.static.reconsiderScrollbars( booklet
.outlinePanel
.$element
[ 0 ] );
2159 }, OO
.ui
.theme
.getDialogTransitionDuration() );
2167 * Find the page closest to the specified page.
2169 * @param {OO.ui.PageLayout} page Page to use as a reference point
2170 * @return {OO.ui.PageLayout|null} Page closest to the specified page
2172 OO
.ui
.BookletLayout
.prototype.findClosestPage = function ( page
) {
2173 var next
, prev
, level
,
2174 pages
= this.stackLayout
.getItems(),
2175 index
= pages
.indexOf( page
);
2177 if ( index
!== -1 ) {
2178 next
= pages
[ index
+ 1 ];
2179 prev
= pages
[ index
- 1 ];
2180 // Prefer adjacent pages at the same level
2181 if ( this.outlined
) {
2182 level
= this.outlineSelectWidget
.findItemFromData( page
.getName() ).getLevel();
2185 level
=== this.outlineSelectWidget
.findItemFromData( prev
.getName() ).getLevel()
2191 level
=== this.outlineSelectWidget
.findItemFromData( next
.getName() ).getLevel()
2197 return prev
|| next
|| null;
2201 * Get the outline widget.
2203 * If the booklet is not outlined, the method will return `null`.
2205 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
2207 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
2208 return this.outlineSelectWidget
;
2212 * Get the outline controls widget.
2214 * If the outline is not editable, the method will return `null`.
2216 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
2218 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
2219 return this.outlineControlsWidget
;
2223 * Get a page by its symbolic name.
2225 * @param {string} name Symbolic name of page
2226 * @return {OO.ui.PageLayout|undefined} Page, if found
2228 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
2229 return this.pages
[ name
];
2233 * Get the current page.
2235 * @return {OO.ui.PageLayout|undefined} Current page, if found
2237 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
2238 var name
= this.getCurrentPageName();
2239 return name
? this.getPage( name
) : undefined;
2243 * Get the symbolic name of the current page.
2245 * @return {string|null} Symbolic name of the current page
2247 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
2248 return this.currentPageName
;
2252 * Add pages to the booklet layout
2254 * When pages are added with the same names as existing pages, the existing pages will be
2255 * automatically removed before the new pages are added.
2257 * @param {OO.ui.PageLayout[]} pages Pages to add
2258 * @param {number} index Index of the insertion point
2261 * @return {OO.ui.BookletLayout} The layout, for chaining
2263 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
2264 var i
, len
, name
, page
, item
, currentIndex
,
2265 stackLayoutPages
= this.stackLayout
.getItems(),
2269 // Remove pages with same names
2270 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2272 name
= page
.getName();
2274 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
2275 // Correct the insertion index
2276 currentIndex
= stackLayoutPages
.indexOf( this.pages
[ name
] );
2277 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2280 remove
.push( this.pages
[ name
] );
2283 if ( remove
.length
) {
2284 this.removePages( remove
);
2288 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2290 name
= page
.getName();
2291 this.pages
[ page
.getName() ] = page
;
2292 if ( this.outlined
) {
2293 item
= new OO
.ui
.OutlineOptionWidget( { data
: name
} );
2294 page
.setOutlineItem( item
);
2299 if ( this.outlined
&& items
.length
) {
2300 this.outlineSelectWidget
.addItems( items
, index
);
2301 this.selectFirstSelectablePage();
2303 this.stackLayout
.addItems( pages
, index
);
2304 this.emit( 'add', pages
, index
);
2310 * Remove the specified pages from the booklet layout.
2312 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2314 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2317 * @return {OO.ui.BookletLayout} The layout, for chaining
2319 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
2320 var i
, len
, name
, page
,
2323 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2325 name
= page
.getName();
2326 delete this.pages
[ name
];
2327 if ( this.outlined
) {
2328 items
.push( this.outlineSelectWidget
.findItemFromData( name
) );
2329 page
.setOutlineItem( null );
2332 if ( this.outlined
&& items
.length
) {
2333 this.outlineSelectWidget
.removeItems( items
);
2334 this.selectFirstSelectablePage();
2336 this.stackLayout
.removeItems( pages
);
2337 this.emit( 'remove', pages
);
2343 * Clear all pages from the booklet layout.
2345 * To remove only a subset of pages from the booklet, use the #removePages method.
2349 * @return {OO.ui.BookletLayout} The layout, for chaining
2351 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
2353 pages
= this.stackLayout
.getItems();
2356 this.currentPageName
= null;
2357 if ( this.outlined
) {
2358 this.outlineSelectWidget
.clearItems();
2359 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2360 pages
[ i
].setOutlineItem( null );
2363 this.stackLayout
.clearItems();
2365 this.emit( 'remove', pages
);
2371 * Set the current page by symbolic name.
2374 * @param {string} name Symbolic name of page
2376 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
2379 page
= this.pages
[ name
],
2380 previousPage
= this.currentPageName
&& this.pages
[ this.currentPageName
];
2382 if ( name
!== this.currentPageName
) {
2383 if ( this.outlined
) {
2384 selectedItem
= this.outlineSelectWidget
.findSelectedItem();
2385 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2386 this.outlineSelectWidget
.selectItemByData( name
);
2390 if ( previousPage
) {
2391 previousPage
.setActive( false );
2392 // Blur anything focused if the next page doesn't have anything focusable.
2393 // This is not needed if the next page has something focusable (because once it is
2394 // focused this blur happens automatically). If the layout is non-continuous, this
2395 // check is meaningless because the next page is not visible yet and thus can't
2399 !OO
.ui
.isMobile() &&
2400 this.stackLayout
.continuous
&&
2401 OO
.ui
.findFocusable( page
.$element
).length
!== 0
2403 $focused
= previousPage
.$element
.find( ':focus' );
2404 if ( $focused
.length
) {
2405 $focused
[ 0 ].blur();
2409 this.currentPageName
= name
;
2410 page
.setActive( true );
2411 this.stackLayout
.setItem( page
);
2412 if ( !this.stackLayout
.continuous
&& previousPage
) {
2413 // This should not be necessary, since any inputs on the previous page should have
2414 // been blurred when it was hidden, but browsers are not very consistent about
2416 $focused
= previousPage
.$element
.find( ':focus' );
2417 if ( $focused
.length
) {
2418 $focused
[ 0 ].blur();
2421 this.emit( 'set', page
);
2427 * For outlined-continuous booklets, also reset the outlineSelectWidget to the first item.
2431 OO
.ui
.BookletLayout
.prototype.resetScroll = function () {
2433 OO
.ui
.BookletLayout
.parent
.prototype.resetScroll
.call( this );
2437 this.stackLayout
.continuous
&&
2438 this.outlineSelectWidget
.findFirstSelectableItem()
2440 this.scrolling
= true;
2441 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.findFirstSelectableItem() );
2442 this.scrolling
= false;
2448 * Select the first selectable page.
2451 * @return {OO.ui.BookletLayout} The layout, for chaining
2453 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
2454 if ( !this.outlineSelectWidget
.findSelectedItem() ) {
2455 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.findFirstSelectableItem() );
2462 * IndexLayouts contain {@link OO.ui.TabPanelLayout tab panel layouts} as well as
2463 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the tab panels
2464 * and select which one to display. By default, only one tab panel is displayed at a time. When a
2465 * user navigates to a new tab panel, the index layout automatically focuses on the first focusable
2466 * element, unless the default setting is changed.
2468 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2471 * // Example of a IndexLayout that contains two TabPanelLayouts.
2473 * function TabPanelOneLayout( name, config ) {
2474 * TabPanelOneLayout.parent.call( this, name, config );
2475 * this.$element.append( '<p>First tab panel</p>' );
2477 * OO.inheritClass( TabPanelOneLayout, OO.ui.TabPanelLayout );
2478 * TabPanelOneLayout.prototype.setupTabItem = function () {
2479 * this.tabItem.setLabel( 'Tab panel one' );
2482 * var tabPanel1 = new TabPanelOneLayout( 'one' ),
2483 * tabPanel2 = new OO.ui.TabPanelLayout( 'two', { label: 'Tab panel two' } );
2485 * tabPanel2.$element.append( '<p>Second tab panel</p>' );
2487 * var index = new OO.ui.IndexLayout();
2489 * index.addTabPanels( [ tabPanel1, tabPanel2 ] );
2490 * $( document.body ).append( index.$element );
2493 * @extends OO.ui.MenuLayout
2496 * @param {Object} [config] Configuration options
2497 * @cfg {OO.ui.StackLayout} [contentPanel] Content stack (see MenuLayout)
2498 * @cfg {boolean} [continuous=false] Show all tab panels, one after another
2499 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new tab panel is
2500 * displayed. Disabled on mobile.
2502 OO
.ui
.IndexLayout
= function OoUiIndexLayout( config
) {
2503 // Configuration initialization
2504 config
= $.extend( {}, config
, { menuPosition
: 'top' } );
2506 // Parent constructor
2507 OO
.ui
.IndexLayout
.parent
.call( this, config
);
2510 this.currentTabPanelName
= null;
2511 // Allow infused widgets to pass existing tabPanels
2512 this.tabPanels
= config
.tabPanels
|| {};
2514 this.ignoreFocus
= false;
2515 this.stackLayout
= this.contentPanel
|| new OO
.ui
.StackLayout( {
2516 continuous
: !!config
.continuous
,
2517 expanded
: this.expanded
2519 this.setContentPanel( this.stackLayout
);
2520 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
2522 // Allow infused widgets to pass an existing tabSelectWidget
2523 this.tabSelectWidget
= config
.tabSelectWidget
|| new OO
.ui
.TabSelectWidget();
2524 this.tabPanel
= this.menuPanel
|| new OO
.ui
.PanelLayout( {
2525 expanded
: this.expanded
2527 this.setMenuPanel( this.tabPanel
);
2529 this.toggleMenu( true );
2532 this.stackLayout
.connect( this, {
2533 set: 'onStackLayoutSet'
2535 this.tabSelectWidget
.connect( this, {
2536 select
: 'onTabSelectWidgetSelect'
2538 if ( this.autoFocus
) {
2539 // Event 'focus' does not bubble, but 'focusin' does.
2540 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
2544 this.$element
.addClass( 'oo-ui-indexLayout' );
2545 this.stackLayout
.$element
.addClass( 'oo-ui-indexLayout-stackLayout' );
2546 this.tabPanel
.$element
2547 .addClass( 'oo-ui-indexLayout-tabPanel' )
2548 .append( this.tabSelectWidget
.$element
);
2550 this.selectFirstSelectableTabPanel();
2555 OO
.inheritClass( OO
.ui
.IndexLayout
, OO
.ui
.MenuLayout
);
2560 * A 'set' event is emitted when a tab panel is {@link #setTabPanel set} to be displayed by the
2564 * @param {OO.ui.TabPanelLayout} tabPanel Current tab panel
2568 * An 'add' event is emitted when tab panels are {@link #addTabPanels added} to the index layout.
2571 * @param {OO.ui.TabPanelLayout[]} tabPanel Added tab panels
2572 * @param {number} index Index tab panels were added at
2576 * A 'remove' event is emitted when tab panels are {@link #clearTabPanels cleared} or
2577 * {@link #removeTabPanels removed} from the index.
2580 * @param {OO.ui.TabPanelLayout[]} tabPanel Removed tab panels
2586 * Handle stack layout focus.
2589 * @param {jQuery.Event} e Focusing event
2591 OO
.ui
.IndexLayout
.prototype.onStackLayoutFocus = function ( e
) {
2594 // Find the tab panel that an element was focused within
2595 $target
= $( e
.target
).closest( '.oo-ui-tabPanelLayout' );
2596 for ( name
in this.tabPanels
) {
2597 // Check for tab panel match, exclude current tab panel to find only tab panel changes
2598 if ( this.tabPanels
[ name
].$element
[ 0 ] === $target
[ 0 ] &&
2599 name
!== this.currentTabPanelName
) {
2600 this.setTabPanel( name
);
2607 * Handle stack layout set events.
2610 * @param {OO.ui.PanelLayout|null} tabPanel The tab panel that is now the current panel
2612 OO
.ui
.IndexLayout
.prototype.onStackLayoutSet = function ( tabPanel
) {
2613 // If everything is unselected, do nothing
2617 // Focus the first element on the newly selected panel
2618 if ( this.autoFocus
&& !OO
.ui
.isMobile() ) {
2624 * Focus the first input in the current tab panel.
2626 * If no tab panel is selected, the first selectable tab panel will be selected.
2627 * If the focus is already in an element on the current tab panel, nothing will happen.
2629 * @param {number} [itemIndex] A specific item to focus on
2631 OO
.ui
.IndexLayout
.prototype.focus = function ( itemIndex
) {
2633 items
= this.stackLayout
.getItems();
2635 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2636 tabPanel
= items
[ itemIndex
];
2638 tabPanel
= this.stackLayout
.getCurrentItem();
2642 this.selectFirstSelectableTabPanel();
2643 tabPanel
= this.stackLayout
.getCurrentItem();
2648 // Only change the focus if is not already in the current page
2649 if ( !OO
.ui
.contains(
2650 tabPanel
.$element
[ 0 ],
2651 this.getElementDocument().activeElement
,
2659 * Find the first focusable input in the index layout and focus
2662 OO
.ui
.IndexLayout
.prototype.focusFirstFocusable = function () {
2663 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2667 * Handle tab widget select events.
2670 * @param {OO.ui.OptionWidget|null} item Selected item
2672 OO
.ui
.IndexLayout
.prototype.onTabSelectWidgetSelect = function ( item
) {
2674 this.setTabPanel( item
.getData() );
2679 * Get the tab panel closest to the specified tab panel.
2681 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2682 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2684 OO
.ui
.IndexLayout
.prototype.getClosestTabPanel = function ( tabPanel
) {
2685 var next
, prev
, level
,
2686 tabPanels
= this.stackLayout
.getItems(),
2687 index
= tabPanels
.indexOf( tabPanel
);
2689 if ( index
!== -1 ) {
2690 next
= tabPanels
[ index
+ 1 ];
2691 prev
= tabPanels
[ index
- 1 ];
2692 // Prefer adjacent tab panels at the same level
2693 level
= this.tabSelectWidget
.findItemFromData( tabPanel
.getName() ).getLevel();
2696 level
=== this.tabSelectWidget
.findItemFromData( prev
.getName() ).getLevel()
2702 level
=== this.tabSelectWidget
.findItemFromData( next
.getName() ).getLevel()
2707 return prev
|| next
|| null;
2711 * Get the tabs widget.
2713 * @return {OO.ui.TabSelectWidget} Tabs widget
2715 OO
.ui
.IndexLayout
.prototype.getTabs = function () {
2716 return this.tabSelectWidget
;
2720 * Get a tab panel by its symbolic name.
2722 * @param {string} name Symbolic name of tab panel
2723 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2725 OO
.ui
.IndexLayout
.prototype.getTabPanel = function ( name
) {
2726 return this.tabPanels
[ name
];
2730 * Get the current tab panel.
2732 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2734 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanel = function () {
2735 var name
= this.getCurrentTabPanelName();
2736 return name
? this.getTabPanel( name
) : undefined;
2740 * Get the symbolic name of the current tab panel.
2742 * @return {string|null} Symbolic name of the current tab panel
2744 OO
.ui
.IndexLayout
.prototype.getCurrentTabPanelName = function () {
2745 return this.currentTabPanelName
;
2749 * Add tab panels to the index layout.
2751 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2752 * will be automatically removed before the new tab panels are added.
2754 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2755 * @param {number} index Index of the insertion point
2758 * @return {OO.ui.BookletLayout} The layout, for chaining
2760 OO
.ui
.IndexLayout
.prototype.addTabPanels = function ( tabPanels
, index
) {
2761 var i
, len
, name
, tabPanel
, item
, currentIndex
,
2762 stackLayoutTabPanels
= this.stackLayout
.getItems(),
2766 // Remove tab panels with same names
2767 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2768 tabPanel
= tabPanels
[ i
];
2769 name
= tabPanel
.getName();
2771 if ( Object
.prototype.hasOwnProperty
.call( this.tabPanels
, name
) ) {
2772 // Correct the insertion index
2773 currentIndex
= stackLayoutTabPanels
.indexOf( this.tabPanels
[ name
] );
2774 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2777 remove
.push( this.tabPanels
[ name
] );
2780 if ( remove
.length
) {
2781 this.removeTabPanels( remove
);
2784 // Add new tab panels
2785 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2786 tabPanel
= tabPanels
[ i
];
2787 name
= tabPanel
.getName();
2788 this.tabPanels
[ tabPanel
.getName() ] = tabPanel
;
2789 item
= new OO
.ui
.TabOptionWidget( { data
: name
} );
2790 tabPanel
.setTabItem( item
);
2794 if ( items
.length
) {
2795 this.tabSelectWidget
.addItems( items
, index
);
2796 this.selectFirstSelectableTabPanel();
2798 this.stackLayout
.addItems( tabPanels
, index
);
2799 this.emit( 'add', tabPanels
, index
);
2805 * Remove the specified tab panels from the index layout.
2807 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2809 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2812 * @return {OO.ui.BookletLayout} The layout, for chaining
2814 OO
.ui
.IndexLayout
.prototype.removeTabPanels = function ( tabPanels
) {
2815 var i
, len
, name
, tabPanel
,
2818 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2819 tabPanel
= tabPanels
[ i
];
2820 name
= tabPanel
.getName();
2821 delete this.tabPanels
[ name
];
2822 items
.push( this.tabSelectWidget
.findItemFromData( name
) );
2823 tabPanel
.setTabItem( null );
2825 if ( items
.length
) {
2826 this.tabSelectWidget
.removeItems( items
);
2827 this.selectFirstSelectableTabPanel();
2829 this.stackLayout
.removeItems( tabPanels
);
2830 this.emit( 'remove', tabPanels
);
2836 * Clear all tab panels from the index layout.
2838 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2842 * @return {OO.ui.BookletLayout} The layout, for chaining
2844 OO
.ui
.IndexLayout
.prototype.clearTabPanels = function () {
2846 tabPanels
= this.stackLayout
.getItems();
2848 this.tabPanels
= {};
2849 this.currentTabPanelName
= null;
2850 this.tabSelectWidget
.clearItems();
2851 for ( i
= 0, len
= tabPanels
.length
; i
< len
; i
++ ) {
2852 tabPanels
[ i
].setTabItem( null );
2854 this.stackLayout
.clearItems();
2856 this.emit( 'remove', tabPanels
);
2862 * Set the current tab panel by symbolic name.
2865 * @param {string} name Symbolic name of tab panel
2867 OO
.ui
.IndexLayout
.prototype.setTabPanel = function ( name
) {
2871 tabPanel
= this.tabPanels
[ name
];
2873 if ( name
!== this.currentTabPanelName
) {
2874 previousTabPanel
= this.getCurrentTabPanel();
2875 selectedItem
= this.tabSelectWidget
.findSelectedItem();
2876 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2877 this.tabSelectWidget
.selectItemByData( name
);
2880 if ( previousTabPanel
) {
2881 previousTabPanel
.setActive( false );
2882 // Blur anything focused if the next tab panel doesn't have anything focusable.
2883 // This is not needed if the next tab panel has something focusable (because once
2884 // it is focused this blur happens automatically). If the layout is non-continuous,
2885 // this check is meaningless because the next tab panel is not visible yet and thus
2886 // can't hold focus.
2889 !OO
.ui
.isMobile() &&
2890 this.stackLayout
.continuous
&&
2891 OO
.ui
.findFocusable( tabPanel
.$element
).length
!== 0
2893 $focused
= previousTabPanel
.$element
.find( ':focus' );
2894 if ( $focused
.length
) {
2895 $focused
[ 0 ].blur();
2899 this.currentTabPanelName
= name
;
2900 tabPanel
.setActive( true );
2901 this.stackLayout
.setItem( tabPanel
);
2902 if ( !this.stackLayout
.continuous
&& previousTabPanel
) {
2903 // This should not be necessary, since any inputs on the previous tab panel should
2904 // have been blurred when it was hidden, but browsers are not very consistent about
2906 $focused
= previousTabPanel
.$element
.find( ':focus' );
2907 if ( $focused
.length
) {
2908 $focused
[ 0 ].blur();
2911 this.emit( 'set', tabPanel
);
2917 * Select the first selectable tab panel.
2920 * @return {OO.ui.BookletLayout} The layout, for chaining
2922 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableTabPanel = function () {
2923 if ( !this.tabSelectWidget
.findSelectedItem() ) {
2924 this.tabSelectWidget
.selectItem( this.tabSelectWidget
.findFirstSelectableItem() );
2931 * ToggleWidget implements basic behavior of widgets with an on/off state.
2932 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2936 * @extends OO.ui.Widget
2937 * @mixins OO.ui.mixin.TitledElement
2940 * @param {Object} [config] Configuration options
2941 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2942 * By default, the toggle is in the 'off' state.
2944 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
2945 // Configuration initialization
2946 config
= config
|| {};
2948 // Parent constructor
2949 OO
.ui
.ToggleWidget
.parent
.call( this, config
);
2951 // Mixin constructor
2952 OO
.ui
.mixin
.TitledElement
.call( this, config
);
2958 this.$element
.addClass( 'oo-ui-toggleWidget' );
2959 this.setValue( !!config
.value
);
2964 OO
.inheritClass( OO
.ui
.ToggleWidget
, OO
.ui
.Widget
);
2965 OO
.mixinClass( OO
.ui
.ToggleWidget
, OO
.ui
.mixin
.TitledElement
);
2972 * A change event is emitted when the on/off state of the toggle changes.
2974 * @param {boolean} value Value representing the new state of the toggle
2980 * Get the value representing the toggle’s state.
2982 * @return {boolean} The on/off state of the toggle
2984 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
2989 * Set the state of the toggle: `true` for 'on', `false` for 'off'.
2991 * @param {boolean} value The state of the toggle
2994 * @return {OO.ui.Widget} The widget, for chaining
2996 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
2998 if ( this.value
!== value
) {
3000 this.emit( 'change', value
);
3001 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
3002 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
3008 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
3009 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
3010 * configured with {@link OO.ui.mixin.IconElement icons},
3011 * {@link OO.ui.mixin.IndicatorElement indicators},
3012 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
3013 * and {@link OO.ui.mixin.LabelElement labels}. Please see
3014 * the [OOUI documentation][1] on MediaWiki for more information.
3017 * // Toggle buttons in the 'off' and 'on' state.
3018 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
3019 * label: 'Toggle Button off'
3021 * toggleButton2 = new OO.ui.ToggleButtonWidget( {
3022 * label: 'Toggle Button on',
3025 * // Append the buttons to the DOM.
3026 * $( document.body ).append( toggleButton1.$element, toggleButton2.$element );
3028 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Buttons_and_Switches#Toggle_buttons
3031 * @extends OO.ui.ToggleWidget
3032 * @mixins OO.ui.mixin.ButtonElement
3033 * @mixins OO.ui.mixin.IconElement
3034 * @mixins OO.ui.mixin.IndicatorElement
3035 * @mixins OO.ui.mixin.LabelElement
3036 * @mixins OO.ui.mixin.FlaggedElement
3037 * @mixins OO.ui.mixin.TabIndexedElement
3040 * @param {Object} [config] Configuration options
3041 * @cfg {boolean} [value=false] The toggle button’s initial on/off
3042 * state. By default, the button is in the 'off' state.
3044 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
3045 // Configuration initialization
3046 config
= config
|| {};
3048 // Parent constructor
3049 OO
.ui
.ToggleButtonWidget
.parent
.call( this, config
);
3051 // Mixin constructors
3052 OO
.ui
.mixin
.ButtonElement
.call( this, $.extend( {
3055 OO
.ui
.mixin
.IconElement
.call( this, config
);
3056 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3057 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3058 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3059 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {
3060 $tabIndexed
: this.$button
3064 this.connect( this, {
3069 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3071 .addClass( 'oo-ui-toggleButtonWidget' )
3072 .append( this.$button
);
3073 this.setTitledElement( this.$button
);
3078 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
3079 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
3080 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IconElement
);
3081 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
3082 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.LabelElement
);
3083 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
3084 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3086 /* Static Properties */
3092 OO
.ui
.ToggleButtonWidget
.static.tagName
= 'span';
3097 * Handle the button action being triggered.
3101 OO
.ui
.ToggleButtonWidget
.prototype.onAction = function () {
3102 this.setValue( !this.value
);
3108 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
3110 if ( value
!== this.value
) {
3111 // Might be called from parent constructor before ButtonElement constructor
3112 if ( this.$button
) {
3113 this.$button
.attr( 'aria-pressed', value
.toString() );
3115 this.setActive( value
);
3119 OO
.ui
.ToggleButtonWidget
.parent
.prototype.setValue
.call( this, value
);
3127 OO
.ui
.ToggleButtonWidget
.prototype.setButtonElement = function ( $button
) {
3128 if ( this.$button
) {
3129 this.$button
.removeAttr( 'aria-pressed' );
3131 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement
.call( this, $button
);
3132 this.$button
.attr( 'aria-pressed', this.value
.toString() );
3136 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
3137 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
3138 * visually by a slider in the leftmost position.
3141 * // Toggle switches in the 'off' and 'on' position.
3142 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget(),
3143 * toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
3146 * // Create a FieldsetLayout to layout and label switches.
3147 * fieldset = new OO.ui.FieldsetLayout( {
3148 * label: 'Toggle switches'
3150 * fieldset.addItems( [
3151 * new OO.ui.FieldLayout( toggleSwitch1, {
3155 * new OO.ui.FieldLayout( toggleSwitch2, {
3160 * $( document.body ).append( fieldset.$element );
3163 * @extends OO.ui.ToggleWidget
3164 * @mixins OO.ui.mixin.TabIndexedElement
3167 * @param {Object} [config] Configuration options
3168 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
3169 * By default, the toggle switch is in the 'off' position.
3171 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
3172 // Parent constructor
3173 OO
.ui
.ToggleSwitchWidget
.parent
.call( this, config
);
3175 // Mixin constructors
3176 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3179 this.dragging
= false;
3180 this.dragStart
= null;
3181 this.sliding
= false;
3182 this.$glow
= $( '<span>' );
3183 this.$grip
= $( '<span>' );
3187 click
: this.onClick
.bind( this ),
3188 keypress
: this.onKeyPress
.bind( this )
3192 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
3193 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
3195 .addClass( 'oo-ui-toggleSwitchWidget' )
3196 .attr( 'role', 'checkbox' )
3197 .append( this.$glow
, this.$grip
);
3202 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
3203 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3208 * Handle mouse click events.
3211 * @param {jQuery.Event} e Mouse click event
3212 * @return {undefined/boolean} False to prevent default if event is handled
3214 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
3215 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
3216 this.setValue( !this.value
);
3222 * Handle key press events.
3225 * @param {jQuery.Event} e Key press event
3226 * @return {undefined/boolean} False to prevent default if event is handled
3228 OO
.ui
.ToggleSwitchWidget
.prototype.onKeyPress = function ( e
) {
3229 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
3230 this.setValue( !this.value
);
3238 OO
.ui
.ToggleSwitchWidget
.prototype.setValue = function ( value
) {
3239 OO
.ui
.ToggleSwitchWidget
.parent
.prototype.setValue
.call( this, value
);
3240 this.$element
.attr( 'aria-checked', this.value
.toString() );
3247 OO
.ui
.ToggleSwitchWidget
.prototype.simulateLabelClick = function () {
3248 if ( !this.isDisabled() ) {
3249 this.setValue( !this.value
);
3255 * OutlineControlsWidget is a set of controls for an
3256 * {@link OO.ui.OutlineSelectWidget outline select widget}.
3257 * Controls include moving items up and down, removing items, and adding different kinds of items.
3259 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3262 * @extends OO.ui.Widget
3263 * @mixins OO.ui.mixin.GroupElement
3266 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
3267 * @param {Object} [config] Configuration options
3268 * @cfg {Object} [abilities] List of abilties
3269 * @cfg {boolean} [abilities.move=true] Allow moving movable items
3270 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
3272 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
3273 // Allow passing positional parameters inside the config object
3274 if ( OO
.isPlainObject( outline
) && config
=== undefined ) {
3276 outline
= config
.outline
;
3279 // Configuration initialization
3280 config
= config
|| {};
3282 // Parent constructor
3283 OO
.ui
.OutlineControlsWidget
.parent
.call( this, config
);
3285 // Mixin constructors
3286 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3289 this.outline
= outline
;
3290 this.$movers
= $( '<div>' );
3291 this.upButton
= new OO
.ui
.ButtonWidget( {
3294 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
3296 this.downButton
= new OO
.ui
.ButtonWidget( {
3299 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
3301 this.removeButton
= new OO
.ui
.ButtonWidget( {
3304 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
3306 this.abilities
= { move: true, remove
: true };
3309 outline
.connect( this, {
3310 select
: 'onOutlineChange',
3311 add
: 'onOutlineChange',
3312 remove
: 'onOutlineChange'
3314 this.upButton
.connect( this, {
3315 click
: [ 'emit', 'move', -1 ]
3317 this.downButton
.connect( this, {
3318 click
: [ 'emit', 'move', 1 ]
3320 this.removeButton
.connect( this, {
3321 click
: [ 'emit', 'remove' ]
3325 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
3326 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
3328 .addClass( 'oo-ui-outlineControlsWidget-movers' )
3329 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
3330 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
3331 this.setAbilities( config
.abilities
|| {} );
3336 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
3337 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.GroupElement
);
3343 * @param {number} places Number of places to move
3355 * @param {Object} abilities List of abilties
3356 * @param {boolean} [abilities.move] Allow moving movable items
3357 * @param {boolean} [abilities.remove] Allow removing removable items
3359 OO
.ui
.OutlineControlsWidget
.prototype.setAbilities = function ( abilities
) {
3362 for ( ability
in this.abilities
) {
3363 if ( abilities
[ ability
] !== undefined ) {
3364 this.abilities
[ ability
] = !!abilities
[ ability
];
3368 this.onOutlineChange();
3372 * Handle outline change events.
3376 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
3377 var i
, len
, firstMovable
, lastMovable
,
3378 items
= this.outline
.getItems(),
3379 selectedItem
= this.outline
.findSelectedItem(),
3380 movable
= this.abilities
.move && selectedItem
&& selectedItem
.isMovable(),
3381 removable
= this.abilities
.remove
&& selectedItem
&& selectedItem
.isRemovable();
3386 while ( ++i
< len
) {
3387 if ( items
[ i
].isMovable() ) {
3388 firstMovable
= items
[ i
];
3394 if ( items
[ i
].isMovable() ) {
3395 lastMovable
= items
[ i
];
3400 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
3401 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
3402 this.removeButton
.setDisabled( !removable
);
3406 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3408 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3409 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3413 * @extends OO.ui.DecoratedOptionWidget
3416 * @param {Object} [config] Configuration options
3417 * @cfg {number} [level] Indentation level
3418 * @cfg {boolean} [movable] Allow modification from
3419 * {@link OO.ui.OutlineControlsWidget outline controls}.
3421 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
3422 // Configuration initialization
3423 config
= config
|| {};
3425 // Parent constructor
3426 OO
.ui
.OutlineOptionWidget
.parent
.call( this, config
);
3430 this.movable
= !!config
.movable
;
3431 this.removable
= !!config
.removable
;
3434 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
3435 this.setLevel( config
.level
);
3440 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3442 /* Static Properties */
3448 OO
.ui
.OutlineOptionWidget
.static.highlightable
= true;
3454 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
3459 * @property {string}
3461 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
3466 * @property {number}
3468 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
3473 * Check if item is movable.
3475 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3477 * @return {boolean} Item is movable
3479 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
3480 return this.movable
;
3484 * Check if item is removable.
3486 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3488 * @return {boolean} Item is removable
3490 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
3491 return this.removable
;
3495 * Get indentation level.
3497 * @return {number} Indentation level
3499 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
3506 OO
.ui
.OutlineOptionWidget
.prototype.setPressed = function ( state
) {
3507 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setPressed
.call( this, state
);
3514 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3516 * @param {boolean} movable Item is movable
3518 * @return {OO.ui.Widget} The widget, for chaining
3520 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
3521 this.movable
= !!movable
;
3522 this.updateThemeClasses();
3529 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3531 * @param {boolean} removable Item is removable
3533 * @return {OO.ui.Widget} The widget, for chaining
3535 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
3536 this.removable
= !!removable
;
3537 this.updateThemeClasses();
3544 OO
.ui
.OutlineOptionWidget
.prototype.setSelected = function ( state
) {
3545 OO
.ui
.OutlineOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3550 * Set indentation level.
3552 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3554 * @return {OO.ui.Widget} The widget, for chaining
3556 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
3557 var levels
= this.constructor.static.levels
,
3558 levelClass
= this.constructor.static.levelClass
,
3561 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
3563 if ( this.level
=== i
) {
3564 this.$element
.addClass( levelClass
+ i
);
3566 this.$element
.removeClass( levelClass
+ i
);
3569 this.updateThemeClasses();
3575 * OutlineSelectWidget is a structured list that contains
3576 * {@link OO.ui.OutlineOptionWidget outline options}
3577 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls}
3580 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3583 * @extends OO.ui.SelectWidget
3584 * @mixins OO.ui.mixin.TabIndexedElement
3587 * @param {Object} [config] Configuration options
3589 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
3590 // Parent constructor
3591 OO
.ui
.OutlineSelectWidget
.parent
.call( this, config
);
3593 // Mixin constructors
3594 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3598 focus
: this.bindDocumentKeyDownListener
.bind( this ),
3599 blur
: this.unbindDocumentKeyDownListener
.bind( this )
3603 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
3608 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
3609 OO
.mixinClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3612 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3613 * can be selected and configured with data. The class is
3614 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3615 * [OOUI documentation on MediaWiki] [1] for more information.
3617 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options#Button_selects_and_options
3620 * @extends OO.ui.OptionWidget
3621 * @mixins OO.ui.mixin.ButtonElement
3622 * @mixins OO.ui.mixin.IconElement
3623 * @mixins OO.ui.mixin.IndicatorElement
3626 * @param {Object} [config] Configuration options
3628 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
3629 // Configuration initialization
3630 config
= config
|| {};
3632 // Parent constructor
3633 OO
.ui
.ButtonOptionWidget
.parent
.call( this, config
);
3635 // Mixin constructors
3636 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3637 OO
.ui
.mixin
.IconElement
.call( this, config
);
3638 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3641 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
3642 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3643 this.$element
.append( this.$button
);
3644 this.setTitledElement( this.$button
);
3649 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.OptionWidget
);
3650 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.ButtonElement
);
3651 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IconElement
);
3652 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.IndicatorElement
);
3654 /* Static Properties */
3657 * Allow button mouse down events to pass through so they can be handled by the parent select widget
3662 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
3668 OO
.ui
.ButtonOptionWidget
.static.highlightable
= false;
3675 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
3676 OO
.ui
.ButtonOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3678 if ( this.constructor.static.selectable
) {
3679 this.setActive( state
);
3686 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3687 * button options and is used together with
3688 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3689 * highlighting, choosing, and selecting mutually exclusive options. Please see
3690 * the [OOUI documentation on MediaWiki] [1] for more information.
3693 * // A ButtonSelectWidget that contains three ButtonOptionWidgets.
3694 * var option1 = new OO.ui.ButtonOptionWidget( {
3696 * label: 'Option 1',
3697 * title: 'Button option 1'
3699 * option2 = new OO.ui.ButtonOptionWidget( {
3701 * label: 'Option 2',
3702 * title: 'Button option 2'
3704 * option3 = new OO.ui.ButtonOptionWidget( {
3706 * label: 'Option 3',
3707 * title: 'Button option 3'
3709 * buttonSelect = new OO.ui.ButtonSelectWidget( {
3710 * items: [ option1, option2, option3 ]
3712 * $( document.body ).append( buttonSelect.$element );
3714 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options
3717 * @extends OO.ui.SelectWidget
3718 * @mixins OO.ui.mixin.TabIndexedElement
3721 * @param {Object} [config] Configuration options
3723 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
3724 // Parent constructor
3725 OO
.ui
.ButtonSelectWidget
.parent
.call( this, config
);
3727 // Mixin constructors
3728 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3732 focus
: this.bindDocumentKeyDownListener
.bind( this ),
3733 blur
: this.unbindDocumentKeyDownListener
.bind( this )
3737 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
3742 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
3743 OO
.mixinClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3746 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3748 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3749 * {@link OO.ui.TabPanelLayout tab panel layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3753 * @extends OO.ui.OptionWidget
3756 * @param {Object} [config] Configuration options
3758 OO
.ui
.TabOptionWidget
= function OoUiTabOptionWidget( config
) {
3759 // Configuration initialization
3760 config
= config
|| {};
3762 // Parent constructor
3763 OO
.ui
.TabOptionWidget
.parent
.call( this, config
);
3767 .addClass( 'oo-ui-tabOptionWidget' )
3768 .attr( 'role', 'tab' );
3773 OO
.inheritClass( OO
.ui
.TabOptionWidget
, OO
.ui
.OptionWidget
);
3775 /* Static Properties */
3781 OO
.ui
.TabOptionWidget
.static.highlightable
= false;
3784 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3786 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3789 * @extends OO.ui.SelectWidget
3790 * @mixins OO.ui.mixin.TabIndexedElement
3793 * @param {Object} [config] Configuration options
3795 OO
.ui
.TabSelectWidget
= function OoUiTabSelectWidget( config
) {
3796 // Parent constructor
3797 OO
.ui
.TabSelectWidget
.parent
.call( this, config
);
3799 // Mixin constructors
3800 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3804 focus
: this.bindDocumentKeyDownListener
.bind( this ),
3805 blur
: this.unbindDocumentKeyDownListener
.bind( this )
3810 .addClass( 'oo-ui-tabSelectWidget' )
3811 .attr( 'role', 'tablist' );
3816 OO
.inheritClass( OO
.ui
.TabSelectWidget
, OO
.ui
.SelectWidget
);
3817 OO
.mixinClass( OO
.ui
.TabSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3820 * TagItemWidgets are used within a {@link OO.ui.TagMultiselectWidget
3821 * TagMultiselectWidget} to display the selected items.
3824 * @extends OO.ui.Widget
3825 * @mixins OO.ui.mixin.ItemWidget
3826 * @mixins OO.ui.mixin.LabelElement
3827 * @mixins OO.ui.mixin.FlaggedElement
3828 * @mixins OO.ui.mixin.TabIndexedElement
3829 * @mixins OO.ui.mixin.DraggableElement
3832 * @param {Object} [config] Configuration object
3833 * @cfg {boolean} [valid=true] Item is valid
3834 * @cfg {boolean} [fixed] Item is fixed. This means the item is
3835 * always included in the values and cannot be removed.
3837 OO
.ui
.TagItemWidget
= function OoUiTagItemWidget( config
) {
3838 config
= config
|| {};
3840 // Parent constructor
3841 OO
.ui
.TagItemWidget
.parent
.call( this, config
);
3843 // Mixin constructors
3844 OO
.ui
.mixin
.ItemWidget
.call( this );
3845 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3846 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3847 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3848 OO
.ui
.mixin
.DraggableElement
.call( this, config
);
3850 this.valid
= config
.valid
=== undefined ? true : !!config
.valid
;
3851 this.fixed
= !!config
.fixed
;
3853 this.closeButton
= new OO
.ui
.ButtonWidget( {
3857 title
: OO
.ui
.msg( 'ooui-item-remove' )
3859 this.closeButton
.setDisabled( this.isDisabled() );
3862 this.closeButton
.connect( this, {
3866 .on( 'click', this.select
.bind( this ) )
3867 .on( 'keydown', this.onKeyDown
.bind( this ) )
3868 // Prevent propagation of mousedown; the tag item "lives" in the
3869 // clickable area of the TagMultiselectWidget, which listens to
3870 // mousedown to open the menu or popup. We want to prevent that
3871 // for clicks specifically on the tag itself, so the actions taken
3872 // are more deliberate. When the tag is clicked, it will emit the
3873 // selection event (similar to how #OO.ui.MultioptionWidget emits 'change')
3874 // and can be handled separately.
3875 .on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
3879 .addClass( 'oo-ui-tagItemWidget' )
3880 .append( this.$label
, this.closeButton
.$element
);
3883 /* Initialization */
3885 OO
.inheritClass( OO
.ui
.TagItemWidget
, OO
.ui
.Widget
);
3886 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.ItemWidget
);
3887 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.LabelElement
);
3888 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
3889 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3890 OO
.mixinClass( OO
.ui
.TagItemWidget
, OO
.ui
.mixin
.DraggableElement
);
3897 * A remove action was performed on the item
3902 * @param {string} direction Direction of the movement, forward or backwards
3904 * A navigate action was performed on the item
3910 * The tag widget was selected. This can occur when the widget
3911 * is either clicked or enter was pressed on it.
3916 * @param {boolean} isValid Item is valid
3918 * Item validity has changed
3923 * @param {boolean} isFixed Item is fixed
3925 * Item fixed state has changed
3931 * Set this item as fixed, meaning it cannot be removed
3933 * @param {string} [state] Item is fixed
3935 * @return {OO.ui.Widget} The widget, for chaining
3937 OO
.ui
.TagItemWidget
.prototype.setFixed = function ( state
) {
3938 state
= state
=== undefined ? !this.fixed
: !!state
;
3940 if ( this.fixed
!== state
) {
3942 if ( this.closeButton
) {
3943 this.closeButton
.toggle( !this.fixed
);
3946 if ( !this.fixed
&& this.elementGroup
&& !this.elementGroup
.isDraggable() ) {
3947 // Only enable the state of the item if the
3948 // entire group is draggable
3949 this.toggleDraggable( !this.fixed
);
3951 this.$element
.toggleClass( 'oo-ui-tagItemWidget-fixed', this.fixed
);
3953 this.emit( 'fixed', this.isFixed() );
3959 * Check whether the item is fixed
3962 OO
.ui
.TagItemWidget
.prototype.isFixed = function () {
3969 OO
.ui
.TagItemWidget
.prototype.setDisabled = function ( state
) {
3970 if ( state
&& this.elementGroup
&& !this.elementGroup
.isDisabled() ) {
3971 OO
.ui
.warnDeprecation( 'TagItemWidget#setDisabled: Disabling individual items is deprecated and will result in inconsistent behavior. Use #setFixed instead. See T193571.' );
3974 OO
.ui
.TagItemWidget
.parent
.prototype.setDisabled
.call( this, state
);
3977 // Verify we have a group, and that the widget is ready
3978 this.toggleDraggable
&& this.elementGroup
&&
3980 !this.elementGroup
.isDraggable()
3982 // Only enable the draggable state of the item if the
3983 // entire group is draggable to begin with, and if the
3984 // item is not fixed
3985 this.toggleDraggable( !state
);
3988 if ( this.closeButton
) {
3989 this.closeButton
.setDisabled( state
);
3996 * Handle removal of the item
3998 * This is mainly for extensibility concerns, so other children
3999 * of this class can change the behavior if they need to. This
4000 * is called by both clicking the 'remove' button but also
4001 * on keypress, which is harder to override if needed.
4005 OO
.ui
.TagItemWidget
.prototype.remove = function () {
4006 if ( !this.isDisabled() && !this.isFixed() ) {
4007 this.emit( 'remove' );
4012 * Handle a keydown event on the widget
4016 * @param {jQuery.Event} e Key down event
4017 * @return {boolean|undefined} false to stop the operation
4019 OO
.ui
.TagItemWidget
.prototype.onKeyDown = function ( e
) {
4023 !this.isDisabled() &&
4025 ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
|| e
.keyCode
=== OO
.ui
.Keys
.DELETE
)
4029 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ENTER
) {
4033 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4034 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4036 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
4050 e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
4051 movement
.left
: movement
.right
4062 OO
.ui
.TagItemWidget
.prototype.select = function () {
4063 if ( !this.isDisabled() ) {
4064 this.emit( 'select' );
4069 * Set the valid state of this item
4071 * @param {boolean} [valid] Item is valid
4074 OO
.ui
.TagItemWidget
.prototype.toggleValid = function ( valid
) {
4075 valid
= valid
=== undefined ? !this.valid
: !!valid
;
4077 if ( this.valid
!== valid
) {
4080 this.setFlags( { invalid
: !this.valid
} );
4082 this.emit( 'valid', this.valid
);
4087 * Check whether the item is valid
4089 * @return {boolean} Item is valid
4091 OO
.ui
.TagItemWidget
.prototype.isValid = function () {
4096 * A basic tag multiselect widget, similar in concept to
4097 * {@link OO.ui.ComboBoxInputWidget combo box widget} that allows the user to add multiple values
4098 * that are displayed in a tag area.
4100 * This widget is a base widget; see {@link OO.ui.MenuTagMultiselectWidget MenuTagMultiselectWidget}
4101 * and {@link OO.ui.PopupTagMultiselectWidget PopupTagMultiselectWidget} for the implementations
4102 * that use a menu and a popup respectively.
4105 * // A TagMultiselectWidget.
4106 * var widget = new OO.ui.TagMultiselectWidget( {
4107 * inputPosition: 'outline',
4108 * allowedValues: [ 'Option 1', 'Option 2', 'Option 3' ],
4109 * selected: [ 'Option 1' ]
4111 * $( document.body ).append( widget.$element );
4114 * @extends OO.ui.Widget
4115 * @mixins OO.ui.mixin.GroupWidget
4116 * @mixins OO.ui.mixin.DraggableGroupElement
4117 * @mixins OO.ui.mixin.IndicatorElement
4118 * @mixins OO.ui.mixin.IconElement
4119 * @mixins OO.ui.mixin.TabIndexedElement
4120 * @mixins OO.ui.mixin.FlaggedElement
4121 * @mixins OO.ui.mixin.TitledElement
4124 * @param {Object} config Configuration object
4125 * @cfg {Object} [input] Configuration options for the input widget
4126 * @cfg {OO.ui.InputWidget} [inputWidget] An optional input widget. If given, it will
4127 * replace the input widget used in the TagMultiselectWidget. If not given,
4128 * TagMultiselectWidget creates its own.
4129 * @cfg {boolean} [inputPosition='inline'] Position of the input. Options are:
4130 * - inline: The input is invisible, but exists inside the tag list, so
4131 * the user types into the tag groups to add tags.
4132 * - outline: The input is underneath the tag area.
4133 * - none: No input supplied
4134 * @cfg {boolean} [allowEditTags=true] Allow editing of the tags by clicking them
4135 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if
4136 * not present in the menu.
4137 * @cfg {Object[]} [allowedValues] An array representing the allowed items
4139 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added
4140 * @cfg {boolean} [allowDisplayInvalidTags=false] Allow the display of
4141 * invalid tags. These tags will display with an invalid state, and
4142 * the widget as a whole will have an invalid state if any invalid tags
4144 * @cfg {number} [tagLimit] An optional limit on the number of selected options.
4145 * If 'tagLimit' is set and is reached, the input is disabled, not allowing any
4146 * additions. If 'tagLimit' is unset or is 0, an unlimited number of items can be
4148 * @cfg {boolean} [allowReordering=true] Allow reordering of the items
4149 * @cfg {Object[]|String[]} [selected] A set of selected tags. If given,
4150 * these will appear in the tag list on initialization, as long as they
4151 * pass the validity tests.
4153 OO
.ui
.TagMultiselectWidget
= function OoUiTagMultiselectWidget( config
) {
4155 rAF
= window
.requestAnimationFrame
|| setTimeout
,
4157 $tabFocus
= $( '<span>' ).addClass( 'oo-ui-tagMultiselectWidget-focusTrap' );
4159 config
= config
|| {};
4161 // Parent constructor
4162 OO
.ui
.TagMultiselectWidget
.parent
.call( this, config
);
4164 // Mixin constructors
4165 OO
.ui
.mixin
.GroupWidget
.call( this, config
);
4166 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
4167 OO
.ui
.mixin
.IconElement
.call( this, config
);
4168 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
4169 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
4170 OO
.ui
.mixin
.DraggableGroupElement
.call( this, config
);
4171 OO
.ui
.mixin
.TitledElement
.call( this, config
);
4173 this.toggleDraggable(
4174 config
.allowReordering
=== undefined ?
4175 true : !!config
.allowReordering
4178 this.inputPosition
=
4179 this.constructor.static.allowedInputPositions
.indexOf( config
.inputPosition
) > -1 ?
4180 config
.inputPosition
: 'inline';
4181 this.allowEditTags
= config
.allowEditTags
=== undefined ? true : !!config
.allowEditTags
;
4182 this.allowArbitrary
= !!config
.allowArbitrary
;
4183 this.allowDuplicates
= !!config
.allowDuplicates
;
4184 this.allowedValues
= config
.allowedValues
|| [];
4185 this.allowDisplayInvalidTags
= config
.allowDisplayInvalidTags
;
4186 this.hasInput
= this.inputPosition
!== 'none';
4187 this.tagLimit
= config
.tagLimit
;
4191 this.$content
= $( '<div>' ).addClass( 'oo-ui-tagMultiselectWidget-content' );
4192 this.$handle
= $( '<div>' )
4193 .addClass( 'oo-ui-tagMultiselectWidget-handle' )
4199 this.$group
.addClass( 'oo-ui-tagMultiselectWidget-group' )
4205 remove
: 'itemRemove',
4206 navigate
: 'itemNavigate',
4207 select
: 'itemSelect',
4210 this.connect( this, {
4211 itemRemove
: 'onTagRemove',
4212 itemSelect
: 'onTagSelect',
4213 itemFixed
: 'onTagFixed',
4214 itemNavigate
: 'onTagNavigate',
4215 change
: 'onChangeTags'
4218 mousedown
: this.onMouseDown
.bind( this )
4223 .addClass( 'oo-ui-tagMultiselectWidget' )
4224 .append( this.$handle
);
4226 if ( this.hasInput
) {
4227 if ( config
.inputWidget
) {
4228 this.input
= config
.inputWidget
;
4230 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
4231 placeholder
: config
.placeholder
,
4232 classes
: [ 'oo-ui-tagMultiselectWidget-input' ]
4233 }, config
.input
) );
4235 this.input
.setDisabled( this.isDisabled() );
4238 focus
: this.onInputFocus
.bind( this ),
4239 blur
: this.onInputBlur
.bind( this ),
4240 'propertychange change click mouseup keydown keyup input cut paste select focus':
4241 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
4242 keydown
: this.onInputKeyDown
.bind( this ),
4243 keypress
: this.onInputKeyPress
.bind( this )
4246 this.input
.$input
.on( inputEvents
);
4247 this.inputPlaceholder
= this.input
.$input
.attr( 'placeholder' );
4249 if ( this.inputPosition
=== 'outline' ) {
4250 // Override max-height for the input widget
4251 // in the case the widget is outline so it can
4252 // stretch all the way if the widget is wide
4253 this.input
.$element
.css( 'max-width', 'inherit' );
4255 .addClass( 'oo-ui-tagMultiselectWidget-outlined' )
4256 .append( this.input
.$element
);
4258 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-inlined' );
4259 // HACK: When the widget is using 'inline' input, the
4260 // behavior needs to only use the $input itself
4261 // so we style and size it accordingly (otherwise
4262 // the styling and sizing can get very convoluted
4263 // when the wrapping divs and other elements)
4264 // We are taking advantage of still being able to
4265 // call the widget itself for operations like
4266 // .getValue() and setDisabled() and .focus() but
4267 // having only the $input attached to the DOM
4268 this.$content
.append( this.input
.$input
);
4271 this.$content
.append( $tabFocus
);
4274 this.setTabIndexedElement(
4280 if ( config
.selected
) {
4281 this.setValue( config
.selected
);
4284 // HACK: Input size needs to be calculated after everything
4287 if ( widget
.hasInput
) {
4288 widget
.updateInputSize();
4293 /* Initialization */
4295 OO
.inheritClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.Widget
);
4296 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.GroupWidget
);
4297 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.DraggableGroupElement
);
4298 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IndicatorElement
);
4299 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.IconElement
);
4300 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
4301 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.FlaggedElement
);
4302 OO
.mixinClass( OO
.ui
.TagMultiselectWidget
, OO
.ui
.mixin
.TitledElement
);
4304 /* Static properties */
4307 * Allowed input positions.
4308 * - inline: The input is inside the tag list
4309 * - outline: The input is under the tag list
4310 * - none: There is no input
4314 OO
.ui
.TagMultiselectWidget
.static.allowedInputPositions
= [ 'inline', 'outline', 'none' ];
4319 * Handle mouse down events.
4322 * @param {jQuery.Event} e Mouse down event
4323 * @return {boolean} False to prevent defaults
4325 OO
.ui
.TagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
4327 !this.isDisabled() &&
4328 ( !this.hasInput
|| e
.target
!== this.input
.$input
[ 0 ] ) &&
4329 e
.which
=== OO
.ui
.MouseButtons
.LEFT
4337 * Handle key press events.
4340 * @param {jQuery.Event} e Key press event
4341 * @return {boolean} Whether to prevent defaults
4343 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyPress = function ( e
) {
4345 withMetaKey
= e
.metaKey
|| e
.ctrlKey
;
4347 if ( !this.isDisabled() ) {
4348 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
4349 stopOrContinue
= this.doInputEnter( e
, withMetaKey
);
4352 // Make sure the input gets resized.
4353 setTimeout( this.updateInputSize
.bind( this ), 0 );
4354 return stopOrContinue
;
4359 * Handle key down events.
4362 * @param {jQuery.Event} e Key down event
4365 OO
.ui
.TagMultiselectWidget
.prototype.onInputKeyDown = function ( e
) {
4366 var movement
, direction
,
4368 withMetaKey
= e
.metaKey
|| e
.ctrlKey
,
4369 isMovementInsideInput = function ( direction
) {
4370 var inputRange
= widget
.input
.getRange(),
4371 inputValue
= widget
.hasInput
&& widget
.input
.getValue();
4373 if ( direction
=== 'forwards' && inputRange
.to
> inputValue
.length
- 1 ) {
4377 if ( direction
=== 'backwards' && inputRange
.from <= 0 ) {
4384 if ( !this.isDisabled() ) {
4385 // 'keypress' event is not triggered for Backspace key
4386 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
) {
4387 return this.doInputBackspace( e
, withMetaKey
);
4388 } else if ( e
.keyCode
=== OO
.ui
.Keys
.ESCAPE
) {
4389 return this.doInputEscape( e
);
4391 e
.keyCode
=== OO
.ui
.Keys
.LEFT
||
4392 e
.keyCode
=== OO
.ui
.Keys
.RIGHT
4394 if ( OO
.ui
.Element
.static.getDir( this.$element
) === 'rtl' ) {
4405 direction
= e
.keyCode
=== OO
.ui
.Keys
.LEFT
?
4406 movement
.left
: movement
.right
;
4408 if ( !this.hasInput
|| !isMovementInsideInput( direction
) ) {
4409 return this.doInputArrow( e
, direction
, withMetaKey
);
4416 * Respond to input focus event
4418 OO
.ui
.TagMultiselectWidget
.prototype.onInputFocus = function () {
4419 this.$element
.addClass( 'oo-ui-tagMultiselectWidget-focus' );
4421 this.toggleValid( true );
4425 * Respond to input blur event
4427 OO
.ui
.TagMultiselectWidget
.prototype.onInputBlur = function () {
4428 this.$element
.removeClass( 'oo-ui-tagMultiselectWidget-focus' );
4430 // Set the widget as invalid if there's text in the input
4431 this.addTagFromInput();
4432 this.toggleValid( this.checkValidity() && ( !this.hasInput
|| !this.input
.getValue() ) );
4436 * Perform an action after the Enter key on the input
4438 * @param {jQuery.Event} e Event data
4439 * @param {boolean} [withMetaKey] Whether this key was pressed with
4440 * a meta key like Control
4441 * @return {boolean} Whether to prevent defaults
4443 OO
.ui
.TagMultiselectWidget
.prototype.doInputEnter = function () {
4444 this.addTagFromInput();
4449 * Perform an action responding to the Enter key on the input
4451 * @param {jQuery.Event} e Event data
4452 * @param {boolean} [withMetaKey] Whether this key was pressed with
4453 * a meta key like Control
4454 * @return {boolean} Whether to prevent defaults
4456 OO
.ui
.TagMultiselectWidget
.prototype.doInputBackspace = function ( e
, withMetaKey
) {
4460 this.inputPosition
=== 'inline' &&
4461 this.input
.getValue() === '' &&
4464 // Delete the last item
4465 items
= this.getItems();
4466 item
= items
[ items
.length
- 1 ];
4468 if ( !item
.isDisabled() && !item
.isFixed() ) {
4469 this.removeItems( [ item
] );
4470 // If Ctrl/Cmd was pressed, delete item entirely.
4471 // Otherwise put it into the text field for editing.
4472 if ( !withMetaKey
) {
4473 this.input
.setValue( item
.getLabel() );
4482 * Perform an action after the Escape key on the input
4484 * @param {jQuery.Event} e Event data
4486 OO
.ui
.TagMultiselectWidget
.prototype.doInputEscape = function () {
4491 * Perform an action after the Left/Right arrow key on the input, select the previous
4492 * item from the input.
4493 * See #getPreviousItem
4495 * @param {jQuery.Event} e Event data
4496 * @param {string} direction Direction of the movement; forwards or backwards
4497 * @param {boolean} [withMetaKey] Whether this key was pressed with
4498 * a meta key like Control
4500 OO
.ui
.TagMultiselectWidget
.prototype.doInputArrow = function ( e
, direction
) {
4502 this.inputPosition
=== 'inline' &&
4504 direction
=== 'backwards'
4506 // Get previous item
4507 this.getPreviousItem().focus();
4512 * Respond to item select event
4514 * @param {OO.ui.TagItemWidget} item Selected item
4516 OO
.ui
.TagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
4517 if ( this.hasInput
&& this.allowEditTags
&& !item
.isFixed() ) {
4518 if ( this.input
.getValue() ) {
4519 this.addTagFromInput();
4521 // 1. Get the label of the tag into the input
4522 this.input
.setValue( item
.getData() );
4523 // 2. Remove the tag
4524 this.removeItems( [ item
] );
4525 // 3. Focus the input
4531 * Respond to item fixed state change
4533 * @param {OO.ui.TagItemWidget} item Selected item
4535 OO
.ui
.TagMultiselectWidget
.prototype.onTagFixed = function ( item
) {
4537 items
= this.getItems();
4539 // Move item to the end of the static items
4540 for ( i
= 0; i
< items
.length
; i
++ ) {
4541 if ( items
[ i
] !== item
&& !items
[ i
].isFixed() ) {
4545 this.addItems( item
, i
);
4548 * Respond to change event, where items were added, removed, or cleared.
4550 OO
.ui
.TagMultiselectWidget
.prototype.onChangeTags = function () {
4551 var isUnderLimit
= this.isUnderLimit();
4555 this.checkValidity() &&
4556 !( this.hasInput
&& this.input
.getValue() )
4559 if ( this.hasInput
) {
4560 this.updateInputSize();
4561 if ( !isUnderLimit
) {
4563 this.input
.setValue( '' );
4565 if ( this.inputPosition
=== 'outline' ) {
4566 // Show/clear the placeholder and enable/disable the input
4567 // based on whether we are/aren't under the specified limit
4568 this.input
.$input
.attr( 'placeholder', isUnderLimit
? this.inputPlaceholder
: '' );
4569 this.input
.setDisabled( !isUnderLimit
);
4571 // Show/hide the input
4572 this.input
.$input
.toggleClass( 'oo-ui-element-hidden', !isUnderLimit
);
4575 this.updateIfHeightChanged();
4581 OO
.ui
.TagMultiselectWidget
.prototype.setDisabled = function ( isDisabled
) {
4583 OO
.ui
.TagMultiselectWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
4585 if ( this.hasInput
&& this.input
) {
4586 if ( !isDisabled
) {
4587 this.updateInputSize();
4589 this.input
.setDisabled( !!isDisabled
&& !this.isUnderLimit() );
4593 this.getItems().forEach( function ( item
) {
4594 item
.setDisabled( !!isDisabled
);
4600 * Respond to tag remove event
4601 * @param {OO.ui.TagItemWidget} item Removed tag
4603 OO
.ui
.TagMultiselectWidget
.prototype.onTagRemove = function ( item
) {
4604 this.removeTagByData( item
.getData() );
4608 * Respond to navigate event on the tag
4610 * @param {OO.ui.TagItemWidget} item Removed tag
4611 * @param {string} direction Direction of movement; 'forwards' or 'backwards'
4613 OO
.ui
.TagMultiselectWidget
.prototype.onTagNavigate = function ( item
, direction
) {
4614 var firstItem
= this.getItems()[ 0 ];
4616 if ( direction
=== 'forwards' ) {
4617 this.getNextItem( item
).focus();
4618 } else if ( !this.inputPosition
=== 'inline' || item
!== firstItem
) {
4619 // If the widget has an inline input, we want to stop at the starting edge
4621 this.getPreviousItem( item
).focus();
4626 * Add tag from input value
4628 OO
.ui
.TagMultiselectWidget
.prototype.addTagFromInput = function () {
4629 var val
= this.input
.getValue(),
4630 isValid
= this.isAllowedData( val
);
4636 if ( isValid
|| this.allowDisplayInvalidTags
) {
4645 OO
.ui
.TagMultiselectWidget
.prototype.clearInput = function () {
4646 this.input
.setValue( '' );
4650 * Check whether the given value is a duplicate of an existing
4651 * tag already in the list.
4653 * @param {string|Object} data Requested value
4654 * @return {boolean} Value is duplicate
4656 OO
.ui
.TagMultiselectWidget
.prototype.isDuplicateData = function ( data
) {
4657 return !!this.findItemFromData( data
);
4661 * Check whether a given value is allowed to be added
4663 * @param {string|Object} data Requested value
4664 * @return {boolean} Value is allowed
4666 OO
.ui
.TagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
4668 !this.allowDuplicates
&&
4669 this.isDuplicateData( data
)
4674 if ( this.allowArbitrary
) {
4678 // Check with allowed values
4680 this.getAllowedValues().some( function ( value
) {
4681 return data
=== value
;
4691 * Get the allowed values list
4693 * @return {string[]} Allowed data values
4695 OO
.ui
.TagMultiselectWidget
.prototype.getAllowedValues = function () {
4696 return this.allowedValues
;
4700 * Add a value to the allowed values list
4702 * @param {string} value Allowed data value
4704 OO
.ui
.TagMultiselectWidget
.prototype.addAllowedValue = function ( value
) {
4705 if ( this.allowedValues
.indexOf( value
) === -1 ) {
4706 this.allowedValues
.push( value
);
4711 * Get the datas of the currently selected items
4713 * @return {string[]|Object[]} Datas of currently selected items
4715 OO
.ui
.TagMultiselectWidget
.prototype.getValue = function () {
4716 return this.getItems()
4717 .filter( function ( item
) {
4718 return item
.isValid();
4720 .map( function ( item
) {
4721 return item
.getData();
4726 * Set the value of this widget by datas.
4728 * @param {string|string[]|Object|Object[]} valueObject An object representing the data
4729 * and label of the value. If the widget allows arbitrary values,
4730 * the items will be added as-is. Otherwise, the data value will
4731 * be checked against allowedValues.
4732 * This object must contain at least a data key. Example:
4733 * { data: 'foo', label: 'Foo item' }
4734 * For multiple items, use an array of objects. For example:
4736 * { data: 'foo', label: 'Foo item' },
4737 * { data: 'bar', label: 'Bar item' }
4739 * Value can also be added with plaintext array, for example:
4740 * [ 'foo', 'bar', 'bla' ] or a single string, like 'foo'
4742 OO
.ui
.TagMultiselectWidget
.prototype.setValue = function ( valueObject
) {
4743 valueObject
= Array
.isArray( valueObject
) ? valueObject
: [ valueObject
];
4746 valueObject
.forEach( function ( obj
) {
4747 if ( typeof obj
=== 'string' ) {
4750 this.addTag( obj
.data
, obj
.label
);
4756 * Add tag to the display area
4758 * @param {string|Object} data Tag data
4759 * @param {string} [label] Tag label. If no label is provided, the
4760 * stringified version of the data will be used instead.
4761 * @return {boolean} Item was added successfully
4763 OO
.ui
.TagMultiselectWidget
.prototype.addTag = function ( data
, label
) {
4765 isValid
= this.isAllowedData( data
);
4767 if ( this.isUnderLimit() && ( isValid
|| this.allowDisplayInvalidTags
) ) {
4768 newItemWidget
= this.createTagItemWidget( data
, label
);
4769 newItemWidget
.toggleValid( isValid
);
4770 this.addItems( [ newItemWidget
] );
4778 * Check whether the number of current tags is within the limit.
4780 * @return {boolean} True if current tag count is within the limit or
4781 * if 'tagLimit' is not set
4783 OO
.ui
.TagMultiselectWidget
.prototype.isUnderLimit = function () {
4784 return !this.tagLimit
||
4785 this.getItemCount() < this.tagLimit
;
4789 * Remove tag by its data property.
4791 * @param {string|Object} data Tag data
4793 OO
.ui
.TagMultiselectWidget
.prototype.removeTagByData = function ( data
) {
4794 var item
= this.findItemFromData( data
);
4796 this.removeItems( [ item
] );
4800 * Construct a OO.ui.TagItemWidget (or a subclass thereof) from given label and data.
4803 * @param {string} data Item data
4804 * @param {string} label The label text.
4805 * @return {OO.ui.TagItemWidget}
4807 OO
.ui
.TagMultiselectWidget
.prototype.createTagItemWidget = function ( data
, label
) {
4808 label
= label
|| data
;
4810 return new OO
.ui
.TagItemWidget( { data
: data
, label
: label
} );
4814 * Given an item, returns the item after it. If the item is already the
4815 * last item, return `this.input`. If no item is passed, returns the
4819 * @param {OO.ui.TagItemWidget} [item] Tag item
4820 * @return {OO.ui.Widget} The next widget available.
4822 OO
.ui
.TagMultiselectWidget
.prototype.getNextItem = function ( item
) {
4823 var itemIndex
= this.items
.indexOf( item
);
4825 if ( item
=== undefined || itemIndex
=== -1 ) {
4826 return this.items
[ 0 ];
4829 if ( itemIndex
=== this.items
.length
- 1 ) { // Last item
4830 if ( this.hasInput
) {
4833 // Return first item
4834 return this.items
[ 0 ];
4837 return this.items
[ itemIndex
+ 1 ];
4842 * Given an item, returns the item before it. If the item is already the
4843 * first item, return `this.input`. If no item is passed, returns the
4847 * @param {OO.ui.TagItemWidget} [item] Tag item
4848 * @return {OO.ui.Widget} The previous widget available.
4850 OO
.ui
.TagMultiselectWidget
.prototype.getPreviousItem = function ( item
) {
4851 var itemIndex
= this.items
.indexOf( item
);
4853 if ( item
=== undefined || itemIndex
=== -1 ) {
4854 return this.items
[ this.items
.length
- 1 ];
4857 if ( itemIndex
=== 0 ) {
4858 if ( this.hasInput
) {
4861 // Return the last item
4862 return this.items
[ this.items
.length
- 1 ];
4865 return this.items
[ itemIndex
- 1 ];
4870 * Update the dimensions of the text input field to encompass all available area.
4871 * This is especially relevant for when the input is at the edge of a line
4872 * and should get smaller. The usual operation (as an inline-block with min-width)
4873 * does not work in that case, pushing the input downwards to the next line.
4877 OO
.ui
.TagMultiselectWidget
.prototype.updateInputSize = function () {
4878 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
4879 if ( this.inputPosition
=== 'inline' && !this.isDisabled() ) {
4880 if ( this.input
.$input
[ 0 ].scrollWidth
=== 0 ) {
4881 // Input appears to be attached but not visible.
4882 // Don't attempt to adjust its size, because our measurements
4883 // are going to fail anyway.
4886 this.input
.$input
.css( 'width', '1em' );
4887 $lastItem
= this.$group
.children().last();
4888 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
4890 // Get the width of the input with the placeholder text as
4891 // the value and save it so that we don't keep recalculating
4893 this.contentWidthWithPlaceholder
=== undefined &&
4894 this.input
.getValue() === '' &&
4895 this.input
.$input
.attr( 'placeholder' ) !== undefined
4897 this.input
.setValue( this.input
.$input
.attr( 'placeholder' ) );
4898 this.contentWidthWithPlaceholder
= this.input
.$input
[ 0 ].scrollWidth
;
4899 this.input
.setValue( '' );
4903 // Always keep the input wide enough for the placeholder text
4904 contentWidth
= Math
.max(
4905 this.input
.$input
[ 0 ].scrollWidth
,
4906 // undefined arguments in Math.max lead to NaN
4907 ( this.contentWidthWithPlaceholder
=== undefined ) ?
4908 0 : this.contentWidthWithPlaceholder
4910 currentWidth
= this.input
.$input
.width();
4912 if ( contentWidth
< currentWidth
) {
4913 this.updateIfHeightChanged();
4914 // All is fine, don't perform expensive calculations
4918 if ( $lastItem
.length
=== 0 ) {
4919 bestWidth
= this.$content
.innerWidth();
4921 bestWidth
= direction
=== 'ltr' ?
4922 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
4923 $lastItem
.position().left
;
4926 // Some safety margin for sanity, because I *really* don't feel like finding out where the
4927 // few pixels this is off by are coming from.
4929 if ( contentWidth
> bestWidth
) {
4930 // This will result in the input getting shifted to the next line
4931 bestWidth
= this.$content
.innerWidth() - 13;
4933 this.input
.$input
.width( Math
.floor( bestWidth
) );
4934 this.updateIfHeightChanged();
4936 this.updateIfHeightChanged();
4941 * Determine if widget height changed, and if so,
4942 * emit the resize event. This is useful for when there are either
4943 * menus or popups attached to the bottom of the widget, to allow
4944 * them to change their positioning in case the widget moved down
4949 OO
.ui
.TagMultiselectWidget
.prototype.updateIfHeightChanged = function () {
4950 var height
= this.$element
.height();
4951 if ( height
!== this.height
) {
4952 this.height
= height
;
4953 this.emit( 'resize' );
4958 * Check whether all items in the widget are valid
4960 * @return {boolean} Widget is valid
4962 OO
.ui
.TagMultiselectWidget
.prototype.checkValidity = function () {
4963 return this.getItems().every( function ( item
) {
4964 return item
.isValid();
4969 * Set the valid state of this item
4971 * @param {boolean} [valid] Item is valid
4974 OO
.ui
.TagMultiselectWidget
.prototype.toggleValid = function ( valid
) {
4975 valid
= valid
=== undefined ? !this.valid
: !!valid
;
4977 if ( this.valid
!== valid
) {
4980 this.setFlags( { invalid
: !this.valid
} );
4982 this.emit( 'valid', this.valid
);
4987 * Get the current valid state of the widget
4989 * @return {boolean} Widget is valid
4991 OO
.ui
.TagMultiselectWidget
.prototype.isValid = function () {
4996 * PopupTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget}
4997 * intended to use a popup. The popup can be configured to have a default input to insert values
5001 * // A PopupTagMultiselectWidget.
5002 * var widget = new OO.ui.PopupTagMultiselectWidget();
5003 * $( document.body ).append( widget.$element );
5005 * // Example: A PopupTagMultiselectWidget with an external popup.
5006 * var popupInput = new OO.ui.TextInputWidget(),
5007 * widget = new OO.ui.PopupTagMultiselectWidget( {
5008 * popupInput: popupInput,
5010 * $content: popupInput.$element
5013 * $( document.body ).append( widget.$element );
5016 * @extends OO.ui.TagMultiselectWidget
5017 * @mixins OO.ui.mixin.PopupElement
5019 * @param {Object} config Configuration object
5020 * @cfg {jQuery} [$overlay] An overlay for the popup.
5021 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
5022 * @cfg {Object} [popup] Configuration options for the popup
5023 * @cfg {OO.ui.InputWidget} [popupInput] An input widget inside the popup that will be
5024 * focused when the popup is opened and will be used as replacement for the
5025 * general input in the widget.
5028 OO
.ui
.PopupTagMultiselectWidget
= function OoUiPopupTagMultiselectWidget( config
) {
5030 defaultConfig
= { popup
: {} };
5032 config
= config
|| {};
5034 // Parent constructor
5035 OO
.ui
.PopupTagMultiselectWidget
.parent
.call( this, $.extend( {
5036 inputPosition
: 'none'
5039 this.$overlay
= ( config
.$overlay
=== true ?
5040 OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
5042 if ( !config
.popup
) {
5043 // For the default base implementation, we give a popup
5044 // with an input widget inside it. For any other use cases
5045 // the popup needs to be populated externally and the
5046 // event handled to add tags separately and manually
5047 defaultInput
= new OO
.ui
.TextInputWidget();
5049 defaultConfig
.popupInput
= defaultInput
;
5050 defaultConfig
.popup
.$content
= defaultInput
.$element
;
5051 defaultConfig
.popup
.padded
= true;
5053 this.$element
.addClass( 'oo-ui-popupTagMultiselectWidget-defaultPopup' );
5056 // Add overlay, and add that to the autoCloseIgnore
5057 defaultConfig
.popup
.$overlay
= this.$overlay
;
5058 defaultConfig
.popup
.$autoCloseIgnore
= this.hasInput
?
5059 this.input
.$element
.add( this.$overlay
) : this.$overlay
;
5061 // Allow extending any of the above
5062 config
= $.extend( defaultConfig
, config
);
5064 // Mixin constructors
5065 OO
.ui
.mixin
.PopupElement
.call( this, config
);
5067 if ( this.hasInput
) {
5068 this.input
.$input
.on( 'focus', this.popup
.toggle
.bind( this.popup
, true ) );
5071 // Configuration options
5072 this.popupInput
= config
.popupInput
;
5073 if ( this.popupInput
) {
5074 this.popupInput
.connect( this, {
5075 enter
: 'onPopupInputEnter'
5080 this.on( 'resize', this.popup
.updateDimensions
.bind( this.popup
) );
5081 this.popup
.connect( this, {
5082 toggle
: 'onPopupToggle'
5084 this.$tabIndexed
.on( 'focus', this.onFocus
.bind( this ) );
5088 .append( this.popup
.$element
)
5089 .addClass( 'oo-ui-popupTagMultiselectWidget' );
5091 // Deprecation warning
5092 OO
.ui
.warnDeprecation( 'PopupTagMultiselectWidget: Deprecated widget. Use MenuTagMultiselectWidget instead. See T208821.' );
5095 /* Initialization */
5097 OO
.inheritClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5098 OO
.mixinClass( OO
.ui
.PopupTagMultiselectWidget
, OO
.ui
.mixin
.PopupElement
);
5103 * Focus event handler.
5107 OO
.ui
.PopupTagMultiselectWidget
.prototype.onFocus = function () {
5108 this.popup
.toggle( true );
5112 * Respond to popup toggle event
5114 * @param {boolean} isVisible Popup is visible
5116 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupToggle = function ( isVisible
) {
5117 if ( isVisible
&& this.popupInput
) {
5118 this.popupInput
.focus();
5123 * Respond to popup input enter event
5125 OO
.ui
.PopupTagMultiselectWidget
.prototype.onPopupInputEnter = function () {
5126 if ( this.popupInput
) {
5127 this.addTagByPopupValue( this.popupInput
.getValue() );
5128 this.popupInput
.setValue( '' );
5135 OO
.ui
.PopupTagMultiselectWidget
.prototype.onTagSelect = function ( item
) {
5136 if ( this.popupInput
&& this.allowEditTags
) {
5137 this.popupInput
.setValue( item
.getData() );
5138 this.removeItems( [ item
] );
5140 this.popup
.toggle( true );
5141 this.popupInput
.focus();
5144 OO
.ui
.PopupTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, item
);
5149 * Add a tag by the popup value.
5150 * Whatever is responsible for setting the value in the popup should call
5151 * this method to add a tag, or use the regular methods like #addTag or
5152 * #setValue directly.
5154 * @param {string} data The value of item
5155 * @param {string} [label] The label of the tag. If not given, the data is used.
5157 OO
.ui
.PopupTagMultiselectWidget
.prototype.addTagByPopupValue = function ( data
, label
) {
5158 this.addTag( data
, label
);
5162 * MenuTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget}
5163 * intended to use a menu of selectable options.
5166 * // A basic MenuTagMultiselectWidget.
5167 * var widget = new OO.ui.MenuTagMultiselectWidget( {
5168 * inputPosition: 'outline',
5170 * { data: 'option1', label: 'Option 1', icon: 'tag' },
5171 * { data: 'option2', label: 'Option 2' },
5172 * { data: 'option3', label: 'Option 3' },
5174 * selected: [ 'option1', 'option2' ]
5176 * $( document.body ).append( widget.$element );
5179 * @extends OO.ui.TagMultiselectWidget
5182 * @param {Object} [config] Configuration object
5183 * @cfg {boolean} [clearInputOnChoose=true] Clear the text input value when a menu option is chosen
5184 * @cfg {Object} [menu] Configuration object for the menu widget
5185 * @cfg {jQuery} [$overlay] An overlay for the menu.
5186 * See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
5187 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
5189 OO
.ui
.MenuTagMultiselectWidget
= function OoUiMenuTagMultiselectWidget( config
) {
5190 config
= config
|| {};
5192 // Parent constructor
5193 OO
.ui
.MenuTagMultiselectWidget
.parent
.call( this, config
);
5195 this.$overlay
= ( config
.$overlay
=== true ?
5196 OO
.ui
.getDefaultOverlay() : config
.$overlay
) || this.$element
;
5197 this.clearInputOnChoose
= config
.clearInputOnChoose
=== undefined ||
5198 !!config
.clearInputOnChoose
;
5199 this.menu
= this.createMenuWidget( $.extend( {
5201 input
: this.hasInput
? this.input
: null,
5202 $input
: this.hasInput
? this.input
.$input
: null,
5203 filterFromInput
: !!this.hasInput
,
5204 highlightOnFilter
: true,
5205 $autoCloseIgnore
: this.hasInput
?
5206 this.input
.$element
: $( [] ),
5207 $floatableContainer
: this.hasInput
&& this.inputPosition
=== 'outline' ?
5208 this.input
.$element
: this.$element
,
5209 $overlay
: this.$overlay
,
5210 disabled
: this.isDisabled()
5212 this.addOptions( config
.options
|| [] );
5215 this.menu
.connect( this, {
5216 choose
: 'onMenuChoose',
5217 toggle
: 'onMenuToggle'
5219 if ( this.hasInput
) {
5220 this.input
.connect( this, {
5221 change
: 'onInputChange'
5224 this.connect( this, {
5229 this.$overlay
.append( this.menu
.$element
);
5230 this.$element
.addClass( 'oo-ui-menuTagMultiselectWidget' );
5231 // Remove MenuSelectWidget's generic focus owner ARIA attribute
5232 // TODO: Should this widget have a `role` that is compatible with this attribute?
5233 this.menu
.$focusOwner
.removeAttr( 'aria-expanded' );
5234 // TagMultiselectWidget already does this, but it doesn't work right because this.menu is
5235 // not yet set up while the parent constructor runs, and #getAllowedValues rejects everything.
5236 if ( config
.selected
) {
5237 this.setValue( config
.selected
);
5241 /* Initialization */
5243 OO
.inheritClass( OO
.ui
.MenuTagMultiselectWidget
, OO
.ui
.TagMultiselectWidget
);
5248 * Respond to resize event
5250 OO
.ui
.MenuTagMultiselectWidget
.prototype.onResize = function () {
5251 // Reposition the menu
5252 this.menu
.position();
5258 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputFocus = function () {
5260 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
5262 this.menu
.toggle( true );
5268 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputBlur = function () {
5270 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onInputBlur
.call( this );
5272 this.menu
.toggle( false );
5276 * Respond to input change event
5278 OO
.ui
.MenuTagMultiselectWidget
.prototype.onInputChange = function () {
5279 this.menu
.toggle( true );
5283 * Respond to menu choose event
5285 * @param {OO.ui.OptionWidget} menuItem Chosen menu item
5287 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuChoose = function ( menuItem
) {
5288 if ( this.hasInput
&& this.clearInputOnChoose
) {
5289 this.input
.setValue( '' );
5292 this.addTag( menuItem
.getData(), menuItem
.getLabel() );
5296 * Respond to menu toggle event. Reset item highlights on hide.
5298 * @param {boolean} isVisible The menu is visible
5300 OO
.ui
.MenuTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
5302 this.menu
.selectItem( null );
5303 this.menu
.highlightItem( null );
5305 setTimeout( function () {
5306 // Remove MenuSelectWidget's generic focus owner ARIA attribute
5307 // TODO: Should this widget have a `role` that is compatible with this attribute?
5308 this.menu
.$focusOwner
.removeAttr( 'aria-expanded' );
5315 OO
.ui
.MenuTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
5316 var menuItem
= this.menu
.findItemFromData( tagItem
.getData() );
5317 if ( !this.allowArbitrary
) {
5318 // Override the base behavior from TagMultiselectWidget; the base behavior
5319 // in TagMultiselectWidget is to remove the tag to edit it in the input,
5320 // but in our case, we want to utilize the menu selection behavior, and
5321 // definitely not remove the item.
5323 // If there is an input that is used for filtering, erase the value so we don't filter
5324 if ( this.hasInput
&& this.menu
.filterFromInput
) {
5325 this.input
.setValue( '' );
5328 // Select the menu item
5329 this.menu
.selectItem( menuItem
);
5334 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
5341 OO
.ui
.MenuTagMultiselectWidget
.prototype.setDisabled = function ( isDisabled
) {
5343 OO
.ui
.MenuTagMultiselectWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
5346 // Protect against calling setDisabled() before the menu was initialized
5347 this.menu
.setDisabled( isDisabled
);
5352 * Highlight the first selectable item in the menu, if configured.
5357 OO
.ui
.MenuTagMultiselectWidget
.prototype.initializeMenuSelection = function () {
5358 if ( !this.menu
.findSelectedItem() ) {
5359 this.menu
.highlightItem(
5360 this.allowArbitrary
?
5362 this.menu
.findFirstSelectableItem()
5370 OO
.ui
.MenuTagMultiselectWidget
.prototype.addTagFromInput = function () {
5371 var val
= this.input
.getValue(),
5372 // Look for a highlighted item first
5373 // Then look for the element that fits the data
5374 item
= this.menu
.findHighlightedItem() || this.menu
.findItemFromData( val
),
5375 data
= item
? item
.getData() : val
,
5376 isValid
= this.isAllowedData( data
);
5378 // Override the parent method so we add from the menu
5379 // rather than directly from the input
5385 if ( isValid
|| this.allowDisplayInvalidTags
) {
5388 this.addTag( data
, item
.getLabel() );
5396 * Return the visible items in the menu. This is mainly used for when
5397 * the menu is filtering results.
5399 * @return {OO.ui.MenuOptionWidget[]} Visible results
5401 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenuVisibleItems = function () {
5402 return this.menu
.getItems().filter( function ( menuItem
) {
5403 return menuItem
.isVisible();
5408 * Create the menu for this widget. This is in a separate method so that
5409 * child classes can override this without polluting the constructor with
5410 * unnecessary extra objects that will be overidden.
5412 * @param {Object} menuConfig Configuration options
5413 * @return {OO.ui.MenuSelectWidget} Menu widget
5415 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
5416 return new OO
.ui
.MenuSelectWidget( menuConfig
);
5420 * Add options to the menu
5422 * @param {Object[]} menuOptions Object defining options
5424 OO
.ui
.MenuTagMultiselectWidget
.prototype.addOptions = function ( menuOptions
) {
5426 items
= menuOptions
.map( function ( obj
) {
5427 return widget
.createMenuOptionWidget( obj
.data
, obj
.label
, obj
.icon
);
5430 this.menu
.addItems( items
);
5434 * Create a menu option widget.
5436 * @param {string} data Item data
5437 * @param {string} [label] Item label
5438 * @param {string} [icon] Symbolic icon name
5439 * @return {OO.ui.OptionWidget} Option widget
5441 OO
.ui
.MenuTagMultiselectWidget
.prototype.createMenuOptionWidget = function ( data
, label
, icon
) {
5442 return new OO
.ui
.MenuOptionWidget( {
5444 label
: label
|| data
,
5452 * @return {OO.ui.MenuSelectWidget} Menu
5454 OO
.ui
.MenuTagMultiselectWidget
.prototype.getMenu = function () {
5459 * Get the allowed values list
5461 * @return {string[]} Allowed data values
5463 OO
.ui
.MenuTagMultiselectWidget
.prototype.getAllowedValues = function () {
5466 // If the parent constructor is calling us, we're not ready yet, this.menu is not set up.
5467 menuDatas
= this.menu
.getItems().map( function ( menuItem
) {
5468 return menuItem
.getData();
5471 return this.allowedValues
.concat( menuDatas
);
5475 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
5476 * widgets can be configured with {@link OO.ui.mixin.IconElement icons}, {@link
5477 * OO.ui.mixin.IndicatorElement indicators} and {@link OO.ui.mixin.TitledElement titles}.
5478 * Please see the [OOUI documentation on MediaWiki] [1] for more information and examples.
5481 * // A file select widget.
5482 * var selectFile = new OO.ui.SelectFileWidget();
5483 * $( document.body ).append( selectFile.$element );
5485 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets
5488 * @extends OO.ui.Widget
5489 * @mixins OO.ui.mixin.IconElement
5490 * @mixins OO.ui.mixin.IndicatorElement
5491 * @mixins OO.ui.mixin.PendingElement
5492 * @mixins OO.ui.mixin.TabIndexedElement
5493 * @mixins OO.ui.mixin.LabelElement
5494 * @mixins OO.ui.mixin.TitledElement
5497 * @param {Object} [config] Configuration options
5498 * @cfg {string[]|null} [accept=null] MIME types to accept. null accepts all types.
5499 * @cfg {string} [placeholder] Text to display when no file is selected.
5500 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
5501 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
5502 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be
5504 * @cfg {number} [thumbnailSizeLimit=20] File size limit in MiB above which to not try and show a
5505 * preview (for performance).
5507 OO
.ui
.SelectFileWidget
= function OoUiSelectFileWidget( config
) {
5510 this.selectButton
= new OO
.ui
.ButtonWidget( {
5511 $element
: $( '<label>' ),
5512 classes
: [ 'oo-ui-selectFileWidget-selectButton' ],
5513 label
: OO
.ui
.msg( 'ooui-selectfile-button-select' )
5516 // Configuration initialization
5517 config
= $.extend( {
5519 placeholder
: OO
.ui
.msg( 'ooui-selectfile-placeholder' ),
5520 notsupported
: OO
.ui
.msg( 'ooui-selectfile-not-supported' ),
5522 showDropTarget
: false,
5523 thumbnailSizeLimit
: 20,
5524 $tabIndexed
: this.selectButton
.$tabIndexed
5527 // Parent constructor
5528 OO
.ui
.SelectFileWidget
.parent
.call( this, config
);
5530 // Mixin constructors
5531 OO
.ui
.mixin
.IconElement
.call( this, config
);
5532 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
5533 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {
5534 $pending
: this.$info
5536 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {
5537 $tabIndexed
: this.selectButton
.$tabIndexed
5539 OO
.ui
.mixin
.LabelElement
.call( this, config
);
5540 OO
.ui
.mixin
.TitledElement
.call( this, config
);
5543 this.$info
= $( '<span>' );
5544 this.showDropTarget
= config
.droppable
&& config
.showDropTarget
;
5545 this.thumbnailSizeLimit
= config
.thumbnailSizeLimit
;
5546 this.isSupported
= this.constructor.static.isSupported();
5547 this.currentFile
= null;
5548 if ( Array
.isArray( config
.accept
) ) {
5549 this.accept
= config
.accept
;
5553 this.placeholder
= config
.placeholder
;
5554 this.notsupported
= config
.notsupported
;
5555 this.onFileSelectedHandler
= this.onFileSelected
.bind( this );
5557 this.selectButton
.setDisabled( this.disabled
|| !this.isSupported
);
5559 this.clearButton
= new OO
.ui
.ButtonWidget( {
5560 classes
: [ 'oo-ui-selectFileWidget-clearButton' ],
5563 disabled
: this.disabled
5567 this.selectButton
.$button
.on( {
5568 keypress
: this.onKeyPress
.bind( this )
5570 this.clearButton
.connect( this, {
5571 click
: 'onClearClick'
5573 if ( config
.droppable
) {
5574 dragHandler
= this.onDragEnterOrOver
.bind( this );
5576 dragenter
: dragHandler
,
5577 dragover
: dragHandler
,
5578 dragleave
: this.onDragLeave
.bind( this ),
5579 drop
: this.onDrop
.bind( this )
5585 this.$label
.addClass( 'oo-ui-selectFileWidget-label' );
5587 .addClass( 'oo-ui-selectFileWidget-info' )
5588 .append( this.$icon
, this.$label
, this.clearButton
.$element
, this.$indicator
);
5590 if ( this.showDropTarget
) {
5591 this.selectButton
.setIcon( 'upload' );
5592 this.$thumbnail
= $( '<div>' ).addClass( 'oo-ui-selectFileWidget-thumbnail' );
5593 this.setPendingElement( this.$thumbnail
);
5595 .addClass( 'oo-ui-selectFileWidget-dropTarget oo-ui-selectFileWidget' )
5597 click
: this.onDropTargetClick
.bind( this )
5602 this.selectButton
.$element
,
5604 .addClass( 'oo-ui-selectFileWidget-dropLabel' )
5605 .text( OO
.ui
.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
5609 .addClass( 'oo-ui-selectFileWidget' )
5610 .append( this.$info
, this.selectButton
.$element
);
5617 OO
.inheritClass( OO
.ui
.SelectFileWidget
, OO
.ui
.Widget
);
5618 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IconElement
);
5619 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IndicatorElement
);
5620 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.PendingElement
);
5621 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.TabIndexedElement
);
5622 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.LabelElement
);
5623 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.TitledElement
);
5625 /* Static Properties */
5628 * Check if this widget is supported
5633 OO
.ui
.SelectFileWidget
.static.isSupported = function () {
5635 if ( OO
.ui
.SelectFileWidget
.static.isSupportedCache
=== null ) {
5636 $input
= $( '<input>' ).attr( 'type', 'file' );
5637 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= $input
[ 0 ].files
!== undefined;
5639 return OO
.ui
.SelectFileWidget
.static.isSupportedCache
;
5642 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= null;
5649 * A change event is emitted when the on/off state of the toggle changes.
5651 * @param {File|null} value New value
5657 * Get the current value of the field
5659 * @return {File|null}
5661 OO
.ui
.SelectFileWidget
.prototype.getValue = function () {
5662 return this.currentFile
;
5666 * Set the current value of the field
5668 * @param {File|null} file File to select
5670 OO
.ui
.SelectFileWidget
.prototype.setValue = function ( file
) {
5671 if ( this.currentFile
!== file
) {
5672 this.currentFile
= file
;
5674 this.emit( 'change', this.currentFile
);
5679 * Update the user interface when a file is selected or unselected
5683 OO
.ui
.SelectFileWidget
.prototype.updateUI = function () {
5685 if ( !this.isSupported
) {
5686 this.$element
.addClass( 'oo-ui-selectFileWidget-notsupported' );
5687 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
5688 this.setLabel( this.notsupported
);
5690 this.$element
.addClass( 'oo-ui-selectFileWidget-supported' );
5691 if ( this.currentFile
) {
5692 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
5694 $label
= $label
.add(
5696 .addClass( 'oo-ui-selectFileWidget-fileName' )
5697 .text( this.currentFile
.name
)
5699 this.setLabel( $label
);
5701 if ( this.showDropTarget
) {
5703 this.loadAndGetImageUrl().done( function ( url
) {
5704 this.$thumbnail
.css( 'background-image', 'url( ' + url
+ ' )' );
5705 }.bind( this ) ).fail( function () {
5706 this.$thumbnail
.append(
5707 new OO
.ui
.IconWidget( {
5709 classes
: [ 'oo-ui-selectFileWidget-noThumbnail-icon' ]
5712 }.bind( this ) ).always( function () {
5715 this.$element
.off( 'click' );
5718 if ( this.showDropTarget
) {
5719 this.$element
.off( 'click' );
5721 click
: this.onDropTargetClick
.bind( this )
5725 .css( 'background-image', '' );
5727 this.$element
.addClass( 'oo-ui-selectFileWidget-empty' );
5728 this.setLabel( this.placeholder
);
5734 * If the selected file is an image, get its URL and load it.
5736 * @return {jQuery.Promise} Promise resolves with the image URL after it has loaded
5738 OO
.ui
.SelectFileWidget
.prototype.loadAndGetImageUrl = function () {
5739 var deferred
= $.Deferred(),
5740 file
= this.currentFile
,
5741 reader
= new FileReader();
5745 ( OO
.getProp( file
, 'type' ) || '' ).indexOf( 'image/' ) === 0 &&
5746 file
.size
< this.thumbnailSizeLimit
* 1024 * 1024
5748 reader
.onload = function ( event
) {
5749 var img
= document
.createElement( 'img' );
5750 img
.addEventListener( 'load', function () {
5752 img
.naturalWidth
=== 0 ||
5753 img
.naturalHeight
=== 0 ||
5754 img
.complete
=== false
5758 deferred
.resolve( event
.target
.result
);
5761 img
.src
= event
.target
.result
;
5763 reader
.readAsDataURL( file
);
5768 return deferred
.promise();
5772 * Add the input to the widget
5776 OO
.ui
.SelectFileWidget
.prototype.addInput = function () {
5777 if ( this.$input
) {
5778 this.$input
.remove();
5781 if ( !this.isSupported
) {
5786 this.$input
= $( '<input>' ).attr( 'type', 'file' );
5787 this.$input
.on( 'change', this.onFileSelectedHandler
);
5788 this.$input
.on( 'click', function ( e
) {
5789 // Prevents dropTarget to get clicked which calls
5790 // a click on this input
5791 e
.stopPropagation();
5796 if ( this.accept
) {
5797 this.$input
.attr( 'accept', this.accept
.join( ', ' ) );
5799 this.selectButton
.$button
.append( this.$input
);
5803 * Determine if we should accept this file
5806 * @param {string} mimeType File MIME type
5809 OO
.ui
.SelectFileWidget
.prototype.isAllowedType = function ( mimeType
) {
5812 if ( !this.accept
|| !mimeType
) {
5816 for ( i
= 0; i
< this.accept
.length
; i
++ ) {
5817 mimeTest
= this.accept
[ i
];
5818 if ( mimeTest
=== mimeType
) {
5820 } else if ( mimeTest
.substr( -2 ) === '/*' ) {
5821 mimeTest
= mimeTest
.substr( 0, mimeTest
.length
- 1 );
5822 if ( mimeType
.substr( 0, mimeTest
.length
) === mimeTest
) {
5832 * Handle file selection from the input
5835 * @param {jQuery.Event} e
5837 OO
.ui
.SelectFileWidget
.prototype.onFileSelected = function ( e
) {
5838 var file
= OO
.getProp( e
.target
, 'files', 0 ) || null;
5840 if ( file
&& !this.isAllowedType( file
.type
) ) {
5844 this.setValue( file
);
5849 * Handle clear button click events.
5852 * @return {undefined/boolean} False to prevent default if event is handled
5854 OO
.ui
.SelectFileWidget
.prototype.onClearClick = function () {
5855 this.setValue( null );
5860 * Handle key press events.
5863 * @param {jQuery.Event} e Key press event
5864 * @return {undefined/boolean} False to prevent default if event is handled
5866 OO
.ui
.SelectFileWidget
.prototype.onKeyPress = function ( e
) {
5867 if ( this.isSupported
&& !this.isDisabled() && this.$input
&&
5868 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
5870 this.$input
.trigger( 'click' );
5876 * Handle drop target click events.
5879 * @param {jQuery.Event} e Key press event
5880 * @return {undefined/boolean} False to prevent default if event is handled
5882 OO
.ui
.SelectFileWidget
.prototype.onDropTargetClick = function () {
5883 if ( this.isSupported
&& !this.isDisabled() && this.$input
) {
5884 this.$input
.trigger( 'click' );
5890 * Handle drag enter and over events
5893 * @param {jQuery.Event} e Drag event
5894 * @return {undefined/boolean} False to prevent default if event is handled
5896 OO
.ui
.SelectFileWidget
.prototype.onDragEnterOrOver = function ( e
) {
5898 droppableFile
= false,
5899 dt
= e
.originalEvent
.dataTransfer
;
5902 e
.stopPropagation();
5904 if ( this.isDisabled() || !this.isSupported
) {
5905 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
5906 dt
.dropEffect
= 'none';
5910 // DataTransferItem and File both have a type property, but in Chrome files
5911 // have no information at this point.
5912 itemOrFile
= OO
.getProp( dt
, 'items', 0 ) || OO
.getProp( dt
, 'files', 0 );
5914 if ( this.isAllowedType( itemOrFile
.type
) ) {
5915 droppableFile
= true;
5917 // dt.types is Array-like, but not an Array
5918 } else if ( Array
.prototype.indexOf
.call( OO
.getProp( dt
, 'types' ) || [], 'Files' ) !== -1 ) {
5919 // File information is not available at this point for security so just assume
5920 // it is acceptable for now.
5921 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
5922 droppableFile
= true;
5925 this.$element
.toggleClass( 'oo-ui-selectFileWidget-canDrop', droppableFile
);
5926 if ( !droppableFile
) {
5927 dt
.dropEffect
= 'none';
5934 * Handle drag leave events
5937 * @param {jQuery.Event} e Drag event
5939 OO
.ui
.SelectFileWidget
.prototype.onDragLeave = function () {
5940 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
5944 * Handle drop events
5947 * @param {jQuery.Event} e Drop event
5948 * @return {undefined/boolean} False to prevent default if event is handled
5950 OO
.ui
.SelectFileWidget
.prototype.onDrop = function ( e
) {
5952 dt
= e
.originalEvent
.dataTransfer
;
5955 e
.stopPropagation();
5956 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
5958 if ( this.isDisabled() || !this.isSupported
) {
5962 file
= OO
.getProp( dt
, 'files', 0 );
5963 if ( file
&& !this.isAllowedType( file
.type
) ) {
5967 this.setValue( file
);
5976 OO
.ui
.SelectFileWidget
.prototype.setDisabled = function ( disabled
) {
5977 OO
.ui
.SelectFileWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
5978 if ( this.selectButton
) {
5979 this.selectButton
.setDisabled( disabled
);
5981 if ( this.clearButton
) {
5982 this.clearButton
.setDisabled( disabled
);
5988 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field},
5989 * where users can type a search query, and a menu of search results,
5990 * which is displayed beneath the query field.
5991 * Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible
5992 * to the user. Users can choose an item from the menu or type a query into the text field to
5993 * search for a matching result item.
5994 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
5996 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
5997 * the [OOUI demos][1] for an example.
5999 * [1]: https://doc.wikimedia.org/oojs-ui/master/demos/#SearchInputWidget-type-search
6002 * @extends OO.ui.Widget
6005 * @param {Object} [config] Configuration options
6006 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6007 * @cfg {string} [value] Initial query value
6009 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
6010 // Configuration initialization
6011 config
= config
|| {};
6013 // Parent constructor
6014 OO
.ui
.SearchWidget
.parent
.call( this, config
);
6017 this.query
= new OO
.ui
.TextInputWidget( {
6019 placeholder
: config
.placeholder
,
6022 this.results
= new OO
.ui
.SelectWidget();
6023 this.$query
= $( '<div>' );
6024 this.$results
= $( '<div>' );
6027 this.query
.connect( this, {
6028 change
: 'onQueryChange',
6029 enter
: 'onQueryEnter'
6031 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
6035 .addClass( 'oo-ui-searchWidget-query' )
6036 .append( this.query
.$element
);
6038 .addClass( 'oo-ui-searchWidget-results' )
6039 .append( this.results
.$element
);
6041 .addClass( 'oo-ui-searchWidget' )
6042 .append( this.$results
, this.$query
);
6047 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
6052 * Handle query key down events.
6055 * @param {jQuery.Event} e Key down event
6057 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
6058 var highlightedItem
, nextItem
,
6059 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
6062 highlightedItem
= this.results
.findHighlightedItem();
6063 if ( !highlightedItem
) {
6064 highlightedItem
= this.results
.findSelectedItem();
6066 nextItem
= this.results
.findRelativeSelectableItem( highlightedItem
, dir
);
6067 this.results
.highlightItem( nextItem
);
6068 nextItem
.scrollElementIntoView();
6073 * Handle select widget select events.
6075 * Clears existing results. Subclasses should repopulate items according to new query.
6078 * @param {string} value New value
6080 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
6082 this.results
.clearItems();
6086 * Handle select widget enter key events.
6088 * Chooses highlighted item.
6091 * @param {string} value New value
6093 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
6094 var highlightedItem
= this.results
.findHighlightedItem();
6095 if ( highlightedItem
) {
6096 this.results
.chooseItem( highlightedItem
);
6101 * Get the query input.
6103 * @return {OO.ui.TextInputWidget} Query input
6105 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
6110 * Get the search results menu.
6112 * @return {OO.ui.SelectWidget} Menu of search results
6114 OO
.ui
.SearchWidget
.prototype.getResults = function () {
6115 return this.results
;
6120 //# sourceMappingURL=oojs-ui-widgets.js.map.json