pdf_cursor_tools.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFCursorTools = exports.CursorTool = undefined;
  27. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  28. var _grab_to_pan = require('./grab_to_pan');
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. var CursorTool = {
  31. SELECT: 0,
  32. HAND: 1,
  33. ZOOM: 2
  34. };
  35. var PDFCursorTools = function () {
  36. function PDFCursorTools(_ref) {
  37. var _this = this;
  38. var container = _ref.container,
  39. eventBus = _ref.eventBus,
  40. _ref$cursorToolOnLoad = _ref.cursorToolOnLoad,
  41. cursorToolOnLoad = _ref$cursorToolOnLoad === undefined ? CursorTool.SELECT : _ref$cursorToolOnLoad;
  42. _classCallCheck(this, PDFCursorTools);
  43. this.container = container;
  44. this.eventBus = eventBus;
  45. this.active = CursorTool.SELECT;
  46. this.activeBeforePresentationMode = null;
  47. this.handTool = new _grab_to_pan.GrabToPan({ element: this.container });
  48. this._addEventListeners();
  49. Promise.resolve().then(function () {
  50. _this.switchTool(cursorToolOnLoad);
  51. });
  52. }
  53. _createClass(PDFCursorTools, [{
  54. key: 'switchTool',
  55. value: function switchTool(tool) {
  56. var _this2 = this;
  57. if (this.activeBeforePresentationMode !== null) {
  58. return;
  59. }
  60. if (tool === this.active) {
  61. return;
  62. }
  63. var disableActiveTool = function disableActiveTool() {
  64. switch (_this2.active) {
  65. case CursorTool.SELECT:
  66. break;
  67. case CursorTool.HAND:
  68. _this2.handTool.deactivate();
  69. break;
  70. case CursorTool.ZOOM:
  71. }
  72. };
  73. switch (tool) {
  74. case CursorTool.SELECT:
  75. disableActiveTool();
  76. break;
  77. case CursorTool.HAND:
  78. disableActiveTool();
  79. this.handTool.activate();
  80. break;
  81. case CursorTool.ZOOM:
  82. default:
  83. console.error('switchTool: "' + tool + '" is an unsupported value.');
  84. return;
  85. }
  86. this.active = tool;
  87. this._dispatchEvent();
  88. }
  89. }, {
  90. key: '_dispatchEvent',
  91. value: function _dispatchEvent() {
  92. this.eventBus.dispatch('cursortoolchanged', {
  93. source: this,
  94. tool: this.active
  95. });
  96. }
  97. }, {
  98. key: '_addEventListeners',
  99. value: function _addEventListeners() {
  100. var _this3 = this;
  101. this.eventBus.on('switchcursortool', function (evt) {
  102. _this3.switchTool(evt.tool);
  103. });
  104. this.eventBus.on('presentationmodechanged', function (evt) {
  105. if (evt.switchInProgress) {
  106. return;
  107. }
  108. var previouslyActive = void 0;
  109. if (evt.active) {
  110. previouslyActive = _this3.active;
  111. _this3.switchTool(CursorTool.SELECT);
  112. _this3.activeBeforePresentationMode = previouslyActive;
  113. } else {
  114. previouslyActive = _this3.activeBeforePresentationMode;
  115. _this3.activeBeforePresentationMode = null;
  116. _this3.switchTool(previouslyActive);
  117. }
  118. });
  119. }
  120. }, {
  121. key: 'activeTool',
  122. get: function get() {
  123. return this.active;
  124. }
  125. }]);
  126. return PDFCursorTools;
  127. }();
  128. exports.CursorTool = CursorTool;
  129. exports.PDFCursorTools = PDFCursorTools;