@grafana/scenes 6.47.0--canary.1236.19306994914.0 → 6.47.0--canary.1236.19328051792.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.
|
@@ -4,6 +4,7 @@ import { MultiOrSingleValueSelect } from '../components/VariableValueSelect.js';
|
|
|
4
4
|
import { MultiValueVariable } from './MultiValueVariable.js';
|
|
5
5
|
import { sceneGraph } from '../../core/sceneGraph/index.js';
|
|
6
6
|
import React from 'react';
|
|
7
|
+
import { omit } from 'lodash';
|
|
7
8
|
|
|
8
9
|
class CustomVariable extends MultiValueVariable {
|
|
9
10
|
constructor(initialState) {
|
|
@@ -50,19 +51,19 @@ class CustomVariable extends MultiValueVariable {
|
|
|
50
51
|
if (typeof parsedOptions[0] !== "object" || parsedOptions[0] === null) {
|
|
51
52
|
throw new Error("Query must be a JSON array of strings or objects");
|
|
52
53
|
}
|
|
53
|
-
const valueProp = "value";
|
|
54
54
|
const textProp = "text";
|
|
55
|
+
const valueProp = "value";
|
|
55
56
|
return parsedOptions.map((o) => {
|
|
56
57
|
var _a;
|
|
57
58
|
return {
|
|
58
59
|
label: (_a = String(o[textProp] || o[valueProp])) == null ? void 0 : _a.trim(),
|
|
59
60
|
value: String(o[valueProp]).trim(),
|
|
60
|
-
properties: o
|
|
61
|
+
properties: omit(o, [textProp, valueProp])
|
|
61
62
|
};
|
|
62
63
|
});
|
|
63
64
|
}
|
|
64
65
|
getValueOptions(args) {
|
|
65
|
-
const options = this.state.valuesFormat === "
|
|
66
|
+
const options = this.state.valuesFormat === "json" ? this.transformJsonToOptions(this.state.query) : this.transformCsvStringToOptions(this.state.query);
|
|
66
67
|
if (!options.length) {
|
|
67
68
|
this.skipNextValidation = true;
|
|
68
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CustomVariable.js","sources":["../../../../src/variables/variants/CustomVariable.tsx"],"sourcesContent":["import { Observable, of } from 'rxjs';\n\nimport { SceneComponentProps } from '../../core/types';\nimport { VariableDependencyConfig } from '../VariableDependencyConfig';\nimport { MultiOrSingleValueSelect } from '../components/VariableValueSelect';\nimport { VariableValueOption } from '../types';\n\nimport { MultiValueVariable, MultiValueVariableState, VariableGetOptionsArgs } from './MultiValueVariable';\nimport { sceneGraph } from '../../core/sceneGraph';\nimport React from 'react';\n\nexport interface CustomVariableState extends MultiValueVariableState {\n query: string;\n valuesFormat?: 'csv' | 'json';\n}\n\nexport class CustomVariable extends MultiValueVariable<CustomVariableState> {\n protected _variableDependency = new VariableDependencyConfig(this, {\n statePaths: ['query'],\n });\n\n public constructor(initialState: Partial<CustomVariableState>) {\n super({\n type: 'custom',\n query: '',\n valuesFormat: 'csv',\n value: '',\n text: '',\n options: [],\n name: '',\n ...initialState,\n });\n }\n\n // We expose this publicly as we also need it outside the variable\n // The interpolate flag is needed since we don't always want to get the interpolated options\n public transformCsvStringToOptions(str: string, interpolate = true): VariableValueOption[] {\n str = interpolate ? sceneGraph.interpolate(this, str) : str;\n const match = str.match(/(?:\\\\,|[^,])+/g) ?? [];\n\n return match.map((text) => {\n text = text.replace(/\\\\,/g, ',');\n const textMatch = /^\\s*(.+)\\s:\\s(.+)$/g.exec(text) ?? [];\n if (textMatch.length === 3) {\n const [, key, value] = textMatch;\n return { label: key.trim(), value: value.trim() };\n } else {\n return { label: text.trim(), value: text.trim() };\n }\n });\n }\n\n public transformJsonToOptions(json: string): VariableValueOption[] {\n const parsedOptions = JSON.parse(json);\n\n if (!Array.isArray(parsedOptions)) {\n throw new Error('Query must be a JSON array');\n }\n\n if (typeof parsedOptions[0] === 'string') {\n return parsedOptions.map((value) => ({ label: value.trim(), value: value.trim() }));\n }\n\n if (typeof parsedOptions[0] !== 'object' || parsedOptions[0] === null) {\n throw new Error('Query must be a JSON array of strings or objects');\n }\n\n const
|
|
1
|
+
{"version":3,"file":"CustomVariable.js","sources":["../../../../src/variables/variants/CustomVariable.tsx"],"sourcesContent":["import { Observable, of } from 'rxjs';\n\nimport { SceneComponentProps } from '../../core/types';\nimport { VariableDependencyConfig } from '../VariableDependencyConfig';\nimport { MultiOrSingleValueSelect } from '../components/VariableValueSelect';\nimport { VariableValueOption } from '../types';\n\nimport { MultiValueVariable, MultiValueVariableState, VariableGetOptionsArgs } from './MultiValueVariable';\nimport { sceneGraph } from '../../core/sceneGraph';\nimport React from 'react';\nimport { omit } from 'lodash';\n\nexport interface CustomVariableState extends MultiValueVariableState {\n query: string;\n valuesFormat?: 'csv' | 'json';\n}\n\nexport class CustomVariable extends MultiValueVariable<CustomVariableState> {\n protected _variableDependency = new VariableDependencyConfig(this, {\n statePaths: ['query'],\n });\n\n public constructor(initialState: Partial<CustomVariableState>) {\n super({\n type: 'custom',\n query: '',\n valuesFormat: 'csv',\n value: '',\n text: '',\n options: [],\n name: '',\n ...initialState,\n });\n }\n\n // We expose this publicly as we also need it outside the variable\n // The interpolate flag is needed since we don't always want to get the interpolated options\n public transformCsvStringToOptions(str: string, interpolate = true): VariableValueOption[] {\n str = interpolate ? sceneGraph.interpolate(this, str) : str;\n const match = str.match(/(?:\\\\,|[^,])+/g) ?? [];\n\n return match.map((text) => {\n text = text.replace(/\\\\,/g, ',');\n const textMatch = /^\\s*(.+)\\s:\\s(.+)$/g.exec(text) ?? [];\n if (textMatch.length === 3) {\n const [, key, value] = textMatch;\n return { label: key.trim(), value: value.trim() };\n } else {\n return { label: text.trim(), value: text.trim() };\n }\n });\n }\n\n public transformJsonToOptions(json: string): VariableValueOption[] {\n const parsedOptions = JSON.parse(json);\n\n if (!Array.isArray(parsedOptions)) {\n throw new Error('Query must be a JSON array');\n }\n\n if (typeof parsedOptions[0] === 'string') {\n return parsedOptions.map((value) => ({ label: value.trim(), value: value.trim() }));\n }\n\n if (typeof parsedOptions[0] !== 'object' || parsedOptions[0] === null) {\n throw new Error('Query must be a JSON array of strings or objects');\n }\n\n const textProp = 'text';\n const valueProp = 'value';\n\n return parsedOptions.map((o) => ({\n label: String(o[textProp] || o[valueProp])?.trim(),\n value: String(o[valueProp]).trim(),\n properties: omit(o, [textProp, valueProp]),\n }));\n }\n\n public getValueOptions(args: VariableGetOptionsArgs): Observable<VariableValueOption[]> {\n const options =\n this.state.valuesFormat === 'json'\n ? this.transformJsonToOptions(this.state.query)\n : this.transformCsvStringToOptions(this.state.query);\n\n if (!options.length) {\n this.skipNextValidation = true;\n }\n\n return of(options);\n }\n\n public static Component = ({ model }: SceneComponentProps<MultiValueVariable>) => {\n return <MultiOrSingleValueSelect model={model} />;\n };\n}\n"],"names":["_a"],"mappings":";;;;;;;;AAiBO,MAAM,uBAAuB,kBAAwC,CAAA;AAAA,EAKnE,YAAY,YAA4C,EAAA;AAC7D,IAAM,KAAA,CAAA;AAAA,MACJ,IAAM,EAAA,QAAA;AAAA,MACN,KAAO,EAAA,EAAA;AAAA,MACP,YAAc,EAAA,KAAA;AAAA,MACd,KAAO,EAAA,EAAA;AAAA,MACP,IAAM,EAAA,EAAA;AAAA,MACN,SAAS,EAAC;AAAA,MACV,IAAM,EAAA,EAAA;AAAA,MACN,GAAG;AAAA,KACJ,CAAA;AAdH,IAAU,IAAA,CAAA,mBAAA,GAAsB,IAAI,wBAAA,CAAyB,IAAM,EAAA;AAAA,MACjE,UAAA,EAAY,CAAC,OAAO;AAAA,KACrB,CAAA;AAAA;AAaD;AAAA;AAAA,EAIO,2BAAA,CAA4B,GAAa,EAAA,WAAA,GAAc,IAA6B,EAAA;AArC7F,IAAA,IAAA,EAAA;AAsCI,IAAA,GAAA,GAAM,WAAc,GAAA,UAAA,CAAW,WAAY,CAAA,IAAA,EAAM,GAAG,CAAI,GAAA,GAAA;AACxD,IAAA,MAAM,SAAQ,EAAI,GAAA,GAAA,CAAA,KAAA,CAAM,gBAAgB,CAAA,KAA1B,YAA+B,EAAC;AAE9C,IAAO,OAAA,KAAA,CAAM,GAAI,CAAA,CAAC,IAAS,KAAA;AAzC/B,MAAAA,IAAAA,GAAAA;AA0CM,MAAO,IAAA,GAAA,IAAA,CAAK,OAAQ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,EAAA;AAuEqC,MAAA,OAAA;AAAA,QAC/B,KAAA,EAAA,CAAO,YAAO,CAAE,CAAA,QAAQ,KAAK,CAAE,CAAA,SAAS,CAAC,CAAA,KAAlC,IAAqC,GAAA,MAAA,GAAA,EAAA,CAAA,IAAA,EAAA;AAAA,QAC5C,OAAO,MAAO,CAAA,CAAA,CAAE,SAAS,CAAC,EAAE,IAAK,EAAA;AAAA,QACjC,YAAY,IAAK,CAAA,CAAA,EAAG,CAAC,QAAA,EAAU,SAAS,CAAC;AAAA,OAC3C;AAAA,KAAE,CAAA;AAAA;AACJ,EAEO,gBAAgB,IAAiE,EAAA;AACtF,IAAA,MAAM,OACJ,GAAA,IAAA,CAAK,KAAM,CAAA,YAAA,KAAiB,SACxB,IAAK,CAAA,sBAAA,CAAuB,IAAK,CAAA,KAAA,CAAM,KAAK,CAC5C,GAAA,IAAA,CAAK,2BAA4B,CAAA,IAAA,CAAK,MAAM,KAAK,CAAA;AAEvD,IAAI,IAAA,CAAC,QAAQ,MAAQ,EAAA;AACnB,MAAA,IAAA,CAAK,kBAAqB,GAAA,IAAA;AAAA;AAG5B,IAAA,OAAO,GAAG,OAAO,CAAA;AAAA;AAMrB;AA7Ea,cAAA,CA0EG,SAAY,GAAA,CAAC,EAAE,KAAA,EAAqD,KAAA;AAChF,EAAO,uBAAA,KAAA,CAAA,aAAA,CAAC,4BAAyB,KAAc,EAAA,CAAA;AACjD,CAAA;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -11891,19 +11891,19 @@ class CustomVariable extends MultiValueVariable {
|
|
|
11891
11891
|
if (typeof parsedOptions[0] !== "object" || parsedOptions[0] === null) {
|
|
11892
11892
|
throw new Error("Query must be a JSON array of strings or objects");
|
|
11893
11893
|
}
|
|
11894
|
-
const valueProp = "value";
|
|
11895
11894
|
const textProp = "text";
|
|
11895
|
+
const valueProp = "value";
|
|
11896
11896
|
return parsedOptions.map((o) => {
|
|
11897
11897
|
var _a;
|
|
11898
11898
|
return {
|
|
11899
11899
|
label: (_a = String(o[textProp] || o[valueProp])) == null ? void 0 : _a.trim(),
|
|
11900
11900
|
value: String(o[valueProp]).trim(),
|
|
11901
|
-
properties: o
|
|
11901
|
+
properties: lodash.omit(o, [textProp, valueProp])
|
|
11902
11902
|
};
|
|
11903
11903
|
});
|
|
11904
11904
|
}
|
|
11905
11905
|
getValueOptions(args) {
|
|
11906
|
-
const options = this.state.valuesFormat === "
|
|
11906
|
+
const options = this.state.valuesFormat === "json" ? this.transformJsonToOptions(this.state.query) : this.transformCsvStringToOptions(this.state.query);
|
|
11907
11907
|
if (!options.length) {
|
|
11908
11908
|
this.skipNextValidation = true;
|
|
11909
11909
|
}
|