dcda8f5951a8ef9b256ec490d3845d46247dc24c
fenris advanced

fenris authored 8 years ago

1) "use strict";
2) 
3) function load_file(url, usage)
4) {
5) 	let request = new XMLHttpRequest();
6) 	request.open("GET", url, true);
7) 	request.send(null);
8) 	request.onreadystatechange = function ()
9) 	{
10) 		usage(null, request.responseText);
11) 	};
12) }
13) 
14) function add_tooltips()
15) {
16) 	for (let dom_node of document.querySelectorAll(".word_fs"))
17) 	{
18) 		let url = "http://folksprak.org/munin.php?query=translation_fs_" + dom_node.textContent + "_en";
19) 		load_file
20) 		(
21) 			url,
22) 			function (error, content)
23) 			{
24) 				try
25) 				{
26) 					let object = JSON.parse(content);
27) 					dom_node.setAttribute("title", object.map(entry => entry.to.stem).join(", "));
28) 				}
29) 				catch (exception)
30) 				{
31) 					console.warn("couldn't parse ", content);
32) 				}
33) 			}
34) 		)
35) 	}
36) }
37) 
fenris foo

fenris authored 8 years ago

38) function convert_to_list(structure)
39) {
40) 	let list = [];
41) 	for (let index = 0; index < structure.length; index += 1) list.push(structure[index]);
42) 	return list;
43) }
44) 
45) class class_contentnode
46) {
47) 	constructor(id, title, children)
48) 	{
49) 		this.id = id;
50) 		this.title = title;
51) 		this.children = children;
52) 	}
53) 	
54) 	generate(path = [])
55) 	{
56) 		let that = this;
57) 		let dom_fragment = document.createDocumentFragment();
58) 		{
59) 			if (this.title != null)
60) 			{
61) 				let dom_link = document.createElement("a");
62) 				dom_link.setAttribute("href", "#" + that.id);
63) 				dom_link.textContent = path.map(x => (x+1).toString()).join(".") + " " + this.title;
64) 				dom_fragment.appendChild(dom_link);
65) 			}
66) 		}
67) 		{
bfadmin-master advanced

bfadmin-master authored 8 years ago

68) 			let dom_list = document.createElement("ol");
fenris foo

fenris authored 8 years ago

69) 			this.children.forEach
70) 			(
71) 				function (child, index)
72) 				{
73) 					if (child != null)
74) 					{
75) 						let dom_element = document.createElement("li");
76) 						dom_element.appendChild(child.generate(path.concat(index)));
77) 						dom_list.appendChild(dom_element);
78) 					}
79) 				}
80) 			);
81) 			dom_fragment.appendChild(dom_list);
82) 		}
83) 		return dom_fragment;
84) 	}
85) 	
86) 	static read(dom_context = document, classes = ["chapter","section","subsection"], id = null, title = null)
87) 	{
88) 		return (
89) 			new class_contentnode
90) 			(
91) 				id,
92) 				title,
93) 				(classes.length == 0) ? [] : convert_to_list(dom_context.querySelectorAll("section" + ("." + classes[0]) + ":not(.pseudo)")).map
94) 				(
95) 					function (dom_context_)
96) 					{
97) 						return (
98) 							class_contentnode.read
99) 							(
100) 								dom_context_,
101) 								classes.slice(1),
102) 								dom_context_.getAttribute("id"),
103) 								dom_context_.querySelector("header").textContent
104) 							)
105) 						);
106) 					}
107) 				)
108) 			)
109) 		);
110) 	}
111) }
112) 
fenris advanced

fenris authored 8 years ago

113) function main()
114) {
fenris foo

fenris authored 8 years ago

115) 	document.querySelector("#toc").appendChild(class_contentnode.read().generate());