grab_to_pan.js 5.0 KB

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