-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibManager.j
More file actions
303 lines (270 loc) · 8.05 KB
/
LibManager.j
File metadata and controls
303 lines (270 loc) · 8.05 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
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
// (c) 2010-2011 by Anton Korenyushkin
@import "BaseEntityManager.j"
@import "FileHandling.j"
@import "UseLibPanelController.j"
var libCode = {};
@implementation Lib (LibManager)
- (CPString)imageName // public
{
return "Lib";
}
- (BOOL)isExpandable // public
{
return !self.isBad;
}
- (unsigned)numberOfChildren // public
{
if (libCode.hasOwnProperty(identifier))
return [libCode[identifier] numberOfChildren];
if (authorName && appName && version) {
self.isLoading = YES;
var request = [[HTTPRequest alloc] initWithMethod:"GET" URL:[self URL] target:self action:@selector(didLoadTree:)];
[request setErrorAction:@selector(didFailToLoadTree)];
[request send];
} else {
self.isBad = YES;
[[[Alert alloc] initWithMessage:"The description of the \"" + name + "\" library is incorrect."
comment:"Please fix the \"manifest.json\" file."]
showPanel];
[manager notify];
}
return 0;
}
- (void)didLoadTree:(JSObject)tree // private
{
libCode[identifier] = [[Folder alloc] initWithTree:tree];
delete self.isLoading;
[manager notify];
}
- (void)didFailToLoadTree // private
{
delete self.isLoading;
self.isBad = YES;
[manager notify];
}
- (Entry)childAtIndex:(unsigned)index // public
{
return [libCode[identifier] childAtIndex:index];
}
- (CPString)URL // public
{
return "/libs/" + authorName + "/" + appName + "/" + version + "/";
}
@end
@implementation LibManager : BaseEntityManager
{
CodeManager codeManager;
UseLibPanelController useLibPanelController;
File manifestFile;
JSObject manifest;
BOOL isProcessing;
}
- (id)initWithCodeManager:(CodeManager)aCodeManager // public
{
if (self = [super initWithApp:aCodeManager.app keyName:"libs"]) {
isLoading = YES;
codeManager = aCodeManager;
useLibPanelController = [[UseLibPanelController alloc] initWithTarget:self action:@selector(useLib:)];
[app addObserver:self forKeyPath:"code"];
}
return self;
}
- (CPString)name // public
{
return "Libraries";
}
- (CPString)imageName // public
{
return "Libs";
}
- (unsigned)numberOfChildren // public
{
return app.libs ? app.libs.length : 0;
}
- (Lib)childAtIndex:(unsigned)index // public
{
return app.libs[index];
}
- (BOOL)isReady // public
{
return !!app.libs;
}
- (void)setLibs:(CPArray)libs // private
{
isLoading = NO;
if (app.libs)
app.libs.forEach(function (lib) { [lib noteDeleted]; });
[app setLibs:libs];
}
- (BOOL)manifestIsCorrect // private
{
return manifest && typeof(manifest) == "object" && manifest.libs && typeof(manifest.libs) == "object";
}
- (void)saveManifest // private
{
[app saveFile:manifestFile content:JSON.stringify(manifest, null, " ")];
}
- (void)observeValueForKeyPath:(CPString)keyPath ofObject:(id)object change:(CPDictionary)change context:(id)context // private
{
switch (keyPath) {
case "code":
var entry = [app.code childWithName:"manifest.json"];
if (entry === manifestFile && app.libs)
return;
[manifestFile removeObserver:self forKeyPath:"content"];
if ([entry isKindOfClass:File]) {
manifestFile = entry;
[manifestFile addObserver:self forKeyPath:"content"];
if (manifestFile.content === nil) {
// FIXME: Race! A request getting the content is sent before receiving a response to a request changing the code.
[app loadFile:manifestFile];
if (!isLoading) {
isLoading = YES;
[self notify];
}
} else {
[self readManifest];
}
} else {
manifestFile = nil;
[self setLibs:[]];
}
break;
case "content":
[self readManifest];
break;
}
}
- (void)readManifest // private
{
try {
manifest = JSON.parse(manifestFile.content);
} catch (error) {
[self setLibs:[]];
return;
}
if (![self manifestIsCorrect]) {
[self setLibs:[]];
return;
}
var oldLibs = app.libs || [];
var newLibs = [];
for (var name in manifest.libs) {
var identifier = manifest.libs[name] + "";
var lib = nil;
for (var i = 0; i < oldLibs.length; ++i) {
if (oldLibs[i].identifier == identifier) {
lib = oldLibs[i];
oldLibs.splice(i, 1);
[lib setName:name];
delete lib.isLoading;
break;
}
}
if (!lib) {
lib = [[Lib alloc] initWithName:name identifier:identifier];
lib.manager = self;
}
newLibs.push(lib);
}
[self setLibs:newLibs];
}
- (void)showUseLib // public
{
[useLibPanelController showWindow:nil];
}
- (void)useLib:(Lib)lib // public
{
if (isProcessing)
return;
if (manifestFile) {
if (![self manifestIsCorrect]) {
[useLibPanelController close];
[[[Alert alloc] initWithMessage:"The file \"manifest.json\" is incorrect."
comment:"Please fix the manifest file."]
showPanel];
return;
}
if (manifest.libs.hasOwnProperty(lib.name)) {
[[[Alert alloc] initWithMessage:"The alias \"" + lib.name + "\" is already taken."
comment:"Please choose another alias."
target:useLibPanelController
action:@selector(didEndErrorSheet:)]
showSheetForWindow:[useLibPanelController window]];
return;
}
}
if (libCode.hasOwnProperty(lib.idenitifier)) {
[self doUseLib:lib];
return;
}
isProcessing = YES;
[[useLibPanelController window] setTitle:"Processing..."];
var request = [[HTTPRequest alloc] initWithMethod:"GET" URL:[lib URL] target:self action:@selector(didGetTree:ofLib:)];
[request setFinishAction:@selector(didLibTreeRequestFinished)];
[request setErrorMessageAction:@selector(didEndLibTreeRequestErrorSheet:)];
[request setWindow:[useLibPanelController window]];
[request setContext:lib];
[request send];
}
- (void)doUseLib:(Lib)lib // private
{
isLoading = YES;
[self notify];
[useLibPanelController close];
if (!manifestFile) {
manifest = {libs: {}};
manifestFile = [[File alloc] initWithName:"manifest.json" parentFolder:app.code];
[manifestFile addObserver:self forKeyPath:"content"];
[codeManager insertItem:manifestFile];
[codeManager notify];
}
manifest.libs[lib.name] = lib.identifier;
[self saveManifest];
}
- (void)didLibTreeRequestFinished // private
{
isProcessing = NO;
[[useLibPanelController window] setTitle:"Use Library"];
}
- (void)didGetTree:(JSObject)tree ofLib:(Lib)lib // private
{
libCode[lib.identifier] = [[Folder alloc] initWithTree:tree];
[self doUseLib:lib];
}
- (void)didEndLibTreeRequestErrorSheet:(Alert)sender // private
{
[useLibPanelController didEndErrorSheet:sender];
}
- (void)renameItem:(Lib)lib to:(CPString)name // protected
{
lib.manager = self;
if (manifest.libs.hasOwnProperty(name)) {
[[[Alert alloc] initWithMessage:"The alias \"" + name + "\" is already taken."
comment:"Please choose another alias."]
showPanel];
return;
}
manifest.libs[name] = lib.identifier;
delete manifest.libs[lib.name];
[self saveManifest];
lib.isLoading = YES;
[lib setName:name];
[app.libs removeObject:lib];
app.libs.push(lib);
}
- (CPString)descriptionOfItems:(CPArray)libs // public
{
return libs.length == 1 ? "library \"" + libs[0].name + "\"" : "selected " + libs.length + " libraries";
}
- (void)deleteItems:(CPArray)libs // public
{
libs.forEach(
function (lib) {
lib.isLoading = YES;
delete manifest.libs[lib.name];
});
[self saveManifest];
[self notify];
}
@end