native-document 1.0.88 → 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.
@@ -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,10 +658,11 @@ 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
668
  DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
@@ -669,32 +672,34 @@ var NativeComponents = (function (exports) {
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]);
@@ -1426,36 +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
1437
  /**
1444
1438
  *
1445
1439
  * @param {HTMLElement} element
1446
1440
  * @param {Object} data
1447
1441
  */
1448
1442
  function bindClassAttribute(element, data) {
1449
- 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];
1450
1447
  const value = data[className];
1451
1448
  if(value.__$isObservable) {
1452
1449
  element.classes.toggle(className, value.val());
1453
- value.subscribe(toggleElementClass.bind(null, element, className));
1450
+ value.subscribe(handleElementAttributeClass, { element, className });
1451
+ // value.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
1454
1452
  continue;
1455
1453
  }
1456
1454
  if(value.__$isObservableWhen) {
1457
1455
  element.classes.toggle(className, value.isMath());
1458
- value.subscribe(toggleElementClass.bind(null, element, className));
1456
+ value.subscribe(handleElementAttributeClass, { element, className });
1459
1457
  continue;
1460
1458
  }
1461
1459
  if(value.$hydrate) {
@@ -1464,7 +1462,6 @@ var NativeComponents = (function (exports) {
1464
1462
  }
1465
1463
  element.classes.toggle(className, value);
1466
1464
  }
1467
- data = null;
1468
1465
  }
1469
1466
 
1470
1467
  /**
@@ -1473,7 +1470,9 @@ var NativeComponents = (function (exports) {
1473
1470
  * @param {Object} data
1474
1471
  */
1475
1472
  function bindStyleAttribute(element, data) {
1476
- 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];
1477
1476
  const value = data[styleName];
