native-document 1.0.28 → 1.0.30

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.
@@ -141,6 +141,76 @@ var NativeDocument = (function (exports) {
141
141
  return this.observable.cleanup();
142
142
  };
143
143
 
144
+ const PluginsManager = (function() {
145
+
146
+ const $plugins = new Map();
147
+ const $pluginByEvents = new Map();
148
+
149
+ return {
150
+ list() {
151
+ return $pluginByEvents;
152
+ },
153
+ add(name, plugin){
154
+ if (!plugin || typeof plugin !== 'object') {
155
+ throw new Error(`Plugin ${name} must be an object`);
156
+ }
157
+ if($plugins.has(name)) {
158
+ return;
159
+ }
160
+
161
+ plugin.$name = name;
162
+ $plugins.set(name ,plugin);
163
+ if(typeof plugin?.init === 'function') {
164
+ plugin.init();
165
+ }
166
+ for(const methodName in plugin) {
167
+ if(/^on[A-Z]/.test(methodName)) {
168
+ if(!$pluginByEvents.has(methodName)) {
169
+ $pluginByEvents.set(methodName, new Set());
170
+ }
171
+ $pluginByEvents.get(methodName).add(plugin);
172
+ }
173
+ }
174
+ },
175
+ remove(pluginName){
176
+ if(!$plugins.has(pluginName)) {
177
+ return;
178
+ }
179
+ const plugin = $plugins.get(pluginName);
180
+ if(typeof plugin.cleanup === 'function') {
181
+ plugin.cleanup();
182
+ }
183
+ for(const [name, sets] of $pluginByEvents.entries() ) {
184
+ if(sets.has(plugin)) {
185
+ sets.delete(plugin);
186
+ }
187
+ if(sets.size === 0) {
188
+ $pluginByEvents.delete(name);
189
+ }
190
+ }
191
+ $plugins.delete(pluginName);
192
+ },
193
+ emit(event, ...data) {
194
+ const eventMethodName = 'on'+event;
195
+ if(!$pluginByEvents.has(eventMethodName)) {
196
+ return;
197
+ }
198
+ const plugins = $pluginByEvents.get(eventMethodName);
199
+
200
+ for(const plugin of plugins) {
201
+ const callback = plugin[eventMethodName];
202
+ if(typeof callback === 'function') {
203
+ try{
204
+ callback.call(plugin, ...data);
205
+ } catch (error) {
206
+ DebugManager$1.error('Plugin Manager', `Error in plugin ${plugin.$name} for event ${eventMethodName}`, error);
207
+ }
208
+ }
209
+ }
210
+ }
211
+ };
212
+ }());
213
+
144
214
  /**
145
215
  *
146
216
  * @param {*} value
@@ -155,6 +225,7 @@ var NativeDocument = (function (exports) {
155
225
  this.$watchers = null;
156
226
 
157
227
  this.$memoryId = null;
228
+ PluginsManager.emit('CreateObservable', this);
158
229
  }
159
230
 
160
231
  Object.defineProperty(ObservableItem.prototype, '$value', {
@@ -236,7 +307,9 @@ var NativeDocument = (function (exports) {
236
307
  }
237
308
  this.$previousValue = this.$currentValue;
238
309
  this.$currentValue = newValue;
310
+ PluginsManager.emit('ObservableBeforeChange', this);
239
311
  this.trigger();
312
+ PluginsManager.emit('ObservableAfterChange', this);
240
313
  };
241
314
 
242
315
  ObservableItem.prototype.val = function() {
@@ -257,6 +330,7 @@ var NativeDocument = (function (exports) {
257
330
  this.$watchers = null;
258
331
  this.trigger = noneTrigger;
259
332
  };
333
+
260
334
  ObservableItem.prototype.cleanup = function() {
261
335
  MemoryManager.unregister(this.$memoryId);
262
336
  this.disconnectAll();
@@ -267,9 +341,10 @@ var NativeDocument = (function (exports) {
267
341
  /**
268
342
  *
269
343
  * @param {Function} callback
344
+ * @param {any} target
270
345
  * @returns {(function(): void)}
271
346
  */
272
- ObservableItem.prototype.subscribe = function(callback) {
347
+ ObservableItem.prototype.subscribe = function(callback, target = null) {
273
348
  this.$listeners = this.$listeners ?? [];
274
349
  if (this.$isCleanedUp) {
275
350
  DebugManager$1.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
@@ -281,9 +356,11 @@ var NativeDocument = (function (exports) {
281
356
 
282
357
  this.$listeners.push(callback);
283
358
  this.assocTrigger();
359
+ PluginsManager.emit('ObservableSubscribe', this, target);
284
360
  return () => {
285
361
  this.unsubscribe(callback);
286
362
  this.assocTrigger();
363
+ PluginsManager.emit('ObservableUnsubscribe', this);
287
364
  };
288
365
  };
289
366
 
@@ -526,6 +603,7 @@ var NativeDocument = (function (exports) {
526
603
  function NDElement(element) {
527
604
  this.$element = element;
528
605
  this.$observer = null;
606
+ PluginsManager.emit('NDElementCreated', element, this);
529
607
  }
530
608
  NDElement.prototype.__$isNDElement = true;
531
609
 
@@ -907,7 +985,7 @@ var NativeDocument = (function (exports) {
907
985
  continue;
908
986
  }
909
987
  if(value.$observer) {
910
- element.classList.toggle(className, value.$observer.val());
988
+ element.classList.toggle(className, value.$observer.val() === value.$target);
911
989
  value.$observer.on(value.$target, function(isTargetValue) {
912
990
  element.classList.toggle(className, isTargetValue);
913
991
  });
@@ -1103,11 +1181,15 @@ var NativeDocument = (function (exports) {
1103
1181
  if(children === null) return;
1104
1182
  const childrenArray = Array.isArray(children) ? children : [children];
1105
1183
 
1184
+ PluginsManager.emit('BeforeProcessChildren', parent);
1185
+
1106
1186
  for(let i = 0, length = childrenArray.length; i < length; i++) {
1107
1187
  let child = this.getChild(childrenArray[i]);
1108
1188
  if (child === null) continue;
1109
1189
  parent.appendChild(child);
1110
1190
  }
1191
+
1192
+ PluginsManager.emit('AfterProcessChildren', parent);
1111
1193
  },
1112
1194
  getChild(child) {
1113
1195
  if(child === null) {
@@ -1136,6 +1218,7 @@ var NativeDocument = (function (exports) {
1136
1218
  return fragment;
1137
1219
  }
1138
1220
  if(Validator.isFunction(child)) {
1221
+ PluginsManager.emit('BeforeProcessComponent', child);
1139
1222
  return this.getChild(child());
1140
1223
  }
1141
1224
  return ElementCreator.createStaticTextNode(null, child);
@@ -1159,6 +1242,7 @@ var NativeDocument = (function (exports) {
1159
1242
  * @returns {HTMLElement|DocumentFragment}
1160
1243
  */
1161
1244
  setup(element, attributes, customWrapper) {
1245
+ PluginsManager.emit('Setup', element, attributes, customWrapper);
1162
1246
  return element;
1163
1247
  }
1164
1248
  };
@@ -1308,6 +1392,15 @@ var NativeDocument = (function (exports) {
1308
1392
  };
1309
1393
  };
1310
1394
 
1395
+ const normalizeComponentArgs = function(props, children) {
1396
+ if(!Validator.isJson(children)) {
1397
+ const temp = props;
1398
+ props = children;
1399
+ children = temp;
1400
+ }
1401
+ return { props, children };
1402
+ };
1403
+
1311
1404
  Function.prototype.args = function(...args) {
1312
1405
  return withValidation(this, args);
1313
1406
  };
@@ -1349,6 +1442,48 @@ var NativeDocument = (function (exports) {
1349
1442
  });
1350
1443
  };
1351
1444
 
1445
+ const cssPropertyAccumulator = function(initialValue = {}) {
1446
+ let data = Validator.isString(initialValue) ? initialValue.split(';').filter(Boolean) : initialValue;
1447
+ const isArray = Validator.isArray(data);
1448
+
1449
+ return {
1450
+ add(key, value) {
1451
+ if(isArray) {
1452
+ data.push(key+' : '+value);
1453
+ return;
1454
+ }
1455
+ data[key] = value;
1456
+ },
1457
+ value() {
1458
+ if(isArray) {
1459
+ return data.join(';').concat(';');
1460
+ }
1461
+ return { ...data };
1462
+ },
1463
+ };
1464
+ };
1465
+
1466
+ const classPropertyAccumulator = function(initialValue = []) {
1467
+ let data = Validator.isString(initialValue) ? initialValue.split(" ").filter(Boolean) : initialValue;
1468
+ const isArray = Validator.isArray(data);
1469
+
1470
+ return {
1471
+ add(key, value = true) {
1472
+ if(isArray) {
1473
+ data.push(key);
1474
+ return;
1475
+ }
1476
+ data[key] = value;
1477
+ },
1478
+ value() {
1479
+ if(isArray) {
1480
+ return data.join(' ');
1481
+ }
1482
+ return { ...data };
1483
+ },
1484
+ };
1485
+ };
1486
+
1352
1487
  const methods = ['push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice'];
1353
1488
 
1354
1489
  /**
@@ -1362,6 +1497,8 @@ var NativeDocument = (function (exports) {
1362
1497
  }
1363
1498
  const observer = Observable(target);
1364
1499
 
1500
+ PluginsManager.emit('CreateObservableArray', observer);
1501
+
1365
1502
  methods.forEach((method) => {
1366
1503
  observer[method] = function(...values) {
1367
1504
  const result = observer.val()[method](...values);
@@ -1567,6 +1704,8 @@ var NativeDocument = (function (exports) {
1567
1704
  const observable = new ObservableItem(initialValue);
1568
1705
  const updatedValue = () => observable.set(callback());
1569
1706
 
1707
+ PluginsManager.emit('CreateObservableComputed', observable, dependencies);
1708
+
1570
1709
  if(Validator.isFunction(dependencies)) {
1571
1710
  if(!Validator.isObservable(dependencies.$observer)) {
1572
1711
  throw new NativeDocumentError('Observable.computed : dependencies must be valid batch function');
@@ -3243,7 +3382,10 @@ var NativeDocument = (function (exports) {
3243
3382
  exports.NDElement = NDElement;
3244
3383
  exports.Observable = Observable;
3245
3384
  exports.Store = Store;
3385
+ exports.classPropertyAccumulator = classPropertyAccumulator;
3386
+ exports.cssPropertyAccumulator = cssPropertyAccumulator;
3246
3387
  exports.elements = elements;
3388
+ exports.normalizeComponentArgs = normalizeComponentArgs;
3247
3389
  exports.router = router;
3248
3390
  exports.withValidation = withValidation;
3249
3391