forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.js
More file actions
121 lines (86 loc) · 2.83 KB
/
Copy pathFile.js
File metadata and controls
121 lines (86 loc) · 2.83 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
var GetURL = require('./GetURL');
var CONST = require('./const');
var XHRLoader = require('./XHRLoader');
var XHRSettings = require('./XHRSettings');
var File = function (type, key, url, responseType)
{
// file type (image, json, etc) for sorting within the Loader
this.type = type;
// unique cache key (unique within its file type)
this.key = key;
// The URL of the file, not including baseURL
this.url = url;
// Set when the Loader calls 'load' on this file
this.src = '';
this.xhrSettings = XHRSettings(responseType);
this.xhrLoader = null;
this.state = CONST.FILE_PENDING;
// Set by onProgress (only if loading via XHR)
this.bytesTotal = 0;
this.bytesLoaded = -1;
this.percentComplete = -1;
// For CORs based loading.
// If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set)
this.crossOrigin = undefined;
// The actual processed file data
this.data = undefined;
// Multipart file? (i.e. an atlas and its json together)
this.multipart = undefined;
this.linkFile = undefined;
this.callback = null;
};
File.prototype.constructor = File;
File.prototype = {
resetXHR: function ()
{
this.xhrLoader.onload = undefined;
this.xhrLoader.onerror = undefined;
this.xhrLoader.onprogress = undefined;
},
// Called when the Image loads
// ProgressEvent
onLoad: function (event)
{
console.log('image loaded');
// console.log(event);
// this.onStateChange(LOADING);
// this.process();
this.resetXHR();
this.callback(this, true);
},
onError: function (event)
{
// console.log('image error');
// console.log(event);
this.resetXHR();
this.callback(this, false);
},
onProgress: function (event)
{
this.bytesLoaded = event.loaded;
this.bytesTotal = event.total;
this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1);
console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)');
},
onProcess: function (callback)
{
this.state = CONST.FILE_PROCESSING;
// If overridden by another class, it must call the callback when finished, then onComplete
callback(this);
this.onComplete();
},
onComplete: function ()
{
console.log('File completed, ready to add to the Loader store');
this.state = CONST.FILE_COMPLETE;
},
// Called by the Loader, starts the actual file downloading
load: function (callback, baseURL, globalXHR)
{
if (baseURL === undefined) { baseURL = ''; }
this.callback = callback;
this.src = GetURL(this, baseURL);
this.xhrLoader = XHRLoader(this, globalXHR);
}
};
module.exports = File;