operations.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Imports
  2. import {generateUUID, htmlOlCommandsOperations} from './../../main';
  3. import {
  4. getOperationTypeByValue, getOperatorByHash,
  5. getOperatorTypeByValue,
  6. htmlAssignVariableScheme,
  7. htmlOperationKindScheme, htmlOperationTypeSelect, htmlOperatorValueInputScheme,
  8. htmlOperatorVariablesSelectScheme,
  9. operationScheme, operatorScheme, operatorTypes
  10. } from "./operations-schemes";
  11. import {getVariableByHash, variables} from "../variables/variables";
  12. // Imports
  13. export const operations = [];
  14. // Creating operation
  15. export function createOperation() {
  16. let operation = Object.assign({}, operationScheme);
  17. operation.hash = generateUUID().replaceAll('-', '');
  18. // TODO: Fix
  19. operation.assignedVariable = variables[0];
  20. operation.operators = [];
  21. operations.push(operation);
  22. let htmlOperation = htmlAssignVariableScheme();
  23. htmlOperation = htmlOperation.replaceAll('<operationId>', operation.hash);
  24. htmlOlCommandsOperations.insertAdjacentHTML('beforeend', htmlOperation);
  25. document.getElementById(`operation${operation.hash}AssignVariableSelect`).addEventListener('change', ev => {
  26. updateOperationAssignValue(ev.target.getAttribute('operation-id'), ev.target.value);
  27. });
  28. addOperatorKind(operation.hash);
  29. }
  30. // Add operator kind to the operation
  31. export function addOperatorKind(hash) {
  32. let operation = getOperationByHash(hash);
  33. console.log(operation);
  34. let htmlOperationKind = htmlOperationKindScheme(operation);
  35. document.getElementById(`operation${operation.hash}OperatorsDiv`).insertAdjacentHTML('beforeend', htmlOperationKind);
  36. document.getElementById(`operation${operation.hash}KindSelect`).addEventListener('change', ev => {
  37. selectedOperationKind(ev.target.getAttribute('operation-id'));
  38. });
  39. }
  40. // Function to be trigger when the operator kind is selected
  41. export function selectedOperationKind(operationHash) {
  42. let operation = getOperationByHash(operationHash);
  43. console.log(operation);
  44. let kindSelect = document.getElementById(`operation${operation.hash}KindSelect`);
  45. // TODO: think into an way to make this dynamic
  46. if (kindSelect.value === 'VARIABLE') {
  47. insertVariableAfterOperationKind(kindSelect, operation);
  48. } else if (kindSelect.value === 'VALUE') {
  49. insertValueAfterOperationKind(kindSelect, operation);
  50. }
  51. }
  52. // Insert variable after operator kind select
  53. export function insertVariableAfterOperationKind(kindSelect, operation) {
  54. let operator = Object.assign({}, operatorScheme);
  55. operator.hash = generateUUID().replaceAll('-', '');
  56. // TODO: Fix ?
  57. operator.variable = variables[0];
  58. operator.type = getOperatorTypeByValue('VARIABLE');
  59. let operatorVariableSelect = htmlOperatorVariablesSelectScheme(operation, operator);
  60. operation.operators.push(operator);
  61. kindSelect.insertAdjacentHTML('afterend', operatorVariableSelect);
  62. kindSelect.remove();
  63. insertOperationTypeAtEndOfOperation(operation, operator);
  64. document.getElementById(`operation${operation.hash}Operator${operator.hash}VariableSelect`).addEventListener('change', ev => {
  65. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  66. });
  67. }
  68. // Insert value after operator kind select
  69. export function insertValueAfterOperationKind(kindSelect, operation) {
  70. let operator = Object.assign({}, operatorScheme);
  71. operator.hash = generateUUID().replaceAll('-', '');
  72. operator.type = getOperatorTypeByValue('VALUE');
  73. let operatorValueInput = htmlOperatorValueInputScheme(operation, operator);
  74. operation.operators.push(operator);
  75. kindSelect.insertAdjacentHTML('afterend', operatorValueInput);
  76. kindSelect.remove();
  77. insertOperationTypeAtEndOfOperation(operation, operator);
  78. document.getElementById(`operation${operation.hash}Operator${operator.hash}ValueInput`).addEventListener('change', ev => {
  79. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  80. });
  81. }
  82. // Add final operation typt end of the operation
  83. export function insertOperationTypeAtEndOfOperation(operation, afterOperator) {
  84. let operator = Object.assign({}, operatorScheme);
  85. operator.hash = generateUUID().replaceAll('-', '');
  86. operator.type = getOperatorTypeByValue('OPERATOR');
  87. operator.operator = getOperationTypeByValue('SEMICOLON');
  88. let operationTypeInput = htmlOperationTypeSelect(operation, operator);
  89. operationTypeInput.replaceAll('<operatorId>', operator.hash);
  90. operation.operators.push(operator);
  91. if (afterOperator.type.value === 'VARIABLE') {
  92. document.querySelector(`select[operator-id='${afterOperator.hash}']`).insertAdjacentHTML('afterend', operationTypeInput);
  93. } else if (afterOperator.type.value === 'VALUE') {
  94. document.querySelector(`input[operator-id='${afterOperator.hash}']`).insertAdjacentHTML('afterend', operationTypeInput);
  95. }
  96. document.getElementById(`operation${operation.hash}Operator${operator.hash}OperationTypeSelect`).addEventListener('change', ev => {
  97. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  98. });
  99. }
  100. // ***********************************************************************
  101. // Listeners
  102. // ***********************************************************************
  103. export function updateOperationAssignValue(operationHash, newVariable) {
  104. let operation = getOperationByHash(operationHash);
  105. let variable = getVariableByHash(newVariable);
  106. operation.assignedVariable = variable;
  107. }
  108. export function updateOperationOperator(operationHash, operatorHash, newValue) {
  109. let operation = getOperationByHash(operationHash);
  110. let operator = getOperatorByHash(operation, operatorHash);
  111. switch (operator.type.value) {
  112. case 'VARIABLE':
  113. let variable = getVariableByHash(newValue);
  114. operator.variable = variable;
  115. break;
  116. case 'VALUE':
  117. operator.value = newValue;
  118. break;
  119. case 'OPERATOR':
  120. operator.operator = getOperationTypeByValue(newValue);
  121. if (operator.operator.name !== ';')
  122. addOperatorKind(operation.hash);
  123. break;
  124. }
  125. }
  126. // ***********************************************************************
  127. // Util
  128. // ***********************************************************************
  129. export function getOperationByHash(hash) {
  130. for (let i = 0; i < operations.length; i++) {
  131. if (operations[i].hash === hash)
  132. return operations[i];
  133. }
  134. return null;
  135. }
  136. // ***********************************************************************