operations.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. const operationTypeSelect = document.getElementById(`operation${operation.hash}Operator${operator.hash}OperationTypeSelect`);
  131. // If the operator isn't a semicolumn, it is the last item on the operator list and teh next elemtn is equal to null,
  132. // we need to add a new operator select
  133. if (operator.operator.name !== ';'
  134. && (operation.operators.indexOf(operator) === (operation.operators.length - 1))
  135. && operationTypeSelect.parentElement.nextElementSibling == null)
  136. addOperatorKind(operation.hash);
  137. else if (operator.operator.name === ';'
  138. && (operation.operators.indexOf(operator) === (operation.operators.length - 1))
  139. && operationTypeSelect.parentElement.nextElementSibling != null)
  140. operationTypeSelect.parentElement.nextElementSibling.remove();
  141. else if (operator.operator.name === ';'
  142. && (operation.operators.indexOf(operator) !== (operation.operators.length - 1))
  143. && operationTypeSelect.parentElement.nextElementSibling != null) {
  144. let indexOfOperator = operation.operators.indexOf(operator);
  145. operation.operators.splice(indexOfOperator);
  146. let elementSibling = operationTypeSelect.parentElement.nextElementSibling;
  147. let nextElementSibling = operationTypeSelect.parentElement.nextElementSibling;
  148. while(nextElementSibling != null) {
  149. nextElementSibling = elementSibling.nextElementSibling;
  150. elementSibling.remove();
  151. elementSibling = nextElementSibling;
  152. }
  153. }
  154. break;
  155. }
  156. }
  157. // Here we remove all operation that include the variable received as parameter
  158. export function deleteOperationByVariable (variable) {
  159. const operationsToRemove = [];
  160. for (const i in operations) {
  161. console.log(operations[i]);
  162. if (operations[i].assignedVariable.hash === variable.hash) {
  163. document.getElementById(`operation${operations[i].hash}Li`).remove();
  164. operationsToRemove.push(operations[i]);
  165. continue;
  166. }
  167. for (const j in operations[i].operators) {
  168. console.log(j);
  169. console.log(operations[i].operators[j]);
  170. if (operations[i].operators[j].type.value === Operators.VARIABLE) {
  171. if (operations[i].operators[j].variable.hash === variable.hash) {
  172. document.getElementById(`operation${operations[i].hash}Li`).remove();
  173. operationsToRemove.push(operations[i]);
  174. // if (operation.operators.length > 2) {
  175. //
  176. // } else {
  177. //
  178. // }
  179. }
  180. }
  181. }
  182. }
  183. for (const i in operationsToRemove) {
  184. operations.splice(operations.indexOf(operationsToRemove[i]), 1);
  185. }
  186. }
  187. // Here we remove all operation that include the variable received as parameter
  188. export function deleteOperation (hash) {
  189. const operation = getOperationByHash(hash);
  190. document.getElementById(`operation${hash}Li`).remove();
  191. operations.splice(operations.indexOf(operation), 1);
  192. }
  193. // Here we remove all operation
  194. export function deleteAllOperation () {
  195. for (let i = 0; i < operations.length; i++) {
  196. document.getElementById(`operation${operations[i].hash}Li`).remove();
  197. }
  198. operations.length = 0;
  199. }
  200. // ***********************************************************************
  201. // Util
  202. // ***********************************************************************
  203. export function getOperationByHash (hash) {
  204. for (let i = 0; i < operations.length; i++) {
  205. if (operations[i].hash === hash)
  206. return operations[i];
  207. }
  208. return null;
  209. }
  210. // ***********************************************************************