functions.js 38 KB

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