-
Notifications
You must be signed in to change notification settings - Fork 1
/
repeat.js
92 lines (75 loc) · 2.59 KB
/
repeat.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
var O = require("pop-observe");
var swap = require("pop-swap");
var empty = [];
module.exports = Repetition;
function Repetition(body, scope) {
this.body = body;
this.scope = scope;
this.iterations = [];
this.Iteration = scope.argument.component;
this.id = scope.id;
this.observer = null;
this._value = null;
this.value = [];
}
Object.defineProperty(Repetition.prototype, "value", {
get: function () {
return this._value;
},
set: function (value) {
if (!Array.isArray(value)) {
throw new Error('Value of repetition must be an array');
}
if (this.observer) {
this.observer.cancel();
this.handleValueRangeChange(empty, this._value, 0);
}
this._value = value;
this.handleValueRangeChange(this._value, empty, 0);
this.observer = O.observeRangeChange(this._value, this, "value");
}
});
Repetition.prototype.handleValueRangeChange = function (plus, minus, index) {
var body = this.body;
var document = this.body.ownerDocument;
for (var offset = index; offset < index + minus.length; offset++) {
var iteration = this.iterations[offset];
body.removeChild(iteration.body);
iteration.value = null;
iteration.index = null;
iteration.body = null;
if (iteration.destroy) {
iteration.destroy();
}
}
var nextIteration = this.iterations[index];
var nextSibling = nextIteration && nextIteration.body;
var add = [];
for (var offset = 0; offset < plus.length; offset++) {
var value = plus[offset];
var iterationNode = document.createBody();
var iterationScope = this.scope.nestComponents();
var iteration = new this.Iteration(iterationNode, iterationScope);
iteration.value = value;
iteration.index = index + offset;
iteration.body = iterationNode;
iterationScope.hookup(this.scope.id + ":iteration", iteration);
body.insertBefore(iterationNode, nextSibling);
add.push(iteration);
}
swap(this.iterations, index, minus.length, add);
// Update indexes
for (var offset = index; offset < this.iterations.length; offset++) {
this.iterations[offset].index = offset;
}
};
Repetition.prototype.redraw = function (region) {
for (var index = 0; index < this.iterations.length; index++) {
var iteration = this.iterations[index];
iteration.redraw(region);
}
};
Repetition.prototype.destroy = function () {
this.observer.cancel();
this.handleValuesRangeChange([], this._value, 0);
};