-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppController.j
More file actions
367 lines (320 loc) · 10.7 KB
/
AppController.j
File metadata and controls
367 lines (320 loc) · 10.7 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
@import <Foundation/CPObject.j>
@import "Constants.j"
@import "TagTreeController.j"
@import "ServiceController.j"
@import "SplashController.j"
@import "MetaController.j"
@implementation AppController : CPObject
{
CPWindow theWindow; //this "outlet" is connected automatically by the Cib
@outlet CPTableView tableView;
@outlet CPOutlineView outlineView;
@outlet CPTextField searchText;
@outlet CPSearchField tagFilter;
@outlet CPView rightPane;
@outlet CPPopover splashPopover;
@outlet CPButton searchButton;
@outlet CPButton resetButton;
@outlet CPScrollView contentView;
@outlet CPSegmentedControl tagMode;
@outlet CPTextField dateText;
@outlet CPScrollView scrollView;
CPPopover metaPopover;
CPTextField contentText;
TagTreeController treeController; // Controls the tag browser
CPArrayController arrayController; // Datasource for cable list
ServiceController searchService;
ServiceController detailService;
BOOL interactiveUser;
var itemTags;
var searchParams;
var timer; // Query-UI delay timer
}
//// Application delegate methods
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
interactiveUser = NO;
searchParams = {};
[self loadHashBang];
if (getCookie(c_name) != c_value) {
// Show splash popover
[splashPopover setContentViewController:[[SplashController alloc] initWithCibName:"Splash" bundle:nil]];
var andOrColumn= [outlineView tableColumnWithIdentifier:"and"];
[splashPopover showRelativeToRect:nil ofView:[outlineView headerView] preferredEdge:CPMaxXEdge];
// Expand first category node of tags
[outlineView expandItem:"Subject"];
}
}
- (void)awakeFromCib
{
// In this case, we want the window from Cib to become our full browser window
[theWindow setFullPlatformWindow:YES];
metaPopover = [[CPPopover alloc] init];
// Show splash popover
[metaPopover setContentViewController:[[MetaController alloc] initWithCibName:"Meta" bundle:nil]];
[splashPopover setContentViewController:[[SplashController alloc] initWithCibName:"Splash" bundle:nil]];
treeController = [[TagTreeController alloc] initWithOutlineView:outlineView];
[treeController setDelegate:self];
[rightPane setBackgroundColor:[CPColor colorWithHexString:"DDDDDD"]];
detailService = [[ServiceController alloc] initWithEndPoint:serverUrl];
[detailService setDelegate:self];
searchService = [[ServiceController alloc] initWithEndPoint:serverUrl];
[searchService setDelegate:self];
contentText = [CPTextField labelWithTitle:""];
[contentText setFrameOrigin:{x:10, y:10}]; // content margin
[contentText setSelectable:YES];
[contentView setDocumentView:contentText];
[scrollView setDelegate:self];
[dateText setObjectValue:DEFAULT_DATE];
}
//// Interface actions
- (@action)tagModeClick:(id)sender
{
switch([tagMode selectedSegment]) {
case 0: // Filter
default:
[treeController filter:""];
break;
case 1: // Active
[treeController filter:"active tags"];
break;
case 2:
[treeController setFilteredTags:itemTags];
break;
}
}
- (@action)metaClick:(id)sender
{
if ([metaPopover shown]) {
[metaPopover close];
}
else {
[metaPopover showRelativeToRect:nil ofView:sender preferredEdge:CPMinYEdge];
}
}
- (@action)helpClick:(id)sender {
if ([splashPopover shown]) {
[splashPopover close];
}
else {
[splashPopover showRelativeToRect:nil ofView:sender preferredEdge:CPMinYEdge];
}
}
- (void)dismissPopover
{
[splashPopover close];
}
- (@action)resetTags:(id)sender
{
[tagMode setSelectedSegment:0];
[searchText setStringValue:""];
[tagFilter setStringValue:""];
[treeController reset];
[self awakeFromCib];
[self setHashBang];
}
- (@action)willFilter:(id)sender
{
[tagMode setSelectedSegment:0]; // Switch to Filter mode
[self tagModeClick:nil];
[treeController filter:[tagFilter stringValue]];
}
- (@action)search:(id)interactive
{
interactiveUser = NO;
if (interactive) interactiveUser = YES;
// Validate date time
var date = [self validateDate:[dateText objectValue]];
if (!date)
{
return;
}
// Search button pressed
searchParams.text = [searchText stringValue].toString();
searchParams.tags = [treeController getCheckedTags];
searchParams.or = [treeController getOrTags];
searchParams.date = date;
searchParams.page = undefined;
// Serialize the searchParms in to the hash bang fragment
if (interactiveUser === YES) [self setHashBang];
// Generate JSON request to populate search results dataSource
[searchService post:queryPath withData:searchParams];
}
- (void)validateDate:(CPString)strDate
{
var date = null;
try
{
date = [[CPDate alloc] initWithString:strDate + " 12:00:00 -0700"];
}
catch (e)
{
[[CPAlert alertWithError:"Beginning near: must be YYYY-MM-DD format."] beginSheetModalForWindow:theWindow];
}
return date;
}
- (void)setHashBang
{
var hashBang = "";
if (searchParams) {
var params = [];
var text = escape(searchParams.text);
var ref = escape(searchParams.refid);
var tags = escape(searchParams.tags.join(','));
var ors = escape(searchParams.or.join(','));
if (text.length > 0) params.push("q=" + text);
if (tags.length > 0) params.push("t=" + tags);
if (ors.length > 0) params.push("o=" + ors);
if (searchParams.refid && ref.length > 0) params.push("i=" + ref);
if (searchParams.date) params.push("d=" + [searchParams.date description].split(" ")[0]);
hashBang = "#!/" + params.join('/');
}
window.location.hash = hashBang;
}
- (void)loadHashBang
{
var hashBang = window.location.hash.split('/');
if (hashBang.length > 0)
// Load whatever search is passed in the url hash fragment
var params = {};
for (var i=0; i<hashBang.length; i++) {
var kvp = hashBang[i].split('=');
if (kvp.length === 2) {
params[kvp[0]] = unescape(kvp[1]);
}
}
if (params['q'] || params['t'] || params['o']) {
searchParams = {
text: params['q'] ? params['q'] : "",
tags: params['t'] ? params['t'].split(',') : [],
or: params['o'] ? params['o'].split(',') : [],
refid: params['i'] ? params['i'] : "",
date: params['d'] ? [self validateDate:params['d']] : undefined,
}
[treeController loadAllTags];
[dateText setObjectValue:params['d']];
[searchText setStringValue:searchParams.text];
[treeController setCheckedTags:searchParams.tags];
[treeController setOrTags:searchParams.or];
[treeController filter:[tagFilter stringValue]];
[tagMode setSelectedSegment:1];
[self search:YES];
}
}
}
//// TagTree View methods
- (void)checkBoxTree:(id)tree didItemCheckChange:(CPObject)item toValue:(id)state
{
// Checked tags changed, show live search - delay incase series of UI events
if (timer) clearInterval(timer);
timer = setInterval(function() {
clearInterval(timer);
[self search:nil];
}, 1000);
}
//// Table View methods
- (void)tableViewSelectionDidChange:(CPNotification)aNotification
{
interactiveUser = YES;
// Show selected cable in the content viewer
if ([arrayController selectedObjects]) {
var selectedItem = [arrayController selectedObjects][0];
searchParams.refid = [selectedItem objectForKey:"refid"];
if (searchParams.refid) [detailService post:itemPath withData:{refid: searchParams.refid}];
}
}
- (void)setSearchResults:(CPArray)dataSource
{
var existingItems = [];
// Append items if paging
if (arrayController && searchParams.page) existingItems = [arrayController contentArray];
arrayController = [[CPArrayController alloc] init];
[arrayController addObjects:existingItems];
[tableView setDelegate:nil]; // Don't listen to any event during data binding (i.e. selected row change)
if (dataSource && dataSource.length > 0) {
// Show count in header name
var count = searchParams.page ? (searchParams.page + 1) * dataSource.length : dataSource.length;
[[[tableView tableColumnWithIdentifier:"Subject"] headerView] setStringValue:"Subjects (" + count + ")"];
for (var i=0; i<dataSource.length; i++)
{
[arrayController addObject:[CPDictionary dictionaryWithJSObject:dataSource[i]]];
}
}
else {
[[[tableView tableColumnWithIdentifier:"Subject"] headerView] setStringValue:"Subjects (0)"];
if (interactiveUser == YES) {
[[CPAlert alertWithError:"No search results found."] beginSheetModalForWindow:theWindow];
}
}
// Update data binding, even if empty
[[tableView tableColumnWithIdentifier:"Refid"] bind:@"value" toObject:arrayController withKeyPath:@"arrangedObjects.refid" options:nil];
[[tableView tableColumnWithIdentifier:"Subject"] bind:@"value" toObject:arrayController withKeyPath:@"arrangedObjects.subject.capitalizedString" options:nil];
[[tableView tableColumnWithIdentifier:"Date"] bind:@"value" toObject:arrayController withKeyPath:@"arrangedObjects.date" options:nil];
[[tableView tableColumnWithIdentifier:"Classification"] bind:@"value" toObject:arrayController withKeyPath:@"arrangedObjects.classification" options:nil];
[tableView sizeLastColumnToFit];
// Select none, or parameterized index
var selectedRow = -1;
if (searchParams.refid) {
for (var i=0; i<dataSource.length; i++) {
if (dataSource[i].refid == searchParams.refid) {
selectedRow = i;
break;
}
}
}
[tableView selectRowIndexes:[CPIndexSet indexSetWithIndex:selectedRow] byExtendingSelection:NO];
[tableView setDelegate:self];
if (interactiveUser) [self tableViewSelectionDidChange:nil];
}
- (void)setContent:(id)item
{
// Show selected item content
[contentText setStringValue:item.content + "\n\n"]
//var attr = [[CPDictionary dictionary] setObject:[CPColor purpleColor] forKey:"CPBackgroundColorAttributeName"];
//[contentText setObjectValue:[[CPAttributedString alloc] initWithString:item.content attributes:attr]];
[contentText sizeToFit];
[contentView scrollToBeginningOfDocument:self];
[contentView needsDisplay];
// Set selected item tags
itemTags = item.tags;
if (interactiveUser) {
[tagMode setSelectedSegment:2]; // Set selected tag mode
[self setHashBang];
}
[self tagModeClick:nil];
}
- (void)scrollViewDidScroll:(CPScrollView)s
{
// Check if at page bottom, load more data
if([[scrollView verticalScroller] floatValue] === 1)
{
// Load the next page of data
if (searchParams.page) searchParams.page++;
else searchParams.page = 1;
[searchService post:queryPath withData:searchParams];
}
}
//// Service delegate methods
- (void)serviceDelegate:(id)del gotJSONObject:(id)obj
{
if (del === detailService) {
[self setContent:obj]
}
else if (del === searchService) {
[self setSearchResults:obj];
}
[searchButton setEnabled:YES];
interactiveUser = NO;
}
- (void)serviceDelegate:(id)del failedWithError:(id)error
{
// Handle errors from the web service
//[[CPAlert alertWithError:"There was an error communicating with the server."] beginSheetModalForWindow:theWindow];
[searchButton setEnabled:YES];
}
- (void)serviceDelegateDidBeginConnection:(id)svc
{
// Indicate UI that actively searching
[searchButton setEnabled:NO];
}
@end