Skip to content

Commit

Permalink
Added event . helping operator
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Mar 5, 2024
1 parent f4a873d commit 257b4a0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h2>Yee</h2>



/**

const myHTML = html`
<!-- ${waitforme()} -->
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jdomjs",
"version": "3.0.2",
"version": "3.0.4",
"description": "A wrapper for query selector and html elements",
"main": "index.js",

Expand Down
30 changes: 28 additions & 2 deletions src/template/TemplateDOMAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class TemplateDOMAdapter {
}

if (value instanceof Hook) {
value.listeners.push(() => {
value.addListener(() => {
setValue(key, value.value)
})
setValue(key, value.value)
Expand All @@ -130,7 +130,33 @@ export default class TemplateDOMAdapter {
elem.value = model.value
}

Object.entries(events).forEach(([key, value]) => elem.addEventListener(key, value))
for (const [key, value] of Object.entries(events)) {
const eventNameParts = key.split('.')
const eventName = eventNameParts.shift()
let handler = typeof value === 'function' ? value : () => {}

if (eventNameParts.length > 0) {
const events = []
for (const part of eventNameParts) {
switch (part.toLowerCase()) {
case 'prevent':
events.push(e => e.preventDefault())
break
case 'stop':
events.push(e => e.stopPropagation())
break
}
}

const oldHandler = handler
handler = e => {
events.forEach(ev => ev(e))
oldHandler(e)
}
}

elem.addEventListener(eventName, handler)
}
onCreate(elem)
}

Expand Down

0 comments on commit 257b4a0

Please sign in to comment.