Skip to content

Commit dfcf427

Browse files
Revert "decouple logic and display (#287)" (#288)
This reverts commit 7bc4919.
1 parent 470937f commit dfcf427

File tree

9 files changed

+314
-292
lines changed

9 files changed

+314
-292
lines changed

code/stats-table.js code/collapsable-table.js

+34-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class StatsTable extends HTMLElement {
1+
class CollapsableTable extends HTMLElement {
22
maxLinesCollapsed = 12;
33

44
// Elements built in constructor
@@ -19,6 +19,14 @@ class StatsTable extends HTMLElement {
1919
// Actually build the content of the element (+ remove the stupid tr)
2020
shadow.innerHTML = `
2121
<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+
2230
#wrapper {
2331
margin-bottom: 1em;
2432
display: flex;
@@ -40,25 +48,37 @@ class StatsTable extends HTMLElement {
4048
td:nth-child(2) { width: 4em; text-align: right; }
4149
4250
button {
43-
width: 20%;
44-
height: 1.5em;
51+
width: 30%;
4552
margin: auto;
4653
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;
5256
}
5357
</style>
5458
59+
<h3> ${this.id} </h3>
5560
<!-- Using a style attribute on top of the stylesheet, as it is used by
5661
the button 'click' event-listner -->
5762
<div id='wrapper' style='max-height: ${this.maxHeightCollapsed}px;'></div>
58-
<button style='display: none'></button>
63+
<button style='display: none'> show more </button>
5964
`;
6065

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');
6169
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+
6282
wrapper.innerHTML = this.innerHTML;
6383
this.innerHTML = ''; // Remove original content
6484

@@ -69,19 +89,19 @@ class StatsTable extends HTMLElement {
6989
const wrapper = shadow.getElementById('wrapper');
7090
if (wrapper.style.maxHeight == `${self.maxHeightCollapsed}px`) {
7191
wrapper.style.maxHeight = `${wrapper.children[0].offsetHeight}px`;
72-
this.className = 'showLess';
92+
this.innerText = 'show less';
7393
} else {
7494
wrapper.style.maxHeight = `${self.maxHeightCollapsed}px`;
75-
this.className = '';
95+
this.innerText = 'show more';
7696
}
7797
});
7898
}
7999

80-
updateTableData(tableSelector, values, precision) {
100+
updateTableData(tableSelector, title, values, precision) {
81101
const table = this.shadowRoot.querySelector(tableSelector);
82102

83103
table.innerHTML =
84-
`<tr><th colspan='2'>${table.title}</td></tr>` +
104+
`<tr><th colspan='2'>${title}</td></tr>` +
85105
Object.entries(values)
86106
.filter(([digram, freq]) => freq >= 10 ** -precision)
87107
.sort(([_, freq1], [__, freq2]) => freq2 - freq1)
@@ -95,5 +115,4 @@ class StatsTable extends HTMLElement {
95115
table.offsetHeight > this.maxHeightCollapsed ? 'block' : 'none';
96116
}
97117
}
98-
99-
customElements.define('stats-table', StatsTable);
118+
customElements.define('collapsable-table', CollapsableTable);

0 commit comments

Comments
 (0)