variables.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.getElementById(`variable${variable.hash}Type`).addEventListener('change', ev => {
  27. const variableHash = ev.target.getAttribute('variable-id');
  28. updateVariable(variableHash);
  29. });
  30. document.getElementById(`variable${variable.hash}Name`).addEventListener('change', ev => {
  31. const variableHash = ev.target.getAttribute('variable-id');
  32. updateVariable(variableHash);
  33. });
  34. document.getElementById(`variable${variable.hash}Value`).addEventListener('change', ev => {
  35. const variableHash = ev.target.getAttribute('variable-id');
  36. updateVariable(variableHash);
  37. });
  38. document.getElementById(`variable${variable.hash}Delete`).addEventListener('click', ev => {
  39. const variableHash = ev.target.getAttribute('variable-id');
  40. deleteVariable(variableHash);
  41. });
  42. updateResume(variable.hash);
  43. updateAllVariableSelect();
  44. // Changing focus to the variable type after creation for screen readers
  45. document.getElementById(`variable${variable.hash}Type`).focus();
  46. }
  47. // *********************************************************************************
  48. // Updating variable
  49. // *********************************************************************************
  50. let variableName;
  51. let variableType;
  52. let variableValue;
  53. function updateVariable (hash) {
  54. const variable = getVariableByHash(hash);
  55. variableName = document.getElementById(`variable${hash}Name`).value;
  56. if (variable.name !== variableName) {
  57. variable.name = variableName;
  58. }
  59. variableType = getVariableTypeByValue(document.getElementById(`variable${hash}Type`).value);
  60. if (variable.type !== variableType) {
  61. variable.type = variableType;
  62. document.getElementById(`variable${hash}Value`).type = variable.type.htmlType;
  63. document.getElementById(`variable${hash}Value`).value = variable.type.defaultValue;
  64. }
  65. variableValue = document.getElementById(`variable${hash}Value`).value;
  66. if (variable.value !== variableValue) {
  67. variable.value = variableValue;
  68. }
  69. updateResume(hash);
  70. }
  71. // *********************************************************************************
  72. // Deleting variable
  73. export function deleteVariable (hash) {
  74. const variable = getVariableByHash(hash);
  75. document.getElementById(`variable${hash}Li`).remove();
  76. deleteOperationByVariable(variable);
  77. variables.splice(variables.indexOf(variable), 1);
  78. updateAllVariableSelect();
  79. }
  80. // Deleting all variable
  81. export function deleteAllVariable () {
  82. for (let i = 0; i < variables.length; i++) {
  83. document.getElementById(`variable${variables[i].hash}Li`).remove();
  84. }
  85. variables.length = 0
  86. }
  87. // ***********************************************************************
  88. // Updating variable resume
  89. // ***********************************************************************
  90. function updateResume (hash) {
  91. document.getElementById(`variable${hash}Resume`).setAttribute('title', generateResume(hash));
  92. }
  93. function generateResume (hash) {
  94. const variable = getVariableByHash(hash);
  95. return `${variable.type.name} ${variable.name} recebe ${variable.value}`;
  96. }
  97. // ***********************************************************************
  98. // Update all variable select
  99. // **********************************************************************
  100. function updateAllVariableSelect () {
  101. const variableSelects = document.getElementsByName('variableSelect');
  102. let variablesSelect = variables.length === 0 ? `<option>N/A</option>` : ``;
  103. for (let i = 0; i < variables.length; i++) {
  104. variablesSelect += `<option value="${variables[i].hash}">${variables[i].name}</option>`
  105. }
  106. for (let i = 0; i < variableSelects.length; i++) {
  107. const selectedOption = variableSelects[i].value;
  108. variableSelects[i].innerHTML = variablesSelect;
  109. variableSelects[i].value = selectedOption;
  110. }
  111. }
  112. // *********************************************************************************
  113. // Util
  114. // *********************************************************************************
  115. export function getVariableByHash (hash) {
  116. for (let i = 0; i < variables.length; i++) {
  117. if (variables[i].hash === hash)
  118. return variables[i];
  119. }
  120. return null;
  121. }
  122. // *********************************************************************************