ActionTreeNode.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Created by Jacky.gao on 2016/2/24.
  3. */
  4. ActionTreeNode=function(parentNode){
  5. TreeNode.call(this,parentNode);
  6. this.actionTypes=[];
  7. this.initNode();
  8. };
  9. ActionTreeNode.prototype=Object.create(TreeNode.prototype);
  10. ActionTreeNode.prototype.constructor=ActionTreeNode;
  11. ActionTreeNode.prototype.initNode=function(){
  12. this.nodeContainer=$("<div class='node actionNode'>");
  13. this.col.append(this.nodeContainer);
  14. this.actionsContainer=$("<span style='display: inline-block;'>");
  15. this.nodeContainer.append(this.actionsContainer);
  16. this.addAction();
  17. var self=this;
  18. var operations=$("<span class='operations'><i class='icon-ok-circle'></i></span>");
  19. this.nodeContainer.append(operations);
  20. var menuItems=[];
  21. menuItems.push({
  22. name:"delete",
  23. label:"删除",
  24. onClick:function(){
  25. URule.confirm("真的要删除当前节点?",function(){
  26. self.delete();
  27. });
  28. }
  29. });
  30. menuItems.push({
  31. name:"addAction",
  32. label:"添加动作",
  33. onClick:function(){
  34. self.addAction(true);
  35. }
  36. });
  37. var menu=new URule.menu.Menu({menuItems:menuItems});
  38. operations.click(function(e){
  39. menu.show(e);
  40. });
  41. };
  42. ActionTreeNode.prototype.addAction=function(notfirst){
  43. var actionContainer=$("<span>");
  44. if(notfirst){
  45. actionContainer.css("display","block");
  46. }
  47. var delIcon=$("<i class='icon-minus-sign icon-large' style='color: #ac2925;padding-right: 5px'></i>");
  48. actionContainer.append(delIcon);
  49. this.actionsContainer.append(actionContainer);
  50. var newActionType=new urule.ActionType(actionContainer);
  51. this.actionTypes.push(newActionType);
  52. actionContainer.actionType=newActionType;
  53. var self=this;
  54. delIcon.click(function(){
  55. if(self.actionTypes.length===1){
  56. URule.alert("动作至少要有一个.");
  57. return;
  58. }
  59. var pos=-1;
  60. $.each(self.actionTypes,function(i,at){
  61. if(at===actionContainer.actionType){
  62. pos=i;
  63. return false;
  64. }
  65. });
  66. if(pos!==-1){
  67. self.actionTypes.splice(pos,1);
  68. actionContainer.remove();
  69. }else{
  70. URule.alert("未找到要删除的动作对象.");
  71. }
  72. });
  73. return newActionType;
  74. };
  75. ActionTreeNode.prototype.initData=function(data){
  76. if(!data){
  77. return;
  78. }
  79. var actions=data["actions"];
  80. if(!actions || actions.length===0){
  81. return;
  82. }
  83. this.actionTypes[0].parentContainer.remove();
  84. this.actionTypes.splice(0,1);
  85. for(var i=0;i<actions.length;i++){
  86. var action=actions[i];
  87. var newActionType=this.addAction(i!==0);
  88. newActionType.initData(action);
  89. }
  90. };
  91. ActionTreeNode.prototype.toXml=function(){
  92. var xml="<action-tree-node>";
  93. $.each(this.actionTypes,function(i,actionType){
  94. xml+=actionType.toXml();
  95. });
  96. xml+="</action-tree-node>";
  97. return xml;
  98. };