-
Notifications
You must be signed in to change notification settings - Fork 10
/
GSMarkupTagFireside.j
executable file
·260 lines (224 loc) · 7.03 KB
/
GSMarkupTagFireside.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
@import "GSMarkupTagObject.j"
@import <Foundation/CPObject.j>
@import <AppKit/CPArrayController.j>
@import "Fireside.j"
@implementation GSMarkupColumn: GSMarkupTagObject
+ (CPString) tagName
{
return @"column";
}
-(CPString) name
{ return [_attributes objectForKey: @"name"];
}
-(BOOL) isPK
{ return [self boolValueForAttribute: @"primaryKey"]==1;
}
-(BOOL) isNumeric
{ return [self boolValueForAttribute: @"numeric"]==1;
}
-(BOOL) isOptimistic
{ return [self boolValueForAttribute: @"optimistic"]==1;
}
/* Will never be called. */
- (id) allocPlatformObject
{ return nil;
}
@end
@implementation GSMarkupRelationship: GSMarkupTagObject
+ (CPString) tagName
{
return @"relationship";
}
- (BOOL) runSynced
{ return [self boolValueForAttribute:"runSynced"]==1;
}
-(CPString) name
{ return [_attributes objectForKey: @"name"];
}
-(CPString) target
{ return [_attributes objectForKey: @"target"];
}
-(CPString) targetColumn
{ return [_attributes objectForKey: @"targetColumn"];
}
-(CPString) bindingColumn
{ return [_attributes objectForKey: @"bindingColumn"];
}
-(BOOL) isToMany
{ return [_attributes objectForKey: @"type"] === "toMany";
}
@end
@implementation GSMarkupEntity: GSMarkupTagObject
+ (CPString) tagName
{
return @"entity";
}
+ (Class) platformObjectClass
{ return [FSEntity class];
}
- (id) initPlatformObject: (id)platformObject
{ var store = [_attributes objectForKey: @"store"];
var name = [_attributes objectForKey: @"id"];
platformObject = [platformObject initWithName: name andStore: store ];
// now extract columns and PK...
var myCols=[CPMutableSet new];
var myPK;
var i, count = _content?_content.length:0;
for (i = 0 ; i < count; i++)
{ var v = _content[i];
if([v isKindOfClass: [GSMarkupColumn class] ])
{ if([v isPK])
{ if(myPK) [CPException raise:CPInvalidArgumentException reason:@"Duplicate PK "+[v name]+"! "+ myPK+" already is PK!"];
else myPK=[v name];
}
if([v isNumeric]) [platformObject addNumericColumn: [v name]];
if([v isOptimistic]) [platformObject addOptimisticColumn:[v name]];
[myCols addObject:[v name]];
} else if([v isKindOfClass: [GSMarkupRelationship class] ])
{ var rel=[[FSRelationship alloc] initWithName: [v name] source: platformObject andTargetEntity: [v target]]; //set name as a temorary symbolic link. will be resolved in decoder later on.
if([v bindingColumn]) [rel setBindingColumn: [v bindingColumn] ];
if([v targetColumn]) [rel setTargetColumn: [v targetColumn] ];
if([v isToMany]) [rel setType: FSRelationshipTypeToMany];
else [rel setType: FSRelationshipTypeToOne];
if([v runSynced]) [rel setRunSynced:YES];
[platformObject addRelationship: rel];
}
}
[platformObject setColumns:myCols];
if(myPK) [platformObject setPk: myPK];
return platformObject;
}
@end
var _sharedUndoManager;
@implementation FSArrayController: CPArrayController
{ id _entity @accessors(property=entity);
id _defaultDict;
}
+(id) sharedUndoManager
{
if(!_sharedUndoManager)
{
_sharedUndoManager=[CPUndoManager new];
}
return _sharedUndoManager;
}
-(CPString) pk
{ return [_entity pk];
}
-(void) setEntity: anEntity
{ _entity=anEntity;
_entity.__ACForSpinner=self;
}
-(void) selectObjectWithPK: myPk
{ var o= [_entity objectWithPK: myPk];
[self setSelectedObjects: [o] ];
}
- (id)selectedObject
{ var s=[self selectedObjects];
return [s count]? [s objectAtIndex:0]:nil;
}
- (id)_defaultNewObject
{ return [_entity createObjectWithDictionary: _defaultDict];
}
- (void)setContent:(id)value
{ if(!value || (value.hasOwnProperty('_proxyObject') && ![value._proxyObject isKindOfClass:[CPArray class]]))
{ value= [_entity _arrayForArray: [] withDefaults: _defaultDict ];
}
if([value respondsToSelector: @selector(defaults) ])
{ _defaultDict=[[value defaults] copy];
}
[super setContent: value];
}
-(BOOL)hasSelection
{ return [[self selectedObjects] count] > 0;
}
-(BOOL)hasSingleRowSelection
{ return [[self selectedObjects] count] == 1;
}
-(BOOL)hasTwoRowsSelected
{ return [[self selectedObjects] count] == 2;
}
-(void) addObject: anObject
{ if(![anObject isKindOfClass: [FSObject class] ] )
anObject=[_entity createObjectWithDictionary:anObject];
[super addObject: anObject];
}
//<!> fixes an issue when adding to an empty arraycontroller
- (void)insertObject:(id)anObject atArrangedObjectIndex:(int)anIndex
{ [super insertObject:anObject atArrangedObjectIndex:MAX(0,anIndex)];
}
- (void)setValue:(id)newValue target:(id)target forKeyPath:(CPString)secondPart oldValue:(id)oldValue
{
[target setValue:newValue forKeyPath:secondPart];
[[[[self class] sharedUndoManager] prepareWithInvocationTarget:self]
setValue:oldValue target:target forKeyPath:secondPart oldValue:newValue];
}
- (void)_insertObjects:(CPArray) objArr undoManager:(CPUndoManager) aMngr
{
var l = [objArr count];
var arrForUndo=[];
for(var i = 0; i < l; i++)
{ var newDict= [[objArr objectAtIndex:i] dictionary];
var newObject= [_entity createObjectWithDictionary:newDict];
arrForUndo.push(newObject)
[self addObject:newObject]
}
[[aMngr prepareWithInvocationTarget:self] _removeObjects: arrForUndo undoManager:aMngr];
}
- (void) _removeObjects:(CPArray) objArr undoManager:(CPUndoManager) aMngr
{
[[aMngr prepareWithInvocationTarget:self] _insertObjects:objArr undoManager:aMngr];
[self removeObjects:objArr];
}
- (void)remove:(id)sender
{
var undoManager=[[self class] sharedUndoManager];
var sel,l;
if (undoManager && (sel=[self selectedObjects]) && (l = [sel count]) )
{ var arr=[];
for(var i = 0; i < l; i++)
{ arr.push([sel objectAtIndex:i]);
}
[[undoManager prepareWithInvocationTarget:self] _insertObjects:arr undoManager:undoManager];
}
[super remove:sender];
}
-(void) reload
{
[[FSRelationship relationshipsWithTargetEntity:_entity] makeObjectsPerformSelector:@selector(_invalidateCache)];
[[[[self class] _binderClassForBinding:@"contentArray"] getBinding:@"contentArray" forObject:self] setValueFor:@"contentArray"];
}
- (void)fullyReloadAsync
{ var entity = self._entity;
var ao = [[FSMutableArray alloc] initWithArray:[] ofEntity:entity];
ao._kvoMethod = @selector(setContent:);
ao._kvoOwner = self;
entity._pkcache = [];
[self setContent:ao];
[entity._store fetchObjectsForURLRequest:[entity._store requestForAddressingAllObjectsInEntity:entity] inEntity:entity requestDelegate:ao];
}
-(void) undo
{
[_sharedUndoManager undo];
}
-(void) redo
{
[_sharedUndoManager redo];
}
@end
@implementation GSMarkupArrayController: GSMarkupTagObject
+ (CPString) tagName
{ return @"arrayController";
}
+ (Class) platformObjectClass
{ return [FSArrayController class];
}
- (id) initPlatformObject: (id)platformObject
{ platformObject = [platformObject init];
[platformObject setAutomaticallyRearrangesObjects: YES];
[platformObject setClearsFilterPredicateOnInsertion:NO]; //<!> fixme: make this configurable via markup
[platformObject setObjectClass:[FSObject class]];
[platformObject setEntity: [_attributes objectForKey: @"entity"] ];
return platformObject;
}
@end