-
Notifications
You must be signed in to change notification settings - Fork 0
/
dddi-angular.js
756 lines (592 loc) · 21.2 KB
/
dddi-angular.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
(function() {
'use strict';
var DiUtils = {
/**
* Function that wraps an array-returning function, such that
* the returned array will always have the same reference
*/
wrapArrayFn: function(sourcArrayFn) {
var targetArray = [];
var result = function() {
var sourceArray = sourcArrayFn();
var l = sourceArray ? sourceArray.length : 0;
DiUtils.resizeArray(targetArray, l);
for(var i = 0; i < l; ++i) {
targetArray[i] = sourceArray[i];
}
return targetArray;
};
return result;
},
resizeArray: function(arr, targetLength, handlers) {
handlers = handlers || {};
var k = arr.length;
var i;
while(arr.length < targetLength) {
i = arr.length;
var init = handlers.preCreate ? handlers.preCreate(i, null, arr) : null;
arr.push(init);
handlers.postCreate && handlers.postCreate(i, init, arr);
}
while(arr.length > targetLength) {
i = arr.length - 1;
var item = arr[i];
handlers.preDestroy && handlers.preDestroy(i, item, arr);
arr.pop();
handlers.postDestroy && handlers.postDestroy(i, item, arr);
}
},
/**
* Takes a model function and binds it to the context returned by a contextFn.
* Returns a new function whose invocation returns the value in regard to the current context.
* If the model has a .assign method, it will be wrapped as well to execute against the context.
*/
bindExpr: function(expr, contextOrFn) {
var result = angular.isFunction(contextOrFn)
? this.bindExprFn(expr, contextOrFn)
: this.bindExprData(expr, contextOrFn)
;
return result;
},
bindExprData: function(expr, context) {
var r = function() {
var r = expr(context);
return r;
};
if(expr.assign) {
r.assign = function(value) {
expr.assign(context, value);
};
}
return r;
},
bindExprFn: function(expr, contextFn) {
var r = function() {
var context = contextFn();
var r = expr(context);
return r;
};
if(expr.assign) {
r.assign = function(value) {
var context = contextFn();
expr.assign(context, value);
};
}
return r;
},
/**
* Binds a provider to a given context
*/
bindProvider: function(provider, contextFn) {
var result = {
fn: provider.fn,
deps: provider.deps.map(function(dep) {
var r = DiUtils.bindDep(dep, contextFn);
return r;
})
};
return result;
},
/**
* Binds a dependency to a given context; called by bindProvider
*/
bindDep: function(dep, contextFn) {
var result = angular.extend({}, dep);
result.fn = DiUtils.bindExpr(dep.expr, contextFn);
return result;
},
bindAssignment: function(assignment, depContextFn, targetContextFn) {
var result = {
assignment: assignment,
// targetExprStr: assignment.targetExprStr
boundTarget: DiUtils.bindExpr(assignment.targetExpr, targetContextFn || depContextFn),
boundProvider: DiUtils.bindProvider(assignment.provider, depContextFn)
};
return result;
},
processProviderSpec: function($parse, spec) {
var result;
if(spec.$inject) {
throw new Error('Not supported yet');
}
else if(Array.isArray(spec)) {
result = DiUtils.processProviderSpecArray($parse, spec);
}
else if(spec instanceof Function) {
// Treat the provider as an identity function that dependends on the given function
var rephrased = [spec, angular.identity];
result = DiUtils.processProviderSpecArray($parse, rephrased);
} else {
throw new Error('Unknow spec');
}
return result;
},
processProviderSpecArray: function($parse, spec) {
var l = spec.length;
var depSpecs = spec.slice(0, l - 1);
var fn = spec[l - 1];
var deps = depSpecs.map(function(depSpec) {
var r = DiUtils.parseDepSpec($parse, depSpec);
return r;
});
var result = {
fn: fn,
deps: deps
};
return result;
},
/**
* =depName - deep equality - $watch( ..., true)
* \@depName - array equality
* depName - default equality - a == b
*
* TODO ?depName - strict equality - a === b
*
*
*/
parseDepSpec: function($parse, depSpec) {
var result;
if(angular.isString(depSpec)) {
var pattern = /(\?)?(=|@)?(.+)/;
var groups = pattern.exec(depSpec);
result = {
expr: $parse(groups[3]),
optional: groups[1] === '?',
cmpMode: groups[2] || ''
};
} else if(angular.isFunction(depSpec)) {
// If the argument is a function, it will always be evaluated and
// the respective value will be passed as an argument to the provider
result = {
expr: depSpec,
optional: true,
cmpMode: ''
};
} else {
throw new Error('Non-string arguments not yet supported');
}
return result;
}
};
var DynamicDi = function(scope, $parse, $q) {
this.scope = scope || {};
this.$parse = $parse;
this.$q = $q;
this.attrToProviderCtrl = {};
var self = this;
this.contextFn = function() {
return self.scope;
};
this.idToArrayMgr = {};
};
DynamicDi.prototype = {
linkArray: function(targetArrayExprOrStr, sourceArrayExprOrStr, handlers) {
var sourceArrayExpr = angular.isString(sourceArrayExprOrStr)
? this.$parse(sourceArrayExprOrStr)
: sourceArrayExprOrStr;
var targetArrayExpr = angular.isString(targetArrayExprOrStr)
? this.$parse(targetArrayExprOrStr)
: targetArrayExprOrStr;
var sourceArrayFn = DiUtils.bindExpr(sourceArrayExpr, this.scope);
var targetArrayFn = DiUtils.bindExpr(targetArrayExpr, this.scope);
var updateFn = function(sourceArr, before) {
var targetArr = targetArrayFn();
if(!targetArr) {
throw new Error('[dddi] \'' + targetArrayExprOrStr + '\' does not evaluate to an array anymore');
}
var l = sourceArr ? sourceArr.length : 0;
DiUtils.resizeArray(targetArr, l, handlers);
};
var targetArr = targetArrayFn();
if(!targetArr && targetArrayFn.assign) {
targetArr = [];
targetArrayFn.assign(targetArr);
} else {
throw new Error('[dddi] Error: \'' + targetArrayExprOrStr + '\' does not evaluate to an array, nor is it writable');
}
var self = this;
var result = this.scope.$watchCollection(sourceArrayFn, updateFn);
var init = sourceArrayFn();
updateFn(init);
return result;
},
/**
* The returned object supports registering dddi assignments
* on arrays: Each assignment is carried out for every array item.
*/
forArray: function(targetArrayExprOrStr) {
if(!angular.isString(targetArrayExprOrStr)) {
throw new Error('Sorry, non-string expressions not supported yet');
}
var arrayExpr = this.$parse(targetArrayExprOrStr);
var arrayFn = DiUtils.bindExpr(arrayExpr, this.contextFn);
// Check if there already exists a template for that array
// otherwise create a new one
var arrayMgr = this.idToArrayMgr[targetArrayExprOrStr];
if(arrayMgr == null) {
arrayMgr = new DddiArrayMgr(this, arrayFn, targetArrayExprOrStr);
this.idToArrayMgr[targetArrayExprOrStr] = arrayMgr;
}
return arrayMgr;
},
processAssignment: function(targetExprStr, providerSpec) {
var targetExpr = this.$parse(targetExprStr);
if(!targetExpr.assign) {
throw new Error('Target is not writeable: ', targetExprStr, providerSpec);
}
var provider = DiUtils.processProviderSpec(this.$parse, providerSpec);
var result = {
targetExpr: targetExpr,
targetExprStr: targetExprStr, // useful for logging
provider: provider
};
return result;
},
register: function(targetExprStr, providerSpec) {
var assignment = this.processAssignment(targetExprStr, providerSpec);
var boundAssignment = this.bindAssignment(assignment);
var result = this.installBoundAssignment(boundAssignment);
return result;
},
bindAssignment: function(assignment) {
var result = DiUtils.bindAssignment(assignment, this.contextFn);
result.targetExprStr = assignment.targetExprStr;
return result;
},
/*
installProvider: function(target, provider, targetExprStr) {
// Bind target and dependency expressions to the scope
var result = this.installProvider(target, boundProvider, targetExprStr);
return result;
},
*/
installBoundAssignment: function(boundAssignment) {
var result = this.installBoundAssignmentCore(boundAssignment.boundTarget, boundAssignment.boundProvider, boundAssignment.targetExprStr);
return result;
},
installBoundAssignmentCore: function(boundTarget, boundProvider, targetExprStr) {
console.log('[dddi] Watching \'' + targetExprStr + '\'');
var self = this;
var deps = boundProvider.deps;
var runningPromise = null;
// We explicitly add the non-referenced newValue and old Value attributes
// so that angular tracks the old value which is useful for debugging
var doChangeAction = function(newValue, oldValue) {
// Resolve dependencies to arguments
var args = deps.map(function(dep) {
// var r = dep.model(self.scope);
var r = dep.fn(); // we assume that the dep is bound to a context
return r;
});
// Validate the dependencies (currently limited to null checking)
// If this step fails, we do not invoke the provider and set the attr to null
var valid = true;
for(var i = 0; i < args.length; ++i) {
var arg = args[i];
var dep = boundProvider.deps[i];
if(!dep.optional && arg == null) {
valid = false;
break;
}
}
// TODO the function should be bound to the context
var val = valid ? boundProvider.fn.apply(self.scope, args) : null;
// Cancel any prior promise
if(runningPromise && runningPromise.cancel) {
runningPromise.cancel();
}
runningPromise = val;
var success = function(v) {
if(runningPromise == val) {
runningPromise = null;
console.log('[dddi] Updating \'' + targetExprStr + '\' with value \'' + v + '\'', v);
// target.assign(self.scope, v);
boundTarget.assign(v);
} else {
console.log('[dddi] Ignoring \'' + targetExprStr + '\' with value \'' + v + '\'', v);
}
};
var fail = function(e) {
if(runningPromise == val) {
runningPromise = null;
console.log('[dddi] Failed \'' + targetExprStr + '\': ', e);
}
};
// Deal with potential promises
// Note: It seems using $q directly may in some cases delay execution of the handlers even if val is NOT a promise
// This is undesired, as it causes dependencies needlessly to be resolved out of order
if(val && val.then) {
self.$q.when(val).then(success, fail);
} else {
success(val);
}
};
// Make the provider take immediate effect
doChangeAction();
// Group the watches:
// none: All reference watches go into an array that will be watched with $watchCollection
// @:
// =: All deep watches will go into a function returning a (static) array of all items to be deep watched
var cmpModeToDeps = {};
deps.forEach(function(dep) {
var group = cmpModeToDeps[dep.cmpMode] = cmpModeToDeps[dep.cmpMode] || [];
group.push(dep);
});
/**
*Function that returns a watchExpression function.
* The latter which will on every call return the same array instance,
* however with updated items
*/
var createArrFn = function(deps) {
var arrayFn = function() {
var r = deps.map(function(dep) {
var s = dep.fn();
return s;
});
return r;
};
var result = DiUtils.wrapArrayFn(arrayFn);
return result;
};
/*
var arr = [];
// Init the array
for(var i = 0; i < deps.length; ++i) {
var val = deps[i].fn();
arr.push(val);
}
var result = function() {
for(var i = 0; i < deps.length; ++i) {
var val = deps[i].fn();
//arr[i] = model(self.scope);
arr[i] = val;
}
return arr;
};
*/
var cmpModes = Object.keys(cmpModeToDeps);
var unwatchers = [];
cmpModes.forEach(function(cmpMode) {
var group = cmpModeToDeps[cmpMode];
var unwatcher;
var fn;
switch(cmpMode) {
case '': {
if(group.length === 1) {
unwatcher = self.scope.$watch(group[0].fn, doChangeAction);
} else {
fn = createArrFn(group);
unwatcher = self.scope.$watchCollection(fn, doChangeAction);
}
unwatchers.push(unwatcher);
break;
}
case '=': {
if(group.length === 1) {
unwatcher = self.scope.$watch(group[0].fn, doChangeAction, true);
} else {
fn = createArrFn(group);
unwatcher = self.scope.$watch(fn, doChangeAction, true);
}
unwatchers.push(unwatcher);
break;
}
case '@': {
var uws = group.map(function(dep) {
var r = self.scope.$watchCollection(dep.fn, doChangeAction);
return r;
});
unwatchers.push.apply(unwatchers, uws);
break;
}
default:
throw new Error('Unsupported watch mode: [' + mode + ']');
}
});
// Wrap each unwatcher with log output
var result = function() {
console.log('[dddi] Unwatching \'' + targetExprStr + '\'');
unwatchers.forEach(function(unwatcher) {
unwatcher();
});
};
return result;
/*
// Register all the watchers
var unwatchFns = deps.map(function(dep) {
var r = self.watch(self.scope, dep.model, function() {
doChangeAction();
}, dep.cmpMode);
return r;
});
var result = unwatchFns;
return result;
*/
}
};
/**
* Object for managing dddi registrations on a specific array
*
* dddi.forArray('arrayExpr').register('attr', [exprs, fn])
*
*
*
*
* With this array thing, we know which attribute in the items in the array
* we want to bind.
*/
var DddiArrayMgr = function(dddi, arrayFn, arrayName) {
this.dddi = dddi;
this.arrayFn = arrayFn;
this.arrayName = arrayName; // For logging purposes
this.assignments = {};
// Keep track of all watchers associated with an array item
this.indexToUnwatchers = [];
this.arrayCache = [];
this.init();
};
/**
* Array API for DDDI
*/
DddiArrayMgr.prototype = {
/**
* Watch the size of the target array, and dynamically add/remove watchers as needed
*/
init: function() {
var self = this;
// Wrap the original arrayFn such the result array always has the same
// reference (we are only interested in changes to the items)
var wrappedArrayFn = DiUtils.wrapArrayFn(this.arrayFn);
this.dddi.scope.$watchCollection(wrappedArrayFn, function(after, before) {
// if the target is not an array (anymore), remove all watchers
if(!angular.isArray(after)) {
DiUtils.resizeArray(self.arrayCache, 0);
//self.arrayCache = [];
self.unwatchAtAll();
}
else {
//self.arrayCache = after;
DiUtils.resizeArray(self.indexToUnwatchers, after.length, {
preCreate: function() {
return [];
},
preDestroy: function(index) {
self.unwatchAtIndex(index);
}
});
DiUtils.resizeArray(self.arrayCache, after.length, {
preCreate: function(index) {
var r = after[index];
return r;
},
postCreate: function(index) {
self.installAllAtIndex(index);
}
});
}
});
},
/**
* Register an assignment for an array
*/
register: function(targetExprStr, providerSpec) {
var contextFn = function() {
return this.scope;
};
var assignment = this.dddi.processAssignment(targetExprStr, providerSpec);
//var boundAssignment = this.bindAssignment(
this.assignments[targetExprStr] = assignment;
this.installAtAll(assignment);
// TODO: The result should be a deregistration function
// which removes all instances of the assignment accross all array elements
return null;
},
/**
* Binds an assignment to a certain array index
*/
bindAssignment: function(index, assignment) {
var self = this;
var depContextFn = function() {
var data = self.arrayCache ? self.arrayCache[index] : null;
var r = angular.extend({}, data);
r.$scope = self.dddi.scope;
r.$index = index;
return r;
};
var targetContextFn = function() {
var arr = self.arrayFn();
var r = arr[index];
// var r = self.arrayCache[index];
return r;
};
var result = DiUtils.bindAssignment(assignment, depContextFn, targetContextFn);
result.targetExprStr = this.arrayName + '[' + index + '].' + assignment.targetExprStr;
return result;
},
installAtAll: function(assignment) {
// Install the assignment on all current array elements
var l = this.arrayCache ? this.arrayCache.length : 0;
for(var i = 0; i < l; ++i) {
this.installAtIndex(i);
// var boundAssignment = this.bindAssignment(i, assignment);
// this.dddi.installBoundAssignment(boundAssignment);
}
},
installAllAtIndex: function(index) {
var self = this;
var keys = Object.keys(this.assignments);
keys.forEach(function(key) {
var assignment = self.assignments[key];
self.installAtIndex(index, assignment);
});
},
installAtIndex: function(index, assignment) {
var boundAssignment = this.bindAssignment(index, assignment);
var unwatcher = this.dddi.installBoundAssignment(boundAssignment);
this.indexToUnwatchers[index].push(unwatcher);
},
/**
*
*/
unwatchAtAll: function() {
this.indexToUnwatchers.forEach(function(unwatchers) {
unwatchers.forEach(function(unwatcher) {
unwatcher();
});
});
},
unwatchAtIndex: function(index) {
var unwatchers = this.indexToUnwatchers[index];
unwatchers.forEach(function(unwatcher) {
unwatcher();
});
}
};
/**
* Dependency: {
* model: 'string',
* optional: 'boolean'
* }
*
* Provider: {
* fn: 'function',
* deps: 'Dependency[]'
* }
*
*
* angular.controller('myCtrl', [ '$scope', '$dddi', function($scope, $dddi) {
* var dddi = new $dddi($scope);
* }]);
*/
angular.module('dddi', [])
.service('$dddi', [ '$parse', '$q', function($parse, $q) {
// We partially bind a DynamicDi instance to the $parse service,
var result = function(scope) {
var r = new DynamicDi(scope, $parse, $q);
return r;
};
// Expose utils
result.utils = DiUtils;
return result;
}]);
})();