objs-core 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EXAMPLES.md +59 -58
- package/README.md +51 -27
- package/SKILL.md +53 -4
- package/objs.built.js +154 -37
- package/objs.built.min.js +26 -26
- package/objs.d.ts +3 -2
- package/objs.js +171 -47
- package/package.json +1 -1
package/objs.built.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Objs-core library
|
|
3
|
-
* @version 2.
|
|
3
|
+
* @version 2.1
|
|
4
4
|
* @author Roman Torshin
|
|
5
5
|
* @license Apache-2.0
|
|
6
6
|
*/
|
|
@@ -13,6 +13,7 @@ const o = (query) => {
|
|
|
13
13
|
parented: {},
|
|
14
14
|
store: {},
|
|
15
15
|
refs: {},
|
|
16
|
+
_refsByIndex: [],
|
|
16
17
|
states: [],
|
|
17
18
|
isDebug: false,
|
|
18
19
|
currentState: "",
|
|
@@ -65,8 +66,40 @@ const o = (query) => {
|
|
|
65
66
|
result.states = [];
|
|
66
67
|
result.ie = {};
|
|
67
68
|
}
|
|
69
|
+
if (Array.isArray(result._refsByIndex)) {
|
|
70
|
+
const currentLen = result._refsByIndex.length;
|
|
71
|
+
if (currentLen > ln) {
|
|
72
|
+
cycleObj(result._refsByIndex, (k) => {
|
|
73
|
+
const idx = +k;
|
|
74
|
+
if (idx >= ln) {
|
|
75
|
+
delete result._refsByIndex[idx];
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
result._refsByIndex.length = ln;
|
|
79
|
+
} else if (currentLen < ln) {
|
|
80
|
+
for (let idx = currentLen; idx < ln; idx++) {
|
|
81
|
+
result._refsByIndex[idx] = {};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
68
85
|
};
|
|
69
86
|
result.reset = o;
|
|
87
|
+
const hydrateDataOInitIn = (containerEl) => {
|
|
88
|
+
if (ssr || !containerEl.querySelectorAll) return;
|
|
89
|
+
const nodes = containerEl.querySelectorAll("[data-o-init]");
|
|
90
|
+
const byId = {};
|
|
91
|
+
nodes.forEach((node) => {
|
|
92
|
+
const id = node.getAttribute("data-o-init");
|
|
93
|
+
if (id === null) return;
|
|
94
|
+
if (!byId[id]) byId[id] = [];
|
|
95
|
+
byId[id].push(node);
|
|
96
|
+
});
|
|
97
|
+
cycleObj(byId, (id) => {
|
|
98
|
+
const inst = o.inits[id];
|
|
99
|
+
if (!inst) return;
|
|
100
|
+
inst.getSSR(Number(id), byId[id]);
|
|
101
|
+
});
|
|
102
|
+
};
|
|
70
103
|
const transform = (el, state, props) => {
|
|
71
104
|
cycleObj(state, (s) => {
|
|
72
105
|
let value = state[s];
|
|
@@ -98,7 +131,7 @@ const o = (query) => {
|
|
|
98
131
|
"root",
|
|
99
132
|
"ref"
|
|
100
133
|
].includes(s)) {
|
|
101
|
-
["html", "innerHTML"].includes(s) ? el.innerHTML = value : (
|
|
134
|
+
["html", "innerHTML"].includes(s) ? (el.innerHTML = value, !ssr && hydrateDataOInitIn(el)) : (
|
|
102
135
|
// className alias
|
|
103
136
|
s === "className" ? el.setAttribute("class", value) : (
|
|
104
137
|
// attach dataset
|
|
@@ -198,21 +231,21 @@ const o = (query) => {
|
|
|
198
231
|
data["data-o-init"] = initN;
|
|
199
232
|
}
|
|
200
233
|
const newEl = (n, prop = {}) => {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const newElem = D.createElement("div");
|
|
205
|
-
newElem.innerHTML = type(data) === functionType ? data(prop) : data;
|
|
206
|
-
if (newElem.children.length > ONE || !newElem.firstElementChild) {
|
|
207
|
-
newElem.dataset.oInit = n;
|
|
208
|
-
return newElem;
|
|
209
|
-
} else {
|
|
210
|
-
newElem.firstElementChild.dataset.oInit = n;
|
|
211
|
-
return newElem.firstElementChild;
|
|
212
|
-
}
|
|
234
|
+
const resolved = type(data) === functionType ? data(prop) : data;
|
|
235
|
+
if (type(resolved) === objectType) {
|
|
236
|
+
return D.createElement(resolved.tag || resolved.tagName || "div");
|
|
213
237
|
}
|
|
238
|
+
const newElem = D.createElement("div");
|
|
239
|
+
newElem.innerHTML = resolved;
|
|
240
|
+
if (newElem.children.length > ONE || !newElem.firstElementChild) {
|
|
241
|
+
newElem.dataset.oInit = n;
|
|
242
|
+
return newElem;
|
|
243
|
+
}
|
|
244
|
+
newElem.firstElementChild.dataset.oInit = n;
|
|
245
|
+
return newElem.firstElementChild;
|
|
214
246
|
};
|
|
215
247
|
const rawData = props;
|
|
248
|
+
if (!Array.isArray(props)) props = [props];
|
|
216
249
|
!props.length ? props = [props] : props;
|
|
217
250
|
const creation = !els[0] && state === "render";
|
|
218
251
|
props = props.map((prop, i2) => {
|
|
@@ -247,19 +280,45 @@ const o = (query) => {
|
|
|
247
280
|
if (creation) {
|
|
248
281
|
buff["data-o-init"] = initN;
|
|
249
282
|
buff["data-o-init-i"] = i2;
|
|
283
|
+
if (buff.events) {
|
|
284
|
+
result._hydrateEvents = result._hydrateEvents || [];
|
|
285
|
+
result._hydrateEvents[i2] = buff.events;
|
|
286
|
+
}
|
|
250
287
|
}
|
|
251
288
|
transform(el, buff, props[j ? i2 : 0]);
|
|
252
289
|
}
|
|
253
290
|
});
|
|
254
291
|
if (creation) {
|
|
292
|
+
result._refsByIndex = [];
|
|
255
293
|
result.refs = {};
|
|
256
|
-
result.els.forEach((el) => {
|
|
294
|
+
result.els.forEach((el, idx) => {
|
|
257
295
|
if (!el.querySelectorAll) return;
|
|
296
|
+
const refsForEl = {};
|
|
258
297
|
el.querySelectorAll("[ref]").forEach((refEl) => {
|
|
259
|
-
|
|
260
|
-
refEl
|
|
298
|
+
const refName = refEl.getAttribute("ref");
|
|
299
|
+
const refInstance = o(refEl);
|
|
300
|
+
refsForEl[refName] = refInstance;
|
|
301
|
+
if (idx === 0) result.refs[refName] = refInstance;
|
|
261
302
|
});
|
|
303
|
+
result._refsByIndex[idx] = refsForEl;
|
|
262
304
|
});
|
|
305
|
+
if (!ssr && result._hydrateEvents) {
|
|
306
|
+
result._hydrateEvents.forEach((evts, idx) => {
|
|
307
|
+
if (!evts) return;
|
|
308
|
+
result.select(idx);
|
|
309
|
+
cycleObj(evts, (event) => {
|
|
310
|
+
const spec = evts[event];
|
|
311
|
+
if (type(spec) === objectType && spec.targetRef && type(spec.handler) === functionType) {
|
|
312
|
+
const refsForIdx = result._refsByIndex?.[idx] ?? result.refs;
|
|
313
|
+
const ref = refsForIdx?.[spec.targetRef];
|
|
314
|
+
if (ref) ref.on(event, spec.handler);
|
|
315
|
+
} else if (type(spec) === functionType) {
|
|
316
|
+
result.on(event, spec);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
result.all();
|
|
321
|
+
}
|
|
263
322
|
}
|
|
264
323
|
}
|
|
265
324
|
if (creation && type(data) === objectType && data.events) {
|
|
@@ -270,19 +329,47 @@ const o = (query) => {
|
|
|
270
329
|
});
|
|
271
330
|
});
|
|
272
331
|
const renderState = states.render || states;
|
|
273
|
-
|
|
332
|
+
const hasStateEvents = !ssr && type(renderState) === objectType && renderState.events;
|
|
333
|
+
const hasHydrateEvents = !ssr && result._hydrateEvents && result._hydrateEvents.length;
|
|
334
|
+
if (hasStateEvents || hasHydrateEvents) {
|
|
274
335
|
result.initSSRAfterGettingSSR = () => {
|
|
336
|
+
result._refsByIndex = [];
|
|
275
337
|
result.refs = {};
|
|
276
|
-
result.els.forEach((el) => {
|
|
338
|
+
result.els.forEach((el, idx) => {
|
|
277
339
|
if (!el.querySelectorAll) return;
|
|
340
|
+
const refsForEl = {};
|
|
278
341
|
el.querySelectorAll("[ref]").forEach((refEl) => {
|
|
279
|
-
|
|
342
|
+
const refName = refEl.getAttribute("ref");
|
|
343
|
+
const refInstance = o(refEl);
|
|
344
|
+
refsForEl[refName] = refInstance;
|
|
345
|
+
if (idx === 0) result.refs[refName] = refInstance;
|
|
280
346
|
refEl.removeAttribute("ref");
|
|
281
347
|
});
|
|
348
|
+
result._refsByIndex[idx] = refsForEl;
|
|
349
|
+
if (idx === 0) result.refs = refsForEl;
|
|
282
350
|
});
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
351
|
+
if (hasStateEvents) {
|
|
352
|
+
cycleObj(renderState.events, (event) => {
|
|
353
|
+
result.on(event, renderState.events[event]);
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
if (result._hydrateEvents) {
|
|
357
|
+
result._hydrateEvents.forEach((evts, idx) => {
|
|
358
|
+
if (!evts) return;
|
|
359
|
+
result.select(idx);
|
|
360
|
+
cycleObj(evts, (event) => {
|
|
361
|
+
const spec = evts[event];
|
|
362
|
+
if (type(spec) === objectType && spec.targetRef && type(spec.handler) === functionType) {
|
|
363
|
+
const refsForIdx = result._refsByIndex?.[idx] ?? result.refs;
|
|
364
|
+
const ref = refsForIdx?.[spec.targetRef];
|
|
365
|
+
if (ref) ref.on(event, spec.handler);
|
|
366
|
+
} else if (type(spec) === functionType) {
|
|
367
|
+
result.on(event, spec);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
result.all();
|
|
372
|
+
}
|
|
286
373
|
};
|
|
287
374
|
}
|
|
288
375
|
}, "init");
|
|
@@ -294,21 +381,37 @@ const o = (query) => {
|
|
|
294
381
|
]);
|
|
295
382
|
loader.connect(self, state, fail);
|
|
296
383
|
}, "connect");
|
|
297
|
-
result.getSSR = returner((initId) => {
|
|
384
|
+
result.getSSR = returner((initId, fromEls) => {
|
|
298
385
|
typeVerify([[initId, [numberType, undefinedType]]]);
|
|
299
386
|
const effectiveId = initId !== void 0 ? initId : result.initID;
|
|
300
387
|
if (ssr || type(initId) === undefinedType && type(result.initID) === undefinedType) {
|
|
301
388
|
return;
|
|
302
389
|
}
|
|
303
|
-
const ssrEls = o.D.querySelectorAll(`[data-o-init="${effectiveId}"]`);
|
|
304
|
-
if (ssrEls.length
|
|
390
|
+
const ssrEls = fromEls && fromEls.length ? fromEls : o.D.querySelectorAll(`[data-o-init="${effectiveId}"]`);
|
|
391
|
+
if (ssrEls.length) {
|
|
305
392
|
result.els = Array.from(ssrEls);
|
|
306
|
-
|
|
307
|
-
|
|
393
|
+
if (initId !== void 0) {
|
|
394
|
+
result.initID = initId;
|
|
395
|
+
o.inits[initId] = result;
|
|
396
|
+
}
|
|
308
397
|
setResultVals(false);
|
|
309
398
|
if (type(result.initSSRAfterGettingSSR) === functionType) {
|
|
310
399
|
result.initSSRAfterGettingSSR();
|
|
311
|
-
|
|
400
|
+
} else if (fromEls && fromEls.length) {
|
|
401
|
+
result._refsByIndex = [];
|
|
402
|
+
result.refs = {};
|
|
403
|
+
result.els.forEach((el, idx) => {
|
|
404
|
+
if (!el.querySelectorAll) return;
|
|
405
|
+
const refsForEl = {};
|
|
406
|
+
el.querySelectorAll("[ref]").forEach((refEl) => {
|
|
407
|
+
const refName = refEl.getAttribute("ref");
|
|
408
|
+
refsForEl[refName] = o(refEl);
|
|
409
|
+
if (idx === 0) result.refs[refName] = refsForEl[refName];
|
|
410
|
+
refEl.removeAttribute("ref");
|
|
411
|
+
});
|
|
412
|
+
result._refsByIndex[idx] = refsForEl;
|
|
413
|
+
if (idx === 0) result.refs = refsForEl;
|
|
414
|
+
});
|
|
312
415
|
}
|
|
313
416
|
}
|
|
314
417
|
}, "getSSR");
|
|
@@ -424,20 +527,31 @@ const o = (query) => {
|
|
|
424
527
|
return { [state]: parseState(result.els[finish]) };
|
|
425
528
|
}, "sample");
|
|
426
529
|
result.select = returner((i2) => {
|
|
427
|
-
|
|
428
|
-
if (
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
530
|
+
let idx = i2;
|
|
531
|
+
if (idx != null && type(idx) === objectType && idx.target && result.els.length) {
|
|
532
|
+
idx = result.els.findIndex((el) => el === idx.target || el.contains(idx.target));
|
|
533
|
+
if (idx < 0) idx = 0;
|
|
534
|
+
}
|
|
535
|
+
typeVerify([[idx, [numberType, undefinedType]]]);
|
|
536
|
+
if (idx === u) {
|
|
537
|
+
idx = result.length - ONE;
|
|
538
|
+
}
|
|
539
|
+
start = idx;
|
|
540
|
+
finish = idx;
|
|
541
|
+
result.el = result.els[idx];
|
|
434
542
|
select = ONE;
|
|
543
|
+
if (Array.isArray(result._refsByIndex) && result._refsByIndex[idx]) {
|
|
544
|
+
result.refs = result._refsByIndex[idx];
|
|
545
|
+
}
|
|
435
546
|
}, "select");
|
|
436
547
|
result.all = returner(() => {
|
|
437
548
|
start = result.length - ONE;
|
|
438
549
|
finish = 0;
|
|
439
550
|
result.el = result.els[0];
|
|
440
551
|
select = 0;
|
|
552
|
+
if (Array.isArray(result._refsByIndex) && result._refsByIndex.length) {
|
|
553
|
+
result.refs = result._refsByIndex[0] || {};
|
|
554
|
+
}
|
|
441
555
|
}, "all");
|
|
442
556
|
result.remove = returner((j2) => {
|
|
443
557
|
typeVerify([[j2, [numberType, undefinedType]]]);
|
|
@@ -464,7 +578,10 @@ const o = (query) => {
|
|
|
464
578
|
if (j2 === u) {
|
|
465
579
|
j2 = finish;
|
|
466
580
|
}
|
|
467
|
-
result.els.splice(
|
|
581
|
+
result.els.splice(j2, ONE);
|
|
582
|
+
if (Array.isArray(result._refsByIndex)) {
|
|
583
|
+
result._refsByIndex.splice(j2, ONE);
|
|
584
|
+
}
|
|
468
585
|
setResultVals();
|
|
469
586
|
}, "skip");
|
|
470
587
|
result.add = returner((el) => {
|