-
Notifications
You must be signed in to change notification settings - Fork 0
/
gedi.js
606 lines (452 loc) · 17.1 KB
/
gedi.js
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//Copyright (C) 2012 Kory Nunn
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var Gel = require('gel-js'),
createPathToken = require('./pathToken'),
Token = Gel.Token,
paths = require('gedi-paths'),
pathConstants = paths.constants,
createSpec = require('spec-js'),
createEvents = require('./events'),
modelOperations = require('./modelOperations'),
get = modelOperations.get,
set = modelOperations.set;
//Create gedi
var gediConstructor = newGedi;
var exceptions = {
invalidPath: 'Invalid path syntax'
};
var arrayProto = [];
//***********************************************
//
// Gedi object.
//
//***********************************************
//Creates the public gedi constructor
function newGedi(model) {
// Storage for the applications model
model = model || {};
// gel instance
var gel = new Gel(),
// Storage for tracking the dirty state of the model
dirtyModel = {},
PathToken = createPathToken(get, model),
// Storage for model event handles
events = createEvents(modelGet, gel, PathToken);
//Initialise model references
events.addModelReference('[/]', model);
//internal functions
//***********************************************
//
// IE indexOf polyfill
//
//***********************************************
//IE Specific idiocy
Array.prototype.indexOf = Array.prototype.indexOf || function (object) {
for (var i = 0; i < this.length; i++) {
if (this === object){
return i;
}
}
};
// http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript
String.prototype.trim = String.prototype.trim || function () { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); };
// http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
Array.isArray = Array.isArray || function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
//End IE land.
//***********************************************
//
// Array Fast Each
//
//***********************************************
function each(object, callback) {
var isArray = Array.isArray(object);
for (var key in object) {
if(isArray && isNaN(key)){
continue;
}
if(callback(object[key], key, object)){
break;
}
}
return object;
}
//***********************************************
//
// Gel integration
//
//***********************************************
gel.tokenConverters.push(PathToken);
gel.scope.isDirty = function(scope, args){
var token = args.getRaw(0, true);
if(!token){
return false;
}
var path = (token instanceof PathToken) ? token.original : token.sourcePathInfo && token.sourcePathInfo.path;
if(!path){
return false;
}
return isDirty(paths.resolve(scope.get('_gmc_'), path));
};
gel.scope.getAllDirty = function (scope, args) {
var token = args.getRaw(0, true),
source = token && token.result;
if (source == null) {
return null;
}
var result = source.constructor(),
path = (token instanceof PathToken) ? token.original : token.sourcePathInfo && token.sourcePathInfo.path;
if(!path){
return result;
}
var resolvedPath = paths.resolve(scope.get('_gmc_'), path),
result,
itemPath;
for (var key in source) {
if (source.hasOwnProperty(key)) {
itemPath = paths.resolve(path, paths.create(key));
if (result instanceof Array) {
isDirty(itemPath) && result.push(source[key]);
} else {
isDirty(itemPath) && (result[key] = source[key]);
}
}
}
return result;
};
//***********************************************
//
// Remove
//
//***********************************************
function remove(path, model) {
var reference = model;
memoiseCache = {};
var pathParts = paths.toParts(path),
index = 0,
pathLength = pathParts.length;
if(paths.isRoot(path)){
overwriteModel({}, model);
return;
}
if(paths.isAbsolute(path)){
index = 1;
}
for(; index < pathLength; index++){
var key = pathParts[index];
//if we have hit a non-object and we have more keys after this one
if (typeof reference[key] !== "object" && index < pathLength - 1) {
break;
}
if (index === pathLength - 1) {
// if we are at the end of the line, delete the last key
if (reference instanceof Array) {
reference.splice(key, 1);
} else {
delete reference[key];
}
} else {
reference = reference[key];
}
}
return reference;
}
//***********************************************
//
// Model Get
//
//***********************************************
function modelGet(binding, parentPath, scope, returnAsTokens) {
if(parentPath && typeof parentPath !== "string"){
scope = parentPath;
parentPath = paths.create();
}
if (binding) {
var gelResult,
expression = binding;
scope = scope || {};
scope['_gmc_'] = parentPath;
return gel.evaluate(expression, scope, returnAsTokens);
}
parentPath = parentPath || paths.create();
binding = paths.resolve(parentPath, binding);
return get(binding, model);
}
//***********************************************
//
// Model Set
//
//***********************************************
function getSourcePathInfo(expression, parentPath, scope, subPathOpperation){
var scope = scope || {},
path;
scope._gmc_ = parentPath;
var resultToken = gel.evaluate(expression, scope, true)[0],
sourcePathInfo = resultToken.sourcePathInfo;
if(sourcePathInfo){
if(sourcePathInfo.subPaths){
each(sourcePathInfo.subPaths, function(item){
subPathOpperation(item);
});
return;
}
path = sourcePathInfo.path;
}else{
path = resultToken.path;
}
if(path){
subPathOpperation(path);
}
}
function DeletedItem(){}
function modelSetPath(path, value, parentPath, dirty, scope){
parentPath = parentPath || paths.create();
path = paths.resolve(parentPath, path);
setDirtyState(path, dirty);
var previousValue = get(path, model);
var keysChanged = set(path, value, model);
if(!(value instanceof DeletedItem)){
events.addModelReference(path, value);
events.trigger(path, keysChanged);
}
if(!(value && typeof value !== 'object') && previousValue && typeof previousValue === 'object'){
events.removeModelReference(path, previousValue);
}
}
function modelSet(expression, value, parentPath, dirty, scope) {
if(typeof expression === 'object' && !paths.create(expression)){
dirty = value;
value = expression;
expression = paths.createRoot();
}else if(typeof parentPath === 'boolean'){
dirty = parentPath;
parentPath = undefined;
}
getSourcePathInfo(expression, parentPath, scope, function(subPath){
modelSetPath(subPath, value, parentPath, dirty, scope);
});
}
//***********************************************
//
// Model Remove
//
//***********************************************
function modelRemove(expression, parentPath, dirty, scope) {
if(parentPath instanceof Boolean){
dirty = parentPath;
parentPath = undefined;
}
itemParentPaths = {};
getSourcePathInfo(expression, parentPath, scope, function(subPath){
modelSetPath(subPath, new DeletedItem(), parentPath, dirty, scope);
itemParentPaths[paths.append(subPath, paths.create(pathConstants.upALevel))] = null;
});
for(var key in itemParentPaths){
if(itemParentPaths.hasOwnProperty(key)){
var itemParentPath = paths.resolve(parentPath || paths.createRoot(), key),
parentObject = get(itemParentPath, model),
isArray = Array.isArray(parentObject);
if(isArray){
var anyRemoved;
for(var i = 0; i < parentObject.length; i++){
if(parentObject[i] instanceof DeletedItem){
parentObject.splice(i, 1);
i--;
anyRemoved = true;
}
}
if(anyRemoved){
events.trigger(itemParentPath);
}
}
// Always run keys version, because array's might have non-index keys
for(var key in parentObject){
if(parentObject[key] instanceof DeletedItem){
delete parentObject[key];
events.trigger(paths.append(itemParentPath, key));
}
}
}
}
}
//***********************************************
//
// Set Dirty State
//
//***********************************************
function setPathDirtyState(path, dirty, parentPath, scope){
var reference = dirtyModel;
if(!paths.create(path)){
throw exceptions.invalidPath;
}
parentPath = parentPath || paths.create();
dirty = dirty !== false;
if(paths.isRoot(path)){
dirtyModel = {
'_isDirty_': dirty
};
return;
}
var index = 0;
if(paths.isAbsolute(path)){
index = 1;
}
var pathParts = paths.toParts(paths.resolve(parentPath, path));
for(; index < pathParts.length; index++){
var key = pathParts[index];
if ((typeof reference[key] !== "object" || reference[key] === null) && index < pathParts.length - 1) {
reference[key] = {};
}
if (index === pathParts.length - 1) {
reference[key] = {};
reference[key]['_isDirty_'] = dirty;
}
else {
reference = reference[key];
}
}
if(!pathParts.length){
dirtyModel['_isDirty_'] = dirty;
}
}
function setDirtyState(expression, dirty, parentPath, scope) {
getSourcePathInfo(expression, parentPath, scope, function(subPath){
setPathDirtyState(subPath, dirty, parentPath, scope);
});
}
//***********************************************
//
// Is Dirty
//
//***********************************************
function isDirty(path) {
var reference,
hasDirtyChildren = function (ref) {
if (typeof ref !== 'object') {
return false;
}
if (ref['_isDirty_']) {
return true;
} else {
for (var key in ref) {
if (hasDirtyChildren(ref[key])) {
return true;
}
}
}
};
reference = get(path, dirtyModel);
return !!hasDirtyChildren(reference);
}
//Public Objects ******************************************************************************
function Gedi() {}
Gedi.prototype = {
paths: {
create: paths.create,
resolve: paths.resolve,
isRoot: paths.isRoot,
isAbsolute: paths.isAbsolute,
append: paths.append,
toParts: paths.toParts
},
/**
## .get
model.get(expression, parentPath, scope, returnAsTokens)
Get data from the model
// get model.stuff
var data = model.get('[stuff]');
Expressions passed to get will be evaluated by gel:
// get a list of items that have a truthy .selected property.
var items = model.get('(filter [items] {item item.selected})');
You can scope paths in the expression to a parent path:
// get the last account.
var items = model.get('(last [])', '[accounts]')'
*/
get: modelGet,
/**
## .set
model.set(expression, value, parentPath, dirty)
Set data into the model
// set model.stuff to true
model.set('[stuff]', true);
Expressions passed to set will be evaluated by gel:
// find all items that are not selected and set them to be selected
model.set('(map (filter [items] {item (! item.selected)}) {item item.selected})', true);
You can scope paths in the expression to a parent path:
// set the last account to a different account.
model.set('(last [])', '[accounts]', someAccount);
*/
set: modelSet,
/**
## .remove
model.remove(expression, value, parentPath, dirty)
If the target key is on an object, the key will be deleted.
If the target key is an index in an array, the item will be spliced out.
remove data from the model
// remove model.stuff.
model.remove('[stuff]');
Expressions passed to remove will be evaluated by gel:
// remove all selected items.
model.remove('(filter [items] {item item.selected})');
You can scope paths in the expression to a parent path:
// remove the last account.
model.remove('(last [])', '[accounts]');
*/
remove: modelRemove,
utils: {
get:get,
set:set
},
init: function (model) {
this.set(model, false);
},
/**
## .bind
model.bind(expression, callback, parentPath)
bind a callback to change events on the model
*/
bind: events.bind,
/**
## .debind
model.debind(expression, callback)
debind a callback
*/
debind: events.debind,
/**
## .trigger
model.trigger(path)
trigger events for a path
*/
trigger: events.trigger,
/**
## .isDirty
model.isDirty(path)
check if a path in the model has been changed since being marked as clean.
*/
isDirty: isDirty,
/**
## .setDirtyState
model.setDirtyState(path, dirty, parentPath)
explicity mark a path in the model as dirty or clean
*/
setDirtyState: setDirtyState,
gel: gel, // expose gel instance for extension
getNumberOfBindings: function(){
function getNumCallbacks(reference){
var length = reference.length;
for (var key in reference) {
if(isNaN(key)){
length += getNumCallbacks(reference[key]);
}
}
return length;
}
return getNumCallbacks(internalBindings);
}
};
return new Gedi();
}
module.exports = gediConstructor;