-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-repeat.html
221 lines (193 loc) · 6.34 KB
/
multi-repeat.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="multi-registerable-mixin.html">
<link rel="import" href="multi-repeat-select.html">
<dom-module id="multi-repeat">
<template>
<slot id="slot"></slot>
<multi-repeat-select selected="{{selected}}" selected-item="{{selectedItem}}" selected-values="{{selectedValues}}" selected-items="{{selectedItems}}" attr-for-selected="[[attrForSelected]]" selected-attribute="[[selectedAttribute]]" multi="[[multi]]">
<template id="repeat" is="dom-repeat" items="{{items}}" rendered-item-count="{{renderedItemCount}}" filter="[[filter]]" sort="[[sort]]" observe="[[observe]]" delay="[[delay]]" as="[[as]]" index-as="[[indexAs]]"></template>
</multi-repeat-select>
</template>
<script>
(function() {
/**
* ## MultiRepeat
*
* `<multi-repeat>`
*
* @memberof MultiVerse
* @customElement
* @polymer
* @demo
**/
class MultiRepeat extends
MultiVerse.mixin.MultiRegisterable(
Polymer.Element) {
static get is() { return 'multi-repeat'; }
static get properties() {
return Object.assign({}, {
/**
* Overriden from Polymer.IronSelectableBehavior
*/
attrForSelected: {
type: String,
value: 'name'
},
/**
* Overriden from Polymer.IronSelectableBehavior
*/
selectedAttribute: {
type: String,
value: 'active'
},
/**
* Overriden from Polymer.IronSelectableBehavior
*/
selectable: {
type: String,
value: 'paper-button'
},
/*
* `multi` set true to allow multiple selection
*/
multi: {
type: Boolean
},
/*
* `fireEventName` the name of the event to be fired when connected. a contained with multi-register-mixin applied
* will listed to this event to register the component.
*/
fireEventName: {
type: String,
value: 'multi-verse-added'
},
renderedItemCount: {
type: Number,
notify: true
},
/**
* Gets or sets the selected element. The default is to use the index of the item.
* @type {string|number}
*/
selected: {
type: String,
notify: true
},
/**
* Returns the currently selected item.
*
* @type {?Object}
*/
selectedItem: {
type: Object,
readOnly: true,
notify: true
},
selectedValues: {
type: Array,
notify: true
},
/**
* Returns the currently selected items.
*
* @type {?Object}
*/
selectedItems: {
type: Object,
notify: true
},
/**
* A function that should determine the sort order of the items. This
* property should either be provided as a string, indicating a method
* name on the element's host, or else be an actual function. The
* function should match the sort function passed to `Array.sort`.
* Using a sort function has no effect on the underlying `items` array.
*/
sort: {
type: Function,
// observer: '__sortChanged'
},
/**
* A function that can be used to filter items out of the view. This
* property should either be provided as a string, indicating a method
* name on the element's host, or else be an actual function. The
* function should match the sort function passed to `Array.filter`.
* Using a filter function has no effect on the underlying `items` array.
*/
filter: {
type: Function,
// observer: '__filterChanged'
},
/**
* When using a `filter` or `sort` function, the `observe` property
* should be set to a space-separated list of the names of item
* sub-fields that should trigger a re-sort or re-filter when changed.
* These should generally be fields of `item` that the sort or filter
* function depends on.
*/
observe: {
type: String,
// observer: '__observeChanged'
},
});
}
ready() {
super.ready();
this.replaceTemplate();
}
dataChanged(data) {
if (data) {
this.items = data;
} else {
this.forceNotify();
}
}
// Note(cg): we need to for rendering of repeat without loosing items .
forceNotify() {
if (!this.items) {
return;
}
for (var i = this.items.length - 1; i >= 0; i--) {
this.notifyPath(`items.${i}`, JSON.parse(JSON.stringify(this.items[i])));
}
}
replaceTemplate( ) {
const template = this.$.slot.assignedNodes().find(n => n.localName === '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.replaceTemplate();
// Note(cg): we need to reset rom-repeat.
this.$.repeat.__ctor = null;
if(this.sort) {
this.$.repeat.sort = this.sort;
}
if(this.filter) {
this.$.repeat.filter = this.filter;
}
if(this.observe) {
this.$.repeat.observe = this.observe;
}
this.$.repeat.__render();
} else {
throw new Error('multi-repeat requires a <template> to be passed to');
}
});
}
const currentTemplate = this.$.repeat.querySelector('template');
this.$.repeat.replaceChild(template, currentTemplate);
}
}
customElements.define(MultiRepeat.is, MultiRepeat);
if (!window.MultiVerse) {
window.MultiVerse = {};
}
/*
* @namespace MultiChart
*/
window.MultiVerse.MultiRepeat = MultiRepeat;
})();
</script>
</dom-module>