forked from GeoloeG/dom-repeat-n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-repeat-n.html
238 lines (209 loc) · 6.33 KB
/
dom-repeat-n.html
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
<link rel="import" href="../polymer/polymer.html">
<!--
A template element that repeat `n` times its content.
Example:
<template is="dom-repeat-n" count="3">
<div>I am div {{index}}</div>
</template>
@demo demo/index.html
@hero hero.svg
-->
<script>
Polymer({
is: 'dom-repeat-n',
extends: 'template',
// The following specifies that the element stamp directly no content,
// i.e. does not have a <template> section declared
_template: null,
/**
* Fired whenever DOM is added or removed by this template (by
* default, rendering occurs lazily).
*
* @event dom-change
*/
properties: {
/**
* `count` specifies the number of times to repeat the template content.
*/
count: {
type: Number,
value: 0,
observer: '_countChanged'
},
/**
* `start` specifies the value of the first index and default to 0.
*/
start: {
type: Number,
value: 0,
observer: '_startChanged'
},
/**
* `increment` specifies the increment value between indices.
*/
increment: {
type: Number,
value: 1,
observer: '_incrementChanged'
},
/**
* The name of the variable to add to the binding scope with the index
* for the templates instances.
*/
indexAs: {
type: String,
value: 'index'
},
},
behaviors: [
Polymer.Templatizer
],
// Element Lifecycle
created: function() {
this._instances = [];
this._pool = [];
},
ready: function() {
// Template instance props that should be excluded from forwarding
this._instanceProps = {};
this._instanceProps[this.indexAs] = true;
// Templatizing (generating the instance constructor) needs to wait
// until ready, since won't have its template content handed back to
// it until then
if (!this.ctor) {
this.templatize(this);
}
},
attached: function() {
var parent = Polymer.dom(Polymer.dom(this).parentNode);
for (var i=0; i<this._instances.length; i++) {
this._attachInstance(i, parent);
}
},
detached: function() {
for (var i=0; i<this._instances.length; i++) {
this._detachInstance(i);
}
},
// Element Behavior
_countChanged: function(newCount, oldCount) {
if(parseInt(newCount)) {
newCount = parseInt(newCount);
}
if (typeof(newCount) !== "number") {
this.count = oldCount;
console.error("dom-repeat-n: count should be a number");
return;
}
if (newCount < 0) {
this.count = oldCount;
console.error("dom-repeat-n: count cannot be negative");
return;
}
// we use async to enable ready to be called before this code
this.async(function() {
var i;
// Generate possible missing instances if count increased
for (i=0; i<newCount; i++) {
var inst = this._instances[i];
if (!inst) {
inst = this._insertInstance(i);
}
}
// Remove any extra instances from previous state
var limit = this._instances.length;
for (i=newCount; i<limit; i++) {
this._detachAndRemoveInstance(i);
}
this._debounceTemplate(this._render);
this.fire('dom-change');
});
},
_startChanged: function(newStart, oldStart) {
if (typeof(newStart) !== "number") {
this.start = oldStart;
console.error("dom-repeat-n: start should be a number");
return;
}
this._debounceTemplate(this._render);
},
_incrementChanged: function(newIncrement, oldIncrement) {
if (typeof(newIncrement) !== "number") {
this.increment = oldIncrement;
console.error("dom-repeat-n: start should be a number");
return;
}
this._debounceTemplate(this._render);
},
_render: function() {
for (var i=0; i<this.count; i++) {
var inst = this._instances[i];
inst.__setProperty(this.indexAs, i*this.increment+this.start, true);
}
},
_attachInstance: function(idx, parent) {
var inst = this._instances[idx];
parent.insertBefore(inst.root, this);
},
_detachInstance: function(idx) {
var inst = this._instances[idx];
for (var i=0; i<inst._children.length; i++) {
var el = inst._children[i];
Polymer.dom(inst.root).appendChild(el);
}
return inst;
},
_detachAndRemoveInstance: function(idx) {
var inst = this._detachInstance(idx);
if (inst) {
this._pool.push(inst);
}
this._instances.splice(idx, 1);
},
_stampInstance: function(idx) {
var model = {};
model[this.indexAs] = idx;
return this.stamp(model);
},
_insertInstance: function(idx) {
var inst = this._pool.pop();
if (!inst) {
inst = this._stampInstance(idx);
}
var beforeRow = this._instances[idx + 1];
var beforeNode = (beforeRow && !beforeRow.isPlaceholder) ? beforeRow._children[0] : this;
var parentNode = Polymer.dom(this).parentNode;
Polymer.dom(parentNode).insertBefore(inst.root, beforeNode);
this._instances.push(inst);
return inst;
},
// Implements extension point from Templatizer mixin
_showHideChildren: function(hidden) {
for (var i=0; i<this._instances.length; i++) {
this._instances[i]._showHideChildren(hidden);
}
},
// Implements extension point from Templatizer mixin
// Called as side-effect of a host property change, responsible for
// notifying parent path change on each inst
_forwardParentProp: function(prop, value) {
var i$ = this._instances;
for (var i=0, inst; (i<i$.length) && (inst=i$[i]); i++) {
if (!inst.isPlaceholder) {
inst.__setProperty(prop, value, true);
}
}
},
// Implements extension point from Templatizer
// Called as side-effect of a host path change, responsible for
// notifying parent path change on each inst
_forwardParentPath: function(path, value) {
var i$ = this._instances;
for (var i=0, inst; (i<i$.length) && (inst=i$[i]); i++) {
if (!inst.isPlaceholder) {
inst._notifyPath(path, value, true);
}
}
}
});
</script>