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