This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
forked from cappuccino/cappuccino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticResource.js
312 lines (240 loc) · 8.35 KB
/
StaticResource.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
var rootResources = { };
function StaticResource(/*CFURL*/ aURL, /*StaticResource*/ aParent, /*BOOL*/ isDirectory, /*BOOL*/ isResolved)
{
this._parent = aParent;
this._eventDispatcher = new EventDispatcher(this);
var name = aURL.absoluteURL().lastPathComponent() || aURL.schemeAndAuthority();
this._name = name;
this._URL = aURL; //new CFURL(aName, aParent && aParent.URL().asDirectoryPathURL());
this._isResolved = !!isResolved;
if (isDirectory)
this._URL = this._URL.asDirectoryPathURL();
if (!aParent)
rootResources[name] = this;
this._isDirectory = !!isDirectory;
this._isNotFound = NO;
if (aParent)
aParent._children[name] = this;
if (isDirectory)
this._children = { };
else
this._contents = "";
}
StaticResource.rootResources = function()
{
return rootResources;
}
exports.StaticResource = StaticResource;
function resolveStaticResource(/*StaticResource*/ aResource)
{
aResource._isResolved = YES;
aResource._eventDispatcher.dispatchEvent(
{
type:"resolve",
staticResource:aResource
});
}
StaticResource.prototype.resolve = function()
{
if (this.isDirectory())
{
var bundle = new CFBundle(this.URL());
// Eat any errors.
bundle.onerror = function() { };
// The bundle will actually resolve this node.
bundle.load(NO);
}
else
{
var self = this;
function onsuccess(/*anEvent*/ anEvent)
{
self._contents = anEvent.request.responseText();
resolveStaticResource(self);
}
function onfailure()
{
self._isNotFound = YES;
resolveStaticResource(self);
}
new FileRequest(this.URL(), onsuccess, onfailure);
}
}
StaticResource.prototype.name = function()
{
return this._name;
}
StaticResource.prototype.URL = function()
{
return this._URL;
}
StaticResource.prototype.contents = function()
{
return this._contents;
}
StaticResource.prototype.children = function()
{
return this._children;
}
StaticResource.prototype.parent = function()
{
return this._parent;
}
StaticResource.prototype.isResolved = function()
{
return this._isResolved;
}
StaticResource.prototype.write = function(/*String*/ aString)
{
this._contents += aString;
}
function rootResourceForAbsoluteURL(/*CFURL*/ anAbsoluteURL)
{
var schemeAndAuthority = anAbsoluteURL.schemeAndAuthority(),
resource = rootResources[schemeAndAuthority];
if (!resource)
resource = new StaticResource(new CFURL(schemeAndAuthority), NULL, YES, YES);
return resource;
}
StaticResource.resourceAtURL = function(/*CFURL|String*/ aURL, /*BOOL*/ resolveAsDirectoriesIfNecessary)
{
aURL = makeAbsoluteURL(aURL).absoluteURL();
var resource = rootResourceForAbsoluteURL(aURL),
components = aURL.pathComponents(),
index = 0,
count = components.length;
for (; index < count; ++index)
{
var name = components[index];
if (hasOwnProperty.call(resource._children, name))
resource = resource._children[name];
else if (resolveAsDirectoriesIfNecessary)
{
// We do this because on Windows the path may start with C: and be
// misinterpreted as a scheme.
if (name !== "/")
name = "./" + name;
resource = new StaticResource(new CFURL(name, resource.URL()), resource, YES, YES);
}
else
throw new Error("Static Resource at " + aURL + " is not resolved (\"" + name + "\")");
}
return resource;
}
StaticResource.prototype.resourceAtURL = function(/*CFURL|String*/ aURL, /*BOOL*/ resolveAsDirectoriesIfNecessary)
{
return StaticResource.resourceAtURL(new CFURL(aURL, this.URL()), resolveAsDirectoriesIfNecessary);
}
StaticResource.resolveResourceAtURL = function(/*CFURL|String*/ aURL, /*BOOL*/ isDirectory, /*Function*/ aCallback)
{
aURL = makeAbsoluteURL(aURL).absoluteURL();
resolveResourceComponents(rootResourceForAbsoluteURL(aURL), isDirectory, aURL.pathComponents(), 0, aCallback);
}
StaticResource.prototype.resolveResourceAtURL = function(/*CFURL|String*/ aURL, /*BOOL*/ isDirectory, /*Function*/ aCallback)
{
StaticResource.resolveResourceAtURL(new CFURL(aURL, this.URL()).absoluteURL(), isDirectory, aCallback);
}
function resolveResourceComponents(/*StaticResource*/ aResource, /*BOOL*/ isDirectory, /*Array*/ components, /*Integer*/ index, /*Function*/ aCallback)
{
var count = components.length;
for (; index < count; ++index)
{
var name = components[index],
child = hasOwnProperty.call(aResource._children, name) && aResource._children[name];
// If the child doesn't exist, create and resolve it.
if (!child)
{
child = new StaticResource(new CFURL(name, aResource.URL()), aResource, index + 1 < count || isDirectory , NO);
child.resolve();
}
// If this resource is still being resolved, just wait and rerun this same method when it's ready.
if (!child.isResolved())
return child.addEventListener("resolve", function()
{
// Continue resolving once this is done.
resolveResourceComponents(aResource, isDirectory, components, index, aCallback);
});
// If we've already determined that this file doesn't exist...
if (child.isNotFound())
return aCallback(null, new Error("File not found: " + components.join("/")));
// If we have more path components and this is not a directory...
if ((index + 1 < count) && child.isFile())
return aCallback(null, new Error("File is not a directory: " + components.join("/")));
aResource = child;
}
aCallback(aResource);
}
function resolveResourceAtURLSearchingIncludeURLs(/*CFURL*/ aURL, /*Number*/ anIndex, /*Function*/ aCallback)
{
var includeURLs = StaticResource.includeURLs(),
searchURL = new CFURL(aURL, includeURLs[anIndex]).absoluteURL();
StaticResource.resolveResourceAtURL(searchURL, NO, function(/*StaticResource*/ aStaticResource)
{
if (!aStaticResource)
{
if (anIndex + 1 < includeURLs.length)
resolveResourceAtURLSearchingIncludeURLs(aURL, anIndex + 1, aCallback);
else
aCallback(NULL);
return;
}
aCallback(aStaticResource);
});
}
StaticResource.resolveResourceAtURLSearchingIncludeURLs = function(/*CFURL*/ aURL, /*Function*/ aCallback)
{
resolveResourceAtURLSearchingIncludeURLs(aURL, 0, aCallback);
}
StaticResource.prototype.addEventListener = function(/*String*/ anEventName, /*Function*/ anEventListener)
{
this._eventDispatcher.addEventListener(anEventName, anEventListener);
}
StaticResource.prototype.removeEventListener = function(/*String*/ anEventName, /*Function*/ anEventListener)
{
this._eventDispatcher.removeEventListener(anEventName, anEventListener);
}
StaticResource.prototype.isNotFound = function()
{
return this._isNotFound;
}
StaticResource.prototype.isFile = function()
{
return !this._isDirectory;
}
StaticResource.prototype.isDirectory = function()
{
return this._isDirectory;
}
StaticResource.prototype.toString = function(/*BOOL*/ includeNotFounds)
{
if (this.isNotFound())
return "<file not found: " + this.name() + ">";
var string = this.name();
if (this.isDirectory())
{
var children = this._children;
for (var name in children)
if (children.hasOwnProperty(name))
{
var child = children[name];
if (includeNotFounds || !child.isNotFound())
string += "\n\t" + children[name].toString(includeNotFounds).split('\n').join("\n\t");
}
}
return string;
}
var includeURLs = NULL;
StaticResource.includeURLs = function()
{
if (includeURLs)
return includeURLs;
var includeURLs = [];
if (!global.OBJJ_INCLUDE_PATHS && !global.OBJJ_INCLUDE_URLS)
includeURLs = ["Frameworks", "Frameworks/Debug"];
else
includeURLs = (global.OBJJ_INCLUDE_PATHS || []).concat(global.OBJJ_INCLUDE_URLS || []);
var count = includeURLs.length;
while (count--)
includeURLs[count] = new CFURL(includeURLs[count]).asDirectoryPathURL();
return includeURLs;
}