-
Notifications
You must be signed in to change notification settings - Fork 1
/
userCard.js
155 lines (134 loc) · 3.26 KB
/
userCard.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const template = document.createElement("template");
template.innerHTML = `
<style>
.user-card {
line-height: 1;
font-family: 'Arial', sans-serif;
background: $f4f4f4;
width: 500px;
display: grid;
grid-template-columns: 1fr 2fr;
grid-gap: 10px;
margin-bottom: 15px;
border-bottom: darkorchid 5px solid;
}
.user-card img {
width: 100%;
}
.user-card button {
cursor: pointer;
background: darkorchid;
color: #fff;
border: 0;
border-radius: 5px;
padding: 5px 10px;
}
</style>
<div class="user-card">
<img />
<div>
<h3></h3>
<div class="info">
<p><slot name="email"></p>
<p><slot name="phone"></p>
</div>
<button id='toggle-info'>Hide Info</button>
</div>
</div>
`;
class UserCard extends HTMLElement {
constructor() {
super();
this._verbose = false;
this.showInfo = true;
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
static get observedAttributes() {
return ["verbose", "name", "token", "userid"];
}
get token() {
return this._token;
}
set token(value) {
this._token = value;
console.log("token", this._token);
}
get name() {
return this._name;
}
set name(value) {
this._name = value;
this.shadowRoot.querySelector("h3").innerText = this._name;
}
attributeChangedCallback(name, oldValue, newValue) {
this.log(`${name} value changed from ${oldValue} to ${newValue}`);
switch (name) {
case "verbose":
this._verbose = newValue === "true";
this.log("version 1.0.44");
break;
case "name":
this._name = newValue;
this.shadowRoot.querySelector("h3").innerText = this._name;
break;
case "userid":
this._userid = newValue;
let gender = "men";
let id = newValue;
if (Number(newValue) > 100) {
id = Number(newValue) - 100;
gender = "women";
}
this.shadowRoot.querySelector(
"img"
).src = `https://randomuser.me/api/portraits/${gender}/${id}.jpg`;
break;
}
}
toggleInfo() {
this.showInfo = !this.showInfo;
const info = this.shadowRoot.querySelector(".info");
const toggleBtn = this.shadowRoot.querySelector("#toggle-info");
if (this.showInfo) {
info.style.display = "block";
toggleBtn.innerText = "Hide Info";
} else {
info.style.display = "none";
toggleBtn.innerText = "Show Info";
}
const event = new CustomEvent(
"componentChanged",
{
detail: {
userid: this._userid,
name: this._name,
time: new Date(),
},
bubbles: true,
cancelable: true
}
);
this.dispatchEvent(event);
}
connectedCallback() {
this.shadowRoot
.querySelector("#toggle-info")
.addEventListener("click", this.toggleInfo.bind(this));
}
disconnectedCallback() {
this.shadowRoot
.querySelector("#toggle-info")
.removeEventListener("click", this.toggleInfo.bind(this));
}
log(msg, arg) {
if (this._verbose) {
if (arg) {
console.log(msg, arg);
} else {
console.log(msg);
}
}
}
}
customElements.define("user-card", UserCard);