| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* globals module, require */ | |
| 2 | |
| 3 "use strict"; | |
| 4 | |
| 5 // Custom Elements ponyfill (a polyfill triggered on demand) | |
| 6 const customElementsPonyfill = require("document-register-element/pony"); | |
| 7 if (typeof customElements !== "object") | |
| 8 customElementsPonyfill(window); | |
| 9 | |
| 10 // external dependencies | |
| 11 const {default: HyperHTMLElement} = require("hyperhtml-element/cjs"); | |
| 12 | |
| 13 // provides a unique-id suffix per each component | |
| 14 let counter = 0; | |
| 15 | |
| 16 // common Custom Element class to extend | |
| 17 class IOElement extends HyperHTMLElement | |
| 18 { | |
| 19 // get a unique ID or, if null, set one and returns it | |
| 20 static getID(element) | |
|
saroyanm
2018/03/20 20:08:19
Question: Why is the generation of unique ID is re
a.giammarchi
2018/03/21 07:27:19
It's not, it's aria related question. Most of the
a.giammarchi
2018/03/21 08:15:57
I've forgotten an important detail. Once you have
saroyanm
2018/03/21 11:22:21
We could also wrap element inside of the label tag
saroyanm
2018/03/21 11:22:22
Noted.
| |
| 21 { | |
| 22 return element.getAttribute("id") || IOElement.setID(element); | |
| 23 } | |
| 24 | |
| 25 // set a unique ID to a generic element and returns the ID | |
| 26 static setID(element) | |
| 27 { | |
| 28 const id = `${element.nodeName.toLowerCase()}-${counter++}`; | |
| 29 element.setAttribute("id", id); | |
| 30 return id; | |
| 31 } | |
| 32 | |
| 33 // lazily retrieve or define a custom element ID | |
| 34 get id() | |
| 35 { | |
| 36 return IOElement.getID(this); | |
| 37 } | |
| 38 | |
| 39 // whenever an element is created, render its content once | |
| 40 created() { this.render(); } | |
| 41 | |
| 42 // by default, render is a no-op | |
| 43 render() {} | |
| 44 } | |
| 45 | |
| 46 // whenever an interpolation with ${{i18n: 'string-id'}} is found | |
| 47 // transform such value into the expected content | |
| 48 // example: | |
| 49 // render() { | |
| 50 // return this.html`<div>${{i18n:'about-abp'}}</div>`; | |
| 51 // } | |
| 52 const {setElementText} = ext.i18n; | |
| 53 IOElement.intent("i18n", id => | |
|
saroyanm
2018/03/20 20:08:19
What does intent do ? I couldn't find it in the do
a.giammarchi
2018/03/21 07:27:19
In HyperHTMLElement, being `define(...)` the metho
saroyanm
2018/03/21 11:22:21
Suggestion: Maybe we can document that somewhere,
a.giammarchi
2018/03/21 14:21:32
Done.
| |
| 54 { | |
| 55 const fragment = document.createDocumentFragment(); | |
| 56 setElementText(fragment, id); | |
| 57 return fragment; | |
| 58 }); | |
| 59 | |
| 60 module.exports = IOElement; | |
| OLD | NEW |