# src/view/Parts.ts

`1import { m, Vnode} from 'hslayout';
2import { tooltip } from './Tooltip';
3import { DocSets } from '../DocSets';
4
5const SourceBase = 'data/src/';
6
7
8// TODO: sort flags in array to garantee sequence of printing
9export function flags(mdl:any, ignore:string[]=[]) {
10 const ignoreExportInKind = ['Method', 'Property'];
11 const knownFlags = {
12 isExported: 'export',
13 isExternal: 'external', //
14 isPublic: 'public',
15 isPrivate: 'private',
16 isProtected: 'protected',
17 isConstructorProperty: 'constructorProperty',
18 isAbstract: 'abstract',
19 isConst: 'const',
20 isStatic: 'static',
21 isOptional: 'optional'
22 };
23 return m('span.hs-item-flags', !mdl.flags? [] :
24 Object.keys(mdl.flags).map((f:string) => {
25 let ign = false;
26 let flag = knownFlags[f];
27 if (flag === undefined) { flag = f; }
28 else { ign = (ignore.indexOf(flag) >= 0); }
29 if (flag === 'export' && ignoreExportInKind.indexOf(mdl.kindString)>=0) { ign = true; }
30 return m(`span.hs-item-${ign?'ignore':(flag===f?'unknown':flag)}-flag`, ign? undefined : flag);
31 })
32 );
33}
34
35export function kindString(mdl:any) {
36 return m('span.hs-item-kind', mdl.kindString);
37}
38
39export function itemName(mdl:any, sub:any) {
40 return m('span.hs-item-name', !mdl.fullPath? sub.name : libLink('a', mdl.lib, mdl.fullPath, sub.name));
41}
42
43
44export function itemTooltip(mdl:any) {
45 return m('span.hs-item-name', tooltip(mdl.name, 'class name and then some', 'bottom'));
46}
47
48export function extensionOf(mdl:any) {
49 return m('span.hs-item-extensions', !mdl.extendedTypes? undefined : [
50 m('span.hs-item-extends', 'extends'),
51 m('span', mdl.extendedTypes.map((t:any, i:number) =>
52 m('span.hs-item-extension', [
53 libLink('a', mdl.lib, DocSets.get(mdl.lib, t.id).fullPath, t.name),
54 mdl.extendedTypes.map.length>(i+1)? ', ': ''
55 ])
56 )),
57 ]);
58}
59
60export function inheritedFrom(mdl:any) {
61 if (mdl.inheritedFrom) {
62 let parent = DocSets.get(mdl.lib, mdl.inheritedFrom.id);
63 parent = DocSets.get(mdl.lib, parent.fullPath.substring(0, parent.fullPath.lastIndexOf('.')));
64 return m('span.hs-item-inherited-from', [
65 m('span', 'inherited from '),
66 libLink('a', parent.lib, parent.fullPath, parent.name)
67 ]);
68 } else {
69 return m('span.hs-item-inherited-from', undefined);
70 }
71}
72
73export function sourceLink(mdl:any) {
74 const source = mdl.sources? mdl.sources[0] : undefined;
75 if (source) {
76 let file = (source.fileName || '').replace('.ts', '.html');
77 const index = file.indexOf(mdl.lib);
78 if (index>0) {
79 file = file.substr(index); // only consider links within the docSet (everything after the lib name)
80 }
81 return m('span.hs-item-member-source',
82 m('a', { href:`${SourceBase}${mdl.lib}/${file}#${Math.max(0,source.line-5)}`, target:"_blank"}, '[source]')
83 );
84 } else {
85 return m('span.hs-item-member-source', '');
86 }
87}
88
89<comment>/**
90 * creates a library link on the specified `name`.
91 * The link points to `/api/<lib>/<id>`
92 * @param css the css tag selector to use
93 * @param cls the css class selector to use
94 * @param lib the lib string to point to
95 * @param id the id number to point to
96 * @param name the name on which to formt he link
97 */</comment>
98export function libLink(css:string, lib:string, id:string, name:string) {
99 return m(css, { href:`/api/${lib}/${id}`, oncreate: m.route.link, onupdate: m.route.link }, name);
100};
101
102<comment>/**
103 * creates a function or method signature
104 */</comment>
105export function signature(s:any, mdl:any): Vnode {
106 const comma = (i:number) => (i>0)? ', ': '';
107 function optional(flags: any) {
108 return (flags && flags.isOptional)? '.hs-item-optional' : '';
109 }
110
111 let sig = [];
112 if (s) {
113 if (s.parameters) {
114 sig = s.parameters.map((p:any, i:number) => m('span', [
115 comma(i),
116 m('span.hs-item-sig-param', [
117 m(`span.hs-item-name${optional(p.flags)}`, p.name),
118 type(p, mdl.lib)
119 ])
120 ]));
121 }
122 switch (mdl.kindString) {
123 case 'Method':
124 case 'Function':
125 case 'Constructor':
126 sig.unshift(m('span.hs-item-name', '('));
127 sig.push(m('span.hs-item-name', ')'));
128 break;
129 default:
130 }
131 }
132 return m('span.hs-item-signature', sig);
133}
134
135<comment>/**
136 * adds a default value, if defined
137 */</comment>
138export function defaultVal(s:any, lib:string): Vnode {
139 if (s && s.defaultValue) {
140 let val = ` = ${s.defaultValue}`.replace(/{/gi, '{ ').replace(/}/gi, ' }');
141 return m('span.hs-item-default', val);
142 } else {
143 return;
144 }
145}
146
147export function type(t:any, lib:string) {
148 function _type(tt:any):any {
149 switch (tt.type) {
150 case undefined: return '';
151 case 'array': return m('span.hs-item-type-array', ['Array<', _type(tt.elementType), '>']);
152
153 case 'tuple': return m('span.hs-item-type-tuple', [
154 '[ ',
155 ...tt.elements.map((e:any, i:number) => [i>0?', ':undefined, _type(e)]),
156 ' ]'
157 ]);
158 case 'intrinsic':
159 case 'instrinct': return m('span.hs-item-type-instrinct', tt.id? libLink('span', lib, tt.fullPath, tt.name) : tt.name);
160 case 'stringLiteral': return m('span.hs-item-type-string-literal', tt.type);
161 case 'union': return m('span.hs-item-type-union', [...tt.types.map((e:any, i:number) => [i>0?' | ':undefined, _type(e)])]);
162 case 'reference': let refRes = tt.name;
163 if (tt.id) {
164 const typeRef = DocSets.get(lib, tt.id);
165 if (typeRef.typeArguments) { refRes = typeRef.name+'<'+ typeRef.typeArguments.map(_type).join(', ') + '>'; }
166 else if (typeRef.id) { refRes = libLink('a', lib, typeRef.fullPath, typeRef.name); }
167 else { refRes = typeRef.name; }
168 }
169 return m('span.hs-item-type-reference', refRes);
170 case 'reflection': let rflRes;
171 if (tt.declaration) {
172 rflRes = !tt.declaration.children? tt.declaration.kindString :
173 m('span.hs-item-reflection', [
174 '{ ',
175 ...tt.declaration.children.map((c:any, i:number) =>
176 [i>0?', ':undefined, c.name, ': ', _type(c.type)]
177 ),
178 ' }'
179 ]);
180 } else {
181 rflRes = 'UNKNOWN';
182 }
183 return m('span.hs-item-type-reflection', rflRes);
184 default: console.log('unknown type '+ tt.type);
185 console.log(t);
186 return t.type;
187 }
188 }
189
190 try {
191 return m('span', !t.type? '': [
192 m('span.hs-item-name',':'),
193 m('span.hs-item-sig-type', _type(t.type)),
194 defaultVal(t, lib)
195 ]);
196 } catch(e) { console.log(e); console.log(e.trace); }
197}
198
199export function makeID(section:string, mdl:any) {
200 let result = section? section+'_' : '';
201 result = (result + (mdl.name || '')).toLowerCase();
202 return (result!=='')? result : undefined;
203}
`