actionsbuilder.utils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var ActionsBuilder;
  2. (function (ActionsBuilder) {
  3. var Utils = (function () {
  4. function Utils(viewer) {
  5. this.copiedStructure = null;
  6. this._viewer = viewer;
  7. }
  8. Utils.prototype.onTestGraph = function () {
  9. var _this = this;
  10. if (this._viewer.root.children.length === 0) {
  11. alert("Please add at least a Trigger and an Action to test the graph");
  12. }
  13. var onTestTarget = function (targetType, target) {
  14. var targetExists = false;
  15. var array = _this._viewer.parameters._getTargetFromType(targetType);
  16. if (array === null) {
  17. return targetExists;
  18. }
  19. for (var i = 0; i < array.length; i++) {
  20. if (array[i] === target) {
  21. targetExists = true;
  22. break;
  23. }
  24. }
  25. return targetExists;
  26. };
  27. var onNodeError = function (action) {
  28. var node = action.node;
  29. node.rect.attr("fill", Raphael.rgb(255, 0, 0));
  30. return false;
  31. };
  32. var onTestAction = function (action) {
  33. console.log("Testing " + action.name);
  34. if (action.combineArray !== null) {
  35. var foundError = false;
  36. for (var i = 0; i < action.combineArray.length; i++) {
  37. if (!onTestAction(action.combineArray[i])) {
  38. foundError = true;
  39. }
  40. }
  41. if (foundError) {
  42. return false;
  43. }
  44. }
  45. else {
  46. var properties = action.properties;
  47. var propertiesResults = action.propertiesResults;
  48. if (properties !== null) {
  49. var object = null;
  50. var propertyPath = null;
  51. for (var i = 0; i < properties.length; i++) {
  52. if (properties[i].text === "target" || properties[i].text === "parent") {
  53. object = _this._viewer.parameters._getObjectFromType(properties[i].targetType);
  54. var targetExists = onTestTarget(propertiesResults[i].targetType, propertiesResults[i].value);
  55. if (!targetExists) {
  56. return onNodeError(action);
  57. }
  58. }
  59. else if (properties[i].text === "propertyPath") {
  60. var property = propertiesResults[i].value;
  61. var effectiveProperty = object;
  62. var p = property.split(".");
  63. for (var j = 0; j < p.length && effectiveProperty !== undefined; j++) {
  64. effectiveProperty = effectiveProperty[p[j]];
  65. }
  66. if (effectiveProperty === undefined) {
  67. return onNodeError(action);
  68. }
  69. else {
  70. propertyPath = effectiveProperty;
  71. }
  72. }
  73. else if (properties[i].text == "value" && propertyPath != null) {
  74. var value = propertiesResults[i].value;
  75. if (!isNaN(propertyPath)) {
  76. var num = parseFloat(value);
  77. if (isNaN(num) || value === "") {
  78. return onNodeError(action);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. var foundError = false;
  85. for (var i = 0; i < action.children.length; i++) {
  86. if (!onTestAction(action.children[i])) {
  87. foundError = true;
  88. }
  89. }
  90. return !foundError;
  91. }
  92. };
  93. var root = this._viewer.root;
  94. var foundError = false;
  95. for (var i = 0; i < root.children.length; i++) {
  96. var trigger = root.children[i];
  97. var properties = trigger.properties;
  98. if (properties !== null && properties.length > 0) {
  99. var parameter = trigger.propertiesResults[0].value;
  100. if (properties[0].targetType !== null) {
  101. if (!onTestTarget("MeshProperties", parameter)) {
  102. foundError = onNodeError(trigger);
  103. }
  104. }
  105. else {
  106. if (!parameter.match(/[a-z]/)) {
  107. foundError = onNodeError(trigger);
  108. }
  109. }
  110. }
  111. for (var j = 0; j < trigger.children.length; j++) {
  112. var child = trigger.children[j];
  113. var result = onTestAction(child);
  114. if (!result) {
  115. foundError = true;
  116. }
  117. }
  118. }
  119. if (foundError) {
  120. alert("Found error(s). the red nodes contain the error.");
  121. }
  122. else {
  123. alert("No error found.");
  124. }
  125. };
  126. Utils.prototype.onReduceAll = function (forceExpand) {
  127. if (forceExpand === void 0) { forceExpand = false; }
  128. if (this._viewer.selectedNode === null) {
  129. return;
  130. }
  131. var action = this._viewer.selectedNode;
  132. if (action.combineArray !== null) {
  133. for (var i = 0; i < action.combineArray.length; i++) {
  134. this._viewer.selectedNode = action.combineArray[i];
  135. this.onReduce(forceExpand, !forceExpand);
  136. }
  137. }
  138. else {
  139. this.onReduce(forceExpand, !forceExpand);
  140. }
  141. for (var i = 0; i < action.children.length; i++) {
  142. this._viewer.selectedNode = action.children[i];
  143. this.onReduceAll(forceExpand);
  144. }
  145. };
  146. Utils.prototype.onReduce = function (forceExpand, forceReduce) {
  147. if (forceExpand === void 0) { forceExpand = false; }
  148. if (forceReduce === void 0) { forceReduce = false; }
  149. if (this._viewer.selectedNode === null) {
  150. return;
  151. }
  152. var node = this._viewer.selectedNode.node;
  153. node.rect.stop(node.rect.animation);
  154. if (forceExpand === true) {
  155. node.minimized = false;
  156. }
  157. else if (forceReduce === true) {
  158. node.minimized = true;
  159. }
  160. else {
  161. node.minimized = !node.minimized;
  162. }
  163. if (node.minimized) {
  164. node.text.hide();
  165. node.rect.attr("width", ActionsBuilder.Viewer.NODE_MINIMIZED_WIDTH * this._viewer.zoom);
  166. }
  167. else {
  168. node.text.show();
  169. node.rect.attr("width", ActionsBuilder.Viewer.NODE_WIDTH * this._viewer.zoom);
  170. }
  171. };
  172. Utils.prototype.onDetachAction = function (forceDetach, forceAttach) {
  173. var _this = this;
  174. if (forceDetach === void 0) { forceDetach = false; }
  175. if (forceAttach === void 0) { forceAttach = false; }
  176. if (this._viewer.selectedNode === null) {
  177. return;
  178. }
  179. var action = this._viewer.selectedNode;
  180. if (forceDetach === true) {
  181. action.node.detached = true;
  182. }
  183. else if (forceAttach === true) {
  184. action.node.detached = false;
  185. }
  186. else {
  187. action.node.detached = !action.node.detached;
  188. }
  189. var onSetColor = function (root, detached) {
  190. var rootNode = root.node;
  191. rootNode.rect.attr("fill", _this._viewer.getNodeColor(root.type, detached));
  192. if (root.combineArray !== null) {
  193. for (var i = 0; i < root.combineArray.length; i++) {
  194. var combineNode = root.combineArray[i].node;
  195. combineNode.rect.attr("fill", _this._viewer.getNodeColor(root.combineArray[i].type, detached));
  196. }
  197. }
  198. for (var i = 0; i < root.children.length; i++) {
  199. onSetColor(root.children[i], detached);
  200. }
  201. };
  202. onSetColor(action, action.node.detached);
  203. };
  204. Utils.prototype.onRemoveNode = function () {
  205. if (this._viewer.selectedNode === null) {
  206. return;
  207. }
  208. var action = this._viewer.selectedNode;
  209. var parent = action.parent;
  210. if (action.type === ActionsBuilder.Type.TRIGGER) {
  211. this.onRemoveBranch();
  212. return;
  213. }
  214. if (action.type === ActionsBuilder.Type.FLOW_CONTROL && parent !== null && parent.combineArray !== null) {
  215. action = parent;
  216. parent = action.parent;
  217. }
  218. if (parent !== null && parent.combineArray !== null) {
  219. parent.removeCombinedAction(action);
  220. if (parent.combineArray.length === 0) {
  221. parent.node.text.attr("text", "combine");
  222. }
  223. }
  224. else {
  225. if (action.combineArray !== null) {
  226. action.removeChild(action.hub);
  227. }
  228. action.parent.removeChild(action);
  229. }
  230. if (action.combineArray !== null) {
  231. this._viewer.removeAction(action.hub, false);
  232. }
  233. this._viewer.removeAction(action, false);
  234. this._viewer.update();
  235. this._viewer.parameters.clearParameters();
  236. this._viewer.selectedNode = null;
  237. };
  238. Utils.prototype.onRemoveBranch = function () {
  239. if (this._viewer.selectedNode === null) {
  240. return;
  241. }
  242. if (this._viewer.selectedNode === this._viewer.root) {
  243. alert("Cannot remove the root node");
  244. return;
  245. }
  246. var action = this._viewer.selectedNode;
  247. var parent = action.parent;
  248. if (action.parent !== null && action.parent.combineArray !== null) {
  249. action = parent;
  250. parent = action.parent;
  251. }
  252. if (action.combineArray !== null) {
  253. action.removeChild(action.hub);
  254. }
  255. action.parent.removeChild(action);
  256. this._viewer.removeAction(action, true);
  257. this._viewer.update();
  258. this._viewer.parameters.clearParameters();
  259. this._viewer.selectedNode = null;
  260. };
  261. Utils.prototype.onCopyStructure = function () {
  262. if (this._viewer.selectedNode === null) {
  263. return;
  264. }
  265. var structure = this.createJSON(this._viewer.selectedNode);
  266. var asText = JSON.stringify(structure);
  267. if (window.clipboardData !== undefined) {
  268. window.clipboardData.setData("text", asText);
  269. }
  270. else {
  271. this.copiedStructure = asText;
  272. }
  273. };
  274. Utils.prototype.onPasteStructure = function () {
  275. if (this._viewer.selectedNode === null) {
  276. return;
  277. }
  278. var asText = (window.clipboardData !== undefined) ? window.clipboardData.getData("text") : this.copiedStructure;
  279. var isJson = asText.length > 0 && asText[0] == "{" && asText[asText.length - 1] == "}";
  280. var structure = JSON.parse(asText);
  281. var action = this._viewer.selectedNode;
  282. if (structure.type === ActionsBuilder.Type.TRIGGER && action !== this._viewer.root) {
  283. alert("You can't paste a trigger if the selected node isn't the root object");
  284. return;
  285. }
  286. if (structure.type !== ActionsBuilder.Type.TRIGGER && action === this._viewer.root) {
  287. alert("You can't paste an action or condition if the selected node is the root object");
  288. return;
  289. }
  290. this.loadFromJSON(structure, action);
  291. this._viewer.update();
  292. };
  293. Utils.prototype.loadFromJSON = function (graph, startAction) {
  294. var _this = this;
  295. if (startAction === null) {
  296. for (var i = 0; i < this._viewer.root.children.length; i++) {
  297. this._viewer.removeAction(this._viewer.root.children[i], true);
  298. }
  299. this._viewer.root.clearChildren();
  300. }
  301. var load = function (root, parent, detached, combine) {
  302. if (parent === null) {
  303. parent = _this._viewer.root;
  304. }
  305. var newAction = null;
  306. if (root.type !== ActionsBuilder.Type.OBJECT && root.type !== ActionsBuilder.Type.SCENE) {
  307. var action = _this._viewer.addAction(parent, root.type, ActionsBuilder.Elements.GetElementFromName(root.name));
  308. for (var i = 0; i < root.properties.length; i++) {
  309. var targetType = root.properties[i].targetType;
  310. if (targetType === undefined) {
  311. targetType = "MeshProperties";
  312. }
  313. action.propertiesResults[i] = { value: root.properties[i].value, targetType: targetType };
  314. }
  315. var node = action.node;
  316. node.detached = root.detached;
  317. if (detached) {
  318. node.rect.attr("fill", _this._viewer.getNodeColor(action.type, detached));
  319. }
  320. if (root.combine !== undefined) {
  321. for (var i = 0; i < root.combine.length; i++) {
  322. load(root.combine[i], action, detached, true);
  323. }
  324. }
  325. if (!combine) {
  326. parent = parent.children[parent.children.length - 1];
  327. }
  328. }
  329. for (var i = 0; i < root.children.length; i++) {
  330. load(root.children[i], newAction !== null && newAction.combineArray !== null ? newAction.hub : parent, root.detached, false);
  331. }
  332. };
  333. load(graph, startAction, false, false);
  334. this._viewer.update();
  335. };
  336. Utils.prototype.createJSON = function (root) {
  337. var action = {
  338. type: root.type,
  339. name: root.name,
  340. detached: root.node.detached,
  341. children: new Array(),
  342. combine: new Array(),
  343. properties: new Array()
  344. };
  345. for (var i = 0; i < root.properties.length; i++) {
  346. action.properties.push({
  347. name: root.properties[i].text,
  348. value: root.propertiesResults[i].value,
  349. targetType: root.propertiesResults[i].targetType
  350. });
  351. }
  352. if (root.combineArray !== null) {
  353. for (var i = 0; i < root.combineArray.length; i++) {
  354. var combinedAction = root.combineArray[i];
  355. action.combine.push(this.createJSON(combinedAction));
  356. }
  357. root = root.children[0];
  358. }
  359. for (var i = 0; i < root.children.length; i++) {
  360. action.children.push(this.createJSON(root.children[i]));
  361. }
  362. return action;
  363. };
  364. Utils.prototype.setElementVisible = function (element, visible) {
  365. element.style.display = visible ? "block" : "none";
  366. };
  367. return Utils;
  368. })();
  369. ActionsBuilder.Utils = Utils;
  370. })(ActionsBuilder || (ActionsBuilder = {}));