-
Notifications
You must be signed in to change notification settings - Fork 14
/
table.html
110 lines (97 loc) · 4.3 KB
/
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
<!-- Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
Uses the HTML <table> content model, but automatically adds a number to
the <caption>. Use this as <table is="cxx-table">, unlike the other custom
elements.
With class="list", the table represents a list, which will be organized
into the number of columns set by the [columns] attribute.
-->
<polymer-element name="cxx-table" extends="table" constructor="CxxTableElement" attributes="columns">
<template>
<style>
:host { margin-left: auto; margin-right: auto;
border-collapse: collapse;
border: thin solid black; }
caption { white-space: nowrap; }
polyfill-next-selector { content: 'caption caption' }
::content caption { display: inline; }
polyfill-next-selector { content: 'th, td' }
::content th, ::content td {
border-style: solid none; border-color: black;
border-width: thin;
padding: 0 0.25em; }
polyfill-next-selector { content: ':host(.column-rules) th, :host(.column-rules) td' }
:host(.column-rules) ::content th,
:host(.column-rules) ::content td {
border-left-style: solid; border-right-style: solid;}
polyfill-next-selector { content: 'th' }
::content th { border-bottom: double medium; }
polyfill-next-selector { content: ':host(.single-border) th' }
:host(.single-border) ::content th { border-bottom: solid thin; }
polyfill-next-selector { content: ':host(.center) td' }
:host(.center) ::content td { text-align: center; }
polyfill-next-selector { content: ':host(.list) td' }
:host(.list) ::content td { border: none; vertical-align: top; }
polyfill-next-selector { content: ':host(.list) ul' }
:host(.list) ::content ul { padding-left: 0; margin: 0;}
polyfill-next-selector { content: ':host(.list) ul li::before' }
:host(.list) ::content ul li::before { content: '';}
</style>
<caption>Table {{table_num}} — <wbr><content select="caption"></content></caption>
<content></content>
</template>
<script>
(function() {
function renumberTables() {
document.querySelectorAll('[is=cxx-table]').array().forEach(function(table, index) {
table.table_num = index + 1;
});
}
Polymer('cxx-table', {
arrangeColumns: function() {
// Balance list-tables so we don't have to do it manually. I tried
// using CSS columns, but the appearance isn't as nice.
if (this.classList.contains('list')) {
// Set ARIA role so screenreaders go straight to the list.
this.setAttribute('role', 'presentation');
// Avoid messing with tables that are manually formatted.
if (this.rows.length == 1 &&
this.rows[0].cells.length == 1 &&
this.rows[0].cells[0].firstElementChild instanceof HTMLUListElement) {
var tr = this.rows[0];
var elems = tr.querySelectorAll('li').array();
tr.cells[0].remove();
var rows = Math.ceil(elems.length / this.columns);
for (var column = 0; column < this.columns; column++) {
var td = document.createElement('td');
tr.appendChild(td);
var ul = document.createElement('ul');
td.appendChild(ul);
for (var row = 0; row < rows; row++) {
var index = column*rows + row;
if (index < elems.length)
ul.appendChild(elems[index]);
}
}
}
}
},
domReady: function() {
renumberTables.apply(this);
this.arrangeColumns();
},
detached: renumberTables,
});
})();
</script>
</polymer-element>