draw.js
上传用户:yfdli66
上传日期:2010-02-20
资源大小:47k
文件大小:8k
源码类别:

JavaScript

开发平台:

JavaScript

  1. /*------------------------------------------------------------------------------
  2. +                        图形绘制  定义了状态接口DrawState,环境角色,DrawContext  +
  3. +                        和实现状态接口的子类 SelectDrawState,BeginDrawState    +
  4. +  EndDrawState,ProcessDrawState,LineDrawState         +
  5. -------------------------------------------------------------------------------*/
  6. /*--------画图状态-----------*/
  7. //画图状态接口
  8. function DrawState () {
  9. //interface
  10. this.draw = draw;
  11. function draw (canvas,clickEvent) {
  12. }
  13. }
  14. //选择
  15. function SelectDrawState () {
  16. //inhert
  17.      this.base=DrawState;
  18.         this.base();
  19. //override
  20. this.draw = draw;
  21. function draw (canvas,clickEvent) {
  22. //是否选择的是节点
  23.          var nodeType = clickEvent.srcElement.ctlNodeType;
  24. if ( !(nodeType == "node" || nodeType == "childNode") ) {
  25. return;
  26. }
  27. //获得节点
  28. var obj;
  29.             if (nodeType == "node") {
  30.              obj=clickEvent.srcElement;
  31.             } else {
  32.              obj=clickEvent.srcElement.parentElement;
  33.             }
  34. fSelectedObj = obj;
  35.             //保存坐标
  36.             fx=fSelectedObj.style.pixelLeft-event.clientX;
  37.          fy=fSelectedObj.style.pixelTop-event.clientY;
  38. //如果是线条
  39.             if (obj.ctlType==CNST_CTLTYPE_LINES) {
  40. fDragApproved=false;
  41.             } else  {
  42. fDragApproved=true;//允许拖动
  43. }
  44. return true;
  45. }
  46. }
  47. //合并
  48. function JoinsDrawState () {
  49. //inhert
  50.      this.base=DrawState;
  51.         this.base();
  52. //override
  53. this.draw = draw;
  54. function draw (canvas,clickEvent) {
  55. //计算鼠标在drawCanvas中的绝对位置
  56. var xx=clickEvent.x-canvas.offsetLeft-10,yy=clickEvent.y-canvas.offsetTop-5;
  57. xx=xx<0?0:xx;
  58. yy=yy<0?0:yy;
  59.             var thisIDName = "w"+fCtlNum;
  60. var thisCaption = "joins"+fCtlNum;
  61.             var gctl =  "<v:shape "+
  62. " oncontextmenu="contextForThis(this)" "+
  63. " path = 'm 0,150 l 0,300, 200,300, 200,150 100,150 200,0 100,150 0,0 100,150  x e'   "+
  64. " id = '"+thisIDName+"' "+
  65. "  ctlType='"+CNST_CTLTYPE_JOINS+"'  "+
  66. " ctlNodeType="node"  "+
  67. " conditionType='or'  "+
  68. " line=""  "+
  69. " style="position:absolute;width:80;height:40px;left:"+xx+";top:"+yy+";">"+
  70.              " <v:shadow ctlNodeType="childNode" on="T" type="single" color="#b3b3b3" offset="3px,3px" style="cursor:default" />"+
  71.                         " <v:TextBox ctlNodeType="childNode" inset="-2px,8px,0px,0px" style="overflow:visible;font-size:10.2pt;cursor:default">"+thisCaption+"</v:TextBox>";
  72.                         "</v:shape>";
  73.             canvas.insertAdjacentHTML('beforeEnd',gctl);
  74. addToObjArray(thisIDName,true); //增加到控件列表
  75.             return true;
  76. }
  77. }
  78.     //分支
  79. function SplitsDrawState () {
  80. //inhert
  81.      this.base=DrawState;
  82.         this.base();
  83. //override
  84. this.draw = draw;
  85. function draw (canvas,clickEvent) {
  86. //计算鼠标在drawCanvas中的绝对位置
  87. var xx=clickEvent.x-canvas.offsetLeft-10,yy=clickEvent.y-canvas.offsetTop-5;
  88. xx=xx<0?0:xx;
  89. yy=yy<0?0:yy;
  90.             var thisIDName = "w"+fCtlNum;
  91. var thisCaption = "splits"+fCtlNum;
  92.             var gctl =  "<v:shape oncontextmenu="contextForThis(this)" path = 'm 0,0 l 0,150, 100,150 0,300 100,150 200,300 100,150 200,150 200,0    x e' id = '"+thisIDName+"' ctlType='"+CNST_CTLTYPE_SPLITS+"' ctlNodeType="node" line=""  style="position:absolute;width:80;height:40px;left:"+xx+";top:"+yy+";">"+
  93.              " <v:shadow ctlNodeType="childNode" on="T" type="single" color="#b3b3b3" offset="3px,3px" style="cursor:default" />"+
  94.                         " <v:TextBox ctlNodeType="childNode" inset="-2px,8px,0px,0px" style="overflow:visible;font-size:10.2pt;cursor:default">"+thisCaption+"</v:TextBox>";
  95.                         "</v:shape>";
  96.             canvas.insertAdjacentHTML('beforeEnd',gctl);
  97. addToObjArray(thisIDName,true); //增加到控件列表
  98.             return true;
  99. }
  100. }
  101. //步骤
  102. function StepsDrawState () {
  103. //inhert
  104.      this.base=DrawState;
  105.         this.base();
  106. //override
  107. this.draw = draw;
  108. function draw (canvas,clickEvent) {
  109. //计算鼠标在drawCanvas中的绝对位置
  110. var xx=clickEvent.x-canvas.offsetLeft-10,yy=clickEvent.y-canvas.offsetTop-5;
  111. xx=xx<0?0:xx;
  112. yy=yy<0?0:yy;
  113. var thisIDName = "w"+fCtlNum;
  114. var thisCaption = "步骤"+fCtlNum;
  115.             var gctl =  "<v:shape "+
  116. " oncontextmenu="contextForThis(this)" "+
  117. " path = 'm 0,0 l 0,150, 200,150, 200,0   x e' "+
  118. "  id = '"+thisIDName+"' "+
  119. "  ctlName = '"+thisCaption+"' "+
  120. "  view = '' "+
  121. "  auto = 'no' "+
  122. "  ctlType='"+CNST_CTLTYPE_STEPS+"' "+
  123. " ctlNodeType="node" "+
  124. " line=""  "+
  125. " style="position:absolute;width:80px;height:40px;left:"+xx+";top:"+yy+";">"+
  126.              " <v:shadow ctlNodeType="childNode" on="T" type="single" color="#b3b3b3" offset="3px,3px" style="cursor:default" />"+
  127.                         " <v:TextBox ctlNodeType="childNode" inset="-2px,8px,0px,0px" style="overflow:visible;font-size:10.2pt;cursor:default">"+thisCaption+"</v:TextBox>";
  128.                         "</v:shape>";
  129.          canvas.insertAdjacentHTML('beforeEnd',gctl);
  130. addToObjArray(thisIDName,true); //增加到控件列表
  131. return true;
  132. }
  133. }
  134. //线条
  135. function LineDrawState () {
  136. //inhert
  137.      this.base=DrawState;
  138.         this.base();
  139. //override
  140. this.draw = draw;
  141. function draw (canvas,clickEvent) {
  142. //是否选择的是节点
  143.          var nodeType = clickEvent.srcElement.ctlNodeType;
  144. if ( !(nodeType == "node" || nodeType == "childNode") ) {
  145. return;
  146. }
  147. //获得节点
  148. var obj;
  149.             if (nodeType == "node") {
  150.              obj=clickEvent.srcElement;
  151.             } else {
  152.              obj=clickEvent.srcElement.parentElement;
  153.             }
  154. if (fAddLineFlag==1) {
  155.              fLineStartObj = obj;
  156.                 fLineStartObj.StrokeColor='red';// 并标记为红色
  157.                 fLineStartObj.strokeweight=2;
  158.                 alert("请选择线条的终点"); //提示选择终点
  159.                 fAddLineFlag++; //进入下一步
  160.             } else if (fAddLineFlag==2) {
  161.              //如果选择的节点不是起点的话
  162.                 if ( obj!=fLineStartObj ) {
  163.                  fLineEndObj=obj;
  164. var thisLineID = fLineStartObj.id + "TO" + fLineEndObj.id;
  165.                     var thisIDName = "w"+fCtlNum;
  166.                     var thisCaption = "线条"+fCtlNum;
  167. thisLineID = thisIDName+":"+thisLineID;
  168. //看看是否已存在这条线段
  169.                     if(!document.all[thisLineID]) {
  170. var  posArray = cf_CalculateLinePos(fLineStartObj,fLineEndObj);
  171. var gctl  = '<v:PolyLine oncontextmenu="contextForThis(this)" ctlType="'+CNST_CTLTYPE_LINES+'"  filled="false" '+
  172. ' style="position:absolute;width:50px;height:50px;z-index:-99;"'+
  173. '           ctlNodeType="node" '+
  174. ' id="' + thisIDName + '" '+
  175. '  ctlName = "'+thisCaption+'" '+
  176. ' owner="" '+
  177. ' status="Queued" '+
  178. ' oldStatus="Finished" '+
  179. ' Points="'+posArray["x1"]+','+posArray["y1"]+' '+posArray["x2"]+','+posArray["y2"]+' '+posArray["x3"]+','+posArray["y3"]+' '+posArray["x4"]+','+posArray["y4"]+'"> '+
  180. ' <v:stroke  EndArrow="Classic"  /> '+
  181. '</v:PolyLine>';
  182. canvas.insertAdjacentHTML('beforeEnd',gctl);
  183.                         fLineStartObj.line += (thisLineID + ";");
  184.                         fLineEndObj.line += (thisLineID + ";");
  185. addToObjArray(thisIDName,false); //增加到控件列表
  186.                      fAddLineFlag = 0; //恢复初始值
  187.                      return true;
  188.                     } else {
  189.                  alert("该条线段已经存在,请重新选择"); //提示选择终点
  190. }
  191.                 }
  192.             }//end if
  193. }//end func
  194. }
  195. /*-----------end-画图状态--------*/
  196. /*-----------画图环境角色--------*/
  197. function DrawContext () {
  198. //property
  199. this.drawState = new SelectDrawState(); //默认为选择状态
  200. //method
  201. this.setDrawState = setDrawState;
  202. this.draw = draw;
  203. function setDrawState (aDrawState) {
  204. this.drawState = aDrawState;
  205. }
  206. function draw (canvas,clickEvent) {
  207. return this.drawState.draw(canvas,clickEvent);
  208. }
  209. }
  210. /*---end--画图环境角色--------*/