@next-core/brick-kit 2.135.1 → 2.136.2

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/index.esm.js CHANGED
@@ -1972,7 +1972,7 @@ class StoryboardContextWrapper {
1972
1972
  return (_this$data$get = this.data.get(name)) === null || _this$data$get === void 0 ? void 0 : _this$data$get.value;
1973
1973
  }
1974
1974
 
1975
- updateValue(name, value, method) {
1975
+ updateValue(name, value, method, callback) {
1976
1976
  var _item$eventTarget2;
1977
1977
 
1978
1978
  if (!this.data.has(name)) {
@@ -1995,36 +1995,58 @@ class StoryboardContextWrapper {
1995
1995
  // eslint-disable-next-line no-console
1996
1996
  console.error("Unexpected storyboard context \"".concat(name, "\", expected \"free-variable\", received \"").concat(item.type, "\"."));
1997
1997
  return;
1998
- } // Todo(steve): support callback for refresh/load.
1999
-
1998
+ }
2000
1999
 
2001
2000
  if (method === "refresh" || method === "load") {
2002
2001
  if (!item.load) {
2003
2002
  throw new Error("You can not ".concat(method, " the storyboard context \"").concat(name, "\" which has no resolve."));
2004
2003
  }
2005
2004
 
2006
- if ((item.loaded || item.loading) && method === "load") {
2007
- // Do not load again.
2008
- return;
2005
+ var promise;
2006
+
2007
+ if (method === "load") {
2008
+ // Try to reuse previous request when calling `load`.
2009
+ if (item.loaded) {
2010
+ promise = Promise.resolve(item.value);
2011
+ } else if (item.loading) {
2012
+ promise = item.loading;
2013
+ }
2009
2014
  }
2010
2015
 
2011
- item.loading = true; // Defaults to ignore cache when refreshing.
2016
+ if (!promise) {
2017
+ promise = item.loading = item.load(_objectSpread({
2018
+ cache: method === "load" ? "default" : "reload"
2019
+ }, value)); // Do not use the chained promise, since the callbacks need the original promise.
2012
2020
 
2013
- item.load(_objectSpread({
2014
- cache: method === "load" ? "default" : "reload"
2015
- }, value)).then(val => {
2016
- var _item$eventTarget;
2021
+ promise.then(val => {
2022
+ var _item$eventTarget;
2023
+
2024
+ item.loaded = true;
2025
+ item.value = val;
2026
+ (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2027
+ detail: item.value
2028
+ }));
2029
+ }, err => {
2030
+ // Let users to override error handling.
2031
+ if (!(callback !== null && callback !== void 0 && callback.error)) {
2032
+ handleHttpError(err);
2033
+ }
2034
+ });
2035
+ }
2036
+
2037
+ if (callback) {
2038
+ var callbackFactory = eventCallbackFactory(callback, () => this.getResolveOptions(_internalApiGetCurrentContext()).mergedContext, null);
2039
+ promise.then(val => {
2040
+ callbackFactory("success")({
2041
+ value: val
2042
+ });
2043
+ callbackFactory("finally")();
2044
+ }, err => {
2045
+ callbackFactory("error")(err);
2046
+ callbackFactory("finally")();
2047
+ });
2048
+ }
2017
2049
 
2018
- item.loading = false;
2019
- item.loaded = true;
2020
- item.value = val;
2021
- (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.tplContextId ? "state.change" : "context.change", {
2022
- detail: item.value
2023
- }));
2024
- }, err => {
2025
- item.loading = false;
2026
- handleHttpError(err);
2027
- });
2028
2050
  return;
2029
2051
  }
2030
2052
 
@@ -2322,6 +2344,22 @@ mediaBreakpointMinWidthMap.forEach((minWidth, breakpoint) => {
2322
2344
  });
2323
2345
  var getMedia = () => MEDIA;
2324
2346
 
2347
+ var FormContextMap = new Map();
2348
+ class CustomFormContext {
2349
+ constructor() {
2350
+ _defineProperty$1(this, "formState", void 0);
2351
+
2352
+ _defineProperty$1(this, "id", uniqueId("form-ctx-"));
2353
+
2354
+ FormContextMap.set(this.id, this);
2355
+ this.formState = new StoryboardContextWrapper();
2356
+ }
2357
+
2358
+ }
2359
+ function getCustomFormContext(formContextId) {
2360
+ return FormContextMap.get(formContextId);
2361
+ }
2362
+
2325
2363
  var symbolForRaw = Symbol.for("pre.evaluated.raw");
