Skip to content

Commit a54c268

Browse files
committed
added fragments for UI
1 parent d1331dc commit a54c268

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

OpenScript.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,25 @@ var OpenScript = {
443443

444444
/**
445445
* Add Event Listeners
446-
* @param {string} event
446+
* @param {string} events - space separated events
447447
* @param {function} listener - asynchronous function
448448
*/
449-
on(event, listener) {
450-
if (this.#logs[event]) {
451-
let emitted = this.#logs[event];
449+
on(events, listener) {
450+
events = events.split(/[\s\|]+/);
452451

453-
for (let i = 0; i < emitted.length; i++) {
454-
listener(...emitted[i].args);
452+
for(let event of events) {
453+
event.trim();
454+
if (this.#logs[event]) {
455+
let emitted = this.#logs[event];
456+
457+
for (let i = 0; i < emitted.length; i++) {
458+
listener(...emitted[i].args);
459+
}
455460
}
461+
462+
this.#emitter.on(event, listener);
456463
}
457464

458-
return this.#emitter.on(event, listener);
459465
}
460466

461467
/**
@@ -1487,9 +1493,17 @@ var OpenScript = {
14871493
attr[`s-${s.id}`] = s.id;
14881494
});
14891495

1496+
let markup = this.render(...args, { withCAttr: true });
1497+
1498+
if(markup.tagName == 'FRAGMENT' && markup.childNodes.length > 0) {
1499+
1500+
let children = markup.childNodes;
1501+
1502+
return children.length > 1 ? children : children[0];
1503+
}
1504+
14901505
if (!this.visible) attr.style = "display: none;";
14911506

1492-
let markup = this.render(...args, { withCAttr: true });
14931507
let cAttributes = {};
14941508

14951509
if (markup instanceof HTMLElement) {
@@ -2280,7 +2294,7 @@ var OpenScript = {
22802294
} else {
22812295
root = this.dom.createElement(name);
22822296
}
2283-
2297+
22842298
let parseAttr = (obj) => {
22852299
for (let k in obj) {
22862300
let v = obj[k];
@@ -2342,11 +2356,18 @@ var OpenScript = {
23422356
val = (root.getAttribute(k) ?? "") + ` ${val}`;
23432357
}
23442358

2345-
root.setAttribute(k, val);
2359+
try{
2360+
root.setAttribute(k, val);
2361+
}
2362+
catch(e) {
2363+
console.error(`Attributes resulting in the error: `, obj);
2364+
throw Error(e);
2365+
}
23462366
}
23472367
};
23482368

23492369
const parse = (arg, isComp) => {
2370+
23502371
if (
23512372
arg instanceof DocumentFragment ||
23522373
arg instanceof HTMLElement
@@ -2375,7 +2396,7 @@ var OpenScript = {
23752396

23762397
if (arg instanceof OpenScript.State) continue;
23772398

2378-
if (Array.isArray(arg)) {
2399+
if (Array.isArray(arg) || arg instanceof HTMLCollection || arg instanceof NodeList) {
23792400
if (isComponent) continue;
23802401

23812402
arg.forEach((e) => {

example/components/App.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
class Table extends OpenScript.Component {
2+
render(...args) {
3+
return h.fragment(
4+
h.table(
5+
{class: 'table'},
6+
h.tbody(
7+
each([1, 2, 3, 4, 5, 6], () => h.Row("Column")),
8+
),
9+
...args
10+
)
11+
)
12+
}
13+
}
14+
15+
class Row extends OpenScript.Component {
16+
render(text, ...args){
17+
return h.fragment(
18+
h.tr(
19+
each([1, 2, 3, 4], () => h.Column(text)),
20+
...args
21+
)
22+
)
23+
}
24+
}
25+
26+
class Column extends OpenScript.Component {
27+
render(text, ...args) {
28+
return h.fragment(
29+
h.td(text, ...args)
30+
);
31+
}
32+
}
33+
134
class App extends OpenScript.Component {
235

336
constructor(){
@@ -69,7 +102,9 @@ class App extends OpenScript.Component {
69102

70103
h.BlogList(context("blogCxt").blogs, context('blogCxt').counter, "I am a blog List. I re-render when counter changes"),
71104

72-
h.BlogList(context("blogCxt").blogs, { value: 0 }, 'I am the same blog list. I do not re-render when counter changes because I do not listen its changes.' )
105+
h.BlogList(context("blogCxt").blogs, { value: 0 }, 'I am the same blog list. I do not re-render when counter changes because I do not listen its changes.' ),
106+
107+
h.Table()
73108
),
74109

75110
...args

example/ojs-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,4 @@ broker.removeStaleEvents(); // broker garbage collection started
134134
| in the console when they are fired
135135
|------------------------------------------
136136
*/
137-
broker.withLogs(true);
137+
broker.withLogs(false);

0 commit comments

Comments
 (0)