# src/view/MainDetail.ts

`1import { m, Vnode} from 'hslayout';
2import { Layout } from 'hslayout';
3import { DocSets } from '../DocSets';
4import { comment, commentLong } from './MainComment';
5import { flags, sourceLink,
6 signature, type,
7 extensionOf, inheritedFrom,
8 kindString, itemName,
9 makeID } from './Parts';
10
11
12<comment>/**
13 * Creates Documentation on the main panel
14 */</comment>
15export class MainDetail extends Layout {
16 getComponents(node:Vnode): Vnode {
17 let lib, field;
18 if (node.attrs.route) {
19 lib = node.attrs.route.lib;
20 field = node.attrs.route.field;
21 }
22 node.attrs.route = undefined;
23
24 let result = getOverview(lib, field) || itemDoc(DocSets.get(lib, field) || '');
25 return m('.hs-main-detail', [result]);
26 }
27}
28
29<comment>/**
30 * Checks if the project overview is being requested and returns the overview,
31 * or `undefined` if not available
32 * @param mdl the module name to check
33 * @return Vnode containing the overview file, or `undefined`
34 */</comment>
35function getOverview(lib:string, mdl:string):Vnode {
36 if (mdl === '0' || mdl === '') { //show module overview
37 mdl = DocSets.get(lib, `${lib}.overview`);
38 if (mdl) { // if project has an overview:
39 return overviewDoc(mdl);
40 }
41 }
42 return undefined;
43}
44
45<comment>/**
46 * Creates documentation for standard items in the main panel
47 * @param mdl the module to document on the main panel
48 */</comment>
49function itemDoc(mdl:any) {
50 const sig = mdl.signatures? mdl.signatures[0] : mdl;
51 return m('.hs-item-doc', [
52 title(mdl, sig),
53 commentLong(sig),
54 members(sig, sig)
55 ]);
56}
57
58<comment>/**
59 * Creates documentation for the project overview in the main panel
60 * @param mdl the module to document on the main panel
61 */</comment>
62function overviewDoc(mdl:any) {
63 const sig = mdl.signatures? mdl.signatures[0] : mdl;
64 return m('.hs-item-doc', [
65 commentLong(sig),
66 ]);
67}
68
69<comment>/**
70 * renders the title of the main panel
71 * @param mdl the module to document
72 * @param sig a signature of the module, or the the module itself
73 */</comment>
74function title(mdl:any, sig:any): Vnode {
75 return m('.hs-item-title', {id: makeID('title', mdl)}, itemDescriptor(mdl, sig));
76}
77
78function members(mdl:any, sig:any): Vnode {
79 if (mdl.groups) {
80 return m('.hs-item-members', [
81 ...mdl.groups.map((g:any) => member(g, mdl.lib, true, true)),
82 ...mdl.groups.map((g:any) => member(g, mdl.lib, true, false)),
83 ...mdl.groups.map((g:any) => member(g, mdl.lib, false, true)),
84 ...mdl.groups.map((g:any) => member(g, mdl.lib, false, false))
85 ]);
86 } else if (mdl.parameters) {
87 return m('.hs-item-members', parameter(mdl.parameters, mdl.lib));
88 } else {
89 return m('.hs-item-members');
90 }
91}
92
93function parameter(g:any[], lib:string): Vnode {
94 let content = g.map((c:any) => m('.hs-item-parameter', {id:makeID('parameter', c)}, itemChild(c)));
95 content.unshift(m('.hs-item-member-title', {id:'parameters'}, m('span', 'Parameters')));
96 return m('.hs-item-member', content);
97}
98
99function member(group:any, lib:string, statc:boolean, publc: boolean): Vnode {
100 const resolve = ((c:number) => DocSets.get(lib, c));
101 const directChildren = ((mdl:any) => !mdl['inheritedFrom']);
102 const inheritedChildren = ((mdl:any) => mdl['inheritedFrom']);
103 const groupMap = {
104 'External modules': '.hs-item-external-module',
105 'Constructors': '.hs-item-constructor',
106 'Classes': '.hs-item-class',
107 'Interfaces': '.hs-item-interface',
108 'Functions': '.hs-item-function',
109 'Methods': '.hs-item-method',
110 'Variables': '.hs-item-variable',
111 'Object literals': '.hs-item-object-literal',
112 'Properties': '.hs-item-property',
113 'Type aliases': '.hs-item-alias',
114 'Accessors': '.hs-item-accessors'
115 };
116 const fn = groupMap[group.title] || '.hs-item-unknown-member';
117 const isPublic = (flags:any) => flags.isPublic || (flags.isExported && !flags.isPrivate);
118
119 const content = group.children
120 .map(resolve)
121 .filter(directChildren)
122 .filter((mdl:any) => statc? mdl.flags.isStatic : !mdl.flags.isStatic)
123 .filter((mdl:any) => publc? isPublic(mdl.flags) : !isPublic(mdl.flags))
124 .map((mdl:any) => m(fn, {id:makeID(group.title, mdl)}, itemChild(mdl)));
125 const inherited = group.children
126 .map(resolve)
127 .filter(inheritedChildren)
128 .filter((mdl:any) => statc? mdl.flags.isStatic : !mdl.flags.isStatic)
129 .filter((mdl:any) => publc? mdl.flags.isPublic : !mdl.flags.isPublic)
130 .map((mdl:any) => m(`.hs-item-inherited ${fn}`, {id:makeID(group.title, mdl)}, itemChild(mdl)));
131
132 const publStr = publc?'Public':'Protected or Private';
133 const statStr = statc?'Static':'';
134 if (inherited.length>0) {
135 inherited.unshift(m('.hs-item-inherited .hs-item-member-title', m('span', `${publStr} ${statStr} Inherited ${group.title}`)));
136 }
137 if (content.length>0) {
138 content.unshift(m('.hs-item-member-title', {id:group.title.toLowerCase()}, m('span', `${publStr} ${statStr} ${group.title}`)));
139 }
140 return m(`.hs-item-member ${statc?'.hs-item-static':''} ${publc?'.hs-item-public':''}`, content.concat(inherited));
141}
142
143function itemDescriptor(mdl:any, sig:any):Vnode {
144 try { return m('.hs-item-desc', [
145 flags(mdl),
146 kindString(mdl),
147 itemName(mdl, mdl),
148 signature(sig, mdl),
149 type(sig, mdl.lib),
150 extensionOf(mdl),
151 inheritedFrom(mdl),
152 sourceLink(mdl)
153 ]);
154 }
155 catch(e) { console.log(e); console.log(mdl); }
156}
157
158function itemChild(mdl:any, sig=mdl): Vnode[] {
159 return mdl.signatures?
160 mdl.signatures.map((s:any) => m('.hs-item-child-signature',[itemDescriptor(mdl, s), comment(s, true)])) :
161 [itemDescriptor(mdl, sig), comment(sig,true)];
162}
163
164
165
`