native-document 1.0.79 → 1.0.81

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.
@@ -1,10 +1,10 @@
1
1
  var NativeDocument = (function (exports) {
2
2
  'use strict';
3
3
 
4
- let DebugManager$1 = {};
4
+ let DebugManager = {};
5
5
 
6
6
  {
7
- DebugManager$1 = {
7
+ DebugManager = {
8
8
  enabled: false,
9
9
 
10
10
  enable() {
@@ -35,7 +35,7 @@ var NativeDocument = (function (exports) {
35
35
  };
36
36
 
37
37
  }
38
- var DebugManager = DebugManager$1;
38
+ var DebugManager$1 = DebugManager;
39
39
 
40
40
  const MemoryManager = (function() {
41
41
 
@@ -84,7 +84,7 @@ var NativeDocument = (function (exports) {
84
84
  }
85
85
  }
86
86
  if (cleanedCount > 0) {
87
- DebugManager.log('Memory Auto Clean', `🧹 Cleaned ${cleanedCount} orphaned observables`);
87
+ DebugManager$1.log('Memory Auto Clean', `🧹 Cleaned ${cleanedCount} orphaned observables`);
88
88
  }
89
89
  }
90
90
  };
@@ -207,7 +207,7 @@ var NativeDocument = (function (exports) {
207
207
  try{
208
208
  callback.call(plugin, ...data);
209
209
  } catch (error) {
210
- DebugManager.error('Plugin Manager', `Error in plugin ${plugin.$name} for event ${eventName}`, error);
210
+ DebugManager$1.error('Plugin Manager', `Error in plugin ${plugin.$name} for event ${eventName}`, error);
211
211
  }
212
212
  }
213
213
  }
@@ -322,6 +322,7 @@ var NativeDocument = (function (exports) {
322
322
  this.$currentValue = value;
323
323
  this.$isCleanedUp = false;
324
324
 
325
+ this.$firstListener = null;
325
326
  this.$listeners = null;
326
327
  this.$watchers = null;
327
328
 
@@ -357,7 +358,7 @@ var NativeDocument = (function (exports) {
357
358
  };
358
359
 
359
360
  ObservableItem.prototype.triggerFirstListener = function(operations) {
360
- this.$listeners[0](this.$currentValue, this.$previousValue, operations || {});
361
+ this.$firstListener(this.$currentValue, this.$previousValue, operations || {});
361
362
  };
362
363
 
363
364
  ObservableItem.prototype.triggerListeners = function(operations) {
@@ -406,22 +407,29 @@ var NativeDocument = (function (exports) {
406
407
  };
407
408
 
408
409
  ObservableItem.prototype.triggerAll = function(operations) {
409
- this.triggerListeners(operations);
410
410
  this.triggerWatchers();
411
+ this.triggerListeners(operations);
411
412
  };
412
413
 
413
414
  ObservableItem.prototype.triggerWatchersAndFirstListener = function(operations) {
414
- this.triggerListeners(operations);
415
415
  this.triggerWatchers();
416
+ this.triggerListeners(operations);
416
417
  };
417
418
 
418
419
  ObservableItem.prototype.assocTrigger = function() {
420
+ this.$firstListener = null;
419
421
  if(this.$watchers?.size && this.$listeners?.length) {
420
422
  this.trigger = (this.$listeners.length === 1) ? this.triggerWatchersAndFirstListener : this.triggerAll;
421
423
  return;
422
424
  }
423
425
  if(this.$listeners?.length) {
424
- this.trigger = (this.$listeners.length === 1) ? this.triggerFirstListener : this.triggerListeners;
426
+ if(this.$listeners.length === 1) {
427
+ this.$firstListener = this.$listeners[0];
428
+ this.trigger = this.triggerFirstListener;
429
+ }
430
+ else {
431
+ this.trigger = this.triggerListeners;
432
+ }
425
433
  return;
426
434
  }
427
435
  if(this.$watchers?.size) {
@@ -506,7 +514,7 @@ var NativeDocument = (function (exports) {
506
514
  ObservableItem.prototype.subscribe = function(callback, target = null) {
507
515
  this.$listeners = this.$listeners ?? [];
508
516
  if (this.$isCleanedUp) {
509
- DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
517
+ DebugManager$1.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
510
518
  return () => {};
511
519
  }
512
520
  if (typeof callback !== 'function') {
@@ -859,7 +867,7 @@ var NativeDocument = (function (exports) {
859
867
  }
860
868
  {
861
869
  if (this[name] && !this.$localExtensions.has(name)) {
862
- DebugManager.warn('NDElement.extend', `Method "${name}" already exists and will be overwritten`);
870
+ DebugManager$1.warn('NDElement.extend', `Method "${name}" already exists and will be overwritten`);
863
871
  }
864
872
  this.$localExtensions.set(name, method);
865
873
  }
@@ -893,17 +901,17 @@ var NativeDocument = (function (exports) {
893
901
  const method = methods[name];
894
902
 
895
903
  if (typeof method !== 'function') {
896
- DebugManager.warn('NDElement.extend', `"${name}" is not a function, skipping`);
904
+ DebugManager$1.warn('NDElement.extend', `"${name}" is not a function, skipping`);
897
905
  continue;
898
906
  }
899
907
 
900
908
  if (protectedMethods.has(name)) {
901
- DebugManager.error('NDElement.extend', `Cannot override protected method "${name}"`);
909
+ DebugManager$1.error('NDElement.extend', `Cannot override protected method "${name}"`);
902
910
  throw new NativeDocumentError(`Cannot override protected method "${name}"`);
903
911
  }
904
912
 
905
913
  if (NDElement.prototype[name]) {
906
- DebugManager.warn('NDElement.extend', `Overwriting existing prototype method "${name}"`);
914
+ DebugManager$1.warn('NDElement.extend', `Overwriting existing prototype method "${name}"`);
907
915
  }
908
916
 
909
917
  NDElement.prototype[name] = method;
@@ -1062,7 +1070,7 @@ var NativeDocument = (function (exports) {
1062
1070
  const foundReserved = Object.keys(attributes).filter(key => reserved.includes(key));
1063
1071
 
1064
1072
  if (foundReserved.length > 0) {
1065
- DebugManager.warn('Validator', `Reserved attributes found: ${foundReserved.join(', ')}`);
1073
+ DebugManager$1.warn('Validator', `Reserved attributes found: ${foundReserved.join(', ')}`);
1066
1074
  }
1067
1075
 
1068
1076
  return attributes;
@@ -1110,7 +1118,7 @@ var NativeDocument = (function (exports) {
1110
1118
  anchorFragment.appendChild = function(child, before = null) {
1111
1119
  const parent = anchorEnd.parentNode;
1112
1120
  if(!parent) {
1113
- DebugManager.error('Anchor', 'Anchor : parent not found', child);
1121
+ DebugManager$1.error('Anchor', 'Anchor : parent not found', child);
1114
1122
  return;
1115
1123
  }
1116
1124
  before = before ?? anchorEnd;
@@ -1368,8 +1376,18 @@ var NativeDocument = (function (exports) {
1368
1376
  function bindClassAttribute(element, data) {
1369
1377
  for(let className in data) {
1370
1378
  const value = data[className];
1371
- if(value?.bindNdClass) {
1372
- value.bindNdClass(element, className);
1379
+ if(Validator.isObservable(value)) {
1380
+ element.classes.toggle(className, value.val());
1381
+ value.subscribe(toggleElementClass.bind(null, element, className));
1382
+ continue;
1383
+ }
1384
+ if(Validator.isObservableWhenResult(value)) {
1385
+ element.classes.toggle(className, value.isMath());
1386
+ value.subscribe(toggleElementClass.bind(null, element, className));
1387
+ continue;
1388
+ }
1389
+ if(value.$hydrate) {
1390
+ value.$hydrate(element, className);
1373
1391
  continue;
1374
1392
  }
1375
1393
  element.classes.toggle(className, value);
@@ -1439,12 +1457,6 @@ var NativeDocument = (function (exports) {
1439
1457
  }
1440
1458
  }
1441
1459
 
1442
- const NdBindings = {
1443
- class: (element, value) => bindClassAttribute(element, value),
1444
- style: (element, value) => bindStyleAttribute(element, value),
1445
- };
1446
-
1447
-
1448
1460
  /**
1449
1461
  *
1450
1462
  * @param {HTMLElement} element
@@ -1460,7 +1472,7 @@ var NativeDocument = (function (exports) {
1460
1472
 
1461
1473
  for(let key in attributes) {
1462
1474
  const attributeName = key.toLowerCase();
1463
- let value = attributes[key];
1475
+ let value = attributes[attributeName];
1464
1476
  if(value == null) {
1465
1477
  continue;
1466
1478
  }
@@ -1468,10 +1480,13 @@ var NativeDocument = (function (exports) {
1468
1480
  value.handleNdAttribute(element, attributeName, value);
1469
1481
  continue;
1470
1482
  }
1471
- if(typeof value === 'object') {
1472
- const binding = NdBindings[attributeName];
1473
- if(binding) {
1474
- binding(element, value);
1483
+ if(typeof value === 'object') {
1484
+ if(attributeName === 'class') {
1485
+ bindClassAttribute(element, value);
1486
+ continue;
1487
+ }
1488
+ if(attributeName === 'style') {
1489
+ bindStyleAttribute(element, value);
1475
1490
  continue;
1476
1491
  }
1477
1492
  }
@@ -1541,14 +1556,6 @@ var NativeDocument = (function (exports) {
1541
1556
  element.setAttribute(attributeName, this);
1542
1557
  };
1543
1558
 
1544
- Number.prototype.handleNdAttribute = function(element, attributeName) {
1545
- element.setAttribute(attributeName, this);
1546
- };
1547
-
1548
- Boolean.prototype.handleNdAttribute = function(element, attrName) {
1549
- bindBooleanAttribute(element, attrName, this);
1550
- };
1551
-
1552
1559
  ObservableItem.prototype.handleNdAttribute = function(element, attributeName) {
1553
1560
  if(BOOLEAN_ATTRIBUTES.has(attributeName)) {
1554
1561
  bindBooleanAttribute(element, attributeName, this);
@@ -1640,14 +1647,19 @@ var NativeDocument = (function (exports) {
1640
1647
  PluginsManager.emit('AfterProcessChildren', parent);
1641
1648
  },
1642
1649
  getChild(child) {
1643
- while (child?.toNdElement) {
1644
- child = child.toNdElement();
1645
-
1646
- if (Validator.isElement(child)) return child;
1647
- if (!child) return null;
1650
+ if(child == null) {
1651
+ return null;
1652
+ }
1653
+ if(child.toNdElement) {
1654
+ do {
1655
+ child = child.toNdElement();
1656
+ if(Validator.isElement(child)) {
1657
+ return child;
1658
+ }
1659
+ } while (child.toNdElement);
1648
1660
  }
1649
1661
 
1650
- return child ? ElementCreator.createStaticTextNode(null, child) : null;
1662
+ return ElementCreator.createStaticTextNode(null, child);
1651
1663
  },
1652
1664
  /**
1653
1665
  *
@@ -2143,14 +2155,6 @@ var NativeDocument = (function (exports) {
2143
2155
  return null;
2144
2156
  };
2145
2157
 
2146
- const findByPath = (root, path) => {
2147
- let target = root;
2148
- for (let i = 0, len = path.length; i < len; i++) {
2149
- target = target.childNodes[path[i]];
2150
- }
2151
- return target;
2152
- };
2153
-
2154
2158
  const $hydrateFn = function(hydrateFunction, targetType, element, property) {
2155
2159
  if(!cloneBindingsDataCache.has(element)) {
2156
2160
  // { classes, styles, attributes, value, attach }
@@ -2176,27 +2180,43 @@ var NativeDocument = (function (exports) {
2176
2180
  }
2177
2181
  };
2178
2182
 
2183
+
2184
+ const applyBindingTreePath = (root, target, data, path) => {
2185
+ let newTarget = null;
2186
+ if(path.fn) {
2187
+ newTarget = path.fn(data, target, root);
2188
+ }
2189
+ if(path.children) {
2190
+ for(let i = 0, length = path.children.length; i < length; i++) {
2191
+ const currentPath = path.children[i];
2192
+ const pathTargetNode = target.childNodes[currentPath.index];
2193
+ applyBindingTreePath(root, pathTargetNode, data, currentPath);
2194
+ }
2195
+ }
2196
+ return newTarget;
2197
+ };
2198
+
2179
2199
  function TemplateCloner($fn) {
2180
2200
  let $node = null;
2181
2201
  let $hasBindingData = false;
2182
2202
 
2183
- const $bindingPaths = [];
2203
+ const $bindingTreePath = {
2204
+ fn: null,
2205
+ children: [],
2206
+ };
2184
2207
 
2185
- const clone = (node, data, path) => {
2208
+ const clone = (node, data, currentPath) => {
2186
2209
  const bindDingData = cloneBindingsDataCache.get(node);
2187
2210
  if(node.nodeType === 3) {
2188
2211
  if(bindDingData && bindDingData.value) {
2189
- $bindingPaths.push({
2190
- path: [...path],
2191
- fn: (data, targetNode, currentRoot) => {
2192
- const newNode = bindDingData.value(data);
2193
- targetNode.replaceWith(newNode);
2194
- if (targetNode === currentRoot) {
2195
- return newNode;
2196
- }
2197
- return null;
2212
+ currentPath.fn = (data, targetNode, currentRoot) => {
2213
+ const newNode = bindDingData.value(data);
2214
+ if (targetNode === currentRoot) {
2215
+ return newNode;
2198
2216
  }
2199
- });
2217
+ targetNode.replaceWith(newNode);
2218
+ return null;
2219
+ };
2200
2220
  return bindDingData.value(data);
2201
2221
  }
2202
2222
  return node.cloneNode(true);
@@ -2208,35 +2228,35 @@ var NativeDocument = (function (exports) {
2208
2228
  if(bindDingData) {
2209
2229
  bindAttributes(nodeCloned, bindDingData, data);
2210
2230
  bindAttachMethods(nodeCloned, bindDingData, data);
2211
- $bindingPaths.push({
2212
- path: [...path],
2213
- fn: (data, targetNode) => {
2214
- bindAttributes(targetNode, bindDingData, data);
2215
- bindAttachMethods(targetNode, bindDingData, data);
2216
- }
2217
- });
2231
+ currentPath.fn = (data, targetNode) => {
2232
+ bindAttributes(targetNode, bindDingData, data);
2233
+ bindAttachMethods(targetNode, bindDingData, data);
2234
+ };
2218
2235
  }
2219
2236
  const childNodes = node.childNodes;
2237
+ const bindingPathChildren = [];
2220
2238
  for(let i = 0, length = childNodes.length; i < length; i++) {
2221
2239
  const childNode = childNodes[i];
2222
- path.push(i);
2240
+ const path = { index: i, fn: null };
2223
2241
  const childNodeCloned = clone(childNode, data, path);
2224
- path.pop();
2242
+ if(path.children || path.fn) {
2243
+ bindingPathChildren.push(path);
2244
+ }
2225
2245
  nodeCloned.appendChild(childNodeCloned);
2226
2246
  }
2247
+ if(bindingPathChildren.length) {
2248
+ currentPath.children = currentPath.children || [];
2249
+ currentPath.children = bindingPathChildren;
2250
+ }
2227
2251
  return nodeCloned;
2228
2252
  };
2229
2253
 
2230
2254
  const cloneWithBindingPaths = (data) => {
2231
2255
  let root = $node.cloneNode(true);
2232
2256
 
2233
- for (let i = 0, len = $bindingPaths.length; i < len; i++) {
2234
- const binding = $bindingPaths[i];
2235
- const target = findByPath(root, binding.path);
2236
- const newRoot = binding.fn(data, target, root);
2237
- if(newRoot) {
2238
- root = newRoot;
2239
- }
2257
+ const newRoot = applyBindingTreePath(root, root, data, $bindingTreePath);
2258
+ if(newRoot) {
2259
+ root = newRoot;
2240
2260
  }
2241
2261
 
2242
2262
  return root;
@@ -2249,7 +2269,7 @@ var NativeDocument = (function (exports) {
2249
2269
  return $node.cloneNode(true);
2250
2270
  }
2251
2271
 
2252
- const firstClone = clone($node, data, []);
2272
+ const firstClone = clone($node, data, $bindingTreePath);
2253
2273
  this.clone = cloneWithBindingPaths;
2254
2274
  return firstClone;
2255
2275
  };
@@ -2357,6 +2377,8 @@ var NativeDocument = (function (exports) {
2357
2377
  };
2358
2378
  }
2359
2379
 
2380
+ DocumentFragment.prototype.__IS_FRAGMENT = true;
2381
+
2360
2382
  Function.prototype.args = function(...args) {
2361
2383
  return exports.withValidation(this, args);
2362
2384
  };
@@ -2891,7 +2913,7 @@ var NativeDocument = (function (exports) {
2891
2913
 
2892
2914
  mutationMethods.forEach((method) => {
2893
2915
  ObservableArray.prototype[method] = function(...values) {
2894
- const result = this.$currentValue[method](...values);
2916
+ const result = this.$currentValue[method].apply(this.$currentValue, values);
2895
2917
  this.trigger({ action: method, args: values, result });
2896
2918
  return result;
2897
2919
  };
@@ -2899,7 +2921,7 @@ var NativeDocument = (function (exports) {
2899
2921
 
2900
2922
  noMutationMethods.forEach((method) => {
2901
2923
  ObservableArray.prototype[method] = function(...values) {
2902
- return this.$currentValue[method](...values);
2924
+ return this.$currentValue[method].apply(this.$currentValue, values);
2903
2925
  };
2904
2926
  });
2905
2927
 
@@ -2917,7 +2939,7 @@ var NativeDocument = (function (exports) {
2917
2939
  };
2918
2940
 
2919
2941
  ObservableArray.prototype.merge = function(values) {
2920
- this.$currentValue.push(...values);
2942
+ this.$currentValue.push.apply(this.$currentValue, values);
2921
2943
  this.trigger({ action: 'merge', args: values });
2922
2944
  };
2923
2945
 
@@ -2993,7 +3015,7 @@ var NativeDocument = (function (exports) {
2993
3015
  const deps = Array.isArray(predicate.dependencies)
2994
3016
  ? predicate.dependencies
2995
3017
  : [predicate.dependencies];
2996
- observableDependencies.push(...deps);
3018
+ observableDependencies.push.apply(observableDependencies, deps);
2997
3019
  }
2998
3020
  } else if(typeof predicate === 'function') {
2999
3021
  filterCallbacks[key] = predicate;
@@ -3434,7 +3456,7 @@ var NativeDocument = (function (exports) {
3434
3456
  }
3435
3457
  cache.set(keyId, { keyId, isNew: true, child: new WeakRef(child), indexObserver});
3436
3458
  } catch (e) {
3437
- DebugManager.error('ForEach', `Error creating element for key ${keyId}` , e);
3459
+ DebugManager$1.error('ForEach', `Error creating element for key ${keyId}` , e);
3438
3460
  throw e;
3439
3461
  }
3440
3462
  return keyId;
@@ -3780,7 +3802,7 @@ var NativeDocument = (function (exports) {
3780
3802
  */
3781
3803
  const ShowIf = function(condition, child, { comment = null, shouldKeepInCache = true} = {}) {
3782
3804
  if(!(Validator.isObservable(condition)) && !Validator.isObservableWhenResult(condition)) {
3783
- return DebugManager.warn('ShowIf', "ShowIf : condition must be an Observable / "+comment, condition);
3805
+ return DebugManager$1.warn('ShowIf', "ShowIf : condition must be an Observable / "+comment, condition);
3784
3806
  }
3785
3807
  const element = Anchor('Show if : '+(comment || ''));
3786
3808
 
@@ -4567,7 +4589,7 @@ var NativeDocument = (function (exports) {
4567
4589
  window.history.pushState({ name: route.name(), params, path}, route.name() || path , path);
4568
4590
  this.handleRouteChange(route, params, query, path);
4569
4591
  } catch (e) {
4570
- DebugManager.error('HistoryRouter', 'Error in pushState', e);
4592
+ DebugManager$1.error('HistoryRouter', 'Error in pushState', e);
4571
4593
  }
4572
4594
  };
4573
4595
  /**
@@ -4580,7 +4602,7 @@ var NativeDocument = (function (exports) {
4580
4602
  window.history.replaceState({ name: route.name(), params, path}, route.name() || path , path);
4581
4603
  this.handleRouteChange(route, params, {}, path);
4582
4604
  } catch(e) {
4583
- DebugManager.error('HistoryRouter', 'Error in replaceState', e);
4605
+ DebugManager$1.error('HistoryRouter', 'Error in replaceState', e);
4584
4606
  }
4585
4607
  };
4586
4608
  this.forward = function() {
@@ -4607,7 +4629,7 @@ var NativeDocument = (function (exports) {
4607
4629
  }
4608
4630
  this.handleRouteChange(route, params, query, path);
4609
4631
  } catch(e) {
4610
- DebugManager.error('HistoryRouter', 'Error in popstate event', e);
4632
+ DebugManager$1.error('HistoryRouter', 'Error in popstate event', e);
4611
4633
  }
4612
4634
  });
4613
4635
  const { route, params, query, path } = this.resolve(defaultPath || (window.location.pathname+window.location.search));
@@ -4770,7 +4792,7 @@ var NativeDocument = (function (exports) {
4770
4792
  listener(request);
4771
4793
  next && next(request);
4772
4794
  } catch (e) {
4773
- DebugManager.warn('Route Listener', 'Error in listener:', e);
4795
+ DebugManager$1.warn('Route Listener', 'Error in listener:', e);
4774
4796
  }
4775
4797
  }
4776
4798
  };
@@ -4929,7 +4951,7 @@ var NativeDocument = (function (exports) {
4929
4951
  */
4930
4952
  Router.create = function(options, callback) {
4931
4953
  if(!Validator.isFunction(callback)) {
4932
- DebugManager.error('Router', 'Callback must be a function', e);
4954
+ DebugManager$1.error('Router', 'Callback must be a function', e);
4933
4955
  throw new RouterError('Callback must be a function');
4934
4956
  }
4935
4957
  const router = new Router(options);