-
Notifications
You must be signed in to change notification settings - Fork 3
/
Location.j
222 lines (176 loc) · 5.48 KB
/
Location.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
@implementation Location : CPObject
{
id superLocation @accessors;
CPString name @accessors;
CPString type @accessors;
CPString address @accessors;
CPArray subLocations @accessors;
CPArray items @accessors;
}
- (id)initWithName:(CPString)aName
{
self = [super init];
if (self)
{
name = aName;
subLocations = [ ];
items = [ ];
type = "";
address = "";
superLocation = nil;
}
return self;
}
- (void)addSubLocation:(Location)aLocation atIndex:(int)anIndex
{
[aLocation setSuperLocation:self];
[subLocations insertObject:aLocation atIndex:anIndex];
}
- (void)addSubLocation:(Location)aLocation
{
[self addSubLocation:aLocation atIndex:subLocations.length];
}
- (void)removeSubLocation:(Location)aLocation
{
[aLocation setSuperLocation:nil];
[subLocations removeObject:aLocation];
}
- (void)moveItemAtIndex:(int)fromIndex toIndex:(int)toIndex
{
if (toIndex > fromIndex)
toIndex--;
if (fromIndex === toIndex)
return;
var temp = items[fromIndex];
[items removeObjectAtIndex:fromIndex];
[items insertObject:temp atIndex:toIndex];
var invoke = [CPInvocation invocationWithMethodSignature:@"moveItemAtIndex:toIndex:"];
[invoke setTarget:self];
[invoke setSelector:@selector(moveItemAtIndex:toIndex:)];
[invoke setArgument:toIndex atIndex:2];
[invoke setArgument:fromIndex atIndex:3];
var appController = [CPApp delegate];
[[appController locationController] refreshItemData]
[[appController undoManager] registerUndoWithTarget:invoke selector:@selector(invoke) object:nil];
}
- (int)indexOfSubLocation:(Location)aLocation
{
return [subLocations indexOfObject:aLocation];
}
- (void)addItem:(Item)anItem
{
[items addObject:anItem];
}
- (void)removeItem:(Item)anItem
{
[items removeObjectIdenticalTo:anItem];
}
- (Item)itemAtIndex:(int)anIndex
{
return [items objectAtIndex:anIndex];
}
- (int)numberOfItems
{
return [items count];
}
- (void)replaceItemWithItem:(CPDictionary)undoItems
{
var oldItem = [undoItems valueForKey:@"oldItem"],
newItem = [undoItems valueForKey:@"newItem"],
controller = [[CPApp delegate] locationController],
locationView = [controller locationView];
[items replaceObjectAtIndex:[items indexOfObject:oldItem] withObject:newItem];
[controller refreshItemData];
//now select that item we just added if it exists...
var selectIndex = [items indexOfObject:newItem];
if (selectIndex !== -1)
{
[[locationView itemTableView] selectRowIndexes:[CPIndexSet indexSetWithIndex:selectIndex] byExtendingSelection:NO];
[locationView setActiveItem:newItem];
}
var dict = [CPDictionary dictionaryWithObjects:[newItem, oldItem] forKeys:[@"oldItem", @"newItem"]];
[[[CPApp delegate] undoManager] registerUndoWithTarget:self selector:@selector(replaceItemWithItem:) object:dict];
}
- (CPDictionary)dictionaryValues
{
var dictionary = [CPDictionary new];
[dictionary setValue:name forKey:@"name"];
[dictionary setValue:type forKey:@"type"];
[dictionary setValue:address forKey:@"address"];
return dictionary;
}
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super init];
if (self)
{
name = [aCoder decodeObjectForKey:@"LocationName"];
type = [aCoder decodeObjectForKey:@"LocationType"];
address = [aCoder decodeObjectForKey:@"LocationAddress"];
subLocations = [aCoder decodeObjectForKey:@"LocationSubLocations"];
superLocation = [aCoder decodeObjectForKey:@"LocationSuperLocation"];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:name forKey:@"LocationName"];
[aCoder encodeObject:type forKey:@"LocationType"];
[aCoder encodeObject:address forKey:@"LocationAddress"];
[aCoder encodeObject:subLocations forKey:@"LocationSubLocations"];
[aCoder encodeObject:superLocation forKey:@"LocationSuperLocation"];
}
- (BOOL)isEqual:(id)anObject
{
if ([anObject class] === [self class])
return ([name isEqual:[anObject name]] && [type isEqual:[anObject type]] && [address isEqual:[anObject address]] && [subLocations isEqual:[anObject subLocations]]);
else
return NO;
}
- (Location)copy
{
var item = [super copy];
[item setSuperLocation:superLocation];
[item setName:name];
[item setType:type];
[item setAddress:address];
[item setSubLocations:subLocations];
return item;
}
- (CPString)toJSON
{
var object = { };
object.name = name;
object.type = type;
object.address = address;
object.subLocations = subLocations;
object.items = items;
return object;
}
+ (Location)decodeJSON:(Object)anObject
{
var item = [[Location alloc] initWithName:anObject.name];
[item setType:anObject.type];
if (![item type])
[item setType:""];
[item setAddress:anObject.address];
for (var i = 0; i < anObject.subLocations.length; i++)
{
var subItem = [Location decodeJSON:anObject.subLocations[i]];
[item addSubLocation:subItem];
}
for (var i = 0; i < anObject.items.length; i++)
{
var anItem = [Item decodeJSON:anObject.items[i]];
[item addItem:anItem];
}
return item;
}
@end
objj_class.prototype.toJSON = objj_object.prototype.toJSON = function()
{
if (this.isa && class_getInstanceMethod(this.isa, "toJSON") != NULL)
return [this toJSON];
else
return String(this) + " (-toJSON not implemented)";
}