functions.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. import $ from 'jquery';
  2. import { Types } from './types';
  3. import * as Models from './ivprog_elements';
  4. import { LocalizedStrings } from './../services/localizedStringsService';
  5. import * as GlobalsManagement from './globals';
  6. import * as VariablesManagement from './variables';
  7. import * as CommandsManagement from './commands';
  8. import * as CodeManagement from './code_generator';
  9. import * as VariableValueMenu from './commands/variable_value_menu';
  10. import { DOMConsole } from './../io/domConsole';
  11. import { IVProgParser } from './../ast/ivprogParser';
  12. import { IVProgProcessor } from './../processor/ivprogProcessor';
  13. import WatchJS from 'melanke-watchjs';
  14. import { SemanticAnalyser } from '../processor/semantic/semanticAnalyser';
  15. import { IVProgAssessment } from '../assessment/ivprogAssessment';
  16. import * as AlgorithmManagement from './algorithm';
  17. import * as Utils from './utils';
  18. import '../Sortable.js';
  19. var counter_new_functions = 0;
  20. var counter_new_parameters = 0;
  21. var ivprog_version = '2019_01_13 Build 18_55'
  22. let studentTemp = null;
  23. let domConsole = null;
  24. window.studentGrade = null;
  25. window.LocalizedStrings = LocalizedStrings;
  26. const program = new Models.Program();
  27. window.system_functions = [];
  28. // Adding math functions:
  29. window.system_functions.push(new Models.SystemFunction('$sin', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  30. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  31. window.system_functions.push(new Models.SystemFunction('$cos', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  32. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  33. window.system_functions.push(new Models.SystemFunction('$tan', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  34. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  35. window.system_functions.push(new Models.SystemFunction('$sqrt', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  36. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  37. window.system_functions.push(new Models.SystemFunction('$pow', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true), new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  38. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  39. window.system_functions.push(new Models.SystemFunction('$log', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  40. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  41. window.system_functions.push(new Models.SystemFunction('$abs', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  42. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  43. window.system_functions.push(new Models.SystemFunction('$negate', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  44. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  45. window.system_functions.push(new Models.SystemFunction('$invert', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  46. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  47. window.system_functions.push(new Models.SystemFunction('$max', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  48. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  49. window.system_functions.push(new Models.SystemFunction('$min', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  50. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  51. // Adding text functions:
  52. window.system_functions.push(new Models.SystemFunction('$substring', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  53. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  54. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  55. window.system_functions.push(new Models.SystemFunction('$length', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  56. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  57. window.system_functions.push(new Models.SystemFunction('$uppercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  58. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  59. window.system_functions.push(new Models.SystemFunction('$lowercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  60. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  61. window.system_functions.push(new Models.SystemFunction('$charAt', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true), new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  62. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  63. // Adding arrangement functions:
  64. window.system_functions.push(new Models.SystemFunction('$numElements', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 1)],
  65. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  66. window.system_functions.push(new Models.SystemFunction('$matrixLines', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 2)],
  67. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  68. window.system_functions.push(new Models.SystemFunction('$matrixColumns', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true, 2)],
  69. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  70. // Adding conversion functions:
  71. window.system_functions.push(new Models.SystemFunction('$isReal', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  72. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  73. window.system_functions.push(new Models.SystemFunction('$isInt', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  74. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  75. window.system_functions.push(new Models.SystemFunction('$isBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  76. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  77. window.system_functions.push(new Models.SystemFunction('$castReal', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  78. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  79. window.system_functions.push(new Models.SystemFunction('$castInt', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  80. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  81. window.system_functions.push(new Models.SystemFunction('$castBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  82. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  83. window.system_functions.push(new Models.SystemFunction('$castString', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  84. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  85. console.log(' ___ ___ ________ \n / / / / / ____/ \n / / / / / / \n / / / / ______ ___ / /__ \n / / / / / \\ / / / ___/ \n / /______ / / / /\\ \\/ / / / \n / / / / / / \\ / / /____ \n/__________/ /___/ /___/ \\___/ /________/ \n\n Laboratório de Informática na Educação\n http://line.ime.usp.br');
  86. const mainFunction = new Models.Function(LocalizedStrings.getUI("start"), Types.VOID, 0, [], true, false);
  87. mainFunction.function_comment = new Models.Comment(LocalizedStrings.getUI('text_comment_main'));
  88. program.addFunction(mainFunction);
  89. window.program_obj = program;
  90. window.generator = CodeManagement.generate;
  91. window.runCodeAssessment = runCodeAssessment;
  92. window.renderAlgorithm = AlgorithmManagement.renderAlgorithm;
  93. window.insertContext = false;
  94. window.watchW = WatchJS;
  95. WatchJS.watch(window.program_obj.globals, function(){
  96. if (window.insertContext) {
  97. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  98. window.insertContext = false;
  99. } else {
  100. AlgorithmManagement.renderAlgorithm();
  101. }
  102. }, 1);
  103. WatchJS.watch(window.program_obj.functions, function(){
  104. if (window.insertContext) {
  105. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  106. window.insertContext = false;
  107. } else {
  108. AlgorithmManagement.renderAlgorithm();
  109. }
  110. }, 1);
  111. function addFunctionHandler () {
  112. var new_function = new Models.Function(LocalizedStrings.getUI("new_function") + "_" + counter_new_functions, Types.VOID, 0, [], false, false, [], new Models.Comment(LocalizedStrings.getUI('text_comment_start')));
  113. program.addFunction(new_function);
  114. counter_new_functions ++;
  115. window.insertContext = true;
  116. var newe = renderFunction(new_function);
  117. newe.css('display', 'none');
  118. newe.fadeIn();
  119. }
  120. function addParameter (function_obj, function_container, is_from_click = false) {
  121. if (function_obj.parameters_list == null) {
  122. function_obj.parameters_list = [];
  123. }
  124. var new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
  125. function_obj.parameters_list.push(new_parameter);
  126. counter_new_parameters ++;
  127. var newe = renderParameter(function_obj, new_parameter, function_container);
  128. if (is_from_click) {
  129. newe.css('display', 'none');
  130. newe.fadeIn();
  131. }
  132. }
  133. function updateReturnType (function_obj, new_type, new_dimensions = 0) {
  134. function_obj.return_type = new_type;
  135. function_obj.return_dimensions = new_dimensions;
  136. }
  137. function removeFunction (function_obj) {
  138. var index = program.functions.indexOf(function_obj);
  139. if (index > -1) {
  140. program.functions.splice(index, 1);
  141. }
  142. }
  143. function minimizeFunction (function_obj) {
  144. function_obj.is_hidden = !function_obj.is_hidden;
  145. }
  146. function addHandlers (function_obj, function_container) {
  147. function_container.find('.ui.dropdown.function_return').dropdown({
  148. onChange: function(value, text, $selectedItem) {
  149. if ($selectedItem.data('dimensions')) {
  150. updateReturnType(function_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  151. } else {
  152. updateReturnType(function_obj, Types[$selectedItem.data('type')]);
  153. }
  154. },
  155. selectOnKeydown: false
  156. });
  157. function_container.find( ".name_function_updated" ).on('click', function(e){
  158. enableNameFunctionUpdate(function_obj, function_container);
  159. });
  160. function_container.find( ".add_parameter_button" ).on('click', function(e){
  161. window.insertContext = true;
  162. addParameter(function_obj, function_container, true);
  163. });
  164. function_container.find('.menu_commands').dropdown({
  165. on: 'hover'
  166. });
  167. function_container.find('.menu_commands a').on('click', function(evt){
  168. if (function_obj.commands == null || function_obj.commands.length == 0) {
  169. function_obj.commands = [];
  170. var new_cmd = CommandsManagement.genericCreateCommand($(this).data('command'));
  171. function_obj.commands.push(new_cmd);
  172. CommandsManagement.renderCommand(new_cmd, function_container.find('.commands_list_div'), 3, function_obj);
  173. } else {
  174. CommandsManagement.createFloatingCommand(function_obj, function_container, $(this).data('command'), evt);
  175. }
  176. });
  177. function_container.find('.add_var_button_function').on('click', function(e){
  178. window.insertContext = true;
  179. VariablesManagement.addVariable(function_obj, function_container, true);
  180. });
  181. function_container.find('.remove_function_button').on('click', function(e){
  182. removeFunction(function_obj);
  183. function_container.fadeOut();
  184. });
  185. function_container.find('.minimize_function_button').on('click', function(e){
  186. minimizeFunction(function_obj);
  187. if (function_obj.is_hidden) {
  188. function_container.find(".add_var_button_function").toggle();
  189. function_container.find(".inline_add_command").toggle();
  190. function_container.find(".function_area").slideToggle();
  191. } else {
  192. function_container.find(".function_area").slideToggle(function(){
  193. function_container.find(".add_var_button_function").toggle();
  194. function_container.find(".inline_add_command").toggle();
  195. });
  196. }
  197. });
  198. }
  199. // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
  200. function renderFunctionReturn (function_obj, function_element) {
  201. var ret = '<div class="ui dropdown function_return">';
  202. if (function_obj.return_dimensions > 0) {
  203. ret += '<div class="text">'+ LocalizedStrings.getUI("vector") +':'+ LocalizedStrings.getUI(function_obj.return_type);
  204. if (function_obj.return_dimensions == 1) {
  205. ret += ' [ ] ';
  206. } else {
  207. ret += ' [ ] [ ] ';
  208. }
  209. ret += '</div>';
  210. } else {
  211. ret += '<div class="text">'+LocalizedStrings.getUI(function_obj.return_type)+'</div>';
  212. }
  213. ret += '<div class="menu">';
  214. for (var tm in Types) {
  215. ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions < 1 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  216. }
  217. for (var tm in Types) {
  218. if (tm == Types.VOID.toUpperCase()) {
  219. continue;
  220. }
  221. ret += '<div class="item">'
  222. + '<i class="dropdown icon"></i>'
  223. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  224. + '<div class="menu">'
  225. + '<div class="item '+(function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions > 0 ? ' selected ' : '')+'" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  226. + '<div class="item '+(function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions > 0 ? ' selected ' : '')+'" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  227. + '</div>'
  228. + '</div>';
  229. }
  230. ret += '</div></div>';
  231. ret = $(ret);
  232. function_element.find('.function_return').append(ret);
  233. }
  234. var cont = 0;
  235. export function renderFunction (function_obj) {
  236. var appender = '<div class="ui secondary segment function_div list-group-item function_cont_'+cont+'">';
  237. if (function_obj.function_comment) {
  238. //appender += renderComment(function_obj.function_comment, sequence, true, -1);
  239. }
  240. appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
  241. 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>')
  242. + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
  243. appender += '<div class="function_signature_div">'+LocalizedStrings.getUI("function")+' ';
  244. if (function_obj.is_main) {
  245. 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> '
  246. + ' <span class="parethesis_function">( </span> <div class="ui large labels parameters_list">';
  247. } else {
  248. appender += '<div class="ui function_return"></div>';
  249. appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">'+function_obj.name+'</span> </div> '
  250. + ' <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">';
  251. }
  252. appender += '</div> <span class="parethesis_function"> ) </span> </div>'
  253. + (function_obj.is_hidden ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ');
  254. appender += '<div class="ui add_var_context add_var_button_function" style="float: left;"><i class="icon plus circle purple"></i><i class="icon circle white back"></i><div class="ui icon button purple"><i class="icon superscript"></i></div></div>';
  255. appender += '<div class="ui top attached segment variables_list_div"></div>';
  256. appender += '<div class="ui bottom attached segment commands_list_div commands_cont_'+cont+'">'
  257. + '<div class="ui rail" style="width: 35px; margin-left: -36px;"><div class="ui sticky sticky_cont_'+cont+'" style="top: 50px !important;">';
  258. appender += '<i class="icon plus circle purple"></i><i class="icon circle white back"></i><div class="ui icon button dropdown menu_commands orange" ><i class="icon code"></i> <div class="menu"> ';
  259. appender += '<a class="item" data-command="'+Models.COMMAND_TYPES.reader+'"><i class="download icon"></i> ' +LocalizedStrings.getUI('text_read_var')+ '</a>'
  260. + '<a class="item" data-command="'+Models.COMMAND_TYPES.writer+'"><i class="upload icon"></i> '+LocalizedStrings.getUI('text_write_var')+'</a>'
  261. + '<a class="item" data-command="'+Models.COMMAND_TYPES.comment+'"><i class="quote left icon"></i> '+LocalizedStrings.getUI('text_comment')+'</a>'
  262. + '<a class="item" data-command="'+Models.COMMAND_TYPES.attribution+'"><i class="arrow left icon"></i> '+LocalizedStrings.getUI('text_attribution')+'</a>'
  263. + '<a class="item" data-command="'+Models.COMMAND_TYPES.functioncall+'"><i class="hand point right icon"></i> '+LocalizedStrings.getUI('text_functioncall')+'</a>'
  264. + '<a class="item" data-command="'+Models.COMMAND_TYPES.iftrue+'" ><i class="random icon"></i> '+LocalizedStrings.getUI('text_iftrue')+'</a>'
  265. + '<a class="item" data-command="'+Models.COMMAND_TYPES.repeatNtimes+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_repeatNtimes')+'</a>'
  266. + '<a class="item" data-command="'+Models.COMMAND_TYPES.whiletrue+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_whiletrue')+'</a>'
  267. + '<a class="item" data-command="'+Models.COMMAND_TYPES.dowhiletrue+'"><i class="sync icon"></i> '+LocalizedStrings.getUI('text_dowhiletrue')+'</a>'
  268. + '<a class="item" data-command="'+Models.COMMAND_TYPES.switch+'"><i class="list icon"></i> '+LocalizedStrings.getUI('text_switch')+'</a>'
  269. + '<a class="item" data-command="'+Models.COMMAND_TYPES.return+'"><i class="reply icon"></i> '+LocalizedStrings.getUI('text_btn_return')+'</a>'
  270. + '</div></div>';
  271. appender += '</div></div>'
  272. +'</div>';
  273. appender += '</div></div>';
  274. appender = $(appender);
  275. $('.all_functions').append(appender);
  276. appender.data('fun', function_obj);
  277. appender.find('.commands_list_div').data('fun', function_obj);
  278. renderFunctionReturn(function_obj, appender);
  279. addHandlers(function_obj, appender);
  280. // Rendering parameters:
  281. for (var j = 0; j < function_obj.parameters_list.length; j++) {
  282. renderParameter(function_obj, function_obj.parameters_list[j], appender);
  283. }
  284. // Rendering variables:
  285. for (var j = 0; j < function_obj.variables_list.length; j++) {
  286. VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
  287. }
  288. // Rendering commands:
  289. for (var j = 0; j < function_obj.commands.length; j++) {
  290. CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
  291. }
  292. $('.minimize_function_button').popup({
  293. content : LocalizedStrings.getUI("tooltip_minimize"),
  294. delay: {
  295. show: 750,
  296. hide: 0
  297. }
  298. });
  299. var function_index = program.functions.indexOf(function_obj);
  300. Sortable.create(appender.find(".variables_list_div")[0], {
  301. handle: '.ellipsis',
  302. animation: 100,
  303. ghostClass: 'ghost',
  304. group: 'local_vars_drag_' + function_index,
  305. onEnd: function (evt) {
  306. updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  307. }
  308. });
  309. addSortableHandler(appender.find(".commands_list_div")[0], function_index);
  310. if (!function_obj.is_main) {
  311. Sortable.create(appender.find(".container_parameters_list")[0], {
  312. handle: '.ellipsis',
  313. animation: 100,
  314. ghostClass: 'ghost',
  315. group: 'parameters_drag_' + program.functions.indexOf(function_obj),
  316. onEnd: function (evt) {
  317. updateSequenceParameters(evt.oldIndex, evt.newIndex, function_obj);
  318. }
  319. });
  320. }
  321. var teste = '.ui.sticky.sticky_cont_'+cont;
  322. $(teste).sticky({
  323. context: '.ui.bottom.attached.segment.commands_list_div.commands_cont_'+cont,
  324. scrollContext: '.ivprog_visual_panel',
  325. observeChanges: true,
  326. offset: 40,
  327. onStick: function (evt) {
  328. $(teste).css('top', '20px', 'important');
  329. },
  330. onBottom: function (evt) {
  331. $(teste).css('top', '20px', 'important');
  332. },
  333. onUnstick: function (evt) {
  334. $(teste).css('top', '20px', 'important');
  335. },
  336. onReposition: function (evt) {
  337. $(teste).css('top', '20px', 'important');
  338. },
  339. onScroll: function (evt) {
  340. $(teste).css('top', '20px', 'important');
  341. if (!isVisible($(teste), $(teste).parent())) {
  342. $(teste).removeClass('fixed');
  343. }
  344. },
  345. onTop: function (evt) {
  346. $(teste).css('top', '20px', 'important');
  347. }
  348. });
  349. cont ++;
  350. return appender;
  351. }
  352. function isVisible (element, container) {
  353. var elementTop = $(element).offset().top,
  354. elementHeight = $(element).height(),
  355. containerTop = $(container).offset().top,
  356. containerHeight = $(container).height() - 30;
  357. return ((((elementTop - containerTop) + elementHeight) > 0)
  358. && ((elementTop - containerTop) < containerHeight));
  359. }
  360. window.evento_drag;
  361. function updateProgramObjDrag () {
  362. var nodes = Array.prototype.slice.call( $('.all_functions').children() );
  363. var function_index;
  364. var start_index;
  365. var function_obj;
  366. $(evento_drag.item).parentsUntil(".all_functions").each(function (index) {
  367. if ($(this).hasClass('function_div')) {
  368. function_index = nodes.indexOf(this);
  369. start_index = index;
  370. function_obj = $(this);
  371. }
  372. });
  373. console.log(function_index);
  374. var path_target = [];
  375. $(evento_drag.item).parentsUntil(".all_functions").each(function (index) {
  376. if ($(this).hasClass('command_container')) {
  377. path_target.push(this);
  378. }
  379. });
  380. if (path_target.length == 0) {
  381. //console.log('soltou na raiz, na posição: ' + evento_drag.newIndex + ' mas ainda não sei de onde saiu ');
  382. } else {
  383. //console.log('soltou dentro de algum bloco, sequência vem logo abaixo (de baixo pra cima): ');
  384. //console.log(path_target);
  385. }
  386. var index_each = [];
  387. var path_relative = [];
  388. for (var i = path_target.length - 1; i >= 0; i --) {
  389. console.log('da vez', $(path_target[i + 1]));
  390. if (i == (path_target.length - 1)) { // está na raiz
  391. var indice_na_raiz = function_obj.find('.command_container').index(path_target[i]);
  392. console.log('índice na raiz: ', indice_na_raiz);
  393. } else {
  394. if ($(path_target[i + 1]).hasClass('iftrue')) {
  395. if ($(path_target[i]).parent().hasClass('commands_if')) {
  396. path_relative.push('if');
  397. index_each.push($(path_target[i]).parent().find('.command_container').index(path_target[i]));
  398. } else {
  399. path_relative.push('else');
  400. index_each.push($(path_target[i]).parent().find('.command_container').index(path_target[i]));
  401. }
  402. } else if ($(path_target[i + 1]).hasClass('dowhiletrue')) {
  403. path_relative.push('dowhiletrue');
  404. index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  405. } else if ($(path_target[i + 1]).hasClass('repeatNtimes')) {
  406. path_relative.push('repeatNtimes');
  407. index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  408. } else if ($(path_target[i + 1]).hasClass('whiletrue')) {
  409. path_relative.push('whiletrue');
  410. index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  411. } else if ($(path_target[i + 1]).hasClass('switch')) {
  412. path_relative.push('switch');
  413. //index_each.push($(path_target[i + 1]).find('.command_container').index(path_target[i]));
  414. }
  415. }
  416. }
  417. var index_in_block = -1;
  418. var is_in_else = $(evento_drag.item).parent().hasClass('commands_else');
  419. index_in_block = $(evento_drag.item).parent().find('.command_container').index(evento_drag.item);
  420. var is_in_case_switch = $(evento_drag.item).parent().hasClass('case_commands_block');
  421. var index_case_of_switch = -1;
  422. if (is_in_case_switch) {
  423. index_case_of_switch = $(evento_drag.item).parent().parent().parent().find('.case_div').index($(evento_drag.item).parent().parent());
  424. }
  425. /*console.log('path_relative:');
  426. console.log(path_relative);
  427. console.log('index_each:');
  428. console.log(index_each);
  429. console.log('index_in_block:');
  430. console.log(index_in_block);
  431. console.log('ele está em algum bloco de senão? ');
  432. console.log(is_in_else);
  433. console.log('ele está dentro de um case de switch?');
  434. console.log(is_in_case_switch);
  435. console.log('qual é o índice do case: ');
  436. console.log(index_case_of_switch);*/
  437. // encontrar o elemento na árvore:
  438. var command_start_point = window.program_obj.functions[function_index].commands[indice_na_raiz];
  439. var block_to_insert = command_start_point;
  440. for (var i = 0; i < index_each.length; i++) {
  441. if (path_relative[i] == "else") {
  442. block_to_insert = block_to_insert.commands_else[index_each[i]];
  443. } else if (path_relative[i] == "switch") {
  444. } else {
  445. block_to_insert = block_to_insert.commands_block[index_each[i]]
  446. }
  447. }
  448. //console.log('command_start_point', command_start_point);
  449. //console.log('block_to_insert', block_to_insert);
  450. // agora tem que alocar o comando na árvore, mas considerar as quatro situações:
  451. // (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.
  452. if (path_target.length == 0) { // soltou na raiz:
  453. window.program_obj.functions[function_index].commands.splice(evento_drag.newIndex, 0, command_in_drag);
  454. } else if (is_in_else) {
  455. if (block_to_insert.commands_else) {
  456. block_to_insert.commands_else.splice(evento_drag.newIndex, 0, command_in_drag);
  457. } else {
  458. block_to_insert.commands_else = [];
  459. block_to_insert.commands_else.push(command_in_drag);
  460. }
  461. } else if (is_in_case_switch) {
  462. } else {
  463. // verificar se tem alguma coisa no bloco:
  464. if (block_to_insert.commands_block) {
  465. block_to_insert.commands_block.splice(evento_drag.newIndex, 0, command_in_drag);
  466. } else {
  467. block_to_insert.commands_block = [];
  468. block_to_insert.commands_block.push(command_in_drag);
  469. }
  470. }
  471. window.draging = false;
  472. renderAlgorithm();
  473. }
  474. function prepareDragHandler (evt) {
  475. window.draging = true;
  476. var nodes = Array.prototype.slice.call( $('.all_functions').children() );
  477. var function_index;
  478. var function_obj;
  479. $(evt.item).parentsUntil(".all_functions").each(function (index) {
  480. if ($(this).hasClass('function_div')) {
  481. function_index = nodes.indexOf(this);
  482. function_obj = window.program_obj.functions[function_index];
  483. }
  484. });
  485. command_in_drag = $(evt.item).data("command");
  486. //console.log('$(evt.item).parent(): ');
  487. //console.log($(evt.item).parent());
  488. // descobrir qual das quatro situações:
  489. if ($(evt.item).parent().hasClass('commands_list_div')) { // está na raiz:
  490. if (function_obj.commands.indexOf(command_in_drag) > -1) {
  491. function_obj.commands.splice(function_obj.commands.indexOf(command_in_drag), 1);
  492. }
  493. } else if ($(evt.item).parent().hasClass('commands_else')) { // está no else:
  494. if ($(evt.item).parent().data('command').commands_else.indexOf(command_in_drag) > -1) {
  495. $(evt.item).parent().data('command').commands_else.splice($(evt.item).parent().data('command').commands_else.indexOf(command_in_drag), 1);
  496. }
  497. } else if ($(evt.item).parent().hasClass('case_commands_block')) { // está em um switch:
  498. } else { // caso padrão:
  499. if ($(evt.item).parent().data('command').commands_block.indexOf(command_in_drag) > -1) {
  500. $(evt.item).parent().data('command').commands_block.splice($(evt.item).parent().data('command').commands_block.indexOf(command_in_drag), 1);
  501. }
  502. }
  503. }
  504. var command_in_drag;
  505. function addSortableHandler (element, id_function) {
  506. var n_group = 'commands_drag_' + id_function;
  507. Sortable.create(element, {
  508. handle: '.command_drag',
  509. ghostClass: 'ghost',
  510. animation: 300,
  511. group: {name: n_group},
  512. onEnd: function (evt) {
  513. //updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  514. var itemEl = evt.item; // dragged HTMLElement
  515. evt.to; // target list
  516. evt.from; // previous list
  517. evt.oldIndex; // element's old index within old parent
  518. evt.newIndex; // element's new index within new parent
  519. //console.log('::EVT::');
  520. //console.log(evt);
  521. window.evento_drag = evt;
  522. try {
  523. updateProgramObjDrag();
  524. } catch (e) {
  525. window.draging = false;
  526. }
  527. },
  528. onStart: function (evt) {
  529. //console.log("START::EVT::");
  530. //console.log(evt);
  531. //console.log("\n\ncommand_in_drag");
  532. try {
  533. prepareDragHandler(evt);
  534. } catch (e) {
  535. window.draging = false;
  536. }
  537. }
  538. });
  539. element = $(element);
  540. element.find(".iftrue").each(function( index ) {
  541. addSortableHandler($(this).find(".block_commands")[0], id_function);
  542. addSortableHandler($(this).find(".block_commands")[1], id_function);
  543. });
  544. element.find(".repeatNtimes").each(function( index ) {
  545. addSortableHandler($(this).find(".block_commands")[0], id_function);
  546. });
  547. element.find(".dowhiletrue").each(function( index ) {
  548. addSortableHandler($(this).find(".block_commands")[0], id_function);
  549. });
  550. element.find(".whiletrue").each(function( index ) {
  551. addSortableHandler($(this).find(".block_commands")[0], id_function);
  552. });
  553. element.find(".switch").each(function( index ) {
  554. $(this).find(".case_div").each(function( index ) {
  555. addSortableHandler($(this).find(".case_commands_block")[0], id_function);
  556. });
  557. });
  558. }
  559. export function initVisualUI () {
  560. // MUST USE CONST, LET, OR VAR !!!!!!
  561. const mainDiv = $('#visual-main-div');
  562. // fill mainDiv with functions and globals...
  563. // renderAlgorithm()...
  564. $('.add_function_button').on('click', () => {
  565. addFunctionHandler();
  566. });
  567. $('.add_global_button').on('click', () => {
  568. window.insertContext = true;
  569. GlobalsManagement.addGlobal(program, true);
  570. });
  571. $('.run_button').on('click', () => {
  572. runCode();
  573. });
  574. $('.visual_coding_button').on('click', () => {
  575. toggleVisualCoding();
  576. });
  577. $('.textual_coding_button').on('click', () => {
  578. toggleTextualCoding();
  579. });
  580. $('.assessment').on('click', () => {
  581. runCodeAssessment();
  582. is_iassign = true;
  583. });
  584. $('.div_toggle_console').on('click', () => {
  585. toggleConsole();
  586. });
  587. $('.expand_button').on('click', () => {
  588. full_screen();
  589. });
  590. $('.main_title h2').prop('title', LocalizedStrings.getUI('text_ivprog_description'));
  591. }
  592. var is_iassign = false;
  593. $( document ).ready(function() {
  594. var time_show = 750;
  595. $('.visual_coding_button').popup({
  596. content : LocalizedStrings.getUI("tooltip_visual"),
  597. delay: {
  598. show: time_show,
  599. hide: 0
  600. }
  601. });
  602. $('.textual_coding_button').popup({
  603. content : LocalizedStrings.getUI("tooltip_textual"),
  604. delay: {
  605. show: time_show,
  606. hide: 0
  607. }
  608. });
  609. $('.upload_file_button').popup({
  610. content : LocalizedStrings.getUI("tooltip_upload"),
  611. delay: {
  612. show: time_show,
  613. hide: 0
  614. }
  615. });
  616. $('.download_file_button').popup({
  617. content : LocalizedStrings.getUI("tooltip_download"),
  618. delay: {
  619. show: time_show,
  620. hide: 0
  621. }
  622. });
  623. $('.undo_button').popup({
  624. content : LocalizedStrings.getUI("tooltip_undo"),
  625. delay: {
  626. show: time_show,
  627. hide: 0
  628. }
  629. });
  630. $('.redo_button').popup({
  631. content : LocalizedStrings.getUI("tooltip_redo"),
  632. delay: {
  633. show: time_show,
  634. hide: 0
  635. }
  636. });
  637. $('.run_button').popup({
  638. content : LocalizedStrings.getUI("tooltip_run"),
  639. delay: {
  640. show: time_show,
  641. hide: 0
  642. }
  643. });
  644. $('.assessment_button').popup({
  645. content : LocalizedStrings.getUI("tooltip_evaluate"),
  646. delay: {
  647. show: time_show,
  648. hide: 0
  649. }
  650. });
  651. $('.help_button').popup({
  652. content : LocalizedStrings.getUI("tooltip_help") + ' - ' + LocalizedStrings.getUI("text_ivprog_version") + ' ' + ivprog_version,
  653. delay: {
  654. show: time_show,
  655. hide: 0
  656. }
  657. });
  658. $('.add_global_button').popup({
  659. content : LocalizedStrings.getUI("tooltip_add_global"),
  660. delay: {
  661. show: time_show,
  662. hide: 0
  663. }
  664. });
  665. $('.div_toggle_console').popup({
  666. content : LocalizedStrings.getUI("tooltip_console"),
  667. delay: {
  668. show: time_show,
  669. hide: 0
  670. }
  671. });
  672. Sortable.create(listWithHandle, {
  673. handle: '.glyphicon-move',
  674. animation: 100,
  675. ghostClass: 'ghost',
  676. group: 'functions_divs_drag',
  677. onEnd: function (evt) {
  678. updateSequenceFunction(evt.oldIndex, evt.newIndex);
  679. }
  680. });
  681. var listGlobalsHandle = document.getElementById("listGlobalsHandle");
  682. Sortable.create(listGlobalsHandle, {
  683. handle: '.ellipsis',
  684. animation: 100,
  685. ghostClass: 'ghost',
  686. group: 'globals_divs_drag',
  687. onEnd: function (evt) {
  688. updateSequenceGlobals(evt.oldIndex, evt.newIndex);
  689. }
  690. });
  691. });
  692. function updateSequenceParameters (oldIndex, newIndex, function_obj) {
  693. function_obj.parameters_list.splice(newIndex, 0, function_obj.parameters_list.splice(oldIndex, 1)[0]);
  694. }
  695. function updateSequenceLocals (oldIndex, newIndex, function_obj) {
  696. function_obj.variables_list.splice(newIndex, 0, function_obj.variables_list.splice(oldIndex, 1)[0]);
  697. }
  698. function updateSequenceGlobals (oldIndex, newIndex) {
  699. program_obj.globals.splice(newIndex, 0, program_obj.globals.splice(oldIndex, 1)[0]);
  700. }
  701. function updateSequenceFunction (oldIndex, newIndex) {
  702. program_obj.functions.splice(newIndex, 0, program_obj.functions.splice(oldIndex, 1)[0]);
  703. }
  704. function runCodeAssessment () {
  705. window.studentGrade = null;
  706. studentTemp = null;
  707. const strCode = CodeManagement.generate();
  708. if (strCode == null) {
  709. return;
  710. }
  711. toggleConsole(true);
  712. if(domConsole == null)
  713. domConsole = new DOMConsole("#ivprog-term");
  714. $("#ivprog-term").slideDown(500);
  715. const testCases = [{input:[1], output:[1]}];
  716. const runner = new IVProgAssessment(strCode, testCases, domConsole);
  717. runner.runTest().then(grade => {
  718. if (!is_iassign) {
  719. parent.getEvaluationCallback(grade);
  720. } else {
  721. is_iassign = false;
  722. }
  723. }).catch( err => console.log(err));
  724. }
  725. function runCode () {
  726. const strCode = CodeManagement.generate();
  727. if (strCode == null) {
  728. return;
  729. }
  730. toggleConsole(true);
  731. if(domConsole == null)
  732. domConsole = new DOMConsole("#ivprog-term");
  733. $("#ivprog-term").slideDown(500);
  734. try {
  735. const parser = IVProgParser.createParser(strCode);
  736. const analyser = new SemanticAnalyser(parser.parseTree());
  737. const data = analyser.analyseTree();
  738. const proc = new IVProgProcessor(data);
  739. proc.registerInput(domConsole);
  740. proc.registerOutput(domConsole);
  741. $("#ivprog-term").addClass('ivprog-term-active');
  742. proc.interpretAST().then( _ => {
  743. domConsole.info("Programa executado com sucesso!");
  744. $("#ivprog-term").removeClass('ivprog-term-active');
  745. }).catch(err => {
  746. domConsole.err(err.message);
  747. $("#ivprog-term").removeClass('ivprog-term-active');
  748. })
  749. } catch (error) {
  750. domConsole.err(error.message);
  751. console.log(error);
  752. }
  753. }
  754. function toggleConsole (is_running) {
  755. if (is_running) {
  756. $('.ivprog-term-div').css('display', 'block');
  757. $('#ivprog-term').css('min-height', '160px');
  758. $('#ivprog-term').css('margin-top', '-170px');
  759. return;
  760. }
  761. if ($('#ivprog-term').css('min-height') == '160px') {
  762. // esconder
  763. $('.ivprog-term-div').css('display', 'none');
  764. $('#ivprog-term').css('min-height', '0');
  765. $('#ivprog-term').css('margin-top', '-30px');
  766. $('#ivprog-term').css('padding', '5px');
  767. } else {
  768. // mostrar
  769. $('.ivprog-term-div').css('display', 'block');
  770. $('#ivprog-term').css('min-height', '160px');
  771. $('#ivprog-term').css('margin-top', '-170px');
  772. }
  773. }
  774. function waitToCloseConsole () {
  775. domConsole.info("Aperte qualquer tecla para fechar...");
  776. const p = new Promise((resolve, _) => {
  777. domConsole.requestInput(resolve, true);
  778. });
  779. p.then( _ => {
  780. domConsole.dispose();
  781. domConsole = null;
  782. $("#ivprog-term").hide();
  783. })
  784. }
  785. function toggleTextualCoding () {
  786. var code = CodeManagement.generate();
  787. if (code == null) {
  788. return;
  789. }
  790. $('.ivprog_visual_panel').css('display', 'none');
  791. $('.ivprog_textual_panel').css('display', 'block');
  792. $('.ivprog_textual_panel').removeClass('loading');
  793. $('.ivprog_textual_code').text(code);
  794. $('.visual_coding_button').removeClass('active');
  795. $('.textual_coding_button').addClass('active');
  796. }
  797. function toggleVisualCoding () {
  798. $('.ivprog_textual_panel').addClass('loading');
  799. $('.ivprog_textual_panel').css('display', 'none');
  800. $('.ivprog_visual_panel').css('display', 'block');
  801. $('.textual_coding_button').removeClass('active');
  802. $('.visual_coding_button').addClass('active');
  803. }
  804. function removeParameter (function_obj, parameter_obj, parameter_container) {
  805. var index = function_obj.parameters_list.indexOf(parameter_obj);
  806. if (index > -1) {
  807. window.insertContext = true;
  808. function_obj.parameters_list.splice(index, 1);
  809. }
  810. $(parameter_container).fadeOut();
  811. }
  812. function updateParameterType (parameter_obj, new_type, new_dimensions = 0) {
  813. parameter_obj.type = new_type;
  814. parameter_obj.dimensions = new_dimensions;
  815. if (new_dimensions > 0) {
  816. parameter_obj.rows = new_dimensions;
  817. parameter_obj.columns = 2;
  818. }
  819. }
  820. function renderParameter (function_obj, parameter_obj, function_container) {
  821. var ret = "";
  822. ret += '<div class="ui label function_name_parameter pink"><i class="ui icon ellipsis vertical inverted"></i>';
  823. ret += '<div class="ui dropdown parameter_type">';
  824. if (parameter_obj.dimensions > 0) {
  825. ret += '<div class="text">'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(parameter_obj.type);
  826. if (parameter_obj.dimensions == 1) {
  827. ret += ' [ ] ';
  828. } else {
  829. ret += ' [ ] [ ] ';
  830. }
  831. ret += '</div>';
  832. } else {
  833. ret += '<div class="text">'+LocalizedStrings.getUI(parameter_obj.type)+'</div>';
  834. }
  835. ret += '<div class="menu">';
  836. for (var tm in Types) {
  837. if (tm == Types.VOID.toUpperCase()) {
  838. continue;
  839. }
  840. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  841. }
  842. for (var tm in Types) {
  843. if (tm == Types.VOID.toUpperCase()) {
  844. continue;
  845. }
  846. ret += '<div class="item">'
  847. + '<i class="dropdown icon"></i>'
  848. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  849. + '<div class="menu">'
  850. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  851. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  852. + '</div>'
  853. + '</div>';
  854. }
  855. ret += '</div></div>';
  856. ret += '<div class="parameter_div_edit"><span class="span_name_parameter label_enable_name_parameter">'+parameter_obj.name+'</span></div> ';
  857. ret += ' <i class="yellow inverted icon times remove_parameter"></i></div>';
  858. ret = $(ret);
  859. function_container.find('.container_parameters_list').append(ret);
  860. ret.find('.remove_parameter').on('click', function(e){
  861. removeParameter(function_obj, parameter_obj, ret);
  862. });
  863. ret.find('.ui.dropdown.parameter_type').dropdown({
  864. onChange: function(value, text, $selectedItem) {
  865. if ($selectedItem.data('dimensions')) {
  866. updateParameterType(parameter_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  867. } else {
  868. updateParameterType(parameter_obj, Types[$selectedItem.data('type')]);
  869. }
  870. },
  871. selectOnKeydown: false
  872. });
  873. ret.find('.label_enable_name_parameter').on('click', function(e){
  874. enableNameParameterUpdate(parameter_obj, ret, function_obj);
  875. });
  876. return ret;
  877. }
  878. function updateParameterName (parameter_var, new_name, parameter_obj_dom, function_obj) {
  879. if (parameter_var.name == new_name) {
  880. return;
  881. }
  882. if (isValidIdentifier(new_name)) {
  883. if (variableNameAlreadyExists(new_name, function_obj)) {
  884. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getUI('inform_valid_variable_duplicated'));
  885. } else {
  886. parameter_var.name = new_name;
  887. }
  888. } else {
  889. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getUI('inform_valid_name'));
  890. }
  891. }
  892. function variableNameAlreadyExists (name_var, function_obj) {
  893. if (function_obj.parameters_list) {
  894. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  895. if (function_obj.parameters_list[i].name == name_var) {
  896. return true;
  897. }
  898. }
  899. }
  900. if (function_obj.variables_list) {
  901. for (var i = 0; i < function_obj.variables_list.length; i++) {
  902. if (function_obj.variables_list[i].name == name_var) {
  903. return true;
  904. }
  905. }
  906. }
  907. return false;
  908. }
  909. function updateFunctionName (function_var, new_name, function_obj_dom) {
  910. if (function_var.name == new_name) {
  911. return;
  912. }
  913. if (isValidIdentifier(new_name)) {
  914. if (functionNameAlreadyExists(new_name)) {
  915. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getUI('inform_valid_name_duplicated'));
  916. } else {
  917. function_var.name = new_name;
  918. }
  919. } else {
  920. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getUI('inform_valid_name'));
  921. }
  922. }
  923. function functionNameAlreadyExists (function_name) {
  924. for (var i = 0; i < window.program_obj.functions.length; i++) {
  925. if (window.program_obj.functions[i].name == function_name) {
  926. return true;
  927. }
  928. }
  929. return false;
  930. }
  931. function isValidIdentifier (identifier_str) {
  932. return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(identifier_str);
  933. }
  934. var opened_name_parameter = false;
  935. var opened_input_parameter = null;
  936. function enableNameParameterUpdate (parameter_obj, parent_node, function_obj) {
  937. if (opened_name_parameter) {
  938. opened_input_parameter.focus();
  939. return;
  940. }
  941. opened_name_parameter = true;
  942. parent_node = $(parent_node);
  943. var input_field;
  944. parent_node.find('.span_name_parameter').text('');
  945. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+parameter_obj.name+"' />" );
  946. input_field.insertBefore(parent_node.find('.span_name_parameter'));
  947. input_field.on('input', function() {
  948. var inputWidth = input_field.textWidth()+10;
  949. opened_input_parameter = input_field;
  950. input_field.focus();
  951. var tmpStr = input_field.val();
  952. input_field.val('');
  953. input_field.val(tmpStr);
  954. input_field.css({
  955. width: inputWidth
  956. })
  957. }).trigger('input');
  958. input_field.focusout(function() {
  959. /// update array:
  960. if (input_field.val().trim()) {
  961. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  962. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  963. }
  964. input_field.off();
  965. input_field.remove();
  966. /// update elements:
  967. opened_name_parameter = false;
  968. opened_input_parameter = false;
  969. });
  970. input_field.on('keydown', function(e) {
  971. var code = e.keyCode || e.which;
  972. if(code == 13) {
  973. if (input_field.val().trim()) {
  974. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  975. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  976. }
  977. input_field.off();
  978. input_field.remove();
  979. /// update elements:
  980. opened_name_parameter = false;
  981. opened_input_parameter = false;
  982. }
  983. if(code == 27) {
  984. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  985. input_field.off();
  986. input_field.remove();
  987. /// update elements:
  988. opened_name_parameter = false;
  989. opened_input_parameter = false;
  990. }
  991. });
  992. input_field.select();
  993. }
  994. var opened_name_function = false;
  995. var opened_input = null;
  996. var previousPadding = null;
  997. function enableNameFunctionUpdate (function_obj, parent_node) {
  998. if (opened_name_function) {
  999. opened_input.focus();
  1000. return;
  1001. }
  1002. parent_node = $(parent_node);
  1003. parent_node.find('.span_name_function').text('');
  1004. var input_field;
  1005. if (!previousPadding) {
  1006. previousPadding = parent_node.find('.span_name_function').css('padding-left');
  1007. }
  1008. parent_node.find('.span_name_function').css('padding-left', '0');
  1009. parent_node.find('.span_name_function').css('padding-right', '0');
  1010. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+function_obj.name+"' />" );
  1011. input_field.insertBefore(parent_node.find('.span_name_function'));
  1012. input_field.on('input', function() {
  1013. var inputWidth = input_field.textWidth()+10;
  1014. opened_input = input_field;
  1015. input_field.focus();
  1016. var tmpStr = input_field.val();
  1017. input_field.val('');
  1018. input_field.val(tmpStr);
  1019. input_field.css({
  1020. width: inputWidth
  1021. })
  1022. }).trigger('input');
  1023. input_field.focusout(function() {
  1024. /// update array:
  1025. if (input_field.val().trim()) {
  1026. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  1027. }
  1028. input_field.off();
  1029. input_field.remove();
  1030. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  1031. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  1032. parent_node.find('.span_name_function').text(function_obj.name);
  1033. /// update elements:
  1034. opened_name_function = false;
  1035. opened_input = false;
  1036. });
  1037. input_field.on('keydown', function(e) {
  1038. var code = e.keyCode || e.which;
  1039. if(code == 13) {
  1040. if (input_field.val().trim()) {
  1041. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  1042. }
  1043. input_field.off();
  1044. input_field.remove();
  1045. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  1046. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  1047. parent_node.find('.span_name_function').text(function_obj.name);
  1048. /// update elements:
  1049. opened_name_function = false;
  1050. opened_input = false;
  1051. }
  1052. if(code == 27) {
  1053. input_field.off();
  1054. input_field.remove();
  1055. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  1056. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  1057. parent_node.find('.span_name_function').text(function_obj.name);
  1058. /// update elements:
  1059. opened_name_function = false;
  1060. opened_input = false;
  1061. }
  1062. });
  1063. input_field.select();
  1064. }