native-document 1.0.40 → 1.0.42

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,20 @@ 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.toBool = function() {
461
+ return !!this.$currentValue;
462
+ };
463
+
464
+ ObservableItem.prototype.toggle = function() {
465
+ this.set(!this.$currentValue);
466
+ };
453
467
 
454
468
  /**
455
469
  *
@@ -639,8 +653,6 @@ var NativeDocument = (function (exports) {
639
653
  }
640
654
  NDElement.prototype.__$isNDElement = true;
641
655
 
642
-
643
-
644
656
  NDElement.prototype.valueOf = function() {
645
657
  return this.$element;
646
658
  };
@@ -845,6 +857,7 @@ var NativeDocument = (function (exports) {
845
857
  const COMMON_NODE_TYPES = {
846
858
  ELEMENT: 1,
847
859
  TEXT: 3,
860
+ COMMENT: 8,
848
861
  DOCUMENT_FRAGMENT: 11
849
862
  };
850
863
 
@@ -855,6 +868,9 @@ var NativeDocument = (function (exports) {
855
868
  isProxy(value) {
856
869
  return value?.__isProxy__
857
870
  },
871
+ isAnchor(value) {
872
+ return value?.__Anchor__
873
+ },
858
874
  isObservableChecker(value) {
859
875
  return value?.__$isObservableChecker || value instanceof ObservableChecker;
860
876
  },
@@ -886,7 +902,8 @@ var NativeDocument = (function (exports) {
886
902
  return value && (
887
903
  value.nodeType === COMMON_NODE_TYPES.ELEMENT ||
888
904
  value.nodeType === COMMON_NODE_TYPES.TEXT ||
889
- value.nodeType === COMMON_NODE_TYPES.DOCUMENT_FRAGMENT
905
+ value.nodeType === COMMON_NODE_TYPES.DOCUMENT_FRAGMENT ||
906
+ value.nodeType === COMMON_NODE_TYPES.COMMENT
890
907
  );
891
908
  },
892
909
  isFragment(value) {
@@ -980,6 +997,7 @@ var NativeDocument = (function (exports) {
980
997
 
981
998
  function Anchor(name, isUniqueChild = false) {
982
999
  const element = document.createDocumentFragment();
1000
+ element.__Anchor__ = true;
983
1001
 
984
1002
  const anchorStart = document.createComment('Anchor Start : '+name);
985
1003
  const anchorEnd = document.createComment('/ Anchor End '+name);
@@ -993,16 +1011,16 @@ var NativeDocument = (function (exports) {
993
1011
  const isParentUniqueChild = (parent) => (isUniqueChild || (parent.firstChild === anchorStart && parent.lastChild === anchorEnd));
994
1012
 
995
1013
  const insertBefore = function(parent, child, target) {
996
- const element = Validator.isElement(child) ? child : ElementCreator.getChild(child);
1014
+ const childElement = Validator.isElement(child) ? child : ElementCreator.getChild(child);
997
1015
  if(parent === element) {
998
- parent.nativeInsertBefore(element, target);
1016
+ parent.nativeInsertBefore(childElement, target);
999
1017
  return;
1000
1018
  }
1001
1019
  if(isParentUniqueChild(parent) && target === anchorEnd) {
1002
- parent.append(element, target);
1020
+ parent.append(childElement, target);
1003
1021
  return;
1004
1022
  }
1005
- parent.insertBefore(element, target);
1023
+ parent.insertBefore(childElement, target);
1006
1024
  };
1007
1025
 
1008
1026
  element.appendElement = function(child, before = null) {
@@ -1024,6 +1042,9 @@ var NativeDocument = (function (exports) {
1024
1042
  before = before ?? anchorEnd;
1025
1043
  insertBefore(parent, child, before);
1026
1044
  };
1045
+ element.append = function(...args ) {
1046
+ return element.appendChild(args);
1047
+ };
1027
1048
 
1028
1049
  element.removeChildren = function() {
1029
1050
  const parent = anchorEnd.parentNode;
@@ -1049,10 +1070,6 @@ var NativeDocument = (function (exports) {
1049
1070
  if(parent === element) {
1050
1071
  return;
1051
1072
  }
1052
- if(isParentUniqueChild(parent)) {
1053
- parent.replaceChildren(anchorStart, anchorEnd);
1054
- return;
1055
- }
1056
1073
  let itemToRemove = anchorStart.nextSibling, tempItem;
1057
1074
  while(itemToRemove && itemToRemove !== anchorEnd) {
1058
1075
  tempItem = itemToRemove.nextSibling;
@@ -1084,9 +1101,6 @@ var NativeDocument = (function (exports) {
1084
1101
  element.appendChild(child, anchor);
1085
1102
  };
1086
1103
 
1087
- element.clear = function() {
1088
- element.remove();
1089
- };
1090
1104
 
1091
1105
  element.endElement = function() {
1092
1106
  return anchorEnd;
@@ -1095,6 +1109,11 @@ var NativeDocument = (function (exports) {
1095
1109
  element.startElement = function() {
1096
1110
  return anchorStart;
1097
1111
  };
1112
+ element.restore = function() {
1113
+ element.appendChild(element);
1114
+ };
1115
+ element.clear = element.remove;
1116
+ element.detach = element.remove;
1098
1117
 
1099
1118
  element.getByIndex = function(index) {
1100
1119
  let currentNode = anchorStart;
@@ -1109,6 +1128,19 @@ var NativeDocument = (function (exports) {
1109
1128
 
1110
1129
  return element;
1111
1130
  }
1131
+ /**
1132
+ *
1133
+ * @param {HTMLElement|DocumentFragment|Text|String|Array} children
1134
+ * @param {{ parent?: HTMLElement, name?: String}} configs
1135
+ * @returns {DocumentFragment}
1136
+ */
1137
+ function createPortal(children, { parent, name = 'unnamed' } = {}) {
1138
+ const anchor = Anchor('Portal '+name);
1139
+ anchor.appendChild(ElementCreator.getChild(children));
1140
+
1141
+ (parent || document.body).appendChild(anchor);
1142
+ return anchor;
1143
+ }
1112
1144
 
