Merge pull request #3763 from Snuffleupagus/refactor-PresentationMode

Refactor Presentation Mode code
This commit is contained in:
Tim van der Meij 2013-10-06 11:22:08 -07:00
commit d4aa36257c
2 changed files with 56 additions and 58 deletions

View File

@ -18,32 +18,37 @@
'use strict'; 'use strict';
var DELAY_BEFORE_HIDING_CONTROLS = 3000; // in ms
var SELECTOR = 'presentationControls';
var PresentationMode = { var PresentationMode = {
active: false, active: false,
args: null, args: null,
request: function presentationModeRequest() { initialize: function presentationModeInitialize(options) {
if (!PDFView.supportsFullscreen) { this.container = options.container;
return false; },
}
var isPresentationMode = document.fullscreenElement || get isFullscreen() {
return (document.fullscreenElement ||
document.mozFullScreen || document.mozFullScreen ||
document.webkitIsFullScreen || document.webkitIsFullScreen ||
document.msFullscreenElement; document.msFullscreenElement);
},
if (isPresentationMode) { request: function presentationModeRequest() {
if (!PDFView.supportsFullscreen || this.isFullscreen) {
return false; return false;
} }
var wrapper = document.getElementById('viewerContainer'); if (this.container.requestFullscreen) {
if (document.documentElement.requestFullscreen) { this.container.requestFullscreen();
wrapper.requestFullscreen(); } else if (this.container.mozRequestFullScreen) {
} else if (document.documentElement.mozRequestFullScreen) { this.container.mozRequestFullScreen();
wrapper.mozRequestFullScreen(); } else if (this.container.webkitRequestFullScreen) {
} else if (document.documentElement.webkitRequestFullScreen) { this.container.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
wrapper.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT); } else if (this.container.msRequestFullscreen) {
} else if (document.documentElement.msRequestFullscreen) { this.container.msRequestFullscreen();
wrapper.msRequestFullscreen();
} else { } else {
return false; return false;
} }
@ -62,10 +67,11 @@ var PresentationMode = {
PDFView.page = this.args.page; PDFView.page = this.args.page;
PDFView.parseScale('page-fit', true); PDFView.parseScale('page-fit', true);
this.showControls(); window.addEventListener('mousemove', this.mouseMove, false);
window.addEventListener('mousedown', this.mouseDown, false);
var viewer = document.getElementById('viewer'); this.showControls();
viewer.setAttribute('contextmenu', 'viewerContextMenu'); this.container.setAttribute('contextmenu', 'viewerContextMenu');
}, },
exit: function presentationModeExit() { exit: function presentationModeExit() {
@ -75,12 +81,13 @@ var PresentationMode = {
PDFView.parseScale(this.args.previousScale); PDFView.parseScale(this.args.previousScale);
PDFView.page = page; PDFView.page = page;
window.removeEventListener('mousemove', this.mouseMove, false);
window.removeEventListener('mousedown', this.mouseDown, false);
this.hideControls(); this.hideControls();
this.args = null; this.args = null;
PDFView.clearMouseScrollState(); PDFView.clearMouseScrollState();
this.container.removeAttribute('contextmenu');
var viewer = document.getElementById('viewer');
viewer.removeAttribute('contextmenu');
// Ensure that the thumbnail of the current page is visible // Ensure that the thumbnail of the current page is visible
// when exiting presentation mode. // when exiting presentation mode.
@ -88,15 +95,13 @@ var PresentationMode = {
}, },
showControls: function presentationModeShowControls() { showControls: function presentationModeShowControls() {
var DELAY_BEFORE_HIDING_CONTROLS = 3000;
var wrapper = document.getElementById('viewerContainer');
if (this.controlsTimeout) { if (this.controlsTimeout) {
clearTimeout(this.controlsTimeout); clearTimeout(this.controlsTimeout);
} else { } else {
wrapper.classList.add('presentationControls'); this.container.classList.add(SELECTOR);
} }
this.controlsTimeout = setTimeout(function hideControlsTimeout() { this.controlsTimeout = setTimeout(function hideControlsTimeout() {
wrapper.classList.remove('presentationControls'); this.container.classList.remove(SELECTOR);
delete this.controlsTimeout; delete this.controlsTimeout;
}.bind(this), DELAY_BEFORE_HIDING_CONTROLS); }.bind(this), DELAY_BEFORE_HIDING_CONTROLS);
}, },
@ -105,44 +110,33 @@ var PresentationMode = {
if (!this.controlsTimeout) { if (!this.controlsTimeout) {
return; return;
} }
this.container.classList.remove(SELECTOR);
clearTimeout(this.controlsTimeout); clearTimeout(this.controlsTimeout);
delete this.controlsTimeout; delete this.controlsTimeout;
},
var wrapper = document.getElementById('viewerContainer'); mouseMove: function presentationModeMouseMove(evt) {
wrapper.classList.remove('presentationControls'); PresentationMode.showControls();
},
mouseDown: function presentationModeMouseDown(evt) {
if (evt.button === 0) {
// Enable clicking of links in presentation mode. Please note:
// Only links pointing to destinations in the current PDF document work.
var isInternalLink = (evt.target.href &&
evt.target.classList.contains('internalLink'));
if (!isInternalLink) {
// Unless an internal link was clicked, advance one page.
evt.preventDefault();
PDFView.page += (evt.shiftKey ? -1 : 1);
}
}
} }
}; };
window.addEventListener('mousemove', function mousemove(evt) {
if (PresentationMode.active) {
PresentationMode.showControls();
}
}, false);
window.addEventListener('mousedown', function mousedown(evt) {
if (PresentationMode.active && evt.button === 0) {
// Enable clicking of links in presentation mode.
// Note: Only links that point to the currently loaded PDF document works.
var targetHref = evt.target.href;
var internalLink = targetHref && (targetHref.replace(/#.*$/, '') ===
window.location.href.replace(/#.*$/, ''));
if (!internalLink) {
// Unless an internal link was clicked, advance a page in presentation
// mode.
evt.preventDefault();
PDFView.page++;
}
}
}, false);
(function presentationModeClosure() { (function presentationModeClosure() {
function presentationModeChange(e) { function presentationModeChange(e) {
var isPresentationMode = document.fullscreenElement || if (PresentationMode.isFullscreen) {
document.mozFullScreen ||
document.webkitIsFullScreen ||
document.msFullscreenElement;
if (isPresentationMode) {
PresentationMode.enter(); PresentationMode.enter();
} else { } else {
PresentationMode.exit(); PresentationMode.exit();

View File

@ -147,6 +147,10 @@ var PDFView = {
passwordCancel: document.getElementById('passwordCancel') passwordCancel: document.getElementById('passwordCancel')
}); });
PresentationMode.initialize({
container: container
});
this.initialized = true; this.initialized = true;
container.addEventListener('scroll', function() { container.addEventListener('scroll', function() {
self.lastScroll = Date.now(); self.lastScroll = Date.now();