2021-04-15 15:12:14 +02:00
|
|
|
/*
|
|
|
|
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-07-01 12:11:40 +02:00
|
|
|
import {tag, text, classNames, setAttribute} from "./general/html.js";
|
2021-04-15 15:12:14 +02:00
|
|
|
/**
|
|
|
|
* @param {Object} vm view model with {avatarUrl, avatarColorNumber, avatarTitle, avatarLetter}
|
|
|
|
* @param {Number} size
|
|
|
|
* @return {Element}
|
|
|
|
*/
|
2021-04-27 15:04:01 +02:00
|
|
|
export function renderStaticAvatar(vm, size, extraClasses = undefined) {
|
2021-06-30 19:57:46 +02:00
|
|
|
const hasAvatar = !!vm.avatarUrl(size);
|
2021-04-27 15:04:01 +02:00
|
|
|
let avatarClasses = classNames({
|
2021-04-15 15:12:14 +02:00
|
|
|
avatar: true,
|
2021-04-28 13:45:43 +02:00
|
|
|
[`size-${size}`]: true,
|
2021-07-01 11:55:28 +02:00
|
|
|
[`usercolor${vm.avatarColorNumber}`]: !hasAvatar
|
2021-04-15 15:12:14 +02:00
|
|
|
});
|
2021-04-27 15:04:01 +02:00
|
|
|
if (extraClasses) {
|
|
|
|
avatarClasses += ` ${extraClasses}`;
|
|
|
|
}
|
2021-04-15 15:12:14 +02:00
|
|
|
const avatarContent = hasAvatar ? renderImg(vm, size) : text(vm.avatarLetter);
|
2021-07-01 12:11:40 +02:00
|
|
|
const avatar = tag.div({className: avatarClasses}, [avatarContent]);
|
|
|
|
if (hasAvatar) {
|
|
|
|
setAttribute(avatar, "data-avatar-letter", vm.avatarLetter);
|
|
|
|
setAttribute(avatar, "data-avatar-color", vm.avatarColorNumber);
|
|
|
|
}
|
|
|
|
return avatar;
|
2021-04-15 15:12:14 +02:00
|
|
|
}
|
|
|
|
|
2021-06-30 20:04:23 +02:00
|
|
|
export function renderImg(vm, size) {
|
2021-04-15 15:12:14 +02:00
|
|
|
const sizeStr = size.toString();
|
2021-04-28 14:10:54 +02:00
|
|
|
return tag.img({src: vm.avatarUrl(size), width: sizeStr, height: sizeStr, title: vm.avatarTitle});
|
2021-04-15 15:12:14 +02:00
|
|
|
}
|
2021-06-30 20:00:44 +02:00
|
|
|
|
|
|
|
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 11:55:28 +02:00
|
|
|
const avatarColorNumber = parent.getAttribute("data-avatar-color");
|
|
|
|
parent.classList.add(`usercolor${avatarColorNumber}`);
|
2021-06-30 20:00:44 +02:00
|
|
|
const avatarLetter = parent.getAttribute("data-avatar-letter");
|
2021-06-30 20:31:38 +02:00
|
|
|
parent.textContent = avatarLetter;
|
2021-06-30 20:00:44 +02:00
|
|
|
}
|