operations.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, Operators, 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. document.getElementById(`operation${operation.hash}Delete`).addEventListener('click', ev => {
  29. deleteOperation(ev.target.getAttribute('operation-id'));
  30. });
  31. addOperatorKind(operation.hash);
  32. }
  33. // Add operator kind to the operation
  34. export function addOperatorKind (hash) {
  35. let operation = getOperationByHash(hash);
  36. console.log(operation);
  37. let htmlOperationKind = htmlOperationKindScheme(operation);
  38. document.getElementById(`operation${operation.hash}OperatorsDiv`).insertAdjacentHTML('beforeend', htmlOperationKind);
  39. document.getElementById(`operation${operation.hash}KindSelect`).addEventListener('change', ev => {
  40. selectedOperationKind(ev.target.getAttribute('operation-id'));
  41. });
  42. }
  43. // Function to be trigger when the operator kind is selected
  44. export function selectedOperationKind (operationHash) {
  45. let operation = getOperationByHash(operationHash);
  46. console.log(operation);
  47. let kindSelect = document.getElementById(`operation${operation.hash}KindSelect`);
  48. // TODO: think into an way to make this dynamic
  49. if (kindSelect.value === 'VARIABLE') {
  50. insertVariableAfterOperationKind(kindSelect, operation);
  51. } else if (kindSelect.value === 'VALUE') {
  52. insertValueAfterOperationKind(kindSelect, operation);
  53. }
  54. }
  55. // Insert variable after operator kind select
  56. export function insertVariableAfterOperationKind (kindSelect, operation) {
  57. let operator = Object.assign({}, operatorScheme);
  58. operator.hash = generateUUID().replaceAll('-', '');
  59. // TODO: Fix ?
  60. operator.variable = variables[0];
  61. operator.type = getOperatorTypeByValue('VARIABLE');
  62. let operatorVariableSelect = htmlOperatorVariablesSelectScheme(operation, operator);
  63. operation.operators.push(operator);
  64. kindSelect.parentElement.parentElement.insertAdjacentHTML('beforeend', operatorVariableSelect);
  65. kindSelect.parentElement.remove();
  66. insertOperationTypeAtEndOfOperation(operation, operator);
  67. document.getElementById(`operation${operation.hash}Operator${operator.hash}VariableSelect`).addEventListener('change', ev => {
  68. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  69. });
  70. }
  71. // Insert value after operator kind select
  72. export function insertValueAfterOperationKind (kindSelect, operation) {
  73. let operator = Object.assign({}, operatorScheme);
  74. operator.hash = generateUUID().replaceAll('-', '');
  75. operator.type = getOperatorTypeByValue('VALUE');
  76. let operatorValueInput = htmlOperatorValueInputScheme(operation, operator);
  77. operation.operators.push(operator);
  78. kindSelect.parentElement.parentElement.insertAdjacentHTML('beforeend', operatorValueInput);
  79. kindSelect.parentElement.remove();
  80. insertOperationTypeAtEndOfOperation(operation, operator);
  81. document.getElementById(`operation${operation.hash}Operator${operator.hash}ValueInput`).addEventListener('change', ev => {
  82. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  83. });
  84. }
  85. // Add final operation typt end of the operation
  86. export function insertOperationTypeAtEndOfOperation (operation, afterOperator) {
  87. let operator = Object.assign({}, operatorScheme);
  88. operator.hash = generateUUID().replaceAll('-', '');
  89. operator.type = getOperatorTypeByValue('OPERATOR');
  90. operator.operator = getOperationTypeByValue('SEMICOLON');
  91. let operationTypeInput = htmlOperationTypeSelect(operation, operator);
  92. operationTypeInput.replaceAll('<operatorId>', operator.hash);
  93. operation.operators.push(operator);
  94. if (afterOperator.type.value === 'VARIABLE') {
  95. document.querySelector(`select[operator-id='${afterOperator.hash}']`).parentElement.insertAdjacentHTML('afterend', operationTypeInput);
  96. } else if (afterOperator.type.value === 'VALUE') {
  97. document.querySelector(`input[operator-id='${afterOperator.hash}']`).parentElement.insertAdjacentHTML('afterend', operationTypeInput);
  98. }
  99. document.getElementById(`operation${operation.hash}Operator${operator.hash}OperationTypeSelect`).addEventListener('change', ev => {
  100. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  101. });
  102. }
  103. // ***********************************************************************
  104. // Listeners
  105. // ***********************************************************************
  106. export function updateOperationAssignValue (operationHash, newVariable) {
  107. let operation = getOperationByHash(operationHash);
  108. let variable = getVariableByHash(newVariable);
  109. operation.assignedVariable = variable;
  110. }
  111. export function updateOperationOperator (operationHash, operatorHash, newValue) {
  112. let operation = getOperationByHash(operationHash);
  113. let operator = getOperatorByHash(operation, operatorHash);
  114. switch (operator.type.value) {
  115. case 'VARIABLE':
  116. let variable = getVariableByHash(newValue);
  117. operator.variable = variable;
  118. break;
  119. case 'VALUE':
  120. operator.value = newValue;
  121. break;
  122. case 'OPERATOR':
  123. operator.operator = getOperationTypeByValue(newValue);
  124. if (operator.operator.name !== ';' && (operation.operators.indexOf(operator) === (operation.operators.length - 1)))
  125. addOperatorKind(operation.hash);
  126. break;
  127. }
  128. }
  129. // Here we remove all operation that include the variable received as parameter
  130. export function deleteOperationByVariable (variable) {
  131. const operationsToRemove = [];
  132. for (const i in operations) {
  133. console.log(operations[i]);
  134. if (operations[i].assignedVariable.hash === variable.hash) {
  135. document.getElementById(`operation${operations[i].hash}Li`).remove();
  136. operationsToRemove.push(operations[i]);
  137. continue;
  138. }
  139. for (const j in operations[i].operators) {
  140. console.log(j);
  141. console.log(operations[i].operators[j]);
  142. if (operations[i].operators[j].type.value === Operators.VARIABLE) {
  143. if (operations[i].operators[j].variable.hash === variable.hash) {
  144. document.getElementById(`operation${operations[i].hash}Li`).remove();
  145. operationsToRemove.push(operations[i]);
  146. // if (operation.operators.length > 2) {
  147. //
  148. // } else {
  149. //
  150. // }
  151. }
  152. }
  153. }
  154. }
  155. for (const i in operationsToRemove) {
  156. operations.splice(operations.indexOf(operationsToRemove[i]), 1);
  157. }
  158. }
  159. // Here we remove all operation that include the variable received as parameter
  160. export function deleteOperation (hash) {
  161. const operation = getOperationByHash(hash);
  162. document.getElementById(`operation${hash}Li`).remove();
  163. operations.splice(operations.indexOf(operation), 1);
  164. }
  165. // ***********************************************************************
  166. // Util
  167. // ***********************************************************************
  168. export function getOperationByHash (hash) {
  169. for (let i = 0; i < operations.length; i++) {
  170. if (operations[i].hash === hash)
  171. return operations[i];
  172. }
  173. return null;
  174. }
  175. // ***********************************************************************