native-document 1.0.40 → 1.0.41

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.
@@ -450,6 +450,16 @@ var NativeDocument = (function (exports) {
450
450
  }
451
451
  return '{{#ObItem::(' +this.$memoryId+ ')}}';
452
452
  };
453
+ ObservableItem.prototype.equals = function(other) {
454
+ if(Validator.isObservable(other)) {
455
+ return this.$currentValue === other.$currentValue;
456
+ }
457
+ return this.$currentValue === other;
458
+ };
459
+
460
+ ObservableItem.prototype.toggle = function() {
461
+ this.set(!this.$currentValue);
462
+ };
453
463
 
454
464
  /**
455
465
  *
@@ -639,8 +649,6 @@ var NativeDocument = (function (exports) {
639
649
  }
640
650
  NDElement.prototype.__$isNDElement = true;
641
651
 
642
-
643
-
644
652
  NDElement.prototype.valueOf = function() {
645
653
  return this.$element;
646
654
  };
@@ -845,6 +853,7 @@ var NativeDocument = (function (exports) {
845
853
  const COMMON_NODE_TYPES = {
846
854
  ELEMENT: 1,
847
855
  TEXT: 3,
856
+ COMMENT: 8,
848
857
  DOCUMENT_FRAGMENT: 11
849
858
  };
850
859
 
@@ -855,6 +864,9 @@ var NativeDocument = (function (exports) {
855
864
  isProxy(value) {
856
865
  return value?.__isProxy__
857
866
  },
867
+ isAnchor(value) {
868
+ return value?.__Anchor__
869
+ },
858
870
  isObservableChecker(value) {
859
871
  return value?.__$isObservableChecker || value instanceof ObservableChecker;
860
872
  },
@@ -886,7 +898,8 @@ var NativeDocument = (function (exports) {
886
898
  return value && (
887
899
  value.nodeType === COMMON_NODE_TYPES.ELEMENT ||
888
900
  value.nodeType === COMMON_NODE_TYPES.TEXT ||
889
- value.nodeType === COMMON_NODE_TYPES.DOCUMENT_FRAGMENT
901
+ value.nodeType === COMMON_NODE_TYPES.DOCUMENT_FRAGMENT ||
902
+ value.nodeType === COMMON_NODE_TYPES.COMMENT
890
903
  );
891
904
  },
892
905
  isFragment(value) {
@@ -980,6 +993,7 @@ var NativeDocument = (function (exports) {
980
993
 
981
994
  function Anchor(name, isUniqueChild = false) {
982
995
  const element = document.createDocumentFragment();
996
+ element.__Anchor__ = true;
983
997
 
984
998
  const anchorStart = document.createComment('Anchor Start : '+name);
985
999
  const anchorEnd = document.createComment('/ Anchor End '+name);
@@ -993,16 +1007,16 @@ var NativeDocument = (function (exports) {
993
1007
  const isParentUniqueChild = (parent) => (isUniqueChild || (parent.firstChild === anchorStart && parent.lastChild === anchorEnd));
994
1008
 
995
1009
  const insertBefore = function(parent, child, target) {
996
- const element = Validator.isElement(child) ? child : ElementCreator.getChild(child);
1010
+ const childElement = Validator.isElement(child) ? child : ElementCreator.getChild(child);
997
1011
  if(parent === element) {
998
- parent.nativeInsertBefore(element, target);
1012
+ parent.nativeInsertBefore(childElement, target);
999
1013
  return;
1000
1014
  }
1001
1015
  if(isParentUniqueChild(parent) && target === anchorEnd) {
1002
- parent.append(element, target);
1016
+ parent.append(childElement, target);
1003
1017
  return;
1004
1018
  }
1005
- parent.insertBefore(element, target);
1019
+ parent.insertBefore(childElement, target);
1006
1020
  };
1007
1021
 
1008
1022
  element.appendElement = function(child, before = null) {
@@ -1024,6 +1038,9 @@ var NativeDocument = (function (exports) {
1024
1038
  before = before ?? anchorEnd;
1025
1039
  insertBefore(parent, child, before);
1026
1040
  };
1041
+ element.append = function(...args ) {
1042
+ return element.appendChild(args);
1043
+ };
1027
1044
 
1028
1045
  element.removeChildren = function() {
1029
1046
  const parent = anchorEnd.parentNode;
@@ -1049,10 +1066,6 @@ var NativeDocument = (function (exports) {
1049
1066
  if(parent === element) {
1050
1067
  return;
1051
1068
  }
1052
- if(isParentUniqueChild(parent)) {
1053
- parent.replaceChildren(anchorStart, anchorEnd);
1054
- return;
1055
- }
1056
1069
  let itemToRemove = anchorStart.nextSibling, tempItem;
1057
1070
  while(itemToRemove && itemToRemove !== anchorEnd) {
1058
1071
  tempItem = itemToRemove.nextSibling;
@@ -1084,9 +1097,6 @@ var NativeDocument = (function (exports) {
1084
1097
  element.appendChild(child, anchor);
1085
1098
  };
1086
1099
 
1087
- element.clear = function() {
1088
- element.remove();
1089
- };
1090
1100
 
1091
1101
  element.endElement = function() {
1092
1102
  return anchorEnd;
@@ -1095,6 +1105,11 @@ var NativeDocument = (function (exports) {
1095
1105
  element.startElement = function() {
1096
1106
  return anchorStart;
1097
1107
  };
1108
+ element.restore = function() {
1109
+ element.appendChild(element);
1110
+ };
1111
+ element.clear = element.remove;
1112
+ element.detach = element.remove;
1098
1113
 
1099
1114
  element.getByIndex = function(index) {
1100
1115
  let currentNode = anchorStart;
@@ -1109,6 +1124,19 @@ var NativeDocument = (function (exports) {
1109
1124
 
1110
1125
  return element;
1111
1126
  }
1127
+ /**
1128
+ *
1129
+ * @param {HTMLElement|DocumentFragment|Text|String|Array} children
1130
+ * @param {{ parent?: HTMLElement, name?: String}} configs
1131
+ * @returns {DocumentFragment}
1132
+ */
1133
+ function createPortal(children, { parent, name = 'unnamed' } = {}) {
1134
+ const anchor = Anchor('Portal '+name);
1135
+ anchor.appendChild(ElementCreator.getChild(children));
1136
+
1137
+ (parent || document.body).appendChild(anchor);
1138
+ return anchor;
1139
+ }
1112
1140
 
