# DocsMenu.ts

`

1import { m, Vnode, Layout } from <quote>'hslayout'</quote>;

2import { DocSets } from <quote>'../DocSets'</quote>;

3import { Menu, SelectorDesc } from <quote>'hswidget'</quote>;

4import { Log } from <quote>'hsutil'</quote>; const log = new Log(<quote>'DocsMenu'</quote>);

5

6<comment>/**</comment>

7<comment> * Creates the title menu for selecting between the different docsets.</comment>

8<comment> * Instantiate the DocsMenu via a standard `mithril` call:```</comment>

9<comment> * m(DocsMenu, { docSet:<pathToIndexFile>})</comment>

10<comment> * ```</comment>

11<comment> * DocsMenu performs the following actions:</comment>

12<comment> * - for the first call of the view lifecycle hook, the available docSets are loaded.</comment>

13<comment> * DocsMenu searches for an index `json` file at the location specified in the </comment>

14<comment> * `docSet` field of the `node.attrs` parameter. If none is specified, the </comment>

15<comment> * default is used as specified in the {@link hsDocs:DocSets.FILE DocSets FILE} setting.</comment>

16<comment> * - DocsMenu retrieves all available docSets via {@link hsDocs:DocSets.DocSets.get DocSets.get}.</comment>

17<comment> * - DocsMenu creates a `SelectorDesc` structure with a {@link hsWidget:hsSelector.SelectorDesc.clicked `clicked`} callback that initiates a route change </comment>

18<comment> * to the selected docSet</comment>

19<comment> */</comment>

20export class DocsMenu extends Layout {

21 docSet = <quote>''</quote>;

22

23 private getDesc(attrs:any):SelectorDesc {

24 if (this.docSet !== attrs.docSet) {

25 this.docSet = attrs.docSet;

26 DocSets.loadList(attrs.docSet)

27 .then(() => {

28 m.redraw();

29 DocSets.getLibs().map((set:string) => {

30 const mdl = DocSets.getNode(0, set);

31 });

32 })

33 .catch(e => {

34 log.error(e.message);

35 log.error(e);

36 });

37 }

38 const items = DocSets.getLibs();

39 return {

40 items: items.map((c:string) => c),

41 <comment>// defaultItem: (attrs.route && attrs.route.lib)? attrs.route.lib : items[0],</comment>

42<comment></comment> defaultItem: m.route.param(<quote>'lib'</quote>) || items[0],

43 clicked: (item:string) => m.route.set(<quote>'/api/:lib/0'</quote>, {lib:item})

44 };

45 }

46

47 getComponents(node:Vnode):Vnode {

48 const desc:SelectorDesc = this.getDesc(node.attrs);

49 return m(Menu, {desc: desc});

50 }

51}

`