Ghost/ghost/admin/app/components/gh-member-avatar.js
Kevin Ansfield 266064fc8f Fixed <GhMemberAvatar> expecting an EmberObject object
no issue

- it should be possible to pass a POJO rather than an EmberObject object so the component is usable with raw data like we have with member events
- switched to using `get()` rather than assuming the passed in object has it's own `.get()` method
2022-01-27 15:09:08 +00:00

38 lines
1.1 KiB
JavaScript

import Component from '@glimmer/component';
import {get} from '@ember/object';
import {htmlSafe} from '@ember/template';
const stringToHslColor = function (str, saturation, lightness) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var h = hash % 360;
return 'hsl(' + h + ', ' + saturation + '%, ' + lightness + '%)';
};
export default class GhMemberAvatarComponent extends Component {
get memberName() {
let {member} = this.args;
// can be given a proxy object from a sparse array so get is required
return get(member, 'name') || get(member, 'email') || 'NM';
}
get backgroundStyle() {
let color = stringToHslColor(this.memberName, 75, 55);
return htmlSafe(`background-color: ${color}`);
}
get initials() {
if (this.memberName === 'NM') {
return 'NM';
}
let names = this.memberName.split(' ');
let intials = names.length > 1 ? [names[0][0], names[names.length - 1][0]] : [names[0][0]];
return intials.join('').toUpperCase();
}
}