native-document 1.0.80 → 1.0.82

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.
@@ -141,79 +141,85 @@ var NativeDocument = (function (exports) {
141
141
  return this.observable.cleanup();
142
142
  };
143
143
 
144
- const PluginsManager = (function() {
144
+ let PluginsManager$1 = null;
145
145
 
146
- const $plugins = new Map();
147
- const $pluginByEvents = new Map();
146
+ {
147
+ PluginsManager$1 = (function() {
148
148
 
149
- return {
150
- list() {
151
- return $pluginByEvents;
152
- },
153
- add(plugin, name){
154
- if (!plugin || typeof plugin !== 'object') {
155
- throw new Error(`Plugin ${name} must be an object`);
156
- }
157
- name = name || plugin.name;
158
- if (!name || typeof name !== 'string') {
159
- throw new Error(`Please, provide a valid plugin name`);
160
- }
161
- if($plugins.has(name)) {
162
- return;
163
- }
149
+ const $plugins = new Map();
150
+ const $pluginByEvents = new Map();
164
151
 
165
- plugin.$name = name;
166
- $plugins.set(name ,plugin);
167
- if(typeof plugin?.init === 'function') {
168
- plugin.init();
169
- }
170
- for(const methodName in plugin) {
171
- if(/^on[A-Z]/.test(methodName)) {
172
- const eventName = methodName.replace(/^on/, '');
173
- if(!$pluginByEvents.has(eventName)) {
174
- $pluginByEvents.set(eventName, new Set());
152
+ return {
153
+ list() {
154
+ return $pluginByEvents;
155
+ },
156
+ add(plugin, name){
157
+ if (!plugin || typeof plugin !== 'object') {
158
+ throw new Error(`Plugin ${name} must be an object`);
159
+ }
160
+ name = name || plugin.name;
161
+ if (!name || typeof name !== 'string') {
162
+ throw new Error(`Please, provide a valid plugin name`);
163
+ }
164
+ if($plugins.has(name)) {
165
+ return;
166
+ }
167
+
168
+ plugin.$name = name;
169
+ $plugins.set(name ,plugin);
170
+ if(typeof plugin?.init === 'function') {
171
+ plugin.init();
172
+ }
173
+ for(const methodName in plugin) {
174
+ if(/^on[A-Z]/.test(methodName)) {
175
+ const eventName = methodName.replace(/^on/, '');
176
+ if(!$pluginByEvents.has(eventName)) {
177
+ $pluginByEvents.set(eventName, new Set());
178
+ }
179
+ $pluginByEvents.get(eventName).add(plugin);
175
180
  }
176
- $pluginByEvents.get(eventName).add(plugin);
177
181
  }
178
- }
179
- },
180
- remove(pluginName){
181
- if(!$plugins.has(pluginName)) {
182
- return;
183
- }
184
- const plugin = $plugins.get(pluginName);
185
- if(typeof plugin.cleanup === 'function') {
186
- plugin.cleanup();
187
- }
188
- for(const [name, sets] of $pluginByEvents.entries() ) {
189
- if(sets.has(plugin)) {
190
- sets.delete(plugin);
182
+ },
183
+ remove(pluginName){
184
+ if(!$plugins.has(pluginName)) {
185
+ return;
191
186
  }
192
- if(sets.size === 0) {
193
- $pluginByEvents.delete(name);
187
+ const plugin = $plugins.get(pluginName);
188
+ if(typeof plugin.cleanup === 'function') {
189
+ plugin.cleanup();
194
190
  }
195
- }
196
- $plugins.delete(pluginName);
197
- },
198
- emit(eventName, ...data) {
199
- if(!$pluginByEvents.has(eventName)) {
200
- return;
201
- }
202
- const plugins = $pluginByEvents.get(eventName);
203
-
204
- for(const plugin of plugins) {
205
- const callback = plugin['on'+eventName];
206
- if(typeof callback === 'function') {
207
- try{
208
- callback.call(plugin, ...data);
209
- } catch (error) {
210
- DebugManager$1.error('Plugin Manager', `Error in plugin ${plugin.$name} for event ${eventName}`, error);
191
+ for(const [name, sets] of $pluginByEvents.entries() ) {
192
+ if(sets.has(plugin)) {
193
+ sets.delete(plugin);
194
+ }
195
+ if(sets.size === 0) {
196
+ $pluginByEvents.delete(name);
197
+ }
198
+ }
199
+ $plugins.delete(pluginName);
200
+ },
201
+ emit(eventName, ...data) {
202
+ if(!$pluginByEvents.has(eventName)) {
203
+ return;
204
+ }
205
+ const plugins = $pluginByEvents.get(eventName);
206
+
207
+ for(const plugin of plugins) {
208
+ const callback = plugin['on'+eventName];
209
+ if(typeof callback === 'function') {
210
+ try{
211
+ callback.call(plugin, ...data);
212
+ } catch (error) {
213
+ DebugManager$1.error('Plugin Manager', `Error in plugin ${plugin.$name} for event ${eventName}`, error);
214
+ }
211
215
  }
212
216
  }
213
217
  }
214
- }
215
- };
216
- }());
218
+ };
219
+ }());
220
+ }
221
+
222
+ var PluginsManager = PluginsManager$1;
217
223
 
218
224
  const ObservableWhen = function(observer, value) {
219
225
  this.$target = value;
@@ -322,6 +328,7 @@ var NativeDocument = (function (exports) {
322
328
  this.$currentValue = value;
323
329
  this.$isCleanedUp = false;
324
330
 
331
+ this.$firstListener = null;
325
332
  this.$listeners = null;
326
333
  this.$watchers = null;
327
334
 
@@ -333,8 +340,9 @@ var NativeDocument = (function (exports) {
333
340
  this.$initialValue = Validator.isObject(value) ? deepClone(value) : value;
334
341
  }
335
342
  }
336
-
337
- PluginsManager.emit('CreateObservable', this);
343
+ {
344
+ PluginsManager.emit('CreateObservable', this);
345
+ }
338
346
  }
339
347
 
340
348
  Object.defineProperty(ObservableItem.prototype, '$value', {
@@ -357,7 +365,7 @@ var NativeDocument = (function (exports) {
357
365
  };
358
366
 
359
367
  ObservableItem.prototype.triggerFirstListener = function(operations) {
360
- this.$listeners[0](this.$currentValue, this.$previousValue, operations || {});
368
+ this.$firstListener(this.$currentValue, this.$previousValue, operations || {});
361
369
  };
362
370
 
363
371
  ObservableItem.prototype.triggerListeners = function(operations) {
@@ -406,22 +414,29 @@ var NativeDocument = (function (exports) {
406
414
  };
407
415
 
408
416
  ObservableItem.prototype.triggerAll = function(operations) {
409
- this.triggerListeners(operations);
410
417
  this.triggerWatchers();
418
+ this.triggerListeners(operations);
411
419
  };
412
420
 
413
421
  ObservableItem.prototype.triggerWatchersAndFirstListener = function(operations) {
414
- this.triggerListeners(operations);
415
422
  this.triggerWatchers();
423
+ this.triggerListeners(operations);
416
424
  };
417
425
 
418
426
  ObservableItem.prototype.assocTrigger = function() {
427
+ this.$firstListener = null;
419
428
  if(this.$watchers?.size && this.$listeners?.length) {
420
429
  this.trigger = (this.$listeners.length === 1) ? this.triggerWatchersAndFirstListener : this.triggerAll;
421
430
  return;
422
431
  }
423
432
  if(this.$listeners?.length) {
424
- this.trigger = (this.$listeners.length === 1) ? this.triggerFirstListener : this.triggerListeners;
433
+ if(this.$listeners.length === 1) {
434
+ this.$firstListener = this.$listeners[0];
435
+ this.trigger = this.triggerFirstListener;
436
+ }
437
+ else {
438
+ this.trigger = this.triggerListeners;
439
+ }
425
440
  return;
426
441
  }
427
442
  if(this.$watchers?.size) {
@@ -452,10 +467,14 @@ var NativeDocument = (function (exports) {
452
467
  }
453
468
  this.$previousValue = this.$currentValue;
454
469
  this.$currentValue = newValue;
455
- PluginsManager.emit('ObservableBeforeChange', this);
470
+ {
471
+ PluginsManager.emit('ObservableBeforeChange', this);
472
+ }
456
473
  this.trigger();
457
474
  this.$previousValue = null;
458
- PluginsManager.emit('ObservableAfterChange', this);
475
+ {
476
+ PluginsManager.emit('ObservableAfterChange', this);
477
+ }
459
478
  };
460
479
 
461
480
  ObservableItem.prototype.val = function() {
@@ -515,11 +534,15 @@ var NativeDocument = (function (exports) {
515
534
 
516
535
  this.$listeners.push(callback);
517
536
  this.assocTrigger();
518
- PluginsManager.emit('ObservableSubscribe', this, target);
537
+ {
538
+ PluginsManager.emit('ObservableSubscribe', this, target);
539
+ }
519
540
  return () => {
520
541
  this.unsubscribe(callback);
521
542
  this.assocTrigger();
522
- PluginsManager.emit('ObservableUnsubscribe', this);
543
+ {
544
+ PluginsManager.emit('ObservableUnsubscribe', this);
545
+ }
523
546
  };
524
547
  };
525
548
 
@@ -739,7 +762,9 @@ var NativeDocument = (function (exports) {
739
762
  function NDElement(element) {
740
763
  this.$element = element;
741
764
  this.$observer = null;
742
- PluginsManager.emit('NDElementCreated', element, this);
765
+ {
766
+ PluginsManager.emit('NDElementCreated', element, this);
767
+ }
743
768
  }
744
769
 
745
770
  NDElement.prototype.__$isNDElement = true;
@@ -908,8 +933,9 @@ var NativeDocument = (function (exports) {
908
933
 
909
934
  NDElement.prototype[name] = method;
910
935
  }
911
-
912
- PluginsManager.emit('NDElementExtended', methods);
936
+ {
937
+ PluginsManager.emit('NDElementExtended', methods);
938
+ }
913
939
 
914
940
  return NDElement;
915
941
  };
@@ -1219,6 +1245,8 @@ var NativeDocument = (function (exports) {
1219
1245
  return anchor;
1220
1246
  }
1221
1247
 
1248
+ DocumentFragment.prototype.setAttribute = () => {};
1249
+
1222
1250
  const BOOLEAN_ATTRIBUTES = new Set([
1223
1251
  'checked',
1224
1252
  'selected',
@@ -1536,7 +1564,9 @@ var NativeDocument = (function (exports) {
1536
1564
 
1537
1565
  Function.prototype.toNdElement = function () {
1538
1566
  const child = this;
1539
- PluginsManager.emit('BeforeProcessComponent', child);
1567
+ {
1568
+ PluginsManager.emit('BeforeProcessComponent', child);
1569
+ }
1540
1570
  return ElementCreator.getChild(child());
1541
1571
  };
1542
1572
 
@@ -1615,8 +1645,9 @@ var NativeDocument = (function (exports) {
1615
1645
  */
1616
1646
  createElement(name) {
1617
1647
  if(name) {
1618
- if($nodeCache.has(name)) {
1619
- return $nodeCache.get(name).cloneNode();
1648
+ const cacheNode = $nodeCache.get(name);
1649
+ if(cacheNode) {
1650
+ return cacheNode.cloneNode();
1620
1651
  }
1621
1652
  const node = document.createElement(name);
1622
1653
  $nodeCache.set(name, node);
@@ -1631,12 +1662,16 @@ var NativeDocument = (function (exports) {
1631
1662
  */
1632
1663
  processChildren(children, parent) {
1633
1664
  if(children === null) return;
1634
- PluginsManager.emit('BeforeProcessChildren', parent);
1665
+ {
1666
+ PluginsManager.emit('BeforeProcessChildren', parent);
1667
+ }
1635
1668
  let child = this.getChild(children);
1636
1669
  if(child) {
1637
1670
  parent.appendChild(child);
1638
1671
  }
1639
- PluginsManager.emit('AfterProcessChildren', parent);
1672
+ {
1673
+ PluginsManager.emit('AfterProcessChildren', parent);
1674
+ }
1640
1675
  },
1641
1676
  getChild(child) {
1642
1677
  if(child == null) {
@@ -1659,7 +1694,6 @@ var NativeDocument = (function (exports) {
1659
1694
  * @param {Object} attributes
1660
1695
  */
1661
1696
  processAttributes(element, attributes) {
1662
- if(Validator.isFragment(element)) return;
1663
1697
  if (attributes) {
1664
1698
  AttributesWrapper(element, attributes);
1665
1699
  }
@@ -1672,7 +1706,9 @@ var NativeDocument = (function (exports) {
1672
1706
  * @returns {HTMLElement|DocumentFragment}
1673
1707
  */
1674
1708
  setup(element, attributes, customWrapper) {
1675
- PluginsManager.emit('Setup', element, attributes, customWrapper);
1709
+ {
1710
+ PluginsManager.emit('Setup', element, attributes, customWrapper);
1711
+ }
1676
1712
  return element;
1677
1713
  }
1678
1714
  };
@@ -2091,13 +2127,8 @@ var NativeDocument = (function (exports) {
2091
2127
  let element = ElementCreator.createElement($tagName);
2092
2128
  let finalElement = (customWrapper && typeof customWrapper === 'function') ? customWrapper(element) : element;
2093
2129
 
2094
- if(attributes) {
2095
- ElementCreator.processAttributes(finalElement, attributes);
2096
- }
2097
- if(children) {
2098
- ElementCreator.processChildren(children, finalElement);
2099
- }
2100
-
2130
+ ElementCreator.processAttributes(finalElement, attributes);
2131
+ ElementCreator.processChildren(children, finalElement);
2101
2132
  return ElementCreator.setup(finalElement, attributes, customWrapper);
2102
2133
  }
2103
2134
 
@@ -2147,14 +2178,6 @@ var NativeDocument = (function (exports) {
2147
2178
  return null;
2148
2179
  };
2149
2180
 
2150
- const findByPath = (root, path) => {
2151
- let target = root;
2152
- for (let i = 0, len = path.length; i < len; i++) {
2153
- target = target.childNodes[path[i]];
2154
- }
2155
- return target;
2156
- };
2157
-
2158
2181
  const $hydrateFn = function(hydrateFunction, targetType, element, property) {
2159
2182
  if(!cloneBindingsDataCache.has(element)) {
2160
2183
  // { classes, styles, attributes, value, attach }
@@ -2180,27 +2203,43 @@ var NativeDocument = (function (exports) {
2180
2203
  }
2181
2204
  };
2182
2205
 
2206
+
2207
+ const applyBindingTreePath = (root, target, data, path) => {
2208
+ let newTarget = null;
2209
+ if(path.fn) {
2210
+ newTarget = path.fn(data, target, root);
2211
+ }
2212
+ if(path.children) {
2213
+ for(let i = 0, length = path.children.length; i < length; i++) {
2214
+ const currentPath = path.children[i];
2215
+ const pathTargetNode = target.childNodes[currentPath.index];
2216
+ applyBindingTreePath(root, pathTargetNode, data, currentPath);
2217
+ }
2218
+ }
2219
+ return newTarget;
2220
+ };
2221
+
2183
2222
  function TemplateCloner($fn) {
2184
2223
  let $node = null;
2185
2224
  let $hasBindingData = false;
2186
2225
 
2187
- const $bindingPaths = [];
2226
+ const $bindingTreePath = {
2227
+ fn: null,
2228
+ children: [],
2229
+ };
2188
2230
 
2189
- const clone = (node, data, path) => {
2231
+ const clone = (node, data, currentPath) => {
2190
2232
  const bindDingData = cloneBindingsDataCache.get(node);
2191
2233
  if(node.nodeType === 3) {
2192
2234
  if(bindDingData && bindDingData.value) {
2193
- $bindingPaths.push({
2194
- path: [...path],
2195
- fn: (data, targetNode, currentRoot) => {
2196
- const newNode = bindDingData.value(data);
2197
- targetNode.replaceWith(newNode);
2198
- if (targetNode === currentRoot) {
2199
- return newNode;
2200
- }
2201
- return null;
2235
+ currentPath.fn = (data, targetNode, currentRoot) => {
2236
+ const newNode = bindDingData.value(data);
2237
+ if (targetNode === currentRoot) {
2238
+ return newNode;
2202
2239
  }
2203
- });
2240
+ targetNode.replaceWith(newNode);
2241
+ return null;
2242
+ };
2204
2243
  return bindDingData.value(data);
2205
2244
  }
2206
2245
  return node.cloneNode(true);
@@ -2212,35 +2251,35 @@ var NativeDocument = (function (exports) {
2212
2251
  if(bindDingData) {
2213
2252
  bindAttributes(nodeCloned, bindDingData, data);
2214
2253
  bindAttachMethods(nodeCloned, bindDingData, data);
2215
- $bindingPaths.push({
2216
- path: [...path],
2217
- fn: (data, targetNode) => {
2218
- bindAttributes(targetNode, bindDingData, data);
2219
- bindAttachMethods(targetNode, bindDingData, data);
2220
- }
2221
- });
2254
+ currentPath.fn = (data, targetNode) => {
2255
+ bindAttributes(targetNode, bindDingData, data);
2256
+ bindAttachMethods(targetNode, bindDingData, data);
2257
+ };
2222
2258
  }
2223
2259
  const childNodes = node.childNodes;
2260
+ const bindingPathChildren = [];
2224
2261
  for(let i = 0, length = childNodes.length; i < length; i++) {
2225
2262
  const childNode = childNodes[i];
2226
- path.push(i);
2263
+ const path = { index: i, fn: null };
2227
2264
  const childNodeCloned = clone(childNode, data, path);
2228
- path.pop();
2265
+ if(path.children || path.fn) {
2266
+ bindingPathChildren.push(path);
2267
+ }
2229
2268
  nodeCloned.appendChild(childNodeCloned);
2230
2269
  }
2270
+ if(bindingPathChildren.length) {
2271
+ currentPath.children = currentPath.children || [];
2272
+ currentPath.children = bindingPathChildren;
2273
+ }
2231
2274
  return nodeCloned;
2232
2275
  };
2233
2276
 
2234
2277
  const cloneWithBindingPaths = (data) => {
2235
2278
  let root = $node.cloneNode(true);
2236
2279
 
2237
- for (let i = 0, len = $bindingPaths.length; i < len; i++) {
2238
- const binding = $bindingPaths[i];
2239
- const target = findByPath(root, binding.path);
2240
- const newRoot = binding.fn(data, target, root);
2241
- if(newRoot) {
2242
- root = newRoot;
2243
- }
2280
+ const newRoot = applyBindingTreePath(root, root, data, $bindingTreePath);
2281
+ if(newRoot) {
2282
+ root = newRoot;
2244
2283
  }
2245
2284
 
2246
2285
  return root;
@@ -2253,7 +2292,7 @@ var NativeDocument = (function (exports) {
2253
2292
  return $node.cloneNode(true);
2254
2293
  }
2255
2294
 
2256
- const firstClone = clone($node, data, []);
2295
+ const firstClone = clone($node, data, $bindingTreePath);
2257
2296
  this.clone = cloneWithBindingPaths;
2258
2297
  return firstClone;
2259
2298
  };
@@ -2881,7 +2920,9 @@ var NativeDocument = (function (exports) {
2881
2920
  }
2882
2921
 
2883
2922
  ObservableItem.call(this, target, configs);
2884
- PluginsManager.emit('CreateObservableArray', this);
2923
+ {
2924
+ PluginsManager.emit('CreateObservableArray', this);
2925
+ }
2885
2926
  };
2886
2927
 
2887
2928
  ObservableArray.prototype = Object.create(ObservableItem.prototype);
@@ -2897,7 +2938,7 @@ var NativeDocument = (function (exports) {
2897
2938
 
2898
2939
  mutationMethods.forEach((method) => {
2899
2940
  ObservableArray.prototype[method] = function(...values) {
2900
- const result = this.$currentValue[method](...values);
2941
+ const result = this.$currentValue[method].apply(this.$currentValue, values);
2901
2942
  this.trigger({ action: method, args: values, result });
2902
2943
  return result;
2903
2944
  };
@@ -2905,7 +2946,7 @@ var NativeDocument = (function (exports) {
2905
2946
 
2906
2947
  noMutationMethods.forEach((method) => {
2907
2948
  ObservableArray.prototype[method] = function(...values) {
2908
- return this.$currentValue[method](...values);
2949
+ return this.$currentValue[method].apply(this.$currentValue, values);
2909
2950
  };
2910
2951
  });
2911
2952
 
@@ -2923,7 +2964,7 @@ var NativeDocument = (function (exports) {
2923
2964
  };
2924
2965
 
2925
2966
  ObservableArray.prototype.merge = function(values) {
2926
- this.$currentValue.push(...values);
2967
+ this.$currentValue.push.apply(this.$currentValue, values);
2927
2968
  this.trigger({ action: 'merge', args: values });
2928
2969
  };
2929
2970
 
@@ -2999,7 +3040,7 @@ var NativeDocument = (function (exports) {
2999
3040
  const deps = Array.isArray(predicate.dependencies)
3000
3041
  ? predicate.dependencies
3001
3042
  : [predicate.dependencies];
3002
- observableDependencies.push(...deps);
3043
+ observableDependencies.push.apply(observableDependencies, deps);
3003
3044
  }
3004
3045
  } else if(typeof predicate === 'function') {
3005
3046
  filterCallbacks[key] = predicate;
@@ -3280,8 +3321,9 @@ var NativeDocument = (function (exports) {
3280
3321
  const initialValue = callback();
3281
3322
  const observable = new ObservableItem(initialValue);
3282
3323
  const updatedValue = nextTick(() => observable.set(callback()));
3283
-
3284
- PluginsManager.emit('CreateObservableComputed', observable, dependencies);
3324
+ {
3325
+ PluginsManager.emit('CreateObservableComputed', observable, dependencies);
3326
+ }
3285
3327
 
3286
3328
  if(Validator.isFunction(dependencies)) {
3287
3329
  if(!Validator.isObservable(dependencies.$observer)) {
@@ -3602,7 +3644,7 @@ var NativeDocument = (function (exports) {
3602
3644
  if(child) {
3603
3645
  cache.set(item, {
3604
3646
  child,
3605
- indexObserver: (indexObserver ? new WeakRef(indexObserver) : null)
3647
+ indexObserver
3606
3648
  });
3607
3649
  return child;
3608
3650
  }