native-document 1.0.98 → 1.0.99

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.
@@ -1168,7 +1168,7 @@ var NativeDocument = (function (exports) {
1168
1168
  return cloned;
1169
1169
  };
1170
1170
 
1171
- const LocalStorage$1 = {
1171
+ const LocalStorage = {
1172
1172
  getJson(key) {
1173
1173
  let value = localStorage.getItem(key);
1174
1174
  try {
@@ -1204,23 +1204,23 @@ var NativeDocument = (function (exports) {
1204
1204
  }
1205
1205
  };
1206
1206
 
1207
- const $getFromStorage$1 = (key, value) => {
1208
- if(!LocalStorage$1.has(key)) {
1207
+ const $getFromStorage = (key, value) => {
1208
+ if(!LocalStorage.has(key)) {
1209
1209
  return value;
1210
1210
  }
1211
1211
  switch (typeof value) {
1212
- case 'object': return LocalStorage$1.getJson(key) ?? value;
1213
- case 'boolean': return LocalStorage$1.getBool(key) ?? value;
1214
- case 'number': return LocalStorage$1.getNumber(key) ?? value;
1215
- default: return LocalStorage$1.get(key, value) ?? value;
1212
+ case 'object': return LocalStorage.getJson(key) ?? value;
1213
+ case 'boolean': return LocalStorage.getBool(key) ?? value;
1214
+ case 'number': return LocalStorage.getNumber(key) ?? value;
1215
+ default: return LocalStorage.get(key, value) ?? value;
1216
1216
  }
1217
1217
  };
1218
1218
 
1219
- const $saveToStorage$1 = (value) => {
1219
+ const $saveToStorage = (value) => {
1220
1220
  switch (typeof value) {
1221
- case 'object': return LocalStorage$1.setJson;
1222
- case 'boolean': return LocalStorage$1.setBool;
1223
- default: return LocalStorage$1.set;
1221
+ case 'object': return LocalStorage.setJson;
1222
+ case 'boolean': return LocalStorage.setBool;
1223
+ default: return LocalStorage.set;
1224
1224
  }
1225
1225
  };
1226
1226
 
@@ -1260,12 +1260,12 @@ var NativeDocument = (function (exports) {
1260
1260
 
1261
1261
  const $createObservable = (value, options = {}) => {
1262
1262
  if(Array.isArray(value)) {
1263
- return Observable$1.array(value, options);
1263
+ return Observable.array(value, options);
1264
1264
  }
1265
1265
  if(typeof value === 'object') {
1266
- return Observable$1.object(value, options);
1266
+ return Observable.object(value, options);
1267
1267
  }
1268
- return Observable$1(value, options);
1268
+ return Observable(value, options);
1269
1269
  };
1270
1270
 
1271
1271
  const $api = {
@@ -1368,7 +1368,7 @@ var NativeDocument = (function (exports) {
1368
1368
  });
1369
1369
 
1370
1370
  // Create computed observable from dependency observers
1371
- const observer = Observable$1.computed(computation, depObservers);
1371
+ const observer = Observable.computed(computation, depObservers);
1372
1372
 
1373
1373
  $stores.set(name, { observer, subscribers: new Set(), resettable: false, composed: true });
1374
1374
  return observer;
@@ -1572,21 +1572,21 @@ var NativeDocument = (function (exports) {
1572
1572
  },
1573
1573
  createPersistent(name, value, localstorage_key) {
1574
1574
  localstorage_key = localstorage_key || name;
1575
- const observer = this.create(name, $getFromStorage$1(localstorage_key, value));
1576
- const saver = $saveToStorage$1(value);
1575
+ const observer = this.create(name, $getFromStorage(localstorage_key, value));
1576
+ const saver = $saveToStorage(value);
1577
1577
 
1578
1578
  observer.subscribe((val) => saver(localstorage_key, val));
1579
1579
  return observer;
1580
1580
  },
1581
1581
  createPersistentResettable(name, value, localstorage_key) {
1582
1582
  localstorage_key = localstorage_key || name;
1583
- const observer = this.createResettable(name, $getFromStorage$1(localstorage_key, value));
1584
- const saver = $saveToStorage$1(value);
1583
+ const observer = this.createResettable(name, $getFromStorage(localstorage_key, value));
1584
+ const saver = $saveToStorage(value);
1585
1585
  observer.subscribe((val) => saver(localstorage_key, val));
1586
1586
 
1587
1587
  const originalReset = observer.reset.bind(observer);
1588
1588
  observer.reset = () => {
1589
- LocalStorage$1.remove(localstorage_key);
1589
+ LocalStorage.remove(localstorage_key);
1590
1590
  originalReset();
1591
1591
  };
1592
1592
 
@@ -2268,18 +2268,18 @@ var NativeDocument = (function (exports) {
2268
2268
  const formatter = Formatters[type];
2269
2269
  const localeObservable = Store.follow('locale');
2270
2270
 
2271
- return Observable$1.computed(() => formatter(self.val(), localeObservable.val(), options),
2271
+ return Observable.computed(() => formatter(self.val(), localeObservable.val(), options),
2272
2272
  [self, localeObservable]
2273
2273
  );
2274
2274
  };
2275
2275
 
2276
2276
  ObservableItem.prototype.persist = function(key, options = {}) {
2277
- let value = $getFromStorage$1(key, this.$currentValue);
2277
+ let value = $getFromStorage(key, this.$currentValue);
2278
2278
  if(options.get) {
2279
2279
  value = options.get(value);
2280
2280
  }
2281
2281
  this.set(value);
2282
- const saver = $saveToStorage$1(this.$currentValue);
2282
+ const saver = $saveToStorage(this.$currentValue);
2283
2283
  this.subscribe((newValue) => {
2284
2284
  saver(key, options.set ? options.set(newValue) : newValue);
2285
2285
  });
@@ -2293,18 +2293,18 @@ var NativeDocument = (function (exports) {
2293
2293
  * @returns {ObservableItem}
2294
2294
  * @constructor
2295
2295
  */
2296
- function Observable$1(value, configs = null) {
2296
+ function Observable(value, configs = null) {
2297
2297
  return new ObservableItem(value, configs);
2298
2298
  }
2299
2299
 
2300
- const $ = Observable$1;
2301
- const obs = Observable$1;
2300
+ const $ = Observable;
2301
+ const obs = Observable;
2302
2302
 
2303
2303
  /**
2304
2304
  *
2305
2305
  * @param {string} propertyName
2306
2306
  */
2307
- Observable$1.useValueProperty = function(propertyName = 'value') {
2307
+ Observable.useValueProperty = function(propertyName = 'value') {
2308
2308
  Object.defineProperty(ObservableItem.prototype, propertyName, {
2309
2309
  get() {
2310
2310
  return this.$currentValue;
@@ -2322,7 +2322,7 @@ var NativeDocument = (function (exports) {
2322
2322
  * @param id
2323
2323
  * @returns {ObservableItem|null}
2324
2324
  */
2325
- Observable$1.getById = function(id) {
2325
+ Observable.getById = function(id) {
2326
2326
  const item = MemoryManager.getObservableById(parseInt(id));
2327
2327
  if(!item) {
2328
2328
  throw new NativeDocumentError('Observable.getById : No observable found with id ' + id);
@@ -2334,7 +2334,7 @@ var NativeDocument = (function (exports) {
2334
2334
  *
2335
2335
  * @param {ObservableItem} observable
2336
2336
  */
2337
- Observable$1.cleanup = function(observable) {
2337
+ Observable.cleanup = function(observable) {
2338
2338
  observable.cleanup();
2339
2339
  };
2340
2340
 
@@ -2343,7 +2343,7 @@ var NativeDocument = (function (exports) {
2343
2343
  * @param {Boolean} enable
2344
2344
  * @param {{interval:Boolean, threshold:number}} options
2345
2345
  */
2346
- Observable$1.autoCleanup = function(enable = false, options = {}) {
2346
+ Observable.autoCleanup = function(enable = false, options = {}) {
2347
2347
  if(!enable) {
2348
2348
  return;
2349
2349
  }
@@ -3497,7 +3497,7 @@ var NativeDocument = (function (exports) {
3497
3497
  String.prototype.use = function(args) {
3498
3498
  const value = this;
3499
3499
 
3500
- return Observable$1.computed(() => {
3500
+ return Observable.computed(() => {
3501
3501
  return value.replace(/\$\{(.*?)}/g, (match, key) => {
3502
3502
  const data = args[key];
3503
3503
  if(Validator.isObservable(data)) {
@@ -3517,7 +3517,7 @@ var NativeDocument = (function (exports) {
3517
3517
  return value;
3518
3518
  }
3519
3519
  const [_, id] = value.match(/\{\{#ObItem::\(([0-9]+)\)\}\}/);
3520
- return Observable$1.getById(id);
3520
+ return Observable.getById(id);
3521
3521
  });
3522
3522
  };
3523
3523
 
@@ -4210,7 +4210,7 @@ var NativeDocument = (function (exports) {
4210
4210
  }
4211
4211
  }
4212
4212
 
4213
- const viewArray = Observable$1.array();
4213
+ const viewArray = Observable.array();
4214
4214
 
4215
4215
  const filters = Object.entries(filterCallbacks);
4216
4216
  const updateView = () => {
@@ -4281,6 +4281,68 @@ var NativeDocument = (function (exports) {
4281
4281
  });
4282
4282
  };
4283
4283
 
4284
+ ObservableArray.prototype.deepSubscribe = function(callback) {
4285
+ const updatedValue = nextTick(() => callback(this.val()));
4286
+ const $listeners = new WeakMap();
4287
+
4288
+ const bindItem = (item) => {
4289
+ if ($listeners.has(item)) {
4290
+ return;
4291
+ }
4292
+ if (item?.__$isObservableArray) {
4293
+ $listeners.set(item, item.deepSubscribe(updatedValue));
4294
+ return;
4295
+ }
4296
+ if (item?.__$isObservable) {
4297
+ item.subscribe(updatedValue);
4298
+ $listeners.set(item, () => item.unsubscribe(updatedValue));
4299
+ }
4300
+ };
4301
+
4302
+ const unbindItem = (item) => {
4303
+ const unsub = $listeners.get(item);
4304
+ if (unsub) {
4305
+ unsub();
4306
+ $listeners.delete(item);
4307
+ }
4308
+ };
4309
+
4310
+ this.$currentValue.forEach(bindItem);
4311
+ this.subscribe(updatedValue);
4312
+
4313
+ this.subscribe((items, _, operations) => {
4314
+ switch (operations?.action) {
4315
+ case 'push':
4316
+ case 'unshift':
4317
+ operations.args.forEach(bindItem);
4318
+ break;
4319
+
4320
+ case 'splice': {
4321
+ const [start, deleteCount, ...newItems] = operations.args;
4322
+ operations.result?.forEach(unbindItem);
4323
+ newItems.forEach(bindItem);
4324
+ break;
4325
+ }
4326
+
4327
+ case 'remove':
4328
+ unbindItem(operations.result);
4329
+ break;
4330
+
4331
+ case 'merge':
4332
+ operations.args.forEach(bindItem);
4333
+ break;
4334
+
4335
+ case 'clear':
4336
+ this.$currentValue.forEach(unbindItem);
4337
+ break;
4338
+ }
4339
+ });
4340
+
4341
+ return () => {
4342
+ this.$currentValue.forEach(unbindItem);
4343
+ };
4344
+ };
4345
+
4284
4346
  /**
4285
4347
  * Creates an observable array with reactive array methods.
4286
4348
  * All mutations trigger updates automatically.
@@ -4296,7 +4358,7 @@ var NativeDocument = (function (exports) {
4296
4358
  * items.push(4); // Triggers update
4297
4359
  * items.subscribe((arr) => console.log(arr));
4298
4360
  */
4299
- Observable$1.array = function(target = [], configs = null) {
4361
+ Observable.array = function(target = [], configs = null) {
4300
4362
  return new ObservableArray(target, configs);
4301
4363
  };
4302
4364
 
@@ -4305,8 +4367,8 @@ var NativeDocument = (function (exports) {
4305
4367
  * @param {Function} callback
4306
4368
  * @returns {Function}
4307
4369
  */
4308
- Observable$1.batch = function(callback) {
4309
- const $observer = Observable$1(0);
4370
+ Observable.batch = function(callback) {
4371
+ const $observer = Observable(0);
4310
4372
  const batch = function() {
4311
4373
  if(Validator.isAsyncFunction(callback)) {
4312
4374
  return (callback(...arguments)).then(() => {
@@ -4360,24 +4422,24 @@ var NativeDocument = (function (exports) {
4360
4422
  if(configs?.deep !== false) {
4361
4423
  const mappedItemValue = itemValue.map(item => {
4362
4424
  if(Validator.isJson(item)) {
4363
- return Observable$1.json(item, configs);
4425
+ return Observable.json(item, configs);
4364
4426
  }
4365
4427
  if(Validator.isArray(item)) {
4366
- return Observable$1.array(item, configs);
4428
+ return Observable.array(item, configs);
4367
4429
  }
4368
- return Observable$1(item, configs);
4430
+ return Observable(item, configs);
4369
4431
  });
4370
- this.$observables[key] = Observable$1.array(mappedItemValue, configs);
4432
+ this.$observables[key] = Observable.array(mappedItemValue, configs);
4371
4433
  continue;
4372
4434
  }
4373
- this.$observables[key] = Observable$1.array(itemValue, configs);
4435
+ this.$observables[key] = Observable.array(itemValue, configs);
4374
4436
  continue;
4375
4437
  }
4376
4438
  if(Validator.isObservable(itemValue) || Validator.isProxy(itemValue)) {
4377
4439
  this.$observables[key] = itemValue;
4378
4440
  continue;
4379
4441
  }
4380
- this.$observables[key] = Observable$1(itemValue, configs);
4442
+ this.$observables[key] = (typeof itemValue === 'object') ? Observable.object(itemValue, configs) : Observable(itemValue, configs);
4381
4443
  }
4382
4444
  };
4383
4445
 
@@ -4439,9 +4501,9 @@ var NativeDocument = (function (exports) {
4439
4501
  if(Validator.isObservable(firstElementFromOriginalValue) || Validator.isProxy(firstElementFromOriginalValue)) {
4440
4502
  const newValues = newValue.map(item => {
4441
4503
  if(Validator.isProxy(firstElementFromOriginalValue)) {
4442
- return Observable$1.init(item, configs);
4504
+ return Observable.init(item, configs);
4443
4505
  }
4444
- return Observable$1(item, configs);
4506
+ return Observable(item, configs);
4445
4507
  });
4446
4508
  targetItem.set(newValues);
4447
4509
  continue;
@@ -4469,7 +4531,7 @@ var NativeDocument = (function (exports) {
4469
4531
  };
4470
4532
  ObservableObject.prototype.$keys = ObservableObject.prototype.keys;
4471
4533
  ObservableObject.prototype.clone = function() {
4472
- return Observable$1.init(this.val(), this.configs);
4534
+ return Observable.init(this.val(), this.configs);
4473
4535
  };
4474
4536
  ObservableObject.prototype.$clone = ObservableObject.prototype.clone;
4475
4537
  ObservableObject.prototype.reset = function() {
@@ -4480,15 +4542,16 @@ var NativeDocument = (function (exports) {
4480
4542
  ObservableObject.prototype.originalSubscribe = ObservableObject.prototype.subscribe;
4481
4543
  ObservableObject.prototype.subscribe = function(callback) {
4482
4544
  const observables = this.observables();
4483
- const updatedValue = nextTick(() => {
4484
- this.$currentValue = this.val();
4485
- this.trigger();
4486
- });
4545
+ const updatedValue = nextTick(() => this.trigger());
4487
4546
 
4488
4547
  this.originalSubscribe(callback);
4489
4548
 
4490
- for(let i = 0, length = observables.length; i < length; i++) {
4549
+ for (let i = 0, length = observables.length; i < length; i++) {
4491
4550
  const observable = observables[i];
4551
+ if (observable.__$isObservableArray) {
4552
+ observable.deepSubscribe(updatedValue);
4553
+ continue
4554
+ }
4492
4555
  observable.subscribe(updatedValue);
4493
4556
  }
4494
4557
  };
@@ -4498,7 +4561,7 @@ var NativeDocument = (function (exports) {
4498
4561
 
4499
4562
  ObservableObject.prototype.update = ObservableObject.prototype.set;
4500
4563
 
4501
- Observable$1.init = function(initialValue, configs = null) {
4564
+ Observable.init = function(initialValue, configs = null) {
4502
4565
  return new ObservableObject(initialValue, configs)
4503
4566
  };
4504
4567
 
@@ -4507,8 +4570,8 @@ var NativeDocument = (function (exports) {
4507
4570
  * @param {any[]} data
4508
4571
  * @return Proxy[]
4509
4572
  */
4510
- Observable$1.arrayOfObject = function(data) {
4511
- return data.map(item => Observable$1.object(item));
4573
+ Observable.arrayOfObject = function(data) {
4574
+ return data.map(item => Observable.object(item));
4512
4575
  };
4513
4576
 
4514
4577
  /**
@@ -4516,7 +4579,7 @@ var NativeDocument = (function (exports) {
4516
4579
  * @param {ObservableItem|Object<ObservableItem>} data
4517
4580
  * @returns {{}|*|null}
4518
4581
  */
4519
- Observable$1.value = function(data) {
4582
+ Observable.value = function(data) {
4520
4583
  if(Validator.isObservable(data)) {
4521
4584
  return data.val();
4522
4585
  }
@@ -4527,15 +4590,15 @@ var NativeDocument = (function (exports) {
4527
4590
  const result = [];
4528
4591
  for(let i = 0, length = data.length; i < length; i++) {
4529
4592
  const item = data[i];
4530
- result.push(Observable$1.value(item));
4593
+ result.push(Observable.value(item));
4531
4594
  }
4532
4595
  return result;
4533
4596
  }
4534
4597
  return data;
4535
4598
  };
4536
4599
 
4537
- Observable$1.object = Observable$1.init;
4538
- Observable$1.json = Observable$1.init;
4600
+ Observable.object = Observable.init;
4601
+ Observable.json = Observable.init;
4539
4602
 
4540
4603
  /**
4541
4604
  * Creates a computed observable that automatically updates when its dependencies change.
@@ -4556,7 +4619,7 @@ var NativeDocument = (function (exports) {
4556
4619
  * const batch = Observable.batch(() => { ... });
4557
4620
  * const computed = Observable.computed(() => { ... }, batch);
4558
4621
  */
4559
- Observable$1.computed = function(callback, dependencies = []) {
4622
+ Observable.computed = function(callback, dependencies = []) {
4560
4623
  const initialValue = callback();
4561
4624
  const observable = new ObservableItem(initialValue);
4562
4625
  const updatedValue = nextTick(() => observable.set(callback()));
@@ -4653,7 +4716,7 @@ var NativeDocument = (function (exports) {
4653
4716
  }
4654
4717
 
4655
4718
  try {
4656
- const indexObserver = callback.length >= 2 ? Observable$1(indexKey) : null;
4719
+ const indexObserver = callback.length >= 2 ? Observable(indexKey) : null;
4657
4720
  let child = ElementCreator.getChild(callback(item, indexObserver));
4658
4721
  if(!child) {
4659
4722
  throw new NativeDocumentError("ForEach child can't be null or undefined!");
@@ -4835,7 +4898,7 @@ var NativeDocument = (function (exports) {
4835
4898
  cache.delete(item);
4836
4899
  }
4837
4900
 
4838
- const indexObserver = isIndexRequired ? Observable$1(indexKey) : null;
4901
+ const indexObserver = isIndexRequired ? Observable(indexKey) : null;
4839
4902
  let child = ElementCreator.getChild(callback(item, indexObserver));
4840
4903
  if(child) {
4841
4904
  cache.set(item, {
@@ -5077,7 +5140,7 @@ var NativeDocument = (function (exports) {
5077
5140
  * HideIf(hasError, Div({}, 'Content'));
5078
5141
  */
5079
5142
  const HideIf = function(condition, child, configs) {
5080
- const hideCondition = Observable$1(!condition.val());
5143
+ const hideCondition = Observable(!condition.val());
5081
5144
  condition.subscribe(value => hideCondition.set(!value));
5082
5145
 
5083
5146
  return ShowIf(hideCondition, child, configs);
@@ -7084,7 +7147,7 @@ var NativeDocument = (function (exports) {
7084
7147
  exports.ElementCreator = ElementCreator;
7085
7148
  exports.HtmlElementWrapper = HtmlElementWrapper;
7086
7149
  exports.NDElement = NDElement;
7087
- exports.Observable = Observable$1;
7150
+ exports.Observable = Observable;
7088
7151
  exports.PluginsManager = PluginsManager;
7089
7152
  exports.SingletonView = SingletonView;
7090
7153
  exports.Store = Store;