@next-core/brick-kit 2.169.1 → 2.171.0

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
@@ -1785,11 +1785,15 @@ function _checkIf(rawIf, ctx, fn) {
1785
1785
  class StoryboardContextWrapper {
1786
1786
  constructor(tplContextId, formContextId) {
1787
1787
  _defineProperty$1(this, "data", new Map());
1788
+ _defineProperty$1(this, "batchUpdate", false);
1789
+ _defineProperty$1(this, "batchUpdateContextsNames", []);
1788
1790
  _defineProperty$1(this, "tplContextId", void 0);
1789
1791
  _defineProperty$1(this, "formContextId", void 0);
1792
+ _defineProperty$1(this, "eventName", void 0);
1790
1793
  _defineProperty$1(this, "pendingStack", []);
1791
1794
  this.tplContextId = tplContextId;
1792
1795
  this.formContextId = formContextId;
1796
+ this.eventName = this.formContextId ? "formstate.change" : this.tplContextId ? "state.change" : "context.change";
1793
1797
  }
1794
1798
  set(name, item) {
1795
1799
  if (this.data.has(name)) {
@@ -1807,6 +1811,54 @@ class StoryboardContextWrapper {
1807
1811
  var _this$data$get;
1808
1812
  return (_this$data$get = this.data.get(name)) === null || _this$data$get === void 0 ? void 0 : _this$data$get.value;
1809
1813
  }
1814
+ getAffectListByContext(name) {
1815
+ var affectNames = [name];
1816
+ this.data.forEach((value, key) => {
1817
+ if (value.type === "free-variable" && value.deps) {
1818
+ var isInDeps = value.deps.some(item => affectNames.includes(item));
1819
+ isInDeps && affectNames.push(key) && affectNames.push(...this.getAffectListByContext(key));
1820
+ }
1821
+ });
1822
+ affectNames.shift();
1823
+ return [...new Set(affectNames)];
1824
+ }
1825
+ updateValues(values, method, argsFactory) {
1826
+ this.batchUpdate = true;
1827
+ this.batchUpdateContextsNames = values.map(item => item.name);
1828
+ if ([...new Set(this.batchUpdateContextsNames)].length !== this.batchUpdateContextsNames.length) {
1829
+ throw new Error("Batch update not allow to update same item");
1830
+ }
1831
+ var updateContexts = {};
1832
+ var affectContexts = {};
1833
+ var affectDepsContextNames = [];
1834
+ values.forEach(arg => {
1835
+ var {
1836
+ name,
1837
+ value
1838
+ } = argsFactory([arg]);
1839
+ var updateContextItem = this.data.get(name);
1840
+ affectDepsContextNames.push(...this.getAffectListByContext(name));
1841
+ updateContextItem.type === "free-variable" && (updateContexts[name] = updateContextItem);
1842
+ this.updateValue(name, value, method);
1843
+ });
1844
+ affectDepsContextNames.filter(item => !updateContexts[item]).forEach(name => {
1845
+ var affectContextItem = this.data.get(name);
1846
+ affectContextItem.type === "free-variable" && (affectContexts[name] = affectContextItem);
1847
+ });
1848
+ var triggerEvent = contexts => {
1849
+ for (var key in contexts) {
1850
+ var _context$eventTarget;
1851
+ var context = contexts[key];
1852
+ (_context$eventTarget = context.eventTarget) === null || _context$eventTarget === void 0 ? void 0 : _context$eventTarget.dispatchEvent(new CustomEvent(this.eventName, {
1853
+ detail: context.value
1854
+ }));
1855
+ }
1856
+ };
1857
+ triggerEvent(updateContexts);
1858
+ triggerEvent(affectContexts);
1859
+ this.batchUpdate = false;
1860
+ return;
1861
+ }
1810
1862
  updateValue(name, value, method, callback) {
1811
1863
  var _item$eventTarget2;
1812
1864
  if (!this.data.has(name)) {
@@ -1850,7 +1902,7 @@ class StoryboardContextWrapper {
1850
1902
  var _item$eventTarget;
1851
1903
  item.loaded = true;
1852
1904
  item.value = val;
1853
- (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.formContextId ? "formstate.change" : this.tplContextId ? "state.change" : "context.change", {
1905
+ (_item$eventTarget = item.eventTarget) === null || _item$eventTarget === void 0 ? void 0 : _item$eventTarget.dispatchEvent(new CustomEvent(this.eventName, {
1854
1906
  detail: item.value
1855
1907
  }));
1856
1908
  }, err => {
@@ -1885,7 +1937,8 @@ class StoryboardContextWrapper {
1885
1937
  item.value = value;
1886
1938
  }
1887
1939
  }
1888
- (_item$eventTarget2 = item.eventTarget) === null || _item$eventTarget2 === void 0 ? void 0 : _item$eventTarget2.dispatchEvent(new CustomEvent(this.formContextId ? "formstate.change" : this.tplContextId ? "state.change" : "context.change", {
1940
+ if (this.batchUpdate) return;
1941
+ (_item$eventTarget2 = item.eventTarget) === null || _item$eventTarget2 === void 0 ? void 0 : _item$eventTarget2.dispatchEvent(new CustomEvent(this.eventName, {
1889
1942
  detail: item.value
1890
1943
  }));
1891
1944
  }
@@ -2069,11 +2122,13 @@ function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardC
2069
2122
  // This is required for tracking context, even if no `onChange` is specified.
2070
2123
  eventTarget: new EventTarget$1(),
2071
2124
  load,
2072
- loaded
2125
+ loaded,
2126
+ deps: []
2073
2127
  };
2128
+ var eventName = storyboardContextWrapper.formContextId ? "formstate.change" : storyboardContextWrapper.tplContextId ? "state.change" : "context.change";
2074
2129
  if (contextConf.onChange) {
2075
2130
  for (var handler of [].concat(contextConf.onChange)) {
2076
- newContext.eventTarget.addEventListener(storyboardContextWrapper.formContextId ? "formstate.change" : storyboardContextWrapper.tplContextId ? "state.change" : "context.change", listenerFactory(handler, mergedContext, brick));
2131
+ newContext.eventTarget.addEventListener(eventName, listenerFactory(handler, mergedContext, brick));
2077
2132
  }
2078
2133
  }
2079
2134
  if (contextConf.track) {
@@ -2081,10 +2136,11 @@ function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardC
2081
2136
  var isFormState = !!storyboardContextWrapper.formContextId;
2082
2137
  // Track its dependencies and auto update when each of them changed.
2083
2138
  var deps = (isFormState ? trackUsedFormState : isTemplateState ? trackUsedState : trackUsedContext)(load ? contextConf.resolve : contextConf.value);
2139
+ !load && (newContext.deps = deps);
2084
2140
  for (var dep of deps) {
2085
2141
  var _eventTarget;
2086
2142
  var ctx = storyboardContextWrapper.get().get(dep);
2087
- ctx === null || ctx === void 0 ? void 0 : (_eventTarget = ctx.eventTarget) === null || _eventTarget === void 0 ? void 0 : _eventTarget.addEventListener(storyboardContextWrapper.formContextId ? "formstate.change" : isTemplateState ? "state.change" : "context.change", () => {
2143
+ ctx === null || ctx === void 0 ? void 0 : (_eventTarget = ctx.eventTarget) === null || _eventTarget === void 0 ? void 0 : _eventTarget.addEventListener(eventName, batchAddListener(() => {
2088
2144
  if (load) {
2089
2145
  storyboardContextWrapper.updateValue(contextConf.name, {
2090
2146
  cache: "default"
@@ -2092,11 +2148,19 @@ function resolveFreeVariableValue(value, contextConf, mergedContext, storyboardC
2092
2148
  } else {
2093
2149
  storyboardContextWrapper.updateValue(contextConf.name, computeRealValue(contextConf.value, mergedContext, true), "replace");
2094
2150
  }
2095
- });
2151
+ }, contextConf, storyboardContextWrapper));
2096
2152
  }
2097
2153
  }
2098
2154
  storyboardContextWrapper.set(contextConf.name, newContext);
2099
2155
  }
2156
+ function batchAddListener(listener, contextConf, storyboardContextWrapper) {
2157
+ return event => {
2158
+ if (storyboardContextWrapper.batchUpdate && storyboardContextWrapper.batchUpdateContextsNames.includes(contextConf.name)) {
2159
+ return;
2160
+ }
2161
+ listener(event);
2162
+ };
2163
+ }
2100
2164
 
2101
2165
  var tplContextMap = new Map();
2102
2166
  class CustomTemplateContext {
@@ -3377,6 +3441,30 @@ function _dev_only_updateStoryboardBySnippet(appId, newSnippet) {
3377
3441
  kernel._dev_only_updateStoryboardBySnippet(appId, newSnippet);
3378
3442
  }
3379
3443
 
3444
+ /* istanbul ignore next */
3445
+ function _dev_only_getContextValue(name, _ref) {
3446
+ var {
3447
+ tplContextId
3448
+ } = _ref;
3449
+ if (tplContextId) {
3450
+ var tplContext = getCustomTemplateContext(tplContextId);
3451
+ return tplContext.state.getValue(name);
3452
+ }
3453
+ return kernel.router.getStoryboardContextWrapper().getValue(name);
3454
+ }
3455
+
3456
+ /* istanbul ignore next */
3457
+ function _dev_only_getAllContextValues(_ref2) {
3458
+ var {
3459
+ tplContextId
3460
+ } = _ref2;
3461
+ if (tplContextId) {
3462
+ var tplContext = getCustomTemplateContext(tplContextId);
3463
+ return tplContext.state.get();
3464
+ }
3465
+ return kernel.router.getStoryboardContextWrapper().get();
3466
+ }
3467
+
3380
3468
  /* istanbul ignore next */
3381
3469
  function _dev_only_updateFormPreviewSettings(appId, formId, settings) {
3382
3470
  kernel._dev_only_updateFormPreviewSettings(appId, formId, settings);
@@ -5913,6 +6001,7 @@ function runConditionalEventHandler(handler, context, runtimeBrick) {
5913
6001
  };
5914
6002
  }
5915
6003
  function listenerFactory(handler, context, runtimeBrick) {
6004
+ var _handler$batch, _handler$batch2;
5916
6005
  if (isConditionalEventHandler(handler)) {
5917
6006
  return runConditionalEventHandler(handler, context, runtimeBrick);
5918
6007
  }
@@ -5981,11 +6070,11 @@ function listenerFactory(handler, context, runtimeBrick) {
5981
6070
  case "context.replace":
5982
6071
  case "context.refresh":
5983
6072
  case "context.load":
5984
- return builtinContextListenerFactory(method, handler.args, handler, handler.callback, context, runtimeBrick);
6073
+ return builtinContextListenerFactory(method, handler.args, (_handler$batch = handler.batch) !== null && _handler$batch !== void 0 ? _handler$batch : true, handler, handler.callback, context, runtimeBrick);
5985
6074
  case "state.update":
5986
6075
  case "state.refresh":
5987
6076
  case "state.load":
5988
- return builtinStateListenerFactory(method, handler.args, handler, handler.callback, context, runtimeBrick);
6077
+ return builtinStateListenerFactory(method, handler.args, (_handler$batch2 = handler.batch) !== null && _handler$batch2 !== void 0 ? _handler$batch2 : true, handler, handler.callback, context, runtimeBrick);
5989
6078
  case "formstate.update":
5990
6079
  return builtinFormStateListenerFactory(method, handler.args, handler, handler.callback, context, runtimeBrick);
5991
6080
  case "tpl.dispatchEvent":
@@ -6099,7 +6188,22 @@ function builtinTplDispatchEventFactory(args, ifContainer, context, runtimeBrick
6099
6188
  tpl.dispatchEvent(new CustomEvent(type, init));
6100
6189
  };
6101
6190
  }
6102
- function builtinContextListenerFactory(method, args, ifContainer, callback, context, runtimeBrick) {
6191
+ function batchUpdate(args, batch, method, context, event) {
6192
+ if (batch) {
6193
+ context.updateValues(args, method, arg => {
6194
+ return argsFactory(arg, context, event)[0];
6195
+ });
6196
+ } else {
6197
+ args.forEach(arg => {
6198
+ var {
6199
+ name,
6200
+ value
6201
+ } = argsFactory([arg], context, event)[0];
6202
+ context.updateValue(name, value, method);
6203
+ });
6204
+ }
6205
+ }
6206
+ function builtinContextListenerFactory(method, args, batch, ifContainer, callback, context, runtimeBrick) {
6103
6207
  return function (event) {
6104
6208
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6105
6209
  event
@@ -6107,11 +6211,16 @@ function builtinContextListenerFactory(method, args, ifContainer, callback, cont
6107
6211
  return;
6108
6212
  }
6109
6213
  var storyboardContext = _internalApiGetStoryboardContextWrapper();
6110
- var [name, value] = argsFactory(args, context, event);
6111
- storyboardContext.updateValue(name, value, method, callback);
6214
+ var isBatch = Array.isArray(args) && args.every(isObject);
6215
+ if (isBatch && (method === "assign" || method === "replace")) {
6216
+ batchUpdate(args, batch, method, storyboardContext, event);
6217
+ } else {
6218
+ var [name, value] = argsFactory(args, context, event);
6219
+ storyboardContext.updateValue(name, value, method, callback);
6220
+ }
6112
6221
  };
6113
6222
  }
6114
- function builtinStateListenerFactory(method, args, ifContainer, callback, context, runtimeBrick) {
6223
+ function builtinStateListenerFactory(method, args, batch, ifContainer, callback, context, runtimeBrick) {
6115
6224
  return function (event) {
6116
6225
  if (!looseCheckIf(ifContainer, _objectSpread(_objectSpread({}, context), {}, {
6117
6226
  event
@@ -6119,8 +6228,14 @@ function builtinStateListenerFactory(method, args, ifContainer, callback, contex
6119
6228
  return;
6120
6229
  }
6121
6230
  var tplContext = getTplContext(context.tplContextId);
6122
- var [name, value] = argsFactory(args, context, event);
6123
- tplContext.state.updateValue(name, value, method === "update" ? "replace" : method, callback);
6231
+ var isBatch = Array.isArray(args) && args.every(isObject);
6232
+ var computedMethod = method === "update" ? "replace" : method;
6233
+ if (isBatch && computedMethod === "replace") {
6234
+ batchUpdate(args, batch, computedMethod, tplContext.state, event);
6235
+ } else {
6236
+ var [name, value] = argsFactory(args, context, event);
6237
+ tplContext.state.updateValue(name, value, computedMethod, callback);
6238
+ }
6124
6239
  };
6125
6240
  }
6126
6241
  function builtinFormStateListenerFactory(method, args, ifContainer, callback, context, runtimeBrick) {
@@ -6560,6 +6675,9 @@ class BrickNode {
6560
6675
  if (brick.iid) {
6561
6676
  node.dataset.iid = brick.iid;
6562
6677
  }
6678
+ if (brick.tplContextId) {
6679
+ node.dataset.tplContextId = brick.tplContextId;
6680
+ }
6563
6681
  setRealProperties(node, brick.properties);
6564
6682
  bindListeners(node, brick.events, brick.context);
6565
6683
  if (Array.isArray(brick.children)) {
@@ -13953,6 +14071,8 @@ var developHelper = {
13953
14071
  updateTemplatePreviewSettings: _dev_only_updateTemplatePreviewSettings,
13954
14072
  updateSnippetPreviewSettings: _dev_only_updateSnippetPreviewSettings,
13955
14073
  updateFormPreviewSettings: _dev_only_updateFormPreviewSettings,
14074
+ getContextValue: _dev_only_getContextValue,
14075
+ getAllContextValues: _dev_only_getAllContextValues,
13956
14076
  render: _dev_only_render
13957
14077
  };
13958
14078