diff --git a/cps/static/js/reading/epub.js b/cps/static/js/reading/epub.js new file mode 100644 index 00000000..d8de6f5b --- /dev/null +++ b/cps/static/js/reading/epub.js @@ -0,0 +1,38 @@ +/* global $, calibre, EPUBJS, ePubReader */ + +(function() { + "use strict"; + + EPUBJS.filePath = calibre.filePath; + EPUBJS.cssPath = calibre.cssPath; + + var reader = ePubReader(calibre.bookUrl, { + restore: true, + bookmarks: [calibre.bookmark] + }); + reader.on("reader:bookmarked", updateBookmark.bind(reader, "add")); + reader.on("reader:unbookmarked", updateBookmark.bind(reader, "remove")); + + /** + * @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 !== location; + }).map(function (bookmark) { + this.removeBookmark(bookmark); + }.bind(this)); + } + + // Save to database + $.ajax(calibre.bookmarkUrl, { + method: "post", + data: { bookmark: location || "" } + }).fail(function (xhr, status, error) { + alert(error); + }); + } +})(); diff --git a/cps/templates/read.html b/cps/templates/read.html index d02f76d1..dc44ce9b 100644 --- a/cps/templates/read.html +++ b/cps/templates/read.html @@ -11,59 +11,6 @@ - - - - - - - - - - - - - - - - -
+ + + + + + + + +