forked from xml3d/xml3d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresourcemanager.js
More file actions
713 lines (630 loc) · 28.3 KB
/
resourcemanager.js
File metadata and controls
713 lines (630 loc) · 28.3 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
(function() {
"use strict";
var c_cachedDocuments = {};
var c_factories = {};
var c_cachedAdapterHandles = {};
var c_canvasIdCounters = {};
var c_formatHandlers = [];
var c_binaryContentTypes = ["application/octet-stream", "text/plain; charset=x-user-defined"];
var c_binaryExtensions = [".bin", ".bson"];
/**
* Register a factory with the resource manager
* @param {XML3D.base.AdapterFactory} factory - the factory to be registered
*/
XML3D.base.registerFactory = function(factory) {
var canvasId = factory.canvasId;
if (!c_factories[canvasId])
c_factories[canvasId] = [];
c_factories[canvasId].push(factory);
};
XML3D.base.registerFormat = function(formatHandler) {
if (formatHandler)
c_formatHandlers.push(formatHandler);
}
XML3D.base.findFormat = function(response, responseType, mimetype) {
for (var i = 0; i < c_formatHandlers.length; ++i) {
var formatHandler = c_formatHandlers[i];
if (c_formatHandlers[i].isFormatSupported(response, responseType, mimetype)) {
return formatHandler;
}
}
return null;
}
/**
* @constructor
*/
var ResourceManager = function() {
};
function getCounterObject(canvasId) {
return c_canvasIdCounters[canvasId];
}
function getOrCreateCounterObject(canvasId) {
var counterObject = c_canvasIdCounters[canvasId];
if (!counterObject) {
counterObject = {counter: 0, listeners: new Array()};
c_canvasIdCounters[canvasId] = counterObject;
}
return counterObject;
}
function notifyLoadCompleteListeners(counterObject) {
var listeners = counterObject.listeners;
counterObject.listeners = new Array();
var i = listeners.length;
while (i--) {
listeners[i](this);
}
}
function loadComplete(canvasId, url) {
// notify all load complete listeners
var counterObject = getCounterObject(canvasId);
if (counterObject) {
XML3D.debug.assert(counterObject.counter > 0, "counter must be > 0");
counterObject.counter--;
if (counterObject.counter == 0) {
notifyLoadCompleteListeners(counterObject);
}
}
}
/*
* Register listener that will be fired when all resources for specified canvasId are loaded.
* Listener is fired only once.
*
* @param {number} canvasId
* @param {EventListener} listener
*/
ResourceManager.prototype.addLoadCompleteListener = function(canvasId, listener) {
var counterObject = getCounterObject(canvasId);
// when counter is 0 we can fire event immediately
if (counterObject === undefined || counterObject.counter == 0) {
listener(canvasId);
return;
}
var idx = counterObject.listeners.indexOf(listener);
if (idx == -1) {
counterObject.listeners.push(listener);
}
};
ResourceManager.prototype.removeLoadCompleteListener = function(canvasId, listener) {
var counterObject = getCounterObject(canvasId);
if (counterObject) {
var idx = counterObject.listeners.indexOf(listener);
if (idx != -1)
counterObject.listeners.splice(idx, 1);
}
};
function stringEndsWithSuffix(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
ResourceManager.prototype.addBinaryContentType = function(type) {
if (c_binaryContentTypes.indexOf(type) == -1)
c_binaryContentTypes.push(type);
};
ResourceManager.prototype.removeBinaryContentType = function(type) {
var idx = c_binaryContentTypes.indexOf(type);
if (idx != -1)
c_binaryContentTypes.splice(idx, 1);
};
function isBinaryContentType(contentType) {
for (var i in c_binaryContentTypes) {
if (contentType == c_binaryContentTypes[i]) {
return true;
}
}
return false;
}
ResourceManager.prototype.addBinaryExtension = function(extension) {
if (c_binaryExtensions.indexOf(extension) == -1)
c_binaryExtensions.push(extension);
};
ResourceManager.prototype.removeBinaryExtension = function(extension) {
var idx = c_binaryExtensions.indexOf(extension);
if (idx != -1)
c_binaryExtensions.splice(idx, 1);
};
function isBinaryExtension(url) {
for (var i in c_binaryExtensions) {
if (stringEndsWithSuffix(url, c_binaryExtensions[i]))
return true;
}
return false;
}
/**
* Load a document via XMLHttpRequest
* @private
* @param {string} url URL of the document
*/
function loadDocument(url) {
var xmlHttp = null;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
xmlHttp = null;
}
if (xmlHttp) {
xmlHttp._url = url;
xmlHttp._contentChecked = false;
xmlHttp.open('GET', url, true);
if (isBinaryExtension(url))
xmlHttp.responseType = "arraybuffer";
xmlHttp.onreadystatechange = function() {
if (xmlHttp._aborted) // This check is possibly not needed
return;
// check compatibility between content and request mode
if (!xmlHttp._contentChecked &&
// 2 - HEADERS_RECEIVED, 3 - LOADING, 4 - DONE
((xmlHttp.readyState == 2 || xmlHttp.readyState == 3 || xmlHttp.readyState == 4) &&
xmlHttp.status == 200)) {
xmlHttp._contentChecked = true; // we check only once
// check if we need to change request mode
var contentType = xmlHttp.getResponseHeader("content-type");
if (contentType) {
var binaryContent = isBinaryContentType(contentType);
var binaryRequest = (xmlHttp.responseType == "arraybuffer");
// When content is not the same as request, we need to repeat request
if (binaryContent != binaryRequest) {
xmlHttp._aborted = true;
var cb = xmlHttp.onreadystatechange;
xmlHttp.onreadystatechange = null;
var url = xmlHttp._url;
xmlHttp.abort();
// Note: We do not recycle XMLHttpRequest !
// This does work only when responseType is changed to "arraybuffer",
// however the size of the xmlHttp.response buffer is then wrong !
// It does not work at all (at least in Chrome) when we use overrideMimeType
// with "text/plain; charset=x-user-defined" argument.
// The latter mode require creation of the fresh XMLHttpRequest.
xmlHttp = new XMLHttpRequest();
xmlHttp._url = url;
xmlHttp._contentChecked = true;
xmlHttp.open('GET', url, true);
if (binaryContent)
xmlHttp.responseType = "arraybuffer";
xmlHttp.onreadystatechange = cb;
xmlHttp.send(null);
return;
}
}
}
// Request mode and content type are compatible here (both binary or both text)
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
XML3D.debug.logDebug("Loaded: " + xmlHttp._url);
XML3D.xmlHttpCallback && XML3D.xmlHttpCallback(xmlHttp);
processResponse(xmlHttp);
}
else {
XML3D.debug.logError("Could not load external document '" + xmlHttp._url +
"': " + xmlHttp.status + " - " + xmlHttp.statusText);
invalidateDocumentHandles(xmlHttp._url);
}
}
};
xmlHttp.send(null);
}
};
/**
* Process response of ajax request from loadDocument()
* @private
* @param {XMLHttpRequest} httpRequest
*/
function processResponse(httpRequest) {
var mimetype = httpRequest.getResponseHeader("content-type");
setDocumentData(httpRequest, httpRequest._url, mimetype);
};
/**
* Initialize data of a received document
* @private
* @param {XMLHttpRequest} httpRequest The XMLHttpRequest of the loaded document
* @param {string} url URL of the loaded document
* @param {string} mimetype The mimetype of the loaded document
*/
function setDocumentData(httpRequest, url, mimetype) {
var docCache = c_cachedDocuments[url];
docCache.mimetype = mimetype;
var cleanedMimetype = mimetype;
if (mimetype.indexOf(';') > 0)
cleanedMimetype = mimetype.substr(0, mimetype.indexOf(';'));
var response = null;
if (httpRequest.responseType == "arraybuffer") {
response = httpRequest.response;
} else if (cleanedMimetype == "application/json") {
response = JSON.parse(httpRequest.responseText);
} else if (cleanedMimetype == "application/xml" || cleanedMimetype == "text/xml") {
response = httpRequest.responseXML;
if (!response) {
XML3D.debug.logError("Invalid external XML document '" + httpRequest._url +
"': XML Syntax error");
return;
}
} else if (cleanedMimetype == "application/octet-stream" || mimetype == "text/plain; charset=x-user-defined") {
XML3D.debug.logError("Possibly wrong loading of resource " + url + ". Mimetype is " + mimetype + " but response is not an ArrayBuffer");
response = httpRequest.response;
} else {
XML3D.debug.logError("Unidentified response type (response = '" + httpRequest.response + "', responseType = '" + httpRequest.responseType + "')");
response = httpRequest.response;
}
var formatHandler = XML3D.base.findFormat(response, httpRequest.responseType, cleanedMimetype);
if (!formatHandler) {
XML3D.debug.logError("No format handler for resource (response = '" + response + "', responseType = '" + httpRequest.responseType + "')");
return;
}
docCache.format = formatHandler;
formatHandler.getFormatData(response, httpRequest.responseType, cleanedMimetype, function(success, result){
if(success){
docCache.response = result;
updateDocumentHandles(url)
}
else{
invalidateDocumentHandles(url);
}
} );
}
/**
* Update all existing handles of a received document
* @param {string} url The URL of the document
*/
function updateDocumentHandles(url) {
var docCache = c_cachedDocuments[url];
var fragments = docCache.fragments;
docCache.fragments = [];
for (var i = 0; i < fragments.length; ++i) {
updateExternalHandles(url, fragments[i]);
}
}
/**
* Invalidate all handles of a document, that could not be loaded.
* @param {string} url The URL of the document
*/
function invalidateDocumentHandles(url) {
var docCache = c_cachedDocuments[url];
var fragments = docCache.fragments;
docCache.fragments = [];
for (var i = 0; i < fragments.length; ++i) {
var fullUrl = url + (fragments[i] ? "#" + fragments[i] : "");
invalidateHandles(fullUrl);
}
}
/**
* Update all handles of a part from an external document
* @param {string} url The URL of the document
* @param {string} fragment Fragment without pound key which defines the part of the document
*/
function updateExternalHandles(url, fragment) {
var response = c_cachedDocuments[url].response;
var mimetype = c_cachedDocuments[url].mimetype;
var format = c_cachedDocuments[url].format;
var fullUrl = url + (fragment ? "#" + fragment : "");
if (!response) {
// In the case the loaded document is not supported we still need to decrement counter object
invalidateHandles(fullUrl);
return;
}
// get part of the resource represented by the fragment
var data = format.getFragmentData(response, fragment);
if (data) {
updateMissingHandles(fullUrl, format, data);
}
else {
invalidateHandles(fullUrl);
}
}
/**
* Update all AdapterHandles without adapters of a certain url
* @param {string} url The complete url + fragment
* @param {FormatHandler} formatHandler Format handler
* @param {Object} data Data of the document corresponding to the url. Possibily a DOM element
*/
function updateMissingHandles(url, formatHandler, data) {
for (var adapterType in c_cachedAdapterHandles[url]) {
for (var canvasId in c_cachedAdapterHandles[url][adapterType]) {
var handle = c_cachedAdapterHandles[url][adapterType][canvasId];
if (!handle.hasAdapter()) {
updateHandle(handle, adapterType, canvasId, formatHandler, data);
loadComplete(canvasId, url);
}
}
}
}
/**
* Invalidate all AdapterHandles without adapters of a certain url
* @param {string} url The complete url + fragment
*/
function invalidateHandles(url) {
for (var adapterType in c_cachedAdapterHandles[url]) {
for (var canvasId in c_cachedAdapterHandles[url][adapterType]) {
var handle = c_cachedAdapterHandles[url][adapterType][canvasId];
handle.setAdapter(null, XML3D.base.AdapterHandle.STATUS.NOT_FOUND);
loadComplete(canvasId, url);
}
}
}
/**
* Update a specific AdapterHandle with the provided data.
* Internally an adapter will be created with 'data' and added to 'handle'
* All other argument are required to finde the correct factory
* @param {XML3D.base.AdapterHandle} handle The AdapterHandle to be updated
* @param {Object} adapterType The type / aspect of the adapter (e.g. XML3D.data or XML3D.webgl)
* @param {number} canvasId Id of corresponding canvas handler. 0 if not dependent of canvas handler
* @param {FormatHandler} format Format handler of the corresponding document
* @param {Object} data Data for this handle. Possibily a DOM element
*/
function updateHandle(handle, adapterType, canvasId, format, data) {
var factory = format.getFactory(adapterType, canvasId);
if(!factory) {
XML3D.debug.logError("Format does not support adapterType " + adapterType);
return;
}
var adapter = factory.getAdapter ? factory.getAdapter(data) : factory.createAdapter(data);
if (adapter) {
handle.setAdapter(adapter, XML3D.base.AdapterHandle.STATUS.READY);
}
}
/**
* Remove the adapter of all AdapterHandles corresponding to the given URL.
* This is called e.g. when a node is remove from the document, or an id changes
* @param {string} url The URL of all AdapterHandles to be cleared.
*/
function clearHandles(url) {
for (var adapterType in c_cachedAdapterHandles[url]) {
for (var canvasId in c_cachedAdapterHandles[url][adapterType]) {
var handle = c_cachedAdapterHandles[url][adapterType][canvasId];
if (handle.hasAdapter()) {
handle.setAdapter(null, XML3D.base.AdapterHandle.STATUS.NOT_FOUND);
}
}
}
}
/**
* Get any adapter, internal or external.
* This function will trigger the loading of documents, if required.
* An AdapterHandle will be always be returned, expect when an invalid (empty) uri is passed.
*
* @param {Document} baseDocument - the document from which to look up the reference
* @param {XML3D.URI} uri - The URI used to find the referred AdapterHandle. Can be relative
* @param {Object} adapterType The type of adapter required (e.g. XML3D.data or XML3D.webgl)
* @param {number=} canvasId Id of canvashandle this adapter depends on, 0 if not depending on any canvashandler
* @returns {?XML3D.base.AdapterHandle} The requested AdapterHandler. Note: might be null
*/
ResourceManager.prototype.getAdapterHandle = function(baseDocument, uri, adapterType, canvasId) {
if (!uri)
return null;
if (typeof uri == "string") uri = new XML3D.URI(uri);
canvasId = canvasId || 0;
if (baseDocument != document || !uri.isLocal()) {
uri = uri.getAbsoluteURI(baseDocument.documentURI);
}
if (!c_cachedAdapterHandles[uri])
c_cachedAdapterHandles[uri] = {};
if (!c_cachedAdapterHandles[uri][adapterType]) {
c_cachedAdapterHandles[uri][adapterType] = {};
}
var handle = c_cachedAdapterHandles[uri][adapterType][canvasId];
if (handle)
return handle;
var handle = new XML3D.base.AdapterHandle(uri);
c_cachedAdapterHandles[uri][adapterType][canvasId] = handle;
if (uri.isLocal()) {
var node = XML3D.URIResolver.resolveLocal(uri);
if (node)
updateHandle(handle, adapterType, canvasId, XML3D.base.xml3dFormatHandler, node);
else
handle.setAdapter(null, XML3D.base.AdapterHandle.STATUS.NOT_FOUND);
}
else {
var counterObject = getOrCreateCounterObject(canvasId);
counterObject.counter++;
var docURI = uri.toStringWithoutFragment();
var docData = c_cachedDocuments[docURI];
if (docData && docData.response) {
updateExternalHandles(docURI, uri.fragment);
} else {
if (!docData) {
loadDocument(docURI);
c_cachedDocuments[docURI] = docData = {
fragments: []
};
}
docData.fragments.push(uri.fragment);
}
}
return handle;
};
/**
* Get any adapter, internal or external.
*
* @param node
* @param adapterType
* @param canvasId
* @return {XML3D.base.Adapter?}
*/
ResourceManager.prototype.getAdapter = function(node, adapterType, canvasId) {
var factory = XML3D.base.xml3dFormatHandler.getFactory(adapterType, canvasId);
if (factory) {
return factory.getAdapter(node);
}
return null;
}
/**
* This function is called when an id of an element changes or if that element is now reachable
* or not reachable anymore. It will update all AdapterHandles connected to the element.
* @param {Element} node Element of which id has changed
* @param {string} previousId Previous id of element
* @param {string} newId New id of element
*/
ResourceManager.prototype.notifyNodeIdChange = function(node, previousId, newId) {
var parent = node;
while (parent.parentNode) parent = parent.parentNode;
if (parent != window.document)
return;
// clear cached adapters of previous id"
if (previousId) {
clearHandles("#" + previousId);
}
if (newId) {
updateMissingHandles("#" + newId, XML3D.base.xml3dFormatHandler, node);
}
}
/**
* This function is called to notify an AdapterHandler about a change (can be triggered through adapters)
* Note that this function only works with nodes inside window.document
* @param {Element} element Element of AdapterHandler. Must be from window.document
* @param {Object} adapterType Type/Aspect of AdapterHandler (e.g. XML3D.data or XML3D.webgl)
* @param {number} canvasId CanvasHandler id of AdapterHandler, 0 if not depending on CanvasHandler
* @param {number} type Type of Notification. Usually XML3D.events.ADAPTER_HANDLE_CHANGED
*/
ResourceManager.prototype.notifyNodeAdapterChange = function(element, adapterType, canvasId, type) {
canvasId = canvasId || 0;
var uri = "#" + element.id;
if (c_cachedAdapterHandles[uri] && c_cachedAdapterHandles[uri][adapterType] &&
c_cachedAdapterHandles[uri][adapterType][canvasId]) {
c_cachedAdapterHandles[uri][adapterType][canvasId].notifyListeners(type);
}
}
/**
* Load data via XMLHttpRequest
* @private
* @param {string} url URL of the document
* @param {function(object)} loadListener Gets the response of the XHR
* @param {function(XMLHttpRequest)} errorListener Get the XHR object for further analyzis
*/
ResourceManager.prototype.loadData = function(url, loadListener, errorListener) {
var xmlHttp = null;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
xmlHttp = null;
}
if (xmlHttp) {
xmlHttp._url = url;
xmlHttp._contentChecked = false;
xmlHttp.open('GET', url, true);
if (isBinaryExtension(url))
xmlHttp.responseType = "arraybuffer";
xmlHttp.onreadystatechange = function() {
if (xmlHttp._aborted) // This check is possibly not needed
return;
// check compatibility between content and request mode
if (!xmlHttp._contentChecked &&
// 2 - HEADERS_RECEIVED, 3 - LOADING, 4 - DONE
((xmlHttp.readyState == 2 || xmlHttp.readyState == 3 || xmlHttp.readyState == 4) &&
xmlHttp.status == 200)) {
xmlHttp._contentChecked = true; // we check only once
// check if we need to change request mode
var contentType = xmlHttp.getResponseHeader("content-type");
if (contentType) {
var binaryContent = isBinaryContentType(contentType);
var binaryRequest = (xmlHttp.responseType == "arraybuffer");
// When content is not the same as request, we need to repeat request
if (binaryContent != binaryRequest) {
xmlHttp._aborted = true;
var cb = xmlHttp.onreadystatechange;
xmlHttp.onreadystatechange = null;
var url = xmlHttp._url;
xmlHttp.abort();
// Note: We do not recycle XMLHttpRequest !
// This does work only when responseType is changed to "arraybuffer",
// however the size of the xmlHttp.response buffer is then wrong !
// It does not work at all (at least in Chrome) when we use overrideMimeType
// with "text/plain; charset=x-user-defined" argument.
// The latter mode require creation of the fresh XMLHttpRequest.
xmlHttp = new XMLHttpRequest();
xmlHttp._url = url;
xmlHttp._contentChecked = true;
xmlHttp.open('GET', url, true);
if (binaryContent)
xmlHttp.responseType = "arraybuffer";
xmlHttp.onreadystatechange = cb;
xmlHttp.send(null);
return;
}
}
}
// Request mode and content type are compatible here (both binary or both text)
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
XML3D.debug.logDebug("Loaded: " + xmlHttp._url);
var mimetype = xmlHttp.getResponseHeader("content-type");
var response = null;
if (xmlHttp.responseType == "arraybuffer") {
response = xmlHttp.response;
} else if (mimetype == "application/json") {
response = JSON.parse(xmlHttp.responseText);
} else if (mimetype == "application/xml" || mimetype == "text/xml") {
response = xmlHttp.responseXML;
} else if (mimetype == "application/octet-stream" || mimetype == "text/plain; charset=x-user-defined") {
XML3D.debug.logError("Possibly wrong loading of resource " + url + ". Mimetype is " + mimetype + " but response is not an ArrayBuffer");
response = xmlHttp.responseText; // FIXME is this correct ?
}
if (loadListener)
loadListener(response);
}
else {
XML3D.debug.logError("Could not load external document '" + xmlHttp._url +
"': " + xmlHttp.status + " - " + xmlHttp.statusText);
if (errorListener)
errorListener(xmlHttp);
}
}
};
xmlHttp.send(null);
}
};
/**
* This function is called to load an Image.
*
* @param {string} uri Image URI
* @param {function(Event, HTMLImageElement)} loadListener Function called when image was successfully loaded.
* It will be called with event as the first and image as the second parameter.
* @param {function(Event, HTMLImageElement)} errorListener Function called when image could not be loaded.
* It will be called with event as the first and image as the second parameter.
* @return {HTMLImageElement}
*/
ResourceManager.prototype.getImage = function(uri, loadListener, errorListener) {
// we use canvasId 0 to represent images loaded in a document
getOrCreateCounterObject(0).counter++;
var image = new Image();
image.onload = function(e) {
loadComplete(0, uri);
loadListener(e, image);
};
image.onerror = function(e) {
loadComplete(0, uri);
errorListener(e, image);
};
image.crossOrigin = "anonymous";
image.src = uri; // here loading starts
return image;
}
/**
* This function is called to load a Video.
*
* @param {string} uri Video URI
* @param {boolean} autoplay
* @param {Object} listeners Dictionary of all listeners to register with video element.
* Listeners will be called with event as the first and video as the second parameter.
* @return {HTMLVideoElement}
*/
ResourceManager.prototype.getVideo = function(uri, autoplay, listeners) {
// we use canvasId 0 to represent videos loaded in a document
getOrCreateCounterObject(0).counter++;
var video = document.createElement("video");
function loadCompleteCallback(event) {
loadComplete(0, uri);
}
video.addEventListener("canplay", loadCompleteCallback, true);
video.addEventListener("error", loadCompleteCallback, true);
video.crossorigin = "anonymous";
video.autoplay = autoplay;
function createCallback(listener) {
return function(event) {
listener(event, video);
};
}
for (var eventName in listeners) {
video.addEventListener(eventName, createCallback(listeners[eventName]), true);
}
video.src = uri; // here loading starts
return video;
}
XML3D.base.resourceManager = new ResourceManager();
})();