|
| 1 | +// Import Third-party Dependencies |
| 2 | +import { LitElement, html, css } from "lit"; |
| 3 | + |
1 | 4 | // Import Internal Dependencies
|
2 | 5 | import { COLORS } from "../../../workspaces/vis-network/src/constants.js";
|
3 |
| -import { createDOMElement, currentLang } from "../../common/utils.js"; |
| 6 | +import { currentLang } from "../../common/utils.js"; |
4 | 7 |
|
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 | + }; |
10 | 12 |
|
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 | +} |
13 | 30 |
|
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 | +} |
20 | 39 |
|
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 | +}`; |
25 | 71 |
|
26 | 72 | show() {
|
27 |
| - this.DOMElement.style.display = "flex"; |
| 73 | + this.isVisible = true; |
28 | 74 | }
|
29 | 75 |
|
30 | 76 | hide() {
|
31 |
| - this.DOMElement.style.display = "none"; |
| 77 | + this.isVisible = false; |
32 | 78 | }
|
33 |
| -} |
34 | 79 |
|
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 | + } |
43 | 96 |
|
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 | + } |
58 | 110 | }
|
| 111 | + |
| 112 | +customElements.define("nsecure-legend", Legend); |
0 commit comments