forked from Neovici/cosmoz-omnitable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmoz-omnitable-repeater-behavior.html
263 lines (224 loc) · 6.55 KB
/
cosmoz-omnitable-repeater-behavior.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
<link rel="import" href="../polymer/polymer.html">
<script>
(function () {
'use strict';
window.Cosmoz = window.Cosmoz || {};
const IS_V2 = Polymer.flush != null;
/**
* @polymerBehavior
*/
Cosmoz.OmnitableRepeaterBehavior = {
properties: {
columns: {
type: Array
},
groupOnColumn: {
type: Object,
observer: '_groupOnColumnChanged'
}
},
observers: [
'_columnsChanged(columns.*)',
],
_elements: null,
/* eslint-disable no-empty-function, no-unused-vars */
/**
* The type of element to be repeated.
* Must be defined in implementors.
*/
_elementType: null,
/**
* Slot name assigned to the repeated elements.
* Must be defined in implementors.
*/
_slotName: null,
/**
* Get a template instance for the specified column
* Must be defined in implementors.
* @param {Object} column - The column.
* @return {Object} - The instance.
*/
_getTemplateInstance: function (column) {},
/**
* Detach and release the template instance associated with
* the specified column and rendered in the specified element.
* Must be defined in implementors.
* @param {Object} instance - The instance.
* @param {Object} column - The column.
* @param {Object} element - The element.
* @return {undefined}
*/
_detachTemplateInstance: function (instance, column, element) {},
/**
* Configure a newly created repeated element
* Must be defined in implementors.
* @param {Object} element - The element.
* @return {undefined}
*/
_configureElement: function (element) {},
/**
* Configure a newly created cell template instance
* Must be defined in implementors.
* @param {Object} instance - The instance.
* @return {undefined}
*/
_configureTemplateInstance: function (instance) {},
/* eslint-enable no-empty-function, no-unused-vars */
/**
* Get an array of the elements generated by this repeater
*/
get elements() {
if (this._elements) {
return this._elements.slice(0);
}
return [];
},
get templateInstances() {
if (this._elements) {
return this._elements.map(function (element) {
return element.__instance;
});
}
return [];
},
/**
* Get the template instance rendered by the specified element.
* @param {Object} element - The element.
* @return {Object} - The instance.
*/
getElementTemplateInstance: function (element) {
return element.__instance;
},
/**
* Get the column that was used to rendered the specified element
* @param {Object} element - The element.
* @return {Object} - The column.
*/
getElementColumn: function (element) {
return element.__column;
},
_columnsChanged: function (change) {
var removedColumns;
if (change.path === 'columns') {
if (this._elements && this._elements.length) {
removedColumns = this._elements.map(function (element) {
return element.__column;
});
this._removeElements(0, removedColumns);
} else {
this._elements = [];
}
this._addElements(0, this.columns.length);
} else if (change.path === 'columns.splices') {
this._renderSplices(change.value.indexSplices);
} else if (change.path !== 'columns.length') {
// column property change
console.warn('column property change');
}
},
_renderSplices: function (splices) {
splices.forEach(function (splice) {
if (splice.removed.length) {
this._removeElements(splice.index, splice.removed);
}
if (splice.addedCount > 0) {
this._addElements(splice.index, splice.addedCount);
}
}, this);
},
_addElements: function (start, count) {
var i,
end = start + count,
column,
instance,
element,
parent = Polymer.dom(this);
for (i = start; i < end; i++) {
column = this.columns[i];
element = document.createElement(this._elementType);
instance = this._getTemplateInstance(column);
element.__instance = instance;
element.__column = column;
if (column === this.groupOnColumn) {
element.setAttribute('hidden', '');
} else if (element.hasAttribute('hidden')) {
element.removeAttribute('hidden', '');
}
this._configureElement(element, column);
this._configureTemplateInstance(instance);
element.setAttribute('slot', this._slotName);
Polymer.dom(element).appendChild(instance.root);
if (i < this._elements.length) {
parent.insertBefore(element, this._elements[i]);
} else {
parent.appendChild(element);
}
// HACK(plequang): repeated element should have style scoped to cosmoz-omnitable, not to the column element.
// This is how it works with native shadow DOM but not with shady.
if (!Polymer.Settings.useNativeShadow && Polymer.StyleTransformer) {
Polymer.StyleTransformer.dom(element, column.is, false, true);
Polymer.StyleTransformer.dom(element, 'cosmoz-omnitable', false, false);
}
this._elements.splice(i, 0, element);
}
},
_removeElements: function (start, removedColumns) {
var i,
index,
element;
for (i = 0; i < removedColumns.length; i++) {
index = start + i;
element = this._elements[index];
if (element.hasAttribute('hidden')) {
element.removeAttribute('hidden');
}
this._detachTemplateInstance(element.__instance, element.__column, element);
Polymer.dom(this).removeChild(element);
}
this._elements.splice(start, removedColumns.length);
},
_groupOnColumnChanged: function (column, previousColumn) {
if (!this._elements || !this._elements.length) {
return;
}
if (previousColumn) {
const previousElement = this._elements.find(e => e.__column === previousColumn);
if (previousElement) {
previousElement.removeAttribute('hidden');
}
}
if (column) {
const element = this._elements.find(e => e.__column === column);
if (element) {
element.setAttribute('hidden', '');
}
}
},
_forwardProperty: function (instance, name, value, flush = false) {
if (IS_V2) {
instance._setPendingProperty(name, value);
} else {
instance[name] = value;
}
if (flush) {
this._forwardPropertiesFlush(instance);
}
},
_forwardNotifyPath: function (instance, path, value, isPathNotification = false, flush = false) {
if (IS_V2) {
instance._setPendingPropertyOrPath(path, value, false, isPathNotification);
} else {
instance.notifyPath(path, value, isPathNotification);
}
if (flush) {
this._forwardPropertiesFlush(instance);
}
},
_forwardPropertiesFlush: function (instance) {
if (instance._flushProperties) {
instance._flushProperties(true);
}
}
};
}());
</script>