@gglib/content 0.6.1 → 0.8.0

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 (90) hide show
  1. package/LICENSE +7 -0
  2. package/Readme.md +1 -1
  3. package/dist/bundles/content.esm.js +1050 -0
  4. package/dist/bundles/content.esm.js.map +1 -0
  5. package/dist/content/src/AssetContainer.d.ts +18 -0
  6. package/dist/content/src/AssetContainer.d.ts.map +1 -0
  7. package/dist/content/src/AssetContainer.js +30 -0
  8. package/dist/content/src/AssetContainer.js.map +1 -0
  9. package/dist/content/src/AssetLoaderRegistry.d.ts +21 -0
  10. package/dist/content/src/AssetLoaderRegistry.d.ts.map +1 -0
  11. package/dist/content/src/AssetLoaderRegistry.js +28 -0
  12. package/dist/content/src/AssetLoaderRegistry.js.map +1 -0
  13. package/dist/content/src/AssetType.d.ts +13 -0
  14. package/dist/content/src/AssetType.d.ts.map +1 -0
  15. package/dist/content/src/AssetType.js +6 -0
  16. package/dist/content/src/AssetType.js.map +1 -0
  17. package/dist/content/src/AsyncExecutor.d.ts +63 -0
  18. package/dist/content/src/AsyncExecutor.d.ts.map +1 -0
  19. package/dist/content/src/AsyncExecutor.js +166 -0
  20. package/dist/content/src/AsyncExecutor.js.map +1 -0
  21. package/dist/content/src/ContentLoader.d.ts +266 -0
  22. package/dist/content/src/ContentLoader.d.ts.map +1 -0
  23. package/dist/content/src/ContentLoader.js +341 -0
  24. package/dist/content/src/ContentLoader.js.map +1 -0
  25. package/dist/content/src/ContentType.d.ts.map +1 -1
  26. package/dist/content/src/ContentType.js +6 -10
  27. package/dist/content/src/ContentType.js.map +1 -1
  28. package/dist/content/src/HttpClient.d.ts +48 -0
  29. package/dist/content/src/HttpClient.d.ts.map +1 -0
  30. package/dist/content/src/HttpClient.js +139 -0
  31. package/dist/content/src/HttpClient.js.map +1 -0
  32. package/dist/content/src/MaterialRegistry.d.ts +14 -0
  33. package/dist/content/src/MaterialRegistry.d.ts.map +1 -0
  34. package/dist/content/src/MaterialRegistry.js +24 -0
  35. package/dist/content/src/MaterialRegistry.js.map +1 -0
  36. package/dist/content/src/ResourceGraph.d.ts +54 -0
  37. package/dist/content/src/ResourceGraph.d.ts.map +1 -0
  38. package/dist/content/src/ResourceGraph.js +195 -0
  39. package/dist/content/src/ResourceGraph.js.map +1 -0
  40. package/dist/content/src/TextureLoader.d.ts +14 -0
  41. package/dist/content/src/TextureLoader.d.ts.map +1 -0
  42. package/dist/content/src/TextureLoader.js +70 -0
  43. package/dist/content/src/TextureLoader.js.map +1 -0
  44. package/dist/content/src/index.d.ts +8 -7
  45. package/dist/content/src/index.d.ts.map +1 -1
  46. package/dist/content/src/index.js +8 -7
  47. package/dist/content/src/index.js.map +1 -1
  48. package/dist/content/tsconfig.build.tsbuildinfo +1 -0
  49. package/dist/content/tsconfig.spec.tsbuildinfo +1 -0
  50. package/dist/stats.html +1248 -637
  51. package/package.json +19 -8
  52. package/dist/bundles/content.umd.js +0 -713
  53. package/dist/bundles/content.umd.js.map +0 -1
  54. package/dist/content/src/ContentManager.d.ts +0 -153
  55. package/dist/content/src/ContentManager.d.ts.map +0 -1
  56. package/dist/content/src/ContentManager.js +0 -361
  57. package/dist/content/src/ContentManager.js.map +0 -1
  58. package/dist/content/src/ContentType.spec.d.ts +0 -2
  59. package/dist/content/src/ContentType.spec.d.ts.map +0 -1
  60. package/dist/content/src/ContentType.spec.js +0 -27
  61. package/dist/content/src/ContentType.spec.js.map +0 -1
  62. package/dist/content/src/Data.d.ts +0 -37
  63. package/dist/content/src/Data.d.ts.map +0 -1
  64. package/dist/content/src/Data.js +0 -34
  65. package/dist/content/src/Data.js.map +0 -1
  66. package/dist/content/src/Loader.d.ts +0 -69
  67. package/dist/content/src/Loader.d.ts.map +0 -1
  68. package/dist/content/src/Loader.js +0 -16
  69. package/dist/content/src/Loader.js.map +0 -1
  70. package/dist/content/src/Pipeline.d.ts +0 -38
  71. package/dist/content/src/Pipeline.d.ts.map +0 -1
  72. package/dist/content/src/Pipeline.js +0 -182
  73. package/dist/content/src/Pipeline.js.map +0 -1
  74. package/dist/content/src/Pipeline.spec.d.ts +0 -2
  75. package/dist/content/src/Pipeline.spec.d.ts.map +0 -1
  76. package/dist/content/src/Pipeline.spec.js +0 -121
  77. package/dist/content/src/Pipeline.spec.js.map +0 -1
  78. package/dist/content/src/PipelineContext.d.ts +0 -40
  79. package/dist/content/src/PipelineContext.d.ts.map +0 -1
  80. package/dist/content/src/PipelineContext.js +0 -2
  81. package/dist/content/src/PipelineContext.js.map +0 -1
  82. package/dist/content/src/symbols.d.ts +0 -2
  83. package/dist/content/src/symbols.d.ts.map +0 -1
  84. package/dist/content/src/symbols.js +0 -2
  85. package/dist/content/src/symbols.js.map +0 -1
  86. package/dist/content/src/utils.d.ts +0 -17
  87. package/dist/content/src/utils.d.ts.map +0 -1
  88. package/dist/content/src/utils.js +0 -42
  89. package/dist/content/src/utils.js.map +0 -1
  90. package/dist/content/tsconfig.tsbuildinfo +0 -2778
