debian-mirror-gitlab/app/assets/javascripts/helpers/avatar_helper.js

36 lines
1 KiB
JavaScript
Raw Normal View History

2020-03-09 13:42:32 +05:30
import { escape } from 'lodash';
2018-11-18 11:00:15 +05:30
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);
2020-03-09 13:42:32 +05:30
return `<div class="avatar identicon ${escape(sizeClass)} ${escape(bgClass)}">${escape(
2018-12-13 13:39:08 +05:30
title,
)}</div>`;
2018-11-18 11:00:15 +05:30
}
export function renderAvatar(entity, options = {}) {
if (!entity.avatar_url) {
return renderIdenticon(entity, options);
}
const { sizeClass = DEFAULT_SIZE_CLASS } = options;
2020-03-09 13:42:32 +05:30
return `<img src="${escape(entity.avatar_url)}" class="avatar ${escape(sizeClass)}" />`;
2018-11-18 11:00:15 +05:30
}