1
0

functions.js 33 KB

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