@grafana/scenes 5.13.0--canary.887.10699625611.0 → 5.13.0--canary.878.10702174754.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -241,7 +241,6 @@ class SceneObjectBase {
241
241
  this._deactivationHandlers = /* @__PURE__ */ new Map();
242
242
  this._subs = new rxjs.Subscription();
243
243
  this._refCount = 0;
244
- this._UNSAFE_PARENT_ACTIVATION = false;
245
244
  if (!state.key) {
246
245
  state.key = uuid.v4();
247
246
  }
@@ -398,19 +397,12 @@ class SceneObjectBase {
398
397
  }
399
398
  }
400
399
  activate() {
401
- let parentDeactivate;
402
- if (this.parent && !this.parent.isActive && this._UNSAFE_PARENT_ACTIVATION) {
403
- parentDeactivate = this.parent.activate();
404
- }
405
400
  if (!this.isActive) {
406
401
  this._internalActivate();
407
402
  }
408
403
  this._refCount++;
409
404
  let called = false;
410
405
  return () => {
411
- if (parentDeactivate) {
412
- parentDeactivate();
413
- }
414
406
  this._refCount--;
415
407
  if (called) {
416
408
  const msg = `SceneObject cancelation handler returned by activate() called a second time`;
@@ -651,6 +643,7 @@ const emptyPanelData = {
651
643
  class SceneObjectUrlSyncConfig {
652
644
  constructor(_sceneObject, _options) {
653
645
  this._sceneObject = _sceneObject;
646
+ this._nextChangeShouldAddHistoryStep = false;
654
647
  this._keys = _options.keys;
655
648
  }
656
649
  getKeys() {
@@ -665,6 +658,14 @@ class SceneObjectUrlSyncConfig {
665
658
  updateFromUrl(values) {
666
659
  this._sceneObject.updateFromUrl(values);
667
660
  }
661
+ performBrowserHistoryAction(callback) {
662
+ this._nextChangeShouldAddHistoryStep = true;
663
+ callback();
664
+ this._nextChangeShouldAddHistoryStep = false;
665
+ }
666
+ shouldCreateHistoryStep(values) {
667
+ return this._nextChangeShouldAddHistoryStep;
668
+ }
668
669
  }
669
670
 
670
671
  const INTERVAL_STRING_REGEX = /^\d+[yYmMsSwWhHdD]$/;
@@ -768,11 +769,15 @@ class SceneTimeRange extends SceneObjectBase {
768
769
  this.state.UNSAFE_nowDelay
769
770
  );
770
771
  if (update.from !== this.state.from || update.to !== this.state.to) {
771
- this.setState(update);
772
+ this._urlSync.performBrowserHistoryAction(() => {
773
+ this.setState(update);
774
+ });
772
775
  }
773
776
  };
774
777
  this.onTimeZoneChange = (timeZone) => {
775
- this.setState({ timeZone });
778
+ this._urlSync.performBrowserHistoryAction(() => {
779
+ this.setState({ timeZone });
780
+ });
776
781
  };
777
782
  this.onRefresh = () => {
778
783
  this.setState({
@@ -6009,6 +6014,16 @@ function getQueryController(sceneObject) {
6009
6014
  }
6010
6015
  return void 0;
6011
6016
  }
6017
+ function getUrlSyncManager(sceneObject) {
6018
+ let parent = sceneObject;
6019
+ while (parent) {
6020
+ if ("urlSyncManager" in parent.state) {
6021
+ return parent.state.urlSyncManager;
6022
+ }
6023
+ parent = parent.parent;
6024
+ }
6025
+ return void 0;
6026
+ }
6012
6027
 
6013
6028
  const sceneGraph = {
6014
6029
  getVariables,
@@ -6024,7 +6039,8 @@ const sceneGraph = {
6024
6039
  findObject,
6025
6040
  findAllObjects,
6026
6041
  getAncestor,
6027
- getQueryController
6042
+ getQueryController,
6043
+ getUrlSyncManager
6028
6044
  };
6029
6045
 
6030
6046
  class UniqueUrlKeyMapper {
@@ -9026,54 +9042,53 @@ var __privateSet = (obj, member, value, setter) => {
9026
9042
  setter ? setter.call(obj, value) : member.set(obj, value);
9027
9043
  return value;
9028
9044
  };
9029
- var _onStateChanged, _cache, _location;
9045
+ var _cache, _location;
9046
+ class NewSceneObjectAddedEvent extends data.BusEventWithPayload {
9047
+ }
9048
+ NewSceneObjectAddedEvent.type = "new-scene-object-added";
9030
9049
  class UrlSyncManager {
9031
- constructor() {
9050
+ constructor(_options = {}) {
9032
9051
  this._urlKeyMapper = new UniqueUrlKeyMapper();
9033
- this._stateSub = null;
9034
9052
  this._paramsCache = new UrlParamsCache();
9035
- __privateAdd(this, _onStateChanged, ({ payload }) => {
9036
- const changedObject = payload.changedObject;
9037
- if (changedObject.urlSync) {
9038
- const newUrlState = changedObject.urlSync.getUrlState();
9039
- const searchParams = runtime.locationService.getSearch();
9040
- const mappedUpdated = {};
9041
- for (const [key, newUrlValue] of Object.entries(newUrlState)) {
9042
- const uniqueKey = this._urlKeyMapper.getUniqueKey(key, changedObject);
9043
- const currentUrlValue = searchParams.getAll(uniqueKey);
9044
- if (!isUrlValueEqual(currentUrlValue, newUrlValue)) {
9045
- mappedUpdated[uniqueKey] = newUrlValue;
9046
- }
9047
- }
9048
- if (Object.keys(mappedUpdated).length > 0) {
9049
- writeSceneLog("UrlSyncManager", "onStateChange updating URL");
9050
- runtime.locationService.partial(mappedUpdated, true);
9051
- this._lastLocation = runtime.locationService.getLocation();
9052
- }
9053
- }
9054
- });
9053
+ this._options = _options;
9055
9054
  }
9056
9055
  initSync(root) {
9057
9056
  var _a;
9058
- if (this._stateSub) {
9057
+ if (this._subs) {
9059
9058
  writeSceneLog("UrlSyncManager", "Unregister previous scene state subscription", (_a = this._sceneRoot) == null ? void 0 : _a.state.key);
9060
- this._stateSub.unsubscribe();
9059
+ this._subs.unsubscribe();
9061
9060
  }
9062
9061
  writeSceneLog("UrlSyncManager", "init", root.state.key);
9063
9062
  this._sceneRoot = root;
9064
- this._stateSub = root.subscribeToEvent(SceneObjectStateChangedEvent, __privateGet(this, _onStateChanged));
9063
+ this._subs = new rxjs.Subscription();
9064
+ this._subs.add(
9065
+ root.subscribeToEvent(SceneObjectStateChangedEvent, (evt) => {
9066
+ this.handleSceneObjectStateChanged(evt.payload.changedObject);
9067
+ })
9068
+ );
9069
+ this._subs.add(
9070
+ root.subscribeToEvent(NewSceneObjectAddedEvent, (evt) => {
9071
+ this.handleNewObject(evt.payload);
9072
+ })
9073
+ );
9065
9074
  this._urlKeyMapper.clear();
9066
9075
  this._lastLocation = runtime.locationService.getLocation();
9067
9076
  this.handleNewObject(this._sceneRoot);
9077
+ if (this._options.updateUrlOnInit) {
9078
+ const urlState = getUrlState(root);
9079
+ if (isUrlStateDifferent(urlState, this._paramsCache.getParams())) {
9080
+ runtime.locationService.partial(urlState, true);
9081
+ }
9082
+ }
9068
9083
  }
9069
9084
  cleanUp(root) {
9070
9085
  if (this._sceneRoot !== root) {
9071
9086
  return;
9072
9087
  }
9073
9088
  writeSceneLog("UrlSyncManager", "Clean up");
9074
- if (this._stateSub) {
9075
- this._stateSub.unsubscribe();
9076
- this._stateSub = null;
9089
+ if (this._subs) {
9090
+ this._subs.unsubscribe();
9091
+ this._subs = void 0;
9077
9092
  writeSceneLog(
9078
9093
  "UrlSyncManager",
9079
9094
  "Root deactived, unsub to state",
@@ -9098,11 +9113,33 @@ class UrlSyncManager {
9098
9113
  }
9099
9114
  syncStateFromUrl(sceneObj, this._paramsCache.getParams(), this._urlKeyMapper);
9100
9115
  }
9116
+ handleSceneObjectStateChanged(changedObject) {
9117
+ var _a, _b;
9118
+ if (!changedObject.urlSync) {
9119
+ return;
9120
+ }
9121
+ const newUrlState = changedObject.urlSync.getUrlState();
9122
+ const searchParams = runtime.locationService.getSearch();
9123
+ const mappedUpdated = {};
9124
+ for (const [key, newUrlValue] of Object.entries(newUrlState)) {
9125
+ const uniqueKey = this._urlKeyMapper.getUniqueKey(key, changedObject);
9126
+ const currentUrlValue = searchParams.getAll(uniqueKey);
9127
+ if (!isUrlValueEqual(currentUrlValue, newUrlValue)) {
9128
+ mappedUpdated[uniqueKey] = newUrlValue;
9129
+ }
9130
+ }
9131
+ if (Object.keys(mappedUpdated).length > 0) {
9132
+ const shouldCreateHistoryEntry = (_b = (_a = changedObject.urlSync).shouldCreateHistoryStep) == null ? void 0 : _b.call(_a, newUrlState);
9133
+ const shouldReplace = shouldCreateHistoryEntry !== true;
9134
+ writeSceneLog("UrlSyncManager", "onStateChange updating URL");
9135
+ runtime.locationService.partial(mappedUpdated, shouldReplace);
9136
+ this._lastLocation = runtime.locationService.getLocation();
9137
+ }
9138
+ }
9101
9139
  getUrlState(root) {
9102
9140
  return getUrlState(root);
9103
9141
  }
9104
9142
  }
9105
- _onStateChanged = new WeakMap();
9106
9143
  class UrlParamsCache {
9107
9144
  constructor() {
9108
9145
  __privateAdd(this, _cache, void 0);
@@ -9120,18 +9157,28 @@ class UrlParamsCache {
9120
9157
  }
9121
9158
  _cache = new WeakMap();
9122
9159
  _location = new WeakMap();
9123
- let urlSyncManager;
9124
- function getUrlSyncManager() {
9125
- if (!urlSyncManager) {
9126
- urlSyncManager = new UrlSyncManager();
9160
+ function isUrlStateDifferent(sceneUrlState, currentParams) {
9161
+ for (let key in sceneUrlState) {
9162
+ if (!isUrlValueEqual(currentParams.getAll(key), sceneUrlState[key])) {
9163
+ return true;
9164
+ }
9127
9165
  }
9128
- return urlSyncManager;
9166
+ return false;
9167
+ }
9168
+ function useUrlSyncManager(options) {
9169
+ return React.useMemo(
9170
+ () => new UrlSyncManager({
9171
+ updateUrlOnInit: options.updateUrlOnInit,
9172
+ createBrowserHistorySteps: options.createBrowserHistorySteps
9173
+ }),
9174
+ [options.updateUrlOnInit, options.createBrowserHistorySteps]
9175
+ );
9129
9176
  }
9130
9177
 
9131
- function useUrlSync(sceneRoot) {
9132
- const urlSyncManager = getUrlSyncManager();
9178
+ function useUrlSync(sceneRoot, options = {}) {
9133
9179
  const location = reactRouterDom.useLocation();
9134
9180
  const [isInitialized, setIsInitialized] = React.useState(false);
9181
+ const urlSyncManager = useUrlSyncManager(options);
9135
9182
  React.useEffect(() => {
9136
9183
  urlSyncManager.initSync(sceneRoot);
9137
9184
  setIsInitialized(true);
@@ -9148,8 +9195,13 @@ function useUrlSync(sceneRoot) {
9148
9195
  return isInitialized;
9149
9196
  }
9150
9197
 
9151
- function UrlSyncContextProvider({ children, scene }) {
9152
- const isInitialized = useUrlSync(scene);
9198
+ function UrlSyncContextProvider({
9199
+ children,
9200
+ scene,
9201
+ updateUrlOnInit,
9202
+ createBrowserHistorySteps
9203
+ }) {
9204
+ const isInitialized = useUrlSync(scene, { updateUrlOnInit, createBrowserHistorySteps });
9153
9205
  if (!isInitialized) {
9154
9206
  return null;
9155
9207
  }
@@ -11440,13 +11492,16 @@ class SceneApp extends SceneObjectBase {
11440
11492
  }
11441
11493
  SceneApp.Component = ({ model }) => {
11442
11494
  const { pages } = model.useState();
11443
- return /* @__PURE__ */ React__default["default"].createElement(reactRouterDom.Switch, null, pages.map((page) => /* @__PURE__ */ React__default["default"].createElement(reactRouterDom.Route, {
11495
+ return /* @__PURE__ */ React__default["default"].createElement(SceneAppContext.Provider, {
11496
+ value: model
11497
+ }, /* @__PURE__ */ React__default["default"].createElement(reactRouterDom.Switch, null, pages.map((page) => /* @__PURE__ */ React__default["default"].createElement(reactRouterDom.Route, {
11444
11498
  key: page.state.url,
11445
11499
  exact: false,
11446
11500
  path: page.state.url,
11447
11501
  render: (props) => renderSceneComponentWithRouteProps(page, props)
11448
- })));
11502
+ }))));
11449
11503
  };
11504
+ const SceneAppContext = React.createContext(null);
11450
11505
  const sceneAppCache = /* @__PURE__ */ new Map();
11451
11506
  function useSceneApp(factory) {
11452
11507
  const cachedApp = sceneAppCache.get(factory);
@@ -11701,6 +11756,7 @@ function SceneAppPageView({ page, routeProps }) {
11701
11756
  const containerState = containerPage.useState();
11702
11757
  const params = useAppQueryParams();
11703
11758
  const scene = page.getScene(routeProps.match);
11759
+ const appContext = React.useContext(SceneAppContext);
11704
11760
  const isInitialized = containerState.initializedScene === scene;
11705
11761
  const { layout } = page.state;
11706
11762
  React.useLayoutEffect(() => {
@@ -11711,7 +11767,7 @@ function SceneAppPageView({ page, routeProps }) {
11711
11767
  React.useEffect(() => {
11712
11768
  return () => containerPage.setState({ initializedScene: void 0 });
11713
11769
  }, [containerPage]);
11714
- const urlSyncInitialized = useUrlSync(containerPage);
11770
+ const urlSyncInitialized = useUrlSync(containerPage, appContext == null ? void 0 : appContext.state.urlSyncOptions);
11715
11771
  if (!isInitialized && !urlSyncInitialized) {
11716
11772
  return null;
11717
11773
  }
@@ -12740,6 +12796,7 @@ exports.IntervalVariable = IntervalVariable;
12740
12796
  exports.LocalValueVariable = LocalValueVariable;
12741
12797
  exports.MultiValueVariable = MultiValueVariable;
12742
12798
  exports.NestedScene = NestedScene;
12799
+ exports.NewSceneObjectAddedEvent = NewSceneObjectAddedEvent;
12743
12800
  exports.PanelBuilders = PanelBuilders;
12744
12801
  exports.PanelOptionsBuilders = PanelOptionsBuilders;
12745
12802
  exports.QueryVariable = QueryVariable;
@@ -12799,7 +12856,6 @@ exports.VizPanelMenu = VizPanelMenu;
12799
12856
  exports.behaviors = index$1;
12800
12857
  exports.dataLayers = index;
12801
12858
  exports.formatRegistry = formatRegistry;
12802
- exports.getUrlSyncManager = getUrlSyncManager;
12803
12859
  exports.isCustomVariableValue = isCustomVariableValue;
12804
12860
  exports.isDataLayer = isDataLayer;
12805
12861
  exports.isDataRequestEnricher = isDataRequestEnricher;