-
Notifications
You must be signed in to change notification settings - Fork 3
/
prism-data-table.html
465 lines (429 loc) · 18.1 KB
/
prism-data-table.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<link rel="import" href="../polymer/polymer.html" />
<link rel="import" href="../polymer/lib/elements/dom-repeat.html" />
<link rel="import" href="../polymer/lib/elements/dom-if.html" />
<link rel="import" href="prism-table-import.html" />
<link rel="import" href="prism-table-selector.html" />
<dom-module id="prism-data-table">
<template>
<style>
a {
color: inherit;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
<prism-table-selector id="selector" disabled$="[[disableSelector]]" multiple$="[[multiple]]" allow-checkbox$="[[allowCheckbox]]"
selected="{{selected}}" selected-items="{{selectedItems}}" on-change="_selectionChanged">
<prism-table responsive$="[[responsive]]" hover$="[[hover]]" shadow$="[[shadow]]" bordered$="[[bordered]]" striped$="[[striped]]"
condensed$="[[condensed]]">
<prism-thead>
<prism-tr>
<template is="dom-if" if="[[allowCheckbox]]">
<prism-th> </prism-th>
</template>
<template is="dom-repeat" items="[[columns]]" as="column">
<!-- Sort 排序事件 -->
<prism-th on-click="_onSort">[[column.label]]</prism-th>
</template>
</prism-tr>
</prism-thead>
<prism-tbody>
<template is="dom-repeat" items="[[rows]]" as="row">
<prism-tr key="[[_getKeyValue(row)]]">
<template is="dom-if" if="[[allowCheckbox]]">
<prism-td title="Selection">
<prism-table-checkbox></prism-table-checkbox>
</prism-td>
</template>
<template is="dom-repeat" items="[[columns]]" as="column">
<!-- FIXME firefox css 套用動態元件不正常 -->
<prism-td title="[[column.label]]" content="[[_content(column, row)]]" allow-html="[[column.allowHtml]]" max-length="[[column.maxLength]]"></prism-td>
</template>
</prism-tr>
</template>
</prism-tbody>
</prism-table>
</prism-table-selector>
</template>
<script>
'use strict';
/**
* `<prism-data-table>` is a data table component use `<prism-table>` and `<prism-table-selector>`
*
* # Usage
*
* Place `<prism-data-table>` in your application
*
* ```html
* <prism-data-table disable-selector
* hover
* shadow
* key-names="email"
* selected="{{value}}"
* selected-items="{{items}}"
* headers="[[header]]"
* rows="[[source]]"></prism-data-table>
* ```
*
* Config columns and how `<prism-data-table>` should render
*
* ```
* this.set('header', [
* {name: 'id', label: 'ID'},
* {name: 'name', label: 'Name'},
* {
* name: 'email', label: 'Email',
* allowHtml: true,
* render: 'template', template: `<a href="mailto:#{email}">#{email}</a>`
* },
* {
* name: 'gender', label: 'Gender',
* render: 'map', map: {M: 'Male', F: 'Female'}
* },
* {
* name: 'disabled', label: 'Status',
* render: 'state', trueText: 'Disabled', falseText: 'Enabled'
* },
* {
* name: 'created', label: 'Created',
* render: 'transform', transform: 'formatDate'
* }
* ])
* ```
*
* @memberof Prhythm
* @element prism-data-table
* @customElement
* @polymer
* @demo demo/data-table.html Basic prism-data-table Demo
*/
class PrismDataTable extends PrismTableSelectorBehavior(PrismTableBehavior(Polymer.Element)) {
static get is() {
return 'prism-data-table';
}
static get properties() {
return {
/**
* Disable selection
*/
disableSelector: {
type: Boolean,
value: false
},
/**
* Define the key names of identity object,
* Key name could be seperated by `,` symbol,
* such as `category.id,product.area`
*/
keyNames: String,
/**
* Column information and configuration
*
* ```javascript
* {
* "name": "id", // Column identity, works as variable name for template content
* "label": "代號", // Column header label, this value display on table header
* "allowHtml": true, // Allow html content, optional
* "render": "template", // Render type, optional, available values are: template/map/transform/state
* "maxLength": 100, // Max text length cell renders, optional
* "template": `<a href="/path/#{param}">#{text}</a>`, // Works when `render` is set as `tempate`, optional
* // tempate allow use variable inside
* "map": {}, // Works when `render` is set as `map`, optional
* // this will map value from row data or column inforamtion
* "trueText": "真", // Works when `render` is set as `state`, optional
* "falseText": "敆" // trueText map when value is true, otherwise falseText
* }
* ```
*/
headers: {
type: Array,
value: []
},
/**
* Table data
*/
rows: {
type: Array,
value: []
}
}
}
static get observers() {
return [
'_columnsChanged(headers.*)'
]
}
/**
* Render table rows when column configuration changes.
*
* @param {Object} headers Column information
* @return {Void}
*/
_columnsChanged(headers) {
let columns = headers.value;
// Rebuild column key namd label
columns = columns.map((item) => {
let out = {
// Define key for data
key: item.name || item.label || item,
// Define text for column display
label: item.label || item,
};
return Object.assign(out, item);
});
this.set('columns', Array.from(columns));
}
/**
* Dispatch change event from `<prism-table-selector>`
*/
_selectionChanged(e) {
// Dispath event
this.dispatchEvent(new CustomEvent('change', { detail: e.detail }));
}
/**
* Toggle selected value, this is a wrapped method for `<prism-table-selector>`
*
* @param {Object} value Selected value
* @return {Void}
*/
toggleSelected(value) {
this.$.selector.toggleSelected(value);
}
/**
* Check value exists in current selection, this is a wrapped method for `<prism-table-selector>`
*
* @param {Object} value Selected value
* @return {Void}
*/
isSelected(value) {
this.$.selector.isSelected(value);
}
/**
* Append selected value into current selection, this is a wrapped method for `<prism-table-selector>`
*
* @param {Object} value Selected value
* @return {Void}
*/
addSelected(value) {
this.$.selector.addSelected(value);
}
/**
* Remove selected value from current selection, this is a wrapped method for `<prism-table-selector>`
*
* @param {Objec} value Selected value
* @return {Void}
*/
removeSelected(value) {
this.$.selector.removeSelected(value);
}
/**
* Clear selection, this is a wrapped method for `<prism-table-selector>`
*
* @return {Void}
*/
clearSelected() {
this.$.selector.clearSelected();
}
/**
* Change selection for selector, this is a wrapped method for `<prism-table-selector>`
*
* @param {Array} selection Selected values
* @return {Void}
*/
changeSelected(selection) {
this.$.selector.changeSelected(selection);
}
/**
* Generate identity object according keyNames property
*
* @param {Object} row Data row
* @return {Object} identity object
*/
_getKeyValue(row) {
let name = this.keyNames;
if (name) {
// Identities is seperated by ','
let segment = name.split(',');
if (segment.length === 1) {
// Single identity
return this._routeProperty(row, name);
} else {
// Multiple identities, compose as a single object
let result = {};
for (let i = 0; i < segment.length; i++) {
// Create and merge indentity object
result = Object.assign(
result,
this._createIdentity(segment[i], this._routeProperty(row, segment[i])));
}
return result;
}
} else {
// Return raw row if key-names attribute is undefined
return row;
}
}
/**
* Create a object instance with path and value.
* This is used for data comparison for selected rows.
*
* ```
* // 'person.child.firstName' = 'Bruce'
* { 'persion': { 'child': { 'firstName': 'Bruce' } } }
* ```
*
* @param {String} path 路徑。
* @param {*} value 最後的屬性值。
* @return {Object}
*/
_createIdentity(path, value) {
// Root instance that to be created
let obj = {};
// Instance for processing
let pro = obj;
path = path.split('.');
for (let i = 0; i < path.length; i++) {
if (i === path.length - 1) {
// Fill value if meet last node
pro[path[i]] = value;
} else {
// Create sub property object
pro[path[i]] = {};
pro = pro[path[i]];
}
}
return obj;
}
/**
* Get value from object instance via path
*
* @param {Object} obj Object value for search
* @param {String} path Object path
* @return {*}
*/
_routeProperty(obj, path) {
path = path.split('.');
for (let i = 0; i < path.length; i++) {
if (path[i] in obj) {
// Drill down to sub property
obj = obj[path[i]];
} else {
return undefined;
}
}
return obj;
}
/**
* Get cell content via column configuration
*
* @param {Object} column Column information
* @param {Object} row Row data
* @return {*} Cell content
*/
_content(column, row) {
switch (column.render || '') {
case 'template':
return this._render(column.template, row, column) || row[column.key] || '';
case 'map':
return (column.map || {})[row[column.key]] || row[column.key] || '';
case 'state':
return (row[column.key] ? column.trueText : column.falseText) || row[column.key] || '';
case 'transform':
return this._transform(row[column.key], row, column) || row[column.key] || '';
default:
// Output text
return row[column.key] || '';
}
}
/**
* Render cell content via template.
* Vairable in template will be replaced from row data or column
* information sequencely, but white space if there is no matched variable name
*
* @param {String} template Text content or html fragment
* @param {Object} row Row data
* @param {Object} column Column information
* @returns {*}
*/
_render(template, row, column) {
if (!template) return null;
let pattern = /#\{([^}]+?)}/;
row = row || {};
column = column || {};
let matcher;
while (matcher = pattern.exec(template)) {
let holder = matcher[0];
let name = matcher[1];
if (name in row) {
template = template.replace(holder, row[name]);
} else {
template = template.replace(holder, column[name] || '');
}
}
return template;
}
/**
* Transform value via customized method
*
* @param {Object} value Source value
* @param {Object} row Row data
* @param {Object} column Column infomration
* @returns {*}
*/
_transform(value, row, column) {
if (!column.transform) return value;
let host = this.__getMethodHost();
if (!host) return value;
// Get function instance from shadow host
let fn = host[column.transform];
if (!fn || typeof (fn) !== 'function') return value;
return fn.call({}, value, row, column);
}
__getMethodHost() {
// Technically this should be the owner of the outermost template.
// In shadow dom, this is always getRootNode().host, but we can
// approximate this via cooperation with our dataHost always setting
// `_methodHost` as long as there were bindings (or id's) on this
// instance causing it to get a dataHost.
return this.__dataHost._methodHost || this.__dataHost;
}
/**
* Sort event
*
* @param {Event} e Event object
* @return {Void}
*/
_onSort(e) {
// Get column index
let cell = e.path[0];
let row = cell.parentElement;
let index = Array.prototype.findIndex.call(row.children, (item) => item === cell);
let column = this.columns[index - 1];
if (this.get('sortIndex') === index) {
// Set direction
switch (this.get('sortDirection')) {
default:
case 'desc':
this.set('sortDirection', 'asc');
break;
case 'asc':
this.set('sortDirection', 'desc');
break;
}
} else {
this.set('sortDirection', 'asc');
}
this.set('sortIndex', index);
// Fire sort event
this.dispatchEvent(new CustomEvent('sort', {
bubble: true,
compose: true,
detail: Object.assign({ direction: this.get('sortDirection') }, column)
}));
}
}
customElements.define(PrismDataTable.is, PrismDataTable);
</script>
</dom-module>