UNPKG

13.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Path = require("path");
4const FS = require("fs");
5const theme_1 = require("../theme");
6const index_1 = require("../../models/reflections/index");
7const UrlMapping_1 = require("../models/UrlMapping");
8const NavigationItem_1 = require("../models/NavigationItem");
9const events_1 = require("../events");
10class DefaultTheme extends theme_1.Theme {
11 constructor(renderer, basePath) {
12 super(renderer, basePath);
13 this.listenTo(renderer, events_1.RendererEvent.BEGIN, this.onRendererBegin, 1024);
14 }
15 isOutputDirectory(path) {
16 if (!FS.existsSync(Path.join(path, 'index.html'))) {
17 return false;
18 }
19 if (!FS.existsSync(Path.join(path, 'assets'))) {
20 return false;
21 }
22 if (!FS.existsSync(Path.join(path, 'assets', 'js', 'main.js'))) {
23 return false;
24 }
25 if (!FS.existsSync(Path.join(path, 'assets', 'images', 'icons.png'))) {
26 return false;
27 }
28 return true;
29 }
30 getUrls(project) {
31 const urls = [];
32 const entryPoint = this.getEntryPoint(project);
33 if (this.application.options.getValue('readme') === 'none') {
34 entryPoint.url = 'index.html';
35 urls.push(new UrlMapping_1.UrlMapping('index.html', entryPoint, 'reflection.hbs'));
36 }
37 else {
38 entryPoint.url = 'globals.html';
39 urls.push(new UrlMapping_1.UrlMapping('globals.html', entryPoint, 'reflection.hbs'));
40 urls.push(new UrlMapping_1.UrlMapping('index.html', project, 'index.hbs'));
41 }
42 if (entryPoint.children) {
43 entryPoint.children.forEach((child) => {
44 if (child instanceof index_1.DeclarationReflection) {
45 DefaultTheme.buildUrls(child, urls);
46 }
47 });
48 }
49 return urls;
50 }
51 getEntryPoint(project) {
52 const entryPoint = this.owner.entryPoint;
53 if (entryPoint) {
54 const reflection = project.getChildByName(entryPoint);
55 if (reflection) {
56 if (reflection instanceof index_1.ContainerReflection) {
57 return reflection;
58 }
59 else {
60 this.application.logger.warn('The given entry point `%s` is not a container.', entryPoint);
61 }
62 }
63 else {
64 this.application.logger.warn('The entry point `%s` could not be found.', entryPoint);
65 }
66 }
67 return project;
68 }
69 getNavigation(project) {
70 function containsExternals(modules) {
71 for (let index = 0, length = modules.length; index < length; index++) {
72 if (modules[index].flags.isExternal) {
73 return true;
74 }
75 }
76 return false;
77 }
78 function sortReflections(modules) {
79 modules.sort((a, b) => {
80 if (a.flags.isExternal && !b.flags.isExternal) {
81 return 1;
82 }
83 if (!a.flags.isExternal && b.flags.isExternal) {
84 return -1;
85 }
86 return a.getFullName() < b.getFullName() ? -1 : 1;
87 });
88 }
89 function includeDedicatedUrls(reflection, item) {
90 (function walk(reflection) {
91 for (const child of reflection.children || []) {
92 if (child.hasOwnDocument && !child.kindOf(index_1.ReflectionKind.SomeModule)) {
93 if (!item.dedicatedUrls) {
94 item.dedicatedUrls = [];
95 }
96 item.dedicatedUrls.push(child.url);
97 walk(child);
98 }
99 }
100 })(reflection);
101 }
102 function buildChildren(reflection, parent) {
103 const modules = reflection.getChildrenByKind(index_1.ReflectionKind.SomeModule);
104 modules.sort((a, b) => {
105 return a.getFullName() < b.getFullName() ? -1 : 1;
106 });
107 modules.forEach((reflection) => {
108 const item = NavigationItem_1.NavigationItem.create(reflection, parent);
109 includeDedicatedUrls(reflection, item);
110 buildChildren(reflection, item);
111 });
112 }
113 function buildGroups(reflections, parent, callback) {
114 let state = -1;
115 const hasExternals = containsExternals(reflections);
116 sortReflections(reflections);
117 reflections.forEach((reflection) => {
118 if (hasExternals && !reflection.flags.isExternal && state !== 1) {
119 new NavigationItem_1.NavigationItem('Internals', undefined, parent, 'tsd-is-external');
120 state = 1;
121 }
122 else if (hasExternals && reflection.flags.isExternal && state !== 2) {
123 new NavigationItem_1.NavigationItem('Externals', undefined, parent, 'tsd-is-external');
124 state = 2;
125 }
126 const item = NavigationItem_1.NavigationItem.create(reflection, parent);
127 includeDedicatedUrls(reflection, item);
128 if (callback) {
129 callback(reflection, item);
130 }
131 });
132 }
133 function build(hasSeparateGlobals) {
134 const root = new NavigationItem_1.NavigationItem('Index', 'index.html');
135 if (entryPoint === project) {
136 const globals = new NavigationItem_1.NavigationItem('Globals', hasSeparateGlobals ? 'globals.html' : 'index.html', root);
137 globals.isGlobals = true;
138 }
139 const modules = [];
140 project.getReflectionsByKind(index_1.ReflectionKind.SomeModule).forEach((someModule) => {
141 let target = someModule.parent;
142 let inScope = (someModule === entryPoint);
143 while (target) {
144 if (target.kindOf(index_1.ReflectionKind.ExternalModule)) {
145 return;
146 }
147 if (entryPoint === target) {
148 inScope = true;
149 }
150 target = target.parent;
151 }
152 if (inScope && someModule instanceof index_1.DeclarationReflection) {
153 modules.push(someModule);
154 }
155 });
156 if (modules.length < 10) {
157 buildGroups(modules, root);
158 }
159 else {
160 buildGroups(entryPoint.getChildrenByKind(index_1.ReflectionKind.SomeModule), root, buildChildren);
161 }
162 return root;
163 }
164 const entryPoint = this.getEntryPoint(project);
165 return build(this.application.options.getValue('readme') !== 'none');
166 }
167 onRendererBegin(event) {
168 if (event.project.groups) {
169 event.project.groups.forEach(DefaultTheme.applyGroupClasses);
170 }
171 for (let id in event.project.reflections) {
172 const reflection = event.project.reflections[id];
173 if (reflection instanceof index_1.DeclarationReflection) {
174 DefaultTheme.applyReflectionClasses(reflection);
175 }
176 if (reflection instanceof index_1.ContainerReflection && reflection.groups) {
177 reflection.groups.forEach(DefaultTheme.applyGroupClasses);
178 }
179 }
180 }
181 static getUrl(reflection, relative, separator = '.') {
182 let url = reflection.getAlias();
183 if (reflection.parent && reflection.parent !== relative &&
184 !(reflection.parent instanceof index_1.ProjectReflection)) {
185 url = DefaultTheme.getUrl(reflection.parent, relative, separator) + separator + url;
186 }
187 return url;
188 }
189 static getMapping(reflection) {
190 return DefaultTheme.MAPPINGS.find(mapping => reflection.kindOf(mapping.kind));
191 }
192 static buildUrls(reflection, urls) {
193 const mapping = DefaultTheme.getMapping(reflection);
194 if (mapping) {
195 if (!reflection.url || !DefaultTheme.URL_PREFIX.test(reflection.url)) {
196 const url = [mapping.directory, DefaultTheme.getUrl(reflection) + '.html'].join('/');
197 urls.push(new UrlMapping_1.UrlMapping(url, reflection, mapping.template));
198 reflection.url = url;
199 reflection.hasOwnDocument = true;
200 }
201 for (const child of reflection.children || []) {
202 if (mapping.isLeaf) {
203 DefaultTheme.applyAnchorUrl(child, reflection);
204 }
205 else {
206 DefaultTheme.buildUrls(child, urls);
207 }
208 }
209 }
210 else if (reflection.parent) {
211 DefaultTheme.applyAnchorUrl(reflection, reflection.parent);
212 }
213 return urls;
214 }
215 static applyAnchorUrl(reflection, container) {
216 if (!reflection.url || !DefaultTheme.URL_PREFIX.test(reflection.url)) {
217 let anchor = DefaultTheme.getUrl(reflection, container, '.');
218 if (reflection['isStatic']) {
219 anchor = 'static-' + anchor;
220 }
221 reflection.url = container.url + '#' + anchor;
222 reflection.anchor = anchor;
223 reflection.hasOwnDocument = false;
224 }
225 reflection.traverse((child) => {
226 if (child instanceof index_1.DeclarationReflection) {
227 DefaultTheme.applyAnchorUrl(child, container);
228 }
229 });
230 }
231 static applyReflectionClasses(reflection) {
232 const classes = [];
233 let kind;
234 if (reflection.kind === index_1.ReflectionKind.Accessor) {
235 if (!reflection.getSignature) {
236 classes.push('tsd-kind-set-signature');
237 }
238 else if (!reflection.setSignature) {
239 classes.push('tsd-kind-get-signature');
240 }
241 else {
242 classes.push('tsd-kind-accessor');
243 }
244 }
245 else {
246 kind = index_1.ReflectionKind[reflection.kind];
247 classes.push(DefaultTheme.toStyleClass('tsd-kind-' + kind));
248 }
249 if (reflection.parent && reflection.parent instanceof index_1.DeclarationReflection) {
250 kind = index_1.ReflectionKind[reflection.parent.kind];
251 classes.push(DefaultTheme.toStyleClass(`tsd-parent-kind-${kind}`));
252 }
253 let hasTypeParameters = !!reflection.typeParameters;
254 reflection.getAllSignatures().forEach((signature) => {
255 hasTypeParameters = hasTypeParameters || !!signature.typeParameters;
256 });
257 if (hasTypeParameters) {
258 classes.push('tsd-has-type-parameter');
259 }
260 if (reflection.overwrites) {
261 classes.push('tsd-is-overwrite');
262 }
263 if (reflection.inheritedFrom) {
264 classes.push('tsd-is-inherited');
265 }
266 if (reflection.flags.isPrivate) {
267 classes.push('tsd-is-private');
268 }
269 if (reflection.flags.isProtected) {
270 classes.push('tsd-is-protected');
271 }
272 if (reflection.flags.isStatic) {
273 classes.push('tsd-is-static');
274 }
275 if (reflection.flags.isExternal) {
276 classes.push('tsd-is-external');
277 }
278 if (!reflection.flags.isExported) {
279 classes.push('tsd-is-not-exported');
280 }
281 reflection.cssClasses = classes.join(' ');
282 }
283 static applyGroupClasses(group) {
284 const classes = [];
285 if (group.allChildrenAreInherited) {
286 classes.push('tsd-is-inherited');
287 }
288 if (group.allChildrenArePrivate) {
289 classes.push('tsd-is-private');
290 }
291 if (group.allChildrenAreProtectedOrPrivate) {
292 classes.push('tsd-is-private-protected');
293 }
294 if (group.allChildrenAreExternal) {
295 classes.push('tsd-is-external');
296 }
297 if (!group.someChildrenAreExported) {
298 classes.push('tsd-is-not-exported');
299 }
300 group.cssClasses = classes.join(' ');
301 }
302 static toStyleClass(str) {
303 return str.replace(/(\w)([A-Z])/g, (m, m1, m2) => m1 + '-' + m2).toLowerCase();
304 }
305}
306DefaultTheme.MAPPINGS = [{
307 kind: [index_1.ReflectionKind.Class],
308 isLeaf: false,
309 directory: 'classes',
310 template: 'reflection.hbs'
311 }, {
312 kind: [index_1.ReflectionKind.Interface],
313 isLeaf: false,
314 directory: 'interfaces',
315 template: 'reflection.hbs'
316 }, {
317 kind: [index_1.ReflectionKind.Enum],
318 isLeaf: false,
319 directory: 'enums',
320 template: 'reflection.hbs'
321 }, {
322 kind: [index_1.ReflectionKind.Module, index_1.ReflectionKind.ExternalModule],
323 isLeaf: false,
324 directory: 'modules',
325 template: 'reflection.hbs'
326 }];
327DefaultTheme.URL_PREFIX = /^(http|ftp)s?:\/\//;
328exports.DefaultTheme = DefaultTheme;
329//# sourceMappingURL=DefaultTheme.js.map
\No newline at end of file