native-document 1.0.88 → 1.0.89

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.
@@ -6,8 +6,8 @@ export const ObservableWhen = function(observer, value) {
6
6
 
7
7
  ObservableWhen.prototype.__$isObservableWhen = true;
8
8
 
9
- ObservableWhen.prototype.subscribe = function(callback) {
10
- return this.$observer.on(this.$target, callback);
9
+ ObservableWhen.prototype.subscribe = function(callback, context = null) {
10
+ return this.$observer.on(this.$target, callback, context);
11
11
  };
12
12
 
13
13
  ObservableWhen.prototype.val = function() {
@@ -0,0 +1,21 @@
1
+
2
+
3
+ export function CallbackHandler(callback) {
4
+
5
+ if(!(this instanceof CallbackHandler)) {
6
+ return new CallbackHandler(callback);
7
+ }
8
+
9
+ const $contextSore = new WeakMap();
10
+
11
+ this.set = (target, context) => {
12
+ $contextSore.set(target, context);
13
+ };
14
+
15
+ this.callback = function() {
16
+ const context = $contextSore.get(this);
17
+ if(context) {
18
+ callback(context, ...arguments);
19
+ }
20
+ };
21
+ }
@@ -1,30 +1,30 @@
1
1
  import Validator from "../utils/validator";
2
- import NativeDocumentError from "../errors/NativeDocumentError";
3
2
  import {BOOLEAN_ATTRIBUTES} from "./constants.js";
4
3
  import {Observable} from "../data/Observable";
5
- import './prototypes/bind-class-extensions';
6
-
7
-
8
- export function toggleElementClass(element, className, shouldAdd) {
9
- element.classes.toggle(className, shouldAdd);
10
- }
11
4
 
5
+ export const handleElementAttributeClass = function(shouldAdd, _, __, context) {
6
+ context.element.classes.toggle(context.className, shouldAdd);
7
+ };
12
8
  /**
13
9
  *
14
10
  * @param {HTMLElement} element
15
11
  * @param {Object} data
16
12
  */
17
13
  export function bindClassAttribute(element, data) {
18
- for(let className in data) {
14
+ const classNames = Object.keys(data);
15
+
16
+ for(let i = 0, length = classNames.length; i < length; i++) {
17
+ const className = classNames[i];
19
18
  const value = data[className];
20
19
  if(value.__$isObservable) {
21
20
  element.classes.toggle(className, value.val());
22
- value.subscribe(toggleElementClass.bind(null, element, className));
21
+ value.subscribe(handleElementAttributeClass, { element, className });
22
+ // value.subscribe((shouldAdd) => element.classes.toggle(className, shouldAdd));
23
23
  continue;
24
24
  }
25
25
  if(value.__$isObservableWhen) {
26
26
  element.classes.toggle(className, value.isMath());
27
- value.subscribe(toggleElementClass.bind(null, element, className));
27
+ value.subscribe(handleElementAttributeClass, { element, className });
28
28
  continue;
29
29
  }
30
30
  if(value.$hydrate) {
@@ -33,7 +33,6 @@ export function bindClassAttribute(element, data) {
33
33
  }
34
34
  element.classes.toggle(className, value)
35
35
  }
36
- data = null;
37
36
  }
38
37
 
39
38
  /**
@@ -42,7 +41,9 @@ export function bindClassAttribute(element, data) {
42
41
  * @param {Object} data
43
42
  */
44
43
  export function bindStyleAttribute(element, data) {
45
- for(let styleName in data) {
44
+ const keys = Object.keys(data)
45
+ for(let i = 0, length = keys.length; i < length; i++) {
46
+ const styleName = keys[i];
46
47
  const value = data[styleName];
47
48
  if(value.__$isObservable) {
48
49
  element.style[styleName] = value.val();
@@ -62,7 +63,8 @@ export function bindStyleAttribute(element, data) {
62
63
  export function bindBooleanAttribute(element, attributeName, value) {
63
64
  const isObservable = value.__$isObservable;
64
65
  const defaultValue = isObservable? value.val() : value;
65
- if(Validator.isBoolean(defaultValue)) {
66
+ const isBoolValue = typeof defaultValue === "boolean";
67
+ if(isBoolValue) {
66
68
  element[attributeName] = defaultValue;
67
69
  }
68
70
  else {
@@ -70,13 +72,13 @@ export function bindBooleanAttribute(element, attributeName, value) {
70
72
  }
71
73
  if(isObservable) {
72
74
  if(attributeName === 'checked') {
73
- if(typeof defaultValue === 'boolean') {
75
+ if(isBoolValue) {
74
76
  element.addEventListener('input', () => value.set(element[attributeName]));
77
+ value.subscribe((newValue) => element[attributeName] = newValue);
78
+ return;
75
79
  }
76
- else {
77
- element.addEventListener('input', () => value.set(element.value));
78
- }
79
- value.subscribe((newValue) => element[attributeName] = newValue);
80
+ element.addEventListener('input', () => value.set(element.value));
81
+ value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
80
82
  return;
81
83
  }
82
84
  value.subscribe((newValue) => element[attributeName] = (newValue === element.value));
@@ -91,14 +93,14 @@ export function bindBooleanAttribute(element, attributeName, value) {
91
93
  * @param {Observable} value
92
94
  */
93
95
  export function bindAttributeWithObservable(element, attributeName, value) {
94
- const applyValue = attributeName === 'value' ? (newValue) => element.value = newValue : (newValue) => element.setAttribute(attributeName, newValue);
95
- value.subscribe(applyValue);
96
-
97
96
  if(attributeName === 'value') {
97
+ value.subscribe((newValue) => element.value = newValue);
98
98
  element.value = value.val();
99
99
  element.addEventListener('input', () => value.set(element.value));
100
100
  return;
101
101
  }
102
+
103
+ value.subscribe((newValue) => element.setAttribute(attributeName, newValue));
102
104
  element.setAttribute(attributeName, value.val());
103
105
  }
104
106
 
@@ -109,11 +111,15 @@ export function bindAttributeWithObservable(element, attributeName, value) {
109
111
  */
110
112
  export default function AttributesWrapper(element, attributes) {
111
113
 
112
- Validator.validateAttributes(attributes);
114
+ if(process.env.NODE_ENV === 'development') {
115
+ Validator.validateAttributes(attributes);
116
+ }
117
+ const attributeNames = Object.keys(attributes);
113
118
 
114
- for(let key in attributes) {
115
- const attributeName = key.toLowerCase();
116
- let value = attributes[attributeName];
119
+ for(let i = 0, length = attributeNames.length; i < length; i++) {
120
+ const originalAttributeName = attributeNames[i];
121
+ const attributeName = originalAttributeName.toLowerCase();
122
+ let value = attributes[originalAttributeName];
117
123
  if(value == null) {
118
124
  continue;
119
125
  }
@@ -136,6 +142,10 @@ export default function AttributesWrapper(element, attributes) {
136
142
  continue;
137
143
  }
138
144
 
145
+ if(attributeName === 'value') {
146
+ element.value = value;
147
+ continue;
148
+ }
139
149
  element.setAttribute(attributeName, value);
140
150
  }
141
151
  return element;
@@ -1,16 +1,20 @@
1
1
  import ObservableItem from "../../../core/data/ObservableItem";
2
- import {toggleElementClass} from "../AttributesWrapper";
3
2
  import {ObservableWhen} from "../../data/ObservableWhen";
4
3
  import TemplateBinding from "../../../core/wrappers/TemplateBinding";
5
4
 
5
+
6
+ export const handleElementAttributeClass = function(shouldAdd, _, __, context) {
7
+ context.element.classes.toggle(context.className, shouldAdd);
8
+ };
9
+
6
10
  ObservableItem.prototype.bindNdClass = function(element, className) {
7
11
  element.classes.toggle(className, this.val());
8
- this.subscribe(toggleElementClass.bind(null, element, className));
12
+ this.subscribe(handleElementAttributeClass, { element, className });
9
13
  };
10
14
 
11
15
  ObservableWhen.prototype.bindNdClass = function(element, className) {
12
16
  element.classes.toggle(className, this.isMath());
13
- this.subscribe(toggleElementClass.bind(null, element, className));
17
+ this.subscribe(handleElementAttributeClass, { element, className });
14
18
  };
15
19
 
16
20
  TemplateBinding.prototype.bindNdClass = function(element, className) {