Chromium Code Reviews| 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.attributes['data-library']; | |
| 28 currentType = body.attributes['data-type']; | |
|
nweiz
2012/01/19 19:20:17
body.dataAttributes['type'] is slightly more idiom
Bob Nystrom
2012/01/19 20:11:07
Done.
| |
| 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 // TODO(bob): Copied from utils.dart. :( | |
| 42 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); | |
| 43 | |
| 44 /** | |
| 45 * Takes [libraries], a JSON object representing a set of libraries and builds | |
| 46 * a string of HTML representation the navigation for those libraries, relative | |
|
nweiz
2012/01/19 19:20:17
s/representation/representing/
nweiz
2012/01/19 19:20:17
"builds a string of HTML" seems to imply that this
Bob Nystrom
2012/01/19 20:11:07
Done. This comment was all kinds of out of date.
| |
| 47 * to [currentLibrary] and [currentType] which describe the current page. | |
| 48 */ | |
| 49 buildNavigation(libraries) { | |
| 50 final libraryNames = libraries.getKeys(); | |
| 51 libraryNames.sort((a, b) => a.compareTo(b)); | |
|
nweiz
2012/01/19 19:20:17
Off topic: this should really be the default argum
Bob Nystrom
2012/01/19 20:11:07
Yeah. Filed a bug: dartbug.com/1235
| |
| 52 | |
| 53 final html = new StringBuffer(); | |
| 54 for (final libraryName in libraryNames) { | |
| 55 html.add('<h2><div class="icon-library"></div>'); | |
| 56 if ((currentLibrary == libraryName) && (currentType == null)) { | |
|
nweiz
2012/01/19 19:20:17
Style nit: redundant parens.
Bob Nystrom
2012/01/19 20:11:07
I usually don't rely on operator precedence, but f
| |
| 57 html.add('<strong>$libraryName</strong>'); | |
|
nweiz
2012/01/19 19:20:17
How sure are we that libraryName will never contai
Bob Nystrom
2012/01/19 20:11:07
Pretty sure, but sanitized just in case.
| |
| 58 } else { | |
| 59 final url = '$prefix${sanitize(libraryName)}.html'; | |
| 60 html.add('<a href="$url">$libraryName</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')) { | |
|
nweiz
2012/01/19 19:20:17
This seems a little hacky. Could you include an is
Bob Nystrom
2012/01/19 20:11:07
It is a bit hacky, but I'm OK with that at least f
| |
| 83 exceptions.add(type); | |
| 84 } else { | |
| 85 types.add(type); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 if ((types.length == 0) && (exceptions.length == 0)) return; | |
|
nweiz
2012/01/19 19:20:17
Style nit: more redundant parens.
Bob Nystrom
2012/01/19 20:11:07
Done.
| |
| 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 } | |
| OLD | NEW |