Merge "Move around "ا" to after "آ" and not before"
[lhc/web/wiklou.git] / resources / lib / oojs-ui / oojs-ui-widgets.js
1 /*!
2 * OOjs UI v0.23.0
3 * https://www.mediawiki.org/wiki/OOjs_UI
4 *
5 * Copyright 2011–2017 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
8 *
9 * Date: 2017-09-05T21:23:58Z
10 */
11 ( function ( OO ) {
12
13 'use strict';
14
15 /**
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.
20 *
21 * @abstract
22 * @class
23 *
24 * @constructor
25 * @param {Object} [config] Configuration options
26 * @cfg {jQuery} [$handle] The part of the element which can be used for dragging, defaults to the whole element
27 * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
28 * but the draggable state should be called from the DraggableGroupElement, which updates
29 * the whole group
30 */
31 OO.ui.mixin.DraggableElement = function OoUiMixinDraggableElement( config ) {
32 config = config || {};
33
34 // Properties
35 this.index = null;
36 this.$handle = config.$handle || this.$element;
37 this.wasHandleUsed = null;
38
39 // Initialize and events
40 this.$element
41 .addClass( 'oo-ui-draggableElement' )
42 .on( {
43 mousedown: this.onDragMouseDown.bind( this ),
44 dragstart: this.onDragStart.bind( this ),
45 dragover: this.onDragOver.bind( this ),
46 dragend: this.onDragEnd.bind( this ),
47 drop: this.onDrop.bind( this )
48 } );
49 this.$handle.addClass( 'oo-ui-draggableElement-handle' );
50 this.toggleDraggable( config.draggable === undefined ? true : !!config.draggable );
51 };
52
53 OO.initClass( OO.ui.mixin.DraggableElement );
54
55 /* Events */
56
57 /**
58 * @event dragstart
59 *
60 * A dragstart event is emitted when the user clicks and begins dragging an item.
61 * @param {OO.ui.mixin.DraggableElement} item The item the user has clicked and is dragging with the mouse.
62 */
63
64 /**
65 * @event dragend
66 * A dragend event is emitted when the user drags an item and releases the mouse,
67 * thus terminating the drag operation.
68 */
69
70 /**
71 * @event drop
72 * A drop event is emitted when the user drags an item and then releases the mouse button
73 * over a valid target.
74 */
75
76 /* Static Properties */
77
78 /**
79 * @inheritdoc OO.ui.mixin.ButtonElement
80 */
81 OO.ui.mixin.DraggableElement.static.cancelButtonMouseDownEvents = false;
82
83 /* Methods */
84
85 /**
86 * Change the draggable state of this widget.
87 * This allows users to temporarily halt the dragging operations.
88 *
89 * @param {boolean} isDraggable Widget supports draggable operations
90 * @fires draggable
91 */
92 OO.ui.mixin.DraggableElement.prototype.toggleDraggable = function ( isDraggable ) {
93 isDraggable = isDraggable !== undefined ? !!isDraggable : !this.draggable;
94
95 if ( this.draggable !== isDraggable ) {
96 this.draggable = isDraggable;
97
98 this.$handle.toggleClass( 'oo-ui-draggableElement-undraggable', !this.draggable );
99
100 // We make the entire element draggable, not just the handle, so that
101 // the whole element appears to move. wasHandleUsed prevents drags from
102 // starting outside the handle
103 this.$element.prop( 'draggable', this.draggable );
104 }
105 };
106
107 /**
108 * Check the draggable state of this widget
109 *
110 * @return {boolean} Widget supports draggable operations
111 */
112 OO.ui.mixin.DraggableElement.prototype.isDraggable = function () {
113 return this.draggable;
114 };
115
116 /**
117 * Respond to mousedown event.
118 *
119 * @private
120 * @param {jQuery.Event} e Drag event
121 */
122 OO.ui.mixin.DraggableElement.prototype.onDragMouseDown = function ( e ) {
123 if ( !this.isDraggable() ) {
124 return;
125 }
126
127 this.wasHandleUsed =
128 // Optimization: if the handle is the whole element this is always true
129 this.$handle[ 0 ] === this.$element[ 0 ] ||
130 // Check the mousedown occurred inside the handle
131 OO.ui.contains( this.$handle[ 0 ], e.target, true );
132 };
133
134 /**
135 * Respond to dragstart event.
136 *
137 * @private
138 * @param {jQuery.Event} e Drag event
139 * @return {boolean} False if the event is cancelled
140 * @fires dragstart
141 */
142 OO.ui.mixin.DraggableElement.prototype.onDragStart = function ( e ) {
143 var element = this,
144 dataTransfer = e.originalEvent.dataTransfer;
145
146 if ( !this.wasHandleUsed || !this.isDraggable() ) {
147 return false;
148 }
149
150 // Define drop effect
151 dataTransfer.dropEffect = 'none';
152 dataTransfer.effectAllowed = 'move';
153 // Support: Firefox
154 // We must set up a dataTransfer data property or Firefox seems to
155 // ignore the fact the element is draggable.
156 try {
157 dataTransfer.setData( 'application-x/OOjs-UI-draggable', this.getIndex() );
158 } catch ( err ) {
159 // The above is only for Firefox. Move on if it fails.
160 }
161 // Briefly add a 'clone' class to style the browser's native drag image
162 this.$element.addClass( 'oo-ui-draggableElement-clone' );
163 // Add placeholder class after the browser has rendered the clone
164 setTimeout( function () {
165 element.$element
166 .removeClass( 'oo-ui-draggableElement-clone' )
167 .addClass( 'oo-ui-draggableElement-placeholder' );
168 } );
169 // Emit event
170 this.emit( 'dragstart', this );
171 return true;
172 };
173
174 /**
175 * Respond to dragend event.
176 *
177 * @private
178 * @fires dragend
179 */
180 OO.ui.mixin.DraggableElement.prototype.onDragEnd = function () {
181 this.$element.removeClass( 'oo-ui-draggableElement-placeholder' );
182 this.emit( 'dragend' );
183 };
184
185 /**
186 * Handle drop event.
187 *
188 * @private
189 * @param {jQuery.Event} e Drop event
190 * @fires drop
191 */
192 OO.ui.mixin.DraggableElement.prototype.onDrop = function ( e ) {
193 e.preventDefault();
194 this.emit( 'drop', e );
195 };
196
197 /**
198 * In order for drag/drop to work, the dragover event must
199 * return false and stop propogation.
200 *
201 * @param {jQuery.Event} e Drag event
202 * @private
203 */
204 OO.ui.mixin.DraggableElement.prototype.onDragOver = function ( e ) {
205 e.preventDefault();
206 };
207
208 /**
209 * Set item index.
210 * Store it in the DOM so we can access from the widget drag event
211 *
212 * @private
213 * @param {number} index Item index
214 */
215 OO.ui.mixin.DraggableElement.prototype.setIndex = function ( index ) {
216 if ( this.index !== index ) {
217 this.index = index;
218 this.$element.data( 'index', index );
219 }
220 };
221
222 /**
223 * Get item index
224 *
225 * @private
226 * @return {number} Item index
227 */
228 OO.ui.mixin.DraggableElement.prototype.getIndex = function () {
229 return this.index;
230 };
231
232 /**
233 * DraggableGroupElement is a mixin class used to create a group element to
234 * contain draggable elements, which are items that can be clicked and dragged by a mouse.
235 * The class is used with OO.ui.mixin.DraggableElement.
236 *
237 * @abstract
238 * @class
239 * @mixins OO.ui.mixin.GroupElement
240 *
241 * @constructor
242 * @param {Object} [config] Configuration options
243 * @cfg {string} [orientation] Item orientation: 'horizontal' or 'vertical'. The orientation
244 * should match the layout of the items. Items displayed in a single row
245 * or in several rows should use horizontal orientation. The vertical orientation should only be
246 * used when the items are displayed in a single column. Defaults to 'vertical'
247 * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
248 */
249 OO.ui.mixin.DraggableGroupElement = function OoUiMixinDraggableGroupElement( config ) {
250 // Configuration initialization
251 config = config || {};
252
253 // Parent constructor
254 OO.ui.mixin.GroupElement.call( this, config );
255
256 // Properties
257 this.orientation = config.orientation || 'vertical';
258 this.dragItem = null;
259 this.itemKeys = {};
260 this.dir = null;
261 this.itemsOrder = null;
262 this.draggable = config.draggable === undefined ? true : !!config.draggable;
263
264 // Events
265 this.aggregate( {
266 dragstart: 'itemDragStart',
267 dragend: 'itemDragEnd',
268 drop: 'itemDrop'
269 } );
270 this.connect( this, {
271 itemDragStart: 'onItemDragStart',
272 itemDrop: 'onItemDropOrDragEnd',
273 itemDragEnd: 'onItemDropOrDragEnd'
274 } );
275
276 // Initialize
277 if ( Array.isArray( config.items ) ) {
278 this.addItems( config.items );
279 }
280 this.$element
281 .addClass( 'oo-ui-draggableGroupElement' )
282 .append( this.$status )
283 .toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation === 'horizontal' );
284 };
285
286 /* Setup */
287 OO.mixinClass( OO.ui.mixin.DraggableGroupElement, OO.ui.mixin.GroupElement );
288
289 /* Events */
290
291 /**
292 * An item has been dragged to a new position, but not yet dropped.
293 *
294 * @event drag
295 * @param {OO.ui.mixin.DraggableElement} item Dragged item
296 * @param {number} [newIndex] New index for the item
297 */
298
299 /**
300 * An item has been dropped at a new position.
301 *
302 * @event reorder
303 * @param {OO.ui.mixin.DraggableElement} item Reordered item
304 * @param {number} [newIndex] New index for the item
305 */
306
307 /**
308 * Draggable state of this widget has changed.
309 *
310 * @event draggable
311 * @param {boolean} [draggable] Widget is draggable
312 */
313
314 /* Methods */
315
316 /**
317 * Change the draggable state of this widget.
318 * This allows users to temporarily halt the dragging operations.
319 *
320 * @param {boolean} isDraggable Widget supports draggable operations
321 * @fires draggable
322 */
323 OO.ui.mixin.DraggableGroupElement.prototype.toggleDraggable = function ( isDraggable ) {
324 isDraggable = isDraggable !== undefined ? !!isDraggable : !this.draggable;
325
326 if ( this.draggable !== isDraggable ) {
327 this.draggable = isDraggable;
328
329 // Tell the items their draggable state changed
330 this.getItems().forEach( function ( item ) {
331 item.toggleDraggable( this.draggable );
332 }.bind( this ) );
333
334 // Emit event
335 this.emit( 'draggable', this.draggable );
336 }
337 };
338
339 /**
340 * Check the draggable state of this widget
341 *
342 * @return {boolean} Widget supports draggable operations
343 */
344 OO.ui.mixin.DraggableGroupElement.prototype.isDraggable = function () {
345 return this.draggable;
346 };
347
348 /**
349 * Respond to item drag start event
350 *
351 * @private
352 * @param {OO.ui.mixin.DraggableElement} item Dragged item
353 */
354 OO.ui.mixin.DraggableGroupElement.prototype.onItemDragStart = function ( item ) {
355 if ( !this.isDraggable() ) {
356 return;
357 }
358 // Make a shallow copy of this.items so we can re-order it during previews
359 // without affecting the original array.
360 this.itemsOrder = this.items.slice();
361 this.updateIndexes();
362 if ( this.orientation === 'horizontal' ) {
363 // Calculate and cache directionality on drag start - it's a little
364 // expensive and it shouldn't change while dragging.
365 this.dir = this.$element.css( 'direction' );
366 }
367 this.setDragItem( item );
368 };
369
370 /**
371 * Update the index properties of the items
372 */
373 OO.ui.mixin.DraggableGroupElement.prototype.updateIndexes = function () {
374 var i, len;
375
376 // Map the index of each object
377 for ( i = 0, len = this.itemsOrder.length; i < len; i++ ) {
378 this.itemsOrder[ i ].setIndex( i );
379 }
380 };
381
382 /**
383 * Handle drop or dragend event and switch the order of the items accordingly
384 *
385 * @private
386 * @param {OO.ui.mixin.DraggableElement} item Dropped item
387 */
388 OO.ui.mixin.DraggableGroupElement.prototype.onItemDropOrDragEnd = function () {
389 var targetIndex, originalIndex,
390 item = this.getDragItem();
391
392 // TODO: Figure out a way to configure a list of legally droppable
393 // elements even if they are not yet in the list
394 if ( item ) {
395 originalIndex = this.items.indexOf( item );
396 // If the item has moved forward, add one to the index to account for the left shift
397 targetIndex = item.getIndex() + ( item.getIndex() > originalIndex ? 1 : 0 );
398 if ( targetIndex !== originalIndex ) {
399 this.reorder( this.getDragItem(), targetIndex );
400 this.emit( 'reorder', this.getDragItem(), targetIndex );
401 }
402 this.updateIndexes();
403 }
404 this.unsetDragItem();
405 // Return false to prevent propogation
406 return false;
407 };
408
409 /**
410 * Respond to dragover event
411 *
412 * @private
413 * @param {jQuery.Event} e Dragover event
414 * @fires reorder
415 */
416 OO.ui.mixin.DraggableGroupElement.prototype.onDragOver = function ( e ) {
417 var overIndex, targetIndex,
418 item = this.getDragItem(),
419 dragItemIndex = item.getIndex();
420
421 // Get the OptionWidget item we are dragging over
422 overIndex = $( e.target ).closest( '.oo-ui-draggableElement' ).data( 'index' );
423
424 if ( overIndex !== undefined && overIndex !== dragItemIndex ) {
425 targetIndex = overIndex + ( overIndex > dragItemIndex ? 1 : 0 );
426
427 if ( targetIndex > 0 ) {
428 this.$group.children().eq( targetIndex - 1 ).after( item.$element );
429 } else {
430 this.$group.prepend( item.$element );
431 }
432 // Move item in itemsOrder array
433 this.itemsOrder.splice( overIndex, 0,
434 this.itemsOrder.splice( dragItemIndex, 1 )[ 0 ]
435 );
436 this.updateIndexes();
437 this.emit( 'drag', item, targetIndex );
438 }
439 // Prevent default
440 e.preventDefault();
441 };
442
443 /**
444 * Reorder the items in the group
445 *
446 * @param {OO.ui.mixin.DraggableElement} item Reordered item
447 * @param {number} newIndex New index
448 */
449 OO.ui.mixin.DraggableGroupElement.prototype.reorder = function ( item, newIndex ) {
450 this.addItems( [ item ], newIndex );
451 };
452
453 /**
454 * Set a dragged item
455 *
456 * @param {OO.ui.mixin.DraggableElement} item Dragged item
457 */
458 OO.ui.mixin.DraggableGroupElement.prototype.setDragItem = function ( item ) {
459 if ( this.dragItem !== item ) {
460 this.dragItem = item;
461 this.$element.on( 'dragover', this.onDragOver.bind( this ) );
462 this.$element.addClass( 'oo-ui-draggableGroupElement-dragging' );
463 }
464 };
465
466 /**
467 * Unset the current dragged item
468 */
469 OO.ui.mixin.DraggableGroupElement.prototype.unsetDragItem = function () {
470 if ( this.dragItem ) {
471 this.dragItem = null;
472 this.$element.off( 'dragover' );
473 this.$element.removeClass( 'oo-ui-draggableGroupElement-dragging' );
474 }
475 };
476
477 /**
478 * Get the item that is currently being dragged.
479 *
480 * @return {OO.ui.mixin.DraggableElement|null} The currently dragged item, or `null` if no item is being dragged
481 */
482 OO.ui.mixin.DraggableGroupElement.prototype.getDragItem = function () {
483 return this.dragItem;
484 };
485
486 /**
487 * RequestManager is a mixin that manages the lifecycle of a promise-backed request for a widget, such as
488 * the {@link OO.ui.mixin.LookupElement}.
489 *
490 * @class
491 * @abstract
492 *
493 * @constructor
494 */
495 OO.ui.mixin.RequestManager = function OoUiMixinRequestManager() {
496 this.requestCache = {};
497 this.requestQuery = null;
498 this.requestRequest = null;
499 };
500
501 /* Setup */
502
503 OO.initClass( OO.ui.mixin.RequestManager );
504
505 /**
506 * Get request results for the current query.
507 *
508 * @return {jQuery.Promise} Promise object which will be passed response data as the first argument of
509 * the done event. If the request was aborted to make way for a subsequent request, this promise
510 * may not be rejected, depending on what jQuery feels like doing.
511 */
512 OO.ui.mixin.RequestManager.prototype.getRequestData = function () {
513 var widget = this,
514 value = this.getRequestQuery(),
515 deferred = $.Deferred(),
516 ourRequest;
517
518 this.abortRequest();
519 if ( Object.prototype.hasOwnProperty.call( this.requestCache, value ) ) {
520 deferred.resolve( this.requestCache[ value ] );
521 } else {
522 if ( this.pushPending ) {
523 this.pushPending();
524 }
525 this.requestQuery = value;
526 ourRequest = this.requestRequest = this.getRequest();
527 ourRequest
528 .always( function () {
529 // We need to pop pending even if this is an old request, otherwise
530 // the widget will remain pending forever.
531 // TODO: this assumes that an aborted request will fail or succeed soon after
532 // being aborted, or at least eventually. It would be nice if we could popPending()
533 // at abort time, but only if we knew that we hadn't already called popPending()
534 // for that request.
535 if ( widget.popPending ) {
536 widget.popPending();
537 }
538 } )
539 .done( function ( response ) {
540 // If this is an old request (and aborting it somehow caused it to still succeed),
541 // ignore its success completely
542 if ( ourRequest === widget.requestRequest ) {
543 widget.requestQuery = null;
544 widget.requestRequest = null;
545 widget.requestCache[ value ] = widget.getRequestCacheDataFromResponse( response );
546 deferred.resolve( widget.requestCache[ value ] );
547 }
548 } )
549 .fail( function () {
550 // If this is an old request (or a request failing because it's being aborted),
551 // ignore its failure completely
552 if ( ourRequest === widget.requestRequest ) {
553 widget.requestQuery = null;
554 widget.requestRequest = null;
555 deferred.reject();
556 }
557 } );
558 }
559 return deferred.promise();
560 };
561
562 /**
563 * Abort the currently pending request, if any.
564 *
565 * @private
566 */
567 OO.ui.mixin.RequestManager.prototype.abortRequest = function () {
568 var oldRequest = this.requestRequest;
569 if ( oldRequest ) {
570 // First unset this.requestRequest to the fail handler will notice
571 // that the request is no longer current
572 this.requestRequest = null;
573 this.requestQuery = null;
574 oldRequest.abort();
575 }
576 };
577
578 /**
579 * Get the query to be made.
580 *
581 * @protected
582 * @method
583 * @abstract
584 * @return {string} query to be used
585 */
586 OO.ui.mixin.RequestManager.prototype.getRequestQuery = null;
587
588 /**
589 * Get a new request object of the current query value.
590 *
591 * @protected
592 * @method
593 * @abstract
594 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
595 */
596 OO.ui.mixin.RequestManager.prototype.getRequest = null;
597
598 /**
599 * Pre-process data returned by the request from #getRequest.
600 *
601 * The return value of this function will be cached, and any further queries for the given value
602 * will use the cache rather than doing API requests.
603 *
604 * @protected
605 * @method
606 * @abstract
607 * @param {Mixed} response Response from server
608 * @return {Mixed} Cached result data
609 */
610 OO.ui.mixin.RequestManager.prototype.getRequestCacheDataFromResponse = null;
611
612 /**
613 * LookupElement is a mixin that creates a {@link OO.ui.MenuSelectWidget menu} of suggested values for
614 * a {@link OO.ui.TextInputWidget text input widget}. Suggested values are based on the characters the user types
615 * into the text input field and, in general, the menu is only displayed when the user types. If a suggested value is chosen
616 * from the lookup menu, that value becomes the value of the input field.
617 *
618 * Note that a new menu of suggested items is displayed when a value is chosen from the lookup menu. If this is
619 * not the desired behavior, disable lookup menus with the #setLookupsDisabled method, then set the value, then
620 * re-enable lookups.
621 *
622 * See the [OOjs UI demos][1] for an example.
623 *
624 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/index.html#widgets-apex-vector-ltr
625 *
626 * @class
627 * @abstract
628 * @mixins OO.ui.mixin.RequestManager
629 *
630 * @constructor
631 * @param {Object} [config] Configuration options
632 * @cfg {jQuery} [$overlay] Overlay for the lookup menu; defaults to relative positioning.
633 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
634 * @cfg {jQuery} [$container=this.$element] The container element. The lookup menu is rendered beneath the specified element.
635 * @cfg {boolean} [allowSuggestionsWhenEmpty=false] Request and display a lookup menu when the text input is empty.
636 * By default, the lookup menu is not generated and displayed until the user begins to type.
637 * @cfg {boolean} [highlightFirst=true] Whether the first lookup result should be highlighted (so, that the user can
638 * take it over into the input with simply pressing return) automatically or not.
639 */
640 OO.ui.mixin.LookupElement = function OoUiMixinLookupElement( config ) {
641 // Configuration initialization
642 config = $.extend( { highlightFirst: true }, config );
643
644 // Mixin constructors
645 OO.ui.mixin.RequestManager.call( this, config );
646
647 // Properties
648 this.$overlay = config.$overlay || this.$element;
649 this.lookupMenu = new OO.ui.MenuSelectWidget( {
650 widget: this,
651 input: this,
652 $floatableContainer: config.$container || this.$element
653 } );
654
655 this.allowSuggestionsWhenEmpty = config.allowSuggestionsWhenEmpty || false;
656
657 this.lookupsDisabled = false;
658 this.lookupInputFocused = false;
659 this.lookupHighlightFirstItem = config.highlightFirst;
660
661 // Events
662 this.$input.on( {
663 focus: this.onLookupInputFocus.bind( this ),
664 blur: this.onLookupInputBlur.bind( this ),
665 mousedown: this.onLookupInputMouseDown.bind( this )
666 } );
667 this.connect( this, { change: 'onLookupInputChange' } );
668 this.lookupMenu.connect( this, {
669 toggle: 'onLookupMenuToggle',
670 choose: 'onLookupMenuItemChoose'
671 } );
672
673 // Initialization
674 this.$input.attr( {
675 role: 'combobox',
676 'aria-owns': this.lookupMenu.getElementId(),
677 'aria-autocomplete': 'list'
678 } );
679 this.$element.addClass( 'oo-ui-lookupElement' );
680 this.lookupMenu.$element.addClass( 'oo-ui-lookupElement-menu' );
681 this.$overlay.append( this.lookupMenu.$element );
682 };
683
684 /* Setup */
685
686 OO.mixinClass( OO.ui.mixin.LookupElement, OO.ui.mixin.RequestManager );
687
688 /* Methods */
689
690 /**
691 * Handle input focus event.
692 *
693 * @protected
694 * @param {jQuery.Event} e Input focus event
695 */
696 OO.ui.mixin.LookupElement.prototype.onLookupInputFocus = function () {
697 this.lookupInputFocused = true;
698 this.populateLookupMenu();
699 };
700
701 /**
702 * Handle input blur event.
703 *
704 * @protected
705 * @param {jQuery.Event} e Input blur event
706 */
707 OO.ui.mixin.LookupElement.prototype.onLookupInputBlur = function () {
708 this.closeLookupMenu();
709 this.lookupInputFocused = false;
710 };
711
712 /**
713 * Handle input mouse down event.
714 *
715 * @protected
716 * @param {jQuery.Event} e Input mouse down event
717 */
718 OO.ui.mixin.LookupElement.prototype.onLookupInputMouseDown = function () {
719 // Only open the menu if the input was already focused.
720 // This way we allow the user to open the menu again after closing it with Esc
721 // by clicking in the input. Opening (and populating) the menu when initially
722 // clicking into the input is handled by the focus handler.
723 if ( this.lookupInputFocused && !this.lookupMenu.isVisible() ) {
724 this.populateLookupMenu();
725 }
726 };
727
728 /**
729 * Handle input change event.
730 *
731 * @protected
732 * @param {string} value New input value
733 */
734 OO.ui.mixin.LookupElement.prototype.onLookupInputChange = function () {
735 if ( this.lookupInputFocused ) {
736 this.populateLookupMenu();
737 }
738 };
739
740 /**
741 * Handle the lookup menu being shown/hidden.
742 *
743 * @protected
744 * @param {boolean} visible Whether the lookup menu is now visible.
745 */
746 OO.ui.mixin.LookupElement.prototype.onLookupMenuToggle = function ( visible ) {
747 if ( !visible ) {
748 // When the menu is hidden, abort any active request and clear the menu.
749 // This has to be done here in addition to closeLookupMenu(), because
750 // MenuSelectWidget will close itself when the user presses Esc.
751 this.abortLookupRequest();
752 this.lookupMenu.clearItems();
753 }
754 };
755
756 /**
757 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
758 *
759 * @protected
760 * @param {OO.ui.MenuOptionWidget} item Selected item
761 */
762 OO.ui.mixin.LookupElement.prototype.onLookupMenuItemChoose = function ( item ) {
763 this.setValue( item.getData() );
764 };
765
766 /**
767 * Get lookup menu.
768 *
769 * @private
770 * @return {OO.ui.MenuSelectWidget}
771 */
772 OO.ui.mixin.LookupElement.prototype.getLookupMenu = function () {
773 return this.lookupMenu;
774 };
775
776 /**
777 * Disable or re-enable lookups.
778 *
779 * When lookups are disabled, calls to #populateLookupMenu will be ignored.
780 *
781 * @param {boolean} disabled Disable lookups
782 */
783 OO.ui.mixin.LookupElement.prototype.setLookupsDisabled = function ( disabled ) {
784 this.lookupsDisabled = !!disabled;
785 };
786
787 /**
788 * Open the menu. If there are no entries in the menu, this does nothing.
789 *
790 * @private
791 * @chainable
792 */
793 OO.ui.mixin.LookupElement.prototype.openLookupMenu = function () {
794 if ( !this.lookupMenu.isEmpty() ) {
795 this.lookupMenu.toggle( true );
796 }
797 return this;
798 };
799
800 /**
801 * Close the menu, empty it, and abort any pending request.
802 *
803 * @private
804 * @chainable
805 */
806 OO.ui.mixin.LookupElement.prototype.closeLookupMenu = function () {
807 this.lookupMenu.toggle( false );
808 this.abortLookupRequest();
809 this.lookupMenu.clearItems();
810 return this;
811 };
812
813 /**
814 * Request menu items based on the input's current value, and when they arrive,
815 * populate the menu with these items and show the menu.
816 *
817 * If lookups have been disabled with #setLookupsDisabled, this function does nothing.
818 *
819 * @private
820 * @chainable
821 */
822 OO.ui.mixin.LookupElement.prototype.populateLookupMenu = function () {
823 var widget = this,
824 value = this.getValue();
825
826 if ( this.lookupsDisabled || this.isReadOnly() ) {
827 return;
828 }
829
830 // If the input is empty, clear the menu, unless suggestions when empty are allowed.
831 if ( !this.allowSuggestionsWhenEmpty && value === '' ) {
832 this.closeLookupMenu();
833 // Skip population if there is already a request pending for the current value
834 } else if ( value !== this.lookupQuery ) {
835 this.getLookupMenuItems()
836 .done( function ( items ) {
837 widget.lookupMenu.clearItems();
838 if ( items.length ) {
839 widget.lookupMenu
840 .addItems( items )
841 .toggle( true );
842 widget.initializeLookupMenuSelection();
843 } else {
844 widget.lookupMenu.toggle( false );
845 }
846 } )
847 .fail( function () {
848 widget.lookupMenu.clearItems();
849 } );
850 }
851
852 return this;
853 };
854
855 /**
856 * Highlight the first selectable item in the menu, if configured.
857 *
858 * @private
859 * @chainable
860 */
861 OO.ui.mixin.LookupElement.prototype.initializeLookupMenuSelection = function () {
862 if ( this.lookupHighlightFirstItem && !this.lookupMenu.getSelectedItem() ) {
863 this.lookupMenu.highlightItem( this.lookupMenu.getFirstSelectableItem() );
864 }
865 };
866
867 /**
868 * Get lookup menu items for the current query.
869 *
870 * @private
871 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument of
872 * the done event. If the request was aborted to make way for a subsequent request, this promise
873 * will not be rejected: it will remain pending forever.
874 */
875 OO.ui.mixin.LookupElement.prototype.getLookupMenuItems = function () {
876 return this.getRequestData().then( function ( data ) {
877 return this.getLookupMenuOptionsFromData( data );
878 }.bind( this ) );
879 };
880
881 /**
882 * Abort the currently pending lookup request, if any.
883 *
884 * @private
885 */
886 OO.ui.mixin.LookupElement.prototype.abortLookupRequest = function () {
887 this.abortRequest();
888 };
889
890 /**
891 * Get a new request object of the current lookup query value.
892 *
893 * @protected
894 * @method
895 * @abstract
896 * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
897 */
898 OO.ui.mixin.LookupElement.prototype.getLookupRequest = null;
899
900 /**
901 * Pre-process data returned by the request from #getLookupRequest.
902 *
903 * The return value of this function will be cached, and any further queries for the given value
904 * will use the cache rather than doing API requests.
905 *
906 * @protected
907 * @method
908 * @abstract
909 * @param {Mixed} response Response from server
910 * @return {Mixed} Cached result data
911 */
912 OO.ui.mixin.LookupElement.prototype.getLookupCacheDataFromResponse = null;
913
914 /**
915 * Get a list of menu option widgets from the (possibly cached) data returned by
916 * #getLookupCacheDataFromResponse.
917 *
918 * @protected
919 * @method
920 * @abstract
921 * @param {Mixed} data Cached result data, usually an array
922 * @return {OO.ui.MenuOptionWidget[]} Menu items
923 */
924 OO.ui.mixin.LookupElement.prototype.getLookupMenuOptionsFromData = null;
925
926 /**
927 * Set the read-only state of the widget.
928 *
929 * This will also disable/enable the lookups functionality.
930 *
931 * @param {boolean} readOnly Make input read-only
932 * @chainable
933 */
934 OO.ui.mixin.LookupElement.prototype.setReadOnly = function ( readOnly ) {
935 // Parent method
936 // Note: Calling #setReadOnly this way assumes this is mixed into an OO.ui.TextInputWidget
937 OO.ui.TextInputWidget.prototype.setReadOnly.call( this, readOnly );
938
939 // During construction, #setReadOnly is called before the OO.ui.mixin.LookupElement constructor
940 if ( this.isReadOnly() && this.lookupMenu ) {
941 this.closeLookupMenu();
942 }
943
944 return this;
945 };
946
947 /**
948 * @inheritdoc OO.ui.mixin.RequestManager
949 */
950 OO.ui.mixin.LookupElement.prototype.getRequestQuery = function () {
951 return this.getValue();
952 };
953
954 /**
955 * @inheritdoc OO.ui.mixin.RequestManager
956 */
957 OO.ui.mixin.LookupElement.prototype.getRequest = function () {
958 return this.getLookupRequest();
959 };
960
961 /**
962 * @inheritdoc OO.ui.mixin.RequestManager
963 */
964 OO.ui.mixin.LookupElement.prototype.getRequestCacheDataFromResponse = function ( response ) {
965 return this.getLookupCacheDataFromResponse( response );
966 };
967
968 /**
969 * TabPanelLayouts are used within {@link OO.ui.IndexLayout index layouts} to create tab panels that
970 * users can select and display from the index's optional {@link OO.ui.TabSelectWidget tab}
971 * navigation. TabPanels are usually not instantiated directly, rather extended to include the
972 * required content and functionality.
973 *
974 * Each tab panel must have a unique symbolic name, which is passed to the constructor. In addition,
975 * the tab panel's tab item is customized (with a label) using the #setupTabItem method. See
976 * {@link OO.ui.IndexLayout IndexLayout} for an example.
977 *
978 * @class
979 * @extends OO.ui.PanelLayout
980 *
981 * @constructor
982 * @param {string} name Unique symbolic name of tab panel
983 * @param {Object} [config] Configuration options
984 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] Label for tab panel's tab
985 */
986 OO.ui.TabPanelLayout = function OoUiTabPanelLayout( name, config ) {
987 // Allow passing positional parameters inside the config object
988 if ( OO.isPlainObject( name ) && config === undefined ) {
989 config = name;
990 name = config.name;
991 }
992
993 // Configuration initialization
994 config = $.extend( { scrollable: true }, config );
995
996 // Parent constructor
997 OO.ui.TabPanelLayout.parent.call( this, config );
998
999 // Properties
1000 this.name = name;
1001 this.label = config.label;
1002 this.tabItem = null;
1003 this.active = false;
1004
1005 // Initialization
1006 this.$element.addClass( 'oo-ui-tabPanelLayout' );
1007 };
1008
1009 /* Setup */
1010
1011 OO.inheritClass( OO.ui.TabPanelLayout, OO.ui.PanelLayout );
1012
1013 /* Events */
1014
1015 /**
1016 * An 'active' event is emitted when the tab panel becomes active. Tab panels become active when they are
1017 * shown in a index layout that is configured to display only one tab panel at a time.
1018 *
1019 * @event active
1020 * @param {boolean} active Tab panel is active
1021 */
1022
1023 /* Methods */
1024
1025 /**
1026 * Get the symbolic name of the tab panel.
1027 *
1028 * @return {string} Symbolic name of tab panel
1029 */
1030 OO.ui.TabPanelLayout.prototype.getName = function () {
1031 return this.name;
1032 };
1033
1034 /**
1035 * Check if tab panel is active.
1036 *
1037 * Tab panels become active when they are shown in a {@link OO.ui.IndexLayout index layout} that is configured to
1038 * display only one tab panel at a time. Additional CSS is applied to the tab panel's tab item to reflect the
1039 * active state.
1040 *
1041 * @return {boolean} Tab panel is active
1042 */
1043 OO.ui.TabPanelLayout.prototype.isActive = function () {
1044 return this.active;
1045 };
1046
1047 /**
1048 * Get tab item.
1049 *
1050 * The tab item allows users to access the tab panel from the index's tab
1051 * navigation. The tab item itself can be customized (with a label, level, etc.) using the #setupTabItem method.
1052 *
1053 * @return {OO.ui.TabOptionWidget|null} Tab option widget
1054 */
1055 OO.ui.TabPanelLayout.prototype.getTabItem = function () {
1056 return this.tabItem;
1057 };
1058
1059 /**
1060 * Set or unset the tab item.
1061 *
1062 * Specify a {@link OO.ui.TabOptionWidget tab option} to set it,
1063 * or `null` to clear the tab item. To customize the tab item itself (e.g., to set a label or tab
1064 * level), use #setupTabItem instead of this method.
1065 *
1066 * @param {OO.ui.TabOptionWidget|null} tabItem Tab option widget, null to clear
1067 * @chainable
1068 */
1069 OO.ui.TabPanelLayout.prototype.setTabItem = function ( tabItem ) {
1070 this.tabItem = tabItem || null;
1071 if ( tabItem ) {
1072 this.setupTabItem();
1073 }
1074 return this;
1075 };
1076
1077 /**
1078 * Set up the tab item.
1079 *
1080 * Use this method to customize the tab item (e.g., to add a label or tab level). To set or unset
1081 * the tab item itself (with a {@link OO.ui.TabOptionWidget tab option} or `null`), use
1082 * the #setTabItem method instead.
1083 *
1084 * @param {OO.ui.TabOptionWidget} tabItem Tab option widget to set up
1085 * @chainable
1086 */
1087 OO.ui.TabPanelLayout.prototype.setupTabItem = function () {
1088 if ( this.label ) {
1089 this.tabItem.setLabel( this.label );
1090 }
1091 return this;
1092 };
1093
1094 /**
1095 * Set the tab panel to its 'active' state.
1096 *
1097 * Tab panels become active when they are shown in a index layout that is configured to display only
1098 * one tab panel at a time. Additional CSS is applied to the tab item to reflect the tab panel's
1099 * active state. Outside of the index context, setting the active state on a tab panel does nothing.
1100 *
1101 * @param {boolean} active Tab panel is active
1102 * @fires active
1103 */
1104 OO.ui.TabPanelLayout.prototype.setActive = function ( active ) {
1105 active = !!active;
1106
1107 if ( active !== this.active ) {
1108 this.active = active;
1109 this.$element.toggleClass( 'oo-ui-tabPanelLayout-active', this.active );
1110 this.emit( 'active', this.active );
1111 }
1112 };
1113
1114 /**
1115 * PageLayouts are used within {@link OO.ui.BookletLayout booklet layouts} to create pages that users can select and display
1116 * from the booklet's optional {@link OO.ui.OutlineSelectWidget outline} navigation. Pages are usually not instantiated directly,
1117 * rather extended to include the required content and functionality.
1118 *
1119 * Each page must have a unique symbolic name, which is passed to the constructor. In addition, the page's outline
1120 * item is customized (with a label, outline level, etc.) using the #setupOutlineItem method. See
1121 * {@link OO.ui.BookletLayout BookletLayout} for an example.
1122 *
1123 * @class
1124 * @extends OO.ui.PanelLayout
1125 *
1126 * @constructor
1127 * @param {string} name Unique symbolic name of page
1128 * @param {Object} [config] Configuration options
1129 */
1130 OO.ui.PageLayout = function OoUiPageLayout( name, config ) {
1131 // Allow passing positional parameters inside the config object
1132 if ( OO.isPlainObject( name ) && config === undefined ) {
1133 config = name;
1134 name = config.name;
1135 }
1136
1137 // Configuration initialization
1138 config = $.extend( { scrollable: true }, config );
1139
1140 // Parent constructor
1141 OO.ui.PageLayout.parent.call( this, config );
1142
1143 // Properties
1144 this.name = name;
1145 this.outlineItem = null;
1146 this.active = false;
1147
1148 // Initialization
1149 this.$element.addClass( 'oo-ui-pageLayout' );
1150 };
1151
1152 /* Setup */
1153
1154 OO.inheritClass( OO.ui.PageLayout, OO.ui.PanelLayout );
1155
1156 /* Events */
1157
1158 /**
1159 * An 'active' event is emitted when the page becomes active. Pages become active when they are
1160 * shown in a booklet layout that is configured to display only one page at a time.
1161 *
1162 * @event active
1163 * @param {boolean} active Page is active
1164 */
1165
1166 /* Methods */
1167
1168 /**
1169 * Get the symbolic name of the page.
1170 *
1171 * @return {string} Symbolic name of page
1172 */
1173 OO.ui.PageLayout.prototype.getName = function () {
1174 return this.name;
1175 };
1176
1177 /**
1178 * Check if page is active.
1179 *
1180 * Pages become active when they are shown in a {@link OO.ui.BookletLayout booklet layout} that is configured to display
1181 * only one page at a time. Additional CSS is applied to the page's outline item to reflect the active state.
1182 *
1183 * @return {boolean} Page is active
1184 */
1185 OO.ui.PageLayout.prototype.isActive = function () {
1186 return this.active;
1187 };
1188
1189 /**
1190 * Get outline item.
1191 *
1192 * The outline item allows users to access the page from the booklet's outline
1193 * navigation. The outline item itself can be customized (with a label, level, etc.) using the #setupOutlineItem method.
1194 *
1195 * @return {OO.ui.OutlineOptionWidget|null} Outline option widget
1196 */
1197 OO.ui.PageLayout.prototype.getOutlineItem = function () {
1198 return this.outlineItem;
1199 };
1200
1201 /**
1202 * Set or unset the outline item.
1203 *
1204 * Specify an {@link OO.ui.OutlineOptionWidget outline option} to set it,
1205 * or `null` to clear the outline item. To customize the outline item itself (e.g., to set a label or outline
1206 * level), use #setupOutlineItem instead of this method.
1207 *
1208 * @param {OO.ui.OutlineOptionWidget|null} outlineItem Outline option widget, null to clear
1209 * @chainable
1210 */
1211 OO.ui.PageLayout.prototype.setOutlineItem = function ( outlineItem ) {
1212 this.outlineItem = outlineItem || null;
1213 if ( outlineItem ) {
1214 this.setupOutlineItem();
1215 }
1216 return this;
1217 };
1218
1219 /**
1220 * Set up the outline item.
1221 *
1222 * Use this method to customize the outline item (e.g., to add a label or outline level). To set or unset
1223 * the outline item itself (with an {@link OO.ui.OutlineOptionWidget outline option} or `null`), use
1224 * the #setOutlineItem method instead.
1225 *
1226 * @param {OO.ui.OutlineOptionWidget} outlineItem Outline option widget to set up
1227 * @chainable
1228 */
1229 OO.ui.PageLayout.prototype.setupOutlineItem = function () {
1230 return this;
1231 };
1232
1233 /**
1234 * Set the page to its 'active' state.
1235 *
1236 * Pages become active when they are shown in a booklet layout that is configured to display only one page at a time. Additional
1237 * CSS is applied to the outline item to reflect the page's active state. Outside of the booklet
1238 * context, setting the active state on a page does nothing.
1239 *
1240 * @param {boolean} active Page is active
1241 * @fires active
1242 */
1243 OO.ui.PageLayout.prototype.setActive = function ( active ) {
1244 active = !!active;
1245
1246 if ( active !== this.active ) {
1247 this.active = active;
1248 this.$element.toggleClass( 'oo-ui-pageLayout-active', active );
1249 this.emit( 'active', this.active );
1250 }
1251 };
1252
1253 /**
1254 * StackLayouts contain a series of {@link OO.ui.PanelLayout panel layouts}. By default, only one panel is displayed
1255 * at a time, though the stack layout can also be configured to show all contained panels, one after another,
1256 * by setting the #continuous option to 'true'.
1257 *
1258 * @example
1259 * // A stack layout with two panels, configured to be displayed continously
1260 * var myStack = new OO.ui.StackLayout( {
1261 * items: [
1262 * new OO.ui.PanelLayout( {
1263 * $content: $( '<p>Panel One</p>' ),
1264 * padded: true,
1265 * framed: true
1266 * } ),
1267 * new OO.ui.PanelLayout( {
1268 * $content: $( '<p>Panel Two</p>' ),
1269 * padded: true,
1270 * framed: true
1271 * } )
1272 * ],
1273 * continuous: true
1274 * } );
1275 * $( 'body' ).append( myStack.$element );
1276 *
1277 * @class
1278 * @extends OO.ui.PanelLayout
1279 * @mixins OO.ui.mixin.GroupElement
1280 *
1281 * @constructor
1282 * @param {Object} [config] Configuration options
1283 * @cfg {boolean} [continuous=false] Show all panels, one after another. By default, only one panel is displayed at a time.
1284 * @cfg {OO.ui.Layout[]} [items] Panel layouts to add to the stack layout.
1285 */
1286 OO.ui.StackLayout = function OoUiStackLayout( config ) {
1287 // Configuration initialization
1288 config = $.extend( { scrollable: true }, config );
1289
1290 // Parent constructor
1291 OO.ui.StackLayout.parent.call( this, config );
1292
1293 // Mixin constructors
1294 OO.ui.mixin.GroupElement.call( this, $.extend( {}, config, { $group: this.$element } ) );
1295
1296 // Properties
1297 this.currentItem = null;
1298 this.continuous = !!config.continuous;
1299
1300 // Initialization
1301 this.$element.addClass( 'oo-ui-stackLayout' );
1302 if ( this.continuous ) {
1303 this.$element.addClass( 'oo-ui-stackLayout-continuous' );
1304 this.$element.on( 'scroll', OO.ui.debounce( this.onScroll.bind( this ), 250 ) );
1305 }
1306 if ( Array.isArray( config.items ) ) {
1307 this.addItems( config.items );
1308 }
1309 };
1310
1311 /* Setup */
1312
1313 OO.inheritClass( OO.ui.StackLayout, OO.ui.PanelLayout );
1314 OO.mixinClass( OO.ui.StackLayout, OO.ui.mixin.GroupElement );
1315
1316 /* Events */
1317
1318 /**
1319 * A 'set' event is emitted when panels are {@link #addItems added}, {@link #removeItems removed},
1320 * {@link #clearItems cleared} or {@link #setItem displayed}.
1321 *
1322 * @event set
1323 * @param {OO.ui.Layout|null} item Current panel or `null` if no panel is shown
1324 */
1325
1326 /**
1327 * When used in continuous mode, this event is emitted when the user scrolls down
1328 * far enough such that currentItem is no longer visible.
1329 *
1330 * @event visibleItemChange
1331 * @param {OO.ui.PanelLayout} panel The next visible item in the layout
1332 */
1333
1334 /* Methods */
1335
1336 /**
1337 * Handle scroll events from the layout element
1338 *
1339 * @param {jQuery.Event} e
1340 * @fires visibleItemChange
1341 */
1342 OO.ui.StackLayout.prototype.onScroll = function () {
1343 var currentRect,
1344 len = this.items.length,
1345 currentIndex = this.items.indexOf( this.currentItem ),
1346 newIndex = currentIndex,
1347 containerRect = this.$element[ 0 ].getBoundingClientRect();
1348
1349 if ( !containerRect || ( !containerRect.top && !containerRect.bottom ) ) {
1350 // Can't get bounding rect, possibly not attached.
1351 return;
1352 }
1353
1354 function getRect( item ) {
1355 return item.$element[ 0 ].getBoundingClientRect();
1356 }
1357
1358 function isVisible( item ) {
1359 var rect = getRect( item );
1360 return rect.bottom > containerRect.top && rect.top < containerRect.bottom;
1361 }
1362
1363 currentRect = getRect( this.currentItem );
1364
1365 if ( currentRect.bottom < containerRect.top ) {
1366 // Scrolled down past current item
1367 while ( ++newIndex < len ) {
1368 if ( isVisible( this.items[ newIndex ] ) ) {
1369 break;
1370 }
1371 }
1372 } else if ( currentRect.top > containerRect.bottom ) {
1373 // Scrolled up past current item
1374 while ( --newIndex >= 0 ) {
1375 if ( isVisible( this.items[ newIndex ] ) ) {
1376 break;
1377 }
1378 }
1379 }
1380
1381 if ( newIndex !== currentIndex ) {
1382 this.emit( 'visibleItemChange', this.items[ newIndex ] );
1383 }
1384 };
1385
1386 /**
1387 * Get the current panel.
1388 *
1389 * @return {OO.ui.Layout|null}
1390 */
1391 OO.ui.StackLayout.prototype.getCurrentItem = function () {
1392 return this.currentItem;
1393 };
1394
1395 /**
1396 * Unset the current item.
1397 *
1398 * @private
1399 * @param {OO.ui.StackLayout} layout
1400 * @fires set
1401 */
1402 OO.ui.StackLayout.prototype.unsetCurrentItem = function () {
1403 var prevItem = this.currentItem;
1404 if ( prevItem === null ) {
1405 return;
1406 }
1407
1408 this.currentItem = null;
1409 this.emit( 'set', null );
1410 };
1411
1412 /**
1413 * Add panel layouts to the stack layout.
1414 *
1415 * Panels will be added to the end of the stack layout array unless the optional index parameter specifies a different
1416 * insertion point. Adding a panel that is already in the stack will move it to the end of the array or the point specified
1417 * by the index.
1418 *
1419 * @param {OO.ui.Layout[]} items Panels to add
1420 * @param {number} [index] Index of the insertion point
1421 * @chainable
1422 */
1423 OO.ui.StackLayout.prototype.addItems = function ( items, index ) {
1424 // Update the visibility
1425 this.updateHiddenState( items, this.currentItem );
1426
1427 // Mixin method
1428 OO.ui.mixin.GroupElement.prototype.addItems.call( this, items, index );
1429
1430 if ( !this.currentItem && items.length ) {
1431 this.setItem( items[ 0 ] );
1432 }
1433
1434 return this;
1435 };
1436
1437 /**
1438 * Remove the specified panels from the stack layout.
1439 *
1440 * Removed panels are detached from the DOM, not removed, so that they may be reused. To remove all panels,
1441 * you may wish to use the #clearItems method instead.
1442 *
1443 * @param {OO.ui.Layout[]} items Panels to remove
1444 * @chainable
1445 * @fires set
1446 */
1447 OO.ui.StackLayout.prototype.removeItems = function ( items ) {
1448 // Mixin method
1449 OO.ui.mixin.GroupElement.prototype.removeItems.call( this, items );
1450
1451 if ( items.indexOf( this.currentItem ) !== -1 ) {
1452 if ( this.items.length ) {
1453 this.setItem( this.items[ 0 ] );
1454 } else {
1455 this.unsetCurrentItem();
1456 }
1457 }
1458
1459 return this;
1460 };
1461
1462 /**
1463 * Clear all panels from the stack layout.
1464 *
1465 * Cleared panels are detached from the DOM, not removed, so that they may be reused. To remove only
1466 * a subset of panels, use the #removeItems method.
1467 *
1468 * @chainable
1469 * @fires set
1470 */
1471 OO.ui.StackLayout.prototype.clearItems = function () {
1472 this.unsetCurrentItem();
1473 OO.ui.mixin.GroupElement.prototype.clearItems.call( this );
1474
1475 return this;
1476 };
1477
1478 /**
1479 * Show the specified panel.
1480 *
1481 * If another panel is currently displayed, it will be hidden.
1482 *
1483 * @param {OO.ui.Layout} item Panel to show
1484 * @chainable
1485 * @fires set
1486 */
1487 OO.ui.StackLayout.prototype.setItem = function ( item ) {
1488 if ( item !== this.currentItem ) {
1489 this.updateHiddenState( this.items, item );
1490
1491 if ( this.items.indexOf( item ) !== -1 ) {
1492 this.currentItem = item;
1493 this.emit( 'set', item );
1494 } else {
1495 this.unsetCurrentItem();
1496 }
1497 }
1498
1499 return this;
1500 };
1501
1502 /**
1503 * Update the visibility of all items in case of non-continuous view.
1504 *
1505 * Ensure all items are hidden except for the selected one.
1506 * This method does nothing when the stack is continuous.
1507 *
1508 * @private
1509 * @param {OO.ui.Layout[]} items Item list iterate over
1510 * @param {OO.ui.Layout} [selectedItem] Selected item to show
1511 */
1512 OO.ui.StackLayout.prototype.updateHiddenState = function ( items, selectedItem ) {
1513 var i, len;
1514
1515 if ( !this.continuous ) {
1516 for ( i = 0, len = items.length; i < len; i++ ) {
1517 if ( !selectedItem || selectedItem !== items[ i ] ) {
1518 items[ i ].$element.addClass( 'oo-ui-element-hidden' );
1519 items[ i ].$element.attr( 'aria-hidden', 'true' );
1520 }
1521 }
1522 if ( selectedItem ) {
1523 selectedItem.$element.removeClass( 'oo-ui-element-hidden' );
1524 selectedItem.$element.removeAttr( 'aria-hidden' );
1525 }
1526 }
1527 };
1528
1529 /**
1530 * 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)
1531 * and its size is customized with the #menuSize config. The content area will fill all remaining space.
1532 *
1533 * @example
1534 * var menuLayout = new OO.ui.MenuLayout( {
1535 * position: 'top'
1536 * } ),
1537 * menuPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1538 * contentPanel = new OO.ui.PanelLayout( { padded: true, expanded: true, scrollable: true } ),
1539 * select = new OO.ui.SelectWidget( {
1540 * items: [
1541 * new OO.ui.OptionWidget( {
1542 * data: 'before',
1543 * label: 'Before',
1544 * } ),
1545 * new OO.ui.OptionWidget( {
1546 * data: 'after',
1547 * label: 'After',
1548 * } ),
1549 * new OO.ui.OptionWidget( {
1550 * data: 'top',
1551 * label: 'Top',
1552 * } ),
1553 * new OO.ui.OptionWidget( {
1554 * data: 'bottom',
1555 * label: 'Bottom',
1556 * } )
1557 * ]
1558 * } ).on( 'select', function ( item ) {
1559 * menuLayout.setMenuPosition( item.getData() );
1560 * } );
1561 *
1562 * menuLayout.$menu.append(
1563 * menuPanel.$element.append( '<b>Menu panel</b>', select.$element )
1564 * );
1565 * menuLayout.$content.append(
1566 * contentPanel.$element.append( '<b>Content panel</b>', '<p>Note that the menu is positioned relative to the content panel: top, bottom, after, before.</p>')
1567 * );
1568 * $( 'body' ).append( menuLayout.$element );
1569 *
1570 * If menu size needs to be overridden, it can be accomplished using CSS similar to the snippet
1571 * below. MenuLayout's CSS will override the appropriate values with 'auto' or '0' to display the
1572 * menu correctly. If `menuPosition` is known beforehand, CSS rules corresponding to other positions
1573 * may be omitted.
1574 *
1575 * .oo-ui-menuLayout-menu {
1576 * height: 200px;
1577 * width: 200px;
1578 * }
1579 * .oo-ui-menuLayout-content {
1580 * top: 200px;
1581 * left: 200px;
1582 * right: 200px;
1583 * bottom: 200px;
1584 * }
1585 *
1586 * @class
1587 * @extends OO.ui.Layout
1588 *
1589 * @constructor
1590 * @param {Object} [config] Configuration options
1591 * @cfg {boolean} [showMenu=true] Show menu
1592 * @cfg {string} [menuPosition='before'] Position of menu: `top`, `after`, `bottom` or `before`
1593 */
1594 OO.ui.MenuLayout = function OoUiMenuLayout( config ) {
1595 // Configuration initialization
1596 config = $.extend( {
1597 showMenu: true,
1598 menuPosition: 'before'
1599 }, config );
1600
1601 // Parent constructor
1602 OO.ui.MenuLayout.parent.call( this, config );
1603
1604 /**
1605 * Menu DOM node
1606 *
1607 * @property {jQuery}
1608 */
1609 this.$menu = $( '<div>' );
1610 /**
1611 * Content DOM node
1612 *
1613 * @property {jQuery}
1614 */
1615 this.$content = $( '<div>' );
1616
1617 // Initialization
1618 this.$menu
1619 .addClass( 'oo-ui-menuLayout-menu' );
1620 this.$content.addClass( 'oo-ui-menuLayout-content' );
1621 this.$element
1622 .addClass( 'oo-ui-menuLayout' )
1623 .append( this.$content, this.$menu );
1624 this.setMenuPosition( config.menuPosition );
1625 this.toggleMenu( config.showMenu );
1626 };
1627
1628 /* Setup */
1629
1630 OO.inheritClass( OO.ui.MenuLayout, OO.ui.Layout );
1631
1632 /* Methods */
1633
1634 /**
1635 * Toggle menu.
1636 *
1637 * @param {boolean} showMenu Show menu, omit to toggle
1638 * @chainable
1639 */
1640 OO.ui.MenuLayout.prototype.toggleMenu = function ( showMenu ) {
1641 showMenu = showMenu === undefined ? !this.showMenu : !!showMenu;
1642
1643 if ( this.showMenu !== showMenu ) {
1644 this.showMenu = showMenu;
1645 this.$element
1646 .toggleClass( 'oo-ui-menuLayout-showMenu', this.showMenu )
1647 .toggleClass( 'oo-ui-menuLayout-hideMenu', !this.showMenu );
1648 this.$menu.attr( 'aria-hidden', this.showMenu ? 'false' : 'true' );
1649 }
1650
1651 return this;
1652 };
1653
1654 /**
1655 * Check if menu is visible
1656 *
1657 * @return {boolean} Menu is visible
1658 */
1659 OO.ui.MenuLayout.prototype.isMenuVisible = function () {
1660 return this.showMenu;
1661 };
1662
1663 /**
1664 * Set menu position.
1665 *
1666 * @param {string} position Position of menu, either `top`, `after`, `bottom` or `before`
1667 * @throws {Error} If position value is not supported
1668 * @chainable
1669 */
1670 OO.ui.MenuLayout.prototype.setMenuPosition = function ( position ) {
1671 this.$element.removeClass( 'oo-ui-menuLayout-' + this.menuPosition );
1672 this.menuPosition = position;
1673 this.$element.addClass( 'oo-ui-menuLayout-' + position );
1674
1675 return this;
1676 };
1677
1678 /**
1679 * Get menu position.
1680 *
1681 * @return {string} Menu position
1682 */
1683 OO.ui.MenuLayout.prototype.getMenuPosition = function () {
1684 return this.menuPosition;
1685 };
1686
1687 /**
1688 * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
1689 * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
1690 * through the pages and select which one to display. By default, only one page is
1691 * displayed at a time and the outline is hidden. When a user navigates to a new page,
1692 * the booklet layout automatically focuses on the first focusable element, unless the
1693 * default setting is changed. Optionally, booklets can be configured to show
1694 * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
1695 *
1696 * @example
1697 * // Example of a BookletLayout that contains two PageLayouts.
1698 *
1699 * function PageOneLayout( name, config ) {
1700 * PageOneLayout.parent.call( this, name, config );
1701 * this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on the left)</p>' );
1702 * }
1703 * OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
1704 * PageOneLayout.prototype.setupOutlineItem = function () {
1705 * this.outlineItem.setLabel( 'Page One' );
1706 * };
1707 *
1708 * function PageTwoLayout( name, config ) {
1709 * PageTwoLayout.parent.call( this, name, config );
1710 * this.$element.append( '<p>Second page</p>' );
1711 * }
1712 * OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
1713 * PageTwoLayout.prototype.setupOutlineItem = function () {
1714 * this.outlineItem.setLabel( 'Page Two' );
1715 * };
1716 *
1717 * var page1 = new PageOneLayout( 'one' ),
1718 * page2 = new PageTwoLayout( 'two' );
1719 *
1720 * var booklet = new OO.ui.BookletLayout( {
1721 * outlined: true
1722 * } );
1723 *
1724 * booklet.addPages ( [ page1, page2 ] );
1725 * $( 'body' ).append( booklet.$element );
1726 *
1727 * @class
1728 * @extends OO.ui.MenuLayout
1729 *
1730 * @constructor
1731 * @param {Object} [config] Configuration options
1732 * @cfg {boolean} [continuous=false] Show all pages, one after another
1733 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is displayed. Disabled on mobile.
1734 * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the pages of the booklet.
1735 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
1736 */
1737 OO.ui.BookletLayout = function OoUiBookletLayout( config ) {
1738 // Configuration initialization
1739 config = config || {};
1740
1741 // Parent constructor
1742 OO.ui.BookletLayout.parent.call( this, config );
1743
1744 // Properties
1745 this.currentPageName = null;
1746 this.pages = {};
1747 this.ignoreFocus = false;
1748 this.stackLayout = new OO.ui.StackLayout( { continuous: !!config.continuous } );
1749 this.$content.append( this.stackLayout.$element );
1750 this.autoFocus = config.autoFocus === undefined || !!config.autoFocus;
1751 this.outlineVisible = false;
1752 this.outlined = !!config.outlined;
1753 if ( this.outlined ) {
1754 this.editable = !!config.editable;
1755 this.outlineControlsWidget = null;
1756 this.outlineSelectWidget = new OO.ui.OutlineSelectWidget();
1757 this.outlinePanel = new OO.ui.PanelLayout( { scrollable: true } );
1758 this.$menu.append( this.outlinePanel.$element );
1759 this.outlineVisible = true;
1760 if ( this.editable ) {
1761 this.outlineControlsWidget = new OO.ui.OutlineControlsWidget(
1762 this.outlineSelectWidget
1763 );
1764 }
1765 }
1766 this.toggleMenu( this.outlined );
1767
1768 // Events
1769 this.stackLayout.connect( this, { set: 'onStackLayoutSet' } );
1770 if ( this.outlined ) {
1771 this.outlineSelectWidget.connect( this, { select: 'onOutlineSelectWidgetSelect' } );
1772 this.scrolling = false;
1773 this.stackLayout.connect( this, { visibleItemChange: 'onStackLayoutVisibleItemChange' } );
1774 }
1775 if ( this.autoFocus ) {
1776 // Event 'focus' does not bubble, but 'focusin' does
1777 this.stackLayout.$element.on( 'focusin', this.onStackLayoutFocus.bind( this ) );
1778 }
1779
1780 // Initialization
1781 this.$element.addClass( 'oo-ui-bookletLayout' );
1782 this.stackLayout.$element.addClass( 'oo-ui-bookletLayout-stackLayout' );
1783 if ( this.outlined ) {
1784 this.outlinePanel.$element
1785 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
1786 .append( this.outlineSelectWidget.$element );
1787 if ( this.editable ) {
1788 this.outlinePanel.$element
1789 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
1790 .append( this.outlineControlsWidget.$element );
1791 }
1792 }
1793 };
1794
1795 /* Setup */
1796
1797 OO.inheritClass( OO.ui.BookletLayout, OO.ui.MenuLayout );
1798
1799 /* Events */
1800
1801 /**
1802 * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the booklet layout.
1803 * @event set
1804 * @param {OO.ui.PageLayout} page Current page
1805 */
1806
1807 /**
1808 * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
1809 *
1810 * @event add
1811 * @param {OO.ui.PageLayout[]} page Added pages
1812 * @param {number} index Index pages were added at
1813 */
1814
1815 /**
1816 * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
1817 * {@link #removePages removed} from the booklet.
1818 *
1819 * @event remove
1820 * @param {OO.ui.PageLayout[]} pages Removed pages
1821 */
1822
1823 /* Methods */
1824
1825 /**
1826 * Handle stack layout focus.
1827 *
1828 * @private
1829 * @param {jQuery.Event} e Focusin event
1830 */
1831 OO.ui.BookletLayout.prototype.onStackLayoutFocus = function ( e ) {
1832 var name, $target;
1833
1834 // Find the page that an element was focused within
1835 $target = $( e.target ).closest( '.oo-ui-pageLayout' );
1836 for ( name in this.pages ) {
1837 // Check for page match, exclude current page to find only page changes
1838 if ( this.pages[ name ].$element[ 0 ] === $target[ 0 ] && name !== this.currentPageName ) {
1839 this.setPage( name );
1840 break;
1841 }
1842 }
1843 };
1844
1845 /**
1846 * Handle visibleItemChange events from the stackLayout
1847 *
1848 * The next visible page is set as the current page by selecting it
1849 * in the outline
1850 *
1851 * @param {OO.ui.PageLayout} page The next visible page in the layout
1852 */
1853 OO.ui.BookletLayout.prototype.onStackLayoutVisibleItemChange = function ( page ) {
1854 // Set a flag to so that the resulting call to #onStackLayoutSet doesn't
1855 // try and scroll the item into view again.
1856 this.scrolling = true;
1857 this.outlineSelectWidget.selectItemByData( page.getName() );
1858 this.scrolling = false;
1859 };
1860
1861 /**
1862 * Handle stack layout set events.
1863 *
1864 * @private
1865 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
1866 */
1867 OO.ui.BookletLayout.prototype.onStackLayoutSet = function ( page ) {
1868 var layout = this;
1869 if ( !this.scrolling && page ) {
1870 page.scrollElementIntoView().done( function () {
1871 if ( layout.autoFocus && !OO.ui.isMobile() ) {
1872 layout.focus();
1873 }
1874 } );
1875 }
1876 };
1877
1878 /**
1879 * Focus the first input in the current page.
1880 *
1881 * If no page is selected, the first selectable page will be selected.
1882 * If the focus is already in an element on the current page, nothing will happen.
1883 *
1884 * @param {number} [itemIndex] A specific item to focus on
1885 */
1886 OO.ui.BookletLayout.prototype.focus = function ( itemIndex ) {
1887 var page,
1888 items = this.stackLayout.getItems();
1889
1890 if ( itemIndex !== undefined && items[ itemIndex ] ) {
1891 page = items[ itemIndex ];
1892 } else {
1893 page = this.stackLayout.getCurrentItem();
1894 }
1895
1896 if ( !page && this.outlined ) {
1897 this.selectFirstSelectablePage();
1898 page = this.stackLayout.getCurrentItem();
1899 }
1900 if ( !page ) {
1901 return;
1902 }
1903 // Only change the focus if is not already in the current page
1904 if ( !OO.ui.contains( page.$element[ 0 ], this.getElementDocument().activeElement, true ) ) {
1905 page.focus();
1906 }
1907 };
1908
1909 /**
1910 * Find the first focusable input in the booklet layout and focus
1911 * on it.
1912 */
1913 OO.ui.BookletLayout.prototype.focusFirstFocusable = function () {
1914 OO.ui.findFocusable( this.stackLayout.$element ).focus();
1915 };
1916
1917 /**
1918 * Handle outline widget select events.
1919 *
1920 * @private
1921 * @param {OO.ui.OptionWidget|null} item Selected item
1922 */
1923 OO.ui.BookletLayout.prototype.onOutlineSelectWidgetSelect = function ( item ) {
1924 if ( item ) {
1925 this.setPage( item.getData() );
1926 }
1927 };
1928
1929 /**
1930 * Check if booklet has an outline.
1931 *
1932 * @return {boolean} Booklet has an outline
1933 */
1934 OO.ui.BookletLayout.prototype.isOutlined = function () {
1935 return this.outlined;
1936 };
1937
1938 /**
1939 * Check if booklet has editing controls.
1940 *
1941 * @return {boolean} Booklet is editable
1942 */
1943 OO.ui.BookletLayout.prototype.isEditable = function () {
1944 return this.editable;
1945 };
1946
1947 /**
1948 * Check if booklet has a visible outline.
1949 *
1950 * @return {boolean} Outline is visible
1951 */
1952 OO.ui.BookletLayout.prototype.isOutlineVisible = function () {
1953 return this.outlined && this.outlineVisible;
1954 };
1955
1956 /**
1957 * Hide or show the outline.
1958 *
1959 * @param {boolean} [show] Show outline, omit to invert current state
1960 * @chainable
1961 */
1962 OO.ui.BookletLayout.prototype.toggleOutline = function ( show ) {
1963 var booklet = this;
1964
1965 if ( this.outlined ) {
1966 show = show === undefined ? !this.outlineVisible : !!show;
1967 this.outlineVisible = show;
1968 this.toggleMenu( show );
1969 if ( show && this.editable ) {
1970 // HACK: Kill dumb scrollbars when the sidebar stops animating, see T161798. Only necessary when
1971 // outline controls are present, delay matches transition on `.oo-ui-menuLayout-menu`.
1972 setTimeout( function () {
1973 OO.ui.Element.static.reconsiderScrollbars( booklet.outlinePanel.$element[ 0 ] );
1974 }, 200 );
1975 }
1976 }
1977
1978 return this;
1979 };
1980
1981 /**
1982 * Find the page closest to the specified page.
1983 *
1984 * @param {OO.ui.PageLayout} page Page to use as a reference point
1985 * @return {OO.ui.PageLayout|null} Page closest to the specified page
1986 */
1987 OO.ui.BookletLayout.prototype.findClosestPage = function ( page ) {
1988 var next, prev, level,
1989 pages = this.stackLayout.getItems(),
1990 index = pages.indexOf( page );
1991
1992 if ( index !== -1 ) {
1993 next = pages[ index + 1 ];
1994 prev = pages[ index - 1 ];
1995 // Prefer adjacent pages at the same level
1996 if ( this.outlined ) {
1997 level = this.outlineSelectWidget.getItemFromData( page.getName() ).getLevel();
1998 if (
1999 prev &&
2000 level === this.outlineSelectWidget.getItemFromData( prev.getName() ).getLevel()
2001 ) {
2002 return prev;
2003 }
2004 if (
2005 next &&
2006 level === this.outlineSelectWidget.getItemFromData( next.getName() ).getLevel()
2007 ) {
2008 return next;
2009 }
2010 }
2011 }
2012 return prev || next || null;
2013 };
2014
2015 /**
2016 * Get the page closest to the specified page.
2017 *
2018 * @deprecated 0.22.6 Use {@link OO.ui.BookletLayout#findClosestPage} instead.
2019 * @param {OO.ui.PageLayout} page Page to use as a reference point
2020 * @return {OO.ui.PageLayout|null} Page closest to the specified page
2021 */
2022 OO.ui.BookletLayout.prototype.getClosestPage = function ( page ) {
2023 OO.ui.warnDeprecation( 'BookletLayout#getClosestPage: Deprecated function. Use findClosestPage instead. See T76630.' );
2024 return this.findClosestPage( page );
2025 };
2026
2027 /**
2028 * Get the outline widget.
2029 *
2030 * If the booklet is not outlined, the method will return `null`.
2031 *
2032 * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
2033 */
2034 OO.ui.BookletLayout.prototype.getOutline = function () {
2035 return this.outlineSelectWidget;
2036 };
2037
2038 /**
2039 * Get the outline controls widget.
2040 *
2041 * If the outline is not editable, the method will return `null`.
2042 *
2043 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
2044 */
2045 OO.ui.BookletLayout.prototype.getOutlineControls = function () {
2046 return this.outlineControlsWidget;
2047 };
2048
2049 /**
2050 * Get a page by its symbolic name.
2051 *
2052 * @param {string} name Symbolic name of page
2053 * @return {OO.ui.PageLayout|undefined} Page, if found
2054 */
2055 OO.ui.BookletLayout.prototype.getPage = function ( name ) {
2056 return this.pages[ name ];
2057 };
2058
2059 /**
2060 * Get the current page.
2061 *
2062 * @return {OO.ui.PageLayout|undefined} Current page, if found
2063 */
2064 OO.ui.BookletLayout.prototype.getCurrentPage = function () {
2065 var name = this.getCurrentPageName();
2066 return name ? this.getPage( name ) : undefined;
2067 };
2068
2069 /**
2070 * Get the symbolic name of the current page.
2071 *
2072 * @return {string|null} Symbolic name of the current page
2073 */
2074 OO.ui.BookletLayout.prototype.getCurrentPageName = function () {
2075 return this.currentPageName;
2076 };
2077
2078 /**
2079 * Add pages to the booklet layout
2080 *
2081 * When pages are added with the same names as existing pages, the existing pages will be
2082 * automatically removed before the new pages are added.
2083 *
2084 * @param {OO.ui.PageLayout[]} pages Pages to add
2085 * @param {number} index Index of the insertion point
2086 * @fires add
2087 * @chainable
2088 */
2089 OO.ui.BookletLayout.prototype.addPages = function ( pages, index ) {
2090 var i, len, name, page, item, currentIndex,
2091 stackLayoutPages = this.stackLayout.getItems(),
2092 remove = [],
2093 items = [];
2094
2095 // Remove pages with same names
2096 for ( i = 0, len = pages.length; i < len; i++ ) {
2097 page = pages[ i ];
2098 name = page.getName();
2099
2100 if ( Object.prototype.hasOwnProperty.call( this.pages, name ) ) {
2101 // Correct the insertion index
2102 currentIndex = stackLayoutPages.indexOf( this.pages[ name ] );
2103 if ( currentIndex !== -1 && currentIndex + 1 < index ) {
2104 index--;
2105 }
2106 remove.push( this.pages[ name ] );
2107 }
2108 }
2109 if ( remove.length ) {
2110 this.removePages( remove );
2111 }
2112
2113 // Add new pages
2114 for ( i = 0, len = pages.length; i < len; i++ ) {
2115 page = pages[ i ];
2116 name = page.getName();
2117 this.pages[ page.getName() ] = page;
2118 if ( this.outlined ) {
2119 item = new OO.ui.OutlineOptionWidget( { data: name } );
2120 page.setOutlineItem( item );
2121 items.push( item );
2122 }
2123 }
2124
2125 if ( this.outlined && items.length ) {
2126 this.outlineSelectWidget.addItems( items, index );
2127 this.selectFirstSelectablePage();
2128 }
2129 this.stackLayout.addItems( pages, index );
2130 this.emit( 'add', pages, index );
2131
2132 return this;
2133 };
2134
2135 /**
2136 * Remove the specified pages from the booklet layout.
2137 *
2138 * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
2139 *
2140 * @param {OO.ui.PageLayout[]} pages An array of pages to remove
2141 * @fires remove
2142 * @chainable
2143 */
2144 OO.ui.BookletLayout.prototype.removePages = function ( pages ) {
2145 var i, len, name, page,
2146 items = [];
2147
2148 for ( i = 0, len = pages.length; i < len; i++ ) {
2149 page = pages[ i ];
2150 name = page.getName();
2151 delete this.pages[ name ];
2152 if ( this.outlined ) {
2153 items.push( this.outlineSelectWidget.getItemFromData( name ) );
2154 page.setOutlineItem( null );
2155 }
2156 }
2157 if ( this.outlined && items.length ) {
2158 this.outlineSelectWidget.removeItems( items );
2159 this.selectFirstSelectablePage();
2160 }
2161 this.stackLayout.removeItems( pages );
2162 this.emit( 'remove', pages );
2163
2164 return this;
2165 };
2166
2167 /**
2168 * Clear all pages from the booklet layout.
2169 *
2170 * To remove only a subset of pages from the booklet, use the #removePages method.
2171 *
2172 * @fires remove
2173 * @chainable
2174 */
2175 OO.ui.BookletLayout.prototype.clearPages = function () {
2176 var i, len,
2177 pages = this.stackLayout.getItems();
2178
2179 this.pages = {};
2180 this.currentPageName = null;
2181 if ( this.outlined ) {
2182 this.outlineSelectWidget.clearItems();
2183 for ( i = 0, len = pages.length; i < len; i++ ) {
2184 pages[ i ].setOutlineItem( null );
2185 }
2186 }
2187 this.stackLayout.clearItems();
2188
2189 this.emit( 'remove', pages );
2190
2191 return this;
2192 };
2193
2194 /**
2195 * Set the current page by symbolic name.
2196 *
2197 * @fires set
2198 * @param {string} name Symbolic name of page
2199 */
2200 OO.ui.BookletLayout.prototype.setPage = function ( name ) {
2201 var selectedItem,
2202 $focused,
2203 page = this.pages[ name ],
2204 previousPage = this.currentPageName && this.pages[ this.currentPageName ];
2205
2206 if ( name !== this.currentPageName ) {
2207 if ( this.outlined ) {
2208 selectedItem = this.outlineSelectWidget.getSelectedItem();
2209 if ( selectedItem && selectedItem.getData() !== name ) {
2210 this.outlineSelectWidget.selectItemByData( name );
2211 }
2212 }
2213 if ( page ) {
2214 if ( previousPage ) {
2215 previousPage.setActive( false );
2216 // Blur anything focused if the next page doesn't have anything focusable.
2217 // This is not needed if the next page has something focusable (because once it is focused
2218 // this blur happens automatically). If the layout is non-continuous, this check is
2219 // meaningless because the next page is not visible yet and thus can't hold focus.
2220 if (
2221 this.autoFocus &&
2222 !OO.ui.isMobile() &&
2223 this.stackLayout.continuous &&
2224 OO.ui.findFocusable( page.$element ).length !== 0
2225 ) {
2226 $focused = previousPage.$element.find( ':focus' );
2227 if ( $focused.length ) {
2228 $focused[ 0 ].blur();
2229 }
2230 }
2231 }
2232 this.currentPageName = name;
2233 page.setActive( true );
2234 this.stackLayout.setItem( page );
2235 if ( !this.stackLayout.continuous && previousPage ) {
2236 // This should not be necessary, since any inputs on the previous page should have been
2237 // blurred when it was hidden, but browsers are not very consistent about this.
2238 $focused = previousPage.$element.find( ':focus' );
2239 if ( $focused.length ) {
2240 $focused[ 0 ].blur();
2241 }
2242 }
2243 this.emit( 'set', page );
2244 }
2245 }
2246 };
2247
2248 /**
2249 * Select the first selectable page.
2250 *
2251 * @chainable
2252 */
2253 OO.ui.BookletLayout.prototype.selectFirstSelectablePage = function () {
2254 if ( !this.outlineSelectWidget.getSelectedItem() ) {
2255 this.outlineSelectWidget.selectItem( this.outlineSelectWidget.getFirstSelectableItem() );
2256 }
2257
2258 return this;
2259 };
2260
2261 /**
2262 * IndexLayouts contain {@link OO.ui.TabPanelLayout tab panel layouts} as well as
2263 * {@link OO.ui.TabSelectWidget tabs} that allow users to easily navigate through the tab panels and
2264 * select which one to display. By default, only one tab panel is displayed at a time. When a user
2265 * navigates to a new tab panel, the index layout automatically focuses on the first focusable element,
2266 * unless the default setting is changed.
2267 *
2268 * TODO: This class is similar to BookletLayout, we may want to refactor to reduce duplication
2269 *
2270 * @example
2271 * // Example of a IndexLayout that contains two TabPanelLayouts.
2272 *
2273 * function TabPanelOneLayout( name, config ) {
2274 * TabPanelOneLayout.parent.call( this, name, config );
2275 * this.$element.append( '<p>First tab panel</p>' );
2276 * }
2277 * OO.inheritClass( TabPanelOneLayout, OO.ui.TabPanelLayout );
2278 * TabPanelOneLayout.prototype.setupTabItem = function () {
2279 * this.tabItem.setLabel( 'Tab panel one' );
2280 * };
2281 *
2282 * var tabPanel1 = new TabPanelOneLayout( 'one' ),
2283 * tabPanel2 = new OO.ui.TabPanelLayout( 'two', { label: 'Tab panel two' } );
2284 *
2285 * tabPanel2.$element.append( '<p>Second tab panel</p>' );
2286 *
2287 * var index = new OO.ui.IndexLayout();
2288 *
2289 * index.addTabPanels ( [ tabPanel1, tabPanel2 ] );
2290 * $( 'body' ).append( index.$element );
2291 *
2292 * @class
2293 * @extends OO.ui.MenuLayout
2294 *
2295 * @constructor
2296 * @param {Object} [config] Configuration options
2297 * @cfg {boolean} [continuous=false] Show all tab panels, one after another
2298 * @cfg {boolean} [expanded=true] Expand the content panel to fill the entire parent element.
2299 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new tab panel is displayed. Disabled on mobile.
2300 */
2301 OO.ui.IndexLayout = function OoUiIndexLayout( config ) {
2302 // Configuration initialization
2303 config = $.extend( {}, config, { menuPosition: 'top' } );
2304
2305 // Parent constructor
2306 OO.ui.IndexLayout.parent.call( this, config );
2307
2308 // Properties
2309 this.currentTabPanelName = null;
2310 this.tabPanels = {};
2311
2312 this.ignoreFocus = false;
2313 this.stackLayout = new OO.ui.StackLayout( {
2314 continuous: !!config.continuous,
2315 expanded: config.expanded
2316 } );
2317 this.$content.append( this.stackLayout.$element );
2318 this.autoFocus = config.autoFocus === undefined || !!config.autoFocus;
2319
2320 this.tabSelectWidget = new OO.ui.TabSelectWidget();
2321 this.tabPanel = new OO.ui.PanelLayout();
2322 this.$menu.append( this.tabPanel.$element );
2323
2324 this.toggleMenu( true );
2325
2326 // Events
2327 this.stackLayout.connect( this, { set: 'onStackLayoutSet' } );
2328 this.tabSelectWidget.connect( this, { select: 'onTabSelectWidgetSelect' } );
2329 if ( this.autoFocus ) {
2330 // Event 'focus' does not bubble, but 'focusin' does
2331 this.stackLayout.$element.on( 'focusin', this.onStackLayoutFocus.bind( this ) );
2332 }
2333
2334 // Initialization
2335 this.$element.addClass( 'oo-ui-indexLayout' );
2336 this.stackLayout.$element.addClass( 'oo-ui-indexLayout-stackLayout' );
2337 this.tabPanel.$element
2338 .addClass( 'oo-ui-indexLayout-tabPanel' )
2339 .append( this.tabSelectWidget.$element );
2340 };
2341
2342 /* Setup */
2343
2344 OO.inheritClass( OO.ui.IndexLayout, OO.ui.MenuLayout );
2345
2346 /* Events */
2347
2348 /**
2349 * A 'set' event is emitted when a tab panel is {@link #setTabPanel set} to be displayed by the index layout.
2350 * @event set
2351 * @param {OO.ui.TabPanelLayout} tabPanel Current tab panel
2352 */
2353
2354 /**
2355 * An 'add' event is emitted when tab panels are {@link #addTabPanels added} to the index layout.
2356 *
2357 * @event add
2358 * @param {OO.ui.TabPanelLayout[]} tabPanel Added tab panels
2359 * @param {number} index Index tab panels were added at
2360 */
2361
2362 /**
2363 * A 'remove' event is emitted when tab panels are {@link #clearTabPanels cleared} or
2364 * {@link #removeTabPanels removed} from the index.
2365 *
2366 * @event remove
2367 * @param {OO.ui.TabPanelLayout[]} tabPanel Removed tab panels
2368 */
2369
2370 /* Methods */
2371
2372 /**
2373 * Handle stack layout focus.
2374 *
2375 * @private
2376 * @param {jQuery.Event} e Focusing event
2377 */
2378 OO.ui.IndexLayout.prototype.onStackLayoutFocus = function ( e ) {
2379 var name, $target;
2380
2381 // Find the tab panel that an element was focused within
2382 $target = $( e.target ).closest( '.oo-ui-tabPanelLayout' );
2383 for ( name in this.tabPanels ) {
2384 // Check for tab panel match, exclude current tab panel to find only tab panel changes
2385 if ( this.tabPanels[ name ].$element[ 0 ] === $target[ 0 ] && name !== this.currentTabPanelName ) {
2386 this.setTabPanel( name );
2387 break;
2388 }
2389 }
2390 };
2391
2392 /**
2393 * Handle stack layout set events.
2394 *
2395 * @private
2396 * @param {OO.ui.PanelLayout|null} tabPanel The tab panel that is now the current panel
2397 */
2398 OO.ui.IndexLayout.prototype.onStackLayoutSet = function ( tabPanel ) {
2399 var layout = this;
2400 if ( tabPanel ) {
2401 tabPanel.scrollElementIntoView().done( function () {
2402 if ( layout.autoFocus && !OO.ui.isMobile() ) {
2403 layout.focus();
2404 }
2405 } );
2406 }
2407 };
2408
2409 /**
2410 * Focus the first input in the current tab panel.
2411 *
2412 * If no tab panel is selected, the first selectable tab panel will be selected.
2413 * If the focus is already in an element on the current tab panel, nothing will happen.
2414 *
2415 * @param {number} [itemIndex] A specific item to focus on
2416 */
2417 OO.ui.IndexLayout.prototype.focus = function ( itemIndex ) {
2418 var tabPanel,
2419 items = this.stackLayout.getItems();
2420
2421 if ( itemIndex !== undefined && items[ itemIndex ] ) {
2422 tabPanel = items[ itemIndex ];
2423 } else {
2424 tabPanel = this.stackLayout.getCurrentItem();
2425 }
2426
2427 if ( !tabPanel ) {
2428 this.selectFirstSelectableTabPanel();
2429 tabPanel = this.stackLayout.getCurrentItem();
2430 }
2431 if ( !tabPanel ) {
2432 return;
2433 }
2434 // Only change the focus if is not already in the current page
2435 if ( !OO.ui.contains( tabPanel.$element[ 0 ], this.getElementDocument().activeElement, true ) ) {
2436 tabPanel.focus();
2437 }
2438 };
2439
2440 /**
2441 * Find the first focusable input in the index layout and focus
2442 * on it.
2443 */
2444 OO.ui.IndexLayout.prototype.focusFirstFocusable = function () {
2445 OO.ui.findFocusable( this.stackLayout.$element ).focus();
2446 };
2447
2448 /**
2449 * Handle tab widget select events.
2450 *
2451 * @private
2452 * @param {OO.ui.OptionWidget|null} item Selected item
2453 */
2454 OO.ui.IndexLayout.prototype.onTabSelectWidgetSelect = function ( item ) {
2455 if ( item ) {
2456 this.setTabPanel( item.getData() );
2457 }
2458 };
2459
2460 /**
2461 * Get the tab panel closest to the specified tab panel.
2462 *
2463 * @param {OO.ui.TabPanelLayout} tabPanel Tab panel to use as a reference point
2464 * @return {OO.ui.TabPanelLayout|null} Tab panel closest to the specified
2465 */
2466 OO.ui.IndexLayout.prototype.getClosestTabPanel = function ( tabPanel ) {
2467 var next, prev, level,
2468 tabPanels = this.stackLayout.getItems(),
2469 index = tabPanels.indexOf( tabPanel );
2470
2471 if ( index !== -1 ) {
2472 next = tabPanels[ index + 1 ];
2473 prev = tabPanels[ index - 1 ];
2474 // Prefer adjacent tab panels at the same level
2475 level = this.tabSelectWidget.getItemFromData( tabPanel.getName() ).getLevel();
2476 if (
2477 prev &&
2478 level === this.tabSelectWidget.getItemFromData( prev.getName() ).getLevel()
2479 ) {
2480 return prev;
2481 }
2482 if (
2483 next &&
2484 level === this.tabSelectWidget.getItemFromData( next.getName() ).getLevel()
2485 ) {
2486 return next;
2487 }
2488 }
2489 return prev || next || null;
2490 };
2491
2492 /**
2493 * Get the tabs widget.
2494 *
2495 * @return {OO.ui.TabSelectWidget} Tabs widget
2496 */
2497 OO.ui.IndexLayout.prototype.getTabs = function () {
2498 return this.tabSelectWidget;
2499 };
2500
2501 /**
2502 * Get a tab panel by its symbolic name.
2503 *
2504 * @param {string} name Symbolic name of tab panel
2505 * @return {OO.ui.TabPanelLayout|undefined} Tab panel, if found
2506 */
2507 OO.ui.IndexLayout.prototype.getTabPanel = function ( name ) {
2508 return this.tabPanels[ name ];
2509 };
2510
2511 /**
2512 * Get the current tab panel.
2513 *
2514 * @return {OO.ui.TabPanelLayout|undefined} Current tab panel, if found
2515 */
2516 OO.ui.IndexLayout.prototype.getCurrentTabPanel = function () {
2517 var name = this.getCurrentTabPanelName();
2518 return name ? this.getTabPanel( name ) : undefined;
2519 };
2520
2521 /**
2522 * Get the symbolic name of the current tab panel.
2523 *
2524 * @return {string|null} Symbolic name of the current tab panel
2525 */
2526 OO.ui.IndexLayout.prototype.getCurrentTabPanelName = function () {
2527 return this.currentTabPanelName;
2528 };
2529
2530 /**
2531 * Add tab panels to the index layout
2532 *
2533 * When tab panels are added with the same names as existing tab panels, the existing tab panels
2534 * will be automatically removed before the new tab panels are added.
2535 *
2536 * @param {OO.ui.TabPanelLayout[]} tabPanels Tab panels to add
2537 * @param {number} index Index of the insertion point
2538 * @fires add
2539 * @chainable
2540 */
2541 OO.ui.IndexLayout.prototype.addTabPanels = function ( tabPanels, index ) {
2542 var i, len, name, tabPanel, item, currentIndex,
2543 stackLayoutTabPanels = this.stackLayout.getItems(),
2544 remove = [],
2545 items = [];
2546
2547 // Remove tab panels with same names
2548 for ( i = 0, len = tabPanels.length; i < len; i++ ) {
2549 tabPanel = tabPanels[ i ];
2550 name = tabPanel.getName();
2551
2552 if ( Object.prototype.hasOwnProperty.call( this.tabPanels, name ) ) {
2553 // Correct the insertion index
2554 currentIndex = stackLayoutTabPanels.indexOf( this.tabPanels[ name ] );
2555 if ( currentIndex !== -1 && currentIndex + 1 < index ) {
2556 index--;
2557 }
2558 remove.push( this.tabPanels[ name ] );
2559 }
2560 }
2561 if ( remove.length ) {
2562 this.removeTabPanels( remove );
2563 }
2564
2565 // Add new tab panels
2566 for ( i = 0, len = tabPanels.length; i < len; i++ ) {
2567 tabPanel = tabPanels[ i ];
2568 name = tabPanel.getName();
2569 this.tabPanels[ tabPanel.getName() ] = tabPanel;
2570 item = new OO.ui.TabOptionWidget( { data: name } );
2571 tabPanel.setTabItem( item );
2572 items.push( item );
2573 }
2574
2575 if ( items.length ) {
2576 this.tabSelectWidget.addItems( items, index );
2577 this.selectFirstSelectableTabPanel();
2578 }
2579 this.stackLayout.addItems( tabPanels, index );
2580 this.emit( 'add', tabPanels, index );
2581
2582 return this;
2583 };
2584
2585 /**
2586 * Remove the specified tab panels from the index layout.
2587 *
2588 * To remove all tab panels from the index, you may wish to use the #clearTabPanels method instead.
2589 *
2590 * @param {OO.ui.TabPanelLayout[]} tabPanels An array of tab panels to remove
2591 * @fires remove
2592 * @chainable
2593 */
2594 OO.ui.IndexLayout.prototype.removeTabPanels = function ( tabPanels ) {
2595 var i, len, name, tabPanel,
2596 items = [];
2597
2598 for ( i = 0, len = tabPanels.length; i < len; i++ ) {
2599 tabPanel = tabPanels[ i ];
2600 name = tabPanel.getName();
2601 delete this.tabPanels[ name ];
2602 items.push( this.tabSelectWidget.getItemFromData( name ) );
2603 tabPanel.setTabItem( null );
2604 }
2605 if ( items.length ) {
2606 this.tabSelectWidget.removeItems( items );
2607 this.selectFirstSelectableTabPanel();
2608 }
2609 this.stackLayout.removeItems( tabPanels );
2610 this.emit( 'remove', tabPanels );
2611
2612 return this;
2613 };
2614
2615 /**
2616 * Clear all tab panels from the index layout.
2617 *
2618 * To remove only a subset of tab panels from the index, use the #removeTabPanels method.
2619 *
2620 * @fires remove
2621 * @chainable
2622 */
2623 OO.ui.IndexLayout.prototype.clearTabPanels = function () {
2624 var i, len,
2625 tabPanels = this.stackLayout.getItems();
2626
2627 this.tabPanels = {};
2628 this.currentTabPanelName = null;
2629 this.tabSelectWidget.clearItems();
2630 for ( i = 0, len = tabPanels.length; i < len; i++ ) {
2631 tabPanels[ i ].setTabItem( null );
2632 }
2633 this.stackLayout.clearItems();
2634
2635 this.emit( 'remove', tabPanels );
2636
2637 return this;
2638 };
2639
2640 /**
2641 * Set the current tab panel by symbolic name.
2642 *
2643 * @fires set
2644 * @param {string} name Symbolic name of tab panel
2645 */
2646 OO.ui.IndexLayout.prototype.setTabPanel = function ( name ) {
2647 var selectedItem,
2648 $focused,
2649 tabPanel = this.tabPanels[ name ],
2650 previousTabPanel = this.currentTabPanelName && this.tabPanels[ this.currentTabPanelName ];
2651
2652 if ( name !== this.currentTabPanelName ) {
2653 selectedItem = this.tabSelectWidget.getSelectedItem();
2654 if ( selectedItem && selectedItem.getData() !== name ) {
2655 this.tabSelectWidget.selectItemByData( name );
2656 }
2657 if ( tabPanel ) {
2658 if ( previousTabPanel ) {
2659 previousTabPanel.setActive( false );
2660 // Blur anything focused if the next tab panel doesn't have anything focusable.
2661 // This is not needed if the next tab panel has something focusable (because once it is focused
2662 // this blur happens automatically). If the layout is non-continuous, this check is
2663 // meaningless because the next tab panel is not visible yet and thus can't hold focus.
2664 if (
2665 this.autoFocus &&
2666 !OO.ui.isMobile() &&
2667 this.stackLayout.continuous &&
2668 OO.ui.findFocusable( tabPanel.$element ).length !== 0
2669 ) {
2670 $focused = previousTabPanel.$element.find( ':focus' );
2671 if ( $focused.length ) {
2672 $focused[ 0 ].blur();
2673 }
2674 }
2675 }
2676 this.currentTabPanelName = name;
2677 tabPanel.setActive( true );
2678 this.stackLayout.setItem( tabPanel );
2679 if ( !this.stackLayout.continuous && previousTabPanel ) {
2680 // This should not be necessary, since any inputs on the previous tab panel should have been
2681 // blurred when it was hidden, but browsers are not very consistent about this.
2682 $focused = previousTabPanel.$element.find( ':focus' );
2683 if ( $focused.length ) {
2684 $focused[ 0 ].blur();
2685 }
2686 }
2687 this.emit( 'set', tabPanel );
2688 }
2689 }
2690 };
2691
2692 /**
2693 * Select the first selectable tab panel.
2694 *
2695 * @chainable
2696 */
2697 OO.ui.IndexLayout.prototype.selectFirstSelectableTabPanel = function () {
2698 if ( !this.tabSelectWidget.getSelectedItem() ) {
2699 this.tabSelectWidget.selectItem( this.tabSelectWidget.getFirstSelectableItem() );
2700 }
2701
2702 return this;
2703 };
2704
2705 /**
2706 * ToggleWidget implements basic behavior of widgets with an on/off state.
2707 * Please see OO.ui.ToggleButtonWidget and OO.ui.ToggleSwitchWidget for examples.
2708 *
2709 * @abstract
2710 * @class
2711 * @extends OO.ui.Widget
2712 *
2713 * @constructor
2714 * @param {Object} [config] Configuration options
2715 * @cfg {boolean} [value=false] The toggle’s initial on/off state.
2716 * By default, the toggle is in the 'off' state.
2717 */
2718 OO.ui.ToggleWidget = function OoUiToggleWidget( config ) {
2719 // Configuration initialization
2720 config = config || {};
2721
2722 // Parent constructor
2723 OO.ui.ToggleWidget.parent.call( this, config );
2724
2725 // Properties
2726 this.value = null;
2727
2728 // Initialization
2729 this.$element.addClass( 'oo-ui-toggleWidget' );
2730 this.setValue( !!config.value );
2731 };
2732
2733 /* Setup */
2734
2735 OO.inheritClass( OO.ui.ToggleWidget, OO.ui.Widget );
2736
2737 /* Events */
2738
2739 /**
2740 * @event change
2741 *
2742 * A change event is emitted when the on/off state of the toggle changes.
2743 *
2744 * @param {boolean} value Value representing the new state of the toggle
2745 */
2746
2747 /* Methods */
2748
2749 /**
2750 * Get the value representing the toggle’s state.
2751 *
2752 * @return {boolean} The on/off state of the toggle
2753 */
2754 OO.ui.ToggleWidget.prototype.getValue = function () {
2755 return this.value;
2756 };
2757
2758 /**
2759 * Set the state of the toggle: `true` for 'on', `false` for 'off'.
2760 *
2761 * @param {boolean} value The state of the toggle
2762 * @fires change
2763 * @chainable
2764 */
2765 OO.ui.ToggleWidget.prototype.setValue = function ( value ) {
2766 value = !!value;
2767 if ( this.value !== value ) {
2768 this.value = value;
2769 this.emit( 'change', value );
2770 this.$element.toggleClass( 'oo-ui-toggleWidget-on', value );
2771 this.$element.toggleClass( 'oo-ui-toggleWidget-off', !value );
2772 }
2773 return this;
2774 };
2775
2776 /**
2777 * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
2778 * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
2779 * configured with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators},
2780 * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
2781 * and {@link OO.ui.mixin.LabelElement labels}. Please see
2782 * the [OOjs UI documentation][1] on MediaWiki for more information.
2783 *
2784 * @example
2785 * // Toggle buttons in the 'off' and 'on' state.
2786 * var toggleButton1 = new OO.ui.ToggleButtonWidget( {
2787 * label: 'Toggle Button off'
2788 * } );
2789 * var toggleButton2 = new OO.ui.ToggleButtonWidget( {
2790 * label: 'Toggle Button on',
2791 * value: true
2792 * } );
2793 * // Append the buttons to the DOM.
2794 * $( 'body' ).append( toggleButton1.$element, toggleButton2.$element );
2795 *
2796 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Toggle_buttons
2797 *
2798 * @class
2799 * @extends OO.ui.ToggleWidget
2800 * @mixins OO.ui.mixin.ButtonElement
2801 * @mixins OO.ui.mixin.IconElement
2802 * @mixins OO.ui.mixin.IndicatorElement
2803 * @mixins OO.ui.mixin.LabelElement
2804 * @mixins OO.ui.mixin.TitledElement
2805 * @mixins OO.ui.mixin.FlaggedElement
2806 * @mixins OO.ui.mixin.TabIndexedElement
2807 *
2808 * @constructor
2809 * @param {Object} [config] Configuration options
2810 * @cfg {boolean} [value=false] The toggle button’s initial on/off
2811 * state. By default, the button is in the 'off' state.
2812 */
2813 OO.ui.ToggleButtonWidget = function OoUiToggleButtonWidget( config ) {
2814 // Configuration initialization
2815 config = config || {};
2816
2817 // Parent constructor
2818 OO.ui.ToggleButtonWidget.parent.call( this, config );
2819
2820 // Mixin constructors
2821 OO.ui.mixin.ButtonElement.call( this, $.extend( {}, config, { active: this.active } ) );
2822 OO.ui.mixin.IconElement.call( this, config );
2823 OO.ui.mixin.IndicatorElement.call( this, config );
2824 OO.ui.mixin.LabelElement.call( this, config );
2825 OO.ui.mixin.TitledElement.call( this, $.extend( {}, config, { $titled: this.$button } ) );
2826 OO.ui.mixin.FlaggedElement.call( this, config );
2827 OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$button } ) );
2828
2829 // Events
2830 this.connect( this, { click: 'onAction' } );
2831
2832 // Initialization
2833 this.$button.append( this.$icon, this.$label, this.$indicator );
2834 this.$element
2835 .addClass( 'oo-ui-toggleButtonWidget' )
2836 .append( this.$button );
2837 };
2838
2839 /* Setup */
2840
2841 OO.inheritClass( OO.ui.ToggleButtonWidget, OO.ui.ToggleWidget );
2842 OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.ButtonElement );
2843 OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.IconElement );
2844 OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.IndicatorElement );
2845 OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.LabelElement );
2846 OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.TitledElement );
2847 OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.FlaggedElement );
2848 OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.TabIndexedElement );
2849
2850 /* Static Properties */
2851
2852 /**
2853 * @static
2854 * @inheritdoc
2855 */
2856 OO.ui.ToggleButtonWidget.static.tagName = 'span';
2857
2858 /* Methods */
2859
2860 /**
2861 * Handle the button action being triggered.
2862 *
2863 * @private
2864 */
2865 OO.ui.ToggleButtonWidget.prototype.onAction = function () {
2866 this.setValue( !this.value );
2867 };
2868
2869 /**
2870 * @inheritdoc
2871 */
2872 OO.ui.ToggleButtonWidget.prototype.setValue = function ( value ) {
2873 value = !!value;
2874 if ( value !== this.value ) {
2875 // Might be called from parent constructor before ButtonElement constructor
2876 if ( this.$button ) {
2877 this.$button.attr( 'aria-pressed', value.toString() );
2878 }
2879 this.setActive( value );
2880 }
2881
2882 // Parent method
2883 OO.ui.ToggleButtonWidget.parent.prototype.setValue.call( this, value );
2884
2885 return this;
2886 };
2887
2888 /**
2889 * @inheritdoc
2890 */
2891 OO.ui.ToggleButtonWidget.prototype.setButtonElement = function ( $button ) {
2892 if ( this.$button ) {
2893 this.$button.removeAttr( 'aria-pressed' );
2894 }
2895 OO.ui.mixin.ButtonElement.prototype.setButtonElement.call( this, $button );
2896 this.$button.attr( 'aria-pressed', this.value.toString() );
2897 };
2898
2899 /**
2900 * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
2901 * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
2902 * visually by a slider in the leftmost position.
2903 *
2904 * @example
2905 * // Toggle switches in the 'off' and 'on' position.
2906 * var toggleSwitch1 = new OO.ui.ToggleSwitchWidget();
2907 * var toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
2908 * value: true
2909 * } );
2910 *
2911 * // Create a FieldsetLayout to layout and label switches
2912 * var fieldset = new OO.ui.FieldsetLayout( {
2913 * label: 'Toggle switches'
2914 * } );
2915 * fieldset.addItems( [
2916 * new OO.ui.FieldLayout( toggleSwitch1, { label: 'Off', align: 'top' } ),
2917 * new OO.ui.FieldLayout( toggleSwitch2, { label: 'On', align: 'top' } )
2918 * ] );
2919 * $( 'body' ).append( fieldset.$element );
2920 *
2921 * @class
2922 * @extends OO.ui.ToggleWidget
2923 * @mixins OO.ui.mixin.TabIndexedElement
2924 *
2925 * @constructor
2926 * @param {Object} [config] Configuration options
2927 * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
2928 * By default, the toggle switch is in the 'off' position.
2929 */
2930 OO.ui.ToggleSwitchWidget = function OoUiToggleSwitchWidget( config ) {
2931 // Parent constructor
2932 OO.ui.ToggleSwitchWidget.parent.call( this, config );
2933
2934 // Mixin constructors
2935 OO.ui.mixin.TabIndexedElement.call( this, config );
2936
2937 // Properties
2938 this.dragging = false;
2939 this.dragStart = null;
2940 this.sliding = false;
2941 this.$glow = $( '<span>' );
2942 this.$grip = $( '<span>' );
2943
2944 // Events
2945 this.$element.on( {
2946 click: this.onClick.bind( this ),
2947 keypress: this.onKeyPress.bind( this )
2948 } );
2949
2950 // Initialization
2951 this.$glow.addClass( 'oo-ui-toggleSwitchWidget-glow' );
2952 this.$grip.addClass( 'oo-ui-toggleSwitchWidget-grip' );
2953 this.$element
2954 .addClass( 'oo-ui-toggleSwitchWidget' )
2955 .attr( 'role', 'checkbox' )
2956 .append( this.$glow, this.$grip );
2957 };
2958
2959 /* Setup */
2960
2961 OO.inheritClass( OO.ui.ToggleSwitchWidget, OO.ui.ToggleWidget );
2962 OO.mixinClass( OO.ui.ToggleSwitchWidget, OO.ui.mixin.TabIndexedElement );
2963
2964 /* Methods */
2965
2966 /**
2967 * Handle mouse click events.
2968 *
2969 * @private
2970 * @param {jQuery.Event} e Mouse click event
2971 */
2972 OO.ui.ToggleSwitchWidget.prototype.onClick = function ( e ) {
2973 if ( !this.isDisabled() && e.which === OO.ui.MouseButtons.LEFT ) {
2974 this.setValue( !this.value );
2975 }
2976 return false;
2977 };
2978
2979 /**
2980 * Handle key press events.
2981 *
2982 * @private
2983 * @param {jQuery.Event} e Key press event
2984 */
2985 OO.ui.ToggleSwitchWidget.prototype.onKeyPress = function ( e ) {
2986 if ( !this.isDisabled() && ( e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER ) ) {
2987 this.setValue( !this.value );
2988 return false;
2989 }
2990 };
2991
2992 /**
2993 * @inheritdoc
2994 */
2995 OO.ui.ToggleSwitchWidget.prototype.setValue = function ( value ) {
2996 OO.ui.ToggleSwitchWidget.parent.prototype.setValue.call( this, value );
2997 this.$element.attr( 'aria-checked', this.value.toString() );
2998 return this;
2999 };
3000
3001 /**
3002 * @inheritdoc
3003 */
3004 OO.ui.ToggleSwitchWidget.prototype.simulateLabelClick = function () {
3005 if ( !this.isDisabled() ) {
3006 this.setValue( !this.value );
3007 }
3008 this.focus();
3009 };
3010
3011 /**
3012 * OutlineControlsWidget is a set of controls for an {@link OO.ui.OutlineSelectWidget outline select widget}.
3013 * Controls include moving items up and down, removing items, and adding different kinds of items.
3014 *
3015 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3016 *
3017 * @class
3018 * @extends OO.ui.Widget
3019 * @mixins OO.ui.mixin.GroupElement
3020 * @mixins OO.ui.mixin.IconElement
3021 *
3022 * @constructor
3023 * @param {OO.ui.OutlineSelectWidget} outline Outline to control
3024 * @param {Object} [config] Configuration options
3025 * @cfg {Object} [abilities] List of abilties
3026 * @cfg {boolean} [abilities.move=true] Allow moving movable items
3027 * @cfg {boolean} [abilities.remove=true] Allow removing removable items
3028 */
3029 OO.ui.OutlineControlsWidget = function OoUiOutlineControlsWidget( outline, config ) {
3030 // Allow passing positional parameters inside the config object
3031 if ( OO.isPlainObject( outline ) && config === undefined ) {
3032 config = outline;
3033 outline = config.outline;
3034 }
3035
3036 // Configuration initialization
3037 config = $.extend( { icon: 'add' }, config );
3038
3039 // Parent constructor
3040 OO.ui.OutlineControlsWidget.parent.call( this, config );
3041
3042 // Mixin constructors
3043 OO.ui.mixin.GroupElement.call( this, config );
3044 OO.ui.mixin.IconElement.call( this, config );
3045
3046 // Properties
3047 this.outline = outline;
3048 this.$movers = $( '<div>' );
3049 this.upButton = new OO.ui.ButtonWidget( {
3050 framed: false,
3051 icon: 'collapse',
3052 title: OO.ui.msg( 'ooui-outline-control-move-up' )
3053 } );
3054 this.downButton = new OO.ui.ButtonWidget( {
3055 framed: false,
3056 icon: 'expand',
3057 title: OO.ui.msg( 'ooui-outline-control-move-down' )
3058 } );
3059 this.removeButton = new OO.ui.ButtonWidget( {
3060 framed: false,
3061 icon: 'trash',
3062 title: OO.ui.msg( 'ooui-outline-control-remove' )
3063 } );
3064 this.abilities = { move: true, remove: true };
3065
3066 // Events
3067 outline.connect( this, {
3068 select: 'onOutlineChange',
3069 add: 'onOutlineChange',
3070 remove: 'onOutlineChange'
3071 } );
3072 this.upButton.connect( this, { click: [ 'emit', 'move', -1 ] } );
3073 this.downButton.connect( this, { click: [ 'emit', 'move', 1 ] } );
3074 this.removeButton.connect( this, { click: [ 'emit', 'remove' ] } );
3075
3076 // Initialization
3077 this.$element.addClass( 'oo-ui-outlineControlsWidget' );
3078 this.$group.addClass( 'oo-ui-outlineControlsWidget-items' );
3079 this.$movers
3080 .addClass( 'oo-ui-outlineControlsWidget-movers' )
3081 .append( this.removeButton.$element, this.upButton.$element, this.downButton.$element );
3082 this.$element.append( this.$icon, this.$group, this.$movers );
3083 this.setAbilities( config.abilities || {} );
3084 };
3085
3086 /* Setup */
3087
3088 OO.inheritClass( OO.ui.OutlineControlsWidget, OO.ui.Widget );
3089 OO.mixinClass( OO.ui.OutlineControlsWidget, OO.ui.mixin.GroupElement );
3090 OO.mixinClass( OO.ui.OutlineControlsWidget, OO.ui.mixin.IconElement );
3091
3092 /* Events */
3093
3094 /**
3095 * @event move
3096 * @param {number} places Number of places to move
3097 */
3098
3099 /**
3100 * @event remove
3101 */
3102
3103 /* Methods */
3104
3105 /**
3106 * Set abilities.
3107 *
3108 * @param {Object} abilities List of abilties
3109 * @param {boolean} [abilities.move] Allow moving movable items
3110 * @param {boolean} [abilities.remove] Allow removing removable items
3111 */
3112 OO.ui.OutlineControlsWidget.prototype.setAbilities = function ( abilities ) {
3113 var ability;
3114
3115 for ( ability in this.abilities ) {
3116 if ( abilities[ ability ] !== undefined ) {
3117 this.abilities[ ability ] = !!abilities[ ability ];
3118 }
3119 }
3120
3121 this.onOutlineChange();
3122 };
3123
3124 /**
3125 * Handle outline change events.
3126 *
3127 * @private
3128 */
3129 OO.ui.OutlineControlsWidget.prototype.onOutlineChange = function () {
3130 var i, len, firstMovable, lastMovable,
3131 items = this.outline.getItems(),
3132 selectedItem = this.outline.getSelectedItem(),
3133 movable = this.abilities.move && selectedItem && selectedItem.isMovable(),
3134 removable = this.abilities.remove && selectedItem && selectedItem.isRemovable();
3135
3136 if ( movable ) {
3137 i = -1;
3138 len = items.length;
3139 while ( ++i < len ) {
3140 if ( items[ i ].isMovable() ) {
3141 firstMovable = items[ i ];
3142 break;
3143 }
3144 }
3145 i = len;
3146 while ( i-- ) {
3147 if ( items[ i ].isMovable() ) {
3148 lastMovable = items[ i ];
3149 break;
3150 }
3151 }
3152 }
3153 this.upButton.setDisabled( !movable || selectedItem === firstMovable );
3154 this.downButton.setDisabled( !movable || selectedItem === lastMovable );
3155 this.removeButton.setDisabled( !removable );
3156 };
3157
3158 /**
3159 * OutlineOptionWidget is an item in an {@link OO.ui.OutlineSelectWidget OutlineSelectWidget}.
3160 *
3161 * Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}, which contain
3162 * {@link OO.ui.PageLayout page layouts}. See {@link OO.ui.BookletLayout BookletLayout}
3163 * for an example.
3164 *
3165 * @class
3166 * @extends OO.ui.DecoratedOptionWidget
3167 *
3168 * @constructor
3169 * @param {Object} [config] Configuration options
3170 * @cfg {number} [level] Indentation level
3171 * @cfg {boolean} [movable] Allow modification from {@link OO.ui.OutlineControlsWidget outline controls}.
3172 */
3173 OO.ui.OutlineOptionWidget = function OoUiOutlineOptionWidget( config ) {
3174 // Configuration initialization
3175 config = config || {};
3176
3177 // Parent constructor
3178 OO.ui.OutlineOptionWidget.parent.call( this, config );
3179
3180 // Properties
3181 this.level = 0;
3182 this.movable = !!config.movable;
3183 this.removable = !!config.removable;
3184
3185 // Initialization
3186 this.$element.addClass( 'oo-ui-outlineOptionWidget' );
3187 this.setLevel( config.level );
3188 };
3189
3190 /* Setup */
3191
3192 OO.inheritClass( OO.ui.OutlineOptionWidget, OO.ui.DecoratedOptionWidget );
3193
3194 /* Static Properties */
3195
3196 /**
3197 * @static
3198 * @inheritdoc
3199 */
3200 OO.ui.OutlineOptionWidget.static.highlightable = true;
3201
3202 /**
3203 * @static
3204 * @inheritdoc
3205 */
3206 OO.ui.OutlineOptionWidget.static.scrollIntoViewOnSelect = true;
3207
3208 /**
3209 * @static
3210 * @inheritable
3211 * @property {string}
3212 */
3213 OO.ui.OutlineOptionWidget.static.levelClass = 'oo-ui-outlineOptionWidget-level-';
3214
3215 /**
3216 * @static
3217 * @inheritable
3218 * @property {number}
3219 */
3220 OO.ui.OutlineOptionWidget.static.levels = 3;
3221
3222 /* Methods */
3223
3224 /**
3225 * Check if item is movable.
3226 *
3227 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3228 *
3229 * @return {boolean} Item is movable
3230 */
3231 OO.ui.OutlineOptionWidget.prototype.isMovable = function () {
3232 return this.movable;
3233 };
3234
3235 /**
3236 * Check if item is removable.
3237 *
3238 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3239 *
3240 * @return {boolean} Item is removable
3241 */
3242 OO.ui.OutlineOptionWidget.prototype.isRemovable = function () {
3243 return this.removable;
3244 };
3245
3246 /**
3247 * Get indentation level.
3248 *
3249 * @return {number} Indentation level
3250 */
3251 OO.ui.OutlineOptionWidget.prototype.getLevel = function () {
3252 return this.level;
3253 };
3254
3255 /**
3256 * @inheritdoc
3257 */
3258 OO.ui.OutlineOptionWidget.prototype.setPressed = function ( state ) {
3259 OO.ui.OutlineOptionWidget.parent.prototype.setPressed.call( this, state );
3260 if ( this.pressed ) {
3261 this.setFlags( { progressive: true } );
3262 } else if ( !this.selected ) {
3263 this.setFlags( { progressive: false } );
3264 }
3265 return this;
3266 };
3267
3268 /**
3269 * Set movability.
3270 *
3271 * Movability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3272 *
3273 * @param {boolean} movable Item is movable
3274 * @chainable
3275 */
3276 OO.ui.OutlineOptionWidget.prototype.setMovable = function ( movable ) {
3277 this.movable = !!movable;
3278 this.updateThemeClasses();
3279 return this;
3280 };
3281
3282 /**
3283 * Set removability.
3284 *
3285 * Removability is used by {@link OO.ui.OutlineControlsWidget outline controls}.
3286 *
3287 * @param {boolean} removable Item is removable
3288 * @chainable
3289 */
3290 OO.ui.OutlineOptionWidget.prototype.setRemovable = function ( removable ) {
3291 this.removable = !!removable;
3292 this.updateThemeClasses();
3293 return this;
3294 };
3295
3296 /**
3297 * @inheritdoc
3298 */
3299 OO.ui.OutlineOptionWidget.prototype.setSelected = function ( state ) {
3300 OO.ui.OutlineOptionWidget.parent.prototype.setSelected.call( this, state );
3301 if ( this.selected ) {
3302 this.setFlags( { progressive: true } );
3303 } else {
3304 this.setFlags( { progressive: false } );
3305 }
3306 return this;
3307 };
3308
3309 /**
3310 * Set indentation level.
3311 *
3312 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
3313 * @chainable
3314 */
3315 OO.ui.OutlineOptionWidget.prototype.setLevel = function ( level ) {
3316 var levels = this.constructor.static.levels,
3317 levelClass = this.constructor.static.levelClass,
3318 i = levels;
3319
3320 this.level = level ? Math.max( 0, Math.min( levels - 1, level ) ) : 0;
3321 while ( i-- ) {
3322 if ( this.level === i ) {
3323 this.$element.addClass( levelClass + i );
3324 } else {
3325 this.$element.removeClass( levelClass + i );
3326 }
3327 }
3328 this.updateThemeClasses();
3329
3330 return this;
3331 };
3332
3333 /**
3334 * OutlineSelectWidget is a structured list that contains {@link OO.ui.OutlineOptionWidget outline options}
3335 * A set of controls can be provided with an {@link OO.ui.OutlineControlsWidget outline controls} widget.
3336 *
3337 * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
3338 *
3339 * @class
3340 * @extends OO.ui.SelectWidget
3341 * @mixins OO.ui.mixin.TabIndexedElement
3342 *
3343 * @constructor
3344 * @param {Object} [config] Configuration options
3345 */
3346 OO.ui.OutlineSelectWidget = function OoUiOutlineSelectWidget( config ) {
3347 // Parent constructor
3348 OO.ui.OutlineSelectWidget.parent.call( this, config );
3349
3350 // Mixin constructors
3351 OO.ui.mixin.TabIndexedElement.call( this, config );
3352
3353 // Events
3354 this.$element.on( {
3355 focus: this.bindKeyDownListener.bind( this ),
3356 blur: this.unbindKeyDownListener.bind( this )
3357 } );
3358
3359 // Initialization
3360 this.$element.addClass( 'oo-ui-outlineSelectWidget' );
3361 };
3362
3363 /* Setup */
3364
3365 OO.inheritClass( OO.ui.OutlineSelectWidget, OO.ui.SelectWidget );
3366 OO.mixinClass( OO.ui.OutlineSelectWidget, OO.ui.mixin.TabIndexedElement );
3367
3368 /**
3369 * ButtonOptionWidget is a special type of {@link OO.ui.mixin.ButtonElement button element} that
3370 * can be selected and configured with data. The class is
3371 * used with OO.ui.ButtonSelectWidget to create a selection of button options. Please see the
3372 * [OOjs UI documentation on MediaWiki] [1] for more information.
3373 *
3374 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Button_selects_and_options
3375 *
3376 * @class
3377 * @extends OO.ui.OptionWidget
3378 * @mixins OO.ui.mixin.ButtonElement
3379 * @mixins OO.ui.mixin.IconElement
3380 * @mixins OO.ui.mixin.IndicatorElement
3381 * @mixins OO.ui.mixin.TitledElement
3382 *
3383 * @constructor
3384 * @param {Object} [config] Configuration options
3385 */
3386 OO.ui.ButtonOptionWidget = function OoUiButtonOptionWidget( config ) {
3387 // Configuration initialization
3388 config = config || {};
3389
3390 // Parent constructor
3391 OO.ui.ButtonOptionWidget.parent.call( this, config );
3392
3393 // Mixin constructors
3394 OO.ui.mixin.ButtonElement.call( this, config );
3395 OO.ui.mixin.IconElement.call( this, config );
3396 OO.ui.mixin.IndicatorElement.call( this, config );
3397 OO.ui.mixin.TitledElement.call( this, $.extend( {}, config, { $titled: this.$button } ) );
3398
3399 // Initialization
3400 this.$element.addClass( 'oo-ui-buttonOptionWidget' );
3401 this.$button.append( this.$icon, this.$label, this.$indicator );
3402 this.$element.append( this.$button );
3403 };
3404
3405 /* Setup */
3406
3407 OO.inheritClass( OO.ui.ButtonOptionWidget, OO.ui.OptionWidget );
3408 OO.mixinClass( OO.ui.ButtonOptionWidget, OO.ui.mixin.ButtonElement );
3409 OO.mixinClass( OO.ui.ButtonOptionWidget, OO.ui.mixin.IconElement );
3410 OO.mixinClass( OO.ui.ButtonOptionWidget, OO.ui.mixin.IndicatorElement );
3411 OO.mixinClass( OO.ui.ButtonOptionWidget, OO.ui.mixin.TitledElement );
3412
3413 /* Static Properties */
3414
3415 /**
3416 * Allow button mouse down events to pass through so they can be handled by the parent select widget
3417 *
3418 * @static
3419 * @inheritdoc
3420 */
3421 OO.ui.ButtonOptionWidget.static.cancelButtonMouseDownEvents = false;
3422
3423 /**
3424 * @static
3425 * @inheritdoc
3426 */
3427 OO.ui.ButtonOptionWidget.static.highlightable = false;
3428
3429 /* Methods */
3430
3431 /**
3432 * @inheritdoc
3433 */
3434 OO.ui.ButtonOptionWidget.prototype.setSelected = function ( state ) {
3435 OO.ui.ButtonOptionWidget.parent.prototype.setSelected.call( this, state );
3436
3437 if ( this.constructor.static.selectable ) {
3438 this.setActive( state );
3439 }
3440
3441 return this;
3442 };
3443
3444 /**
3445 * ButtonSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains
3446 * button options and is used together with
3447 * OO.ui.ButtonOptionWidget. The ButtonSelectWidget provides an interface for
3448 * highlighting, choosing, and selecting mutually exclusive options. Please see
3449 * the [OOjs UI documentation on MediaWiki] [1] for more information.
3450 *
3451 * @example
3452 * // Example: A ButtonSelectWidget that contains three ButtonOptionWidgets
3453 * var option1 = new OO.ui.ButtonOptionWidget( {
3454 * data: 1,
3455 * label: 'Option 1',
3456 * title: 'Button option 1'
3457 * } );
3458 *
3459 * var option2 = new OO.ui.ButtonOptionWidget( {
3460 * data: 2,
3461 * label: 'Option 2',
3462 * title: 'Button option 2'
3463 * } );
3464 *
3465 * var option3 = new OO.ui.ButtonOptionWidget( {
3466 * data: 3,
3467 * label: 'Option 3',
3468 * title: 'Button option 3'
3469 * } );
3470 *
3471 * var buttonSelect=new OO.ui.ButtonSelectWidget( {
3472 * items: [ option1, option2, option3 ]
3473 * } );
3474 * $( 'body' ).append( buttonSelect.$element );
3475 *
3476 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
3477 *
3478 * @class
3479 * @extends OO.ui.SelectWidget
3480 * @mixins OO.ui.mixin.TabIndexedElement
3481 *
3482 * @constructor
3483 * @param {Object} [config] Configuration options
3484 */
3485 OO.ui.ButtonSelectWidget = function OoUiButtonSelectWidget( config ) {
3486 // Parent constructor
3487 OO.ui.ButtonSelectWidget.parent.call( this, config );
3488
3489 // Mixin constructors
3490 OO.ui.mixin.TabIndexedElement.call( this, config );
3491
3492 // Events
3493 this.$element.on( {
3494 focus: this.bindKeyDownListener.bind( this ),
3495 blur: this.unbindKeyDownListener.bind( this )
3496 } );
3497
3498 // Initialization
3499 this.$element.addClass( 'oo-ui-buttonSelectWidget' );
3500 };
3501
3502 /* Setup */
3503
3504 OO.inheritClass( OO.ui.ButtonSelectWidget, OO.ui.SelectWidget );
3505 OO.mixinClass( OO.ui.ButtonSelectWidget, OO.ui.mixin.TabIndexedElement );
3506
3507 /**
3508 * TabOptionWidget is an item in a {@link OO.ui.TabSelectWidget TabSelectWidget}.
3509 *
3510 * Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}, which contain
3511 * {@link OO.ui.TabPanelLayout tab panel layouts}. See {@link OO.ui.IndexLayout IndexLayout}
3512 * for an example.
3513 *
3514 * @class
3515 * @extends OO.ui.OptionWidget
3516 *
3517 * @constructor
3518 * @param {Object} [config] Configuration options
3519 */
3520 OO.ui.TabOptionWidget = function OoUiTabOptionWidget( config ) {
3521 // Configuration initialization
3522 config = config || {};
3523
3524 // Parent constructor
3525 OO.ui.TabOptionWidget.parent.call( this, config );
3526
3527 // Initialization
3528 this.$element.addClass( 'oo-ui-tabOptionWidget' );
3529 };
3530
3531 /* Setup */
3532
3533 OO.inheritClass( OO.ui.TabOptionWidget, OO.ui.OptionWidget );
3534
3535 /* Static Properties */
3536
3537 /**
3538 * @static
3539 * @inheritdoc
3540 */
3541 OO.ui.TabOptionWidget.static.highlightable = false;
3542
3543 /**
3544 * TabSelectWidget is a list that contains {@link OO.ui.TabOptionWidget tab options}
3545 *
3546 * **Currently, this class is only used by {@link OO.ui.IndexLayout index layouts}.**
3547 *
3548 * @class
3549 * @extends OO.ui.SelectWidget
3550 * @mixins OO.ui.mixin.TabIndexedElement
3551 *
3552 * @constructor
3553 * @param {Object} [config] Configuration options
3554 */
3555 OO.ui.TabSelectWidget = function OoUiTabSelectWidget( config ) {
3556 // Parent constructor
3557 OO.ui.TabSelectWidget.parent.call( this, config );
3558
3559 // Mixin constructors
3560 OO.ui.mixin.TabIndexedElement.call( this, config );
3561
3562 // Events
3563 this.$element.on( {
3564 focus: this.bindKeyDownListener.bind( this ),
3565 blur: this.unbindKeyDownListener.bind( this )
3566 } );
3567
3568 // Initialization
3569 this.$element.addClass( 'oo-ui-tabSelectWidget' );
3570 };
3571
3572 /* Setup */
3573
3574 OO.inheritClass( OO.ui.TabSelectWidget, OO.ui.SelectWidget );
3575 OO.mixinClass( OO.ui.TabSelectWidget, OO.ui.mixin.TabIndexedElement );
3576
3577 /**
3578 * CapsuleItemWidgets are used within a {@link OO.ui.CapsuleMultiselectWidget
3579 * CapsuleMultiselectWidget} to display the selected items.
3580 *
3581 * @class
3582 * @extends OO.ui.Widget
3583 * @mixins OO.ui.mixin.ItemWidget
3584 * @mixins OO.ui.mixin.LabelElement
3585 * @mixins OO.ui.mixin.FlaggedElement
3586 * @mixins OO.ui.mixin.TabIndexedElement
3587 *
3588 * @constructor
3589 * @param {Object} [config] Configuration options
3590 */
3591 OO.ui.CapsuleItemWidget = function OoUiCapsuleItemWidget( config ) {
3592 // Configuration initialization
3593 config = config || {};
3594
3595 // Parent constructor
3596 OO.ui.CapsuleItemWidget.parent.call( this, config );
3597
3598 // Mixin constructors
3599 OO.ui.mixin.ItemWidget.call( this );
3600 OO.ui.mixin.LabelElement.call( this, config );
3601 OO.ui.mixin.FlaggedElement.call( this, config );
3602 OO.ui.mixin.TabIndexedElement.call( this, config );
3603
3604 // Events
3605 this.closeButton = new OO.ui.ButtonWidget( {
3606 framed: false,
3607 icon: 'close',
3608 tabIndex: -1,
3609 title: OO.ui.msg( 'ooui-item-remove' )
3610 } ).on( 'click', this.onCloseClick.bind( this ) );
3611
3612 this.on( 'disable', function ( disabled ) {
3613 this.closeButton.setDisabled( disabled );
3614 }.bind( this ) );
3615
3616 // Initialization
3617 this.$element
3618 .on( {
3619 click: this.onClick.bind( this ),
3620 keydown: this.onKeyDown.bind( this )
3621 } )
3622 .addClass( 'oo-ui-capsuleItemWidget' )
3623 .append( this.$label, this.closeButton.$element );
3624 };
3625
3626 /* Setup */
3627
3628 OO.inheritClass( OO.ui.CapsuleItemWidget, OO.ui.Widget );
3629 OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.ItemWidget );
3630 OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.LabelElement );
3631 OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.FlaggedElement );
3632 OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.TabIndexedElement );
3633
3634 /* Methods */
3635
3636 /**
3637 * Handle close icon clicks
3638 */
3639 OO.ui.CapsuleItemWidget.prototype.onCloseClick = function () {
3640 var element = this.getElementGroup();
3641
3642 if ( element && $.isFunction( element.removeItems ) ) {
3643 element.removeItems( [ this ] );
3644 element.focus();
3645 }
3646 };
3647
3648 /**
3649 * Handle click event for the entire capsule
3650 */
3651 OO.ui.CapsuleItemWidget.prototype.onClick = function () {
3652 var element = this.getElementGroup();
3653
3654 if ( !this.isDisabled() && element && $.isFunction( element.editItem ) ) {
3655 element.editItem( this );
3656 }
3657 };
3658
3659 /**
3660 * Handle keyDown event for the entire capsule
3661 *
3662 * @param {jQuery.Event} e Key down event
3663 */
3664 OO.ui.CapsuleItemWidget.prototype.onKeyDown = function ( e ) {
3665 var element = this.getElementGroup();
3666
3667 if ( e.keyCode === OO.ui.Keys.BACKSPACE || e.keyCode === OO.ui.Keys.DELETE ) {
3668 element.removeItems( [ this ] );
3669 element.focus();
3670 return false;
3671 } else if ( e.keyCode === OO.ui.Keys.ENTER ) {
3672 element.editItem( this );
3673 return false;
3674 } else if ( e.keyCode === OO.ui.Keys.LEFT ) {
3675 element.getPreviousItem( this ).focus();
3676 } else if ( e.keyCode === OO.ui.Keys.RIGHT ) {
3677 element.getNextItem( this ).focus();
3678 }
3679 };
3680
3681 /**
3682 * CapsuleMultiselectWidgets are something like a {@link OO.ui.ComboBoxInputWidget combo box widget}
3683 * that allows for selecting multiple values.
3684 *
3685 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
3686 *
3687 * @example
3688 * // Example: A CapsuleMultiselectWidget.
3689 * var capsule = new OO.ui.CapsuleMultiselectWidget( {
3690 * label: 'CapsuleMultiselectWidget',
3691 * selected: [ 'Option 1', 'Option 3' ],
3692 * menu: {
3693 * items: [
3694 * new OO.ui.MenuOptionWidget( {
3695 * data: 'Option 1',
3696 * label: 'Option One'
3697 * } ),
3698 * new OO.ui.MenuOptionWidget( {
3699 * data: 'Option 2',
3700 * label: 'Option Two'
3701 * } ),
3702 * new OO.ui.MenuOptionWidget( {
3703 * data: 'Option 3',
3704 * label: 'Option Three'
3705 * } ),
3706 * new OO.ui.MenuOptionWidget( {
3707 * data: 'Option 4',
3708 * label: 'Option Four'
3709 * } ),
3710 * new OO.ui.MenuOptionWidget( {
3711 * data: 'Option 5',
3712 * label: 'Option Five'
3713 * } )
3714 * ]
3715 * }
3716 * } );
3717 * $( 'body' ).append( capsule.$element );
3718 *
3719 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
3720 *
3721 * @class
3722 * @extends OO.ui.Widget
3723 * @mixins OO.ui.mixin.GroupElement
3724 * @mixins OO.ui.mixin.PopupElement
3725 * @mixins OO.ui.mixin.TabIndexedElement
3726 * @mixins OO.ui.mixin.IndicatorElement
3727 * @mixins OO.ui.mixin.IconElement
3728 * @uses OO.ui.CapsuleItemWidget
3729 * @uses OO.ui.MenuSelectWidget
3730 *
3731 * @constructor
3732 * @param {Object} [config] Configuration options
3733 * @cfg {string} [placeholder] Placeholder text
3734 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if not present in the menu.
3735 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added.
3736 * @cfg {Object} [menu] (required) Configuration options to pass to the
3737 * {@link OO.ui.MenuSelectWidget menu select widget}.
3738 * @cfg {Object} [popup] Configuration options to pass to the {@link OO.ui.PopupWidget popup widget}.
3739 * If specified, this popup will be shown instead of the menu (but the menu
3740 * will still be used for item labels and allowArbitrary=false). The widgets
3741 * in the popup should use {@link #addItemsFromData} or {@link #addItems} as necessary.
3742 * @cfg {jQuery} [$overlay=this.$element] Render the menu or popup into a separate layer.
3743 * This configuration is useful in cases where the expanded menu is larger than
3744 * its containing `<div>`. The specified overlay layer is usually on top of
3745 * the containing `<div>` and has a larger area. By default, the menu uses
3746 * relative positioning.
3747 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
3748 */
3749 OO.ui.CapsuleMultiselectWidget = function OoUiCapsuleMultiselectWidget( config ) {
3750 var $tabFocus;
3751
3752 // Parent constructor
3753 OO.ui.CapsuleMultiselectWidget.parent.call( this, config );
3754
3755 // Configuration initialization
3756 config = $.extend( {
3757 allowArbitrary: false,
3758 allowDuplicates: false,
3759 $overlay: this.$element
3760 }, config );
3761
3762 // Properties (must be set before mixin constructor calls)
3763 this.$handle = $( '<div>' );
3764 this.$input = config.popup ? null : $( '<input>' );
3765 if ( config.placeholder !== undefined && config.placeholder !== '' ) {
3766 this.$input.attr( 'placeholder', config.placeholder );
3767 }
3768
3769 // Mixin constructors
3770 OO.ui.mixin.GroupElement.call( this, config );
3771 if ( config.popup ) {
3772 config.popup = $.extend( {}, config.popup, {
3773 align: 'forwards',
3774 anchor: false
3775 } );
3776 OO.ui.mixin.PopupElement.call( this, config );
3777 $tabFocus = $( '<span>' );
3778 OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: $tabFocus } ) );
3779 } else {
3780 this.popup = null;
3781 $tabFocus = null;
3782 OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$input } ) );
3783 }
3784 OO.ui.mixin.IndicatorElement.call( this, config );
3785 OO.ui.mixin.IconElement.call( this, config );
3786
3787 // Properties
3788 this.$content = $( '<div>' );
3789 this.allowArbitrary = config.allowArbitrary;
3790 this.allowDuplicates = config.allowDuplicates;
3791 this.$overlay = config.$overlay;
3792 this.menu = new OO.ui.MenuSelectWidget( $.extend(
3793 {
3794 widget: this,
3795 $input: this.$input,
3796 $floatableContainer: this.$element,
3797 filterFromInput: true,
3798 disabled: this.isDisabled()
3799 },
3800 config.menu
3801 ) );
3802
3803 // Events
3804 if ( this.popup ) {
3805 $tabFocus.on( {
3806 focus: this.focus.bind( this )
3807 } );
3808 this.popup.$element.on( 'focusout', this.onPopupFocusOut.bind( this ) );
3809 if ( this.popup.$autoCloseIgnore ) {
3810 this.popup.$autoCloseIgnore.on( 'focusout', this.onPopupFocusOut.bind( this ) );
3811 }
3812 this.popup.connect( this, {
3813 toggle: function ( visible ) {
3814 $tabFocus.toggle( !visible );
3815 }
3816 } );
3817 } else {
3818 this.$input.on( {
3819 focus: this.onInputFocus.bind( this ),
3820 blur: this.onInputBlur.bind( this ),
3821 'propertychange change click mouseup keydown keyup input cut paste select focus':
3822 OO.ui.debounce( this.updateInputSize.bind( this ) ),
3823 keydown: this.onKeyDown.bind( this ),
3824 keypress: this.onKeyPress.bind( this )
3825 } );
3826 }
3827 this.menu.connect( this, {
3828 choose: 'onMenuChoose',
3829 toggle: 'onMenuToggle',
3830 add: 'onMenuItemsChange',
3831 remove: 'onMenuItemsChange'
3832 } );
3833 this.$handle.on( {
3834 mousedown: this.onMouseDown.bind( this )
3835 } );
3836
3837 // Initialization
3838 if ( this.$input ) {
3839 this.$input.prop( 'disabled', this.isDisabled() );
3840 this.$input.attr( {
3841 role: 'combobox',
3842 'aria-owns': this.menu.getElementId(),
3843 'aria-autocomplete': 'list'
3844 } );
3845 }
3846 if ( config.data ) {
3847 this.setItemsFromData( config.data );
3848 }
3849 this.$content.addClass( 'oo-ui-capsuleMultiselectWidget-content' )
3850 .append( this.$group );
3851 this.$group.addClass( 'oo-ui-capsuleMultiselectWidget-group' );
3852 this.$handle.addClass( 'oo-ui-capsuleMultiselectWidget-handle' )
3853 .append( this.$indicator, this.$icon, this.$content );
3854 this.$element.addClass( 'oo-ui-capsuleMultiselectWidget' )
3855 .append( this.$handle );
3856 if ( this.popup ) {
3857 this.popup.$element.addClass( 'oo-ui-capsuleMultiselectWidget-popup' );
3858 this.$content.append( $tabFocus );
3859 this.$overlay.append( this.popup.$element );
3860 } else {
3861 this.$content.append( this.$input );
3862 this.$overlay.append( this.menu.$element );
3863 }
3864 if ( $tabFocus ) {
3865 $tabFocus.addClass( 'oo-ui-capsuleMultiselectWidget-focusTrap' );
3866 }
3867
3868 // Input size needs to be calculated after everything else is rendered
3869 setTimeout( function () {
3870 if ( this.$input ) {
3871 this.updateInputSize();
3872 }
3873 }.bind( this ) );
3874
3875 this.onMenuItemsChange();
3876 };
3877
3878 /* Setup */
3879
3880 OO.inheritClass( OO.ui.CapsuleMultiselectWidget, OO.ui.Widget );
3881 OO.mixinClass( OO.ui.CapsuleMultiselectWidget, OO.ui.mixin.GroupElement );
3882 OO.mixinClass( OO.ui.CapsuleMultiselectWidget, OO.ui.mixin.PopupElement );
3883 OO.mixinClass( OO.ui.CapsuleMultiselectWidget, OO.ui.mixin.TabIndexedElement );
3884 OO.mixinClass( OO.ui.CapsuleMultiselectWidget, OO.ui.mixin.IndicatorElement );
3885 OO.mixinClass( OO.ui.CapsuleMultiselectWidget, OO.ui.mixin.IconElement );
3886
3887 /* Events */
3888
3889 /**
3890 * @event change
3891 *
3892 * A change event is emitted when the set of selected items changes.
3893 *
3894 * @param {Mixed[]} datas Data of the now-selected items
3895 */
3896
3897 /**
3898 * @event resize
3899 *
3900 * A resize event is emitted when the widget's dimensions change to accomodate newly added items or
3901 * current user input.
3902 */
3903
3904 /* Methods */
3905
3906 /**
3907 * Construct a OO.ui.CapsuleItemWidget (or a subclass thereof) from given label and data.
3908 * May return `null` if the given label and data are not valid.
3909 *
3910 * @protected
3911 * @param {Mixed} data Custom data of any type.
3912 * @param {string} label The label text.
3913 * @return {OO.ui.CapsuleItemWidget|null}
3914 */
3915 OO.ui.CapsuleMultiselectWidget.prototype.createItemWidget = function ( data, label ) {
3916 if ( label === '' ) {
3917 return null;
3918 }
3919 return new OO.ui.CapsuleItemWidget( { data: data, label: label } );
3920 };
3921
3922 /**
3923 * @inheritdoc
3924 */
3925 OO.ui.CapsuleMultiselectWidget.prototype.getInputId = function () {
3926 if ( !this.$input ) {
3927 return null;
3928 }
3929 return OO.ui.mixin.TabIndexedElement.prototype.getInputId.call( this );
3930 };
3931
3932 /**
3933 * Get the data of the items in the capsule
3934 *
3935 * @return {Mixed[]}
3936 */
3937 OO.ui.CapsuleMultiselectWidget.prototype.getItemsData = function () {
3938 return this.getItems().map( function ( item ) {
3939 return item.data;
3940 } );
3941 };
3942
3943 /**
3944 * Set the items in the capsule by providing data
3945 *
3946 * @chainable
3947 * @param {Mixed[]} datas
3948 * @return {OO.ui.CapsuleMultiselectWidget}
3949 */
3950 OO.ui.CapsuleMultiselectWidget.prototype.setItemsFromData = function ( datas ) {
3951 var widget = this,
3952 menu = this.menu,
3953 items = this.getItems();
3954
3955 $.each( datas, function ( i, data ) {
3956 var j, label,
3957 item = menu.getItemFromData( data );
3958
3959 if ( item ) {
3960 label = item.label;
3961 } else if ( widget.allowArbitrary ) {
3962 label = String( data );
3963 } else {
3964 return;
3965 }
3966
3967 item = null;
3968 for ( j = 0; j < items.length; j++ ) {
3969 if ( items[ j ].data === data && items[ j ].label === label ) {
3970 item = items[ j ];
3971 items.splice( j, 1 );
3972 break;
3973 }
3974 }
3975 if ( !item ) {
3976 item = widget.createItemWidget( data, label );
3977 }
3978 if ( item ) {
3979 widget.addItems( [ item ], i );
3980 }
3981 } );
3982
3983 if ( items.length ) {
3984 widget.removeItems( items );
3985 }
3986
3987 return this;
3988 };
3989
3990 /**
3991 * Add items to the capsule by providing their data
3992 *
3993 * @chainable
3994 * @param {Mixed[]} datas
3995 * @return {OO.ui.CapsuleMultiselectWidget}
3996 */
3997 OO.ui.CapsuleMultiselectWidget.prototype.addItemsFromData = function ( datas ) {
3998 var widget = this,
3999 menu = this.menu,
4000 items = [];
4001
4002 $.each( datas, function ( i, data ) {
4003 var item;
4004
4005 if ( !widget.getItemFromData( data ) || widget.allowDuplicates ) {
4006 item = menu.getItemFromData( data );
4007 if ( item ) {
4008 item = widget.createItemWidget( data, item.label );
4009 } else if ( widget.allowArbitrary ) {
4010 item = widget.createItemWidget( data, String( data ) );
4011 }
4012 if ( item ) {
4013 items.push( item );
4014 }
4015 }
4016 } );
4017
4018 if ( items.length ) {
4019 this.addItems( items );
4020 }
4021
4022 return this;
4023 };
4024
4025 /**
4026 * Add items to the capsule by providing a label
4027 *
4028 * @param {string} label
4029 * @return {boolean} Whether the item was added or not
4030 */
4031 OO.ui.CapsuleMultiselectWidget.prototype.addItemFromLabel = function ( label ) {
4032 var item, items;
4033 item = this.menu.getItemFromLabel( label, true );
4034 if ( item ) {
4035 this.addItemsFromData( [ item.data ] );
4036 return true;
4037 } else if ( this.allowArbitrary ) {
4038 items = this.getItems();
4039 this.addItemsFromData( [ label ] );
4040 return !OO.compare( this.getItems(), items );
4041 }
4042 return false;
4043 };
4044
4045 /**
4046 * Remove items by data
4047 *
4048 * @chainable
4049 * @param {Mixed[]} datas
4050 * @return {OO.ui.CapsuleMultiselectWidget}
4051 */
4052 OO.ui.CapsuleMultiselectWidget.prototype.removeItemsFromData = function ( datas ) {
4053 var widget = this,
4054 items = [];
4055
4056 $.each( datas, function ( i, data ) {
4057 var item = widget.getItemFromData( data );
4058 if ( item ) {
4059 items.push( item );
4060 }
4061 } );
4062
4063 if ( items.length ) {
4064 this.removeItems( items );
4065 }
4066
4067 return this;
4068 };
4069
4070 /**
4071 * @inheritdoc
4072 */
4073 OO.ui.CapsuleMultiselectWidget.prototype.addItems = function ( items ) {
4074 var same, i, l,
4075 oldItems = this.items.slice();
4076
4077 OO.ui.mixin.GroupElement.prototype.addItems.call( this, items );
4078
4079 if ( this.items.length !== oldItems.length ) {
4080 same = false;
4081 } else {
4082 same = true;
4083 for ( i = 0, l = oldItems.length; same && i < l; i++ ) {
4084 same = same && this.items[ i ] === oldItems[ i ];
4085 }
4086 }
4087 if ( !same ) {
4088 this.emit( 'change', this.getItemsData() );
4089 this.updateInputSize();
4090 }
4091
4092 return this;
4093 };
4094
4095 /**
4096 * Removes the item from the list and copies its label to `this.$input`.
4097 *
4098 * @param {Object} item
4099 */
4100 OO.ui.CapsuleMultiselectWidget.prototype.editItem = function ( item ) {
4101 this.addItemFromLabel( this.$input.val() );
4102 this.clearInput();
4103 this.$input.val( item.label );
4104 this.updateInputSize();
4105 this.focus();
4106 this.menu.updateItemVisibility(); // Hack, we shouldn't be calling this method directly
4107 this.removeItems( [ item ] );
4108 };
4109
4110 /**
4111 * @inheritdoc
4112 */
4113 OO.ui.CapsuleMultiselectWidget.prototype.removeItems = function ( items ) {
4114 var same, i, l,
4115 oldItems = this.items.slice();
4116
4117 OO.ui.mixin.GroupElement.prototype.removeItems.call( this, items );
4118
4119 if ( this.items.length !== oldItems.length ) {
4120 same = false;
4121 } else {
4122 same = true;
4123 for ( i = 0, l = oldItems.length; same && i < l; i++ ) {
4124 same = same && this.items[ i ] === oldItems[ i ];
4125 }
4126 }
4127 if ( !same ) {
4128 this.emit( 'change', this.getItemsData() );
4129 this.updateInputSize();
4130 }
4131
4132 return this;
4133 };
4134
4135 /**
4136 * @inheritdoc
4137 */
4138 OO.ui.CapsuleMultiselectWidget.prototype.clearItems = function () {
4139 if ( this.items.length ) {
4140 OO.ui.mixin.GroupElement.prototype.clearItems.call( this );
4141 this.emit( 'change', this.getItemsData() );
4142 this.updateInputSize();
4143 }
4144 return this;
4145 };
4146
4147 /**
4148 * Given an item, returns the item after it. If its the last item,
4149 * returns `this.$input`. If no item is passed, returns the very first
4150 * item.
4151 *
4152 * @param {OO.ui.CapsuleItemWidget} [item]
4153 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4154 */
4155 OO.ui.CapsuleMultiselectWidget.prototype.getNextItem = function ( item ) {
4156 var itemIndex;
4157
4158 if ( item === undefined ) {
4159 return this.items[ 0 ];
4160 }
4161
4162 itemIndex = this.items.indexOf( item );
4163 if ( itemIndex < 0 ) { // Item not in list
4164 return false;
4165 } else if ( itemIndex === this.items.length - 1 ) { // Last item
4166 return this.$input;
4167 } else {
4168 return this.items[ itemIndex + 1 ];
4169 }
4170 };
4171
4172 /**
4173 * Given an item, returns the item before it. If its the first item,
4174 * returns `this.$input`. If no item is passed, returns the very last
4175 * item.
4176 *
4177 * @param {OO.ui.CapsuleItemWidget} [item]
4178 * @return {OO.ui.CapsuleItemWidget|jQuery|boolean}
4179 */
4180 OO.ui.CapsuleMultiselectWidget.prototype.getPreviousItem = function ( item ) {
4181 var itemIndex;
4182
4183 if ( item === undefined ) {
4184 return this.items[ this.items.length - 1 ];
4185 }
4186
4187 itemIndex = this.items.indexOf( item );
4188 if ( itemIndex < 0 ) { // Item not in list
4189 return false;
4190 } else if ( itemIndex === 0 ) { // First item
4191 return this.$input;
4192 } else {
4193 return this.items[ itemIndex - 1 ];
4194 }
4195 };
4196
4197 /**
4198 * Get the capsule widget's menu.
4199 *
4200 * @return {OO.ui.MenuSelectWidget} Menu widget
4201 */
4202 OO.ui.CapsuleMultiselectWidget.prototype.getMenu = function () {
4203 return this.menu;
4204 };
4205
4206 /**
4207 * Handle focus events
4208 *
4209 * @private
4210 * @param {jQuery.Event} event
4211 */
4212 OO.ui.CapsuleMultiselectWidget.prototype.onInputFocus = function () {
4213 if ( !this.isDisabled() ) {
4214 this.updateInputSize();
4215 this.menu.toggle( true );
4216 }
4217 };
4218
4219 /**
4220 * Handle blur events
4221 *
4222 * @private
4223 * @param {jQuery.Event} event
4224 */
4225 OO.ui.CapsuleMultiselectWidget.prototype.onInputBlur = function () {
4226 this.addItemFromLabel( this.$input.val() );
4227 this.clearInput();
4228 };
4229
4230 /**
4231 * Handles popup focus out events.
4232 *
4233 * @private
4234 * @param {jQuery.Event} e Focus out event
4235 */
4236 OO.ui.CapsuleMultiselectWidget.prototype.onPopupFocusOut = function () {
4237 var widget = this.popup;
4238
4239 setTimeout( function () {
4240 if (
4241 widget.isVisible() &&
4242 !OO.ui.contains( widget.$element.add( widget.$autoCloseIgnore ).get(), document.activeElement, true )
4243 ) {
4244 widget.toggle( false );
4245 }
4246 } );
4247 };
4248
4249 /**
4250 * Handle mouse down events.
4251 *
4252 * @private
4253 * @param {jQuery.Event} e Mouse down event
4254 */
4255 OO.ui.CapsuleMultiselectWidget.prototype.onMouseDown = function ( e ) {
4256 if ( e.which === OO.ui.MouseButtons.LEFT ) {
4257 this.focus();
4258 return false;
4259 } else {
4260 this.updateInputSize();
4261 }
4262 };
4263
4264 /**
4265 * Handle key press events.
4266 *
4267 * @private
4268 * @param {jQuery.Event} e Key press event
4269 */
4270 OO.ui.CapsuleMultiselectWidget.prototype.onKeyPress = function ( e ) {
4271 if ( !this.isDisabled() ) {
4272 if ( e.which === OO.ui.Keys.ESCAPE ) {
4273 this.clearInput();
4274 return false;
4275 }
4276
4277 if ( !this.popup ) {
4278 this.menu.toggle( true );
4279 if ( e.which === OO.ui.Keys.ENTER ) {
4280 if ( this.addItemFromLabel( this.$input.val() ) ) {
4281 this.clearInput();
4282 }
4283 return false;
4284 }
4285
4286 // Make sure the input gets resized.
4287 setTimeout( this.updateInputSize.bind( this ), 0 );
4288 }
4289 }
4290 };
4291
4292 /**
4293 * Handle key down events.
4294 *
4295 * @private
4296 * @param {jQuery.Event} e Key down event
4297 */
4298 OO.ui.CapsuleMultiselectWidget.prototype.onKeyDown = function ( e ) {
4299 if (
4300 !this.isDisabled() &&
4301 this.$input.val() === '' &&
4302 this.items.length
4303 ) {
4304 // 'keypress' event is not triggered for Backspace
4305 if ( e.keyCode === OO.ui.Keys.BACKSPACE ) {
4306 if ( e.metaKey || e.ctrlKey ) {
4307 this.removeItems( this.items.slice( -1 ) );
4308 } else {
4309 this.editItem( this.items[ this.items.length - 1 ] );
4310 }
4311 return false;
4312 } else if ( e.keyCode === OO.ui.Keys.LEFT ) {
4313 this.getPreviousItem().focus();
4314 } else if ( e.keyCode === OO.ui.Keys.RIGHT ) {
4315 this.getNextItem().focus();
4316 }
4317 }
4318 };
4319
4320 /**
4321 * Update the dimensions of the text input field to encompass all available area.
4322 *
4323 * @private
4324 * @param {jQuery.Event} e Event of some sort
4325 */
4326 OO.ui.CapsuleMultiselectWidget.prototype.updateInputSize = function () {
4327 var $lastItem, direction, contentWidth, currentWidth, bestWidth;
4328 if ( this.$input && !this.isDisabled() ) {
4329 this.$input.css( 'width', '1em' );
4330 $lastItem = this.$group.children().last();
4331 direction = OO.ui.Element.static.getDir( this.$handle );
4332
4333 // Get the width of the input with the placeholder text as
4334 // the value and save it so that we don't keep recalculating
4335 if (
4336 this.contentWidthWithPlaceholder === undefined &&
4337 this.$input.val() === '' &&
4338 this.$input.attr( 'placeholder' ) !== undefined
4339 ) {
4340 this.$input.val( this.$input.attr( 'placeholder' ) );
4341 this.contentWidthWithPlaceholder = this.$input[ 0 ].scrollWidth;
4342 this.$input.val( '' );
4343
4344 }
4345
4346 // Always keep the input wide enough for the placeholder text
4347 contentWidth = Math.max(
4348 this.$input[ 0 ].scrollWidth,
4349 // undefined arguments in Math.max lead to NaN
4350 ( this.contentWidthWithPlaceholder === undefined ) ?
4351 0 : this.contentWidthWithPlaceholder
4352 );
4353 currentWidth = this.$input.width();
4354
4355 if ( contentWidth < currentWidth ) {
4356 this.updateIfHeightChanged();
4357 // All is fine, don't perform expensive calculations
4358 return;
4359 }
4360
4361 if ( $lastItem.length === 0 ) {
4362 bestWidth = this.$content.innerWidth();
4363 } else {
4364 bestWidth = direction === 'ltr' ?
4365 this.$content.innerWidth() - $lastItem.position().left - $lastItem.outerWidth() :
4366 $lastItem.position().left;
4367 }
4368
4369 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
4370 // pixels this is off by are coming from.
4371 bestWidth -= 10;
4372 if ( contentWidth > bestWidth ) {
4373 // This will result in the input getting shifted to the next line
4374 bestWidth = this.$content.innerWidth() - 10;
4375 }
4376 this.$input.width( Math.floor( bestWidth ) );
4377 this.updateIfHeightChanged();
4378 } else {
4379 this.updateIfHeightChanged();
4380 }
4381 };
4382
4383 /**
4384 * Determine if widget height changed, and if so, update menu position and emit 'resize' event.
4385 *
4386 * @private
4387 */
4388 OO.ui.CapsuleMultiselectWidget.prototype.updateIfHeightChanged = function () {
4389 var height = this.$element.height();
4390 if ( height !== this.height ) {
4391 this.height = height;
4392 this.menu.position();
4393 if ( this.popup ) {
4394 this.popup.updateDimensions();
4395 }
4396 this.emit( 'resize' );
4397 }
4398 };
4399
4400 /**
4401 * Handle menu choose events.
4402 *
4403 * @private
4404 * @param {OO.ui.OptionWidget} item Chosen item
4405 */
4406 OO.ui.CapsuleMultiselectWidget.prototype.onMenuChoose = function ( item ) {
4407 if ( item && item.isVisible() ) {
4408 this.addItemsFromData( [ item.getData() ] );
4409 this.clearInput();
4410 }
4411 };
4412
4413 /**
4414 * Handle menu toggle events.
4415 *
4416 * @private
4417 * @param {boolean} isVisible Menu toggle event
4418 */
4419 OO.ui.CapsuleMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
4420 this.$element.toggleClass( 'oo-ui-capsuleMultiselectWidget-open', isVisible );
4421 };
4422
4423 /**
4424 * Handle menu item change events.
4425 *
4426 * @private
4427 */
4428 OO.ui.CapsuleMultiselectWidget.prototype.onMenuItemsChange = function () {
4429 this.setItemsFromData( this.getItemsData() );
4430 this.$element.toggleClass( 'oo-ui-capsuleMultiselectWidget-empty', this.menu.isEmpty() );
4431 };
4432
4433 /**
4434 * Clear the input field
4435 *
4436 * @private
4437 */
4438 OO.ui.CapsuleMultiselectWidget.prototype.clearInput = function () {
4439 if ( this.$input ) {
4440 this.$input.val( '' );
4441 this.updateInputSize();
4442 }
4443 if ( this.popup ) {
4444 this.popup.toggle( false );
4445 }
4446 this.menu.toggle( false );
4447 this.menu.selectItem();
4448 this.menu.highlightItem();
4449 };
4450
4451 /**
4452 * @inheritdoc
4453 */
4454 OO.ui.CapsuleMultiselectWidget.prototype.setDisabled = function ( disabled ) {
4455 var i, len;
4456
4457 // Parent method
4458 OO.ui.CapsuleMultiselectWidget.parent.prototype.setDisabled.call( this, disabled );
4459
4460 if ( this.$input ) {
4461 this.$input.prop( 'disabled', this.isDisabled() );
4462 }
4463 if ( this.menu ) {
4464 this.menu.setDisabled( this.isDisabled() );
4465 }
4466 if ( this.popup ) {
4467 this.popup.setDisabled( this.isDisabled() );
4468 }
4469
4470 if ( this.items ) {
4471 for ( i = 0, len = this.items.length; i < len; i++ ) {
4472 this.items[ i ].updateDisabled();
4473 }
4474 }
4475
4476 return this;
4477 };
4478
4479 /**
4480 * Focus the widget
4481 *
4482 * @chainable
4483 */
4484 OO.ui.CapsuleMultiselectWidget.prototype.focus = function () {
4485 if ( !this.isDisabled() ) {
4486 if ( this.popup ) {
4487 this.popup.setSize( this.$handle.outerWidth() );
4488 this.popup.toggle( true );
4489 OO.ui.findFocusable( this.popup.$element ).focus();
4490 } else {
4491 OO.ui.mixin.TabIndexedElement.prototype.focus.call( this );
4492 }
4493 }
4494 return this;
4495 };
4496
4497 /**
4498 * TagItemWidgets are used within a {@link OO.ui.TagMultiselectWidget
4499 * TagMultiselectWidget} to display the selected items.
4500 *
4501 * @class
4502 * @extends OO.ui.Widget
4503 * @mixins OO.ui.mixin.ItemWidget
4504 * @mixins OO.ui.mixin.LabelElement
4505 * @mixins OO.ui.mixin.FlaggedElement
4506 * @mixins OO.ui.mixin.TabIndexedElement
4507 * @mixins OO.ui.mixin.DraggableElement
4508 *
4509 * @constructor
4510 * @param {Object} [config] Configuration object
4511 * @cfg {boolean} [valid=true] Item is valid
4512 */
4513 OO.ui.TagItemWidget = function OoUiTagItemWidget( config ) {
4514 config = config || {};
4515
4516 // Parent constructor
4517 OO.ui.TagItemWidget.parent.call( this, config );
4518
4519 // Mixin constructors
4520 OO.ui.mixin.ItemWidget.call( this );
4521 OO.ui.mixin.LabelElement.call( this, config );
4522 OO.ui.mixin.FlaggedElement.call( this, config );
4523 OO.ui.mixin.TabIndexedElement.call( this, config );
4524 OO.ui.mixin.DraggableElement.call( this, config );
4525
4526 this.valid = config.valid === undefined ? true : !!config.valid;
4527
4528 this.closeButton = new OO.ui.ButtonWidget( {
4529 framed: false,
4530 icon: 'close',
4531 tabIndex: -1,
4532 title: OO.ui.msg( 'ooui-item-remove' )
4533 } );
4534 this.closeButton.setDisabled( this.isDisabled() );
4535
4536 // Events
4537 this.closeButton
4538 .connect( this, { click: 'remove' } );
4539 this.$element
4540 .on( 'click', this.select.bind( this ) )
4541 .on( 'keydown', this.onKeyDown.bind( this ) )
4542 // Prevent propagation of mousedown; the tag item "lives" in the
4543 // clickable area of the TagMultiselectWidget, which listens to
4544 // mousedown to open the menu or popup. We want to prevent that
4545 // for clicks specifically on the tag itself, so the actions taken
4546 // are more deliberate. When the tag is clicked, it will emit the
4547 // selection event (similar to how #OO.ui.MultioptionWidget emits 'change')
4548 // and can be handled separately.
4549 .on( 'mousedown', function ( e ) { e.stopPropagation(); } );
4550
4551 // Initialization
4552 this.$element
4553 .addClass( 'oo-ui-tagItemWidget' )
4554 .append( this.$label, this.closeButton.$element );
4555 };
4556
4557 /* Initialization */
4558
4559 OO.inheritClass( OO.ui.TagItemWidget, OO.ui.Widget );
4560 OO.mixinClass( OO.ui.TagItemWidget, OO.ui.mixin.ItemWidget );
4561 OO.mixinClass( OO.ui.TagItemWidget, OO.ui.mixin.LabelElement );
4562 OO.mixinClass( OO.ui.TagItemWidget, OO.ui.mixin.FlaggedElement );
4563 OO.mixinClass( OO.ui.TagItemWidget, OO.ui.mixin.TabIndexedElement );
4564 OO.mixinClass( OO.ui.TagItemWidget, OO.ui.mixin.DraggableElement );
4565
4566 /* Events */
4567
4568 /**
4569 * @event remove
4570 *
4571 * A remove action was performed on the item
4572 */
4573
4574 /**
4575 * @event navigate
4576 * @param {string} direction Direction of the movement, forward or backwards
4577 *
4578 * A navigate action was performed on the item
4579 */
4580
4581 /**
4582 * @event select
4583 *
4584 * The tag widget was selected. This can occur when the widget
4585 * is either clicked or enter was pressed on it.
4586 */
4587
4588 /**
4589 * @event valid
4590 * @param {boolean} isValid Item is valid
4591 *
4592 * Item validity has changed
4593 */
4594
4595 /* Methods */
4596
4597 /**
4598 * @inheritdoc
4599 */
4600 OO.ui.TagItemWidget.prototype.setDisabled = function ( state ) {
4601 // Parent method
4602 OO.ui.TagItemWidget.parent.prototype.setDisabled.call( this, state );
4603
4604 if ( this.closeButton ) {
4605 this.closeButton.setDisabled( state );
4606 }
4607 return this;
4608 };
4609
4610 /**
4611 * Handle removal of the item
4612 *
4613 * This is mainly for extensibility concerns, so other children
4614 * of this class can change the behavior if they need to. This
4615 * is called by both clicking the 'remove' button but also
4616 * on keypress, which is harder to override if needed.
4617 *
4618 * @fires remove
4619 */
4620 OO.ui.TagItemWidget.prototype.remove = function () {
4621 if ( !this.isDisabled() ) {
4622 this.emit( 'remove' );
4623 }
4624 };
4625
4626 /**
4627 * Handle a keydown event on the widget
4628 *
4629 * @fires navigate
4630 * @fires remove
4631 * @param {jQuery.Event} e Key down event
4632 * @return {boolean|undefined} false to stop the operation
4633 */
4634 OO.ui.TagItemWidget.prototype.onKeyDown = function ( e ) {
4635 var movement;
4636
4637 if ( e.keyCode === OO.ui.Keys.BACKSPACE || e.keyCode === OO.ui.Keys.DELETE ) {
4638 this.remove();
4639 return false;
4640 } else if ( e.keyCode === OO.ui.Keys.ENTER ) {
4641 this.select();
4642 return false;
4643 } else if (
4644 e.keyCode === OO.ui.Keys.LEFT ||
4645 e.keyCode === OO.ui.Keys.RIGHT
4646 ) {
4647 if ( OO.ui.Element.static.getDir( this.$element ) === 'rtl' ) {
4648 movement = {
4649 left: 'forwards',
4650 right: 'backwards'
4651 };
4652 } else {
4653 movement = {
4654 left: 'backwards',
4655 right: 'forwards'
4656 };
4657 }
4658
4659 this.emit(
4660 'navigate',
4661 e.keyCode === OO.ui.Keys.LEFT ?
4662 movement.left : movement.right
4663 );
4664 }
4665 };
4666
4667 /**
4668 * Select this item
4669 *
4670 * @fires select
4671 */
4672 OO.ui.TagItemWidget.prototype.select = function () {
4673 if ( !this.isDisabled() ) {
4674 this.emit( 'select' );
4675 }
4676 };
4677
4678 /**
4679 * Set the valid state of this item
4680 *
4681 * @param {boolean} [valid] Item is valid
4682 * @fires valid
4683 */
4684 OO.ui.TagItemWidget.prototype.toggleValid = function ( valid ) {
4685 valid = valid === undefined ? !this.valid : !!valid;
4686
4687 if ( this.valid !== valid ) {
4688 this.valid = valid;
4689
4690 this.setFlags( { invalid: !this.valid } );
4691
4692 this.emit( 'valid', this.valid );
4693 }
4694 };
4695
4696 /**
4697 * Check whether the item is valid
4698 *
4699 * @return {boolean} Item is valid
4700 */
4701 OO.ui.TagItemWidget.prototype.isValid = function () {
4702 return this.valid;
4703 };
4704
4705 /**
4706 * A basic tag multiselect widget, similar in concept to {@link OO.ui.ComboBoxInputWidget combo box widget}
4707 * that allows the user to add multiple values that are displayed in a tag area.
4708 *
4709 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
4710 *
4711 * This widget is a base widget; see {@link OO.ui.MenuTagMultiselectWidget MenuTagMultiselectWidget} and
4712 * {@link OO.ui.PopupTagMultiselectWidget PopupTagMultiselectWidget} for the implementations that use
4713 * a menu and a popup respectively.
4714 *
4715 * @example
4716 * // Example: A basic TagMultiselectWidget.
4717 * var widget = new OO.ui.TagMultiselectWidget( {
4718 * inputPosition: 'outline',
4719 * allowedValues: [ 'Option 1', 'Option 2', 'Option 3' ],
4720 * selected: [ 'Option 1' ]
4721 * } );
4722 * $( 'body' ).append( widget.$element );
4723 *
4724 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
4725 *
4726 * @class
4727 * @extends OO.ui.Widget
4728 * @mixins OO.ui.mixin.GroupWidget
4729 * @mixins OO.ui.mixin.DraggableGroupElement
4730 * @mixins OO.ui.mixin.IndicatorElement
4731 * @mixins OO.ui.mixin.IconElement
4732 * @mixins OO.ui.mixin.TabIndexedElement
4733 * @mixins OO.ui.mixin.FlaggedElement
4734 *
4735 * @constructor
4736 * @param {Object} config Configuration object
4737 * @cfg {Object} [input] Configuration options for the input widget
4738 * @cfg {OO.ui.InputWidget} [inputWidget] An optional input widget. If given, it will
4739 * replace the input widget used in the TagMultiselectWidget. If not given,
4740 * TagMultiselectWidget creates its own.
4741 * @cfg {boolean} [inputPosition='inline'] Position of the input. Options are:
4742 * - inline: The input is invisible, but exists inside the tag list, so
4743 * the user types into the tag groups to add tags.
4744 * - outline: The input is underneath the tag area.
4745 * - none: No input supplied
4746 * @cfg {boolean} [allowEditTags=true] Allow editing of the tags by clicking them
4747 * @cfg {boolean} [allowArbitrary=false] Allow data items to be added even if
4748 * not present in the menu.
4749 * @cfg {Object[]} [allowedValues] An array representing the allowed items
4750 * by their datas.
4751 * @cfg {boolean} [allowDuplicates=false] Allow duplicate items to be added
4752 * @cfg {boolean} [allowDisplayInvalidTags=false] Allow the display of
4753 * invalid tags. These tags will display with an invalid state, and
4754 * the widget as a whole will have an invalid state if any invalid tags
4755 * are present.
4756 * @cfg {boolean} [allowReordering=true] Allow reordering of the items
4757 * @cfg {Object[]|String[]} [selected] A set of selected tags. If given,
4758 * these will appear in the tag list on initialization, as long as they
4759 * pass the validity tests.
4760 */
4761 OO.ui.TagMultiselectWidget = function OoUiTagMultiselectWidget( config ) {
4762 var inputEvents,
4763 rAF = window.requestAnimationFrame || setTimeout,
4764 widget = this,
4765 $tabFocus = $( '<span>' )
4766 .addClass( 'oo-ui-tagMultiselectWidget-focusTrap' );
4767
4768 config = config || {};
4769
4770 // Parent constructor
4771 OO.ui.TagMultiselectWidget.parent.call( this, config );
4772
4773 // Mixin constructors
4774 OO.ui.mixin.GroupWidget.call( this, config );
4775 OO.ui.mixin.IndicatorElement.call( this, config );
4776 OO.ui.mixin.IconElement.call( this, config );
4777 OO.ui.mixin.TabIndexedElement.call( this, config );
4778 OO.ui.mixin.FlaggedElement.call( this, config );
4779 OO.ui.mixin.DraggableGroupElement.call( this, config );
4780
4781 this.toggleDraggable(
4782 config.allowReordering === undefined ?
4783 true : !!config.allowReordering
4784 );
4785
4786 this.inputPosition =
4787 this.constructor.static.allowedInputPositions.indexOf( config.inputPosition ) > -1 ?
4788 config.inputPosition : 'inline';
4789 this.allowEditTags = config.allowEditTags === undefined ? true : !!config.allowEditTags;
4790 this.allowArbitrary = !!config.allowArbitrary;
4791 this.allowDuplicates = !!config.allowDuplicates;
4792 this.allowedValues = config.allowedValues || [];
4793 this.allowDisplayInvalidTags = config.allowDisplayInvalidTags;
4794 this.hasInput = this.inputPosition !== 'none';
4795 this.height = null;
4796 this.valid = true;
4797
4798 this.$content = $( '<div>' )
4799 .addClass( 'oo-ui-tagMultiselectWidget-content' );
4800 this.$handle = $( '<div>' )
4801 .addClass( 'oo-ui-tagMultiselectWidget-handle' )
4802 .append(
4803 this.$indicator,
4804 this.$icon,
4805 this.$content
4806 .append(
4807 this.$group
4808 .addClass( 'oo-ui-tagMultiselectWidget-group' )
4809 )
4810 );
4811
4812 // Events
4813 this.aggregate( {
4814 remove: 'itemRemove',
4815 navigate: 'itemNavigate',
4816 select: 'itemSelect'
4817 } );
4818 this.connect( this, {
4819 itemRemove: 'onTagRemove',
4820 itemSelect: 'onTagSelect',
4821 itemNavigate: 'onTagNavigate',
4822 change: 'onChangeTags'
4823 } );
4824 this.$handle.on( {
4825 mousedown: this.onMouseDown.bind( this )
4826 } );
4827
4828 // Initialize
4829 this.$element
4830 .addClass( 'oo-ui-tagMultiselectWidget' )
4831 .append( this.$handle );
4832
4833 if ( this.hasInput ) {
4834 if ( config.inputWidget ) {
4835 this.input = config.inputWidget;
4836 } else {
4837 this.input = new OO.ui.TextInputWidget( $.extend( {
4838 placeholder: config.placeholder,
4839 classes: [ 'oo-ui-tagMultiselectWidget-input' ]
4840 }, config.input ) );
4841 }
4842 this.input.setDisabled( this.isDisabled() );
4843
4844 inputEvents = {
4845 focus: this.onInputFocus.bind( this ),
4846 blur: this.onInputBlur.bind( this ),
4847 'propertychange change click mouseup keydown keyup input cut paste select focus':
4848 OO.ui.debounce( this.updateInputSize.bind( this ) ),
4849 keydown: this.onInputKeyDown.bind( this ),
4850 keypress: this.onInputKeyPress.bind( this )
4851 };
4852
4853 this.input.$input.on( inputEvents );
4854
4855 if ( this.inputPosition === 'outline' ) {
4856 // Override max-height for the input widget
4857 // in the case the widget is outline so it can
4858 // stretch all the way if the widet is wide
4859 this.input.$element.css( 'max-width', 'inherit' );
4860 this.$element
4861 .addClass( 'oo-ui-tagMultiselectWidget-outlined' )
4862 .append( this.input.$element );
4863 } else {
4864 this.$element.addClass( 'oo-ui-tagMultiselectWidget-inlined' );
4865 // HACK: When the widget is using 'inline' input, the
4866 // behavior needs to only use the $input itself
4867 // so we style and size it accordingly (otherwise
4868 // the styling and sizing can get very convoluted
4869 // when the wrapping divs and other elements)
4870 // We are taking advantage of still being able to
4871 // call the widget itself for operations like
4872 // .getValue() and setDisabled() and .focus() but
4873 // having only the $input attached to the DOM
4874 this.$content.append( this.input.$input );
4875 }
4876 } else {
4877 this.$content.append( $tabFocus );
4878 }
4879
4880 this.setTabIndexedElement(
4881 this.hasInput ?
4882 this.input.$input :
4883 $tabFocus
4884 );
4885
4886 if ( config.selected ) {
4887 this.setValue( config.selected );
4888 }
4889
4890 // HACK: Input size needs to be calculated after everything
4891 // else is rendered
4892 rAF( function () {
4893 if ( widget.hasInput ) {
4894 widget.updateInputSize();
4895 }
4896 } );
4897 };
4898
4899 /* Initialization */
4900
4901 OO.inheritClass( OO.ui.TagMultiselectWidget, OO.ui.Widget );
4902 OO.mixinClass( OO.ui.TagMultiselectWidget, OO.ui.mixin.GroupWidget );
4903 OO.mixinClass( OO.ui.TagMultiselectWidget, OO.ui.mixin.DraggableGroupElement );
4904 OO.mixinClass( OO.ui.TagMultiselectWidget, OO.ui.mixin.IndicatorElement );
4905 OO.mixinClass( OO.ui.TagMultiselectWidget, OO.ui.mixin.IconElement );
4906 OO.mixinClass( OO.ui.TagMultiselectWidget, OO.ui.mixin.TabIndexedElement );
4907 OO.mixinClass( OO.ui.TagMultiselectWidget, OO.ui.mixin.FlaggedElement );
4908
4909 /* Static properties */
4910
4911 /**
4912 * Allowed input positions.
4913 * - inline: The input is inside the tag list
4914 * - outline: The input is under the tag list
4915 * - none: There is no input
4916 *
4917 * @property {Array}
4918 */
4919 OO.ui.TagMultiselectWidget.static.allowedInputPositions = [ 'inline', 'outline', 'none' ];
4920
4921 /* Methods */
4922
4923 /**
4924 * Handle mouse down events.
4925 *
4926 * @private
4927 * @param {jQuery.Event} e Mouse down event
4928 * @return {boolean} False to prevent defaults
4929 */
4930 OO.ui.TagMultiselectWidget.prototype.onMouseDown = function ( e ) {
4931 if ( !this.isDisabled() && e.which === OO.ui.MouseButtons.LEFT ) {
4932 this.focus();
4933 return false;
4934 }
4935 };
4936
4937 /**
4938 * Handle key press events.
4939 *
4940 * @private
4941 * @param {jQuery.Event} e Key press event
4942 * @return {boolean} Whether to prevent defaults
4943 */
4944 OO.ui.TagMultiselectWidget.prototype.onInputKeyPress = function ( e ) {
4945 var stopOrContinue,
4946 withMetaKey = e.metaKey || e.ctrlKey;
4947
4948 if ( !this.isDisabled() ) {
4949 if ( e.which === OO.ui.Keys.ENTER ) {
4950 stopOrContinue = this.doInputEnter( e, withMetaKey );
4951 }
4952
4953 // Make sure the input gets resized.
4954 setTimeout( this.updateInputSize.bind( this ), 0 );
4955 return stopOrContinue;
4956 }
4957 };
4958
4959 /**
4960 * Handle key down events.
4961 *
4962 * @private
4963 * @param {jQuery.Event} e Key down event
4964 * @return {boolean}
4965 */
4966 OO.ui.TagMultiselectWidget.prototype.onInputKeyDown = function ( e ) {
4967 var movement, direction,
4968 withMetaKey = e.metaKey || e.ctrlKey;
4969
4970 if ( !this.isDisabled() ) {
4971 // 'keypress' event is not triggered for Backspace
4972 if ( e.keyCode === OO.ui.Keys.BACKSPACE ) {
4973 return this.doInputBackspace( e, withMetaKey );
4974 } else if ( e.keyCode === OO.ui.Keys.ESCAPE ) {
4975 return this.doInputEscape( e );
4976 } else if (
4977 e.keyCode === OO.ui.Keys.LEFT ||
4978 e.keyCode === OO.ui.Keys.RIGHT
4979 ) {
4980 if ( OO.ui.Element.static.getDir( this.$element ) === 'rtl' ) {
4981 movement = {
4982 left: 'forwards',
4983 right: 'backwards'
4984 };
4985 } else {
4986 movement = {
4987 left: 'backwards',
4988 right: 'forwards'
4989 };
4990 }
4991 direction = e.keyCode === OO.ui.Keys.LEFT ?
4992 movement.left : movement.right;
4993
4994 return this.doInputArrow( e, direction, withMetaKey );
4995 }
4996 }
4997 };
4998
4999 /**
5000 * Respond to input focus event
5001 */
5002 OO.ui.TagMultiselectWidget.prototype.onInputFocus = function () {
5003 this.$element.addClass( 'oo-ui-tagMultiselectWidget-focus' );
5004 };
5005
5006 /**
5007 * Respond to input blur event
5008 */
5009 OO.ui.TagMultiselectWidget.prototype.onInputBlur = function () {
5010 this.$element.removeClass( 'oo-ui-tagMultiselectWidget-focus' );
5011 };
5012
5013 /**
5014 * Perform an action after the enter key on the input
5015 *
5016 * @param {jQuery.Event} e Event data
5017 * @param {boolean} [withMetaKey] Whether this key was pressed with
5018 * a meta key like 'ctrl'
5019 * @return {boolean} Whether to prevent defaults
5020 */
5021 OO.ui.TagMultiselectWidget.prototype.doInputEnter = function () {
5022 this.addTagFromInput();
5023 return false;
5024 };
5025
5026 /**
5027 * Perform an action responding to the enter key on the input
5028 *
5029 * @param {jQuery.Event} e Event data
5030 * @param {boolean} [withMetaKey] Whether this key was pressed with
5031 * a meta key like 'ctrl'
5032 * @return {boolean} Whether to prevent defaults
5033 */
5034 OO.ui.TagMultiselectWidget.prototype.doInputBackspace = function ( e, withMetaKey ) {
5035 var items, item;
5036
5037 if (
5038 this.inputPosition === 'inline' &&
5039 this.input.getValue() === '' &&
5040 !this.isEmpty()
5041 ) {
5042 // Delete the last item
5043 items = this.getItems();
5044 item = items[ items.length - 1 ];
5045 this.removeItems( [ item ] );
5046 // If Ctrl/Cmd was pressed, delete item entirely.
5047 // Otherwise put it into the text field for editing.
5048 if ( !withMetaKey ) {
5049 this.input.setValue( item.getData() );
5050 }
5051
5052 return false;
5053 }
5054 };
5055
5056 /**
5057 * Perform an action after the escape key on the input
5058 *
5059 * @param {jQuery.Event} e Event data
5060 */
5061 OO.ui.TagMultiselectWidget.prototype.doInputEscape = function () {
5062 this.clearInput();
5063 };
5064
5065 /**
5066 * Perform an action after the arrow key on the input, select the previous
5067 * or next item from the input.
5068 * See #getPreviousItem and #getNextItem
5069 *
5070 * @param {jQuery.Event} e Event data
5071 * @param {string} direction Direction of the movement; forwards or backwards
5072 * @param {boolean} [withMetaKey] Whether this key was pressed with
5073 * a meta key like 'ctrl'
5074 */
5075 OO.ui.TagMultiselectWidget.prototype.doInputArrow = function ( e, direction ) {
5076 if (
5077 this.inputPosition === 'inline' &&
5078 !this.isEmpty()
5079 ) {
5080 if ( direction === 'backwards' ) {
5081 // Get previous item
5082 this.getPreviousItem().focus();
5083 } else {
5084 // Get next item
5085 this.getNextItem().focus();
5086 }
5087 }
5088 };
5089
5090 /**
5091 * Respond to item select event
5092 *
5093 * @param {OO.ui.TagItemWidget} item Selected item
5094 */
5095 OO.ui.TagMultiselectWidget.prototype.onTagSelect = function ( item ) {
5096 if ( this.hasInput && this.allowEditTags ) {
5097 if ( this.input.getValue() ) {
5098 this.addTagFromInput();
5099 }
5100 // 1. Get the label of the tag into the input
5101 this.input.setValue( item.getData() );
5102 // 2. Remove the tag
5103 this.removeItems( [ item ] );
5104 // 3. Focus the input
5105 this.focus();
5106 }
5107 };
5108
5109 /**
5110 * Respond to change event, where items were added, removed, or cleared.
5111 */
5112 OO.ui.TagMultiselectWidget.prototype.onChangeTags = function () {
5113 this.toggleValid( this.checkValidity() );
5114 if ( this.hasInput ) {
5115 this.updateInputSize();
5116 }
5117 this.updateIfHeightChanged();
5118 };
5119
5120 /**
5121 * @inheritdoc
5122 */
5123 OO.ui.TagMultiselectWidget.prototype.setDisabled = function ( isDisabled ) {
5124 // Parent method
5125 OO.ui.TagMultiselectWidget.parent.prototype.setDisabled.call( this, isDisabled );
5126
5127 if ( this.hasInput && this.input ) {
5128 this.input.setDisabled( !!isDisabled );
5129 }
5130
5131 if ( this.items ) {
5132 this.getItems().forEach( function ( item ) {
5133 item.setDisabled( !!isDisabled );
5134 } );
5135 }
5136 };
5137
5138 /**
5139 * Respond to tag remove event
5140 * @param {OO.ui.TagItemWidget} item Removed tag
5141 */
5142 OO.ui.TagMultiselectWidget.prototype.onTagRemove = function ( item ) {
5143 this.removeTagByData( item.getData() );
5144 };
5145
5146 /**
5147 * Respond to navigate event on the tag
5148 *
5149 * @param {OO.ui.TagItemWidget} item Removed tag
5150 * @param {string} direction Direction of movement; 'forwards' or 'backwards'
5151 */
5152 OO.ui.TagMultiselectWidget.prototype.onTagNavigate = function ( item, direction ) {
5153 if ( direction === 'forwards' ) {
5154 this.getNextItem( item ).focus();
5155 } else {
5156 this.getPreviousItem( item ).focus();
5157 }
5158 };
5159
5160 /**
5161 * Add tag from input value
5162 */
5163 OO.ui.TagMultiselectWidget.prototype.addTagFromInput = function () {
5164 var val = this.input.getValue(),
5165 isValid = this.isAllowedData( val );
5166
5167 if ( !val ) {
5168 return;
5169 }
5170
5171 if ( isValid || this.allowDisplayInvalidTags ) {
5172 this.addTag( val );
5173 this.clearInput();
5174 this.focus();
5175 }
5176 };
5177
5178 /**
5179 * Clear the input
5180 */
5181 OO.ui.TagMultiselectWidget.prototype.clearInput = function () {
5182 this.input.setValue( '' );
5183 };
5184
5185 /**
5186 * Check whether the given value is a duplicate of an existing
5187 * tag already in the list.
5188 *
5189 * @param {string|Object} data Requested value
5190 * @return {boolean} Value is duplicate
5191 */
5192 OO.ui.TagMultiselectWidget.prototype.isDuplicateData = function ( data ) {
5193 return !!this.getItemFromData( data );
5194 };
5195
5196 /**
5197 * Check whether a given value is allowed to be added
5198 *
5199 * @param {string|Object} data Requested value
5200 * @return {boolean} Value is allowed
5201 */
5202 OO.ui.TagMultiselectWidget.prototype.isAllowedData = function ( data ) {
5203 if (
5204 !this.allowDuplicates &&
5205 this.isDuplicateData( data )
5206 ) {
5207 return false;
5208 }
5209
5210 if ( this.allowArbitrary ) {
5211 return true;
5212 }
5213
5214 // Check with allowed values
5215 if (
5216 this.getAllowedValues().some( function ( value ) {
5217 return data === value;
5218 } )
5219 ) {
5220 return true;
5221 }
5222
5223 return false;
5224 };
5225
5226 /**
5227 * Get the allowed values list
5228 *
5229 * @return {string[]} Allowed data values
5230 */
5231 OO.ui.TagMultiselectWidget.prototype.getAllowedValues = function () {
5232 return this.allowedValues;
5233 };
5234
5235 /**
5236 * Add a value to the allowed values list
5237 *
5238 * @param {string} value Allowed data value
5239 */
5240 OO.ui.TagMultiselectWidget.prototype.addAllowedValue = function ( value ) {
5241 if ( this.allowedValues.indexOf( value ) === -1 ) {
5242 this.allowedValues.push( value );
5243 }
5244 };
5245
5246 /**
5247 * Get the datas of the currently selected items
5248 *
5249 * @return {string[]|Object[]} Datas of currently selected items
5250 */
5251 OO.ui.TagMultiselectWidget.prototype.getValue = function () {
5252 return this.getItems()
5253 .filter( function ( item ) {
5254 return item.isValid();
5255 } )
5256 .map( function ( item ) {
5257 return item.getData();
5258 } );
5259 };
5260
5261 /**
5262 * Set the value of this widget by datas.
5263 *
5264 * @param {string|string[]|Object|Object[]} valueObject An object representing the data
5265 * and label of the value. If the widget allows arbitrary values,
5266 * the items will be added as-is. Otherwise, the data value will
5267 * be checked against allowedValues.
5268 * This object must contain at least a data key. Example:
5269 * { data: 'foo', label: 'Foo item' }
5270 * For multiple items, use an array of objects. For example:
5271 * [
5272 * { data: 'foo', label: 'Foo item' },
5273 * { data: 'bar', label: 'Bar item' }
5274 * ]
5275 * Value can also be added with plaintext array, for example:
5276 * [ 'foo', 'bar', 'bla' ] or a single string, like 'foo'
5277 */
5278 OO.ui.TagMultiselectWidget.prototype.setValue = function ( valueObject ) {
5279 valueObject = Array.isArray( valueObject ) ? valueObject : [ valueObject ];
5280
5281 this.clearItems();
5282 valueObject.forEach( function ( obj ) {
5283 if ( typeof obj === 'string' ) {
5284 this.addTag( obj );
5285 } else {
5286 this.addTag( obj.data, obj.label );
5287 }
5288 }.bind( this ) );
5289 };
5290
5291 /**
5292 * Add tag to the display area
5293 *
5294 * @param {string|Object} data Tag data
5295 * @param {string} [label] Tag label. If no label is provided, the
5296 * stringified version of the data will be used instead.
5297 * @return {boolean} Item was added successfully
5298 */
5299 OO.ui.TagMultiselectWidget.prototype.addTag = function ( data, label ) {
5300 var newItemWidget,
5301 isValid = this.isAllowedData( data );
5302
5303 if ( isValid || this.allowDisplayInvalidTags ) {
5304 newItemWidget = this.createTagItemWidget( data, label );
5305 newItemWidget.toggleValid( isValid );
5306 this.addItems( [ newItemWidget ] );
5307 return true;
5308 }
5309 return false;
5310 };
5311
5312 /**
5313 * Remove tag by its data property.
5314 *
5315 * @param {string|Object} data Tag data
5316 */
5317 OO.ui.TagMultiselectWidget.prototype.removeTagByData = function ( data ) {
5318 var item = this.getItemFromData( data );
5319
5320 this.removeItems( [ item ] );
5321 };
5322
5323 /**
5324 * Construct a OO.ui.TagItemWidget (or a subclass thereof) from given label and data.
5325 *
5326 * @protected
5327 * @param {string} data Item data
5328 * @param {string} label The label text.
5329 * @return {OO.ui.TagItemWidget}
5330 */
5331 OO.ui.TagMultiselectWidget.prototype.createTagItemWidget = function ( data, label ) {
5332 label = label || data;
5333
5334 return new OO.ui.TagItemWidget( { data: data, label: label } );
5335 };
5336
5337 /**
5338 * Given an item, returns the item after it. If the item is already the
5339 * last item, return `this.input`. If no item is passed, returns the
5340 * very first item.
5341 *
5342 * @protected
5343 * @param {OO.ui.TagItemWidget} [item] Tag item
5344 * @return {OO.ui.Widget} The next widget available.
5345 */
5346 OO.ui.TagMultiselectWidget.prototype.getNextItem = function ( item ) {
5347 var itemIndex = this.items.indexOf( item );
5348
5349 if ( item === undefined || itemIndex === -1 ) {
5350 return this.items[ 0 ];
5351 }
5352
5353 if ( itemIndex === this.items.length - 1 ) { // Last item
5354 if ( this.hasInput ) {
5355 return this.input;
5356 } else {
5357 // Return first item
5358 return this.items[ 0 ];
5359 }
5360 } else {
5361 return this.items[ itemIndex + 1 ];
5362 }
5363 };
5364
5365 /**
5366 * Given an item, returns the item before it. If the item is already the
5367 * first item, return `this.input`. If no item is passed, returns the
5368 * very last item.
5369 *
5370 * @protected
5371 * @param {OO.ui.TagItemWidget} [item] Tag item
5372 * @return {OO.ui.Widget} The previous widget available.
5373 */
5374 OO.ui.TagMultiselectWidget.prototype.getPreviousItem = function ( item ) {
5375 var itemIndex = this.items.indexOf( item );
5376
5377 if ( item === undefined || itemIndex === -1 ) {
5378 return this.items[ this.items.length - 1 ];
5379 }
5380
5381 if ( itemIndex === 0 ) {
5382 if ( this.hasInput ) {
5383 return this.input;
5384 } else {
5385 // Return the last item
5386 return this.items[ this.items.length - 1 ];
5387 }
5388 } else {
5389 return this.items[ itemIndex - 1 ];
5390 }
5391 };
5392
5393 /**
5394 * Update the dimensions of the text input field to encompass all available area.
5395 * This is especially relevant for when the input is at the edge of a line
5396 * and should get smaller. The usual operation (as an inline-block with min-width)
5397 * does not work in that case, pushing the input downwards to the next line.
5398 *
5399 * @private
5400 */
5401 OO.ui.TagMultiselectWidget.prototype.updateInputSize = function () {
5402 var $lastItem, direction, contentWidth, currentWidth, bestWidth;
5403 if ( this.inputPosition === 'inline' && !this.isDisabled() ) {
5404 if ( this.input.$input[ 0 ].scrollWidth === 0 ) {
5405 // Input appears to be attached but not visible.
5406 // Don't attempt to adjust its size, because our measurements
5407 // are going to fail anyway.
5408 return;
5409 }
5410 this.input.$input.css( 'width', '1em' );
5411 $lastItem = this.$group.children().last();
5412 direction = OO.ui.Element.static.getDir( this.$handle );
5413
5414 // Get the width of the input with the placeholder text as
5415 // the value and save it so that we don't keep recalculating
5416 if (
5417 this.contentWidthWithPlaceholder === undefined &&
5418 this.input.getValue() === '' &&
5419 this.input.$input.attr( 'placeholder' ) !== undefined
5420 ) {
5421 this.input.setValue( this.input.$input.attr( 'placeholder' ) );
5422 this.contentWidthWithPlaceholder = this.input.$input[ 0 ].scrollWidth;
5423 this.input.setValue( '' );
5424
5425 }
5426
5427 // Always keep the input wide enough for the placeholder text
5428 contentWidth = Math.max(
5429 this.input.$input[ 0 ].scrollWidth,
5430 // undefined arguments in Math.max lead to NaN
5431 ( this.contentWidthWithPlaceholder === undefined ) ?
5432 0 : this.contentWidthWithPlaceholder
5433 );
5434 currentWidth = this.input.$input.width();
5435
5436 if ( contentWidth < currentWidth ) {
5437 this.updateIfHeightChanged();
5438 // All is fine, don't perform expensive calculations
5439 return;
5440 }
5441
5442 if ( $lastItem.length === 0 ) {
5443 bestWidth = this.$content.innerWidth();
5444 } else {
5445 bestWidth = direction === 'ltr' ?
5446 this.$content.innerWidth() - $lastItem.position().left - $lastItem.outerWidth() :
5447 $lastItem.position().left;
5448 }
5449
5450 // Some safety margin for sanity, because I *really* don't feel like finding out where the few
5451 // pixels this is off by are coming from.
5452 bestWidth -= 10;
5453 if ( contentWidth > bestWidth ) {
5454 // This will result in the input getting shifted to the next line
5455 bestWidth = this.$content.innerWidth() - 10;
5456 }
5457 this.input.$input.width( Math.floor( bestWidth ) );
5458 this.updateIfHeightChanged();
5459 } else {
5460 this.updateIfHeightChanged();
5461 }
5462 };
5463
5464 /**
5465 * Determine if widget height changed, and if so,
5466 * emit the resize event. This is useful for when there are either
5467 * menus or popups attached to the bottom of the widget, to allow
5468 * them to change their positioning in case the widget moved down
5469 * or up.
5470 *
5471 * @private
5472 */
5473 OO.ui.TagMultiselectWidget.prototype.updateIfHeightChanged = function () {
5474 var height = this.$element.height();
5475 if ( height !== this.height ) {
5476 this.height = height;
5477 this.emit( 'resize' );
5478 }
5479 };
5480
5481 /**
5482 * Check whether all items in the widget are valid
5483 *
5484 * @return {boolean} Widget is valid
5485 */
5486 OO.ui.TagMultiselectWidget.prototype.checkValidity = function () {
5487 return this.getItems().every( function ( item ) {
5488 return item.isValid();
5489 } );
5490 };
5491
5492 /**
5493 * Set the valid state of this item
5494 *
5495 * @param {boolean} [valid] Item is valid
5496 * @fires valid
5497 */
5498 OO.ui.TagMultiselectWidget.prototype.toggleValid = function ( valid ) {
5499 valid = valid === undefined ? !this.valid : !!valid;
5500
5501 if ( this.valid !== valid ) {
5502 this.valid = valid;
5503
5504 this.setFlags( { invalid: !this.valid } );
5505
5506 this.emit( 'valid', this.valid );
5507 }
5508 };
5509
5510 /**
5511 * Get the current valid state of the widget
5512 *
5513 * @return {boolean} Widget is valid
5514 */
5515 OO.ui.TagMultiselectWidget.prototype.isValid = function () {
5516 return this.valid;
5517 };
5518
5519 /**
5520 * PopupTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5521 * to use a popup. The popup can be configured to have a default input to insert values into the widget.
5522 *
5523 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
5524 *
5525 * @example
5526 * // Example: A basic PopupTagMultiselectWidget.
5527 * var widget = new OO.ui.PopupTagMultiselectWidget();
5528 * $( 'body' ).append( widget.$element );
5529 *
5530 * // Example: A PopupTagMultiselectWidget with an external popup.
5531 * var popupInput = new OO.ui.TextInputWidget(),
5532 * widget = new OO.ui.PopupTagMultiselectWidget( {
5533 * popupInput: popupInput,
5534 * popup: {
5535 * $content: popupInput.$element
5536 * }
5537 * } );
5538 * $( 'body' ).append( widget.$element );
5539 *
5540 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
5541 *
5542 * @class
5543 * @extends OO.ui.TagMultiselectWidget
5544 * @mixins OO.ui.mixin.PopupElement
5545 *
5546 * @param {Object} config Configuration object
5547 * @cfg {jQuery} [$overlay] An overlay for the popup.
5548 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
5549 * @cfg {Object} [popup] Configuration options for the popup
5550 * @cfg {OO.ui.InputWidget} [popupInput] An input widget inside the popup that will be
5551 * focused when the popup is opened and will be used as replacement for the
5552 * general input in the widget.
5553 */
5554 OO.ui.PopupTagMultiselectWidget = function OoUiPopupTagMultiselectWidget( config ) {
5555 var defaultInput,
5556 defaultConfig = { popup: {} };
5557
5558 config = config || {};
5559
5560 // Parent constructor
5561 OO.ui.PopupTagMultiselectWidget.parent.call( this, $.extend( { inputPosition: 'none' }, config ) );
5562
5563 this.$overlay = config.$overlay || this.$element;
5564
5565 if ( !config.popup ) {
5566 // For the default base implementation, we give a popup
5567 // with an input widget inside it. For any other use cases
5568 // the popup needs to be populated externally and the
5569 // event handled to add tags separately and manually
5570 defaultInput = new OO.ui.TextInputWidget();
5571
5572 defaultConfig.popupInput = defaultInput;
5573 defaultConfig.popup.$content = defaultInput.$element;
5574
5575 this.$element.addClass( 'oo-ui-popupTagMultiselectWidget-defaultPopup' );
5576 }
5577
5578 // Add overlay, and add that to the autoCloseIgnore
5579 defaultConfig.popup.$overlay = this.$overlay;
5580 defaultConfig.popup.$autoCloseIgnore = this.hasInput ?
5581 this.input.$element.add( this.$overlay ) : this.$overlay;
5582
5583 // Allow extending any of the above
5584 config = $.extend( defaultConfig, config );
5585
5586 // Mixin constructors
5587 OO.ui.mixin.PopupElement.call( this, config );
5588
5589 if ( this.hasInput ) {
5590 this.input.$input.on( 'focus', this.popup.toggle.bind( this.popup, true ) );
5591 }
5592
5593 // Configuration options
5594 this.popupInput = config.popupInput;
5595 if ( this.popupInput ) {
5596 this.popupInput.connect( this, {
5597 enter: 'onPopupInputEnter'
5598 } );
5599 }
5600
5601 // Events
5602 this.on( 'resize', this.popup.updateDimensions.bind( this.popup ) );
5603 this.popup.connect( this, { toggle: 'onPopupToggle' } );
5604 this.$tabIndexed
5605 .on( 'focus', this.onFocus.bind( this ) );
5606
5607 // Initialize
5608 this.$element
5609 .append( this.popup.$element )
5610 .addClass( 'oo-ui-popupTagMultiselectWidget' );
5611 };
5612
5613 /* Initialization */
5614
5615 OO.inheritClass( OO.ui.PopupTagMultiselectWidget, OO.ui.TagMultiselectWidget );
5616 OO.mixinClass( OO.ui.PopupTagMultiselectWidget, OO.ui.mixin.PopupElement );
5617
5618 /* Methods */
5619
5620 /**
5621 * Focus event handler.
5622 *
5623 * @private
5624 */
5625 OO.ui.PopupTagMultiselectWidget.prototype.onFocus = function () {
5626 this.popup.toggle( true );
5627 };
5628
5629 /**
5630 * Respond to popup toggle event
5631 *
5632 * @param {boolean} isVisible Popup is visible
5633 */
5634 OO.ui.PopupTagMultiselectWidget.prototype.onPopupToggle = function ( isVisible ) {
5635 if ( isVisible && this.popupInput ) {
5636 this.popupInput.focus();
5637 }
5638 };
5639
5640 /**
5641 * Respond to popup input enter event
5642 */
5643 OO.ui.PopupTagMultiselectWidget.prototype.onPopupInputEnter = function () {
5644 if ( this.popupInput ) {
5645 this.addTagByPopupValue( this.popupInput.getValue() );
5646 this.popupInput.setValue( '' );
5647 }
5648 };
5649
5650 /**
5651 * @inheritdoc
5652 */
5653 OO.ui.PopupTagMultiselectWidget.prototype.onTagSelect = function ( item ) {
5654 if ( this.popupInput && this.allowEditTags ) {
5655 this.popupInput.setValue( item.getData() );
5656 this.removeItems( [ item ] );
5657
5658 this.popup.toggle( true );
5659 this.popupInput.focus();
5660 } else {
5661 // Parent
5662 OO.ui.PopupTagMultiselectWidget.parent.prototype.onTagSelect.call( this, item );
5663 }
5664 };
5665
5666 /**
5667 * Add a tag by the popup value.
5668 * Whatever is responsible for setting the value in the popup should call
5669 * this method to add a tag, or use the regular methods like #addTag or
5670 * #setValue directly.
5671 *
5672 * @param {string} data The value of item
5673 * @param {string} [label] The label of the tag. If not given, the data is used.
5674 */
5675 OO.ui.PopupTagMultiselectWidget.prototype.addTagByPopupValue = function ( data, label ) {
5676 this.addTag( data, label );
5677 };
5678
5679 /**
5680 * MenuTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
5681 * to use a menu of selectable options.
5682 *
5683 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
5684 *
5685 * @example
5686 * // Example: A basic MenuTagMultiselectWidget.
5687 * var widget = new OO.ui.MenuTagMultiselectWidget( {
5688 * inputPosition: 'outline',
5689 * options: [
5690 * { data: 'option1', label: 'Option 1' },
5691 * { data: 'option2', label: 'Option 2' },
5692 * { data: 'option3', label: 'Option 3' },
5693 * ],
5694 * selected: [ 'option1', 'option2' ]
5695 * } );
5696 * $( 'body' ).append( widget.$element );
5697 *
5698 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
5699 *
5700 * @class
5701 * @extends OO.ui.TagMultiselectWidget
5702 *
5703 * @constructor
5704 * @param {Object} [config] Configuration object
5705 * @cfg {Object} [menu] Configuration object for the menu widget
5706 * @cfg {jQuery} [$overlay] An overlay for the menu.
5707 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
5708 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
5709 */
5710 OO.ui.MenuTagMultiselectWidget = function OoUiMenuTagMultiselectWidget( config ) {
5711 config = config || {};
5712
5713 // Parent constructor
5714 OO.ui.MenuTagMultiselectWidget.parent.call( this, config );
5715
5716 this.$overlay = config.$overlay || this.$element;
5717
5718 this.menu = this.createMenuWidget( $.extend( {
5719 widget: this,
5720 input: this.hasInput ? this.input : null,
5721 $input: this.hasInput ? this.input.$input : null,
5722 filterFromInput: !!this.hasInput,
5723 $autoCloseIgnore: this.hasInput ?
5724 this.input.$element.add( this.$overlay ) : this.$overlay,
5725 $floatableContainer: this.hasInput && this.inputPosition === 'outline' ?
5726 this.input.$element : this.$element,
5727 $overlay: this.$overlay,
5728 disabled: this.isDisabled()
5729 }, config.menu ) );
5730 this.addOptions( config.options || [] );
5731
5732 // Events
5733 this.menu.connect( this, {
5734 choose: 'onMenuChoose',
5735 toggle: 'onMenuToggle'
5736 } );
5737 if ( this.hasInput ) {
5738 this.input.connect( this, { change: 'onInputChange' } );
5739 }
5740 this.connect( this, { resize: 'onResize' } );
5741
5742 // Initialization
5743 this.$overlay
5744 .append( this.menu.$element );
5745 this.$element
5746 .addClass( 'oo-ui-menuTagMultiselectWidget' );
5747 // TagMultiselectWidget already does this, but it doesn't work right because this.menu is not yet
5748 // set up while the parent constructor runs, and #getAllowedValues rejects everything.
5749 if ( config.selected ) {
5750 this.setValue( config.selected );
5751 }
5752 };
5753
5754 /* Initialization */
5755
5756 OO.inheritClass( OO.ui.MenuTagMultiselectWidget, OO.ui.TagMultiselectWidget );
5757
5758 /* Methods */
5759
5760 /**
5761 * Respond to resize event
5762 */
5763 OO.ui.MenuTagMultiselectWidget.prototype.onResize = function () {
5764 // Reposition the menu
5765 this.menu.position();
5766 };
5767
5768 /**
5769 * @inheritdoc
5770 */
5771 OO.ui.MenuTagMultiselectWidget.prototype.onInputFocus = function () {
5772 // Parent method
5773 OO.ui.MenuTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
5774
5775 this.menu.toggle( true );
5776 };
5777
5778 /**
5779 * Respond to input change event
5780 */
5781 OO.ui.MenuTagMultiselectWidget.prototype.onInputChange = function () {
5782 this.menu.toggle( true );
5783 };
5784
5785 /**
5786 * Respond to menu choose event
5787 *
5788 * @param {OO.ui.OptionWidget} menuItem Chosen menu item
5789 */
5790 OO.ui.MenuTagMultiselectWidget.prototype.onMenuChoose = function ( menuItem ) {
5791 // Add tag
5792 this.addTag( menuItem.getData(), menuItem.getLabel() );
5793 };
5794
5795 /**
5796 * Respond to menu toggle event. Reset item highlights on hide.
5797 *
5798 * @param {boolean} isVisible The menu is visible
5799 */
5800 OO.ui.MenuTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
5801 if ( !isVisible ) {
5802 this.menu.selectItem( null );
5803 this.menu.highlightItem( null );
5804 }
5805 };
5806
5807 /**
5808 * @inheritdoc
5809 */
5810 OO.ui.MenuTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
5811 var menuItem = this.menu.getItemFromData( tagItem.getData() );
5812 // Override the base behavior from TagMultiselectWidget; the base behavior
5813 // in TagMultiselectWidget is to remove the tag to edit it in the input,
5814 // but in our case, we want to utilize the menu selection behavior, and
5815 // definitely not remove the item.
5816
5817 // Select the menu item
5818 this.menu.selectItem( menuItem );
5819
5820 this.focus();
5821 };
5822
5823 /**
5824 * @inheritdoc
5825 */
5826 OO.ui.MenuTagMultiselectWidget.prototype.addTagFromInput = function () {
5827 var inputValue = this.input.getValue(),
5828 validated = false,
5829 highlightedItem = this.menu.getHighlightedItem(),
5830 item = this.menu.getItemFromData( inputValue );
5831
5832 // Override the parent method so we add from the menu
5833 // rather than directly from the input
5834
5835 // Look for a highlighted item first
5836 if ( highlightedItem ) {
5837 validated = this.addTag( highlightedItem.getData(), highlightedItem.getLabel() );
5838 } else if ( item ) {
5839 // Look for the element that fits the data
5840 validated = this.addTag( item.getData(), item.getLabel() );
5841 } else {
5842 // Otherwise, add the tag - the method will only add if the
5843 // tag is valid or if invalid tags are allowed
5844 validated = this.addTag( inputValue );
5845 }
5846
5847 if ( validated ) {
5848 this.clearInput();
5849 this.focus();
5850 }
5851 };
5852
5853 /**
5854 * Return the visible items in the menu. This is mainly used for when
5855 * the menu is filtering results.
5856 *
5857 * @return {OO.ui.MenuOptionWidget[]} Visible results
5858 */
5859 OO.ui.MenuTagMultiselectWidget.prototype.getMenuVisibleItems = function () {
5860 return this.menu.getItems().filter( function ( menuItem ) {
5861 return menuItem.isVisible();
5862 } );
5863 };
5864
5865 /**
5866 * Create the menu for this widget. This is in a separate method so that
5867 * child classes can override this without polluting the constructor with
5868 * unnecessary extra objects that will be overidden.
5869 *
5870 * @param {Object} menuConfig Configuration options
5871 * @return {OO.ui.MenuSelectWidget} Menu widget
5872 */
5873 OO.ui.MenuTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
5874 return new OO.ui.MenuSelectWidget( menuConfig );
5875 };
5876
5877 /**
5878 * Add options to the menu
5879 *
5880 * @param {Object[]} menuOptions Object defining options
5881 */
5882 OO.ui.MenuTagMultiselectWidget.prototype.addOptions = function ( menuOptions ) {
5883 var widget = this,
5884 items = menuOptions.map( function ( obj ) {
5885 return widget.createMenuOptionWidget( obj.data, obj.label );
5886 } );
5887
5888 this.menu.addItems( items );
5889 };
5890
5891 /**
5892 * Create a menu option widget.
5893 *
5894 * @param {string} data Item data
5895 * @param {string} [label] Item label
5896 * @return {OO.ui.OptionWidget} Option widget
5897 */
5898 OO.ui.MenuTagMultiselectWidget.prototype.createMenuOptionWidget = function ( data, label ) {
5899 return new OO.ui.MenuOptionWidget( {
5900 data: data,
5901 label: label || data
5902 } );
5903 };
5904
5905 /**
5906 * Get the menu
5907 *
5908 * @return {OO.ui.MenuSelectWidget} Menu
5909 */
5910 OO.ui.MenuTagMultiselectWidget.prototype.getMenu = function () {
5911 return this.menu;
5912 };
5913
5914 /**
5915 * Get the allowed values list
5916 *
5917 * @return {string[]} Allowed data values
5918 */
5919 OO.ui.MenuTagMultiselectWidget.prototype.getAllowedValues = function () {
5920 var menuDatas = [];
5921 if ( this.menu ) {
5922 // If the parent constructor is calling us, we're not ready yet, this.menu is not set up.
5923 menuDatas = this.menu.getItems().map( function ( menuItem ) {
5924 return menuItem.getData();
5925 } );
5926 }
5927 return this.allowedValues.concat( menuDatas );
5928 };
5929
5930 /**
5931 * SelectFileWidgets allow for selecting files, using the HTML5 File API. These
5932 * widgets can be configured with {@link OO.ui.mixin.IconElement icons} and {@link
5933 * OO.ui.mixin.IndicatorElement indicators}.
5934 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
5935 *
5936 * @example
5937 * // Example of a file select widget
5938 * var selectFile = new OO.ui.SelectFileWidget();
5939 * $( 'body' ).append( selectFile.$element );
5940 *
5941 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets
5942 *
5943 * @class
5944 * @extends OO.ui.Widget
5945 * @mixins OO.ui.mixin.IconElement
5946 * @mixins OO.ui.mixin.IndicatorElement
5947 * @mixins OO.ui.mixin.PendingElement
5948 * @mixins OO.ui.mixin.LabelElement
5949 *
5950 * @constructor
5951 * @param {Object} [config] Configuration options
5952 * @cfg {string[]|null} [accept=null] MIME types to accept. null accepts all types.
5953 * @cfg {string} [placeholder] Text to display when no file is selected.
5954 * @cfg {string} [notsupported] Text to display when file support is missing in the browser.
5955 * @cfg {boolean} [droppable=true] Whether to accept files by drag and drop.
5956 * @cfg {boolean} [showDropTarget=false] Whether to show a drop target. Requires droppable to be true.
5957 * @cfg {number} [thumbnailSizeLimit=20] File size limit in MiB above which to not try and show a
5958 * preview (for performance)
5959 */
5960 OO.ui.SelectFileWidget = function OoUiSelectFileWidget( config ) {
5961 var dragHandler;
5962
5963 // Configuration initialization
5964 config = $.extend( {
5965 accept: null,
5966 placeholder: OO.ui.msg( 'ooui-selectfile-placeholder' ),
5967 notsupported: OO.ui.msg( 'ooui-selectfile-not-supported' ),
5968 droppable: true,
5969 showDropTarget: false,
5970 thumbnailSizeLimit: 20
5971 }, config );
5972
5973 // Parent constructor
5974 OO.ui.SelectFileWidget.parent.call( this, config );
5975
5976 // Mixin constructors
5977 OO.ui.mixin.IconElement.call( this, config );
5978 OO.ui.mixin.IndicatorElement.call( this, config );
5979 OO.ui.mixin.PendingElement.call( this, $.extend( {}, config, { $pending: this.$info } ) );
5980 OO.ui.mixin.LabelElement.call( this, config );
5981
5982 // Properties
5983 this.$info = $( '<span>' );
5984 this.showDropTarget = config.showDropTarget;
5985 this.thumbnailSizeLimit = config.thumbnailSizeLimit;
5986 this.isSupported = this.constructor.static.isSupported();
5987 this.currentFile = null;
5988 if ( Array.isArray( config.accept ) ) {
5989 this.accept = config.accept;
5990 } else {
5991 this.accept = null;
5992 }
5993 this.placeholder = config.placeholder;
5994 this.notsupported = config.notsupported;
5995 this.onFileSelectedHandler = this.onFileSelected.bind( this );
5996
5997 this.selectButton = new OO.ui.ButtonWidget( {
5998 classes: [ 'oo-ui-selectFileWidget-selectButton' ],
5999 label: OO.ui.msg( 'ooui-selectfile-button-select' ),
6000 disabled: this.disabled || !this.isSupported
6001 } );
6002
6003 this.clearButton = new OO.ui.ButtonWidget( {
6004 classes: [ 'oo-ui-selectFileWidget-clearButton' ],
6005 framed: false,
6006 icon: 'close',
6007 disabled: this.disabled
6008 } );
6009
6010 // Events
6011 this.selectButton.$button.on( {
6012 keypress: this.onKeyPress.bind( this )
6013 } );
6014 this.clearButton.connect( this, {
6015 click: 'onClearClick'
6016 } );
6017 if ( config.droppable ) {
6018 dragHandler = this.onDragEnterOrOver.bind( this );
6019 this.$element.on( {
6020 dragenter: dragHandler,
6021 dragover: dragHandler,
6022 dragleave: this.onDragLeave.bind( this ),
6023 drop: this.onDrop.bind( this )
6024 } );
6025 }
6026
6027 // Initialization
6028 this.addInput();
6029 this.$label.addClass( 'oo-ui-selectFileWidget-label' );
6030 this.$info
6031 .addClass( 'oo-ui-selectFileWidget-info' )
6032 .append( this.$icon, this.$label, this.clearButton.$element, this.$indicator );
6033
6034 if ( config.droppable && config.showDropTarget ) {
6035 this.selectButton.setIcon( 'upload' );
6036 this.$thumbnail = $( '<div>' ).addClass( 'oo-ui-selectFileWidget-thumbnail' );
6037 this.setPendingElement( this.$thumbnail );
6038 this.$element
6039 .addClass( 'oo-ui-selectFileWidget-dropTarget oo-ui-selectFileWidget' )
6040 .on( {
6041 click: this.onDropTargetClick.bind( this )
6042 } )
6043 .append(
6044 this.$thumbnail,
6045 this.$info,
6046 this.selectButton.$element,
6047 $( '<span>' )
6048 .addClass( 'oo-ui-selectFileWidget-dropLabel' )
6049 .text( OO.ui.msg( 'ooui-selectfile-dragdrop-placeholder' ) )
6050 );
6051 } else {
6052 this.$element
6053 .addClass( 'oo-ui-selectFileWidget' )
6054 .append( this.$info, this.selectButton.$element );
6055 }
6056 this.updateUI();
6057 };
6058
6059 /* Setup */
6060
6061 OO.inheritClass( OO.ui.SelectFileWidget, OO.ui.Widget );
6062 OO.mixinClass( OO.ui.SelectFileWidget, OO.ui.mixin.IconElement );
6063 OO.mixinClass( OO.ui.SelectFileWidget, OO.ui.mixin.IndicatorElement );
6064 OO.mixinClass( OO.ui.SelectFileWidget, OO.ui.mixin.PendingElement );
6065 OO.mixinClass( OO.ui.SelectFileWidget, OO.ui.mixin.LabelElement );
6066
6067 /* Static Properties */
6068
6069 /**
6070 * Check if this widget is supported
6071 *
6072 * @static
6073 * @return {boolean}
6074 */
6075 OO.ui.SelectFileWidget.static.isSupported = function () {
6076 var $input;
6077 if ( OO.ui.SelectFileWidget.static.isSupportedCache === null ) {
6078 $input = $( '<input>' ).attr( 'type', 'file' );
6079 OO.ui.SelectFileWidget.static.isSupportedCache = $input[ 0 ].files !== undefined;
6080 }
6081 return OO.ui.SelectFileWidget.static.isSupportedCache;
6082 };
6083
6084 OO.ui.SelectFileWidget.static.isSupportedCache = null;
6085
6086 /* Events */
6087
6088 /**
6089 * @event change
6090 *
6091 * A change event is emitted when the on/off state of the toggle changes.
6092 *
6093 * @param {File|null} value New value
6094 */
6095
6096 /* Methods */
6097
6098 /**
6099 * Get the current value of the field
6100 *
6101 * @return {File|null}
6102 */
6103 OO.ui.SelectFileWidget.prototype.getValue = function () {
6104 return this.currentFile;
6105 };
6106
6107 /**
6108 * Set the current value of the field
6109 *
6110 * @param {File|null} file File to select
6111 */
6112 OO.ui.SelectFileWidget.prototype.setValue = function ( file ) {
6113 if ( this.currentFile !== file ) {
6114 this.currentFile = file;
6115 this.updateUI();
6116 this.emit( 'change', this.currentFile );
6117 }
6118 };
6119
6120 /**
6121 * Focus the widget.
6122 *
6123 * Focusses the select file button.
6124 *
6125 * @chainable
6126 */
6127 OO.ui.SelectFileWidget.prototype.focus = function () {
6128 this.selectButton.focus();
6129 return this;
6130 };
6131
6132 /**
6133 * Blur the widget.
6134 *
6135 * @chainable
6136 */
6137 OO.ui.SelectFileWidget.prototype.blur = function () {
6138 this.selectButton.blur();
6139 return this;
6140 };
6141
6142 /**
6143 * @inheritdoc
6144 */
6145 OO.ui.SelectFileWidget.prototype.simulateLabelClick = function () {
6146 this.focus();
6147 };
6148
6149 /**
6150 * Update the user interface when a file is selected or unselected
6151 *
6152 * @protected
6153 */
6154 OO.ui.SelectFileWidget.prototype.updateUI = function () {
6155 var $label;
6156 if ( !this.isSupported ) {
6157 this.$element.addClass( 'oo-ui-selectFileWidget-notsupported' );
6158 this.$element.removeClass( 'oo-ui-selectFileWidget-empty' );
6159 this.setLabel( this.notsupported );
6160 } else {
6161 this.$element.addClass( 'oo-ui-selectFileWidget-supported' );
6162 if ( this.currentFile ) {
6163 this.$element.removeClass( 'oo-ui-selectFileWidget-empty' );
6164 $label = $( [] );
6165 $label = $label.add(
6166 $( '<span>' )
6167 .addClass( 'oo-ui-selectFileWidget-fileName' )
6168 .text( this.currentFile.name )
6169 );
6170 this.setLabel( $label );
6171
6172 if ( this.showDropTarget ) {
6173 this.pushPending();
6174 this.loadAndGetImageUrl().done( function ( url ) {
6175 this.$thumbnail.css( 'background-image', 'url( ' + url + ' )' );
6176 }.bind( this ) ).fail( function () {
6177 this.$thumbnail.append(
6178 new OO.ui.IconWidget( {
6179 icon: 'attachment',
6180 classes: [ 'oo-ui-selectFileWidget-noThumbnail-icon' ]
6181 } ).$element
6182 );
6183 }.bind( this ) ).always( function () {
6184 this.popPending();
6185 }.bind( this ) );
6186 this.$element.off( 'click' );
6187 }
6188 } else {
6189 if ( this.showDropTarget ) {
6190 this.$element.off( 'click' );
6191 this.$element.on( {
6192 click: this.onDropTargetClick.bind( this )
6193 } );
6194 this.$thumbnail
6195 .empty()
6196 .css( 'background-image', '' );
6197 }
6198 this.$element.addClass( 'oo-ui-selectFileWidget-empty' );
6199 this.setLabel( this.placeholder );
6200 }
6201 }
6202 };
6203
6204 /**
6205 * If the selected file is an image, get its URL and load it.
6206 *
6207 * @return {jQuery.Promise} Promise resolves with the image URL after it has loaded
6208 */
6209 OO.ui.SelectFileWidget.prototype.loadAndGetImageUrl = function () {
6210 var deferred = $.Deferred(),
6211 file = this.currentFile,
6212 reader = new FileReader();
6213
6214 if (
6215 file &&
6216 ( OO.getProp( file, 'type' ) || '' ).indexOf( 'image/' ) === 0 &&
6217 file.size < this.thumbnailSizeLimit * 1024 * 1024
6218 ) {
6219 reader.onload = function ( event ) {
6220 var img = document.createElement( 'img' );
6221 img.addEventListener( 'load', function () {
6222 if (
6223 img.naturalWidth === 0 ||
6224 img.naturalHeight === 0 ||
6225 img.complete === false
6226 ) {
6227 deferred.reject();
6228 } else {
6229 deferred.resolve( event.target.result );
6230 }
6231 } );
6232 img.src = event.target.result;
6233 };
6234 reader.readAsDataURL( file );
6235 } else {
6236 deferred.reject();
6237 }
6238
6239 return deferred.promise();
6240 };
6241
6242 /**
6243 * Add the input to the widget
6244 *
6245 * @private
6246 */
6247 OO.ui.SelectFileWidget.prototype.addInput = function () {
6248 if ( this.$input ) {
6249 this.$input.remove();
6250 }
6251
6252 if ( !this.isSupported ) {
6253 this.$input = null;
6254 return;
6255 }
6256
6257 this.$input = $( '<input>' ).attr( 'type', 'file' );
6258 this.$input.on( 'change', this.onFileSelectedHandler );
6259 this.$input.on( 'click', function ( e ) {
6260 // Prevents dropTarget to get clicked which calls
6261 // a click on this input
6262 e.stopPropagation();
6263 } );
6264 this.$input.attr( {
6265 tabindex: -1
6266 } );
6267 if ( this.accept ) {
6268 this.$input.attr( 'accept', this.accept.join( ', ' ) );
6269 }
6270 this.selectButton.$button.append( this.$input );
6271 };
6272
6273 /**
6274 * Determine if we should accept this file
6275 *
6276 * @private
6277 * @param {string} mimeType File MIME type
6278 * @return {boolean}
6279 */
6280 OO.ui.SelectFileWidget.prototype.isAllowedType = function ( mimeType ) {
6281 var i, mimeTest;
6282
6283 if ( !this.accept || !mimeType ) {
6284 return true;
6285 }
6286
6287 for ( i = 0; i < this.accept.length; i++ ) {
6288 mimeTest = this.accept[ i ];
6289 if ( mimeTest === mimeType ) {
6290 return true;
6291 } else if ( mimeTest.substr( -2 ) === '/*' ) {
6292 mimeTest = mimeTest.substr( 0, mimeTest.length - 1 );
6293 if ( mimeType.substr( 0, mimeTest.length ) === mimeTest ) {
6294 return true;
6295 }
6296 }
6297 }
6298
6299 return false;
6300 };
6301
6302 /**
6303 * Handle file selection from the input
6304 *
6305 * @private
6306 * @param {jQuery.Event} e
6307 */
6308 OO.ui.SelectFileWidget.prototype.onFileSelected = function ( e ) {
6309 var file = OO.getProp( e.target, 'files', 0 ) || null;
6310
6311 if ( file && !this.isAllowedType( file.type ) ) {
6312 file = null;
6313 }
6314
6315 this.setValue( file );
6316 this.addInput();
6317 };
6318
6319 /**
6320 * Handle clear button click events.
6321 *
6322 * @private
6323 */
6324 OO.ui.SelectFileWidget.prototype.onClearClick = function () {
6325 this.setValue( null );
6326 return false;
6327 };
6328
6329 /**
6330 * Handle key press events.
6331 *
6332 * @private
6333 * @param {jQuery.Event} e Key press event
6334 */
6335 OO.ui.SelectFileWidget.prototype.onKeyPress = function ( e ) {
6336 if ( this.isSupported && !this.isDisabled() && this.$input &&
6337 ( e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
6338 ) {
6339 this.$input.click();
6340 return false;
6341 }
6342 };
6343
6344 /**
6345 * Handle drop target click events.
6346 *
6347 * @private
6348 * @param {jQuery.Event} e Key press event
6349 */
6350 OO.ui.SelectFileWidget.prototype.onDropTargetClick = function () {
6351 if ( this.isSupported && !this.isDisabled() && this.$input ) {
6352 this.$input.click();
6353 return false;
6354 }
6355 };
6356
6357 /**
6358 * Handle drag enter and over events
6359 *
6360 * @private
6361 * @param {jQuery.Event} e Drag event
6362 */
6363 OO.ui.SelectFileWidget.prototype.onDragEnterOrOver = function ( e ) {
6364 var itemOrFile,
6365 droppableFile = false,
6366 dt = e.originalEvent.dataTransfer;
6367
6368 e.preventDefault();
6369 e.stopPropagation();
6370
6371 if ( this.isDisabled() || !this.isSupported ) {
6372 this.$element.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6373 dt.dropEffect = 'none';
6374 return false;
6375 }
6376
6377 // DataTransferItem and File both have a type property, but in Chrome files
6378 // have no information at this point.
6379 itemOrFile = OO.getProp( dt, 'items', 0 ) || OO.getProp( dt, 'files', 0 );
6380 if ( itemOrFile ) {
6381 if ( this.isAllowedType( itemOrFile.type ) ) {
6382 droppableFile = true;
6383 }
6384 // dt.types is Array-like, but not an Array
6385 } else if ( Array.prototype.indexOf.call( OO.getProp( dt, 'types' ) || [], 'Files' ) !== -1 ) {
6386 // File information is not available at this point for security so just assume
6387 // it is acceptable for now.
6388 // https://bugzilla.mozilla.org/show_bug.cgi?id=640534
6389 droppableFile = true;
6390 }
6391
6392 this.$element.toggleClass( 'oo-ui-selectFileWidget-canDrop', droppableFile );
6393 if ( !droppableFile ) {
6394 dt.dropEffect = 'none';
6395 }
6396
6397 return false;
6398 };
6399
6400 /**
6401 * Handle drag leave events
6402 *
6403 * @private
6404 * @param {jQuery.Event} e Drag event
6405 */
6406 OO.ui.SelectFileWidget.prototype.onDragLeave = function () {
6407 this.$element.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6408 };
6409
6410 /**
6411 * Handle drop events
6412 *
6413 * @private
6414 * @param {jQuery.Event} e Drop event
6415 */
6416 OO.ui.SelectFileWidget.prototype.onDrop = function ( e ) {
6417 var file = null,
6418 dt = e.originalEvent.dataTransfer;
6419
6420 e.preventDefault();
6421 e.stopPropagation();
6422 this.$element.removeClass( 'oo-ui-selectFileWidget-canDrop' );
6423
6424 if ( this.isDisabled() || !this.isSupported ) {
6425 return false;
6426 }
6427
6428 file = OO.getProp( dt, 'files', 0 );
6429 if ( file && !this.isAllowedType( file.type ) ) {
6430 file = null;
6431 }
6432 if ( file ) {
6433 this.setValue( file );
6434 }
6435
6436 return false;
6437 };
6438
6439 /**
6440 * @inheritdoc
6441 */
6442 OO.ui.SelectFileWidget.prototype.setDisabled = function ( disabled ) {
6443 OO.ui.SelectFileWidget.parent.prototype.setDisabled.call( this, disabled );
6444 if ( this.selectButton ) {
6445 this.selectButton.setDisabled( disabled );
6446 }
6447 if ( this.clearButton ) {
6448 this.clearButton.setDisabled( disabled );
6449 }
6450 return this;
6451 };
6452
6453 /**
6454 * SearchWidgets combine a {@link OO.ui.TextInputWidget text input field}, where users can type a search query,
6455 * and a menu of search results, which is displayed beneath the query
6456 * field. Unlike {@link OO.ui.mixin.LookupElement lookup menus}, search result menus are always visible to the user.
6457 * Users can choose an item from the menu or type a query into the text field to search for a matching result item.
6458 * In general, search widgets are used inside a separate {@link OO.ui.Dialog dialog} window.
6459 *
6460 * Each time the query is changed, the search result menu is cleared and repopulated. Please see
6461 * the [OOjs UI demos][1] for an example.
6462 *
6463 * [1]: https://tools.wmflabs.org/oojs-ui/oojs-ui/demos/#dialogs-mediawiki-vector-ltr
6464 *
6465 * @class
6466 * @extends OO.ui.Widget
6467 *
6468 * @constructor
6469 * @param {Object} [config] Configuration options
6470 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
6471 * @cfg {string} [value] Initial query value
6472 */
6473 OO.ui.SearchWidget = function OoUiSearchWidget( config ) {
6474 // Configuration initialization
6475 config = config || {};
6476
6477 // Parent constructor
6478 OO.ui.SearchWidget.parent.call( this, config );
6479
6480 // Properties
6481 this.query = new OO.ui.TextInputWidget( {
6482 icon: 'search',
6483 placeholder: config.placeholder,
6484 value: config.value
6485 } );
6486 this.results = new OO.ui.SelectWidget();
6487 this.$query = $( '<div>' );
6488 this.$results = $( '<div>' );
6489
6490 // Events
6491 this.query.connect( this, {
6492 change: 'onQueryChange',
6493 enter: 'onQueryEnter'
6494 } );
6495 this.query.$input.on( 'keydown', this.onQueryKeydown.bind( this ) );
6496
6497 // Initialization
6498 this.$query
6499 .addClass( 'oo-ui-searchWidget-query' )
6500 .append( this.query.$element );
6501 this.$results
6502 .addClass( 'oo-ui-searchWidget-results' )
6503 .append( this.results.$element );
6504 this.$element
6505 .addClass( 'oo-ui-searchWidget' )
6506 .append( this.$results, this.$query );
6507 };
6508
6509 /* Setup */
6510
6511 OO.inheritClass( OO.ui.SearchWidget, OO.ui.Widget );
6512
6513 /* Methods */
6514
6515 /**
6516 * Handle query key down events.
6517 *
6518 * @private
6519 * @param {jQuery.Event} e Key down event
6520 */
6521 OO.ui.SearchWidget.prototype.onQueryKeydown = function ( e ) {
6522 var highlightedItem, nextItem,
6523 dir = e.which === OO.ui.Keys.DOWN ? 1 : ( e.which === OO.ui.Keys.UP ? -1 : 0 );
6524
6525 if ( dir ) {
6526 highlightedItem = this.results.getHighlightedItem();
6527 if ( !highlightedItem ) {
6528 highlightedItem = this.results.getSelectedItem();
6529 }
6530 nextItem = this.results.getRelativeSelectableItem( highlightedItem, dir );
6531 this.results.highlightItem( nextItem );
6532 nextItem.scrollElementIntoView();
6533 }
6534 };
6535
6536 /**
6537 * Handle select widget select events.
6538 *
6539 * Clears existing results. Subclasses should repopulate items according to new query.
6540 *
6541 * @private
6542 * @param {string} value New value
6543 */
6544 OO.ui.SearchWidget.prototype.onQueryChange = function () {
6545 // Reset
6546 this.results.clearItems();
6547 };
6548
6549 /**
6550 * Handle select widget enter key events.
6551 *
6552 * Chooses highlighted item.
6553 *
6554 * @private
6555 * @param {string} value New value
6556 */
6557 OO.ui.SearchWidget.prototype.onQueryEnter = function () {
6558 var highlightedItem = this.results.getHighlightedItem();
6559 if ( highlightedItem ) {
6560 this.results.chooseItem( highlightedItem );
6561 }
6562 };
6563
6564 /**
6565 * Get the query input.
6566 *
6567 * @return {OO.ui.TextInputWidget} Query input
6568 */
6569 OO.ui.SearchWidget.prototype.getQuery = function () {
6570 return this.query;
6571 };
6572
6573 /**
6574 * Get the search results menu.
6575 *
6576 * @return {OO.ui.SelectWidget} Menu of search results
6577 */
6578 OO.ui.SearchWidget.prototype.getResults = function () {
6579 return this.results;
6580 };
6581
6582 /**
6583 * NumberInputWidgets combine a {@link OO.ui.TextInputWidget text input} (where a value
6584 * can be entered manually) and two {@link OO.ui.ButtonWidget button widgets}
6585 * (to adjust the value in increments) to allow the user to enter a number.
6586 *
6587 * @example
6588 * // Example: A NumberInputWidget.
6589 * var numberInput = new OO.ui.NumberInputWidget( {
6590 * label: 'NumberInputWidget',
6591 * input: { value: 5 },
6592 * min: 1,
6593 * max: 10
6594 * } );
6595 * $( 'body' ).append( numberInput.$element );
6596 *
6597 * @class
6598 * @extends OO.ui.TextInputWidget
6599 *
6600 * @constructor
6601 * @param {Object} [config] Configuration options
6602 * @cfg {Object} [minusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget decrementing button widget}.
6603 * @cfg {Object} [plusButton] Configuration options to pass to the {@link OO.ui.ButtonWidget incrementing button widget}.
6604 * @cfg {boolean} [allowInteger=false] Whether the field accepts only integer values.
6605 * @cfg {number} [min=-Infinity] Minimum allowed value
6606 * @cfg {number} [max=Infinity] Maximum allowed value
6607 * @cfg {number} [step=1] Delta when using the buttons or up/down arrow keys
6608 * @cfg {number|null} [pageStep] Delta when using the page-up/page-down keys. Defaults to 10 times #step.
6609 * @cfg {boolean} [showButtons=true] Whether to show the plus and minus buttons.
6610 */
6611 OO.ui.NumberInputWidget = function OoUiNumberInputWidget( config ) {
6612 var $field = $( '<div>' )
6613 .addClass( 'oo-ui-numberInputWidget-field' );
6614
6615 // Configuration initialization
6616 config = $.extend( {
6617 allowInteger: false,
6618 min: -Infinity,
6619 max: Infinity,
6620 step: 1,
6621 pageStep: null,
6622 showButtons: true
6623 }, config );
6624
6625 // For backward compatibility
6626 $.extend( config, config.input );
6627 this.input = this;
6628
6629 // Parent constructor
6630 OO.ui.NumberInputWidget.parent.call( this, $.extend( config, {
6631 type: 'number'
6632 } ) );
6633
6634 if ( config.showButtons ) {
6635 this.minusButton = new OO.ui.ButtonWidget( $.extend(
6636 {
6637 disabled: this.isDisabled(),
6638 tabIndex: -1,
6639 classes: [ 'oo-ui-numberInputWidget-minusButton' ],
6640 icon: 'subtract'
6641 },
6642 config.minusButton
6643 ) );
6644 this.plusButton = new OO.ui.ButtonWidget( $.extend(
6645 {
6646 disabled: this.isDisabled(),
6647 tabIndex: -1,
6648 classes: [ 'oo-ui-numberInputWidget-plusButton' ],
6649 icon: 'add'
6650 },
6651 config.plusButton
6652 ) );
6653 }
6654
6655 // Events
6656 this.$input.on( {
6657 keydown: this.onKeyDown.bind( this ),
6658 'wheel mousewheel DOMMouseScroll': this.onWheel.bind( this )
6659 } );
6660 if ( config.showButtons ) {
6661 this.plusButton.connect( this, {
6662 click: [ 'onButtonClick', +1 ]
6663 } );
6664 this.minusButton.connect( this, {
6665 click: [ 'onButtonClick', -1 ]
6666 } );
6667 }
6668
6669 // Build the field
6670 $field.append( this.$input );
6671 if ( config.showButtons ) {
6672 $field
6673 .prepend( this.minusButton.$element )
6674 .append( this.plusButton.$element );
6675 }
6676
6677 // Initialization
6678 this.setAllowInteger( config.allowInteger || config.isInteger );
6679 this.setRange( config.min, config.max );
6680 this.setStep( config.step, config.pageStep );
6681 // Set the validation method after we set allowInteger and range
6682 // so that it doesn't immediately call setValidityFlag
6683 this.setValidation( this.validateNumber.bind( this ) );
6684
6685 this.$element
6686 .addClass( 'oo-ui-numberInputWidget' )
6687 .toggleClass( 'oo-ui-numberInputWidget-buttoned', config.showButtons )
6688 .append( $field );
6689 };
6690
6691 /* Setup */
6692
6693 OO.inheritClass( OO.ui.NumberInputWidget, OO.ui.TextInputWidget );
6694
6695 /* Methods */
6696
6697 /**
6698 * Set whether only integers are allowed
6699 *
6700 * @param {boolean} flag
6701 */
6702 OO.ui.NumberInputWidget.prototype.setAllowInteger = function ( flag ) {
6703 this.allowInteger = !!flag;
6704 this.setValidityFlag();
6705 };
6706 // Backward compatibility
6707 OO.ui.NumberInputWidget.prototype.setIsInteger = OO.ui.NumberInputWidget.prototype.setAllowInteger;
6708
6709 /**
6710 * Get whether only integers are allowed
6711 *
6712 * @return {boolean} Flag value
6713 */
6714 OO.ui.NumberInputWidget.prototype.getAllowInteger = function () {
6715 return this.allowInteger;
6716 };
6717 // Backward compatibility
6718 OO.ui.NumberInputWidget.prototype.getIsInteger = OO.ui.NumberInputWidget.prototype.getAllowInteger;
6719
6720 /**
6721 * Set the range of allowed values
6722 *
6723 * @param {number} min Minimum allowed value
6724 * @param {number} max Maximum allowed value
6725 */
6726 OO.ui.NumberInputWidget.prototype.setRange = function ( min, max ) {
6727 if ( min > max ) {
6728 throw new Error( 'Minimum (' + min + ') must not be greater than maximum (' + max + ')' );
6729 }
6730 this.min = min;
6731 this.max = max;
6732 this.setValidityFlag();
6733 };
6734
6735 /**
6736 * Get the current range
6737 *
6738 * @return {number[]} Minimum and maximum values
6739 */
6740 OO.ui.NumberInputWidget.prototype.getRange = function () {
6741 return [ this.min, this.max ];
6742 };
6743
6744 /**
6745 * Set the stepping deltas
6746 *
6747 * @param {number} step Normal step
6748 * @param {number|null} pageStep Page step. If null, 10 * step will be used.
6749 */
6750 OO.ui.NumberInputWidget.prototype.setStep = function ( step, pageStep ) {
6751 if ( step <= 0 ) {
6752 throw new Error( 'Step value must be positive' );
6753 }
6754 if ( pageStep === null ) {
6755 pageStep = step * 10;
6756 } else if ( pageStep <= 0 ) {
6757 throw new Error( 'Page step value must be positive' );
6758 }
6759 this.step = step;
6760 this.pageStep = pageStep;
6761 };
6762
6763 /**
6764 * Get the current stepping values
6765 *
6766 * @return {number[]} Step and page step
6767 */
6768 OO.ui.NumberInputWidget.prototype.getStep = function () {
6769 return [ this.step, this.pageStep ];
6770 };
6771
6772 /**
6773 * Get the current value of the widget as a number
6774 *
6775 * @return {number} May be NaN, or an invalid number
6776 */
6777 OO.ui.NumberInputWidget.prototype.getNumericValue = function () {
6778 return +this.getValue();
6779 };
6780
6781 /**
6782 * Adjust the value of the widget
6783 *
6784 * @param {number} delta Adjustment amount
6785 */
6786 OO.ui.NumberInputWidget.prototype.adjustValue = function ( delta ) {
6787 var n, v = this.getNumericValue();
6788
6789 delta = +delta;
6790 if ( isNaN( delta ) || !isFinite( delta ) ) {
6791 throw new Error( 'Delta must be a finite number' );
6792 }
6793
6794 if ( isNaN( v ) ) {
6795 n = 0;
6796 } else {
6797 n = v + delta;
6798 n = Math.max( Math.min( n, this.max ), this.min );
6799 if ( this.allowInteger ) {
6800 n = Math.round( n );
6801 }
6802 }
6803
6804 if ( n !== v ) {
6805 this.setValue( n );
6806 }
6807 };
6808 /**
6809 * Validate input
6810 *
6811 * @private
6812 * @param {string} value Field value
6813 * @return {boolean}
6814 */
6815 OO.ui.NumberInputWidget.prototype.validateNumber = function ( value ) {
6816 var n = +value;
6817 if ( value === '' ) {
6818 return !this.isRequired();
6819 }
6820
6821 if ( isNaN( n ) || !isFinite( n ) ) {
6822 return false;
6823 }
6824
6825 if ( this.allowInteger && Math.floor( n ) !== n ) {
6826 return false;
6827 }
6828
6829 if ( n < this.min || n > this.max ) {
6830 return false;
6831 }
6832
6833 return true;
6834 };
6835
6836 /**
6837 * Handle mouse click events.
6838 *
6839 * @private
6840 * @param {number} dir +1 or -1
6841 */
6842 OO.ui.NumberInputWidget.prototype.onButtonClick = function ( dir ) {
6843 this.adjustValue( dir * this.step );
6844 };
6845
6846 /**
6847 * Handle mouse wheel events.
6848 *
6849 * @private
6850 * @param {jQuery.Event} event
6851 */
6852 OO.ui.NumberInputWidget.prototype.onWheel = function ( event ) {
6853 var delta = 0;
6854
6855 if ( !this.isDisabled() && this.$input.is( ':focus' ) ) {
6856 // Standard 'wheel' event
6857 if ( event.originalEvent.deltaMode !== undefined ) {
6858 this.sawWheelEvent = true;
6859 }
6860 if ( event.originalEvent.deltaY ) {
6861 delta = -event.originalEvent.deltaY;
6862 } else if ( event.originalEvent.deltaX ) {
6863 delta = event.originalEvent.deltaX;
6864 }
6865
6866 // Non-standard events
6867 if ( !this.sawWheelEvent ) {
6868 if ( event.originalEvent.wheelDeltaX ) {
6869 delta = -event.originalEvent.wheelDeltaX;
6870 } else if ( event.originalEvent.wheelDeltaY ) {
6871 delta = event.originalEvent.wheelDeltaY;
6872 } else if ( event.originalEvent.wheelDelta ) {
6873 delta = event.originalEvent.wheelDelta;
6874 } else if ( event.originalEvent.detail ) {
6875 delta = -event.originalEvent.detail;
6876 }
6877 }
6878
6879 if ( delta ) {
6880 delta = delta < 0 ? -1 : 1;
6881 this.adjustValue( delta * this.step );
6882 }
6883
6884 return false;
6885 }
6886 };
6887
6888 /**
6889 * Handle key down events.
6890 *
6891 * @private
6892 * @param {jQuery.Event} e Key down event
6893 */
6894 OO.ui.NumberInputWidget.prototype.onKeyDown = function ( e ) {
6895 if ( !this.isDisabled() ) {
6896 switch ( e.which ) {
6897 case OO.ui.Keys.UP:
6898 this.adjustValue( this.step );
6899 return false;
6900 case OO.ui.Keys.DOWN:
6901 this.adjustValue( -this.step );
6902 return false;
6903 case OO.ui.Keys.PAGEUP:
6904 this.adjustValue( this.pageStep );
6905 return false;
6906 case OO.ui.Keys.PAGEDOWN:
6907 this.adjustValue( -this.pageStep );
6908 return false;
6909 }
6910 }
6911 };
6912
6913 /**
6914 * @inheritdoc
6915 */
6916 OO.ui.NumberInputWidget.prototype.setDisabled = function ( disabled ) {
6917 // Parent method
6918 OO.ui.NumberInputWidget.parent.prototype.setDisabled.call( this, disabled );
6919
6920 if ( this.minusButton ) {
6921 this.minusButton.setDisabled( this.isDisabled() );
6922 }
6923 if ( this.plusButton ) {
6924 this.plusButton.setDisabled( this.isDisabled() );
6925 }
6926
6927 return this;
6928 };
6929
6930 }( OO ) );
6931
6932 //# sourceMappingURL=oojs-ui-widgets.js.map