mirror of
https://github.com/janeczku/calibre-web
synced 2024-12-24 09:00:29 +00:00
84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
/* global $, calibre, EPUBJS, ePubReader */
|
|
|
|
var reader;
|
|
|
|
(function() {
|
|
"use strict";
|
|
|
|
EPUBJS.filePath = calibre.filePath;
|
|
EPUBJS.cssPath = calibre.cssPath;
|
|
|
|
reader = ePubReader(calibre.bookUrl, {
|
|
restore: true,
|
|
bookmarks: calibre.bookmark ? [calibre.bookmark] : []
|
|
});
|
|
|
|
reader.rendition.themes.register("lightTheme", "/static/css/epub_themes.css");
|
|
reader.rendition.themes.register("darkTheme", "/static/css/epub_themes.css");
|
|
reader.rendition.themes.register("sepiaTheme", "/static/css/epub_themes.css");
|
|
reader.rendition.themes.register("blackTheme", "/static/css/epub_themes.css");
|
|
|
|
if (calibre.useBookmarks) {
|
|
reader.on("reader:bookmarked", updateBookmark.bind(reader, "add"));
|
|
reader.on("reader:unbookmarked", updateBookmark.bind(reader, "remove"));
|
|
} else {
|
|
$("#bookmark, #show-Bookmarks").remove();
|
|
}
|
|
|
|
// Enable swipe support
|
|
// I have no idea why swiperRight/swiperLeft from plugins is not working, events just don't get fired
|
|
var touchStart = 0;
|
|
var touchEnd = 0;
|
|
|
|
reader.rendition.on('touchstart', function(event) {
|
|
touchStart = event.changedTouches[0].screenX;
|
|
});
|
|
reader.rendition.on('touchend', function(event) {
|
|
touchEnd = event.changedTouches[0].screenX;
|
|
if (touchStart < touchEnd) {
|
|
if(reader.book.package.metadata.direction === "rtl") {
|
|
reader.rendition.next();
|
|
} else {
|
|
reader.rendition.prev();
|
|
}
|
|
// Swiped Right
|
|
}
|
|
if (touchStart > touchEnd) {
|
|
if(reader.book.package.metadata.direction === "rtl") {
|
|
reader.rendition.prev();
|
|
} else {
|
|
reader.rendition.next();
|
|
}
|
|
// Swiped Left
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @param {string} action - Add or remove bookmark
|
|
* @param {string|int} location - Location or zero
|
|
*/
|
|
function updateBookmark(action, location) {
|
|
// Remove other bookmarks (there can only be one)
|
|
if (action === "add") {
|
|
this.settings.bookmarks.filter(function (bookmark) {
|
|
return bookmark && bookmark !== location;
|
|
}).map(function (bookmark) {
|
|
this.removeBookmark(bookmark);
|
|
}.bind(this));
|
|
}
|
|
|
|
var csrftoken = $("input[name='csrf_token']").val();
|
|
|
|
// Save to database
|
|
$.ajax(calibre.bookmarkUrl, {
|
|
method: "post",
|
|
data: { bookmark: location || "" },
|
|
headers: { "X-CSRFToken": csrftoken }
|
|
}).fail(function (xhr, status, error) {
|
|
alert(error);
|
|
});
|
|
}
|
|
})();
|
|
|
|
|