1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00

Pass through view listeners

This commit is contained in:
fchasen 2015-06-28 17:48:53 -04:00
parent 23d88d2e99
commit 4e566dde12
5 changed files with 157 additions and 79 deletions

79
dist/epub.js vendored
View file

@ -3869,6 +3869,9 @@ EPUBJS.View = function(section, options) {
this.element.style.display = "block"; this.element.style.display = "block";
} }
// Dom events to listen for
this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
}; };
EPUBJS.View.prototype.create = function() { EPUBJS.View.prototype.create = function() {
@ -4230,14 +4233,24 @@ EPUBJS.View.prototype.listeners = function() {
this.mediaQueryListeners(); this.mediaQueryListeners();
this.resizeListenters(); // this.resizeListenters();
this.addEventListeners();
this.addSelectionListeners();
};
EPUBJS.View.prototype.removeListeners = function() {
this.removeEventListeners();
this.removeSelectionListeners();
}; };
EPUBJS.View.prototype.resizeListenters = function() { EPUBJS.View.prototype.resizeListenters = function() {
// Test size again // Test size again
clearTimeout(this.expanding); clearTimeout(this.expanding);
// this.expanding = setTimeout(this.expand.bind(this), 350); this.expanding = setTimeout(this.expand.bind(this), 350);
}; };
//https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js //https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js
@ -4370,6 +4383,8 @@ EPUBJS.View.prototype.destroy = function() {
} }
if(this.displayed){ if(this.displayed){
this.removeListeners();
this.stopExpanding = true; this.stopExpanding = true;
this.element.removeChild(this.iframe); this.element.removeChild(this.iframe);
this.displayed = false; this.displayed = false;
@ -4520,6 +4535,55 @@ EPUBJS.View.prototype.addScript = function(src) {
}.bind(this)); }.bind(this));
}; };
EPUBJS.View.prototype.addEventListeners = function(){
if(!this.document) {
return;
}
this.listenedEvents.forEach(function(eventName){
this.document.addEventListener(eventName, this.triggerEvent.bind(this), false);
}, this);
};
EPUBJS.View.prototype.removeEventListeners = function(){
if(!this.document) {
return;
}
this.listenedEvents.forEach(function(eventName){
this.document.removeEventListener(eventName, this.triggerEvent, false);
}, this);
};
// Pass browser events
EPUBJS.View.prototype.triggerEvent = function(e){
this.trigger(e.type, e);
};
EPUBJS.View.prototype.addSelectionListeners = function(){
if(!this.document) {
return;
}
this.document.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
};
EPUBJS.View.prototype.removeSelectionListeners = function(){
if(!this.document) {
return;
}
this.document.removeEventListener("selectionchange", this.onSelectionChange, false);
};
EPUBJS.View.prototype.onSelectionChange = function(e){
if (this.selectionEndTimeout) {
clearTimeout(this.selectionEndTimeout);
}
this.selectionEndTimeout = setTimeout(function() {
this.selectedRange = this.window.getSelection();
this.trigger("selected", this.selectedRange);
}.bind(this), 500);
};
RSVP.EventTarget.mixin(EPUBJS.View.prototype); RSVP.EventTarget.mixin(EPUBJS.View.prototype);
EPUBJS.Views = function(container) { EPUBJS.Views = function(container) {
@ -4859,6 +4923,7 @@ EPUBJS.Rendition = function(book, options) {
this.hooks.show = new EPUBJS.Hook(this); this.hooks.show = new EPUBJS.Hook(this);
this.hooks.content.register(EPUBJS.replace.links.bind(this)); this.hooks.content.register(EPUBJS.replace.links.bind(this));
this.hooks.content.register(this.passViewEvents.bind(this));
// this.hooks.display.register(this.afterDisplay.bind(this)); // this.hooks.display.register(this.afterDisplay.bind(this));
@ -5429,6 +5494,16 @@ EPUBJS.Rendition.prototype.scrollTo = function(x, y, silent){
// }; // };
}; };
EPUBJS.Rendition.prototype.passViewEvents = function(view){
view.listenedEvents.forEach(function(e){
view.on(e, this.triggerViewEvent.bind(this));
}.bind(this));
};
EPUBJS.Rendition.prototype.triggerViewEvent = function(e){
this.trigger(e.type, e);
};
//-- Enable binding events to Renderer //-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype); RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype);

6
dist/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1,72 +0,0 @@
/*
EPUBJS.Renderer.prototype.listeners = function(){
// Dom events to listen for
this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click"];
this.upEvent = "mouseup";
this.downEvent = "mousedown";
if('ontouchstart' in document.documentElement) {
this.listenedEvents.push("touchstart", "touchend");
this.upEvent = "touchend";
this.downEvent = "touchstart";
}
// Resize events
// this.resized = _.debounce(this.onResized.bind(this), 100);
};
//-- Listeners for events in the frame
EPUBJS.Renderer.prototype.onResized = function(e) {
var width = this.container.clientWidth;
var height = this.container.clientHeight;
this.resize(width, height, false);
};
EPUBJS.Renderer.prototype.addEventListeners = function(){
if(!this.render.document) {
return;
}
this.listenedEvents.forEach(function(eventName){
this.render.document.addEventListener(eventName, this.triggerEvent.bind(this), false);
}, this);
};
EPUBJS.Renderer.prototype.removeEventListeners = function(){
if(!this.render.document) {
return;
}
this.listenedEvents.forEach(function(eventName){
this.render.document.removeEventListener(eventName, this.triggerEvent, false);
}, this);
};
// Pass browser events
EPUBJS.Renderer.prototype.triggerEvent = function(e){
this.trigger("renderer:"+e.type, e);
};
EPUBJS.Renderer.prototype.addSelectionListeners = function(){
this.render.document.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
};
EPUBJS.Renderer.prototype.removeSelectionListeners = function(){
if(!this.render.document) {
return;
}
this.doc.removeEventListener("selectionchange", this.onSelectionChange, false);
};
EPUBJS.Renderer.prototype.onSelectionChange = function(e){
if (this.selectionEndTimeout) {
clearTimeout(this.selectionEndTimeout);
}
this.selectionEndTimeout = setTimeout(function() {
this.selectedRange = this.render.window.getSelection();
this.trigger("renderer:selected", this.selectedRange);
}.bind(this), 500);
};
*/

View file

@ -26,6 +26,7 @@ EPUBJS.Rendition = function(book, options) {
this.hooks.show = new EPUBJS.Hook(this); this.hooks.show = new EPUBJS.Hook(this);
this.hooks.content.register(EPUBJS.replace.links.bind(this)); this.hooks.content.register(EPUBJS.replace.links.bind(this));
this.hooks.content.register(this.passViewEvents.bind(this));
// this.hooks.display.register(this.afterDisplay.bind(this)); // this.hooks.display.register(this.afterDisplay.bind(this));
@ -596,5 +597,15 @@ EPUBJS.Rendition.prototype.scrollTo = function(x, y, silent){
// }; // };
}; };
EPUBJS.Rendition.prototype.passViewEvents = function(view){
view.listenedEvents.forEach(function(e){
view.on(e, this.triggerViewEvent.bind(this));
}.bind(this));
};
EPUBJS.Rendition.prototype.triggerViewEvent = function(e){
this.trigger(e.type, e);
};
//-- Enable binding events to Renderer //-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype); RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype);

View file

@ -30,6 +30,9 @@ EPUBJS.View = function(section, options) {
this.element.style.display = "block"; this.element.style.display = "block";
} }
// Dom events to listen for
this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
}; };
EPUBJS.View.prototype.create = function() { EPUBJS.View.prototype.create = function() {
@ -391,14 +394,24 @@ EPUBJS.View.prototype.listeners = function() {
this.mediaQueryListeners(); this.mediaQueryListeners();
this.resizeListenters(); // this.resizeListenters();
this.addEventListeners();
this.addSelectionListeners();
};
EPUBJS.View.prototype.removeListeners = function() {
this.removeEventListeners();
this.removeSelectionListeners();
}; };
EPUBJS.View.prototype.resizeListenters = function() { EPUBJS.View.prototype.resizeListenters = function() {
// Test size again // Test size again
clearTimeout(this.expanding); clearTimeout(this.expanding);
// this.expanding = setTimeout(this.expand.bind(this), 350); this.expanding = setTimeout(this.expand.bind(this), 350);
}; };
//https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js //https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js
@ -531,6 +544,8 @@ EPUBJS.View.prototype.destroy = function() {
} }
if(this.displayed){ if(this.displayed){
this.removeListeners();
this.stopExpanding = true; this.stopExpanding = true;
this.element.removeChild(this.iframe); this.element.removeChild(this.iframe);
this.displayed = false; this.displayed = false;
@ -681,4 +696,53 @@ EPUBJS.View.prototype.addScript = function(src) {
}.bind(this)); }.bind(this));
}; };
EPUBJS.View.prototype.addEventListeners = function(){
if(!this.document) {
return;
}
this.listenedEvents.forEach(function(eventName){
this.document.addEventListener(eventName, this.triggerEvent.bind(this), false);
}, this);
};
EPUBJS.View.prototype.removeEventListeners = function(){
if(!this.document) {
return;
}
this.listenedEvents.forEach(function(eventName){
this.document.removeEventListener(eventName, this.triggerEvent, false);
}, this);
};
// Pass browser events
EPUBJS.View.prototype.triggerEvent = function(e){
this.trigger(e.type, e);
};
EPUBJS.View.prototype.addSelectionListeners = function(){
if(!this.document) {
return;
}
this.document.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
};
EPUBJS.View.prototype.removeSelectionListeners = function(){
if(!this.document) {
return;
}
this.document.removeEventListener("selectionchange", this.onSelectionChange, false);
};
EPUBJS.View.prototype.onSelectionChange = function(e){
if (this.selectionEndTimeout) {
clearTimeout(this.selectionEndTimeout);
}
this.selectionEndTimeout = setTimeout(function() {
this.selectedRange = this.window.getSelection();
this.trigger("selected", this.selectedRange);
}.bind(this), 500);
};
RSVP.EventTarget.mixin(EPUBJS.View.prototype); RSVP.EventTarget.mixin(EPUBJS.View.prototype);