@babylonjs/viewer 5.0.0-rc.4 → 5.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/configuration/configuration.d.ts +107 -107
  2. package/configuration/configuration.js +15 -15
  3. package/configuration/configuration.js.map +1 -1
  4. package/configuration/configurationCompatibility.d.ts +8 -8
  5. package/configuration/configurationCompatibility.js +65 -65
  6. package/configuration/configurationCompatibility.js.map +1 -1
  7. package/configuration/configurationContainer.d.ts +10 -10
  8. package/configuration/configurationContainer.js +9 -9
  9. package/configuration/configurationContainer.js.map +1 -1
  10. package/configuration/globals.d.ts +6 -6
  11. package/configuration/globals.js +17 -17
  12. package/configuration/globals.js.map +1 -1
  13. package/configuration/index.d.ts +2 -2
  14. package/configuration/index.js +3 -2
  15. package/configuration/index.js.map +1 -1
  16. package/configuration/interfaces/cameraConfiguration.d.ts +31 -31
  17. package/configuration/interfaces/cameraConfiguration.js +1 -1
  18. package/configuration/interfaces/cameraConfiguration.js.map +1 -1
  19. package/configuration/interfaces/colorGradingConfiguration.d.ts +81 -81
  20. package/configuration/interfaces/colorGradingConfiguration.js +1 -1
  21. package/configuration/interfaces/colorGradingConfiguration.js.map +1 -1
  22. package/configuration/interfaces/defaultRenderingPipelineConfiguration.d.ts +20 -20
  23. package/configuration/interfaces/defaultRenderingPipelineConfiguration.js +1 -1
  24. package/configuration/interfaces/defaultRenderingPipelineConfiguration.js.map +1 -1
  25. package/configuration/interfaces/environmentMapConfiguration.d.ts +22 -22
  26. package/configuration/interfaces/environmentMapConfiguration.js +1 -1
  27. package/configuration/interfaces/environmentMapConfiguration.js.map +1 -1
  28. package/configuration/interfaces/groundConfiguration.d.ts +24 -24
  29. package/configuration/interfaces/groundConfiguration.js +1 -1
  30. package/configuration/interfaces/groundConfiguration.js.map +1 -1
  31. package/configuration/interfaces/imageProcessingConfiguration.d.ts +43 -43
  32. package/configuration/interfaces/imageProcessingConfiguration.js +1 -1
  33. package/configuration/interfaces/imageProcessingConfiguration.js.map +1 -1
  34. package/configuration/interfaces/index.d.ts +15 -15
  35. package/configuration/interfaces/index.js +15 -15
  36. package/configuration/interfaces/index.js.map +1 -1
  37. package/configuration/interfaces/lightConfiguration.d.ts +60 -60
  38. package/configuration/interfaces/lightConfiguration.js +1 -1
  39. package/configuration/interfaces/lightConfiguration.js.map +1 -1
  40. package/configuration/interfaces/modelAnimationConfiguration.d.ts +26 -26
  41. package/configuration/interfaces/modelAnimationConfiguration.js +1 -1
  42. package/configuration/interfaces/modelAnimationConfiguration.js.map +1 -1
  43. package/configuration/interfaces/modelConfiguration.d.ts +65 -65
  44. package/configuration/interfaces/modelConfiguration.js +1 -1
  45. package/configuration/interfaces/modelConfiguration.js.map +1 -1
  46. package/configuration/interfaces/observersConfiguration.d.ts +5 -5
  47. package/configuration/interfaces/observersConfiguration.js +1 -1
  48. package/configuration/interfaces/observersConfiguration.js.map +1 -1
  49. package/configuration/interfaces/sceneConfiguration.d.ts +48 -48
  50. package/configuration/interfaces/sceneConfiguration.js +1 -1
  51. package/configuration/interfaces/sceneConfiguration.js.map +1 -1
  52. package/configuration/interfaces/sceneOptimizerConfiguration.d.ts +23 -23
  53. package/configuration/interfaces/sceneOptimizerConfiguration.js +1 -1
  54. package/configuration/interfaces/sceneOptimizerConfiguration.js.map +1 -1
  55. package/configuration/interfaces/skyboxConfiguration.d.ts +21 -21
  56. package/configuration/interfaces/skyboxConfiguration.js +1 -1
  57. package/configuration/interfaces/skyboxConfiguration.js.map +1 -1
  58. package/configuration/interfaces/templateConfiguration.d.ts +67 -67
  59. package/configuration/interfaces/templateConfiguration.js +1 -1
  60. package/configuration/interfaces/templateConfiguration.js.map +1 -1
  61. package/configuration/interfaces/vrConfiguration.d.ts +16 -16
  62. package/configuration/interfaces/vrConfiguration.js +1 -1
  63. package/configuration/interfaces/vrConfiguration.js.map +1 -1
  64. package/configuration/loader.d.ts +4 -4
  65. package/configuration/loader.js +16 -28
  66. package/configuration/loader.js.map +1 -1
  67. package/configuration/mappers.d.ts +42 -42
  68. package/configuration/mappers.js +190 -189
  69. package/configuration/mappers.js.map +1 -1
  70. package/configuration/renderOnlyLoader.d.ts +33 -33
  71. package/configuration/renderOnlyLoader.js +161 -158
  72. package/configuration/renderOnlyLoader.js.map +1 -1
  73. package/configuration/types/default.d.ts +6 -6
  74. package/configuration/types/default.js +120 -119
  75. package/configuration/types/default.js.map +1 -1
  76. package/configuration/types/environmentMap.d.ts +5 -5
  77. package/configuration/types/environmentMap.js +13 -13
  78. package/configuration/types/environmentMap.js.map +1 -1
  79. package/configuration/types/extended.d.ts +6 -6
  80. package/configuration/types/extended.js +316 -316
  81. package/configuration/types/extended.js.map +1 -1
  82. package/configuration/types/index.d.ts +13 -13
  83. package/configuration/types/index.js +49 -48
  84. package/configuration/types/index.js.map +1 -1
  85. package/configuration/types/minimal.d.ts +6 -6
  86. package/configuration/types/minimal.js +42 -42
  87. package/configuration/types/minimal.js.map +1 -1
  88. package/configuration/types/renderOnlyDefault.d.ts +30 -30
  89. package/configuration/types/renderOnlyDefault.js +30 -30
  90. package/configuration/types/renderOnlyDefault.js.map +1 -1
  91. package/configuration/types/shadowLight.d.ts +9 -9
  92. package/configuration/types/shadowLight.js +63 -63
  93. package/configuration/types/shadowLight.js.map +1 -1
  94. package/helper/index.d.ts +26 -26
  95. package/helper/index.js +62 -56
  96. package/helper/index.js.map +1 -1
  97. package/index.d.ts +30 -30
  98. package/index.js +45 -41
  99. package/index.js.map +1 -1
  100. package/initializer.d.ts +11 -11
  101. package/initializer.js +34 -33
  102. package/initializer.js.map +1 -1
  103. package/interfaces.d.ts +5 -5
  104. package/interfaces.js +1 -1
  105. package/interfaces.js.map +1 -1
  106. package/labs/environmentSerializer.d.ts +126 -126
  107. package/labs/environmentSerializer.js +190 -186
  108. package/labs/environmentSerializer.js.map +1 -1
  109. package/labs/texture.d.ts +183 -184
  110. package/labs/texture.js +300 -297
  111. package/labs/texture.js.map +1 -1
  112. package/labs/viewerLabs.d.ts +51 -51
  113. package/labs/viewerLabs.js +133 -131
  114. package/labs/viewerLabs.js.map +1 -1
  115. package/license.md +71 -0
  116. package/loader/modelLoader.d.ts +47 -44
  117. package/loader/modelLoader.js +189 -182
  118. package/loader/modelLoader.js.map +1 -1
  119. package/loader/plugins/applyMaterialConfig.d.ts +12 -12
  120. package/loader/plugins/applyMaterialConfig.js +15 -15
  121. package/loader/plugins/applyMaterialConfig.js.map +1 -1
  122. package/loader/plugins/extendedMaterialLoaderPlugin.d.ts +9 -9
  123. package/loader/plugins/extendedMaterialLoaderPlugin.js +15 -15
  124. package/loader/plugins/extendedMaterialLoaderPlugin.js.map +1 -1
  125. package/loader/plugins/index.d.ts +18 -17
  126. package/loader/plugins/index.js +42 -41
  127. package/loader/plugins/index.js.map +1 -1
  128. package/loader/plugins/loaderPlugin.d.ts +24 -24
  129. package/loader/plugins/loaderPlugin.js +1 -1
  130. package/loader/plugins/loaderPlugin.js.map +1 -1
  131. package/loader/plugins/msftLodLoaderPlugin.d.ts +12 -12
  132. package/loader/plugins/msftLodLoaderPlugin.js +20 -19
  133. package/loader/plugins/msftLodLoaderPlugin.js.map +1 -1
  134. package/loader/plugins/telemetryLoaderPlugin.d.ts +12 -12
  135. package/loader/plugins/telemetryLoaderPlugin.js +35 -35
  136. package/loader/plugins/telemetryLoaderPlugin.js.map +1 -1
  137. package/managers/observablesManager.d.ts +66 -66
  138. package/managers/observablesManager.js +34 -34
  139. package/managers/observablesManager.js.map +1 -1
  140. package/managers/sceneManager.d.ts +243 -245
  141. package/managers/sceneManager.js +1388 -1387
  142. package/managers/sceneManager.js.map +1 -1
  143. package/managers/telemetryManager.d.ts +57 -57
  144. package/managers/telemetryManager.js +113 -113
  145. package/managers/telemetryManager.js.map +1 -1
  146. package/model/modelAnimation.d.ts +215 -215
  147. package/model/modelAnimation.js +232 -232
  148. package/model/modelAnimation.js.map +1 -1
  149. package/model/viewerModel.d.ts +228 -236
  150. package/model/viewerModel.js +669 -664
  151. package/model/viewerModel.js.map +1 -1
  152. package/optimizer/custom/extended.d.ts +11 -13
  153. package/optimizer/custom/extended.js +98 -100
  154. package/optimizer/custom/extended.js.map +1 -1
  155. package/optimizer/custom/index.d.ts +8 -8
  156. package/optimizer/custom/index.js +24 -24
  157. package/optimizer/custom/index.js.map +1 -1
  158. package/package.json +41 -215
  159. package/readme.md +12 -8
  160. package/renderOnlyIndex.d.ts +11 -11
  161. package/renderOnlyIndex.js +17 -15
  162. package/renderOnlyIndex.js.map +1 -1
  163. package/templating/eventManager.d.ts +35 -35
  164. package/templating/eventManager.js +65 -63
  165. package/templating/eventManager.js.map +1 -1
  166. package/templating/plugins/hdButtonPlugin.d.ts +9 -9
  167. package/templating/plugins/hdButtonPlugin.js +21 -34
  168. package/templating/plugins/hdButtonPlugin.js.map +1 -1
  169. package/templating/plugins/printButton.d.ts +9 -10
  170. package/templating/plugins/printButton.js +40 -53
  171. package/templating/plugins/printButton.js.map +1 -1
  172. package/templating/templateManager.d.ts +190 -189
  173. package/templating/templateManager.js +553 -543
  174. package/templating/templateManager.js.map +1 -1
  175. package/templating/viewerTemplatePlugin.d.ts +21 -21
  176. package/templating/viewerTemplatePlugin.js +68 -68
  177. package/templating/viewerTemplatePlugin.js.map +1 -1
  178. package/viewer/defaultViewer.d.ts +122 -116
  179. package/viewer/defaultViewer.js +665 -670
  180. package/viewer/defaultViewer.js.map +1 -1
  181. package/viewer/renderOnlyViewer.d.ts +8 -8
  182. package/viewer/renderOnlyViewer.js +43 -50
  183. package/viewer/renderOnlyViewer.js.map +1 -1
  184. package/viewer/viewer.d.ts +254 -253
  185. package/viewer/viewer.js +777 -763
  186. package/viewer/viewer.js.map +1 -1
  187. package/viewer/viewerManager.d.ts +55 -55
  188. package/viewer/viewerManager.js +87 -86
  189. package/viewer/viewerManager.js.map +1 -1
  190. package/viewer/viewerWithTemplate.d.ts +9 -9
  191. package/viewer/viewerWithTemplate.js +19 -32
  192. package/viewer/viewerWithTemplate.js.map +1 -1