1113
1145
  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
1146
 
@@ -1830,6 +1862,7 @@ var NativeDocument = (function (exports) {
1830
1862
  this.attach = (fn) => {
1831
1863
  return createBinding(fn, 'attach');
1832
1864
  };
1865
+
1833
1866
  }
1834
1867
 
1835
1868
  function useCache(fn) {
@@ -1852,6 +1885,52 @@ var NativeDocument = (function (exports) {
1852
1885
  };
1853
1886
  }
1854
1887
 
1888
+ function SingletonView($viewCreator) {
1889
+ let $cacheNode = null;
1890
+ let $components = null;
1891
+
1892
+ this.render = (data) => {
1893
+ if(!$cacheNode) {
1894
+ $cacheNode = $viewCreator(this);
1895
+ }
1896
+ if(!$components) {
1897
+ return $cacheNode;
1898
+ }
1899
+ for(const index in $components) {
1900
+ const updater = $components[index];
1901
+ updater(...data);
1902
+ }
1903
+ return $cacheNode;
1904
+ };
1905
+
1906
+ this.createSection = (name, fn) => {
1907
+ $components = $components || {};
1908
+ const anchor = new Anchor('Component '+name);
1909
+
1910
+ $components[name] = function(...args) {
1911
+ anchor.removeChildren();
1912
+ if(!fn) {
1913
+ anchor.append(args);
1914
+ return;
1915
+ }
1916
+ anchor.appendChild(fn(...args));
1917
+ };
1918
+ return anchor;
1919
+ };
1920
+ }
1921
+
1922
+
1923
+ function useSingleton(fn) {
1924
+ let $cache = null;
1925
+
1926
+ return function(...args) {
1927
+ if(!$cache) {
1928
+ $cache = new SingletonView(fn);
1929
+ }
1930
+ return $cache.render(args);
1931
+ };
1932
+ }
1933
+
1855
1934
  Function.prototype.args = function(...args) {
1856
1935
  return exports.withValidation(this, args);
1857
1936
  };
@@ -1988,6 +2067,12 @@ var NativeDocument = (function (exports) {
1988
2067
  observer.populateAndRender = function(iteration, callback) {
1989
2068
  observer.trigger({ action: 'populate', args: [observer.val(), iteration, callback] });
1990
2069
  };
2070
+
2071
+ observer.removeItem = function(item) {
2072
+ const indexOfItem = observer.val().indexOf(item);
2073
+ return observer.remove(indexOfItem);
2074
+ };
2075
+
1991
2076
  observer.remove = function(index) {
1992
2077
  const deleted = observer.val().splice(index, 1);
1993
2078
  if(deleted.length === 0) {
@@ -2021,6 +2106,24 @@ var NativeDocument = (function (exports) {
2021
2106
  return observer.val().length;
2022
2107
  };
2023
2108
 
2109
+ /**
2110
+ *
2111
+ * @param {Function} condition
2112
+ * @returns {number}
2113
+ */
2114
+ observer.count = (condition) => {
2115
+ let count = 0;
2116
+ observer.val().forEach((item, index) => {
2117
+ if(condition(item, index)) {
2118
+ count++;
2119
+ }
2120
+ });
2121
+ return count;
2122
+ };
2123
+ observer.isEmpty = function() {
2124
+ return observer.val().length === 0;
2125
+ };
2126
+
2024
2127
  const overrideMethods = ['map', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat', 'includes', 'indexOf'];
2025
2128
  overrideMethods.forEach((method) => {
2026
2129
  observer[method] = (...args) => {
@@ -2060,6 +2163,10 @@ var NativeDocument = (function (exports) {
2060
2163
  const data = {};
2061
2164
  for(const key in initialValue) {
2062
2165
  const itemValue = initialValue[key];
2166
+ if(Array.isArray(itemValue)) {
2167
+ data[key] = Observable.array(itemValue);
2168
+ continue;
2169
+ }
2063
2170
  data[key] = Observable(itemValue);
2064
2171
  }
2065
2172
 
@@ -2717,7 +2824,7 @@ var NativeDocument = (function (exports) {
2717
2824
  *
2718
2825
  * @param {ObservableItem|ObservableChecker} condition
2719
2826
  * @param {*} child
2720
- * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} comment
2827
+ * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} configs
2721
2828
  * @returns {DocumentFragment}
2722
2829
  */
2723
2830
  const ShowIf = function(condition, child, { comment = null, shouldKeepInCache = true} = {}) {
@@ -2733,7 +2840,7 @@ var NativeDocument = (function (exports) {
2733
2840
  }
2734
2841
  childElement = ElementCreator.getChild(child);
2735
2842
  if(Validator.isFragment(childElement)) {
2736
- childElement = Array.from(childElement.children);
2843
+ childElement = Array.from(childElement.childNodes);
2737
2844
  }
2738
2845
  return childElement;
2739
2846
  };
@@ -2758,15 +2865,14 @@ var NativeDocument = (function (exports) {
2758
2865
  * Hide the element if the condition is true
2759
2866
  * @param {ObservableItem|ObservableChecker} condition
2760
2867
  * @param child
2761
- * @param comment
2868
+ * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} configs
2762
2869
  * @returns {DocumentFragment}
2763
2870
  */
2764
- const HideIf = function(condition, child, comment) {
2765
-
2871
+ const HideIf = function(condition, child, configs) {
2766
2872
  const hideCondition = Observable(!condition.val());
2767
2873
  condition.subscribe(value => hideCondition.set(!value));
2768
2874
 
2769
- return ShowIf(hideCondition, child, comment);
2875
+ return ShowIf(hideCondition, child, configs);
2770
2876
  };
2771
2877
 
2772
2878
  /**
@@ -2774,11 +2880,11 @@ var NativeDocument = (function (exports) {
2774
2880
  *
2775
2881
  * @param {ObservableItem|ObservableChecker} condition
2776
2882
  * @param {*} child
2777
- * @param {string|null} comment
2883
+ * @param {{comment?: string|null, shouldKeepInCache?: Boolean}} configs
2778
2884
  * @returns {DocumentFragment}
2779
2885
  */
2780
- const HideIfNot = function(condition, child, comment) {
2781
- return ShowIf(condition, child, comment);
2886
+ const HideIfNot = function(condition, child, configs) {
2887
+ return ShowIf(condition, child, configs);
2782
2888
  };
2783
2889
 
2784
2890
  /**
@@ -2794,7 +2900,7 @@ var NativeDocument = (function (exports) {
2794
2900
  throw new NativeDocumentError("Toggle : condition must be an Observable");
2795
2901
  }
2796
2902
 
2797
- const anchor = new Anchor();
2903
+ const anchor = new Anchor('Match');
2798
2904
  const cache = new Map();
2799
2905
 
2800
2906
  const getItem = function(key) {
@@ -2839,7 +2945,6 @@ var NativeDocument = (function (exports) {
2839
2945
  * @returns {DocumentFragment}
2840
2946
  */
2841
2947
  const Switch = function ($condition, onTrue, onFalse) {
2842
-
2843
2948
  if(!Validator.isObservable($condition)) {
2844
2949
  throw new NativeDocumentError("Toggle : condition must be an Observable");
2845
2950
  }
@@ -3003,11 +3108,13 @@ var NativeDocument = (function (exports) {
3003
3108
  * @returns {Image}
3004
3109
  */
3005
3110
  const AsyncImg = function(src, defaultImage, attributes, callback) {
3006
- const image = Img(defaultImage || src, attributes);
3111
+ const defaultSrc = Validator.isObservable(src) ? src.val() : src;
3112
+ const image = Img(defaultImage || defaultSrc, attributes);
3007
3113
  const img = new Image();
3114
+
3008
3115
  img.onload = () => {
3009
3116
  Validator.isFunction(callback) && callback(null, image);
3010
- image.src = src;
3117
+ image.src = Validator.isObservable(src) ? src.val() : src;
3011
3118
  };
3012
3119
  img.onerror = () => {
3013
3120
  Validator.isFunction(callback) && callback(new NativeDocumentError('Image not found'));
@@ -3017,7 +3124,7 @@ var NativeDocument = (function (exports) {
3017
3124
  img.src = newSrc;
3018
3125
  });
3019
3126
  }
3020
- img.src = src;
3127
+ img.src = defaultSrc;
3021
3128
  return image;
3022
3129
  };
3023
3130
 
@@ -3198,7 +3305,8 @@ var NativeDocument = (function (exports) {
3198
3305
  Video: Video,
3199
3306
  Wbr: Wbr,
3200
3307
  WeekInput: WeekInput,
3201
- When: When
3308
+ When: When,
3309
+ createPortal: createPortal
3202
3310
  });
3203
3311
 
3204
3312
  const RouteParamPatterns = {
@@ -3214,7 +3322,7 @@ var NativeDocument = (function (exports) {
3214
3322
  */
3215
3323
  function Route($path, $component, $options = {}) {
3216
3324
 
3217
- $path = '/'+trim($path, '/');
3325
+ $path = '/'+trim($path, '/').replace(/\/+/, '/');
3218
3326
 
3219
3327
  let $pattern = null;
3220
3328
  let $name = $options.name || null;
@@ -3222,6 +3330,7 @@ var NativeDocument = (function (exports) {
3222
3330
  const $middlewares = $options.middlewares || [];
3223
3331
  const $shouldRebuild = $options.shouldRebuild || false;
3224
3332
  const $paramsValidators = $options.with || {};
3333
+ const $layout = $options.layout || null;
3225
3334
 
3226
3335
  const $params = {};
3227
3336
  const $paramsNames = [];
@@ -3266,6 +3375,7 @@ var NativeDocument = (function (exports) {
3266
3375
  this.middlewares = () => $middlewares;
3267
3376
  this.shouldRebuild = () => $shouldRebuild;
3268
3377
  this.path = () => $path;
3378
+ this.layout = () => $layout;
3269
3379
 
3270
3380
  /**
3271
3381
  *
@@ -3358,6 +3468,14 @@ var NativeDocument = (function (exports) {
3358
3468
  });
3359
3469
  name && fullName.push(name);
3360
3470
  return fullName.join('.');
3471
+ },
3472
+ layout: ($groupTree) => {
3473
+ for(let i = $groupTree.length - 1; i >= 0; i--) {
3474
+ if($groupTree[i]?.options?.layout) {
3475
+ return $groupTree[i].options.layout;
3476
+ }
3477
+ }
3478
+ return null;
3361
3479
  }
3362
3480
  };
3363
3481
 
@@ -3587,8 +3705,13 @@ var NativeDocument = (function (exports) {
3587
3705
 
3588
3706
  const $cache = new Map();
3589
3707
 
3590
- const updateContainer = function(node) {
3708
+ const updateContainer = function(node, route) {
3591
3709
  container.innerHTML = '';
3710
+ const layout = route.layout();
3711
+ if(layout) {
3712
+ container.appendChild(layout(node));
3713
+ return;
3714
+ }
3592
3715
  container.appendChild(node);
3593
3716
  };
3594
3717
 
@@ -3599,13 +3722,13 @@ var NativeDocument = (function (exports) {
3599
3722
  const { route, params, query, path } = state;
3600
3723
  if($cache.has(path)) {
3601
3724
  const cacheNode = $cache.get(path);
3602
- updateContainer(cacheNode);
3725
+ updateContainer(cacheNode, route);
3603
3726
  return;
3604
3727
  }
3605
3728
  const Component = route.component();
3606
3729
  const node = Component({ params, query });
3607
3730
  $cache.set(path, node);
3608
- updateContainer(node);
3731
+ updateContainer(node, route);
3609
3732
  };
3610
3733
 
3611
3734
  router.subscribe(handleCurrentRouterState);
@@ -3659,7 +3782,7 @@ var NativeDocument = (function (exports) {
3659
3782
  *
3660
3783
  * @param {string} path
3661
3784
  * @param {Function} component
3662
- * @param {{name:?string, middlewares:Function[], shouldRebuild:Boolean, with: Object }} options
3785
+ * @param {{name:?string, middlewares:Function[], shouldRebuild:Boolean, with: Object, layout: Function }} options
3663
3786
  * @returns {this}
3664
3787
  */
3665
3788
  this.add = function(path, component, options) {
@@ -3667,6 +3790,7 @@ var NativeDocument = (function (exports) {
3667
3790
  ...options,
3668
3791
  middlewares: RouteGroupHelper.fullMiddlewares($groupTree, options?.middlewares || []),
3669
3792
  name: options?.name ? RouteGroupHelper.fullName($groupTree, options.name) : null,
3793
+ layout: options?.layout || RouteGroupHelper.layout($groupTree)
3670
3794
  });
3671
3795
  $routes.push(route);
3672
3796
  if(route.name()) {
@@ -3890,6 +4014,7 @@ var NativeDocument = (function (exports) {
3890
4014
  exports.NDElement = NDElement;
3891
4015
  exports.Observable = Observable;
3892
4016
  exports.PluginsManager = PluginsManager;
4017
+ exports.SingletonView = SingletonView;
3893
4018
  exports.Store = Store;
3894
4019
  exports.TemplateCloner = TemplateCloner;
3895
4020
  exports.Validator = validator;
@@ -3900,6 +4025,7 @@ var NativeDocument = (function (exports) {
3900
4025
  exports.normalizeComponentArgs = normalizeComponentArgs;
3901
4026
  exports.router = router;
3902
4027
  exports.useCache = useCache;
4028
+ exports.useSingleton = useSingleton;
3903
4029
 
3904
4030
  return exports;
3905
4031