2021-04-15 18:42:14 +05:30
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 14:15:06 +05:30
|
|
|
import {tag, text, classNames, setAttribute} from "./general/html";
|
2021-04-15 18:42:14 +05:30
|
|
|
/**
|
|
|
|
* @param {Object} vm view model with {avatarUrl, avatarColorNumber, avatarTitle, avatarLetter}
|
|
|
|
* @param {Number} size
|
|
|
|
* @return {Element}
|
|
|
|
*/
|
2021-04-27 18:34:01 +05:30
|
|
|
export function renderStaticAvatar(vm, size, extraClasses = undefined) {
|
2021-06-30 23:27:46 +05:30
|
|
|
const hasAvatar = !!vm.avatarUrl(size);
|
2021-04-27 18:34:01 +05:30
|
|
|
let avatarClasses = classNames({
|
2021-04-15 18:42:14 +05:30
|
|
|
avatar: true,
|
2021-04-28 17:15:43 +05:30
|
|
|
[`size-${size}`]: true,
|
2021-07-01 15:25:28 +05:30
|
|
|
[`usercolor${vm.avatarColorNumber}`]: !hasAvatar
|
2021-04-15 18:42:14 +05:30
|
|
|
});
|
2021-04-27 18:34:01 +05:30
|
|
|
if (extraClasses) {
|
|
|
|
avatarClasses += ` ${extraClasses}`;
|
|
|
|
}
|
2021-04-15 18:42:14 +05:30
|
|
|
const avatarContent = hasAvatar ? renderImg(vm, size) : text(vm.avatarLetter);
|
2022-06-29 16:26:20 +05:30
|
|
|
const avatar = tag.div({className: avatarClasses, "data-testid": "avatar"}, [avatarContent]);
|
2021-07-01 15:41:40 +05:30
|
|
|
if (hasAvatar) {
|
|
|
|
setAttribute(avatar, "data-avatar-letter", vm.avatarLetter);
|
|
|
|
setAttribute(avatar, "data-avatar-color", vm.avatarColorNumber);
|
|
|
|
}
|
|
|
|
return avatar;
|
2021-04-15 18:42:14 +05:30
|
|
|
}
|
|
|
|
|
2021-06-30 23:34:23 +05:30
|
|
|
export function renderImg(vm, size) {
|
2021-04-15 18:42:14 +05:30
|
|
|
const sizeStr = size.toString();
|
2021-04-28 17:40:54 +05:30
|
|
|
return tag.img({src: vm.avatarUrl(size), width: sizeStr, height: sizeStr, title: vm.avatarTitle});
|
2021-04-15 18:42:14 +05:30
|
|
|
}
|
2021-06-30 23:30:44 +05:30
|
|
|
|
|
|
|
function isAvatarEvent(e) {
|
|
|
|
const element = e.target;
|
|
|
|
const parent = element.parentElement;
|
|
|
|
return element.tagName === "IMG" && parent.classList.contains("avatar");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleAvatarError(e) {
|
|
|
|
if (!isAvatarEvent(e)) { return; }
|
|
|
|
const parent = e.target.parentElement;
|
2021-07-01 15:25:28 +05:30
|
|
|
const avatarColorNumber = parent.getAttribute("data-avatar-color");
|
|
|
|
parent.classList.add(`usercolor${avatarColorNumber}`);
|
2021-06-30 23:30:44 +05:30
|
|
|
const avatarLetter = parent.getAttribute("data-avatar-letter");
|
2021-07-01 00:01:38 +05:30
|
|
|
parent.textContent = avatarLetter;
|
2021-06-30 23:30:44 +05:30
|
|
|
}
|