functions.js 33 KB

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