2016-12-01 12:50:58 +00:00
|
|
|
var GetURL = require('./GetURL');
|
2016-12-06 15:15:42 +00:00
|
|
|
var CONST = require('./const');
|
2016-12-01 12:50:58 +00:00
|
|
|
var XHRLoader = require('./XHRLoader');
|
|
|
|
var XHRSettings = require('./XHRSettings');
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
var File = function (type, key, url, responseType)
|
2016-11-30 01:24:33 +00:00
|
|
|
{
|
2016-12-01 12:50:58 +00:00
|
|
|
// file type (image, json, etc) for sorting within the Loader
|
|
|
|
this.type = type;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
// unique cache key (unique within its file type)
|
|
|
|
this.key = key;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
// The URL of the file, not including baseURL
|
|
|
|
this.url = url;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
// Set when the Loader calls 'load' on this file
|
|
|
|
this.src = '';
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.xhrSettings = XHRSettings(responseType);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.xhrLoader = null;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-06 15:15:42 +00:00
|
|
|
this.state = CONST.FILE_PENDING;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-06 16:49:29 +00:00
|
|
|
// Set by onProgress (only if loading via XHR)
|
2016-12-01 12:50:58 +00:00
|
|
|
this.bytesTotal = 0;
|
|
|
|
this.bytesLoaded = -1;
|
|
|
|
this.percentComplete = -1;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
// For CORs based loading.
|
|
|
|
// If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set)
|
|
|
|
this.crossOrigin = undefined;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
// The actual processed file data
|
|
|
|
this.data = undefined;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-06 16:49:29 +00:00
|
|
|
// Multipart file? (i.e. an atlas and its json together)
|
2016-12-01 12:50:58 +00:00
|
|
|
this.multipart = undefined;
|
|
|
|
this.linkFile = undefined;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.callback = null;
|
|
|
|
};
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
File.prototype.constructor = File;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
File.prototype = {
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
resetXHR: function ()
|
|
|
|
{
|
|
|
|
this.xhrLoader.onload = undefined;
|
|
|
|
this.xhrLoader.onerror = undefined;
|
|
|
|
this.xhrLoader.onprogress = undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called when the Image loads
|
|
|
|
// ProgressEvent
|
|
|
|
onLoad: function (event)
|
|
|
|
{
|
2016-12-07 00:04:32 +00:00
|
|
|
// console.log('image loaded');
|
|
|
|
// console.log(event);
|
2016-12-01 12:50:58 +00:00
|
|
|
// this.onStateChange(LOADING);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-07 00:04:32 +00:00
|
|
|
this.process();
|
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.resetXHR();
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.callback(this, true);
|
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
onError: function (event)
|
|
|
|
{
|
2016-12-07 00:04:32 +00:00
|
|
|
// console.log('image error');
|
|
|
|
// console.log(event);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.resetXHR();
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.callback(this, false);
|
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
onProgress: function (event)
|
|
|
|
{
|
|
|
|
this.bytesLoaded = event.loaded;
|
|
|
|
this.bytesTotal = event.total;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)');
|
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-07 00:04:32 +00:00
|
|
|
process: function ()
|
2016-12-01 12:50:58 +00:00
|
|
|
{
|
2016-12-07 00:04:32 +00:00
|
|
|
// Override by an extending class
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
onComplete: function ()
|
|
|
|
{
|
2016-12-07 00:04:32 +00:00
|
|
|
console.log('File completed, ready to add to the Loader store');
|
|
|
|
|
|
|
|
this.state = CONST.FILE_COMPLETE;
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
// Called by the Loader, starts the actual file downloading
|
|
|
|
load: function (callback, baseURL, globalXHR)
|
|
|
|
{
|
|
|
|
if (baseURL === undefined) { baseURL = ''; }
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.callback = callback;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.src = GetURL(this, baseURL);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.xhrLoader = XHRLoader(this, globalXHR);
|
|
|
|
}
|
2016-11-30 01:24:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = File;
|