malinajs 0.6.43
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/CHANGELOG.md +58 -0
- package/LICENSE +21 -0
- package/compile.js +6115 -0
- package/malina-esbuild.js +121 -0
- package/malina-rollup.js +44 -0
- package/malina.js +6094 -0
- package/package.json +43 -0
- package/plugins/sass.js +26 -0
- package/readme.md +58 -0
- package/runtime.js +954 -0
package/runtime.js
ADDED
|
@@ -0,0 +1,954 @@
|
|
|
1
|
+
let __app_onerror = console.error;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const configure = (option) => {
|
|
5
|
+
__app_onerror = option.onerror;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
const isFunction = fn => typeof fn == 'function';
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const isObject = d => typeof d == 'object';
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const safeCall = fn => {
|
|
16
|
+
try {
|
|
17
|
+
return isFunction(fn) && fn();
|
|
18
|
+
} catch (e) {
|
|
19
|
+
__app_onerror(e);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function $watch(cd, fn, callback, w) {
|
|
24
|
+
if(!w) w = {};
|
|
25
|
+
w.fn = fn;
|
|
26
|
+
w.cb = callback;
|
|
27
|
+
if(!('value' in w)) w.value = NaN;
|
|
28
|
+
cd.watchers.push(w);
|
|
29
|
+
return w;
|
|
30
|
+
}
|
|
31
|
+
function $watchReadOnly(cd, fn, callback) {
|
|
32
|
+
return $watch(cd, fn, callback, {ro: true});
|
|
33
|
+
}
|
|
34
|
+
function addEvent(cd, el, event, callback) {
|
|
35
|
+
el.addEventListener(event, callback);
|
|
36
|
+
cd_onDestroy(cd, () => {
|
|
37
|
+
el.removeEventListener(event, callback);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function cd_onDestroy(cd, fn) {
|
|
41
|
+
if(fn) cd._d.push(fn);
|
|
42
|
+
}
|
|
43
|
+
function $$removeItem(array, item) {
|
|
44
|
+
let i = array.indexOf(item);
|
|
45
|
+
if(i>=0) array.splice(i, 1);
|
|
46
|
+
}
|
|
47
|
+
function $ChangeDetector(parent) {
|
|
48
|
+
this.parent = parent;
|
|
49
|
+
this.children = [];
|
|
50
|
+
this.watchers = [];
|
|
51
|
+
this._d = [];
|
|
52
|
+
this.prefix = [];
|
|
53
|
+
this.$$ = parent?.$$;
|
|
54
|
+
}
|
|
55
|
+
$ChangeDetector.prototype.new = function() {
|
|
56
|
+
var cd = new $ChangeDetector(this);
|
|
57
|
+
this.children.push(cd);
|
|
58
|
+
return cd;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
$ChangeDetector.prototype.destroy = function(option) {
|
|
62
|
+
if(option !== false && this.parent) $$removeItem(this.parent.children, this);
|
|
63
|
+
this.watchers.length = 0;
|
|
64
|
+
this.prefix.length = 0;
|
|
65
|
+
this._d.map(safeCall);
|
|
66
|
+
this._d.length = 0;
|
|
67
|
+
this.children.map(cd => cd.destroy(false));
|
|
68
|
+
this.children.length = 0;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
const isArray = (a) => Array.isArray(a);
|
|
73
|
+
|
|
74
|
+
const compareArray = (a, b) => {
|
|
75
|
+
let a0 = isArray(a);
|
|
76
|
+
let a1 = isArray(b);
|
|
77
|
+
if(a0 !== a1) return true;
|
|
78
|
+
if(!a0) return a !== b;
|
|
79
|
+
if(a.length !== b.length) return true;
|
|
80
|
+
for(let i=0;i<a.length;i++) {
|
|
81
|
+
if(a[i] !== b[i]) return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
function $$compareArray(w, value) {
|
|
88
|
+
if(!compareArray(w.value, value)) return 0;
|
|
89
|
+
if(isArray(value)) w.value = value.slice();
|
|
90
|
+
else w.value = value;
|
|
91
|
+
w.cb(w.value);
|
|
92
|
+
return w.ro ? 0 : 1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const compareDeep = (a, b, lvl) => {
|
|
96
|
+
if(lvl < 0 || !a || !b) return a !== b;
|
|
97
|
+
if(a === b) return false;
|
|
98
|
+
let o0 = isObject(a);
|
|
99
|
+
let o1 = isObject(b);
|
|
100
|
+
if(!(o0 && o1)) return a !== b;
|
|
101
|
+
|
|
102
|
+
let a0 = isArray(a);
|
|
103
|
+
let a1 = isArray(b);
|
|
104
|
+
if(a0 !== a1) return true;
|
|
105
|
+
|
|
106
|
+
if(a0) {
|
|
107
|
+
if(a.length !== b.length) return true;
|
|
108
|
+
for(let i=0;i<a.length;i++) {
|
|
109
|
+
if(compareDeep(a[i], b[i], lvl-1)) return true;
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
let set = {};
|
|
113
|
+
for(let k in a) {
|
|
114
|
+
if(compareDeep(a[k], b[k], lvl-1)) return true;
|
|
115
|
+
set[k] = true;
|
|
116
|
+
}
|
|
117
|
+
for(let k in b) {
|
|
118
|
+
if(set[k]) continue;
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return false;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
function cloneDeep(d, lvl) {
|
|
127
|
+
if(lvl < 0 || !d) return d;
|
|
128
|
+
|
|
129
|
+
if(isObject(d)) {
|
|
130
|
+
if(d instanceof Date) return d;
|
|
131
|
+
if(d instanceof Element) return d;
|
|
132
|
+
if(isArray(d)) return d.map(i => cloneDeep(i, lvl-1));
|
|
133
|
+
let r = {};
|
|
134
|
+
for(let k in d) r[k] = cloneDeep(d[k], lvl-1);
|
|
135
|
+
return r;
|
|
136
|
+
}
|
|
137
|
+
return d;
|
|
138
|
+
}
|
|
139
|
+
const $$cloneDeep = function(d) {
|
|
140
|
+
return cloneDeep(d, 10);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
function $$deepComparator(depth) {
|
|
144
|
+
return function(w, value) {
|
|
145
|
+
let diff = compareDeep(w.value, value, depth);
|
|
146
|
+
diff && (w.value = cloneDeep(value, depth), !w.idle && w.cb(value));
|
|
147
|
+
w.idle = false;
|
|
148
|
+
return !w.ro && diff ? 1 : 0;
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const $$compareDeep = $$deepComparator(10);
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
const keyComparator = (w, value) => {
|
|
155
|
+
let diff = false;
|
|
156
|
+
for(let k in value) {
|
|
157
|
+
if(w.value[k] != value[k]) diff = true;
|
|
158
|
+
w.value[k] = value[k];
|
|
159
|
+
}
|
|
160
|
+
diff && !w.idle && w.cb(value);
|
|
161
|
+
w.idle = false;
|
|
162
|
+
return !w.ro && diff ? 1 : 0;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
const fire = w => {
|
|
167
|
+
if(w.cmp) w.cmp(w, w.fn());
|
|
168
|
+
else {
|
|
169
|
+
w.value = w.fn();
|
|
170
|
+
w.cb(w.value);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
function $digest($cd) {
|
|
175
|
+
let loop = 10;
|
|
176
|
+
let w;
|
|
177
|
+
while(loop >= 0) {
|
|
178
|
+
let changes = 0;
|
|
179
|
+
let index = 0;
|
|
180
|
+
let queue = [];
|
|
181
|
+
let i, value, cd = $cd;
|
|
182
|
+
while(cd) {
|
|
183
|
+
for(i=0;i<cd.prefix.length;i++) cd.prefix[i]();
|
|
184
|
+
for(i=0;i<cd.watchers.length;i++) {
|
|
185
|
+
w = cd.watchers[i];
|
|
186
|
+
value = w.fn();
|
|
187
|
+
if(w.value !== value) {
|
|
188
|
+
if(w.cmp) {
|
|
189
|
+
changes += w.cmp(w, value);
|
|
190
|
+
} else {
|
|
191
|
+
w.value = value;
|
|
192
|
+
if(!w.ro) changes++;
|
|
193
|
+
w.cb(w.value);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
} if(cd.children.length) queue.push.apply(queue, cd.children);
|
|
197
|
+
cd = queue[index++];
|
|
198
|
+
}
|
|
199
|
+
loop--;
|
|
200
|
+
if(!changes) break;
|
|
201
|
+
}
|
|
202
|
+
if(loop < 0) __app_onerror('Infinity changes: ', w);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let templatecache = {};
|
|
206
|
+
let templatecacheSvg = {};
|
|
207
|
+
|
|
208
|
+
let $$uniqIndex = 1;
|
|
209
|
+
|
|
210
|
+
const childNodes = 'childNodes';
|
|
211
|
+
const firstChild = 'firstChild';
|
|
212
|
+
|
|
213
|
+
let noop = a => a;
|
|
214
|
+
|
|
215
|
+
const insertAfter = (label, node) => {
|
|
216
|
+
label.parentNode.insertBefore(node, label.nextSibling);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const createTextNode = (text) => {
|
|
220
|
+
let f = document.createDocumentFragment();
|
|
221
|
+
f.append(text);
|
|
222
|
+
return f;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const $$htmlToFragment = (html) => {
|
|
226
|
+
if(templatecache[html]) return templatecache[html].cloneNode(true);
|
|
227
|
+
|
|
228
|
+
let t = document.createElement('template');
|
|
229
|
+
t.innerHTML = html.replace(/<>/g, '<!---->');
|
|
230
|
+
let result = t.content;
|
|
231
|
+
templatecache[html] = result.cloneNode(true);
|
|
232
|
+
return result;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const $$htmlToFragmentClean = (html) => {
|
|
236
|
+
if(templatecache[html]) return templatecache[html].cloneNode(true);
|
|
237
|
+
|
|
238
|
+
let t = document.createElement('template');
|
|
239
|
+
t.innerHTML = html.replace(/<>/g, '<!---->');
|
|
240
|
+
let result = t.content;
|
|
241
|
+
|
|
242
|
+
let it = document.createNodeIterator(result, 128);
|
|
243
|
+
let n;
|
|
244
|
+
while(n = it.nextNode()) {
|
|
245
|
+
if(!n.nodeValue) n.parentNode.replaceChild(document.createTextNode(''), n);
|
|
246
|
+
} templatecache[html] = result.cloneNode(true);
|
|
247
|
+
return result;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
function svgToFragment(content) {
|
|
251
|
+
if(templatecacheSvg[content]) return templatecacheSvg[content].cloneNode(true);
|
|
252
|
+
let t = document.createElement('template');
|
|
253
|
+
t.innerHTML = '<svg>' + content + '</svg>';
|
|
254
|
+
|
|
255
|
+
let result = document.createDocumentFragment();
|
|
256
|
+
let svg = t.content[firstChild];
|
|
257
|
+
while(svg[firstChild]) result.appendChild(svg[firstChild]);
|
|
258
|
+
templatecacheSvg[content] = result.cloneNode(true);
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
function $$removeElements(el, last) {
|
|
262
|
+
let next;
|
|
263
|
+
while(el) {
|
|
264
|
+
next = el.nextSibling;
|
|
265
|
+
el.remove();
|
|
266
|
+
if(el == last) break;
|
|
267
|
+
el = next;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function removeElementsBetween(el, stop) {
|
|
271
|
+
let next;
|
|
272
|
+
el = el.nextSibling;
|
|
273
|
+
while(el) {
|
|
274
|
+
next = el.nextSibling;
|
|
275
|
+
if(el == stop) break;
|
|
276
|
+
el.remove();
|
|
277
|
+
el = next;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const getFinalLabel = n => {
|
|
281
|
+
if(n.nextSibling) return n.nextSibling;
|
|
282
|
+
let e = document.createTextNode('');
|
|
283
|
+
n.parentNode.appendChild(e);
|
|
284
|
+
return e;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
let _tick_list = [];
|
|
289
|
+
let _tick_planned = {};
|
|
290
|
+
function $tick(fn, uniq) {
|
|
291
|
+
if(uniq) {
|
|
292
|
+
if(_tick_planned[uniq]) return;
|
|
293
|
+
_tick_planned[uniq] = true;
|
|
294
|
+
}
|
|
295
|
+
_tick_list.push(fn);
|
|
296
|
+
if(_tick_planned.$tick) return;
|
|
297
|
+
_tick_planned.$tick = true;
|
|
298
|
+
setTimeout(() => {
|
|
299
|
+
_tick_planned = {};
|
|
300
|
+
let list = _tick_list;
|
|
301
|
+
_tick_list = [];
|
|
302
|
+
list.map(safeCall);
|
|
303
|
+
}, 0);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function $makeEmitter(option) {
|
|
307
|
+
return (name, detail) => {
|
|
308
|
+
let fn = option.events[name];
|
|
309
|
+
if(!fn) return;
|
|
310
|
+
let e = document.createEvent('CustomEvent');
|
|
311
|
+
e.initCustomEvent(name, false, false, detail);
|
|
312
|
+
fn(e);
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function $$addEventForComponent(list, event, fn) {
|
|
317
|
+
let prev = list[event];
|
|
318
|
+
if(prev) {
|
|
319
|
+
if(prev._list) prev._list.push(fn);
|
|
320
|
+
else {
|
|
321
|
+
function handler(e) {
|
|
322
|
+
handler._list.forEach(fn => {
|
|
323
|
+
fn(e);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
handler._list = [prev, fn];
|
|
327
|
+
list[event] = handler;
|
|
328
|
+
}
|
|
329
|
+
} else list[event] = fn;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
let current_component, $context;
|
|
333
|
+
|
|
334
|
+
const $onDestroy = fn => current_component._d.push(fn);
|
|
335
|
+
const $onMount = fn => current_component._m.push(fn);
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
const $insertElementByOption = ($label, $option, $element) => {
|
|
339
|
+
if ($option.$l) {
|
|
340
|
+
insertAfter($label, $element);
|
|
341
|
+
} else {
|
|
342
|
+
$label.appendChild($element);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
const $readOnlyBase = {
|
|
348
|
+
a: ($component) => {
|
|
349
|
+
$component.$cd = {
|
|
350
|
+
_d: $component._d,
|
|
351
|
+
watchers: [],
|
|
352
|
+
prefix: [],
|
|
353
|
+
new: () => $component.$cd,
|
|
354
|
+
destroy: noop,
|
|
355
|
+
$$: $component
|
|
356
|
+
};
|
|
357
|
+
},
|
|
358
|
+
b: ($component) => {
|
|
359
|
+
let watchers = $component.$cd.watchers;
|
|
360
|
+
let prefix = $component.$cd.prefix;
|
|
361
|
+
while(watchers.length || prefix.length) {
|
|
362
|
+
let wl = watchers.slice();
|
|
363
|
+
watchers.length = 0;
|
|
364
|
+
prefix.forEach(safeCall);
|
|
365
|
+
prefix.length = 0;
|
|
366
|
+
wl.forEach(w => w.cb(w.fn()));
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
const $base = {
|
|
373
|
+
a: ($component) => {
|
|
374
|
+
let $cd = new $ChangeDetector();
|
|
375
|
+
$cd.$$ = $component;
|
|
376
|
+
$onDestroy(() => $cd.destroy());
|
|
377
|
+
|
|
378
|
+
let id = `a${$$uniqIndex++}`;
|
|
379
|
+
let process;
|
|
380
|
+
let apply = r => {
|
|
381
|
+
if (process) return r;
|
|
382
|
+
$tick(() => {
|
|
383
|
+
try {
|
|
384
|
+
process = true;
|
|
385
|
+
$digest($cd);
|
|
386
|
+
} finally {
|
|
387
|
+
process = false;
|
|
388
|
+
}
|
|
389
|
+
}, id);
|
|
390
|
+
return r;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
$component.$cd = $cd;
|
|
394
|
+
$component.apply = apply;
|
|
395
|
+
$component.push = apply;
|
|
396
|
+
},
|
|
397
|
+
b: ($component) => {
|
|
398
|
+
$component.apply();
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
const makeComponent = (init, $base) => {
|
|
404
|
+
return ($element, $option={}) => {
|
|
405
|
+
let prev = current_component;
|
|
406
|
+
$context = $option.context || {};
|
|
407
|
+
let $component = current_component = {
|
|
408
|
+
$option,
|
|
409
|
+
destroy: () => $component._d.map(safeCall),
|
|
410
|
+
context: $context,
|
|
411
|
+
exported: {},
|
|
412
|
+
_d: [],
|
|
413
|
+
_m: []
|
|
414
|
+
};
|
|
415
|
+
$base.a($component);
|
|
416
|
+
|
|
417
|
+
try {
|
|
418
|
+
$insertElementByOption($element, $option, init($option, $component.apply));
|
|
419
|
+
$base.b($component);
|
|
420
|
+
} finally {
|
|
421
|
+
current_component = prev;
|
|
422
|
+
$context = null;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
$tick(() => $component._d.push(...$component._m.map(safeCall)));
|
|
426
|
+
return $component;
|
|
427
|
+
};
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
const callComponent = (cd, context, component, label, option, propFn, cmp, setter, classFn) => {
|
|
432
|
+
option.$l = 1;
|
|
433
|
+
option.context = {...context};
|
|
434
|
+
let $component, parentWatch, childWatch;
|
|
435
|
+
|
|
436
|
+
if(propFn) {
|
|
437
|
+
if(cmp) {
|
|
438
|
+
parentWatch = $watch(cd, propFn, value => {
|
|
439
|
+
option.props = value;
|
|
440
|
+
if($component) {
|
|
441
|
+
$component.push?.();
|
|
442
|
+
childWatch && (childWatch.idle = true);
|
|
443
|
+
$component.apply?.();
|
|
444
|
+
}
|
|
445
|
+
}, {ro: true, value: {}, cmp});
|
|
446
|
+
fire(parentWatch);
|
|
447
|
+
} else option.props = propFn();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if(classFn) {
|
|
451
|
+
fire($watch(cd, classFn, value => {
|
|
452
|
+
option.$class = value;
|
|
453
|
+
$component?.apply?.();
|
|
454
|
+
}, {ro: true, value: {}, cmp: keyComparator}));
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
$component = safeCall(() => component(label, option));
|
|
458
|
+
if($component) {
|
|
459
|
+
cd_onDestroy(cd, $component.destroy);
|
|
460
|
+
|
|
461
|
+
if(setter && $component.exportedProps) {
|
|
462
|
+
childWatch = $watch($component.$cd, $component.exportedProps, value => {
|
|
463
|
+
setter(value);
|
|
464
|
+
cd.$$.apply();
|
|
465
|
+
}, {ro: true, idle: true, value: parentWatch.value, cmp});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return $component;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
const autoSubscribe = (...list) => {
|
|
473
|
+
list.forEach(i => {
|
|
474
|
+
if(i.subscribe) {
|
|
475
|
+
let unsub = i.subscribe(current_component.apply);
|
|
476
|
+
if(isFunction(unsub)) cd_onDestroy(current_component, unsub);
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
const addStyles = (id, content) => {
|
|
483
|
+
if(document.head.querySelector('style#' + id)) return;
|
|
484
|
+
let style = document.createElement('style');
|
|
485
|
+
style.id = id;
|
|
486
|
+
style.innerHTML = content;
|
|
487
|
+
document.head.appendChild(style);
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
const addClass = (el, className) => el.classList.add(className);
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
const bindClass = (cd, element, fn, className) => {
|
|
495
|
+
$watchReadOnly(cd, fn, value => {
|
|
496
|
+
if(value) addClass(element, className);
|
|
497
|
+
else element.classList.remove(className);
|
|
498
|
+
});
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
const setClassToElement = (element, value) => {
|
|
503
|
+
if(typeof element.className == 'string') element.className = value;
|
|
504
|
+
else element.className.baseVal = value;
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
const bindText = (cd, element, fn) => {
|
|
509
|
+
$watchReadOnly(cd, () => '' + fn(), value => {
|
|
510
|
+
element.textContent = value;
|
|
511
|
+
});
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
const bindStyle = (cd, element, name, fn) => {
|
|
516
|
+
$watchReadOnly(cd, fn, (value) => {
|
|
517
|
+
element.style[name] = value;
|
|
518
|
+
});
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
const bindAttributeBase = (element, name, value) => {
|
|
523
|
+
if(value != null) element.setAttribute(name, value);
|
|
524
|
+
else element.removeAttribute(name);
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
const bindAttribute = (cd, element, name, fn) => {
|
|
529
|
+
$watchReadOnly(cd, () => {
|
|
530
|
+
let v = fn();
|
|
531
|
+
return v == null ? v : '' + v;
|
|
532
|
+
}, value => bindAttributeBase(element, name, value));
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
const bindAction = (cd, element, action, fn, subscribe) => {
|
|
537
|
+
$tick(() => {
|
|
538
|
+
let handler, value;
|
|
539
|
+
if(fn) {
|
|
540
|
+
value = fn();
|
|
541
|
+
handler = action.apply(null, [element].concat(value));
|
|
542
|
+
} else handler = action(element);
|
|
543
|
+
if(handler?.destroy) cd_onDestroy(cd, handler.destroy);
|
|
544
|
+
subscribe?.(cd, fn, handler, value);
|
|
545
|
+
});
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
const __bindActionSubscribe = (cd, fn, handler, value) => {
|
|
550
|
+
if(handler?.update && fn) {
|
|
551
|
+
$watch(cd, fn, args => {
|
|
552
|
+
handler.update.apply(handler, args);
|
|
553
|
+
}, {cmp: $$deepComparator(1), value: cloneDeep(value, 1) });
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
const bindInput = (cd, element, name, get, set) => {
|
|
559
|
+
let w = $watchReadOnly(cd, name == 'checked' ? () => !!get() : get, value => {
|
|
560
|
+
element[name] = value == null ? '' : value;
|
|
561
|
+
});
|
|
562
|
+
addEvent(cd, element, 'input', () => {
|
|
563
|
+
set(w.value = element[name]);
|
|
564
|
+
});
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
const makeClassResolver = ($option, classMap, metaClass, mainName) => {
|
|
569
|
+
if(!$option.$class) $option.$class = {};
|
|
570
|
+
if(!mainName && metaClass.main) mainName = 'main';
|
|
571
|
+
return (line, defaults) => {
|
|
572
|
+
let result = [];
|
|
573
|
+
if(defaults) result.push(defaults);
|
|
574
|
+
line.trim().split(/\s+/).forEach(name => {
|
|
575
|
+
let meta;
|
|
576
|
+
if(name[0] == '$') {
|
|
577
|
+
name = name.substring(1);
|
|
578
|
+
meta = true;
|
|
579
|
+
}
|
|
580
|
+
let h = metaClass[name] || meta;
|
|
581
|
+
if(h) {
|
|
582
|
+
let className = ($option.$class[name === mainName ? '$$main' : name] || '').trim();
|
|
583
|
+
if(className) {
|
|
584
|
+
result.push(className);
|
|
585
|
+
} else if(h !== true) {
|
|
586
|
+
result.push(name, h);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
let h2 = classMap[name];
|
|
590
|
+
if(h2) {
|
|
591
|
+
result.push(name, h2);
|
|
592
|
+
} else if(!h) {
|
|
593
|
+
result.push(name);
|
|
594
|
+
}
|
|
595
|
+
});
|
|
596
|
+
return result.join(' ');
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
const makeExternalProperty = ($component, name, getter, setter) => {
|
|
602
|
+
Object.defineProperty($component, name, {
|
|
603
|
+
get: getter,
|
|
604
|
+
set: v => {setter(v); $component.apply();}
|
|
605
|
+
});
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
const attachSlotBase = ($context, $cd, slotName, label, props, placeholder) => {
|
|
610
|
+
let $slot = $cd.$$.$option.slots?.[slotName];
|
|
611
|
+
if($slot) $slot($cd, label, $context, props);
|
|
612
|
+
else placeholder?.();
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
const attachSlot = ($context, $cd, slotName, label, props, placeholder, cmp) => {
|
|
617
|
+
let $slot = $cd.$$.$option.slots?.[slotName];
|
|
618
|
+
if($slot) {
|
|
619
|
+
let resultProps = {}, push;
|
|
620
|
+
if(props) {
|
|
621
|
+
let setter = (k) => {
|
|
622
|
+
return v => {
|
|
623
|
+
resultProps[k] = v;
|
|
624
|
+
push?.();
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
for(let k in props) {
|
|
628
|
+
let v = props[k];
|
|
629
|
+
if(isFunction(v)) {
|
|
630
|
+
fire($watch($cd, v, setter(k), {ro: true, cmp}));
|
|
631
|
+
} else resultProps[k] = v;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
push = $slot($cd, label, $context, resultProps);
|
|
635
|
+
} else placeholder?.();
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
const makeSlot = (parentCD, fn) => {
|
|
640
|
+
return (callerCD, label, $context, props) => {
|
|
641
|
+
let $cd = parentCD.new();
|
|
642
|
+
cd_onDestroy(callerCD, () => $cd.destroy());
|
|
643
|
+
let r = fn($cd, $context, callerCD, props || {});
|
|
644
|
+
insertAfter(label, r.el || r);
|
|
645
|
+
$cd.$$.apply?.();
|
|
646
|
+
return r.push;
|
|
647
|
+
};
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
const makeSlotStatic = (fn) => {
|
|
652
|
+
return (callerCD, label) => {
|
|
653
|
+
insertAfter(label, fn());
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
const eachDefaultKey = (item, index, array) => isObject(array[0]) ? item : index;
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
const attachAnchor = ($option, $cd, name, el) => {
|
|
662
|
+
let fn = $option.anchor?.[name];
|
|
663
|
+
if(fn) cd_onDestroy($cd, fn(el));
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
const spreadAttributes = (cd, el, fn) => {
|
|
668
|
+
const props = Object.getOwnPropertyDescriptors(el.__proto__);
|
|
669
|
+
let prev = {};
|
|
670
|
+
const set = (k, v) => {
|
|
671
|
+
if(k == 'style') el.style.cssText = v;
|
|
672
|
+
else if(props[k]?.set) el[k] = v;
|
|
673
|
+
else bindAttributeBase(el, k, v);
|
|
674
|
+
};
|
|
675
|
+
const apply = (state) => {
|
|
676
|
+
for(let k in state) {
|
|
677
|
+
let value = state[k];
|
|
678
|
+
if(prev[k] != value) {
|
|
679
|
+
set(k, value);
|
|
680
|
+
prev[k] = value;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
for(let k in prev) {
|
|
684
|
+
if(!(k in state)) {
|
|
685
|
+
set(k, null);
|
|
686
|
+
delete prev[k];
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
$watch(cd, fn, apply, {cmp: (_, state) => {
|
|
691
|
+
apply(state);
|
|
692
|
+
return 0;
|
|
693
|
+
}});
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
const attchExportedFragment = ($parentCD, $childCD, name, label, props, events, template, innerFn) => {
|
|
698
|
+
let fn = $childCD.$$.exported[name];
|
|
699
|
+
if(fn) {
|
|
700
|
+
let slot;
|
|
701
|
+
if(template) {
|
|
702
|
+
slot = (childCD, label) => {
|
|
703
|
+
const $parentElement = $$htmlToFragment(template);
|
|
704
|
+
if(innerFn) {
|
|
705
|
+
let $cd = $parentCD.new();
|
|
706
|
+
cd_onDestroy(childCD, () => $cd.destroy());
|
|
707
|
+
innerFn($cd, $parentElement);
|
|
708
|
+
$cd.$$.apply?.();
|
|
709
|
+
}
|
|
710
|
+
insertAfter(label, $parentElement);
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
if(isFunction(props)) props = props($parentCD, $childCD);
|
|
715
|
+
fn($parentCD, $childCD, label, props, events, slot);
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
const exportFragment = ($component, name, fn) => {
|
|
721
|
+
$component.exported[name] = ($parentCD, $childCD, label, props, events, slot) => {
|
|
722
|
+
let $cd = $childCD.new();
|
|
723
|
+
cd_onDestroy($parentCD, () => $cd.destroy());
|
|
724
|
+
fn($cd, label, props, events, slot);
|
|
725
|
+
$component.apply();
|
|
726
|
+
};
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
const prefixPush = ($cd, fn) => {
|
|
731
|
+
$cd.prefix.push(fn);
|
|
732
|
+
fn();
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
const observeProps = (cmp, fn) => {
|
|
737
|
+
return (cd, target) => {
|
|
738
|
+
let result;
|
|
739
|
+
fire($watch(cd, fn, value => {
|
|
740
|
+
result = value;
|
|
741
|
+
target.$$.apply();
|
|
742
|
+
}, {ro: true, value: {}, cmp}));
|
|
743
|
+
return () => result;
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
const unwrapProps = (cd, props, fn) => {
|
|
749
|
+
if(props) {
|
|
750
|
+
if(isFunction(props)) prefixPush(cd, () => fn(props()));
|
|
751
|
+
else fn(props);
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
function $$htmlBlock($cd, tag, fn) {
|
|
756
|
+
let lastElement;
|
|
757
|
+
let create = (html) => {
|
|
758
|
+
let fr;
|
|
759
|
+
if(tag.parentElement instanceof SVGElement) fr = svgToFragment(html);
|
|
760
|
+
else fr = $$htmlToFragment(html);
|
|
761
|
+
lastElement = fr.lastChild;
|
|
762
|
+
insertAfter(tag, fr);
|
|
763
|
+
};
|
|
764
|
+
let destroy = () => {
|
|
765
|
+
if(!lastElement) return;
|
|
766
|
+
let next, el = tag.nextSibling;
|
|
767
|
+
while(el) {
|
|
768
|
+
next = el.nextSibling;
|
|
769
|
+
el.remove();
|
|
770
|
+
if(el == lastElement) break;
|
|
771
|
+
el = next;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
lastElement = null;
|
|
775
|
+
};
|
|
776
|
+
$watch($cd, fn, (html) => {
|
|
777
|
+
destroy();
|
|
778
|
+
if(html) create(html);
|
|
779
|
+
}, {ro: true});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function $$ifBlock($cd, $parentElement, fn, tpl, build, tplElse, buildElse) {
|
|
783
|
+
let childCD;
|
|
784
|
+
let first, last;
|
|
785
|
+
|
|
786
|
+
function create(fr, builder) {
|
|
787
|
+
childCD = $cd.new();
|
|
788
|
+
let tpl = fr.cloneNode(true);
|
|
789
|
+
builder(childCD, tpl);
|
|
790
|
+
first = tpl[firstChild];
|
|
791
|
+
last = tpl.lastChild;
|
|
792
|
+
insertAfter($parentElement, tpl);
|
|
793
|
+
}
|
|
794
|
+
function destroy() {
|
|
795
|
+
if(!childCD) return;
|
|
796
|
+
childCD.destroy();
|
|
797
|
+
childCD = null;
|
|
798
|
+
$$removeElements(first, last);
|
|
799
|
+
first = last = null;
|
|
800
|
+
}
|
|
801
|
+
$watch($cd, fn, (value) => {
|
|
802
|
+
if(value) {
|
|
803
|
+
destroy();
|
|
804
|
+
create(tpl, build);
|
|
805
|
+
} else {
|
|
806
|
+
destroy();
|
|
807
|
+
if(buildElse) create(tplElse, buildElse);
|
|
808
|
+
}
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function $$awaitBlock($cd, label, relation, fn, $$apply, build_main, tpl_main, build_then, tpl_then, build_catch, tpl_catch) {
|
|
813
|
+
let promise, childCD;
|
|
814
|
+
let first, last, status = 0;
|
|
815
|
+
|
|
816
|
+
function remove() {
|
|
817
|
+
if(!childCD) return;
|
|
818
|
+
childCD.destroy();
|
|
819
|
+
childCD = null;
|
|
820
|
+
$$removeElements(first, last);
|
|
821
|
+
first = last = null;
|
|
822
|
+
}
|
|
823
|
+
function render(build, tpl, value) {
|
|
824
|
+
if(childCD) remove();
|
|
825
|
+
if(!tpl) return;
|
|
826
|
+
childCD = $cd.new();
|
|
827
|
+
let fr = tpl.cloneNode(true);
|
|
828
|
+
build(childCD, fr, value);
|
|
829
|
+
$$apply();
|
|
830
|
+
first = fr[firstChild];
|
|
831
|
+
last = fr.lastChild;
|
|
832
|
+
insertAfter(label, fr);
|
|
833
|
+
}
|
|
834
|
+
$watch($cd, relation, () => {
|
|
835
|
+
let p = fn();
|
|
836
|
+
if(status !== 1) render(build_main, tpl_main);
|
|
837
|
+
status = 1;
|
|
838
|
+
if(p && p instanceof Promise) {
|
|
839
|
+
promise = p;
|
|
840
|
+
promise.then(value => {
|
|
841
|
+
status = 2;
|
|
842
|
+
if(promise !== p) return;
|
|
843
|
+
render(build_then, tpl_then, value);
|
|
844
|
+
}).catch(value => {
|
|
845
|
+
status = 3;
|
|
846
|
+
if(promise !== p) return;
|
|
847
|
+
render(build_catch, tpl_catch, value);
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
}, {ro: true, cmp: $$deepComparator(1)});
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
function $$eachBlock($parentCD, label, onlyChild, fn, getKey, itemTemplate, bind) {
|
|
854
|
+
let $cd = $parentCD.new();
|
|
855
|
+
|
|
856
|
+
let mapping = new Map();
|
|
857
|
+
let lastNode;
|
|
858
|
+
let tplLength = itemTemplate[childNodes].length;
|
|
859
|
+
|
|
860
|
+
$watch($cd, fn, (array) => {
|
|
861
|
+
if(!array) array = [];
|
|
862
|
+
if(typeof(array) == 'number') array = [...Array(array)].map((_,i) => i + 1);
|
|
863
|
+
else if(!isArray(array)) array = [];
|
|
864
|
+
|
|
865
|
+
let newMapping = new Map();
|
|
866
|
+
let prevNode, parentNode;
|
|
867
|
+
if(onlyChild) {
|
|
868
|
+
prevNode = null;
|
|
869
|
+
parentNode = label;
|
|
870
|
+
} else {
|
|
871
|
+
prevNode = label;
|
|
872
|
+
parentNode = label.parentNode;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if(mapping.size) {
|
|
876
|
+
let ctx, count = 0;
|
|
877
|
+
for(let i=0;i<array.length;i++) {
|
|
878
|
+
ctx = mapping.get(getKey(array[i], i, array));
|
|
879
|
+
if(ctx) {
|
|
880
|
+
ctx.a = true;
|
|
881
|
+
count++;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
if(!count && lastNode) {
|
|
886
|
+
if(onlyChild) label.textContent = '';
|
|
887
|
+
else $$removeElements(label.nextSibling, lastNode);
|
|
888
|
+
$cd.children.forEach(cd => cd.destroy(false));
|
|
889
|
+
$cd.children.length = 0;
|
|
890
|
+
mapping.clear();
|
|
891
|
+
} else {
|
|
892
|
+
$cd.children = [];
|
|
893
|
+
mapping.forEach(ctx => {
|
|
894
|
+
if(ctx.a) {
|
|
895
|
+
ctx.a = false;
|
|
896
|
+
$cd.children.push(ctx.cd);
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
$$removeElements(ctx.first, ctx.last);
|
|
900
|
+
ctx.cd.destroy(false);
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
let i, item, next_ctx, ctx, nextEl;
|
|
906
|
+
for(i=0;i<array.length;i++) {
|
|
907
|
+
item = array[i];
|
|
908
|
+
if(next_ctx) {
|
|
909
|
+
ctx = next_ctx;
|
|
910
|
+
next_ctx = null;
|
|
911
|
+
} else ctx = mapping.get(getKey(item, i, array));
|
|
912
|
+
if(ctx) {
|
|
913
|
+
nextEl = i == 0 && onlyChild ? parentNode[firstChild] : prevNode.nextSibling;
|
|
914
|
+
if(nextEl != ctx.first) {
|
|
915
|
+
let insert = true;
|
|
916
|
+
|
|
917
|
+
if(tplLength == 1 && (i + 1 < array.length) && prevNode?.nextSibling) {
|
|
918
|
+
next_ctx = mapping.get(getKey(array[i + 1], i + 1, array));
|
|
919
|
+
if(next_ctx && prevNode.nextSibling.nextSibling === next_ctx.first) {
|
|
920
|
+
parentNode.replaceChild(ctx.first, prevNode.nextSibling);
|
|
921
|
+
insert = false;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
if(insert) {
|
|
926
|
+
let insertBefore = prevNode?.nextSibling;
|
|
927
|
+
let next, el = ctx.first;
|
|
928
|
+
while(el) {
|
|
929
|
+
next = el.nextSibling;
|
|
930
|
+
parentNode.insertBefore(el, insertBefore);
|
|
931
|
+
if(el == ctx.last) break;
|
|
932
|
+
el = next;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
ctx.rebind(i, item);
|
|
937
|
+
} else {
|
|
938
|
+
let tpl = itemTemplate.cloneNode(true);
|
|
939
|
+
let childCD = $cd.new();
|
|
940
|
+
ctx = {cd: childCD};
|
|
941
|
+
bind(ctx, tpl, item, i);
|
|
942
|
+
ctx.first = tpl[firstChild];
|
|
943
|
+
ctx.last = tpl.lastChild;
|
|
944
|
+
parentNode.insertBefore(tpl, prevNode?.nextSibling);
|
|
945
|
+
}
|
|
946
|
+
prevNode = ctx.last;
|
|
947
|
+
newMapping.set(getKey(item, i, array), ctx);
|
|
948
|
+
} lastNode = prevNode;
|
|
949
|
+
mapping.clear();
|
|
950
|
+
mapping = newMapping;
|
|
951
|
+
}, {ro: true, cmp: $$compareArray});
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
export { $$addEventForComponent, $$awaitBlock, $$cloneDeep, $$compareArray, $$compareDeep, $$deepComparator, $$eachBlock, $$htmlBlock, $$htmlToFragment, $$htmlToFragmentClean, $$ifBlock, $$removeElements, $$removeItem, $ChangeDetector, $base, $context, $digest, $insertElementByOption, $makeEmitter, $onDestroy, $onMount, $readOnlyBase, $tick, $watch, $watchReadOnly, __app_onerror, __bindActionSubscribe, addClass, addEvent, addStyles, attachAnchor, attachSlot, attachSlotBase, attchExportedFragment, autoSubscribe, bindAction, bindAttribute, bindAttributeBase, bindClass, bindInput, bindStyle, bindText, callComponent, cd_onDestroy, childNodes, cloneDeep, configure, createTextNode, current_component, eachDefaultKey, exportFragment, fire, firstChild, getFinalLabel, insertAfter, isArray, isFunction, keyComparator, makeClassResolver, makeComponent, makeExternalProperty, makeSlot, makeSlotStatic, noop, observeProps, prefixPush, removeElementsBetween, setClassToElement, spreadAttributes, svgToFragment, unwrapProps };
|