pdf_presentation_mode.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFPresentationMode = undefined;
  20. var _ui_utils = require('pdfjs-web/ui_utils');
  21. var DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS = 1500;
  22. var DELAY_BEFORE_HIDING_CONTROLS = 3000;
  23. var ACTIVE_SELECTOR = 'pdfPresentationMode';
  24. var CONTROLS_SELECTOR = 'pdfPresentationModeControls';
  25. var PDFPresentationMode = function PDFPresentationModeClosure() {
  26. function PDFPresentationMode(options) {
  27. this.container = options.container;
  28. this.viewer = options.viewer || options.container.firstElementChild;
  29. this.pdfViewer = options.pdfViewer;
  30. this.eventBus = options.eventBus;
  31. var contextMenuItems = options.contextMenuItems || null;
  32. this.active = false;
  33. this.args = null;
  34. this.contextMenuOpen = false;
  35. this.mouseScrollTimeStamp = 0;
  36. this.mouseScrollDelta = 0;
  37. this.touchSwipeState = null;
  38. if (contextMenuItems) {
  39. contextMenuItems.contextFirstPage.addEventListener('click', function PDFPresentationMode_contextFirstPageClick(e) {
  40. this.contextMenuOpen = false;
  41. this.eventBus.dispatch('firstpage');
  42. }.bind(this));
  43. contextMenuItems.contextLastPage.addEventListener('click', function PDFPresentationMode_contextLastPageClick(e) {
  44. this.contextMenuOpen = false;
  45. this.eventBus.dispatch('lastpage');
  46. }.bind(this));
  47. contextMenuItems.contextPageRotateCw.addEventListener('click', function PDFPresentationMode_contextPageRotateCwClick(e) {
  48. this.contextMenuOpen = false;
  49. this.eventBus.dispatch('rotatecw');
  50. }.bind(this));
  51. contextMenuItems.contextPageRotateCcw.addEventListener('click', function PDFPresentationMode_contextPageRotateCcwClick(e) {
  52. this.contextMenuOpen = false;
  53. this.eventBus.dispatch('rotateccw');
  54. }.bind(this));
  55. }
  56. }
  57. PDFPresentationMode.prototype = {
  58. request: function PDFPresentationMode_request() {
  59. if (this.switchInProgress || this.active || !this.viewer.hasChildNodes()) {
  60. return false;
  61. }
  62. this._addFullscreenChangeListeners();
  63. this._setSwitchInProgress();
  64. this._notifyStateChange();
  65. if (this.container.requestFullscreen) {
  66. this.container.requestFullscreen();
  67. } else if (this.container.mozRequestFullScreen) {
  68. this.container.mozRequestFullScreen();
  69. } else if (this.container.webkitRequestFullscreen) {
  70. this.container.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  71. } else if (this.container.msRequestFullscreen) {
  72. this.container.msRequestFullscreen();
  73. } else {
  74. return false;
  75. }
  76. this.args = {
  77. page: this.pdfViewer.currentPageNumber,
  78. previousScale: this.pdfViewer.currentScaleValue
  79. };
  80. return true;
  81. },
  82. _mouseWheel: function PDFPresentationMode_mouseWheel(evt) {
  83. if (!this.active) {
  84. return;
  85. }
  86. evt.preventDefault();
  87. var delta = (0, _ui_utils.normalizeWheelEventDelta)(evt);
  88. var MOUSE_SCROLL_COOLDOWN_TIME = 50;
  89. var PAGE_SWITCH_THRESHOLD = 0.1;
  90. var currentTime = new Date().getTime();
  91. var storedTime = this.mouseScrollTimeStamp;
  92. if (currentTime > storedTime && currentTime - storedTime < MOUSE_SCROLL_COOLDOWN_TIME) {
  93. return;
  94. }
  95. if (this.mouseScrollDelta > 0 && delta < 0 || this.mouseScrollDelta < 0 && delta > 0) {
  96. this._resetMouseScrollState();
  97. }
  98. this.mouseScrollDelta += delta;
  99. if (Math.abs(this.mouseScrollDelta) >= PAGE_SWITCH_THRESHOLD) {
  100. var totalDelta = this.mouseScrollDelta;
  101. this._resetMouseScrollState();
  102. var success = totalDelta > 0 ? this._goToPreviousPage() : this._goToNextPage();
  103. if (success) {
  104. this.mouseScrollTimeStamp = currentTime;
  105. }
  106. }
  107. },
  108. get isFullscreen() {
  109. return !!(document.fullscreenElement || document.mozFullScreen || document.webkitIsFullScreen || document.msFullscreenElement);
  110. },
  111. _goToPreviousPage: function PDFPresentationMode_goToPreviousPage() {
  112. var page = this.pdfViewer.currentPageNumber;
  113. if (page <= 1) {
  114. return false;
  115. }
  116. this.pdfViewer.currentPageNumber = page - 1;
  117. return true;
  118. },
  119. _goToNextPage: function PDFPresentationMode_goToNextPage() {
  120. var page = this.pdfViewer.currentPageNumber;
  121. if (page >= this.pdfViewer.pagesCount) {
  122. return false;
  123. }
  124. this.pdfViewer.currentPageNumber = page + 1;
  125. return true;
  126. },
  127. _notifyStateChange: function PDFPresentationMode_notifyStateChange() {
  128. this.eventBus.dispatch('presentationmodechanged', {
  129. source: this,
  130. active: this.active,
  131. switchInProgress: !!this.switchInProgress
  132. });
  133. },
  134. _setSwitchInProgress: function PDFPresentationMode_setSwitchInProgress() {
  135. if (this.switchInProgress) {
  136. clearTimeout(this.switchInProgress);
  137. }
  138. this.switchInProgress = setTimeout(function switchInProgressTimeout() {
  139. this._removeFullscreenChangeListeners();
  140. delete this.switchInProgress;
  141. this._notifyStateChange();
  142. }.bind(this), DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS);
  143. },
  144. _resetSwitchInProgress: function PDFPresentationMode_resetSwitchInProgress() {
  145. if (this.switchInProgress) {
  146. clearTimeout(this.switchInProgress);
  147. delete this.switchInProgress;
  148. }
  149. },
  150. _enter: function PDFPresentationMode_enter() {
  151. this.active = true;
  152. this._resetSwitchInProgress();
  153. this._notifyStateChange();
  154. this.container.classList.add(ACTIVE_SELECTOR);
  155. setTimeout(function enterPresentationModeTimeout() {
  156. this.pdfViewer.currentPageNumber = this.args.page;
  157. this.pdfViewer.currentScaleValue = 'page-fit';
  158. }.bind(this), 0);
  159. this._addWindowListeners();
  160. this._showControls();
  161. this.contextMenuOpen = false;
  162. this.container.setAttribute('contextmenu', 'viewerContextMenu');
  163. window.getSelection().removeAllRanges();
  164. },
  165. _exit: function PDFPresentationMode_exit() {
  166. var page = this.pdfViewer.currentPageNumber;
  167. this.container.classList.remove(ACTIVE_SELECTOR);
  168. setTimeout(function exitPresentationModeTimeout() {
  169. this.active = false;
  170. this._removeFullscreenChangeListeners();
  171. this._notifyStateChange();
  172. this.pdfViewer.currentScaleValue = this.args.previousScale;
  173. this.pdfViewer.currentPageNumber = page;
  174. this.args = null;
  175. }.bind(this), 0);
  176. this._removeWindowListeners();
  177. this._hideControls();
  178. this._resetMouseScrollState();
  179. this.container.removeAttribute('contextmenu');
  180. this.contextMenuOpen = false;
  181. },
  182. _mouseDown: function PDFPresentationMode_mouseDown(evt) {
  183. if (this.contextMenuOpen) {
  184. this.contextMenuOpen = false;
  185. evt.preventDefault();
  186. return;
  187. }
  188. if (evt.button === 0) {
  189. var isInternalLink = evt.target.href && evt.target.classList.contains('internalLink');
  190. if (!isInternalLink) {
  191. evt.preventDefault();
  192. this.pdfViewer.currentPageNumber += evt.shiftKey ? -1 : 1;
  193. }
  194. }
  195. },
  196. _contextMenu: function PDFPresentationMode_contextMenu() {
  197. this.contextMenuOpen = true;
  198. },
  199. _showControls: function PDFPresentationMode_showControls() {
  200. if (this.controlsTimeout) {
  201. clearTimeout(this.controlsTimeout);
  202. } else {
  203. this.container.classList.add(CONTROLS_SELECTOR);
  204. }
  205. this.controlsTimeout = setTimeout(function showControlsTimeout() {
  206. this.container.classList.remove(CONTROLS_SELECTOR);
  207. delete this.controlsTimeout;
  208. }.bind(this), DELAY_BEFORE_HIDING_CONTROLS);
  209. },
  210. _hideControls: function PDFPresentationMode_hideControls() {
  211. if (!this.controlsTimeout) {
  212. return;
  213. }
  214. clearTimeout(this.controlsTimeout);
  215. this.container.classList.remove(CONTROLS_SELECTOR);
  216. delete this.controlsTimeout;
  217. },
  218. _resetMouseScrollState: function PDFPresentationMode_resetMouseScrollState() {
  219. this.mouseScrollTimeStamp = 0;
  220. this.mouseScrollDelta = 0;
  221. },
  222. _touchSwipe: function PDFPresentationMode_touchSwipe(evt) {
  223. if (!this.active) {
  224. return;
  225. }
  226. var SWIPE_MIN_DISTANCE_THRESHOLD = 50;
  227. var SWIPE_ANGLE_THRESHOLD = Math.PI / 6;
  228. if (evt.touches.length > 1) {
  229. this.touchSwipeState = null;
  230. return;
  231. }
  232. switch (evt.type) {
  233. case 'touchstart':
  234. this.touchSwipeState = {
  235. startX: evt.touches[0].pageX,
  236. startY: evt.touches[0].pageY,
  237. endX: evt.touches[0].pageX,
  238. endY: evt.touches[0].pageY
  239. };
  240. break;
  241. case 'touchmove':
  242. if (this.touchSwipeState === null) {
  243. return;
  244. }
  245. this.touchSwipeState.endX = evt.touches[0].pageX;
  246. this.touchSwipeState.endY = evt.touches[0].pageY;
  247. evt.preventDefault();
  248. break;
  249. case 'touchend':
  250. if (this.touchSwipeState === null) {
  251. return;
  252. }
  253. var delta = 0;
  254. var dx = this.touchSwipeState.endX - this.touchSwipeState.startX;
  255. var dy = this.touchSwipeState.endY - this.touchSwipeState.startY;
  256. var absAngle = Math.abs(Math.atan2(dy, dx));
  257. if (Math.abs(dx) > SWIPE_MIN_DISTANCE_THRESHOLD && (absAngle <= SWIPE_ANGLE_THRESHOLD || absAngle >= Math.PI - SWIPE_ANGLE_THRESHOLD)) {
  258. delta = dx;
  259. } else if (Math.abs(dy) > SWIPE_MIN_DISTANCE_THRESHOLD && Math.abs(absAngle - Math.PI / 2) <= SWIPE_ANGLE_THRESHOLD) {
  260. delta = dy;
  261. }
  262. if (delta > 0) {
  263. this._goToPreviousPage();
  264. } else if (delta < 0) {
  265. this._goToNextPage();
  266. }
  267. break;
  268. }
  269. },
  270. _addWindowListeners: function PDFPresentationMode_addWindowListeners() {
  271. this.showControlsBind = this._showControls.bind(this);
  272. this.mouseDownBind = this._mouseDown.bind(this);
  273. this.mouseWheelBind = this._mouseWheel.bind(this);
  274. this.resetMouseScrollStateBind = this._resetMouseScrollState.bind(this);
  275. this.contextMenuBind = this._contextMenu.bind(this);
  276. this.touchSwipeBind = this._touchSwipe.bind(this);
  277. window.addEventListener('mousemove', this.showControlsBind);
  278. window.addEventListener('mousedown', this.mouseDownBind);
  279. window.addEventListener('wheel', this.mouseWheelBind);
  280. window.addEventListener('keydown', this.resetMouseScrollStateBind);
  281. window.addEventListener('contextmenu', this.contextMenuBind);
  282. window.addEventListener('touchstart', this.touchSwipeBind);
  283. window.addEventListener('touchmove', this.touchSwipeBind);
  284. window.addEventListener('touchend', this.touchSwipeBind);
  285. },
  286. _removeWindowListeners: function PDFPresentationMode_removeWindowListeners() {
  287. window.removeEventListener('mousemove', this.showControlsBind);
  288. window.removeEventListener('mousedown', this.mouseDownBind);
  289. window.removeEventListener('wheel', this.mouseWheelBind);
  290. window.removeEventListener('keydown', this.resetMouseScrollStateBind);
  291. window.removeEventListener('contextmenu', this.contextMenuBind);
  292. window.removeEventListener('touchstart', this.touchSwipeBind);
  293. window.removeEventListener('touchmove', this.touchSwipeBind);
  294. window.removeEventListener('touchend', this.touchSwipeBind);
  295. delete this.showControlsBind;
  296. delete this.mouseDownBind;
  297. delete this.mouseWheelBind;
  298. delete this.resetMouseScrollStateBind;
  299. delete this.contextMenuBind;
  300. delete this.touchSwipeBind;
  301. },
  302. _fullscreenChange: function PDFPresentationMode_fullscreenChange() {
  303. if (this.isFullscreen) {
  304. this._enter();
  305. } else {
  306. this._exit();
  307. }
  308. },
  309. _addFullscreenChangeListeners: function PDFPresentationMode_addFullscreenChangeListeners() {
  310. this.fullscreenChangeBind = this._fullscreenChange.bind(this);
  311. window.addEventListener('fullscreenchange', this.fullscreenChangeBind);
  312. window.addEventListener('mozfullscreenchange', this.fullscreenChangeBind);
  313. window.addEventListener('webkitfullscreenchange', this.fullscreenChangeBind);
  314. window.addEventListener('MSFullscreenChange', this.fullscreenChangeBind);
  315. },
  316. _removeFullscreenChangeListeners: function PDFPresentationMode_removeFullscreenChangeListeners() {
  317. window.removeEventListener('fullscreenchange', this.fullscreenChangeBind);
  318. window.removeEventListener('mozfullscreenchange', this.fullscreenChangeBind);
  319. window.removeEventListener('webkitfullscreenchange', this.fullscreenChangeBind);
  320. window.removeEventListener('MSFullscreenChange', this.fullscreenChangeBind);
  321. delete this.fullscreenChangeBind;
  322. }
  323. };
  324. return PDFPresentationMode;
  325. }();
  326. exports.PDFPresentationMode = PDFPresentationMode;