(function(){"use strict";var e=u8.Base.Geometry.Rectangle;function t(e){$.extend(this,t.defaults,e);this.bind(e);this.model.bind("Update",this._onModelUpdate.bind(this))}t.defaults={algorithm:"EfficientSugiyama",edgeRouting:"Orthogonal",positionMode:-1,vertexSpacing:10,layerSpacing:35};$.extend(t.prototype,{create:function(e,t){this._vertices={};this._edges={};this._visibleArea=e;var i=this.model;if(!i||i.isEmpty()){this._applyLayout(e,{vertexPositions:{},edgeRoutes:[]});t();return}this._updateLayout(t)},getVertexPosition:function(e){e=this._vertices&&this._vertices[e];return e&&e.pos},getEdgeRoute:function(e){var t=this._edges&&this._edges[e];return t},getVertexSize:function(e){var t=this.getAutoVertexSize&&this.getAutoVertexSize(e);return t||{width:64,height:64}},getBoundingRect:function(){return new e(0,0,this.width,this.height)},_applyLayout:function(e,t){var i=this.model;var r=this._vertices;var a=t.vertexPositions;var s=this.orientation=="Horizontal";var h=this._getBounds(t);e=e||h;var o=Math.max(h.width,e.width||0);var n=Math.max(h.height,e.height||0);this.width=o;this.height=n;var d=-h.left+o*.5-h.width*.5;var v=-h.top;var u=function(e,t){return{x:d+e,y:v+t}};for(var g in a){var f=a[g],p=r[g]||{width:0,height:0};p.x=f.x;p.y=f.y;var c=u(p.x-p.width*.5,p.y-p.height*.5);p.pos={left:c.x,top:c.y,width:p.width,height:p.height};r[g]=p}var l=i.edges;var y=t.edgeRoutes.toObject("id");for(var _=0;_<l.length;_++){var x=l[_];var m=r[x.source];var w=r[x.target];var S=y[x.id];var U=!s&&m.y>w.y;var b=u(m.x+(s?m.width*.5:0),m.y+(!s?m.height*.5:0));var L=u(w.x+(s?w.width*-.5-3:0),w.y-(!s?w.height*.5+3:0));var V=[b];var R=S&&S.route;if(R){if(U){if(R[0].y<R[R.length-1].y){R.reverse()}}else if(s&&m.x>w.x){if(R[0].x<R[R.length-1].x){R.reverse()}}for(var E=0;E<R.length;E++){var M=R[E];V.push(u(M.x,M.y))}}V.push(L);this._edges[x.id]={points:V}}},_getBounds:function(e){var t=1e5,i=1e5,r=-1e5,a=-1e5;var s=e.vertexPositions;for(var h in s){var o=s[h];var n=this._vertices[h];var d=o.x-n.width*.5;var v=o.y-n.height*.5;var u=d+n.width;var g=v+n.height;if(d<t){t=d}if(v<i){i=v}if(u>r){r=u}if(g>a){a=g}}var f=e.edgeRoutes;for(var p=0;p<f.length;p++){var c=f[p];var l=c.route;for(var y=0;y<l.length;y++){var _=l[y];if(_.x<t){t=_.x}if(_.y<i){i=_.y}if(_.x>r){r=_.x}if(_.y>a){a=_.y}}}if(r<t){return{left:0,top:0,width:0,height:0}}t-=2;i-=2;r+=2;a+=2;return{left:t,top:i,width:r-t,height:a-i}},_updateLayout:function(e,t){if(typeof e=="function"){t=e;e=null}var i=this.model.toTransport();var r=i.vertices;for(var a=0;a<r.length;a++){var s=r[a];var h=s.id;var o=this.getVertexSize(s);this._vertices[h]=o;r[a]=$.extend({},s,{width:o.width,height:o.height})}var n=this;var d={algorithm:this.algorithm,positionMode:this.positionMode,edgeRouting:this.edgeRouting,vertexSpacing:this.vertexSpacing,layerSpacing:this.layerSpacing,orientation:this.orientation};u8.request("GraphChannel.Layout",{graph:i,options:d},function(i,r){if(!r.error){n._applyLayout(n._visibleArea,r.response);if(e){n.raise("Update",e)}}if(t){t()}})},_onModelUpdate:function(e,t){if(this._isDisposed){return}var i=!this.__queuedUpdates;if(!this.__queuedUpdates){this.__queuedUpdates={}}var r=this.__queuedUpdates;switch(t.what){case"addVertex":case"addEdge":break;case"removeVertex":if(!r.removedVertices){r.removedVertices={}}r.removedVertices[t.value.id]=true;break;case"removeEdge":if(!r.removedEdges){r.removedEdges={}}r.removedEdges[t.value.id]=true;break;default:break}if(!i){return}var a=this;var s=function(){if(a._isDisposed){return}var e={vertices:a._vertices,edges:a._edges};a._updateLayout($.extend(e,a.__queuedUpdates));a.__queuedUpdates=null}.despam(100);s()}});t.makeClass({name:"LayeredGraphLayout",events:["Update"]});u8.services.factory.ready("GraphModel",function(){u8.Base.Graphs.addLayout("layered",{create:function(e){return new t(e)}})})})();