Skip to content

Commit d365396

Browse files
committed
chore(nav-drawer): make isOs and isPlatformBrowser properties, #4426
Also reduce wait in nav-drawer test from 2000 -> 200ms Also remove unnecessary checks for isPlatformBrowser in nav-drawer
1 parent a0e3164 commit d365396

File tree

6 files changed

+27
-37
lines changed

6 files changed

+27
-37
lines changed

projects/igniteui-angular/src/lib/core/touch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class HammerGesturesManager {
2121
private _hammerManagers: Array<{ element: EventTarget, manager: HammerManager; }> = [];
2222

2323
constructor(private _zone: NgZone, @Inject(DOCUMENT) private doc: any, private platformUtil: PlatformUtil) {
24-
this.platformBrowser = this.platformUtil.isPlatformBrowser();
24+
this.platformBrowser = this.platformUtil.isPlatformBrowser;
2525
if (this.platformBrowser) {
2626
this.hammerOptions = {
2727
// D.P. #447 Force TouchInput due to PointerEventInput bug (https://github.com/hammerjs/hammer.js/issues/1065)

projects/igniteui-angular/src/lib/core/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ export function isFirefox(): boolean {
231231

232232
/**
233233
* @hidden
234-
* TODO: make injectable, check isPlatformBrowser()
235234
*/
236235
@Injectable({ providedIn: 'root' })
237236
export class PlatformUtil {
@@ -241,12 +240,12 @@ export class PlatformUtil {
241240
this.platformBrowser = isPlatformBrowser(this.platformId);
242241
}
243242

244-
public isIOS(): boolean {
243+
public get isIOS(): boolean {
245244
const iosBrowser = this.platformBrowser && /iPad|iPhone|iPod/.test(navigator.userAgent) && !('MSStream' in window);
246245
return iosBrowser;
247246
}
248247

249-
public isPlatformBrowser(): boolean {
248+
public get isPlatformBrowser(): boolean {
250249
return this.platformBrowser;
251250
}
252251
}

projects/igniteui-angular/src/lib/grids/cell.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
588588
this.nativeElement.addEventListener('compositionend', this.compositionEndHandler);
589589
}
590590
});
591-
if (this.platformUtil.isIOS()) {
591+
if (this.platformUtil.isIOS) {
592592
this.touchManager.addEventListener(this.nativeElement, 'doubletap', this.onDoubleClick, {
593593
cssProps: { } /* don't disable user-select, etc */
594594
} as HammerOptions);

projects/igniteui-angular/src/lib/grids/grid/cell.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ describe('IgxGrid - Cell component #grid', () => {
182182

183183
it('Should not attach doubletap handler for non-iOS', () => {
184184
const addListenerSpy = spyOn(HammerGesturesManager.prototype, 'addEventListener');
185-
spyOn(PlatformUtil.prototype, 'isIOS').and.returnValue(false);
185+
spyOnProperty(PlatformUtil.prototype, 'isIOS').and.returnValue(false);
186186
const fix = TestBed.createComponent(DefaultGridComponent);
187187
fix.detectChanges();
188188
expect(addListenerSpy).not.toHaveBeenCalled();
189189
});
190190

191191
it('Should handle doubletap on iOS, trigger onDoubleClick event', () => {
192192
const addListenerSpy = spyOn(HammerGesturesManager.prototype, 'addEventListener');
193-
spyOn(PlatformUtil.prototype, 'isIOS').and.returnValue(true);
193+
spyOnProperty(PlatformUtil.prototype, 'isIOS').and.returnValue(true);
194194
const fix = TestBed.createComponent(DefaultGridComponent);
195195
fix.detectChanges();
196196

projects/igniteui-angular/src/lib/navigation-drawer/navigation-drawer.component.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ describe('Navigation Drawer', () => {
525525
window.dispatchEvent(new Event('resize'));
526526

527527
// wait for debounce
528-
await wait(2000);
528+
await wait(200);
529529
expect(fixture.componentInstance.navDrawer.pin).toBe(false, `Shouldn't change state on resize if window width is the same`);
530530
expect(fixture.componentInstance.pin).toBe(true, 'Parent component pin remain on resize if window width is the same');
531531
fixture.componentInstance.pin = true;
@@ -535,15 +535,15 @@ describe('Navigation Drawer', () => {
535535
window.dispatchEvent(new Event('resize'));
536536

537537
// wait for debounce
538-
await wait(2000);
538+
await wait(200);
539539
expect(fixture.componentInstance.navDrawer.pin).toBe(true, 'Should pin on window resize over threshold');
540540
expect(fixture.componentInstance.pin).toBe(true, 'Parent pin update on window resize over threshold');
541541

542542
widthSpyOverride.and.returnValue(768);
543543
window.dispatchEvent(new Event('resize'));
544544

545545
// wait for debounce
546-
await wait(2000);
546+
await wait(200);
547547
expect(fixture.componentInstance.navDrawer.pin).toBe(false, 'Should un-pin on window resize below threshold');
548548
expect(fixture.componentInstance.pin).toBe(false, 'Parent pin update on window resize below threshold');
549549
fixture.componentInstance.pinThreshold = 500;

projects/igniteui-angular/src/lib/navigation-drawer/navigation-drawer.component.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -614,18 +614,14 @@ export class IgxNavigationDrawerComponent implements
614614
}
615615

616616
private getWindowWidth() {
617-
if (!this.platformUtil.isPlatformBrowser()) {
618-
return;
619-
}
620-
621617
return (window.innerWidth > 0) ? window.innerWidth : screen.width;
622618
}
623619

624620
/**
625621
* Sets the drawer width.
626622
*/
627623
private setDrawerWidth(width: string) {
628-
if (this.platformUtil.isPlatformBrowser()) {
624+
if (this.platformUtil.isPlatformBrowser) {
629625
requestAnimationFrame(() => {
630626
if (this.drawer) {
631627
this.renderer.setElementStyle(this.drawer, 'width', width);
@@ -659,7 +655,7 @@ export class IgxNavigationDrawerComponent implements
659655
this._touchManager.addGlobalEventListener('document', 'panmove', this.pan);
660656
this._touchManager.addGlobalEventListener('document', 'panend', this.panEnd);
661657
}
662-
if (!this._resizeObserver && this.platformUtil.isPlatformBrowser()) {
658+
if (!this._resizeObserver && this.platformUtil.isPlatformBrowser) {
663659
this._resizeObserver = fromEvent(window, 'resize').pipe(debounce(() => interval(150)))
664660
.subscribe((value) => {
665661
this.checkPinThreshold(value);
@@ -677,6 +673,9 @@ export class IgxNavigationDrawerComponent implements
677673
}
678674

679675
private checkPinThreshold = (evt?: Event) => {
676+
if (!this.platformUtil.isPlatformBrowser) {
677+
return;
678+
}
680679
let windowWidth;
681680
if (this.pinThreshold) {
682681
windowWidth = this.getWindowWidth();
@@ -815,27 +814,19 @@ export class IgxNavigationDrawerComponent implements
815814
* @param opacity optional value to apply to the overlay
816815
*/
817816
private setXSize(x: number, opacity?: string) {
818-
if (this.platformUtil.isPlatformBrowser()) {
819-
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
820-
window.requestAnimationFrame(() => {
821-
this.setXSizeInternal(x, opacity);
822-
});
823-
} else {
824-
this.setXSizeInternal(x, opacity);
825-
}
826-
}
827-
828-
private setXSizeInternal(x: number, opacity?: string) {
829-
if (this.hasAnimateWidth) {
830-
this.renderer.setElementStyle(this.drawer, 'width', x ? Math.abs(x) + 'px' : '');
831-
} else {
832-
this.renderer.setElementStyle(this.drawer, 'transform', x ? 'translate3d(' + x + 'px,0,0)' : '');
833-
this.renderer.setElementStyle(this.drawer, '-webkit-transform',
834-
x ? 'translate3d(' + x + 'px,0,0)' : '');
835-
}
836-
if (opacity !== undefined) {
837-
this.renderer.setElementStyle(this.overlay, 'opacity', opacity);
838-
}
817+
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
818+
window.requestAnimationFrame(() => {
819+
if (this.hasAnimateWidth) {
820+
this.renderer.setElementStyle(this.drawer, 'width', x ? Math.abs(x) + 'px' : '');
821+
} else {
822+
const transform = x ? 'translate3d(' + x + 'px,0,0)' : '';
823+
this.renderer.setElementStyle(this.drawer, 'transform', transform);
824+
this.renderer.setElementStyle(this.drawer, '-webkit-transform', transform);
825+
}
826+
if (opacity !== undefined) {
827+
this.renderer.setElementStyle(this.overlay, 'opacity', opacity);
828+
}
829+
});
839830
}
840831

841832
private toggleOpenedEvent = (evt?) => {

0 commit comments

Comments
 (0)