• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 14441306397

14 Apr 2025 08:42AM CUT coverage: 91.617% (-0.01%) from 91.629%
14441306397

Pull #15517

github

web-flow
Merge ef6afccab into fb9119e28
Pull Request #15517: Sizing panes correctly with minSize is used when the browser is shrinked

13322 of 15586 branches covered (85.47%)

3 of 7 new or added lines in 1 file covered. (42.86%)

1 existing line in 1 file now uncovered.

26863 of 29321 relevant lines covered (91.62%)

34009.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.44
/projects/igniteui-angular/src/lib/splitter/splitter.component.ts
1
import { DOCUMENT } from '@angular/common';
2
import { AfterContentInit, Component, ContentChildren, ElementRef, EventEmitter, HostBinding, HostListener, Inject, Input, Output, QueryList, booleanAttribute, forwardRef } from '@angular/core';
3
import { DragDirection, IDragMoveEventArgs, IDragStartEventArgs, IgxDragDirective, IgxDragIgnoreDirective } from '../directives/drag-drop/drag-drop.directive';
4
import { IgxSplitterPaneComponent } from './splitter-pane/splitter-pane.component';
5

6
/**
7
 * An enumeration that defines the `SplitterComponent` panes orientation.
8
 */
9
export enum SplitterType {
2✔
10
    Horizontal,
2✔
11
    Vertical
2✔
12
}
13

14
export declare interface ISplitterBarResizeEventArgs {
15
    pane: IgxSplitterPaneComponent;
16
    sibling: IgxSplitterPaneComponent;
17
}
18

19
/**
20
 * Provides a framework for a simple layout, splitting the view horizontally or vertically
21
 * into multiple smaller resizable and collapsible areas.
22
 *
23
 * @igxModule IgxSplitterModule
24
 *
25
 * @igxParent Layouts
26
 *
27
 * @igxTheme igx-splitter-theme
28
 *
29
 * @igxKeywords splitter panes layout
30
 *
31
 * @igxGroup presentation
32
 *
33
 * @example
34
 * ```html
35
 * <igx-splitter>
36
 *  <igx-splitter-pane>
37
 *      ...
38
 *  </igx-splitter-pane>
39
 *  <igx-splitter-pane>
40
 *      ...
41
 *  </igx-splitter-pane>
42
 * </igx-splitter>
43
 * ```
44
 */
