functions_sidebar.js 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. import $ from 'jquery';
  2. import { Types } from './types';
  3. import * as Models from './ivprog_elements_sidebar';
  4. import { LocalizedStrings } from './../services/localizedStringsService';
  5. import * as GlobalsManagement from './globals_sidebar';
  6. import * as VariablesManagement from './variables';
  7. import * as CommandsManagement from './commands_sidebar';
  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_sidebar';
  17. import * as Utils from './utils';
  18. import '../Sortable.js';
  19. var counter_new_functions = 0;
  20. var counter_new_parameters = 0;
  21. let studentTemp = null;
  22. let domConsole = null;
  23. window.studentGrade = null;
  24. window.LocalizedStrings = LocalizedStrings;
  25. const program = new Models.Program();
  26. window.system_functions = [];
  27. // Adding math functions:
  28. window.system_functions.push(new Models.SystemFunction('$sin', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  29. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  30. window.system_functions.push(new Models.SystemFunction('$cos', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  31. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  32. window.system_functions.push(new Models.SystemFunction('$tan', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  33. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  34. window.system_functions.push(new Models.SystemFunction('$sqrt', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  35. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  36. window.system_functions.push(new Models.SystemFunction('$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)],
  37. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  38. window.system_functions.push(new Models.SystemFunction('$log', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  39. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  40. window.system_functions.push(new Models.SystemFunction('$abs', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  41. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  42. window.system_functions.push(new Models.SystemFunction('$negate', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  43. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  44. window.system_functions.push(new Models.SystemFunction('$invert', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  45. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  46. window.system_functions.push(new Models.SystemFunction('$max', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  47. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  48. window.system_functions.push(new Models.SystemFunction('$min', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  49. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
  50. // Adding text functions:
  51. window.system_functions.push(new Models.SystemFunction('$substring', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  52. 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)],
  53. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  54. window.system_functions.push(new Models.SystemFunction('$length', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  55. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  56. window.system_functions.push(new Models.SystemFunction('$uppercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  57. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  58. window.system_functions.push(new Models.SystemFunction('$lowercase', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  59. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  60. 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)],
  61. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.text));
  62. // Adding arrangement functions:
  63. 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)],
  64. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  65. 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)],
  66. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  67. 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)],
  68. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.arrangement));
  69. // Adding conversion functions:
  70. window.system_functions.push(new Models.SystemFunction('$isReal', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  71. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  72. window.system_functions.push(new Models.SystemFunction('$isInt', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  73. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  74. window.system_functions.push(new Models.SystemFunction('$isBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  75. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  76. window.system_functions.push(new Models.SystemFunction('$castReal', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  77. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  78. window.system_functions.push(new Models.SystemFunction('$castInt', Types.INTEGER, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  79. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  80. window.system_functions.push(new Models.SystemFunction('$castBool', Types.BOOLEAN, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  81. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  82. window.system_functions.push(new Models.SystemFunction('$castString', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
  83. null, Models.SYSTEM_FUNCTIONS_CATEGORIES.conversion));
  84. 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');
  85. const mainFunction = new Models.Function(LocalizedStrings.getUI("start"), Types.VOID, 0, [], true, false);
  86. mainFunction.function_comment = new Models.Comment(LocalizedStrings.getUI('text_comment_main'));
  87. program.addFunction(mainFunction);
  88. window.program_obj = program;
  89. window.generator = CodeManagement.generate;
  90. window.runCodeAssessment = runCodeAssessment;
  91. window.renderAlgorithm = AlgorithmManagement.renderAlgorithm;
  92. window.insertContext = false;
  93. window.watchW = WatchJS;
  94. WatchJS.watch(window.program_obj.globals, function(){
  95. if (window.insertContext) {
  96. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  97. window.insertContext = false;
  98. } else {
  99. AlgorithmManagement.renderAlgorithm();
  100. }
  101. }, 1);
  102. WatchJS.watch(window.program_obj.functions, function(){
  103. if (window.insertContext) {
  104. setTimeout(function(){ AlgorithmManagement.renderAlgorithm(); }, 300);
  105. window.insertContext = false;
  106. } else {
  107. AlgorithmManagement.renderAlgorithm();
  108. }
  109. }, 0);
  110. function addFunctionHandler () {
  111. 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')));
  112. program.addFunction(new_function);
  113. counter_new_functions ++;
  114. window.insertContext = true;
  115. var newe = renderFunction(new_function);
  116. newe.css('display', 'none');
  117. newe.fadeIn();
  118. }
  119. function addParameter (function_obj, function_container, is_from_click = false) {
  120. if (function_obj.parameters_list == null) {
  121. function_obj.parameters_list = [];
  122. }
  123. var new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
  124. function_obj.parameters_list.push(new_parameter);
  125. counter_new_parameters ++;
  126. var newe = renderParameter(function_obj, new_parameter, function_container);
  127. if (is_from_click) {
  128. newe.css('display', 'none');
  129. newe.fadeIn();
  130. }
  131. }
  132. function updateReturnType (function_obj, new_type, new_dimensions = 0) {
  133. function_obj.return_type = new_type;
  134. function_obj.return_dimensions = new_dimensions;
  135. }
  136. function removeFunction (function_obj) {
  137. var index = program.functions.indexOf(function_obj);
  138. if (index > -1) {
  139. program.functions.splice(index, 1);
  140. }
  141. }
  142. function minimizeFunction (function_obj) {
  143. function_obj.is_hidden = !function_obj.is_hidden;
  144. }
  145. function addHandlers (function_obj, function_container) {
  146. function_container.find('.ui.dropdown.function_return').dropdown({
  147. onChange: function(value, text, $selectedItem) {
  148. if ($selectedItem.data('dimensions')) {
  149. updateReturnType(function_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  150. } else {
  151. updateReturnType(function_obj, Types[$selectedItem.data('type')]);
  152. }
  153. },
  154. selectOnKeydown: false
  155. });
  156. function_container.find( ".name_function_updated" ).on('click', function(e){
  157. enableNameFunctionUpdate(function_obj, function_container);
  158. });
  159. function_container.find( ".add_parameter_button" ).on('click', function(e){
  160. window.insertContext = true;
  161. addParameter(function_obj, function_container, true);
  162. });
  163. function_container.find('.menu_commands').dropdown({
  164. on: 'hover'
  165. });
  166. function_container.find('.menu_commands a').on('click', function(evt){
  167. if (function_obj.commands == null || function_obj.commands.length == 0) {
  168. function_obj.commands = [];
  169. var new_cmd = CommandsManagement.genericCreateCommand($(this).data('command'));
  170. function_obj.commands.push(new_cmd);
  171. CommandsManagement.renderCommand(new_cmd, function_container.find('.commands_list_div'), 3, function_obj);
  172. } else {
  173. CommandsManagement.createFloatingCommand(function_obj, function_container, $(this).data('command'), evt);
  174. }
  175. });
  176. function_container.find('.add_var_button_function').on('click', function(e){
  177. window.insertContext = true;
  178. VariablesManagement.addVariable(function_obj, function_container, true);
  179. });
  180. function_container.find('.remove_function_button').on('click', function(e){
  181. removeFunction(function_obj);
  182. function_container.fadeOut();
  183. });
  184. function_container.find('.minimize_function_button').on('click', function(e){
  185. minimizeFunction(function_obj);
  186. if (function_obj.is_hidden) {
  187. function_container.find(".add_var_button_function").toggle();
  188. function_container.find(".inline_add_command").toggle();
  189. function_container.find(".function_area").slideToggle();
  190. } else {
  191. function_container.find(".function_area").slideToggle(function(){
  192. function_container.find(".add_var_button_function").toggle();
  193. function_container.find(".inline_add_command").toggle();
  194. });
  195. }
  196. });
  197. }
  198. // Essa função imprime o tipo de retorno da função e cria o menu do tipo 'select' para alteração
  199. function renderFunctionReturn (function_obj, function_element) {
  200. var ret = '<div class="ui dropdown function_return">';
  201. if (function_obj.return_dimensions > 0) {
  202. ret += '<div class="text">'+ LocalizedStrings.getUI("vector") +':'+ LocalizedStrings.getUI(function_obj.return_type);
  203. if (function_obj.return_dimensions == 1) {
  204. ret += ' [ ] ';
  205. } else {
  206. ret += ' [ ] [ ] ';
  207. }
  208. ret += '</div>';
  209. } else {
  210. ret += '<div class="text">'+LocalizedStrings.getUI(function_obj.return_type) + '</div>';
  211. }
  212. ret += '<div class="menu">';
  213. for (var tm in Types) {
  214. ret += '<div class="item ' + (function_obj.return_type == tm.toLowerCase() && function_obj.return_dimensions < 1 ? ' selected ' : '') + '" data-type="' + tm + '" >' + LocalizedStrings.getUI(tm.toLowerCase()) + '</div>';
  215. }
  216. for (var tm in Types) {
  217. if (tm == Types.VOID.toUpperCase()) {
  218. continue;
  219. }
  220. ret += '<div class="item">'
  221. + '<i class="dropdown icon"></i>'
  222. + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase())
  223. + '<div class="menu">'
  224. + '<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>'
  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="2">[ ] [ ] </div>'
  226. + '</div>'
  227. + '</div>';
  228. }
  229. ret += '</div></div>';
  230. ret = $(ret);
  231. function_element.find('.function_return').append(ret);
  232. }
  233. export function renderFunction(function_obj) {
  234. var appender = '<div class="ui secondary segment function_div list-group-item">';
  235. if (function_obj.function_comment) {
  236. //appender += renderComment(function_obj.function_comment, sequence, true, -1);
  237. }
  238. appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
  239. 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>')
  240. + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
  241. appender += '<div class="function_signature_div">' + LocalizedStrings.getUI("function") + ' ';
  242. if (function_obj.is_main) {
  243. 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> '
  244. + ' <span class="parethesis_function">( </span> <div class="ui large labels parameters_list">';
  245. } else {
  246. appender += '<div class="ui function_return"></div>';
  247. appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">' + function_obj.name + '</span> </div> '
  248. + ' <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">';
  249. var menu_func = generateMenuButton(function_obj);
  250. /*menu_func.css("display","none")
  251. $('.functions_labels').append(menu_func);
  252. menu_func.fadeIn();*/
  253. $('.functions_labels').append(menu_func);
  254. }
  255. appender += '</div> <span class="parethesis_function"> ) </span> </div>'
  256. + (function_obj.is_hidden ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ');
  257. appender += '<div class="ui add_var_context add_var_button_function" style="float: left; display: none;"><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>';
  258. appender += '<div class="ui top attached segment variables_list_div" style="display: none;"></div>';
  259. appender += '<div class="ui inline_add_command" style="display: none;"><i class="icon plus circle purple"></i><i class="icon circle white back"></i><div class="ui icon button dropdown menu_commands orange" style="float: left;" ><i class="icon code"></i> <div class="menu"> ';
  260. appender += '<a class="item" data-command="' + Models.COMMAND_TYPES.reader + '"><i class="download icon"></i> ' + LocalizedStrings.getUI('text_read_var') + '</a>'
  261. + '<a class="item" data-command="' + Models.COMMAND_TYPES.writer + '"><i class="upload icon"></i> ' + LocalizedStrings.getUI('text_write_var') + '</a>'
  262. + '<a class="item" data-command="' + Models.COMMAND_TYPES.comment + '"><i class="quote left icon"></i> ' + LocalizedStrings.getUI('text_comment') + '</a>'
  263. + '<a class="item" data-command="' + Models.COMMAND_TYPES.attribution + '"><i class="arrow left icon"></i> ' + LocalizedStrings.getUI('text_attribution') + '</a>'
  264. + '<a class="item" data-command="' + Models.COMMAND_TYPES.functioncall + '"><i class="hand point right icon"></i> ' + LocalizedStrings.getUI('text_functioncall') + '</a>'
  265. + '<a class="item" data-command="' + Models.COMMAND_TYPES.iftrue + '" ><i class="random icon"></i> ' + LocalizedStrings.getUI('text_iftrue') + '</a>'
  266. + '<a class="item" data-command="' + Models.COMMAND_TYPES.repeatNtimes + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_repeatNtimes') + '</a>'
  267. + '<a class="item" data-command="' + Models.COMMAND_TYPES.whiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_whiletrue') + '</a>'
  268. + '<a class="item" data-command="' + Models.COMMAND_TYPES.dowhiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_dowhiletrue') + '</a>'
  269. + '<a class="item" data-command="' + Models.COMMAND_TYPES.switch + '"><i class="list icon"></i> ' + LocalizedStrings.getUI('text_switch') + '</a>'
  270. + '<a class="item" data-command="' + Models.COMMAND_TYPES.return + '"><i class="reply icon"></i> ' + LocalizedStrings.getUI('text_btn_return') + '</a>'
  271. + '</div></div></div>';
  272. appender += '<div class="ui bottom attached segment commands_list_div"></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')
  278. .data('fun', function_obj)
  279. .attr('droppable', true)
  280. .on('dragenter', function (e) {
  281. e.preventDefault();
  282. console.log('dragenter');
  283. console.log(e.target)
  284. $(e.target).addClass('div-over')
  285. //e.stopPropagation();
  286. }).on('dragover', function (e) {
  287. e.preventDefault();
  288. })
  289. .on('dragleave', function (e) {
  290. e.preventDefault();
  291. //e.stopPropagation();
  292. console.log("dragleave")
  293. $(e.target).removeClass('div-over')
  294. console.log(e.target)
  295. })
  296. .on('drop', function (e, bundle) {
  297. e.preventDefault();
  298. if (program_obj.dataTransfer == null)
  299. return;
  300. if (bundle) {
  301. e.clientX = bundle.clientX;
  302. e.clientY = bundle.clientY;
  303. }
  304. //console.log('ondrop ' + e.originalEvent.dataTransfer.getData("text"));
  305. console.log(e)
  306. $(e.target).removeClass('div-over')
  307. //var data = JSON.parse(e.originalEvent.dataTransfer.getData("text"));
  308. var data = program_obj.dataTransfer;
  309. if (data.type == 'command')
  310. CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, data.content);
  311. else if (data.type == 'var')
  312. CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "attribution", data.content);
  313. else {
  314. if (!data.content.parameters_list)
  315. data.content.parameters_list = [];
  316. CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "functioncall", data.content);
  317. }
  318. //program_obj.dataTransfer;
  319. program_obj.dataTransfer = null;
  320. }).on('click', function (e) {
  321. if (window.ghostNode) {
  322. console.log("drop click");
  323. $(document).off('mousemove').off('mousedown').off('keyup');
  324. $(window.ghostNode).remove();
  325. delete window.ghostNode;
  326. $(this).trigger('drop', [e]);
  327. $('.div-over').removeClass('div-over');
  328. }
  329. })
  330. .on('mouseover', function (evt) {
  331. if (window.ghostNode) {
  332. evt.type = 'dragenter';
  333. //$(this).trigger('dragenter');
  334. console.log('mouseover');
  335. $(this).trigger(evt);
  336. }
  337. })
  338. .on('mouseout', function (evt) {
  339. //$(this).trigger('dragleave');
  340. if (window.ghostNode) {
  341. evt.type = 'dragleave';
  342. console.log('mouseout');
  343. $(this).trigger(evt);
  344. }
  345. });
  346. renderFunctionReturn(function_obj, appender);
  347. addHandlers(function_obj, appender);
  348. // Rendering parameters:
  349. for (var j = 0; j < function_obj.parameters_list.length; j++) {
  350. renderParameter(function_obj, function_obj.parameters_list[j], appender);
  351. }
  352. // Rendering variables:
  353. for (var j = 0; j < function_obj.variables_list.length; j++) {
  354. VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
  355. }
  356. // Rendering commands:
  357. for (var j = 0; j < function_obj.commands.length; j++) {
  358. CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
  359. }
  360. $('.minimize_function_button').popup({
  361. content : LocalizedStrings.getUI("tooltip_minimize"),
  362. delay: {
  363. show: 750,
  364. hide: 0
  365. }
  366. });
  367. Sortable.create(appender.find(".variables_list_div")[0], {
  368. handle: '.ellipsis',
  369. animation: 100,
  370. ghostClass: 'ghost',
  371. group: 'local_vars_drag_' + program.functions.indexOf(function_obj),
  372. onEnd: function (evt) {
  373. updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  374. }
  375. });
  376. Sortable.create(appender.find(".commands_list_div")[0], {
  377. handle: '.command_drag',
  378. animation: 100,
  379. ghostClass: 'ghost',
  380. group: 'commands_drag_' + program.functions.indexOf(function_obj),
  381. onEnd: function (evt) {
  382. //updateSequenceLocals(evt.oldIndex, evt.newIndex, function_obj);
  383. }
  384. });
  385. if (!function_obj.is_main) {
  386. Sortable.create(appender.find(".container_parameters_list")[0], {
  387. handle: '.ellipsis',
  388. animation: 100,
  389. ghostClass: 'ghost',
  390. group: 'parameters_drag_' + program.functions.indexOf(function_obj),
  391. onEnd: function (evt) {
  392. updateSequenceParameters(evt.oldIndex, evt.newIndex, function_obj);
  393. }
  394. });
  395. }
  396. return appender;
  397. }
  398. export function initVisualUI () {
  399. // MUST USE CONST, LET, OR VAR !!!!!!
  400. const mainDiv = $('#visual-main-div');
  401. // fill mainDiv with functions and globals...
  402. // renderAlgorithm()...
  403. $('.add_function_button').on('click', () => {
  404. addFunctionHandler();
  405. });
  406. $('.add_global_button').on('click', () => {
  407. window.insertContext = true;
  408. GlobalsManagement.addGlobal(program, true);
  409. });
  410. $('.run_button').on('click', () => {
  411. runCode();
  412. });
  413. $('.visual_coding_button').on('click', () => {
  414. toggleVisualCoding();
  415. });
  416. $('.textual_coding_button').on('click', () => {
  417. toggleTextualCoding();
  418. });
  419. $('.assessment').on('click', () => {
  420. runCodeAssessment();
  421. is_iassign = true;
  422. });
  423. $('.div_toggle_console').on('click', () => {
  424. toggleConsole();
  425. });
  426. $('.expand_button').on('click', () => {
  427. full_screen();
  428. });
  429. $('.main_title h2').prop('title', LocalizedStrings.getUI('text_ivprog_description'));
  430. }
  431. var is_iassign = false;
  432. $(document).ready(function () {
  433. var time_show = 750;
  434. $('.visual_coding_button').popup({
  435. content: LocalizedStrings.getUI("tooltip_visual"),
  436. delay: {
  437. show: time_show,
  438. hide: 0
  439. }
  440. });
  441. $('.textual_coding_button').popup({
  442. content: LocalizedStrings.getUI("tooltip_textual"),
  443. delay: {
  444. show: time_show,
  445. hide: 0
  446. }
  447. });
  448. $('.upload_file_button').popup({
  449. content: LocalizedStrings.getUI("tooltip_upload"),
  450. delay: {
  451. show: time_show,
  452. hide: 0
  453. }
  454. });
  455. $('.download_file_button').popup({
  456. content: LocalizedStrings.getUI("tooltip_download"),
  457. delay: {
  458. show: time_show,
  459. hide: 0
  460. }
  461. });
  462. $('.undo_button').popup({
  463. content: LocalizedStrings.getUI("tooltip_undo"),
  464. delay: {
  465. show: time_show,
  466. hide: 0
  467. }
  468. });
  469. $('.redo_button').popup({
  470. content: LocalizedStrings.getUI("tooltip_redo"),
  471. delay: {
  472. show: time_show,
  473. hide: 0
  474. }
  475. });
  476. $('.run_button').popup({
  477. content: LocalizedStrings.getUI("tooltip_run"),
  478. delay: {
  479. show: time_show,
  480. hide: 0
  481. }
  482. });
  483. $('.assessment_button').popup({
  484. content: LocalizedStrings.getUI("tooltip_evaluate"),
  485. delay: {
  486. show: time_show,
  487. hide: 0
  488. }
  489. });
  490. $('.help_button').popup({
  491. content : LocalizedStrings.getUI("tooltip_help") + ' - ' + LocalizedStrings.getUI("text_ivprog_version"),
  492. delay: {
  493. show: time_show,
  494. hide: 0
  495. }
  496. });
  497. $('.add_global_button').popup({
  498. content: LocalizedStrings.getUI("tooltip_add_global"),
  499. delay: {
  500. show: time_show,
  501. hide: 0
  502. }
  503. });
  504. $('.div_toggle_console').popup({
  505. content: LocalizedStrings.getUI("tooltip_console"),
  506. delay: {
  507. show: time_show,
  508. hide: 0
  509. }
  510. });
  511. Sortable.create(listWithHandle, {
  512. handle: '.glyphicon-move',
  513. animation: 100,
  514. ghostClass: 'ghost',
  515. group: 'functions_divs_drag',
  516. onEnd: function (evt) {
  517. updateSequenceFunction(evt.oldIndex, evt.newIndex);
  518. }
  519. });
  520. var listGlobalsHandle = document.getElementById("listGlobalsHandle");
  521. Sortable.create(listGlobalsHandle, {
  522. handle: '.ellipsis',
  523. animation: 100,
  524. ghostClass: 'ghost',
  525. group: 'globals_divs_drag',
  526. onEnd: function (evt) {
  527. updateSequenceGlobals(evt.oldIndex, evt.newIndex);
  528. }
  529. });
  530. renderAlgorithm();
  531. });
  532. function updateSequenceParameters(oldIndex, newIndex, function_obj) {
  533. function_obj.parameters_list.splice(newIndex, 0, function_obj.parameters_list.splice(oldIndex, 1)[0]);
  534. }
  535. function updateSequenceLocals(oldIndex, newIndex, function_obj) {
  536. function_obj.variables_list.splice(newIndex, 0, function_obj.variables_list.splice(oldIndex, 1)[0]);
  537. }
  538. function updateSequenceGlobals(oldIndex, newIndex) {
  539. program_obj.globals.splice(newIndex, 0, program_obj.globals.splice(oldIndex, 1)[0]);
  540. }
  541. function updateSequenceFunction(oldIndex, newIndex) {
  542. program_obj.functions.splice(newIndex, 0, program_obj.functions.splice(oldIndex, 1)[0]);
  543. }
  544. function runCodeAssessment () {
  545. window.studentGrade = null;
  546. studentTemp = null;
  547. const strCode = CodeManagement.generate();
  548. if (strCode == null) {
  549. return;
  550. }
  551. toggleConsole(true);
  552. if(domConsole == null)
  553. domConsole = new DOMConsole("#ivprog-term");
  554. $("#ivprog-term").slideDown(500);
  555. const runner = new IVProgAssessment(strCode, testCases, domConsole);
  556. runner.runTest().then(grade => {
  557. if (!is_iassign) {
  558. parent.getEvaluationCallback(grade);
  559. } else {
  560. is_iassign = false;
  561. }
  562. }).catch( err => domConsole.err(err.message));
  563. }
  564. function runCode() {
  565. const strCode = CodeManagement.generate();
  566. if (strCode == null) {
  567. return;
  568. }
  569. toggleConsole(true);
  570. if(domConsole == null)
  571. domConsole = new DOMConsole("#ivprog-term");
  572. $("#ivprog-term").slideDown(500);
  573. try {
  574. const parser = IVProgParser.createParser(strCode);
  575. const analyser = new SemanticAnalyser(parser.parseTree());
  576. const data = analyser.analyseTree();
  577. const proc = new IVProgProcessor(data);
  578. proc.registerInput(domConsole);
  579. proc.registerOutput(domConsole);
  580. $("#ivprog-term").addClass('ivprog-term-active');
  581. proc.interpretAST().then( _ => {
  582. domConsole.info("Programa executado com sucesso!");
  583. $("#ivprog-term").removeClass('ivprog-term-active');
  584. }).catch(err => {
  585. domConsole.err(err.message);
  586. $("#ivprog-term").removeClass('ivprog-term-active');
  587. })
  588. } catch (error) {
  589. domConsole.err(error.message);
  590. console.log(error);
  591. }
  592. }
  593. function toggleConsole (is_running) {
  594. if (is_running) {
  595. $('.ivprog-term-div').css('display', 'block');
  596. $('#ivprog-term').css('min-height', '160px');
  597. $('#ivprog-term').css('margin-top', '-170px');
  598. return;
  599. }
  600. if ($('#ivprog-term').css('min-height') == '160px') {
  601. // esconder
  602. $('.ivprog-term-div').css('display', 'none');
  603. $('#ivprog-term').css('min-height', '0');
  604. $('#ivprog-term').css('margin-top', '-30px');
  605. $('#ivprog-term').css('padding', '5px');
  606. } else {
  607. // mostrar
  608. $('.ivprog-term-div').css('display', 'block');
  609. $('#ivprog-term').css('min-height', '160px');
  610. $('#ivprog-term').css('margin-top', '-170px');
  611. }
  612. }
  613. function waitToCloseConsole () {
  614. domConsole.info("Aperte qualquer tecla para fechar...");
  615. const p = new Promise((resolve, _) => {
  616. domConsole.requestInput(resolve, true);
  617. });
  618. p.then( _ => {
  619. domConsole.dispose();
  620. domConsole = null;
  621. $("#ivprog-term").hide();
  622. })
  623. }
  624. function toggleTextualCoding () {
  625. var code = CodeManagement.generate();
  626. if (code == null) {
  627. return;
  628. }
  629. $('.ivprog_visual_panel').css('display', 'none');
  630. $('.ivprog_textual_panel').css('display', 'block');
  631. $('.ivprog_textual_panel').removeClass('loading');
  632. $('.ivprog_textual_code').text(code);
  633. $('.visual_coding_button').removeClass('active');
  634. $('.textual_coding_button').addClass('active');
  635. $('.tabs').addClass('loading')
  636. }
  637. function toggleVisualCoding () {
  638. $('.ivprog_textual_panel').addClass('loading');
  639. $('.ivprog_textual_panel').css('display', 'none');
  640. $('.ivprog_visual_panel').css('display', 'block');
  641. $('.textual_coding_button').removeClass('active');
  642. $('.visual_coding_button').addClass('active');
  643. $('.menu .item').tab();
  644. $('.tabs').removeClass('loading')
  645. }
  646. function removeParameter (function_obj, parameter_obj, parameter_container) {
  647. var index = function_obj.parameters_list.indexOf(parameter_obj);
  648. if (index > -1) {
  649. window.insertContext = true;
  650. function_obj.parameters_list.splice(index, 1);
  651. }
  652. $(parameter_container).fadeOut();
  653. }
  654. function updateParameterType (parameter_obj, new_type, new_dimensions = 0) {
  655. parameter_obj.type = new_type;
  656. parameter_obj.dimensions = new_dimensions;
  657. if (new_dimensions > 0) {
  658. parameter_obj.rows = new_dimensions;
  659. parameter_obj.columns = 2;
  660. }
  661. }
  662. function renderParameter (function_obj, parameter_obj, function_container) {
  663. var ret = "";
  664. ret += '<div class="ui label function_name_parameter pink"><i class="ui icon ellipsis vertical inverted"></i>';
  665. ret += '<div class="ui dropdown parameter_type">';
  666. if (parameter_obj.dimensions > 0) {
  667. ret += '<div class="text">'+ LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(parameter_obj.type);
  668. if (parameter_obj.dimensions == 1) {
  669. ret += ' [ ] ';
  670. } else {
  671. ret += ' [ ] [ ] ';
  672. }
  673. ret += '</div>';
  674. } else {
  675. ret += '<div class="text">' + LocalizedStrings.getUI(parameter_obj.type) + '</div>';
  676. }
  677. ret += '<div class="menu">';
  678. for (var tm in Types) {
  679. if (tm == Types.VOID.toUpperCase()) {
  680. continue;
  681. }
  682. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="' + tm + '" >' + LocalizedStrings.getUI(tm.toLowerCase()) + '</div>';
  683. }
  684. for (var tm in Types) {
  685. if (tm == Types.VOID.toUpperCase()) {
  686. continue;
  687. }
  688. ret += '<div class="item">'
  689. + '<i class="dropdown icon"></i>'
  690. + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase())
  691. + '<div class="menu">'
  692. + '<div class="item" data-text="' + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase()) + ' [ ] " data-type="' + tm + '" data-dimensions="1">[ ]</div>'
  693. + '<div class="item" data-text="' + LocalizedStrings.getUI('vector') + ':' + LocalizedStrings.getUI(tm.toLowerCase()) + ' [ ] [ ] " data-type="' + tm + '" data-dimensions="2">[ ] [ ] </div>'
  694. + '</div>'
  695. + '</div>';
  696. }
  697. ret += '</div></div>';
  698. ret += '<div class="parameter_div_edit"><span class="span_name_parameter label_enable_name_parameter">' + parameter_obj.name + '</span></div> ';
  699. ret += ' <i class="yellow inverted icon times remove_parameter"></i></div>';
  700. ret = $(ret);
  701. function_container.find('.container_parameters_list').append(ret);
  702. ret.find('.remove_parameter').on('click', function (e) {
  703. removeParameter(function_obj, parameter_obj, ret);
  704. });
  705. ret.find('.ui.dropdown.parameter_type').dropdown({
  706. onChange: function (value, text, $selectedItem) {
  707. if ($selectedItem.data('dimensions')) {
  708. updateParameterType(parameter_obj, Types[$selectedItem.data('type')], $selectedItem.data('dimensions'));
  709. } else {
  710. updateParameterType(parameter_obj, Types[$selectedItem.data('type')]);
  711. }
  712. },
  713. selectOnKeydown: false
  714. });
  715. ret.find('.label_enable_name_parameter').on('click', function (e) {
  716. enableNameParameterUpdate(parameter_obj, ret);
  717. });
  718. return ret;
  719. }
  720. function updateParameterName (parameter_var, new_name, parameter_obj_dom, function_obj) {
  721. if (isValidIdentifier(new_name)) {
  722. if (variableNameAlreadyExists(new_name, function_obj)) {
  723. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getUI('inform_valid_variable_duplicated'));
  724. } else {
  725. parameter_var.name = new_name;
  726. }
  727. } else {
  728. Utils.renderErrorMessage(parameter_obj_dom.find('.parameter_div_edit'), LocalizedStrings.getUI('inform_valid_name'));
  729. }
  730. }
  731. function variableNameAlreadyExists (name_var, function_obj) {
  732. if (function_obj.parameters_list) {
  733. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  734. if (function_obj.parameters_list[i].name == name_var) {
  735. return true;
  736. }
  737. }
  738. }
  739. if (function_obj.variables_list) {
  740. for (var i = 0; i < function_obj.variables_list.length; i++) {
  741. if (function_obj.variables_list[i].name == name_var) {
  742. return true;
  743. }
  744. }
  745. }
  746. return false;
  747. }
  748. function updateFunctionName (function_var, new_name, function_obj_dom) {
  749. if (isValidIdentifier(new_name)) {
  750. if (functionNameAlreadyExists(new_name)) {
  751. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getUI('inform_valid_name_duplicated'));
  752. } else {
  753. function_var.name = new_name;
  754. }
  755. } else {
  756. Utils.renderErrorMessage(function_obj_dom.find('.function_name_div'), LocalizedStrings.getUI('inform_valid_name'));
  757. }
  758. }
  759. function functionNameAlreadyExists (function_name) {
  760. for (var i = 0; i < window.program_obj.functions.length; i++) {
  761. if (window.program_obj.functions[i].name == function_name) {
  762. return true;
  763. }
  764. }
  765. return false;
  766. }
  767. function isValidIdentifier (identifier_str) {
  768. return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(identifier_str);
  769. }
  770. var opened_name_parameter = false;
  771. var opened_input_parameter = null;
  772. function enableNameParameterUpdate (parameter_obj, parent_node) {
  773. if (opened_name_parameter) {
  774. opened_input_parameter.focus();
  775. return;
  776. }
  777. opened_name_parameter = true;
  778. parent_node = $(parent_node);
  779. var input_field;
  780. parent_node.find('.span_name_parameter').text('');
  781. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='" + parameter_obj.name + "' />");
  782. input_field.insertBefore(parent_node.find('.span_name_parameter'));
  783. input_field.on('input', function () {
  784. var inputWidth = input_field.textWidth() + 10;
  785. opened_input_parameter = input_field;
  786. input_field.focus();
  787. var tmpStr = input_field.val();
  788. input_field.val('');
  789. input_field.val(tmpStr);
  790. input_field.css({
  791. width: inputWidth
  792. })
  793. }).trigger('input');
  794. input_field.focusout(function () {
  795. /// update array:
  796. if (input_field.val().trim()) {
  797. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  798. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  799. }
  800. input_field.off();
  801. input_field.remove();
  802. /// update elements:
  803. opened_name_parameter = false;
  804. opened_input_parameter = false;
  805. });
  806. input_field.on('keydown', function (e) {
  807. var code = e.keyCode || e.which;
  808. if (code == 13) {
  809. if (input_field.val().trim()) {
  810. updateParameterName(parameter_obj, input_field.val().trim(), parent_node, function_obj);
  811. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  812. }
  813. input_field.off();
  814. input_field.remove();
  815. /// update elements:
  816. opened_name_parameter = false;
  817. opened_input_parameter = false;
  818. }
  819. if (code == 27) {
  820. parent_node.find('.span_name_parameter').text(parameter_obj.name);
  821. input_field.off();
  822. input_field.remove();
  823. /// update elements:
  824. opened_name_parameter = false;
  825. opened_input_parameter = false;
  826. }
  827. });
  828. input_field.select();
  829. }
  830. var opened_name_function = false;
  831. var opened_input = null;
  832. var previousPadding = null;
  833. function enableNameFunctionUpdate(function_obj, parent_node) {
  834. if (opened_name_function) {
  835. opened_input.focus();
  836. return;
  837. }
  838. parent_node = $(parent_node);
  839. parent_node.find('.span_name_function').text('');
  840. var input_field;
  841. if (!previousPadding) {
  842. previousPadding = parent_node.find('.span_name_function').css('padding-left');
  843. }
  844. parent_node.find('.span_name_function').css('padding-left', '0');
  845. parent_node.find('.span_name_function').css('padding-right', '0');
  846. input_field = $("<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='" + function_obj.name + "' />");
  847. input_field.insertBefore(parent_node.find('.span_name_function'));
  848. input_field.on('input', function () {
  849. var inputWidth = input_field.textWidth() + 10;
  850. opened_input = input_field;
  851. input_field.focus();
  852. var tmpStr = input_field.val();
  853. input_field.val('');
  854. input_field.val(tmpStr);
  855. input_field.css({
  856. width: inputWidth
  857. })
  858. }).trigger('input');
  859. input_field.focusout(function () {
  860. /// update array:
  861. if (input_field.val().trim()) {
  862. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  863. }
  864. input_field.off();
  865. input_field.remove();
  866. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  867. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  868. parent_node.find('.span_name_function').text(function_obj.name);
  869. /// update elements:
  870. opened_name_function = false;
  871. opened_input = false;
  872. });
  873. input_field.on('keydown', function (e) {
  874. var code = e.keyCode || e.which;
  875. if (code == 13) {
  876. if (input_field.val().trim()) {
  877. updateFunctionName(function_obj, input_field.val().trim(), parent_node);
  878. }
  879. input_field.off();
  880. input_field.remove();
  881. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  882. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  883. parent_node.find('.span_name_function').text(function_obj.name);
  884. /// update elements:
  885. opened_name_function = false;
  886. opened_input = false;
  887. }
  888. if (code == 27) {
  889. input_field.off();
  890. input_field.remove();
  891. parent_node.find('.span_name_function').css('padding-left', previousPadding);
  892. parent_node.find('.span_name_function').css('padding-right', previousPadding);
  893. parent_node.find('.span_name_function').text(function_obj.name);
  894. /// update elements:
  895. opened_name_function = false;
  896. opened_input = false;
  897. }
  898. });
  899. input_field.select();
  900. }
  901. /****************************************************
  902. //DOUGLAS
  903. *******************************************************/
  904. export function generateMenuButton(function_obj) {
  905. if (function_obj.identifier) {
  906. var identifier = LocalizedStrings.getUI(function_obj.identifier);
  907. var menu_button = '<button class="fluid ui container segment labeled icon button list-group-item menu-item" draggable="true" data-function="' + identifier + '"><i class="code icon"></i> <span class="function_name">' + identifier + '</span> (<span class="function_params"></span>) : <span class="function_return_type">' + LocalizedStrings.getUI(function_obj.return_type) + '</span></button>';
  908. } else {
  909. var menu_button = '<button class="fluid ui container segment labeled icon button list-group-item menu-item" draggable="true" data-function="' + function_obj.name + '"><i class="code icon"></i> <span class="function_name">' + function_obj.name + '</span> (<span class="function_params"></span>) : <span class="function_return_type">' + LocalizedStrings.getUI(function_obj.return_type) + '</span></button>';
  910. }
  911. var params = "";
  912. menu_button = $(menu_button);
  913. for (var j = 0; j < function_obj.parameters_list.length; j++) {
  914. if (j > 0)
  915. params += ',';
  916. if (!function_obj.identifier) {
  917. params += LocalizedStrings.getUI(function_obj.parameters_list[j].type);
  918. } else {
  919. params += function_obj.parameters_list[j].type;
  920. }
  921. }
  922. menu_button
  923. .data('fun', function_obj)
  924. .on('dragstart', function (e) {
  925. program_obj.dataTransfer = { type: "function", content: function_obj };
  926. //e.originalEvent.dataTransfer.setData("text", JSON.stringify({type:"function",content:function_obj}));
  927. //evt.originalEvent.dataTransfer.setData("text",$(this).data('command'));
  928. })
  929. .find('.function_params').text(params)
  930. .find('.function_return_type').text(function_obj.type);
  931. menu_button
  932. .on('click', function (evt) {
  933. $(this).trigger('dragstart');
  934. if (window.ghostNode) {
  935. $(window.ghostNode).remove();
  936. $(document).off('mousemove');
  937. }
  938. window.ghostNode = $(this).clone();
  939. ghostNode.outerWidth($(this).outerWidth());
  940. ghostNode.draggable().appendTo('body');
  941. ghostNode.css('position', 'absolute');
  942. ghostNode.css('left', evt.pageX);
  943. ghostNode.css('top', evt.pageY);
  944. evt.type = 'drag';
  945. evt.target = ghostNode[0];
  946. ghostNode.trigger(evt);
  947. $(document).on('mousemove', function (evt) {
  948. ghostNode.css('left', evt.pageX);
  949. ghostNode.css('top', evt.pageY);
  950. });
  951. $(document).on('mousedown', function (evt) {
  952. console.log("length ===");
  953. console.log($(evt.target).closest(".commands_list_div"))
  954. if ($(evt.target).closest(".commands_list_div").length <= 0) {
  955. if (window.ghostNode) {
  956. console.log("drop click");
  957. $('.div-over').removeClass('div-over');
  958. $(window.ghostNode).remove();
  959. delete window.ghostNode;
  960. $(document).off('mousemove').off('mousedown').off('keyup');
  961. }
  962. }
  963. });
  964. $(document).keyup(function (e) {
  965. console.log("KeyUp")
  966. if (e.key === "Escape") {
  967. console.log("escape");
  968. $('.div-over').removeClass('div-over');
  969. $(window.ghostNode).remove();
  970. delete window.ghostNode;
  971. $(document).off('mousemove').off('mousedown').off('keyup');
  972. }
  973. });
  974. });
  975. return menu_button;
  976. }
  977. removeFunction = function (function_obj) {
  978. var index = program.functions.indexOf(function_obj);
  979. if (index > -1) {
  980. program.functions.splice(index, 1);
  981. }
  982. $('.functions_labels > [data-function=' + function_obj.name + ']').remove();
  983. }
  984. // renderFunction = function (function_obj) {
  985. // var appender = '<div class="ui secondary segment function_div list-group-item">';
  986. // if (function_obj.function_comment) {
  987. // //appender += renderComment(function_obj.function_comment, sequence, true, -1);
  988. // }
  989. // appender += '<span class="glyphicon glyphicon-move move_function" aria-hidden="true"><i class="icon sort alternate vertical"></i></span>';
  990. // 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>')
  991. // + '<button class="ui icon button tiny minimize_function_button"><i class="icon window minimize"></i></button>';
  992. // appender += '<div class="ui small icon buttons add_var_top_button"><div class="ui icon button add_var_button_function"><i class="icon superscript"></i></div>';
  993. // appender += '<div class="ui icon button dropdown menu_commands" ><i class="icon code"></i> <div class="menu"> ';
  994. // appender += '<a class="item" data-command="' + Models.COMMAND_TYPES.reader + '"><i class="download icon"></i> ' + LocalizedStrings.getUI('text_read_var') + '</a>'
  995. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.writer + '"><i class="upload icon"></i> ' + LocalizedStrings.getUI('text_write_var') + '</a>'
  996. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.comment + '"><i class="quote left icon"></i> ' + LocalizedStrings.getUI('text_comment') + '</a>'
  997. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.attribution + '"><i class="arrow left icon"></i> ' + LocalizedStrings.getUI('text_attribution') + '</a>'
  998. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.functioncall + '"><i class="hand point right icon"></i> ' + LocalizedStrings.getUI('text_functioncall') + '</a>'
  999. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.iftrue + '" ><i class="random icon"></i> ' + LocalizedStrings.getUI('text_iftrue') + '</a>'
  1000. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.repeatNtimes + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_repeatNtimes') + '</a>'
  1001. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.whiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_whiletrue') + '</a>'
  1002. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.dowhiletrue + '"><i class="sync icon"></i> ' + LocalizedStrings.getUI('text_dowhiletrue') + '</a>'
  1003. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.switch + '"><i class="list icon"></i> ' + LocalizedStrings.getUI('text_switch') + '</a>'
  1004. // + '<a class="item" data-command="' + Models.COMMAND_TYPES.return + '"><i class="reply icon"></i> ' + LocalizedStrings.getUI('text_btn_return') + '</a>'
  1005. // + '</div></div></div>';
  1006. // appender += '<div class="function_signature_div">' + LocalizedStrings.getUI("function") + ' ';
  1007. // if (function_obj.is_main) {
  1008. // 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> '
  1009. // + '( <div class="ui large labels parameters_list">';
  1010. // } else {
  1011. // appender += '<div class="ui function_return"></div>';
  1012. // appender += '<div class="function_name_div function_name_div_updated"><span class="span_name_function name_function_updated">' + function_obj.name + '</span> </div> '
  1013. // + ' <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">';
  1014. // $('.functions_labels').append(generateMenuButton(function_obj));
  1015. // console.log("aqui");
  1016. // //var menu_button = $('.functions_labels > [data-function=' + function_obj.name + ']');
  1017. // //var params = "";
  1018. // //menu_button
  1019. // }
  1020. // appender += '</div> ) </div>'
  1021. // + (function_obj.is_hidden ? ' <div class="function_area" style="display: none;"> ' : ' <div class="function_area"> ')
  1022. // + '<div class="ui top attached segment variables_list_div">'
  1023. // /*+ renderVariables(function_obj, sequence)*/
  1024. // + '</div>'
  1025. // + '<div class="ui bottom attached segment commands_list_div" id="function_drag_cmd_">';
  1026. // appender += '</div>';
  1027. // appender += '<div class="function_close_div"></div>'
  1028. // + '</div>'
  1029. // + '</div>';
  1030. // appender = $(appender);
  1031. // $('.all_functions').append(appender);
  1032. // appender.data('fun', function_obj);
  1033. // appender.find('.commands_list_div')
  1034. // .data('fun', function_obj)
  1035. // .attr('droppable', true)
  1036. // .on('dragenter', function (e) {
  1037. // e.preventDefault();
  1038. // console.log('dragenter');
  1039. // console.log(e.target)
  1040. // $(e.target).addClass('div-over')
  1041. // //e.stopPropagation();
  1042. // }).on('dragover', function (e) {
  1043. // e.preventDefault();
  1044. // })
  1045. // .on('dragleave', function (e) {
  1046. // e.preventDefault();
  1047. // //e.stopPropagation();
  1048. // console.log("dragleave")
  1049. // $(e.target).removeClass('div-over')
  1050. // console.log(e.target)
  1051. // })
  1052. // .on('drop', function (e, bundle) {
  1053. // e.preventDefault();
  1054. // if (bundle) {
  1055. // e.clientX = bundle.clientX;
  1056. // e.clientY = bundle.clientY;
  1057. // }
  1058. // //console.log('ondrop ' + e.originalEvent.dataTransfer.getData("text"));
  1059. // console.log(e)
  1060. // $(e.target).removeClass('div-over')
  1061. // //var data = JSON.parse(e.originalEvent.dataTransfer.getData("text"));
  1062. // var data = program_obj.dataTransfer;
  1063. // if (data.type == 'command')
  1064. // CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, data.content);
  1065. // else if (data.type == 'var')
  1066. // CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "attribution", data.content);
  1067. // else {
  1068. // if (!data.content.parameters_list)
  1069. // data.content.parameters_list = [];
  1070. // CommandsManagement.prepareManageCommand(function_obj, $(e.target).closest('.function_div'), e, "functioncall", data.content);
  1071. // }
  1072. // //program_obj.dataTransfer;
  1073. // }).on('click', function (e) {
  1074. // if (window.ghostNode) {
  1075. // console.log("drop click");
  1076. // $(document).off('mousemove').off('mousedown').off('keyup');
  1077. // $(window.ghostNode).remove();
  1078. // delete window.ghostNode;
  1079. // $(this).trigger('drop', [e]);
  1080. // $('.div-over').removeClass('div-over');
  1081. // }
  1082. // })
  1083. // .on('mouseover', function (evt) {
  1084. // if (window.ghostNode) {
  1085. // evt.type = 'dragenter';
  1086. // //$(this).trigger('dragenter');
  1087. // console.log('mouseover');
  1088. // $(this).trigger(evt);
  1089. // }
  1090. // })
  1091. // .on('mouseout', function (evt) {
  1092. // //$(this).trigger('dragleave');
  1093. // if (window.ghostNode) {
  1094. // evt.type = 'dragleave';
  1095. // console.log('mouseout');
  1096. // $(this).trigger(evt);
  1097. // }
  1098. // });
  1099. // renderFunctionReturn(function_obj, appender);
  1100. // addHandlers(function_obj, appender);
  1101. // // Rendering parameters:
  1102. // for (var j = 0; j < function_obj.parameters_list.length; j++) {
  1103. // renderParameter(function_obj, function_obj.parameters_list[j], appender);
  1104. // }
  1105. // // Rendering variables:
  1106. // for (var j = 0; j < function_obj.variables_list.length; j++) {
  1107. // VariablesManagement.renderVariable(appender, function_obj.variables_list[j], function_obj);
  1108. // }
  1109. // // Rendering commands:
  1110. // for (var j = 0; j < function_obj.commands.length; j++) {
  1111. // CommandsManagement.renderCommand(function_obj.commands[j], $(appender.find('.commands_list_div')[0]), 3, function_obj);
  1112. // }
  1113. // console.log('kk')
  1114. // console.log($($('.function_div')[0]).data('fun'))
  1115. // console.log(appender.data('fun'))
  1116. // }
  1117. initVisualUI = function () {
  1118. // MUST USE CONST, LET, OR VAR !!!!!!
  1119. const mainDiv = $('#visual-main-div');
  1120. // fill mainDiv with functions and globals...
  1121. // renderAlgorithm()...
  1122. $('.add_function_button').on('click', () => {
  1123. addFunctionHandler();
  1124. });
  1125. $('.add_global_button').on('click', () => {
  1126. GlobalsManagement.addGlobal(program);
  1127. });
  1128. $('.run_button').on('click', () => {
  1129. runCode();
  1130. });
  1131. $('.visual_coding_button').on('click', () => {
  1132. toggleVisualCoding();
  1133. });
  1134. $('.textual_coding_button').on('click', () => {
  1135. toggleTextualCoding();
  1136. });
  1137. $('.assessment').on('click', () => {
  1138. runCodeAssessment();
  1139. is_iassign = true;
  1140. });
  1141. $('.div_toggle_console').on('click', () => {
  1142. toggleConsole();
  1143. });
  1144. var commands = [
  1145. { type: Models.COMMAND_TYPES.reader, icon: "download", text: LocalizedStrings.getUI('text_read_var'), alt: "Solicita a introdução de um valor pelo teclado e grava em uma variável."},
  1146. { type: Models.COMMAND_TYPES.writer, icon: "upload", text: LocalizedStrings.getUI('text_write_var'), alt: "Imprime o conteúdo de uma variável, valor ou função. Pode somar várias variáveis ou concatenar sequências de texto." },
  1147. { type: Models.COMMAND_TYPES.comment, icon: "quote left", text: LocalizedStrings.getUI('text_comment'), alt: "Permite a inserção de comentários do autor. Serão desconsiderados na execução." },
  1148. { type: Models.COMMAND_TYPES.attribution, icon: "arrow left", text: LocalizedStrings.getUI('text_attribution'), alt: "Resolve uma expressão e armazena em uma variável." },
  1149. { type: Models.COMMAND_TYPES.iftrue, icon: "random", text: LocalizedStrings.getUI('text_iftrue'), alt: "Avalia uma condição como verdadeira ou falsa, escolhendo entre dois fluxos dependendo do valor." },
  1150. { type: Models.COMMAND_TYPES.repeatNtimes, icon: "sync", text: LocalizedStrings.getUI('text_repeatNtimes'), alt: "Repete um bloco de código N vezes." },
  1151. { type: Models.COMMAND_TYPES.whiletrue, icon: "sync", text: LocalizedStrings.getUI('text_whiletrue'), alt: "Enquanto a condição for verdadeira, repete um bloco de código, avaliando a condição antes de cada execução." },
  1152. { type: Models.COMMAND_TYPES.dowhiletrue, icon: "sync", text: LocalizedStrings.getUI('text_dowhiletrue'), alt: "Enquanto a condição for verdadeira, repete um bloco de código, avaliando a condição depois de cada execução." },
  1153. { type: Models.COMMAND_TYPES.switch, icon: "list", text: LocalizedStrings.getUI('text_switch'), alt: "Avalia o valor de uma expressão e escolhe um bloco de código para executar dependendo do valor." },
  1154. { type: Models.COMMAND_TYPES.return, icon: "reply", text: LocalizedStrings.getUI('text_btn_return'), alt: "Retorna o valor ao término de uma função." },
  1155. //{ type: Models.COMMAND_TYPES.break, icon: "stop", text: LocalizedStrings.getUI('text_break') },
  1156. //{type: Models.COMMAND_TYPES.functioncall, icon: "hand point right", text: LocalizedStrings.getUI('text_functioncall')},
  1157. ];
  1158. for (var i = commands.length-1; i >= 0; i--) {
  1159. var command = '<button class="fluid ui container segment labeled icon button list-group-item menu-item" title="'+ commands[i].alt + '" draggable="true" data-command="' + commands[i].type + '"><i class="' + commands[i].icon + ' icon"></i> ' + commands[i].text + '</button>';
  1160. command = $(command);
  1161. command.on('dragstart', function (evt) {
  1162. //evt.originalEvent.dataTransfer.setData("text",$(this).data('command'));
  1163. //evt.originalEvent.dataTransfer.setData("text",JSON.stringify({type:"command",content:$(this).data('command')}));
  1164. program_obj.dataTransfer = { type: "command", content: $(this).data('command') };
  1165. console.log('dragstart')
  1166. console.log(evt);
  1167. });
  1168. command.on('click', function (evt) {
  1169. $(this).trigger('dragstart');
  1170. if (window.ghostNode) {
  1171. $(window.ghostNode).remove();
  1172. $(document).off('mousemove');
  1173. }
  1174. window.ghostNode = $(this).clone();
  1175. ghostNode.outerWidth($(this).outerWidth());
  1176. ghostNode.draggable().appendTo('body');
  1177. ghostNode.css('position', 'absolute');
  1178. ghostNode.css('left', evt.pageX);
  1179. ghostNode.css('top', evt.pageY);
  1180. evt.type = 'drag';
  1181. evt.target = ghostNode[0];
  1182. ghostNode.trigger(evt);
  1183. $(document).on('mousemove', function (evt) {
  1184. ghostNode.css('left', evt.pageX);
  1185. ghostNode.css('top', evt.pageY);
  1186. });
  1187. $(document).on('mousedown', function (evt) {
  1188. console.log("length ===");
  1189. console.log($(evt.target).closest(".commands_list_div"))
  1190. if ($(evt.target).closest(".commands_list_div").length <= 0) {
  1191. if (window.ghostNode) {
  1192. console.log("drop click");
  1193. $('.div-over').removeClass('div-over');
  1194. $(window.ghostNode).remove();
  1195. delete window.ghostNode;
  1196. $(document).off('mousemove').off('mousedown').off('keyup');
  1197. }
  1198. }
  1199. });
  1200. $(document).keyup(function (e) {
  1201. console.log("KeyUp")
  1202. if (e.key === "Escape") {
  1203. console.log("escape");
  1204. $('.div-over').removeClass('div-over');
  1205. $(window.ghostNode).remove();
  1206. delete window.ghostNode;
  1207. $(document).off('mousemove').off('mousedown').off('keyup');
  1208. }
  1209. });
  1210. });
  1211. $('.list-commands').prepend(command);
  1212. }
  1213. /*
  1214. $(document).on('mousemove',function(evt) {
  1215. if (typeof ghostNode !== 'undefined') {
  1216. ghostNode.css('left', evt.pageX - 15);
  1217. ghostNode.css('top', evt.pageY - 15);
  1218. }
  1219. });*/
  1220. var library_labels = $('.library_labels');
  1221. for (var i = 0; i < system_functions.length; i++) {
  1222. var system_button = generateMenuButton(system_functions[i]);
  1223. var category = system_functions[i].category;
  1224. var cat_accord = null;
  1225. if (library_labels.find('.' + category).length == 0) {
  1226. cat_accord =
  1227. '<div class="ui styled accordion ' + category + '"> \
  1228. <div class="title"> \
  1229. <i class="dropdown icon"></i> \
  1230. ' + LocalizedStrings.getUI(category) + ' \
  1231. </div> \
  1232. <div class="content"><div>';
  1233. library_labels.append(cat_accord);
  1234. }
  1235. cat_accord = $(library_labels.find('.' + category)[0]);
  1236. cat_accord.find('.content').append(system_button);
  1237. }
  1238. $('.accordion').accordion();
  1239. }
  1240. /*
  1241. renderParameter = function(function_obj, parameter_obj, function_container) {
  1242. var ret = "";
  1243. ret += '<div class="ui label function_name_parameter"><span class="span_name_parameter label_enable_name_parameter">'+parameter_obj.name+'</span> <i class="icon small pencil alternate enable_edit_name_parameter label_enable_name_parameter"></i>';
  1244. ret += '<div class="ui dropdown parameter_type">';
  1245. if (parameter_obj.dimensions > 0) {
  1246. ret += '<div class="text">'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(parameter_obj.type);
  1247. ret += '</div>';
  1248. } else {
  1249. ret += '<div class="text">'+LocalizedStrings.getUI(parameter_obj.type)+'</div>';
  1250. }
  1251. ret += '<i class="dropdown icon"></i>'
  1252. + '<div class="menu">';
  1253. for (var tm in Types) {
  1254. if (tm == Types.VOID.toUpperCase()) {
  1255. continue;
  1256. }
  1257. ret += '<div class="item ' + (parameter_obj.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  1258. }
  1259. for (var tm in Types) {
  1260. if (tm == Types.VOID.toUpperCase()) {
  1261. continue;
  1262. }
  1263. ret += '<div class="item">'
  1264. + '<i class="dropdown icon"></i>'
  1265. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  1266. + '<div class="menu">'
  1267. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  1268. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  1269. + '</div>'
  1270. + '</div>';
  1271. }
  1272. ret += '</div></div>';
  1273. ret += ' <i class="red icon times remove_parameter"></i></div>';
  1274. ret = $(ret);
  1275. function_container.find('.container_parameters_list').append(ret);
  1276. ret.find('.remove_parameter').on('click', function(e){
  1277. removeParameter(function_obj, parameter_obj, ret);
  1278. });
  1279. ret.find('.ui.dropdown.parameter_type').dropdown({
  1280. onChange: function(value, text, $selectedItem) {
  1281. if ($($selectedItem).data('dimensions')) {
  1282. updateParameterType(parameter_obj, Types[$($selectedItem).data('type')], $($selectedItem).data('dimensions'));
  1283. } else {
  1284. updateParameterType(parameter_obj, Types[$($selectedItem).data('type')]);
  1285. }
  1286. }
  1287. });
  1288. ret.find('.label_enable_name_parameter').on('click', function(e){
  1289. enableNameParameterUpdate(parameter_obj, ret);
  1290. });
  1291. }
  1292. enableNameFunctionUpdate = function(function_obj, parent_node) {
  1293. if (opened_name_function) {
  1294. $(opened_input).focus();
  1295. return;
  1296. }
  1297. $(parent_node).find('.span_name_function').text('');
  1298. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+function_obj.name+"' />" ).insertBefore($(parent_node).find('.span_name_function'));
  1299. $('.width-dynamic').on('input', function() {
  1300. var inputWidth = $(this).textWidth()+10;
  1301. opened_input = this;
  1302. $(this).focus();
  1303. var tmpStr = $(this).val();
  1304. $(this).val('');
  1305. $(this).val(tmpStr);
  1306. $(this).css({
  1307. width: inputWidth
  1308. })
  1309. }).trigger('input');
  1310. $('.width-dynamic').focusout(function() {
  1311. /// update array:
  1312. if ($(this).val().trim()) {
  1313. function_obj.name = $(this).val().trim();
  1314. }
  1315. $(this).remove();
  1316. $(parent_node).find('.span_name_function').text(function_obj.name);
  1317. /// update elements:
  1318. opened_name_function = false;
  1319. opened_input = false;
  1320. });
  1321. $('.width-dynamic').on('keydown', function(e) {
  1322. var code = e.keyCode || e.which;
  1323. if(code == 13) {
  1324. $('.functions_labels > [data-function=' + function_obj.name + ']')
  1325. .attr('data-function', $(this).val().trim())
  1326. .html('<i class="list icon"></i> ' + $(this).val().trim());
  1327. if ($(this).val().trim()) {
  1328. function_obj.name = $(this).val().trim();
  1329. }
  1330. $(this).remove();
  1331. $(parent_node).find('.span_name_function').text(function_obj.name);
  1332. /// update elements:
  1333. opened_name_function = false;
  1334. opened_input = false;
  1335. }
  1336. if(code == 27) {
  1337. $(this).remove();
  1338. $(parent_node).find('.span_name_function').text(function_obj.name);
  1339. /// update elements:
  1340. opened_name_function = false;
  1341. opened_input = false;
  1342. }
  1343. });
  1344. }
  1345. addParameter = function (function_obj, function_container) {
  1346. if (function_obj.parameters_list == null) {
  1347. function_obj.parameters_list = [];
  1348. }
  1349. var new_parameter = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI("new_parameter") + "_" + counter_new_parameters);
  1350. new_parameter.function_obj = function_obj;
  1351. function_obj.parameters_list.push(new_parameter);
  1352. counter_new_parameters ++;
  1353. renderParameter(function_obj, new_parameter, function_container);
  1354. //updateMenuButton(function_obj);
  1355. }
  1356. removeParameter = function (function_obj, parameter_obj, parameter_container) {
  1357. var index = function_obj.parameters_list.indexOf(parameter_obj);
  1358. if (index > -1) {
  1359. function_obj.parameters_list.splice(index, 1);
  1360. }
  1361. $(parameter_container).remove();
  1362. }*/
  1363. /*
  1364. updateReturnType = function (function_obj, new_type, new_dimensions = 0) {
  1365. function_obj.return_type = new_type;
  1366. function_obj.return_dimensions = new_dimensions;
  1367. var menu_button = $('.functions_labels > [data-function=' + function_obj.name + ']');
  1368. menu_button.find('.function_return_type').text(LocalizedStrings.getUI(new_type));
  1369. }*/
  1370. /*
  1371. updateParameterType = function (parameter_obj, new_type, new_dimensions = 0) {
  1372. parameter_obj.type = new_type;
  1373. parameter_obj.dimensions = new_dimensions;
  1374. if (new_dimensions > 0) {
  1375. parameter_obj.rows = new_dimensions;
  1376. parameter_obj.columns = 2;
  1377. }
  1378. //updateMenuButton(parameter_obj.function_obj);
  1379. }*/