functions.js 36 KB

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