1 var FileRepo = new function() {
2 /* API for files repository */
6 this.init = function(success, error) {
7 self.initRoot(success);
10 this.initRoot = function(success) {
11 // fs size is irrelevant, PERSISTENT is futile (on Android, at least)
12 window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
13 console.log('local fs found: ' + fs.root.fullPath);
17 console.log('local fs not found');
23 this.withLocalHtml = function(book_id, success, error) {
24 console.log('info:withLocalHtml: id:' + book_id);
25 View.spinner('Otwieranie treści utworu');
27 error && error('info:withLocalHtml: no local html: no usable filesystem');
29 var url = "file://" + self.root.fullPath + "/html/" + book_id;
30 console.log('info:withLocalHtml: local ajax: ' + url);
31 var xhr = new XMLHttpRequest();
32 xhr.open('GET', url, true);
33 xhr.onload = function() {
34 console.log('info:withLocalHtml: fetched by local ajax: ' + url);
35 success && success(xhr.responseText);
42 this.withLocal = function(win, fail) {
44 self.root.getDirectory('html', {}, function(dir) {
45 var reader = dir.createReader();
46 reader.readEntries(win, fail);
55 // downloads HTML file from server, saves it in cache and calls success with file contents
56 this.withHtmlFromServer = function(book_id, success, error) {
57 console.log('info:withHtmlFromServer: id:' + book_id);
59 Catalogue.withBook(book_id, function(book) {
60 var url = WL + book.html_file;
61 console.log('info:withHtmlFromServer: fetching url: ' + url);
63 View.spinner("Pobieranie treści utworu z sieci");
66 Downloader.downloadFile(url, self.root.fullPath + "/html/", ""+book_id, true,
68 console.log('info:withHtmlFromServer: loaded file from WL');
69 self.withLocalHtml(book_id, success, error);
71 console.log('error downloading file!')
72 error && error("error: " + data);
76 // there's no big fs, so we'll just get the text from AJAX
77 console.log('info:withHtmlFromServer: ajax: ' + url);
78 var xhr = new XMLHttpRequest();
80 xhr.onload = function() {
81 console.log('info:withHtmlFromServer: fetched by ajax: ' + url);
82 success && success(xhr.responseText);
84 xhr.onerror = function() {
85 console.log('error downloading file!')
86 error && error("error: " + data);
93 // calls the callback with contents of the HTML file for a given book,
94 // loaded from the server and cached locally
95 this.withHtml = function(id, success, error) {
96 console.log('info:withHtml: id:' + id);
97 self.withLocalHtml(id, success, function() {
98 self.withHtmlFromServer(id, success, error);
103 this.clear = function() {
104 FileRepo.withLocal(function(local) {
112 this.deleteIfExists = function(id) {
114 self.root.getFile('html/' + id, {create: false}, function(f) {