# src/view/DocsMenu.ts

`1import { m, Vnode, Layout } from 'hslayout';
2import { DocSets } from '../DocSets';
3import { Menu, SelectorDesc } from 'hswidget';
4
5<comment>/**
6 * Creates the title menu for selecting between the different docsets.
7 * Instantiate the DocsMenu via a standard `mithril` call:```
8 * m(DocsMenu, { docSet:<pathToIndexFile>})
9 * ```
10 * DocsMenu performs the following actions:
11 * - for the first call of the view lifecycle hook, the available docSets are loaded.
12 * DocsMenu searches for an index `json` file at the location specified in the
13 * `docSet` field of the `node.attrs` parameter. If none is specified, the
14 * default is used as specified in the {@link hsDocs:DocSets.FILE DocSets FILE} setting.
15 * - DocsMenu retrieves all available docSets via {@link hsDocs:DocSets.DocSets.get DocSets.get}.
16 * - DocsMenu creates a `SelectorDesc` structure with a {@link hsWidget:hsSelector.SelectorDesc.changed `changed`} callback that initiates a route change
17 * to the selected docSet
18 */</comment>
19export class DocsMenu extends Layout {
20 docSet = '';
21
22 private getDesc(attrs:any):SelectorDesc {
23 if (this.docSet !== attrs.docSet) {
24 this.docSet = attrs.docSet;
25 DocSets.loadList(attrs.docSet);
26 }
27 const items = DocSets.get();
28 return {
29 items: items.map((c:string) => c),
30 defaultItem: (attrs.route && attrs.route.lib)? attrs.route.lib : items[0],
31 changed: (item:string) => m.route.set('/api/:lib/0', {lib:item})
32 };
33 }
34
35 getComponents(node:Vnode):Vnode {
36 const desc:SelectorDesc = this.getDesc(node.attrs);
37 return m(Menu, {desc: desc});
38 }
39}
`