Advertisement
Liliana797979

element builder - js advanced

Oct 11th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Contact {
  2.     constructor(firstName, lastName, phone, email) {
  3.         this.firstName = firstName;
  4.         this.lastName = lastName;
  5.         this.phone = phone;
  6.         this.email = email;
  7.         this._online = false;
  8.     }
  9.  
  10.     get online() {
  11.         return this._online;
  12.     }
  13.  
  14.     set online(value) {
  15.  
  16.         if (this.titleDiv) {
  17.             if (!value) {
  18.                 this.titleDiv.classList.remove("online");
  19.             } else {
  20.                 this.titleDiv.classList.add("online");
  21.             }
  22.         }
  23.         this._online = value;
  24.     }
  25.  
  26.     render(id) {
  27.         const article = document.createElement("article");
  28.         this.titleDiv = document.createElement("div");
  29.         this.titleDiv.classList.add("title");
  30.         this.titleButton = document.createElement("button");
  31.         this.titleButton.innerHTML = "&#8505";
  32.         this.titleDiv.innerHTML = `${this.firstName} ${this.lastName}`;
  33.         this.titleDiv.appendChild(this.titleButton);
  34.  
  35.  
  36.         if (this._online) {
  37.             this.titleDiv.classList.add("online");
  38.         }
  39.  
  40.         this.infoDiv = document.createElement('div');
  41.         this.infoDiv.classList.add("info");
  42.         this.infoDiv.style.display = "none";
  43.         this.infoDiv.innerHTML = `<span>&phone; ${this.phone}</span><span>&#9993; ${this.email}</span>`
  44.  
  45.         this.titleButton.addEventListener("click", ()=> {
  46.             this.infoDiv.style.display = this.infoDiv.style.display == "none" ? "block" : "none";
  47.         })
  48.  
  49.         article.appendChild(this.titleDiv);
  50.         article.appendChild(this.infoDiv);
  51.  
  52.         document.getElementById(id).appendChild(article);
  53.     }
  54.  
  55. }
  56. let contacts = [
  57.     new Contact("Ivan", "Ivanov", "0888 123 456", "i.ivanov@gmail.com"),
  58.     new Contact("Maria", "Petrova", "0899 987 654", "mar4eto@abv.bg"),
  59.     new Contact("Jordan", "Kirov", "0988 456 789", "jordk@gmail.com")
  60. ];
  61. contacts.forEach(c => c.render('main'));
  62.  
  63. // After 1 second, change the online status to true
  64. setTimeout(() => contacts[1].online = true, 2000);
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement