X-Git-Url: https://git.mdrn.pl/wl-mobile.git/blobdiff_plain/d947c00d92691df552a793f8d9c2991c931ad244..HEAD:/assets/www/js/history.js

diff --git a/assets/www/js/history.js b/assets/www/js/history.js
new file mode 100644
index 0000000..1b9f9c6
--- /dev/null
+++ b/assets/www/js/history.js
@@ -0,0 +1,100 @@
+/*
+ * 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) {
+		console.log('History.init');
+
+		self.viewStack = [];
+		navigator.app.overrideBackbutton(); 
+		document.addEventListener("backbutton", History.goBack, true);
+
+		success && success();
+	};
+
+	self.visit = function(url, offset) {
+		offset = offset || 0;
+		self.viewStack.push(View.current);
+		console.log('History.visit: ' + url);
+		View.enter(url, offset);
+	};
+	
+	self.goBack = function() {
+		if (self.viewStack.length > 0) {
+			var url = self.viewStack.pop();
+			console.log('History.goBack: ' + url);
+			View.enter(url);
+		}
+		else {
+			console.log('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
+		console.log("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();
+		console.log("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) {
+		console.log("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();
+	}
+}