AvatarRendering

A utility class providing rendering of avatars / resource initials.

Avatar rendering
//<code-header>
fiddle.title = 'Avatar rendering';
CSSHelper.insertRule('.b-resource-wrap { margin-inline-end:1em; transition: margin-inline-end .5s; }', targetElement.getRootNode());
CSSHelper.insertRule('.b-resource-avatar { height: 3.5em;width:3.5em}', targetElement.getRootNode());
CSSHelper.insertRule('.overlap .b-resource-wrap { margin-inline-end : -2em; }', targetElement.getRootNode());
targetElement.innerHTML = '<div id="avatars" class="centeredColumn" style="display:flex;flex-direction:row;width:100%"></div>';
//</code-header>
new SlideToggle({
    insertFirst : targetElement,
    label       : 'Overlap',
    onChange({ value }) {
        targetElement.classList.toggle('overlap', value);
    }
});

const avatarRendering = new AvatarRendering({
    element : targetElement,
    tooltip : {
        onBeforeShow() {
            const
                target = this.triggeredByEvent.target.closest('.b-resource-info'),
                index  = Array.from(target.parentElement.children).indexOf(target);

            this.html = names[index];
        }
    }
});

class Resource extends Model {
    static fields = [
        'name',
        'imageUrl'
    ];

    get initials() {
        return this.name?.split(' ').map(part => part[0]).join('') || '';
    }
}

// Last person has no image so the initials are used
const names = ['Arnold', 'Dave', 'Emilia', 'Gloria', 'Rob', 'Andrew Bird'];

// Generate one avatar per person
names.forEach(name => DomHelper.createElement({
    parent   : 'avatars',
    class    : 'b-resource-wrap',
    children : [
        avatarRendering.getResourceAvatar(new Resource({
            name,
            imageUrl : name.includes('Andrew') ? null : `data/Grid/images/transparent-users/${name.toLowerCase()}.png`
        })
        )
    ]
})
);

Configs

4
colorPrefix: String= b-sch-

Prefix prepended to a supplied color to create a CSS class applied when showing initials.

element: HTMLElement

Element used to listen for load errors. Normally the owning widgets own element.

size: String | Number | null

The requested size of the avatars. If not null then the size is added as width and height to the avatar element whether it is image or initials container

A tooltip config object to enable using a custom tooltip for the avatars. Listen for beforeShow and set your html there.

Properties

6

Class hierarchy

isAvatarRendering: Boolean= truereadonly
Identifies an object as an instance of AvatarRendering class, or subclass thereof.
isAvatarRendering: Boolean= truereadonlystatic
Identifies an object as an instance of AvatarRendering class, or subclass thereof.

Lifecycle

configBase

Functions

15

Other

Returns a DOM config object containing a resource avatar, icon or resource initials. Display priority in that order.

ParameterTypeDescription
optionsAvatarConfig | AvatarConfig[]

A single avatar config object or an array of the same.

Returns: DomConfig

Configuration

applyDefaultsstaticBase

Events

Lifecycle

destroystaticBase

Misc

initClassstaticBase
isOfTypeNamestaticBase
mixinstaticBase

Typedefs

1

An object that describes properties of an avatar.

ParameterTypeDescription
initialsString

Resource initials

colorString

Background color for initials

iconClsString

Icon cls

imageUrlString

Image url

defaultImageUrlString

Default image url, fallback if image fails to load or there is none specified. Leave out to show initials instead.

datasetObject

Dataset to apply to the resulting element

altString

Image description

CSS variables

10
NameDescription
--b-avatar-sizeAvatar size
--b-avatar-initials-text-transformText transform for avatar initials
--b-avatar-initials-font-weightFont weight for avatar initials
--b-avatar-border-radiusBorder radius for avatar
--b-avatar-aspect-ratioAspect ratio for avatar
--b-avatar-icon-font-sizeFont size for avatar icon
--b-avatar-borderBorder for avatar
--b-avatar-backgroundAvatar background. Visible when displaying icon, initials or an image with transparent background
--b-avatar-icon-colorColor for avatar icons
--b-avatar-initials-colorColor for avatar initials