UNPKG

4.25 kBJavaScriptView Raw
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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.GrabToPan = void 0;
28const CSS_CLASS_GRAB = "grab-to-pan-grab";
29
30class GrabToPan {
31 constructor(options) {
32 this.element = options.element;
33 this.document = options.element.ownerDocument;
34
35 if (typeof options.ignoreTarget === "function") {
36 this.ignoreTarget = options.ignoreTarget;
37 }
38
39 this.onActiveChanged = options.onActiveChanged;
40 this.activate = this.activate.bind(this);
41 this.deactivate = this.deactivate.bind(this);
42 this.toggle = this.toggle.bind(this);
43 this._onMouseDown = this.#onMouseDown.bind(this);
44 this._onMouseMove = this.#onMouseMove.bind(this);
45 this._endPan = this.#endPan.bind(this);
46 const overlay = this.overlay = document.createElement("div");
47 overlay.className = "grab-to-pan-grabbing";
48 }
49
50 activate() {
51 if (!this.active) {
52 this.active = true;
53 this.element.addEventListener("mousedown", this._onMouseDown, true);
54 this.element.classList.add(CSS_CLASS_GRAB);
55 this.onActiveChanged?.(true);
56 }
57 }
58
59 deactivate() {
60 if (this.active) {
61 this.active = false;
62 this.element.removeEventListener("mousedown", this._onMouseDown, true);
63
64 this._endPan();
65
66 this.element.classList.remove(CSS_CLASS_GRAB);
67 this.onActiveChanged?.(false);
68 }
69 }
70
71 toggle() {
72 if (this.active) {
73 this.deactivate();
74 } else {
75 this.activate();
76 }
77 }
78
79 ignoreTarget(node) {
80 return node.matches("a[href], a[href] *, input, textarea, button, button *, select, option");
81 }
82
83 #onMouseDown(event) {
84 if (event.button !== 0 || this.ignoreTarget(event.target)) {
85 return;
86 }
87
88 if (event.originalTarget) {
89 try {
90 event.originalTarget.tagName;
91 } catch (e) {
92 return;
93 }
94 }
95
96 this.scrollLeftStart = this.element.scrollLeft;
97 this.scrollTopStart = this.element.scrollTop;
98 this.clientXStart = event.clientX;
99 this.clientYStart = event.clientY;
100 this.document.addEventListener("mousemove", this._onMouseMove, true);
101 this.document.addEventListener("mouseup", this._endPan, true);
102 this.element.addEventListener("scroll", this._endPan, true);
103 event.preventDefault();
104 event.stopPropagation();
105 const focusedElement = document.activeElement;
106
107 if (focusedElement && !focusedElement.contains(event.target)) {
108 focusedElement.blur();
109 }
110 }
111
112 #onMouseMove(event) {
113 this.element.removeEventListener("scroll", this._endPan, true);
114
115 if (!(event.buttons & 1)) {
116 this._endPan();
117
118 return;
119 }
120
121 const xDiff = event.clientX - this.clientXStart;
122 const yDiff = event.clientY - this.clientYStart;
123 const scrollTop = this.scrollTopStart - yDiff;
124 const scrollLeft = this.scrollLeftStart - xDiff;
125
126 if (this.element.scrollTo) {
127 this.element.scrollTo({
128 top: scrollTop,
129 left: scrollLeft,
130 behavior: "instant"
131 });
132 } else {
133 this.element.scrollTop = scrollTop;
134 this.element.scrollLeft = scrollLeft;
135 }
136
137 if (!this.overlay.parentNode) {
138 document.body.append(this.overlay);
139 }
140 }
141
142 #endPan() {
143 this.element.removeEventListener("scroll", this._endPan, true);
144 this.document.removeEventListener("mousemove", this._onMouseMove, true);
145 this.document.removeEventListener("mouseup", this._endPan, true);
146 this.overlay.remove();
147 }
148
149}
150
151exports.GrabToPan = GrabToPan;
\No newline at end of file