Initial check-in
[dygraphs.git] / mochikit_v14 / doc / js / toc.js
1 function function_ref(fn) {
2 return A({"href": fn[1], "class": "mochiref reference"}, fn[0], BR());
3 };
4
5 function toggle_docs() {
6 toggleElementClass("invisible", "show_index", "function_index");
7 return false;
8 };
9
10 function create_toc() {
11 if (getElement("distribution")) {
12 return global_index();
13 }
14 if (getElement("api-reference")) {
15 return module_index();
16 }
17 };
18
19 function doXHTMLRequest(url) {
20 var d = doXHR(url, {mimeType: 'text/xml'});
21 return d.addCallback(function (res) {
22 if (res.responseXML.documentElement) {
23 return res.responseXML.documentElement;
24 } else {
25 var container = document.createElement('div');
26 container.innerHTML = res.responseText;
27 return container;
28 }
29 });
30 };
31
32 function load_request(href, div, doc) {
33 var functions = withDocument(doc, spider_doc);
34 forEach(functions, function (func) {
35 // fix anchors
36 if (func[1].charAt(0) == "#") {
37 func[1] = href + func[1];
38 } else if (func[1].lastIndexOf("#") != -1) {
39 func[1] = href + "#" + func[1].split("#")[1];
40 }
41 });
42 var showLink = A({"class": "force-pointer"}, "[+]");
43 var hideLink = A({"class": "force-pointer"}, "[\u2013]");
44 var functionIndex = DIV({"id": "function_index", "class": "invisible"},
45 hideLink,
46 P(null, map(function_ref, functions))
47 );
48 var toggleFunc = function (e) {
49 toggleElementClass("invisible", showLink, functionIndex);
50 };
51 connect(showLink, "onclick", toggleFunc);
52 connect(hideLink, "onclick", toggleFunc);
53 replaceChildNodes(div,
54 showLink,
55 functionIndex
56 );
57 return [showLink, toggleFunc];
58 };
59
60 function global_index() {
61 var distList = getElementsByTagAndClassName("ul")[0];
62 var bullets = getElementsByTagAndClassName("li", null, distList);
63 var lst = [];
64 for (var i = 0; i < bullets.length; i++) {
65 var tag = bullets[i];
66 var firstLink = getElementsByTagAndClassName("a", "mochiref", tag)[0];
67 var href = getNodeAttribute(firstLink, "href");
68 var div = DIV(null, "[\u2026]");
69 appendChildNodes(tag, BR(), div);
70 lst.push(doXHTMLRequest(href).addCallback(load_request, href, div));
71 }
72
73 var loadingNode = DIV(null, "[loading index\u2026]");
74 distList.parentNode.insertBefore(P(null, loadingNode), distList);
75
76 var dl = gatherResults(lst).addCallback(function (res) {
77 var toggleFunc = function (e) {
78 for (var i = 0; i < res.length; i++) {
79 var item = res[i];
80 if (!hasElementClass(item[0], "invisible")) {
81 item[1]();
82 }
83 }
84 };
85 var node = A({"class": "force-pointer"}, "[click to expand all]");
86 swapDOM(loadingNode, node);
87 connect(node, "onclick", toggleFunc);
88 });
89 };
90
91 function spider_doc() {
92 return map(
93 function (tag) {
94 return [scrapeText(tag), getNodeAttribute(tag, "href")];
95 },
96 getElementsByTagAndClassName("a", "mochidef")
97 );
98 };
99
100 function module_index() {
101 var sections = getElementsByTagAndClassName("div", "section");
102 var ptr = sections[1];
103 var ref = DIV({"class": "section"},
104 H1(null, "Function Index"),
105 A({"id": "show_index", "href": "#", "onclick": toggle_docs}, "[show]"),
106 DIV({"id": "function_index", "class": "invisible"},
107 A({"href":"#", "onclick": toggle_docs}, "[hide]"),
108 P(null, map(function_ref, spider_doc()))));
109 ptr.parentNode.insertBefore(ref, ptr);
110 };
111
112 connect(window, 'onload', create_toc);
113