workspace-variables.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. let creatingVariable = false;
  2. let variables = [];
  3. /*
  4. * Workspace position modal attributes
  5. */
  6. let $workspacePositionModal = $("#workspacePositionModal");
  7. let $workspacePositionModalBody = $("#workspacePositionModalBody");
  8. let $workspacePositionModalPositionsSelect = $(
  9. "#workspacePositionModalPositionsSelect"
  10. );
  11. let workspaceToBeAddId = null;
  12. let workspaceToBeAddVariable = null;
  13. let workspaceToBeAddAfterComponent = null;
  14. function addVariable(
  15. variable = {name: "", type: dataTypes.String, value: ""},
  16. id = null
  17. ) {
  18. subMenuOptions.style.display = "none";
  19. if (creatingVariable === false) {
  20. // Blocking future variable creation until finish the current creation
  21. creatingVariable = true;
  22. let html = "";
  23. html += `<div class="col-3 p-2" id="variableCreationDiv">`;
  24. html += ` <div class="card">`;
  25. html += ` <div class="card-header">`;
  26. html += ` <h3>Adicionar variável</h3>`;
  27. html += ` </div>`;
  28. html += ` <div class="card-body">`;
  29. html += ` <div class="row">`;
  30. html += ` <div class="col-3">`;
  31. html += ` <label for="variableName">Nome: </label>`;
  32. html += ` </div>`;
  33. html += ` <div class="col-9">`;
  34. html += ` <input id="variableName" type="text" value="${variable.name}">`;
  35. html += ` </div>`;
  36. html += ` </div>`;
  37. html += ` <div class="row">`;
  38. html += ` <div class="col-3">`;
  39. html += ` <label for="variableType">Tipo: </label>`;
  40. html += ` </div>`;
  41. html += ` <div class="col-9">`;
  42. html += ` <select id="variableType">`;
  43. // Filling data type select with available data types in {data-types.js}
  44. for (let dataType in dataTypes) {
  45. if (dataType === variable.type)
  46. html += ` <option value="${dataType}" selected>${dataTypes[dataType]}</option>`;
  47. else
  48. html += ` <option value="${dataType}">${dataTypes[dataType]}</option>`;
  49. }
  50. html += ` </select>`;
  51. html += ` </div>`;
  52. html += ` </div>`;
  53. html += ` <div class="row">`;
  54. html += ` <div class="col-3">`;
  55. html += ` <label for="variableValue">Valor: </label>`;
  56. html += ` </div>`;
  57. html += ` <div class="col-9">`;
  58. html += ` <input id="variableValue" type="text" value="${variable.value}">`;
  59. html += ` </div>`;
  60. html += ` </div>`;
  61. html += ` </div>`;
  62. html += ` <div class="card-footer">`;
  63. html += ` <div class="row">`;
  64. html += ` <div class="col-6">`;
  65. html += ` <button type="button" class="btn btn-warning" accesskey="c" onclick="cancelAddVariable(${id})">Cancelar</button>`;
  66. html += ` </div>`;
  67. html += ` <div class="col-6">`;
  68. html += ` <button type="button" class="btn btn-info" accesskey="s" onclick="addVariableToCode({id: ${id}})">Salvar</button>`;
  69. html += ` </div>`;
  70. html += ` </div>`;
  71. html += ` </div>`;
  72. html += ` </div>`;
  73. html += `</div>`;
  74. $workspace.append(html);
  75. }
  76. }
  77. function addVariableToCode({id = null}) {
  78. let code = "";
  79. let variable = {
  80. name: $("#variableName").val(),
  81. type: $("#variableType").val(),
  82. value: $("#variableValue").val(),
  83. };
  84. code = mountCodeFromVariable(variable);
  85. // Sending code to screen
  86. addCodeToScreen({id: id, code: code, variable: variable});
  87. // Removing div form for variable creation
  88. $("#variableCreationDiv").remove();
  89. // Enabling future variable creation
  90. creatingVariable = false;
  91. }
  92. function cancelAddVariable(id = null) {
  93. // Removing div form for variable creation
  94. $("#variableCreationDiv").remove();
  95. if (id != null) $(`#variableDiv${id}`).show();
  96. // Enabling future variable creation
  97. creatingVariable = false;
  98. }
  99. function editVariable(
  100. id = null,
  101. variable = {name: "", type: dataTypes.String, value: ""}
  102. ) {
  103. $(`#variableDiv${id}`).hide();
  104. addVariable(variable, id);
  105. }
  106. function deleteVariable(variableIndex = null, subMenuOptionId) {
  107. trashSound.play();
  108. document.getElementById(subMenuOptionId).remove();
  109. for (let i = 0; i < variables[variableIndex].attributions.length; i++) {
  110. variables[variableIndex].attributions[i].parentElement.parentElement.remove();
  111. }
  112. for (let i = 0; i < variables[variableIndex].usages.length; i++) {
  113. variables[variableIndex].usages[i].remove();
  114. }
  115. variables.splice(variableIndex, 1);
  116. // Refresh variables sub menu
  117. buildVariablesSubmenu();
  118. }
  119. function addCodeToScreen({
  120. id = null,
  121. code = "Some code",
  122. variable = {name: "", type: "String", value: ""},
  123. }) {
  124. let html = ``;
  125. if (id != null) {
  126. $(`#mainCode${id}`).html(`<span>${code}</span>`);
  127. $(`#globalMenu${id}`).val(code);
  128. $(`#positionCode${id}`).html(
  129. `<button type="button" class="btn btn-dark" id="positionCode${id}" onclick='askWhereToPlaceTheVariable(${id}, ${JSON.stringify(
  130. variable
  131. )})'>Posicionar</button>`
  132. );
  133. $(`#editCode${id}`).html(
  134. `<button type="button" class="btn btn-info" id="editCodeButton${id}" onclick='editVariable(${id}, ${JSON.stringify(
  135. variable
  136. )})'>Editar</button>`
  137. );
  138. $(`#variableDiv${id}`).show();
  139. updateVariable({
  140. id: id,
  141. name: variable.name,
  142. type: variable.type,
  143. value: variable.value,
  144. code: code,
  145. element: document.getElementById(`variableDiv${id}`),
  146. });
  147. } else {
  148. id = universalId.next();
  149. html += `<div class="col-3 p-2" id="variableDiv${id}">`;
  150. html += ` <input type="hidden" id="globalMenu${id}" value="${code}"/>`;
  151. html += ` <div class="card" id="variableCard${id}">`;
  152. html += ` <div class="card-header" id="variableCardHeader${id}">`;
  153. html += ` <h4>Variável</h4>`;
  154. html += ` </div>`;
  155. html += ` <div class="card-body" id="variableCardBody${id}">`;
  156. html += ` <div class="row code-line" id="codeLine${id}">`;
  157. html += ` <div class="col-2 line-number" id="mainLine${id}"><span>${getCurrentMainLineNumber()}</span></div>`;
  158. html += ` <div class="col-10 line-code" id="mainCode${id}"><span>${code}</span></div>`;
  159. html += ` </div>`;
  160. html += ` </div>`;
  161. html += ` <div class="card-footer" id="variableCardFooter${id}">`;
  162. html += ` <div class="row">`;
  163. html += ` <div class="col-4" id="deleteCode${id}">`;
  164. html += ` <button type="button" class="btn btn-danger" id="deleteCodeButton${id}" onclick="deleteVariable(${id})">Remover</button>`;
  165. html += ` </div>`;
  166. html += ` <div class="col-4 align-self-center" id="positionCode${id}">`;
  167. html += ` <button type="button" class="btn btn-dark" id="positionCodeButton${id}" onclick='askWhereToPlaceTheVariable(${id}, ${JSON.stringify(
  168. variable
  169. )})'>Posicionar</button>`;
  170. html += ` </div>`;
  171. html += ` <div class="col-3" id="editCode${id}">`;
  172. html += ` <button type="button" class="btn btn-info" id="editCodeButton${id}" onclick='editVariable(${id}, ${JSON.stringify(
  173. variable
  174. )})'>Editar</button>`;
  175. html += ` </div>`;
  176. html += ` </div>`;
  177. html += ` </div>`;
  178. html += ` </div>`;
  179. html += `</div>`;
  180. // Saving reference to variable in variables array
  181. variables.push({
  182. id: id,
  183. name: variable.name,
  184. type: variable.type,
  185. value: variable.value,
  186. code: code,
  187. element: document.getElementById(`variableDiv${universalId.next()}`),
  188. attributions: [],
  189. usages: []
  190. });
  191. }
  192. }
  193. function askWhereToPlaceTheVariable(
  194. id = null,
  195. variable = {name: "", type: "String", value: ""}
  196. ) {
  197. if (buildAllWorkspaceComponentsSelect()) {
  198. $workspacePositionModal.modal("toggle");
  199. $workspacePositionModalPositionsSelect.focus();
  200. workspaceToBeAddId = id;
  201. workspaceToBeAddVariable = variable;
  202. } else {
  203. addVariableToMain({id: id, variable: variable});
  204. }
  205. }
  206. function buildAllWorkspaceComponentsSelect() {
  207. let allWorkspaceComponents = $(
  208. `input[type="hidden"][name="${WORKSPACE_COMPONENTS_NAME}"]`
  209. );
  210. let selectOptions = [];
  211. if (allWorkspaceComponents.length > 0) {
  212. for (let i = 0; i < allWorkspaceComponents.length; i++) {
  213. selectOptions.push(
  214. `<option value="${allWorkspaceComponents[i].attributes.universalworkspaceid.value}">${allWorkspaceComponents[i].value}</option>`
  215. );
  216. }
  217. $workspacePositionModalPositionsSelect.empty(); // remove old options
  218. selectOptions.forEach((object, index, array) => {
  219. $workspacePositionModalPositionsSelect.append($(object));
  220. });
  221. return true;
  222. } else {
  223. return false;
  224. }
  225. }
  226. $("#workspacePositionModalAddButton").click(() => {
  227. addVariableToMain({
  228. id: workspaceToBeAddId,
  229. variable: workspaceToBeAddVariable,
  230. afterComponentWithId: $workspacePositionModalPositionsSelect.val(),
  231. });
  232. // Reset variables for future use
  233. workspaceToBeAddId = null;
  234. workspaceToBeAddVariable = null;
  235. // Hide position modal
  236. $workspacePositionModal.modal("toggle");
  237. });
  238. function updateVariable(
  239. variable = {
  240. id: 0,
  241. name: "Name",
  242. type: "String",
  243. value: "Value",
  244. code: 'String Name = "value"',
  245. element: document.getElementById("body"),
  246. }
  247. ) {
  248. for (let i = 0; i < variables.length; i++) {
  249. if (variables[i].id === variable.id) {
  250. variables[i] = variable;
  251. return;
  252. }
  253. }
  254. console.warn(
  255. `Can't find variable with id: ${variable.id} to update, please verify the variable universalId`
  256. );
  257. }