lethain / mahou

A Cappuccino, Yahoo! BOSS, and Google App Engine based web search

This URL has Read+Write access

mahou / static / WLResultsView.j
100644 116 lines (95 sloc) 2.52 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
import <Foundation/CPObject.j>
import "WLScrollView.j"
 
@implementation WLResultsView : WLScrollView {
  CPArray _results;
  CPString _searchString;
  CPCollectionView _collectionView;
  int _offset;
  int _count;
  BOOL _countChanged;
  BOOL _recieved;
}
 
-(id)initWithFrame:aFrame {
  self = [super initWithFrame:aFrame];
  [self createCollectionView];
  [self setAutohidesScrollers:YES];
  _count = 20;
  _countChanged = NO;
  return self;
}
 
-(void)createCollectionView {
  var bounds = [self bounds];
  var aFrame = CPRectMake(0,0,
CGRectGetWidth(bounds)-20,CGRectGetHeight(bounds));
  
  _collectionView = [[CPCollectionView alloc] initWithFrame:aFrame];
  [_collectionView setDelegate:self];
  [_collectionView setItemPrototype:[self _itemPrototype]];
  [_collectionView setMinItemSize:[self _minItemSize]];
  [_collectionView setMaxItemSize:[self _maxItemSize]];
  [_collectionView setAutoresizingMask: CPViewWidthSizable];
  [self setDocumentView:_collectionView];
}
 
-(CPSize)_minItemSize {
  return CGSizeMake(150,150);
}
 
-(CPSize)_maxItemSize {
  return CGSizeMake(150,150);
}
 
-(CPCollectionViewItem)_itemPrototype {
  // implement in subclass
}
 
-(void)searchFor: (CPString)searchString {
  if (!_countChanged && [_searchString caseInsensitiveCompare:searchString]==0) {
    // Don't re-search already search results.
    return;
  }
  else _countChanged = NO;
 
  [self _clearResults];
  _searchString = searchString;
  _offset = 0;
  [self _search];
}
 
-(int)_offsetIncrement {
  return 20;
}
 
-(void)_clearResults {
  //[self createCollectionView];
  _results = [[CPArray alloc] init];
}
 
-(void)_setResults: (CPArray)anArray {
  [_results addObjectsFromArray:anArray];
  [self _resultsUpdated];
  _recieved = YES;
}
 
-(void)scrollerMovedToPosition: (float)scrollerPos {
  if (scrollerPos > 0.8 && _recieved != NO) {
    _recieved = NO;
    [self _search];
  }
}
 
-(void)_resultsUpdated {
  [_collectionView setContent:[]];
  //[self createCollectionView];
  [_collectionView setContent:_results];
}
 
-(void)_search {
  _offset = _offset + [self _offsetIncrement];
}
 
-(void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data {
  [self _setResults:eval(data)];
}
- (void)connection:(CPURLConnection)aConnection didFailWithError:(CPString)error
{
    //alert("error: " + error);
}
 
-(void)connectionDidFinishLoading:(CPURLConnection)connection {
  // finished
}
 
-(void)setCount: (int)anInt {
  _countChanged = YES;
  _count = anInt;
}
 
-(void)count {
  return _count;
}
 
@end