Skip to content

Commit 7424fc3

Browse files
authored
fix(splitter): can' t detect changes with OnPush strategy (#UIM-880) (#831)
1 parent d11317e commit 7424fc3

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

packages/mosaic-dev/splitter/module.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// tslint:disable:no-console
2-
import { Component, NgModule, ViewEncapsulation } from '@angular/core';
2+
import { ChangeDetectionStrategy, Component, NgModule, ViewEncapsulation } from '@angular/core';
33
import { BrowserModule } from '@angular/platform-browser';
44
import { McButtonModule } from '@ptsecurity/mosaic/button';
55
import { McIconModule } from '@ptsecurity/mosaic/icon';
@@ -11,7 +11,8 @@ import { Direction, McSplitterModule } from '../../mosaic/splitter';
1111
selector: 'app',
1212
templateUrl: './template.html',
1313
styleUrls: ['../main.scss', './styles.scss'],
14-
encapsulation: ViewEncapsulation.None
14+
encapsulation: ViewEncapsulation.None,
15+
changeDetection: ChangeDetectionStrategy.OnPush
1516
})
1617
export class DemoComponent {
1718
guttersVisibility = true;
@@ -25,17 +26,13 @@ export class DemoComponent {
2526

2627

2728
@NgModule({
28-
declarations: [
29-
DemoComponent
30-
],
29+
declarations: [DemoComponent],
3130
imports: [
3231
BrowserModule,
3332
McButtonModule,
3433
McSplitterModule,
3534
McIconModule
3635
],
37-
bootstrap: [
38-
DemoComponent
39-
]
36+
bootstrap: [DemoComponent]
4037
})
4138
export class DemoModule {}

packages/mosaic-dev/splitter/template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h2 class="mc-h2">Horizontal</h2>
3434

3535

3636
<mc-splitter class="horizontal-block" [direction]="DIRECTION.Horizontal">
37-
<div mc-splitter-area>first</div>
37+
<div mc-splitter-area style="min-width: 200px"><input type="text"></div>
3838
<div mc-splitter-area class="flex">second</div>
3939
<div mc-splitter-area>third</div>
4040
</mc-splitter>

packages/mosaic/splitter/splitter.component.ts

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,36 @@ export enum Direction {
6363
}
6464
})
6565
export class McGutterDirective implements OnInit {
66+
67+
@Input()
6668
get direction(): Direction {
6769
return this._direction;
6870
}
6971

70-
@Input()
7172
set direction(direction: Direction) {
7273
this._direction = direction;
7374
}
7475

7576
private _direction: Direction = Direction.Vertical;
7677

78+
79+
@Input()
7780
get order(): number {
7881
return this._order;
7982
}
8083

81-
@Input()
8284
set order(order: number) {
8385
this._order = coerceNumberProperty(order);
8486
}
8587

8688
private _order: number = 0;
8789

90+
91+
@Input()
8892
get size(): number {
8993
return this._size;
9094
}
9195

92-
@Input()
9396
set size(size: number) {
9497
this._size = coerceNumberProperty(size);
9598
}
@@ -143,23 +146,23 @@ export class McGutterDirective implements OnInit {
143146
export class McGutterGhostDirective {
144147
@Input() visible: boolean;
145148

149+
@Input()
146150
get x(): number {
147151
return this._x;
148152
}
149153

150-
@Input()
151154
set x(x: number) {
152155
this._x = x;
153156
this.setStyle(StyleProperty.Left, coerceCssPixelValue(x));
154157
}
155158

156159
private _x: number = 0;
157160

161+
@Input()
158162
get y(): number {
159163
return this._y;
160164
}
161165

162-
@Input()
163166
set y(y: number) {
164167
this._y = y;
165168
this.setStyle(StyleProperty.Top, coerceCssPixelValue(y));
@@ -168,23 +171,23 @@ export class McGutterGhostDirective {
168171
private _y: number = 0;
169172

170173

174+
@Input()
171175
get direction(): Direction {
172176
return this._direction;
173177
}
174178

175-
@Input()
176179
set direction(direction: Direction) {
177180
this._direction = direction;
178181
this.updateDimensions();
179182
}
180183

181184
private _direction: Direction = Direction.Vertical;
182185

186+
@Input()
183187
get size(): number {
184188
return this._size;
185189
}
186190

187-
@Input()
188191
set size(size: number) {
189192
this._size = coerceNumberProperty(size);
190193
this.updateDimensions();
@@ -240,55 +243,59 @@ export class McSplitterComponent implements OnInit {
240243
private readonly areaPositionDivider: number = 2;
241244
private readonly listeners: (() => void)[] = [];
242245

246+
247+
@Input()
243248
get hideGutters(): boolean {
244249
return this._hideGutters;
245250
}
246251

247-
@Input()
248252
set hideGutters(value: boolean) {
249253
this._hideGutters = coerceBooleanProperty(value);
250254
}
251255

252256
private _hideGutters: boolean = false;
253257

258+
259+
@Input()
254260
get direction(): Direction {
255261
return this._direction;
256262
}
257263

258-
@Input()
259264
set direction(direction: Direction) {
260265
this._direction = direction;
261266
}
262267

263268
private _direction: Direction;
264269

270+
@Input()
265271
get disabled(): boolean {
266272
return this._disabled;
267273
}
268274

269-
@Input()
270275
set disabled(disabled: boolean) {
271276
this._disabled = coerceBooleanProperty(disabled);
272277
}
273278

274279
private _disabled: boolean = false;
275280

281+
282+
@Input()
276283
get useGhost(): boolean {
277284
return this._useGhost;
278285
}
279286

280-
@Input()
281287
set useGhost(useGhost: boolean) {
282288
this._useGhost = coerceBooleanProperty(useGhost);
283289
}
284290

285291
private _useGhost: boolean = false;
286292

293+
294+
@Input()
287295
get gutterSize(): number {
288296
return this._gutterSize;
289297
}
290298

291-
@Input()
292299
set gutterSize(gutterSize: number) {
293300
const size = coerceNumberProperty(gutterSize);
294301
this._gutterSize = size > 0 ? size : this.gutterSize;
@@ -344,8 +351,10 @@ export class McSplitterComponent implements OnInit {
344351

345352
const leftArea = this.areas[leftAreaIndex];
346353
const rightArea = this.areas[rightAreaIndex];
354+
347355
leftArea.initialSize = leftArea.area.getSize();
348356
rightArea.initialSize = rightArea.area.getSize();
357+
349358
let currentGutter: McGutterDirective | undefined;
350359

351360
if (this.useGhost) {
@@ -367,6 +376,7 @@ export class McSplitterComponent implements OnInit {
367376
} else {
368377
this.areas.forEach((item) => {
369378
const size = item.area.getSize();
379+
370380
item.area.disableFlex();
371381
item.area.setSize(size);
372382
});
@@ -407,9 +417,7 @@ export class McSplitterComponent implements OnInit {
407417
return false;
408418
});
409419

410-
if (indexToRemove === -1) {
411-
return;
412-
}
420+
if (indexToRemove === -1) { return; }
413421

414422
this.areas.splice(indexToRemove, 1);
415423
}
@@ -428,11 +436,13 @@ export class McSplitterComponent implements OnInit {
428436
});
429437
}
430438

431-
private onMouseMove(event: MouseEvent,
432-
startPoint: IPoint,
433-
leftArea: IArea,
434-
rightArea: IArea,
435-
currentGutter: McGutterDirective | undefined) {
439+
private onMouseMove(
440+
event: MouseEvent,
441+
startPoint: IPoint,
442+
leftArea: IArea,
443+
rightArea: IArea,
444+
currentGutter: McGutterDirective | undefined
445+
) {
436446
if (!this.isDragging || this.disabled) { return; }
437447

438448
const endPoint: IPoint = {
@@ -454,13 +464,10 @@ export class McSplitterComponent implements OnInit {
454464
const key = this.isVertical() ? 'y' : 'x';
455465

456466
const minPos = leftPos[key] - leftMin;
457-
458467
const maxPos = rightPos[key] + (rightArea.area.getSize() || 0) - rightMin - currentGutter.size;
459-
460468
const newPos = gutterPosition[key] - offset;
461469

462470
this.ghost[key] = newPos < minPos ? minPos : Math.min(newPos, maxPos);
463-
464471
} else {
465472
this.resizeAreas(leftArea, rightArea, offset);
466473
}
@@ -487,16 +494,19 @@ export class McSplitterComponent implements OnInit {
487494
}
488495
}
489496

490-
private onMouseUp(leftArea: IArea,
491-
rightArea: IArea,
492-
currentGutter: McGutterDirective | undefined) {
497+
private onMouseUp(
498+
leftArea: IArea,
499+
rightArea: IArea,
500+
currentGutter: McGutterDirective | undefined
501+
) {
493502
while (this.listeners.length > 0) {
494503
const unsubscribe = this.listeners.pop();
495504

496505
if (unsubscribe) {
497506
unsubscribe();
498507
}
499508
}
509+
500510
if (this.useGhost && currentGutter) {
501511
const gutterPosition = currentGutter.getPosition();
502512
const offset = this.ghost.direction === Direction.Vertical ?
@@ -511,6 +521,8 @@ export class McSplitterComponent implements OnInit {
511521
this.updateGutter();
512522

513523
this.gutterPositionChange.emit();
524+
525+
this.changeDetectorRef.markForCheck();
514526
}
515527

516528
private setStyle(property: StyleProperty, value: string | number) {
@@ -532,7 +544,7 @@ export class McSplitterAreaDirective implements OnInit, OnDestroy {
532544
private elementRef: ElementRef,
533545
private renderer: Renderer2,
534546
private splitter: McSplitterComponent
535-
) { }
547+
) {}
536548

537549
isResizing(): boolean {
538550
return this.splitter.isDragging;
@@ -555,7 +567,8 @@ export class McSplitterAreaDirective implements OnInit, OnDestroy {
555567
this.removeStyle(StyleProperty.Width);
556568
}
557569

558-
this.splitter.gutterPositionChange.subscribe(() => this.emitSizeChange());
570+
this.splitter.gutterPositionChange
571+
.subscribe(this.emitSizeChange);
559572
}
560573

561574
ngOnDestroy(): void {
@@ -567,10 +580,9 @@ export class McSplitterAreaDirective implements OnInit, OnDestroy {
567580
}
568581

569582
setSize(size: number): void {
570-
if (!isNaN(size)) {
571-
const sz = coerceNumberProperty(size);
572-
this.setStyle(this.getSizeProperty(), coerceCssPixelValue(sz));
573-
}
583+
if (isNaN(size)) { return; }
584+
585+
this.setStyle(this.getSizeProperty(), coerceCssPixelValue(coerceNumberProperty(size)));
574586
}
575587

576588
getSize(): number {
@@ -620,7 +632,7 @@ export class McSplitterAreaDirective implements OnInit, OnDestroy {
620632
this.renderer.removeStyle(this.elementRef.nativeElement, style);
621633
}
622634

623-
private emitSizeChange(): void {
635+
private emitSizeChange = () => {
624636
this.sizeChange.emit(this.getSize());
625637
}
626638
}

0 commit comments

Comments
 (0)