native-document 1.0.45 → 1.0.47
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/dist/native-document.dev.js +95 -34
- package/dist/native-document.dev.js.map +1 -1
- package/dist/native-document.devtools.min.js +1 -1
- package/dist/native-document.min.js +1 -1
- package/package.json +1 -1
- package/src/data/ObservableItem.js +5 -1
- package/src/data/observable-helpers/array.js +1 -1
- package/src/data/observable-helpers/object.js +91 -25
- package/src/wrappers/ElementCreator.js +4 -0
- package/types/observable.d.ts +2 -0
|
@@ -438,7 +438,11 @@ var NativeDocument = (function (exports) {
|
|
|
438
438
|
ObservableItem.prototype.check = function(callback) {
|
|
439
439
|
return new ObservableChecker(this, callback)
|
|
440
440
|
};
|
|
441
|
-
|
|
441
|
+
|
|
442
|
+
ObservableItem.prototype.get = function(key) {
|
|
443
|
+
const item = this.$currentValue[key];
|
|
444
|
+
return Validator.isObservable(item) ? item.val() : item;
|
|
445
|
+
};
|
|
442
446
|
|
|
443
447
|
ObservableItem.prototype.when = function(value) {
|
|
444
448
|
return {$target: value, $observer: this};
|
|
@@ -990,11 +994,6 @@ var NativeDocument = (function (exports) {
|
|
|
990
994
|
};
|
|
991
995
|
}
|
|
992
996
|
|
|
993
|
-
var validator = /*#__PURE__*/Object.freeze({
|
|
994
|
-
__proto__: null,
|
|
995
|
-
default: Validator
|
|
996
|
-
});
|
|
997
|
-
|
|
998
997
|
function Anchor(name, isUniqueChild = false) {
|
|
999
998
|
const element = document.createDocumentFragment();
|
|
1000
999
|
element.__Anchor__ = true;
|
|
@@ -1273,7 +1272,7 @@ var NativeDocument = (function (exports) {
|
|
|
1273
1272
|
}
|
|
1274
1273
|
if(Validator.isObservable(value)) {
|
|
1275
1274
|
if(['checked'].includes(attributeName)) {
|
|
1276
|
-
element.addEventListener('input', updateObserverFromInput.bind(null, element, attributeName, defaultValue));
|
|
1275
|
+
element.addEventListener('input', updateObserverFromInput.bind(null, element, attributeName, defaultValue, value));
|
|
1277
1276
|
}
|
|
1278
1277
|
value.subscribe(updateInputFromObserver.bind(null, element, attributeName));
|
|
1279
1278
|
}
|
|
@@ -1464,6 +1463,9 @@ var NativeDocument = (function (exports) {
|
|
|
1464
1463
|
if(Validator.isNDElement(child)) {
|
|
1465
1464
|
return child.$element ?? child.$build?.() ?? null;
|
|
1466
1465
|
}
|
|
1466
|
+
if(Validator.$element) {
|
|
1467
|
+
return Validator.$element;
|
|
1468
|
+
}
|
|
1467
1469
|
if(Validator.isArray(child)) {
|
|
1468
1470
|
const fragment = document.createDocumentFragment();
|
|
1469
1471
|
for(let i = 0, length = child.length; i < length; i++) {
|
|
@@ -2037,7 +2039,7 @@ var NativeDocument = (function (exports) {
|
|
|
2037
2039
|
* @param {Array} target
|
|
2038
2040
|
* @returns {ObservableItem}
|
|
2039
2041
|
*/
|
|
2040
|
-
Observable.array = function(target) {
|
|
2042
|
+
Observable.array = function(target, { propagation = false, deep = false } = {}) {
|
|
2041
2043
|
if(!Array.isArray(target)) {
|
|
2042
2044
|
throw new NativeDocumentError('Observable.array : target must be an array');
|
|
2043
2045
|
}
|
|
@@ -2154,43 +2156,86 @@ var NativeDocument = (function (exports) {
|
|
|
2154
2156
|
return batch;
|
|
2155
2157
|
};
|
|
2156
2158
|
|
|
2159
|
+
const ObservableObjectValue = function(data) {
|
|
2160
|
+
const result = {};
|
|
2161
|
+
for(const key in data) {
|
|
2162
|
+
const dataItem = data[key];
|
|
2163
|
+
if(Validator.isObservable(dataItem)) {
|
|
2164
|
+
let value = dataItem.val();
|
|
2165
|
+
if(Array.isArray(value)) {
|
|
2166
|
+
value = value.map(item => {
|
|
2167
|
+
if(Validator.isObservable(item)) {
|
|
2168
|
+
return item.val();
|
|
2169
|
+
}
|
|
2170
|
+
if(Validator.isProxy(item)) {
|
|
2171
|
+
return item.$value;
|
|
2172
|
+
}
|
|
2173
|
+
return item;
|
|
2174
|
+
});
|
|
2175
|
+
}
|
|
2176
|
+
result[key] = value;
|
|
2177
|
+
} else if(Validator.isProxy(dataItem)) {
|
|
2178
|
+
result[key] = dataItem.$value;
|
|
2179
|
+
} else {
|
|
2180
|
+
result[key] = dataItem;
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
return result;
|
|
2184
|
+
};
|
|
2185
|
+
|
|
2186
|
+
const ObservableGet = function(target, property) {
|
|
2187
|
+
const item = target[property];
|
|
2188
|
+
if(Validator.isObservable(item)) {
|
|
2189
|
+
return item.val();
|
|
2190
|
+
}
|
|
2191
|
+
if(Validator.isProxy(item)) {
|
|
2192
|
+
return item.$value;
|
|
2193
|
+
}
|
|
2194
|
+
return item;
|
|
2195
|
+
};
|
|
2196
|
+
|
|
2157
2197
|
/**
|
|
2158
2198
|
*
|
|
2159
2199
|
* @param {Object} initialValue
|
|
2160
2200
|
* @returns {Proxy}
|
|
2161
2201
|
*/
|
|
2162
|
-
Observable.init = function(initialValue) {
|
|
2202
|
+
Observable.init = function(initialValue, { propagation= false, deep = true } = {}) {
|
|
2163
2203
|
const data = {};
|
|
2164
2204
|
for(const key in initialValue) {
|
|
2165
2205
|
const itemValue = initialValue[key];
|
|
2166
2206
|
if(Array.isArray(itemValue)) {
|
|
2167
|
-
|
|
2207
|
+
if(deep) {
|
|
2208
|
+
data[key] = itemValue.map(item => {
|
|
2209
|
+
if(Validator.isJson(item)) {
|
|
2210
|
+
return Observable.json(item, { propagation, deep });
|
|
2211
|
+
}
|
|
2212
|
+
if(Validator.isArray(item)) {
|
|
2213
|
+
return Observable.array(item, { propagation, deep });
|
|
2214
|
+
}
|
|
2215
|
+
return Observable(item);
|
|
2216
|
+
});
|
|
2217
|
+
continue;
|
|
2218
|
+
}
|
|
2219
|
+
data[key] = Observable.array(itemValue, { propagation });
|
|
2220
|
+
continue;
|
|
2221
|
+
}
|
|
2222
|
+
if(Validator.isObservable(itemValue) || Validator.isProxy(itemValue)) {
|
|
2223
|
+
data[key] = itemValue;
|
|
2168
2224
|
continue;
|
|
2169
2225
|
}
|
|
2170
2226
|
data[key] = Observable(itemValue);
|
|
2171
2227
|
}
|
|
2172
2228
|
|
|
2173
|
-
const $val =
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
result[key] = dataItem.val();
|
|
2179
|
-
} else if(Validator.isProxy(dataItem)) {
|
|
2180
|
-
result[key] = dataItem.$value;
|
|
2181
|
-
} else {
|
|
2182
|
-
result[key] = dataItem;
|
|
2183
|
-
}
|
|
2184
|
-
}
|
|
2185
|
-
return result;
|
|
2186
|
-
};
|
|
2187
|
-
const $clone = function() {
|
|
2188
|
-
return Observable.init($val());
|
|
2189
|
-
};
|
|
2190
|
-
const $updateWith = function(values) {
|
|
2229
|
+
const $val = () => ObservableObjectValue(data);
|
|
2230
|
+
|
|
2231
|
+
const $clone = () => Observable.init($val(), { propagation, deep });
|
|
2232
|
+
|
|
2233
|
+
const $updateWith = (values) => {
|
|
2191
2234
|
Observable.update(proxy, values);
|
|
2192
2235
|
};
|
|
2193
2236
|
|
|
2237
|
+
const $get = (key) => ObservableGet(data, key);
|
|
2238
|
+
|
|
2194
2239
|
const proxy = new Proxy(data, {
|
|
2195
2240
|
get(target, property) {
|
|
2196
2241
|
if(property === '__isProxy__') {
|
|
@@ -2202,12 +2247,18 @@ var NativeDocument = (function (exports) {
|
|
|
2202
2247
|
if(property === '$clone') {
|
|
2203
2248
|
return $clone;
|
|
2204
2249
|
}
|
|
2250
|
+
if(property === '$keys') {
|
|
2251
|
+
return Object.keys(initialValue);
|
|
2252
|
+
}
|
|
2205
2253
|
if(property === '$observables') {
|
|
2206
2254
|
return Object.values(target);
|
|
2207
2255
|
}
|
|
2208
|
-
if(property === '$updateWith') {
|
|
2256
|
+
if(property === '$updateWith' || property === '$set') {
|
|
2209
2257
|
return $updateWith;
|
|
2210
2258
|
}
|
|
2259
|
+
if(property === '$get') {
|
|
2260
|
+
return $get;
|
|
2261
|
+
}
|
|
2211
2262
|
if(target[property] !== undefined) {
|
|
2212
2263
|
return target[property];
|
|
2213
2264
|
}
|
|
@@ -2260,16 +2311,26 @@ var NativeDocument = (function (exports) {
|
|
|
2260
2311
|
};
|
|
2261
2312
|
|
|
2262
2313
|
|
|
2263
|
-
Observable.update = function($target,
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
}
|
|
2314
|
+
Observable.update = function($target, newData) {
|
|
2315
|
+
const data = Validator.isProxy(newData) ? newData.$value : newData;
|
|
2316
|
+
|
|
2267
2317
|
for(const key in data) {
|
|
2268
2318
|
const targetItem = $target[key];
|
|
2319
|
+
const newValueOrigin = newData[key];
|
|
2269
2320
|
const newValue = data[key];
|
|
2270
2321
|
|
|
2271
2322
|
if(Validator.isObservable(targetItem)) {
|
|
2272
2323
|
if(Validator.isArray(newValue)) {
|
|
2324
|
+
if(Validator.isObservable(newValueOrigin[0]) || Validator.isProxy(newValueOrigin[0])) {
|
|
2325
|
+
const newValues = newValue.map(item => {
|
|
2326
|
+
if(Validator.isObservable(newValueOrigin[0])) {
|
|
2327
|
+
return Observable(item);
|
|
2328
|
+
}
|
|
2329
|
+
return Observable.init(newValueOrigin[0]);
|
|
2330
|
+
});
|
|
2331
|
+
targetItem.set(newValues);
|
|
2332
|
+
continue;
|
|
2333
|
+
}
|
|
2273
2334
|
targetItem.set([...newValue]);
|
|
2274
2335
|
continue;
|
|
2275
2336
|
}
|
|
@@ -4017,7 +4078,7 @@ var NativeDocument = (function (exports) {
|
|
|
4017
4078
|
exports.SingletonView = SingletonView;
|
|
4018
4079
|
exports.Store = Store;
|
|
4019
4080
|
exports.TemplateCloner = TemplateCloner;
|
|
4020
|
-
exports.Validator =
|
|
4081
|
+
exports.Validator = Validator;
|
|
4021
4082
|
exports.classPropertyAccumulator = classPropertyAccumulator;
|
|
4022
4083
|
exports.createTextNode = createTextNode;
|
|
4023
4084
|
exports.cssPropertyAccumulator = cssPropertyAccumulator;
|