2019-06-18 10:17:27 -05:00
|
|
|
import { NavModel, NavModelItem, NavIndex } from '@grafana/data';
|
2018-09-02 12:36:36 -05:00
|
|
|
|
2020-08-07 02:00:44 -05:00
|
|
|
const getNotFoundModel = (): NavModel => {
|
2018-09-07 10:55:38 -05:00
|
|
|
const node: NavModelItem = {
|
2018-09-02 12:36:36 -05:00
|
|
|
id: 'not-found',
|
|
|
|
text: 'Page not found',
|
2020-04-12 15:20:02 -05:00
|
|
|
icon: 'exclamation-triangle',
|
2018-09-02 12:36:36 -05:00
|
|
|
subTitle: '404 Error',
|
|
|
|
url: 'not-found',
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
node: node,
|
|
|
|
main: node,
|
|
|
|
};
|
2020-08-07 02:00:44 -05:00
|
|
|
};
|
2018-09-02 12:36:36 -05:00
|
|
|
|
2020-08-07 02:00:44 -05:00
|
|
|
export const getNavModel = (navIndex: NavIndex, id: string, fallback?: NavModel, onlyChild = false): NavModel => {
|
2018-09-02 12:36:36 -05:00
|
|
|
if (navIndex[id]) {
|
|
|
|
const node = navIndex[id];
|
2022-08-01 08:47:52 -05:00
|
|
|
const nodeWithActive = enrichNodeWithActiveState(node);
|
|
|
|
const main = onlyChild ? nodeWithActive : getSectionRoot(nodeWithActive);
|
2018-09-02 12:36:36 -05:00
|
|
|
|
|
|
|
return {
|
2022-08-01 08:47:52 -05:00
|
|
|
node: nodeWithActive,
|
2020-08-07 02:00:44 -05:00
|
|
|
main,
|
2018-09-02 12:36:36 -05:00
|
|
|
};
|
|
|
|
}
|
2018-09-13 07:10:51 -05:00
|
|
|
|
|
|
|
if (fallback) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getNotFoundModel();
|
2020-08-07 02:00:44 -05:00
|
|
|
};
|
2019-01-16 08:59:05 -06:00
|
|
|
|
2022-07-22 03:42:41 -05:00
|
|
|
function getSectionRoot(node: NavModelItem): NavModelItem {
|
2022-08-01 08:47:52 -05:00
|
|
|
return node.parentItem ? getSectionRoot(node.parentItem) : node;
|
|
|
|
}
|
2022-07-22 03:42:41 -05:00
|
|
|
|
2022-08-01 08:47:52 -05:00
|
|
|
function enrichNodeWithActiveState(node: NavModelItem): NavModelItem {
|
|
|
|
const nodeCopy = { ...node };
|
|
|
|
if (nodeCopy.parentItem) {
|
|
|
|
nodeCopy.parentItem = { ...nodeCopy.parentItem };
|
|
|
|
const root = nodeCopy.parentItem;
|
2022-07-22 03:42:41 -05:00
|
|
|
|
2022-08-01 08:47:52 -05:00
|
|
|
if (root.children) {
|
|
|
|
root.children = root.children.map((item) => {
|
|
|
|
if (item.id === node.id) {
|
|
|
|
return { ...nodeCopy, active: true };
|
|
|
|
}
|
2022-07-22 03:42:41 -05:00
|
|
|
|
2022-08-01 08:47:52 -05:00
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
2022-07-22 03:42:41 -05:00
|
|
|
|
2022-08-01 08:47:52 -05:00
|
|
|
nodeCopy.parentItem = enrichNodeWithActiveState(root);
|
|
|
|
}
|
|
|
|
return nodeCopy;
|
2022-07-22 03:42:41 -05:00
|
|
|
}
|
|
|
|
|
2019-01-16 08:59:05 -06:00
|
|
|
export const getTitleFromNavModel = (navModel: NavModel) => {
|
2019-02-13 04:14:53 -06:00
|
|
|
return `${navModel.main.text}${navModel.node.text ? ': ' + navModel.node.text : ''}`;
|
2019-01-16 08:59:05 -06:00
|
|
|
};
|