code-view.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const $code = $("#code");
  2. const $main = $("#main");
  3. const $functions = $("#functions");
  4. let universalId = 0;
  5. let creatingVariable = false;
  6. function addVariable(variable = {name: "", type: dataTypes.String, value: ""}, id = null) {
  7. if (creatingVariable === false) {
  8. // Blocking future variable creation until finish the current creation
  9. creatingVariable = true;
  10. let html = "";
  11. html += `<div class="card col-3" id="variableCreationDiv">`;
  12. html += ` <div class="card-header">`;
  13. html += ` <h3>Adicionar variável</h3>`;
  14. html += ` </div>`;
  15. html += ` <div class="card-body">`;
  16. html += ` <div class="row">`;
  17. html += ` <div class="col-3">`;
  18. html += ` <label for="variableName">Nome: </label>`;
  19. html += ` </div>`;
  20. html += ` <div class="col-9">`;
  21. html += ` <input id="varibaleName" type="text" value="${variable.name}">`;
  22. html += ` </div>`;
  23. html += ` </div>`;
  24. html += ` <div class="row">`;
  25. html += ` <div class="col-3">`;
  26. html += ` <label for="variableType">Tipo: </label>`;
  27. html += ` </div>`;
  28. html += ` <div class="col-9">`;
  29. html += ` <select id="variableType">`;
  30. // Filling data type select with available data types in {data-types.js}
  31. for (let dataType in dataTypes) {
  32. if (dataType === variable.type)
  33. html += ` <option value="${dataType}" selected>${dataTypes[dataType]}</option>`;
  34. else
  35. html += ` <option value="${dataType}">${dataTypes[dataType]}</option>`;
  36. }
  37. html += ` </select>`;
  38. html += ` </div>`;
  39. html += ` </div>`;
  40. html += ` <div class="row">`;
  41. html += ` <div class="col-3">`;
  42. html += ` <label for="variableValue">Valor: </label>`;
  43. html += ` </div>`;
  44. html += ` <div class="col-9">`;
  45. html += ` <input id="variableValue" type="text" value="${variable.value}">`;
  46. html += ` </div>`;
  47. html += ` </div>`;
  48. html += ` </div>`;
  49. html += ` <div class="card-footer">`;
  50. html += ` <div class="row">`;
  51. html += ` <div class="col-6">`;
  52. html += ` <button type="button" accesskey="c" onclick="cancelAddVariable(${id})">Cancelar</button>`;
  53. html += ` </div>`;
  54. html += ` <div class="col-6">`;
  55. html += ` <button type="button" accesskey="s" onclick="addVariableTocode({id: ${id}})">Salvar</button>`;
  56. html += ` </div>`;
  57. html += ` </div>`;
  58. html += ` </div>`;
  59. html += `</div>`;
  60. $code.append(html);
  61. }
  62. }
  63. function addVariableTocode({id = null}) {
  64. let code = "";
  65. let variable = {
  66. name: $("#varibaleName").val(),
  67. type: $("#variableType").val(),
  68. value: $("#variableValue").val()
  69. };
  70. let variableBarrier = getVariableTypeBarrier(variable.type);
  71. code += `${variable.type} ${variable.name} = ${variableBarrier}${variable.value}${variableBarrier}`;
  72. // Sending code to screen
  73. addCodeToScreen({id: id, code: code, variable: variable});
  74. // Removing div form for variable creation
  75. $("#variableCreationDiv").remove();
  76. // Enabling future variable creation
  77. creatingVariable = false;
  78. }
  79. function cancelAddVariable(id = null) {
  80. // Removing div form for variable creation
  81. $("#variableCreationDiv").remove();
  82. if (id != null)
  83. $(`#codeLine${id}`).show();
  84. // Enabling future variable creation
  85. creatingVariable = false;
  86. }
  87. function editVariable(id = null, variable = {name: "", type: dataTypes.String, value: ""}) {
  88. $(`#codeLine${id}`).hide();
  89. addVariable(variable, id);
  90. }
  91. function deleteVariable(id = null) {
  92. if (id != null)
  93. $(`#codeLine${id}`).remove();
  94. }
  95. function addCodeToScreen({
  96. id = null,
  97. code = "Some code",
  98. variable = {name: "", type: "String", value: ""}
  99. }) {
  100. let html = ``;
  101. if (id != null) {
  102. $(`#mainCode${id}`).html(`<span>${code}</span>`);
  103. $(`#editCode${id}`).html(`<button type="button" id="editCodeButton${id}" onclick='editVariable(${id}, ${JSON.stringify(variable)})'>Editar</button>`);
  104. $(`#codeLine${id}`).show();
  105. } else {
  106. html += `<div class="row code-line" id="codeLine${universalId}">`;
  107. html += ` <div class="col-1 line-number" id="mainLine${universalId}"><span>${getCurrentMainLineNumber()}</span></div>`;
  108. html += ` <div class="col-7 line-code" id="mainCode${universalId}"><span>${code}</span></div>`;
  109. html += ` <div class="col-2" id="editCode${universalId}">`;
  110. html += ` <button type="button" id="editCodeButton${universalId}" onclick='editVariable(${universalId}, ${JSON.stringify(variable)})'>Editar</button>`;
  111. html += ` </div>`;
  112. html += ` <div class="col-2" id="deleteCode${universalId}">`;
  113. html += ` <button type="button" id="deleteCodeButton${universalId}" onclick="deleteVariable(${universalId})">Deletar</button>`;
  114. html += ` </div>`;
  115. html += `</div>`;
  116. $code.append(html);
  117. }
  118. // Increasing universal id
  119. universalId += 1;
  120. }
  121. function addFunctionToScreen({
  122. name = "functionName",
  123. }) {
  124. }
  125. function getCurrentMainLineNumber() {
  126. // TO DO: Find a way to know in wath line the main code is
  127. return $(".code-line").length + 1;
  128. }