native-document 1.0.87 → 1.0.89

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.
@@ -256,16 +256,16 @@ var NativeComponents = (function (exports) {
256
256
  return this.$build();
257
257
  };
258
258
 
259
- let DebugManager = {};
259
+ let DebugManager$1 = {};
260
260
  {
261
- DebugManager = {
261
+ DebugManager$1 = {
262
262
  log() {},
263
263
  warn() {},
264
264
  error() {},
265
265
  disable() {}
266
266
  };
267
267
  }
268
- var DebugManager$1 = DebugManager;
268
+ var DebugManager = DebugManager$1;
269
269
 
270
270
  const MemoryManager = (function() {
271
271
 
@@ -314,7 +314,7 @@ var NativeComponents = (function (exports) {
314
314
  }
315
315
  }
316
316
  if (cleanedCount > 0) {
317
- DebugManager$1.log('Memory Auto Clean', `🧹 Cleaned ${cleanedCount} orphaned observables`);
317
+ DebugManager.log('Memory Auto Clean', `🧹 Cleaned ${cleanedCount} orphaned observables`);
318
318
  }
319
319
  }
320
320
  };
@@ -334,10 +334,10 @@ var NativeComponents = (function (exports) {
334
334
 
335
335
  ObservableChecker.prototype.__$isObservableChecker = true;
336
336
 
337
- ObservableChecker.prototype.subscribe = function(callback) {
338
- const unSubscribe = this.observable.subscribe((value) => {
339
- callback && callback(this.checker(value));
340
- });
337
+ ObservableChecker.prototype.subscribe = function(callback, context = null) {
338
+ const unSubscribe = this.observable.subscribe((value, _, __) => {
339
+ callback && callback(this.checker(value), _, __, context);
340
+ }, context);
341
341
  this.unSubscriptions.push(unSubscribe);
342
342
  return unSubscribe;
343
343
  };
@@ -369,8 +369,8 @@ var NativeComponents = (function (exports) {
369
369
 
370
370
  ObservableWhen.prototype.__$isObservableWhen = true;
371
371
 
372
- ObservableWhen.prototype.subscribe = function(callback) {
373
- return this.$observer.on(this.$target, callback);
372
+ ObservableWhen.prototype.subscribe = function(callback, context = null) {
373
+ return this.$observer.on(this.$target, callback, context);
374
374
  };
375
375
 
376
376
  ObservableWhen.prototype.val = function() {
@@ -508,7 +508,7 @@ var NativeComponents = (function (exports) {
508
508
  };
509
509
 
510
510
  ObservableItem.prototype.triggerFirstListener = function(operations) {
511
- this.$firstListener(this.$currentValue, this.$previousValue, operations || {});
511
+ this.$firstListener.callback(this.$currentValue, this.$previousValue, operations || {}, this.$firstListener.context);
512
512
  };
513
513
 
514
514
  ObservableItem.prototype.triggerListeners = function(operations) {
@@ -518,14 +518,14 @@ var NativeComponents = (function (exports) {
518
518
 
519
519
  operations = operations || DEFAULT_OPERATIONS;
520
520
  for(let i = 0, length = $listeners.length; i < length; i++) {
521
- $listeners[i];
522
- $listeners[i]($currentValue, $previousValue, operations);
521
+ const listener = $listeners[i];
522
+ listener.callback($currentValue, $previousValue, operations, listener.context);
523
523
  }
524
524
  };
525
525
 
526
526
  const handleWatcherCallback = function(callbacks, value) {
527
- if(typeof callbacks === "function") {
528
- callbacks(value);
527
+ if(callbacks.callback) {
528
+ callbacks.callback(value, null, null, callbacks.context);
529
529
  return;
530
530
  }
531
531
  if (callbacks.set) {
@@ -533,7 +533,9 @@ var NativeComponents = (function (exports) {
533
533
  return;
534
534
  }
535
535
  callbacks.forEach(callback => {
536
- callback.set ? callback.set(value) : callback(value);
536
+ callback.callback
537
+ ? callback.callback(value, null, null, callback.context)
538
+ : callback.set(value);
537
539
  });
538
540
  };
539
541
 
@@ -656,45 +658,48 @@ var NativeComponents = (function (exports) {
656
658
  /**
657
659
  *
658
660
  * @param {Function} callback
661
+ * @param {?Object} context
659
662
  * @param {any} target
660
663
  * @returns {(function(): void)}
661
664
  */
662
- ObservableItem.prototype.subscribe = function(callback, target = null) {
665
+ ObservableItem.prototype.subscribe = function(callback, context = null, target = null) {
663
666
  this.$listeners = this.$listeners ?? [];
664
667
  if (this.$isCleanedUp) {
665
- DebugManager$1.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
668
+ DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
666
669
  return () => {};
667
670
  }
668
671
  if (typeof callback !== 'function') {
669
672
  throw new NativeDocumentError('Callback must be a function');
670
673
  }
671
674
 
672
- this.$listeners.push(callback);
675
+
676
+ const finalCallback = { callback, context };
677
+ this.$listeners.push(finalCallback);
673
678
  this.assocTrigger();
674
679
  return () => {
675
- this.unsubscribe(callback);
680
+ this.unsubscribe(finalCallback);
676
681
  this.assocTrigger();
677
682
  };
678
683
  };
679
684
 
680
- ObservableItem.prototype.on = function(value, callback) {
685
+ ObservableItem.prototype.on = function(value, callback, context = null) {
681
686
  this.$watchers = this.$watchers ?? new Map();
682
687
 
683
688
  let watchValueList = this.$watchers.get(value);
689
+ const finalCallback = { callback, context };
684
690
 
685
691
  if(!watchValueList) {
686
- this.$watchers.set(value, callback);
692
+ this.$watchers.set(value, finalCallback);
687
693
  } else if(!Validator.isArray(watchValueList)) {
688
- watchValueList = [watchValueList];
694
+ watchValueList = [watchValueList, finalCallback];
689
695
  this.$watchers.set(value, watchValueList);
690
- return;
691
696
  } else {
692
- watchValueList.push(callback);
697
+ watchValueList.push(finalCallback);
693
698
  }
694
699
 
695
700
  this.assocTrigger();
696
701
  return () => {
697
- const index = watchValueList.indexOf(callback);
702
+ const index = watchValueList.indexOf(finalCallback);
698
703
  watchValueList?.splice(index, 1);
699
704
  if(watchValueList.size === 1) {
700
705
  this.$watchers.set(value, watchValueList[0]);
@@ -1035,17 +1040,17 @@ var NativeComponents = (function (exports) {
1035
1040
  const method = methods[name];
1036
1041
 
1037
1042
  if (typeof method !== 'function') {
1038
- DebugManager$1.warn('NDElement.extend', `"${name}" is not a function, skipping`);
1043
+ DebugManager.warn('NDElement.extend', `"${name}" is not a function, skipping`);
1039
1044
  continue;
1040
1045
  }
1041
1046
 
1042
1047
  if (protectedMethods.has(name)) {
1043
- DebugManager$1.error('NDElement.extend', `Cannot override protected method "${name}"`);
1048
+ DebugManager.error('NDElement.extend', `Cannot override protected method "${name}"`);
1044
1049
  throw new NativeDocumentError(`Cannot override protected method "${name}"`);
1045
1050
  }
1046
1051
 
1047
1052
  if (NDElement.prototype[name]) {
1048
- DebugManager$1.warn('NDElement.extend', `Overwriting existing prototype method "${name}"`);
1053
+ DebugManager.warn('NDElement.extend', `Overwriting existing prototype method "${name}"`);
1049
1054
  }
1050
1055
 
1051
1056
  NDElement.prototype[name] = method;
@@ -1227,7 +1232,7 @@ var NativeComponents = (function (exports) {
1227
1232
  anchorFragment.appendChild = function(child, before = null) {
1228
1233
  const parent = anchorEnd.parentNode;
1229
1234
  if(!parent) {
1230
- DebugManager$1.error('Anchor', 'Anchor : parent not found', child);
1235
+ DebugManager.error('Anchor', 'Anchor : parent not found', child);
1231
1236
  return;
1232
1237
  }
1233
1238
  before = before ?? anchorEnd;
@@ -1426,56 +1431,29 @@ var NativeComponents = (function (exports) {
1426
1431
  setInterval(() => MemoryManager.cleanObservables(threshold), interval);
1427
1432
  };
1428
1433
 
1429
- ObservableItem.prototype.bindNdClass = function(element, className) {
1430
- element.classes.toggle(className, this.val());
1431
- this.subscribe(toggleElementClass.bind(null, element, className));
1432
- };
1433
-
1434
- ObservableWhen.prototype.bindNdClass = function(element, className) {
1435
- element.classes.toggle(className, this.isMath());
1436
- this.subscribe(toggleElementClass.bind(null, element, className));
1434
+ const handleElementAttributeClass = function(shouldAdd, _, __, context) {
1435
+ context.element.classes.toggle(context.className, shouldAdd);
1437
1436
  };
1438
-
1439
- function toggleElementClass(element, className, shouldAdd) {
1440
- element.classes.toggle(className, shouldAdd);
1441
- }
1442
-
1443
- function toggleElementStyle(element, styleName, newValue) {
1444
- element.style[styleName] = newValue;
1445
- }
1446
-
1447
- function updateInputFromObserver(element, attributeName, newValue) {
1448
- if(Validator.isBoolean(newValue)) {
1449
- element[attributeName] = newValue;
1450
- return;
1451
- }
1452
- element[attributeName] = newValue === element.value;
1453
- }
1454
-
1455
- function updateObserverFromInput(element, attributeName, defaultValue, value) {
1456
- if(Validator.isBoolean(defaultValue)) {
1457
- value.set(element[attributeName]);
1458
- return;
1459
- }
1460
- value.set(element.value);
1461
- }
1462
-
1463
1437
  /**
1464
1438
  *
1465
1439
  * @param {HTMLElement} element
1466
1440
  * @param {Object} data
1467
1441
  */
1468
1442
  function bindClassAttribute(element, data) {
1469
- for(let className in data) {
1443
+ const classNames = Object.keys(data);
1444
+
1445
+ for(let i = 0, length = classNames.length; i < length; i++) {
1446
+ const className = classNames[i];
1470
1447
  const value = data[className];
1471
- if(Validator.isObservable(value)) {
1448
+ if(value.__$isObservable) {
1472
1449
  element.classes.toggle(className, value.val());
1473
- value.subscribe(toggleElementClass.bind(null, element, className));
1450
+ value.subscribe(handleElementAttributeClass, { element, className });
1451
+ // value.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
1474
1452
  continue;
1475
1453
  }
1476
- if(Validator.isObservableWhenResult(value)) {
1454
+ if(value.__$isObservableWhen) {
1477
1455
  element.classes.toggle(className, value.isMath());
1478
- value.subscribe(toggleElementClass.bind(null, element, className));
1456
+ value.subscribe(handleElementAttributeClass, { element, className });
1479
1457
  continue;
1480
1458
  }
1481
1459
  if(value.$hydrate) {
@@ -1484,7 +1462,6 @@ var NativeComponents = (function (exports) {
1484
1462
  }
1485
1463
  element.classes.toggle(className, value);
1486
1464
  }
1487
- data = null;
1488
1465
  }
1489
1466
 
1490
1467
  /**
@@ -1493,11 +1470,13 @@ var NativeComponents = (function (exports) {
1493
1470
  * @param {Object} data
1494
1471
  */
1495
1472
  function bindStyleAttribute(element, data) {
1496
- for(let styleName in data) {
1473
+ const keys = Object.keys(data);
1474
+ for(let i = 0, length = keys.length; i < length; i++) {
1475
+ const styleName = keys[i];
1497
1476
  const value = data[styleName];
1498
- if(Validator.isObservable(value)) {
1477
+ if(value.__$isObservable) {
1499
1478
  element.style[styleName] = value.val();
1500
- value.subscribe(toggleElementStyle.bind(null, element, styleName));
1479
+ value.subscribe((newValue) => element.style[styleName] = newValue);
1501
1480
  continue;
1502
1481
  }
1503
1482
  element.style[styleName] = value;
@@ -1511,18 +1490,27 @@ var NativeComponents = (function (exports) {
1511
1490
  * @param {boolean|number|Observable} value
1512
1491
  */
1513
1492
  function bindBooleanAttribute(element, attributeName, value) {
1514
- const defaultValue = Validator.isObservable(value) ? value.val() : value;
1515
- if(Validator.isBoolean(defaultValue)) {
1493
+ const isObservable = value.__$isObservable;
1494
+ const defaultValue = isObservable? value.val() : value;
1495
+ const isBoolValue = typeof defaultValue === "boolean";
1496
+ if(isBoolValue) {
1516
1497
  element[attributeName] = defaultValue;
1517
1498
  }
1518
1499
  else {
1519
1500
  element[attributeName] = defaultValue === element.value;
1520
1501
  }
1521
- if(Validator.isObservable(value)) {
1522
- if(['checked'].includes(attributeName)) {
1523
- element.addEventListener('input', updateObserverFromInput.bind(null, element, attributeName, defaultValue, value));
1502
+ if(isObservable) {
1503
+ if(attributeName === 'checked') {
1504
+ if(isBoolValue) {
1505
+ element.addEventListener('input', () => value.set(element[attributeName]));
1506
+ value.subscribe((newValue) => element[attributeName] = newValue);
1507
+ return;
1508
+ }
1509
+ element.addEventListener('input', () => value.set(element.value));
1510
+ value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
1511
+ return;
1524
1512
  }
1525
- value.subscribe(updateInputFromObserver.bind(null, element, attributeName));
1513
+ value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
1526
1514
  }
1527
1515
  }
1528
1516
 
@@ -1534,19 +1522,15 @@ var NativeComponents = (function (exports) {
1534
1522
  * @param {Observable} value
1535
1523
  */
1536
1524
  function bindAttributeWithObservable(element, attributeName, value) {
1537
- const applyValue = (newValue) => {
1538
- if(attributeName === 'value') {
1539
- element.value = newValue;
1540
- return;
1541
- }
1542
- element.setAttribute(attributeName, newValue);
1543
- };
1544
- applyValue(value.val());
1545
- value.subscribe(applyValue);
1546
-
1547
1525
  if(attributeName === 'value') {
1526
+ value.subscribe((newValue) => element.value = newValue);
1527
+ element.value = value.val();
1548
1528
  element.addEventListener('input', () => value.set(element.value));
1529
+ return;
1549
1530
  }
1531
+
1532
+ value.subscribe((newValue) => element.setAttribute(attributeName, newValue));
1533
+ element.setAttribute(attributeName, value.val());
1550
1534
  }
1551
1535
 
1552
1536
  /**
@@ -1555,14 +1539,12 @@ var NativeComponents = (function (exports) {
1555
1539
  * @param {Object} attributes
1556
1540
  */
1557
1541
  function AttributesWrapper(element, attributes) {
1542
+ const attributeNames = Object.keys(attributes);
1558
1543
 
1559
- if(!Validator.isObject(attributes)) {
1560
- throw new NativeDocumentError('Attributes must be an object');
1561
- }
1562
-
1563
- for(let key in attributes) {
1564
- const attributeName = key.toLowerCase();
1565
- let value = attributes[attributeName];
1544
+ for(let i = 0, length = attributeNames.length; i < length; i++) {
1545
+ const originalAttributeName = attributeNames[i];
1546
+ const attributeName = originalAttributeName.toLowerCase();
1547
+ let value = attributes[originalAttributeName];
1566
1548
  if(value == null) {
1567
1549
  continue;
1568
1550
  }
@@ -1585,6 +1567,10 @@ var NativeComponents = (function (exports) {
1585
1567
  continue;
1586
1568
  }
1587
1569
 
1570
+ if(attributeName === 'value') {
1571
+ element.value = value;
1572
+ continue;
1573
+ }
1588
1574
  element.setAttribute(attributeName, value);
1589
1575
  }
1590
1576
  return element;
@@ -1714,6 +1700,14 @@ var NativeComponents = (function (exports) {
1714
1700
  }
1715
1701
  return Anchor('Fragment');
1716
1702
  },
1703
+ bindTextNode(textNode, value) {
1704
+ if(value?.__$isObservable) {
1705
+ value.subscribe(newValue => textNode.nodeValue = newValue);
1706
+ textNode.nodeValue = value.val();
1707
+ return;
1708
+ }
1709
+ textNode.nodeValue = value;
1710
+ },
1717
1711
  /**
1718
1712
  *
1719
1713
  * @param {*} children