forked from Pomax/nrGrammar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preloader.js
executable file
·174 lines (140 loc) · 4.79 KB
/
preloader.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Preload all the book data in plain text form,
* and then let the browser perform the rendering.
*/
schedule(function loadData() {
// menu nonsense
var headings = [];
function extendMenu(section) {
var menu = find("#menu"), sel;
var headerElement = find("header");
var curhs = section.find("h1, h2, h3");
if(curhs instanceof Array) {} else { curhs = [curhs]; }
var hideh3 = function(sel) {
if(sel && sel.children.length > 50) {
sel.classList.add("hideh3");
}
}
curhs.forEach(function(h) {
if(h.localName === "h1") {
hideh3(sel);
sel = create("ul");
menu.add(sel);
}
sel.add(create("li", { class: h.localName}, "<a href='#" + h.id +"'>" + h.textContent + "</a>"));
});
hideh3(sel);
curhs = curhs.map(function(e) {
return {
top: e.getBoundingClientRect().top - headerElement.getBoundingClientRect().top,
id: e.id
};
});
headings = headings.concat(curhs);
}
function setupMenuScrollBehaviour() {
var prev;
var closest = function() {
var top = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
var h, i = headings.length;
while (i--) {
h = headings[i];
if (top >= h.top - 1) return h;
}
};
document.onscroll = function() {
var h = closest();
if (!h) return;
if (prev) {
prev.classList.remove('active');
prev.parent("ul").classList.remove("active");
}
var a = find('nav a[href="#' + h.id + '"]');
a.classList.add('active');
a.parent("ul").classList.add("active");
prev = a;
};
var data = find("script[type='text/html']").innerHTML;
menu.add(data);
window.scrollBy(0,1);
window.scrollBy(0,-1);
}
var nav = find("nav");
nav.listen("touchstart", function(evt) { nav.classes().add("active"); });
find("#content").listen("touchstart", function(evt) { nav.classes().remove("active"); });
// =============================
var base = window.GrammarLoaderConfig ? GrammarLoaderConfig.base : '',
dir = base + "./data/pages/en-GB/",
pages = [
"preface/onlinedraft",
"syntax",
"verb_grammar",
"more_verb_grammar",
"particles",
"counters",
"language_patterns"
],
appendices = [
"conjugation",
"set_phrases",
"glossary"
],
main = find("#content"),
ol_chapters = find("#chapters"),
ol_appendices = find("#appendices"),
ol_indexes = find("#indexes"),
getData = function(dir, filename, callback) {
return get(dir + filename + ".txt", callback);
};
/**
* Run our data injection
*/
(function(main, dir, pages, appendices) {
var data = { keys:[], pages: {}, html: {}},
destinations = [ol_chapters].repeat(pages.length).concat([ol_appendices].repeat(appendices.length)),
tocarray = [true].repeat(pages.length).concat([false].repeat(appendices.length)),
files = pages.concat(appendices),
markIndicator = 0;
/**
* load each file individually
*/
(function loadFile(files, dir, destinations, fullToC) {
if(files.length==0) {
setupMenuScrollBehaviour();
document.head.add(find("link[href='counters.css']").remove());
if (window.GrammarLoaderConfig && GrammarLoaderConfig.onGrammarLoaded) {
GrammarLoaderConfig.onGrammarLoaded();
}
return;
}
var filename = files.splice(0,1)[0],
destination = destinations.splice(0,1)[0],
buildtoc = fullToC.splice(0,1)[0];
var loadData = function(filename, data, next) {
var dataDiv = create("div", data);
var chapter = create("section", { class: filename });
main.add(chapter);
// prevent page-blocking by not loading in the entire section in a single go!
(function process(list) {
if(list.children.length > 0) {
for(var i=0; i<100 && list.length>0; i++) {
chapter.add(list.get(0));
}
return setTimeout(function() { process(list); }, 0);
}
setTimeout(function() { next(chapter); }, 0);
}(dataDiv));
};
getData(dir, filename, function (xhr) {
var useprefix = ([pages[0]].concat(appendices).indexOf(filename) === -1);
var prefix = markIndicator++;
var fileData = xhr.responseText.split("\n").slice(4).join("\n");
var conversion = BookToHTML.convert(fileData, useprefix, prefix, base);
loadData(filename, conversion.html, function(chapter) {
extendMenu(chapter);
loadFile(files, dir, destinations, fullToC);
});
});
}(files, dir, destinations, tocarray));
}(main, dir, pages, appendices));
});