rumious 1.0.13 → 2.0.1
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/dist/app/app.d.ts +10 -8
- package/dist/app/index.d.ts +0 -1
- package/dist/component/component.d.ts +15 -24
- package/dist/component/element.d.ts +10 -15
- package/dist/global.d.ts +15 -0
- package/dist/index.d.ts +7 -27
- package/dist/index.js +1042 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx/component.d.ts +3 -0
- package/dist/jsx/dynamic.d.ts +2 -0
- package/dist/jsx/element.d.ts +9 -0
- package/dist/jsx/index.d.ts +4 -4
- package/dist/jsx/template.d.ts +25 -0
- package/dist/{app/module.d.ts → module/index.d.ts} +1 -1
- package/dist/ref/index.d.ts +1 -2
- package/dist/ref/ref.d.ts +37 -0
- package/dist/render/context.d.ts +7 -9
- package/dist/render/index.d.ts +4 -3
- package/dist/render/list.d.ts +16 -0
- package/dist/render/render.d.ts +3 -2
- package/dist/render/view.d.ts +19 -0
- package/dist/state/index.d.ts +1 -2
- package/dist/state/list.d.ts +14 -0
- package/dist/state/reactor.d.ts +15 -16
- package/dist/state/state.d.ts +11 -8
- package/dist/types/component.d.ts +5 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/jsx.d.ts +16 -8
- package/dist/types/state.d.ts +8 -0
- package/dist/types/template.d.ts +5 -0
- package/dist/utils/checker.d.ts +2 -0
- package/package.json +34 -53
- package/rollup.config.js +21 -0
- package/src/app/app.ts +45 -0
- package/src/app/index.ts +1 -0
- package/src/component/component.ts +69 -0
- package/src/component/element.ts +76 -0
- package/src/component/index.ts +2 -0
- package/src/global.ts +20 -0
- package/src/index.ts +13 -0
- package/src/jsx/component.ts +17 -0
- package/src/jsx/dynamic.ts +87 -0
- package/src/jsx/element.ts +77 -0
- package/src/jsx/index.ts +7 -0
- package/src/jsx/template.ts +377 -0
- package/src/module/index.ts +7 -0
- package/src/ref/index.ts +1 -0
- package/src/ref/ref.ts +178 -0
- package/src/render/context.ts +11 -0
- package/src/render/index.ts +4 -0
- package/src/render/list.ts +115 -0
- package/src/render/render.ts +31 -0
- package/src/render/view.ts +101 -0
- package/src/state/index.ts +2 -0
- package/src/state/list.ts +96 -0
- package/src/state/reactor.ts +65 -0
- package/src/state/state.ts +68 -0
- package/src/types/component.ts +6 -0
- package/src/types/index.ts +3 -0
- package/src/types/state.ts +16 -0
- package/src/types/template.ts +7 -0
- package/src/utils/checker.ts +5 -0
- package/tsconfig.json +20 -0
- package/README.md +0 -7
- package/dist/context/context.d.ts +0 -12
- package/dist/context/index.d.ts +0 -1
- package/dist/index.cjs +0 -1
- package/dist/index.esm.js +0 -1
- package/dist/index.global.d.ts +0 -46
- package/dist/index.min.js +0 -1
- package/dist/performance/index.d.ts +0 -5
- package/dist/ref/children.d.ts +0 -23
- package/dist/ref/element.d.ts +0 -49
- package/dist/render/array.d.ts +0 -21
- package/dist/render/directives.d.ts +0 -1
- package/dist/render/dynamic.d.ts +0 -2
- package/dist/render/injector.d.ts +0 -15
- package/dist/render/struct.d.ts +0 -85
- package/dist/render/template.d.ts +0 -5
- package/dist/state/array.d.ts +0 -22
- package/dist/state/object.d.ts +0 -31
- package/dist/types/render.d.ts +0 -5
- package/dist/types/utils.d.ts +0 -1
- package/dist/types/warp.d.ts +0 -5
- package/dist/utils/checkers.d.ts +0 -1
- package/dist/utils/name.d.ts +0 -2
- package/dist/utils/oberve.d.ts +0 -1
package/dist/index.js
ADDED
@@ -0,0 +1,1042 @@
|
|
1
|
+
class RumiousRenderContext {
|
2
|
+
app;
|
3
|
+
target;
|
4
|
+
onRendered = [];
|
5
|
+
constructor(app, target) {
|
6
|
+
this.app = app;
|
7
|
+
this.target = target;
|
8
|
+
}
|
9
|
+
}
|
10
|
+
|
11
|
+
function render(content, container, context) {
|
12
|
+
context.onRendered = [];
|
13
|
+
let result = content(container, context);
|
14
|
+
for (var i = 0; i < context.onRendered.length; i++) {
|
15
|
+
context.onRendered[i]();
|
16
|
+
}
|
17
|
+
return result;
|
18
|
+
}
|
19
|
+
function renderFrag(content, context) {
|
20
|
+
let container = document.createDocumentFragment();
|
21
|
+
context.onRendered = [];
|
22
|
+
let result = content(container, context);
|
23
|
+
for (var i = 0; i < context.onRendered.length; i++) {
|
24
|
+
context.onRendered[i]();
|
25
|
+
}
|
26
|
+
return result;
|
27
|
+
}
|
28
|
+
|
29
|
+
class RumiousViewControl {
|
30
|
+
targets = [];
|
31
|
+
constructor() { }
|
32
|
+
addTarget(target) {
|
33
|
+
this.targets.push(target);
|
34
|
+
}
|
35
|
+
setView(template) {
|
36
|
+
const targets = this.targets;
|
37
|
+
if (targets.length === 0) {
|
38
|
+
throw new Error(`RumiousRenderError: No target assigned to ViewControl`);
|
39
|
+
}
|
40
|
+
for (let i = 0; i < targets.length; i++) {
|
41
|
+
render(template, targets[i].element, targets[i].context);
|
42
|
+
}
|
43
|
+
}
|
44
|
+
removeChild(index) {
|
45
|
+
for (let i = 0; i < this.targets.length; i++) {
|
46
|
+
let parent = this.targets[i].element.parentElement;
|
47
|
+
if (!parent)
|
48
|
+
return;
|
49
|
+
let element = parent.children[index];
|
50
|
+
if (element)
|
51
|
+
parent.removeChild(element);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
addChild(template, prepend = false) {
|
55
|
+
const targets = this.targets;
|
56
|
+
if (targets.length === 0) {
|
57
|
+
throw new Error(`RumiousRenderError: No target assigned to ViewControl`);
|
58
|
+
}
|
59
|
+
for (let i = 0; i < targets.length; i++) {
|
60
|
+
let templ = renderFrag(template, targets[i].context);
|
61
|
+
if (!prepend)
|
62
|
+
targets[i].element.appendChild(templ);
|
63
|
+
else
|
64
|
+
targets[i].element.prepend(templ);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
each(callback) {
|
68
|
+
for (let target of this.targets) {
|
69
|
+
callback(target);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
emptyAll() {
|
73
|
+
const targets = this.targets;
|
74
|
+
for (let i = 0; i < targets.length; i++) {
|
75
|
+
targets[i].element.textContent = '';
|
76
|
+
}
|
77
|
+
}
|
78
|
+
empty(target) {
|
79
|
+
const targets = this.targets;
|
80
|
+
for (let i = 0; i < targets.length; i++) {
|
81
|
+
if (targets[i].element === target) {
|
82
|
+
target.textContent = '';
|
83
|
+
return;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
updateChild(index, template) {
|
88
|
+
for (let i = 0; i < this.targets.length; i++) {
|
89
|
+
const { element, context } = this.targets[i];
|
90
|
+
const parent = element.parentElement;
|
91
|
+
if (!parent)
|
92
|
+
continue;
|
93
|
+
const oldChild = parent.children[index];
|
94
|
+
const newNode = renderFrag(template, context);
|
95
|
+
if (oldChild) {
|
96
|
+
parent.replaceChild(newNode, oldChild);
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
101
|
+
function createViewControl() {
|
102
|
+
return new RumiousViewControl();
|
103
|
+
}
|
104
|
+
|
105
|
+
class RumiousPagination {
|
106
|
+
view;
|
107
|
+
data;
|
108
|
+
templFn;
|
109
|
+
keyFn;
|
110
|
+
currentPage = 0;
|
111
|
+
limit = 50;
|
112
|
+
pos = [0, 0];
|
113
|
+
constructor(view, data, templFn, keyFn) {
|
114
|
+
this.view = view;
|
115
|
+
this.data = data;
|
116
|
+
this.templFn = templFn;
|
117
|
+
this.keyFn = keyFn;
|
118
|
+
}
|
119
|
+
show() {
|
120
|
+
let [start, end] = this.calcPos();
|
121
|
+
let list = this.data.value.slice(start, end);
|
122
|
+
this.pos = [start, end];
|
123
|
+
for (let data of list) {
|
124
|
+
this.keyFn(data);
|
125
|
+
let templ = this.templFn(data);
|
126
|
+
this.view.addChild(templ);
|
127
|
+
}
|
128
|
+
if (!this.data.reactor)
|
129
|
+
return;
|
130
|
+
this.data.reactor.addInternalBinding(this.onDataChange.bind(this));
|
131
|
+
}
|
132
|
+
calcPos() {
|
133
|
+
const size = this.data.value.length;
|
134
|
+
const totalPage = Math.ceil(size / this.limit);
|
135
|
+
const currentPage = Math.max(0, Math.min(this.currentPage, totalPage - 1));
|
136
|
+
const start = currentPage * this.limit;
|
137
|
+
const end = Math.min(start + this.limit, size);
|
138
|
+
return [start, end];
|
139
|
+
}
|
140
|
+
onDataChange(commit) {
|
141
|
+
const [start, end] = this.calcPos();
|
142
|
+
const total = this.data.value.length;
|
143
|
+
const { type, key, value } = commit;
|
144
|
+
if (type === 'set') {
|
145
|
+
this.view.emptyAll();
|
146
|
+
this.show();
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
if (typeof key === 'number' && key < start) {
|
150
|
+
this.view.emptyAll();
|
151
|
+
this.show();
|
152
|
+
return;
|
153
|
+
}
|
154
|
+
if (typeof key === 'number' && key >= start && key < end) {
|
155
|
+
const indexInView = key - start;
|
156
|
+
switch (type) {
|
157
|
+
case 'update': {
|
158
|
+
const item = this.data.value[key];
|
159
|
+
const templ = this.templFn(item);
|
160
|
+
this.view.updateChild(indexInView, templ);
|
161
|
+
break;
|
162
|
+
}
|
163
|
+
case 'remove': {
|
164
|
+
this.view.removeChild(indexInView);
|
165
|
+
const extraIndex = end - 1;
|
166
|
+
if (extraIndex < total) {
|
167
|
+
const extraItem = this.data.value[extraIndex];
|
168
|
+
const extraTemplate = this.templFn(extraItem);
|
169
|
+
this.view.addChild(extraTemplate);
|
170
|
+
}
|
171
|
+
break;
|
172
|
+
}
|
173
|
+
case 'insert':
|
174
|
+
case 'prepend': {
|
175
|
+
const item = this.data.value[key];
|
176
|
+
const templ = this.templFn(item);
|
177
|
+
this.view.addChild(templ, true);
|
178
|
+
const currentViewSize = end - start + 1;
|
179
|
+
if (currentViewSize > this.limit) {
|
180
|
+
this.view.removeChild(this.limit);
|
181
|
+
}
|
182
|
+
break;
|
183
|
+
}
|
184
|
+
case 'append': {
|
185
|
+
if (key < start + this.limit) {
|
186
|
+
const item = this.data.value[key];
|
187
|
+
const templ = this.templFn(item);
|
188
|
+
this.view.addChild(templ);
|
189
|
+
const currentViewSize = end - start + 1;
|
190
|
+
if (currentViewSize > this.limit) {
|
191
|
+
this.view.removeChild(0);
|
192
|
+
}
|
193
|
+
}
|
194
|
+
break;
|
195
|
+
}
|
196
|
+
}
|
197
|
+
return;
|
198
|
+
}
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
class RumiousApp {
|
203
|
+
config;
|
204
|
+
modules = [];
|
205
|
+
context = new RumiousRenderContext(this, this);
|
206
|
+
constructor(config) {
|
207
|
+
this.config = config;
|
208
|
+
}
|
209
|
+
addModule(ModuleClass, options) {
|
210
|
+
const instance = ModuleClass.init(this, options);
|
211
|
+
this.modules.push(instance);
|
212
|
+
return instance;
|
213
|
+
}
|
214
|
+
render(content) {
|
215
|
+
render(content, this.config.root, this.context);
|
216
|
+
}
|
217
|
+
}
|
218
|
+
function createApp(config) {
|
219
|
+
return new RumiousApp(config);
|
220
|
+
}
|
221
|
+
|
222
|
+
class RumiousReactor {
|
223
|
+
target;
|
224
|
+
bindings = [];
|
225
|
+
internal = [];
|
226
|
+
isUIBatch = true;
|
227
|
+
scheduled = false;
|
228
|
+
queuedCommits = [];
|
229
|
+
constructor(target) {
|
230
|
+
this.target = target;
|
231
|
+
}
|
232
|
+
addBinding(binding) {
|
233
|
+
this.bindings.push(binding);
|
234
|
+
}
|
235
|
+
removeBinding(binding) {
|
236
|
+
this.bindings = this.bindings.filter(b => b !== binding);
|
237
|
+
}
|
238
|
+
addInternalBinding(binding) {
|
239
|
+
this.internal.push(binding);
|
240
|
+
}
|
241
|
+
removeInternalBinding(binding) {
|
242
|
+
this.internal = this.internal.filter(b => b !== binding);
|
243
|
+
}
|
244
|
+
notify(commit) {
|
245
|
+
for (const binding of this.bindings) {
|
246
|
+
binding(commit);
|
247
|
+
}
|
248
|
+
if (this.isUIBatch) {
|
249
|
+
this.scheduleInternalUpdate(commit);
|
250
|
+
}
|
251
|
+
else {
|
252
|
+
for (const binding of this.internal) {
|
253
|
+
binding(commit);
|
254
|
+
}
|
255
|
+
}
|
256
|
+
}
|
257
|
+
scheduleInternalUpdate(commit) {
|
258
|
+
this.queuedCommits.push(commit);
|
259
|
+
if (!this.scheduled) {
|
260
|
+
this.scheduled = true;
|
261
|
+
queueMicrotask(() => {
|
262
|
+
this.flushInternal();
|
263
|
+
});
|
264
|
+
}
|
265
|
+
}
|
266
|
+
flushInternal() {
|
267
|
+
const lastCommit = this.queuedCommits[this.queuedCommits.length - 1];
|
268
|
+
for (const binding of this.internal) {
|
269
|
+
binding(lastCommit); // chỉ gửi commit cuối cùng
|
270
|
+
}
|
271
|
+
this.queuedCommits = [];
|
272
|
+
this.scheduled = false;
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
class RumiousState {
|
277
|
+
value;
|
278
|
+
reactor;
|
279
|
+
constructor(value, reactor) {
|
280
|
+
this.value = value;
|
281
|
+
this.reactor = reactor;
|
282
|
+
if (!this.reactor) {
|
283
|
+
this.reactor = new RumiousReactor(this);
|
284
|
+
}
|
285
|
+
}
|
286
|
+
set(value) {
|
287
|
+
this.value = value;
|
288
|
+
this.reactor?.notify({
|
289
|
+
type: 'set',
|
290
|
+
value: value,
|
291
|
+
state: this
|
292
|
+
});
|
293
|
+
}
|
294
|
+
get() {
|
295
|
+
return this.value;
|
296
|
+
}
|
297
|
+
slientUpdate(value) {
|
298
|
+
this.value = value;
|
299
|
+
}
|
300
|
+
update(updater) {
|
301
|
+
this.set(updater(this.value));
|
302
|
+
}
|
303
|
+
toJSON() {
|
304
|
+
return JSON.stringify(this.value);
|
305
|
+
}
|
306
|
+
trigger() {
|
307
|
+
this.reactor?.notify({
|
308
|
+
type: 'set',
|
309
|
+
value: this.value,
|
310
|
+
state: this
|
311
|
+
});
|
312
|
+
}
|
313
|
+
}
|
314
|
+
function createState(value) {
|
315
|
+
return new RumiousState(value);
|
316
|
+
}
|
317
|
+
function watch(state, callback) {
|
318
|
+
if (state.reactor)
|
319
|
+
state.reactor.addBinding(callback);
|
320
|
+
}
|
321
|
+
function unwatch(state, callback) {
|
322
|
+
if (state.reactor)
|
323
|
+
state.reactor.removeBinding(callback);
|
324
|
+
}
|
325
|
+
|
326
|
+
class RumiousListState extends RumiousState {
|
327
|
+
constructor(value = [], reactor) {
|
328
|
+
super(value, reactor);
|
329
|
+
}
|
330
|
+
append(value) {
|
331
|
+
this.value.push(value);
|
332
|
+
this.reactor?.notify({
|
333
|
+
type: 'append',
|
334
|
+
state: this,
|
335
|
+
key: this.value.length - 1,
|
336
|
+
value
|
337
|
+
});
|
338
|
+
}
|
339
|
+
prepend(value) {
|
340
|
+
this.value.unshift(value);
|
341
|
+
this.reactor?.notify({
|
342
|
+
type: 'prepend',
|
343
|
+
state: this,
|
344
|
+
key: 0,
|
345
|
+
value
|
346
|
+
});
|
347
|
+
}
|
348
|
+
insert(pos, value) {
|
349
|
+
this.value.splice(pos, 0, value);
|
350
|
+
this.reactor?.notify({
|
351
|
+
type: 'insert',
|
352
|
+
state: this,
|
353
|
+
value,
|
354
|
+
key: pos
|
355
|
+
});
|
356
|
+
}
|
357
|
+
updateAt(pos, value) {
|
358
|
+
this.value[pos] = value;
|
359
|
+
this.reactor?.notify({
|
360
|
+
type: 'update',
|
361
|
+
state: this,
|
362
|
+
value,
|
363
|
+
key: pos
|
364
|
+
});
|
365
|
+
}
|
366
|
+
remove(pos) {
|
367
|
+
let currentValue = this.value[pos];
|
368
|
+
this.value.splice(pos, 1);
|
369
|
+
this.reactor?.notify({
|
370
|
+
type: 'remove',
|
371
|
+
state: this,
|
372
|
+
value: currentValue,
|
373
|
+
key: pos
|
374
|
+
});
|
375
|
+
}
|
376
|
+
clear() {
|
377
|
+
this.value.length = 0;
|
378
|
+
this.reactor?.notify({
|
379
|
+
type: 'set',
|
380
|
+
state: this,
|
381
|
+
value: []
|
382
|
+
});
|
383
|
+
}
|
384
|
+
reverse() {
|
385
|
+
this.value.reverse();
|
386
|
+
this.reactor?.notify({
|
387
|
+
type: 'set',
|
388
|
+
state: this,
|
389
|
+
value: this.value
|
390
|
+
});
|
391
|
+
}
|
392
|
+
filter(predicate) {
|
393
|
+
this.value = this.value.filter(predicate);
|
394
|
+
this.reactor?.notify({
|
395
|
+
type: 'set',
|
396
|
+
state: this,
|
397
|
+
value: this.value
|
398
|
+
});
|
399
|
+
}
|
400
|
+
}
|
401
|
+
function createListState(values) {
|
402
|
+
return new RumiousListState(values);
|
403
|
+
}
|
404
|
+
|
405
|
+
class RumiousComponent {
|
406
|
+
props;
|
407
|
+
app;
|
408
|
+
element;
|
409
|
+
context;
|
410
|
+
static tagName = 'rumious-component';
|
411
|
+
constructor() { }
|
412
|
+
createViewControl() {
|
413
|
+
return createViewControl();
|
414
|
+
}
|
415
|
+
mountTo(template, target) {
|
416
|
+
return render(template, target, this.context);
|
417
|
+
}
|
418
|
+
prepare(props, context, element) {
|
419
|
+
this.app = context.app;
|
420
|
+
this.element = element;
|
421
|
+
this.props = props;
|
422
|
+
this.context = new RumiousRenderContext(context.app, this);
|
423
|
+
}
|
424
|
+
template() {
|
425
|
+
throw new Error(`RumiousRenderError: Cannot render empty component !`);
|
426
|
+
}
|
427
|
+
requestRender() {
|
428
|
+
let template = this.template();
|
429
|
+
render(template, this.element, this.context);
|
430
|
+
}
|
431
|
+
remove() {
|
432
|
+
this.element.remove();
|
433
|
+
}
|
434
|
+
onCreate() { }
|
435
|
+
onRender() { }
|
436
|
+
onDestroy() { }
|
437
|
+
beforeRender() { }
|
438
|
+
}
|
439
|
+
class Fragment extends RumiousComponent {
|
440
|
+
}
|
441
|
+
|
442
|
+
class RumiousComponentElement extends HTMLElement {
|
443
|
+
component;
|
444
|
+
props;
|
445
|
+
context;
|
446
|
+
instance;
|
447
|
+
constructor() {
|
448
|
+
super();
|
449
|
+
}
|
450
|
+
setContext(context) {
|
451
|
+
this.context = context;
|
452
|
+
}
|
453
|
+
async connectedCallback() {
|
454
|
+
let instance = new this.component();
|
455
|
+
this.instance = instance;
|
456
|
+
instance.prepare(this.props, this.context, this);
|
457
|
+
instance.onCreate();
|
458
|
+
await instance.beforeRender();
|
459
|
+
instance.requestRender();
|
460
|
+
instance.onRender();
|
461
|
+
}
|
462
|
+
disconnectedCallback() {
|
463
|
+
this.instance.onDestroy();
|
464
|
+
}
|
465
|
+
}
|
466
|
+
function createComponentElement(context, component, props) {
|
467
|
+
if (!window.customElements.get(component.tagName)) {
|
468
|
+
window.customElements.define(component.tagName, class extends RumiousComponentElement {
|
469
|
+
constructor() {
|
470
|
+
super();
|
471
|
+
this.component = component;
|
472
|
+
this.props = props;
|
473
|
+
this.context = context;
|
474
|
+
}
|
475
|
+
});
|
476
|
+
}
|
477
|
+
let element = document.createElement(component.tagName);
|
478
|
+
return element;
|
479
|
+
}
|
480
|
+
function renderComponent(component, props) {
|
481
|
+
if (!window.customElements.get(component.tagName)) {
|
482
|
+
window.customElements.define(component.tagName, class extends RumiousComponentElement {
|
483
|
+
constructor() {
|
484
|
+
super();
|
485
|
+
this.component = component;
|
486
|
+
this.props = props;
|
487
|
+
}
|
488
|
+
});
|
489
|
+
}
|
490
|
+
let element = document.createElement(component.tagName);
|
491
|
+
return element;
|
492
|
+
}
|
493
|
+
|
494
|
+
class RumiousRef {
|
495
|
+
element;
|
496
|
+
_mounted = false;
|
497
|
+
_onMountCallbacks = [];
|
498
|
+
_onUnmountCallbacks = [];
|
499
|
+
constructor() { }
|
500
|
+
setTarget(element) {
|
501
|
+
this.element = element;
|
502
|
+
this._mounted = true;
|
503
|
+
for (const cb of this._onMountCallbacks) {
|
504
|
+
cb(element);
|
505
|
+
}
|
506
|
+
}
|
507
|
+
reset() {
|
508
|
+
if (this._mounted) {
|
509
|
+
for (const cb of this._onUnmountCallbacks) {
|
510
|
+
cb();
|
511
|
+
}
|
512
|
+
}
|
513
|
+
this.element = undefined;
|
514
|
+
this._mounted = false;
|
515
|
+
}
|
516
|
+
get() {
|
517
|
+
return this._mounted ? this.element : undefined;
|
518
|
+
}
|
519
|
+
isMounted() {
|
520
|
+
return this._mounted;
|
521
|
+
}
|
522
|
+
has() {
|
523
|
+
return this.isMounted();
|
524
|
+
}
|
525
|
+
onMount(cb) {
|
526
|
+
this._onMountCallbacks.push(cb);
|
527
|
+
if (this._mounted)
|
528
|
+
cb(this.element);
|
529
|
+
}
|
530
|
+
onUnmount(cb) {
|
531
|
+
this._onUnmountCallbacks.push(cb);
|
532
|
+
}
|
533
|
+
toString() {
|
534
|
+
return `[RumiousRef ${this._mounted ? "mounted" : "not mounted"}]`;
|
535
|
+
}
|
536
|
+
focus() {
|
537
|
+
this.assertMounted();
|
538
|
+
this.element.focus();
|
539
|
+
}
|
540
|
+
addClass(className) {
|
541
|
+
this.assertMounted();
|
542
|
+
this.element.classList.add(className);
|
543
|
+
}
|
544
|
+
removeClass(className) {
|
545
|
+
this.assertMounted();
|
546
|
+
this.element.classList.remove(className);
|
547
|
+
}
|
548
|
+
toggleClass(className) {
|
549
|
+
this.assertMounted();
|
550
|
+
this.element.classList.toggle(className);
|
551
|
+
}
|
552
|
+
setAttr(key, value) {
|
553
|
+
this.assertMounted();
|
554
|
+
this.element.setAttribute(key, value);
|
555
|
+
}
|
556
|
+
removeAttr(key) {
|
557
|
+
this.assertMounted();
|
558
|
+
this.element.removeAttribute(key);
|
559
|
+
}
|
560
|
+
query(selector) {
|
561
|
+
this.assertMounted();
|
562
|
+
return this.element.querySelector(selector);
|
563
|
+
}
|
564
|
+
queryAll(selector) {
|
565
|
+
this.assertMounted();
|
566
|
+
return this.element.querySelectorAll(selector);
|
567
|
+
}
|
568
|
+
get value() {
|
569
|
+
this.assertMounted();
|
570
|
+
return 'value' in this.element ? this.element.value : undefined;
|
571
|
+
}
|
572
|
+
set value(val) {
|
573
|
+
this.assertMounted();
|
574
|
+
if ('value' in this.element) {
|
575
|
+
this.element.value = val;
|
576
|
+
}
|
577
|
+
else {
|
578
|
+
throw new Error("RumiousRefError: Element has no 'value' property.");
|
579
|
+
}
|
580
|
+
}
|
581
|
+
get text() {
|
582
|
+
this.assertMounted();
|
583
|
+
return this.element.textContent;
|
584
|
+
}
|
585
|
+
set text(val) {
|
586
|
+
this.assertMounted();
|
587
|
+
this.element.textContent = val;
|
588
|
+
}
|
589
|
+
get html() {
|
590
|
+
this.assertMounted();
|
591
|
+
return this.element.innerHTML;
|
592
|
+
}
|
593
|
+
set html(val) {
|
594
|
+
this.assertMounted();
|
595
|
+
this.element.innerHTML = val;
|
596
|
+
}
|
597
|
+
get checked() {
|
598
|
+
this.assertMounted();
|
599
|
+
return 'checked' in this.element ? Boolean(this.element.checked) : false;
|
600
|
+
}
|
601
|
+
set checked(val) {
|
602
|
+
this.assertMounted();
|
603
|
+
if ('checked' in this.element) {
|
604
|
+
this.element.checked = val;
|
605
|
+
}
|
606
|
+
else {
|
607
|
+
throw new Error("RumiousRefError: Element has no 'checked' property.");
|
608
|
+
}
|
609
|
+
}
|
610
|
+
get disabled() {
|
611
|
+
this.assertMounted();
|
612
|
+
return 'disabled' in this.element ? Boolean(this.element.disabled) : false;
|
613
|
+
}
|
614
|
+
set disabled(val) {
|
615
|
+
this.assertMounted();
|
616
|
+
if ('disabled' in this.element) {
|
617
|
+
this.element.disabled = val;
|
618
|
+
}
|
619
|
+
else {
|
620
|
+
throw new Error("RumiousRefError: Element has no 'disabled' property.");
|
621
|
+
}
|
622
|
+
}
|
623
|
+
get component() {
|
624
|
+
if (this.element instanceof RumiousComponentElement) {
|
625
|
+
return this.element.instance;
|
626
|
+
}
|
627
|
+
else {
|
628
|
+
return null;
|
629
|
+
}
|
630
|
+
}
|
631
|
+
assertMounted() {
|
632
|
+
if (!this._mounted) {
|
633
|
+
throw new Error("RumiousRefError: Element is not mounted.");
|
634
|
+
}
|
635
|
+
}
|
636
|
+
}
|
637
|
+
function createRef() {
|
638
|
+
return new RumiousRef();
|
639
|
+
}
|
640
|
+
|
641
|
+
function appendChild(parent, node) {
|
642
|
+
if (typeof node === 'string')
|
643
|
+
parent.appendChild(document.createTextNode(node));
|
644
|
+
else
|
645
|
+
parent.appendChild(node);
|
646
|
+
}
|
647
|
+
function element(parent, tagName, attrs) {
|
648
|
+
const el = document.createElement(tagName);
|
649
|
+
if (attrs) {
|
650
|
+
for (let key in attrs) {
|
651
|
+
el.setAttribute(key, attrs[key]);
|
652
|
+
}
|
653
|
+
}
|
654
|
+
parent.appendChild(el);
|
655
|
+
return el;
|
656
|
+
}
|
657
|
+
function replaceNode(oldNode, newNode) {
|
658
|
+
const parent = oldNode.parentNode;
|
659
|
+
if (parent) {
|
660
|
+
parent.replaceChild(newNode, oldNode);
|
661
|
+
}
|
662
|
+
else {
|
663
|
+
console.warn('replaceNode: oldNode has no parent. Cannot replace.');
|
664
|
+
}
|
665
|
+
}
|
666
|
+
function createEvent(target, name, callback) {
|
667
|
+
if (!target.__rumiousEvents) {
|
668
|
+
target.__rumiousEvents = {};
|
669
|
+
}
|
670
|
+
target.__rumiousEvents[name] = callback;
|
671
|
+
}
|
672
|
+
function triggerEvent(name, event) {
|
673
|
+
const path = (event.composedPath?.() ?? [event.target]);
|
674
|
+
for (const target of path) {
|
675
|
+
if (target instanceof HTMLElement &&
|
676
|
+
'__rumiousEvents' in target) {
|
677
|
+
const element = target;
|
678
|
+
const handler = element.__rumiousEvents?.[name];
|
679
|
+
if (handler) {
|
680
|
+
handler(event);
|
681
|
+
break;
|
682
|
+
}
|
683
|
+
}
|
684
|
+
}
|
685
|
+
}
|
686
|
+
function delegateEvents(events) {
|
687
|
+
for (const name of events) {
|
688
|
+
window.addEventListener(name, (e) => triggerEvent(name, e));
|
689
|
+
}
|
690
|
+
}
|
691
|
+
|
692
|
+
function isTemplate(fn) {
|
693
|
+
return typeof fn === 'function' && fn.__isTemplate === true;
|
694
|
+
}
|
695
|
+
|
696
|
+
function handleReactiveNode(node, value, context) {
|
697
|
+
let currentNode = node;
|
698
|
+
const update = () => {
|
699
|
+
if (!document.contains(currentNode) && value.reactor) {
|
700
|
+
value.reactor.removeBinding(update);
|
701
|
+
return;
|
702
|
+
}
|
703
|
+
const newNode = value.value;
|
704
|
+
if (newNode instanceof RumiousComponentElement) {
|
705
|
+
newNode.setContext(context);
|
706
|
+
}
|
707
|
+
currentNode.parentNode?.replaceChild(newNode, currentNode);
|
708
|
+
currentNode = newNode;
|
709
|
+
};
|
710
|
+
context.onRendered.push(() => {
|
711
|
+
update();
|
712
|
+
if (!value.reactor)
|
713
|
+
return;
|
714
|
+
value.reactor.addBinding(update);
|
715
|
+
});
|
716
|
+
return node;
|
717
|
+
}
|
718
|
+
function isPrimitive(value) {
|
719
|
+
return value === null || (typeof value !== 'object' && typeof value !== 'function');
|
720
|
+
}
|
721
|
+
function createDynamicValue(context, value) {
|
722
|
+
if (Array.isArray(value)) {
|
723
|
+
const fragment = document.createDocumentFragment();
|
724
|
+
for (const item of value) {
|
725
|
+
if (isTemplate(item)) {
|
726
|
+
const rendered = item(document.createDocumentFragment(), context);
|
727
|
+
fragment.appendChild(rendered);
|
728
|
+
}
|
729
|
+
else if (isPrimitive(item)) {
|
730
|
+
if (item !== null && item !== undefined && item !== false) {
|
731
|
+
fragment.appendChild(document.createTextNode(String(item)));
|
732
|
+
}
|
733
|
+
}
|
734
|
+
}
|
735
|
+
return fragment;
|
736
|
+
}
|
737
|
+
if (isTemplate(value)) {
|
738
|
+
return value(document.createDocumentFragment(), context);
|
739
|
+
}
|
740
|
+
if (value instanceof RumiousState && value.value instanceof Node) {
|
741
|
+
return handleReactiveNode(document.createTextNode(''), value, context);
|
742
|
+
}
|
743
|
+
if (value instanceof RumiousState && value.reactor) {
|
744
|
+
let node = document.createTextNode('');
|
745
|
+
context.onRendered.push(() => {
|
746
|
+
node.textContent = String(value.get());
|
747
|
+
if (!value.reactor)
|
748
|
+
return;
|
749
|
+
value.reactor.addBinding((commit) => node.textContent = String(commit.state.get()));
|
750
|
+
});
|
751
|
+
return node;
|
752
|
+
}
|
753
|
+
if (isPrimitive(value) && value !== null && value !== undefined && value !== false) {
|
754
|
+
return document.createTextNode(String(value));
|
755
|
+
}
|
756
|
+
return document.createTextNode('');
|
757
|
+
}
|
758
|
+
|
759
|
+
function createTemplate(fn) {
|
760
|
+
return Object.assign(fn, {
|
761
|
+
__isTemplate: true
|
762
|
+
});
|
763
|
+
}
|
764
|
+
function html(h) {
|
765
|
+
let template = document.createElement('template');
|
766
|
+
template.innerHTML = h;
|
767
|
+
return template.content.cloneNode(true);
|
768
|
+
}
|
769
|
+
const directives = {
|
770
|
+
ref(context, modifier, target, value) {
|
771
|
+
if (value instanceof RumiousRef) {
|
772
|
+
value.setTarget(target);
|
773
|
+
}
|
774
|
+
else {
|
775
|
+
throw new Error("Cannot setup element reference for non-RumiousRef object !");
|
776
|
+
}
|
777
|
+
},
|
778
|
+
model(context, modifier, element, state) {
|
779
|
+
const tag = element.tagName, type = element.type;
|
780
|
+
if (tag === "TEXTAREA") {
|
781
|
+
element.addEventListener("input", () => state.set(element.value));
|
782
|
+
}
|
783
|
+
else if (tag === "SELECT") {
|
784
|
+
element.addEventListener("change", () => {
|
785
|
+
const s = element;
|
786
|
+
state.set(s.multiple ? Array.from(s.selectedOptions).map(o => o.value) : s.value);
|
787
|
+
});
|
788
|
+
}
|
789
|
+
else if (tag === "INPUT") {
|
790
|
+
if (type === "checkbox") {
|
791
|
+
element.addEventListener("change", () => state.set(element.checked));
|
792
|
+
}
|
793
|
+
else if (type === "radio") {
|
794
|
+
element.addEventListener("change", () => {
|
795
|
+
const i = element;
|
796
|
+
if (i.checked)
|
797
|
+
state.set(i.value);
|
798
|
+
});
|
799
|
+
}
|
800
|
+
else if (type === "file") {
|
801
|
+
element.addEventListener("change", () => {
|
802
|
+
const f = element.files;
|
803
|
+
state.set(element.hasAttribute("multiple") ? f : f?.[0] ?? null);
|
804
|
+
});
|
805
|
+
}
|
806
|
+
else {
|
807
|
+
element.addEventListener("input", () => {
|
808
|
+
const val = element.value;
|
809
|
+
state.set(type === "number" ? (val === "" ? null : +val) : val);
|
810
|
+
});
|
811
|
+
}
|
812
|
+
}
|
813
|
+
},
|
814
|
+
on(context, event, element, callback) {
|
815
|
+
createEvent(element, event, callback);
|
816
|
+
},
|
817
|
+
bind(context, modifier, element, state) {
|
818
|
+
let reactive = () => { };
|
819
|
+
switch (modifier) {
|
820
|
+
case 'text':
|
821
|
+
reactive = () => { element.textContent = String(state.get()); };
|
822
|
+
break;
|
823
|
+
case 'html':
|
824
|
+
reactive = () => { element.innerHTML = String(state.get()); };
|
825
|
+
break;
|
826
|
+
case 'style':
|
827
|
+
reactive = () => {
|
828
|
+
const styles = state.get();
|
829
|
+
if (typeof styles === 'string') {
|
830
|
+
element.setAttribute('style', styles);
|
831
|
+
}
|
832
|
+
else if (typeof styles === 'object') {
|
833
|
+
Object.assign(element.style, styles);
|
834
|
+
}
|
835
|
+
};
|
836
|
+
break;
|
837
|
+
case 'class':
|
838
|
+
reactive = () => {
|
839
|
+
const cls = state.get();
|
840
|
+
if (typeof cls === 'string')
|
841
|
+
element.className = cls;
|
842
|
+
else if (Array.isArray(cls))
|
843
|
+
element.className = cls.join(' ');
|
844
|
+
else if (typeof cls === 'object') {
|
845
|
+
element.className = Object.entries(cls)
|
846
|
+
.filter(([_, active]) => active)
|
847
|
+
.map(([name]) => name)
|
848
|
+
.join(' ');
|
849
|
+
}
|
850
|
+
};
|
851
|
+
break;
|
852
|
+
case 'disabled':
|
853
|
+
reactive = () => {
|
854
|
+
if ('disabled' in element)
|
855
|
+
element.disabled = Boolean(state.get());
|
856
|
+
};
|
857
|
+
break;
|
858
|
+
case 'checked':
|
859
|
+
reactive = () => {
|
860
|
+
if (element instanceof HTMLInputElement || element instanceof HTMLInputElement) {
|
861
|
+
element.checked = Boolean(state.get());
|
862
|
+
}
|
863
|
+
};
|
864
|
+
break;
|
865
|
+
case 'value':
|
866
|
+
reactive = () => {
|
867
|
+
if ('value' in element)
|
868
|
+
element.value = String(state.get());
|
869
|
+
};
|
870
|
+
break;
|
871
|
+
default:
|
872
|
+
throw new Error(`Unknown bind directive modifier: ${modifier}`);
|
873
|
+
}
|
874
|
+
function onStateChange(commit) {
|
875
|
+
if (!document.contains(element) && state.reactor) {
|
876
|
+
state.reactor.removeInternalBinding(onStateChange);
|
877
|
+
return;
|
878
|
+
}
|
879
|
+
reactive();
|
880
|
+
}
|
881
|
+
context.onRendered.push(() => {
|
882
|
+
reactive();
|
883
|
+
if (!state.reactor)
|
884
|
+
return;
|
885
|
+
state.reactor.addInternalBinding(onStateChange);
|
886
|
+
});
|
887
|
+
},
|
888
|
+
attr(context, attrName, element, state) {
|
889
|
+
function onStateChange(commit) {
|
890
|
+
if (!document.contains(element) && state.reactor) {
|
891
|
+
state.reactor.removeInternalBinding(onStateChange);
|
892
|
+
return;
|
893
|
+
}
|
894
|
+
element.setAttribute(attrName, String(state.get()));
|
895
|
+
}
|
896
|
+
context.onRendered.push(() => {
|
897
|
+
onStateChange();
|
898
|
+
if (!state.reactor)
|
899
|
+
return;
|
900
|
+
state.reactor.addInternalBinding(onStateChange);
|
901
|
+
});
|
902
|
+
},
|
903
|
+
prop(context, name, element, state) {
|
904
|
+
function onStateChange(commit) {
|
905
|
+
if (!document.contains(element) && state.reactor) {
|
906
|
+
state.reactor.removeInternalBinding(onStateChange);
|
907
|
+
return;
|
908
|
+
}
|
909
|
+
element[name] = state.get();
|
910
|
+
}
|
911
|
+
context.onRendered.push(() => {
|
912
|
+
onStateChange();
|
913
|
+
if (!state.reactor)
|
914
|
+
return;
|
915
|
+
state.reactor.addInternalBinding(onStateChange);
|
916
|
+
});
|
917
|
+
},
|
918
|
+
html(context, modifier, element, state) {
|
919
|
+
function onStateChange(commit) {
|
920
|
+
if (!document.contains(element) && state.reactor) {
|
921
|
+
state.reactor.removeInternalBinding(onStateChange);
|
922
|
+
return;
|
923
|
+
}
|
924
|
+
element.innerHTML = String(state.get());
|
925
|
+
}
|
926
|
+
context.onRendered.push(() => {
|
927
|
+
onStateChange();
|
928
|
+
if (!state.reactor)
|
929
|
+
return;
|
930
|
+
state.reactor.addInternalBinding(onStateChange);
|
931
|
+
});
|
932
|
+
},
|
933
|
+
show(context, modifier, element, state) {
|
934
|
+
function onStateChange(commit) {
|
935
|
+
if (!document.contains(element) && state.reactor) {
|
936
|
+
state.reactor.removeInternalBinding(onStateChange);
|
937
|
+
return;
|
938
|
+
}
|
939
|
+
element.style.display = Boolean(state.get()) ? 'block' : 'none';
|
940
|
+
}
|
941
|
+
context.onRendered.push(() => {
|
942
|
+
onStateChange();
|
943
|
+
if (!state.reactor)
|
944
|
+
return;
|
945
|
+
state.reactor.addInternalBinding(onStateChange);
|
946
|
+
});
|
947
|
+
},
|
948
|
+
hide(context, modifier, element, state) {
|
949
|
+
function onStateChange(commit) {
|
950
|
+
if (!document.contains(element) && state.reactor) {
|
951
|
+
state.reactor.removeInternalBinding(onStateChange);
|
952
|
+
return;
|
953
|
+
}
|
954
|
+
element.style.display = !Boolean(state.get()) ? 'block' : 'none';
|
955
|
+
}
|
956
|
+
context.onRendered.push(() => {
|
957
|
+
onStateChange();
|
958
|
+
if (!state.reactor)
|
959
|
+
return;
|
960
|
+
state.reactor.addInternalBinding(onStateChange);
|
961
|
+
});
|
962
|
+
},
|
963
|
+
each(context, modifier, element, configs) {
|
964
|
+
context = new RumiousRenderContext(context.app, context.target);
|
965
|
+
const keyToNode = new Map();
|
966
|
+
const nodeOrder = [];
|
967
|
+
for (const item of configs.value.value) {
|
968
|
+
const key = configs.key(item);
|
969
|
+
const templ = renderFrag(configs.templ(item, key), context);
|
970
|
+
const dom = templ.childNodes[0];
|
971
|
+
keyToNode.set(key, dom);
|
972
|
+
nodeOrder.push(key);
|
973
|
+
element.appendChild(dom);
|
974
|
+
}
|
975
|
+
if (!configs.value.reactor)
|
976
|
+
return;
|
977
|
+
configs.value.reactor.addInternalBinding((commit) => {
|
978
|
+
const value = commit.value;
|
979
|
+
const key = configs.key(value);
|
980
|
+
if (commit.type === 'remove') {
|
981
|
+
const oldDom = keyToNode.get(key);
|
982
|
+
if (oldDom) {
|
983
|
+
element.removeChild(oldDom);
|
984
|
+
keyToNode.delete(key);
|
985
|
+
const index = nodeOrder.indexOf(key);
|
986
|
+
if (index !== -1)
|
987
|
+
nodeOrder.splice(index, 1);
|
988
|
+
}
|
989
|
+
return;
|
990
|
+
}
|
991
|
+
const templ = renderFrag(configs.templ(value, key), context);
|
992
|
+
const dom = templ.childNodes[0];
|
993
|
+
switch (commit.type) {
|
994
|
+
case 'append':
|
995
|
+
keyToNode.set(key, dom);
|
996
|
+
nodeOrder.push(key);
|
997
|
+
element.appendChild(dom);
|
998
|
+
break;
|
999
|
+
case 'prepend':
|
1000
|
+
keyToNode.set(key, dom);
|
1001
|
+
nodeOrder.unshift(key);
|
1002
|
+
element.prepend(dom);
|
1003
|
+
break;
|
1004
|
+
case 'update': {
|
1005
|
+
const oldDom = keyToNode.get(key);
|
1006
|
+
if (oldDom) {
|
1007
|
+
keyToNode.set(key, dom);
|
1008
|
+
element.replaceChild(dom, oldDom);
|
1009
|
+
}
|
1010
|
+
break;
|
1011
|
+
}
|
1012
|
+
case 'insert': {
|
1013
|
+
const index = commit.key;
|
1014
|
+
const anchorKey = nodeOrder[index];
|
1015
|
+
const anchorNode = keyToNode.get(anchorKey) ?? null;
|
1016
|
+
keyToNode.set(key, dom);
|
1017
|
+
nodeOrder.splice(index, 0, key);
|
1018
|
+
element.insertBefore(dom, anchorNode);
|
1019
|
+
break;
|
1020
|
+
}
|
1021
|
+
}
|
1022
|
+
});
|
1023
|
+
},
|
1024
|
+
view(context, modifier, element, configs) {
|
1025
|
+
configs.addTarget({
|
1026
|
+
element,
|
1027
|
+
context
|
1028
|
+
});
|
1029
|
+
}
|
1030
|
+
};
|
1031
|
+
|
1032
|
+
function createComponent(context, component, props) {
|
1033
|
+
let element = createComponentElement(context, component, props);
|
1034
|
+
return element;
|
1035
|
+
}
|
1036
|
+
|
1037
|
+
class RumiousModule {
|
1038
|
+
}
|
1039
|
+
|
1040
|
+
const __version__ = "2.x";
|
1041
|
+
|
1042
|
+
export { Fragment, RumiousApp, RumiousComponent, RumiousComponentElement, RumiousListState, RumiousModule, RumiousPagination, RumiousRef, RumiousRenderContext, RumiousState, RumiousViewControl, __version__, appendChild, createApp, createComponent, createComponentElement, createDynamicValue, createEvent, createListState, createRef, createState, createTemplate, createViewControl, delegateEvents, directives, element, html, render, renderComponent, renderFrag, replaceNode, unwatch, watch };
|