pdf_presentation_mode.js 12 KB

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