| 1 | // https://d3js.org/d3-drag/ v3.0.0 Copyright 2010-2021 Mike Bostock
|
|---|
| 2 | (function (global, factory) {
|
|---|
| 3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-dispatch'), require('d3-selection')) :
|
|---|
| 4 | typeof define === 'function' && define.amd ? define(['exports', 'd3-dispatch', 'd3-selection'], factory) :
|
|---|
| 5 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}, global.d3, global.d3));
|
|---|
| 6 | }(this, (function (exports, d3Dispatch, d3Selection) { 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | // These are typically used in conjunction with noevent to ensure that we can
|
|---|
| 9 | // preventDefault on the event.
|
|---|
| 10 | const nonpassive = {passive: false};
|
|---|
| 11 | const nonpassivecapture = {capture: true, passive: false};
|
|---|
| 12 |
|
|---|
| 13 | function nopropagation(event) {
|
|---|
| 14 | event.stopImmediatePropagation();
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | function noevent(event) {
|
|---|
| 18 | event.preventDefault();
|
|---|
| 19 | event.stopImmediatePropagation();
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | function nodrag(view) {
|
|---|
| 23 | var root = view.document.documentElement,
|
|---|
| 24 | selection = d3Selection.select(view).on("dragstart.drag", noevent, nonpassivecapture);
|
|---|
| 25 | if ("onselectstart" in root) {
|
|---|
| 26 | selection.on("selectstart.drag", noevent, nonpassivecapture);
|
|---|
| 27 | } else {
|
|---|
| 28 | root.__noselect = root.style.MozUserSelect;
|
|---|
| 29 | root.style.MozUserSelect = "none";
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | function yesdrag(view, noclick) {
|
|---|
| 34 | var root = view.document.documentElement,
|
|---|
| 35 | selection = d3Selection.select(view).on("dragstart.drag", null);
|
|---|
| 36 | if (noclick) {
|
|---|
| 37 | selection.on("click.drag", noevent, nonpassivecapture);
|
|---|
| 38 | setTimeout(function() { selection.on("click.drag", null); }, 0);
|
|---|
| 39 | }
|
|---|
| 40 | if ("onselectstart" in root) {
|
|---|
| 41 | selection.on("selectstart.drag", null);
|
|---|
| 42 | } else {
|
|---|
| 43 | root.style.MozUserSelect = root.__noselect;
|
|---|
| 44 | delete root.__noselect;
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | var constant = x => () => x;
|
|---|
| 49 |
|
|---|
| 50 | function DragEvent(type, {
|
|---|
| 51 | sourceEvent,
|
|---|
| 52 | subject,
|
|---|
| 53 | target,
|
|---|
| 54 | identifier,
|
|---|
| 55 | active,
|
|---|
| 56 | x, y, dx, dy,
|
|---|
| 57 | dispatch
|
|---|
| 58 | }) {
|
|---|
| 59 | Object.defineProperties(this, {
|
|---|
| 60 | type: {value: type, enumerable: true, configurable: true},
|
|---|
| 61 | sourceEvent: {value: sourceEvent, enumerable: true, configurable: true},
|
|---|
| 62 | subject: {value: subject, enumerable: true, configurable: true},
|
|---|
| 63 | target: {value: target, enumerable: true, configurable: true},
|
|---|
| 64 | identifier: {value: identifier, enumerable: true, configurable: true},
|
|---|
| 65 | active: {value: active, enumerable: true, configurable: true},
|
|---|
| 66 | x: {value: x, enumerable: true, configurable: true},
|
|---|
| 67 | y: {value: y, enumerable: true, configurable: true},
|
|---|
| 68 | dx: {value: dx, enumerable: true, configurable: true},
|
|---|
| 69 | dy: {value: dy, enumerable: true, configurable: true},
|
|---|
| 70 | _: {value: dispatch}
|
|---|
| 71 | });
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | DragEvent.prototype.on = function() {
|
|---|
| 75 | var value = this._.on.apply(this._, arguments);
|
|---|
| 76 | return value === this._ ? this : value;
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | // Ignore right-click, since that should open the context menu.
|
|---|
| 80 | function defaultFilter(event) {
|
|---|
| 81 | return !event.ctrlKey && !event.button;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | function defaultContainer() {
|
|---|
| 85 | return this.parentNode;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | function defaultSubject(event, d) {
|
|---|
| 89 | return d == null ? {x: event.x, y: event.y} : d;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | function defaultTouchable() {
|
|---|
| 93 | return navigator.maxTouchPoints || ("ontouchstart" in this);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | function drag() {
|
|---|
| 97 | var filter = defaultFilter,
|
|---|
| 98 | container = defaultContainer,
|
|---|
| 99 | subject = defaultSubject,
|
|---|
| 100 | touchable = defaultTouchable,
|
|---|
| 101 | gestures = {},
|
|---|
| 102 | listeners = d3Dispatch.dispatch("start", "drag", "end"),
|
|---|
| 103 | active = 0,
|
|---|
| 104 | mousedownx,
|
|---|
| 105 | mousedowny,
|
|---|
| 106 | mousemoving,
|
|---|
| 107 | touchending,
|
|---|
| 108 | clickDistance2 = 0;
|
|---|
| 109 |
|
|---|
| 110 | function drag(selection) {
|
|---|
| 111 | selection
|
|---|
| 112 | .on("mousedown.drag", mousedowned)
|
|---|
| 113 | .filter(touchable)
|
|---|
| 114 | .on("touchstart.drag", touchstarted)
|
|---|
| 115 | .on("touchmove.drag", touchmoved, nonpassive)
|
|---|
| 116 | .on("touchend.drag touchcancel.drag", touchended)
|
|---|
| 117 | .style("touch-action", "none")
|
|---|
| 118 | .style("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | function mousedowned(event, d) {
|
|---|
| 122 | if (touchending || !filter.call(this, event, d)) return;
|
|---|
| 123 | var gesture = beforestart(this, container.call(this, event, d), event, d, "mouse");
|
|---|
| 124 | if (!gesture) return;
|
|---|
| 125 | d3Selection.select(event.view)
|
|---|
| 126 | .on("mousemove.drag", mousemoved, nonpassivecapture)
|
|---|
| 127 | .on("mouseup.drag", mouseupped, nonpassivecapture);
|
|---|
| 128 | nodrag(event.view);
|
|---|
| 129 | nopropagation(event);
|
|---|
| 130 | mousemoving = false;
|
|---|
| 131 | mousedownx = event.clientX;
|
|---|
| 132 | mousedowny = event.clientY;
|
|---|
| 133 | gesture("start", event);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | function mousemoved(event) {
|
|---|
| 137 | noevent(event);
|
|---|
| 138 | if (!mousemoving) {
|
|---|
| 139 | var dx = event.clientX - mousedownx, dy = event.clientY - mousedowny;
|
|---|
| 140 | mousemoving = dx * dx + dy * dy > clickDistance2;
|
|---|
| 141 | }
|
|---|
| 142 | gestures.mouse("drag", event);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | function mouseupped(event) {
|
|---|
| 146 | d3Selection.select(event.view).on("mousemove.drag mouseup.drag", null);
|
|---|
| 147 | yesdrag(event.view, mousemoving);
|
|---|
| 148 | noevent(event);
|
|---|
| 149 | gestures.mouse("end", event);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | function touchstarted(event, d) {
|
|---|
| 153 | if (!filter.call(this, event, d)) return;
|
|---|
| 154 | var touches = event.changedTouches,
|
|---|
| 155 | c = container.call(this, event, d),
|
|---|
| 156 | n = touches.length, i, gesture;
|
|---|
| 157 |
|
|---|
| 158 | for (i = 0; i < n; ++i) {
|
|---|
| 159 | if (gesture = beforestart(this, c, event, d, touches[i].identifier, touches[i])) {
|
|---|
| 160 | nopropagation(event);
|
|---|
| 161 | gesture("start", event, touches[i]);
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | function touchmoved(event) {
|
|---|
| 167 | var touches = event.changedTouches,
|
|---|
| 168 | n = touches.length, i, gesture;
|
|---|
| 169 |
|
|---|
| 170 | for (i = 0; i < n; ++i) {
|
|---|
| 171 | if (gesture = gestures[touches[i].identifier]) {
|
|---|
| 172 | noevent(event);
|
|---|
| 173 | gesture("drag", event, touches[i]);
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | function touchended(event) {
|
|---|
| 179 | var touches = event.changedTouches,
|
|---|
| 180 | n = touches.length, i, gesture;
|
|---|
| 181 |
|
|---|
| 182 | if (touchending) clearTimeout(touchending);
|
|---|
| 183 | touchending = setTimeout(function() { touchending = null; }, 500); // Ghost clicks are delayed!
|
|---|
| 184 | for (i = 0; i < n; ++i) {
|
|---|
| 185 | if (gesture = gestures[touches[i].identifier]) {
|
|---|
| 186 | nopropagation(event);
|
|---|
| 187 | gesture("end", event, touches[i]);
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | function beforestart(that, container, event, d, identifier, touch) {
|
|---|
| 193 | var dispatch = listeners.copy(),
|
|---|
| 194 | p = d3Selection.pointer(touch || event, container), dx, dy,
|
|---|
| 195 | s;
|
|---|
| 196 |
|
|---|
| 197 | if ((s = subject.call(that, new DragEvent("beforestart", {
|
|---|
| 198 | sourceEvent: event,
|
|---|
| 199 | target: drag,
|
|---|
| 200 | identifier,
|
|---|
| 201 | active,
|
|---|
| 202 | x: p[0],
|
|---|
| 203 | y: p[1],
|
|---|
| 204 | dx: 0,
|
|---|
| 205 | dy: 0,
|
|---|
| 206 | dispatch
|
|---|
| 207 | }), d)) == null) return;
|
|---|
| 208 |
|
|---|
| 209 | dx = s.x - p[0] || 0;
|
|---|
| 210 | dy = s.y - p[1] || 0;
|
|---|
| 211 |
|
|---|
| 212 | return function gesture(type, event, touch) {
|
|---|
| 213 | var p0 = p, n;
|
|---|
| 214 | switch (type) {
|
|---|
| 215 | case "start": gestures[identifier] = gesture, n = active++; break;
|
|---|
| 216 | case "end": delete gestures[identifier], --active; // falls through
|
|---|
| 217 | case "drag": p = d3Selection.pointer(touch || event, container), n = active; break;
|
|---|
| 218 | }
|
|---|
| 219 | dispatch.call(
|
|---|
| 220 | type,
|
|---|
| 221 | that,
|
|---|
| 222 | new DragEvent(type, {
|
|---|
| 223 | sourceEvent: event,
|
|---|
| 224 | subject: s,
|
|---|
| 225 | target: drag,
|
|---|
| 226 | identifier,
|
|---|
| 227 | active: n,
|
|---|
| 228 | x: p[0] + dx,
|
|---|
| 229 | y: p[1] + dy,
|
|---|
| 230 | dx: p[0] - p0[0],
|
|---|
| 231 | dy: p[1] - p0[1],
|
|---|
| 232 | dispatch
|
|---|
| 233 | }),
|
|---|
| 234 | d
|
|---|
| 235 | );
|
|---|
| 236 | };
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | drag.filter = function(_) {
|
|---|
| 240 | return arguments.length ? (filter = typeof _ === "function" ? _ : constant(!!_), drag) : filter;
|
|---|
| 241 | };
|
|---|
| 242 |
|
|---|
| 243 | drag.container = function(_) {
|
|---|
| 244 | return arguments.length ? (container = typeof _ === "function" ? _ : constant(_), drag) : container;
|
|---|
| 245 | };
|
|---|
| 246 |
|
|---|
| 247 | drag.subject = function(_) {
|
|---|
| 248 | return arguments.length ? (subject = typeof _ === "function" ? _ : constant(_), drag) : subject;
|
|---|
| 249 | };
|
|---|
| 250 |
|
|---|
| 251 | drag.touchable = function(_) {
|
|---|
| 252 | return arguments.length ? (touchable = typeof _ === "function" ? _ : constant(!!_), drag) : touchable;
|
|---|
| 253 | };
|
|---|
| 254 |
|
|---|
| 255 | drag.on = function() {
|
|---|
| 256 | var value = listeners.on.apply(listeners, arguments);
|
|---|
| 257 | return value === listeners ? drag : value;
|
|---|
| 258 | };
|
|---|
| 259 |
|
|---|
| 260 | drag.clickDistance = function(_) {
|
|---|
| 261 | return arguments.length ? (clickDistance2 = (_ = +_) * _, drag) : Math.sqrt(clickDistance2);
|
|---|
| 262 | };
|
|---|
| 263 |
|
|---|
| 264 | return drag;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | exports.drag = drag;
|
|---|
| 268 | exports.dragDisable = nodrag;
|
|---|
| 269 | exports.dragEnable = yesdrag;
|
|---|
| 270 |
|
|---|
| 271 | Object.defineProperty(exports, '__esModule', { value: true });
|
|---|
| 272 |
|
|---|
| 273 | })));
|
|---|