You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.7 KiB

2 months ago
2 months ago
2 months ago
  1. var outputs = [];
  2. var simulator = [];
  3. var graph = "";
  4. var textarea = document.getElementById("firstInput");
  5. var editor = CodeMirror.fromTextArea(textarea, {
  6. mode: "text-x/myLanguage",
  7. lineNumbers: true,
  8. autoCloseTags: true,
  9. });
  10. var input2 = document.getElementById("secondInput");
  11. var editor2 = CodeMirror.fromTextArea(input2, {
  12. mode: "text-x/myLanguage",
  13. lineNumbers: true,
  14. });
  15. document.addEventListener("DOMContentLoaded", function () {
  16. //Run function
  17. document.getElementById("runBtn").addEventListener("click", function () {
  18. var input1 = editor.getValue();
  19. console.log("CodeMirror input1:", input1);
  20. var input2 = editor2.getValue();
  21. console.log("Second input:", input2);
  22. if (!input1 || !input2) {
  23. Toastify({
  24. text: "Input fields cannot be empty!",
  25. duration: 3000,
  26. close: true,
  27. gravity: "top",
  28. }).showToast();
  29. } else {
  30. outputFunction(input1, input2);
  31. simulatorFunction(input1, input2);
  32. //graphFunction(input1, input2);
  33. updateTextarea(getActiveTabContent());
  34. // Clear the input fields after execution
  35. document.getElementById("secondInput").value = "";
  36. // Log checked checkbox values
  37. let checked = document.querySelectorAll("input[type='checkbox']:checked");
  38. let checkedValues = Array.from(checked).map((cb) => cb.value);
  39. console.log(checkedValues);
  40. }
  41. });
  42. });
  43. //Functions
  44. function simulatorFunction(input1, input2) {
  45. var result = input1 + input2;
  46. simulator.push(result);
  47. }
  48. function outputFunction(input1, input2) {
  49. var result = input1 + " " + input2;
  50. outputs.push(result);
  51. }
  52. function graphFunction(svg) {
  53. graph = svg;
  54. }
  55. function getActiveTabContent() {
  56. var activeTabId = document.querySelector(".rightNav.active").id;
  57. switch (activeTabId) {
  58. case "output":
  59. return outputs;
  60. case "simulator":
  61. return simulator;
  62. case "graph":
  63. return graph;
  64. default:
  65. return [];
  66. }
  67. }
  68. function updateTextarea(data) {
  69. scrollableOutput.innerHTML = "";
  70. if (data == graph) {
  71. if (data == "") {
  72. scrollableOutput.innerHTML = "";
  73. } else {
  74. scrollableOutput.appendChild(data);
  75. }
  76. } else {
  77. const formattedData = data.join("\r\n").replace(/(\r\n|\r|\n)/g, "<br>");
  78. scrollableOutput.innerHTML = formattedData;
  79. }
  80. }
  81. // Add click event listeners to tab links
  82. document.querySelectorAll(".rightNav").forEach((navLink) => {
  83. navLink.addEventListener("click", function (event) {
  84. event.preventDefault();
  85. document.querySelectorAll(".rightNav").forEach((navLink) => {
  86. navLink.classList.remove("active");
  87. });
  88. this.classList.add("active");
  89. updateTextarea(getActiveTabContent());
  90. });
  91. });