-
Notifications
You must be signed in to change notification settings - Fork 293
/
list-item.component.ts
77 lines (65 loc) · 1.88 KB
/
list-item.component.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
import { Component, Input, HostBinding } from '@angular/core';
export interface ListItem {
id: string;
index: number;
name: string;
gender: string;
age: number;
email: string;
phone: string;
address: string;
company: string;
}
@Component({
selector: 'list-item',
template: `
<div class="avatar">{{item.index}}</div>
<div class="item-content">
<div class="name">{{item.name}}</div>
<div>
<span class="badge">{{item.age}}/{{item.gender.substr(0, 1).toUpperCase()}}</span>
<span>{{item.email}} | {{item.phone}}</span>
</div>
<div>{{item.address}}</div>
</div>
`,
styleUrls: ['./list-item.scss']
})
export class ListItemComponent {
@Input()
public item: ListItem;
@Input()
public randomWidth: boolean = false;
@Input()
public randomHeight: boolean = false;
@HostBinding('style.width')
public get styleWidth(): string {
if (!this.randomWidth)
{
return undefined;
}
return (100 + this.stringToHash(this.item.name) % 900).toString() + 'px';
}
private static Seed: number;
public static ResetSeed(): void {
ListItemComponent.Seed = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
}
@HostBinding('style.height')
public get styleHeight(): string {
if (!this.randomHeight) {
return undefined;
}
return (50 + this.stringToHash(this.item.name) % 450).toString() + 'px';
}
@HostBinding('style.border')
public get styleBorder(): string {
if (!this.randomWidth && !this.randomHeight) {
return undefined;
}
return '1px solid black';
}
private stringToHash(text: string): number {
return [].reduce.call(text, (accumulator, character) => (accumulator << 5) - accumulator + character.charCodeAt(0), 0) ^ ListItemComponent.Seed;
}
}
ListItemComponent.ResetSeed();