native-document 1.0.39 → 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) {
@@ -973,8 +986,14 @@ var NativeDocument = (function (exports) {
973
986
  };
974
987
  }
975
988
 
989
+ var validator = /*#__PURE__*/Object.freeze({
990
+ __proto__: null,
991
+ default: Validator
992
+ });
993
+
976
994
  function Anchor(name, isUniqueChild = false) {
977
995
  const element = document.createDocumentFragment();
996
+ element.__Anchor__ = true;
978
997
 
979
998
  const anchorStart = document.createComment('Anchor Start : '+name);
980
999
  const anchorEnd = document.createComment('/ Anchor End '+name);
@@ -988,16 +1007,16 @@ var NativeDocument = (function (exports) {
988
1007
  const isParentUniqueChild = (parent) => (isUniqueChild || (parent.firstChild === anchorStart && parent.lastChild === anchorEnd));
989
1008
 
990
1009
  const insertBefore = function(parent, child, target) {
991
- const element = Validator.isElement(child) ? child : ElementCreator.getChild(child);
1010
+ const childElement = Validator.isElement(child) ? child : ElementCreator.getChild(child);
992
1011
  if(parent === element) {
993
- parent.nativeInsertBefore(element, target);
1012
+ parent.nativeInsertBefore(childElement, target);
994
1013
  return;
995
1014
  }
996
1015
  if(isParentUniqueChild(parent) && target === anchorEnd) {
997
- parent.append(element, target);
1016
+ parent.append(childElement, target);
998
1017
  return;
999
1018
  }
1000
- parent.insertBefore(element, target);
1019
+ parent.insertBefore(childElement, target);
1001
1020
  };
1002
1021
 
1003
1022
  element.appendElement = function(child, before = null) {
@@ -1019,6 +1038,9 @@ var NativeDocument = (function (exports) {
1019
1038
  before = before ?? anchorEnd;
1020
1039
  insertBefore(parent, child, before);
1021
1040
  };
1041
+ element.append = function(...args ) {
1042
+ return element.appendChild(args);
1043
+ };
1022
1044
 
1023
1045
  element.removeChildren = function() {
1024
1046
  const parent = anchorEnd.parentNode;
@@ -1044,10 +1066,6 @@ var NativeDocument = (function (exports) {
1044
1066
  if(parent === element) {
1045
1067
  return;
1046
1068
  }
1047
- if(isParentUniqueChild(parent)) {
1048
- parent.replaceChildren(anchorStart, anchorEnd);
1049
- return;
1050
- }
1051
1069
  let itemToRemove = anchorStart.nextSibling, tempItem;
1052
1070
  while(itemToRemove && itemToRemove !== anchorEnd) {
1053
1071
  tempItem = itemToRemove.nextSibling;
@@ -1079,9 +1097,6 @@ var NativeDocument = (function (exports) {
1079
1097
  element.appendChild(child, anchor);
1080
1098
  };
1081
1099
 
1082
- element.clear = function() {
1083
- element.remove();
1084
- };
1085
1100
 
1086
1101
  element.endElement = function() {
1087
1102
  return anchorEnd;
@@ -1090,6 +1105,11 @@ var NativeDocument = (function (exports) {
1090
1105
  element.startElement = function() {
1091
1106
  return anchorStart;
1092
1107
  };
1108
+ element.restore = function() {
1109
+ element.appendChild(element);
1110
+ };
1111
+ element.clear = element.remove;
1112
+ element.detach = element.remove;
1093
1113
 
1094
1114
  element.getByIndex = function(index) {
1095
1115
  let currentNode = anchorStart;
@@ -1104,6 +1124,19 @@ var NativeDocument = (function (exports) {
1104
1124
 
1105
1125
  return element;
1106
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
+ }
1107
1140
 
1108
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'];
1109
1142
 
@@ -1281,6 +1314,9 @@ var NativeDocument = (function (exports) {
1281
1314
  for(let key in attributes) {
1282
1315
  const attributeName = key.toLowerCase();
1283
1316
  let value = attributes[attributeName];
1317
+ if(value === null || value === undefined) {
1318
+ continue;
1319
+ }
1284
1320
  if(Validator.isString(value)) {
1285
1321
  value = value.resolveObservableTemplate ? value.resolveObservableTemplate() : value;
1286
1322
  if(Validator.isString(value)) {
@@ -1822,6 +1858,7 @@ var NativeDocument = (function (exports) {
1822
1858
  this.attach = (fn) => {
1823
1859
  return createBinding(fn, 'attach');
1824
1860
  };
1861
+
1825
1862
  }
1826
1863
 
1827
1864
  function useCache(fn) {
@@ -1844,6 +1881,52 @@ var NativeDocument = (function (exports) {
1844
1881
  };
1845
1882
  }
1846
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
+
1847
1930
  Function.prototype.args = function(...args) {
1848
1931
  return exports.withValidation(this, args);
1849
1932
  };
@@ -1980,6 +2063,12 @@ var NativeDocument = (function (exports) {
1980
2063
  observer.populateAndRender = function(iteration, callback) {
1981
2064
  observer.trigger({ action: 'populate', args: [observer.val(), iteration, callback] });
1982
2065
  };
2066
+
2067
+ observer.removeItem = function(item) {
2068
+ const indexOfItem = observer.val().indexOf(item);
2069
+ return observer.remove(indexOfItem);
2070
+ };
2071
+
1983
2072
  observer.remove = function(index) {
1984
2073
  const deleted = observer.val().splice(index, 1);
1985
2074
  if(deleted.length === 0) {
@@ -2013,6 +2102,24 @@ var NativeDocument = (function (exports) {
2013
2102
  return observer.val().length;
2014
2103
  };
2015
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
+
2016
2123
  const overrideMethods = ['map', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat', 'includes', 'indexOf'];
2017
2124
  overrideMethods.forEach((method) => {
2018
2125
  observer[method] = (...args) => {
@@ -2052,11 +2159,7 @@ var NativeDocument = (function (exports) {
2052
2159
  const data = {};
2053
2160
  for(const key in initialValue) {
2054
2161
  const itemValue = initialValue[key];
2055
- if(Validator.isJson(itemValue)) {
2056
- data[key] = Observable.init(itemValue);
2057
- continue;
2058
- }
2059
- else if(Validator.isArray(itemValue)) {
2162
+ if(Array.isArray(itemValue)) {
2060
2163
  data[key] = Observable.array(itemValue);
2061
2164
  continue;
2062
2165
  }
@@ -2078,7 +2181,7 @@ var NativeDocument = (function (exports) {
2078
2181
  return result;
2079
2182
  };
2080
2183
  const $clone = function() {
2081
-
2184
+ return Observable.init($val());
2082
2185
  };
2083
2186
  const $updateWith = function(values) {
2084
2187
  Observable.update(proxy, values);
@@ -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,8 +4003,10 @@ 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;
4009
+ exports.Validator = validator;
3895
4010
  exports.classPropertyAccumulator = classPropertyAccumulator;
3896
4011
  exports.createTextNode = createTextNode;
3897
4012
  exports.cssPropertyAccumulator = cssPropertyAccumulator;
@@ -3899,6 +4014,7 @@ var NativeDocument = (function (exports) {
3899
4014
  exports.normalizeComponentArgs = normalizeComponentArgs;
3900
4015
  exports.router = router;
3901
4016
  exports.useCache = useCache;
4017
+ exports.useSingleton = useSingleton;
3902
4018
 
3903
4019
  return exports;
3904
4020