native-document 1.0.72 → 1.0.75

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.
@@ -2,7 +2,8 @@ import Anchor from "../elements/anchor";
2
2
  import Validator from "../utils/validator";
3
3
  import AttributesWrapper from "./AttributesWrapper";
4
4
  import PluginsManager from "../utils/plugins-manager";
5
- import './prototype-extensions';
5
+ import './prototypes/nd-element-extensions';
6
+ import './prototypes/attributes-extensions';
6
7
 
7
8
  const $nodeCache = new Map();
8
9
  let $textNodeCache = null;
@@ -75,20 +76,10 @@ export const ElementCreator = {
75
76
  processChildren(children, parent) {
76
77
  if(children === null) return;
77
78
  PluginsManager.emit('BeforeProcessChildren', parent);
78
- if(!Array.isArray(children)) {
79
- let child = this.getChild(children);
80
- if(child) {
81
- parent.appendChild(child);
82
- }
83
- }
84
- else {
85
- for(let i = 0, length = children.length; i < length; i++) {
86
- let child = this.getChild(children[i]);
87
- if (child === null) continue;
88
- parent.appendChild(child);
89
- }
79
+ let child = this.getChild(children);
80
+ if(child) {
81
+ parent.appendChild(child);
90
82
  }
91
-
92
83
  PluginsManager.emit('AfterProcessChildren', parent);
93
84
  },
94
85
  getChild(child) {
@@ -101,8 +101,8 @@ NDElement.prototype.closedShadow = function(style = null) {
101
101
  return this.shadow('closed', style);
102
102
  };
103
103
 
104
- NDElement.prototype.attach = function(bindingHydrator) {
105
- bindingHydrator.$hydrate(this.$element);
104
+ NDElement.prototype.attach = function(methodName, bindingHydrator) {
105
+ bindingHydrator.$hydrate(this.$element, methodName);
106
106
  return this.$element;
107
107
  };
108
108
 
@@ -9,7 +9,7 @@ const cloneBindingsDataCache = new WeakMap();
9
9
  const bindAttributes = (node, bindDingData, data) => {
10
10
  let attributes = null;
11
11
  if(bindDingData.attributes) {
12
- attributes = attributes || {};
12
+ attributes = {};
13
13
  for (const attr in bindDingData.attributes) {
14
14
  attributes[attr] = bindDingData.attributes[attr](...data);
15
15
  }
@@ -39,29 +39,29 @@ const bindAttributes = (node, bindDingData, data) => {
39
39
  return null;
40
40
  };
41
41
 
42
- const $hydrateFn = function(hydrateFunction, target, element, property) {
42
+ const $hydrateFn = function(hydrateFunction, targetType, element, property) {
43
43
  if(!cloneBindingsDataCache.has(element)) {
44
44
  // { classes, styles, attributes, value, attach }
45
45
  cloneBindingsDataCache.set(element, {});
46
46
  }
47
47
  const hydrationState = cloneBindingsDataCache.get(element);
48
- if(target === 'value') {
48
+ if(targetType === 'value') {
49
49
  hydrationState.value = hydrateFunction;
50
50
  return;
51
51
  }
52
- if(target === 'attach') {
53
- hydrationState.attach = hydrateFunction;
54
- return;
55
- }
56
- hydrationState[target] = hydrationState[target] || {};
57
- hydrationState[target][property] = hydrateFunction;
52
+ hydrationState[targetType] = hydrationState[targetType] || {};
53
+ hydrationState[targetType][property] = hydrateFunction;
58
54
  }
59
55
 
60
56
  const bindAttachMethods = function(node, bindDingData, data) {
61
57
  if(!bindDingData.attach) {
62
58
  return null;
63
59
  }
64
- bindDingData.attach(node, ...data);
60
+ for(const methodName in bindDingData.attach) {
61
+ node.nd[methodName](function(...args) {
62
+ bindDingData.attach[methodName].call(this, ...[...args, ...data]);
63
+ });
64
+ }
65
65
  };
66
66
 
67
67
  export function TemplateCloner($fn) {
@@ -109,10 +109,10 @@ export function TemplateCloner($fn) {
109
109
  };
110
110
 
111
111
 
112
- const createBinding = (hydrateFunction, target) => {
112
+ const createBinding = (hydrateFunction, targetType) => {
113
113
  return new TemplateBinding((element, property) => {
114
114
  $hasBindingData = true;
115
- $hydrateFn(hydrateFunction, target, element, property)
115
+ $hydrateFn(hydrateFunction, targetType, element, property)
116
116
  });
117
117
  };
118
118
 
@@ -0,0 +1,49 @@
1
+ import Validator from "../../utils/validator";
2
+ import {Observable} from "../../data/Observable";
3
+ import {
4
+ bindAttributeWithObservable,
5
+ bindBooleanAttribute,
6
+ bindClassAttribute,
7
+ bindStyleAttribute
8
+ } from "../AttributesWrapper";
9
+ import ObservableItem from "../../data/ObservableItem";
10
+ import TemplateBinding from "../TemplateBinding";
11
+ import {BOOLEAN_ATTRIBUTES} from "../constants";
12
+
13
+ Object.prototype.handleNdAttribute = function(element, attributeName) {
14
+ if(attributeName === 'class') {
15
+ bindClassAttribute(element, this);
16
+ return;
17
+ }
18
+ if(attributeName === 'style') {
19
+ bindStyleAttribute(element, this);
20
+
21
+ }
22
+ };
23
+
24
+ String.prototype.handleNdAttribute = function(element, attributeName) {
25
+ let value = this.resolveObservableTemplate ? this.resolveObservableTemplate() : this;
26
+ if(Validator.isString(value)) {
27
+ element.setAttribute(attributeName, value);
28
+ return;
29
+ }
30
+ const observables = value.filter(item => Validator.isObservable(item));
31
+ value = Observable.computed(() => {
32
+ return value.map(item => Validator.isObservable(item) ? item.val() : item).join(' ') || ' ';
33
+ }, observables);
34
+
35
+ if(BOOLEAN_ATTRIBUTES.includes(attributeName)) {
36
+ bindBooleanAttribute(element, attributeName, value);
37
+ return;
38
+ }
39
+
40
+ bindAttributeWithObservable(element, attributeName, value);
41
+ };
42
+
43
+ ObservableItem.prototype.handleNdAttribute = function(element, attributeName) {
44
+ bindAttributeWithObservable(element, attributeName, this);
45
+ };
46
+
47
+ TemplateBinding.prototype.handleNdAttribute = function(element, attributeName) {
48
+ this.$hydrate(element, attributeName);
49
+ }
@@ -1,9 +1,9 @@
1
- import ObservableItem from "../data/ObservableItem";
2
- import {NDElement} from "./NDElement";
3
- import TemplateBinding from "./TemplateBinding";
4
- import {ElementCreator} from "./ElementCreator";
5
- import PluginsManager from "../utils/plugins-manager";
6
- import Validator from "../utils/validator";
1
+ import ObservableItem from "../../data/ObservableItem";
2
+ import {NDElement} from "../NDElement";
3
+ import TemplateBinding from "../TemplateBinding";
4
+ import {ElementCreator} from "../ElementCreator";
5
+ import PluginsManager from "../../utils/plugins-manager";
6
+ import Validator from "../../utils/validator";
7
7
 
8
8
  String.prototype.toNdElement = function () {
9
9
  const formattedChild = this.resolveObservableTemplate ? this.resolveObservableTemplate() : this;
@@ -40,7 +40,9 @@ NDElement.prototype.toNdElement = function () {
40
40
  Array.prototype.toNdElement = function () {
41
41
  const fragment = document.createDocumentFragment();
42
42
  for(let i = 0, length = this.length; i < length; i++) {
43
- fragment.appendChild(ElementCreator.getChild(this[i]));
43
+ const child = ElementCreator.getChild(this[i]);
44
+ if(child === null) continue;
45
+ fragment.appendChild(child);
44
46
  }
45
47
  return fragment;
46
48
  };