-// FIXME: htmlescape strings!
-
-var VERSION = '0.1';
-
-
-var FileRepo = new function() {
- /* API for files repository */
- var self = this;
- const WL_URL = 'http://www.wolnelektury.pl';
- this.root = null;
-
- this.init = function(success, error) {
- self.initRoot(success);
- };
-
- this.initRoot = function(success) {
- // fs size is irrelevant, PERSISTENT is futile (on Android, at least)
- window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
- console.log('local fs found: ' + fs.root.fullPath);
- self.root = fs.root;
- success && success();
- }, function() {
- console.log('local fs not found');
- success && success();
- });
- };
-
-
- this.withLocalHtml = function(book_id, success, error) {
- console.log('info:withLocalHtml: id:' + book_id);
- View.spinner('Otwieranie treści utworu');
- if (!self.root)
- error && error('info:withLocalHtml: no local html: no usable filesystem');
-
- var url = "file://" + self.root.fullPath + "/html/" + book_id;
- console.log('info:withLocalHtml: local ajax: ' + url);
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.onload = function() {
- console.log('info:withLocalHtml: fetched by local ajax: ' + url);
- success && success(xhr.responseText);
- }
- xhr.onerror = error;
- xhr.send();
- };
-
-
- // downloads HTML file from server, saves it in cache and calls success with file contents
- this.withHtmlFromServer = function(book_id, success, error) {
- console.log('info:withHtmlFromServer: id:' + book_id);
- // read file from WL
- Catalogue.withBook(book_id, function(book) {
- var url = WL_URL + book.html_file;
- console.log('info:withHtmlFromServer: fetching url: ' + url);
-
- View.spinner("Pobieranie treści utworu z sieci");
-
- if (self.root) {
- window.plugins.downloader.downloadFile(url, self.root.fullPath + "/html/", ""+book_id, true,
- function(data){
- console.log('info:withHtmlFromServer: loaded file from WL');
- self.withLocalHtml(book_id, success, error);
- }, function(data) {
- console.log('error downloading file!')
- error && error("error: "+data);
- });
- }
- else {
- // there's no big fs, so we'll just get the text from AJAX
- console.log('info:withHtmlFromServer: ajax: ' + url);
- var xhr = new XMLHttpRequest();
- xhr.open(url);
- xhr.onload = function() {
- console.log('info:withHtmlFromServer: fetched by ajax: ' + url);
- success && success(xhr.responseText);
- }
- xhr.send();
- }
- });
- };
-
- // calls the callback with contents of the HTML file for a given book,
- // loaded from the server and cached locally
- this.withHtml = function(id, success, error) {
- console.log('info:withHtml: id:' + id);
- self.withLocalHtml(id, success, function() {
- self.withHtmlFromServer(id, success, error);
- });
- };
-};
-
-
-var View = new function() {
- var self = this;
- self.minOffset = 1000;
- //self.element
- self.categories = {
- autor: 'Autorzy',
- rodzaj: 'Rodzaje',
- gatunek: 'Gatunki',
- epoka: 'Epoki'
- };
-
-
- self.init = function() {
- console.log('View.init');
-
- self.viewStack = [];
- self.current;
- navigator.app.overrideBackbutton();
- document.addEventListener("backbutton", View.goBack, true);
-
- self._searchbox = document.getElementById("searchbox");
- self._searchinput = document.getElementById("search");
- self._content = document.getElementById("content");
-
- self.enter(location.href);
- };
-
-
- this.sanitize = function(text) {
- return text.replace(/&/g, "&").replace(/</g, "<");
- };
-
- this.showSearch = function() {
- self._searchbox.style.display = "block";
- };
-
- this.hideSearch = function() {
- self._searchbox.style.display = "none";
- };
-
- this.spinner = function(text) {
- if (!text)
- text = "Ładowanie";
- self._content.innerHTML = "<div class='spinner'><img src='img/spinner.gif' /><br/><span id='spinnertext'>" + text +"</span></div>";
- scroll(0, 0);
- };
-
- this.content = function(text) {
- console.log('content');
-
- self._content.innerHTML = '';
- self._content.innerHTML = text;
- scroll(0, 0);
- }
-
- this.enter = function(url) {
- console.log('View.enter: ' + url);
-
- self.current = url;
- var view = 'Index';
- var arg = null;
-
- var query_start = url.indexOf('?');
- if (query_start != -1) {
- var slash_index = url.indexOf('/', query_start + 1);
- if (slash_index != -1) {
- view = url.substr(query_start + 1, slash_index - query_start - 1);
- arg = url.substr(slash_index + 1);
- }
- else {
- view = url.substr(query_start + 1);
- }
- }
- console.log('View.enter: ' + view + ' ' + arg);
- self['enter' + view](arg);
- }
-
- this.enterIndex = function(arg) {
- console.log('enterIndex');
- self.showSearch();
- var html = "<div class='book-list'>";
- for (category in self.categories)
- html += self.a('Category', category) + self.categories[category] + "</a>\n";
- html += "</div>" +
- "<p id='logo'><img src='img/wl-logo.png' alt='Wolne Lektury' /><br/>\n" +
- "szkolna biblioteka internetowa" +
- //"<br/>v. " + VERSION +
- "</p>";
- self.content(html);
- };
-
- this.enterBook = function(id) {
- id = parseInt(id);
- console.log('enterBook: ' + id);
- self.showSearch();
-
- Catalogue.withBook(id, function(book) {
- Catalogue.withChildren(id, function(children) {
- Catalogue.withAuthors(id, function(authors) {
- var html = "<h1><span class='subheader'>";
- var auths = [];
- for (a in authors) auths.push(authors[a].name);
- html += auths.join(", ");
- html += "</span>" + book.title + "</h1>\n";
- if (book.html_file) {
- html += "<p class='buttons'>" + self.a('BookText', id) + "Czytaj tekst</a></p>";
- }
- if (children.length) {
- html += "<div class='book-list'>";
- for (c in children) {
- child = children[c];
- html += self.a('Book', child.id) + child.title + "</a>\n";
- }
- html += "</div>";
- }
- self.content(html);
- });
- });
- });
- }
-
- this.enterBookText = function(id) {
- id = parseInt(id);
- self.spinner("Otwieranie utworu");
- console.log('enterBookText: ' + id);
- self.hideSearch();
-
- FileRepo.withHtml(id, function(data) {
- self.content(data);
- });
- }
-
- this.enterTag = function(id) {
- id = parseInt(id);
- console.log('enterTag: ' + id);
- self.showSearch();
-
- self.spinner("Otwieranie listy utworów");
-
- Catalogue.withTag(id, function(tag) {
- var html = "<h1><span class='subheader upper'>" + tag.category + ': </span>' + tag.name + "</h1>\n";
- html += "<div class='book-list'>";
- if (tag._books) {
- Catalogue.withBooks(tag._books, function(books) {
- for (var i in books) {
- var book = books[i];
- html += self.a('Book', book.id) + book.title + "</a>\n";
- }
- html += "</div>";
- self.content(html);
- });
- }
- });
- };
-
-
- this.enterCategory = function(category) {
- console.log('enterCategory: ' + category);
- self.spinner("Otwieranie katalogu");
- self.showSearch();
-
- Catalogue.withCategory(category, function(tags) {
- var html = "<h1>" + self.categories[category] + "</h1>\n";
- html += "<div class='book-list'>";
- for (i in tags) {
- tag = tags[i];
- html += self.a('Tag', tag.id) + tag.name + "</a>\n";
- }
- html += "</div>";
- self.content(html);
- });
- };
-
-