OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** Provides client-side behavior for generated docs. */ | |
6 #library('client-live-nav'); | |
7 | |
8 #import('dart:html'); | |
9 #import('dart:json'); | |
10 #import('../../frog/lang.dart', prefix: 'frog'); | |
11 #import('classify.dart'); | |
12 #import('markdown.dart', prefix: 'md'); | |
13 | |
14 #source('client-shared.dart'); | |
15 | |
16 // The names of the library and type that this page documents. | |
17 String currentLibrary = null; | |
18 String currentType = null; | |
19 | |
20 // What we need to prefix relative URLs with to get them to work. | |
21 String prefix = ''; | |
22 | |
23 main() { | |
24 window.on.contentLoaded.add((e) { | |
25 // Figure out where we are. | |
26 final body = document.query('body'); | |
27 currentLibrary = body.dataAttributes['library']; | |
28 currentType = body.dataAttributes['type']; | |
29 prefix = (currentType != null) ? '../' : ''; | |
30 | |
31 enableCodeBlocks(); | |
32 | |
33 // Request the navigation data so we can build the HTML for it. | |
34 new XMLHttpRequest.getTEMPNAME('${prefix}nav.json', (request) { | |
35 buildNavigation(JSON.parse(request.responseText)); | |
36 }); | |
37 }); | |
38 } | |
39 | |
40 /** Turns [name] into something that's safe to use as a file name. */ | |
41 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); | |
42 | |
43 /** | |
44 * Takes [libraries], a JSON object representing a set of libraries and builds | |
45 * the appropriate navigation DOM for it relative to the current library and | |
46 * type. | |
47 */ | |
48 buildNavigation(libraries) { | |
49 final libraryNames = libraries.getKeys(); | |
50 libraryNames.sort((a, b) => a.compareTo(b)); | |
51 | |
52 final html = new StringBuffer(); | |
53 for (final libraryName in libraryNames) { | |
54 html.add('<h2><div class="icon-library"></div>'); | |
55 if (currentLibrary == libraryName && currentType == null) { | |
56 html.add('<strong>${md.escapeHtml(libraryName)}</strong>'); | |
57 } else { | |
58 final url = '$prefix${sanitize(libraryName)}.html'; | |
59 html.add('<a href="$url">${md.escapeHtml(libraryName)}</a>'); | |
60 } | |
61 html.add('</h2>'); | |
62 | |
63 // Only list the types for the current library. | |
64 if (currentLibrary == libraryName) { | |
65 buildLibraryNavigation(html, libraries[libraryName]); | |
66 } | |
67 } | |
68 | |
69 // Insert it into the DOM. | |
70 final navElement = document.query('.nav'); | |
71 navElement.innerHTML = html.toString(); | |
72 } | |
73 | |
74 /** Writes the navigation for the types contained by [library] to [html]. */ | |
75 buildLibraryNavigation(StringBuffer html, library) { | |
76 // Show the exception types separately. | |
77 final types = []; | |
78 final exceptions = []; | |
79 | |
80 for (final type in library) { | |
81 if (type['name'].endsWith('Exception')) { | |
82 exceptions.add(type); | |
83 } else { | |
84 types.add(type); | |
85 } | |
86 } | |
87 | |
88 if (types.length == 0 && exceptions.length == 0) return; | |
89 | |
90 writeType(String icon, type) { | |
91 html.add('<li>'); | |
92 if (currentType == type['name']) { | |
93 html.add( | |
94 '<div class="icon-$icon"></div><strong>${type["name"]}</strong>'); | |
95 } else { | |
96 html.add( | |
97 ''' | |
98 <a href="$prefix${type["url"]}"> | |
99 <div class="icon-$icon"></div>${type["name"]} | |
100 </a> | |
101 '''); | |
102 } | |
103 html.add('</li>'); | |
104 } | |
105 | |
106 html.add('<ul class="icon">'); | |
107 types.forEach((type) => writeType(type['kind'], type)); | |
108 exceptions.forEach((type) => writeType('exception', type)); | |
109 html.add('</ul>'); | |
110 } | |
OLD | NEW |