2326
2364
  var symbolForContext = Symbol.for("pre.evaluated.context");
2327
2365
  function isPreEvaluated(raw) {
@@ -2394,6 +2432,7 @@ function evaluate(raw) {
2394
2432
  var attemptToVisitData = attemptToVisitGlobals.has("DATA");
2395
2433
  var attemptToVisitTpl = attemptToVisitGlobals.has("TPL");
2396
2434
  var attemptToVisitState = attemptToVisitGlobals.has("STATE");
2435
+ var attemptToVisitFormState = attemptToVisitGlobals.has("FORM_STATE");
2397
2436
  var attemptToVisitTplOrState = attemptToVisitTpl || attemptToVisitState; // Ignore evaluating if `event` is missing in context.
2398
2437
  // Since it should be evaluated during events handling.
2399
2438
 
@@ -2445,6 +2484,20 @@ function evaluate(raw) {
2445
2484
  }
2446
2485
  }
2447
2486
 
2487
+ if (attemptToVisitFormState && runtimeContext.formContextId) {
2488
+ var formContext = getCustomFormContext(runtimeContext.formContextId);
2489
+ globalVariables.FORM_STATE = getDynamicReadOnlyProxy({
2490
+ get(target, key) {
2491
+ return formContext.formState.getValue(key);
2492
+ },
2493
+
2494
+ ownKeys() {
2495
+ return Array.from(formContext.formState.get().keys());
2496
+ }
2497
+
2498
+ });
2499
+ }
2500
+
2448
2501
  var {
2449
2502
  app: currentApp,
2450
2503
  query,
@@ -2898,7 +2951,7 @@ var computeRealValue = (value, context, injectDeep, internalOptions) => {
2898
2951
 
2899
2952
  if (preEvaluated || isEvaluable(value)) {
2900
2953
  var runtimeContext = {};
2901
- var keys = ["event", "tplContextId", "overrideApp", "appendI18nNamespace"];
2954
+ var keys = ["event", "tplContextId", "overrideApp", "appendI18nNamespace", "formContextId"];
2902
2955
 
2903
2956
  for (var key of keys) {
2904
2957
  if (context !== null && context !== void 0 && context[key]) {
@@ -6311,12 +6364,12 @@ function listenerFactory(handler, context, runtimeBrick) {
6311
6364
  case "context.replace":
6312
6365
  case "context.refresh":
6313
6366
  case "context.load":
6314
- return builtinContextListenerFactory(method, handler.args, handler, context);
6367
+ return builtinContextListenerFactory(method, handler.args, handler, handler.callback, context);
6315
6368
 
6316
6369
  case "state.update":
6317
6370
  case "state.refresh":
6318
6371
  case "state.load":
6319
- return builtinStateListenerFactory(method, handler.args, handler, context);
6372
+ return builtinStateListenerFactory(method, handler.args, handler, handler.callback, context);
6320
6373
 
6321
6374
  case "tpl.dispatchEvent":
6322
6375
  return builtinTplDispatchEventFactory(handler.args, handler, context);
@@ -6445,7 +6498,7 @@ function builtinTplDispatchEventFactory(args, ifContainer, context) {
6445
6498
  };
6446
6499
  }
6447
6500
 
6448
- function builtinContextListenerFactory(method, args, ifContainer, context) {
6501
+ function builtinContextListenerFactory(method, args, ifContainer, callback, context) {
6449
6502
  return function (event) {
6450
6503
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6451
6504
  event
@@ -6456,11 +6509,11 @@ function builtinContextListenerFactory(method, args, ifContainer, context) {
6456
6509
  var storyboardContext = _internalApiGetStoryboardContextWrapper();
6457
6510
 
6458
6511
  var [name, value] = argsFactory(args, context, event);
6459
- storyboardContext.updateValue(name, value, method);
6512
+ storyboardContext.updateValue(name, value, method, callback);
6460
6513
  };
6461
6514
  }
6462
6515
 
6463
- function builtinStateListenerFactory(method, args, ifContainer, context) {
6516
+ function builtinStateListenerFactory(method, args, ifContainer, callback, context) {
6464
6517
  return function (event) {
6465
6518
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6466
6519
  event
@@ -6470,7 +6523,7 @@ function builtinStateListenerFactory(method, args, ifContainer, context) {
6470
6523
 
6471
6524
  var tplContext = getTplContext(context.tplContextId);
6472
6525
  var [name, value] = argsFactory(args, context, event);
6473
- tplContext.state.updateValue(name, value, method === "update" ? "replace" : method);
6526
+ tplContext.state.updateValue(name, value, method === "update" ? "replace" : method, callback);
6474
6527
  };
6475
6528
  }
6476
6529
 
@@ -6671,13 +6724,39 @@ function customListenerFactory(handler, ifContainer, context, runtimeBrick) {
6671
6724
  };
6672
6725
  }
6673
6726
 
6727
+ function eventCallbackFactory(callback, getContext, runtimeBrick) {
6728
+ return function callbackFactory(type) {
6729
+ return function (result) {
6730
+ if (callback !== null && callback !== void 0 && callback[type]) {
6731
+ try {
6732
+ var event = new CustomEvent("callback.".concat(type), {
6733
+ detail: result
6734
+ });
6735
+ var context = getContext();
6736
+ [].concat(callback[type]).forEach(eachHandler => {
6737
+ listenerFactory(eachHandler, context, runtimeBrick)(event);
6738
+ });
6739
+ } catch (err) {
6740
+ // Do not throw errors in `callback.success` or `callback.progress`,
6741
+ // to avoid the following triggering of `callback.error`.
6742
+ // eslint-disable-next-line
6743
+ console.error(err);
6744
+ }
6745
+ } else if (type === "error") {
6746
+ // eslint-disable-next-line
6747
+ console.error("Unhandled callback error:", result);
6748
+ }
6749
+ };
6750
+ };
6751
+ }
6752
+
6674
6753
  function brickCallback(_x2, _x3, _x4, _x5, _x6, _x7, _x8) {
6675
6754
  return _brickCallback.apply(this, arguments);
6676
6755
  }
6677
6756
 
6678
6757
  function _brickCallback() {
6679
6758
  _brickCallback = _asyncToGenerator$3(function* (target, handler, method, context, runtimeBrick, event, options) {
6680
- var _handler$callback, _poll;
6759
+ var _poll;
6681
6760
 
6682
6761
  if (typeof target[method] !== "function") {
6683
6762
  // eslint-disable-next-line no-console
@@ -6704,45 +6783,17 @@ function _brickCallback() {
6704
6783
  };
6705
6784
  }();
6706
6785
 
6707
- var {
6708
- success,
6709
- error,
6710
- finally: finallyHook,
6711
- progress
6712
- } = (_handler$callback = handler.callback) !== null && _handler$callback !== void 0 ? _handler$callback : {};
6713
-
6714
- if (!(success || error || finallyHook || progress)) {
6786
+ if (!handler.callback) {
6715
6787
  task();
6716
6788
  return;
6717
6789
  }
6718
6790
 
6719
- var callbackFactory = (eventType, specificHandler) => result => {
6720
- if (specificHandler) {
6721
- try {
6722
- var _event = new CustomEvent(eventType, {
6723
- detail: result
6724
- });
6725
-
6726
- [].concat(specificHandler).forEach(eachHandler => {
6727
- listenerFactory(eachHandler, context, runtimeBrick)(_event);
6728
- });
6729
- } catch (err) {
6730
- // Do not throw errors in `callback.success` or `callback.progress`,
6731
- // to avoid the following triggering of `callback.error`.
6732
- // eslint-disable-next-line
6733
- console.error(err);
6734
- }
6735
- } else if (eventType === "callback.error") {
6736
- // eslint-disable-next-line
6737
- console.error("Unhandled callback error:", result);
6738
- }
6739
- };
6740
-
6791
+ var callbackFactory = eventCallbackFactory(handler.callback, () => context, runtimeBrick);
6741
6792
  var pollableCallback = {
6742
- progress: callbackFactory("callback.progress", progress),
6743
- success: callbackFactory("callback.success", success),
6744
- error: callbackFactory("callback.error", error),
6745
- finally: callbackFactory("callback.finally", finallyHook)
6793
+ progress: callbackFactory("progress"),
6794
+ success: callbackFactory("success"),
6795
+ error: callbackFactory("error"),
6796
+ finally: callbackFactory("finally")
6746
6797
  };
6747
6798
  var poll;
6748
6799
 
@@ -8256,6 +8307,7 @@ function applyColorTheme(options) {
8256
8307
 
8257
8308
  var formRenderer = "form-renderer.form-renderer";
8258
8309
  var filterProperties = ["instanceId", "brick", "slots", "properties", "events", "if", "context", "bricks", "mountPoint"];
8310
+ var symbolForFormContextId = Symbol.for("form.contextId");
8259
8311
 
8260
8312
  function collectRefsInTemplate(template) {
8261
8313
  var refMap = new Map();
@@ -9937,58 +9989,73 @@ function listenOnTrackingContext(brick, trackingContextList, context) {
9937
9989
  }
9938
9990
  }
9939
9991
 
9940
- function ExpandCustomForm(formData, brickConf, isPreview) {
9941
- var errorBrick = {
9942
- brick: "presentational-bricks.brick-illustration",
9943
- properties: {
9944
- category: "default",
9945
- header: {
9946
- title: "参数错误"
9947
- },
9948
- mode: "guide",
9949
- name: "search-empty"
9992
+ function ExpandCustomForm(_x, _x2, _x3, _x4) {
9993
+ return _ExpandCustomForm.apply(this, arguments);
9994
+ }
9995
+
9996
+ function _ExpandCustomForm() {
9997
+ _ExpandCustomForm = _asyncToGenerator$3(function* (formData, brickConf, isPreview, context) {
9998
+ var errorBrick = {
9999
+ brick: "presentational-bricks.brick-illustration",
10000
+ properties: {
10001
+ category: "default",
10002
+ header: {
10003
+ title: "参数错误"
10004
+ },
10005
+ mode: "guide",
10006
+ name: "search-empty"
10007
+ }
10008
+ };
10009
+ var formContext = new CustomFormContext();
10010
+
10011
+ if (Array.isArray(formData.context)) {
10012
+ yield formContext.formState.define(formData.context, _objectSpread(_objectSpread({}, context), {}, {
10013
+ formContextId: formContext.id
10014
+ }), {});
9950
10015
  }
9951
- };
9952
10016
 
9953
- try {
9954
- var formStoryboard = getStoryboard([formData.formSchema], [], formData.fields, isPreview);
9955
- formStoryboard[0] = _.isEmpty(formStoryboard[0]) ? errorBrick : formStoryboard[0];
9956
- return _objectSpread(_objectSpread({}, brickConf), {}, {
9957
- brick: "div",
9958
- slots: {
9959
- "": {
9960
- bricks: [{
9961
- brick: "basic-bricks.micro-view",
9962
- properties: {
9963
- style: {
9964
- padding: "12px"
9965
- }
9966
- },
9967
- slots: {
9968
- content: {
9969
- bricks: formStoryboard,
9970
- type: "bricks"
10017
+ try {
10018
+ var formStoryboard = getStoryboard([formData.formSchema], [], formData.fields, isPreview, formContext.id);
10019
+ formStoryboard[0] = _.isEmpty(formStoryboard[0]) ? errorBrick : formStoryboard[0];
10020
+ return _objectSpread(_objectSpread({}, brickConf), {}, {
10021
+ brick: "div",
10022
+ slots: {
10023
+ "": {
10024
+ bricks: [{
10025
+ brick: "basic-bricks.micro-view",
10026
+ properties: {
10027
+ style: {
10028
+ padding: "12px"
10029
+ }
10030
+ },
10031
+ slots: {
10032
+ content: {
10033
+ bricks: formStoryboard,
10034
+ type: "bricks"
10035
+ }
9971
10036
  }
9972
- }
9973
- }],
9974
- type: "bricks"
10037
+ }],
10038
+ type: "bricks"
10039
+ }
9975
10040
  }
9976
- }
9977
- });
9978
- } catch (error) {
9979
- // eslint-disable-next-line no-console
9980
- console.warn(error.message);
9981
- return {
9982
- brick: "div",
9983
- slots: {
9984
- "": {
9985
- bricks: [errorBrick],
9986
- type: "bricks"
10041
+ });
10042
+ } catch (error) {
10043
+ // eslint-disable-next-line no-console
10044
+ console.warn(error.message);
10045
+ return {
10046
+ brick: "div",
10047
+ slots: {
10048
+ "": {
10049
+ bricks: [errorBrick],
10050
+ type: "bricks"
10051
+ }
9987
10052
  }
9988
- }
9989
- };
9990
- }
10053
+ };
10054
+ }
10055
+ });
10056
+ return _ExpandCustomForm.apply(this, arguments);
9991
10057
  }
10058
+
9992
10059
  function getDefaultProperties(_name, fields) {
9993
10060
  var field = fields.filter(item => item.fieldId === _name)[0];
9994
10061
 
@@ -10221,7 +10288,7 @@ function getDefaultProperties(_name, fields) {
10221
10288
  return defaultValue;
10222
10289
  } else return {};
10223
10290
  }
10224
- function getStoryboard(datasource, result, fields, isPreview) {
10291
+ function getStoryboard(datasource, result, fields, isPreview, formContextId) {
10225
10292
  var _loop = function (i) {
10226
10293
  var dataItem = datasource[i];
10227
10294
  var resultItem = {}; //数据初始化:根据id,字段类型获取默认属性
@@ -10263,7 +10330,7 @@ function getStoryboard(datasource, result, fields, isPreview) {
10263
10330
  if (Array.isArray(dataItem.bricks)) {
10264
10331
  var _Object$keys;
10265
10332
 
10266
- resultItem["slots"] = _.groupBy(getStoryboard(dataItem.bricks, [], fields, isPreview), "mountPoint");
10333
+ resultItem["slots"] = _.groupBy(getStoryboard(dataItem.bricks, [], fields, isPreview, formContextId), "mountPoint");
10267
10334
  (_Object$keys = Object.keys(resultItem["slots"])) === null || _Object$keys === void 0 ? void 0 : _Object$keys.forEach(item => {
10268
10335
  resultItem.slots[item] = {
10269
10336
  bricks: resultItem.slots[item],
@@ -10272,6 +10339,9 @@ function getStoryboard(datasource, result, fields, isPreview) {
10272
10339
  });
10273
10340
  }
10274
10341
 
10342
+ resultItem = _objectSpread(_objectSpread({}, resultItem), {}, {
10343
+ [symbolForFormContextId]: formContextId
10344
+ });
10275
10345
  result[i] = resultItem;
10276
10346
  };
10277
10347
 
@@ -10328,7 +10398,8 @@ class LocationContext {
10328
10398
  getContext(_ref) {
10329
10399
  var {
10330
10400
  match,
10331
- tplContextId
10401
+ tplContextId,
10402
+ formContextId
10332
10403
  } = _ref;
10333
10404
  var auth = getAuth();
10334
10405
  var context = {
@@ -10348,7 +10419,8 @@ class LocationContext {
10348
10419
  flags: this.kernel.getFeatureFlags(),
10349
10420
  segues: this.segues,
10350
10421
  storyboardContext: this.storyboardContextWrapper.get(),
10351
- tplContextId
10422
+ tplContextId,
10423
+ formContextId
10352
10424
  };
10353
10425
  return context;
10354
10426
  }
@@ -10688,10 +10760,12 @@ class LocationContext {
10688
10760
 
10689
10761
  var tplStack = _arguments.length > 4 && _arguments[4] !== undefined ? _arguments[4] : [];
10690
10762
  var tplContextId = brickConf[symbolForTplContextId];
10763
+ var formContextId = brickConf[symbolForFormContextId];
10691
10764
 
10692
10765
  var context = _this6.getContext({
10693
10766
  match,
10694
- tplContextId
10767
+ tplContextId,
10768
+ formContextId
10695
10769
  }); // First, check whether the brick should be rendered.
10696
10770
 
10697
10771
 
@@ -10721,6 +10795,10 @@ class LocationContext {
10721
10795
  tplStack.push(tplTagName);
10722
10796
  }
10723
10797
 
10798
+ if (brickConf.brick === formRenderer) {
10799
+ brickConf.properties.formData = JSON.stringify(brickConf.properties.formData);
10800
+ }
10801
+
10724
10802
  var brick = {};
10725
10803
  yield _this6.storyboardContextWrapper.define(brickConf.context, context, brick);
10726
10804
  yield _this6.preCheckPermissions(brickConf, context);
@@ -10734,6 +10812,7 @@ class LocationContext {
10734
10812
  slotId,
10735
10813
  refForProxy: brickConf[symbolForRefForProxy],
10736
10814
  tplContextId,
10815
+ formContextId,
10737
10816
  iid: brickConf.iid
10738
10817
  }, (_brickConf$lifeCycle = brickConf.lifeCycle) !== null && _brickConf$lifeCycle !== void 0 && _brickConf$lifeCycle.onScrollIntoView ? {
10739
10818
  lifeCycle: {
@@ -10779,8 +10858,8 @@ class LocationContext {
10779
10858
  }
10780
10859
 
10781
10860
  if (brick.type === formRenderer) {
10782
- var formData = brick.properties.formData;
10783
- expandedBrickConf = ExpandCustomForm(formData, brickConf, brick.properties.isPreview);
10861
+ var formData = JSON.parse(brick.properties.formData);
10862
+ expandedBrickConf = yield ExpandCustomForm(formData, brickConf, brick.properties.isPreview, context);
10784
10863
  yield _this6.kernel.loadDynamicBricksInBrickConf(expandedBrickConf);
10785
10864
  }
10786
10865