2018-04-04 10:48:47 -07:00
|
|
|
import Component from '@ember/component';
|
2018-09-12 17:06:47 -07:00
|
|
|
import ComputedProperty from '@ember/object/computed';
|
2018-04-04 10:48:47 -07:00
|
|
|
import { IAvatar } from 'wherehows-web/typings/app/avatars';
|
2018-09-12 17:06:47 -07:00
|
|
|
import { alias } from '@ember-decorators/object/computed';
|
|
|
|
import { set } from '@ember/object';
|
|
|
|
import { classNames, tagName, attribute } from '@ember-decorators/component';
|
2018-04-04 10:48:47 -07:00
|
|
|
|
2018-09-12 17:06:47 -07:00
|
|
|
@tagName('img')
|
|
|
|
@classNames('avatar')
|
2018-04-04 10:48:47 -07:00
|
|
|
export default class AvatarImage extends Component {
|
|
|
|
/**
|
|
|
|
* The avatar object to render
|
|
|
|
* @type {IAvatar}
|
|
|
|
*/
|
2018-09-12 17:06:47 -07:00
|
|
|
avatar!: IAvatar;
|
2018-04-04 10:48:47 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the image url for the avatar
|
2018-04-09 19:04:11 -07:00
|
|
|
* @type {ComputedProperty<IAvatar['imageUrl']>}
|
2018-04-04 10:48:47 -07:00
|
|
|
*/
|
2018-09-12 17:06:47 -07:00
|
|
|
@attribute
|
|
|
|
@alias('avatar.imageUrl')
|
|
|
|
src: ComputedProperty<IAvatar['imageUrl']>;
|
2018-04-04 10:48:47 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Aliases the name property on the related avatar
|
|
|
|
* @type {ComputedProperty<IAvatar['name']>}
|
|
|
|
*/
|
2018-09-12 17:06:47 -07:00
|
|
|
@attribute('alt')
|
|
|
|
@alias('avatar.name')
|
|
|
|
name: ComputedProperty<IAvatar['name']>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for image error event
|
|
|
|
* @memberof AvatarImage
|
|
|
|
*/
|
|
|
|
@attribute
|
|
|
|
onerror = (): void => {
|
|
|
|
// Change avatar if an error occurs when the image is loaded
|
|
|
|
this.onImageFallback();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the fallback image for an avatar
|
|
|
|
* @memberof AvatarImage
|
|
|
|
*/
|
|
|
|
onImageFallback(): void {
|
2018-09-14 17:10:45 -07:00
|
|
|
if (!this.isDestroyed) {
|
|
|
|
set(this, 'src', this.avatar.imageUrlFallback);
|
|
|
|
}
|
2018-09-12 17:06:47 -07:00
|
|
|
}
|
2018-04-04 10:48:47 -07:00
|
|
|
}
|