actionsbuilder.viewer.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. var ActionsBuilder;
  2. (function (ActionsBuilder) {
  3. var Viewer = (function () {
  4. /*
  5. * Constructor
  6. * @param type: the root type object (OBJECT or SCENE)
  7. */
  8. function Viewer(type) {
  9. var _this = this;
  10. this.objectName = "Unnamed Object";
  11. this.zoom = 1.0;
  12. this._firstUpdate = true;
  13. // Get HTML elements
  14. this.viewerContainer = document.getElementById("GraphContainerID");
  15. this.viewerElement = document.getElementById("GraphElementID");
  16. // Create element
  17. this.paper = Raphael("GraphElementID", screen.width, screen.height);
  18. // Configure this
  19. //var name = type === Type.OBJECT ? "Unnamed object" : "Scene";
  20. this.root = this.addAction(null, type, { name: this.objectName, text: this.objectName, properties: [], description: "" });
  21. this.selectedNode = null;
  22. // Configure events
  23. window.addEventListener("resize", function (event) {
  24. _this.onResize(event);
  25. });
  26. window.addEventListener("mousemove", function (event) {
  27. _this.onMove(event);
  28. });
  29. this.paper.canvas.addEventListener("click", function (event) {
  30. _this.onClick(event);
  31. });
  32. // Load modules
  33. this._toolbar = new ActionsBuilder.Toolbar(this);
  34. this._contextMenu = new ActionsBuilder.ContextMenu(this);
  35. this.parameters = new ActionsBuilder.Parameters(this);
  36. this.utils = new ActionsBuilder.Utils(this);
  37. // Finish
  38. this.parameters.parametersHelpElement.textContent = Viewer._DEFAULT_INFO_MESSAGE;
  39. this.onResize(null);
  40. }
  41. Object.defineProperty(Viewer, "NODE_WIDTH", {
  42. get: function () {
  43. return Viewer._NODE_WIDTH;
  44. },
  45. enumerable: true,
  46. configurable: true
  47. });
  48. Object.defineProperty(Viewer, "NODE_HEIGHT", {
  49. get: function () {
  50. return Viewer._NODE_HEIGHT;
  51. },
  52. enumerable: true,
  53. configurable: true
  54. });
  55. Object.defineProperty(Viewer, "NODE_MINIMIZED_WIDTH", {
  56. get: function () {
  57. return Viewer._NODE_MINIMIZE_WIDTH;
  58. },
  59. enumerable: true,
  60. configurable: true
  61. });
  62. Object.defineProperty(Viewer, "VERTICAL_OFFSET", {
  63. get: function () {
  64. return Viewer._VERTICAL_OFFSET;
  65. },
  66. enumerable: true,
  67. configurable: true
  68. });
  69. /*
  70. * Resize event
  71. * @param event: the resize event
  72. */
  73. Viewer.prototype.onResize = function (event) {
  74. var tools = document.getElementById("ToolsButtonsID");
  75. this.viewerContainer.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 - 50 + "px";
  76. this.viewerElement.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 - 50 + "px";
  77. this.parameters.onResize();
  78. this._toolbar.onResize();
  79. if (this.paper.height < window.innerHeight) {
  80. this.paper.setSize(this.paper.width, window.innerHeight);
  81. }
  82. if (this._firstUpdate) {
  83. this.viewerElement.scrollLeft = ((this.viewerElement.scrollWidth / 2) - (this.viewerElement.getBoundingClientRect().width / 2));
  84. this._firstUpdate = false;
  85. }
  86. };
  87. /*
  88. * Handles the onMove event
  89. * @param event: the onMove mouse event
  90. */
  91. Viewer.prototype.onMove = function (event) {
  92. this.mousex = event.clientX - this.paper.canvas.getBoundingClientRect().left;
  93. this.mousey = event.clientY - this.paper.canvas.getBoundingClientRect().top;
  94. };
  95. /*
  96. * Handles the onClick event to get selected node
  97. * @param event: the onClick mouse event
  98. */
  99. Viewer.prototype.onClick = function (event) {
  100. if (this._contextMenu.showing) {
  101. return;
  102. }
  103. // Reset selected node
  104. if (this.selectedNode !== null) {
  105. var node = this.selectedNode.node;
  106. node.rect.attr("fill", this.getNodeColor(this.selectedNode.type, node.detached));
  107. }
  108. // Configure new selected node
  109. var result = this.traverseGraph(null, this.mousex, this.mousey, true);
  110. if (result.hit) {
  111. this.selectedNode = result.action;
  112. var node = this.selectedNode.node;
  113. node.rect.attr("fill", this.getSelectedNodeColor(this.selectedNode.type, node.detached));
  114. }
  115. else {
  116. this.selectedNode = null;
  117. this.parameters.clearParameters();
  118. this.parameters.parametersHelpElement.textContent = Viewer._DEFAULT_INFO_MESSAGE;
  119. }
  120. };
  121. /*
  122. * Set the color theme of the viewer
  123. * @param color: the color theme ( ex: "rgb(64, 64, 64)" )
  124. */
  125. Viewer.prototype.setColorTheme = function (color) {
  126. this.paper.canvas.style.background = color;
  127. };
  128. /*
  129. * Returns the color according to the given parameters
  130. * @param action: the action used to select the color
  131. * @param detached: if the node is attached to its parent or not
  132. */
  133. Viewer.prototype.getNodeColor = function (type, detached) {
  134. if (detached) {
  135. return Raphael.rgb(96, 122, 14);
  136. }
  137. switch (type) {
  138. case ActionsBuilder.Type.TRIGGER:
  139. return Raphael.rgb(133, 154, 185);
  140. break;
  141. case ActionsBuilder.Type.ACTION:
  142. return Raphael.rgb(182, 185, 132);
  143. break;
  144. case ActionsBuilder.Type.FLOW_CONTROL:
  145. return Raphael.rgb(185, 132, 140);
  146. break;
  147. case ActionsBuilder.Type.OBJECT:
  148. case ActionsBuilder.Type.SCENE:
  149. return Raphael.rgb(255, 255, 255);
  150. break;
  151. default: break;
  152. }
  153. return null;
  154. };
  155. /*
  156. * Returns the selected node color according to the given parameters
  157. * @param action: the action used to select the color
  158. * @param detached: if the node is attached to its parent or not
  159. */
  160. Viewer.prototype.getSelectedNodeColor = function (type, detached) {
  161. if (detached) {
  162. return Raphael.rgb(96, 122, 14);
  163. }
  164. switch (type) {
  165. case ActionsBuilder.Type.TRIGGER:
  166. return Raphael.rgb(41, 129, 255);
  167. break;
  168. case ActionsBuilder.Type.ACTION:
  169. return Raphael.rgb(255, 220, 42);
  170. break;
  171. case ActionsBuilder.Type.FLOW_CONTROL:
  172. return Raphael.rgb(255, 41, 53);
  173. break;
  174. case ActionsBuilder.Type.OBJECT:
  175. case ActionsBuilder.Type.SCENE:
  176. return Raphael.rgb(255, 255, 255);
  177. break;
  178. default: break;
  179. }
  180. return null;
  181. };
  182. /*
  183. * Removes the given action from the graph
  184. * @param action: the action to remove
  185. * @param removeChildren: if remove the branch or not
  186. */
  187. Viewer.prototype.removeAction = function (action, removeChildren) {
  188. // If selected node is combine
  189. if (action.parent !== null && action.parent.hub === action) {
  190. this.removeAction(action.parent, false);
  191. return;
  192. }
  193. // Basic suppress
  194. this.removeNode(action.node);
  195. if (action.combineArray !== null) {
  196. this.removeNode(action.hub.node);
  197. // Remove combine array
  198. for (var i = 0; i < action.combineArray.length; i++) {
  199. this.removeNode(action.combineArray[i].node);
  200. }
  201. action.combineArray.length = 0;
  202. }
  203. if (removeChildren) {
  204. for (var i = 0; i < action.children.length; i++) {
  205. this.removeAction(action.children[i], removeChildren);
  206. }
  207. action.clearChildren();
  208. }
  209. else {
  210. for (var i = 0; i < action.children.length; i++) {
  211. action.parent.addChild(action.children[i]);
  212. action.children[i].parent = action.parent;
  213. }
  214. }
  215. };
  216. /*
  217. * Removes the given node (not the action)
  218. * @param node: the node to remove
  219. */
  220. Viewer.prototype.removeNode = function (node) {
  221. node.rect.remove();
  222. node.text.remove();
  223. if (node.line !== null) {
  224. node.line.remove();
  225. }
  226. };
  227. /*
  228. * Updates the graph viewer
  229. */
  230. Viewer.prototype.update = function () {
  231. var _this = this;
  232. // Set root position
  233. this._setActionPosition(this.root, (this.paper.width / 2) - (Viewer.NODE_WIDTH / 2) * this.zoom, 10);
  234. // Sets node size
  235. var onSetNodeSize = function (node) {
  236. node.rect.attr("width", node.minimized ? Viewer.NODE_MINIMIZED_WIDTH : Viewer.NODE_WIDTH * _this.zoom);
  237. node.rect.attr("height", Viewer.NODE_HEIGHT * _this.zoom);
  238. node.text.attr("font-size", 11 * _this.zoom);
  239. };
  240. // First pass: set actions positions according to parents
  241. var onSetPositionPass = function (action, yPosition) {
  242. var node = action.node;
  243. var parent = action.parent !== null ? action.parent : null;
  244. // Set node properties (size, text size, etc.)
  245. if (action.combineArray !== null) {
  246. for (var i = 0; i < action.combineArray.length; i++) {
  247. var combinedNode = action.combineArray[i].node;
  248. onSetNodeSize(combinedNode);
  249. }
  250. }
  251. onSetNodeSize(node);
  252. // Set position from parent
  253. if (parent) {
  254. var parentx = parent.node.rect.attr("x");
  255. if (parent.combineArray !== null && parent.combineArray.length > 1) {
  256. parentx += parent.node.rect.attr("width") / 2;
  257. }
  258. _this._setActionPosition(action, parentx, yPosition);
  259. _this._setActionLine(action);
  260. }
  261. // Calculate total width for current action
  262. var totalSize = 0;
  263. for (var i = 0; i < action.children.length; i++) {
  264. var childNode = action.children[i].node;
  265. totalSize += childNode.rect.attr("width");
  266. }
  267. // Get values to place nodes according to the parent position
  268. var nodeWidth = node.rect.attr("width");
  269. var startingPositionX = node.rect.attr("x");
  270. // Set children positions
  271. for (var i = 0; i < action.children.length; i++) {
  272. var childAction = action.children[i];
  273. var childNode = childAction.node;
  274. var newPositionX = startingPositionX;
  275. if (childAction.combineArray !== null && childAction.combineArray.length > 1) {
  276. newPositionX -= (childNode.rect.attr("width") / 2) - nodeWidth / 2;
  277. }
  278. var newPositionY = yPosition + Viewer.VERTICAL_OFFSET * _this.zoom;
  279. onSetPositionPass(childAction, newPositionY);
  280. _this._setActionPosition(childAction, newPositionX, newPositionY);
  281. _this._setActionLine(childAction);
  282. }
  283. };
  284. onSetPositionPass(this.root, 10 * this.zoom);
  285. // Seconds pass, get sizes of groups
  286. var onGetSizePass = function (action, maxSize) {
  287. var mySize = 0;
  288. if (action.combineArray !== null) {
  289. for (var i = 0; i < action.combineArray.length; i++) {
  290. mySize += action.combineArray[i].node.rect.attr("width");
  291. }
  292. }
  293. else {
  294. mySize = action.node.rect.attr("width");
  295. }
  296. if (mySize > maxSize) {
  297. maxSize = mySize;
  298. }
  299. for (var i = 0; i < action.children.length; i++) {
  300. maxSize = onGetSizePass(action.children[i], maxSize);
  301. }
  302. return maxSize;
  303. };
  304. // Resize canvas
  305. var onResizeCanvas = function (action) {
  306. var node = action.node;
  307. var nodex = node.rect.attr("x");
  308. var nodey = node.rect.attr("y");
  309. if (nodex < 0 || nodex > _this.paper.width) {
  310. _this.paper.setSize(_this.paper.width + 1000, _this.paper.height);
  311. _this._setActionPosition(_this.root, (_this.paper.width / 2) - (Viewer.NODE_WIDTH / 2) * _this.zoom, 10);
  312. }
  313. if (nodey > _this.paper.height) {
  314. _this.paper.setSize(_this.paper.width, _this.paper.height + 1000);
  315. _this._setActionPosition(_this.root, (_this.paper.width / 2) - (Viewer.NODE_WIDTH / 2) * _this.zoom, 10);
  316. }
  317. };
  318. var widths = new Array();
  319. for (var i = 0; i < this.root.children.length; i++) {
  320. var trigger = this.root.children[i];
  321. var triggerResult = { triggerWidth: onGetSizePass(trigger, 0), childrenWidths: new Array() };
  322. if (trigger.children.length > 0) {
  323. triggerResult.triggerWidth = 0;
  324. }
  325. for (var j = 0; j < trigger.children.length; j++) {
  326. var actionWidth = onGetSizePass(trigger.children[j], 0);
  327. triggerResult.triggerWidth += actionWidth + 15;
  328. triggerResult.childrenWidths.push({
  329. triggerWidth: actionWidth,
  330. childrenWidths: null
  331. });
  332. }
  333. widths.push(triggerResult);
  334. }
  335. // Third pass, set positions of nodes
  336. var onSetNodePosition = function (action, widthArray, isChild) {
  337. var actionsCount = action.children.length;
  338. var actionsMiddle = actionsCount % 2;
  339. var actionsHasMiddle = actionsMiddle !== 0;
  340. var actionsLeftOffset = 0;
  341. var actionsRightOffset = 0;
  342. var actionWidth = action.node.rect.attr("width");
  343. if (actionsHasMiddle && actionsCount > 1) {
  344. var middle = Math.floor(actionsCount / 2);
  345. actionsLeftOffset += widthArray[middle].triggerWidth / 2;
  346. actionsRightOffset += widthArray[middle].triggerWidth / 2;
  347. }
  348. // Move left
  349. var leftStart = actionsHasMiddle ? Math.floor(actionsCount / 2) - 1 : (actionsCount / 2) - 1;
  350. for (var i = leftStart; i >= 0; i--) {
  351. var child = action.children[i];
  352. var node = child.node;
  353. var width = (widthArray[i].triggerWidth) + 15;
  354. _this._setActionPosition(action.children[i], node.rect.attr("x") - actionsLeftOffset - (width / 2), node.rect.attr("y"));
  355. _this._setActionLine(child);
  356. onResizeCanvas(child);
  357. actionsLeftOffset += width;
  358. }
  359. // Move right
  360. var rightStart = actionsHasMiddle ? Math.round(actionsCount / 2) : actionsCount / 2;
  361. for (var i = rightStart; i < actionsCount; i++) {
  362. var child = action.children[i];
  363. var node = child.node;
  364. var width = (widthArray[i].triggerWidth) + 15;
  365. _this._setActionPosition(action.children[i], node.rect.attr("x") + actionsRightOffset + (width / 2), node.rect.attr("y"));
  366. _this._setActionLine(child);
  367. onResizeCanvas(child);
  368. actionsRightOffset += width;
  369. }
  370. };
  371. onSetNodePosition(this.root, widths, false);
  372. for (var i = 0; i < this.root.children.length; i++) {
  373. onSetNodePosition(this.root.children[i], widths[i].childrenWidths, true);
  374. }
  375. };
  376. /*
  377. * Adds an action to the graph viewer and returns it
  378. * @param parent: the parent action
  379. * @param type: the action type
  380. * @param element: the Actions Builder type (TRIGGERS, ACTIONS, FLOW_CONTROLS)
  381. */
  382. Viewer.prototype.addAction = function (parent, type, element) {
  383. var node = this._createNode(element.text, type, parent === null);
  384. var action = new ActionsBuilder.Action(node);
  385. if (element.name === "CombineAction") {
  386. action.combineArray = new Array();
  387. var hubElement = ActionsBuilder.Elements.FLOW_CONTROLS[ActionsBuilder.Elements.FLOW_CONTROLS.length - 1];
  388. var hub = this.addAction(action, ActionsBuilder.Type.FLOW_CONTROL, hubElement);
  389. action.hub = hub;
  390. action.addChild(hub);
  391. this._createActionAnimation(hub);
  392. }
  393. action.name = element.name;
  394. action.properties = element.properties;
  395. action.type = type;
  396. // Configure properties
  397. for (var i = 0; i < action.properties.length; i++) {
  398. action.propertiesResults.push({ targetType: action.properties[i].targetType, value: action.properties[i].value });
  399. }
  400. if (action.properties !== null && action.properties.length > 0) {
  401. if (action.properties[0].text === "target") {
  402. action.propertiesResults[0].value = this.objectName;
  403. }
  404. }
  405. if (parent !== null) {
  406. if (parent.combineArray === null) {
  407. parent.addChild(action);
  408. }
  409. else if (parent.combineArray !== null && action.name !== "Hub") {
  410. parent.combineArray.push(action);
  411. action.parent = parent;
  412. action.combineAction = parent;
  413. parent.node.text.attr("text", "");
  414. }
  415. }
  416. // Create animation
  417. this._createActionAnimation(action);
  418. return action;
  419. };
  420. /*
  421. * Traverses the graph viewer and returns if an action
  422. * is selected at coordinates (x, y)
  423. * @param start: the start node. Can be null
  424. * @param x: the x coordinate
  425. * @param y: the y coordinate
  426. * @param traverseCombine: if we traverse combine actions children
  427. */
  428. Viewer.prototype.traverseGraph = function (start, x, y, traverseCombine) {
  429. if (start === null)
  430. start = this.root;
  431. var result = { action: start, hit: true };
  432. if (start.node.isPointInside(x, y)) {
  433. return result;
  434. }
  435. for (var i = 0; i < start.children.length; i++) {
  436. var action = start.children[i];
  437. if (action.node.isPointInside(x, y)) {
  438. result.hit = true;
  439. result.action = start.children[i];
  440. if (traverseCombine && action.combineArray !== null) {
  441. for (var j = 0; j < action.combineArray.length; j++) {
  442. if (action.combineArray[j].node.isPointInside(x, y)) {
  443. result.action = action.combineArray[j];
  444. break;
  445. }
  446. }
  447. }
  448. return result;
  449. }
  450. result = this.traverseGraph(action, x, y, traverseCombine);
  451. if (result.hit) {
  452. return result;
  453. }
  454. }
  455. result.hit = false;
  456. result.action = null;
  457. return result;
  458. };
  459. /*
  460. * Sets the action's position (node)
  461. * @param action: the action to place
  462. * @param x: the x position of the action
  463. * @param y: the y position of the action
  464. */
  465. Viewer.prototype._setActionPosition = function (action, x, y) {
  466. var node = action.node;
  467. var offsetx = node.rect.attr("x") - x;
  468. var parent = action.parent;
  469. if (parent !== null && parent.combineArray !== null && parent.combineArray.length > 1) {
  470. var parentNode = parent.node;
  471. x = parentNode.rect.attr("x") + (parent.node.rect.attr("width") / 2) - (node.rect.attr("width") / 2);
  472. }
  473. node.rect.attr("x", x);
  474. node.rect.attr("y", y);
  475. var textBBox = node.text.getBBox();
  476. var textWidth = 0;
  477. if (textBBox !== null && textBBox !== undefined) {
  478. textWidth = textBBox.width;
  479. }
  480. node.text.attr("x", x + node.rect.attr("width") / 2 - textWidth / 2);
  481. node.text.attr("y", y + node.rect.attr("height") / 2);
  482. if (action.combineArray !== null && action.combineArray.length > 0) {
  483. var length = 0;
  484. for (var i = 0; i < action.combineArray.length; i++) {
  485. var combinedAction = action.combineArray[i];
  486. var combinedNode = combinedAction.node;
  487. combinedNode.rect.attr("x", node.rect.attr("x") + length);
  488. combinedNode.rect.attr("y", node.rect.attr("y"));
  489. textBBox = combinedNode.text.getBBox();
  490. if (textBBox !== null) {
  491. textWidth = textBBox.width;
  492. }
  493. combinedNode.text.attr("x", combinedNode.rect.attr("x") + combinedNode.rect.attr("width") / 2 - textWidth / 2);
  494. combinedNode.text.attr("y", y + combinedNode.rect.attr("height") / 2);
  495. length += combinedNode.rect.attr("width");
  496. }
  497. node.rect.attr("width", length);
  498. }
  499. for (var i = 0; i < action.children.length; i++) {
  500. var child = action.children[i];
  501. this._setActionPosition(child, child.node.rect.attr("x") - offsetx, y + Viewer.VERTICAL_OFFSET * this.zoom);
  502. this._setActionLine(child);
  503. }
  504. };
  505. /*
  506. * Configures the line (link) between the action and its parent
  507. * @param action: the action to configure
  508. */
  509. Viewer.prototype._setActionLine = function (action) {
  510. if (action.node.line === null) {
  511. return;
  512. }
  513. var node = action.node;
  514. var nodex = node.rect.attr("x");
  515. var nodey = node.rect.attr("y");
  516. var nodeWidth = node.rect.attr("width");
  517. var nodeHeight = node.rect.attr("height");
  518. var parent = action.parent.node;
  519. var parentx = parent.rect.attr("x");
  520. var parenty = parent.rect.attr("y");
  521. var parentWidth = parent.rect.attr("width");
  522. var parentHeight = parent.rect.attr("height");
  523. if (node.detached) {
  524. node.line.attr("path", ["M", nodex, nodey, "L", nodex, nodey]);
  525. return;
  526. }
  527. var line1x = nodex + (nodeWidth / 2);
  528. var line1y = nodey;
  529. var line2y = line1y - (line1y - parenty - parentHeight) / 2;
  530. var line3x = parentx + (parentWidth / 2);
  531. var line4y = parenty + parentHeight;
  532. node.line.attr("path", ["M", line1x, line1y, "L", line1x, line2y, "L", line3x, line2y, "L", line3x, line4y]);
  533. };
  534. /*
  535. * Creates and returns a node
  536. * @param text: the text to draw in the nde
  537. * @param color: the node's color
  538. * @param noLine: if draw a line to the parent or not
  539. */
  540. Viewer.prototype._createNode = function (text, type, noLine) {
  541. var node = new ActionsBuilder.Node();
  542. var color = this.getNodeColor(type, false);
  543. node.rect = this.paper.rect(20, 20, Viewer.NODE_WIDTH, Viewer.NODE_HEIGHT, 0);
  544. node.rect.attr("fill", color);
  545. node.text = this.paper.text(20, 20, text);
  546. node.text.attr("font-size", 11);
  547. node.text.attr("text-anchor", "start");
  548. node.text.attr("font-family", "Sinkin Sans Light");
  549. if (!noLine) {
  550. node.line = this.paper.path("");
  551. node.line.attr("stroke", color);
  552. }
  553. return node;
  554. };
  555. /*
  556. * Creates the drag animation
  557. * @param action: the action to animate
  558. */
  559. Viewer.prototype._createActionAnimation = function (action) {
  560. var _this = this;
  561. var node = action.node;
  562. var finished = true;
  563. var nodex = 0;
  564. var nodey = 0;
  565. var onMove = function (dx, dy, x, y) { };
  566. var onStart = function (x, y, event) {
  567. if (node.minimized) {
  568. return;
  569. }
  570. if (finished) {
  571. nodex = node.rect.attr("x");
  572. nodey = node.rect.attr("y");
  573. }
  574. finished = false;
  575. node.rect.animate({
  576. x: node.rect.attr("x") - 10,
  577. y: node.rect.attr("y"),
  578. width: (Viewer.NODE_WIDTH + 20) * _this.zoom,
  579. height: (Viewer.NODE_HEIGHT + 10) * _this.zoom,
  580. opacity: 0.25
  581. }, 500, ">");
  582. };
  583. var onEnd = function (event) {
  584. if (!node.minimized) {
  585. node.rect.animate({
  586. x: nodex,
  587. y: nodey,
  588. width: Viewer.NODE_WIDTH * _this.zoom,
  589. height: Viewer.NODE_HEIGHT * _this.zoom,
  590. opacity: 1.0
  591. }, 500, ">", function () { finished = true; });
  592. }
  593. var dragResult = _this.traverseGraph(null, _this.mousex, _this.mousey, true);
  594. if (dragResult.hit && dragResult.action === action || !dragResult.hit) {
  595. // Create parameters. Action can be null
  596. _this.parameters.createParameters(action);
  597. }
  598. else {
  599. // Manage drag'n'drop
  600. if (dragResult.action.children.length > 0 && action.type !== ActionsBuilder.Type.TRIGGER) {
  601. return;
  602. }
  603. if (action.type === ActionsBuilder.Type.TRIGGER && dragResult.action !== _this.root) {
  604. return;
  605. }
  606. if (action.type === ActionsBuilder.Type.ACTION && dragResult.action === _this.root) {
  607. return;
  608. }
  609. if (action.type === ActionsBuilder.Type.FLOW_CONTROL && (dragResult.action === _this.root || dragResult.action.type === ActionsBuilder.Type.FLOW_CONTROL)) {
  610. return;
  611. }
  612. if (action === dragResult.action.parent) {
  613. return;
  614. }
  615. if (action.parent !== null && action.parent.combineArray !== null) {
  616. return;
  617. }
  618. // Reset node
  619. node.rect.stop(node.rect.animation);
  620. node.text.stop(node.text.animation);
  621. node.rect.undrag();
  622. node.text.undrag();
  623. node.rect.attr("opacity", 1.0);
  624. node.rect.attr("width", Viewer.NODE_WIDTH);
  625. node.rect.attr("height", Viewer.NODE_HEIGHT);
  626. if (action.parent !== null) {
  627. // Configure drag'n'drop
  628. action.parent.removeChild(action);
  629. dragResult.action.addChild(action);
  630. _this.update();
  631. _this._createActionAnimation(action);
  632. }
  633. }
  634. };
  635. node.rect.drag(onMove, onStart, onEnd);
  636. node.text.drag(onMove, onStart, onEnd);
  637. };
  638. // Statics
  639. Viewer._NODE_WIDTH = 150;
  640. Viewer._NODE_HEIGHT = 25;
  641. Viewer._NODE_MINIMIZE_WIDTH = 50;
  642. Viewer._VERTICAL_OFFSET = 70;
  643. Viewer._DEFAULT_INFO_MESSAGE = "Select or add a node to customize actions";
  644. return Viewer;
  645. })();
  646. ActionsBuilder.Viewer = Viewer;
  647. })(ActionsBuilder || (ActionsBuilder = {}));
  648. //# sourceMappingURL=actionsbuilder.viewer.js.map