@optionfactory/ful 1.0.1 → 1.0.3

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/ful.iife.js CHANGED
@@ -491,6 +491,7 @@ var ful = (function (exports, ftl) {
491
491
  param(k, ...vs) {
492
492
  if (vs.length === 0 || vs[0] === null || vs[0] === undefined) {
493
493
  this.#params.delete(k);
494
+ return this;
494
495
  }
495
496
  for (const v of vs) {
496
497
  this.#params.append(k, v);
@@ -949,6 +950,65 @@ var ful = (function (exports, ftl) {
949
950
  }
950
951
  }
951
952
 
953
+ class AsyncEvents {
954
+ static async fireAsync(el, evt) {
955
+ el.dispatchEvent(evt);
956
+ return await evt.async?.promise;
957
+ }
958
+ /**
959
+ *
960
+ * @param {*} el
961
+ * @param {*} type
962
+ * @param {*} fn returning the result
963
+ * @param {*} options
964
+ * @returns
965
+ */
966
+ static asyncOn(el, type, fn, options) {
967
+ const listener = async (event) => {
968
+ let resolve, reject;
969
+ const promise = new Promise((res, rej) => {
970
+ resolve = res;
971
+ reject = rej;
972
+ });
973
+ event.async = { promise };
974
+ try {
975
+ //@ts-ignore
976
+ resolve(await fn(event));
977
+ } catch (e) {
978
+ //@ts-ignore
979
+ reject(e);
980
+ }
981
+ };
982
+ el.addEventListener(type, listener, options);
983
+ return listener;
984
+ }
985
+ /**
986
+ *
987
+ * @param {*} el
988
+ * @param {*} type
989
+ * @param {*} listener the listener returned by asyncOn
990
+ * @param {*} options
991
+ */
992
+ static asyncOff(el, type, listener, options) {
993
+ el.removeEventListener(type, listener, options);
994
+ }
995
+ static mixInto(...classes) {
996
+ for (const k of classes) {
997
+ Object.assign(k.prototype, {
998
+ async fireAsync(evt) {
999
+ return await AsyncEvents.fireAsync(this, evt);
1000
+ },
1001
+ asyncOn(type, fn, options) {
1002
+ return AsyncEvents.asyncOn(this, type, fn, options);
1003
+ },
1004
+ asyncOff(type, listener, options) {
1005
+ return AsyncEvents.asyncOff(this, type, listener, options);
1006
+ }
1007
+ });
1008
+ }
1009
+ }
1010
+ }
1011
+
952
1012
  const timing = {
953
1013
  sleep(ms) {
954
1014
  return new Promise(resolve => setTimeout(resolve, ms));
@@ -1243,8 +1303,8 @@ var ful = (function (exports, ftl) {
1243
1303
  async prepare(values, form) {
1244
1304
  return await this.#requestMapper(values, form);
1245
1305
  }
1246
- async submit(values, form) {
1247
- return values;
1306
+ async submit(values, form, response) {
1307
+ return response;
1248
1308
  }
1249
1309
  async transform(response, form) {
1250
1310
  return await this.#responseMapper(response, form);
@@ -1286,13 +1346,17 @@ var ful = (function (exports, ftl) {
1286
1346
  try {
1287
1347
  const loader = Loaders.fromAttributes(this, 'loaders:form');
1288
1348
  const values = this.values;
1289
- const request = await loader.prepare(values, this);
1290
- const se = new CustomEvent('submit', { bubbles: true, cancelable: true, detail: { values, request } });
1291
- if (!this.dispatchEvent(se)) {
1292
- return;
1293
- }
1349
+ let request = await loader.prepare(values, this);
1294
1350
  try {
1295
- const response = await loader.submit(se.detail.request, this);
1351
+ const se = new CustomEvent('submit', { bubbles: true, cancelable: true, detail: { values, request } });
1352
+ if (!this.dispatchEvent(se)) {
1353
+ return;
1354
+ }
1355
+ const sre = new CustomEvent('submit:requested', { bubbles: true, cancelable: false, detail: { values: se.detail.values, request: se.detail.request} });
1356
+ let response = await AsyncEvents.fireAsync(this, sre);
1357
+ request = sre.detail.request;
1358
+
1359
+ response = await loader.submit(request, this, response);
1296
1360
  const mapped = await loader.transform(response, this);
1297
1361
  this.dispatchEvent(new CustomEvent('submit:success', { bubbles: true, cancelable: false, detail: { values, request, response: mapped } }));
1298
1362
  } catch (e) {
@@ -1300,12 +1364,13 @@ var ful = (function (exports, ftl) {
1300
1364
  if (e instanceof Failure) {
1301
1365
  this.errors = e.problems;
1302
1366
  }
1303
- throw e;
1367
+ console.warn("failed to submit form", this, "reason:", e);
1304
1368
  }
1305
1369
  } finally {
1306
1370
  this.spinner(false);
1307
1371
  }
1308
1372
  }
1373
+
1309
1374
  spinner(spin) {
1310
1375
  this.querySelectorAll('ful-spinner').forEach(el => {
1311
1376
  const hel = /** @type HTMLElement */ (el);
@@ -2578,6 +2643,7 @@ var ful = (function (exports, ftl) {
2578
2643
  }
2579
2644
  }
2580
2645
 
2646
+ exports.AsyncEvents = AsyncEvents;
2581
2647
  exports.AuthorizationCodeFlow = AuthorizationCodeFlow;
2582
2648
  exports.AuthorizationCodeFlowInterceptor = AuthorizationCodeFlowInterceptor;
2583
2649
  exports.AuthorizationCodeFlowSession = AuthorizationCodeFlowSession;