-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathasset-element.js
More file actions
64 lines (57 loc) · 2.49 KB
/
asset-element.js
File metadata and controls
64 lines (57 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { formatMoney, BaseHTMLElement } from './base.js';
class AssetElement extends BaseHTMLElement {
/**
* Supported attributes
* {string} id: asset id
* {string} name: asset name
* {number} market-size
* {number} previous-market-size
* {string} denomination
* {string} class: additional class to append to the root div
*/
connectedCallback() {
this.#render();
}
attributeChangedCallback(name, oldValue, newValue) {
this.#render();
}
#render() {
const [id, name, marketSize, previousMarketSize, denomination] = [this.getAttribute("id"), this.getAttribute("name"),
this.getAttribute("market-size"), this.getAttribute("previous-market-size"), this.getAttribute("denomination")];
if (!id || !name) {
return;
}
const classesToAppend = this.getAttribute("class");
let previousMarketSizeComponent;
if (previousMarketSize && previousMarketSize != marketSize) {
const previousMarketSizeInt = parseInt(previousMarketSize);
const currentMarketSizeInt = parseInt(marketSize);
const marketIsUp = currentMarketSizeInt > previousMarketSizeInt;
let marketPercentageDiff;
if (marketIsUp) {
marketPercentageDiff = Math.round((currentMarketSizeInt - previousMarketSizeInt) * 100 * 100 / previousMarketSizeInt) / 100.0;
} else {
marketPercentageDiff = Math.round((previousMarketSizeInt - currentMarketSizeInt) * 100 * 100 / currentMarketSizeInt) / 100.0;
}
previousMarketSizeComponent = `
<div>
<span class="w-1/2 inline-block">${this.translation("previous-market-size-label")}:</span><span class="underline text-right w-1/2 inline-block">${formatMoney(previousMarketSize, denomination)}</span>
<div>
<p class="text-right italic">${marketIsUp ? this.translation('up-by-info') : this.translation('down-by-info')} ${marketPercentageDiff}%</p>`;
} else {
previousMarketSizeComponent = ``;
}
this.innerHTML = `
<div data-id=${id} class="border-1 p-2 rounded-lg ${classesToAppend ? classesToAppend : ""}">
<p class="font-bold">${name}</p>
<div>
<span class="w-1/2 inline-block">${this.translation('market-size-label')}:</span><span class="underline text-right w-1/2 inline-block">${formatMoney(marketSize, denomination)}</span>
</div>
${previousMarketSizeComponent}
</div>
`;
}
}
export function register() {
customElements.define("asset-element", AssetElement);
}