45
@Component({
46
    selector: 'igx-splitter',
47
    templateUrl: './splitter.component.html',
48
    imports: [forwardRef(() => IgxSplitBarComponent)]
58✔
49
})
50
export class IgxSplitterComponent implements AfterContentInit {
2✔
51
    /**
52
     * Gets the list of splitter panes.
53
     *
54
     * @example
55
     * ```typescript
56
     * const panes = this.splitter.panes;
57
     * ```
58
     */
59
    @ContentChildren(IgxSplitterPaneComponent, { read: IgxSplitterPaneComponent })
60
    public panes!: QueryList<IgxSplitterPaneComponent>;
61

62
    /**
63
    * @hidden
64
    * @internal
65
    */
66
    @HostBinding('class.igx-splitter')
67
    public cssClass = 'igx-splitter';
22✔
68

69
    /**
70
     * @hidden @internal
71
     * Gets/Sets the `overflow` property of the current splitter.
72
     */
73
    @HostBinding('style.overflow')
74
    public overflow = 'hidden';
22✔
75

76
    /**
77
     * @hidden @internal
78
     * Sets/Gets the `display` property of the current splitter.
79
     */
80
    @HostBinding('style.display')
81
    public display = 'flex';
22✔
82

83
    /**
84
     * @hidden
85
     * @internal
86
     */
87
    @HostBinding('attr.aria-orientation')
88
    public get orientation() {
89
        return this.type === SplitterType.Horizontal ? 'horizontal' : 'vertical';
160✔
90
    }
91

92
    /**
93
     * Event fired when resizing of panes starts.
94
     *
95
     * @example
96
     * ```html
97
     * <igx-splitter (resizeStart)='resizeStart($event)'>
98
     *  <igx-splitter-pane>...</igx-splitter-pane>
99
     * </igx-splitter>
100
     * ```
101
     */
102
    @Output()
103
    public resizeStart = new EventEmitter<ISplitterBarResizeEventArgs>();
22✔
104

105
    /**
106
     * Event fired when resizing of panes is in progress.
107
     *
108
     * @example
109
     * ```html
110
     * <igx-splitter (resizing)='resizing($event)'>
111
     *  <igx-splitter-pane>...</igx-splitter-pane>
112
     * </igx-splitter>
113
     * ```
114
     */
115
    @Output()
116
    public resizing = new EventEmitter<ISplitterBarResizeEventArgs>();
22✔
117

118

119
    /**
120
     * Event fired when resizing of panes ends.
121
     *
122
     * @example
123
     * ```html
124
     * <igx-splitter (resizeEnd)='resizeEnd($event)'>
125
     *  <igx-splitter-pane>...</igx-splitter-pane>
126
     * </igx-splitter>
127
     * ```
128
     */
129
    @Output()
130
    public resizeEnd = new EventEmitter<ISplitterBarResizeEventArgs>();
22✔
131

132
    private _type: SplitterType = SplitterType.Horizontal;
22✔
133

134
    /**
135
     * @hidden @internal
136
     * A field that holds the initial size of the main `IgxSplitterPaneComponent` in each pair of panes divided by a splitter bar.
137
     */
138
    private initialPaneSize!: number;
139

140
    /**
141
     * @hidden @internal
142
     * A field that holds the initial size of the sibling pane in each pair of panes divided by a gripper.
143
     * @memberof SplitterComponent
144
     */
145
    private initialSiblingSize!: number;
146

147
    /**
148
     * @hidden @internal
149
     * The main pane in each pair of panes divided by a gripper.
150
     */
151
    private pane!: IgxSplitterPaneComponent;
152

153
    /**
154
     * The sibling pane in each pair of panes divided by a splitter bar.
155
     */
156
    private sibling!: IgxSplitterPaneComponent;
157

158
    constructor(@Inject(DOCUMENT) public document, private elementRef: ElementRef) { }
22✔
159
    /**
160
     * Gets/Sets the splitter orientation.
161
     *
162
     * @example
163
     * ```html
164
     * <igx-splitter [type]="type">...</igx-splitter>
165
     * ```
166
     */
167
    @Input()
168
    public get type() {
169
        return this._type;
646✔
170
    }
171
    public set type(value) {
172
        this._type = value;
28✔
173
        this.resetPaneSizes();
28✔
174
        this.panes?.notifyOnChanges();
28✔
175
    }
176

177
    /**
178
     * Sets the visibility of the handle and expanders in the splitter bar.
179
     * False by default
180
     *
181
     * @example
182
     * ```html
183
     * <igx-splitter [nonCollapsible]='true'>
184
     * </igx-splitter>
185
     * ```
186
     */
187
    @Input({ transform: booleanAttribute })
188
    public nonCollapsible = false; // Input to toggle showing/hiding expanders
22✔
189

190
    /**
191
     * @hidden @internal
192
     * Gets the `flex-direction` property of the current `SplitterComponent`.
193
     */
194
    @HostBinding('style.flex-direction')
195
    public get direction(): string {
196
        return this.type === SplitterType.Horizontal ? 'row' : 'column';
160✔
197
    }
198

199
    /** @hidden @internal */
200
    public ngAfterContentInit(): void {
201
        this.initPanes();
22✔
202
        this.panes.changes.subscribe(() => {
22✔
203
            this.initPanes();
9✔
204
        });
205
    }
206

207
    /**
208
     * @hidden @internal
209
     * This method performs  initialization logic when the user starts dragging the splitter bar between each pair of panes.
210
     * @param pane - the main pane associated with the currently dragged bar.
211
     */
212
    public onMoveStart(pane: IgxSplitterPaneComponent) {
213
        const panes = this.panes.toArray();
18✔
214
        this.pane = pane;
18✔
215
        this.sibling = panes[panes.indexOf(this.pane) + 1];
18✔
216

217
        const paneRect = this.pane.element.getBoundingClientRect();
18✔
218
        this.initialPaneSize = this.type === SplitterType.Horizontal ? paneRect.width : paneRect.height;
18✔
219

220
        const siblingRect = this.sibling.element.getBoundingClientRect();
18✔
221
        this.initialSiblingSize = this.type === SplitterType.Horizontal ? siblingRect.width : siblingRect.height;
18✔
222
        const args: ISplitterBarResizeEventArgs = { pane: this.pane, sibling: this.sibling };
18✔
223
        this.resizeStart.emit(args);
18✔
224
    }
225

226
    /**
227
     * @hidden @internal
228
     * This method performs calculations concerning the sizes of each pair of panes when the bar between them is dragged.
229
     * @param delta - The difference along the X (or Y) axis between the initial and the current point when dragging the bar.
230
     */
231
    public onMoving(delta: number) {
232
        const [ paneSize, siblingSize ] = this.calcNewSizes(delta);
18✔
233

234
        this.pane.dragSize = paneSize + 'px';
18✔
235
        this.sibling.dragSize = siblingSize + 'px';
18✔
236

237
        const args: ISplitterBarResizeEventArgs = { pane: this.pane, sibling: this.sibling };
18✔
238
        this.resizing.emit(args);
18✔
239
    }
240

241
    public onMoveEnd(delta: number) {
242
        let [ paneSize, siblingSize ] = this.calcNewSizes(delta);
6✔
243

244
        if (paneSize + siblingSize > this.getTotalSize() && delta < 0) {
6!
NEW
245
            paneSize = this.getTotalSize();
×
NEW
246
            siblingSize = 0;
×
247
        } else if(paneSize + siblingSize > this.getTotalSize() && delta > 0) {
6!
NEW
248
            paneSize = 0;
×
NEW
249
            siblingSize = this.getTotalSize();
×
250
        }
251

252
        if (this.pane.isPercentageSize) {
6✔
253
            // handle % resizes
254
            const totalSize = this.getTotalSize();
3✔
255
            const percentPaneSize = (paneSize / totalSize) * 100;
3✔
256
            this.pane.size = percentPaneSize + '%';
3✔
257
        } else {
258
            // px resize
259
            this.pane.size = paneSize + 'px';
3✔
260
        }
261

262
        if (this.sibling.isPercentageSize) {
6✔
263
            // handle % resizes
264
            const totalSize = this.getTotalSize();
4✔
265
            const percentSiblingPaneSize = (siblingSize / totalSize) * 100;
4✔
266
            this.sibling.size = percentSiblingPaneSize + '%';
4✔
267
        } else {
268
            // px resize
269
            this.sibling.size = siblingSize + 'px';
2✔
270
        }
271
        this.pane.dragSize = null;
6✔
272
        this.sibling.dragSize = null;
6✔
273

274
        const args: ISplitterBarResizeEventArgs = { pane: this.pane, sibling: this.sibling };
6✔
275
        this.resizeEnd.emit(args);
6✔
276
    }
277

278
    /** @hidden @internal */
279
    public getPaneSiblingsByOrder(order: number, barIndex: number): Array<IgxSplitterPaneComponent> {
280
        const panes = this.panes.toArray();
198✔
281
        const prevPane = panes[order - barIndex - 1];
198✔
282
        const nextPane = panes[order - barIndex];
198✔
283
        const siblings = [prevPane, nextPane];
198✔
284
        return siblings;
198✔
285
    }
286

287
    private getTotalSize() {
288
        const computed = this.document.defaultView.getComputedStyle(this.elementRef.nativeElement);
23✔
289
        const totalSize = this.type === SplitterType.Horizontal ? computed.getPropertyValue('width') : computed.getPropertyValue('height');
23✔
290
        return parseFloat(totalSize);
23✔
291
    }
292

293

294
    /**
295
     * @hidden @internal
296
     * This method inits panes with properties.
297
     */
298
    private initPanes() {
299
        this.panes.forEach(pane => {
31✔
300
            pane.owner = this;
69✔
301
            if (this.type === SplitterType.Horizontal) {
69✔
302
                pane.minWidth = pane.minSize ?? '0';
50✔
303
                pane.maxWidth = pane.maxSize ?? '100%';
50✔
304
            } else {
305
                pane.minHeight = pane.minSize ?? '0';
19✔
306
                pane.maxHeight = pane.maxSize ?? '100%';
19✔
307
            }
308
        });
309
        this.assignFlexOrder();
31✔
310
        if (this.panes.filter(x => x.collapsed).length > 0) {
69✔
311
            // if any panes are collapsed, reset sizes.
312
            this.resetPaneSizes();
2✔
313
        }
314
    }
315

316
    /**
317
     * @hidden @internal
318
     * This method reset pane sizes.
319
     */
320
    private resetPaneSizes() {
321
        if (this.panes) {
30✔
322
            // if type is changed runtime, should reset sizes.
323
            this.panes.forEach(x => {
9✔
324
                x.size = 'auto'
21✔
325
                x.minWidth = '0';
21✔
326
                x.maxWidth = '100%';
21✔
327
                x.minHeight = '0';
21✔
328
                x.maxHeight = '100%';
21✔
329
            });
330
        }
331
    }
332

333
    /**
334
     * @hidden @internal
335
     * This method assigns the order of each pane.
336
     */
337
    private assignFlexOrder() {
338
        let k = 0;
31✔
339
        this.panes.forEach((pane: IgxSplitterPaneComponent) => {
31✔
340
            pane.order = k;
69✔
341
            k += 2;
69✔
342
        });
343
    }
344

345
    /**
346
     * @hidden @internal
347
     * Calculates new sizes for the panes based on move delta and initial sizes
348
     */
349
    private calcNewSizes(delta: number): [number, number] {
350
        const min = parseInt(this.pane.minSize, 10) || 0;
24✔
351
        const minSibling = parseInt(this.sibling.minSize, 10) || 0;
24✔
352
        const max = parseInt(this.pane.maxSize, 10) || this.initialPaneSize + this.initialSiblingSize - minSibling;
24✔
353
        const maxSibling = parseInt(this.sibling.maxSize, 10) || this.initialPaneSize + this.initialSiblingSize - min;
24✔
354

355
        if (delta < 0) {
24✔
356
            const maxPossibleDelta = Math.min(
16✔
357
                max - this.initialPaneSize,
358
                this.initialSiblingSize - minSibling,
359
            )
360
            delta = Math.min(maxPossibleDelta, Math.abs(delta)) * -1;
16✔
361
        } else {
362
            const maxPossibleDelta = Math.min(
8✔
363
                this.initialPaneSize - min,
364
                maxSibling - this.initialSiblingSize
365
            )
366
            delta = Math.min(maxPossibleDelta, Math.abs(delta));
8✔
367
        }
368
        return [this.initialPaneSize - delta, this.initialSiblingSize + delta];
24✔
369
    }
370
}
371

