nicer spinner animation, some cleaning
[wl-mobile.git] / assets / www / js / history.js
1 /*
2  * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
3  * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4  */
5
6 var History = new function() {
7         var self = this;
8
9         self.init = function(success, error) {
10                 console.log('History.init');
11
12                 self.viewStack = [];
13                 navigator.app.overrideBackbutton(); 
14                 document.addEventListener("backbutton", History.goBack, true);
15
16                 success && success();
17         };
18
19         self.visit = function(url, offset) {
20                 offset = offset || 0;
21                 self.viewStack.push(View.current);
22                 console.log('History.visit: ' + url);
23                 View.enter(url, offset);
24         };
25         
26         self.goBack = function() {
27                 if (self.viewStack.length > 0) {
28                         var url = self.viewStack.pop();
29                         console.log('History.goBack: ' + url);
30                         View.enter(url);
31                 }
32                 else {
33                         console.log('History: exiting');
34                         navigator.app.exitApp();
35                 }
36         };
37
38
39         self.lastRead = function() {
40                 var last_read = window.localStorage.getItem('History.last_ids');
41                 try {
42                         return last_read.split(';');
43                 } catch (err) {
44                         return [];
45                 }
46         };
47
48         self.addRead = function(id, offset) {
49                 id = "" + id; // this should check if int
50                 console.log("History.addRead: " + id);
51                 var last_read = self.lastRead();
52                 var lastly = last_read.indexOf(id);
53                 if (lastly != -1) {
54                         last_read.splice(lastly, 1);
55                 }
56                 while (last_read.length >= 10) {
57                         last_read.pop();
58                 }
59                 last_read.unshift(id);
60                 window.localStorage.setItem('History.last_ids', last_read.join(';'));
61         }
62
63
64         self.bookmarks = function() {
65                 var bookmarks = window.localStorage.getItem('History.bookmarks');
66                 try {
67                         return JSON.parse(bookmarks) || [];
68                 } catch (err) {
69                         return [];
70                 }
71         };
72
73         self.addBookmark = function(name) {
74                 var id=(new Date).getTime();
75                 console.log("History.addBookmark: " + id);
76
77                 var bms = self.bookmarks();
78                 bms.unshift({
79                         id: id,
80                         name: name,
81                         title: View.currentTitle,
82                         view: View.currentView,
83                         par: View.currentPar,
84                         offset: currentOffset()
85                 });
86                 window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
87         }
88
89         self.deleteBookmark = function(id) {
90                 console.log("History.deleteBookmark: " + id);
91                 var bms = self.bookmarks();
92                 for (b in bms) {
93                         if (bms[b].id == id) {
94                                 bms.splice(b, 1);
95                         }
96                 }
97                 window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
98                 View.onBookmarkChange();
99         }
100 }