variables.js 6.5 KB

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