1
- class StatsTable extends HTMLElement {
1
+ class CollapsableTable extends HTMLElement {
2
2
maxLinesCollapsed = 12 ;
3
3
4
4
// Elements built in constructor
@@ -19,6 +19,14 @@ class StatsTable extends HTMLElement {
19
19
// Actually build the content of the element (+ remove the stupid tr)
20
20
shadow . innerHTML = `
21
21
<style>
22
+ /* Mostly copy-pasted from '/css/heatmap.css', with some ajustments */
23
+ h3 { border-bottom: 1px dotted; }
24
+
25
+ #header {
26
+ text-align: right;
27
+ margin-top: -1em;
28
+ }
29
+
22
30
#wrapper {
23
31
margin-bottom: 1em;
24
32
display: flex;
@@ -40,25 +48,37 @@ class StatsTable extends HTMLElement {
40
48
td:nth-child(2) { width: 4em; text-align: right; }
41
49
42
50
button {
43
- width: 20%;
44
- height: 1.5em;
51
+ width: 30%;
45
52
margin: auto;
46
53
background-color: #88fa;
47
- cursor: pointer;
48
- clip-path: polygon(50% 100%, 0% 0%, 100% 0%);
49
- }
50
- button.showLess {
51
- clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
54
+ border: 1px solid black;
55
+ border-radius: 15px;
52
56
}
53
57
</style>
54
58
59
+ <h3> ${ this . id } </h3>
55
60
<!-- Using a style attribute on top of the stylesheet, as it is used by
56
61
the button 'click' event-listner -->
57
62
<div id='wrapper' style='max-height: ${ this . maxHeightCollapsed } px;'></div>
58
- <button style='display: none'></button>
63
+ <button style='display: none'> show more </button>
59
64
` ;
60
65
66
+ // If we find a 'small' element, then move it in a '#header' div instead of
67
+ // the '#wrapper' div. A 'slot' is probably better, but I can’t make it work
68
+ const smallElement = this . querySelector ( 'small' ) ;
61
69
const wrapper = shadow . getElementById ( 'wrapper' ) ;
70
+ if ( smallElement ) {
71
+ // Placing the 'small' element in a wrapper div, otherwise the 'text-align'
72
+ // and 'margin-top' css properties don’t do anything.
73
+ const smallElementWrapper = document . createElement ( 'div' ) ;
74
+ smallElementWrapper . id = 'header' ;
75
+ smallElementWrapper . appendChild ( smallElement . cloneNode ( true ) ) ;
76
+
77
+ shadow . insertBefore ( smallElementWrapper , wrapper ) ;
78
+ // Remove the 'small' element from this.innerHTML, before moving that to shadow
79
+ smallElement . remove ( ) ;
80
+ }
81
+
62
82
wrapper . innerHTML = this . innerHTML ;
63
83
this . innerHTML = '' ; // Remove original content
64
84
@@ -69,19 +89,19 @@ class StatsTable extends HTMLElement {
69
89
const wrapper = shadow . getElementById ( 'wrapper' ) ;
70
90
if ( wrapper . style . maxHeight == `${ self . maxHeightCollapsed } px` ) {
71
91
wrapper . style . maxHeight = `${ wrapper . children [ 0 ] . offsetHeight } px` ;
72
- this . className = 'showLess ' ;
92
+ this . innerText = 'show less ' ;
73
93
} else {
74
94
wrapper . style . maxHeight = `${ self . maxHeightCollapsed } px` ;
75
- this . className = '' ;
95
+ this . innerText = 'show more ' ;
76
96
}
77
97
} ) ;
78
98
}
79
99
80
- updateTableData ( tableSelector , values , precision ) {
100
+ updateTableData ( tableSelector , title , values , precision ) {
81
101
const table = this . shadowRoot . querySelector ( tableSelector ) ;
82
102
83
103
table . innerHTML =
84
- `<tr><th colspan='2'>${ table . title } </td></tr>` +
104
+ `<tr><th colspan='2'>${ title } </td></tr>` +
85
105
Object . entries ( values )
86
106
. filter ( ( [ digram , freq ] ) => freq >= 10 ** - precision )
87
107
. sort ( ( [ _ , freq1 ] , [ __ , freq2 ] ) => freq2 - freq1 )
@@ -95,5 +115,4 @@ class StatsTable extends HTMLElement {
95
115
table . offsetHeight > this . maxHeightCollapsed ? 'block' : 'none' ;
96
116
}
97
117
}
98
-
99
- customElements . define ( 'stats-table' , StatsTable ) ;
118
+ customElements . define ( 'collapsable-table' , CollapsableTable ) ;
0 commit comments