Browse Source

Show identifier image if avatar cannot be loaded.

pull/932/head
Sebastian 4 years ago
parent
commit
6898389e0e
  1. 2
      frontend/src/app/framework/angular/avatar.component.html
  2. 18
      frontend/src/app/framework/angular/avatar.component.ts
  3. 77
      frontend/src/app/framework/angular/avatar.stories.tsx
  4. 2
      frontend/src/app/theme/_common.scss

2
frontend/src/app/framework/angular/avatar.component.html

@ -1 +1 @@
<img *ngIf="imageSource" [style.width]="imageSize" [style.height]="imageSize" [src]="imageSource | sqxSafeUrl"> <img *ngIf="imageSource" [style.width]="imageSize" [style.height]="imageSize" [src]="imageSource | sqxSafeUrl" (error)="unsetImage()">

18
frontend/src/app/framework/angular/avatar.component.ts

@ -27,9 +27,15 @@ export class AvatarComponent implements OnChanges {
public imageSource?: string | null; public imageSource?: string | null;
public imageSize = '50px'; public imageSize = '50px';
public actualImage: string | undefined | null;
public ngOnChanges(changes: SimpleChanges) { public ngOnChanges(changes: SimpleChanges) {
if (changes['image']) {
this.actualImage = this.image;
}
if (changes['image'] || changes['identifier']) { if (changes['image'] || changes['identifier']) {
this.imageSource = this.image || this.createSvg(); this.updateSource();
} }
if (changes['size']) { if (changes['size']) {
@ -37,6 +43,16 @@ export class AvatarComponent implements OnChanges {
} }
} }
private updateSource() {
this.imageSource = this.actualImage || this.createSvg();
}
public unsetImage() {
this.actualImage = null;
this.updateSource();
}
private createSvg() { private createSvg() {
if (this.identifier) { if (this.identifier) {
return `data:image/svg+xml;utf8,${picasso(this.identifier)}`; return `data:image/svg+xml;utf8,${picasso(this.identifier)}`;

77
frontend/src/app/framework/angular/avatar.stories.tsx

@ -0,0 +1,77 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { moduleMetadata } from '@storybook/angular';
import { Meta, Story } from '@storybook/angular/types-6-0';
import { AvatarComponent, SqxFrameworkModule } from '@app/framework';
export default {
title: 'Framework/Avatar',
component: AvatarComponent,
argTypes: {
image: {
control: 'text',
},
identifier: {
control: 'text',
},
size: {
control: 'number',
},
},
decorators: [
moduleMetadata({
imports: [
BrowserAnimationsModule,
SqxFrameworkModule,
SqxFrameworkModule.forRoot(),
],
}),
],
} as Meta;
const Template: Story<AvatarComponent> = (args: AvatarComponent) => ({
props: args,
});
export const Empty = Template.bind({});
Empty.args = {
};
export const Image = Template.bind({});
Image.args = {
image: 'https://via.placeholder.com/50',
};
export const InvalidImage = Template.bind({});
InvalidImage.args = {
image: 'https://invalid-url',
};
export const Identifier = Template.bind({});
Identifier.args = {
identifier: 'image',
};
export const Large = Template.bind({});
Large.args = {
identifier: 'image',
size: 200,
};
export const InvalidImageWithFallback = Template.bind({});
InvalidImageWithFallback.args = {
image: 'https://invalid-url',
identifier: 'image',
};

2
frontend/src/app/theme/_common.scss

@ -22,7 +22,7 @@ body {
} }
hr { hr {
background-color: $color-border; border-color: $color-border;
} }
// Common style for user email. // Common style for user email.

Loading…
Cancel
Save