mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 11:16:35 +00:00
Add person picture to user badge (#6784)
* Use person picture ha-user-badge * Fix missing import * lint * User person picture in user-badge Co-authored-by: Ludeeus <ludeeus@ludeeus.dev>
This commit is contained in:
parent
584e509a9c
commit
426a0727c3
@ -374,7 +374,11 @@ class HaSidebar extends LitElement {
|
|||||||
@mouseleave=${this._itemMouseLeave}
|
@mouseleave=${this._itemMouseLeave}
|
||||||
>
|
>
|
||||||
<paper-icon-item>
|
<paper-icon-item>
|
||||||
<ha-user-badge slot="item-icon" .user=${hass.user}></ha-user-badge>
|
<ha-user-badge
|
||||||
|
slot="item-icon"
|
||||||
|
.user=${hass.user}
|
||||||
|
.hass=${hass}
|
||||||
|
></ha-user-badge>
|
||||||
|
|
||||||
<span class="item-text">
|
<span class="item-text">
|
||||||
${hass.user ? hass.user.name : ""}
|
${hass.user ? hass.user.name : ""}
|
||||||
|
@ -3,13 +3,16 @@ import {
|
|||||||
CSSResult,
|
CSSResult,
|
||||||
customElement,
|
customElement,
|
||||||
html,
|
html,
|
||||||
|
internalProperty,
|
||||||
LitElement,
|
LitElement,
|
||||||
property,
|
property,
|
||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
|
import { styleMap } from "lit-html/directives/style-map";
|
||||||
import { toggleAttribute } from "../../common/dom/toggle_attribute";
|
import { toggleAttribute } from "../../common/dom/toggle_attribute";
|
||||||
|
import { computeStateDomain } from "../../common/entity/compute_state_domain";
|
||||||
import { User } from "../../data/user";
|
import { User } from "../../data/user";
|
||||||
import { CurrentUser } from "../../types";
|
import { CurrentUser, HomeAssistant } from "../../types";
|
||||||
|
|
||||||
const computeInitials = (name: string) => {
|
const computeInitials = (name: string) => {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
@ -29,26 +32,93 @@ const computeInitials = (name: string) => {
|
|||||||
|
|
||||||
@customElement("ha-user-badge")
|
@customElement("ha-user-badge")
|
||||||
class StateBadge extends LitElement {
|
class StateBadge extends LitElement {
|
||||||
@property() public user?: User | CurrentUser;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
@property({ attribute: false }) public user?: User | CurrentUser;
|
||||||
const user = this.user;
|
|
||||||
const initials = user ? computeInitials(user.name) : "?";
|
@internalProperty() private _personPicture?: string;
|
||||||
return html` ${initials} `;
|
|
||||||
}
|
private _personEntityId?: string;
|
||||||
|
|
||||||
protected updated(changedProps) {
|
protected updated(changedProps) {
|
||||||
super.updated(changedProps);
|
super.updated(changedProps);
|
||||||
toggleAttribute(
|
if (changedProps.has("user")) {
|
||||||
this,
|
this._getPersonPicture();
|
||||||
"long",
|
if (!this._personPicture) {
|
||||||
(this.user ? computeInitials(this.user.name) : "?").length > 2
|
toggleAttribute(
|
||||||
);
|
this,
|
||||||
|
"long",
|
||||||
|
(this.hass.user ? computeInitials(this.hass.user.name) : "?").length >
|
||||||
|
2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const oldHass = changedProps.get("hass");
|
||||||
|
if (
|
||||||
|
this._personEntityId &&
|
||||||
|
oldHass &&
|
||||||
|
this.hass.states[this._personEntityId] !==
|
||||||
|
oldHass.states[this._personEntityId]
|
||||||
|
) {
|
||||||
|
const state = this.hass.states[this._personEntityId];
|
||||||
|
if (state) {
|
||||||
|
this._personPicture = state.attributes.entity_picture;
|
||||||
|
} else {
|
||||||
|
this._getPersonPicture();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected render(): TemplateResult {
|
||||||
|
if (!this.hass || !this.user) {
|
||||||
|
return html``;
|
||||||
|
}
|
||||||
|
const user = this.user;
|
||||||
|
const picture = this._personPicture;
|
||||||
|
|
||||||
|
return html`
|
||||||
|
${picture
|
||||||
|
? html`<div
|
||||||
|
style=${styleMap({ backgroundImage: `url(${picture})` })}
|
||||||
|
class="picture"
|
||||||
|
></div>`
|
||||||
|
: html`<div
|
||||||
|
class="initials"
|
||||||
|
?long=${(user ? computeInitials(user.name) : "?").length > 2}
|
||||||
|
>
|
||||||
|
${computeInitials(user?.name!)}
|
||||||
|
</div>`}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _getPersonPicture() {
|
||||||
|
this._personEntityId = undefined;
|
||||||
|
this._personPicture = undefined;
|
||||||
|
if (!this.hass || !this.user) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const entity of Object.values(this.hass.states)) {
|
||||||
|
if (
|
||||||
|
entity.attributes.user_id === this.user!.id &&
|
||||||
|
computeStateDomain(entity) === "person"
|
||||||
|
) {
|
||||||
|
this._personEntityId = entity.entity_id;
|
||||||
|
this._personPicture = entity.attributes.entity_picture;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResult {
|
static get styles(): CSSResult {
|
||||||
return css`
|
return css`
|
||||||
:host {
|
.picture {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
background-size: cover;
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
|
.initials {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
width: 40px;
|
width: 40px;
|
||||||
@ -60,8 +130,7 @@ class StateBadge extends LitElement {
|
|||||||
color: var(--text-light-primary-color, var(--primary-text-color));
|
color: var(--text-light-primary-color, var(--primary-text-color));
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
:host([long]) .initials {
|
||||||
:host([long]) {
|
|
||||||
font-size: 80%;
|
font-size: 80%;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -53,7 +53,11 @@ class HaUserPicker extends LitElement {
|
|||||||
${this._sortedUsers(this.users).map(
|
${this._sortedUsers(this.users).map(
|
||||||
(user) => html`
|
(user) => html`
|
||||||
<paper-icon-item data-user-id=${user.id}>
|
<paper-icon-item data-user-id=${user.id}>
|
||||||
<ha-user-badge .user=${user} slot="item-icon"></ha-user-badge>
|
<ha-user-badge
|
||||||
|
.hass=${this.hass}
|
||||||
|
.user=${user}
|
||||||
|
slot="item-icon"
|
||||||
|
></ha-user-badge>
|
||||||
${user.name}
|
${user.name}
|
||||||
</paper-icon-item>
|
</paper-icon-item>
|
||||||
`
|
`
|
||||||
|
@ -1,17 +1,21 @@
|
|||||||
|
import "@material/mwc-fab";
|
||||||
|
import { mdiPlus } from "@mdi/js";
|
||||||
import "@polymer/paper-item/paper-icon-item";
|
import "@polymer/paper-item/paper-icon-item";
|
||||||
import "@polymer/paper-item/paper-item-body";
|
import "@polymer/paper-item/paper-item-body";
|
||||||
import {
|
import {
|
||||||
css,
|
css,
|
||||||
CSSResult,
|
CSSResult,
|
||||||
html,
|
html,
|
||||||
|
internalProperty,
|
||||||
LitElement,
|
LitElement,
|
||||||
property,
|
property,
|
||||||
internalProperty,
|
|
||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
|
import { styleMap } from "lit-html/directives/style-map";
|
||||||
import { compare } from "../../../common/string/compare";
|
import { compare } from "../../../common/string/compare";
|
||||||
import "../../../components/ha-card";
|
import "../../../components/ha-card";
|
||||||
import "@material/mwc-fab";
|
import "../../../components/ha-svg-icon";
|
||||||
|
import "../../../components/user/ha-user-badge";
|
||||||
import {
|
import {
|
||||||
createPerson,
|
createPerson,
|
||||||
deletePerson,
|
deletePerson,
|
||||||
@ -30,9 +34,6 @@ import {
|
|||||||
loadPersonDetailDialog,
|
loadPersonDetailDialog,
|
||||||
showPersonDetailDialog,
|
showPersonDetailDialog,
|
||||||
} from "./show-dialog-person-detail";
|
} from "./show-dialog-person-detail";
|
||||||
import "../../../components/ha-svg-icon";
|
|
||||||
import { mdiPlus } from "@mdi/js";
|
|
||||||
import { styleMap } from "lit-html/directives/style-map";
|
|
||||||
|
|
||||||
class HaConfigPerson extends LitElement {
|
class HaConfigPerson extends LitElement {
|
||||||
@property({ attribute: false }) public hass?: HomeAssistant;
|
@property({ attribute: false }) public hass?: HomeAssistant;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user