|
@@ -1,6 +1,6 @@
|
|
|
|
|
|
-import { Observable } from 'babylonjs';
|
|
|
-import { isUrl, loadFile, camelToKebab, kebabToCamel } from './helper';
|
|
|
+import { Observable, IFileRequest, Tools } from 'babylonjs';
|
|
|
+import { isUrl, camelToKebab, kebabToCamel } from './helper';
|
|
|
|
|
|
export interface ITemplateConfiguration {
|
|
|
location?: string; // #template-id OR http://example.com/loading.html
|
|
@@ -212,6 +212,8 @@ export class Template {
|
|
|
private fragment: DocumentFragment;
|
|
|
private htmlTemplate: string;
|
|
|
|
|
|
+ private loadRequests: Array<IFileRequest>;
|
|
|
+
|
|
|
constructor(public name: string, private _configuration: ITemplateConfiguration) {
|
|
|
this.onInit = new Observable<Template>();
|
|
|
this.onLoaded = new Observable<Template>();
|
|
@@ -219,6 +221,8 @@ export class Template {
|
|
|
this.onStateChange = new Observable<Template>();
|
|
|
this.onEventTriggered = new Observable<EventCallback>();
|
|
|
|
|
|
+ this.loadRequests = [];
|
|
|
+
|
|
|
this.isLoaded = false;
|
|
|
this.isShown = false;
|
|
|
/*
|
|
@@ -228,7 +232,7 @@ export class Template {
|
|
|
*/
|
|
|
this.onInit.notifyObservers(this);
|
|
|
|
|
|
- let htmlContentPromise = getTemplateAsHtml(_configuration);
|
|
|
+ let htmlContentPromise = this.getTemplateAsHtml(_configuration);
|
|
|
|
|
|
this.initPromise = htmlContentPromise.then(htmlTemplate => {
|
|
|
if (htmlTemplate) {
|
|
@@ -340,6 +344,38 @@ export class Template {
|
|
|
this.isLoaded = false;
|
|
|
// remove from parent
|
|
|
this.parent.removeChild(this.fragment);
|
|
|
+
|
|
|
+ this.loadRequests.forEach(request => {
|
|
|
+ request.abort();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private getTemplateAsHtml(templateConfig: ITemplateConfiguration): Promise<string> {
|
|
|
+ if (!templateConfig) {
|
|
|
+ return Promise.reject('No templateConfig provided');
|
|
|
+ } else if (templateConfig.html) {
|
|
|
+ return Promise.resolve(templateConfig.html);
|
|
|
+ } else {
|
|
|
+ let location = getTemplateLocation(templateConfig);
|
|
|
+ if (isUrl(location)) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let fileRequest = Tools.LoadFile(location, (data: string) => {
|
|
|
+ resolve(data);
|
|
|
+ }, undefined, undefined, false, (request, error: any) => {
|
|
|
+ reject(error);
|
|
|
+ });
|
|
|
+ this.loadRequests.push(fileRequest);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ location = location.replace('#', '');
|
|
|
+ let element = document.getElementById(location);
|
|
|
+ if (element) {
|
|
|
+ return Promise.resolve(element.innerHTML);
|
|
|
+ } else {
|
|
|
+ return Promise.reject('Template ID not found');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private registeredEvents: Array<{ htmlElement: HTMLElement, eventName: string, function: EventListenerOrEventListenerObject }>;
|
|
@@ -389,27 +425,6 @@ export class Template {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function getTemplateAsHtml(templateConfig: ITemplateConfiguration): Promise<string> {
|
|
|
- if (!templateConfig) {
|
|
|
- return Promise.reject('No templateConfig provided');
|
|
|
- } else if (templateConfig.html) {
|
|
|
- return Promise.resolve(templateConfig.html);
|
|
|
- } else {
|
|
|
- let location = getTemplateLocation(templateConfig);
|
|
|
- if (isUrl(location)) {
|
|
|
- return loadFile(location);
|
|
|
- } else {
|
|
|
- location = location.replace('#', '');
|
|
|
- let element = document.getElementById(location);
|
|
|
- if (element) {
|
|
|
- return Promise.resolve(element.innerHTML);
|
|
|
- } else {
|
|
|
- return Promise.reject('Template ID not found');
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
export function getTemplateLocation(templateConfig): string {
|
|
|
if (!templateConfig || typeof templateConfig === 'string') {
|
|
|
return templateConfig;
|