native-document 1.0.152 → 1.0.154
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.
- package/cdn.js +0 -4
- package/dist/native-document.components.min.js +1410 -172
- package/dist/native-document.dev.js +3207 -2943
- package/dist/native-document.dev.js.map +1 -1
- package/dist/native-document.min.js +1 -1
- package/index.js +11 -7
- package/package.json +2 -1
- package/src/core/data/Observable.js +141 -0
- package/src/core/data/ObservableArray.js +1 -2
- package/src/core/data/ObservableItem.js +2 -0
- package/src/core/data/ObservableObject.js +10 -10
- package/src/core/data/observable-helpers/observable.is-to.js +1 -2
- package/src/core/data/observable-helpers/observable.prototypes.js +1 -2
- package/src/core/data/observable-helpers/array.js +0 -22
- package/src/core/data/observable-helpers/batch.js +0 -22
- package/src/core/data/observable-helpers/computed.js +0 -55
- package/src/core/data/observable-helpers/object.js +0 -48
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ObservableItem from "./ObservableItem";
|
|
2
2
|
import Validator from "../utils/validator";
|
|
3
3
|
import {nextTick} from "../utils/helpers";
|
|
4
|
-
import
|
|
4
|
+
import ObservableArray from "./ObservableArray";
|
|
5
5
|
|
|
6
6
|
export const ObservableObject = function(target, configs) {
|
|
7
7
|
ObservableItem.call(this, target);
|
|
@@ -43,24 +43,24 @@ ObservableObject.prototype.$load = function(initialValue) {
|
|
|
43
43
|
if(configs?.deep !== false) {
|
|
44
44
|
const mappedItemValue = itemValue.map(item => {
|
|
45
45
|
if(Validator.isJson(item)) {
|
|
46
|
-
return
|
|
46
|
+
return new ObservableObject(item, configs);
|
|
47
47
|
}
|
|
48
48
|
if(Validator.isArray(item)) {
|
|
49
|
-
return
|
|
49
|
+
return new ObservableArray(item, configs);
|
|
50
50
|
}
|
|
51
|
-
return
|
|
51
|
+
return new ObservableItem(item, configs);
|
|
52
52
|
});
|
|
53
|
-
this.$observables[key] =
|
|
53
|
+
this.$observables[key] = new ObservableArray(mappedItemValue, configs);
|
|
54
54
|
continue;
|
|
55
55
|
}
|
|
56
|
-
this.$observables[key] =
|
|
56
|
+
this.$observables[key] = new ObservableArray(itemValue, configs);
|
|
57
57
|
continue;
|
|
58
58
|
}
|
|
59
59
|
if(Validator.isObservable(itemValue) || Validator.isProxy(itemValue)) {
|
|
60
60
|
this.$observables[key] = itemValue;
|
|
61
61
|
continue;
|
|
62
62
|
}
|
|
63
|
-
this.$observables[key] = (typeof itemValue === 'object') ?
|
|
63
|
+
this.$observables[key] = (typeof itemValue === 'object') ? new ObservableObject(itemValue, configs) : new ObservableItem(itemValue, configs);
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
|
|
@@ -122,9 +122,9 @@ ObservableObject.prototype.set = function(newData) {
|
|
|
122
122
|
if(Validator.isObservable(firstElementFromOriginalValue) || Validator.isProxy(firstElementFromOriginalValue)) {
|
|
123
123
|
const newValues = newValue.map(item => {
|
|
124
124
|
if(Validator.isProxy(firstElementFromOriginalValue)) {
|
|
125
|
-
return
|
|
125
|
+
return new ObservableObject(item, configs);
|
|
126
126
|
}
|
|
127
|
-
return
|
|
127
|
+
return ObservableItem(item, configs);
|
|
128
128
|
});
|
|
129
129
|
targetItem.set(newValues);
|
|
130
130
|
continue;
|
|
@@ -152,7 +152,7 @@ ObservableObject.prototype.keys = function() {
|
|
|
152
152
|
};
|
|
153
153
|
ObservableObject.prototype.$keys = ObservableObject.prototype.keys;
|
|
154
154
|
ObservableObject.prototype.clone = function() {
|
|
155
|
-
return
|
|
155
|
+
return new ObservableObject(this.val(), this.configs);
|
|
156
156
|
};
|
|
157
157
|
ObservableObject.prototype.$clone = ObservableObject.prototype.clone;
|
|
158
158
|
ObservableObject.prototype.reset = function() {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import ObservableItem from "../ObservableItem";
|
|
2
|
-
import { Observable } from "../Observable";
|
|
3
2
|
|
|
4
|
-
const $computed = (fn, dependencies) =>
|
|
3
|
+
const $computed = (fn, dependencies) => ObservableItem.computed(fn, dependencies);
|
|
5
4
|
const $checker = (obs, fn) => obs.transform(fn);
|
|
6
5
|
|
|
7
6
|
//
|
|
@@ -15,7 +15,6 @@ import ObservableItem from "../ObservableItem";
|
|
|
15
15
|
import ObservableChecker from "../ObservableChecker";
|
|
16
16
|
import {Formatters} from "../../utils/formatters";
|
|
17
17
|
import NativeDocumentError from "../../errors/NativeDocumentError";
|
|
18
|
-
import {Observable} from "../Observable";
|
|
19
18
|
|
|
20
19
|
ObservableItem.prototype.when = function(value) {
|
|
21
20
|
return new ObservableWhen(this, value);
|
|
@@ -113,7 +112,7 @@ ObservableItem.prototype.format = function(type, options = {}) {
|
|
|
113
112
|
const formatter = Formatters[type];
|
|
114
113
|
const localeObservable = Formatters.locale;
|
|
115
114
|
|
|
116
|
-
return
|
|
115
|
+
return ObservableItem.computed(() => formatter(self.val(), localeObservable.val(), options),
|
|
117
116
|
[self, localeObservable]
|
|
118
117
|
);
|
|
119
118
|
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {Observable} from "../Observable";
|
|
2
|
-
import ObservableArray from "../ObservableArray";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Creates an observable array with reactive array methods.
|
|
7
|
-
* All mutations trigger updates automatically.
|
|
8
|
-
*
|
|
9
|
-
* @param {Array} [target=[]] - Initial array value
|
|
10
|
-
* @param {Object|null} [configs=null] - Configuration options
|
|
11
|
-
* // @param {boolean} [configs.propagation=true] - Whether to propagate changes to parent observables
|
|
12
|
-
* // @param {boolean} [configs.deep=false] - Whether to make nested objects observable
|
|
13
|
-
* @param {boolean} [configs.reset=false] - Whether to store initial value for reset()
|
|
14
|
-
* @returns {ObservableArray} An observable array with reactive methods
|
|
15
|
-
* @example
|
|
16
|
-
* const items = Observable.array([1, 2, 3]);
|
|
17
|
-
* items.push(4); // Triggers update
|
|
18
|
-
* items.subscribe((arr) => console.log(arr));
|
|
19
|
-
*/
|
|
20
|
-
Observable.array = function(target = [], configs = null) {
|
|
21
|
-
return new ObservableArray(target, configs);
|
|
22
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import Validator from "../../../core/utils/validator";
|
|
2
|
-
import {Observable} from "../Observable";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* @param {Function} callback
|
|
7
|
-
* @returns {Function}
|
|
8
|
-
*/
|
|
9
|
-
Observable.batch = function(callback) {
|
|
10
|
-
const $observer = Observable(0);
|
|
11
|
-
const batch = function() {
|
|
12
|
-
if(Validator.isAsyncFunction(callback)) {
|
|
13
|
-
return (callback(...arguments)).then(() => {
|
|
14
|
-
$observer.trigger();
|
|
15
|
-
}).catch(error => { throw error; });
|
|
16
|
-
}
|
|
17
|
-
callback(...arguments);
|
|
18
|
-
$observer.trigger();
|
|
19
|
-
};
|
|
20
|
-
batch.$observer = $observer;
|
|
21
|
-
return batch;
|
|
22
|
-
};
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import ObservableItem from "../ObservableItem";
|
|
2
|
-
import Validator from "../../utils/validator";
|
|
3
|
-
import NativeDocumentError from "../..//errors/NativeDocumentError";
|
|
4
|
-
import {Observable} from "../Observable";
|
|
5
|
-
import PluginsManager from "../../utils/plugins-manager";
|
|
6
|
-
import {nextTick} from "../../utils/helpers";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates a computed observable that automatically updates when its dependencies change.
|
|
10
|
-
* The callback is re-executed whenever any dependency observable changes.
|
|
11
|
-
*
|
|
12
|
-
* @param {Function} callback - Function that returns the computed value
|
|
13
|
-
* @param {Array<ObservableItem|ObservableChecker|ObservableProxy>|Function} [dependencies=[]] - Array of observables to watch, or batch function
|
|
14
|
-
* @returns {ObservableItem} A new observable that updates automatically
|
|
15
|
-
* @example
|
|
16
|
-
* const firstName = Observable('John');
|
|
17
|
-
* const lastName = Observable('Doe');
|
|
18
|
-
* const fullName = Observable.computed(
|
|
19
|
-
* () => `${firstName.val()} ${lastName.val()}`,
|
|
20
|
-
* [firstName, lastName]
|
|
21
|
-
* );
|
|
22
|
-
*
|
|
23
|
-
* // With batch function
|
|
24
|
-
* const batch = Observable.batch(() => { ... });
|
|
25
|
-
* const computed = Observable.computed(() => { ... }, batch);
|
|
26
|
-
*/
|
|
27
|
-
Observable.computed = function(callback, dependencies = []) {
|
|
28
|
-
const initialValue = callback();
|
|
29
|
-
const observable = new ObservableItem(initialValue);
|
|
30
|
-
const getValues = () => dependencies.map((item) => item.val());
|
|
31
|
-
const updatedValue = nextTick(() => observable.set(callback(...getValues())));
|
|
32
|
-
if(process.env.NODE_ENV === 'development') {
|
|
33
|
-
PluginsManager.emit('CreateObservableComputed', observable, dependencies);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if(Validator.isFunction(dependencies)) {
|
|
37
|
-
if(!Validator.isObservable(dependencies.$observer)) {
|
|
38
|
-
throw new NativeDocumentError('Observable.computed : dependencies must be valid batch function');
|
|
39
|
-
}
|
|
40
|
-
dependencies.$observer.subscribe(updatedValue);
|
|
41
|
-
return observable;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
dependencies.forEach(dependency => {
|
|
45
|
-
if(Validator.isProxy(dependency)) {
|
|
46
|
-
dependency.$observables.forEach((observable) => {
|
|
47
|
-
observable.subscribe(updatedValue);
|
|
48
|
-
});
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
dependency.subscribe(updatedValue);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
return observable;
|
|
55
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import Validator from "../../utils/validator";
|
|
2
|
-
import {Observable} from "../Observable";
|
|
3
|
-
import {ObservableObject} from "../ObservableObject";
|
|
4
|
-
import ObservableItem from "../ObservableItem";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Observable.init = function(initialValue, configs = null) {
|
|
8
|
-
return new ObservableObject(initialValue, configs)
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param {any[]} data
|
|
14
|
-
* @return Proxy[]
|
|
15
|
-
*/
|
|
16
|
-
Observable.arrayOfObject = function(data) {
|
|
17
|
-
return data.map(item => Observable.object(item));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Get the value of an observable or an object of observables.
|
|
22
|
-
* @param {ObservableItem|Object<ObservableItem>} data
|
|
23
|
-
* @returns {{}|*|null}
|
|
24
|
-
*/
|
|
25
|
-
Observable.value = function(data) {
|
|
26
|
-
if(data?.__$isObservableArray) {
|
|
27
|
-
const result = [];
|
|
28
|
-
for(let i = 0, length = data.length; i < length; i++) {
|
|
29
|
-
const item = data.at(i);
|
|
30
|
-
result.push(Observable.value(item));
|
|
31
|
-
}
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
if(data?.__$Observable) {
|
|
35
|
-
return data.val();
|
|
36
|
-
}
|
|
37
|
-
if(Validator.isProxy(data)) {
|
|
38
|
-
return data.$value;
|
|
39
|
-
}
|
|
40
|
-
return data;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
ObservableItem.prototype.resolve = function () {
|
|
44
|
-
return Observable.value(this);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
Observable.object = Observable.init;
|
|
48
|
-
Observable.json = Observable.init;
|