X-Git-Url: https://git.mdrn.pl/wl-mobile.git/blobdiff_plain/065d23f9494e1afb7f61ada08b728405b1b7a0cf..b9acd6c8b2300a76624900cd19f9e40e59ff3f7b:/assets/www/js/catalogue.js?ds=inline diff --git a/assets/www/js/catalogue.js b/assets/www/js/catalogue.js deleted file mode 100644 index 268c817..0000000 --- a/assets/www/js/catalogue.js +++ /dev/null @@ -1,378 +0,0 @@ -var DB_VER = '0.9.15'; - -var WL_INITIAL = WL + '/media/api/mobile/initial/initial.db'; -var WL_UPDATE = WL + '/api/changes/SINCE.json?book_fields=author,html,parent,parent_number,sort_key,title' + - '&tag_fields=books,category,name,sort_key' + - '&tag_categories=author,epoch,genre,kind'; - - - -var categories = {'author': 'autor', - 'epoch': 'epoka', - 'genre': 'gatunek', - 'kind': 'rodzaj', - 'theme': 'motyw' - } - -// FIXME: htmlescape strings! - - -// 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[parseInt(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 has_ver = window.localStorage.getItem('db_ver'); - if (has_ver == DB_VER) { - console.log('db ok, skipping') - success && success(); - return; - } - - var done = function() { - FileRepo.clear(); - window.localStorage.setItem('db_ver', DB_VER); - console.log('db updated'); - success && success(); - }; - - // db initialize - // this is Android-specific for now - var android = device.version.split('.')[0]; - if (android > 1) { - self.upload_db_android(done, error); - } else { - error && error("Nieobsługiwana wersja systemu. Wymagany Android>=2.0."); - }; - }; - - - this.upload_db_android = function(success, error) { - // TODO: this should be downloaded from teh net, not stored in res - - console.log('upload db for Android 2.x+'); - // upload databases description file - - var dbname = "wolnelektury"; - var db = window.openDatabase(dbname, "1.0", "WL Catalogue", 500000); - if (db) { - console.log('db created successfully'); - DBPut.fetch(WL_INITIAL, function(data) { - console.log('db fetch successful'); - success && success(); - }, function(data) { - error && error('Błąd podczas pobierania bazy danych: ' + data); - }); - } else { - error && error('Błąd podczas inicjowania bazy danych: ' + data); - } - }; - - - this.withState = function(callback) { - self.db.transaction(function(tx) { - tx.executeSql("SELECT * FROM state", [], - function(tx, results) { - if (results.rows.length) { - callback(results.rows.item(0)); - } - else { - callback({last_checked: 0}); - } - }); - }); - }; - - - this.withBook = function(id, callback, error) { - console.log('withBook '+id) - self.db.transaction(function(tx) { - tx.executeSql("SELECT * FROM book WHERE id="+id, [], - function(tx, results) { - if (results.rows.length) { - callback(results.rows.item(0)); - } - else { - error && error(); - } - }); - }); - }; - - 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 sort_key", [], - function(tx, results) { - var items = []; - var count = results.rows.length; - for (var i=0; i