1/** 2 * currency Class 3 * @constructor 4 */ 5class CurrencyView { 6 currency 7 currencies_with_symbols 8 currencyClasses 9 /** 10 * @param {string} template 11 * @param {JSON} currencies_with_symbols 12 */ 13 constructor(currencies_with_symbols, options = {}) { 14 this.currencies_with_symbols = currencies_with_symbols 15 this.currencyClasses = Object.keys(currencies_with_symbols).map((v) => `current-currency-${v}`) 16 17 this.#getCurrency() 18 } 19 20 /** 21 * figure out what is the currency that should be queried 22 * @private 23 * @returns {void} 24 */ 25 #getCurrency() { 26 const currency_from_cookie = Cookie.readCookie('currency_val') 27 if (currency_from_cookie) { 28 this.currency = currency_from_cookie 29 } else { 30 const country = document.querySelector('[data-xcountry]')?.getAttribute('data-xcountry') 31 this.currency = country ? this.#getCurrencyFromCountry(country) : 'GBP' 32 } 33 34 events.publish('CurrencyView_currency_declared', { currency: this.currency }) 35 } 36 37 #getCurrencyFromCountry(country) { 38 const usdCountries = new Set(['United States', 'Canada', 'Argentina', 'Mexico']) 39 const gbpCountries = new Set(['United Kingdom', 'Wales', 'Scotland', 'Northern Ireland', 'Qatar']) 40 41 if (gbpCountries.has(country)) { 42 return 'GBP' 43 } else if (usdCountries.has(country)) { 44 return 'USD' 45 } 46 return 'EUR' 47 } 48 /** 49 * Setting the icon that will be displayed in the menu 50 * @private 51 * @returns {void} 52 */ 53 #updateDropdown() { 54 document.querySelectorAll('.currency-dropdown').forEach((dropdown) => { 55 dropdown?.classList.remove(...this.currencyClasses) 56 dropdown?.classList.add(`current-currency-${this.currency}`) 57 }) 58 } 59 60 /** 61 * onclick behaviour for when selecting a currency. 62 * @param {Element} el 63 * @returns {void} 64 */ 65 selectCurrency(el) { 66 67 this.#setCurrency(el) 68 this.#updateCookie() 69 this.#updateDropdown() 70 // this.#publishEvent() 71 // Only reload if not prevented 72 window.location.reload() 73 } 74 75 /** 76 * Set the selected currency. 77 * @param {Element} el 78 * @private 79 * @returns {void} 80 */ 81 #setCurrency(el) { 82 this.currency = el.getAttribute('data-currency') 83 } 84 85 /** 86 * Update the cookie with the selected currency. 87 * @private 88 * @returns {void} 89 */ 90 #updateCookie() { 91 Cookie.createCookie('currency_val', this.currency, 7) 92 } 93 94 /** 95 * Publish event to notify other parts of the application about the currency change. 96 * @private 97 * @returns {void} 98 */ 99 #publishEvent() { 100 events.publish('CurrencyView_currency_declared', { 101 currency: this.currency, 102 }) 103 } 104}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.