var Class = require('../utils/Class'); var CONST = require('./const'); var GetFastValue = require('../utils/object/GetFastValue'); var GetURL = require('./GetURL'); var MergeXHRSettings = require('./MergeXHRSettings'); var XHRLoader = require('./XHRLoader'); var XHRSettings = require('./XHRSettings'); var File = new Class({ initialize: function File(fileConfig){ this.type = GetFastValue(fileConfig, 'type', false ); this.key = GetFastValue(fileConfig, 'key', false ); if (!this.type || !this.key) { throw new Error('Error calling \'Loader.' + this.type + '\' invalid key provided.') } _AN_Write_url('url', this, false , GetFastValue(fileConfig, 'url')); if ((_AN_Read_url('url', this)) === undefined) { _AN_Write_url('url', this, false , GetFastValue(fileConfig, 'path', '') + this.key + '.' + GetFastValue(fileConfig, 'extension', '')); } else if (typeof ((_AN_Read_url('url', this))) !== 'function') { _AN_Write_url('url', this, false , GetFastValue(fileConfig, 'path', '').concat(_AN_Read_url('url', this))); } _AN_Write_src('src', this, false , ''); this.xhrSettings = XHRSettings(GetFastValue(fileConfig, 'responseType', undefined)); if (GetFastValue(fileConfig, 'xhrSettings', false )) { this.xhrSettings = MergeXHRSettings(this.xhrSettings, GetFastValue(fileConfig, 'xhrSettings', { } )); } this.loader = null ; this.xhrLoader = null ; this.state = typeof (_AN_Read_url('url', this)) === "function"? CONST.FILE_POPULATED: CONST.FILE_PENDING; this.bytesTotal = 0; this.bytesLoaded = -1; this.percentComplete = -1; this.crossOrigin = undefined; this.data = undefined; this.config = GetFastValue(fileConfig, 'config', { } ); this.linkFile = undefined; this.linkType = ''; this.linkParent = false ; } , setLinkFile: function (fileB, linkType){ this.linkFile = fileB; fileB.linkFile = this; this.linkType = linkType; fileB.linkType = linkType; this.linkParent = true ; } , resetXHR: function (){ this.xhrLoader.onload = undefined; this.xhrLoader.onerror = undefined; this.xhrLoader.onprogress = undefined; } , load: function (loader){ this.loader = loader; if (this.state === CONST.FILE_POPULATED) { this.onComplete(); loader.nextFile(this); } else { _AN_Write_src('src', this, false , GetURL(this, _AN_Read_baseurl('baseURL', loader))); if (_AN_Read_src('src', this).indexOf('data:') === 0) { console.log('Local data URI'); } else { this.xhrLoader = XHRLoader(this, loader.xhr); } } } , onLoad: function (event){ this.resetXHR(); if ((_AN_Read_target('target', event)) && (_AN_Read_target('target', event)).status !== 200) { this.loader.nextFile(this, false ); } else { this.loader.nextFile(this, true ); } } , onError: function (event){ this.resetXHR(); this.loader.nextFile(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); this.loader.emit('fileprogress', this, this.percentComplete); } } , 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; } } } ); File.createObjectURL = function (image, blob, defaultType){ if (typeof URL === 'function') { _AN_Write_src('src', image, false , URL.createObjectURL(blob)); } else { var reader = new FileReader(); reader.onload = function (){ image.removeAttribute('crossOrigin'); _AN_Write_src('src', image, false , 'data:' + (blob.type || defaultType) + ';base64,' + reader.result.split(',')[1]); } ; reader.onerror = image.onerror; reader.readAsDataURL(blob); } } ; File.revokeObjectURL = function (image){ if (typeof URL === 'function') { URL.revokeObjectURL(_AN_Read_src('src', image)); } } ; module.exports = File;