diff options
author | Frankie <frankie.pangilinan@consensys.net> | 2016-06-09 05:12:25 +0800 |
---|---|---|
committer | Frankie <frankie.pangilinan@consensys.net> | 2016-06-09 05:12:25 +0800 |
commit | 03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed (patch) | |
tree | e47867a4f8a8ad4d6ed3ae200cc08c7f0dc1f88c /ui/lib/icon-factory.js | |
parent | e9407777cc6f44a87a4c34e07562f278d66a48c4 (diff) | |
parent | 1836b83a530a001b83bffebbcf3483220fa02a21 (diff) | |
download | tangerine-wallet-browser-03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed.tar tangerine-wallet-browser-03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed.tar.gz tangerine-wallet-browser-03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed.tar.bz2 tangerine-wallet-browser-03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed.tar.lz tangerine-wallet-browser-03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed.tar.xz tangerine-wallet-browser-03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed.tar.zst tangerine-wallet-browser-03e9ff6e42fe230a1f7a0e6185eca57248c7a2ed.zip |
Mend CHANGE.log
Diffstat (limited to 'ui/lib/icon-factory.js')
-rw-r--r-- | ui/lib/icon-factory.js | 52 |
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 +} |