functions.js 35 KB

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