variables.js 3.5 KB

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