Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.06 KB | None | 0 0
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-dispatch'), require('d3-drag'), require('d3-interpolate'), require('d3-selection'), require('d3-transition')) :
  3. typeof define === 'function' && define.amd ? define(['exports', 'd3-dispatch', 'd3-drag', 'd3-interpolate', 'd3-selection', 'd3-transition'], factory) :
  4. (factory((global.d3 = global.d3 || {}),global.d3,global.d3,global.d3,global.d3,global.d3));
  5. }(this, (function (exports,d3Dispatch,d3Drag,d3Interpolate,d3Selection,d3Transition) { 'use strict';
  6.  
  7. var constant = function(x) {
  8. return function() {
  9. return x;
  10. };
  11. };
  12.  
  13. var BrushEvent = function(target, type, selection) {
  14. this.target = target;
  15. this.type = type;
  16. this.selection = selection;
  17. };
  18.  
  19. function nopropagation() {
  20. d3Selection.event.stopImmediatePropagation();
  21. }
  22.  
  23. var noevent = function() {
  24. d3Selection.event.preventDefault();
  25. d3Selection.event.stopImmediatePropagation();
  26. };
  27.  
  28. var MODE_DRAG = {name: "drag"};
  29. var MODE_SPACE = {name: "space"};
  30. var MODE_HANDLE = {name: "handle"};
  31. var MODE_CENTER = {name: "center"};
  32.  
  33. var X = {
  34. name: "x",
  35. handles: ["e", "w"].map(type),
  36. input: function(x, e) { return x && [[x[0], e[0][1]], [x[1], e[1][1]]]; },
  37. output: function(xy) { return xy && [xy[0][0], xy[1][0]]; }
  38. };
  39.  
  40. var Y = {
  41. name: "y",
  42. handles: ["n", "s"].map(type),
  43. input: function(y, e) { return y && [[e[0][0], y[0]], [e[1][0], y[1]]]; },
  44. output: function(xy) { return xy && [xy[0][1], xy[1][1]]; }
  45. };
  46.  
  47. var XY = {
  48. name: "xy",
  49. handles: ["n", "e", "s", "w", "nw", "ne", "se", "sw"].map(type),
  50. input: function(xy) { return xy; },
  51. output: function(xy) { return xy; }
  52. };
  53.  
  54. var cursors = {
  55. overlay: "crosshair",
  56. selection: "move",
  57. n: "ns-resize",
  58. e: "ew-resize",
  59. s: "ns-resize",
  60. w: "ew-resize",
  61. nw: "nwse-resize",
  62. ne: "nesw-resize",
  63. se: "nwse-resize",
  64. sw: "nesw-resize"
  65. };
  66.  
  67. var flipX = {
  68. e: "w",
  69. w: "e",
  70. nw: "ne",
  71. ne: "nw",
  72. se: "sw",
  73. sw: "se"
  74. };
  75.  
  76. var flipY = {
  77. n: "s",
  78. s: "n",
  79. nw: "sw",
  80. ne: "se",
  81. se: "ne",
  82. sw: "nw"
  83. };
  84.  
  85. var signsX = {
  86. overlay: +1,
  87. selection: +1,
  88. n: null,
  89. e: +1,
  90. s: null,
  91. w: -1,
  92. nw: -1,
  93. ne: +1,
  94. se: +1,
  95. sw: -1
  96. };
  97.  
  98. var signsY = {
  99. overlay: +1,
  100. selection: +1,
  101. n: -1,
  102. e: null,
  103. s: +1,
  104. w: null,
  105. nw: -1,
  106. ne: -1,
  107. se: +1,
  108. sw: +1
  109. };
  110.  
  111. function type(t) {
  112. return {type: t};
  113. }
  114.  
  115. // Ignore right-click, since that should open the context menu.
  116. function defaultFilter() {
  117. return !d3Selection.event.button;
  118. }
  119.  
  120. function defaultExtent() {
  121. var svg = this.ownerSVGElement || this;
  122. return [[0, 0], [svg.width.baseVal.value, svg.height.baseVal.value]];
  123. }
  124.  
  125. // Like d3.local, but with the name “__brush” rather than auto-generated.
  126. function local(node) {
  127. while (!node.__brush) if (!(node = node.parentNode)) return;
  128. return node.__brush;
  129. }
  130.  
  131. function empty(extent) {
  132. return extent[0][0] === extent[1][0]
  133. || extent[0][1] === extent[1][1];
  134. }
  135.  
  136. function brushSelection(node) {
  137. var state = node.__brush;
  138. return state ? state.dim.output(state.selection) : null;
  139. }
  140.  
  141. function brushX() {
  142. return brush$1(X);
  143. }
  144.  
  145. function brushY() {
  146. return brush$1(Y);
  147. }
  148.  
  149. var brush = function() {
  150. return brush$1(XY);
  151. };
  152.  
  153. function brush$1(dim) {
  154. var extent = defaultExtent,
  155. filter = defaultFilter,
  156. listeners = d3Dispatch.dispatch(brush, "start", "brush", "end"),
  157. handleSize = 6,
  158. touchending;
  159.  
  160. function brush(group) {
  161.  
  162. var overlay = group
  163. .property("__brush", initialize)
  164. .selectAll(".overlay")
  165. .data([type("overlay")]);
  166.  
  167. overlay.enter().append("rect")
  168. .attr("class", "overlay")
  169. .attr("pointer-events", "all")
  170. .attr("cursor", cursors.overlay)
  171. .merge(overlay)
  172. .each(function() {
  173. var extent = local(this).extent;
  174. d3Selection.select(this)
  175. .attr("x", extent[0][0])
  176. .attr("y", extent[0][1])
  177. .attr("width", extent[1][0] - extent[0][0])
  178. .attr("height", extent[1][1] - extent[0][1]);
  179. });
  180.  
  181. group.selectAll(".selection")
  182. .data([type("selection")])
  183. .enter().append("rect")
  184. .attr("class", "selection")
  185. .attr("cursor", cursors.selection)
  186. .attr("fill", "#777")
  187. .attr("fill-opacity", 0.3)
  188. .attr("stroke", "#fff")
  189. .attr("shape-rendering", "crispEdges");
  190.  
  191. var handle = group.selectAll(".handle")
  192. .data(dim.handles, function(d) { return d.type; });
  193.  
  194. handle.exit().remove();
  195.  
  196. handle.enter().append("rect")
  197. .attr("class", function(d) { return "handle handle--" + d.type; })
  198. .attr("cursor", function(d) { return cursors[d.type]; });
  199.  
  200. group
  201. .each(redraw)
  202. .attr("fill", "none")
  203. .attr("pointer-events", "all")
  204. .style("-webkit-tap-highlight-color", "rgba(0,0,0,0)")
  205. .on("mousedown.brush touchstart.brush", started);
  206. }
  207.  
  208. brush.move = function(group, selection) {
  209. if (group.selection) {
  210. group
  211. .on("start.brush", function() { emitter(this, arguments).beforestart().start(); })
  212. .on("interrupt.brush end.brush", function() { emitter(this, arguments).end(); })
  213. .tween("brush", function() {
  214. var that = this,
  215. state = that.__brush,
  216. emit = emitter(that, arguments),
  217. selection0 = state.selection,
  218. selection1 = dim.input(typeof selection === "function" ? selection.apply(this, arguments) : selection, state.extent),
  219. i = d3Interpolate.interpolate(selection0, selection1);
  220.  
  221. function tween(t) {
  222. state.selection = t === 1 && empty(selection1) ? null : i(t);
  223. redraw.call(that);
  224. emit.brush();
  225. }
  226.  
  227. return selection0 && selection1 ? tween : tween(1);
  228. });
  229. } else {
  230. group
  231. .each(function() {
  232. var that = this,
  233. args = arguments,
  234. state = that.__brush,
  235. selection1 = dim.input(typeof selection === "function" ? selection.apply(that, args) : selection, state.extent),
  236. emit = emitter(that, args).beforestart();
  237.  
  238. d3Transition.interrupt(that);
  239. state.selection = selection1 == null || empty(selection1) ? null : selection1;
  240. redraw.call(that);
  241. emit.start().brush().end();
  242. });
  243. }
  244. };
  245.  
  246. function redraw() {
  247. var group = d3Selection.select(this),
  248. selection = local(this).selection;
  249.  
  250. if (selection) {
  251. group.selectAll(".selection")
  252. .style("display", null)
  253. .attr("x", selection[0][0])
  254. .attr("y", selection[0][1])
  255. .attr("width", selection[1][0] - selection[0][0])
  256. .attr("height", selection[1][1] - selection[0][1]);
  257.  
  258. group.selectAll(".handle")
  259. .style("display", null)
  260. .attr("x", function(d) { return d.type[d.type.length - 1] === "e" ? selection[1][0] - handleSize / 2 : selection[0][0] - handleSize / 2; })
  261. .attr("y", function(d) { return d.type[0] === "s" ? selection[1][1] - handleSize / 2 : selection[0][1] - handleSize / 2; })
  262. .attr("width", function(d) { return d.type === "n" || d.type === "s" ? selection[1][0] - selection[0][0] + handleSize : handleSize; })
  263. .attr("height", function(d) { return d.type === "e" || d.type === "w" ? selection[1][1] - selection[0][1] + handleSize : handleSize; });
  264. }
  265.  
  266. else {
  267. group.selectAll(".selection,.handle")
  268. .style("display", "none")
  269. .attr("x", null)
  270. .attr("y", null)
  271. .attr("width", null)
  272. .attr("height", null);
  273. }
  274. }
  275.  
  276. function emitter(that, args) {
  277. return that.__brush.emitter || new Emitter(that, args);
  278. }
  279.  
  280. function Emitter(that, args) {
  281. this.that = that;
  282. this.args = args;
  283. this.state = that.__brush;
  284. this.active = 0;
  285. }
  286.  
  287. Emitter.prototype = {
  288. beforestart: function() {
  289. if (++this.active === 1) this.state.emitter = this, this.starting = true;
  290. return this;
  291. },
  292. start: function() {
  293. if (this.starting) this.starting = false, this.emit("start");
  294. return this;
  295. },
  296. brush: function() {
  297. this.emit("brush");
  298. return this;
  299. },
  300. end: function() {
  301. if (--this.active === 0) delete this.state.emitter, this.emit("end");
  302. return this;
  303. },
  304. emit: function(type) {
  305. d3Selection.customEvent(new BrushEvent(brush, type, dim.output(this.state.selection)), listeners.apply, listeners, [type, this.that, this.args]);
  306. }
  307. };
  308.  
  309. function started() {
  310. if (d3Selection.event.touches) { if (d3Selection.event.changedTouches.length < d3Selection.event.touches.length) return noevent(); }
  311. else if (touchending) return;
  312. if (!filter.apply(this, arguments)) return;
  313.  
  314. var that = this,
  315. type = d3Selection.event.target.__data__.type,
  316. mode = (d3Selection.event.metaKey ? type = "overlay" : type) === "selection" ? MODE_DRAG : (d3Selection.event.altKey ? MODE_CENTER : MODE_HANDLE),
  317. signX = dim === Y ? null : signsX[type],
  318. signY = dim === X ? null : signsY[type],
  319. state = local(that),
  320. extent = state.extent,
  321. selection = state.selection,
  322. W = extent[0][0], w0, w1,
  323. N = extent[0][1], n0, n1,
  324. E = extent[1][0], e0, e1,
  325. S = extent[1][1], s0, s1,
  326. dx,
  327. dy,
  328. moving,
  329. lockX,
  330. lockY,
  331. point0 = d3Selection.mouse(that),
  332. point = point0,
  333. emit = emitter(that, arguments).beforestart();
  334.  
  335. if (type === "overlay") {
  336. state.selection = selection = [
  337. [w0 = dim === Y ? W : point0[0], n0 = dim === X ? N : point0[1]],
  338. [e0 = dim === Y ? E : w0, s0 = dim === X ? S : n0]
  339. ];
  340. } else {
  341. w0 = selection[0][0];
  342. n0 = selection[0][1];
  343. e0 = selection[1][0];
  344. s0 = selection[1][1];
  345. }
  346.  
  347. w1 = w0;
  348. n1 = n0;
  349. e1 = e0;
  350. s1 = s0;
  351.  
  352. var group = d3Selection.select(that)
  353. .attr("pointer-events", "none");
  354.  
  355. var overlay = group.selectAll(".overlay")
  356. .attr("cursor", cursors[type]);
  357.  
  358. if (d3Selection.event.touches) {
  359. group
  360. .on("touchmove.brush", moved, true)
  361. .on("touchend.brush touchcancel.brush", ended, true);
  362. } else {
  363. var view = d3Selection.select(d3Selection.event.view)
  364. .on("keydown.brush", keydowned, true)
  365. .on("keyup.brush", keyupped, true)
  366. .on("mousemove.brush", moved, true)
  367. .on("mouseup.brush", ended, true);
  368.  
  369. d3Drag.dragDisable(d3Selection.event.view);
  370. }
  371.  
  372. nopropagation();
  373. d3Transition.interrupt(that);
  374. redraw.call(that);
  375. emit.start();
  376.  
  377. function moved() {
  378. var point1 = d3Selection.mouse(that);
  379. point = point1;
  380. moving = true;
  381. noevent();
  382. move();
  383. }
  384.  
  385. function move() {
  386. var t;
  387.  
  388. dx = point[0] - point0[0];
  389. dy = point[1] - point0[1];
  390.  
  391. switch (mode) {
  392. case MODE_SPACE:
  393. case MODE_DRAG: {
  394. if (signX) dx = Math.max(W - w0, Math.min(E - e0, dx)), w1 = w0 + dx, e1 = e0 + dx;
  395. if (signY) dy = Math.max(N - n0, Math.min(S - s0, dy)), n1 = n0 + dy, s1 = s0 + dy;
  396. break;
  397. }
  398. case MODE_HANDLE: {
  399. if (signX < 0) dx = Math.max(W - w0, Math.min(E - w0, dx)), w1 = w0 + dx, e1 = e0;
  400. else if (signX > 0) dx = Math.max(W - e0, Math.min(E - e0, dx)), w1 = w0, e1 = e0 + dx;
  401. if (signY < 0) dy = Math.max(N - n0, Math.min(S - n0, dy)), n1 = n0 + dy, s1 = s0;
  402. else if (signY > 0) dy = Math.max(N - s0, Math.min(S - s0, dy)), n1 = n0, s1 = s0 + dy;
  403. break;
  404. }
  405. case MODE_CENTER: {
  406. if (signX) w1 = Math.max(W, Math.min(E, w0 - dx * signX)), e1 = Math.max(W, Math.min(E, e0 + dx * signX));
  407. if (signY) n1 = Math.max(N, Math.min(S, n0 - dy * signY)), s1 = Math.max(N, Math.min(S, s0 + dy * signY));
  408. break;
  409. }
  410. }
  411.  
  412. if (e1 < w1) {
  413. signX *= -1;
  414. t = w0, w0 = e0, e0 = t;
  415. t = w1, w1 = e1, e1 = t;
  416. if (type in flipX) overlay.attr("cursor", cursors[type = flipX[type]]);
  417. }
  418.  
  419. if (s1 < n1) {
  420. signY *= -1;
  421. t = n0, n0 = s0, s0 = t;
  422. t = n1, n1 = s1, s1 = t;
  423. if (type in flipY) overlay.attr("cursor", cursors[type = flipY[type]]);
  424. }
  425.  
  426. if (state.selection) selection = state.selection; // May be set by brush.move!
  427. if (lockX) w1 = selection[0][0], e1 = selection[1][0];
  428. if (lockY) n1 = selection[0][1], s1 = selection[1][1];
  429.  
  430. if (selection[0][0] !== w1
  431. || selection[0][1] !== n1
  432. || selection[1][0] !== e1
  433. || selection[1][1] !== s1) {
  434. state.selection = [[w1, n1], [e1, s1]];
  435. redraw.call(that);
  436. emit.brush();
  437. }
  438. }
  439.  
  440. function ended() {
  441. nopropagation();
  442. if (d3Selection.event.touches) {
  443. if (d3Selection.event.touches.length) return;
  444. if (touchending) clearTimeout(touchending);
  445. touchending = setTimeout(function() { touchending = null; }, 500); // Ghost clicks are delayed!
  446. group.on("touchmove.brush touchend.brush touchcancel.brush", null);
  447. } else {
  448. d3Drag.dragEnable(d3Selection.event.view, moving);
  449. view.on("keydown.brush keyup.brush mousemove.brush mouseup.brush", null);
  450. }
  451. group.attr("pointer-events", "all");
  452. overlay.attr("cursor", cursors.overlay);
  453. if (state.selection) selection = state.selection; // May be set by brush.move (on start)!
  454. if (empty(selection)) state.selection = null, redraw.call(that);
  455. emit.end();
  456. }
  457.  
  458. function keydowned() {
  459. switch (d3Selection.event.keyCode) {
  460. case 18: { // ALT
  461. if (mode === MODE_HANDLE) {
  462. if (signX) e0 = e1 - dx * signX, w0 = w1 + dx * signX;
  463. if (signY) s0 = s1 - dy * signY, n0 = n1 + dy * signY;
  464. mode = MODE_CENTER;
  465. move();
  466. }
  467. break;
  468. }
  469. case 32: { // SPACE; takes priority over ALT
  470. if (mode === MODE_HANDLE || mode === MODE_CENTER) {
  471. if (signX < 0) e0 = e1 - dx; else if (signX > 0) w0 = w1 - dx;
  472. if (signY < 0) s0 = s1 - dy; else if (signY > 0) n0 = n1 - dy;
  473. mode = MODE_SPACE;
  474. overlay.attr("cursor", cursors.selection);
  475. move();
  476. }
  477. break;
  478. }
  479. default: return;
  480. }
  481. noevent();
  482. }
  483.  
  484. function keyupped() {
  485. switch (d3Selection.event.keyCode) {
  486. case 18: { // ALT
  487. if (mode === MODE_CENTER) {
  488. if (signX < 0) e0 = e1; else if (signX > 0) w0 = w1;
  489. if (signY < 0) s0 = s1; else if (signY > 0) n0 = n1;
  490. mode = MODE_HANDLE;
  491. move();
  492. }
  493. break;
  494. }
  495. case 32: { // SPACE
  496. if (mode === MODE_SPACE) {
  497. if (d3Selection.event.altKey) {
  498. if (signX) e0 = e1 - dx * signX, w0 = w1 + dx * signX;
  499. if (signY) s0 = s1 - dy * signY, n0 = n1 + dy * signY;
  500. mode = MODE_CENTER;
  501. } else {
  502. if (signX < 0) e0 = e1; else if (signX > 0) w0 = w1;
  503. if (signY < 0) s0 = s1; else if (signY > 0) n0 = n1;
  504. mode = MODE_HANDLE;
  505. }
  506. overlay.attr("cursor", cursors[type]);
  507. move();
  508. }
  509. break;
  510. }
  511. default: return;
  512. }
  513. noevent();
  514. }
  515. }
  516.  
  517. function initialize() {
  518. var state = this.__brush || {selection: null};
  519. state.extent = extent.apply(this, arguments);
  520. state.dim = dim;
  521. return state;
  522. }
  523.  
  524. brush.extent = function(_) {
  525. return arguments.length ? (extent = typeof _ === "function" ? _ : constant([[+_[0][0], +_[0][1]], [+_[1][0], +_[1][1]]]), brush) : extent;
  526. };
  527.  
  528. brush.filter = function(_) {
  529. return arguments.length ? (filter = typeof _ === "function" ? _ : constant(!!_), brush) : filter;
  530. };
  531.  
  532. brush.handleSize = function(_) {
  533. return arguments.length ? (handleSize = +_, brush) : handleSize;
  534. };
  535.  
  536. brush.on = function() {
  537. var value = listeners.on.apply(listeners, arguments);
  538. return value === listeners ? brush : value;
  539. };
  540.  
  541. return brush;
  542. }
  543.  
  544. exports.brush = brush;
  545. exports.brushX = brushX;
  546. exports.brushY = brushY;
  547. exports.brushSelection = brushSelection;
  548.  
  549. Object.defineProperty(exports, '__esModule', { value: true });
  550.  
  551. })));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement