X-Git-Url: https://git.mdrn.pl/wl-mobile.git/blobdiff_plain/8b7b42d45674861437a31e9f9125a277ce998721..b9acd6c8b2300a76624900cd19f9e40e59ff3f7b:/assets/www/js/catalogue.js diff --git a/assets/www/js/catalogue.js b/assets/www/js/catalogue.js deleted file mode 100644 index db9c069..0000000 --- a/assets/www/js/catalogue.js +++ /dev/null @@ -1,727 +0,0 @@ -// 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(/"; - 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 = "
"; - for (category in self.categories) - html += self.a('Category', category) + self.categories[category] + "\n"; - html += "
" + - ""; - 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 = "

"; - var auths = []; - for (a in authors) auths.push(authors[a].name); - html += auths.join(", "); - html += "" + book.title + "

\n"; - if (book.html_file) { - html += "

" + self.a('BookText', id) + "Czytaj tekst

"; - } - if (children.length) { - html += "
"; - for (c in children) { - child = children[c]; - html += self.a('Book', child.id) + child.title + "\n"; - } - html += "
"; - } - 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 = "

" + tag.category + ': ' + tag.name + "

\n"; - html += "
"; - 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 + "\n"; - } - html += "
"; - self.content(html); - }); - } - }); - }; - - - this.enterCategory = function(category) { - console.log('enterCategory: ' + category); - self.spinner("Otwieranie katalogu"); - self.showSearch(); - - Catalogue.withCategory(category, function(tags) { - var html = "

" + self.categories[category] + "

\n"; - html += "
"; - for (i in tags) { - tag = tags[i]; - html += self.a('Tag', tag.id) + tag.name + "\n"; - } - html += "
"; - self.content(html); - }); - }; - - - - this.enterSearch = function(query) { - console.log('enterTag: ' + query); - self.showSearch(); - - var html = "

Szukana fraza:" + View.sanitize(query) + "

\n"; - - if (query.length < 2) { - html += "

Szukana fraza musi mieć co najmniej dwa znaki

"; - self.content(html); - return; - } - - Catalogue.withSearch(query, function(results) { - if (results.length == 1) { - self.enter(self.href(results[0].view, results[0].id)); - return; - } - if (results.length == 0) { - html += "

Brak wyników wyszukiwania

"; - } - else { - html += "
"; - for (var i in results) { - var result = results[i]; - html += self.a(result.view, result.id) + result.label + "\n"; - } - html += "
"; - } - self.content(html); - }); - }; - - - /* search form submit callback */ - this.search = function() { - self.goTo('?Search/' + self._searchinput.value); - return false; - } - - - this.href = function(view, par) { - return "?"+view+"/"+par; - }; - - this.a = function(view, par) { - return ""; - }; - - this.goTo = function(url) { - self.viewStack.push(self.current); - console.log('goTo: ' + url); - self.enter(url); - }; - - this.goBack = function() { - if (self.viewStack.length > 0) { - var url = self.viewStack.pop(); - console.log('goBack: ' + url); - self.enter(url); - } - else { - console.log('exiting'); - navigator.app.exitApp(); - } - }; -} - - -/* -// for preparing sql statements -// use like: -// var s = new Sql("INSERT ... '{0}', '{1}' ..."; -// s.prepare("abc", ...) -var Sql = function(scheme) { - var self = this; - self.text = scheme; - - self.sql_escape = function(term) { - return term.toString().replace("'", "''"); - }; - - self.prepare = function() { - var args = arguments; - return self.text.replace(/{(\d+)}/g, function(match, number) { - return self.sql_escape(args[number]); - }); - } -};*/ - - -var Catalogue = new function() { - /* API for database */ - - var self = this; - self.db = null; - - this.init = function(success, error) { - console.log('Catalogue.init'); - - self.updateDB(function() { - self.db = window.openDatabase("wolnelektury", "1.0", "WL Catalogue", 1); - if (self.db) { - /*var regexp = { - onFunctionCall: function(val) { - var re = new RegExp(val.getString(0)); - if (val.getString(1).match(re)) - return 1; - else - return 0; - } - }; - self.db.createFunction("REGEXP", 2, regexp);*/ - - success && success(); - } else { - error && error('Nie mogę otworzyć bazy danych: ' + err); - } - - }, function(err) { - error && error('Błąd migracji: ' + err); - }); - }; - - self.sqlSanitize = function(term) { - return term.toString().replace("'", "''"); - }; - - - /* check if DB needs updating and upload a fresh copy, if so */ - this.updateDB = function(success, error) { - var db_ver = '0.1.4'; - if (window.localStorage.getItem('db_ver') == db_ver) { - console.log('db ok, skipping') - success && success(); - return; - } - - var done = function() { - window.localStorage.setItem('db_ver', db_ver); - console.log('db updated'); - success && success(); - }; - - // db initialize - // this is Android-specific for now - var version = device.version.split('.')[0]; - switch(version) { - case '1': - self.upload_db_android1(done, error); - break; - case '2': - self.upload_db_android2(done, error); - break; - case '3': - default: - error && error('Błąd migracji: ' + err); - }; - }; - - - this.upload_db_android1 = function(success, error) { - console.log('upload db for Android 1.x'); - window.requestFileSystem(LocalFileSystem.APPLICATION, 0, function(fs) { - window.plugins.assetcopy.copy("initial/wolnelektury.db", - fs.root.fullPath + "/databases/wolnelektury.db", true, - function(data) { - console.log('db upload successful'); - success && success(); - }, function(data) { - error && error("database upload error: " + data); - }); - }, error); - }; - - - this.upload_db_android2 = function(success, error) { - console.log('upload db for Android 2.x'); - window.requestFileSystem(LocalFileSystem.APPLICATION, 0, function(fs) { - - // upload databases description file - window.plugins.assetcopy.copy("initial/Databases.db", - fs.root.fullPath + "/app_database/Databases.db", true, - function(data) { - console.log('db descriptior upload successful'); - - // upload the database file - window.plugins.assetcopy.copy("initial/0000000000000001.db", - fs.root.fullPath + "/app_database/file__0/0000000000000001.db", true, - function(data) { - console.log('db upload successful'); - success && success(); - }, function(data) { - error && error("database upload error: " + data); - }); - - - }, function(data) { - error && error("database descriptor upload error: " + data); - }); - - }, error); - }; - - - this.withBook = function(id, callback) { - console.log('withBook '+id) - self.db.transaction(function(tx) { - tx.executeSql("SELECT * FROM book WHERE id="+id, [], - function(tx, results) { - callback(results.rows.item(0)); - }); - }); - }; - - this.withBooks = function(ids, callback) { - console.log('withBooks ' + ids) - self.db.transaction(function(tx) { - tx.executeSql("SELECT * FROM book WHERE id IN ("+ids+") ORDER BY title", [], - function(tx, results) { - var items = []; - var count = results.rows.length; - for (var i=0; i