Skip to content

Commit 20e70e6

Browse files
committed
fix(nav-drawer): move isPlatformBrowser to util, #4426
1 parent 628269d commit 20e70e6

File tree

6 files changed

+70
-25
lines changed

6 files changed

+70
-25
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Inject, Injectable, NgZone, PLATFORM_ID } from '@angular/core';
1+
import { Inject, Injectable, NgZone } from '@angular/core';
22
import { ɵgetDOM as getDOM } from '@angular/platform-browser';
3-
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
3+
import { DOCUMENT } from '@angular/common';
4+
import { PlatformUtil } from './utils';
45

56
const EVENT_SUFFIX = 'precise';
67

@@ -19,8 +20,8 @@ export class HammerGesturesManager {
1920

2021
private _hammerManagers: Array<{ element: EventTarget, manager: HammerManager; }> = [];
2122

22-
constructor(private _zone: NgZone, @Inject(DOCUMENT) private doc: any) {
23-
this.platformBrowser = isPlatformBrowser(PLATFORM_ID);
23+
constructor(private _zone: NgZone, @Inject(DOCUMENT) private doc: any, private platformUtil: PlatformUtil) {
24+
this.platformBrowser = this.platformUtil.isPlatformBrowser();
2425
if (this.platformBrowser) {
2526
this.hammerOptions = {
2627
// 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: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Injectable, PLATFORM_ID, Inject } from '@angular/core';
2+
import { isPlatformBrowser } from '@angular/common';
3+
14
/**
25
*@hidden
36
*/
@@ -230,11 +233,22 @@ export function isFirefox(): boolean {
230233
* @hidden
231234
* TODO: make injectable, check isPlatformBrowser()
232235
*/
236+
@Injectable({ providedIn: 'root' })
233237
export class PlatformUtil {
234-
static isIOS(): boolean {
235-
const iosBrowser = typeof window !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent) && !('MSStream' in window);
238+
private platformBrowser: boolean;
239+
240+
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
241+
this.platformBrowser = isPlatformBrowser(this.platformId);
242+
}
243+
244+
public isIOS(): boolean {
245+
const iosBrowser = this.platformBrowser && /iPad|iPhone|iPod/.test(navigator.userAgent) && !('MSStream' in window);
236246
return iosBrowser;
237247
}
248+
249+
public isPlatformBrowser(): boolean {
250+
return this.platformBrowser;
251+
}
238252
}
239253

240254
/**
@@ -246,8 +260,21 @@ export function isLeftClick(event: PointerEvent) {
246260

247261
/** @hidden */
248262
export function isNavigationKey(key: string): boolean {
249-
return ['down', 'up', 'left', 'right', 'arrowdown', 'arrowup', 'arrowleft', 'arrowright',
250-
'home', 'end', 'space', 'spacebar', ' '].indexOf(key) !== -1;
263+
return [
264+
'down',
265+
'up',
266+
'left',
267+
'right',
268+
'arrowdown',
269+
'arrowup',
270+
'arrowleft',
271+
'arrowright',
272+
'home',
273+
'end',
274+
'space',
275+
'spacebar',
276+
' '
277+
].indexOf(key) !== -1;
251278
}
252279

253280
/**
@@ -285,8 +312,21 @@ export interface CancelableBrowserEventArgs extends CancelableEventArgs {
285312
event?: Event;
286313
}
287314

288-
export const NAVIGATION_KEYS = new Set(['down', 'up', 'left', 'right', 'arrowdown', 'arrowup', 'arrowleft', 'arrowright',
289-
'home', 'end', 'space', 'spacebar', ' ']);
315+
export const NAVIGATION_KEYS = new Set([
316+
'down',
317+
'up',
318+
'left',
319+
'right',
320+
'arrowdown',
321+
'arrowup',
322+
'arrowleft',
323+
'arrowright',
324+
'home',
325+
'end',
326+
'space',
327+
'spacebar',
328+
' '
329+
]);
290330
export const ROW_EXPAND_KEYS = new Set('right down arrowright arrowdown'.split(' '));
291331
export const ROW_COLLAPSE_KEYS = new Set('left up arrowleft arrowup'.split(' '));
292332
export const SUPPORTED_KEYS = new Set([...Array.from(NAVIGATION_KEYS), 'tab', 'enter', 'f2', 'escape', 'esc']);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,8 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
555555
public cdr: ChangeDetectorRef,
556556
private element: ElementRef,
557557
protected zone: NgZone,
558-
private touchManager: HammerGesturesManager) { }
558+
private touchManager: HammerGesturesManager,
559+
protected platformUtil: PlatformUtil) { }
559560

560561
private addPointerListeners(selection) {
561562
if (selection !== GridSelectionMode.multiple) { return; }
@@ -587,7 +588,7 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
587588
this.nativeElement.addEventListener('compositionend', this.compositionEndHandler);
588589
}
589590
});
590-
if (PlatformUtil.isIOS()) {
591+
if (this.platformUtil.isIOS()) {
591592
this.touchManager.addEventListener(this.nativeElement, 'doubletap', this.onDoubleClick, {
592593
cssProps: { } /* don't disable user-select, etc */
593594
} as HammerOptions);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IgxHierarchicalGridComponent } from './hierarchical-grid.component';
66
// import { IgxHierarchicalSelectionAPIService } from './selection';
77
import { IgxGridSelectionService, IgxGridCRUDService } from '../../core/grid-selection';
88
import { HammerGesturesManager } from '../../core/touch';
9+
import { PlatformUtil } from '../../core/utils';
910

1011
@Component({
1112
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -27,9 +28,10 @@ export class IgxHierarchicalGridCellComponent extends IgxGridCellComponent imple
2728
public cdr: ChangeDetectorRef,
2829
private helement: ElementRef,
2930
protected zone: NgZone,
30-
touchManager: HammerGesturesManager
31+
touchManager: HammerGesturesManager,
32+
protected platformUtil: PlatformUtil
3133
) {
32-
super(selectionService, crudService, gridAPI, cdr, helement, zone, touchManager);
34+
super(selectionService, crudService, gridAPI, cdr, helement, zone, touchManager, platformUtil);
3335
// this.hSelection = <IgxHierarchicalSelectionAPIService>selection;
3436
}
3537

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Component, ChangeDetectorRef, ElementRef, ViewChild, Inject,
33
import { IgxGridCellComponent } from '../cell.component';
44
import { IgxTreeGridAPIService } from './tree-grid-api.service';
55
import { GridBaseAPIService } from '../api.service';
6-
import { getNodeSizeViaRange } from '../../core/utils';
6+
import { getNodeSizeViaRange, PlatformUtil } from '../../core/utils';
77
import { DOCUMENT } from '@angular/common';
88
import { IgxGridBaseComponent, IGridDataBindable } from '../grid';
99
import { IgxGridSelectionService, IgxGridCRUDService } from '../../core/grid-selection';
@@ -26,8 +26,9 @@ export class IgxTreeGridCellComponent extends IgxGridCellComponent implements On
2626
element: ElementRef,
2727
protected zone: NgZone,
2828
touchManager: HammerGesturesManager,
29-
@Inject(DOCUMENT) public document) {
30-
super(selectionService, crudService, gridAPI, cdr, element, zone, touchManager);
29+
@Inject(DOCUMENT) public document,
30+
protected platformUtil: PlatformUtil) {
31+
super(selectionService, crudService, gridAPI, cdr, element, zone, touchManager, platformUtil);
3132
this.treeGridAPI = <IgxTreeGridAPIService>gridAPI;
3233
}
3334

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ import {
1414
Output,
1515
Renderer,
1616
SimpleChange,
17-
ViewChild,
18-
PLATFORM_ID
17+
ViewChild
1918
} from '@angular/core';
2019
import { fromEvent, interval, Subscription } from 'rxjs';
2120
import { debounce } from 'rxjs/operators';
2221
import { IgxNavigationService, IToggleView } from '../core/navigation';
2322
import { HammerGesturesManager } from '../core/touch';
2423
import { IgxNavDrawerMiniTemplateDirective, IgxNavDrawerTemplateDirective } from './navigation-drawer.directives';
25-
import { isPlatformBrowser } from '@angular/common';
24+
import { PlatformUtil } from '../core/utils';
2625

2726
let NEXT_ID = 0;
2827
/**
@@ -419,7 +418,8 @@ export class IgxNavigationDrawerComponent implements
419418
@Optional() private _state: IgxNavigationService,
420419
// private animate: AnimationBuilder, TODO
421420
protected renderer: Renderer,
422-
private _touchManager: HammerGesturesManager) {
421+
private _touchManager: HammerGesturesManager,
422+
private platformUtil: PlatformUtil) {
423423
}
424424

425425
/**
@@ -614,7 +614,7 @@ export class IgxNavigationDrawerComponent implements
614614
}
615615

616616
private getWindowWidth() {
617-
if (!isPlatformBrowser(PLATFORM_ID)) {
617+
if (!this.platformUtil.isPlatformBrowser()) {
618618
return;
619619
}
620620

@@ -625,7 +625,7 @@ export class IgxNavigationDrawerComponent implements
625625
* Sets the drawer width.
626626
*/
627627
private setDrawerWidth(width: string) {
628-
if (isPlatformBrowser(PLATFORM_ID)) {
628+
if (this.platformUtil.isPlatformBrowser()) {
629629
requestAnimationFrame(() => {
630630
if (this.drawer) {
631631
this.renderer.setElementStyle(this.drawer, 'width', width);
@@ -659,7 +659,7 @@ export class IgxNavigationDrawerComponent implements
659659
this._touchManager.addGlobalEventListener('document', 'panmove', this.pan);
660660
this._touchManager.addGlobalEventListener('document', 'panend', this.panEnd);
661661
}
662-
if (!this._resizeObserver && isPlatformBrowser(PLATFORM_ID)) {
662+
if (!this._resizeObserver && this.platformUtil.isPlatformBrowser()) {
663663
this._resizeObserver = fromEvent(window, 'resize').pipe(debounce(() => interval(150)))
664664
.subscribe((value) => {
665665
this.checkPinThreshold(value);
@@ -815,7 +815,7 @@ export class IgxNavigationDrawerComponent implements
815815
* @param opacity optional value to apply to the overlay
816816
*/
817817
private setXSize(x: number, opacity?: string) {
818-
if (isPlatformBrowser(PLATFORM_ID)) {
818+
if (this.platformUtil.isPlatformBrowser()) {
819819
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
820820
window.requestAnimationFrame(() => {
821821
this.setXSizeInternal(x, opacity);

0 commit comments

Comments
 (0)