domql 3.7.0 → 3.7.4

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.
Files changed (2) hide show
  1. package/dist/iife/index.js +115 -47
  2. package/package.json +4 -4
@@ -631,8 +631,38 @@ var Domql = (() => {
631
631
  });
632
632
 
633
633
  // ../utils/dist/esm/function.js
634
+ var hasHandlerPlugin, resolveHandler, runPluginHook;
634
635
  var init_function = __esm({
635
636
  "../utils/dist/esm/function.js"() {
637
+ hasHandlerPlugin = (ctx) => {
638
+ const plugins = ctx?.plugins;
639
+ if (!plugins || !plugins.length) return false;
640
+ for (const plugin of plugins) {
641
+ if (plugin.resolveHandler) return true;
642
+ }
643
+ return false;
644
+ };
645
+ resolveHandler = (handler, element) => {
646
+ if (typeof handler === "function") return handler;
647
+ const plugins = element?.context?.plugins;
648
+ if (!plugins) return handler;
649
+ for (const plugin of plugins) {
650
+ if (plugin.resolveHandler) {
651
+ const resolved = plugin.resolveHandler(handler, element);
652
+ if (typeof resolved === "function") return resolved;
653
+ }
654
+ }
655
+ return handler;
656
+ };
657
+ runPluginHook = (hookName, element, ...args) => {
658
+ const plugins = element?.context?.plugins;
659
+ if (!plugins) return;
660
+ for (const plugin of plugins) {
661
+ if (typeof plugin[hookName] === "function") {
662
+ plugin[hookName](element, ...args);
663
+ }
664
+ }
665
+ };
636
666
  }
637
667
  });
638
668
 
@@ -1050,14 +1080,17 @@ var Domql = (() => {
1050
1080
  }
1051
1081
  return element;
1052
1082
  };
