grab_to_pan.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.GrabToPan = void 0;
  27. const CSS_CLASS_GRAB = "grab-to-pan-grab";
  28. class GrabToPan {
  29. constructor(options) {
  30. this.element = options.element;
  31. this.document = options.element.ownerDocument;
  32. if (typeof options.ignoreTarget === "function") {
  33. this.ignoreTarget = options.ignoreTarget;
  34. }
  35. this.onActiveChanged = options.onActiveChanged;
  36. this.activate = this.activate.bind(this);
  37. this.deactivate = this.deactivate.bind(this);
  38. this.toggle = this.toggle.bind(this);
  39. this._onMouseDown = this.#onMouseDown.bind(this);
  40. this._onMouseMove = this.#onMouseMove.bind(this);
  41. this._endPan = this.#endPan.bind(this);
  42. const overlay = this.overlay = document.createElement("div");
  43. overlay.className = "grab-to-pan-grabbing";
  44. }
  45. activate() {
  46. if (!this.active) {
  47. this.active = true;
  48. this.element.addEventListener("mousedown", this._onMouseDown, true);
  49. this.element.classList.add(CSS_CLASS_GRAB);
  50. this.onActiveChanged?.(true);
  51. }
  52. }
  53. deactivate() {
  54. if (this.active) {
  55. this.active = false;
  56. this.element.removeEventListener("mousedown", this._onMouseDown, true);
  57. this._endPan();
  58. this.element.classList.remove(CSS_CLASS_GRAB);
  59. this.onActiveChanged?.(false);
  60. }
  61. }
  62. toggle() {
  63. if (this.active) {
  64. this.deactivate();
  65. } else {
  66. this.activate();
  67. }
  68. }
  69. ignoreTarget(node) {
  70. return node.matches("a[href], a[href] *, input, textarea, button, button *, select, option");
  71. }
  72. #onMouseDown(event) {
  73. if (event.button !== 0 || this.ignoreTarget(event.target)) {
  74. return;
  75. }
  76. if (event.originalTarget) {
  77. try {
  78. event.originalTarget.tagName;
  79. } catch (e) {
  80. return;
  81. }
  82. }
  83. this.scrollLeftStart = this.element.scrollLeft;
  84. this.scrollTopStart = this.element.scrollTop;
  85. this.clientXStart = event.clientX;
  86. this.clientYStart = event.clientY;
  87. this.document.addEventListener("mousemove", this._onMouseMove, true);
  88. this.document.addEventListener("mouseup", this._endPan, true);
  89. this.element.addEventListener("scroll", this._endPan, true);
  90. event.preventDefault();
  91. event.stopPropagation();
  92. const focusedElement = document.activeElement;
  93. if (focusedElement && !focusedElement.contains(event.target)) {
  94. focusedElement.blur();
  95. }
  96. }
  97. #onMouseMove(event) {
  98. this.element.removeEventListener("scroll", this._endPan, true);
  99. if (!(event.buttons & 1)) {
  100. this._endPan();
  101. return;
  102. }
  103. const xDiff = event.clientX - this.clientXStart;
  104. const yDiff = event.clientY - this.clientYStart;
  105. const scrollTop = this.scrollTopStart - yDiff;
  106. const scrollLeft = this.scrollLeftStart - xDiff;
  107. if (this.element.scrollTo) {
  108. this.element.scrollTo({
  109. top: scrollTop,
  110. left: scrollLeft,
  111. behavior: "instant"
  112. });
  113. } else {
  114. this.element.scrollTop = scrollTop;
  115. this.element.scrollLeft = scrollLeft;
  116. }
  117. if (!this.overlay.parentNode) {
  118. document.body.append(this.overlay);
  119. }
  120. }
  121. #endPan() {
  122. this.element.removeEventListener("scroll", this._endPan, true);
  123. this.document.removeEventListener("mousemove", this._onMouseMove, true);
  124. this.document.removeEventListener("mouseup", this._endPan, true);
  125. this.overlay.remove();
  126. }
  127. }
  128. exports.GrabToPan = GrabToPan;