(function(snaker){ var designer={}; designer.config={ editable:true, lineHeight:15, basePath:"", rect:{ attr:{ x:10,y:10,width:100,height:50,r:5,fill:"90-#fff-#C0C0C0",stroke:"#000","stroke-width":1 }, showType:"image&text", type:"state", name:{text:"state","font-style":"italic"}, text:{text:"状态","font-size":13}, margin:5, props:[], img:{} }, path:{ attr:{ path:{path:"M10 10L100 100",stroke:"#808080",fill:"none","stroke-width":2}, arrow:{path:"M10 10L10 10",stroke:"#808080",fill:"#808080","stroke-width":2,radius:4}, fromDot:{width:5,height:5,stroke:"#fff",fill:"#000",cursor:"move","stroke-width":2}, toDot:{width:5,height:5,stroke:"#fff",fill:"#000",cursor:"move","stroke-width":2}, bigDot:{width:5,height:5,stroke:"#fff",fill:"#000",cursor:"move","stroke-width":2}, smallDot:{width:5,height:5,stroke:"#fff",fill:"#000",cursor:"move","stroke-width":3} }, text:{text:"",cursor:"move",background:"#000"}, textPos:{x:0,y:-10}, props:{ name:{name:"name",label:"名称",value:"",editor:function(){return new designer.editors.inputEditor()}}, displayName:{name:"displayName",label:"显示",value:"",editor:function(){return new designer.editors.textEditor()}}, expr:{name:"expr",label:"表达式",value:"",editor:function(){return new designer.editors.inputEditor()}} } }, tools:{ attr:{ left:10,top:10 }, pointer:{}, path:{}, states:{}, save:{ onclick:function(c){alert(c)} } }, props:{ attr:{top:328,left:10}, props:{} }, restore:"", activeRects:{ rects:[], rectAttr:{stroke:"#ff0000","stroke-width":2} }, historyRects:{ rects:[], pathAttr:{ path:{stroke:"#00ff00"}, arrow:{stroke:"#00ff00",fill:"#00ff00"} } } }; designer.util={ isLine:function(g,f,e){ var d,c; if((g.x-e.x)==0){ d=1 }else{ d=(g.y-e.y)/(g.x-e.x) } c=(f.x-e.x)*d+e.y; if((f.y-c)<10&&(f.y-c)>-10){ f.y=c; return true } return false }, center:function(d,c){ return{x:(d.x-c.x)/2+c.x,y:(d.y-c.y)/2+c.y} }, nextId:(function(){ var c=0; return function(){return ++c} })(), connPoint:function(j,d){ var c=d,e={x:j.x+j.width/2,y:j.y+j.height/2}; var l=(e.y-c.y)/(e.x-c.x); l=isNaN(l)?0:l; var k=j.height/j.width; var h=c.yk&&h==-1){ g=e.y-j.height/2;i=e.x+h*j.height/2/l }else{ if(Math.abs(l)>k&&h==1){ g=e.y+j.height/2;i=e.x+h*j.height/2/l }else{ if(Math.abs(l)/g,"#5"); vv = vv.replace(/"; return r }; this.restore=function(o){ var r=o; E=snaker.extend(true,E,o); f.attr({text:r.text.text}); B() }; this.getName=function(){ for(var o in E.props){ if(o=="name"){ return E.props[o].value; } } } this.getBBox=function(){return q}; this.getId=function(){return g}; this.remove=function(){ t.remove(); f.remove(); n.remove(); e.remove(); s.remove(); for(var o in i){ i[o].remove() } }; this.text=function(){return f.attr("text")}; this.attr=function(o){ if(o){t.attr(o)} }; B() }; designer.path=function(q,n,u,e){ var v=this,z=n,B=snaker.extend(true,{},designer.config.path),i,t,f,h=B.textPos,y,w,k=u,s=e,g="path"+designer.util.nextId(),x; function p(G,H,Left,L){ var F=this,M=G,r,o=Left,O=L,K,I,N=H; switch(M){ case"from":r=z.rect(H.x-B.attr.fromDot.width/2,H.y-B.attr.fromDot.height/2,B.attr.fromDot.width,B.attr.fromDot.height).attr(B.attr.fromDot);break; case"big":r=z.rect(H.x-B.attr.bigDot.width/2,H.y-B.attr.bigDot.height/2,B.attr.bigDot.width,B.attr.bigDot.height).attr(B.attr.bigDot);break; case"small":r=z.rect(H.x-B.attr.smallDot.width/2,H.y-B.attr.smallDot.height/2,B.attr.smallDot.width,B.attr.smallDot.height).attr(B.attr.smallDot);break; case"to":r=z.rect(H.x-B.attr.toDot.width/2,H.y-B.attr.toDot.height/2,B.attr.toDot.width,B.attr.toDot.height).attr(B.attr.toDot);break } if(r&&(M=="big"||M=="small")){ r.drag(function(Q,P){C(Q,P)},function(){J()},function(){E()}); var C=function(R,Q){var P=(K+R),S=(I+Q);F.moveTo(P,S)}; var J=function(){ if(M=="big"){K=r.attr("x")+B.attr.bigDot.width/2;I=r.attr("y")+B.attr.bigDot.height/2} if(M=="small"){K=r.attr("x")+B.attr.smallDot.width/2;I=r.attr("y")+B.attr.smallDot.height/2} }; var E=function(){} } this.type=function(P){if(P){M=P}else{return M}}; this.node=function(P){if(P){r=P}else{return r}}; this.left=function(P){if(P){o=P}else{return o}}; this.right=function(P){if(P){O=P}else{return O}}; this.remove=function(){o=null;O=null;r.remove()}; this.pos=function(P){if(P){N=P;r.attr({x:N.x-r.attr("width")/2,y:N.y-r.attr("height")/2});return this}else{return N}}; this.moveTo=function(Q,T){ this.pos({x:Q,y:T}); switch(M){ case"from":if(O&&O.right()&&O.right().type()=="to"){O.right().pos(designer.util.connPoint(s.getBBox(),N))}if(O&&O.right()){O.pos(designer.util.center(N,O.right().pos()))}break; case"big":if(O&&O.right()&&O.right().type()=="to"){O.right().pos(designer.util.connPoint(s.getBBox(),N))}if(o&&o.left()&&o.left().type()=="from"){o.left().pos(designer.util.connPoint(k.getBBox(),N))}if(O&&O.right()){O.pos(designer.util.center(N,O.right().pos()))}if(o&&o.left()){o.pos(designer.util.center(N,o.left().pos()))}var S={x:N.x,y:N.y};if(designer.util.isLine(o.left().pos(),S,O.right().pos())){M="small";r.attr(B.attr.smallDot);this.pos(S);var P=o;o.left().right(o.right());o=o.left();P.remove();var R=O;O.right().left(O.left());O=O.right();R.remove()}break; case"small":if(o&&O&&!designer.util.isLine(o.pos(),{x:N.x,y:N.y},O.pos())){M="big";r.attr(B.attr.bigDot);var P=new p("small",designer.util.center(o.pos(),N),o,o.right());o.right(P);o=P;var R=new p("small",designer.util.center(O.pos(),N),O.left(),O);O.left(R);O=R}break; case"to":if(o&&o.left()&&o.left().type()=="from"){o.left().pos(designer.util.connPoint(k.getBBox(),N))}if(o&&o.left()){o.pos(designer.util.center(N,o.left().pos()))}break }m() } } function j(){ var D,C,E=k.getBBox(),F=s.getBBox(),r,o; r=designer.util.connPoint(E,{x:F.x+F.width/2,y:F.y+F.height/2}); o=designer.util.connPoint(F,r); D=new p("from",r,null,new p("small",{x:(r.x+o.x)/2,y:(r.y+o.y)/2})); D.right().left(D); C=new p("to",o,D.right(),null); D.right().right(C); this.toPathString=function(){ if(!D){return""} var J=D,I="M"+J.pos().x+" "+J.pos().y,H=""; while(J.right()){ J=J.right(); I+="L"+J.pos().x+" "+J.pos().y } var G=designer.util.arrow(J.left().pos(),J.pos(),B.attr.arrow.radius); H="M"+G[0].x+" "+G[0].y+"L"+G[1].x+" "+G[1].y+"L"+G[2].x+" "+G[2].y+"z"; return[I,H] }; this.toJson=function(){ var G="[",H=D; while(H){ if(H.type()=="big"){ G+="{x:"+Math.round(H.pos().x)+",y:"+Math.round(H.pos().y)+"}," } H=H.right() } if(G.substring(G.length-1,G.length)==","){ G=G.substring(0,G.length-1) } G+="]"; return G }; this.toXml=function(){ var G="",H=D; while(H){ if(H.type()=="big"){ G+=(Math.round(H.pos().x)-180)+","+Math.round(H.pos().y)+";" } H=H.right() } if(G.substring(G.length-1,G.length)==";"){ G=G.substring(0,G.length-1) } return G }; this.restore=function(H){ var I=H,J=D.right(); for(var G=0;G/g,"#5"); vv = vv.replace(/"); m[l].value=m[l].value.replace(/#6/g,"<"); m[l].value=m[l].value.replace(/#7/g,"&"); if(!m[l].label){ continue; } e.append(""+m[l].label+"
"); if(m[l].editor){ m[l].editor().init(m,l,"p"+l,o,g) } } }; snaker(g).bind("showprops",d) }; designer.editors={ textEditor:function(){ var d,e,c,g,f; this.init=function(i,h,m,l,j){ d=i;e=h;c=m;g=l;f=j; snaker('').val(g.text()).change(function(){ i[e].value=snaker(this).val(); snaker(f).trigger("textchange",[snaker(this).val(),g]) }).appendTo("#"+c); snaker("#"+c).data("editor",this) }; this.destroy=function(){ snaker("#"+c+" input").each(function(){ d[e].value=snaker(this).val(); snaker(f).trigger("textchange",[snaker(this).val(),g]) }) } } }; designer.init=function(x,r){ var v=snaker(window).width(),e=snaker(window).height(),y=Raphael(x,v*1.5,e*1.5),q={},g={}; snaker.extend(true,designer.config,r); snaker(document).keydown(function(i){ if(!designer.config.editable){return} if(i.keyCode==46){ var j=snaker(y).data("currNode"); if(j){ if(j.getId().substring(0,4)=="rect"){ snaker(y).trigger("removerect",j) }else{ if(j.getId().substring(0,4)=="path"){ snaker(y).trigger("removepath",j) } } snaker(y).removeData("currNode") } } }); snaker(document).click(function(){ snaker(y).data("currNode",null); snaker(y).trigger("click",{ getId:function(){return ""} }); snaker(y).trigger("showprops",[designer.config.props.props,{getId:function(){return ""}}]) }); var w=function(c,i){ if(!designer.config.editable){return} if(i.getId().substring(0,4)=="rect"){ q[i.getId()]=null; i.remove() }else{ if(i.getId().substring(0,4)=="path"){ g[i.getId()]=null; i.remove() } } }; snaker(y).bind("removepath",w); snaker(y).bind("removerect",w); snaker(y).bind("addrect",function(j,c,k){ var i=new designer.rect(snaker.extend(true,{},designer.config.tools.states[c],k),y); q[i.getId()]=i }); var f=function(i,k,j){ var c=new designer.path({},y,k,j); g[c.getId()]=c }; snaker(y).bind("addpath",f); snaker(y).data("mod","point"); if(designer.config.editable){ snaker("#toolbox").draggable({handle:"#toolbox_handle"}).css(designer.config.tools.attr); snaker("#toolbox .node").hover(function(){snaker(this).addClass("mover")},function(){snaker(this).removeClass("mover")}); snaker("#toolbox .selectable").click(function(){ snaker(".selected").removeClass("selected"); snaker(this).addClass("selected"); snaker(y).data("mod",this.id) }); snaker("#toolbox .state").each(function(){ snaker(this).draggable({helper:"clone"}) }); snaker(x).droppable({ accept:".state", drop:function(c,i){ snaker(y).trigger("addrect",[i.helper.attr("type"),{attr:{x:i.helper.offset().left,y:i.helper.offset().top}}]) } }); snaker("#save").click(function(){ /** var i="{states:{"; for(var c in q){ if(q[c]){ i+=q[c].getId()+":"+q[c].toJson()+"," } } if(i.substring(i.length-1,i.length)==","){ i=i.substring(0,i.length-1) } i+="},paths:{"; for(var c in g){ if(g[c]){ i+=g[c].getId()+":"+g[c].toJson()+"," } } if(i.substring(i.length-1,i.length)==","){ i=i.substring(0,i.length-1) } i+="},props:{props:{"; for(var c in designer.config.props.props){ i+=c+":{value:'"+designer.config.props.props[c].value+"'}," } if(i.substring(i.length-1,i.length)==","){ i=i.substring(0,i.length-1) } i+="}}}"; */ var i="