commands_sidebar.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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 CommentsManagement from './commands/comment';
  8. import * as ReadersManagement from './commands/reader';
  9. import * as WritersManagement from './commands/writer';
  10. import * as AttributionsManagement from './commands/attribution';
  11. import * as IftruesManagement from './commands/iftrue';
  12. import * as RepeatNtimesManagement from './commands/repeatNtimes';
  13. import * as WhiletruesManagement from './commands/whiletrue';
  14. import * as DowhiletruesManagement from './commands/dowhiletrue';
  15. import * as SwitchesManagement from './commands/switch';
  16. import * as FunctioncallsManagement from './commands/functioncall';
  17. import * as VariableValueMenuManagement from './commands/variable_value_menu';
  18. import * as BreaksManagement from './commands/break';
  19. var has_element_created_draged = false;
  20. var which_element_is_draged = null;
  21. export function removeCommand (command, function_obj, dom_obj) {
  22. console.log('debugging removeCommand');
  23. console.log('command');
  24. console.log(command);
  25. console.log('function_obj');
  26. console.log(function_obj);
  27. console.log('dom_obj');
  28. console.log(dom_obj);
  29. if (function_obj.commands.indexOf(command) > -1) {
  30. function_obj.commands.splice(function_obj.commands.indexOf(command), 1);
  31. return true;
  32. }
  33. // Utilize dois parantNode, pois o primeiro é o div de comandos
  34. try {
  35. if (dom_obj.parent().parent().data('command').commands_block.indexOf(command) > -1) {
  36. dom_obj.parent().parent().data('command').commands_block.splice
  37. (dom_obj.parent().parent().data('command').commands_block.indexOf(command), 1);
  38. return true;
  39. }
  40. } catch (err) {}
  41. try {
  42. if (dom_obj.parent().parent().data('command').type == Models.COMMAND_TYPES.iftrue) {
  43. if (dom_obj.parent().parent().data('command').commands_else.indexOf(command) > -1) {
  44. dom_obj.parent().parent().data('command').commands_else.splice
  45. (dom_obj.parent().parent().data('command').commands_else.indexOf(command), 1);
  46. return true;
  47. }
  48. }
  49. } catch (err) {}
  50. console.log('veja: ');
  51. console.log(dom_obj.parent());
  52. if (dom_obj.parent().data('switchcase')) {
  53. console.log("o que encontrei: ");
  54. console.log(dom_obj.parent().data('switchcase'));
  55. dom_obj.parent().data('switchcase').commands_block.splice(dom_obj.parent().data('switchcase').commands_block.indexOf(command), 1);
  56. return true;
  57. }
  58. return false;
  59. }
  60. export function createFloatingCommand (function_obj, function_container, command_type, mouse_event) {
  61. var floatingObject;
  62. switch (command_type) {
  63. case Models.COMMAND_TYPES.break:
  64. floatingObject = BreaksManagement.createFloatingCommand();
  65. break;
  66. case Models.COMMAND_TYPES.comment:
  67. floatingObject = CommentsManagement.createFloatingCommand();
  68. break;
  69. case Models.COMMAND_TYPES.reader:
  70. floatingObject = ReadersManagement.createFloatingCommand();
  71. break;
  72. case Models.COMMAND_TYPES.writer:
  73. floatingObject = WritersManagement.createFloatingCommand();
  74. break;
  75. case Models.COMMAND_TYPES.attribution:
  76. floatingObject = AttributionsManagement.createFloatingCommand();
  77. break;
  78. case Models.COMMAND_TYPES.iftrue:
  79. floatingObject = IftruesManagement.createFloatingCommand();
  80. break;
  81. case Models.COMMAND_TYPES.repeatNtimes:
  82. floatingObject = RepeatNtimesManagement.createFloatingCommand();
  83. break;
  84. case Models.COMMAND_TYPES.whiletrue:
  85. floatingObject = WhiletruesManagement.createFloatingCommand();
  86. break;
  87. case Models.COMMAND_TYPES.dowhiletrue:
  88. floatingObject = DowhiletruesManagement.createFloatingCommand();
  89. break;
  90. case Models.COMMAND_TYPES.switch:
  91. floatingObject = SwitchesManagement.createFloatingCommand();
  92. break;
  93. case Models.COMMAND_TYPES.functioncall:
  94. floatingObject = FunctioncallsManagement.createFloatingCommand();
  95. break;
  96. }
  97. floatingObject.draggable().appendTo("body");
  98. floatingObject.mouseup(function(evt) {
  99. manageCommand(function_obj, function_container, evt, command_type);
  100. });
  101. floatingObject.css("position", "absolute");
  102. mouse_event.type = "mousedown.draggable";
  103. mouse_event.target = floatingObject[0];
  104. floatingObject.css("left", mouse_event.pageX - 15);
  105. floatingObject.css("top", mouse_event.pageY - 15);
  106. floatingObject.trigger(mouse_event);
  107. }
  108. // before_after_inside: 1 -> before, 2 -> after, 3 -> inside
  109. export function renderCommand (command, element_reference, before_after_inside, function_obj) {
  110. var createdElement;
  111. switch (command.type) {
  112. case Models.COMMAND_TYPES.comment:
  113. createdElement = CommentsManagement.renderCommand(command, function_obj);
  114. break;
  115. case Models.COMMAND_TYPES.break:
  116. createdElement = BreaksManagement.renderCommand(command, function_obj);
  117. break;
  118. case Models.COMMAND_TYPES.reader:
  119. createdElement = ReadersManagement.renderCommand(command, function_obj);
  120. break;
  121. case Models.COMMAND_TYPES.writer:
  122. createdElement = WritersManagement.renderCommand(command, function_obj);
  123. break;
  124. case Models.COMMAND_TYPES.attribution:
  125. createdElement = AttributionsManagement.renderCommand(command, function_obj);
  126. break;
  127. case Models.COMMAND_TYPES.functioncall:
  128. createdElement = FunctioncallsManagement.renderCommand(command, function_obj);
  129. break;
  130. case Models.COMMAND_TYPES.iftrue:
  131. createdElement = IftruesManagement.renderCommand(command, function_obj);
  132. break;
  133. case Models.COMMAND_TYPES.repeatNtimes:
  134. createdElement = RepeatNtimesManagement.renderCommand(command, function_obj);
  135. break;
  136. case Models.COMMAND_TYPES.whiletrue:
  137. createdElement = WhiletruesManagement.renderCommand(command, function_obj);
  138. break;
  139. case Models.COMMAND_TYPES.dowhiletrue:
  140. createdElement = DowhiletruesManagement.renderCommand(command, function_obj);
  141. break;
  142. case Models.COMMAND_TYPES.switch:
  143. createdElement = SwitchesManagement.renderCommand(command, function_obj);
  144. break;
  145. }
  146. switch (before_after_inside) {
  147. case 1:
  148. createdElement.insertBefore(element_reference);
  149. break;
  150. case 2:
  151. createdElement.insertAfter(element_reference);
  152. break;
  153. case 3:
  154. element_reference.append(createdElement);
  155. break;
  156. }
  157. }
  158. export function genericCreateCommand (command_type) {
  159. switch (command_type) {
  160. case Models.COMMAND_TYPES.break:
  161. return new Models.Break();
  162. case Models.COMMAND_TYPES.comment:
  163. return new Models.Comment(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_value, LocalizedStrings.getUI('text_comment'), null, null, false));
  164. case Models.COMMAND_TYPES.reader:
  165. return new Models.Reader(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false));
  166. case Models.COMMAND_TYPES.writer:
  167. return new Models.Writer([new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  168. case Models.COMMAND_TYPES.attribution:
  169. return new Models.Attribution(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  170. []);
  171. case Models.COMMAND_TYPES.functioncall:
  172. return new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false), null);
  173. case Models.COMMAND_TYPES.iftrue:
  174. return new Models.IfTrue(new Models.ConditionalExpression(null), null, null);
  175. case Models.COMMAND_TYPES.repeatNtimes:
  176. return new Models.RepeatNTimes(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  177. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  178. null, new Models.ConditionalExpression(null), null, null);
  179. case Models.COMMAND_TYPES.whiletrue:
  180. return new Models.WhileTrue(new Models.ConditionalExpression(null), null);
  181. case Models.COMMAND_TYPES.dowhiletrue:
  182. return new Models.DoWhileTrue(new Models.ConditionalExpression(null), null);
  183. case Models.COMMAND_TYPES.switch:
  184. var sc = [new Models.SwitchCase(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true))];
  185. return new Models.Switch(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true), sc);
  186. }
  187. }
  188. function manageCommand (function_obj, function_container, event, command_type, function_called) {
  189. $( ".created_element" ).each(function( index ) {
  190. $(this).remove();
  191. });
  192. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  193. console.log('soltou no: ');
  194. console.log(el);
  195. console.log(el.data('fun'));
  196. // Primeiro verificar se ele soltou no espaço da função correta:
  197. var hier = el.parentsUntil(".all_functions");
  198. var esta_correto = false;
  199. var esta_na_div_correta = false;
  200. if (el.hasClass("commands_list_div")) {
  201. esta_na_div_correta = true;
  202. }
  203. for (var i = 0; i < hier.length; i++) {
  204. var temp = $(hier[i]);
  205. if (temp.hasClass("commands_list_div")) {
  206. esta_na_div_correta = true;
  207. }
  208. if (temp.data('fun') == function_obj) {
  209. esta_correto = true;
  210. break;
  211. }
  212. }
  213. if (!esta_correto) {
  214. has_element_created_draged = false;
  215. which_element_is_draged = null;
  216. return;
  217. } else {
  218. if (!esta_na_div_correta) {
  219. has_element_created_draged = false;
  220. which_element_is_draged = null;
  221. return;
  222. }
  223. }
  224. // Agora é descobrir qual o escopo para adicionar o comando:
  225. // Se o elemento clicado possuir o atributo "fun", então, é direto na div dos comandos:
  226. if (typeof el.data('fun') !== 'undefined') {
  227. // Se a lista de comandos estiver vazia, então é o primeiro.
  228. // Portanto, ele deve soltar o elemento obrigatoriamente no objeto vazio
  229. if ((el.data('fun').commands == null) || (el.data('fun').commands.length == 0)) {
  230. // pode adicionar
  231. el.data('fun').commands = [];
  232. var new_cmd = null;
  233. if (function_called == null)
  234. new_cmd = genericCreateCommand(command_type);
  235. else if (command_type == 'functioncall')
  236. new_cmd = new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, function_called, null, null, false), null);
  237. el.data('fun').commands.push(new_cmd);
  238. renderCommand(new_cmd, $(function_container).find('.commands_list_div'), 3, function_obj);
  239. } else { // Entra nesse else, caso já existam outros comandos no bloco:
  240. findNearbyCommandToAddInFunctionScope(el, event, $(function_container).find('.commands_list_div'), function_obj, command_type, function_called);
  241. }
  242. } else {
  243. console.log("soltou em um comando");
  244. // descobrir em qual comando ele soltou:
  245. var hier_find = el.parentsUntil(".commands_list_div");
  246. var hierarquia_bottom_up = [];
  247. if (typeof el.data('command') !== 'undefined') {
  248. hierarquia_bottom_up.push(el.data('command'));
  249. }
  250. for (var i = 0; i < hier_find.length; i++) {
  251. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  252. hierarquia_bottom_up.push($(hier_find[i]).data('command'));
  253. }
  254. }
  255. console.log("comando em que soltou: ");
  256. console.log(hierarquia_bottom_up[0]);
  257. console.log("hierarquia de baixo para cima na árvore, de onde ele soltou: ");
  258. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  259. console.log(hierarquia_bottom_up[i]);
  260. }
  261. // Se for do tipo break, verificar se está no contexto correto:
  262. // Caso não esteja no contexto, apenas retorna sem dar continuidade:
  263. var is_correct_context = false;
  264. if (command_type == Models.COMMAND_TYPES.break) {
  265. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  266. if ((hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.repeatNtimes)
  267. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.whiletrue)
  268. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.dowhiletrue)
  269. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.switch)) {
  270. is_correct_context = true;
  271. break;
  272. }
  273. }
  274. if (!is_correct_context) {
  275. console.error("Context not allowed to insert BREAK COMMAND!");
  276. return;
  277. }
  278. }
  279. // se a hierarquia possuir apenas um elemento, então está na raiz dos comandos:
  280. if (hierarquia_bottom_up.length == 1) {
  281. console.log('QQ1');
  282. var sub_elemento = false;
  283. for (var i = 0; i < hier_find.length; i++) {
  284. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  285. console.log('QQ2');
  286. findBeforeOrAfterCommandToAdd(hier_find[i], event, function_obj, command_type);
  287. sub_elemento = true;
  288. break;
  289. }
  290. }
  291. if (!sub_elemento) {
  292. console.log('QQ3');
  293. findBeforeOrAfterCommandToAdd(el[0], event, function_obj, command_type);
  294. }
  295. } else {
  296. console.log('QQ4');
  297. // caso exista mais de um elemento na hierarquia:
  298. if (typeof $(el).data('command') !== 'undefined') {
  299. console.log('QQ5');
  300. console.log("PPP1");
  301. insertCommandInBlockHierar(el[0], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  302. } else {
  303. console.log('QQ6');
  304. var sub_elemento = false;
  305. for (var i = 0; i < hier_find.length; i++) {
  306. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  307. console.log('QQ7');
  308. insertCommandInBlockHierar(hier_find[i], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  309. sub_elemento = true;
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. }
  316. has_element_created_draged = false;
  317. which_element_is_draged = null;
  318. }
  319. function insertCommandInBlockHierar (el, event, function_obj, command_type, hier_dom, hier_obj, function_called = null) {
  320. var el_jq = $(el);
  321. var command_parent = el_jq.data('command');
  322. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  323. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  324. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ||
  325. (el_jq.data('command').type == Models.COMMAND_TYPES.switch) ) {
  326. console.log('QQ17');
  327. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  328. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  329. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  330. console.log('QQ18');
  331. // Se não tiver outro comando ainda no bloco, só adiciona:
  332. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  333. command_parent.commands_block = [];
  334. var recentComand = null;
  335. if(function_called == null)
  336. recentComand = genericCreateCommand(command_type);
  337. else if (command_type == "functioncall")
  338. recentComand = new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, function_called, null, null, false), null);
  339. command_parent.commands_block.push(recentComand);
  340. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  341. } else { // Se já tem algum comando no bloco:
  342. findNearbyCommandToAddInBlockScope(el, event, el, function_obj, command_type, command_parent);
  343. }
  344. } else {
  345. // QUANDO FOR BLOCO DO TIPO IF OU SWITCH/CASE:
  346. addCommandToSwitchCase(event, function_obj, command_type);
  347. }
  348. } else {
  349. console.log('QQ19');
  350. // entra neste bloco, se soltou o comando sobre outro comando dentro de um subbloco:
  351. findBeforeOrAfterCommandToAddInsertBlock(el, event, function_obj, command_type);
  352. }
  353. }
  354. function findNearbyCommandToAddInBlockScope (el, event, node_list_commands, function_obj, command_type, command_parent, function_called = null) {
  355. var all_sub = $(node_list_commands).find('div.command_container');
  356. var menor_distancia = 999999999;
  357. var elemento_menor_distancia = null;
  358. var antes = true;
  359. var t_bot;
  360. var t_top;
  361. // Descobrindo o elemento mais próximo:
  362. for (var i = 0; i < all_sub.length; i++) {
  363. t_top = all_sub[i].getBoundingClientRect().top;
  364. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  365. if ((t_top - event.clientY) < menor_distancia) {
  366. menor_distancia = event.clientY - t_top;
  367. elemento_menor_distancia = all_sub[i];
  368. }
  369. }
  370. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  371. var recentComand = null;
  372. if (function_called == null)
  373. recentComand = genericCreateCommand(command_type);
  374. else if (command_type == 'functioncall')
  375. recentComand = new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, function_called, null, null, false), null);
  376. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  377. if ((borda_inferior - event.clientY) < menor_distancia) {
  378. command_parent.commands_block.push(recentComand);
  379. //
  380. renderCommand(recentComand, node_list_commands, 3, function_obj);
  381. } else {
  382. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  383. if (index > -1) {
  384. command_parent.commands_block.splice(index, 0, recentComand);
  385. }
  386. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  387. }
  388. }
  389. function findBeforeOrAfterCommandToAddInsertBlock (el, event, function_obj, command_type) {
  390. var el_jq = $(el);
  391. var command_parent = $(el.parentNode.parentNode).data('command');
  392. var command_target = el_jq.data('command');
  393. var temp_parent = $(el.parentNode.parentNode);
  394. var is_in_else = false;
  395. if (!command_parent) {
  396. command_parent = el_jq.data('command');
  397. temp_parent = el_jq;
  398. var hier = el_jq.parentsUntil(".command_container");
  399. for (var i = 0; i < hier.length; i++) {
  400. var temp = $(hier[i]);
  401. if (typeof temp.data('else') != 'undefined') {
  402. is_in_else = true;
  403. }
  404. if (typeof temp.data('command') != 'undefined') {
  405. command_parent = temp.data('command');
  406. temp_parent = temp;
  407. }
  408. }
  409. }
  410. var hier = el_jq.parentsUntil(".command_container");
  411. for (var i = 0; i < hier.length; i++) {
  412. var temp = $(hier[i]);
  413. if (typeof temp.data('else') != 'undefined') {
  414. is_in_else = true;
  415. }
  416. }
  417. if (command_parent == command_target) {
  418. var hier = el_jq.parentsUntil(".command_container");
  419. for (var i = 0; i < hier.length; i++) {
  420. var temp = $(hier[i]);
  421. if (typeof temp.data('else') !== 'undefined') {
  422. is_in_else = true;
  423. break;
  424. }
  425. }
  426. }
  427. if ((command_parent.type != Models.COMMAND_TYPES.iftrue) && (command_parent.type != Models.COMMAND_TYPES.switch)) {
  428. var hier = temp_parent.parentsUntil(".all_cases_div");
  429. console.log("vou procurar!!");
  430. for (var i = 0; i < hier.length; i++) {
  431. console.log("estou vasculhando...");
  432. var temp = $(hier[i]);
  433. if (typeof temp.data('switchcase') !== 'undefined') {
  434. console.log("encontrei");
  435. command_parent = temp.data('switchcase');
  436. is_in_else = false;
  437. break;
  438. }
  439. }
  440. }
  441. console.log('debugging:');
  442. console.log('el_jq');
  443. console.log(el_jq);
  444. console.log('command_parent');
  445. console.log(command_parent);
  446. console.log('command_target');
  447. console.log(command_target);
  448. var menor_distancia = 999999999;
  449. var antes = true;
  450. var t_bot;
  451. var t_top;
  452. t_top = el.getBoundingClientRect().top;
  453. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  454. var d_top = event.clientY - t_top; // distancia topo
  455. var d_bot = t_bot - event.clientY; // distancia baixo
  456. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  457. if (d_top < d_bot) {
  458. var recentComand = genericCreateCommand(command_type);
  459. console.log('MMM1');
  460. if (is_in_else) {
  461. console.log('MMM2');
  462. if (command_parent == command_target) {
  463. console.log('MMM3');
  464. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  465. command_parent.commands_else = [];
  466. var recentComand = genericCreateCommand(command_type);
  467. command_parent.commands_else.push(recentComand);
  468. renderCommand(recentComand, el_jq, 3, function_obj);
  469. } else { // Se já tem algum comando no bloco:
  470. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  471. }
  472. return;
  473. }
  474. console.log('MMM7');
  475. var index = command_parent.commands_else.indexOf(command_target);
  476. if (index > -1) {
  477. command_parent.commands_else.splice(index, 0, recentComand);
  478. }
  479. renderCommand(recentComand, el, 1, function_obj);
  480. } else {
  481. console.log('MMM4');
  482. if (command_parent == command_target) {
  483. console.log('Nxxxx5');
  484. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  485. command_parent.commands_block = [];
  486. console.log('SSS4');
  487. var recentComand = genericCreateCommand(command_type);
  488. command_parent.commands_block.push(recentComand);
  489. renderCommand(recentComand, el_jq, 3, function_obj);
  490. } else {
  491. console.log('SSS5');
  492. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  493. }
  494. return;
  495. }
  496. console.log('MMM6');
  497. var index = command_parent.commands_block.indexOf(command_target);
  498. if (index > -1) {
  499. command_parent.commands_block.splice(index, 0, recentComand);
  500. }
  501. renderCommand(recentComand, el, 1, function_obj);
  502. }
  503. } else {
  504. console.log('XXX1');
  505. var recentComand = genericCreateCommand(command_type);
  506. if (is_in_else) {
  507. if (command_parent == command_target) {
  508. console.log('MMM3');
  509. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  510. command_parent.commands_else = [];
  511. console.log('SSS1');
  512. var recentComand = genericCreateCommand(command_type);
  513. command_parent.commands_else.push(recentComand);
  514. renderCommand(recentComand, el_jq, 3, function_obj);
  515. } else { // Se já tem algum comando no bloco:
  516. console.log('SSS2');
  517. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  518. }
  519. return;
  520. }
  521. console.log('XXX2');
  522. var index = command_parent.commands_else.indexOf(command_target);
  523. if (index > -1) {
  524. command_parent.commands_else.splice((index + 1), 0, recentComand);
  525. }
  526. renderCommand(recentComand, el, 2, function_obj);
  527. } else {
  528. if (command_parent == command_target) {
  529. console.log('Nxxxx78');
  530. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  531. command_parent.commands_block = [];
  532. var recentComand = genericCreateCommand(command_type);
  533. command_parent.commands_block.push(recentComand);
  534. console.log('SSS6');
  535. renderCommand(recentComand, el_jq, 3, function_obj);
  536. } else {
  537. console.log('SSS7');
  538. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  539. }
  540. return;
  541. }
  542. console.log('XXX3');
  543. var index = command_parent.commands_block.indexOf(command_target);
  544. if (index > -1) {
  545. command_parent.commands_block.splice((index + 1), 0, recentComand);
  546. }
  547. renderCommand(recentComand, el, 2, function_obj);
  548. }
  549. }
  550. }
  551. function insertCommandInBlock (el, event, function_obj, command_type) {
  552. var el_jq = $(el);
  553. var command_parent = el_jq.data('command');
  554. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  555. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  556. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  557. // Se não tiver outro comando ainda no bloco, só adiciona:
  558. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  559. command_parent.commands_block = [];
  560. var recentComand = genericCreateCommand(command_type);
  561. command_parent.commands_block.push(recentComand);
  562. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  563. } else { // Se já tem algum comando no bloco:
  564. findInBlockCorrectPlace(el, event, function_obj, command_type);
  565. }
  566. } else if (el_jq.data('command').type == Models.COMMAND_TYPES.iftrue) {
  567. console.log('QQ9');
  568. // no if ou no else?
  569. var correct_div = $(document.elementFromPoint(event.pageX, event.pageY));
  570. var is_in_if = true;
  571. if (correct_div.data('if')) {
  572. is_in_if = true;
  573. } else if (correct_div.data('else')) {
  574. is_in_if = false;
  575. } else {
  576. var hier = correct_div.parentsUntil(".command_container");
  577. for (var i = 0; i < hier.length; i++) {
  578. var temp = $(hier[i]);
  579. if (typeof temp.data('if') !== 'undefined') {
  580. is_in_if = true;
  581. break;
  582. }
  583. if (typeof temp.data('else') !== 'undefined') {
  584. is_in_if = false;
  585. break;
  586. }
  587. }
  588. }
  589. if (is_in_if) {
  590. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  591. command_parent.commands_block = [];
  592. var recentComand = genericCreateCommand(command_type);
  593. command_parent.commands_block.push(recentComand);
  594. renderCommand(recentComand, el_jq.find('.commands_if'), 3, function_obj);
  595. } else { // Se já tem algum comando no bloco:
  596. findInBlockCorrectPlace(el_jq.find('.commands_if'), event, function_obj, command_type);
  597. }
  598. } else {
  599. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  600. command_parent.commands_else = [];
  601. var recentComand = genericCreateCommand(command_type);
  602. command_parent.commands_else.push(recentComand);
  603. renderCommand(recentComand, el_jq.find('.commands_else'), 3, function_obj);
  604. } else { // Se já tem algum comando no bloco:
  605. findInBlockCorrectPlace(el_jq.find('.commands_else'), event, function_obj, command_type, true);
  606. }
  607. }
  608. } else { // é do tipo switch
  609. console.log("está tentando inserir em um switch que está na raiz!");
  610. addCommandToSwitchCase(event, function_obj, command_type);
  611. }
  612. }
  613. function addCommandToSwitchCase (event, function_obj, command_type) {
  614. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  615. var which_case = el.data('switchcase');
  616. var case_div = el;
  617. if (!which_case) {
  618. var hier_find = el.parentsUntil(".all_cases_div");
  619. for (var i = 0; i < hier_find.length; i++) {
  620. if (typeof $(hier_find[i]).data('switchcase') !== 'undefined') {
  621. which_case = $(hier_find[i]).data('switchcase');
  622. case_div = $(hier_find[i]);
  623. break;
  624. }
  625. }
  626. }
  627. if (which_case.commands_block == null || which_case.commands_block.length < 1) {
  628. which_case.commands_block = [];
  629. var recentComand = genericCreateCommand(command_type);
  630. which_case.commands_block.push(recentComand);
  631. renderCommand(recentComand, case_div.find('.case_commands_block'), 3, function_obj);
  632. } else {
  633. findInBlockCorrectPlaceInSwitchCase(which_case, case_div, event, function_obj, command_type);
  634. }
  635. }
  636. function findInBlockCorrectPlaceInSwitchCase (which_case, case_div, event, function_obj, command_type) {
  637. var all_sub = case_div.find('div.command_container');
  638. var menor_distancia = 999999999;
  639. var elemento_menor_distancia = null;
  640. var antes = true;
  641. var t_bot;
  642. var t_top;
  643. // Descobrindo o elemento mais próximo:
  644. for (var i = 0; i < all_sub.length; i++) {
  645. t_top = all_sub[i].getBoundingClientRect().top;
  646. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  647. if ((t_top - event.clientY) < menor_distancia) {
  648. menor_distancia = event.clientY - t_top;
  649. elemento_menor_distancia = all_sub[i];
  650. }
  651. }
  652. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  653. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  654. if ((borda_inferior - event.clientY) < menor_distancia) {
  655. var recentComand = genericCreateCommand(command_type);
  656. which_case.commands_block.push(recentComand);
  657. renderCommand(recentComand, $(case_div.find('.case_commands_block')[0]), 3, function_obj);
  658. } else {
  659. var recentComand = genericCreateCommand(command_type);
  660. var index = which_case.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  661. if (index > -1) {
  662. which_case.commands_block.splice(index, 0, recentComand);
  663. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  664. }
  665. }
  666. }
  667. function findInBlockCorrectPlace (el, event, function_obj, command_type, is_in_else = false) {
  668. var el_jq = $(el);
  669. var all_sub = el_jq.find('div.command_container');
  670. var menor_distancia = 999999999;
  671. var elemento_menor_distancia = null;
  672. var antes = true;
  673. var t_bot;
  674. var t_top;
  675. // Descobrindo o elemento mais próximo:
  676. for (var i = 0; i < all_sub.length; i++) {
  677. t_top = all_sub[i].getBoundingClientRect().top;
  678. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  679. if ((t_top - event.clientY) < menor_distancia) {
  680. menor_distancia = event.clientY - t_top;
  681. elemento_menor_distancia = all_sub[i];
  682. }
  683. }
  684. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  685. console.log("menor_distancia: ");
  686. console.log(elemento_menor_distancia);
  687. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  688. if ((borda_inferior - event.clientY) < menor_distancia) {
  689. console.log('QQ11');
  690. var recentComand = genericCreateCommand(command_type);
  691. var command_parent = el_jq.data('command');
  692. if (is_in_else) {
  693. console.log('QQ15');
  694. command_parent.commands_else.push(recentComand);
  695. console.log('el_jq');
  696. console.log(el_jq);
  697. console.log("$(el_jq.find('.commands_else')[0]):: ");
  698. console.log($(el_jq.find('.commands_else')[0]));
  699. renderCommand(recentComand, el_jq, 3, function_obj);
  700. } else {
  701. console.log('QQ16');
  702. command_parent.commands_block.push(recentComand);
  703. renderCommand(recentComand, $(el_jq.find('.block_commands')[0]), 3, function_obj);
  704. }
  705. } else {
  706. console.log('QQ12');
  707. var recentComand = genericCreateCommand(command_type);
  708. var command_parent = el_jq.data('command');
  709. if (is_in_else) {
  710. var index = command_parent.commands_else.indexOf($(elemento_menor_distancia).data('command'));
  711. if (index > -1) {
  712. command_parent.commands_else.splice(index, 0, recentComand);
  713. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  714. }
  715. } else {
  716. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  717. if (index > -1) {
  718. command_parent.commands_block.splice(index, 0, recentComand);
  719. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  720. }
  721. }
  722. }
  723. }
  724. function findBeforeOrAfterCommandToAdd (el, event, function_obj, command_type) {
  725. switch ($(el).data('command').type) {
  726. case Models.COMMAND_TYPES.iftrue:
  727. case Models.COMMAND_TYPES.switch:
  728. case Models.COMMAND_TYPES.repeatNtimes:
  729. case Models.COMMAND_TYPES.whiletrue:
  730. case Models.COMMAND_TYPES.dowhiletrue:
  731. insertCommandInBlock(el, event, function_obj, command_type);
  732. return;
  733. }
  734. var menor_distancia = 999999999;
  735. var antes = true;
  736. var t_bot;
  737. var t_top;
  738. t_top = el.getBoundingClientRect().top;
  739. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  740. var d_top = event.clientY - t_top; // distancia topo
  741. var d_bot = t_bot - event.clientY; // distancia baixo
  742. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  743. if (d_top < d_bot) {
  744. var recentComand = genericCreateCommand(command_type);
  745. var index = function_obj.commands.indexOf($(el).data('command'));
  746. if (index > -1) {
  747. function_obj.commands.splice(index, 0, recentComand);
  748. }
  749. renderCommand(recentComand, el, 1, function_obj);
  750. } else {
  751. var recentComand = genericCreateCommand(command_type);
  752. var index = function_obj.commands.indexOf($(el).data('command'));
  753. if (index > -1) {
  754. function_obj.commands.splice((index + 1), 0, recentComand);
  755. }
  756. renderCommand(recentComand, el, 2, function_obj);
  757. }
  758. }
  759. function findNearbyCommandToAddInFunctionScope (el, event, node_list_commands, function_obj, command_type, function_called = null) {
  760. var all_sub = $(node_list_commands).find('div.command_container');
  761. var menor_distancia = 999999999;
  762. var elemento_menor_distancia = null;
  763. var antes = true;
  764. var t_bot;
  765. var t_top;
  766. // Descobrindo o elemento mais próximo:
  767. for (var i = 0; i < all_sub.length; i++) {
  768. t_top = all_sub[i].getBoundingClientRect().top;
  769. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  770. if ((t_top - event.clientY) < menor_distancia) {
  771. menor_distancia = event.clientY - t_top;
  772. elemento_menor_distancia = all_sub[i];
  773. }
  774. }
  775. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  776. var recentComand = null;
  777. if (function_called == null)
  778. recentComand = genericCreateCommand(command_type);
  779. else if (command_type == 'functioncall')
  780. recentComand = new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, function_called, null, null, false), null);
  781. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  782. if ((borda_inferior - event.clientY) < menor_distancia) {
  783. function_obj.commands.push(recentComand);
  784. //
  785. renderCommand(recentComand, node_list_commands, 3, function_obj);
  786. } else {
  787. var index = function_obj.commands.indexOf($(elemento_menor_distancia).data('command'));
  788. if (index > -1) {
  789. function_obj.commands.splice(index, 0, recentComand);
  790. }
  791. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  792. }
  793. }
  794. //******************************************************
  795. //* DOUGLAS
  796. //******************************************************
  797. createFloatingCommand = function (function_obj, function_container, command_type, mouse_event) {
  798. var floatingObject;
  799. switch (command_type) {
  800. case Models.COMMAND_TYPES.break:
  801. floatingObject = BreaksManagement.createFloatingCommand();
  802. break;
  803. case Models.COMMAND_TYPES.comment:
  804. floatingObject = CommentsManagement.createFloatingCommand();
  805. break;
  806. case Models.COMMAND_TYPES.reader:
  807. floatingObject = ReadersManagement.createFloatingCommand();
  808. break;
  809. case Models.COMMAND_TYPES.writer:
  810. floatingObject = WritersManagement.createFloatingCommand();
  811. break;
  812. case Models.COMMAND_TYPES.attribution:
  813. floatingObject = AttributionsManagement.createFloatingCommand();
  814. break;
  815. case Models.COMMAND_TYPES.iftrue:
  816. floatingObject = IftruesManagement.createFloatingCommand();
  817. break;
  818. case Models.COMMAND_TYPES.repeatNtimes:
  819. floatingObject = RepeatNtimesManagement.createFloatingCommand();
  820. break;
  821. case Models.COMMAND_TYPES.whiletrue:
  822. floatingObject = WhiletruesManagement.createFloatingCommand();
  823. break;
  824. case Models.COMMAND_TYPES.dowhiletrue:
  825. floatingObject = DowhiletruesManagement.createFloatingCommand();
  826. break;
  827. case Models.COMMAND_TYPES.switch:
  828. floatingObject = SwitchesManagement.createFloatingCommand();
  829. break;
  830. case Models.COMMAND_TYPES.functioncall:
  831. floatingObject = FunctioncallsManagement.createFloatingCommand();
  832. break;
  833. }
  834. floatingObject.draggable().appendTo("body");
  835. floatingObject.on('dragstart', function(e) {
  836. console.log("TESTEEEEE");
  837. }).trigger('dragstart');
  838. floatingObject.mouseup(function(evt) {
  839. console.log('mouseup')
  840. $(".commands_list_div, .commands_list_div, .block_commands, .command_container").off('mouseenter mouseleave');
  841. manageCommand(function_obj, function_container, evt, command_type);
  842. });
  843. floatingObject.css("position", "absolute");
  844. mouse_event.type = "mousedown.draggable";
  845. mouse_event.target = floatingObject[0];
  846. floatingObject.css("left", mouse_event.pageX - 15);
  847. floatingObject.css("top", mouse_event.pageY - 15);
  848. floatingObject.trigger(mouse_event);
  849. }
  850. /*
  851. manageCommand = function(function_obj, function_container, event, command_type) {
  852. $( ".created_element" ).each(function( index ) {
  853. $(this).remove();
  854. });
  855. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  856. console.log('soltou no: ');
  857. console.log(el);
  858. console.log(el.data('fun'));
  859. // Primeiro verificar se ele soltou no espaço da função correta:
  860. var hier = el.parentsUntil(".all_functions");
  861. var esta_correto = false;
  862. var esta_na_div_correta = false;
  863. if (el.hasClass("commands_list_div")) {
  864. esta_na_div_correta = true;
  865. }
  866. for (var i = 0; i < hier.length; i++) {
  867. var temp = $(hier[i]);
  868. if (temp.hasClass("commands_list_div")) {
  869. esta_na_div_correta = true;
  870. }
  871. if (temp.data('fun') == function_obj || function_obj == null) {
  872. function_obj = temp.data('fun');
  873. esta_correto = true;
  874. break;
  875. }
  876. }
  877. if (!esta_correto) {
  878. has_element_created_draged = false;
  879. which_element_is_draged = null;
  880. return;
  881. } else {
  882. if (!esta_na_div_correta) {
  883. has_element_created_draged = false;
  884. which_element_is_draged = null;
  885. return;
  886. }
  887. }
  888. // Agora é descobrir qual o escopo para adicionar o comando:
  889. // Se o elemento clicado possuir o atributo "fun", então, é direto na div dos comandos:
  890. if (typeof el.data('fun') !== 'undefined') {
  891. // Se a lista de comandos estiver vazia, então é o primeiro.
  892. // Portanto, ele deve soltar o elemento obrigatoriamente no objeto vazio
  893. if ((el.data('fun').commands == null) || (el.data('fun').commands.length == 0)) {
  894. // pode adicionar
  895. el.data('fun').commands = [];
  896. var new_cmd = genericCreateCommand(command_type);
  897. el.data('fun').commands.push(new_cmd);
  898. renderCommand(new_cmd, $(function_container).find('.commands_list_div'), 3, function_obj);
  899. } else { // Entra nesse else, caso já existam outros comandos no bloco:
  900. findNearbyCommandToAddInFunctionScope(el, event, $(function_container).find('.commands_list_div'), function_obj, command_type);
  901. }
  902. } else {
  903. console.log("soltou em um comando");
  904. // descobrir em qual comando ele soltou:
  905. var hier_find = el.parentsUntil(".commands_list_div");
  906. var hierarquia_bottom_up = [];
  907. if (typeof el.data('command') !== 'undefined') {
  908. hierarquia_bottom_up.push(el.data('command'));
  909. }
  910. for (var i = 0; i < hier_find.length; i++) {
  911. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  912. hierarquia_bottom_up.push($(hier_find[i]).data('command'));
  913. }
  914. }
  915. console.log("comando em que soltou: ");
  916. console.log(hierarquia_bottom_up[0]);
  917. console.log("hierarquia de baixo para cima na árvore, de onde ele soltou: ");
  918. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  919. console.log(hierarquia_bottom_up[i]);
  920. }
  921. // se a hierarquia possuir apenas um elemento, então está na raiz dos comandos:
  922. if (hierarquia_bottom_up.length == 1) {
  923. var sub_elemento = false;
  924. for (var i = 0; i < hier_find.length; i++) {
  925. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  926. findBeforeOrAfterCommandToAdd(hier_find[i], event, function_obj, command_type);
  927. sub_elemento = true;
  928. break;
  929. }
  930. }
  931. if (!sub_elemento) {
  932. findBeforeOrAfterCommandToAdd(el[0], event, function_obj, command_type);
  933. }
  934. } else {
  935. // caso exista mais de um elemento na hierarquia:
  936. if (typeof $(el).data('command') !== 'undefined') {
  937. console.log("PPP1");
  938. insertCommandInBlockHierar(el[0], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  939. } else {
  940. var sub_elemento = false;
  941. for (var i = 0; i < hier_find.length; i++) {
  942. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  943. insertCommandInBlockHierar(hier_find[i], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  944. sub_elemento = true;
  945. break;
  946. }
  947. }
  948. }
  949. }
  950. }
  951. has_element_created_draged = false;
  952. which_element_is_draged = null;
  953. }
  954. */
  955. export function prepareManageCommand (function_obj, function_container, evt, command_type, function_called = null) {
  956. manageCommand(function_obj, function_container, evt, command_type, function_called);
  957. }