variables.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Imports
  2. import {htmlOlCommandsVariables, generateUUID} from './../../main';
  3. import {getVariableTypeByValue, htmlVariableScheme, variableScheme, variableTypes} from "./variables-schemes";
  4. // Imports
  5. export const variables = [];
  6. let variablesIndex = 0;
  7. // Creating variable
  8. export function createVariable () {
  9. const variable = Object.assign({}, variableScheme);
  10. variable.hash = generateUUID().replaceAll('-', '');
  11. variable.name = 'var' + variablesIndex;
  12. variable.type = variableTypes[0];
  13. variable.value = variable.type.defaultValue;
  14. variables.push(variable);
  15. variablesIndex++;
  16. let htmlVariable = htmlVariableScheme();
  17. htmlVariable = htmlVariable.replaceAll('<variableId>', variable.hash);
  18. htmlVariable = htmlVariable.replaceAll('<variableName>', variable.name);
  19. htmlOlCommandsVariables.insertAdjacentHTML('beforeend', htmlVariable);
  20. document.getElementById(`variable${variable.hash}Type`).addEventListener('change', ev => {
  21. let variableHash = ev.target.getAttribute('variable-id');
  22. updateVariable(variableHash);
  23. });
  24. document.getElementById(`variable${variable.hash}Name`).addEventListener('change', ev => {
  25. let variableHash = ev.target.getAttribute('variable-id');
  26. updateVariable(variableHash);
  27. });
  28. document.getElementById(`variable${variable.hash}Value`).addEventListener('change', ev => {
  29. let variableHash = ev.target.getAttribute('variable-id');
  30. updateVariable(variableHash);
  31. });
  32. }
  33. // *********************************************************************************
  34. // Updating variable
  35. // *********************************************************************************
  36. let variableName;
  37. let variableType;
  38. let variableValue;
  39. function updateVariable (hash) {
  40. let variable = getVariableByHash(hash);
  41. variableName = document.getElementById(`variable${hash}Name`).value;
  42. if (variable.name !== variableName) {
  43. variable.name = variableName;
  44. }
  45. variableType = getVariableTypeByValue(document.getElementById(`variable${hash}Type`).value);
  46. if (variable.type !== variableType) {
  47. variable.type = variableType;
  48. document.getElementById(`variable${hash}Value`).type = variable.type.htmlType;
  49. document.getElementById(`variable${hash}Value`).value = variable.type.defaultValue;
  50. }
  51. variableValue = document.getElementById(`variable${hash}Value`).value;
  52. if (variable.value !== variableValue) {
  53. variable.value = variableValue;
  54. }
  55. }
  56. // *********************************************************************************
  57. // Deleting variable
  58. export function deleteVariable (hash) {
  59. let variable = getVariableByHash(hash);
  60. document.getElementById(`variable${hash}Li`).remove();
  61. variables.splice(variables.indexOf(variable), 1);
  62. }
  63. // *********************************************************************************
  64. // Util
  65. // *********************************************************************************
  66. export function getVariableByHash (hash) {
  67. for (let i = 0; i < variables.length; i++) {
  68. if (variables[i].hash === hash)
  69. return variables[i];
  70. }
  71. return null;
  72. }
  73. // *********************************************************************************