functions.js 36 KB

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