Skip to content

Commit d5a6297

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 3d7a063 commit d5a6297

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
@@ -561,7 +561,7 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
561561
this.nativeElement.addEventListener('compositionend', this.compositionEndHandler);
562562
}
563563
});
564-
if (this.platformUtil.isIOS()) {
564+
if (this.platformUtil.isIOS) {
565565
this.touchManager.addEventListener(this.nativeElement, 'doubletap', this.onDoubleClick, {
566566
cssProps: { } /* don't disable user-select, etc */
567567
} 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
@@ -193,15 +193,15 @@ describe('IgxGrid - Cell component', () => {
193193

194194
it('Should not attach doubletap handler for non-iOS', () => {
195195
const addListenerSpy = spyOn(HammerGesturesManager.prototype, 'addEventListener');
196-
spyOn(PlatformUtil.prototype, 'isIOS').and.returnValue(false);
196+
spyOnProperty(PlatformUtil.prototype, 'isIOS').and.returnValue(false);
197197
const fix = TestBed.createComponent(DefaultGridComponent);
198198
fix.detectChanges();
199199
expect(addListenerSpy).not.toHaveBeenCalled();
200200
});
201201

202202
it('Should handle doubletap on iOS, trigger onDoubleClick event', () => {
203203
const addListenerSpy = spyOn(HammerGesturesManager.prototype, 'addEventListener');
204-
spyOn(PlatformUtil.prototype, 'isIOS').and.returnValue(true);
204+
spyOnProperty(PlatformUtil.prototype, 'isIOS').and.returnValue(true);
205205
const fix = TestBed.createComponent(DefaultGridComponent);
206206
fix.detectChanges();
207207

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
@@ -596,18 +596,14 @@ export class IgxNavigationDrawerComponent implements
596596
}
597597

598598
private getWindowWidth() {
599-
if (!this.platformUtil.isPlatformBrowser()) {
600-
return;
601-
}
602-
603599
return (window.innerWidth > 0) ? window.innerWidth : screen.width;
604600
}
605601

606602
/**
607603
* Sets the drawer width.
608604
*/
609605
private setDrawerWidth(width: string) {
610-
if (this.platformUtil.isPlatformBrowser()) {
606+
if (this.platformUtil.isPlatformBrowser) {
611607
requestAnimationFrame(() => {
612608
if (this.drawer) {
613609
this.renderer.setElementStyle(this.drawer, 'width', width);
@@ -641,7 +637,7 @@ export class IgxNavigationDrawerComponent implements
641637
this._touchManager.addGlobalEventListener('document', 'panmove', this.pan);
642638
this._touchManager.addGlobalEventListener('document', 'panend', this.panEnd);
643639
}
644-
if (!this._resizeObserver && this.platformUtil.isPlatformBrowser()) {
640+
if (!this._resizeObserver && this.platformUtil.isPlatformBrowser) {
645641
this._resizeObserver = fromEvent(window, 'resize').pipe(debounce(() => interval(150)))
646642
.subscribe((value) => {
647643
this.checkPinThreshold(value);
@@ -659,6 +655,9 @@ export class IgxNavigationDrawerComponent implements
659655
}
660656

661657
private checkPinThreshold = (evt?: Event) => {
658+
if (!this.platformUtil.isPlatformBrowser) {
659+
return;
660+
}
662661
let windowWidth;
663662
if (this.pinThreshold) {
664663
windowWidth = this.getWindowWidth();
@@ -797,27 +796,19 @@ export class IgxNavigationDrawerComponent implements
797796
* @param opacity optional value to apply to the overlay
798797
*/
799798
private setXSize(x: number, opacity?: string) {
800-
if (this.platformUtil.isPlatformBrowser()) {
801-
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
802-
window.requestAnimationFrame(() => {
803-
this.setXSizeInternal(x, opacity);
804-
});
805-
} else {
806-
this.setXSizeInternal(x, opacity);
807-
}
808-
}
809-
810-
private setXSizeInternal(x: number, opacity?: string) {
811-
if (this.hasAnimateWidth) {
812-
this.renderer.setElementStyle(this.drawer, 'width', x ? Math.abs(x) + 'px' : '');
813-
} else {
814-
this.renderer.setElementStyle(this.drawer, 'transform', x ? 'translate3d(' + x + 'px,0,0)' : '');
815-
this.renderer.setElementStyle(this.drawer, '-webkit-transform',
816-
x ? 'translate3d(' + x + 'px,0,0)' : '');
817-
}
818-
if (opacity !== undefined) {
819-
this.renderer.setElementStyle(this.overlay, 'opacity', opacity);
820-
}
799+
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
800+
window.requestAnimationFrame(() => {
801+
if (this.hasAnimateWidth) {
802+
this.renderer.setElementStyle(this.drawer, 'width', x ? Math.abs(x) + 'px' : '');
803+
} else {
804+
const transform = x ? 'translate3d(' + x + 'px,0,0)' : '';
805+
this.renderer.setElementStyle(this.drawer, 'transform', transform);
806+
this.renderer.setElementStyle(this.drawer, '-webkit-transform', transform);
807+
}
808+
if (opacity !== undefined) {
809+
this.renderer.setElementStyle(this.overlay, 'opacity', opacity);
810+
}
811+
});
821812
}
822813

823814
private toggleOpenedEvent = (evt?) => {

0 commit comments

Comments
 (0)