@@ -1,544 +1,554 @@
1
- import { Observable } from '@babylonjs/core/Misc/observable.js';
2
- import { Tools } from '@babylonjs/core/Misc/tools.js';
3
- import { isUrl, camelToKebab, kebabToCamel, deepmerge } from '../helper/index.js';
4
- import * as Handlebars from 'handlebars/dist/handlebars';
5
- import { EventManager } from './eventManager.js';
6
- /**
7
- * The template manager, a member of the viewer class, will manage the viewer's templates and generate the HTML.
8
- * The template manager managers a single viewer and can be seen as the collection of all sub-templates of the viewer.
9
- */
10
- var TemplateManager = /** @class */ (function () {
11
- function TemplateManager(containerElement) {
12
- this.containerElement = containerElement;
13
- this.templates = {};
14
- this.onTemplateInit = new Observable();
15
- this.onTemplateLoaded = new Observable();
16
- this.onTemplateStateChange = new Observable();
17
- this.onAllLoaded = new Observable();
18
- this.onEventTriggered = new Observable();
19
- this.eventManager = new EventManager(this);
20
- }
21
- /**
22
- * Initialize the template(s) for the viewer. Called bay the Viewer class
23
- * @param templates the templates to be used to initialize the main template
24
- */
25
- TemplateManager.prototype.initTemplate = function (templates) {
26
- var _this = this;
27
- var internalInit = function (dependencyMap, name, parentTemplate) {
28
- //init template
29
- var template = _this.templates[name];
30
- var childrenTemplates = Object.keys(dependencyMap).map(function (childName) {
31
- return internalInit(dependencyMap[childName], childName, template);
32
- });
33
- // register the observers
34
- //template.onLoaded.add(() => {
35
- var addToParent = function () {
36
- var lastElements = parentTemplate && parentTemplate.parent.querySelectorAll(camelToKebab(name));
37
- var containingElement = (lastElements && lastElements.length && lastElements.item(lastElements.length - 1)) || _this.containerElement;
38
- template.appendTo(containingElement);
39
- _this._checkLoadedState();
40
- };
41
- if (parentTemplate && !parentTemplate.parent) {
42
- parentTemplate.onAppended.add(function () {
43
- addToParent();
44
- });
45
- }
46
- else {
47
- addToParent();
48
- }
49
- //});
50
- return template;
51
- };
52
- //build the html tree
53
- return this._buildHTMLTree(templates).then(function (htmlTree) {
54
- if (_this.templates['main']) {
55
- internalInit(htmlTree, 'main');
56
- }
57
- else {
58
- _this._checkLoadedState();
59
- }
60
- return;
61
- });
62
- };
63
- /**
64
- *
65
- * This function will create a simple map with child-dependencies of the template html tree.
66
- * It will compile each template, check if its children exist in the configuration and will add them if they do.
67
- * It is expected that the main template will be called main!
68
- *
69
- * @param templates
70
- */
71
- TemplateManager.prototype._buildHTMLTree = function (templates) {
72
- var _this = this;
73
- var promises = Object.keys(templates).map(function (name) {
74
- // if the template was overridden
75
- if (!templates[name]) {
76
- return Promise.resolve(false);
77
- }
78
- // else - we have a template, let's do our job!
79
- var template = new Template(name, templates[name]);
80
- template.onLoaded.add(function () {
81
- _this.onTemplateLoaded.notifyObservers(template);
82
- });
83
- template.onStateChange.add(function () {
84
- _this.onTemplateStateChange.notifyObservers(template);
85
- });
86
- _this.onTemplateInit.notifyObservers(template);
87
- // make sure the global onEventTriggered is called as well
88
- template.onEventTriggered.add(function (eventData) { return _this.onEventTriggered.notifyObservers(eventData); });
89
- _this.templates[name] = template;
90
- return template.initPromise;
91
- });
92
- return Promise.all(promises).then(function () {
93
- var templateStructure = {};
94
- // now iterate through all templates and check for children:
95
- var buildTree = function (parentObject, name) {
96
- _this.templates[name].isInHtmlTree = true;
97
- var childNodes = _this.templates[name].getChildElements().filter(function (n) { return !!_this.templates[n]; });
98
- childNodes.forEach(function (element) {
99
- parentObject[element] = {};
100
- buildTree(parentObject[element], element);
101
- });
102
- };
103
- if (_this.templates['main']) {
104
- buildTree(templateStructure, "main");
105
- }
106
- return templateStructure;
107
- });
108
- };
109
- /**
110
- * Get the canvas in the template tree.
111
- * There must be one and only one canvas inthe template.
112
- */
113
- TemplateManager.prototype.getCanvas = function () {
114
- return this.containerElement.querySelector('canvas');
115
- };
116
- /**
117
- * Get a specific template from the template tree
118
- * @param name the name of the template to load
119
- */
120
- TemplateManager.prototype.getTemplate = function (name) {
121
- return this.templates[name];
122
- };
123
- TemplateManager.prototype._checkLoadedState = function () {
124
- var _this = this;
125
- var done = Object.keys(this.templates).length === 0 || Object.keys(this.templates).every(function (key) {
126
- return (_this.templates[key].isLoaded && !!_this.templates[key].parent) || !_this.templates[key].isInHtmlTree;
127
- });
128
- if (done) {
129
- this.onAllLoaded.notifyObservers(this);
130
- }
131
- };
132
- /**
133
- * Dispose the template manager
134
- */
135
- TemplateManager.prototype.dispose = function () {
136
- var _this = this;
137
- // dispose all templates
138
- Object.keys(this.templates).forEach(function (template) {
139
- _this.templates[template].dispose();
140
- });
141
- this.templates = {};
142
- this.eventManager.dispose();
143
- this.onTemplateInit.clear();
144
- this.onAllLoaded.clear();
145
- this.onEventTriggered.clear();
146
- this.onTemplateLoaded.clear();
147
- this.onTemplateStateChange.clear();
148
- };
149
- return TemplateManager;
150
- }());
151
- export { TemplateManager };
152
- // register a new helper. modified https://stackoverflow.com/questions/9838925/is-there-any-method-to-iterate-a-map-with-handlebars-js
153
- Handlebars.registerHelper('eachInMap', function (map, block) {
154
- var out = '';
155
- Object.keys(map).map(function (prop) {
156
- var data = map[prop];
157
- if (typeof data === 'object') {
158
- data.id = data.id || prop;
159
- out += block.fn(data);
160
- }
161
- else {
162
- out += block.fn({ id: prop, value: data });
163
- }
164
- });
165
- return out;
166
- });
167
- Handlebars.registerHelper('add', function (a, b) {
168
- var out = a + b;
169
- return out;
170
- });
171
- Handlebars.registerHelper('eq', function (a, b) {
172
- var out = (a == b);
173
- return out;
174
- });
175
- Handlebars.registerHelper('or', function (a, b) {
176
- var out = a || b;
177
- return out;
178
- });
179
- Handlebars.registerHelper('not', function (a) {
180
- var out = !a;
181
- return out;
182
- });
183
- Handlebars.registerHelper('count', function (map) {
184
- return map.length;
185
- });
186
- Handlebars.registerHelper('gt', function (a, b) {
187
- var out = a > b;
188
- return out;
189
- });
190
- /**
191
- * This class represents a single template in the viewer's template tree.
192
- * An example for a template is a single canvas, an overlay (containing sub-templates) or the navigation bar.
193
- * A template is injected using the template manager in the correct position.
194
- * The template is rendered using Handlebars and can use Handlebars' features (such as parameter injection)
195
- *
196
- * For further information please refer to the documentation page, https://doc.babylonjs.com
197
- */
198
- var Template = /** @class */ (function () {
199
- function Template(name, _configuration) {
200
- var _this = this;
201
- this.name = name;
202
- this._configuration = _configuration;
203
- this.onLoaded = new Observable();
204
- this.onAppended = new Observable();
205
- this.onStateChange = new Observable();
206
- this.onEventTriggered = new Observable();
207
- this.onParamsUpdated = new Observable();
208
- this.onHTMLRendered = new Observable();
209
- this.loadRequests = [];
210
- this.isLoaded = false;
211
- this.isShown = false;
212
- this.isInHtmlTree = false;
213
- var htmlContentPromise = this._getTemplateAsHtml(_configuration);
214
- this.initPromise = htmlContentPromise.then(function (htmlTemplate) {
215
- if (htmlTemplate) {
216
- _this._htmlTemplate = htmlTemplate;
217
- var compiledTemplate = Handlebars.compile(htmlTemplate, { noEscape: (_this._configuration.params && !!_this._configuration.params.noEscape) });
218
- var config = _this._configuration.params || {};
219
- _this._rawHtml = compiledTemplate(config);
220
- try {
221
- _this._fragment = document.createRange().createContextualFragment(_this._rawHtml);
222
- }
223
- catch (e) {
224
- var test_1 = document.createElement(_this.name);
225
- test_1.innerHTML = _this._rawHtml;
226
- _this._fragment = test_1;
227
- }
228
- _this.isLoaded = true;
229
- _this.isShown = true;
230
- _this.onLoaded.notifyObservers(_this);
231
- }
232
- return _this;
233
- });
234
- }
235
- /**
236
- * Some templates have parameters (like background color for example).
237
- * The parameters are provided to Handlebars which in turn generates the template.
238
- * This function will update the template with the new parameters
239
- *
240
- * Note that when updating parameters the events will be registered again (after being cleared).
241
- *
242
- * @param params the new template parameters
243
- */
244
- Template.prototype.updateParams = function (params, append) {
245
- if (append === void 0) { append = true; }
246
- if (append) {
247
- this._configuration.params = deepmerge(this._configuration.params, params);
248
- }
249
- else {
250
- this._configuration.params = params;
251
- }
252
- // update the template
253
- if (this.isLoaded) {
254
- // this.dispose();
255
- }
256
- var compiledTemplate = Handlebars.compile(this._htmlTemplate);
257
- var config = this._configuration.params || {};
258
- this._rawHtml = compiledTemplate(config);
259
- try {
260
- this._fragment = document.createRange().createContextualFragment(this._rawHtml);
261
- }
262
- catch (e) {
263
- var test_2 = document.createElement(this.name);
264
- test_2.innerHTML = this._rawHtml;
265
- this._fragment = test_2;
266
- }
267
- if (this.parent) {
268
- this.appendTo(this.parent, true);
269
- }
270
- };
271
- Template.prototype.redraw = function () {
272
- this.updateParams({});
273
- };
274
- Object.defineProperty(Template.prototype, "configuration", {
275
- /**
276
- * Get the template'S configuration
277
- */
278
- get: function () {
279
- return this._configuration;
280
- },
281
- enumerable: false,
282
- configurable: true
283
- });
284
- /**
285
- * A template can be a parent element for other templates or HTML elements.
286
- * This function will deliver all child HTML elements of this template.
287
- */
288
- Template.prototype.getChildElements = function () {
289
- var childrenArray = [];
290
- //Edge and IE don't support frage,ent.children
291
- var children = this._fragment && this._fragment.children;
292
- if (!this._fragment) {
293
- var fragment = this.parent.querySelector(this.name);
294
- if (fragment) {
295
- children = fragment.querySelectorAll('*');
296
- }
297
- }
298
- if (!children) {
299
- // casting to HTMLCollection, as both NodeListOf and HTMLCollection have 'item()' and 'length'.
300
- children = this._fragment.querySelectorAll('*');
301
- }
302
- for (var i = 0; i < children.length; ++i) {
303
- var child = children.item(i);
304
- if (child) {
305
- childrenArray.push(kebabToCamel(child.nodeName.toLowerCase()));
306
- }
307
- }
308
- return childrenArray;
309
- };
310
- /**
311
- * Appending the template to a parent HTML element.
312
- * If a parent is already set and you wish to replace the old HTML with new one, forceRemove should be true.
313
- * @param parent the parent to which the template is added
314
- * @param forceRemove if the parent already exists, shoud the template be removed from it?
315
- */
316
- Template.prototype.appendTo = function (parent, forceRemove) {
317
- var _this = this;
318
- if (this.parent) {
319
- if (forceRemove && this._addedFragment) {
320
- /*let fragement = this.parent.querySelector(this.name)
321
- if (fragement)
322
- this.parent.removeChild(fragement);*/
323
- this.parent.innerHTML = '';
324
- }
325
- else {
326
- return;
327
- }
328
- }
329
- this.parent = parent;
330
- if (this._configuration.id) {
331
- this.parent.id = this._configuration.id;
332
- }
333
- if (this._fragment) {
334
- this.parent.appendChild(this._fragment);
335
- this._addedFragment = this._fragment;
336
- }
337
- else {
338
- this.parent.insertAdjacentHTML("beforeend", this._rawHtml);
339
- }
340
- this.onHTMLRendered.notifyObservers(this);
341
- // appended only one frame after.
342
- setTimeout(function () {
343
- _this._registerEvents();
344
- _this.onAppended.notifyObservers(_this);
345
- });
346
- };
347
- /**
348
- * Show the template using the provided visibilityFunction, or natively using display: flex.
349
- * The provided function returns a promise that should be fullfilled when the element is shown.
350
- * Since it is a promise async operations are more than possible.
351
- * See the default viewer for an opacity example.
352
- * @param visibilityFunction The function to execute to show the template.
353
- */
354
- Template.prototype.show = function (visibilityFunction) {
355
- var _this = this;
356
- if (this._isHiding) {
357
- return Promise.resolve(this);
358
- }
359
- return Promise.resolve().then(function () {
360
- _this._isShowing = true;
361
- if (visibilityFunction) {
362
- return visibilityFunction(_this);
363
- }
364
- else {
365
- // flex? box? should this be configurable easier than the visibilityFunction?
366
- _this.parent.style.display = 'flex';
367
- // support old browsers with no flex:
368
- if (_this.parent.style.display !== 'flex') {
369
- _this.parent.style.display = '';
370
- }
371
- return _this;
372
- }
373
- }).then(function () {
374
- _this.isShown = true;
375
- _this._isShowing = false;
376
- _this.onStateChange.notifyObservers(_this);
377
- return _this;
378
- });
379
- };
380
- /**
381
- * Hide the template using the provided visibilityFunction, or natively using display: none.
382
- * The provided function returns a promise that should be fullfilled when the element is hidden.
383
- * Since it is a promise async operations are more than possible.
384
- * See the default viewer for an opacity example.
385
- * @param visibilityFunction The function to execute to show the template.
386
- */
387
- Template.prototype.hide = function (visibilityFunction) {
388
- var _this = this;
389
- if (this._isShowing) {
390
- return Promise.resolve(this);
391
- }
392
- return Promise.resolve().then(function () {
393
- _this._isHiding = true;
394
- if (visibilityFunction) {
395
- return visibilityFunction(_this);
396
- }
397
- else {
398
- // flex? box? should this be configurable easier than the visibilityFunction?
399
- _this.parent.style.display = 'none';
400
- return _this;
401
- }
402
- }).then(function () {
403
- _this.isShown = false;
404
- _this._isHiding = false;
405
- _this.onStateChange.notifyObservers(_this);
406
- return _this;
407
- });
408
- };
409
- /**
410
- * Dispose this template
411
- */
412
- Template.prototype.dispose = function () {
413
- this.onAppended.clear();
414
- this.onEventTriggered.clear();
415
- this.onLoaded.clear();
416
- this.onStateChange.clear();
417
- this.isLoaded = false;
418
- // remove from parent
419
- try {
420
- this.parent.removeChild(this._fragment);
421
- }
422
- catch (e) {
423
- //noop
424
- }
425
- this.loadRequests.forEach(function (request) {
426
- request.abort();
427
- });
428
- if (this._registeredEvents) {
429
- this._registeredEvents.forEach(function (evt) {
430
- evt.htmlElement.removeEventListener(evt.eventName, evt.function);
431
- });
432
- }
433
- };
434
- Template.prototype._getTemplateAsHtml = function (templateConfig) {
435
- var _this = this;
436
- if (!templateConfig) {
437
- return Promise.reject('No templateConfig provided');
438
- }
439
- else if (templateConfig.html && !templateConfig.location) {
440
- return Promise.resolve(templateConfig.html);
441
- }
442
- else {
443
- var location_1 = this._getTemplateLocation(templateConfig);
444
- if (isUrl(location_1)) {
445
- return new Promise(function (resolve, reject) {
446
- var fileRequest = Tools.LoadFile(location_1, function (data) {
447
- resolve(data);
448
- }, undefined, undefined, false, function (request, error) {
449
- reject(error);
450
- });
451
- _this.loadRequests.push(fileRequest);
452
- });
453
- }
454
- else {
455
- location_1 = location_1.replace('#', '');
456
- var element = document.getElementById(location_1);
457
- if (element) {
458
- return Promise.resolve(element.innerHTML);
459
- }
460
- else {
461
- return Promise.reject('Template ID not found');
462
- }
463
- }
464
- }
465
- };
466
- Template.prototype._registerEvents = function () {
467
- var _this = this;
468
- this._registeredEvents = this._registeredEvents || [];
469
- if (this._registeredEvents.length) {
470
- // first remove the registered events
471
- this._registeredEvents.forEach(function (evt) {
472
- evt.htmlElement.removeEventListener(evt.eventName, evt.function);
473
- });
474
- }
475
- if (this._configuration.events) {
476
- var _loop_1 = function (eventName) {
477
- if (this_1._configuration.events && this_1._configuration.events[eventName]) {
478
- var functionToFire_1 = function (selector, event) {
479
- _this.onEventTriggered.notifyObservers({ event: event, template: _this, selector: selector });
480
- };
481
- // if boolean, set the parent as the event listener
482
- if (typeof this_1._configuration.events[eventName] === 'boolean') {
483
- var selector = this_1.parent.id;
484
- if (selector) {
485
- selector = '#' + selector;
486
- }
487
- else {
488
- selector = this_1.parent.tagName;
489
- }
490
- var binding = functionToFire_1.bind(this_1, selector);
491
- this_1.parent.addEventListener(eventName, binding, false);
492
- this_1._registeredEvents.push({
493
- htmlElement: this_1.parent,
494
- eventName: eventName,
495
- function: binding
496
- });
497
- }
498
- else if (typeof this_1._configuration.events[eventName] === 'object') {
499
- var selectorsArray = Object.keys(this_1._configuration.events[eventName] || {});
500
- // strict null checl is working incorrectly, must override:
501
- var event_1 = this_1._configuration.events[eventName] || {};
502
- selectorsArray.filter(function (selector) { return event_1[selector]; }).forEach(function (selector) {
503
- var htmlElement = _this.parent.querySelector(selector);
504
- if (!htmlElement) {
505
- // backcompat, fallback to id
506
- if (selector && selector.indexOf('#') !== 0) {
507
- selector = '#' + selector;
508
- }
509
- try {
510
- htmlElement = _this.parent.querySelector(selector);
511
- }
512
- catch (e) { }
513
- }
514
- if (htmlElement) {
515
- var binding = functionToFire_1.bind(_this, selector);
516
- htmlElement.addEventListener(eventName, binding, false);
517
- _this._registeredEvents.push({
518
- htmlElement: htmlElement,
519
- eventName: eventName,
520
- function: binding
521
- });
522
- }
523
- });
524
- }
525
- }
526
- };
527
- var this_1 = this;
528
- for (var eventName in this._configuration.events) {
529
- _loop_1(eventName);
530
- }
531
- }
532
- };
533
- Template.prototype._getTemplateLocation = function (templateConfig) {
534
- if (!templateConfig || typeof templateConfig === 'string') {
535
- return templateConfig;
536
- }
537
- else {
538
- return templateConfig.location;
539
- }
540
- };
541
- return Template;
542
- }());
543
- export { Template };
1
+ import { Observable } from "@babylonjs/core/Misc/observable.js";
2
+ import { Tools } from "@babylonjs/core/Misc/tools.js";
3
+ // eslint-disable-next-line import/no-internal-modules
4
+ import { isUrl, camelToKebab, kebabToCamel, deepmerge } from "../helper/index.js";
5
+ import * as Handlebars from "handlebars";
6
+ import { EventManager } from "./eventManager.js";
7
+ /**
8
+ * The template manager, a member of the viewer class, will manage the viewer's templates and generate the HTML.
9
+ * The template manager managers a single viewer and can be seen as the collection of all sub-templates of the viewer.
10
+ */
11
+ var TemplateManager = /** @class */ (function () {
12
+ function TemplateManager(containerElement) {
13
+ this.containerElement = containerElement;
14
+ this._templates = {};
15
+ this.onTemplateInit = new Observable();
16
+ this.onTemplateLoaded = new Observable();
17
+ this.onTemplateStateChange = new Observable();
18
+ this.onAllLoaded = new Observable();
19
+ this.onEventTriggered = new Observable();
20
+ this.eventManager = new EventManager(this);
21
+ }
22
+ /**
23
+ * Initialize the template(s) for the viewer. Called bay the Viewer class
24
+ * @param templates the templates to be used to initialize the main template
25
+ */
26
+ TemplateManager.prototype.initTemplate = function (templates) {
27
+ var _this = this;
28
+ var internalInit = function (dependencyMap, name, parentTemplate) {
29
+ //init template
30
+ var template = _this._templates[name];
31
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
32
+ Object.keys(dependencyMap).map(function (childName) {
33
+ return internalInit(dependencyMap[childName], childName, template);
34
+ });
35
+ // register the observers
36
+ //template.onLoaded.add(() => {
37
+ var addToParent = function () {
38
+ var lastElements = parentTemplate && parentTemplate.parent.querySelectorAll(camelToKebab(name));
39
+ var containingElement = (lastElements && lastElements.length && lastElements.item(lastElements.length - 1)) || _this.containerElement;
40
+ template.appendTo(containingElement);
41
+ _this._checkLoadedState();
42
+ };
43
+ if (parentTemplate && !parentTemplate.parent) {
44
+ parentTemplate.onAppended.add(function () {
45
+ addToParent();
46
+ });
47
+ }
48
+ else {
49
+ addToParent();
50
+ }
51
+ //});
52
+ return template;
53
+ };
54
+ //build the html tree
55
+ return this._buildHTMLTree(templates).then(function (htmlTree) {
56
+ if (_this._templates["main"]) {
57
+ internalInit(htmlTree, "main");
58
+ }
59
+ else {
60
+ _this._checkLoadedState();
61
+ }
62
+ return;
63
+ });
64
+ };
65
+ /**
66
+ *
67
+ * This function will create a simple map with child-dependencies of the template html tree.
68
+ * It will compile each template, check if its children exist in the configuration and will add them if they do.
69
+ * It is expected that the main template will be called main!
70
+ *
71
+ * @param templates
72
+ */
73
+ TemplateManager.prototype._buildHTMLTree = function (templates) {
74
+ var _this = this;
75
+ var promises = Object.keys(templates).map(function (name) {
76
+ // if the template was overridden
77
+ if (!templates[name]) {
78
+ return Promise.resolve(false);
79
+ }
80
+ // else - we have a template, let's do our job!
81
+ var template = new Template(name, templates[name]);
82
+ template.onLoaded.add(function () {
83
+ _this.onTemplateLoaded.notifyObservers(template);
84
+ });
85
+ template.onStateChange.add(function () {
86
+ _this.onTemplateStateChange.notifyObservers(template);
87
+ });
88
+ _this.onTemplateInit.notifyObservers(template);
89
+ // make sure the global onEventTriggered is called as well
90
+ template.onEventTriggered.add(function (eventData) { return _this.onEventTriggered.notifyObservers(eventData); });
91
+ _this._templates[name] = template;
92
+ return template.initPromise;
93
+ });
94
+ return Promise.all(promises).then(function () {
95
+ var templateStructure = {};
96
+ // now iterate through all templates and check for children:
97
+ var buildTree = function (parentObject, name) {
98
+ _this._templates[name].isInHtmlTree = true;
99
+ var childNodes = _this._templates[name].getChildElements().filter(function (n) { return !!_this._templates[n]; });
100
+ childNodes.forEach(function (element) {
101
+ parentObject[element] = {};
102
+ buildTree(parentObject[element], element);
103
+ });
104
+ };
105
+ if (_this._templates["main"]) {
106
+ buildTree(templateStructure, "main");
107
+ }
108
+ return templateStructure;
109
+ });
110
+ };
111
+ /**
112
+ * Get the canvas in the template tree.
113
+ * There must be one and only one canvas inthe template.
114
+ */
115
+ TemplateManager.prototype.getCanvas = function () {
116
+ return this.containerElement.querySelector("canvas");
117
+ };
118
+ /**
119
+ * Get a specific template from the template tree
120
+ * @param name the name of the template to load
121
+ */
122
+ TemplateManager.prototype.getTemplate = function (name) {
123
+ return this._templates[name];
124
+ };
125
+ TemplateManager.prototype._checkLoadedState = function () {
126
+ var _this = this;
127
+ var done = Object.keys(this._templates).length === 0 ||
128
+ Object.keys(this._templates).every(function (key) {
129
+ return (_this._templates[key].isLoaded && !!_this._templates[key].parent) || !_this._templates[key].isInHtmlTree;
130
+ });
131
+ if (done) {
132
+ this.onAllLoaded.notifyObservers(this);
133
+ }
134
+ };
135
+ /**
136
+ * Dispose the template manager
137
+ */
138
+ TemplateManager.prototype.dispose = function () {
139
+ var _this = this;
140
+ // dispose all templates
141
+ Object.keys(this._templates).forEach(function (template) {
142
+ _this._templates[template].dispose();
143
+ });
144
+ this._templates = {};
145
+ this.eventManager.dispose();
146
+ this.onTemplateInit.clear();
147
+ this.onAllLoaded.clear();
148
+ this.onEventTriggered.clear();
149
+ this.onTemplateLoaded.clear();
150
+ this.onTemplateStateChange.clear();
151
+ };
152
+ return TemplateManager;
153
+ }());
154
+ export { TemplateManager };
155
+ // register a new helper. modified https://stackoverflow.com/questions/9838925/is-there-any-method-to-iterate-a-map-with-handlebars-js
156
+ Handlebars.registerHelper("eachInMap", function (map, block) {
157
+ var out = "";
158
+ Object.keys(map).map(function (prop) {
159
+ var data = map[prop];
160
+ if (typeof data === "object") {
161
+ data.id = data.id || prop;
162
+ out += block.fn(data);
163
+ }
164
+ else {
165
+ out += block.fn({ id: prop, value: data });
166
+ }
167
+ });
168
+ return out;
169
+ });
170
+ Handlebars.registerHelper("add", function (a, b) {
171
+ var out = a + b;
172
+ return out;
173
+ });
174
+ Handlebars.registerHelper("eq", function (a, b) {
175
+ var out = a == b;
176
+ return out;
177
+ });
178
+ Handlebars.registerHelper("or", function (a, b) {
179
+ var out = a || b;
180
+ return out;
181
+ });
182
+ Handlebars.registerHelper("not", function (a) {
183
+ var out = !a;
184
+ return out;
185
+ });
186
+ Handlebars.registerHelper("count", function (map) {
187
+ return map.length;
188
+ });
189
+ Handlebars.registerHelper("gt", function (a, b) {
190
+ var out = a > b;
191
+ return out;
192
+ });
193
+ /**
194
+ * This class represents a single template in the viewer's template tree.
195
+ * An example for a template is a single canvas, an overlay (containing sub-templates) or the navigation bar.
196
+ * A template is injected using the template manager in the correct position.
197
+ * The template is rendered using Handlebars and can use Handlebars' features (such as parameter injection)
198
+ *
199
+ * For further information please refer to the documentation page, https://doc.babylonjs.com
200
+ */
201
+ var Template = /** @class */ (function () {
202
+ function Template(name, _configuration) {
203
+ var _this = this;
204
+ this.name = name;
205
+ this._configuration = _configuration;
206
+ this.onLoaded = new Observable();
207
+ this.onAppended = new Observable();
208
+ this.onStateChange = new Observable();
209
+ this.onEventTriggered = new Observable();
210
+ this.onParamsUpdated = new Observable();
211
+ this.onHTMLRendered = new Observable();
212
+ this._loadRequests = [];
213
+ this.isLoaded = false;
214
+ this.isShown = false;
215
+ this.isInHtmlTree = false;
216
+ var htmlContentPromise = this._getTemplateAsHtml(_configuration);
217
+ this.initPromise = htmlContentPromise.then(function (htmlTemplate) {
218
+ if (htmlTemplate) {
219
+ _this._htmlTemplate = htmlTemplate;
220
+ var compiledTemplate = Handlebars.compile(htmlTemplate, { noEscape: _this._configuration.params && !!_this._configuration.params.noEscape });
221
+ var config = _this._configuration.params || {};
222
+ _this._rawHtml = compiledTemplate(config);
223
+ try {
224
+ _this._fragment = document.createRange().createContextualFragment(_this._rawHtml);
225
+ }
226
+ catch (e) {
227
+ var test_1 = document.createElement(_this.name);
228
+ test_1.innerHTML = _this._rawHtml;
229
+ _this._fragment = test_1;
230
+ }
231
+ _this.isLoaded = true;
232
+ _this.isShown = true;
233
+ _this.onLoaded.notifyObservers(_this);
234
+ }
235
+ return _this;
236
+ });
237
+ }
238
+ /**
239
+ * Some templates have parameters (like background color for example).
240
+ * The parameters are provided to Handlebars which in turn generates the template.
241
+ * This function will update the template with the new parameters
242
+ *
243
+ * Note that when updating parameters the events will be registered again (after being cleared).
244
+ *
245
+ * @param params the new template parameters
246
+ * @param append
247
+ */
248
+ Template.prototype.updateParams = function (params, append) {
249
+ if (append === void 0) { append = true; }
250
+ if (append) {
251
+ this._configuration.params = deepmerge(this._configuration.params, params);
252
+ }
253
+ else {
254
+ this._configuration.params = params;
255
+ }
256
+ // update the template
257
+ if (this.isLoaded) {
258
+ // this.dispose();
259
+ }
260
+ var compiledTemplate = Handlebars.compile(this._htmlTemplate);
261
+ var config = this._configuration.params || {};
262
+ this._rawHtml = compiledTemplate(config);
263
+ try {
264
+ this._fragment = document.createRange().createContextualFragment(this._rawHtml);
265
+ }
266
+ catch (e) {
267
+ var test_2 = document.createElement(this.name);
268
+ test_2.innerHTML = this._rawHtml;
269
+ this._fragment = test_2;
270
+ }
271
+ if (this.parent) {
272
+ this.appendTo(this.parent, true);
273
+ }
274
+ };
275
+ Template.prototype.redraw = function () {
276
+ this.updateParams({});
277
+ };
278
+ Object.defineProperty(Template.prototype, "configuration", {
279
+ /**
280
+ * Get the template'S configuration
281
+ */
282
+ get: function () {
283
+ return this._configuration;
284
+ },
285
+ enumerable: false,
286
+ configurable: true
287
+ });
288
+ /**
289
+ * A template can be a parent element for other templates or HTML elements.
290
+ * This function will deliver all child HTML elements of this template.
291
+ */
292
+ Template.prototype.getChildElements = function () {
293
+ var childrenArray = [];
294
+ //Edge and IE don't support frage,ent.children
295
+ var children = this._fragment && this._fragment.children;
296
+ if (!this._fragment) {
297
+ var fragment = this.parent.querySelector(this.name);
298
+ if (fragment) {
299
+ children = fragment.querySelectorAll("*");
300
+ }
301
+ }
302
+ if (!children) {
303
+ // casting to HTMLCollection, as both NodeListOf and HTMLCollection have 'item()' and 'length'.
304
+ children = this._fragment.querySelectorAll("*");
305
+ }
306
+ for (var i = 0; i < children.length; ++i) {
307
+ var child = children.item(i);
308
+ if (child) {
309
+ childrenArray.push(kebabToCamel(child.nodeName.toLowerCase()));
310
+ }
311
+ }
312
+ return childrenArray;
313
+ };
314
+ /**
315
+ * Appending the template to a parent HTML element.
316
+ * If a parent is already set and you wish to replace the old HTML with new one, forceRemove should be true.
317
+ * @param parent the parent to which the template is added
318
+ * @param forceRemove if the parent already exists, shoud the template be removed from it?
319
+ */
320
+ Template.prototype.appendTo = function (parent, forceRemove) {
321
+ var _this = this;
322
+ if (this.parent) {
323
+ if (forceRemove && this._addedFragment) {
324
+ /*let fragement = this.parent.querySelector(this.name)
325
+ if (fragement)
326
+ this.parent.removeChild(fragement);*/
327
+ this.parent.innerHTML = "";
328
+ }
329
+ else {
330
+ return;
331
+ }
332
+ }
333
+ this.parent = parent;
334
+ if (this._configuration.id) {
335
+ this.parent.id = this._configuration.id;
336
+ }
337
+ if (this._fragment) {
338
+ this.parent.appendChild(this._fragment);
339
+ this._addedFragment = this._fragment;
340
+ }
341
+ else {
342
+ this.parent.insertAdjacentHTML("beforeend", this._rawHtml);
343
+ }
344
+ this.onHTMLRendered.notifyObservers(this);
345
+ // appended only one frame after.
346
+ setTimeout(function () {
347
+ _this._registerEvents();
348
+ _this.onAppended.notifyObservers(_this);
349
+ });
350
+ };
351
+ /**
352
+ * Show the template using the provided visibilityFunction, or natively using display: flex.
353
+ * The provided function returns a promise that should be fulfilled when the element is shown.
354
+ * Since it is a promise async operations are more than possible.
355
+ * See the default viewer for an opacity example.
356
+ * @param visibilityFunction The function to execute to show the template.
357
+ */
358
+ Template.prototype.show = function (visibilityFunction) {
359
+ var _this = this;
360
+ if (this._isHiding) {
361
+ return Promise.resolve(this);
362
+ }
363
+ return Promise.resolve()
364
+ .then(function () {
365
+ _this._isShowing = true;
366
+ if (visibilityFunction) {
367
+ return visibilityFunction(_this);
368
+ }
369
+ else {
370
+ // flex? box? should this be configurable easier than the visibilityFunction?
371
+ _this.parent.style.display = "flex";
372
+ // support old browsers with no flex:
373
+ if (_this.parent.style.display !== "flex") {
374
+ _this.parent.style.display = "";
375
+ }
376
+ return _this;
377
+ }
378
+ })
379
+ .then(function () {
380
+ _this.isShown = true;
381
+ _this._isShowing = false;
382
+ _this.onStateChange.notifyObservers(_this);
383
+ return _this;
384
+ });
385
+ };
386
+ /**
387
+ * Hide the template using the provided visibilityFunction, or natively using display: none.
388
+ * The provided function returns a promise that should be fulfilled when the element is hidden.
389
+ * Since it is a promise async operations are more than possible.
390
+ * See the default viewer for an opacity example.
391
+ * @param visibilityFunction The function to execute to show the template.
392
+ */
393
+ Template.prototype.hide = function (visibilityFunction) {
394
+ var _this = this;
395
+ if (this._isShowing) {
396
+ return Promise.resolve(this);
397
+ }
398
+ return Promise.resolve()
399
+ .then(function () {
400
+ _this._isHiding = true;
401
+ if (visibilityFunction) {
402
+ return visibilityFunction(_this);
403
+ }
404
+ else {
405
+ // flex? box? should this be configurable easier than the visibilityFunction?
406
+ _this.parent.style.display = "none";
407
+ return _this;
408
+ }
409
+ })
410
+ .then(function () {
411
+ _this.isShown = false;
412
+ _this._isHiding = false;
413
+ _this.onStateChange.notifyObservers(_this);
414
+ return _this;
415
+ });
416
+ };
417
+ /**
418
+ * Dispose this template
419
+ */
420
+ Template.prototype.dispose = function () {
421
+ this.onAppended.clear();
422
+ this.onEventTriggered.clear();
423
+ this.onLoaded.clear();
424
+ this.onStateChange.clear();
425
+ this.isLoaded = false;
426
+ // remove from parent
427
+ try {
428
+ this.parent.removeChild(this._fragment);
429
+ }
430
+ catch (e) {
431
+ //noop
432
+ }
433
+ this._loadRequests.forEach(function (request) {
434
+ request.abort();
435
+ });
436
+ if (this._registeredEvents) {
437
+ this._registeredEvents.forEach(function (evt) {
438
+ evt.htmlElement.removeEventListener(evt.eventName, evt.function);
439
+ });
440
+ }
441
+ };
442
+ Template.prototype._getTemplateAsHtml = function (templateConfig) {
443
+ var _this = this;
444
+ if (!templateConfig) {
445
+ return Promise.reject("No templateConfig provided");
446
+ }
447
+ else if (templateConfig.html && !templateConfig.location) {
448
+ return Promise.resolve(templateConfig.html);
449
+ }
450
+ else {
451
+ var location_1 = this._getTemplateLocation(templateConfig);
452
+ if (isUrl(location_1)) {
453
+ return new Promise(function (resolve, reject) {
454
+ var fileRequest = Tools.LoadFile(location_1, function (data) {
455
+ resolve(data);
456
+ }, undefined, undefined, false, function (request, error) {
457
+ reject(error);
458
+ });
459
+ _this._loadRequests.push(fileRequest);
460
+ });
461
+ }
462
+ else {
463
+ location_1 = location_1.replace("#", "");
464
+ var element = document.getElementById(location_1);
465
+ if (element) {
466
+ return Promise.resolve(element.innerHTML);
467
+ }
468
+ else {
469
+ return Promise.reject("Template ID not found");
470
+ }
471
+ }
472
+ }
473
+ };
474
+ Template.prototype._registerEvents = function () {
475
+ var _this = this;
476
+ this._registeredEvents = this._registeredEvents || [];
477
+ if (this._registeredEvents.length) {
478
+ // first remove the registered events
479
+ this._registeredEvents.forEach(function (evt) {
480
+ evt.htmlElement.removeEventListener(evt.eventName, evt.function);
481
+ });
482
+ }
483
+ if (this._configuration.events) {
484
+ var _loop_1 = function (eventName) {
485
+ if (this_1._configuration.events && this_1._configuration.events[eventName]) {
486
+ var functionToFire_1 = function (selector, event) {
487
+ _this.onEventTriggered.notifyObservers({ event: event, template: _this, selector: selector });
488
+ };
489
+ // if boolean, set the parent as the event listener
490
+ if (typeof this_1._configuration.events[eventName] === "boolean") {
491
+ var selector = this_1.parent.id;
492
+ if (selector) {
493
+ selector = "#" + selector;
494
+ }
495
+ else {
496
+ selector = this_1.parent.tagName;
497
+ }
498
+ var binding = functionToFire_1.bind(this_1, selector);
499
+ this_1.parent.addEventListener(eventName, binding, false);
500
+ this_1._registeredEvents.push({
501
+ htmlElement: this_1.parent,
502
+ eventName: eventName,
503
+ function: binding,
504
+ });
505
+ }
506
+ else if (typeof this_1._configuration.events[eventName] === "object") {
507
+ var selectorsArray = Object.keys(this_1._configuration.events[eventName] || {});
508
+ // strict null check is working incorrectly, must override:
509
+ var event_1 = this_1._configuration.events[eventName] || {};
510
+ selectorsArray
511
+ .filter(function (selector) { return typeof event_1 !== "boolean" && event_1[selector]; })
512
+ .forEach(function (selector) {
513
+ var htmlElement = _this.parent.querySelector(selector);
514
+ if (!htmlElement) {
515
+ // backcompat, fallback to id
516
+ if (selector && selector.indexOf("#") !== 0) {
517
+ selector = "#" + selector;
518
+ }
519
+ try {
520
+ htmlElement = _this.parent.querySelector(selector);
521
+ }
522
+ catch (e) { }
523
+ }
524
+ if (htmlElement) {
525
+ var binding = functionToFire_1.bind(_this, selector);
526
+ htmlElement.addEventListener(eventName, binding, false);
527
+ _this._registeredEvents.push({
528
+ htmlElement: htmlElement,
529
+ eventName: eventName,
530
+ function: binding,
531
+ });
532
+ }
533
+ });
534
+ }
535
+ }
536
+ };
537
+ var this_1 = this;
538
+ for (var eventName in this._configuration.events) {
539
+ _loop_1(eventName);
540
+ }
541
+ }
542
+ };
543
+ Template.prototype._getTemplateLocation = function (templateConfig) {
544
+ if (!templateConfig || typeof templateConfig === "string") {
545
+ return templateConfig;
546
+ }
547
+ else {
548
+ return templateConfig.location;
549
+ }
550
+ };
551
+ return Template;
552
+ }());
553
+ export { Template };
544
554
  //# sourceMappingURL=templateManager.js.map