variables.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // Changing focus to the variable type after creation for screen readers
  38. document.getElementById(`variable${variable.hash}Type`).focus();
  39. }
  40. // *********************************************************************************
  41. // Updating variable
  42. // *********************************************************************************
  43. let variableName;
  44. let variableType;
  45. let variableValue;
  46. function updateVariable (hash) {
  47. const variable = getVariableByHash(hash);
  48. variableName = document.getElementById(`variable${hash}Name`).value;
  49. if (variable.name !== variableName) {
  50. variable.name = variableName;
  51. }
  52. variableType = getVariableTypeByValue(document.getElementById(`variable${hash}Type`).value);
  53. if (variable.type !== variableType) {
  54. variable.type = variableType;
  55. document.getElementById(`variable${hash}Value`).type = variable.type.htmlType;
  56. document.getElementById(`variable${hash}Value`).value = variable.type.defaultValue;
  57. }
  58. variableValue = document.getElementById(`variable${hash}Value`).value;
  59. if (variable.value !== variableValue) {
  60. variable.value = variableValue;
  61. }
  62. }
  63. // *********************************************************************************
  64. // Deleting variable
  65. export function deleteVariable (hash) {
  66. const variable = getVariableByHash(hash);
  67. document.getElementById(`variable${hash}Li`).remove();
  68. deleteOperationByVariable(variable);
  69. variables.splice(variables.indexOf(variable), 1);
  70. }
  71. // Deleting all variable
  72. export function deleteAllVariable () {
  73. for (let i = 0; i < variables.length; i++) {
  74. document.getElementById(`variable${variables[i].hash}Li`).remove();
  75. }
  76. variables.length = 0
  77. }
  78. // *********************************************************************************
  79. // Util
  80. // *********************************************************************************
  81. export function getVariableByHash (hash) {
  82. for (let i = 0; i < variables.length; i++) {
  83. if (variables[i].hash === hash)
  84. return variables[i];
  85. }
  86. return null;
  87. }
  88. // *********************************************************************************