Added onClick and onEvent shorthand event handlers to reduce duplicate code.

This commit is contained in:
2020-08-21 22:13:06 -07:00
parent b0b9a83cc6
commit 138513ce2b
2 changed files with 70 additions and 2 deletions

View File

@ -139,6 +139,59 @@ class IgniteTemplate {
return this;
}
onClick(func) {
this.on("click", func);
return this;
}
onEnter(func) {
var eventName = "keydown";
if (!this.events[eventName]) {
this.events[eventName] = [];
}
if (func instanceof IgniteProperty) {
this.callbacks.push(func.attach((oldValue, newValue) => {
//Create a new wrapped function to check for the enter key being pressed.
var wrapped = (e) => {
if (e.key === 'Enter') {
newValue(e);
}
};
//Store the wrapped functino so that it's the old value next time around
//and the old event can be removed in the future
func._value = wrapped;
//Invoke the on event changed with the old value and our wrapped value.
this.onEventChanged(oldValue, wrapped, eventName)
}));
//Create the initial wrapper
var old = func._value;
var wrapped = (e) => {
if (e.key === 'Enter') {
old(e);
}
};
//Store the wrapped so that it's the old value next time around.
func._value = wrapped;
this.events[eventName].push(wrapped);
} else {
this.on(eventName, (e) => {
if (e.key === 'Enter') {
func(e);
}
});
}
return this;
}
/**
* Sets a css style on this template and adds it to the element
* once it's constructed.
@ -414,6 +467,14 @@ class a extends IgniteTemplate {
}
}
class input extends IgniteTemplate {
constructor(...items) {
super(items);
this.tagName = "input";
}
}
class html extends IgniteTemplate {
constructor(code) {
super([]);
@ -583,5 +644,6 @@ export {
html,
list,
a,
input,
slot
};