forked from ArchipelProject/TNKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTNTableViewDataSource.j
463 lines (366 loc) · 12 KB
/
TNTableViewDataSource.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* TNTableViewDataSource.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPSearchField.j>
@import <AppKit/CPTableView.j>
/*! @ingroup tnkit
Simple table view datasource with filtering support
Exemple of uses
var colA = [[CPTableColumn alloc] initWithIdentifier:@"keypath1"],
colB = [[CPTableColumn alloc] initWithIdentifier:@"keypath2"];
[aTableView addTableColumn:colA];
[aTableView addTableColumn:colB];
...
datasource = [[TNTableViewDataSource alloc] init];
[datasource setTable:aTableView];
[datasource setSearchableKeyPaths:[@"keypath1"]]; // only key path1 is searchable
[aTableView setDataSource:datasource];
...
// bind a CPSearchField to the datasource
[aSearchField setTarget:datasource];
[aSearchField setAction:@selector(filterObjects:)];
...
[datasource addObject:anObject];
[aTableView reloadData];
Not the anObject must have keypath1, keypath2 if you want to be able to search and display
*/
@implementation TNTableViewDataSource: CPObject
{
CPArray _content @accessors(property=content);
CPArray _searchableKeyPaths @accessors(property=searchableKeyPaths);
CPTableView _table @accessors(property=table);
CPPredicate _displayFilter @accessors(property=displayFilter);
id _delegate @accessors(property=delegate);
CPArray _filteredContent;
CPSearchField _searchField;
CPPredicate _filter;
BOOL _needsFilter;
}
#pragma mark -
#pragma mark Initialization
- (id)init
{
if (self = [super init])
{
_content = [CPArray array];
_filteredContent = [CPArray array];
_searchableKeyPaths = [CPArray array];
_filter = nil;
_needsFilter = NO;
}
return self;
}
#pragma mark -
#pragma mark Filtering
/*! this action should be bound to a CPSearchField
it will filter the content of the datasource according to the sender value
@param sender the sender of the action
*/
- (IBAction)filterObjects:(id)sender
{
if (!_searchField)
_searchField = sender;
[self setFilterString:[sender stringValue]];
}
- (void)setFilterPredicate:(CPPredicate)aPredicate
{
_filter = aPredicate;
[self _performFiltering];
}
/*! Set the filter. The filter must be a string
@param aString CPString representing the filter
*/
- (void)setFilterString:(CPString)aString
{
if (aString && [aString length])
{
try {_filter = [CPPredicate predicateWithFormat:aString];}catch(e){};
// if predicate creation failed, build a predicate according to searchable ketpaths
if (!_filter)
{
var tempPredicateString = @"";
for (var i = 0, c = [_searchableKeyPaths count]; i < c; i++)
{
tempPredicateString += _searchableKeyPaths[i] + " contains[c] '" + aString + "' ";
if (i + 1 < [_searchableKeyPaths count])
tempPredicateString += " OR ";
}
if ([tempPredicateString length])
_filter = [CPPredicate predicateWithFormat:tempPredicateString];
}
}
else
_filter = nil;
[self _performFiltering];
}
/*! @ignore
Return a filtered content using a filter predicate
@param aPredicate the predicate
@return CPArray with filtered content
*/
- (void)_filterWithPredicate:(CPPredicate)aPredicate
{
if (_displayFilter)
return [[_content filteredArrayUsingPredicate:_filter] filteredArrayUsingPredicate:_displayFilter];
else
return [_content filteredArrayUsingPredicate:_filter];
}
- (void)_performFiltering
{
_filteredContent = [CPArray array];
if (!_filter)
{
_filteredContent = _displayFilter ? [[_content copy] filteredArrayUsingPredicate:_displayFilter] : [_content copy];
[_table reloadData];
return;
}
_filteredContent = [self _filterWithPredicate:_filter];
[_table reloadData];
}
#pragma mark -
#pragma mark Content management
/*! set the given array of object as the content of the datasource
@param aContent CPArray containing the datas of the datasource
*/
- (void)setContent:(CPArray)aContent
{
_content = aContent;
_filteredContent = _displayFilter ? [[_content copy] filteredArrayUsingPredicate:_displayFilter] : [_content copy];
_needsFilter = YES;
}
/*! add an object to the datasource
@param anObject object to add
*/
- (void)addObject:(id)anObject
{
[_content addObject:anObject];
if (!_displayFilter || [_displayFilter evaluateWithObject:anObject])
[_filteredContent addObject:anObject];
_needsFilter = YES;
}
/*! add some objects to the datasource
@param someObjects array of objects to add
*/
- (void)addObjectsFromArray:(CPArray)someObjects
{
[_content addObjectsFromArray:someObjects];
if (!_displayFilter)
{
for (var i = [someObjects count] - 1; i >= 0; i--)
{
var obj = someObjects[i];
if ([_displayFilter evaluateWithObject:obj])
[_filteredContent addObject:obj];
}
}
_needsFilter = YES;
}
/*! Chek if contents contains given object
@param anObject the object to search
@return YES if anObject is in the contents
*/
- (void)containsObject:(id)anObject
{
return [_filteredContent containsObject:anObject];
}
/*! insert an object at a given index in the datasource
@param anObject object to add
@param anObject int representing the position
*/
- (void)insertObject:(id)anObject atIndex:(int)anIndex
{
[_content insertObject:anObject atIndex:anIndex];
if (!_displayFilter || [_displayFilter evaluateWithObject:anObject])
[_filteredContent insertObject:anObject atIndex:anIndex];
_needsFilter = YES;
}
/*! return the object at given index
@param anObject int representing the position
@return the object
*/
- (void)objectAtIndex:(int)index
{
return _filteredContent[index];
}
/*! return the objects at given indexes contained in a CPIndexSet
@param anObject CPIndexSet representing the positions
@return the objects
*/
- (CPArray)objectsAtIndexes:(CPIndexSet)aSet
{
return [_filteredContent objectsAtIndexes:aSet];
}
/*! removes the object at given index
@param anObject int representing the position
*/
- (void)removeObjectAtIndex:(int)index
{
var object = _filteredContent[index];
[_filteredContent removeObjectAtIndex:index];
[_content removeObject:object];
_needsFilter = YES;
}
/*! removes the object at given indexes contained in a CPIndexSet
@param anObject CPIndexSet representing the positions
*/
- (void)removeObjectsAtIndexes:(CPIndexSet)aSet
{
try
{
var objects = [_filteredContent objectsAtIndexes:aSet];
[_filteredContent removeObjectsAtIndexes:aSet];
[_content removeObjectsInArray:objects];
_needsFilter = YES;
}
catch(e)
{
CPLog.error(e);
}
}
/*! remove the given object from the array
@param anObject the object to remove
*/
- (void)removeObject:(id)anObject
{
[_content removeObject:anObject];
[_filteredContent removeObject:anObject];
_needsFilter = YES;
}
/*! remove all objects
*/
- (void)removeAllObjects
{
[_content removeAllObjects];
[_filteredContent removeAllObjects];
_needsFilter = YES;
}
/*! remove last object
*/
- (void)removeLastObject
{
[_content removeLastObject];
[_filteredContent removeLastObject];
_needsFilter = YES;
}
/*! remove first object
*/
- (void)removeFirstObject
{
[_content removeFirstObject];
[_filteredContent removeFirstObject];
_needsFilter = YES;
}
/*! return the index of the given object
@param anObject the object
@return the index of the given object
*/
- (int)indexOfObject:(id)anObject
{
return [_filteredContent indexOfObject:anObject];
}
/*! returns the number of object in the datasource
If a filter is applied, it will return the number of objects
matching the filyter , not the total
@return the number of object
*/
- (int)count
{
return [_filteredContent count];
}
/*! Removes the objects from the array
*/
- (void)removeObjectsInArray:(CPArray)someObjects
{
[_content removeObjectsInArray:someObjects];
[_filteredContent removeObjectsInArray:someObjects];
_needsFilter = YES;
}
#pragma mark -
#pragma mark Datasource implementation
- (CPNumber)numberOfRowsInTableView:(CPTableView)aTable
{
return [_filteredContent count];
}
- (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPNumber)aCol row:(CPNumber)aRow
{
if (_needsFilter)
{
[self filterObjects:_searchField];
_needsFilter = NO;
}
if (aRow >= [_filteredContent count])
return nil;
if (_delegate
&& [_delegate respondsToSelector:@selector(dataSource:willReachEndOfData:)]
&& [aTableView numberOfRows] == aRow + 1)
{
[_delegate dataSource:self willReachEndOfData:aRow];
}
return [_filteredContent[aRow] valueForKeyPath:[aCol identifier]];
}
- (void)tableView:(CPTableView)aTableView sortDescriptorsDidChange:(CPArray)oldDescriptors
{
var indexes = [aTableView selectedRowIndexes],
selectedObjects = [_filteredContent objectsAtIndexes:indexes],
indexesToSelect = [[CPIndexSet alloc] init];
[_filteredContent sortUsingDescriptors:[aTableView sortDescriptors]];
[_content sortUsingDescriptors:[aTableView sortDescriptors]];
[_table reloadData];
for (var i = 0, c = [selectedObjects count]; i < c; i++)
{
var object = selectedObjects[i];
[indexesToSelect addIndex:[_filteredContent indexOfObject:object]];
}
[_table selectRowIndexes:indexesToSelect byExtendingSelection:NO];
}
- (void)tableView:(CPTableView)aTableView setObjectValue:(id)aValue forTableColumn:(CPTableColumn)aCol row:(int)aRow
{
if (aRow >= [_filteredContent count])
return;
var identifier = [aCol identifier];
[_filteredContent[aRow] setValue:aValue forKeyPath:identifier];
}
#pragma mark -
#pragma mark Drag and drop
/*! DataSource delegate
*/
- (BOOL)tableView:(CPTableView)aTableView writeRowsWithIndexes:(CPIndexSet)rowIndexes toPasteboard:(CPPasteboard)thePasteBoard
{
if (_delegate && [_delegate respondsToSelector:@selector(dataSource:writeRowsWithIndexes:toPasteboard:)])
return [_delegate dataSource:self writeRowsWithIndexes:rowIndexes toPasteboard:thePasteBoard];
return NO;
}
/*! DataSource delegate
*/
- (CPDragOperation)tableView:(CPTableView)aTableView validateDrop:(CPDraggingInfo)info proposedRow:(int)row proposedDropOperation:(CPTableViewDropOperation)operation
{
if (_delegate && [_delegate respondsToSelector:@selector(dataSource:validateDrop:proposedRow:proposedDropOperation:)])
return [_delegate dataSource:self validateDrop:info proposedRow:row proposedDropOperation:operation];
return CPDragOperationNone;
}
/*! DataSource delegate
*/
- (BOOL)tableView:(CPTableView)aTableView acceptDrop:(CPDraggingInfo)info row:(int)row dropOperation:(CPTableViewDropOperation)operation
{
if (_delegate && [_delegate respondsToSelector:@selector(dataSource:acceptDrop:row:dropOperation:)])
return [_delegate dataSource:self acceptDrop:info row:row dropOperation:operation];
return NO;
}
@end