ivprog-visual-functions-1.0.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. var counter_new_functions = 0;
  2. var counter_new_parameters = 0;
  3. function addFunctionHandler() {
  4. new_function = new Funcao("new_function_" + counter_new_functions);
  5. adicionarFuncao(new_function);
  6. counter_new_functions ++;
  7. renderAlgorithm();
  8. }
  9. function updateSequenceFunctionHandler(index_from, index_to) {
  10. programa.funcoes.splice(index_to, 0, programa.funcoes.splice(index_from, 1)[0]);
  11. renderAlgorithm();
  12. }
  13. function removeFunctionHandler(div_function, sequence) {
  14. programa.funcoes.splice(sequence, 1);
  15. $(div_function).slideUp(400, function(){
  16. renderAlgorithm();
  17. });
  18. }
  19. function minimizeFunctionHandler(div_function, sequence) {
  20. $(div_function).find(".function_area").toggle();
  21. programa.funcoes[sequence].esta_oculta = !programa.funcoes[sequence].esta_oculta;
  22. }
  23. function renderAlgorithm() {
  24. $('.all_functions').empty();
  25. for (i = 0; i < programa.funcoes.length; i++) {
  26. appendFunction(programa.funcoes[i], i);
  27. }
  28. $('.data_types_dropdown').dropdown();
  29. $('.parameter_data_types_dropdown').dropdown();
  30. addHandlers();
  31. }
  32. function addHandlers() {
  33. }
  34. function addParameter(sequence) {
  35. if (programa.funcoes[sequence].lista_parametros == null) {
  36. programa.funcoes[sequence].lista_parametros = new Array();
  37. }
  38. programa.funcoes[sequence].lista_parametros.push(new Variavel(tiposDados.integer, "new_parameter_" + counter_new_parameters));
  39. counter_new_parameters ++;
  40. renderAlgorithm();
  41. }
  42. function updateFunctionReturn(sequence, new_value) {
  43. programa.funcoes[sequence].tipo_retorno = new_value;
  44. }
  45. function updateParameterType(wich_function, wich_parameter, new_value) {
  46. programa.funcoes[wich_function].lista_parametros[wich_parameter].tipo = new_value;
  47. }
  48. var opened_name_function = false;
  49. var opened_input = null;
  50. var sequence_name_opened;
  51. function enableNameFunctionUpdate(div_el, sequence) {
  52. if (opened_name_function) {
  53. $(opened_input).focus();
  54. return;
  55. }
  56. opened_name_function = true;
  57. sequence_name_opened = sequence;
  58. $(div_el).find('.span_name_function').text('');
  59. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+programa.funcoes[sequence].nome+"' />" ).insertBefore($(div_el).find('.span_name_function'));
  60. $('.width-dynamic').on('input', function() {
  61. var inputWidth = $(this).textWidth()+10;
  62. opened_input = this;
  63. $(this).focus();
  64. var tmpStr = $(this).val();
  65. $(this).val('');
  66. $(this).val(tmpStr);
  67. $(this).css({
  68. width: inputWidth
  69. })
  70. }).trigger('input');
  71. $('.width-dynamic').focusout(function() {
  72. /// update array:
  73. if ($(this).val().trim()) {
  74. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  75. }
  76. $(this).remove();
  77. /// update elements:
  78. opened_name_function = false;
  79. opened_input = false;
  80. renderAlgorithm();
  81. });
  82. $('.width-dynamic').on('keydown', function(e) {
  83. var code = e.keyCode || e.which;
  84. if(code == 13) {
  85. if ($(this).val().trim()) {
  86. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  87. }
  88. $(this).remove();
  89. /// update elements:
  90. opened_name_function = false;
  91. opened_input = false;
  92. renderAlgorithm();
  93. }
  94. if(code == 27) {
  95. $(div_el).find('.span_name_function').text(programa.funcoes[sequence_name_opened].nome);
  96. $(this).remove();
  97. /// update elements:
  98. opened_name_function = false;
  99. opened_input = false;
  100. }
  101. });
  102. }
  103. var opened_name_parameter = false;
  104. var opened_input_parameter = null;
  105. var sequence_name_opened_parameter;
  106. var sequence_function_opened_parameter;
  107. function enableNameParameterUpdate(parent_node, which_function, which_parameter) {
  108. if (opened_name_parameter) {
  109. $(opened_input_parameter).focus();
  110. return;
  111. }
  112. opened_name_parameter = true;
  113. sequence_name_opened_parameter = which_parameter;
  114. sequence_function_opened_parameter = which_function;
  115. $(parent_node).find('.span_name_parameter').text('');
  116. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+programa.funcoes[which_function].lista_parametros[which_parameter].nome+"' />" ).insertBefore($(parent_node).find('.span_name_parameter'));
  117. $('.width-dynamic').on('input', function() {
  118. var inputWidth = $(this).textWidth()+10;
  119. opened_input_parameter = this;
  120. $(this).focus();
  121. var tmpStr = $(this).val();
  122. $(this).val('');
  123. $(this).val(tmpStr);
  124. $(this).css({
  125. width: inputWidth
  126. })
  127. }).trigger('input');
  128. $('.width-dynamic').focusout(function() {
  129. /// update array:
  130. if ($(this).val().trim()) {
  131. programa.funcoes[which_function].lista_parametros[which_parameter].nome = $(this).val().trim();
  132. }
  133. $(this).remove();
  134. /// update elements:
  135. opened_name_parameter = false;
  136. opened_input_parameter = false;
  137. renderAlgorithm();
  138. });
  139. $('.width-dynamic').on('keydown', function(e) {
  140. var code = e.keyCode || e.which;
  141. if(code == 13) {
  142. if ($(this).val().trim()) {
  143. programa.funcoes[which_function].lista_parametros[which_parameter].nome = $(this).val().trim();
  144. }
  145. $(this).remove();
  146. /// update elements:
  147. opened_name_parameter = false;
  148. opened_input_parameter = false;
  149. renderAlgorithm();
  150. }
  151. if(code == 27) {
  152. $(parent_node).find('.span_name_parameter').text(programa.funcoes[which_function].lista_parametros[which_parameter].nome);
  153. $(this).remove();
  154. /// update elements:
  155. opened_name_parameter = false;
  156. opened_input_parameter = false;
  157. }
  158. });
  159. }
  160. function appendFunction(function_obj, sequence) {
  161. var appender = '<div class="ui secondary segment function_div list-group-item">'
  162. + '<span class="glyphicon glyphicon-move" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>'
  163. + (!function_obj.eh_principal ? '<button class="ui icon button large remove_function_button" onclick="removeFunctionHandler(this.parentNode, '+sequence+')"><i class="red icon times"></i></button>' : '')
  164. + '<button class="ui icon button tiny minimize_function_button" onclick="minimizeFunctionHandler(this.parentNode, '+sequence+')"><i class="icon window minimize"></i></button>'
  165. + '<div class="function_signature_div">function '
  166. + '<select class="ui fluid dropdown data_types_dropdown" onchange="updateFunctionReturn('+sequence+', this.value)">'
  167. + '<option value="'+tiposDados.void+'" '+(function_obj.tipo_retorno == tiposDados.void ? 'selected' : '')+'>'+tiposDados.void+'</option>'
  168. + '<option value="'+tiposDados.integer+'" '+(function_obj.tipo_retorno == tiposDados.integer ? 'selected' : '')+'>'+tiposDados.integer+'</option>'
  169. + '<option value="'+tiposDados.real+'" '+(function_obj.tipo_retorno == tiposDados.real ? 'selected' : '')+'>'+tiposDados.real+'</option>'
  170. + '<option value="'+tiposDados.text+'" '+(function_obj.tipo_retorno == tiposDados.text ? 'selected' : '')+'>'+tiposDados.text+'</option>'
  171. + '<option value="'+tiposDados.boolean+'" '+(function_obj.tipo_retorno == tiposDados.boolean ? 'selected' : '')+'>'+tiposDados.boolean+'</option>'
  172. + '</select>'
  173. + '<div class="function_name_div"><span class="span_name_function" ondblclick="enableNameFunctionUpdate(this.parentNode, '+sequence+')" >'+function_obj.nome+'</span> <i class="icon small pencil alternate enable_edit_name_function" onclick="enableNameFunctionUpdate(this.parentNode, '+sequence+')"></i></div> '
  174. + '( <i class="ui icon plus square outline add_parameter" onclick="addParameter('+sequence+')"></i> <div class="ui large labels parameters_list">';
  175. if (function_obj.lista_parametros != null) {
  176. for (var j = 0; j < function_obj.lista_parametros.length; j++) {
  177. var par_temp = function_obj.lista_parametros[j];
  178. appender += '<div class="ui label function_name_parameter"><span class="span_name_parameter" ondblclick="enableNameParameterUpdate(this.parentNode, '+sequence+', '+j+')">'+par_temp.nome+'</span> <i class="icon small pencil alternate enable_edit_name_parameter" onclick="enableNameParameterUpdate(this.parentNode, '+sequence+', '+j+')"></i>';
  179. appender += '<select class="ui fluid dropdown parameter_data_types_dropdown" onchange="updateParameterType('+sequence+', '+j+', this.value)">'
  180. + '<option value="'+tiposDados.integer+'" '+(par_temp.tipo == tiposDados.integer ? 'selected' : '')+'>'+tiposDados.integer+'</option>'
  181. + '<option value="'+tiposDados.real+'" '+(par_temp.tipo == tiposDados.real ? 'selected' : '')+'>'+tiposDados.real+'</option>'
  182. + '<option value="'+tiposDados.text+'" '+(par_temp.tipo == tiposDados.text ? 'selected' : '')+'>'+tiposDados.text+'</option>'
  183. + '<option value="'+tiposDados.boolean+'" '+(par_temp.tipo == tiposDados.boolean ? 'selected' : '')+'>'+tiposDados.boolean+'</option>'
  184. + '</select>';
  185. appender += "</div>";
  186. }
  187. }
  188. appender += '</div> ) {</div>'
  189. + (function_obj.esta_oculta ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ')
  190. + '<div class="ui top attached segment variables_list_div"><div class="ui teal small labeled icon button add_variable_button">Variable<i class="add icon"></i></div></div>'
  191. + '<div class="ui bottom attached segment commands_list_div"><div class="ui teal small labeled icon button add_command_button">Command<i class="add icon"></i></div></div>'
  192. + '<div class="function_close_div">}</div>'
  193. + '</div>'
  194. + '</div>';
  195. $('.all_functions').append(appender);
  196. }
  197. $.fn.textWidth = function(text, font) {
  198. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  199. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  200. return $.fn.textWidth.fakeEl.width();
  201. };