/** @license Copyright (c) 2024 trading_peter This program is available under Apache License Version 2.0 */ import '@tp/tp-icon/tp-icon.js'; import { LitElement, html, css, svg } from 'lit'; import { classMap } from 'lit/directives/class-map.js'; class TpAvatar extends LitElement { static get styles() { return [ css` :host { display: inline-block; position: relative; border-radius: 50%; } :host [hidden] { display: none; } #avatarImg { width: var(--era-avatar-size, 32px); height: var(--era-avatar-size, 32px); background-size: cover; background-position: center center; border-radius: 50%; opacity: 0; } #avatarImg.visible { transition: opacity 300ms; opacity: 1; } tp-icon { opacity: 1; transition: opacity 100ms; color: #BDBDBD; position: absolute; inset: 0; --tp-icon-height: var(--ef-avatar-size, 32px); --tp-icon-width: var(--ef-avatar-size, 32px); } tp-icon.hidden { opacity: 0; transition: opacity 300ms linear 0ms; } ` ]; } render() { const { loaded, hasSrc } = this; return html`
`; } static get properties() { return { loaded: { type: Boolean }, baseUrl: { type: String }, loading: { type: Boolean }, hasSrc: { type: Boolean }, src: { type: String }, }; } static get defaultAccountIcon() { return svg``; } updated(changes) { if (changes.has('src')) { if (!this.src) { this.hasSrc = false; return; } if (this.src !== changes.get('src')) { this.hasSrc = true; if (newSrc.indexOf(this.baseUrl) !== 0 && !this.hasSchema(newSrc)) { this.src = this.baseUrl + this.src; return; } this.loaded = false; if (Boolean(newSrc)) { this.loadImage(); } } } } hasSchema(src) { return src.indexOf('http://') === 0 || src.indexOf('https://') === 0; } loadedChanged() { if (this.loaded) { this.$.avatarImg.style.backgroundImage = `url("${this.src}")`; } } loadImage() { var img = new Image(); img.src = this.src; this.loading = true; img.onload = function() { this.loading = false; this.loaded = true; this.loadedChanged(); }.bind(this); } } window.customElements.define('tp-avatar', TpAvatar);