grab_to_pan.js 5.2 KB

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