forked from d3/d3.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3-brush.v1.min.js
2 lines (2 loc) · 8.31 KB
/
d3-brush.v1.min.js
1
2
// https://d3js.org/d3-brush/ Version 1.0.4. Copyright 2017 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.d3=t.d3||{},t.d3,t.d3,t.d3,t.d3,t.d3)}(this,function(t,e,n,r,s,i){"use strict";function a(){s.event.stopImmediatePropagation()}function u(t){return{type:t}}function o(){return!s.event.button}function l(){var t=this.ownerSVGElement||this;return[[0,0],[t.width.baseVal.value,t.height.baseVal.value]]}function c(t){for(;!t.__brush;)if(!(t=t.parentNode))return;return t.__brush}function h(t){return t[0][0]===t[1][0]||t[0][1]===t[1][1]}function f(t){var e=t.__brush;return e?e.dim.output(e.selection):null}function p(){return v(M)}function d(){return v(k)}function v(t){function f(e){var n=e.property("__brush",S).selectAll(".overlay").data([u("overlay")]);n.enter().append("rect").attr("class","overlay").attr("pointer-events","all").attr("cursor",A.overlay).merge(n).each(function(){var t=c(this).extent;s.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([u("selection")]).enter().append("rect").attr("class","selection").attr("cursor",A.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 A[t.type]}),e.each(p).attr("fill","none").attr("pointer-events","all").style("-webkit-tap-highlight-color","rgba(0,0,0,0)").on("mousedown.brush touchstart.brush",z)}function p(){var t=s.select(this),e=c(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]-D/2:e[0][0]-D/2}).attr("y",function(t){return"s"===t.type[0]?e[1][1]-D/2:e[0][1]-D/2}).attr("width",function(t){return"n"===t.type||"s"===t.type?e[1][0]-e[0][0]+D:D}).attr("height",function(t){return"e"===t.type||"w"===t.type?e[1][1]-e[0][1]+D:D})):t.selectAll(".selection,.handle").style("display","none").attr("x",null).attr("y",null).attr("width",null).attr("height",null)}function d(t,e){return t.__brush.emitter||new v(t,e)}function v(t,e){this.that=t,this.args=e,this.state=t.__brush,this.active=0}function z(){function e(){var t=s.mouse(X);!$||N||O||(Math.abs(t[0]-et[0])>Math.abs(t[1]-et[1])?O=!0:N=!0),et=t,G=!0,b(),r()}function r(){var t;switch(D=et[0]-tt[0],I=et[1]-tt[1],B){case g:case w:F&&(D=Math.max(R-f,Math.min(W-z,D)),v=f+D,S=z+D),H&&(I=Math.max(U-m,Math.min(Z-V,I)),y=m+I,C=V+I);break;case x:F<0?(D=Math.max(R-f,Math.min(W-f,D)),v=f+D,S=z):F>0&&(D=Math.max(R-z,Math.min(W-z,D)),v=f,S=z+D),H<0?(I=Math.max(U-m,Math.min(Z-m,I)),y=m+I,C=V):H>0&&(I=Math.max(U-V,Math.min(Z-V,I)),y=m,C=V+I);break;case _:F&&(v=Math.max(R,Math.min(W,f-D*F)),S=Math.max(R,Math.min(W,z+D*F))),H&&(y=Math.max(U,Math.min(Z,m-I*H)),C=Math.max(U,Math.min(Z,V+I*H)))}S<v&&(F*=-1,t=f,f=z,z=t,t=v,v=S,S=t,Y in q&&st.attr("cursor",A[Y=q[Y]])),C<y&&(H*=-1,t=m,m=V,V=t,t=y,y=C,C=t,Y in E&&st.attr("cursor",A[Y=E[Y]])),J.selection&&(Q=J.selection),N&&(v=Q[0][0],S=Q[1][0]),O&&(y=Q[0][1],C=Q[1][1]),Q[0][0]===v&&Q[0][1]===y&&Q[1][0]===S&&Q[1][1]===C||(J.selection=[[v,y],[S,C]],p.call(X),nt.brush())}function u(){if(a(),s.event.touches){if(s.event.touches.length)return;T&&clearTimeout(T),T=setTimeout(function(){T=null},500),rt.on("touchmove.brush touchend.brush touchcancel.brush",null)}else n.dragEnable(s.event.view,G),it.on("keydown.brush keyup.brush mousemove.brush mouseup.brush",null);rt.attr("pointer-events","all"),st.attr("cursor",A.overlay),J.selection&&(Q=J.selection),h(Q)&&(J.selection=null,p.call(X)),nt.end()}function o(){switch(s.event.keyCode){case 16:$=F&&H;break;case 18:B===x&&(F&&(z=S-D*F,f=v+D*F),H&&(V=C-I*H,m=y+I*H),B=_,r());break;case 32:B!==x&&B!==_||(F<0?z=S-D:F>0&&(f=v-D),H<0?V=C-I:H>0&&(m=y-I),B=g,st.attr("cursor",A.selection),r());break;default:return}b()}function l(){switch(s.event.keyCode){case 16:$&&(N=O=$=!1,r());break;case 18:B===_&&(F<0?z=S:F>0&&(f=v),H<0?V=C:H>0&&(m=y),B=x,r());break;case 32:B===g&&(s.event.altKey?(F&&(z=S-D*F,f=v+D*F),H&&(V=C-I*H,m=y+I*H),B=_):(F<0?z=S:F>0&&(f=v),H<0?V=C:H>0&&(m=y),B=x),st.attr("cursor",A[Y]),r());break;default:return}b()}if(s.event.touches){if(s.event.changedTouches.length<s.event.touches.length)return b()}else if(T)return;if(j.apply(this,arguments)){var f,v,m,y,z,S,V,C,D,I,G,N,O,X=this,Y=s.event.target.__data__.type,B="selection"===(s.event.metaKey?Y="overlay":Y)?w:s.event.altKey?_:x,F=t===k?null:K[Y],H=t===M?null:P[Y],J=c(X),L=J.extent,Q=J.selection,R=L[0][0],U=L[0][1],W=L[1][0],Z=L[1][1],$=F&&H&&s.event.shiftKey,tt=s.mouse(X),et=tt,nt=d(X,arguments).beforestart();"overlay"===Y?J.selection=Q=[[f=t===k?R:tt[0],m=t===M?U:tt[1]],[z=t===k?W:f,V=t===M?Z:m]]:(f=Q[0][0],m=Q[0][1],z=Q[1][0],V=Q[1][1]),v=f,y=m,S=z,C=V;var rt=s.select(X).attr("pointer-events","none"),st=rt.selectAll(".overlay").attr("cursor",A[Y]);if(s.event.touches)rt.on("touchmove.brush",e,!0).on("touchend.brush touchcancel.brush",u,!0);else{var it=s.select(s.event.view).on("keydown.brush",o,!0).on("keyup.brush",l,!0).on("mousemove.brush",e,!0).on("mouseup.brush",u,!0);n.dragDisable(s.event.view)}a(),i.interrupt(X),p.call(X),nt.start()}}function S(){var e=this.__brush||{selection:null};return e.extent=V.apply(this,arguments),e.dim=t,e}var T,V=l,j=o,C=e.dispatch(f,"start","brush","end"),D=6;return f.move=function(e,n){e.selection?e.on("start.brush",function(){d(this,arguments).beforestart().start()}).on("interrupt.brush end.brush",function(){d(this,arguments).end()}).tween("brush",function(){function e(t){i.selection=1===t&&h(o)?null:l(t),p.call(s),a.brush()}var s=this,i=s.__brush,a=d(s,arguments),u=i.selection,o=t.input("function"==typeof n?n.apply(this,arguments):n,i.extent),l=r.interpolate(u,o);return u&&o?e:e(1)}):e.each(function(){var e=this,r=arguments,s=e.__brush,a=t.input("function"==typeof n?n.apply(e,r):n,s.extent),u=d(e,r).beforestart();i.interrupt(e),s.selection=null==a||h(a)?null:a,p.call(e),u.start().brush().end()})},v.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},brush:function(){return this.emit("brush"),this},end:function(){return 0==--this.active&&(delete this.state.emitter,this.emit("end")),this},emit:function(e){s.customEvent(new y(f,e,t.output(this.state.selection)),C.apply,C,[e,this.that,this.args])}},f.extent=function(t){return arguments.length?(V="function"==typeof t?t:m([[+t[0][0],+t[0][1]],[+t[1][0],+t[1][1]]]),f):V},f.filter=function(t){return arguments.length?(j="function"==typeof t?t:m(!!t),f):j},f.handleSize=function(t){return arguments.length?(D=+t,f):D},f.on=function(){var t=C.on.apply(C,arguments);return t===C?f:t},f}var m=function(t){return function(){return t}},y=function(t,e,n){this.target=t,this.type=e,this.selection=n},b=function(){s.event.preventDefault(),s.event.stopImmediatePropagation()},w={name:"drag"},g={name:"space"},x={name:"handle"},_={name:"center"},M={name:"x",handles:["e","w"].map(u),input:function(t,e){return t&&[[t[0],e[0][1]],[t[1],e[1][1]]]},output:function(t){return t&&[t[0][0],t[1][0]]}},k={name:"y",handles:["n","s"].map(u),input:function(t,e){return t&&[[e[0][0],t[0]],[e[1][0],t[1]]]},output:function(t){return t&&[t[0][1],t[1][1]]}},z={name:"xy",handles:["n","e","s","w","nw","ne","se","sw"].map(u),input:function(t){return t},output:function(t){return t}},A={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"},q={e:"w",w:"e",nw:"ne",ne:"nw",se:"sw",sw:"se"},E={n:"s",s:"n",nw:"sw",ne:"se",se:"ne",sw:"nw"},K={overlay:1,selection:1,n:null,e:1,s:null,w:-1,nw:-1,ne:1,se:1,sw:-1},P={overlay:1,selection:1,n:-1,e:null,s:1,w:null,nw:-1,ne:-1,se:1,sw:1},S=function(){return v(z)};t.brush=S,t.brushX=p,t.brushY=d,t.brushSelection=f,Object.defineProperty(t,"__esModule",{value:!0})});