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