operations.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // Imports
  2. import { generateUUID, htmlOlCommandsOperations } from "./../../main";
  3. import {
  4. getOperationTypeByValue,
  5. getOperatorByHash,
  6. getOperatorTypeByValue,
  7. htmlAssignVariableScheme,
  8. htmlOperationKindScheme,
  9. htmlOperationTypeSelect,
  10. htmlOperatorValueInputScheme,
  11. htmlOperatorVariablesSelectScheme,
  12. operationScheme,
  13. Operators,
  14. operatorScheme,
  15. printOperationScheme,
  16. } from "./operations-schemes";
  17. import { getVariableByHash, variables } from "../variables/variables";
  18. // Imports
  19. export const operations = [];
  20. // Creating operation
  21. export function createOperation() {
  22. let operation = Object.assign({}, operationScheme);
  23. operation.hash = generateUUID().replaceAll("-", "");
  24. // TODO: Fix
  25. operation.assignedVariable = variables[0];
  26. operation.operators = [];
  27. operations.push(operation);
  28. let htmlOperation = htmlAssignVariableScheme();
  29. htmlOperation = htmlOperation.replaceAll("<operationId>", operation.hash);
  30. htmlOlCommandsOperations.insertAdjacentHTML("beforeend", htmlOperation);
  31. document
  32. .getElementById(`operation${operation.hash}AssignVariableSelect`)
  33. .addEventListener("change", (ev) => {
  34. updateOperationAssignValue(
  35. ev.target.getAttribute("operation-id"),
  36. ev.target.value
  37. );
  38. });
  39. document
  40. .getElementById(`operation${operation.hash}Delete`)
  41. .addEventListener("click", (ev) => {
  42. deleteOperation(ev.target.getAttribute("operation-id"));
  43. });
  44. document
  45. .getElementById(`operation${operation.hash}Resume`)
  46. .addEventListener("focus", (ev) => {
  47. const operationHash = ev.target.getAttribute("operation-id");
  48. manageOperationEditing(operationHash);
  49. });
  50. document
  51. .getElementById(`operation${operation.hash}Display`)
  52. .addEventListener("keyup", (ev) => {
  53. const operationHash = ev.target.getAttribute("operation-id");
  54. if (ev.key === "Enter") manageOperationEditing(operationHash, true);
  55. });
  56. addOperatorKind(operation.hash);
  57. updateResume(operation.hash);
  58. // Changing focus to the variable type after creation for screen readers
  59. document
  60. .getElementById(`operation${operation.hash}AssignVariableSelect`)
  61. .focus();
  62. }
  63. // Add operator kind to the operation
  64. export function addOperatorKind(hash) {
  65. let operation = getOperationByHash(hash);
  66. console.log(operation);
  67. let htmlOperationKind = htmlOperationKindScheme(operation);
  68. document
  69. .getElementById(`operation${operation.hash}OperatorsDiv`)
  70. .insertAdjacentHTML("beforeend", htmlOperationKind);
  71. document
  72. .getElementById(`operation${operation.hash}KindSelect`)
  73. .addEventListener("focusout", (ev) => {
  74. selectedOperationKind(ev.target.getAttribute("operation-id"));
  75. ev.target.parentElement.remove();
  76. });
  77. }
  78. // Function to be trigger when the operator kind is selected
  79. export function selectedOperationKind(operationHash) {
  80. let operation = getOperationByHash(operationHash);
  81. console.log(operation);
  82. let kindSelect = document.getElementById(
  83. `operation${operation.hash}KindSelect`
  84. );
  85. // TODO: think into an way to make this dynamic
  86. if (kindSelect.value === "VARIABLE") {
  87. insertVariableAfterOperationKind(kindSelect, operation);
  88. } else if (kindSelect.value === "VALUE") {
  89. insertValueAfterOperationKind(kindSelect, operation);
  90. }
  91. }
  92. // Insert variable after operator kind select
  93. export function insertVariableAfterOperationKind(kindSelect, operation) {
  94. let operator = Object.assign({}, operatorScheme);
  95. operator.hash = generateUUID().replaceAll("-", "");
  96. // TODO: Fix ?
  97. operator.variable = variables[0];
  98. operator.type = getOperatorTypeByValue("VARIABLE");
  99. let operatorVariableSelect = htmlOperatorVariablesSelectScheme(
  100. operation,
  101. operator
  102. );
  103. operation.operators.push(operator);
  104. kindSelect.parentElement.parentElement.insertAdjacentHTML(
  105. "beforeend",
  106. operatorVariableSelect
  107. );
  108. insertOperationTypeAtEndOfOperation(operation, operator);
  109. document
  110. .getElementById(
  111. `operation${operation.hash}Operator${operator.hash}VariableSelect`
  112. )
  113. .addEventListener("change", (ev) => {
  114. updateOperationOperator(
  115. ev.target.getAttribute("operation-id"),
  116. ev.target.getAttribute("operator-id"),
  117. ev.target.value
  118. );
  119. });
  120. updateResume(operation.hash);
  121. // Changing focus to the variable select after select for screen readers
  122. document
  123. .getElementById(
  124. `operation${operation.hash}Operator${operator.hash}VariableSelect`
  125. )
  126. .focus();
  127. }
  128. // Insert value after operator kind select
  129. export function insertValueAfterOperationKind(kindSelect, operation) {
  130. let operator = Object.assign({}, operatorScheme);
  131. operator.hash = generateUUID().replaceAll("-", "");
  132. operator.type = getOperatorTypeByValue("VALUE");
  133. let operatorValueInput = htmlOperatorValueInputScheme(operation, operator);
  134. operation.operators.push(operator);
  135. kindSelect.parentElement.parentElement.insertAdjacentHTML(
  136. "beforeend",
  137. operatorValueInput
  138. );
  139. kindSelect.parentElement.remove();
  140. insertOperationTypeAtEndOfOperation(operation, operator);
  141. document
  142. .getElementById(
  143. `operation${operation.hash}Operator${operator.hash}ValueInput`
  144. )
  145. .addEventListener("change", (ev) => {
  146. updateOperationOperator(
  147. ev.target.getAttribute("operation-id"),
  148. ev.target.getAttribute("operator-id"),
  149. ev.target.value
  150. );
  151. });
  152. updateResume(operation.hash);
  153. // Changing focus to the input value after select for screen readers
  154. document
  155. .getElementById(
  156. `operation${operation.hash}Operator${operator.hash}ValueInput`
  157. )
  158. .focus();
  159. }
  160. // Add final operation type end of the operation
  161. export function insertOperationTypeAtEndOfOperation(operation, afterOperator) {
  162. let operator = Object.assign({}, operatorScheme);
  163. operator.hash = generateUUID().replaceAll("-", "");
  164. operator.type = getOperatorTypeByValue("OPERATOR");
  165. operator.operator = getOperationTypeByValue("SEMICOLON");
  166. let operationTypeInput = htmlOperationTypeSelect(operation, operator);
  167. operationTypeInput.replaceAll("<operatorId>", operator.hash);
  168. operation.operators.push(operator);
  169. if (afterOperator.type.value === "VARIABLE") {
  170. document
  171. .querySelector(`select[operator-id='${afterOperator.hash}']`)
  172. .parentElement.insertAdjacentHTML("afterend", operationTypeInput);
  173. } else if (afterOperator.type.value === "VALUE") {
  174. document
  175. .querySelector(`input[operator-id='${afterOperator.hash}']`)
  176. .parentElement.insertAdjacentHTML("afterend", operationTypeInput);
  177. }
  178. document
  179. .getElementById(
  180. `operation${operation.hash}Operator${operator.hash}OperationTypeSelect`
  181. )
  182. .addEventListener("change", (ev) => {
  183. updateOperationOperator(
  184. ev.target.getAttribute("operation-id"),
  185. ev.target.getAttribute("operator-id"),
  186. ev.target.value
  187. );
  188. });
  189. }
  190. // ***********************************************************************
  191. // Listeners
  192. // ***********************************************************************
  193. export function updateOperationAssignValue(operationHash, newVariable) {
  194. let operation = getOperationByHash(operationHash);
  195. let variable = getVariableByHash(newVariable);
  196. operation.assignedVariable = variable;
  197. updateResume(operation.hash);
  198. }
  199. export function updateOperationOperator(operationHash, operatorHash, newValue) {
  200. let operation = getOperationByHash(operationHash);
  201. let operator = getOperatorByHash(operation, operatorHash);
  202. switch (operator.type.value) {
  203. case "VARIABLE":
  204. let variable = getVariableByHash(newValue);
  205. operator.variable = variable;
  206. break;
  207. case "VALUE":
  208. operator.value = newValue;
  209. break;
  210. case "OPERATOR":
  211. operator.operator = getOperationTypeByValue(newValue);
  212. const operationTypeSelect = document.getElementById(
  213. `operation${operation.hash}Operator${operator.hash}OperationTypeSelect`
  214. );
  215. // If the operator isn't a semicolumn, it is the last item on the operator list and teh next elemtn is equal to null,
  216. // we need to add a new operator select
  217. if (
  218. operator.operator.name !== ";" &&
  219. operation.operators.indexOf(operator) ===
  220. operation.operators.length - 1 &&
  221. operationTypeSelect.parentElement.nextElementSibling == null
  222. )
  223. addOperatorKind(operation.hash);
  224. else if (
  225. operator.operator.name === ";" &&
  226. operation.operators.indexOf(operator) ===
  227. operation.operators.length - 1 &&
  228. operationTypeSelect.parentElement.nextElementSibling != null
  229. )
  230. operationTypeSelect.parentElement.nextElementSibling.remove();
  231. else if (
  232. operator.operator.name === ";" &&
  233. operation.operators.indexOf(operator) !==
  234. operation.operators.length - 1 &&
  235. operationTypeSelect.parentElement.nextElementSibling != null
  236. ) {
  237. let indexOfOperator = operation.operators.indexOf(operator) + 1;
  238. operation.operators.splice(indexOfOperator);
  239. let elementSibling =
  240. operationTypeSelect.parentElement.nextElementSibling;
  241. let nextElementSibling =
  242. operationTypeSelect.parentElement.nextElementSibling;
  243. while (nextElementSibling != null) {
  244. nextElementSibling = elementSibling.nextElementSibling;
  245. elementSibling.remove();
  246. elementSibling = nextElementSibling;
  247. }
  248. }
  249. break;
  250. }
  251. updateResume(operation.hash);
  252. }
  253. // Here we remove all operation that include the variable received as parameter
  254. export function deleteOperationByVariable(variable) {
  255. const operationsToRemove = [];
  256. for (const i in operations) {
  257. console.log(operations[i]);
  258. if (operations[i].assignedVariable.hash === variable.hash) {
  259. document.getElementById(`operation${operations[i].hash}Li`).remove();
  260. operationsToRemove.push(operations[i]);
  261. continue;
  262. }
  263. for (const j in operations[i].operators) {
  264. console.log(j);
  265. console.log(operations[i].operators[j]);
  266. if (operations[i].operators[j].type.value === Operators.VARIABLE) {
  267. if (operations[i].operators[j].variable.hash === variable.hash) {
  268. document.getElementById(`operation${operations[i].hash}Li`).remove();
  269. operationsToRemove.push(operations[i]);
  270. // if (operation.operators.length > 2) {
  271. //
  272. // } else {
  273. //
  274. // }
  275. }
  276. }
  277. }
  278. }
  279. for (const i in operationsToRemove) {
  280. operations.splice(operations.indexOf(operationsToRemove[i]), 1);
  281. }
  282. }
  283. // Here we remove all operation that include the variable received as parameter
  284. export function deleteOperation(hash) {
  285. const operation = getOperationByHash(hash);
  286. document.getElementById(`operation${hash}Li`).remove();
  287. operations.splice(operations.indexOf(operation), 1);
  288. }
  289. // Here we remove all operation
  290. export function deleteAllOperation() {
  291. for (let i = 0; i < operations.length; i++) {
  292. document.getElementById(`operation${operations[i].hash}Li`).remove();
  293. }
  294. operations.length = 0;
  295. }
  296. // ***********************************************************************
  297. // Managing operation editing
  298. // ***********************************************************************
  299. const manageOperationEditing = (hash, enableForEditing = false) => {
  300. const operation = getOperationByHash(hash);
  301. const operationForm = document.getElementById(
  302. `operation${operation.hash}Form`
  303. );
  304. switch (operation.type) {
  305. case "ASSIGN":
  306. if (operation.editing && !enableForEditing) {
  307. // Editing fields
  308. operationForm.childNodes[1].childNodes[1].style.display = "none";
  309. operationForm.childNodes[1].childNodes[3].style.display = "none";
  310. operationForm.childNodes[1].childNodes[5].style.display = "none";
  311. // Resume field
  312. operationForm.childNodes[1].childNodes[7].style.display = "inline";
  313. operation.editing = false;
  314. } else if (enableForEditing) {
  315. // Editing fields
  316. operationForm.childNodes[1].childNodes[1].style.display = "inline";
  317. operationForm.childNodes[1].childNodes[3].style.display = "inline";
  318. operationForm.childNodes[1].childNodes[5].style.display = "inline";
  319. // Resume field
  320. operationForm.childNodes[1].childNodes[7].style.display = "none";
  321. // Changing focus to firt child
  322. document
  323. .getElementById(`operation${operation.hash}AssignVariableSelect`)
  324. .focus();
  325. operation.editing = true;
  326. }
  327. break;
  328. case "PRINT":
  329. if (operation.editing && !enableForEditing) {
  330. // Editing fields
  331. operationForm.childNodes[1].childNodes[1].style.display = "none";
  332. operationForm.childNodes[1].childNodes[3].style.display = "none";
  333. // Resume field
  334. operationForm.childNodes[1].childNodes[5].style.display = "inline";
  335. operation.editing = false;
  336. } else if (enableForEditing) {
  337. // Editing fields
  338. operationForm.childNodes[1].childNodes[1].style.display = "inline";
  339. operationForm.childNodes[1].childNodes[3].style.display = "inline";
  340. // Resume field
  341. operationForm.childNodes[1].childNodes[5].style.display = "none";
  342. // Changing focus to firt child
  343. document
  344. .getElementById(`operation${operation.hash}VariableSelectLabel`)
  345. .focus();
  346. operation.editing = true;
  347. }
  348. break;
  349. }
  350. };
  351. // ***********************************************************************
  352. // Updating variable resume
  353. // ***********************************************************************
  354. function updateResume(hash) {
  355. const resume = generateResume(hash);
  356. document.getElementById(`operation${hash}DisplayCode`).innerText =
  357. resume.display;
  358. document
  359. .getElementById(`operation${hash}Resume`)
  360. .setAttribute("title", resume.resume);
  361. }
  362. function generateResume(hash) {
  363. const operation = getOperationByHash(hash);
  364. let resume;
  365. let display;
  366. switch (operation.type) {
  367. case "ASSIGN":
  368. resume = `${operation.assignedVariable.name}, recebe: `;
  369. display = `${operation.assignedVariable.name} <- `;
  370. for (const i in operation.operators) {
  371. console.log(operation.operators[i]);
  372. switch (operation.operators[i].type.value) {
  373. case Operators.OPERATOR:
  374. resume += `${operation.operators[i].operator.friendlyName} `;
  375. display += `${operation.operators[i].operator.name} `;
  376. break;
  377. case Operators.VALUE:
  378. resume += `${operation.operators[i].value} `;
  379. display += `${operation.operators[i].value} `;
  380. break;
  381. case Operators.VARIABLE:
  382. resume += `${operation.operators[i].variable.name} `;
  383. display += `${operation.operators[i].variable.name} `;
  384. break;
  385. }
  386. }
  387. break;
  388. case "PRINT":
  389. resume = `Escreva a variável: ${operation.assignedVariable.name}`;
  390. display = `escreva (${operation.assignedVariable.name});`;
  391. break;
  392. }
  393. return { resume: resume, display: display };
  394. }
  395. // ***********************************************************************
  396. // Print
  397. // ***********************************************************************
  398. export function createPrintOperation() {
  399. const operation = Object.assign({}, operationScheme);
  400. operation.hash = generateUUID().replaceAll("-", "");
  401. operation.assignedVariable = variables.length > 0 ? variables[0] : null;
  402. operation.type = "PRINT";
  403. operations.push(operation);
  404. const printOperation = printOperationScheme(operation);
  405. htmlOlCommandsOperations.insertAdjacentHTML("beforeend", printOperation);
  406. updateResume(operation.hash);
  407. // Changing focus to the variable type after creation for screen readers
  408. document
  409. .getElementById(`operation${operation.hash}VariableSelectLabel`)
  410. .focus();
  411. document
  412. .getElementById(`operation${operation.hash}VariableSelect`)
  413. .addEventListener("change", (ev) => {
  414. updatePrintOperation(
  415. ev.target.getAttribute("operation-id"),
  416. ev.target.value
  417. );
  418. });
  419. document
  420. .getElementById(`operation${operation.hash}Delete`)
  421. .addEventListener("click", (ev) => {
  422. deleteOperation(ev.target.getAttribute("operation-id"));
  423. });
  424. document
  425. .getElementById(`operation${operation.hash}Resume`)
  426. .addEventListener("focus", (ev) => {
  427. const operationHash = ev.target.getAttribute("operation-id");
  428. manageOperationEditing(operationHash);
  429. });
  430. document
  431. .getElementById(`operation${operation.hash}Display`)
  432. .addEventListener("keyup", (ev) => {
  433. const operationHash = ev.target.getAttribute("operation-id");
  434. if (ev.key === "Enter") manageOperationEditing(operationHash, true);
  435. });
  436. }
  437. // TODO:
  438. // terminal => basico
  439. function updatePrintOperation(hash, newValue) {
  440. const operation = getOperationByHash(hash);
  441. const variable = getVariableByHash(newValue);
  442. operation.assignedVariable = variable;
  443. updateResume(operation.hash);
  444. }
  445. // ***********************************************************************
  446. // Util
  447. // ***********************************************************************
  448. export function getOperationByHash(hash) {
  449. for (let i = 0; i < operations.length; i++) {
  450. if (operations[i].hash === hash) return operations[i];
  451. }
  452. return null;
  453. }
  454. // ***********************************************************************