-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileViewerController.j
151 lines (110 loc) · 2.91 KB
/
FileViewerController.j
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
@import <AppKit/CPOutlineView.j>
@implementation FileViewerController : CPObject
{
CPArray _data ;
CPString _projectName @accessors(property=projectName);
CPDictionary _fileInfo;
}
-(id) init
{
self = [super init];
if( self )
{
_data = Nil;
_projectName = @"Project";
_fileInfo = @{};
}
return self;
}
-(CPDictionary) _dataToDictionary:(JSObject)fileObj
{
var theFiles = [];
if(fileObj.files)
{
var count = fileObj.files.length,
i = 0;
for(; i < count; i++)
{
var d = [self _dataToDictionary:fileObj.files[i]];
[self setInfo:d forFile:fileObj.files[i].path];
theFiles.push(d);
}
}
return @{@"name" : fileObj.name, @"path" : fileObj.path, @"isDirectory" : fileObj.isDirectory, "root" : NO,
@"timestamp" : -1, @"contents" : @"", @"unsaved" : NO, @"isLoaded" : NO,
@"files" : theFiles};
}
-(void) setData:(CPArray)data
{
console.log(data)
_data = data;
_fileInfo = @{};
var count = _data.length,
i = 0;
var theFiles = [];
for(; i < count; i++)
{
var fileObj = _data[i];
var d = [self _dataToDictionary:fileObj];
[self setInfo:d
forFile:fileObj.path];
theFiles.push(d);
}
[self setInfo:@{@"name" : _projectName, @"isDirectory" : YES, @"root" : YES, @"unsaved" : NO,
@"files" : theFiles}
forFile:@"__root__"];
}
-(id)outlineView:(CPOutlineView)outlineView numberOfChildrenOfItem:(id)anItem
{
if(!_data)
return 0;
if(!anItem)
return 1;
if([anItem objectForKey:@"isDirectory"])
{
return [[anItem objectForKey:@"files"] count]
}
return 0;
}
-(id) outlineView:(CPOutlineView)outlineView child:(int)childIndex ofItem:(id)anItem
{
if(!anItem)
{
return [self infoForFile:@"__root__"];
}
if([anItem objectForKey:@"isDirectory"])
{
return [[anItem objectForKey:@"files"] objectAtIndex:childIndex];
}
return nil;
}
-(BOOL) outlineView:(CPOutlineView)outlineView isItemExpandable:(id)anItem
{
if(!anItem)
return YES;
return [anItem objectForKey:@"isDirectory"];
}
-(id) outlineView:(CPOutlineView)outlineView objectValueForTableColumn:(CPTableColumn)aTableColumn byItem:(id)anItem
{
var name = [anItem objectForKey:@"name"],
unsaved = [anItem objectForKey:@"unsaved"],
isDir = [anItem objectForKey:@"isDirectory"];
if(unsaved && !isDir)
{
return name + "*";
}
return name;
}
-(CPArray) allFiles
{
return [_fileInfo allKeys];
}
-(void) setInfo:(CPDictionary)info forFile:(CPString)filePath
{
[_fileInfo setObject:info forKey:filePath];
}
-(CPDictionary) infoForFile:(CPString)filePath
{
return [_fileInfo objectForKey:filePath];
}
@end