# all files / src/layouts/ BookletLayout.js

12.7% Statements 24/189
0% Branches 0/121
0% Functions 0/25
12.7% Lines 24/189

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     /**
     * BookletLayouts contain {@link OO.ui.PageLayout page layouts} as well as
     * an {@link OO.ui.OutlineSelectWidget outline} that allows users to easily navigate
     * through the pages and select which one to display. By default, only one page is
     * displayed at a time and the outline is hidden. When a user navigates to a new page,
     * the booklet layout automatically focuses on the first focusable element, unless the
     * default setting is changed. Optionally, booklets can be configured to show
     * {@link OO.ui.OutlineControlsWidget controls} for adding, moving, and removing items.
     *
     *     @example
     *     // Example of a BookletLayout that contains two PageLayouts.
     *
     *     function PageOneLayout( name, config ) {
     *         PageOneLayout.parent.call( this, name, config );
     *         this.$element.append( '<p>First page</p><p>(This booklet has an outline, displayed on the left)</p>' );
     *     }
     *     OO.inheritClass( PageOneLayout, OO.ui.PageLayout );
     *     PageOneLayout.prototype.setupOutlineItem = function () {
     *         this.outlineItem.setLabel( 'Page One' );
     *     };
     *
     *     function PageTwoLayout( name, config ) {
     *         PageTwoLayout.parent.call( this, name, config );
     *         this.$element.append( '<p>Second page</p>' );
     *     }
     *     OO.inheritClass( PageTwoLayout, OO.ui.PageLayout );
     *     PageTwoLayout.prototype.setupOutlineItem = function () {
     *         this.outlineItem.setLabel( 'Page Two' );
     *     };
     *
     *     var page1 = new PageOneLayout( 'one' ),
     *         page2 = new PageTwoLayout( 'two' );
     *
     *     var booklet = new OO.ui.BookletLayout( {
     *         outlined: true
     *     } );
     *
     *     booklet.addPages ( [ page1, page2 ] );
     *     $( 'body' ).append( booklet.$element );
     *
     * @class
     * @extends OO.ui.MenuLayout
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {boolean} [continuous=false] Show all pages, one after another
     * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when a new page is displayed. Disabled on mobile.
     * @cfg {boolean} [outlined=false] Show the outline. The outline is used to navigate through the pages of the booklet.
     * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
     */
    OO.ui.BookletLayout = function OoUiBookletLayout( config ) {
    	// Configuration initialization
    	config = config || {};

    	// Parent constructor
    	OO.ui.BookletLayout.parent.call( this, config );

    	// Properties
    	this.currentPageName = null;
    	this.pages = {};
    	this.ignoreFocus = false;
    	this.stackLayout = new OO.ui.StackLayout( { continuous: !!config.continuous } );
    	this.$content.append( this.stackLayout.$element );
    	this.autoFocus = config.autoFocus === undefined || !!config.autoFocus;
    	this.outlineVisible = false;
    	this.outlined = !!config.outlined;
    	if ( this.outlined ) {
    		this.editable = !!config.editable;
    		this.outlineControlsWidget = null;
    		this.outlineSelectWidget = new OO.ui.OutlineSelectWidget();
    		this.outlinePanel = new OO.ui.PanelLayout( { scrollable: true } );
    		this.$menu.append( this.outlinePanel.$element );
    		this.outlineVisible = true;
    		if ( this.editable ) {
    			this.outlineControlsWidget = new OO.ui.OutlineControlsWidget(
    				this.outlineSelectWidget
    			);
    		}
    	}
    	this.toggleMenu( this.outlined );

    	// Events
    	this.stackLayout.connect( this, { set: 'onStackLayoutSet' } );
    	if ( this.outlined ) {
    		this.outlineSelectWidget.connect( this, { select: 'onOutlineSelectWidgetSelect' } );
    		this.scrolling = false;
    		this.stackLayout.connect( this, { visibleItemChange: 'onStackLayoutVisibleItemChange' } );
    	}
    	if ( this.autoFocus ) {
    		// Event 'focus' does not bubble, but 'focusin' does
    		this.stackLayout.$element.on( 'focusin', this.onStackLayoutFocus.bind( this ) );
    	}

    	// Initialization
    	this.$element.addClass( 'oo-ui-bookletLayout' );
    	this.stackLayout.$element.addClass( 'oo-ui-bookletLayout-stackLayout' );
    	if ( this.outlined ) {
    		this.outlinePanel.$element
    			.addClass( 'oo-ui-bookletLayout-outlinePanel' )
    			.append( this.outlineSelectWidget.$element );
    		if ( this.editable ) {
    			this.outlinePanel.$element
    				.addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
    				.append( this.outlineControlsWidget.$element );
    		}
    	}
    };

    /* Setup */

    OO.inheritClass( OO.ui.BookletLayout, OO.ui.MenuLayout );

    /* Events */

    /**
     * A 'set' event is emitted when a page is {@link #setPage set} to be displayed by the booklet layout.
     * @event set
     * @param {OO.ui.PageLayout} page Current page
     */

    /**
     * An 'add' event is emitted when pages are {@link #addPages added} to the booklet layout.
     *
     * @event add
     * @param {OO.ui.PageLayout[]} page Added pages
     * @param {number} index Index pages were added at
     */

    /**
     * A 'remove' event is emitted when pages are {@link #clearPages cleared} or
     * {@link #removePages removed} from the booklet.
     *
     * @event remove
     * @param {OO.ui.PageLayout[]} pages Removed pages
     */

    /* Methods */

    /**
     * Handle stack layout focus.
     *
     * @private
     * @param {jQuery.Event} e Focusin event
     */
    OO.ui.BookletLayout.prototype.onStackLayoutFocus = function ( e ) {
    	var name, $target;

    	// Find the page that an element was focused within
    	$target = $( e.target ).closest( '.oo-ui-pageLayout' );
    	for ( name in this.pages ) {
    		// Check for page match, exclude current page to find only page changes
    		if ( this.pages[ name ].$element[ 0 ] === $target[ 0 ] && name !== this.currentPageName ) {
    			this.setPage( name );
    			break;
    		}
    	}
    };

    /**
     * Handle visibleItemChange events from the stackLayout
     *
     * The next visible page is set as the current page by selecting it
     * in the outline
     *
     * @param {OO.ui.PageLayout} page The next visible page in the layout
     */
    OO.ui.BookletLayout.prototype.onStackLayoutVisibleItemChange = function ( page ) {
    	// Set a flag to so that the resulting call to #onStackLayoutSet doesn't
    	// try and scroll the item into view again.
    	this.scrolling = true;
    	this.outlineSelectWidget.selectItemByData( page.getName() );
    	this.scrolling = false;
    };

    /**
     * Handle stack layout set events.
     *
     * @private
     * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
     */
    OO.ui.BookletLayout.prototype.onStackLayoutSet = function ( page ) {
    	var layout = this;
    	if ( !this.scrolling && page ) {
    		page.scrollElementIntoView().done( function () {
    			if ( layout.autoFocus && !OO.ui.isMobile() ) {
    				layout.focus();
    			}
    		} );
    	}
    };

    /**
     * Focus the first input in the current page.
     *
     * If no page is selected, the first selectable page will be selected.
     * If the focus is already in an element on the current page, nothing will happen.
     *
     * @param {number} [itemIndex] A specific item to focus on
     */
    OO.ui.BookletLayout.prototype.focus = function ( itemIndex ) {
    	var page,
    		items = this.stackLayout.getItems();

    	if ( itemIndex !== undefined && items[ itemIndex ] ) {
    		page = items[ itemIndex ];
    	} else {
    		page = this.stackLayout.getCurrentItem();
    	}

    	if ( !page && this.outlined ) {
    		this.selectFirstSelectablePage();
    		page = this.stackLayout.getCurrentItem();
    	}
    	if ( !page ) {
    		return;
    	}
    	// Only change the focus if is not already in the current page
    	if ( !OO.ui.contains( page.$element[ 0 ], this.getElementDocument().activeElement, true ) ) {
    		page.focus();
    	}
    };

    /**
     * Find the first focusable input in the booklet layout and focus
     * on it.
     */
    OO.ui.BookletLayout.prototype.focusFirstFocusable = function () {
    	OO.ui.findFocusable( this.stackLayout.$element ).focus();
    };

    /**
     * Handle outline widget select events.
     *
     * @private
     * @param {OO.ui.OptionWidget|null} item Selected item
     */
    OO.ui.BookletLayout.prototype.onOutlineSelectWidgetSelect = function ( item ) {
    	if ( item ) {
    		this.setPage( item.getData() );
    	}
    };

    /**
     * Check if booklet has an outline.
     *
     * @return {boolean} Booklet has an outline
     */
    OO.ui.BookletLayout.prototype.isOutlined = function () {
    	return this.outlined;
    };

    /**
     * Check if booklet has editing controls.
     *
     * @return {boolean} Booklet is editable
     */
    OO.ui.BookletLayout.prototype.isEditable = function () {
    	return this.editable;
    };

    /**
     * Check if booklet has a visible outline.
     *
     * @return {boolean} Outline is visible
     */
    OO.ui.BookletLayout.prototype.isOutlineVisible = function () {
    	return this.outlined && this.outlineVisible;
    };

    /**
     * Hide or show the outline.
     *
     * @param {boolean} [show] Show outline, omit to invert current state
     * @chainable
     */
    OO.ui.BookletLayout.prototype.toggleOutline = function ( show ) {
    	var booklet = this;

    	if ( this.outlined ) {
    		show = show === undefined ? !this.outlineVisible : !!show;
    		this.outlineVisible = show;
    		this.toggleMenu( show );
    		if ( show && this.editable ) {
    			// HACK: Kill dumb scrollbars when the sidebar stops animating, see T161798. Only necessary when
    			// outline controls are present, delay matches transition on `.oo-ui-menuLayout-menu`.
    			setTimeout( function () {
    				OO.ui.Element.static.reconsiderScrollbars( booklet.outlinePanel.$element[ 0 ] );
    			}, 200 );
    		}
    	}

    	return this;
    };

    /**
     * Find the page closest to the specified page.
     *
     * @param {OO.ui.PageLayout} page Page to use as a reference point
     * @return {OO.ui.PageLayout|null} Page closest to the specified page
     */
    OO.ui.BookletLayout.prototype.findClosestPage = function ( page ) {
    	var next, prev, level,
    		pages = this.stackLayout.getItems(),
    		index = pages.indexOf( page );

    	if ( index !== -1 ) {
    		next = pages[ index + 1 ];
    		prev = pages[ index - 1 ];
    		// Prefer adjacent pages at the same level
    		if ( this.outlined ) {
    			level = this.outlineSelectWidget.getItemFromData( page.getName() ).getLevel();
    			if (
    				prev &&
    				level === this.outlineSelectWidget.getItemFromData( prev.getName() ).getLevel()
    			) {
    				return prev;
    			}
    			if (
    				next &&
    				level === this.outlineSelectWidget.getItemFromData( next.getName() ).getLevel()
    			) {
    				return next;
    			}
    		}
    	}
    	return prev || next || null;
    };

    /**
     * Get the page closest to the specified page.
     *
     * @deprecated 0.22.6 Use {@link OO.ui.BookletLayout#findClosestPage} instead.
     * @param {OO.ui.PageLayout} page Page to use as a reference point
     * @return {OO.ui.PageLayout|null} Page closest to the specified page
     */
    OO.ui.BookletLayout.prototype.getClosestPage = function ( page ) {
    	OO.ui.warnDeprecation( 'BookletLayout#getClosestPage: Deprecated function. Use findClosestPage instead. See T76630.' );
    	return this.findClosestPage( page );
    };

    /**
     * Get the outline widget.
     *
     * If the booklet is not outlined, the method will return `null`.
     *
     * @return {OO.ui.OutlineSelectWidget|null} Outline widget, or null if the booklet is not outlined
     */
    OO.ui.BookletLayout.prototype.getOutline = function () {
    	return this.outlineSelectWidget;
    };

    /**
     * Get the outline controls widget.
     *
     * If the outline is not editable, the method will return `null`.
     *
     * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
     */
    OO.ui.BookletLayout.prototype.getOutlineControls = function () {
    	return this.outlineControlsWidget;
    };

    /**
     * Get a page by its symbolic name.
     *
     * @param {string} name Symbolic name of page
     * @return {OO.ui.PageLayout|undefined} Page, if found
     */
    OO.ui.BookletLayout.prototype.getPage = function ( name ) {
    	return this.pages[ name ];
    };

    /**
     * Get the current page.
     *
     * @return {OO.ui.PageLayout|undefined} Current page, if found
     */
    OO.ui.BookletLayout.prototype.getCurrentPage = function () {
    	var name = this.getCurrentPageName();
    	return name ? this.getPage( name ) : undefined;
    };

    /**
     * Get the symbolic name of the current page.
     *
     * @return {string|null} Symbolic name of the current page
     */
    OO.ui.BookletLayout.prototype.getCurrentPageName = function () {
    	return this.currentPageName;
    };

    /**
     * Add pages to the booklet layout
     *
     * When pages are added with the same names as existing pages, the existing pages will be
     * automatically removed before the new pages are added.
     *
     * @param {OO.ui.PageLayout[]} pages Pages to add
     * @param {number} index Index of the insertion point
     * @fires add
     * @chainable
     */
    OO.ui.BookletLayout.prototype.addPages = function ( pages, index ) {
    	var i, len, name, page, item, currentIndex,
    		stackLayoutPages = this.stackLayout.getItems(),
    		remove = [],
    		items = [];

    	// Remove pages with same names
    	for ( i = 0, len = pages.length; i < len; i++ ) {
    		page = pages[ i ];
    		name = page.getName();

    		if ( Object.prototype.hasOwnProperty.call( this.pages, name ) ) {
    			// Correct the insertion index
    			currentIndex = stackLayoutPages.indexOf( this.pages[ name ] );
    			if ( currentIndex !== -1 && currentIndex + 1 < index ) {
    				index--;
    			}
    			remove.push( this.pages[ name ] );
    		}
    	}
    	if ( remove.length ) {
    		this.removePages( remove );
    	}

    	// Add new pages
    	for ( i = 0, len = pages.length; i < len; i++ ) {
    		page = pages[ i ];
    		name = page.getName();
    		this.pages[ page.getName() ] = page;
    		if ( this.outlined ) {
    			item = new OO.ui.OutlineOptionWidget( { data: name } );
    			page.setOutlineItem( item );
    			items.push( item );
    		}
    	}

    	if ( this.outlined && items.length ) {
    		this.outlineSelectWidget.addItems( items, index );
    		this.selectFirstSelectablePage();
    	}
    	this.stackLayout.addItems( pages, index );
    	this.emit( 'add', pages, index );

    	return this;
    };

    /**
     * Remove the specified pages from the booklet layout.
     *
     * To remove all pages from the booklet, you may wish to use the #clearPages method instead.
     *
     * @param {OO.ui.PageLayout[]} pages An array of pages to remove
     * @fires remove
     * @chainable
     */
    OO.ui.BookletLayout.prototype.removePages = function ( pages ) {
    	var i, len, name, page,
    		items = [];

    	for ( i = 0, len = pages.length; i < len; i++ ) {
    		page = pages[ i ];
    		name = page.getName();
    		delete this.pages[ name ];
    		if ( this.outlined ) {
    			items.push( this.outlineSelectWidget.getItemFromData( name ) );
    			page.setOutlineItem( null );
    		}
    	}
    	if ( this.outlined && items.length ) {
    		this.outlineSelectWidget.removeItems( items );
    		this.selectFirstSelectablePage();
    	}
    	this.stackLayout.removeItems( pages );
    	this.emit( 'remove', pages );

    	return this;
    };

    /**
     * Clear all pages from the booklet layout.
     *
     * To remove only a subset of pages from the booklet, use the #removePages method.
     *
     * @fires remove
     * @chainable
     */
    OO.ui.BookletLayout.prototype.clearPages = function () {
    	var i, len,
    		pages = this.stackLayout.getItems();

    	this.pages = {};
    	this.currentPageName = null;
    	if ( this.outlined ) {
    		this.outlineSelectWidget.clearItems();
    		for ( i = 0, len = pages.length; i < len; i++ ) {
    			pages[ i ].setOutlineItem( null );
    		}
    	}
    	this.stackLayout.clearItems();

    	this.emit( 'remove', pages );

    	return this;
    };

    /**
     * Set the current page by symbolic name.
     *
     * @fires set
     * @param {string} name Symbolic name of page
     */
    OO.ui.BookletLayout.prototype.setPage = function ( name ) {
    	var selectedItem,
    		$focused,
    		page = this.pages[ name ],
    		previousPage = this.currentPageName && this.pages[ this.currentPageName ];

    	if ( name !== this.currentPageName ) {
    		if ( this.outlined ) {
    			selectedItem = this.outlineSelectWidget.getSelectedItem();
    			if ( selectedItem && selectedItem.getData() !== name ) {
    				this.outlineSelectWidget.selectItemByData( name );
    			}
    		}
    		if ( page ) {
    			if ( previousPage ) {
    				previousPage.setActive( false );
    				// Blur anything focused if the next page doesn't have anything focusable.
    				// This is not needed if the next page has something focusable (because once it is focused
    				// this blur happens automatically). If the layout is non-continuous, this check is
    				// meaningless because the next page is not visible yet and thus can't hold focus.
    				if (
    					this.autoFocus &&
    					!OO.ui.isMobile() &&
    					this.stackLayout.continuous &&
    					OO.ui.findFocusable( page.$element ).length !== 0
    				) {
    					$focused = previousPage.$element.find( ':focus' );
    					if ( $focused.length ) {
    						$focused[ 0 ].blur();
    					}
    				}
    			}
    			this.currentPageName = name;
    			page.setActive( true );
    			this.stackLayout.setItem( page );
    			if ( !this.stackLayout.continuous && previousPage ) {
    				// This should not be necessary, since any inputs on the previous page should have been
    				// blurred when it was hidden, but browsers are not very consistent about this.
    				$focused = previousPage.$element.find( ':focus' );
    				if ( $focused.length ) {
    					$focused[ 0 ].blur();
    				}
    			}
    			this.emit( 'set', page );
    		}
    	}
    };

    /**
     * Select the first selectable page.
     *
     * @chainable
     */
    OO.ui.BookletLayout.prototype.selectFirstSelectablePage = function () {
    	if ( !this.outlineSelectWidget.getSelectedItem() ) {
    		this.outlineSelectWidget.selectItem( this.outlineSelectWidget.getFirstSelectableItem() );
    	}

    	return this;
    };