This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
d3-brush.v1.min.js
2 lines (2 loc) · 8.92 KB
/
d3-brush.v1.min.js
1
2
// https://d3js.org/d3-brush/ v1.1.6 Copyright 2020 Mike Bostock
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("d3-dispatch"),require("d3-drag"),require("d3-interpolate"),require("d3-selection"),require("d3-transition")):"function"==typeof define&&define.amd?define(["exports","d3-dispatch","d3-drag","d3-interpolate","d3-selection","d3-transition"],e):e((t=t||self).d3=t.d3||{},t.d3,t.d3,t.d3,t.d3,t.d3)}(this,function(t,e,n,r,i,s){"use strict";function a(t){return function(){return t}}function u(t,e,n){this.target=t,this.type=e,this.selection=n}function o(){i.event.stopImmediatePropagation()}function l(){i.event.preventDefault(),i.event.stopImmediatePropagation()}var c={name:"drag"},h={name:"space"},f={name:"handle"},d={name:"center"};function p(t){return[+t[0],+t[1]]}function v(t){return[p(t[0]),p(t[1])]}var y={name:"x",handles:["w","e"].map(k),input:function(t,e){return null==t?null:[[+t[0],e[0][1]],[+t[1],e[1][1]]]},output:function(t){return t&&[t[0][0],t[1][0]]}},m={name:"y",handles:["n","s"].map(k),input:function(t,e){return null==t?null:[[e[0][0],+t[0]],[e[1][0],+t[1]]]},output:function(t){return t&&[t[0][1],t[1][1]]}},b={name:"xy",handles:["n","w","e","s","nw","ne","sw","se"].map(k),input:function(t){return null==t?null:v(t)},output:function(t){return t}},w={overlay:"crosshair",selection:"move",n:"ns-resize",e:"ew-resize",s:"ns-resize",w:"ew-resize",nw:"nwse-resize",ne:"nesw-resize",se:"nwse-resize",sw:"nesw-resize"},g={e:"w",w:"e",nw:"ne",ne:"nw",se:"sw",sw:"se"},x={n:"s",s:"n",nw:"sw",ne:"se",se:"ne",sw:"nw"},M={overlay:1,selection:1,n:null,e:1,s:null,w:-1,nw:-1,ne:1,se:1,sw:-1},_={overlay:1,selection:1,n:-1,e:null,s:1,w:null,nw:-1,ne:-1,se:1,sw:1};function k(t){return{type:t}}function z(){return!i.event.ctrlKey&&!i.event.button}function A(){var t=this.ownerSVGElement||this;return t.hasAttribute("viewBox")?[[(t=t.viewBox.baseVal).x,t.y],[t.x+t.width,t.y+t.height]]:[[0,0],[t.width.baseVal.value,t.height.baseVal.value]]}function q(){return navigator.maxTouchPoints||"ontouchstart"in this}function K(t){for(;!t.__brush;)if(!(t=t.parentNode))return;return t.__brush}function E(t){var p,b=A,E=z,P=q,T=!0,V=e.dispatch("start","brush","end"),S=6;function j(e){var n=e.property("__brush",O).selectAll(".overlay").data([k("overlay")]);n.enter().append("rect").attr("class","overlay").attr("pointer-events","all").attr("cursor",w.overlay).merge(n).each(function(){var t=K(this).extent;i.select(this).attr("x",t[0][0]).attr("y",t[0][1]).attr("width",t[1][0]-t[0][0]).attr("height",t[1][1]-t[0][1])}),e.selectAll(".selection").data([k("selection")]).enter().append("rect").attr("class","selection").attr("cursor",w.selection).attr("fill","#777").attr("fill-opacity",.3).attr("stroke","#fff").attr("shape-rendering","crispEdges");var r=e.selectAll(".handle").data(t.handles,function(t){return t.type});r.exit().remove(),r.enter().append("rect").attr("class",function(t){return"handle handle--"+t.type}).attr("cursor",function(t){return w[t.type]}),e.each(B).attr("fill","none").attr("pointer-events","all").on("mousedown.brush",I).filter(P).on("touchstart.brush",I).on("touchmove.brush",G).on("touchend.brush touchcancel.brush",N).style("touch-action","none").style("-webkit-tap-highlight-color","rgba(0,0,0,0)")}function B(){var t=i.select(this),e=K(this).selection;e?(t.selectAll(".selection").style("display",null).attr("x",e[0][0]).attr("y",e[0][1]).attr("width",e[1][0]-e[0][0]).attr("height",e[1][1]-e[0][1]),t.selectAll(".handle").style("display",null).attr("x",function(t){return"e"===t.type[t.type.length-1]?e[1][0]-S/2:e[0][0]-S/2}).attr("y",function(t){return"s"===t.type[0]?e[1][1]-S/2:e[0][1]-S/2}).attr("width",function(t){return"n"===t.type||"s"===t.type?e[1][0]-e[0][0]+S:S}).attr("height",function(t){return"e"===t.type||"w"===t.type?e[1][1]-e[0][1]+S:S})):t.selectAll(".selection,.handle").style("display","none").attr("x",null).attr("y",null).attr("width",null).attr("height",null)}function C(t,e,n){var r=t.__brush.emitter;return!r||n&&r.clean?new D(t,e,n):r}function D(t,e,n){this.that=t,this.args=e,this.state=t.__brush,this.active=0,this.clean=n}function I(){if((!p||i.event.touches)&&E.apply(this,arguments)){var e,r,a,u,v,b,k,z,A,q,P,V,S=this,j=i.event.target.__data__.type,D="selection"===(T&&i.event.metaKey?j="overlay":j)?c:T&&i.event.altKey?d:f,I=t===m?null:M[j],G=t===y?null:_[j],N=K(S),O=N.extent,X=N.selection,Y=O[0][0],F=O[0][1],H=O[1][0],J=O[1][1],L=0,Q=0,R=I&&G&&T&&i.event.shiftKey,U=i.event.touches?(V=i.event.changedTouches[0].identifier,function(t){return i.touch(t,i.event.touches,V)}):i.mouse,W=U(S),Z=W,$=C(S,arguments,!0).beforestart();"overlay"===j?(X&&(A=!0),N.selection=X=[[e=t===m?Y:W[0],a=t===y?F:W[1]],[v=t===m?H:e,k=t===y?J:a]]):(e=X[0][0],a=X[0][1],v=X[1][0],k=X[1][1]),r=e,u=a,b=v,z=k;var tt=i.select(S).attr("pointer-events","none"),et=tt.selectAll(".overlay").attr("cursor",w[j]);if(i.event.touches)$.moved=rt,$.ended=st;else{var nt=i.select(i.event.view).on("mousemove.brush",rt,!0).on("mouseup.brush",st,!0);T&&nt.on("keydown.brush",function(){switch(i.event.keyCode){case 16:R=I&&G;break;case 18:D===f&&(I&&(v=b-L*I,e=r+L*I),G&&(k=z-Q*G,a=u+Q*G),D=d,it());break;case 32:D!==f&&D!==d||(I<0?v=b-L:I>0&&(e=r-L),G<0?k=z-Q:G>0&&(a=u-Q),D=h,et.attr("cursor",w.selection),it());break;default:return}l()},!0).on("keyup.brush",function(){switch(i.event.keyCode){case 16:R&&(q=P=R=!1,it());break;case 18:D===d&&(I<0?v=b:I>0&&(e=r),G<0?k=z:G>0&&(a=u),D=f,it());break;case 32:D===h&&(i.event.altKey?(I&&(v=b-L*I,e=r+L*I),G&&(k=z-Q*G,a=u+Q*G),D=d):(I<0?v=b:I>0&&(e=r),G<0?k=z:G>0&&(a=u),D=f),et.attr("cursor",w[j]),it());break;default:return}l()},!0),n.dragDisable(i.event.view)}o(),s.interrupt(S),B.call(S),$.start()}function rt(){var t=U(S);!R||q||P||(Math.abs(t[0]-Z[0])>Math.abs(t[1]-Z[1])?P=!0:q=!0),Z=t,A=!0,l(),it()}function it(){var t;switch(L=Z[0]-W[0],Q=Z[1]-W[1],D){case h:case c:I&&(L=Math.max(Y-e,Math.min(H-v,L)),r=e+L,b=v+L),G&&(Q=Math.max(F-a,Math.min(J-k,Q)),u=a+Q,z=k+Q);break;case f:I<0?(L=Math.max(Y-e,Math.min(H-e,L)),r=e+L,b=v):I>0&&(L=Math.max(Y-v,Math.min(H-v,L)),r=e,b=v+L),G<0?(Q=Math.max(F-a,Math.min(J-a,Q)),u=a+Q,z=k):G>0&&(Q=Math.max(F-k,Math.min(J-k,Q)),u=a,z=k+Q);break;case d:I&&(r=Math.max(Y,Math.min(H,e-L*I)),b=Math.max(Y,Math.min(H,v+L*I))),G&&(u=Math.max(F,Math.min(J,a-Q*G)),z=Math.max(F,Math.min(J,k+Q*G)))}b<r&&(I*=-1,t=e,e=v,v=t,t=r,r=b,b=t,j in g&&et.attr("cursor",w[j=g[j]])),z<u&&(G*=-1,t=a,a=k,k=t,t=u,u=z,z=t,j in x&&et.attr("cursor",w[j=x[j]])),N.selection&&(X=N.selection),q&&(r=X[0][0],b=X[1][0]),P&&(u=X[0][1],z=X[1][1]),X[0][0]===r&&X[0][1]===u&&X[1][0]===b&&X[1][1]===z||(N.selection=[[r,u],[b,z]],B.call(S),$.brush())}function st(){if(o(),i.event.touches){if(i.event.touches.length)return;p&&clearTimeout(p),p=setTimeout(function(){p=null},500)}else n.dragEnable(i.event.view,A),nt.on("keydown.brush keyup.brush mousemove.brush mouseup.brush",null);tt.attr("pointer-events","all"),et.attr("cursor",w.overlay),N.selection&&(X=N.selection),function(t){return t[0][0]===t[1][0]||t[0][1]===t[1][1]}(X)&&(N.selection=null,B.call(S)),$.end()}}function G(){C(this,arguments).moved()}function N(){C(this,arguments).ended()}function O(){var e=this.__brush||{selection:null};return e.extent=v(b.apply(this,arguments)),e.dim=t,e}return j.move=function(e,n){e.selection?e.on("start.brush",function(){C(this,arguments).beforestart().start()}).on("interrupt.brush end.brush",function(){C(this,arguments).end()}).tween("brush",function(){var e=this,i=e.__brush,s=C(e,arguments),a=i.selection,u=t.input("function"==typeof n?n.apply(this,arguments):n,i.extent),o=r.interpolate(a,u);function l(t){i.selection=1===t&&null===u?null:o(t),B.call(e),s.brush()}return null!==a&&null!==u?l:l(1)}):e.each(function(){var e=this,r=arguments,i=e.__brush,a=t.input("function"==typeof n?n.apply(e,r):n,i.extent),u=C(e,r).beforestart();s.interrupt(e),i.selection=null===a?null:a,B.call(e),u.start().brush().end()})},j.clear=function(t){j.move(t,null)},D.prototype={beforestart:function(){return 1==++this.active&&(this.state.emitter=this,this.starting=!0),this},start:function(){return this.starting?(this.starting=!1,this.emit("start")):this.emit("brush"),this},brush:function(){return this.emit("brush"),this},end:function(){return 0==--this.active&&(delete this.state.emitter,this.emit("end")),this},emit:function(e){i.customEvent(new u(j,e,t.output(this.state.selection)),V.apply,V,[e,this.that,this.args])}},j.extent=function(t){return arguments.length?(b="function"==typeof t?t:a(v(t)),j):b},j.filter=function(t){return arguments.length?(E="function"==typeof t?t:a(!!t),j):E},j.touchable=function(t){return arguments.length?(P="function"==typeof t?t:a(!!t),j):P},j.handleSize=function(t){return arguments.length?(S=+t,j):S},j.keyModifiers=function(t){return arguments.length?(T=!!t,j):T},j.on=function(){var t=V.on.apply(V,arguments);return t===V?j:t},j}t.brush=function(){return E(b)},t.brushSelection=function(t){var e=t.__brush;return e?e.dim.output(e.selection):null},t.brushX=function(){return E(y)},t.brushY=function(){return E(m)},Object.defineProperty(t,"__esModule",{value:!0})});