ios version
[wl-mobile.git] / 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                 debug('History.init');
11
12                 self.viewStack = [];
13                 //navigator.app.overrideBackbutton(); FIXME!!!
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                 View.showBack(true);
23                 debug('History.visit: ' + url);
24                 View.enter(url, offset);
25         };
26         
27         self.goBack = function() {
28                 if (self.viewStack.length > 0) {
29                         var url = self.viewStack.pop();
30                         debug('History.goBack: ' + url);
31                         if (self.viewStack.length == 0)
32                                 View.showBack(false);
33                         View.enter(url);
34                 }
35                 else {
36                         debug('History: exiting');
37                         navigator.app.exitApp();
38                 }
39         };
40
41
42         self.lastRead = function() {
43                 var last_read = window.localStorage.getItem('History.last_ids');
44                 try {
45                         return last_read.split(';');
46                 } catch (err) {
47                         return [];
48                 }
49         };
50
51         self.addRead = function(id, offset) {
52                 id = "" + id; // this should check if int
53                 debug("History.addRead: " + id);
54                 var last_read = self.lastRead();
55                 var lastly = last_read.indexOf(id);
56                 if (lastly != -1) {
57                         last_read.splice(lastly, 1);
58                 }
59                 while (last_read.length >= 10) {
60                         last_read.pop();
61                 }
62                 last_read.unshift(id);
63                 window.localStorage.setItem('History.last_ids', last_read.join(';'));
64         }
65
66
67         self.bookmarks = function() {
68                 var bookmarks = window.localStorage.getItem('History.bookmarks');
69                 try {
70                         return JSON.parse(bookmarks) || [];
71                 } catch (err) {
72                         return [];
73                 }
74         };
75
76         self.addBookmark = function(name) {
77                 var id=(new Date).getTime();
78                 debug("History.addBookmark: " + id);
79
80                 var bms = self.bookmarks();
81                 bms.unshift({
82                         id: id,
83                         name: name,
84                         title: View.currentTitle,
85                         view: View.currentView,
86                         par: View.currentPar,
87                         offset: currentOffset()
88                 });
89                 window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
90         }
91
92         self.deleteBookmark = function(id) {
93                 debug("History.deleteBookmark: " + id);
94                 var bms = self.bookmarks();
95                 for (b in bms) {
96                         if (bms[b].id == id) {
97                                 bms.splice(b, 1);
98                         }
99                 }
100                 window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
101                 View.onBookmarkChange();
102         }
103 }