Skip to content
Permalink
Browse files

fix(ssr): fix angular global window and document references

  • Loading branch information...
adamdbradley committed Feb 25, 2019
1 parent ceaef7e commit f44c17e03bfcd9f6f9375a19a8d06e9393124ac9
@@ -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;
@@ -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 {
@@ -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';

@@ -141,7 +141,8 @@ export class IonicModule {
useFactory: appInitialize,
multi: true,
deps: [
ConfigToken
ConfigToken,
DOCUMENT
]
}
]
@@ -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';
@@ -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);
}
}
@@ -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';
@@ -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);
}
}
@@ -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;
}
@@ -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()
};
}

@@ -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';
@@ -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);
}
}
@@ -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';

@@ -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);
}

/**
@@ -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);
}

/**
@@ -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);
}

/**
@@ -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);
}

/**
@@ -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);
}

/**
@@ -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);
}

/**
@@ -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');
}
}
@@ -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';
@@ -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> {
@@ -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';
@@ -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.
You can’t perform that action at this time.