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