3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2016 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2016-02-02T22:07:00Z
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.
26 OO
.ui
.mixin
.DraggableElement
= function OoUiMixinDraggableElement() {
30 // Initialize and events
32 .attr( 'draggable', true )
33 .addClass( 'oo-ui-draggableElement' )
35 dragstart
: this.onDragStart
.bind( this ),
36 dragover
: this.onDragOver
.bind( this ),
37 dragend
: this.onDragEnd
.bind( this ),
38 drop
: this.onDrop
.bind( this )
42 OO
.initClass( OO
.ui
.mixin
.DraggableElement
);
49 * A dragstart event is emitted when the user clicks and begins dragging an item.
50 * @param {OO.ui.mixin.DraggableElement} item The item the user has clicked and is dragging with the mouse.
55 * A dragend event is emitted when the user drags an item and releases the mouse,
56 * thus terminating the drag operation.
61 * A drop event is emitted when the user drags an item and then releases the mouse button
62 * over a valid target.
65 /* Static Properties */
68 * @inheritdoc OO.ui.mixin.ButtonElement
70 OO
.ui
.mixin
.DraggableElement
.static.cancelButtonMouseDownEvents
= false;
75 * Respond to dragstart event.
78 * @param {jQuery.Event} event jQuery event
81 OO
.ui
.mixin
.DraggableElement
.prototype.onDragStart = function ( e
) {
82 var dataTransfer
= e
.originalEvent
.dataTransfer
;
84 dataTransfer
.dropEffect
= 'none';
85 dataTransfer
.effectAllowed
= 'move';
87 // We must set up a dataTransfer data property or Firefox seems to
88 // ignore the fact the element is draggable.
90 dataTransfer
.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
92 // The above is only for Firefox. Move on if it fails.
95 this.$element
.addClass( 'oo-ui-draggableElement-dragging' );
97 this.emit( 'dragstart', this );
102 * Respond to dragend event.
107 OO
.ui
.mixin
.DraggableElement
.prototype.onDragEnd = function () {
108 this.$element
.removeClass( 'oo-ui-draggableElement-dragging' );
109 this.emit( 'dragend' );
116 * @param {jQuery.Event} event jQuery event
119 OO
.ui
.mixin
.DraggableElement
.prototype.onDrop = function ( e
) {
121 this.emit( 'drop', e
);
125 * In order for drag/drop to work, the dragover event must
126 * return false and stop propogation.
130 OO
.ui
.mixin
.DraggableElement
.prototype.onDragOver = function ( e
) {
136 * Store it in the DOM so we can access from the widget drag event
139 * @param {number} Item index
141 OO
.ui
.mixin
.DraggableElement
.prototype.setIndex = function ( index
) {
142 if ( this.index
!== index
) {
144 this.$element
.data( 'index', index
);
152 * @return {number} Item index
154 OO
.ui
.mixin
.DraggableElement
.prototype.getIndex = function () {
159 * DraggableGroupElement is a mixin class used to create a group element to
160 * contain draggable elements, which are items that can be clicked and dragged by a mouse.
161 * The class is used with OO.ui.mixin.DraggableElement.
165 * @mixins OO.ui.mixin.GroupElement
168 * @param {Object} [config] Configuration options
169 * @cfg {string} [orientation] Item orientation: 'horizontal' or 'vertical'. The orientation
170 * should match the layout of the items. Items displayed in a single row
171 * or in several rows should use horizontal orientation. The vertical orientation should only be
172 * used when the items are displayed in a single column. Defaults to 'vertical'
174 OO
.ui
.mixin
.DraggableGroupElement
= function OoUiMixinDraggableGroupElement( config
) {
175 // Configuration initialization
176 config
= config
|| {};
178 // Parent constructor
179 OO
.ui
.mixin
.GroupElement
.call( this, config
);
182 this.orientation
= config
.orientation
|| 'vertical';
183 this.dragItem
= null;
184 this.itemDragOver
= null;
186 this.sideInsertion
= '';
190 dragstart
: 'itemDragStart',
191 dragend
: 'itemDragEnd',
194 this.connect( this, {
195 itemDragStart
: 'onItemDragStart',
196 itemDrop
: 'onItemDrop',
197 itemDragEnd
: 'onItemDragEnd'
200 dragover
: this.onDragOver
.bind( this ),
201 dragleave
: this.onDragLeave
.bind( this )
205 if ( Array
.isArray( config
.items
) ) {
206 this.addItems( config
.items
);
208 this.$placeholder
= $( '<div>' )
209 .addClass( 'oo-ui-draggableGroupElement-placeholder' );
211 .addClass( 'oo-ui-draggableGroupElement' )
212 .append( this.$status
)
213 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation
=== 'horizontal' )
214 .prepend( this.$placeholder
);
218 OO
.mixinClass( OO
.ui
.mixin
.DraggableGroupElement
, OO
.ui
.mixin
.GroupElement
);
223 * A 'reorder' event is emitted when the order of items in the group changes.
226 * @param {OO.ui.mixin.DraggableElement} item Reordered item
227 * @param {number} [newIndex] New index for the item
233 * Respond to item drag start event
236 * @param {OO.ui.mixin.DraggableElement} item Dragged item
238 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDragStart = function ( item
) {
241 // Map the index of each object
242 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
243 this.items
[ i
].setIndex( i
);
246 if ( this.orientation
=== 'horizontal' ) {
247 // Set the height of the indicator
248 this.$placeholder
.css( {
249 height
: item
.$element
.outerHeight(),
253 // Set the width of the indicator
254 this.$placeholder
.css( {
256 width
: item
.$element
.outerWidth()
259 this.setDragItem( item
);
263 * Respond to item drag end event
267 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDragEnd = function () {
268 this.unsetDragItem();
273 * Handle drop event and switch the order of the items accordingly
276 * @param {OO.ui.mixin.DraggableElement} item Dropped item
279 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onItemDrop = function ( item
) {
280 var toIndex
= item
.getIndex();
281 // Check if the dropped item is from the current group
282 // TODO: Figure out a way to configure a list of legally droppable
283 // elements even if they are not yet in the list
284 if ( this.getDragItem() ) {
285 // If the insertion point is 'after', the insertion index
286 // is shifted to the right (or to the left in RTL, hence 'after')
287 if ( this.sideInsertion
=== 'after' ) {
291 this.emit( 'reorder', this.getDragItem(), toIndex
);
293 this.unsetDragItem();
294 // Return false to prevent propogation
299 * Handle dragleave event.
303 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onDragLeave = function () {
304 // This means the item was dragged outside the widget
307 .addClass( 'oo-ui-element-hidden' );
311 * Respond to dragover event
314 * @param {jQuery.Event} event Event details
316 OO
.ui
.mixin
.DraggableGroupElement
.prototype.onDragOver = function ( e
) {
317 var dragOverObj
, $optionWidget
, itemOffset
, itemMidpoint
, itemBoundingRect
,
318 itemSize
, cssOutput
, dragPosition
, itemIndex
, itemPosition
,
319 clientX
= e
.originalEvent
.clientX
,
320 clientY
= e
.originalEvent
.clientY
;
322 // Get the OptionWidget item we are dragging over
323 dragOverObj
= this.getElementDocument().elementFromPoint( clientX
, clientY
);
324 $optionWidget
= $( dragOverObj
).closest( '.oo-ui-draggableElement' );
325 if ( $optionWidget
[ 0 ] ) {
326 itemOffset
= $optionWidget
.offset();
327 itemBoundingRect
= $optionWidget
[ 0 ].getBoundingClientRect();
328 itemPosition
= $optionWidget
.position();
329 itemIndex
= $optionWidget
.data( 'index' );
335 itemIndex
!== this.getDragItem().getIndex()
337 if ( this.orientation
=== 'horizontal' ) {
338 // Calculate where the mouse is relative to the item width
339 itemSize
= itemBoundingRect
.width
;
340 itemMidpoint
= itemBoundingRect
.left
+ itemSize
/ 2;
341 dragPosition
= clientX
;
342 // Which side of the item we hover over will dictate
343 // where the placeholder will appear, on the left or
346 left
: dragPosition
< itemMidpoint
? itemPosition
.left
: itemPosition
.left
+ itemSize
,
347 top
: itemPosition
.top
350 // Calculate where the mouse is relative to the item height
351 itemSize
= itemBoundingRect
.height
;
352 itemMidpoint
= itemBoundingRect
.top
+ itemSize
/ 2;
353 dragPosition
= clientY
;
354 // Which side of the item we hover over will dictate
355 // where the placeholder will appear, on the top or
358 top
: dragPosition
< itemMidpoint
? itemPosition
.top
: itemPosition
.top
+ itemSize
,
359 left
: itemPosition
.left
362 // Store whether we are before or after an item to rearrange
363 // For horizontal layout, we need to account for RTL, as this is flipped
364 if ( this.orientation
=== 'horizontal' && this.$element
.css( 'direction' ) === 'rtl' ) {
365 this.sideInsertion
= dragPosition
< itemMidpoint
? 'after' : 'before';
367 this.sideInsertion
= dragPosition
< itemMidpoint
? 'before' : 'after';
369 // Add drop indicator between objects
372 .removeClass( 'oo-ui-element-hidden' );
374 // This means the item was dragged outside the widget
377 .addClass( 'oo-ui-element-hidden' );
386 * @param {OO.ui.mixin.DraggableElement} item Dragged item
388 OO
.ui
.mixin
.DraggableGroupElement
.prototype.setDragItem = function ( item
) {
389 this.dragItem
= item
;
393 * Unset the current dragged item
395 OO
.ui
.mixin
.DraggableGroupElement
.prototype.unsetDragItem = function () {
396 this.dragItem
= null;
397 this.itemDragOver
= null;
398 this.$placeholder
.addClass( 'oo-ui-element-hidden' );
399 this.sideInsertion
= '';
403 * Get the item that is currently being dragged.
405 * @return {OO.ui.mixin.DraggableElement|null} The currently dragged item, or `null` if no item is being dragged
407 OO
.ui
.mixin
.DraggableGroupElement
.prototype.getDragItem = function () {
408 return this.dragItem
;
412 * Check if an item in the group is currently being dragged.
414 * @return {Boolean} Item is being dragged
416 OO
.ui
.mixin
.DraggableGroupElement
.prototype.isDragging = function () {
417 return this.getDragItem() !== null;
421 * RequestManager is a mixin that manages the lifecycle of a promise-backed request for a widget, such as
422 * the {@link OO.ui.mixin.LookupElement}.
429 OO
.ui
.mixin
.RequestManager
= function OoUiMixinRequestManager() {
430 this.requestCache
= {};
431 this.requestQuery
= null;
432 this.requestRequest
= null;
437 OO
.initClass( OO
.ui
.mixin
.RequestManager
);
440 * Get request results for the current query.
442 * @return {jQuery.Promise} Promise object which will be passed response data as the first argument of
443 * the done event. If the request was aborted to make way for a subsequent request, this promise
444 * may not be rejected, depending on what jQuery feels like doing.
446 OO
.ui
.mixin
.RequestManager
.prototype.getRequestData = function () {
448 value
= this.getRequestQuery(),
449 deferred
= $.Deferred(),
453 if ( Object
.prototype.hasOwnProperty
.call( this.requestCache
, value
) ) {
454 deferred
.resolve( this.requestCache
[ value
] );
456 if ( this.pushPending
) {
459 this.requestQuery
= value
;
460 ourRequest
= this.requestRequest
= this.getRequest();
462 .always( function () {
463 // We need to pop pending even if this is an old request, otherwise
464 // the widget will remain pending forever.
465 // TODO: this assumes that an aborted request will fail or succeed soon after
466 // being aborted, or at least eventually. It would be nice if we could popPending()
467 // at abort time, but only if we knew that we hadn't already called popPending()
469 if ( widget
.popPending
) {
473 .done( function ( response
) {
474 // If this is an old request (and aborting it somehow caused it to still succeed),
475 // ignore its success completely
476 if ( ourRequest
=== widget
.requestRequest
) {
477 widget
.requestQuery
= null;
478 widget
.requestRequest
= null;
479 widget
.requestCache
[ value
] = widget
.getRequestCacheDataFromResponse( response
);
480 deferred
.resolve( widget
.requestCache
[ value
] );
484 // If this is an old request (or a request failing because it's being aborted),
485 // ignore its failure completely
486 if ( ourRequest
=== widget
.requestRequest
) {
487 widget
.requestQuery
= null;
488 widget
.requestRequest
= null;
493 return deferred
.promise();
497 * Abort the currently pending request, if any.
501 OO
.ui
.mixin
.RequestManager
.prototype.abortRequest = function () {
502 var oldRequest
= this.requestRequest
;
504 // First unset this.requestRequest to the fail handler will notice
505 // that the request is no longer current
506 this.requestRequest
= null;
507 this.requestQuery
= null;
513 * Get the query to be made.
518 * @return {string} query to be used
520 OO
.ui
.mixin
.RequestManager
.prototype.getRequestQuery
= null;
523 * Get a new request object of the current query value.
528 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
530 OO
.ui
.mixin
.RequestManager
.prototype.getRequest
= null;
533 * Pre-process data returned by the request from #getRequest.
535 * The return value of this function will be cached, and any further queries for the given value
536 * will use the cache rather than doing API requests.
541 * @param {Mixed} response Response from server
542 * @return {Mixed} Cached result data
544 OO
.ui
.mixin
.RequestManager
.prototype.getRequestCacheDataFromResponse
= null;
547 * LookupElement is a mixin that creates a {@link OO.ui.FloatingMenuSelectWidget menu} of suggested values for
548 * a {@link OO.ui.TextInputWidget text input widget}. Suggested values are based on the characters the user types
549 * into the text input field and, in general, the menu is only displayed when the user types. If a suggested value is chosen
550 * from the lookup menu, that value becomes the value of the input field.
552 * Note that a new menu of suggested items is displayed when a value is chosen from the lookup menu. If this is
553 * not the desired behavior, disable lookup menus with the #setLookupsDisabled method, then set the value, then
556 * See the [OOjs UI demos][1] for an example.
558 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/index.html#widgets-apex-vector-ltr
564 * @param {Object} [config] Configuration options
565 * @cfg {jQuery} [$overlay] Overlay for the lookup menu; defaults to relative positioning
566 * @cfg {jQuery} [$container=this.$element] The container element. The lookup menu is rendered beneath the specified element.
567 * @cfg {boolean} [allowSuggestionsWhenEmpty=false] Request and display a lookup menu when the text input is empty.
568 * By default, the lookup menu is not generated and displayed until the user begins to type.
569 * @cfg {boolean} [highlightFirst=true] Whether the first lookup result should be highlighted (so, that the user can
570 * take it over into the input with simply pressing return) automatically or not.
572 OO
.ui
.mixin
.LookupElement
= function OoUiMixinLookupElement( config
) {
573 // Configuration initialization
574 config
= $.extend( { highlightFirst
: true }, config
);
576 // Mixin constructors
577 OO
.ui
.mixin
.RequestManager
.call( this, config
);
580 this.$overlay
= config
.$overlay
|| this.$element
;
581 this.lookupMenu
= new OO
.ui
.FloatingMenuSelectWidget( {
584 $container
: config
.$container
|| this.$element
587 this.allowSuggestionsWhenEmpty
= config
.allowSuggestionsWhenEmpty
|| false;
589 this.lookupsDisabled
= false;
590 this.lookupInputFocused
= false;
591 this.lookupHighlightFirstItem
= config
.highlightFirst
;
595 focus
: this.onLookupInputFocus
.bind( this ),
596 blur
: this.onLookupInputBlur
.bind( this ),
597 mousedown
: this.onLookupInputMouseDown
.bind( this )
599 this.connect( this, { change
: 'onLookupInputChange' } );
600 this.lookupMenu
.connect( this, {
601 toggle
: 'onLookupMenuToggle',
602 choose
: 'onLookupMenuItemChoose'
606 this.$element
.addClass( 'oo-ui-lookupElement' );
607 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupElement-menu' );
608 this.$overlay
.append( this.lookupMenu
.$element
);
613 OO
.mixinClass( OO
.ui
.mixin
.LookupElement
, OO
.ui
.mixin
.RequestManager
);
618 * Handle input focus event.
621 * @param {jQuery.Event} e Input focus event
623 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputFocus = function () {
624 this.lookupInputFocused
= true;
625 this.populateLookupMenu();
629 * Handle input blur event.
632 * @param {jQuery.Event} e Input blur event
634 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputBlur = function () {
635 this.closeLookupMenu();
636 this.lookupInputFocused
= false;
640 * Handle input mouse down event.
643 * @param {jQuery.Event} e Input mouse down event
645 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputMouseDown = function () {
646 // Only open the menu if the input was already focused.
647 // This way we allow the user to open the menu again after closing it with Esc
648 // by clicking in the input. Opening (and populating) the menu when initially
649 // clicking into the input is handled by the focus handler.
650 if ( this.lookupInputFocused
&& !this.lookupMenu
.isVisible() ) {
651 this.populateLookupMenu();
656 * Handle input change event.
659 * @param {string} value New input value
661 OO
.ui
.mixin
.LookupElement
.prototype.onLookupInputChange = function () {
662 if ( this.lookupInputFocused
) {
663 this.populateLookupMenu();
668 * Handle the lookup menu being shown/hidden.
671 * @param {boolean} visible Whether the lookup menu is now visible.
673 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuToggle = function ( visible
) {
675 // When the menu is hidden, abort any active request and clear the menu.
676 // This has to be done here in addition to closeLookupMenu(), because
677 // MenuSelectWidget will close itself when the user presses Esc.
678 this.abortLookupRequest();
679 this.lookupMenu
.clearItems();
684 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
687 * @param {OO.ui.MenuOptionWidget} item Selected item
689 OO
.ui
.mixin
.LookupElement
.prototype.onLookupMenuItemChoose = function ( item
) {
690 this.setValue( item
.getData() );
697 * @return {OO.ui.FloatingMenuSelectWidget}
699 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenu = function () {
700 return this.lookupMenu
;
704 * Disable or re-enable lookups.
706 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
708 * @param {boolean} disabled Disable lookups
710 OO
.ui
.mixin
.LookupElement
.prototype.setLookupsDisabled = function ( disabled
) {
711 this.lookupsDisabled
= !!disabled
;
715 * Open the menu. If there are no entries in the menu, this does nothing.
720 OO
.ui
.mixin
.LookupElement
.prototype.openLookupMenu = function () {
721 if ( !this.lookupMenu
.isEmpty() ) {
722 this.lookupMenu
.toggle( true );
728 * Close the menu, empty it, and abort any pending request.
733 OO
.ui
.mixin
.LookupElement
.prototype.closeLookupMenu = function () {
734 this.lookupMenu
.toggle( false );
735 this.abortLookupRequest();
736 this.lookupMenu
.clearItems();
741 * Request menu items based on the input's current value, and when they arrive,
742 * populate the menu with these items and show the menu.
744 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
749 OO
.ui
.mixin
.LookupElement
.prototype.populateLookupMenu = function () {
751 value
= this.getValue();
753 if ( this.lookupsDisabled
|| this.isReadOnly() ) {
757 // If the input is empty, clear the menu, unless suggestions when empty are allowed.
758 if ( !this.allowSuggestionsWhenEmpty
&& value
=== '' ) {
759 this.closeLookupMenu();
760 // Skip population if there is already a request pending for the current value
761 } else if ( value
!== this.lookupQuery
) {
762 this.getLookupMenuItems()
763 .done( function ( items
) {
764 widget
.lookupMenu
.clearItems();
765 if ( items
.length
) {
769 widget
.initializeLookupMenuSelection();
771 widget
.lookupMenu
.toggle( false );
775 widget
.lookupMenu
.clearItems();
783 * Highlight the first selectable item in the menu, if configured.
788 OO
.ui
.mixin
.LookupElement
.prototype.initializeLookupMenuSelection = function () {
789 if ( this.lookupHighlightFirstItem
&& !this.lookupMenu
.getSelectedItem() ) {
790 this.lookupMenu
.highlightItem( this.lookupMenu
.getFirstSelectableItem() );
795 * Get lookup menu items for the current query.
798 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
799 * the done event. If the request was aborted to make way for a subsequent request, this promise
800 * will not be rejected: it will remain pending forever.
802 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuItems = function () {
803 return this.getRequestData().then( function ( data
) {
804 return this.getLookupMenuOptionsFromData( data
);
809 * Abort the currently pending lookup request, if any.
813 OO
.ui
.mixin
.LookupElement
.prototype.abortLookupRequest = function () {
818 * Get a new request object of the current lookup query value.
823 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
825 OO
.ui
.mixin
.LookupElement
.prototype.getLookupRequest
= null;
828 * Pre-process data returned by the request from #getLookupRequest.
830 * The return value of this function will be cached, and any further queries for the given value
831 * will use the cache rather than doing API requests.
836 * @param {Mixed} response Response from server
837 * @return {Mixed} Cached result data
839 OO
.ui
.mixin
.LookupElement
.prototype.getLookupCacheDataFromResponse
= null;
842 * Get a list of menu option widgets from the (possibly cached) data returned by
843 * #getLookupCacheDataFromResponse.
848 * @param {Mixed} data Cached result data, usually an array
849 * @return {OO.ui.MenuOptionWidget[]} Menu items
851 OO
.ui
.mixin
.LookupElement
.prototype.getLookupMenuOptionsFromData
= null;
854 * Set the read-only state of the widget.
856 * This will also disable/enable the lookups functionality.
858 * @param {boolean} readOnly Make input read-only
861 OO
.ui
.mixin
.LookupElement
.prototype.setReadOnly = function ( readOnly
) {
863 // Note: Calling #setReadOnly this way assumes this is mixed into an OO.ui.TextInputWidget
864 OO
.ui
.TextInputWidget
.prototype.setReadOnly
.call( this, readOnly
);
866 // During construction, #setReadOnly is called before the OO.ui.mixin.LookupElement constructor
867 if ( this.isReadOnly() && this.lookupMenu
) {
868 this.closeLookupMenu();
875 * @inheritdoc OO.ui.mixin.RequestManager
877 OO
.ui
.mixin
.LookupElement
.prototype.getRequestQuery = function () {
878 return this.getValue();
882 * @inheritdoc OO.ui.mixin.RequestManager
884 OO
.ui
.mixin
.LookupElement
.prototype.getRequest = function () {
885 return this.getLookupRequest();
889 * @inheritdoc OO.ui.mixin.RequestManager
891 OO
.ui
.mixin
.LookupElement
.prototype.getRequestCacheDataFromResponse = function ( response
) {
892 return this.getLookupCacheDataFromResponse( response
);
896 * CardLayouts are used within {@link OO.ui.IndexLayout index layouts} to create cards that users can select and display
897 * from the index's optional {@link OO.ui.TabSelectWidget tab} navigation. Cards are usually not instantiated directly,
898 * rather extended to include the required content and functionality.
900 * Each card must have a unique symbolic name, which is passed to the constructor. In addition, the card's tab
901 * item is customized (with a label) using the #setupTabItem method. See
902 * {@link OO.ui.IndexLayout IndexLayout} for an example.
905 * @extends OO.ui.PanelLayout
908 * @param {string} name Unique symbolic name of card
909 * @param {Object} [config] Configuration options
910 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] Label for card's tab
912 OO
.ui
.CardLayout
= function OoUiCardLayout( name
, config
) {
913 // Allow passing positional parameters inside the config object
914 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
919 // Configuration initialization
920 config
= $.extend( { scrollable
: true }, config
);
922 // Parent constructor
923 OO
.ui
.CardLayout
.parent
.call( this, config
);
927 this.label
= config
.label
;
932 this.$element
.addClass( 'oo-ui-cardLayout' );
937 OO
.inheritClass( OO
.ui
.CardLayout
, OO
.ui
.PanelLayout
);
942 * An 'active' event is emitted when the card becomes active. Cards become active when they are
943 * shown in a index layout that is configured to display only one card at a time.
946 * @param {boolean} active Card is active
952 * Get the symbolic name of the card.
954 * @return {string} Symbolic name of card
956 OO
.ui
.CardLayout
.prototype.getName = function () {
961 * Check if card is active.
963 * Cards become active when they are shown in a {@link OO.ui.IndexLayout index layout} that is configured to display
964 * only one card at a time. Additional CSS is applied to the card's tab item to reflect the active state.
966 * @return {boolean} Card is active
968 OO
.ui
.CardLayout
.prototype.isActive = function () {
975 * The tab item allows users to access the card from the index's tab
976 * navigation. The tab item itself can be customized (with a label, level, etc.) using the #setupTabItem method.
978 * @return {OO.ui.TabOptionWidget|null} Tab option widget
980 OO
.ui
.CardLayout
.prototype.getTabItem = function () {
985 * Set or unset the tab item.
987 * Specify a {@link OO.ui.TabOptionWidget tab option} to set it,
988 * or `null` to clear the tab item. To customize the tab item itself (e.g., to set a label or tab
989 * level), use #setupTabItem instead of this method.
991 * @param {OO.ui.TabOptionWidget|null} tabItem Tab option widget, null to clear
994 OO
.ui
.CardLayout
.prototype.setTabItem = function ( tabItem
) {
995 this.tabItem
= tabItem
|| null;
1003 * Set up the tab item.
1005 * Use this method to customize the tab item (e.g., to add a label or tab level). To set or unset
1006 * the tab item itself (with a {@link OO.ui.TabOptionWidget tab option} or `null`), use
1007 * the #setTabItem method instead.
1009 * @param {OO.ui.TabOptionWidget} tabItem Tab option widget to set up
1012 OO
.ui
.CardLayout
.prototype.setupTabItem = function () {
1014 this.tabItem
.setLabel( this.label
);
1020 * Set the card to its 'active' state.
1022 * Cards become active when they are shown in a index layout that is configured to display only one card at a time. Additional
1023 * CSS is applied to the tab item to reflect the card's active state. Outside of the index
1024 * context, setting the active state on a card does nothing.
1026 * @param {boolean} value Card is active
1029 OO
.ui
.CardLayout
.prototype.setActive = function ( active
) {
1032 if ( active
!== this.active
) {
1033 this.active
= active
;
1034 this.$element
.toggleClass( 'oo-ui-cardLayout-active', this.active
);
1035 this.emit( 'active', this.active
);
1040 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that users can select and display
1041 * from the booklet's optional {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated directly,
1042 * rather extended to include the required content and functionality.
1044 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the page's outline
1045 * item is customized (with a label, outline level, etc.) using the #setupOutlineItem method. See
1046 * {@link OO.ui.BookletLayout BookletLayout} for an example.
1049 * @extends OO.ui.PanelLayout
1052 * @param {string} name Unique symbolic name of page
1053 * @param {Object} [config] Configuration options
1055 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
1056 // Allow passing positional parameters inside the config object
1057 if ( OO
.isPlainObject( name
) && config
=== undefined ) {
1062 // Configuration initialization
1063 config
= $.extend( { scrollable
: true }, config
);
1065 // Parent constructor
1066 OO
.ui
.PageLayout
.parent
.call( this, config
);
1070 this.outlineItem
= null;
1071 this.active
= false;
1074 this.$element
.addClass( 'oo-ui-pageLayout' );
1079 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
1084 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1085 * shown in a booklet layout that is configured to display only one page at a time.
1088 * @param {boolean} active Page is active
1094 * Get the symbolic name of the page.
1096 * @return {string} Symbolic name of page
1098 OO
.ui
.PageLayout
.prototype.getName = function () {
1103 * Check if page is active.
1105 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is configured to display
1106 * only one page at a time. Additional CSS is applied to the page's outline item to reflect the active state.
1108 * @return {boolean} Page is active
1110 OO
.ui
.PageLayout
.prototype.isActive = function () {
1117 * The outline item allows users to access the page from the booklet's outline
1118 * navigation. The outline item itself can be customized (with a label, level, etc.) using the #setupOutlineItem method.
1120 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1122 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
1123 return this.outlineItem
;
1127 * Set or unset the outline item.
1129 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1130 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label or outline
1131 * level), use #setupOutlineItem instead of this method.
1133 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1136 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
1137 this.outlineItem
= outlineItem
|| null;
1138 if ( outlineItem
) {
1139 this.setupOutlineItem();
1145 * Set up the outline item.
1147 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or unset
1148 * the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or `null`), use
1149 * the #setOutlineItem method instead.
1151 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1154 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
1159 * Set the page to its 'active' state.
1161 * Pages become active when they are shown in a booklet layout that is configured to display only one page at a time. Additional
1162 * CSS is applied to the outline item to reflect the page's active state. Outside of the booklet
1163 * context, setting the active state on a page does nothing.
1165 * @param {boolean} value Page is active
1168 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
1171 if ( active
!== this.active
) {
1172 this.active
= active
;
1173 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
1174 this.emit( 'active', this.active
);
1179 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one panel is displayed
1180 * at a time, though the stack layout can also be configured to show all contained panels, one after another,
1181 * by setting the #continuous option to 'true'.
1184 * // A stack layout with two panels, configured to be displayed continously
1185 * var myStack = new OO.ui.StackLayout( {
1187 * new OO.ui.PanelLayout( {
1188 * $content: $( '<p>Panel One</p>' ),
1192 * new OO.ui.PanelLayout( {
1193 * $content: $( '<p>Panel Two</p>' ),
1200 * $( 'body' ).append( myStack.$element );
1203 * @extends OO.ui.PanelLayout
1204 * @mixins OO.ui.mixin.GroupElement
1207 * @param {Object} [config] Configuration options
1208 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel is displayed at a time.
1209 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1211 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
1212 // Configuration initialization
1213 config
= $.extend( { scrollable
: true }, config
);
1215 // Parent constructor
1216 OO
.ui
.StackLayout
.parent
.call( this, config
);
1218 // Mixin constructors
1219 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
1222 this.currentItem
= null;
1223 this.continuous
= !!config
.continuous
;
1226 this.$element
.addClass( 'oo-ui-stackLayout' );
1227 if ( this.continuous
) {
1228 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
1229 this.$element
.on( 'scroll', OO
.ui
.debounce( this.onScroll
.bind( this ), 250 ) );
1231 if ( Array
.isArray( config
.items
) ) {
1232 this.addItems( config
.items
);
1238 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
1239 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.mixin
.GroupElement
);
1244 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1245 * {@link #clearItems cleared} or {@link #setItem displayed}.
1248 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1252 * When used in continuous mode, this event is emitted when the user scrolls down
1253 * far enough such that currentItem is no longer visible.
1255 * @event visibleItemChange
1256 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1262 * Handle scroll events from the layout element
1264 * @param {jQuery.Event} e
1265 * @fires visibleItemChange
1267 OO
.ui
.StackLayout
.prototype.onScroll = function () {
1269 len
= this.items
.length
,
1270 currentIndex
= this.items
.indexOf( this.currentItem
),
1271 newIndex
= currentIndex
,
1272 containerRect
= this.$element
[ 0 ].getBoundingClientRect();
1274 if ( !containerRect
|| ( !containerRect
.top
&& !containerRect
.bottom
) ) {
1275 // Can't get bounding rect, possibly not attached.
1279 function getRect( item
) {
1280 return item
.$element
[ 0 ].getBoundingClientRect();
1283 function isVisible( item
) {
1284 var rect
= getRect( item
);
1285 return rect
.bottom
> containerRect
.top
&& rect
.top
< containerRect
.bottom
;
1288 currentRect
= getRect( this.currentItem
);
1290 if ( currentRect
.bottom
< containerRect
.top
) {
1291 // Scrolled down past current item
1292 while ( ++newIndex
< len
) {
1293 if ( isVisible( this.items
[ newIndex
] ) ) {
1297 } else if ( currentRect
.top
> containerRect
.bottom
) {
1298 // Scrolled up past current item
1299 while ( --newIndex
>= 0 ) {
1300 if ( isVisible( this.items
[ newIndex
] ) ) {
1306 if ( newIndex
!== currentIndex
) {
1307 this.emit( 'visibleItemChange', this.items
[ newIndex
] );
1312 * Get the current panel.
1314 * @return {OO.ui.Layout|null}
1316 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
1317 return this.currentItem
;
1321 * Unset the current item.
1324 * @param {OO.ui.StackLayout} layout
1327 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
1328 var prevItem
= this.currentItem
;
1329 if ( prevItem
=== null ) {
1333 this.currentItem
= null;
1334 this.emit( 'set', null );
1338 * Add panel layouts to the stack layout.
1340 * Panels will be added to the end of the stack layout array unless the optional index parameter specifies a different
1341 * insertion point. Adding a panel that is already in the stack will move it to the end of the array or the point specified
1344 * @param {OO.ui.Layout[]} items Panels to add
1345 * @param {number} [index] Index of the insertion point
1348 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
1349 // Update the visibility
1350 this.updateHiddenState( items
, this.currentItem
);
1353 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
, index
);
1355 if ( !this.currentItem
&& items
.length
) {
1356 this.setItem( items
[ 0 ] );
1363 * Remove the specified panels from the stack layout.
1365 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all panels,
1366 * you may wish to use the #clearItems method instead.
1368 * @param {OO.ui.Layout[]} items Panels to remove
1372 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
1374 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
1376 if ( items
.indexOf( this.currentItem
) !== -1 ) {
1377 if ( this.items
.length
) {
1378 this.setItem( this.items
[ 0 ] );
1380 this.unsetCurrentItem();
1388 * Clear all panels from the stack layout.
1390 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1391 * a subset of panels, use the #removeItems method.
1396 OO
.ui
.StackLayout
.prototype.clearItems = function () {
1397 this.unsetCurrentItem();
1398 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
1404 * Show the specified panel.
1406 * If another panel is currently displayed, it will be hidden.
1408 * @param {OO.ui.Layout} item Panel to show
1412 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
1413 if ( item
!== this.currentItem
) {
1414 this.updateHiddenState( this.items
, item
);
1416 if ( this.items
.indexOf( item
) !== -1 ) {
1417 this.currentItem
= item
;
1418 this.emit( 'set', item
);
1420 this.unsetCurrentItem();
1428 * Update the visibility of all items in case of non-continuous view.
1430 * Ensure all items are hidden except for the selected one.
1431 * This method does nothing when the stack is continuous.
1434 * @param {OO.ui.Layout[]} items Item list iterate over
1435 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1437 OO
.ui
.StackLayout
.prototype.updateHiddenState = function ( items
, selectedItem
) {
1440 if ( !this.continuous
) {
1441 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
1442 if ( !selectedItem
|| selectedItem
!== items
[ i
] ) {
1443 items
[ i
].$element
.addClass( 'oo-ui-element-hidden' );
1446 if ( selectedItem
) {
1447 selectedItem
.$element
.removeClass( 'oo-ui-element-hidden' );
1453 * MenuLayouts combine a menu and a content {@link OO.ui.PanelLayout panel}. The menu is positioned relative to the content (after, before, top, or bottom)
1454 * and its size is customized with the #menuSize config. The content area will fill all remaining space.
1457 * var menuLayout = new OO.ui.MenuLayout( {
1460 * menuPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1461 * contentPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1462 * select = new OO.ui.SelectWidget( {
1464 * new OO.ui.OptionWidget( {
1468 * new OO.ui.OptionWidget( {
1472 * new OO.ui.OptionWidget( {
1476 * new OO.ui.OptionWidget( {
1481 * } ).on( 'select', function ( item ) {
1482 * menuLayout.setMenuPosition( item.getData() );
1485 * menuLayout.$menu.append(
1486 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element )
1488 * menuLayout.$content.append(
1489 * contentPanel.$element.append( '<b>Content panel</b>', '<p>Note that the menu is positioned relative to the content panel: top, bottom, after, before.</p>')
1491 * $( 'body' ).append( menuLayout.$element );
1493 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1494 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1495 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1498 * .oo-ui-menuLayout-menu {
1502 * .oo-ui-menuLayout-content {
1510 * @extends OO.ui.Layout
1513 * @param {Object} [config] Configuration options
1514 * @cfg {boolean} [showMenu=true] Show menu
1515 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1517 OO
.ui
.MenuLayout
= function OoUiMenuLayout( config
) {
1518 // Configuration initialization
1519 config
= $.extend( {
1521 menuPosition
: 'before'
1524 // Parent constructor
1525 OO
.ui
.MenuLayout
.parent
.call( this, config
);
1530 * @property {jQuery}
1532 this.$menu
= $( '<div>' );
1536 * @property {jQuery}
1538 this.$content
= $( '<div>' );
1542 .addClass( 'oo-ui-menuLayout-menu' );
1543 this.$content
.addClass( 'oo-ui-menuLayout-content' );
1545 .addClass( 'oo-ui-menuLayout' )
1546 .append( this.$content
, this.$menu
);
1547 this.setMenuPosition( config
.menuPosition
);
1548 this.toggleMenu( config
.showMenu
);
1553 OO
.inheritClass( OO
.ui
.MenuLayout
, OO
.ui
.Layout
);
1560 * @param {boolean} showMenu Show menu, omit to toggle
1563 OO
.ui
.MenuLayout
.prototype.toggleMenu = function ( showMenu
) {
1564 showMenu
= showMenu
=== undefined ? !this.showMenu
: !!showMenu
;
1566 if ( this.showMenu
!== showMenu
) {
1567 this.showMenu
= showMenu
;
1569 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu
)
1570 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu
);
1577 * Check if menu is visible
1579 * @return {boolean} Menu is visible
1581 OO
.ui
.MenuLayout
.prototype.isMenuVisible = function () {
1582 return this.showMenu
;
1586 * Set menu position.
1588 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1589 * @throws {Error} If position value is not supported
1592 OO
.ui
.MenuLayout
.prototype.setMenuPosition = function ( position
) {
1593 this.$element
.removeClass( 'oo-ui-menuLayout-' + this.menuPosition
);
1594 this.menuPosition
= position
;
1595 this.$element
.addClass( 'oo-ui-menuLayout-' + position
);
1601 * Get menu position.
1603 * @return {string} Menu position
1605 OO
.ui
.MenuLayout
.prototype.getMenuPosition = function () {
1606 return this.menuPosition
;
1610 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1611 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1612 * through the pages and select which one to display. By default, only one page is
1613 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1614 * the booklet layout automatically focuses on the first focusable element, unless the
1615 * default setting is changed. Optionally, booklets can be configured to show
1616 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1619 * // Example of a BookletLayout that contains two PageLayouts.
1621 * function PageOneLayout( name, config ) {
1622 * PageOneLayout.parent.call( this, name, config );
1623 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on the left)</p>' );
1625 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1626 * PageOneLayout.prototype.setupOutlineItem = function () {
1627 * this.outlineItem.setLabel( 'Page One' );
1630 * function PageTwoLayout( name, config ) {
1631 * PageTwoLayout.parent.call( this, name, config );
1632 * this.$element.append( '<p>Second page</p>' );
1634 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1635 * PageTwoLayout.prototype.setupOutlineItem = function () {
1636 * this.outlineItem.setLabel( 'Page Two' );
1639 * var page1 = new PageOneLayout( 'one' ),
1640 * page2 = new PageTwoLayout( 'two' );
1642 * var booklet = new OO.ui.BookletLayout( {
1646 * booklet.addPages ( [ page1, page2 ] );
1647 * $( 'body' ).append( booklet.$element );
1650 * @extends OO.ui.MenuLayout
1653 * @param {Object} [config] Configuration options
1654 * @cfg {boolean} [continuous=false] Show all pages, one after another
1655 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is displayed.
1656 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the pages of the booklet.
1657 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
1659 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
1660 // Configuration initialization
1661 config
= config
|| {};
1663 // Parent constructor
1664 OO
.ui
.BookletLayout
.parent
.call( this, config
);
1667 this.currentPageName
= null;
1669 this.ignoreFocus
= false;
1670 this.stackLayout
= new OO
.ui
.StackLayout( { continuous
: !!config
.continuous
} );
1671 this.$content
.append( this.stackLayout
.$element
);
1672 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
1673 this.outlineVisible
= false;
1674 this.outlined
= !!config
.outlined
;
1675 if ( this.outlined
) {
1676 this.editable
= !!config
.editable
;
1677 this.outlineControlsWidget
= null;
1678 this.outlineSelectWidget
= new OO
.ui
.OutlineSelectWidget();
1679 this.outlinePanel
= new OO
.ui
.PanelLayout( { scrollable
: true } );
1680 this.$menu
.append( this.outlinePanel
.$element
);
1681 this.outlineVisible
= true;
1682 if ( this.editable
) {
1683 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
1684 this.outlineSelectWidget
1688 this.toggleMenu( this.outlined
);
1691 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
1692 if ( this.outlined
) {
1693 this.outlineSelectWidget
.connect( this, { select
: 'onOutlineSelectWidgetSelect' } );
1694 this.scrolling
= false;
1695 this.stackLayout
.connect( this, { visibleItemChange
: 'onStackLayoutVisibleItemChange' } );
1697 if ( this.autoFocus
) {
1698 // Event 'focus' does not bubble, but 'focusin' does
1699 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
1703 this.$element
.addClass( 'oo-ui-bookletLayout' );
1704 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
1705 if ( this.outlined
) {
1706 this.outlinePanel
.$element
1707 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1708 .append( this.outlineSelectWidget
.$element
);
1709 if ( this.editable
) {
1710 this.outlinePanel
.$element
1711 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1712 .append( this.outlineControlsWidget
.$element
);
1719 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.MenuLayout
);
1724 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the booklet layout.
1726 * @param {OO.ui.PageLayout} page Current page
1730 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
1733 * @param {OO.ui.PageLayout[]} page Added pages
1734 * @param {number} index Index pages were added at
1738 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
1739 * {@link #removePages removed} from the booklet.
1742 * @param {OO.ui.PageLayout[]} pages Removed pages
1748 * Handle stack layout focus.
1751 * @param {jQuery.Event} e Focusin event
1753 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
1756 // Find the page that an element was focused within
1757 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
1758 for ( name
in this.pages
) {
1759 // Check for page match, exclude current page to find only page changes
1760 if ( this.pages
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentPageName
) {
1761 this.setPage( name
);
1768 * Handle visibleItemChange events from the stackLayout
1770 * The next visible page is set as the current page by selecting it
1773 * @param {OO.ui.PageLayout} page The next visible page in the layout
1775 OO
.ui
.BookletLayout
.prototype.onStackLayoutVisibleItemChange = function ( page
) {
1776 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
1777 // try and scroll the item into view again.
1778 this.scrolling
= true;
1779 this.outlineSelectWidget
.selectItemByData( page
.getName() );
1780 this.scrolling
= false;
1784 * Handle stack layout set events.
1787 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
1789 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
1791 if ( !this.scrolling
&& page
) {
1792 page
.scrollElementIntoView( { complete: function () {
1793 if ( layout
.autoFocus
) {
1801 * Focus the first input in the current page.
1803 * If no page is selected, the first selectable page will be selected.
1804 * If the focus is already in an element on the current page, nothing will happen.
1805 * @param {number} [itemIndex] A specific item to focus on
1807 OO
.ui
.BookletLayout
.prototype.focus = function ( itemIndex
) {
1809 items
= this.stackLayout
.getItems();
1811 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
1812 page
= items
[ itemIndex
];
1814 page
= this.stackLayout
.getCurrentItem();
1817 if ( !page
&& this.outlined
) {
1818 this.selectFirstSelectablePage();
1819 page
= this.stackLayout
.getCurrentItem();
1824 // Only change the focus if is not already in the current page
1825 if ( !OO
.ui
.contains( page
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
1831 * Find the first focusable input in the booklet layout and focus
1834 OO
.ui
.BookletLayout
.prototype.focusFirstFocusable = function () {
1835 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
1839 * Handle outline widget select events.
1842 * @param {OO.ui.OptionWidget|null} item Selected item
1844 OO
.ui
.BookletLayout
.prototype.onOutlineSelectWidgetSelect = function ( item
) {
1846 this.setPage( item
.getData() );
1851 * Check if booklet has an outline.
1853 * @return {boolean} Booklet has an outline
1855 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
1856 return this.outlined
;
1860 * Check if booklet has editing controls.
1862 * @return {boolean} Booklet is editable
1864 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
1865 return this.editable
;
1869 * Check if booklet has a visible outline.
1871 * @return {boolean} Outline is visible
1873 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
1874 return this.outlined
&& this.outlineVisible
;
1878 * Hide or show the outline.
1880 * @param {boolean} [show] Show outline, omit to invert current state
1883 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
1884 if ( this.outlined
) {
1885 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
1886 this.outlineVisible
= show
;
1887 this.toggleMenu( show
);
1894 * Get the page closest to the specified page.
1896 * @param {OO.ui.PageLayout} page Page to use as a reference point
1897 * @return {OO.ui.PageLayout|null} Page closest to the specified page
1899 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
1900 var next
, prev
, level
,
1901 pages
= this.stackLayout
.getItems(),
1902 index
= pages
.indexOf( page
);
1904 if ( index
!== -1 ) {
1905 next
= pages
[ index
+ 1 ];
1906 prev
= pages
[ index
- 1 ];
1907 // Prefer adjacent pages at the same level
1908 if ( this.outlined
) {
1909 level
= this.outlineSelectWidget
.getItemFromData( page
.getName() ).getLevel();
1912 level
=== this.outlineSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
1918 level
=== this.outlineSelectWidget
.getItemFromData( next
.getName() ).getLevel()
1924 return prev
|| next
|| null;
1928 * Get the outline widget.
1930 * If the booklet is not outlined, the method will return `null`.
1932 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
1934 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
1935 return this.outlineSelectWidget
;
1939 * Get the outline controls widget.
1941 * If the outline is not editable, the method will return `null`.
1943 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
1945 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
1946 return this.outlineControlsWidget
;
1950 * Get a page by its symbolic name.
1952 * @param {string} name Symbolic name of page
1953 * @return {OO.ui.PageLayout|undefined} Page, if found
1955 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
1956 return this.pages
[ name
];
1960 * Get the current page.
1962 * @return {OO.ui.PageLayout|undefined} Current page, if found
1964 OO
.ui
.BookletLayout
.prototype.getCurrentPage = function () {
1965 var name
= this.getCurrentPageName();
1966 return name
? this.getPage( name
) : undefined;
1970 * Get the symbolic name of the current page.
1972 * @return {string|null} Symbolic name of the current page
1974 OO
.ui
.BookletLayout
.prototype.getCurrentPageName = function () {
1975 return this.currentPageName
;
1979 * Add pages to the booklet layout
1981 * When pages are added with the same names as existing pages, the existing pages will be
1982 * automatically removed before the new pages are added.
1984 * @param {OO.ui.PageLayout[]} pages Pages to add
1985 * @param {number} index Index of the insertion point
1989 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
1990 var i
, len
, name
, page
, item
, currentIndex
,
1991 stackLayoutPages
= this.stackLayout
.getItems(),
1995 // Remove pages with same names
1996 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
1998 name
= page
.getName();
2000 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
2001 // Correct the insertion index
2002 currentIndex
= stackLayoutPages
.indexOf( this.pages
[ name
] );
2003 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2006 remove
.push( this.pages
[ name
] );
2009 if ( remove
.length
) {
2010 this.removePages( remove
);
2014 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2016 name
= page
.getName();
2017 this.pages
[ page
.getName() ] = page
;
2018 if ( this.outlined
) {
2019 item
= new OO
.ui
.OutlineOptionWidget( { data
: name
} );
2020 page
.setOutlineItem( item
);
2025 if ( this.outlined
&& items
.length
) {
2026 this.outlineSelectWidget
.addItems( items
, index
);
2027 this.selectFirstSelectablePage();
2029 this.stackLayout
.addItems( pages
, index
);
2030 this.emit( 'add', pages
, index
);
2036 * Remove the specified pages from the booklet layout.
2038 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2040 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2044 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
2045 var i
, len
, name
, page
,
2048 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2050 name
= page
.getName();
2051 delete this.pages
[ name
];
2052 if ( this.outlined
) {
2053 items
.push( this.outlineSelectWidget
.getItemFromData( name
) );
2054 page
.setOutlineItem( null );
2057 if ( this.outlined
&& items
.length
) {
2058 this.outlineSelectWidget
.removeItems( items
);
2059 this.selectFirstSelectablePage();
2061 this.stackLayout
.removeItems( pages
);
2062 this.emit( 'remove', pages
);
2068 * Clear all pages from the booklet layout.
2070 * To remove only a subset of pages from the booklet, use the #removePages method.
2075 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
2077 pages
= this.stackLayout
.getItems();
2080 this.currentPageName
= null;
2081 if ( this.outlined
) {
2082 this.outlineSelectWidget
.clearItems();
2083 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
2084 pages
[ i
].setOutlineItem( null );
2087 this.stackLayout
.clearItems();
2089 this.emit( 'remove', pages
);
2095 * Set the current page by symbolic name.
2098 * @param {string} name Symbolic name of page
2100 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
2103 page
= this.pages
[ name
],
2104 previousPage
= this.currentPageName
&& this.pages
[ this.currentPageName
];
2106 if ( name
!== this.currentPageName
) {
2107 if ( this.outlined
) {
2108 selectedItem
= this.outlineSelectWidget
.getSelectedItem();
2109 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2110 this.outlineSelectWidget
.selectItemByData( name
);
2114 if ( previousPage
) {
2115 previousPage
.setActive( false );
2116 // Blur anything focused if the next page doesn't have anything focusable.
2117 // This is not needed if the next page has something focusable (because once it is focused
2118 // this blur happens automatically). If the layout is non-continuous, this check is
2119 // meaningless because the next page is not visible yet and thus can't hold focus.
2122 this.stackLayout
.continuous
&&
2123 OO
.ui
.findFocusable( page
.$element
).length
!== 0
2125 $focused
= previousPage
.$element
.find( ':focus' );
2126 if ( $focused
.length
) {
2127 $focused
[ 0 ].blur();
2131 this.currentPageName
= name
;
2132 page
.setActive( true );
2133 this.stackLayout
.setItem( page
);
2134 if ( !this.stackLayout
.continuous
&& previousPage
) {
2135 // This should not be necessary, since any inputs on the previous page should have been
2136 // blurred when it was hidden, but browsers are not very consistent about this.
2137 $focused
= previousPage
.$element
.find( ':focus' );
2138 if ( $focused
.length
) {
2139 $focused
[ 0 ].blur();
2142 this.emit( 'set', page
);
2148 * Select the first selectable page.
2152 OO
.ui
.BookletLayout
.prototype.selectFirstSelectablePage = function () {
2153 if ( !this.outlineSelectWidget
.getSelectedItem() ) {
2154 this.outlineSelectWidget
.selectItem( this.outlineSelectWidget
.getFirstSelectableItem() );
2161 * IndexLayouts contain {@link OO.ui.CardLayout card layouts} as well as
2162 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the cards and
2163 * select which one to display. By default, only one card is displayed at a time. When a user
2164 * navigates to a new card, the index layout automatically focuses on the first focusable element,
2165 * unless the default setting is changed.
2167 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2170 * // Example of a IndexLayout that contains two CardLayouts.
2172 * function CardOneLayout( name, config ) {
2173 * CardOneLayout.parent.call( this, name, config );
2174 * this.$element.append( '<p>First card</p>' );
2176 * OO.inheritClass( CardOneLayout, OO.ui.CardLayout );
2177 * CardOneLayout.prototype.setupTabItem = function () {
2178 * this.tabItem.setLabel( 'Card one' );
2181 * var card1 = new CardOneLayout( 'one' ),
2182 * card2 = new CardLayout( 'two', { label: 'Card two' } );
2184 * card2.$element.append( '<p>Second card</p>' );
2186 * var index = new OO.ui.IndexLayout();
2188 * index.addCards ( [ card1, card2 ] );
2189 * $( 'body' ).append( index.$element );
2192 * @extends OO.ui.MenuLayout
2195 * @param {Object} [config] Configuration options
2196 * @cfg {boolean} [continuous=false] Show all cards, one after another
2197 * @cfg {boolean} [expanded=true] Expand the content panel to fill the entire parent element.
2198 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new card is displayed.
2200 OO
.ui
.IndexLayout
= function OoUiIndexLayout( config
) {
2201 // Configuration initialization
2202 config
= $.extend( {}, config
, { menuPosition
: 'top' } );
2204 // Parent constructor
2205 OO
.ui
.IndexLayout
.parent
.call( this, config
);
2208 this.currentCardName
= null;
2210 this.ignoreFocus
= false;
2211 this.stackLayout
= new OO
.ui
.StackLayout( {
2212 continuous
: !!config
.continuous
,
2213 expanded
: config
.expanded
2215 this.$content
.append( this.stackLayout
.$element
);
2216 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
2218 this.tabSelectWidget
= new OO
.ui
.TabSelectWidget();
2219 this.tabPanel
= new OO
.ui
.PanelLayout();
2220 this.$menu
.append( this.tabPanel
.$element
);
2222 this.toggleMenu( true );
2225 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
2226 this.tabSelectWidget
.connect( this, { select
: 'onTabSelectWidgetSelect' } );
2227 if ( this.autoFocus
) {
2228 // Event 'focus' does not bubble, but 'focusin' does
2229 this.stackLayout
.$element
.on( 'focusin', this.onStackLayoutFocus
.bind( this ) );
2233 this.$element
.addClass( 'oo-ui-indexLayout' );
2234 this.stackLayout
.$element
.addClass( 'oo-ui-indexLayout-stackLayout' );
2235 this.tabPanel
.$element
2236 .addClass( 'oo-ui-indexLayout-tabPanel' )
2237 .append( this.tabSelectWidget
.$element
);
2242 OO
.inheritClass( OO
.ui
.IndexLayout
, OO
.ui
.MenuLayout
);
2247 * A 'set' event is emitted when a card is {@link #setCard set} to be displayed by the index layout.
2249 * @param {OO.ui.CardLayout} card Current card
2253 * An 'add' event is emitted when cards are {@link #addCards added} to the index layout.
2256 * @param {OO.ui.CardLayout[]} card Added cards
2257 * @param {number} index Index cards were added at
2261 * A 'remove' event is emitted when cards are {@link #clearCards cleared} or
2262 * {@link #removeCards removed} from the index.
2265 * @param {OO.ui.CardLayout[]} cards Removed cards
2271 * Handle stack layout focus.
2274 * @param {jQuery.Event} e Focusin event
2276 OO
.ui
.IndexLayout
.prototype.onStackLayoutFocus = function ( e
) {
2279 // Find the card that an element was focused within
2280 $target
= $( e
.target
).closest( '.oo-ui-cardLayout' );
2281 for ( name
in this.cards
) {
2282 // Check for card match, exclude current card to find only card changes
2283 if ( this.cards
[ name
].$element
[ 0 ] === $target
[ 0 ] && name
!== this.currentCardName
) {
2284 this.setCard( name
);
2291 * Handle stack layout set events.
2294 * @param {OO.ui.PanelLayout|null} card The card panel that is now the current panel
2296 OO
.ui
.IndexLayout
.prototype.onStackLayoutSet = function ( card
) {
2299 card
.scrollElementIntoView( { complete: function () {
2300 if ( layout
.autoFocus
) {
2308 * Focus the first input in the current card.
2310 * If no card is selected, the first selectable card will be selected.
2311 * If the focus is already in an element on the current card, nothing will happen.
2312 * @param {number} [itemIndex] A specific item to focus on
2314 OO
.ui
.IndexLayout
.prototype.focus = function ( itemIndex
) {
2316 items
= this.stackLayout
.getItems();
2318 if ( itemIndex
!== undefined && items
[ itemIndex
] ) {
2319 card
= items
[ itemIndex
];
2321 card
= this.stackLayout
.getCurrentItem();
2325 this.selectFirstSelectableCard();
2326 card
= this.stackLayout
.getCurrentItem();
2331 // Only change the focus if is not already in the current page
2332 if ( !OO
.ui
.contains( card
.$element
[ 0 ], this.getElementDocument().activeElement
, true ) ) {
2338 * Find the first focusable input in the index layout and focus
2341 OO
.ui
.IndexLayout
.prototype.focusFirstFocusable = function () {
2342 OO
.ui
.findFocusable( this.stackLayout
.$element
).focus();
2346 * Handle tab widget select events.
2349 * @param {OO.ui.OptionWidget|null} item Selected item
2351 OO
.ui
.IndexLayout
.prototype.onTabSelectWidgetSelect = function ( item
) {
2353 this.setCard( item
.getData() );
2358 * Get the card closest to the specified card.
2360 * @param {OO.ui.CardLayout} card Card to use as a reference point
2361 * @return {OO.ui.CardLayout|null} Card closest to the specified card
2363 OO
.ui
.IndexLayout
.prototype.getClosestCard = function ( card
) {
2364 var next
, prev
, level
,
2365 cards
= this.stackLayout
.getItems(),
2366 index
= cards
.indexOf( card
);
2368 if ( index
!== -1 ) {
2369 next
= cards
[ index
+ 1 ];
2370 prev
= cards
[ index
- 1 ];
2371 // Prefer adjacent cards at the same level
2372 level
= this.tabSelectWidget
.getItemFromData( card
.getName() ).getLevel();
2375 level
=== this.tabSelectWidget
.getItemFromData( prev
.getName() ).getLevel()
2381 level
=== this.tabSelectWidget
.getItemFromData( next
.getName() ).getLevel()
2386 return prev
|| next
|| null;
2390 * Get the tabs widget.
2392 * @return {OO.ui.TabSelectWidget} Tabs widget
2394 OO
.ui
.IndexLayout
.prototype.getTabs = function () {
2395 return this.tabSelectWidget
;
2399 * Get a card by its symbolic name.
2401 * @param {string} name Symbolic name of card
2402 * @return {OO.ui.CardLayout|undefined} Card, if found
2404 OO
.ui
.IndexLayout
.prototype.getCard = function ( name
) {
2405 return this.cards
[ name
];
2409 * Get the current card.
2411 * @return {OO.ui.CardLayout|undefined} Current card, if found
2413 OO
.ui
.IndexLayout
.prototype.getCurrentCard = function () {
2414 var name
= this.getCurrentCardName();
2415 return name
? this.getCard( name
) : undefined;
2419 * Get the symbolic name of the current card.
2421 * @return {string|null} Symbolic name of the current card
2423 OO
.ui
.IndexLayout
.prototype.getCurrentCardName = function () {
2424 return this.currentCardName
;
2428 * Add cards to the index layout
2430 * When cards are added with the same names as existing cards, the existing cards will be
2431 * automatically removed before the new cards are added.
2433 * @param {OO.ui.CardLayout[]} cards Cards to add
2434 * @param {number} index Index of the insertion point
2438 OO
.ui
.IndexLayout
.prototype.addCards = function ( cards
, index
) {
2439 var i
, len
, name
, card
, item
, currentIndex
,
2440 stackLayoutCards
= this.stackLayout
.getItems(),
2444 // Remove cards with same names
2445 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2447 name
= card
.getName();
2449 if ( Object
.prototype.hasOwnProperty
.call( this.cards
, name
) ) {
2450 // Correct the insertion index
2451 currentIndex
= stackLayoutCards
.indexOf( this.cards
[ name
] );
2452 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
2455 remove
.push( this.cards
[ name
] );
2458 if ( remove
.length
) {
2459 this.removeCards( remove
);
2463 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2465 name
= card
.getName();
2466 this.cards
[ card
.getName() ] = card
;
2467 item
= new OO
.ui
.TabOptionWidget( { data
: name
} );
2468 card
.setTabItem( item
);
2472 if ( items
.length
) {
2473 this.tabSelectWidget
.addItems( items
, index
);
2474 this.selectFirstSelectableCard();
2476 this.stackLayout
.addItems( cards
, index
);
2477 this.emit( 'add', cards
, index
);
2483 * Remove the specified cards from the index layout.
2485 * To remove all cards from the index, you may wish to use the #clearCards method instead.
2487 * @param {OO.ui.CardLayout[]} cards An array of cards to remove
2491 OO
.ui
.IndexLayout
.prototype.removeCards = function ( cards
) {
2492 var i
, len
, name
, card
,
2495 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2497 name
= card
.getName();
2498 delete this.cards
[ name
];
2499 items
.push( this.tabSelectWidget
.getItemFromData( name
) );
2500 card
.setTabItem( null );
2502 if ( items
.length
) {
2503 this.tabSelectWidget
.removeItems( items
);
2504 this.selectFirstSelectableCard();
2506 this.stackLayout
.removeItems( cards
);
2507 this.emit( 'remove', cards
);
2513 * Clear all cards from the index layout.
2515 * To remove only a subset of cards from the index, use the #removeCards method.
2520 OO
.ui
.IndexLayout
.prototype.clearCards = function () {
2522 cards
= this.stackLayout
.getItems();
2525 this.currentCardName
= null;
2526 this.tabSelectWidget
.clearItems();
2527 for ( i
= 0, len
= cards
.length
; i
< len
; i
++ ) {
2528 cards
[ i
].setTabItem( null );
2530 this.stackLayout
.clearItems();
2532 this.emit( 'remove', cards
);
2538 * Set the current card by symbolic name.
2541 * @param {string} name Symbolic name of card
2543 OO
.ui
.IndexLayout
.prototype.setCard = function ( name
) {
2546 card
= this.cards
[ name
],
2547 previousCard
= this.currentCardName
&& this.cards
[ this.currentCardName
];
2549 if ( name
!== this.currentCardName
) {
2550 selectedItem
= this.tabSelectWidget
.getSelectedItem();
2551 if ( selectedItem
&& selectedItem
.getData() !== name
) {
2552 this.tabSelectWidget
.selectItemByData( name
);
2555 if ( previousCard
) {
2556 previousCard
.setActive( false );
2557 // Blur anything focused if the next card doesn't have anything focusable.
2558 // This is not needed if the next card has something focusable (because once it is focused
2559 // this blur happens automatically). If the layout is non-continuous, this check is
2560 // meaningless because the next card is not visible yet and thus can't hold focus.
2563 this.stackLayout
.continuous
&&
2564 OO
.ui
.findFocusable( card
.$element
).length
!== 0
2566 $focused
= previousCard
.$element
.find( ':focus' );
2567 if ( $focused
.length
) {
2568 $focused
[ 0 ].blur();
2572 this.currentCardName
= name
;
2573 card
.setActive( true );
2574 this.stackLayout
.setItem( card
);
2575 if ( !this.stackLayout
.continuous
&& previousCard
) {
2576 // This should not be necessary, since any inputs on the previous card should have been
2577 // blurred when it was hidden, but browsers are not very consistent about this.
2578 $focused
= previousCard
.$element
.find( ':focus' );
2579 if ( $focused
.length
) {
2580 $focused
[ 0 ].blur();
2583 this.emit( 'set', card
);
2589 * Select the first selectable card.
2593 OO
.ui
.IndexLayout
.prototype.selectFirstSelectableCard = function () {
2594 if ( !this.tabSelectWidget
.getSelectedItem() ) {
2595 this.tabSelectWidget
.selectItem( this.tabSelectWidget
.getFirstSelectableItem() );
2602 * ToggleWidget implements basic behavior of widgets with an on/off state.
2603 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2607 * @extends OO.ui.Widget
2610 * @param {Object} [config] Configuration options
2611 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2612 * By default, the toggle is in the 'off' state.
2614 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
2615 // Configuration initialization
2616 config
= config
|| {};
2618 // Parent constructor
2619 OO
.ui
.ToggleWidget
.parent
.call( this, config
);
2625 this.$element
.addClass( 'oo-ui-toggleWidget' );
2626 this.setValue( !!config
.value
);
2631 OO
.inheritClass( OO
.ui
.ToggleWidget
, OO
.ui
.Widget
);
2638 * A change event is emitted when the on/off state of the toggle changes.
2640 * @param {boolean} value Value representing the new state of the toggle
2646 * Get the value representing the toggle’s state.
2648 * @return {boolean} The on/off state of the toggle
2650 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
2655 * Set the state of the toggle: `true` for 'on', `false' for 'off'.
2657 * @param {boolean} value The state of the toggle
2661 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
2663 if ( this.value
!== value
) {
2665 this.emit( 'change', value
);
2666 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
2667 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
2668 this.$element
.attr( 'aria-checked', value
.toString() );
2674 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
2675 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
2676 * configured with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators},
2677 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
2678 * and {@link OO.ui.mixin.LabelElement labels}. Please see
2679 * the [OOjs UI documentation][1] on MediaWiki for more information.
2682 * // Toggle buttons in the 'off' and 'on' state.
2683 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
2684 * label: 'Toggle Button off'
2686 * var toggleButton2 = new OO.ui.ToggleButtonWidget( {
2687 * label: 'Toggle Button on',
2690 * // Append the buttons to the DOM.
2691 * $( 'body' ).append( toggleButton1.$element, toggleButton2.$element );
2693 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Toggle_buttons
2696 * @extends OO.ui.ToggleWidget
2697 * @mixins OO.ui.mixin.ButtonElement
2698 * @mixins OO.ui.mixin.IconElement
2699 * @mixins OO.ui.mixin.IndicatorElement
2700 * @mixins OO.ui.mixin.LabelElement
2701 * @mixins OO.ui.mixin.TitledElement
2702 * @mixins OO.ui.mixin.FlaggedElement
2703 * @mixins OO.ui.mixin.TabIndexedElement
2706 * @param {Object} [config] Configuration options
2707 * @cfg {boolean} [value=false] The toggle button’s initial on/off
2708 * state. By default, the button is in the 'off' state.
2710 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
2711 // Configuration initialization
2712 config
= config
|| {};
2714 // Parent constructor
2715 OO
.ui
.ToggleButtonWidget
.parent
.call( this, config
);
2717 // Mixin constructors
2718 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
2719 OO
.ui
.mixin
.IconElement
.call( this, config
);
2720 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
2721 OO
.ui
.mixin
.LabelElement
.call( this, config
);
2722 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
2723 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
2724 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
2727 this.connect( this, { click
: 'onAction' } );
2730 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
2732 .addClass( 'oo-ui-toggleButtonWidget' )
2733 .append( this.$button
);
2738 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
2739 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
2740 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IconElement
);
2741 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
2742 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.LabelElement
);
2743 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TitledElement
);
2744 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
2745 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2750 * Handle the button action being triggered.
2754 OO
.ui
.ToggleButtonWidget
.prototype.onAction = function () {
2755 this.setValue( !this.value
);
2761 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
2763 if ( value
!== this.value
) {
2764 // Might be called from parent constructor before ButtonElement constructor
2765 if ( this.$button
) {
2766 this.$button
.attr( 'aria-pressed', value
.toString() );
2768 this.setActive( value
);
2772 OO
.ui
.ToggleButtonWidget
.parent
.prototype.setValue
.call( this, value
);
2780 OO
.ui
.ToggleButtonWidget
.prototype.setButtonElement = function ( $button
) {
2781 if ( this.$button
) {
2782 this.$button
.removeAttr( 'aria-pressed' );
2784 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement
.call( this, $button
);
2785 this.$button
.attr( 'aria-pressed', this.value
.toString() );
2789 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
2790 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
2791 * visually by a slider in the leftmost position.
2794 * // Toggle switches in the 'off' and 'on' position.
2795 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget();
2796 * var toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
2800 * // Create a FieldsetLayout to layout and label switches
2801 * var fieldset = new OO.ui.FieldsetLayout( {
2802 * label: 'Toggle switches'
2804 * fieldset.addItems( [
2805 * new OO.ui.FieldLayout( toggleSwitch1, { label: 'Off', align: 'top' } ),
2806 * new OO.ui.FieldLayout( toggleSwitch2, { label: 'On', align: 'top' } )
2808 * $( 'body' ).append( fieldset.$element );
2811 * @extends OO.ui.ToggleWidget
2812 * @mixins OO.ui.mixin.TabIndexedElement
2815 * @param {Object} [config] Configuration options
2816 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
2817 * By default, the toggle switch is in the 'off' position.
2819 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
2820 // Parent constructor
2821 OO
.ui
.ToggleSwitchWidget
.parent
.call( this, config
);
2823 // Mixin constructors
2824 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
2827 this.dragging
= false;
2828 this.dragStart
= null;
2829 this.sliding
= false;
2830 this.$glow
= $( '<span>' );
2831 this.$grip
= $( '<span>' );
2835 click
: this.onClick
.bind( this ),
2836 keypress
: this.onKeyPress
.bind( this )
2840 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
2841 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
2843 .addClass( 'oo-ui-toggleSwitchWidget' )
2844 .attr( 'role', 'checkbox' )
2845 .append( this.$glow
, this.$grip
);
2850 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
2851 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.mixin
.TabIndexedElement
);
2856 * Handle mouse click events.
2859 * @param {jQuery.Event} e Mouse click event
2861 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
2862 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
2863 this.setValue( !this.value
);
2869 * Handle key press events.
2872 * @param {jQuery.Event} e Key press event
2874 OO
.ui
.ToggleSwitchWidget
.prototype.onKeyPress = function ( e
) {
2875 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
2876 this.setValue( !this.value
);
2882 * OutlineControlsWidget is a set of controls for an {@link OO.ui.OutlineSelectWidget outline select widget}.
2883 * Controls include moving items up and down, removing items, and adding different kinds of items.
2885 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
2888 * @extends OO.ui.Widget
2889 * @mixins OO.ui.mixin.GroupElement
2890 * @mixins OO.ui.mixin.IconElement
2893 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
2894 * @param {Object} [config] Configuration options
2895 * @cfg {Object} [abilities] List of abilties
2896 * @cfg {boolean} [abilities.move=true] Allow moving movable items
2897 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
2899 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
2900 // Allow passing positional parameters inside the config object
2901 if ( OO
.isPlainObject( outline
) && config
=== undefined ) {
2903 outline
= config
.outline
;
2906 // Configuration initialization
2907 config
= $.extend( { icon
: 'add' }, config
);
2909 // Parent constructor
2910 OO
.ui
.OutlineControlsWidget
.parent
.call( this, config
);
2912 // Mixin constructors
2913 OO
.ui
.mixin
.GroupElement
.call( this, config
);
2914 OO
.ui
.mixin
.IconElement
.call( this, config
);
2917 this.outline
= outline
;
2918 this.$movers
= $( '<div>' );
2919 this.upButton
= new OO
.ui
.ButtonWidget( {
2922 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
2924 this.downButton
= new OO
.ui
.ButtonWidget( {
2927 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
2929 this.removeButton
= new OO
.ui
.ButtonWidget( {
2932 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
2934 this.abilities
= { move: true, remove
: true };
2937 outline
.connect( this, {
2938 select
: 'onOutlineChange',
2939 add
: 'onOutlineChange',
2940 remove
: 'onOutlineChange'
2942 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
2943 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
2944 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
2947 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
2948 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
2950 .addClass( 'oo-ui-outlineControlsWidget-movers' )
2951 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
2952 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
2953 this.setAbilities( config
.abilities
|| {} );
2958 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
2959 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.GroupElement
);
2960 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.mixin
.IconElement
);
2966 * @param {number} places Number of places to move
2978 * @param {Object} abilities List of abilties
2979 * @param {boolean} [abilities.move] Allow moving movable items
2980 * @param {boolean} [abilities.remove] Allow removing removable items
2982 OO
.ui
.OutlineControlsWidget
.prototype.setAbilities = function ( abilities
) {
2985 for ( ability
in this.abilities
) {
2986 if ( abilities
[ ability
] !== undefined ) {
2987 this.abilities
[ ability
] = !!abilities
[ ability
];
2991 this.onOutlineChange();
2996 * Handle outline change events.
2998 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
2999 var i
, len
, firstMovable
, lastMovable
,
3000 items
= this.outline
.getItems(),
3001 selectedItem
= this.outline
.getSelectedItem(),
3002 movable
= this.abilities
.move && selectedItem
&& selectedItem
.isMovable(),
3003 removable
= this.abilities
.remove
&& selectedItem
&& selectedItem
.isRemovable();
3008 while ( ++i
< len
) {
3009 if ( items
[ i
].isMovable() ) {
3010 firstMovable
= items
[ i
];
3016 if ( items
[ i
].isMovable() ) {
3017 lastMovable
= items
[ i
];
3022 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
3023 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
3024 this.removeButton
.setDisabled( !removable
);
3028 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3030 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3031 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3035 * @extends OO.ui.DecoratedOptionWidget
3038 * @param {Object} [config] Configuration options
3039 * @cfg {number} [level] Indentation level
3040 * @cfg {boolean} [movable] Allow modification from {@link OO.ui.OutlineControlsWidget outline controls}.
3042 OO
.ui
.OutlineOptionWidget
= function OoUiOutlineOptionWidget( config
) {
3043 // Configuration initialization
3044 config
= config
|| {};
3046 // Parent constructor
3047 OO
.ui
.OutlineOptionWidget
.parent
.call( this, config
);
3051 this.movable
= !!config
.movable
;
3052 this.removable
= !!config
.removable
;
3055 this.$element
.addClass( 'oo-ui-outlineOptionWidget' );
3056 this.setLevel( config
.level
);
3061 OO
.inheritClass( OO
.ui
.OutlineOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3063 /* Static Properties */
3065 OO
.ui
.OutlineOptionWidget
.static.highlightable
= false;
3067 OO
.ui
.OutlineOptionWidget
.static.scrollIntoViewOnSelect
= true;
3069 OO
.ui
.OutlineOptionWidget
.static.levelClass
= 'oo-ui-outlineOptionWidget-level-';
3071 OO
.ui
.OutlineOptionWidget
.static.levels
= 3;
3076 * Check if item is movable.
3078 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3080 * @return {boolean} Item is movable
3082 OO
.ui
.OutlineOptionWidget
.prototype.isMovable = function () {
3083 return this.movable
;
3087 * Check if item is removable.
3089 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3091 * @return {boolean} Item is removable
3093 OO
.ui
.OutlineOptionWidget
.prototype.isRemovable = function () {
3094 return this.removable
;
3098 * Get indentation level.
3100 * @return {number} Indentation level
3102 OO
.ui
.OutlineOptionWidget
.prototype.getLevel = function () {
3109 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3111 * @param {boolean} movable Item is movable
3114 OO
.ui
.OutlineOptionWidget
.prototype.setMovable = function ( movable
) {
3115 this.movable
= !!movable
;
3116 this.updateThemeClasses();
3123 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3125 * @param {boolean} removable Item is removable
3128 OO
.ui
.OutlineOptionWidget
.prototype.setRemovable = function ( removable
) {
3129 this.removable
= !!removable
;
3130 this.updateThemeClasses();
3135 * Set indentation level.
3137 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3140 OO
.ui
.OutlineOptionWidget
.prototype.setLevel = function ( level
) {
3141 var levels
= this.constructor.static.levels
,
3142 levelClass
= this.constructor.static.levelClass
,
3145 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
3147 if ( this.level
=== i
) {
3148 this.$element
.addClass( levelClass
+ i
);
3150 this.$element
.removeClass( levelClass
+ i
);
3153 this.updateThemeClasses();
3159 * OutlineSelectWidget is a structured list that contains {@link OO.ui.OutlineOptionWidget outline options}
3160 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls} widget.
3162 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3165 * @extends OO.ui.SelectWidget
3166 * @mixins OO.ui.mixin.TabIndexedElement
3169 * @param {Object} [config] Configuration options
3171 OO
.ui
.OutlineSelectWidget
= function OoUiOutlineSelectWidget( config
) {
3172 // Parent constructor
3173 OO
.ui
.OutlineSelectWidget
.parent
.call( this, config
);
3175 // Mixin constructors
3176 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3180 focus
: this.bindKeyDownListener
.bind( this ),
3181 blur
: this.unbindKeyDownListener
.bind( this )
3185 this.$element
.addClass( 'oo-ui-outlineSelectWidget' );
3190 OO
.inheritClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.SelectWidget
);
3191 OO
.mixinClass( OO
.ui
.OutlineSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3194 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3195 * can be selected and configured with data. The class is
3196 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3197 * [OOjs UI documentation on MediaWiki] [1] for more information.
3199 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Button_selects_and_options
3202 * @extends OO.ui.DecoratedOptionWidget
3203 * @mixins OO.ui.mixin.ButtonElement
3204 * @mixins OO.ui.mixin.TabIndexedElement
3205 * @mixins OO.ui.mixin.TitledElement
3208 * @param {Object} [config] Configuration options
3210 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( config
) {
3211 // Configuration initialization
3212 config
= config
|| {};
3214 // Parent constructor
3215 OO
.ui
.ButtonOptionWidget
.parent
.call( this, config
);
3217 // Mixin constructors
3218 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3219 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
3220 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, {
3221 $tabIndexed
: this.$button
,
3226 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
3227 this.$button
.append( this.$element
.contents() );
3228 this.$element
.append( this.$button
);
3233 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
3234 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.ButtonElement
);
3235 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.TitledElement
);
3236 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3238 /* Static Properties */
3240 // Allow button mouse down events to pass through so they can be handled by the parent select widget
3241 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
3243 OO
.ui
.ButtonOptionWidget
.static.highlightable
= false;
3250 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
3251 OO
.ui
.ButtonOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
3253 if ( this.constructor.static.selectable
) {
3254 this.setActive( state
);
3261 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3262 * button options and is used together with
3263 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3264 * highlighting, choosing, and selecting mutually exclusive options. Please see
3265 * the [OOjs UI documentation on MediaWiki] [1] for more information.
3268 * // Example: A ButtonSelectWidget that contains three ButtonOptionWidgets
3269 * var option1 = new OO.ui.ButtonOptionWidget( {
3271 * label: 'Option 1',
3272 * title: 'Button option 1'
3275 * var option2 = new OO.ui.ButtonOptionWidget( {
3277 * label: 'Option 2',
3278 * title: 'Button option 2'
3281 * var option3 = new OO.ui.ButtonOptionWidget( {
3283 * label: 'Option 3',
3284 * title: 'Button option 3'
3287 * var buttonSelect=new OO.ui.ButtonSelectWidget( {
3288 * items: [ option1, option2, option3 ]
3290 * $( 'body' ).append( buttonSelect.$element );
3292 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
3295 * @extends OO.ui.SelectWidget
3296 * @mixins OO.ui.mixin.TabIndexedElement
3299 * @param {Object} [config] Configuration options
3301 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
3302 // Parent constructor
3303 OO
.ui
.ButtonSelectWidget
.parent
.call( this, config
);
3305 // Mixin constructors
3306 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3310 focus
: this.bindKeyDownListener
.bind( this ),
3311 blur
: this.unbindKeyDownListener
.bind( this )
3315 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
3320 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
3321 OO
.mixinClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3324 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3326 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3327 * {@link OO.ui.CardLayout card layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3331 * @extends OO.ui.OptionWidget
3334 * @param {Object} [config] Configuration options
3336 OO
.ui
.TabOptionWidget
= function OoUiTabOptionWidget( config
) {
3337 // Configuration initialization
3338 config
= config
|| {};
3340 // Parent constructor
3341 OO
.ui
.TabOptionWidget
.parent
.call( this, config
);
3344 this.$element
.addClass( 'oo-ui-tabOptionWidget' );
3349 OO
.inheritClass( OO
.ui
.TabOptionWidget
, OO
.ui
.OptionWidget
);
3351 /* Static Properties */
3353 OO
.ui
.TabOptionWidget
.static.highlightable
= false;
3356 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3358 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3361 * @extends OO.ui.SelectWidget
3362 * @mixins OO.ui.mixin.TabIndexedElement
3365 * @param {Object} [config] Configuration options
3367 OO
.ui
.TabSelectWidget
= function OoUiTabSelectWidget( config
) {
3368 // Parent constructor
3369 OO
.ui
.TabSelectWidget
.parent
.call( this, config
);
3371 // Mixin constructors
3372 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
3376 focus
: this.bindKeyDownListener
.bind( this ),
3377 blur
: this.unbindKeyDownListener
.bind( this )
3381 this.$element
.addClass( 'oo-ui-tabSelectWidget' );
3386 OO
.inheritClass( OO
.ui
.TabSelectWidget
, OO
.ui
.SelectWidget
);
3387 OO
.mixinClass( OO
.ui
.TabSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3390 * CapsuleItemWidgets are used within a {@link OO.ui.CapsuleMultiSelectWidget
3391 * CapsuleMultiSelectWidget} to display the selected items.
3394 * @extends OO.ui.Widget
3395 * @mixins OO.ui.mixin.ItemWidget
3396 * @mixins OO.ui.mixin.IndicatorElement
3397 * @mixins OO.ui.mixin.LabelElement
3398 * @mixins OO.ui.mixin.FlaggedElement
3399 * @mixins OO.ui.mixin.TabIndexedElement
3402 * @param {Object} [config] Configuration options
3404 OO
.ui
.CapsuleItemWidget
= function OoUiCapsuleItemWidget( config
) {
3405 // Configuration initialization
3406 config
= config
|| {};
3408 // Parent constructor
3409 OO
.ui
.CapsuleItemWidget
.parent
.call( this, config
);
3411 // Properties (must be set before mixin constructor calls)
3412 this.$indicator
= $( '<span>' );
3414 // Mixin constructors
3415 OO
.ui
.mixin
.ItemWidget
.call( this );
3416 OO
.ui
.mixin
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$indicator
, indicator
: 'clear' } ) );
3417 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3418 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3419 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} ) );
3422 this.$indicator
.on( {
3423 keydown
: this.onCloseKeyDown
.bind( this ),
3424 click
: this.onCloseClick
.bind( this )
3429 .addClass( 'oo-ui-capsuleItemWidget' )
3430 .append( this.$indicator
, this.$label
);
3435 OO
.inheritClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.Widget
);
3436 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.ItemWidget
);
3437 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.IndicatorElement
);
3438 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.LabelElement
);
3439 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.FlaggedElement
);
3440 OO
.mixinClass( OO
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3445 * Handle close icon clicks
3446 * @param {jQuery.Event} event
3448 OO
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
3449 var element
= this.getElementGroup();
3451 if ( !this.isDisabled() && element
&& $.isFunction( element
.removeItems
) ) {
3452 element
.removeItems( [ this ] );
3458 * Handle close keyboard events
3459 * @param {jQuery.Event} event Key down event
3461 OO
.ui
.CapsuleItemWidget
.prototype.onCloseKeyDown = function ( e
) {
3462 if ( !this.isDisabled() && $.isFunction( this.getElementGroup().removeItems
) ) {
3463 switch ( e
.which
) {
3464 case OO
.ui
.Keys
.ENTER
:
3465 case OO
.ui
.Keys
.BACKSPACE
:
3466 case OO
.ui
.Keys
.SPACE
:
3467 this.getElementGroup().removeItems( [ this ] );
3474 * CapsuleMultiSelectWidgets are something like a {@link OO.ui.ComboBoxInputWidget combo box widget}
3475 * that allows for selecting multiple values.
3477 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
3480 * // Example: A CapsuleMultiSelectWidget.
3481 * var capsule = new OO.ui.CapsuleMultiSelectWidget( {
3482 * label: 'CapsuleMultiSelectWidget',
3483 * selected: [ 'Option 1', 'Option 3' ],
3486 * new OO.ui.MenuOptionWidget( {
3488 * label: 'Option One'
3490 * new OO.ui.MenuOptionWidget( {
3492 * label: 'Option Two'
3494 * new OO.ui.MenuOptionWidget( {
3496 * label: 'Option Three'
3498 * new OO.ui.MenuOptionWidget( {
3500 * label: 'Option Four'
3502 * new OO.ui.MenuOptionWidget( {
3504 * label: 'Option Five'
3509 * $( 'body' ).append( capsule.$element );
3511 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
3514 * @extends OO.ui.Widget
3515 * @mixins OO.ui.mixin.TabIndexedElement
3516 * @mixins OO.ui.mixin.GroupElement
3519 * @param {Object} [config] Configuration options
3520 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if not present in the menu.
3521 * @cfg {Object} [menu] Configuration options to pass to the {@link OO.ui.MenuSelectWidget menu select widget}.
3522 * @cfg {Object} [popup] Configuration options to pass to the {@link OO.ui.PopupWidget popup widget}.
3523 * If specified, this popup will be shown instead of the menu (but the menu
3524 * will still be used for item labels and allowArbitrary=false). The widgets
3525 * in the popup should use this.addItemsFromData() or this.addItems() as necessary.
3526 * @cfg {jQuery} [$overlay] Render the menu or popup into a separate layer.
3527 * This configuration is useful in cases where the expanded menu is larger than
3528 * its containing `<div>`. The specified overlay layer is usually on top of
3529 * the containing `<div>` and has a larger area. By default, the menu uses
3530 * relative positioning.
3532 OO
.ui
.CapsuleMultiSelectWidget
= function OoUiCapsuleMultiSelectWidget( config
) {
3535 // Configuration initialization
3536 config
= config
|| {};
3538 // Parent constructor
3539 OO
.ui
.CapsuleMultiSelectWidget
.parent
.call( this, config
);
3541 // Properties (must be set before mixin constructor calls)
3542 this.$input
= config
.popup
? null : $( '<input>' );
3543 this.$handle
= $( '<div>' );
3545 // Mixin constructors
3546 OO
.ui
.mixin
.GroupElement
.call( this, config
);
3547 if ( config
.popup
) {
3548 config
.popup
= $.extend( {}, config
.popup
, {
3552 OO
.ui
.mixin
.PopupElement
.call( this, config
);
3553 $tabFocus
= $( '<span>' );
3554 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: $tabFocus
} ) );
3558 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
3560 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3561 OO
.ui
.mixin
.IconElement
.call( this, config
);
3564 this.$content
= $( '<div>' );
3565 this.allowArbitrary
= !!config
.allowArbitrary
;
3566 this.$overlay
= config
.$overlay
|| this.$element
;
3567 this.menu
= new OO
.ui
.FloatingMenuSelectWidget( $.extend(
3570 $input
: this.$input
,
3571 $container
: this.$element
,
3572 filterFromInput
: true,
3573 disabled
: this.isDisabled()
3581 focus
: this.onFocusForPopup
.bind( this )
3583 this.popup
.$element
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3584 if ( this.popup
.$autoCloseIgnore
) {
3585 this.popup
.$autoCloseIgnore
.on( 'focusout', this.onPopupFocusOut
.bind( this ) );
3587 this.popup
.connect( this, {
3588 toggle: function ( visible
) {
3589 $tabFocus
.toggle( !visible
);
3594 focus
: this.onInputFocus
.bind( this ),
3595 blur
: this.onInputBlur
.bind( this ),
3596 'propertychange change click mouseup keydown keyup input cut paste select focus':
3597 OO
.ui
.debounce( this.updateInputSize
.bind( this ) ),
3598 keydown
: this.onKeyDown
.bind( this ),
3599 keypress
: this.onKeyPress
.bind( this )
3602 this.menu
.connect( this, {
3603 choose
: 'onMenuChoose',
3604 add
: 'onMenuItemsChange',
3605 remove
: 'onMenuItemsChange'
3608 mousedown
: this.onMouseDown
.bind( this )
3612 if ( this.$input
) {
3613 this.$input
.prop( 'disabled', this.isDisabled() );
3616 'aria-autocomplete': 'list'
3618 this.updateInputSize();
3620 if ( config
.data
) {
3621 this.setItemsFromData( config
.data
);
3623 this.$content
.addClass( 'oo-ui-capsuleMultiSelectWidget-content' )
3624 .append( this.$group
);
3625 this.$group
.addClass( 'oo-ui-capsuleMultiSelectWidget-group' );
3626 this.$handle
.addClass( 'oo-ui-capsuleMultiSelectWidget-handle' )
3627 .append( this.$indicator
, this.$icon
, this.$content
);
3628 this.$element
.addClass( 'oo-ui-capsuleMultiSelectWidget' )
3629 .append( this.$handle
);
3631 this.$content
.append( $tabFocus
);
3632 this.$overlay
.append( this.popup
.$element
);
3634 this.$content
.append( this.$input
);
3635 this.$overlay
.append( this.menu
.$element
);
3637 this.onMenuItemsChange();
3642 OO
.inheritClass( OO
.ui
.CapsuleMultiSelectWidget
, OO
.ui
.Widget
);
3643 OO
.mixinClass( OO
.ui
.CapsuleMultiSelectWidget
, OO
.ui
.mixin
.GroupElement
);
3644 OO
.mixinClass( OO
.ui
.CapsuleMultiSelectWidget
, OO
.ui
.mixin
.PopupElement
);
3645 OO
.mixinClass( OO
.ui
.CapsuleMultiSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3646 OO
.mixinClass( OO
.ui
.CapsuleMultiSelectWidget
, OO
.ui
.mixin
.IndicatorElement
);
3647 OO
.mixinClass( OO
.ui
.CapsuleMultiSelectWidget
, OO
.ui
.mixin
.IconElement
);
3654 * A change event is emitted when the set of selected items changes.
3656 * @param {Mixed[]} datas Data of the now-selected items
3662 * Construct a OO.ui.CapsuleItemWidget (or a subclass thereof) from given label and data.
3665 * @param {Mixed} data Custom data of any type.
3666 * @param {string} label The label text.
3667 * @return {OO.ui.CapsuleItemWidget}
3669 OO
.ui
.CapsuleMultiSelectWidget
.prototype.createItemWidget = function ( data
, label
) {
3670 return new OO
.ui
.CapsuleItemWidget( { data
: data
, label
: label
} );
3674 * Get the data of the items in the capsule
3677 OO
.ui
.CapsuleMultiSelectWidget
.prototype.getItemsData = function () {
3678 return $.map( this.getItems(), function ( e
) { return e
.data
; } );
3682 * Set the items in the capsule by providing data
3684 * @param {Mixed[]} datas
3685 * @return {OO.ui.CapsuleMultiSelectWidget}
3687 OO
.ui
.CapsuleMultiSelectWidget
.prototype.setItemsFromData = function ( datas
) {
3690 items
= this.getItems();
3692 $.each( datas
, function ( i
, data
) {
3694 item
= menu
.getItemFromData( data
);
3698 } else if ( widget
.allowArbitrary
) {
3699 label
= String( data
);
3705 for ( j
= 0; j
< items
.length
; j
++ ) {
3706 if ( items
[ j
].data
=== data
&& items
[ j
].label
=== label
) {
3708 items
.splice( j
, 1 );
3713 item
= widget
.createItemWidget( data
, label
);
3715 widget
.addItems( [ item
], i
);
3718 if ( items
.length
) {
3719 widget
.removeItems( items
);
3726 * Add items to the capsule by providing their data
3728 * @param {Mixed[]} datas
3729 * @return {OO.ui.CapsuleMultiSelectWidget}
3731 OO
.ui
.CapsuleMultiSelectWidget
.prototype.addItemsFromData = function ( datas
) {
3736 $.each( datas
, function ( i
, data
) {
3739 if ( !widget
.getItemFromData( data
) ) {
3740 item
= menu
.getItemFromData( data
);
3742 items
.push( widget
.createItemWidget( data
, item
.label
) );
3743 } else if ( widget
.allowArbitrary
) {
3744 items
.push( widget
.createItemWidget( data
, String( data
) ) );
3749 if ( items
.length
) {
3750 this.addItems( items
);
3757 * Remove items by data
3759 * @param {Mixed[]} datas
3760 * @return {OO.ui.CapsuleMultiSelectWidget}
3762 OO
.ui
.CapsuleMultiSelectWidget
.prototype.removeItemsFromData = function ( datas
) {
3766 $.each( datas
, function ( i
, data
) {
3767 var item
= widget
.getItemFromData( data
);
3773 if ( items
.length
) {
3774 this.removeItems( items
);
3783 OO
.ui
.CapsuleMultiSelectWidget
.prototype.addItems = function ( items
) {
3785 oldItems
= this.items
.slice();
3787 OO
.ui
.mixin
.GroupElement
.prototype.addItems
.call( this, items
);
3789 if ( this.items
.length
!== oldItems
.length
) {
3793 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
3794 same
= same
&& this.items
[ i
] === oldItems
[ i
];
3798 this.emit( 'change', this.getItemsData() );
3799 this.menu
.position();
3808 OO
.ui
.CapsuleMultiSelectWidget
.prototype.removeItems = function ( items
) {
3810 oldItems
= this.items
.slice();
3812 OO
.ui
.mixin
.GroupElement
.prototype.removeItems
.call( this, items
);
3814 if ( this.items
.length
!== oldItems
.length
) {
3818 for ( i
= 0, l
= oldItems
.length
; same
&& i
< l
; i
++ ) {
3819 same
= same
&& this.items
[ i
] === oldItems
[ i
];
3823 this.emit( 'change', this.getItemsData() );
3824 this.menu
.position();
3833 OO
.ui
.CapsuleMultiSelectWidget
.prototype.clearItems = function () {
3834 if ( this.items
.length
) {
3835 OO
.ui
.mixin
.GroupElement
.prototype.clearItems
.call( this );
3836 this.emit( 'change', this.getItemsData() );
3837 this.menu
.position();
3843 * Get the capsule widget's menu.
3844 * @return {OO.ui.MenuSelectWidget} Menu widget
3846 OO
.ui
.CapsuleMultiSelectWidget
.prototype.getMenu = function () {
3851 * Handle focus events
3854 * @param {jQuery.Event} event
3856 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onInputFocus = function () {
3857 if ( !this.isDisabled() ) {
3858 this.menu
.toggle( true );
3863 * Handle blur events
3866 * @param {jQuery.Event} event
3868 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onInputBlur = function () {
3869 if ( this.allowArbitrary
&& this.$input
.val().trim() !== '' ) {
3870 this.addItemsFromData( [ this.$input
.val() ] );
3876 * Handle focus events
3879 * @param {jQuery.Event} event
3881 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onFocusForPopup = function () {
3882 if ( !this.isDisabled() ) {
3883 this.popup
.setSize( this.$handle
.width() );
3884 this.popup
.toggle( true );
3885 this.popup
.$element
.find( '*' )
3886 .filter( function () { return OO
.ui
.isFocusableElement( $( this ), true ); } )
3893 * Handles popup focus out events.
3896 * @param {Event} e Focus out event
3898 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onPopupFocusOut = function () {
3899 var widget
= this.popup
;
3901 setTimeout( function () {
3903 widget
.isVisible() &&
3904 !OO
.ui
.contains( widget
.$element
[ 0 ], document
.activeElement
, true ) &&
3905 ( !widget
.$autoCloseIgnore
|| !widget
.$autoCloseIgnore
.has( document
.activeElement
).length
)
3907 widget
.toggle( false );
3913 * Handle mouse down events.
3916 * @param {jQuery.Event} e Mouse down event
3918 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onMouseDown = function ( e
) {
3919 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
3923 this.updateInputSize();
3928 * Handle key press events.
3931 * @param {jQuery.Event} e Key press event
3933 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onKeyPress = function ( e
) {
3936 if ( !this.isDisabled() ) {
3937 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
3942 if ( !this.popup
) {
3943 this.menu
.toggle( true );
3944 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
3945 item
= this.menu
.getItemFromLabel( this.$input
.val(), true );
3947 this.addItemsFromData( [ item
.data
] );
3949 } else if ( this.allowArbitrary
&& this.$input
.val().trim() !== '' ) {
3950 this.addItemsFromData( [ this.$input
.val() ] );
3956 // Make sure the input gets resized.
3957 setTimeout( this.updateInputSize
.bind( this ), 0 );
3963 * Handle key down events.
3966 * @param {jQuery.Event} e Key down event
3968 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onKeyDown = function ( e
) {
3969 if ( !this.isDisabled() ) {
3970 // 'keypress' event is not triggered for Backspace
3971 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
&& this.$input
.val() === '' ) {
3972 if ( this.items
.length
) {
3973 this.removeItems( this.items
.slice( -1 ) );
3981 * Update the dimensions of the text input field to encompass all available area.
3984 * @param {jQuery.Event} e Event of some sort
3986 OO
.ui
.CapsuleMultiSelectWidget
.prototype.updateInputSize = function () {
3987 var $lastItem
, direction
, contentWidth
, currentWidth
, bestWidth
;
3988 if ( !this.isDisabled() ) {
3989 this.$input
.css( 'width', '1em' );
3990 $lastItem
= this.$group
.children().last();
3991 direction
= OO
.ui
.Element
.static.getDir( this.$handle
);
3992 contentWidth
= this.$input
[ 0 ].scrollWidth
;
3993 currentWidth
= this.$input
.width();
3995 if ( contentWidth
< currentWidth
) {
3996 // All is fine, don't perform expensive calculations
4000 if ( !$lastItem
.length
) {
4001 bestWidth
= this.$content
.innerWidth();
4003 bestWidth
= direction
=== 'ltr' ?
4004 this.$content
.innerWidth() - $lastItem
.position().left
- $lastItem
.outerWidth() :
4005 $lastItem
.position().left
;
4007 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
4008 // pixels this is off by are coming from.
4010 if ( contentWidth
> bestWidth
) {
4011 // This will result in the input getting shifted to the next line
4012 bestWidth
= this.$content
.innerWidth() - 10;
4014 this.$input
.width( Math
.floor( bestWidth
) );
4016 this.menu
.position();
4021 * Handle menu choose events.
4024 * @param {OO.ui.OptionWidget} item Chosen item
4026 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onMenuChoose = function ( item
) {
4027 if ( item
&& item
.isVisible() ) {
4028 this.addItemsFromData( [ item
.getData() ] );
4034 * Handle menu item change events.
4038 OO
.ui
.CapsuleMultiSelectWidget
.prototype.onMenuItemsChange = function () {
4039 this.setItemsFromData( this.getItemsData() );
4040 this.$element
.toggleClass( 'oo-ui-capsuleMultiSelectWidget-empty', this.menu
.isEmpty() );
4044 * Clear the input field
4047 OO
.ui
.CapsuleMultiSelectWidget
.prototype.clearInput = function () {
4048 if ( this.$input
) {
4049 this.$input
.val( '' );
4050 this.updateInputSize();
4053 this.popup
.toggle( false );
4055 this.menu
.toggle( false );
4056 this.menu
.selectItem();
4057 this.menu
.highlightItem();
4063 OO
.ui
.CapsuleMultiSelectWidget
.prototype.setDisabled = function ( disabled
) {
4067 OO
.ui
.CapsuleMultiSelectWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
4069 if ( this.$input
) {
4070 this.$input
.prop( 'disabled', this.isDisabled() );
4073 this.menu
.setDisabled( this.isDisabled() );
4076 this.popup
.setDisabled( this.isDisabled() );
4080 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
4081 this.items
[ i
].updateDisabled();
4091 * @return {OO.ui.CapsuleMultiSelectWidget}
4093 OO
.ui
.CapsuleMultiSelectWidget
.prototype.focus = function () {
4094 if ( !this.isDisabled() ) {
4096 this.popup
.setSize( this.$handle
.width() );
4097 this.popup
.toggle( true );
4098 this.popup
.$element
.find( '*' )
4099 .filter( function () { return OO
.ui
.isFocusableElement( $( this ), true ); } )
4103 this.updateInputSize();
4104 this.menu
.toggle( true );
4105 this.$input
.focus();
4112 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
4113 * widgets can be configured with {@link OO.ui.mixin.IconElement icons} and {@link
4114 * OO.ui.mixin.IndicatorElement indicators}.
4115 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
4118 * // Example of a file select widget
4119 * var selectFile = new OO.ui.SelectFileWidget();
4120 * $( 'body' ).append( selectFile.$element );
4122 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets
4125 * @extends OO.ui.Widget
4126 * @mixins OO.ui.mixin.IconElement
4127 * @mixins OO.ui.mixin.IndicatorElement
4128 * @mixins OO.ui.mixin.PendingElement
4129 * @mixins OO.ui.mixin.LabelElement
4132 * @param {Object} [config] Configuration options
4133 * @cfg {string[]|null} [accept=null] MIME types to accept. null accepts all types.
4134 * @cfg {string} [placeholder] Text to display when no file is selected.
4135 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
4136 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
4137 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be true.
4138 * @cfg {boolean} [dragDropUI=false] Deprecated alias for showDropTarget
4140 OO
.ui
.SelectFileWidget
= function OoUiSelectFileWidget( config
) {
4143 // TODO: Remove in next release
4144 if ( config
&& config
.dragDropUI
) {
4145 config
.showDropTarget
= true;
4148 // Configuration initialization
4149 config
= $.extend( {
4151 placeholder
: OO
.ui
.msg( 'ooui-selectfile-placeholder' ),
4152 notsupported
: OO
.ui
.msg( 'ooui-selectfile-not-supported' ),
4154 showDropTarget
: false
4157 // Parent constructor
4158 OO
.ui
.SelectFileWidget
.parent
.call( this, config
);
4160 // Mixin constructors
4161 OO
.ui
.mixin
.IconElement
.call( this, config
);
4162 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
4163 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {}, config
, { $pending
: this.$info
} ) );
4164 OO
.ui
.mixin
.LabelElement
.call( this, $.extend( {}, config
, { autoFitLabel
: true } ) );
4167 this.$info
= $( '<span>' );
4170 this.showDropTarget
= config
.showDropTarget
;
4171 this.isSupported
= this.constructor.static.isSupported();
4172 this.currentFile
= null;
4173 if ( Array
.isArray( config
.accept
) ) {
4174 this.accept
= config
.accept
;
4178 this.placeholder
= config
.placeholder
;
4179 this.notsupported
= config
.notsupported
;
4180 this.onFileSelectedHandler
= this.onFileSelected
.bind( this );
4182 this.selectButton
= new OO
.ui
.ButtonWidget( {
4183 classes
: [ 'oo-ui-selectFileWidget-selectButton' ],
4184 label
: OO
.ui
.msg( 'ooui-selectfile-button-select' ),
4185 disabled
: this.disabled
|| !this.isSupported
4188 this.clearButton
= new OO
.ui
.ButtonWidget( {
4189 classes
: [ 'oo-ui-selectFileWidget-clearButton' ],
4192 disabled
: this.disabled
4196 this.selectButton
.$button
.on( {
4197 keypress
: this.onKeyPress
.bind( this )
4199 this.clearButton
.connect( this, {
4200 click
: 'onClearClick'
4202 if ( config
.droppable
) {
4203 dragHandler
= this.onDragEnterOrOver
.bind( this );
4205 dragenter
: dragHandler
,
4206 dragover
: dragHandler
,
4207 dragleave
: this.onDragLeave
.bind( this ),
4208 drop
: this.onDrop
.bind( this )
4215 this.$label
.addClass( 'oo-ui-selectFileWidget-label' );
4217 .addClass( 'oo-ui-selectFileWidget-info' )
4218 .append( this.$icon
, this.$label
, this.clearButton
.$element
, this.$indicator
);
4220 .addClass( 'oo-ui-selectFileWidget' )
4221 .append( this.$info
, this.selectButton
.$element
);
4222 if ( config
.droppable
&& config
.showDropTarget
) {
4223 this.$dropTarget
= $( '<div>' )
4224 .addClass( 'oo-ui-selectFileWidget-dropTarget' )
4225 .text( OO
.ui
.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
4227 click
: this.onDropTargetClick
.bind( this )
4229 this.$element
.prepend( this.$dropTarget
);
4235 OO
.inheritClass( OO
.ui
.SelectFileWidget
, OO
.ui
.Widget
);
4236 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IconElement
);
4237 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.IndicatorElement
);
4238 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.PendingElement
);
4239 OO
.mixinClass( OO
.ui
.SelectFileWidget
, OO
.ui
.mixin
.LabelElement
);
4241 /* Static Properties */
4244 * Check if this widget is supported
4249 OO
.ui
.SelectFileWidget
.static.isSupported = function () {
4251 if ( OO
.ui
.SelectFileWidget
.static.isSupportedCache
=== null ) {
4252 $input
= $( '<input type="file">' );
4253 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= $input
[ 0 ].files
!== undefined;
4255 return OO
.ui
.SelectFileWidget
.static.isSupportedCache
;
4258 OO
.ui
.SelectFileWidget
.static.isSupportedCache
= null;
4265 * A change event is emitted when the on/off state of the toggle changes.
4267 * @param {File|null} value New value
4273 * Get the current value of the field
4275 * @return {File|null}
4277 OO
.ui
.SelectFileWidget
.prototype.getValue = function () {
4278 return this.currentFile
;
4282 * Set the current value of the field
4284 * @param {File|null} file File to select
4286 OO
.ui
.SelectFileWidget
.prototype.setValue = function ( file
) {
4287 if ( this.currentFile
!== file
) {
4288 this.currentFile
= file
;
4290 this.emit( 'change', this.currentFile
);
4297 * Focusses the select file button.
4301 OO
.ui
.SelectFileWidget
.prototype.focus = function () {
4302 this.selectButton
.$button
[ 0 ].focus();
4307 * Update the user interface when a file is selected or unselected
4311 OO
.ui
.SelectFileWidget
.prototype.updateUI = function () {
4313 if ( !this.isSupported
) {
4314 this.$element
.addClass( 'oo-ui-selectFileWidget-notsupported' );
4315 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
4316 this.setLabel( this.notsupported
);
4318 this.$element
.addClass( 'oo-ui-selectFileWidget-supported' );
4319 if ( this.currentFile
) {
4320 this.$element
.removeClass( 'oo-ui-selectFileWidget-empty' );
4322 $label
= $label
.add(
4324 .addClass( 'oo-ui-selectFileWidget-fileName' )
4325 .text( this.currentFile
.name
)
4327 if ( this.currentFile
.type
!== '' ) {
4328 $label
= $label
.add(
4330 .addClass( 'oo-ui-selectFileWidget-fileType' )
4331 .text( this.currentFile
.type
)
4334 this.setLabel( $label
);
4336 this.$element
.addClass( 'oo-ui-selectFileWidget-empty' );
4337 this.setLabel( this.placeholder
);
4343 * Add the input to the widget
4347 OO
.ui
.SelectFileWidget
.prototype.addInput = function () {
4348 if ( this.$input
) {
4349 this.$input
.remove();
4352 if ( !this.isSupported
) {
4357 this.$input
= $( '<input type="file">' );
4358 this.$input
.on( 'change', this.onFileSelectedHandler
);
4362 if ( this.accept
) {
4363 this.$input
.attr( 'accept', this.accept
.join( ', ' ) );
4365 this.selectButton
.$button
.append( this.$input
);
4369 * Determine if we should accept this file
4372 * @param {string} File MIME type
4375 OO
.ui
.SelectFileWidget
.prototype.isAllowedType = function ( mimeType
) {
4378 if ( !this.accept
|| !mimeType
) {
4382 for ( i
= 0; i
< this.accept
.length
; i
++ ) {
4383 mimeTest
= this.accept
[ i
];
4384 if ( mimeTest
=== mimeType
) {
4386 } else if ( mimeTest
.substr( -2 ) === '/*' ) {
4387 mimeTest
= mimeTest
.substr( 0, mimeTest
.length
- 1 );
4388 if ( mimeType
.substr( 0, mimeTest
.length
) === mimeTest
) {
4398 * Handle file selection from the input
4401 * @param {jQuery.Event} e
4403 OO
.ui
.SelectFileWidget
.prototype.onFileSelected = function ( e
) {
4404 var file
= OO
.getProp( e
.target
, 'files', 0 ) || null;
4406 if ( file
&& !this.isAllowedType( file
.type
) ) {
4410 this.setValue( file
);
4415 * Handle clear button click events.
4419 OO
.ui
.SelectFileWidget
.prototype.onClearClick = function () {
4420 this.setValue( null );
4425 * Handle key press events.
4428 * @param {jQuery.Event} e Key press event
4430 OO
.ui
.SelectFileWidget
.prototype.onKeyPress = function ( e
) {
4431 if ( this.isSupported
&& !this.isDisabled() && this.$input
&&
4432 ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
4434 this.$input
.click();
4440 * Handle drop target click events.
4443 * @param {jQuery.Event} e Key press event
4445 OO
.ui
.SelectFileWidget
.prototype.onDropTargetClick = function () {
4446 if ( this.isSupported
&& !this.isDisabled() && this.$input
) {
4447 this.$input
.click();
4453 * Handle drag enter and over events
4456 * @param {jQuery.Event} e Drag event
4458 OO
.ui
.SelectFileWidget
.prototype.onDragEnterOrOver = function ( e
) {
4460 droppableFile
= false,
4461 dt
= e
.originalEvent
.dataTransfer
;
4464 e
.stopPropagation();
4466 if ( this.isDisabled() || !this.isSupported
) {
4467 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
4468 dt
.dropEffect
= 'none';
4472 // DataTransferItem and File both have a type property, but in Chrome files
4473 // have no information at this point.
4474 itemOrFile
= OO
.getProp( dt
, 'items', 0 ) || OO
.getProp( dt
, 'files', 0 );
4476 if ( this.isAllowedType( itemOrFile
.type
) ) {
4477 droppableFile
= true;
4479 // dt.types is Array-like, but not an Array
4480 } else if ( Array
.prototype.indexOf
.call( OO
.getProp( dt
, 'types' ) || [], 'Files' ) !== -1 ) {
4481 // File information is not available at this point for security so just assume
4482 // it is acceptable for now.
4483 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
4484 droppableFile
= true;
4487 this.$element
.toggleClass( 'oo-ui-selectFileWidget-canDrop', droppableFile
);
4488 if ( !droppableFile
) {
4489 dt
.dropEffect
= 'none';
4496 * Handle drag leave events
4499 * @param {jQuery.Event} e Drag event
4501 OO
.ui
.SelectFileWidget
.prototype.onDragLeave = function () {
4502 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
4506 * Handle drop events
4509 * @param {jQuery.Event} e Drop event
4511 OO
.ui
.SelectFileWidget
.prototype.onDrop = function ( e
) {
4513 dt
= e
.originalEvent
.dataTransfer
;
4516 e
.stopPropagation();
4517 this.$element
.removeClass( 'oo-ui-selectFileWidget-canDrop' );
4519 if ( this.isDisabled() || !this.isSupported
) {
4523 file
= OO
.getProp( dt
, 'files', 0 );
4524 if ( file
&& !this.isAllowedType( file
.type
) ) {
4528 this.setValue( file
);
4537 OO
.ui
.SelectFileWidget
.prototype.setDisabled = function ( disabled
) {
4538 OO
.ui
.SelectFileWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
4539 if ( this.selectButton
) {
4540 this.selectButton
.setDisabled( disabled
);
4542 if ( this.clearButton
) {
4543 this.clearButton
.setDisabled( disabled
);
4549 * Progress bars visually display the status of an operation, such as a download,
4550 * and can be either determinate or indeterminate:
4552 * - **determinate** process bars show the percent of an operation that is complete.
4554 * - **indeterminate** process bars use a visual display of motion to indicate that an operation
4555 * is taking place. Because the extent of an indeterminate operation is unknown, the bar does
4556 * not use percentages.
4558 * The value of the `progress` configuration determines whether the bar is determinate or indeterminate.
4561 * // Examples of determinate and indeterminate progress bars.
4562 * var progressBar1 = new OO.ui.ProgressBarWidget( {
4565 * var progressBar2 = new OO.ui.ProgressBarWidget();
4567 * // Create a FieldsetLayout to layout progress bars
4568 * var fieldset = new OO.ui.FieldsetLayout;
4569 * fieldset.addItems( [
4570 * new OO.ui.FieldLayout( progressBar1, {label: 'Determinate', align: 'top'}),
4571 * new OO.ui.FieldLayout( progressBar2, {label: 'Indeterminate', align: 'top'})
4573 * $( 'body' ).append( fieldset.$element );
4576 * @extends OO.ui.Widget
4579 * @param {Object} [config] Configuration options
4580 * @cfg {number|boolean} [progress=false] The type of progress bar (determinate or indeterminate).
4581 * To create a determinate progress bar, specify a number that reflects the initial percent complete.
4582 * By default, the progress bar is indeterminate.
4584 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
4585 // Configuration initialization
4586 config
= config
|| {};
4588 // Parent constructor
4589 OO
.ui
.ProgressBarWidget
.parent
.call( this, config
);
4592 this.$bar
= $( '<div>' );
4593 this.progress
= null;
4596 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
4597 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar' );
4600 role
: 'progressbar',
4602 'aria-valuemax': 100
4604 .addClass( 'oo-ui-progressBarWidget' )
4605 .append( this.$bar
);
4610 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
4612 /* Static Properties */
4614 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
4619 * Get the percent of the progress that has been completed. Indeterminate progresses will return `false`.
4621 * @return {number|boolean} Progress percent
4623 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
4624 return this.progress
;
4628 * Set the percent of the process completed or `false` for an indeterminate process.
4630 * @param {number|boolean} progress Progress percent or `false` for indeterminate
4632 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
4633 this.progress
= progress
;
4635 if ( progress
!== false ) {
4636 this.$bar
.css( 'width', this.progress
+ '%' );
4637 this.$element
.attr( 'aria-valuenow', this.progress
);
4639 this.$bar
.css( 'width', '' );
4640 this.$element
.removeAttr( 'aria-valuenow' );
4642 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', !progress
);
4646 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field}, where users can type a search query,
4647 * and a menu of search results, which is displayed beneath the query
4648 * field. Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible to the user.
4649 * Users can choose an item from the menu or type a query into the text field to search for a matching result item.
4650 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
4652 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
4653 * the [OOjs UI demos][1] for an example.
4655 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/#dialogs-mediawiki-vector-ltr
4658 * @extends OO.ui.Widget
4661 * @param {Object} [config] Configuration options
4662 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
4663 * @cfg {string} [value] Initial query value
4665 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
4666 // Configuration initialization
4667 config
= config
|| {};
4669 // Parent constructor
4670 OO
.ui
.SearchWidget
.parent
.call( this, config
);
4673 this.query
= new OO
.ui
.TextInputWidget( {
4675 placeholder
: config
.placeholder
,
4678 this.results
= new OO
.ui
.SelectWidget();
4679 this.$query
= $( '<div>' );
4680 this.$results
= $( '<div>' );
4683 this.query
.connect( this, {
4684 change
: 'onQueryChange',
4685 enter
: 'onQueryEnter'
4687 this.query
.$input
.on( 'keydown', this.onQueryKeydown
.bind( this ) );
4691 .addClass( 'oo-ui-searchWidget-query' )
4692 .append( this.query
.$element
);
4694 .addClass( 'oo-ui-searchWidget-results' )
4695 .append( this.results
.$element
);
4697 .addClass( 'oo-ui-searchWidget' )
4698 .append( this.$results
, this.$query
);
4703 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
4708 * Handle query key down events.
4711 * @param {jQuery.Event} e Key down event
4713 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
4714 var highlightedItem
, nextItem
,
4715 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
4718 highlightedItem
= this.results
.getHighlightedItem();
4719 if ( !highlightedItem
) {
4720 highlightedItem
= this.results
.getSelectedItem();
4722 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
4723 this.results
.highlightItem( nextItem
);
4724 nextItem
.scrollElementIntoView();
4729 * Handle select widget select events.
4731 * Clears existing results. Subclasses should repopulate items according to new query.
4734 * @param {string} value New value
4736 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
4738 this.results
.clearItems();
4742 * Handle select widget enter key events.
4744 * Chooses highlighted item.
4747 * @param {string} value New value
4749 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
4750 var highlightedItem
= this.results
.getHighlightedItem();
4751 if ( highlightedItem
) {
4752 this.results
.chooseItem( highlightedItem
);
4757 * Get the query input.
4759 * @return {OO.ui.TextInputWidget} Query input
4761 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
4766 * Get the search results menu.
4768 * @return {OO.ui.SelectWidget} Menu of search results
4770 OO
.ui
.SearchWidget
.prototype.getResults = function () {
4771 return this.results
;
4775 * NumberInputWidgets combine a {@link OO.ui.TextInputWidget text input} (where a value
4776 * can be entered manually) and two {@link OO.ui.ButtonWidget button widgets}
4777 * (to adjust the value in increments) to allow the user to enter a number.
4780 * // Example: A NumberInputWidget.
4781 * var numberInput = new OO.ui.NumberInputWidget( {
4782 * label: 'NumberInputWidget',
4783 * input: { value: 5, min: 1, max: 10 }
4785 * $( 'body' ).append( numberInput.$element );
4788 * @extends OO.ui.Widget
4791 * @param {Object} [config] Configuration options
4792 * @cfg {Object} [input] Configuration options to pass to the {@link OO.ui.TextInputWidget text input widget}.
4793 * @cfg {Object} [minusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget decrementing button widget}.
4794 * @cfg {Object} [plusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget incrementing button widget}.
4795 * @cfg {boolean} [isInteger=false] Whether the field accepts only integer values.
4796 * @cfg {number} [min=-Infinity] Minimum allowed value
4797 * @cfg {number} [max=Infinity] Maximum allowed value
4798 * @cfg {number} [step=1] Delta when using the buttons or up/down arrow keys
4799 * @cfg {number|null} [pageStep] Delta when using the page-up/page-down keys. Defaults to 10 times #step.
4801 OO
.ui
.NumberInputWidget
= function OoUiNumberInputWidget( config
) {
4802 // Configuration initialization
4803 config
= $.extend( {
4811 // Parent constructor
4812 OO
.ui
.NumberInputWidget
.parent
.call( this, config
);
4815 this.input
= new OO
.ui
.TextInputWidget( $.extend(
4817 disabled
: this.isDisabled()
4821 this.minusButton
= new OO
.ui
.ButtonWidget( $.extend(
4823 disabled
: this.isDisabled(),
4828 classes
: [ 'oo-ui-numberInputWidget-minusButton' ],
4832 this.plusButton
= new OO
.ui
.ButtonWidget( $.extend(
4834 disabled
: this.isDisabled(),
4839 classes
: [ 'oo-ui-numberInputWidget-plusButton' ],
4845 this.input
.connect( this, {
4846 change
: this.emit
.bind( this, 'change' ),
4847 enter
: this.emit
.bind( this, 'enter' )
4849 this.input
.$input
.on( {
4850 keydown
: this.onKeyDown
.bind( this ),
4851 'wheel mousewheel DOMMouseScroll': this.onWheel
.bind( this )
4853 this.plusButton
.connect( this, {
4854 click
: [ 'onButtonClick', +1 ]
4856 this.minusButton
.connect( this, {
4857 click
: [ 'onButtonClick', -1 ]
4861 this.setIsInteger( !!config
.isInteger
);
4862 this.setRange( config
.min
, config
.max
);
4863 this.setStep( config
.step
, config
.pageStep
);
4865 this.$field
= $( '<div>' ).addClass( 'oo-ui-numberInputWidget-field' )
4867 this.minusButton
.$element
,
4868 this.input
.$element
,
4869 this.plusButton
.$element
4871 this.$element
.addClass( 'oo-ui-numberInputWidget' ).append( this.$field
);
4872 this.input
.setValidation( this.validateNumber
.bind( this ) );
4877 OO
.inheritClass( OO
.ui
.NumberInputWidget
, OO
.ui
.Widget
);
4882 * A `change` event is emitted when the value of the input changes.
4888 * An `enter` event is emitted when the user presses 'enter' inside the text box.
4896 * Set whether only integers are allowed
4897 * @param {boolean} flag
4899 OO
.ui
.NumberInputWidget
.prototype.setIsInteger = function ( flag
) {
4900 this.isInteger
= !!flag
;
4901 this.input
.setValidityFlag();
4905 * Get whether only integers are allowed
4906 * @return {boolean} Flag value
4908 OO
.ui
.NumberInputWidget
.prototype.getIsInteger = function () {
4909 return this.isInteger
;
4913 * Set the range of allowed values
4914 * @param {number} min Minimum allowed value
4915 * @param {number} max Maximum allowed value
4917 OO
.ui
.NumberInputWidget
.prototype.setRange = function ( min
, max
) {
4919 throw new Error( 'Minimum (' + min
+ ') must not be greater than maximum (' + max
+ ')' );
4923 this.input
.setValidityFlag();
4927 * Get the current range
4928 * @return {number[]} Minimum and maximum values
4930 OO
.ui
.NumberInputWidget
.prototype.getRange = function () {
4931 return [ this.min
, this.max
];
4935 * Set the stepping deltas
4936 * @param {number} step Normal step
4937 * @param {number|null} pageStep Page step. If null, 10 * step will be used.
4939 OO
.ui
.NumberInputWidget
.prototype.setStep = function ( step
, pageStep
) {
4941 throw new Error( 'Step value must be positive' );
4943 if ( pageStep
=== null ) {
4944 pageStep
= step
* 10;
4945 } else if ( pageStep
<= 0 ) {
4946 throw new Error( 'Page step value must be positive' );
4949 this.pageStep
= pageStep
;
4953 * Get the current stepping values
4954 * @return {number[]} Step and page step
4956 OO
.ui
.NumberInputWidget
.prototype.getStep = function () {
4957 return [ this.step
, this.pageStep
];
4961 * Get the current value of the widget
4964 OO
.ui
.NumberInputWidget
.prototype.getValue = function () {
4965 return this.input
.getValue();
4969 * Get the current value of the widget as a number
4970 * @return {number} May be NaN, or an invalid number
4972 OO
.ui
.NumberInputWidget
.prototype.getNumericValue = function () {
4973 return +this.input
.getValue();
4977 * Set the value of the widget
4978 * @param {string} value Invalid values are allowed
4980 OO
.ui
.NumberInputWidget
.prototype.setValue = function ( value
) {
4981 this.input
.setValue( value
);
4985 * Adjust the value of the widget
4986 * @param {number} delta Adjustment amount
4988 OO
.ui
.NumberInputWidget
.prototype.adjustValue = function ( delta
) {
4989 var n
, v
= this.getNumericValue();
4992 if ( isNaN( delta
) || !isFinite( delta
) ) {
4993 throw new Error( 'Delta must be a finite number' );
5000 n
= Math
.max( Math
.min( n
, this.max
), this.min
);
5001 if ( this.isInteger
) {
5002 n
= Math
.round( n
);
5014 * @param {string} value Field value
5017 OO
.ui
.NumberInputWidget
.prototype.validateNumber = function ( value
) {
5019 if ( isNaN( n
) || !isFinite( n
) ) {
5023 /*jshint bitwise: false */
5024 if ( this.isInteger
&& ( n
| 0 ) !== n
) {
5027 /*jshint bitwise: true */
5029 if ( n
< this.min
|| n
> this.max
) {
5037 * Handle mouse click events.
5040 * @param {number} dir +1 or -1
5042 OO
.ui
.NumberInputWidget
.prototype.onButtonClick = function ( dir
) {
5043 this.adjustValue( dir
* this.step
);
5047 * Handle mouse wheel events.
5050 * @param {jQuery.Event} event
5052 OO
.ui
.NumberInputWidget
.prototype.onWheel = function ( event
) {
5055 // Standard 'wheel' event
5056 if ( event
.originalEvent
.deltaMode
!== undefined ) {
5057 this.sawWheelEvent
= true;
5059 if ( event
.originalEvent
.deltaY
) {
5060 delta
= -event
.originalEvent
.deltaY
;
5061 } else if ( event
.originalEvent
.deltaX
) {
5062 delta
= event
.originalEvent
.deltaX
;
5065 // Non-standard events
5066 if ( !this.sawWheelEvent
) {
5067 if ( event
.originalEvent
.wheelDeltaX
) {
5068 delta
= -event
.originalEvent
.wheelDeltaX
;
5069 } else if ( event
.originalEvent
.wheelDeltaY
) {
5070 delta
= event
.originalEvent
.wheelDeltaY
;
5071 } else if ( event
.originalEvent
.wheelDelta
) {
5072 delta
= event
.originalEvent
.wheelDelta
;
5073 } else if ( event
.originalEvent
.detail
) {
5074 delta
= -event
.originalEvent
.detail
;
5079 delta
= delta
< 0 ? -1 : 1;
5080 this.adjustValue( delta
* this.step
);
5087 * Handle key down events.
5090 * @param {jQuery.Event} e Key down event
5092 OO
.ui
.NumberInputWidget
.prototype.onKeyDown = function ( e
) {
5093 if ( !this.isDisabled() ) {
5094 switch ( e
.which
) {
5096 this.adjustValue( this.step
);
5098 case OO
.ui
.Keys
.DOWN
:
5099 this.adjustValue( -this.step
);
5101 case OO
.ui
.Keys
.PAGEUP
:
5102 this.adjustValue( this.pageStep
);
5104 case OO
.ui
.Keys
.PAGEDOWN
:
5105 this.adjustValue( -this.pageStep
);
5114 OO
.ui
.NumberInputWidget
.prototype.setDisabled = function ( disabled
) {
5116 OO
.ui
.NumberInputWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
5119 this.input
.setDisabled( this.isDisabled() );
5121 if ( this.minusButton
) {
5122 this.minusButton
.setDisabled( this.isDisabled() );
5124 if ( this.plusButton
) {
5125 this.plusButton
.setDisabled( this.isDisabled() );