/
admin-image.directive.ts
80 lines (58 loc) · 1.92 KB
/
admin-image.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Directive, OnChanges, Input, Renderer2, ElementRef } from '@angular/core';
import { AdminImageConfig } from './admin-image.config';
import { Md5 } from 'ts-md5';
@Directive({
selector: '[appAdminImage]'
})
export class AdminImageDirective implements OnChanges {
/**
* @param {string} image
*
* source of the image;
* - in the case of user (gr)avatar it's the e-mail address,
* - in the case of project logo it's the image url
*/
@Input() image: string;
/**
* @param {string} type
*
* type of image; you can use it with
* - project
* - user
*/
@Input() type: string;
/**
* image source
*/
source: string;
/**
* in case of an error
*/
onError: string = AdminImageConfig.defaultNotFound;
constructor(private _renderer: Renderer2,
private _ele: ElementRef
) { }
ngOnChanges() {
this.source = this.image;
switch (this.type) {
case 'user':
this.onError = AdminImageConfig.defaultUser;
if (this.image === null || this.image === undefined) {
this.source = AdminImageConfig.defaultUser;
} else {
this.source = location.protocol + '//www.gravatar.com/avatar/' + Md5.hashStr(this.image) + '?d=mp&s=256';
}
break;
case 'project':
this.onError = AdminImageConfig.defaultProject;
if (this.image === null || this.image === undefined) {
this.source = AdminImageConfig.defaultProject;
}
break;
default:
this.source = this.image;
}
this._renderer.setAttribute(this._ele.nativeElement, 'src', this.source);
this._renderer.setAttribute(this._ele.nativeElement, 'onError', 'this.src=\'' + this.onError + '\'');
}
}