var GetURL = require('./GetURL'); var CONST = require('./const'); var XHRLoader = require('./XHRLoader'); var XHRSettings = require('./XHRSettings'); var MergeXHRSettings = require('./MergeXHRSettings'); var File = function (type, key, url, responseType, xhrSettings){ this.type = type; this.key = key; _AN_Write_url('url', this, false , url); _AN_Write_src('src', this, false , ''); this.xhrSettings = XHRSettings(responseType); if (xhrSettings) { this.xhrSettings = MergeXHRSettings(this.xhrSettings, xhrSettings); } this.xhrLoader = null ; this.state = CONST.FILE_PENDING; this.bytesTotal = 0; this.bytesLoaded = -1; this.percentComplete = -1; this.crossOrigin = undefined; this.data = undefined; this.linkFile = undefined; this.linkType = ''; this.callback = null ; } ; File.prototype.constructor = File; File.prototype = { resetXHR: function (){ this.xhrLoader.onload = undefined; this.xhrLoader.onerror = undefined; this.xhrLoader.onprogress = undefined; } , onLoad: function (event){ this.resetXHR(); this.callback(this, true ); } , onError: function (event){ this.resetXHR(); this.callback(this, false ); } , onProgress: function (event){ if (event.lengthComputable) { this.bytesLoaded = event.loaded; this.bytesTotal = event.total; this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1); } } , onProcess: function (callback){ this.state = CONST.FILE_PROCESSING; this.onComplete(); callback(this); } , onComplete: function (){ if (this.linkFile) { if (this.linkFile.state === CONST.FILE_WAITING_LINKFILE) { this.state = CONST.FILE_COMPLETE; this.linkFile.state = CONST.FILE_COMPLETE; } else { this.state = CONST.FILE_WAITING_LINKFILE; } } else { this.state = CONST.FILE_COMPLETE; } } , load: function (callback, baseURL, globalXHR){ if (baseURL === undefined) { baseURL = ''; } this.callback = callback; _AN_Write_src('src', this, false , GetURL(this, baseURL)); if (_AN_Read_src('src', this).indexOf('data:') === 0) { console.log('Local data URI'); } else { this.xhrLoader = XHRLoader(this, globalXHR); } } } ; module.exports = File;