0bd196b82a4162e28c7bcea29cb0d370e17698d9
[wl-mobile.git] / assets / www / js / filerepo.js
1 var FileRepo = new function() {
2         /* API for files repository */
3         var self = this;
4         this.root = null;
5
6         this.init = function(success, error) {
7                 self.initRoot(success);
8         };
9
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);
14                         self.root = fs.root;
15                         success && success();
16                 }, function() {
17                         console.log('local fs not found');
18                         success && success();
19                 });
20         };
21
22
23         this.withLocalHtml = function(book_id, success, error) {
24                 console.log('info:withLocalHtml: id:' + book_id);
25                 View.spinner('Otwieranie treści utworu');
26                 if (!self.root)
27                         error && error('info:withLocalHtml: no local html: no usable filesystem');
28
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);
36                 }
37                 xhr.onerror = error;
38                 xhr.send();
39         };
40
41
42         this.withLocal = function(win, fail) {
43                 if (self.root) {
44                         self.root.getDirectory('html', {}, function(dir) {
45                                 var reader = dir.createReader();
46                                 reader.readEntries(win, fail);
47                         });
48                 }
49                 else {
50                         win && win([]);
51                 }
52         }
53
54
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);
58                 // read file from WL
59                 Catalogue.withBook(book_id, function(book) {
60                         var url = WL + book.html_file;
61                         console.log('info:withHtmlFromServer: fetching url: ' + url);
62
63                         View.spinner("Pobieranie treści utworu z sieci");
64
65                         if (self.root) {
66                                 Downloader.downloadFile(url, self.root.fullPath + "/html/", ""+book_id, true,
67                                         function(data){
68                                                 console.log('info:withHtmlFromServer: loaded file from WL');
69                                                 self.withLocalHtml(book_id, success, error);
70                                         }, function(data) {
71                                                 console.log('error downloading file!')
72                                                 error && error("error: " + data);
73                                         });
74                         }
75                         else {
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();
79                                 xhr.open("GET", url);
80                                 xhr.onload = function() {
81                                         console.log('info:withHtmlFromServer: fetched by ajax: ' + url);
82                                         success && success(xhr.responseText);
83                                 }
84                                 xhr.onerror = function() {
85                                         console.log('error downloading file!')
86                                         error && error("error: " + data);
87                                 }
88                                 xhr.send();
89                         }
90                 });             
91         };
92         
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);
99                 });
100         };
101
102
103         this.clear = function() {
104                 FileRepo.withLocal(function(local) {
105                         for (i in local) {
106                                 local[i].remove();
107                         }
108                 });
109         };
110
111
112         this.deleteIfExists = function(id) {
113                 if (self.root) {
114                         self.root.getFile('html/' + id, {create: false}, function(f) {
115                                 f.remove();
116                         });
117                 }
118         }
119 };