Browse Source

Convert presentation mode to ES6 syntax

Tim van der Meij 8 years ago
parent
commit
ccdc7ba3c8
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
  1. 194
      web/pdf_presentation_mode.js

194
web/pdf_presentation_mode.js

@ -15,10 +15,19 @@ @@ -15,10 +15,19 @@
import { normalizeWheelEventDelta } from './ui_utils';
var DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS = 1500; // in ms
var DELAY_BEFORE_HIDING_CONTROLS = 3000; // in ms
var ACTIVE_SELECTOR = 'pdfPresentationMode';
var CONTROLS_SELECTOR = 'pdfPresentationModeControls';
const DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS = 1500; // in ms
const DELAY_BEFORE_HIDING_CONTROLS = 3000; // in ms
const ACTIVE_SELECTOR = 'pdfPresentationMode';
const CONTROLS_SELECTOR = 'pdfPresentationModeControls';
const MOUSE_SCROLL_COOLDOWN_TIME = 50; // in ms
const PAGE_SWITCH_THRESHOLD = 0.1;
// Number of CSS pixels for a movement to count as a swipe.
const SWIPE_MIN_DISTANCE_THRESHOLD = 50;
// Swipe angle deviation from the x or y axis before it is not
// considered a swipe in that direction any more.
const SWIPE_ANGLE_THRESHOLD = Math.PI / 6;
/**
* @typedef {Object} PDFPresentationModeOptions
@ -30,15 +39,11 @@ var CONTROLS_SELECTOR = 'pdfPresentationModeControls'; @@ -30,15 +39,11 @@ var CONTROLS_SELECTOR = 'pdfPresentationModeControls';
* to the context menu in Presentation Mode.
*/
class PDFPresentationMode {
/**
* @class
*/
var PDFPresentationMode = (function PDFPresentationModeClosure() {
/**
* @constructs PDFPresentationMode
* @param {PDFPresentationModeOptions} options
*/
function PDFPresentationMode(options) {
constructor(options) {
this.container = options.container;
this.viewer = options.viewer || options.container.firstElementChild;
this.pdfViewer = options.pdfViewer;
@ -53,37 +58,31 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -53,37 +58,31 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
this.touchSwipeState = null;
if (contextMenuItems) {
contextMenuItems.contextFirstPage.addEventListener('click',
function PDFPresentationMode_contextFirstPageClick(e) {
contextMenuItems.contextFirstPage.addEventListener('click', () => {
this.contextMenuOpen = false;
this.eventBus.dispatch('firstpage');
}.bind(this));
contextMenuItems.contextLastPage.addEventListener('click',
function PDFPresentationMode_contextLastPageClick(e) {
});
contextMenuItems.contextLastPage.addEventListener('click', () => {
this.contextMenuOpen = false;
this.eventBus.dispatch('lastpage');
}.bind(this));
contextMenuItems.contextPageRotateCw.addEventListener('click',
function PDFPresentationMode_contextPageRotateCwClick(e) {
});
contextMenuItems.contextPageRotateCw.addEventListener('click', () => {
this.contextMenuOpen = false;
this.eventBus.dispatch('rotatecw');
}.bind(this));
contextMenuItems.contextPageRotateCcw.addEventListener('click',
function PDFPresentationMode_contextPageRotateCcwClick(e) {
});
contextMenuItems.contextPageRotateCcw.addEventListener('click', () => {
this.contextMenuOpen = false;
this.eventBus.dispatch('rotateccw');
}.bind(this));
});
}
}
PDFPresentationMode.prototype = {
/**
* Request the browser to enter fullscreen mode.
* @returns {boolean} Indicating if the request was successful.
*/
request: function PDFPresentationMode_request() {
if (this.switchInProgress || this.active ||
!this.viewer.hasChildNodes()) {
request() {
if (this.switchInProgress || this.active || !this.viewer.hasChildNodes()) {
return false;
}
this._addFullscreenChangeListeners();
@ -108,12 +107,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -108,12 +107,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
};
return true;
},
}
/**
* @private
*/
_mouseWheel: function PDFPresentationMode_mouseWheel(evt) {
_mouseWheel(evt) {
if (!this.active) {
return;
}
@ -121,10 +120,6 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -121,10 +120,6 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
evt.preventDefault();
var delta = normalizeWheelEventDelta(evt);
var MOUSE_SCROLL_COOLDOWN_TIME = 50;
var PAGE_SWITCH_THRESHOLD = 0.1;
var currentTime = (new Date()).getTime();
var storedTime = this.mouseScrollTimeStamp;
@ -149,19 +144,17 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -149,19 +144,17 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
this.mouseScrollTimeStamp = currentTime;
}
}
},
}
get isFullscreen() {
return !!(document.fullscreenElement ||
document.mozFullScreen ||
document.webkitIsFullScreen ||
document.msFullscreenElement);
},
return !!(document.fullscreenElement || document.mozFullScreen ||
document.webkitIsFullScreen || document.msFullscreenElement);
}
/**
* @private
*/
_goToPreviousPage: function PDFPresentationMode_goToPreviousPage() {
_goToPreviousPage() {
var page = this.pdfViewer.currentPageNumber;
// If we're at the first page, we don't need to do anything.
if (page <= 1) {
@ -169,12 +162,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -169,12 +162,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
}
this.pdfViewer.currentPageNumber = (page - 1);
return true;
},
}
/**
* @private
*/
_goToNextPage: function PDFPresentationMode_goToNextPage() {
_goToNextPage() {
var page = this.pdfViewer.currentPageNumber;
// If we're at the last page, we don't need to do anything.
if (page >= this.pdfViewer.pagesCount) {
@ -182,18 +175,18 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -182,18 +175,18 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
}
this.pdfViewer.currentPageNumber = (page + 1);
return true;
},
}
/**
* @private
*/
_notifyStateChange: function PDFPresentationMode_notifyStateChange() {
_notifyStateChange() {
this.eventBus.dispatch('presentationmodechanged', {
source: this,
active: this.active,
switchInProgress: !!this.switchInProgress
switchInProgress: !!this.switchInProgress,
});
},
}
/**
* Used to initialize a timeout when requesting Presentation Mode,
@ -201,34 +194,34 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -201,34 +194,34 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
* This timeout is used to prevent the current page from being scrolled
* partially, or completely, out of view when entering Presentation Mode.
* NOTE: This issue seems limited to certain zoom levels (e.g. page-width).
*
* @private
*/
_setSwitchInProgress: function PDFPresentationMode_setSwitchInProgress() {
_setSwitchInProgress() {
if (this.switchInProgress) {
clearTimeout(this.switchInProgress);
}
this.switchInProgress = setTimeout(function switchInProgressTimeout() {
this.switchInProgress = setTimeout(() => {
this._removeFullscreenChangeListeners();
delete this.switchInProgress;
this._notifyStateChange();
}.bind(this), DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS);
},
}, DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS);
}
/**
* @private
*/
_resetSwitchInProgress:
function PDFPresentationMode_resetSwitchInProgress() {
_resetSwitchInProgress() {
if (this.switchInProgress) {
clearTimeout(this.switchInProgress);
delete this.switchInProgress;
}
},
}
/**
* @private
*/
_enter: function PDFPresentationMode_enter() {
_enter() {
this.active = true;
this._resetSwitchInProgress();
this._notifyStateChange();
@ -236,10 +229,10 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -236,10 +229,10 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
// Ensure that the correct page is scrolled into view when entering
// Presentation Mode, by waiting until fullscreen mode in enabled.
setTimeout(function enterPresentationModeTimeout() {
setTimeout(() => {
this.pdfViewer.currentPageNumber = this.args.page;
this.pdfViewer.currentScaleValue = 'page-fit';
}.bind(this), 0);
}, 0);
this._addWindowListeners();
this._showControls();
@ -250,18 +243,18 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -250,18 +243,18 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
// for the user to deselect text that is selected (e.g. with "Select all")
// when entering Presentation Mode, hence we remove any active selection.
window.getSelection().removeAllRanges();
},
}
/**
* @private
*/
_exit: function PDFPresentationMode_exit() {
_exit() {
var page = this.pdfViewer.currentPageNumber;
this.container.classList.remove(ACTIVE_SELECTOR);
// Ensure that the correct page is scrolled into view when exiting
// Presentation Mode, by waiting until fullscreen mode is disabled.
setTimeout(function exitPresentationModeTimeout() {
setTimeout(() => {
this.active = false;
this._removeFullscreenChangeListeners();
this._notifyStateChange();
@ -269,27 +262,27 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -269,27 +262,27 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
this.pdfViewer.currentScaleValue = this.args.previousScale;
this.pdfViewer.currentPageNumber = page;
this.args = null;
}.bind(this), 0);
}, 0);
this._removeWindowListeners();
this._hideControls();
this._resetMouseScrollState();
this.container.removeAttribute('contextmenu');
this.contextMenuOpen = false;
},
}
/**
* @private
*/
_mouseDown: function PDFPresentationMode_mouseDown(evt) {
_mouseDown(evt) {
if (this.contextMenuOpen) {
this.contextMenuOpen = false;
evt.preventDefault();
return;
}
if (evt.button === 0) {
// Enable clicking of links in presentation mode. Please note:
// Only links pointing to destinations in the current PDF document work.
// Enable clicking of links in presentation mode. Note: only links
// pointing to destinations in the current PDF document work.
var isInternalLink = (evt.target.href &&
evt.target.classList.contains('internalLink'));
if (!isInternalLink) {
@ -298,78 +291,72 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -298,78 +291,72 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
this.pdfViewer.currentPageNumber += (evt.shiftKey ? -1 : 1);
}
}
},
}
/**
* @private
*/
_contextMenu: function PDFPresentationMode_contextMenu() {
_contextMenu() {
this.contextMenuOpen = true;
},
}
/**
* @private
*/
_showControls: function PDFPresentationMode_showControls() {
_showControls() {
if (this.controlsTimeout) {
clearTimeout(this.controlsTimeout);
} else {
this.container.classList.add(CONTROLS_SELECTOR);
}
this.controlsTimeout = setTimeout(function showControlsTimeout() {
this.controlsTimeout = setTimeout(() => {
this.container.classList.remove(CONTROLS_SELECTOR);
delete this.controlsTimeout;
}.bind(this), DELAY_BEFORE_HIDING_CONTROLS);
},
}, DELAY_BEFORE_HIDING_CONTROLS);
}
/**
* @private
*/
_hideControls: function PDFPresentationMode_hideControls() {
_hideControls() {
if (!this.controlsTimeout) {
return;
}
clearTimeout(this.controlsTimeout);
this.container.classList.remove(CONTROLS_SELECTOR);
delete this.controlsTimeout;
},
}
/**
* Resets the properties used for tracking mouse scrolling events.
*
* @private
*/
_resetMouseScrollState:
function PDFPresentationMode_resetMouseScrollState() {
_resetMouseScrollState() {
this.mouseScrollTimeStamp = 0;
this.mouseScrollDelta = 0;
},
}
/**
* @private
*/
_touchSwipe: function PDFPresentationMode_touchSwipe(evt) {
_touchSwipe(evt) {
if (!this.active) {
return;
}
// Must move at least these many CSS pixels for it to count as a swipe
var SWIPE_MIN_DISTANCE_THRESHOLD = 50;
// The swipe angle is allowed to deviate from the x or y axis by this much
// before it is not considered a swipe in that direction any more.
var SWIPE_ANGLE_THRESHOLD = Math.PI / 6;
if (evt.touches.length > 1) {
// Multiple touch points detected, cancel the swipe.
// Multiple touch points detected; cancel the swipe.
this.touchSwipeState = null;
return;
}
switch (evt.type) {
case 'touchstart':
this.touchSwipeState = {
startX: evt.touches[0].pageX,
startY: evt.touches[0].pageY,
endX: evt.touches[0].pageX,
endY: evt.touches[0].pageY
endY: evt.touches[0].pageY,
};
break;
case 'touchmove':
@ -378,9 +365,8 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -378,9 +365,8 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
}
this.touchSwipeState.endX = evt.touches[0].pageX;
this.touchSwipeState.endY = evt.touches[0].pageY;
// Do a preventDefault to avoid the swipe from triggering browser
// gestures (Chrome in particular has some sort of swipe gesture in
// fullscreen mode).
// Avoid the swipe from triggering browser gestures (Chrome in
// particular has some sort of swipe gesture in fullscreen mode).
evt.preventDefault();
break;
case 'touchend':
@ -394,11 +380,11 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -394,11 +380,11 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
if (Math.abs(dx) > SWIPE_MIN_DISTANCE_THRESHOLD &&
(absAngle <= SWIPE_ANGLE_THRESHOLD ||
absAngle >= (Math.PI - SWIPE_ANGLE_THRESHOLD))) {
// horizontal swipe
// Horizontal swipe.
delta = dx;
} else if (Math.abs(dy) > SWIPE_MIN_DISTANCE_THRESHOLD &&
Math.abs(absAngle - (Math.PI / 2)) <= SWIPE_ANGLE_THRESHOLD) {
// vertical swipe
// Vertical swipe.
delta = dy;
}
if (delta > 0) {
@ -408,12 +394,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -408,12 +394,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
}
break;
}
},
}
/**
* @private
*/
_addWindowListeners: function PDFPresentationMode_addWindowListeners() {
_addWindowListeners() {
this.showControlsBind = this._showControls.bind(this);
this.mouseDownBind = this._mouseDown.bind(this);
this.mouseWheelBind = this._mouseWheel.bind(this);
@ -429,13 +415,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -429,13 +415,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
window.addEventListener('touchstart', this.touchSwipeBind);
window.addEventListener('touchmove', this.touchSwipeBind);
window.addEventListener('touchend', this.touchSwipeBind);
},
}
/**
* @private
*/
_removeWindowListeners:
function PDFPresentationMode_removeWindowListeners() {
_removeWindowListeners() {
window.removeEventListener('mousemove', this.showControlsBind);
window.removeEventListener('mousedown', this.mouseDownBind);
window.removeEventListener('wheel', this.mouseWheelBind);
@ -451,24 +436,23 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -451,24 +436,23 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
delete this.resetMouseScrollStateBind;
delete this.contextMenuBind;
delete this.touchSwipeBind;
},
}
/**
* @private
*/
_fullscreenChange: function PDFPresentationMode_fullscreenChange() {
_fullscreenChange() {
if (this.isFullscreen) {
this._enter();
} else {
this._exit();
}
},
}
/**
* @private
*/
_addFullscreenChangeListeners:
function PDFPresentationMode_addFullscreenChangeListeners() {
_addFullscreenChangeListeners() {
this.fullscreenChangeBind = this._fullscreenChange.bind(this);
window.addEventListener('fullscreenchange', this.fullscreenChangeBind);
@ -480,13 +464,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -480,13 +464,12 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
window.addEventListener('MSFullscreenChange',
this.fullscreenChangeBind);
}
},
}
/**
* @private
*/
_removeFullscreenChangeListeners:
function PDFPresentationMode_removeFullscreenChangeListeners() {
_removeFullscreenChangeListeners() {
window.removeEventListener('fullscreenchange', this.fullscreenChangeBind);
window.removeEventListener('mozfullscreenchange',
this.fullscreenChangeBind);
@ -500,10 +483,7 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { @@ -500,10 +483,7 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
delete this.fullscreenChangeBind;
}
};
return PDFPresentationMode;
})();
}
export {
PDFPresentationMode,

Loading…
Cancel
Save