2 * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
3 * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 var View = new function() {
8 //self.minOffset = 1000;
22 self.init = function(success, error) {
25 self._searchbox = document.getElementById("searchbox");
26 self._searchinput = document.getElementById("search");
27 self._content = document.getElementById("content");
28 self._back = document.getElementById("back-button");
31 self.currentView = '';
33 self.currentTitle = '';
35 self.checkNightMode();
40 this.go = function() {
41 document.getElementById("cover").style.display = 'none';
42 self.at_spinner = false;
47 this.sanitize = function(text) {
48 return text.replace(/&/g, "&").replace(/</g, "<");
51 this.showSearch = function() {
52 self._searchbox.style.display = "block";
55 this.hideSearch = function() {
56 self._searchbox.style.display = "none";
59 this.spinner = function(text) {
62 if (self.at_spinner) {
63 document.getElementById("spinnertext").innerHTML = text;
66 self._content.innerHTML = "<div class='spinner'><img src='img/spinner.png' /><div id='spinnertext'>" + text +"</div></div>";
67 self.at_spinner = true;
72 this.content = function(text, offset) {
74 self.at_spinner = false;
76 self._content.innerHTML = '';
77 self._content.innerHTML = text;
81 this.enter = function(url, offset) {
82 debug('View.enter: ' + url);
88 var slash_index = url.indexOf('/');
89 if (slash_index != -1) {
90 view = url.substr(0, slash_index);
91 arg = url.substr(slash_index + 1);
97 debug('View.enter: ' + view + ' ' + arg);
99 self.currentView = view;
100 self.currentPar = arg;
101 self['enter' + view](arg, offset);
105 this.enterIndex = function(arg, offset) {
107 Menu.setInfoButton("ProjectInfo", "O projekcie", true);
109 self.currentTitle = "Początek";
112 html += "<div class='buttons'>";
113 html += Links.button('Last', '', 'Ostatnio czytane');
114 html += Links.button('Bookmarks', '', 'Zakładki');
116 for (category in self.categories)
117 html += Links.button('Category', category, self.categories[category], 0);
120 html += "</div>" +"";
121 /*"<p id='logo'><img src='img/logo-wl-nq8.png' alt='Wolne Lektury' /><br/>\n" +
122 "szkolna biblioteka internetowa" +
124 self.content(html, offset);
127 this.enterBook = function(id, offset) {
129 debug('enterBook: ' + id);
130 Menu.setInfoButton("BookInfo", "O utworze", true);
133 Catalogue.withBook(id, function(book) {
134 self.currentTitle = book.authors + ', ' + book.title;
136 Catalogue.withChildren(id, function(children) {
137 var html = "<h1><span class='subheader'>";
138 html += book.authors;
139 html += "</span>" + book.title + "</h1>\n";
140 if (book.html_file) {
141 html += "<div class='buttons'>" + Links.button('BookText', id, "Czytaj tekst") + "</div>";
143 if (children.length) {
144 html += "<div class='buttons'>";
145 for (c in children) {
147 html += Links.bookLink(child);
151 self.content(html, offset);
158 this.enterBookText = function(id, offset) {
160 self.spinner("Otwieranie utworu");
161 debug('enterBookText: ' + id);
162 Menu.setInfoButton("BookInfo", "O utworze", true);
165 setTimeout("History.addRead("+id+");", 0);
167 FileRepo.withHtml(id, function(data) {
168 self.content(data, offset);
170 alert("Błąd pobierania: nie udało się pobrać treści utworu.");
173 Catalogue.withBook(id, function(book) {
174 self.currentTitle = book.authors + ', ' + book.title;
179 this.enterLast = function(ignored, offset) {
182 Menu.setInfoButton("ProjectInfo", "O projekcie", true);
183 self.currentTitle = 'Ostatnio czytane';
184 var html = "<h1><span class='subheader'>Ostatnio czytane</h1>\n";
186 var last_read = History.lastRead();
187 var some_books = false;
189 html += "<div class='buttons'>";
190 var add_books = function() {
191 if (last_read.length) {
192 var id = last_read.shift();
193 Catalogue.withBook(id, function(book) {
194 html += Links.bookLink(book);
203 html += "<p>Nie przeczytano żadnych utworów.</p>";
206 self.content(html, offset);
213 this.enterBookmarks = function(ignored, offset) {
214 debug("enterBookmarks");
216 Menu.setInfoButton("ProjectInfo", "O projekcie", true);
217 self.currentTitle = 'Zakładki';
218 var html = "<h1><span class='subheader'>Zakładki</h1>\n";
220 var bookmarks = History.bookmarks();
221 if (!bookmarks.length) {
222 html += "<p>Nie utworzono żadnych zakładek.</p>";
223 self.content(html, offset);
227 html += "<div class='buttons bookmarks'>";
228 for (i in bookmarks) {
229 var bm = bookmarks[i];
232 text += "<div class='sub'>" + bm.title + "</div>";
233 html += Links.deleteButton(bm.id);
234 html += Links.button(bm.view, bm.par, text, bm.offset);
237 self.content(html, offset);
240 this.onBookmarkChange = function() {
241 // TODO: preserve offset
242 if (self.currentView == 'Bookmarks') {
243 self.enterBookmarks();
247 this.enterTag = function(id, offset) {
249 debug('enterTag: ' + id);
250 Menu.setInfoButton("TagInfo", "O...", true);
253 self.spinner("Otwieranie listy utworów");
255 Catalogue.withTag(id, function(tag) {
256 Menu.setInfoButton("TagInfo", "O " + self.category_msc[tag.category], true);
257 self.currentTitle = tag.category + ': ' + tag.name;
258 var html = "<h1><span class='subheader upper'>" + tag.category + ': </span>' + tag.name + "</h1>\n";
259 html += "<div class='buttons'>";
261 Catalogue.withBooks(tag.books, function(books) {
262 for (var i in books) {
264 html += Links.bookLink(book);
267 self.content(html, offset);
276 this.enterCategory = function(category, offset) {
277 debug('enterCategory: ' + category);
278 Menu.setInfoButton("ProjectInfo", "O projekcie", true);
279 self.spinner("Otwieranie katalogu");
281 self.currentTitle = self.categories[category];
283 Catalogue.withCategory(category, function(tags) {
284 var html = "<h1>" + self.categories[category] + "</h1>\n";
285 html += "<div class='buttons'>";
288 html += Links.button('Tag', tag.id, tag.name);
291 self.content(html, offset);
296 this.enterSearch = function(query, offset) {
297 debug('enterSearch: ' + query);
298 Menu.setInfoButton("ProjectInfo", "O projekcie", true);
299 self.currentTitle = 'Szukaj: ' + query;
302 var html = "<h1><span class='subheader'>Szukana fraza:</span>" + View.sanitize(query) + "</h1>\n";
304 if (query.length < 2) {
305 html += "<p>Szukana fraza musi mieć co najmniej dwa znaki</p>";
306 self.content(html, offset);
310 Catalogue.withSearch(query, function(results) {
311 if (results.length == 1) {
312 var result = results[0];
313 if (result.view == 'Book' && result.item.html_file) {
314 self.enter(Links.href('BookText', result.item.id));
317 self.enter(Links.href(result.view, result.item.id));
321 if (results.length == 0) {
322 html += "<p>Brak wyników wyszukiwania</p>";
325 html += "<div class='buttons'>";
326 for (var i in results) {
327 var result = results[i];
328 if (result.view == 'Book')
329 html += Links.bookLink(result.item)
331 html += Links.button(result.view, result.item.id, result.item.name+"<div class='sub'>"+result.item.category+"</div>");
335 self.content(html, offset);
342 this.enterProjectInfo = function(arg, offset) {
343 debug('enterProjectInfo');
344 Menu.setInfoButton("ProjectInfo", "O projekcie", false);
346 self.currentTitle = "O projekcie";
350 html += '<div class="info">';
353 html += "<p style='text-align:center'><img src='img/logo-wl.png' /></p>";
354 html += "<p>Biblioteka internetowa Wolne Lektury "+
355 " udostępnia w swoich zbiorach lektury szkolne zalecane do użytku przez" +
356 " Ministerstwo Edukacji Narodowej i inne dzieła literatury.</p>";
358 html += "<p style='text-align:center'><img src='img/logo-fnp.png' /></p>";
360 html += "<img style='float:left;' src='img/procent.png' />" +
361 "<p style='margin-left: 50px'>" +
362 "Przekaż 1% podatku na rozwój Wolnych Lektur.<br/>" +
363 "Fundacja Nowoczesna Polska<br/>" +
364 "KRS 0000070056</p>";
366 html += "<p>Większość pozycji w bibliotece należy do domeny publicznej "+
367 "co oznacza, że nie są już chronione majatkowym prawem autorskim, "+
368 "a więc można je swobodnie wykorzystywać, publikować i rozpowszechniać. "+
369 "Publikujemy również kilka utworów, które autorzy udostępnili na wolnej licencji "+
370 "<a href='http://creativecommons.org/licenses/by-sa/3.0/deed.pl'>"+
371 "Creative Commons Uznanie Autorstwa - Na Tych Samych Warunkach 3.0.PL</a>.</p>";
373 html += "<p style='text-align:center'><img src='img/cc-by-sa.png' /></p>";
375 html += "<p>Copyright © 2011 Fundacja Nowoczesna Polska. Aplikacja jest wolnym oprogramowaniem "+
376 "dostępnym na licencji GNU Affero GPL w wersji 3 lub późniejszej.</p>";
378 html += "<p>Więcej informacji o projekcie znajduje sie na stronie <a href='http://www.wolnelektury.pl'>http://www.wolnelektury.pl</a>.</p>";
383 self.content(html, offset);
387 this.enterBookInfo = function(id, offset) {
389 debug('enterBookInfo: ' + id);
390 Menu.setInfoButton("ProjectInfo", "O projekcie", true);
393 Catalogue.withBook(id, function(book) {
394 self.currentTitle = "Informacje o: " + book.title;
396 var html = '<h2>' + book.authors + ', ' + book.title + '</h2>';
398 var url = WL + '/api/book/' + id + '/info.html';
400 var xhr = new XMLHttpRequest();
401 xhr.open("GET", url);
402 xhr.onload = function() {
403 debug('BookInfo: fetched by ajax: ' + url);
405 html += '<div class="info">';
406 html += xhr.responseText;
409 self.content(html, offset);
411 xhr.onerror = function(e) {
412 self.content("Brak informacji.", offset);
421 this.enterTagInfo = function(id, offset) {
423 debug('enterTagInfo: ' + id);
424 Menu.setInfoButton("ProjectInfo", "O projekcie", true);
427 Catalogue.withTag(id, function(tag) {
428 self.currentTitle = "Informacje o " + tag.name;
429 var html = '<h2>' + tag.name + '</h2>';
431 var url = WL + '/api/tag/' + id + '/info.html';
433 var xhr = new XMLHttpRequest();
434 xhr.open("GET", url);
435 xhr.onload = function() {
436 debug('TagInfo: fetched by ajax: ' + url);
438 html += '<div class="info">';
439 html += xhr.responseText;
442 self.content(html, offset);
444 xhr.onerror = function(e) {
445 self.content("Brak informacji.", offset);
454 /* search form submit callback */
455 this.search = function() {
456 History.visit('Search/' + self._searchinput.value);
457 self._content.focus()
462 self.getNightMode = function() {
463 night_mode = window.localStorage.getItem('View.night_mode');
464 if (night_mode === undefined)
470 self.checkNightMode = function() {
471 night_mode = self.getNightMode();
473 document.body.setAttribute("class", "night-mode");
476 document.body.setAttribute("class", "");
480 self.setNightMode = function(night_mode) {
481 night_mode = night_mode ? "1" : "";
482 window.localStorage.setItem('View.night_mode', night_mode);
483 self.checkNightMode();
486 self.toggleNightMode = function(night_mode) {
487 self.setNightMode(!self.getNightMode());
490 self.showBack = function(show) {
492 self._back.style.display = 'inline-block';
494 self._back.style.display = 'none';