@@ -1,713 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@gglib/utils')) :
3
- typeof define === 'function' && define.amd ? define('Gglib.Content', ['exports', '@gglib/utils'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.Gglib = global.Gglib || {}, global.Gglib.Content = {}), global.Gglib.Utils));
5
- }(this, (function (exports, utils) { 'use strict';
6
-
7
- /*! *****************************************************************************
8
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
- PERFORMANCE OF THIS SOFTWARE.
20
- ***************************************************************************** */
21
-
22
- function __awaiter(thisArg, _arguments, P, generator) {
23
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
- return new (P || (P = Promise))(function (resolve, reject) {
25
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
- step((generator = generator.apply(thisArg, _arguments || [])).next());
29
- });
30
- }
31
-
32
- function __generator(thisArg, body) {
33
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
34
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
35
- function verb(n) { return function (v) { return step([n, v]); }; }
36
- function step(op) {
37
- if (f) throw new TypeError("Generator is already executing.");
38
- while (_) try {
39
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
40
- if (y = 0, t) op = [op[0] & 2, t.value];
41
- switch (op[0]) {
42
- case 0: case 1: t = op; break;
43
- case 4: _.label++; return { value: op[1], done: false };
44
- case 5: _.label++; y = op[1]; op = [0]; continue;
45
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
46
- default:
47
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
48
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
49
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
50
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
51
- if (t[2]) _.ops.pop();
52
- _.trys.pop(); continue;
53
- }
54
- op = body.call(thisArg, _);
55
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
56
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
57
- }
58
- }
59
-
60
- // for explanation see:
61
- // https://www.debuggex.com/
62
- var regex = /(([a-z0-9-]+)\/([a-z0-9+-]+))?((;\s*([a-z0-9-]+=)?(([a-z0-9]+)|("[^"]*")))+)?/;
63
- /**
64
- * Describes components of a mime type
65
- *
66
- * @public
67
- */
68
- var ContentType = /** @class */ (function () {
69
- function ContentType() {
70
- }
71
- ContentType.parse = function (input) {
72
- var match = regex.exec(input) || [];
73
- return match ? {
74
- mimeType: match[1],
75
- mediaType: match[2],
76
- subType: match[3],
77
- params: (match[4] || ''),
78
- } : null;
79
- };
80
- return ContentType;
81
- }());
82
-
83
- /**
84
- * @public
85
- */
86
- function dataFromXhr(xhr) {
87
- return {
88
- xhr: xhr,
89
- source: xhr.responseURL,
90
- content: xhr.response || xhr.responseText,
91
- type: ContentType.parse(xhr.getResponseHeader('content-type')),
92
- };
93
- }
94
- /**
95
- * @public
96
- */
97
- function dataFromUri(uri) {
98
- return {
99
- source: uri.uri,
100
- content: uri.data,
101
- type: ContentType.parse(uri.contentType),
102
- };
103
- }
104
- /**
105
- * @public
106
- */
107
- function dataFromElement(path, el, convert) {
108
- if (convert === void 0) { convert = function (it) { return it; }; }
109
- return {
110
- source: path,
111
- type: ContentType.parse(el.getAttribute('type')),
112
- content: convert(el.textContent),
113
- };
114
- }
115
-
116
- /**
117
- * @public
118
- */
119
- var LoaderEntry = /** @class */ (function () {
120
- function LoaderEntry(input, output, handle) {
121
- this.input = input;
122
- this.output = output;
123
- this.handle = handle;
124
- }
125
- LoaderEntry.prototype.toString = function () {
126
- return this.input.toString() + " => " + this.output.toString();
127
- };
128
- return LoaderEntry;
129
- }());
130
-
131
- function describeSym(sym) {
132
- if (!sym) {
133
- return "" + sym;
134
- }
135
- if (sym['name']) {
136
- return sym['name'];
137
- }
138
- return sym.toString();
139
- }
140
- function warn(msg) {
141
- utils.Log.warn('[Content.Loader] ' + msg);
142
- }
143
- var defaultPipeline;
144
- /**
145
- * @public
146
- */
147
- var Pipeline = /** @class */ (function () {
148
- function Pipeline() {
149
- this.loaders = [];
150
- }
151
- Object.defineProperty(Pipeline, "default", {
152
- /**
153
- * Returns the default pipeline instance
154
- */
155
- get: function () {
156
- if (defaultPipeline == null) {
157
- defaultPipeline = new Pipeline();
158
- }
159
- return defaultPipeline;
160
- },
161
- enumerable: false,
162
- configurable: true
163
- });
164
- Pipeline.run = function (context) {
165
- return __awaiter(this, void 0, void 0, function () {
166
- return __generator(this, function (_a) {
167
- return [2 /*return*/, context.pipeline.run(context.source, context.target, null, context)];
168
- });
169
- });
170
- };
171
- /**
172
- * Registeres a loader function
173
- *
174
- * @param loader - The loader specification
175
- */
176
- Pipeline.prototype.register = function (loader) {
177
- var _this = this;
178
- var inputs = Array.isArray(loader.input) ? loader.input : [loader.input];
179
- inputs.forEach(function (input) {
180
- for (var _i = 0, _a = _this.loaders; _i < _a.length; _i++) {
181
- var entry = _a[_i];
182
- if (entry.handle === loader.handle && entry.input === input && entry.output === loader.output) {
183
- warn("same loader was registered twice... loader is skipped. " + entry.toString());
184
- return;
185
- }
186
- }
187
- _this.loaders.push(new LoaderEntry(input, loader.output, loader.handle));
188
- });
189
- };
190
- /**
191
- *
192
- * @param source - The source type or symbol identifying the input type
193
- * @param target - The target type or symbol identifying the target type
194
- * @param input - The input value to import. Its type is identified by `source`
195
- * @param context - The context to use during the import
196
- */
197
- Pipeline.prototype.run = function (source, target, input, context) {
198
- return __awaiter(this, void 0, void 0, function () {
199
- var pipeline;
200
- return __generator(this, function (_a) {
201
- if (typeof source === 'string') {
202
- if (utils.DataUri.isDataUri(source)) {
203
- source = ContentType.parse(utils.DataUri.parse(source).contentType).mimeType;
204
- }
205
- else {
206
- source = utils.Uri.ext(source);
207
- }
208
- }
209
- pipeline = this.resolve(source, target);
210
- if (pipeline.length === 0) {
211
- throw new Error("Loader is missing: " + describeSym(source) + " => " + describeSym(target));
212
- }
213
- return [2 /*return*/, this.walk(pipeline, input, context)];
214
- });
215
- });
216
- };
217
- Pipeline.prototype.graph = function (source, target, input, context) {
218
- return __awaiter(this, void 0, void 0, function () {
219
- return __generator(this, function (_a) {
220
- return [2 /*return*/];
221
- });
222
- });
223
- };
224
- /**
225
- * Detects whether there is a loding path from `source` type to the `target` type
226
- *
227
- * @param source - The source type or symbol identifying the input type
228
- * @param target - The target type or symbol identifying the target type
229
- */
230
- Pipeline.prototype.canLoad = function (source, target) {
231
- return this.resolve(source, target).length > 0;
232
- };
233
- Pipeline.prototype.resolve = function (source, target, exclude) {
234
- var _this = this;
235
- if (exclude === void 0) { exclude = []; }
236
- var candidates = this.loaders.filter(function (it) { return exclude.indexOf(it) === -1 && it.input === source; });
237
- exclude.push.apply(exclude, candidates);
238
- return candidates.map(function (it) {
239
- if (it.output === target) {
240
- return {
241
- entry: it,
242
- next: [],
243
- };
244
- }
245
- var next = _this.resolve(it.output, target, exclude);
246
- return next && next.length ? {
247
- entry: it,
248
- next: next,
249
- } : null;
250
- })
251
- .filter(function (it) { return it != null; })
252
- .sort(function (a, b) { return a.next.length <= b.next.length ? -1 : 1; });
253
- };
254
- Pipeline.prototype.walk = function (nodes, input, context, depth) {
255
- if (depth === void 0) { depth = 1; }
256
- return __awaiter(this, void 0, void 0, function () {
257
- var _loop_1, this_1, i, state_1;
258
- return __generator(this, function (_a) {
259
- switch (_a.label) {
260
- case 0:
261
- if (nodes.length === 0) {
262
- // finish recursion
263
- return [2 /*return*/, input];
264
- }
265
- _loop_1 = function (i) {
266
- var node, loaded, result;
267
- return __generator(this, function (_a) {
268
- switch (_a.label) {
269
- case 0:
270
- node = nodes[i];
271
- context.pipeline = this_1;
272
- utils.Log.group("stage:" + depth + " (" + (i + 1) + ") " + describeSym(node.entry.input) + " => " + describeSym(node.entry.output));
273
- return [4 /*yield*/, utils.Log.groupEndAsync(function () { return node.entry.handle(input, context); })];
274
- case 1:
275
- loaded = _a.sent();
276
- return [4 /*yield*/, this_1.walk(node.next, loaded, context, depth + 1)];
277
- case 2:
278
- result = _a.sent();
279
- if (result) {
280
- return [2 /*return*/, { value: result }];
281
- }
282
- return [2 /*return*/];
283
- }
284
- });
285
- };
286
- this_1 = this;
287
- i = 0;
288
- _a.label = 1;
289
- case 1:
290
- if (!(i < nodes.length)) return [3 /*break*/, 4];
291
- return [5 /*yield**/, _loop_1(i)];
292
- case 2:
293
- state_1 = _a.sent();
294
- if (typeof state_1 === "object")
295
- return [2 /*return*/, state_1.value];
296
- _a.label = 3;
297
- case 3:
298
- i++;
299
- return [3 /*break*/, 1];
300
- case 4: return [2 /*return*/, null];
301
- }
302
- });
303
- });
304
- };
305
- return Pipeline;
306
- }());
307
-
308
- function getOption(options, key, fallback) {
309
- if (key in options) {
310
- return options[key];
311
- }
312
- return fallback;
313
- }
314
- /**
315
- * @public
316
- */
317
- var ContentManager = /** @class */ (function () {
318
- /**
319
- *
320
- */
321
- function ContentManager(device, options) {
322
- if (options === void 0) { options = {}; }
323
- /**
324
- * Allows to rewrite a url
325
- *
326
- * @remarks
327
- * Use this to remap a resource uri to another location.
328
- */
329
- this.rewriteUrl = function (it) { return it; };
330
- /**
331
- * Allows to rewrite an http request
332
- *
333
- * @remarks
334
- * Use this to add authentication or header fields to the request.
335
- * Avoid changing the `url` string.
336
- */
337
- this.rewriteRequest = function (it) { return it; };
338
- /**
339
- * Encodes a string to an array buffer
340
- */
341
- this.encode = function (str) {
342
- var buf = new ArrayBuffer(str.length * 2);
343
- var bufView = new Uint8Array(buf);
344
- for (var i = 0; i < str.length; i++) {
345
- bufView[i] = str.charCodeAt(i);
346
- }
347
- return buf;
348
- };
349
- /**
350
- * Indicates whether assets embedded in DOM elements are enabled
351
- */
352
- this.enableDomAssets = true;
353
- /**
354
- * The instance of the loader to use
355
- */
356
- this.pipeline = Pipeline.default;
357
- /**
358
- * Collection of all loaded assets
359
- */
360
- this.loaded = new Map();
361
- /**
362
- * Collection of all ongoing loading promises
363
- */
364
- this.loading = new Map();
365
- /**
366
- * Collection of existing download requests
367
- */
368
- this.downloads = new Map();
369
- this.device = device;
370
- this.setup(options);
371
- }
372
- ContentManager.prototype.setup = function (options) {
373
- this.enableDomAssets = getOption(options, 'enableDomAssets', this.enableDomAssets);
374
- this.pipeline = getOption(options, 'pipeline', this.pipeline);
375
- };
376
- /**
377
- * Downloads a resource as json
378
- */
379
- ContentManager.prototype.downloadJSON = function (optionsOrUrl) {
380
- return __awaiter(this, void 0, void 0, function () {
381
- var options;
382
- return __generator(this, function (_a) {
383
- options = this.createHttpOptions(optionsOrUrl);
384
- options.xhr = utils.Http.createXMLHttpRequest('json');
385
- return [2 /*return*/, this.download(options)];
386
- });
387
- });
388
- };
389
- /**
390
- * Downloads a resource as xml document
391
- */
392
- ContentManager.prototype.downloadDocument = function (optionsOrUrl) {
393
- return __awaiter(this, void 0, void 0, function () {
394
- var options;
395
- return __generator(this, function (_a) {
396
- options = this.createHttpOptions(optionsOrUrl);
397
- options.xhr = utils.Http.createXMLHttpRequest('document');
398
- return [2 /*return*/, this.download(options)];
399
- });
400
- });
401
- };
402
- /**
403
- * Downloads a resource as text
404
- */
405
- ContentManager.prototype.downloadText = function (optionsOrUrl) {
406
- return __awaiter(this, void 0, void 0, function () {
407
- var options;
408
- return __generator(this, function (_a) {
409
- options = this.createHttpOptions(optionsOrUrl);
410
- options.xhr = utils.Http.createXMLHttpRequest('text');
411
- return [2 /*return*/, this.download(options)];
412
- });
413
- });
414
- };
415
- /**
416
- * Downloads a resource as `Blob`
417
- */
418
- ContentManager.prototype.downloadBlob = function (optionsOrUrl) {
419
- return __awaiter(this, void 0, void 0, function () {
420
- var options;
421
- return __generator(this, function (_a) {
422
- options = this.createHttpOptions(optionsOrUrl);
423
- options.xhr = utils.Http.createXMLHttpRequest('blob');
424
- return [2 /*return*/, this.download(options)];
425
- });
426
- });
427
- };
428
- /**
429
- * Downloads a resource as `ArrayBuffer`
430
- */
431
- ContentManager.prototype.downloadArrayBuffer = function (optionsOrUrl) {
432
- return __awaiter(this, void 0, void 0, function () {
433
- var options;
434
- return __generator(this, function (_a) {
435
- options = this.createHttpOptions(optionsOrUrl);
436
- options.xhr = utils.Http.createXMLHttpRequest('arraybuffer');
437
- return [2 /*return*/, this.download(options)];
438
- });
439
- });
440
- };
441
- /**
442
- * Downloads data from a remote location.
443
- *
444
- * @remarks
445
- * If dom assets are enabled then an attempt will be made to find a DOM Element matching the following criteria
446
- * - element has a `type` attribute
447
- * - element has an `id` attribute with the value of the given url
448
- *
449
- * If such DOM Element exists then its textContent is taken as a result
450
- */
451
- ContentManager.prototype.download = function (optionsOrUrl) {
452
- return __awaiter(this, void 0, void 0, function () {
453
- var options, path, type;
454
- var _this = this;
455
- return __generator(this, function (_a) {
456
- options = this.createHttpOptions(optionsOrUrl);
457
- if (this.rewriteRequest) {
458
- options = this.rewriteRequest(options) || options;
459
- }
460
- path = options.url;
461
- type = options.xhr ? options.xhr.responseType || 'text' : 'text';
462
- if (this.downloads.has(type)) {
463
- if (this.downloads.get(type).has(path)) {
464
- return [2 /*return*/, this.downloads.get(type).get(path)];
465
- }
466
- }
467
- else {
468
- this.downloads.set(type, new Map());
469
- }
470
- return [2 /*return*/, this.downloads
471
- .get(type)
472
- .set(path, (function () { return __awaiter(_this, void 0, void 0, function () {
473
- var element, element;
474
- return __generator(this, function (_a) {
475
- if (this.enableDomAssets) {
476
- element = document.getElementById(path) || document.getElementById(path.replace(/^[\/|#]/, ''));
477
- if (!element || !element.hasAttribute('type')) ;
478
- else if (type === 'text') {
479
- return [2 /*return*/, Promise.resolve(dataFromElement(path, element))];
480
- }
481
- else if (type === 'json') {
482
- return [2 /*return*/, Promise.resolve(dataFromElement(path, element, function (text) { return JSON.parse(text); }))];
483
- }
484
- else if (type === 'arraybuffer') {
485
- return [2 /*return*/, Promise.resolve(dataFromElement(path, element, this.encode))];
486
- }
487
- else ;
488
- }
489
- if (this.enableDomAssets && type === 'text') {
490
- element = document.getElementById(path) || document.getElementById(path.replace(/^[\/|#]/, ''));
491
- if (element && element.hasAttribute('type')) {
492
- return [2 /*return*/, Promise.resolve(dataFromElement(path, element))];
493
- }
494
- }
495
- return [2 /*return*/, utils.Http.request(options)
496
- .catch(function (xhr) {
497
- throw new Error(xhr.responseURL + " " + xhr.statusText + " (" + xhr.status + ")");
498
- })
499
- .then(dataFromXhr)];
500
- });
501
- }); })())
502
- .get(path)];
503
- });
504
- });
505
- };
506
- /**
507
- * Loads and instantiates multiple assets
508
- *
509
- * @remarks
510
- * The given batch must be an object where each key identifies the target type e.g. `Model` or `Effect` etc.
511
- * The result has always the same structure as the given batch but all asset paths are replaced with an asset instance.
512
- *
513
- * If the batch value is a string, then that is used as an asset path where the asset is downloaded from.
514
- *
515
- * ```ts
516
- * load({ Model: 'path/to/model.obj' })
517
- * // => { Model: INSTANCE }
518
- * ```
519
- *
520
- * If the batch value is an array, then each entry is used as an asset path.
521
- *
522
- * ```ts
523
- * load({ Model: ['path/1.obj', 'path/2.obj'] })
524
- * // => { Model: [INSTANCE1, INSTANCE2] }
525
- * ```
526
- *
527
- * If the batch value is an object, then each entry is used as an asset path.
528
- *
529
- * ```ts
530
- * load({ Model: { foo: 'path/foo.obj' })
531
- * // => { Model: { foo: INSTANCE } }
532
- * ```
533
- *
534
- */
535
- ContentManager.prototype.loadBatch = function (batch) {
536
- var _this = this;
537
- var result = {};
538
- var promises = [];
539
- function enqueue(job) {
540
- promises.push(job.then(function () {
541
- // TODO: notify
542
- }));
543
- }
544
- Object.keys(batch).forEach(function (key) {
545
- var _a = batch[key], source = _a[0], targetType = _a[1];
546
- enqueue(_this.load(source, targetType).then(function (res) {
547
- result[key] = res;
548
- }));
549
- });
550
- return Promise.all(promises).then(function () { return result; });
551
- };
552
- /**
553
- * Checks whether loading from given source is supported.
554
- *
555
- * @param src - The source url to load from. This will be passed to `rewriteUrl`.
556
- * @param targetType - The target asset type or a symbol identifying the target type.
557
- */
558
- ContentManager.prototype.canLoad = function (src, targetType) {
559
- var remapped = this.rewriteUrl(src);
560
- if (remapped) {
561
- src = remapped;
562
- }
563
- if (utils.DataUri.isDataUri(src)) {
564
- return this.pipeline.canLoad(ContentType.parse(utils.DataUri.parse(src).contentType).mimeType, targetType);
565
- }
566
- else {
567
- return this.pipeline.canLoad(utils.Uri.ext(src), targetType);
568
- }
569
- };
570
- /**
571
- * Loads and instantiates a single asset
572
- *
573
- * @param src - The source url to load from. This will be passed to `rewriteUrl`.
574
- * @param targetType - The target asset type or a symbol identifying the target type.
575
- * @param options - Options which will be available in the loading context.
576
- */
577
- ContentManager.prototype.load = function (src, targetType, options) {
578
- if (options === void 0) { options = {}; }
579
- return __awaiter(this, void 0, void 0, function () {
580
- var requested, remapped, key, group, uri, source, target, source, target, loader;
581
- var _this = this;
582
- return __generator(this, function (_a) {
583
- requested = utils.Uri.merge(location.pathname, src);
584
- remapped = this.rewriteUrl(requested);
585
- if (requested !== remapped) {
586
- utils.Log.debug("[Content.Manager] remap Url " + src + " => " + remapped);
587
- src = remapped;
588
- }
589
- else {
590
- src = requested;
591
- }
592
- if (utils.DataUri.isDataUri(src)) {
593
- uri = utils.DataUri.parse(src);
594
- source = ContentType.parse(uri.contentType).mimeType;
595
- target = targetType['name'] || targetType.toString();
596
- key = source + " => " + target;
597
- group = "[Content.Manager] load " + key + " (from DataUri '" + uri.contentType + "')";
598
- }
599
- else {
600
- source = src;
601
- target = targetType['name'] || targetType.toString();
602
- key = source + " => " + target;
603
- group = "[Content.Manager] load " + key;
604
- }
605
- if (this.loading.has(key)) {
606
- return [2 /*return*/, this.loading.get(key)];
607
- }
608
- if (this.loaded.has(key)) {
609
- return [2 /*return*/, Promise.resolve(this.loaded.get(key))];
610
- }
611
- utils.Log.group(group);
612
- loader = utils.Log.groupEndAsync(function () { return Pipeline.run({
613
- manager: _this,
614
- source: src,
615
- target: targetType,
616
- options: options,
617
- pipeline: _this.pipeline,
618
- }); });
619
- return [2 /*return*/, this.loading.set(key, loader).get(key).then(function (result) {
620
- _this.loading.delete(key);
621
- _this.loaded.set(key, result);
622
- return result;
623
- })];
624
- });
625
- });
626
- };
627
- /**
628
- * Unloads all currently loaded assets
629
- */
630
- ContentManager.prototype.unload = function () {
631
- this.loaded.forEach(function (value, key) {
632
- try {
633
- if (typeof value.unload === 'function') {
634
- value.unload();
635
- }
636
- }
637
- catch (e) {
638
- utils.Log.warn("[Content.Manager] failed to unload asset at '" + key + "'", e);
639
- }
640
- });
641
- this.loaded.clear();
642
- this.loading.clear();
643
- // TODO: abort ongoing xhr requests
644
- this.downloads.forEach(function (v) { return v.clear(); });
645
- this.downloads.clear();
646
- };
647
- ContentManager.prototype.createHttpOptions = function (optionsOrUrl) {
648
- var options = optionsOrUrl;
649
- if (typeof optionsOrUrl === 'string') {
650
- options = { url: optionsOrUrl };
651
- }
652
- return options;
653
- };
654
- return ContentManager;
655
- }());
656
-
657
- var JsonObject = Symbol('JsonObject');
658
-
659
- /**
660
- * Registeres a loader specification at the default pipeline (`Pipeline.default`) and returns
661
- * without any modification
662
- *
663
- * @public
664
- * @param spec - The loader specification to register
665
- */
666
- function loader() {
667
- var spec;
668
- if (arguments.length === 3) {
669
- spec = {
670
- input: arguments[0],
671
- output: arguments[1],
672
- handle: arguments[3],
673
- };
674
- }
675
- else if (arguments.length === 1) {
676
- spec = arguments[0];
677
- }
678
- else {
679
- throw new Error('invalid arguments');
680
- }
681
- Pipeline.default.register(spec);
682
- return spec;
683
- }
684
- /**
685
- * Resolves a uri reference relative to the source of given context.
686
- *
687
- * @public
688
- * @remarks
689
- * If `refUri` is absolute or is in data uri format then it is returned as is. Otherwise
690
- * it is merged with `context.source`
691
- *
692
- * @param refUri - The uri
693
- * @param context - The context
694
- */
695
- function resolveUri(refUri, context) {
696
- return utils.DataUri.isDataUri(refUri) ? refUri : utils.Uri.merge(context.source, refUri);
697
- }
698
-
699
- exports.ContentManager = ContentManager;
700
- exports.ContentType = ContentType;
701
- exports.JsonObject = JsonObject;
702
- exports.LoaderEntry = LoaderEntry;
703
- exports.Pipeline = Pipeline;
704
- exports.dataFromElement = dataFromElement;
705
- exports.dataFromUri = dataFromUri;
706
- exports.dataFromXhr = dataFromXhr;
707
- exports.loader = loader;
708
- exports.resolveUri = resolveUri;
709
-
710
- Object.defineProperty(exports, '__esModule', { value: true });
711
-
712
- })));
713
- //# sourceMappingURL=content.umd.js.map