Skip to content

Commit ab91f71

Browse files
author
Fernando Doglio
committed
adding event handler code and new components to showcase data-sharing using events
1 parent ff4c3a9 commit ab91f71

10 files changed

+201
-10
lines changed

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</head>
66
<body>
77
<hm-card withShadow>
8-
<hm-card>test</hm-card>
8+
<hm-card>Test</hm-card>
99

1010
<h1>This is a title</h1>
1111

index2.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="./src/index.js" type="module"></script>
5+
</head>
6+
<body>
7+
<hm-card withShadow width="50" centered>
8+
<hm-list-random-people></hm-list-random-people>
9+
</hm-card>
10+
</body>
11+
</html>

src/Button.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as CC from './CustomComponent.js'
2+
import * as EH from './EvenHandler.js'
23

34
const CustomComponent = CC.default
5+
const EventHandler = EH.default
46

57
export default class HMButton extends CustomComponent{
68
static observedAttributes = [];
@@ -20,6 +22,8 @@ export default class HMButton extends CustomComponent{
2022
margin: 5px;
2123
}
2224
`;
25+
26+
EventHandler.subscribeToEvent('show', this)
2327
}
2428

2529

src/Card.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ export default class HMCard extends CustomComponent{
1818
.hm-card.with-shadow {
1919
box-shadow: 5px 10px #cbbaba
2020
}
21+
22+
.centered {
23+
margin: 0 auto;
24+
}
25+
26+
.w-80 {
27+
width: 80%;
28+
}
29+
.w-50 {
30+
width: 50%;
31+
}
32+
.w-70 {
33+
width: 70%;
34+
}
2135
`;
2236
}
2337

@@ -31,9 +45,17 @@ export default class HMCard extends CustomComponent{
3145
if(this._attributes.withshadow != null) {
3246
elemClass.push("with-shadow")
3347
}
48+
49+
if(this._attributes.width != null) {
50+
elemClass.push("w-" + this._attributes.width)
51+
}
52+
53+
if(this._attributes.centered != null) {
54+
elemClass.push('centered')
55+
}
56+
3457
elemClass.push("hm-card")
3558

36-
let mySlotID = Date.now()
3759
wrapper.innerHTML = `
3860
<div class="${elemClass.join(" ")}">
3961
<slot ></slot>

src/CustomComponent.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import * as EH from './EvenHandler.js'
2+
3+
const EventHandler = EH.default
4+
15
export default class CustomComponent extends HTMLElement {
26
static ID = 0;
37
constructor(){
@@ -22,6 +26,10 @@ export default class CustomComponent extends HTMLElement {
2226
console.log("My custom component got instantiated! (", this.compID, ")")
2327
}
2428

29+
handleEvent(event, params) {
30+
console.log("The event ", event, " was triggered with params", params)
31+
}
32+
2533
/**
2634
* Lifecycle method, called whenever an observed property changes
2735
*/
@@ -36,9 +44,14 @@ export default class CustomComponent extends HTMLElement {
3644
return null;
3745
}
3846

39-
display() {
47+
display(force=false) {
48+
if(force) {
49+
[...this.mainComp.children]
50+
.forEach(this.mainComp.removeChild.bind(this.mainComp))
51+
this.isAttached = false;
52+
}
4053
if(this.isAttached) {
41-
console.log("already rendered...")
54+
console.log("Already rendered...")
4255
return;
4356
}
4457
console.log("Displaying...", this.compName)
@@ -82,6 +95,7 @@ export default class CustomComponent extends HTMLElement {
8295
connectedCallback() {
8396
this.setUpAccessors()
8497
this.display()
98+
EventHandler.triggerEvent('show')
8599
for(let i = 0; i < this.childNodes.length; i++) {
86100
let child = this.childNodes[i]
87101
this.append(child)

src/EvenHandler.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
export default class EventHandler {
3+
static events = {}
4+
5+
static subscribeToEvent(event, elem) {
6+
if(!EventHandler.events[event]) {
7+
EventHandler.events[event] = []
8+
}
9+
EventHandler.events[event].push(elem)
10+
}
11+
12+
static triggerEvent(event, params) {
13+
if(!EventHandler.events[event]) return;
14+
EventHandler.events[event].forEach(elem => {
15+
if(elem.handleEvent) {
16+
elem.handleEvent(event, params)
17+
}
18+
})
19+
}
20+
}

src/Stack.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ export default class HMStack extends CustomComponent{
2727
// the wrapper element
2828
const wrapper = document.createElement("div")
2929

30-
31-
32-
33-
34-
const direction = this._attributes.dir // this.getAttribute("dir")
30+
const direction = this._attributes.dir
3531
let elemClasses = []
3632

3733
switch(direction) {

src/customWC/ListPeople.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
9+
export default class HMListPeople extends CustomComponent{
10+
static observedAttributes = [];
11+
constructor() {
12+
super()
13+
this.compName = "ListPeople"
14+
this.customStyle = `
15+
.custom-comp{
16+
padding: 10px;
17+
display: block;
18+
}
19+
li {
20+
padding: 5px;
21+
border: 2px solid #aaa;
22+
margin-top: 3px;
23+
}
24+
ul {
25+
list-style-type: none;
26+
}
27+
`;
28+
29+
this.people = []
30+
EventHandler.subscribeToEvent('person-added', this)
31+
}
32+
33+
handleEvent(event, data) {
34+
if(event !== 'person-added') return;
35+
this.people.push(data)
36+
this.display(true)
37+
}
38+
39+
render() {
40+
41+
// the wrapper element
42+
const wrapper = document.createElement("div")
43+
44+
const list = document.createElement("ul")
45+
46+
this.people.forEach( p => {
47+
const elem = document.createElement('li')
48+
elem.textContent = p
49+
list.appendChild(elem)
50+
})
51+
52+
wrapper.appendChild(list)
53+
54+
return wrapper
55+
}
56+
57+
58+
}
59+
60+
customElements.define("hm-list-people", HMListPeople)

src/customWC/ListRandomPeople.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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)

src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
export * as HMButtom from './Button.js'
33
export * as HMCard from './Card.js'
44
export * as HMStack from './Stack.js'
5-
export * as HMTextInput from './TextInput.js'
5+
export * as HMTextInput from './TextInput.js'
6+
7+
export * as HMListPeople from './customWC/ListPeople.js'
8+
export * as HMListRandomPeople from './customWC/ListRandomPeople.js'

0 commit comments

Comments
 (0)