functions.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. import { Types } from './types';
  2. import * as Models from './ivprog_elements';
  3. import { LocalizedStrings } from './../services/localizedStringsService';
  4. import * as GlobalsManagement from './globals';
  5. import * as VariablesManagement from './variables';
  6. import * as CommandsManagement from './commands';
  7. import * as CodeManagement from './code_generator';
  8. import * as VariableValueMenu from './commands/variable_value_menu';
  9. import { DOMConsole } from './../io/domConsole';
  10. import { IVProgProcessor } from './../processor/ivprogProcessor';
  11. import WatchJS from 'melanke-watchjs';
  12. import { SemanticAnalyser } from '../processor/semantic/semanticAnalyser';
  13. import { IVProgAssessment } from '../assessment/ivprogAssessment';
  14. import * as AlgorithmManagement from './algorithm';
  15. import * as Utils from './utils';
  16. import { registerUserEvent, ActionTypes } from "./../services/userLog";
  17. import VersionInfo from './../../.ima_version.json';
  18. import * as TextEditor from "./text_editor";
  19. import { isValidIdentifier } from "./../util/utils";
  20. import { Modes } from '../processor/modes';
  21. var counter_new_functions = 0;
  22. var counter_new_parameters = 0;
  23. var ivprog_version = VersionInfo.version;
  24. const globalChangeListeners = [];
  25. const functionsChangeListeners = [];
  26. let proc = null;
  27. let domConsole = null;
  28. let _testCases = [];
  29. let isRunning = false;
  30. window.studentGrade = null;
  31. window.LocalizedStrings = LocalizedStrings;
  32. const program = new Models.Program();
  33. window.system_functions = [];
  34. // Adding math functions:
  35. window.system_functions.push(new Models.SystemFunction('$sin', 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('$cos', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  38. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  39. window.system_functions.push(new Models.SystemFunction('$tan', 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('$sqrt', 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('$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)],
  44. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  45. window.system_functions.push(new Models.SystemFunction('$log', 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('$abs', 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('$negate', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  50. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  51. window.system_functions.push(new Models.SystemFunction('$invert', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  52. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  53. window.system_functions.push(new Models.SystemFunction('$max', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  54. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  55. window.system_functions.push(new Models.SystemFunction('$min', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  56. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  57. window.system_functions.push(new Models.SystemFunction('$rand', Types.REAL, 0, [],
  58. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  59. // Adding text functions:
  60. window.system_functions.push(new Models.SystemFunction('$substring', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  61. 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. window.system_functions.push(new Models.SystemFunction('$length', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  64. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  65. window.system_functions.push(new Models.SystemFunction('$uppercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  66. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  67. window.system_functions.push(new Models.SystemFunction('$lowercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  68. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  69. window.system_functions.push(new Models.SystemFunction('$charAt', Types.CHAR, 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)],
  70. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  71. // Adding arrangement functions:
  72. 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)],
  73. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  74. 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)],
  75. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  76. 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)],
  77. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  78. // Adding conversion functions:
  79. window.system_functions.push(new Models.SystemFunction('$isReal', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  80. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  81. window.system_functions.push(new Models.SystemFunction('$isInt', 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('$isBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  84. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  85. window.system_functions.push(new Models.SystemFunction('$castReal', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  86. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  87. window.system_functions.push(new Models.SystemFunction('$castInt', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  88. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  89. window.system_functions.push(new Models.SystemFunction('$castBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  90. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  91. window.system_functions.push(new Models.SystemFunction('$castString', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  92. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  93. window.system_functions.push(new Models.SystemFunction('$castChar', Types.CHAR, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  94. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  95. 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');
  96. const mainFunction = new Models.Function(LocalizedStrings.getUI("start"), Types.VOID, 0, [], true, false);
  97. mainFunction.function_comment = new Models.Comment(LocalizedStrings.getUI('text_comment_main'));
  98. program.addFunction(mainFunction);
  99. window.program_obj = program;
  100. window.generator = CodeManagement.generate;
  101. window.runCodeAssessment = runCodeAssessment;
  102. window.renderAlgorithm = AlgorithmManagement.renderAlgorithm;
  103. window.insertContext = false;
  104. window.watchW = WatchJS;
  105. WatchJS.watch(window.program_obj.globals, function(){
  106. if (window.insertContext) {
  107. setTimeout(function() {
  108. AlgorithmManagement.renderAlgorithm();
  109. globalChangeListeners.forEach(x => x());
  110. }, 300);
  111. window.insertContext = false;
  112. } else {
  113. AlgorithmManagement.renderAlgorithm();
  114. globalChangeListeners.forEach(x => x());
  115. }
  116. }, 1);
  117. WatchJS.watch(window.program_obj.functions, function(){
  118. if (window.insertContext) {
  119. setTimeout(function(){
  120. AlgorithmManagement.renderAlgorithm();
  121. functionsChangeListeners.forEach( x => x());
  122. }, 300);
  123. window.insertContext = false;
  124. } else {
  125. AlgorithmManagement.renderAlgorithm();
  126. functionsChangeListeners.forEach( x => x());
  127. }
  128. }, 1);
  129. function addFunctionHandler () {
  130. const 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')));
  131. program.addFunction(new_function);
  132. counter_new_functions ++;
  133. window.insertContext = true;
  134. registerUserEvent(new_function.name, ActionTypes.INSERT_FUNCTION);
  135. // var newe = renderFunction(new_function);
  136. renderFunction(new_function);
  137. /*newe.css('display', 'none');
  138. newe.fadeIn();*/
  139. }
  140. function addParameter (function_obj, function_container/*, is_from_click = false*/) {
  141. if (function_obj.parameters_list == null) {
  142. function_obj.parameters_list = [];
  143. }
  144. const new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
  145. function_obj.parameters_list.push(new_parameter);
  146. counter_new_parameters ++;
  147. registerUserEvent(function_obj.name, ActionTypes.INSERT_FUNCTION_PARAM, new_parameter.name, Types.INTEGER, 0);
  148. //var newe = renderParameter(function_obj, new_parameter, function_container);
  149. renderParameter(function_obj, new_parameter, function_container);
  150. // if (is_from_click) {
  151. // newe.css('display', 'none');
  152. // newe.fadeIn();
  153. // }
  154. }
  155. function updateReturnType (function_obj, new_type, new_dimensions = 0) {
  156. registerUserEvent(function_obj.name, ActionTypes.CHANGE_FUNCTION_RETURN, new_type, new_dimensions);
  157. function_obj.return_type = new_type;
  158. function_obj.return_dimensions = new_dimensions;
  159. }
  160. function removeFunction (function_obj) {
  161. var index = program.functions.indexOf(function_obj);
  162. if (index > -1) {
  163. registerUserEvent(function_obj.name, ActionTypes.REMOVE_FUNCTION);
  164. program.functions.splice(index, 1);
  165. }
  166. }
  167. function minimizeFunction (function_obj) {
  168. function_obj.is_hidden = !function_obj.is_hidden;
  169. }
  170. function addHandlers (function_obj, function_container) {
  171. function_container.find('.ui.dropdown.function_return').dropdown({
  172. onChange: function(value, text, $selectedItem) {
  173. if ($selectedItem.data('dimensions')) {
  174. updateReturnType(function_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  175. } else {
  176. updateReturnType(function_obj, Types[$selectedItem.data('type')]);
  177. }
  178. },
  179. selectOnKeydown: false
  180. });
  181. function_container.find( ".name_function_updated" ).on('click', function(e){
  182. enableNameFunctionUpdate(function_obj, function_container);
  183. });
  184. function_container.find( ".add_parameter_button" ).on('click', function(e){
  185. window.insertContext = true;
  186. addParameter(function_obj, function_container, true);
  187. });
  188. function_container.find('.menu_commands').dropdown({
  189. on: 'hover'
  190. });
  191. function_container.find('.menu_commands a').on('click', function(evt){
  192. if (function_obj.commands == null || function_obj.commands.length == 0) {
  193. function_obj.commands = [];
  194. var new_cmd = CommandsManagement.genericCreateCommand($(this).data('command'));
  195. function_obj.commands.push(new_cmd);
  196. CommandsManagement.renderCommand(new_cmd, function_container.find('.commands_list_div'), 3, function_obj);
  197. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, $(this).data('command'), '/', 0);
  198. } else {
  199. CommandsManagement.createFloatingCommand(function_obj, function_container, $(this).data('command'), evt);
  200. }
  201. });
  202. function_container.find('.add_var_button_function').on('click', function(e){
  203. window.insertContext = true;
  204. VariablesManagement.addVariable(function_obj, function_container, true);
  205. });
  206. function_container.find('.remove_function_button').on('click', function(e){
  207. removeFunction(function_obj);
  208. function_container.fadeOut();
  209. });
  210. function_container.find('.minimize_function_button').on('click', function(e){
  211. minimizeFunction(function_obj);
  212. if (function_obj.is_hidden) {
  213. function_container.find(".add_var_button_function").toggle();
  214. function_container.find(".inline_add_command").toggle();
  215. function_container.find(".function_area").slideToggle();
  216. } else {
  217. function_container.find(".function_area").slideToggle(function(){
  218. function_container.find(".add_var_button_function").toggle();
  219. function_container.find(".inline_add_command").toggle();
  220. });
  221. }
  222. });
  223. }
  224. // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
  225. function renderFunctionReturn (function_obj, function_element) {
  226. var ret = '<div class="ui dropdown function_return">';
  227. if (function_obj.return_dimensions == 1) {
  228. ret += '<div class="text">'+ LocalizedStrings.getUI("vector") +': '+ LocalizedStrings.getUI(`type_${function_obj.return_type.toLowerCase()}`);
  229. ret += ' [ ] </div>';
  230. } else if (function_obj.return_dimensions == 2) {
  231. ret += '<div class="text">'+ LocalizedStrings.getUI("matrix") +': '+ LocalizedStrings.getUI(`type_${function_obj.return_type.toLowerCase()}`);
  232. ret += ' [ ] [ ] </div>';
  233. } else {
  234. ret += '<div class="text">'+LocalizedStrings.getUI(`type_${function_obj.return_type.toLowerCase()}`)+'</div>';
  235. }
  236. ret += '<div class="menu">';
  237. for (var tm in Types) {
  238. ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions < 1 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  239. }
  240. ret += '<div class="item ' + (function_obj.return_dimensions == 1 ? ' selected ' : '') + '">'
  241. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('vector')
  242. + '<div class="menu">';
  243. for (var tm in Types) {
  244. if (tm == Types.VOID.toUpperCase()) {
  245. continue;
  246. }
  247. ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions == 1 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="1"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  248. }
  249. ret += '</div></div>';
  250. ret += '<div class="item ' + (function_obj.return_dimensions == 2 ? ' selected ' : '') + '">'
  251. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('matrix')
  252. + '<div class="menu">';
  253. for (var tm in Types) {
  254. if (tm == Types.VOID.toUpperCase()) {
  255. continue;
  256. }
  257. ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions == 2 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('matrix')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="2"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  258. }
  259. ret += '</div></div>';
  260. ret += '</div></div>';
  261. ret = $(ret);
  262. function_element.find('.function_return').append(ret);
  263. }
  264. var cont = 0;
  265. export function renderFunction (function_obj) {
  266. var appender = '<div class="ui secondary segment function_div list-group-item function_cont_'+cont+'">';
  267. if (function_obj.function_comment) {
  268. //appender += renderComment(function_obj.function_comment, sequence, true, -1);
  269. }
  270. appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
  271. 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>')
  272. + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
  273. appender += '<div class="function_signature_div">'+LocalizedStrings.getUI("function")+' ';
  274. if (function_obj.is_main) {
  275. appender += '<div class="function_name_div"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' + LocalizedStrings.getUI('type_void') + ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="span_name_function" >'+function_obj.name+'</span> </div> '
  276. + ' <span class="parethesis_function">( </span> <div class="ui large labels parameters_list">';
  277. } else {
  278. appender += '<div class="ui function_return"></div>';
  279. appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">'+function_obj.name+'</span> </div> '
  280. + ' <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">';
  281. }
  282. appender += '</div> <span class="parethesis_function"> ) </span> </div>'
  283. + (function_obj.is_hidden ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ');
  284. appender += '<div class="ui add_var_context add_var_button_function" style="float: left;"> <div class="ui icon button purple"> ';
  285. appender += '<i class="icons"><i class="icon superscript" style="margin-top: -2px;margin-bottom: 2px;margin-left: 1px;margin-right: 1px;"></i><i class="corner add icon inverted" style="font-size: 9px;padding-top: 6px;padding-left: 9px;"></i></i>';
  286. appender += '</div></div>';
  287. appender += '<div class="ui top attached segment variables_list_div"></div>';
  288. appender += '<div class="ui bottom attached segment commands_list_div commands_cont_'+cont+'">'
  289. + '<div class="ui rail" style="width: 35px; margin-left: -36px;"><div class="ui sticky sticky_cont_'+cont+'" style="top: 50px !important;">';
  290. appender += '<div class="ui icon button dropdown menu_commands orange" > '
  291. + '<i class="icons"><i class="icon code" style="margin-top: -1px;margin-bottom: 1px;margin-right: 0px;"></i><i class="corner add icon inverted" style="font-size: 9px;padding-top: 6px;padding-left: 9px;"></i></i>'
  292. + '<div class="menu"> ';
  293. appender += '<a class="item" data-command="'+Models.COMMAND_TYPES.reader+'"><i class="download icon"></i> ' +LocalizedStrings.getUI('text_read_var')+ '</a>'
  294. + '<a class="item" data-command="'+Models.COMMAND_TYPES.writer+'"><i class="upload icon"></i> '+LocalizedStrings.getUI('text_write_var')+'</a>'
  295. + '<a class="item" data-command="'+Models.COMMAND_TYPES.comment+'"><i class="quote left icon"></i> '+LocalizedStrings.getUI('text_comment')+'</a>'
  296. + '<a class="item" data-command="'+Models.COMMAND_TYPES.attribution+'"><i class="arrow left icon"></i> '+LocalizedStrings.getUI('text_attribution')+'</a>'
  297. + '<a class="item" data-command="'+Models.COMMAND_TYPES.functioncall+'"><i class="hand point right icon"></i> '+LocalizedStrings.getUI('text_functioncall')+'</a>'
  298. + '<a class="item" data-command="'+Models.COMMAND_TYPES.iftrue+'" ><i class="random icon"></i> '+LocalizedStrings.getUI('text_iftrue')+'</a>'
  299. + '<a class="item" data-command="'+Models.COMMAND_TYPES.repeatNtimes+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_repeatNtimes')+'</a>'
  300. + '<a class="item" data-command="'+Models.COMMAND_TYPES.whiletrue+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_whiletrue')+'</a>'
  301. + '<a class="item" data-command="'+Models.COMMAND_TYPES.dowhiletrue+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_dowhiletrue')+'</a>'
  302. + '<a class="item" data-command="'+Models.COMMAND_TYPES.switch+'"><i class="list icon"></i> '+LocalizedStrings.getUI('text_switch')+'</a>'
  303. + '<a class="item" data-command="'+Models.COMMAND_TYPES.return+'"><i class="reply icon"></i> '+LocalizedStrings.getUI('text_btn_return')+'</a>'
  304. + '</div></div>';
  305. appender += '</div></div>'
  306. +'</div>';
  307. appender += '</div></div>';
  308. appender = $(appender);
  309. $('.all_functions').append(appender);
  310. appender.data('fun', function_obj);
  311. appender.find('.commands_list_div').data('fun', function_obj);
  312. renderFunctionReturn(function_obj, appender);
  313. addHandlers(function_obj, appender);
  314. // Rendering parameters:
  315. for (var j = 0; j < function_obj.parameters_list.length; j++) {
  316. renderParameter(function_obj, function_obj.parameters_list[j], appender);
  317. }
  318. // Rendering variables:
  319. for (var j = 0; j < function_obj.variables_list.length; j++) {
  320. VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
  321. }
  322. // Rendering commands:
  323. for (var j = 0; j < function_obj.commands.length; j++) {
  324. CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
  325. }
  326. appender.find('.minimize_function_button').popup({
  327. content : LocalizedStrings.getUI("tooltip_hide_function"),
  328. delay: {
  329. show: 750,
  330. hide: 0
  331. }
  332. });
  333. appender.find('.move_function').popup({
  334. content : LocalizedStrings.getUI("btn_drag_and_drop_function"),
  335. delay: {
  336. show: 1000,
  337. hide: 0
  338. }
  339. });
  340. appender.find('.remove_function_button').popup({
  341. content : LocalizedStrings.getUI("btn_delete_function"),
  342. delay: {
  343. show: 750,
  344. hide: 0
  345. }
  346. });
  347. appender.find('.add_var_button_function').popup({
  348. content : LocalizedStrings.getUI("btn_add_var"),
  349. delay: {
  350. show: 750,
  351. hide: 0
  352. }
  353. });
  354. appender.find('a[data-command="attribution"]').popup({
  355. content : LocalizedStrings.getUI("tooltip_menu_attribution"),
  356. position: 'right center',
  357. delay: {
  358. show: 750,
  359. hide: 0
  360. }
  361. });
  362. appender.find('a[data-command="functioncall"]').popup({
  363. content : LocalizedStrings.getUI("tooltip_menu_call_function"),
  364. position: 'right center',
  365. delay: {
  366. show: 750,
  367. hide: 0
  368. }
  369. });
  370. appender.find('a[data-command="iftrue"]').popup({
  371. content : LocalizedStrings.getUI("tooltip_menu_if_else"),
  372. position: 'right center',
  373. delay: {
  374. show: 750,
  375. hide: 0
  376. }
  377. });
  378. appender.find('a[data-command="comment"]').popup({
  379. content : LocalizedStrings.getUI("tooltip_menu_comments"),
  380. position: 'right center',
  381. delay: {
  382. show: 750,
  383. hide: 0
  384. }
  385. });
  386. appender.find('a[data-command="reader"]').popup({
  387. content : LocalizedStrings.getUI("tooltip_menu_read"),
  388. position: 'right center',
  389. delay: {
  390. show: 750,
  391. hide: 0
  392. }
  393. });
  394. appender.find('a[data-command="writer"]').popup({
  395. content : LocalizedStrings.getUI("tooltip_menu_write"),
  396. position: 'right center',
  397. delay: {
  398. show: 750,
  399. hide: 0
  400. }
  401. });
  402. appender.find('a[data-command="repeatNtimes"]').popup({
  403. content : LocalizedStrings.getUI("tooltip_menu_repeat_n_times"),
  404. position: 'right center',
  405. delay: {
  406. show: 750,
  407. hide: 0
  408. }
  409. });
  410. appender.find('a[data-command="whiletrue"]').popup({
  411. content : LocalizedStrings.getUI("tooltip_menu_repeat_whiletrue"),
  412. position: 'right center',
  413. delay: {
  414. show: 750,
  415. hide: 0
  416. }
  417. });
  418. appender.find('a[data-command="dowhiletrue"]').popup({
  419. content : LocalizedStrings.getUI("tooltip_menu_repeat_dowhiletrue"),
  420. position: 'right center',
  421. delay: {
  422. show: 750,
  423. hide: 0
  424. }
  425. });
  426. appender.find('a[data-command="switch"]').popup({
  427. content : LocalizedStrings.getUI("tooltip_menu_repeat_switch"),
  428. position: 'right center',
  429. delay: {
  430. show: 750,
  431. hide: 0
  432. }
  433. });
  434. appender.find('a[data-command="return"]').popup({
  435. content : LocalizedStrings.getUI("tooltip_menu_repeat_return"),
  436. position: 'right center',
  437. delay: {
  438. show: 750,
  439. hide: 0
  440. }
  441. });
  442. var function_index = program.functions.indexOf(function_obj);
  443. Sortable.create(appender.find(".variables_list_div")[0], {
  444. handle: '.ellipsis',
  445. animation: 100,
  446. ghostClass: 'ghost',
  447. group: 'local_vars_drag_' + function_index,
  448. onEnd: function (evt) {
  449. updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  450. }
  451. });
  452. addSortableHandler(appender.find(".commands_list_div")[0], function_index);
  453. if (!function_obj.is_main) {
  454. Sortable.create(appender.find(".container_parameters_list")[0], {
  455. handle: '.ellipsis',
  456. animation: 100,
  457. ghostClass: 'ghost',
  458. group: 'parameters_drag_' + program.functions.indexOf(function_obj),
  459. onEnd: function (evt) {
  460. updateSequenceParameters(evt.oldIndex, evt.newIndex, function_obj);
  461. }
  462. });
  463. }
  464. if (function_obj.commands.length > 0) {
  465. var teste = '.ui.sticky.sticky_cont_'+cont;
  466. $(teste).sticky({
  467. context: '.ui.bottom.attached.segment.commands_list_div.commands_cont_'+cont,
  468. scrollContext: '.ivprog_visual_panel',
  469. observeChanges: true,
  470. offset: 40,
  471. onStick: function (evt) {
  472. $(teste).css('top', '20px', 'important');
  473. },
  474. onBottom: function (evt) {
  475. $(teste).css('top', '20px', 'important');
  476. },
  477. onUnstick: function (evt) {
  478. $(teste).css('top', '20px', 'important');
  479. },
  480. onReposition: function (evt) {
  481. $(teste).css('top', '20px', 'important');
  482. },
  483. onScroll: function (evt) {
  484. $(teste).css('top', '20px', 'important');
  485. if (!isVisible($(teste), $(teste).parent())) {
  486. $(teste).removeClass('fixed');
  487. }
  488. },
  489. onTop: function (evt) {
  490. $(teste).css('top', '20px', 'important');
  491. }
  492. });
  493. }
  494. cont ++;
  495. appender.find('.add_parameter_button').popup({
  496. content : LocalizedStrings.getUI("text_add_parameters"),
  497. delay: {
  498. show: 750,
  499. hide: 0
  500. }
  501. });
  502. return appender;
  503. }
  504. function isVisible (element, container) {
  505. var elementTop = $(element).offset().top,
  506. elementHeight = $(element).height(),
  507. containerTop = $(container).offset().top,
  508. containerHeight = $(container).height() - 30;
  509. return ((((elementTop - containerTop) + elementHeight) > 0)
  510. && ((elementTop - containerTop) < containerHeight));
  511. }
  512. window.evento_drag;
  513. function updateProgramObjDrag () {
  514. const nodes = Array.prototype.slice.call( $('.all_functions').children() );
  515. let function_index;
  516. var start_index;
  517. let function_obj;
  518. $(evento_drag.item).parentsUntil(".all_functions").each(function (index) {
  519. const ref = $(this);
  520. if (ref.hasClass('function_div')) {
  521. function_index = nodes.indexOf(this);
  522. start_index = index;
  523. function_obj = ref;
  524. }
  525. });
  526. const path_target = [];
  527. $(evento_drag.item).parentsUntil(".all_functions").each(function () {
  528. if ($(this).hasClass('command_container')) {
  529. path_target.push(this);
  530. }
  531. });
  532. if (path_target.length == 0) {
  533. //console.log('soltou na raiz, na posição: ' + evento_drag.newIndex + ' mas ainda não sei de onde saiu ');
  534. } else {
  535. //console.log('soltou dentro de algum bloco, sequência vem logo abaixo (de baixo pra cima): ');
  536. //console.log(path_target);
  537. }
  538. var index_each = [];
  539. var path_relative = [];
  540. for (var i = path_target.length - 1; i >= 0; i --) {
  541. if (i == (path_target.length - 1)) { // está na raiz
  542. var indice_na_raiz = function_obj.find('.command_container').index(path_target[i]);
  543. } else {
  544. if ($(path_target[i + 1]).hasClass('iftrue')) {
  545. if ($(path_target[i]).parent().hasClass('commands_if')) {
  546. path_relative.push('if');
  547. index_each.push($(path_target[i]).parent().find('.command_container').index(path_target[i]));
  548. } else {
  549. path_relative.push('else');
  550. index_each.push($(path_target[i]).parent().find('.command_container').index(path_target[i]));
  551. }
  552. } else if ($(path_target[i + 1]).hasClass('dowhiletrue')) {
  553. path_relative.push('dowhiletrue');
  554. index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  555. } else if ($(path_target[i + 1]).hasClass('repeatNtimes')) {
  556. path_relative.push('repeatNtimes');
  557. index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  558. } else if ($(path_target[i + 1]).hasClass('whiletrue')) {
  559. path_relative.push('whiletrue');
  560. index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  561. } else if ($(path_target[i + 1]).hasClass('switch')) {
  562. path_relative.push('switch');
  563. //index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  564. }
  565. }
  566. }
  567. // var index_in_block = -1;
  568. const is_in_else = $(evento_drag.item).parent().hasClass('commands_else');
  569. // index_in_block = $(evento_drag.item).parent().find('.command_container').index(evento_drag.item);
  570. const is_in_case_switch = $(evento_drag.item).parent().hasClass('case_commands_block');
  571. // encontrar o elemento na árvore:
  572. var command_start_point = window.program_obj.functions[function_index].commands[indice_na_raiz];
  573. var block_to_insert = command_start_point;
  574. for (var i = 0; i < index_each.length; i++) {
  575. if (path_relative[i] == "else") {
  576. block_to_insert = block_to_insert.commands_else[index_each[i]];
  577. } else if (path_relative[i] == "switch") {
  578. } else {
  579. block_to_insert = block_to_insert.commands_block[index_each[i]]
  580. }
  581. }
  582. //console.log('command_start_point', command_start_point);
  583. //console.log('block_to_insert', block_to_insert);
  584. // agora tem que alocar o comando na árvore, mas considerar as quatro situações:
  585. // (1) se está em um else ou (2) se está em switch ou (3) será um caso padrão ou (4) se será na raiz.
  586. if (path_target.length == 0) { // soltou na raiz:
  587. window.program_obj.functions[function_index].commands.splice(evento_drag.newIndex - 1, 0, command_in_drag);
  588. } else if (is_in_else) {
  589. if (block_to_insert.commands_else) {
  590. block_to_insert.commands_else.splice(evento_drag.newIndex, 0, command_in_drag);
  591. } else {
  592. block_to_insert.commands_else = [];
  593. block_to_insert.commands_else.push(command_in_drag);
  594. }
  595. } else if (is_in_case_switch) {
  596. } else {
  597. // verificar se tem alguma coisa no bloco:
  598. if (block_to_insert.commands_block) {
  599. block_to_insert.commands_block.splice(evento_drag.newIndex, 0, command_in_drag);
  600. } else {
  601. block_to_insert.commands_block = [];
  602. block_to_insert.commands_block.push(command_in_drag);
  603. }
  604. }
  605. window.draging = false;
  606. renderAlgorithm();
  607. }
  608. function prepareDragHandler (evt) {
  609. window.draging = true;
  610. var nodes = Array.prototype.slice.call( $('.all_functions').children() );
  611. var function_index;
  612. var function_obj;
  613. $(evt.item).parentsUntil(".all_functions").each(function (index) {
  614. if ($(this).hasClass('function_div')) {
  615. function_index = nodes.indexOf(this);
  616. function_obj = window.program_obj.functions[function_index];
  617. }
  618. });
  619. command_in_drag = $(evt.item).data("command");
  620. //console.log('$(evt.item).parent(): ');
  621. //console.log($(evt.item).parent());
  622. // descobrir qual das quatro situações:
  623. if ($(evt.item).parent().hasClass('commands_list_div')) { // está na raiz:
  624. if (function_obj.commands.indexOf(command_in_drag) > -1) {
  625. function_obj.commands.splice(function_obj.commands.indexOf(command_in_drag), 1);
  626. }
  627. } else if ($(evt.item).parent().hasClass('commands_else')) { // está no else:
  628. if ($(evt.item).parent().data('command').commands_else.indexOf(command_in_drag) > -1) {
  629. $(evt.item).parent().data('command').commands_else.splice($(evt.item).parent().data('command').commands_else.indexOf(command_in_drag), 1);
  630. }
  631. } else if ($(evt.item).parent().hasClass('case_commands_block')) { // está em um switch:
  632. } else { // caso padrão:
  633. if ($(evt.item).parent().data('command').commands_block.indexOf(command_in_drag) > -1) {
  634. $(evt.item).parent().data('command').commands_block.splice($(evt.item).parent().data('command').commands_block.indexOf(command_in_drag), 1);
  635. }
  636. }
  637. }
  638. var command_in_drag;
  639. function addSortableHandler (element, id_function) {
  640. var n_group = 'commands_drag_' + id_function;
  641. Sortable.create(element, {
  642. handle: '.command_drag',
  643. ghostClass: 'ghost',
  644. animation: 300,
  645. group: {name: n_group},
  646. onEnd: function (evt) {
  647. var nodes = Array.prototype.slice.call( $('.all_functions').children() );
  648. var function_index;
  649. var function_obj;
  650. $(evt.item).parentsUntil(".all_functions").each(function (index) {
  651. if ($(this).hasClass('function_div')) {
  652. function_index = nodes.indexOf(this);
  653. function_obj = window.program_obj.functions[function_index];
  654. }
  655. });
  656. registerUserEvent(function_obj.name, ActionTypes.MOVE_COMMAND, $(evt.item).data('command').type, '/', 'from: ' + evt.oldIndex + ' to: ' + evt.newIndex);
  657. //updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  658. var itemEl = evt.item; // dragged HTMLElement
  659. evt.to; // target list
  660. evt.from; // previous list
  661. evt.oldIndex; // element's old index within old parent
  662. evt.newIndex; // element's new index within new parent
  663. //console.log('::EVT::');
  664. //console.log(evt);
  665. window.evento_drag = evt;
  666. try {
  667. updateProgramObjDrag();
  668. } catch (e) {
  669. console.error(e);
  670. window.draging = false;
  671. }
  672. },
  673. onStart: function (evt) {
  674. //console.log("START::EVT::");
  675. //console.log(evt);
  676. //console.log("\n\ncommand_in_drag");
  677. try {
  678. prepareDragHandler(evt);
  679. } catch (e) {
  680. window.draging = false;
  681. }
  682. }
  683. });
  684. element = $(element);
  685. element.find(".iftrue").each(function( index ) {
  686. addSortableHandler($(this).find(".block_commands")[0], id_function);
  687. addSortableHandler($(this).find(".block_commands")[1], id_function);
  688. });
  689. element.find(".repeatNtimes").each(function( index ) {
  690. addSortableHandler($(this).find(".block_commands")[0], id_function);
  691. });
  692. element.find(".dowhiletrue").each(function( index ) {
  693. addSortableHandler($(this).find(".block_commands")[0], id_function);
  694. });
  695. element.find(".whiletrue").each(function( index ) {
  696. addSortableHandler($(this).find(".block_commands")[0], id_function);
  697. });
  698. element.find(".switch").each(function( index ) {
  699. $(this).find(".case_div").each(function( index ) {
  700. addSortableHandler($(this).find(".case_commands_block")[0], id_function);
  701. });
  702. });
  703. }
  704. export function initVisualUI () {
  705. // MUST USE CONST, LET, OR VAR !!!!!!
  706. // const mainDiv = $('#visual-main-div');
  707. // fill mainDiv with functions and globals...
  708. // renderAlgorithm()...
  709. domConsole = new DOMConsole("ivprog-term-div");
  710. domConsole.hide();
  711. $(document.getElementById("ivprog-term-div")).draggable()
  712. $('.add_function_button').on('click', () => {
  713. addFunctionHandler();
  714. }).popup({
  715. content : LocalizedStrings.getUI("text_add_function"),
  716. delay: {
  717. show: 750,
  718. hide: 0
  719. }
  720. });
  721. $('.add_global_button').on('click', () => {
  722. window.insertContext = true;
  723. GlobalsManagement.addGlobal(program, true);
  724. });
  725. $('#run_button').on('click', function () {
  726. showStopButton();
  727. runCode();
  728. });
  729. $('#download_file_button').on('click', function () {
  730. downloadFile();
  731. });
  732. $('#upload_file_button').on('click', function () {
  733. $('#ivph_file').click().off('change').change(uploadFile);
  734. });
  735. $('#stop_button').on('click', function () {
  736. showRunButton();
  737. stopExecution();
  738. });
  739. $('.visual_coding_button').on('click', () => {
  740. toggleVisualCoding();
  741. });
  742. $('.textual_coding_button').on('click', () => {
  743. toggleTextualCoding();
  744. });
  745. $('.assessment').on('click', () => {
  746. is_iassign = true;
  747. runCodeAssessment();
  748. });
  749. $('.div_toggle_console').on('click', () => {
  750. toggleConsole();
  751. });
  752. $('.expand_button').on('click', () => {
  753. full_screen();
  754. });
  755. $('.help_button').on('click', () => {
  756. window.open('https://www.usp.br/line/ivprog/', '_blank');
  757. });
  758. $('.main_title h2').prop('title', LocalizedStrings.getUI('text_ivprog_description'));
  759. var time_show = 750;
  760. $('.visual_coding_button').popup({
  761. content : LocalizedStrings.getUI("tooltip_visual"),
  762. delay: {
  763. show: time_show,
  764. hide: 0
  765. }
  766. });
  767. $('.textual_coding_button').popup({
  768. content : LocalizedStrings.getUI("tooltip_textual"),
  769. delay: {
  770. show: time_show,
  771. hide: 0
  772. }
  773. });
  774. $('.upload_file_button').popup({
  775. content : LocalizedStrings.getUI("tooltip_upload"),
  776. delay: {
  777. show: time_show,
  778. hide: 0
  779. }
  780. });
  781. $('.download_file_button').popup({
  782. content : LocalizedStrings.getUI("tooltip_download"),
  783. delay: {
  784. show: time_show,
  785. hide: 0
  786. }
  787. });
  788. $('.undo_button').popup({
  789. content : LocalizedStrings.getUI("tooltip_undo"),
  790. delay: {
  791. show: time_show,
  792. hide: 0
  793. }
  794. });
  795. $('.redo_button').popup({
  796. content : LocalizedStrings.getUI("tooltip_redo"),
  797. delay: {
  798. show: time_show,
  799. hide: 0
  800. }
  801. });
  802. $('.run_button').popup({
  803. content : LocalizedStrings.getUI("tooltip_run"),
  804. delay: {
  805. show: time_show,
  806. hide: 0
  807. }
  808. });
  809. $('.assessment_button').popup({
  810. content : LocalizedStrings.getUI("tooltip_evaluate"),
  811. delay: {
  812. show: time_show,
  813. hide: 0
  814. }
  815. });
  816. $('.help_button').popup({
  817. content : LocalizedStrings.getUI("tooltip_help") + ' - ' + LocalizedStrings.getUI("text_ivprog_version") + ' ' + ivprog_version,
  818. delay: {
  819. show: time_show,
  820. hide: 0
  821. }
  822. });
  823. $('.add_global_button').popup({
  824. content : LocalizedStrings.getUI("tooltip_add_global"),
  825. delay: {
  826. show: time_show,
  827. hide: 0
  828. }
  829. });
  830. $('.div_toggle_console').popup({
  831. content : LocalizedStrings.getUI("tooltip_console"),
  832. delay: {
  833. show: time_show,
  834. hide: 0
  835. }
  836. });
  837. Sortable.create(listWithHandle, {
  838. handle: '.glyphicon-move',
  839. animation: 100,
  840. ghostClass: 'ghost',
  841. group: 'functions_divs_drag',
  842. onEnd: function (evt) {
  843. updateSequenceFunction(evt.oldIndex, evt.newIndex);
  844. }
  845. });
  846. var listGlobalsHandle = document.getElementById("listGlobalsHandle");
  847. Sortable.create(listGlobalsHandle, {
  848. handle: '.ellipsis',
  849. animation: 100,
  850. ghostClass: 'ghost',
  851. group: 'globals_divs_drag',
  852. onEnd: function (evt) {
  853. updateSequenceGlobals(evt.oldIndex, evt.newIndex);
  854. }
  855. });
  856. }
  857. export function setTestCases (testCases) {
  858. _testCases = testCases;
  859. }
  860. export function getTestCases () {
  861. // Deep clone of test cases to avoid unauthorized modification
  862. // TODO: It may be not possible to use this once custom test are fully implemented
  863. return JSON.parse(JSON.stringify(_testCases));
  864. }
  865. var is_iassign = false;
  866. function updateSequenceParameters (oldIndex, newIndex, function_obj) {
  867. function_obj.parameters_list.splice(newIndex, 0, function_obj.parameters_list.splice(oldIndex, 1)[0]);
  868. }
  869. function updateSequenceLocals (oldIndex, newIndex, function_obj) {
  870. function_obj.variables_list.splice(newIndex, 0, function_obj.variables_list.splice(oldIndex, 1)[0]);
  871. }
  872. function updateSequenceGlobals (oldIndex, newIndex) {
  873. program_obj.globals.splice(newIndex, 0, program_obj.globals.splice(oldIndex, 1)[0]);
  874. }
  875. function updateSequenceFunction (oldIndex, newIndex) {
  876. program_obj.functions.splice(newIndex, 0, program_obj.functions.splice(oldIndex, 1)[0]);
  877. }
  878. function runCodeAssessment () {
  879. if (isRunning) {
  880. // cannot run assessment or it's already running
  881. return -1;
  882. }
  883. let strCode = null;
  884. is_iassign = false;
  885. window.studentGrade = null;
  886. if (settingsProgrammingTypes == "textual") {
  887. strCode = TextEditor.getCode();
  888. } else {
  889. strCode = CodeManagement.generate();
  890. }
  891. if (strCode == null) {
  892. // No code, so no assessment
  893. return -1;
  894. }
  895. toggleConsole(true);
  896. try {
  897. const data = SemanticAnalyser.analyseFromSource(strCode);
  898. isRunning = true;
  899. const runner = new IVProgAssessment(data, _testCases, domConsole);
  900. runner.runTest().then(grade => {
  901. if (!is_iassign) {
  902. parent.getEvaluationCallback(grade);
  903. } else {
  904. is_iassign = false;
  905. }
  906. isRunning = false;
  907. }).catch( err => {
  908. console.log(err);
  909. isRunning = false;
  910. });
  911. } catch (error) {
  912. isRunning = false;
  913. domConsole.err(error.message);
  914. console.log(error);
  915. if (!is_iassign) {
  916. parent.getEvaluationCallback(0);
  917. } else {
  918. is_iassign = false;
  919. }
  920. }
  921. return 0;
  922. }
  923. function runCode () {
  924. if (isRunning) {
  925. return;
  926. }
  927. let strCode = null;
  928. if (settingsProgrammingTypes == "textual") {
  929. strCode = TextEditor.getCode();
  930. } else {
  931. strCode = CodeManagement.generate();
  932. }
  933. if (strCode == null) {
  934. return;
  935. }
  936. toggleConsole(true);
  937. // if(domConsole == null)
  938. // domConsole = new DOMConsole("#ivprog-term");
  939. //$("#ivprog-term").slideDown(500);
  940. try {
  941. const data = SemanticAnalyser.analyseFromSource(strCode);
  942. proc = new IVProgProcessor(data);
  943. proc.registerInput(domConsole);
  944. proc.registerOutput(domConsole);
  945. $("#ivprog-term").addClass('ivprog-term-active');
  946. isRunning = true;
  947. proc.interpretAST().then( _ => {
  948. scheduleCall(() => {
  949. if(domConsole.pending_writes.length == 0) {
  950. if(proc.mode === Modes.ABORT) {
  951. domConsole.info(LocalizedStrings.getMessage("aborted_execution"));
  952. } else {
  953. domConsole.info(LocalizedStrings.getMessage("success_execution"));
  954. }
  955. $("#ivprog-term").removeClass('ivprog-term-active');
  956. isRunning = false;
  957. proc = null;
  958. showRunButton();
  959. return true;
  960. }
  961. return false;
  962. },100);
  963. }).catch(err => {
  964. scheduleCall(() => {
  965. if(domConsole.pending_writes.length == 0) {
  966. if(err instanceof Error) {
  967. domConsole.err(err.message);
  968. } else {
  969. domConsole.err(err);
  970. }
  971. $("#ivprog-term").removeClass('ivprog-term-active');
  972. isRunning = false;
  973. proc = null;
  974. showRunButton();
  975. return true;
  976. }
  977. return false;
  978. },100);
  979. })
  980. } catch (error) {
  981. scheduleCall(() => {
  982. if(domConsole.pending_writes.length == 0) {
  983. isRunning = false;
  984. proc = null;
  985. showRunButton();
  986. domConsole.err(error.message);
  987. console.log(error);
  988. return true;
  989. }
  990. return false;
  991. },100);
  992. }
  993. }
  994. function toggleConsole (is_running) {
  995. if (is_running) {
  996. $('.ivprog-term-div').css('display', 'block');
  997. $('#ivprog-term').css('min-height', '160px');
  998. if(domConsole != null)
  999. domConsole.focus();
  1000. //$('#ivprog-term').css('margin-top', '-170px');
  1001. return;
  1002. } else {
  1003. domConsole.hide();
  1004. return;
  1005. }
  1006. if ($('#ivprog-term').css('min-height') == '160px') {
  1007. // esconder
  1008. $('.ivprog-term-div').css('display', 'none');
  1009. $('#ivprog-term').css('min-height', '0');
  1010. //$('#ivprog-term').css('margin-top', '-30px');
  1011. //$('#ivprog-term').css('padding', '5px');
  1012. } else {
  1013. // mostrar
  1014. $('.ivprog-term-div').css('display', 'block');
  1015. $('#ivprog-term').css('min-height', '160px');
  1016. //$('#ivprog-term').css('margin-top', '-170px');
  1017. }
  1018. }
  1019. // function waitToCloseConsole () {
  1020. // domConsole.info("Aperte qualquer tecla para fechar...");
  1021. // const p = new Promise((resolve, _) => {
  1022. // domConsole.requestInput(resolve, true);
  1023. // });
  1024. // p.then( _ => {
  1025. // domConsole.dispose();
  1026. // domConsole = null;
  1027. // $("#ivprog-term").hide();
  1028. // })
  1029. // }
  1030. function toggleTextualCoding () {
  1031. let code = null;
  1032. if (settingsProgrammingTypes != "textual") {
  1033. code = CodeManagement.generate();
  1034. if (code == null) {
  1035. return;
  1036. }
  1037. }
  1038. $('.ivprog_visual_panel').css('display', 'none');
  1039. $('.ivprog_textual_panel').css('display', 'block');
  1040. $('.ivprog_textual_panel').removeClass('loading');
  1041. TextEditor.updateEditor();
  1042. if (code != null)
  1043. TextEditor.setCode(code);
  1044. //$('.ivprog_textual_code').text(code);
  1045. $('.visual_coding_button').removeClass('active');
  1046. $('.textual_coding_button').addClass('active');
  1047. }
  1048. function toggleVisualCoding () {
  1049. $('.ivprog_textual_panel').addClass('loading');
  1050. $('.ivprog_textual_panel').css('display', 'none');
  1051. $('.ivprog_visual_panel').css('display', 'block');
  1052. $('.textual_coding_button').removeClass('active');
  1053. $('.visual_coding_button').addClass('active');
  1054. }
  1055. function removeParameter (function_obj, parameter_obj, parameter_container) {
  1056. registerUserEvent(parameter_obj.name, ActionTypes.REMOVE_FUNCTION_PARAM, function_obj.name);
  1057. var index = function_obj.parameters_list.indexOf(parameter_obj);
  1058. if (index > -1) {
  1059. window.insertContext = true;
  1060. function_obj.parameters_list.splice(index, 1);
  1061. }
  1062. $(parameter_container).fadeOut();
  1063. }
  1064. function updateParameterType (parameter_obj, new_type, function_name, new_dimensions = 0) {
  1065. registerUserEvent(parameter_obj.name, ActionTypes.CHANGE_PARAM_TYPE, function_name, new_type, new_dimensions);
  1066. parameter_obj.type = new_type;
  1067. parameter_obj.dimensions = new_dimensions;
  1068. if (new_dimensions > 0) {
  1069. parameter_obj.rows = new_dimensions;
  1070. parameter_obj.columns = 2;
  1071. }
  1072. }
  1073. function renderParameter (function_obj, parameter_obj, function_container) {
  1074. let ret = "";
  1075. ret += '<div class="ui label function_name_parameter pink"><i class="ui icon ellipsis vertical inverted"></i>';
  1076. ret += '<div class="ui dropdown parameter_type">';
  1077. if (parameter_obj.dimensions > 0) {
  1078. ret += '<div class="text">'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(`type_${parameter_obj.type.toLowerCase()}`);
  1079. if (parameter_obj.dimensions == 1) {
  1080. ret += ' [ ] ';
  1081. } else {
  1082. ret += ' [ ] [ ] ';
  1083. }
  1084. ret += '</div>';
  1085. } else {
  1086. ret += '<div class="text">'+LocalizedStrings.getUI(`type_${parameter_obj.type.toLowerCase()}`)+'</div>';
  1087. }
  1088. ret += '<div class="menu">';
  1089. for (const tm in Types) {
  1090. if (tm == Types.VOID.toUpperCase()) {
  1091. continue;
  1092. }
  1093. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() && parameter_obj.dimensions == 0 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  1094. }
  1095. ret += '<div class="item ' + (parameter_obj.dimensions == 1 ? ' selected ' : '') + '">'
  1096. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('vector')
  1097. + '<div class="menu">';
  1098. for (var tm in Types) {
  1099. if (tm == Types.VOID.toUpperCase()) {
  1100. continue;
  1101. }
  1102. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() && parameter_obj.dimensions == 1 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="1"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  1103. }
  1104. ret += '</div></div>';
  1105. ret += '<div class="item ' + (parameter_obj.dimensions == 2 ? ' selected ' : '') + '">'
  1106. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('matrix')
  1107. + '<div class="menu">';
  1108. for (var tm in Types) {
  1109. if (tm == Types.VOID.toUpperCase()) {
  1110. continue;
  1111. }
  1112. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() && parameter_obj.dimensions == 2 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('matrix')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="2"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  1113. }
  1114. ret += '</div></div>';
  1115. ret += '</div></div>';
  1116. ret += '<div class="parameter_div_edit"><span class="span_name_parameter label_enable_name_parameter">'+parameter_obj.name+'</span></div> ';
  1117. ret += ' <i class="yellow inverted icon times remove_parameter"></i></div>';
  1118. ret = $(ret);
  1119. function_container.find('.container_parameters_list').append(ret);
  1120. ret.find('.remove_parameter').on('click', function(e){
  1121. removeParameter(function_obj, parameter_obj, ret);
  1122. });
  1123. ret.find('.ui.dropdown.parameter_type').dropdown({
  1124. onChange: function(_, __, $selectedItem) {
  1125. if ($selectedItem.data('dimensions')) {
  1126. updateParameterType(parameter_obj, Types[$selectedItem.data('type')], function_obj.name, $selectedItem.data('dimensions'));
  1127. } else {
  1128. updateParameterType(parameter_obj, Types[$selectedItem.data('type')], function_obj.name);
  1129. }
  1130. },
  1131. selectOnKeydown: false
  1132. });
  1133. ret.find('.parameter_div_edit').on('click', function(e){
  1134. registerUserEvent(function_obj.name, ActionTypes.ENTER_CHANGE_PARAM_NAME, parameter_obj.name);
  1135. enableNameParameterUpdate(parameter_obj, ret, function_obj);
  1136. });
  1137. return ret;
  1138. }
  1139. function updateParameterName (parameter_var, new_name, parameter_obj_dom, function_obj) {
  1140. if (parameter_var.name == new_name) {
  1141. return;
  1142. }
  1143. if (isValidIdentifier(new_name)) {
  1144. if (variableNameAlreadyExists(new_name, function_obj)) {
  1145. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getError('inform_valid_param_duplicated', [new_name, function_obj.name]));
  1146. } else {
  1147. registerUserEvent(function_obj.name, ActionTypes.RENAME_FUNCTION_PARAM, parameter_var.name, new_name);
  1148. parameter_var.name = new_name;
  1149. }
  1150. } else {
  1151. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getError('inform_valid_identifier'));
  1152. }
  1153. }
  1154. function variableNameAlreadyExists (name_var, function_obj) {
  1155. if (function_obj.parameters_list) {
  1156. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  1157. if (function_obj.parameters_list[i].name == name_var) {
  1158. return true;
  1159. }
  1160. }
  1161. }
  1162. if (function_obj.variables_list) {
  1163. for (var i = 0; i < function_obj.variables_list.length; i++) {
  1164. if (function_obj.variables_list[i].name == name_var) {
  1165. return true;
  1166. }
  1167. }
  1168. }
  1169. return false;
  1170. }
  1171. function updateFunctionName (function_var, new_name, function_obj_dom) {
  1172. if (function_var.name == new_name) {
  1173. return;
  1174. }
  1175. if (isValidIdentifier(new_name)) {
  1176. if (functionNameAlreadyExists(new_name)) {
  1177. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getError('inform_valid_function_duplicated', [new_name]));
  1178. } else {
  1179. registerUserEvent(function_var.name, ActionTypes.RENAME_FUNCTION, new_name);
  1180. function_var.name = new_name;
  1181. }
  1182. } else {
  1183. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getError('inform_valid_identifier'));
  1184. }
  1185. }
  1186. function functionNameAlreadyExists (function_name) {
  1187. for (var i = 0; i < window.program_obj.functions.length; i++) {
  1188. if (window.program_obj.functions[i].name == function_name) {
  1189. return true;
  1190. }
  1191. }
  1192. return false;
  1193. }
  1194. var opened_name_parameter = false;
  1195. var opened_input_parameter = null;
  1196. function enableNameParameterUpdate (parameter_obj, parent_node, function_obj) {
  1197. if (opened_name_parameter) {
  1198. opened_input_parameter.focus();
  1199. return;
  1200. }
  1201. opened_name_parameter = true;
  1202. parent_node = $(parent_node);
  1203. var input_field;
  1204. parent_node.find('.span_name_parameter').text('');
  1205. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+parameter_obj.name+"' />" );
  1206. input_field.insertBefore(parent_node.find('.span_name_parameter'));
  1207. input_field.on('input', function() {
  1208. var inputWidth = input_field.textWidth()+10;
  1209. opened_input_parameter = input_field;
  1210. input_field.focus();
  1211. var tmpStr = input_field.val();
  1212. input_field.val('');
  1213. input_field.val(tmpStr);
  1214. input_field.css({
  1215. width: inputWidth
  1216. })
  1217. }).trigger('input');
  1218. input_field.focusout(function() {
  1219. /// update array:
  1220. if (input_field.val().trim()) {
  1221. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  1222. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  1223. }
  1224. input_field.off();
  1225. input_field.remove();
  1226. /// update elements:
  1227. opened_name_parameter = false;
  1228. opened_input_parameter = false;
  1229. });
  1230. input_field.on('keydown', function(e) {
  1231. var code = e.keyCode || e.which;
  1232. if(code == 13) {
  1233. if (input_field.val().trim()) {
  1234. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  1235. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  1236. }
  1237. input_field.off();
  1238. input_field.remove();
  1239. /// update elements:
  1240. opened_name_parameter = false;
  1241. opened_input_parameter = false;
  1242. }
  1243. if(code == 27) {
  1244. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  1245. input_field.off();
  1246. input_field.remove();
  1247. /// update elements:
  1248. opened_name_parameter = false;
  1249. opened_input_parameter = false;
  1250. }
  1251. });
  1252. input_field.select();
  1253. }
  1254. var opened_name_function = false;
  1255. var opened_input = null;
  1256. var previousPadding = null;
  1257. function enableNameFunctionUpdate (function_obj, parent_node) {
  1258. if (opened_name_function) {
  1259. opened_input.focus();
  1260. return;
  1261. }
  1262. parent_node = $(parent_node);
  1263. parent_node.find('.span_name_function').text('');
  1264. var input_field;
  1265. if (!previousPadding) {
  1266. previousPadding = parent_node.find('.span_name_function').css('padding-left');
  1267. }
  1268. parent_node.find('.span_name_function').css('padding-left', '0');
  1269. parent_node.find('.span_name_function').css('padding-right', '0');
  1270. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+function_obj.name+"' />" );
  1271. input_field.insertBefore(parent_node.find('.span_name_function'));
  1272. input_field.on('input', function() {
  1273. var inputWidth = input_field.textWidth()+10;
  1274. opened_input = input_field;
  1275. input_field.focus();
  1276. var tmpStr = input_field.val();
  1277. input_field.val('');
  1278. input_field.val(tmpStr);
  1279. input_field.css({
  1280. width: inputWidth
  1281. })
  1282. }).trigger('input');
  1283. input_field.focusout(function() {
  1284. /// update array:
  1285. if (input_field.val().trim()) {
  1286. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  1287. }
  1288. input_field.off();
  1289. input_field.remove();
  1290. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  1291. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  1292. parent_node.find('.span_name_function').text(function_obj.name);
  1293. /// update elements:
  1294. opened_name_function = false;
  1295. opened_input = false;
  1296. });
  1297. input_field.on('keydown', function(e) {
  1298. var code = e.keyCode || e.which;
  1299. if(code == 13) {
  1300. if (input_field.val().trim()) {
  1301. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  1302. }
  1303. input_field.off();
  1304. input_field.remove();
  1305. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  1306. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  1307. parent_node.find('.span_name_function').text(function_obj.name);
  1308. /// update elements:
  1309. opened_name_function = false;
  1310. opened_input = false;
  1311. }
  1312. if(code == 27) {
  1313. input_field.off();
  1314. input_field.remove();
  1315. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  1316. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  1317. parent_node.find('.span_name_function').text(function_obj.name);
  1318. /// update elements:
  1319. opened_name_function = false;
  1320. opened_input = false;
  1321. }
  1322. });
  1323. input_field.select();
  1324. }
  1325. export function addFunctionChangeListener (callback) {
  1326. functionsChangeListeners.push(callback);
  1327. return functionsChangeListeners.length - 1;
  1328. }
  1329. export function addGlobalChangeListener (callback) {
  1330. globalChangeListeners.push(callback);
  1331. return globalChangeListeners.length - 1;
  1332. }
  1333. export function removeGlobalListener (index) {
  1334. globalChangeListeners.splice(index, 1);
  1335. }
  1336. export function removeFunctionListener (index) {
  1337. functionsChangeListeners.splice(index);
  1338. }
  1339. function scheduleCall (callback, time) {
  1340. const scheduled_call = () => {
  1341. const id = setInterval(() => {
  1342. if(callback()) {
  1343. clearInterval(id);
  1344. }
  1345. }, time)
  1346. }
  1347. scheduled_call();
  1348. }
  1349. function showRunButton () {
  1350. document.getElementById('run_button').style.display = 'inline';
  1351. document.getElementById('stop_button').style.display = 'none';
  1352. }
  1353. function showStopButton () {
  1354. document.getElementById('run_button').style.display = 'none';
  1355. document.getElementById('stop_button').style.display = 'inline';
  1356. }
  1357. function stopExecution () {
  1358. domConsole.clearPendingWrites();
  1359. domConsole.cancelPendingInputRequests();
  1360. if(!isRunning) {
  1361. return;
  1362. }
  1363. proc.mode = Modes.ABORT;
  1364. }
  1365. function downloadFile() {
  1366. var contentToSend = '';
  1367. if (settingsProgrammingTypes == "textual") {
  1368. contentToSend += ivprogCore.CodeEditor.getCode();
  1369. } else {
  1370. contentToSend += JSON.stringify(window.program_obj, function(key, value) {
  1371. if (key == 'dom_object') {
  1372. return;
  1373. }
  1374. return value;
  1375. });
  1376. }
  1377. var date = new Date();
  1378. var temp = date.toISOString().split('T')[0] + "_" + date.toTimeString().split(' ')[0].replaceAll(':', '-');
  1379. var blob = new Blob([contentToSend],
  1380. { type: "text/plain;charset=utf-8" });
  1381. saveAs(blob, "ivprog-exported_" + temp + ".ivph");
  1382. }
  1383. function uploadFile (evt) {
  1384. var oFReader = new FileReader();
  1385. oFReader.readAsText(document.getElementById("ivph_file").files[0]);
  1386. oFReader.onload = function (oFREvent) {
  1387. var txt = oFREvent.target.result;
  1388. try {
  1389. window.program_obj = JSON.parse(txt);
  1390. window.renderAlgorithm();
  1391. }
  1392. catch (e) {
  1393. showInvalidFile();
  1394. }
  1395. };
  1396. }