krle997

ntf

Jun 8th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const $ = (k) => document.getElementById(k);
  2. const mrnd = () => Math.random();
  3. const save = (k, v) => localStorage.setItem(k, JSON.stringify(v));
  4. const raf = (cb) => requestAnimationFrame(cb);
  5.  
  6. const notification = {};
  7. class Notification {
  8.   constructor(id, header, body) {
  9.     this._id = id;
  10.     this._header = header;
  11.     this._body = body;
  12.     this._opacity = 1;
  13.     this._interval = undefined;
  14.   }
  15.  
  16.   get id() { return this._id; }
  17.   get header() { return this._header; }
  18.   get body() { return this._body; }
  19.   get opacity() { return this._opacity; }
  20.   get interval() { return this._interval; }
  21.  
  22.   set opacity(v) {
  23.     this._opacity = v;
  24.     $(`notification${this.id}`).style.opacity = p.opacity;
  25.   }
  26.   set interval(v) {
  27.     this._interval = v;
  28.   }
  29.  
  30.   static fadeOut(k) {
  31.     const p = notification[k];
  32.  
  33.     p.opacity = p.opacity - 0.01;
  34.  
  35.     if(p.opacity <= 0) {
  36.       $(`notification${p.id}`).parentNode.removeChild($(`notification${p.id}`));
  37.       delete notification[k];
  38.       save("notifications", notification);
  39.     }
  40.     else { p.interval = raf(() => this.fadeOut(k)); }
  41.   }
  42.   static close(k) {
  43.     const p = notification[k];
  44.  
  45.     $(`notification${p.id}Close`).onclick = () => false;
  46.     p.interval = raf(() => this.fadeOut(k));
  47.   }
  48.   static render(k) {
  49.     const p = notification[k];
  50.  
  51.     $("notificationContainer").insertAdjacentHTML("beforeend", `
  52.       <div class="notification" id="notification${p.id}">
  53.         <div class="notification-header fwhite">
  54.           ${p.header}
  55.           <div class="notification-btn f16" id="notification${p.id}Close">X</div>
  56.         </div>
  57.         <div class="notification-content">
  58.           ${p.body}
  59.         </div>
  60.       </div>
  61.     `);
  62.  
  63.     $(`notification${p.id}Close`).onclick = () => this.close(k);
  64.     save("notifications", notification);
  65.   }
  66.   static generate(header, body) {
  67.     const k = mrnd();
  68.     notification[k] = new Notification(k, header, body);
  69.     this.render(k);
  70.   }
  71. }
Add Comment
Please, Sign In to add comment