Skip to content

Commit

Permalink
fix(ssr): fix angular global window and document references
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Feb 25, 2019
1 parent ceaef7e commit f44c17e
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 187 deletions.
6 changes: 3 additions & 3 deletions angular/src/app-initialize.ts
Expand Up @@ -3,10 +3,10 @@ import { defineCustomElements } from '@ionic/core/loader';
import { Config } from './providers/config';
import { IonicWindow } from './types/interfaces';

export function appInitialize(config: Config) {
export function appInitialize(config: Config, doc: Document) {
return (): any => {
const win: IonicWindow | undefined = window as any;
if (typeof win !== 'undefined') {
const win: IonicWindow | undefined = doc.defaultView as any;
if (win) {
const Ionic = win.Ionic = win.Ionic || {};

Ionic.config = config;
Expand Down
12 changes: 7 additions & 5 deletions angular/src/directives/navigation/ion-router-outlet.ts
Expand Up @@ -82,11 +82,13 @@ export class IonRouterOutlet implements OnDestroy, OnInit {
this.activateWith(context.route, context.resolver || null);
}
}
this.nativeEl.componentOnReady().then(() => {
if (this._swipeGesture === undefined) {
this.swipeGesture = this.config.getBoolean('swipeBackEnabled', this.nativeEl.mode === 'ios');
}
});
if ((this.nativeEl as any).componentOnReady) {
this.nativeEl.componentOnReady().then(() => {
if (this._swipeGesture === undefined) {
this.swipeGesture = this.config.getBoolean('swipeBackEnabled', this.nativeEl.mode === 'ios');
}
});
}
}

get isActivated(): boolean {
Expand Down
5 changes: 3 additions & 2 deletions angular/src/ionic-module.ts
@@ -1,4 +1,4 @@
import { CommonModule } from '@angular/common';
import { CommonModule, DOCUMENT } from '@angular/common';
import { APP_INITIALIZER, ModuleWithProviders, NgModule } from '@angular/core';
import { IonicConfig } from '@ionic/core';

Expand Down Expand Up @@ -141,7 +141,8 @@ export class IonicModule {
useFactory: appInitialize,
multi: true,
deps: [
ConfigToken
ConfigToken,
DOCUMENT
]
}
]
Expand Down
7 changes: 4 additions & 3 deletions angular/src/providers/action-sheet-controller.ts
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { ActionSheetOptions } from '@ionic/core';

import { OverlayBaseController } from '../util/overlay';
Expand All @@ -7,7 +8,7 @@ import { OverlayBaseController } from '../util/overlay';
providedIn: 'root',
})
export class ActionSheetController extends OverlayBaseController<ActionSheetOptions, HTMLIonActionSheetElement> {
constructor() {
super('ion-action-sheet-controller');
constructor(@Inject(DOCUMENT) doc: any) {
super('ion-action-sheet-controller', doc);
}
}
7 changes: 4 additions & 3 deletions angular/src/providers/alert-controller.ts
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { AlertOptions } from '@ionic/core';

import { OverlayBaseController } from '../util/overlay';
Expand All @@ -7,7 +8,7 @@ import { OverlayBaseController } from '../util/overlay';
providedIn: 'root',
})
export class AlertController extends OverlayBaseController<AlertOptions, HTMLIonAlertElement> {
constructor() {
super('ion-alert-controller');
constructor(@Inject(DOCUMENT) doc: any) {
super('ion-alert-controller', doc);
}
}
5 changes: 2 additions & 3 deletions angular/src/providers/config.ts
Expand Up @@ -43,9 +43,8 @@ export class Config {
export const ConfigToken = new InjectionToken<any>('USERCONFIG');

function getConfig(): CoreConfig | null {
const win: IonicWindow | undefined = window as any;
if (typeof win !== 'undefined') {
const Ionic = win.Ionic;
if (typeof (window as any) !== 'undefined') {
const Ionic = (window as IonicWindow).Ionic;
if (Ionic && Ionic.config) {
return Ionic.config;
}
Expand Down
19 changes: 14 additions & 5 deletions angular/src/providers/dom-controller.ts
Expand Up @@ -23,14 +23,23 @@ export class DomController {
}

function getQueue() {
const Ionic = (window as any).Ionic;
if (Ionic && Ionic.queue) {
return Ionic.queue;
const win = typeof (window as any) !== 'undefined' ? window : null as any;

if (win != null) {
const Ionic = win.Ionic;
if (Ionic && Ionic.queue) {
return Ionic.queue;
}

return {
read: (cb: any) => win.requestAnimationFrame(cb),
write: (cb: any) => win.requestAnimationFrame(cb)
};
}

return {
read: (cb: any) => window.requestAnimationFrame(cb),
write: (cb: any) => window.requestAnimationFrame(cb)
read: (cb: any) => cb(),
write: (cb: any) => cb()
};
}

Expand Down
7 changes: 4 additions & 3 deletions angular/src/providers/loading-controller.ts
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { LoadingOptions } from '@ionic/core';

import { OverlayBaseController } from '../util/overlay';
Expand All @@ -7,7 +8,7 @@ import { OverlayBaseController } from '../util/overlay';
providedIn: 'root',
})
export class LoadingController extends OverlayBaseController<LoadingOptions, HTMLIonLoadingElement> {
constructor() {
super('ion-loading-controller');
constructor(@Inject(DOCUMENT) doc: any) {
super('ion-loading-controller', doc);
}
}
26 changes: 15 additions & 11 deletions angular/src/providers/menu-controller.ts
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';

import { proxyMethod } from '../util/util';

Expand All @@ -8,13 +9,16 @@ const CTRL = 'ion-menu-controller';
})
export class MenuController {

constructor(@Inject(DOCUMENT) private doc: any) {
}

/**
* Programmatically open the Menu.
* @param [menuId] Optionally get the menu by its id, or side.
* @return returns a promise when the menu is fully opened
*/
open(menuId?: string): Promise<boolean> {
return proxyMethod(CTRL, 'open', menuId);
return proxyMethod(CTRL, this.doc, 'open', menuId);
}

/**
Expand All @@ -25,7 +29,7 @@ export class MenuController {
* @return returns a promise when the menu is fully closed
*/
close(menuId?: string): Promise<boolean> {
return proxyMethod(CTRL, 'close', menuId);
return proxyMethod(CTRL, this.doc, 'close', menuId);
}

/**
Expand All @@ -35,7 +39,7 @@ export class MenuController {
* @return returns a promise when the menu has been toggled
*/
toggle(menuId?: string): Promise<boolean> {
return proxyMethod(CTRL, 'toggle', menuId);
return proxyMethod(CTRL, this.doc, 'toggle', menuId);
}

/**
Expand All @@ -47,7 +51,7 @@ export class MenuController {
* @return Returns the instance of the menu, which is useful for chaining.
*/
enable(shouldEnable: boolean, menuId?: string): Promise<HTMLIonMenuElement> {
return proxyMethod(CTRL, 'enable', shouldEnable, menuId);
return proxyMethod(CTRL, this.doc, 'enable', shouldEnable, menuId);
}

/**
Expand All @@ -57,7 +61,7 @@ export class MenuController {
* @return Returns the instance of the menu, which is useful for chaining.
*/
swipeEnable(shouldEnable: boolean, menuId?: string): Promise<HTMLIonMenuElement> {
return proxyMethod(CTRL, 'swipeEnable', shouldEnable, menuId);
return proxyMethod(CTRL, this.doc, 'swipeEnable', shouldEnable, menuId);
}

/**
Expand All @@ -66,15 +70,15 @@ export class MenuController {
* If the menuId is not specified, it returns true if ANY menu is currenly open.
*/
isOpen(menuId?: string): Promise<boolean> {
return proxyMethod(CTRL, 'isOpen', menuId);
return proxyMethod(CTRL, this.doc, 'isOpen', menuId);
}

/**
* @param [menuId] Optionally get the menu by its id, or side.
* @return Returns true if the menu is currently enabled, otherwise false.
*/
isEnabled(menuId?: string): Promise<boolean> {
return proxyMethod(CTRL, 'isEnabled', menuId);
return proxyMethod(CTRL, this.doc, 'isEnabled', menuId);
}

/**
Expand All @@ -87,20 +91,20 @@ export class MenuController {
* @return Returns the instance of the menu if found, otherwise `null`.
*/
get(menuId?: string): Promise<HTMLIonMenuElement> {
return proxyMethod(CTRL, 'get', menuId);
return proxyMethod(CTRL, this.doc, 'get', menuId);
}

/**
* @return Returns the instance of the menu already opened, otherwise `null`.
*/
getOpen(): Promise<HTMLIonMenuElement> {
return proxyMethod(CTRL, 'getOpen');
return proxyMethod(CTRL, this.doc, 'getOpen');
}

/**
* @return Returns an array of all menu instances.
*/
getMenus(): Promise<HTMLIonMenuElement[]> {
return proxyMethod(CTRL, 'getMenus');
return proxyMethod(CTRL, this.doc, 'getMenus');
}
}
6 changes: 4 additions & 2 deletions angular/src/providers/modal-controller.ts
@@ -1,4 +1,5 @@
import { ComponentFactoryResolver, Injectable, Injector } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { ComponentFactoryResolver, Inject, Injectable, Injector } from '@angular/core';
import { ModalOptions } from '@ionic/core';

import { OverlayBaseController } from '../util/overlay';
Expand All @@ -12,8 +13,9 @@ export class ModalController extends OverlayBaseController<ModalOptions, HTMLIon
private angularDelegate: AngularDelegate,
private resolver: ComponentFactoryResolver,
private injector: Injector,
@Inject(DOCUMENT) doc: any
) {
super('ion-modal-controller');
super('ion-modal-controller', doc);
}

create(opts: ModalOptions): Promise<HTMLIonModalElement> {
Expand Down
7 changes: 4 additions & 3 deletions angular/src/providers/picker-controller.ts
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { PickerOptions } from '@ionic/core';

import { OverlayBaseController } from '../util/overlay';
Expand All @@ -7,7 +8,7 @@ import { OverlayBaseController } from '../util/overlay';
providedIn: 'root',
})
export class PickerController extends OverlayBaseController<PickerOptions, HTMLIonPickerElement> {
constructor() {
super('ion-picker-controller');
constructor(@Inject(DOCUMENT) doc: any) {
super('ion-picker-controller', doc);
}
}

0 comments on commit f44c17e

Please sign in to comment.