-
Notifications
You must be signed in to change notification settings - Fork 13
/
TNTableViewLazyDataSource.j
266 lines (224 loc) · 7.21 KB
/
TNTableViewLazyDataSource.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
/*
* TNTableViewLazyDataSource.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPSearchField.j>
@import <AppKit/CPTableView.j>
/*! @ingroup tnkit
Simple table view datasource managing lazy loading with filtering support
Must have a delegate reponsible for fetching data, and the delegate must implement
<pre>- (void)tableViewDataSourceNeedsLoading:</pre>
<pre>- (void)tableViewDataSource:applyFilter:</pre>
<pre>- (void)tableViewDataSource:removeFilter:</pre>
The delegate is reponsible for adding/removing content into the datasource
*/
@implementation TNTableViewLazyDataSource: CPObject
{
BOOL _currentlyLoading @accessors(getter=isCurrentlyLoading, setter=setCurrentlyLoading:);
CPArray _content @accessors(property=content);
CPArray _searchableKeyPaths @accessors(property=searchableKeyPaths);
CPTableView _table @accessors(property=table);
id _delegate @accessors(property=delegate);
int _lazyLoadingTrigger @accessors(property=lazyLoadingTrigger);
int _totalCount @accessors(property=totalCount);
CPSearchField _searchField;
CPString _filter;
}
#pragma mark -
#pragma mark Initialization
- (id)init
{
if (self = [super init])
{
_searchableKeyPaths = [CPArray array];
_content = [CPArray array];
_lazyLoadingTrigger = 10;
_currentlyLoading = NO;
_totalCount = -1;
}
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
*/
- (@action)filterObjects:(id)sender
{
if (!_searchField)
_searchField = sender;
_filter = [[sender stringValue] uppercaseString];
if (!(_filter) || (_filter == @""))
{
if (!_currentlyLoading && _delegate && [_delegate respondsToSelector:@selector(tableViewDataSource:removeFilter:)])
{
_currentlyLoading = YES;
[_delegate tableViewDataSource:self removeFilter:_filter];
}
return;
}
if (!_currentlyLoading && _delegate && [_delegate respondsToSelector:@selector(tableViewDataSource:applyFilter:)])
{
_currentlyLoading = YES;
[_delegate tableViewDataSource:self applyFilter:_filter];
}
}
#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;
}
/*! add an object to the datasource
@param anObject object to add
*/
- (void)addObject:(id)anObject
{
[_content addObject:anObject];
}
/*! 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 [_content 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];
}
/*! return the object at given index
@param anObject int representing the position
@return the object
*/
- (void)objectAtIndex:(int)index
{
return _content[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 [_content objectsAtIndexes:aSet];
}
/*! removes the object at given index
@param anObject int representing the position
*/
- (void)removeObjectAtIndex:(int)index
{
[_content removeObjectAtIndex:index];
}
/*! removes the object at given indexes contained in a CPIndexSet
@param anObject CPIndexSet representing the positions
*/
- (void)removeObjectsAtIndexes:(CPIndexSet)aSet
{
[_content removeObjectsAtIndexes:aSet];
}
/*! remove the given object from the array
@param anObject the object to remove
*/
- (void)removeObject:(id)anObject
{
[_content removeObject:anObject];
}
/*! remove all objects
*/
- (void)removeAllObjects
{
[_content removeAllObjects];
}
/*! remove last object
*/
- (void)removeLastObject
{
[_content removeLastObject];
}
/*! remove first object
*/
- (void)removeFirstObject
{
[_content removeFirstObject];
}
/*! return the index of the given object
@param anObject the object
@return the index of the given object
*/
- (int)indexOfObject:(id)anObject
{
return [_content 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 [_content count];
}
#pragma mark -
#pragma mark Datasource implementation
- (CPNumber)numberOfRowsInTableView:(CPTableView)aTable
{
return [_content count];
}
- (id)tableView:(CPTableView)aTable objectValueForTableColumn:(CPNumber)aCol row:(CPNumber)aRow
{
var identifier = [aCol identifier];
if (!_currentlyLoading
&& (_filter == @"" || !_filter)
&& [_content count] < _totalCount
&& (aRow + _lazyLoadingTrigger >= [_content count])
&& _delegate
&& [_delegate respondsToSelector:@selector(tableViewDataSourceNeedsLoading:)])
{
_currentlyLoading = YES;
[_delegate tableViewDataSourceNeedsLoading:self];
}
return [_content[aRow] valueForKeyPath:identifier];
}
- (void)tableView:(CPTableView)aTableView sortDescriptorsDidChange:(CPArray)oldDescriptors
{
var indexes = [aTableView selectedRowIndexes],
selectedObjects = [_content objectsAtIndexes:indexes],
indexesToSelect = [[CPIndexSet alloc] init];
[_content sortUsingDescriptors:[aTableView sortDescriptors]];
for (var i = 0, c = [selectedObjects count]; i < c; i++)
{
var object = selectedObjects[i];
[indexesToSelect addIndex:[_content indexOfObject:object]];
}
[_table selectRowIndexes:indexesToSelect byExtendingSelection:NO];
}
- (void)tableView:(CPTableView)aTableView setObjectValue:(id)aValue forTableColumn:(CPTableColumn)aCol row:(int)aRow
{
var identifier = [aCol identifier];
[_content[aRow] setValue:aValue forKeyPath:identifier];
}
@end