Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

Commit

Permalink
fix(universal): workaround incomplete parse5 adapter
Browse files Browse the repository at this point in the history
 - DOM.query is not implemented in Parse5 adapter.  Add a method to the ViewportHelper class that returns the root document element, and use DOM.querySelector from there.
  • Loading branch information
justindujardin committed Apr 10, 2016
1 parent 3566d67 commit 0cf5608
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ng2-material/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export * from './components/tabs/tabs';

export * from './core/util/media';

import {ViewportHelper, BrowserViewportHelper} from "./core/util/viewport";
import {ViewportHelper, BrowserViewportHelper, NodeViewportHelper} from "./core/util/viewport";
import {provide} from "angular2/core";
export * from './core/util/viewport';
export * from './core/util/animate';
Expand Down Expand Up @@ -111,6 +111,7 @@ export const MATERIAL_DIRECTIVES: Type[] = CONST_EXPR([
* Material Design component providers for use in a Node.JS environment.
*/
export const MATERIAL_NODE_PROVIDERS: any[] = CONST_EXPR([
provide(ViewportHelper, {useClass: NodeViewportHelper}),
MdDialog,
Media,
SidenavService,
Expand Down
6 changes: 4 additions & 2 deletions ng2-material/components/backdrop/backdrop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Animate} from "../../core/util/animate";
import {ElementRef, ViewEncapsulation, Component, Input, Output, EventEmitter} from "angular2/core";
import {DOM} from "angular2/src/platform/dom/dom_adapter";
import {ViewportHelper} from "../../core/util/viewport";

/**
* An overlay for content on the page.
Expand Down Expand Up @@ -55,7 +56,8 @@ export class MdBackdrop {
@Output()
onShown: EventEmitter<MdBackdrop> = new EventEmitter<MdBackdrop>();

constructor(public element: ElementRef) {
constructor(public element: ElementRef, public viewport: ViewportHelper) {
this._body = DOM.querySelector(viewport.getDocumentNativeElement(), 'body');
}

/**
Expand Down Expand Up @@ -87,7 +89,7 @@ export class MdBackdrop {
private _visible: boolean = false;
private _transitioning: boolean = false;
private _previousOverflow: string = null;
private _body: HTMLBodyElement = DOM.query('body');
private _body: HTMLElement = null;

onClick() {
if (this.clickClose && !this._transitioning && this.visible) {
Expand Down
4 changes: 3 additions & 1 deletion ng2-material/components/sidenav/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {MdBackdrop} from "../backdrop/backdrop";
import {CONST} from "angular2/src/facade/lang";
import {SidenavService} from "./sidenav_service";
import {TimerWrapper} from "angular2/src/facade/async";
import {ViewportHelper} from "../../core/util/viewport";


@CONST()
Expand Down Expand Up @@ -129,12 +130,13 @@ export class MdSidenav extends MdBackdrop implements OnInit, OnDestroy {
transitionAddClass = false;

constructor(public element: ElementRef,
public viewport: ViewportHelper,
@Inject(forwardRef(() => SidenavService))
public service: SidenavService,
public renderer: Renderer,
@Optional() @SkipSelf() @Host() @Inject(forwardRef(() => MdSidenavContainer))
public container: MdSidenavContainer) {
super(element);
super(element, viewport);
this.renderer.setElementClass(this.element.nativeElement, this.transitionClass, !this.transitionAddClass);
}

Expand Down
42 changes: 39 additions & 3 deletions ng2-material/core/util/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ export abstract class ViewportHelper {

abstract matchMedia(query: string): ViewportMediaMatch;

abstract scrollTop():number;

abstract requestFrame(fn:(elapsed?:number)=>any);
abstract scrollTop(): number;
abstract getDocumentNativeElement(): any;

abstract requestFrame(fn: (elapsed?: number)=>any);
}

export class BrowserViewportHelper extends ViewportHelper {
getDocumentNativeElement(): any {
return window.document;
}
requestFrame(fn: (elapsed?: number)=>any) {
return window.requestAnimationFrame(fn);
}

matchMedia(query: string): ViewportMediaMatch {
return window.matchMedia(query);
}
Expand All @@ -35,3 +40,34 @@ export class BrowserViewportHelper extends ViewportHelper {
}

}

export class NodeViewportMediaMatch implements ViewportMediaMatch {
constructor(public matches: boolean = false, public media: string = '') {

}

addListener(listener: MediaQueryListListener): void {
}

removeListener(listener: MediaQueryListListener): void {
}

}

export class NodeViewportHelper extends ViewportHelper {
getDocumentNativeElement(): any {
return {};
}
requestFrame(fn: (elapsed?: number)=>any) {
return process.nextTick(fn);
}

matchMedia(query: string): ViewportMediaMatch {
return new NodeViewportMediaMatch(false, query);
}

scrollTop(): number {
return 0;
}

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ng2-material",
"version": "0.3.4",
"description": "",
"main": "all.ts",
"main": "dist/ng2-material.js",
"repository": {
"type": "git",
"url": "git@github.com:justindujardin/ng2-material.git"
Expand Down
1 change: 0 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"ng2-material/core/util/util.ts",
"ng2-material/core/util/viewport.ts",
"ng2-material/webpack_all.ts",
"ng2-material/webpack_scripts.ts",
"ng2-material/webpack_styles.ts"
]
}
8 changes: 2 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"version": "1.6.0",
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"filesGlob": [
Expand Down

0 comments on commit 0cf5608

Please sign in to comment.