ivprog-visual-functions-1.0.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. var counter_new_functions = 0;
  2. var counter_new_parameters = 0;
  3. var counter_new_variables = 0;
  4. function addFunctionHandler() {
  5. new_function = new Funcao(i18n("new_function") + "_" + counter_new_functions);
  6. adicionarFuncao(new_function);
  7. counter_new_functions ++;
  8. renderAlgorithm();
  9. }
  10. function updateSequenceFunctionHandler(index_from, index_to) {
  11. programa.funcoes.splice(index_to, 0, programa.funcoes.splice(index_from, 1)[0]);
  12. renderAlgorithm();
  13. }
  14. function removeFunctionHandler(div_function, sequence) {
  15. programa.funcoes.splice(sequence, 1);
  16. $(div_function).slideUp(400, function(){
  17. renderAlgorithm();
  18. });
  19. }
  20. function minimizeFunctionHandler(div_function, sequence) {
  21. $(div_function).find(".function_area").toggle();
  22. programa.funcoes[sequence].esta_oculta = !programa.funcoes[sequence].esta_oculta;
  23. }
  24. function renderAlgorithm() {
  25. $('.all_functions').empty();
  26. for (i = 0; i < programa.funcoes.length; i++) {
  27. appendFunction(programa.funcoes[i], i);
  28. }
  29. $('.data_types_dropdown').dropdown();
  30. $('.parameter_data_types_dropdown').dropdown();
  31. addHandlers();
  32. }
  33. function addHandlers() {
  34. $('.ui.dropdown.function_return')
  35. .dropdown({
  36. onChange: function(value, text, $selectedItem) {
  37. classList = $selectedItem.attr('class').split(/\s+/);
  38. $.each(classList, function(index, item) {
  39. if (item.indexOf("seq_") > -1) {
  40. seq = item.split("seq_")[1];
  41. for (tm in tiposDados) {
  42. if ($selectedItem.hasClass(tm)) {
  43. programa.funcoes[seq].tipo_retorno = tm;
  44. }
  45. }
  46. updateFunctionReturn(seq, value);
  47. }
  48. });
  49. }
  50. })
  51. ;
  52. $('.ui.dropdown.parameter_type').dropdown({
  53. onChange: function(value, text, $selectedItem) {
  54. classList = $selectedItem.attr('class').split(/\s+/);
  55. var fun;
  56. var seq;
  57. $.each(classList, function(index, item) {
  58. if (item.indexOf("fun_") > -1) {
  59. fun = item.split("fun_")[1];
  60. }
  61. if (item.indexOf("seq_") > -1) {
  62. seq = item.split("seq_")[1];
  63. }
  64. });
  65. var dim = 0;
  66. if (value.indexOf(i18n(tiposDados.vector)) > -1) {
  67. dim = 1;
  68. }
  69. for (tm in tiposDados) {
  70. if ($selectedItem.hasClass(tm)) {
  71. updateParameterType(fun, seq, tm, dim);
  72. break;
  73. }
  74. }
  75. }
  76. });
  77. $('.ui.dropdown.variable_type').dropdown({
  78. onChange: function(value, text, $selectedItem) {
  79. classList = $selectedItem.attr('class').split(/\s+/);
  80. var fun;
  81. var seq;
  82. $.each(classList, function(index, item) {
  83. if (item.indexOf("fun_") > -1) {
  84. fun = item.split("fun_")[1];
  85. }
  86. if (item.indexOf("seq_") > -1) {
  87. seq = item.split("seq_")[1];
  88. }
  89. });
  90. var dim = 0;
  91. if (value.indexOf(i18n(tiposDados.vector)) > -1) {
  92. dim = value.split('[').length - 1;
  93. }
  94. for (tm in tiposDados) {
  95. if ($selectedItem.hasClass(tm)) {
  96. console.log("possui: " + tm);
  97. updateVariableType(fun, seq, tm, dim);
  98. break;
  99. }
  100. }
  101. }
  102. });
  103. }
  104. function updateVariableType(wich_function, wich_variable, new_value, new_dimensions) {
  105. programa.funcoes[wich_function].variaveis[wich_variable].tipo = new_value;
  106. programa.funcoes[wich_function].variaveis[wich_variable].dimensoes = new_dimensions;
  107. }
  108. function addVariable(sequence) {//tipo, nome, valor
  109. var v = new Variavel(tiposDados.integer, i18n('new_variable') + '_' + counter_new_variables, 1);
  110. adicionarVariavel(sequence, v);
  111. counter_new_variables ++;
  112. renderAlgorithm();
  113. }
  114. function deleteVariable(which_function, which_variable) {
  115. programa.funcoes[which_function].variaveis.splice(which_variable, 1);
  116. renderAlgorithm();
  117. }
  118. function addParameter(sequence) {
  119. if (programa.funcoes[sequence].lista_parametros == null) {
  120. programa.funcoes[sequence].lista_parametros = new Array();
  121. }
  122. programa.funcoes[sequence].lista_parametros.push(new Variavel(tiposDados.integer, i18n("new_parameter") + "_" + counter_new_parameters));
  123. counter_new_parameters ++;
  124. renderAlgorithm();
  125. }
  126. function updateFunctionReturn(sequence, new_value) {
  127. if (new_value.indexOf(i18n(tiposDados.vector)) > -1) {
  128. programa.funcoes[sequence].dimensoes_retorno = 1;
  129. } else {
  130. programa.funcoes[sequence].dimensoes_retorno = 0;
  131. }
  132. }
  133. function updateParameterType(wich_function, wich_parameter, new_value, new_dimensions) {
  134. programa.funcoes[wich_function].lista_parametros[wich_parameter].tipo = new_value;
  135. programa.funcoes[wich_function].lista_parametros[wich_parameter].dimensoes = new_dimensions;
  136. }
  137. var opened_name_function = false;
  138. var opened_input = null;
  139. var sequence_name_opened;
  140. function enableNameFunctionUpdate(div_el, sequence) {
  141. if (opened_name_function) {
  142. $(opened_input).focus();
  143. return;
  144. }
  145. opened_name_function = true;
  146. sequence_name_opened = sequence;
  147. $(div_el).find('.span_name_function').text('');
  148. $( "<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'));
  149. $('.width-dynamic').on('input', function() {
  150. var inputWidth = $(this).textWidth()+10;
  151. opened_input = this;
  152. $(this).focus();
  153. var tmpStr = $(this).val();
  154. $(this).val('');
  155. $(this).val(tmpStr);
  156. $(this).css({
  157. width: inputWidth
  158. })
  159. }).trigger('input');
  160. $('.width-dynamic').focusout(function() {
  161. /// update array:
  162. if ($(this).val().trim()) {
  163. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  164. }
  165. $(this).remove();
  166. /// update elements:
  167. opened_name_function = false;
  168. opened_input = false;
  169. renderAlgorithm();
  170. });
  171. $('.width-dynamic').on('keydown', function(e) {
  172. var code = e.keyCode || e.which;
  173. if(code == 13) {
  174. if ($(this).val().trim()) {
  175. programa.funcoes[sequence_name_opened].nome = $(this).val().trim();
  176. }
  177. $(this).remove();
  178. /// update elements:
  179. opened_name_function = false;
  180. opened_input = false;
  181. renderAlgorithm();
  182. }
  183. if(code == 27) {
  184. $(div_el).find('.span_name_function').text(programa.funcoes[sequence_name_opened].nome);
  185. $(this).remove();
  186. /// update elements:
  187. opened_name_function = false;
  188. opened_input = false;
  189. }
  190. });
  191. }
  192. var opened_name_variable = false;
  193. var opened_input_variable = null;
  194. var sequence_name_opened_variable;
  195. var sequence_function_opened_variable;
  196. function enableNameVariableUpdate(parent_node, which_function, which_parameter) {
  197. if (opened_name_variable) {
  198. $(opened_input_variable).focus();
  199. return;
  200. }
  201. opened_name_variable = true;
  202. sequence_name_opened_variable = which_parameter;
  203. sequence_function_opened_variable = which_function;
  204. $(parent_node).find('.span_name_variable').text('');
  205. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  206. + programa.funcoes[which_function].variaveis[which_parameter].nome + "' />" ).insertBefore($(parent_node).find('.span_name_variable'));
  207. $('.width-dynamic').on('input', function() {
  208. var inputWidth = $(this).textWidth()+10;
  209. opened_input_variable = this;
  210. $(this).focus();
  211. var tmpStr = $(this).val();
  212. $(this).val('');
  213. $(this).val(tmpStr);
  214. $(this).css({
  215. width: inputWidth
  216. })
  217. }).trigger('input');
  218. $('.width-dynamic').focusout(function() {
  219. /// update array:
  220. if ($(this).val().trim()) {
  221. programa.funcoes[which_function].variaveis[which_parameter].nome = $(this).val().trim();
  222. }
  223. $(this).remove();
  224. /// update elements:
  225. opened_name_variable = false;
  226. opened_input_variable = false;
  227. renderAlgorithm();
  228. });
  229. $('.width-dynamic').on('keydown', function(e) {
  230. var code = e.keyCode || e.which;
  231. if(code == 13) {
  232. if ($(this).val().trim()) {
  233. programa.funcoes[which_function].variaveis[which_parameter].nome = $(this).val().trim();
  234. }
  235. $(this).remove();
  236. /// update elements:
  237. opened_name_variable = false;
  238. opened_input_variable = false;
  239. renderAlgorithm();
  240. }
  241. if(code == 27) {
  242. $(parent_node).find('.span_name_variable').text(programa.funcoes[which_function].variaveis[which_parameter].nome);
  243. $(this).remove();
  244. /// update elements:
  245. opened_name_variable = false;
  246. opened_input_variable = false;
  247. }
  248. });
  249. }
  250. var opened_name_parameter = false;
  251. var opened_input_parameter = null;
  252. var sequence_name_opened_parameter;
  253. var sequence_function_opened_parameter;
  254. function enableNameParameterUpdate(parent_node, which_function, which_parameter) {
  255. if (opened_name_parameter) {
  256. $(opened_input_parameter).focus();
  257. return;
  258. }
  259. opened_name_parameter = true;
  260. sequence_name_opened_parameter = which_parameter;
  261. sequence_function_opened_parameter = which_function;
  262. $(parent_node).find('.span_name_parameter').text('');
  263. $( "<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'));
  264. $('.width-dynamic').on('input', function() {
  265. var inputWidth = $(this).textWidth()+10;
  266. opened_input_parameter = this;
  267. $(this).focus();
  268. var tmpStr = $(this).val();
  269. $(this).val('');
  270. $(this).val(tmpStr);
  271. $(this).css({
  272. width: inputWidth
  273. })
  274. }).trigger('input');
  275. $('.width-dynamic').focusout(function() {
  276. /// update array:
  277. if ($(this).val().trim()) {
  278. programa.funcoes[which_function].lista_parametros[which_parameter].nome = $(this).val().trim();
  279. }
  280. $(this).remove();
  281. /// update elements:
  282. opened_name_parameter = false;
  283. opened_input_parameter = false;
  284. renderAlgorithm();
  285. });
  286. $('.width-dynamic').on('keydown', function(e) {
  287. var code = e.keyCode || e.which;
  288. if(code == 13) {
  289. if ($(this).val().trim()) {
  290. programa.funcoes[which_function].lista_parametros[which_parameter].nome = $(this).val().trim();
  291. }
  292. $(this).remove();
  293. /// update elements:
  294. opened_name_parameter = false;
  295. opened_input_parameter = false;
  296. renderAlgorithm();
  297. }
  298. if(code == 27) {
  299. $(parent_node).find('.span_name_parameter').text(programa.funcoes[which_function].lista_parametros[which_parameter].nome);
  300. $(this).remove();
  301. /// update elements:
  302. opened_name_parameter = false;
  303. opened_input_parameter = false;
  304. }
  305. });
  306. }
  307. function removeParameter(parent_node, which_function, which_parameter) {
  308. programa.funcoes[which_function].lista_parametros.splice(which_parameter, 1);
  309. renderAlgorithm();
  310. }
  311. function appendFunction(function_obj, sequence) {
  312. var appender = '<div class="ui secondary segment function_div list-group-item">'
  313. + '<span class="glyphicon glyphicon-move" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>'
  314. + (!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>' : '')
  315. + '<button class="ui icon button tiny minimize_function_button" onclick="minimizeFunctionHandler(this.parentNode, '+sequence+')"><i class="icon window minimize"></i></button>'
  316. + '<div class="function_signature_div">'+i18n('function')+' ';
  317. if (function_obj.eh_principal) {
  318. 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> '
  319. + '( <div class="ui large labels parameters_list">';
  320. } else {
  321. appender += renderFunctionReturn(function_obj, sequence);
  322. 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> '
  323. + '( <i class="ui icon plus square outline add_parameter" onclick="addParameter('+sequence+')"></i> <div class="ui large labels parameters_list">';
  324. }
  325. appender += renderFunctionParameters(function_obj, sequence);
  326. appender += '</div> ) {</div>'
  327. + (function_obj.esta_oculta ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ')
  328. + '<div class="ui top attached segment variables_list_div"><div class="ui teal small labeled icon button add_variable_button" onclick="addVariable('+sequence+')">'+i18n('Variable')+'<i class="add icon"></i></div>'
  329. + renderVariables(function_obj, sequence)
  330. + '</div>'
  331. + '<div class="ui bottom attached segment commands_list_div"><div class="ui teal small labeled icon button add_command_button seq_'+sequence+'">'+i18n('Command')+'<i class="add icon"></i></div></div>'
  332. + '<div class="function_close_div">}</div>'
  333. + '</div>'
  334. + '</div>';
  335. $('.all_functions').append(appender);
  336. }
  337. // Essa função imprime os parâmetros e cria os elementos para a sua manipulação
  338. function renderFunctionParameters(function_obj, sequence) {
  339. var ret = "";
  340. if (function_obj.lista_parametros != null) {
  341. for (var j = 0; j < function_obj.lista_parametros.length; j++) {
  342. var par_temp = function_obj.lista_parametros[j];
  343. 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>';
  344. ret += '<div class="ui dropdown parameter_type seq_'+j+' fun_'+sequence+'">';
  345. if (par_temp.dimensoes > 0) {
  346. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+ i18n(tiposDados.vector)+':'+i18n(par_temp.tipo);
  347. ret += '</div>';
  348. } else {
  349. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+i18n(par_temp.tipo)+'</div>';
  350. }
  351. ret += '<i class="dropdown icon"></i>'
  352. + '<div class="menu seq_'+j+' fun_'+sequence+'">';
  353. var i = 0;
  354. for (tm in tiposDados) {
  355. i ++;
  356. if (i == 1) { continue; }
  357. if (i == (Object.keys(tiposDados).length - 1)) { break; }
  358. ret += '<div class="item ' + ((par_temp.tipo == tm && par_temp.dimensoes < 1) ? ' selected ' : '') + ' seq_'+j+' fun_'+sequence+' '+tm+'" >'+i18n(tm)+'</div>';
  359. }
  360. i = 0;
  361. for (tm in tiposDados) {
  362. i ++;
  363. if (i == 1) { continue; }
  364. if (i == (Object.keys(tiposDados).length)) { break; }
  365. ret += '<div class="item seq_'+j+' '+tm+' fun_'+sequence+' ' + ((par_temp.tipo == tm && par_temp.dimensoes > 0) ? ' selected ' : '') + ' ">'
  366. + i18n(tiposDados.vector)+':'+i18n(tm)
  367. + '</div>';
  368. }
  369. ret += '</div></div>';
  370. ret += ' <i class="red icon times remove_parameter" onclick="removeParameter(this.parentNode, '+sequence+', '+j+')"></i></div>';
  371. }
  372. }
  373. return ret;
  374. }
  375. // Essa função imprime as variáveis e os recursos para sua manipulação
  376. function renderVariables(function_obj, sequence) {
  377. var ret = "";
  378. if (function_obj.variaveis != null) {
  379. for (var j = 0; j < function_obj.variaveis.length; j++) {
  380. var par_temp = function_obj.variaveis[j];
  381. ret += '<div class="ui label name_variable"><span class="span_name_variable" ondblclick="enableNameVariableUpdate(this.parentNode, '+sequence+', '+j+')">'+par_temp.nome+'</span> <i class="icon small pencil alternate enable_edit_name_parameter" onclick="enableNameVariableUpdate(this.parentNode, '+sequence+', '+j+')"></i>';
  382. ret += '<div class="ui dropdown variable_type seq_'+j+' fun_'+sequence+'">';
  383. if (par_temp.dimensoes > 0) {
  384. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+ i18n(tiposDados.vector)+':'+i18n(par_temp.tipo);
  385. for (i = 0; i < par_temp.dimensoes; i ++) {
  386. ret += ' [ ] ';
  387. }
  388. ret += '</div>';
  389. } else {
  390. ret += '<div class="text seq_'+j+' fun_'+sequence+'">'+i18n(par_temp.tipo)+'</div>';
  391. }
  392. ret += '<i class="dropdown icon"></i>'
  393. + '<div class="menu seq_'+j+' fun_'+sequence+'">';
  394. var i = 0;
  395. for (tm in tiposDados) {
  396. i ++;
  397. if (i == 1) { continue; }
  398. if (i == (Object.keys(tiposDados).length - 1)) { break; }
  399. ret += '<div class="item ' + (par_temp.tipo == tm ? ' selected ' : '') + ' seq_'+j+' fun_'+sequence+' '+tm+'" >'+i18n(tm)+'</div>';
  400. }
  401. i = 0;
  402. for (tm in tiposDados) {
  403. i ++;
  404. if (i == 1) { continue; }
  405. if (i == (Object.keys(tiposDados).length)) { break; }
  406. ret += '<div class="item seq_'+j+' fun_'+sequence+'">'
  407. + '<i class="dropdown icon"></i>'
  408. + i18n(tiposDados.vector)+':'+i18n(tm)
  409. + '<div class="menu seq_'+j+' fun_'+sequence+'">'
  410. + '<div class="item seq_'+j+' fun_'+sequence+' '+tm+'" data-text="'+ i18n(tiposDados.vector)+':'+i18n(tm)+' [ ] ">[ ]</div>'
  411. + '<div class="item seq_'+j+' fun_'+sequence+' '+tm+'" data-text="'+ i18n(tiposDados.vector)+':'+i18n(tm)+' [ ] [ ] ">[ ] [ ] </div>'
  412. + '</div>'
  413. + '</div>';
  414. }
  415. ret += '</div></div>';
  416. ret += ' = ' + par_temp.valor + ' <i class="red icon times remove_parameter" onclick="deleteVariable('+sequence+', '+j+')"></i></div>';
  417. }
  418. }
  419. return ret;
  420. }
  421. // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
  422. function renderFunctionReturn(function_obj, sequence) {
  423. var ret = '<div class="ui dropdown function_return seq_'+sequence+'">';
  424. if (function_obj.dimensoes_retorno > 0) {
  425. ret += '<div class="text seq_'+sequence+'"">'+ i18n(tiposDados.vector)+':'+i18n(function_obj.tipo_retorno);
  426. ret += '</div>';
  427. } else {
  428. ret += '<div class="text seq_'+sequence+'"">'+i18n(function_obj.tipo_retorno)+'</div>';
  429. }
  430. ret += '<i class="dropdown icon"></i>'
  431. + '<div class="menu seq_'+sequence+'"">';
  432. var i = 0;
  433. for (tm in tiposDados) {
  434. if (i == (Object.keys(tiposDados).length - 1)) { break; }
  435. ret += '<div class="item ' + ((function_obj.tipo_retorno == tm && function_obj.dimensoes_retorno < 1) ? ' selected ' : '') + ' seq_'+sequence+' '+tm+'" >'+i18n(tm)+'</div>';
  436. i ++;
  437. }
  438. i = 0;
  439. for (tm in tiposDados) {
  440. i ++;
  441. if (i == 1) { continue; }
  442. if (i == (Object.keys(tiposDados).length)) { break; }
  443. 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)+' ">'
  444. + i18n(tiposDados.vector)+':'+i18n(tm)
  445. + '</div>';
  446. }
  447. ret += '</div></div>';
  448. return ret;
  449. }
  450. $.fn.textWidth = function(text, font) {
  451. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  452. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  453. return $.fn.textWidth.fakeEl.width();
  454. };