-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprofile.js
More file actions
41 lines (41 loc) · 1.35 KB
/
Copy pathprofile.js
File metadata and controls
41 lines (41 loc) · 1.35 KB
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
class ProfilePhotoSubject {
constructor() {
this.subscribersList = [];
}
subscribe(element) {
this.subscribersList.push(element);
}
emitChanges(imgBase64) {
this.subscribersList.forEach(subscriber => {
subscriber.updateElement(imgBase64);
});
}
}
class ProfilePhotoObserver {
constructor(id) {
this.elementId = id;
}
updateElement(imgBase64) {
let profPic = document.getElementById(this.elementId);
profPic.src = imgBase64;
}
}
const profilePhotoSubject = new ProfilePhotoSubject();
const profilePhotoObserver1 = new ProfilePhotoObserver('profile_pic_1');
const profilePhotoObserver2 = new ProfilePhotoObserver('profile_pic_2');
profilePhotoSubject.subscribe(profilePhotoObserver1);
profilePhotoSubject.subscribe(profilePhotoObserver2);
let uploadBtn = document.getElementById('upload_btn');
let uploadInput = document.getElementById('upload_input');
uploadBtn.addEventListener("click", () => {
uploadInput.click();
});
uploadInput.addEventListener("change", (event) => {
if (event.target.files) {
let reader = new FileReader();
reader.readAsDataURL(event.target.files[0]);
reader.onload = (loadEvent) => {
profilePhotoSubject.emitChanges(loadEvent.target.result);
};
}
});