-
Notifications
You must be signed in to change notification settings - Fork 12
/
d2l-table.js
286 lines (258 loc) · 7.97 KB
/
d2l-table.js
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
/**
# D2L Table
## Usage
```
<link rel="import" href="d2l-table.html">
<d2l-table>
<d2l-thead>
<d2l-tr>
<d2l-th>Table header cell</d2l-th>
</d2l-tr>
</d2l-thead>
<d2l-tfoot>
<d2l-tr>
<d2l-th>Table header cell</d2l-th>
</d2l-tr>
</d2l-tfoot>
<d2l-tbody>
<d2l-tr>
<d2l-td>Table cell</d2l-td>
</d2l-tr>
</d2l-tbody>
</d2l-table>
```
### Table attributes
Attribute | Description
----------|-------------
`type` | Table style - "default" or "light"
### Row Attributes
Attribute | Description
----------|-------------
`selected` | Apply selected style
`header` | Apply header style
## Styling
Custom property | Description | Default
----------------|-------------|----------
`--d2l-table-border-color` | Border Color | `var(--d2l-color-mica)` |
`--d2l-table-border-radius` | Corner Border Radius | `0.3rem` |
`--d2l-table-border` | Border | `1px solid var(--d2l-table-border-color);` |
`--d2l-table-header-background-color` | Header background color (th elements under `<thead>` or `<tr header>`) | `var(--d2l-color-regolith);` |
`--d2l-table-body-background-color` | Body background color (non-header) | `#fff` |
`--d2l-table-row-border-color-selected` | Selected row border color | `var(--d2l-color-celestine)` |
`--d2l-table-row-background-color-selected` | Selected row background color | `var(--d2l-color-celestine-plus-2)` |
`--d2l-table-border-overflow` | Border to show when the table overflows | `dashed 1px var(--d2l-color-mica)` |
`--d2l-table-light-border-color` | Border color for light style | `var(--d2l-color-gypsum)` |
`--d2l-table-light-border` | Border for light style | `1px solid var(--d2l-table-light-border-color)` |
`--d2l-table-light-header-background-color` | Header background color (th elements under `<thead>` or `<tr header>`) for light style | `#fff` |
```
<link rel="import" href="d2l-table.html">
<custom-style>
<style is="custom-style">
.ugly-table {
--d2l-table-border-color: purple
--d2l-table-border-radius: 0;
--d2l-table-header-background-color: grey;
--d2l-table-body-background-color: blue;
--d2l-table-row-border-color-selected: black;
--d2l-table-row-background-color-selected: black;
--d2l-table-border-overflow: none;
}
</style>
</custom-style>
<d2l-table class="ugly-table">
<d2l-thead>
<d2l-tr>
<d2l-th>Table header cell</d2l-th>
</d2l-tr>
</d2l-thead>
<d2l-tfoot>
<d2l-tr>
<d2l-th>Table header cell</d2l-th>
</d2l-tr>
</d2l-tfoot>
<d2l-tbody>
<d2l-tr>
<d2l-td>Table cell</d2l-td>
</d2l-tr>
</d2l-tbody>
</d2l-table>
```
@demo demo/index.html Table demo
@demo demo/index.rtl.html RTL Table demo
@demo demo/responsive.html Identical tables with different widths
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import '@polymer/polymer/polymer-legacy.js';
import '@brightspace-ui/core/components/colors/colors.js';
import 'fastdom/fastdom.js';
import './d2l-scroll-wrapper.js';
import './d2l-table-col-sort-button.js';
import './d2l-table-observer-behavior.js';
import './d2l-table-style.js';
import './d2l-table-wrapper.js';
import './d2l-tbody.js';
import './d2l-td.js';
import './d2l-tfoot.js';
import './d2l-th.js';
import './d2l-thead.js';
import './d2l-tr.js';
import './d2l-tspan.js';
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import { dom } from '@polymer/polymer/lib/legacy/polymer.dom.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<dom-module id="d2l-table">
<template strip-whitespace="">
<style>
:host {
background-color: transparent;
display: block;
width: 100%;
--d2l-table-border-color: var(--d2l-color-mica);
--d2l-table-border: 1px solid var(--d2l-table-border-color);
--d2l-table-header-background-color: var(--d2l-color-regolith);
--d2l-table-border-radius: 0.3rem;
--d2l-table-row-border-color-selected: var(--d2l-color-celestine);
--d2l-table-row-background-color-selected: var(--d2l-color-celestine-plus-2);
}
:host([hidden]) {
display: none;
}
:host([type="light"]) {
--d2l-table-border-color: var(--d2l-color-gypsum);
--d2l-table-header-background-color: #ffffff;
}
.d2l-table-inner {
background-color: transparent;
border-collapse: separate!important;
border-spacing: 0;
display: table;
font-size: 0.8rem;
font-weight: 400;
width: 100%;
}
</style>
<d2l-scroll-wrapper show-actions>
<div class="d2l-table-inner" role="table"><slot id="slot"></slot></div>
</d2l-scroll-wrapper>
</template>
</dom-module>`;
document.head.appendChild($_documentContainer.content);
Polymer({
is: 'd2l-table',
behaviors: [
D2L.PolymerBehaviors.Table.LocalObserverBehavior
],
listeners: {
'd2l-table-local-observer': '_handleLocalObserver'
},
properties: {
/*
* Styling type, possible options are "default" or "light"
*/
type: {
type: String,
value: 'default',
reflectToAttribute: true
}
},
__applyInQueue: false,
attached: function() {
afterNextRender(this, function() {
this._applyClassNames();
}.bind(this));
},
notifyResize: function() {
this.$$('d2l-scroll-wrapper').notifyResize();
},
_applyClassNames: function() {
// don't allow a bunch of calls to queue up
if (this.__applyInQueue) return;
this.__applyInQueue = true;
fastdom.mutate(function() {
this.__applyInQueue = false;
var rows = this._getRowsAndCells();
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
if (i === 0) {
row.elem.classList.add('d2l-table-row-first');
if (row.elem.tagName === 'D2L-TSPAN') {
row.elem.$.float.classList.add('d2l-table-row-first');
}
} else {
row.elem.classList.remove('d2l-table-row-first');
if (row.elem.tagName === 'D2L-TSPAN') {
row.elem.$.float.classList.remove('d2l-table-row-first');
}
}
if (i === rows.length - 1) {
row.elem.classList.add('d2l-table-row-last');
if (row.elem.tagName === 'D2L-TSPAN') {
row.elem.$.float.classList.add('d2l-table-row-last');
}
} else {
row.elem.classList.remove('d2l-table-row-last');
if (row.elem.tagName === 'D2L-TSPAN') {
row.elem.$.float.classList.remove('d2l-table-row-last');
}
}
for (var j = 0; j < row.cells.length; j++) {
var cell = row.cells[j];
if (j === 0) {
cell.classList.add('d2l-table-cell-first');
} else {
cell.classList.remove('d2l-table-cell-first');
}
if (j === row.cells.length - 1) {
cell.classList.add('d2l-table-cell-last');
} else {
cell.classList.remove('d2l-table-cell-last');
}
}
}
}.bind(this));
},
_handleLocalObserver: function() {
// descendants have been added/removed from somewhere in
// the <slot>,so re-calculate
this._applyClassNames();
},
_getRowsAndCells: function() {
var headerRows = [];
var bodyRows = [];
var footerRows = [];
function recurse(node, rows) {
var keepGoing = true;
var childRows = rows;
if (node.nodeType === 1) {
var tagName = node.tagName.toLowerCase();
if (tagName === 'd2l-tr' || tagName === 'd2l-tspan') {
rows.push({ elem: node, cells: [] });
} else if (tagName === 'd2l-td' || tagName === 'd2l-th') {
if (rows.length > 0) {
rows[rows.length - 1].cells.push(node);
keepGoing = false;
}
} else if (tagName === 'thead' || tagName === 'd2l-thead') {
childRows = headerRows;
} else if (tagName === 'tfoot' || tagName === 'd2l-tfoot') {
childRows = footerRows;
} else if (tagName === 'tbody' || tagName === 'd2l-tbody') {
childRows = bodyRows;
}
}
if (!keepGoing) return;
var children = dom(node).getEffectiveChildNodes();
for (var i = 0; i < children.length; i++) {
recurse(children[i], childRows);
}
}
recurse(this.root, bodyRows);
var rows = headerRows.concat(bodyRows.concat(footerRows));
return rows;
}
});