summaryrefslogtreecommitdiff
path: root/js/offline.js
blob: 749b3ff09fcabdc82fe90d6b26e5e667784ea2fa (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';

function offline_search(form) {
	var query = $(".search_query").val();

	localforage.setItem("epube.search-query", query).then(function() {
		populate_list();
	});

	return false;
}

function offline_remove2(elem) {
	var bookId = elem.getAttribute("data-book-id");

	return offline_remove(bookId, function() {
		$("#cell-" + bookId).remove();
	});
}

function offline_clear() {

	if (confirm("Remove all offline data?")) {

		var promises = [];

		localforage.iterate(function(value, key, i) {

			if (key.match("epube-book")) {
				promises.push(localforage.removeItem(key));
			}
		});

		Promise.all(promises).then(function() {
			window.setTimeout(function() {
				populate_list();
			}, 500);
		});
	}
}


function populate_list() {

	var query = $.urlParam("query");

	if (query) query = query.toLowerCase();

	var books = $("#books_container");
	books.html("");

	localforage.iterate(function(value, key, i) {
		if (key.match(/epube-book\.\d{1,}$/)) {

			Promise.all([
				localforage.getItem(key),
				localforage.getItem(key + ".cover"),
				localforage.getItem(key + ".lastread"),
				localforage.getItem(key + ".book")
			]).then(function(results) {

				if (results[0] && results[3]) {
					var info = results[0];

					if (query) {
						var match =
							(info.series_name && info.series_name.toLowerCase().match(query)) ||
							(info.title && info.title.toLowerCase().match(query)) ||
							(info.author_sort && info.author_sort.toLowerCase().match(query));

						if (!match) return;
					}


					var cover = false;

					if (results && results[1]) {
						cover = URL.createObjectURL(results[1]);
					}

					var in_progress = false;
					var is_read = false;

					var lastread = results[2];
					if (lastread) {

						in_progress = lastread.page > 0;
						is_read = lastread.total > 0 && lastread.total - lastread.page < 5;
					}

					var cell = "<div class='col-xs-6 col-sm-3 col-md-2 index_cell' id=\"cell-"+info.id+"\">";

					var cover_read = is_read ? "read" : "";
					var title_class = in_progress ? "in_progress" : "";

					cell += "<div class=\"thumb "+cover_read+"\">";
					cell += "<a href=\"read.html?id="+info.epub_id+"&b="+info.id+"\"><img data-src=\"holder.js/120x180\"></a>";

					cell += "<div class=\"caption\">";
					cell += "<div><a class=\""+title_class+"\" href=\"read.html?id="+info.epub_id+"&b="+info.id+"\">" +
						info.title + "</a></div>";

					cell += "<div><a href=\#\" class=\"author_link\">" + info.author_sort + "</a></div>";

					if (info.series_name) {
						cell += "<div><a href=\"\" class=\"series_link\">" +
							info.series_name + " [" + info.series_index + "]</a></div>";
					}

					cell += "</div>";

					cell += "<div class=\"dropdown\" style=\"white-space : nowrap\">";
					cell += "<a href=\"#\" data-toggle=\"dropdown\" role=\"button\">" +
						"More..." + "<span class=\"caret\"></span></a>";

					cell += "<ul class=\"dropdown-menu\">";
					cell += "<li><a href=\"#\" data-book-id=\""+info.id+"\" onclick=\"return show_summary(this)\">Summary</a></li>";
					cell += "<li><a href=\"#\" data-book-id=\""+info.id+"\" onclick=\"offline_remove2(this)\">Remove offline data</a></li>";
					cell += "</ul>";

					cell += "</div>";

					cell += "</div>";
					cell += "</div>";

					var cell = $(cell);

					if (cover) {
						cell.find("img").attr("src", cover).qtip({
							position: {
								target: 'mouse',
								adjust: {
									mouse: false
								}
							},
							style: {
								classes: 'qtip-light qtip-custom'
							},
							show: {
								delay: 1000
							},
							hide: 'unfocus mouseleave',
							content: {
								text: info.comment ? info.comment : "No description available",
								title: info.title
							}
						});

						cell.find(".series_link")
							.attr("title", info.series_name + " [" + info.series_index  + "]")
							.attr("href", "offline.html?query=" + encodeURIComponent(info.series_name));

						cell.find(".author_link")
							.attr("title", info.author_sort)
							.attr("href", "offline.html?query=" + encodeURIComponent(info.author_sort));

					}

					books.append(cell);

					Holder.run();
				}
			});
		}
	});

}

function show_summary(elem) {
	var bookId = elem.getAttribute("data-book-id");

	localforage.getItem("epube-book." + bookId).then(function(data) {

		var comment = data.comment ? data.comment : 'No description available';

		$("#summary-modal .modal-title").html(data.title);
		$("#summary-modal .book-summary").html(comment);

		$("#summary-modal").modal();

	});

	return false;
}