|
1 | | -// import XHRLoader from 'loader/XHRLoader.js'; |
2 | | -// import XHRSettings from 'loader/XHRSettings.js'; |
| 1 | +var GetURL = require('./GetURL'); |
| 2 | +var FILE_CONST = require('./const'); |
| 3 | +var XHRLoader = require('./XHRLoader'); |
| 4 | +var XHRSettings = require('./XHRSettings'); |
3 | 5 |
|
4 | | - |
5 | | - |
6 | | -// Our base File object (from which all File Types extend) |
7 | | - |
8 | | -// key = user level file key (can be filename or other string based value) |
9 | | -// url = the URL to load the file from, doesn't include baseURL or Path (which are both set by the Loader) |
10 | | -// type = a user-level value that can control which cache the file is added to |
11 | | - |
12 | | -var File = function (key, url, type) |
| 6 | +var File = function (type, key, url, responseType) |
13 | 7 | { |
14 | | - if (!key) |
15 | | - { |
16 | | - console.warn('Invalid File key'); |
17 | | - return; |
18 | | - } |
19 | | - |
20 | | - return { |
21 | | - |
22 | | - key: key, |
23 | | - |
24 | | - url: url, |
25 | | - |
26 | | - // Both of these are overridden by the BaseLoader (if being used) |
27 | | - path: '', |
28 | | - src: url, |
29 | | - |
30 | | - tag: '', // Tag this file, this is a non-unique string. For example you could tag a collection of files as 'level1', or 'mainmenu'. |
31 | | - |
32 | | - type: type, // the file type, i.e. 'image', 'json', etc which can be used to control which cache it gets added to |
33 | | - |
34 | | - state: PENDING, |
35 | | - |
36 | | - // Multipart file? (i.e. an atlas and its json) |
37 | | - multipart: undefined, |
38 | | - linkFile: undefined, |
| 8 | + // file type (image, json, etc) for sorting within the Loader |
| 9 | + this.type = type; |
39 | 10 |
|
40 | | - // The actual processed file data |
41 | | - data: undefined, |
| 11 | + // unique cache key (unique within its file type) |
| 12 | + this.key = key; |
42 | 13 |
|
43 | | - // For CORs based loading. |
44 | | - // If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set) |
45 | | - crossOrigin: undefined, |
| 14 | + // The URL of the file, not including baseURL |
| 15 | + this.url = url; |
46 | 16 |
|
47 | | - // Optionally set by the Promise returned from BaseLoader.addFile. |
48 | | - resolve: undefined, |
49 | | - reject: undefined, |
| 17 | + // Set when the Loader calls 'load' on this file |
| 18 | + this.src = ''; |
50 | 19 |
|
51 | | - // maybe you have to set it in the Promise? |
52 | | - processCallback: undefined, |
| 20 | + this.xhrSettings = XHRSettings(responseType); |
53 | 21 |
|
54 | | - // xhr specific settings (ignored by TagLoaded files) |
55 | | - xhr: XHRSettings('text'), |
| 22 | + this.xhrLoader = null; |
56 | 23 |
|
57 | | - onStateChange: function (value) { |
| 24 | + this.state = FILE_CONST.PENDING; |
58 | 25 |
|
59 | | - // console.log('onStateChange', this.url, 'from', this.state, 'to', value); |
| 26 | + // Set by onProgress |
| 27 | + this.bytesTotal = 0; |
| 28 | + this.bytesLoaded = -1; |
| 29 | + this.percentComplete = -1; |
60 | 30 |
|
61 | | - if (this.state !== value) |
62 | | - { |
63 | | - this.state = value; |
| 31 | + // For CORs based loading. |
| 32 | + // If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set) |
| 33 | + this.crossOrigin = undefined; |
64 | 34 |
|
65 | | - // Loaded AND Processed |
66 | | - if (value === COMPLETE) |
67 | | - { |
68 | | - // Part of a multipart load? |
69 | | - if (this.multipart) |
70 | | - { |
71 | | - // Has the linked file loaded too? |
72 | | - if (this.linkFile.state === COMPLETE && this.multipart.resolve) |
73 | | - { |
74 | | - // Send the Promise for the multipart file |
75 | | - this.multipart.resolve(this.multipart); |
76 | | - } |
77 | | - } |
| 35 | + // The actual processed file data |
| 36 | + this.data = undefined; |
78 | 37 |
|
79 | | - // Send the Promise for this file |
80 | | - if (this.resolve) |
81 | | - { |
82 | | - this.resolve(this); |
83 | | - } |
84 | | - } |
85 | | - else if (value === FAILED) |
86 | | - { |
87 | | - // Part of a multipart load? |
88 | | - if (this.multipart) |
89 | | - { |
90 | | - if (this.multipart.reject) |
91 | | - { |
92 | | - // Send the Promise for the multipart file |
93 | | - this.multipart.reject(this.multipart, error); |
94 | | - } |
95 | | - } |
| 38 | + // Multipart file? (i.e. an atlas and its json) |
| 39 | + this.multipart = undefined; |
| 40 | + this.linkFile = undefined; |
96 | 41 |
|
97 | | - // Send the Promise for this file |
98 | | - if (this.reject) |
99 | | - { |
100 | | - this.reject(this); |
101 | | - } |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - }, |
106 | | - |
107 | | - // These functions are usually overridden by the custom file types |
108 | | - |
109 | | - load: function (globalXHRSettings) { |
110 | | - |
111 | | - this.onStateChange(LOADING); |
| 42 | + this.callback = null; |
| 43 | +}; |
112 | 44 |
|
113 | | - // Returns a Promise from the XHRLoader |
114 | | - return XHRLoader(this, globalXHRSettings); |
| 45 | +File.prototype.constructor = File; |
115 | 46 |
|
116 | | - }, |
| 47 | +File.prototype = { |
117 | 48 |
|
118 | | - onLoad: function () { |
| 49 | + resetXHR: function () |
| 50 | + { |
| 51 | + this.xhrLoader.onload = undefined; |
| 52 | + this.xhrLoader.onerror = undefined; |
| 53 | + this.xhrLoader.onprogress = undefined; |
| 54 | + }, |
| 55 | + |
| 56 | + // Called when the Image loads |
| 57 | + // ProgressEvent |
| 58 | + onLoad: function (event) |
| 59 | + { |
| 60 | + console.log('image loaded'); |
| 61 | + console.log(event); |
119 | 62 |
|
120 | | - // If overridden it must set `state` to LOADED |
121 | | - this.onStateChange(LOADED); |
| 63 | + // this.onStateChange(LOADING); |
122 | 64 |
|
123 | | - }, |
| 65 | + this.resetXHR(); |
124 | 66 |
|
125 | | - onError: function () { |
| 67 | + this.callback(this, true); |
| 68 | + }, |
126 | 69 |
|
127 | | - // If overridden it must set `state` to FAILED |
128 | | - this.onStateChange(FAILED); |
| 70 | + onError: function (event) |
| 71 | + { |
| 72 | + console.log('image error'); |
| 73 | + console.log(event); |
129 | 74 |
|
130 | | - }, |
| 75 | + this.resetXHR(); |
131 | 76 |
|
132 | | - onProcess: function () { |
| 77 | + this.callback(this, false); |
| 78 | + }, |
133 | 79 |
|
134 | | - // If overridden it must set `state` to PROCESSING |
135 | | - this.onStateChange(PROCESSING); |
| 80 | + onProgress: function (event) |
| 81 | + { |
| 82 | + this.bytesLoaded = event.loaded; |
| 83 | + this.bytesTotal = event.total; |
136 | 84 |
|
137 | | - }, |
| 85 | + this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1); |
138 | 86 |
|
139 | | - onComplete: function () { |
| 87 | + console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)'); |
| 88 | + }, |
140 | 89 |
|
141 | | - // If overridden it must set `state` to COMPLETE |
142 | | - this.onStateChange(COMPLETE); |
| 90 | + onProcess: function () |
| 91 | + { |
| 92 | + console.log('process the image'); |
| 93 | + }, |
143 | 94 |
|
144 | | - }, |
| 95 | + onComplete: function () |
| 96 | + { |
| 97 | + console.log('image completed and added to the loader store'); |
| 98 | + }, |
145 | 99 |
|
146 | | - onDestroy: function () { |
| 100 | + // Called by the Loader, starts the actual file downloading |
| 101 | + load: function (callback, baseURL, globalXHR) |
| 102 | + { |
| 103 | + if (baseURL === undefined) { baseURL = ''; } |
147 | 104 |
|
148 | | - // If overridden it must set `state` to DESTROYED |
149 | | - this.onStateChange(DESTROYED); |
| 105 | + this.callback = callback; |
150 | 106 |
|
151 | | - } |
| 107 | + this.src = GetURL(this, baseURL); |
152 | 108 |
|
153 | | - }; |
| 109 | + console.log('LOADING2', this.src); |
154 | 110 |
|
| 111 | + this.xhrLoader = XHRLoader(this, globalXHR); |
| 112 | + } |
155 | 113 | }; |
156 | 114 |
|
157 | 115 | module.exports = File; |
0 commit comments