-
Notifications
You must be signed in to change notification settings - Fork 140
/
dTree.min.js
2 lines (2 loc) · 9.25 KB
/
dTree.min.js
1
2
function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var a=0;a<t.length;a++){var r=t[a];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,a,r){return a&&e(t.prototype,a),r&&e(t,r),t}}();!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.dTree=t()}(this,function(){"use strict";var e=function(){function e(t,a,r){_classCallCheck(this,e),e.DEBUG_LEVEL=r.debug?1:0,this.root=t,this.siblings=a,this.opts=r,this.allNodes=this._flatten(this.root),this.nodeSize=r.callbacks.nodeSize.call(this,_.filter(this.allNodes,function(e){return!(e.hidden||_.get(e,"data.isMarriage"))}),r.nodeWidth,r.callbacks.textRenderer),this.marriageSize=r.callbacks.marriageSize.call(this,_.filter(this.allNodes,function(e){return!e.hidden&&_.get(e,"data.isMarriage")}),this.opts.marriageNodeSize)}return _createClass(e,[{key:"create",value:function(){var e=this.opts,t=(this.allNodes,this.nodeSize),a=e.width+e.margin.left+e.margin.right,r=e.height+e.margin.top+e.margin.bottom,n=this.zoom=d3.zoom().scaleExtent([.1,10]).on("zoom",function(){o.attr("transform",d3.event.transform)}),i=this.svg=d3.select(e.target).append("svg").attr("viewBox",[0,0,a,r]).call(n),o=this.g=i.append("g");i.call(n.transform,d3.zoomIdentity.translate(a/2,e.margin.top).scale(1)),this.tree=d3.tree().nodeSize([2*t[0],e.callbacks.nodeHeightSeperation.call(this,t[0],t[1])]),this.tree.separation(function(e,t){return e.data.hidden||t.data.hidden?.3:.6}),this._update(this.root)}},{key:"_update",value:function(e){var t=this.opts,a=(this.allNodes,this.nodeSize),r=this.marriageSize,n=this.tree(e),i=n.links();this.g.selectAll(".link").data(i).enter().filter(function(e){return!e.target.data.noParent}).append("path").attr("class",t.styles.linage).attr("d",this._elbow);var o=this.g.selectAll(".node").data(n.descendants()).enter();this._linkSiblings(),this.g.selectAll(".sibling").data(this.siblings).enter().append("path").attr("class",t.styles.marriage).attr("d",_.bind(this._siblingLine,this)),o.append("foreignObject").filter(function(e){return!e.data.hidden}).attr("x",function(e){return Math.round(e.x-e.cWidth/2)+"px"}).attr("y",function(e){return Math.round(e.y-e.cHeight/2)+"px"}).attr("width",function(e){return e.cWidth+"px"}).attr("height",function(e){return e.cHeight+"px"}).attr("id",function(e){return e.id}).html(function(e){return e.data.isMarriage?t.callbacks.marriageRenderer.call(this,e.x,e.y,r[0],r[1],e.data.extra,e.data.id,e.data["class"]):t.callbacks.nodeRenderer.call(this,e.data.name,e.x,e.y,a[0],a[1],e.data.extra,e.data.id,e.data["class"],e.data.textClass,t.callbacks.textRenderer)}).on("dblclick",function(){d3.event.stopPropagation()}).on("click",function(e){2===d3.event.detail||e.data.hidden||(e.data.isMarriage?t.callbacks.marriageClick.call(this,e.data.extra,e.data.id):t.callbacks.nodeClick.call(this,e.data.name,e.data.extra,e.data.id))}).on("contextmenu",function(e){e.data.hidden||(d3.event.preventDefault(),e.data.isMarriage?t.callbacks.marriageRightClick.call(this,e.data.extra,e.data.id):t.callbacks.nodeRightClick.call(this,e.data.name,e.data.extra,e.data.id))})}},{key:"_flatten",value:function(e){function t(e){e.children&&e.children.forEach(t),e.id||(e.id=++r),a.push(e)}var a=[],r=0;return t(e),a}},{key:"_elbow",value:function(e,t){if(e.target.data.noParent)return"M0,0L0,0";var a=Math.round(e.target.y+.5*(e.source.y-e.target.y)),r=[{x:e.target.x,y:e.target.y},{x:e.target.x,y:a},{x:e.source.x,y:e.source.y}],n=d3.line().curve(d3.curveStepAfter).x(function(e){return e.x}).y(function(e){return e.y});return n(r)}},{key:"_linkSiblings",value:function(){var e=this.allNodes;_.forEach(this.siblings,function(t){var a=e.filter(function(e){return t.source.id==e.data.id}),r=e.filter(function(e){return t.target.id==e.data.id});t.source.x=a[0].x,t.source.y=a[0].y,t.target.x=r[0].x,t.target.y=r[0].y;var n=null!=a[0].data.marriageNode?a[0].data.marriageNode.id:r[0].data.marriageNode.id,i=e.find(function(e){return e.data.id==n});t.source.marriageNode=i,t.target.marriageNode=i})}},{key:"_siblingLine",value:function(e,t){var a=Math.round(e.target.y+.5*(e.source.y-e.target.y)),r=this.nodeSize[0],n=this.nodeSize[1];e.number>0&&(a-=Math.round(8*n/10));var i=[{x:e.source.x,y:e.source.y},{x:Math.round(e.source.x+6*r/10),y:e.source.y},{x:Math.round(e.source.x+6*r/10),y:a},{x:e.target.marriageNode.x,y:a},{x:e.target.marriageNode.x,y:e.target.y},{x:e.target.x,y:e.target.y}],o=d3.line().curve(d3.curveStepAfter).x(function(e){return e.x}).y(function(e){return e.y});return o(i)}}],[{key:"_nodeHeightSeperation",value:function(e,t){return t+25}},{key:"_nodeSize",value:function(e,t,a){var r=0,n=document.createElement("svg");return document.body.appendChild(n),_.map(e,function(e){var i=document.createElement("div");i.setAttribute("class",e.data["class"]),i.style.visibility="hidden",i.style.maxWidth=t+"px";var o=a(e.data.name,e.data.extra,e.data.textClass);i.innerHTML=o,n.appendChild(i);var d=i.offsetHeight;n.removeChild(i),r=Math.max(r,d),e.cHeight=d,e.data.hidden?e.cWidth=0:e.cWidth=t}),document.body.removeChild(n),[t,r]}},{key:"_marriageSize",value:function(e,t){return _.map(e,function(e){e.data.hidden||(e.cHeight=t,e.cWidth=t)}),[t,t]}},{key:"_nodeRenderer",value:function(e,t,a,r,n,i,o,d,s,l){var c="";return c+="<div ",c+='style="height:100%;width:100%;" ',c+='class="'+d+'" ',c+='id="node'+o+'">\n',c+=l(e,i,s),c+="</div>"}},{key:"_textRenderer",value:function(e,t,a){var r="";return r+="<p ",r+='align="center" ',r+='class="'+a+'">\n',r+=e,r+="</p>\n"}},{key:"_marriageRenderer",value:function(e,t,a,r,n,i,o){return'<div style="height:100%" class="'+o+'" id="node'+i+'"></div>'}},{key:"_debug",value:function(t){e.DEBUG_LEVEL>0&&console.log(t)}}]),e}(),t={VERSION:"2.4.1",init:function(t){function a(e,t){var a=arguments.length<=2||void 0===arguments[2]?1:arguments[2],r=arguments.length<=3||void 0===arguments[3]?500:arguments[3];i.svg.transition().duration(r).call(i.zoom.transform,d3.zoomIdentity.translate(n.width/2,n.height/2).scale(a).translate(-e,-t))}var r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=_.defaultsDeep(r||{},{target:"#graph",debug:!1,width:600,height:600,hideMarriageNodes:!0,callbacks:{nodeClick:function(e,t,a){},nodeRightClick:function(e,t,a){},marriageClick:function(e,t){},marriageRightClick:function(e,t){},nodeHeightSeperation:function(t,a){return e._nodeHeightSeperation(t,a)},nodeRenderer:function(t,a,r,n,i,o,d,s,l,c){return e._nodeRenderer(t,a,r,n,i,o,d,s,l,c)},nodeSize:function(t,a,r){return e._nodeSize(t,a,r)},nodeSorter:function(e,t,a,r){return 0},textRenderer:function(t,a,r){return e._textRenderer(t,a,r)},marriageRenderer:function(t,a,r,n,i,o,d){return e._marriageRenderer(t,a,r,n,i,o,d)},marriageSize:function(t,a){return e._marriageSize(t,a)}},margin:{top:0,right:0,bottom:0,left:0},nodeWidth:100,marriageNodeSize:10,styles:{node:"node",marriageNode:"marriageNode",linage:"linage",marriage:"marriage",text:"nodeText"}}),t=this._preprocess(t,n),i=new e(t.root,t.siblings,n);return i.create(),{resetZoom:function(){var e=arguments.length<=0||void 0===arguments[0]?500:arguments[0];i.svg.transition().duration(e).call(i.zoom.transform,d3.zoomIdentity.translate(n.width/2,n.margin.top).scale(1))},zoomTo:a,zoomToNode:function(e){var t=arguments.length<=1||void 0===arguments[1]?2:arguments[1],r=arguments.length<=2||void 0===arguments[2]?500:arguments[2],n=_.find(i.allNodes,{data:{id:e}});n&&a(n.x,n.y,t,r)},zoomToFit:function(){var e=arguments.length<=0||void 0===arguments[0]?500:arguments[0],t=i.g.node().getBBox(),a=t.width,r=t.height,n=i.svg.node().clientWidth,o=i.svg.node().clientHeight,d=.95/Math.max(a/n,r/o);i.svg.transition().duration(e).call(i.zoom.transform,d3.zoomIdentity.translate(n/2-d*(t.x+a/2),o/2-d*(t.y+r/2)).scale(d))}}},_preprocess:function(e,a){var r=[],n=0,i={name:"",id:n++,hidden:!0,children:[]},o=function d(e,o){var s={name:e.name,id:n++,hidden:!1,children:[],extra:e.extra,textClass:e.textClass?e.textClass:a.styles.text,"class":e["class"]?e["class"]:a.styles.node};o==i&&(s.noParent=!0);for(var l=0;l<e.depthOffset;l++){var c={name:"",id:n++,hidden:!0,children:[],noParent:s.noParent};o.children.push(c),o=c}t._sortPersons(e.children,a),_.forEach(e.children,function(e){d(e,s)}),o.children.push(s),t._sortMarriages(e.marriages,a),_.forEach(e.marriages,function(e,i){var l={name:"",id:n++,hidden:a.hideMarriageNodes,noParent:!0,children:[],isMarriage:!0,extra:e.extra,"class":e["class"]?e["class"]:a.styles.marriageNode},c=e.spouse,u={name:c.name,id:n++,hidden:!1,noParent:!0,children:[],textClass:c.textClass?c.textClass:a.styles.text,"class":c["class"]?c["class"]:a.styles.node,extra:c.extra,marriageNode:l};o.children.push(l,u),t._sortPersons(e.children,a),_.forEach(e.children,function(e){d(e,l)}),r.push({source:{id:s.id},target:{id:u.id},number:i})})};return _.forEach(e,function(e){o(e,i)}),{root:d3.hierarchy(i),siblings:r}},_sortPersons:function(e,t){return void 0!=e&&e.sort(function(e,a){return t.callbacks.nodeSorter.call(this,e.name,e.extra,a.name,a.extra)}),e},_sortMarriages:function(e,t){return void 0!=e&&Array.isArray(e)&&e.sort(function(e,a){var r=e.spouse,n=a.spouse;return t.callbacks.nodeSorter.call(this,r.name,r.extra,n.name,n.extra)}),e}};return t});
//# sourceMappingURL=dTree.min.js.map