operations.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // Changing focus to the variable select after select for screen readers
  73. document.getElementById(`operation${operation.hash}Operator${operator.hash}VariableSelect`).focus();
  74. }
  75. // Insert value after operator kind select
  76. export function insertValueAfterOperationKind (kindSelect, operation) {
  77. let operator = Object.assign({}, operatorScheme);
  78. operator.hash = generateUUID().replaceAll('-', '');
  79. operator.type = getOperatorTypeByValue('VALUE');
  80. let operatorValueInput = htmlOperatorValueInputScheme(operation, operator);
  81. operation.operators.push(operator);
  82. kindSelect.parentElement.parentElement.insertAdjacentHTML('beforeend', operatorValueInput);
  83. kindSelect.parentElement.remove();
  84. insertOperationTypeAtEndOfOperation(operation, operator);
  85. document.getElementById(`operation${operation.hash}Operator${operator.hash}ValueInput`).addEventListener('change', ev => {
  86. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  87. });
  88. // Changing focus to the input value after select for screen readers
  89. document.getElementById(`operation${operation.hash}Operator${operator.hash}ValueInput`).focus();
  90. }
  91. // Add final operation typt end of the operation
  92. export function insertOperationTypeAtEndOfOperation (operation, afterOperator) {
  93. let operator = Object.assign({}, operatorScheme);
  94. operator.hash = generateUUID().replaceAll('-', '');
  95. operator.type = getOperatorTypeByValue('OPERATOR');
  96. operator.operator = getOperationTypeByValue('SEMICOLON');
  97. let operationTypeInput = htmlOperationTypeSelect(operation, operator);
  98. operationTypeInput.replaceAll('<operatorId>', operator.hash);
  99. operation.operators.push(operator);
  100. if (afterOperator.type.value === 'VARIABLE') {
  101. document.querySelector(`select[operator-id='${afterOperator.hash}']`).parentElement.insertAdjacentHTML('afterend', operationTypeInput);
  102. } else if (afterOperator.type.value === 'VALUE') {
  103. document.querySelector(`input[operator-id='${afterOperator.hash}']`).parentElement.insertAdjacentHTML('afterend', operationTypeInput);
  104. }
  105. document.getElementById(`operation${operation.hash}Operator${operator.hash}OperationTypeSelect`).addEventListener('change', ev => {
  106. updateOperationOperator(ev.target.getAttribute('operation-id'), ev.target.getAttribute('operator-id'), ev.target.value)
  107. });
  108. }
  109. // ***********************************************************************
  110. // Listeners
  111. // ***********************************************************************
  112. export function updateOperationAssignValue (operationHash, newVariable) {
  113. let operation = getOperationByHash(operationHash);
  114. let variable = getVariableByHash(newVariable);
  115. operation.assignedVariable = variable;
  116. }
  117. export function updateOperationOperator (operationHash, operatorHash, newValue) {
  118. let operation = getOperationByHash(operationHash);
  119. let operator = getOperatorByHash(operation, operatorHash);
  120. switch (operator.type.value) {
  121. case 'VARIABLE':
  122. let variable = getVariableByHash(newValue);
  123. operator.variable = variable;
  124. break;
  125. case 'VALUE':
  126. operator.value = newValue;
  127. break;
  128. case 'OPERATOR':
  129. operator.operator = getOperationTypeByValue(newValue);
  130. if (operator.operator.name !== ';' && (operation.operators.indexOf(operator) === (operation.operators.length - 1)))
  131. addOperatorKind(operation.hash);
  132. break;
  133. }
  134. }
  135. // Here we remove all operation that include the variable received as parameter
  136. export function deleteOperationByVariable (variable) {
  137. const operationsToRemove = [];
  138. for (const i in operations) {
  139. console.log(operations[i]);
  140. if (operations[i].assignedVariable.hash === variable.hash) {
  141. document.getElementById(`operation${operations[i].hash}Li`).remove();
  142. operationsToRemove.push(operations[i]);
  143. continue;
  144. }
  145. for (const j in operations[i].operators) {
  146. console.log(j);
  147. console.log(operations[i].operators[j]);
  148. if (operations[i].operators[j].type.value === Operators.VARIABLE) {
  149. if (operations[i].operators[j].variable.hash === variable.hash) {
  150. document.getElementById(`operation${operations[i].hash}Li`).remove();
  151. operationsToRemove.push(operations[i]);
  152. // if (operation.operators.length > 2) {
  153. //
  154. // } else {
  155. //
  156. // }
  157. }
  158. }
  159. }
  160. }
  161. for (const i in operationsToRemove) {
  162. operations.splice(operations.indexOf(operationsToRemove[i]), 1);
  163. }
  164. }
  165. // Here we remove all operation that include the variable received as parameter
  166. export function deleteOperation (hash) {
  167. const operation = getOperationByHash(hash);
  168. document.getElementById(`operation${hash}Li`).remove();
  169. operations.splice(operations.indexOf(operation), 1);
  170. }
  171. // Here we remove all operation
  172. export function deleteAllOperation () {
  173. for (let i = 0; i < operations.length; i++) {
  174. document.getElementById(`operation${operations[i].hash}Li`).remove();
  175. }
  176. operations.length = 0;
  177. }
  178. // ***********************************************************************
  179. // Util
  180. // ***********************************************************************
  181. export function getOperationByHash (hash) {
  182. for (let i = 0; i < operations.length; i++) {
  183. if (operations[i].hash === hash)
  184. return operations[i];
  185. }
  186. return null;
  187. }
  188. // ***********************************************************************