-
Notifications
You must be signed in to change notification settings - Fork 155
/
vaadin-grid.html
549 lines (489 loc) · 19.8 KB
/
vaadin-grid.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
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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../vaadin-themable-mixin/vaadin-themable-mixin.html">
<link rel="import" href="vaadin-grid-scroller.html">
<link rel="import" href="vaadin-grid-dynamic-columns-mixin.html">
<link rel="import" href="vaadin-grid-data-provider-mixin.html">
<link rel="import" href="vaadin-grid-array-data-provider-mixin.html">
<link rel="import" href="vaadin-grid-cell-click-mixin.html">
<link rel="import" href="vaadin-grid-active-item-mixin.html">
<link rel="import" href="vaadin-grid-scroll-mixin.html">
<link rel="import" href="vaadin-grid-selection-mixin.html">
<link rel="import" href="vaadin-grid-sort-mixin.html">
<link rel="import" href="vaadin-grid-row-details-mixin.html">
<link rel="import" href="vaadin-grid-keyboard-navigation-mixin.html">
<link rel="import" href="vaadin-grid-a11y-mixin.html">
<link rel="import" href="vaadin-grid-filter-mixin.html">
<link rel="import" href="vaadin-grid-column-reordering-mixin.html">
<link rel="import" href="vaadin-grid-column-resizing-mixin.html">
<link rel="import" href="vaadin-grid-column.html">
<link rel="import" href="vaadin-grid-outer-scroller.html">
<link rel="import" href="vaadin-grid-styles.html">
<dom-module id="vaadin-grid">
<template>
<style include="vaadin-grid-styles"></style>
<div id="scroller" role="application" no-scrollbars$="[[!_scrollbarWidth]]" safari$="[[_safari]]" ios$="[[_ios]]" loading$=[[_loading]] edge$="[[_edge]]">
<table id="table" role="grid" aria-multiselectable="true">
<caption id="fixedsizer" class="sizer" part="row"></caption>
<thead id="header" role="rowgroup"></thead>
<tbody id="items" role="rowgroup"></tbody>
<tfoot id="footer" role="rowgroup"></tfoot>
</table>
<div part="reorder-ghost"></div>
<vaadin-grid-outer-scroller id="outerscroller"
scroll-target="[[scrollTarget]]"
scroll-handler="[[_this]]">
<div id="outersizer" class="sizer" part="row"></div>
</vaadin-grid-outer-scroller>
</div>
<!-- The template needs at least one slot or else shady doesn't distribute -->
<slot name="nodistribute"></slot>
<div id="focusexit" tabindex="0"></div>
</template>
</dom-module>
<script>
if (!Polymer.Element) {
throw new Error(`Unexpected Polymer version ${Polymer.version} is used, expected v2.0.0 or later.`);
}
{
/**
* `<vaadin-grid>` is a free, high quality data grid / data table Polymer element.
*
* ### Quick Start
*
* - Use the [`<vaadin-grid-column>`](#/elements/vaadin-grid-column) element to configure the grid columns.
*
* - Then assign an array to the [`items`](#/elements/vaadin-grid#property-items) property to visualize your data.
*
* #### Example:
* ```html
* <vaadin-grid items='[{"name": "John", "surname": "Lennon", "role": "singer"},
* {"name": "Ringo", "surname": "Starr", "role": "drums"}]'>
* <vaadin-grid-column>
* <template class="header">Name</template>
* <template>[[item.name]]</template>
* </vaadin-grid-column>
* <vaadin-grid-column>
* <template class="header">Surname</template>
* <template>[[item.surname]]</template>
* </vaadin-grid-column>
* <vaadin-grid-column>
* <template class="header">Role</template>
* <template>[[item.role]]</template>
* </vaadin-grid-column>
* </vaadin-grid>
* ```
*
* - The following helper elements can be used for further customization:
* - [`<vaadin-grid-column-group>`](#/elements/vaadin-grid-column-group)
* - [`<vaadin-grid-filter>`](#/elements/vaadin-grid-filter)
* - [`<vaadin-grid-sorter>`](#/elements/vaadin-grid-sorter)
* - [`<vaadin-grid-selection-column>`](#/elements/vaadin-grid-selection-column)
*
* __Note that the helper elements must be explicitly imported.__
* If you want to import everything at once you can use the `all-imports.html` bundle.
*
* - A column template can be decorated with one the following class names to specify its purpose
* - "header": Marks a header template
* - "footer": Marks a footer template
* - "row-details": Marks a row details template
*
* - The following built-in template variables can be bound to inside the column templates
* - "index": Number representing the row index
* - "item" and it's sub-properties: Data object (provided by a data provider / items array)
* - "selected": True if the item is selected (can be two-way bound)
* - "expanded": True if the item is expanded for row details (can be two-way bound)
*
* ### Lazy Loading with Function Data Provider
*
* In addition to assigning an array to the items property, you can alternatively
* provide the `<vaadin-grid>` data through the
* [`dataProvider`](#/elements/vaadin-grid#property-dataProvider) function property.
* The `<vaadin-grid>` calls this function lazily, only when it needs more data
* to be displayed.
*
* See the [`dataProvider`](#/elements/vaadin-grid#property-dataProvider) in
* the API reference below for the detailed data provider arguments description,
* and the “Assigning Data” page in the demos.
*
* __Note that when using function data providers, `size` always needs to be
* set manually.__
*
* ```javascript
* grid.size = 200; // The total number of items
* grid.dataProvider = function(params, callback) {
* var url = 'https://api.example/data' +
* '?page=' + params.page + // the requested page index
* '&per_page=' + params.pageSize; // number of items on the page
* var xhr = new XMLHttpRequest();
* xhr.onload = function() {
* var response = JSON.parse(xhr.responseText);
* callback(response.employees);
* };
* xhr.open('GET', url, true);
* xhr.send();
* };
* ```
*
* ### Styling
*
* The following shadow DOM parts are available for styling:
*
* Part name | Description
* ----------------|----------------
* `row` | Row in the internal table
* `cell` | Cell in the internal table
* `header-cell` | Header cell in the internal table
* `body-cell` | Body cell in the internal table
* `footer-cell` | Footer cell in the internal table
* `details-cell` | Row details cell in the internal table
* `resize-handle` | Handle for resizing the columns
* `reorder-ghost` | Ghost element of the header cell being dragged
*
* The following state attributes are available for styling:
*
* Attribute | Description | Part name
* -------------|-------------|------------
* `loading` | Set when the grid is loading data from data provider | :host
* `interacting` | Keyboad navigation in interaction mode | :host
* `navigating` | Keyboad navigation in navigation mode | :host
* `reordering` | Set when the grid's columns are being reordered | :host
* `reorder-status` | Reflects the status of a cell while columns are being reordered | cell
* `frozen` | Frozen cell | cell
* `last-frozen` | Last frozen cell | cell
* `last-column` | Last visible cell on a row | cell
* `selected` | Selected row | row
* `odd` | Odd row | row
*
*
* @memberof Vaadin
* @mixes Vaadin.ThemableMixin
* @mixes Vaadin.Grid.DataProviderMixin
* @mixes Vaadin.Grid.ArrayDataProviderMixin
* @mixes Vaadin.Grid.DynamicColumnsMixin
* @mixes Vaadin.Grid.CellClickMixin
* @mixes Vaadin.Grid.ActiveItemMixin
* @mixes Vaadin.Grid.ScrollMixin
* @mixes Vaadin.Grid.SelectionMixin
* @mixes Vaadin.Grid.SortMixin
* @mixes Vaadin.Grid.RowDetailsMixin
* @mixes Vaadin.Grid.KeyboardNavigationMixin
* @mixes Vaadin.Grid.FilterMixin
* @mixes Vaadin.Grid.ColumnReorderingMixin
* @mixes Vaadin.Grid.ColumnResizingMixin
* @demo demo/index.html
*/
class GridElement extends
Vaadin.ThemableMixin(
Vaadin.Grid.DataProviderMixin(
Vaadin.Grid.ArrayDataProviderMixin(
Vaadin.Grid.DynamicColumnsMixin(
Vaadin.Grid.CellClickMixin(
Vaadin.Grid.ActiveItemMixin(
Vaadin.Grid.ScrollMixin(
Vaadin.Grid.SelectionMixin(
Vaadin.Grid.SortMixin(
Vaadin.Grid.RowDetailsMixin(
Vaadin.Grid.KeyboardNavigationMixin(
Vaadin.Grid.A11yMixin(
Vaadin.Grid.FilterMixin(
Vaadin.Grid.ColumnReorderingMixin(
Vaadin.Grid.ColumnResizingMixin(
Vaadin.Grid.ScrollerElement))))))))))))))) {
static get is() {
return 'vaadin-grid';
}
static get observers() {
return [
'_columnTreeChanged(_columnTree, _columnTree.*)'
];
}
static get properties() {
return {
_safari: {
type: Boolean,
value: /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
},
_this: {
type: Object,
value: function() {
return this;
}
},
_ios: {
type: Boolean,
value: function() {
return navigator.userAgent.match(/iP(?:hone|ad;(?: U;)? CPU) OS (\d+)/);
}
},
_edge: {
type: Boolean,
value: function() {
return typeof CSS !== 'undefined' && CSS.supports('(-ms-ime-align:auto)');
}
},
_ie: {
type: Boolean,
value: function() {
return !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));
}
}
};
}
constructor() {
super();
this.addEventListener('animationend', this._onAnimationEnd);
}
_createScrollerRows(count) {
const rows = [];
for (var i = 0; i < count; i++) {
const row = document.createElement('tr');
row.setAttribute('part', 'row');
row.setAttribute('role', 'row');
if (this._columnTree) {
this._updateRow(row, this._columnTree[this._columnTree.length - 1], 'body', false, true);
}
rows.push(row);
}
if (this._columnTree) {
this._columnTree[this._columnTree.length - 1].forEach(c => c.notifyPath('_cells.*', c._cells));
}
Polymer.RenderStatus.beforeNextRender(this, () => {
this._updateLastColumn();
this._resetKeyboardNavigation();
});
return rows;
}
_getRowTarget() {
return this.$.items;
}
_createCell(tagName) {
const contentId = this._contentIndex = this._contentIndex + 1 || 0;
const slotName = 'vaadin-grid-cell-content-' + contentId;
const cellContent = document.createElement('vaadin-grid-cell-content');
cellContent.setAttribute('slot', slotName);
const cell = document.createElement(tagName);
cell.id = slotName.replace('-content-', '-');
cell.setAttribute('tabindex', '-1');
cell.setAttribute('role', tagName === 'td' ? 'gridcell' : 'columnheader');
const slot = document.createElement('slot');
slot.setAttribute('name', slotName);
cell.appendChild(slot);
cell._content = cellContent;
// With native Shadow DOM, mousedown on slotted element does not focus
// focusable slot wrapper, that is why cells are not focused with
// mousedown. Workaround: make cellContent focusable and delegate
// focus to assigned cell.
cellContent.setAttribute('tabindex', '-1');
cellContent.addEventListener('focusin', (e) => {
if (e.composedPath()[0] === cellContent) {
e.stopPropagation();
}
this._onMouseCellFocus(e);
});
return cell;
}
_updateRow(row, columns, section, isColumnRow, noNotify) {
section = section || 'body';
const contentsFragment = document.createDocumentFragment();
Array.from(row.children).forEach(cell => cell._vacant = true);
row.innerHTML = '';
if (row.id !== 'outersizer' && row.id !== 'fixedsizer') {
row.hidden = true;
}
columns.forEach((column, index) => {
let cell;
if (section === 'body') {
// Body
column._cells = column._cells || [];
cell = column._cells.filter(cell => cell._vacant)[0];
if (!cell) {
cell = this._createCell('td');
column._cells.push(cell);
}
cell.setAttribute('part', 'cell body-cell');
row.appendChild(cell);
if (index === columns.length - 1 && this._rowDetailsTemplate) {
// Add details cell as last cell to body rows
this._detailsCells = this._detailsCells || [];
const detailsCell = this._detailsCells.filter(cell => cell._vacant)[0] || this._createCell('td');
if (this._detailsCells.indexOf(detailsCell) === -1) {
this._detailsCells.push(detailsCell);
}
if (!detailsCell._content.parentElement) {
contentsFragment.appendChild(detailsCell._content);
}
this._configureDetailsCell(detailsCell);
row.appendChild(detailsCell);
this._a11ySetRowDetailsCell(row, detailsCell);
detailsCell._vacant = false;
}
if (column.notifyPath && !noNotify) {
column.notifyPath('_cells.*', column._cells);
}
} else {
// Header & footer
const tagName = section === 'header' ? 'th' : 'td';
if (isColumnRow || column.localName === 'vaadin-grid-column-group') {
cell = column[`_${section}Cell`] || this._createCell(tagName);
row.appendChild(cell);
if (cell._instance) {
row.hidden = false;
}
column[`_${section}Cell`] = cell;
} else {
column._emptyCells = column._emptyCells || [];
cell = column._emptyCells.filter(cell => cell._vacant)[0] || this._createCell(tagName);
row.appendChild(cell);
if (column._emptyCells.indexOf(cell) === -1) {
column._emptyCells.push(cell);
}
}
cell.setAttribute('part', `cell ${section}-cell`);
}
if (!cell._content.parentElement) {
contentsFragment.appendChild(cell._content);
}
cell._vacant = false;
cell._column = column;
});
// Might be empty if only cache was used
this.appendChild(contentsFragment);
this._frozenCellsChanged();
this._updateLastColumn();
}
_updateScrollerItem(row, index) {
this._preventScrollerRotatingCellFocus(row, index);
if (!this._columnTree) {
return;
}
Array.from(row.children).forEach(cell => {
if (cell._instance) {
cell._instance.index = index;
}
});
this._toggleAttribute('odd', index % 2, row);
this._a11yUpdateRowRowindex(row, index);
this._getItem(index, row);
}
_columnTreeChanged(columnTree, splices) {
Array.from(this.$.items.children).forEach(row => this._updateRow(row, columnTree[columnTree.length - 1]));
while (this.$.header.children.length < columnTree.length) {
const headerRow = document.createElement('tr');
headerRow.setAttribute('part', 'row');
headerRow.setAttribute('role', 'row');
this.$.header.appendChild(headerRow);
const footerRow = document.createElement('tr');
footerRow.setAttribute('part', 'row');
footerRow.setAttribute('role', 'row');
this.$.footer.appendChild(footerRow);
}
while (this.$.header.children.length > columnTree.length) {
this.$.header.removeChild(this.$.header.firstElementChild);
this.$.footer.removeChild(this.$.footer.firstElementChild);
}
Array.from(this.$.header.children)
.forEach((headerRow, index) => this._updateRow(headerRow, columnTree[index], 'header', index === columnTree.length - 1));
Array.from(this.$.footer.children)
.forEach((footerRow, index) => this._updateRow(footerRow, columnTree[columnTree.length - 1 - index], 'footer', index === 0));
// Sizer rows
this._updateRow(this.$.outersizer, columnTree[columnTree.length - 1]);
this._updateRow(this.$.fixedsizer, columnTree[columnTree.length - 1]);
this._resizeHandler();
this._frozenCellsChanged();
this._updateLastColumn();
this._resetKeyboardNavigation();
this._a11yUpdateHeaderRows();
this._a11yUpdateFooterRows();
}
_updateItem(row, item) {
// row.style.minHeight = item ? '' : this.$.scroller._physicalAverage + 'px';
row._item = item;
Array.from(row.children).forEach(cell => {
if (cell._instance) {
if (cell._instance.item === item) {
// Force data system to pick up subproperty changes
cell._instance.item = {};
}
cell._instance.item = item;
cell._instance.index = row.index;
}
});
this._toggleRowSelected(row, this._isSelected(item));
this._toggleRowExpanded(row, this._rowDetailsTemplate && this._isExpanded(item));
if (this._rowDetailsTemplate) {
this._toggleDetailsCell(row, item);
}
}
_toggleRowExpanded(row, expanded) {
Array.from(row.children).forEach(cell => {
if (cell._instance) {
cell._instance.__expanded__ = expanded;
cell._instance.expanded = expanded;
}
});
this._a11yUpdateRowExpanded(row, expanded);
}
_toggleRowSelected(row, selected) {
Array.from(row.children).forEach(cell => {
if (cell._instance) {
cell._instance.__selected__ = selected;
cell._instance.selected = selected;
}
});
this._toggleAttribute('selected', selected, row);
this._a11yUpdateRowSelected(row, selected);
}
_resizeHandler() {
this._updateDetailsCellHeights();
this._accessIronListAPI(super._resizeHandler, true);
this._updateHeaderFooterMetrics();
}
_updateHeaderFooterMetrics() {
const headerHeight = this.$.header.clientHeight + 'px';
const footerHeight = this.$.footer.clientHeight + 'px';
[this.$.outersizer, this.$.fixedsizer, this.$.items].forEach(element => {
element.style.borderTopWidth = headerHeight;
element.style.borderBottomWidth = footerHeight;
});
Polymer.RenderStatus.afterNextRender(this.$.header, () => {
if (this._pendingScrollToIndex) {
this._scrollToIndex(this._pendingScrollToIndex);
}
});
}
_onAnimationEnd(e) {
if (e.animationName === 'vaadin-grid-appear') {
this._render();
this._updateHeaderFooterMetrics();
e.stopPropagation();
}
}
_toggleAttribute(name, bool, node) {
if (bool) {
node.setAttribute(name, '');
} else {
node.removeAttribute(name);
}
}
ready() {
super.ready();
if (document.doctype === null) {
console.warn(
'<vaadin-grid> requires the "standards mode" declaration. Please add <!DOCTYPE html> to the HTML document.'
);
}
}
}
customElements.define(GridElement.is, GridElement);
/**
* @namespace Vaadin
*/
window.Vaadin = window.Vaadin || {};
Vaadin.GridElement = GridElement;
}
</script>