This repository was archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRepeaterBlock.ts
More file actions
179 lines (143 loc) · 4.63 KB
/
RepeaterBlock.ts
File metadata and controls
179 lines (143 loc) · 4.63 KB
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
import Block = require('./Block');
import BlockProcessor = require('./BlockProcessor');
import IBlockSpec = require('./IBlockSpec');
import IItem = require('./IItem');
import List = require('./List');
import View = require('./View');
class RepeaterBlock extends Block {
source: string;
iterator: string;
blockTemplate: IBlockSpec[];
rendered = false;
_lastList;
_currentList = new List<IItem>();
constructor(view: View, parent: Block, source: string, iterator: string, blockTemplate: IBlockSpec[]) {
super(view, parent);
this.source = source;
this.iterator = iterator;
this.blockTemplate = blockTemplate;
}
render() {
this.rendered = true;
this._reload();
}
bind() {
this.bound = true;
var list = this.getList();
if (list.wasList) {
this.events.on(list.list, 'change', this.onChange.bind(this));
}
super.bind();
}
update() {
var previous = this._lastList;
var list = this.getList();
if (previous !== list.list) {
if (list.wasList) {
this.events.on(list.list, 'change', this.onChange.bind(this));
}
if (previous && previous.isList) {
this.events.off(previous, 'change');
}
this._reload();
}
super.update();
}
onChange(args?) {
var changeType = args ? args.type : 'reset';
switch (changeType) {
case 'insert':
this._insertChild(args.item, args.index);
break;
case 'remove':
this._removeChild(args.index);
break;
default:
this._reload();
break;
}
this.update();
}
getList(): { list: List<IItem>; wasList: boolean } {
var list = this.getValue(this.source);
this._lastList = list;
var wasList = true;
if (!list) {
list = new List<IItem>();
wasList = false;
}
if (!list.isList) {
if (!Array.isArray(list)) {
list = [list];
}
list = new List<IItem>(list);
wasList = false;
}
return {
list: list,
wasList: wasList
};
}
_insertChild(item, index: number) {
var previousIndex = index - 1;
var precedingElement: Node;
if (previousIndex < 0) {
precedingElement = this.placeholder;
} else {
var previousBlockElements = this.children[previousIndex].elements;
precedingElement = previousBlockElements[previousBlockElements.length - 1];
}
this._currentList.insertAt(index, item);
var child = new Block(this.view, this);
this.children.splice(index, 0, child);
child.scope = {};
child.scope[this.iterator] = item;
child.template = BlockProcessor.processTemplate(child, this.blockTemplate);
if (this.rendered) {
child.render();
}
this.parent.insertElements(child.elements, <any>precedingElement);
if (this.bound) {
child.bind();
}
}
_removeChild(index: number) {
var child = this.children.splice(index, 1)[0];
this._currentList.removeAt(index);
child.dispose();
this.parent.removeElements(child.elements);
child.parent = null;
child.view = null;
}
_updateChild(index: number, item: any) {
var child = this.children[index];
child.scope[this.iterator] = item;
child.update();
}
_reload() {
var newList = this.getList().list;
var currentList = this._currentList;
var count = newList.getCount();
for (var i = 0; i < count; i++) {
var newItem = newList.getAt(i);
var currentItem = currentList.getAt(i);
var newKey = (newItem.key = newItem.key || i);
var currentKey = currentItem ? (currentItem.key = currentItem.key || i) : null;
if (newItem && !currentItem) {
this._insertChild(newItem, i);
} else if (newKey !== currentKey) {
if (currentList.findBy('key', newKey) === -1) {
this._insertChild(newItem, i);
} else {
this._removeChild(i--);
}
} else {
this._updateChild(i, newItem);
}
}
while (currentList.getCount() > newList.getCount()) {
this._removeChild(i);
}
}
}
export = RepeaterBlock;