|
| 1 | +import * as CC from '../CustomComponent.js' |
| 2 | +import * as EH from '../EvenHandler.js' |
| 3 | + |
| 4 | +const CustomComponent = CC.default |
| 5 | +const EventHandler = EH.default |
| 6 | + |
| 7 | + |
| 8 | +const randomFirstNames = ["John", "Daniel", "Jonathan", "Steve", "Bruce", "Sarah", "Maria", "Laura"] |
| 9 | +const randomLastNames = ["Meyers", "Reyes", "Toriro", "Jhons", "Rogers", "Banner", "Parker"] |
| 10 | + |
| 11 | +export default class HMListRandomPeople extends CustomComponent{ |
| 12 | + static observedAttributes = []; |
| 13 | + constructor() { |
| 14 | + super() |
| 15 | + this.compName = "ListRandomPeople" |
| 16 | + this.customStyle = ` |
| 17 | + .custom-comp{ |
| 18 | + padding: 10px; |
| 19 | + display: block; |
| 20 | + color: red; |
| 21 | + } |
| 22 | + .legend { |
| 23 | + display: block; |
| 24 | + } |
| 25 | + button { |
| 26 | + margin: 5px; |
| 27 | + } |
| 28 | + `; |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + generateRandomPerson() { |
| 33 | + const nIndex = Math.round(Math.random() * (randomFirstNames.length - 1)) |
| 34 | + const lnIndex = Math.round(Math.random() * (randomLastNames.length - 1)) |
| 35 | + const name = randomFirstNames[nIndex] + " " + randomLastNames[lnIndex] |
| 36 | + EventHandler.triggerEvent('person-added', name) |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + render() { |
| 41 | + |
| 42 | + // the wrapper element |
| 43 | + const wrapper = document.createElement("div") |
| 44 | + |
| 45 | + const buttonElem = document.createElement('button') |
| 46 | + buttonElem.textContent = "Generate new person" |
| 47 | + |
| 48 | + buttonElem.addEventListener("click", this.generateRandomPerson.bind(this)) |
| 49 | + |
| 50 | + const list = document.createElement('hm-list-people') |
| 51 | + |
| 52 | + wrapper.appendChild(buttonElem) |
| 53 | + wrapper.appendChild(list) |
| 54 | + |
| 55 | + return wrapper |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +customElements.define("hm-list-random-people", HMListRandomPeople) |
0 commit comments