@grafana/data#NavIndex TypeScript Examples

The following examples show how to use @grafana/data#NavIndex. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: navModel.test.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
describe('applicationReducer', () => {
  describe('when updateNavIndex is dispatched', () => {
    it('then state should be correct', () => {
      reducerTester<NavIndex>()
        .givenReducer(navIndexReducer, { ...initialState })
        .whenActionIsDispatched(
          updateNavIndex({
            id: 'parent',
            text: 'Some Text',
            children: [
              {
                id: 'child',
                text: 'Child',
              },
            ],
          })
        )
        .thenStateShouldEqual({
          ...initialState,
          child: {
            id: 'child',
            text: 'Child',
            parentItem: {
              id: 'parent',
              text: 'Some Text',
              children: [
                {
                  id: 'child',
                  text: 'Child',
                },
              ],
            },
          },
        });
    });
  });
});
Example #2
Source File: navModel.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
function buildNavIndex(navIndex: NavIndex, children: NavModelItem[], parentItem?: NavModelItem) {
  for (const node of children) {
    navIndex[node.id] = {
      ...node,
      parentItem: parentItem,
    };

    if (node.children) {
      buildNavIndex(navIndex, node.children, node);
    }
  }
}
Example #3
Source File: navModel.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
navIndexReducer = (state: NavIndex = initialState, action: AnyAction): NavIndex => {
  if (updateNavIndex.match(action)) {
    const newPages: NavIndex = {};
    const payload = action.payload;

    for (const node of payload.children) {
      newPages[node.id] = {
        ...node,
        parentItem: payload,
      };
    }

    return { ...state, ...newPages };
  }

  return state;
}
Example #4
Source File: navModel.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
export function getNavModel(navIndex: NavIndex, id: string, fallback?: NavModel): NavModel {
  if (navIndex[id]) {
    const node = navIndex[id];
    const main = {
      ...node.parentItem,
    };

    main.children = main.children.map(item => {
      return {
        ...item,
        active: item.url === node.url,
      };
    });

    return {
      node: node,
      main: main,
    };
  }

  if (fallback) {
    return fallback;
  }

  return getNotFoundModel();
}
Example #5
Source File: navModel.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
export function buildInitialState(): NavIndex {
  const navIndex: NavIndex = {};
  const rootNodes = config.bootData.navTree as NavModelItem[];
  buildNavIndex(navIndex, rootNodes);
  return navIndex;
}
Example #6
Source File: navModel.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
initialState: NavIndex = {}