2020-05-22 10:39:21 +09:00
|
|
|
import connectionManager from 'connectionManager';
|
|
|
|
import loading from 'loading';
|
|
|
|
import keyboardnavigation from 'keyboardnavigation';
|
|
|
|
import dialogHelper from 'dialogHelper';
|
2020-05-29 19:13:02 +10:00
|
|
|
import events from 'events';
|
2020-05-22 10:39:21 +09:00
|
|
|
import 'css!./style';
|
|
|
|
import 'material-icons';
|
|
|
|
import 'paper-icon-button-light';
|
|
|
|
|
2020-05-28 21:14:25 +10:00
|
|
|
import TableOfContent from './tableOfContent';
|
|
|
|
|
2020-05-22 10:39:21 +09:00
|
|
|
export class BookPlayer {
|
2020-05-22 10:42:58 +09:00
|
|
|
constructor() {
|
|
|
|
this.name = 'Book Player';
|
|
|
|
this.type = 'mediaplayer';
|
|
|
|
this.id = 'bookplayer';
|
|
|
|
this.priority = 1;
|
2020-05-28 21:14:25 +10:00
|
|
|
|
|
|
|
this.onDialogClosed = this.onDialogClosed.bind(this);
|
|
|
|
this.openTableOfContents = this.openTableOfContents.bind(this);
|
|
|
|
this.onWindowKeyUp = this.onWindowKeyUp.bind(this);
|
2020-05-22 10:42:58 +09:00
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
play(options) {
|
2020-05-29 19:13:02 +10:00
|
|
|
this._progress = 0;
|
|
|
|
this._loaded = false;
|
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
loading.show();
|
|
|
|
let elem = this.createMediaElement();
|
|
|
|
return this.setCurrentSrc(elem, options);
|
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
stop() {
|
2020-05-28 21:14:25 +10:00
|
|
|
this.unbindEvents();
|
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
let elem = this._mediaElement;
|
2020-05-28 18:38:13 +10:00
|
|
|
let tocElement = this._tocElement;
|
2020-05-22 10:42:58 +09:00
|
|
|
let rendition = this._rendition;
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-28 18:38:13 +10:00
|
|
|
if (elem) {
|
|
|
|
dialogHelper.close(elem);
|
2020-05-22 10:42:58 +09:00
|
|
|
this._mediaElement = null;
|
2020-05-22 10:39:21 +09:00
|
|
|
}
|
2020-05-28 18:38:13 +10:00
|
|
|
|
|
|
|
if (tocElement) {
|
2020-05-28 21:14:25 +10:00
|
|
|
tocElement.destroy();
|
2020-05-28 18:38:13 +10:00
|
|
|
this._tocElement = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rendition) {
|
|
|
|
rendition.destroy();
|
|
|
|
}
|
2020-05-29 19:13:02 +10:00
|
|
|
|
|
|
|
// Hide loader in case player was not fully loaded yet
|
|
|
|
loading.hide();
|
|
|
|
this._cancellationToken.shouldCancel = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentItem() {
|
|
|
|
return this._currentItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTime() {
|
|
|
|
return this._progress * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
duration() {
|
|
|
|
return 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBufferedRanges() {
|
|
|
|
return [{
|
|
|
|
start: 0,
|
|
|
|
end: 10000000
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
volume() {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
isMuted() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
paused() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
seekable() {
|
|
|
|
return true;
|
2020-05-22 10:42:58 +09:00
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
onWindowKeyUp(e) {
|
|
|
|
let key = keyboardnavigation.getKeyName(e);
|
|
|
|
let rendition = this._rendition;
|
|
|
|
let book = rendition.book;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'l':
|
|
|
|
case 'ArrowRight':
|
|
|
|
case 'Right':
|
2020-05-29 19:13:02 +10:00
|
|
|
if (this._loaded) {
|
|
|
|
book.package.metadata.direction === 'rtl' ? rendition.prev() : rendition.next();
|
|
|
|
}
|
2020-05-22 10:42:58 +09:00
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
case 'ArrowLeft':
|
|
|
|
case 'Left':
|
2020-05-29 19:13:02 +10:00
|
|
|
if (this._loaded) {
|
|
|
|
book.package.metadata.direction === 'rtl' ? rendition.next() : rendition.prev();
|
|
|
|
}
|
2020-05-22 10:42:58 +09:00
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
if (this._tocElement) {
|
2020-05-28 21:14:25 +10:00
|
|
|
// Close table of contents on ESC if it is open
|
|
|
|
this._tocElement.destroy();
|
|
|
|
} else {
|
|
|
|
// Otherwise stop the entire book player
|
|
|
|
this.stop();
|
2020-05-22 10:42:58 +09:00
|
|
|
}
|
|
|
|
break;
|
2020-05-16 22:02:52 +10:00
|
|
|
}
|
2020-05-22 10:42:58 +09:00
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
onDialogClosed() {
|
|
|
|
this.stop();
|
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-28 21:14:25 +10:00
|
|
|
bindMediaElementEvents() {
|
|
|
|
let elem = this._mediaElement;
|
2020-05-19 16:05:44 +10:00
|
|
|
|
2020-05-28 21:14:25 +10:00
|
|
|
elem.addEventListener('close', this.onDialogClosed, {once: true});
|
|
|
|
elem.querySelector('.btnBookplayerExit').addEventListener('click', this.onDialogClosed, {once: true});
|
|
|
|
elem.querySelector('.btnBookplayerToc').addEventListener('click', this.openTableOfContents);
|
|
|
|
}
|
2020-05-19 16:05:44 +10:00
|
|
|
|
2020-05-28 21:14:25 +10:00
|
|
|
bindEvents() {
|
|
|
|
this.bindMediaElementEvents();
|
|
|
|
|
|
|
|
document.addEventListener('keyup', this.onWindowKeyUp);
|
|
|
|
// FIXME: I don't really get why document keyup event is not triggered when epub is in focus
|
|
|
|
this._rendition.on('keyup', this.onWindowKeyUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
unbindMediaElementEvents() {
|
|
|
|
let elem = this._mediaElement;
|
|
|
|
|
|
|
|
elem.removeEventListener('close', this.onDialogClosed);
|
|
|
|
elem.querySelector('.btnBookplayerExit').removeEventListener('click', this.onDialogClosed);
|
|
|
|
elem.querySelector('.btnBookplayerToc').removeEventListener('click', this.openTableOfContents);
|
|
|
|
}
|
|
|
|
|
|
|
|
unbindEvents() {
|
|
|
|
if (this._mediaElement) {
|
|
|
|
this.unbindMediaElementEvents();
|
|
|
|
}
|
|
|
|
document.removeEventListener('keyup', this.onWindowKeyUp);
|
|
|
|
if (this._rendition) {
|
|
|
|
this._rendition.off('keyup', this.onWindowKeyUp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openTableOfContents() {
|
2020-05-29 19:13:02 +10:00
|
|
|
if (this._loaded) {
|
|
|
|
this._tocElement = new TableOfContent(this);
|
|
|
|
}
|
2020-05-22 10:42:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
createMediaElement() {
|
|
|
|
let elem = this._mediaElement;
|
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
return elem;
|
2020-05-19 16:05:44 +10:00
|
|
|
}
|
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
elem = document.getElementById('bookPlayer');
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
if (!elem) {
|
|
|
|
elem = dialogHelper.createDialog({
|
|
|
|
exitAnimationDuration: 400,
|
|
|
|
size: 'fullscreen',
|
|
|
|
autoFocus: false,
|
|
|
|
scrollY: false,
|
|
|
|
exitAnimation: 'fadeout',
|
|
|
|
removeOnClose: true
|
|
|
|
});
|
|
|
|
elem.id = 'bookPlayer';
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
let html = '';
|
|
|
|
html += '<div class="topRightActionButtons">';
|
|
|
|
html += '<button is="paper-icon-button-light" class="autoSize bookplayerButton btnBookplayerExit hide-mouse-idle-tv" tabindex="-1"><i class="material-icons bookplayerButtonIcon close"></i></button>';
|
|
|
|
html += '</div>';
|
|
|
|
html += '<div class="topLeftActionButtons">';
|
|
|
|
html += '<button is="paper-icon-button-light" class="autoSize bookplayerButton btnBookplayerToc hide-mouse-idle-tv" tabindex="-1"><i class="material-icons bookplayerButtonIcon toc"></i></button>';
|
|
|
|
html += '</div>';
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
elem.innerHTML = html;
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
dialogHelper.open(elem);
|
2020-05-16 22:02:52 +10:00
|
|
|
}
|
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
this._mediaElement = elem;
|
|
|
|
|
|
|
|
return elem;
|
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:42:58 +09:00
|
|
|
setCurrentSrc(elem, options) {
|
2020-05-28 18:39:49 +10:00
|
|
|
let item = options.items[0];
|
2020-05-29 19:13:02 +10:00
|
|
|
this._currentItem = item;
|
|
|
|
this.streamInfo = {
|
|
|
|
started: true,
|
|
|
|
ended: false,
|
|
|
|
mediaSource: {
|
|
|
|
Id: item.Id
|
|
|
|
}
|
|
|
|
};
|
2020-05-28 18:39:49 +10:00
|
|
|
|
|
|
|
let serverId = item.ServerId;
|
2020-05-22 10:42:58 +09:00
|
|
|
let apiClient = connectionManager.getApiClient(serverId);
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-28 18:45:28 +10:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
require(['epubjs'], (epubjs) => {
|
2020-05-28 18:39:49 +10:00
|
|
|
let downloadHref = apiClient.getItemDownloadUrl(item.Id);
|
2020-05-22 10:42:58 +09:00
|
|
|
let book = epubjs.default(downloadHref, {openAs: 'epub'});
|
|
|
|
let rendition = book.renderTo(elem, {width: '100%', height: '97%'});
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-28 18:45:28 +10:00
|
|
|
this._currentSrc = downloadHref;
|
|
|
|
this._rendition = rendition;
|
2020-05-29 19:13:02 +10:00
|
|
|
let cancellationToken = {
|
|
|
|
shouldCancel: false
|
|
|
|
};
|
|
|
|
this._cancellationToken = cancellationToken;
|
|
|
|
|
2020-05-28 18:45:28 +10:00
|
|
|
return rendition.display().then(() => {
|
2020-05-29 19:13:02 +10:00
|
|
|
let epubElem = document.querySelector('.epub-container');
|
|
|
|
epubElem.style.display = 'none';
|
|
|
|
|
2020-05-28 21:14:25 +10:00
|
|
|
this.bindEvents();
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-29 19:13:02 +10:00
|
|
|
return this._rendition.book.locations.generate(1024).then(() => {
|
|
|
|
if (cancellationToken.shouldCancel) {
|
|
|
|
return reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._loaded = true;
|
|
|
|
epubElem.style.display = 'block';
|
|
|
|
rendition.on('relocated', (locations) => {
|
|
|
|
this._progress = book.locations.percentageFromCfi(locations.start.cfi);
|
|
|
|
|
|
|
|
events.trigger(this, 'timeupdate');
|
|
|
|
});
|
|
|
|
|
|
|
|
loading.hide();
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
});
|
2020-05-28 18:45:28 +10:00
|
|
|
}, () => {
|
2020-05-22 10:42:58 +09:00
|
|
|
console.error('Failed to display epub');
|
|
|
|
return reject();
|
2020-05-16 22:02:52 +10:00
|
|
|
});
|
|
|
|
});
|
2020-05-22 10:42:58 +09:00
|
|
|
});
|
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:39:21 +09:00
|
|
|
canPlayMediaType(mediaType) {
|
2020-05-16 22:02:52 +10:00
|
|
|
return (mediaType || '').toLowerCase() === 'book';
|
2020-05-22 10:39:21 +09:00
|
|
|
}
|
2020-05-30 13:00:51 +02:00
|
|
|
|
|
|
|
canPlayItem(item) {
|
|
|
|
if (item.Path && (item.Path.endsWith('epub'))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-22 10:39:21 +09:00
|
|
|
}
|
2020-05-16 22:02:52 +10:00
|
|
|
|
2020-05-22 10:39:21 +09:00
|
|
|
export default BookPlayer;
|