grab_to_pan.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 = GrabToPan;
  27. function GrabToPan(options) {
  28. this.element = options.element;
  29. this.document = options.element.ownerDocument;
  30. if (typeof options.ignoreTarget === "function") {
  31. this.ignoreTarget = options.ignoreTarget;
  32. }
  33. this.onActiveChanged = options.onActiveChanged;
  34. this.activate = this.activate.bind(this);
  35. this.deactivate = this.deactivate.bind(this);
  36. this.toggle = this.toggle.bind(this);
  37. this._onmousedown = this._onmousedown.bind(this);
  38. this._onmousemove = this._onmousemove.bind(this);
  39. this._endPan = this._endPan.bind(this);
  40. const overlay = this.overlay = document.createElement("div");
  41. overlay.className = "grab-to-pan-grabbing";
  42. }
  43. GrabToPan.prototype = {
  44. CSS_CLASS_GRAB: "grab-to-pan-grab",
  45. activate: function GrabToPan_activate() {
  46. if (!this.active) {
  47. this.active = true;
  48. this.element.addEventListener("mousedown", this._onmousedown, true);
  49. this.element.classList.add(this.CSS_CLASS_GRAB);
  50. if (this.onActiveChanged) {
  51. this.onActiveChanged(true);
  52. }
  53. }
  54. },
  55. deactivate: function GrabToPan_deactivate() {
  56. if (this.active) {
  57. this.active = false;
  58. this.element.removeEventListener("mousedown", this._onmousedown, true);
  59. this._endPan();
  60. this.element.classList.remove(this.CSS_CLASS_GRAB);
  61. if (this.onActiveChanged) {
  62. this.onActiveChanged(false);
  63. }
  64. }
  65. },
  66. toggle: function GrabToPan_toggle() {
  67. if (this.active) {
  68. this.deactivate();
  69. } else {
  70. this.activate();
  71. }
  72. },
  73. ignoreTarget: function GrabToPan_ignoreTarget(node) {
  74. return node.matches("a[href], a[href] *, input, textarea, button, button *, select, option");
  75. },
  76. _onmousedown: function GrabToPan__onmousedown(event) {
  77. if (event.button !== 0 || this.ignoreTarget(event.target)) {
  78. return;
  79. }
  80. if (event.originalTarget) {
  81. try {
  82. event.originalTarget.tagName;
  83. } catch (e) {
  84. return;
  85. }
  86. }
  87. this.scrollLeftStart = this.element.scrollLeft;
  88. this.scrollTopStart = this.element.scrollTop;
  89. this.clientXStart = event.clientX;
  90. this.clientYStart = event.clientY;
  91. this.document.addEventListener("mousemove", this._onmousemove, true);
  92. this.document.addEventListener("mouseup", this._endPan, true);
  93. this.element.addEventListener("scroll", this._endPan, true);
  94. event.preventDefault();
  95. event.stopPropagation();
  96. const focusedElement = document.activeElement;
  97. if (focusedElement && !focusedElement.contains(event.target)) {
  98. focusedElement.blur();
  99. }
  100. },
  101. _onmousemove: function GrabToPan__onmousemove(event) {
  102. this.element.removeEventListener("scroll", this._endPan, true);
  103. if (isLeftMouseReleased(event)) {
  104. this._endPan();
  105. return;
  106. }
  107. const xDiff = event.clientX - this.clientXStart;
  108. const yDiff = event.clientY - this.clientYStart;
  109. const scrollTop = this.scrollTopStart - yDiff;
  110. const scrollLeft = this.scrollLeftStart - xDiff;
  111. if (this.element.scrollTo) {
  112. this.element.scrollTo({
  113. top: scrollTop,
  114. left: scrollLeft,
  115. behavior: "instant"
  116. });
  117. } else {
  118. this.element.scrollTop = scrollTop;
  119. this.element.scrollLeft = scrollLeft;
  120. }
  121. if (!this.overlay.parentNode) {
  122. document.body.appendChild(this.overlay);
  123. }
  124. },
  125. _endPan: function GrabToPan__endPan() {
  126. this.element.removeEventListener("scroll", this._endPan, true);
  127. this.document.removeEventListener("mousemove", this._onmousemove, true);
  128. this.document.removeEventListener("mouseup", this._endPan, true);
  129. this.overlay.remove();
  130. }
  131. };
  132. function isLeftMouseReleased(event) {
  133. if ("buttons" in event) {
  134. return !(event.buttons & 1);
  135. }
  136. const chrome = window.chrome;
  137. const isChrome15OrOpera15plus = chrome && (chrome.webstore || chrome.app);
  138. const isSafari6plus = /Apple/.test(navigator.vendor) && /Version\/([6-9]\d*|[1-5]\d+)/.test(navigator.userAgent);
  139. if (isChrome15OrOpera15plus || isSafari6plus) {
  140. return event.which === 0;
  141. }
  142. return false;
  143. }