Skip to content

Commit 3db4e95

Browse files
authored
refactor(legend): initialize lit migration (#546)
1 parent f7e5116 commit 3db4e95

File tree

6 files changed

+103
-104
lines changed

6 files changed

+103
-104
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"highlightjs-line-numbers.js": "^2.8.0",
116116
"ini": "^5.0.0",
117117
"kleur": "^4.1.5",
118+
"lit": "^3.3.1",
118119
"ms": "^2.1.3",
119120
"open": "^10.1.0",
120121
"pino": "^9.3.2",

public/components/legend/legend.css

Lines changed: 0 additions & 57 deletions
This file was deleted.

public/components/legend/legend.js

Lines changed: 97 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,112 @@
1+
// Import Third-party Dependencies
2+
import { LitElement, html, css } from "lit";
3+
14
// Import Internal Dependencies
25
import { COLORS } from "../../../workspaces/vis-network/src/constants.js";
3-
import { createDOMElement, currentLang } from "../../common/utils.js";
6+
import { currentLang } from "../../common/utils.js";
47

5-
export class Legend {
6-
constructor(
7-
options = {}
8-
) {
9-
const { show = false } = options;
8+
class Legend extends LitElement {
9+
static properties = {
10+
isVisible: { type: Boolean }
11+
};
1012

11-
const colors = COLORS.LIGHT;
12-
const { legend } = window.i18n[currentLang()];
13+
static styles = css`
14+
.container {
15+
position: absolute;
16+
right: 10px;
17+
bottom: 40px;
18+
z-index: 30;
19+
max-width: 692px;
20+
overflow: hidden;
21+
transition: transform 0.3s;
22+
display: flex;
23+
flex-flow: column wrap;
24+
justify-content: right;
25+
font-size: 14px;
26+
color: #030421;
27+
padding: 0 10px 10px 0;
28+
border-radius: 4px;
29+
}
1330
14-
const fragment = document.createDocumentFragment();
15-
fragment.append(
16-
createLegendBoxElement(colors.WARN, legend.warn),
17-
createLegendBoxElement(colors.FRIENDLY, legend.friendly),
18-
createLegendBoxElement(colors.DEFAULT, legend.default)
19-
);
31+
.legend-box {
32+
box-sizing: border-box;
33+
display: inline-flex;
34+
flex-direction: row-reverse;
35+
align-items: center;
36+
height: 24px;
37+
border-radius: 4px;
38+
}
2039
21-
this.DOMElement = document.getElementById("legend");
22-
this.DOMElement.replaceChildren(fragment);
23-
show && this.show();
24-
}
40+
.legend-badge {
41+
display: inline-block;
42+
width: 15px;
43+
height: 15px;
44+
margin: 0 5px;
45+
border-radius: 50%;
46+
box-shadow: 0 0 10px rgb(0 0 0 / 34%);
47+
}
48+
49+
.legend {
50+
font-weight: bold;
51+
padding-left: 6px;
52+
display: none;
53+
}
54+
55+
.legend-box:not(:hover) {
56+
background: transparent !important;
57+
}
58+
59+
.legend-box:hover {
60+
border: 1px solid rgb(48 56 165 / 60%);
61+
}
62+
63+
.legend-box:hover > .legend {
64+
display: flex;
65+
align-items: center;
66+
}
67+
68+
.legend-box:hover .legend-badge {
69+
box-shadow: none;
70+
}`;
2571

2672
show() {
27-
this.DOMElement.style.display = "flex";
73+
this.isVisible = true;
2874
}
2975

3076
hide() {
31-
this.DOMElement.style.display = "none";
77+
this.isVisible = false;
3278
}
33-
}
3479

35-
function createLegendBoxElement(
36-
theme,
37-
text
38-
) {
39-
const styles = {
40-
backgroundColor: theme.color,
41-
color: theme.font.color
42-
};
80+
render() {
81+
if (!this.isVisible) {
82+
return html``;
83+
}
84+
85+
const colors = COLORS.LIGHT;
86+
const { legend } = window.i18n[currentLang()];
87+
88+
return html`
89+
<div class="container">
90+
${this.#createLegendBoxElement(colors.WARN, legend.warn)}
91+
${this.#createLegendBoxElement(colors.FRIENDLY, legend.friendly)}
92+
${this.#createLegendBoxElement(colors.DEFAULT, legend.default)}
93+
</div>
94+
`;
95+
}
4396

44-
return createDOMElement("div", {
45-
className: "legend-box",
46-
childs: [
47-
createDOMElement("div", {
48-
className: "legend-badge",
49-
styles
50-
}),
51-
createDOMElement("div", {
52-
className: "legend",
53-
text
54-
})
55-
],
56-
styles
57-
});
97+
#createLegendBoxElement(
98+
theme,
99+
text
100+
) {
101+
const style = `background-color: ${theme.color}; color: ${theme.font.color};`;
102+
103+
return html`
104+
<div class="legend-box" style=${style}>
105+
<div class="legend-badge" style=${style}></div>
106+
<div class="legend">${text}</div>
107+
</div>
108+
`;
109+
}
58110
}
111+
112+
customElements.define("nsecure-legend", Legend);

public/main.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
@import url("./font/roboto/roboto.css");
66
@import url("./font/mononoki/mononoki.css");
77
@import url("./components/locker/locker.css");
8-
@import url("./components/legend/legend.css");
98
@import url("./components/popup/popup.css");
109
@import url("./components/file-box/file-box.css");
1110
@import url("./components/expandable/expandable.css");

public/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ViewNavigation } from "./components/navigation/navigation.js";
77
import { Wiki } from "./components/wiki/wiki.js";
88
import { Popup } from "./components/popup/popup.js";
99
import { Locker } from "./components/locker/locker.js";
10-
import { Legend } from "./components/legend/legend.js";
10+
import "./components/legend/legend.js";
1111
import { Settings } from "./components/views/settings/settings.js";
1212
import { HomeView } from "./components/views/home/home.js";
1313
import { SearchView } from "./components/views/search/search.js";
@@ -125,7 +125,9 @@ async function init(options = {}) {
125125
theme: window.settings.config.theme
126126
});
127127
window.locker = new Locker(nsn);
128-
window.legend = new Legend({ show: window.settings.config.showFriendlyDependencies });
128+
const legend = document.getElementById("legend");
129+
legend.isVisible = window.settings.config.showFriendlyDependencies;
130+
window.legend = legend;
129131
homeView ??= new HomeView(secureDataSet, nsn);
130132
searchview ??= new SearchView(secureDataSet, nsn);
131133

views/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<i class="icon-lock-open"></i>
8585
<p>[[=z.token('network.unlocked')]]</p>
8686
</div>
87-
<div id="legend">
87+
<nsecure-legend id="legend">
8888
</div>
8989
</div>
9090
<div id="search--view" class="view hidden">

0 commit comments

Comments
 (0)