Skip to content

Commit 0eee4eb

Browse files
committed
fix(nav-drawer): bypass all calls to browsers' object, #4426
1 parent a4d960c commit 0eee4eb

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
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 & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import {
1414
Output,
1515
Renderer,
1616
SimpleChange,
17-
TemplateRef,
18-
ViewChild
17+
ViewChild,
18+
PLATFORM_ID
1919
} from '@angular/core';
2020
import { fromEvent, interval, Subscription } from 'rxjs';
2121
import { debounce } from 'rxjs/operators';
2222
import { IgxNavigationService, IToggleView } from '../core/navigation';
2323
import { HammerGesturesManager } from '../core/touch';
2424
import { IgxNavDrawerMiniTemplateDirective, IgxNavDrawerTemplateDirective } from './navigation-drawer.directives';
25+
import { isPlatformBrowser } from '@angular/common';
2526

2627
let NEXT_ID = 0;
2728
/**
@@ -595,18 +596,26 @@ export class IgxNavigationDrawerComponent implements
595596
}
596597

597598
private getWindowWidth() {
599+
if (!isPlatformBrowser(PLATFORM_ID)) {
600+
return;
601+
}
602+
598603
return (window.innerWidth > 0) ? window.innerWidth : screen.width;
599604
}
600605

601606
/**
602607
* Sets the drawer width.
603608
*/
604609
private setDrawerWidth(width: string) {
605-
requestAnimationFrame(() => {
606-
if (this.drawer) {
607-
this.renderer.setElementStyle(this.drawer, 'width', width);
608-
}
609-
});
610+
if (isPlatformBrowser(PLATFORM_ID)) {
611+
requestAnimationFrame(() => {
612+
if (this.drawer) {
613+
this.renderer.setElementStyle(this.drawer, 'width', width);
614+
}
615+
});
616+
} else {
617+
this.renderer.setElementStyle(this.drawer, 'width', width);
618+
}
610619
}
611620

612621
/**
@@ -632,7 +641,7 @@ export class IgxNavigationDrawerComponent implements
632641
this._touchManager.addGlobalEventListener('document', 'panmove', this.pan);
633642
this._touchManager.addGlobalEventListener('document', 'panend', this.panEnd);
634643
}
635-
if (!this._resizeObserver) {
644+
if (!this._resizeObserver && isPlatformBrowser(PLATFORM_ID)) {
636645
this._resizeObserver = fromEvent(window, 'resize').pipe(debounce(() => interval(150)))
637646
.subscribe((value) => {
638647
this.checkPinThreshold(value);
@@ -788,19 +797,27 @@ export class IgxNavigationDrawerComponent implements
788797
* @param opacity optional value to apply to the overlay
789798
*/
790799
private setXSize(x: number, opacity?: string) {
791-
// Angular polyfills patches window.requestAnimationFrame, but switch to DomAdapter API (TODO)
792-
window.requestAnimationFrame(() => {
793-
if (this.hasAnimateWidth) {
794-
this.renderer.setElementStyle(this.drawer, 'width', x ? Math.abs(x) + 'px' : '');
795-
} else {
796-
this.renderer.setElementStyle(this.drawer, 'transform', x ? 'translate3d(' + x + 'px,0,0)' : '');
797-
this.renderer.setElementStyle(this.drawer, '-webkit-transform',
798-
x ? 'translate3d(' + x + 'px,0,0)' : '');
799-
}
800-
if (opacity !== undefined) {
801-
this.renderer.setElementStyle(this.overlay, 'opacity', opacity);
802-
}
803-
});
800+
if (isPlatformBrowser(PLATFORM_ID)) {
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+
}
804821
}
805822

806823
private toggleOpenedEvent = (evt?) => {

0 commit comments

Comments
 (0)