grafana/public/app/core/selectors/navModel.ts
Torkel Ödegaard 264645eecd
TopNav: Dashboard settings (#52682)
* Scenes: Support new top nav

* Page: Make Page component support new and old dashboard page layouts

* Pass scrollbar props

* Fixing flex layout for dashboard

* Progress on dashboard settings working with topnav

* Updated

* Annotations working

* Starting to work fully

* Fix merge issue

* Fixed tests

* Added buttons to annotations editor

* Updating tests

* Move Page component to each page

* fixed general settings page

* Fixed versions

* Fixed annotation item page

* Variables section working

* Fixed tests

* Minor fixes to versions

* Update

* Fixing unit tests

* Adding add variable button

* Restore annotations edit form so it's the same as before

* Fixed semicolon in dashboard permissions

* Fixing unit tests

* Fixing tests

* Minor test update

* Fixing unit test

* Fixing e2e tests

* fix for e2e test

* fix a11y issues

* Changing places Settings -> General

* Trying to fix a11y

* I hope this fixes the e2e test

* Fixing merge issue

* tweak
2022-08-24 18:05:12 +02:00

67 lines
1.5 KiB
TypeScript

import { NavModel, NavModelItem, NavIndex } from '@grafana/data';
const getNotFoundModel = (): NavModel => {
const node: NavModelItem = {
id: 'not-found',
text: 'Page not found',
icon: 'exclamation-triangle',
subTitle: '404 Error',
url: 'not-found',
};
return {
node: node,
main: node,
};
};
export const getNavModel = (navIndex: NavIndex, id: string, fallback?: NavModel, onlyChild = false): NavModel => {
if (navIndex[id]) {
const node = navIndex[id];
const nodeWithActive = enrichNodeWithActiveState(node);
const main = onlyChild ? nodeWithActive : getSectionRoot(nodeWithActive);
return {
node: nodeWithActive,
main,
};
}
if (fallback) {
return fallback;
}
return getNotFoundModel();
};
function getSectionRoot(node: NavModelItem): NavModelItem {
return node.parentItem ? getSectionRoot(node.parentItem) : node;
}
function enrichNodeWithActiveState(node: NavModelItem): NavModelItem {
const nodeCopy = { ...node };
if (nodeCopy.parentItem) {
nodeCopy.parentItem = { ...nodeCopy.parentItem };
const root = nodeCopy.parentItem;
if (root.children) {
root.children = root.children.map((item) => {
if (item.id === node.id) {
return { ...nodeCopy, active: true };
}
return item;
});
}
nodeCopy.parentItem = enrichNodeWithActiveState(root);
}
return nodeCopy;
}
export const getTitleFromNavModel = (navModel: NavModel) => {
return `${navModel.main.text}${navModel.node.text ? ': ' + navModel.node.text : ''}`;
};