functions.js 38 KB

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