functions.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. import $ from 'jquery';
  2. import { Types } from './types';
  3. import * as Models from './ivprog_elements';
  4. import { LocalizedStrings } from './../services/localizedStringsService';
  5. import * as GlobalsManagement from './globals';
  6. import * as VariablesManagement from './variables';
  7. import * as CommandsManagement from './commands';
  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';
  17. import '../Sortable.js';
  18. var counter_new_functions = 0;
  19. var counter_new_parameters = 0;
  20. let studentTemp = null;
  21. let domConsole = null;
  22. window.studentGrade = null;
  23. window.LocalizedStrings = LocalizedStrings;
  24. const program = new Models.Program();
  25. window.system_functions = [];
  26. // Adding math functions:
  27. window.system_functions.push(new Models.SystemFunction('$sin', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  28. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  29. window.system_functions.push(new Models.SystemFunction('$cos', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  30. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  31. window.system_functions.push(new Models.SystemFunction('$tan', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  32. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  33. window.system_functions.push(new Models.SystemFunction('$sqrt', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  34. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  35. 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)],
  36. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  37. window.system_functions.push(new Models.SystemFunction('$log', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  38. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  39. window.system_functions.push(new Models.SystemFunction('$abs', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  40. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  41. window.system_functions.push(new Models.SystemFunction('$negate', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  42. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  43. window.system_functions.push(new Models.SystemFunction('$invert', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  44. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  45. window.system_functions.push(new Models.SystemFunction('$max', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  46. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  47. window.system_functions.push(new Models.SystemFunction('$min', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  48. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  49. // Adding text functions:
  50. window.system_functions.push(new Models.SystemFunction('$substring', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  51. 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)],
  52. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  53. window.system_functions.push(new Models.SystemFunction('$length', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  54. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  55. window.system_functions.push(new Models.SystemFunction('$uppercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  56. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  57. window.system_functions.push(new Models.SystemFunction('$lowercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  58. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  59. 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)],
  60. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  61. // Adding arrangement functions:
  62. 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)],
  63. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  64. 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)],
  65. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  66. 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)],
  67. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  68. // Adding conversion functions:
  69. window.system_functions.push(new Models.SystemFunction('$isReal', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  70. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  71. window.system_functions.push(new Models.SystemFunction('$isInt', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  72. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  73. window.system_functions.push(new Models.SystemFunction('$isBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  74. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  75. window.system_functions.push(new Models.SystemFunction('$castReal', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  76. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  77. window.system_functions.push(new Models.SystemFunction('$castInt', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  78. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  79. window.system_functions.push(new Models.SystemFunction('$castBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  80. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  81. window.system_functions.push(new Models.SystemFunction('$castString', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  82. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  83. /*const variable1 = new Models.Variable(Types.INTEGER, "a", 1);
  84. const parameter1 = new Models.Variable(Types.INTEGER, "par_1", 1);
  85. const command1 = new Models.Comment(new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.only_value, "Testing rendering commands"));
  86. const sumFunction = new Models.Function("soma", Types.INTEGER, 0, [parameter1], false, false, [], null, [command1]);
  87. program.addFunction(sumFunction);
  88. */
  89. 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');
  90. const mainFunction = new Models.Function(LocalizedStrings.getUI("start"), Types.VOID, 0, [], true, false);
  91. mainFunction.function_comment = new Models.Comment(LocalizedStrings.getUI('text_comment_main'));
  92. program.addFunction(mainFunction);
  93. window.program_obj = program;
  94. window.generator = CodeManagement.generate;
  95. window.runCodeAssessment = runCodeAssessment;
  96. window.renderAlgorithm = AlgorithmManagement.renderAlgorithm;
  97. window.insertContext = false;
  98. window.watchW = WatchJS;
  99. WatchJS.watch(window.program_obj.globals, function(){
  100. if (window.insertContext) {
  101. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  102. window.insertContext = false;
  103. } else {
  104. AlgorithmManagement.renderAlgorithm();
  105. }
  106. }, 1);
  107. WatchJS.watch(window.program_obj.functions, function(){
  108. if (window.insertContext) {
  109. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  110. window.insertContext = false;
  111. } else {
  112. AlgorithmManagement.renderAlgorithm();
  113. }
  114. }, 0);
  115. function addFunctionHandler () {
  116. 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')));
  117. program.addFunction(new_function);
  118. counter_new_functions ++;
  119. window.insertContext = true;
  120. var newe = renderFunction(new_function);
  121. newe.css('display', 'none');
  122. newe.fadeIn();
  123. }
  124. function addParameter (function_obj, function_container, is_from_click = false) {
  125. if (function_obj.parameters_list == null) {
  126. function_obj.parameters_list = [];
  127. }
  128. var new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
  129. function_obj.parameters_list.push(new_parameter);
  130. counter_new_parameters ++;
  131. var newe = renderParameter(function_obj, new_parameter, function_container);
  132. if (is_from_click) {
  133. newe.css('display', 'none');
  134. newe.fadeIn();
  135. }
  136. }
  137. function updateReturnType (function_obj, new_type, new_dimensions = 0) {
  138. function_obj.return_type = new_type;
  139. function_obj.return_dimensions = new_dimensions;
  140. }
  141. function removeFunction (function_obj) {
  142. var index = program.functions.indexOf(function_obj);
  143. if (index > -1) {
  144. program.functions.splice(index, 1);
  145. }
  146. }
  147. function minimizeFunction (function_obj) {
  148. function_obj.is_hidden = !function_obj.is_hidden;
  149. }
  150. function addHandlers (function_obj, function_container) {
  151. function_container.find('.ui.dropdown.function_return').dropdown({
  152. onChange: function(value, text, $selectedItem) {
  153. if ($selectedItem.data('dimensions')) {
  154. updateReturnType(function_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  155. } else {
  156. updateReturnType(function_obj, Types[$selectedItem.data('type')]);
  157. }
  158. },
  159. selectOnKeydown: false
  160. });
  161. function_container.find( ".name_function_updated" ).on('click', function(e){
  162. enableNameFunctionUpdate(function_obj, function_container);
  163. });
  164. function_container.find( ".add_parameter_button" ).on('click', function(e){
  165. window.insertContext = true;
  166. addParameter(function_obj, function_container, true);
  167. });
  168. function_container.find('.menu_commands').dropdown({
  169. on: 'hover'
  170. });
  171. function_container.find('.menu_commands a').on('click', function(evt){
  172. if (function_obj.commands == null || function_obj.commands.length == 0) {
  173. function_obj.commands = [];
  174. var new_cmd = CommandsManagement.genericCreateCommand($(this).data('command'));
  175. function_obj.commands.push(new_cmd);
  176. CommandsManagement.renderCommand(new_cmd, function_container.find('.commands_list_div'), 3, function_obj);
  177. } else {
  178. CommandsManagement.createFloatingCommand(function_obj, function_container, $(this).data('command'), evt);
  179. }
  180. });
  181. function_container.find('.add_var_button_function').on('click', function(e){
  182. window.insertContext = true;
  183. VariablesManagement.addVariable(function_obj, function_container, true);
  184. });
  185. function_container.find('.remove_function_button').on('click', function(e){
  186. removeFunction(function_obj);
  187. function_container.fadeOut();
  188. });
  189. function_container.find('.minimize_function_button').on('click', function(e){
  190. minimizeFunction(function_obj);
  191. if (function_obj.is_hidden) {
  192. function_container.find(".add_var_button_function").toggle();
  193. function_container.find(".inline_add_command").toggle();
  194. function_container.find(".function_area").slideToggle();
  195. } else {
  196. function_container.find(".function_area").slideToggle(function(){
  197. function_container.find(".add_var_button_function").toggle();
  198. function_container.find(".inline_add_command").toggle();
  199. });
  200. }
  201. });
  202. }
  203. // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
  204. function renderFunctionReturn (function_obj, function_element) {
  205. var ret = '<div class="ui dropdown function_return">';
  206. if (function_obj.return_dimensions > 0) {
  207. ret += '<div class="text">'+ LocalizedStrings.getUI("vector") +':'+ LocalizedStrings.getUI(function_obj.return_type);
  208. if (function_obj.return_dimensions == 1) {
  209. ret += ' [ ] ';
  210. } else {
  211. ret += ' [ ] [ ] ';
  212. }
  213. ret += '</div>';
  214. } else {
  215. ret += '<div class="text">'+LocalizedStrings.getUI(function_obj.return_type)+'</div>';
  216. }
  217. ret += '<div class="menu">';
  218. for (var tm in Types) {
  219. ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions < 1 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  220. }
  221. for (var tm in Types) {
  222. if (tm == Types.VOID.toUpperCase()) {
  223. continue;
  224. }
  225. ret += '<div class="item">'
  226. + '<i class="dropdown icon"></i>'
  227. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  228. + '<div class="menu">'
  229. + '<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>'
  230. + '<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>'
  231. + '</div>'
  232. + '</div>';
  233. }
  234. ret += '</div></div>';
  235. ret = $(ret);
  236. function_element.find('.function_return').append(ret);
  237. }
  238. export function renderFunction (function_obj) {
  239. var appender = '<div class="ui secondary segment function_div list-group-item">';
  240. if (function_obj.function_comment) {
  241. //appender += renderComment(function_obj.function_comment, sequence, true, -1);
  242. }
  243. appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
  244. 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>')
  245. + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
  246. appender += '<div class="function_signature_div">'+LocalizedStrings.getUI("function")+' ';
  247. if (function_obj.is_main) {
  248. 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> '
  249. + ' <span class="parethesis_function">( </span> <div class="ui large labels parameters_list">';
  250. } else {
  251. appender += '<div class="ui function_return"></div>';
  252. appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">'+function_obj.name+'</span> </div> '
  253. + ' <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">';
  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').data('fun', function_obj);
  278. renderFunctionReturn(function_obj, appender);
  279. addHandlers(function_obj, appender);
  280. // Rendering parameters:
  281. for (var j = 0; j < function_obj.parameters_list.length; j++) {
  282. renderParameter(function_obj, function_obj.parameters_list[j], appender);
  283. }
  284. // Rendering variables:
  285. for (var j = 0; j < function_obj.variables_list.length; j++) {
  286. VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
  287. }
  288. // Rendering commands:
  289. for (var j = 0; j < function_obj.commands.length; j++) {
  290. CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
  291. }
  292. $('.minimize_function_button').popup({
  293. content : LocalizedStrings.getUI("tooltip_minimize"),
  294. delay: {
  295. show: 750,
  296. hide: 0
  297. }
  298. });
  299. Sortable.create(appender.find(".variables_list_div")[0], {
  300. handle: '.ellipsis',
  301. animation: 100,
  302. ghostClass: 'ghost',
  303. group: 'local_vars_drag_' + program.functions.indexOf(function_obj),
  304. onEnd: function (evt) {
  305. updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  306. }
  307. });
  308. Sortable.create(appender.find(".commands_list_div")[0], {
  309. handle: '.command_drag',
  310. animation: 100,
  311. ghostClass: 'ghost',
  312. group: 'commands_drag_' + program.functions.indexOf(function_obj),
  313. onEnd: function (evt) {
  314. //updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  315. }
  316. });
  317. if (!function_obj.is_main) {
  318. Sortable.create(appender.find(".container_parameters_list")[0], {
  319. handle: '.ellipsis',
  320. animation: 100,
  321. ghostClass: 'ghost',
  322. group: 'parameters_drag_' + program.functions.indexOf(function_obj),
  323. onEnd: function (evt) {
  324. updateSequenceParameters(evt.oldIndex, evt.newIndex, function_obj);
  325. }
  326. });
  327. }
  328. return appender;
  329. }
  330. export function initVisualUI () {
  331. // MUST USE CONST, LET, OR VAR !!!!!!
  332. const mainDiv = $('#visual-main-div');
  333. // fill mainDiv with functions and globals...
  334. // renderAlgorithm()...
  335. $('.add_function_button').on('click', () => {
  336. addFunctionHandler();
  337. });
  338. $('.add_global_button').on('click', () => {
  339. window.insertContext = true;
  340. GlobalsManagement.addGlobal(program, true);
  341. });
  342. $('.run_button').on('click', () => {
  343. runCode();
  344. });
  345. $('.visual_coding_button').on('click', () => {
  346. toggleVisualCoding();
  347. });
  348. $('.textual_coding_button').on('click', () => {
  349. toggleTextualCoding();
  350. });
  351. $('.assessment').on('click', () => {
  352. runCodeAssessment();
  353. is_iassign = true;
  354. });
  355. $('.div_toggle_console').on('click', () => {
  356. toggleConsole();
  357. });
  358. $('.expand_button').on('click', () => {
  359. full_screen();
  360. });
  361. $('.main_title h2').prop('title', LocalizedStrings.getUI('text_ivprog_description'));
  362. }
  363. var is_iassign = false;
  364. $( document ).ready(function() {
  365. for (var i = 0; i < program.functions.length; i++) {
  366. renderFunction(program.functions[i]);
  367. }
  368. var time_show = 750;
  369. $('.visual_coding_button').popup({
  370. content : LocalizedStrings.getUI("tooltip_visual"),
  371. delay: {
  372. show: time_show,
  373. hide: 0
  374. }
  375. });
  376. $('.textual_coding_button').popup({
  377. content : LocalizedStrings.getUI("tooltip_textual"),
  378. delay: {
  379. show: time_show,
  380. hide: 0
  381. }
  382. });
  383. $('.upload_file_button').popup({
  384. content : LocalizedStrings.getUI("tooltip_upload"),
  385. delay: {
  386. show: time_show,
  387. hide: 0
  388. }
  389. });
  390. $('.download_file_button').popup({
  391. content : LocalizedStrings.getUI("tooltip_download"),
  392. delay: {
  393. show: time_show,
  394. hide: 0
  395. }
  396. });
  397. $('.undo_button').popup({
  398. content : LocalizedStrings.getUI("tooltip_undo"),
  399. delay: {
  400. show: time_show,
  401. hide: 0
  402. }
  403. });
  404. $('.redo_button').popup({
  405. content : LocalizedStrings.getUI("tooltip_redo"),
  406. delay: {
  407. show: time_show,
  408. hide: 0
  409. }
  410. });
  411. $('.run_button').popup({
  412. content : LocalizedStrings.getUI("tooltip_run"),
  413. delay: {
  414. show: time_show,
  415. hide: 0
  416. }
  417. });
  418. $('.assessment_button').popup({
  419. content : LocalizedStrings.getUI("tooltip_evaluate"),
  420. delay: {
  421. show: time_show,
  422. hide: 0
  423. }
  424. });
  425. $('.help_button').popup({
  426. content : LocalizedStrings.getUI("tooltip_help") + ' - ' + LocalizedStrings.getUI("text_ivprog_version"),
  427. delay: {
  428. show: time_show,
  429. hide: 0
  430. }
  431. });
  432. $('.add_global_button').popup({
  433. content : LocalizedStrings.getUI("tooltip_add_global"),
  434. delay: {
  435. show: time_show,
  436. hide: 0
  437. }
  438. });
  439. $('.div_toggle_console').popup({
  440. content : LocalizedStrings.getUI("tooltip_console"),
  441. delay: {
  442. show: time_show,
  443. hide: 0
  444. }
  445. });
  446. Sortable.create(listWithHandle, {
  447. handle: '.glyphicon-move',
  448. animation: 100,
  449. ghostClass: 'ghost',
  450. group: 'functions_divs_drag',
  451. onEnd: function (evt) {
  452. updateSequenceFunction(evt.oldIndex, evt.newIndex);
  453. }
  454. });
  455. var listGlobalsHandle = document.getElementById("listGlobalsHandle");
  456. Sortable.create(listGlobalsHandle, {
  457. handle: '.ellipsis',
  458. animation: 100,
  459. ghostClass: 'ghost',
  460. group: 'globals_divs_drag',
  461. onEnd: function (evt) {
  462. updateSequenceGlobals(evt.oldIndex, evt.newIndex);
  463. }
  464. });
  465. });
  466. function updateSequenceParameters (oldIndex, newIndex, function_obj) {
  467. function_obj.parameters_list.splice(newIndex, 0, function_obj.parameters_list.splice(oldIndex, 1)[0]);
  468. }
  469. function updateSequenceLocals (oldIndex, newIndex, function_obj) {
  470. function_obj.variables_list.splice(newIndex, 0, function_obj.variables_list.splice(oldIndex, 1)[0]);
  471. }
  472. function updateSequenceGlobals (oldIndex, newIndex) {
  473. program_obj.globals.splice(newIndex, 0, program_obj.globals.splice(oldIndex, 1)[0]);
  474. }
  475. function updateSequenceFunction (oldIndex, newIndex) {
  476. program_obj.functions.splice(newIndex, 0, program_obj.functions.splice(oldIndex, 1)[0]);
  477. }
  478. function runCodeAssessment () {
  479. toggleConsole(true);
  480. window.studentGrade = null;
  481. studentTemp = null;
  482. const strCode = CodeManagement.generate();
  483. if (strCode == null) {
  484. return;
  485. }
  486. if(domConsole == null)
  487. domConsole = new DOMConsole("#ivprog-term");
  488. $("#ivprog-term").slideDown(500);
  489. const runner = new IVProgAssessment(strCode, testCases, domConsole);
  490. runner.runTest().then(grade => {
  491. if (!is_iassign) {
  492. parent.getEvaluationCallback(grade);
  493. } else {
  494. is_iassign = false;
  495. }
  496. }).catch( err => domConsole.err(err.message));
  497. }
  498. function runCode () {
  499. toggleConsole(true);
  500. const strCode = CodeManagement.generate();
  501. if (strCode == null) {
  502. return;
  503. }
  504. if(domConsole == null)
  505. domConsole = new DOMConsole("#ivprog-term");
  506. $("#ivprog-term").slideDown(500);
  507. try {
  508. const parser = IVProgParser.createParser(strCode);
  509. const analyser = new SemanticAnalyser(parser.parseTree());
  510. const data = analyser.analyseTree();
  511. const proc = new IVProgProcessor(data);
  512. proc.registerInput(domConsole);
  513. proc.registerOutput(domConsole);
  514. $("#ivprog-term").addClass('ivprog-term-active');
  515. proc.interpretAST().then( _ => {
  516. domConsole.info("Programa executado com sucesso!");
  517. $("#ivprog-term").removeClass('ivprog-term-active');
  518. }).catch(err => {
  519. domConsole.err(err.message);
  520. $("#ivprog-term").removeClass('ivprog-term-active');
  521. })
  522. } catch (error) {
  523. domConsole.err(error.message);
  524. console.log(error);
  525. }
  526. }
  527. function toggleConsole (is_running) {
  528. if (is_running) {
  529. $('.ivprog-term-div').css('display', 'block');
  530. $('#ivprog-term').css('min-height', '160px');
  531. $('#ivprog-term').css('margin-top', '-170px');
  532. return;
  533. }
  534. if ($('#ivprog-term').css('min-height') == '160px') {
  535. // esconder
  536. $('.ivprog-term-div').css('display', 'none');
  537. $('#ivprog-term').css('min-height', '0');
  538. $('#ivprog-term').css('margin-top', '-30px');
  539. $('#ivprog-term').css('padding', '5px');
  540. } else {
  541. // mostrar
  542. $('.ivprog-term-div').css('display', 'block');
  543. $('#ivprog-term').css('min-height', '160px');
  544. $('#ivprog-term').css('margin-top', '-170px');
  545. }
  546. }
  547. function waitToCloseConsole () {
  548. domConsole.info("Aperte qualquer tecla para fechar...");
  549. const p = new Promise((resolve, _) => {
  550. domConsole.requestInput(resolve, true);
  551. });
  552. p.then( _ => {
  553. domConsole.dispose();
  554. domConsole = null;
  555. $("#ivprog-term").hide();
  556. })
  557. }
  558. function toggleTextualCoding () {
  559. var code = CodeManagement.generate();
  560. $('.ivprog_visual_panel').css('display', 'none');
  561. $('.ivprog_textual_panel').css('display', 'block');
  562. $('.ivprog_textual_panel').removeClass('loading');
  563. $('.ivprog_textual_code').text(code);
  564. $('.visual_coding_button').removeClass('active');
  565. $('.textual_coding_button').addClass('active');
  566. }
  567. function toggleVisualCoding () {
  568. $('.ivprog_textual_panel').addClass('loading');
  569. $('.ivprog_textual_panel').css('display', 'none');
  570. $('.ivprog_visual_panel').css('display', 'block');
  571. $('.textual_coding_button').removeClass('active');
  572. $('.visual_coding_button').addClass('active');
  573. }
  574. function removeParameter (function_obj, parameter_obj, parameter_container) {
  575. var index = function_obj.parameters_list.indexOf(parameter_obj);
  576. if (index > -1) {
  577. window.insertContext = true;
  578. function_obj.parameters_list.splice(index, 1);
  579. }
  580. $(parameter_container).fadeOut();
  581. }
  582. function updateParameterType(parameter_obj, new_type, new_dimensions = 0) {
  583. parameter_obj.type = new_type;
  584. parameter_obj.dimensions = new_dimensions;
  585. if (new_dimensions > 0) {
  586. parameter_obj.rows = new_dimensions;
  587. parameter_obj.columns = 2;
  588. }
  589. }
  590. function renderParameter (function_obj, parameter_obj, function_container) {
  591. var ret = "";
  592. ret += '<div class="ui label function_name_parameter pink"><i class="ui icon ellipsis vertical inverted"></i>';
  593. ret += '<div class="ui dropdown parameter_type">';
  594. if (parameter_obj.dimensions > 0) {
  595. ret += '<div class="text">'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(parameter_obj.type);
  596. if (parameter_obj.dimensions == 1) {
  597. ret += ' [ ] ';
  598. } else {
  599. ret += ' [ ] [ ] ';
  600. }
  601. ret += '</div>';
  602. } else {
  603. ret += '<div class="text">'+LocalizedStrings.getUI(parameter_obj.type)+'</div>';
  604. }
  605. ret += '<div class="menu">';
  606. for (var tm in Types) {
  607. if (tm == Types.VOID.toUpperCase()) {
  608. continue;
  609. }
  610. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  611. }
  612. for (var tm in Types) {
  613. if (tm == Types.VOID.toUpperCase()) {
  614. continue;
  615. }
  616. ret += '<div class="item">'
  617. + '<i class="dropdown icon"></i>'
  618. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  619. + '<div class="menu">'
  620. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  621. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  622. + '</div>'
  623. + '</div>';
  624. }
  625. ret += '</div></div>';
  626. ret += '<div class="parameter_div_edit"><span class="span_name_parameter label_enable_name_parameter">'+parameter_obj.name+'</span></div> ';
  627. ret += ' <i class="yellow inverted icon times remove_parameter"></i></div>';
  628. ret = $(ret);
  629. function_container.find('.container_parameters_list').append(ret);
  630. ret.find('.remove_parameter').on('click', function(e){
  631. removeParameter(function_obj, parameter_obj, ret);
  632. });
  633. ret.find('.ui.dropdown.parameter_type').dropdown({
  634. onChange: function(value, text, $selectedItem) {
  635. if ($selectedItem.data('dimensions')) {
  636. updateParameterType(parameter_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  637. } else {
  638. updateParameterType(parameter_obj, Types[$selectedItem.data('type')]);
  639. }
  640. },
  641. selectOnKeydown: false
  642. });
  643. ret.find('.label_enable_name_parameter').on('click', function(e){
  644. enableNameParameterUpdate(parameter_obj, ret);
  645. });
  646. return ret;
  647. }
  648. var opened_name_parameter = false;
  649. var opened_input_parameter = null;
  650. function enableNameParameterUpdate (parameter_obj, parent_node) {
  651. if (opened_name_parameter) {
  652. opened_input_parameter.focus();
  653. return;
  654. }
  655. opened_name_parameter = true;
  656. parent_node = $(parent_node);
  657. var input_field;
  658. parent_node.find('.span_name_parameter').text('');
  659. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+parameter_obj.name+"' />" );
  660. input_field.insertBefore(parent_node.find('.span_name_parameter'));
  661. input_field.on('input', function() {
  662. var inputWidth = input_field.textWidth()+10;
  663. opened_input_parameter = input_field;
  664. input_field.focus();
  665. var tmpStr = input_field.val();
  666. input_field.val('');
  667. input_field.val(tmpStr);
  668. input_field.css({
  669. width: inputWidth
  670. })
  671. }).trigger('input');
  672. input_field.focusout(function() {
  673. /// update array:
  674. if (input_field.val().trim()) {
  675. parameter_obj.name = input_field.val().trim();
  676. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  677. }
  678. input_field.off();
  679. input_field.remove();
  680. /// update elements:
  681. opened_name_parameter = false;
  682. opened_input_parameter = false;
  683. });
  684. input_field.on('keydown', function(e) {
  685. var code = e.keyCode || e.which;
  686. if(code == 13) {
  687. if (input_field.val().trim()) {
  688. parameter_obj.name = input_field.val().trim();
  689. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  690. }
  691. input_field.off();
  692. input_field.remove();
  693. /// update elements:
  694. opened_name_parameter = false;
  695. opened_input_parameter = false;
  696. }
  697. if(code == 27) {
  698. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  699. input_field.off();
  700. input_field.remove();
  701. /// update elements:
  702. opened_name_parameter = false;
  703. opened_input_parameter = false;
  704. }
  705. });
  706. input_field.select();
  707. }
  708. var opened_name_function = false;
  709. var opened_input = null;
  710. var previousPadding = null;
  711. function enableNameFunctionUpdate (function_obj, parent_node) {
  712. if (opened_name_function) {
  713. opened_input.focus();
  714. return;
  715. }
  716. parent_node = $(parent_node);
  717. parent_node.find('.span_name_function').text('');
  718. var input_field;
  719. if (!previousPadding) {
  720. previousPadding = parent_node.find('.span_name_function').css('padding-left');
  721. }
  722. parent_node.find('.span_name_function').css('padding-left', '0');
  723. parent_node.find('.span_name_function').css('padding-right', '0');
  724. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+function_obj.name+"' />" );
  725. input_field.insertBefore(parent_node.find('.span_name_function'));
  726. input_field.on('input', function() {
  727. var inputWidth = input_field.textWidth()+10;
  728. opened_input = input_field;
  729. input_field.focus();
  730. var tmpStr = input_field.val();
  731. input_field.val('');
  732. input_field.val(tmpStr);
  733. input_field.css({
  734. width: inputWidth
  735. })
  736. }).trigger('input');
  737. input_field.focusout(function() {
  738. /// update array:
  739. if (input_field.val().trim()) {
  740. function_obj.name = input_field.val().trim();
  741. }
  742. input_field.off();
  743. input_field.remove();
  744. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  745. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  746. parent_node.find('.span_name_function').text(function_obj.name);
  747. /// update elements:
  748. opened_name_function = false;
  749. opened_input = false;
  750. });
  751. input_field.on('keydown', function(e) {
  752. var code = e.keyCode || e.which;
  753. if(code == 13) {
  754. if (input_field.val().trim()) {
  755. function_obj.name = input_field.val().trim();
  756. }
  757. input_field.off();
  758. input_field.remove();
  759. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  760. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  761. parent_node.find('.span_name_function').text(function_obj.name);
  762. /// update elements:
  763. opened_name_function = false;
  764. opened_input = false;
  765. }
  766. if(code == 27) {
  767. input_field.off();
  768. input_field.remove();
  769. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  770. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  771. parent_node.find('.span_name_function').text(function_obj.name);
  772. /// update elements:
  773. opened_name_function = false;
  774. opened_input = false;
  775. }
  776. });
  777. input_field.select();
  778. }