bitwarden-estensione-browser/angular/src/components/icon.component.ts

108 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-04-04 23:04:31 +02:00
import {
Component,
Input,
OnChanges,
} from '@angular/core';
import { CipherType } from 'jslib-common/enums/cipherType';
2018-04-04 23:04:31 +02:00
import { CipherView } from 'jslib-common/models/view/cipherView';
2018-11-06 18:13:11 +01:00
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
import { StateService } from 'jslib-common/abstractions/state.service';
2018-04-04 23:04:31 +02:00
import { ConstantsService } from 'jslib-common/services/constants.service';
2018-04-04 23:04:31 +02:00
import { Utils } from 'jslib-common/misc/utils';
2018-06-08 05:36:39 +02:00
2018-11-06 21:53:45 +01:00
const IconMap: any = {
2018-11-06 18:13:11 +01:00
'fa-globe': String.fromCharCode(0xf0ac),
'fa-sticky-note-o': String.fromCharCode(0xf24a),
'fa-id-card-o': String.fromCharCode(0xf2c3),
'fa-credit-card': String.fromCharCode(0xf09d),
'fa-android': String.fromCharCode(0xf17b),
'fa-apple': String.fromCharCode(0xf179),
};
2018-04-04 23:04:31 +02:00
@Component({
selector: 'app-vault-icon',
2018-04-06 18:01:21 +02:00
templateUrl: 'icon.component.html',
2018-04-04 23:04:31 +02:00
})
export class IconComponent implements OnChanges {
2018-11-06 18:13:11 +01:00
@Input() cipher: CipherView;
2018-04-04 23:04:31 +02:00
icon: string;
image: string;
fallbackImage: string;
imageEnabled: boolean;
private iconsUrl: string;
2018-11-15 22:55:54 +01:00
constructor(environmentService: EnvironmentService, protected stateService: StateService) {
this.iconsUrl = environmentService.getIconsUrl();
2018-04-04 23:04:31 +02:00
}
async ngOnChanges() {
// Components may be re-used when using cdk-virtual-scroll. Which puts the component in a weird state,
// to avoid this we reset all state variables.
this.image = null;
this.fallbackImage = null;
2018-04-04 23:04:31 +02:00
this.imageEnabled = !(await this.stateService.get<boolean>(ConstantsService.disableFaviconKey));
2018-11-15 22:55:54 +01:00
this.load();
}
get iconCode(): string {
return IconMap[this.icon];
}
2018-04-04 23:04:31 +02:00
2018-11-15 22:55:54 +01:00
protected load() {
2018-04-04 23:04:31 +02:00
switch (this.cipher.type) {
case CipherType.Login:
this.icon = 'fa-globe';
this.setLoginIcon();
break;
case CipherType.SecureNote:
this.icon = 'fa-sticky-note-o';
break;
case CipherType.Card:
this.icon = 'fa-credit-card';
break;
case CipherType.Identity:
this.icon = 'fa-id-card-o';
break;
default:
break;
}
}
private setLoginIcon() {
if (this.cipher.login.uri) {
let hostnameUri = this.cipher.login.uri;
let isWebsite = false;
if (hostnameUri.indexOf('androidapp://') === 0) {
this.icon = 'fa-android';
this.image = null;
} else if (hostnameUri.indexOf('iosapp://') === 0) {
this.icon = 'fa-apple';
this.image = null;
} else if (this.imageEnabled && hostnameUri.indexOf('://') === -1 && hostnameUri.indexOf('.') > -1) {
hostnameUri = 'http://' + hostnameUri;
isWebsite = true;
} else if (this.imageEnabled) {
isWebsite = hostnameUri.indexOf('http') === 0 && hostnameUri.indexOf('.') > -1;
}
if (this.imageEnabled && isWebsite) {
try {
2018-06-08 05:36:39 +02:00
this.image = this.iconsUrl + '/' + Utils.getHostname(hostnameUri) + '/icon.png';
2018-04-04 23:04:31 +02:00
this.fallbackImage = 'images/fa-globe.png';
} catch (e) {
// Ignore error since the fallback icon will be shown if image is null.
}
2018-04-04 23:04:31 +02:00
}
} else {
this.image = null;
}
}
}