native-document 1.0.31 → 1.0.33
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/native-document.dev.js +331 -128
- package/dist/native-document.dev.js.map +1 -1
- package/dist/native-document.min.js +1 -1
- package/elements.d.ts +6 -0
- package/index.d.ts +1 -5
- package/index.js +2 -1
- package/package.json +1 -1
- package/rollup.config.js +1 -0
- package/src/data/ObservableItem.js +32 -10
- package/src/data/observable-helpers/array.js +2 -1
- package/src/elements/anchor.js +20 -13
- package/src/elements/control/for-each-array.js +10 -19
- package/src/utils/args-types.js +98 -69
- package/src/utils/prototypes.js +17 -0
- package/src/wrappers/AttributesWrapper.js +4 -0
- package/src/wrappers/ElementCreator.js +16 -2
- package/src/wrappers/HtmlElementWrapper.js +13 -18
- package/src/wrappers/NDElement.js +6 -1
- package/src/wrappers/TemplateCloner.js +129 -0
- package/types/elements.d.ts +2 -0
- package/types/template-cloner.ts +36 -0
|
@@ -264,14 +264,24 @@ var NativeDocument = (function (exports) {
|
|
|
264
264
|
const $currentValue = this.$currentValue;
|
|
265
265
|
|
|
266
266
|
if($watchers.has($currentValue)) {
|
|
267
|
-
$watchers.get($currentValue)
|
|
268
|
-
|
|
269
|
-
|
|
267
|
+
const $currentValueCallbacks = $watchers.get($currentValue);
|
|
268
|
+
if(!Validator.isArray($currentValueCallbacks)) {
|
|
269
|
+
$currentValueCallbacks.set ? $currentValueCallbacks.set(true) : $currentValueCallbacks(true);
|
|
270
|
+
} else {
|
|
271
|
+
$currentValueCallbacks.forEach(callback => {
|
|
272
|
+
callback.set ? callback.set(true) : callback(true);
|
|
273
|
+
});
|
|
274
|
+
}
|
|
270
275
|
}
|
|
271
276
|
if($watchers.has($previousValue)) {
|
|
272
|
-
$watchers.get($previousValue)
|
|
273
|
-
|
|
274
|
-
|
|
277
|
+
const $previousValueCallbacks = $watchers.get($previousValue);
|
|
278
|
+
if(typeof $previousValueCallbacks === "function") {
|
|
279
|
+
$previousValueCallbacks.set ? $previousValueCallbacks.set(false) : $previousValueCallbacks(false);
|
|
280
|
+
} else {
|
|
281
|
+
$previousValueCallbacks.forEach(callback => {
|
|
282
|
+
callback.set ? callback.set(false) : callback(false);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
275
285
|
}
|
|
276
286
|
};
|
|
277
287
|
|
|
@@ -309,6 +319,7 @@ var NativeDocument = (function (exports) {
|
|
|
309
319
|
this.$currentValue = newValue;
|
|
310
320
|
PluginsManager.emit('ObservableBeforeChange', this);
|
|
311
321
|
this.trigger();
|
|
322
|
+
this.$previousValue = null;
|
|
312
323
|
PluginsManager.emit('ObservableAfterChange', this);
|
|
313
324
|
};
|
|
314
325
|
|
|
@@ -322,7 +333,9 @@ var NativeDocument = (function (exports) {
|
|
|
322
333
|
this.$currentValue = null;
|
|
323
334
|
if(this.$watchers) {
|
|
324
335
|
for (const [_, watchValueList] of this.$watchers) {
|
|
325
|
-
|
|
336
|
+
if(Validator.isArray(watchValueList)) {
|
|
337
|
+
watchValueList.splice(0);
|
|
338
|
+
}
|
|
326
339
|
}
|
|
327
340
|
}
|
|
328
341
|
this.$watchers?.clear();
|
|
@@ -368,17 +381,25 @@ var NativeDocument = (function (exports) {
|
|
|
368
381
|
this.$watchers = this.$watchers ?? new Map();
|
|
369
382
|
|
|
370
383
|
let watchValueList = this.$watchers.get(value);
|
|
384
|
+
|
|
371
385
|
if(!watchValueList) {
|
|
372
|
-
|
|
386
|
+
this.$watchers.set(value, callback);
|
|
387
|
+
} else if(!Validator.isArray(watchValueList)) {
|
|
388
|
+
watchValueList = [watchValueList];
|
|
373
389
|
this.$watchers.set(value, watchValueList);
|
|
390
|
+
return;
|
|
391
|
+
} else {
|
|
392
|
+
watchValueList.push(callback);
|
|
374
393
|
}
|
|
375
394
|
|
|
376
|
-
watchValueList.push(callback);
|
|
377
395
|
this.assocTrigger();
|
|
378
396
|
return () => {
|
|
379
397
|
const index = watchValueList.indexOf(callback);
|
|
380
398
|
watchValueList?.splice(index, 1);
|
|
381
|
-
if(watchValueList.size ===
|
|
399
|
+
if(watchValueList.size === 1) {
|
|
400
|
+
this.$watchers.set(value, watchValueList[0]);
|
|
401
|
+
}
|
|
402
|
+
else if(watchValueList.size === 0) {
|
|
382
403
|
this.$watchers?.delete(value);
|
|
383
404
|
watchValueList = null;
|
|
384
405
|
}
|
|
@@ -691,6 +712,11 @@ var NativeDocument = (function (exports) {
|
|
|
691
712
|
|
|
692
713
|
NDElement.prototype.node = NDElement.prototype.htmlElement;
|
|
693
714
|
|
|
715
|
+
NDElement.prototype.attach = function(methodName, bindingHydrator) {
|
|
716
|
+
bindingHydrator.$hydrate(this.$element, methodName);
|
|
717
|
+
return this.$element;
|
|
718
|
+
};
|
|
719
|
+
|
|
694
720
|
const Validator = {
|
|
695
721
|
isObservable(value) {
|
|
696
722
|
return value instanceof ObservableItem || value instanceof ObservableChecker || value?.__$isObservable;
|
|
@@ -809,7 +835,7 @@ var NativeDocument = (function (exports) {
|
|
|
809
835
|
}
|
|
810
836
|
};
|
|
811
837
|
|
|
812
|
-
function Anchor(name) {
|
|
838
|
+
function Anchor(name, isUniqueChild = false) {
|
|
813
839
|
const element = document.createDocumentFragment();
|
|
814
840
|
|
|
815
841
|
const anchorStart = document.createComment('Anchor Start : '+name);
|
|
@@ -822,14 +848,25 @@ var NativeDocument = (function (exports) {
|
|
|
822
848
|
element.nativeAppendChild = element.appendChild;
|
|
823
849
|
|
|
824
850
|
const insertBefore = function(parent, child, target) {
|
|
851
|
+
const element = Validator.isElement(child) ? child : ElementCreator.getChild(child);
|
|
825
852
|
if(parent === element) {
|
|
826
|
-
parent.nativeInsertBefore(
|
|
853
|
+
parent.nativeInsertBefore(element, target);
|
|
827
854
|
return;
|
|
828
855
|
}
|
|
829
|
-
|
|
856
|
+
if(isUniqueChild || target === anchorEnd) {
|
|
857
|
+
parent.append(element, target);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
parent.insertBefore(element, target);
|
|
830
861
|
};
|
|
831
862
|
|
|
832
863
|
element.appendElement = function(child, before = null) {
|
|
864
|
+
if(isUniqueChild) {
|
|
865
|
+
(before && before !== anchorEnd)
|
|
866
|
+
? anchorEnd.parentNode.insertBefore(child, anchorEnd)
|
|
867
|
+
: anchorEnd.parentNode.append(child, anchorEnd);
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
833
870
|
if(anchorEnd.parentNode === element) {
|
|
834
871
|
anchorEnd.parentNode.nativeInsertBefore(child, before || anchorEnd);
|
|
835
872
|
return;
|
|
@@ -844,14 +881,6 @@ var NativeDocument = (function (exports) {
|
|
|
844
881
|
return;
|
|
845
882
|
}
|
|
846
883
|
before = before ?? anchorEnd;
|
|
847
|
-
if(Validator.isArray(child)) {
|
|
848
|
-
const fragment = document.createDocumentFragment();
|
|
849
|
-
for(let i = 0, length = child.length; i < length; i++) {
|
|
850
|
-
fragment.appendChild(ElementCreator.getChild(child[i]));
|
|
851
|
-
}
|
|
852
|
-
insertBefore(parent, fragment, before);
|
|
853
|
-
return element;
|
|
854
|
-
}
|
|
855
884
|
insertBefore(parent, child, before);
|
|
856
885
|
};
|
|
857
886
|
|
|
@@ -860,7 +889,7 @@ var NativeDocument = (function (exports) {
|
|
|
860
889
|
if(parent === element) {
|
|
861
890
|
return;
|
|
862
891
|
}
|
|
863
|
-
if(parent.firstChild === anchorStart && parent.lastChild === anchorEnd) {
|
|
892
|
+
if(isUniqueChild || (parent.firstChild === anchorStart && parent.lastChild === anchorEnd)) {
|
|
864
893
|
parent.replaceChildren(anchorStart, anchorEnd);
|
|
865
894
|
return;
|
|
866
895
|
}
|
|
@@ -879,6 +908,10 @@ var NativeDocument = (function (exports) {
|
|
|
879
908
|
if(parent === element) {
|
|
880
909
|
return;
|
|
881
910
|
}
|
|
911
|
+
if(isUniqueChild) {
|
|
912
|
+
parent.replaceChildren(anchorEnd, anchorEnd);
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
882
915
|
let itemToRemove = anchorStart.nextSibling, tempItem;
|
|
883
916
|
while(itemToRemove !== anchorEnd) {
|
|
884
917
|
tempItem = itemToRemove.nextSibling;
|
|
@@ -898,7 +931,7 @@ var NativeDocument = (function (exports) {
|
|
|
898
931
|
if(!parent) {
|
|
899
932
|
return;
|
|
900
933
|
}
|
|
901
|
-
if(parent.firstChild === anchorStart && parent.lastChild === anchorEnd) {
|
|
934
|
+
if(isUniqueChild || (parent.firstChild === anchorStart && parent.lastChild === anchorEnd)) {
|
|
902
935
|
parent.replaceChildren(anchorStart, child, anchorEnd);
|
|
903
936
|
return;
|
|
904
937
|
}
|
|
@@ -995,6 +1028,10 @@ var NativeDocument = (function (exports) {
|
|
|
995
1028
|
});
|
|
996
1029
|
continue;
|
|
997
1030
|
}
|
|
1031
|
+
if(value.$hydrate) {
|
|
1032
|
+
value.$hydrate(element, className);
|
|
1033
|
+
continue;
|
|
1034
|
+
}
|
|
998
1035
|
element.classList.toggle(className, value);
|
|
999
1036
|
}
|
|
1000
1037
|
}
|
|
@@ -1142,11 +1179,22 @@ var NativeDocument = (function (exports) {
|
|
|
1142
1179
|
*/
|
|
1143
1180
|
createObservableNode(parent, observable) {
|
|
1144
1181
|
const text = ElementCreator.createTextNode();
|
|
1145
|
-
observable.subscribe(value => text.nodeValue =
|
|
1182
|
+
observable.subscribe(value => text.nodeValue = value);
|
|
1146
1183
|
text.nodeValue = observable.val();
|
|
1147
1184
|
parent && parent.appendChild(text);
|
|
1148
1185
|
return text;
|
|
1149
1186
|
},
|
|
1187
|
+
/**
|
|
1188
|
+
*
|
|
1189
|
+
* @param {HTMLElement|DocumentFragment} parent
|
|
1190
|
+
* @param {{$hydrate: Function}} item
|
|
1191
|
+
* @returns {Text}
|
|
1192
|
+
*/
|
|
1193
|
+
createHydratableNode(parent, item) {
|
|
1194
|
+
const text = ElementCreator.createTextNode();
|
|
1195
|
+
item.$hydrate(text);
|
|
1196
|
+
return text;
|
|
1197
|
+
},
|
|
1150
1198
|
|
|
1151
1199
|
/**
|
|
1152
1200
|
*
|
|
@@ -1156,7 +1204,7 @@ var NativeDocument = (function (exports) {
|
|
|
1156
1204
|
*/
|
|
1157
1205
|
createStaticTextNode(parent, value) {
|
|
1158
1206
|
let text = ElementCreator.createTextNode();
|
|
1159
|
-
text.nodeValue =
|
|
1207
|
+
text.nodeValue = value;
|
|
1160
1208
|
parent && parent.appendChild(text);
|
|
1161
1209
|
return text;
|
|
1162
1210
|
},
|
|
@@ -1225,6 +1273,9 @@ var NativeDocument = (function (exports) {
|
|
|
1225
1273
|
PluginsManager.emit('BeforeProcessComponent', child);
|
|
1226
1274
|
return this.getChild(child());
|
|
1227
1275
|
}
|
|
1276
|
+
if(child?.$hydrate) {
|
|
1277
|
+
return ElementCreator.createHydratableNode(null, child);
|
|
1278
|
+
}
|
|
1228
1279
|
return ElementCreator.createStaticTextNode(null, child);
|
|
1229
1280
|
},
|
|
1230
1281
|
/**
|
|
@@ -1270,6 +1321,9 @@ var NativeDocument = (function (exports) {
|
|
|
1270
1321
|
}
|
|
1271
1322
|
}
|
|
1272
1323
|
|
|
1324
|
+
exports.withValidation = (fn) => fn;
|
|
1325
|
+
exports.ArgTypes = {};
|
|
1326
|
+
|
|
1273
1327
|
/**
|
|
1274
1328
|
*
|
|
1275
1329
|
* @type {{string: (function(*): {name: *, type: string, validate: function(*): boolean}),
|
|
@@ -1287,88 +1341,93 @@ var NativeDocument = (function (exports) {
|
|
|
1287
1341
|
* validate: function(*): boolean})
|
|
1288
1342
|
* }}
|
|
1289
1343
|
*/
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
name,
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1344
|
+
{
|
|
1345
|
+
exports.ArgTypes = {
|
|
1346
|
+
string: (name) => ({ name, type: 'string', validate: (v) => Validator.isString(v) }),
|
|
1347
|
+
number: (name) => ({ name, type: 'number', validate: (v) => Validator.isNumber(v) }),
|
|
1348
|
+
boolean: (name) => ({ name, type: 'boolean', validate: (v) => Validator.isBoolean(v) }),
|
|
1349
|
+
observable: (name) => ({ name, type: 'observable', validate: (v) => Validator.isObservable(v) }),
|
|
1350
|
+
element: (name) => ({ name, type: 'element', validate: (v) => Validator.isElement(v) }),
|
|
1351
|
+
function: (name) => ({ name, type: 'function', validate: (v) => Validator.isFunction(v) }),
|
|
1352
|
+
object: (name) => ({ name, type: 'object', validate: (v) => (Validator.isObject(v)) }),
|
|
1353
|
+
objectNotNull: (name) => ({ name, type: 'object', validate: (v) => (Validator.isObject(v) && v !== null) }),
|
|
1354
|
+
children: (name) => ({ name, type: 'children', validate: (v) => Validator.validateChildren(v) }),
|
|
1355
|
+
attributes: (name) => ({ name, type: 'attributes', validate: (v) => Validator.validateAttributes(v) }),
|
|
1356
|
+
|
|
1357
|
+
// Optional arguments
|
|
1358
|
+
optional: (argType) => ({ ...argType, optional: true }),
|
|
1359
|
+
|
|
1360
|
+
// Union types
|
|
1361
|
+
oneOf: (name, ...argTypes) => ({
|
|
1362
|
+
name,
|
|
1363
|
+
type: 'oneOf',
|
|
1364
|
+
types: argTypes,
|
|
1365
|
+
validate: (v) => argTypes.some(type => type.validate(v))
|
|
1366
|
+
})
|
|
1367
|
+
};
|
|
1313
1368
|
|
|
1314
|
-
/**
|
|
1315
|
-
*
|
|
1316
|
-
* @param {Array} args
|
|
1317
|
-
* @param {Array} argSchema
|
|
1318
|
-
* @param {string} fnName
|
|
1319
|
-
*/
|
|
1320
|
-
const validateArgs = (args, argSchema, fnName = 'Function') => {
|
|
1321
|
-
if (!argSchema) return;
|
|
1322
1369
|
|
|
1323
|
-
|
|
1370
|
+
/**
|
|
1371
|
+
*
|
|
1372
|
+
* @param {Array} args
|
|
1373
|
+
* @param {Array} argSchema
|
|
1374
|
+
* @param {string} fnName
|
|
1375
|
+
*/
|
|
1376
|
+
const validateArgs = (args, argSchema, fnName = 'Function') => {
|
|
1377
|
+
if (!argSchema) return;
|
|
1378
|
+
|
|
1379
|
+
const errors = [];
|
|
1324
1380
|
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1381
|
+
// Check the number of arguments
|
|
1382
|
+
const requiredCount = argSchema.filter(arg => !arg.optional).length;
|
|
1383
|
+
if (args.length < requiredCount) {
|
|
1384
|
+
errors.push(`${fnName}: Expected at least ${requiredCount} arguments, got ${args.length}`);
|
|
1385
|
+
}
|
|
1330
1386
|
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1387
|
+
// Validate each argument
|
|
1388
|
+
argSchema.forEach((schema, index) => {
|
|
1389
|
+
const position = index + 1;
|
|
1390
|
+
const value = args[index];
|
|
1391
|
+
|
|
1392
|
+
if (value === undefined) {
|
|
1393
|
+
if (!schema.optional) {
|
|
1394
|
+
errors.push(`${fnName}: Missing required argument '${schema.name}' at position ${position}`);
|
|
1395
|
+
}
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1335
1398
|
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
errors.push(`${fnName}:
|
|
1399
|
+
if (!schema.validate(value)) {
|
|
1400
|
+
const valueTypeOf = value?.constructor?.name || typeof value;
|
|
1401
|
+
errors.push(`${fnName}: Invalid argument '${schema.name}' at position ${position}, expected ${schema.type}, got ${valueTypeOf}`);
|
|
1339
1402
|
}
|
|
1340
|
-
|
|
1341
|
-
}
|
|
1403
|
+
});
|
|
1342
1404
|
|
|
1343
|
-
if (
|
|
1344
|
-
|
|
1345
|
-
errors.push(`${fnName}: Invalid argument '${schema.name}' at position ${position}, expected ${schema.type}, got ${valueTypeOf}`);
|
|
1405
|
+
if (errors.length > 0) {
|
|
1406
|
+
throw new ArgTypesError(`Argument validation failed`, errors);
|
|
1346
1407
|
}
|
|
1347
|
-
}
|
|
1408
|
+
};
|
|
1348
1409
|
|
|
1349
|
-
if (errors.length > 0) {
|
|
1350
|
-
throw new ArgTypesError(`Argument validation failed`, errors);
|
|
1351
|
-
}
|
|
1352
|
-
};
|
|
1353
1410
|
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1411
|
+
|
|
1412
|
+
/**
|
|
1413
|
+
* @param {Function} fn
|
|
1414
|
+
* @param {Array} argSchema
|
|
1415
|
+
* @param {string} fnName
|
|
1416
|
+
* @returns {Function}
|
|
1417
|
+
*/
|
|
1418
|
+
exports.withValidation = (fn, argSchema, fnName = 'Function') => {
|
|
1419
|
+
if(!Validator.isArray(argSchema)) {
|
|
1420
|
+
throw new NativeDocumentError('withValidation : argSchema must be an array');
|
|
1421
|
+
}
|
|
1422
|
+
return function(...args) {
|
|
1423
|
+
validateArgs(args, argSchema, fn.name || fnName);
|
|
1424
|
+
return fn.apply(this, args);
|
|
1425
|
+
};
|
|
1367
1426
|
};
|
|
1368
|
-
}
|
|
1427
|
+
}
|
|
1369
1428
|
|
|
1370
1429
|
const normalizeComponentArgs = function(props, children = null) {
|
|
1371
|
-
if(!Validator.isJson(props)) {
|
|
1430
|
+
if(!Validator.isJson(props) || props?.$hydrate) {
|
|
1372
1431
|
const temp = children;
|
|
1373
1432
|
children = props;
|
|
1374
1433
|
props = temp;
|
|
@@ -1376,6 +1435,29 @@ var NativeDocument = (function (exports) {
|
|
|
1376
1435
|
return { props, children };
|
|
1377
1436
|
};
|
|
1378
1437
|
|
|
1438
|
+
/**
|
|
1439
|
+
*
|
|
1440
|
+
* @param {*} value
|
|
1441
|
+
* @returns {Text}
|
|
1442
|
+
*/
|
|
1443
|
+
const createTextNode = function(value) {
|
|
1444
|
+
return (Validator.isObservable(value))
|
|
1445
|
+
? ElementCreator.createObservableNode(null, value)
|
|
1446
|
+
: ElementCreator.createStaticTextNode(null, value);
|
|
1447
|
+
};
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
function createHtmlElement($tagName, _attributes, _children = null, customWrapper) {
|
|
1451
|
+
const { props: attributes, children = null } = normalizeComponentArgs(_attributes, _children);
|
|
1452
|
+
const element = ElementCreator.createElement($tagName);
|
|
1453
|
+
const finalElement = (typeof customWrapper === 'function') ? customWrapper(element) : element;
|
|
1454
|
+
|
|
1455
|
+
ElementCreator.processAttributes(finalElement, attributes);
|
|
1456
|
+
ElementCreator.processChildren(children, finalElement);
|
|
1457
|
+
|
|
1458
|
+
return ElementCreator.setup(finalElement, attributes, customWrapper);
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1379
1461
|
/**
|
|
1380
1462
|
*
|
|
1381
1463
|
* @param {string} name
|
|
@@ -1383,26 +1465,154 @@ var NativeDocument = (function (exports) {
|
|
|
1383
1465
|
* @returns {Function}
|
|
1384
1466
|
*/
|
|
1385
1467
|
function HtmlElementWrapper(name, customWrapper) {
|
|
1386
|
-
|
|
1468
|
+
return (_attributes, _children = null) => createHtmlElement(name.toLowerCase(), _attributes, _children, customWrapper);
|
|
1469
|
+
}
|
|
1387
1470
|
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1471
|
+
const cloneBindingsDataCache = new WeakMap();
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
const bindAttributes = (node, bindDingData, data) => {
|
|
1475
|
+
if(!bindDingData) {
|
|
1476
|
+
return null;
|
|
1477
|
+
}
|
|
1478
|
+
const attributes = { };
|
|
1479
|
+
if(bindDingData.attributes) {
|
|
1480
|
+
for (const attr in bindDingData.attributes) {
|
|
1481
|
+
attributes[attr] = bindDingData.attributes[attr](...data);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
if(bindDingData.classes) {
|
|
1486
|
+
attributes.class = {};
|
|
1487
|
+
for (const className in bindDingData.classes) {
|
|
1488
|
+
attributes.class[className] = bindDingData.classes[className](...data);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
if(bindDingData.styles) {
|
|
1493
|
+
attributes.style = {};
|
|
1494
|
+
for (const property in bindDingData.styles) {
|
|
1495
|
+
attributes.style[property] = bindDingData.styles[property](...data);
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
if(Object.keys(attributes)) {
|
|
1500
|
+
ElementCreator.processAttributes(node, attributes);
|
|
1501
|
+
return attributes;
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
return null;
|
|
1505
|
+
};
|
|
1506
|
+
|
|
1507
|
+
|
|
1508
|
+
const bindAttachesMethods = function(node, bindDingData, data) {
|
|
1509
|
+
if(!bindDingData?.attaches) {
|
|
1510
|
+
return null;
|
|
1511
|
+
}
|
|
1512
|
+
for(const methodName in bindDingData.attaches) {
|
|
1513
|
+
node.nd[methodName](function(...args) {
|
|
1514
|
+
bindDingData.attaches[methodName].call(this, ...[...args, ...data]);
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
};
|
|
1518
|
+
|
|
1519
|
+
function TemplateCloner($fn) {
|
|
1520
|
+
let $node = null;
|
|
1521
|
+
|
|
1522
|
+
const clone = (node, data) => {
|
|
1523
|
+
const bindDingData = cloneBindingsDataCache.get(node);
|
|
1524
|
+
if(node instanceof Text) {
|
|
1525
|
+
if(bindDingData?.value) {
|
|
1526
|
+
return bindDingData.value(data);
|
|
1527
|
+
}
|
|
1528
|
+
return node.cloneNode(true);
|
|
1529
|
+
}
|
|
1530
|
+
const nodeCloned = node.cloneNode();
|
|
1531
|
+
bindAttributes(nodeCloned, bindDingData, data);
|
|
1532
|
+
bindAttachesMethods(nodeCloned, bindDingData, data);
|
|
1533
|
+
|
|
1534
|
+
for(let i = 0, length = node.childNodes.length; i < length; i++) {
|
|
1535
|
+
const childNode = node.childNodes[i];
|
|
1536
|
+
const childNodeCloned = clone(childNode, data);
|
|
1537
|
+
nodeCloned.appendChild(childNodeCloned);
|
|
1538
|
+
}
|
|
1539
|
+
return nodeCloned;
|
|
1540
|
+
};
|
|
1393
1541
|
|
|
1394
|
-
|
|
1395
|
-
|
|
1542
|
+
this.clone = (data) => {
|
|
1543
|
+
if(!$node) {
|
|
1544
|
+
$node = $fn(this);
|
|
1545
|
+
}
|
|
1546
|
+
return clone($node, data);
|
|
1547
|
+
};
|
|
1548
|
+
|
|
1549
|
+
const createBinding = (hydrateFunction, target) => {
|
|
1550
|
+
return {
|
|
1551
|
+
$hydrate : function(element, property) {
|
|
1552
|
+
if(!cloneBindingsDataCache.has(element)) {
|
|
1553
|
+
// { classes, styles, attributes, value, attaches }
|
|
1554
|
+
cloneBindingsDataCache.set(element, {});
|
|
1555
|
+
}
|
|
1556
|
+
const hydrationState = cloneBindingsDataCache.get(element);
|
|
1557
|
+
if(target === 'value') {
|
|
1558
|
+
hydrationState.value = hydrateFunction;
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
hydrationState[target] = hydrationState[target] || {};
|
|
1562
|
+
hydrationState[target][property] = hydrateFunction;
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
};
|
|
1396
1566
|
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1567
|
+
this.style = (fn) => {
|
|
1568
|
+
return createBinding(fn, 'styles');
|
|
1569
|
+
};
|
|
1570
|
+
this.class = (fn) => {
|
|
1571
|
+
return createBinding(fn, 'classes');
|
|
1572
|
+
};
|
|
1573
|
+
this.value = (fn) => {
|
|
1574
|
+
return createBinding(function(data) {
|
|
1575
|
+
return createTextNode(fn(...data));
|
|
1576
|
+
}, 'value');
|
|
1577
|
+
};
|
|
1578
|
+
this.attr = (fn) => {
|
|
1579
|
+
return createBinding(fn, 'attributes');
|
|
1580
|
+
};
|
|
1581
|
+
this.attach = (fn) => {
|
|
1582
|
+
return createBinding(fn, 'attaches');
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
function useCache(fn) {
|
|
1587
|
+
let $cache = null;
|
|
1588
|
+
|
|
1589
|
+
return function(...args) {
|
|
1590
|
+
if(!$cache) {
|
|
1591
|
+
$cache = new TemplateCloner(fn);
|
|
1400
1592
|
}
|
|
1593
|
+
|
|
1594
|
+
return $cache.clone(args);
|
|
1401
1595
|
};
|
|
1402
1596
|
}
|
|
1403
1597
|
|
|
1404
1598
|
Function.prototype.args = function(...args) {
|
|
1405
|
-
return withValidation(this, args);
|
|
1599
|
+
return exports.withValidation(this, args);
|
|
1600
|
+
};
|
|
1601
|
+
|
|
1602
|
+
Function.prototype.cached = function(...args) {
|
|
1603
|
+
let $cache = null;
|
|
1604
|
+
let getCache = function(){ return $cache; };
|
|
1605
|
+
return () => {
|
|
1606
|
+
if(!$cache) {
|
|
1607
|
+
$cache = this.apply(this, args);
|
|
1608
|
+
if($cache.cloneNode) {
|
|
1609
|
+
getCache = function() { return $cache.cloneNode(true); };
|
|
1610
|
+
} else if($cache.$element) {
|
|
1611
|
+
getCache = function() { return new NDElement($cache.$element.cloneNode(true)); };
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
return getCache();
|
|
1615
|
+
};
|
|
1406
1616
|
};
|
|
1407
1617
|
|
|
1408
1618
|
Function.prototype.errorBoundary = function(callback) {
|
|
@@ -1514,7 +1724,8 @@ var NativeDocument = (function (exports) {
|
|
|
1514
1724
|
};
|
|
1515
1725
|
|
|
1516
1726
|
observer.merge = function(values) {
|
|
1517
|
-
observer.
|
|
1727
|
+
observer.$value.push(...values);
|
|
1728
|
+
observer.trigger({ action: 'merge', args: values });
|
|
1518
1729
|
};
|
|
1519
1730
|
|
|
1520
1731
|
observer.populateAndRender = function(iteration, callback) {
|
|
@@ -2009,20 +2220,15 @@ var NativeDocument = (function (exports) {
|
|
|
2009
2220
|
cache.delete(keyId);
|
|
2010
2221
|
}
|
|
2011
2222
|
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
return child;
|
|
2022
|
-
} catch (e) {
|
|
2023
|
-
DebugManager$1.error('ForEach', `Error creating element for key ${keyId}` , e);
|
|
2024
|
-
throw e;
|
|
2025
|
-
}
|
|
2223
|
+
const indexObserver = isIndexRequired ? Observable(indexKey) : null;
|
|
2224
|
+
let child = ElementCreator.getChild(callback(item, indexObserver));
|
|
2225
|
+
cache.set(keyId, {
|
|
2226
|
+
keyId,
|
|
2227
|
+
child: child,
|
|
2228
|
+
indexObserver: (indexObserver ? new WeakRef(indexObserver) : null)
|
|
2229
|
+
});
|
|
2230
|
+
keysCache.set(item, keyId);
|
|
2231
|
+
return child;
|
|
2026
2232
|
};
|
|
2027
2233
|
const getChildByKey = function(keyId) {
|
|
2028
2234
|
const cacheItem = cache.get(keyId);
|
|
@@ -2086,11 +2292,7 @@ var NativeDocument = (function (exports) {
|
|
|
2086
2292
|
element.appendElement(fragment, blockEnd);
|
|
2087
2293
|
},
|
|
2088
2294
|
removeOne(element, index) {
|
|
2089
|
-
|
|
2090
|
-
if(child) {
|
|
2091
|
-
removeCacheItemByKey(getItemKey(element, index), true);
|
|
2092
|
-
}
|
|
2093
|
-
child = null;
|
|
2295
|
+
removeCacheItemByKey(getItemKey(element, index), true);
|
|
2094
2296
|
},
|
|
2095
2297
|
clear,
|
|
2096
2298
|
merge(items) {
|
|
@@ -3376,18 +3578,19 @@ var NativeDocument = (function (exports) {
|
|
|
3376
3578
|
Router: Router
|
|
3377
3579
|
});
|
|
3378
3580
|
|
|
3379
|
-
exports.ArgTypes = ArgTypes;
|
|
3380
3581
|
exports.ElementCreator = ElementCreator;
|
|
3381
3582
|
exports.HtmlElementWrapper = HtmlElementWrapper;
|
|
3382
3583
|
exports.NDElement = NDElement;
|
|
3383
3584
|
exports.Observable = Observable;
|
|
3384
3585
|
exports.Store = Store;
|
|
3586
|
+
exports.TemplateCloner = TemplateCloner;
|
|
3385
3587
|
exports.classPropertyAccumulator = classPropertyAccumulator;
|
|
3588
|
+
exports.createTextNode = createTextNode;
|
|
3386
3589
|
exports.cssPropertyAccumulator = cssPropertyAccumulator;
|
|
3387
3590
|
exports.elements = elements;
|
|
3388
3591
|
exports.normalizeComponentArgs = normalizeComponentArgs;
|
|
3389
3592
|
exports.router = router;
|
|
3390
|
-
exports.
|
|
3593
|
+
exports.useCache = useCache;
|
|
3391
3594
|
|
|
3392
3595
|
return exports;
|
|
3393
3596
|
|