functions_sidebar.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. import $ from 'jquery';
  2. import { Types } from './types';
  3. import * as Models from './ivprog_elements_sidebar';
  4. import { LocalizedStrings } from './../services/localizedStringsService';
  5. import * as GlobalsManagement from './globals_sidebar';
  6. import * as VariablesManagement from './variables';
  7. import * as CommandsManagement from './commands_sidebar';
  8. import * as CodeManagement from './code_generator';
  9. import * as VariableValueMenu from './commands/variable_value_menu';
  10. import { DOMConsole } from './../io/domConsole';
  11. import { IVProgParser } from './../ast/ivprogParser';
  12. import { IVProgProcessor } from './../processor/ivprogProcessor';
  13. import WatchJS from 'melanke-watchjs';
  14. import { SemanticAnalyser } from '../processor/semantic/semanticAnalyser';
  15. import { IVProgAssessment } from '../assessment/ivprogAssessment';
  16. import * as AlgorithmManagement from './algorithm_sidebar';
  17. import * as Utils from './utils';
  18. import '../Sortable.js';
  19. var counter_new_functions = 0;
  20. var counter_new_parameters = 0;
  21. let studentTemp = null;
  22. let domConsole = null;
  23. window.studentGrade = null;
  24. window.LocalizedStrings = LocalizedStrings;
  25. const program = new Models.Program();
  26. window.system_functions = [];
  27. // Adding math functions:
  28. window.system_functions.push(new Models.SystemFunction('$sin', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  29. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  30. window.system_functions.push(new Models.SystemFunction('$cos', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  31. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  32. window.system_functions.push(new Models.SystemFunction('$tan', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  33. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  34. window.system_functions.push(new Models.SystemFunction('$sqrt', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  35. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  36. window.system_functions.push(new Models.SystemFunction('$pow', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true), new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  37. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  38. window.system_functions.push(new Models.SystemFunction('$log', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  39. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  40. window.system_functions.push(new Models.SystemFunction('$abs', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  41. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  42. window.system_functions.push(new Models.SystemFunction('$negate', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  43. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  44. window.system_functions.push(new Models.SystemFunction('$invert', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  45. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  46. window.system_functions.push(new Models.SystemFunction('$max', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  47. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  48. window.system_functions.push(new Models.SystemFunction('$min', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  49. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  50. // Adding text functions:
  51. window.system_functions.push(new Models.SystemFunction('$substring', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  52. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  53. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  54. window.system_functions.push(new Models.SystemFunction('$length', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  55. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  56. window.system_functions.push(new Models.SystemFunction('$uppercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  57. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  58. window.system_functions.push(new Models.SystemFunction('$lowercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  59. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  60. window.system_functions.push(new Models.SystemFunction('$charAt', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true), new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  61. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  62. // Adding arrangement functions:
  63. window.system_functions.push(new Models.SystemFunction('$numElements', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 1)],
  64. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  65. window.system_functions.push(new Models.SystemFunction('$matrixLines', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 2)],
  66. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  67. window.system_functions.push(new Models.SystemFunction('$matrixColumns', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 2)],
  68. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  69. // Adding conversion functions:
  70. window.system_functions.push(new Models.SystemFunction('$isReal', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  71. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  72. window.system_functions.push(new Models.SystemFunction('$isInt', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  73. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  74. window.system_functions.push(new Models.SystemFunction('$isBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  75. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  76. window.system_functions.push(new Models.SystemFunction('$castReal', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  77. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  78. window.system_functions.push(new Models.SystemFunction('$castInt', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  79. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  80. window.system_functions.push(new Models.SystemFunction('$castBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  81. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  82. window.system_functions.push(new Models.SystemFunction('$castString', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  83. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  84. console.log(' ___ ___ ________ \n / / / / / ____/ \n / / / / / / \n / / / / ______ ___ / /__ \n / / / / / \\ / / / ___/ \n / /______ / / / /\\ \\/ / / / \n / / / / / / \\ / / /____ \n/__________/ /___/ /___/ \\___/ /________/ \n\n Laboratório de Informática na Educação\n http://line.ime.usp.br');
  85. const mainFunction = new Models.Function(LocalizedStrings.getUI("start"), Types.VOID, 0, [], true, false);
  86. mainFunction.function_comment = new Models.Comment(LocalizedStrings.getUI('text_comment_main'));
  87. program.addFunction(mainFunction);
  88. window.program_obj = program;
  89. window.generator = CodeManagement.generate;
  90. window.runCodeAssessment = runCodeAssessment;
  91. window.renderAlgorithm = AlgorithmManagement.renderAlgorithm;
  92. window.insertContext = false;
  93. window.watchW = WatchJS;
  94. WatchJS.watch(window.program_obj.globals, function(){
  95. if (window.insertContext) {
  96. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  97. window.insertContext = false;
  98. } else {
  99. AlgorithmManagement.renderAlgorithm();
  100. }
  101. }, 1);
  102. WatchJS.watch(window.program_obj.functions, function(){
  103. if (window.insertContext) {
  104. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  105. window.insertContext = false;
  106. } else {
  107. AlgorithmManagement.renderAlgorithm();
  108. }
  109. }, 0);
  110. function addFunctionHandler () {
  111. var new_function = new Models.Function(LocalizedStrings.getUI("new_function") + "_" + counter_new_functions, Types.VOID, 0, [], false, false, [], new Models.Comment(LocalizedStrings.getUI('text_comment_start')));
  112. program.addFunction(new_function);
  113. counter_new_functions ++;
  114. window.insertContext = true;
  115. var newe = renderFunction(new_function);
  116. newe.css('display', 'none');
  117. newe.fadeIn();
  118. }
  119. function addParameter (function_obj, function_container, is_from_click = false) {
  120. if (function_obj.parameters_list == null) {
  121. function_obj.parameters_list = [];
  122. }
  123. var new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
  124. function_obj.parameters_list.push(new_parameter);
  125. counter_new_parameters ++;
  126. var newe = renderParameter(function_obj, new_parameter, function_container);
  127. if (is_from_click) {
  128. newe.css('display', 'none');
  129. newe.fadeIn();
  130. }
  131. }
  132. function updateReturnType (function_obj, new_type, new_dimensions = 0) {
  133. function_obj.return_type = new_type;
  134. function_obj.return_dimensions = new_dimensions;
  135. }
  136. function removeFunction (function_obj) {
  137. var index = program.functions.indexOf(function_obj);
  138. if (index > -1) {
  139. program.functions.splice(index, 1);
  140. }
  141. }
  142. function minimizeFunction (function_obj) {
  143. function_obj.is_hidden = !function_obj.is_hidden;
  144. }
  145. function addHandlers (function_obj, function_container) {
  146. function_container.find('.ui.dropdown.function_return').dropdown({
  147. onChange: function(value, text, $selectedItem) {
  148. if ($selectedItem.data('dimensions')) {
  149. updateReturnType(function_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  150. } else {
  151. updateReturnType(function_obj, Types[$selectedItem.data('type')]);
  152. }
  153. },
  154. selectOnKeydown: false
  155. });
  156. function_container.find( ".name_function_updated" ).on('click', function(e){
  157. enableNameFunctionUpdate(function_obj, function_container);
  158. });
  159. function_container.find( ".add_parameter_button" ).on('click', function(e){
  160. window.insertContext = true;
  161. addParameter(function_obj, function_container, true);
  162. });
  163. function_container.find('.menu_commands').dropdown({
  164. on: 'hover'
  165. });
  166. function_container.find('.menu_commands a').on('click', function(evt){
  167. if (function_obj.commands == null || function_obj.commands.length == 0) {
  168. function_obj.commands = [];
  169. var new_cmd = CommandsManagement.genericCreateCommand($(this).data('command'));
  170. function_obj.commands.push(new_cmd);
  171. CommandsManagement.renderCommand(new_cmd, function_container.find('.commands_list_div'), 3, function_obj);
  172. } else {
  173. CommandsManagement.createFloatingCommand(function_obj, function_container, $(this).data('command'), evt);
  174. }
  175. });
  176. function_container.find('.add_var_button_function').on('click', function(e){
  177. window.insertContext = true;
  178. VariablesManagement.addVariable(function_obj, function_container, true);
  179. });
  180. function_container.find('.remove_function_button').on('click', function(e){
  181. removeFunction(function_obj);
  182. function_container.fadeOut();
  183. });
  184. function_container.find('.minimize_function_button').on('click', function(e){
  185. minimizeFunction(function_obj);
  186. if (function_obj.is_hidden) {
  187. function_container.find(".add_var_button_function").toggle();
  188. function_container.find(".inline_add_command").toggle();
  189. function_container.find(".function_area").slideToggle();
  190. } else {
  191. function_container.find(".function_area").slideToggle(function(){
  192. function_container.find(".add_var_button_function").toggle();
  193. function_container.find(".inline_add_command").toggle();
  194. });
  195. }
  196. });
  197. }
  198. // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
  199. function renderFunctionReturn (function_obj, function_element) {
  200. var ret = '<div class="ui dropdown function_return">';
  201. if (function_obj.return_dimensions > 0) {
  202. ret += '<div class="text">'+ LocalizedStrings.getUI("vector") +':'+ LocalizedStrings.getUI(function_obj.return_type);
  203. if (function_obj.return_dimensions == 1) {
  204. ret += ' [ ] ';
  205. } else {
  206. ret += ' [ ] [ ] ';
  207. }
  208. ret += '</div>';
  209. } else {
  210. ret += '<div class="text">'+LocalizedStrings.getUI(function_obj.return_type) + '</div>';
  211. }
  212. ret += '<div class="menu">';
  213. for (var tm in Types) {
  214. ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions < 1 ? ' selected ' : '') + '" data-type="' + tm + '" >' + LocalizedStrings.getUI(tm.toLowerCase()) + '</div>';
  215. }
  216. for (var tm in Types) {
  217. if (tm == Types.VOID.toUpperCase()) {
  218. continue;
  219. }
  220. ret += '<div class="item">'
  221. + '<i class="dropdown icon"></i>'
  222. + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase())
  223. + '<div class="menu">'
  224. + '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions > 0 ? ' selected ' : '') + '" data-text="' + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase()) + ' [ ] " data-type="' + tm + '" data-dimensions="1">[ ]</div>'
  225. + '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions > 0 ? ' selected ' : '') + '" data-text="' + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase()) + ' [ ] [ ] " data-type="' + tm + '" data-dimensions="2">[ ] [ ] </div>'
  226. + '</div>'
  227. + '</div>';
  228. }
  229. ret += '</div></div>';
  230. ret = $(ret);
  231. function_element.find('.function_return').append(ret);
  232. }
  233. export function renderFunction(function_obj) {
  234. var appender = '<div class="ui secondary segment function_div list-group-item">';
  235. if (function_obj.function_comment) {
  236. //appender += renderComment(function_obj.function_comment, sequence, true, -1);
  237. }
  238. appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
  239. appender += (function_obj.is_main ? '<div class="div_start_minimize_v"> </div>' : '<button class="ui icon button large remove_function_button"><i class="red icon times"></i></button>')
  240. + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
  241. appender += '<div class="function_signature_div">' + LocalizedStrings.getUI("function") + ' ';
  242. if (function_obj.is_main) {
  243. appender += '<div class="function_name_div"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' + LocalizedStrings.getUI('void') + ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="span_name_function" >' + function_obj.name + '</span> </div> '
  244. + ' <span class="parethesis_function">( </span> <div class="ui large labels parameters_list">';
  245. } else {
  246. appender += '<div class="ui function_return"></div>';
  247. appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">' + function_obj.name + '</span> </div> '
  248. + ' <span class="parethesis_function"> ( </span> <i class="ui icon plus square outline add_parameter_button"></i> <div class="ui large labels parameters_list container_parameters_list">';
  249. var menu_func = generateMenuButton(function_obj);
  250. /*menu_func.css("display","none")
  251. $('.functions_labels').append(menu_func);
  252. menu_func.fadeIn();*/
  253. $('.functions_labels').append(menu_func);
  254. }
  255. appender += '</div> <span class="parethesis_function"> ) </span> </div>'
  256. + (function_obj.is_hidden ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ');
  257. appender += '<div class="ui add_var_context add_var_button_function" style="float: left;"><i class="icon plus circle purple"></i><i class="icon circle white back"></i><div class="ui icon button purple"><i class="icon superscript"></i></div></div>';
  258. appender += '<div class="ui top attached segment variables_list_div"></div>';
  259. appender += '<div class="ui inline_add_command"><i class="icon plus circle purple"></i><i class="icon circle white back"></i><div class="ui icon button dropdown menu_commands orange" style="float: left;" ><i class="icon code"></i> <div class="menu"> ';
  260. appender += '<a class="item" data-command="' + Models.COMMAND_TYPES.reader + '"><i class="download icon"></i> ' + LocalizedStrings.getUI('text_read_var') + '</a>'
  261. + '<a class="item" data-command="' + Models.COMMAND_TYPES.writer + '"><i class="upload icon"></i> ' + LocalizedStrings.getUI('text_write_var') + '</a>'
  262. + '<a class="item" data-command="' + Models.COMMAND_TYPES.comment + '"><i class="quote left icon"></i> ' + LocalizedStrings.getUI('text_comment') + '</a>'
  263. + '<a class="item" data-command="' + Models.COMMAND_TYPES.attribution + '"><i class="arrow left icon"></i> ' + LocalizedStrings.getUI('text_attribution') + '</a>'
  264. + '<a class="item" data-command="' + Models.COMMAND_TYPES.functioncall + '"><i class="hand point right icon"></i> ' + LocalizedStrings.getUI('text_functioncall') + '</a>'
  265. + '<a class="item" data-command="' + Models.COMMAND_TYPES.iftrue + '" ><i class="random icon"></i> ' + LocalizedStrings.getUI('text_iftrue') + '</a>'
  266. + '<a class="item" data-command="' + Models.COMMAND_TYPES.repeatNtimes + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_repeatNtimes') + '</a>'
  267. + '<a class="item" data-command="' + Models.COMMAND_TYPES.whiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_whiletrue') + '</a>'
  268. + '<a class="item" data-command="' + Models.COMMAND_TYPES.dowhiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_dowhiletrue') + '</a>'
  269. + '<a class="item" data-command="' + Models.COMMAND_TYPES.switch + '"><i class="list icon"></i> ' + LocalizedStrings.getUI('text_switch') + '</a>'
  270. + '<a class="item" data-command="' + Models.COMMAND_TYPES.return + '"><i class="reply icon"></i> ' + LocalizedStrings.getUI('text_btn_return') + '</a>'
  271. + '</div></div></div>';
  272. appender += '<div class="ui bottom attached segment commands_list_div"></div>';
  273. appender += '</div></div>';
  274. appender = $(appender);
  275. $('.all_functions').append(appender);
  276. appender.data('fun', function_obj);
  277. appender.find('.commands_list_div')
  278. .data('fun', function_obj)
  279. .attr('droppable', true)
  280. .on('dragenter', function (e) {
  281. e.preventDefault();
  282. console.log('dragenter');
  283. console.log(e.target)
  284. $(e.target).addClass('div-over')
  285. //e.stopPropagation();
  286. }).on('dragover', function (e) {
  287. e.preventDefault();
  288. })
  289. .on('dragleave', function (e) {
  290. e.preventDefault();
  291. //e.stopPropagation();
  292. console.log("dragleave")
  293. $(e.target).removeClass('div-over')
  294. console.log(e.target)
  295. })
  296. .on('drop', function (e, bundle) {
  297. e.preventDefault();
  298. if (program_obj.dataTransfer == null)
  299. return;
  300. if (bundle) {
  301. e.clientX = bundle.clientX;
  302. e.clientY = bundle.clientY;
  303. }
  304. //console.log('ondrop ' + e.originalEvent.dataTransfer.getData("text"));
  305. console.log(e)
  306. $(e.target).removeClass('div-over')
  307. //var data = JSON.parse(e.originalEvent.dataTransfer.getData("text"));
  308. var data = program_obj.dataTransfer;
  309. if (data.type == 'command')
  310. CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, data.content);
  311. else if (data.type == 'var')
  312. CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "attribution", data.content);
  313. else {
  314. if (!data.content.parameters_list)
  315. data.content.parameters_list = [];
  316. CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "functioncall", data.content);
  317. }
  318. //program_obj.dataTransfer;
  319. program_obj.dataTransfer = null;
  320. }).on('click', function (e) {
  321. if (window.ghostNode) {
  322. console.log("drop click");
  323. $(document).off('mousemove').off('mousedown').off('keyup');
  324. $(window.ghostNode).remove();
  325. delete window.ghostNode;
  326. $(this).trigger('drop', [e]);
  327. $('.div-over').removeClass('div-over');
  328. }
  329. })
  330. .on('mouseover', function (evt) {
  331. if (window.ghostNode) {
  332. evt.type = 'dragenter';
  333. //$(this).trigger('dragenter');
  334. console.log('mouseover');
  335. $(this).trigger(evt);
  336. }
  337. })
  338. .on('mouseout', function (evt) {
  339. //$(this).trigger('dragleave');
  340. if (window.ghostNode) {
  341. evt.type = 'dragleave';
  342. console.log('mouseout');
  343. $(this).trigger(evt);
  344. }
  345. });
  346. renderFunctionReturn(function_obj, appender);
  347. addHandlers(function_obj, appender);
  348. // Rendering parameters:
  349. for (var j = 0; j < function_obj.parameters_list.length; j++) {
  350. renderParameter(function_obj, function_obj.parameters_list[j], appender);
  351. }
  352. // Rendering variables:
  353. for (var j = 0; j < function_obj.variables_list.length; j++) {
  354. VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
  355. }
  356. // Rendering commands:
  357. for (var j = 0; j < function_obj.commands.length; j++) {
  358. CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
  359. }
  360. $('.minimize_function_button').popup({
  361. content : LocalizedStrings.getUI("tooltip_minimize"),
  362. delay: {
  363. show: 750,
  364. hide: 0
  365. }
  366. });
  367. Sortable.create(appender.find(".variables_list_div")[0], {
  368. handle: '.ellipsis',
  369. animation: 100,
  370. ghostClass: 'ghost',
  371. group: 'local_vars_drag_' + program.functions.indexOf(function_obj),
  372. onEnd: function (evt) {
  373. updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  374. }
  375. });
  376. Sortable.create(appender.find(".commands_list_div")[0], {
  377. handle: '.command_drag',
  378. animation: 100,
  379. ghostClass: 'ghost',
  380. group: 'commands_drag_' + program.functions.indexOf(function_obj),
  381. onEnd: function (evt) {
  382. //updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  383. }
  384. });
  385. if (!function_obj.is_main) {
  386. Sortable.create(appender.find(".container_parameters_list")[0], {
  387. handle: '.ellipsis',
  388. animation: 100,
  389. ghostClass: 'ghost',
  390. group: 'parameters_drag_' + program.functions.indexOf(function_obj),
  391. onEnd: function (evt) {
  392. updateSequenceParameters(evt.oldIndex, evt.newIndex, function_obj);
  393. }
  394. });
  395. }
  396. return appender;
  397. }
  398. export function initVisualUI () {
  399. // MUST USE CONST, LET, OR VAR !!!!!!
  400. const mainDiv = $('#visual-main-div');
  401. // fill mainDiv with functions and globals...
  402. // renderAlgorithm()...
  403. $('.add_function_button').on('click', () => {
  404. addFunctionHandler();
  405. });
  406. $('.add_global_button').on('click', () => {
  407. window.insertContext = true;
  408. GlobalsManagement.addGlobal(program, true);
  409. });
  410. $('.run_button').on('click', () => {
  411. runCode();
  412. });
  413. $('.visual_coding_button').on('click', () => {
  414. toggleVisualCoding();
  415. });
  416. $('.textual_coding_button').on('click', () => {
  417. toggleTextualCoding();
  418. });
  419. $('.assessment').on('click', () => {
  420. runCodeAssessment();
  421. is_iassign = true;
  422. });
  423. $('.div_toggle_console').on('click', () => {
  424. toggleConsole();
  425. });
  426. $('.expand_button').on('click', () => {
  427. full_screen();
  428. });
  429. $('.main_title h2').prop('title', LocalizedStrings.getUI('text_ivprog_description'));
  430. }
  431. var is_iassign = false;
  432. $(document).ready(function () {
  433. for (var i = 0; i < program.functions.length; i++) {
  434. renderFunction(program.functions[i]);
  435. }
  436. var time_show = 750;
  437. $('.visual_coding_button').popup({
  438. content: LocalizedStrings.getUI("tooltip_visual"),
  439. delay: {
  440. show: time_show,
  441. hide: 0
  442. }
  443. });
  444. $('.textual_coding_button').popup({
  445. content: LocalizedStrings.getUI("tooltip_textual"),
  446. delay: {
  447. show: time_show,
  448. hide: 0
  449. }
  450. });
  451. $('.upload_file_button').popup({
  452. content: LocalizedStrings.getUI("tooltip_upload"),
  453. delay: {
  454. show: time_show,
  455. hide: 0
  456. }
  457. });
  458. $('.download_file_button').popup({
  459. content: LocalizedStrings.getUI("tooltip_download"),
  460. delay: {
  461. show: time_show,
  462. hide: 0
  463. }
  464. });
  465. $('.undo_button').popup({
  466. content: LocalizedStrings.getUI("tooltip_undo"),
  467. delay: {
  468. show: time_show,
  469. hide: 0
  470. }
  471. });
  472. $('.redo_button').popup({
  473. content: LocalizedStrings.getUI("tooltip_redo"),
  474. delay: {
  475. show: time_show,
  476. hide: 0
  477. }
  478. });
  479. $('.run_button').popup({
  480. content: LocalizedStrings.getUI("tooltip_run"),
  481. delay: {
  482. show: time_show,
  483. hide: 0
  484. }
  485. });
  486. $('.assessment_button').popup({
  487. content: LocalizedStrings.getUI("tooltip_evaluate"),
  488. delay: {
  489. show: time_show,
  490. hide: 0
  491. }
  492. });
  493. $('.help_button').popup({
  494. content : LocalizedStrings.getUI("tooltip_help") + ' - ' + LocalizedStrings.getUI("text_ivprog_version"),
  495. delay: {
  496. show: time_show,
  497. hide: 0
  498. }
  499. });
  500. $('.add_global_button').popup({
  501. content: LocalizedStrings.getUI("tooltip_add_global"),
  502. delay: {
  503. show: time_show,
  504. hide: 0
  505. }
  506. });
  507. $('.div_toggle_console').popup({
  508. content: LocalizedStrings.getUI("tooltip_console"),
  509. delay: {
  510. show: time_show,
  511. hide: 0
  512. }
  513. });
  514. Sortable.create(listWithHandle, {
  515. handle: '.glyphicon-move',
  516. animation: 100,
  517. ghostClass: 'ghost',
  518. group: 'functions_divs_drag',
  519. onEnd: function (evt) {
  520. updateSequenceFunction(evt.oldIndex, evt.newIndex);
  521. }
  522. });
  523. var listGlobalsHandle = document.getElementById("listGlobalsHandle");
  524. Sortable.create(listGlobalsHandle, {
  525. handle: '.ellipsis',
  526. animation: 100,
  527. ghostClass: 'ghost',
  528. group: 'globals_divs_drag',
  529. onEnd: function (evt) {
  530. updateSequenceGlobals(evt.oldIndex, evt.newIndex);
  531. }
  532. });
  533. renderAlgorithm();
  534. });
  535. function updateSequenceParameters(oldIndex, newIndex, function_obj) {
  536. function_obj.parameters_list.splice(newIndex, 0, function_obj.parameters_list.splice(oldIndex, 1)[0]);
  537. }
  538. function updateSequenceLocals(oldIndex, newIndex, function_obj) {
  539. function_obj.variables_list.splice(newIndex, 0, function_obj.variables_list.splice(oldIndex, 1)[0]);
  540. }
  541. function updateSequenceGlobals(oldIndex, newIndex) {
  542. program_obj.globals.splice(newIndex, 0, program_obj.globals.splice(oldIndex, 1)[0]);
  543. }
  544. function updateSequenceFunction(oldIndex, newIndex) {
  545. program_obj.functions.splice(newIndex, 0, program_obj.functions.splice(oldIndex, 1)[0]);
  546. }
  547. function runCodeAssessment () {
  548. window.studentGrade = null;
  549. studentTemp = null;
  550. const strCode = CodeManagement.generate();
  551. if (strCode == null) {
  552. return;
  553. }
  554. toggleConsole(true);
  555. if(domConsole == null)
  556. domConsole = new DOMConsole("#ivprog-term");
  557. $("#ivprog-term").slideDown(500);
  558. const runner = new IVProgAssessment(strCode, testCases, domConsole);
  559. runner.runTest().then(grade => {
  560. if (!is_iassign) {
  561. parent.getEvaluationCallback(grade);
  562. } else {
  563. is_iassign = false;
  564. }
  565. }).catch( err => domConsole.err(err.message));
  566. }
  567. function runCode() {
  568. const strCode = CodeManagement.generate();
  569. if (strCode == null) {
  570. return;
  571. }
  572. toggleConsole(true);
  573. if(domConsole == null)
  574. domConsole = new DOMConsole("#ivprog-term");
  575. $("#ivprog-term").slideDown(500);
  576. try {
  577. const parser = IVProgParser.createParser(strCode);
  578. const analyser = new SemanticAnalyser(parser.parseTree());
  579. const data = analyser.analyseTree();
  580. const proc = new IVProgProcessor(data);
  581. proc.registerInput(domConsole);
  582. proc.registerOutput(domConsole);
  583. $("#ivprog-term").addClass('ivprog-term-active');
  584. proc.interpretAST().then( _ => {
  585. domConsole.info("Programa executado com sucesso!");
  586. $("#ivprog-term").removeClass('ivprog-term-active');
  587. }).catch(err => {
  588. domConsole.err(err.message);
  589. $("#ivprog-term").removeClass('ivprog-term-active');
  590. })
  591. } catch (error) {
  592. domConsole.err(error.message);
  593. console.log(error);
  594. }
  595. }
  596. function toggleConsole (is_running) {
  597. if (is_running) {
  598. $('.ivprog-term-div').css('display', 'block');
  599. $('#ivprog-term').css('min-height', '160px');
  600. $('#ivprog-term').css('margin-top', '-170px');
  601. return;
  602. }
  603. if ($('#ivprog-term').css('min-height') == '160px') {
  604. // esconder
  605. $('.ivprog-term-div').css('display', 'none');
  606. $('#ivprog-term').css('min-height', '0');
  607. $('#ivprog-term').css('margin-top', '-30px');
  608. $('#ivprog-term').css('padding', '5px');
  609. } else {
  610. // mostrar
  611. $('.ivprog-term-div').css('display', 'block');
  612. $('#ivprog-term').css('min-height', '160px');
  613. $('#ivprog-term').css('margin-top', '-170px');
  614. }
  615. }
  616. function waitToCloseConsole () {
  617. domConsole.info("Aperte qualquer tecla para fechar...");
  618. const p = new Promise((resolve, _) => {
  619. domConsole.requestInput(resolve, true);
  620. });
  621. p.then( _ => {
  622. domConsole.dispose();
  623. domConsole = null;
  624. $("#ivprog-term").hide();
  625. })
  626. }
  627. function toggleTextualCoding () {
  628. var code = CodeManagement.generate();
  629. if (code == null) {
  630. return;
  631. }
  632. $('.ivprog_visual_panel').css('display', 'none');
  633. $('.ivprog_textual_panel').css('display', 'block');
  634. $('.ivprog_textual_panel').removeClass('loading');
  635. $('.ivprog_textual_code').text(code);
  636. $('.visual_coding_button').removeClass('active');
  637. $('.textual_coding_button').addClass('active');
  638. $('.tabs').addClass('loading')
  639. }
  640. function toggleVisualCoding () {
  641. $('.ivprog_textual_panel').addClass('loading');
  642. $('.ivprog_textual_panel').css('display', 'none');
  643. $('.ivprog_visual_panel').css('display', 'block');
  644. $('.textual_coding_button').removeClass('active');
  645. $('.visual_coding_button').addClass('active');
  646. $('.menu .item').tab();
  647. $('.tabs').removeClass('loading')
  648. }
  649. function removeParameter (function_obj, parameter_obj, parameter_container) {
  650. var index = function_obj.parameters_list.indexOf(parameter_obj);
  651. if (index > -1) {
  652. window.insertContext = true;
  653. function_obj.parameters_list.splice(index, 1);
  654. }
  655. $(parameter_container).fadeOut();
  656. }
  657. function updateParameterType (parameter_obj, new_type, new_dimensions = 0) {
  658. parameter_obj.type = new_type;
  659. parameter_obj.dimensions = new_dimensions;
  660. if (new_dimensions > 0) {
  661. parameter_obj.rows = new_dimensions;
  662. parameter_obj.columns = 2;
  663. }
  664. }
  665. function renderParameter (function_obj, parameter_obj, function_container) {
  666. var ret = "";
  667. ret += '<div class="ui label function_name_parameter pink"><i class="ui icon ellipsis vertical inverted"></i>';
  668. ret += '<div class="ui dropdown parameter_type">';
  669. if (parameter_obj.dimensions > 0) {
  670. ret += '<div class="text">'+ LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(parameter_obj.type);
  671. if (parameter_obj.dimensions == 1) {
  672. ret += ' [ ] ';
  673. } else {
  674. ret += ' [ ] [ ] ';
  675. }
  676. ret += '</div>';
  677. } else {
  678. ret += '<div class="text">' + LocalizedStrings.getUI(parameter_obj.type) + '</div>';
  679. }
  680. ret += '<div class="menu">';
  681. for (var tm in Types) {
  682. if (tm == Types.VOID.toUpperCase()) {
  683. continue;
  684. }
  685. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="' + tm + '" >' + LocalizedStrings.getUI(tm.toLowerCase()) + '</div>';
  686. }
  687. for (var tm in Types) {
  688. if (tm == Types.VOID.toUpperCase()) {
  689. continue;
  690. }
  691. ret += '<div class="item">'
  692. + '<i class="dropdown icon"></i>'
  693. + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase())
  694. + '<div class="menu">'
  695. + '<div class="item" data-text="' + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase()) + ' [ ] " data-type="' + tm + '" data-dimensions="1">[ ]</div>'
  696. + '<div class="item" data-text="' + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase()) + ' [ ] [ ] " data-type="' + tm + '" data-dimensions="2">[ ] [ ] </div>'
  697. + '</div>'
  698. + '</div>';
  699. }
  700. ret += '</div></div>';
  701. ret += '<div class="parameter_div_edit"><span class="span_name_parameter label_enable_name_parameter">' + parameter_obj.name + '</span></div> ';
  702. ret += ' <i class="yellow inverted icon times remove_parameter"></i></div>';
  703. ret = $(ret);
  704. function_container.find('.container_parameters_list').append(ret);
  705. ret.find('.remove_parameter').on('click', function (e) {
  706. removeParameter(function_obj, parameter_obj, ret);
  707. });
  708. ret.find('.ui.dropdown.parameter_type').dropdown({
  709. onChange: function (value, text, $selectedItem) {
  710. if ($selectedItem.data('dimensions')) {
  711. updateParameterType(parameter_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  712. } else {
  713. updateParameterType(parameter_obj, Types[$selectedItem.data('type')]);
  714. }
  715. },
  716. selectOnKeydown: false
  717. });
  718. ret.find('.label_enable_name_parameter').on('click', function (e) {
  719. enableNameParameterUpdate(parameter_obj, ret);
  720. });
  721. return ret;
  722. }
  723. function updateParameterName (parameter_var, new_name, parameter_obj_dom, function_obj) {
  724. if (isValidIdentifier(new_name)) {
  725. if (variableNameAlreadyExists(new_name, function_obj)) {
  726. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getUI('inform_valid_variable_duplicated'));
  727. } else {
  728. parameter_var.name = new_name;
  729. }
  730. } else {
  731. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getUI('inform_valid_name'));
  732. }
  733. }
  734. function variableNameAlreadyExists (name_var, function_obj) {
  735. if (function_obj.parameters_list) {
  736. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  737. if (function_obj.parameters_list[i].name == name_var) {
  738. return true;
  739. }
  740. }
  741. }
  742. if (function_obj.variables_list) {
  743. for (var i = 0; i < function_obj.variables_list.length; i++) {
  744. if (function_obj.variables_list[i].name == name_var) {
  745. return true;
  746. }
  747. }
  748. }
  749. return false;
  750. }
  751. function updateFunctionName (function_var, new_name, function_obj_dom) {
  752. if (isValidIdentifier(new_name)) {
  753. if (functionNameAlreadyExists(new_name)) {
  754. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getUI('inform_valid_name_duplicated'));
  755. } else {
  756. function_var.name = new_name;
  757. }
  758. } else {
  759. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getUI('inform_valid_name'));
  760. }
  761. }
  762. function functionNameAlreadyExists (function_name) {
  763. for (var i = 0; i < window.program_obj.functions.length; i++) {
  764. if (window.program_obj.functions[i].name == function_name) {
  765. return true;
  766. }
  767. }
  768. return false;
  769. }
  770. function isValidIdentifier (identifier_str) {
  771. return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(identifier_str);
  772. }
  773. var opened_name_parameter = false;
  774. var opened_input_parameter = null;
  775. function enableNameParameterUpdate (parameter_obj, parent_node) {
  776. if (opened_name_parameter) {
  777. opened_input_parameter.focus();
  778. return;
  779. }
  780. opened_name_parameter = true;
  781. parent_node = $(parent_node);
  782. var input_field;
  783. parent_node.find('.span_name_parameter').text('');
  784. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='" + parameter_obj.name + "' />");
  785. input_field.insertBefore(parent_node.find('.span_name_parameter'));
  786. input_field.on('input', function () {
  787. var inputWidth = input_field.textWidth() + 10;
  788. opened_input_parameter = input_field;
  789. input_field.focus();
  790. var tmpStr = input_field.val();
  791. input_field.val('');
  792. input_field.val(tmpStr);
  793. input_field.css({
  794. width: inputWidth
  795. })
  796. }).trigger('input');
  797. input_field.focusout(function () {
  798. /// update array:
  799. if (input_field.val().trim()) {
  800. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  801. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  802. }
  803. input_field.off();
  804. input_field.remove();
  805. /// update elements:
  806. opened_name_parameter = false;
  807. opened_input_parameter = false;
  808. });
  809. input_field.on('keydown', function (e) {
  810. var code = e.keyCode || e.which;
  811. if (code == 13) {
  812. if (input_field.val().trim()) {
  813. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  814. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  815. }
  816. input_field.off();
  817. input_field.remove();
  818. /// update elements:
  819. opened_name_parameter = false;
  820. opened_input_parameter = false;
  821. }
  822. if (code == 27) {
  823. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  824. input_field.off();
  825. input_field.remove();
  826. /// update elements:
  827. opened_name_parameter = false;
  828. opened_input_parameter = false;
  829. }
  830. });
  831. input_field.select();
  832. }
  833. var opened_name_function = false;
  834. var opened_input = null;
  835. var previousPadding = null;
  836. function enableNameFunctionUpdate(function_obj, parent_node) {
  837. if (opened_name_function) {
  838. opened_input.focus();
  839. return;
  840. }
  841. parent_node = $(parent_node);
  842. parent_node.find('.span_name_function').text('');
  843. var input_field;
  844. if (!previousPadding) {
  845. previousPadding = parent_node.find('.span_name_function').css('padding-left');
  846. }
  847. parent_node.find('.span_name_function').css('padding-left', '0');
  848. parent_node.find('.span_name_function').css('padding-right', '0');
  849. input_field = $("<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='" + function_obj.name + "' />");
  850. input_field.insertBefore(parent_node.find('.span_name_function'));
  851. input_field.on('input', function () {
  852. var inputWidth = input_field.textWidth() + 10;
  853. opened_input = input_field;
  854. input_field.focus();
  855. var tmpStr = input_field.val();
  856. input_field.val('');
  857. input_field.val(tmpStr);
  858. input_field.css({
  859. width: inputWidth
  860. })
  861. }).trigger('input');
  862. input_field.focusout(function () {
  863. /// update array:
  864. if (input_field.val().trim()) {
  865. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  866. }
  867. input_field.off();
  868. input_field.remove();
  869. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  870. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  871. parent_node.find('.span_name_function').text(function_obj.name);
  872. /// update elements:
  873. opened_name_function = false;
  874. opened_input = false;
  875. });
  876. input_field.on('keydown', function (e) {
  877. var code = e.keyCode || e.which;
  878. if (code == 13) {
  879. if (input_field.val().trim()) {
  880. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  881. }
  882. input_field.off();
  883. input_field.remove();
  884. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  885. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  886. parent_node.find('.span_name_function').text(function_obj.name);
  887. /// update elements:
  888. opened_name_function = false;
  889. opened_input = false;
  890. }
  891. if (code == 27) {
  892. input_field.off();
  893. input_field.remove();
  894. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  895. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  896. parent_node.find('.span_name_function').text(function_obj.name);
  897. /// update elements:
  898. opened_name_function = false;
  899. opened_input = false;
  900. }
  901. });
  902. input_field.select();
  903. }
  904. /****************************************************
  905. //DOUGLAS
  906. *******************************************************/
  907. export function generateMenuButton(function_obj) {
  908. if (function_obj.identifier) {
  909. var identifier = LocalizedStrings.getUI(function_obj.identifier);
  910. var menu_button = '<button class="fluid ui container segment labeled icon button list-group-item menu-item" draggable="true" data-function="' + identifier + '"><i class="code icon"></i> <span class="function_name">' + identifier + '</span> (<span class="function_params"></span>) : <span class="function_return_type">' + LocalizedStrings.getUI(function_obj.return_type) + '</span></button>';
  911. } else {
  912. var menu_button = '<button class="fluid ui container segment labeled icon button list-group-item menu-item" draggable="true" data-function="' + function_obj.name + '"><i class="code icon"></i> <span class="function_name">' + function_obj.name + '</span> (<span class="function_params"></span>) : <span class="function_return_type">' + LocalizedStrings.getUI(function_obj.return_type) + '</span></button>';
  913. }
  914. var params = "";
  915. menu_button = $(menu_button);
  916. for (var j = 0; j < function_obj.parameters_list.length; j++) {
  917. if (j > 0)
  918. params += ',';
  919. if (!function_obj.identifier) {
  920. params += LocalizedStrings.getUI(function_obj.parameters_list[j].type);
  921. } else {
  922. params += function_obj.parameters_list[j].type;
  923. }
  924. }
  925. menu_button
  926. .data('fun', function_obj)
  927. .on('dragstart', function (e) {
  928. program_obj.dataTransfer = { type: "function", content: function_obj };
  929. //e.originalEvent.dataTransfer.setData("text", JSON.stringify({type:"function",content:function_obj}));
  930. //evt.originalEvent.dataTransfer.setData("text",$(this).data('command'));
  931. })
  932. .find('.function_params').text(params)
  933. .find('.function_return_type').text(function_obj.type);
  934. menu_button
  935. .on('click', function (evt) {
  936. $(this).trigger('dragstart');
  937. if (window.ghostNode) {
  938. $(window.ghostNode).remove();
  939. $(document).off('mousemove');
  940. }
  941. window.ghostNode = $(this).clone();
  942. ghostNode.outerWidth($(this).outerWidth());
  943. ghostNode.draggable().appendTo('body');
  944. ghostNode.css('position', 'absolute');
  945. ghostNode.css('left', evt.pageX);
  946. ghostNode.css('top', evt.pageY);
  947. evt.type = 'drag';
  948. evt.target = ghostNode[0];
  949. ghostNode.trigger(evt);
  950. $(document).on('mousemove', function (evt) {
  951. ghostNode.css('left', evt.pageX);
  952. ghostNode.css('top', evt.pageY);
  953. });
  954. $(document).on('mousedown', function (evt) {
  955. console.log("length ===");
  956. console.log($(evt.target).closest(".commands_list_div"))
  957. if ($(evt.target).closest(".commands_list_div").length <= 0) {
  958. if (window.ghostNode) {
  959. console.log("drop click");
  960. $('.div-over').removeClass('div-over');
  961. $(window.ghostNode).remove();
  962. delete window.ghostNode;
  963. $(document).off('mousemove').off('mousedown').off('keyup');
  964. }
  965. }
  966. });
  967. $(document).keyup(function (e) {
  968. console.log("KeyUp")
  969. if (e.key === "Escape") {
  970. console.log("escape");
  971. $('.div-over').removeClass('div-over');
  972. $(window.ghostNode).remove();
  973. delete window.ghostNode;
  974. $(document).off('mousemove').off('mousedown').off('keyup');
  975. }
  976. });
  977. });
  978. return menu_button;
  979. }
  980. removeFunction = function (function_obj) {
  981. var index = program.functions.indexOf(function_obj);
  982. if (index > -1) {
  983. program.functions.splice(index, 1);
  984. }
  985. $('.functions_labels > [data-function=' + function_obj.name + ']').remove();
  986. }
  987. // renderFunction = function (function_obj) {
  988. // var appender = '<div class="ui secondary segment function_div list-group-item">';
  989. // if (function_obj.function_comment) {
  990. // //appender += renderComment(function_obj.function_comment, sequence, true, -1);
  991. // }
  992. // appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
  993. // appender += (function_obj.is_main ? '<div class="div_start_minimize_v"> </div>' : '<button class="ui icon button large remove_function_button"><i class="red icon times"></i></button>')
  994. // + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
  995. // appender += '<div class="ui small icon buttons add_var_top_button"><div class="ui icon button add_var_button_function"><i class="icon superscript"></i></div>';
  996. // appender += '<div class="ui icon button dropdown menu_commands" ><i class="icon code"></i> <div class="menu"> ';
  997. // appender += '<a class="item" data-command="' + Models.COMMAND_TYPES.reader + '"><i class="download icon"></i> ' + LocalizedStrings.getUI('text_read_var') + '</a>'
  998. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.writer + '"><i class="upload icon"></i> ' + LocalizedStrings.getUI('text_write_var') + '</a>'
  999. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.comment + '"><i class="quote left icon"></i> ' + LocalizedStrings.getUI('text_comment') + '</a>'
  1000. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.attribution + '"><i class="arrow left icon"></i> ' + LocalizedStrings.getUI('text_attribution') + '</a>'
  1001. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.functioncall + '"><i class="hand point right icon"></i> ' + LocalizedStrings.getUI('text_functioncall') + '</a>'
  1002. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.iftrue + '" ><i class="random icon"></i> ' + LocalizedStrings.getUI('text_iftrue') + '</a>'
  1003. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.repeatNtimes + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_repeatNtimes') + '</a>'
  1004. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.whiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_whiletrue') + '</a>'
  1005. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.dowhiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_dowhiletrue') + '</a>'
  1006. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.switch + '"><i class="list icon"></i> ' + LocalizedStrings.getUI('text_switch') + '</a>'
  1007. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.return + '"><i class="reply icon"></i> ' + LocalizedStrings.getUI('text_btn_return') + '</a>'
  1008. // + '</div></div></div>';
  1009. // appender += '<div class="function_signature_div">' + LocalizedStrings.getUI("function") + ' ';
  1010. // if (function_obj.is_main) {
  1011. // appender += '<div class="function_name_div"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' + LocalizedStrings.getUI('void') + ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="span_name_function" >' + function_obj.name + '</span> </div> '
  1012. // + '( <div class="ui large labels parameters_list">';
  1013. // } else {
  1014. // appender += '<div class="ui function_return"></div>';
  1015. // appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">' + function_obj.name + '</span> </div> '
  1016. // + ' <span class="parethesis_function"> ( </span> <i class="ui icon plus square outline add_parameter_button"></i> <div class="ui large labels parameters_list container_parameters_list">';
  1017. // $('.functions_labels').append(generateMenuButton(function_obj));
  1018. // console.log("aqui");
  1019. // //var menu_button = $('.functions_labels > [data-function=' + function_obj.name + ']');
  1020. // //var params = "";
  1021. // //menu_button
  1022. // }
  1023. // appender += '</div> ) </div>'
  1024. // + (function_obj.is_hidden ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ')
  1025. // + '<div class="ui top attached segment variables_list_div">'
  1026. // /*+ renderVariables(function_obj, sequence)*/
  1027. // + '</div>'
  1028. // + '<div class="ui bottom attached segment commands_list_div" id="function_drag_cmd_">';
  1029. // appender += '</div>';
  1030. // appender += '<div class="function_close_div"></div>'
  1031. // + '</div>'
  1032. // + '</div>';
  1033. // appender = $(appender);
  1034. // $('.all_functions').append(appender);
  1035. // appender.data('fun', function_obj);
  1036. // appender.find('.commands_list_div')
  1037. // .data('fun', function_obj)
  1038. // .attr('droppable', true)
  1039. // .on('dragenter', function (e) {
  1040. // e.preventDefault();
  1041. // console.log('dragenter');
  1042. // console.log(e.target)
  1043. // $(e.target).addClass('div-over')
  1044. // //e.stopPropagation();
  1045. // }).on('dragover', function (e) {
  1046. // e.preventDefault();
  1047. // })
  1048. // .on('dragleave', function (e) {
  1049. // e.preventDefault();
  1050. // //e.stopPropagation();
  1051. // console.log("dragleave")
  1052. // $(e.target).removeClass('div-over')
  1053. // console.log(e.target)
  1054. // })
  1055. // .on('drop', function (e, bundle) {
  1056. // e.preventDefault();
  1057. // if (bundle) {
  1058. // e.clientX = bundle.clientX;
  1059. // e.clientY = bundle.clientY;
  1060. // }
  1061. // //console.log('ondrop ' + e.originalEvent.dataTransfer.getData("text"));
  1062. // console.log(e)
  1063. // $(e.target).removeClass('div-over')
  1064. // //var data = JSON.parse(e.originalEvent.dataTransfer.getData("text"));
  1065. // var data = program_obj.dataTransfer;
  1066. // if (data.type == 'command')
  1067. // CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, data.content);
  1068. // else if (data.type == 'var')
  1069. // CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "attribution", data.content);
  1070. // else {
  1071. // if (!data.content.parameters_list)
  1072. // data.content.parameters_list = [];
  1073. // CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "functioncall", data.content);
  1074. // }
  1075. // //program_obj.dataTransfer;
  1076. // }).on('click', function (e) {
  1077. // if (window.ghostNode) {
  1078. // console.log("drop click");
  1079. // $(document).off('mousemove').off('mousedown').off('keyup');
  1080. // $(window.ghostNode).remove();
  1081. // delete window.ghostNode;
  1082. // $(this).trigger('drop', [e]);
  1083. // $('.div-over').removeClass('div-over');
  1084. // }
  1085. // })
  1086. // .on('mouseover', function (evt) {
  1087. // if (window.ghostNode) {
  1088. // evt.type = 'dragenter';
  1089. // //$(this).trigger('dragenter');
  1090. // console.log('mouseover');
  1091. // $(this).trigger(evt);
  1092. // }
  1093. // })
  1094. // .on('mouseout', function (evt) {
  1095. // //$(this).trigger('dragleave');
  1096. // if (window.ghostNode) {
  1097. // evt.type = 'dragleave';
  1098. // console.log('mouseout');
  1099. // $(this).trigger(evt);
  1100. // }
  1101. // });
  1102. // renderFunctionReturn(function_obj, appender);
  1103. // addHandlers(function_obj, appender);
  1104. // // Rendering parameters:
  1105. // for (var j = 0; j < function_obj.parameters_list.length; j++) {
  1106. // renderParameter(function_obj, function_obj.parameters_list[j], appender);
  1107. // }
  1108. // // Rendering variables:
  1109. // for (var j = 0; j < function_obj.variables_list.length; j++) {
  1110. // VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
  1111. // }
  1112. // // Rendering commands:
  1113. // for (var j = 0; j < function_obj.commands.length; j++) {
  1114. // CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
  1115. // }
  1116. // console.log('kk')
  1117. // console.log($($('.function_div')[0]).data('fun'))
  1118. // console.log(appender.data('fun'))
  1119. // }
  1120. initVisualUI = function () {
  1121. // MUST USE CONST, LET, OR VAR !!!!!!
  1122. const mainDiv = $('#visual-main-div');
  1123. // fill mainDiv with functions and globals...
  1124. // renderAlgorithm()...
  1125. $('.add_function_button').on('click', () => {
  1126. addFunctionHandler();
  1127. });
  1128. $('.add_global_button').on('click', () => {
  1129. GlobalsManagement.addGlobal(program);
  1130. });
  1131. $('.run_button').on('click', () => {
  1132. runCode();
  1133. });
  1134. $('.visual_coding_button').on('click', () => {
  1135. toggleVisualCoding();
  1136. });
  1137. $('.textual_coding_button').on('click', () => {
  1138. toggleTextualCoding();
  1139. });
  1140. $('.assessment').on('click', () => {
  1141. runCodeAssessment();
  1142. is_iassign = true;
  1143. });
  1144. $('.div_toggle_console').on('click', () => {
  1145. toggleConsole();
  1146. });
  1147. var commands = [
  1148. { type: Models.COMMAND_TYPES.reader, icon: "download", text: LocalizedStrings.getUI('text_read_var') },
  1149. { type: Models.COMMAND_TYPES.writer, icon: "upload", text: LocalizedStrings.getUI('text_write_var') },
  1150. { type: Models.COMMAND_TYPES.comment, icon: "quote left", text: LocalizedStrings.getUI('text_comment') },
  1151. { type: Models.COMMAND_TYPES.attribution, icon: "arrow left", text: LocalizedStrings.getUI('text_attribution') },
  1152. { type: Models.COMMAND_TYPES.iftrue, icon: "random", text: LocalizedStrings.getUI('text_iftrue') },
  1153. { type: Models.COMMAND_TYPES.repeatNtimes, icon: "sync", text: LocalizedStrings.getUI('text_repeatNtimes') },
  1154. { type: Models.COMMAND_TYPES.whiletrue, icon: "sync", text: LocalizedStrings.getUI('text_whiletrue') },
  1155. { type: Models.COMMAND_TYPES.dowhiletrue, icon: "sync", text: LocalizedStrings.getUI('text_dowhiletrue') },
  1156. { type: Models.COMMAND_TYPES.switch, icon: "list", text: LocalizedStrings.getUI('text_switch') },
  1157. { type: Models.COMMAND_TYPES.return, icon: "reply", text: LocalizedStrings.getUI('text_btn_return') },
  1158. //{ type: Models.COMMAND_TYPES.break, icon: "stop", text: LocalizedStrings.getUI('text_break') },
  1159. //{type: Models.COMMAND_TYPES.functioncall, icon: "hand point right", text: LocalizedStrings.getUI('text_functioncall')},
  1160. ];
  1161. for (var i = commands.length-1; i >= 0; i--) {
  1162. var command = '<button class="fluid ui container segment labeled icon button list-group-item menu-item" draggable="true" data-command="' + commands[i].type + '"><i class="' + commands[i].icon + ' icon"></i> ' + commands[i].text + '</button>';
  1163. command = $(command);
  1164. command.on('dragstart', function (evt) {
  1165. //evt.originalEvent.dataTransfer.setData("text",$(this).data('command'));
  1166. //evt.originalEvent.dataTransfer.setData("text",JSON.stringify({type:"command",content:$(this).data('command')}));
  1167. program_obj.dataTransfer = { type: "command", content: $(this).data('command') };
  1168. console.log('dragstart')
  1169. console.log(evt);
  1170. });
  1171. command.on('click', function (evt) {
  1172. $(this).trigger('dragstart');
  1173. if (window.ghostNode) {
  1174. $(window.ghostNode).remove();
  1175. $(document).off('mousemove');
  1176. }
  1177. window.ghostNode = $(this).clone();
  1178. ghostNode.outerWidth($(this).outerWidth());
  1179. ghostNode.draggable().appendTo('body');
  1180. ghostNode.css('position', 'absolute');
  1181. ghostNode.css('left', evt.pageX);
  1182. ghostNode.css('top', evt.pageY);
  1183. evt.type = 'drag';
  1184. evt.target = ghostNode[0];
  1185. ghostNode.trigger(evt);
  1186. $(document).on('mousemove', function (evt) {
  1187. ghostNode.css('left', evt.pageX);
  1188. ghostNode.css('top', evt.pageY);
  1189. });
  1190. $(document).on('mousedown', function (evt) {
  1191. console.log("length ===");
  1192. console.log($(evt.target).closest(".commands_list_div"))
  1193. if ($(evt.target).closest(".commands_list_div").length <= 0) {
  1194. if (window.ghostNode) {
  1195. console.log("drop click");
  1196. $('.div-over').removeClass('div-over');
  1197. $(window.ghostNode).remove();
  1198. delete window.ghostNode;
  1199. $(document).off('mousemove').off('mousedown').off('keyup');
  1200. }
  1201. }
  1202. });
  1203. $(document).keyup(function (e) {
  1204. console.log("KeyUp")
  1205. if (e.key === "Escape") {
  1206. console.log("escape");
  1207. $('.div-over').removeClass('div-over');
  1208. $(window.ghostNode).remove();
  1209. delete window.ghostNode;
  1210. $(document).off('mousemove').off('mousedown').off('keyup');
  1211. }
  1212. });
  1213. });
  1214. $('.list-commands').prepend(command);
  1215. }
  1216. /*
  1217. $(document).on('mousemove',function(evt) {
  1218. if (typeof ghostNode !== 'undefined') {
  1219. ghostNode.css('left', evt.pageX - 15);
  1220. ghostNode.css('top', evt.pageY - 15);
  1221. }
  1222. });*/
  1223. var library_labels = $('.library_labels');
  1224. for (var i = 0; i < system_functions.length; i++) {
  1225. var system_button = generateMenuButton(system_functions[i]);
  1226. var category = system_functions[i].category;
  1227. var cat_accord = null;
  1228. if (library_labels.find('.' + category).length == 0) {
  1229. cat_accord =
  1230. '<div class="ui styled accordion ' + category + '"> \
  1231. <div class="title"> \
  1232. <i class="dropdown icon"></i> \
  1233. ' + LocalizedStrings.getUI(category) + ' \
  1234. </div> \
  1235. <div class="content"><div>';
  1236. library_labels.append(cat_accord);
  1237. }
  1238. cat_accord = $(library_labels.find('.' + category)[0]);
  1239. cat_accord.find('.content').append(system_button);
  1240. }
  1241. $('.accordion').accordion();
  1242. }
  1243. /*
  1244. renderParameter = function(function_obj, parameter_obj, function_container) {
  1245. var ret = "";
  1246. ret += '<div class="ui label function_name_parameter"><span class="span_name_parameter label_enable_name_parameter">'+parameter_obj.name+'</span> <i class="icon small pencil alternate enable_edit_name_parameter label_enable_name_parameter"></i>';
  1247. ret += '<div class="ui dropdown parameter_type">';
  1248. if (parameter_obj.dimensions > 0) {
  1249. ret += '<div class="text">'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(parameter_obj.type);
  1250. ret += '</div>';
  1251. } else {
  1252. ret += '<div class="text">'+LocalizedStrings.getUI(parameter_obj.type)+'</div>';
  1253. }
  1254. ret += '<i class="dropdown icon"></i>'
  1255. + '<div class="menu">';
  1256. for (var tm in Types) {
  1257. if (tm == Types.VOID.toUpperCase()) {
  1258. continue;
  1259. }
  1260. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  1261. }
  1262. for (var tm in Types) {
  1263. if (tm == Types.VOID.toUpperCase()) {
  1264. continue;
  1265. }
  1266. ret += '<div class="item">'
  1267. + '<i class="dropdown icon"></i>'
  1268. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  1269. + '<div class="menu">'
  1270. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  1271. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  1272. + '</div>'
  1273. + '</div>';
  1274. }
  1275. ret += '</div></div>';
  1276. ret += ' <i class="red icon times remove_parameter"></i></div>';
  1277. ret = $(ret);
  1278. function_container.find('.container_parameters_list').append(ret);
  1279. ret.find('.remove_parameter').on('click', function(e){
  1280. removeParameter(function_obj, parameter_obj, ret);
  1281. });
  1282. ret.find('.ui.dropdown.parameter_type').dropdown({
  1283. onChange: function(value, text, $selectedItem) {
  1284. if ($($selectedItem).data('dimensions')) {
  1285. updateParameterType(parameter_obj, Types[$($selectedItem).data('type')], $($selectedItem).data('dimensions'));
  1286. } else {
  1287. updateParameterType(parameter_obj, Types[$($selectedItem).data('type')]);
  1288. }
  1289. }
  1290. });
  1291. ret.find('.label_enable_name_parameter').on('click', function(e){
  1292. enableNameParameterUpdate(parameter_obj, ret);
  1293. });
  1294. }
  1295. enableNameFunctionUpdate = function(function_obj, parent_node) {
  1296. if (opened_name_function) {
  1297. $(opened_input).focus();
  1298. return;
  1299. }
  1300. $(parent_node).find('.span_name_function').text('');
  1301. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+function_obj.name+"' />" ).insertBefore($(parent_node).find('.span_name_function'));
  1302. $('.width-dynamic').on('input', function() {
  1303. var inputWidth = $(this).textWidth()+10;
  1304. opened_input = this;
  1305. $(this).focus();
  1306. var tmpStr = $(this).val();
  1307. $(this).val('');
  1308. $(this).val(tmpStr);
  1309. $(this).css({
  1310. width: inputWidth
  1311. })
  1312. }).trigger('input');
  1313. $('.width-dynamic').focusout(function() {
  1314. /// update array:
  1315. if ($(this).val().trim()) {
  1316. function_obj.name = $(this).val().trim();
  1317. }
  1318. $(this).remove();
  1319. $(parent_node).find('.span_name_function').text(function_obj.name);
  1320. /// update elements:
  1321. opened_name_function = false;
  1322. opened_input = false;
  1323. });
  1324. $('.width-dynamic').on('keydown', function(e) {
  1325. var code = e.keyCode || e.which;
  1326. if(code == 13) {
  1327. $('.functions_labels > [data-function=' + function_obj.name + ']')
  1328. .attr('data-function', $(this).val().trim())
  1329. .html('<i class="list icon"></i> ' + $(this).val().trim());
  1330. if ($(this).val().trim()) {
  1331. function_obj.name = $(this).val().trim();
  1332. }
  1333. $(this).remove();
  1334. $(parent_node).find('.span_name_function').text(function_obj.name);
  1335. /// update elements:
  1336. opened_name_function = false;
  1337. opened_input = false;
  1338. }
  1339. if(code == 27) {
  1340. $(this).remove();
  1341. $(parent_node).find('.span_name_function').text(function_obj.name);
  1342. /// update elements:
  1343. opened_name_function = false;
  1344. opened_input = false;
  1345. }
  1346. });
  1347. }
  1348. addParameter = function (function_obj, function_container) {
  1349. if (function_obj.parameters_list == null) {
  1350. function_obj.parameters_list = [];
  1351. }
  1352. var new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
  1353. new_parameter.function_obj = function_obj;
  1354. function_obj.parameters_list.push(new_parameter);
  1355. counter_new_parameters ++;
  1356. renderParameter(function_obj, new_parameter, function_container);
  1357. //updateMenuButton(function_obj);
  1358. }
  1359. removeParameter = function (function_obj, parameter_obj, parameter_container) {
  1360. var index = function_obj.parameters_list.indexOf(parameter_obj);
  1361. if (index > -1) {
  1362. function_obj.parameters_list.splice(index, 1);
  1363. }
  1364. $(parameter_container).remove();
  1365. }*/
  1366. /*
  1367. updateReturnType = function (function_obj, new_type, new_dimensions = 0) {
  1368. function_obj.return_type = new_type;
  1369. function_obj.return_dimensions = new_dimensions;
  1370. var menu_button = $('.functions_labels > [data-function=' + function_obj.name + ']');
  1371. menu_button.find('.function_return_type').text(LocalizedStrings.getUI(new_type));
  1372. }*/
  1373. /*
  1374. updateParameterType = function (parameter_obj, new_type, new_dimensions = 0) {
  1375. parameter_obj.type = new_type;
  1376. parameter_obj.dimensions = new_dimensions;
  1377. if (new_dimensions > 0) {
  1378. parameter_obj.rows = new_dimensions;
  1379. parameter_obj.columns = 2;
  1380. }
  1381. //updateMenuButton(parameter_obj.function_obj);
  1382. }*/