372
/**
373
 * @hidden @internal
374
 * Represents the draggable bar that visually separates panes and allows for changing their sizes.
375
 */
376
@Component({
377
    selector: 'igx-splitter-bar',
378
    templateUrl: './splitter-bar.component.html',
379
    imports: [IgxDragDirective, IgxDragIgnoreDirective]
380
})
381
export class IgxSplitBarComponent {
2✔
382
    /**
383
     * Set css class to the host element.
384
     */
385
    @HostBinding('class.igx-splitter-bar-host')
386
    public cssClass = 'igx-splitter-bar-host';
28✔
387

388
     /**
389
     * Sets the visibility of the handle and expanders in the splitter bar.
390
     */
391
    @Input({ transform: booleanAttribute })
392
    public nonCollapsible;
393

394
    /**
395
     * Gets/Sets the orientation.
396
     */
397
    @Input()
398
    public type: SplitterType = SplitterType.Horizontal;
28✔
399

400
    /**
401
     * Sets/gets the element order.
402
     */
403
    @HostBinding('style.order')
404
    @Input()
405
    public order!: number;
406

407
    /**
408
     * @hidden
409
     * @internal
410
     */
411
    @HostBinding('attr.tabindex')
412
    public get tabindex() {
413
        return this.resizeDisallowed ? null : 0;
198✔
414
    }
415

416
    /**
417
     * @hidden
418
     * @internal
419
     */
420
    @HostBinding('attr.aria-orientation')
421
    public get orientation() {
422
        return this.type === SplitterType.Horizontal ? 'horizontal' : 'vertical';
198✔
423
    }
424

425
    /**
426
     * @hidden
427
     * @internal
428
     */
429
    public get cursor() {
430
        if (this.resizeDisallowed) {
200✔
431
            return '';
39✔
432
        }
433
        return this.type === SplitterType.Horizontal ? 'col-resize' : 'row-resize';
161✔
434
    }
435

436
    /**
437
     * Sets/gets the `SplitPaneComponent` associated with the current `SplitBarComponent`.
438
     *
439
     * @memberof SplitBarComponent
440
     */
441
    @Input()
442
    public pane!: IgxSplitterPaneComponent;
443

444
    /**
445
     * Sets/Gets the `SplitPaneComponent` sibling components associated with the current `SplitBarComponent`.
446
     */
447
    @Input()
448
    public siblings!: Array<IgxSplitterPaneComponent>;
449

450
    /**
451
     * An event that is emitted whenever we start dragging the current `SplitBarComponent`.
452
     */
453
    @Output()
454
    public moveStart = new EventEmitter<IgxSplitterPaneComponent>();
28✔
455

456
    /**
457
     * An event that is emitted while we are dragging the current `SplitBarComponent`.
458
     */
459
    @Output()
460
    public moving = new EventEmitter<number>();
28✔
461

462
    @Output()
463
    public movingEnd = new EventEmitter<number>();
28✔
464

465
    /**
466
     * A temporary holder for the pointer coordinates.
467
     */
468
    private startPoint!: number;
469

470
    private interactionKeys = new Set('right down left up arrowright arrowdown arrowleft arrowup'.split(' '));
28✔
471

472
    /**
473
     * @hidden @internal
474
     */
475
    public get prevButtonHidden() {
476
        return this.siblings[0].collapsed && !this.siblings[1].collapsed;
198✔
477
    }
478

479
    /**
480
     * @hidden @internal
481
     */
482
    @HostListener('keydown', ['$event'])
483
    public keyEvent(event: KeyboardEvent) {
484
        const key = event.key.toLowerCase();
17✔
485
        const ctrl = event.ctrlKey;
17✔
486
        event.stopPropagation();
17✔
487
        if (this.interactionKeys.has(key)) {
17✔
488
            event.preventDefault();
17✔
489
        }
490
        switch (key) {
17!
491
            case 'arrowup':
492
            case 'up':
493
                if (this.type === SplitterType.Vertical) {
3✔
494
                    if (ctrl) {
3✔
495
                        this.onCollapsing(false);
2✔
496
                        break;
2✔
497
                    }
498
                    if (!this.resizeDisallowed) {
1✔
499
                        event.preventDefault();
1✔
500
                        this.moveStart.emit(this.pane);
1✔
501
                        this.moving.emit(10);
1✔
502
                    }
503
                }
504
                break;
1✔
505
            case 'arrowdown':
506
            case 'down':
507
                if (this.type === SplitterType.Vertical) {
5✔
508
                    if (ctrl) {
5✔
509
                        this.onCollapsing(true);
2✔
510
                        break;
2✔
511
                    }
512
                    if (!this.resizeDisallowed) {
3✔
513
                        event.preventDefault();
2✔
514
                        this.moveStart.emit(this.pane);
2✔
515
                        this.moving.emit(-10);
2✔
516
                    }
517
                }
518
                break;
3✔
519
            case 'arrowleft':
520
            case 'left':
521
                if (this.type === SplitterType.Horizontal) {
3✔
522
                    if (ctrl) {
3✔
523
                        this.onCollapsing(false);
2✔
524
                        break;
2✔
525
                    }
526
                    if (!this.resizeDisallowed) {
1✔
527
                        event.preventDefault();
1✔
528
                        this.moveStart.emit(this.pane);
1✔
529
                        this.moving.emit(10);
1✔
530
                    }
531
                }
532
                break;
1✔
533
            case 'arrowright':
534
            case 'right':
535
                if (this.type === SplitterType.Horizontal) {
6✔
536
                    if (ctrl) {
6✔
537
                        this.onCollapsing(true);
2✔
538
                        break;
2✔
539
                    }
540
                    if (!this.resizeDisallowed) {
4✔
541
                        event.preventDefault();
2✔
542
                        this.moveStart.emit(this.pane);
2✔
543
                        this.moving.emit(-10);
2✔
544
                    }
545
                }
546
                break;
4✔
547
            default:
548
                break;
×
549
        }
550
    }
551

552
    /**
553
     * @hidden @internal
554
     */
555
    public get dragDir() {
556
        return this.type === SplitterType.Horizontal ? DragDirection.VERTICAL : DragDirection.HORIZONTAL;
198✔
557
    }
558

559
    /**
560
     * @hidden @internal
561
     */
562
    public get nextButtonHidden() {
563
        return this.siblings[1].collapsed && !this.siblings[0].collapsed;
198✔
564
    }
565

566
    /**
567
     * @hidden @internal
568
     */
569
    public onDragStart(event: IDragStartEventArgs) {
570
        if (this.resizeDisallowed) {
1✔
571
            event.cancel = true;
1✔
572
            return;
1✔
573
        }
574
        this.startPoint = this.type === SplitterType.Horizontal ? event.startX : event.startY;
×
575
        this.moveStart.emit(this.pane);
×
576
    }
577

578
    /**
579
     * @hidden @internal
580
     */
581
    public onDragMove(event: IDragMoveEventArgs) {
582
        const isHorizontal = this.type === SplitterType.Horizontal;
×
583
        const curr = isHorizontal ? event.pageX : event.pageY;
×
584
        const delta = this.startPoint - curr;
×
585
        if (delta !== 0) {
×
586
            this.moving.emit(delta);
×
587
            event.cancel = true;
×
588
            event.owner.element.nativeElement.style.transform = '';
×
589
        }
590
    }
591

592
    public onDragEnd(event: any) {
593
        const isHorizontal = this.type === SplitterType.Horizontal;
×
594
        const curr = isHorizontal ? event.pageX : event.pageY;
×
595
        const delta = this.startPoint - curr;
×
596
        if (delta !== 0) {
×
597
            this.movingEnd.emit(delta);
×
598
        }
599
    }
600

601
    protected get resizeDisallowed() {
602
        const relatedTabs = this.siblings;
408✔
603
        return !!relatedTabs.find(x => x.resizable === false || x.collapsed === true);
768✔
604
    }
605

606
    /**
607
     * @hidden @internal
608
     */
609
    public onCollapsing(next: boolean) {
610
        const prevSibling = this.siblings[0];
15✔
611
        const nextSibling = this.siblings[1];
15✔
612
        let target;
613
        if (next) {
15✔
614
            // if next is clicked when prev pane is hidden, show prev pane, else hide next pane.
615
            target = prevSibling.collapsed ? prevSibling : nextSibling;
7✔
616
        } else {
617
            // if prev is clicked when next pane is hidden, show next pane, else hide prev pane.
618
            target = nextSibling.collapsed ? nextSibling : prevSibling;
8✔
619
        }
620
        target.toggle();
15✔
621
    }
622
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc