1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-18 20:59:39 +02:00
Maybe/vendor/javascript/d3-zoom.js
Josh Pigford 868d4ede6e
Sankey Diagram (#2269)
* Enhance cash flow dashboard with new cash flow period handling and improved Sankey diagram rendering. Update D3 and related dependencies for better performance and features.

* Fix Rubocop offenses

* Refactor Sankey chart controller to use Number.parseFloat for value formatting and improve code readability by restructuring conditional logic for node shapes.
2025-05-20 13:31:05 -05:00

4 lines
10 KiB
JavaScript

// d3-zoom@3.0.0 downloaded from https://ga.jspm.io/npm:d3-zoom@3.0.0/src/index.js
import{dispatch as t}from"d3-dispatch";import{dragDisable as o,dragEnable as e}from"d3-drag";import{interpolateZoom as n}from"d3-interpolate";import{select as i,pointer as r}from"d3-selection";import{interrupt as u}from"d3-transition";var constant=t=>()=>t;function ZoomEvent(t,{sourceEvent:o,target:e,transform:n,dispatch:i}){Object.defineProperties(this,{type:{value:t,enumerable:true,configurable:true},sourceEvent:{value:o,enumerable:true,configurable:true},target:{value:e,enumerable:true,configurable:true},transform:{value:n,enumerable:true,configurable:true},_:{value:i}})}function Transform(t,o,e){this.k=t;this.x=o;this.y=e}Transform.prototype={constructor:Transform,scale:function(t){return 1===t?this:new Transform(this.k*t,this.x,this.y)},translate:function(t,o){return 0===t&0===o?this:new Transform(this.k,this.x+this.k*t,this.y+this.k*o)},apply:function(t){return[t[0]*this.k+this.x,t[1]*this.k+this.y]},applyX:function(t){return t*this.k+this.x},applyY:function(t){return t*this.k+this.y},invert:function(t){return[(t[0]-this.x)/this.k,(t[1]-this.y)/this.k]},invertX:function(t){return(t-this.x)/this.k},invertY:function(t){return(t-this.y)/this.k},rescaleX:function(t){return t.copy().domain(t.range().map(this.invertX,this).map(t.invert,t))},rescaleY:function(t){return t.copy().domain(t.range().map(this.invertY,this).map(t.invert,t))},toString:function(){return"translate("+this.x+","+this.y+") scale("+this.k+")"}};var s=new Transform(1,0,0);transform.prototype=Transform.prototype;function transform(t){while(!t.__zoom)if(!(t=t.parentNode))return s;return t.__zoom}function nopropagation(t){t.stopImmediatePropagation()}function noevent(t){t.preventDefault();t.stopImmediatePropagation()}function defaultFilter(t){return(!t.ctrlKey||"wheel"===t.type)&&!t.button}function defaultExtent(){var t=this;if(t instanceof SVGElement){t=t.ownerSVGElement||t;if(t.hasAttribute("viewBox")){t=t.viewBox.baseVal;return[[t.x,t.y],[t.x+t.width,t.y+t.height]]}return[[0,0],[t.width.baseVal.value,t.height.baseVal.value]]}return[[0,0],[t.clientWidth,t.clientHeight]]}function defaultTransform(){return this.__zoom||s}function defaultWheelDelta(t){return-t.deltaY*(1===t.deltaMode?.05:t.deltaMode?1:.002)*(t.ctrlKey?10:1)}function defaultTouchable(){return navigator.maxTouchPoints||"ontouchstart"in this}function defaultConstrain(t,o,e){var n=t.invertX(o[0][0])-e[0][0],i=t.invertX(o[1][0])-e[1][0],r=t.invertY(o[0][1])-e[0][1],u=t.invertY(o[1][1])-e[1][1];return t.translate(i>n?(n+i)/2:Math.min(0,n)||Math.max(0,i),u>r?(r+u)/2:Math.min(0,r)||Math.max(0,u))}function zoom(){var a,h,c,l=defaultFilter,m=defaultExtent,f=defaultConstrain,p=defaultWheelDelta,d=defaultTouchable,v=[0,Infinity],z=[[-Infinity,-Infinity],[Infinity,Infinity]],y=250,g=n,_=t("start","zoom","end"),w=500,T=150,k=0,x=10;function zoom(t){t.property("__zoom",defaultTransform).on("wheel.zoom",wheeled,{passive:false}).on("mousedown.zoom",mousedowned).on("dblclick.zoom",dblclicked).filter(d).on("touchstart.zoom",touchstarted).on("touchmove.zoom",touchmoved).on("touchend.zoom touchcancel.zoom",touchended).style("-webkit-tap-highlight-color","rgba(0,0,0,0)")}zoom.transform=function(t,o,e,n){var i=t.selection?t.selection():t;i.property("__zoom",defaultTransform);t!==i?schedule(t,o,e,n):i.interrupt().each((function(){gesture(this,arguments).event(n).start().zoom(null,"function"===typeof o?o.apply(this,arguments):o).end()}))};zoom.scaleBy=function(t,o,e,n){zoom.scaleTo(t,(function(){var t=this.__zoom.k,e="function"===typeof o?o.apply(this,arguments):o;return t*e}),e,n)};zoom.scaleTo=function(t,o,e,n){zoom.transform(t,(function(){var t=m.apply(this,arguments),n=this.__zoom,i=null==e?centroid(t):"function"===typeof e?e.apply(this,arguments):e,r=n.invert(i),u="function"===typeof o?o.apply(this,arguments):o;return f(translate(scale(n,u),i,r),t,z)}),e,n)};zoom.translateBy=function(t,o,e,n){zoom.transform(t,(function(){return f(this.__zoom.translate("function"===typeof o?o.apply(this,arguments):o,"function"===typeof e?e.apply(this,arguments):e),m.apply(this,arguments),z)}),null,n)};zoom.translateTo=function(t,o,e,n,i){zoom.transform(t,(function(){var t=m.apply(this,arguments),i=this.__zoom,r=null==n?centroid(t):"function"===typeof n?n.apply(this,arguments):n;return f(s.translate(r[0],r[1]).scale(i.k).translate("function"===typeof o?-o.apply(this,arguments):-o,"function"===typeof e?-e.apply(this,arguments):-e),t,z)}),n,i)};function scale(t,o){o=Math.max(v[0],Math.min(v[1],o));return o===t.k?t:new Transform(o,t.x,t.y)}function translate(t,o,e){var n=o[0]-e[0]*t.k,i=o[1]-e[1]*t.k;return n===t.x&&i===t.y?t:new Transform(t.k,n,i)}function centroid(t){return[(+t[0][0]+ +t[1][0])/2,(+t[0][1]+ +t[1][1])/2]}function schedule(t,o,e,n){t.on("start.zoom",(function(){gesture(this,arguments).event(n).start()})).on("interrupt.zoom end.zoom",(function(){gesture(this,arguments).event(n).end()})).tween("zoom",(function(){var t=this,i=arguments,r=gesture(t,i).event(n),u=m.apply(t,i),s=null==e?centroid(u):"function"===typeof e?e.apply(t,i):e,a=Math.max(u[1][0]-u[0][0],u[1][1]-u[0][1]),h=t.__zoom,c="function"===typeof o?o.apply(t,i):o,l=g(h.invert(s).concat(a/h.k),c.invert(s).concat(a/c.k));return function(t){if(1===t)t=c;else{var o=l(t),e=a/o[2];t=new Transform(e,s[0]-o[0]*e,s[1]-o[1]*e)}r.zoom(null,t)}}))}function gesture(t,o,e){return!e&&t.__zooming||new Gesture(t,o)}function Gesture(t,o){this.that=t;this.args=o;this.active=0;this.sourceEvent=null;this.extent=m.apply(t,o);this.taps=0}Gesture.prototype={event:function(t){t&&(this.sourceEvent=t);return this},start:function(){if(1===++this.active){this.that.__zooming=this;this.emit("start")}return this},zoom:function(t,o){this.mouse&&"mouse"!==t&&(this.mouse[1]=o.invert(this.mouse[0]));this.touch0&&"touch"!==t&&(this.touch0[1]=o.invert(this.touch0[0]));this.touch1&&"touch"!==t&&(this.touch1[1]=o.invert(this.touch1[0]));this.that.__zoom=o;this.emit("zoom");return this},end:function(){if(0===--this.active){delete this.that.__zooming;this.emit("end")}return this},emit:function(t){var o=i(this.that).datum();_.call(t,this.that,new ZoomEvent(t,{sourceEvent:this.sourceEvent,target:zoom,type:t,transform:this.that.__zoom,dispatch:_}),o)}};function wheeled(t,...o){if(l.apply(this,arguments)){var e=gesture(this,o).event(t),n=this.__zoom,i=Math.max(v[0],Math.min(v[1],n.k*Math.pow(2,p.apply(this,arguments)))),s=r(t);if(e.wheel){e.mouse[0][0]===s[0]&&e.mouse[0][1]===s[1]||(e.mouse[1]=n.invert(e.mouse[0]=s));clearTimeout(e.wheel)}else{if(n.k===i)return;e.mouse=[s,n.invert(s)];u(this);e.start()}noevent(t);e.wheel=setTimeout(wheelidled,T);e.zoom("mouse",f(translate(scale(n,i),e.mouse[0],e.mouse[1]),e.extent,z))}function wheelidled(){e.wheel=null;e.end()}}function mousedowned(t,...n){if(!c&&l.apply(this,arguments)){var s=t.currentTarget,a=gesture(this,n,true).event(t),h=i(t.view).on("mousemove.zoom",mousemoved,true).on("mouseup.zoom",mouseupped,true),m=r(t,s),p=t.clientX,d=t.clientY;o(t.view);nopropagation(t);a.mouse=[m,this.__zoom.invert(m)];u(this);a.start()}function mousemoved(t){noevent(t);if(!a.moved){var o=t.clientX-p,e=t.clientY-d;a.moved=o*o+e*e>k}a.event(t).zoom("mouse",f(translate(a.that.__zoom,a.mouse[0]=r(t,s),a.mouse[1]),a.extent,z))}function mouseupped(t){h.on("mousemove.zoom mouseup.zoom",null);e(t.view,a.moved);noevent(t);a.event(t).end()}}function dblclicked(t,...o){if(l.apply(this,arguments)){var e=this.__zoom,n=r(t.changedTouches?t.changedTouches[0]:t,this),u=e.invert(n),s=e.k*(t.shiftKey?.5:2),a=f(translate(scale(e,s),n,u),m.apply(this,o),z);noevent(t);y>0?i(this).transition().duration(y).call(schedule,a,n,t):i(this).call(zoom.transform,a,n,t)}}function touchstarted(t,...o){if(l.apply(this,arguments)){var e,n,i,s,c=t.touches,m=c.length,f=gesture(this,o,t.changedTouches.length===m).event(t);nopropagation(t);for(n=0;n<m;++n){i=c[n],s=r(i,this);s=[s,this.__zoom.invert(s),i.identifier];f.touch0?f.touch1||f.touch0[2]===s[2]||(f.touch1=s,f.taps=0):(f.touch0=s,e=true,f.taps=1+!!a)}a&&(a=clearTimeout(a));if(e){f.taps<2&&(h=s[0],a=setTimeout((function(){a=null}),w));u(this);f.start()}}}function touchmoved(t,...o){if(this.__zooming){var e,n,i,u,s=gesture(this,o).event(t),a=t.changedTouches,h=a.length;noevent(t);for(e=0;e<h;++e){n=a[e],i=r(n,this);s.touch0&&s.touch0[2]===n.identifier?s.touch0[0]=i:s.touch1&&s.touch1[2]===n.identifier&&(s.touch1[0]=i)}n=s.that.__zoom;if(s.touch1){var c=s.touch0[0],l=s.touch0[1],m=s.touch1[0],p=s.touch1[1],d=(d=m[0]-c[0])*d+(d=m[1]-c[1])*d,v=(v=p[0]-l[0])*v+(v=p[1]-l[1])*v;n=scale(n,Math.sqrt(d/v));i=[(c[0]+m[0])/2,(c[1]+m[1])/2];u=[(l[0]+p[0])/2,(l[1]+p[1])/2]}else{if(!s.touch0)return;i=s.touch0[0],u=s.touch0[1]}s.zoom("touch",f(translate(n,i,u),s.extent,z))}}function touchended(t,...o){if(this.__zooming){var e,n,u=gesture(this,o).event(t),s=t.changedTouches,a=s.length;nopropagation(t);c&&clearTimeout(c);c=setTimeout((function(){c=null}),w);for(e=0;e<a;++e){n=s[e];u.touch0&&u.touch0[2]===n.identifier?delete u.touch0:u.touch1&&u.touch1[2]===n.identifier&&delete u.touch1}u.touch1&&!u.touch0&&(u.touch0=u.touch1,delete u.touch1);if(u.touch0)u.touch0[1]=this.__zoom.invert(u.touch0[0]);else{u.end();if(2===u.taps){n=r(n,this);if(Math.hypot(h[0]-n[0],h[1]-n[1])<x){var l=i(this).on("dblclick.zoom");l&&l.apply(this,arguments)}}}}}zoom.wheelDelta=function(t){return arguments.length?(p="function"===typeof t?t:constant(+t),zoom):p};zoom.filter=function(t){return arguments.length?(l="function"===typeof t?t:constant(!!t),zoom):l};zoom.touchable=function(t){return arguments.length?(d="function"===typeof t?t:constant(!!t),zoom):d};zoom.extent=function(t){return arguments.length?(m="function"===typeof t?t:constant([[+t[0][0],+t[0][1]],[+t[1][0],+t[1][1]]]),zoom):m};zoom.scaleExtent=function(t){return arguments.length?(v[0]=+t[0],v[1]=+t[1],zoom):[v[0],v[1]]};zoom.translateExtent=function(t){return arguments.length?(z[0][0]=+t[0][0],z[1][0]=+t[1][0],z[0][1]=+t[0][1],z[1][1]=+t[1][1],zoom):[[z[0][0],z[0][1]],[z[1][0],z[1][1]]]};zoom.constrain=function(t){return arguments.length?(f=t,zoom):f};zoom.duration=function(t){return arguments.length?(y=+t,zoom):y};zoom.interpolate=function(t){return arguments.length?(g=t,zoom):g};zoom.on=function(){var t=_.on.apply(_,arguments);return t===_?zoom:t};zoom.clickDistance=function(t){return arguments.length?(k=(t=+t)*t,zoom):Math.sqrt(k)};zoom.tapDistance=function(t){return arguments.length?(x=+t,zoom):x};return zoom}export{Transform as ZoomTransform,zoom,s as zoomIdentity,transform as zoomTransform};