Debian Codegraph @

resources/templates/kobo.js

raw
1 var gPosition = 0;
2 var gProgress = 0;
3 var gCurrentPage = 0;
4 var gPageCount = 0;
5 var gClientHeight = null;
6
7 function getPosition()
8 {
9 return gPosition;
10 }
11
12 function getProgress()
13 {
14 return gProgress;
15 }
16
17 function getPageCount()
18 {
19 return gPageCount;
20 }
21
22 function getCurrentPage()
23 {
24 return gCurrentPage;
25 }
26
27 function turnOnNightMode(nightModeOn) {
28 var body = document.getElementsByTagName('body')[0].style;
29 var aTags = document.getElementsByTagName('a');
30
31 var textColor;
32 var bgColor;
33
34 if (nightModeOn > 0) {
35 textColor = "#FFFFFF !important";
36 bgColor = "#000000 !important";
37 } else {
38 textColor = "#000000 !important";
39 bgColor = "#FFFFFF !important";
40 }
41
42 for (i = 0; i < aTags.length; i++) {
43 aTags[i].style.color = textColor;
44 }
45
46 body.color = textColor;
47 body.backgroundColor = bgColor;
48
49 window.device.turnOnNightModeDone();
50 }
51
52 function setupBookColumns()
53 {
54 var body = document.getElementsByTagName('body')[0].style;
55 body.marginLeft = '0px !important';
56 body.marginRight = '0px !important';
57 body.marginTop = '0px !important';
58 body.marginBottom = '0px !important';
59 body.paddingTop = '0px !important';
60 body.paddingBottom = '0px !important';
61 body.webkitNbspMode = 'space';
62
63 var bc = document.getElementById('book-columns').style;
64 bc.width = (window.innerWidth * 2) + 'px !important';
65 bc.height = window.innerHeight + 'px !important';
66 bc.marginTop = '0px !important';
67 bc.webkitColumnWidth = window.innerWidth + 'px !important';
68 bc.webkitColumnGap = '0px !important';
69 bc.overflow = 'none';
70 bc.paddingTop = '0px !important';
71 bc.paddingBottom = '0px !important';
72 gCurrentPage = 1;
73 gProgress = gPosition = 0;
74
75 var bi = document.getElementById('book-inner').style;
76 bi.marginLeft = '10px';
77 bi.marginRight = '10px';
78 bi.padding = '0';
79
80 window.device.print ("bc.height = "+ bc.height);
81 window.device.print ("window.innerHeight ="+ window.innerHeight);
82
83 gPageCount = document.body.scrollWidth / window.innerWidth;
84
85 if (gClientHeight < window.innerHeight) {
86 gPageCount = 1;
87 }
88 }
89
90 function paginate(tagId)
91 {
92 // Get the height of the page. We do this only once. In setupBookColumns we compare this
93 // value to the height of the window and then decide whether to force the page count to one.
94 if (gClientHeight == undefined) {
95 gClientHeight = document.getElementById('book-columns').clientHeight;
96 }
97
98 setupBookColumns();
99 //window.scrollTo(0, window.innerHeight);
100
101 window.device.reportPageCount(gPageCount);
102 var tagIdPageNumber = 0;
103 if (tagId.length > 0) {
104 tagIdPageNumber = estimatePageNumberForAnchor (tagId);
105 }
106 window.device.finishedPagination(tagId, tagIdPageNumber);
107 }
108
109 function repaginate(tagId) {
110 window.device.print ("repaginating, gPageCount:" + gPageCount);
111 paginate(tagId);
112 }
113
114 function paginateAndMaintainProgress()
115 {
116 var savedProgress = gProgress;
117 setupBookColumns();
118 goProgress(savedProgress);
119 }
120
121 function updateBookmark()
122 {
123 gProgress = (gCurrentPage - 1.0) / gPageCount;
124 var anchorName = estimateFirstAnchorForPageNumber(gCurrentPage - 1);
125 window.device.finishedUpdateBookmark(anchorName);
126 }
127
128 function goBack()
129 {
130 if (gCurrentPage > 1)
131 {
132 --gCurrentPage;
133 gPosition -= window.innerWidth;
134 window.scrollTo(gPosition, 0);
135 window.device.pageChanged();
136 } else {
137 window.device.previousChapter();
138 }
139 }
140
141 function goForward()
142 {
143 if (gCurrentPage < gPageCount)
144 {
145 ++gCurrentPage;
146 gPosition += window.innerWidth;
147 window.scrollTo(gPosition, 0);
148 window.device.pageChanged();
149 } else {
150 window.device.nextChapter();
151 }
152 }
153
154 function goPage(pageNumber, callPageReadyWhenDone)
155 {
156 if (pageNumber > 0 && pageNumber <= gPageCount)
157 {
158 gCurrentPage = pageNumber;
159 gPosition = (gCurrentPage - 1) * window.innerWidth;
160 window.scrollTo(gPosition, 0);
161 if (callPageReadyWhenDone > 0) {
162 window.device.pageReady();
163 } else {
164 window.device.pageChanged();
165 }
166 }
167 }
168
169 function goProgress(progress)
170 {
171 progress += 0.0001;
172
173 var progressPerPage = 1.0 / gPageCount;
174 var newPage = 0;
175
176 for (var page = 0; page < gPageCount; page++) {
177 var low = page * progressPerPage;
178 var high = low + progressPerPage;
179 if (progress >= low && progress < high) {
180 newPage = page;
181 break;
182 }
183 }
184
185 gCurrentPage = newPage + 1;
186 gPosition = (gCurrentPage - 1) * window.innerWidth;
187 window.scrollTo(gPosition, 0);
188 updateProgress();
189 }
190
191 /* BOOKMARKING CODE */
192
193 /**
194 * Estimate the first anchor for the specified page number. This is used on the broken WebKit
195 * where we do not know for sure if the specific anchor actually is on the page.
196 */
197
198
199 function estimateFirstAnchorForPageNumber(page)
200 {
201 var spans = document.getElementsByTagName('span');
202 var lastKoboSpanId = "";
203 for (var i = 0; i < spans.length; i++) {
204 if (spans[i].id.substr(0, 5) == "kobo.") {
205 lastKoboSpanId = spans[i].id;
206 if (spans[i].offsetTop >= (page * window.innerHeight)) {
207 return spans[i].id;
208 }
209 }
210 }
211 return lastKoboSpanId;
212 }
213
214 /**
215 * Estimate the page number for the specified anchor. This is used on the broken WebKit where we
216 * do not know for sure how things are columnized. The page number returned is zero based.
217 */
218
219 function estimatePageNumberForAnchor(spanId)
220 {
221 var span = document.getElementById(spanId);
222 if (span) {
223 return Math.floor(span.offsetTop / window.innerHeight);
224 }
225 return 0;
226 }