daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Nov 23rd, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. class HTMLElementPlus extends HTMLElement {
  4.  
  5.     static defaultAttributeValue(name) {
  6.         return;
  7.     }
  8.  
  9.     static parseAttributeValue(name, value) {
  10.         return value;
  11.     }
  12.  
  13.     constructor() {
  14.         super();
  15.         this.refs = new Proxy(
  16.             {},
  17.             {
  18.                 get: this.__getFromShadowRoot.bind(this)
  19.             }
  20.         );
  21.  
  22.         // Gets populated by attributeChangedCallback
  23.         this.__attributesMap = {};
  24.  
  25.         this.__waitingOnAttr = (this.constructor.observedAttributes
  26.             ? this.constructor.observedAttributes
  27.             : []
  28.         ).filter(name => {
  29.             this.__attributesMap[name] = this.constructor.defaultAttributeValue(name);
  30.             return !!this.attributes.getNamedItem(name);
  31.         });
  32.     }
  33.  
  34.     __getFromShadowRoot(target, name) {
  35.         return this.shadowRoot.querySelector('[ref="' + name + '"]');
  36.     }
  37.  
  38.     attributeChangedCallback(attr, oldValue, newValue) {
  39.         this.__attributesMap[attr] = this.constructor.parseAttributeValue(
  40.             attr,
  41.             newValue
  42.         );
  43.  
  44.         if (this.__waitingOnAttr.length) {
  45.             const index = this.__waitingOnAttr.indexOf(attr);
  46.             if (index !== -1) {
  47.                 // Remove it from array.
  48.                 this.__waitingOnAttr.splice(index, 1);
  49.             }
  50.         }
  51.  
  52.         // All attributes parsed
  53.         if (this.__waitingOnAttr.length === 0) {
  54.             this.allAttributesChangedCallback(this.__attributesMap);
  55.         }
  56.     }
  57.  
  58.     emitEvent(name, detail) {
  59.         this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true }));
  60.     }
  61.  
  62.     allAttributesChangedCallback() {}
  63. }
  64.  
  65. window.HTMLElementPlus = HTMLElementPlus;
RAW Paste Data
Top