Skip to content

Commit 4fec937

Browse files
committed
fix(nav-drawer): move isPlatformBrowser to util, #4426
1 parent 0eee4eb commit 4fec937

File tree

6 files changed

+71
-26
lines changed

6 files changed

+71
-26
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
/**
@@ -278,8 +305,21 @@ export interface CancelableBrowserEventArgs extends CancelableEventArgs {
278305
event?: Event;
279306
}
280307

281-
export const NAVIGATION_KEYS = new Set(['down', 'up', 'left', 'right', 'arrowdown', 'arrowup', 'arrowleft', 'arrowright',
282-
'home', 'end', 'space', 'spacebar', ' ']);
308+
export const NAVIGATION_KEYS = new Set([
309+
'down',
310+
'up',
311+
'left',
312+
'right',
313+
'arrowdown',
314+
'arrowup',
315+
'arrowleft',
316+
'arrowright',
317+
'home',
318+
'end',
319+
'space',
320+
'spacebar',
321+
' '
322+
]);
283323
export const ROW_EXPAND_KEYS = new Set('right down arrowright arrowdown'.split(' '));
284324
export const ROW_COLLAPSE_KEYS = new Set('left up arrowleft arrowup'.split(' '));
285325
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
@@ -538,7 +538,8 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
538538
public cdr: ChangeDetectorRef,
539539
private element: ElementRef,
540540
protected zone: NgZone,
541-
private touchManager: HammerGesturesManager) { }
541+
private touchManager: HammerGesturesManager,
542+
protected platformUtil: PlatformUtil) { }
542543

543544

544545
/**
@@ -560,7 +561,7 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
560561
this.nativeElement.addEventListener('compositionend', this.compositionEndHandler);
561562
}
562563
});
563-
if (PlatformUtil.isIOS()) {
564+
if (this.platformUtil.isIOS()) {
564565
this.touchManager.addEventListener(this.nativeElement, 'doubletap', this.onDoubleClick, {
565566
cssProps: { } /* don't disable user-select, etc */
566567
} as HammerOptions);

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

Lines changed: 5 additions & 3 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,10 +28,11 @@ 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, selection, cdr, helement, zone, touchManager);
33-
this.hSelection = <IgxHierarchicalSelectionAPIService>selection;
34+
super(selectionService, crudService, gridAPI, selection, cdr, helement, zone, touchManager, platformUtil);
35+
// this.hSelection = <IgxHierarchicalSelectionAPIService>selection;
3436
}
3537

3638
ngOnInit() {

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 { IgxGridCellComponent } from '../cell.component';
33
import { IgxTreeGridAPIService } from './tree-grid-api.service';
44
import { GridBaseAPIService } from '../api.service';
55
import { IgxSelectionAPIService } from '../../core/selection';
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';
@@ -27,8 +27,9 @@ export class IgxTreeGridCellComponent extends IgxGridCellComponent implements On
2727
element: ElementRef,
2828
protected zone: NgZone,
2929
touchManager: HammerGesturesManager,
30-
@Inject(DOCUMENT) public document) {
31-
super(selectionService, crudService, gridAPI, selection, cdr, element, zone, touchManager);
30+
@Inject(DOCUMENT) public document,
31+
protected platformUtil: PlatformUtil) {
32+
super(selectionService, crudService, gridAPI, selection, cdr, element, zone, touchManager, platformUtil);
3233
this.treeGridAPI = <IgxTreeGridAPIService>gridAPI;
3334
}
3435

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
/**
@@ -401,7 +400,8 @@ export class IgxNavigationDrawerComponent implements
401400
@Optional() private _state: IgxNavigationService,
402401
// private animate: AnimationBuilder, TODO
403402
protected renderer: Renderer,
404-
private _touchManager: HammerGesturesManager) {
403+
private _touchManager: HammerGesturesManager,
404+
private platformUtil: PlatformUtil) {
405405
}
406406

407407
/**
@@ -596,7 +596,7 @@ export class IgxNavigationDrawerComponent implements
596596
}
597597

598598
private getWindowWidth() {
599-
if (!isPlatformBrowser(PLATFORM_ID)) {
599+
if (!this.platformUtil.isPlatformBrowser()) {
600600
return;
601601
}
602602

@@ -607,7 +607,7 @@ export class IgxNavigationDrawerComponent implements
607607
* Sets the drawer width.
608608
*/
609609
private setDrawerWidth(width: string) {
610-
if (isPlatformBrowser(PLATFORM_ID)) {
610+
if (this.platformUtil.isPlatformBrowser()) {
611611
requestAnimationFrame(() => {
612612
if (this.drawer) {
613613
this.renderer.setElementStyle(this.drawer, 'width', width);
@@ -641,7 +641,7 @@ export class IgxNavigationDrawerComponent implements
641641
this._touchManager.addGlobalEventListener('document', 'panmove', this.pan);
642642
this._touchManager.addGlobalEventListener('document', 'panend', this.panEnd);
643643
}
644-
if (!this._resizeObserver && isPlatformBrowser(PLATFORM_ID)) {
644+
if (!this._resizeObserver && this.platformUtil.isPlatformBrowser()) {
645645
this._resizeObserver = fromEvent(window, 'resize').pipe(debounce(() => interval(150)))
646646
.subscribe((value) => {
647647
this.checkPinThreshold(value);
@@ -797,7 +797,7 @@ export class IgxNavigationDrawerComponent implements
797797
* @param opacity optional value to apply to the overlay
798798
*/
799799
private setXSize(x: number, opacity?: string) {
800-
if (isPlatformBrowser(PLATFORM_ID)) {
800+
if (this.platformUtil.isPlatformBrowser()) {
801801
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
802802
window.requestAnimationFrame(() => {
803803
this.setXSizeInternal(x, opacity);

0 commit comments

Comments
 (0)