-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexmg-tbody.html
More file actions
618 lines (550 loc) · 17.1 KB
/
exmg-tbody.html
File metadata and controls
618 lines (550 loc) · 17.1 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../polymer/lib/elements/array-selector.html">
<dom-module id="exmg-tbody">
<template>
<style>
:host {
width: 100%;
}
.tr {
@apply --layout;
box-sizing: border-box;
border-bottom: 1px solid #dbdbdb;
@apply --exmg-tbody-tr;
}
.tr.selected {
background: #fff;
position: relative;
z-index: 20;
@apply --exmg-tbody-tr-selected;
}
.tr.selected:hover .td {
background: none;
}
.tr[hidden] {
display: none;
}
.tr .td.hover > span > * {
display: none;
}
.tr.selected .td.hover > * {
display: none;
}
.tr:hover .td {
background-color: #f5f5f5;
@apply --exmg-tbody-td-hover;
}
.tr:hover .td.hover > span > * {
display: inline-block;
@apply --exmg-tbody-td-hover-content;
}
.tr.expandable {
@apply --layout-vertical;
display: none;
padding: 72px 36px 24px;
position: relative;
margin-left: -12px;
margin-top: -50px;
z-index: 10;
width: calc(100% + 24px);
height: calc(100% + 48px);
background: #fff;
border: 1px solid #dbdbdb;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14),
0 1px 5px 0 rgba(0, 0, 0, 0.12),
0 3px 1px -2px rgba(0, 0, 0, 0.2);
@apply --exmg-tbody-tr-expandable;
}
.tr.expandable[aria-expanded] {
@apply --layout;
}
.tr.expandable > span {
@apply --layout;
@apply --layout-center;
@apply --exmg-tbody-tr-expandable-expanded;
}
.tr.expandable > span > * {
padding: 0 24px 0 0;
}
.td {
@apply --layout;
@apply --layout-center;
height: 48px;
box-sizing: border-box;
overflow: hidden;
padding: 0 6px;
@apply --exmg-tbody-td;
}
.td > span {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.td:first-child {
padding-left: 24px;
}
.td:last-child {
padding-right: 24px;
}
.flex {
@apply --layout-flex;
}
.flex-auto {
/* stretch and squish */
@apply --layout-flex-auto;
}
.flex-none {
/* fixed width */
@apply --layout-flex-none;
}
.flex-right {
@apply --layout;
@apply --layout-end-justified;
padding-right: 24px;
padding-left: 6px !important;
}
.collapsable {
display: none;
}
@media(min-width: 600px) {
.collapsable {
@apply --layout;
}
}
/* Should this be here or elsewhere? */
paper-button {
@apply --layout-self-end;
transition: all .2s linear;
font-size: 14px;
font-weight: 500;
opacity: .6;
}
paper-button:hover {
background: #ddd;
opacity: 1;
}
</style>
<slot></slot>
<!-- Array helper -->
<array-selector id="selector" items="{{items}}" selected="{{selectedItems}}" selected-item="{{selectedItem}}"></array-selector>
</template>
<script>
/**
* `exmg-tbody`
* Datatable
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class ExmgTbody extends Polymer.mixinBehaviors([Polymer.Templatizer], Polymer.Element) {
static get is() { return 'exmg-tbody'; }
static get properties() {
return {
/**
* An array containing items determining how many instances of the template
* to stamp and that that each template instance should bind to.
*/
items: {
type: Array
},
/**
* The name of the variable to add to the binding scope for the array
* element associated with a given template instance.
*/
as: {
type: String,
value: 'item'
},
/**
* The name of the variable to add to the binding scope to indicate
* if the row is selected.
*/
selectedAs: {
type: String,
value: 'selected'
},
/**
* The name of the variable to add to the binding scope with the index
* for the row.
*/
indexAs: {
type: String,
value: 'index'
},
/**
* When true, tapping a row will select the item, placing its data model
* in the set of selected items retrievable via the selection property.
*
* Note that tapping focusable elements within the list item will not
* result in selection, since they are presumed to have their * own action.
*/
selectionEnabled: {
type: Boolean,
value: false
},
/**
* When `multiSelection` is false, this is the currently selected item, or `null`
* if no item is selected.
*/
selectedItem: {
type: Object,
notify: true
},
/**
* When `multiSelection` is true, this is an array that contains the selected items.
*/
selectedItems: {
type: Object,
notify: true
},
/**
* When `true`, multiple items may be selected at once (in this case,
* `selected` is an array of currently selected items). When `false`,
* only one item may be selected at a time.
*/
multiSelection: {
type: Boolean,
value: false
},
/**
* When `true`, extra style information is set on the first column for correct display.
*/
checkboxUsed: {
type: Boolean,
value: false
}
};
}
static get observers() {
return [
'_itemsChanged(items.*)',
'_multiSelectionChanged(multiSelection)',
'_selectionEnabledChanged(selectionEnabled)'
]
}
_multiSelectionChanged(multiSelection) {
this.clearSelection();
this.$.selector.multi = multiSelection;
}
/**
* Add an event listener to `tap` if `selectionEnabled` is true,
* it will remove the listener otherwise.
*/
_selectionEnabledChanged(selectionEnabled) {
if(selectionEnabled) {
this._addTapListener();
} else {
this._removeTapListener();
}
}
_addTapListener() {
Polymer.Gestures.addListener(this, 'tap', this._selectionHandler);
}
_removeTapListener() {
Polymer.Gestures.removeListener(this, 'tap', this._selectionHandler);
}
/**
* Select an item from an event object.
*/
_selectionHandler(e) {
let m = e.path.find(entry => {
return entry.classList && entry.classList.contains('tr');
});
let model = this.modelForElement(m);
if (!model) {
return;
}
let modelTabIndex, activeElTabIndex;
let target = Polymer.dom(e).path[0];
let activeEl = Polymer.dom(this.domHost ? this.domHost.root : document).activeElement;
let physicalItem = this._physicalItems[model[this.indexAs]];
// Safari does not focus certain form controls via mouse
// https://bugs.webkit.org/show_bug.cgi?id=118043
if (target.localName === 'input' ||
target.localName === 'button' ||
target.localName === 'select') {
return;
}
// Set a temporary tabindex
modelTabIndex = model.tabIndex;
model.tabIndex = -100;
activeElTabIndex = activeEl ? activeEl.tabIndex : -1;
model.tabIndex = modelTabIndex;
// Only select the item if the tap wasn't on a focusable child
// or the element bound to `tabIndex`
if (activeEl && physicalItem !== activeEl && physicalItem.contains(activeEl) && activeElTabIndex !== SECRET_TABINDEX) {
return;
}
this.toggleSelectionForItem(model[this.as]);
}
disconnectedCallback() {
super.disconnectedCallback();
this._removeTapListener();
}
connectedCallback() {
super.connectedCallback();
if (this._physicalCount === 0) {
this._debounceRender(this._render);
}
}
_debounceRender(cb) {
this._renderDebouncer = Polymer.Debouncer.debounce(
this._renderDebouncer,
Polymer.Async.animationFrame,
cb.bind(this));
Polymer.enqueueDebouncer(this._renderDebouncer);
}
/**
* Renders the a new list.
*/
_render() {
if (!this.isAttached) {
return;
}
if (this._physicalCount === 0) {
this._increasePool(this.items.length);
} else {
if(this.items.length > this._physicalCount) {
this._increasePool(this.items.length - this._physicalCount);
} else {
this._update();
}
}
}
/**
* Called when the items have changed. That is, ressignments
* to `items`, splices or updates to a single item.
*/
_itemsChanged(change) {
if (change.path === 'items') {
this.items = this.items || [];
this._physicalCount = this._physicalCount || 0;
this._physicalItems = this._physicalItems || [];
this._debounceRender(this._render);
} else if (change.path === 'items.splices') {
change.value.indexSplices.forEach(function(splice) {
// deselect removed items
splice.removed.forEach(this._removeItem, this);
}, this);
this._debounceRender(this._render);
} else {
if(change.path !== 'items.length') {
this._forwardItemPath(change.path, change.value);
}
}
}
_removeItem(item) {
this.$.selector.deselect(item);
}
_forwardItemPath(path, value) {
path = path.slice(6); // 'items.'.length == 6
let dot = path.indexOf('.') + 1;
if (dot === 0) {
dot = path.length;
}
let idx = parseInt(path.substring(0, dot), 10);
let offscreenItem = this._offscreenFocusedItem;
let inst = this.modelForElement(this._physicalItems[idx]);
if (!inst || inst[this.indexAs] !== idx) {
return;
}
path = path.substring(dot);
path = this.as + (path ? '.' + path : '');
inst._setPendingPropertyOrPath(path, value, false, true);
inst._flushProperties && inst._flushProperties(true);
}
/**
* Creates a pool of DOM elements and attaches them to the local dom.
*
* @param {number} size Size of the pool
*/
_createPool(size) {
let physicalItems = new Array(size);
this._ensureTemplatized();
for (let i = 0; i < size; i++) {
let inst = this.stamp(null);
// First element child is item; Safari doesn't support children[0]
// on a doc fragment.
physicalItems[i] = inst.root.querySelector('*');
this.shadowRoot.appendChild(inst.root);
}
return physicalItems;
}
/**
* Increases the pool size.
*/
_increasePool(poolSize) {
// Concat arrays in place.
[].push.apply(this._physicalItems, this._createPool(poolSize));
this._physicalCount = this._physicalItems.length;
this._update();
}
/**
* Templetizes the user template.
*/
_ensureTemplatized() {
// Check if already templatized
if (!this.ctor) {
// Template instance props that should be excluded from forwarding
let props = {};
props.__key__ = true;
props[this.as] = true;
props[this.indexAs] = true;
props[this.selectedAs] = true;
props.tabIndex = true;
this._instanceProps = props;
this._userTemplate = this.querySelector('template');
if (this._userTemplate) {
this.templatize(this._userTemplate);
} else {
console.warn('exm-tbody requires a template to be provided in light-dom');
}
}
}
/**
* Update the list of items, starting from the `_virtualStart` item.
* @param {!Array<number>=} itemSet
*/
_update(itemSet) {
if ((itemSet && itemSet.length === 0) || this._physicalCount === 0) {
return;
}
this._assignModels(itemSet);
}
/**
* Assigns the data models to a given set of items.
* @param {!Array<number>=} itemSet
*/
_assignModels(itemSet) {
let count = 0;
this._physicalItems.forEach((el) => {
let inst = this.modelForElement(el);
let item = this.items[count];
if (item != null && item !== undefined) {
//inst.__key__ = this._collection ? this._collection.getKey(item) : null;
this._forwardProperty(inst, this.as, item);
this._forwardProperty(inst, this.selectedAs, this.$.selector.isSelected(item));
this._forwardProperty(inst, this.indexAs, count);
this._forwardProperty(inst, 'tabIndex', -1);
inst._flushProperties && inst._flushProperties(true);
el.removeAttribute('hidden');
} else {
el.setAttribute('hidden', '');
}
count++;
});
}
_forwardProperty(inst, name, value) {
inst._setPendingProperty(name, value);
}
/* Templatizer bindings for v2 */
_forwardHostPropV2(prop, value) {
(this._physicalItems || [])
.concat([this._offscreenFocusedItem, this._focusBackfillItem])
.forEach(function(item) {
if (item) {
this.modelForElement(item).forwardHostProp(prop, value);
}
}, this);
}
_notifyInstancePropV2(inst, prop, value) {
if (Polymer.Path.matches(this.as, prop)) {
let idx = inst[this.indexAs];
if (prop == this.as) {
this.items[idx] = value;
}
this.notifyPath(Polymer.Path.translate(this.as, 'items.' + idx, prop), value);
}
}
/**
* Selects the given item.
*
* @method selectItem
* @param {Object} item The item instance.
*/
selectItem(item) {
return this.selectIndex(this.items.indexOf(item));
}
/**
* Selects the item at the given index in the items array.
*
* @method selectIndex
* @param {Object} index The item instance.
*/
selectIndex(index) {
if (index < 0 || index >= this.items) {
return;
}
if (!this.multiSelection && this.selectedItem) {
this.clearSelection();
}
let model = this.modelForElement(this._physicalItems[index]);
if (model) {
model[this.selectedAs] = true;
}
this.$.selector.selectIndex(index);
}
/**
* Clears the current selection in the list.
*
* @method clearSelection
*/
clearSelection() {
if(this._physicalItems) {
this._physicalItems.map((el) => {
this.modelForElement(el)[this.selectedAs] = false;
});
}
this.$.selector.clearSelection();
}
/**
* Deselects the given item.
*
* @method deselect
* @param {Object} item The item instance.
*/
deselectItem(item) {
return this.deselectIndex(this.items.indexOf(item));
}
/**
* Deselects the item at the given index in the items array.
*
* @method deselectIndex
* @param {number} index The index of the item in the items array.
*/
deselectIndex(index) {
if (index < 0 || index >= this.items) {
return;
}
let model = this.modelForElement(this._physicalItems[index]);
model[this.selectedAs] = false;
this.$.selector.deselectIndex(index);
}
/**
* Selects or deselects a given item depending on whether the item
* has already been selected.
*
* @method toggleSelectionForItem
* @param {Object} item The item object.
*/
toggleSelectionForItem(item) {
return this.toggleSelectionForIndex(this.items.indexOf(item));
}
/**
* Selects or deselects the item at the given index in the items array
* depending on whether the item has already been selected.
*
* @method toggleSelectionForIndex
* @param {Object} index The index of the item in the items array.
*/
toggleSelectionForIndex(index) {
let isSelected = this.$.selector.isIndexSelected
? this.$.selector.isIndexSelected(index) : this.$.selector.isSelected(this.items[index]);
isSelected ? this.deselectIndex(index) : this.selectIndex(index);
}
}
window.customElements.define(ExmgTbody.is, ExmgTbody);
</script>
</dom-module>