variables.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Imports
  2. import { htmlOlCommandsVariables, generateUUID } from "./../../main";
  3. import {
  4. getVariableTypeByValue,
  5. htmlVariableScheme,
  6. variableScheme,
  7. VariableType,
  8. } from "./variables-schemes";
  9. import { deleteOperationByVariable } from "../operations/operations";
  10. // Imports
  11. export const variables = [];
  12. let variablesIndex = 0;
  13. // Creating variable
  14. export function createVariable() {
  15. const variable = Object.assign({}, variableScheme);
  16. variable.hash = generateUUID().replaceAll("-", "");
  17. variable.name = "var" + variablesIndex;
  18. variable.type = VariableType[0];
  19. variable.value = variable.type.defaultValue;
  20. variables.push(variable);
  21. variablesIndex++;
  22. let htmlVariable = htmlVariableScheme();
  23. htmlVariable = htmlVariable.replaceAll("<variableId>", variable.hash);
  24. htmlVariable = htmlVariable.replaceAll("<variableName>", variable.name);
  25. htmlOlCommandsVariables.insertAdjacentHTML("beforeend", htmlVariable);
  26. document
  27. .getElementById(`variable${variable.hash}Type`)
  28. .addEventListener("change", (ev) => {
  29. const variableHash = ev.target.getAttribute("variable-id");
  30. updateVariable(variableHash);
  31. });
  32. document
  33. .getElementById(`variable${variable.hash}Name`)
  34. .addEventListener("change", (ev) => {
  35. const variableHash = ev.target.getAttribute("variable-id");
  36. updateVariable(variableHash);
  37. });
  38. document
  39. .getElementById(`variable${variable.hash}Value`)
  40. .addEventListener("change", (ev) => {
  41. const variableHash = ev.target.getAttribute("variable-id");
  42. updateVariable(variableHash);
  43. });
  44. document
  45. .getElementById(`variable${variable.hash}Delete`)
  46. .addEventListener("click", (ev) => {
  47. const variableHash = ev.target.getAttribute("variable-id");
  48. deleteVariable(variableHash);
  49. });
  50. document
  51. .getElementById(`variable${variable.hash}Resume`)
  52. .addEventListener("focus", (ev) => {
  53. const variableHash = ev.target.getAttribute("variable-id");
  54. manageVariableEditing(variableHash);
  55. });
  56. document
  57. .getElementById(`variable${variable.hash}Display`)
  58. .addEventListener("keyup", (ev) => {
  59. const variableHash = ev.target.getAttribute("variable-id");
  60. if (ev.key === "Enter") manageVariableEditing(variableHash, true);
  61. });
  62. updateResume(variable.hash);
  63. updateAllVariableSelect();
  64. // Changing focus to the variable type after creation for screen readers
  65. document.getElementById(`variable${variable.hash}Type`).focus();
  66. }
  67. // *********************************************************************************
  68. // Updating variable
  69. // *********************************************************************************
  70. let variableName;
  71. let variableType;
  72. let variableValue;
  73. function updateVariable(hash) {
  74. const variable = getVariableByHash(hash);
  75. variableName = document.getElementById(`variable${hash}Name`).value;
  76. if (variable.name !== variableName) {
  77. variable.name = variableName;
  78. }
  79. variableType = getVariableTypeByValue(
  80. document.getElementById(`variable${hash}Type`).value
  81. );
  82. if (variable.type !== variableType) {
  83. variable.type = variableType;
  84. document.getElementById(`variable${hash}Value`).type =
  85. variable.type.htmlType;
  86. document.getElementById(`variable${hash}Value`).value =
  87. variable.type.defaultValue;
  88. }
  89. variableValue = document.getElementById(`variable${hash}Value`).value;
  90. if (variable.value !== variableValue) {
  91. variable.value = variableValue;
  92. }
  93. updateResume(hash);
  94. }
  95. // *********************************************************************************
  96. // Deleting variable
  97. export function deleteVariable(hash) {
  98. const variable = getVariableByHash(hash);
  99. document.getElementById(`variable${hash}Li`).remove();
  100. deleteOperationByVariable(variable);
  101. variables.splice(variables.indexOf(variable), 1);
  102. updateAllVariableSelect();
  103. }
  104. // Deleting all variable
  105. export function deleteAllVariable() {
  106. for (let i = 0; i < variables.length; i++) {
  107. document.getElementById(`variable${variables[i].hash}Li`).remove();
  108. }
  109. variables.length = 0;
  110. }
  111. // ***********************************************************************
  112. // Managing variable editing
  113. // ***********************************************************************
  114. const manageVariableEditing = (hash, enableForEditing = false) => {
  115. const variable = getVariableByHash(hash);
  116. const variableForm = document.getElementById(`variable${variable.hash}Form`);
  117. if (variable.editing && !enableForEditing) {
  118. // Editing fields
  119. variableForm.childNodes[1].childNodes[1].style.display = "none";
  120. variableForm.childNodes[1].childNodes[3].style.display = "none";
  121. variableForm.childNodes[1].childNodes[5].style.display = "none";
  122. variableForm.childNodes[1].childNodes[7].style.display = "none";
  123. // Resume field
  124. variableForm.childNodes[1].childNodes[9].style.display = "inline";
  125. variable.editing = false;
  126. } else if (enableForEditing) {
  127. // Editing fields
  128. variableForm.childNodes[1].childNodes[1].style.display = "inline";
  129. variableForm.childNodes[1].childNodes[3].style.display = "inline";
  130. variableForm.childNodes[1].childNodes[5].style.display = "inline";
  131. variableForm.childNodes[1].childNodes[7].style.display = "inline";
  132. // Resume field
  133. variableForm.childNodes[1].childNodes[9].style.display = "none";
  134. // Changing focus to firt child
  135. document.getElementById(`variable${variable.hash}Type`).focus();
  136. variable.editing = true;
  137. }
  138. };
  139. // ***********************************************************************
  140. // Updating variable resume
  141. // ***********************************************************************
  142. function updateResume(hash) {
  143. const resume = generateResume(hash);
  144. document.getElementById(`variable${hash}DisplayCode`).innerText = resume.display;
  145. document
  146. .getElementById(`variable${hash}Resume`)
  147. .setAttribute("title", resume.resume);
  148. }
  149. function generateResume(hash) {
  150. const variable = getVariableByHash(hash);
  151. let resume = `${variable.type.name} ${variable.name}, recebe ${variable.value}`;
  152. let display = `${variable.type.name} ${variable.name} <- ${variable.value};`;
  153. return {resume: resume, display: display};
  154. }
  155. // ***********************************************************************
  156. // Update all variable select
  157. // **********************************************************************
  158. function updateAllVariableSelect() {
  159. const variableSelects = document.getElementsByName("variableSelect");
  160. let variablesSelect = variables.length === 0 ? `<option>N/A</option>` : ``;
  161. for (let i = 0; i < variables.length; i++) {
  162. variablesSelect += `<option value="${variables[i].hash}">${variables[i].name}</option>`;
  163. }
  164. for (let i = 0; i < variableSelects.length; i++) {
  165. const selectedOption = variableSelects[i].value;
  166. variableSelects[i].innerHTML = variablesSelect;
  167. variableSelects[i].value = selectedOption;
  168. }
  169. }
  170. // *********************************************************************************
  171. // Util
  172. // *********************************************************************************
  173. export function getVariableByHash(hash) {
  174. for (let i = 0; i < variables.length; i++) {
  175. if (variables[i].hash === hash) return variables[i];
  176. }
  177. return null;
  178. }
  179. // *********************************************************************************