ivprog-visual-functions-1.0.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. var counter_new_functions = 0;
  2. var counter_new_parameters = 0;
  3. function addFunctionHandler() {
  4. new_function = new Funcao(i18n("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. $('.ui.dropdown.function_return')
  34. .dropdown({
  35. onChange: function(value, text, $selectedItem) {
  36. classList = $selectedItem.attr('class').split(/\s+/);
  37. $.each(classList, function(index, item) {
  38. if (item.indexOf("seq_") > -1) {
  39. seq = item.split("seq_")[1];
  40. for (tm in tiposDados) {
  41. if ($selectedItem.hasClass(tm)) {
  42. programa.funcoes[seq].tipo_retorno = tm;
  43. }
  44. }
  45. updateFunctionReturn(seq, value);
  46. }
  47. });
  48. }
  49. })
  50. ;
  51. $('.ui.dropdown.parameter_type').dropdown({
  52. onChange: function(value, text, $selectedItem) {
  53. classList = $selectedItem.attr('class').split(/\s+/);
  54. var fun;
  55. var seq;
  56. $.each(classList, function(index, item) {
  57. if (item.indexOf("fun_") > -1) {
  58. fun = item.split("fun_")[1];
  59. }
  60. if (item.indexOf("seq_") > -1) {
  61. seq = item.split("seq_")[1];
  62. }
  63. });
  64. var dim = 0;
  65. if (value.indexOf(i18n(tiposDados.vector)) > -1) {
  66. dim = 1;
  67. }
  68. for (tm in tiposDados) {
  69. if ($selectedItem.hasClass(tm)) {
  70. console.log("possui: " + tm);
  71. updateParameterType(fun, seq, tm, dim);
  72. break;
  73. }
  74. }
  75. }
  76. });
  77. }
  78. function addParameter(sequence) {
  79. if (programa.funcoes[sequence].lista_parametros == null) {
  80. programa.funcoes[sequence].lista_parametros = new Array();
  81. }
  82. programa.funcoes[sequence].lista_parametros.push(new Variavel(tiposDados.integer, i18n("new_parameter") + "_" + counter_new_parameters));
  83. counter_new_parameters ++;
  84. renderAlgorithm();
  85. }
  86. function updateFunctionReturn(sequence, new_value) {
  87. if (new_value.indexOf(i18n(tiposDados.vector)) > -1) {
  88. programa.funcoes[sequence].dimensoes_retorno = 1;
  89. } else {
  90. programa.funcoes[sequence].dimensoes_retorno = 0;
  91. }
  92. }
  93. function updateParameterType(wich_function, wich_parameter, new_value, new_dimensions) {
  94. programa.funcoes[wich_function].lista_parametros[wich_parameter].tipo = new_value;
  95. programa.funcoes[wich_function].lista_parametros[wich_parameter].dimensoes = new_dimensions;
  96. }
  97. var opened_name_function = false;
  98. var opened_input = null;
  99. var sequence_name_opened;
  100. function enableNameFunctionUpdate(div_el, sequence) {
  101. if (opened_name_function) {
  102. $(opened_input).focus();
  103. return;
  104. }
  105. opened_name_function = true;
  106. sequence_name_opened = sequence;
  107. $(div_el).find('.span_name_function').text('');
  108. $( "<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'));
  109. $('.width-dynamic').on('input', function() {
  110. var inputWidth = $(this).textWidth()+10;
  111. opened_input = this;
  112. $(this).focus();
  113. var tmpStr = $(this).val();
  114. $(this).val('');
  115. $(this).val(tmpStr);
  116. $(this).css({
  117. width: inputWidth
  118. })
  119. }).trigger('input');
  120. $('.width-dynamic').focusout(function() {
  121. /// update array:
  122. if ($(this).val().trim()) {
  123. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  124. }
  125. $(this).remove();
  126. /// update elements:
  127. opened_name_function = false;
  128. opened_input = false;
  129. renderAlgorithm();
  130. });
  131. $('.width-dynamic').on('keydown', function(e) {
  132. var code = e.keyCode || e.which;
  133. if(code == 13) {
  134. if ($(this).val().trim()) {
  135. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  136. }
  137. $(this).remove();
  138. /// update elements:
  139. opened_name_function = false;
  140. opened_input = false;
  141. renderAlgorithm();
  142. }
  143. if(code == 27) {
  144. $(div_el).find('.span_name_function').text(programa.funcoes[sequence_name_opened].nome);
  145. $(this).remove();
  146. /// update elements:
  147. opened_name_function = false;
  148. opened_input = false;
  149. }
  150. });
  151. }
  152. var opened_name_parameter = false;
  153. var opened_input_parameter = null;
  154. var sequence_name_opened_parameter;
  155. var sequence_function_opened_parameter;
  156. function enableNameParameterUpdate(parent_node, which_function, which_parameter) {
  157. if (opened_name_parameter) {
  158. $(opened_input_parameter).focus();
  159. return;
  160. }
  161. opened_name_parameter = true;
  162. sequence_name_opened_parameter = which_parameter;
  163. sequence_function_opened_parameter = which_function;
  164. $(parent_node).find('.span_name_parameter').text('');
  165. $( "<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'));
  166. $('.width-dynamic').on('input', function() {
  167. var inputWidth = $(this).textWidth()+10;
  168. opened_input_parameter = this;
  169. $(this).focus();
  170. var tmpStr = $(this).val();
  171. $(this).val('');
  172. $(this).val(tmpStr);
  173. $(this).css({
  174. width: inputWidth
  175. })
  176. }).trigger('input');
  177. $('.width-dynamic').focusout(function() {
  178. /// update array:
  179. if ($(this).val().trim()) {
  180. programa.funcoes[which_function].lista_parametros[which_parameter].nome = $(this).val().trim();
  181. }
  182. $(this).remove();
  183. /// update elements:
  184. opened_name_parameter = false;
  185. opened_input_parameter = false;
  186. renderAlgorithm();
  187. });
  188. $('.width-dynamic').on('keydown', function(e) {
  189. var code = e.keyCode || e.which;
  190. if(code == 13) {
  191. if ($(this).val().trim()) {
  192. programa.funcoes[which_function].lista_parametros[which_parameter].nome = $(this).val().trim();
  193. }
  194. $(this).remove();
  195. /// update elements:
  196. opened_name_parameter = false;
  197. opened_input_parameter = false;
  198. renderAlgorithm();
  199. }
  200. if(code == 27) {
  201. $(parent_node).find('.span_name_parameter').text(programa.funcoes[which_function].lista_parametros[which_parameter].nome);
  202. $(this).remove();
  203. /// update elements:
  204. opened_name_parameter = false;
  205. opened_input_parameter = false;
  206. }
  207. });
  208. }
  209. function removeParameter(parent_node, which_function, which_parameter) {
  210. programa.funcoes[which_function].lista_parametros.splice(which_parameter, 1);
  211. renderAlgorithm();
  212. }
  213. function appendFunction(function_obj, sequence) {
  214. var appender = '<div class="ui secondary segment function_div list-group-item">'
  215. + '<span class="glyphicon glyphicon-move" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>'
  216. + (!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>' : '')
  217. + '<button class="ui icon button tiny minimize_function_button" onclick="minimizeFunctionHandler(this.parentNode, '+sequence+')"><i class="icon window minimize"></i></button>'
  218. + '<div class="function_signature_div">'+i18n('function')+' ';
  219. if (function_obj.eh_principal) {
  220. appender += '<div class="function_name_div"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' + i18n('void') + ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="span_name_function" >'+function_obj.nome+'</span> </div> '
  221. + '( <div class="ui large labels parameters_list">';
  222. } else {
  223. appender += renderFunctionReturn(function_obj, sequence);
  224. appender += '<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> '
  225. + '( <i class="ui icon plus square outline add_parameter" onclick="addParameter('+sequence+')"></i> <div class="ui large labels parameters_list">';
  226. }
  227. appender += renderFunctionParameters(function_obj, sequence);
  228. appender += '</div> ) {</div>'
  229. + (function_obj.esta_oculta ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ')
  230. + '<div class="ui top attached segment variables_list_div"><div class="ui teal small labeled icon button add_variable_button">'+i18n('Variable')+'<i class="add icon"></i></div></div>'
  231. + '<div class="ui bottom attached segment commands_list_div"><div class="ui teal small labeled icon button add_command_button">'+i18n('Command')+'<i class="add icon"></i></div></div>'
  232. + '<div class="function_close_div">}</div>'
  233. + '</div>'
  234. + '</div>';
  235. $('.all_functions').append(appender);
  236. }
  237. // Essa função imprime os parâmetros e cria os elementos para a sua manipulação
  238. function renderFunctionParameters(function_obj, sequence) {
  239. var ret = "";
  240. if (function_obj.lista_parametros != null) {
  241. for (var j = 0; j < function_obj.lista_parametros.length; j++) {
  242. var par_temp = function_obj.lista_parametros[j];
  243. ret += '<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>';
  244. ret += '<div class="ui dropdown parameter_type seq_'+j+' fun_'+sequence+'">';
  245. if (par_temp.dimensoes > 0) {
  246. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+ i18n(tiposDados.vector)+':'+i18n(par_temp.tipo);
  247. ret += '</div>';
  248. } else {
  249. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+i18n(par_temp.tipo)+'</div>';
  250. }
  251. ret += '<i class="dropdown icon"></i>'
  252. + '<div class="menu seq_'+j+' fun_'+sequence+'">';
  253. var i = 0;
  254. for (tm in tiposDados) {
  255. i ++;
  256. if (i == 1) { continue; }
  257. if (i == (Object.keys(tiposDados).length - 1)) { break; }
  258. ret += '<div class="item ' + ((par_temp.tipo == tm && par_temp.dimensoes < 1) ? ' selected ' : '') + ' seq_'+j+' fun_'+sequence+' '+tm+'" >'+i18n(tm)+'</div>';
  259. }
  260. i = 0;
  261. for (tm in tiposDados) {
  262. i ++;
  263. if (i == 1) { continue; }
  264. if (i == (Object.keys(tiposDados).length)) { break; }
  265. ret += '<div class="item seq_'+j+' '+tm+' fun_'+sequence+' ' + ((par_temp.tipo == tm && par_temp.dimensoes > 0) ? ' selected ' : '') + ' ">'
  266. + i18n(tiposDados.vector)+':'+i18n(tm)
  267. + '</div>';
  268. }
  269. ret += '</div></div>';
  270. ret += ' <i class="red icon times remove_parameter" onclick="removeParameter(this.parentNode, '+sequence+', '+j+')"></i></div>';
  271. }
  272. }
  273. return ret;
  274. }
  275. function renderVariables(function_obj, sequence) {
  276. var ret = "";
  277. if (function_obj.lista_parametros != null) {
  278. for (var j = 0; j < function_obj.lista_parametros.length; j++) {
  279. var par_temp = function_obj.lista_parametros[j];
  280. /*ret += '<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>';
  281. ret += '<select class="ui fluid dropdown parameter_data_types_dropdown" onchange="updateParameterType('+sequence+', '+j+', this.value)">'
  282. + '<option value="'+tiposDados.integer+'" '+(par_temp.tipo == tiposDados.integer ? 'selected' : '')+'>'+i18n(tiposDados.integer)+'</option>'
  283. + '<option value="'+tiposDados.real+'" '+(par_temp.tipo == tiposDados.real ? 'selected' : '')+'>'+i18n(tiposDados.real)+'</option>'
  284. + '<option value="'+tiposDados.text+'" '+(par_temp.tipo == tiposDados.text ? 'selected' : '')+'>'+i18n(tiposDados.text)+'</option>'
  285. + '<option value="'+tiposDados.boolean+'" '+(par_temp.tipo == tiposDados.boolean ? 'selected' : '')+'>'+i18n(tiposDados.boolean)+'</option>'
  286. + '</select>';
  287. ret += ' <i class="red icon times remove_parameter" onclick="removeParameter(this.parentNode, '+sequence+', '+j+')"></i></div>';*/
  288. ret += '<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>';
  289. ret += '<div class="ui dropdown parameter_type seq_'+j+' fun_'+sequence+'">';
  290. if (par_temp.dimensoes > 0) {
  291. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+ i18n(tiposDados.vector)+':'+i18n(par_temp.tipo);
  292. for (i = 0; i < par_temp.dimensoes; i ++) {
  293. ret += ' [ ] ';
  294. }
  295. ret += '</div>';
  296. } else {
  297. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+i18n(par_temp.tipo)+'</div>';
  298. }
  299. ret += '<i class="dropdown icon"></i>'
  300. + '<div class="menu seq_'+j+' fun_'+sequence+'">';
  301. var i = 0;
  302. for (tm in tiposDados) {
  303. i ++;
  304. if (i == 1) { continue; }
  305. if (i == (Object.keys(tiposDados).length - 1)) { break; }
  306. ret += '<div class="item ' + (par_temp.tipo == tm ? ' selected ' : '') + ' seq_'+j+' fun_'+sequence+' '+tm+'" >'+i18n(tm)+'</div>';
  307. }
  308. i = 0;
  309. for (tm in tiposDados) {
  310. i ++;
  311. if (i == 1) { continue; }
  312. if (i == (Object.keys(tiposDados).length)) { break; }
  313. ret += '<div class="item seq_'+j+' fun_'+sequence+'">'
  314. + '<i class="dropdown icon"></i>'
  315. + i18n(tiposDados.vector)+':'+i18n(tm)
  316. + '<div class="menu seq_'+j+' fun_'+sequence+'">'
  317. + '<div class="item seq_'+j+' fun_'+sequence+' '+tm+'" data-text="'+ i18n(tiposDados.vector)+':'+i18n(tm)+' [ ] ">[ ]</div>'
  318. + '<div class="item seq_'+j+' fun_'+sequence+' '+tm+'" data-text="'+ i18n(tiposDados.vector)+':'+i18n(tm)+' [ ] [ ] ">[ ] [ ] </div>'
  319. + '<div class="item seq_'+j+' fun_'+sequence+' '+tm+'" data-text="'+ i18n(tiposDados.vector)+':'+i18n(tm)+' [ ] [ ] [ ]">[ ] [ ] [ ] </div>'
  320. + '</div>'
  321. + '</div>';
  322. }
  323. ret += '</div></div>';
  324. ret += ' <i class="red icon times remove_parameter" onclick="removeParameter(this.parentNode, '+sequence+', '+j+')"></i></div>';
  325. }
  326. }
  327. return ret;
  328. }
  329. // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
  330. function renderFunctionReturn(function_obj, sequence) {
  331. var ret = '<div class="ui dropdown function_return seq_'+sequence+'">';
  332. if (function_obj.dimensoes_retorno > 0) {
  333. ret += '<div class="text seq_'+sequence+'"">'+ i18n(tiposDados.vector)+':'+i18n(function_obj.tipo_retorno);
  334. ret += '</div>';
  335. } else {
  336. ret += '<div class="text seq_'+sequence+'"">'+i18n(function_obj.tipo_retorno)+'</div>';
  337. }
  338. ret += '<i class="dropdown icon"></i>'
  339. + '<div class="menu seq_'+sequence+'"">';
  340. var i = 0;
  341. for (tm in tiposDados) {
  342. if (i == (Object.keys(tiposDados).length - 1)) { break; }
  343. ret += '<div class="item ' + ((function_obj.tipo_retorno == tm && function_obj.dimensoes_retorno < 1) ? ' selected ' : '') + ' seq_'+sequence+' '+tm+'" >'+i18n(tm)+'</div>';
  344. i ++;
  345. }
  346. i = 0;
  347. for (tm in tiposDados) {
  348. i ++;
  349. if (i == 1) { continue; }
  350. if (i == (Object.keys(tiposDados).length)) { break; }
  351. ret += '<div class="item seq_'+sequence+' '+tm+' '+ ((function_obj.tipo_retorno == tm && function_obj.dimensoes_retorno > 0) ? ' selected ' : '') +'" data-text="'+i18n(tiposDados.vector)+':'+i18n(tm)+' ">'
  352. + i18n(tiposDados.vector)+':'+i18n(tm)
  353. + '</div>';
  354. }
  355. ret += '</div></div>';
  356. return ret;
  357. }
  358. $.fn.textWidth = function(text, font) {
  359. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  360. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  361. return $.fn.textWidth.fakeEl.width();
  362. };