-
Notifications
You must be signed in to change notification settings - Fork 2
/
dom-repeat-n.html
282 lines (253 loc) · 11.6 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
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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/mixins/mutable-data.html">
<script>
(function() {
'use strict';
/**
* @constructor
* @implements {Polymer_OptionalMutableData}
* @extends {Polymer.Element}
*/
const domRepeatBase = Polymer.OptionalMutableData(Polymer.Element);
/**
* 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
* @customElement
* @polymer
* @memberof Polymer
* @extends Polymer.Element
* @appliesMixin Polymer.MutableData
* @summary Custom element for stamping instance of a template bound to items in an array.
*/
class DomRepeatN extends domRepeatBase {
static get is() { return 'dom-repeat-n'; }
// The following specifies that the element stamp directly no content,
// i.e. does not have a <template> section declared
static get template() { return null; }
static get properties() {
/**
* Fired whenever DOM is added or removed by this template (by
* default, rendering occurs lazily).
*
* @event dom-change
*/
return {
/**
* `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'
}
}
}
// Element Lifecycle
constructor() {
super();
this.__instances = [];
this.__pool = [];
this.__renderDebouncer = null;
this.__ctor = null;
this.__isDetached = true;
this.template = null;
}
disconnectedCallback() {
super.disconnectedCallback();
this.__isDetached = true;
for (var i=0; i<this.__instances.length; i++) {
this.__detachInstance(i);
}
}
connectedCallback() {
super.connectedCallback();
// only perform attachment if the element was previously detached.
if (this.__isDetached) {
this.__isDetached = false;
var parent = this.parentNode;
for (let i=0; i<this.__instances.length; i++) {
this.__attachInstance(i, parent);
}
}
}
// Element Behavior
__ensureTemplatized() {
// 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) {
let template = this.template = this.querySelector('template');
if (!template) {
// Wait until childList changes and template should be there by then
let observer = new MutationObserver(() => {
if (this.querySelector('template')) {
observer.disconnect();
this.__render();
} else {
throw new Error('dom-repeat requires a <template> child');
}
});
observer.observe(this, {childList: true});
return false;
}
this.__ctor = Polymer.Templatize.templatize(template, this, {
mutableData: this.mutableData,
parentModel: true,
});
}
return true
}
_countChanged(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=limit-1; i>newCount-1; i--) {
this._detachAndRemoveInstance(i);
}
this._debounceTemplate(this._render);
this.fire('dom-change');
});
}
_startChanged(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(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() {
for (var i=0, k=this.__instances.length; i<k; i++) {
var inst = this.__instances[i];
inst.__setProperty(this.indexAs, i*this.increment+this.start, true);
}
}
_attachInstance(idx, parent) {
var inst = this.__instances[idx];
parent.insertBefore(inst.root, this);
}
_detachInstance(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(idx) {
var inst = this._detachInstance(idx);
if (inst) {
this._pool.push(inst);
}
this.__instances.splice(idx, 1);
}
_stampInstance(idx) {
var model = {};
model[this.indexAs] = idx;
return this.stamp(model);
}
_insertInstance(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(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(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(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);
}
}
}
}
customElements.define(DomRepeatN.is, DomRepeatN);
})();
</script>