-
Notifications
You must be signed in to change notification settings - Fork 5
/
cosmoz-omnitable-repeater-mixin.html
273 lines (237 loc) · 7.03 KB
/
cosmoz-omnitable-repeater-mixin.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
<link rel="import" href="../shadycss/apply-shim.html">
<link rel="import" href="../polymer/lib/utils/mixin.html"/>
<script>
(() => {
'use strict';
window.Cosmoz = window.Cosmoz || {};
/**
* Allows manipulation of elements.
* @callback OmnitableRepeaterMixin~forEachElementCallback
* @param {HTMLElement} element
* @param {OmnitableColumn} column
* @param {TemplateInstance} instance
* @return {void}
*/
const editableRegex = /columns\.(\d*)\.editable/i;
/**
*
* @polymer
* @mixinFunction
*/
Cosmoz.OmnitableRepeaterMixin = Polymer.dedupingMixin(base => class extends base {
static get properties() {
return {
columns: {
type: Array
},
groupOnColumn: {
type: Object,
observer: '_groupOnColumnChanged'
}
};
}
constructor() {
super();
this._elements = [];
this._columnsObserver = null;
}
/**
* Adds an observer to render the cells when the columns are changed.
* @return {void}
*/
trackColumns() {
if (this._columnsObserver) {
throw new Error('The columns are already tracked.');
}
this._createMethodObserver('_columnsChanged(columns.*)');
// HACK we have to muck around with the property effect references because
// _createMethodObserver does not provide any reference to the property
// effect so we can easily remove it
this._columnsObserver = this[this.PROPERTY_EFFECT_TYPES.OBSERVE]['columns']
.find(fx => fx.info.methodName === '_columnsChanged');
}
/**
* Stops reacting to column changes.
* @return {void}
*/
stopTrackingColumns() {
if (!this._columnsObserver) {
throw new Error('The columns are not tracked.');
}
this._removePropertyEffect('columns', this.PROPERTY_EFFECT_TYPES.OBSERVE, this._columnsObserver);
this._columnsObserver = null;
}
/**
* Renders all cells.
* @return {void}
*/
renderCells() {
this._addElements(0, this.columns.length);
}
/**
* Destroys all cells.
* @return {void}
*/
destroyCells() {
this._removeElements(0, this.columns);
}
/**
* Forwards a property change to all cells.
* @param {String} property the property
* @param {any} value the new value
* @return {void}
*/
forwardChange(property, value) {
this.forwardPathChange({path: property, value});
}
/**
* Forwards a path change to all cells.
* @param {Object} changeRecord the change record
* @return {void}
*/
forwardPathChange(changeRecord) {
this._elements.forEach(element =>
this._forwardNotifyPath(element.__instance, changeRecord.path, changeRecord.value, true)
);
}
/**
* Runs a callback on each element.
* @param {OmnitableRepeaterMixin~forEachElementCallback} callback the callback
* @return {void}
*/
forEachElement(callback) {
this._elements.forEach(element => callback(element, element.__column, element.__instance));
}
/* eslint-disable no-empty-function, no-unused-vars */
/**
* The type of element to be repeated.
* Must be defined in implementors.
* @return {String} The type of element to be repeated.
*/
get _elementType() {
return null;
}
/**
* Slot name assigned to the repeated elements.
* Must be defined in implementors.
* @return {String} Slot name assigned to the repeated elements.
*/
get _slotName() {
return null;
}
/**
* Get a template instance for the specified column
* Must be defined in implementors.
* @abstract
* @param {OmnitableColumnMixin} column - The column.
* @return {Object} - The instance.
*/
_getTemplateInstance(column) {}
/* eslint-enable no-empty-function, no-unused-vars */
/**
* Configure a newly created repeated element
*
* @param {HTMLElement} element the root cell element
* @param {OmnitableColumnMixin} column the column
* @param {TemplateInstance} instance the template instance
* @return {void}
*/
_configureElement(element, column, instance) {
element.__instance = instance;
element.__column = column;
if (column === this.groupOnColumn) {
element.setAttribute('hidden', true);
}
element.setAttribute('slot', this._slotName);
}
_columnsChanged({ path, base, value }) {
const reMatch = editableRegex.exec(path);
if (path === 'columns') {
if (this._elements && this._elements.length) {
const removedColumns = this._elements.map(element => {
return element.__column;
});
this._removeElements(0, removedColumns);
} else {
this._elements = [];
}
this._addElements(0, this.columns.length);
} else if (path === 'columns.splices') {
this._renderSplices(value.indexSplices);
} else if (reMatch) {
const index = reMatch[1] >> 0,
column = base[index];
this._removeElements(index, [column]);
this._addElements(index, 1);
} else if (path !== 'columns.length') {
// column property change
console.warn('column property change', path);
}
}
_renderSplices(splices) {
splices.forEach(splice => {
if (splice.removed.length) {
this._removeElements(splice.index, splice.removed);
}
if (splice.addedCount > 0) {
this._addElements(splice.index, splice.addedCount);
}
});
}
_addElements(start, count) {
const end = start + count;
for (let i = start; i < end; i++) {
const element = document.createElement(this._elementType),
column = this.columns[i],
instance = this._getTemplateInstance(column);
this._configureElement(element, column, instance);
element.appendChild(instance.root);
if (i < this._elements.length) {
this.insertBefore(element, this._elements[i]);
} else {
this.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(start, removedColumns) {
this._elements
.splice(start, removedColumns.length)
.forEach(element => {
element.__column.recycleInstance(element.__instance);
element.__instance = element.__column = element.column = null;
this.removeChild(element);
});
}
_groupOnColumnChanged(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', '');
}
}
}
_forwardNotifyPath(instance, path, value, flush = false) {
instance._setPendingPropertyOrPath(path, value, false, true);
if (flush) {
instance._flushProperties(true);
}
}
});
})();
</script>