-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathd3.domVisualizer.min.js
2 lines (2 loc) · 5.16 KB
/
d3.domVisualizer.min.js
1
2
/*! d3.domVisualizer 2014-09-27 */
d3.domVisualizer=function(a,b){b=b||{},this.d3TreeRoot=!1,this.duration=b.duration||500,this.nodeSize=b.nodeSize||[50,80],this.autoIncrement=0,this.fitToContainer=b.fitToContainer||!0,this.displayTextNodes=b.displayTextNodes||!0,this.displayTextIndentNodes=b.displayTextIndentNodes||!1,this.displayCommentNodes=b.displayCommentNodes||!1,this.tree=d3.layout.tree(),this.container="string"==typeof a?document.querySelector(a):a,this.fitToContainer?this.tree.size([this.container.clientWidth+43,this.container.clientHeight-45]):this.tree.nodeSize(this.nodeSize),this.diagonal=d3.svg.diagonal().projection(function(a){return[a.x,a.y]})},d3.domVisualizer.prototype.update=function(a){a=a||!1;var b=this.tree.nodes(this.d3TreeRoot),c=d3.min(b,function(a){return a.x}),d=d3.max(b,function(a){return a.x})-c,e=d3.max(b,function(a){return a.y})-d3.min(b,function(a){return a.y}),f=d3.select(this.container).select("svg");f[0][0]||(f=d3.select(this.container).append("svg"),f.append("g"));var g=f.select("g");f.attr({width:d+45,height:e+45}),g.attr("transform","translate("+(-c+20)+",30)"),b.forEach(function(a){a.children||(a.children=[])});var h=g.selectAll("g.node").data(b,function(a){return a.id}),i=h;a&&(i=h.transition().duration(this.duration)),i.attr("transform",function(a){return"translate("+a.x+","+a.y+")"}).style("opacity",1).select("text").text(function(a){return a.value});var j=h.enter().append("svg:g").attr("class",function(a){return"node "+a.type}).style("opacity",.5).attr("transform",function(a){for(var b=this.d3TreeRoot.x,c=this.d3TreeRoot.y,d=a;d.par;){if(d.par.x0&&d.par.y0){b=d.par.x0,c=d.par.y0;break}d=d.par}return"translate("+b+","+c+")"}.bind(this));j.append("svg:circle").attr("r",10),j.append("text").attr("text-anchor","middle").attr("dy",-15).text(function(a){return a.value}),a&&(j=j.transition().duration(this.duration)),j.attr("transform",function(a){return"translate("+a.x+","+a.y+")"}).style("opacity",1),h.exit().remove();var k=g.selectAll("path.link").data(this.tree.links(b),function(a){return a.target.id});k.enter().insert("svg:path","g").attr("class","link").attr("d",function(a){for(var b=this.d3TreeRoot.x,c=this.d3TreeRoot.y,d=a.source;d;){if(d.x0&&d.y0){b=d.x0,c=d.y0;break}d=d.par}b=a.source&&a.source.x0?a.source.x0:b,c=a.source&&a.source.y0?a.source.y0:c;var e={x:b,y:c};return this.diagonal({source:e,target:e})}.bind(this)).style("opacity",0),k.transition().duration(this.duration).attr("d",this.diagonal).style("opacity",1),k.exit().remove(),b.forEach(function(a){a.x0=a.x,a.y0=a.y})},d3.domVisualizer.prototype.getRealNodeName=function(a){var b=a.tagName.toLowerCase();return a.hasAttribute("id")?b+="#"+a.getAttribute("id"):a.hasAttribute("class")&&[].forEach.call(a.className.split(" "),function(a){b+="."+a}),b},d3.domVisualizer.prototype.buildTree=function(a,b){var c=new DeltaJS.tree.Node;switch(c.id=++this.autoIncrement,c.depth=b||0,a.nodeType){case Node.ELEMENT_NODE:c.value=this.getRealNodeName(a),c.type="element",a.childNodes.length&&[].forEach.call(a.childNodes,function(a){c.append(this.buildTree(a,c.depth+1))}.bind(this));break;case Node.TEXT_NODE:c.value="#text",c.type=a.nodeValue.match(/^\s+$/)?"text indent":"text";break;case Node.COMMENT_NODE:c.value="<!-- -->",c.type="comment"}return c},d3.domVisualizer.prototype.parseHTML=function(a){var b,c=document.implementation.createHTMLDocument("");-1!==a.indexOf("<html")?(c.documentElement.innerHTML=a,b=c.documentElement):-1!==a.indexOf("<body")?(c.documentElement.innerHTML=a,b=c.body):(c.body.innerHTML=a,b=c.body.firstElementChild);var d=null;if(this.d3TreeRoot&&(d=this.d3TreeRoot),this.d3TreeRoot=this.buildTree(b),d){var e,f,g,h;f=[],e=[],g=[],matching=new DeltaJS.tree.Matching,h=new DeltaJS.xcc.Diff(d,this.d3TreeRoot),h.matchTrees(matching),this.d3TreeRoot.forEach(function(a){a.partner||console.log(a)}),collector=new DeltaJS.delta.DeltaCollector(matching,d,this.d3TreeRoot),collector.forEachChange(function(a){switch(a.type){case DeltaJS.delta.UPDATE_NODE_TYPE:g=g.concat(a.insert);break;case DeltaJS.delta.UPDATE_FOREST_TYPE:a.remove.length>0&&(e=e.concat(a.remove)),a.insert.length>0&&(f=f.concat(a.insert))}}),e.forEach(function(a){a.par.children.splice(a.childidx,1);var b=0;a.par.children.forEach(function(a){a.childidx=b++})}),g.forEach(function(a){a.partner.value=a.value}),f.forEach(function(a){a.par.partner.children.splice(a.childidx,0,a),a.par=a.par.partner?a.par.partner:a.par,a.forEach(function(a){a.parent=a.par});var b=0;a.par.partner.children.forEach(function(a){a.childidx=b++})}),this.d3TreeRoot=d,this.d3TreeRoot.forEach(function(a){a.partner&&delete a.partner})}},d3.domVisualizer.prototype.addNodesByType=function(a,b){if(b=b||this.d3TreeRoot,"undefined"!=typeof b._children){b.children=b.children||[];var c=b._children.filter(function(b){return b.type===a}),d=d3.bisector(function(a,b){return a.id-b.id}).right;c.forEach(function(a){var c=d(b.children,a);b.children.splice(c,0,a)}),b.children.forEach(function(b){this.addNodesByType(a,b)}.bind(this))}},d3.domVisualizer.prototype.removeNodesByType=function(a,b){if(b=b||this.d3TreeRoot,"undefined"!=typeof b._children)for(var c=0;c<b.children.length;c++)-1!==b.children[c].type.indexOf(a)?b.children.splice(c--,1):this.removeNodesByType(a,b.children[c])};