operations.js 8.7 KB

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