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.
6 var History = new function() {
9 self.init = function(success, error) {
10 console.log('History.init');
13 navigator.app.overrideBackbutton();
14 document.addEventListener("backbutton", History.goBack, true);
19 self.visit = function(url, offset) {
21 self.viewStack.push(View.current);
22 console.log('History.visit: ' + url);
23 View.enter(url, offset);
26 self.goBack = function() {
27 if (self.viewStack.length > 0) {
28 var url = self.viewStack.pop();
29 console.log('History.goBack: ' + url);
33 console.log('History: exiting');
34 navigator.app.exitApp();
39 self.lastRead = function() {
40 var last_read = window.localStorage.getItem('History.last_ids');
42 return last_read.split(';');
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);
54 last_read.splice(lastly, 1);
56 while (last_read.length >= 10) {
59 last_read.unshift(id);
60 window.localStorage.setItem('History.last_ids', last_read.join(';'));
64 self.bookmarks = function() {
65 var bookmarks = window.localStorage.getItem('History.bookmarks');
67 return JSON.parse(bookmarks) || [];
73 self.addBookmark = function(name) {
74 var id=(new Date).getTime();
75 console.log("History.addBookmark: " + id);
77 var bms = self.bookmarks();
81 title: View.currentTitle,
82 view: View.currentView,
84 offset: currentOffset()
86 window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
89 self.deleteBookmark = function(id) {
90 console.log("History.deleteBookmark: " + id);
91 var bms = self.bookmarks();
93 if (bms[b].id == id) {
97 window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
98 View.onBookmarkChange();