AvatarRendering
A utility class providing rendering of avatars / resource initials.
//<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
Configs
4Prefix prepended to a supplied color to create a CSS class applied when showing initials.
Element used to listen for load errors. Normally the owning widgets own element.
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
Properties
6Class hierarchy
Functions
15
Functions
15Other
Returns a DOM config object containing a resource avatar, icon or resource initials. Display priority in that order.
| Parameter | Type | Description |
|---|---|---|
options | AvatarConfig | AvatarConfig[] | A single avatar config object or an array of the same. |
Configuration
Events
Typedefs
1
Typedefs
1An object that describes properties of an avatar.
| Parameter | Type | Description |
|---|---|---|
initials | String | Resource initials |
color | String | Background color for initials |
iconCls | String | Icon cls |
imageUrl | String | Image url |
defaultImageUrl | String | Default image url, fallback if image fails to load or there is none specified. Leave out to show initials instead. |
dataset | Object | Dataset to apply to the resulting element |
alt | String | Image description |
CSS variables
10
CSS variables
10| Name | Description |
|---|---|
--b-avatar-size | Avatar size |
--b-avatar-initials-text-transform | Text transform for avatar initials |
--b-avatar-initials-font-weight | Font weight for avatar initials |
--b-avatar-border-radius | Border radius for avatar |
--b-avatar-aspect-ratio | Aspect ratio for avatar |
--b-avatar-icon-font-size | Font size for avatar icon |
--b-avatar-border | Border for avatar |
--b-avatar-background | Avatar background. Visible when displaying icon, initials or an image with transparent background |
--b-avatar-icon-color | Color for avatar icons |
--b-avatar-initials-color | Color for avatar initials |