native-document 1.0.36 → 1.0.38

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.
@@ -150,10 +150,14 @@ var NativeDocument = (function (exports) {
150
150
  list() {
151
151
  return $pluginByEvents;
152
152
  },
153
- add(name, plugin){
153
+ add(plugin, name){
154
154
  if (!plugin || typeof plugin !== 'object') {
155
155
  throw new Error(`Plugin ${name} must be an object`);
156
156
  }
157
+ name = name || plugin.name;
158
+ if (!name || typeof name !== 'string') {
159
+ throw new Error(`Please, provide a valid plugin name`);
160
+ }
157
161
  if($plugins.has(name)) {
158
162
  return;
159
163
  }
@@ -198,7 +202,7 @@ var NativeDocument = (function (exports) {
198
202
  const plugins = $pluginByEvents.get(eventName);
199
203
 
200
204
  for(const plugin of plugins) {
201
- const callback = plugin[eventName];
205
+ const callback = plugin['on'+eventName];
202
206
  if(typeof callback === 'function') {
203
207
  try{
204
208
  callback.call(plugin, ...data);
@@ -691,11 +695,153 @@ var NativeDocument = (function (exports) {
691
695
 
692
696
  NDElement.prototype.node = NDElement.prototype.htmlElement;
693
697
 
698
+ NDElement.prototype.shadow = function(mode, style = null) {
699
+ const $element = this.$element;
700
+ const children = Array.from($element.childNodes);
701
+ const shadowRoot = $element.attachShadow({ mode });
702
+ if(style) {
703
+ const styleNode = document.createElement("style");
704
+ styleNode.textContent = style;
705
+ shadowRoot.appendChild(styleNode);
706
+ }
707
+ $element.append = shadowRoot.append.bind(shadowRoot);
708
+ $element.appendChild = shadowRoot.appendChild.bind(shadowRoot);
709
+ shadowRoot.append(...children);
710
+
711
+ return this;
712
+ };
713
+ NDElement.prototype.openShadow = function(style = null) {
714
+ return this.shadow('open', style);
715
+ };
716
+ NDElement.prototype.closedShadow = function(style = null) {
717
+ return this.shadow('closed', style);
718
+ };
719
+
694
720
  NDElement.prototype.attach = function(bindingHydrator) {
695
721
  bindingHydrator.$hydrate(this.$element);
696
722
  return this.$element;
697
723
  };
698
724
 
725
+
726
+ /**
727
+ *
728
+ * ND events API
729
+ *
730
+ */
731
+
732
+ const DelegatedEventsCallbackStore = {};
733
+
734
+ const addCallbackToCallbacksStore = function(element, eventName, callback) {
735
+ if(!element) return;
736
+ if(!DelegatedEventsCallbackStore[eventName]) {
737
+ const eventStore = new WeakMap();
738
+ DelegatedEventsCallbackStore[eventName] = eventStore;
739
+ eventStore.set(element, callback);
740
+ return;
741
+ }
742
+ const eventStore = DelegatedEventsCallbackStore[eventName];
743
+
744
+ if(!eventStore.has(element)) {
745
+ eventStore.set(element, callback);
746
+ return;
747
+ }
748
+ const existingCallbacks = eventStore.get(element);
749
+ if(!Validator.isArray(existingCallbacks)) {
750
+ eventStore.set(element, [store[eventName], callback]);
751
+ return;
752
+ }
753
+ existingCallbacks.push(callback);
754
+ };
755
+
756
+ const handleDelegatedCallbacks = function(container, eventName) {
757
+ container.addEventListener(eventName, (event) => {
758
+ const eventStore = DelegatedEventsCallbackStore[eventName];
759
+ if(!eventStore) {
760
+ return;
761
+ }
762
+ let target = event.target;
763
+ while(target && target !== container) {
764
+ const callback = eventStore.get(target);
765
+ if(!callback) {
766
+ target = target.parentElement;
767
+ continue;
768
+ }
769
+
770
+ if(Validator.isFunction(callback)) {
771
+ callback.call(target, event);
772
+ }
773
+ else {
774
+ for(let i = 0; i < callback.length; i++) {
775
+ callback[i].call(target, event);
776
+ }
777
+ }
778
+ return;
779
+ }
780
+ });
781
+ };
782
+
783
+
784
+ const preventDefaultWrapper = function(element, eventName, callback) {
785
+ element.addEventListener(eventName, (event) => {
786
+ event.preventDefault();
787
+ callback && callback.call(element, event);
788
+ });
789
+ return this;
790
+ };
791
+ const stopPropagationWrapper = function(element, eventName, callback) {
792
+ element.addEventListener(eventName, (event) => {
793
+ event.stopPropagation();
794
+ callback && callback.call(element, event);
795
+ });
796
+ return this;
797
+ };
798
+ const preventDefaultAndStopPropagationWrapper = function(element, eventName, callback) {
799
+ element.addEventListener(eventName, (event) => {
800
+ event.stopPropagation();
801
+ event.preventDefault();
802
+ callback && callback.call(element, event);
803
+ });
804
+ return this;
805
+ };
806
+ const captureEventWrapper = function(element, eventName, directHandler) {
807
+ if(directHandler) {
808
+ element.addEventListener(eventName, directHandler);
809
+ return this;
810
+ }
811
+ handleDelegatedCallbacks(element, eventName);
812
+ return this;
813
+ };
814
+
815
+ for(const event of EVENTS) {
816
+ const eventName = event.toLowerCase();
817
+ NDElement.prototype['on'+event] = function(callback) {
818
+ this.$element.addEventListener(eventName, callback);
819
+ return this;
820
+ };
821
+ NDElement.prototype['onPrevent'+event] = function(callback) {
822
+ preventDefaultWrapper(this.$element, eventName, callback);
823
+ return this;
824
+ };
825
+ NDElement.prototype['onStop'+event] = function(callback) {
826
+ stopPropagationWrapper(this.$element, eventName, callback);
827
+ return this;
828
+ };
829
+ NDElement.prototype['onPreventStop'+event] = function(callback) {
830
+ preventDefaultAndStopPropagationWrapper(this.$element, eventName, callback);
831
+ return this;
832
+ };
833
+
834
+ NDElement.prototype['when'+event] = function(callback) {
835
+ addCallbackToCallbacksStore(this.$element, eventName, callback);
836
+ return this;
837
+ };
838
+
839
+ NDElement.prototype['capture'+event] = function(directHandler) {
840
+ captureEventWrapper(this.$element, eventName, directHandler);
841
+ return this;
842
+ };
843
+ }
844
+
699
845
  const COMMON_NODE_TYPES = {
700
846
  ELEMENT: 1,
701
847
  TEXT: 3,
@@ -1571,6 +1717,23 @@ var NativeDocument = (function (exports) {
1571
1717
  return null;
1572
1718
  };
1573
1719
 
1720
+ const $hydrateFn = function(hydrateFunction, target, element, property) {
1721
+ if(!cloneBindingsDataCache.has(element)) {
1722
+ // { classes, styles, attributes, value, attach }
1723
+ cloneBindingsDataCache.set(element, {});
1724
+ }
1725
+ const hydrationState = cloneBindingsDataCache.get(element);
1726
+ if(target === 'value') {
1727
+ hydrationState.value = hydrateFunction;
1728
+ return;
1729
+ }
1730
+ if(target === 'attach') {
1731
+ hydrationState.attach = hydrateFunction;
1732
+ return;
1733
+ }
1734
+ hydrationState[target] = hydrationState[target] || {};
1735
+ hydrationState[target][property] = hydrateFunction;
1736
+ };
1574
1737
 
1575
1738
  const bindAttachMethods = function(node, bindDingData, data) {
1576
1739
  if(!bindDingData.attach) {
@@ -1581,6 +1744,7 @@ var NativeDocument = (function (exports) {
1581
1744
 
1582
1745
  function TemplateCloner($fn) {
1583
1746
  let $node = null;
1747
+ let $hasBindingData = false;
1584
1748
 
1585
1749
  const clone = (node, data) => {
1586
1750
  const bindDingData = cloneBindingsDataCache.get(node);
@@ -1590,11 +1754,14 @@ var NativeDocument = (function (exports) {
1590
1754
  }
1591
1755
  return node.cloneNode(true);
1592
1756
  }
1593
- const nodeCloned = node.cloneNode();
1757
+ const nodeCloned = node.cloneNode(node.fullCloneNode);
1594
1758
  if(bindDingData) {
1595
1759
  bindAttributes(nodeCloned, bindDingData, data);
1596
1760
  bindAttachMethods(nodeCloned, bindDingData, data);
1597
1761
  }
1762
+ if(node.fullCloneNode) {
1763
+ return nodeCloned;
1764
+ }
1598
1765
  const childNodes = node.childNodes;
1599
1766
  for(let i = 0, length = childNodes.length; i < length; i++) {
1600
1767
  const childNode = childNodes[i];
@@ -1607,31 +1774,25 @@ var NativeDocument = (function (exports) {
1607
1774
  this.clone = (data) => {
1608
1775
  if(!$node) {
1609
1776
  $node = $fn(this);
1777
+ if(!$hasBindingData) {
1778
+ const nodeCloned = $node.cloneNode(true);
1779
+ nodeCloned.fullCloneNode = true;
1780
+ return nodeCloned;
1781
+ }
1782
+ }
1783
+ if(!$hasBindingData) {
1784
+ return $node.cloneNode(true);
1610
1785
  }
1611
1786
  return clone($node, data);
1612
1787
  };
1613
1788
 
1614
- const $hydrateFn = function(hydrateFunction, target, element, property) {
1615
- if(!cloneBindingsDataCache.has(element)) {
1616
- // { classes, styles, attributes, value, attach }
1617
- cloneBindingsDataCache.set(element, {});
1618
- }
1619
- const hydrationState = cloneBindingsDataCache.get(element);
1620
- if(target === 'value') {
1621
- hydrationState.value = hydrateFunction;
1622
- return;
1623
- }
1624
- if(target === 'attach') {
1625
- hydrationState.attach = hydrateFunction;
1626
- return;
1627
- }
1628
- hydrationState[target] = hydrationState[target] || {};
1629
- hydrationState[target][property] = hydrateFunction;
1630
- };
1631
1789
 
1632
1790
  const createBinding = (hydrateFunction, target) => {
1633
1791
  return {
1634
- $hydrate : (element, property) => $hydrateFn(hydrateFunction, target, element, property),
1792
+ $hydrate : (element, property) => {
1793
+ $hasBindingData = true;
1794
+ $hydrateFn(hydrateFunction, target, element, property);
1795
+ },
1635
1796
  }
1636
1797
  };
1637
1798
 
@@ -1740,118 +1901,6 @@ var NativeDocument = (function (exports) {
1740
1901
  });
1741
1902
  };
1742
1903
 
1743
- (function() {
1744
- const DelegatedEventsCallbackStore = {};
1745
-
1746
- const addCallbackToCallbacksStore = function(element, eventName, callback) {
1747
- if(!element) return;
1748
- if(!DelegatedEventsCallbackStore[eventName]) {
1749
- const eventStore = new WeakMap();
1750
- DelegatedEventsCallbackStore[eventName] = eventStore;
1751
- eventStore.set(element, callback);
1752
- return;
1753
- }
1754
- const eventStore = DelegatedEventsCallbackStore[eventName];
1755
-
1756
- if(!eventStore.has(element)) {
1757
- eventStore.set(element, callback);
1758
- return;
1759
- }
1760
- const existingCallbacks = eventStore.get(element);
1761
- if(!Validator.isArray(existingCallbacks)) {
1762
- eventStore.set(element, [store[eventName], callback]);
1763
- return;
1764
- }
1765
- existingCallbacks.push(callback);
1766
- };
1767
-
1768
- const handleDelegatedCallbacks = function(container, eventName) {
1769
- container.addEventListener(eventName, (event) => {
1770
- const eventStore = DelegatedEventsCallbackStore[eventName];
1771
- if(!eventStore) {
1772
- return;
1773
- }
1774
- let target = event.target;
1775
- while(target && target !== container) {
1776
- const callback = eventStore.get(target);
1777
- if(!callback) {
1778
- target = target.parentElement;
1779
- continue;
1780
- }
1781
-
1782
- if(Validator.isFunction(callback)) {
1783
- callback.call(target, event);
1784
- }
1785
- else {
1786
- for(let i = 0; i < callback.length; i++) {
1787
- callback[i].call(target, event);
1788
- }
1789
- }
1790
- return;
1791
- }
1792
- });
1793
- };
1794
-
1795
-
1796
- const preventDefaultWrapper = function(element, eventName, callback) {
1797
- element.addEventListener(eventName, (event) => {
1798
- event.preventDefault();
1799
- callback && callback.call(element, event);
1800
- });
1801
- return this;
1802
- };
1803
- const stopPropagationWrapper = function(element, eventName, callback) {
1804
- element.addEventListener(eventName, (event) => {
1805
- event.stopPropagation();
1806
- callback && callback.call(element, event);
1807
- });
1808
- return this;
1809
- };
1810
- const preventDefaultAndStopPropagationWrapper = function(element, eventName, callback) {
1811
- element.addEventListener(eventName, (event) => {
1812
- event.stopPropagation();
1813
- event.preventDefault();
1814
- callback && callback.call(element, event);
1815
- });
1816
- return this;
1817
- };
1818
- const captureEventWrapper = function(element, eventName, directHandler) {
1819
- if(directHandler) {
1820
- element.addEventListener(eventName, directHandler);
1821
- return this;
1822
- }
1823
- handleDelegatedCallbacks(element, eventName);
1824
- return this;
1825
- };
1826
-
1827
- for(const event of EVENTS) {
1828
- const eventName = event.toLowerCase();
1829
- NDElement.prototype['on'+event] = function(callback) {
1830
- this.$element.addEventListener(eventName, callback);
1831
- return this;
1832
- };
1833
- NDElement.prototype['onPrevent'+event] = function(callback) {
1834
- return preventDefaultWrapper(this.$element, eventName, callback);
1835
- };
1836
- NDElement.prototype['onStop'+event] = function(callback) {
1837
- return stopPropagationWrapper(this.$element, eventName, callback);
1838
- };
1839
- NDElement.prototype['onPreventStop'+event] = function(callback) {
1840
- return preventDefaultAndStopPropagationWrapper(this.$element, eventName, callback);
1841
- };
1842
-
1843
- NDElement.prototype['when'+event] = function(callback) {
1844
- addCallbackToCallbacksStore(this.$element, eventName, callback);
1845
- return this;
1846
- };
1847
-
1848
- NDElement.prototype['capture'+event] = function(directHandler) {
1849
- captureEventWrapper(this.$element, eventName, directHandler);
1850
- return this;
1851
- };
1852
- }
1853
- }());
1854
-
1855
1904
  const cssPropertyAccumulator = function(initialValue = {}) {
1856
1905
  let data = Validator.isString(initialValue) ? initialValue.split(';').filter(Boolean) : initialValue;
1857
1906
  const isArray = Validator.isArray(data);
@@ -3840,6 +3889,7 @@ var NativeDocument = (function (exports) {
3840
3889
  exports.HtmlElementWrapper = HtmlElementWrapper;
3841
3890
  exports.NDElement = NDElement;
3842
3891
  exports.Observable = Observable;
3892
+ exports.PluginsManager = PluginsManager;
3843
3893
  exports.Store = Store;
3844
3894
  exports.TemplateCloner = TemplateCloner;
3845
3895
  exports.classPropertyAccumulator = classPropertyAccumulator;