1478
1477
  if(value.__$isObservable) {
1479
1478
  element.style[styleName] = value.val();
@@ -1493,7 +1492,8 @@ var NativeComponents = (function (exports) {
1493
1492
  function bindBooleanAttribute(element, attributeName, value) {
1494
1493
  const isObservable = value.__$isObservable;
1495
1494
  const defaultValue = isObservable? value.val() : value;
1496
- if(Validator.isBoolean(defaultValue)) {
1495
+ const isBoolValue = typeof defaultValue === "boolean";
1496
+ if(isBoolValue) {
1497
1497
  element[attributeName] = defaultValue;
1498
1498
  }
1499
1499
  else {
@@ -1501,13 +1501,13 @@ var NativeComponents = (function (exports) {
1501
1501
  }
1502
1502
  if(isObservable) {
1503
1503
  if(attributeName === 'checked') {
1504
- if(typeof defaultValue === 'boolean') {
1504
+ if(isBoolValue) {
1505
1505
  element.addEventListener('input', () => value.set(element[attributeName]));
1506
+ value.subscribe((newValue) => element[attributeName] = newValue);
1507
+ return;
1506
1508
  }
1507
- else {
1508
- element.addEventListener('input', () => value.set(element.value));
1509
- }
1510
- value.subscribe((newValue) => element[attributeName] = newValue);
1509
+ element.addEventListener('input', () => value.set(element.value));
1510
+ value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
1511
1511
  return;
1512
1512
  }
1513
1513
  value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
@@ -1522,14 +1522,14 @@ var NativeComponents = (function (exports) {
1522
1522
  * @param {Observable} value
1523
1523
  */
1524
1524
  function bindAttributeWithObservable(element, attributeName, value) {
1525
- const applyValue = attributeName === 'value' ? (newValue) => element.value = newValue : (newValue) => element.setAttribute(attributeName, newValue);
1526
- value.subscribe(applyValue);
1527
-
1528
1525
  if(attributeName === 'value') {
1526
+ value.subscribe((newValue) => element.value = newValue);
1529
1527
  element.value = value.val();
1530
1528
  element.addEventListener('input', () => value.set(element.value));
1531
1529
  return;
1532
1530
  }
1531
+
1532
+ value.subscribe((newValue) => element.setAttribute(attributeName, newValue));
1533
1533
  element.setAttribute(attributeName, value.val());
1534
1534
  }
1535
1535
 
@@ -1539,10 +1539,12 @@ var NativeComponents = (function (exports) {
1539
1539
  * @param {Object} attributes
1540
1540
  */
1541
1541
  function AttributesWrapper(element, attributes) {
1542
+ const attributeNames = Object.keys(attributes);
1542
1543
 
1543
- for(let key in attributes) {
1544
- const attributeName = key.toLowerCase();
1545
- 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];
1546
1548
  if(value == null) {
1547
1549
  continue;
1548
1550
  }
@@ -1565,6 +1567,10 @@ var NativeComponents = (function (exports) {
1565
1567
  continue;
1566
1568
  }
1567
1569
 
1570
+ if(attributeName === 'value') {
1571
+ element.value = value;
1572
+ continue;
1573
+ }
1568
1574
  element.setAttribute(attributeName, value);
1569
1575
  }
1570
1576
  return element;
@@ -113,10 +113,10 @@ var NativeDocument = (function (exports) {
113
113
 
114
114
  ObservableChecker.prototype.__$isObservableChecker = true;
115
115
 
116
- ObservableChecker.prototype.subscribe = function(callback) {
117
- const unSubscribe = this.observable.subscribe((value) => {
118
- callback && callback(this.checker(value));
119
- });
116
+ ObservableChecker.prototype.subscribe = function(callback, context = null) {
117
+ const unSubscribe = this.observable.subscribe((value, _, __) => {
118
+ callback && callback(this.checker(value), _, __, context);
119
+ }, context);
120
120
  this.unSubscriptions.push(unSubscribe);
121
121
  return unSubscribe;
122
122
  };
@@ -228,8 +228,8 @@ var NativeDocument = (function (exports) {
228
228
 
229
229
  ObservableWhen.prototype.__$isObservableWhen = true;
230
230
 
231
- ObservableWhen.prototype.subscribe = function(callback) {
232
- return this.$observer.on(this.$target, callback);
231
+ ObservableWhen.prototype.subscribe = function(callback, context = null) {
232
+ return this.$observer.on(this.$target, callback, context);
233
233
  };
234
234
 
235
235
  ObservableWhen.prototype.val = function() {
@@ -365,7 +365,7 @@ var NativeDocument = (function (exports) {
365
365
  };
366
366
 
367
367
  ObservableItem.prototype.triggerFirstListener = function(operations) {
368
- this.$firstListener(this.$currentValue, this.$previousValue, operations || {});
368
+ this.$firstListener.callback(this.$currentValue, this.$previousValue, operations || {}, this.$firstListener.context);
369
369
  };
370
370
 
371
371
  ObservableItem.prototype.triggerListeners = function(operations) {
@@ -375,14 +375,14 @@ var NativeDocument = (function (exports) {
375
375
 
376
376
  operations = operations || DEFAULT_OPERATIONS;
377
377
  for(let i = 0, length = $listeners.length; i < length; i++) {
378
- $listeners[i];
379
- $listeners[i]($currentValue, $previousValue, operations);
378
+ const listener = $listeners[i];
379
+ listener.callback($currentValue, $previousValue, operations, listener.context);
380
380
  }
381
381
  };
382
382
 
383
383
  const handleWatcherCallback = function(callbacks, value) {
384
- if(typeof callbacks === "function") {
385
- callbacks(value);
384
+ if(callbacks.callback) {
385
+ callbacks.callback(value, null, null, callbacks.context);
386
386
  return;
387
387
  }
388
388
  if (callbacks.set) {
@@ -390,7 +390,9 @@ var NativeDocument = (function (exports) {
390
390
  return;
391
391
  }
392
392
  callbacks.forEach(callback => {
393
- callback.set ? callback.set(value) : callback(value);
393
+ callback.callback
394
+ ? callback.callback(value, null, null, callback.context)
395
+ : callback.set(value);
394
396
  });
395
397
  };
396
398
 
@@ -519,10 +521,11 @@ var NativeDocument = (function (exports) {
519
521
  /**
520
522
  *
521
523
  * @param {Function} callback
524
+ * @param {?Object} context
522
525
  * @param {any} target
523
526
  * @returns {(function(): void)}
524
527
  */
525
- ObservableItem.prototype.subscribe = function(callback, target = null) {
528
+ ObservableItem.prototype.subscribe = function(callback, context = null, target = null) {
526
529
  this.$listeners = this.$listeners ?? [];
527
530
  if (this.$isCleanedUp) {
528
531
  DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
@@ -532,13 +535,15 @@ var NativeDocument = (function (exports) {
532
535
  throw new NativeDocumentError('Callback must be a function');
533
536
  }
534
537
 
535
- this.$listeners.push(callback);
538
+
539
+ const finalCallback = { callback, context };
540
+ this.$listeners.push(finalCallback);
536
541
  this.assocTrigger();
537
542
  {
538
543
  PluginsManager.emit('ObservableSubscribe', this, target);
539
544
  }
540
545
  return () => {
541
- this.unsubscribe(callback);
546
+ this.unsubscribe(finalCallback);
542
547
  this.assocTrigger();
543
548
  {
544
549
  PluginsManager.emit('ObservableUnsubscribe', this);
@@ -546,24 +551,24 @@ var NativeDocument = (function (exports) {
546
551
  };
547
552
  };
548
553
 
549
- ObservableItem.prototype.on = function(value, callback) {
554
+ ObservableItem.prototype.on = function(value, callback, context = null) {
550
555
  this.$watchers = this.$watchers ?? new Map();
551
556
 
552
557
  let watchValueList = this.$watchers.get(value);
558
+ const finalCallback = { callback, context };
553
559
 
554
560
  if(!watchValueList) {
555
- this.$watchers.set(value, callback);
561
+ this.$watchers.set(value, finalCallback);
556
562
  } else if(!Validator.isArray(watchValueList)) {
557
- watchValueList = [watchValueList];
563
+ watchValueList = [watchValueList, finalCallback];
558
564
  this.$watchers.set(value, watchValueList);
559
- return;
560
565
  } else {
561
- watchValueList.push(callback);
566
+ watchValueList.push(finalCallback);
562
567
  }
563
568
 
564
569
  this.assocTrigger();
565
570
  return () => {
566
- const index = watchValueList.indexOf(callback);
571
+ const index = watchValueList.indexOf(finalCallback);
567
572
  watchValueList?.splice(index, 1);
568
573
  if(watchValueList.size === 1) {
569
574
  this.$watchers.set(value, watchValueList[0]);
@@ -1349,40 +1354,29 @@ var NativeDocument = (function (exports) {
1349
1354
  setInterval(() => MemoryManager.cleanObservables(threshold), interval);
1350
1355
  };
1351
1356
 
1352
- ObservableItem.prototype.bindNdClass = function(element, className) {
1353
- element.classes.toggle(className, this.val());
1354
- this.subscribe(toggleElementClass.bind(null, element, className));
1355
- };
1356
-
1357
- ObservableWhen.prototype.bindNdClass = function(element, className) {
1358
- element.classes.toggle(className, this.isMath());
1359
- this.subscribe(toggleElementClass.bind(null, element, className));
1357
+ const handleElementAttributeClass = function(shouldAdd, _, __, context) {
1358
+ context.element.classes.toggle(context.className, shouldAdd);
1360
1359
  };
1361
-
1362
- TemplateBinding.prototype.bindNdClass = function(element, className) {
1363
- this.$hydrate(element, className);
1364
- };
1365
-
1366
- function toggleElementClass(element, className, shouldAdd) {
1367
- element.classes.toggle(className, shouldAdd);
1368
- }
1369
-
1370
1360
  /**
1371
1361
  *
1372
1362
  * @param {HTMLElement} element
1373
1363
  * @param {Object} data
1374
1364
  */
1375
1365
  function bindClassAttribute(element, data) {
1376
- for(let className in data) {
1366
+ const classNames = Object.keys(data);
1367
+
1368
+ for(let i = 0, length = classNames.length; i < length; i++) {
1369
+ const className = classNames[i];
1377
1370
  const value = data[className];
1378
1371
  if(value.__$isObservable) {
1379
1372
  element.classes.toggle(className, value.val());
1380
- value.subscribe(toggleElementClass.bind(null, element, className));
1373
+ value.subscribe(handleElementAttributeClass, { element, className });
1374
+ // value.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
1381
1375
  continue;
1382
1376
  }
1383
1377
  if(value.__$isObservableWhen) {
1384
1378
  element.classes.toggle(className, value.isMath());
1385
- value.subscribe(toggleElementClass.bind(null, element, className));
1379
+ value.subscribe(handleElementAttributeClass, { element, className });
1386
1380
  continue;
1387
1381
  }
1388
1382
  if(value.$hydrate) {
@@ -1391,7 +1385,6 @@ var NativeDocument = (function (exports) {
1391
1385
  }
1392
1386
  element.classes.toggle(className, value);
1393
1387
  }
1394
- data = null;
1395
1388
  }
1396
1389
 
1397
1390
  /**
@@ -1400,7 +1393,9 @@ var NativeDocument = (function (exports) {
1400
1393
  * @param {Object} data
1401
1394
  */
1402
1395
  function bindStyleAttribute(element, data) {
1403
- for(let styleName in data) {
1396
+ const keys = Object.keys(data);
1397
+ for(let i = 0, length = keys.length; i < length; i++) {
1398
+ const styleName = keys[i];
1404
1399
  const value = data[styleName];
1405
1400
  if(value.__$isObservable) {
1406
1401
  element.style[styleName] = value.val();
@@ -1420,7 +1415,8 @@ var NativeDocument = (function (exports) {
1420
1415
  function bindBooleanAttribute(element, attributeName, value) {
1421
1416
  const isObservable = value.__$isObservable;
1422
1417
  const defaultValue = isObservable? value.val() : value;
1423
- if(Validator.isBoolean(defaultValue)) {
1418
+ const isBoolValue = typeof defaultValue === "boolean";
1419
+ if(isBoolValue) {
1424
1420
  element[attributeName] = defaultValue;
1425
1421
  }
1426
1422
  else {
@@ -1428,13 +1424,13 @@ var NativeDocument = (function (exports) {
1428
1424
  }
1429
1425
  if(isObservable) {
1430
1426
  if(attributeName === 'checked') {
1431
- if(typeof defaultValue === 'boolean') {
1427
+ if(isBoolValue) {
1432
1428
  element.addEventListener('input', () => value.set(element[attributeName]));
1429
+ value.subscribe((newValue) => element[attributeName] = newValue);
1430
+ return;
1433
1431
  }
1434
- else {
1435
- element.addEventListener('input', () => value.set(element.value));
1436
- }
1437
- value.subscribe((newValue) => element[attributeName] = newValue);
1432
+ element.addEventListener('input', () => value.set(element.value));
1433
+ value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
1438
1434
  return;
1439
1435
  }
1440
1436
  value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
@@ -1449,14 +1445,14 @@ var NativeDocument = (function (exports) {
1449
1445
  * @param {Observable} value
1450
1446
  */
1451
1447
  function bindAttributeWithObservable(element, attributeName, value) {
1452
- const applyValue = attributeName === 'value' ? (newValue) => element.value = newValue : (newValue) => element.setAttribute(attributeName, newValue);
1453
- value.subscribe(applyValue);
1454
-
1455
1448
  if(attributeName === 'value') {
1449
+ value.subscribe((newValue) => element.value = newValue);
1456
1450
  element.value = value.val();
1457
1451
  element.addEventListener('input', () => value.set(element.value));
1458
1452
  return;
1459
1453
  }
1454
+
1455
+ value.subscribe((newValue) => element.setAttribute(attributeName, newValue));
1460
1456
  element.setAttribute(attributeName, value.val());
1461
1457
  }
1462
1458
 
@@ -1467,11 +1463,15 @@ var NativeDocument = (function (exports) {
1467
1463
  */
1468
1464
  function AttributesWrapper(element, attributes) {
1469
1465
 
1470
- Validator.validateAttributes(attributes);
1466
+ {
1467
+ Validator.validateAttributes(attributes);
1468
+ }
1469
+ const attributeNames = Object.keys(attributes);
1471
1470
 
1472
- for(let key in attributes) {
1473
- const attributeName = key.toLowerCase();
1474
- let value = attributes[attributeName];
1471
+ for(let i = 0, length = attributeNames.length; i < length; i++) {
1472
+ const originalAttributeName = attributeNames[i];
1473
+ const attributeName = originalAttributeName.toLowerCase();
1474
+ let value = attributes[originalAttributeName];
1475
1475
  if(value == null) {
1476
1476
  continue;
1477
1477
  }
@@ -1494,6 +1494,10 @@ var NativeDocument = (function (exports) {
1494
1494
  continue;
1495
1495
  }
1496
1496
 
1497
+ if(attributeName === 'value') {
1498
+ element.value = value;
1499
+ continue;
1500
+ }
1497
1501
  element.setAttribute(attributeName, value);
1498
1502
  }
1499
1503
  return element;