-
Notifications
You must be signed in to change notification settings - Fork 5
/
ISSortBar.j
167 lines (128 loc) · 5.06 KB
/
ISSortBar.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
/*
* ISSortBar.j
* GithubIssues
*
* Created by Randy Luecke on April 14, 2011.
* Copyright 2011, RCLConcepts, LLC All rights reserved.
*/
var kSortBarHeight = 36,
blankImage = nil;
/*!
This view represents a bar used for sorting issues
*/
@implementation ISSortBar : CPView
{
CPArray visibleSortItems; //This will be preserved.
CPArray sortButtons;
CPArray sortDescriptors;
@outlet id delegate @accessors;
@outlet CPSearchField searchField;
CPPopUpButton optionsPopupButton;
}
- (void)awakeFromCib
{
[self setBackgroundColor:[CPColor colorWithPatternImage:resourcesImage("sortbar_backgroung.png", 21, 36)]];
// See if the user has their orn set of visible sort items
var defaults = [CPUserDefaults standardUserDefaults];
visibleSortItems = [defaults objectForKey:"visibleSortItems"] || [];
blankImage = resourcesImage("blank.png", 10, 10);
// FIX ME: I'm sure this can be done easier
var items = ["ID", "Title", "Created", "Updated", "Creator", "Pull Request"],
keys = ["number", "title", "created_at", "updated_at", "user.login", "pull_request"],
c = items.length,
i = 0,
origin = CGPointMake(-1, 0),
optionsMenu = [[CPMenu alloc] init];
[optionsMenu setMinimumWidth:130];
sortButtons = [];
sortDescriptors = [];
for (; i < c; i++)
{
var descriptor = [[CPSortDescriptor alloc] initWithKey:keys[i] ascending:YES],
sub = [[ISSortItem alloc] initWithTitle:items[i] sortDescriptor:descriptor];
[sortDescriptors addObject:descriptor];
[sub setFrameOrigin:origin];
[sub setTarget:self];
[sub setAction:@selector(buttonWasClicked:)];
[sortButtons addObject:sub];
// add the menu item
var item = [[CPMenuItem alloc] initWithTitle:items[i] action:@selector(didClickMenuItem:) keyEquivalent:nil];
if (visibleSortItems.length)
{
if ([visibleSortItems containsObject:items[i]])
{
[self addSubview:sub];
origin.x += [sub frameSize].width;
[item setState:CPOnState];
}
}
else
{
[item setState:CPOnState];
[self addSubview:sub];
origin.x += [sub frameSize].width;
}
[optionsMenu addItem:item];
}
}
- (void)buttonWasClicked:(id)sender
{
// remove the images from all buttons
[sortButtons makeObjectsPerformSelector:@selector(setImage:) withObjects:[blankImage]];
[sortButtons makeObjectsPerformSelector:@selector(unsetThemeState:) withObjects:[CPThemeStateHighlighted]];
[sortDescriptors removeObject:[sender sortDescriptor]];
// toggle the sort descriptor
[sender toggleSort];
[sender setThemeState:CPThemeStateHighlighted];
// move the sort descriptor to the front
[sortDescriptors insertObject:[sender sortDescriptor] atIndex:0];
// fire the delegate
if ([delegate respondsToSelector:@selector(sortDescriptorsDidChange:)])
[delegate sortDescriptorsDidChange:sortDescriptors];
}
@end
/*!
A sort item represents a view in the sort bar
Sort items have two properties
- A sort descriptor
- A title
*/
@implementation ISSortItem : CPButton
{
CPSortDescriptor sortDescriptor @accessors;
CPImage sortImageUp;
CPImage sortImageDown;
}
- (id)initWithTitle:(CPString)aTitle sortDescriptor:(CPSortDescriptor)aSortDescriptor
{
self = [super initWithFrame:CGRectMake(0,0,100,24)];
sortDescriptor = aSortDescriptor;
[self setValue:[CPColor clearColor] forThemeAttribute:"bezel-color" inState:CPThemeStateNormal];
[self setValue:[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[resourcesImage("sortbar-active-left.png", 3, 35), resourcesImage("sortbar-active-center.png", 27, 36), resourcesImage("sortbar-active-right.png", 3, 36)] isVertical:NO]] forThemeAttribute:"bezel-color" inState:CPThemeStateHighlighted];
[self setValue:[CPColor colorWithRed:145/255 green:150/255 blue:153/255 alpha:1] forThemeAttribute:"text-color"];
[self setValue:[CPColor whiteColor] forThemeAttribute:"text-shadow-color"];
[self setValue:CGSizeMake(0,1) forThemeAttribute:"text-shadow-offset"];
[self setValue:CGInsetMake(0, 10, 0, 17) forThemeAttribute:"content-inset" inState:CPThemeStateBordered];
[self setFont:[CPFont boldSystemFontOfSize:11]];
[self setTitle:aTitle];
[self sizeToFit];
sortImageUp = resourcesImage("FIXME_arrowup.png", 10, 10);
sortImageDown = resourcesImage("FIXME_arrowdown.png", 10, 10);
[self setImage:blankImage];
[self setImagePosition:CPImageRight];
var size = [self frameSize];
size.width += 24;
size.height = kSortBarHeight;
[self setFrameSize:size];
return self;
}
- (void)toggleSort
{
var newDescriptor = [sortDescriptor reversedSortDescriptor];
sortDescriptor = newDescriptor;
if ([sortDescriptor ascending])
[self setImage:sortImageUp];
else
[self setImage:sortImageDown];
}
@end