-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathISIssuesController.j
360 lines (281 loc) · 9.42 KB
/
ISIssuesController.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
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
/*
* AppController.j
* GithubIssues
*
* Created by Randy Luecke on April 14, 2011.
* Copyright 2011, RCLConcepts, LLC All rights reserved.
*/
/*!
This class is used as the controller between the model (an array of issues)
and the view (the list of issues).
*/
@implementation ISIssuesController : CPArrayController
{
ISRepository activeRepository @accessors;
@outlet CPView containerView;
@outlet ISSortBar sortBar;
CPScrollView scrollView;
CPTableView issuesList;
CPString visisbleIssuesKey;
@outlet ISIssueDataView dataviewproto;
}
- (id)init
{
self = [super init];
visisbleIssuesKey = "open";
return self;
}
- (void)_showIssues
{
// For faster startup times we lazily load
// the issues view
if (!scrollView)
{
var containerSize = [containerView bounds],
offsetHeight = [sortBar bounds].size.height;
scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0, offsetHeight, containerSize.size.width, containerSize.size.height - offsetHeight)];
[scrollView setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
[containerView addSubview:scrollView];
issuesList = [[CPTableView alloc] initWithFrame:CGRectMakeZero()];
[issuesList setDataSource:self];
[issuesList setDelegate:self];
var col = [[CPTableColumn alloc] initWithIdentifier:"issues"];
[col setDataView:dataviewproto];
[issuesList setColumnAutoresizingStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
[issuesList setHeaderView:nil];
[issuesList setCornerView:nil];
[issuesList setRowHeight:60];
[issuesList setAlternatingRowBackgroundColors:
[
[CPColor colorWithRed:250/255 green:250/255 blue:250/255 alpha:1],
[CPColor colorWithRed:245/255 green:247/255 blue:247/255 alpha:1]
]
];
[issuesList setUsesAlternatingRowBackgroundColors:YES];
[issuesList setGridColor:[CPColor colorWithRed:218/255 green:225/255 blue:230/255 alpha:1]];
[issuesList setGridStyleMask:CPTableViewSolidHorizontalGridLineMask];
[issuesList setSelectionHighlightColor:[CPColor colorWithRed:216/255 green:230/255 blue:240/255 alpha:1]];
[scrollView setDocumentView:issuesList];
[scrollView setHasHorizontalScroller:NO];
[scrollView setAutohidesScrollers:YES];
[issuesList addTableColumn:col];
[issuesList sizeLastColumnToFit];
[col bind:CPValueBinding toObject:self withKeyPath:"arrangedObjects" options:nil];
// [self bind: toObject: withKeyPath: options:]
}
if (!activeRepository)
{
// Make sure the table knows we have 0 rows now.
[self setContent:[]];
return;
}
var issues = [activeRepository valueForKey:visisbleIssuesKey];
if (issues)
{
// Reapply the current search to the new issues.
[self searchDidChange:nil];
// FIX ME: find the currently selected item
// Then after the reload call reselect that item if it still exists.
//[issuesList reloadData];
[self setContent:issues];
}
else
[[ISGithubAPIController sharedController] loadIssuesForRepository:activeRepository state:visisbleIssuesKey callback:nil];
}
- (@action)searchDidChange:(id)sender
{
// FIX ME: filter bar and better predicate stuff
if (![sender stringValue])
var filter = nil;
else
var filter = [CPPredicate predicateWithFormat:"title like[cd] %@", [sender stringValue]];
[self setFilterPredicate:filter];
}
// FIX ME: maybe we should just call setSortDescriptors: directly
- (void)sortDescriptorsDidChange:(CPArray)newSortDescriptors
{
[self setSortDescriptors:newSortDescriptors];
}
/*!
Sets the active repository,
This method registers observers for the visible issue key.
*/
- (void)setActiveRepository:(ISRepository)aRepo
{
// Remove the previous observer
[activeRepository removeObserver:self forKeyPath:visisbleIssuesKey];
activeRepository = aRepo;
[self setContent:[activeRepository valueForKey:visisbleIssuesKey]];
//Add a new observer
[activeRepository addObserver:self forKeyPath:visisbleIssuesKey options:nil context:nil];
[self _showIssues];
}
/*!
When the visisble issues key (open/closed) changes
we redisplay new issues.
*/
- (void)observeValueForKeyPath:(id)path ofObject:(id)obj change:(id)change context:(id)context
{
if (obj !== activeRepository)
return;
[self _showIssues];
}
/*!
Sent by the assignee button
*/
- (@action)_assignee:(id)sender
{
[CPMenu popUpContextMenu:[self _assigneesMenu] withEvent:[CPApp currentEvent] forView:sender];
}
/*!
Returns the menu for assignee.
*/
- (CPMenu)_assigneesMenu
{
var menu = [[CPMenu alloc] init];
var assignees = [activeRepository collaboratorNames],
count = [assignees count];
for (var i = 0; i < count; i++)
{
var assignee = assignees[i],
item = [[CPMenuItem alloc] initWithTitle:assignee action:@selector(_toggleTag:) keyEquivalent:nil];
if (assignee.isUsed)
[item setState:CPOnState];
[item setTarget:self];
[item setTag:assignee];
[menu addItem:item];
}
return menu;
}
/*!
The action of the milestone button.
This method creates a popup menu for the button.
*/
- (@action)_milestones:(id)aSender
{
[CPMenu popUpContextMenu:[self _milestonesMenu] withEvent:[CPApp currentEvent] forView:aSender];
}
/*!
Returns a CPMenu for the label setting thingy for the selected item
*/
- (CPMenu)_milestonesMenu
{
var menu = [[CPMenu alloc] init],
newItem = [[CPMenuItem alloc] initWithTitle:@"New Milestone" action:@selector(newMilestone:) keyEquivalent:nil];
[newItem setTarget:self];
[menu addItem:newItem];
var milestones = [self _milestonesForSelectedIssues],
count = [milestones count];
if (count)
[menu addItem:[CPMenuItem separatorItem]];
for (var i = 0; i < count; i++)
{
var milestone = milestones[i],
item = [[CPMenuItem alloc] initWithTitle:[milestone objectForKey:"title"] action:@selector(_toggleLabel:) keyEquivalent:nil];
if ([milestone objectForKey:"isUsed"])
[item setState:CPOnState];
[item setTarget:self];
[item setTag:milestone];
[menu addItem:item];
}
return menu;
}
/*!
Returns an array of the milestones.
This has an extra property isUsed
*/
- (CPArray)_milestonesForSelectedIssues
{
// FIX ME: actually make use of isUsed.
return [activeRepository milestones];
}
- (void)newMilestone:(sender)aSender
{
alert("do this");
}
/*!
Sent by the assignee button
*/
- (@action)_label:(id)sender
{
[CPMenu popUpContextMenu:[self _labelsMenu] withEvent:[CPApp currentEvent] forView:sender];
}
/*!
Returns the menu for assignee.
*/
- (CPMenu)_labelsMenu
{
var menu = [[CPMenu alloc] init],
newItem = [[CPMenuItem alloc] initWithTitle:@"New Label" action:@selector(newLabel:) keyEquivalent:nil];
[newItem setTarget:self];
[menu addItem:newItem];
var labels = [self _labelsForSelectedIssue],
count = [labels count];
if (count)
[menu addItem:[CPMenuItem separatorItem]];
for (var i = 0; i < count; i++)
{
var label = labels[i],
item = [[CPMenuItem alloc] initWithTitle:[label name] action:@selector(_toggleTag:) keyEquivalent:nil];
if ([label isUsed])
[item setState:CPOnState];
[item setTarget:self];
[item setTag:label];
[menu addItem:item];
}
return menu;
}
/*!
Returns an array of ISLabels for the selected item.
This is specific to a single itme becuase it has an isUsed
key for each item.
*/
- (CPArray)_labelsForSelectedIssue
{
var allLabels = [activeRepository labels],
newLabels = [CPArray arrayWithDeepCopyOfArray:allLabels],
usedLabels = [[self selectedObjects][0] labels],
i = 0,
c = [newLabels count];
for (; i < c; i++)
{
if ([usedLabels containsObject:allLabels[i]])
[newLabels[i] setNumberOfIssues:1];
else
[newLabels[i] setNumberOfIssues:0]; // because a deep copy also copies the number, and since we're only interested in a single issue we don't need it.
}
return newLabels;
}
/*!
This method actually creates a NEW label
*/
- (@action)newLabel:(id)aSender
{
[[[ISNewLabelWindowController alloc] initWithWindowCibName:"NewLabelWindow"] showWindow:self];
}
/*!
This method toggles a particular label
*/
- (@action)toggleLabel:(id)aSender
{
// FIX ME: this probably doesnt work
var label = [aSender tag],
selector = [label objectForKey:"isUsed"] ? @selector(unsetLabelForSelectedIssue:) : @selector(setLabelForSelectedIssue:);
[self performSelector:selector withObject:label];
}
- (void)unsetLabelForSelectedIssue:(CPDictionary)aLabel
{
}
- (void)setLabelForSelectedIssue:(CPDictionary)aLabel
{
}
- (void)visisbleIssuesSelectionDidChange:(BOOL)aOpenIssuesAreSelected
{
// Remove the previous observer
[activeRepository removeObserver:self forKeyPath:visisbleIssuesKey];
// Change the key and add a new observer
visisbleIssuesKey = aOpenIssuesAreSelected ? "open" : "closed";
[activeRepository addObserver:self forKeyPath:visisbleIssuesKey options:nil context:nil];
[self _showIssues];
}
@end