MarketSuggestions.sys.mjs (3157B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { RealtimeSuggestProvider } from "moz-src:///browser/components/urlbar/private/RealtimeSuggestProvider.sys.mjs"; 6 7 /** 8 * A feature that supports Market suggestions like stocks, indexes, and funds. 9 */ 10 export class MarketSuggestions extends RealtimeSuggestProvider { 11 get realtimeType() { 12 return "market"; 13 } 14 15 get isSponsored() { 16 return false; 17 } 18 19 get merinoProvider() { 20 return "polygon"; 21 } 22 23 getViewTemplateForDescriptionTop(_item, index) { 24 return [ 25 { 26 name: `name_${index}`, 27 tag: "span", 28 classList: ["urlbarView-market-name"], 29 }, 30 { 31 tag: "span", 32 classList: ["urlbarView-realtime-description-separator-dot"], 33 }, 34 { 35 name: `ticker_${index}`, 36 tag: "span", 37 }, 38 ]; 39 } 40 41 getViewTemplateForDescriptionBottom(_item, index) { 42 return [ 43 { 44 name: `todays_change_perc_${index}`, 45 tag: "span", 46 classList: ["urlbarView-market-todays-change-perc"], 47 }, 48 { 49 tag: "span", 50 classList: ["urlbarView-realtime-description-separator-dot"], 51 }, 52 { 53 name: `last_price_${index}`, 54 tag: "span", 55 classList: ["urlbarView-market-last-price"], 56 }, 57 { 58 tag: "span", 59 classList: ["urlbarView-realtime-description-separator-dot"], 60 }, 61 { 62 name: `exchange_${index}`, 63 tag: "span", 64 classList: ["urlbarView-market-exchange"], 65 }, 66 ]; 67 } 68 69 getViewUpdateForPayloadItem(item, index) { 70 let arrowImageUri; 71 let changeDescription; 72 let changePercent = parseFloat(item.todays_change_perc); 73 if (changePercent < 0) { 74 changeDescription = "down"; 75 arrowImageUri = "chrome://browser/skin/urlbar/market-down.svg"; 76 } else if (changePercent > 0) { 77 changeDescription = "up"; 78 arrowImageUri = "chrome://browser/skin/urlbar/market-up.svg"; 79 } else { 80 changeDescription = "unchanged"; 81 arrowImageUri = "chrome://browser/skin/urlbar/market-unchanged.svg"; 82 } 83 84 let imageUri = item.image_url; 85 let isImageAnArrow = false; 86 if (!imageUri) { 87 isImageAnArrow = true; 88 imageUri = arrowImageUri; 89 } 90 91 return { 92 [`item_${index}`]: { 93 attributes: { 94 change: changeDescription, 95 }, 96 }, 97 [`image_container_${index}`]: { 98 attributes: { 99 "is-arrow": isImageAnArrow ? "" : null, 100 }, 101 }, 102 [`image_${index}`]: { 103 attributes: { 104 src: imageUri, 105 }, 106 }, 107 [`name_${index}`]: { 108 textContent: item.name, 109 }, 110 [`ticker_${index}`]: { 111 textContent: item.ticker, 112 }, 113 [`todays_change_perc_${index}`]: { 114 textContent: `${item.todays_change_perc}%`, 115 }, 116 [`last_price_${index}`]: { 117 textContent: item.last_price, 118 }, 119 [`exchange_${index}`]: { 120 textContent: item.exchange, 121 }, 122 }; 123 } 124 }