mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 01:52:19 +08:00
chore(ssr): fix document.body reference (#18863)
This commit is contained in:
@ -16,7 +16,7 @@ export function appInitialize(config: Config, doc: Document, zone: NgZone) {
|
|||||||
_zoneGate: (h: any) => zone.run(h)
|
_zoneGate: (h: any) => zone.run(h)
|
||||||
};
|
};
|
||||||
|
|
||||||
const aelFn = '__zone_symbol__addEventListener' in (document.body as any)
|
const aelFn = '__zone_symbol__addEventListener' in (doc.body as any)
|
||||||
? '__zone_symbol__addEventListener'
|
? '__zone_symbol__addEventListener'
|
||||||
: 'addEventListener';
|
: 'addEventListener';
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ export class ValueAccessor implements ControlValueAccessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setIonicClasses(element: ElementRef) {
|
export const setIonicClasses = (element: ElementRef) => {
|
||||||
raf(() => {
|
raf(() => {
|
||||||
const input = element.nativeElement as HTMLElement;
|
const input = element.nativeElement as HTMLElement;
|
||||||
const classes = getClasses(input);
|
const classes = getClasses(input);
|
||||||
@ -54,9 +54,9 @@ export function setIonicClasses(element: ElementRef) {
|
|||||||
setClasses(item, classes);
|
setClasses(item, classes);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function getClasses(element: HTMLElement) {
|
const getClasses = (element: HTMLElement) => {
|
||||||
const classList = element.classList;
|
const classList = element.classList;
|
||||||
const classes = [];
|
const classes = [];
|
||||||
for (let i = 0; i < classList.length; i++) {
|
for (let i = 0; i < classList.length; i++) {
|
||||||
@ -66,7 +66,7 @@ function getClasses(element: HTMLElement) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return classes;
|
return classes;
|
||||||
}
|
};
|
||||||
|
|
||||||
function setClasses(element: HTMLElement, classes: string[]) {
|
function setClasses(element: HTMLElement, classes: string[]) {
|
||||||
const classList = element.classList;
|
const classList = element.classList;
|
||||||
|
@ -246,16 +246,19 @@ export class StackController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanupAsync(activeRoute: RouteView, views: RouteView[], viewsSnapshot: RouteView[], location: Location) {
|
const cleanupAsync = (activeRoute: RouteView, views: RouteView[], viewsSnapshot: RouteView[], location: Location) => {
|
||||||
return new Promise(resolve => {
|
if (typeof (requestAnimationFrame as any) === 'function') {
|
||||||
|
return new Promise<any>(resolve => {
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
cleanup(activeRoute, views, viewsSnapshot, location);
|
cleanup(activeRoute, views, viewsSnapshot, location);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
};
|
||||||
|
|
||||||
function cleanup(activeRoute: RouteView, views: RouteView[], viewsSnapshot: RouteView[], location: Location) {
|
const cleanup = (activeRoute: RouteView, views: RouteView[], viewsSnapshot: RouteView[], location: Location) => {
|
||||||
viewsSnapshot
|
viewsSnapshot
|
||||||
.filter(view => !views.includes(view))
|
.filter(view => !views.includes(view))
|
||||||
.forEach(destroyView);
|
.forEach(destroyView);
|
||||||
@ -280,4 +283,4 @@ function cleanup(activeRoute: RouteView, views: RouteView[], viewsSnapshot: Rout
|
|||||||
view.ref.changeDetectorRef.detach();
|
view.ref.changeDetectorRef.detach();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
@ -2,7 +2,7 @@ import { ComponentRef } from '@angular/core';
|
|||||||
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
|
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
|
||||||
import { NavDirection, RouterDirection } from '@ionic/core';
|
import { NavDirection, RouterDirection } from '@ionic/core';
|
||||||
|
|
||||||
export function insertView(views: RouteView[], view: RouteView, direction: RouterDirection) {
|
export const insertView = (views: RouteView[], view: RouteView, direction: RouterDirection) => {
|
||||||
if (direction === 'root') {
|
if (direction === 'root') {
|
||||||
return setRoot(views, view);
|
return setRoot(views, view);
|
||||||
} else if (direction === 'forward') {
|
} else if (direction === 'forward') {
|
||||||
@ -10,15 +10,15 @@ export function insertView(views: RouteView[], view: RouteView, direction: Route
|
|||||||
} else {
|
} else {
|
||||||
return setBack(views, view);
|
return setBack(views, view);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function setRoot(views: RouteView[], view: RouteView) {
|
const setRoot = (views: RouteView[], view: RouteView) => {
|
||||||
views = views.filter(v => v.stackId !== view.stackId);
|
views = views.filter(v => v.stackId !== view.stackId);
|
||||||
views.push(view);
|
views.push(view);
|
||||||
return views;
|
return views;
|
||||||
}
|
};
|
||||||
|
|
||||||
function setForward(views: RouteView[], view: RouteView) {
|
const setForward = (views: RouteView[], view: RouteView) => {
|
||||||
const index = views.indexOf(view);
|
const index = views.indexOf(view);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
views = views.filter(v => v.stackId !== view.stackId || v.id <= view.id);
|
views = views.filter(v => v.stackId !== view.stackId || v.id <= view.id);
|
||||||
@ -26,30 +26,30 @@ function setForward(views: RouteView[], view: RouteView) {
|
|||||||
views.push(view);
|
views.push(view);
|
||||||
}
|
}
|
||||||
return views;
|
return views;
|
||||||
}
|
};
|
||||||
|
|
||||||
function setBack(views: RouteView[], view: RouteView) {
|
const setBack = (views: RouteView[], view: RouteView) => {
|
||||||
const index = views.indexOf(view);
|
const index = views.indexOf(view);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
return views.filter(v => v.stackId !== view.stackId || v.id <= view.id);
|
return views.filter(v => v.stackId !== view.stackId || v.id <= view.id);
|
||||||
} else {
|
} else {
|
||||||
return setRoot(views, view);
|
return setRoot(views, view);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export function getUrl(router: Router, activatedRoute: ActivatedRoute) {
|
export const getUrl = (router: Router, activatedRoute: ActivatedRoute) => {
|
||||||
const urlTree = router.createUrlTree(['.'], { relativeTo: activatedRoute });
|
const urlTree = router.createUrlTree(['.'], { relativeTo: activatedRoute });
|
||||||
return router.serializeUrl(urlTree);
|
return router.serializeUrl(urlTree);
|
||||||
}
|
};
|
||||||
|
|
||||||
export function isTabSwitch(enteringView: RouteView, leavingView: RouteView | undefined) {
|
export const isTabSwitch = (enteringView: RouteView, leavingView: RouteView | undefined) => {
|
||||||
if (!leavingView) {
|
if (!leavingView) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return enteringView.stackId !== leavingView.stackId;
|
return enteringView.stackId !== leavingView.stackId;
|
||||||
}
|
};
|
||||||
|
|
||||||
export function computeStackId(prefixUrl: string[] | undefined, url: string) {
|
export const computeStackId = (prefixUrl: string[] | undefined, url: string) => {
|
||||||
if (!prefixUrl) {
|
if (!prefixUrl) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -63,22 +63,22 @@ export function computeStackId(prefixUrl: string[] | undefined, url: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
};
|
||||||
|
|
||||||
export function toSegments(path: string): string[] {
|
export const toSegments = (path: string) => {
|
||||||
return path
|
return path
|
||||||
.split('/')
|
.split('/')
|
||||||
.map(s => s.trim())
|
.map(s => s.trim())
|
||||||
.filter(s => s !== '');
|
.filter(s => s !== '');
|
||||||
}
|
};
|
||||||
|
|
||||||
export function destroyView(view: RouteView | undefined) {
|
export const destroyView = (view: RouteView | undefined) => {
|
||||||
if (view) {
|
if (view) {
|
||||||
// TODO lifecycle event
|
// TODO lifecycle event
|
||||||
view.ref.destroy();
|
view.ref.destroy();
|
||||||
view.unlistenEvents();
|
view.unlistenEvents();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export interface StackEvent {
|
export interface StackEvent {
|
||||||
enteringView: RouteView;
|
enteringView: RouteView;
|
||||||
|
@ -5,17 +5,17 @@ export const raf = (h: any) => {
|
|||||||
return (win.__zone_symbol__requestAnimationFrame) ? win.__zone_symbol__requestAnimationFrame(h) : requestAnimationFrame(h);
|
return (win.__zone_symbol__requestAnimationFrame) ? win.__zone_symbol__requestAnimationFrame(h) : requestAnimationFrame(h);
|
||||||
};
|
};
|
||||||
|
|
||||||
export function proxyMethod(ctrlName: string, doc: Document, methodName: string, ...args: any[]) {
|
export const proxyMethod = (ctrlName: string, doc: Document, methodName: string, ...args: any[]) => {
|
||||||
const controller = ensureElementInBody(ctrlName, doc);
|
const controller = ensureElementInBody(ctrlName, doc);
|
||||||
return controller.componentOnReady()
|
return controller.componentOnReady()
|
||||||
.then(() => (controller as any)[methodName].apply(controller, args));
|
.then(() => (controller as any)[methodName].apply(controller, args));
|
||||||
}
|
};
|
||||||
|
|
||||||
export function ensureElementInBody(elementName: string, doc: Document) {
|
export const ensureElementInBody = (elementName: string, doc: Document) => {
|
||||||
let element = doc.querySelector(elementName);
|
let element = doc.querySelector(elementName);
|
||||||
if (!element) {
|
if (!element) {
|
||||||
element = doc.createElement(elementName);
|
element = doc.createElement(elementName);
|
||||||
doc.body.appendChild(element);
|
doc.body.appendChild(element);
|
||||||
}
|
}
|
||||||
return element as HTMLStencilElement;
|
return element as HTMLStencilElement;
|
||||||
}
|
};
|
||||||
|
Reference in New Issue
Block a user