/
templateManager.ts
196 lines (159 loc) · 6.33 KB
/
templateManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { Observable } from 'babylonjs';
import { isUrl, loadFile, camelToKebab } from './helper';
export interface TemplateConfiguration {
location?: string; // #template-id OR http://example.com/loading.html
html?: string; // raw html string
id?: string;
config?: { [key: string]: string | number | boolean };
children?: { [name: string]: TemplateConfiguration };
}
export class TemplateManager {
public onInit: Observable<Template>;
public onLoaded: Observable<Template>;
public onStateChange: Observable<Template>;
public onAllLoaded: Observable<TemplateManager>;
private templates: { [name: string]: Template };
constructor(public containerElement: HTMLElement) {
this.templates = {};
this.onInit = new Observable<Template>();
this.onLoaded = new Observable<Template>();
this.onStateChange = new Observable<Template>();
this.onAllLoaded = new Observable<TemplateManager>();
}
public initTemplate(configuration: TemplateConfiguration, name: string = 'main', parent?: Template) {
//init template
let template = new Template(name, configuration);
this.templates[name] = template;
let childrenMap = configuration.children || {};
let childrenTemplates = Object.keys(childrenMap).map(name => {
return this.initTemplate(configuration.children[name], name, template);
});
// register the observers
template.onLoaded.add(() => {
let addToParent = () => {
let containingElement = parent && parent.parent.querySelector(camelToKebab(name)) || this.containerElement;
template.appendTo(containingElement);
this.checkLoadedState();
}
if (parent && !parent.parent) {
parent.onAppended.add(() => {
addToParent();
});
} else {
addToParent();
}
});
return template;
}
// assumiung only ONE(!) canvas
public getCanvas(): HTMLCanvasElement {
return this.containerElement.querySelector('canvas');
}
public getTemplate(name: string) {
return this.templates[name];
}
private checkLoadedState() {
let done = Object.keys(this.templates).every((key) => {
return this.templates[key].isLoaded && !!this.templates[key].parent;
});
if (done) {
this.onAllLoaded.notifyObservers(this);
}
}
}
import * as Handlebars from 'handlebars/dist/handlebars.min.js';
export class Template {
public onInit: Observable<Template>;
public onLoaded: Observable<Template>;
public onAppended: Observable<Template>;
public onStateChange: Observable<Template>;
public isLoaded: boolean;
public parent: HTMLElement;
private fragment: DocumentFragment;
constructor(public name: string, private configuration: TemplateConfiguration) {
this.onInit = new Observable<Template>();
this.onLoaded = new Observable<Template>();
this.onAppended = new Observable<Template>();
this.onStateChange = new Observable<Template>();
this.isLoaded = false;
/*
if (configuration.id) {
this.parent.id = configuration.id;
}
*/
this.onInit.notifyObservers(this);
let htmlContentPromise = getTemplateAsHtml(configuration);
htmlContentPromise.then(htmlTemplate => {
if (htmlTemplate) {
let compiledTemplate = Handlebars.compile(htmlTemplate);
let config = this.configuration.config || {};
let rawHtml = compiledTemplate(config);
this.fragment = document.createRange().createContextualFragment(rawHtml);
this.isLoaded = true;
this.onLoaded.notifyObservers(this);
}
});
}
public appendTo(parent: HTMLElement) {
if (this.parent) {
console.error('Alread appanded to ', this.parent);
} else {
this.parent = parent;
if (this.configuration.id) {
this.parent.id = this.configuration.id;
}
this.parent.appendChild(this.fragment);
// appended only one frame after.
setTimeout(() => {
this.onAppended.notifyObservers(this);
});
}
}
public show(visibilityFunction?: (template: Template) => Promise<Template>): Promise<Template> {
if (visibilityFunction) {
return visibilityFunction(this).then(() => {
this.onStateChange.notifyObservers(this);
return this;
});
} else {
// flex? box? should this be configurable easier than the visibilityFunction?
this.parent.style.display = 'flex';
this.onStateChange.notifyObservers(this);
return Promise.resolve(this);
}
}
public hide(visibilityFunction?: (template: Template) => Promise<Template>): Promise<Template> {
if (visibilityFunction) {
return visibilityFunction(this).then(() => {
this.onStateChange.notifyObservers(this);
return this;
});
} else {
this.parent.style.display = 'none';
this.onStateChange.notifyObservers(this);
return Promise.resolve(this);
}
}
}
export function getTemplateAsHtml(templateConfig: TemplateConfiguration): Promise<string> {
if (!templateConfig) {
return Promise.resolve(undefined);
} else if (templateConfig.html) {
return Promise.resolve(templateConfig.html);
} else {
let location = getTemplateLocation(templateConfig);
if (isUrl(location)) {
return loadFile(location);
} else {
location = location.replace('#', '');
document.getElementById('#' + location);
}
}
}
export function getTemplateLocation(templateConfig): string {
if (!templateConfig || typeof templateConfig === 'string') {
return templateConfig;
} else {
return templateConfig.location;
}
}