1113
1141
  const BOOLEAN_ATTRIBUTES = ['checked', 'selected', 'disabled', 'readonly', 'required', 'autofocus', 'multiple', 'autocomplete', 'hidden', 'contenteditable', 'spellcheck', 'translate', 'draggable', 'async', 'defer', 'autoplay', 'controls', 'loop', 'muted', 'download', 'reversed', 'open', 'default', 'formnovalidate', 'novalidate', 'scoped', 'itemscope', 'allowfullscreen', 'allowpaymentrequest', 'playsinline'];
1114
1142
 
@@ -1830,6 +1858,7 @@ var NativeDocument = (function (exports) {
1830
1858
  this.attach = (fn) => {
1831
1859
  return createBinding(fn, 'attach');
1832
1860
  };
1861
+
1833
1862
  }
1834
1863
 
1835
1864
  function useCache(fn) {
@@ -1852,6 +1881,52 @@ var NativeDocument = (function (exports) {
1852
1881
  };
1853
1882
  }
1854
1883
 
1884
+ function SingletonView($viewCreator) {
1885
+ let $cacheNode = null;
1886
+ let $components = null;
1887
+
1888
+ this.render = (data) => {
1889
+ if(!$cacheNode) {
1890
+ $cacheNode = $viewCreator(this);
1891
+ }
1892
+ if(!$components) {
1893
+ return $cacheNode;
1894
+ }
1895
+ for(const index in $components) {
1896
+ const updater = $components[index];
1897
+ updater(...data);
1898
+ }
1899
+ return $cacheNode;
1900
+ };
1901
+
1902
+ this.createSection = (name, fn) => {
1903
+ $components = $components || {};
1904
+ const anchor = new Anchor('Component '+name);
1905
+
1906
+ $components[name] = function(...args) {
1907
+ anchor.removeChildren();
1908
+ if(!fn) {
1909
+ anchor.append(args);
1910
+ return;
1911
+ }
1912
+ anchor.appendChild(fn(...args));
1913
+ };
1914
+ return anchor;
1915
+ };
1916
+ }
1917
+
1918
+
1919
+ function useSingleton(fn) {
1920
+ let $cache = null;
1921
+
1922
+ return function(...args) {
1923
+ if(!$cache) {
1924
+ $cache = new SingletonView(fn);
1925
+ }
1926
+ return $cache.render(args);
1927
+ };
1928
+ }
1929
+
1855
1930
  Function.prototype.args = function(...args) {
1856
1931
  return exports.withValidation(this, args);
1857
1932
  };
@@ -1988,6 +2063,12 @@ var NativeDocument = (function (exports) {
1988
2063
  observer.populateAndRender = function(iteration, callback) {
1989
2064
  observer.trigger({ action: 'populate', args: [observer.val(), iteration, callback] });
1990
2065
  };
2066
+
2067
+ observer.removeItem = function(item) {
2068
+ const indexOfItem = observer.val().indexOf(item);
2069
+ return observer.remove(indexOfItem);
2070
+ };
2071
+
1991
2072
  observer.remove = function(index) {
1992
2073
  const deleted = observer.val().splice(index, 1);
1993
2074
  if(deleted.length === 0) {
@@ -2021,6 +2102,24 @@ var NativeDocument = (function (exports) {
2021
2102
  return observer.val().length;
2022
2103
  };
2023
2104
 
2105
+ /**
2106
+ *
2107
+ * @param {Function} condition
2108
+ * @returns {number}
2109
+ */
2110
+ observer.count = (condition) => {
2111
+ let count = 0;
2112
+ observer.val().forEach((item, index) => {
2113
+ if(condition(item, index)) {
2114
+ count++;
2115
+ }
2116
+ });
2117
+ return count;
2118
+ };
2119
+ observer.isEmpty = function() {
2120
+ return observer.val().length === 0;
2121
+ };
2122
+
2024
2123
  const overrideMethods = ['map', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat', 'includes', 'indexOf'];
2025
2124
  overrideMethods.forEach((method) => {
2026
2125
  observer[method] = (...args) => {
@@ -2060,6 +2159,10 @@ var NativeDocument = (function (exports) {
2060
2159
  const data = {};
2061
2160
  for(const key in initialValue) {
2062
2161
  const itemValue = initialValue[key];
2162
+ if(Array.isArray(itemValue)) {
2163
+ data[key] = Observable.array(itemValue);
2164
+ continue;
2165
+ }
2063
2166
  data[key] = Observable(itemValue);
2064
2167
  }
2065
2168
 
@@ -2717,7 +2820,7 @@ var NativeDocument = (function (exports) {
2717
2820
  *
2718
2821
  * @param {ObservableItem|ObservableChecker} condition
2719
2822
  * @param {*} child
2720
- * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} comment
2823
+ * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} configs
2721
2824
  * @returns {DocumentFragment}
2722
2825
  */
2723
2826
  const ShowIf = function(condition, child, { comment = null, shouldKeepInCache = true} = {}) {
@@ -2733,7 +2836,7 @@ var NativeDocument = (function (exports) {
2733
2836
  }
2734
2837
  childElement = ElementCreator.getChild(child);
2735
2838
  if(Validator.isFragment(childElement)) {
2736
- childElement = Array.from(childElement.children);
2839
+ childElement = Array.from(childElement.childNodes);
2737
2840
  }
2738
2841
  return childElement;
2739
2842
  };
@@ -2758,15 +2861,14 @@ var NativeDocument = (function (exports) {
2758
2861
  * Hide the element if the condition is true
2759
2862
  * @param {ObservableItem|ObservableChecker} condition
2760
2863
  * @param child
2761
- * @param comment
2864
+ * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} configs
2762
2865
  * @returns {DocumentFragment}
2763
2866
  */
2764
- const HideIf = function(condition, child, comment) {
2765
-
2867
+ const HideIf = function(condition, child, configs) {
2766
2868
  const hideCondition = Observable(!condition.val());
2767
2869
  condition.subscribe(value => hideCondition.set(!value));
2768
2870
 
2769
- return ShowIf(hideCondition, child, comment);
2871
+ return ShowIf(hideCondition, child, configs);
2770
2872
  };
2771
2873
 
2772
2874
  /**
@@ -2774,11 +2876,11 @@ var NativeDocument = (function (exports) {
2774
2876
  *
2775
2877
  * @param {ObservableItem|ObservableChecker} condition
2776
2878
  * @param {*} child
2777
- * @param {string|null} comment
2879
+ * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} configs
2778
2880
  * @returns {DocumentFragment}
2779
2881
  */
2780
- const HideIfNot = function(condition, child, comment) {
2781
- return ShowIf(condition, child, comment);
2882
+ const HideIfNot = function(condition, child, configs) {
2883
+ return ShowIf(condition, child, configs);
2782
2884
  };
2783
2885
 
2784
2886
  /**
@@ -2794,7 +2896,7 @@ var NativeDocument = (function (exports) {
2794
2896
  throw new NativeDocumentError("Toggle : condition must be an Observable");
2795
2897
  }
2796
2898
 
2797
- const anchor = new Anchor();
2899
+ const anchor = new Anchor('Match');
2798
2900
  const cache = new Map();
2799
2901
 
2800
2902
  const getItem = function(key) {
@@ -2839,7 +2941,6 @@ var NativeDocument = (function (exports) {
2839
2941
  * @returns {DocumentFragment}
2840
2942
  */
2841
2943
  const Switch = function ($condition, onTrue, onFalse) {
2842
-
2843
2944
  if(!Validator.isObservable($condition)) {
2844
2945
  throw new NativeDocumentError("Toggle : condition must be an Observable");
2845
2946
  }
@@ -3198,7 +3299,8 @@ var NativeDocument = (function (exports) {
3198
3299
  Video: Video,
3199
3300
  Wbr: Wbr,
3200
3301
  WeekInput: WeekInput,
3201
- When: When
3302
+ When: When,
3303
+ createPortal: createPortal
3202
3304
  });
3203
3305
 
3204
3306
  const RouteParamPatterns = {
@@ -3222,6 +3324,7 @@ var NativeDocument = (function (exports) {
3222
3324
  const $middlewares = $options.middlewares || [];
3223
3325
  const $shouldRebuild = $options.shouldRebuild || false;
3224
3326
  const $paramsValidators = $options.with || {};
3327
+ const $layout = $options.layout || null;
3225
3328
 
3226
3329
  const $params = {};
3227
3330
  const $paramsNames = [];
@@ -3266,6 +3369,7 @@ var NativeDocument = (function (exports) {
3266
3369
  this.middlewares = () => $middlewares;
3267
3370
  this.shouldRebuild = () => $shouldRebuild;
3268
3371
  this.path = () => $path;
3372
+ this.layout = () => $layout;
3269
3373
 
3270
3374
  /**
3271
3375
  *
@@ -3358,6 +3462,9 @@ var NativeDocument = (function (exports) {
3358
3462
  });
3359
3463
  name && fullName.push(name);
3360
3464
  return fullName.join('.');
3465
+ },
3466
+ layout: ($groupTree) => {
3467
+ return $groupTree[$groupTree.length-1]?.options?.layout || null;
3361
3468
  }
3362
3469
  };
3363
3470
 
@@ -3587,8 +3694,13 @@ var NativeDocument = (function (exports) {
3587
3694
 
3588
3695
  const $cache = new Map();
3589
3696
 
3590
- const updateContainer = function(node) {
3697
+ const updateContainer = function(node, route) {
3591
3698
  container.innerHTML = '';
3699
+ const layout = route.layout();
3700
+ if(layout) {
3701
+ container.appendChild(layout(node));
3702
+ return;
3703
+ }
3592
3704
  container.appendChild(node);
3593
3705
  };
3594
3706
 
@@ -3599,13 +3711,13 @@ var NativeDocument = (function (exports) {
3599
3711
  const { route, params, query, path } = state;
3600
3712
  if($cache.has(path)) {
3601
3713
  const cacheNode = $cache.get(path);
3602
- updateContainer(cacheNode);
3714
+ updateContainer(cacheNode, route);
3603
3715
  return;
3604
3716
  }
3605
3717
  const Component = route.component();
3606
3718
  const node = Component({ params, query });
3607
3719
  $cache.set(path, node);
3608
- updateContainer(node);
3720
+ updateContainer(node, route);
3609
3721
  };
3610
3722
 
3611
3723
  router.subscribe(handleCurrentRouterState);
@@ -3659,7 +3771,7 @@ var NativeDocument = (function (exports) {
3659
3771
  *
3660
3772
  * @param {string} path
3661
3773
  * @param {Function} component
3662
- * @param {{name:?string, middlewares:Function[], shouldRebuild:Boolean, with: Object }} options
3774
+ * @param {{name:?string, middlewares:Function[], shouldRebuild:Boolean, with: Object, layout: Function }} options
3663
3775
  * @returns {this}
3664
3776
  */
3665
3777
  this.add = function(path, component, options) {
@@ -3667,6 +3779,7 @@ var NativeDocument = (function (exports) {
3667
3779
  ...options,
3668
3780
  middlewares: RouteGroupHelper.fullMiddlewares($groupTree, options?.middlewares || []),
3669
3781
  name: options?.name ? RouteGroupHelper.fullName($groupTree, options.name) : null,
3782
+ layout: options?.layout || RouteGroupHelper.layout($groupTree)
3670
3783
  });
3671
3784
  $routes.push(route);
3672
3785
  if(route.name()) {
@@ -3890,6 +4003,7 @@ var NativeDocument = (function (exports) {
3890
4003
  exports.NDElement = NDElement;
3891
4004
  exports.Observable = Observable;
3892
4005
  exports.PluginsManager = PluginsManager;
4006
+ exports.SingletonView = SingletonView;
3893
4007
  exports.Store = Store;
3894
4008
  exports.TemplateCloner = TemplateCloner;
3895
4009
  exports.Validator = validator;
@@ -3900,6 +4014,7 @@ var NativeDocument = (function (exports) {
3900
4014
  exports.normalizeComponentArgs = normalizeComponentArgs;
3901
4015
  exports.router = router;
3902
4016
  exports.useCache = useCache;
4017
+ exports.useSingleton = useSingleton;
3903
4018
 
3904
4019
  return exports;
3905
4020