+/*
+ * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
+ * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+ */
+
+var History = new function() {
+ var self = this;
+
+ self.init = function(success, error) {
+ debug('History.init');
+
+ self.viewStack = [];
+ //navigator.app.overrideBackbutton(); FIXME!!!
+ //document.addEventListener("backbutton", History.goBack, true);
+
+ success && success();
+ };
+
+ self.visit = function(url, offset) {
+ offset = offset || 0;
+ self.viewStack.push(View.current);
+ View.showBack(true);
+ debug('History.visit: ' + url);
+ View.enter(url, offset);
+ };
+
+ self.goBack = function() {
+ if (self.viewStack.length > 0) {
+ var url = self.viewStack.pop();
+ debug('History.goBack: ' + url);
+ if (self.viewStack.length == 0)
+ View.showBack(false);
+ View.enter(url);
+ }
+ else {
+ debug('History: exiting');
+ navigator.app.exitApp();
+ }
+ };
+
+
+ self.lastRead = function() {
+ var last_read = window.localStorage.getItem('History.last_ids');
+ try {
+ return last_read.split(';');
+ } catch (err) {
+ return [];
+ }
+ };
+
+ self.addRead = function(id, offset) {
+ id = "" + id; // this should check if int
+ debug("History.addRead: " + id);
+ var last_read = self.lastRead();
+ var lastly = last_read.indexOf(id);
+ if (lastly != -1) {
+ last_read.splice(lastly, 1);
+ }
+ while (last_read.length >= 10) {
+ last_read.pop();
+ }
+ last_read.unshift(id);
+ window.localStorage.setItem('History.last_ids', last_read.join(';'));
+ }
+
+
+ self.bookmarks = function() {
+ var bookmarks = window.localStorage.getItem('History.bookmarks');
+ try {
+ return JSON.parse(bookmarks) || [];
+ } catch (err) {
+ return [];
+ }
+ };
+
+ self.addBookmark = function(name) {
+ var id=(new Date).getTime();
+ debug("History.addBookmark: " + id);
+
+ var bms = self.bookmarks();
+ bms.unshift({
+ id: id,
+ name: name,
+ title: View.currentTitle,
+ view: View.currentView,
+ par: View.currentPar,
+ offset: currentOffset()
+ });
+ window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
+ }
+
+ self.deleteBookmark = function(id) {
+ debug("History.deleteBookmark: " + id);
+ var bms = self.bookmarks();
+ for (b in bms) {
+ if (bms[b].id == id) {
+ bms.splice(b, 1);
+ }
+ }
+ window.localStorage.setItem('History.bookmarks', JSON.stringify(bms));
+ View.onBookmarkChange();
+ }
+}