Skip to content

Commit 19c181c

Browse files
committed
support scripts in event bindings
1 parent ff2b33e commit 19c181c

3 files changed

Lines changed: 36 additions & 28 deletions

File tree

sample/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ <h1>Hello World</h1>
7070
<h1>Hello World</h1>
7171
<div>[[this.ctx]]</div>
7272
<template repeat:myitem="[[this.list]]">
73-
<button @click="{{this.ctx = myitem}}"
74-
$aaaonclick="[['this.getRootNode().host.ctx = \'' + myitem + '\'']]">[[myitem]] - [[index]]</button>
73+
<button @click="{{this.ctx = myitem}}">[[myitem]] - [[index]]</button>
7574
</template>
7675
</template>
7776
</node-projects-dce>

src/BaseCustomWebComponent.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -232,29 +232,37 @@ export class BaseCustomWebComponentNoAttachedTemplate extends HTMLElement {
232232
b(true, false);
233233
}
234234
} else if (a.value[0] === '{' && a.value[1] === '{' && a.value[a.value.length - 1] === '}' && a.value[a.value.length - 2] === '}') {
235-
const attributeValues = a.value.substring(2, a.value.length - 2).split('::');
236-
let nm = a.name;
237-
if (nm[0] == '.')
238-
nm = nm.substring(1);
239-
let value = attributeValues[0];
240-
let event = (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) ? 'input' : (node instanceof HTMLSelectElement ? 'change' : nm + '-changed');
241-
if (attributeValues.length > 1 && attributeValues[1])
242-
event = attributeValues[1];
243-
const camelCased = nm.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
244-
let noNull = false;
245-
if (value[0] === '?') {
246-
value = value.substring(1);
247-
noNull = true;
248-
}
249-
const b = (firstRun?: boolean, onlyWhenChanged?: boolean) => this._bindingSetNodeValue(firstRun, node, a, camelCased, value, repeatBindingItems, removeAttributes, host, context, noNull, onlyWhenChanged);
250-
this._bindings.push([b, null]);
251-
b(true, false);
252-
if (event) {
253-
for (let x of event.split(';')) {
254-
if (node[x] instanceof TypedEvent)
255-
(<TypedEvent<void>>node[x]).on((e) => this._bindingsSetValue(host ?? this, value.replaceAll('?', ''), (<HTMLInputElement>node)[camelCased], context, repeatBindingItems));
256-
else
257-
node.addEventListener(x, (e) => this._bindingsSetValue(host ?? this, value.replaceAll('?', ''), (<HTMLInputElement>node)[camelCased], context, repeatBindingItems));
235+
if (a.name[0] === '@') {
236+
const event = a.name.substring(1);
237+
if (node[event] instanceof TypedEvent)
238+
(<TypedEvent<void>>node[event]).on((e) => this._bindingRunEvalInt(a.value.substring(2, a.value.length - 2), repeatBindingItems, <any>e, context));
239+
else
240+
node.addEventListener(event, (e) => this._bindingRunEvalInt(a.value.substring(2, a.value.length - 2), repeatBindingItems, e, context));
241+
} else {
242+
const attributeValues = a.value.substring(2, a.value.length - 2).split('::');
243+
let nm = a.name;
244+
if (nm[0] == '.')
245+
nm = nm.substring(1);
246+
let value = attributeValues[0];
247+
let event = (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) ? 'input' : (node instanceof HTMLSelectElement ? 'change' : nm + '-changed');
248+
if (attributeValues.length > 1 && attributeValues[1])
249+
event = attributeValues[1];
250+
const camelCased = nm.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
251+
let noNull = false;
252+
if (value[0] === '?') {
253+
value = value.substring(1);
254+
noNull = true;
255+
}
256+
const b = (firstRun?: boolean, onlyWhenChanged?: boolean) => this._bindingSetNodeValue(firstRun, node, a, camelCased, value, repeatBindingItems, removeAttributes, host, context, noNull, onlyWhenChanged);
257+
this._bindings.push([b, null]);
258+
b(true, false);
259+
if (event) {
260+
for (let x of event.split(';')) {
261+
if (node[x] instanceof TypedEvent)
262+
(<TypedEvent<void>>node[x]).on((e) => this._bindingsSetValue(host ?? this, value.replaceAll('?', ''), (<HTMLInputElement>node)[camelCased], context, repeatBindingItems));
263+
else
264+
node.addEventListener(x, (e) => this._bindingsSetValue(host ?? this, value.replaceAll('?', ''), (<HTMLInputElement>node)[camelCased], context, repeatBindingItems));
265+
}
258266
}
259267
}
260268
}

src/DeclaritiveBaseCustomWebcomponent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,20 @@ class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttached
152152
customElements.define("node-projects-dce", DeclaritiveBaseCustomWebcomponent);
153153

154154
/*
155-
<node-projects-dce name="simple-dce-demo" properties='{"list":"Array", "list2":"Array"}' enable-bindings >
155+
<node-projects-dce name="simple-dce-demo" properties='{"list":"Array", "list2":"Array", "ctx":{"type":"String","reflect":true}}' enable-bindings >
156156
<template>
157157
<style>h1 {color: red}</style>
158158
<h1>Hello World</h1>
159+
<div style="border: solid 3px black">Ctx: [[this.ctx]]</div>
159160
<template repeat:myitem="[[this.list]]">
160161
<button>[[myitem.toUpperCase()]] - <b>[[myitem.toLowerCase()]]</b> - [[index]]</button>
161162
<ul>
162163
<template repeat:myitem2="[[this.list2]]" repeat-index="inneridx">
163-
<button>[[myitem.toUpperCase()]] - <b>[[myitem2.toLowerCase()]]</b> - [[inneridx * 100]]</button>
164+
<button @click="{{this.ctx = myItem2}}" >[[myitem.toUpperCase()]] - <b>[[myitem2.toLowerCase()]]</b> - [[inneridx * 100]]</button>
164165
</template>
165166
</ul>
166167
</template>
167168
</template>
168169
</node-projects-dce>
169-
<simple-dce-demo list='["aa","bb","cc"]' list2='["hello", "you"]' style="position:absolute;left:184px;top:-53px;"></simple-dce-demo>
170+
<simple-dce-demo list='["aa","bb","cc"]' list2='["hello", "you"]' ctx="TestCtx" style="position:absolute;left:184px;top:-53px;"></simple-dce-demo>
170171
*/

0 commit comments

Comments
 (0)