summaryrefslogtreecommitdiff
path: root/js/index.js
blob: 60e4d12a558796cb944e4b216a932d0bea5403d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function cache_refresh() {
	if ('serviceWorker' in navigator) {
		localforage.getItem("epube.cache-timestamp").then(function(stamp) {
			var ts = parseInt(new Date().getTime()/1000);

			if (!stamp || ts - stamp > 3600) {
				console.log('asking worker to refresh cache');
				navigator.serviceWorker.controller.postMessage("refresh-cache");
				localforage.setItem("epube.cache-timestamp", ts);
			}

		});
	}
}

function mark_offline(elem) {

	var bookId = elem.getAttribute("data-book-id");
	var cacheId = "epube-book." + bookId;

	localforage.getItem(cacheId).then(function(book) {
		if (book) {
			elem.onclick = function() {
				offline_remove(bookId, function() {
					mark_offline(elem);
				});
				return false;
			};

			elem.innerHTML = "Remove offline data";


		} else {
			elem.onclick = function() {
				offline_cache(bookId, function() {
					mark_offline(elem);
				});
				return false;
			};

			elem.innerHTML = "Make available offline";
		}
	});
}

function mark_offline_books() {
	var elems = $(".offline_dropitem");

	$.each(elems, function (i, elem) {
		mark_offline(elem);
	});
}

function offline_cache(bookId, callback) {
	console.log("offline cache: " + bookId);

	$.post("backend.php", {op: "getinfo", id: bookId}, function(data) {

		if (data) {
			var cacheId = 'epube-book.' + bookId;

			localforage.setItem(cacheId, data).then(function(data) {

				console.log(cacheId + ' got data');

				fetch('backend.php?op=download&id=' + data.epub_id, {credentials: 'same-origin'}).then(function(resp) {
					if (resp.status == 200) {
						console.log(cacheId + ' got book');

						callback();

						localforage.setItem(cacheId + '.book', resp.blob());
					}
				});

				fetch("backend.php?op=getpagination&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) {
					if (resp.status == 200) {
						console.log(cacheId + ' got pagination');

						resp.text().then(function(text) {
							localforage.setItem(cacheId + '.pagination', JSON.parse(text));
						});
					}
				});

				fetch("backend.php?op=getlastread&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) {
					if (resp.status == 200) {
						console.log(cacheId + ' got lastread');
						resp.text().then(function(text) {
							localforage.setItem(cacheId + '.lastread', JSON.parse(text));
						});
					}
				});

				if (data.has_cover) {

					fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) {

						if (resp.status == 200) {
							console.log(cacheId + ' got cover');
							localforage.setItem(cacheId + '.cover', resp.blob());
						}

					});

				}

			});
		}

	});
}