-
Notifications
You must be signed in to change notification settings - Fork 5
/
inter.js
3450 lines (2760 loc) · 92.8 KB
/
inter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Interjs
* Version - 2.2.4
* MIT LICENSED BY - Denis Power
* Repo - https://github.com/interjs/inter
* 2021 - 2024
* GENERATED BY INTER BUILDER
*
*/
(function () {
function runInvalidTemplateArgumentError(arg) {
syErr(`The argument of the template function must be a plain Javascript object,
but you defined "${valueType(arg)}" as its argument.
`);
}
function runInvalidEventHandlerWarning(eventName) {
ParserWarning(`The "${eventName}" event was not created because
its handler is not a function, in the tempate function.`);
}
function runIllegalTextWarning() {
ParserWarning(`The template parser found an element
with both the text property and the children property,
and in this case Inter ignores the text property.`);
}
function runInvalidEventWarning(eventName) {
ParserWarning(`"${eventName}" doesn't seem to be a valid dom event.`);
}
function runInvalidStyleWarning(styleName) {
ParserWarning(`"${styleName}" doesn't seem to be a valid style name.`);
}
function runCanNotRenderConditionallyWarning() {
ParserWarning(`You can not conditionally render the main
container in the template function.`);
}
function runInvalidTagOptionError(tag) {
syErr(`"${valueType(tag)}" is an invalid tag name,
in the template function.`);
}
function runInvalidObjectOptionsError() {
syErr(`The "events", "attrs" and "styles" options in the template function
must be plain Javascript objects, and you didn't define
one or more of those options as plain Javascript object.`);
}
function runIllegalAttrsPropWarning(prop) {
const styleProp = `You should not use the style attribute(in attrs object) to create styles for the element,
use the "styles" object instead, like:
{
tag: "p", text: "Some text", styles: { color: "green" }
}
`;
const event = `You shoud not use "${prop}" as an attribute name, it seems to be a dom event,
use it as property of the "events" object, like:
{
tag: "button", text: "Some text", events: { ${prop}: () => { //Some code here } }
}
`;
consW(prop.startsWith("on") ? event : styleProp);
}
function runInvalidStyleValue(name, value) {
ParserWarning(`"${value}" is an invalid value for the "${name}" style.`);
}
function runCanNotDefineReactivePropWarning() {
consW(`Inter failed to define reactivity
in a plain Javascript object, because it is not configurable.`);
}
function runDetecteReservedPropdWarnig(prop) {
consW(
`"${prop}" is a reserved property, do not create a property with this name.`
);
}
function runInvalidDefinePropsValueError(props) {
syErr(`The value of "defineProps" must be a plain Javascript object, and you
defined "${valueType(props)}" as its value`);
}
function runInvalidSetPropsValueError(props) {
syErr(`The value of "setProps" must be a plain Javascript object, and you
defined "${valueType(props)}" as its value`);
}
function runInvalidDeletePropsValueError(props) {
syErr(`The value of "deleteProps" must be an Array object, and you
defined "${valueType(props)}" as its value`);
}
function runNotConfigurableArrayError() {
err(`Inter failed to define the reactivity,
because the Array of the each option is not configurable.`);
}
function runUnsupportedEachValueError(value) {
syErr(`"${valueType(
value
)}" is not a valid value for the "each" option in renderList.
The values that are accepted in "each" option, are:
Array.
Plain js object.
Number.
Map.
Set.`);
}
function runInvalidAddItemsSecodArgumentError() {
syErr("The second argument of [LIST REACTOR].addItems must be a number.");
}
function runInvalidAddItemsFirstArgumentError() {
syErr("The first argument of [LIST REACTOR ].addItems must be an Array.");
}
function runInvalidTemplateReturnError() {
syErr(`The template function is not being returned inside the "do" method in
renderList(reactive listing), just return the template function.`);
}
function runIvalidRequestArgumentError(arg) {
syErr(`The argument of [Backend instance].request method
must be a plain javascript object, and you defined "${valueType(arg)}"
as its argument.`);
}
function runInvalidTypeOptionError() {
syErr(`You must define the type(method) of request, in Ajax with the "type" option and
it must be a string.`);
}
function runInvalidPathOptionError() {
syErr(`You must define the path where the request will be sent, with the "path" option and
it must be a string.`);
}
function runUnsupportedRequestTypeWarning(type) {
err(`"${type}" is an unsupported request type in Ajax.`);
}
function runInvalidAjaxEventWarning(name) {
consW(`There's not any event named "${name}" in Ajax request.`);
}
function runInvalidSecurityObjectWarning() {
consW(`Invalid "security" object, security object must have the username and passoword
properties.`);
}
function runInvalidCallBackError() {
syErr(`The arguments of "okay", "error" and "response" methods must be
functions.`);
}
function runInvalidResponseArgumentNumberError(argNumber) {
syErr(`The response method must have two arguments and you only
defined ${argNumber} argument.`);
}
function runInvalidHeadersOptionError(headers) {
syErr(`the "headers" property must be an object, and
you defined it as : ${valueType(headers)}.`);
}
function runInvalidAjaxEventsOptionError(events) {
syErr(`the "events" property must be an object, and
you defined it as : ${valueType(events)}.`);
}
function runReservedRefNameWarning(refName) {
consW(`"${refName}" is a reserved reference name, use other name.`);
}
function runInvalidSetRefsValueError(arg) {
syErr(`"${valueType(arg)}" is not a valid value for the "setRefs" property.
The value of the setRefs property must be a plain Javascript object.`);
}
function runInvalidRefArgument() {
syErr(
"The argument of the Ref function must be a plain Javascript object."
);
}
function runInvalidRefInProperty() {
syErr(
"The value of the 'in' property on the Ref function must be a string."
);
}
function runInvalidRefDataProperty() {
syErr(
"The value of the 'data' property on the Ref function must be a plain Javascript object. "
);
}
function runInvalidRenderIfInOptionError() {
syErr(`The value of the "in" property in the renderIf function
must be a string.`);
}
function runInvalidRenderIfDataOptionError() {
syErr(`The value of the "data" property in the renderIf function
must be a plain Javascript object.`);
}
function runNotDefinedConditionalPropWarning(prop) {
consW(`"${prop}" was not defined as a conditional property.`);
}
function runAlreadyUsedPropInConditionalGroupError(prop) {
err(`
Two elements in the conditional group can not have the same conditional property.
Property: "${prop}"
`);
}
function runInvalidRenderIfObserveArgumentError() {
syErr(`The argument of [renderIf reactor].observe()
must be a function.`);
}
function runInvalidRenderIfArgError() {
syErr("The argument of renderIf must be a plain Javascript object.");
}
function runInvalidConditionalPropValueError(prop) {
err(`The value of a conditional property must be boolean(true/false),
and the value of "${prop}" property is not boolean.`);
}
function runHasMoreThanOneCondtionalAttributeError(child) {
ParserWarning(`The conditional rendering parser found a/an "${getTagName(
child
)}"
element which has more than one conditional atribute, it's forbidden.`);
}
function runNotDefinedIfNotPropWarning(child, _ifNot /*propValue*/, data) {
if (_ifNot.trim().length == 0) {
runInvalidConditionalAttrs("_ifNot");
return;
}
ParserWarning(`
The conditional rendering parser found
an element with the "_ifNot" attribute and the value
of this attribute is not a conditional property.
{
element: ${child.nodeName.toLowerCase()},
_ifNot: ${_ifNot},
data: { ${Object.keys(data)} }
}
`);
}
function runInvalidConditionalAttrs(attrName) {
ParserWarning(`The conditional rendering parser found an ${attrName} attribute that does not
have a value assigned to it. Assign a value to the ${attrName} attribute.
`);
}
function runNotDefinedElseIfPropWarning(propValue) {
if (propValue.trim().length == 0) {
runInvalidConditionalAttrs("_elseIf");
return;
}
ParserWarning(`The conditional rendering parser found an element which has the "_elseIf"
conditional property whose the value is: "${propValue}",
but you did not define any conditional property with that name.
`);
}
function runInvalidElseAttributeError() {
ParserWarning(`The parser found an element with the "_else" attribute,
but there is not an element with the "_if" or a valid "_elseIf" attribute before it.`);
}
function runInvalidElseIfAttributeError(child) {
ParserWarning(`a/an "${getTagName(
child
)}" element has the "_elseIf" attribute,
but it does not come after an element with the "_if" or a valid "_elseIf" attribute.`);
}
function runInvalidSetCondsValueError(arg) {
syErr(`The value of [renderIf reactor].setConds must be
a plain Javascript object, and you defined ${valueType(arg)}
as its value.`);
}
function runNotDefinedIfPropWarning(propValue, child, data) {
if (propValue.trim().length == 0) {
runInvalidConditionalAttrs("_if");
return;
}
ParserWarning(`
The conditional rendering parser found
an element which has the "_if" attribute and the value
of this attribute is not a conditional property.
{
element: ${child.nodeName.toLowerCase()},
_if: ${propValue},
data: { ${Object.keys(data)} }
}
`);
}
function runNotDefinedManagerError(name) {
ParserWarning(`
The attribute manager parser found an attribute manager
named "${name}", but you did not define it in the "data" object.
`);
}
function runInvalidEventHandlerError(name, handler) {
syErr(`
"${valueType(handler)}" is an invalid
handler for the "${name}" event, you must
define only a function as the handler of a dom event.
`);
}
function runCanNotGetTheValueOfAnEventWarning(name) {
consW(`
you are trying to get the value of "${name}",
it's an event, and you can not get the value of an event.
`);
}
function runInvalidSetAttrsValueError(props) {
syErr(`
"${valueType(props)}" is an invalid value for the "setAttrs" property.
The "setAttrs" property only accepts a plain Javascript object as its
value.
`);
}
function runUnexpectedPropWarning(prop) {
consW(`
The "${prop}" property was not defined in the manager object.
`);
}
function runNotCallebleError(arg) {
syErr(`The argument of the observe method must be a function,
and you defined ${valueType(arg)} as its argument.`);
}
// Helpers functions.
function isValidTemplateReturn(arg) {
return isObj(arg) && arg.element && arg[Symbol.for("template")];
}
function isNotConfigurable(obj) {
return (
Object.isFrozen(obj) || Object.isSealed(obj) || !Object.isExtensible(obj)
);
}
function isObj(arg) {
// For plain objects.
return Object.prototype.toString.apply(arg, void 0) == "[object Object]";
}
function getTagName(elementNode) {
return elementNode.nodeName.toLowerCase();
}
function isSet(arg) {
return arg instanceof Set;
}
function hasOwnProperty(target, prop) {
const hasOwn = Object.prototype.hasOwnProperty.call(target, prop);
return hasOwn;
}
function isMap(arg) {
return arg instanceof Map;
}
function isDefined(arg) {
return arg != void 0;
}
/**
* Indirect boolean value checking can cause
* unexpected result, that's why I am using direct
* checking here.
*
*/
function isTrue(v) {
return Object.is(v, true);
}
function isFalse(v) {
return Object.is(v, false);
}
/*</>*/
function isCallable(fn) {
return typeof fn == "function";
}
function isEmptyObj(obj) {
return Object.keys(obj).length == 0;
}
function isAtag(tag) {
return tag instanceof HTMLElement;
}
function validDomEvent(eventName) {
return eventName in HTMLElement.prototype;
}
function validStyleName(styeName) {
return styeName in document.createElement("p").style;
}
function createText(text) {
return document.createTextNode(text);
}
function validTagOption(option) {
return typeof option == "string";
}
function validObjectOptions(option1, option2, option3) {
//For the styles, attrs and events options
return isObj(option1) && isObj(option2) && isObj(option3);
}
function getId(id) {
if (typeof id !== "string")
syErr("The value of the id attribute must be a string.");
const el = document.getElementById(id);
if (el == void 0)
err(`There's not an element on the document with id "${id}".`);
else return el;
}
function valueType(val) {
if (
typeof val == "undefined" ||
typeof val == "symbol" ||
typeof val == "bigint" ||
typeof val == "boolean" ||
typeof val == "function" ||
typeof val == "number" ||
typeof val == "string"
) {
return typeof val;
} else {
/**
*
* @val may be an array, a plain object or even
* a native Javascript object,
* let's check with the type() function.
*
*/
return type(val);
}
}
// WARNINGS HELPERS
function syErr(err) {
throw new Error(`Inter syntaxError : ${err}`);
}
function err(e) {
throw new Error(`Inter error: ${e}`);
}
function consW(w) {
console.warn(`Inter warning: ${w}`);
}
function ParserWarning(w) {
console.error(`Inter parser error: ${w}`);
}
//
function isArray(arg) {
return Array.isArray(arg);
}
function type(val) {
// All Javascript objects.
const isAnobject =
isDefined(val) &&
Object.prototype.toString.call(val).startsWith("[object");
if (isAnobject) {
return Object.prototype.toString
.call(val)
.replace("[object", "")
.replace("]", "")
.replace(/\s/g, "")
.toLowerCase();
} else {
/**
* @val is null.
*
*/
return "null";
}
}
function isBool(val) {
/**
*
* Don't use typeof val==="boolean"; due to 1 and 0.
*
*/
return val == true || val == false;
}
//Just for renderList.
function validInProperty(IN) {
return typeof IN == "string";
}
function validEachProperty(each) {
return (
each instanceof Array ||
isObj(each) ||
each instanceof Map ||
each instanceof Set ||
typeof each === "number"
);
}
function toIterable(data) {
const iterable = {
values: new Array(),
type: void 0,
};
if (isArray(data)) {
iterable.values = data;
iterable.type = "array";
} else if (isObj(data)) {
iterable.values = Object.entries(data);
iterable.type = "object";
} else if (data instanceof Map) {
data.forEach((value, key) => {
iterable.values.push([key, value]);
});
iterable.type = "object";
} else if (data instanceof Set) {
iterable.values = Array.from(data);
iterable.type = "set";
} else if (typeof data === "number") {
for (let i = 0; i < data; i++) {
iterable.values.push(i);
}
iterable.type = "number";
}
return iterable;
}
function Iterable(data) {
this.source = toIterable(data);
this.break = !1;
}
Iterable.prototype.each = function (callBack) {
let index = -1;
for (const data of this.source.values) {
index++;
callBack(data, index, this.source.type);
if (this.break) break;
}
};
function isNegativeValue(value) {
value = typeof value == "string" ? value.trim() : value;
const nevagativeValues = new Set([0, false, null, undefined, ""]);
return nevagativeValues.has(value);
}
function isPositiveValue(value) {
return !isNegativeValue(value);
}
function isTringOrNumber(value) {
typeof value == "string" || typeof value == "number";
}
//</>
function hasProp(object) {
return Object.keys(object).length > 0;
}
function hasRefs(text) {
return /{\s*.*\s*}/.test(text);
}
function getRefs(text) {
/**
*
* @text must be a string containing refs.
*
* This function is used in reference computation,
* it helps Inter making an eficient reference computation.
*
*/
const ref = /{\s*(:?[\w-\s]+)\s*}/g;
const refs = new Set();
text.replace(ref, (plainRef) => {
const refName = plainRef.replace("{", "").replace("}", "").trim();
refs.add(refName);
});
return Array.from(refs);
}
function hasRefNamed(text, refName) {
const pattern = new RegExp(`{\\s*${refName}\\s*}`);
return pattern.test(text);
}
/**
*
* We are considering them as special attributes
* because we must not use the setAttribute method
* to set them.
*
*/
const specialAttrs = new Set(["currentTime", "value"]);
function runRefParsing(rootElement, refs, refCache) {
function getTextNodes(el) {
const _childNodes = new Set();
if (el.hasChildNodes())
for (const child of el.childNodes) {
if (
child.nodeType == 3 &&
child.textContent.trim().length > 0 &&
hasRefs(child.textContent)
) {
_childNodes.add(child);
}
}
return Array.from(_childNodes);
}
const children = rootElement.getElementsByTagName("*");
function runTextRefParsing(parentNode) {
function parseRefsInText(node) {
for (const ref in refs) {
if (
node.textContent.trim().length > 0 &&
hasRefNamed(node.textContent, ref)
) {
const setting = {
target: node,
text: node.textContent,
};
refCache.add(setting);
break;
}
}
}
if (parentNode.nodeType == 1) {
for (const node of parentNode.childNodes) {
if (node.hasChildNodes() && node.nodeType == 1) {
runTextRefParsing(node);
continue;
}
parseRefsInText(node);
}
} else if (parentNode.nodeType == 3) {
// Parsing the references
// in the main container
// text nodes.
parseRefsInText(parentNode);
}
}
function parseRefsInAttrs(elementNode) {
const setting = {
target: elementNode,
attrs: Object.create(null),
refs: refs,
};
for (const attr of elementNode.attributes) {
for (const ref in refs) {
if (hasRefNamed(attr.value, ref)) {
if (!specialAttrs.has(attr.name)) {
setting.attrs[attr.name] = attr.value;
} else {
refCache.specialAttrs.add({
target: elementNode,
attr: {
[attr.name]: attr.value,
},
});
elementNode.removeAttribute(attr.name);
}
break;
}
}
}
if (hasProp(setting.attrs)) {
// The true argument says to the parser
// to register the reference as an attribute reference.
refCache.add(setting, true);
}
}
const textNodes = getTextNodes(rootElement);
if (textNodes.length > 0) {
for (const text of textNodes) {
runTextRefParsing(text);
}
}
for (const child of children) {
runTextRefParsing(child);
parseRefsInAttrs(child);
}
refCache.updateRefs();
}
function Ref(obj) {
if (new.target != void 0) {
syErr("Do not call the Ref function with the new keyword.");
} else if (!isObj(obj)) runInvalidRefArgument();
else {
const { in: IN, data } = obj;
if (!(typeof IN === "string")) runInvalidRefInProperty();
if (!isObj(data)) runInvalidRefDataProperty();
const reservedRefNames = new Set(["setRefs", "observe"]);
for (const refName in data) {
if (reservedRefNames.has(refName)) {
runReservedRefNameWarning(refName);
delete data[refName];
continue;
}
if (isCallable(data[refName])) {
data[refName] = data[refName].call(data);
}
}
const proxyTarget = Object.assign({}, data);
const refParser = {
attrs: new Set(), // Attribute reference.
texts: new Set(), // Text reference.
specialAttrs: new Set(),
observed: new Map(),
refs: proxyTarget,
hadIteratedOverSpecialAttrs: false,
add(setting, attr) {
// if attr, the parser must register the reference
// as an attribute reference.
if (attr) {
this.attrs.add(setting);
} else {
this.texts.add(setting);
}
},
updateSpecialAttrs() {
for (const special of this.specialAttrs) {
const { target } = special;
// eslint-disable-next-line prefer-const
let [attrName, attrValue] = Object.entries(special.attr)[0];
const refs = getRefs(attrValue);
for (const ref of refs) {
if (reservedRefNames.has(ref)) continue;
if (ref in this.refs) {
const pattern = new RegExp(`{\\s*(:?${ref})\\s*}`, "g");
attrValue = attrValue.replace(pattern, this.refs[ref]);
if (!hasRefs(attrValue)) break;
}
}
target[attrName] = attrValue;
}
},
updateAttrRef() {
for (const attributeRef of this.attrs) {
const { target, attrs } = attributeRef;
// eslint-disable-next-line prefer-const
for (let [name, value] of Object.entries(attrs)) {
const refNames = getRefs(value);
for (const refName of refNames) {
if (reservedRefNames.has(refName)) continue;
if (refName in this.refs) {
const pattern = new RegExp(`{\\s*(:?${refName})\\s*}`, "g");
value = value.replace(pattern, this.refs[refName]);
if (!hasRefs(value)) break;
}
}
if (target.getAttribute(name) !== value) {
target.setAttribute(name, value);
}
}
}
},
updateTextRef() {
if (this.texts.size > 0) {
for (const textRef of this.texts) {
// eslint-disable-next-line prefer-const
let { target, text } = textRef;
// Returns the ref Names
// on the "text" string.
const refNames = getRefs(text);
for (const refName of refNames) {
if (reservedRefNames.has(refName)) continue;
if (refName in this.refs) {
const pattern = new RegExp(`{\\s*(:?${refName})\\s*}`, "g");
text = text.replace(pattern, this.refs[refName]);
if (!hasRefs(text)) break;
}
}
if (target.textContent !== text) {
target.textContent = text;
}
}
}
},
updateRefs() {
if (this.texts.size > 0) this.updateTextRef();
if (this.attrs.size > 0) this.updateAttrRef();
if (this.specialAttrs.size > 0) this.updateSpecialAttrs();
},
};
runRefParsing(getId(IN), proxyTarget, refParser);
function runObserveCallBack(refName, value, oldValue) {
if (refParser.observed.size == 1 && !reservedRefNames.has(refName)) {
const callBack = refParser.observed.get("callBack");
callBack(refName, value, oldValue);
}
}
const reactor = new Proxy(proxyTarget, {
set(target, key, value, proxy) {
if (key in target && target[key] == value) return false;
const oldValue = target[key];
if (isCallable(value)) {
value = value.call(proxy);
}
Reflect.set(...arguments);
runObserveCallBack(key, value, oldValue);
if (!(key in proxy)) {
// Dynamic ref.
runRefParsing(getId(IN), proxyTarget, refParser);
} else {
refParser.updateRefs();
return true;
}
},
get(...args) {
return Reflect.get(...args);
},
});
Object.defineProperties(reactor, {
setRefs: {
set(o) {
if (isObj(o)) {
let hasNewRefName = false;
for (const [refName, refValue] of Object.entries(o)) {
if (reservedRefNames.has(refName)) {
runReservedRefNameWarning(refName);
continue;
}
if (!hasOwnProperty(this, refName)) hasNewRefName = true;
if (hasOwnProperty(this, refName) && this[refName] == refValue)
continue;
const oldRefValue = proxyTarget[refName];
if (isCallable(refValue)) {
proxyTarget[refName] = refValue.call(this);
} else {
proxyTarget[refName] = refValue;
}
runObserveCallBack(refName, refValue, oldRefValue);
}
if (hasNewRefName)
runRefParsing(getId(IN), proxyTarget, refParser);
} else runInvalidSetRefsValueError(o);
},
enumerable: !1,
},
observe: {
value(callBack) {
if (!isCallable(callBack)) {
syErr(
"The argument of [Reference reactor].observe() must be a function."
);
}
if (refParser.observed.size === 0) {
refParser.observed.set("callBack", callBack);
return true;
}
return false;
},