utils.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /// <reference path="raphael.js" />
  2. /// <reference path="viewer.js" />
  3. var AB;
  4. (function (AB) {
  5. var Utils = (function () {
  6. function Utils(viewer) {
  7. // Members
  8. this.viewer = viewer;
  9. }
  10. // Removes the selected node
  11. Utils.prototype.onRemoveNode = function () {
  12. if (!this.viewer.selectedNode)
  13. return;
  14. var node = this.viewer.selectedNode;
  15. if (node.type == AB.ActionsBuilder.Type.FLOW_CONTROL && node.parent.combine)
  16. this.viewer.selectedNode = node.parent;
  17. node.parent.removeChild(node);
  18. this.viewer._removeAction(node, false);
  19. if (node.combine) {
  20. // Remove hub
  21. var hub = node.hub;
  22. node.parent.removeChild(hub.action);
  23. this.viewer._removeAction(hub.action, false);
  24. if (hub.action.children.length > 0)
  25. node.hub.action.children[0].parent = node.parent;
  26. }
  27. this.viewer.update();
  28. this.viewer.parametersManager.clearParameters();
  29. this.viewer.selectedNode = null;
  30. }
  31. // Removes the selected branch (starting from the selected node)
  32. Utils.prototype.onRemoveBranch = function () {
  33. if (!this.viewer.selectedNode)
  34. return;
  35. var node = this.viewer.selectedNode;
  36. if (node.type == AB.ActionsBuilder.Type.FLOW_CONTROL && node.parent.combine)
  37. this.selectedNode = node.parent;
  38. node.parent.removeChild(this.selectedNode);
  39. this.viewer._removeAction(node, true);
  40. this.viewer.update();
  41. this.viewer.parametersManager.clearParameters();
  42. this.viewer.selectedNode = null;
  43. }
  44. // Detaches the selected node
  45. // forceDetach: forces the Detach Color (green) for children of the selected node
  46. // forceAttach: same as forceDetach but for the original node color
  47. Utils.prototype.onDetachNode = function (forceDetach, forceAttach) {
  48. if (!this.viewer.selectedNode)
  49. return;
  50. var node = this.viewer.selectedNode;
  51. if (forceDetach == null) forceDetach = false;
  52. if (forceAttach == null) forceAttach = false;
  53. var detached = node.node.detached;
  54. if (forceAttach)
  55. detached = false;
  56. else if (forceDetach)
  57. detached = true;
  58. else
  59. detached = !detached;
  60. node.node.detached = detached;
  61. var scope = this;
  62. var resetColor = function (root, color) {
  63. var col = color == null ? scope.viewer.getNodeColor(root, scope.viewer.isParentDetached(root)) : color;
  64. root.node.attr(root.node.rect, "fill", col);
  65. if (root.node.line)
  66. root.node.attr(root.node.line, "stroke", col);
  67. if (root.combine) {
  68. for (var i = 0; i < root.combineArray.length; i++) {
  69. resetColor(root.combineArray[i], color);
  70. }
  71. }
  72. for (var i = 0; i < root.children.length; i++) {
  73. resetColor(root.children[i], color);
  74. }
  75. };
  76. this.viewer._setLine(node);
  77. resetColor(node, !detached ? null : this.viewer.getNodeColor(node));
  78. }
  79. // Disconnects all triggers
  80. Utils.prototype.onDetachAll = function (forceDetach, forceAttach) {
  81. var scope = this;
  82. var detach = function (root) {
  83. scope.viewer.selectedNode = root;
  84. scope.onDetachNode(forceDetach, forceAttach);
  85. };
  86. for (var i = 0; i < this.viewer.root.action.children.length; i++)
  87. detach(this.viewer.root.action.children[i]);
  88. }
  89. // Copies the selected node structure (except root node) to the
  90. // clipboard
  91. Utils.prototype.onCopyStructure = function () {
  92. if (!this.viewer.selectedNode)
  93. return;
  94. var structure = this.viewer.createJSON(this.viewer.selectedNode);
  95. var asText = JSON.stringify(structure);
  96. window.clipboardData.setData("text", asText);
  97. }
  98. // Pastes the previously copied structure (onCopyStructure) to
  99. // the selected node
  100. Utils.prototype.onPasteStructure = function () {
  101. if (!this.viewer.selectedNode)
  102. return;
  103. var asText = window.clipboardData.getData("text");
  104. var isJson = asText.length > 0 && asText[0] == "{" && asText[asText.length - 1] == "}";
  105. var structure = JSON.parse(asText);
  106. var node = this.viewer.selectedNode;
  107. if (structure.type == AB.ActionsBuilder.Type.TRIGGER && node.node != this.viewer.root) {
  108. alert("You can't paste a trigger if the selected node isn't the root object");
  109. return;
  110. }
  111. if (structure.type != AB.ActionsBuilder.Type.TRIGGER && node.node == this.viewer.root) {
  112. alert("You can't paste an action or condition if the selected node is the root object");
  113. return;
  114. }
  115. this.viewer.loadFromJSON(structure, node);
  116. this.viewer.update();
  117. }
  118. // Reduces the select node's width
  119. Utils.prototype.onReduce = function (forceExpand, forceReduce) {
  120. if (!this.viewer.selectedNode)
  121. return;
  122. if (forceExpand == null) forceExpand = false;
  123. if (forceReduce == null) forceReduce = false;
  124. var node = this.viewer.selectedNode.node;
  125. var width = node.attr(node.rect, "width");
  126. if ((width >= Graph.NODE_WIDTH && !forceExpand) || forceReduce) {
  127. node.text.hide();
  128. node.rect.stop();
  129. }
  130. else {
  131. node.text.show();
  132. node.attr(node.rect, "opacity", 1.0);
  133. }
  134. node.attr(node.rect, "width", (width >= Graph.NODE_WIDTH * this.viewer.zoom && !forceExpand) || forceReduce ? Graph.NODE_MINIMIZED_WIDTH * this.viewer.zoom : Graph.NODE_WIDTH * this.viewer.zoom);
  135. node.minimized = !node.minimized;
  136. this.viewer.update();
  137. }
  138. // Reduces all graph's nodes
  139. Utils.prototype.onReduceAll = function (forceExpand) {
  140. if (forceExpand == null)
  141. forceExpand = false;
  142. var scope = this;
  143. var reduce = function (root) {
  144. scope.viewer.selectedNode = root;
  145. scope.onReduce(forceExpand, forceExpand ? false : true);
  146. if (root.combine) {
  147. for (var i = 0; i < root.combineArray.length; i++)
  148. reduce(root.combineArray[i]);
  149. }
  150. for (var i = 0; i < root.children.length; i++)
  151. reduce(root.children[i]);
  152. };
  153. for (var i = 0; i < this.viewer.root.action.children.length; i++)
  154. reduce(this.viewer.root.action.children[i]);
  155. this.viewer.selectedNode = null;
  156. }
  157. return Utils;
  158. })();
  159. AB.Utils = Utils;
  160. })(AB || (AB = {}));