-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathassets-and-currencies.js
More file actions
146 lines (130 loc) · 5.25 KB
/
assets-and-currencies.js
File metadata and controls
146 lines (130 loc) · 5.25 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { BaseHTMLElement } from "./base.js";
/**
* @typedef {Object} AssetOrCurrencyElement
* @property {string} id
* @property {string} name
* @property {number} marketSize
* @property {string} denomination
*/
class AssetsAndCurrencies extends BaseHTMLElement {
#assets = [];
#assetsValueChangeReason = null;
#currencies = [];
#denomination = "USD";
#assetsContainer = null;
#currenciesContainer = null;
/** @type {AssetOrCurrencyElement[]} */
set assets(value) {
this.#assets = value;
this.#renderAssets();
}
set assetsValueChangeReason(value) {
this.#assetsValueChangeReason = value;
this.#renderAssets();
}
/** @type {AssetOrCurrencyElement[]} */
set currencies(value) {
this.#currencies = value;
this.#renderCurrencies();
}
/** @type {string} */
set denomination(value) {
this.#denomination = value;
this.#renderAssets();
this.#renderCurrencies();
}
connectedCallback() {
this.innerHTML = `
<div class="m-4">
<tabs-container active-tab-class="underline">
<div class="flex" data-tabs-header>
<tab-header>${this.translation('assets-header')}</tab-header>
<tab-header>${this.translation('currencies-header')}</tab-header>
</div>
<div data-tabs-body>
<div class="h-[40dvh] overflow-y-auto">
${this.#assetsHTML()}
</div>
<div class="h-[40dvh] overflow-y-auto">
${this.#currenciesHTML()}
</div>
</div>
</tabs-container>
</div>`;
const tabsBody = this.querySelector("[data-tabs-body]");
this.#assetsContainer = tabsBody.children[0];
this.#currenciesContainer = tabsBody.children[1];
}
#assetsHTML(previousAssetElements = []) {
return this.#assets.map(a => {
const previousAsset = previousAssetElements.find(pa => pa.id == a.id);
let previousMarketSize;
if (!previousAsset) {
previousMarketSize = a.marketSize;
} else {
const previousAssetDenomination = previousAsset.getAttribute("denomination");
const previousAssetMarketSize = previousAsset.getAttribute("market-size");
// if denomination has changed, comparing current market size with the previous is meaningless
if (previousAssetDenomination != a.denomination) {
previousMarketSize = a.marketSize;
} else if (previousAssetMarketSize != a.marketSize) {
previousMarketSize = previousAssetMarketSize;
} else {
previousMarketSize = previousAsset.getAttribute("previous-market-size");
}
}
return `<asset-element class="my-2" id="${a.id}" name="${a.name}"
market-size="${a.marketSize}" previous-market-size="${previousMarketSize}"
denomination="${a.denomination}"
value-change-reason="${this.#assetsValueChangeReason}"
${this.translationAttribute('market-size-label')}
${this.translationAttribute('previous-market-size-label')}
${this.translationAttribute('up-by-info')}
${this.translationAttribute('down-by-info')}>
</asset-element>`;
}).join("\n");
}
#currenciesHTML(previousCurrencyElements = []) {
return this.#currencies.map(c => {
const previousCurrency = previousCurrencyElements.find(pc => pc.id == c.id);
let previousMarketSize;
if (!previousCurrency) {
previousMarketSize = c.marketSize;
} else {
const previousCurrencyDenomination = previousCurrency.getAttribute("denomination");
const previousCurrencyMarketSize = previousCurrency.getAttribute("market-size");
// if denomination has changed, comparing current market size with the previous is meaningless
if (previousCurrencyDenomination != c.denomination) {
previousMarketSize = c.marketSize;
} else if (previousCurrencyMarketSize != c.marketSize) {
previousMarketSize = previousCurrencyMarketSize;
} else {
previousMarketSize = previousCurrency.getAttribute("previous-market-size");
}
}
return `<currency-element class="my-2" id="${c.id}" name="${c.name}"
market-size="${c.marketSize}" previous-market-size="${previousMarketSize}"
denomination="${c.denomination}"
${this.translationAttribute('daily-turnover-label')}
${this.translationAttribute('yearly-turnover-label')}
${this.translationAttribute('up-by-info')}
${this.translationAttribute('down-by-info')}>
</currency-element>`})
.join("\n");
}
#renderAssets() {
if (this.#assetsContainer) {
const currentAssetElements = [...this.querySelectorAll("asset-element")];
this.#assetsContainer.innerHTML = this.#assetsHTML(currentAssetElements);
}
}
#renderCurrencies() {
if (this.#currenciesContainer) {
const currentCurrencyElements = [...this.querySelectorAll("currency-element")];
this.#currenciesContainer.innerHTML = this.#currenciesHTML(currentCurrencyElements);
}
}
}
export function register() {
customElements.define('assets-and-currencies', AssetsAndCurrencies);
}