Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: utils/dartdoc/client-live-nav.dart

Issue 9256002: Add a mode to dartdoc to generate the navigation on the client. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Respond to review. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « utils/dartdoc/classify.dart ('k') | utils/dartdoc/client-shared.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 final saneLibrary = sanitize(libraryName);
55 html.add('<h2><div class="icon-library"></div>');
56 if (currentLibrary == libraryName && currentType == null) {
57 html.add('<strong>$saneLibrary</strong>');
58 } else {
59 final url = '$prefix$saneLibrary.html';
60 html.add('<a href="$url">$saneLibrary</a>');
61 }
62 html.add('</h2>');
63
64 // Only list the types for the current library.
65 if (currentLibrary == libraryName) {
66 buildLibraryNavigation(html, libraries[libraryName]);
67 }
68 }
69
70 // Insert it into the DOM.
71 final navElement = document.query('.nav');
72 navElement.innerHTML = html.toString();
73 }
74
75 /** Writes the navigation for the types contained by [library] to [html]. */
76 buildLibraryNavigation(StringBuffer html, library) {
77 // Show the exception types separately.
78 final types = [];
79 final exceptions = [];
80
81 for (final type in library) {
82 if (type['name'].endsWith('Exception')) {
83 exceptions.add(type);
84 } else {
85 types.add(type);
86 }
87 }
88
89 if (types.length == 0 && exceptions.length == 0) return;
90
91 writeType(String icon, type) {
92 html.add('<li>');
93 if (currentType == type['name']) {
94 html.add(
95 '<div class="icon-$icon"></div><strong>${type["name"]}</strong>');
96 } else {
97 html.add(
98 '''
99 <a href="$prefix${type["url"]}">
100 <div class="icon-$icon"></div>${type["name"]}
101 </a>
102 ''');
103 }
104 html.add('</li>');
105 }
106
107 html.add('<ul>');
108 types.forEach((type) => writeType(type['kind'], type));
109 exceptions.forEach((type) => writeType('exception', type));
110 html.add('</ul>');
111 }
OLDNEW
« no previous file with comments | « utils/dartdoc/classify.dart ('k') | utils/dartdoc/client-shared.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698