variables.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Imports
  2. import {htmlOlCommandsVariables, generateUUID} from './../../main';
  3. import {getVariableTypeByValue, htmlVariableScheme, variableScheme, variableTypes} from "./variables-schemes";
  4. import {deleteOperationByVariable} from "../operations/operations";
  5. // Imports
  6. export const variables = [];
  7. let variablesIndex = 0;
  8. // Creating variable
  9. export function createVariable () {
  10. const variable = Object.assign({}, variableScheme);
  11. variable.hash = generateUUID().replaceAll('-', '');
  12. variable.name = 'var' + variablesIndex;
  13. variable.type = variableTypes[0];
  14. variable.value = variable.type.defaultValue;
  15. variables.push(variable);
  16. variablesIndex++;
  17. let htmlVariable = htmlVariableScheme();
  18. htmlVariable = htmlVariable.replaceAll('<variableId>', variable.hash);
  19. htmlVariable = htmlVariable.replaceAll('<variableName>', variable.name);
  20. htmlOlCommandsVariables.insertAdjacentHTML('beforeend', htmlVariable);
  21. document.getElementById(`variable${variable.hash}Type`).addEventListener('change', ev => {
  22. const variableHash = ev.target.getAttribute('variable-id');
  23. updateVariable(variableHash);
  24. });
  25. document.getElementById(`variable${variable.hash}Name`).addEventListener('change', ev => {
  26. const variableHash = ev.target.getAttribute('variable-id');
  27. updateVariable(variableHash);
  28. });
  29. document.getElementById(`variable${variable.hash}Value`).addEventListener('change', ev => {
  30. const variableHash = ev.target.getAttribute('variable-id');
  31. updateVariable(variableHash);
  32. });
  33. document.getElementById(`variable${variable.hash}Delete`).addEventListener('click', ev => {
  34. const variableHash = ev.target.getAttribute('variable-id');
  35. deleteVariable(variableHash);
  36. });
  37. updateResume(variable.hash);
  38. // Changing focus to the variable type after creation for screen readers
  39. document.getElementById(`variable${variable.hash}Type`).focus();
  40. }
  41. // *********************************************************************************
  42. // Updating variable
  43. // *********************************************************************************
  44. let variableName;
  45. let variableType;
  46. let variableValue;
  47. function updateVariable (hash) {
  48. const variable = getVariableByHash(hash);
  49. variableName = document.getElementById(`variable${hash}Name`).value;
  50. if (variable.name !== variableName) {
  51. variable.name = variableName;
  52. }
  53. variableType = getVariableTypeByValue(document.getElementById(`variable${hash}Type`).value);
  54. if (variable.type !== variableType) {
  55. variable.type = variableType;
  56. document.getElementById(`variable${hash}Value`).type = variable.type.htmlType;
  57. document.getElementById(`variable${hash}Value`).value = variable.type.defaultValue;
  58. }
  59. variableValue = document.getElementById(`variable${hash}Value`).value;
  60. if (variable.value !== variableValue) {
  61. variable.value = variableValue;
  62. }
  63. updateResume(hash);
  64. }
  65. // *********************************************************************************
  66. // Deleting variable
  67. export function deleteVariable (hash) {
  68. const variable = getVariableByHash(hash);
  69. document.getElementById(`variable${hash}Li`).remove();
  70. deleteOperationByVariable(variable);
  71. variables.splice(variables.indexOf(variable), 1);
  72. }
  73. // Deleting all variable
  74. export function deleteAllVariable () {
  75. for (let i = 0; i < variables.length; i++) {
  76. document.getElementById(`variable${variables[i].hash}Li`).remove();
  77. }
  78. variables.length = 0
  79. }
  80. // ***********************************************************************
  81. // Updating variable resume
  82. // ***********************************************************************
  83. function updateResume (hash) {
  84. document.getElementById(`variable${hash}Resume`).setAttribute('title', generateResume(hash));
  85. }
  86. function generateResume (hash) {
  87. const variable = getVariableByHash(hash);
  88. return `${variable.type.name} ${variable.name} recebe ${variable.value}`;
  89. }
  90. // *********************************************************************************
  91. // Util
  92. // *********************************************************************************
  93. export function getVariableByHash (hash) {
  94. for (let i = 0; i < variables.length; i++) {
  95. if (variables[i].hash === hash)
  96. return variables[i];
  97. }
  98. return null;
  99. }
  100. // *********************************************************************************