Skip to content

Commit 628269d

Browse files
committed
fix(nav-drawer): bypass all calls to browsers' object, #4426
1 parent 2eff952 commit 628269d

File tree

2 files changed

+70
-39
lines changed

2 files changed

+70
-39
lines changed

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

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

55
const EVENT_SUFFIX = 'precise';
66

@@ -11,26 +11,31 @@ const EVENT_SUFFIX = 'precise';
1111
*/
1212
@Injectable()
1313
export class HammerGesturesManager {
14+
private platformBrowser: boolean;
1415
/**
1516
* Event option defaults for each recognizer, see http://hammerjs.github.io/api/ for API listing.
1617
*/
17-
protected hammerOptions: HammerOptions = {
18-
// D.P. #447 Force TouchInput due to PointerEventInput bug (https://github.com/hammerjs/hammer.js/issues/1065)
19-
// see https://github.com/IgniteUI/igniteui-angular/issues/447#issuecomment-324601803
20-
inputClass: Hammer.TouchInput,
21-
recognizers: [
22-
[ Hammer.Pan, { threshold: 0 } ],
23-
[ Hammer.Swipe, {
24-
direction: Hammer.DIRECTION_HORIZONTAL
25-
}],
26-
[Hammer.Tap],
27-
[Hammer.Tap, { event: 'doubletap', taps: 2 }, ['tap']]
28-
]
29-
};
18+
protected hammerOptions: HammerOptions = {};
3019

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

3322
constructor(private _zone: NgZone, @Inject(DOCUMENT) private doc: any) {
23+
this.platformBrowser = isPlatformBrowser(PLATFORM_ID);
24+
if (this.platformBrowser) {
25+
this.hammerOptions = {
26+
// D.P. #447 Force TouchInput due to PointerEventInput bug (https://github.com/hammerjs/hammer.js/issues/1065)
27+
// see https://github.com/IgniteUI/igniteui-angular/issues/447#issuecomment-324601803
28+
inputClass: Hammer.TouchInput,
29+
recognizers: [
30+
[Hammer.Pan, { threshold: 0 }],
31+
[Hammer.Swipe, {
32+
direction: Hammer.DIRECTION_HORIZONTAL
33+
}],
34+
[Hammer.Tap],
35+
[Hammer.Tap, { event: 'doubletap', taps: 2 }, ['tap']]
36+
]
37+
};
38+
}
3439
}
3540

3641
public supports(eventName: string): boolean {
@@ -41,10 +46,14 @@ export class HammerGesturesManager {
4146
* Add listener extended with options for Hammer.js. Will use defaults if none are provided.
4247
* Modeling after other event plugins for easy future modifications.
4348
*/
44-
public addEventListener(element: HTMLElement,
45-
eventName: string,
46-
eventHandler: (eventObj) => void,
47-
options: HammerOptions = null): () => void {
49+
public addEventListener(
50+
element: HTMLElement,
51+
eventName: string,
52+
eventHandler: (eventObj) => void,
53+
options: HammerOptions = null): () => void {
54+
if (!this.platformBrowser) {
55+
return;
56+
}
4857

4958
// Creating the manager bind events, must be done outside of angular
5059
return this._zone.runOutsideAngular(() => {
@@ -67,6 +76,10 @@ export class HammerGesturesManager {
6776
* @param target Can be one of either window, body or document(fallback default).
6877
*/
6978
public addGlobalEventListener(target: string, eventName: string, eventHandler: (eventObj) => void): () => void {
79+
if (!this.platformBrowser) {
80+
return;
81+
}
82+
7083
const element = this.getGlobalEventTarget(target);
7184

7285
// Creating the manager bind events, must be done outside of angular

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import {
1414
Output,
1515
Renderer,
1616
SimpleChange,
17-
ViewChild
17+
ViewChild,
18+
PLATFORM_ID
1819
} from '@angular/core';
1920
import { fromEvent, interval, Subscription } from 'rxjs';
2021
import { debounce } from 'rxjs/operators';
2122
import { IgxNavigationService, IToggleView } from '../core/navigation';
2223
import { HammerGesturesManager } from '../core/touch';
2324
import { IgxNavDrawerMiniTemplateDirective, IgxNavDrawerTemplateDirective } from './navigation-drawer.directives';
25+
import { isPlatformBrowser } from '@angular/common';
2426

2527
let NEXT_ID = 0;
2628
/**
@@ -612,18 +614,26 @@ export class IgxNavigationDrawerComponent implements
612614
}
613615

614616
private getWindowWidth() {
617+
if (!isPlatformBrowser(PLATFORM_ID)) {
618+
return;
619+
}
620+
615621
return (window.innerWidth > 0) ? window.innerWidth : screen.width;
616622
}
617623

618624
/**
619625
* Sets the drawer width.
620626
*/
621627
private setDrawerWidth(width: string) {
622-
requestAnimationFrame(() => {
623-
if (this.drawer) {
624-
this.renderer.setElementStyle(this.drawer, 'width', width);
625-
}
626-
});
628+
if (isPlatformBrowser(PLATFORM_ID)) {
629+
requestAnimationFrame(() => {
630+
if (this.drawer) {
631+
this.renderer.setElementStyle(this.drawer, 'width', width);
632+
}
633+
});
634+
} else {
635+
this.renderer.setElementStyle(this.drawer, 'width', width);
636+
}
627637
}
628638

629639
/**
@@ -649,7 +659,7 @@ export class IgxNavigationDrawerComponent implements
649659
this._touchManager.addGlobalEventListener('document', 'panmove', this.pan);
650660
this._touchManager.addGlobalEventListener('document', 'panend', this.panEnd);
651661
}
652-
if (!this._resizeObserver) {
662+
if (!this._resizeObserver && isPlatformBrowser(PLATFORM_ID)) {
653663
this._resizeObserver = fromEvent(window, 'resize').pipe(debounce(() => interval(150)))
654664
.subscribe((value) => {
655665
this.checkPinThreshold(value);
@@ -805,19 +815,27 @@ export class IgxNavigationDrawerComponent implements
805815
* @param opacity optional value to apply to the overlay
806816
*/
807817
private setXSize(x: number, opacity?: string) {
808-
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
809-
window.requestAnimationFrame(() => {
810-
if (this.hasAnimateWidth) {
811-
this.renderer.setElementStyle(this.drawer, 'width', x ? Math.abs(x) + 'px' : '');
812-
} else {
813-
this.renderer.setElementStyle(this.drawer, 'transform', x ? 'translate3d(' + x + 'px,0,0)' : '');
814-
this.renderer.setElementStyle(this.drawer, '-webkit-transform',
815-
x ? 'translate3d(' + x + 'px,0,0)' : '');
816-
}
817-
if (opacity !== undefined) {
818-
this.renderer.setElementStyle(this.overlay, 'opacity', opacity);
819-
}
820-
});
818+
if (isPlatformBrowser(PLATFORM_ID)) {
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+
}
821839
}
822840

823841
private toggleOpenedEvent = (evt?) => {

0 commit comments

Comments
 (0)