pdf_cursor_tools.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.PDFCursorTools = exports.CursorTool = undefined;
  20. 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; }; }();
  21. var _grab_to_pan = require('./grab_to_pan');
  22. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  23. var CursorTool = {
  24. SELECT: 0,
  25. HAND: 1,
  26. ZOOM: 2
  27. };
  28. var PDFCursorTools = function () {
  29. function PDFCursorTools(_ref) {
  30. var _this = this;
  31. var container = _ref.container,
  32. eventBus = _ref.eventBus,
  33. preferences = _ref.preferences;
  34. _classCallCheck(this, PDFCursorTools);
  35. this.container = container;
  36. this.eventBus = eventBus;
  37. this.active = CursorTool.SELECT;
  38. this.activeBeforePresentationMode = null;
  39. this.handTool = new _grab_to_pan.GrabToPan({ element: this.container });
  40. this._addEventListeners();
  41. preferences.get('cursorToolOnLoad').then(function (value) {
  42. _this.switchTool(value);
  43. }).catch(function () {});
  44. }
  45. _createClass(PDFCursorTools, [{
  46. key: 'switchTool',
  47. value: function switchTool(tool) {
  48. var _this2 = this;
  49. if (this.activeBeforePresentationMode !== null) {
  50. return;
  51. }
  52. if (tool === this.active) {
  53. return;
  54. }
  55. var disableActiveTool = function disableActiveTool() {
  56. switch (_this2.active) {
  57. case CursorTool.SELECT:
  58. break;
  59. case CursorTool.HAND:
  60. _this2.handTool.deactivate();
  61. break;
  62. case CursorTool.ZOOM:
  63. }
  64. };
  65. switch (tool) {
  66. case CursorTool.SELECT:
  67. disableActiveTool();
  68. break;
  69. case CursorTool.HAND:
  70. disableActiveTool();
  71. this.handTool.activate();
  72. break;
  73. case CursorTool.ZOOM:
  74. default:
  75. console.error('switchTool: "' + tool + '" is an unsupported value.');
  76. return;
  77. }
  78. this.active = tool;
  79. this._dispatchEvent();
  80. }
  81. }, {
  82. key: '_dispatchEvent',
  83. value: function _dispatchEvent() {
  84. this.eventBus.dispatch('cursortoolchanged', {
  85. source: this,
  86. tool: this.active
  87. });
  88. }
  89. }, {
  90. key: '_addEventListeners',
  91. value: function _addEventListeners() {
  92. var _this3 = this;
  93. this.eventBus.on('switchcursortool', function (evt) {
  94. _this3.switchTool(evt.tool);
  95. });
  96. this.eventBus.on('presentationmodechanged', function (evt) {
  97. if (evt.switchInProgress) {
  98. return;
  99. }
  100. var previouslyActive = void 0;
  101. if (evt.active) {
  102. previouslyActive = _this3.active;
  103. _this3.switchTool(CursorTool.SELECT);
  104. _this3.activeBeforePresentationMode = previouslyActive;
  105. } else {
  106. previouslyActive = _this3.activeBeforePresentationMode;
  107. _this3.activeBeforePresentationMode = null;
  108. _this3.switchTool(previouslyActive);
  109. }
  110. });
  111. }
  112. }, {
  113. key: 'activeTool',
  114. get: function get() {
  115. return this.active;
  116. }
  117. }]);
  118. return PDFCursorTools;
  119. }();
  120. exports.CursorTool = CursorTool;
  121. exports.PDFCursorTools = PDFCursorTools;