aboutsummaryrefslogtreecommitdiffstats
path: root/ui/lib/icon-factory.js
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2016-06-07 05:05:13 +0800
committerDan Finlay <dan@danfinlay.com>2016-06-07 05:05:13 +0800
commitd5c378b09aff6e56585f9d05779d72c4e7919d5b (patch)
tree7e0fb017321c486273a67ae9a000ae8ed735d4bb /ui/lib/icon-factory.js
parent8dcba9a606f6d3593a5eedbc96452b9828179262 (diff)
downloadtangerine-wallet-browser-d5c378b09aff6e56585f9d05779d72c4e7919d5b.tar
tangerine-wallet-browser-d5c378b09aff6e56585f9d05779d72c4e7919d5b.tar.gz
tangerine-wallet-browser-d5c378b09aff6e56585f9d05779d72c4e7919d5b.tar.bz2
tangerine-wallet-browser-d5c378b09aff6e56585f9d05779d72c4e7919d5b.tar.lz
tangerine-wallet-browser-d5c378b09aff6e56585f9d05779d72c4e7919d5b.tar.xz
tangerine-wallet-browser-d5c378b09aff6e56585f9d05779d72c4e7919d5b.tar.zst
tangerine-wallet-browser-d5c378b09aff6e56585f9d05779d72c4e7919d5b.zip
Cache identicons
Fixes #197 Also as a side effect, by creating this `iconFactory.cache` object, we have a convenient place for specifying stock icons for known contracts! We can just hard-code image addresses in the `ui/lib/icon-factory.js` cache instantiation, and those values will be injected into the identicon image tag `src` attributes.
Diffstat (limited to 'ui/lib/icon-factory.js')
-rw-r--r--ui/lib/icon-factory.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/ui/lib/icon-factory.js b/ui/lib/icon-factory.js
new file mode 100644
index 000000000..1b1df9490
--- /dev/null
+++ b/ui/lib/icon-factory.js
@@ -0,0 +1,52 @@
+var iconFactory
+
+module.exports = function(jazzicon) {
+ if (!iconFactory) {
+ iconFactory = new IconFactory(jazzicon)
+ }
+ return iconFactory
+}
+
+function IconFactory(jazzicon) {
+ this.jazzicon = jazzicon
+ this.cache = {}
+}
+
+IconFactory.prototype.iconForAddress = function(address, diameter) {
+ if (this.isCached(address, diameter)) {
+ return this.cache[address][diameter]
+ }
+
+ const dataUri = this.generateNewUri(address, diameter)
+ this.cacheIcon(address, diameter, dataUri)
+ return dataUri
+}
+
+IconFactory.prototype.generateNewUri = function(address, diameter) {
+ var numericRepresentation = jsNumberForAddress(address)
+ var identicon = this.jazzicon(diameter, numericRepresentation)
+ var identiconSrc = identicon.innerHTML
+ var dataUri = 'data:image/svg+xml;charset=utf-8,'+encodeURIComponent(identiconSrc)
+ return dataUri
+}
+
+IconFactory.prototype.cacheIcon = function(address, diameter, icon) {
+ if (!(address in this.cache)) {
+ var sizeCache = {}
+ sizeCache[diameter] = icon
+ return this.cache[address] = sizeCache
+
+ } else {
+ return this.cache[address][diameter] = icon
+ }
+}
+
+IconFactory.prototype.isCached = function(address, diameter) {
+ return address in this.cache && diameter in this.cache[address]
+}
+
+function jsNumberForAddress(address) {
+ var addr = address.slice(2, 10)
+ var seed = parseInt(addr, 16)
+ return seed
+}