@grafana/scenes 4.31.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -188,32 +188,32 @@ class UserActionEvent extends data.BusEventWithPayload {
188
188
  }
189
189
  UserActionEvent.type = "scene-object-user-action";
190
190
 
191
- var __accessCheck$1 = (obj, member, msg) => {
191
+ var __accessCheck$2 = (obj, member, msg) => {
192
192
  if (!member.has(obj))
193
193
  throw TypeError("Cannot " + msg);
194
194
  };
195
- var __privateGet$1 = (obj, member, getter) => {
196
- __accessCheck$1(obj, member, "read from private field");
195
+ var __privateGet$2 = (obj, member, getter) => {
196
+ __accessCheck$2(obj, member, "read from private field");
197
197
  return getter ? getter.call(obj) : member.get(obj);
198
198
  };
199
- var __privateAdd$1 = (obj, member, value) => {
199
+ var __privateAdd$2 = (obj, member, value) => {
200
200
  if (member.has(obj))
201
201
  throw TypeError("Cannot add the same private member more than once");
202
202
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
203
203
  };
204
- var __privateSet = (obj, member, value, setter) => {
205
- __accessCheck$1(obj, member, "write to private field");
204
+ var __privateSet$1 = (obj, member, value, setter) => {
205
+ __accessCheck$2(obj, member, "write to private field");
206
206
  setter ? setter.call(obj, value) : member.set(obj, value);
207
207
  return value;
208
208
  };
209
209
  var _ref;
210
210
  class SceneObjectRef {
211
211
  constructor(ref) {
212
- __privateAdd$1(this, _ref, void 0);
213
- __privateSet(this, _ref, ref);
212
+ __privateAdd$2(this, _ref, void 0);
213
+ __privateSet$1(this, _ref, ref);
214
214
  }
215
215
  resolve() {
216
- return __privateGet$1(this, _ref);
216
+ return __privateGet$2(this, _ref);
217
217
  }
218
218
  }
219
219
  _ref = new WeakMap();
@@ -586,103 +586,22 @@ function registerRuntimeDataSource({ dataSource }) {
586
586
  runtimeDataSources.set(dataSource.uid, dataSource);
587
587
  }
588
588
 
589
- class UniqueUrlKeyMapper {
590
- constructor() {
591
- this.index = /* @__PURE__ */ new Map();
592
- }
593
- getUniqueKey(key, obj) {
594
- const objectsWithKey = this.index.get(key);
595
- if (!objectsWithKey) {
596
- throw new Error("Cannot find any scene object that uses the key '" + key + "'");
597
- }
598
- const address = objectsWithKey.findIndex((o) => o.sceneObject === obj);
599
- if (address > 0) {
600
- return `${key}-${address + 1}`;
601
- }
602
- return key;
603
- }
604
- rebuildIndex(root) {
605
- this.index.clear();
606
- this.buildIndex(root, 0);
607
- }
608
- buildIndex(sceneObject, depth) {
609
- if (sceneObject.urlSync) {
610
- for (const key of sceneObject.urlSync.getKeys()) {
611
- const hit = this.index.get(key);
612
- if (hit) {
613
- hit.push({ sceneObject, depth });
614
- hit.sort((a, b) => a.depth - b.depth);
615
- } else {
616
- this.index.set(key, [{ sceneObject, depth }]);
617
- }
618
- }
619
- }
620
- sceneObject.forEachChild((child) => this.buildIndex(child, depth + 1));
621
- }
622
- }
623
-
624
- function getUrlState(root) {
625
- const urlKeyMapper = new UniqueUrlKeyMapper();
626
- urlKeyMapper.rebuildIndex(root);
627
- const result = {};
628
- const visitNode = (obj) => {
629
- if (obj.urlSync) {
630
- const newUrlState = obj.urlSync.getUrlState();
631
- for (const [key, value] of Object.entries(newUrlState)) {
632
- if (value != null) {
633
- const uniqueKey = urlKeyMapper.getUniqueKey(key, obj);
634
- result[uniqueKey] = value;
635
- }
636
- }
637
- }
638
- obj.forEachChild(visitNode);
639
- };
640
- visitNode(root);
641
- return result;
642
- }
643
- function syncStateFromSearchParams(root, urlParams) {
644
- const urlKeyMapper = new UniqueUrlKeyMapper();
645
- urlKeyMapper.rebuildIndex(root);
646
- syncStateFromUrl(root, urlParams, urlKeyMapper);
647
- }
648
- function syncStateFromUrl(sceneObject, urlParams, urlKeyMapper) {
649
- if (sceneObject.urlSync) {
650
- const urlState = {};
651
- const currentState = sceneObject.urlSync.getUrlState();
652
- for (const key of sceneObject.urlSync.getKeys()) {
653
- const uniqueKey = urlKeyMapper.getUniqueKey(key, sceneObject);
654
- const newValue = urlParams.getAll(uniqueKey);
655
- const currentValue = currentState[key];
656
- if (isUrlValueEqual(newValue, currentValue)) {
657
- continue;
658
- }
659
- if (newValue.length > 0) {
660
- if (Array.isArray(currentValue)) {
661
- urlState[key] = newValue;
662
- } else {
663
- urlState[key] = newValue[0];
664
- }
665
- } else {
666
- urlState[key] = null;
667
- }
668
- }
669
- if (Object.keys(urlState).length > 0) {
670
- sceneObject.urlSync.updateFromUrl(urlState);
589
+ function lookupVariable(name, sceneObject) {
590
+ const variables = sceneObject.state.$variables;
591
+ if (!variables) {
592
+ if (sceneObject.parent) {
593
+ return lookupVariable(name, sceneObject.parent);
594
+ } else {
595
+ return null;
671
596
  }
672
597
  }
673
- sceneObject.forEachChild((child) => syncStateFromUrl(child, urlParams, urlKeyMapper));
674
- }
675
- function isUrlValueEqual(currentUrlValue, newUrlValue) {
676
- if (currentUrlValue.length === 0 && newUrlValue == null) {
677
- return true;
678
- }
679
- if (!Array.isArray(newUrlValue) && (currentUrlValue == null ? void 0 : currentUrlValue.length) === 1) {
680
- return newUrlValue === currentUrlValue[0];
681
- }
682
- if ((newUrlValue == null ? void 0 : newUrlValue.length) === 0 && currentUrlValue === null) {
683
- return true;
598
+ const found = variables.getByName(name);
599
+ if (found) {
600
+ return found;
601
+ } else if (sceneObject.parent) {
602
+ return lookupVariable(name, sceneObject.parent);
684
603
  }
685
- return lodash.isEqual(currentUrlValue, newUrlValue);
604
+ return null;
686
605
  }
687
606
 
688
607
  var __defProp$F = Object.defineProperty;
@@ -1314,736 +1233,718 @@ function sqlStringFormatter(value) {
1314
1233
  })}'`;
1315
1234
  }
1316
1235
 
1317
- function lookupVariable(name, sceneObject) {
1318
- const variables = sceneObject.state.$variables;
1319
- if (!variables) {
1320
- if (sceneObject.parent) {
1321
- return lookupVariable(name, sceneObject.parent);
1322
- } else {
1323
- return null;
1324
- }
1236
+ class SkipFormattingValue {
1237
+ constructor(_value) {
1238
+ this._value = _value;
1325
1239
  }
1326
- const found = variables.getByName(name);
1327
- if (found) {
1328
- return found;
1329
- } else if (sceneObject.parent) {
1330
- return lookupVariable(name, sceneObject.parent);
1240
+ formatter() {
1241
+ return this._value;
1331
1242
  }
1332
- return null;
1333
1243
  }
1334
1244
 
1335
- function sceneInterpolator(sceneObject, target, scopedVars, format, interpolations) {
1336
- if (!target) {
1337
- return target != null ? target : "";
1338
- }
1339
- VARIABLE_REGEX.lastIndex = 0;
1340
- return target.replace(VARIABLE_REGEX, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
1341
- const variableName = var1 || var2 || var3;
1342
- const fmt = fmt2 || fmt3 || format;
1343
- const variable = lookupFormatVariable(variableName, match, scopedVars, sceneObject);
1344
- if (!variable) {
1345
- if (interpolations) {
1346
- interpolations.push({ match, variableName, fieldPath, format: fmt, value: match, found: false });
1347
- }
1348
- return match;
1349
- }
1350
- const value = formatValue(sceneObject, variable, variable.getValue(fieldPath), fmt);
1351
- if (interpolations) {
1352
- interpolations.push({ match, variableName, fieldPath, format: fmt, value, found: value !== match });
1353
- }
1354
- return value;
1355
- });
1356
- }
1357
- function lookupFormatVariable(name, match, scopedVars, sceneObject) {
1358
- if (scopedVars && scopedVars.hasOwnProperty(name)) {
1359
- const scopedVar = scopedVars[name];
1360
- if (scopedVar) {
1361
- return getSceneVariableForScopedVar(name, scopedVar);
1362
- }
1245
+ class UrlTimeRangeMacro {
1246
+ constructor(name, sceneObject) {
1247
+ this.state = { name, type: "url_variable" };
1248
+ this._sceneObject = sceneObject;
1363
1249
  }
1364
- const variable = lookupVariable(name, sceneObject);
1365
- if (variable) {
1366
- return variable;
1250
+ getValue() {
1251
+ var _a;
1252
+ const timeRange = getTimeRange(this._sceneObject);
1253
+ const urlState = (_a = timeRange.urlSync) == null ? void 0 : _a.getUrlState();
1254
+ return new SkipFormattingValue(data.urlUtil.toUrlParams(urlState));
1367
1255
  }
1368
- const Macro = macrosIndex.get(name);
1369
- if (Macro) {
1370
- return new Macro(name, sceneObject, match, scopedVars);
1256
+ getValueText() {
1257
+ return "";
1371
1258
  }
1372
- return null;
1373
1259
  }
1374
- function formatValue(context, variable, value, formatNameOrFn) {
1375
- if (value === null || value === void 0) {
1376
- return "";
1260
+ class TimeFromAndToMacro {
1261
+ constructor(name, sceneObject) {
1262
+ this.state = { name, type: "time_macro" };
1263
+ this._sceneObject = sceneObject;
1377
1264
  }
1378
- if (isCustomVariableValue(value)) {
1379
- return sceneInterpolator(context, value.formatter(formatNameOrFn));
1265
+ getValue() {
1266
+ const timeRange = getTimeRange(this._sceneObject);
1267
+ if (this.state.name === "__from") {
1268
+ return timeRange.state.value.from.valueOf();
1269
+ } else {
1270
+ return timeRange.state.value.to.valueOf();
1271
+ }
1380
1272
  }
1381
- if (!Array.isArray(value) && typeof value === "object") {
1382
- value = `${value}`;
1273
+ getValueText() {
1274
+ const timeRange = getTimeRange(this._sceneObject);
1275
+ if (this.state.name === "__from") {
1276
+ return data.dateTimeFormat(timeRange.state.value.from, { timeZone: timeRange.getTimeZone() });
1277
+ } else {
1278
+ return data.dateTimeFormat(timeRange.state.value.to, { timeZone: timeRange.getTimeZone() });
1279
+ }
1383
1280
  }
1384
- if (typeof formatNameOrFn === "function") {
1385
- return formatNameOrFn(value, {
1386
- name: variable.state.name,
1387
- type: variable.state.type,
1388
- multi: variable.state.isMulti,
1389
- includeAll: variable.state.includeAll
1390
- });
1281
+ }
1282
+ class TimezoneMacro {
1283
+ constructor(name, sceneObject) {
1284
+ this.state = { name, type: "time_macro" };
1285
+ this._sceneObject = sceneObject;
1391
1286
  }
1392
- let args = [];
1393
- if (!formatNameOrFn) {
1394
- formatNameOrFn = schema.VariableFormatID.Glob;
1395
- } else {
1396
- args = formatNameOrFn.split(":");
1397
- if (args.length > 1) {
1398
- formatNameOrFn = args[0];
1399
- args = args.slice(1);
1400
- } else {
1401
- args = [];
1287
+ getValue() {
1288
+ const timeRange = getTimeRange(this._sceneObject);
1289
+ const timeZone = timeRange.getTimeZone();
1290
+ if (timeZone === "browser") {
1291
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
1402
1292
  }
1293
+ return timeZone;
1403
1294
  }
1404
- let formatter = formatRegistry.getIfExists(formatNameOrFn);
1405
- if (!formatter) {
1406
- console.error(`Variable format ${formatNameOrFn} not found. Using glob format as fallback.`);
1407
- formatter = formatRegistry.get(schema.VariableFormatID.Glob);
1295
+ getValueText() {
1296
+ return this.getValue();
1408
1297
  }
1409
- return formatter.formatter(value, args, variable);
1410
- }
1411
-
1412
- function isSceneObject(obj) {
1413
- return obj.useState !== void 0;
1414
- }
1415
- function isDataRequestEnricher(obj) {
1416
- return "enrichDataRequest" in obj;
1417
- }
1418
- function isFiltersRequestEnricher(obj) {
1419
- return "enrichFiltersRequest" in obj;
1420
1298
  }
1421
- function isDataLayer(obj) {
1422
- return "isDataLayer" in obj;
1299
+ class IntervalMacro {
1300
+ constructor(name, sceneObject, match) {
1301
+ this.state = { name, type: "time_macro", match };
1302
+ this._sceneObject = sceneObject;
1303
+ }
1304
+ getValue() {
1305
+ var _a;
1306
+ const data = getData(this._sceneObject);
1307
+ if (data) {
1308
+ const request = (_a = data.state.data) == null ? void 0 : _a.request;
1309
+ if (!request) {
1310
+ return this.state.match;
1311
+ }
1312
+ if (this.state.name === "__interval_ms") {
1313
+ return request.intervalMs;
1314
+ }
1315
+ return request.interval;
1316
+ }
1317
+ return this.state.match;
1318
+ }
1423
1319
  }
1424
1320
 
1425
- var __accessCheck = (obj, member, msg) => {
1426
- if (!member.has(obj))
1427
- throw TypeError("Cannot " + msg);
1428
- };
1429
- var __privateGet = (obj, member, getter) => {
1430
- __accessCheck(obj, member, "read from private field");
1431
- return getter ? getter.call(obj) : member.get(obj);
1432
- };
1433
- var __privateAdd = (obj, member, value) => {
1434
- if (member.has(obj))
1435
- throw TypeError("Cannot add the same private member more than once");
1436
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1437
- };
1438
- var _running;
1439
- function isQueryController(s) {
1440
- return "isQueryController" in s;
1441
- }
1442
- class SceneQueryController extends SceneObjectBase {
1443
- constructor() {
1444
- super({ isRunning: false });
1445
- this.isQueryController = true;
1446
- __privateAdd(this, _running, /* @__PURE__ */ new Set());
1447
- this.addActivationHandler(() => {
1448
- return () => __privateGet(this, _running).clear();
1449
- });
1450
- }
1451
- queryStarted(entry) {
1452
- __privateGet(this, _running).add(entry);
1453
- this.changeRunningQueryCount(1);
1454
- if (!this.state.isRunning) {
1455
- this.setState({ isRunning: true });
1456
- }
1321
+ class AllVariablesMacro {
1322
+ constructor(name, sceneObject) {
1323
+ this.state = { name, type: "url_variable" };
1324
+ this._sceneObject = sceneObject;
1457
1325
  }
1458
- queryCompleted(entry) {
1459
- if (!__privateGet(this, _running).has(entry)) {
1460
- return;
1461
- }
1462
- __privateGet(this, _running).delete(entry);
1463
- this.changeRunningQueryCount(-1);
1464
- if (__privateGet(this, _running).size === 0) {
1465
- this.setState({ isRunning: false });
1326
+ getValue() {
1327
+ const allVars = collectAllVariables(this._sceneObject);
1328
+ const format = formatRegistry.get(schema.VariableFormatID.QueryParam);
1329
+ const params = [];
1330
+ for (const name of Object.keys(allVars)) {
1331
+ const variable = allVars[name];
1332
+ const value = variable.getValue();
1333
+ if (!value) {
1334
+ continue;
1335
+ }
1336
+ if (isCustomVariableValue(value)) {
1337
+ params.push(value.formatter(schema.VariableFormatID.QueryParam));
1338
+ } else {
1339
+ params.push(format.formatter(value, [], variable));
1340
+ }
1466
1341
  }
1342
+ return new SkipFormattingValue(params.join("&"));
1467
1343
  }
1468
- changeRunningQueryCount(dir) {
1469
- var _a;
1470
- window.__grafanaRunningQueryCount = ((_a = window.__grafanaRunningQueryCount) != null ? _a : 0) + dir;
1344
+ getValueText() {
1345
+ return "";
1471
1346
  }
1472
- cancelAll() {
1473
- var _a;
1474
- for (const entry of __privateGet(this, _running).values()) {
1475
- (_a = entry.cancel) == null ? void 0 : _a.call(entry);
1347
+ }
1348
+ function collectAllVariables(sceneObject, record = {}) {
1349
+ if (sceneObject.state.$variables) {
1350
+ for (const variable of sceneObject.state.$variables.state.variables) {
1351
+ if (variable.state.skipUrlSync) {
1352
+ continue;
1353
+ }
1354
+ if (!record[variable.state.name]) {
1355
+ record[variable.state.name] = variable;
1356
+ }
1476
1357
  }
1477
1358
  }
1359
+ if (sceneObject.parent) {
1360
+ collectAllVariables(sceneObject.parent, record);
1361
+ }
1362
+ return record;
1478
1363
  }
1479
- _running = new WeakMap();
1480
1364
 
1481
- function getVariables(sceneObject) {
1482
- var _a;
1483
- return (_a = getClosest(sceneObject, (s) => s.state.$variables)) != null ? _a : EmptyVariableSet;
1484
- }
1485
- function getData(sceneObject) {
1486
- var _a;
1487
- return (_a = getClosest(sceneObject, (s) => s.state.$data)) != null ? _a : EmptyDataNode;
1488
- }
1489
- function isSceneLayout(s) {
1490
- return "isDraggable" in s;
1365
+ var __defProp$D = Object.defineProperty;
1366
+ var __defProps$r = Object.defineProperties;
1367
+ var __getOwnPropDescs$r = Object.getOwnPropertyDescriptors;
1368
+ var __getOwnPropSymbols$D = Object.getOwnPropertySymbols;
1369
+ var __hasOwnProp$D = Object.prototype.hasOwnProperty;
1370
+ var __propIsEnum$D = Object.prototype.propertyIsEnumerable;
1371
+ var __defNormalProp$D = (obj, key, value) => key in obj ? __defProp$D(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1372
+ var __spreadValues$D = (a, b) => {
1373
+ for (var prop in b || (b = {}))
1374
+ if (__hasOwnProp$D.call(b, prop))
1375
+ __defNormalProp$D(a, prop, b[prop]);
1376
+ if (__getOwnPropSymbols$D)
1377
+ for (var prop of __getOwnPropSymbols$D(b)) {
1378
+ if (__propIsEnum$D.call(b, prop))
1379
+ __defNormalProp$D(a, prop, b[prop]);
1380
+ }
1381
+ return a;
1382
+ };
1383
+ var __spreadProps$r = (a, b) => __defProps$r(a, __getOwnPropDescs$r(b));
1384
+ function getTemplateProxyForField(field, frame, frames) {
1385
+ return new Proxy(
1386
+ {},
1387
+ {
1388
+ get: (obj, key) => {
1389
+ if (key === "name") {
1390
+ return field.name;
1391
+ }
1392
+ if (key === "displayName") {
1393
+ return data.getFieldDisplayName(field, frame, frames);
1394
+ }
1395
+ if (key === "labels" || key === "formattedLabels") {
1396
+ if (!field.labels) {
1397
+ return "";
1398
+ }
1399
+ return __spreadProps$r(__spreadValues$D({}, field.labels), {
1400
+ __values: Object.values(field.labels).sort().join(", "),
1401
+ toString: () => {
1402
+ return data.formatLabels(field.labels, "", true);
1403
+ }
1404
+ });
1405
+ }
1406
+ return void 0;
1407
+ }
1408
+ }
1409
+ );
1491
1410
  }
1492
- function getLayout(scene) {
1493
- const parent = getClosest(scene, (s) => isSceneLayout(s) ? s : void 0);
1494
- if (parent) {
1495
- return parent;
1411
+
1412
+ class ValueMacro {
1413
+ constructor(name, sceneObject, _match, _scopedVars) {
1414
+ this._match = _match;
1415
+ this._scopedVars = _scopedVars;
1416
+ this.state = { name, type: "__value" };
1496
1417
  }
1497
- return null;
1498
- }
1499
- function interpolate(sceneObject, value, scopedVars, format, interpolations) {
1500
- if (value === "" || value == null) {
1418
+ getValue(fieldPath) {
1419
+ var _a, _b;
1420
+ const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1421
+ if (!dataContext) {
1422
+ return this._match;
1423
+ }
1424
+ const { frame, rowIndex, field, calculatedValue } = dataContext.value;
1425
+ if (calculatedValue) {
1426
+ switch (fieldPath) {
1427
+ case "numeric":
1428
+ return calculatedValue.numeric;
1429
+ case "raw":
1430
+ return calculatedValue.numeric;
1431
+ case "time":
1432
+ return "";
1433
+ case "text":
1434
+ default:
1435
+ return data.formattedValueToString(calculatedValue);
1436
+ }
1437
+ }
1438
+ if (rowIndex == null) {
1439
+ return this._match;
1440
+ }
1441
+ if (fieldPath === "time") {
1442
+ const timeField = frame.fields.find((f) => f.type === data.FieldType.time);
1443
+ return timeField ? timeField.values.get(rowIndex) : void 0;
1444
+ }
1445
+ if (!field) {
1446
+ return this._match;
1447
+ }
1448
+ const value = field.values.get(rowIndex);
1449
+ if (fieldPath === "raw") {
1450
+ return value;
1451
+ }
1452
+ const displayProcessor = (_b = field.display) != null ? _b : fallbackDisplayProcessor;
1453
+ const result = displayProcessor(value);
1454
+ switch (fieldPath) {
1455
+ case "numeric":
1456
+ return result.numeric;
1457
+ case "text":
1458
+ default:
1459
+ return data.formattedValueToString(result);
1460
+ }
1461
+ }
1462
+ getValueText() {
1501
1463
  return "";
1502
1464
  }
1503
- return sceneInterpolator(sceneObject, value, scopedVars, format, interpolations);
1504
1465
  }
1505
- function hasVariableDependencyInLoadingState(sceneObject) {
1506
- if (!sceneObject.variableDependency) {
1507
- return false;
1466
+ const fallbackDisplayProcessor = data.getDisplayProcessor();
1467
+ class DataMacro {
1468
+ constructor(name, sceneObject, _match, _scopedVars) {
1469
+ this._match = _match;
1470
+ this._scopedVars = _scopedVars;
1471
+ this.state = { name, type: "__data" };
1508
1472
  }
1509
- for (const name of sceneObject.variableDependency.getNames()) {
1510
- const variable = lookupVariable(name, sceneObject);
1511
- if (!variable) {
1512
- continue;
1473
+ getValue(fieldPath) {
1474
+ var _a, _b;
1475
+ const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1476
+ if (!dataContext || !fieldPath) {
1477
+ return this._match;
1513
1478
  }
1514
- const set = variable.parent;
1515
- if (set.isVariableLoadingOrWaitingToUpdate(variable)) {
1516
- return true;
1479
+ const { frame, rowIndex } = dataContext.value;
1480
+ if (rowIndex === void 0 || fieldPath === void 0) {
1481
+ return this._match;
1517
1482
  }
1483
+ const obj = {
1484
+ name: frame.name,
1485
+ refId: frame.refId,
1486
+ fields: data.getFieldDisplayValuesProxy({ frame, rowIndex })
1487
+ };
1488
+ return (_b = getFieldAccessor(fieldPath)(obj)) != null ? _b : "";
1489
+ }
1490
+ getValueText() {
1491
+ return "";
1518
1492
  }
1519
- return false;
1520
1493
  }
1521
- function findObjectInternal(scene, check, alreadySearchedChild, shouldSearchUp) {
1522
- if (check(scene)) {
1523
- return scene;
1494
+ class SeriesMacro {
1495
+ constructor(name, sceneObject, _match, _scopedVars) {
1496
+ this._match = _match;
1497
+ this._scopedVars = _scopedVars;
1498
+ this.state = { name, type: "__series" };
1524
1499
  }
1525
- let found = null;
1526
- scene.forEachChild((child) => {
1527
- if (child === alreadySearchedChild) {
1528
- return;
1500
+ getValue(fieldPath) {
1501
+ var _a;
1502
+ const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1503
+ if (!dataContext || !fieldPath) {
1504
+ return this._match;
1529
1505
  }
1530
- let maybe = findObjectInternal(child, check);
1531
- if (maybe) {
1532
- found = maybe;
1506
+ if (fieldPath !== "name") {
1507
+ return this._match;
1533
1508
  }
1534
- });
1535
- if (found) {
1536
- return found;
1537
- }
1538
- if (shouldSearchUp && scene.parent) {
1539
- return findObjectInternal(scene.parent, check, scene, true);
1509
+ const { frame, frameIndex } = dataContext.value;
1510
+ return data.getFrameDisplayName(frame, frameIndex);
1540
1511
  }
1541
- return null;
1542
- }
1543
- function findByKey(sceneObject, key) {
1544
- const found = findObject(sceneObject, (sceneToCheck) => {
1545
- return sceneToCheck.state.key === key;
1546
- });
1547
- if (!found) {
1548
- throw new Error("Unable to find scene with key " + key);
1512
+ getValueText() {
1513
+ return "";
1549
1514
  }
1550
- return found;
1551
1515
  }
1552
- function findByKeyAndType(sceneObject, key, targetType) {
1553
- const found = findObject(sceneObject, (sceneToCheck) => {
1554
- return sceneToCheck.state.key === key;
1555
- });
1556
- if (!found) {
1557
- throw new Error("Unable to find scene with key " + key);
1558
- }
1559
- if (!(found instanceof targetType)) {
1560
- throw new Error(`Found scene object with key ${key} does not match type ${targetType.name}`);
1516
+ class FieldMacro {
1517
+ constructor(name, sceneObject, _match, _scopedVars) {
1518
+ this._match = _match;
1519
+ this._scopedVars = _scopedVars;
1520
+ this.state = { name, type: "__field" };
1561
1521
  }
1562
- return found;
1563
- }
1564
- function findObject(scene, check) {
1565
- return findObjectInternal(scene, check, void 0, true);
1566
- }
1567
- function findAllObjects(scene, check) {
1568
- const found = [];
1569
- scene.forEachChild((child) => {
1570
- if (check(child)) {
1571
- found.push(child);
1572
- }
1573
- found.push(...findAllObjects(child, check));
1574
- });
1575
- return found;
1576
- }
1577
- function getDataLayers(sceneObject, localOnly = false) {
1578
- let currentLevel = sceneObject;
1579
- let collected = [];
1580
- while (currentLevel) {
1581
- const dataProvider = currentLevel.state.$data;
1582
- if (!dataProvider) {
1583
- currentLevel = currentLevel.parent;
1584
- continue;
1585
- }
1586
- if (isDataLayer(dataProvider)) {
1587
- collected = collected.concat(dataProvider);
1588
- } else {
1589
- if (dataProvider.state.$data && isDataLayer(dataProvider.state.$data)) {
1590
- collected = collected.concat(dataProvider.state.$data);
1591
- }
1522
+ getValue(fieldPath) {
1523
+ var _a, _b;
1524
+ const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1525
+ if (!dataContext || !fieldPath) {
1526
+ return this._match;
1592
1527
  }
1593
- if (localOnly && collected.length > 0) {
1594
- break;
1528
+ if (fieldPath === void 0 || fieldPath === "") {
1529
+ return this._match;
1595
1530
  }
1596
- currentLevel = currentLevel.parent;
1531
+ const { frame, field, data } = dataContext.value;
1532
+ const obj = getTemplateProxyForField(field, frame, data);
1533
+ return (_b = getFieldAccessor(fieldPath)(obj)) != null ? _b : "";
1534
+ }
1535
+ getValueText() {
1536
+ return "";
1597
1537
  }
1598
- return collected;
1599
1538
  }
1600
- function getAncestor(sceneObject, ancestorType) {
1601
- let parent = sceneObject;
1602
- while (parent) {
1603
- if (parent instanceof ancestorType) {
1604
- return parent;
1539
+
1540
+ class UrlMacro {
1541
+ constructor(name, _) {
1542
+ this.state = { name, type: "url_macro" };
1543
+ }
1544
+ getValue(fieldPath) {
1545
+ var _a;
1546
+ const location = runtime.locationService.getLocation();
1547
+ const subUrl = (_a = runtime.config.appSubUrl) != null ? _a : "";
1548
+ switch (fieldPath != null ? fieldPath : "") {
1549
+ case "params":
1550
+ return new UrlStateFormatter(location.search);
1551
+ case "path":
1552
+ return subUrl + location.pathname;
1553
+ case "":
1554
+ default:
1555
+ return subUrl + location.pathname + location.search;
1605
1556
  }
1606
- parent = parent.parent;
1607
1557
  }
1608
- if (!parent) {
1609
- throw new Error("Unable to find parent of type " + ancestorType.name);
1558
+ getValueText() {
1559
+ return "";
1610
1560
  }
1611
- return parent;
1612
1561
  }
1613
- function getQueryController(sceneObject) {
1614
- let parent = sceneObject;
1615
- while (parent) {
1616
- if (parent.state.$behaviors) {
1617
- for (const behavior of parent.state.$behaviors) {
1618
- if (isQueryController(behavior)) {
1619
- return behavior;
1620
- }
1562
+ class UrlStateFormatter {
1563
+ constructor(_urlQueryParams) {
1564
+ this._urlQueryParams = _urlQueryParams;
1565
+ }
1566
+ formatter(options) {
1567
+ if (!options) {
1568
+ return this._urlQueryParams;
1569
+ }
1570
+ const params = options.split(":");
1571
+ if (params[0] === "exclude" && params.length > 1) {
1572
+ const allParams = new URLSearchParams(this._urlQueryParams);
1573
+ for (const param of params[1].split(",")) {
1574
+ allParams.delete(param);
1621
1575
  }
1576
+ return `?${allParams}`;
1622
1577
  }
1623
- parent = parent.parent;
1624
- }
1625
- return void 0;
1626
- }
1627
-
1628
- class SkipFormattingValue {
1629
- constructor(_value) {
1630
- this._value = _value;
1631
- }
1632
- formatter() {
1633
- return this._value;
1578
+ if (params[0] === "include" && params.length > 1) {
1579
+ const allParams = new URLSearchParams(this._urlQueryParams);
1580
+ const includeOnly = params[1].split(",");
1581
+ for (const param of allParams.keys()) {
1582
+ if (!includeOnly.includes(param)) {
1583
+ allParams.delete(param);
1584
+ }
1585
+ }
1586
+ return `?${allParams}`;
1587
+ }
1588
+ return this._urlQueryParams;
1634
1589
  }
1635
1590
  }
1636
1591
 
1637
- class UrlTimeRangeMacro {
1638
- constructor(name, sceneObject) {
1639
- this.state = { name, type: "url_variable" };
1640
- this._sceneObject = sceneObject;
1592
+ class UserMacro {
1593
+ constructor(name, _) {
1594
+ this.state = { name, type: "user_macro" };
1641
1595
  }
1642
- getValue() {
1643
- var _a;
1644
- const timeRange = getTimeRange(this._sceneObject);
1645
- const urlState = (_a = timeRange.urlSync) == null ? void 0 : _a.getUrlState();
1646
- return new SkipFormattingValue(data.urlUtil.toUrlParams(urlState));
1596
+ getValue(fieldPath) {
1597
+ const user = runtime.config.bootData.user;
1598
+ switch (fieldPath) {
1599
+ case "login":
1600
+ return user.login;
1601
+ case "email":
1602
+ return user.email;
1603
+ case "id":
1604
+ default:
1605
+ return String(user.id);
1606
+ }
1647
1607
  }
1648
1608
  getValueText() {
1649
1609
  return "";
1650
1610
  }
1651
1611
  }
1652
- class TimeFromAndToMacro {
1653
- constructor(name, sceneObject) {
1654
- this.state = { name, type: "time_macro" };
1655
- this._sceneObject = sceneObject;
1612
+ class OrgMacro {
1613
+ constructor(name, _) {
1614
+ this.state = { name, type: "org_macro" };
1656
1615
  }
1657
- getValue() {
1658
- const timeRange = getTimeRange(this._sceneObject);
1659
- if (this.state.name === "__from") {
1660
- return timeRange.state.value.from.valueOf();
1661
- } else {
1662
- return timeRange.state.value.to.valueOf();
1616
+ getValue(fieldPath) {
1617
+ const user = runtime.config.bootData.user;
1618
+ switch (fieldPath) {
1619
+ case "name":
1620
+ return user.orgName;
1621
+ case "id":
1622
+ default:
1623
+ return String(user.orgId);
1663
1624
  }
1664
1625
  }
1665
1626
  getValueText() {
1666
- const timeRange = getTimeRange(this._sceneObject);
1667
- if (this.state.name === "__from") {
1668
- return data.dateTimeFormat(timeRange.state.value.from, { timeZone: timeRange.getTimeZone() });
1669
- } else {
1670
- return data.dateTimeFormat(timeRange.state.value.to, { timeZone: timeRange.getTimeZone() });
1671
- }
1627
+ return "";
1672
1628
  }
1673
1629
  }
1674
- class TimezoneMacro {
1675
- constructor(name, sceneObject) {
1676
- this.state = { name, type: "time_macro" };
1677
- this._sceneObject = sceneObject;
1678
- }
1679
- getValue() {
1680
- const timeRange = getTimeRange(this._sceneObject);
1681
- const timeZone = timeRange.getTimeZone();
1682
- if (timeZone === "browser") {
1683
- return Intl.DateTimeFormat().resolvedOptions().timeZone;
1684
- }
1685
- return timeZone;
1686
- }
1687
- getValueText() {
1688
- return this.getValue();
1630
+
1631
+ const macrosIndex = /* @__PURE__ */ new Map([
1632
+ [data.DataLinkBuiltInVars.includeVars, AllVariablesMacro],
1633
+ [data.DataLinkBuiltInVars.keepTime, UrlTimeRangeMacro],
1634
+ ["__value", ValueMacro],
1635
+ ["__data", DataMacro],
1636
+ ["__series", SeriesMacro],
1637
+ ["__field", FieldMacro],
1638
+ ["__url", UrlMacro],
1639
+ ["__from", TimeFromAndToMacro],
1640
+ ["__to", TimeFromAndToMacro],
1641
+ ["__timezone", TimezoneMacro],
1642
+ ["__user", UserMacro],
1643
+ ["__org", OrgMacro],
1644
+ ["__interval", IntervalMacro],
1645
+ ["__interval_ms", IntervalMacro]
1646
+ ]);
1647
+ function registerVariableMacro(name, macro) {
1648
+ if (macrosIndex.get(name)) {
1649
+ throw new Error(`Macro already registered ${name}`);
1689
1650
  }
1651
+ macrosIndex.set(name, macro);
1652
+ return () => {
1653
+ macrosIndex.delete(name);
1654
+ };
1690
1655
  }
1691
- class IntervalMacro {
1692
- constructor(name, sceneObject, match) {
1693
- this.state = { name, type: "time_macro", match };
1694
- this._sceneObject = sceneObject;
1656
+
1657
+ function sceneInterpolator(sceneObject, target, scopedVars, format, interpolations) {
1658
+ if (!target) {
1659
+ return target != null ? target : "";
1695
1660
  }
1696
- getValue() {
1697
- var _a;
1698
- const data = getData(this._sceneObject);
1699
- if (data) {
1700
- const request = (_a = data.state.data) == null ? void 0 : _a.request;
1701
- if (!request) {
1702
- return this.state.match;
1703
- }
1704
- if (this.state.name === "__interval_ms") {
1705
- return request.intervalMs;
1661
+ VARIABLE_REGEX.lastIndex = 0;
1662
+ return target.replace(VARIABLE_REGEX, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
1663
+ const variableName = var1 || var2 || var3;
1664
+ const fmt = fmt2 || fmt3 || format;
1665
+ const variable = lookupFormatVariable(variableName, match, scopedVars, sceneObject);
1666
+ if (!variable) {
1667
+ if (interpolations) {
1668
+ interpolations.push({ match, variableName, fieldPath, format: fmt, value: match, found: false });
1706
1669
  }
1707
- return request.interval;
1670
+ return match;
1708
1671
  }
1709
- return this.state.match;
1710
- }
1672
+ const value = formatValue(sceneObject, variable, variable.getValue(fieldPath), fmt);
1673
+ if (interpolations) {
1674
+ interpolations.push({ match, variableName, fieldPath, format: fmt, value, found: value !== match });
1675
+ }
1676
+ return value;
1677
+ });
1711
1678
  }
1712
-
1713
- class AllVariablesMacro {
1714
- constructor(name, sceneObject) {
1715
- this.state = { name, type: "url_variable" };
1716
- this._sceneObject = sceneObject;
1717
- }
1718
- getValue() {
1719
- const allVars = collectAllVariables(this._sceneObject);
1720
- const format = formatRegistry.get(schema.VariableFormatID.QueryParam);
1721
- const params = [];
1722
- for (const name of Object.keys(allVars)) {
1723
- const variable = allVars[name];
1724
- const value = variable.getValue();
1725
- if (!value) {
1726
- continue;
1727
- }
1728
- if (isCustomVariableValue(value)) {
1729
- params.push(value.formatter(schema.VariableFormatID.QueryParam));
1730
- } else {
1731
- params.push(format.formatter(value, [], variable));
1732
- }
1679
+ function lookupFormatVariable(name, match, scopedVars, sceneObject) {
1680
+ if (scopedVars && scopedVars.hasOwnProperty(name)) {
1681
+ const scopedVar = scopedVars[name];
1682
+ if (scopedVar) {
1683
+ return getSceneVariableForScopedVar(name, scopedVar);
1733
1684
  }
1734
- return new SkipFormattingValue(params.join("&"));
1735
1685
  }
1736
- getValueText() {
1737
- return "";
1686
+ const variable = lookupVariable(name, sceneObject);
1687
+ if (variable) {
1688
+ return variable;
1738
1689
  }
1690
+ const Macro = macrosIndex.get(name);
1691
+ if (Macro) {
1692
+ return new Macro(name, sceneObject, match, scopedVars);
1693
+ }
1694
+ return null;
1739
1695
  }
1740
- function collectAllVariables(sceneObject, record = {}) {
1741
- if (sceneObject.state.$variables) {
1742
- for (const variable of sceneObject.state.$variables.state.variables) {
1743
- if (variable.state.skipUrlSync) {
1744
- continue;
1745
- }
1746
- if (!record[variable.state.name]) {
1747
- record[variable.state.name] = variable;
1748
- }
1696
+ function formatValue(context, variable, value, formatNameOrFn) {
1697
+ if (value === null || value === void 0) {
1698
+ return "";
1699
+ }
1700
+ if (isCustomVariableValue(value)) {
1701
+ return sceneInterpolator(context, value.formatter(formatNameOrFn));
1702
+ }
1703
+ if (!Array.isArray(value) && typeof value === "object") {
1704
+ value = `${value}`;
1705
+ }
1706
+ if (typeof formatNameOrFn === "function") {
1707
+ return formatNameOrFn(value, {
1708
+ name: variable.state.name,
1709
+ type: variable.state.type,
1710
+ multi: variable.state.isMulti,
1711
+ includeAll: variable.state.includeAll
1712
+ });
1713
+ }
1714
+ let args = [];
1715
+ if (!formatNameOrFn) {
1716
+ formatNameOrFn = schema.VariableFormatID.Glob;
1717
+ } else {
1718
+ args = formatNameOrFn.split(":");
1719
+ if (args.length > 1) {
1720
+ formatNameOrFn = args[0];
1721
+ args = args.slice(1);
1722
+ } else {
1723
+ args = [];
1749
1724
  }
1750
1725
  }
1751
- if (sceneObject.parent) {
1752
- collectAllVariables(sceneObject.parent, record);
1726
+ let formatter = formatRegistry.getIfExists(formatNameOrFn);
1727
+ if (!formatter) {
1728
+ console.error(`Variable format ${formatNameOrFn} not found. Using glob format as fallback.`);
1729
+ formatter = formatRegistry.get(schema.VariableFormatID.Glob);
1753
1730
  }
1754
- return record;
1731
+ return formatter.formatter(value, args, variable);
1755
1732
  }
1756
1733
 
1757
- var __defProp$D = Object.defineProperty;
1758
- var __defProps$r = Object.defineProperties;
1759
- var __getOwnPropDescs$r = Object.getOwnPropertyDescriptors;
1760
- var __getOwnPropSymbols$D = Object.getOwnPropertySymbols;
1761
- var __hasOwnProp$D = Object.prototype.hasOwnProperty;
1762
- var __propIsEnum$D = Object.prototype.propertyIsEnumerable;
1763
- var __defNormalProp$D = (obj, key, value) => key in obj ? __defProp$D(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1764
- var __spreadValues$D = (a, b) => {
1765
- for (var prop in b || (b = {}))
1766
- if (__hasOwnProp$D.call(b, prop))
1767
- __defNormalProp$D(a, prop, b[prop]);
1768
- if (__getOwnPropSymbols$D)
1769
- for (var prop of __getOwnPropSymbols$D(b)) {
1770
- if (__propIsEnum$D.call(b, prop))
1771
- __defNormalProp$D(a, prop, b[prop]);
1772
- }
1773
- return a;
1774
- };
1775
- var __spreadProps$r = (a, b) => __defProps$r(a, __getOwnPropDescs$r(b));
1776
- function getTemplateProxyForField(field, frame, frames) {
1777
- return new Proxy(
1778
- {},
1779
- {
1780
- get: (obj, key) => {
1781
- if (key === "name") {
1782
- return field.name;
1783
- }
1784
- if (key === "displayName") {
1785
- return data.getFieldDisplayName(field, frame, frames);
1786
- }
1787
- if (key === "labels" || key === "formattedLabels") {
1788
- if (!field.labels) {
1789
- return "";
1790
- }
1791
- return __spreadProps$r(__spreadValues$D({}, field.labels), {
1792
- __values: Object.values(field.labels).sort().join(", "),
1793
- toString: () => {
1794
- return data.formatLabels(field.labels, "", true);
1795
- }
1796
- });
1797
- }
1798
- return void 0;
1799
- }
1800
- }
1801
- );
1734
+ function isSceneObject(obj) {
1735
+ return obj.useState !== void 0;
1736
+ }
1737
+ function isDataRequestEnricher(obj) {
1738
+ return "enrichDataRequest" in obj;
1739
+ }
1740
+ function isFiltersRequestEnricher(obj) {
1741
+ return "enrichFiltersRequest" in obj;
1742
+ }
1743
+ function isDataLayer(obj) {
1744
+ return "isDataLayer" in obj;
1802
1745
  }
1803
1746
 
1804
- class ValueMacro {
1805
- constructor(name, sceneObject, _match, _scopedVars) {
1806
- this._match = _match;
1807
- this._scopedVars = _scopedVars;
1808
- this.state = { name, type: "__value" };
1747
+ var __accessCheck$1 = (obj, member, msg) => {
1748
+ if (!member.has(obj))
1749
+ throw TypeError("Cannot " + msg);
1750
+ };
1751
+ var __privateGet$1 = (obj, member, getter) => {
1752
+ __accessCheck$1(obj, member, "read from private field");
1753
+ return getter ? getter.call(obj) : member.get(obj);
1754
+ };
1755
+ var __privateAdd$1 = (obj, member, value) => {
1756
+ if (member.has(obj))
1757
+ throw TypeError("Cannot add the same private member more than once");
1758
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1759
+ };
1760
+ var _running;
1761
+ function isQueryController(s) {
1762
+ return "isQueryController" in s;
1763
+ }
1764
+ class SceneQueryController extends SceneObjectBase {
1765
+ constructor() {
1766
+ super({ isRunning: false });
1767
+ this.isQueryController = true;
1768
+ __privateAdd$1(this, _running, /* @__PURE__ */ new Set());
1769
+ this.addActivationHandler(() => {
1770
+ return () => __privateGet$1(this, _running).clear();
1771
+ });
1809
1772
  }
1810
- getValue(fieldPath) {
1811
- var _a, _b;
1812
- const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1813
- if (!dataContext) {
1814
- return this._match;
1815
- }
1816
- const { frame, rowIndex, field, calculatedValue } = dataContext.value;
1817
- if (calculatedValue) {
1818
- switch (fieldPath) {
1819
- case "numeric":
1820
- return calculatedValue.numeric;
1821
- case "raw":
1822
- return calculatedValue.numeric;
1823
- case "time":
1824
- return "";
1825
- case "text":
1826
- default:
1827
- return data.formattedValueToString(calculatedValue);
1828
- }
1829
- }
1830
- if (rowIndex == null) {
1831
- return this._match;
1832
- }
1833
- if (fieldPath === "time") {
1834
- const timeField = frame.fields.find((f) => f.type === data.FieldType.time);
1835
- return timeField ? timeField.values.get(rowIndex) : void 0;
1836
- }
1837
- if (!field) {
1838
- return this._match;
1773
+ queryStarted(entry) {
1774
+ __privateGet$1(this, _running).add(entry);
1775
+ this.changeRunningQueryCount(1);
1776
+ if (!this.state.isRunning) {
1777
+ this.setState({ isRunning: true });
1839
1778
  }
1840
- const value = field.values.get(rowIndex);
1841
- if (fieldPath === "raw") {
1842
- return value;
1779
+ }
1780
+ queryCompleted(entry) {
1781
+ if (!__privateGet$1(this, _running).has(entry)) {
1782
+ return;
1843
1783
  }
1844
- const displayProcessor = (_b = field.display) != null ? _b : fallbackDisplayProcessor;
1845
- const result = displayProcessor(value);
1846
- switch (fieldPath) {
1847
- case "numeric":
1848
- return result.numeric;
1849
- case "text":
1850
- default:
1851
- return data.formattedValueToString(result);
1784
+ __privateGet$1(this, _running).delete(entry);
1785
+ this.changeRunningQueryCount(-1);
1786
+ if (__privateGet$1(this, _running).size === 0) {
1787
+ this.setState({ isRunning: false });
1852
1788
  }
1853
1789
  }
1854
- getValueText() {
1855
- return "";
1856
- }
1857
- }
1858
- const fallbackDisplayProcessor = data.getDisplayProcessor();
1859
- class DataMacro {
1860
- constructor(name, sceneObject, _match, _scopedVars) {
1861
- this._match = _match;
1862
- this._scopedVars = _scopedVars;
1863
- this.state = { name, type: "__data" };
1790
+ changeRunningQueryCount(dir) {
1791
+ var _a;
1792
+ window.__grafanaRunningQueryCount = ((_a = window.__grafanaRunningQueryCount) != null ? _a : 0) + dir;
1864
1793
  }
1865
- getValue(fieldPath) {
1866
- var _a, _b;
1867
- const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1868
- if (!dataContext || !fieldPath) {
1869
- return this._match;
1870
- }
1871
- const { frame, rowIndex } = dataContext.value;
1872
- if (rowIndex === void 0 || fieldPath === void 0) {
1873
- return this._match;
1794
+ cancelAll() {
1795
+ var _a;
1796
+ for (const entry of __privateGet$1(this, _running).values()) {
1797
+ (_a = entry.cancel) == null ? void 0 : _a.call(entry);
1874
1798
  }
1875
- const obj = {
1876
- name: frame.name,
1877
- refId: frame.refId,
1878
- fields: data.getFieldDisplayValuesProxy({ frame, rowIndex })
1879
- };
1880
- return (_b = getFieldAccessor(fieldPath)(obj)) != null ? _b : "";
1881
1799
  }
1882
- getValueText() {
1800
+ }
1801
+ _running = new WeakMap();
1802
+
1803
+ function getVariables(sceneObject) {
1804
+ var _a;
1805
+ return (_a = getClosest(sceneObject, (s) => s.state.$variables)) != null ? _a : EmptyVariableSet;
1806
+ }
1807
+ function getData(sceneObject) {
1808
+ var _a;
1809
+ return (_a = getClosest(sceneObject, (s) => s.state.$data)) != null ? _a : EmptyDataNode;
1810
+ }
1811
+ function isSceneLayout(s) {
1812
+ return "isDraggable" in s;
1813
+ }
1814
+ function getLayout(scene) {
1815
+ const parent = getClosest(scene, (s) => isSceneLayout(s) ? s : void 0);
1816
+ if (parent) {
1817
+ return parent;
1818
+ }
1819
+ return null;
1820
+ }
1821
+ function interpolate(sceneObject, value, scopedVars, format, interpolations) {
1822
+ if (value === "" || value == null) {
1883
1823
  return "";
1884
1824
  }
1825
+ return sceneInterpolator(sceneObject, value, scopedVars, format, interpolations);
1885
1826
  }
1886
- class SeriesMacro {
1887
- constructor(name, sceneObject, _match, _scopedVars) {
1888
- this._match = _match;
1889
- this._scopedVars = _scopedVars;
1890
- this.state = { name, type: "__series" };
1827
+ function hasVariableDependencyInLoadingState(sceneObject) {
1828
+ if (!sceneObject.variableDependency) {
1829
+ return false;
1891
1830
  }
1892
- getValue(fieldPath) {
1893
- var _a;
1894
- const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1895
- if (!dataContext || !fieldPath) {
1896
- return this._match;
1831
+ for (const name of sceneObject.variableDependency.getNames()) {
1832
+ const variable = lookupVariable(name, sceneObject);
1833
+ if (!variable) {
1834
+ continue;
1897
1835
  }
1898
- if (fieldPath !== "name") {
1899
- return this._match;
1836
+ const set = variable.parent;
1837
+ if (set.isVariableLoadingOrWaitingToUpdate(variable)) {
1838
+ return true;
1900
1839
  }
1901
- const { frame, frameIndex } = dataContext.value;
1902
- return data.getFrameDisplayName(frame, frameIndex);
1903
- }
1904
- getValueText() {
1905
- return "";
1906
1840
  }
1841
+ return false;
1907
1842
  }
1908
- class FieldMacro {
1909
- constructor(name, sceneObject, _match, _scopedVars) {
1910
- this._match = _match;
1911
- this._scopedVars = _scopedVars;
1912
- this.state = { name, type: "__field" };
1843
+ function findObjectInternal(scene, check, alreadySearchedChild, shouldSearchUp) {
1844
+ if (check(scene)) {
1845
+ return scene;
1913
1846
  }
1914
- getValue(fieldPath) {
1915
- var _a, _b;
1916
- const dataContext = (_a = this._scopedVars) == null ? void 0 : _a.__dataContext;
1917
- if (!dataContext || !fieldPath) {
1918
- return this._match;
1847
+ let found = null;
1848
+ scene.forEachChild((child) => {
1849
+ if (child === alreadySearchedChild) {
1850
+ return;
1919
1851
  }
1920
- if (fieldPath === void 0 || fieldPath === "") {
1921
- return this._match;
1852
+ let maybe = findObjectInternal(child, check);
1853
+ if (maybe) {
1854
+ found = maybe;
1922
1855
  }
1923
- const { frame, field, data } = dataContext.value;
1924
- const obj = getTemplateProxyForField(field, frame, data);
1925
- return (_b = getFieldAccessor(fieldPath)(obj)) != null ? _b : "";
1856
+ });
1857
+ if (found) {
1858
+ return found;
1926
1859
  }
1927
- getValueText() {
1928
- return "";
1860
+ if (shouldSearchUp && scene.parent) {
1861
+ return findObjectInternal(scene.parent, check, scene, true);
1929
1862
  }
1863
+ return null;
1930
1864
  }
1931
-
1932
- class UrlMacro {
1933
- constructor(name, _) {
1934
- this.state = { name, type: "url_macro" };
1865
+ function findByKey(sceneObject, key) {
1866
+ const found = findObject(sceneObject, (sceneToCheck) => {
1867
+ return sceneToCheck.state.key === key;
1868
+ });
1869
+ if (!found) {
1870
+ throw new Error("Unable to find scene with key " + key);
1935
1871
  }
1936
- getValue(fieldPath) {
1937
- var _a;
1938
- const location = runtime.locationService.getLocation();
1939
- const subUrl = (_a = runtime.config.appSubUrl) != null ? _a : "";
1940
- switch (fieldPath != null ? fieldPath : "") {
1941
- case "params":
1942
- return new UrlStateFormatter(location.search);
1943
- case "path":
1944
- return subUrl + location.pathname;
1945
- case "":
1946
- default:
1947
- return subUrl + location.pathname + location.search;
1948
- }
1872
+ return found;
1873
+ }
1874
+ function findByKeyAndType(sceneObject, key, targetType) {
1875
+ const found = findObject(sceneObject, (sceneToCheck) => {
1876
+ return sceneToCheck.state.key === key;
1877
+ });
1878
+ if (!found) {
1879
+ throw new Error("Unable to find scene with key " + key);
1949
1880
  }
1950
- getValueText() {
1951
- return "";
1881
+ if (!(found instanceof targetType)) {
1882
+ throw new Error(`Found scene object with key ${key} does not match type ${targetType.name}`);
1952
1883
  }
1884
+ return found;
1953
1885
  }
1954
- class UrlStateFormatter {
1955
- constructor(_urlQueryParams) {
1956
- this._urlQueryParams = _urlQueryParams;
1957
- }
1958
- formatter(options) {
1959
- if (!options) {
1960
- return this._urlQueryParams;
1886
+ function findObject(scene, check) {
1887
+ return findObjectInternal(scene, check, void 0, true);
1888
+ }
1889
+ function findAllObjects(scene, check) {
1890
+ const found = [];
1891
+ scene.forEachChild((child) => {
1892
+ if (check(child)) {
1893
+ found.push(child);
1894
+ }
1895
+ found.push(...findAllObjects(child, check));
1896
+ });
1897
+ return found;
1898
+ }
1899
+ function getDataLayers(sceneObject, localOnly = false) {
1900
+ let currentLevel = sceneObject;
1901
+ let collected = [];
1902
+ while (currentLevel) {
1903
+ const dataProvider = currentLevel.state.$data;
1904
+ if (!dataProvider) {
1905
+ currentLevel = currentLevel.parent;
1906
+ continue;
1961
1907
  }
1962
- const params = options.split(":");
1963
- if (params[0] === "exclude" && params.length > 1) {
1964
- const allParams = new URLSearchParams(this._urlQueryParams);
1965
- for (const param of params[1].split(",")) {
1966
- allParams.delete(param);
1908
+ if (isDataLayer(dataProvider)) {
1909
+ collected = collected.concat(dataProvider);
1910
+ } else {
1911
+ if (dataProvider.state.$data && isDataLayer(dataProvider.state.$data)) {
1912
+ collected = collected.concat(dataProvider.state.$data);
1967
1913
  }
1968
- return `?${allParams}`;
1969
1914
  }
1970
- if (params[0] === "include" && params.length > 1) {
1971
- const allParams = new URLSearchParams(this._urlQueryParams);
1972
- const includeOnly = params[1].split(",");
1973
- for (const param of allParams.keys()) {
1974
- if (!includeOnly.includes(param)) {
1975
- allParams.delete(param);
1976
- }
1977
- }
1978
- return `?${allParams}`;
1915
+ if (localOnly && collected.length > 0) {
1916
+ break;
1979
1917
  }
1980
- return this._urlQueryParams;
1918
+ currentLevel = currentLevel.parent;
1981
1919
  }
1920
+ return collected;
1982
1921
  }
1983
-
1984
- class UserMacro {
1985
- constructor(name, _) {
1986
- this.state = { name, type: "user_macro" };
1987
- }
1988
- getValue(fieldPath) {
1989
- const user = runtime.config.bootData.user;
1990
- switch (fieldPath) {
1991
- case "login":
1992
- return user.login;
1993
- case "email":
1994
- return user.email;
1995
- case "id":
1996
- default:
1997
- return String(user.id);
1922
+ function getAncestor(sceneObject, ancestorType) {
1923
+ let parent = sceneObject;
1924
+ while (parent) {
1925
+ if (parent instanceof ancestorType) {
1926
+ return parent;
1998
1927
  }
1928
+ parent = parent.parent;
1999
1929
  }
2000
- getValueText() {
2001
- return "";
1930
+ if (!parent) {
1931
+ throw new Error("Unable to find parent of type " + ancestorType.name);
2002
1932
  }
1933
+ return parent;
2003
1934
  }
2004
- class OrgMacro {
2005
- constructor(name, _) {
2006
- this.state = { name, type: "org_macro" };
2007
- }
2008
- getValue(fieldPath) {
2009
- const user = runtime.config.bootData.user;
2010
- switch (fieldPath) {
2011
- case "name":
2012
- return user.orgName;
2013
- case "id":
2014
- default:
2015
- return String(user.orgId);
1935
+ function getQueryController(sceneObject) {
1936
+ let parent = sceneObject;
1937
+ while (parent) {
1938
+ if (parent.state.$behaviors) {
1939
+ for (const behavior of parent.state.$behaviors) {
1940
+ if (isQueryController(behavior)) {
1941
+ return behavior;
1942
+ }
1943
+ }
2016
1944
  }
1945
+ parent = parent.parent;
2017
1946
  }
2018
- getValueText() {
2019
- return "";
2020
- }
2021
- }
2022
-
2023
- const macrosIndex = /* @__PURE__ */ new Map([
2024
- [data.DataLinkBuiltInVars.includeVars, AllVariablesMacro],
2025
- [data.DataLinkBuiltInVars.keepTime, UrlTimeRangeMacro],
2026
- ["__value", ValueMacro],
2027
- ["__data", DataMacro],
2028
- ["__series", SeriesMacro],
2029
- ["__field", FieldMacro],
2030
- ["__url", UrlMacro],
2031
- ["__from", TimeFromAndToMacro],
2032
- ["__to", TimeFromAndToMacro],
2033
- ["__timezone", TimezoneMacro],
2034
- ["__user", UserMacro],
2035
- ["__org", OrgMacro],
2036
- ["__interval", IntervalMacro],
2037
- ["__interval_ms", IntervalMacro]
2038
- ]);
2039
- function registerVariableMacro(name, macro) {
2040
- if (macrosIndex.get(name)) {
2041
- throw new Error(`Macro already registered ${name}`);
2042
- }
2043
- macrosIndex.set(name, macro);
2044
- return () => {
2045
- macrosIndex.delete(name);
2046
- };
1947
+ return void 0;
2047
1948
  }
2048
1949
 
2049
1950
  const sceneGraph = {
@@ -2063,6 +1964,117 @@ const sceneGraph = {
2063
1964
  getQueryController
2064
1965
  };
2065
1966
 
1967
+ class UniqueUrlKeyMapper {
1968
+ constructor() {
1969
+ this.index = /* @__PURE__ */ new Map();
1970
+ }
1971
+ getUniqueKey(key, obj) {
1972
+ const objectsWithKey = this.index.get(key);
1973
+ if (!objectsWithKey) {
1974
+ this.index.set(key, [obj]);
1975
+ return key;
1976
+ }
1977
+ let address = objectsWithKey.findIndex((o) => o === obj);
1978
+ if (address === -1) {
1979
+ filterOutOrphanedObjects(objectsWithKey);
1980
+ objectsWithKey.push(obj);
1981
+ address = objectsWithKey.length - 1;
1982
+ }
1983
+ if (address > 0) {
1984
+ return `${key}-${address + 1}`;
1985
+ }
1986
+ return key;
1987
+ }
1988
+ clear() {
1989
+ this.index.clear();
1990
+ }
1991
+ }
1992
+ function filterOutOrphanedObjects(sceneObjects) {
1993
+ for (const obj of sceneObjects) {
1994
+ if (isOrphanOrInActive(obj)) {
1995
+ const index = sceneObjects.indexOf(obj);
1996
+ sceneObjects.splice(index, 1);
1997
+ }
1998
+ }
1999
+ }
2000
+ function isOrphanOrInActive(obj) {
2001
+ const root = obj.getRoot();
2002
+ if (!sceneGraph.findObject(root, (child) => child === obj)) {
2003
+ return true;
2004
+ }
2005
+ return false;
2006
+ }
2007
+
2008
+ function getUrlState(root) {
2009
+ const urlKeyMapper = new UniqueUrlKeyMapper();
2010
+ const result = {};
2011
+ const visitNode = (obj) => {
2012
+ if (obj.urlSync) {
2013
+ const newUrlState = obj.urlSync.getUrlState();
2014
+ for (const [key, value] of Object.entries(newUrlState)) {
2015
+ if (value != null) {
2016
+ const uniqueKey = urlKeyMapper.getUniqueKey(key, obj);
2017
+ result[uniqueKey] = value;
2018
+ }
2019
+ }
2020
+ }
2021
+ obj.forEachChild(visitNode);
2022
+ };
2023
+ visitNode(root);
2024
+ return result;
2025
+ }
2026
+ function syncStateFromSearchParams(root, urlParams) {
2027
+ const urlKeyMapper = new UniqueUrlKeyMapper();
2028
+ syncStateFromUrl(root, urlParams, urlKeyMapper);
2029
+ }
2030
+ function syncStateFromUrl(root, urlParams, urlKeyMapper) {
2031
+ if (!root.parent) {
2032
+ syncUrlStateToObject(root, urlParams, urlKeyMapper);
2033
+ }
2034
+ root.forEachChild((child) => {
2035
+ syncUrlStateToObject(child, urlParams, urlKeyMapper);
2036
+ });
2037
+ root.forEachChild((child) => syncStateFromUrl(child, urlParams, urlKeyMapper));
2038
+ }
2039
+ function syncUrlStateToObject(sceneObject, urlParams, urlKeyMapper) {
2040
+ if (sceneObject.urlSync) {
2041
+ const urlState = {};
2042
+ const currentState = sceneObject.urlSync.getUrlState();
2043
+ for (const key of sceneObject.urlSync.getKeys()) {
2044
+ const uniqueKey = urlKeyMapper.getUniqueKey(key, sceneObject);
2045
+ const newValue = urlParams.getAll(uniqueKey);
2046
+ const currentValue = currentState[key];
2047
+ if (isUrlValueEqual(newValue, currentValue)) {
2048
+ continue;
2049
+ }
2050
+ if (newValue.length > 0) {
2051
+ if (Array.isArray(currentValue)) {
2052
+ urlState[key] = newValue;
2053
+ } else {
2054
+ urlState[key] = newValue[0];
2055
+ }
2056
+ } else {
2057
+ urlState[key] = null;
2058
+ }
2059
+ }
2060
+ if (Object.keys(urlState).length > 0) {
2061
+ sceneObject.urlSync.updateFromUrl(urlState);
2062
+ }
2063
+ }
2064
+ }
2065
+ function isUrlValueEqual(currentUrlValue, newUrlValue) {
2066
+ if (currentUrlValue.length === 0 && newUrlValue == null) {
2067
+ return true;
2068
+ }
2069
+ if (!Array.isArray(newUrlValue) && (currentUrlValue == null ? void 0 : currentUrlValue.length) === 1) {
2070
+ return newUrlValue === currentUrlValue[0];
2071
+ }
2072
+ if ((newUrlValue == null ? void 0 : newUrlValue.length) === 0 && currentUrlValue === null) {
2073
+ return true;
2074
+ }
2075
+ return lodash.isEqual(currentUrlValue, newUrlValue);
2076
+ }
2077
+
2066
2078
  async function getDataSource(datasource, scopedVars) {
2067
2079
  if (datasource == null ? void 0 : datasource.uid) {
2068
2080
  const runtimeDataSource = runtimeDataSources.get(datasource.uid);
@@ -3504,7 +3516,16 @@ function AdHocFilterRenderer({ filter, model }) {
3504
3516
  label,
3505
3517
  "data-testid": `AdHocFilter-${filter.key}`,
3506
3518
  className: styles.field
3507
- }, valueSelect);
3519
+ }, /* @__PURE__ */ React__default["default"].createElement("div", {
3520
+ className: styles.wrapper
3521
+ }, /* @__PURE__ */ React__default["default"].createElement(ui.Select, {
3522
+ className: styles.operator,
3523
+ value: filter.operator,
3524
+ disabled: model.state.readOnly,
3525
+ options: model._getOperators(),
3526
+ width: "auto",
3527
+ onChange: (v) => model._updateFilter(filter, "operator", v)
3528
+ }), valueSelect));
3508
3529
  } else {
3509
3530
  return /* @__PURE__ */ React__default["default"].createElement(ui.Field, {
3510
3531
  label: "Select label",
@@ -7767,90 +7788,125 @@ IntervalVariable.Component = ({ model }) => {
7767
7788
  });
7768
7789
  };
7769
7790
 
7791
+ var __accessCheck = (obj, member, msg) => {
7792
+ if (!member.has(obj))
7793
+ throw TypeError("Cannot " + msg);
7794
+ };
7795
+ var __privateGet = (obj, member, getter) => {
7796
+ __accessCheck(obj, member, "read from private field");
7797
+ return getter ? getter.call(obj) : member.get(obj);
7798
+ };
7799
+ var __privateAdd = (obj, member, value) => {
7800
+ if (member.has(obj))
7801
+ throw TypeError("Cannot add the same private member more than once");
7802
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7803
+ };
7804
+ var __privateSet = (obj, member, value, setter) => {
7805
+ __accessCheck(obj, member, "write to private field");
7806
+ setter ? setter.call(obj, value) : member.set(obj, value);
7807
+ return value;
7808
+ };
7809
+ var _urlKeyMapper, _sceneRoot, _stateSub, _lastLocation, _paramsCache, _onStateChanged, _cache, _location;
7770
7810
  class UrlSyncManager {
7771
7811
  constructor() {
7772
- this._urlKeyMapper = new UniqueUrlKeyMapper();
7773
- this._stateSub = null;
7774
- this._locationSub = null;
7775
- this._ignoreNextLocationUpdate = false;
7776
- this._onLocationUpdate = (location) => {
7777
- if (this._ignoreNextLocationUpdate) {
7778
- this._ignoreNextLocationUpdate = false;
7779
- return;
7780
- }
7781
- if (this._lastPath !== location.pathname) {
7782
- return;
7783
- }
7784
- const urlParams = new URLSearchParams(location.search);
7785
- this._urlKeyMapper.rebuildIndex(this._sceneRoot);
7786
- syncStateFromUrl(this._sceneRoot, urlParams, this._urlKeyMapper);
7787
- this._lastPath = location.pathname;
7788
- };
7789
- this._onStateChanged = ({ payload }) => {
7812
+ __privateAdd(this, _urlKeyMapper, new UniqueUrlKeyMapper());
7813
+ __privateAdd(this, _sceneRoot, void 0);
7814
+ __privateAdd(this, _stateSub, null);
7815
+ __privateAdd(this, _lastLocation, void 0);
7816
+ __privateAdd(this, _paramsCache, new UrlParamsCache());
7817
+ __privateAdd(this, _onStateChanged, ({ payload }) => {
7790
7818
  const changedObject = payload.changedObject;
7791
7819
  if (changedObject.urlSync) {
7792
7820
  const newUrlState = changedObject.urlSync.getUrlState();
7793
7821
  const searchParams = runtime.locationService.getSearch();
7794
7822
  const mappedUpdated = {};
7795
- this._urlKeyMapper.rebuildIndex(this._sceneRoot);
7796
7823
  for (const [key, newUrlValue] of Object.entries(newUrlState)) {
7797
- const uniqueKey = this._urlKeyMapper.getUniqueKey(key, changedObject);
7824
+ const uniqueKey = __privateGet(this, _urlKeyMapper).getUniqueKey(key, changedObject);
7798
7825
  const currentUrlValue = searchParams.getAll(uniqueKey);
7799
7826
  if (!isUrlValueEqual(currentUrlValue, newUrlValue)) {
7800
7827
  mappedUpdated[uniqueKey] = newUrlValue;
7801
7828
  }
7802
7829
  }
7803
7830
  if (Object.keys(mappedUpdated).length > 0) {
7804
- this._ignoreNextLocationUpdate = true;
7831
+ writeSceneLog("UrlSyncManager", "onStateChange updating URL");
7805
7832
  runtime.locationService.partial(mappedUpdated, true);
7833
+ __privateSet(this, _lastLocation, runtime.locationService.getLocation());
7806
7834
  }
7807
7835
  }
7808
- };
7836
+ });
7809
7837
  }
7810
7838
  initSync(root) {
7811
- if (!this._locationSub) {
7812
- writeSceneLog("UrlSyncManager", "New location listen");
7813
- this._locationSub = runtime.locationService.getHistory().listen(this._onLocationUpdate);
7814
- }
7815
- if (this._stateSub) {
7816
- writeSceneLog("UrlSyncManager", "Unregister previous scene state subscription", this._sceneRoot.state.key);
7817
- this._stateSub.unsubscribe();
7839
+ var _a;
7840
+ if (__privateGet(this, _stateSub)) {
7841
+ writeSceneLog("UrlSyncManager", "Unregister previous scene state subscription", (_a = __privateGet(this, _sceneRoot)) == null ? void 0 : _a.state.key);
7842
+ __privateGet(this, _stateSub).unsubscribe();
7818
7843
  }
7819
- this._sceneRoot = root;
7820
- this._lastPath = runtime.locationService.getLocation().pathname;
7821
- this._stateSub = root.subscribeToEvent(SceneObjectStateChangedEvent, this._onStateChanged);
7822
- this.syncFrom(this._sceneRoot);
7844
+ writeSceneLog("UrlSyncManager", "init", root.state.key);
7845
+ __privateSet(this, _sceneRoot, root);
7846
+ __privateSet(this, _stateSub, root.subscribeToEvent(SceneObjectStateChangedEvent, __privateGet(this, _onStateChanged)));
7847
+ __privateGet(this, _urlKeyMapper).clear();
7848
+ __privateSet(this, _lastLocation, runtime.locationService.getLocation());
7849
+ this.handleNewObject(__privateGet(this, _sceneRoot));
7823
7850
  }
7824
7851
  cleanUp(root) {
7825
- if (this._sceneRoot !== root) {
7852
+ if (__privateGet(this, _sceneRoot) !== root) {
7826
7853
  return;
7827
7854
  }
7828
7855
  writeSceneLog("UrlSyncManager", "Clean up");
7829
- if (this._locationSub) {
7830
- this._locationSub();
7831
- writeSceneLog("UrlSyncManager", "Unregister history listen");
7832
- this._locationSub = null;
7833
- }
7834
- if (this._stateSub) {
7835
- this._stateSub.unsubscribe();
7836
- this._stateSub = null;
7856
+ if (__privateGet(this, _stateSub)) {
7857
+ __privateGet(this, _stateSub).unsubscribe();
7858
+ __privateSet(this, _stateSub, null);
7837
7859
  writeSceneLog(
7838
7860
  "UrlSyncManager",
7839
7861
  "Root deactived, unsub to state",
7840
7862
  "same key",
7841
- this._sceneRoot.state.key === root.state.key
7863
+ __privateGet(this, _sceneRoot).state.key === root.state.key
7842
7864
  );
7843
7865
  }
7866
+ __privateSet(this, _sceneRoot, void 0);
7867
+ __privateSet(this, _lastLocation, void 0);
7868
+ }
7869
+ handleNewLocation(location) {
7870
+ if (!__privateGet(this, _sceneRoot) || __privateGet(this, _lastLocation) === location) {
7871
+ return;
7872
+ }
7873
+ writeSceneLog("UrlSyncManager", "handleNewLocation");
7874
+ __privateSet(this, _lastLocation, location);
7875
+ syncStateFromUrl(__privateGet(this, _sceneRoot), __privateGet(this, _paramsCache).getParams(), __privateGet(this, _urlKeyMapper));
7844
7876
  }
7845
- syncFrom(sceneObj) {
7846
- const urlParams = runtime.locationService.getSearch();
7847
- this._urlKeyMapper.rebuildIndex(this._sceneRoot);
7848
- syncStateFromUrl(sceneObj, urlParams, this._urlKeyMapper);
7877
+ handleNewObject(sceneObj) {
7878
+ if (!__privateGet(this, _sceneRoot)) {
7879
+ return;
7880
+ }
7881
+ syncStateFromUrl(sceneObj, __privateGet(this, _paramsCache).getParams(), __privateGet(this, _urlKeyMapper));
7849
7882
  }
7850
7883
  getUrlState(root) {
7851
7884
  return getUrlState(root);
7852
7885
  }
7853
7886
  }
7887
+ _urlKeyMapper = new WeakMap();
7888
+ _sceneRoot = new WeakMap();
7889
+ _stateSub = new WeakMap();
7890
+ _lastLocation = new WeakMap();
7891
+ _paramsCache = new WeakMap();
7892
+ _onStateChanged = new WeakMap();
7893
+ class UrlParamsCache {
7894
+ constructor() {
7895
+ __privateAdd(this, _cache, void 0);
7896
+ __privateAdd(this, _location, void 0);
7897
+ }
7898
+ getParams() {
7899
+ const location = runtime.locationService.getLocation();
7900
+ if (__privateGet(this, _location) === location) {
7901
+ return __privateGet(this, _cache);
7902
+ }
7903
+ __privateSet(this, _location, location);
7904
+ __privateSet(this, _cache, new URLSearchParams(location.search));
7905
+ return __privateGet(this, _cache);
7906
+ }
7907
+ }
7908
+ _cache = new WeakMap();
7909
+ _location = new WeakMap();
7854
7910
  let urlSyncManager;
7855
7911
  function getUrlSyncManager() {
7856
7912
  if (!urlSyncManager) {
@@ -7859,6 +7915,34 @@ function getUrlSyncManager() {
7859
7915
  return urlSyncManager;
7860
7916
  }
7861
7917
 
7918
+ function useUrlSync(sceneRoot) {
7919
+ const urlSyncManager = getUrlSyncManager();
7920
+ const location = reactRouterDom.useLocation();
7921
+ const [isInitialized, setIsInitialized] = React.useState(false);
7922
+ React.useEffect(() => {
7923
+ urlSyncManager.initSync(sceneRoot);
7924
+ setIsInitialized(true);
7925
+ return () => urlSyncManager.cleanUp(sceneRoot);
7926
+ }, [sceneRoot, urlSyncManager]);
7927
+ React.useEffect(() => {
7928
+ const latestLocation = runtime.locationService.getLocation();
7929
+ const locationToHandle = latestLocation !== location ? latestLocation : location;
7930
+ if (latestLocation !== location) {
7931
+ console.log("latestLocation different from location");
7932
+ }
7933
+ urlSyncManager.handleNewLocation(locationToHandle);
7934
+ }, [sceneRoot, urlSyncManager, location]);
7935
+ return isInitialized;
7936
+ }
7937
+
7938
+ function UrlSyncContextProvider({ children, scene }) {
7939
+ const isInitialized = useUrlSync(scene);
7940
+ if (!isInitialized) {
7941
+ return null;
7942
+ }
7943
+ return children;
7944
+ }
7945
+
7862
7946
  function setWindowGrafanaSceneContext(activeScene) {
7863
7947
  const prevScene = window.__grafanaSceneContext;
7864
7948
  writeSceneLog("setWindowGrafanaScene", "set window.__grafanaSceneContext", activeScene);
@@ -7878,13 +7962,9 @@ class EmbeddedScene extends SceneObjectBase {
7878
7962
  const unsetGlobalScene = setWindowGrafanaSceneContext(this);
7879
7963
  return () => {
7880
7964
  unsetGlobalScene();
7881
- getUrlSyncManager().cleanUp(this);
7882
7965
  };
7883
7966
  });
7884
7967
  }
7885
- initUrlSync() {
7886
- getUrlSyncManager().initSync(this);
7887
- }
7888
7968
  }
7889
7969
  EmbeddedScene.Component = EmbeddedSceneRenderer;
7890
7970
  function EmbeddedSceneRenderer({ model }) {
@@ -10357,7 +10437,8 @@ function SceneAppPageView({ page, routeProps }) {
10357
10437
  React.useEffect(() => {
10358
10438
  return () => containerPage.setState({ initializedScene: void 0 });
10359
10439
  }, [containerPage]);
10360
- if (!isInitialized) {
10440
+ const urlSyncInitialized = useUrlSync(containerPage);
10441
+ if (!isInitialized && !urlSyncInitialized) {
10361
10442
  return null;
10362
10443
  }
10363
10444
  const pageNav = {
@@ -10430,13 +10511,9 @@ class SceneAppPage extends SceneObjectBase {
10430
10511
  super(state);
10431
10512
  this._sceneCache = /* @__PURE__ */ new Map();
10432
10513
  this._drilldownCache = /* @__PURE__ */ new Map();
10433
- this.addActivationHandler(() => {
10434
- return () => getUrlSyncManager().cleanUp(this);
10435
- });
10436
10514
  }
10437
10515
  initializeScene(scene) {
10438
10516
  this.setState({ initializedScene: scene });
10439
- getUrlSyncManager().initSync(this);
10440
10517
  }
10441
10518
  getScene(routeMatch) {
10442
10519
  let scene = this._sceneCache.get(routeMatch.url);
@@ -11431,6 +11508,7 @@ exports.SceneVariableValueChangedEvent = SceneVariableValueChangedEvent;
11431
11508
  exports.SplitLayout = SplitLayout;
11432
11509
  exports.TestVariable = TestVariable;
11433
11510
  exports.TextBoxVariable = TextBoxVariable;
11511
+ exports.UrlSyncContextProvider = UrlSyncContextProvider;
11434
11512
  exports.UrlSyncManager = UrlSyncManager;
11435
11513
  exports.UserActionEvent = UserActionEvent;
11436
11514
  exports.VariableDependencyConfig = VariableDependencyConfig;
@@ -11458,4 +11536,5 @@ exports.sceneGraph = sceneGraph;
11458
11536
  exports.sceneUtils = sceneUtils;
11459
11537
  exports.useSceneApp = useSceneApp;
11460
11538
  exports.useSceneObjectState = useSceneObjectState;
11539
+ exports.useUrlSync = useUrlSync;
11461
11540
  //# sourceMappingURL=index.js.map