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

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import { escape } from 'lodash';
2020-05-24 23:13:21 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
2021-03-11 19:13:27 +05:30
import { getFirstCharacterCapitalized } from '~/lib/utils/text_utility';
2018-11-18 11:00:15 +05:30
export const DEFAULT_SIZE_CLASS = 's40';
export const IDENTICON_BG_COUNT = 7;
export function getIdenticonBackgroundClass(entityId) {
2020-05-24 23:13:21 +05:30
// If a GraphQL string id is passed in, convert it to the entity number
const id = typeof entityId === 'string' ? getIdFromGraphQLId(entityId) : entityId;
const type = (id % IDENTICON_BG_COUNT) + 1;
2018-11-18 11:00:15 +05:30
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-13 15:44:24 +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-13 15:44:24 +05:30
return `<img src="${escape(entity.avatar_url)}" class="avatar ${escape(sizeClass)}" />`;
2018-11-18 11:00:15 +05:30
}