Skip to content

Commit 681c542

Browse files
Refactor
1 parent 51c5330 commit 681c542

File tree

3 files changed

+82
-50
lines changed

3 files changed

+82
-50
lines changed

Public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<div id="statistics-tab-pane" class="tab-pane" role="tabpanel" aria-labelledby="statistics-tab"
110110
tabindex="0">
111111
<div id="statistics-container" class="" role="document">
112-
<table id="statistics" class="table table-borderless table-striped table-hover table-sm">
112+
<table class="table table-borderless table-striped table-hover table-sm">
113113
<thead>
114114
<tr>
115115
<th scope="col" style="width: 60%;">Syntax</th>

Public/js/app.js

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
import { Tooltip } from "bootstrap";
44
import { TreeView } from "./tree_view.js";
5+
import { StatisticsView } from "./statistics.js";
56
import { Popover } from "./popover.js";
67
import { WebSocketClient } from "./websocket.js";
78
import { debounce } from "./debounce.js";
89

910
import "../css/editor.css";
1011
import "../css/syntax.css";
1112

12-
import DataTable from "datatables.net";
13-
import "datatables.net-bs5/css/dataTables.bootstrap5.min.css";
14-
1513
import "ace-builds/src-min-noconflict/ace";
1614
import "ace-builds/src-min-noconflict/ext-language_tools";
1715
import "ace-builds/src-min-noconflict/mode-swift";
@@ -55,7 +53,9 @@ export class App {
5553
this.popover = new Popover();
5654

5755
this.syntaxMapContainer = document.getElementById("syntax-map");
58-
this.statisticsContainer = document.getElementById("statistics");
56+
this.statisticsView = new StatisticsView(
57+
document.getElementById("statistics-container")
58+
);
5959

6060
this.init();
6161
}
@@ -476,51 +476,28 @@ export class App {
476476
}
477477

478478
updateStatistics(statistics) {
479-
const tbody = this.statisticsContainer.querySelector(":scope > tbody");
480-
tbody.innerHTML = "";
481-
482-
for (const row of statistics) {
483-
const tr = document.createElement("tr");
484-
tr.innerHTML = `<td style="font-family: 'Menlo', sans-serif, monospace;">${row.text}</td><td><div>${row.ranges.length}</div></td>`;
485-
tbody.appendChild(tr);
486-
487-
tr.addEventListener("mouseover", (event) => {
488-
event.preventDefault();
489-
event.stopPropagation();
490-
491-
for (const range of row.ranges) {
492-
this.editor.session.addMarker(
493-
new Range(
494-
range.startRow,
495-
range.startColumn,
496-
range.endRow,
497-
range.endColumn
498-
),
499-
"editor-marker",
500-
"text"
501-
);
502-
}
503-
});
504-
tr.addEventListener("mouseout", (event) => {
505-
event.preventDefault();
506-
event.stopPropagation();
507-
508-
const markers = this.editor.session.getMarkers();
509-
for (const [key, value] of Object.entries(markers)) {
510-
this.editor.session.removeMarker(value.id);
511-
}
512-
});
513-
}
514-
515-
if (this.dataTable) {
516-
this.dataTable.destroy();
517-
}
518-
this.dataTable = new DataTable("#statistics", {
519-
autoWidth: false,
520-
info: false,
521-
paging: false,
522-
searching: false,
523-
});
479+
this.statisticsView.update(statistics);
480+
481+
this.statisticsView.onmouseover = (event, target, ranges) => {
482+
for (const range of ranges) {
483+
this.editor.session.addMarker(
484+
new Range(
485+
range.startRow,
486+
range.startColumn,
487+
range.endRow,
488+
range.endColumn
489+
),
490+
"editor-marker",
491+
"text"
492+
);
493+
}
494+
};
495+
this.statisticsView.onmouseout = (event, target) => {
496+
const markers = this.editor.session.getMarkers();
497+
for (const [key, value] of Object.entries(markers)) {
498+
this.editor.session.removeMarker(value.id);
499+
}
500+
};
524501
}
525502
}
526503

Public/js/statistics.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"use strict";
2+
3+
import DataTable from "datatables.net";
4+
import "datatables.net-bs5/css/dataTables.bootstrap5.min.css";
5+
6+
export class StatisticsView {
7+
constructor(container) {
8+
this.container = container;
9+
10+
this.onmouseover = () => {};
11+
this.onmouseout = () => {};
12+
13+
this.init();
14+
}
15+
16+
init() {
17+
this.body = this.container.querySelector(":scope > table > tbody");
18+
}
19+
20+
update(statistics) {
21+
this.body.innerHTML = "";
22+
23+
for (const row of statistics) {
24+
const tr = document.createElement("tr");
25+
tr.innerHTML = `<td style="font-family: Menlo, Consolas, 'DejaVu Sans Mono', 'Ubuntu Mono', monospace;">${row.text}</td><td><div>${row.ranges.length}</div></td>`;
26+
this.body.appendChild(tr);
27+
28+
tr.addEventListener("mouseover", (event) => {
29+
event.preventDefault();
30+
event.stopPropagation();
31+
32+
this.onmouseover(event, tr, row.ranges);
33+
});
34+
tr.addEventListener("mouseout", (event) => {
35+
event.preventDefault();
36+
event.stopPropagation();
37+
38+
this.onmouseout(event, tr);
39+
});
40+
}
41+
42+
if (this.dataTable) {
43+
this.dataTable.destroy();
44+
}
45+
this.dataTable = new DataTable(
46+
this.container.querySelector(":scope > table"),
47+
{
48+
autoWidth: false,
49+
info: false,
50+
paging: false,
51+
searching: false,
52+
}
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)