grab_to_pan.js 4.9 KB

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