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.

110 lines
2.9 KiB

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