@grafana/ui
Version:
Grafana Components Library
154 lines (149 loc) • 5.79 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
var data = require('@grafana/data');
var i18n = require('@grafana/i18n');
var icon = require('../../types/icon.cjs');
;
function frameHasName(name, names) {
if (!name) {
return false;
}
return names.display.has(name) || names.raw.has(name);
}
function getFrameFieldsDisplayNames(data$1, filter, existingNames, parentData = data$1, scope = "series") {
const names = existingNames != null ? existingNames : {
display: /* @__PURE__ */ new Set(),
raw: /* @__PURE__ */ new Set(),
fields: /* @__PURE__ */ new Map(),
scopes: /* @__PURE__ */ new Map()
};
for (const frame of data$1) {
for (const field of frame.fields) {
if (filter && !filter(field)) {
continue;
}
if (field.type === data.FieldType.nestedFrames) {
field.values.forEach(
(nestedData) => getFrameFieldsDisplayNames(nestedData, filter, names, parentData, "nested")
);
continue;
}
const disp = data.getFieldDisplayName(field, frame, parentData);
names.display.add(disp);
names.fields.set(disp, field);
names.scopes.set(disp, scope);
if (field.name && disp !== field.name) {
names.raw.add(field.name);
names.fields.set(field.name, field);
names.scopes.set(field.name, scope);
}
}
}
return names;
}
function getGroupLabelForScope(scope) {
switch (scope) {
case "nested":
return i18n.t("grafana-ui.matchers.groups.nested", "Nested");
case "annotation":
return i18n.t("grafana-ui.matchers.groups.annotation", "Annotations");
case "exemplar":
return i18n.t("grafana-ui.matchers.groups.exemplar", "Exemplars");
case "series":
default:
return i18n.t("grafana-ui.matchers.groups.series", "Dataframe");
}
}
function getGroupDescriptionForScope(scope) {
switch (scope) {
case "nested":
return i18n.t("grafana-ui.matchers.groups.nested.description", "Fields from nested dataframes.");
case "annotation":
return i18n.t("grafana-ui.matchers.groups.annotation.description", "Annotations series for this panel.");
case "series":
return i18n.t("grafana-ui.matchers.groups.series.description", "Fields from the dataframes in this panel.");
case "exemplar":
return i18n.t("grafana-ui.matchers.groups.exemplar.description", "Trace exemplars for this panel.");
default:
return void 0;
}
}
function useFieldDisplayNames(data, filter, scope) {
return React.useMemo(() => getFrameFieldsDisplayNames(data, filter, void 0, void 0, scope), [data, filter, scope]);
}
function useMatcherSelectOptions(displayNames, currentName, { firstItem, fieldType, baseNameMode, scope } = {}) {
return React.useMemo(() => {
var _a;
let found = false;
const uniqueScopes = new Set(displayNames.scopes.values());
const shouldShowScopes = !scope && uniqueScopes.size > 1;
const isFound = (name) => name === currentName && (scope ? displayNames.scopes.get(name) === scope : true);
const getGroup = (name) => shouldShowScopes ? getGroupLabelForScope(displayNames.scopes.get(name)) : void 0;
const optionFactory = (getLabel, getExtraOptions) => (name) => ({
value: name,
label: getLabel(name),
group: getGroup(name),
...getExtraOptions == null ? void 0 : getExtraOptions(name)
});
const displayNameBuilder = optionFactory(
(name) => name,
(name) => {
const field = displayNames.fields.get(name);
if (!field) {
return;
}
return { icon: icon.getFieldTypeIcon(field) };
}
);
const baseFieldNameBuilder = optionFactory(
(name) => i18n.t("grafana-ui.matchers.labels.base-field-name", "{{name}} (base field name)", { name })
);
let options = [];
if (firstItem) {
options.push(firstItem);
}
const sets = [];
if (baseNameMode === data.FieldNamePickerBaseNameMode.OnlyBaseNames) {
sets.push({ set: displayNames.raw, builder: baseFieldNameBuilder });
} else {
sets.push({ set: displayNames.display, builder: displayNameBuilder });
}
if (baseNameMode !== data.FieldNamePickerBaseNameMode.ExcludeBaseNames) {
sets.push({ set: displayNames.raw, builder: baseFieldNameBuilder });
}
for (const { set, builder } of sets) {
const shouldCheckFieldType = fieldType && set === displayNames.display;
for (const name of set) {
if (!found && isFound(name)) {
found = true;
}
if (scope && displayNames.scopes.get(name) !== scope) {
continue;
}
if (shouldCheckFieldType && fieldType !== ((_a = displayNames.fields.get(name)) == null ? void 0 : _a.type)) {
continue;
}
options.push(builder(name));
}
}
if (currentName && !found) {
const notFoundOption = optionFactory(
(name) => i18n.t("grafana-ui.matchers.labels.not-found", "{{name}} (not found)", { name })
);
options.push(notFoundOption(currentName));
}
return options;
}, [baseNameMode, currentName, displayNames, fieldType, firstItem, scope]);
}
function getUniqueMatcherScopes(data) {
return /* @__PURE__ */ new Set([...getFrameFieldsDisplayNames(data).scopes.values()]);
}
exports.frameHasName = frameHasName;
exports.getFrameFieldsDisplayNames = getFrameFieldsDisplayNames;
exports.getGroupDescriptionForScope = getGroupDescriptionForScope;
exports.getGroupLabelForScope = getGroupLabelForScope;
exports.getUniqueMatcherScopes = getUniqueMatcherScopes;
exports.useFieldDisplayNames = useFieldDisplayNames;
exports.useMatcherSelectOptions = useMatcherSelectOptions;
//# sourceMappingURL=utils.cjs.map