ivprog-visual-functions-1.0.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var counter_new_functions = 0;
  2. function addFunctionHandler() {
  3. new_function = new Funcao("new_function_" + counter_new_functions);
  4. adicionarFuncao(new_function);
  5. counter_new_functions ++;
  6. renderAlgorithm();
  7. }
  8. function updateSequenceFunctionHandler(index_from, index_to) {
  9. programa.funcoes.splice(index_to, 0, programa.funcoes.splice(index_from, 1)[0]);
  10. renderAlgorithm();
  11. }
  12. function removeFunctionHandler(div_function, sequence) {
  13. programa.funcoes.splice(sequence, 1);
  14. $(div_function).slideUp(400, function(){
  15. renderAlgorithm();
  16. });
  17. }
  18. function minimizeFunctionHandler(div_function, sequence) {
  19. $(div_function).find(".function_area").toggle();
  20. programa.funcoes[sequence].esta_oculta = !programa.funcoes[sequence].esta_oculta;
  21. }
  22. function renderAlgorithm() {
  23. $('.all_functions').empty();
  24. for (i = 0; i < programa.funcoes.length; i++) {
  25. appendFunction(programa.funcoes[i], i);
  26. }
  27. $('.data_types_dropdown').dropdown();
  28. }
  29. function updateFunctionReturn(sequence, new_value) {
  30. programa.funcoes[sequence].tipo_retorno = new_value;
  31. }
  32. var opened_name_function = false;
  33. var opened_input = null;
  34. var sequence_name_opened;
  35. function enableNameFunctionUpdate(div_el, sequence) {
  36. if (opened_name_function) {
  37. $(opened_input).focus();
  38. return;
  39. }
  40. opened_name_function = true;
  41. sequence_name_opened = sequence;
  42. $(div_el).find('.span_name_function').text('');
  43. $( "<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'));
  44. $('.width-dynamic').on('input', function() {
  45. var inputWidth = $(this).textWidth()+10;
  46. opened_input = this;
  47. $(this).focus();
  48. var tmpStr = $(this).val();
  49. $(this).val('');
  50. $(this).val(tmpStr);
  51. $(this).css({
  52. width: inputWidth
  53. })
  54. }).trigger('input');
  55. $('.width-dynamic').focusout(function() {
  56. /// update array:
  57. if ($(this).val().trim()) {
  58. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  59. }
  60. $(this).remove();
  61. /// update elements:
  62. opened_name_function = false;
  63. opened_input = false;
  64. renderAlgorithm();
  65. });
  66. $('.width-dynamic').on('keydown', function(e) {
  67. var code = e.keyCode || e.which;
  68. if(code == 13) {
  69. if ($(this).val().trim()) {
  70. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  71. }
  72. $(this).remove();
  73. /// update elements:
  74. opened_name_function = false;
  75. opened_input = false;
  76. renderAlgorithm();
  77. }
  78. });
  79. }
  80. function appendFunction(function_obj, sequence) {
  81. $('.all_functions').append('<div class="ui secondary segment function_div list-group-item">'
  82. + '<span class="glyphicon glyphicon-move" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>'
  83. + (!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>' : '')
  84. + '<button class="ui icon button tiny minimize_function_button" onclick="minimizeFunctionHandler(this.parentNode, '+sequence+')"><i class="icon window minimize"></i></button>'
  85. + '<div class="function_signature_div">function '
  86. + '<select class="ui fluid dropdown data_types_dropdown" onchange="updateFunctionReturn('+sequence+', this.value)">'
  87. + '<option value="'+tiposDados.void+'" '+(function_obj.tipo_retorno == tiposDados.void ? 'selected' : '')+'>'+tiposDados.void+'</option>'
  88. + '<option value="'+tiposDados.integer+'" '+(function_obj.tipo_retorno == tiposDados.integer ? 'selected' : '')+'>'+tiposDados.integer+'</option>'
  89. + '<option value="'+tiposDados.real+'" '+(function_obj.tipo_retorno == tiposDados.real ? 'selected' : '')+'>'+tiposDados.real+'</option>'
  90. + '<option value="'+tiposDados.text+'" '+(function_obj.tipo_retorno == tiposDados.text ? 'selected' : '')+'>'+tiposDados.text+'</option>'
  91. + '<option value="'+tiposDados.boolean+'" '+(function_obj.tipo_retorno == tiposDados.boolean ? 'selected' : '')+'>'+tiposDados.boolean+'</option>'
  92. + '</select>'
  93. + '<div class="function_name_div"><span class="span_name_function">'+function_obj.nome+'</span> <i class="icon small pencil alternate enable_edit_name_function" onclick="enableNameFunctionUpdate(this.parentNode, '+sequence+')"></i></div> ( ) {</div>'
  94. + (function_obj.esta_oculta ? '<div class="function_area" style="display: none;">' : '<div class="function_area">')
  95. + '<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>'
  96. + '<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>'
  97. + '<div class="function_close_div">}</div>'
  98. + '</div>'
  99. + '</div>');
  100. }
  101. $.fn.textWidth = function(text, font) {
  102. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  103. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  104. return $.fn.textWidth.fakeEl.width();
  105. };