2018-08-01 19:43:50 +00:00
|
|
|
import _ from 'underscore';
|
|
|
|
import { getFirstCharacterCapitalized } from '~/lib/utils/text_utility';
|
|
|
|
|
|
|
|
export const DEFAULT_SIZE_CLASS = 's40';
|
|
|
|
export const IDENTICON_BG_COUNT = 7;
|
|
|
|
|
|
|
|
export function getIdenticonBackgroundClass(entityId) {
|
|
|
|
const type = (entityId % IDENTICON_BG_COUNT) + 1;
|
|
|
|
return `bg${type}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getIdenticonTitle(entityName) {
|
|
|
|
return getFirstCharacterCapitalized(entityName) || ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderIdenticon(entity, options = {}) {
|
|
|
|
const { sizeClass = DEFAULT_SIZE_CLASS } = options;
|
|
|
|
|
|
|
|
const bgClass = getIdenticonBackgroundClass(entity.id);
|
|
|
|
const title = getIdenticonTitle(entity.name);
|
|
|
|
|
2018-10-24 19:17:03 +00:00
|
|
|
return `<div class="avatar identicon ${_.escape(sizeClass)} ${_.escape(bgClass)}">${_.escape(
|
|
|
|
title,
|
|
|
|
)}</div>`;
|
2018-08-01 19:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderAvatar(entity, options = {}) {
|
|
|
|
if (!entity.avatar_url) {
|
|
|
|
return renderIdenticon(entity, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { sizeClass = DEFAULT_SIZE_CLASS } = options;
|
|
|
|
|
|
|
|
return `<img src="${_.escape(entity.avatar_url)}" class="avatar ${_.escape(sizeClass)}" />`;
|
|
|
|
}
|