From 9a79f6989e4eb1d7ff142d93c6dbd89f4b0f668f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 16 Oct 2017 11:02:39 -0400 Subject: [PATCH] icon component updates for all cipher types --- src/popup/app/components/iconComponent.js | 85 +++++++++++++++---- .../app/directives/fallbackSrcDirective.js | 11 +++ src/popup/app/vault/views/vault.html | 4 +- src/popup/index.html | 1 + src/popup/less/components.less | 1 + 5 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 src/popup/app/directives/fallbackSrcDirective.js diff --git a/src/popup/app/components/iconComponent.js b/src/popup/app/components/iconComponent.js index 837314e7..48b6f9c1 100644 --- a/src/popup/app/components/iconComponent.js +++ b/src/popup/app/components/iconComponent.js @@ -3,25 +3,76 @@ angular .component('icon', { bindings: { - uri: '<' + cipher: '<' }, - template: '
', - controller: function (stateService) { - this.$onInit = (function () { - this.enabled = function () { - return stateService.getState('faviconEnabled'); - }; - }).bind(this); + template: '
' + + '' + + '' + + '
', + controller: function (stateService, constantsService) { + var ctrl = this; - this.$onChanges = (function () { - var hostname; - try { - hostname = new URL(this.uri).hostname; - this.url = 'https://icons.bitwarden.com/' + hostname + '/icon.png'; - } catch (e) { - // Invalid URL. - this.url = chrome.extension.getURL('images/fa-globe.png'); + ctrl.$onInit = function () { + ctrl.enabled = stateService.getState('faviconEnabled'); + if (ctrl.enabled) { + switch (ctrl.cipher.type) { + case constantsService.cipherType.login: + setLoginIcon(ctrl.cipher); + break; + case constantsService.cipherType.secureNote: + ctrl.icon = 'fa-sticky-note-o'; + break; + case constantsService.cipherType.card: + ctrl.icon = 'fa-credit-card'; + break; + case constantsService.cipherType.identity: + ctrl.icon = 'fa-id-card-o'; + break; + default: + break; + } } - }).bind(this); + }; + + ctrl.$onChanges = function () { + if (ctrl.cipher.type === constantsService.cipherType.login) { + setLoginIcon(ctrl.cipher); + } + }; + + function setLoginIcon() { + if (ctrl.cipher.login.uri) { + var hostnameUri = ctrl.cipher.login.uri, + isWebsite = false; + + if (hostnameUri.indexOf('androidapp://') === 0) { + ctrl.icon = 'fa-android'; + } + else if (hostnameUri.indexOf('iosapp://') === 0) { + ctrl.icon = 'fa-apple'; + } + else if (hostnameUri.indexOf('://') === -1 && hostnameUri.indexOf('http://') !== 0 && + hostnameUri.indexOf('https://') !== 0) { + hostnameUri = "http://" + hostnameUri; + isWebsite = true; + } + else { + isWebsite = hostnameUri.indexOf('http') === 0 && hostnameUri.indexOf('.') > 0; + } + + if (isWebsite) { + try { + var url = new URL(hostnameUri); + ctrl.image = 'https://icons.bitwarden.com/' + url.hostname + '/icon.png'; + ctrl.fallbackImage = chrome.extension.getURL('images/fa-globe.png'); + } + catch (e) { } + } + } + + if (!ctrl.icon) { + ctrl.icon = 'fa-globe'; + } + } } }); diff --git a/src/popup/app/directives/fallbackSrcDirective.js b/src/popup/app/directives/fallbackSrcDirective.js new file mode 100644 index 00000000..ac93c191 --- /dev/null +++ b/src/popup/app/directives/fallbackSrcDirective.js @@ -0,0 +1,11 @@ +angular + .module('bit.directives') + + .directive('fallbackSrc', function () { + return function (scope, element, attrs) { + var el = $(element); + el.bind('error', function (event) { + el.attr('src', attrs.fallbackSrc); + }); + }; + }); diff --git a/src/popup/app/vault/views/vault.html b/src/popup/app/vault/views/vault.html index 6c7b0b09..f5ef2208 100644 --- a/src/popup/app/vault/views/vault.html +++ b/src/popup/app/vault/views/vault.html @@ -55,7 +55,7 @@ ng-class="{'disabled': !cipher.uri}"> - + {{cipher.name}} @@ -88,7 +88,7 @@ ng-class="{'disabled': !cipher.uri}"> - + {{cipher.name}} diff --git a/src/popup/index.html b/src/popup/index.html index 73854300..9d433752 100644 --- a/src/popup/index.html +++ b/src/popup/index.html @@ -58,6 +58,7 @@ + diff --git a/src/popup/less/components.less b/src/popup/less/components.less index ee9d6b9b..8edc078c 100644 --- a/src/popup/less/components.less +++ b/src/popup/less/components.less @@ -357,6 +357,7 @@ height: 36px; width: 34px; margin-left: -5px; + color: @text-muted; img { border-radius: 3px;