actionsbuilder.list.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. var ActionsBuilder;
  2. (function (ActionsBuilder) {
  3. var ListElement = (function () {
  4. function ListElement() {
  5. this.rect = null;
  6. this.text = null;
  7. this.name = "";
  8. this.type = ActionsBuilder.Type.TRIGGER;
  9. this.element = null;
  10. }
  11. return ListElement;
  12. })();
  13. ActionsBuilder.ListElement = ListElement;
  14. var List = (function () {
  15. /**
  16. * Constructor
  17. */
  18. function List(viewer) {
  19. var _this = this;
  20. this._listElements = new Array();
  21. // Get HTML elements
  22. this.listElement = document.getElementById("ListsElementID");
  23. this.triggersElement = document.getElementById("TriggersListID");
  24. this.actionsElement = document.getElementById("ActionsListID");
  25. this.flowControlsElement = document.getElementById("FlowActionsListID");
  26. this._parentContainer = document.getElementById("ParentContainerID");
  27. // Configure this
  28. this._viewer = viewer;
  29. // Create elements (lists)
  30. this.triggersList = Raphael("TriggersListID", (25 * screen.width) / 100, 400);
  31. this.actionsList = Raphael("ActionsListID", (25 * screen.width) / 100, 400);
  32. this.flowControlsList = Raphael("FlowActionsListID", (25 * screen.width) / 100, 400);
  33. // Manage events
  34. window.addEventListener("resize", function (event) {
  35. _this.onResize(event);
  36. });
  37. }
  38. Object.defineProperty(List, "ELEMENT_HEIGHT", {
  39. get: function () {
  40. return 25;
  41. },
  42. enumerable: true,
  43. configurable: true
  44. });
  45. /**
  46. * Resize event that resizes the list element dynamically
  47. * @param event: the resize event
  48. */
  49. List.prototype.onResize = function (event) {
  50. var tools = document.getElementById("ToolsButtonsID");
  51. this.listElement.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 + "px";
  52. var listElementWidth = this.listElement.getBoundingClientRect().width;
  53. for (var i = 0; i < this._listElements.length; i++) {
  54. var rect = this._listElements[i].rect;
  55. rect.attr("width", listElementWidth - 40);
  56. }
  57. this.triggersList.setSize(listElementWidth, this.triggersList.height);
  58. this.actionsList.setSize(listElementWidth, this.triggersList.height);
  59. this.flowControlsList.setSize(listElementWidth, this.triggersList.height);
  60. };
  61. List.prototype.createListsElements = function () {
  62. var excludedTriggers = [6, 9, 10];
  63. var yPosition = 10;
  64. var textColor = Raphael.rgb(61, 72, 76);
  65. var whiteColor = Raphael.rgb(255, 255, 255);
  66. var configureTitle = function (listElement, rectColor) {
  67. listElement.text.attr("x", 15);
  68. listElement.rect.attr("fill", rectColor);
  69. listElement.text.attr("font-family", "Sinkin Sans Medium");
  70. listElement.text.attr("font-size", "11");
  71. };
  72. // Create triggers
  73. var triggers = this._createListElement(this.triggersList, yPosition, "TRIGGERS", ActionsBuilder.Type.TRIGGER, whiteColor, false);
  74. yPosition += List.ELEMENT_HEIGHT;
  75. configureTitle(triggers, Raphael.rgb(41, 129, 255));
  76. for (var i = 0; i < ActionsBuilder.Elements.TRIGGERS.length; i++) {
  77. var element = ActionsBuilder.Elements.TRIGGERS[i];
  78. if (this._viewer.root.type === ActionsBuilder.Type.OBJECT && excludedTriggers.indexOf(i) !== -1) {
  79. continue;
  80. }
  81. var trigger = this._createListElement(this.triggersList, yPosition, element.text, ActionsBuilder.Type.TRIGGER, textColor, true, element);
  82. trigger.rect.attr("fill", Raphael.rgb(133, 154, 185));
  83. yPosition += List.ELEMENT_HEIGHT;
  84. }
  85. yPosition += List.ELEMENT_HEIGHT;
  86. this.triggersElement.style.height = this.triggersList.canvas.style.height = yPosition + "px";
  87. this._createCollapseAnimation(this.triggersList, this.triggersElement, triggers, yPosition);
  88. // Create actions
  89. yPosition = 10;
  90. var actions = this._createListElement(this.actionsList, yPosition, "ACTIONS", ActionsBuilder.Type.ACTION, textColor, false);
  91. yPosition += List.ELEMENT_HEIGHT;
  92. configureTitle(actions, Raphael.rgb(255, 220, 42));
  93. for (var i = 0; i < ActionsBuilder.Elements.ACTIONS.length; i++) {
  94. var element = ActionsBuilder.Elements.ACTIONS[i];
  95. var action = this._createListElement(this.actionsList, yPosition, element.text, ActionsBuilder.Type.ACTION, textColor, true, element);
  96. action.rect.attr("fill", Raphael.rgb(182, 185, 132));
  97. yPosition += List.ELEMENT_HEIGHT;
  98. }
  99. yPosition += List.ELEMENT_HEIGHT;
  100. this.actionsElement.style.height = this.actionsList.canvas.style.height = yPosition + "px";
  101. this._createCollapseAnimation(this.actionsList, this.actionsElement, actions, yPosition);
  102. // Create flow controls
  103. yPosition = 10;
  104. var flowControls = this._createListElement(this.flowControlsList, yPosition, "FLOW CONTROLS", ActionsBuilder.Type.FLOW_CONTROL, whiteColor, false);
  105. yPosition += List.ELEMENT_HEIGHT;
  106. configureTitle(flowControls, Raphael.rgb(255, 41, 53));
  107. for (var i = 0; i < ActionsBuilder.Elements.FLOW_CONTROLS.length - 1; i++) {
  108. var element = ActionsBuilder.Elements.FLOW_CONTROLS[i];
  109. var flowControl = this._createListElement(this.flowControlsList, yPosition, element.text, ActionsBuilder.Type.FLOW_CONTROL, textColor, true, element);
  110. flowControl.rect.attr("fill", Raphael.rgb(185, 132, 140));
  111. yPosition += List.ELEMENT_HEIGHT;
  112. }
  113. yPosition += List.ELEMENT_HEIGHT;
  114. this.flowControlsElement.style.height = this.flowControlsList.canvas.style.height = yPosition + "px";
  115. this._createCollapseAnimation(this.flowControlsList, this.flowControlsElement, flowControls, yPosition);
  116. };
  117. /**
  118. * Clears the list of elements and removes the elements
  119. */
  120. List.prototype.clearLists = function () {
  121. for (var i = 0; i < this._listElements.length; i++) {
  122. this._removeListElement(this._listElements[i]);
  123. }
  124. this._listElements.splice(0, this._listElements.length - 1);
  125. };
  126. /**
  127. * Sets the color theme of the lists
  128. * @param color: the theme color
  129. */
  130. List.prototype.setColorTheme = function (color) {
  131. this.triggersList.canvas.style.backgroundColor = color;
  132. this.actionsList.canvas.style.backgroundColor = color;
  133. this.flowControlsList.canvas.style.backgroundColor = color;
  134. };
  135. /**
  136. * Creates a list element
  137. * @param paper: the Raphael.js paper
  138. * @param yPosition: the y position of the element
  139. * @param text: the element text
  140. * @param type: the element type (trigger, action, flow control)
  141. * @param textColor: the text color
  142. * @param drag: if the element should be drag'n'dropped
  143. */
  144. List.prototype._createListElement = function (paper, yPosition, text, type, textColor, drag, element) {
  145. var object = new ListElement();
  146. object.rect = paper.rect(10, yPosition, 300, List.ELEMENT_HEIGHT);
  147. object.text = paper.text(30, yPosition + object.rect.attr("height") / 2, text);
  148. object.text.attr("fill", textColor);
  149. object.text.attr("text-anchor", "start");
  150. object.text.attr("font-size", "12");
  151. object.text.attr("text-anchor", "start");
  152. object.text.attr("font-family", "Sinkin Sans Light");
  153. if (drag) {
  154. this._createListElementAnimation(object);
  155. }
  156. object.type = type;
  157. object.element = element;
  158. this._listElements.push(object);
  159. return object;
  160. };
  161. /**
  162. * Removes a list element
  163. * @param element: the element to remove
  164. */
  165. List.prototype._removeListElement = function (element) {
  166. element.rect.remove();
  167. element.text.remove();
  168. };
  169. /*
  170. * Creates the collapse animation of a list
  171. * @param paper: the list paper
  172. * @param htmlElement: the list div container
  173. * @param element: the list element to click on
  174. * @param expandedHeight: the height when the list is expanded
  175. */
  176. List.prototype._createCollapseAnimation = function (paper, htmlElement, element, expandedHeight) {
  177. var onClick = function (event) {
  178. var height = htmlElement.style.height;
  179. if (height === expandedHeight + "px") {
  180. htmlElement.style.height = paper.canvas.style.height = 35 + "px";
  181. }
  182. else {
  183. htmlElement.style.height = paper.canvas.style.height = expandedHeight + "px";
  184. }
  185. };
  186. element.rect.click(onClick);
  187. };
  188. /*
  189. * Creates the animation of a list element
  190. * @param element: the list element to animate
  191. */
  192. List.prototype._createListElementAnimation = function (element) {
  193. var _this = this;
  194. var onMove = function (dx, dy, x, y) { };
  195. var onStart = function (x, y, event) {
  196. _this._parentContainer.style.cursor = "copy";
  197. element.rect.animate({
  198. x: -10,
  199. opacity: 0.25
  200. }, 500, ">");
  201. element.text.animate({
  202. x: 10,
  203. opacity: 0.25
  204. }, 500, ">");
  205. };
  206. var onEnd = function (event) {
  207. _this._parentContainer.style.cursor = "default";
  208. element.rect.animate({
  209. x: 10,
  210. opacity: 1.0
  211. }, 500, "<");
  212. element.text.animate({
  213. x: 30,
  214. opacity: 1.0
  215. }, 500, "<");
  216. var dragResult = _this._viewer.traverseGraph(null, _this._viewer.mousex, _this._viewer.mousey, false);
  217. if (dragResult.hit) {
  218. if (element.type === ActionsBuilder.Type.TRIGGER && dragResult.action !== _this._viewer.root) {
  219. alert("Triggers can be dragged only on the root node (the mesh)");
  220. return;
  221. }
  222. if (element.type === ActionsBuilder.Type.ACTION && dragResult.action === _this._viewer.root) {
  223. alert("Please add a trigger before.");
  224. return;
  225. }
  226. if (element.type === ActionsBuilder.Type.FLOW_CONTROL && (dragResult.action === _this._viewer.root || (dragResult.action.type === ActionsBuilder.Type.FLOW_CONTROL && dragResult.action.parent.hub === null))) {
  227. return;
  228. }
  229. if (element.type === ActionsBuilder.Type.FLOW_CONTROL && dragResult.action.combineArray !== null) {
  230. alert("A condition cannot be handled by a Combine Action.");
  231. return;
  232. }
  233. if ((element.type === ActionsBuilder.Type.FLOW_CONTROL || element.type === ActionsBuilder.Type.ACTION) && dragResult.action.type === ActionsBuilder.Type.TRIGGER && dragResult.action.children.length > 0) {
  234. alert("Triggers can have only one child. Please add another trigger of same type.");
  235. return;
  236. }
  237. if (!(dragResult.action.combineArray !== null) && dragResult.action.children.length > 0 && dragResult.action.type !== ActionsBuilder.Type.TRIGGER && dragResult.action !== _this._viewer.root) {
  238. alert("An action can have only one child.");
  239. return;
  240. }
  241. _this._viewer.addAction(dragResult.action, element.type, element.element);
  242. _this._viewer.update();
  243. }
  244. };
  245. element.rect.drag(onMove, onStart, onEnd);
  246. element.text.drag(onMove, onStart, onEnd);
  247. };
  248. return List;
  249. })();
  250. ActionsBuilder.List = List;
  251. })(ActionsBuilder || (ActionsBuilder = {}));