openmedialibrary_reader/epub.js/js/plugins/hypothesis.js

81 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-12-01 15:40:31 +00:00
// Hypothesis Customized embedding
// This hypothesis config function returns a new constructor which modifies
// annotator for a better integration. Below we create our own EpubAnnotationSidebar
// Constructor, customizing the show and hide function to take acount for the reader UI.
2015-11-19 17:33:10 +00:00
window.hypothesisConfig = function() {
var Annotator = window.Annotator;
var $main = $("#main");
2015-12-01 15:40:31 +00:00
function EpubAnnotationSidebar(elem, options) {
2015-11-19 17:33:10 +00:00
options = {
server: true,
origin: true,
showHighlights: true,
Toolbar: {container: '#annotation-controls'}
}
Annotator.Host.call(this, elem, options);
}
2015-12-01 15:40:31 +00:00
EpubAnnotationSidebar.prototype = Object.create(Annotator.Host.prototype);
2015-11-19 17:33:10 +00:00
2015-12-01 15:40:31 +00:00
EpubAnnotationSidebar.prototype.show = function() {
2015-11-19 17:33:10 +00:00
this.frame.css({
'margin-left': (-1 * this.frame.width()) + "px"
});
this.frame.removeClass('annotator-collapsed');
if (!$main.hasClass('single')) {
$main.addClass("single");
this.toolbar.find('[name=sidebar-toggle]').removeClass('h-icon-chevron-left').addClass('h-icon-chevron-right');
this.setVisibleHighlights(true);
}
};
2015-12-01 15:40:31 +00:00
EpubAnnotationSidebar.prototype.hide = function() {
2015-11-19 17:33:10 +00:00
this.frame.css({
'margin-left': ''
});
this.frame.addClass('annotator-collapsed');
if ($main.hasClass('single')) {
$main.removeClass("single");
this.toolbar.find('[name=sidebar-toggle]').removeClass('h-icon-chevron-right').addClass('h-icon-chevron-left');
this.setVisibleHighlights(false);
}
};
return {
2015-12-01 15:40:31 +00:00
constructor: EpubAnnotationSidebar,
2015-11-19 17:33:10 +00:00
}
};
2015-12-01 15:40:31 +00:00
// This is the Epub.js plugin. Annotations are updated on location change.
2015-11-19 17:33:10 +00:00
EPUBJS.reader.plugins.HypothesisController = function (Book) {
2015-12-01 15:40:31 +00:00
var reader = this;
var $main = $("#main");
2015-11-19 17:33:10 +00:00
2015-12-01 15:40:31 +00:00
var updateAnnotations = function () {
var annotator = Book.renderer.render.window.annotator;
if (annotator && annotator.constructor.$) {
var annotations = getVisibleAnnotations(annotator.constructor.$);
annotator.showAnnotations(annotations)
}
};
2015-11-19 17:33:10 +00:00
2015-12-01 15:40:31 +00:00
var getVisibleAnnotations = function ($) {
var width = Book.renderer.render.iframe.clientWidth;
return $('.annotator-hl').map(function() {
var $this = $(this),
left = this.getBoundingClientRect().left;
2015-11-19 17:33:10 +00:00
2015-12-01 15:40:31 +00:00
if (left >= 0 && left <= width) {
return $this.data('annotation');
}
}).get();
};
2015-11-19 17:33:10 +00:00
2015-12-01 15:40:31 +00:00
Book.on("renderer:locationChanged", updateAnnotations);
2015-11-19 17:33:10 +00:00
2015-12-01 15:40:31 +00:00
return {}
2015-11-19 17:33:10 +00:00
};