Merge pull request #3752 from Snuffleupagus/move-PresentationMode

Move Presentation Mode related code from viewer.js to its own file
This commit is contained in:
Tim van der Meij 2013-10-02 06:30:51 -07:00
commit fc5ed402fb
6 changed files with 198 additions and 170 deletions

View File

@ -15,9 +15,9 @@
* limitations under the License.
*/
/* globals RenderingStates, PDFView, PDFHistory, PDFFindBar, PDFJS, mozL10n,
CustomStyle, scrollIntoView, SCROLLBAR_PADDING, CSS_UNITS,
UNKNOWN_SCALE, DEFAULT_SCALE, getOutputScale, TextLayerBuilder,
cache, Stats */
CustomStyle, PresentationMode, scrollIntoView, SCROLLBAR_PADDING,
CSS_UNITS, UNKNOWN_SCALE, DEFAULT_SCALE, getOutputScale,
TextLayerBuilder, cache, Stats */
'use strict';
@ -238,7 +238,7 @@ var PageView = function pageView(container, id, scale,
};
this.scrollIntoView = function pageViewScrollIntoView(dest) {
if (PDFView.isPresentationMode) { // Avoid breaking presentation mode.
if (PresentationMode.active) { // Avoid breaking presentation mode.
dest = null;
}
if (!dest) {
@ -379,7 +379,7 @@ var PageView = function pageView(container, id, scale,
pageIndex: this.id - 1,
lastScrollSource: PDFView,
viewport: this.viewport,
isViewerInPresentationMode: PDFView.isPresentationMode
isViewerInPresentationMode: PresentationMode.active
}) : null;
// TODO(mack): use data attributes to store these
ctx._scaleX = outputScale.sx;

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFJS, PDFView */
/* globals PDFJS, PDFView, PresentationMode */
'use strict';
@ -264,7 +264,7 @@ var PDFHistory = {
return null;
}
var params = { hash: this.currentBookmark, page: this.currentPage };
if (PDFView.isPresentationMode) {
if (PresentationMode.active) {
params.hash = null;
}
return params;

157
web/presentation_mode.js Normal file
View File

@ -0,0 +1,157 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFView, scrollIntoView */
'use strict';
var PresentationMode = {
active: false,
args: null,
request: function presentationModeRequest() {
if (!PDFView.supportsFullscreen) {
return false;
}
var isPresentationMode = document.fullscreenElement ||
document.mozFullScreen ||
document.webkitIsFullScreen ||
document.msFullscreenElement;
if (isPresentationMode) {
return false;
}
var wrapper = document.getElementById('viewerContainer');
if (document.documentElement.requestFullscreen) {
wrapper.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
wrapper.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullScreen) {
wrapper.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
} else if (document.documentElement.msRequestFullscreen) {
wrapper.msRequestFullscreen();
} else {
return false;
}
this.args = {
page: PDFView.page,
previousScale: PDFView.currentScaleValue
};
return true;
},
enter: function presentationModeEnter() {
this.active = true;
PDFView.page = this.args.page;
PDFView.parseScale('page-fit', true);
this.showControls();
var viewer = document.getElementById('viewer');
viewer.setAttribute('contextmenu', 'viewerContextMenu');
},
exit: function presentationModeExit() {
this.active = false;
var page = PDFView.page;
PDFView.parseScale(this.args.previousScale);
PDFView.page = page;
this.hideControls();
this.args = null;
PDFView.clearMouseScrollState();
var viewer = document.getElementById('viewer');
viewer.removeAttribute('contextmenu');
// Ensure that the thumbnail of the current page is visible
// when exiting presentation mode.
scrollIntoView(document.getElementById('thumbnailContainer' + page));
},
showControls: function presentationModeShowControls() {
var DELAY_BEFORE_HIDING_CONTROLS = 3000;
var wrapper = document.getElementById('viewerContainer');
if (this.controlsTimeout) {
clearTimeout(this.controlsTimeout);
} else {
wrapper.classList.add('presentationControls');
}
this.controlsTimeout = setTimeout(function hideControlsTimeout() {
wrapper.classList.remove('presentationControls');
delete this.controlsTimeout;
}.bind(this), DELAY_BEFORE_HIDING_CONTROLS);
},
hideControls: function presentationModeHideControls() {
if (!this.controlsTimeout) {
return;
}
clearTimeout(this.controlsTimeout);
delete this.controlsTimeout;
var wrapper = document.getElementById('viewerContainer');
wrapper.classList.remove('presentationControls');
}
};
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 presentationModeChange(e) {
var isPresentationMode = document.fullscreenElement ||
document.mozFullScreen ||
document.webkitIsFullScreen ||
document.msFullscreenElement;
if (isPresentationMode) {
PresentationMode.enter();
} else {
PresentationMode.exit();
}
}
window.addEventListener('fullscreenchange', presentationModeChange, false);
window.addEventListener('mozfullscreenchange', presentationModeChange, false);
window.addEventListener('webkitfullscreenchange', presentationModeChange,
false);
window.addEventListener('MSFullscreenChange', presentationModeChange, false);
})();

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFView, SCROLLBAR_PADDING */
/* globals PDFView, PresentationMode, SCROLLBAR_PADDING */
'use strict';
@ -59,7 +59,7 @@ var SecondaryToolbar = {
// Event handling functions.
presentationModeClick: function secondaryToolbarPresentationModeClick(evt) {
PDFView.presentationMode();
PresentationMode.request();
this.close();
},

View File

@ -68,6 +68,7 @@ limitations under the License.
<script type="text/javascript" src="pdf_history.js"></script>
<script type="text/javascript" src="secondary_toolbar.js"></script>
<script type="text/javascript" src="password_prompt.js"></script>
<script type="text/javascript" src="presentation_mode.js"></script>
<!--#endif-->
<script type="text/javascript" src="debugger.js"></script>

View File

@ -18,7 +18,7 @@
PDFFindController, ProgressBar, TextLayerBuilder, DownloadManager,
getFileName, getOutputScale, scrollIntoView, getPDFFileNameFromURL,
PDFHistory, PageView, ThumbnailView, noContextMenuHandler,
SecondaryToolbar, PasswordPrompt */
SecondaryToolbar, PasswordPrompt, PresentationMode */
'use strict';
@ -168,6 +168,7 @@ var currentPageNumber = 1;
//#include pdf_history.js
//#include secondary_toolbar.js
//#include password_prompt.js
//#include presentation_mode.js
var PDFView = {
pages: [],
@ -183,8 +184,6 @@ var PDFView = {
sidebarOpen: false,
pageViewScroll: null,
thumbnailViewScroll: null,
isPresentationMode: false,
presentationModeArgs: null,
pageRotation: 0,
mouseScrollTimeStamp: 0,
mouseScrollDelta: 0,
@ -205,6 +204,23 @@ var PDFView = {
this.watchScroll(thumbnailContainer, this.thumbnailViewScroll,
this.renderHighestPriority.bind(this));
PDFFindBar.initialize({
bar: document.getElementById('findbar'),
toggleButton: document.getElementById('viewFind'),
findField: document.getElementById('findInput'),
highlightAllCheckbox: document.getElementById('findHighlightAll'),
caseSensitiveCheckbox: document.getElementById('findMatchCase'),
findMsg: document.getElementById('findMsg'),
findStatusIcon: document.getElementById('findStatusIcon'),
findPreviousButton: document.getElementById('findPrevious'),
findNextButton: document.getElementById('findNext')
});
PDFFindController.initialize({
pdfPageSource: this,
integratedFind: this.supportsIntegratedFind
});
SecondaryToolbar.initialize({
toolbar: document.getElementById('secondaryToolbar'),
toggleButton: document.getElementById('secondaryToolbarToggle'),
@ -226,23 +242,6 @@ var PDFView = {
passwordCancel: document.getElementById('passwordCancel')
});
PDFFindBar.initialize({
bar: document.getElementById('findbar'),
toggleButton: document.getElementById('viewFind'),
findField: document.getElementById('findInput'),
highlightAllCheckbox: document.getElementById('findHighlightAll'),
caseSensitiveCheckbox: document.getElementById('findMatchCase'),
findMsg: document.getElementById('findMsg'),
findStatusIcon: document.getElementById('findStatusIcon'),
findPreviousButton: document.getElementById('findPrevious'),
findNextButton: document.getElementById('findNext')
});
PDFFindController.initialize({
pdfPageSource: this,
integratedFind: this.supportsIntegratedFind
});
this.initialized = true;
container.addEventListener('scroll', function() {
self.lastScroll = Date.now();
@ -1283,7 +1282,7 @@ var PDFView = {
getVisiblePages: function pdfViewGetVisiblePages() {
return this.getVisibleElements(this.container, this.pages,
!this.isPresentationMode);
!PresentationMode.active);
},
getVisibleThumbs: function pdfViewGetVisibleThumbs() {
@ -1389,91 +1388,6 @@ var PDFView = {
div.removeChild(div.lastChild);
},
presentationMode: function pdfViewPresentationMode() {
if (!this.supportsFullscreen) {
return false;
}
var isPresentationMode = document.fullscreenElement ||
document.mozFullScreen ||
document.webkitIsFullScreen ||
document.msFullscreenElement;
if (isPresentationMode) {
return false;
}
var wrapper = document.getElementById('viewerContainer');
if (document.documentElement.requestFullscreen) {
wrapper.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
wrapper.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullScreen) {
wrapper.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
} else if (document.documentElement.msRequestFullscreen) {
wrapper.msRequestFullscreen();
} else {
return false;
}
this.presentationModeArgs = {
page: this.page,
previousScale: this.currentScaleValue
};
return true;
},
enterPresentationMode: function pdfViewEnterPresentationMode() {
this.isPresentationMode = true;
this.page = this.presentationModeArgs.page;
this.parseScale('page-fit', true);
this.showPresentationControls();
var viewer = document.getElementById('viewer');
viewer.setAttribute('contextmenu', 'viewerContextMenu');
},
exitPresentationMode: function pdfViewExitPresentationMode() {
this.isPresentationMode = false;
this.parseScale(this.presentationModeArgs.previousScale);
this.page = this.page;
this.clearMouseScrollState();
this.hidePresentationControls();
this.presentationModeArgs = null;
var viewer = document.getElementById('viewer');
viewer.removeAttribute('contextmenu');
// Ensure that the thumbnail of the current page is visible
// when exiting presentation mode.
scrollIntoView(document.getElementById('thumbnailContainer' + this.page));
},
showPresentationControls: function pdfViewShowPresentationControls() {
var DELAY_BEFORE_HIDING_CONTROLS = 3000;
var wrapper = document.getElementById('viewerContainer');
if (this.presentationControlsTimeout) {
clearTimeout(this.presentationControlsTimeout);
} else {
wrapper.classList.add('presentationControls');
}
this.presentationControlsTimeout = setTimeout(function hideControls() {
wrapper.classList.remove('presentationControls');
delete PDFView.presentationControlsTimeout;
}, DELAY_BEFORE_HIDING_CONTROLS);
},
hidePresentationControls: function pdfViewShowPresentationControls() {
if (!this.presentationControlsTimeout) {
return;
}
clearTimeout(this.presentationControlsTimeout);
delete this.presentationControlsTimeout;
var wrapper = document.getElementById('viewerContainer');
wrapper.classList.remove('presentationControls');
},
rotatePages: function pdfViewPageRotation(delta) {
this.pageRotation = (this.pageRotation + 360 + delta) % 360;
@ -2051,36 +1965,14 @@ window.addEventListener('DOMMouseScroll', function(evt) {
var ticks = evt.detail;
var direction = (ticks > 0) ? 'zoomOut' : 'zoomIn';
PDFView[direction](Math.abs(ticks));
} else if (PDFView.isPresentationMode) {
} else if (PresentationMode.active) {
var FIREFOX_DELTA_FACTOR = -40;
PDFView.mouseScroll(evt.detail * FIREFOX_DELTA_FACTOR);
}
}, false);
window.addEventListener('mousemove', function mousemove(evt) {
if (PDFView.isPresentationMode) {
PDFView.showPresentationControls();
}
}, false);
window.addEventListener('mousedown', function mousedown(evt) {
if (PDFView.isPresentationMode && 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);
window.addEventListener('click', function click(evt) {
if (!PDFView.isPresentationMode) {
if (!PresentationMode.active) {
if (SecondaryToolbar.isOpen && PDFView.container.contains(evt.target)) {
SecondaryToolbar.close();
}
@ -2148,8 +2040,7 @@ window.addEventListener('keydown', function keydown(evt) {
if (cmd === 3 || cmd === 10) {
switch (evt.keyCode) {
case 80: // p
PDFView.presentationMode();
SecondaryToolbar.close();
SecondaryToolbar.presentationModeClick();
handled = true;
break;
}
@ -2173,7 +2064,7 @@ window.addEventListener('keydown', function keydown(evt) {
}
var controlsElement = document.getElementById('toolbar');
while (curElement) {
if (curElement === controlsElement && !PDFView.isPresentationMode)
if (curElement === controlsElement && !PresentationMode.active)
return; // ignoring if the 'toolbar' element is focused
curElement = curElement.parentNode;
}
@ -2183,7 +2074,7 @@ window.addEventListener('keydown', function keydown(evt) {
case 38: // up arrow
case 33: // pg up
case 8: // backspace
if (!PDFView.isPresentationMode &&
if (!PresentationMode.active &&
PDFView.currentScaleValue !== 'page-fit') {
break;
}
@ -2213,7 +2104,7 @@ window.addEventListener('keydown', function keydown(evt) {
case 40: // down arrow
case 34: // pg down
case 32: // spacebar
if (!PDFView.isPresentationMode &&
if (!PresentationMode.active &&
PDFView.currentScaleValue !== 'page-fit') {
break;
}
@ -2231,13 +2122,13 @@ window.addEventListener('keydown', function keydown(evt) {
break;
case 36: // home
if (PDFView.isPresentationMode) {
if (PresentationMode.active) {
PDFView.page = 1;
handled = true;
}
break;
case 35: // end
if (PDFView.isPresentationMode) {
if (PresentationMode.active) {
PDFView.page = PDFView.pdfDocument.numPages;
handled = true;
}
@ -2252,7 +2143,7 @@ window.addEventListener('keydown', function keydown(evt) {
if (cmd === 4) { // shift-key
switch (evt.keyCode) {
case 32: // spacebar
if (!PDFView.isPresentationMode &&
if (!PresentationMode.active &&
PDFView.currentScaleValue !== 'page-fit') {
break;
}
@ -2269,13 +2160,13 @@ window.addEventListener('keydown', function keydown(evt) {
if (cmd === 2) { // alt-key
switch (evt.keyCode) {
case 37: // left arrow
if (PDFView.isPresentationMode) {
if (PresentationMode.active) {
PDFHistory.back();
handled = true;
}
break;
case 39: // right arrow
if (PDFView.isPresentationMode) {
if (PresentationMode.active) {
PDFHistory.forward();
handled = true;
}
@ -2297,27 +2188,6 @@ window.addEventListener('afterprint', function afterPrint(evt) {
PDFView.afterPrint();
});
(function presentationModeClosure() {
function presentationModeChange(e) {
var isPresentationMode = document.fullscreenElement ||
document.mozFullScreen ||
document.webkitIsFullScreen ||
document.msFullscreenElement;
if (isPresentationMode) {
PDFView.enterPresentationMode();
} else {
PDFView.exitPresentationMode();
}
}
window.addEventListener('fullscreenchange', presentationModeChange, false);
window.addEventListener('mozfullscreenchange', presentationModeChange, false);
window.addEventListener('webkitfullscreenchange', presentationModeChange,
false);
window.addEventListener('MSFullscreenChange', presentationModeChange, false);
})();
(function animationStartedClosure() {
// The offsetParent is not set until the pdf.js iframe or object is visible.
// Waiting for first animation.