-
Notifications
You must be signed in to change notification settings - Fork 1
/
THRList.j
123 lines (109 loc) · 3.66 KB
/
THRList.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
@import <AppKit/CPView.j>
@import "THRRowTemplate.j"
@implementation THRList : CPView
{
CPArray contentArray;
THRRowTemplate rowTemplate;
CPString contentString @accessors;
}
- (id)initWithFrame:(CGRect)aRect rowTemplate:(THRRowTemplate)aRowTemplate bindTo:(id) aController withKeyPath:(CPString) aKeyPath
{
if (self = [super initWithFrame:aRect])
{
rowTemplate = aRowTemplate;
contentArray = [CPArray array];
[self addObserver:self forKeyPath:@"contentArray" options:(CPKeyValueObservingOptionNew) context:nil];
[self bind:@"contentArray" toObject:aController withKeyPath:aKeyPath options:nil];
}
return self;
}
- (void) observeValueForKeyPath:(CPString *)keyPath ofObject:(id) object change:(CPDictionary *)change context:(void *)context
{
if( [keyPath isEqual:@"contentArray"] )
{
if( contentArray )
{/*
var tmpArray = contentString.split("##");
var c=[tmpArray count];
[contentArray removeAllObjects];
for( var i=0;i<c;i++ )
{
var objectArray = [tmpArray objectAtIndex:i].split(";");
[contentArray addObject:objectArray];
}*/
[self updateLayout];
}
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void) updateLayout
{
var rowsCount = [contentArray count];
var rowHeight = rowTemplate.height;
var rowWidth = [self bounds].size.width-100;
var frameHeight = rowHeight * rowsCount;
var frameWidth = rowWidth + 100.0;
var oldFrame = [self frame];
var subViewsCount = [[self subviews] count];
for( var i=subViewsCount-1;i>=0;i--)
[[[self subviews] objectAtIndex:i] removeFromSuperview];
var odd=YES;
for( var i=0;i<rowsCount; i++ )
{
var newRow = [[CPView alloc] initWithFrame:CGRectMake(0,0+rowHeight*i,frameWidth, rowHeight)];
if( (i>0) || (i==0 && rowsCount>1) )
{
var removeButton = [[CPButton alloc] initWithFrame:CGRectMake(rowWidth+5.0,(rowHeight-24)/2.0,40,24)];
[removeButton setTitle:@"-"];
[removeButton setBezelStyle:CPRoundedBezelStyle];
[removeButton setTarget:self];
[removeButton setAction:@selector(removeRow:)];
[removeButton setTag:(100+i)];
[newRow addSubview:removeButton];
}
if( i==(rowsCount-1) )
{
var addButton = [[CPButton alloc] initWithFrame:CGRectMake(rowWidth+50.0,(rowHeight-24)/2.0,40,24)];
[addButton setTitle:@"+"];
[addButton setBezelStyle:CPRoundedBezelStyle];
[addButton setTarget:self];
[addButton setAction:@selector(addRow:)];
[newRow addSubview:addButton];
}
odd=!odd;
if( odd )
{
[newRow setBackgroundColor:[CPColor colorWithHexString:@"E0ECFA"]];
}
var newRowTemplate =[rowTemplate newRowAtIndex:i withContent:[contentArray objectAtIndex:i]];
[newRow addSubview:newRowTemplate];
[self addSubview:newRow];
}
[self setFrame:CGRectMake(oldFrame.origin.x, oldFrame.origin.y, frameWidth, frameHeight)];
//[self setNeedsDisplay:YES];
}
- (void) removeRow:(id) sender
{
var rowIndex = [sender tag]-100;
[contentArray removeObjectAtIndex:rowIndex];
[self updateLayout];
}
- (void) addRow:(id) sender
{
[contentArray addObject:[rowTemplate defaultRowString]];
[self updateLayout];
}
/*
- (void)drawRect:(CGRect)aRect
{
// Add drawing code here
}
*/
/*
- (void)layoutSubviews
{
// Add layout code here
}
*/
@end