Break dependencies between PresentationMode and other code, and add PresentationMode related utility methods to PDFViewer

This patch:
 - Adds a couple of utility methods to `PDFViewer` to enable checking `presentationModeState` without cumbersome comparisons.

 - Disentangles `PresentationMode` from `PDFHistory` and `HandTool`, by adding event listeners for `presentationmodechanged` to both of them.

 - Adds a utility function to `PDFViewerApplication` for requesting PresentationMode.

 - Prevents initialization of `PresentationMode` if the browser does not support the fullscreen API.
This commit is contained in:
Jonas Jenwald 2015-01-29 13:37:49 +01:00
parent 846eb967cc
commit 2dc1af8028
6 changed files with 83 additions and 63 deletions

View file

@ -174,7 +174,6 @@ var PDFViewerApplication = {
SecondaryToolbar.initialize({
toolbar: document.getElementById('secondaryToolbar'),
presentationMode: PresentationMode,
toggleButton: document.getElementById('secondaryToolbarToggle'),
presentationModeButton:
document.getElementById('secondaryPresentationMode'),
@ -190,14 +189,16 @@ var PDFViewerApplication = {
documentPropertiesButton: document.getElementById('documentProperties')
});
PresentationMode.initialize({
container: container,
secondaryToolbar: SecondaryToolbar,
firstPage: document.getElementById('contextFirstPage'),
lastPage: document.getElementById('contextLastPage'),
pageRotateCw: document.getElementById('contextPageRotateCw'),
pageRotateCcw: document.getElementById('contextPageRotateCcw')
});
if (this.supportsFullscreen) {
PresentationMode.initialize({
container: container,
secondaryToolbar: SecondaryToolbar,
firstPage: document.getElementById('contextFirstPage'),
lastPage: document.getElementById('contextLastPage'),
pageRotateCw: document.getElementById('contextPageRotateCw'),
pageRotateCcw: document.getElementById('contextPageRotateCcw')
});
}
PasswordPrompt.initialize({
overlayName: 'passwordOverlay',
@ -1334,6 +1335,13 @@ var PDFViewerApplication = {
this.pdfViewer.scrollPageIntoView(pageNumber);
},
requestPresentationMode: function pdfViewRequestPresentationMode() {
if (!this.supportsFullscreen) {
return;
}
PresentationMode.request();
},
/**
* This function flips the page in presentation mode if the user scrolls up
* or down with large enough motion and prevents page flipping too often.
@ -1976,7 +1984,7 @@ function handleMouseWheel(evt) {
evt.wheelDelta / MOUSE_WHEEL_DELTA_FACTOR;
var direction = (ticks < 0) ? 'zoomOut' : 'zoomIn';
if (PresentationMode.active) {
if (PDFViewerApplication.pdfViewer.isInPresentationMode) {
evt.preventDefault();
PDFViewerApplication.mouseScroll(ticks * MOUSE_WHEEL_DELTA_FACTOR);
} else if (evt.ctrlKey || evt.metaKey) {
@ -1990,9 +1998,9 @@ window.addEventListener('DOMMouseScroll', handleMouseWheel);
window.addEventListener('mousewheel', handleMouseWheel);
window.addEventListener('click', function click(evt) {
if (!PresentationMode.active) {
if (!PDFViewerApplication.pdfViewer.isInPresentationMode) {
if (SecondaryToolbar.opened &&
PDFViewerApplication.pdfViewer.containsElement(evt.target)) {
PDFViewerApplication.pdfViewer.containsElement(evt.target)) {
SecondaryToolbar.close();
}
} else if (evt.button === 0) {
@ -2013,15 +2021,13 @@ window.addEventListener('keydown', function keydown(evt) {
(evt.shiftKey ? 4 : 0) |
(evt.metaKey ? 8 : 0);
var pdfViewer = PDFViewerApplication.pdfViewer;
var isViewerInPresentationMode = pdfViewer && pdfViewer.isInPresentationMode;
// First, handle the key bindings that are independent whether an input
// control is selected or not.
if (cmd === 1 || cmd === 8 || cmd === 5 || cmd === 12) {
// either CTRL or META key with optional SHIFT.
var pdfViewer = PDFViewerApplication.pdfViewer;
var inPresentationMode = pdfViewer &&
(pdfViewer.presentationModeState === PresentationModeState.CHANGING ||
pdfViewer.presentationModeState === PresentationModeState.FULLSCREEN);
switch (evt.keyCode) {
case 70: // f
if (!PDFViewerApplication.supportsIntegratedFind) {
@ -2040,7 +2046,7 @@ window.addEventListener('keydown', function keydown(evt) {
case 107: // FF '+' and '='
case 187: // Chrome '+'
case 171: // FF with German keyboard
if (!inPresentationMode) {
if (!isViewerInPresentationMode) {
PDFViewerApplication.zoomIn();
}
handled = true;
@ -2048,14 +2054,14 @@ window.addEventListener('keydown', function keydown(evt) {
case 173: // FF/Mac '-'
case 109: // FF '-'
case 189: // Chrome '-'
if (!inPresentationMode) {
if (!isViewerInPresentationMode) {
PDFViewerApplication.zoomOut();
}
handled = true;
break;
case 48: // '0'
case 96: // '0' on Numpad of Swedish keyboard
if (!inPresentationMode) {
if (!isViewerInPresentationMode) {
// keeping it unhandled (to restore page zoom to 100%)
setTimeout(function () {
// ... and resetting the scale after browser adjusts its scale
@ -2083,7 +2089,7 @@ window.addEventListener('keydown', function keydown(evt) {
if (cmd === 3 || cmd === 10) {
switch (evt.keyCode) {
case 80: // p
SecondaryToolbar.presentationModeClick();
PDFViewerApplication.requestPresentationMode();
handled = true;
break;
case 71: // g
@ -2117,15 +2123,15 @@ window.addEventListener('keydown', function keydown(evt) {
case 38: // up arrow
case 33: // pg up
case 8: // backspace
if (!PresentationMode.active &&
PDFViewerApplication.currentScaleValue !== 'page-fit') {
if (!isViewerInPresentationMode &&
PDFViewerApplication.currentScaleValue !== 'page-fit') {
break;
}
/* in presentation mode */
/* falls through */
case 37: // left arrow
// horizontal scrolling using arrow keys
if (PDFViewerApplication.pdfViewer.isHorizontalScrollbarEnabled) {
if (pdfViewer.isHorizontalScrollbarEnabled) {
break;
}
/* falls through */
@ -2148,14 +2154,14 @@ window.addEventListener('keydown', function keydown(evt) {
case 40: // down arrow
case 34: // pg down
case 32: // spacebar
if (!PresentationMode.active &&
if (!isViewerInPresentationMode &&
PDFViewerApplication.currentScaleValue !== 'page-fit') {
break;
}
/* falls through */
case 39: // right arrow
// horizontal scrolling using arrow keys
if (PDFViewerApplication.pdfViewer.isHorizontalScrollbarEnabled) {
if (pdfViewer.isHorizontalScrollbarEnabled) {
break;
}
/* falls through */
@ -2166,13 +2172,13 @@ window.addEventListener('keydown', function keydown(evt) {
break;
case 36: // home
if (PresentationMode.active || PDFViewerApplication.page > 1) {
if (isViewerInPresentationMode || PDFViewerApplication.page > 1) {
PDFViewerApplication.page = 1;
handled = true;
}
break;
case 35: // end
if (PresentationMode.active || (PDFViewerApplication.pdfDocument &&
if (isViewerInPresentationMode || (PDFViewerApplication.pdfDocument &&
PDFViewerApplication.page < PDFViewerApplication.pagesCount)) {
PDFViewerApplication.page = PDFViewerApplication.pagesCount;
handled = true;
@ -2180,7 +2186,7 @@ window.addEventListener('keydown', function keydown(evt) {
break;
case 72: // 'h'
if (!PresentationMode.active) {
if (!isViewerInPresentationMode) {
HandTool.toggle();
}
break;
@ -2193,7 +2199,7 @@ window.addEventListener('keydown', function keydown(evt) {
if (cmd === 4) { // shift-key
switch (evt.keyCode) {
case 32: // spacebar
if (!PresentationMode.active &&
if (!isViewerInPresentationMode &&
PDFViewerApplication.currentScaleValue !== 'page-fit') {
break;
}
@ -2207,25 +2213,25 @@ window.addEventListener('keydown', function keydown(evt) {
}
}
if (!handled && !PresentationMode.active) {
if (!handled && !isViewerInPresentationMode) {
// 33=Page Up 34=Page Down 35=End 36=Home
// 37=Left 38=Up 39=Right 40=Down
if (evt.keyCode >= 33 && evt.keyCode <= 40 &&
!PDFViewerApplication.pdfViewer.containsElement(curElement)) {
!pdfViewer.containsElement(curElement)) {
// The page container is not focused, but a page navigation key has been
// pressed. Change the focus to the viewer container to make sure that
// navigation by keyboard works as expected.
PDFViewerApplication.pdfViewer.focus();
pdfViewer.focus();
}
// 32=Spacebar
if (evt.keyCode === 32 && curElementTagName !== 'BUTTON') {
//#if (FIREFOX || MOZCENTRAL)
// // Workaround for issue in Firefox, that prevents scroll keys from
// // working when elements with 'tabindex' are focused. (#3498)
// PDFViewerApplication.pdfViewer.blur();
// pdfViewer.blur();
//#else
if (!PDFViewerApplication.pdfViewer.containsElement(curElement)) {
PDFViewerApplication.pdfViewer.focus();
if (!pdfViewer.containsElement(curElement)) {
pdfViewer.focus();
}
//#endif
}
@ -2234,13 +2240,13 @@ window.addEventListener('keydown', function keydown(evt) {
if (cmd === 2) { // alt-key
switch (evt.keyCode) {
case 37: // left arrow
if (PresentationMode.active) {
if (isViewerInPresentationMode) {
PDFHistory.back();
handled = true;
}
break;
case 39: // right arrow
if (PresentationMode.active) {
if (isViewerInPresentationMode) {
PDFHistory.forward();
handled = true;
}