Skip to content

Commit 1d22f5a

Browse files
committed
File has a reference to its Loader, will also now emit progress events on itself.
1 parent 4473af2 commit 1d22f5a

1 file changed

Lines changed: 39 additions & 35 deletions

File tree

src/loader/File.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ var File = new Class({
4848
this.xhrSettings = MergeXHRSettings(this.xhrSettings, GetFastValue(fileConfig, 'xhrSettings', {}));
4949
}
5050

51+
// The LoaderPlugin instance that is loading this file
52+
this.loader = null;
53+
5154
this.xhrLoader = null;
5255

5356
this.state = CONST.FILE_PENDING;
@@ -70,8 +73,6 @@ var File = new Class({
7073
// Multipart file? (i.e. an atlas and its json together)
7174
this.linkFile = undefined;
7275
this.linkType = '';
73-
74-
this.callback = null;
7576
},
7677

7778
resetXHR: function ()
@@ -81,26 +82,54 @@ var File = new Class({
8182
this.xhrLoader.onprogress = undefined;
8283
},
8384

85+
// Called by the Loader, starts the actual file downloading.
86+
// During the load the methods onLoad, onProgress, etc are called based on the XHR events.
87+
load: function (loader)
88+
{
89+
this.loader = loader;
90+
91+
if (this.state === CONST.FILE_POPULATED)
92+
{
93+
this.onComplete();
94+
95+
loader.nextFile(this);
96+
}
97+
else
98+
{
99+
this.src = GetURL(this, loader.baseURL);
100+
101+
if (this.src.indexOf('data:') === 0)
102+
{
103+
console.log('Local data URI');
104+
}
105+
else
106+
{
107+
this.xhrLoader = XHRLoader(this, loader.xhr);
108+
}
109+
}
110+
},
111+
84112
// Called when the file loads, is sent a DOM ProgressEvent
85113
onLoad: function (event)
86114
{
87115
this.resetXHR();
88116

89117
if (event.target && event.target.status !== 200)
90118
{
91-
this.callback(this, false);
119+
this.loader.nextFile(this, false);
92120
}
93121
else
94122
{
95-
this.callback(this, true);
123+
this.loader.nextFile(this, true);
96124
}
97125
},
98126

127+
// Called when the file errors, is sent a DOM ProgressEvent
99128
onError: function (event)
100129
{
101130
this.resetXHR();
102131

103-
this.callback(this, false);
132+
this.loader.nextFile(this, false);
104133
},
105134

106135
onProgress: function (event)
@@ -111,11 +140,14 @@ var File = new Class({
111140
this.bytesTotal = event.total;
112141

113142
this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1);
114-
}
115143

116-
// console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)');
144+
// console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)');
145+
this.loader.emit('fileprogress', this, this.percentComplete);
146+
}
117147
},
118148

149+
// Usually overriden by the FileTypes and is called by Loader.finishedLoading.
150+
// The callback is Loader.processUpdate
119151
onProcess: function (callback)
120152
{
121153
this.state = CONST.FILE_PROCESSING;
@@ -145,34 +177,6 @@ var File = new Class({
145177
{
146178
this.state = CONST.FILE_COMPLETE;
147179
}
148-
},
149-
150-
// Called by the Loader, starts the actual file downloading
151-
load: function (callback, baseURL, globalXHR)
152-
{
153-
if (baseURL === undefined) { baseURL = ''; }
154-
155-
this.callback = callback;
156-
157-
if (this.state === CONST.FILE_POPULATED)
158-
{
159-
this.onComplete();
160-
161-
callback(this);
162-
}
163-
else
164-
{
165-
this.src = GetURL(this, baseURL);
166-
167-
if (this.src.indexOf('data:') === 0)
168-
{
169-
console.log('Local data URI');
170-
}
171-
else
172-
{
173-
this.xhrLoader = XHRLoader(this, globalXHR);
174-
}
175-
}
176180
}
177181

178182
});

0 commit comments

Comments
 (0)