1053
- trackSourcemapDeep = (sourcemap, obj, sourceName) => {
1083
+ trackSourcemapDeep = (sourcemap, obj, sourceName, seen) => {
1084
+ if (!seen) seen = /* @__PURE__ */ new WeakSet();
1085
+ if (seen.has(obj)) return;
1086
+ seen.add(obj);
1054
1087
  for (const key in obj) {
1055
1088
  if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
1056
1089
  if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
1057
1090
  const val = obj[key];
1058
1091
  if (isObject(val) && !isArray(val)) {
1059
1092
  sourcemap[key] = sourcemap[key] || {};
1060
- trackSourcemapDeep(sourcemap[key], val, sourceName);
1093
+ trackSourcemapDeep(sourcemap[key], val, sourceName, seen);
1061
1094
  } else {
1062
1095
  sourcemap[key] = sourceName;
1063
1096
  }
@@ -1255,7 +1288,9 @@ var Domql = (() => {
1255
1288
  if (isFunction(origEvent)) {
1256
1289
  on[eventName] = (...args) => {
1257
1290
  const originalEventRetunrs = origEvent(...args);
1258
- if (originalEventRetunrs !== false) funcFromProps(...args);
1291
+ if (originalEventRetunrs !== false) {
1292
+ if (isFunction(funcFromProps)) return funcFromProps(...args);
1293
+ }
1259
1294
  };
1260
1295
  } else on[eventName] = funcFromProps;
1261
1296
  }
@@ -1271,7 +1306,8 @@ var Domql = (() => {
1271
1306
  const cachedKeys = opts.cachedKeys || [];
1272
1307
  for (const key in obj) {
1273
1308
  const value = obj[key];
1274
- const isEventHandler = key.length > 2 && key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110 && key[2] === key[2].toUpperCase() && isFunction(value);
1309
+ const isOnKey = key.length > 2 && key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110 && key[2] === key[2].toUpperCase();
1310
+ const isEventHandler = isOnKey && (isFunction(value) || value != null && hasHandlerPlugin(this.context));
1275
1311
  if (isEventHandler) {
1276
1312
  const eventName = lowercaseFirstLetter(key.slice(2));
1277
1313
  if (obj.on) obj.on[eventName] = value;
@@ -1311,8 +1347,8 @@ var Domql = (() => {
1311
1347
  for (const key in obj.props) {
1312
1348
  const value = obj.props[key];
1313
1349
  const isEvent = key.length > 2 && key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110;
1314
- const isFn = isFunction(value);
1315
- if (isEvent && isFn) {
1350
+ const isHandler = isFunction(value) || value != null && hasHandlerPlugin(this.context);
1351
+ if (isEvent && isHandler) {
1316
1352
  addEventFromProps(key, obj);
1317
1353
  delete obj.props[key];
1318
1354
  continue;
@@ -1363,6 +1399,7 @@ var Domql = (() => {
1363
1399
  init_object();
1364
1400
  init_types();
1365
1401
  init_string();
1402
+ init_function();
1366
1403
  RE_UPPER = /^[A-Z]/;
1367
1404
  RE_DIGITS = /^\d+$/;
1368
1405
  CSS_SELECTOR_PREFIXES = /* @__PURE__ */ new Set([":", "@", "[", "*", "+", "~", "&", ">", "$", "-", ".", "!"]);
@@ -1786,6 +1823,7 @@ var Domql = (() => {
1786
1823
  var init_triggerEvent = __esm({
1787
1824
  "../utils/dist/esm/triggerEvent.js"() {
1788
1825
  init_types();
1826
+ init_function();
1789
1827
  getOnOrPropsEvent = (param, element) => {
1790
1828
  const onEvent = element.on?.[param];
1791
1829
  if (onEvent) return onEvent;
@@ -1795,6 +1833,7 @@ var Domql = (() => {
1795
1833
  return props[propKey];
1796
1834
  };
1797
1835
  applyEvent = (param, element, state2, context, options) => {
1836
+ param = resolveHandler(param, element);
1798
1837
  if (!isFunction(param)) return;
1799
1838
  try {
1800
1839
  const result = param.call(
@@ -1821,6 +1860,7 @@ var Domql = (() => {
1821
1860
  if (!element) {
1822
1861
  throw new Error("Element is required");
1823
1862
  }
1863
+ runPluginHook(param, element, options);
1824
1864
  const appliedFunction = getOnOrPropsEvent(param, element);
1825
1865
  if (appliedFunction) {
1826
1866
  const { state: state2, context } = element;
@@ -1828,6 +1868,7 @@ var Domql = (() => {
1828
1868
  }
1829
1869
  };
1830
1870
  applyEventUpdate = (param, updatedObj, element, state2, context, options) => {
1871
+ param = resolveHandler(param, element);
1831
1872
  if (!isFunction(param)) return;
1832
1873
  try {
1833
1874
  const result = param.call(
@@ -1852,6 +1893,7 @@ var Domql = (() => {
1852
1893
  }
1853
1894
  };
1854
1895
  triggerEventOnUpdate = (param, updatedObj, element, options) => {
1896
+ runPluginHook(param, element, updatedObj, options);
1855
1897
  const appliedFunction = getOnOrPropsEvent(param, element);
1856
1898
  if (appliedFunction) {
1857
1899
  const { state: state2, context } = element;
@@ -2307,13 +2349,14 @@ var Domql = (() => {
2307
2349
  createAdapter: () => createAdapter,
2308
2350
  default: () => fetch_default,
2309
2351
  executeFetch: () => executeFetch,
2352
+ fetchPlugin: () => fetchPlugin,
2310
2353
  initAdapterAuth: () => initAdapterAuth,
2311
2354
  parseDuration: () => parseDuration,
2312
2355
  queryClient: () => queryClient,
2313
2356
  registerAdapter: () => registerAdapter,
2314
2357
  resolveDb: () => resolveDb
2315
2358
  });
2316
- var ADAPTER_METHODS, BUILTIN_ADAPTERS, registerAdapter, createAdapter, resolveDb, parseDuration, cacheStore, querySubscribers, activeQueries, buildCacheKey, getCacheEntry, setCacheEntry, invalidateCache, removeCache, parseCacheConfig, gcTimer, startGC, DEFAULT_RETRY, DEFAULT_RETRY_DELAY, resolveRetryConfig, withRetry, globalListeners, globalListenersAttached, attachGlobalListeners, resolveFetchConfig, resolveParamsSync, resolveParams, initAdapterAuth, resolveAdapter, triggerCallback, collectFormData, updateElementState, setFetchStatus, runFetch, bindEvent, bindAutoRefetch, applyOptimisticUpdate, rollbackOptimistic, runMutation, executeFetch, queryClient, fetch_default;
2359
+ var ADAPTER_METHODS, BUILTIN_ADAPTERS, registerAdapter, createAdapter, resolveDb, parseDuration, cacheStore, querySubscribers, activeQueries, buildCacheKey, getCacheEntry, setCacheEntry, invalidateCache, removeCache, parseCacheConfig, gcTimer, startGC, DEFAULT_RETRY, DEFAULT_RETRY_DELAY, resolveRetryConfig, withRetry, globalListeners, globalListenersAttached, attachGlobalListeners, resolveFetchConfig, resolveParamsSync, resolveLanguage, resolveParams, initAdapterAuth, resolveAdapter, triggerCallback, collectFormData, updateElementState, setFetchStatus, runFetch, bindEvent, bindAutoRefetch, applyOptimisticUpdate, rollbackOptimistic, runMutation, executeFetch, queryClient, fetchPlugin, fetch_default;
2317
2360
  var init_fetch = __esm({
2318
2361
  "../../plugins/fetch/index.js"() {
2319
2362
  "use strict";
@@ -2509,13 +2552,23 @@ var Domql = (() => {
2509
2552
  if (!params || isFunction(params)) return params;
2510
2553
  return params;
2511
2554
  };
2512
- resolveParams = (params, element) => {
2513
- if (!params) return void 0;
2514
- if (isFunction(params)) return params(element, element.state);
2515
- const resolved = {};
2516
- for (const key in params) {
2517
- const val = params[key];
2518
- resolved[key] = isFunction(val) ? val(element, element.state) : val;
2555
+ resolveLanguage = (element, context) => {
2556
+ const root = element?.state?.root || context?.state?.root;
2557
+ if (root?.lang) return root.lang;
2558
+ return void 0;
2559
+ };
2560
+ resolveParams = (params, element, context) => {
2561
+ let resolved;
2562
+ if (!params) {
2563
+ resolved = void 0;
2564
+ } else if (isFunction(params)) {
2565
+ resolved = params(element, element.state);
2566
+ } else {
2567
+ resolved = {};
2568
+ for (const key in params) {
2569
+ const val = params[key];
2570
+ resolved[key] = isFunction(val) ? val(element, element.state) : val;
2571
+ }
2519
2572
  }
2520
2573
  return resolved;
2521
2574
  };
@@ -2553,7 +2606,7 @@ var Domql = (() => {
2553
2606
  db.__resolving = resolveDb(db);
2554
2607
  const resolved = await db.__resolving;
2555
2608
  db.__resolved = resolved;
2556
- context.db = resolved;
2609
+ context.fetch = resolved;
2557
2610
  delete db.__resolving;
2558
2611
  if (db.auth !== false) await initAdapterAuth(resolved, context);
2559
2612
  return resolved;
@@ -2601,7 +2654,7 @@ var Domql = (() => {
2601
2654
  ref.__fetchError = status.error;
2602
2655
  };
2603
2656
  runFetch = async (config, element, context, opts = {}) => {
2604
- const db = context?.db;
2657
+ const db = context?.fetch;
2605
2658
  if (!db) return;
2606
2659
  if (config.enabled === false) return;
2607
2660
  if (isFunction(config.enabled) && !config.enabled(element, element.state)) return;
@@ -2641,7 +2694,7 @@ var Domql = (() => {
2641
2694
  const q = element.getQuery(adapter.name || "paths");
2642
2695
  if (q) select = q.select || q.length && q.join(",") || void 0;
2643
2696
  }
2644
- const params = resolveParams(rawParams, element);
2697
+ const params = resolveParams(rawParams, element, context);
2645
2698
  const cacheConfig = parseCacheConfig(cacheRaw);
2646
2699
  const retryConfig = resolveRetryConfig(config);
2647
2700
  const cacheKey = cacheConfig ? cacheRaw?.key || `${from}:${method}:${JSON.stringify(params || "")}${infinite ? ":infinite" : ""}${page ? ":p" + JSON.stringify(page) : ""}` : null;
@@ -2711,6 +2764,10 @@ var Domql = (() => {
2711
2764
  const fn = adapter[method];
2712
2765
  if (!isFunction(fn)) return { data: null, error: { message: `Method "${method}" not found on adapter` } };
2713
2766
  const request2 = { from, select, params, single, limit, offset, order, headers, baseUrl };
2767
+ const lang = resolveLanguage(element, context);
2768
+ if (lang) {
2769
+ request2.headers = { ...request2.headers, "Accept-Language": lang };
2770
+ }
2714
2771
  if (page !== void 0) {
2715
2772
  if (isObject(page)) {
2716
2773
  if (page.offset !== void 0) request2.offset = page.offset;
@@ -2893,7 +2950,7 @@ var Domql = (() => {
2893
2950
  }
2894
2951
  };
2895
2952
  runMutation = async (config, element, context) => {
2896
- const db = context?.db;
2953
+ const db = context?.fetch;
2897
2954
  if (!db) return;
2898
2955
  const adapter = await resolveAdapter(db, context);
2899
2956
  if (!adapter) return;
@@ -2930,7 +2987,11 @@ var Domql = (() => {
2930
2987
  const fn = adapter[method];
2931
2988
  if (!isFunction(fn)) return;
2932
2989
  const request2 = { from, data: mutationData, headers, baseUrl };
2933
- if (config.params) request2.params = resolveParams(config.params, element);
2990
+ if (config.params) request2.params = resolveParams(config.params, element, context);
2991
+ const lang = resolveLanguage(element, context);
2992
+ if (lang) {
2993
+ request2.headers = { ...request2.headers, "Accept-Language": lang };
2994
+ }
2934
2995
  const retryConfig = resolveRetryConfig(config);
2935
2996
  const result = await withRetry(() => fn(request2), retryConfig);
2936
2997
  const { data: data2, error: error2 } = result || {};
@@ -2975,7 +3036,7 @@ var Domql = (() => {
2975
3036
  };
2976
3037
  executeFetch = (param, element, state2, context) => {
2977
3038
  if (!param) return;
2978
- const db = context?.db;
3039
+ const db = context?.fetch;
2979
3040
  if (!db) return;
2980
3041
  const fetchProp = exec(param, element);
2981
3042
  if (!fetchProp) return;
@@ -3061,7 +3122,7 @@ var Domql = (() => {
3061
3122
  setCacheEntry(key, data2, null);
3062
3123
  },
3063
3124
  prefetchQuery: async (config, context) => {
3064
- const db = context?.db;
3125
+ const db = context?.fetch;
3065
3126
  if (!db) return;
3066
3127
  const adapter = await resolveAdapter(db, context);
3067
3128
  if (!adapter) return;
@@ -3085,6 +3146,15 @@ var Domql = (() => {
3085
3146
  getCache: () => cacheStore,
3086
3147
  clear: () => removeCache()
3087
3148
  };
3149
+ fetchPlugin = {
3150
+ name: "fetch",
3151
+ // Hook into element creation to auto-execute fetch configs
3152
+ create(element) {
3153
+ const fetchProp = element.fetch || element.props?.fetch;
3154
+ if (!fetchProp) return;
3155
+ executeFetch(fetchProp, element, element.state, element.context);
3156
+ }
3157
+ };
3088
3158
  fetch_default = executeFetch;
3089
3159
  }
3090
3160
  });
@@ -3384,18 +3454,18 @@ var Domql = (() => {
3384
3454
  const result = fn.call(this, ...args);
3385
3455
  if (result && typeof result.then === "function") {
3386
3456
  result.catch((err) => {
3387
- this.error = err;
3457
+ error.call(this, err);
3388
3458
  });
3389
3459
  }
3390
3460
  return result;
3391
3461
  } catch (err) {
3392
- this.error = err;
3462
+ error.call(this, err);
3393
3463
  if (context?.strictMode) throw err;
3394
3464
  }
3395
3465
  }
3396
3466
  async function getDB() {
3397
3467
  const element = this;
3398
- const db = element.context?.db;
3468
+ const db = element.context?.fetch;
3399
3469
  if (!db) return null;
3400
3470
  if (typeof db.select === "function") {
3401
3471
  if (!db.__authInitialized && db.getSession) {
@@ -3416,7 +3486,7 @@ var Domql = (() => {
3416
3486
  db.__resolving = resolvePromise;
3417
3487
  const resolved = await resolvePromise;
3418
3488
  db.__resolved = resolved;
3419
- element.context.db = resolved;
3489
+ element.context.fetch = resolved;
3420
3490
  delete db.__resolving;
3421
3491
  if (resolved.getSession) {
3422
3492
  const { initAdapterAuth: initAdapterAuth2 } = await Promise.resolve().then(() => (init_fetch(), fetch_exports));
@@ -3776,7 +3846,7 @@ ${element}` : "";
3776
3846
  return props[propKey];
3777
3847
  };
3778
3848
  var registerNodeEvent = (param, element, node, options) => {
3779
- const appliedFunction = getOnOrPropsEvent2(param, element);
3849
+ const appliedFunction = resolveHandler(getOnOrPropsEvent2(param, element), element);
3780
3850
  if (isFunction(appliedFunction)) {
3781
3851
  const { __ref: ref } = element;
3782
3852
  if (!ref.__eventListeners) ref.__eventListeners = {};
@@ -3811,24 +3881,11 @@ ${element}` : "";
3811
3881
  }
3812
3882
  };
3813
3883
  var applyEventsOnNode = (element, options) => {
3814
- const { node, on, props } = element;
3815
- const handled = /* @__PURE__ */ new Set();
3884
+ const { node, on } = element;
3816
3885
  for (const param in on) {
3817
3886
  if (DOMQL_EVENTS.has(param)) continue;
3818
- handled.add(param);
3819
3887
  registerNodeEvent(param, element, node, options);
3820
3888
  }
3821
- if (props) {
3822
- for (const key in props) {
3823
- if (key.length > 2 && key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110 && isFunction(props[key])) {
3824
- const thirdChar = key[2];
3825
- if (thirdChar !== thirdChar.toUpperCase()) continue;
3826
- const eventName = thirdChar.toLowerCase() + key.slice(3);
3827
- if (handled.has(eventName) || DOMQL_EVENTS.has(eventName)) continue;
3828
- registerNodeEvent(eventName, element, node, options);
3829
- }
3830
- }
3831
- }
3832
3889
  };
3833
3890
 
3834
3891
  // ../element/dist/esm/event/can.js
@@ -3918,25 +3975,30 @@ ${element}` : "";
3918
3975
  if (isFunction(origEvent)) {
3919
3976
  on[eventName] = (...args) => {
3920
3977
  const originalEventRetunrs = origEvent(...args);
3921
- if (originalEventRetunrs !== false) return funcFromProps(...args);
3978
+ if (originalEventRetunrs !== false) {
3979
+ if (isFunction(funcFromProps)) return funcFromProps(...args);
3980
+ }
3922
3981
  };
3923
3982
  } else on[eventName] = funcFromProps;
3924
3983
  }
3925
3984
  };
3926
3985
  var propagateEventsFromElement = (element) => {
3927
3986
  const { on } = element;
3987
+ const pluginActive = hasHandlerPlugin(element.context);
3928
3988
  for (const param in element) {
3929
3989
  if (param.charCodeAt(0) !== 111 || param.charCodeAt(1) !== 110 || !Object.prototype.hasOwnProperty.call(element, param)) continue;
3930
- const fn = element[param];
3931
- if (!isFunction(fn)) continue;
3990
+ const handler = element[param];
3991
+ if (!isFunction(handler) && !(pluginActive && handler != null)) continue;
3932
3992
  const eventName = lowercaseFirstLetter(param.slice(2));
3933
3993
  const origEvent = on[eventName];
3934
3994
  if (isFunction(origEvent)) {
3935
3995
  on[eventName] = (...args) => {
3936
3996
  const ret = origEvent(...args);
3937
- if (ret !== false) return fn(...args);
3997
+ if (ret !== false) {
3998
+ if (isFunction(handler)) return handler(...args);
3999
+ }
3938
4000
  };
3939
- } else on[eventName] = fn;
4001
+ } else on[eventName] = handler;
3940
4002
  }
3941
4003
  };
3942
4004
 
@@ -4214,7 +4276,10 @@ ${element}` : "";
4214
4276
  const attrs = exec(params, element);
4215
4277
  if (props.attr) deepMerge(attrs, props.attr);
4216
4278
  for (const attr2 in attrs) {
4217
- const val = exec(attrs[attr2], element);
4279
+ let val = exec(attrs[attr2], element);
4280
+ if (isString(val) && val.includes("{{") && element.call) {
4281
+ val = element.call("replaceLiteralsWithObjectFields", val, element.state);
4282
+ }
4218
4283
  if (val === __attr[attr2]) continue;
4219
4284
  if (val !== false && val !== void 0 && val !== null && node.setAttribute) {
4220
4285
  node.setAttribute(attr2, val);
@@ -4275,7 +4340,10 @@ ${element}` : "";
4275
4340
  // ../element/dist/esm/mixins/html.js
4276
4341
  init_esm();
4277
4342
  function html(param, element, node) {
4278
- const prop = exec(param ?? element?.props?.html, element);
4343
+ let prop = exec(param ?? element?.props?.html, element);
4344
+ if (isString(prop) && prop.includes("{{") && element.call) {
4345
+ prop = element.call("replaceLiteralsWithObjectFields", prop, element.state);
4346
+ }
4279
4347
  const { __ref } = element;
4280
4348
  if (prop !== __ref.__html) {
4281
4349
  if (node.nodeName === "SVG") node.textContent = prop;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "domql",
3
- "version": "3.7.0",
3
+ "version": "3.7.4",
4
4
  "license": "CC-BY-NC-4.0",
5
5
  "type": "module",
6
6
  "module": "./dist/esm/index.js",
@@ -25,9 +25,9 @@
25
25
  "build:iife": "cross-env NODE_ENV=$NODE_ENV esbuild index.js --bundle --target=es2020 --format=iife --global-name=Domql --outfile=dist/iife/index.js --define:process.env.NODE_ENV=process.env.NODE_ENV"
26
26
  },
27
27
  "dependencies": {
28
- "@domql/element": "^3.7.0",
29
- "@domql/state": "^3.7.0",
30
- "@domql/utils": "^3.7.0"
28
+ "@domql/element": "^3.7.4",
29
+ "@domql/state": "^3.7.4",
30
+ "@domql/utils": "^3.7.4"
31
31
  },
32
32
  "gitHead": "9fc1b79b41cdc725ca6b24aec64920a599634681",
33
33
  "browser": "./dist/esm/index.js",